On Fri, Mar 17, 2023 at 11:33:25AM -1000, Tejun Heo wrote: ... > +/** > + * scx_bpf_task_cgroup - Return the sched cgroup of a task > + * @p: task of interest > + * > + * @p->sched_task_group->css.cgroup represents the cgroup @p is associated with > + * from the scheduler's POV. SCX operations should use this function to > + * determine @p's current cgroup as, unlike following @p->cgroups, > + * @p->sched_task_group is protected by @p's rq lock and thus atomic w.r.t. all > + * rq-locked operations. Can be called on the parameter tasks of rq-locked > + * operations. The restriction guarantees that @p's rq is locked by the caller. > + */ > +struct cgroup *scx_bpf_task_cgroup(struct task_struct *p) > +{ > + struct task_group *tg = p->sched_task_group; > + struct cgroup *cgrp = &cgrp_dfl_root.cgrp; > + > + if (!scx_kf_allowed_on_arg_tasks(__SCX_KF_RQ_LOCKED, p)) > + goto out; > + > + /* > + * A task_group may either be a cgroup or an autogroup. In the latter > + * case, @tg->css.cgroup is %NULL. A task_group can't become the other > + * kind once created. > + */ > + if (tg && tg->css.cgroup) > + cgrp = tg->css.cgroup; > + else > + cgrp = &cgrp_dfl_root.cgrp; > +out: > + cgroup_get(cgrp); > + return cgrp; > +} ^ #ifdef CONFIG_CGROUP_SCHED, otherwise we may get build errors, like: kernel/sched/ext.c:4251:34: error: 'struct task_struct' has no member named 'sched_task_group' > + > BTF_SET8_START(scx_kfunc_ids_any) > BTF_ID_FLAGS(func, scx_bpf_kick_cpu) > BTF_ID_FLAGS(func, scx_bpf_dsq_nr_queued) > @@ -3431,6 +3803,7 @@ BTF_ID_FLAGS(func, scx_bpf_error_bstr, KF_TRUSTED_ARGS) > BTF_ID_FLAGS(func, scx_bpf_destroy_dsq) > BTF_ID_FLAGS(func, scx_bpf_task_running, KF_RCU) > BTF_ID_FLAGS(func, scx_bpf_task_cpu, KF_RCU) > +BTF_ID_FLAGS(func, scx_bpf_task_cgroup, KF_RCU | KF_ACQUIRE) Ditto. -Andrea