Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp4202029pxu; Wed, 9 Dec 2020 10:46:36 -0800 (PST) X-Google-Smtp-Source: ABdhPJwkc2N77xlTwdnU9IYh6t08zpa0XpxwOf494jiwkAcHlpq/GUePcMdK2cHxcwqlrMuZUQlH X-Received: by 2002:a17:906:c007:: with SMTP id e7mr3142920ejz.511.1607539596085; Wed, 09 Dec 2020 10:46:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1607539596; cv=none; d=google.com; s=arc-20160816; b=XcYuRLi6i1ecAJXp4gttLi9BvK3K/VOYIpDSL1L4eeMKekWzKC8stm1mGG8MZNS/ct odeYOHVXisoOpJpxpL3pqkFcmYOcpffH8MqL4b2qQGo34rwTmgI7mvbVnJaVRFDServI LmPD/xoAxhD8SKEx0LyJy6UyOeppUhCRjENGsJS0KU7COGznz28wCKZB0MFAfFASNa0I GTXSJTI8nmgF9xiUQC/dDXwd3H7zii9V9H3XNGFNHin1TPmnbSG/AFwjXSQDK9IxskTm mplbtLGX+jLCf1bY35iu1fT9soXN2l9ELbkK2/XyaHfrksBw7RW48VYCDAfEONa8iy0N zhZA== 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=NdCm8CFK3OGqOnzEdvDg1PfZ7u0cKfw5AJJh6hUhnmA=; b=BAbQBCAhTdTS12W9+3cZFDSy6roWTPNlbrb385FKJM/DJ2uEMmnEiCUupMMxW7coHN y7IL3kEcPtDVeQa8+iDmGHobHd+19WRi9S1PQqWz9QesQ1k8vdGP0vRw5GooIPsLlztj 5HKZ4HQo9xLYiULWADfDb1CsrhK3Cc5YJzd5DB/gbUQzN9rANgGdCkLmzH3s6+LNGkvQ gPSGIq3JFIYWZpXCEprEcOWeaOcjQXLFWOtkezilRlfC4zh0EXnE+PzfsMF6qlXVIyQu cjHyPBq1/SJQRaJEJWYGAncauqv5uTaR/JMWytQJHBZnRukoqeCqxkFYVeyzIQr+J1VE XDfw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=D2uZ9n9f; dkim=neutral (no key) header.i=@linutronix.de header.b=AcFRmX1B; 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 f12si1478579ejl.311.2020.12.09.10.46.11; Wed, 09 Dec 2020 10:46:36 -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=D2uZ9n9f; dkim=neutral (no key) header.i=@linutronix.de header.b=AcFRmX1B; 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 S1733303AbgLISmv (ORCPT + 99 others); Wed, 9 Dec 2020 13:42:51 -0500 Received: from Galois.linutronix.de ([193.142.43.55]:48336 "EHLO galois.linutronix.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728404AbgLISja (ORCPT ); Wed, 9 Dec 2020 13:39:30 -0500 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=1607539127; 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=NdCm8CFK3OGqOnzEdvDg1PfZ7u0cKfw5AJJh6hUhnmA=; b=D2uZ9n9fZ/xYHioJTDqK+hS15K5DoDvqDC5NDYYlxknfdgMF6qG4DutfMm9hADcAyE9k9P ekT/Npam2BgGPpvf+4MneLSj67cGPLD2ON84bSUAhkXZBiEUXFRot4tDwc3MygfhHT3nd8 Nb9iKAZgXZC7EEvTO+7A2jkcW4xZLBu9evrWED2vaFAXhQFkPFjXx6pjb9Pdgk8UbQjdqr P6n6k6KQKBd1cHaNUCBqjdadNfknH0QWiRMh4fV1BceuvsuOu/IgYX1TQxM1+i1uyYrp6K 8xLk4OY62lFZV8ofoLIooRe9KmbLCVWMnG1qi2WtftaJ3P8LCjNwKIrMR9jf5A== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1607539127; 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=NdCm8CFK3OGqOnzEdvDg1PfZ7u0cKfw5AJJh6hUhnmA=; b=AcFRmX1BL9VbcQbtxWtNr/ExIVVFiRlX76V7VpQUbxZQvWZ6N/rb9nMR4Az3XYEEP6kZv5 vgOJdM116wLueACw== 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: Enable reader optimistic lock stealing Cc: Waiman Long , "Peter Zijlstra (Intel)" , Davidlohr Bueso , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20201121041416.12285-4-longman@redhat.com> References: <20201121041416.12285-4-longman@redhat.com> MIME-Version: 1.0 Message-ID: <160753912756.3364.6893166069488987740.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: 1a728dff855a318bb58bcc1259b1826a7ad9f0bd Gitweb: https://git.kernel.org/tip/1a728dff855a318bb58bcc1259b1826a7ad9f0bd Author: Waiman Long AuthorDate: Fri, 20 Nov 2020 23:14:14 -05:00 Committer: Peter Zijlstra CommitterDate: Wed, 09 Dec 2020 17:08:48 +01:00 locking/rwsem: Enable reader optimistic lock stealing If the optimistic spinning queue is empty and the rwsem does not have the handoff or write-lock bits set, it is actually not necessary to call rwsem_optimistic_spin() to spin on it. Instead, it can steal the lock directly as its reader bias is in the count already. If it is the first reader in this state, it will try to wake up other readers in the wait queue. With this patch applied, the following were the lock event counts after rebooting a 2-socket system and a "make -j96" kernel rebuild. rwsem_opt_rlock=4437 rwsem_rlock=29 rwsem_rlock_steal=19 So lock stealing represents about 0.4% of all the read locks acquired in the slow path. Signed-off-by: Waiman Long Signed-off-by: Peter Zijlstra (Intel) Reviewed-by: Davidlohr Bueso Link: https://lkml.kernel.org/r/20201121041416.12285-4-longman@redhat.com --- kernel/locking/lock_events_list.h | 1 + kernel/locking/rwsem.c | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/kernel/locking/lock_events_list.h b/kernel/locking/lock_events_list.h index 239039d..270a0d3 100644 --- a/kernel/locking/lock_events_list.h +++ b/kernel/locking/lock_events_list.h @@ -63,6 +63,7 @@ LOCK_EVENT(rwsem_opt_nospin) /* # of disabled optspins */ LOCK_EVENT(rwsem_opt_norspin) /* # of disabled reader-only optspins */ LOCK_EVENT(rwsem_opt_rlock2) /* # of opt-acquired 2ndary read locks */ LOCK_EVENT(rwsem_rlock) /* # of read locks acquired */ +LOCK_EVENT(rwsem_rlock_steal) /* # of read locks by lock stealing */ LOCK_EVENT(rwsem_rlock_fast) /* # of fast read locks acquired */ LOCK_EVENT(rwsem_rlock_fail) /* # of failed read lock acquisitions */ LOCK_EVENT(rwsem_rlock_handoff) /* # of read lock handoffs */ diff --git a/kernel/locking/rwsem.c b/kernel/locking/rwsem.c index c055f4b..ba5e239 100644 --- a/kernel/locking/rwsem.c +++ b/kernel/locking/rwsem.c @@ -976,6 +976,12 @@ static inline bool rwsem_reader_phase_trylock(struct rw_semaphore *sem, } return false; } + +static inline bool rwsem_no_spinners(struct rw_semaphore *sem) +{ + return !osq_is_locked(&sem->osq); +} + #else static inline bool rwsem_can_spin_on_owner(struct rw_semaphore *sem, unsigned long nonspinnable) @@ -996,6 +1002,11 @@ static inline bool rwsem_reader_phase_trylock(struct rw_semaphore *sem, return false; } +static inline bool rwsem_no_spinners(sem) +{ + return false; +} + static inline int rwsem_spin_on_owner(struct rw_semaphore *sem, unsigned long nonspinnable) { @@ -1027,6 +1038,22 @@ rwsem_down_read_slowpath(struct rw_semaphore *sem, long count, int state) goto queue; /* + * Reader optimistic lock stealing + * + * We can take the read lock directly without doing + * rwsem_optimistic_spin() if the conditions are right. + * Also wake up other readers if it is the first reader. + */ + if (!(count & (RWSEM_WRITER_LOCKED | RWSEM_FLAG_HANDOFF)) && + rwsem_no_spinners(sem)) { + rwsem_set_reader_owned(sem); + lockevent_inc(rwsem_rlock_steal); + if (rcnt == 1) + goto wake_readers; + return sem; + } + + /* * Save the current read-owner of rwsem, if available, and the * reader nonspinnable bit. */ @@ -1048,6 +1075,7 @@ rwsem_down_read_slowpath(struct rw_semaphore *sem, long count, int state) * Wake up other readers in the wait list if the front * waiter is a reader. */ +wake_readers: if ((atomic_long_read(&sem->count) & RWSEM_FLAG_WAITERS)) { raw_spin_lock_irq(&sem->wait_lock); if (!list_empty(&sem->wait_list))