diff mbox series

[RFC,bpf-next,3/8] bpf: Add kfuncs for cgroup1 hierarchy

Message ID 20231007140304.4390-4-laoar.shao@gmail.com (mailing list archive)
State Superseded
Delegated to: BPF
Headers show
Series bpf, cgroup: Add BPF support for cgroup1 hierarchy | expand

Checks

Context Check Description
bpf/vmtest-bpf-next-PR success PR summary
bpf/vmtest-bpf-next-VM_Test-0 success Logs for ShellCheck
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for bpf-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit fail Errors and warnings before: 1383 this patch: 1387
netdev/cc_maintainers success CCed 12 of 12 maintainers
netdev/build_clang fail Errors and warnings before: 1364 this patch: 1366
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn fail Errors and warnings before: 1406 this patch: 1410
netdev/checkpatch warning WARNING: line length of 87 exceeds 80 columns WARNING: line length of 93 exceeds 80 columns WARNING: line length of 96 exceeds 80 columns WARNING: line length of 99 exceeds 80 columns
netdev/kdoc fail Errors and warnings before: 0 this patch: 2
netdev/source_inline success Was 0 now: 0

Commit Message

Yafang Shao Oct. 7, 2023, 2:02 p.m. UTC
Two new kfuncs are added to retrieve the cgroup1 IDs.

- bpf_task_cgroup1_id_within_hierarchy
  Retrieves the associated cgroup ID of a task whithin a specific
  cgroup1 hierarchy. The cgroup1 hierarchy is identified by its
  hierarchy ID.
- bpf_task_ancestor_cgroup1_id_within_hierarchy
  Retrieves the associated ancestor cgroup ID of a task whithin a
  specific cgroup1 hierarchy. he specific ancestor cgroup is determined by
  the ancestor level within the cgroup1 hierarchy.

These two new kfuncs enable the tracing of tasks within a designated
container or cgroup directory in BPF programs.

Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
---
 kernel/bpf/helpers.c | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)

Comments

Tejun Heo Oct. 7, 2023, 3:57 p.m. UTC | #1
On Sat, Oct 07, 2023 at 02:02:59PM +0000, Yafang Shao wrote:
> +
> +/**
> + * bpf_task_cgroup_id_within_hierarchy - Retrieves the associated cgroup ID of a
> + * task within a specific cgroup1 hierarchy.
> + * @task: The target task
> + * @hierarchy_id: The ID of a cgroup1 hierarchy
> + */
> +__bpf_kfunc u64 bpf_task_cgroup1_id_within_hierarchy(struct task_struct *task, int hierarchy_id)
> +{
> +	return task_cgroup1_id_within_hierarchy(task, hierarchy_id);
> +}
> +
> +/**
> + * bpf_task_ancestor_cgroup_id_within_hierarchy - Retrieves the associated
> + * ancestor cgroup ID of a task within a specific cgroup1 hierarchy.
> + * @task: The target task
> + * @hierarchy_id: The ID of a cgroup1 hierarchy
> + * @ancestor_level: The cgroup level of the ancestor in the cgroup1 hierarchy
> + */
> +__bpf_kfunc u64 bpf_task_ancestor_cgroup1_id_within_hierarchy(struct task_struct *task,
> +							      int hierarchy_id, int ancestor_level)
> +{
> +	return task_ancestor_cgroup1_id_within_hierarchy(task, hierarchy_id, ancestor_level);
> +}

The same here. Please make one helper that returns a kptr and then let the
user call bpf_cgroup_ancestor() if desired.

Thanks.
Yafang Shao Oct. 8, 2023, 2:37 a.m. UTC | #2
On Sat, Oct 7, 2023 at 11:57 PM Tejun Heo <tj@kernel.org> wrote:
>
> On Sat, Oct 07, 2023 at 02:02:59PM +0000, Yafang Shao wrote:
> > +
> > +/**
> > + * bpf_task_cgroup_id_within_hierarchy - Retrieves the associated cgroup ID of a
> > + * task within a specific cgroup1 hierarchy.
> > + * @task: The target task
> > + * @hierarchy_id: The ID of a cgroup1 hierarchy
> > + */
> > +__bpf_kfunc u64 bpf_task_cgroup1_id_within_hierarchy(struct task_struct *task, int hierarchy_id)
> > +{
> > +     return task_cgroup1_id_within_hierarchy(task, hierarchy_id);
> > +}
> > +
> > +/**
> > + * bpf_task_ancestor_cgroup_id_within_hierarchy - Retrieves the associated
> > + * ancestor cgroup ID of a task within a specific cgroup1 hierarchy.
> > + * @task: The target task
> > + * @hierarchy_id: The ID of a cgroup1 hierarchy
> > + * @ancestor_level: The cgroup level of the ancestor in the cgroup1 hierarchy
> > + */
> > +__bpf_kfunc u64 bpf_task_ancestor_cgroup1_id_within_hierarchy(struct task_struct *task,
> > +                                                           int hierarchy_id, int ancestor_level)
> > +{
> > +     return task_ancestor_cgroup1_id_within_hierarchy(task, hierarchy_id, ancestor_level);
> > +}
>
> The same here. Please make one helper that returns a kptr and then let the
> user call bpf_cgroup_ancestor() if desired.

Sure, will do it.
diff mbox series

Patch

diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
index dd1c69ee3375..39ec6f9f2027 100644
--- a/kernel/bpf/helpers.c
+++ b/kernel/bpf/helpers.c
@@ -2214,6 +2214,30 @@  __bpf_kfunc long bpf_task_under_cgroup(struct task_struct *task,
 {
 	return task_under_cgroup_hierarchy(task, ancestor);
 }
+
+/**
+ * bpf_task_cgroup_id_within_hierarchy - Retrieves the associated cgroup ID of a
+ * task within a specific cgroup1 hierarchy.
+ * @task: The target task
+ * @hierarchy_id: The ID of a cgroup1 hierarchy
+ */
+__bpf_kfunc u64 bpf_task_cgroup1_id_within_hierarchy(struct task_struct *task, int hierarchy_id)
+{
+	return task_cgroup1_id_within_hierarchy(task, hierarchy_id);
+}
+
+/**
+ * bpf_task_ancestor_cgroup_id_within_hierarchy - Retrieves the associated
+ * ancestor cgroup ID of a task within a specific cgroup1 hierarchy.
+ * @task: The target task
+ * @hierarchy_id: The ID of a cgroup1 hierarchy
+ * @ancestor_level: The cgroup level of the ancestor in the cgroup1 hierarchy
+ */
+__bpf_kfunc u64 bpf_task_ancestor_cgroup1_id_within_hierarchy(struct task_struct *task,
+							      int hierarchy_id, int ancestor_level)
+{
+	return task_ancestor_cgroup1_id_within_hierarchy(task, hierarchy_id, ancestor_level);
+}
 #endif /* CONFIG_CGROUPS */
 
 /**
@@ -2520,6 +2544,8 @@  BTF_ID_FLAGS(func, bpf_cgroup_release, KF_RELEASE)
 BTF_ID_FLAGS(func, bpf_cgroup_ancestor, KF_ACQUIRE | KF_RCU | KF_RET_NULL)
 BTF_ID_FLAGS(func, bpf_cgroup_from_id, KF_ACQUIRE | KF_RET_NULL)
 BTF_ID_FLAGS(func, bpf_task_under_cgroup, KF_RCU)
+BTF_ID_FLAGS(func, bpf_task_cgroup1_id_within_hierarchy, KF_RCU)
+BTF_ID_FLAGS(func, bpf_task_ancestor_cgroup1_id_within_hierarchy, KF_RCU)
 #endif
 BTF_ID_FLAGS(func, bpf_task_from_pid, KF_ACQUIRE | KF_RET_NULL)
 BTF_ID_FLAGS(func, bpf_throw)