* [PATCH] bpf: generate const static pointers for kernel helpers
@ 2024-01-27 18:50 Jose E. Marchesi
2024-01-27 20:29 ` Yonghong Song
2024-01-30 2:00 ` patchwork-bot+netdevbpf
0 siblings, 2 replies; 4+ messages in thread
From: Jose E. Marchesi @ 2024-01-27 18:50 UTC (permalink / raw)
To: bpf
Cc: Jose E . Marchesi, alexei.starovoitov, yonghong.song, eddyz87,
cupertino.miranda, david.faust
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(-)
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']):
--
2.30.2
^ permalink raw reply related [flat|nested] 4+ messages in thread* Re: [PATCH] bpf: generate const static pointers for kernel helpers
2024-01-27 18:50 [PATCH] bpf: generate const static pointers for kernel helpers Jose E. Marchesi
@ 2024-01-27 20:29 ` Yonghong Song
2024-01-28 11:57 ` Jose E. Marchesi
2024-01-30 2:00 ` patchwork-bot+netdevbpf
1 sibling, 1 reply; 4+ messages in thread
From: Yonghong Song @ 2024-01-27 20:29 UTC (permalink / raw)
To: Jose E. Marchesi, bpf
Cc: alexei.starovoitov, eddyz87, cupertino.miranda, david.faust
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']):
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH] bpf: generate const static pointers for kernel helpers
2024-01-27 20:29 ` Yonghong Song
@ 2024-01-28 11:57 ` Jose E. Marchesi
0 siblings, 0 replies; 4+ messages in thread
From: Jose E. Marchesi @ 2024-01-28 11:57 UTC (permalink / raw)
To: Yonghong Song
Cc: bpf, alexei.starovoitov, eddyz87, cupertino.miranda, david.faust
> 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']):
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] bpf: generate const static pointers for kernel helpers
2024-01-27 18:50 [PATCH] bpf: generate const static pointers for kernel helpers Jose E. Marchesi
2024-01-27 20:29 ` Yonghong Song
@ 2024-01-30 2:00 ` patchwork-bot+netdevbpf
1 sibling, 0 replies; 4+ messages in thread
From: patchwork-bot+netdevbpf @ 2024-01-30 2:00 UTC (permalink / raw)
To: Jose E. Marchesi
Cc: bpf, alexei.starovoitov, yonghong.song, eddyz87,
cupertino.miranda, david.faust
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!
--
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-01-30 2:00 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-01-27 18:50 [PATCH] bpf: generate const static pointers for kernel helpers Jose E. Marchesi
2024-01-27 20:29 ` Yonghong Song
2024-01-28 11:57 ` Jose E. Marchesi
2024-01-30 2:00 ` 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;
as well as URLs for NNTP newsgroup(s).