public inbox for bpf@vger.kernel.org
 help / color / mirror / Atom feed
* Re: [PATCH] selftests/bpf: Remove unneeded variable "ret"
       [not found] ` <6228af14241b831be4bae6ebcd63799e@208suo.com>
@ 2023-06-13 13:42   ` Yonghong Song
  2023-06-13 18:13     ` Jakub Sitnicki
  0 siblings, 1 reply; 2+ messages in thread
From: Yonghong Song @ 2023-06-13 13:42 UTC (permalink / raw)
  To: baomingtong001, ast, daniel, andrii, martin.lau, song, yhs,
	john.fastabend, kpsingh, sdf, haoluo, jolsa, mykolal, shuah,
	jakub, Jakub Kicinski
  Cc: bpf, linux-kselftest, linux-kernel



On 6/13/23 1:50 AM, baomingtong001@208suo.com wrote:
> Fix the following coccicheck warning:
> 
> tools/testing/selftests/bpf/progs/tailcall_bpf2bpf6.c:28:14-17: Unneeded 
> variable: "ret".
> 
> Return "1".
> 
> Signed-off-by: Mingtong Bao <baomingtong001@208suo.com>
> ---
>   tools/testing/selftests/bpf/progs/tailcall_bpf2bpf6.c | 3 +--
>   1 file changed, 1 insertion(+), 2 deletions(-)
> 
> diff --git a/tools/testing/selftests/bpf/progs/tailcall_bpf2bpf6.c 
> b/tools/testing/selftests/bpf/progs/tailcall_bpf2bpf6.c
> index 4a9f63bea66c..7f0146682577 100644
> --- a/tools/testing/selftests/bpf/progs/tailcall_bpf2bpf6.c
> +++ b/tools/testing/selftests/bpf/progs/tailcall_bpf2bpf6.c
> @@ -25,10 +25,9 @@ static __noinline
>   int subprog_tail(struct __sk_buff *skb)
>   {
>       /* Don't propagate the constant to the caller */
> -    volatile int ret = 1;
> 
>       bpf_tail_call_static(skb, &jmp_table, 0);
> -    return ret;
> +    return 1;

Please pay attention to the comment:
    /* Don't propagate the constant to the caller */
which clearly says 'constant' is not preferred.

The patch introduced this change is:
     5e0b0a4c52d30   selftests/bpf: Test tail call counting with bpf2bpf 
and data on stack

The test intentionally want to:
   'Specifically when the size 

     of data allocated on BPF stack is not a multiple on 8.'

Note that with volatile and without volatile, the generated
code will be different and it will result in different
verification path.

cc Jakub for further clarification.

>   }
> 
>   SEC("tc")

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] selftests/bpf: Remove unneeded variable "ret"
  2023-06-13 13:42   ` [PATCH] selftests/bpf: Remove unneeded variable "ret" Yonghong Song
@ 2023-06-13 18:13     ` Jakub Sitnicki
  0 siblings, 0 replies; 2+ messages in thread
From: Jakub Sitnicki @ 2023-06-13 18:13 UTC (permalink / raw)
  To: Yonghong Song
  Cc: baomingtong001, ast, daniel, andrii, martin.lau, song, yhs,
	john.fastabend, kpsingh, sdf, haoluo, jolsa, mykolal, shuah,
	Jakub Kicinski, bpf, linux-kselftest, linux-kernel

On Tue, Jun 13, 2023 at 06:42 AM -07, Yonghong Song wrote:
> On 6/13/23 1:50 AM, baomingtong001@208suo.com wrote:
>> Fix the following coccicheck warning:
>> tools/testing/selftests/bpf/progs/tailcall_bpf2bpf6.c:28:14-17: Unneeded
>> variable: "ret".
>> Return "1".
>> Signed-off-by: Mingtong Bao <baomingtong001@208suo.com>
>> ---
>>   tools/testing/selftests/bpf/progs/tailcall_bpf2bpf6.c | 3 +--
>>   1 file changed, 1 insertion(+), 2 deletions(-)
>> diff --git a/tools/testing/selftests/bpf/progs/tailcall_bpf2bpf6.c
>> b/tools/testing/selftests/bpf/progs/tailcall_bpf2bpf6.c
>> index 4a9f63bea66c..7f0146682577 100644
>> --- a/tools/testing/selftests/bpf/progs/tailcall_bpf2bpf6.c
>> +++ b/tools/testing/selftests/bpf/progs/tailcall_bpf2bpf6.c
>> @@ -25,10 +25,9 @@ static __noinline
>>   int subprog_tail(struct __sk_buff *skb)
>>   {
>>       /* Don't propagate the constant to the caller */
>> -    volatile int ret = 1;
>>       bpf_tail_call_static(skb, &jmp_table, 0);
>> -    return ret;
>> +    return 1;
>
> Please pay attention to the comment:
>    /* Don't propagate the constant to the caller */
> which clearly says 'constant' is not preferred.
>
> The patch introduced this change is:
>     5e0b0a4c52d30   selftests/bpf: Test tail call counting with bpf2bpf and data
>    on stack
>
> The test intentionally want to:
>   'Specifically when the size      of data allocated on BPF stack is not a
>  multiple on 8.'
>
> Note that with volatile and without volatile, the generated
> code will be different and it will result in different
> verification path.
>
> cc Jakub for further clarification.

Yonghong is right. We can't replace it like that.

Compiler is smart and pull up the constant into subprog_tail() caller.

And it doesn't have the slightest idea that bpf_tail_call_static() is
actually tail call (destroy frame + jump) and control doesn't return to
subprog_tail().

(You can read more about BPF tail calls in [1] and [2] if they are not
familiar.)

IOW, we need an r0 store to happen after a call to BPF tail call helper
(call 12) to remain in subprog_tail body for the regression test to
work:

$ llvm-objdump -d --no-show-raw-insn tailcall_bpf2bpf6.bpf.o

tailcall_bpf2bpf6.bpf.o:        file format elf64-bpf

Disassembly of section .text:

0000000000000000 <subprog_tail>:
       0:       r6 = r1
       1:       w1 = 1
       2:       *(u32 *)(r10 - 4) = r1
       3:       r7 = 0 ll
       5:       r1 = r6
       6:       r2 = r7
       7:       r3 = 0
       8:       call 12
       9:       r0 = *(u32 *)(r10 - 4) <-- this must stay
      10:       exit

You could take a shot at replacing it with inline asm, if you want.

[1] https://docs.cilium.io/en/stable/bpf/architecture/#bpf-to-bpf-calls
[2] https://blog.cloudflare.com/assembly-within-bpf-tail-calls-on-x86-and-arm/

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-06-13 18:25 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <20230613084315.62021-1-luojianhong@cdjrlc.com>
     [not found] ` <6228af14241b831be4bae6ebcd63799e@208suo.com>
2023-06-13 13:42   ` [PATCH] selftests/bpf: Remove unneeded variable "ret" Yonghong Song
2023-06-13 18:13     ` Jakub Sitnicki

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox