From patchwork Fri Aug 2 20:45:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13751979 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5E7726D1B9 for ; Fri, 2 Aug 2024 20:45:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722631518; cv=none; b=B39P95/pwhO0GqLFd8//hTb0EmQcuAN3O2xZWOURP96SQ8yBNWdtljEe3Yqp5rOab0B/f2SIatq4YXQh5QNQv1HsPxW1VlTI3OGB7Wei4XFm812BAfW4gDr9ZpSJ115qv6BtgC5/BZrSSuxAQ/u7zw+TwUGyRWHy4I1KOZldxXQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722631518; c=relaxed/simple; bh=lVGVJTk9jnqyafCwVBq3gYkwAdJxTfIQ8N912VK7nC4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=sYShchFrRN0jsvzJRXFUMccHNDmbhBYOSpMCk9HD+BLs1y/gEmqfuZpkkC49HJPCuClCH8AUC265KvpVngun1Dtbnqrv/7QKcepLjR28zaNXtgNrityxcNN8OQBDBHst0YIJ9VtPz+vbiM0Kd2ovmQ4/d1Z23aEwG7Uq9Xdb010= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=bieb6yJb; arc=none smtp.client-ip=209.85.219.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="bieb6yJb" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-e0bcd04741fso4707189276.2 for ; Fri, 02 Aug 2024 13:45:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722631516; x=1723236316; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=Igz2ZJyNNATN1XeEvBd17znAw6/Dz7d0KpIxkojeTYo=; b=bieb6yJboFbRZuMdvr6Jz5h7L1lBfHS3nGXl849mEGTeNeQX/kWdBxxSpjddTBiXSB +GxxiuCvNmHws9YTG6fHBoWHf5ULZH8TatpROqu5D5nC0WCT99I+gkXPUUzk7+qDqbAW u/OtGLFvp5VtzIxWxRxDerPcWu11TlRuNbNgale1awhdN5GMCVsjJXXFfaShj/c24Bf0 JD9Xy1psa5hrF3teiIwr86hWF068H4jsqCRoxTqxBlfL2j7gv5l65rpIh8HBFpWuO+So KEYlEqLHXS1xD0iit+yrUEB9gigeiUKSKHZqIj01sb0/KTRkng7GOMhc4OQH9p01SW2Z HmnA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722631516; x=1723236316; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=Igz2ZJyNNATN1XeEvBd17znAw6/Dz7d0KpIxkojeTYo=; b=Q0ji+3fdftEpB1hFApF3TpPt17VkN9DGJoMx5DtUomqkRynpgDGSZs7V/y22H0DQi5 ye3R0CXdUktVhjYEW5IGIa2aKQxruEnvwBZTeQD2gHnel7DgaVpy6VOaFFbbUwGa0KiE VJ3nxGtHlvOjSrNYwctTIQQcMprrKLMx43MB3+eAeZ7LVqTIohBulJwEKux+wa6CWfL9 BaabIh1pxB43cHnBLhHTTyeKxFQ4HA+t/1KY8pndMu3bKyhT6AckQJU7bC8uDYeC6fjj wL/M0gNvVVgZJQ0txdkmXMgaJZZhrRBxkM81oMHmax+GrpfHFTr7iV6fJCqWj4qE3nIv 8dNw== X-Gm-Message-State: AOJu0YwJFVO/kjcU8nuM9cr12QO76sXAd0QqLI1clj8OY+kFAyK9Z+ky +lCC0DVs6W70DMBCHnt5PgEEnROIkwL4PrLLeHVSfHMVker0YEHzJySCeBuilWE0PapuV9Gfsbs g6g== X-Google-Smtp-Source: AGHT+IGAc96EWJLQ/4sJpltgqTG6OOrlRceDtu62MpXUFhsp63bSYtrY6Grjcmv8UziSYaCDnsvGdEWEGCI= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:1610:b0:e0b:6ba4:7d60 with SMTP id 3f1490d57ef6-e0bde21c34dmr206063276.4.1722631516483; Fri, 02 Aug 2024 13:45:16 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 2 Aug 2024 13:45:09 -0700 In-Reply-To: <20240802204511.352017-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240802204511.352017-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc2.264.g509ed76dc8-goog Message-ID: <20240802204511.352017-2-seanjc@google.com> Subject: [PATCH 1/3] KVM: SVM: Add a helper to convert a SME-aware PA back to a struct page From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Add __sme_pa_to_page() to pair with __sme_page_pa() and use it to replace open coded equivalents, including for "iopm_base", which previously avoided having to do __sme_clr() by storing the raw PA in the global variable. Opportunistically convert __sme_page_pa() to a helper to provide type safety. No functional change intended. Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/svm.c | 9 ++++----- arch/x86/kvm/svm/svm.h | 16 +++++++++++++++- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index c115d26844f7..2c44261fda70 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -1124,8 +1124,7 @@ static void svm_hardware_unsetup(void) for_each_possible_cpu(cpu) svm_cpu_uninit(cpu); - __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), - get_order(IOPM_SIZE)); + __free_pages(__sme_pa_to_page(iopm_base), get_order(IOPM_SIZE)); iopm_base = 0; } @@ -1301,7 +1300,7 @@ static void init_vmcb(struct kvm_vcpu *vcpu) if (!kvm_hlt_in_guest(vcpu->kvm)) svm_set_intercept(svm, INTERCEPT_HLT); - control->iopm_base_pa = __sme_set(iopm_base); + control->iopm_base_pa = iopm_base; control->msrpm_base_pa = __sme_set(__pa(svm->msrpm)); control->int_ctl = V_INTR_MASKING_MASK; @@ -1503,7 +1502,7 @@ static void svm_vcpu_free(struct kvm_vcpu *vcpu) sev_free_vcpu(vcpu); - __free_page(pfn_to_page(__sme_clr(svm->vmcb01.pa) >> PAGE_SHIFT)); + __free_page(__sme_pa_to_page(svm->vmcb01.pa)); __free_pages(virt_to_page(svm->msrpm), get_order(MSRPM_SIZE)); } @@ -5250,7 +5249,7 @@ static __init int svm_hardware_setup(void) iopm_va = page_address(iopm_pages); memset(iopm_va, 0xff, PAGE_SIZE * (1 << order)); - iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT; + iopm_base = __sme_page_pa(iopm_pages); init_msrpm_offsets(); diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 76107c7d0595..2b095acdb97f 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -25,7 +25,21 @@ #include "cpuid.h" #include "kvm_cache_regs.h" -#define __sme_page_pa(x) __sme_set(page_to_pfn(x) << PAGE_SHIFT) +/* + * Helpers to convert to/from physical addresses for pages whose address is + * consumed directly by hardware. Even though it's a physical address, SVM + * often restricts the address to the natural width, hence 'unsigned long' + * instead of 'hpa_t'. + */ +static inline unsigned long __sme_page_pa(struct page *page) +{ + return __sme_set(page_to_pfn(page) << PAGE_SHIFT); +} + +static inline struct page *__sme_pa_to_page(unsigned long pa) +{ + return pfn_to_page(__sme_clr(pa) >> PAGE_SHIFT); +} #define IOPM_SIZE PAGE_SIZE * 3 #define MSRPM_SIZE PAGE_SIZE * 2 From patchwork Fri Aug 2 20:45:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13751980 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1287078C88 for ; Fri, 2 Aug 2024 20:45:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722631520; cv=none; b=lt3UZpiGY3DKIIvfkOKpB/dCCiMvBPvLV6m1yCapcrSrPh6aLSAN8dchSFMTI6fgpJnhn3Q+KiD6RvYU2R3f2gM43MK2LgsyTrNxWD3c8WSc4PSKNrcWibZM3uL4m20j5hWygYhAhLAR2p2EA22Yw/jTNH81QxPuyRVHCmJkO3w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722631520; c=relaxed/simple; bh=cEmBxHf9lxfhKNujzAja0PlMob6xSKDSsBphNmFY1I8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=VW6nvnocus4Nz/vD8JKtrLW5hoc1inXKUyH8fPSgprvwKSPS04bdicb79BCeNVInmhxtpfTYEXir0PxThtOZzGQZvTCzeqEQ0GH7hsk6KgdONOSgi6lLJsqcGZMThrfZyOxGGS7n3LauPRjZNmQBP5A4lM2UBY/VQvgQHuulxAg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=vTmrSSg2; arc=none smtp.client-ip=209.85.214.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="vTmrSSg2" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-1fc52d8bf24so39515725ad.1 for ; Fri, 02 Aug 2024 13:45:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722631518; x=1723236318; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=Ef/zFHnzH9ziD4r800QDczQlgiF3jMvJYDOovUok0uI=; b=vTmrSSg25bm4QZoascZIVHbSyDjR61ae2uHC0PRVm6F2TkQR8jnPLOS6KPclk0tNJP NLKcizkkCgFVyTtxzUTlbbUU/pT0ZDJmN7EWe+GcZLr2m+JiGf11MEzw6bd5I07RBXD0 Bd0+ENeWfucRvzF90Dk5inUm9mxz5Lta39y0atd3UfndejQOS164rwUKW38kqTLb2YVj K6vK34xrdNzMkukJiL5W1IvSuAWjYsLwbbzE4lyK+gQcIoGE4R3ogHKhx1KDtI1CSHi5 gGXar3pYueGE44onnV/yhtyW3WfKOcgDZJ+bNmtFHtcFWghcUpqB5VAf/I8wpnuT/Jvr ZvZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722631518; x=1723236318; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=Ef/zFHnzH9ziD4r800QDczQlgiF3jMvJYDOovUok0uI=; b=M+UM15iMpMPlyIzhub8gP8AfOQkBsYCmNDiXIAHJDmm6fpOuyvtaaToTF/GN/9Zy22 t3MqZYlXUUbgrzSg4sXD3KEZbslZ0o167Jt0YGyGFMzUm00cHvhvDOLW0fQgrS8ZW0i5 7wFmLcr1dmKBvzzuEtr4ewqFTeSzlHCErXSpNagfr2rliXHjFju1R0vI3E83Xr5q7+wu q03jaooiHF6tQVCTZSiWTNwkAQBuGedZ5lVaVS6QqhbG8dcoM2+SUpSpp6gil41dS58I efSY/1UQAsD57LZyfn5UZIkKR6zQhYB/pGV/UU/Vd1Uw/yK1On3thfuSuX+LvyhqdLXh 803g== X-Gm-Message-State: AOJu0YzgKbJ8WzMbD8jErrVQ32FZUxkRAy+i2et2DEP9aOi6KSyOIriq RLCOnVxg0pBpYQZU0HDxh+c825N56fn5gNYSHMYaVa5ANoD8HBBY/1l0cgEDEY5gc/G1phZwA2e Ueg== X-Google-Smtp-Source: AGHT+IGUaCH5GW1GuzLfD6kYR2Ssu+ja9saSMgach2eDhR8Bt+zw4+jG2/PGa+s1mkXBM9gh1y7A6UyHYEU= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:dac5:b0:1f7:516:4235 with SMTP id d9443c01a7336-1ff57bed391mr2256355ad.6.1722631518251; Fri, 02 Aug 2024 13:45:18 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 2 Aug 2024 13:45:10 -0700 In-Reply-To: <20240802204511.352017-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240802204511.352017-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc2.264.g509ed76dc8-goog Message-ID: <20240802204511.352017-3-seanjc@google.com> Subject: [PATCH 2/3] KVM: SVM: Add host SEV-ES save area structure into VMCB via a union From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Incorporate the _host_ SEV-ES save area into the VMCB as a union with the legacy save area. The SEV-ES variant used to save/load host state is larger than the legacy save area, but resides at the same offset. Prefix the field with "host" to make it as obvious as possible that the SEV-ES variant in the VMCB is only ever used for host state. Guest state for SEV-ES VMs is stored in a completely separate page (VMSA), albeit with the same layout as the host state. Add a compile-time assert to ensure the VMCB layout is correct, i.e. that KVM's layout matches the architectural definitions. No functional change intended. Signed-off-by: Sean Christopherson --- arch/x86/include/asm/svm.h | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/arch/x86/include/asm/svm.h b/arch/x86/include/asm/svm.h index f0dea3750ca9..2b59b9951c90 100644 --- a/arch/x86/include/asm/svm.h +++ b/arch/x86/include/asm/svm.h @@ -516,6 +516,20 @@ struct ghcb { u32 ghcb_usage; } __packed; +struct vmcb { + struct vmcb_control_area control; + union { + struct vmcb_save_area save; + + /* + * For SEV-ES VMs, the save area in the VMCB is used only to + * save/load host state. Guest state resides in a separate + * page, the aptly named VM Save Area (VMSA), that is encrypted + * with the guest's private key. + */ + struct sev_es_save_area host_sev_es_save; + }; +} __packed; #define EXPECTED_VMCB_SAVE_AREA_SIZE 744 #define EXPECTED_GHCB_SAVE_AREA_SIZE 1032 @@ -532,6 +546,7 @@ static inline void __unused_size_checks(void) BUILD_BUG_ON(sizeof(struct ghcb_save_area) != EXPECTED_GHCB_SAVE_AREA_SIZE); BUILD_BUG_ON(sizeof(struct sev_es_save_area) != EXPECTED_SEV_ES_SAVE_AREA_SIZE); BUILD_BUG_ON(sizeof(struct vmcb_control_area) != EXPECTED_VMCB_CONTROL_AREA_SIZE); + BUILD_BUG_ON(offsetof(struct vmcb, save) != EXPECTED_VMCB_CONTROL_AREA_SIZE); BUILD_BUG_ON(sizeof(struct ghcb) != EXPECTED_GHCB_SIZE); /* Check offsets of reserved fields */ @@ -568,11 +583,6 @@ static inline void __unused_size_checks(void) BUILD_BUG_RESERVED_OFFSET(ghcb, 0xff0); } -struct vmcb { - struct vmcb_control_area control; - struct vmcb_save_area save; -} __packed; - #define SVM_CPUID_FUNC 0x8000000a #define SVM_SELECTOR_S_SHIFT 4 From patchwork Fri Aug 2 20:45:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13751981 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0A73A1311B4 for ; Fri, 2 Aug 2024 20:45:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722631522; cv=none; b=kE1YdxRo0bPcNVnvRakWt0v4u36fMaZ0ars/xuPlwj/MquVhDWn+QbvYDFMJ38pKeBNz4r08SI/K7K/l9s/3/2CWV2qce98c2Tnyt1XXrMXQiX7nKjSfiIUPCfNUnZFEbP0EqFO4EZrdJN1GtF/jMcP3baper7amxl+RSzp3Ens= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722631522; c=relaxed/simple; bh=XiBb3/2+Y11JQa3ISIlTyC0BrcKFHgasOhtRB+Hckb4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=WLBLnuliJZQzRHh0dLFgk3ojqOw6/LU2MP9IvxoCREKIHJtLLAR0vLfD+gfa8IANPAE6KZ10c2PhRVk6ewPVH6QlzDt/zIH+d8NvxP+10fL1Td0FjIDwCxvd+hd30ptp9v8Fso4w33x9HVV0adkgD/74nHivo9lpgA8hnwJyyrQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=uecvvOGg; arc=none smtp.client-ip=209.85.219.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="uecvvOGg" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e02a4de4f4eso13637049276.1 for ; Fri, 02 Aug 2024 13:45:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722631520; x=1723236320; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=YAVVvosBGaQooisla/uYDPtaWdSHgHnkf46AZuaMyGQ=; b=uecvvOGgVyYCt+c/MkRI5y4Vk0wETSAq/8N+Gma+yXBslla+RIfzm7exAG/w4s6bA6 HGawnO3mM86LTmDFHieQX10Jlz7RHKhrxYtiYXuS/SZlRVmXYKhjN35PEIPYZyC2QQZi tbKRr3dEUJFB60pAAHFid/tVwORz7k+4FfxOLPgD3KdjsvjKz3Kl431b5TUIGNsc8QHC kPDxv0FjCtjwZ9SpiJEMcfw5lO/PAKgz/SVXXyXJZGbi/KvKZ5Hw+JGicKyo37Ym4Oe9 3qyfLeQwhpjqGDHyO+0aWRita+Xr4O7IZv9yRcSaWwCiVA4mnupuNv6BbSwToAx1V4ax R/hw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722631520; x=1723236320; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=YAVVvosBGaQooisla/uYDPtaWdSHgHnkf46AZuaMyGQ=; b=fOOHAKtUSAr3ZaXruqZxx7jCVPEHU9MFtFVHvFGZEKCJ6xtbKRN7XTApqkXDffessK vHElMn4wPvQZDDB+Bi9OV/TOxldDfAsUw5kiK2Bf0ozUbQtbVuuoF9x1B8Dh3f2HUmQi E3eRcdsE6qc5p7hP/cFxUXJgnZCs7XXpI/rPu2VTAYsf5Yx7aWFuDiLjDibYuxk3mP+y pwAkmBQBhsKMiZSB8X0/TrG9LTz+XgwRSuh59/twuk5smOElPVjtn1Gc65tKVq+pIbvw uBLlLgFDXQ1+Ok8hTro8u+1OIB2/tZuI3uu5nBCyGDZ7HIf+lGuv8e8+Bi2pwci52x7Z 7YOQ== X-Gm-Message-State: AOJu0Yyn2secpJ8I++FbpzqBi415nGZ7iuNUJIdryMq08HQzeEiqvApj WrDSzJLl1m/Qh4OdPUnTb72GlaCsAxRSlSNh46hQr2X/nd+LQMGRxhTyXWeFPQWy5mSw2pKWopf +aA== X-Google-Smtp-Source: AGHT+IFPDPOfed4bfrEZNnwZ17lL7+A2nCaNcU3DAzSYWZX3ae0+7bgADBtnO+nLvjjjI443/H/XthQlK0Q= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:18c3:b0:e0b:4559:e6e8 with SMTP id 3f1490d57ef6-e0bde3f022dmr7643276.7.1722631519965; Fri, 02 Aug 2024 13:45:19 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 2 Aug 2024 13:45:11 -0700 In-Reply-To: <20240802204511.352017-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240802204511.352017-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc2.264.g509ed76dc8-goog Message-ID: <20240802204511.352017-4-seanjc@google.com> Subject: [PATCH 3/3] KVM: SVM: Track the per-CPU host save area as a VMCB pointer From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org The host save area is a VMCB, track it as such to help readers follow along, but mostly to cleanup/simplify the retrieval of the SEV-ES host save area. Note, the compile-time assertion that offsetof(struct vmcb, save) == EXPECTED_VMCB_CONTROL_AREA_SIZE ensures that the SEV-ES save area is indeed at offset 0x400 (whoever added the expected/architectural VMCB offsets apparently likes decimal). No functional change intended. Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/svm.c | 15 ++++++++------- arch/x86/kvm/svm/svm.h | 2 +- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 2c44261fda70..43156ac93fb3 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -573,7 +573,7 @@ static void __svm_write_tsc_multiplier(u64 multiplier) static __always_inline struct sev_es_save_area *sev_es_host_save_area(struct svm_cpu_data *sd) { - return page_address(sd->save_area) + 0x400; + return &sd->save_area->host_sev_es_save; } static inline void kvm_cpu_svm_disable(void) @@ -696,7 +696,7 @@ static void svm_cpu_uninit(int cpu) return; kfree(sd->sev_vmcbs); - __free_page(sd->save_area); + __free_page(__sme_pa_to_page(sd->save_area_pa)); sd->save_area_pa = 0; sd->save_area = NULL; } @@ -704,23 +704,24 @@ static void svm_cpu_uninit(int cpu) static int svm_cpu_init(int cpu) { struct svm_cpu_data *sd = per_cpu_ptr(&svm_data, cpu); + struct page *save_area_page; int ret = -ENOMEM; memset(sd, 0, sizeof(struct svm_cpu_data)); - sd->save_area = snp_safe_alloc_page_node(cpu_to_node(cpu), GFP_KERNEL); - if (!sd->save_area) + save_area_page = snp_safe_alloc_page_node(cpu_to_node(cpu), GFP_KERNEL); + if (!save_area_page) return ret; ret = sev_cpu_init(sd); if (ret) goto free_save_area; - sd->save_area_pa = __sme_page_pa(sd->save_area); + sd->save_area = page_address(save_area_page); + sd->save_area_pa = __sme_page_pa(save_area_page); return 0; free_save_area: - __free_page(sd->save_area); - sd->save_area = NULL; + __free_page(save_area_page); return ret; } diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 2b095acdb97f..43fa6a16eb19 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -335,7 +335,7 @@ struct svm_cpu_data { u32 next_asid; u32 min_asid; - struct page *save_area; + struct vmcb *save_area; unsigned long save_area_pa; struct vmcb *current_vmcb;