diff mbox series

[bpf-next,v3,4/6] libbpf: Unify memory address casting operation style

Message ID 20220530092815.1112406-5-pulehui@huawei.com (mailing list archive)
State Changes Requested
Delegated to: BPF
Headers show
Series Support riscv jit to provide | expand

Checks

Context Check Description
bpf/vmtest-bpf-next-VM_Test-2 success Logs for Kernel LATEST on ubuntu-latest with llvm-15
bpf/vmtest-bpf-next-VM_Test-3 success Logs for Kernel LATEST on z15 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 Series has a cover letter
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: 0 this patch: 0
netdev/cc_maintainers success CCed 14 of 14 maintainers
netdev/build_clang success Errors and warnings before: 0 this patch: 0
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: 0 this patch: 0
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 27 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-1 fail Logs for Kernel LATEST on ubuntu-latest with gcc
bpf/vmtest-bpf-next-PR fail merge-conflict

Commit Message

Pu Lehui May 30, 2022, 9:28 a.m. UTC
The members of bpf_prog_info, which are line_info, jited_line_info,
jited_ksyms and jited_func_lens, store u64 address pointed to the
corresponding memory regions. Memory addresses are conceptually
unsigned, (unsigned long) casting makes more sense, so let's make
a change for conceptual uniformity.

Signed-off-by: Pu Lehui <pulehui@huawei.com>
---
 tools/lib/bpf/bpf_prog_linfo.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

Comments

Daniel Borkmann May 30, 2022, 9:03 p.m. UTC | #1
On 5/30/22 11:28 AM, Pu Lehui wrote:
> The members of bpf_prog_info, which are line_info, jited_line_info,
> jited_ksyms and jited_func_lens, store u64 address pointed to the
> corresponding memory regions. Memory addresses are conceptually
> unsigned, (unsigned long) casting makes more sense, so let's make
> a change for conceptual uniformity.
> 
> Signed-off-by: Pu Lehui <pulehui@huawei.com>
> ---
>   tools/lib/bpf/bpf_prog_linfo.c | 9 +++++----
>   1 file changed, 5 insertions(+), 4 deletions(-)
> 
> diff --git a/tools/lib/bpf/bpf_prog_linfo.c b/tools/lib/bpf/bpf_prog_linfo.c
> index 5c503096ef43..7beb060d0671 100644
> --- a/tools/lib/bpf/bpf_prog_linfo.c
> +++ b/tools/lib/bpf/bpf_prog_linfo.c
> @@ -127,7 +127,8 @@ struct bpf_prog_linfo *bpf_prog_linfo__new(const struct bpf_prog_info *info)
>   	prog_linfo->raw_linfo = malloc(data_sz);
>   	if (!prog_linfo->raw_linfo)
>   		goto err_free;
> -	memcpy(prog_linfo->raw_linfo, (void *)(long)info->line_info, data_sz);
> +	memcpy(prog_linfo->raw_linfo, (void *)(unsigned long)info->line_info,
> +	       data_sz);

Took in patch 1-3, lgtm, thanks! My question around the cleanups in patch 4-6 ...
there are various other such cases e.g. in libbpf, perhaps makes sense to clean all
of them up at once and not just the 4 locations in here.

Thanks,
Daniel
Andrii Nakryiko June 3, 2022, 9:03 p.m. UTC | #2
On Mon, May 30, 2022 at 2:03 PM Daniel Borkmann <daniel@iogearbox.net> wrote:
>
> On 5/30/22 11:28 AM, Pu Lehui wrote:
> > The members of bpf_prog_info, which are line_info, jited_line_info,
> > jited_ksyms and jited_func_lens, store u64 address pointed to the
> > corresponding memory regions. Memory addresses are conceptually
> > unsigned, (unsigned long) casting makes more sense, so let's make
> > a change for conceptual uniformity.
> >
> > Signed-off-by: Pu Lehui <pulehui@huawei.com>
> > ---
> >   tools/lib/bpf/bpf_prog_linfo.c | 9 +++++----
> >   1 file changed, 5 insertions(+), 4 deletions(-)
> >
> > diff --git a/tools/lib/bpf/bpf_prog_linfo.c b/tools/lib/bpf/bpf_prog_linfo.c
> > index 5c503096ef43..7beb060d0671 100644
> > --- a/tools/lib/bpf/bpf_prog_linfo.c
> > +++ b/tools/lib/bpf/bpf_prog_linfo.c
> > @@ -127,7 +127,8 @@ struct bpf_prog_linfo *bpf_prog_linfo__new(const struct bpf_prog_info *info)
> >       prog_linfo->raw_linfo = malloc(data_sz);
> >       if (!prog_linfo->raw_linfo)
> >               goto err_free;
> > -     memcpy(prog_linfo->raw_linfo, (void *)(long)info->line_info, data_sz);
> > +     memcpy(prog_linfo->raw_linfo, (void *)(unsigned long)info->line_info,
> > +            data_sz);
>
> Took in patch 1-3, lgtm, thanks! My question around the cleanups in patch 4-6 ...
> there are various other such cases e.g. in libbpf, perhaps makes sense to clean all
> of them up at once and not just the 4 locations in here.

if (void *)(long) pattern is wrong, then I guess the best replacement
should be (void *)(uintptr_t) ?

>
> Thanks,
> Daniel
Pu Lehui July 7, 2022, 11:49 a.m. UTC | #3
On 2022/5/31 5:03, Daniel Borkmann wrote:
> On 5/30/22 11:28 AM, Pu Lehui wrote:
>> The members of bpf_prog_info, which are line_info, jited_line_info,
>> jited_ksyms and jited_func_lens, store u64 address pointed to the
>> corresponding memory regions. Memory addresses are conceptually
>> unsigned, (unsigned long) casting makes more sense, so let's make
>> a change for conceptual uniformity.
>>
>> Signed-off-by: Pu Lehui <pulehui@huawei.com>
>> ---
>>   tools/lib/bpf/bpf_prog_linfo.c | 9 +++++----
>>   1 file changed, 5 insertions(+), 4 deletions(-)
>>
>> diff --git a/tools/lib/bpf/bpf_prog_linfo.c 
>> b/tools/lib/bpf/bpf_prog_linfo.c
>> index 5c503096ef43..7beb060d0671 100644
>> --- a/tools/lib/bpf/bpf_prog_linfo.c
>> +++ b/tools/lib/bpf/bpf_prog_linfo.c
>> @@ -127,7 +127,8 @@ struct bpf_prog_linfo *bpf_prog_linfo__new(const 
>> struct bpf_prog_info *info)
>>       prog_linfo->raw_linfo = malloc(data_sz);
>>       if (!prog_linfo->raw_linfo)
>>           goto err_free;
>> -    memcpy(prog_linfo->raw_linfo, (void *)(long)info->line_info, 
>> data_sz);
>> +    memcpy(prog_linfo->raw_linfo, (void *)(unsigned 
>> long)info->line_info,
>> +           data_sz);
> 
> Took in patch 1-3, lgtm, thanks! My question around the cleanups in 
> patch 4-6 ...
> there are various other such cases e.g. in libbpf, perhaps makes sense 
> to clean all
> of them up at once and not just the 4 locations in here.
> 

sorry for reply so late, I will take this soon.

> Thanks,
> Daniel
> .
Pu Lehui July 7, 2022, 12:23 p.m. UTC | #4
On 2022/6/4 5:03, Andrii Nakryiko wrote:
> On Mon, May 30, 2022 at 2:03 PM Daniel Borkmann <daniel@iogearbox.net> wrote:
>>
>> On 5/30/22 11:28 AM, Pu Lehui wrote:
>>> The members of bpf_prog_info, which are line_info, jited_line_info,
>>> jited_ksyms and jited_func_lens, store u64 address pointed to the
>>> corresponding memory regions. Memory addresses are conceptually
>>> unsigned, (unsigned long) casting makes more sense, so let's make
>>> a change for conceptual uniformity.
>>>
>>> Signed-off-by: Pu Lehui <pulehui@huawei.com>
>>> ---
>>>    tools/lib/bpf/bpf_prog_linfo.c | 9 +++++----
>>>    1 file changed, 5 insertions(+), 4 deletions(-)
>>>
>>> diff --git a/tools/lib/bpf/bpf_prog_linfo.c b/tools/lib/bpf/bpf_prog_linfo.c
>>> index 5c503096ef43..7beb060d0671 100644
>>> --- a/tools/lib/bpf/bpf_prog_linfo.c
>>> +++ b/tools/lib/bpf/bpf_prog_linfo.c
>>> @@ -127,7 +127,8 @@ struct bpf_prog_linfo *bpf_prog_linfo__new(const struct bpf_prog_info *info)
>>>        prog_linfo->raw_linfo = malloc(data_sz);
>>>        if (!prog_linfo->raw_linfo)
>>>                goto err_free;
>>> -     memcpy(prog_linfo->raw_linfo, (void *)(long)info->line_info, data_sz);
>>> +     memcpy(prog_linfo->raw_linfo, (void *)(unsigned long)info->line_info,
>>> +            data_sz);
>>
>> Took in patch 1-3, lgtm, thanks! My question around the cleanups in patch 4-6 ...
>> there are various other such cases e.g. in libbpf, perhaps makes sense to clean all
>> of them up at once and not just the 4 locations in here.
> 
> if (void *)(long) pattern is wrong, then I guess the best replacement
> should be (void *)(uintptr_t) ?
> 

I also think that (void *)(uintptr_t) would be the best replacement. I 
applied the changes to kernel/bpf and samples/bpf, and it worked fine. 
But in selftests/bpf, the following similar error occur at compile time:

progs/test_cls_redirect.c:504:11: error: cast to 'uint8_t *' (aka 
'unsigned char *') from smaller integer type 'uintptr_t' (aka 'unsigned 
int') [-Werror,-Wint-to-pointer-cast]
	.head = (uint8_t *)(uintptr_t)skb->data,

I take clang to compile with the front and back end separation, like 
samples/bpf, and it works. It seems that the all-in-one clang has 
problems handling the uintptr_t.

>>
>> Thanks,
>> Daniel
> .
>
Andrii Nakryiko July 8, 2022, 10:30 p.m. UTC | #5
On Thu, Jul 7, 2022 at 5:23 AM Pu Lehui <pulehui@huawei.com> wrote:
>
>
>
> On 2022/6/4 5:03, Andrii Nakryiko wrote:
> > On Mon, May 30, 2022 at 2:03 PM Daniel Borkmann <daniel@iogearbox.net> wrote:
> >>
> >> On 5/30/22 11:28 AM, Pu Lehui wrote:
> >>> The members of bpf_prog_info, which are line_info, jited_line_info,
> >>> jited_ksyms and jited_func_lens, store u64 address pointed to the
> >>> corresponding memory regions. Memory addresses are conceptually
> >>> unsigned, (unsigned long) casting makes more sense, so let's make
> >>> a change for conceptual uniformity.
> >>>
> >>> Signed-off-by: Pu Lehui <pulehui@huawei.com>
> >>> ---
> >>>    tools/lib/bpf/bpf_prog_linfo.c | 9 +++++----
> >>>    1 file changed, 5 insertions(+), 4 deletions(-)
> >>>
> >>> diff --git a/tools/lib/bpf/bpf_prog_linfo.c b/tools/lib/bpf/bpf_prog_linfo.c
> >>> index 5c503096ef43..7beb060d0671 100644
> >>> --- a/tools/lib/bpf/bpf_prog_linfo.c
> >>> +++ b/tools/lib/bpf/bpf_prog_linfo.c
> >>> @@ -127,7 +127,8 @@ struct bpf_prog_linfo *bpf_prog_linfo__new(const struct bpf_prog_info *info)
> >>>        prog_linfo->raw_linfo = malloc(data_sz);
> >>>        if (!prog_linfo->raw_linfo)
> >>>                goto err_free;
> >>> -     memcpy(prog_linfo->raw_linfo, (void *)(long)info->line_info, data_sz);
> >>> +     memcpy(prog_linfo->raw_linfo, (void *)(unsigned long)info->line_info,
> >>> +            data_sz);
> >>
> >> Took in patch 1-3, lgtm, thanks! My question around the cleanups in patch 4-6 ...
> >> there are various other such cases e.g. in libbpf, perhaps makes sense to clean all
> >> of them up at once and not just the 4 locations in here.
> >
> > if (void *)(long) pattern is wrong, then I guess the best replacement
> > should be (void *)(uintptr_t) ?
> >
>
> I also think that (void *)(uintptr_t) would be the best replacement. I
> applied the changes to kernel/bpf and samples/bpf, and it worked fine.
> But in selftests/bpf, the following similar error occur at compile time:
>
> progs/test_cls_redirect.c:504:11: error: cast to 'uint8_t *' (aka
> 'unsigned char *') from smaller integer type 'uintptr_t' (aka 'unsigned
> int') [-Werror,-Wint-to-pointer-cast]
>         .head = (uint8_t *)(uintptr_t)skb->data,

this is BPF-side code so using system's uintptr_t definition won't
work correctly here. Just do (unsigned long) instead?

>
> I take clang to compile with the front and back end separation, like
> samples/bpf, and it works. It seems that the all-in-one clang has
> problems handling the uintptr_t.
>
> >>
> >> Thanks,
> >> Daniel
> > .
> >
Pu Lehui July 9, 2022, 1:32 a.m. UTC | #6
On 2022/7/9 6:30, Andrii Nakryiko wrote:
> On Thu, Jul 7, 2022 at 5:23 AM Pu Lehui <pulehui@huawei.com> wrote:
>>
>>
>>
>> On 2022/6/4 5:03, Andrii Nakryiko wrote:
>>> On Mon, May 30, 2022 at 2:03 PM Daniel Borkmann <daniel@iogearbox.net> wrote:
>>>>
>>>> On 5/30/22 11:28 AM, Pu Lehui wrote:
>>>>> The members of bpf_prog_info, which are line_info, jited_line_info,
>>>>> jited_ksyms and jited_func_lens, store u64 address pointed to the
>>>>> corresponding memory regions. Memory addresses are conceptually
>>>>> unsigned, (unsigned long) casting makes more sense, so let's make
>>>>> a change for conceptual uniformity.
>>>>>
>>>>> Signed-off-by: Pu Lehui <pulehui@huawei.com>
>>>>> ---
>>>>>     tools/lib/bpf/bpf_prog_linfo.c | 9 +++++----
>>>>>     1 file changed, 5 insertions(+), 4 deletions(-)
>>>>>
>>>>> diff --git a/tools/lib/bpf/bpf_prog_linfo.c b/tools/lib/bpf/bpf_prog_linfo.c
>>>>> index 5c503096ef43..7beb060d0671 100644
>>>>> --- a/tools/lib/bpf/bpf_prog_linfo.c
>>>>> +++ b/tools/lib/bpf/bpf_prog_linfo.c
>>>>> @@ -127,7 +127,8 @@ struct bpf_prog_linfo *bpf_prog_linfo__new(const struct bpf_prog_info *info)
>>>>>         prog_linfo->raw_linfo = malloc(data_sz);
>>>>>         if (!prog_linfo->raw_linfo)
>>>>>                 goto err_free;
>>>>> -     memcpy(prog_linfo->raw_linfo, (void *)(long)info->line_info, data_sz);
>>>>> +     memcpy(prog_linfo->raw_linfo, (void *)(unsigned long)info->line_info,
>>>>> +            data_sz);
>>>>
>>>> Took in patch 1-3, lgtm, thanks! My question around the cleanups in patch 4-6 ...
>>>> there are various other such cases e.g. in libbpf, perhaps makes sense to clean all
>>>> of them up at once and not just the 4 locations in here.
>>>
>>> if (void *)(long) pattern is wrong, then I guess the best replacement
>>> should be (void *)(uintptr_t) ?
>>>
>>
>> I also think that (void *)(uintptr_t) would be the best replacement. I
>> applied the changes to kernel/bpf and samples/bpf, and it worked fine.
>> But in selftests/bpf, the following similar error occur at compile time:
>>
>> progs/test_cls_redirect.c:504:11: error: cast to 'uint8_t *' (aka
>> 'unsigned char *') from smaller integer type 'uintptr_t' (aka 'unsigned
>> int') [-Werror,-Wint-to-pointer-cast]
>>          .head = (uint8_t *)(uintptr_t)skb->data,
> 
> this is BPF-side code so using system's uintptr_t definition won't
> work correctly here. Just do (unsigned long) instead?
> 

It is fine by me, and for this cleanup

>>
>> I take clang to compile with the front and back end separation, like
>> samples/bpf, and it works. It seems that the all-in-one clang has
>> problems handling the uintptr_t.
>>
>>>>
>>>> Thanks,
>>>> Daniel
>>> .
>>>
> .
>
diff mbox series

Patch

diff --git a/tools/lib/bpf/bpf_prog_linfo.c b/tools/lib/bpf/bpf_prog_linfo.c
index 5c503096ef43..7beb060d0671 100644
--- a/tools/lib/bpf/bpf_prog_linfo.c
+++ b/tools/lib/bpf/bpf_prog_linfo.c
@@ -127,7 +127,8 @@  struct bpf_prog_linfo *bpf_prog_linfo__new(const struct bpf_prog_info *info)
 	prog_linfo->raw_linfo = malloc(data_sz);
 	if (!prog_linfo->raw_linfo)
 		goto err_free;
-	memcpy(prog_linfo->raw_linfo, (void *)(long)info->line_info, data_sz);
+	memcpy(prog_linfo->raw_linfo, (void *)(unsigned long)info->line_info,
+	       data_sz);
 
 	nr_jited_func = info->nr_jited_ksyms;
 	if (!nr_jited_func ||
@@ -148,7 +149,7 @@  struct bpf_prog_linfo *bpf_prog_linfo__new(const struct bpf_prog_info *info)
 	if (!prog_linfo->raw_jited_linfo)
 		goto err_free;
 	memcpy(prog_linfo->raw_jited_linfo,
-	       (void *)(long)info->jited_line_info, data_sz);
+	       (void *)(unsigned long)info->jited_line_info, data_sz);
 
 	/* Number of jited_line_info per jited func */
 	prog_linfo->nr_jited_linfo_per_func = malloc(nr_jited_func *
@@ -166,8 +167,8 @@  struct bpf_prog_linfo *bpf_prog_linfo__new(const struct bpf_prog_info *info)
 		goto err_free;
 
 	if (dissect_jited_func(prog_linfo,
-			       (__u64 *)(long)info->jited_ksyms,
-			       (__u32 *)(long)info->jited_func_lens))
+			       (__u64 *)(unsigned long)info->jited_ksyms,
+			       (__u32 *)(unsigned long)info->jited_func_lens))
 		goto err_free;
 
 	return prog_linfo;