All of lore.kernel.org
 help / color / mirror / Atom feed
From: Yosry Ahmed <yosry@kernel.org>
To: Jim Mattson <jmattson@google.com>
Cc: Sean Christopherson <seanjc@google.com>,
	 Paolo Bonzini <pbonzini@redhat.com>,
	Maxim Levitsky <mlevitsk@redhat.com>,
	 Vitaly Kuznetsov <vkuznets@redhat.com>,
	Tom Lendacky <thomas.lendacky@amd.com>,
	kvm@vger.kernel.org,  linux-kernel@vger.kernel.org,
	David Woodhouse <dwmw@amazon.co.uk>
Subject: Re: [PATCH v3 26/27] KVM: nSVM: Use different ASIDs for L1 and L2
Date: Sat, 17 Oct 2026 00:25:27 +0000	[thread overview]
Message-ID: <atK3Y5XxL-zmAvY-@google.com> (raw)
In-Reply-To: <CALMp9eS8KDozNGT9J_uN69W32QEvTbg2JrtRqN0ESi00b_uq=A@mail.gmail.com>

On Fri, Oct 16, 2026 at 02:17:30PM -0700, Jim Mattson wrote:
> On Mon, Oct 12, 2026 at 4:37 PM Yosry Ahmed <yosry@kernel.org> wrote:
> >
> > Now that TLB flushes are properly handled and tracked for L1 vs L2
> > ASIDs, allocate a separate new ASID for L2 for each vCPU, similar to how
> > VMX handles VPIDs. Drop the unconditional flushes and syncs on nested
> > transitions.
> >
> > L1 and L2 can now only share an ASID in two cases:
> > - KVM runs out of ASIDs and uses the fallback ASID for both L1 and L2.
> > - SEV VMs always use the same ASID for SEV-specific requirements.
> >
> > WARN if the ASID is shared otherwise.
> >
> > Initialize last_asid to 0, such that the L2 ASID is always flushed on
> > first nested VM-Enter after it's allocated.
> >
> > On SEV VM migration, free the L2 ASID on the destination vCPU (if nested
> > was already initialized) before overwriting it with the SEV ASID.
> >
> > On a Turin CPU, this results in 8-15% performance boost in CPUID rate
> > microbenchmark [1] and netperf TCP_RR latency/throughput.
> >
> > [1] https://lore.kernel.org/kvm/20231109180646.2963718-1-khorenko@virtuozzo.com/
> >
> > Signed-off-by: Yosry Ahmed <yosry@kernel.org>
> > ---
> >  arch/x86/kvm/svm/nested.c | 32 +++++++++++++++++++++++---------
> >  arch/x86/kvm/svm/sev.c    |  6 ++++--
> >  2 files changed, 27 insertions(+), 11 deletions(-)
> >
> > diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c
> > index 4ffc10bf9232e..6fe68f2ae282d 100644
> > --- a/arch/x86/kvm/svm/nested.c
> > +++ b/arch/x86/kvm/svm/nested.c
> > @@ -711,6 +711,10 @@ static void nested_svm_entry_tlb_flush(struct kvm_vcpu *vcpu)
> >          * context is not tagged by the ASID, so the shadow NPTs cannot be
> >          * reused across different L2 ASIDs.
> >          *
> > +        * Note, last_asid is initialized as 0, so the first nested VM-Enter
> > +        * after setting EFER.SVME will always flush the TLB to avoid using
> > +        * stale entries.
> > +        *
> >          * If L1 requested a full TLB flush for all ASIDs (including its own),
> >          * L1's own ASID is also flushed on nested VM-Exit, before running L1.
> >          *
> > @@ -727,12 +731,10 @@ static void nested_svm_entry_tlb_flush(struct kvm_vcpu *vcpu)
> >          * If L1 and L2 share the same ASID in hardware (when using the fallback
> >          * ASID for both, or for SEV guests), flush it on nested transitions.
> >          */
> > -       if (svm->asid == svm->nested.asid02)
> > +       if (svm->asid == svm->nested.asid02) {
> > +               WARN_ON_ONCE(svm->asid != fallback_asid && !is_sev_guest(vcpu));
> >                 kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
> > -
> > -       /* TODO: optimize unconditional TLB flush/MMU sync */
> > -       kvm_make_request(KVM_REQ_MMU_SYNC, vcpu);
> > -       kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
> > +       }
> 
> Per the APM, volume 2, Section 15.16.1, "TLB Flush":
> 
> > All TLB entries belonging to all ASIDs are flushed by SMI, RSM, MTRR modifications, IORR modifications, and access to other system MSRs that affect address translation.
> 
> With two separate ASIDs and the unconditional flush removed, do we
> flush both ASIDs in the above instances?

SMI and RSM, yeah I think so. kvm_smm_changed() ->
kvm_mmu_reset_context() deletes all roots, and kvm_mmu_load() will flush
the current ASID when a new root is loaded.

MTRR writes seem to do nothing beyond store the value, probably since
0a7b73559b39 ("KVM: x86: Remove VMX support for virtualizing guest MTRR
memtypes"). It is documented as an errata but I maybe KVM should still
flush the TLB? If that's a real bug, it's separate from this series tho.
We just need to make sure if we fix it, we should flush both ASIDs. We
can probably just reset the MMU context here too?

I can't find anything related to IORR in KVM.

For MSRs, I think the relevant ones are EFER and PAT:

- For EFER, a change to KVM_MMU_EFER_ROLE_BITS resets the MMU similar to
  SMI/RSM, so covers both ASIDs. Clearing EFER.SVME frees L2's ASID and
  re-allocating an ASID will flush it by clearing last_asid.

- For PAT, I am not sure if that falls under "system MSRs that affect
  address translation", but KVM isn't flushing for L1 today on PAT
  writes anyway, so I hope not?

> 
> Moreover, does this change the semantics of PV TLB flush? In
> record_steal_time(), we have:
> 
> if (st_preempted & KVM_VCPU_FLUSH_TLB)
>         kvm_vcpu_flush_tlb_guest(vcpu);
> 
> IIUC, an idle vCPU that wakes into L2 will flush only L2's ASID. In
> the past, with a shared ASID, L1's ASID was always flushed on emulated
> VM-exit anyway. Now, L1's ASID will be unaffected. Though I can't find
> any real documentation on PV TLB flush, it *seems* like the remote
> vCPU would want L1's ASID to be flushed. OTOH, this change makes nSVM
> behave just like nVMX.

I am honestly not sure. It seems like we should be flushing the context
that issued the PV flush. But I also suppose (aside from Hyper-V) all L2
PV calls should go to L1, not L0, so should we always flush L1's context
here?

It would be a pre-existing bug, but this series would be adding it to
SVM as well. Adding David here since his name appears all over the git
log for that code, so hopefully he'll tell us for sure.

> 
> 
> Nit: with this change, the following stale comment in
> svm_flush_tlb_asid() needs to be updated:
> 
> /*
> * Flush only the current ASID even if the TLB flush was invoked via
> * kvm_flush_remote_tlbs(). Although flushing remote TLBs requires all
> * ASIDs to be flushed, KVM uses a single ASID for L1 and L2, and
> * unconditionally does a TLB flush on both nested VM-Enter and nested
> * VM-Exit (via kvm_mmu_reset_context()).
> */

Will fix.

Thanks!

  reply	other threads:[~2026-10-17  0:25 UTC|newest]

Thread overview: 88+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-12 23:36 [PATCH v3 00/27] KVM: nSVM: Optimize nSVM TLB flushes Yosry Ahmed
2026-10-12 23:36 ` [PATCH v3 01/27] KVM: nSVM: Flush the TLB after forcefully leaving nested Yosry Ahmed
2026-10-14 15:02   ` Jim Mattson
2026-10-14 15:18     ` Yosry Ahmed
2026-10-16 19:32       ` Jim Mattson
2026-10-12 23:36 ` [PATCH v3 02/27] KVM: SVM: Document number of ASIDs CPUID setting Yosry Ahmed
2026-10-14 15:07   ` Jim Mattson
2026-10-12 23:36 ` [PATCH v3 03/27] KVM: VMX: Generalize VPID allocation to be vendor-neutral Yosry Ahmed
2026-10-14 18:22   ` Jim Mattson
2026-10-14 20:38     ` Yosry Ahmed
2026-10-16 19:34       ` Jim Mattson
2026-10-12 23:36 ` [PATCH v3 04/27] KVM: x86/mmu: Support specifying reserved TLB tags Yosry Ahmed
2026-10-14 21:49   ` Jim Mattson
2026-10-14 22:07     ` Yosry Ahmed
2026-10-12 23:36 ` [PATCH v3 05/27] KVM: SVM: Add helpers to set/clear ASID flush in VMCB Yosry Ahmed
2026-10-14 22:00   ` Jim Mattson
2026-10-14 22:03     ` Yosry Ahmed
2026-10-14 22:22       ` Jim Mattson
2026-10-14 22:25         ` Yosry Ahmed
2026-10-14 22:32           ` Jim Mattson
2026-10-12 23:36 ` [PATCH v3 06/27] KVM: SVM: Fallback to flush everything if FLUSHBYASID is not available Yosry Ahmed
2026-10-14 22:45   ` Jim Mattson
2026-10-14 22:50     ` Yosry Ahmed
2026-10-14 23:27       ` Jim Mattson
2026-10-12 23:36 ` [PATCH v3 07/27] KVM: SEV: Do ASID initialization at VMCB initialization Yosry Ahmed
2026-10-14 23:31   ` Jim Mattson
2026-10-12 23:36 ` [PATCH v3 08/27] KVM: SEV: Expose sev_get_asid() outside of sev.c Yosry Ahmed
2026-10-14 23:33   ` Jim Mattson
2026-10-12 23:36 ` [PATCH v3 09/27] KVM: SEV: Explicitly initialize the per vCPU ASID on SEV VM migration Yosry Ahmed
2026-10-14 23:35   ` Jim Mattson
2026-10-12 23:36 ` [PATCH v3 10/27] KVM: SVM: Use a static ASID per vCPU Yosry Ahmed
2026-10-12 23:54   ` sashiko-bot
2026-10-12 23:55     ` Yosry Ahmed
2026-10-12 23:36 ` [PATCH v3 11/27] KVM: SVM: Only flush the fallback ASID when used by a different vCPU Yosry Ahmed
2026-10-15 21:44   ` Jim Mattson
2026-10-15 22:03     ` Yosry Ahmed
2026-10-16 19:36       ` Jim Mattson
2026-10-12 23:36 ` [PATCH v3 12/27] KVM: nSVM: Drop svm->nested.initialized Yosry Ahmed
2026-10-15 21:48   ` Jim Mattson
2026-10-12 23:36 ` [PATCH v3 13/27] KVM: nSVM: Add a placeholder ASID for L2 Yosry Ahmed
2026-10-13  0:01   ` sashiko-bot
2026-10-13 14:25     ` Yosry Ahmed
2026-10-15 21:55   ` Jim Mattson
2026-10-15 22:10     ` Yosry Ahmed
2026-10-15 22:21       ` Jim Mattson
2026-10-15 22:27         ` Yosry Ahmed
2026-10-12 23:36 ` [PATCH v3 14/27] KVM: x86: hyper-v: Rename kvm_hv_vcpu_purge_flush_tlb() Yosry Ahmed
2026-10-15 21:58   ` Jim Mattson
2026-10-12 23:36 ` [PATCH v3 15/27] KVM: x86: hyper-v: Allow purging all TLB flush FIFOs Yosry Ahmed
2026-10-12 23:56   ` sashiko-bot
2026-10-13 14:42     ` Yosry Ahmed
2026-10-15 22:02   ` Jim Mattson
2026-10-12 23:36 ` [PATCH v3 16/27] KVM: nSVM: Flush both L1 and L2 ASIDs on KVM_REQ_TLB_FLUSH Yosry Ahmed
2026-10-15 22:05   ` Jim Mattson
2026-10-12 23:36 ` [PATCH v3 17/27] KVM: nSVM: Always switch VMCB before leaving guest mode Yosry Ahmed
2026-10-15 22:08   ` Jim Mattson
2026-10-12 23:36 ` [PATCH v3 18/27] KVM: nSVM: Split nested_svm_transition_tlb_flush() into entry/exit fns Yosry Ahmed
2026-10-15 22:10   ` Jim Mattson
2026-10-12 23:36 ` [PATCH v3 19/27] KVM: nSVM: Service local TLB flushes before nested transitions Yosry Ahmed
2026-10-15 22:19   ` Jim Mattson
2026-10-15 22:34     ` Yosry Ahmed
2026-10-16 19:39       ` Jim Mattson
2026-10-12 23:36 ` [PATCH v3 20/27] KVM: x86: Add KVM_REQ_MMU_SYNC_ALL_ROOTS Yosry Ahmed
2026-10-15 22:24   ` Jim Mattson
2026-10-12 23:36 ` [PATCH v3 21/27] KVM: nSVM: Handle nested TLB flush requests through TLB_CONTROL Yosry Ahmed
2026-10-15 22:33   ` Jim Mattson
2026-10-12 23:36 ` [PATCH v3 22/27] KVM: nSVM: Flush the TLB if L1 changes L2's ASID in vmcb12 Yosry Ahmed
2026-10-15 23:09   ` Jim Mattson
2026-10-15 23:20     ` Yosry Ahmed
2026-10-15 23:50       ` Jim Mattson
2026-10-15 23:56         ` Yosry Ahmed
2026-10-16 19:24           ` Jim Mattson
2026-10-12 23:36 ` [PATCH v3 23/27] KVM: nSVM: Do not reset TLB_CONTROL in vmcb02 on nested VM-Enter Yosry Ahmed
2026-10-15 23:16   ` Jim Mattson
2026-10-15 23:22     ` Yosry Ahmed
2026-10-12 23:36 ` [PATCH v3 24/27] KVM: nSVM: Flush L2's ASID when emulating INVLPGA Yosry Ahmed
2026-10-15 23:37   ` Jim Mattson
2026-10-15 23:42     ` Yosry Ahmed
2026-10-16  0:53       ` Sean Christopherson
2026-10-16 19:41         ` Jim Mattson
2026-10-12 23:36 ` [PATCH v3 25/27] KVM: nSVM: Flush the ASID on nested transitions if shared by L1 and L2 Yosry Ahmed
2026-10-15 23:42   ` Jim Mattson
2026-10-12 23:36 ` [PATCH v3 26/27] KVM: nSVM: Use different ASIDs for " Yosry Ahmed
2026-10-16 21:17   ` Jim Mattson
2026-10-17  0:25     ` Yosry Ahmed [this message]
2026-10-12 23:36 ` [PATCH v3 27/27] KVM: selftests: Add a test for nested TLB flushes Yosry Ahmed
2026-10-14 13:23 ` [PATCH v3 00/27] KVM: nSVM: Optimize nSVM " Jim Mattson
2026-10-14 14:37   ` Yosry Ahmed

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=atK3Y5XxL-zmAvY-@google.com \
    --to=yosry@kernel.org \
    --cc=dwmw@amazon.co.uk \
    --cc=jmattson@google.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mlevitsk@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=seanjc@google.com \
    --cc=thomas.lendacky@amd.com \
    --cc=vkuznets@redhat.com \
    /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.