2022-04-07 23:26:11

by Andrew Morton

[permalink] [raw]
Subject: Re: [PATCH] percpu_ref: call wake_up_all() after percpu_ref_put() completes

(cc Ming Lei)

On Thu, 7 Apr 2022 18:33:35 +0800 Qi Zheng <[email protected]> wrote:

> In the percpu_ref_call_confirm_rcu(), we call the wake_up_all()
> before calling percpu_ref_put(), which will cause the value of
> percpu_ref to be unstable when percpu_ref_switch_to_atomic_sync()
> returns.
>
> CPU0 CPU1
>
> percpu_ref_switch_to_atomic_sync(&ref)
> --> percpu_ref_switch_to_atomic(&ref)
> --> percpu_ref_get(ref); /* put after confirmation */
> call_rcu(&ref->data->rcu, percpu_ref_switch_to_atomic_rcu);
>
> percpu_ref_switch_to_atomic_rcu
> --> percpu_ref_call_confirm_rcu
> --> data->confirm_switch = NULL;
> wake_up_all(&percpu_ref_switch_waitq);
>
> /* here waiting to wake up */
> wait_event(percpu_ref_switch_waitq, !ref->data->confirm_switch);
> (A)percpu_ref_put(ref);
> /* The value of &ref is unstable! */
> percpu_ref_is_zero(&ref)
> (B)percpu_ref_put(ref);
>
> As shown above, assuming that the counts on each cpu add up to 0 before
> calling percpu_ref_switch_to_atomic_sync(), we expect that after switching
> to atomic mode, percpu_ref_is_zero() can return true. But actually it will
> return different values in the two cases of A and B, which is not what
> we expected.
>
> Maybe the original purpose of percpu_ref_switch_to_atomic_sync() is
> just to ensure that the conversion to atomic mode is completed, but it
> should not return with an extra reference count.
>
> Calling wake_up_all() after percpu_ref_put() ensures that the value of
> percpu_ref is stable after percpu_ref_switch_to_atomic_sync() returns.
> So just do it.

Thanks. I'll grab this, but shall await input from others before doing
anything else with it.

> Signed-off-by: Qi Zheng <[email protected]>
> +++ b/lib/percpu-refcount.c
> @@ -154,13 +154,14 @@ static void percpu_ref_call_confirm_rcu(struct rcu_head *rcu)
>
> data->confirm_switch(ref);
> data->confirm_switch = NULL;
> - wake_up_all(&percpu_ref_switch_waitq);
>
> if (!data->allow_reinit)
> __percpu_ref_exit(ref);
>
> /* drop ref from percpu_ref_switch_to_atomic() */
> percpu_ref_put(ref);
> +
> + wake_up_all(&percpu_ref_switch_waitq);
> }
>
> static void percpu_ref_switch_to_atomic_rcu(struct rcu_head *rcu)


2022-04-08 01:09:09

by Dennis Zhou

[permalink] [raw]
Subject: Re: [PATCH] percpu_ref: call wake_up_all() after percpu_ref_put() completes

Hello,

On Thu, Apr 07, 2022 at 03:57:52PM -0700, Andrew Morton wrote:
> (cc Ming Lei)
>
> On Thu, 7 Apr 2022 18:33:35 +0800 Qi Zheng <[email protected]> wrote:
>
> > In the percpu_ref_call_confirm_rcu(), we call the wake_up_all()
> > before calling percpu_ref_put(), which will cause the value of
> > percpu_ref to be unstable when percpu_ref_switch_to_atomic_sync()
> > returns.
> >
> > CPU0 CPU1
> >
> > percpu_ref_switch_to_atomic_sync(&ref)
> > --> percpu_ref_switch_to_atomic(&ref)
> > --> percpu_ref_get(ref); /* put after confirmation */
> > call_rcu(&ref->data->rcu, percpu_ref_switch_to_atomic_rcu);
> >
> > percpu_ref_switch_to_atomic_rcu
> > --> percpu_ref_call_confirm_rcu
> > --> data->confirm_switch = NULL;
> > wake_up_all(&percpu_ref_switch_waitq);
> >
> > /* here waiting to wake up */
> > wait_event(percpu_ref_switch_waitq, !ref->data->confirm_switch);
> > (A)percpu_ref_put(ref);
> > /* The value of &ref is unstable! */
> > percpu_ref_is_zero(&ref)
> > (B)percpu_ref_put(ref);
> >
> > As shown above, assuming that the counts on each cpu add up to 0 before
> > calling percpu_ref_switch_to_atomic_sync(), we expect that after switching
> > to atomic mode, percpu_ref_is_zero() can return true. But actually it will
> > return different values in the two cases of A and B, which is not what
> > we expected.
> >
> > Maybe the original purpose of percpu_ref_switch_to_atomic_sync() is
> > just to ensure that the conversion to atomic mode is completed, but it
> > should not return with an extra reference count.
> >
> > Calling wake_up_all() after percpu_ref_put() ensures that the value of
> > percpu_ref is stable after percpu_ref_switch_to_atomic_sync() returns.
> > So just do it.
>
> Thanks. I'll grab this, but shall await input from others before doing
> anything else with it.
>

Seems right to me. The percpu_ref protects the __percpu_ref_exit(), not
the waiters.

Acked-by: Dennis Zhou <[email protected]>

Thanks,
Dennis

> > Signed-off-by: Qi Zheng <[email protected]>
> > +++ b/lib/percpu-refcount.c
> > @@ -154,13 +154,14 @@ static void percpu_ref_call_confirm_rcu(struct rcu_head *rcu)
> >
> > data->confirm_switch(ref);
> > data->confirm_switch = NULL;
> > - wake_up_all(&percpu_ref_switch_waitq);
> >
> > if (!data->allow_reinit)
> > __percpu_ref_exit(ref);
> >
> > /* drop ref from percpu_ref_switch_to_atomic() */
> > percpu_ref_put(ref);
> > +
> > + wake_up_all(&percpu_ref_switch_waitq);
> > }
> >
> > static void percpu_ref_switch_to_atomic_rcu(struct rcu_head *rcu)
>

2022-04-08 02:08:51

by Ming Lei

[permalink] [raw]
Subject: Re: [PATCH] percpu_ref: call wake_up_all() after percpu_ref_put() completes

On Thu, Apr 07, 2022 at 03:57:52PM -0700, Andrew Morton wrote:
> (cc Ming Lei)
>
> On Thu, 7 Apr 2022 18:33:35 +0800 Qi Zheng <[email protected]> wrote:
>
> > In the percpu_ref_call_confirm_rcu(), we call the wake_up_all()
> > before calling percpu_ref_put(), which will cause the value of
> > percpu_ref to be unstable when percpu_ref_switch_to_atomic_sync()
> > returns.
> >
> > CPU0 CPU1
> >
> > percpu_ref_switch_to_atomic_sync(&ref)
> > --> percpu_ref_switch_to_atomic(&ref)
> > --> percpu_ref_get(ref); /* put after confirmation */
> > call_rcu(&ref->data->rcu, percpu_ref_switch_to_atomic_rcu);
> >
> > percpu_ref_switch_to_atomic_rcu
> > --> percpu_ref_call_confirm_rcu
> > --> data->confirm_switch = NULL;
> > wake_up_all(&percpu_ref_switch_waitq);
> >
> > /* here waiting to wake up */
> > wait_event(percpu_ref_switch_waitq, !ref->data->confirm_switch);
> > (A)percpu_ref_put(ref);
> > /* The value of &ref is unstable! */
> > percpu_ref_is_zero(&ref)
> > (B)percpu_ref_put(ref);
> >
> > As shown above, assuming that the counts on each cpu add up to 0 before
> > calling percpu_ref_switch_to_atomic_sync(), we expect that after switching
> > to atomic mode, percpu_ref_is_zero() can return true. But actually it will

Looks all current users expect the refcount is stable after percpu_ref_switch_to_atomic_sync
returns, even though the API itself doesn't mention the point explicitly.

> > return different values in the two cases of A and B, which is not what
> > we expected.
> >
> > Maybe the original purpose of percpu_ref_switch_to_atomic_sync() is
> > just to ensure that the conversion to atomic mode is completed, but it
> > should not return with an extra reference count.
> >
> > Calling wake_up_all() after percpu_ref_put() ensures that the value of
> > percpu_ref is stable after percpu_ref_switch_to_atomic_sync() returns.
> > So just do it.
>
> Thanks. I'll grab this, but shall await input from others before doing
> anything else with it.
>
> > Signed-off-by: Qi Zheng <[email protected]>
> > +++ b/lib/percpu-refcount.c
> > @@ -154,13 +154,14 @@ static void percpu_ref_call_confirm_rcu(struct rcu_head *rcu)
> >
> > data->confirm_switch(ref);
> > data->confirm_switch = NULL;
> > - wake_up_all(&percpu_ref_switch_waitq);
> >
> > if (!data->allow_reinit)
> > __percpu_ref_exit(ref);
> >
> > /* drop ref from percpu_ref_switch_to_atomic() */
> > percpu_ref_put(ref);
> > +
> > + wake_up_all(&percpu_ref_switch_waitq);
> > }

Looks fine:

Reviewed-by: Ming Lei <[email protected]>


Thanks,
Ming