[PATCH bpf-next v4 07/17] sched/psi: add cgroup_id field to psi_group structure

From: Ben Gardon

Date: Wed Dec 02 2026 - 16:05:13 EST


From: Roman Gushchin <roman.gushchin@xxxxxxxxx>

Allow a BPF program attached to a PSI tracepoint to identify the associated
memory cgroup, store cgroup_id inside struct psi_group.

Note that the root cgroup aliases psi_system and keeps cgroup_id == 0.

Signed-off-by: Roman Gushchin <roman.gushchin@xxxxxxxxx>
Signed-off-by: Ben Gardon <bgardon@xxxxxxxxxx>
---
v3 -> v4:
- Guard cgroup_id with #ifdef CONFIG_CGROUPS and note root/psi_system
aliasing (cgroup_id == 0).

v3: https://lore.kernel.org/all/20260127024421.494929-15-roman.gushchin@xxxxxxxxx/

include/linux/psi_types.h | 6 ++++++
kernel/sched/psi.c | 1 +
2 files changed, 7 insertions(+)

diff --git a/include/linux/psi_types.h b/include/linux/psi_types.h
index dd10c22299ab..597f6fabf688 100644
--- a/include/linux/psi_types.h
+++ b/include/linux/psi_types.h
@@ -159,6 +159,12 @@ struct psi_trigger {

struct psi_group {
struct psi_group *parent;
+
+#ifdef CONFIG_CGROUPS
+ /* Cgroup id for non-root cgroups; 0 for root (aliases psi_system) */
+ u64 cgroup_id;
+#endif
+
bool enabled;

/* Protects data used by the aggregator */
diff --git a/kernel/sched/psi.c b/kernel/sched/psi.c
index d89c40902492..571266028a50 100644
--- a/kernel/sched/psi.c
+++ b/kernel/sched/psi.c
@@ -1119,6 +1119,7 @@ int psi_cgroup_alloc(struct cgroup *cgroup)
if (!cgroup->psi)
return -ENOMEM;

+ cgroup->psi->cgroup_id = cgroup_id(cgroup);
cgroup->psi->pcpu = alloc_percpu(struct psi_group_cpu);
if (!cgroup->psi->pcpu) {
kfree(cgroup->psi);
--
2.56.1.911.g883d8bcb20-goog