Received: by 2002:a25:868d:0:0:0:0:0 with SMTP id z13csp2170153ybk; Mon, 11 May 2020 13:48:00 -0700 (PDT) X-Google-Smtp-Source: APiQypLpkTJBRVmDpUOHWk8kl5dB88Q4ONy15WsvEgu0U3N+UxEoz9WPTPEwiVdQ1yeXBoZ5RG25 X-Received: by 2002:a50:8750:: with SMTP id 16mr9335299edv.309.1589230080538; Mon, 11 May 2020 13:48:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1589230080; cv=none; d=google.com; s=arc-20160816; b=x9kuCCNKipSn1G7MhbsATpS9c9KC4nmgMfXmZA+5CXaHeWO95nVgnRHHF3RHGcXdi9 8xBFACjK4vsOIIgz37sk2Lnd3nlDVqUM3f4qzKTvRTt6bQjzK7T91Sjf+Vnot0F2FQrx nE5EmVyYxLc18JqBEzdh7hO6GRm/qKT23lrYSyYicpXF97Xwzs6DWY36srtCGC7jeICN FXDtPfrfPTnf+kHzauTpBd93+uThRs1luSLilhehR/qVe/GlHmX40PIvnY3/lCFJY09q lMQI9kf2SsU6TPV19s3+enUYkZuufAPRfe1QFEDdpYORG1Qy6UNa8gMBH42YDiCBze5a fnjw== 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=G5GZb1PTnQ7WXsnHkyJfx0wRzdE859Ot3WN49lGvuig=; b=kQMquR3Pw1oEyl9DqUjBEHyTzC/k22UAovXKwMMtkryJlrRWZQjDvuFREELNu7PK3J w1WbJ3DADcVWMSf7WIsM/TCS+IajjwRFpGPY5zNRp2Z7gq1d79oljN6wm8ci2bJc7Qdo sD8ZQsRSGnSQm0bWge4d0ArjCsX6JBcGHRC6Uq8B18VynpY47VSvH5IcTPwkk3Lp2f04 iVTJVdWZ4z9aXepNAet87U0F0xEFEicxOxsJYNiX+RWvEnzJcFh6HMtkQjhY1aZRIo5t 41SEOthiYraFmZMiolJP4wm8ttpHHD0oknXbd8qUrkr15fDhCp7qOWQMt8Jp/8AOU1Nh Cchw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=OLLeKPpf; 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 bi11si2963968edb.606.2020.05.11.13.47.37; Mon, 11 May 2020 13:48:00 -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=OLLeKPpf; 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 S1731879AbgEKUm4 (ORCPT + 99 others); Mon, 11 May 2020 16:42:56 -0400 Received: from mail.kernel.org ([198.145.29.99]:54604 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731852AbgEKUmx (ORCPT ); Mon, 11 May 2020 16:42:53 -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 4C560206A3; Mon, 11 May 2020 20:42:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1589229772; bh=hzvMaW2SSTvV/8f7MU63L7H08XB628Ws8TlF80gIhyA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=OLLeKPpf2Xf4ULx4PXN5k0XTyaJu19XbR+mDVmIw8ObnU4DzpQ63MpUhdYUV64QdQ KZ+t1fsSVWnqxw+NZX2nyhfBZTDJfA5BDtJbTAkA8xG48jrOaoGohZHRnOlXB//Kti drsJTd7uBrDryz3wVRhyYcrykPI+X/tz9UZWkoyY= From: Will Deacon To: linux-kernel@vger.kernel.org Cc: elver@google.com, tglx@linutronix.de, paulmck@kernel.org, mingo@kernel.org, peterz@infradead.org, will@kernel.org Subject: [PATCH v5 12/18] READ_ONCE: Drop pointer qualifiers when reading from scalar types Date: Mon, 11 May 2020 21:41:44 +0100 Message-Id: <20200511204150.27858-13-will@kernel.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200511204150.27858-1-will@kernel.org> References: <20200511204150.27858-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 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. Cc: Peter Zijlstra Cc: Arnd Bergmann Suggested-by: Linus Torvalds Reported-by: Michael Ellerman Signed-off-by: Will Deacon --- 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 e1b839e42563..0caced170a8a 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 e970f97a7fcb..6ed0612bc143 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) || \ -- 2.26.2.645.ge9eca65c58-goog