Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp3832498ybt; Tue, 30 Jun 2020 12:03:59 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzZGBbA2/qXytPAOA0llMTxCYjAHSwlfyABCp1szcMY4OuQVKQ28a2p2oC1jRx35Uafsv/W X-Received: by 2002:a50:cc8a:: with SMTP id q10mr25230382edi.257.1593543392605; Tue, 30 Jun 2020 11:56:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1593543392; cv=none; d=google.com; s=arc-20160816; b=CwROIRdPZO3eetjfgJnZ/CmQhjFx9aGnLdB0rRsFZrF3nvuA0aQJ7juIgG19GTC4t0 AP6EmF3yzauBg73JmtLpgfTggt17se+vH3I3zWIT+emp3yDosmzAaoNHUvaXr3RbEpS1 qnTwIw1XQny/Qk44AW708Rbo+m5JNmHZunqc4FtOxkoHwYamCJrgXCDQbfGMaCoGc+Zq T49Nv7aqKYOU4BUyn29flCDZxDgt491DxXGv/Qpc0UWHGRCAjWn7asn0rxMCq64K/0he tVJehXtXR+9exZDoXP9WCNFKOEYwuKBTHINFQaVFt1qFYUkyZ3C8ZTRsfSdU5Dnd6Sow ++/A== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=yNYxpmD5d8loPVcFAyunbtR2+GGGX2vjHC2fzR+G0HM=; b=pwv6uo4M3pBfZn0e2oybdFF3sAfPiLw1pHI32SskokGbZpa8lzN92ZmKIdfffxUzIs GRelcKOyfBPoJaR0TX26c/d2+CbiN2JQzmnWvNplxMCciMe37HVoG9jOv3t/1x6pUXH3 I32qeQY12IGS6wLYyqkuSdzFMrrrVH3tP/QeaHnDgYrOT6SQ0d0Kp24j/rqwoxZ6LJ+Y DVm8zvNv3DTZJaF8CKRB6bNjvY8vMoUwQ9TvldGguVj4x9+WzrS11IxHY2XVLAP3qCrZ sP4fkT3Znz1OoWonMf6BRCToPhIhiyggt0eWCDDORcVwRUnLggNx83nT/u+IgIAhBWCg nAxQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=SwHbTNuw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id s5si2213702eja.431.2020.06.30.11.56.09; Tue, 30 Jun 2020 11:56:32 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=SwHbTNuw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S1730506AbgF3RjF (ORCPT + 99 others); Tue, 30 Jun 2020 13:39:05 -0400 Received: from mail.kernel.org ([198.145.29.99]:52380 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2390480AbgF3Ri7 (ORCPT ); Tue, 30 Jun 2020 13:38:59 -0400 Received: from localhost.localdomain (236.31.169.217.in-addr.arpa [217.169.31.236]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 1BB4720884; Tue, 30 Jun 2020 17:38:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1593538738; bh=jk+5sT2fqYF73trdlHjbb8Xz5ZVVf+j6UQNLR9NTc5c=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=SwHbTNuwRHrvvq/CMqLWJgRrQVnZ73gOKAJgzWHmw6wsu2PqTwOxxDOmvftW+7gI9 7bWSfmd4acJAOw8FWuTsQ03TWYX56fSJJMw2exEERvcDbn0rUS6OXwj5ckCilNhzOW am/U2lPr0UTmgzCS/4W8senaW9+wJVzNpT1aorSg= From: Will Deacon To: linux-kernel@vger.kernel.org Cc: Will Deacon , Sami Tolvanen , Nick Desaulniers , Kees Cook , Marco Elver , "Paul E. McKenney" , Josh Triplett , Matt Turner , Ivan Kokshaysky , Richard Henderson , Peter Zijlstra , Alan Stern , "Michael S. Tsirkin" , Jason Wang , Arnd Bergmann , Boqun Feng , Catalin Marinas , Mark Rutland , linux-arm-kernel@lists.infradead.org, linux-alpha@vger.kernel.org, virtualization@lists.linux-foundation.org, kernel-team@android.com Subject: [PATCH 18/18] arm64: lto: Strengthen READ_ONCE() to acquire when CLANG_LTO=y Date: Tue, 30 Jun 2020 18:37:34 +0100 Message-Id: <20200630173734.14057-19-will@kernel.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200630173734.14057-1-will@kernel.org> References: <20200630173734.14057-1-will@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org When building with LTO, there is an increased risk of the compiler converting an address dependency headed by a READ_ONCE() invocation into a control dependency and consequently allowing for harmful reordering by the CPU. Ensure that such transformations are harmless by overriding the generic READ_ONCE() definition with one that provides acquire semantics when building with LTO. Signed-off-by: Will Deacon --- arch/arm64/include/asm/rwonce.h | 63 +++++++++++++++++++++++++++++++ arch/arm64/kernel/vdso/Makefile | 2 +- arch/arm64/kernel/vdso32/Makefile | 2 +- 3 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 arch/arm64/include/asm/rwonce.h diff --git a/arch/arm64/include/asm/rwonce.h b/arch/arm64/include/asm/rwonce.h new file mode 100644 index 000000000000..515e360b01a1 --- /dev/null +++ b/arch/arm64/include/asm/rwonce.h @@ -0,0 +1,63 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Copyright (C) 2020 Google LLC. + */ +#ifndef __ASM_RWONCE_H +#define __ASM_RWONCE_H + +#ifdef CONFIG_CLANG_LTO + +#include +#include + +#ifndef BUILD_VDSO + +#ifdef CONFIG_AS_HAS_LDAPR +#define __LOAD_RCPC(sfx, regs...) \ + ALTERNATIVE( \ + "ldar" #sfx "\t" #regs, \ + ".arch_extension rcpc\n" \ + "ldapr" #sfx "\t" #regs, \ + ARM64_HAS_LDAPR) +#else +#define __LOAD_RCPC(sfx, regs...) "ldar" #sfx "\t" #regs +#endif /* CONFIG_AS_HAS_LDAPR */ + +#define __READ_ONCE(x) \ +({ \ + int atomic = 1; \ + union { __unqual_scalar_typeof(x) __val; char __c[1]; } __u; \ + typeof(&(x)) __x = &(x); \ + switch (sizeof(x)) { \ + case 1: \ + asm volatile(__LOAD_RCPC(b, %w0, %1) \ + : "=r" (*(__u8 *)__u.__c) \ + : "Q" (*__x) : "memory"); \ + break; \ + case 2: \ + asm volatile(__LOAD_RCPC(h, %w0, %1) \ + : "=r" (*(__u16 *)__u.__c) \ + : "Q" (*__x) : "memory"); \ + break; \ + case 4: \ + asm volatile(__LOAD_RCPC(, %w0, %1) \ + : "=r" (*(__u32 *)__u.__c) \ + : "Q" (*__x) : "memory"); \ + break; \ + case 8: \ + asm volatile(__LOAD_RCPC(, %0, %1) \ + : "=r" (*(__u64 *)__u.__c) \ + : "Q" (*__x) : "memory"); \ + break; \ + default: \ + atomic = 0; \ + } \ + atomic ? (typeof(x))__u.__val : (*(volatile typeof(x) *)__x); \ +}) + +#endif /* !BUILD_VDSO */ +#endif /* CONFIG_CLANG_LTO */ + +#include + +#endif /* __ASM_RWONCE_H */ diff --git a/arch/arm64/kernel/vdso/Makefile b/arch/arm64/kernel/vdso/Makefile index 45d5cfe46429..60df97f2e7de 100644 --- a/arch/arm64/kernel/vdso/Makefile +++ b/arch/arm64/kernel/vdso/Makefile @@ -28,7 +28,7 @@ ldflags-y := -shared -nostdlib -soname=linux-vdso.so.1 --hash-style=sysv \ $(btildflags-y) -T ccflags-y := -fno-common -fno-builtin -fno-stack-protector -ffixed-x18 -ccflags-y += -DDISABLE_BRANCH_PROFILING +ccflags-y += -DDISABLE_BRANCH_PROFILING -DBUILD_VDSO CFLAGS_REMOVE_vgettimeofday.o = $(CC_FLAGS_FTRACE) -Os $(CC_FLAGS_SCS) $(GCC_PLUGINS_CFLAGS) KBUILD_CFLAGS += $(DISABLE_LTO) diff --git a/arch/arm64/kernel/vdso32/Makefile b/arch/arm64/kernel/vdso32/Makefile index d88148bef6b0..4fdf3754a058 100644 --- a/arch/arm64/kernel/vdso32/Makefile +++ b/arch/arm64/kernel/vdso32/Makefile @@ -43,7 +43,7 @@ cc32-as-instr = $(call try-run,\ # As a result we set our own flags here. # KBUILD_CPPFLAGS and NOSTDINC_FLAGS from top-level Makefile -VDSO_CPPFLAGS := -D__KERNEL__ -nostdinc -isystem $(shell $(CC_COMPAT) -print-file-name=include) +VDSO_CPPFLAGS := -DBUILD_VDSO -D__KERNEL__ -nostdinc -isystem $(shell $(CC_COMPAT) -print-file-name=include) VDSO_CPPFLAGS += $(LINUXINCLUDE) # Common C and assembly flags -- 2.27.0.212.ge8ba1cc988-goog