From nobody Mon Feb 9 07:21:19 2026 Received: from out-182.mta1.migadu.com (out-182.mta1.migadu.com [95.215.58.182]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 584DD1D7985; Mon, 28 Apr 2025 03:36:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745811418; cv=none; b=NrCOoNxB6mfuTEGXE1uDzDHPb4aQKkPSkKltnrZ+w1FMg7NGTpndwqCPoAonsuUPbZ9kIHVH/58hzBiVRRp/NtHNaJHV47lp9IJjAOS+bxF6XfwCe5xXckdj4zRjQn/dmBKtwH+BC6cZsXx4RSEEGDjiVpJ0/SVDVCL28jme4A8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745811418; c=relaxed/simple; bh=p9u1+q/UAqQxfKSlhOraLhTIMrQENFQhj5zOREccz9c=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=eWgjsK43ip9QRu3XB2CrYzgAEXvQIrc+iS+QXuPtSnRnruVFpXb1y+0III8L1VZqT1Lug8VixCwZywpEwaMQCLtR745OpFWsSLCOGhaotJsDbwRca/4u4gyU4GeJlxuKtjaknS5CZt9eLv8EV0TEcXkY5hNQwYv48uHqNDm0ZQY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=lrtnBPLN; arc=none smtp.client-ip=95.215.58.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="lrtnBPLN" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1745811414; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=32V32g0Ec/6L0UXIreJ3hArx8mY8wFKy2VxIfcAawIw=; b=lrtnBPLNXgwRxE1ROADeMOVv/joeCnNWdE2cgUBZ3JDih4j+ujB3VSZxNkwMdMJuK4gH9H nRnU9Y8dG1fSA0AJ5otgKGfEDeJpapwRWBeLLpLMP9GfKHOHKBZoFx3AGGmjOq4+i9Owtl PE1H4gahuHDezIQUEP649NIu+NInKaY= From: Roman Gushchin To: linux-kernel@vger.kernel.org Cc: Andrew Morton , Alexei Starovoitov , Johannes Weiner , Michal Hocko , Shakeel Butt , Suren Baghdasaryan , David Rientjes , Josh Don , Chuyi Zhou , cgroups@vger.kernel.org, linux-mm@kvack.org, bpf@vger.kernel.org, Roman Gushchin Subject: [PATCH rfc 07/12] bpf: selftests: introduce read_cgroup_file() helper Date: Mon, 28 Apr 2025 03:36:12 +0000 Message-ID: <20250428033617.3797686-8-roman.gushchin@linux.dev> In-Reply-To: <20250428033617.3797686-1-roman.gushchin@linux.dev> References: <20250428033617.3797686-1-roman.gushchin@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" Implement read_cgroup_file() helper to read from cgroup control files, e.g. statistics. Signed-off-by: Roman Gushchin --- tools/testing/selftests/bpf/cgroup_helpers.c | 39 ++++++++++++++++++++ tools/testing/selftests/bpf/cgroup_helpers.h | 2 + 2 files changed, 41 insertions(+) diff --git a/tools/testing/selftests/bpf/cgroup_helpers.c b/tools/testing/s= elftests/bpf/cgroup_helpers.c index e4535451322e..3ffd4b764f91 100644 --- a/tools/testing/selftests/bpf/cgroup_helpers.c +++ b/tools/testing/selftests/bpf/cgroup_helpers.c @@ -125,6 +125,45 @@ int enable_controllers(const char *relative_path, cons= t char *controllers) return __enable_controllers(cgroup_path, controllers); } =20 +static size_t __read_cgroup_file(const char *cgroup_path, const char *file, + char *buf, size_t size) +{ + char file_path[PATH_MAX + 1]; + size_t ret; + int fd; + + snprintf(file_path, sizeof(file_path), "%s/%s", cgroup_path, file); + fd =3D open(file_path, O_RDONLY); + if (fd < 0) { + log_err("Opening %s", file_path); + return -1; + } + + ret =3D read(fd, buf, size); + close(fd); + return ret; +} + +/** + * read_cgroup_file() - Read to a cgroup file + * @relative_path: The cgroup path, relative to the workdir + * @file: The name of the file in cgroupfs to read to + * @buf: Buffer to read from the file + * @size: Size of the buffer + * + * Read to a file in the given cgroup's directory. + * + * If successful, the number of read bytes is returned. + */ +size_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/s= elftests/bpf/cgroup_helpers.h index 502845160d88..821cb76db1f7 100644 --- a/tools/testing/selftests/bpf/cgroup_helpers.h +++ b/tools/testing/selftests/bpf/cgroup_helpers.h @@ -11,6 +11,8 @@ =20 /* cgroupv2 related */ int enable_controllers(const char *relative_path, const char *controllers); +size_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, --=20 2.49.0.901.g37484f566f-goog