bpf: Fix torn writes in non-prealloc htab with BPF_F_LOCK
Status from this message: This series was applied to bpf/bpf-next.git (master)
Get series with b4
b4 am [email protected] change-id 20260331-bpf_map_torn_writes-624bd38e2c50Versions newest first
Patches in v1 All 2 applied
| # | Subject and files | Size | Replies | Applied |
|---|---|---|---|---|
| 1/2 | bpf: Use copy_map_value_locked() in alloc_htab_elem() for BPF_F_LOCKkernel/ | +4 | 0 replies | Applied |
| 2/2 | selftests/bpf: Add torn write detection test for htab BPF_F_LOCKtools/ tools/ | +184 | 0 replies | Applied |
Reviews trailers per patch
No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.
Cover letter [PATCH 0/2] bpf: Fix torn writes in non-prealloc htab with BPF_F_LOCK
A torn write issue was reported in htab_map_update_elem() with BPF_F_LOCK on hash maps. The BPF_F_LOCK fast path performs a lockless lookup and copies the value under the element's embedded spin_lock. A concurrent delete can free the element via bpf_mem_cache_free(), which allows immediate reuse. When alloc_htab_elem() recycles the same memory, it writes the value with plain copy_map_value() without taking the spin_lock, racing with the stale lock holder and producing torn writes.
Patch 1 fixes alloc_htab_elem() to use copy_map_value_locked() when BPF_F_LOCK is set.
Patch 2 adds a selftest that reliably detects the torn writes on an unpatched kernel.
---
Mykyta Yatsenko (2):
bpf: Use copy_map_value_locked() in alloc_htab_elem() for BPF_F_LOCK selftests/bpf: Add torn write detection test for htab BPF_F_LOCK
kernel/bpf/hashtab.c | 4 + .../testing/selftests/bpf/prog_tests/htab_reuse.c | 169 ++++++++++++++++++++- tools/testing/selftests/bpf/progs/htab_reuse.c | 16 ++ 3 files changed, 188 insertions(+), 1 deletion(-)
---
base-commit: 11c7d8e8ad8eed1304b65c42bf5fde9df947a9e6
change-id: 20260331-bpf_map_torn_writes-624bd38e2c50
Best regards,
Signature · 1 line
Mykyta Yatsenko <[email protected]>