Skip to contentSkip to conversation

bpf: Fix missing migration protection in __rhtab_map_lookup_and_delete_batch()

PATCH Applied Ömer Mete Kaya 3 participants 3 messages Sep 29 – Oct 2 BPF

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 bpf: Fix missing migration protection in __rhtab_map_lookup_and_delete_batch()

bpf_mem_cache_free_rcu() uses this_cpu_ptr() which requires migration to be disabled. All callers of rhtab_delete_elem() disable migration except __rhtab_map_lookup_and_delete_batch(), which calls it under rcu_read_lock() only.

On CONFIG_PREEMPT_RCU, rcu_read_lock() does not disable preemption or migration, so the task can migrate between CPUs during the delete loop, causing this_cpu_ptr() to trigger:

  BUG: using smp_processor_id() in preemptible [00000000] code

Fix by wrapping the delete loop in migrate_disable()/migrate_enable() in __rhtab_map_lookup_and_delete_batch(), matching the migration protection that the other callers already provide.

Fixes 818e00848227 ("bpf: Implement iteration ops for resizable hashtab")
Reported-by [email protected]

---
Changes in v2:
- Fix root cause in __rhtab_map_lookup_and_delete_batch() with migrate_disable()/migrate_enable() instead of stretching bpf_disable_instrumentation() in rhtab_delete_elem().
- Fix Fixes: tag to 818e00848227.
- Fix commit message: migration disabled, not preemption.

Tested with syzkaller:
- Before the fix: the reported warning was reproduced reliably.
- After the fix: the warning was no longer reproducible.

 kernel/bpf/hashtab.c | 2 ++
 1 file changed, 2 insertions(+)
kernel/bpf/hashtab.c +2 -0
index f744a42bb813..2bb9ac7fc73e 100644
@@ -3369,8 +3369,10 @@ static int __rhtab_map_lookup_and_delete_batch(struct bpf_map *map,
 	}
 
 	if (do_delete) {
+		migrate_disable();
 		for (i = 0; i < total; i++)
 			rhtab_delete_elem(rhtab, del_elems[i], NULL, 0);
+		migrate_enable();
 	}
 
 	rcu_read_unlock();

--
2.55.0

Acked-by on v2 1/1
#1 lore raw
Quoted 15 lines · Ömer Mete Kaya

bpf_mem_cache_free_rcu() uses this_cpu_ptr() which requires migration to be disabled. All callers of rhtab_delete_elem() disable migration except __rhtab_map_lookup_and_delete_batch(), which calls it under rcu_read_lock() only.

On CONFIG_PREEMPT_RCU, rcu_read_lock() does not disable preemption or migration, so the task can migrate between CPUs during the delete loop, causing this_cpu_ptr() to trigger:

  BUG: using smp_processor_id() in preemptible [00000000] code

Fix by wrapping the delete loop in migrate_disable()/migrate_enable() in __rhtab_map_lookup_and_delete_batch(), matching the migration protection that the other callers already provide.

Quoted 6 lines
Fixes 818e00848227 ("bpf: Implement iteration ops for resizable hashtab")
Reported-by [email protected]

---

Quoted 33 lines

Changes in v2:
- Fix root cause in __rhtab_map_lookup_and_delete_batch() with migrate_disable()/migrate_enable() instead of stretching bpf_disable_instrumentation() in rhtab_delete_elem().
- Fix Fixes: tag to 818e00848227.
- Fix commit message: migration disabled, not preemption.

Tested with syzkaller:
- Before the fix: the reported warning was reproduced reliably.
- After the fix: the warning was no longer reproducible.

 kernel/bpf/hashtab.c | 2 ++
 1 file changed, 2 insertions(+)
kernel/bpf/hashtab.c +2 -1
index f744a42bb813..2bb9ac7fc73e 100644
@@ -3369,8 +3369,10 @@ static int __rhtab_map_lookup_and_delete_batch(struct bpf_map *map,
 	}
 
 	if (do_delete) {
+		migrate_disable();
 		for (i = 0; i < total; i++)
 			rhtab_delete_elem(rhtab, del_elems[i], NULL, 0);
+		migrate_enable();
 	}
 
 	rcu_read_unlock();
--

2.55.0

#2 lore raw
In reply to Ömer Mete Kaya
This patch was applied to bpf/bpf.git (master) by Alexei Starovoitov <[email protected]>:

Hello:

This patch was applied to bpf/bpf.git (master)
by Alexei Starovoitov <[email protected]>:

Quoted 10 lines · On Tue, 29 Sep 2026 11:10:34 +0300 you wrote:

bpf_mem_cache_free_rcu() uses this_cpu_ptr() which requires migration to be disabled. All callers of rhtab_delete_elem() disable migration except __rhtab_map_lookup_and_delete_batch(), which calls it under rcu_read_lock() only.

On CONFIG_PREEMPT_RCU, rcu_read_lock() does not disable preemption or migration, so the task can migrate between CPUs during the delete loop, causing this_cpu_ptr() to trigger:

[...]

Here is the summary with links:
- [bpf,v2] bpf: Fix missing migration protection in __rhtab_map_lookup_and_delete_batch()

    https://git.kernel.org/bpf/bpf/c/de020dc8049b

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html