Skip to contentSkip to conversation

selftests/cgroup: convert cg_read_*long*() helpers to s64

PATCH Applied Wilson Felipe Pereira 2 participants 2 messages Nov 6 – Nov 9

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

PATCH v2 1/1 selftests/cgroup: convert cg_read_*long*() helpers to s64

Cgroup byte and time counters in control files (such as memory.current, memory.peak, memory.stat, and cpu.stat) are exported by the kernel as 64-bit values, while cg_read_long(), cg_read_long_fd(), and cg_read_key_long() parse them with atol() and return a signed long. On 32-bit platforms, atol() clamps values above 2 GiB to LONG_MAX.

Although current tests do not allocate enough memory to hit LONG_MAX, converting these helpers to 64-bit keeps the selftest types consistent with the kernel counters and avoids 32-bit truncation:

- Define s64 as int64_t in cgroup_util.h (matching tools/include/linux/types.h) and use PRId64 from <inttypes.h>.
- Convert cg_read_long(), cg_read_long_fd(), cg_read_key_long(), and cg_read_key_long_poll() to cg_read_s64(), cg_read_s64_fd(), cg_read_key_s64(), and cg_read_key_s64_poll() using atoll().
- Update cg_write_numeric(), values_close(), and values_close_report() to take s64 and use llabs().
- Update callers across the cgroup selftests to use s64 and PRId64.

---
v1 -> v2:
- Rebase on linus/master (v7.4-rc1+) and convert the new cg_read_key_long() call in test_pids_events_notify() (Tejun Heo).
- Define s64 as int64_t in cgroup_util.h (matching tools/include/linux/types.h) and use PRId64 from <inttypes.h> to fix the KVM selftests build failure (Tejun Heo).
- Convert cg_read_long(), cg_read_long_fd(), and cg_write_numeric() to s64 alongside the remaining callers in test_freezer.c, test_cpu.c, test_hugetlb_memcg.c, test_kmem.c, test_memcontrol.c, and test_zswap.c (Tejun Heo).
- Update commit description to note atol() saturates at LONG_MAX and clean up stray whitespace/alignment changes (Tejun Heo).

v1: https://lore.kernel.org/all/[email protected]/

 .../selftests/cgroup/lib/cgroup_util.c        |  26 +--
 .../cgroup/lib/include/cgroup_util.h          |  32 +--
 tools/testing/selftests/cgroup/test_cpu.c     |  48 ++---
 tools/testing/selftests/cgroup/test_freezer.c |  88 +++++----
 .../selftests/cgroup/test_hugetlb_memcg.c     |  26 +--
 tools/testing/selftests/cgroup/test_kmem.c    |  62 +++---
 .../selftests/cgroup/test_memcontrol.c        | 183 +++++++++---------
 tools/testing/selftests/cgroup/test_pids.c    |   6 +-
 tools/testing/selftests/cgroup/test_zswap.c   |  77 ++++----
 9 files changed, 284 insertions(+), 264 deletions(-)
tools/testing/selftests/cgroup/lib/cgroup_util.c +13 -13
index 823b0312b2b2..279dfc5acfe1 100644
@@ -175,27 +175,27 @@ int cg_read_strstr(const char *cgroup, const char *control, const char *needle)
 	return strstr(buf, needle) ? 0 : -1;
 }
 
-long cg_read_long(const char *cgroup, const char *control)
+s64 cg_read_s64(const char *cgroup, const char *control)
 {
 	char buf[128];
 
 	if (cg_read(cgroup, control, buf, sizeof(buf)))
 		return -1;
 
-	return atol(buf);
+	return atoll(buf);
 }
 
-long cg_read_long_fd(int fd)
+s64 cg_read_s64_fd(int fd)
 {
 	char buf[128];
 
 	if (pread(fd, buf, sizeof(buf), 0) <= 0)
 		return -1;
 
-	return atol(buf);
+	return atoll(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;
@@ -207,18 +207,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;
 
@@ -270,12 +270,12 @@ int cg_open(const char *cgroup, const char *control, int flags)
 	return open(path, flags);
 }
 
-int cg_write_numeric(const char *cgroup, const char *control, long value)
+int cg_write_numeric(const char *cgroup, const char *control, s64 value)
 {
 	char buf[64];
 	int ret;
 
-	ret = sprintf(buf, "%lu", value);
+	ret = sprintf(buf, "%" PRId64, value);
 	if (ret < 0)
 		return ret;
 
tools/testing/selftests/cgroup/lib/include/cgroup_util.h +18 -14
index 5307f3bf32a0..bc0dd25f24ee 100644
@@ -1,7 +1,10 @@
 /* SPDX-License-Identifier: GPL-2.0 */
+#include <inttypes.h>
 #include <stdbool.h>
 #include <stdlib.h>
 
+typedef int64_t s64;
+
 #ifndef BUF_SIZE
 #define BUF_SIZE 4096
 #endif
@@ -27,26 +30,27 @@
 /*
  * 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 | "
-			"tolerance=%d%% | actual_error=%.2f%%\n",
+			"[FAIL] actual=%" PRId64 " expected=%" PRId64
+			" | diff=%" PRId64 " | limit=%" PRId64
+			" | tolerance=%d%% | actual_error=%.2f%%\n",
 			a, b, diff, limit, err, actual_err);
 
 	return close;
@@ -70,16 +74,16 @@ extern int cg_read_strcmp_wait(const char *cgroup, const char *control,
 				   const char *expected);
 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_s64(const char *cgroup, const char *control);
+s64 cg_read_s64_fd(int fd);
+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);
-int cg_write_numeric(const char *cgroup, const char *control, long value);
+int cg_write_numeric(const char *cgroup, const char *control, s64 value);
 extern int cg_run(const char *cgroup,
 		  int (*fn)(const char *cgroup, void *arg),
 		  void *arg);
tools/testing/selftests/cgroup/test_cpu.c +25 -23

144 lines not on this page · raw message

tools/testing/selftests/cgroup/test_freezer.c +46 -42

305 lines not on this page · raw message

tools/testing/selftests/cgroup/test_hugetlb_memcg.c +14 -12
index 8c5aced813b6..803248c496e4 100644
@@ -96,16 +96,17 @@ static int hugetlb_test_program(const char *cgroup, void *arg)
 {
 	char *test_group = (char *)arg;
 	void *addr;
-	long old_current, expected_current, current;
+	s64 old_current, expected_current, current;
 	int ret = EXIT_FAILURE;
 
-	old_current = cg_read_long(test_group, "memory.current");
+	old_current = cg_read_s64(test_group, "memory.current");
 	set_nr_hugepages(20);
-	current = cg_read_long(test_group, "memory.current");
+	current = cg_read_s64(test_group, "memory.current");
 	if (current - old_current >= MB(2)) {
 		ksft_print_msg(
 			"setting nr_hugepages should not increase hugepage usage.\n");
-		ksft_print_msg("before: %ld, after: %ld\n", old_current, current);
+		ksft_print_msg("before: %" PRId64 ", after: %" PRId64 "\n",
+			       old_current, current);
 		return EXIT_FAILURE;
 	}
 
@@ -114,10 +115,11 @@ static int hugetlb_test_program(const char *cgroup, void *arg)
 		ksft_print_msg("fail to mmap.\n");
 		return EXIT_FAILURE;
 	}
-	current = cg_read_long(test_group, "memory.current");
+	current = cg_read_s64(test_group, "memory.current");
 	if (current - old_current >= MB(2)) {
 		ksft_print_msg("mmap should not increase hugepage usage.\n");
-		ksft_print_msg("before: %ld, after: %ld\n", old_current, current);
+		ksft_print_msg("before: %" PRId64 ", after: %" PRId64 "\n",
+			       old_current, current);
 		goto out_failed_munmap;
 	}
 	old_current = current;
@@ -125,35 +127,35 @@ static int hugetlb_test_program(const char *cgroup, void *arg)
 	/* read the first page */
 	check_first(addr);
 	expected_current = old_current + MB(2);
-	current = cg_read_long(test_group, "memory.current");
+	current = cg_read_s64(test_group, "memory.current");
 	if (!values_close(expected_current, current, 5)) {
 		ksft_print_msg("memory usage should increase by around 2MB.\n");
 		ksft_print_msg(
-			"expected memory: %ld, actual memory: %ld\n",
+			"expected memory: %" PRId64 ", actual memory: %" PRId64 "\n",
 			expected_current, current);
 		goto out_failed_munmap;
 	}
 
 	/* write to the whole range */
 	write_data(addr);
-	current = cg_read_long(test_group, "memory.current");
+	current = cg_read_s64(test_group, "memory.current");
 	expected_current = old_current + MB(8);
 	if (!values_close(expected_current, current, 5)) {
 		ksft_print_msg("memory usage should increase by around 8MB.\n");
 		ksft_print_msg(
-			"expected memory: %ld, actual memory: %ld\n",
+			"expected memory: %" PRId64 ", actual memory: %" PRId64 "\n",
 			expected_current, current);
 		goto out_failed_munmap;
 	}
 
 	/* unmap the whole range */
 	munmap(addr, LENGTH);
-	current = cg_read_long(test_group, "memory.current");
+	current = cg_read_s64(test_group, "memory.current");
 	expected_current = old_current;
 	if (!values_close(expected_current, current, 5)) {
 		ksft_print_msg("memory usage should go back down.\n");
 		ksft_print_msg(
-			"expected memory: %ld, actual memory: %ld\n",
+			"expected memory: %" PRId64 ", actual memory: %" PRId64 "\n",
 			expected_current, current);
 		return ret;
 	}
tools/testing/selftests/cgroup/test_kmem.c +32 -30

141 lines not on this page · raw message

tools/testing/selftests/cgroup/test_memcontrol.c +92 -91

577 lines not on this page · raw message

tools/testing/selftests/cgroup/test_pids.c +3 -3
index e775c5510cf2..9c1cda47c73d 100644
@@ -114,9 +114,9 @@ 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;
 
 
@@ -179,7 +179,7 @@ static int test_pids_events_notify(const char *root)
 	if (cg_run_nowait(cg_pids, run_success, NULL) != -1 || errno != EAGAIN)
 		goto cleanup;
 
-	if (cg_read_key_long(cg_pids, "pids.events", "max ") != 1)
+	if (cg_read_key_s64(cg_pids, "pids.events", "max ") != 1)
 		goto cleanup;
 
 	pfd.fd = inotify_fd;
tools/testing/selftests/cgroup/test_zswap.c +41 -36

194 lines not on this page · raw message

Signature · 1 line
2.56.0.506.g506a7eabc5-goog
maintainer Applied
#1 lore raw

Applied to cgroup/for-7.5.

Thanks.

--
tejun