All of lore.kernel.org
 help / color / mirror / Atom feed
From: Andrew Cooper <Andrew.Cooper3@citrix.com>
To: Jan Beulich <jbeulich@suse.com>, Edwin Torok <edvin.torok@citrix.com>
Cc: Roger Pau Monne <roger.pau@citrix.com>, Wei Liu <wl@xen.org>,
	Xen-devel <xen-devel@lists.xenproject.org>,
	Demi Marie Obenour <demi@invisiblethingslab.com>
Subject: Re: [PATCH] x86/hvm: Improve hvm_set_guest_pat() code generation again
Date: Fri, 16 Dec 2022 20:53:49 +0000	[thread overview]
Message-ID: <77198021-f45c-9d75-c1da-5022d3ca99a2@citrix.com> (raw)
In-Reply-To: <74d0425a-a206-2bcb-50d6-e5bb4c5e2bf3@suse.com>

On 10/08/2022 3:06 pm, Jan Beulich wrote:
> On 10.08.2022 15:36, Andrew Cooper wrote:
>> From: Edwin Török <edvin.torok@citrix.com>
>>
>> Following on from cset 9ce0a5e207f3 ("x86/hvm: Improve hvm_set_guest_pat()
>> code generation"), and the discovery that Clang/LLVM makes some especially
>> disastrous code generation for the loop at -O2
>>
>>   https://github.com/llvm/llvm-project/issues/54644
>>
>> Edvin decided to remove the loop entirely by fully vectorising it.  This is
>> substantially more efficient than the loop, and rather harder for a typical
>> compiler to mess up.
>>
>> Signed-off-by: Edwin Török <edvin.torok@citrix.com>
>> Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>
> The main downside being that changing the code to fit in a new PAT
> type will now be harder.

When was the last PAT type change?

Trick question.  Never, because PAT hasn't changed since it was
introduced 24 years ago in the Pentium III.

I really don't think we're in danger of needing to change this logic.

> I wonder in particular whether with that
> in mind it wouldn't be better to express the check not in terms of
> relations, but in terms of set / clear bits ("bits 3-7 clear AND
> (bit 2 set OR bit 1 clear)"). The code kind of does so already, but
> the variable names don't reflect that (and would hence need to
> change in such an event).

That would reduced clarity.

The bits being set or cleared are trivial for any developer, given the
particularly basic RHS expressions.

The constant names are what relate the bit patterns to the description
of the problem.

>> --- a/xen/arch/x86/hvm/hvm.c
>> +++ b/xen/arch/x86/hvm/hvm.c
>> @@ -302,24 +302,43 @@ void hvm_get_guest_pat(struct vcpu *v, u64 *guest_pat)
>>          *guest_pat = v->arch.hvm.pat_cr;
>>  }
>>  
>> -int hvm_set_guest_pat(struct vcpu *v, uint64_t guest_pat)
>> +/*
>> + * MSR_PAT takes 8 uniform fields, each of which must be a valid architectural
>> + * memory type (0, 1, 4-7).  This is a fully vectorised form of the
>> + * 8-iteration loop over bytes looking for PAT_TYPE_* constants.
> While grep-ing for PAT_TYPE_ will hit this line, I think we want
> every individual type to also be found here when grep-ing for one.
> The actual values aren't going to change, but perhaps the beast
> way to do so would still be by way of BUILD_BUG_ON()s.

Why?  What does that solve or improve?

"pat" is the thing people are going to be looking for if they're
actually trying to find this logic.

(And I bring this patch up specifically after reviewing Demi's series,
where PAT_TYPE_* changes to X86_MT_* but "pat" is still the useful
search term IMO.)

>
>> + */
>> +static bool pat_valid(uint64_t val)
>>  {
>> -    unsigned int i;
>> -    uint64_t tmp;
>> +    /* Yields a non-zero value in any lane which had value greater than 7. */
>> +    uint64_t any_gt_7   =  val & 0xf8f8f8f8f8f8f8f8;
> This and the other constant want to gain UL suffixes.

Fixed.

> (While I'm
> open to be convinced otherwise on the earlier two points, this one
> I'm going to insist on. Yet in case it would end up being the only
> change in need of making, it could of course be done while
> committing.)

I've tweaked the grammar a bit, but I don't think the other changes
would be an improvement.

~Andrew

  reply	other threads:[~2022-12-16 20:54 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-10 13:36 [PATCH] x86/hvm: Improve hvm_set_guest_pat() code generation again Andrew Cooper
2022-08-10 14:06 ` Jan Beulich
2022-12-16 20:53   ` Andrew Cooper [this message]
2022-12-19  7:28     ` Jan Beulich
2023-03-24  0:59       ` Andrew Cooper
2023-03-24  9:32         ` Jan Beulich
2023-03-24 12:15           ` Andrew Cooper

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=77198021-f45c-9d75-c1da-5022d3ca99a2@citrix.com \
    --to=andrew.cooper3@citrix.com \
    --cc=demi@invisiblethingslab.com \
    --cc=edvin.torok@citrix.com \
    --cc=jbeulich@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.