Re: [PATCH] percpu_rwsem: fix missed wakeup due to reordering of load

From: Andrea Parri
Date: Fri Dec 21 2018 - 04:46:06 EST


On Fri, Dec 21, 2018 at 12:59:13PM +0530, Prateek Sood wrote:
> P1 is releaseing the cpu_hotplug_lock and P2 is acquiring
> cpu_hotplug_lock.
>
> P1 P2
> percpu_up_read() path percpu_down_write() path
>
> rcu_sync_enter() //gp_state=GP_PASSED
>
> rcu_sync_is_idle() //returns false down_write(rw_sem)
>
> __percpu_up_read()
>
> [L] task = rcu_dereference(w->task) //NULL
>
> smp_rmb() [S] w->task = current
>
> smp_mb()
>
> [L] readers_active_check() //fails
> schedule()
>
> [S] __this_cpu_dec(read_count)
>
> Since load of task can result in NULL, it can lead to missed wakeup
> in rcuwait_wake_up(). Above sequence violated the following constraint
> in rcuwait_wake_up():
>
> WAIT WAKE
> [S] tsk = current [S] cond = true
> MB (A) MB (B)
> [L] cond [L] tsk
>
> This can happen as smp_rmb() in rcuwait_wake_up() will provide ordering
> of load before barrier with load and store after barrier for arm64
> architecture. Here the requirement is to order store before smp_rmb()
> with load after the smp_rmb().
>
> For the usage of rcuwait_wake_up() in __percpu_up_read() full barrier
> (smp_mb) is required to complete the constraint of rcuwait_wake_up().
>
> Signed-off-by: Prateek Sood <prsood@xxxxxxxxxxxxxx>
> Acked-by: Davidlohr Bueso <dbueso@xxxxxxx>

It looks like Peter has already queued this, c.f.,

https://git.kernel.org/pub/scm/linux/kernel/git/peterz/queue.git/commit/?h=locking/core&id=73685b8af253cf32b1b41b3045f2828c6fb2439e

with a modified changelog and my Reviewed-by (that I confirm).

I can't tell how/when this is going to be upstreamed (guess via -tip),
Peter?

Andrea


>
> ---
> kernel/exit.c | 4 ++--
> 1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/kernel/exit.c b/kernel/exit.c
> index ac1a814..696e0e1 100644
> --- a/kernel/exit.c
> +++ b/kernel/exit.c
> @@ -298,7 +298,7 @@ void rcuwait_wake_up(struct rcuwait *w)
> /*
> * Order condition vs @task, such that everything prior to the load
> * of @task is visible. This is the condition as to why the user called
> - * rcuwait_trywake() in the first place. Pairs with set_current_state()
> + * rcuwait_wake_up() in the first place. Pairs with set_current_state()
> * barrier (A) in rcuwait_wait_event().
> *
> * WAIT WAKE
> @@ -306,7 +306,7 @@ void rcuwait_wake_up(struct rcuwait *w)
> * MB (A) MB (B)
> * [L] cond [L] tsk
> */
> - smp_rmb(); /* (B) */
> + smp_mb(); /* (B) */
>
> /*
> * Avoid using task_rcu_dereference() magic as long as we are careful,
> --
> Qualcomm India Private Limited, on behalf of Qualcomm Innovation Center, Inc.,
> is a member of Code Aurora Forum, a Linux Foundation Collaborative Project.
>