Re: [PATCH v3 04/27] KVM: x86/mmu: Support specifying reserved TLB tags
From: Yosry Ahmed
Date: Wed Oct 14 2026 - 18:08:20 EST
On Wed, Oct 14, 2026 at 2:50 PM Jim Mattson <jmattson@xxxxxxxxxx> wrote:
>
> On Mon, Oct 12, 2026 at 4:36 PM Yosry Ahmed <yosry@xxxxxxxxxx> wrote:
> >
> > In preparation for using the TLB tags allocator for SVM, which has a
> > range of ASIDs allocated for SEV/SNP, pass in the number of reserved TLB
> > tags when initializing the TLB tags allocator. The bitmap is
> > conceptually shifted such that bit=0 corresponds to tag=nr_reserved.
> >
> > Specifying the minimum value during initialization also makes the API
> > clearer, as tag=0 is now explicitly reserved by the caller.
> >
> > No functional change intended for VMX as VPID=0 was already implicitly
> > reserved.
> >
> > Signed-off-by: Yosry Ahmed <yosry@xxxxxxxxxx>
> > ---
> > arch/x86/kvm/mmu.h | 2 +-
> > arch/x86/kvm/mmu/mmu.c | 42 ++++++++++++++++++++++++++++--------------
> > arch/x86/kvm/vmx/vmx.h | 2 +-
> > 3 files changed, 30 insertions(+), 16 deletions(-)
> >
> > diff --git a/arch/x86/kvm/mmu.h b/arch/x86/kvm/mmu.h
> > index de79e002edf8f..b443a5083bfb0 100644
> > --- a/arch/x86/kvm/mmu.h
> > +++ b/arch/x86/kvm/mmu.h
> > @@ -413,7 +413,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(unsigned int nr_total, unsigned int nr_reserved);
> > 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 97d49247d9d82..c096c1a2b4684 100644
> > --- a/arch/x86/kvm/mmu/mmu.c
> > +++ b/arch/x86/kvm/mmu/mmu.c
> > @@ -8182,9 +8182,17 @@ static struct {
> > spinlock_t lock;
> > unsigned long *bitmap;
> > unsigned int nr;
> > + kvm_tlb_tag_t start;
> > } tlb_tags;
> >
> > -int kvm_init_tlb_tags(unsigned int nr)
> > +/*
> > + * Initialize @nr_total TLB tags, where @nr_reserved of those tags are reserved.
> > + * Reserved tags start at 0 and are contiguous. Hence, usable tags are
> > + * [nr_reserved, nr_total). At least one reserved tag is required, as 0 is the
> > + * host TLB tag on both VMX and AMD, and is the value returned on a failed tag
>
> Nit: VMX and SVM -or- Intel and AMD
LOL, how did I end up with that.
>
> > + * allocation.
> > + */
> > +int kvm_init_tlb_tags(unsigned int nr_total, unsigned int nr_reserved)
> > {
> > /*
> > * Limit the number of TLB tags to VMX's hardcoded maximum of 0x10000
> > @@ -8194,7 +8202,12 @@ int kvm_init_tlb_tags(unsigned int nr)
> > * need a solution that works for both Intel and AMD.
> > */
> > const unsigned int MAX_NR_TLB_TAGS = VMX_NR_VPIDS;
> > + unsigned int nr;
> >
> > + if (WARN_ON_ONCE(!nr_reserved || nr_reserved > nr_total))
> > + return -EINVAL;
> > +
> > + nr = nr_total - nr_reserved;
> > if (!nr)
> > return 0;
> >
> > @@ -8208,13 +8221,8 @@ int kvm_init_tlb_tags(unsigned int nr)
> > if (!tlb_tags.bitmap)
> > return -ENOMEM;
> >
> > - /*
> > - * 0 is the host's TLB tag for both VMX's VPID and SVM's ASID, and is
> > - * returned on failed allocations (e.g. no more tags left).
> > - */
> > - __set_bit(0, tlb_tags.bitmap);
> > -
> > tlb_tags.nr = nr;
> > + tlb_tags.start = nr_reserved;
>
> Can you also clear tlb_tags.start in kvm_destroy_tlb_tags()?
Can do.