Re: [PATCH v2 1/2] bpf: Add kfuncs for storing struct task_struct * as a kptr

David Vernet
2022-10-03T21:11:45Z

On Mon, Oct 03, 2022 at 12:20:57PM -0700, Martin KaFai Lau wrote:
> On 10/1/22 7:47 AM, David Vernet wrote:
> > diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
> > index b069517a3da0..36cbe1b8f8b1 100644
> > --- a/kernel/bpf/helpers.c
> > +++ b/kernel/bpf/helpers.c
> > @@ -1700,20 +1700,93 @@ bpf_base_func_proto(enum bpf_func_id func_id)
> > }
> > }
> > -BTF_SET8_START(tracing_btf_ids)
> > +__diag_push();
> > +__diag_ignore_all("-Wmissing-prototypes",
> > + "Global functions as their definitions will be in vmlinux BTF");
> > +
> > +/**
> > + * bpf_task_acquire - Acquire a reference to a task. A task acquired by this
> > + * kfunc which is not stored in a map as a kptr, must be released by calling
> > + * bpf_task_release().
> > + * @p: The task on which a reference is being acquired.
> > + */
> > +__used noinline
> > +struct task_struct *bpf_task_acquire(struct task_struct *p)
> > +{
> > + refcount_inc(&p->rcu_users);
>
> This probably needs to be refcount_inc_not_zero() also for the cases like
> during the task free tracepoint ?

Thanks, you're probably right. As you pointed out offline as well, some
fentry functions may expect a NULL pointer, so we probably need to
update this to check for NULL and also return KF_RET_NULL. I'll take
care of that in v2 once we've aligned on the RCU / sleepable progs
question we're discussing with Kumar.