From mboxrd@z Thu Jan 1 00:00:00 1970 From: Alan Cox Subject: Re: [RFC PATCH] asm/generic: introduce if_nospec and nospec_barrier Date: Thu, 4 Jan 2018 22:55:15 +0000 Message-ID: <20180104225515.13a40f0f@alans-desktop> References: <20180103223827.39601-1-mark.rutland@arm.com> <151502463248.33513.5960736946233335087.stgit@dwillia2-desk3.amr.corp.intel.com> <20180104010754.22ca6a74@alans-desktop> <20180104192648.GA10427@amd> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Return-path: Received: from www.llwyncelyn.cymru ([82.70.14.225]:47916 "EHLO fuzix.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751058AbeADW4r (ORCPT ); Thu, 4 Jan 2018 17:56:47 -0500 In-Reply-To: Sender: linux-arch-owner@vger.kernel.org List-ID: To: Linus Torvalds Cc: Dan Williams , Pavel Machek , Julia Lawall , Linux Kernel Mailing List , Mark Rutland , linux-arch@vger.kernel.org, Peter Zijlstra , Greg KH , Thomas Gleixner , Elena Reshetova , Alan Cox , Dan Carpenter > and the *implementation* might be architecture-specific, but one > particular implementation would be something like simply > > #define array_access(base, idx, max) ({ \ > union { typeof(base[0]) _val; unsigned long _bit; } __u;\ > unsigned long _i = (idx); \ > unsigned long _m = (max); \ > unsigned long _mask = _i < _m ? ~0 : 0; \ > OPTIMIZER_HIDE_VAR(_mask); \ > __u._val = base[_i & _mask]; \ > __u._bit &= _mask; \ > __u._val; }) > > (Ok, the above is not exhaustively tested, but you get the idea, and > gcc doesn't seem to mess it up *too* badly). How do you ensure that the CPU doesn't speculate j < _m ? ~0 : 0 pick the wrong mask and then reference base[] ? It's a nice idea but I think we'd need to run it past the various CPU vendors especially before it was implemented as a generic solution. Anding with a constant works because the constant doesn't get speculated and nor does the and with a constant, but you've got a whole additional conditional path in your macro. Alan