Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp922366ybl; Fri, 10 Jan 2020 08:58:50 -0800 (PST) X-Google-Smtp-Source: APXvYqzRZmITcdy3zxSqtUdrf2Odgb5e5aoQiETT2F8RbqmVVXZOc0oddh2Kct5URaDIEx5ICeJm X-Received: by 2002:a54:4f14:: with SMTP id e20mr2812915oiy.84.1578675530322; Fri, 10 Jan 2020 08:58:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1578675530; cv=none; d=google.com; s=arc-20160816; b=N5chnQ5qLr5jY0rPNhPfS16Bp+wkB0wsDtNpKwjXv3bZ1pgdqw0rNVS/YmPR3rRD9n 4xZ5QmJfy/GW3S4w9ckXlOt9kVox6oMurSAOAl/cnKXkvoctZxJtveEovWKFNF/iOZy/ zDXovrbiwwFwAluH/fFNxqE7MWHFULZ4P1nepsXzoVdMvtN4l9c2ERJFhhcXa6+f0Zj7 eHLCaw6xItEdDv5sed1IM4VWwIWgcMGLu3t7qEjfAE4i43OyLxB461JHjiGiNGosl9II H0ThmgYpdPnZS/8Gc6LluYPdTd7Fy3OsCSX6nypC90KXSkgk5pWRYkadG8XWW7MTXkJ+ yYfQ== 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=dHq/pZk+cvOTtT8UhzSTlz3AJxF0H9SZE/neYo1dKJM=; b=mfK3mKtc9luKyX5c5bLs+9VlVrlkV/re5tJaBGQbemcrtAy+ji2fHPdeV1B7kO7z/A xw2Fr2SF9bXbt1UWgA5ITA/IpNmKApQelqIvItHUycHq7+9sMaDacBu/Rpa8pDjWr2bp PjKiwFP3tOAqESKeH3fvYF81jPzU31NRMwFZQX2BeZGhg1AKgiNf8emJ6ESHdMyuwIVL ofyw1UDT85RcsowSb+x3CHNbqZiBNc7YU3dMGOJNjll9OQiWYWCXFjMisn01Rac0Fte0 /m0ZjeLgLIkPWrQEjkORSROSGvaI1PW6PmxSdN0B1HEKaRoIZ18VDhbgkXIqbRTjde5K ePKw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=ME3nY8rH; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id q7si1458313otn.108.2020.01.10.08.58.39; Fri, 10 Jan 2020 08:58:50 -0800 (PST) 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=ME3nY8rH; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728457AbgAJQ4z (ORCPT + 99 others); Fri, 10 Jan 2020 11:56:55 -0500 Received: from mail.kernel.org ([198.145.29.99]:60216 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728679AbgAJQ4x (ORCPT ); Fri, 10 Jan 2020 11:56:53 -0500 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 450CF20673; Fri, 10 Jan 2020 16:56:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578675412; bh=BfC1DUM2FbyKqtot6fGX9BmvsFtlomuMxuOA8Kux2Tc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ME3nY8rH5j6WCZDCKGJs+dlD5LZboMjZBNWPdLoOi//iDi0bqdY/CPXDrXTiCQYdh bsGLq+s9LrWr4U23IkDeRezapTmQQaXXrs8NuYBoheoGlNKUbFp9A7vNmuTMTkuBj8 z1aZM9FZ3oLnnPB0xPWVKMHjsGI74agY/reTb6DU= From: Will Deacon To: linux-kernel@vger.kernel.org Cc: linux-arch@vger.kernel.org, kernel-team@android.com, Will Deacon , Michael Ellerman , Peter Zijlstra , Linus Torvalds , Segher Boessenkool , Christian Borntraeger , Luc Van Oostenryck , Arnd Bergmann Subject: [RFC PATCH 4/8] READ_ONCE: Simplify implementations of {READ,WRITE}_ONCE() Date: Fri, 10 Jan 2020 16:56:32 +0000 Message-Id: <20200110165636.28035-5-will@kernel.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200110165636.28035-1-will@kernel.org> References: <20200110165636.28035-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 The implementations of {READ,WRITE}_ONCE() suffer from a significant amount of indirection and complexity due to a historic GCC bug: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58145 which was originally worked around by 230fa253df63 ("kernel: Provide READ_ONCE and ASSIGN_ONCE"). Since GCC 4.8 is fairly vintage at this point and we emit a warning if we detect it during the build, return {READ,WRITE}_ONCE() to their former glory with an implementation that is easier to understand and, crucially, more amenable to optimisation. A side effect of this simplification is that WRITE_ONCE() no longer returns a value, but nobody seems to be relying on that and the new behaviour is aligned with smp_store_release(). Suggested-by: Linus Torvalds Cc: Peter Zijlstra Cc: Michael Ellerman Cc: Arnd Bergmann Cc: Christian Borntraeger Signed-off-by: Will Deacon --- include/linux/compiler.h | 104 ++++++++++----------------------------- 1 file changed, 25 insertions(+), 79 deletions(-) diff --git a/include/linux/compiler.h b/include/linux/compiler.h index 5e88e7e33abe..44974d658f30 100644 --- a/include/linux/compiler.h +++ b/include/linux/compiler.h @@ -177,60 +177,6 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val, # define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __LINE__) #endif -#include - -#define __READ_ONCE_SIZE \ -({ \ - switch (size) { \ - case 1: *(__u8 *)res = *(volatile __u8 *)p; break; \ - case 2: *(__u16 *)res = *(volatile __u16 *)p; break; \ - case 4: *(__u32 *)res = *(volatile __u32 *)p; break; \ - case 8: *(__u64 *)res = *(volatile __u64 *)p; break; \ - default: \ - barrier(); \ - __builtin_memcpy((void *)res, (const void *)p, size); \ - barrier(); \ - } \ -}) - -static __always_inline -void __read_once_size(const volatile void *p, void *res, int size) -{ - __READ_ONCE_SIZE; -} - -#ifdef CONFIG_KASAN -/* - * We can't declare function 'inline' because __no_sanitize_address confilcts - * with inlining. Attempt to inline it may cause a build failure. - * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67368 - * '__maybe_unused' allows us to avoid defined-but-not-used warnings. - */ -# define __no_kasan_or_inline __no_sanitize_address notrace __maybe_unused -#else -# define __no_kasan_or_inline __always_inline -#endif - -static __no_kasan_or_inline -void __read_once_size_nocheck(const volatile void *p, void *res, int size) -{ - __READ_ONCE_SIZE; -} - -static __always_inline void __write_once_size(volatile void *p, void *res, int size) -{ - switch (size) { - case 1: *(volatile __u8 *)p = *(__u8 *)res; break; - case 2: *(volatile __u16 *)p = *(__u16 *)res; break; - case 4: *(volatile __u32 *)p = *(__u32 *)res; break; - case 8: *(volatile __u64 *)p = *(__u64 *)res; break; - default: - barrier(); - __builtin_memcpy((void *)p, (const void *)res, size); - barrier(); - } -} - /* * Prevent the compiler from merging or refetching reads or writes. The * compiler is also forbidden from reordering successive instances of @@ -240,11 +186,7 @@ static __always_inline void __write_once_size(volatile void *p, void *res, int s * statements. * * These two macros will also work on aggregate data types like structs or - * unions. If the size of the accessed data type exceeds the word size of - * the machine (e.g., 32 bits or 64 bits) READ_ONCE() and WRITE_ONCE() will - * fall back to memcpy(). There's at least two memcpy()s: one for the - * __builtin_memcpy() and then one for the macro doing the copy of variable - * - '__u' allocated on the stack. + * unions. * * Their two major use cases are: (1) Mediating communication between * process-level code and irq/NMI handlers, all running on the same CPU, @@ -256,23 +198,35 @@ static __always_inline void __write_once_size(volatile void *p, void *res, int s #include #include -#define __READ_ONCE(x, check) \ +/* + * Use READ_ONCE_NOCHECK() instead of READ_ONCE() if you need + * to hide memory access from KASAN. + */ +#define READ_ONCE_NOCHECK(x) \ ({ \ - union { typeof(x) __val; char __c[1]; } __u; \ - if (check) \ - __read_once_size(&(x), __u.__c, sizeof(x)); \ - else \ - __read_once_size_nocheck(&(x), __u.__c, sizeof(x)); \ - smp_read_barrier_depends(); /* Enforce dependency ordering from x */ \ - __u.__val; \ + typeof(x) __x = *(volatile typeof(x) *)&(x); \ + smp_read_barrier_depends(); \ + __x; \ }) -#define READ_ONCE(x) __READ_ONCE(x, 1) +#define READ_ONCE(x) READ_ONCE_NOCHECK(x) + +#define WRITE_ONCE(x, val) \ +do { \ + *(volatile typeof(x) *)&(x) = (val); \ +} while (0) + +#ifdef CONFIG_KASAN /* - * Use READ_ONCE_NOCHECK() instead of READ_ONCE() if you need - * to hide memory access from KASAN. + * We can't declare function 'inline' because __no_sanitize_address conflicts + * with inlining. Attempt to inline it may cause a build failure. + * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67368 + * '__maybe_unused' allows us to avoid defined-but-not-used warnings. */ -#define READ_ONCE_NOCHECK(x) __READ_ONCE(x, 0) +# define __no_kasan_or_inline __no_sanitize_address notrace __maybe_unused +#else +# define __no_kasan_or_inline __always_inline +#endif static __no_kasan_or_inline unsigned long read_word_at_a_time(const void *addr) @@ -281,14 +235,6 @@ unsigned long read_word_at_a_time(const void *addr) return *(unsigned long *)addr; } -#define WRITE_ONCE(x, val) \ -({ \ - union { typeof(x) __val; char __c[1]; } __u = \ - { .__val = (__force typeof(x)) (val) }; \ - __write_once_size(&(x), __u.__c, sizeof(x)); \ - __u.__val; \ -}) - #endif /* __KERNEL__ */ /* -- 2.25.0.rc1.283.g88dfdc4193-goog