Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp6386927imu; Mon, 21 Jan 2019 08:04:15 -0800 (PST) X-Google-Smtp-Source: ALg8bN4vp5PCr9dE/5YL19Q5vBubdEYKzMUidnvrfcBdIke00QDMjmCnrf3g2UJFHVbFZWAgCAh+ X-Received: by 2002:a63:790e:: with SMTP id u14mr28656079pgc.452.1548086655313; Mon, 21 Jan 2019 08:04:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1548086655; cv=none; d=google.com; s=arc-20160816; b=yCGGfwuACkcV0hQH368d+rfxlgdC8RIioAq+1X3IRHzunUaNOLJBeCZvGvYIaeNFIW kPXLPR0xp20zL5sXtWsSXnNtrapyyWDNPMM3yXj8MHaGnk8UsL3WmD9RNmpTsCfefZ7D tFhMWooAqxRwHxjYDJ1iCyU2d2/EFefRmCv7BaWy2Osa7rUJ+hXU1ChIeZbJrUjPzzw8 ZYxmVGRIlHrBwzBrBcwgU1MqfU/mr9sL+o+pd1A6myXm+FSC1fsep5TgnjlSx63nuzBg wqlCsXGF9ro/KMawpymPkqZQkH+fBUWpvnycNBX3qv8qt5U1wYrFJZcmdeOURNgl9av4 fFjA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-transfer-encoding:content-disposition:mime-version :references:message-id:subject:cc:to:from:date; bh=Js0DfkF1VEMMX49809yfTr3Cs7jo5nFvebVViQ2ykhU=; b=nBfKzNfX5PrWQMc92F3Izs1EnQuKZrQlgOCeRNwK8cLr5TD73+McbBpnQiT/Z3SnZS FMiogJRYjNDfNgmdOVBWmu3psnGryBiGsguKZxwbjsX+JkfVM1TwVd1aY2cyOrXjhV+s bLgS4kkDqRDjDDAx5JtRiuv4hYXZ7wTAu+3sro/4n45RVuiYzBSYefvmwHBNCM9PABRJ 2Eg+6NI6k1YEngSd9b1cOw8hG7aVUYMOynCF28TJE8rLGfd4Imo/LMBPbS9jx+egzbqv jNSFeHgAQISi0eZmBse0d8pZREr3p7G2CKSXfDLLORbAQx/2yS/dWk88hdW8uJSTDBaB sQ2w== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b124si13395287pfg.47.2019.01.21.08.03.56; Mon, 21 Jan 2019 08:04:15 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729469AbfAUQCb convert rfc822-to-8bit (ORCPT + 99 others); Mon, 21 Jan 2019 11:02:31 -0500 Received: from mx2.suse.de ([195.135.220.15]:39390 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1728761AbfAUQCb (ORCPT ); Mon, 21 Jan 2019 11:02:31 -0500 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay1.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id 53432AF8A; Mon, 21 Jan 2019 16:02:29 +0000 (UTC) Date: Mon, 21 Jan 2019 08:02:18 -0800 From: Davidlohr Bueso To: Waiman Long 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 Subject: Re: [PATCH v4] sched/wake_q: Reduce reference counting for special users Message-ID: <20190121160218.rakg7vvhlx4pqpvu@linux-r8p5> References: <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> <772085e9-35f3-1c32-1b87-f101cbc5f8f7@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1; format=flowed Content-Disposition: inline Content-Transfer-Encoding: 8BIT In-Reply-To: <772085e9-35f3-1c32-1b87-f101cbc5f8f7@redhat.com> User-Agent: NeoMutt/20180323 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi - considering that the wake_q patches were picked up for tip/urgent, can this one make it in as well? Thanks, Davidlohr On Tue, 18 Dec 2018, Waiman Long wrote: >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 >