Skip to contentSkip to conversation

bpf: use flexible array members, not zero-length

PATCH Superseded Stephen Kitt 4 participants 7 messages Sep 28, 2019 – Sep 30, 2019

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

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
index 4d565d202ce3..24ce50fc1fc1 100644
@@ -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
index e0276520171b..c02ea0e1a588 100644
@@ -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
index 2e83a34f8c79..26eaa3f594aa 100644
@@ -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
#1 lore raw
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[];
...
};

---
 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
index 4d565d202ce3..24ce50fc1fc1 100644
@@ -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
index e0276520171b..c02ea0e1a588 100644
@@ -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
index 2e83a34f8c79..26eaa3f594aa 100644

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 */
Acked-by
#2 lore raw
Quoted 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[];
...
};

---

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
index 4d565d202ce3..24ce50fc1fc1 100644
@@ -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
index e0276520171b..c02ea0e1a588 100644
@@ -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 -0
 struct perf_sample_lost {
tools/lib/bpf/libbpf_internal.h +1 -1
index 2e83a34f8c79..26eaa3f594aa 100644
@@ -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:

#3 lore raw

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
index 4d565d202ce3..24ce50fc1fc1 100644
@@ -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
index e0276520171b..c02ea0e1a588 100644
@@ -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
index 2e83a34f8c79..930ada2276bf 100644
@@ -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
#4 lore raw
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

---

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
index 4d565d202ce3..24ce50fc1fc1 100644
@@ -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
index e0276520171b..c02ea0e1a588 100644
@@ -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
index 2e83a34f8c79..930ada2276bf 100644
@@ -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 */
#5 lore raw
In reply to Song Liu

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:

Thanks!

Regards,

Stephen

maintainer
#6 lore raw
In reply to Stephen Kitt
Quoted 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

Thanks,
Daniel