Re: [PATCH v3 bpf-next 3/5] bpf: Introduce kptr_rcu.

Tejun Heo
2023-02-28T19:39:37Z

On Mon, Feb 27, 2023 at 08:01:19PM -0800, Alexei Starovoitov wrote:
> From: Alexei Starovoitov <ast@kernel.org>
>
> The life time of certain kernel structures like 'struct cgroup' is protected by RCU.
> Hence it's safe to dereference them directly from __kptr tagged pointers in bpf maps.
> The resulting pointer is MEM_RCU and can be passed to kfuncs that expect KF_RCU.
> Derefrence of other kptr-s returns PTR_UNTRUSTED.
>
> For example:
> struct map_value {
> struct cgroup __kptr *cgrp;
> };
>
> SEC("tp_btf/cgroup_mkdir")
> int BPF_PROG(test_cgrp_get_ancestors, struct cgroup *cgrp_arg, const char *path)
> {
> struct cgroup *cg, *cg2;
>
> cg = bpf_cgroup_acquire(cgrp_arg); // cg is PTR_TRUSTED and ref_obj_id > 0
> bpf_kptr_xchg(&v->cgrp, cg);
>
> cg2 = v->cgrp; // This is new feature introduced by this patch.
> // cg2 is PTR_MAYBE_NULL | MEM_RCU.
> // When cg2 != NULL, it's a valid cgroup, but its percpu_ref could be zero
>
> bpf_cgroup_ancestor(cg2, level); // safe to do.
> }
>
> Signed-off-by: Alexei Starovoitov <ast@kernel.org>

From cgroup POV:

Acked-by: Tejun Heo <tj@kernel.org>

Thanks.

--
tejun