From nobody Wed Dec 17 17:39:08 2025 Received: from out-182.mta1.migadu.com (out-182.mta1.migadu.com [95.215.58.182]) (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 712B82F1FC3 for ; Wed, 8 Oct 2025 10:26:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759919213; cv=none; b=g3d5+Hheeli3XA5652LQ6RX4EwSsnqmK6ujK5lH1HkjaxCGQHnVeoOFvoxUPoiXcOpGRTXPBbLjciXiI9mFKnvVLbtcAzKw8+3b6e/TlK8pZPJZGgwkFECJGFpXQAiblIm56h8+uyocTACXbQm1kAta9SkAXCfKh4jfFo4Cek5c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759919213; c=relaxed/simple; bh=phR0YDtWGzD/4mUwv1ESjbXd3UFwT/l6vesT8v0fZ+o=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=e+f2xGcK1FdJm2eiqLnaT8zce7lAHPmOr4nGTFgDWE67bbWLruXaiCafy0c97ZliFn+CqI/n0OpOveip3MITEiyVaVgIquzF4WuW9w3QVfiP0fHPVLd05do6+xnoXwG/tNOekGBT7fVNh6MPcxstsmfYWQb4qtjF4ef1lv2xoSE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=oEc8OSKn; arc=none smtp.client-ip=95.215.58.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="oEc8OSKn" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1759919208; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=rdZziyvcxZWaWfSQpHk5+HHuMfV2FrS0eMWiX/f6VmI=; b=oEc8OSKnysKsxegizFSZS0TbBRJKjkKj68BwTD9mcWhw0f+/7sttOq3Em7B0JKqc4ikidE +URh12161VbAryPZ0a6P2qSI/JC9mcj5hi+XMjw6gXP2Uab/OzmqmdFGjog8aetvR2YObj 6d+sQ6OKk3mX30uaXUDHZDAsLkhcV0Y= From: KaFai Wan To: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org, shuah@kernel.org, kafai.wan@linux.dev, toke@redhat.com, linux-kernel@vger.kernel.org, bpf@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: Le Chen Subject: [PATCH bpf v3 1/2] bpf: Avoid RCU context warning when unpinning htab with internal structs Date: Wed, 8 Oct 2025 18:26:26 +0800 Message-ID: <20251008102628.808045-2-kafai.wan@linux.dev> In-Reply-To: <20251008102628.808045-1-kafai.wan@linux.dev> References: <20251008102628.808045-1-kafai.wan@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" When unpinning a BPF hash table (htab or htab_lru) that contains internal structures (timer, workqueue, or task_work) in its values, a BUG warning is triggered: BUG: sleeping function called from invalid context at kernel/bpf/hashtab.c= :244 in_atomic(): 1, irqs_disabled(): 0, non_block: 0, pid: 14, name: ksoftirqd= /0 ... The issue arises from the interaction between BPF object unpinning and RCU callback mechanisms: 1. BPF object unpinning uses ->free_inode() which schedules cleanup via call_rcu(), deferring the actual freeing to an RCU callback that executes within the RCU_SOFTIRQ context. 2. During cleanup of hash tables containing internal structures, htab_map_free_internal_structs() is invoked, which includes cond_resched() or cond_resched_rcu() calls to yield the CPU during potentially long operations. However, cond_resched() or cond_resched_rcu() cannot be safely called from atomic RCU softirq context, leading to the BUG warning when attempting to reschedule. Fix this by changing from ->free_inode() to ->destroy_inode() and rename bpf_free_inode() to bpf_destroy_inode() for BPF objects (prog, map, link). This allows direct inode freeing without RCU callback scheduling, avoiding the invalid context warning. Reported-by: Le Chen Closes: https://lore.kernel.org/all/1444123482.1827743.1750996347470.JavaMa= il.zimbra@sjtu.edu.cn/ Fixes: 68134668c17f ("bpf: Add map side support for bpf timers.") Suggested-by: Alexei Starovoitov Signed-off-by: KaFai Wan Acked-by: Yonghong Song --- kernel/bpf/inode.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kernel/bpf/inode.c b/kernel/bpf/inode.c index f90bdcc0a047..81780bcf8d25 100644 --- a/kernel/bpf/inode.c +++ b/kernel/bpf/inode.c @@ -775,7 +775,7 @@ static int bpf_show_options(struct seq_file *m, struct = dentry *root) return 0; } =20 -static void bpf_free_inode(struct inode *inode) +static void bpf_destroy_inode(struct inode *inode) { enum bpf_type type; =20 @@ -790,7 +790,7 @@ const struct super_operations bpf_super_ops =3D { .statfs =3D simple_statfs, .drop_inode =3D inode_just_drop, .show_options =3D bpf_show_options, - .free_inode =3D bpf_free_inode, + .destroy_inode =3D bpf_destroy_inode, }; =20 enum { --=20 2.43.0 From nobody Wed Dec 17 17:39:08 2025 Received: from out-187.mta1.migadu.com (out-187.mta1.migadu.com [95.215.58.187]) (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 080DA2F6164 for ; Wed, 8 Oct 2025 10:26:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.187 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759919219; cv=none; b=l8tGJpbYn/zAkQgtY0TKIqV0HiQSV7Zo5WjrD+Kzz/+Jo5/ju8fbrbgRTwijb3etKCcrDsDd+V+eaF9abbVX+kyqiQi9EyfhjT0TYLdphGE+Fh/C20yVukA7seJqUZjJhiCMMcwF5ty0e13TtNrOjl+WnvCF2fRQnEFPWkUM9nQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759919219; c=relaxed/simple; bh=8uQeL4E477nom03PLC006W88FoYSSTjNO0YClm3+fj8=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ecToyERgQgH+sAuVE5C3b4OHr3moM1huMBdvJhIQlX/kj7joxEJh9YDLTy5rh2hJvnl4aSiUg5pi8IJ7H29lkdBzGZwXceydgH+iZife3alrxiymw1QPUUI6faeWpJxqCQ8uGhUIrKsr9bmS24YmZDAK1BFPJWF02Nm/p9WL5nc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=Sy1AyRzU; arc=none smtp.client-ip=95.215.58.187 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="Sy1AyRzU" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1759919215; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=oEa07dvEPCZz8jmImHhIJpaCW6KALRSdrL+C0O70/q4=; b=Sy1AyRzUUz2Xfm32LPcOuKzUNlkZ+JXkK0iNkuxCfw9vOX75tM/4k6Bq/b0WuAds0dDEPY D072HfO3K1h8Ks2ZUY/0xlqXSZERK8/sk4vNxh7QQmKG9pwOPh3Fd57NQIH3tsG2T8mzP1 LJHuf/WoXC2Pk4MaCybxSX8KrBIahEE= From: KaFai Wan To: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org, shuah@kernel.org, kafai.wan@linux.dev, toke@redhat.com, linux-kernel@vger.kernel.org, bpf@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf v3 2/2] selftests/bpf: Add test for unpinning htab with internal timer struct Date: Wed, 8 Oct 2025 18:26:27 +0800 Message-ID: <20251008102628.808045-3-kafai.wan@linux.dev> In-Reply-To: <20251008102628.808045-1-kafai.wan@linux.dev> References: <20251008102628.808045-1-kafai.wan@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" Add test to verify that unpinning hash tables containing internal timer structures does not trigger context warnings. Each subtest (timer_prealloc and timer_no_prealloc) can trigger the context warning when unpinning, but the warning cannot be triggered twice within a short time interval (a HZ), which is expected behavior. Signed-off-by: KaFai Wan Acked-by: Yonghong Song --- .../selftests/bpf/prog_tests/pinning_htab.c | 36 +++++++++++++++++++ .../selftests/bpf/progs/test_pinning_htab.c | 25 +++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/pinning_htab.c create mode 100644 tools/testing/selftests/bpf/progs/test_pinning_htab.c diff --git a/tools/testing/selftests/bpf/prog_tests/pinning_htab.c b/tools/= testing/selftests/bpf/prog_tests/pinning_htab.c new file mode 100644 index 000000000000..16bd74be3dbe --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/pinning_htab.c @@ -0,0 +1,36 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include "test_pinning_htab.skel.h" + +static void unpin_map(const char *map_name, const char *pin_path) +{ + struct test_pinning_htab *skel; + struct bpf_map *map; + int err; + + skel =3D test_pinning_htab__open_and_load(); + if (!ASSERT_OK_PTR(skel, "skel open_and_load")) + return; + + map =3D bpf_object__find_map_by_name(skel->obj, map_name); + if (!ASSERT_OK_PTR(map, "bpf_object__find_map_by_name")) + goto out; + + err =3D bpf_map__pin(map, pin_path); + if (!ASSERT_OK(err, "bpf_map__pin")) + goto out; + + err =3D bpf_map__unpin(map, pin_path); + ASSERT_OK(err, "bpf_map__unpin"); +out: + test_pinning_htab__destroy(skel); +} + +void test_pinning_htab(void) +{ + if (test__start_subtest("timer_prealloc")) + unpin_map("timer_prealloc", "/sys/fs/bpf/timer_prealloc"); + if (test__start_subtest("timer_no_prealloc")) + unpin_map("timer_no_prealloc", "/sys/fs/bpf/timer_no_prealloc"); +} diff --git a/tools/testing/selftests/bpf/progs/test_pinning_htab.c b/tools/= testing/selftests/bpf/progs/test_pinning_htab.c new file mode 100644 index 000000000000..ae227930c73c --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_pinning_htab.c @@ -0,0 +1,25 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include "vmlinux.h" +#include + +char _license[] SEC("license") =3D "GPL"; + +struct timer_val { + struct bpf_timer timer; +}; + +struct { + __uint(type, BPF_MAP_TYPE_HASH); + __type(key, __u32); + __type(value, struct timer_val); + __uint(max_entries, 1); +} timer_prealloc SEC(".maps"); + +struct { + __uint(type, BPF_MAP_TYPE_HASH); + __type(key, __u32); + __type(value, struct timer_val); + __uint(max_entries, 1); + __uint(map_flags, BPF_F_NO_PREALLOC); +} timer_no_prealloc SEC(".maps"); --=20 2.43.0