From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pg1-f170.google.com (mail-pg1-f170.google.com [209.85.215.170]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BB30A3EDE42 for ; Tue, 25 Aug 2026 09:17:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.170 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787649429; cv=none; b=UIpZhrbBmIn8ElUoKt+RwbBq8fP+zZz/ao0KqSh3vyetwwt0palB5x8oTFv3/lNN4UTEjqRFcFS3Axf+UivZFT3ieuBnOjtMgJSmWMrRkw1mTMd3ZPnKZXhq1S7TzgPPNz6LYyy1aNHnMQlRTCOt+Aj3myubCuAzRXgfRUIHQ1o= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787649429; c=relaxed/simple; bh=6Z6Q5pPOjFxZCt8cI3vyHstO1u6wtP4+biQ04XHKri8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=pa/eSdZBQ1/d5oB0t5uRxOOFN4g2eNrU1LjAHFyPd3v7LVqEY7dv4aS0PFqIfraQ1Z9zJJ16U8030c3tV1jqy/X6LsxBmWkqibHRT7iltCeQ9hp6s0FTpR8M5XbjYE3ijdcK8VpBv+l/Fe1ejf7/r9OBiXmAzGuziao/suJEv4s= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=r4XifViY; arc=none smtp.client-ip=209.85.215.170 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="r4XifViY" Received: by mail-pg1-f170.google.com with SMTP id 41be03b00d2f7-ca766c1c9ccso3095143a12.0 for ; Tue, 25 Aug 2026 02:17:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1787649427; x=1788254227; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=l6cOQXsmgqLviVD25X5rJGEEdQ3Qp9SiwjwLR9fqyds=; b=r4XifViYSBAFRrpivNFWKZwelwO0otOgqGxn06YSeysNa1uTPXk5OGBD2lmqzyctXr QrF0uyjdHOIDn92crpeHMTYbCXpbBIWdPZiMYffMOYewQnyIetmVYUhhQVvjTCTmIcb3 RcvIE4KkuHRd5Ctlh8rErgz+w+lAwGlgUc/fU2Zb/DIVGbhCuFfiHYrIkZUQjlmpwxQ6 H7KBzlw6yMoxspMmuEMcuzJvb8QLsCjBbZgWuET/GjX2ZIPTX32AIp1kWWKiQf+6h+iX p6QSVf5mEB4FrbjoAB0wjI/9uvlfbM7VNtzicaIrw3OU3hv5sP3GUX7oAzgLLE/1kJBT EtTg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1787649427; x=1788254227; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=l6cOQXsmgqLviVD25X5rJGEEdQ3Qp9SiwjwLR9fqyds=; b=HuOeDj/UGjpq5Uw8MiCtXZf0HYWm3fQwxqoTiDjgeI4y6XKQpPz3EuPx2EDqmjF47C 00zd8F4v1odbkd9VQ/bkMswxQ4kK4FjPsuyONTfYzaZazN03ZSME+jTVqirSioaXggWj 52bpbjSCqf/frrK4mzWoduDRHnLqwPM6rm0aMmTlkgryutd6X3eIH/aKlUowuDLzThds sbu5m4QgcKc5IijrwML1dhN+FsQZ2os1sPo2nByqQdP1f/DZG+ofHoMqhnOF4itvWI7j /F1jNLuXJbHhP4f4Fu26a1BAslES0z+6UddqTUqh+v11VK9MleiE4gl9Ho5FEqDvu74S j1dQ== X-Forwarded-Encrypted: i=1; AHgh+RoNIEo3KyY2fNmY2Zw2PgA6mKBOXxw9cjTwKQv4UQX4woP3pj8wBnksl0L7LToqjkZqtSRw193vpwvnmYM=@vger.kernel.org X-Gm-Message-State: AFuF++kRg7zKobYLphhX/02ugSCpeAI1vh0/pBXiGXL3sDVYhtMlC9UD XWmeiTuAP2YZcQUi+oq5pQGna1gXmxXQL/nS4PL1KM15J6+lTJCUG6J0 X-Gm-Gg: AR+sD10MRM3KGDEQxfMTbnoDfjLv9ekstRqbvKOw6dElpCeGe3vF58jaXXmiKum+Pdm ZjioyBcDL9npQzzNfg/3H6IJ9j8OxPqqgS4F2YbBsGlaG1WYK5Mi8lpZl8b9U/15cyn9fK0MYir LnCKYeSWCwFOcxHe+51XeGq9I1B2JvOEJf3tuIrFASQD+RolrvhU3l9lKVunTTsQY5SoReQkOrh 3FNpeex911WIXAMqnC2cFvD6y/07UQBb6HAGuMaNvphYbRtlGEVtY29nwCwj/od9uGxDOB+dcY4 0Xm9+r0oro7JqxCeoChsGXsClVCjRXrZGQ4WUGrA5tlRRDYVvTrzSJDCapTlaltKrhfP3RBrpal qM5GLBfC2Xc7gBbGQu1H2xiFzz9L/lWrN+FomWgCHqiSsbXrgJbWKsOGfecS3isQbVLsdd2PFyo dN4eQNBOgnqeuBWEb+gRyRmsncduP3Ixh/h2hDu6kQz6Ninl3TNMiM6Vt5NA6GFT/ABtXaAPzoR XQqw7EAn2ma1YaEnchaS/Vn1XWxWw== X-Received: by 2002:a05:6a21:9214:b0:3cc:f280:ebc6 with SMTP id adf61e73a8af0-3cd4ba4839amr55998320637.5.1787649426894; Tue, 25 Aug 2026 02:17:06 -0700 (PDT) Received: from localhost.localdomain ([103.120.31.178]) by smtp.gmail.com with ESMTPSA id a92af1059eb24-141860fe08csm52122658c88.8.2026.08.25.02.17.03 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 25 Aug 2026 02:17:06 -0700 (PDT) From: Khawar Ahemad To: bpf@vger.kernel.org, linux-kernel@vger.kernel.org Cc: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, eddyz87@gmail.com, jiayuan.chen@linux.dev, emil@etsalapatis.com Subject: [PATCH v6 3/4] selftests/bpf: Add read_cgroup_file() to cgroup_helpers Date: Tue, 25 Aug 2026 14:46:46 +0530 Message-ID: <20260825091647.81632-4-ahemadkhawar123@gmail.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260825091647.81632-1-ahemadkhawar123@gmail.com> References: <20260825091647.81632-1-ahemadkhawar123@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Jiayuan Chen 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. Reviewed-by: Emil Tsalapatis Signed-off-by: Jiayuan Chen --- 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 45cd0b479f..4183ff6150 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 3857304be8..d42d2e1304 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); -- 2.54.0 (Apple Git-157)