Re: [RFC PATCH v2 04/25] KVM: x86/mmu: Support specifying a minimum TLB tag

From: Yosry Ahmed

Date: Thu Jul 23 2026 - 01:05:48 EST


On Wed, Jul 22, 2026 at 05:28:00PM -0700, Sean Christopherson wrote:
> On Tue, Jun 16, 2026, Yosry Ahmed wrote:
> > In preparation for using the TLB tags allocator for SVM, which has a
> > range of ASIDs allocated for SEV/SNP, pass in a minimum TLB tag when
> > initializing the TLB tags allocator. The bitmap is conceptually shifted
> > such that bit=0 corresponds to tag=min.
> >
> > Specifying the minimum value during initialization also makes the API
> > clearer, as the passed number of tags becomes the actual number of
> > *usable* tags, and tag=0 is explicitly excluded by the caller.
> >
> > No functional change intended for VMX as VPID=0 is not used anyway.
> >
> > Signed-off-by: Yosry Ahmed <yosry@xxxxxxxxxx>
> > ---
> > arch/x86/kvm/mmu.h | 2 +-
> > arch/x86/kvm/mmu/mmu.c | 38 +++++++++++++++++++++++---------------
> > arch/x86/kvm/vmx/vmx.h | 3 ++-
> > 3 files changed, 26 insertions(+), 17 deletions(-)
> >
> > diff --git a/arch/x86/kvm/mmu.h b/arch/x86/kvm/mmu.h
> > index 9a2916012cbff..cfffee92b8b71 100644
> > --- a/arch/x86/kvm/mmu.h
> > +++ b/arch/x86/kvm/mmu.h
> > @@ -337,7 +337,7 @@ static inline bool kvm_is_gfn_alias(struct kvm *kvm, gfn_t gfn)
> >
> > typedef unsigned int kvm_tlb_tag_t;
> >
> > -int kvm_init_tlb_tags(unsigned int nr);
> > +int kvm_init_tlb_tags(kvm_tlb_tag_t min, unsigned int nr);
> > void kvm_destroy_tlb_tags(void);
> > kvm_tlb_tag_t kvm_alloc_tlb_tag(void);
> > void kvm_free_tlb_tag(kvm_tlb_tag_t tag);
> > diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c
> > index e021ed562502f..bf2e0c2205631 100644
> > --- a/arch/x86/kvm/mmu/mmu.c
> > +++ b/arch/x86/kvm/mmu/mmu.c
> > @@ -8197,24 +8197,26 @@ static struct {
> > spinlock_t lock;
> > unsigned long *bitmap;
> > unsigned int nr;
> > + kvm_tlb_tag_t min;
> > } tlb_tags;
> >
> > -int kvm_init_tlb_tags(unsigned int nr)
> > +int kvm_init_tlb_tags(kvm_tlb_tag_t min, unsigned int nr)
>
> Hmm, I think this code should handle "nr - min" instead of requiring the caller
> to do so. Because people that know how SEV+ works, but aren't familiar with this
> code (like I am right now), will probably find it confusing that nothing in here
> appears to account for the "reserved" ASIDs.
>
> Maybe this? With documentation that reserved ASIDs start at 0, inclusive, and
> are contiguous.
>
> int kvm_init_tlb_tags(unsigned int nr, unsigned nr_reserved)
> {
>
> }

So essentially make the reserved tags included in 'nr', and min becomes
nr_reserved. I wanted to keep it generic by not factoring in the
assumptions about the reserved tags, but we can revisit later if ever
needed (very unlikely).

I am fine with this suggestion.

>
> And then tlb_tags.min becomes something like tlb_tags.offset (that's a terrible
> name, but you probably get the gist).