From 7795ccbf82e0eff86fb8fd4e891f3e35c183c2b2 Mon Sep 17 00:00:00 2001 From: Bauerchen Date: Thu, 15 Jul 2021 20:12:41 +0800 Subject: [PATCH] tqos/mbuf: write a help function to get cgroup struct from task_struct. In order to support cgroup V1 with mbuf and sli, we need a special cgroup structure, cpuacct subsys cgroup is nice. We only prepare mbuf and sli for cpuacct cgroup in V1. so first find cpuacct cgroup and if return NULl or root, just find df1_cgrp. Signed-off-by: Bauerchen Reviewed-by: mungerjiang Reviewed-by: mungerjiang --- include/linux/cgroup.h | 1 + kernel/cgroup/cgroup.c | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/include/linux/cgroup.h b/include/linux/cgroup.h index 7541951d6..c3d7a27ca 100644 --- a/include/linux/cgroup.h +++ b/include/linux/cgroup.h @@ -960,4 +960,5 @@ static inline void cgroup_bpf_put(struct cgroup *cgrp) {} #endif /* CONFIG_CGROUP_BPF */ ssize_t mbuf_print(struct cgroup *cgrp, const char *fmt, ...); +struct cgroup *get_cgroup_from_task(struct task_struct *task); #endif /* _LINUX_CGROUP_H */ diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c index a9f33ad42..f8d6cd396 100644 --- a/kernel/cgroup/cgroup.c +++ b/kernel/cgroup/cgroup.c @@ -3763,6 +3763,33 @@ static int mbuf_show(struct seq_file *s, void *v) return 0; } +/* + * Get cgroup struct from task_struct for mbuf and sli. + * + * In cgroup V1, return cpuacct cgroup, and if cpuacct cgroup is root, df1_cgrp + * is returned, but is harmless. + * + * In cgroup V2, just return task_struct.cgroups.dfl_cgrp. + */ +struct cgroup *get_cgroup_from_task(struct task_struct *task) +{ + struct cgroup *cgrp; + + /* First, try to get cpuacct cgroup for V1*/ + cgrp = task_cgroup(task, cpuacct_cgrp_id); + if (cgrp && cgrp->level) + return cgrp; + + /* + * If can not find cpuacct cgroup or cpuacct cgroup is root, just return + * dfl_cgrp. + */ + cgrp = task_dfl_cgroup(task); + + return cgrp; +} + + ssize_t mbuf_print(struct cgroup *cgrp, const char *fmt, ...) { struct mbuf_slot *mb;