Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751170AbeADW4t (ORCPT + 1 other); Thu, 4 Jan 2018 17:56:49 -0500 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 Date: Thu, 4 Jan 2018 22:55:15 +0000 From: Alan Cox 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 Subject: Re: [RFC PATCH] asm/generic: introduce if_nospec and nospec_barrier Message-ID: <20180104225515.13a40f0f@alans-desktop> In-Reply-To: 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> Organization: Intel Corporation X-Mailer: Claws Mail 3.15.1-dirty (GTK+ 2.24.31; x86_64-redhat-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Return-Path: > 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