From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=85.214.250.239 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796118343; b=J/pJDalC7nlzLEr1mzLTpHpVjXXuWAPL08USm41ZOd3t135iDJ+qNwCvWX8uO9t1hE7R 7Ndd6kaqmF9+sS+1YyNosb/KKYA3NoosYJikN1iOv3QJjZOMfbm7xhMdBqmLkZwsDxOtA OZMD9DlLg65xWq1HZvAJLcYF1RN59furURM6zvQ0SrLCIzC+Om35UJ6b0pbFmozNlg6P/ K5mHXihpjcSsbskrmwo0Z89uQ7ikjtg8fdVSn7aMFf7IvOzrKUHehp8zpE2mtVkXtaXlf TQrCfI6gCuJu9KWQBsVFbP7Fr6O/PoWLbvT3Zp8yD9+c44ujCjho4E+fCdZ586iwdww== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796118343; h=DMARC-Filter:Received:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=6B1ktQZVIO3x7cKgfM/Aa/oSDudX8kZAWCTnmkhF+Gc=; b=eznBL8nIrgzneEQGaBj3fBHW7Dfz7ju+3jAjXdpGyah7GNy5QKhU/pDVSCAvJpujQSkh tTKep/LJq2n9rVca5JQcGkpggLKlp7CgTY1G/B/896/sm8NPh2ca8KWQGZcJ8+EHNsNy4 xbauJspwIOyG9nH1DLG5f61A+SixeVEk1bhaWKcXkdusl+ZVzNbVNvIMMj6zDj0uFi6aB cjNxDwFkoGZu2bxI9M7etjNzZcD8SFvikiA8vx0QoBbBJXvZB3jf2k2GHxGEqMUZ4+Uyt QbF+Q50KlqcioEFGV03+Iz+Dc8ByErxaRc5H6RZgrpeupJLvwKlM0dxzSSX3W6EnLpw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none header.from=8bytes.org; spf=pass smtp.mailfrom=8bytes.org; arc=none smtp.remote-ip=85.214.250.239 Received: from mail.8bytes.org (mail.8bytes.org [85.214.250.239]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 87886606A33E; Tue, 1 Dec 2026 09:45:40 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 87886606A33E Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=8bytes.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=8bytes.org Received: from io.home.8bytes.org (p549216fa.dip0.t-ipconnect.de [84.146.22.250]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mail.8bytes.org (Postfix) with ESMTPSA id 0304A24586C; Tue, 1 Dec 2026 10:45:34 +0100 (CET) From: =?UTF-8?q?J=C3=B6rg=20R=C3=B6del?= To: Paolo Bonzini , Sean Christopherson Cc: Michael Roth , Liam Merwick , Vishal Annapurve , Ninad Naik , Joerg Roedel , Tom Lendacky , James Bottomley , kvm@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, coconut-svsm@lists.linux.dev, joro@8bytes.org Subject: [PATCH v4 3/7] KVM: SVM: Implement GET_AP_APIC_IDS NAE event Date: Tue, 1 Dec 2026 10:45:21 +0100 Message-ID: <20261201094525.1240736-4-joro@8bytes.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261201094525.1240736-1-joro@8bytes.org> References: <20261201094525.1240736-1-joro@8bytes.org> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Joerg Roedel Implement the GHCB APIC ID List event so an SNP guest can discover its vCPUs without a broadcast INIT-SIPI sequence. Use the direct VMSA capability as the userspace opt-in, as Sean suggested, to keep guest feature discovery stable for existing SNP VMs across kernel updates. Gate both GHCB feature queries and event dispatch on the same condition. Check the entire supplied range for address overflow and shared writable backing and write the exact descriptor under the vCPU's SRCU read lock. For undersized requests, return the required page count without writing. Codex assisted with feature gating, buffer validation and build checks. Signed-off-by: Joerg Roedel --- arch/x86/include/asm/sev-common.h | 1 + arch/x86/include/uapi/asm/svm.h | 1 + arch/x86/kvm/svm/sev.c | 102 +++++++++++++++++++++++++++++- 3 files changed, 101 insertions(+), 3 deletions(-) diff --git a/arch/x86/include/asm/sev-common.h b/arch/x86/include/asm/sev-common.h index 01a6e4dbe423..c041f85dfd5c 100644 --- a/arch/x86/include/asm/sev-common.h +++ b/arch/x86/include/asm/sev-common.h @@ -136,6 +136,7 @@ enum psc_op { #define GHCB_HV_FT_SNP BIT_ULL(0) #define GHCB_HV_FT_SNP_AP_CREATION BIT_ULL(1) +#define GHCB_HV_FT_APIC_ID_LIST BIT_ULL(4) #define GHCB_HV_FT_SNP_MULTI_VMPL BIT_ULL(5) /* diff --git a/arch/x86/include/uapi/asm/svm.h b/arch/x86/include/uapi/asm/svm.h index e80676185092..c9c85891971a 100644 --- a/arch/x86/include/uapi/asm/svm.h +++ b/arch/x86/include/uapi/asm/svm.h @@ -118,6 +118,7 @@ #define SVM_VMGEXIT_AP_CREATE_ON_INIT 0 #define SVM_VMGEXIT_AP_CREATE 1 #define SVM_VMGEXIT_AP_DESTROY 2 +#define SVM_VMGEXIT_GET_APIC_IDS 0x80000017ull #define SVM_VMGEXIT_SNP_RUN_VMPL 0x80000018ull #define SVM_VMGEXIT_SAVIC 0x8000001aull #define SVM_VMGEXIT_SAVIC_REGISTER_GPA 0 diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 18ccab113989..f22490f86a4a 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -3544,7 +3544,8 @@ static bool sev_es_are_required_ghcb_fields_valid(struct vcpu_svm *svm) case SVM_VMGEXIT_AP_CREATION: return kvm_ghcb_rax_is_valid(svm) || lower_32_bits(control->exit_info_1) == SVM_VMGEXIT_AP_DESTROY; - break; + case SVM_VMGEXIT_GET_APIC_IDS: + return kvm_ghcb_rax_is_valid(svm); case SVM_VMGEXIT_MMIO_READ: case SVM_VMGEXIT_MMIO_WRITE: case SVM_VMGEXIT_PSC: @@ -4261,6 +4262,93 @@ static int sev_snp_ap_creation(struct vcpu_svm *svm) return 0; } +struct sev_apic_id_desc { + u32 num_entries; + u32 apic_ids[]; +}; + +static bool sev_snp_apic_id_list_enabled(struct kvm_vcpu *vcpu) +{ + return is_sev_snp_guest(vcpu) && + to_kvm_sev_info(vcpu->kvm)->snp_direct_vmsa; +} + +static u64 sev_ghcb_hv_features(struct kvm_vcpu *vcpu) +{ + u64 features = GHCB_HV_FT_SUPPORTED; + + if (sev_snp_apic_id_list_enabled(vcpu)) + features |= GHCB_HV_FT_APIC_ID_LIST; + + return features; +} + +static int sev_snp_get_apic_ids(struct vcpu_svm *svm) +{ + struct kvm_vcpu *vcpu = &svm->vcpu; + struct kvm *kvm = vcpu->kvm; + struct sev_apic_id_desc *desc; + unsigned int nr_vcpus, size; + unsigned int i; + gpa_t gpa, end_gpa; + gfn_t gfn; + u64 pages, len; + + nr_vcpus = atomic_read(&kvm->online_vcpus); + size = sizeof(*desc) + (nr_vcpus * sizeof(desc->apic_ids[0])); + + pages = vcpu->arch.regs[VCPU_REGS_RAX]; + if (pages < PFN_UP(size)) { + vcpu->arch.regs[VCPU_REGS_RAX] = PFN_UP(size); + return 1; + } + + gpa = svm->vmcb->control.exit_info_1; + if (!PAGE_ALIGNED(gpa) || + check_mul_overflow(pages, (u64)PAGE_SIZE, &len) || + check_add_overflow(gpa, len - 1, &end_gpa) || + !page_address_valid(vcpu, gpa) || + !page_address_valid(vcpu, end_gpa & PAGE_MASK)) + goto invalid_buffer; + + for (gfn = gpa_to_gfn(gpa); gfn <= gpa_to_gfn(end_gpa); gfn++) { + struct kvm_memory_slot *slot = gfn_to_memslot(kvm, gfn); + + if (kvm_is_error_hva(gfn_to_hva_memslot(slot, gfn)) || + kvm_is_private_gfn(kvm, gfn)) + goto invalid_buffer; + + if (signal_pending(current)) + return -EINTR; + cond_resched(); + } + + desc = kvzalloc(size, GFP_KERNEL_ACCOUNT); + if (!desc) + return -ENOMEM; + + desc->num_entries = nr_vcpus; + for (i = 0; i < nr_vcpus; i++) { + struct kvm_vcpu *listed_vcpu = kvm_get_vcpu(kvm, i); + + if (WARN_ON_ONCE(!listed_vcpu)) + goto invalid_buffer_free; + desc->apic_ids[i] = listed_vcpu->vcpu_id; + } + + if (kvm_write_guest(kvm, gpa, desc, size)) + goto invalid_buffer_free; + + kvfree(desc); + return 1; + +invalid_buffer_free: + kvfree(desc); +invalid_buffer: + svm_vmgexit_bad_input(svm, GHCB_ERR_INVALID_INPUT); + return 1; +} + static int snp_handle_guest_req(struct vcpu_svm *svm, gpa_t req_gpa, gpa_t resp_gpa) { struct sev_data_snp_guest_request data = {0}; @@ -4491,7 +4579,7 @@ static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) GHCB_MSR_INFO_POS); break; case GHCB_MSR_HV_FT_REQ: - set_ghcb_msr_bits(svm, GHCB_HV_FT_SUPPORTED, + set_ghcb_msr_bits(svm, sev_ghcb_hv_features(&svm->vcpu), GHCB_MSR_HV_FT_MASK, GHCB_MSR_HV_FT_POS); set_ghcb_msr_bits(svm, GHCB_MSR_HV_FT_RESP, GHCB_MSR_INFO_MASK, GHCB_MSR_INFO_POS); @@ -4631,6 +4719,12 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu) return 1; } + if (control->exit_code == SVM_VMGEXIT_GET_APIC_IDS && + !sev_snp_apic_id_list_enabled(vcpu)) { + svm_vmgexit_bad_input(svm, GHCB_ERR_INVALID_EVENT); + return 1; + } + if (!sev_es_are_required_ghcb_fields_valid(svm)) { /* * Print the exit code even though it may not be marked valid @@ -4713,7 +4807,7 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu) return 1; } case SVM_VMGEXIT_HV_FEATURES: - svm_vmgexit_success(svm, GHCB_HV_FT_SUPPORTED); + svm_vmgexit_success(svm, sev_ghcb_hv_features(vcpu)); return 1; case SVM_VMGEXIT_TERM_REQUEST: pr_info("SEV-ES guest requested termination: reason %#llx info %#llx\n", @@ -4736,6 +4830,8 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu) if (sev_snp_ap_creation(svm)) svm_vmgexit_bad_input(svm, GHCB_ERR_INVALID_INPUT); return 1; + case SVM_VMGEXIT_GET_APIC_IDS: + return sev_snp_get_apic_ids(svm); case SVM_VMGEXIT_GUEST_REQUEST: case SVM_VMGEXIT_EXT_GUEST_REQUEST: if (!PAGE_ALIGNED(control->exit_info_1) || -- 2.53.0