Re: [PATCH v4] kernel/fork: beware of __put_task_struct calling context

From: Wander Lairson Costa
Date: Mon Feb 06 2023 - 13:37:44 EST


On Mon, Feb 6, 2023 at 1:04 PM, Sebastian Andrzej Siewior
<bigeasy@xxxxxxxxxxxxx> wrote:
>
> On 2023-02-06 16:27:12 [+0100], Oleg Nesterov wrote:
> > On 02/06, Sebastian Andrzej Siewior wrote:
> > >
> > > On 2023-02-06 10:04:47 [-0300], Wander Lairson Costa wrote:
> > >
> > > > @@ -857,6 +857,29 @@ void __put_task_struct(struct task_struct *tsk)
> > > …
> > > > +void __put_task_struct(struct task_struct *tsk)
> > > > +{
> > > > + if (IS_ENABLED(CONFIG_PREEMPT_RT) && (!preemptible() || !in_task()))
> > >
> > > Is it safe to use the rcu member in any case?
> >
> > I thinks it is safe but deserves a comment. I guess Wander misunderstood
> > me when I asked him to do this...
> >
> > __put_task_struct() is called when refcount_dec_and_test(&t->usage) succeeds.
> >
> > This means that it can't "conflict" with put_task_struct_rcu_user() which
> > abuses ->rcu the same way; rcu_users has a reference so task->usage can't
> > be zero after rcu_users 1 -> 0 transition.
>
> Sounds good.
>
> > > If so why not use it
> > > unconditionally?
> >
> > performance ?
>
> All the free() part is moved from the caller into rcu.
>
> >
> > And... I still don't like the name of delayed_put_task_struct_rcu() to me
> > ___put_task_struct_rcu() looks a bit less confusing, note that we already
> > have delayed_put_task_struct(). But this is minor.
>
> So if we do it unconditionally then we could get rid of
> put_task_struct_rcu_user().
> Otherwise we could use put_task_struct_rcu_user() in that timer
> callback because it will lead to lockdep warnings once printk is fixed.

put_task_struct_rcu_user() calls delayed_put_task_struct(), which does
more than just call __put_task_struct(). I tried this approach at the
beginning, but I got another splat (unfortunately, I don't remember
where).