From patchwork Thu Sep 21 08:21:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: tip-bot2 for Thomas Gleixner X-Patchwork-Id: 142831 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:172:b0:3f2:4152:657d with SMTP id h50csp5053954vqi; Thu, 21 Sep 2023 11:29:37 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGL8rR+p8XkMO9UHSC1/aKLUs6W7+zKMq8ctIPb86LR7Rks5xI3AwIr1yXsPUoY7HNIksBO X-Received: by 2002:a05:6a00:1593:b0:68b:e29c:b61 with SMTP id u19-20020a056a00159300b0068be29c0b61mr7503060pfk.19.1695320977672; Thu, 21 Sep 2023 11:29:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695320977; cv=none; d=google.com; s=arc-20160816; b=Ud/KFo37BZugKw4vE6oG6o3zwouAwrc78UHRn2RpBx/umDTTni989fsGNjvIyn2rF2 Q1k8u0Pj+LEZDW2UMrcKXnLqRWoBz215DT3CwYXSMocKWwsPdU9fr/NXg007Ufgp6tRy o3QPtiYOqcNLDvwAEt5bXAeG1mDE96RisLZEe63HPyuweJfVXUM6WLfARD+4qkJqz8w2 emp3WVKNNZUnsiaD7Yk82uIFrKyGDtWjIQPSbluWVvgdOQD2m59rzOz3WGYM3i3FGG0D XBpAvpM55tT7Nmb6O+x0hTG4EfyJv/SK6c1S2dodGOD2o7QIFuwwleICdJxMrG2izt7l tQEA== 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=wiigUf4+jV0yAF+Zmw7mPWMC69tUoz6kH5ojPyKhrv8=; fh=hw7LRm6Wg98dn8znObwI+Sm0CttIBj0RbnEQzWRjxds=; b=xvE74+ig7bfElkXJ/v7JWGDfSSGEHIvj6DbI/B6YeinaJ2k2u0O57kxBN+dBtYD0eW j7uTtDZybLQBVyOEJIzsMldT3kE34eac/fnVjQYBFQZAssSSHWZ2+l4JuC/iAKm9ZH+f B5V0dLHJxo53O4UgiEAcsy3u3B9M5CaW/lk3044S8DrnSW+GIOCLCLVMitsBfCpcTReQ M3CnnNcO4JvDtISnQDYxZAeBPU7W6rHKr7jXZgf2+y/1VqVpy3s2Fl/asKs/CN0a72Pp 5UXF5xe9dEcosYwIOjMj2JtOHuuH0LdMJibpzsKSefbbU2V7EqVmG4Yl2UJs/tMS+I8N g6oQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=ZOvmRcTt; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id u8-20020a056a00158800b006901387b09bsi2078738pfk.344.2023.09.21.11.29.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Sep 2023 11:29:37 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=ZOvmRcTt; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id 8F10583328F4; Thu, 21 Sep 2023 11:13:35 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229718AbjIUSNO (ORCPT + 29 others); Thu, 21 Sep 2023 14:13:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47720 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230493AbjIUSM2 (ORCPT ); Thu, 21 Sep 2023 14:12:28 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B8FD551017; Thu, 21 Sep 2023 10:15:29 -0700 (PDT) Date: Thu, 21 Sep 2023 08:21:03 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1695284464; 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=wiigUf4+jV0yAF+Zmw7mPWMC69tUoz6kH5ojPyKhrv8=; b=ZOvmRcTt7EpDy0oaAAiRjC62np4g/P8zNuyvJvyUQl9MArMWMvc5NSarBmvczIPfW+vCPy EkuBd9UdxHXH5dNT+Bjo+m5xgzI0ItmfB84sEUgEd7kKNFYoBLhHab5p+0IARIvpjq/Saj g0CzySsuzCckutq5ohPzh6PHnGcIfy+OUN8gcYzbq4/OaGoLTGZfw6H7Jks137xkqkiPzk SuATBJg+K9056WfKPwNp5aoOGYmwTjZroHodw0ep9M7ovcyxRmYeJdgZxYHpy1yQWBUMMH fTjunoY3vdxV7ax1+VvtAVgoQV8E1rbjzH5oCZGdmtQHhcjerXoneXjSfKRFrg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1695284464; 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=wiigUf4+jV0yAF+Zmw7mPWMC69tUoz6kH5ojPyKhrv8=; b=/FNGDqgDPriMlRRWDBCrnBTBYd2N7ljHrxjJU2PpmxI+7MqR9DVBzDssCgj3EHZEu++onC aL6baZyXJK4DPMDA== From: "tip-bot2 for Guo Ren" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: locking/core] asm-generic: ticket-lock: Optimize arch_spin_value_unlocked() Cc: Guo Ren , Guo Ren , Ingo Molnar , Waiman Long , Will Deacon , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20230908154339.3250567-1-guoren@kernel.org> References: <20230908154339.3250567-1-guoren@kernel.org> MIME-Version: 1.0 Message-ID: <169528446324.27769.8480622781969702147.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails X-Spam-Status: No, score=0.3 required=5.0 tests=DATE_IN_PAST_06_12,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Thu, 21 Sep 2023 11:13:35 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1777672889340382259 X-GMAIL-MSGID: 1777672889340382259 The following commit has been merged into the locking/core branch of tip: Commit-ID: c6f4a90022524d06f6d9de323b1757031dcf0c26 Gitweb: https://git.kernel.org/tip/c6f4a90022524d06f6d9de323b1757031dcf0c26 Author: Guo Ren AuthorDate: Fri, 08 Sep 2023 11:43:39 -04:00 Committer: Ingo Molnar CommitterDate: Thu, 21 Sep 2023 10:17:00 +02:00 asm-generic: ticket-lock: Optimize arch_spin_value_unlocked() The arch_spin_value_unlocked() of ticket-lock would cause the compiler to generate inefficient asm code in riscv architecture because of unnecessary memory access to the contended value. Before the patch: void lockref_get(struct lockref *lockref) { 78: fd010113 add sp,sp,-48 7c: 02813023 sd s0,32(sp) 80: 02113423 sd ra,40(sp) 84: 03010413 add s0,sp,48 0000000000000088 <.LBB296>: CMPXCHG_LOOP( 88: 00053783 ld a5,0(a0) After the patch: void lockref_get(struct lockref *lockref) { CMPXCHG_LOOP( 78: 00053783 ld a5,0(a0) After the patch, the lockref_get() could get in a fast path instead of the function's prologue. This is because ticket lock complex logic would limit compiler optimization for the spinlock fast path, and qspinlock won't. The caller of arch_spin_value_unlocked() could benefit from this change. Currently, the only caller is lockref. Signed-off-by: Guo Ren Signed-off-by: Guo Ren Signed-off-by: Ingo Molnar Acked-by: Waiman Long Acked-by: Will Deacon Link: https://lore.kernel.org/r/20230908154339.3250567-1-guoren@kernel.org --- include/asm-generic/spinlock.h | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/include/asm-generic/spinlock.h b/include/asm-generic/spinlock.h index fdfebcb..90803a8 100644 --- a/include/asm-generic/spinlock.h +++ b/include/asm-generic/spinlock.h @@ -68,11 +68,18 @@ static __always_inline void arch_spin_unlock(arch_spinlock_t *lock) smp_store_release(ptr, (u16)val + 1); } +static __always_inline int arch_spin_value_unlocked(arch_spinlock_t lock) +{ + u32 val = lock.counter; + + return ((val >> 16) == (val & 0xffff)); +} + static __always_inline int arch_spin_is_locked(arch_spinlock_t *lock) { - u32 val = atomic_read(lock); + arch_spinlock_t val = READ_ONCE(*lock); - return ((val >> 16) != (val & 0xffff)); + return !arch_spin_value_unlocked(val); } static __always_inline int arch_spin_is_contended(arch_spinlock_t *lock) @@ -82,11 +89,6 @@ static __always_inline int arch_spin_is_contended(arch_spinlock_t *lock) return (s16)((val >> 16) - (val & 0xffff)) > 1; } -static __always_inline int arch_spin_value_unlocked(arch_spinlock_t lock) -{ - return !arch_spin_is_locked(&lock); -} - #include #endif /* __ASM_GENERIC_SPINLOCK_H */