mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Emil Tsalapatis" <emil@etsalapatis.com>
To: "Jiayuan Chen" <jiayuan.chen@linux.dev>, <bpf@vger.kernel.org>
Cc: "Alexei Starovoitov" <ast@kernel.org>,
	"Daniel Borkmann" <daniel@iogearbox.net>,
	"Andrii Nakryiko" <andrii@kernel.org>,
	"Eduard Zingerman" <eddyz87@gmail.com>,
	"Kumar Kartikeya Dwivedi" <memxor@gmail.com>,
	"Martin KaFai Lau" <martin.lau@linux.dev>,
	"Song Liu" <song@kernel.org>,
	"Yonghong Song" <yonghong.song@linux.dev>,
	"Jiri Olsa" <jolsa@kernel.org>,
	"Emil Tsalapatis" <emil@etsalapatis.com>,
	"Ihor Solodrai" <ihor.solodrai@linux.dev>,
	"John Fastabend" <john.fastabend@gmail.com>,
	"Shuah Khan" <shuah@kernel.org>,
	"Sebastian Andrzej Siewior" <bigeasy@linutronix.de>,
	"Clark Williams" <clrkwllms@kernel.org>,
	"Steven Rostedt" <rostedt@goodmis.org>,
	<linux-kernel@vger.kernel.org>, <linux-kselftest@vger.kernel.org>,
	<linux-rt-devel@lists.linux.dev>
Subject: Re: [PATCH bpf-next v3 3/4] selftests/bpf: Add read_cgroup_file() to cgroup_helpers
Date: Tue, 18 Aug 2026 19:26:00 -0400	[thread overview]
Message-ID: <DKSGI6P7W52Z.3KO3SK2SG7EHG@etsalapatis.com> (raw)
In-Reply-To: <20260808140720.293604-4-jiayuan.chen@linux.dev>

On Sat Aug 8, 2026 at 10:05 AM EDT, Jiayuan Chen wrote:
> cgroup_helpers has write_cgroup_file()/write_cgroup_file_parent() but no
> read counterpart. Add read_cgroup_file() and read_cgroup_file_parent() so
> a forked child can read a cgroup file (e.g. memory.current) from the work
> dir owned by the parent that set the environment up, without hand-building
> the /mnt/... path.
>
> Signed-off-by: Jiayuan Chen <jiayuan.chen@linux.dev>

Reviewed-by: Emil Tsalapatis <emil@etsalapatis.com>

The "- 24" is questionable, but it's already all over the file. We can
clean it up separately.

> ---
>  tools/testing/selftests/bpf/cgroup_helpers.c | 67 ++++++++++++++++++++
>  tools/testing/selftests/bpf/cgroup_helpers.h |  4 ++
>  2 files changed, 71 insertions(+)
>
> diff --git a/tools/testing/selftests/bpf/cgroup_helpers.c b/tools/testing/selftests/bpf/cgroup_helpers.c
> index 45cd0b479fe3..4183ff6150c2 100644
> --- a/tools/testing/selftests/bpf/cgroup_helpers.c
> +++ b/tools/testing/selftests/bpf/cgroup_helpers.c
> @@ -188,6 +188,73 @@ int write_cgroup_file_parent(const char *relative_path, const char *file,
>  	return __write_cgroup_file(cgroup_path, file, buf);
>  }
>  
> +static int __read_cgroup_file(const char *cgroup_path, const char *file,
> +			      char *buf, size_t len)
> +{
> +	char file_path[PATH_MAX + 1];
> +	ssize_t got;
> +	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;
> +	}
> +
> +	got = read(fd, buf, len - 1);
> +	if (got < 0) {
> +		log_err("Reading %s", file_path);
> +		close(fd);
> +		return 1;
> +	}
> +	buf[got] = '\0';
> +	close(fd);
> +	return 0;
> +}
> +
> +/**
> + * 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 into, NUL-terminated on success
> + * @len: Size of @buf
> + *
> + * Read from a file in the given cgroup's directory.
> + *
> + * If successful, 0 is returned.
> + */
> +int read_cgroup_file(const char *relative_path, const char *file,
> +		     char *buf, size_t len)
> +{
> +	char cgroup_path[PATH_MAX - 24];
> +
> +	format_cgroup_path(cgroup_path, relative_path);
> +	return __read_cgroup_file(cgroup_path, file, buf, len);
> +}
> +
> +/**
> + * read_cgroup_file_parent() - Read from a cgroup file in the parent process
> + *                             workdir
> + * @relative_path: The cgroup path, relative to the parent process workdir
> + * @file: The name of the file in cgroupfs to read from
> + * @buf: Buffer to read into, NUL-terminated on success
> + * @len: Size of @buf
> + *
> + * Read from a file in the given cgroup's directory under the parent process
> + * workdir.
> + *
> + * If successful, 0 is returned.
> + */
> +int read_cgroup_file_parent(const char *relative_path, const char *file,
> +			    char *buf, size_t len)
> +{
> +	char cgroup_path[PATH_MAX - 24];
> +
> +	format_parent_cgroup_path(cgroup_path, relative_path);
> +	return __read_cgroup_file(cgroup_path, file, buf, len);
> +}
> +
>  /**
>   * setup_cgroup_environment() - Setup the cgroup environment
>   *
> diff --git a/tools/testing/selftests/bpf/cgroup_helpers.h b/tools/testing/selftests/bpf/cgroup_helpers.h
> index 3857304be874..d42d2e13044e 100644
> --- a/tools/testing/selftests/bpf/cgroup_helpers.h
> +++ b/tools/testing/selftests/bpf/cgroup_helpers.h
> @@ -15,6 +15,10 @@ 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,
>  			     const char *buf);
> +int read_cgroup_file(const char *relative_path, const char *file,
> +		     char *buf, size_t len);
> +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 get_root_cgroup(void);
>  int create_and_get_cgroup(const char *relative_path);


  reply	other threads:[~2026-08-18 23:26 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-08 14:05 [PATCH bpf-next v3 0/4] bpf: arena: handle memory.max on fault-in with reclaim/OOM Jiayuan Chen
2026-08-08 14:05 ` [PATCH bpf-next v3 1/4] bpf: Add a sleepable page allocator for map memory Jiayuan Chen
2026-08-08 14:05 ` [PATCH bpf-next v3 2/4] bpf: arena: allocate the fault-in page outside the lock Jiayuan Chen
2026-08-18 23:18   ` Emil Tsalapatis
2026-08-08 14:05 ` [PATCH bpf-next v3 3/4] selftests/bpf: Add read_cgroup_file() to cgroup_helpers Jiayuan Chen
2026-08-18 23:26   ` Emil Tsalapatis [this message]
2026-08-08 14:05 ` [PATCH bpf-next v3 4/4] selftests/bpf: Add a test for arena fault-in under memory.max Jiayuan Chen
2026-08-19  0:51   ` Emil Tsalapatis

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=DKSGI6P7W52Z.3KO3SK2SG7EHG@etsalapatis.com \
    --to=emil@etsalapatis.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bigeasy@linutronix.de \
    --cc=bpf@vger.kernel.org \
    --cc=clrkwllms@kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=ihor.solodrai@linux.dev \
    --cc=jiayuan.chen@linux.dev \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-rt-devel@lists.linux.dev \
    --cc=martin.lau@linux.dev \
    --cc=memxor@gmail.com \
    --cc=rostedt@goodmis.org \
    --cc=shuah@kernel.org \
    --cc=song@kernel.org \
    --cc=yonghong.song@linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®