Skip to content

Commit dfa94ce

Browse files
Pei XiaoAlexei Starovoitov
authored andcommitted
bpf: Use refcount_t instead of atomic_t for mmap_count
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]> Link: https://lore.kernel.org/r/6ecce439a6bc81adb85d5080908ea8959b792a50.1735542814.git.xiaopei01@kylinos.cn Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 654a338 commit dfa94ce

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

kernel/bpf/arena.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ static u64 arena_map_mem_usage(const struct bpf_map *map)
218218
struct vma_list {
219219
struct vm_area_struct *vma;
220220
struct list_head head;
221-
atomic_t mmap_count;
221+
refcount_t mmap_count;
222222
};
223223

224224
static int remember_vma(struct bpf_arena *arena, struct vm_area_struct *vma)
@@ -228,7 +228,7 @@ static int remember_vma(struct bpf_arena *arena, struct vm_area_struct *vma)
228228
vml = kmalloc(sizeof(*vml), GFP_KERNEL);
229229
if (!vml)
230230
return -ENOMEM;
231-
atomic_set(&vml->mmap_count, 1);
231+
refcount_set(&vml->mmap_count, 1);
232232
vma->vm_private_data = vml;
233233
vml->vma = vma;
234234
list_add(&vml->head, &arena->vma_list);
@@ -239,7 +239,7 @@ static void arena_vm_open(struct vm_area_struct *vma)
239239
{
240240
struct vma_list *vml = vma->vm_private_data;
241241

242-
atomic_inc(&vml->mmap_count);
242+
refcount_inc(&vml->mmap_count);
243243
}
244244

245245
static void arena_vm_close(struct vm_area_struct *vma)
@@ -248,7 +248,7 @@ static void arena_vm_close(struct vm_area_struct *vma)
248248
struct bpf_arena *arena = container_of(map, struct bpf_arena, map);
249249
struct vma_list *vml = vma->vm_private_data;
250250

251-
if (!atomic_dec_and_test(&vml->mmap_count))
251+
if (!refcount_dec_and_test(&vml->mmap_count))
252252
return;
253253
guard(mutex)(&arena->lock);
254254
/* update link list under lock */

0 commit comments

Comments
 (0)