Skip to contentSkip to conversation

perf build: Build error in libbpf missing initialization

PATCH In review Thomas Richter 3 participants 5 messages Jul 27, 2018 – Jul 28, 2018

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

PATCH 1/1 perf build: Build error in libbpf missing initialization

In linux-next tree compiling the perf tool with additional make flags "EXTRA_CFLAGS="-Wp,-D_FORTIFY_SOURCE=2 -O2"
causes a compiler error. It is the warning
'variable may be used uninitialized'
which is treated as error:

I compile it using a FEDORA 28 installation, my gcc compiler version:
gcc (GCC) 8.0.1 20180324 (Red Hat 8.0.1-0.20)

The file that causes the error is tools/lib/bpf/libbpf.c

Here is the error message:

[root@p23lp27] # make V=1 EXTRA_CFLAGS="-Wp,-D_FORTIFY_SOURCE=2 -O2" [...]
Makefile.config:849: No openjdk development package found, please
install JDK package, e.g. openjdk-8-jdk, java-1.8.0-openjdk-devel
Warning: Kernel ABI header at 'tools/include/uapi/linux/if_link.h'
differs from latest version at 'include/uapi/linux/if_link.h'
CC libbpf.o
libbpf.c: In function ‘bpf_perf_event_read_simple’:
libbpf.c:2342:6: error: ‘ret’ may be used uninitialized in this
function [-Werror=maybe-uninitialized]

  int ret;
      ^

cc1: all warnings being treated as errors
mv: cannot stat './.libbpf.o.tmp': No such file or directory /home6/tmricht/linux-next/tools/build/Makefile.build:96: recipe for target 'libbpf.o' failed

Fix this warning and add an addition check at the beginning of the while loop.

Cc Alexei Starovoitov <[email protected]>
Cc Daniel Borkmann <[email protected]>
---
 tools/lib/bpf/libbpf.c | 2 ++
 1 file changed, 2 insertions(+)
tools/lib/bpf/libbpf.c +2 -0
index 73465caa33ba..66965ca96113 100644
@@ -2349,6 +2349,8 @@ bpf_perf_event_read_simple(void *mem, unsigned long size,
 
 	begin = base + data_tail % size;
 	end = base + data_head % size;
+	if (begin == end)
+		return LIBBPF_PERF_EVENT_ERROR;
 
 	while (begin != end) {
 		struct perf_event_header *ehdr;
Signature · 1 line
2.16.4
#1 lore raw
Quoted 29 lines · Thomas Richter

In linux-next tree compiling the perf tool with additional make flags "EXTRA_CFLAGS="-Wp,-D_FORTIFY_SOURCE=2 -O2"
causes a compiler error. It is the warning
'variable may be used uninitialized'
which is treated as error:

I compile it using a FEDORA 28 installation, my gcc compiler version:
gcc (GCC) 8.0.1 20180324 (Red Hat 8.0.1-0.20)

The file that causes the error is tools/lib/bpf/libbpf.c

Here is the error message:

[root@p23lp27] # make V=1 EXTRA_CFLAGS="-Wp,-D_FORTIFY_SOURCE=2 -O2" [...]
Makefile.config:849: No openjdk development package found, please
install JDK package, e.g. openjdk-8-jdk, java-1.8.0-openjdk-devel
Warning: Kernel ABI header at 'tools/include/uapi/linux/if_link.h'
differs from latest version at 'include/uapi/linux/if_link.h'
CC libbpf.o
libbpf.c: In function ‘bpf_perf_event_read_simple’:
libbpf.c:2342:6: error: ‘ret’ may be used uninitialized in this
function [-Werror=maybe-uninitialized]

  int ret;
      ^

cc1: all warnings being treated as errors
mv: cannot stat './.libbpf.o.tmp': No such file or directory /home6/tmricht/linux-next/tools/build/Makefile.build:96: recipe for target 'libbpf.o' failed

Quoted 8 lines

Fix this warning and add an addition check at the beginning of the while loop.

Cc Alexei Starovoitov <[email protected]>
Cc Daniel Borkmann <[email protected]>

Ah, you already sent this, LGTM, thanks Thomas!

Quoted 16 lines
 tools/lib/bpf/libbpf.c | 2 ++
 1 file changed, 2 insertions(+)
tools/lib/bpf/libbpf.c +2 -0
index 73465caa33ba..66965ca96113 100644
@@ -2349,6 +2349,8 @@ bpf_perf_event_read_simple(void *mem, unsigned long size,
 
 	begin = base + data_tail % size;
 	end = base + data_head % size;
+	if (begin == end)
+		return LIBBPF_PERF_EVENT_ERROR;
 
 	while (begin != end) {
 		struct perf_event_header *ehdr;
maintainer
#2 lore raw
Quoted 50 lines · Jakub Kicinski
Quoted 29 lines · Thomas Richter

In linux-next tree compiling the perf tool with additional make flags "EXTRA_CFLAGS="-Wp,-D_FORTIFY_SOURCE=2 -O2"
causes a compiler error. It is the warning
'variable may be used uninitialized'
which is treated as error:

I compile it using a FEDORA 28 installation, my gcc compiler version:
gcc (GCC) 8.0.1 20180324 (Red Hat 8.0.1-0.20)

The file that causes the error is tools/lib/bpf/libbpf.c

Here is the error message:

[root@p23lp27] # make V=1 EXTRA_CFLAGS="-Wp,-D_FORTIFY_SOURCE=2 -O2" [...]
Makefile.config:849: No openjdk development package found, please
install JDK package, e.g. openjdk-8-jdk, java-1.8.0-openjdk-devel
Warning: Kernel ABI header at 'tools/include/uapi/linux/if_link.h'
differs from latest version at 'include/uapi/linux/if_link.h'
CC libbpf.o
libbpf.c: In function ‘bpf_perf_event_read_simple’:
libbpf.c:2342:6: error: ‘ret’ may be used uninitialized in this
function [-Werror=maybe-uninitialized]

  int ret;
      ^

cc1: all warnings being treated as errors
mv: cannot stat './.libbpf.o.tmp': No such file or directory /home6/tmricht/linux-next/tools/build/Makefile.build:96: recipe for target 'libbpf.o' failed

Quoted 8 lines

Fix this warning and add an addition check at the beginning of the while loop.

Cc Alexei Starovoitov <[email protected]>
Cc Daniel Borkmann <[email protected]>

Ah, you already sent this, LGTM, thanks Thomas!

Quoted 9 lines
 tools/lib/bpf/libbpf.c | 2 ++
 1 file changed, 2 insertions(+)
tools/lib/bpf/libbpf.c +0 -0
index 73465caa33ba..66965ca96113 100644
@@ -2349,6 +2349,8 @@ bpf_perf_event_read_simple(void *mem, unsigned long size,
 
Quoted 7 lines

tools/lib/bpf/libbpf.c bpf_perf_event_read_simple(void *mem, unsigned long size, · line 2356

Quoted 7 lines
tools/lib/bpf/libbpf.c +2 -0
 	begin = base + data_tail % size;
 	end = base + data_head % size;
+	if (begin == end)
+		return LIBBPF_PERF_EVENT_ERROR;
 
 	while (begin != end) {
 		struct perf_event_header *ehdr;

One question though, any objections to go for something like the below instead? I doubt we ever hit this in a 'normal' situation, and given we already test for the begin and end anyway, we could just avoid the extra test altogether. I could change it to the below if you're good as well (no need to resend anything):

tools/lib/bpf/libbpf.c +1 -1
index d881d37..1aafdbe 100644
@@ -2273,8 +2273,8 @@ bpf_perf_event_read_simple(void *mem, unsigned long size,
        volatile struct perf_event_mmap_page *header = mem;
        __u64 data_tail = header->data_tail;
        __u64 data_head = header->data_head;
+       int ret = LIBBPF_PERF_EVENT_ERROR;
        void *base, *begin, *end;
-       int ret;
 
        asm volatile("" ::: "memory"); /* in real code it should be smp_rmb() */
        if (data_head == data_tail)

Thanks,
Daniel

#3 lore raw
Quoted 69 lines · Daniel Borkmann
Quoted 50 lines · Jakub Kicinski
Quoted 29 lines · Thomas Richter

In linux-next tree compiling the perf tool with additional make flags "EXTRA_CFLAGS="-Wp,-D_FORTIFY_SOURCE=2 -O2"
causes a compiler error. It is the warning
'variable may be used uninitialized'
which is treated as error:

I compile it using a FEDORA 28 installation, my gcc compiler version:
gcc (GCC) 8.0.1 20180324 (Red Hat 8.0.1-0.20)

The file that causes the error is tools/lib/bpf/libbpf.c

Here is the error message:

[root@p23lp27] # make V=1 EXTRA_CFLAGS="-Wp,-D_FORTIFY_SOURCE=2 -O2" [...]
Makefile.config:849: No openjdk development package found, please
install JDK package, e.g. openjdk-8-jdk, java-1.8.0-openjdk-devel
Warning: Kernel ABI header at 'tools/include/uapi/linux/if_link.h'
differs from latest version at 'include/uapi/linux/if_link.h'
CC libbpf.o
libbpf.c: In function ‘bpf_perf_event_read_simple’:
libbpf.c:2342:6: error: ‘ret’ may be used uninitialized in this
function [-Werror=maybe-uninitialized]

  int ret;
      ^

cc1: all warnings being treated as errors
mv: cannot stat './.libbpf.o.tmp': No such file or directory /home6/tmricht/linux-next/tools/build/Makefile.build:96: recipe for target 'libbpf.o' failed

Quoted 8 lines

Fix this warning and add an addition check at the beginning of the while loop.

Cc Alexei Starovoitov <[email protected]>
Cc Daniel Borkmann <[email protected]>

Ah, you already sent this, LGTM, thanks Thomas!

Quoted 9 lines
 tools/lib/bpf/libbpf.c | 2 ++
 1 file changed, 2 insertions(+)
tools/lib/bpf/libbpf.c +0 -0
index 73465caa33ba..66965ca96113 100644
@@ -2349,6 +2349,8 @@ bpf_perf_event_read_simple(void *mem, unsigned long size,
 
Quoted 7 lines

tools/lib/bpf/libbpf.c bpf_perf_event_read_simple(void *mem, unsigned long size, · line 2356

Quoted 7 lines
tools/lib/bpf/libbpf.c +2 -0
 	begin = base + data_tail % size;
 	end = base + data_head % size;
+	if (begin == end)
+		return LIBBPF_PERF_EVENT_ERROR;
 
 	while (begin != end) {
 		struct perf_event_header *ehdr;  

One question though, any objections to go for something like the below instead? I doubt we ever hit this in a 'normal' situation, and given we already test for the begin and end anyway, we could just avoid the extra test altogether. I could change it to the below if you're good as well (no need to resend anything):

tools/lib/bpf/libbpf.c +0 -0
index d881d37..1aafdbe 100644

tools/lib/bpf/libbpf.c bpf_perf_event_read_simple(void *mem, unsigned long size, · line 2280

Quoted 9 lines
tools/lib/bpf/libbpf.c +1 -1
@@ -2273,8 +2273,8 @@ bpf_perf_event_read_simple(void *mem, unsigned long size,
        volatile struct perf_event_mmap_page *header = mem;
        __u64 data_tail = header->data_tail;
        __u64 data_head = header->data_head;
+       int ret = LIBBPF_PERF_EVENT_ERROR;
        void *base, *begin, *end;
-       int ret;
 
        asm volatile("" ::: "memory"); /* in real code it should be smp_rmb() */
        if (data_head == data_tail)

No real objection, although as a matter of personal taste I'm not a big fan of initializing err/ret variables unless the code is explicitly structured to make use of it. Here it looks slightly more like silencing a compiler warning, hence my preference to address the actual cause of the warning rather than catch all. I guess one could argue the other way, i.e. if the loop never run (and therefore ret was not overwritten) there must be *some* error. I like verbose/explicit code I guess..

Up to you :)

maintainer
#4 lore raw
Quoted 89 lines · Jakub Kicinski
Quoted 69 lines · Daniel Borkmann
Quoted 50 lines · Jakub Kicinski
Quoted 29 lines · Thomas Richter

In linux-next tree compiling the perf tool with additional make flags "EXTRA_CFLAGS="-Wp,-D_FORTIFY_SOURCE=2 -O2"
causes a compiler error. It is the warning
'variable may be used uninitialized'
which is treated as error:

I compile it using a FEDORA 28 installation, my gcc compiler version:
gcc (GCC) 8.0.1 20180324 (Red Hat 8.0.1-0.20)

The file that causes the error is tools/lib/bpf/libbpf.c

Here is the error message:

[root@p23lp27] # make V=1 EXTRA_CFLAGS="-Wp,-D_FORTIFY_SOURCE=2 -O2" [...]
Makefile.config:849: No openjdk development package found, please
install JDK package, e.g. openjdk-8-jdk, java-1.8.0-openjdk-devel
Warning: Kernel ABI header at 'tools/include/uapi/linux/if_link.h'
differs from latest version at 'include/uapi/linux/if_link.h'
CC libbpf.o
libbpf.c: In function ‘bpf_perf_event_read_simple’:
libbpf.c:2342:6: error: ‘ret’ may be used uninitialized in this
function [-Werror=maybe-uninitialized]

  int ret;
      ^

cc1: all warnings being treated as errors
mv: cannot stat './.libbpf.o.tmp': No such file or directory /home6/tmricht/linux-next/tools/build/Makefile.build:96: recipe for target 'libbpf.o' failed

Quoted 8 lines

Fix this warning and add an addition check at the beginning of the while loop.

Cc Alexei Starovoitov <[email protected]>
Cc Daniel Borkmann <[email protected]>

Ah, you already sent this, LGTM, thanks Thomas!

Quoted 9 lines
 tools/lib/bpf/libbpf.c | 2 ++
 1 file changed, 2 insertions(+)
tools/lib/bpf/libbpf.c +0 -0
index 73465caa33ba..66965ca96113 100644
@@ -2349,6 +2349,8 @@ bpf_perf_event_read_simple(void *mem, unsigned long size,
 
Quoted 7 lines

tools/lib/bpf/libbpf.c bpf_perf_event_read_simple(void *mem, unsigned long size, · line 2356

Quoted 7 lines
tools/lib/bpf/libbpf.c +2 -0
 	begin = base + data_tail % size;
 	end = base + data_head % size;
+	if (begin == end)
+		return LIBBPF_PERF_EVENT_ERROR;
 
 	while (begin != end) {
 		struct perf_event_header *ehdr;  

One question though, any objections to go for something like the below instead? I doubt we ever hit this in a 'normal' situation, and given we already test for the begin and end anyway, we could just avoid the extra test altogether. I could change it to the below if you're good as well (no need to resend anything):

tools/lib/bpf/libbpf.c +0 -0
index d881d37..1aafdbe 100644

tools/lib/bpf/libbpf.c bpf_perf_event_read_simple(void *mem, unsigned long size, · line 2280

Quoted 9 lines
tools/lib/bpf/libbpf.c +1 -1
@@ -2273,8 +2273,8 @@ bpf_perf_event_read_simple(void *mem, unsigned long size,
        volatile struct perf_event_mmap_page *header = mem;
        __u64 data_tail = header->data_tail;
        __u64 data_head = header->data_head;
+       int ret = LIBBPF_PERF_EVENT_ERROR;
        void *base, *begin, *end;
-       int ret;
 
        asm volatile("" ::: "memory"); /* in real code it should be smp_rmb() */
        if (data_head == data_tail)

No real objection, although as a matter of personal taste I'm not a big fan of initializing err/ret variables unless the code is explicitly structured to make use of it. Here it looks slightly more like silencing a compiler warning, hence my preference to address the actual cause of the warning rather than catch all. I guess one could argue the other way, i.e. if the loop never run (and therefore ret was not overwritten) there must be *some* error. I like verbose/explicit code I guess..

Quoted 1 line

Up to you :)

Ok, I pushed this variant out to the bpf tree since it also is affected there. Thanks a lot everyone!