diff options
author | Jose E. Marchesi <jose.marchesi@oracle.com> | 2024-01-27 19:50:31 +0100 |
---|---|---|
committer | Andrii Nakryiko <andrii@kernel.org> | 2024-01-29 16:46:12 -0800 |
commit | ff2071a7b7fd77908417603c4a785822939b3841 (patch) | |
tree | e98a8449e31b80a0d7c0288571ca0a0aa426a09f /scripts/bpf_doc.py | |
parent | f2e4040c82d3fddd11fa7c64e8f810e6f9cb7460 (diff) |
bpf: Generate const static pointers for kernel helpers
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>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Acked-by: Yonghong Song <yonghong.song@linux.dev>
Link: https://lore.kernel.org/bpf/20240127185031.29854-1-jose.marchesi@oracle.com
Diffstat (limited to 'scripts/bpf_doc.py')
-rwxr-xr-x | scripts/bpf_doc.py | 2 |
1 files changed, 1 insertions, 1 deletions
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']): |