* [PATCH] bpf: Use refcount_t instead of atomic_t for mmap_count
@ 2024-12-30 7:16 Pei Xiao
2024-12-30 16:40 ` Jiri Olsa
2024-12-31 4:20 ` patchwork-bot+netdevbpf
0 siblings, 2 replies; 4+ messages in thread
From: Pei Xiao @ 2024-12-30 7:16 UTC (permalink / raw)
To: bpf, linux-kernel; +Cc: Pei Xiao, kernel test robot
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: b90d77e5fd78 ("bpf: Fix remap of arena.")
Reported-by: kernel test robot <lkp@intel.com>
Closes: https://lore.kernel.org/oe-kbuild-all/202412292037.LXlYSHKl-lkp@intel.com/
Signed-off-by: Pei Xiao <xiaopei01@kylinos.cn>
---
kernel/bpf/arena.c | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/kernel/bpf/arena.c b/kernel/bpf/arena.c
index 945a5680f6a5..8caf56a308d9 100644
--- a/kernel/bpf/arena.c
+++ b/kernel/bpf/arena.c
@@ -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)
@@ -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);
@@ -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)
@@ -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 */
--
2.25.1
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] bpf: Use refcount_t instead of atomic_t for mmap_count
2024-12-30 7:16 [PATCH] bpf: Use refcount_t instead of atomic_t for mmap_count Pei Xiao
@ 2024-12-30 16:40 ` Jiri Olsa
2024-12-31 3:00 ` Pei Xiao
2024-12-31 4:20 ` patchwork-bot+netdevbpf
1 sibling, 1 reply; 4+ messages in thread
From: Jiri Olsa @ 2024-12-30 16:40 UTC (permalink / raw)
To: Pei Xiao; +Cc: bpf, linux-kernel, kernel test robot
On Mon, Dec 30, 2024 at 03:16:55PM +0800, Pei Xiao wrote:
> Use an API that resembles more the actual use of mmap_count.
I'm not sure I understand the issue, could you provide more details?
thanks,
jirka
>
> Found by cocci:
> kernel/bpf/arena.c:245:6-25: WARNING: atomic_dec_and_test variation before object free at line 249.
>
> Fixes: b90d77e5fd78 ("bpf: Fix remap of arena.")
> Reported-by: kernel test robot <lkp@intel.com>
> Closes: https://lore.kernel.org/oe-kbuild-all/202412292037.LXlYSHKl-lkp@intel.com/
> Signed-off-by: Pei Xiao <xiaopei01@kylinos.cn>
> ---
> kernel/bpf/arena.c | 8 ++++----
> 1 file changed, 4 insertions(+), 4 deletions(-)
>
> diff --git a/kernel/bpf/arena.c b/kernel/bpf/arena.c
> index 945a5680f6a5..8caf56a308d9 100644
> --- a/kernel/bpf/arena.c
> +++ b/kernel/bpf/arena.c
> @@ -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)
> @@ -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);
> @@ -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)
> @@ -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 */
> --
> 2.25.1
>
>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] bpf: Use refcount_t instead of atomic_t for mmap_count
2024-12-30 16:40 ` Jiri Olsa
@ 2024-12-31 3:00 ` Pei Xiao
0 siblings, 0 replies; 4+ messages in thread
From: Pei Xiao @ 2024-12-31 3:00 UTC (permalink / raw)
To: Jiri Olsa; +Cc: bpf, linux-kernel, kernel test robot
在 2024/12/31 00:40, Jiri Olsa 写道:
> On Mon, Dec 30, 2024 at 03:16:55PM +0800, Pei Xiao wrote:
>> Use an API that resembles more the actual use of mmap_count.
>
> I'm not sure I understand the issue, could you provide more details?
>
hi,
refcount_t type which allows us to catch overflow and underflow issues.
thanks!
Pei.
> thanks,
> jirka
>
>>
>> Found by cocci:
>> kernel/bpf/arena.c:245:6-25: WARNING: atomic_dec_and_test variation before object free at line 249.
>>
>> Fixes: b90d77e5fd78 ("bpf: Fix remap of arena.")
>> Reported-by: kernel test robot <lkp@intel.com>
>> Closes: https://lore.kernel.org/oe-kbuild-all/202412292037.LXlYSHKl-lkp@intel.com/
>> Signed-off-by: Pei Xiao <xiaopei01@kylinos.cn>
>> ---
>> kernel/bpf/arena.c | 8 ++++----
>> 1 file changed, 4 insertions(+), 4 deletions(-)
>>
>> diff --git a/kernel/bpf/arena.c b/kernel/bpf/arena.c
>> index 945a5680f6a5..8caf56a308d9 100644
>> --- a/kernel/bpf/arena.c
>> +++ b/kernel/bpf/arena.c
>> @@ -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)
>> @@ -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);
>> @@ -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)
>> @@ -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 */
>> --
>> 2.25.1
>>
>>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] bpf: Use refcount_t instead of atomic_t for mmap_count
2024-12-30 7:16 [PATCH] bpf: Use refcount_t instead of atomic_t for mmap_count Pei Xiao
2024-12-30 16:40 ` Jiri Olsa
@ 2024-12-31 4:20 ` patchwork-bot+netdevbpf
1 sibling, 0 replies; 4+ messages in thread
From: patchwork-bot+netdevbpf @ 2024-12-31 4:20 UTC (permalink / raw)
To: Pei Xiao; +Cc: bpf, linux-kernel, lkp
Hello:
This patch was applied to bpf/bpf-next.git (master)
by Alexei Starovoitov <ast@kernel.org>:
On Mon, 30 Dec 2024 15:16:55 +0800 you wrote:
> 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: b90d77e5fd78 ("bpf: Fix remap of arena.")
> Reported-by: kernel test robot <lkp@intel.com>
> Closes: https://lore.kernel.org/oe-kbuild-all/202412292037.LXlYSHKl-lkp@intel.com/
> Signed-off-by: Pei Xiao <xiaopei01@kylinos.cn>
>
> [...]
Here is the summary with links:
- bpf: Use refcount_t instead of atomic_t for mmap_count
https://git.kernel.org/bpf/bpf-next/c/dfa94ce54f41
You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2024-12-31 4:20 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-12-30 7:16 [PATCH] bpf: Use refcount_t instead of atomic_t for mmap_count Pei Xiao
2024-12-30 16:40 ` Jiri Olsa
2024-12-31 3:00 ` Pei Xiao
2024-12-31 4:20 ` 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