diff mbox series

[bpf-next] bpf: use kvmemdup_bpfptr helper

Message ID 1662721097-23793-1-git-send-email-wangyufen@huawei.com (mailing list archive)
State Superseded
Delegated to: BPF
Headers show
Series [bpf-next] bpf: use kvmemdup_bpfptr helper | expand

Checks

Context Check Description
bpf/vmtest-bpf-next-VM_Test-12 success Logs for test_progs_no_alu32 on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-15 success Logs for test_verifier on s390x with gcc
netdev/tree_selection success Clearly marked for bpf-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/subject_prefix success Link
netdev/cover_letter success Single patches do not need cover letters
netdev/patch_count success Link
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 2 this patch: 2
netdev/cc_maintainers success CCed 12 of 12 maintainers
netdev/build_clang success Errors and warnings before: 5 this patch: 5
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 2 this patch: 2
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 24 lines checked
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-next-VM_Test-9 success Logs for test_progs on s390x with gcc
bpf/vmtest-bpf-next-PR success PR summary
bpf/vmtest-bpf-next-VM_Test-6 success Logs for test_maps on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-1 success Logs for build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-16 success Logs for test_verifier on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-17 success Logs for test_verifier on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-7 success Logs for test_maps on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-8 success Logs for test_maps on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-10 success Logs for test_progs on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-11 success Logs for test_progs on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-13 success Logs for test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-14 success Logs for test_progs_no_alu32 on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-4 success Logs for llvm-toolchain
bpf/vmtest-bpf-next-VM_Test-5 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-2 success Logs for build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-3 success Logs for build for x86_64 with llvm-16

Commit Message

wangyufen Sept. 9, 2022, 10:58 a.m. UTC
Use kvmemdup_bpfptr helper instead of open-coding to
simplify the code.

Signed-off-by: Wang Yufen <wangyufen@huawei.com>
---
 kernel/bpf/syscall.c | 14 ++++----------
 1 file changed, 4 insertions(+), 10 deletions(-)

Comments

Stanislav Fomichev Sept. 9, 2022, 5:27 p.m. UTC | #1
On 09/09, Wang Yufen wrote:
> Use kvmemdup_bpfptr helper instead of open-coding to
> simplify the code.

> Signed-off-by: Wang Yufen <wangyufen@huawei.com>
> ---
>   kernel/bpf/syscall.c | 14 ++++----------
>   1 file changed, 4 insertions(+), 10 deletions(-)

> diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
> index 4e9d4622aef7..13ce28081982 100644
> --- a/kernel/bpf/syscall.c
> +++ b/kernel/bpf/syscall.c
> @@ -1413,20 +1413,14 @@ static int map_update_elem(union bpf_attr *attr,  
> bpfptr_t uattr)
>   	}

>   	value_size = bpf_map_value_size(map);
> -
> -	err = -ENOMEM;
> -	value = kvmalloc(value_size, GFP_USER | __GFP_NOWARN);
> -	if (!value)
> +	value = kvmemdup_bpfptr(uvalue, value_size);
> +	if (IS_ERR(value)) {
> +		err = PTR_ERR(value);
>   		goto free_key;
> -
> -	err = -EFAULT;
> -	if (copy_from_bpfptr(value, uvalue, value_size) != 0)
> -		goto free_value;
> +	}

>   	err = bpf_map_update_value(map, f, key, value, attr->flags);


[..]

> -free_value:
> -	kvfree(value);

And here you leak the value. We need to free it after update regardless
of error/success. That's why it is coded like that.

>   free_key:
>   	kvfree(key);
>   err_put:
> --
> 2.25.1
wangyufen Sept. 13, 2022, 4:24 a.m. UTC | #2
在 2022/9/10 1:27, sdf@google.com 写道:
> On 09/09, Wang Yufen wrote:
>> Use kvmemdup_bpfptr helper instead of open-coding to
>> simplify the code.
>
>> Signed-off-by: Wang Yufen <wangyufen@huawei.com>
>> ---
>>   kernel/bpf/syscall.c | 14 ++++----------
>>   1 file changed, 4 insertions(+), 10 deletions(-)
>
>> diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
>> index 4e9d4622aef7..13ce28081982 100644
>> --- a/kernel/bpf/syscall.c
>> +++ b/kernel/bpf/syscall.c
>> @@ -1413,20 +1413,14 @@ static int map_update_elem(union bpf_attr 
>> *attr, bpfptr_t uattr)
>>       }
>
>>       value_size = bpf_map_value_size(map);
>> -
>> -    err = -ENOMEM;
>> -    value = kvmalloc(value_size, GFP_USER | __GFP_NOWARN);
>> -    if (!value)
>> +    value = kvmemdup_bpfptr(uvalue, value_size);
>> +    if (IS_ERR(value)) {
>> +        err = PTR_ERR(value);
>>           goto free_key;
>> -
>> -    err = -EFAULT;
>> -    if (copy_from_bpfptr(value, uvalue, value_size) != 0)
>> -        goto free_value;
>> +    }
>
>>       err = bpf_map_update_value(map, f, key, value, attr->flags);
>
>
> [..]
>
>> -free_value:
>> -    kvfree(value);
>
> And here you leak the value. We need to free it after update regardless
> of error/success. That's why it is coded like that.

Thanks for your comments.

It's my mistake.

Can I keep kvfree(value);    and send v2?

>
>>   free_key:
>>       kvfree(key);
>>   err_put:
>> -- 
>> 2.25.1
>
diff mbox series

Patch

diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index 4e9d4622aef7..13ce28081982 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -1413,20 +1413,14 @@  static int map_update_elem(union bpf_attr *attr, bpfptr_t uattr)
 	}
 
 	value_size = bpf_map_value_size(map);
-
-	err = -ENOMEM;
-	value = kvmalloc(value_size, GFP_USER | __GFP_NOWARN);
-	if (!value)
+	value = kvmemdup_bpfptr(uvalue, value_size);
+	if (IS_ERR(value)) {
+		err = PTR_ERR(value);
 		goto free_key;
-
-	err = -EFAULT;
-	if (copy_from_bpfptr(value, uvalue, value_size) != 0)
-		goto free_value;
+	}
 
 	err = bpf_map_update_value(map, f, key, value, attr->flags);
 
-free_value:
-	kvfree(value);
 free_key:
 	kvfree(key);
 err_put: