Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp75632pxj; Wed, 23 Jun 2021 16:07:38 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxxIWhqRVjHi4DNCbwWMutNb+oTjB7kAEd5zhSPlSFYcISDx+H9CU5ZmAgVR+Vv5g8j/Iyn X-Received: by 2002:a05:6402:1581:: with SMTP id c1mr2824504edv.213.1624489657850; Wed, 23 Jun 2021 16:07:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1624489657; cv=none; d=google.com; s=arc-20160816; b=0pLkDN+iVkStzAb6OC4uvsCJUUqu7RsLQaEwuXLTa1NxdMp53lgkiCJHyREMe2rFWU yKryLCdFOJYkhqq8p50M2I6oR8iAvwA4iioser5p/Kem6bs1PZMY71rh5gnQ5NSoKhGr nE0xl0jRAr4VlGgLbRHPGTxYJsuUIY+DeM1+Ot+Y76CcNZC8CqQCnDjUsV3WyNbniIiw 2l5sWgzPx90dVvFaITLVMzT4rDY7UlqiwLc0xharxqHqk/ya+bmz4e0r3+2xQRXEzCKc D9PbNZH5HgCvmHwFTdRyH3Q1is4eYZh21wj4jFgkInUDm8MhtRvhTlxnZVTKQcA5+bXk QERw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:reply-to:dkim-signature; bh=jbfGWs931zA+JGWuuEGa9C1XdcBcxFB7u/E81/FmgGU=; b=qLQS3qQgS/Ogywa3ksHHqOPOfZXbRG7YItnHpjBfqxo2nouN+C27zZk4lEfkRuCuYc d5Jk/7x2q1K7fEMPQa3R+nzblqdzGvtJyUlyV2CxbHcjIZ/EaP18Vj5vRmtz1hsOklQ3 aqPLJidtbRdMxJLIrUjSQ93oDQYJdTfTJD2hJwDvDnZSSRrhjGqX9GPCG8cACMBBIroa dnLa6ze5DhvdwodhxOiMj/TYeGHlPfUu6IVqIbze9vRJsZC87Kx4yTAYYC4BRs2ZveMD zo/7sKHilp3oiHNjzz5nrpj0V6XH98a0aG/jWYqqDbkzA3rr5HfXPUasHU76DSXj/lLb PRdA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=q4witubl; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id bz25si1061499ejc.193.2021.06.23.16.07.15; Wed, 23 Jun 2021 16:07:37 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=q4witubl; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229949AbhFWXIZ (ORCPT + 99 others); Wed, 23 Jun 2021 19:08:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34678 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229889AbhFWXIV (ORCPT ); Wed, 23 Jun 2021 19:08:21 -0400 Received: from mail-qt1-x84a.google.com (mail-qt1-x84a.google.com [IPv6:2607:f8b0:4864:20::84a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A1D5DC061756 for ; Wed, 23 Jun 2021 16:06:03 -0700 (PDT) Received: by mail-qt1-x84a.google.com with SMTP id h15-20020ac846cf0000b029024f584a591eso2048200qto.15 for ; Wed, 23 Jun 2021 16:06:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=reply-to:date:in-reply-to:message-id:mime-version:references :subject:from:to:cc; bh=jbfGWs931zA+JGWuuEGa9C1XdcBcxFB7u/E81/FmgGU=; b=q4witublxlQfQh9byhgfZ2JZsN+6vdXlBZi0aMSrSAjmRmYvx9rFDvbS8yFOPcM4LB /1NxO5D6/eZNco8/BabNsUhn0brbIh92TLER1gzpd13/O2Zy76WZxJCtulEKzpBuvW8I 988XdjGpKD6Bcr9HncDuGdCXh2LXROdtzD4VOqCu/2y55QLPQE7xBUTZweL+nFzXB24g pq1yIkl8TKm2Ia2OD8LDhSuqUA7TXbKcCoFPsTwJNu7HxvV/RRB8Ow0uEVbf5L8Hy2UQ IUMWRxhgRXtwlrhbbhWzCi+yckSL81UJn5vCW0JR7fdCZtUtz5KZSghBUQ4rW47zwXQm s2xg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:reply-to:date:in-reply-to:message-id :mime-version:references:subject:from:to:cc; bh=jbfGWs931zA+JGWuuEGa9C1XdcBcxFB7u/E81/FmgGU=; b=qrYEqBhCnaytqwClo2l235fCdjjgrxv50UXFpddpG0ni36UpcW/Kvgq8AmKgqgbfvQ AJBdvC7xV3vId52w1hXgbVjB1yLaicQje2HAQisaBcEfmNgPKt/xvzkkfkVpNu3fdxBd qCbuWeIFzlO3/eVCqpUtvTCM+8L+bgWt6Vwk1HJI/2whCeQRZr+Ki0F2IYeAEg7DPbTQ KNgvCydeIV1HYs18eezB/vvMKegZFwl99FcUk6kjyNSMHnwWXAmmKgKzvBsG8lmMDZoK xE5zlKt9edQX53+YAAO5xc2RBk6AbzC3cB6HYiGtzaBqspsi9AQfj20j6qc0853MJXYC kBVg== X-Gm-Message-State: AOAM533MWp0SuGE5bbu58NQAnD1Bro0RtZniTDfcPq9lAZZj/kCe7TaL bx8XXLg5iPR9J2GJnrNj/zlRSkK3p6Y= X-Received: from seanjc798194.pdx.corp.google.com ([2620:15c:f:10:e9e:5b86:b4f2:e3c9]) (user=seanjc job=sendgmr) by 2002:ad4:5a07:: with SMTP id ei7mr2045985qvb.46.1624489562735; Wed, 23 Jun 2021 16:06:02 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 23 Jun 2021 16:05:47 -0700 In-Reply-To: <20210623230552.4027702-1-seanjc@google.com> Message-Id: <20210623230552.4027702-3-seanjc@google.com> Mime-Version: 1.0 References: <20210623230552.4027702-1-seanjc@google.com> X-Mailer: git-send-email 2.32.0.288.g62a8d224e6-goog Subject: [PATCH 2/7] KVM: x86: Use kernel's x86_phys_bits to handle reduced MAXPHYADDR From: Sean Christopherson To: Paolo Bonzini Cc: Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Peter Gonda , Brijesh Singh , Tom Lendacky Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Use boot_cpu_data.x86_phys_bits instead of the raw CPUID information to enumerate the MAXPHYADDR for KVM guests when TDP is disabled (the guest version is only relevant to NPT/TDP). When using shadow paging, any reductions to the host's MAXPHYADDR apply to KVM and its guests as well, i.e. using the raw CPUID info will cause KVM to misreport the number of PA bits available to the guest. Unconditionally zero out the "Physical Address bit reduction" entry. For !TDP, the adjustment is already done, and for TDP enumerating the host's reduction is wrong as the reduction does not apply to GPAs. Fixes: 9af9b94068fb ("x86/cpu/AMD: Handle SME reduction in physical address size") Cc: stable@vger.kernel.org Signed-off-by: Sean Christopherson --- arch/x86/kvm/cpuid.c | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c index 4b2f8c6b41e8..28878671d648 100644 --- a/arch/x86/kvm/cpuid.c +++ b/arch/x86/kvm/cpuid.c @@ -941,11 +941,18 @@ static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function) unsigned phys_as = entry->eax & 0xff; /* - * Use bare metal's MAXPHADDR if the CPU doesn't report guest - * MAXPHYADDR separately, or if TDP (NPT) is disabled, as the - * guest version "applies only to guests using nested paging". + * If TDP (NPT) is disabled use the adjusted host MAXPHYADDR as + * the guest operates in the same PA space as the host, i.e. + * reductions in MAXPHYADDR for memory encryption affect shadow + * paging, too. + * + * If TDP is enabled but an explicit guest MAXPHYADDR is not + * provided, use the raw bare metal MAXPHYADDR as reductions to + * the HPAs do not affect GPAs. */ - if (!g_phys_as || !tdp_enabled) + if (!tdp_enabled) + g_phys_as = boot_cpu_data.x86_phys_bits; + else if (!g_phys_as) g_phys_as = phys_as; entry->eax = g_phys_as | (virt_as << 8); @@ -970,12 +977,18 @@ static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function) case 0x8000001a: case 0x8000001e: break; - /* Support memory encryption cpuid if host supports it */ case 0x8000001F: - if (!kvm_cpu_cap_has(X86_FEATURE_SEV)) + if (!kvm_cpu_cap_has(X86_FEATURE_SEV)) { entry->eax = entry->ebx = entry->ecx = entry->edx = 0; - else + } else { cpuid_entry_override(entry, CPUID_8000_001F_EAX); + + /* + * Enumerate '0' for "PA bits reduction", the adjusted + * MAXPHYADDR is enumerated directly (see 0x80000008). + */ + entry->ebx &= ~GENMASK(11, 6); + } break; /*Add support for Centaur's CPUID instruction*/ case 0xC0000000: -- 2.32.0.288.g62a8d224e6-goog