* [PATCH bpf] bpf: Drop the write-active count when a writable arena mapping closes
@ 2026-12-02 7:00 Anh Nguyen Hoang via B4 Relay
2026-12-02 13:30 ` Alexei Starovoitov
0 siblings, 1 reply; 2+ messages in thread
From: Anh Nguyen Hoang via B4 Relay @ 2026-12-02 7:00 UTC (permalink / raw)
To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko
Cc: John Fastabend, Eduard Zingerman, Kumar Kartikeya Dwivedi,
Martin KaFai Lau, Song Liu, Yonghong Song, Jiri Olsa,
Emil Tsalapatis, Ihor Solodrai, Barret Rhoden, bpf,
Anh Nguyen Hoang
From: Anh Nguyen Hoang <anhnh.4work@gmail.com>
bpf_map_mmap() takes a write-active count on the map for a writable
mapping and relies on the .open and .close callbacks of the vm_ops it
installs to keep that count in step with the VMAs. arena_map_mmap() then
replaces vma->vm_ops with arena_vm_ops, and arena_vm_open() and
arena_vm_close() only maintain the arena's vma_list. The count taken in
bpf_map_mmap() is never dropped.
As a result, once an arena was mmap()ed with PROT_WRITE, map->writecnt
stays elevated after munmap() and BPF_MAP_FREEZE on that arena fails
with -EBUSY for the rest of the map's lifetime.
Drop the count in arena_vm_close(). No path is known to call
arena_vm_open() today: VM_DONTCOPY, arena_vm_may_split() and
arena_get_unmapped_area() refuse fork, split and mremap() moves. It
already takes a reference on the vma_list entry in case it is called,
so take the write-active count there as well, as bpf_map_mmap_open()
does. Make bpf_map_write_active_inc() and bpf_map_write_active_dec()
available outside of syscall.c for that.
Found by an automated code-checking tool. Tested in QEMU (x86_64) with
a program that maps an arena writable, unmaps it and calls
BPF_MAP_FREEZE: the freeze fails with -EBUSY before this change and
succeeds after it. It still fails with -EBUSY while a writable mapping
exists.
Fixes: 317460317a02 ("bpf: Introduce bpf_arena.")
Signed-off-by: Anh Nguyen Hoang <anhnh.4work@gmail.com>
---
I could not find a way to reach arena_vm_open() today, but I kept it
paired with arena_vm_close() so the count stays right if that changes.
I can send a v2 without it if you prefer.
BPF selftests (test_progs, in QEMU without KVM) give the same results
with and without the patch.
Testing:
- Compile-tested on x86_64 (allmodconfig, W=1): kernel/bpf/arena.o
kernel/bpf/syscall.o; no new warnings.
- checkpatch.pl --strict: no errors or warnings.
This patch was drafted with AI assistance (Claude, claude-fable-5-2).
---
include/linux/bpf.h | 2 ++
kernel/bpf/arena.c | 6 ++++++
kernel/bpf/syscall.c | 4 ++--
3 files changed, 10 insertions(+), 2 deletions(-)
diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index a6976776e90b..71f3efd1f46c 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -2984,6 +2984,8 @@ void bpf_map_put(struct bpf_map *map);
void *bpf_map_area_alloc(u64 size, int numa_node);
void *bpf_map_area_mmapable_alloc(u64 size, int numa_node);
void bpf_map_area_free(void *base);
+void bpf_map_write_active_inc(struct bpf_map *map);
+void bpf_map_write_active_dec(struct bpf_map *map);
bool bpf_map_write_active(const struct bpf_map *map);
void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr);
int generic_map_lookup_batch(struct bpf_map *map,
diff --git a/kernel/bpf/arena.c b/kernel/bpf/arena.c
index 70a49b1200a9..ec545c89e36d 100644
--- a/kernel/bpf/arena.c
+++ b/kernel/bpf/arena.c
@@ -460,8 +460,11 @@ static int remember_vma(struct bpf_arena *arena, struct vm_area_struct *vma)
static void arena_vm_open(struct vm_area_struct *vma)
{
+ struct bpf_map *map = vma->vm_file->private_data;
struct vma_list *vml = vma->vm_private_data;
+ if (vma->vm_flags & VM_MAYWRITE)
+ bpf_map_write_active_inc(map);
refcount_inc(&vml->mmap_count);
}
@@ -481,6 +484,9 @@ static void arena_vm_close(struct vm_area_struct *vma)
struct bpf_arena *arena = container_of(map, struct bpf_arena, map);
struct vma_list *vml = vma->vm_private_data;
+ /* Pairs with bpf_map_mmap() and arena_vm_open() */
+ if (vma->vm_flags & VM_MAYWRITE)
+ bpf_map_write_active_dec(map);
if (!refcount_dec_and_test(&vml->mmap_count))
return;
guard(mutex)(&arena->lock);
diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index 91d078bb13d2..cec1469e10c7 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -119,12 +119,12 @@ const struct bpf_map_ops bpf_map_offload_ops = {
.map_mem_usage = bpf_map_offload_map_mem_usage,
};
-static void bpf_map_write_active_inc(struct bpf_map *map)
+void bpf_map_write_active_inc(struct bpf_map *map)
{
atomic64_inc(&map->writecnt);
}
-static void bpf_map_write_active_dec(struct bpf_map *map)
+void bpf_map_write_active_dec(struct bpf_map *map)
{
atomic64_dec(&map->writecnt);
}
---
base-commit: 12e84ae1e0bd1f221eb804ecfa2a1e2105270015
change-id: 20261201-kbug-l1940-88ae1676a587
Best regards,
--
Anh Nguyen Hoang <anhnh.4work@gmail.com>
^ permalink raw reply related [flat|nested] 2+ messages in thread* Re: [PATCH bpf] bpf: Drop the write-active count when a writable arena mapping closes
2026-12-02 7:00 [PATCH bpf] bpf: Drop the write-active count when a writable arena mapping closes Anh Nguyen Hoang via B4 Relay
@ 2026-12-02 13:30 ` Alexei Starovoitov
0 siblings, 0 replies; 2+ messages in thread
From: Alexei Starovoitov @ 2026-12-02 13:30 UTC (permalink / raw)
To: anhnh.4work, Daniel Borkmann, Andrii Nakryiko
Cc: John Fastabend, Eduard Zingerman, Kumar Kartikeya Dwivedi,
Martin KaFai Lau, Song Liu, Yonghong Song, Jiri Olsa,
Emil Tsalapatis, Ihor Solodrai, Barret Rhoden, bpf
On Tue, Dec 01, 2026 at 11:00 PM Anh Nguyen Hoang via B4 Relay <devnull+anhnh.4work.gmail.com@kernel.org> wrote:
> As a result, once an arena was mmap()ed with PROT_WRITE, map->writecnt
> stays elevated after munmap() and BPF_MAP_FREEZE on that arena fails
> with -EBUSY for the rest of the map's lifetime.
The count is off, but nothing is broken.
The freeze has no meaning for an arena.
pw-bot: cr
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-12-02 13:38 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02 7:00 [PATCH bpf] bpf: Drop the write-active count when a writable arena mapping closes Anh Nguyen Hoang via B4 Relay
2026-12-02 13:30 ` Alexei Starovoitov
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox