🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.netdev
Article #4231

Re: [PATCH bpf-next 04/38] bpf: don't hand out a linked element as extra element

#4231
From: bot+bpf-ci
Date: Sat, 17 Oct 2026 01:52
100 lines
9554 bytes
--===============0001666948501514510==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit

> bpf: don't hand out a linked element as extra element

TL;DR:
- htab_map_update_elem() fills the extra-elem slot before cancelling l_old's
  fields; a nested update may reuse it, so a live element's timer could be
  cancelled (not memory-unsafe)
- comment style nit

> diff --git a/kernel/bpf/hashtab.c b/kernel/bpf/hashtab.c
> index 9d4791ee1741..923fec694747 100644
> --- a/kernel/bpf/hashtab.c
> +++ b/kernel/bpf/hashtab.c

[ ... ]

> @@ -1459,11 +1465,14 @@ static long htab_map_update_elem(struct bpf_map *map, void *key, void *value,
>  	if (l_old) {
>  		hlist_nulls_del_rcu(&l_old->hash_node);
>  
> -		/* l_old has already been stashed in htab->extra_elems, cancel
> -		 * its reusable special fields before it is available for reuse.
> +		/* l_old replaces the extra element that alloc_htab_elem() took,
> +		 * cancel its reusable special fields before the next update on
> +		 * this cpu reuses it.
>  		 */

this isn't a bug, but this comment is rewritten completely and still keeps
its text on the opening line.  Could the opening /* go on its own line, as
is preferred in kernel/bpf/?

  /*
   * l_old replaces the extra element that alloc_htab_elem() took,
   * cancel its reusable special fields before the next update on
   * this cpu reuses it.
   */

> -		if (htab_is_prealloc(htab))
> +		if (htab_is_prealloc(htab)) {
> +			WRITE_ONCE(*this_cpu_ptr(htab->extra_elems), l_old);
>  			check_and_cancel_fields(htab, l_old);

The new comment says l_old's special fields are cancelled before the next
update on this cpu reuses it.  Is that what this code does?  The slot is
filled first and the fields are cancelled afterwards:

    WRITE_ONCE(*this_cpu_ptr(htab->extra_elems), l_old);
    check_and_cancel_fields(htab, l_old);

Can a nested update on this cpu run between these two calls?  That could be
an NMI (perf_event program), or an fentry program on bpf_obj_cancel_fields(),
which check_and_cancel_fields() calls after the slot is filled and which
tools/testing/selftests/bpf/progs/htab_update.c already attaches an updating
program to.

If that nested htab_map_update_elem() updates an existing key k2 in another
bucket, alloc_htab_elem() takes l_old from the slot.  copy_map_value() skips
the special fields, so the new element still carries the bpf_timer/bpf_wq/
task_work state of k1's replaced value.

The nested update then links l_old into k2's bucket.  When the outer update
resumes, check_and_cancel_fields(htab, l_old) runs:

    bpf_obj_free_timer() -> bpf_async_cancel_and_free()

on what is now k2's live element.

Timers are allowed in hash maps used by tracing/perf_event/fentry programs
(check_map_prog_compatibility() only restricts spin_lock and graph roots), so
this looks reachable.  In that window bpf_timer_init() on k2 returns -EBUSY
(__bpf_async_init() finds the stale async->cb), and any timer armed through
k2 is cancelled and freed right after.  This doesn't look memory-unsafe, and
it is narrower than before the patch, where the slot was filled in
alloc_htab_elem().

The order is also the reverse of free_htab_elem(), which cancels before
pcpu_freelist_push().  Would swapping the two statements (cancel first, then
WRITE_ONCE) make the comment true?  A nested update in that window would then
fail with -EBUSY, like the rest of the window this patch closes.

If the current order is intended, to keep the -EBUSY window short, should the
comment say that cancellation can happen after a nested reuse?

> +		}
>  	}
>  	htab_unlock_bucket(b, flags);
>  	if (l_old && !htab_is_prealloc(htab))


---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/38991548713
--===============0001666948501514510==--
Technical Details
Message-ID: <c6e3cf4c8e650803377f900f18c4ffc183e5883781dcd2015c0898d598928f53@mail.kernel.org>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-347638-linux-kernel-netdev=dewy.ru
References: <20261017012058.355787-5-alexei.starovoitov@gmail.com>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-347638-linux-kernel-netdev=dewy.ru
From: bot+bpf-ci@kernel.org
Newsgroups: linux.kernel.netdev
Subject: Re: [PATCH bpf-next 04/38] bpf: don't hand out a linked element as extra element
Date: Sat, 17 Oct 2026 01:52:45 +0000 (UTC)
Organization: Usenet Network
Message-ID: <c6e3cf4c8e650803377f900f18c4ffc183e5883781dcd2015c0898d598928f53@mail.kernel.org>
References: <20261017012058.355787-5-alexei.starovoitov@gmail.com>
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="===============0001666948501514510=="
Injection-Info: usenet.network;
	logging-data="2939993"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:3rBqX+8azhXNs3mhSpzZ964nb7k= sha256:MLPam9VyJUQL+8SuIOTXoWnQJ8kGeKZ5tr+Jye1Zuw4=
	sha1:E37Tmf2KJqh6XGlioBB92IV6fkE= sha256:crIu2+ir9nlJAVQMvnZRTSbXs30BfStPdiEDrCjXAuw=
List-ID: <netdev.vger.kernel.org>
X-Mailpost-Empty-Hdrs: X-Spam-Level
X-Received: from localhost (localhost [127.0.0.1])
	by mail.dewy.ru (Postfix) with ESMTP id 0615A110662; Sat, 17 Oct 2026 04:53:03 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.64
X-Spam-Status: No, score=-3.64 tagged_above=-9999 required=6.31
	tests=[DCC_REPUT_70_89=0.1, DKIMWL_WL_HIGH=-0.24, DKIM_SIGNED=0.1,
	DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1,
	MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001,
	SPF_PASS=-0.001] autolearn=ham autolearn_force=no
X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key)
	header.d=kernel.org
X-Received: from mail.dewy.ru ([127.0.0.1])
	by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024)
	with ESMTP id Omi_RNz_psA2;
	Sat, 17 Oct 2026 04:52:59 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.105.105.114; helo=tor.lore.kernel.org; envelope-from=netdev+bounces-347638-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> 
X-Authentication-Results: OpenDMARC; dmarc=pass (p=quarantine dis=none) header.from=kernel.org
X-Received: from tor.lore.kernel.org (tor.lore.kernel.org [172.105.105.114])
	by mail.dewy.ru (Postfix) with ESMTPS id 9BBA9110668; Sat, 17 Oct 2026 04:52:51 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.90.174.1])
	by tor.lore.kernel.org (Postfix) with ESMTP id EE609329B9; Sat, 17 Oct 2026 01:52:48 +0000 (UTC)
X-Received: from localhost.localdomain (localhost.localdomain [127.0.0.1])
	by smtp.subspace.kernel.org (Postfix) with ESMTP id 77EA83815DC;
	Sat, 17 Oct 2026 01:52:48 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
	dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="e4X/eiZB"
X-Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18])
	(using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits))
	(No client certificate requested)
	by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5D34C303A04;
	Sat, 17 Oct 2026 01:52:47 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18
X-ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116;
	t=1792201968; cv=none; b=HGX32hZyFF2tQk3YWC7+upvRYSj/JkIyClRJeoUIDIyCdO69nT46Cj/LvjGy4fRLDWCAF8KsT5rHlUXgLSoG8W9zBusQ/tIqDHJhtiQlTM4dSuksdMfolhMseUbhpqnVBnrhHldrBLW/EpO1OX07lJ75s2L9jtVbyxkPILfiYdw=
X-ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org;
	s=arc-20240116; t=1792201968; c=relaxed/simple;
	bh=+bZI8wly5eDGayryvsGqobJTAS+537SL6h1SyJlRTIE=;
	h=Content-Type:MIME-Version:Message-Id:In-Reply-To:References:
	 Subject:From:To:Cc:Date; b=LHg6uabnaKNsLmbp/wHQNyxgi3AFU6OVD0rLGSRxUY5vEna48dqVoMnEYCQZT+s+9n5y5JCXJMBG2hXqXYKstNUjipUpW7qqQfcXECGNYXF0r1sdumSepESZ88ZTqH+m4nYXntXLR9EnsQ3+W9dxw0JD30T/FfUT88C4jjZdDHM=
X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=e4X/eiZB; arc=none smtp.client-ip=100.103.45.18
X-Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8D5131F000C4;
	Sat, 17 Oct 2026 01:52:45 +0000 (UTC)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org;
	s=k20260515; t=1792201966;
	bh=NPwlg68vom/2k1L+SuKMG8v4a0E3d3PLIOOfY4gmI/c=;
	h=In-Reply-To:References:Subject:From:To:Cc:Date;
	b=e4X/eiZBdsTNlRz0aeltuH2MfViOeRX9D3vOX+4SFXm0C35Lu3T72YWtdl40wdlDu
	 aCR1WbOQh6H9PNgBjyhKCnBxb9x4jAox6FxuGuMsCcwsTAAWyNEEyZOp6AkGrBPErl
	 YtTwlr8zitHAbylSkjMYTXniJoXQZtPhPdSdVgqRjZTsglu1WYHM2ItQlXQns9MPn2
	 pqfSt5uNpozj98vkCvKZ3hOoCZ4TyqbQW5RTJSQgA1Uz2pLnUPQiUeQckHMPFeXxto
	 gfLawOwxoOTBrpgPiFeglpb7QsywHj8MmjYEuaLQrRAg0OkVrFj/k3AL5CpdiW+BYJ
	 2xu1x86RSYV8A==
X-Precedence: bulk
X-Mailing-List: netdev@vger.kernel.org
X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org>
X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org>
X-In-Reply-To: <20261017012058.355787-5-alexei.starovoitov@gmail.com>
X-Cc: daniel@iogearbox.net,andrii@kernel.org,eddyz87@gmail.com,memxor@gmail.com,netdev@vger.kernel.org,josef@toxicpanda.com,mason@kernel.org,ast@kernel.org,andrii@kernel.org,daniel@iogearbox.net,martin.lau@kernel.org,eddyz87@gmail.com,yonghong.song@linux.dev,mason@kernel.org,ihor.solodrai@linux.dev