Skip to content

Commit 332ea1f

Browse files
htejunAlexei Starovoitov
authored andcommitted
bpf: Add bpf_cgroup_from_id() kfunc
cgroup ID is an userspace-visible 64bit value uniquely identifying a given cgroup. As the IDs are used widely, it's useful to be able to look up the matching cgroups. Add bpf_cgroup_from_id(). v2: Separate out selftest into its own patch as suggested by Alexei. Signed-off-by: Tejun Heo <[email protected]> Link: https://lore.kernel.org/r/Y/bBaG96t0/gQl9/@slm.duckdns.org Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 746ce76 commit 332ea1f

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

Documentation/bpf/kfuncs.rst

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -583,13 +583,17 @@ Here's an example of how it can be used:
583583
584584
----
585585

586-
Another kfunc available for interacting with ``struct cgroup *`` objects is
587-
bpf_cgroup_ancestor(). This allows callers to access the ancestor of a cgroup,
588-
and return it as a cgroup kptr.
586+
Other kfuncs available for interacting with ``struct cgroup *`` objects are
587+
bpf_cgroup_ancestor() and bpf_cgroup_from_id(), allowing callers to access
588+
the ancestor of a cgroup and find a cgroup by its ID, respectively. Both
589+
return a cgroup kptr.
589590

590591
.. kernel-doc:: kernel/bpf/helpers.c
591592
:identifiers: bpf_cgroup_ancestor
592593

594+
.. kernel-doc:: kernel/bpf/helpers.c
595+
:identifiers: bpf_cgroup_from_id
596+
593597
Eventually, BPF should be updated to allow this to happen with a normal memory
594598
load in the program itself. This is currently not possible without more work in
595599
the verifier. bpf_cgroup_ancestor() can be used as follows:

kernel/bpf/helpers.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2101,6 +2101,23 @@ __bpf_kfunc struct cgroup *bpf_cgroup_ancestor(struct cgroup *cgrp, int level)
21012101
cgroup_get(ancestor);
21022102
return ancestor;
21032103
}
2104+
2105+
/**
2106+
* bpf_cgroup_from_id - Find a cgroup from its ID. A cgroup returned by this
2107+
* kfunc which is not subsequently stored in a map, must be released by calling
2108+
* bpf_cgroup_release().
2109+
* @cgrp: The cgroup for which we're performing a lookup.
2110+
* @level: The level of ancestor to look up.
2111+
*/
2112+
__bpf_kfunc struct cgroup *bpf_cgroup_from_id(u64 cgid)
2113+
{
2114+
struct cgroup *cgrp;
2115+
2116+
cgrp = cgroup_get_from_id(cgid);
2117+
if (IS_ERR(cgrp))
2118+
return NULL;
2119+
return cgrp;
2120+
}
21042121
#endif /* CONFIG_CGROUPS */
21052122

21062123
/**
@@ -2167,6 +2184,7 @@ BTF_ID_FLAGS(func, bpf_cgroup_acquire, KF_ACQUIRE | KF_TRUSTED_ARGS)
21672184
BTF_ID_FLAGS(func, bpf_cgroup_kptr_get, KF_ACQUIRE | KF_KPTR_GET | KF_RET_NULL)
21682185
BTF_ID_FLAGS(func, bpf_cgroup_release, KF_RELEASE)
21692186
BTF_ID_FLAGS(func, bpf_cgroup_ancestor, KF_ACQUIRE | KF_TRUSTED_ARGS | KF_RET_NULL)
2187+
BTF_ID_FLAGS(func, bpf_cgroup_from_id, KF_ACQUIRE | KF_RET_NULL)
21702188
#endif
21712189
BTF_ID_FLAGS(func, bpf_task_from_pid, KF_ACQUIRE | KF_RET_NULL)
21722190
BTF_SET8_END(generic_btf_ids)

0 commit comments

Comments
 (0)