Skip to contentSkip to conversation

samples/bpf: fix LLVM compilation warning with

PATCH Superseded Daniel T. Lee 2 participants 9 messages Dec 17, 2022 – Dec 18, 2022

Subsystems (3)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover samples/bpf: fix LLVM compilation warning with

Currently, compiling samples/bpf with LLVM emits several warning. They are only small details, but they do not appear when compiled with GCC. Detailed compilation command and warning logs can be found from bpf CI.

Daniel T. Lee (3):
samples/bpf: remove unused function with test_lru_dist samples/bpf: replace meaningless counter with tracex4 samples/bpf: fix uninitialized warning with
test_current_task_under_cgroup

 samples/bpf/test_current_task_under_cgroup_user.c | 6 ++++--
 samples/bpf/test_lru_dist.c                       | 5 -----
 samples/bpf/tracex4_user.c                        | 4 ++--
 3 files changed, 6 insertions(+), 9 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/3 samples/bpf: remove unused function with test_lru_dist

Currently, compiling samples/bpf with LLVM warns about the unused function with test_lru_dist.

./samples/bpf/test_lru_dist.c:45:19:
warning: unused function 'list_empty' [-Wunused-function] static inline int list_empty(const struct list_head *head)
^
1 warning generated.

This commit resolve this compiler warning by removing the abandoned function.

---
 samples/bpf/test_lru_dist.c | 5 -----
 1 file changed, 5 deletions(-)
samples/bpf/test_lru_dist.c +0 -5
index 5efb91763d65..1c161276d57b 100644
@@ -42,11 +42,6 @@ static inline void INIT_LIST_HEAD(struct list_head *list)
 	list->prev = list;
 }
 
-static inline int list_empty(const struct list_head *head)
-{
-	return head->next == head;
-}
-
 static inline void __list_add(struct list_head *new,
 			      struct list_head *prev,
 			      struct list_head *next)
Signature · 1 line
2.34.1
#3 lore raw

PATCH 2/3 samples/bpf: replace meaningless counter with tracex4

In reply to Daniel T. Lee

Currently, compiling samples/bpf with LLVM warns about the unused but set variable with tracex4_user.

    ./samples/bpf/tracex4_user.c:54:14:
    warning: variable 'i' set but not used [-Wunused-but-set-variable]
        int map_fd, i, j = 0;
                    ^
                    1 warning generated.

This commit resolve this compiler warning by replacing the meaningless counter.

---
 samples/bpf/tracex4_user.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
samples/bpf/tracex4_user.c +2 -2
index 227b05a0bc88..dee8f0a091ba 100644
@@ -51,7 +51,7 @@ int main(int ac, char **argv)
 	struct bpf_program *prog;
 	struct bpf_object *obj;
 	char filename[256];
-	int map_fd, i, j = 0;
+	int map_fd, j = 0;
 
 	snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
 	obj = bpf_object__open_file(filename, NULL);
@@ -82,7 +82,7 @@ int main(int ac, char **argv)
 		j++;
 	}
 
-	for (i = 0; ; i++) {
+	while (1) {
 		print_old_objects(map_fd);
 		sleep(1);
 	}
Signature · 1 line
2.34.1
#5 lore raw

PATCH 3/3 samples/bpf: fix uninitialized warning with test_current_task_under_cgroup

In reply to Daniel T. Lee

Currently, compiling samples/bpf with LLVM warns about the uninitialized use of variable with test_current_task_under_cgroup.

    ./samples/bpf/test_current_task_under_cgroup_user.c:57:6:
    warning: variable 'cg2' is used uninitialized whenever 'if' condition is true [-Wsometimes-uninitialized]
	    if (setup_cgroup_environment())
		^~~~~~~~~~~~~~~~~~~~~~~~~~
    ./samples/bpf/test_current_task_under_cgroup_user.c:106:8:
    note: uninitialized use occurs here
	    close(cg2);
		  ^~~
    ./samples/bpf/test_current_task_under_cgroup_user.c:57:2:
    note: remove the 'if' if its condition is always false
	    if (setup_cgroup_environment())
	    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    ./samples/bpf/test_current_task_under_cgroup_user.c:19:9:
    note: initialize the variable 'cg2' to silence this warning
	    int cg2, idx = 0, rc = 1;
		   ^
		    = 0
    1 warning generated.

This commit resolve this compiler warning by pre-initialize the variable with error for safeguard.

---
 samples/bpf/test_current_task_under_cgroup_user.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
samples/bpf/test_current_task_under_cgroup_user.c +4 -2
index ac251a417f45..6fb25906835e 100644
@@ -14,9 +14,9 @@
 int main(int argc, char **argv)
 {
 	pid_t remote_pid, local_pid = getpid();
+	int cg2 = -1, idx = 0, rc = 1;
 	struct bpf_link *link = NULL;
 	struct bpf_program *prog;
-	int cg2, idx = 0, rc = 1;
 	struct bpf_object *obj;
 	char filename[256];
 	int map_fd[2];
@@ -103,7 +103,9 @@ int main(int argc, char **argv)
 	rc = 0;
 
 err:
-	close(cg2);
+	if (cg2 != -1)
+		close(cg2);
+
 	cleanup_cgroup_environment();
 
 cleanup:
Signature · 1 line
2.34.1
#7 lore raw
In reply to Daniel T. Lee
Quoted 3 lines · Daniel T. Lee

Currently, compiling samples/bpf with LLVM emits several warning. They are only small details, but they do not appear when compiled with GCC. Detailed compilation command and warning logs can be found from bpf CI.

Could you change the subject line to
samples/bpf: fix LLVM compilation warning

Quoted 12 lines

Daniel T. Lee (3):
samples/bpf: remove unused function with test_lru_dist samples/bpf: replace meaningless counter with tracex4 samples/bpf: fix uninitialized warning with
test_current_task_under_cgroup

  samples/bpf/test_current_task_under_cgroup_user.c | 6 ++++--
  samples/bpf/test_lru_dist.c                       | 5 -----
  samples/bpf/tracex4_user.c                        | 4 ++--
  3 files changed, 6 insertions(+), 9 deletions(-)
Acked-by on v1 1/3
#2 lore raw

Re: [bpf-next 1/3] samples/bpf: remove unused function with test_lru_dist

In reply to Daniel T. Lee
Quoted 9 lines · Daniel T. Lee

Currently, compiling samples/bpf with LLVM warns about the unused function with test_lru_dist.

./samples/bpf/test_lru_dist.c:45:19:
warning: unused function 'list_empty' [-Wunused-function] static inline int list_empty(const struct list_head *head)
^
1 warning generated.

Quoted 4 lines

This commit resolve this compiler warning by removing the abandoned function.

Acked-by on v1 2/3
#4 lore raw

Re: [bpf-next 2/3] samples/bpf: replace meaningless counter with tracex4

In reply to Daniel T. Lee
Quoted 9 lines · Daniel T. Lee

Currently, compiling samples/bpf with LLVM warns about the unused but set variable with tracex4_user.

     ./samples/bpf/tracex4_user.c:54:14:
     warning: variable 'i' set but not used [-Wunused-but-set-variable]
         int map_fd, i, j = 0;
                     ^
                     1 warning generated.
Quoted 4 lines

This commit resolve this compiler warning by replacing the meaningless counter.

Acked-by on v1 3/3
#6 lore raw

Re: [bpf-next 3/3] samples/bpf: fix uninitialized warning with test_current_task_under_cgroup

In reply to Daniel T. Lee
Quoted 22 lines · Daniel T. Lee

Currently, compiling samples/bpf with LLVM warns about the uninitialized use of variable with test_current_task_under_cgroup.

     ./samples/bpf/test_current_task_under_cgroup_user.c:57:6:
     warning: variable 'cg2' is used uninitialized whenever 'if' condition is true [-Wsometimes-uninitialized]
	    if (setup_cgroup_environment())
		^~~~~~~~~~~~~~~~~~~~~~~~~~
     ./samples/bpf/test_current_task_under_cgroup_user.c:106:8:
     note: uninitialized use occurs here
	    close(cg2);
		  ^~~
     ./samples/bpf/test_current_task_under_cgroup_user.c:57:2:
     note: remove the 'if' if its condition is always false
	    if (setup_cgroup_environment())
	    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     ./samples/bpf/test_current_task_under_cgroup_user.c:19:9:
     note: initialize the variable 'cg2' to silence this warning
	    int cg2, idx = 0, rc = 1;
		   ^
		    = 0
     1 warning generated.
Quoted 4 lines

This commit resolve this compiler warning by pre-initialize the variable with error for safeguard.

#8 lore raw
In reply to Yonghong Song
Quoted 23 lines · Yonghong Song
Quoted 3 lines · Daniel T. Lee

Currently, compiling samples/bpf with LLVM emits several warning. They are only small details, but they do not appear when compiled with GCC. Detailed compilation command and warning logs can be found from bpf CI.

Could you change the subject line to
samples/bpf: fix LLVM compilation warning

Quoted 12 lines

Daniel T. Lee (3):
samples/bpf: remove unused function with test_lru_dist samples/bpf: replace meaningless counter with tracex4 samples/bpf: fix uninitialized warning with
test_current_task_under_cgroup

  samples/bpf/test_current_task_under_cgroup_user.c | 6 ++++--
  samples/bpf/test_lru_dist.c                       | 5 -----
  samples/bpf/tracex4_user.c                        | 4 ++--
  3 files changed, 6 insertions(+), 9 deletions(-)

Thanks for pointing this out.
I will send a v2 patch.