Skip to content

Commit

Permalink
bpf: Use refcount_t instead of atomic_t for mmap_count
Browse files Browse the repository at this point in the history
Use an API that resembles more the actual use of mmap_count.

Found by cocci:
kernel/bpf/arena.c:245:6-25: WARNING: atomic_dec_and_test variation before object free at line 249.

Fixes: b90d77e ("bpf: Fix remap of arena.")
Reported-by: kernel test robot <[email protected]>
Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
Signed-off-by: Pei Xiao <[email protected]>
  • Loading branch information
Pei Xiao authored and Kernel Patches Daemon committed Dec 30, 2024
1 parent 70e482f commit 2c6f73e
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions kernel/bpf/arena.c
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ static u64 arena_map_mem_usage(const struct bpf_map *map)
struct vma_list {
struct vm_area_struct *vma;
struct list_head head;
atomic_t mmap_count;
refcount_t mmap_count;
};

static int remember_vma(struct bpf_arena *arena, struct vm_area_struct *vma)
Expand All @@ -228,7 +228,7 @@ static int remember_vma(struct bpf_arena *arena, struct vm_area_struct *vma)
vml = kmalloc(sizeof(*vml), GFP_KERNEL);
if (!vml)
return -ENOMEM;
atomic_set(&vml->mmap_count, 1);
refcount_set(&vml->mmap_count, 1);
vma->vm_private_data = vml;
vml->vma = vma;
list_add(&vml->head, &arena->vma_list);
Expand All @@ -239,7 +239,7 @@ static void arena_vm_open(struct vm_area_struct *vma)
{
struct vma_list *vml = vma->vm_private_data;

atomic_inc(&vml->mmap_count);
refcount_inc(&vml->mmap_count);
}

static void arena_vm_close(struct vm_area_struct *vma)
Expand All @@ -248,7 +248,7 @@ 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;

if (!atomic_dec_and_test(&vml->mmap_count))
if (!refcount_dec_and_test(&vml->mmap_count))
return;
guard(mutex)(&arena->lock);
/* update link list under lock */
Expand Down

0 comments on commit 2c6f73e

Please sign in to comment.