Received: by 2002:a05:6358:c692:b0:131:369:b2a3 with SMTP id fe18csp3903880rwb; Sun, 30 Jul 2023 19:40:56 -0700 (PDT) X-Google-Smtp-Source: APBJJlHb5rwzPt2liwLAz/APR4gh8iidvgtNLM4bN2S91qvoEwcanF7rvKAVDNUIaR8JShnfLLYJ X-Received: by 2002:a05:6402:7c6:b0:521:aeba:c6c0 with SMTP id u6-20020a05640207c600b00521aebac6c0mr7183570edy.36.1690771256212; Sun, 30 Jul 2023 19:40:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690771256; cv=none; d=google.com; s=arc-20160816; b=LAW80ZriDc2uD7tbSLEePhKanOWr2VABZuL19mart3SPx2zBqQZaGnVucpMZbzT3k/ D+oyxbcwIT4RmqMkv1FO8lnGcK96869hekMeYwMxm0p/zEHpFw4inFuneXY47EL4A+M+ MDAObfVhUu3gr6OI+cIxsFsv9R1cZymXJsndwZ70/wYS/xiQw1s2WQaV8ruVrF6gzP3W jljVzguKf7Juu09ANw1YTmEHwuK4VgDWkrdrPJp4+hhUTvOfUo6s/2x8cFVKtaefyO5l HRIiydfvOt5Vx/6mVsiju19RSFA+USsR4chqTvbIOvFWDFB+Tf/vjDqJNyftER/WYEzW uOkA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=7C/YDvLDqM5oBWvLugTMwG6osGe28lZQbLSKuSrexFM=; fh=ynD8gm6gJQXSez8FdW3HqBskq1VQGujBF8+c/DnQoyg=; b=OygHbr7V/7hYvHjuDYw8XV9xqUlZAjPHNV93vN9vRFpZah2UQLtCHGulM2Zpn6RSzi dzGxHXcEP0gBV8DphwUAVdH7rzhND+ySXuVmlBVQZJFUcE6CkEIK/ikRHfwp37NNoQpb nIK1tMEwgbyOFev4mfygXSFx2hba+0su5h2nzssiSWSlR2+DKWxdslBoiEivYpMxxmiq YS0UimhEl64xalLS+qM470WXkB5N0UBN03cll3hRIZ3jToZBOzWdS99WEfy8v7v9crDV p0/HjuW98Rg7I72bc4m0/mzgEG68qNT96jaNqFGDIyM77ku1bp039bTg7uUuKsdNoG5M H+2A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=G6PYP+nC; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id m10-20020a50ef0a000000b005223814710csi6506721eds.3.2023.07.30.19.40.32; Sun, 30 Jul 2023 19:40:56 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=G6PYP+nC; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229661AbjGaCdY (ORCPT + 99 others); Sun, 30 Jul 2023 22:33:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37224 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229608AbjGaCdX (ORCPT ); Sun, 30 Jul 2023 22:33:23 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3AFDCE78; Sun, 30 Jul 2023 19:33:22 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 8440760E0A; Mon, 31 Jul 2023 02:33:21 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6B6D6C433C8; Mon, 31 Jul 2023 02:33:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1690770800; bh=0gAtJOQpVMD8BwO73WEaaztsqkDnNRkgEfGHbRe8E4w=; h=From:To:Cc:Subject:Date:From; b=G6PYP+nCCFGX5k/Hi5Jr75omCG6AH0IALpAYUKDAbmdOdLbSoz/oEoCh2GaopoYJA b4So3ErnwWMVWBWd/RtKCT2QAL5tjhLvwXpmwIKWtbXOm778YrJtOxLUDPLM8wyrY9 LY37LeLPQFbJrspWfmGzgLb5qKB3z2/OvFHOe5kzJGll3iu+mMAS0tZr4Pwc3WYWy4 X/Yk1+kNU/5RC3BLEVkhgYmXdPMd8jvH/hgJzD6JLQi0EE5TcqOdwEx/tMBua3PB92 WOJvUGpzDAiv0fc4Hri9UdC+z1JAs75/diQ1JRd/fnElYCXG3XqJmtF0WdpeYpsb1w RdFH53r+vERNA== From: guoren@kernel.org To: guoren@kernel.org, David.Laight@ACULAB.COM, will@kernel.org, peterz@infradead.org, mingo@redhat.com, longman@redhat.com Cc: linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org, Guo Ren Subject: [PATCH V2] asm-generic: ticket-lock: Optimize arch_spin_value_unlocked Date: Sun, 30 Jul 2023 22:33:08 -0400 Message-Id: <20230731023308.3748432-1-guoren@kernel.org> X-Mailer: git-send-email 2.36.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Guo Ren The arch_spin_value_unlocked would cause an unnecessary memory access to the contended value. Although it won't cause a significant performance gap in most architectures, the arch_spin_value_unlocked argument contains enough information. Thus, remove unnecessary atomic_read in arch_spin_value_unlocked(). The caller of arch_spin_value_unlocked() could benefit from this change. Currently, the only caller is lockref. Signed-off-by: Guo Ren Cc: Waiman Long Cc: David Laight Cc: Peter Zijlstra Signed-off-by: Guo Ren --- Changelog V2: - Fixup commit log with Waiman advice. - Add Waiman comment in the commit msg. --- 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 fdfebcb050f4..90803a826ba0 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 */ -- 2.36.1