Received: by 10.213.65.68 with SMTP id h4csp1745211imn; Sun, 1 Apr 2018 13:51:29 -0700 (PDT) X-Google-Smtp-Source: AIpwx4/5bRkXmvDHsS2Glk1xqa5pd783tLlXjck67asrPNV0COJsIvF26Y19alC+LMFcKH0Ws0Fc X-Received: by 2002:a17:902:4001:: with SMTP id b1-v6mr7216981pld.273.1522615889184; Sun, 01 Apr 2018 13:51:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1522615889; cv=none; d=google.com; s=arc-20160816; b=o8rGQ1985G2iZAFkIPM/pytUpAnRFJdz0zx6cvQ2EVzJurA/Iore/FKVSoylBhuIRf pULMmZSsMNXEX+Q5gAzPTLfD7pYouzjf5B3ruMoXEeucMgAXLeFA2rMAvXAqV15JAy7A V35oHqh4fvX9ix1FiSHU+0vjiy2zexxC2PnZYwZLj0jzgy5oJGHRF/+vD0t27eRJ7Y+2 ODi1ZEU4E2tWbrNlyLgeu9Qs7++EL58iSTrEx61v1JQJBgQ6mZlFa49fDNDjlsH2bXpO U9FKg/KdD8PNmhfTivkEavPvkKM1Tx17VUltnsRWqzmvv59D5W9Pc7rOkwAfTpdmDGQl Gfrw== 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:to:from:subject :organization:arc-authentication-results; bh=XBHChMEjDCq008ToZflbAD5FbopCYLjxXJiXcZXAH4s=; b=F6ixfiZeyDTg08S2gqyFDOEwEBmG1XWIg1kEQYiG4PWN/T4jofatmPwVuz4YTFE3wc hN8RI9S++PdPoDLcPJb7hD810HPG7f33xvk0Le7S6yl0MlTUoXPYq/bOsSlACN+uMy5M ZXKv5tn9TMgtVB6PaKTActe5Z9hxV3QiMbG7P9tdava7QWLbcojUG23jk8dblPHQmlrj PdvCqT+MfE3pEIFF0tRnTTxbPGG+LG95jCIcpKFqwl3H5tKIbong8SPZ6kmykR2ThmtG FHisI0W0Xy088i65CKDqVN4BITarqpqRyZe5NLWRg39P9v9FJuc8M8klS29vwkI9rSsC QrCA== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id s2si9907342pfb.39.2018.04.01.13.51.15; Sun, 01 Apr 2018 13:51:29 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754616AbeDAUsO (ORCPT + 99 others); Sun, 1 Apr 2018 16:48:14 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:52976 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1754292AbeDAUl7 (ORCPT ); Sun, 1 Apr 2018 16:41:59 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id B0955722DD for ; Sun, 1 Apr 2018 20:41:58 +0000 (UTC) Received: from warthog.procyon.org.uk (ovpn-120-59.rdu2.redhat.com [10.10.120.59]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4933C215CDAC; Sun, 1 Apr 2018 20:41:58 +0000 (UTC) Organization: Red Hat UK Ltd. Registered Address: Red Hat UK Ltd, Amberley Place, 107-111 Peascod Street, Windsor, Berkshire, SI4 1TE, United Kingdom. Registered in England and Wales under Company Registration No. 3798903 Subject: [PATCH 17/45] C++: Turn READ_ONCE(), WRITE_ONCE() & co. into inline template functions From: David Howells To: linux-kernel@vger.kernel.org Date: Sun, 01 Apr 2018 21:41:57 +0100 Message-ID: <152261531782.30503.2451449805347145060.stgit@warthog.procyon.org.uk> In-Reply-To: <152261521484.30503.16131389653845029164.stgit@warthog.procyon.org.uk> References: <152261521484.30503.16131389653845029164.stgit@warthog.procyon.org.uk> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit X-Scanned-By: MIMEDefang 2.78 on 10.11.54.6 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.2]); Sun, 01 Apr 2018 20:41:58 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.2]); Sun, 01 Apr 2018 20:41:58 +0000 (UTC) for IP:'10.11.54.6' DOMAIN:'int-mx06.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'dhowells@redhat.com' RCPT:'' Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Turn READ_ONCE(), WRITE_ONCE() and similar into inline template functions. This allows the source to be simplified since there doesn't need to be magic casting and switch-statements inside macroisation. Signed-off-by: David Howells --- include/linux/compiler.h | 93 ++++++++++++++-------------------------------- 1 file changed, 29 insertions(+), 64 deletions(-) diff --git a/include/linux/compiler.h b/include/linux/compiler.h index ab4711c63601..f9be82b93e20 100644 --- a/include/linux/compiler.h +++ b/include/linux/compiler.h @@ -3,6 +3,8 @@ #define __LINUX_COMPILER_H #include +#include +#include #ifndef __ASSEMBLY__ @@ -168,26 +170,6 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val, #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 @@ -200,26 +182,6 @@ void __read_once_size(const volatile void *p, void *res, int size) # 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 @@ -242,27 +204,30 @@ static __always_inline void __write_once_size(volatile void *p, void *res, int s * with an explicit memory barrier or atomic instruction that provides the * required ordering. */ -#include -#include +template +static inline P READ_ONCE(P &ptr) +{ + barrier(); + P val = *(volatile P *)&ptr; + smp_read_barrier_depends(); /* Enforce dependency ordering from x */ + return val; +} -#define __READ_ONCE(x, check) \ -({ \ - 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; \ -}) -#define READ_ONCE(x) __READ_ONCE(x, 1) +template +static __no_kasan_or_inline P READ_ONCE_NOCHECK(P &ptr) +{ + barrier(); + P val = *(volatile P *)&ptr; + smp_read_barrier_depends(); /* Enforce dependency ordering from x */ + return val; +} /* - * Use READ_ONCE_NOCHECK() instead of READ_ONCE() if you need - * to hide memory access from KASAN. + * This function is not '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 READ_ONCE_NOCHECK(x) __READ_ONCE(x, 0) - static __no_kasan_or_inline unsigned long read_word_at_a_time(const void *addr) { @@ -270,13 +235,13 @@ 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; \ -}) +template +static inline void WRITE_ONCE(P &ptr, T val) +{ + barrier(); + *(volatile P *)&ptr = val; + barrier(); +} #endif /* __KERNEL__ */