Skip to contentSkip to conversation

selftests/cgroup: fixes for test_zswap on single core VM

PATCH New Wilson Felipe Pereira 1 participant 4 messages Sep 4

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

v6 cover selftests/cgroup: fixes for test_zswap on single core VM

This series fixes two test failures in test_zswap observed when running on a single-core VM (-smp 1) with 4GB of RAM.

Patch 1 addresses a race condition in test_zswap_writeback() where waitpid() returns before the exiting child process is switched away by the kernel, causing an immediate write of "+memory" to cgroup.subtree_control to fail with -EBUSY. We fix this by waiting for cgroup.events to report "populated 0".

Patch 2 standardizes cgroup key-value stat and event parsers on 64-bit types by replacing cg_read_key_long() with cg_read_key_s64() across the entire cgroup selftest suite, avoiding 32-bit integer overflows on systems with large memory or high counter values.

Patch 3 fixes an implicit unsigned conversion bug in test_no_kmem_bypass() where small negative timing differences between debugfs stored_pages and cgroup zswapped bytes caused the comparison to falsely fail due to unsigned promotion.

v5 -> v6:
- Now it is a 3-patch series.
- Patch 1: wait for cgroup to unpopulate in test_zswap_writeback (unchanged, retains Michal's Acked-by).
- Patch 2: convert cg_read_key_*() helpers to s64, define s64 as signed long long in cgroup_util.h, update poll/reporting helpers (values_close, values_close_report with llabs), helper functions, and all callers across cgroup selftests to avoid 32-bit overflow/truncation (suggested by Michal Koutný).
- signed long long for s64 was based on powerpc/include/utils.h
- Patch 3: test_zswap: fix implicit unsigned promotion bug in test_no_kmem_bypass, updated to use s64 for delta and max_delta.
- Verify warning-free builds on both 64-bit and 32-bit (gcc -m32).

v4 -> v5:
- Patch 2: Introduce cg_read_key_long_long() to return a long long value, and have cg_read_key_long use it and convert to long.
- Patch 2: Make zswapped a long long. This prevents an overflow on 32-bit platforms (Andrew Morton, Sashiko).

v3 -> v4:
- Patch 2: Use long long for delta and max_delta and explicitly cast stored_pages to avoid integer overflow and implicit unsigned promotion warnings on 32-bit platforms (Andrew Morton, Sashiko).

v2 -> v3:
- Patch 1: Collect Acked-by from Michal Koutný.
- Patch 2: Explicitly cast stored_pages to (long) in delta calculation and inline the return comparison (Michal Koutný).

v1 -> v2:
- Patch 1: Replace EBUSY retry loop with cg_read_strcmp_wait() waiting for cgroup.events "populated 0" (Michal Koutný).
- Patch 1: Clarify task lifecycle in commit description (Yosry Ahmed).
- Patch 2: Remove abs() and declare delta/zswapped as signed longs with a signed threshold comparison (Michal Koutný).
- Patch 2: Add Fixes tag (Michal Koutný).

v5: https://lore.kernel.org/all/[email protected]/
v4: https://lore.kernel.org/all/[email protected]/
v3: https://lore.kernel.org/all/[email protected]/
v2: https://lore.kernel.org/all/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/

Wilson Felipe Pereira (3):
selftests/cgroup: test_zswap: wait for cgroup to unpopulate in
test_zswap_writeback
selftests/cgroup: convert cg_read_key_*() helpers to s64 selftests/cgroup: test_zswap: fix implicit unsigned promotion bug in
test_no_kmem_bypass

 .../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   | 63 +++++++++--------
 8 files changed, 154 insertions(+), 138 deletions(-)

--
2.55.0.979.g7e5102b832-goog

#1 lore raw

PATCH v6 1/3 selftests/cgroup: test_zswap: wait for cgroup to unpopulate in test_zswap_writeback

When running test_zswap on a single-core VM (-smp 1) with 4GB of RAM, test_zswap_writeback intermittently fails on the initial run after boot.

In test_zswap_writeback(), after waitpid() reaps the child process created by test_zswap_writeback_one(), writing "+memory" to cgroup.subtree_control can fail with -EBUSY. Under cgroup v2, enabling domain subtree controllers is forbidden while any tasks remain in cgroup.procs.

When a child process exits, exit_notify() wakes the parent process, allowing waitpid() to return immediately. However, the cgroup populated task count (nr_populated_csets) is only decremented when the exiting task is switched away via finish_task_switch() -> cgroup_task_dead(). On single-core systems, the parent runs before the dead child has been switched out, causing "+memory" to fail with -EBUSY if written immediately after waitpid() returns.

Fix this by waiting for cgroup.events to report "populated 0\n" via cg_read_strcmp_wait() before enabling subtree control.

---
 tools/testing/selftests/cgroup/test_zswap.c | 2 ++
 1 file changed, 2 insertions(+)
tools/testing/selftests/cgroup/test_zswap.c +2 -0
index 609c48f38524..8f2c9aa4776c 100644
@@ -408,6 +408,8 @@ static int test_zswap_writeback(const char *root, bool wb)
 	 * Thus, the parent's setting shall be what's in effect. */
 	if (cg_write(test_group, "memory.zswap.max", "max"))
 		goto out;
+	if (cg_read_strcmp_wait(test_group, "cgroup.events", "populated 0\n"))
+		goto out;
 	if (cg_write(test_group, "cgroup.subtree_control", "+memory"))
 		goto out;
 
Signature · 1 line
2.55.0.979.g7e5102b832-goog
#2 lore raw

PATCH v6 2/3 selftests/cgroup: convert cg_read_key_*() helpers to s64

In reply to Wilson Felipe Pereira

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.

---
 .../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   | 54 ++++++++-------
 8 files changed, 148 insertions(+), 135 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 +29 -25

141 lines not on this page · raw message

Signature · 1 line
2.55.0.979.g7e5102b832-goog
#3 lore raw

PATCH v6 3/3 selftests/cgroup: test_zswap: fix implicit unsigned promotion bug in test_no_kmem_bypass

In reply to Wilson Felipe Pereira

In test_no_kmem_bypass(), delta (stored_pages * page_size - zswapped) is checked against stored_pages * page_size / 4 to verify that the pages pushed to zswap belong to the test memory cgroup.

Due to slight stat update timing differences, delta can evaluate to a small negative number (e.g. -5MB out of 1GB). Because delta was declared as a signed int and stored_pages is an unsigned size_t, C's usual arithmetic conversions implicitly promote a negative delta to a large unsigned 64-bit integer, causing delta < stored_pages * page_size / 4 to falsely evaluate to 0 and fail the test.

Fix this by declaring delta and max_delta as signed 64-bit values (s64), explicitly casting stored_pages, and comparing against a signed threshold, ensuring negative deltas correctly evaluate to true.

Fixes a549f9f31561 ("selftests: cgroup: add test_zswap with no kmem bypass test")
---
 tools/testing/selftests/cgroup/test_zswap.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
tools/testing/selftests/cgroup/test_zswap.c +5 -4
index f4ee440b3c66..6e7ee11bc97a 100644
@@ -632,11 +632,12 @@ static int test_no_kmem_bypass(const char *root)
 			break;
 		/* If memory was pushed to zswap, verify it belongs to memcg */
 		if (stored_pages > stored_pages_threshold) {
-			s64 zswapped = cg_read_key_s64(test_group, "memory.stat", "zswapped ");
-			int delta = stored_pages * page_size - zswapped;
-			int result_ok = delta < stored_pages * page_size / 4;
+			s64 zswapped = cg_read_key_s64(
+				test_group, "memory.stat", "zswapped ");
+			s64 delta = (s64)stored_pages * page_size - zswapped;
+			s64 max_delta = (s64)stored_pages * page_size / 4;
 
-			ret = result_ok ? KSFT_PASS : KSFT_FAIL;
+			ret = (delta < max_delta) ? KSFT_PASS : KSFT_FAIL;
 			break;
 		}
 	}
Signature · 1 line
2.55.0.979.g7e5102b832-goog