From: Brahmajit Das <[email protected]> arena_memcg has a static alloc_pagecache() that fills the page cache with clean, reclaimable pages by reading a sparse O_TMPFILE. The next patch needs the same helper for the memcg kfunc tests.
Move it to cgroup_helpers.c, which test_progs already links, so both tests share one copy and fixes to the reclaimability detection only have to be made once. No functional change. Assisted-by: Claude:claude-opus-5-5 Signed-off-by: Brahmajit Das <[email protected]> --- tools/testing/selftests/bpf/cgroup_helpers.c | 34 +++++++++++++++++++ tools/testing/selftests/bpf/cgroup_helpers.h | 1 + .../selftests/bpf/prog_tests/arena_memcg.c | 34 ------------------- 3 files changed, 35 insertions(+), 34 deletions(-) diff --git a/tools/testing/selftests/bpf/cgroup_helpers.c b/tools/testing/selftests/bpf/cgroup_helpers.c index 4183ff6150c2..ace15b9cd2e4 100644 --- a/tools/testing/selftests/bpf/cgroup_helpers.c +++ b/tools/testing/selftests/bpf/cgroup_helpers.c @@ -5,6 +5,8 @@ #include <sys/stat.h> #include <sys/types.h> #include <sys/xattr.h> +#include <sys/vfs.h> +#include <linux/magic.h> #include <linux/limits.h> #include <stdio.h> #include <stdlib.h> @@ -613,6 +615,38 @@ int cgroup_setup_and_join(const char *path) { return cg_fd; } +/* + * Fill the page cache with @size bytes of clean, reclaimable pages by + * reading a sparse temp file, the way the cgroup selftests do. Returns the + * fd, which must stay open: closing it drops the cache. Returns -EOPNOTSUPP + * if the working directory cannot back such a file, -1 on error. + */ +int alloc_pagecache(size_t size) +{ + struct statfs stfs; + char buf[4096]; + size_t off; + int fd; + + 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) { + close(fd); + return -EOPNOTSUPP; + } + if (ftruncate(fd, size)) + goto err; + for (off = 0; off < size; off += sizeof(buf)) + if (read(fd, buf, sizeof(buf)) < 0) + goto err; + return fd; +err: + close(fd); + return -1; +} + /** * setup_classid_environment() - Setup the cgroupv1 net_cls environment * diff --git a/tools/testing/selftests/bpf/cgroup_helpers.h b/tools/testing/selftests/bpf/cgroup_helpers.h index d42d2e13044e..6cc5ccb63fc2 100644 --- a/tools/testing/selftests/bpf/cgroup_helpers.h +++ b/tools/testing/selftests/bpf/cgroup_helpers.h @@ -20,6 +20,7 @@ int read_cgroup_file(const char *relative_path, const char *file, int read_cgroup_file_parent(const char *relative_path, const char *file, char *buf, size_t len); int cgroup_setup_and_join(const char *relative_path); +int alloc_pagecache(size_t size); int get_root_cgroup(void); int create_and_get_cgroup(const char *relative_path); void remove_cgroup(const char *relative_path); diff --git a/tools/testing/selftests/bpf/prog_tests/arena_memcg.c b/tools/testing/selftests/bpf/prog_tests/arena_memcg.c index c76a7eb2f01d..55c8db956ff3 100644 --- a/tools/testing/selftests/bpf/prog_tests/arena_memcg.c +++ b/tools/testing/selftests/bpf/prog_tests/arena_memcg.c @@ -2,8 +2,6 @@ #define _GNU_SOURCE #include <test_progs.h> #include <fcntl.h> -#include <linux/magic.h> -#include <sys/vfs.h> #include <sys/wait.h> #include <unistd.h> #include <sys/user.h> @@ -37,38 +35,6 @@ static void dump_memcg(void) fprintf(stderr, "memory.events:\n%s", buf); } -/* - * Fill the page cache with @size bytes of clean, reclaimable pages by - * reading a sparse temp file, the way the cgroup selftests do. Returns the - * fd, which must stay open: closing it drops the cache. Returns -EOPNOTSUPP - * if the working directory cannot back such a file, -1 on error. - */ -static int alloc_pagecache(size_t size) -{ - struct statfs stfs; - char buf[4096]; - size_t off; - int fd; - - 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) { - close(fd); - return -EOPNOTSUPP; - } - if (ftruncate(fd, size)) - goto err; - for (off = 0; off < size; off += sizeof(buf)) - if (read(fd, buf, sizeof(buf)) < 0) - goto err; - return fd; -err: - close(fd); - return -1; -} - void serial_test_arena_memcg(void) { int cgroup_fd = -1, status, err; -- 2.56.0

