From: Jan Beulich <jbeulich@suse.com>
To: Andrew Cooper <andrew.cooper3@citrix.com>
Cc: "Juergen Gross" <jgross@suse.com>,
Xen-devel <xen-devel@lists.xenproject.org>,
"Wei Liu" <wl@xen.org>, "Roger Pau Monné" <roger.pau@citrix.com>
Subject: Re: [Xen-devel] [PATCH v3 7/7] x86/nospec: Optimise array_index_mask_nospec() for power-of-2 arrays
Date: Fri, 25 Oct 2019 15:25:33 +0200 [thread overview]
Message-ID: <175da82e-982f-c08a-326a-db4693a16ac6@suse.com> (raw)
In-Reply-To: <c1794d03-8ec3-d350-aa7d-43cc5701d562@citrix.com>
On 25.10.2019 14:58, Andrew Cooper wrote:
> On 25/10/2019 13:24, Jan Beulich wrote:
>> On 23.10.2019 15:58, Andrew Cooper wrote:
>>> @@ -21,9 +28,15 @@ static inline unsigned long array_index_mask_nospec(unsigned long index,
>>> {
>>> unsigned long mask;
>>>
>>> - asm volatile ( "cmp %[size], %[index]; sbb %[mask], %[mask];"
>>> - : [mask] "=r" (mask)
>>> - : [size] "g" (size), [index] "r" (index) );
>>> + if ( __builtin_constant_p(size) && IS_POWER_OF_2(size) )
>>> + {
>>> + mask = size - 1;
>>> + OPTIMIZER_HIDE_VAR(mask);
>> I can't seem to be able to figure why you need this.
>
> Because I found cases where the AND was elided by the compiler entirely
> without it.
Would you mind mentioning this in the description, or in a comment?
>>> --- a/xen/include/xen/config.h
>>> +++ b/xen/include/xen/config.h
>>> @@ -75,6 +75,7 @@
>>> #define GB(_gb) (_AC(_gb, ULL) << 30)
>>>
>>> #define IS_ALIGNED(val, align) (((val) & ((align) - 1)) == 0)
>>> +#define IS_POWER_OF_2(val) ((val) && IS_ALIGNED(val, val))
>> While the risk may seem low for someone to pass an expression with
>> side effect here, evaluating "val" up to three times here doesn't
>> look very desirable.
>
> That is easy to fix.
>
>> As a minor remark, without considering representation I'd expect
>> an expression IS_ALIGNED(val, val) to consistently produce "true"
>> for all non-zero values. E.g. 3 is certainly "aligned" on a
>> boundary of 3.
>
> IS_ALIGNED() comes with an expectation of being against a power of 2,
> because otherwise you'd need a DIV instruction for the general case.
>
> Some users can't cope with a compile time check.
>
>> Finally this may want guarding against use on signed types - at
>> the very least it looks to produce the wrong answer for e.g.
>> INT_MIN or LONG_MIN. I.e. perhaps the expression to the left of
>> && wants to be (val) > 0.
>
> How about the above expansion fix becoming:
>
> ({
> unsigned typeof(val) _val = val;
> _val && (_val & (_val - 1)) == 0;
> })
Well, if the "unsigned typeof()" construct works - why not (with
"val" properly parenthesized, and preferable the leading underscore
changed to a trailing one).
> This check makes no sense on negative numbers.
Of course not, but someone might use it on a signed type and get
back true when it was supposed to be false, just because the value
used ended up being a negative number.
Jan
_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel
prev parent reply other threads:[~2019-10-25 13:25 UTC|newest]
Thread overview: 42+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-10-23 13:58 [Xen-devel] [PATCH for-4.13 v3 0/7] Unbreak evaluate_nospec() and livepatching Andrew Cooper
2019-10-23 13:58 ` [Xen-devel] [PATCH v3 1/7] x86/nospec: Two trivial fixes Andrew Cooper
2019-10-23 14:03 ` Jan Beulich
2019-10-23 14:43 ` Jürgen Groß
2019-10-23 13:58 ` [Xen-devel] [PATCH v3 2/7] xen/nospec: Use always_inline to fix code gen for evaluate_nospec Andrew Cooper
2019-10-23 14:44 ` Jürgen Groß
2019-10-25 12:03 ` Jan Beulich
2019-10-25 12:10 ` Andrew Cooper
2019-10-25 12:34 ` Jan Beulich
2019-10-25 15:27 ` Andrew Cooper
2019-10-25 15:40 ` Jan Beulich
2019-10-25 21:56 ` Norbert Manthey
2019-10-28 17:05 ` Andrew Cooper
2019-10-29 8:25 ` Norbert Manthey
2019-10-29 13:46 ` Andrew Cooper
2019-10-29 14:03 ` Jan Beulich
2019-10-29 14:16 ` Andrew Cooper
2019-10-29 14:33 ` Norbert Manthey
2019-10-29 16:53 ` Andrew Cooper
2019-10-30 8:33 ` Jan Beulich
2019-10-23 13:58 ` [Xen-devel] [PATCH v3 3/7] xen/nospec: Introduce CONFIG_SPECULATIVE_HARDEN_BRANCH Andrew Cooper
2019-10-23 14:45 ` Jürgen Groß
2019-10-25 12:04 ` Jan Beulich
2019-10-23 13:58 ` [Xen-devel] [PATCH v3 4/7] x86/nospec: Rename and rework l1tf-barrier as branch-harden Andrew Cooper
2019-10-23 14:43 ` Jürgen Groß
2019-10-25 12:09 ` Jan Beulich
2019-10-29 17:00 ` Andrew Cooper
2019-10-23 13:58 ` [Xen-devel] [PATCH v3 5/7] x86/livepatch: Fail the build if duplicate symbols exist Andrew Cooper
2019-10-23 14:46 ` Jürgen Groß
2019-10-23 16:14 ` Konrad Rzeszutek Wilk
2019-10-23 16:37 ` Ross Lagerwall
2019-10-24 12:03 ` Jan Beulich
2019-10-29 17:06 ` Andrew Cooper
2019-10-30 8:41 ` Jan Beulich
2019-10-30 10:37 ` Andrew Cooper
2019-10-30 11:21 ` Jan Beulich
2019-10-23 13:58 ` [Xen-devel] [PATCH for-next v3 6/7] x86/nospec: Move array_index_mask_nospec() into nospec.h Andrew Cooper
2019-10-25 12:10 ` Jan Beulich
2019-10-23 13:58 ` [Xen-devel] [PATCH v3 7/7] x86/nospec: Optimise array_index_mask_nospec() for power-of-2 arrays Andrew Cooper
2019-10-25 12:24 ` Jan Beulich
2019-10-25 12:58 ` Andrew Cooper
2019-10-25 13:25 ` Jan Beulich [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=175da82e-982f-c08a-326a-db4693a16ac6@suse.com \
--to=jbeulich@suse.com \
--cc=andrew.cooper3@citrix.com \
--cc=jgross@suse.com \
--cc=roger.pau@citrix.com \
--cc=wl@xen.org \
--cc=xen-devel@lists.xenproject.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.