Return-Path: Received: from mail-wm0-f66.google.com ([74.125.82.66]:53911 "EHLO mail-wm0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1033454AbeBOTwO (ORCPT ); Thu, 15 Feb 2018 14:52:14 -0500 Received: by mail-wm0-f66.google.com with SMTP id t74so2987595wme.3 for ; Thu, 15 Feb 2018 11:52:13 -0800 (PST) From: Rasmus Villemoes To: Thomas Gleixner , Dan Williams , Will Deacon , Ingo Molnar , Rasmus Villemoes Cc: Linus Torvalds , stable@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH] linux/nospec.h: allow index argument to have const-qualified type Date: Thu, 15 Feb 2018 20:52:07 +0100 Message-Id: <20180215195209.15299-1-linux@rasmusvillemoes.dk> In-Reply-To: References: Sender: stable-owner@vger.kernel.org List-ID: The last expression in a statement expression need not be a bare variable, quoting gcc docs The last thing in the compound statement should be an expression followed by a semicolon; the value of this subexpression serves as the value of the entire construct. and we already use that in e.g. the min/max macros which end with a ternary expression. This way, we can allow index to have const-qualified type, which will in some cases avoid the need for introducing a local copy of index of non-const qualified type. That, in turn, can prevent readers not familiar with the internals of array_index_nospec from wondering about the seemingly redundant extra variable, and I think that's worthwhile considering how confusing the whole _nospec business is. The expression _i&_mask has type unsigned long (since that is the type of _mask, and the BUILD_BUG_ONs guarantee that _i will get promoted to that), so in order not to change the type of the whole expression, add a cast back to typeof(_i). Cc: stable@vger.kernel.org Signed-off-by: Rasmus Villemoes --- cc stable because if this is ok, there will probably be future users relying on this which also get cc'ed to -stable. include/linux/nospec.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/include/linux/nospec.h b/include/linux/nospec.h index fbc98e2c8228..132e3f5a2e0d 100644 --- a/include/linux/nospec.h +++ b/include/linux/nospec.h @@ -72,7 +72,6 @@ static inline unsigned long array_index_mask_nospec(unsigned long index, BUILD_BUG_ON(sizeof(_i) > sizeof(long)); \ BUILD_BUG_ON(sizeof(_s) > sizeof(long)); \ \ - _i &= _mask; \ - _i; \ + (typeof(_i)) (_i & _mask); \ }) #endif /* _LINUX_NOSPEC_H */ -- 2.15.1