Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp9929011ybi; Wed, 24 Jul 2019 12:30:33 -0700 (PDT) X-Google-Smtp-Source: APXvYqzW5bj9sDZbcpJmDr/T07XRpuOcq/IqCOf5dwR6Zj/d5DbIAnEPFOTyRLwN8oqD6A79Qsds X-Received: by 2002:a63:1c22:: with SMTP id c34mr82402066pgc.56.1563996633229; Wed, 24 Jul 2019 12:30:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1563996633; cv=none; d=google.com; s=arc-20160816; b=boTxxXpZsLahHbYsVG2JZB/kxeDXksxHmBNnKWbvWD0lKtx1qyvw7xWPS8b0ga+0pA cNcDZ7WRfI295AeTR58SwxH9Gt8o9vhPza3+8+oBHGhJCHPJmHfZ5em1VdBTuklZmhU1 gDREJwPXha3GzglSqspXkVLMA0dzvxVdNYYAOKPW4KyBtivjhJtzVdrI0sK4Db6bB1ON gZxNxlXDnPOfM41yKS9VA6GgBHY3XdzAwwGBYyCvk9OlALbqvVMMmmVHB6jm2Ff/PM32 8vFaSrm1vkd89Zx7YTvTkajxWrIm2PAKVX7VwE6s3Ztg8d1tzyYeSmgXSr1bpnYcIB4F IM8w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=zps/s5eJ7LEIuZTiZN2jy6XI9nagrKG/hW1HI9qdUe4=; b=D/9274WjKEXedBBaoqFax9wBJl5dmOKztYPj4w6d/47kifEqHRevz036eZ+UVLIjfy XOhwsdn6yoxw95+I3T7Zq7TifxcIBKo2JbMnvKroDwMX9kTTB4H+P22yXmLGsZR+cdfN 3fgKZEfxKFIAp/eJvrz1M6Lbpal/9xuo1eXE0swwpX1ffOkjUbeG/2eC2OIBHlEJOMLI sl6lQCklHRThqCN3ovAwImT2tEInoZ/8nOkzyzFuy39WJy/lXHXJT9UKuOR0Nk3DWNd/ UssTfEwE0gfBKiTh16TImENWPUPsCJGSYa7SeRh7tT4XNluuYCeVlzAwijo1YYGo7Ajw +REQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=vQHd3++i; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id j66si13564557plb.375.2019.07.24.12.30.18; Wed, 24 Jul 2019 12:30:33 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=vQHd3++i; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388200AbfGXT2N (ORCPT + 99 others); Wed, 24 Jul 2019 15:28:13 -0400 Received: from mail.kernel.org ([198.145.29.99]:46338 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387564AbfGXT2M (ORCPT ); Wed, 24 Jul 2019 15:28:12 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 6E82121951; Wed, 24 Jul 2019 19:28:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1563996490; bh=pOCUmSmq9JI+C32yE8DmJOiiIfpXoiolRStGBUkbEGo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=vQHd3++iRnA42QVonmzy1MfqaoZn+AEoH7eSLvPiavikldzVSIrJXsz5Yjv98g+Wc rkmhZH6CESn2TPe3m2a3nOP779HZxAdIVASXT+ybs+Tnti4TAVy8rlAl2IrX0JkziB dxbtiGsSE/EzzWtRIxckyeUsRQCo4OeqK0eCwhtg= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, "Peter Zijlstra (Intel)" , Linus Torvalds , Thomas Gleixner , Ingo Molnar , Sasha Levin Subject: [PATCH 5.2 114/413] x86/atomic: Fix smp_mb__{before,after}_atomic() Date: Wed, 24 Jul 2019 21:16:45 +0200 Message-Id: <20190724191743.477040643@linuxfoundation.org> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190724191735.096702571@linuxfoundation.org> References: <20190724191735.096702571@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org [ Upstream commit 69d927bba39517d0980462efc051875b7f4db185 ] Recent probing at the Linux Kernel Memory Model uncovered a 'surprise'. Strongly ordered architectures where the atomic RmW primitive implies full memory ordering and smp_mb__{before,after}_atomic() are a simple barrier() (such as x86) fail for: *x = 1; atomic_inc(u); smp_mb__after_atomic(); r0 = *y; Because, while the atomic_inc() implies memory order, it (surprisingly) does not provide a compiler barrier. This then allows the compiler to re-order like so: atomic_inc(u); *x = 1; smp_mb__after_atomic(); r0 = *y; Which the CPU is then allowed to re-order (under TSO rules) like: atomic_inc(u); r0 = *y; *x = 1; And this very much was not intended. Therefore strengthen the atomic RmW ops to include a compiler barrier. NOTE: atomic_{or,and,xor} and the bitops already had the compiler barrier. Signed-off-by: Peter Zijlstra (Intel) Cc: Linus Torvalds Cc: Peter Zijlstra Cc: Thomas Gleixner Signed-off-by: Ingo Molnar Signed-off-by: Sasha Levin --- Documentation/atomic_t.txt | 3 +++ arch/x86/include/asm/atomic.h | 8 ++++---- arch/x86/include/asm/atomic64_64.h | 8 ++++---- arch/x86/include/asm/barrier.h | 4 ++-- 4 files changed, 13 insertions(+), 10 deletions(-) diff --git a/Documentation/atomic_t.txt b/Documentation/atomic_t.txt index dca3fb0554db..65bb09a29324 100644 --- a/Documentation/atomic_t.txt +++ b/Documentation/atomic_t.txt @@ -194,6 +194,9 @@ These helper barriers exist because architectures have varying implicit ordering on their SMP atomic primitives. For example our TSO architectures provide full ordered atomics and these barriers are no-ops. +NOTE: when the atomic RmW ops are fully ordered, they should also imply a +compiler barrier. + Thus: atomic_fetch_add(); diff --git a/arch/x86/include/asm/atomic.h b/arch/x86/include/asm/atomic.h index ea3d95275b43..115127c7ad28 100644 --- a/arch/x86/include/asm/atomic.h +++ b/arch/x86/include/asm/atomic.h @@ -54,7 +54,7 @@ static __always_inline void arch_atomic_add(int i, atomic_t *v) { asm volatile(LOCK_PREFIX "addl %1,%0" : "+m" (v->counter) - : "ir" (i)); + : "ir" (i) : "memory"); } /** @@ -68,7 +68,7 @@ static __always_inline void arch_atomic_sub(int i, atomic_t *v) { asm volatile(LOCK_PREFIX "subl %1,%0" : "+m" (v->counter) - : "ir" (i)); + : "ir" (i) : "memory"); } /** @@ -95,7 +95,7 @@ static __always_inline bool arch_atomic_sub_and_test(int i, atomic_t *v) static __always_inline void arch_atomic_inc(atomic_t *v) { asm volatile(LOCK_PREFIX "incl %0" - : "+m" (v->counter)); + : "+m" (v->counter) :: "memory"); } #define arch_atomic_inc arch_atomic_inc @@ -108,7 +108,7 @@ static __always_inline void arch_atomic_inc(atomic_t *v) static __always_inline void arch_atomic_dec(atomic_t *v) { asm volatile(LOCK_PREFIX "decl %0" - : "+m" (v->counter)); + : "+m" (v->counter) :: "memory"); } #define arch_atomic_dec arch_atomic_dec diff --git a/arch/x86/include/asm/atomic64_64.h b/arch/x86/include/asm/atomic64_64.h index dadc20adba21..5e86c0d68ac1 100644 --- a/arch/x86/include/asm/atomic64_64.h +++ b/arch/x86/include/asm/atomic64_64.h @@ -45,7 +45,7 @@ static __always_inline void arch_atomic64_add(long i, atomic64_t *v) { asm volatile(LOCK_PREFIX "addq %1,%0" : "=m" (v->counter) - : "er" (i), "m" (v->counter)); + : "er" (i), "m" (v->counter) : "memory"); } /** @@ -59,7 +59,7 @@ static inline void arch_atomic64_sub(long i, atomic64_t *v) { asm volatile(LOCK_PREFIX "subq %1,%0" : "=m" (v->counter) - : "er" (i), "m" (v->counter)); + : "er" (i), "m" (v->counter) : "memory"); } /** @@ -87,7 +87,7 @@ static __always_inline void arch_atomic64_inc(atomic64_t *v) { asm volatile(LOCK_PREFIX "incq %0" : "=m" (v->counter) - : "m" (v->counter)); + : "m" (v->counter) : "memory"); } #define arch_atomic64_inc arch_atomic64_inc @@ -101,7 +101,7 @@ static __always_inline void arch_atomic64_dec(atomic64_t *v) { asm volatile(LOCK_PREFIX "decq %0" : "=m" (v->counter) - : "m" (v->counter)); + : "m" (v->counter) : "memory"); } #define arch_atomic64_dec arch_atomic64_dec diff --git a/arch/x86/include/asm/barrier.h b/arch/x86/include/asm/barrier.h index 14de0432d288..84f848c2541a 100644 --- a/arch/x86/include/asm/barrier.h +++ b/arch/x86/include/asm/barrier.h @@ -80,8 +80,8 @@ do { \ }) /* Atomic operations are already serializing on x86 */ -#define __smp_mb__before_atomic() barrier() -#define __smp_mb__after_atomic() barrier() +#define __smp_mb__before_atomic() do { } while (0) +#define __smp_mb__after_atomic() do { } while (0) #include -- 2.20.1