From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.0 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A54B6C43387 for ; Tue, 18 Dec 2018 20:35:39 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 6966620675 for ; Tue, 18 Dec 2018 20:35:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727127AbeLRUfi (ORCPT ); Tue, 18 Dec 2018 15:35:38 -0500 Received: from mx1.redhat.com ([209.132.183.28]:34398 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727185AbeLRUfc (ORCPT ); Tue, 18 Dec 2018 15:35:32 -0500 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 4F77E58E45; Tue, 18 Dec 2018 20:35:32 +0000 (UTC) Received: from llong.remote.csb (dhcp-17-223.bos.redhat.com [10.18.17.223]) by smtp.corp.redhat.com (Postfix) with ESMTP id DC4277A5FC; Tue, 18 Dec 2018 20:35:30 +0000 (UTC) Subject: Re: [PATCH v4] sched/wake_q: Reduce reference counting for special users To: Davidlohr Bueso Cc: Peter Zijlstra , Yongji Xie , mingo@redhat.com, will.deacon@arm.com, linux-kernel@vger.kernel.org, Xie Yongji , zhangyu31@baidu.com, liuqi16@baidu.com, yuanlinsi01@baidu.com, nixun@baidu.com, lilin24@baidu.com, andrea.parri@amarulasolutions.com References: <20181129221714.GF11632@hirez.programming.kicks-ass.net> <20181217113718.GB4900@worktop.programming.kicks-ass.net> <20181217205310.pvwcryyaqlrzmaex@linux-r8p5> <20181218131031.GJ15430@hirez.programming.kicks-ass.net> <20181218131419.GD16284@hirez.programming.kicks-ass.net> <20181218185443.g5jzinm3vnrkznmm@linux-r8p5> <20181218193009.ytzz5rlrsh27rjre@linux-r8p5> <20181218193957.cjx34nn5tku2eaps@linux-r8p5> <20181218195352.7orq3upiwfdbrdne@linux-r8p5> From: Waiman Long Openpgp: preference=signencrypt Autocrypt: addr=longman@redhat.com; prefer-encrypt=mutual; keydata= xsFNBFgsZGsBEAC3l/RVYISY3M0SznCZOv8aWc/bsAgif1H8h0WPDrHnwt1jfFTB26EzhRea XQKAJiZbjnTotxXq1JVaWxJcNJL7crruYeFdv7WUJqJzFgHnNM/upZuGsDIJHyqBHWK5X9ZO jRyfqV/i3Ll7VIZobcRLbTfEJgyLTAHn2Ipcpt8mRg2cck2sC9+RMi45Epweu7pKjfrF8JUY r71uif2ThpN8vGpn+FKbERFt4hW2dV/3awVckxxHXNrQYIB3I/G6mUdEZ9yrVrAfLw5M3fVU CRnC6fbroC6/ztD40lyTQWbCqGERVEwHFYYoxrcGa8AzMXN9CN7bleHmKZrGxDFWbg4877zX 0YaLRypme4K0ULbnNVRQcSZ9UalTvAzjpyWnlnXCLnFjzhV7qsjozloLTkZjyHimSc3yllH7 VvP/lGHnqUk7xDymgRHNNn0wWPuOpR97J/r7V1mSMZlni/FVTQTRu87aQRYu3nKhcNJ47TGY evz/U0ltaZEU41t7WGBnC7RlxYtdXziEn5fC8b1JfqiP0OJVQfdIMVIbEw1turVouTovUA39 Qqa6Pd1oYTw+Bdm1tkx7di73qB3x4pJoC8ZRfEmPqSpmu42sijWSBUgYJwsziTW2SBi4hRjU h/Tm0NuU1/R1bgv/EzoXjgOM4ZlSu6Pv7ICpELdW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qa2UdVJalc8B9NrAnLiyRsg72Nurwzvknv7anSgIkL+doXDaG21DgCYTD wGA5uquIgb8p3/ENgYpDPrsZ72CxVC2NEJjJwwnRBStjJOGQX4lV1uhN1XsZjBbRHdKF2W9g weim8xU= Organization: Red Hat Message-ID: <772085e9-35f3-1c32-1b87-f101cbc5f8f7@redhat.com> Date: Tue, 18 Dec 2018 15:35:30 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.9.1 MIME-Version: 1.0 In-Reply-To: <20181218195352.7orq3upiwfdbrdne@linux-r8p5> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Content-Language: en-US X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Tue, 18 Dec 2018 20:35:32 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 12/18/2018 02:53 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 > --- > > - Changes from v3: fixed wake_q_add_safe. While previous version >  had been tested with a bootup, the failed cmpxchg path obviously >  hadn't been exercised.  Sorry about the noise. > > include/linux/sched/wake_q.h |  4 +-- > kernel/futex.c               |  3 +-- > kernel/locking/rwsem-xadd.c  |  4 +-- > kernel/sched/core.c          | 60 > ++++++++++++++++++++++++++++++++------------ > 4 files changed, 48 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..be977df66a21 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,55 @@ 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. > + */ > +void wake_q_add(struct wake_q_head *head, struct task_struct *task) > +{ > +    if (__wake_q_add(head, task)) > +        get_task_struct(task); > +} > + > +/** > + * wake_q_add_safe() - safely 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. > + * > + * This function is essentially a task-safe equivalent to > wake_q_add(). Callers > + * that already hold reference to @task can call the 'safe' version > and trust > + * wake_q to do the right thing depending whether or not the @task is > already > + * queued for wakeup. > + */ > +void wake_q_add_safe(struct wake_q_head *head, struct task_struct *task) > +{ > +    if (!__wake_q_add(head, task)) > +        put_task_struct(task); > } > > void wake_up_q(struct wake_q_head *head) Acked-by: Waiman Long