X-Received: by 2002:a17:90a:4306:b0:1b9:80b3:7a3d with SMTP id q6-20020a17090a430600b001b980b37a3dmr14739933pjg.66.1645715919448; Thu, 24 Feb 2022 07:18:39 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1645715919; cv=none; d=google.com; s=arc-20160816; b=ba+ZQsJW31x5udiODLUfkVBGknsD7BC4XgvuJdBOYfRfY37jVMNAlB66SQrq0pkF3a j7agMe45YxmHtTAVehLIN2MmGQLaWg5YiV+rtxDuJE7K9KBOYUVAbluVihsGYridcJKj UQ16fyOJKdeLYfJL39eLrD91YUlbDZH5y6Ww9Jajj4LKuUszv8M2BtgqmFS4vXK7ch+a 9AuCHPaU4gOqOSzIFXph3lTka1J8ScLOM8dHoV8HtqHUr+MwhOAK9GSfAXK7J43i9PmM c9QbAeA6ZiqYHq7EL3SoCKVIyGH5r30sN+LCNYKeFweTsd+hOr44rAxH6MdaGVhOD6TV C3Kw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date; bh=tdDzopduzMOAWtP9DjszS3iEvDOZEosY/oZIRNB5KAQ=; b=Fpg1ZPemQ7vJeONHVInZrZ68OXUmCpezthC5NShMcm6rbQnx8LPZYE2fgLEg/nLg1c 5B4qYRCYriDwbD0jPhuaUhAau4/B0y1q2YUAnXlR2NXgJldpHeV6bwOR5dZ+V/Iq9omW +xaT/afQxkBcytiFhBgckUwPra7AZrl/BV9zkVfVTNPzu2llr8D+i3wI0rHV5ZA4ij2D K7I4QbF8VmdZ5pCIOKNG5oDRCuCBqLMC3w43siaTHIHDN2gUJeQIwhjbB7ASpLCaSyHY DW6/ngWp1LWTNKVIMp4nF62C0F65HnpQVAcZ7Aoyzx15UNtPTbX73vs1CZez0q1gyFOc SAMA== 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id b9si697637plh.334.2022.02.24.07.18.15; Thu, 24 Feb 2022 07:18:39 -0800 (PST) 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233424AbiBXK2c (ORCPT + 99 others); Thu, 24 Feb 2022 05:28:32 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40076 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232742AbiBXK2b (ORCPT ); Thu, 24 Feb 2022 05:28:31 -0500 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id B5ED21B8FE2 for ; Thu, 24 Feb 2022 02:28:01 -0800 (PST) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 80CB01509; Thu, 24 Feb 2022 02:28:01 -0800 (PST) Received: from lakrids (usa-sjc-imap-foss1.foss.arm.com [10.121.207.14]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 164A73F70D; Thu, 24 Feb 2022 02:27:59 -0800 (PST) Date: Thu, 24 Feb 2022 10:27:54 +0000 From: Mark Rutland To: Junru Shen Cc: Will Deacon , Peter Zijlstra , Boqun Feng , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H . Peter Anvin" , linux-kernel@vger.kernel.org Subject: Re: [PATCH] atomic: Put the fetching of the old value into the loop when doing atomic CAS Message-ID: References: <20220224082438.580191-1-hhusjrsjr@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220224082438.580191-1-hhusjrsjr@gmail.com> X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_HI, 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 On Thu, Feb 24, 2022 at 04:24:38PM +0800, Junru Shen wrote: > Put the acquisition of the expected value inside the loop to prevent > an infinite loop when it does not match. I suspect you've found this by inspection, as I don't beleive this can happen. See below. > Signed-off-by: Junru Shen > --- > arch/x86/include/asm/atomic64_64.h | 9 ++++++--- > 1 file changed, 6 insertions(+), 3 deletions(-) > > diff --git a/arch/x86/include/asm/atomic64_64.h b/arch/x86/include/asm/atomic64_64.h > index 7886d0578..3df04c44c 100644 > --- a/arch/x86/include/asm/atomic64_64.h > +++ b/arch/x86/include/asm/atomic64_64.h > @@ -207,9 +207,10 @@ static inline void arch_atomic64_and(s64 i, atomic64_t *v) > > static inline s64 arch_atomic64_fetch_and(s64 i, atomic64_t *v) > { > - s64 val = arch_atomic64_read(v); > + s64 val; > > do { > + val = arch_atomic64_read(v); > } while (!arch_atomic64_try_cmpxchg(v, &val, val & i)); ^^^^ See this bit above? ----------------------------' If arch_atomic64_try_cmpxchg() fails, it writes the value in memory back to this address, so it has already done the equivalent of arch_atomic64_read(v). If you're seing this go wrong, it implies that arch_atomic64_try_cmpxchg() is being mis-compiled, so please provide an example and the disassembly. Likewise for the other instances below. Thanks, Mark. > return val; > } > @@ -225,9 +226,10 @@ static inline void arch_atomic64_or(s64 i, atomic64_t *v) > > static inline s64 arch_atomic64_fetch_or(s64 i, atomic64_t *v) > { > - s64 val = arch_atomic64_read(v); > + s64 val; > > do { > + val = arch_atomic64_read(v); > } while (!arch_atomic64_try_cmpxchg(v, &val, val | i)); > return val; > } > @@ -243,9 +245,10 @@ static inline void arch_atomic64_xor(s64 i, atomic64_t *v) > > static inline s64 arch_atomic64_fetch_xor(s64 i, atomic64_t *v) > { > - s64 val = arch_atomic64_read(v); > + s64 val; > > do { > + val = arch_atomic64_read(v); > } while (!arch_atomic64_try_cmpxchg(v, &val, val ^ i)); > return val; > } > -- > 2.30.2 >