From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f51.google.com (mail-pj1-f51.google.com [209.85.216.51]) (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 61D80221D96 for ; Tue, 18 Aug 2026 00:24:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.51 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787012697; cv=none; b=ghf//GjzLGkBBy+BiNB/WubPxjqiEPjqe7UUP0Ezv4SOwKS50vpFyJavM3Y1vgK4tE0Iem4C8Z7NmgDav2VlCYS81viMugdmoH8yOeMtCKNT58fkorLHhD5svHU53xh1a6M2UkfLcYGGXsdktfLY9V54rX8/YykBLTPLMDb/hmU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787012697; c=relaxed/simple; bh=qWRqvfmnNty80NoawIZpg8GiTpU1j9V039JR3Hleqg0=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=HqWKqsTa+7oBT0BTPN7RS0l045ajeytDTqPRWD9VWK5w7VSflAQqXU9/3JipRNd/IJnNVucnsIOJFqPNNeSNEOj3o75Ednxk0BWZbv9PWgJi0HyFpxJ3HTAV8sDf2SNm+b/V4pxDVK+P+ZIE6zCDFX0Jq48GNHi0Yy+YGCkTzQQ= 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=HXSrOccU; arc=none smtp.client-ip=209.85.216.51 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="HXSrOccU" Received: by mail-pj1-f51.google.com with SMTP id 98e67ed59e1d1-382ef647e20so3868960a91.1 for ; Mon, 17 Aug 2026 17:24:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1787012694; x=1787617494; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=XQzhuTEmSzFVRktqX1xn6ptqrzbe2xgge0zZzqfRxOc=; b=HXSrOccUFFHs/y8UgtdBxzzK6rYbzWVrbVupWEqVQP7QD+L1+5JMUb4bDRJIznS0OJ WW/yPAkPwh864IwLN3KS2ygMcnptY5b90KTyJjRf4mZeFKaIJU2pUWwDlmP515UKpxBC j9qOFX/JP/cLIdAI2xLTCajl1IpfF/gU0q6cfGgWrYmr1Xhc+gB4al54DImdU2g19xy+ yh0yLJAR+LYlyoaq4RQrnJqheZFBt1527McScmewJpxrhdVXaqR5bKvLLsLggcX4pFNd xErBq9eUsdnKsTQBe7AZSOkGey/+pCIHxhniibUG6u5lNRW+8WusdUn6dxCgLI1wSkmq 8siQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1787012694; x=1787617494; h=content-transfer-encoding:mime-version: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=XQzhuTEmSzFVRktqX1xn6ptqrzbe2xgge0zZzqfRxOc=; b=KgsfI+R03IwWFR+3+wXS5KUxzVparCAVi9w8DTWMSBI+yxZgR1t1VXPbicuIb01T0h JI2if7e8GznPYKuXa7iCPCxnuSeSNt/jRvYch1YNGeU8IiwuLZ/dajYh2LY6nsXOMfNx p+jGgA0mEfVIqWuWDJ0WZiFErnTC+yp5eEK2J35wtuGV1PySCbxDojI4agtWHBa3NcV7 lbab0CiKmXf26i6BNVPEzEVBlSxl/3WwM+gFSCTrnYRbZx3sApVGblF7tAR7BovNlSrI yrin4TDM+s4694gjnKH8eYc3pKcZSvf0ipQJHf1+lBeJksl+77vzWEQ4CpxxAnle3Ka8 g0eg== X-Forwarded-Encrypted: i=1; AHgh+RqiKPdvR0n+J9ggwTE7l4afxmyQGhdS4gAldOFGRpZR76bucoTgWiwB8NOSu9KoxqqCs4Q6j63dgrcWBbo=@vger.kernel.org X-Gm-Message-State: AOJu0YwKB0sqP6KZdRc3VwdRJwY+EyITeKGut4kDY9JL11CPoS/Pa3be 61XyUKWbt2z2ZvbVSJaaiRezD+AIPIa4Mas1Uw5eNIkDYNeLRuwy9nEo X-Gm-Gg: AR+sD137w9gFITpAamiyuPJgtpGhHY7x+ktEcpMdbwO50FKTLS3/w60J8uTunFJG7jC GqTSwYsezbfI0JIuKylpr7xf0UjH/4CbmfMqhJ1ZBYbx9j6oJc6uAWh2G3KxAG4cJ+f/j//nScl gVTVFQIpuxwSSU+o98mz19QnMhtPCjyLdgvyYwpv7S7pCODkXxZ1YQegX7UpzjAgn3l9RzG79FY 6ZSAtekMoK6jCMY7/m8wCHOSBnPGz3ONx60VxJRWmdaBncfMC1qf4D7aYKqnWJ7IlLCoQ0IXBRC SSEYhwoeez+tL76bt/7+nLRGcLrxFgnaH9qNX5ec4Qs3YzFfxZee5av0war34dNVhTF+xpFYMch TRwBI1RYcuJJ7bgnDE5wZjmlNYS+Whyew+xNSMBkqqUykR9X6DyEf4NEHo+picaBqT4kKGDNL+u QnDSaseaRiv761mqONYGYQMd/oZc2T0RE+ZSnXNl27tfHNVemtFxR6IfA= X-Received: by 2002:a17:90b:2c8d:b0:38f:caa2:5721 with SMTP id 98e67ed59e1d1-3933e62f4f8mr29637987a91.17.1787012693684; Mon, 17 Aug 2026 17:24:53 -0700 (PDT) Received: from localhost ([2a03:2880:9ff:67::]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-32678dcedf4sm10261905eec.5.2026.08.17.17.24.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Aug 2026 17:24:53 -0700 (PDT) From: Ziyang Men To: Tejun Heo , Johannes Weiner , =?UTF-8?q?Michal=20Koutn=C3=BD?= , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Eduard Zingerman , Kumar Kartikeya Dwivedi , Shuah Khan , kernel-team@meta.com Cc: Ingo Molnar , Peter Zijlstra , Vincent Guittot , Ben Segall , Dietmar Eggemann , Martin KaFai Lau , Song Liu , Yonghong Song , Jiri Olsa , Emil Tsalapatis , Roman Gushchin , Shakeel Butt , JP Kobryn , Ziyang Men , bpf@vger.kernel.org, cgroups@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 0/2] cgroup: expose cpu.stat to BPF Date: Mon, 17 Aug 2026 17:24:48 -0700 Message-ID: <20260818002450.3071325-1-ziyang.meme@gmail.com> X-Mailer: git-send-email 2.53.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit As the title suggests, this series adds bpf kfuncs for the cgroup CPU controller. Motivation: Collecting cgroup statistics is expensive: the existing method is to open and parse a cgroup file. memcg already has an efficient alternative through BPF; this series extends that idea to cpu. Design: - Add bpf_css_flush_rstat() and bpf_cgroup_base_stat() to cgroup rstat. The second kfunc returns the raw cgroup_base_stat after the same cputime adjustment used by cpu.stat. - Leave reading the CFS bandwidth counters to the BPF program. They are plain fields of tg->cfs_bandwidth, so they need no kernel code. - Add bpf_css_to_task_group(), which returns a checked RCU pointer of the task_group*. This is usefule to compute the throttled time in bpf side since the bpf_per_cpu_ptr() requires a verifier-known struct task_group * and a pointer carrying the MEM_PERCPU property. This use the convention that task_group embeds its css at offset zero, so no scheduler helper or scheduler source change is needed. === Changes since v1: - Make the rstat flush generic, move it to rstat.c, and take a css. - Return cgroup_base_stat directly instead of repacking its CPU times. - Replace the throttled-time aggregation kfunc with a checked RCU cast in the CPU cgroup BPF code. The cast preserves the verifier type needed for the BPF program to perform the per-CPU sum itself. - Remove the changes in the scheduler part. Tested on VM with v7.2-rc5. Ziyang Men (2): cgroup: add BPF kfuncs to read a cpu cgroup's stats selftests/bpf: add cgroup_iter_cpu test for cpu cgroup kfuncs kernel/cgroup/Makefile | 2 + kernel/cgroup/bpf_cpu.c | 61 +++++ kernel/cgroup/rstat.c | 59 +++- tools/testing/selftests/bpf/cgroup_iter_cpu.h | 22 ++ tools/testing/selftests/bpf/config | 3 + .../bpf/prog_tests/cgroup_iter_cpu.c | 259 ++++++++++++++++++ .../selftests/bpf/progs/cgroup_iter_cpu.c | 113 ++++++++ 7 files changed, 517 insertions(+), 2 deletions(-) create mode 100644 kernel/cgroup/bpf_cpu.c create mode 100644 tools/testing/selftests/bpf/cgroup_iter_cpu.h create mode 100644 tools/testing/selftests/bpf/prog_tests/cgroup_iter_cpu.c create mode 100644 tools/testing/selftests/bpf/progs/cgroup_iter_cpu.c -- 2.53.0-Meta