Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795625419; b=5CugZqjnOYuuuZHvForBiz9ehCjMNOkPht1l45BVsS0YxKErhWuLXpnciVYuXxIop4Q4 GN+7P7RRxj/5YThfznc7Us2qf3lIBed5vTM7DYP4nnykYB4d08BSLeIaIFKxVyW4ECpYl 4YFC++3bvoRoauFk2bfiBvDB0teutAyXxOJ45gExV/ITP5zzpI/STitz9nCYMa5z9LYlL VhQenxMEh7wZimW8ht5kFz1h1nFIltZNNr3V/8Q8FUIuzROCohS+j5hzzstmIFtRh1WFf HSP49KXMnAz66D59Zun4x+i2+oJLz1W/3DCPZnbL7TxuhCO0onBz5I2U/tokPuwZZTQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795625419; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=gZtyepsJ+bv99G7L3U5BJn5+jcf/9KMJo6tfYhvyjc4=; b=PgouzqYeOH9TLn8FLh9fZS8nzBOr8VAiL+lh/FrlFgI971NwhIHqpXmmaOMW/oLIVWkk JPW0gyaZjHVr3babwV1mKPY8scQebKtJxXHH+mFprUOekVa1F/zPbys3q+wwL5JDwPEyS yEyFsfXkAvjs9POSJAXbe4L2iOHV3mrhq9BfcjeMw8YxfjOp9ft1KOotQDRG3N5TFxb9A 0OmXfbiY8ACjD+Ufaedu+43hqLKoDXix3UeWwsCD5Oh6xg0U5OSs9OwbdIeyIHQsZ50QB eT346fxi6yQuG8aR1rbI1/ss15OjRSuWQHT9Kd2zxAFJlUjTv0Sv/L/5hTGSRFcMSyw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b="sGi39t/x"; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5487A606A31E; Wed, 25 Nov 2026 16:50:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=sGi39t/x Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8227A1F000C4; Wed, 25 Nov 2026 16:50:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1795625419; bh=gZtyepsJ+bv99G7L3U5BJn5+jcf/9KMJo6tfYhvyjc4=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=sGi39t/xdcBkjpEOPkZHcBmCRhVhmtgsqHjN8xrHkcjfhTjOTVdFJ+DPJUX4uGQC2 crFeeMLboH7CiYvRn6FDbYXv3id75W0IY+6O4A5IZGrX4wmSt1p3xjlAbJfgA0Tzrk Nqu27FbPOONrfPk0werPklf4rlUoQpwKdtNM4Zag= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Josef Bacik , Alexei Starovoitov , Sasha Levin Subject: [PATCH 6.12 508/816] bpf: Count sleepable callbacks as sleepable map users Date: Wed, 25 Nov 2026 15:34:16 +0100 Message-ID: <20261125143900.944660487@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261125143849.205329330@linuxfoundation.org> References: <20261125143849.205329330@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: patches@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.12-stable review patch. If anyone has any objections, please let me know. ------------------ From: Josef Bacik [ Upstream commit 927c9c985f3ef4d30c086884b7e0ecb933f742ef ] map->sleepable_refcnt tells bpf_map_fd_put_ptr() whether an inner map that is taken out of its outer map has to wait for an RCU Tasks Trace grace period as well. It counts the sleepable programs that use the outer map. The bpf_wq or bpf_task_work callback of a program that is not sleepable runs under rcu_read_lock_trace() only, and is not counted. Such a callback can look an inner map up and keep it while user space deletes it from the outer map. The inner map is freed behind a plain RCU grace period, under the callback: BUG: KASAN: slab-use-after-free in array_map_update_elem+0x6bc/0x7d0 Count a program with such a callback. The maps are bound before the verifier meets the callback, so raise the count of those that are bound when it does. Fixes: 8e83da9732d9 ("bpf: add bpf_wq_start") Signed-off-by: Josef Bacik Signed-off-by: Alexei Starovoitov Link: https://patch.msgid.link/20261017163745.3341787-24-alexei.starovoitov@gmail.com [ sashal: Adapted to missing helpers by keeping the local is_bpf_wq_set_callback_impl_kfunc() check and is_may_goto_insn() name, and by editing add_used_map_from_fd() without the check_map_prog_compatibility() context. ] Signed-off-by: Sasha Levin --- kernel/bpf/core.c | 2 +- kernel/bpf/syscall.c | 2 +- kernel/bpf/verifier.c | 17 +++++++++++++++-- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c index df529db44f35c..de2c0ffb30252 100644 --- a/kernel/bpf/core.c +++ b/kernel/bpf/core.c @@ -2873,7 +2873,7 @@ void __bpf_free_used_maps(struct bpf_prog_aux *aux, bool sleepable; u32 i; - sleepable = aux->prog->sleepable; + sleepable = bpf_prog_runs_sleepable(aux->prog); for (i = 0; i < len; i++) { map = used_maps[i]; if (map->ops->map_poke_untrack) diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 3e084cf77d134..8fd823047c3a0 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -5754,7 +5754,7 @@ static int bpf_prog_bind_map(union bpf_attr *attr) /* The bpf program will not access the bpf map, but for the sake of * simplicity, increase sleepable_refcnt for sleepable program as well. */ - if (prog->sleepable) + if (bpf_prog_runs_sleepable(prog)) atomic64_inc(&map->sleepable_refcnt); memcpy(used_maps_new, used_maps_old, sizeof(used_maps_old[0]) * prog->aux->used_map_cnt); diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index d8317cef6e8ab..1404e842580ba 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -547,6 +547,19 @@ static bool is_async_callback_calling_insn(struct bpf_insn *insn) (bpf_pseudo_kfunc_call(insn) && is_async_callback_calling_kfunc(insn->imm)); } +/* From here on the maps of the program count it as a sleepable user. */ +static void mark_sleepable_async_cb(struct bpf_verifier_env *env) +{ + int i; + + if (bpf_prog_runs_sleepable(env->prog)) + return; + + env->prog->aux->sleepable_async_cb = true; + for (i = 0; i < env->used_map_cnt; i++) + atomic64_inc(&env->used_maps[i]->sleepable_refcnt); +} + static bool is_may_goto_insn(struct bpf_insn *insn) { return insn->code == (BPF_JMP | BPF_JCOND) && insn->src_reg == BPF_MAY_GOTO; @@ -10175,7 +10188,7 @@ static int push_callback_call(struct bpf_verifier_env *env, struct bpf_insn *ins if (IS_ERR(async_cb)) return PTR_ERR(async_cb); if (is_bpf_wq_set_callback_impl_kfunc(insn->imm)) - env->prog->aux->sleepable_async_cb = true; + mark_sleepable_async_cb(env); callee = async_cb->frame[0]; callee->async_entry_cnt = state->frame[0]->async_entry_cnt + 1; @@ -19651,7 +19664,7 @@ static int add_used_map_from_fd(struct bpf_verifier_env *env, int fd, bool *reus return -E2BIG; } - if (env->prog->sleepable) + if (bpf_prog_runs_sleepable(env->prog)) atomic64_inc(&map->sleepable_refcnt); /* hold the map. If the program is rejected by verifier, -- 2.53.0