Archive-only list for syzbot
 help / color / mirror / Atom feed
* [PATCH RFC] bpf: Reject rhtab delete and insert in NMI context
@ 2026-11-29  1:48 syzbot
  2026-12-01 13:46 ` Slawomir Stepien
  0 siblings, 1 reply; 2+ messages in thread
From: syzbot @ 2026-11-29  1:48 UTC (permalink / raw)
  To: syzkaller-upstream-moderation; +Cc: syzbot

BPF_MAP_TYPE_RHASH operations that remove or insert elements
(rhtab_delete_elem() and the new-element insertion path in
rhtab_map_update_elem()) acquire rhashtable bucket bit-spinlocks via
rht_lock() / rht_lock_nested(). Those functions disable local IRQs and take
an unconditional spinning lock (trylock = 0). Currently,
rhtab_delete_elem() and rhtab_map_update_elem() only wrap
rhashtable_remove_fast() and rhashtable_lookup_get_insert_fast() with
bpf_disable_instrumentation() / bpf_enable_instrumentation(). While that
suppresses perf, kprobe, and tracepoint BPF programs during the critical
section, it does not prevent a BPF program from running in NMI context when
bpf_prog_active is zero and calling into these mutation paths.

When a BPF_MAP_TYPE_RHASH bucket lock ("rhashtable_bucket") is first
acquired in normal task context (for instance via bpf(BPF_MAP_UPDATE_ELEM)
-> rhtab_map_update_elem() -> rhashtable_lookup_get_insert_fast()) and
later acquired in NMI context (for instance when a hardware breakpoint perf
event raises #DB during a syscall and invokes bpf_overflow_handler() ->
rhtab_map_delete_elem() -> rhtab_delete_elem() ->
rhashtable_remove_fast()), lockdep reports an inconsistent {INITIAL USE} ->
{IN-NMI} lock usage:

  WARNING: inconsistent lock state
  inconsistent {INITIAL USE} -> {IN-NMI} usage.
  ... takes:
  ffff88801fe8b838 (rhashtable_bucket#14){....}-{0:0}, at:
  rht_lock+0x8b/0x180
  {INITIAL USE} state was registered at:
    lock_acquire+0x115/0x350
    rht_lock+0xa3/0x180
    rhashtable_lookup_get_insert_fast
    rhtab_map_update_elem+0x9e4/0x1e70
  ...
  Call Trace:
   <#DB>
   print_usage_bug+0x28c/0x2e0
   lock_acquire+0x1dd/0x350
   rht_lock+0xa3/0x180
   rhashtable_remove_fast
   rhtab_delete_elem+0x407/0x1b00
   rhtab_map_delete_elem+0xe2/0x190
   bpf_overflow_handler
   perf_bp_event+0x318/0x430
   exc_debug_kernel+0xb1/0x140

This is a real deadlock and safety hazard rather than a lockdep false
positive. Generic rhashtable maintenance such as rht_deferred_worker()
acquires rht_lock() without calling bpf_disable_instrumentation(), so an
NMI interrupting that worker on the same CPU can spin forever in
bit_spin_lock() if the NMI BPF program accesses the same bucket; NMI
tracing programs also ignore bpf_prog_active. In addition,
rhashtable_lookup_get_insert_fast() can allocate nested or resized bucket
tables with GFP_ATOMIC (nested_table_alloc() and bucket_table_alloc()),
which is not safe in NMI context.

Reject NMI callers with -EOPNOTSUPP in rhtab_delete_elem() before calling
bpf_disable_instrumentation() / rhashtable_remove_fast(), and in
rhtab_map_update_elem() after the lockless existing-element update path and
BPF_EXIST check but before allocating or inserting a new element. Lockless
RCU lookups, iteration, and in-place updates of existing keys via
rhtab_map_update_existing() do not acquire rht_lock() or allocate bucket
tables and remain permitted in NMI context. Also update the comments above
bpf_disable_instrumentation() in both functions to clarify that it guards
against same-CPU instrumentation re-entrancy for non-NMI callers rather
than making NMI execution safe.

Fixes: 16b4d3e2fb24 ("bpf: Implement resizable hashmap basic functions")

Fixes: 16b4d3e2fb24 ("bpf: Implement resizable hashmap basic functions")
Assisted-by: Gemini:gemini-4-hydrogen Gemini:gemini-4-carbon Gemini:gemini-3.8-flash syzbot
Reported-by: syzbot+2fb31ade2d0e920fea9a@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=2fb31ade2d0e920fea9a
Link: https://syzkaller.appspot.com/ai_job?id=9dd3a058-bbe2-4008-9614-1906bedd8a73
To: "Andrii Nakryiko" <andrii@kernel.org>
To: "Alexei Starovoitov" <ast@kernel.org>
To: <bpf@vger.kernel.org>
To: "Daniel Borkmann" <daniel@iogearbox.net>
To: "Eduard Zingerman" <eddyz87@gmail.com>
To: "Kumar Kartikeya Dwivedi" <memxor@gmail.com>
To: "Mykyta Yatsenko" <yatsenko@meta.com>
Cc: "Emil Tsalapatis" <emil@etsalapatis.com>
Cc: "Ihor Solodrai" <ihor.solodrai@linux.dev>
Cc: "Jiri Olsa" <jolsa@kernel.org>
Cc: "Martin KaFai Lau" <martin.lau@linux.dev>
Cc: "Song Liu" <song@kernel.org>
Cc: "Yonghong Song" <yonghong.song@linux.dev>

---
diff --git a/kernel/bpf/hashtab.c b/kernel/bpf/hashtab.c
index 6e2115477..a330f0ee1 100644
--- a/kernel/bpf/hashtab.c
+++ b/kernel/bpf/hashtab.c
@@ -3143,11 +3143,15 @@ static int rhtab_delete_elem(struct bpf_rhtab *rhtab, struct rhtab_elem *elem, v
 {
 	int err;
 
+	if (in_nmi())
+		return -EOPNOTSUPP;
+
 	/*
-	 * disable_instrumentation() mitigates the deadlock for programs running in NMI context.
-	 * rhashtable locks bucket with local_irq_save(). Only NMI programs may reenter
-	 * rhashtable code, bpf_disable_instrumentation() disables programs running in NMI, except
-	 * raw tracepoints, which we don't have in rhashtable.
+	 * rhashtable bucket bit-spinlocks are not NMI-safe (can deadlock with
+	 * rht_deferred_worker() or NMI tracing progs that ignore bpf_prog_active),
+	 * so NMI callers are rejected above. Disable instrumentation to prevent
+	 * same-CPU kprobe/tracepoint/perf BPF re-entrancy while holding the
+	 * bucket lock.
 	 */
 	bpf_disable_instrumentation();
 
@@ -3249,6 +3253,15 @@ static long rhtab_map_update_elem(struct bpf_map *map, void *key, void *value, u
 	if (map_flags & BPF_EXIST)
 		return -ENOENT;
 
+	/*
+	 * Inserting a new element acquires rhashtable bucket bit-spinlocks and
+	 * may allocate bucket tables with GFP_ATOMIC, neither of which is safe
+	 * in NMI context. Updating an existing element above is lockless under
+	 * RCU and remains allowed in NMI.
+	 */
+	if (in_nmi())
+		return -EOPNOTSUPP;
+
 	/*
 	 * Reject new insertions while map_release_uref cleanup walks the
 	 * table. Without this, new elements could keep triggering rehash
@@ -3268,7 +3281,7 @@ static long rhtab_map_update_elem(struct bpf_map *map, void *key, void *value, u
 	memcpy(elem->data, key, map->key_size);
 	copy_map_value(map, rhtab_elem_value(elem, map->key_size), value);
 
-	/* Prevent deadlock for NMI programs attempting to take bucket lock */
+	/* Prevent instrumentation re-entrancy while holding bucket lock */
 	bpf_disable_instrumentation();
 
 	if (map->key_size == sizeof(long))


base-commit: b6a9fa34ce4187cb69d3c588a4bbffc31ce5db8f
-- 
This is an AI-generated patch subject to moderation.
Reply with '#syz upstream' to Sign-off the patch as a human author
and send it to the upstream kernel mailing lists.
Reply with '#syz reject' to reject it ('#syz unreject' to undo).

See https://goo.gle/syzbot-ai-patches for information about AI-generated patches.
You can comment on the patch as usual, syzbot will try to address
the comments and send a new version of the patch if necessary.
syzbot engineers can be reached at syzkaller@googlegroups.com.

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH RFC] bpf: Reject rhtab delete and insert in NMI context
  2026-11-29  1:48 [PATCH RFC] bpf: Reject rhtab delete and insert in NMI context syzbot
@ 2026-12-01 13:46 ` Slawomir Stepien
  0 siblings, 0 replies; 2+ messages in thread
From: Slawomir Stepien @ 2026-12-01 13:46 UTC (permalink / raw)
  To: syzbot; +Cc: syzkaller-upstream-moderation, syzbot

I would reject this:

1. There is an existing idea (proposed by maintainer) on lore that is better and also fixes one
additional issue (livelock during lookup):
https://lore.kernel.org/all/20261122-bpf-rhtab-irqs-disabled-v2-0-207aedcc2215@columbia.edu/.
2. The idea from point 1 is missing one fix for memory allocation in NMI, but I guess it would be
done as follow up and reusing the new trylock_irqsoff.

So, for now:

#syz reject

On lis 29, 2026 01:48, 'syzbot' via syzkaller-upstream-moderation wrote:
> BPF_MAP_TYPE_RHASH operations that remove or insert elements
> (rhtab_delete_elem() and the new-element insertion path in
> rhtab_map_update_elem()) acquire rhashtable bucket bit-spinlocks via
> rht_lock() / rht_lock_nested(). Those functions disable local IRQs and take
> an unconditional spinning lock (trylock = 0). Currently,
> rhtab_delete_elem() and rhtab_map_update_elem() only wrap
> rhashtable_remove_fast() and rhashtable_lookup_get_insert_fast() with
> bpf_disable_instrumentation() / bpf_enable_instrumentation(). While that
> suppresses perf, kprobe, and tracepoint BPF programs during the critical
> section, it does not prevent a BPF program from running in NMI context when
> bpf_prog_active is zero and calling into these mutation paths.
> 
> When a BPF_MAP_TYPE_RHASH bucket lock ("rhashtable_bucket") is first
> acquired in normal task context (for instance via bpf(BPF_MAP_UPDATE_ELEM)
> -> rhtab_map_update_elem() -> rhashtable_lookup_get_insert_fast()) and
> later acquired in NMI context (for instance when a hardware breakpoint perf
> event raises #DB during a syscall and invokes bpf_overflow_handler() ->
> rhtab_map_delete_elem() -> rhtab_delete_elem() ->
> rhashtable_remove_fast()), lockdep reports an inconsistent {INITIAL USE} ->
> {IN-NMI} lock usage:
> 
>   WARNING: inconsistent lock state
>   inconsistent {INITIAL USE} -> {IN-NMI} usage.
>   ... takes:
>   ffff88801fe8b838 (rhashtable_bucket#14){....}-{0:0}, at:
>   rht_lock+0x8b/0x180
>   {INITIAL USE} state was registered at:
>     lock_acquire+0x115/0x350
>     rht_lock+0xa3/0x180
>     rhashtable_lookup_get_insert_fast
>     rhtab_map_update_elem+0x9e4/0x1e70
>   ...
>   Call Trace:
>    <#DB>
>    print_usage_bug+0x28c/0x2e0
>    lock_acquire+0x1dd/0x350
>    rht_lock+0xa3/0x180
>    rhashtable_remove_fast
>    rhtab_delete_elem+0x407/0x1b00
>    rhtab_map_delete_elem+0xe2/0x190
>    bpf_overflow_handler
>    perf_bp_event+0x318/0x430
>    exc_debug_kernel+0xb1/0x140
> 
> This is a real deadlock and safety hazard rather than a lockdep false
> positive. Generic rhashtable maintenance such as rht_deferred_worker()
> acquires rht_lock() without calling bpf_disable_instrumentation(), so an
> NMI interrupting that worker on the same CPU can spin forever in
> bit_spin_lock() if the NMI BPF program accesses the same bucket; NMI
> tracing programs also ignore bpf_prog_active. In addition,
> rhashtable_lookup_get_insert_fast() can allocate nested or resized bucket
> tables with GFP_ATOMIC (nested_table_alloc() and bucket_table_alloc()),
> which is not safe in NMI context.
> 
> Reject NMI callers with -EOPNOTSUPP in rhtab_delete_elem() before calling
> bpf_disable_instrumentation() / rhashtable_remove_fast(), and in
> rhtab_map_update_elem() after the lockless existing-element update path and
> BPF_EXIST check but before allocating or inserting a new element. Lockless
> RCU lookups, iteration, and in-place updates of existing keys via
> rhtab_map_update_existing() do not acquire rht_lock() or allocate bucket
> tables and remain permitted in NMI context. Also update the comments above
> bpf_disable_instrumentation() in both functions to clarify that it guards
> against same-CPU instrumentation re-entrancy for non-NMI callers rather
> than making NMI execution safe.
> 
> Fixes: 16b4d3e2fb24 ("bpf: Implement resizable hashmap basic functions")
> 
> Fixes: 16b4d3e2fb24 ("bpf: Implement resizable hashmap basic functions")
> Assisted-by: Gemini:gemini-4-hydrogen Gemini:gemini-4-carbon Gemini:gemini-3.8-flash syzbot
> Reported-by: syzbot+2fb31ade2d0e920fea9a@syzkaller.appspotmail.com
> Closes: https://syzkaller.appspot.com/bug?extid=2fb31ade2d0e920fea9a
> Link: https://syzkaller.appspot.com/ai_job?id=9dd3a058-bbe2-4008-9614-1906bedd8a73
> To: "Andrii Nakryiko" <andrii@kernel.org>
> To: "Alexei Starovoitov" <ast@kernel.org>
> To: <bpf@vger.kernel.org>
> To: "Daniel Borkmann" <daniel@iogearbox.net>
> To: "Eduard Zingerman" <eddyz87@gmail.com>
> To: "Kumar Kartikeya Dwivedi" <memxor@gmail.com>
> To: "Mykyta Yatsenko" <yatsenko@meta.com>
> Cc: "Emil Tsalapatis" <emil@etsalapatis.com>
> Cc: "Ihor Solodrai" <ihor.solodrai@linux.dev>
> Cc: "Jiri Olsa" <jolsa@kernel.org>
> Cc: "Martin KaFai Lau" <martin.lau@linux.dev>
> Cc: "Song Liu" <song@kernel.org>
> Cc: "Yonghong Song" <yonghong.song@linux.dev>
> 
> ---
> diff --git a/kernel/bpf/hashtab.c b/kernel/bpf/hashtab.c
> index 6e2115477..a330f0ee1 100644
> --- a/kernel/bpf/hashtab.c
> +++ b/kernel/bpf/hashtab.c
> @@ -3143,11 +3143,15 @@ static int rhtab_delete_elem(struct bpf_rhtab *rhtab, struct rhtab_elem *elem, v
>  {
>  	int err;
>  
> +	if (in_nmi())
> +		return -EOPNOTSUPP;
> +
>  	/*
> -	 * disable_instrumentation() mitigates the deadlock for programs running in NMI context.
> -	 * rhashtable locks bucket with local_irq_save(). Only NMI programs may reenter
> -	 * rhashtable code, bpf_disable_instrumentation() disables programs running in NMI, except
> -	 * raw tracepoints, which we don't have in rhashtable.
> +	 * rhashtable bucket bit-spinlocks are not NMI-safe (can deadlock with
> +	 * rht_deferred_worker() or NMI tracing progs that ignore bpf_prog_active),
> +	 * so NMI callers are rejected above. Disable instrumentation to prevent
> +	 * same-CPU kprobe/tracepoint/perf BPF re-entrancy while holding the
> +	 * bucket lock.
>  	 */
>  	bpf_disable_instrumentation();
>  
> @@ -3249,6 +3253,15 @@ static long rhtab_map_update_elem(struct bpf_map *map, void *key, void *value, u
>  	if (map_flags & BPF_EXIST)
>  		return -ENOENT;
>  
> +	/*
> +	 * Inserting a new element acquires rhashtable bucket bit-spinlocks and
> +	 * may allocate bucket tables with GFP_ATOMIC, neither of which is safe
> +	 * in NMI context. Updating an existing element above is lockless under
> +	 * RCU and remains allowed in NMI.
> +	 */
> +	if (in_nmi())
> +		return -EOPNOTSUPP;
> +
>  	/*
>  	 * Reject new insertions while map_release_uref cleanup walks the
>  	 * table. Without this, new elements could keep triggering rehash
> @@ -3268,7 +3281,7 @@ static long rhtab_map_update_elem(struct bpf_map *map, void *key, void *value, u
>  	memcpy(elem->data, key, map->key_size);
>  	copy_map_value(map, rhtab_elem_value(elem, map->key_size), value);
>  
> -	/* Prevent deadlock for NMI programs attempting to take bucket lock */
> +	/* Prevent instrumentation re-entrancy while holding bucket lock */
>  	bpf_disable_instrumentation();
>  
>  	if (map->key_size == sizeof(long))
> 
> 
> base-commit: b6a9fa34ce4187cb69d3c588a4bbffc31ce5db8f

-- 
Slawomir Stepien

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-01 13:46 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-29  1:48 [PATCH RFC] bpf: Reject rhtab delete and insert in NMI context syzbot
2026-12-01 13:46 ` Slawomir Stepien

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox