Received: by 2002:a25:868d:0:0:0:0:0 with SMTP id z13csp2765851ybk; Tue, 12 May 2020 07:38:57 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzdn+5nf5hn0wZDgsnv13+zgHlNZ9AP+eoMV02bk5gX19G63OtVy1xXWfJoKz+5PkZeCV4M X-Received: by 2002:a17:906:57d6:: with SMTP id u22mr5288487ejr.49.1589294337051; Tue, 12 May 2020 07:38:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1589294337; cv=none; d=google.com; s=arc-20160816; b=CEIPHZOUOLidEkfIvtqB4dk5vy4zbftZ70f+M+OT5HFXJfwsVM+AX4lIojB4da6BP0 FZNytDPXdhxwkowgmQ9ZpHkc128qI3D0Q24IKWNF993NOG5pepMVs2vZA6QAawCPsJKm lmtWJMh9pRwTPcnFejsH3I9g6UPt+HgHukQkIbR1p/5O0Wt0QsbBAFDVwS87dHKmpZRb j8IDviHAvHD/Jcr9xtEP+JjJsy3+PrTLXtGEUImlYa+4gdUl7i/ZS5pi4Vh6LmKH3RFe 9iUYbNfQPiZBSAOqR51iQYLiIIWC114/WqjaOdGwpqYw8Ky5ygddqf/YgD3Sla4m1+AB 1IiA== 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 :robot-unsubscribe:robot-id:message-id:mime-version:references :in-reply-to:cc:subject:to:reply-to:from:date; bh=T/5BBoJEkae9hcO5H4NYwDvaY9QaEeE7wc5sJW93S5Y=; b=hT3eeXM2UbXEzNt0A8Ngy4pLY9JfOHpX5PuEEC9+McHOh1ZksrCjSQGvK7kzpNkle5 o7HMtw7wfEEx1qmsQj7w09flehj/5lcKJ44R4A3plYA/F1X0MHHP0WrdeRUzWQ4QKZB0 qY6F4W4oMOiB14/TyLgCzFD7qm7S1jY0kYUvYxa0BkOa03FP6AGairOxQj7/p0tIPIIE sD5gdBtYhnKWmcg7vFMO1hVILEAKzDsrXuPq/45XcpV+DQmEdKMZGA1q1pmbRqxGeDbV DdoriyO41zwVtPnwUG0QUG+v8j5ApnSnI8feNuIxCjwiaCRchAKluL2CcOvFT03S2d1i Umrw== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id j93si4763425edd.447.2020.05.12.07.38.34; Tue, 12 May 2020 07:38:57 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726168AbgELOhI (ORCPT + 99 others); Tue, 12 May 2020 10:37:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39394 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730381AbgELOhG (ORCPT ); Tue, 12 May 2020 10:37:06 -0400 Received: from Galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D777DC061A0C; Tue, 12 May 2020 07:37:05 -0700 (PDT) Received: from [5.158.153.53] (helo=tip-bot2.lab.linutronix.de) by Galois.linutronix.de with esmtpsa (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1jYW1g-0005nm-Be; Tue, 12 May 2020 16:36:56 +0200 Received: from [127.0.1.1] (localhost [IPv6:::1]) by tip-bot2.lab.linutronix.de (Postfix) with ESMTP id EB8141C02FC; Tue, 12 May 2020 16:36:55 +0200 (CEST) Date: Tue, 12 May 2020 14:36:55 -0000 From: "tip-bot2 for Will Deacon" Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: locking/kcsan] READ_ONCE: Drop pointer qualifiers when reading from scalar types Cc: Michael Ellerman , Linus Torvalds , Will Deacon , Thomas Gleixner , "Peter Zijlstra (Intel)" , Arnd Bergmann , x86 , LKML In-Reply-To: <20200511204150.27858-13-will@kernel.org> References: <20200511204150.27858-13-will@kernel.org> MIME-Version: 1.0 Message-ID: <158929421588.390.634536033679985583.tip-bot2@tip-bot2> X-Mailer: tip-git-log-daemon Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit X-Linutronix-Spam-Score: -1.0 X-Linutronix-Spam-Level: - X-Linutronix-Spam-Status: No , -1.0 points, 5.0 required, ALL_TRUSTED=-1,SHORTCIRCUIT=-0.0001 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The following commit has been merged into the locking/kcsan branch of tip: Commit-ID: 7b364f0949ae2dd205d5e9afa4b82ee17030d928 Gitweb: https://git.kernel.org/tip/7b364f0949ae2dd205d5e9afa4b82ee17030d928 Author: Will Deacon AuthorDate: Mon, 11 May 2020 21:41:44 +01:00 Committer: Thomas Gleixner CommitterDate: Tue, 12 May 2020 11:04:14 +02:00 READ_ONCE: Drop pointer qualifiers when reading from scalar types Passing a volatile-qualified pointer to READ_ONCE() is an absolute trainwreck for code generation: the use of 'typeof()' to define a temporary variable inside the macro means that the final evaluation in macro scope ends up forcing a read back from the stack. When stack protector is enabled (the default for arm64, at least), this causes the compiler to vomit up all sorts of junk. Unfortunately, dropping pointer qualifiers inside the macro poses quite a challenge, especially since the pointed-to type is permitted to be an aggregate, and this is relied upon by mm/ code accessing things like 'pmd_t'. Based on numerous hacks and discussions on the mailing list, this is the best I've managed to come up with. Introduce '__unqual_scalar_typeof()' which takes an expression and, if the expression is an optionally qualified 8, 16, 32 or 64-bit scalar type, evaluates to the unqualified type. Other input types, including aggregates, remain unchanged. Hopefully READ_ONCE() on volatile aggregate pointers isn't something we do on a fast-path. Reported-by: Michael Ellerman Suggested-by: Linus Torvalds Signed-off-by: Will Deacon Signed-off-by: Thomas Gleixner Acked-by: Peter Zijlstra (Intel) Cc: Arnd Bergmann Link: https://lkml.kernel.org/r/20200511204150.27858-13-will@kernel.org --- include/linux/compiler.h | 6 +++--- include/linux/compiler_types.h | 26 ++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/include/linux/compiler.h b/include/linux/compiler.h index 733605f..548294e 100644 --- a/include/linux/compiler.h +++ b/include/linux/compiler.h @@ -204,7 +204,7 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val, * atomicity or dependency ordering guarantees. Note that this may result * in tears! */ -#define __READ_ONCE(x) (*(const volatile typeof(x) *)&(x)) +#define __READ_ONCE(x) (*(const volatile __unqual_scalar_typeof(x) *)&(x)) #define __READ_ONCE_SCALAR(x) \ ({ \ @@ -212,10 +212,10 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val, kcsan_check_atomic_read(__xp, sizeof(*__xp)); \ __kcsan_disable_current(); \ ({ \ - typeof(x) __x = __READ_ONCE(*__xp); \ + __unqual_scalar_typeof(x) __x = __READ_ONCE(*__xp); \ __kcsan_enable_current(); \ smp_read_barrier_depends(); \ - __x; \ + (typeof(x))__x; \ }); \ }) diff --git a/include/linux/compiler_types.h b/include/linux/compiler_types.h index e970f97..6ed0612 100644 --- a/include/linux/compiler_types.h +++ b/include/linux/compiler_types.h @@ -210,6 +210,32 @@ struct ftrace_likely_data { /* Are two types/vars the same type (ignoring qualifiers)? */ #define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b)) +/* + * __unqual_scalar_typeof(x) - Declare an unqualified scalar type, leaving + * non-scalar types unchanged. + * + * We build this out of a couple of helper macros in a vain attempt to + * help you keep your lunch down while reading it. + */ +#define __pick_scalar_type(x, type, otherwise) \ + __builtin_choose_expr(__same_type(x, type), (type)0, otherwise) + +/* + * 'char' is not type-compatible with either 'signed char' or 'unsigned char', + * so we include the naked type here as well as the signed/unsigned variants. + */ +#define __pick_integer_type(x, type, otherwise) \ + __pick_scalar_type(x, type, \ + __pick_scalar_type(x, unsigned type, \ + __pick_scalar_type(x, signed type, otherwise))) + +#define __unqual_scalar_typeof(x) typeof( \ + __pick_integer_type(x, char, \ + __pick_integer_type(x, short, \ + __pick_integer_type(x, int, \ + __pick_integer_type(x, long, \ + __pick_integer_type(x, long long, x)))))) + /* Is this type a native word size -- useful for atomic operations */ #define __native_word(t) \ (sizeof(t) == sizeof(char) || sizeof(t) == sizeof(short) || \