Skip to contentSkip to conversation

selftests/cgroup: convert cg_read_key_*() helpers to s64

PATCH Superseded Wilson Felipe Pereira 2 participants 3 messages Oct 22 – Oct 24

Subsystems (5)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selftests/cgroup: convert cg_read_key_*() helpers to s64

In the kernel, cgroup statistics and event counters in multi-key control files (such as memory.stat, cpu.stat, and memory.events) are internally stored and exported as 64-bit values.

On 32-bit platforms, cg_read_key_long() relies on atol(), which returns a 32-bit signed long. When values in memory.stat or event counters exceed 2.14GB (such as on 32-bit systems with large memory), the parsed value overflows and wraps into negative numbers, leading to subtle bugs and false test failures.

Standardize on explicitly-sized 64-bit helpers and types across the cgroup selftests:

- Define s64 in cgroup_util.h as signed long long.
- Convert cg_read_key_long() to cg_read_key_s64() using atoll().
- Convert cg_read_key_long_poll() to cg_read_key_s64_poll(), updating both the return type and the expected argument to s64.
- Update values_close() and values_close_report() to operate on s64 and use llabs() to prevent downcasting/truncation of 64-bit differences.
- Migrate helper functions (get_cg_wb_count(), get_zswpout(), get_zswap_incomp(), wait_for_writeback(), and cg_check_freezetime()) and their callers to s64.
- Migrate all callers across test_cpu, test_kmem, test_memcontrol, test_pids, test_freezer, and test_zswap to cg_read_key_s64().
- Update corresponding printf formats to %lld and difference checks to llabs() for s64.

---

Notes:
This patch was previously Patch 2/3 of v6 ("selftests/cgroup: fixes for test_zswap on single core VM"):
https://lore.kernel.org/all/[email protected]/

Since Patches 1 and 3 from that series were already applied to mm-stable from v4 ("selftests/cgroup: test_zswap: wait for cgroup to unpopulate in test_zswap_writeback" and "selftests/cgroup: test_zswap: fix implicit unsigned promotion bug in test_no_kmem_bypass"), this standalone patch is rebased on top of mm-stable to complete the s64 conversion across the cgroup selftests (including updating test_no_kmem_bypass() to s64).

 .../selftests/cgroup/lib/cgroup_util.c        | 14 ++--
 .../cgroup/lib/include/cgroup_util.h          | 22 +++---
 tools/testing/selftests/cgroup/test_cpu.c     | 47 ++++++-------
 tools/testing/selftests/cgroup/test_freezer.c | 16 ++---
 tools/testing/selftests/cgroup/test_kmem.c    | 56 ++++++++-------
 .../selftests/cgroup/test_memcontrol.c        | 69 ++++++++++---------
 tools/testing/selftests/cgroup/test_pids.c    |  5 +-
 tools/testing/selftests/cgroup/test_zswap.c   | 62 +++++++++--------
 8 files changed, 151 insertions(+), 140 deletions(-)
tools/testing/selftests/cgroup/lib/cgroup_util.c +7 -7
index 2596c12cd864..ed3c4893833a 100644
@@ -172,7 +172,7 @@ long cg_read_long_fd(int fd)
 	return atol(buf);
 }
 
-long cg_read_key_long(const char *cgroup, const char *control, const char *key)
+s64 cg_read_key_s64(const char *cgroup, const char *control, const char *key)
 {
 	char buf[BUF_SIZE];
 	char *ptr;
@@ -184,18 +184,18 @@ long cg_read_key_long(const char *cgroup, const char *control, const char *key)
 	if (!ptr)
 		return -1;
 
-	return atol(ptr + strlen(key));
+	return atoll(ptr + strlen(key));
 }
 
-long cg_read_key_long_poll(const char *cgroup, const char *control,
-			   const char *key, long expected, int retries,
-			   useconds_t wait_interval_us)
+s64 cg_read_key_s64_poll(const char *cgroup, const char *control,
+			 const char *key, s64 expected, int retries,
+			 useconds_t wait_interval_us)
 {
-	long val = -1;
+	s64 val = -1;
 	int i;
 
 	for (i = 0; i < retries; i++) {
-		val = cg_read_key_long(cgroup, control, key);
+		val = cg_read_key_s64(cgroup, control, key);
 		if (val < 0)
 			return val;
 
tools/testing/selftests/cgroup/lib/include/cgroup_util.h +12 -10
index 5d39c709ac7a..ddb05932d9c7 100644
@@ -2,6 +2,8 @@
 #include <stdbool.h>
 #include <stdlib.h>
 
+typedef signed long long s64;
+
 #ifndef BUF_SIZE
 #define BUF_SIZE 4096
 #endif
@@ -24,25 +26,25 @@
 /*
  * Checks if two given values differ by less than err% of their sum.
  */
-static inline int values_close(long a, long b, int err)
+static inline int values_close(s64 a, s64 b, int err)
 {
-	return labs(a - b) <= (a + b) / 100 * err;
+	return llabs(a - b) <= (a + b) / 100 * err;
 }
 
 /*
  * Checks if two given values differ by less than err% of their sum and assert
  * with detailed debug info if not.
  */
-static inline int values_close_report(long a, long b, int err)
+static inline int values_close_report(s64 a, s64 b, int err)
 {
-	long diff  = labs(a - b);
-	long limit = (a + b) / 100 * err;
+	s64 diff  = llabs(a - b);
+	s64 limit = (a + b) / 100 * err;
 	double actual_err = (a + b) ? (100.0 * diff / (a + b)) : 0.0;
 	int close = diff <= limit;
 
 	if (!close)
 		fprintf(stderr,
-			"[FAIL] actual=%ld expected=%ld | diff=%ld | limit=%ld | "
+			"[FAIL] actual=%lld expected=%lld | diff=%lld | limit=%lld | "
 			"tolerance=%d%% | actual_error=%.2f%%\n",
 			a, b, diff, limit, err, actual_err);
 
@@ -69,10 +71,10 @@ extern int cg_read_strstr(const char *cgroup, const char *control,
 			  const char *needle);
 extern long cg_read_long(const char *cgroup, const char *control);
 extern long cg_read_long_fd(int fd);
-long cg_read_key_long(const char *cgroup, const char *control, const char *key);
-long cg_read_key_long_poll(const char *cgroup, const char *control,
-			   const char *key, long expected, int retries,
-			   useconds_t wait_interval_us);
+s64 cg_read_key_s64(const char *cgroup, const char *control, const char *key);
+s64 cg_read_key_s64_poll(const char *cgroup, const char *control,
+			 const char *key, s64 expected, int retries,
+			 useconds_t wait_interval_us);
 extern long cg_read_lc(const char *cgroup, const char *control);
 extern int cg_write(const char *cgroup, const char *control, char *buf);
 extern int cg_open(const char *cgroup, const char *control, int flags);
tools/testing/selftests/cgroup/test_cpu.c +24 -23

137 lines not on this page · raw message

tools/testing/selftests/cgroup/test_freezer.c +7 -9
index f28bb02e9783..f4f082c13d4b 100644
@@ -807,10 +807,9 @@ static int test_cgfreezer_vfork(const char *root)
 /*
  * Get the current frozen_usec for the cgroup.
  */
-static long cg_check_freezetime(const char *cgroup)
+static s64 cg_check_freezetime(const char *cgroup)
 {
-	return cg_read_key_long(cgroup, "cgroup.stat.local",
-				"frozen_usec ");
+	return cg_read_key_s64(cgroup, "cgroup.stat.local", "frozen_usec ");
 }
 
 /*
@@ -820,7 +819,7 @@ static int test_cgfreezer_time_empty(const char *root)
 {
 	int ret = KSFT_FAIL;
 	char *cgroup = NULL;
-	long prev, curr;
+	s64 prev, curr;
 
 	cgroup = cg_name(root, "cg_time_test_empty");
 	if (!cgroup)
@@ -839,7 +838,7 @@ static int test_cgfreezer_time_empty(const char *root)
 		goto cleanup;
 	}
 	if (curr > 0) {
-		debug("Expect time (%ld) to be 0\n", curr);
+		debug("Expect time (%lld) to be 0\n", curr);
 		goto cleanup;
 	}
 
@@ -853,8 +852,7 @@ static int test_cgfreezer_time_empty(const char *root)
 	usleep(1000);
 	curr = cg_check_freezetime(cgroup);
 	if (curr < 1000) {
-		debug("Expect time (%ld) to be at least 1000 us\n",
-		      curr);
+		debug("Expect time (%lld) to be at least 1000 us\n", curr);
 		goto cleanup;
 	}
 
@@ -867,7 +865,7 @@ static int test_cgfreezer_time_empty(const char *root)
 	prev = curr;
 	curr = cg_check_freezetime(cgroup);
 	if (curr <= prev) {
-		debug("Expect time (%ld) to be more than previous check (%ld)\n",
+		debug("Expect time (%lld) to be more than previous check (%lld)\n",
 		      curr, prev);
 		goto cleanup;
 	}
@@ -879,7 +877,7 @@ static int test_cgfreezer_time_empty(const char *root)
 	prev = curr;
 	curr = cg_check_freezetime(cgroup);
 	if (curr != prev) {
-		debug("Expect time (%ld) to be unchanged from previous check (%ld)\n",
+		debug("Expect time (%lld) to be unchanged from previous check (%lld)\n",
 		      curr, prev);
 		goto cleanup;
 	}
tools/testing/selftests/cgroup/test_kmem.c +30 -26

133 lines not on this page · raw message

tools/testing/selftests/cgroup/test_memcontrol.c +37 -32

213 lines not on this page · raw message

tools/testing/selftests/cgroup/test_pids.c +2 -3
index 710109b53dfe..f4d5bc9f5f24 100644
@@ -111,12 +111,11 @@ static int test_pids_events(const char *root)
 	if (kill(pid, SIGINT))
 		goto cleanup;
 
-	if (cg_read_key_long(cg_child, "pids.events", "max ") != 0)
+	if (cg_read_key_s64(cg_child, "pids.events", "max ") != 0)
 		goto cleanup;
-	if (cg_read_key_long(cg_parent, "pids.events", "max ") != 1)
+	if (cg_read_key_s64(cg_parent, "pids.events", "max ") != 1)
 		goto cleanup;
 
-
 	ret = KSFT_PASS;
 
 cleanup:
tools/testing/selftests/cgroup/test_zswap.c +32 -30

156 lines not on this page · raw message

Signature · 1 line
2.56.0.218.g1ec904b173-goog
maintainer
#1 lore raw

Hello, Wilson.

The following is a Claude-generated review.

Quoted 5 lines · Wilson Felipe Pereira

On 32-bit platforms, cg_read_key_long() relies on atol(), which returns a 32-bit signed long. When values in memory.stat or event counters exceed 2.14GB (such as on 32-bit systems with large memory), the parsed value overflows and wraps into negative numbers, leading to subtle bugs and false test failures.

On 32-bit, glibc's atol() saturates at LONG_MAX instead of wrapping:
atol("3000000000") returns 2147483647. Event counters are counts, not bytes. No test reads a value near 2^31 from its cgroups, so the false failures are hypothetical. Can the description be updated to match?

cg_read_long() and cg_read_long_fd() still use atol() and return long. They read memory.current, memory.peak and the swap counters, which the same reasoning covers.

Quoted 3 lines

- Migrate helper functions (get_cg_wb_count(), get_zswpout(), get_zswap_incomp(), wait_for_writeback(), and cg_check_freezetime()) and their callers to s64.

In test_freezer.c, only test_cgfreezer_time_empty() is converted. test_cgfreezer_time_simple(), _populate(), _migrate(), _parent(), _child() and _nested() still store cg_check_freezetime() results in long and print them with %ld. In test_cpu.c, overprovision_validate() still computes "long delta" from two s64 usage values. A 32-bit build with -Wshorten-64-to-32 flags all of these.

Quoted 2 lines

rebased on top of mm-stable to complete the s64 conversion across the cgroup selftests (including updating test_no_kmem_bypass() to s64).

This doesn't apply to mainline. The test_zswap.c hunks depend on the test_zswap fixes in mm-stable, which aren't in mainline yet. The tcp_client() hunk in test_memcontrol.c conflicts with eb69e955de7b ("selftests: cgroup: make test_memcg_sock check flushed sock stats"), which went in through the cgroup tree for v7.4. Can it be rebased on v7.4-rc1 once that's out?

Quoted 1 line

+typedef signed long long s64;

This is the main problem. The KVM selftests build libcgroup too:
kvm/Makefile.kvm includes ../cgroup/lib/libcgroup.mk and links cgroup_util.o into every KVM test, and access_tracking_perf_test.c includes cgroup_util.h. That build uses -I tools/include, and tools/include/linux/types.h has "typedef int64_t s64;", which is long on 64-bit.

cgroup_util.c then fails with "typedef redefinition with different types ('long long' vs 'int64_t' (aka 'long'))", so none of the KVM selftests build on 64-bit. Maybe use int64_t with PRId64 from <inttypes.h> instead of the local typedef and %lld?

tools/testing/selftests/cgroup/test_pids.c static int test_pids_events(const char *root) · line 119

Quoted 2 lines
tools/testing/selftests/cgroup/test_pids.c +0 -1
-
 	ret = KSFT_PASS;

The blank line removal is unrelated to the conversion, same for the one in test_cpu.c.

tools/testing/selftests/cgroup/test_zswap.c static int test_zswap_incompressible(const char *root) · line 804

Quoted 2 lines
tools/testing/selftests/cgroup/test_zswap.c +2 -0
+		ksft_print_msg("debug: zswpout=%lld zswapped=%lld zswap_b=%lld\n",
+				zswpout, zswapped, zswap_b);

The continuation line was aligned with the opening parenthesis before and now isn't.

Thanks.

--
tejun

#2 lore raw
Quoted 61 lines · Tejun Heo

Hello, Wilson.

The following is a Claude-generated review.

Quoted 5 lines · Wilson Felipe Pereira

On 32-bit platforms, cg_read_key_long() relies on atol(), which returns a 32-bit signed long. When values in memory.stat or event counters exceed 2.14GB (such as on 32-bit systems with large memory), the parsed value overflows and wraps into negative numbers, leading to subtle bugs and false test failures.

On 32-bit, glibc's atol() saturates at LONG_MAX instead of wrapping:
atol("3000000000") returns 2147483647. Event counters are counts, not bytes. No test reads a value near 2^31 from its cgroups, so the false failures are hypothetical. Can the description be updated to match?

cg_read_long() and cg_read_long_fd() still use atol() and return long. They read memory.current, memory.peak and the swap counters, which the same reasoning covers.

Quoted 3 lines

- Migrate helper functions (get_cg_wb_count(), get_zswpout(), get_zswap_incomp(), wait_for_writeback(), and cg_check_freezetime()) and their callers to s64.

In test_freezer.c, only test_cgfreezer_time_empty() is converted. test_cgfreezer_time_simple(), _populate(), _migrate(), _parent(), _child() and _nested() still store cg_check_freezetime() results in long and print them with %ld. In test_cpu.c, overprovision_validate() still computes "long delta" from two s64 usage values. A 32-bit build with -Wshorten-64-to-32 flags all of these.

Quoted 2 lines

rebased on top of mm-stable to complete the s64 conversion across the cgroup selftests (including updating test_no_kmem_bypass() to s64).

This doesn't apply to mainline. The test_zswap.c hunks depend on the test_zswap fixes in mm-stable, which aren't in mainline yet. The tcp_client() hunk in test_memcontrol.c conflicts with eb69e955de7b ("selftests: cgroup: make test_memcg_sock check flushed sock stats"), which went in through the cgroup tree for v7.4. Can it be rebased on v7.4-rc1 once that's out?

Quoted 1 line

+typedef signed long long s64;

This is the main problem. The KVM selftests build libcgroup too:
kvm/Makefile.kvm includes ../cgroup/lib/libcgroup.mk and links cgroup_util.o into every KVM test, and access_tracking_perf_test.c includes cgroup_util.h. That build uses -I tools/include, and tools/include/linux/types.h has "typedef int64_t s64;", which is long on 64-bit.

cgroup_util.c then fails with "typedef redefinition with different types ('long long' vs 'int64_t' (aka 'long'))", so none of the KVM selftests build on 64-bit. Maybe use int64_t with PRId64 from <inttypes.h> instead of the local typedef and %lld?

tools/testing/selftests/cgroup/test_pids.c static int test_pids_events(const char *root) · line 119

Quoted 2 lines
tools/testing/selftests/cgroup/test_pids.c +0 -1
-
 	ret = KSFT_PASS;

The blank line removal is unrelated to the conversion, same for the one in test_cpu.c.

Quoted 7 lines

tools/testing/selftests/cgroup/test_zswap.c static int test_zswap_incompressible(const char *root) · line 804

Quoted 2 lines
tools/testing/selftests/cgroup/test_zswap.c +2 -0
+		ksft_print_msg("debug: zswpout=%lld zswapped=%lld zswap_b=%lld\n",
+				zswpout, zswapped, zswap_b);

The continuation line was aligned with the opening parenthesis before and now isn't.

Thanks.

Thanks for the review and I'm sorry for the build failures! I believe I messed up with I rebased it on mm-stable and didn't check all places that had changed.

I've updated the patch locally to:
* Use typedef int64_t s64 and to use PRId64 from <inttypes.h>, similar to how KVM selftests are using, while fixing the build failures.
* Convert cg_read_long(), cg_read_long_fd(), and cg_write_numeric() to s64 along with the remaining callers in test_freezer.c, test_cpu.c, test_hugetlb_memcg.c, test_kmem.c, test_memcontrol.c, and test_zswap.c.
* Update the commit description to reflect atol() clamping at LONG_MAX and clean up the whitespace changes.

I'll wait until 7.4-rc1 is out and send the v2 that applies cleanly on mm-stable and cgroup tree.

Thanks!