Re: [PATCH bpf-next v2] selftests/bpf: Fall back when memcg_proactive_reclaim data lands in shmem

From: Hui Zhu

Date: Wed Nov 18 2026 - 22:05:14 EST



2026/11/18 11:49, Alexei Starovoitov:
On Tue, Nov 17, 2026 at 09:07 AM Kurt Lin <lylin1023@xxxxxxxxx> wrote:
After writing the file, check memory.stat and move on to the next
candidate directory if most of the data was charged as shmem; skip only
when none is left. Give each candidate its own cgroup so that stale
stats from a previous attempt cannot interfere, and poll memory.stat
until the write shows up, as reading it does not force a stats flush.
Fail the test if memory.stat cannot be read or never shows the write.
This is overkill.

To exercise the polling, FILE_SIZE was temporarily cut to 2 MiB in a
128-vCPU guest, which keeps the write below the flush threshold.
So the test with 32 MiB didn't hit it.
Pls don't add code just because a bot said so.

Here is what I mean. I didn't compile it:

diff --git a/tools/testing/selftests/bpf/prog_tests/arena_memcg.c b/tools/testing/selftests/bpf/prog_tests/arena_memcg.c
index c76a7eb..334bfa6 100644
--- a/tools/testing/selftests/bpf/prog_tests/arena_memcg.c
+++ b/tools/testing/selftests/bpf/prog_tests/arena_memcg.c
@@ -53,8 +53,12 @@ static int alloc_pagecache(size_t size)
fd = open(".", O_TMPFILE | O_RDWR | O_EXCL, 0600);
if (fd < 0)
return errno == EOPNOTSUPP ? -EOPNOTSUPP : -1;
- /* tmpfs hands out shmem pages, which are not reclaimable without swap */
- if (fstatfs(fd, &stfs) || stfs.f_type == TMPFS_MAGIC) {
+ /*
+ * tmpfs hands out shmem pages, which are not reclaimable without swap.
+ * overlayfs hides the type of its upper layer, which can be tmpfs.
+ */
+ if (fstatfs(fd, &stfs) || stfs.f_type == TMPFS_MAGIC ||
+ stfs.f_type == OVERLAYFS_SUPER_MAGIC) {
close(fd);
return -EOPNOTSUPP;
}
diff --git a/tools/testing/selftests/bpf/prog_tests/memcg_proactive_reclaim.c b/tools/testing/selftests/bpf/prog_tests/memcg_proactive_reclaim.c
index c22dcfa..2ba0791 100644
--- a/tools/testing/selftests/bpf/prog_tests/memcg_proactive_reclaim.c
+++ b/tools/testing/selftests/bpf/prog_tests/memcg_proactive_reclaim.c
@@ -30,7 +30,8 @@ struct reclaim_args {
* being available, and a read-only mount cannot host the file at all (EROFS
* applies to root too). /tmp is tmpfs on many systems, and test_progs is
* routinely run from a tmpfs working directory, so both candidates need the
- * check.
+ * check. overlayfs hides the type of its upper layer, which is tmpfs for /tmp
+ * in virtme-ng guests.
*/
static const char *workload_dir(void)
{
@@ -40,7 +41,8 @@ static const char *workload_dir(void)

for (i = 0; i < ARRAY_SIZE(dirs); i++)
if (!statfs(dirs[i], &st) && st.f_type != TMPFS_MAGIC &&
- st.f_type != RAMFS_MAGIC && !access(dirs[i], W_OK))
+ st.f_type != RAMFS_MAGIC &&
+ st.f_type != OVERLAYFS_SUPER_MAGIC && !access(dirs[i], W_OK))
return dirs[i];

return NULL;

pw-bot: cr

Hi Alexei,

What about two things: only check when the test fails, and check
KPF_SWAPBACKED instead of f_type?

1. Check only when the test fails.
What the test needs is "these pages are reclaimable", not "these pages
are not shmem": with swap available shmem reclaims fine, and then
reclaimed == 0 is a real regression that must not be skipped. So probe on
the failure path only -- nothing is paid when the test passes, there is
no polling, and a regression cannot be masked.

2. Probe with KPF_SWAPBACKED.
f_type is a blacklist, so the next stacking filesystem repeats this.
shmem-ness is a property of the pages rather than of the mount, so ask
the pages: /proc/self/pagemap for VA -> PFN, /proc/kpageflags for
PFN -> flags, then test bit 14.

    #define PAGEMAP_PRESENT(e)  (((e) & (1ull << 63)) != 0)
    #define PAGEMAP_PFN(e)      ((e) & ((1ull << 55) - 1))
    #define KPF_SWAPBACKED      (1ull << 14)

    /* 1 if fd is backed by shmem, 0 if not, -1 if it could not tell */
    static int file_is_shmem(int fd)
    {
            unsigned long ps = getpagesize();
            uint64_t ent = 0, flags = 0;
            int pfd = -1, kfd = -1, ret = -1;
            char *addr;

            addr = mmap(NULL, ps, PROT_READ, MAP_SHARED, fd, 0);
            if (addr == MAP_FAILED)
                    return -1;
            *(volatile char *)addr;

            pfd = open("/proc/self/pagemap", O_RDONLY);
            kfd = open("/proc/kpageflags", O_RDONLY);
            if (pfd < 0 || kfd < 0)
                    goto out;
            if (pread(pfd, &ent, sizeof(ent),
                      ((uintptr_t)addr / ps) * sizeof(ent)) != sizeof(ent))
                    goto out;
            /* unprivileged readers see PM_PRESENT with the PFN zeroed */
            if (!PAGEMAP_PRESENT(ent) || !PAGEMAP_PFN(ent))
                    goto out;
            if (pread(kfd, &flags, sizeof(flags),
                      PAGEMAP_PFN(ent) * sizeof(flags)) != sizeof(flags))
                    goto out;

            ret = !!(flags & KPF_SWAPBACKED);
    out:
            if (pfd >= 0)
                    close(pfd);
            if (kfd >= 0)
                    close(kfd);
            munmap(addr, ps);
            return ret;
    }

    if (!skel->bss->reclaimed) {
            struct sysinfo si;

            sysinfo(&si);
            if (!si.totalswap && file_is_shmem(data_fd) == 1) {
                    test__skip();
                    goto out;
            }
    }
    ASSERT_GT(skel->bss->reclaimed, 0, "reclaimed");

What it buys: it sees through any stacking filesystem instead of only the
listed ones, it is per-page rather than inferred from a magic number, it
needs no polling and no extra cgroup, and it costs nothing on the success
path. Returning -1 when it cannot tell keeps the caller conservative --
a failure beats a silent pass. Both files are root-only, which these tests
already are, and selftests/mm/vm_util.c already pairs pagemap with
kpageflags.

Best,
Hui