From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) (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 99DF83F39D8 for ; Tue, 25 Aug 2026 09:50:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.179 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787651419; cv=none; b=nbdoO0qMlCQJABfjiz2NtjY51+kny+Vz0mr2Mq+lYZ1YmeqehLgiZ05OcfofCOmp33+WblXlPZsAf3YGma9GwcYyPIcY5tb/j0XsUwZUz4550wrTx2tq+eTX14h8zR2X+SzYbZz9iAMNmfeDXbHPTVR7mns1xVG5r5F8WThKIC0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787651419; c=relaxed/simple; bh=/yvTesosUitvxpBGbkVrq+T3ZwnrBfxMu1Tk0/tPvCI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=aYtyWXF1v0cDJ1sEavNQke2iJgefFq8trtFPHMDJUZPh++lhMCRMxRLx8GzUJKva7QSpCGpa89Nugh+gfNmpJzUs/cyZie57YgJOgvTu1Cd36Z0FSzpJR10FRBgJ6+ppis4mqOWaWtHObfVUUsiFZO3w5yTAd1LUmW2nPnvwA3E= 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=CngOv4bz; arc=none smtp.client-ip=209.85.210.179 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="CngOv4bz" Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-84fa3b14ee1so3613228b3a.0 for ; Tue, 25 Aug 2026 02:50:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1787651417; x=1788256217; 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=CngOv4bzYh+25ozMucU3E6MIUefWURDeHhjBbjDsIW4AimcgCwmuboDqdUbU9p3M3k 7/Boc0MPIbzZwAAyo/j7VAeGsGncu5xwleC6zY9zCVdSoSgFVNi3RrWr7O6IdbVgkmC/ xhKI67Yb4vI6K0eiBFqo0YkmQTkCA/8ulIpLpMKLb2Iet4InUKfyzNfNMRJvbhCcxLvX qQJV/Q0NqlvxDQy5cPlTAAZ4G4F7aYlaNlIxeeawtpEMUjanvr97QsvLEryK4r93cMkh b/RyC4VSERs9xfk64vPm5QZPwHKickM1q3ArX+AG/gIOdZgsRLFZ+tDg3/JxX9cATL7i bi+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1787651417; x=1788256217; 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=FWNqd71si4t4Wzh+s485L6s8U58OxXqTRcJfhY71br0v3t/Ok0qUATdi00FGhmo4zJ 9inbV/WcY9/LcifM1FlgJJI87WdpZXBrGqaEVHSJ3Ac1PofiEdcZ1VRXM0pWZWX72lpG YZ2mNELo2SDqBURUH4BphiOTuBP27FUzLL5EYsCZsblO+d+qE9oApXNou57X+ZVJaqE9 dVPZtA2eM7uRMmzmYkpBQNz5nhbkJckUIwzuJFWl/sV6ZoHCHNViGhCZmiilnF16IdbD nv/5gvDplWTDlzwVU3Sx5uHaOYvU4Q1PUifmNDgmP1yqp/S1X7lr8/Oas2S+90d0HmPV hVVA== X-Gm-Message-State: AFuF++kQ5NHt5r2/4vndM8I01nH4sA1X+QLUlP1EzcF/kh0aosHiX+Yb XArB2vLZjIBF7wonjdF4l2toIgWyNrw3gqviVEVI5S3qgEg+K+YzXutQ X-Gm-Gg: AR+sD10I8aGStvit8e8r/LRC/1HXSVGx16kT6yo2gBY92MLtffjM61J3MNQAF3xgslx fM1tfBQIL42XbxFzUyniELWrBm6z7jAoLpvCZU/Ew+tCWMdaq7ZbEow/WxoAeO54lBb84goNki3 TidPeC6q46/ftEuKllygfTCvZWYeCR84U9ITq9YRz8XQzbnfeoRUrwSxIqQmsrdmssWuF0WJAUd OIfpkk09F/hLfZBCj4tNtwzOnsTQ2MyNkk2smyptf0T6tkBEJSUew6BkjohrhX9+5qCP9YdIobf cA0l/xVIOJDXECJi6/flw7w7JfQwmmBT/aaKVWtYfvpL7uwSKBOyrOIRiMleEPWK3ZkgZPukFss BAVIxUT4AYxMOMlNxix74595omg4V2tpymNDIOasL2WSfpdP8yn7RSW3EbDfhXS8eatBwz5ZeV0 CDGk0eujvSR4n4gOKR75W0nNxOHpPy/9UhByz+S98yrzAzw7J6Yw8jEx0MQdXXNtxMZrY0TYyKh E1aihbbEs3AEGi0kGJcVEJM X-Received: by 2002:a05:6a21:138f:b0:3c1:fbf:1e2e with SMTP id adf61e73a8af0-3cd3008bf4bmr56836649637.10.1787651416876; Tue, 25 Aug 2026 02:50:16 -0700 (PDT) Received: from localhost.localdomain ([103.120.31.178]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-32827662702sm5923062eec.10.2026.08.25.02.50.12 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 25 Aug 2026 02:50:16 -0700 (PDT) From: Khawar Ahemad To: bpf@vger.kernel.org Cc: linux-kernel@vger.kernel.org, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, eddyz87@gmail.com, jiayuan.chen@linux.dev, emil@etsalapatis.com, ahemadkhawar123@gmail.com Subject: [PATCH bpf-next v6 3/4] selftests/bpf: Add read_cgroup_file() to cgroup_helpers Date: Tue, 25 Aug 2026 15:19:54 +0530 Message-ID: <20260825094955.83240-4-ahemadkhawar123@gmail.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260825094955.83240-1-ahemadkhawar123@gmail.com> References: <20260825094955.83240-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)