2022-11-22 06:36:03

by David Vernet

[permalink] [raw]
Subject: [PATCH bpf-next 0/4] Support storing struct cgroup * objects as kptrs

In [0], we added support for storing struct task_struct * objects as
kptrs. This patch set extends this effort to also include storing struct
cgroup * object as kptrs.

As with tasks, there are many possible use cases for storing cgroups in
maps. During tracing, for example, it could be useful to query cgroup
statistics such as PSI averages, or tracking which tasks are migrating
to and from the cgroup.

[0]: https://lore.kernel.org/all/[email protected]/

David Vernet (4):
bpf: Enable cgroups to be used as kptrs
selftests/bpf: Add cgroup kfunc / kptr selftests
bpf: Add bpf_cgroup_ancestor() kfunc
selftests/bpf: Add selftests for bpf_cgroup_ancestor() kfunc

kernel/bpf/helpers.c | 103 ++++++-
tools/testing/selftests/bpf/DENYLIST.s390x | 1 +
.../selftests/bpf/prog_tests/cgrp_kfunc.c | 175 ++++++++++++
.../selftests/bpf/progs/cgrp_kfunc_common.h | 72 +++++
.../selftests/bpf/progs/cgrp_kfunc_failure.c | 260 ++++++++++++++++++
.../selftests/bpf/progs/cgrp_kfunc_success.c | 170 ++++++++++++
6 files changed, 778 insertions(+), 3 deletions(-)
create mode 100644 tools/testing/selftests/bpf/prog_tests/cgrp_kfunc.c
create mode 100644 tools/testing/selftests/bpf/progs/cgrp_kfunc_common.h
create mode 100644 tools/testing/selftests/bpf/progs/cgrp_kfunc_failure.c
create mode 100644 tools/testing/selftests/bpf/progs/cgrp_kfunc_success.c

--
2.38.1


2022-11-22 06:36:04

by David Vernet

[permalink] [raw]
Subject: [PATCH bpf-next 3/4] bpf: Add bpf_cgroup_ancestor() kfunc

struct cgroup * objects have a variably sized struct cgroup *ancestors[]
field which stores pointers to their ancestor cgroups. If using a cgroup
as a kptr, it can be useful to access these ancestors, but doing so
requires variable offset accesses for PTR_TO_BTF_ID, which is currently
unsupported.

This is a very useful field to access for cgroup kptrs, as programs may
wish to walk their ancestor cgroups when determining e.g. their
proportional cpu.weight. So as to enable this functionality with cgroup
kptrs before var_off is supported for PTR_TO_BTF_ID, this patch adds a
bpf_cgroup_ancestor() kfunc which accesses the cgroup node on behalf of
the caller, and acquires a reference on it. Once var_off is supported
for PTR_TO_BTF_ID, and fields inside a struct can be marked as trusted
so they retain the PTR_TRUSTED modifier when walked, this can be
removed.

Signed-off-by: David Vernet <[email protected]>
---
kernel/bpf/helpers.c | 20 ++++++++++++++++++++
1 file changed, 20 insertions(+)

diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
index 420a4251cccc..e4e9db301db5 100644
--- a/kernel/bpf/helpers.c
+++ b/kernel/bpf/helpers.c
@@ -1938,6 +1938,25 @@ void bpf_cgroup_release(struct cgroup *cgrp)

cgroup_put(cgrp);
}
+
+/**
+ * bpf_cgroup_ancestor - Perform a lookup on an entry in a cgroup's ancestor
+ * array. 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.
+ */
+struct cgroup *bpf_cgroup_ancestor(struct cgroup *cgrp, int level)
+{
+ struct cgroup *ancestor;
+
+ if (level > cgrp->level || level < 0)
+ return NULL;
+
+ ancestor = cgrp->ancestors[level];
+ cgroup_get(ancestor);
+ return ancestor;
+}
#endif /* CONFIG_CGROUPS */

void *bpf_cast_to_kern_ctx(void *obj)
@@ -1970,6 +1989,7 @@ BTF_ID_FLAGS(func, bpf_task_release, KF_RELEASE)
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)
#endif
BTF_SET8_END(generic_btf_ids)

--
2.38.1

2022-11-22 23:15:23

by Tejun Heo

[permalink] [raw]
Subject: Re: [PATCH bpf-next 0/4] Support storing struct cgroup * objects as kptrs

On Mon, Nov 21, 2022 at 11:54:54PM -0600, David Vernet wrote:
> In [0], we added support for storing struct task_struct * objects as
> kptrs. This patch set extends this effort to also include storing struct
> cgroup * object as kptrs.
>
> As with tasks, there are many possible use cases for storing cgroups in
> maps. During tracing, for example, it could be useful to query cgroup
> statistics such as PSI averages, or tracking which tasks are migrating
> to and from the cgroup.
>
> [0]: https://lore.kernel.org/all/[email protected]/
>
> David Vernet (4):
> bpf: Enable cgroups to be used as kptrs
> selftests/bpf: Add cgroup kfunc / kptr selftests
> bpf: Add bpf_cgroup_ancestor() kfunc
> selftests/bpf: Add selftests for bpf_cgroup_ancestor() kfunc

From cgroup POV:

Acked-by: Tejun Heo <[email protected]>

Thanks.

--
tejun

2022-11-22 23:51:03

by patchwork-bot+netdevbpf

[permalink] [raw]
Subject: Re: [PATCH bpf-next 0/4] Support storing struct cgroup * objects as kptrs

Hello:

This series was applied to bpf/bpf-next.git (master)
by Alexei Starovoitov <[email protected]>:

On Mon, 21 Nov 2022 23:54:54 -0600 you wrote:
> In [0], we added support for storing struct task_struct * objects as
> kptrs. This patch set extends this effort to also include storing struct
> cgroup * object as kptrs.
>
> As with tasks, there are many possible use cases for storing cgroups in
> maps. During tracing, for example, it could be useful to query cgroup
> statistics such as PSI averages, or tracking which tasks are migrating
> to and from the cgroup.
>
> [...]

Here is the summary with links:
- [bpf-next,1/4] bpf: Enable cgroups to be used as kptrs
https://git.kernel.org/bpf/bpf-next/c/fda01efc6160
- [bpf-next,2/4] selftests/bpf: Add cgroup kfunc / kptr selftests
https://git.kernel.org/bpf/bpf-next/c/f583ddf15e57
- [bpf-next,3/4] bpf: Add bpf_cgroup_ancestor() kfunc
https://git.kernel.org/bpf/bpf-next/c/5ca786707829
- [bpf-next,4/4] selftests/bpf: Add selftests for bpf_cgroup_ancestor() kfunc
https://git.kernel.org/bpf/bpf-next/c/227a89cf5041

You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html