Skip to content

Commit b5ad4cd

Browse files
zf1575192187Alexei Starovoitov
authored and
Alexei Starovoitov
committed
bpf: Add bpf_task_under_cgroup() kfunc
Add a kfunc that's similar to the bpf_current_task_under_cgroup. The difference is that it is a designated task. When hook sched related functions, sometimes it is necessary to specify a task instead of the current task. Signed-off-by: Feng Zhou <[email protected]> Acked-by: Yonghong Song <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent f4dea96 commit b5ad4cd

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

kernel/bpf/helpers.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2149,6 +2149,22 @@ __bpf_kfunc struct cgroup *bpf_cgroup_from_id(u64 cgid)
21492149
return NULL;
21502150
return cgrp;
21512151
}
2152+
2153+
/**
2154+
* bpf_task_under_cgroup - wrap task_under_cgroup_hierarchy() as a kfunc, test
2155+
* task's membership of cgroup ancestry.
2156+
* @task: the task to be tested
2157+
* @ancestor: possible ancestor of @task's cgroup
2158+
*
2159+
* Tests whether @task's default cgroup hierarchy is a descendant of @ancestor.
2160+
* It follows all the same rules as cgroup_is_descendant, and only applies
2161+
* to the default hierarchy.
2162+
*/
2163+
__bpf_kfunc long bpf_task_under_cgroup(struct task_struct *task,
2164+
struct cgroup *ancestor)
2165+
{
2166+
return task_under_cgroup_hierarchy(task, ancestor);
2167+
}
21522168
#endif /* CONFIG_CGROUPS */
21532169

21542170
/**
@@ -2400,6 +2416,7 @@ BTF_ID_FLAGS(func, bpf_cgroup_acquire, KF_ACQUIRE | KF_RCU | KF_RET_NULL)
24002416
BTF_ID_FLAGS(func, bpf_cgroup_release, KF_RELEASE)
24012417
BTF_ID_FLAGS(func, bpf_cgroup_ancestor, KF_ACQUIRE | KF_RCU | KF_RET_NULL)
24022418
BTF_ID_FLAGS(func, bpf_cgroup_from_id, KF_ACQUIRE | KF_RET_NULL)
2419+
BTF_ID_FLAGS(func, bpf_task_under_cgroup, KF_RCU)
24032420
#endif
24042421
BTF_ID_FLAGS(func, bpf_task_from_pid, KF_ACQUIRE | KF_RET_NULL)
24052422
BTF_SET8_END(generic_btf_ids)

0 commit comments

Comments
 (0)