Skip to content

bpf: Fix torn writes in non-prealloc htab with BPF_F_LOCK

Mykyta Yatsenko bpf 1 version Applied

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-624bd38e2c50

Versions newest first

  1. v1 Latest2 patches 3 repliesApplied 2/2

Patches in v1 All 2 applied

#Subject and filesSizeRepliesApplied
1/2bpf: Use copy_map_value_locked() in alloc_htab_elem() for BPF_F_LOCKkernel/bpf/hashtab.c+4 -00 repliesApplied
2/2selftests/bpf: Add torn write detection test for htab BPF_F_LOCKtools/testing/selftests/bpf/prog_tests/htab_reuse.c tools/testing/selftests/bpf/progs/htab_reuse.c+184 -10 repliesApplied

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.

Read the v1 thread

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]>