Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp574440pxb; Tue, 19 Oct 2021 08:40:10 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxGxJNzecuvu8eaR/YYprte+eKiA298oVvH+UILIHRE0FKLa3aJWsD687m5acv/e9QeMlwk X-Received: by 2002:a17:907:75c1:: with SMTP id jl1mr39861141ejc.288.1634658009811; Tue, 19 Oct 2021 08:40:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1634658009; cv=none; d=google.com; s=arc-20160816; b=HU/Gmy9q2XSDMPUTjZgieXOKcHjIY2qpnyq73/iesjJpn3oP7bg49InQURvWT8LIVO gmF3arsC2rdK/MhfiH9B7bUyH9GDAwzInU5JrHNsVOCdNllMFhGUV2wGtmH6QzO3EL7G Sa2yKfDZdE6pXWusYKzVISWm++Zu8fm+RcHiVXR2UE7lszWF8qT5WQu9pRbcSM5PmFqT TmVTQE0aGIqVsm1893Sw4ugZV2YO4IIFfH823/KXlBPAIDUztKNuHxDcdLK852OXEeiP i453AY9xQi0Xo4SalaK48o/TpERT5BHdDxSKdMixGsGnGqpVygc+I1FAXY7CQebeDNgV +PlQ== 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=Kiuf8lNuMz3AF0XWv9PfZ84rabRQqrQddA3gryuhXg0=; b=BqQbCFfhDVU3SnrO8sP6TtB3kPADASmGLTtBMteeURwyjdZHBMe7ClkeVarFD6oJTQ 2ycffXlAYKYgFls0KSMHFsZCo1DQwdVbHfosO7XjUAUTcvjrYrutcJTQ18CkuJQdmnJ3 3IHvzyUsoAc7bA6iSe3THSD7idxCPIZPBbJI2v+Jb/g5MNfrdtP9ABLDnhV+sQ1V+Y3T I2Regkgu4SafWnh9KT8me4KBE5aKxNIaXa5VTCq2u5tE3fV+xufd/0/txM1RcJkZ/BWv OOHV6Ze9q/DwGVTTff/Xyb/qKDhYroPg7iBhiDMzCCOl2461c7sX+d2mmPvL/VdGV6AL Q6uw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=WCPjl7+R; dkim=neutral (no key) header.i=@linutronix.de; 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 gt25si25341265ejb.780.2021.10.19.08.39.46; Tue, 19 Oct 2021 08:40: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=WCPjl7+R; dkim=neutral (no key) header.i=@linutronix.de; 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 S232249AbhJSPhy (ORCPT + 99 others); Tue, 19 Oct 2021 11:37:54 -0400 Received: from Galois.linutronix.de ([193.142.43.55]:46502 "EHLO galois.linutronix.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231574AbhJSPhv (ORCPT ); Tue, 19 Oct 2021 11:37:51 -0400 Date: Tue, 19 Oct 2021 15:35:36 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1634657737; 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=Kiuf8lNuMz3AF0XWv9PfZ84rabRQqrQddA3gryuhXg0=; b=WCPjl7+RMe/FbiMfHY7BzgANp1Q2bCQToEWQ+aoVhLeHl9u3nDYe1XeHnYdjuryYlQqUhI fJF5EbWAIsFJ915PnKQ1JUPiWj4MawwCBTLrixTOL/OQKPImjcCI5lPazrKJ7qHBspaMBs HcNWGWEwCT6VbIglq34eUhh0TWanSePMqg7g3me3522E4wsuM4ue3/FFQ9olX4gXvpUsal gEsRCmk+gvcE+fzmB3UD3tfyOPKzA2Vf2SeSMYbTLJn1mEzMh4ADuChA2WdlJomTQASI+u 3shKtEEkXl7USfHk3WiafRY/1G1ejgejMi9jGrCdBPFrtxSS7cj+C/QNb8FO8g== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1634657737; 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=Kiuf8lNuMz3AF0XWv9PfZ84rabRQqrQddA3gryuhXg0=; b=vyfVKJk+tKrQTWAURYQUggbGLNvo9Ly9bIJUMbj9xDiCiAASOif8nM7wS8V31A1j4ADaw+ T2j8O/m8A0un1GBg== 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: Remove rcu_read_{,un}lock() for preempt_{dis,en}able() Cc: Yanfei Xu , "Peter Zijlstra (Intel)" , Waiman Long , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20211013134154.1085649-2-yanfei.xu@windriver.com> References: <20211013134154.1085649-2-yanfei.xu@windriver.com> MIME-Version: 1.0 Message-ID: <163465773649.25758.15688942192146286296.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: 6c2787f2a20ceb49c98bd06f7dad1589eed1c951 Gitweb: https://git.kernel.org/tip/6c2787f2a20ceb49c98bd06f7dad1589eed1c951 Author: Yanfei Xu AuthorDate: Wed, 13 Oct 2021 21:41:52 +08:00 Committer: Peter Zijlstra CommitterDate: Tue, 19 Oct 2021 17:27:06 +02:00 locking: Remove rcu_read_{,un}lock() for preempt_{dis,en}able() preempt_disable/enable() is equal to RCU read-side crital section, and the spinning codes in mutex and rwsem could ensure that the preemption is disabled. So let's remove the unnecessary rcu_read_lock/unlock for saving some cycles in hot codes. Signed-off-by: Yanfei Xu Signed-off-by: Peter Zijlstra (Intel) Acked-by: Waiman Long Link: https://lore.kernel.org/r/20211013134154.1085649-2-yanfei.xu@windriver.com --- kernel/locking/mutex.c | 22 +++++++++++++++------- kernel/locking/rwsem.c | 14 +++++++++----- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/kernel/locking/mutex.c b/kernel/locking/mutex.c index 2fede72..db19136 100644 --- a/kernel/locking/mutex.c +++ b/kernel/locking/mutex.c @@ -351,13 +351,16 @@ bool mutex_spin_on_owner(struct mutex *lock, struct task_struct *owner, { bool ret = true; - rcu_read_lock(); + lockdep_assert_preemption_disabled(); + while (__mutex_owner(lock) == owner) { /* * Ensure we emit the owner->on_cpu, dereference _after_ - * checking lock->owner still matches owner. If that fails, - * owner might point to freed memory. If it still matches, - * the rcu_read_lock() ensures the memory stays valid. + * checking lock->owner still matches owner. And we already + * disabled preemption which is equal to the RCU read-side + * crital section in optimistic spinning code. Thus the + * task_strcut structure won't go away during the spinning + * period */ barrier(); @@ -377,7 +380,6 @@ bool mutex_spin_on_owner(struct mutex *lock, struct task_struct *owner, cpu_relax(); } - rcu_read_unlock(); return ret; } @@ -390,19 +392,25 @@ static inline int mutex_can_spin_on_owner(struct mutex *lock) struct task_struct *owner; int retval = 1; + lockdep_assert_preemption_disabled(); + if (need_resched()) return 0; - rcu_read_lock(); + /* + * We already disabled preemption which is equal to the RCU read-side + * crital section in optimistic spinning code. Thus the task_strcut + * structure won't go away during the spinning period. + */ owner = __mutex_owner(lock); /* * As lock holder preemption issue, we both skip spinning if task is not * on cpu or its cpu is preempted */ + if (owner) retval = owner->on_cpu && !vcpu_is_preempted(task_cpu(owner)); - rcu_read_unlock(); /* * If lock->owner is not set, the mutex has been released. Return true diff --git a/kernel/locking/rwsem.c b/kernel/locking/rwsem.c index 29eea50..884aa08 100644 --- a/kernel/locking/rwsem.c +++ b/kernel/locking/rwsem.c @@ -635,7 +635,10 @@ static inline bool rwsem_can_spin_on_owner(struct rw_semaphore *sem) } preempt_disable(); - rcu_read_lock(); + /* + * Disable preemption is equal to the RCU read-side crital section, + * thus the task_strcut structure won't go away. + */ owner = rwsem_owner_flags(sem, &flags); /* * Don't check the read-owner as the entry may be stale. @@ -643,7 +646,6 @@ static inline bool rwsem_can_spin_on_owner(struct rw_semaphore *sem) if ((flags & RWSEM_NONSPINNABLE) || (owner && !(flags & RWSEM_READER_OWNED) && !owner_on_cpu(owner))) ret = false; - rcu_read_unlock(); preempt_enable(); lockevent_cond_inc(rwsem_opt_fail, !ret); @@ -671,12 +673,13 @@ rwsem_spin_on_owner(struct rw_semaphore *sem) unsigned long flags, new_flags; enum owner_state state; + lockdep_assert_preemption_disabled(); + owner = rwsem_owner_flags(sem, &flags); state = rwsem_owner_state(owner, flags); if (state != OWNER_WRITER) return state; - rcu_read_lock(); for (;;) { /* * When a waiting writer set the handoff flag, it may spin @@ -694,7 +697,9 @@ rwsem_spin_on_owner(struct rw_semaphore *sem) * Ensure we emit the owner->on_cpu, dereference _after_ * checking sem->owner still matches owner, if that fails, * owner might point to free()d memory, if it still matches, - * the rcu_read_lock() ensures the memory stays valid. + * our spinning context already disabled preemption which is + * equal to RCU read-side crital section ensures the memory + * stays valid. */ barrier(); @@ -705,7 +710,6 @@ rwsem_spin_on_owner(struct rw_semaphore *sem) cpu_relax(); } - rcu_read_unlock(); return state; }