Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp4201785pxu; Wed, 9 Dec 2020 10:46:11 -0800 (PST) X-Google-Smtp-Source: ABdhPJwZahxgVPq81RygnxZSx2qNIYF4CK8SKPhKP/NCfz1KJFx+4j7l27FKASz8TAS3ClDg1hFL X-Received: by 2002:a17:906:145b:: with SMTP id q27mr3338535ejc.86.1607539570945; Wed, 09 Dec 2020 10:46:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1607539570; cv=none; d=google.com; s=arc-20160816; b=PhWkhfjMKsDpFohkCe4+f4ngiIhUQajRtXSf6o8bfOcqKcuFcH1I2l1viwBRSxgiiC FCpTCAaxyuVaWoNMsGkc7LAMMYKe+iOuPbkbOmdlvcy1m3JjSqSYvqrL5kwGZv9y9fDR pxad6Izij5mxIiCori5A8ZUbqp1N6LVSCPnNziDfUy/2sIKG6aWjvKUTH/sxwyTdACJz SOMDx1Blf6ciU0AnUdzcX3a+5ZwmoMK5AmrTXEmtbpwPtvtoeZHvKnJm0mIPslTVXO3/ p40Gmem9Wh1c42FCyaXZcvBH3Ks72vdHvQ1feC1wPKfUmAjB2kdSGn1VRGxsbavQiJ49 P/7w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:robot-unsubscribe :robot-id:message-id:mime-version:references:in-reply-to:cc:subject :to:reply-to:sender:from:dkim-signature:dkim-signature:date; bh=TcV8BJ/omVvrB3Axifuak69PnU7W6cTm9AfQijivhLY=; b=BmZvkX/qsp2WWY3y15OLJYDxKtQXmzwcRP+gSMEL7Tc2NzEPRaaJ3EDHeeE1ULMNOd dR6BhL34OIazqY3WGLslBAtoGPc6SyeIatmXwauP2pedqD12rf/J3T9RnTpJs6WM3469 2Ls/g99fHY/axb63G9EsFCzkFXvlVwQ0B30ShCtLh50EM9A0spju9rP2s3Q71CEMmn3J a8UukyzYbOzXuJSUwos4H436GDWUDVDtUH9N8UEEAc6Z3uWMD8japymCR+6w3Z21N7t6 3+tK0RMuzYWeILmKlWkAE5bpayeAtJ0Hk7Sl5VAhT8cc8Ql7MzyrHOe1zYTSNnE0ez0q o9Cw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b="zLIweO/9"; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=l0ObtSSP; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id l9si1390635edf.357.2020.12.09.10.45.44; Wed, 09 Dec 2020 10:46:10 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b="zLIweO/9"; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=l0ObtSSP; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2387413AbgLISnD (ORCPT + 99 others); Wed, 9 Dec 2020 13:43:03 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46806 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728997AbgLISja (ORCPT ); Wed, 9 Dec 2020 13:39:30 -0500 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8BE02C0613D6; Wed, 9 Dec 2020 10:38:50 -0800 (PST) Date: Wed, 09 Dec 2020 18:38:47 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1607539128; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=TcV8BJ/omVvrB3Axifuak69PnU7W6cTm9AfQijivhLY=; b=zLIweO/9cHztXuWdnhznL7bnx1ezX4xf59K8KCbSsw/gO32P4tda4R9ZzdkgSLqoDWyQmU annw/2LGybNuFiwuZ1oWKvuWLiV47G/hnMjrWb0o029/fkIxQURy3H9Q/Et/ptH5+aLvoY mU4AAcguGQ5m1F8UBfk62GGH0Vg2e9e6lhMpb2+2J231pQrW+lny5KMNCuOaSTpwoOmvPq s3FmLVNMkPF9v3CZGFUTPHzeDm6LjBvDfezG5AuRunmn4WnLh/F5tZbL2Xe1qRuH9lyaE6 QRm8P6zv0U0v7NUbRLDkTikO6ocRDc2ttMuUOf/QSozl2yxETeUoQoXS6BCQEQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1607539128; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=TcV8BJ/omVvrB3Axifuak69PnU7W6cTm9AfQijivhLY=; b=l0ObtSSPJw8gpia911IDSerAr5mYQeoGEvyA1mer4WU9KgsSCDbCJF99wELrYLy3bgdO62 dmzNXzMh9LrzSLBQ== From: "tip-bot2 for Waiman Long" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: locking/core] locking/rwsem: Prevent potential lock starvation Cc: Waiman Long , "Peter Zijlstra (Intel)" , Davidlohr Bueso , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20201121041416.12285-3-longman@redhat.com> References: <20201121041416.12285-3-longman@redhat.com> MIME-Version: 1.0 Message-ID: <160753912781.3364.7199376060300852014.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The following commit has been merged into the locking/core branch of tip: Commit-ID: 2f06f702925b512a95b95dca3855549c047eef58 Gitweb: https://git.kernel.org/tip/2f06f702925b512a95b95dca3855549c047eef58 Author: Waiman Long AuthorDate: Fri, 20 Nov 2020 23:14:13 -05:00 Committer: Peter Zijlstra CommitterDate: Wed, 09 Dec 2020 17:08:48 +01:00 locking/rwsem: Prevent potential lock starvation The lock handoff bit is added in commit 4f23dbc1e657 ("locking/rwsem: Implement lock handoff to prevent lock starvation") to avoid lock starvation. However, allowing readers to do optimistic spinning does introduce an unlikely scenario where lock starvation can happen. The lock handoff bit may only be set when a waiter is being woken up. In the case of reader unlock, wakeup happens only when the reader count reaches 0. If there is a continuous stream of incoming readers acquiring read lock via optimistic spinning, it is possible that the reader count may never reach 0 and so the handoff bit will never be asserted. One way to prevent this scenario from happening is to disallow optimistic spinning if the rwsem is currently owned by readers. If the previous or current owner is a writer, optimistic spinning will be allowed. If the previous owner is a reader but the reader count has reached 0 before, a wakeup should have been issued. So the handoff mechanism will be kicked in to prevent lock starvation. As a result, it should be OK to do optimistic spinning in this case. This patch may have some impact on reader performance as it reduces reader optimistic spinning especially if the lock critical sections are short the number of contending readers are small. Signed-off-by: Waiman Long Signed-off-by: Peter Zijlstra (Intel) Reviewed-by: Davidlohr Bueso Link: https://lkml.kernel.org/r/20201121041416.12285-3-longman@redhat.com --- kernel/locking/rwsem.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/kernel/locking/rwsem.c b/kernel/locking/rwsem.c index 5768b90..c055f4b 100644 --- a/kernel/locking/rwsem.c +++ b/kernel/locking/rwsem.c @@ -1010,16 +1010,27 @@ rwsem_spin_on_owner(struct rw_semaphore *sem, unsigned long nonspinnable) static struct rw_semaphore __sched * rwsem_down_read_slowpath(struct rw_semaphore *sem, long count, int state) { - long adjustment = -RWSEM_READER_BIAS; + long owner, adjustment = -RWSEM_READER_BIAS; + long rcnt = (count >> RWSEM_READER_SHIFT); struct rwsem_waiter waiter; DEFINE_WAKE_Q(wake_q); bool wake = false; /* + * To prevent a constant stream of readers from starving a sleeping + * waiter, don't attempt optimistic spinning if the lock is currently + * owned by readers. + */ + owner = atomic_long_read(&sem->owner); + if ((owner & RWSEM_READER_OWNED) && (rcnt > 1) && + !(count & RWSEM_WRITER_LOCKED)) + goto queue; + + /* * Save the current read-owner of rwsem, if available, and the * reader nonspinnable bit. */ - waiter.last_rowner = atomic_long_read(&sem->owner); + waiter.last_rowner = owner; if (!(waiter.last_rowner & RWSEM_READER_OWNED)) waiter.last_rowner &= RWSEM_RD_NONSPINNABLE;