diff mbox series

bpf: generate const static pointers for kernel helpers

Message ID 20240127185031.29854-1-jose.marchesi@oracle.com (mailing list archive)
State Accepted
Commit ff2071a7b7fd77908417603c4a785822939b3841
Delegated to: BPF
Headers show
Series bpf: generate const static pointers for kernel helpers | expand

Checks

Context Check Description
netdev/tree_selection success Not a local patch
bpf/vmtest-bpf-next-PR success PR summary
bpf/vmtest-bpf-next-VM_Test-0 success Logs for Lint
bpf/vmtest-bpf-next-VM_Test-3 success Logs for Validate matrix.py
bpf/vmtest-bpf-next-VM_Test-2 success Logs for Unittests
bpf/vmtest-bpf-next-VM_Test-1 success Logs for ShellCheck
bpf/vmtest-bpf-next-VM_Test-5 success Logs for aarch64-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-11 pending Logs for s390x-gcc / build / build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-12 success Logs for s390x-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-9 success Logs for aarch64-gcc / test (test_verifier, false, 360) / test_verifier on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-15 success Logs for x86_64-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-4 success Logs for aarch64-gcc / build / build for aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-6 success Logs for aarch64-gcc / test (test_maps, false, 360) / test_maps on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-13 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-10 success Logs for aarch64-gcc / veristat
bpf/vmtest-bpf-next-VM_Test-7 success Logs for aarch64-gcc / test (test_progs, false, 360) / test_progs on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-8 success Logs for aarch64-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-30 success Logs for x86_64-llvm-18 / build / build for x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-14 success Logs for x86_64-gcc / build / build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-29 success Logs for x86_64-llvm-17 / veristat
bpf/vmtest-bpf-next-VM_Test-37 success Logs for x86_64-llvm-18 / veristat
bpf/vmtest-bpf-next-VM_Test-23 success Logs for x86_64-llvm-17 / build / build for x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-20 success Logs for x86_64-gcc / test (test_progs_parallel, true, 30) / test_progs_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-25 success Logs for x86_64-llvm-17 / test (test_maps, false, 360) / test_maps on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-16 success Logs for x86_64-gcc / test (test_maps, false, 360) / test_maps on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-26 success Logs for x86_64-llvm-17 / test (test_progs, false, 360) / test_progs on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-21 success Logs for x86_64-gcc / test (test_verifier, false, 360) / test_verifier on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-31 success Logs for x86_64-llvm-18 / build-release / build for x86_64 with llvm-18 and -O2 optimization
bpf/vmtest-bpf-next-VM_Test-36 success Logs for x86_64-llvm-18 / test (test_verifier, false, 360) / test_verifier on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-19 success Logs for x86_64-gcc / test (test_progs_no_alu32_parallel, true, 30) / test_progs_no_alu32_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-32 success Logs for x86_64-llvm-18 / test (test_maps, false, 360) / test_maps on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-28 success Logs for x86_64-llvm-17 / test (test_verifier, false, 360) / test_verifier on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-22 success Logs for x86_64-gcc / veristat / veristat on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-27 success Logs for x86_64-llvm-17 / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-18 success Logs for x86_64-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-34 success Logs for x86_64-llvm-18 / test (test_progs_cpuv4, false, 360) / test_progs_cpuv4 on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-35 success Logs for x86_64-llvm-18 / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-17 success Logs for x86_64-gcc / test (test_progs, false, 360) / test_progs on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-33 success Logs for x86_64-llvm-18 / test (test_progs, false, 360) / test_progs on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-24 success Logs for x86_64-llvm-17 / build-release / build for x86_64 with llvm-17 and -O2 optimization

Commit Message

Jose E. Marchesi Jan. 27, 2024, 6:50 p.m. UTC
The generated bpf_helper_defs.h file currently contains definitions
like this for the kernel helpers, which are static objects:

  static void *(*bpf_map_lookup_elem)(void *map, const void *key) = (void *) 1;

These work well in both clang and GCC because both compilers do
constant propagation with -O1 and higher optimization, resulting in
`call 1' BPF instructions being generated, which are calls to kernel
helpers.

However, there is a discrepancy on how the -Wunused-variable
warning (activated by -Wall) is handled in these compilers:

- clang will not emit -Wunused-variable warnings for static variables
  defined in C header files, be them constant or not constant.

- GCC will not emit -Wunused-variable warnings for _constant_ static
  variables defined in header files, but it will emit warnings for
  non-constant static variables defined in header files.

There is no reason for these bpf_helpers_def.h pointers to not be
declared constant, and it is actually desirable to do so, since their
values are not to be changed.  So this patch modifies bpf_doc.py to
generate prototypes like:

  static void *(* const bpf_map_lookup_elem)(void *map, const void *key) = (void *) 1;

This allows GCC to not error while compiling BPF programs with `-Wall
-Werror', while still being able to detect and error on legitimate
unused variables in the program themselves.

This change doesn't impact the desired constant propagation in neither
Clang nor GCC with -O1 and higher.  On the contrary, being declared as
constant may increase the odds they get constant folded when
used/referred to in certain circumstances.

Tested in bpf-next master.
No regressions.

Signed-off-by: Jose E. Marchesi <jose.marchesi@oracle.com>
Cc: alexei.starovoitov@gmail.com
Cc: yonghong.song@linux.dev
Cc: eddyz87@gmail.com
Cc: cupertino.miranda@oracle.com
Cc: david.faust@oracle.com
---
 scripts/bpf_doc.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Yonghong Song Jan. 27, 2024, 8:29 p.m. UTC | #1
On 1/27/24 10:50 AM, Jose E. Marchesi wrote:
> The generated bpf_helper_defs.h file currently contains definitions
> like this for the kernel helpers, which are static objects:
>
>    static void *(*bpf_map_lookup_elem)(void *map, const void *key) = (void *) 1;
>
> These work well in both clang and GCC because both compilers do
> constant propagation with -O1 and higher optimization, resulting in
> `call 1' BPF instructions being generated, which are calls to kernel
> helpers.
>
> However, there is a discrepancy on how the -Wunused-variable
> warning (activated by -Wall) is handled in these compilers:
>
> - clang will not emit -Wunused-variable warnings for static variables
>    defined in C header files, be them constant or not constant.
>
> - GCC will not emit -Wunused-variable warnings for _constant_ static
>    variables defined in header files, but it will emit warnings for
>    non-constant static variables defined in header files.
>
> There is no reason for these bpf_helpers_def.h pointers to not be
> declared constant, and it is actually desirable to do so, since their
> values are not to be changed.  So this patch modifies bpf_doc.py to
> generate prototypes like:
>
>    static void *(* const bpf_map_lookup_elem)(void *map, const void *key) = (void *) 1;
>
> This allows GCC to not error while compiling BPF programs with `-Wall
> -Werror', while still being able to detect and error on legitimate
> unused variables in the program themselves.
>
> This change doesn't impact the desired constant propagation in neither
> Clang nor GCC with -O1 and higher.  On the contrary, being declared as
> constant may increase the odds they get constant folded when
> used/referred to in certain circumstances.
>
> Tested in bpf-next master.
> No regressions.
>
> Signed-off-by: Jose E. Marchesi <jose.marchesi@oracle.com>
> Cc: alexei.starovoitov@gmail.com
> Cc: yonghong.song@linux.dev
> Cc: eddyz87@gmail.com
> Cc: cupertino.miranda@oracle.com
> Cc: david.faust@oracle.com

LGTM. Please add proper tag in the commit subject, e.g.,
"[PATCH bpf-next]" instead of "[PATCH]", so CI can pick
up the patch and do proper test.

Acked-by: Yonghong Song <yonghong.song@linux.dev>

> ---
>   scripts/bpf_doc.py | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/scripts/bpf_doc.py b/scripts/bpf_doc.py
> index 61b7dddedc46..2b94749c99cc 100755
> --- a/scripts/bpf_doc.py
> +++ b/scripts/bpf_doc.py
> @@ -827,7 +827,7 @@ class PrinterHelpers(Printer):
>                   print(' *{}{}'.format(' \t' if line else '', line))
>   
>           print(' */')
> -        print('static %s %s(*%s)(' % (self.map_type(proto['ret_type']),
> +        print('static %s %s(* const %s)(' % (self.map_type(proto['ret_type']),
>                                         proto['ret_star'], proto['name']), end='')
>           comma = ''
>           for i, a in enumerate(proto['args']):
Jose E. Marchesi Jan. 28, 2024, 11:57 a.m. UTC | #2
> On 1/27/24 10:50 AM, Jose E. Marchesi wrote:
>> The generated bpf_helper_defs.h file currently contains definitions
>> like this for the kernel helpers, which are static objects:
>>
>>    static void *(*bpf_map_lookup_elem)(void *map, const void *key) = (void *) 1;
>>
>> These work well in both clang and GCC because both compilers do
>> constant propagation with -O1 and higher optimization, resulting in
>> `call 1' BPF instructions being generated, which are calls to kernel
>> helpers.
>>
>> However, there is a discrepancy on how the -Wunused-variable
>> warning (activated by -Wall) is handled in these compilers:
>>
>> - clang will not emit -Wunused-variable warnings for static variables
>>    defined in C header files, be them constant or not constant.
>>
>> - GCC will not emit -Wunused-variable warnings for _constant_ static
>>    variables defined in header files, but it will emit warnings for
>>    non-constant static variables defined in header files.
>>
>> There is no reason for these bpf_helpers_def.h pointers to not be
>> declared constant, and it is actually desirable to do so, since their
>> values are not to be changed.  So this patch modifies bpf_doc.py to
>> generate prototypes like:
>>
>>    static void *(* const bpf_map_lookup_elem)(void *map, const void *key) = (void *) 1;
>>
>> This allows GCC to not error while compiling BPF programs with `-Wall
>> -Werror', while still being able to detect and error on legitimate
>> unused variables in the program themselves.
>>
>> This change doesn't impact the desired constant propagation in neither
>> Clang nor GCC with -O1 and higher.  On the contrary, being declared as
>> constant may increase the odds they get constant folded when
>> used/referred to in certain circumstances.
>>
>> Tested in bpf-next master.
>> No regressions.
>>
>> Signed-off-by: Jose E. Marchesi <jose.marchesi@oracle.com>
>> Cc: alexei.starovoitov@gmail.com
>> Cc: yonghong.song@linux.dev
>> Cc: eddyz87@gmail.com
>> Cc: cupertino.miranda@oracle.com
>> Cc: david.faust@oracle.com
>
> LGTM. Please add proper tag in the commit subject, e.g.,
> "[PATCH bpf-next]" instead of "[PATCH]", so CI can pick
> up the patch and do proper test.

Sorry about that.  I will use the proper Subject in future patches.

>
> Acked-by: Yonghong Song <yonghong.song@linux.dev>
>
>> ---
>>   scripts/bpf_doc.py | 2 +-
>>   1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/scripts/bpf_doc.py b/scripts/bpf_doc.py
>> index 61b7dddedc46..2b94749c99cc 100755
>> --- a/scripts/bpf_doc.py
>> +++ b/scripts/bpf_doc.py
>> @@ -827,7 +827,7 @@ class PrinterHelpers(Printer):
>>                   print(' *{}{}'.format(' \t' if line else '', line))
>>             print(' */')
>> -        print('static %s %s(*%s)(' % (self.map_type(proto['ret_type']),
>> +        print('static %s %s(* const %s)(' % (self.map_type(proto['ret_type']),
>>                                         proto['ret_star'], proto['name']), end='')
>>           comma = ''
>>           for i, a in enumerate(proto['args']):
patchwork-bot+netdevbpf@kernel.org Jan. 30, 2024, 2 a.m. UTC | #3
Hello:

This patch was applied to bpf/bpf-next.git (master)
by Andrii Nakryiko <andrii@kernel.org>:

On Sat, 27 Jan 2024 19:50:31 +0100 you wrote:
> The generated bpf_helper_defs.h file currently contains definitions
> like this for the kernel helpers, which are static objects:
> 
>   static void *(*bpf_map_lookup_elem)(void *map, const void *key) = (void *) 1;
> 
> These work well in both clang and GCC because both compilers do
> constant propagation with -O1 and higher optimization, resulting in
> `call 1' BPF instructions being generated, which are calls to kernel
> helpers.
> 
> [...]

Here is the summary with links:
  - bpf: generate const static pointers for kernel helpers
    https://git.kernel.org/bpf/bpf-next/c/ff2071a7b7fd

You are awesome, thank you!
diff mbox series

Patch

diff --git a/scripts/bpf_doc.py b/scripts/bpf_doc.py
index 61b7dddedc46..2b94749c99cc 100755
--- a/scripts/bpf_doc.py
+++ b/scripts/bpf_doc.py
@@ -827,7 +827,7 @@  class PrinterHelpers(Printer):
                 print(' *{}{}'.format(' \t' if line else '', line))
 
         print(' */')
-        print('static %s %s(*%s)(' % (self.map_type(proto['ret_type']),
+        print('static %s %s(* const %s)(' % (self.map_type(proto['ret_type']),
                                       proto['ret_star'], proto['name']), end='')
         comma = ''
         for i, a in enumerate(proto['args']):