public inbox for bpf@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/2] bpf: Fix torn writes in non-prealloc htab with BPF_F_LOCK
@ 2026-04-01 13:50 Mykyta Yatsenko
  2026-04-01 13:50 ` [PATCH 1/2] bpf: Use copy_map_value_locked() in alloc_htab_elem() for BPF_F_LOCK Mykyta Yatsenko
                   ` (3 more replies)
  0 siblings, 4 replies; 7+ messages in thread
From: Mykyta Yatsenko @ 2026-04-01 13:50 UTC (permalink / raw)
  To: bpf, ast, andrii, daniel, kafai, kernel-team, eddyz87, memxor
  Cc: Aaron Esau, Mykyta Yatsenko

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.

Reported-by: Aaron Esau <aaron1esau@gmail.com>
Signed-off-by: Mykyta Yatsenko <yatsenko@meta.com>
---
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,
--  
Mykyta Yatsenko <yatsenko@meta.com>


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

end of thread, other threads:[~2026-04-06  1:50 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-04-01 13:50 [PATCH 0/2] bpf: Fix torn writes in non-prealloc htab with BPF_F_LOCK Mykyta Yatsenko
2026-04-01 13:50 ` [PATCH 1/2] bpf: Use copy_map_value_locked() in alloc_htab_elem() for BPF_F_LOCK Mykyta Yatsenko
2026-04-01 13:50 ` [PATCH 2/2] selftests/bpf: Add torn write detection test for htab BPF_F_LOCK Mykyta Yatsenko
2026-04-01 15:21 ` [PATCH 0/2] bpf: Fix torn writes in non-prealloc htab with BPF_F_LOCK Kumar Kartikeya Dwivedi
2026-04-01 15:33   ` Mykyta Yatsenko
2026-04-01 15:42     ` Kumar Kartikeya Dwivedi
2026-04-06  1:50 ` patchwork-bot+netdevbpf

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