Skip to contentSkip to conversation

selftests/cgroups: fix clang build failures, warnings

PATCH Applied John Hubbard 4 participants 11 messages May 3, 2024

Subsystems (5)

v1 of 3 Applied Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover selftests/cgroups: fix clang build failures, warnings

Hi,

Just a bunch of fixes as part of my work to make selftests build cleanly with clang.

Enjoy!

thanks,
John Hubbard

John Hubbard (4):
selftests/cgroup: fix clang build failures for abs() calls selftests/cgroup: fix clang warnings: uninitialized fd variable selftests/cgroup: cpu_hogger init: use {} instead of {NULL} selftests/cgroup: fix uninitialized variables in test_zswap.c

 tools/testing/selftests/cgroup/cgroup_util.h     | 2 +-
 tools/testing/selftests/cgroup/test_cpu.c        | 4 ++--
 tools/testing/selftests/cgroup/test_kmem.c       | 4 ++--
 tools/testing/selftests/cgroup/test_memcontrol.c | 4 +++-
 tools/testing/selftests/cgroup/test_zswap.c      | 4 ++--
 5 files changed, 10 insertions(+), 8 deletions(-)

base-commit: f03359bca01bf4372cf2c118cd9a987a5951b1c8
prerequisite-patch-id: b901ece2a5b78503e2fb5480f20e304d36a0ea27

Signature · 1 line
2.45.0
#1 lore raw

PATCH 1/4 selftests/cgroup: fix clang build failures for abs() calls

First of all, in order to build with clang at all, one must first apply Valentin Obst's build fix for LLVM [1]. Once that is done, then when building with clang, via:

make LLVM=1 -C tools/testing/selftests

...clang is pickier than gcc, about which version of abs(3) to call, depending on the argument type:

   int abs(int j);
   long labs(long j);
   long long llabs(long long j);

...and this is causing both build failures and warnings, when running:

make LLVM=1 -C tools/testing/selftests

Fix this by calling labs() in value_close(), because the arguments are unambiguously "long" type.

[1] https://lore.kernel.org/all/20240329-selftests-libmk-llvm-rfc-v1-1-2f9ed7d1c49f@valentinobst.de/

---
 tools/testing/selftests/cgroup/cgroup_util.h | 2 +-
 tools/testing/selftests/cgroup/test_kmem.c   | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)
tools/testing/selftests/cgroup/cgroup_util.h +1 -1
index 1df7f202214a..239633e936df 100644
@@ -18,7 +18,7 @@
  */
 static inline int values_close(long a, long b, int err)
 {
-	return abs(a - b) <= (a + b) / 100 * err;
+	return labs(a - b) <= (a + b) / 100 * err;
 }
 
 extern int cg_find_unified_root(char *root, size_t len);
tools/testing/selftests/cgroup/test_kmem.c +2 -2
index c82f974b85c9..d21d3d280ca2 100644
@@ -192,7 +192,7 @@ static int test_kmem_memcg_deletion(const char *root)
 		goto cleanup;
 
 	sum = anon + file + kernel + sock;
-	if (abs(sum - current) < MAX_VMSTAT_ERROR) {
+	if (labs(sum - current) < MAX_VMSTAT_ERROR) {
 		ret = KSFT_PASS;
 	} else {
 		printf("memory.current = %ld\n", current);
@@ -380,7 +380,7 @@ static int test_percpu_basic(const char *root)
 	current = cg_read_long(parent, "memory.current");
 	percpu = cg_read_key_long(parent, "memory.stat", "percpu ");
 
-	if (current > 0 && percpu > 0 && abs(current - percpu) <
+	if (current > 0 && percpu > 0 && labs(current - percpu) <
 	    MAX_VMSTAT_ERROR)
 		ret = KSFT_PASS;
 	else
Signature · 1 line
2.45.0
maintainer Reviewed-by on v1 1/4
#2 lore raw

Re: [PATCH 1/4] selftests/cgroup: fix clang build failures for abs() calls

Quoted 15 lines · John Hubbard

First of all, in order to build with clang at all, one must first apply Valentin Obst's build fix for LLVM [1]. Once that is done, then when building with clang, via:

make LLVM=1 -C tools/testing/selftests

...clang is pickier than gcc, about which version of abs(3) to call, depending on the argument type:

   int abs(int j);
   long labs(long j);
   long long llabs(long long j);

...and this is causing both build failures and warnings, when running:

Quoted 8 lines

make LLVM=1 -C tools/testing/selftests

Fix this by calling labs() in value_close(), because the arguments are unambiguously "long" type.

[1] https://lore.kernel.org/all/20240329-selftests-libmk-llvm-rfc-v1-1-2f9ed7d1c49f@valentinobst.de/

Thanks!

#3 lore raw

PATCH 2/4 selftests/cgroup: fix clang warnings: uninitialized fd variable

In reply to John Hubbard

First of all, in order to build with clang at all, one must first apply Valentin Obst's build fix for LLVM [1]. Once that is done, then when building with clang, via:

make LLVM=1 -C tools/testing/selftests

...clang warns about fd being used uninitialized, in test_memcg_reclaim()'s error handling path.

Fix this by initializing fd to -1.

[1] https://lore.kernel.org/all/20240329-selftests-libmk-llvm-rfc-v1-1-2f9ed7d1c49f@valentinobst.de/

---
 tools/testing/selftests/cgroup/test_memcontrol.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
tools/testing/selftests/cgroup/test_memcontrol.c +3 -1
index c7c9572003a8..a97832b0c1cd 100644
@@ -716,7 +716,9 @@ static bool reclaim_until(const char *memcg, long goal)
  */
 static int test_memcg_reclaim(const char *root)
 {
-	int ret = KSFT_FAIL, fd, retries;
+	int ret = KSFT_FAIL;
+	int fd = -1;
+	int retries;
 	char *memcg;
 	long current, expected_usage;
 
Signature · 1 line
2.45.0
maintainer Reviewed-by on v1 2/4
#4 lore raw

Re: [PATCH 2/4] selftests/cgroup: fix clang warnings: uninitialized fd variable

Quoted 6 lines · John Hubbard

First of all, in order to build with clang at all, one must first apply Valentin Obst's build fix for LLVM [1]. Once that is done, then when building with clang, via:

make LLVM=1 -C tools/testing/selftests

Quoted 8 lines

...clang warns about fd being used uninitialized, in test_memcg_reclaim()'s error handling path.

Fix this by initializing fd to -1.

[1] https://lore.kernel.org/all/20240329-selftests-libmk-llvm-rfc-v1-1-2f9ed7d1c49f@valentinobst.de/

#5 lore raw

PATCH 3/4 selftests/cgroup: cpu_hogger init: use {} instead of {NULL}

In reply to John Hubbard

First of all, in order to build with clang at all, one must first apply Valentin Obst's build fix for LLVM [1]. Once that is done, then when building with clang, via:

make LLVM=1 -C tools/testing/selftests

...clang generates warning here, because struct cpu_hogger has multiple fields, and the code is initializing an array of these structs, and it is incorrect to specify a single NULL value as the initializer.

Fix this by initializing with {}, so that the compiler knows to use default initializer values for all fields in each array entry.

[1] https://lore.kernel.org/all/20240329-selftests-libmk-llvm-rfc-v1-1-2f9ed7d1c49f@valentinobst.de/

---
 tools/testing/selftests/cgroup/test_cpu.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/testing/selftests/cgroup/test_cpu.c +2 -2
index 24020a2c68dc..e4266b60e5ac 100644
@@ -237,7 +237,7 @@ run_cpucg_weight_test(
 {
 	int ret = KSFT_FAIL, i;
 	char *parent = NULL;
-	struct cpu_hogger children[3] = {NULL};
+	struct cpu_hogger children[3] = {};
 
 	parent = cg_name(root, "cpucg_test_0");
 	if (!parent)
@@ -408,7 +408,7 @@ run_cpucg_nested_weight_test(const char *root, bool overprovisioned)
 {
 	int ret = KSFT_FAIL, i;
 	char *parent = NULL, *child = NULL;
-	struct cpu_hogger leaf[3] = {NULL};
+	struct cpu_hogger leaf[3] = {};
 	long nested_leaf_usage, child_usage;
 	int nprocs = get_nprocs();
 
Signature · 1 line
2.45.0
maintainer Reviewed-by on v1 3/4
#6 lore raw

Re: [PATCH 3/4] selftests/cgroup: cpu_hogger init: use {} instead of {NULL}

Quoted 10 lines · John Hubbard

First of all, in order to build with clang at all, one must first apply Valentin Obst's build fix for LLVM [1]. Once that is done, then when building with clang, via:

make LLVM=1 -C tools/testing/selftests

...clang generates warning here, because struct cpu_hogger has multiple fields, and the code is initializing an array of these structs, and it is incorrect to specify a single NULL value as the initializer.

Quoted 6 lines

Fix this by initializing with {}, so that the compiler knows to use default initializer values for all fields in each array entry.

[1] https://lore.kernel.org/all/20240329-selftests-libmk-llvm-rfc-v1-1-2f9ed7d1c49f@valentinobst.de/

#7 lore raw

PATCH 4/4 selftests/cgroup: fix uninitialized variables in test_zswap.c

In reply to John Hubbard

First of all, in order to build with clang at all, one must first apply Valentin Obst's build fix for LLVM [1]. Once that is done, then when building with clang, via:

make LLVM=1 -C tools/testing/selftests

...clang finds and warning about some uninitialized variables. Fix these by initializing them.

[1] https://lore.kernel.org/all/20240329-selftests-libmk-llvm-rfc-v1-1-2f9ed7d1c49f@valentinobst.de/

---
 tools/testing/selftests/cgroup/test_zswap.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/testing/selftests/cgroup/test_zswap.c +2 -2
index f0e488ed90d8..6aac80eadc5d 100644
@@ -257,7 +257,7 @@ static int test_no_invasive_cgroup_shrink(const char *root)
 {
 	int ret = KSFT_FAIL;
 	size_t control_allocation_size = MB(10);
-	char *control_allocation, *wb_group = NULL, *control_group = NULL;
+	char *control_allocation = NULL, *wb_group = NULL, *control_group = NULL;
 
 	wb_group = setup_test_group_1M(root, "per_memcg_wb_test1");
 	if (!wb_group)
@@ -342,7 +342,7 @@ static int test_no_kmem_bypass(const char *root)
 	struct sysinfo sys_info;
 	int ret = KSFT_FAIL;
 	int child_status;
-	char *test_group;
+	char *test_group = NULL;
 	pid_t child_pid;
 
 	/* Read sys info and compute test values accordingly */
Signature · 1 line
2.45.0
maintainer Reviewed-by on v1 4/4
#8 lore raw

Re: [PATCH 4/4] selftests/cgroup: fix uninitialized variables in test_zswap.c

Quoted 4 lines · John Hubbard

First of all, in order to build with clang at all, one must first apply Valentin Obst's build fix for LLVM [1]. Once that is done, then when building with clang, via:

Quoted 8 lines

make LLVM=1 -C tools/testing/selftests

...clang finds and warning about some uninitialized variables. Fix these by initializing them.

[1] https://lore.kernel.org/all/20240329-selftests-libmk-llvm-rfc-v1-1-2f9ed7d1c49f@valentinobst.de/

Thanks!

maintainer Reviewed-by on v1 4/4
#9 lore raw

Re: [PATCH 4/4] selftests/cgroup: fix uninitialized variables in test_zswap.c

In reply to John Hubbard
Quoted 5 lines · John Hubbard

First of all, in order to build with clang at all, one must first apply Valentin Obst's build fix for LLVM [1]. Once that is done, then when building with clang, via:

Quoted 8 lines

make LLVM=1 -C tools/testing/selftests

...clang finds and warning about some uninitialized variables. Fix these by initializing them.

[1] https://lore.kernel.org/all/20240329-selftests-libmk-llvm-rfc-v1-1-2f9ed7d1c49f@valentinobst.de/

Quoted 1 line

---

maintainer Applied
#10 lore raw
In reply to John Hubbard
Quoted 11 lines · John Hubbard

Hi,

Just a bunch of fixes as part of my work to make selftests build cleanly with clang.

Enjoy!

thanks,
John Hubbard

Quoted 5 lines

John Hubbard (4):
selftests/cgroup: fix clang build failures for abs() calls selftests/cgroup: fix clang warnings: uninitialized fd variable selftests/cgroup: cpu_hogger init: use {} instead of {NULL} selftests/cgroup: fix uninitialized variables in test_zswap.c

Applied to cgroup/for-6.10.

Thanks.

Signature · 1 line
tejun