bpf: Add bpf_cgroup_from_id() kfunc
Subsystems (4)
PATCH 1/1 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().
--- Documentation/bpf/kfuncs.rst | 10 +++-- kernel/bpf/helpers.c | 18 ++++++++ .../selftests/bpf/prog_tests/cgrp_kfunc.c | 1 + .../selftests/bpf/progs/cgrp_kfunc_common.h | 1 + .../selftests/bpf/progs/cgrp_kfunc_success.c | 42 +++++++++++++++++++ 5 files changed, 69 insertions(+), 3 deletions(-)
Documentation/bpf/kfuncs.rst +7 -3
@@ -583,13 +583,17 @@ You may also acquire a reference to a ``struct cgroup`` kptr that's already ---- -Another kfunc available for interacting with ``struct cgroup *`` objects is -bpf_cgroup_ancestor(). This allows callers to access the ancestor of a cgroup, -and return it as a cgroup kptr. +Other kfuncs available for interacting with ``struct cgroup *`` objects are +bpf_cgroup_ancestor() and bpf_cgroup_from_id(), allowing callers to access +the ancestor of a cgroup and find a cgroup by its ID, respectively. Both +return a cgroup kptr. .. kernel-doc:: kernel/bpf/helpers.c :identifiers: bpf_cgroup_ancestor +.. kernel-doc:: kernel/bpf/helpers.c + :identifiers: bpf_cgroup_from_id + Eventually, BPF should be updated to allow this to happen with a normal memory load in the program itself. This is currently not possible without more work in the verifier. bpf_cgroup_ancestor() can be used as follows:
kernel/bpf/helpers.c +18 -0
@@ -2101,6 +2101,23 @@ __bpf_kfunc struct cgroup *bpf_cgroup_ancestor(struct cgroup *cgrp, int level)cgroup_get (ancestor); return ancestor; } + +/** +* bpf_cgroup_from_id - Find a cgroup from its ID. A cgroup returned by this +* kfunc which is not subsequently stored in a map, must be released by calling +* bpf_cgroup_release(). +* @cgrp: The cgroup for which we're performing a lookup. +* @level: The level of ancestor to look up. +*/ +__bpf_kfunc struct cgroup *bpf_cgroup_from_id(u64 cgid) +{ +struct cgroup *cgrp; + + cgrp =cgroup_get_from_id (cgid); +if (IS_ERR (cgrp)) +return NULL ; +return cgrp; +}#endif /* CONFIG_CGROUPS */ /**
@@ -2167,6 +2184,7 @@ BTF_ID_FLAGS(func, bpf_cgroup_acquire, KF_ACQUIRE | KF_TRUSTED_ARGS) BTF_ID_FLAGS(func, bpf_cgroup_kptr_get, KF_ACQUIRE | KF_KPTR_GET | KF_RET_NULL) BTF_ID_FLAGS(func, bpf_cgroup_release, KF_RELEASE) BTF_ID_FLAGS(func, bpf_cgroup_ancestor, KF_ACQUIRE | KF_TRUSTED_ARGS | KF_RET_NULL) +BTF_ID_FLAGS(func, bpf_cgroup_from_id, KF_ACQUIRE | KF_RET_NULL)#endif BTF_ID_FLAGS(func, bpf_task_from_pid, KF_ACQUIRE | KF_RET_NULL)BTF_SET8_END (generic_btf_ids)
tools/testing/selftests/bpf/prog_tests/cgrp_kfunc.c +1 -0
@@ -84,6 +84,7 @@ static const char * const success_tests[] = {"test_cgrp_xchg_release" ,"test_cgrp_get_release" ,"test_cgrp_get_ancestors" , + "test_cgrp_from_id", };void test_cgrp_kfunc (void )
tools/testing/selftests/bpf/progs/cgrp_kfunc_common.h +1 -0
@@ -24,6 +24,7 @@ struct cgroup *bpf_cgroup_acquire(struct cgroup *p) __ksym;struct cgroup *bpf_cgroup_kptr_get (struct cgroup **pp) __ksym; void bpf_cgroup_release(struct cgroup *p) __ksym;struct cgroup *bpf_cgroup_ancestor (struct cgroup *cgrp,int level) __ksym; +struct cgroup *bpf_cgroup_from_id (u64 cgid) __ksym;static inline struct __cgrps_kfunc_map_value *cgrps_kfunc_map_value_lookup (struct cgroup *cgrp) {
tools/testing/selftests/bpf/progs/cgrp_kfunc_success.c +42 -0
45 lines not on this page · raw message
Signature · 1 line
2.39.2
Quoted 5 lines · Tejun Heo
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().
Quoted 8 lines
Signed-off-by Tejun Heo <[email protected]>--- Documentation/bpf/kfuncs.rst | 10 +++-- kernel/bpf/helpers.c | 18 ++++++++ .../selftests/bpf/prog_tests/cgrp_kfunc.c | 1 + .../selftests/bpf/progs/cgrp_kfunc_common.h | 1 + .../selftests/bpf/progs/cgrp_kfunc_success.c | 42 +++++++++++++++++++ 5 files changed, 69 insertions(+), 3 deletions(-)
Looks great, but could you please split it into two patches:
One for helpers.c and kfuncs.rst and another for selftests with [PATCH bpf-next] selftests/bpf: subject ? Doesn't matter much in this case, but it might ease backporting and easier to track patches grouped by subj.
Quoted 15 lines · Alexei Starovoitov
Quoted 5 lines · Tejun Heo
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().Quoted 8 lines
Signed-off-by Tejun Heo <[email protected]>--- Documentation/bpf/kfuncs.rst | 10 +++-- kernel/bpf/helpers.c | 18 ++++++++ .../selftests/bpf/prog_tests/cgrp_kfunc.c | 1 + .../selftests/bpf/progs/cgrp_kfunc_common.h | 1 + .../selftests/bpf/progs/cgrp_kfunc_success.c | 42 +++++++++++++++++++ 5 files changed, 69 insertions(+), 3 deletions(-)
Quoted 5 lines
Looks great, but could you please split it into two patches:
One forhelpers.candkfuncs.rstand another for selftests with [PATCH bpf-next] selftests/bpf: subject ? Doesn't matter much in this case, but it might ease backporting and easier to track patches grouped by subj.
Will do.
Thanks.
Signature · 1 line
tejun