bpf: use flexible array members, not zero-length
Subsystems (5)
PATCH 1/1 bpf: use flexible array members, not zero-length
This switches zero-length arrays in variable-length structs to C99 flexible array members. GCC will then ensure that the arrays are always the last element in the struct.
Coccinelle:
@@
identifier S, fld;
type T;
@@
struct S {
...
- T fld[0];
+ T fld[];
...
};
--- Documentation/bpf/btf.rst | 2 +- tools/lib/bpf/libbpf.c | 2 +- tools/lib/bpf/libbpf_internal.h | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-)
Documentation/bpf/btf.rst +1 -1
@@ -670,7 +670,7 @@ func_info for each specific ELF section.:: __u32 sec_name_off; /* offset to section name */ __u32 num_info; /* Followed by num_info * record_size number of bytes */ - __u8 data[0]; + __u8 data[]; }; Here, num_info must be greater than 0.
tools/lib/bpf/libbpf.c +1 -1
@@ -5577,7 +5577,7 @@ static struct perf_buffer *__perf_buffer__new(int map_fd, size_t page_cnt,struct perf_sample_raw {struct perf_event_header header;uint32_t size; -char data[0 ]; +char data[]; };struct perf_sample_lost {
tools/lib/bpf/libbpf_internal.h +1 -1
@@ -86,7 +86,7 @@ struct btf_ext_info_sec {__u32 sec_name_off;__u32 num_info;/* Followed by num_info * record_size number of bytes */ -__u8 data[0 ]; +__u8 data[]; };/* The minimum bpf_func_info checked by the loader */
Signature · 1 line
2.20.1
Quoted 55 lines · Stephen Kitt
This switches zero-length arrays in variable-length structs to C99 flexible array members. GCC will then ensure that the arrays are always the last element in the struct.
Coccinelle:
@@
identifier S, fld;
type T;
@@struct S {
...
- T fld[0];
+ T fld[];
...
};Signed-off-by Stephen Kitt <[email protected]>--- Documentation/bpf/btf.rst | 2 +- tools/lib/bpf/libbpf.c | 2 +- tools/lib/bpf/libbpf_internal.h | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-)Documentation/bpf/btf.rst +1
-1@@ -670,7 +670,7 @@ func_info for each specific ELF section.:: __u32 sec_name_off; /* offset to section name */ __u32 num_info; /* Followed by num_info * record_size number of bytes */ - __u8 data[0]; + __u8 data[]; }; Here, num_info must be greater than 0.tools/lib/bpf/libbpf.c +1
-1@@ -5577,7 +5577,7 @@ static struct perf_buffer *__perf_buffer__new(int map_fd, size_t page_cnt,struct perf_sample_raw {struct perf_event_header header;uint32_t size; -char data[0 ]; +char data[]; };struct perf_sample_lost {tools/lib/bpf/libbpf_internal.h +0
-0
tools/lib/bpf/libbpf_internal.h struct btf_ext_info_sec { · line 89
Quoted 5 lines
tools/lib/bpf/libbpf_internal.h +1
-1@@ -86,7 +86,7 @@ struct btf_ext_info_sec {__u32 sec_name_off;__u32 num_info;/* Followed by num_info * record_size number of bytes */ -__u8 data[0 ]; +__u8 data[];
I think you should preserve the tab here.
--
Gustavo
Quoted 4 lines
tools/lib/bpf/libbpf_internal.h +0
-0};/* The minimum bpf_func_info checked by the loader */
Song Liu
Acked-byQuoted 56 lines · Gustavo A. R. Silva
Quoted 49 lines · Stephen Kitt
This switches zero-length arrays in variable-length structs to C99 flexible array members. GCC will then ensure that the arrays are always the last element in the struct.
Coccinelle:
@@
identifier S, fld;
type T;
@@struct S {
...
- T fld[0];
+ T fld[];
...
};Signed-off-by Stephen Kitt <[email protected]>---
Documentation/bpf/btf.rst | 2 +- tools/lib/bpf/libbpf.c | 2 +- tools/lib/bpf/libbpf_internal.h | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-)Documentation/bpf/btf.rst +1
-1@@ -670,7 +670,7 @@ func_info for each specific ELF section.:: __u32 sec_name_off; /* offset to section name */ __u32 num_info; /* Followed by num_info * record_size number of bytes */ - __u8 data[0]; + __u8 data[]; };Here,
num_infomust be greater than 0.tools/lib/bpf/libbpf.c +1
-1@@ -5577,7 +5577,7 @@ static struct perf_buffer *__perf_buffer__new(int map_fd, size_t page_cnt,struct perf_sample_raw {struct perf_event_header header;uint32_t size; -char data[0 ]; +char data[]; };
Quoted 13 lines
tools/lib/bpf/libbpf_internal.h struct btf_ext_info_sec { · line 89
Quoted 11 lines
tools/lib/bpf/libbpf.c +0
-0struct perf_sample_lost {tools/lib/bpf/libbpf_internal.h +1
-1@@ -86,7 +86,7 @@ struct btf_ext_info_sec {__u32 sec_name_off;__u32 num_info;/* Followed by num_info * record_size number of bytes */ -__u8 data[0 ]; +__u8 data[];I think you should preserve the tab here.
Agreed.
Besides this:
PATCH v2 1/1 bpf: use flexible array members, not zero-length
This switches zero-length arrays in variable-length structs to C99 flexible array members. GCC will then ensure that the arrays are always the last element in the struct.
Coccinelle:
@@
identifier S, fld;
type T;
@@
struct S {
...
T fld[
- 0
];
...
};
--- Documentation/bpf/btf.rst | 2 +- tools/lib/bpf/libbpf.c | 2 +- tools/lib/bpf/libbpf_internal.h | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-)
Documentation/bpf/btf.rst +1 -1
@@ -670,7 +670,7 @@ func_info for each specific ELF section.:: __u32 sec_name_off; /* offset to section name */ __u32 num_info; /* Followed by num_info * record_size number of bytes */ - __u8 data[0]; + __u8 data[]; }; Here, num_info must be greater than 0.
tools/lib/bpf/libbpf.c +1 -1
@@ -5577,7 +5577,7 @@ static struct perf_buffer *__perf_buffer__new(int map_fd, size_t page_cnt,struct perf_sample_raw {struct perf_event_header header;uint32_t size; -char data[0 ]; +char data[]; };struct perf_sample_lost {
tools/lib/bpf/libbpf_internal.h +1 -1
@@ -86,7 +86,7 @@ struct btf_ext_info_sec {__u32 sec_name_off;__u32 num_info;/* Followed by num_info * record_size number of bytes */ -__u8 data[0 ]; +__u8 data[]; };/* The minimum bpf_func_info checked by the loader */
Signature · 1 line
2.20.1
Quoted 18 lines · Stephen Kitt
This switches zero-length arrays in variable-length structs to C99 flexible array members. GCC will then ensure that the arrays are always the last element in the struct.
Coccinelle:
@@
identifier S, fld;
type T;
@@struct S {
...
T fld[
- 0
];
...
};
Quoted 2 lines
Signed-off-by Stephen Kitt <[email protected]>---
What changed in v2?
You should include a changelog here.
I encourage you to read the following document:
https://kernelnewbies.org/Outreachyfirstpatch
In particular this section "Versioning one patch revision"
Also, always CC the lkml: [email protected]
Thanks
--
Gustavo
Quoted 45 lines
Documentation/bpf/btf.rst | 2 +- tools/lib/bpf/libbpf.c | 2 +- tools/lib/bpf/libbpf_internal.h | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-)Documentation/bpf/btf.rst +1
-1@@ -670,7 +670,7 @@ func_info for each specific ELF section.:: __u32 sec_name_off; /* offset to section name */ __u32 num_info; /* Followed by num_info * record_size number of bytes */ - __u8 data[0]; + __u8 data[]; }; Here, num_info must be greater than 0.tools/lib/bpf/libbpf.c +1
-1@@ -5577,7 +5577,7 @@ static struct perf_buffer *__perf_buffer__new(int map_fd, size_t page_cnt,struct perf_sample_raw {struct perf_event_header header;uint32_t size; -char data[0 ]; +char data[]; };struct perf_sample_lost {tools/lib/bpf/libbpf_internal.h +1
-1@@ -86,7 +86,7 @@ struct btf_ext_info_sec {__u32 sec_name_off;__u32 num_info;/* Followed by num_info * record_size number of bytes */ -__u8 data[0 ]; +__u8 data[]; };/* The minimum bpf_func_info checked by the loader */
Le 30/09/2019 08:07, Song Liu a écrit :
Quoted 4 lines
Quoted 2 lines · Gustavo A. R. Silva
I think you should preserve the tab here.
Agreed.
Indeed (and I thought I’d checked whitespace changes!). V2 upcoming with an improved Coccinelle script which preserves whitespace and fixes a couple of other issues which appear in other files (attributes, and structs defined alongside the field declaration).
Quoted 3 lines
Besides this:
Acked-by Song Liu <[email protected]>
Thanks!
Regards,
Stephen
Daniel Borkmann
maintainerQuoted 10 lines · Stephen Kitt
This switches zero-length arrays in variable-length structs to C99 flexible array members. GCC will then ensure that the arrays are always the last element in the struct.
Coccinelle:
@@
identifier S, fld;
type T;
@@
Quoted 6 lines
struct S {
...
- T fld[0];
+ T fld[];
...
};
You did not explain the "why is it needed" part, only what your change is doing. What [compilation?] issue are you seeing that you're trying to fix? This sort of information must be present in a changelog.
Quoted 1 line
Signed-off-by Stephen Kitt <[email protected]>
Thanks,
Daniel