[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20260127024421.494929-12-roman.gushchin@linux.dev>
Date: Mon, 26 Jan 2026 18:44:14 -0800
From: Roman Gushchin <roman.gushchin@...ux.dev>
To: bpf@...r.kernel.org
Cc: Michal Hocko <mhocko@...e.com>,
Alexei Starovoitov <ast@...nel.org>,
Matt Bobrowski <mattbobrowski@...gle.com>,
Shakeel Butt <shakeel.butt@...ux.dev>,
JP Kobryn <inwardvessel@...il.com>,
linux-kernel@...r.kernel.org,
linux-mm@...ck.org,
Suren Baghdasaryan <surenb@...gle.com>,
Johannes Weiner <hannes@...xchg.org>,
Andrew Morton <akpm@...ux-foundation.org>,
Roman Gushchin <roman.gushchin@...ux.dev>
Subject: [PATCH bpf-next v3 11/17] bpf: selftests: introduce read_cgroup_file() helper
Implement read_cgroup_file() helper to read from cgroup control files,
e.g. statistics.
Signed-off-by: Roman Gushchin <roman.gushchin@...ux.dev>
---
tools/testing/selftests/bpf/cgroup_helpers.c | 45 ++++++++++++++++++++
tools/testing/selftests/bpf/cgroup_helpers.h | 3 ++
2 files changed, 48 insertions(+)
diff --git a/tools/testing/selftests/bpf/cgroup_helpers.c b/tools/testing/selftests/bpf/cgroup_helpers.c
index 20cede4db3ce..fc5f22409ce5 100644
--- a/tools/testing/selftests/bpf/cgroup_helpers.c
+++ b/tools/testing/selftests/bpf/cgroup_helpers.c
@@ -126,6 +126,51 @@ int enable_controllers(const char *relative_path, const char *controllers)
return __enable_controllers(cgroup_path, controllers);
}
+static ssize_t __read_cgroup_file(const char *cgroup_path, const char *file,
+ char *buf, size_t size)
+{
+ char file_path[PATH_MAX + 1];
+ ssize_t ret;
+ int fd;
+
+ snprintf(file_path, sizeof(file_path), "%s/%s", cgroup_path, file);
+ fd = open(file_path, O_RDONLY);
+ if (fd < 0) {
+ log_err("Opening %s", file_path);
+ return -1;
+ }
+
+ ret = read(fd, buf, size);
+ if (ret < 0) {
+ close(fd);
+ log_err("Reading %s", file_path);
+ return -1;
+ }
+
+ close(fd);
+ return ret;
+}
+
+/**
+ * read_cgroup_file() - Read from a cgroup file
+ * @relative_path: The cgroup path, relative to the workdir
+ * @file: The name of the file in cgroupfs to read from
+ * @buf: Buffer to read from the file
+ * @size: Size of the buffer
+ *
+ * Read from a file in the given cgroup's directory.
+ *
+ * If successful, the number of read bytes is returned.
+ */
+ssize_t read_cgroup_file(const char *relative_path, const char *file,
+ char *buf, size_t size)
+{
+ char cgroup_path[PATH_MAX - 24];
+
+ format_cgroup_path(cgroup_path, relative_path);
+ return __read_cgroup_file(cgroup_path, file, buf, size);
+}
+
static int __write_cgroup_file(const char *cgroup_path, const char *file,
const char *buf)
{
diff --git a/tools/testing/selftests/bpf/cgroup_helpers.h b/tools/testing/selftests/bpf/cgroup_helpers.h
index 3857304be874..66a08b64838b 100644
--- a/tools/testing/selftests/bpf/cgroup_helpers.h
+++ b/tools/testing/selftests/bpf/cgroup_helpers.h
@@ -4,6 +4,7 @@
#include <errno.h>
#include <string.h>
+#include <sys/types.h>
#define clean_errno() (errno == 0 ? "None" : strerror(errno))
#define log_err(MSG, ...) fprintf(stderr, "(%s:%d: errno: %s) " MSG "\n", \
@@ -11,6 +12,8 @@
/* cgroupv2 related */
int enable_controllers(const char *relative_path, const char *controllers);
+ssize_t read_cgroup_file(const char *relative_path, const char *file,
+ char *buf, size_t size);
int write_cgroup_file(const char *relative_path, const char *file,
const char *buf);
int write_cgroup_file_parent(const char *relative_path, const char *file,
--
2.52.0
Powered by blists - more mailing lists