mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Paolo Bonzini <pbonzini@redhat.com>
To: Sairaj Kodilkar <sarunkod@amd.com>,
	Sean Christopherson <seanjc@google.com>
Cc: "H. Peter Anvin" <hpa@zytor.com>, Borislav Petkov <bp@alien8.de>,
	Dave Hansen <dave.hansen@linux.intel.com>,
	Ingo Molnar <mingo@redhat.com>, Thomas Gleixner <tglx@kernel.org>,
	kvm@vger.kernel.org, linux-kernel@vger.kernel.org,
	x86@kernel.org, suravee.suthikulpanit@amd.com,
	vasant.hegde@amd.com, nikunj.dadhania@amd.com,
	Manali.Shukla@amd.com
Subject: Re: [PATCH] KVM: x86: Add support for cmpxchg16b emulation
Date: Tue, 31 Mar 2026 12:20:41 +0200	[thread overview]
Message-ID: <7c2c8f49-5778-4b64-a54e-1ef0f5e24edb@redhat.com> (raw)
In-Reply-To: <99507d9b-c3be-4a77-8b84-79a30c7dd402@amd.com>

On 3/31/26 11:28, Sairaj Kodilkar wrote:
> 
> 
> On 3/12/2026 5:16 PM, Paolo Bonzini wrote:
>> On 3/6/26 16:38, Sean Christopherson wrote:
>>>> -    if (ctxt->dst.bytes == 16)
>>>> +    /* Use of the REX.W prefix promotes operation to 128 bits */
>>>> +    if (ctxt->rex_bits & REX_W)
>>>
>>> Uh, yeah, and the caller specifically pivoted on this size.  I'm a-ok 
>>> with a
>>> sanity check, but it should be exactly that, e.g.
>>>
>>>     if (WARN_ON_ONCE(8 + (ctxt->rex_bits & REX_W) * 8 != ctxt- 
>>> >dst.bytes))
>>>         return X86EMUL_UNHANDLEABLE;
>>
>>
>>> And then emulator_cmpxchg_emulated() should be taught to do 
>>> cmpxchg16b itself:
>>>
>>>     /* guests cmpxchg8b have to be emulated atomically */
>>>     if (bytes > 8 || (bytes & (bytes - 1)))
>>>         goto emul_write;
>>>
>>> That might be a big lift, e.g. to get a 16-byte uaccess version.  If 
>>> it's
>>> unreasonably difficult, we should reject emulation of LOCK CMPXCHG16B.
>>
>> It's actually pretty easy because a kind soul already wrote the
>> cmpxchg8b version of the macros.  Compile-tested only (and checked
>> that the asm does have the instruction):
>>
>> diff --git a/arch/x86/include/asm/uaccess.h b/arch/x86/include/asm/ 
>> uaccess.h
>> index 3a7755c1a4410..f40e815263233 100644
>> --- a/arch/x86/include/asm/uaccess.h
>> +++ b/arch/x86/include/asm/uaccess.h
>> @@ -388,14 +388,13 @@ do {                                    \
>>          *_old = __old;                        \
>>      likely(success);                    })
>>
>> -#ifdef CONFIG_X86_32
>> -#define __try_cmpxchg64_user_asm(_ptr, _pold, _new, label) ({    \
>> +#define __try_cmpxchg8b_16b_user_asm(_b, _ptr, _pold, _new, label) 
>> ({    \
>>      bool success;                            \
>>      __typeof__(_ptr) _old = (__typeof__(_ptr))(_pold);        \
>>      __typeof__(*(_ptr)) __old = *_old;                \
>>      __typeof__(*(_ptr)) __new = (_new);                \
>>      asm_goto_output("\n"                        \
>> -             "1: " LOCK_PREFIX "cmpxchg8b %[ptr]\n"        \
>> +             "1: " LOCK_PREFIX "cmpxchg" #_b "b %[ptr]\n"    \
> 
> Hi Paolo,
> 
> I think its better to keep two implementations separate because this
> adds an additional complexity while passing the upper and lower half
> of the "_new"
> The cmpxchg8b expects ecx as "(u32)((u64)_new >> 32)" where as
> cmpxchg16b expects ecx as "(u64)((u128)_new >> 64)"

Yes, makes sense.  Either way, you have the existing 
__try_cmpxchg64_user_asm as a blueprint and that makes it much easier to 
implement __try_cmpxchg128_user_asm.

Paolo


  reply	other threads:[~2026-03-31 10:20 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-06 10:20 Sairaj Kodilkar
2026-03-06 15:38 ` Sean Christopherson
2026-03-11 11:18   ` Sairaj Kodilkar
2026-03-12 11:46   ` Paolo Bonzini
2026-03-31  9:28     ` Sairaj Kodilkar
2026-03-31 10:20       ` Paolo Bonzini [this message]
2026-03-07  2:39 ` kernel test robot
2026-03-07  5:40 ` kernel test robot

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=7c2c8f49-5778-4b64-a54e-1ef0f5e24edb@redhat.com \
    --to=pbonzini@redhat.com \
    --cc=Manali.Shukla@amd.com \
    --cc=bp@alien8.de \
    --cc=dave.hansen@linux.intel.com \
    --cc=hpa@zytor.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=nikunj.dadhania@amd.com \
    --cc=sarunkod@amd.com \
    --cc=seanjc@google.com \
    --cc=suravee.suthikulpanit@amd.com \
    --cc=tglx@kernel.org \
    --cc=vasant.hegde@amd.com \
    --cc=x86@kernel.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®