From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f49.google.com (mail-pj1-f49.google.com [209.85.216.49]) (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 3DDBE3EBF07 for ; Tue, 25 Aug 2026 09:20:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.49 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787649614; cv=none; b=MK43qdHP1tUanM1BhwqnhKvo1K2+83jv4KDfcSD9gGNONsjJ82OaQxajR1aI2tadqp4HR7Ee25HtBfUB4KUFU7Y41Q7Wws2K54IuqKhvaD7cwFbmYZEhsfHb84S//nxOZ6sIcIiLUBgy7nzOCUjfjXoM9NtQTd5fqhIKtWX77bk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787649614; c=relaxed/simple; bh=/yvTesosUitvxpBGbkVrq+T3ZwnrBfxMu1Tk0/tPvCI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=oPs0MaYot2Sm432gR2QugyWfOG/pQB7/HDoq4vkeuk/Hhypug2v2qI1mcnT5p2w/w3t9sxc5rHegZbD2t8kRTAsiK6ST/C5ijlYNEgfdQOmH34X1soZ6tfJmku3ti1XIK336gauCggVt/LgRrvdew2zmQ9L0ZeTuXpGh0ar/KRE= 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=V0iie5TW; arc=none smtp.client-ip=209.85.216.49 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="V0iie5TW" Received: by mail-pj1-f49.google.com with SMTP id 98e67ed59e1d1-3900e39d935so4927837a91.0 for ; Tue, 25 Aug 2026 02:20:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1787649612; x=1788254412; 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=0rBrebsEU++q5oyTmtoojXSPfykwosiXDRrVD5SURpU=; b=V0iie5TW7J64I2H/GdYFujiQlMqRe+ajXUvwGW2hSpngsp8mtQWakparn/E4JuDzxi PXZfwqVOBvggzCELl1nnjMXQw29EZMK65JKrx+e/aO+AANwfqa3j219qupcpU+fl6aH1 6OtIhJe+zwzKbJUNmjo5noMX7pXSoKm0nG2VI4YfQHL6dOwRpiP4yZBTUIZmMFDk5EHV +8R3HVjB4syDvKaOTNILBAWnYfMK7JiAD0d1kUzhaOD6OObIKZ2sHOG3YHGv9ucxAGd8 URe9tdLiIH3FJAJ9fCU7JNWfIXJ743JEfncyVDt8TOZxOKueIBFO4EoJo40GKSJotgGY 14RQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1787649612; x=1788254412; 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=0rBrebsEU++q5oyTmtoojXSPfykwosiXDRrVD5SURpU=; b=o3G8NjLiq20Rskwnw58VrOZzbyE+4Rldw7LAbyuKtRdpPIMHsMAzU6QtedFrYHQZyv J/0ag7y6xx1I8/QAH3MVtNfPLJdlmoWMfUKzN1zGdzjjB/00CPln2RA4/CnqJwWXA5Db xaZ1DHJXtVEc8nPXxZGANkvyQNtsKYBLxUJfmAnyWdiWsXGv0nEUAbGHkXa32U9J9oPU CUlkOWBtaGf3IOSrqt/G32sgjM6kqaN95RpsHCxRjOm7iYNVnfRc9zfDqysErIseWohV QjAW3S38yBnr4SoED06o+vHk01cpn/iuxEkfE+T0/1G+oEs4hIHteSWxiisn53CFKoh0 c1qg== X-Forwarded-Encrypted: i=1; AHgh+Rq4mTBHfVTY3uRE77AA042Ga/j6qHfEc9bYhUbg7tJIvtOzMF3zHIh9eP5koIfZyL5lSLBNGVrmVRqEUPU=@vger.kernel.org X-Gm-Message-State: AFuF++kzIvaXOdGHD7nuReEzwdR0t4jNupUiDUmKpyMeKv7g7MEKmMoV txFecjdngLX6GVtXd5Y1/lMTFrFi6MahWivrf6c/2H1IWIL/niJXjEla X-Gm-Gg: AR+sD11DvFdyiScINpFuxyq9FSM8KvbFdIvdJN3Rzd3TCOc3ozRccZVkZrDQJaNGw5i O9Z3ZdTYhDyOkjyCgAlgzPIeUNCADcpgmhzZkONX79L5Zc4DBUqCHCWfiUpGJn2FocbUytKxbC4 jugshYnZeZjI9NCOCZloECKPlr7NxSAnsJqgemEcsKwcZ21cIiicEHWuffFPNcdddsRL/RuLku0 Tsp51X9z90q8RmWHjeIYkuIJVr2WvuXyKiGVFD5ocVTUhMiXenmZkHLwCvNJg9xHeLeNf+tDlx6 FAG8px/z6plo3GWsLmfOdxEp4NfdoC2eDOhlE5gE4cDO/qKplqZNs7p82CUnN92LOH+G1Miv6Ib Ua2I/bQzWRnBgxSO1bzrU8PCG+UqmFXOeVuTEiop1yFqyyk0Xy910CF0KZvIKjooZH4ZhdNQXKd TZHOyeFqXmFKc7fWiaP1z8xCJwMeloBUPLEjUj72RNCGJAKOrxExFG1TSkRJaayhJlX2lUnqciL AEKZGzTXG7NI5Ivk2Ydq5yWC2Rwwg== X-Received: by 2002:a17:90b:580d:b0:38f:5869:387b with SMTP id 98e67ed59e1d1-395df24e757mr49635358a91.9.1787649612347; Tue, 25 Aug 2026 02:20:12 -0700 (PDT) Received: from localhost.localdomain ([103.120.31.178]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-3282afb407bsm5386163eec.30.2026.08.25.02.20.08 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 25 Aug 2026 02:20:11 -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:49:51 +0530 Message-ID: <20260825091952.81971-4-ahemadkhawar123@gmail.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260825091952.81971-1-ahemadkhawar123@gmail.com> References: <20260825091952.81971-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 Signed-off-by: Khawar Ahemad --- 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)