Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp3952300yba; Wed, 17 Apr 2019 01:09:03 -0700 (PDT) X-Google-Smtp-Source: APXvYqxPoOvWm9sISc0s+FfP53A7EQ2Dak1wYbavPOzl9GW9C5ogXq9wNYSm4SVT7lk9Aa2TtYD1 X-Received: by 2002:a65:6282:: with SMTP id f2mr1536384pgv.152.1555488543545; Wed, 17 Apr 2019 01:09:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1555488543; cv=none; d=google.com; s=arc-20160816; b=vsGirEfWAYkQV93bRW3NMF2AIvasUTyn0UiyzMdS8NJHs8wQetOi0mi1QHkb1oz5oc +bMSfhxDluVy6GAiFtOvUe04CMryZTM6CZi0B6YFHoGjcymRgVMMjtRh5HlZC6R7Obik LVAQhH56TWsxnzoZ4OsJj/UxMoI4DocOnvj7THNgLeW2pKJsit0tVfDZ/0mnNj/VQvFC aJXYkJaWF/XCQzlcMuCzQU+3k04WCkuYfpjbOBC4p/UKp0AwQ5wBNs0Ncq255IS3bI0A DayA4+E57qzEwd2yKnBmcl9Cqj0+fXrJ7v6UjYIUIwhQQ5fB4d5HajtDA+AHL9/aFx6g oUJg== 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-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=wSQxgLC5Hy48gnSZgUERxB5HKeEI4hy3A6LfcwqNs7Q=; b=N8Smgo8db0OUC87tF4PbABe7J8w4EtQFwQ0ItpvvTGioozCwwqeAcdoh1mGgxnq377 pYzy50crdiHVvbZ4NYb81PN5cz+SZbx/l+LL6F7E9GYVNywbW0SdzTZdnKrPqHUSaBnt Of1iaqUOt/6VdmkP3+kO15/lIkbrP5m4BTWpCmHL18qFuDHhWF+a2s8c4RVonYb92atx 1anJuY+kJTH2lihUaKAzBPnHsAby8+fkcrnw7wTEVr7/8sQJocJLmQsGyqy6gX+vjZhA hXijavwe/ED7OQ9Mvi50HpOJZeYOaeJB1FnWdCRg7waOb3R12xa6xa7p0ocrRYcA7WBv Zx/g== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=merlin.20170209 header.b="Gai/umrG"; 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 c15si33111087pls.358.2019.04.17.01.08.48; Wed, 17 Apr 2019 01:09:03 -0700 (PDT) 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; dkim=fail header.i=@infradead.org header.s=merlin.20170209 header.b="Gai/umrG"; 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 S1731221AbfDQIGF (ORCPT + 99 others); Wed, 17 Apr 2019 04:06:05 -0400 Received: from merlin.infradead.org ([205.233.59.134]:54332 "EHLO merlin.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728373AbfDQIGF (ORCPT ); Wed, 17 Apr 2019 04:06:05 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=merlin.20170209; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=wSQxgLC5Hy48gnSZgUERxB5HKeEI4hy3A6LfcwqNs7Q=; b=Gai/umrGbb4i8h2P4E1YcNJgE sHB7ZjWHi/WyZqmecdm1+rnhdbpf8Qii3Lkh7FuFheSxRO6YPB6Yrrfjnk4ZHa96cZfBTIVjN7otE 2xlRLzi0YMEb0InNgFqVJs7s+RAzv3YswWJLH66gtR3EutrLiDBr2FU1tWLS/MP9SgrA+5AbVNWtI IN7lXEKdDoSmwsLxpQY69fIWyyBOHAqahrhdQ+vdklWO5Xo4sXBCRw+kh5xRITOiQYHGGdYm6hUwj EeLITIrbsSgtOw0RtFGB2fxIpkeiwPgVi4xB0in6oDWrVBj/HlohghCpqv4nQS9/IcTCcbB6UerYJ ujO05Xarg==; Received: from j217100.upc-j.chello.nl ([24.132.217.100] helo=hirez.programming.kicks-ass.net) by merlin.infradead.org with esmtpsa (Exim 4.90_1 #2 (Red Hat Linux)) id 1hGfZo-0006KG-B1; Wed, 17 Apr 2019 08:05:52 +0000 Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id E9D2829B52932; Wed, 17 Apr 2019 10:05:49 +0200 (CEST) Date: Wed, 17 Apr 2019 10:05:49 +0200 From: Peter Zijlstra To: Waiman Long Cc: Ingo Molnar , Will Deacon , Thomas Gleixner , linux-kernel@vger.kernel.org, x86@kernel.org, Davidlohr Bueso , Linus Torvalds , Tim Chen , huang ying Subject: Re: [PATCH v4 07/16] locking/rwsem: Implement lock handoff to prevent lock starvation Message-ID: <20190417080549.GA4038@hirez.programming.kicks-ass.net> References: <20190413172259.2740-1-longman@redhat.com> <20190413172259.2740-8-longman@redhat.com> <20190416154937.GL12232@hirez.programming.kicks-ass.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Apr 16, 2019 at 02:16:11PM -0400, Waiman Long wrote: > >> @@ -608,56 +687,63 @@ __rwsem_down_write_failed_common(struct rw_semaphore *sem, int state) > >> */ > >> waiter.task = current; > >> waiter.type = RWSEM_WAITING_FOR_WRITE; > >> + waiter.timeout = jiffies + RWSEM_WAIT_TIMEOUT; > >> > >> raw_spin_lock_irq(&sem->wait_lock); > >> > >> /* account for this before adding a new element to the list */ > >> + wstate = list_empty(&sem->wait_list) ? WRITER_FIRST : WRITER_NOT_FIRST; > >> > >> list_add_tail(&waiter.list, &sem->wait_list); > >> > >> /* we're now waiting on the lock */ > >> + if (wstate == WRITER_NOT_FIRST) { > >> count = atomic_long_read(&sem->count); > >> > >> /* > >> + * If there were already threads queued before us and: > >> + * 1) there are no no active locks, wake the front > >> + * queued process(es) as the handoff bit might be set. > >> + * 2) there are no active writers and some readers, the lock > >> + * must be read owned; so we try to wake any read lock > >> + * waiters that were queued ahead of us. > >> */ > >> + if (!RWSEM_COUNT_LOCKED(count)) > >> + __rwsem_mark_wake(sem, RWSEM_WAKE_ANY, &wake_q); > >> + else if (!(count & RWSEM_WRITER_MASK) && > >> + (count & RWSEM_READER_MASK)) > >> __rwsem_mark_wake(sem, RWSEM_WAKE_READERS, &wake_q); > > Does the above want to be something like: > > > > if (!(count & RWSEM_WRITER_LOCKED)) { > > __rwsem_mark_wake(sem, (count & RWSEM_READER_MASK) ? > > RWSEM_WAKE_READERS : > > RWSEM_WAKE_ANY, &wake_q); > > } > > Yes. > > >> + else > >> + goto wait; > >> > >> + /* > >> + * The wakeup is normally called _after_ the wait_lock > >> + * is released, but given that we are proactively waking > >> + * readers we can deal with the wake_q overhead as it is > >> + * similar to releasing and taking the wait_lock again > >> + * for attempting rwsem_try_write_lock(). > >> + */ > >> + wake_up_q(&wake_q); > > Hurmph.. the reason we do wake_up_q() outside of wait_lock is such that > > those tasks don't bounce on wait_lock. Also, it removes a great deal of > > hold-time from wait_lock. > > > > So I'm not sure I buy your argument here. > > > > Actually, we don't want to release the wait_lock, do wake_up_q() and > acquire the wait_lock again as the state would have been changed. I > didn't change the comment on this patch, but will reword it to discuss that. I don't understand, we've queued ourselves, we're on the list, we're not first. How would dropping the lock to try and kick waiters before us be a problem? Sure, once we re-acquire the lock we have to re-avaluate @wstate to see if we're first now or not, but we need to do that anyway. So what is wrong with the below? --- a/include/linux/sched/wake_q.h +++ b/include/linux/sched/wake_q.h @@ -51,6 +51,11 @@ static inline void wake_q_init(struct wa head->lastp = &head->first; } +static inline bool wake_q_empty(struct wake_q_head *head) +{ + return head->first == WAKE_Q_TAIL; +} + 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); --- a/kernel/locking/rwsem.c +++ b/kernel/locking/rwsem.c @@ -700,25 +700,22 @@ __rwsem_down_write_failed_common(struct * must be read owned; so we try to wake any read lock * waiters that were queued ahead of us. */ - if (!(count & RWSEM_LOCKED_MASK)) - __rwsem_mark_wake(sem, RWSEM_WAKE_ANY, &wake_q); - else if (!(count & RWSEM_WRITER_MASK) && - (count & RWSEM_READER_MASK)) - __rwsem_mark_wake(sem, RWSEM_WAKE_READERS, &wake_q); - else + if (count & RWSEM_WRITER_LOCKED) goto wait; - /* - * The wakeup is normally called _after_ the wait_lock - * is released, but given that we are proactively waking - * readers we can deal with the wake_q overhead as it is - * similar to releasing and taking the wait_lock again - * for attempting rwsem_try_write_lock(). - */ - wake_up_q(&wake_q); - /* - * Reinitialize wake_q after use. - */ - wake_q_init(&wake_q); + + __rwsem_mark_wake(sem, (count & RWSEM_READER_MASK) ? + RWSEM_WAKE_READERS : + RWSEM_WAKE_ANY, &wake_q); + + if (!wake_q_empty(&wake_q)) { + raw_spin_unlock_irq(&sem->wait_lock); + wake_up_q(&wake_q); + /* used again, reinit */ + wake_q_init(&wake_q); + raw_spin_lock_irq(&sem->wait_lock); + if (rwsem_waiter_is_first(sem, &waiter)) + wstate = WRITER_FIRST; + } } else { count = atomic_long_add_return(RWSEM_FLAG_WAITERS, &sem->count); }