Skip to contentSkip to conversation

samples/bpf: fix LLVM compilation warning

PATCH New Daniel T. Lee 1 participant 4 messages Dec 18, 2022

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

v4 cover samples/bpf: fix LLVM compilation warning

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(-)

---
Changes in V2:
- Change the cover letter subject
Changes in V3:
- Fix style problem with the patch
Changes in V4:
- Change the cover letter subject

Signature · 1 line
2.34.1
#1 lore raw

PATCH v4 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
#2 lore raw

PATCH v4 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
#3 lore raw

PATCH v4 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