From: Sean Christopherson <seanjc@google.com>
To: Sean Christopherson <seanjc@google.com>,
Paolo Bonzini <pbonzini@redhat.com>,
Joerg Roedel <joro@8bytes.org>,
David Woodhouse <dwmw2@infradead.org>,
Lu Baolu <baolu.lu@linux.intel.com>
Cc: kvm@vger.kernel.org, iommu@lists.linux.dev,
linux-kernel@vger.kernel.org, Sairaj Kodilkar <sarunkod@amd.com>,
Vasant Hegde <vasant.hegde@amd.com>,
Maxim Levitsky <mlevitsk@redhat.com>,
Joao Martins <joao.m.martins@oracle.com>,
Francesco Lavra <francescolavra.fl@gmail.com>,
David Matlack <dmatlack@google.com>
Subject: [PATCH v2 12/59] KVM: SVM: Track AVIC tables as natively sized pointers, not "struct pages"
Date: Thu, 22 May 2025 17:59:17 -0700 [thread overview]
Message-ID: <20250523010004.3240643-13-seanjc@google.com> (raw)
In-Reply-To: <20250523010004.3240643-1-seanjc@google.com>
Allocate and track AVIC's logical and physical tables as u32 and u64
pointers respectively, as managing the pages as "struct page" pointers
adds an almost absurd amount of boilerplate and complexity. E.g. with
page_address() out of the way, svm->avic_physical_id_cache becomes
completely superfluous, and will be removed in a future cleanup.
No functional change intended.
Tested-by: Sairaj Kodilkar <sarunkod@amd.com>
Signed-off-by: Sean Christopherson <seanjc@google.com>
---
arch/x86/kvm/svm/avic.c | 49 ++++++++++++++---------------------------
arch/x86/kvm/svm/svm.h | 4 ++--
2 files changed, 18 insertions(+), 35 deletions(-)
diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c
index 948bab48083b..bf18b0b643d9 100644
--- a/arch/x86/kvm/svm/avic.c
+++ b/arch/x86/kvm/svm/avic.c
@@ -172,10 +172,8 @@ void avic_vm_destroy(struct kvm *kvm)
if (!enable_apicv)
return;
- if (kvm_svm->avic_logical_id_table_page)
- __free_page(kvm_svm->avic_logical_id_table_page);
- if (kvm_svm->avic_physical_id_table_page)
- __free_page(kvm_svm->avic_physical_id_table_page);
+ free_page((unsigned long)kvm_svm->avic_logical_id_table);
+ free_page((unsigned long)kvm_svm->avic_physical_id_table);
spin_lock_irqsave(&svm_vm_data_hash_lock, flags);
hash_del(&kvm_svm->hnode);
@@ -188,27 +186,19 @@ int avic_vm_init(struct kvm *kvm)
int err = -ENOMEM;
struct kvm_svm *kvm_svm = to_kvm_svm(kvm);
struct kvm_svm *k2;
- struct page *p_page;
- struct page *l_page;
u32 vm_id;
if (!enable_apicv)
return 0;
- /* Allocating physical APIC ID table (4KB) */
- p_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
- if (!p_page)
+ kvm_svm->avic_physical_id_table = (void *)get_zeroed_page(GFP_KERNEL_ACCOUNT);
+ if (!kvm_svm->avic_physical_id_table)
goto free_avic;
- kvm_svm->avic_physical_id_table_page = p_page;
-
- /* Allocating logical APIC ID table (4KB) */
- l_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
- if (!l_page)
+ kvm_svm->avic_logical_id_table = (void *)get_zeroed_page(GFP_KERNEL_ACCOUNT);
+ if (!kvm_svm->avic_logical_id_table)
goto free_avic;
- kvm_svm->avic_logical_id_table_page = l_page;
-
spin_lock_irqsave(&svm_vm_data_hash_lock, flags);
again:
vm_id = next_vm_id = (next_vm_id + 1) & AVIC_VM_ID_MASK;
@@ -242,12 +232,10 @@ static phys_addr_t avic_get_backing_page_address(struct vcpu_svm *svm)
void avic_init_vmcb(struct vcpu_svm *svm, struct vmcb *vmcb)
{
struct kvm_svm *kvm_svm = to_kvm_svm(svm->vcpu.kvm);
- phys_addr_t lpa = __sme_set(page_to_phys(kvm_svm->avic_logical_id_table_page));
- phys_addr_t ppa = __sme_set(page_to_phys(kvm_svm->avic_physical_id_table_page));
vmcb->control.avic_backing_page = avic_get_backing_page_address(svm);
- vmcb->control.avic_logical_id = lpa;
- vmcb->control.avic_physical_id = ppa;
+ vmcb->control.avic_logical_id = __sme_set(__pa(kvm_svm->avic_logical_id_table));
+ vmcb->control.avic_physical_id = __sme_set(__pa(kvm_svm->avic_physical_id_table));
vmcb->control.avic_vapic_bar = APIC_DEFAULT_PHYS_BASE;
if (kvm_apicv_activated(svm->vcpu.kvm))
@@ -261,7 +249,7 @@ static int avic_init_backing_page(struct kvm_vcpu *vcpu)
struct kvm_svm *kvm_svm = to_kvm_svm(vcpu->kvm);
struct vcpu_svm *svm = to_svm(vcpu);
u32 id = vcpu->vcpu_id;
- u64 *table, new_entry;
+ u64 new_entry;
/*
* Inhibit AVIC if the vCPU ID is bigger than what is supported by AVIC
@@ -277,8 +265,8 @@ static int avic_init_backing_page(struct kvm_vcpu *vcpu)
return 0;
}
- BUILD_BUG_ON((AVIC_MAX_PHYSICAL_ID + 1) * sizeof(*table) > PAGE_SIZE ||
- (X2AVIC_MAX_PHYSICAL_ID + 1) * sizeof(*table) > PAGE_SIZE);
+ BUILD_BUG_ON((AVIC_MAX_PHYSICAL_ID + 1) * sizeof(new_entry) > PAGE_SIZE ||
+ (X2AVIC_MAX_PHYSICAL_ID + 1) * sizeof(new_entry) > PAGE_SIZE);
if (WARN_ON_ONCE(!vcpu->arch.apic->regs))
return -EINVAL;
@@ -297,18 +285,16 @@ static int avic_init_backing_page(struct kvm_vcpu *vcpu)
return ret;
}
- /* Setting AVIC backing page address in the phy APIC ID table */
- table = page_address(kvm_svm->avic_physical_id_table_page);
-
/* Note, fls64() returns the bit position, +1. */
BUILD_BUG_ON(__PHYSICAL_MASK_SHIFT >
fls64(AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK));
+ /* Setting AVIC backing page address in the phy APIC ID table */
new_entry = avic_get_backing_page_address(svm) |
AVIC_PHYSICAL_ID_ENTRY_VALID_MASK;
- WRITE_ONCE(table[id], new_entry);
+ WRITE_ONCE(kvm_svm->avic_physical_id_table[id], new_entry);
- svm->avic_physical_id_cache = &table[id];
+ svm->avic_physical_id_cache = &kvm_svm->avic_physical_id_table[id];
return 0;
}
@@ -442,7 +428,7 @@ static int avic_kick_target_vcpus_fast(struct kvm *kvm, struct kvm_lapic *source
if (apic_x2apic_mode(source))
avic_logical_id_table = NULL;
else
- avic_logical_id_table = page_address(kvm_svm->avic_logical_id_table_page);
+ avic_logical_id_table = kvm_svm->avic_logical_id_table;
/*
* AVIC is inhibited if vCPUs aren't mapped 1:1 with logical
@@ -544,7 +530,6 @@ unsigned long avic_vcpu_get_apicv_inhibit_reasons(struct kvm_vcpu *vcpu)
static u32 *avic_get_logical_id_entry(struct kvm_vcpu *vcpu, u32 ldr, bool flat)
{
struct kvm_svm *kvm_svm = to_kvm_svm(vcpu->kvm);
- u32 *logical_apic_id_table;
u32 cluster, index;
ldr = GET_APIC_LOGICAL_ID(ldr);
@@ -565,9 +550,7 @@ static u32 *avic_get_logical_id_entry(struct kvm_vcpu *vcpu, u32 ldr, bool flat)
return NULL;
index += (cluster << 2);
- logical_apic_id_table = (u32 *) page_address(kvm_svm->avic_logical_id_table_page);
-
- return &logical_apic_id_table[index];
+ return &kvm_svm->avic_logical_id_table[index];
}
static void avic_ldr_write(struct kvm_vcpu *vcpu, u8 g_physical_id, u32 ldr)
diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h
index 71e3c003580e..ec5d77d42a49 100644
--- a/arch/x86/kvm/svm/svm.h
+++ b/arch/x86/kvm/svm/svm.h
@@ -123,8 +123,8 @@ struct kvm_svm {
/* Struct members for AVIC */
u32 avic_vm_id;
- struct page *avic_logical_id_table_page;
- struct page *avic_physical_id_table_page;
+ u32 *avic_logical_id_table;
+ u64 *avic_physical_id_table;
struct hlist_node hnode;
struct kvm_sev_info sev_info;
--
2.49.0.1151.ga128411c76-goog
next prev parent reply other threads:[~2025-05-23 1:00 UTC|newest]
Thread overview: 68+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-05-23 0:59 [PATCH v2 00/59] KVM: iommu: Overhaul device posted IRQs support Sean Christopherson
2025-05-23 0:59 ` [PATCH v2 01/59] KVM: x86: Pass new routing entries and irqfd when updating IRTEs Sean Christopherson
2025-05-23 0:59 ` [PATCH v2 02/59] KVM: SVM: Track per-vCPU IRTEs using kvm_kernel_irqfd structure Sean Christopherson
2025-05-23 0:59 ` [PATCH v2 03/59] KVM: SVM: Delete IRTE link from previous vCPU before setting new IRTE Sean Christopherson
2025-05-23 0:59 ` [PATCH v2 04/59] iommu/amd: KVM: SVM: Delete now-unused cached/previous GA tag fields Sean Christopherson
2025-05-23 0:59 ` [PATCH v2 05/59] KVM: SVM: Delete IRTE link from previous vCPU irrespective of new routing Sean Christopherson
2025-05-23 0:59 ` [PATCH v2 06/59] KVM: SVM: Drop pointless masking of default APIC base when setting V_APIC_BAR Sean Christopherson
2025-05-23 0:59 ` [PATCH v2 07/59] KVM: SVM: Drop pointless masking of kernel page pa's with AVIC HPA masks Sean Christopherson
2025-05-23 0:59 ` [PATCH v2 08/59] KVM: SVM: Add helper to deduplicate code for getting AVIC backing page Sean Christopherson
2025-05-23 0:59 ` [PATCH v2 09/59] KVM: SVM: Drop vcpu_svm's pointless avic_backing_page field Sean Christopherson
2025-05-23 0:59 ` [PATCH v2 10/59] KVM: SVM: Inhibit AVIC if ID is too big instead of rejecting vCPU creation Sean Christopherson
2025-05-23 0:59 ` [PATCH v2 11/59] KVM: SVM: Drop redundant check in AVIC code on ID during " Sean Christopherson
2025-05-23 0:59 ` Sean Christopherson [this message]
2025-05-23 0:59 ` [PATCH v2 13/59] KVM: SVM: Drop superfluous "cache" of AVIC Physical ID entry pointer Sean Christopherson
2025-05-23 0:59 ` [PATCH v2 14/59] KVM: VMX: Move enable_ipiv knob to common x86 Sean Christopherson
2025-05-23 0:59 ` [PATCH v2 15/59] KVM: SVM: Add enable_ipiv param, never set IsRunning if disabled Sean Christopherson
2025-05-23 0:59 ` [PATCH v2 16/59] KVM: SVM: Disable (x2)AVIC IPI virtualization if CPU has erratum #1235 Sean Christopherson
2025-05-23 0:59 ` [PATCH v2 17/59] KVM: VMX: Suppress PI notifications whenever the vCPU is put Sean Christopherson
2025-05-23 0:59 ` [PATCH v2 18/59] KVM: SVM: Add a comment to explain why avic_vcpu_blocking() ignores IRQ blocking Sean Christopherson
2025-05-23 0:59 ` [PATCH v2 19/59] iommu/amd: KVM: SVM: Use pi_desc_addr to derive ga_root_ptr Sean Christopherson
2025-05-23 0:59 ` [PATCH v2 20/59] iommu/amd: KVM: SVM: Pass NULL @vcpu_info to indicate "not guest mode" Sean Christopherson
2025-05-23 0:59 ` [PATCH v2 21/59] KVM: SVM: Stop walking list of routing table entries when updating IRTE Sean Christopherson
2025-05-23 0:59 ` [PATCH v2 22/59] KVM: VMX: " Sean Christopherson
2025-05-23 0:59 ` [PATCH v2 23/59] KVM: SVM: Extract SVM specific code out of get_pi_vcpu_info() Sean Christopherson
2025-05-23 0:59 ` [PATCH v2 24/59] KVM: x86: Move IRQ routing/delivery APIs from x86.c => irq.c Sean Christopherson
2025-05-23 0:59 ` [PATCH v2 25/59] KVM: x86: Nullify irqfd->producer after updating IRTEs Sean Christopherson
2025-05-23 0:59 ` [PATCH v2 26/59] KVM: x86: Dedup AVIC vs. PI code for identifying target vCPU Sean Christopherson
2025-05-23 0:59 ` [PATCH v2 27/59] KVM: x86: Move posted interrupt tracepoint to common code Sean Christopherson
2025-05-23 0:59 ` [PATCH v2 28/59] KVM: SVM: Clean up return handling in avic_pi_update_irte() Sean Christopherson
2025-05-23 0:59 ` [PATCH v2 29/59] iommu: KVM: Split "struct vcpu_data" into separate AMD vs. Intel structs Sean Christopherson
2025-05-23 0:59 ` [PATCH v2 30/59] KVM: Don't WARN if updating IRQ bypass route fails Sean Christopherson
2025-05-23 0:59 ` [PATCH v2 31/59] KVM: Fold kvm_arch_irqfd_route_changed() into kvm_arch_update_irqfd_routing() Sean Christopherson
2025-05-23 0:59 ` [PATCH v2 32/59] KVM: x86: Track irq_bypass_vcpu in common x86 code Sean Christopherson
2025-05-23 0:59 ` [PATCH v2 33/59] KVM: x86: Skip IOMMU IRTE updates if there's no old or new vCPU being targeted Sean Christopherson
2025-05-23 0:59 ` [PATCH v2 34/59] KVM: x86: Don't update IRTE entries when old and new routes were !MSI Sean Christopherson
2025-05-23 0:59 ` [PATCH v2 35/59] KVM: SVM: Revert IRTE to legacy mode if IOMMU doesn't provide IR metadata Sean Christopherson
2025-05-23 0:59 ` [PATCH v2 36/59] KVM: SVM: Take and hold ir_list_lock across IRTE updates in IOMMU Sean Christopherson
2025-05-23 0:59 ` [PATCH v2 37/59] iommu/amd: Document which IRTE fields amd_iommu_update_ga() can modify Sean Christopherson
2025-05-23 0:59 ` [PATCH v2 38/59] iommu/amd: KVM: SVM: Infer IsRun from validity of pCPU destination Sean Christopherson
2025-05-30 10:13 ` Sairaj Kodilkar
2025-06-02 21:59 ` Sean Christopherson
2025-05-23 0:59 ` [PATCH v2 39/59] iommu/amd: Factor out helper for manipulating IRTE GA/CPU info Sean Christopherson
2025-05-23 0:59 ` [PATCH v2 40/59] iommu/amd: KVM: SVM: Set pCPU info in IRTE when setting vCPU affinity Sean Christopherson
2025-05-23 0:59 ` [PATCH v2 41/59] iommu/amd: KVM: SVM: Add IRTE metadata to affined vCPU's list if AVIC is inhibited Sean Christopherson
2025-05-30 10:06 ` Sairaj Kodilkar
2025-06-02 22:26 ` Sean Christopherson
2025-05-23 0:59 ` [PATCH v2 42/59] KVM: SVM: Don't check for assigned device(s) when updating affinity Sean Christopherson
2025-05-23 0:59 ` [PATCH v2 43/59] KVM: SVM: Don't check for assigned device(s) when activating AVIC Sean Christopherson
2025-05-23 0:59 ` [PATCH v2 44/59] KVM: SVM: WARN if (de)activating guest mode in IOMMU fails Sean Christopherson
2025-05-23 0:59 ` [PATCH v2 45/59] KVM: SVM: Process all IRTEs on affinity change even if one update fails Sean Christopherson
2025-05-23 0:59 ` [PATCH v2 46/59] KVM: SVM: WARN if updating IRTE GA fields in IOMMU fails Sean Christopherson
2025-05-23 0:59 ` [PATCH v2 47/59] KVM: x86: Drop superfluous "has assigned device" check in kvm_pi_update_irte() Sean Christopherson
2025-05-23 0:59 ` [PATCH v2 48/59] KVM: x86: WARN if IRQ bypass isn't supported " Sean Christopherson
2025-05-23 0:59 ` [PATCH v2 49/59] KVM: x86: WARN if IRQ bypass routing is updated without in-kernel local APIC Sean Christopherson
2025-05-23 0:59 ` [PATCH v2 50/59] KVM: SVM: WARN if ir_list is non-empty at vCPU free Sean Christopherson
2025-05-23 0:59 ` [PATCH v2 51/59] KVM: x86: Decouple device assignment from IRQ bypass Sean Christopherson
2025-05-23 0:59 ` [PATCH v2 52/59] KVM: VMX: WARN if VT-d Posted IRQs aren't possible when starting " Sean Christopherson
2025-05-23 0:59 ` [PATCH v2 53/59] KVM: SVM: Use vcpu_idx, not vcpu_id, for GA log tag/metadata Sean Christopherson
2025-05-23 0:59 ` [PATCH v2 54/59] iommu/amd: WARN if KVM calls GA IRTE helpers without virtual APIC support Sean Christopherson
2025-05-23 1:00 ` [PATCH v2 55/59] KVM: SVM: Fold avic_set_pi_irte_mode() into its sole caller Sean Christopherson
2025-05-23 1:00 ` [PATCH v2 56/59] KVM: SVM: Don't check vCPU's blocking status when toggling AVIC on/off Sean Christopherson
2025-05-23 1:00 ` [PATCH v2 57/59] KVM: SVM: Consolidate IRTE update " Sean Christopherson
2025-05-23 1:00 ` [PATCH v2 58/59] iommu/amd: KVM: SVM: Allow KVM to control need for GA log interrupts Sean Christopherson
2025-05-23 1:00 ` [PATCH v2 59/59] KVM: SVM: Generate GA log IRQs only if the associated vCPUs is blocking Sean Christopherson
2025-06-04 17:11 ` [PATCH v2 00/59] KVM: iommu: Overhaul device posted IRQs support Paolo Bonzini
2025-06-09 12:20 ` Sairaj Kodilkar
2025-06-09 14:34 ` [PATCH " Sean Christopherson
2025-06-12 11:03 ` Sairaj Kodilkar
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=20250523010004.3240643-13-seanjc@google.com \
--to=seanjc@google.com \
--cc=baolu.lu@linux.intel.com \
--cc=dmatlack@google.com \
--cc=dwmw2@infradead.org \
--cc=francescolavra.fl@gmail.com \
--cc=iommu@lists.linux.dev \
--cc=joao.m.martins@oracle.com \
--cc=joro@8bytes.org \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mlevitsk@redhat.com \
--cc=pbonzini@redhat.com \
--cc=sarunkod@amd.com \
--cc=vasant.hegde@amd.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox