Skip to contentSkip to conversation

selftests/bpf: Skip memcg_proactive_reclaim when data lands in shmem

PATCH Superseded Kurt Lin 3 participants 4 messages Nov 13 – Nov 16

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Kurt Lin

author
#0 lore raw

PATCH 1/1 selftests/bpf: Skip memcg_proactive_reclaim when data lands in shmem

workload_dir() keeps the data file off tmpfs because shmem cannot be reclaimed without swap. But statfs() only sees the top of a stacking
filesystem: with /tmp on overlayfs over tmpfs, as in virtme-ng guests, the check passes and all 32 MiB are still charged as shmem. Whether the test then passes depends on unrelated page cache in the cgroup; in a full test_progs run it fails with:

test_memcg_proactive_reclaim:FAIL:reclaimed unexpected reclaimed: actual 0 <= expected 0

Check memory.stat after writing the file and skip when most of the data was charged as shmem.

An AI coding assistant (Claude) was used to find and analyze the failure and to draft the fix and this changelog.

Fixes 0f9b1e9ef584 ("selftests/bpf: Add bpf_proactive_reclaim test")

---

Notes:
Tested in a virtme-ng guest without swap: with /tmp on overlayfs over tmpfs the test now skips; on a disk-backed directory it passes.

After writing 32 MiB, memory.stat shows shmem 32.0 MiB on overlayfs /tmp and on tmpfs, and 0.0 MiB on a disk-backed filesystem.

 .../bpf/prog_tests/memcg_proactive_reclaim.c  | 28 +++++++++++++++++++
 1 file changed, 28 insertions(+)
tools/testing/selftests/bpf/prog_tests/memcg_proactive_reclaim.c +28 -0
index c22dcfabe856..c03550676041 100644
@@ -46,6 +46,28 @@ static const char *workload_dir(void)
 	return NULL;
 }
 
+/*
+ * statfs() only sees the top of a stacking filesystem. When /tmp is an
+ * overlayfs whose upper layer lives on tmpfs, as in virtme-ng guests, the
+ * check above passes but the written data still ends up as shmem. Ask the
+ * cgroup how the pages were actually charged.
+ */
+static bool charged_as_shmem(void)
+{
+	static char buf[8192];
+	unsigned long long shmem;
+	char *p;
+
+	if (read_cgroup_file(CG_PATH, "memory.stat", buf, sizeof(buf)))
+		return false;
+
+	p = strstr(buf, "\nshmem ");
+	if (!p || sscanf(p, "\nshmem %llu", &shmem) != 1)
+		return false;
+
+	return shmem >= FILE_SIZE / 2;
+}
+
 void test_memcg_proactive_reclaim(void)
 {
 	struct memcg_proactive_reclaim *skel = NULL;
@@ -99,6 +121,12 @@ void test_memcg_proactive_reclaim(void)
 	if (!ASSERT_OK(fsync(data_fd), "fsync"))
 		goto out;
 
+	if (charged_as_shmem()) {
+		printf("%s:SKIP: %s is backed by shmem\n", __func__, dir);
+		test__skip();
+		goto out;
+	}
+
 	args.cgroup_id = cgroup_id;
 	args.size = FILE_SIZE;
 	skel->bss->reclaimed = 0;

base-commit: 0021f17128de5dc0b19f1a523f448df2440d8088

Signature · 1 line
2.56.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Low] selftests/bpf: workload_dir() never falls back to . when /tmp is overlayfs - [Low] selftests/bpf…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Low] selftests/bpf: workload_dir() never falls back to . when /tmp is overlayfs
- [Low] selftests/bpf: unflushed per-CPU memcg stats read in charged_as_shmem()
--

commit e2cf55e88beac1906e9ded044c8e29f6b8bf9980
Author: Kurt Lin <[email protected]>

selftests/bpf: Skip memcg_proactive_reclaim when data lands in shmem

This commit skips the memcg_proactive_reclaim selftest when memory.stat shows that the test file's pages were charged as shmem, such as when /tmp is an overlayfs backed by tmpfs.

Quoted 19 lines
tools/testing/selftests/bpf/prog_tests/memcg_proactive_reclaim.c +11 -0
index c22dcfabe856e..c035506760416 100644
@@ -46,6 +46,28 @@ static const char *workload_dir(void)
 	return NULL;
 }
 
+/*
+ * statfs() only sees the top of a stacking filesystem. When /tmp is an
+ * overlayfs whose upper layer lives on tmpfs, as in virtme-ng guests, the
+ * check above passes but the written data still ends up as shmem. Ask the
+ * cgroup how the pages were actually charged.
+ */
+static bool charged_as_shmem(void)
+{
+	static char buf[8192];
+	unsigned long long shmem;
+	char *p;

tools/testing/selftests/bpf/prog_tests/memcg_proactive_reclaim.c static const char *workload_dir(void) · line 69

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/memcg_proactive_reclaim.c +10 -0
+
+	if (read_cgroup_file(CG_PATH, "memory.stat", buf, sizeof(buf)))
+		return false;
+
+	p = strstr(buf, "\nshmem ");
+	if (!p || sscanf(p, "\nshmem %llu", &shmem) != 1)
+		return false;
+
+	return shmem >= FILE_SIZE / 2;
+}

[Severity: Low]
Can charged_as_shmem() read 0 for shmem when per-CPU memcg stats have not been flushed yet?

Reading memory.stat calls __mem_cgroup_flush_stats(memcg, false), which skips the rstat flush unless pending updates exceed MEMCG_CHARGE_BATCH * num_online_cpus():

mm/memcontrol.c:memcg_vmstats_needs_flush() {
	return atomic_long_read(&vmstats->stats_updates) >
		MEMCG_CHARGE_BATCH * num_online_cpus();
}

On a 64K-page kernel with 24 or more online CPUs (or a 4K-page kernel with 384 or more CPUs), writing 32 MiB of shmem does not cross that threshold, so charged_as_shmem() sees shmem as 0 and fails to skip the test. Retrying until the "file " counter in memory.stat reaches at least FILE_SIZE / 2 (across the 2s periodic flusher window, as in cgroup_iter_memcg.c) would ensure the stats are flushed before checking shmem.

tools/testing/selftests/bpf/prog_tests/memcg_proactive_reclaim.c void test_memcg_proactive_reclaim(void) · line 128

Quoted 12 lines
tools/testing/selftests/bpf/prog_tests/memcg_proactive_reclaim.c +5 -0
 void test_memcg_proactive_reclaim(void)
 {
 	struct memcg_proactive_reclaim *skel = NULL;
@@ -99,6 +121,12 @@ void test_memcg_proactive_reclaim(void)
 	if (!ASSERT_OK(fsync(data_fd), "fsync"))
 		goto out;
 
+	if (charged_as_shmem()) {
+		printf("%s:SKIP: %s is backed by shmem\n", __func__, dir);
+		test__skip();
+		goto out;
+	}

[Severity: Low]
Does jumping straight to out here prevent falling back to "." when /tmp is overlayfs over tmpfs?

workload_dir() checks dirs[] = { "/tmp", "." } so the test can use the current working directory when /tmp is on tmpfs. When /tmp is an overlayfs over tmpfs, statfs("/tmp", &st) sees OVERLAYFS_SUPER_MAGIC and returns "/tmp" on the first iteration, and this check then skips the whole test without ever trying "." even if test_progs is running from a writable disk-backed directory.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to Kurt Lin

Hi Kurt,

I have two suggestions below.

2026/11/14 02:01, Kurt Lin:

Quoted 41 lines

workload_dir() keeps the data file off tmpfs because shmem cannot be reclaimed without swap. But statfs() only sees the top of a stacking
filesystem: with /tmp on overlayfs over tmpfs, as in virtme-ng guests, the check passes and all 32 MiB are still charged as shmem. Whether the test then passes depends on unrelated page cache in the cgroup; in a full test_progs run it fails with:

test_memcg_proactive_reclaim:FAIL:reclaimed unexpected reclaimed: actual 0 <= expected 0

Check memory.stat after writing the file and skip when most of the data was charged as shmem.

An AI coding assistant (Claude) was used to find and analyze the failure and to draft the fix and this changelog.

Fixes 0f9b1e9ef584 ("selftests/bpf: Add bpf_proactive_reclaim test")

---

Notes:
Tested in a virtme-ng guest without swap: with /tmp on overlayfs over tmpfs the test now skips; on a disk-backed directory it passes.

After writing 32 MiB, memory.stat shows shmem 32.0 MiB on overlayfs /tmp and on tmpfs, and 0.0 MiB on a disk-backed filesystem.

  .../bpf/prog_tests/memcg_proactive_reclaim.c  | 28 +++++++++++++++++++
  1 file changed, 28 insertions(+)
tools/testing/selftests/bpf/prog_tests/memcg_proactive_reclaim.c +4 -0
index c22dcfabe856..c03550676041 100644
@@ -46,6 +46,28 @@ static const char *workload_dir(void)
  	return NULL;
  }
 
+/*
+ * statfs() only sees the top of a stacking filesystem. When /tmp is an
+ * overlayfs whose upper layer lives on tmpfs, as in virtme-ng guests, the
+ * check above passes but the written data still ends up as shmem. Ask the

tools/testing/selftests/bpf/prog_tests/memcg_proactive_reclaim.c static const char *workload_dir(void) · line 62

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/memcg_proactive_reclaim.c +10 -0
+ * cgroup how the pages were actually charged.
+ */
+static bool charged_as_shmem(void)
+{
+	static char buf[8192];
+	unsigned long long shmem;
+	char *p;
+
+	if (read_cgroup_file(CG_PATH, "memory.stat", buf, sizeof(buf)))
+		return false;

Would it make sense to turn the silent "return false" into an explicit assertion, e.g.:

    if (!ASSERT_OK(read_cgroup_file(CG_PATH, "memory.stat", buf, sizeof(buf)), "read memory.stat")) return false;

My reasoning: memory.stat exists on every cgroup v2 hierarchy and the "shmem" key is emitted unconditionally (not behind any CONFIG), and cgroup_setup_and_join() has already succeeded by the time this runs. So a read/parse failure here likely means something is genuinely broken, rather than an environment limitation. With the silent fallback, a shmem-backed dir falls through to the old confusing "reclaimed: actual 0 <= expected 0" failure and the real cause is never reported. The ASSERT_* macros are statement expressions, so this works inside the bool helper: the failure is recorded with a clear message first, and any subsequent "reclaimed" failure becomes obviously secondary. (Skipping on the error path would be worse still -- a genuine breakage would be counted as a pass, so I'd keep it as a failure either way.)

tools/testing/selftests/bpf/prog_tests/memcg_proactive_reclaim.c static const char *workload_dir(void) · line 68

Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/memcg_proactive_reclaim.c +6 -0
+
+	p = strstr(buf, "\nshmem ");
+	if (!p || sscanf(p, "\nshmem %llu", &shmem) != 1)
+		return false;
+
+	return shmem >= FILE_SIZE / 2;

Small nit: a one-line comment on the threshold might help future readers, something like "all-or-nothing: the data file is either fully shmem (stacked tmpfs) or not shmem at all (disk-backed), so half of FILE_SIZE is a safe midpoint". Also, printing the measured shmem value in the SKIP message could be handy when debugging odd environments.

Best,
Hui

Quoted 20 lines
tools/testing/selftests/bpf/prog_tests/memcg_proactive_reclaim.c +8 -0
+}
+
  void test_memcg_proactive_reclaim(void)
  {
  	struct memcg_proactive_reclaim *skel = NULL;
@@ -99,6 +121,12 @@ void test_memcg_proactive_reclaim(void)
  	if (!ASSERT_OK(fsync(data_fd), "fsync"))
  		goto out;
 
+	if (charged_as_shmem()) {
+		printf("%s:SKIP: %s is backed by shmem\n", __func__, dir);
+		test__skip();
+		goto out;
+	}
+
  	args.cgroup_id = cgroup_id;
  	args.size = FILE_SIZE;
  	skel->bss->reclaimed = 0;

base-commit: 0021f17128de5dc0b19f1a523f448df2440d8088

author
#3 lore raw

Hi Hui,

Thanks for the review.

Quoted 6 lines · Hui Zhu

Would it make sense to turn the silent "return false" into an explicit assertion, e.g.:

     if (!ASSERT_OK(read_cgroup_file(CG_PATH, "memory.stat", buf,
                     sizeof(buf)), "read memory.stat"))
         return false;

Agreed, a read or parse failure will fail the test in v2.

Quoted 5 lines

Small nit: a one-line comment on the threshold might help future readers, something like "all-or-nothing: the data file is either fully shmem (stacked tmpfs) or not shmem at all (disk-backed), so half of FILE_SIZE is a safe midpoint". Also, printing the measured shmem value in the SKIP message could be handy when debugging odd environments.

Will do both.

v2 will also address two points sashiko raised: memory.stat is not force-flushed, so right after the write it can still read 0 on machines with many CPUs, and a shmem-backed /tmp should fall back to "." instead of skipping. I plan to poll memory.stat until the write shows up and to try each candidate directory in a cgroup of its own.

Thanks,
Kurt