Received: by 2002:a05:6358:c692:b0:131:369:b2a3 with SMTP id fe18csp4314266rwb; Mon, 31 Jul 2023 05:13:26 -0700 (PDT) X-Google-Smtp-Source: APBJJlHahJcD/VnKMihp1vq2xjP3T0+FI8cdpsF8LUNqkY+mjh7202bmdfTHFcabB7TmOkbz3reR X-Received: by 2002:aa7:d5d9:0:b0:51d:f0fd:bc80 with SMTP id d25-20020aa7d5d9000000b0051df0fdbc80mr10800986eds.17.1690805606005; Mon, 31 Jul 2023 05:13:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690805605; cv=none; d=google.com; s=arc-20160816; b=k+qpU97jsdSzTCJS5DY8Rsgphyuj3DqgjkRajETvAAbxhsqoT/yKWAYWU20jwbYbDT cwfWxF1n4nIr0zgFwF6ir93sMpRf8ZeViZwPB6rv8EXaIMf+pWGgWAs3d2pSisUfj1xs fXQeuskl5uFzH7EDRBov3ZQC2SKgXf5tjQxucecDliGIvC6G5zuzmqQG8rdHmIigj+NV yfIsoEKkfN7lQ0PlTeWJwMEmNxk18P+la2nTiGy3O/2VtMCJKugeEto7iSj3WhNIcUms FvSIEuEeAKJSe0DRAF2ep9knl7CqYtRUa+f8tQCjxf0FXgKYOi0h7X3u6p1qUYZ1kToN caVQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id; bh=5DLOWPQSviJb7sEjxA+8GAQRuYUY7ddxejMb8PTMsyo=; fh=fXDaEGS170GRS9uhKxqcpNgv2yaliyiMN+M5GP1gu0Q=; b=ppZ6/U9SIaoK4oNVjAC1Q7NQNhxqT7t+QCksNsNEjei70E2Eh3Qz+FKby7H8od6fE4 Bs2NMg9Spnt4T2xz1gwYCulY/son8V1BBAnjI2Kef9BrzGpx3EP1lenWirEd3ozHtdWH Gw5D7/4DAQTks8BsPMTaYbmHKehq3waNpqhjtbdjZIuUyY0gh+PAcaQ3UOnzG5DCRVXW rS6q/XmoxILAQtNC6A1KuzDgpVQgH0hCKVR8X/dFcwmkg5LenE23TpyvHpvaLKZePdHf 8WWVXhJZ6iCOlmRR9jyr+YbthLezk2Ny3I4Cnf9mz/uHx0PcfxygGLqADlXR1Rs0bNoo OK/w== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id w6-20020a056402128600b005227a2fd0afsi6471182edv.369.2023.07.31.05.13.01; Mon, 31 Jul 2023 05:13:25 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230368AbjGaL1c (ORCPT + 99 others); Mon, 31 Jul 2023 07:27:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38664 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230344AbjGaL1a (ORCPT ); Mon, 31 Jul 2023 07:27:30 -0400 Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id E518F125; Mon, 31 Jul 2023 04:27:24 -0700 (PDT) Received: from loongson.cn (unknown [10.20.42.170]) by gateway (Coremail) with SMTP id _____8Dxg_CamsdkXjgNAA--.31654S3; Mon, 31 Jul 2023 19:27:22 +0800 (CST) Received: from [10.20.42.170] (unknown [10.20.42.170]) by localhost.localdomain (Coremail) with SMTP id AQAAf8Cx7yOXmsdk11lCAA--.2410S3; Mon, 31 Jul 2023 19:27:19 +0800 (CST) Message-ID: Date: Mon, 31 Jul 2023 19:27:19 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.13.0 Subject: Re: [PATCH V2] asm-generic: ticket-lock: Optimize arch_spin_value_unlocked Content-Language: en-US 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 References: <20230731023308.3748432-1-guoren@kernel.org> From: bibo mao In-Reply-To: <20230731023308.3748432-1-guoren@kernel.org> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-CM-TRANSID: AQAAf8Cx7yOXmsdk11lCAA--.2410S3 X-CM-SenderInfo: xpdruxter6z05rqj20fqof0/ X-Coremail-Antispam: 1Uk129KBj93XoW7CFW5Zw13CrWDAF4rWF18Xrc_yoW8Kw1Upr 98CFs3AF47CFykZFZFyF42vr1rJwsF9r18ur90gwn2yFsrX3s5KanY9rn0vr1jk3WxKrsx XFW2gFy5uayjyFXCm3ZEXasCq-sJn29KB7ZKAUJUUUU3529EdanIXcx71UUUUU7KY7ZEXa sCq-sGcSsGvfJ3Ic02F40EFcxC0VAKzVAqx4xG6I80ebIjqfuFe4nvWSU5nxnvy29KBjDU 0xBIdaVrnRJUUUPab4IE77IF4wAFF20E14v26r1j6r4UM7CY07I20VC2zVCF04k26cxKx2 IYs7xG6rWj6s0DM7CIcVAFz4kK6r1Y6r17M28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48v e4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_JFI_Gr1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI 0_Gr0_Cr1l84ACjcxK6I8E87Iv67AKxVW8Jr0_Cr1UM28EF7xvwVC2z280aVCY1x0267AK xVW8Jr0_Cr1UM2kKe7AKxVWUAVWUtwAS0I0E0xvYzxvE52x082IY62kv0487Mc804VCY07 AIYIkI8VC2zVCFFI0UMc02F40EFcxC0VAKzVAqx4xG6I80ewAv7VC0I7IYx2IY67AKxVWU AVWUtwAv7VC2z280aVAFwI0_Jr0_Gr1lOx8S6xCaFVCjc4AY6r1j6r4UM4x0Y48IcVAKI4 8JMxk0xIA0c2IEe2xFo4CEbIxvr21lc7CjxVAaw2AFwI0_JF0_Jw1l42xK82IYc2Ij64vI r41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1l4IxYO2xFxVAFwI0_JF0_Jw1lx2IqxVAqx4xG67 AKxVWUJVWUGwC20s026x8GjcxK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r1q6r43MIIY rxkI7VAKI48JMIIF0xvE2Ix0cI8IcVAFwI0_JFI_Gr1lIxAIcVC0I7IYx2IY6xkF7I0E14 v26r1j6r4UMIIF0xvE42xK8VAvwI8IcIk0rVWUJVWUCwCI42IY6I8E87Iv67AKxVW8JVWx JwCI42IY6I8E87Iv6xkF7I0E14v26r4j6r4UJbIYCTnIWIevJa73UjIFyTuYvjxUcbAwUU UUU X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,NICE_REPLY_A, 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 在 2023/7/31 10:33, guoren@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)); > +} I do not know much about lock, will it be cached in register without memory access again like READ_ONCE or atomic_read? Regards Bibo Mao > + > 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 */