🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.netdev
Article #4356

[PATCH bpf-next v2 02/43] bpf: forbid sleeping in hash map for_each callbacks

#4356
From: Alexei Starovoitov
Date: Sat, 17 Oct 2026 16:37
145 lines
13690 bytes
From: Josef Bacik <josef@toxicpanda.com>

A sleepable program (e.g. SEC("syscall"), lsm.s, fentry.s) can pass a
callback to bpf_for_each_map_elem() on a hash map and call a sleepable
helper or kfunc such as bpf_copy_from_user() from it. The verifier
accepts this, but at run time the callback is invoked from inside an RCU
read-side critical section:

    bpf_for_each_hash_elem()
      rcu_read_lock();
      callback_fn(map, key, val, ctx, 0);
        bpf_copy_from_user()       <- may fault and sleep
      rcu_read_unlock();

bpf_each_rhash_elem() does the same for BPF_MAP_TYPE_RHASH. Sleeping
there triggers the "Voluntary context switch within RCU read-side
critical section!" warning, delays grace periods, and with
CONFIG_PREEMPT_RCU=n silently ends the read-side critical section that
the bucket walk relies on.

Synchronous callbacks are verified with the caller's state, so
kernel/bpf/verifier.c:in_sleepable_context() only looks at the program's
sleepability and the locks the program took itself, and does not know
that the helper holds rcu_read_lock() around the callback.

With a SEC("syscall") program whose callback reads a page that has to be
read from disk first:

  BUG: sleeping function called from invalid context at include/linux/uaccess.h:169
  RCU nest depth: 1, expected: 0
   __might_fault+0x29/0x80
   _copy_from_user+0x26/0x90
   bpf_copy_from_user+0x23/0x60
   bpf_prog_17d44fa39eebd6cc_cb+0x35/0x46
   bpf_for_each_hash_elem+0xdb/0x280

Fix by marking the callback frame of hash map iteration as running under
RCU and making in_sleepable_context() false while such a frame is on the
call stack, so sleepable helpers, kfuncs and global functions are
rejected there as inside bpf_rcu_read_lock(). The rules for trusting RCU
protected pointers are left alone, which is conservative and keeps
callbacks that verify today and do not sleep working. The flag is
compared in func_states_equal() so that pruning never mixes the two
contexts. Array map callbacks run without any lock and keep the caller's
context.

Fixes: 314ee05e2fc6 ("bpf: Add hashtab support for bpf_for_each_map_elem() helper")
Fixes: 818e00848227 ("bpf: Implement iteration ops for resizable hashtab")
Signed-off-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
---
 include/linux/bpf_verifier.h |  5 +++++
 kernel/bpf/states.c          |  3 +++
 kernel/bpf/verifier.c        | 28 +++++++++++++++++++++++++++-
 3 files changed, 35 insertions(+), 1 deletion(-)

diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h
index 112ca1511da4..816948df8fff 100644
--- a/include/linux/bpf_verifier.h
+++ b/include/linux/bpf_verifier.h
@@ -379,6 +379,11 @@ struct bpf_func_state {
 	bool in_async_callback_fn;
 	bool in_exception_callback_fn;
 	bool no_stack_arg_load;
+	/*
+	 * The helper invokes this callback under rcu_read_lock(), so the
+	 * callback and its callees run in an RCU read-side critical section.
+	 */
+	bool in_rcu_callback_fn;
 	/* For callback calling functions that limit number of possible
 	 * callback executions (e.g. bpf_loop) keeps track of current
 	 * simulated iteration number.
diff --git a/kernel/bpf/states.c b/kernel/bpf/states.c
index 953700822fa7..b3d250caaaa6 100644
--- a/kernel/bpf/states.c
+++ b/kernel/bpf/states.c
@@ -995,6 +995,9 @@ static bool func_states_equal(struct bpf_verifier_env *env, struct bpf_func_stat
 	if (!old->no_stack_arg_load && cur->no_stack_arg_load)
 		return false;
 
+	if (old->in_rcu_callback_fn != cur->in_rcu_callback_fn)
+		return false;
+
 	for (i = 0; i < MAX_BPF_REG; i++)
 		if (((1 << i) & live_regs) &&
 		    !regsafe(env, &old->regs[i], &cur->regs[i],
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index ddfa0c6d2755..883cf148235c 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -11530,6 +11530,19 @@ static int set_map_elem_callback_state(struct bpf_verifier_env *env,
 	if (err)
 		return err;
 
+	switch (map->map_type) {
+	case BPF_MAP_TYPE_HASH:
+	case BPF_MAP_TYPE_PERCPU_HASH:
+	case BPF_MAP_TYPE_LRU_HASH:
+	case BPF_MAP_TYPE_LRU_PERCPU_HASH:
+	case BPF_MAP_TYPE_RHASH:
+		/* Hash tables run the callback under rcu_read_lock(). */
+		callee->in_rcu_callback_fn = true;
+		break;
+	default:
+		break;
+	}
+
 	callee->in_callback_fn = true;
 	callee->callback_ret_range = retval_range(0, 1);
 	return 0;
@@ -12273,10 +12286,22 @@ int bpf_get_helper_proto(struct bpf_verifier_env *env, int func_id,
 	return *ptr && (*ptr)->func ? 0 : -EINVAL;
 }
 
+/* Is any frame on the current path a callback that runs under RCU? */
+static bool in_rcu_callback(struct bpf_verifier_env *env)
+{
+	struct bpf_verifier_state *state = env->cur_state;
+	int i;
+
+	for (i = 0; i <= state->curframe; i++)
+		if (state->frame[i]->in_rcu_callback_fn)
+			return true;
+	return false;
+}
+
 /* Check if we're in a sleepable context. */
 static inline bool in_sleepable_context(struct bpf_verifier_env *env)
 {
-	return !in_rcu_cs(env);
+	return !in_rcu_cs(env) && !in_rcu_callback(env);
 }
 
 static const char *non_sleepable_context_description(struct bpf_verifier_env *env)
@@ -12290,6 +12315,7 @@ static const char *non_sleepable_context_description(struct bpf_verifier_env *en
 			return "IRQ-disabled region";
 		if (env->cur_state->active_locks)
 			return "lock region";
+		return "callback running under rcu_read_lock";
 	}
 	return "non-sleepable prog";
 }
-- 
2.55.0
Technical Details
Message-ID: <20261017163745.3341787-3-alexei.starovoitov@gmail.com>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-347776-linux-kernel-netdev=dewy.ru
References: <20261017163745.3341787-1-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-347776-linux-kernel-netdev=dewy.ru
From: alexei.starovoitov@gmail.com (Alexei Starovoitov)
Newsgroups: linux.kernel.netdev
Subject: [PATCH bpf-next v2 02/43] bpf: forbid sleeping in hash map for_each callbacks
Date: Sat, 17 Oct 2026 16:37:04 +0000
Organization: Usenet Network
Message-ID: <20261017163745.3341787-3-alexei.starovoitov@gmail.com>
References: <20261017163745.3341787-1-alexei.starovoitov@gmail.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
	logging-data="3085910"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:UdhDnWI36tkNXymRWHnW/47XzuU= sha256:sJRYlinvAb2TAURThSMxcqc/6mYJzykcKO/9/yYyaSA=
	sha1:XeYn7/KSQo8VKVvRGJyB0FUNEZc= sha256:gKnfUXTaMHqxzHV3JEXf1iYSN4RhH6UBD74KA86UzW4=
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 7C9EC110713; Sat, 17 Oct 2026 19:44:28 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.048
X-Spam-Status: No, score=-3.048 tagged_above=-9999 required=6.31
	tests=[DCC_REPUT_70_89=0.1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1,
	DKIM_VALID_AU=-0.1, FREEMAIL_FORGED_FROMDOMAIN=0.001,
	FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=0.25,
	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=gmail.com
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 OrTKtyScUtRA;
	Sat, 17 Oct 2026 19:44:26 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=104.64.211.4; helo=sin.lore.kernel.org; envelope-from=netdev+bounces-347776-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> 
X-Authentication-Results: OpenDMARC; dmarc=fail (p=none dis=none) header.from=gmail.com
X-Received: from sin.lore.kernel.org (sin.lore.kernel.org [104.64.211.4])
	by mail.dewy.ru (Postfix) with ESMTPS id 6E66D1106F3; Sat, 17 Oct 2026 19:44:17 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.90.174.1])
	by sin.lore.kernel.org (Postfix) with ESMTP id 942912DEA1; Sat, 17 Oct 2026 16:38:06 +0000 (UTC)
X-Received: from localhost.localdomain (localhost.localdomain [127.0.0.1])
	by smtp.subspace.kernel.org (Postfix) with ESMTP id C9E803AAF69;
	Sat, 17 Oct 2026 16:38:03 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
	dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="EytWKGHL"
X-Received: from mail-pj1-f54.google.com (mail-pj1-f54.google.com [209.85.216.54])
	(using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits))
	(No client certificate requested)
	by smtp.subspace.kernel.org (Postfix) with ESMTPS id 600D43AAF43
	for <netdev@vger.kernel.org>; Sat, 17 Oct 2026 16:38:02 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.54
X-ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116;
	t=1792255083; cv=none; b=R6w5lClHVALrPbQkrJJ64x6Wgd1dX8yNEJC/2uKI5P2vYVe/4BaYHt1uYEIvpuvqYqj/+8EKvgNYcx/GM3JrpfB76J02/FCcJtJBMIl8H1xowfPkjwEbcxH5/3sjAi0Ek+OjML3+GuqtfRL7XA4J3U2YBjSflWaz4hbAvumrXzI=
X-ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org;
	s=arc-20240116; t=1792255083; c=relaxed/simple;
	bh=FKqteCSXO1CbRVOSlk9/EvC/7gM2fsr0/PRO0BW8AjQ=;
	h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References:
	 MIME-Version; b=hY3l4QTJJ5aI9PLxKEFIBT1O4wTbMGiFNl136xwxhQ8SD4x8JdPnB2f5BSa9+LhOwGQ53CK2RaFiYM7vqVoTswdtql8UHBz/k+hPk8XnlZ7jhwjE6pECnG60O/a+GRlMwOw7Wco1USRiKILHKdhN7e3qCpwS7JhmYQ73B+lBlC8=
X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=EytWKGHL; arc=none smtp.client-ip=209.85.216.54
X-Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com
X-Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com
X-Received: by mail-pj1-f54.google.com with SMTP id 98e67ed59e1d1-3acc96a256dso577591a91.3
        for <netdev@vger.kernel.org>; Sat, 17 Oct 2026 09:38:02 -0700 (PDT)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20251104; t=1792255082; x=1792859882; darn=vger.kernel.org;
        h=content-transfer-encoding:mime-version:references:in-reply-to
         :message-id:date:subject:cc:to:from:from:to:cc:subject:date
         :message-id:reply-to:content-type;
        bh=zLOTcel4mG6H6gLmD7SemEC02pAxD8Wu1dMrbttEvSQ=;
        b=EytWKGHLIlscd6Iv7ivBDhksN1qYR4Isn0+r3DKD0ldR4FeYXtCd60L8GxEqsac8ZH
         t8HS9mjiDWTbOJn68fq1VB+vH28eQ2S9D03ZWhNq2o925gv7dIKdQ0xnmt9T5Ojqm9/L
         T6b3IpF8FA0ryD7nyhSbi2yHT7QdL5zZu+tpNDamSQrIs/WNIzTYDGbl1yEF/hFFUC3G
         vXlexjQhDkKAOfjLbs+LDnH/wMr+9eNx6uLsccFjCVvoEhTvLafFITvMIlkCewo/5DCB
         4BnLjWojAFA1x4DRLl3+cPbaiMjnU+xJrNulfNvHFmb8+8nWky8zvpzJ2goA2sI+k7jG
         MN8A==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20260707; t=1792255082; x=1792859882;
        h=content-transfer-encoding:mime-version:references:in-reply-to
         :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from
         :to:cc:subject:date:message-id:reply-to:content-type;
        bh=zLOTcel4mG6H6gLmD7SemEC02pAxD8Wu1dMrbttEvSQ=;
        b=YXGlBWb592S+1gcU8hvXLnO3SqivyrSbegF9QsK0p3yjAfemWzZj1OnD9x3yX6DbVM
         HHTTR5Y7MjnjuTLw72fSlj1MY2y8/OW7vVhEEyRjUVqmml/Id0Hg8lCetVfpMxh7r07a
         C0Tac0XdIQOumzzPFPVEDL6rrDc1vop5kxuCA37exLqu0gxbcP2Oo0jH+iNxVnFrMwa1
         lZ6c1Go2chXfoEMY/EH4Qr6li9CRn4a2fyO106U86mDCSzXEhbEIWT7//BMbfiVQZ43s
         LSMqm3KPIwtg06xYJgER3DKz3weU3QWDT4z0i8theXpurEgQEcS6mJnZ3uVZdJmUDy4n
         7isA==
X-Forwarded-Encrypted: i=1; AKwUvBy1r3MsADMWWeSucWcohjtDBzxUAuM633aOKWfBT34WEmZzEeI1Uptz8qz2TBOtUe5CNRafQCY=@vger.kernel.org
X-Gm-Message-State: AFq9FYIk1mZeQ3P+LEzZnWVXgGj/llGhQtErGP60zqlogxu1Ktad/jpB
	wjAC7cVxTqafKVoNLRZPgrXJXJM7h6OpN/c0zuNb/q7MsTBxJ0Ewr9E8
X-Gm-Gg: AYBFou29lCS1x2weMMMDOvD4shgb+HAm1TRQ3TsY+Cda/N4gMovzGrvGDiPTCP1rrsR
	1YRaYQ0+JN+iBlY7yULCfDamaGewgPGF1n6oOidG/zlfmElLqAOc86t1eFevb5DgGhTP1SHPZu3
	+WWtIRVo2TlbcPiY7H0Vdei8FyEQhn4S0K8HOPepk+Qijh0aBQw2b72ICrAcghU1nYYh6Rpsq6H
	rfVlJSPZPeBlB4y8bMXob+Vbhp+CG8SOrJFRPzwwSqia18OzuHIZusQTLgzZjwndarktCVsQllJ
	YjYE9+Cc7Go70bMCE6VjSZaGXWs/HAUsRaRQ3GjcObuGdOHEkc+ireeI1QiNloy+KB0dYKOWbHR
	9APA4oHEEvajDgi58upbIvDmHBpOrmqEffkujudUdxNsFagRxo2IIQl5E0qZ+nNSBDuFkc2Qs4s
	Ud165VR/aUW4BczCYRY87VRymO0uGpYW3c8L80AZxBFWdlt3hm0sVsHtRdigezYdX9OAR2ijghn
	CiVHb/TKBK8ran37q0uv9vIOmY6OTnW4MwX42Jm5/NyV7YN8SU2RKYUoT1/EgC6eE7zqF8ocLcu
	B3/+/xBQcvXzCxS0zLsY6qx0qyuPsY/1hg+EI1d0Fi4=
X-Received: by 2002:a17:90b:278d:b0:3ac:9706:772c with SMTP id 98e67ed59e1d1-3acef5cc100mr2799243a91.60.1792255081507;
        Sat, 17 Oct 2026 09:38:01 -0700 (PDT)
X-Received: from coder-ast-ast-dev-ml-0.tail2dda0.ts.net ([153.61.198.255])
        by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3acc9d266d6sm4323835a91.1.2026.10.17.09.38.00
        (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
        Sat, 17 Oct 2026 09:38:01 -0700 (PDT)
X-Cc: daniel@iogearbox.net,
	andrii@kernel.org,
	eddyz87@gmail.com,
	memxor@gmail.com,
	netdev@vger.kernel.org,
	josef@toxicpanda.com,
	mason@kernel.org
X-Mailer: git-send-email 2.55.0
X-In-Reply-To: <20261017163745.3341787-1-alexei.starovoitov@gmail.com>
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>