Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp572561pxb; Tue, 19 Oct 2021 08:38:09 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxb7rZtwUesb797ENY7kJmfYNJj6nE7+nTzxaiyWEvIB0PfMhDdy35AX1mdX0NrBbaYmPGg X-Received: by 2002:a17:906:5a47:: with SMTP id my7mr37528591ejc.128.1634657889099; Tue, 19 Oct 2021 08:38:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1634657889; cv=none; d=google.com; s=arc-20160816; b=xzIoEfss36WC3n6rQRwQtkYWUNE2x6Qhe+lYcV8UVe/rK6pA/5tRJDNvKtYiO4xPFU S6AlbTCuEh4s2LOu/vXpedr1MHGgKV2BcNfWb/oAh4YvQd5IW8EOCRYwS1BAwq3UJEDZ kdWf71v9I1IiVIey7kz0k6D/k7V8i6oLx6UzFV95hN2kioZc2czduNEOBLkB2Is9OorD oAsXiaP+wycgEpHnnQVYEw3YDHZLePGgCvGKUxhXODguIaxd1xK31kTIWCkITpSoEn8x 999dSmky4sb/wxGMM7KkE/x5+KgOCSlv0ntkFTdynUK6q7PZn45ns57IEOqxZjC1pCL/ itGA== 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=x/P7g0Zh6Qy/byTGY6asgJ/NyEUYUC3qrvGNB9kE778=; b=Njc/CkIZ6pQSG8A4ItY+eIj7JIYRcea6F2fpgOciFHHk4LpKqRzY8WmkDXLuIzAonm 8BN0u61Ot1/xzIsl1r2fFJpoZR/2TZUu5Z/UVPFQDMfJsuCFfer53hAovzMdg+WkhyHD Z7HanYaR1w82AyQyRh72sHTQ+AbS67jc7f+VOON3B4eVzNVDUs3LUL3VLx0+Ob9WF+UG 7FQqHvSm5G9PpQXBGGyVOIKGQnMx4Q1N3m7Af++KLK4a231WbPxyDVb4G+3uwJA0Dc9m gnMJ6X56tv5GvdqU95/qBD2wGW+DXSbDXiszkC/tNFjfec6FThx5RvY3iynMZmmYnfME Iycw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=eIHSKHbb; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=1o02xzke; 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 r2si25002712edx.162.2021.10.19.08.37.44; Tue, 19 Oct 2021 08:38:09 -0700 (PDT) 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=eIHSKHbb; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=1o02xzke; 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 S232508AbhJSPh4 (ORCPT + 99 others); Tue, 19 Oct 2021 11:37:56 -0400 Received: from Galois.linutronix.de ([193.142.43.55]:46514 "EHLO galois.linutronix.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230097AbhJSPhw (ORCPT ); Tue, 19 Oct 2021 11:37:52 -0400 Date: Tue, 19 Oct 2021 15:35:37 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1634657738; 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=x/P7g0Zh6Qy/byTGY6asgJ/NyEUYUC3qrvGNB9kE778=; b=eIHSKHbbZ6ibvJUSyIZ47g7olx29HMXSbtJsHP2vHeNpFJRAgLj9uXCLi8zRO8xUsM1Z+C OZrzOVHXuU+lyNplG6RwmZ0cOnlGYMsUTPtYBIbbRH2/vweUW97pI5VORDeZZH5l8RINpl QuzlLxWXzuRU+yZoyLA+cfoxCSVwLbYPpY8kg0Ur5Kz0SIYEec/oBxCjerIRHl4K2iQcOg IFNYlz6UAroxD+vNnni05LGACsjLGSiItxiV5pJUHsrpJ2/FQZG+bnAAbDcQnmAAKVkPfl l8icmZJwOXSF1QmxQOKixxP/QyAyKwe5cy0zzvgNpdqbvHiuKW47DbtYbV2fCQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1634657738; 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=x/P7g0Zh6Qy/byTGY6asgJ/NyEUYUC3qrvGNB9kE778=; b=1o02xzkeKcawS/gwxws1o6VSjc0+9HcV1ZnFrLZeSxNJTTLjwyV7rippurONeJZA+Dpqwf +Qrv2evyhGLrrmBA== From: "tip-bot2 for Yanfei Xu" 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: Disable preemption for spinning region Cc: Yanfei Xu , "Peter Zijlstra (Intel)" , Waiman Long , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20211013134154.1085649-3-yanfei.xu@windriver.com> References: <20211013134154.1085649-3-yanfei.xu@windriver.com> MIME-Version: 1.0 Message-ID: <163465773747.25758.16516293859176730628.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: 7cdacc5f52d68a9370f182c844b5b3e6cc975cc1 Gitweb: https://git.kernel.org/tip/7cdacc5f52d68a9370f182c844b5b3e6cc975cc1 Author: Yanfei Xu AuthorDate: Wed, 13 Oct 2021 21:41:53 +08:00 Committer: Peter Zijlstra CommitterDate: Tue, 19 Oct 2021 17:27:05 +02:00 locking/rwsem: Disable preemption for spinning region The spinning region rwsem_spin_on_owner() should not be preempted, however the rwsem_down_write_slowpath() invokes it and don't disable preemption. Fix it by adding a pair of preempt_disable/enable(). Signed-off-by: Yanfei Xu [peterz: Fix CONFIG_RWSEM_SPIN_ON_OWNER=n build] Signed-off-by: Peter Zijlstra (Intel) Acked-by: Waiman Long Link: https://lore.kernel.org/r/20211013134154.1085649-3-yanfei.xu@windriver.com --- kernel/locking/rwsem.c | 53 +++++++++++++++++++++++------------------ 1 file changed, 30 insertions(+), 23 deletions(-) diff --git a/kernel/locking/rwsem.c b/kernel/locking/rwsem.c index 000e8d5..29eea50 100644 --- a/kernel/locking/rwsem.c +++ b/kernel/locking/rwsem.c @@ -577,6 +577,24 @@ static inline bool rwsem_try_write_lock(struct rw_semaphore *sem, return true; } +/* + * The rwsem_spin_on_owner() function returns the following 4 values + * depending on the lock owner state. + * OWNER_NULL : owner is currently NULL + * OWNER_WRITER: when owner changes and is a writer + * OWNER_READER: when owner changes and the new owner may be a reader. + * OWNER_NONSPINNABLE: + * when optimistic spinning has to stop because either the + * owner stops running, is unknown, or its timeslice has + * been used up. + */ +enum owner_state { + OWNER_NULL = 1 << 0, + OWNER_WRITER = 1 << 1, + OWNER_READER = 1 << 2, + OWNER_NONSPINNABLE = 1 << 3, +}; + #ifdef CONFIG_RWSEM_SPIN_ON_OWNER /* * Try to acquire write lock before the writer has been put on wait queue. @@ -632,23 +650,6 @@ static inline bool rwsem_can_spin_on_owner(struct rw_semaphore *sem) return ret; } -/* - * The rwsem_spin_on_owner() function returns the following 4 values - * depending on the lock owner state. - * OWNER_NULL : owner is currently NULL - * OWNER_WRITER: when owner changes and is a writer - * OWNER_READER: when owner changes and the new owner may be a reader. - * OWNER_NONSPINNABLE: - * when optimistic spinning has to stop because either the - * owner stops running, is unknown, or its timeslice has - * been used up. - */ -enum owner_state { - OWNER_NULL = 1 << 0, - OWNER_WRITER = 1 << 1, - OWNER_READER = 1 << 2, - OWNER_NONSPINNABLE = 1 << 3, -}; #define OWNER_SPINNABLE (OWNER_NULL | OWNER_WRITER | OWNER_READER) static inline enum owner_state @@ -878,12 +879,11 @@ static inline bool rwsem_optimistic_spin(struct rw_semaphore *sem) static inline void clear_nonspinnable(struct rw_semaphore *sem) { } -static inline int +static inline enum owner_state rwsem_spin_on_owner(struct rw_semaphore *sem) { - return 0; + return OWNER_NONSPINNABLE; } -#define OWNER_NULL 1 #endif /* @@ -1095,9 +1095,16 @@ wait: * In this case, we attempt to acquire the lock again * without sleeping. */ - if (wstate == WRITER_HANDOFF && - rwsem_spin_on_owner(sem) == OWNER_NULL) - goto trylock_again; + if (wstate == WRITER_HANDOFF) { + enum owner_state owner_state; + + preempt_disable(); + owner_state = rwsem_spin_on_owner(sem); + preempt_enable(); + + if (owner_state == OWNER_NULL) + goto trylock_again; + } /* Block until there are no active lockers. */ for (;;) {