Re: [PATCH v2] sched/wake_q: Reduce reference counting for special users

From: Waiman Long
Date: Tue Dec 18 2018 - 14:17:13 EST


On 12/18/2018 01:54 PM, Davidlohr Bueso wrote:
> Some users, specifically futexes and rwsems, required fixes
> that allowed the callers to be safe when wakeups occur before
> they are expected by wake_up_q(). Such scenarios also play
> games and rely on reference counting, and until now were
> pivoting on wake_q doing it. With the wake_q_add() call being
> moved down, this can no longer be the case. As such we end up
> with a a double task refcounting overhead; and these callers
> care enough about this (being rather core-ish).
>
> This patch introduces a wake_q_add_safe() call that serves
> for callers that have already done refcounting and therefore the
> task is 'safe' from wake_q point of view (int that it requires
> reference throughout the entire queue/>wakeup cycle). In the one
> case it has internal reference counting, in the other case it
> consumes the reference counting.
>
> Signed-off-by: Davidlohr Bueso <dbueso@xxxxxxx>
> ---
>
> Changes from v1:
> - Simplify some of the interface allowing callers to not worry
> Â about the return value of the add operation. This makes caller
> Â refcouting two levels/asymm by putting the refcouting in the
> Â wake_q's hands, but the benefits are worth it.
>
> Applies on peterz' queue tree.
>
> include/linux/sched/wake_q.h |Â 4 +--
> kernel/futex.cÂÂÂÂÂÂÂÂÂÂÂÂÂÂ |Â 3 +--
> kernel/locking/rwsem-xadd.c | 4 +--
> kernel/sched/core.cÂÂÂÂÂÂÂÂÂ | 62
> ++++++++++++++++++++++++++++++++------------
> 4 files changed, 50 insertions(+), 23 deletions(-)
>
> diff --git a/include/linux/sched/wake_q.h b/include/linux/sched/wake_q.h
> index 545f37138057..ad826d2a4557 100644
> --- a/include/linux/sched/wake_q.h
> +++ b/include/linux/sched/wake_q.h
> @@ -51,8 +51,8 @@ static inline void wake_q_init(struct wake_q_head
> *head)
> ÂÂÂÂhead->lastp = &head->first;
> }
>
> -extern void wake_q_add(struct wake_q_head *head,
> -ÂÂÂÂÂÂÂÂÂÂÂÂÂÂ struct task_struct *task);
> +extern void wake_q_add(struct wake_q_head *head, struct task_struct
> *task);
> +extern void wake_q_add_safe(struct wake_q_head *head, struct
> task_struct *task);
> extern void wake_up_q(struct wake_q_head *head);
>
> #endif /* _LINUX_SCHED_WAKE_Q_H */
> diff --git a/kernel/futex.c b/kernel/futex.c
> index d14971f6ed3d..6218d98f649b 100644
> --- a/kernel/futex.c
> +++ b/kernel/futex.c
> @@ -1402,8 +1402,7 @@ static void mark_wake_futex(struct wake_q_head
> *wake_q, struct futex_q *q)
> ÂÂÂÂ * Queue the task for later wakeup for after we've released
> ÂÂÂÂ * the hb->lock. wake_q_add() grabs reference to p.
> ÂÂÂÂ */
> -ÂÂÂ wake_q_add(wake_q, p);
> -ÂÂÂ put_task_struct(p);
> +ÂÂÂ wake_q_add_safe(wake_q, p);
> }
>
> /*
> diff --git a/kernel/locking/rwsem-xadd.c b/kernel/locking/rwsem-xadd.c
> index 50d9af615dc4..fbe96341beee 100644
> --- a/kernel/locking/rwsem-xadd.c
> +++ b/kernel/locking/rwsem-xadd.c
> @@ -211,9 +211,7 @@ static void __rwsem_mark_wake(struct rw_semaphore
> *sem,
> ÂÂÂÂÂÂÂÂ * Ensure issuing the wakeup (either by us or someone else)
> ÂÂÂÂÂÂÂÂ * after setting the reader waiter to nil.
> ÂÂÂÂÂÂÂÂ */
> -ÂÂÂÂÂÂÂ wake_q_add(wake_q, tsk);
> -ÂÂÂÂÂÂÂ /* wake_q_add() already take the task ref */
> -ÂÂÂÂÂÂÂ put_task_struct(tsk);
> +ÂÂÂÂÂÂÂ wake_q_add_safe(wake_q, tsk);
> ÂÂÂÂ}
>
> ÂÂÂÂadjustment = woken * RWSEM_ACTIVE_READ_BIAS - adjustment;
> diff --git a/kernel/sched/core.c b/kernel/sched/core.c
> index d740d7a3608d..ef85c6ad4caa 100644
> --- a/kernel/sched/core.c
> +++ b/kernel/sched/core.c
> @@ -396,19 +396,7 @@ static bool set_nr_if_polling(struct task_struct *p)
> #endif
> #endif
>
> -/**
> - * wake_q_add() - queue a wakeup for 'later' waking.
> - * @head: the wake_q_head to add @task to
> - * @task: the task to queue for 'later' wakeup
> - *
> - * Queue a task for later wakeup, most likely by the wake_up_q() call
> in the
> - * same context, _HOWEVER_ this is not guaranteed, the wakeup can come
> - * instantly.
> - *
> - * This function must be used as-if it were wake_up_process(); IOW
> the task
> - * must be ready to be woken at this location.
> - */
> -void wake_q_add(struct wake_q_head *head, struct task_struct *task)
> +static bool __wake_q_add(struct wake_q_head *head, struct task_struct
> *task)
> {
> ÂÂÂÂstruct wake_q_node *node = &task->wake_q;
>
> @@ -422,15 +410,57 @@ void wake_q_add(struct wake_q_head *head, struct
> task_struct *task)
> ÂÂÂÂ */
> ÂÂÂÂsmp_mb__before_atomic();
> ÂÂÂÂif (unlikely(cmpxchg_relaxed(&node->next, NULL, WAKE_Q_TAIL)))
> -ÂÂÂÂÂÂÂ return;
> -
> -ÂÂÂ get_task_struct(task);
> +ÂÂÂÂÂÂÂ return false;
>
> ÂÂÂÂ/*
> ÂÂÂÂ * The head is context local, there can be no concurrency.
> ÂÂÂÂ */
> ÂÂÂÂ*head->lastp = node;
> ÂÂÂÂhead->lastp = &node->next;
> +ÂÂÂ return true;
> +}
> +
> +/**
> + * wake_q_add() - queue a wakeup for 'later' waking.
> + * @head: the wake_q_head to add @task to
> + * @task: the task to queue for 'later' wakeup
> + *
> + * Queue a task for later wakeup, most likely by the wake_up_q() call
> in the
> + * same context, _HOWEVER_ this is not guaranteed, the wakeup can come
> + * instantly.
> + *
> + * This function must be used as-if it were wake_up_process(); IOW
> the task
> + * must be ready to be woken at this location.
> + *
> + * The @task will be refcounted upon a

Are you missing some text here?

-Longman