Received: by 2002:a05:7208:9594:b0:7e:5202:c8b4 with SMTP id gs20csp2231849rbb; Tue, 27 Feb 2024 15:30:37 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCX44CmSaF8yxt6KhARCj0NorPRUUC15nAn1KywRULPxeQW9zEYJC7G5XO/95mVEzKomH71LePJQgJRR+6EYyuWz/8Gxk2xY5s1kvKtVtg== X-Google-Smtp-Source: AGHT+IEBP3UMAu8ZQSlxCshN0oOS/dRuCjDdFG6RXOrq0+B73LCiamldSCALzyOUZ92LlqCwUD8i X-Received: by 2002:a05:6a00:4fd5:b0:6e5:109b:959 with SMTP id le21-20020a056a004fd500b006e5109b0959mr9465278pfb.25.1709076637682; Tue, 27 Feb 2024 15:30:37 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709076637; cv=pass; d=google.com; s=arc-20160816; b=ImA0qa8G4m1B7M4OxALaIJnW9C9KXzWorIa16v+/twiej9BcZnJWNGH3Bxc5ay+KXn KM/7b5iZ/IjizYEjYPiYNgzwp2fMWYc/vDGSOaPAYScW2K0iy9+kUcGBmGBpHwgzaF0Z toPgfOJDdAgdqH8gX5xjgDNgl3qN6d0tugmwYspInD2SdcPdiuelctDjtcFPx7HOCFl3 wFDCnAs72IPyweDrKSuHua7iQBpUpxgH+WNwK64M0+OZlOqEAb7gLh7gZVRIem9P8RRH IqcmmZezRB4C+6thFTEvuUJ7prfMpY9VUAGsI+tQ2cpJAEGwuTt71+ZwYi589QrSWYyA crxw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=wp0PKi7LIqDV/bOhI4X8sRzgu//akmwfPB7H3G2jAjw=; fh=miuXyzfw5j8qsOURKCmYrSiXirUlKyDkR37pPJ+5+/s=; b=fRXqd66fiXAxuXmGxdcno5LwAAH7ZYvJzyCU93gj2U/+S1VdFqO8F215KjqBKU9DwT ZIK1g6PQ8+8tQl556LQxNMp02KF8DVrdeoCtkXNYJHbFcZpXhGW9cacqmlYqpok33lU+ C5lcEoBws7fG4lIkpNGaNVv6exXpwcf47wsleClHBaJpnQRDOKhRIB0eUM7vfPCcHJDR sZ9r5znfsmEp/SF9ijnQ3ReMBWSwbbg0WOQtiQJRwygOJzOTjOqvcK8Rm8Whc3UG/Cl7 jWqhObz3rlavvE2N6srfSAqwOBdSFpzulIRlkEFDNDJbdfxISi4oZ286dTS6WFHPc/ZD cdxQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=gFpZvA3+; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-84210-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-84210-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id 28-20020a63155c000000b005cfd6ba16f7si6178672pgv.647.2024.02.27.15.30.37 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Feb 2024 15:30:37 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-84210-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=gFpZvA3+; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-84210-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-84210-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 558C028FCDA for ; Tue, 27 Feb 2024 23:28:52 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6BF7A5F856; Tue, 27 Feb 2024 23:21:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="gFpZvA3+" Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C30C45915D for ; Tue, 27 Feb 2024 23:21:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709076075; cv=none; b=lhynzc1P2YVxOiQtOO7oBzFmc1NswWBK92clDaPajJ9VUd2yg8yczmvYmKajr4s6LTHf/kNehq9Lc7Fg+h6RtLBZyh73FTHF/i4pjFWepYFVdctmKlUNq18PiNAwahaAbj6raXmQeDFXQoM3khbMiOfUN484GLlB3nNfw5KtkTQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709076075; c=relaxed/simple; bh=9CfWX8HdW2UX3ZUVgxAdyd0guyhqoDk/NzwOHyIQqR4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=d7fhvZqWc3T2Cb0/VtNKUyHjM9x4GciDeXu3TJBjiPs7wniXTqt2J3MEcY9WSwKiTUD6rb4qvZuEX+jmXyglmDlHnTLZA32jS10lndOg6wgadEVr1k4yYFRpBkkSTfWLjXGBMAm4iC5ZlMXCKM06P+7sJv6FDul+hCdUih0f9F8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=gFpZvA3+; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1709076071; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=wp0PKi7LIqDV/bOhI4X8sRzgu//akmwfPB7H3G2jAjw=; b=gFpZvA3+sVEhYHSNytivq2UbWmBTmSTvfB4jUqp2K/DDL7k2yEr9ZA6FxwciEeEemYwe/D 3b/J+WWtbgiwo3/GN1DutMc57AVPar9g/iV5e2ZWkRSsL88hxoA+GWvtTts0jyT/n3Ho5I 4cmsMDhkq3Vz7B4xU07FvmeWTHBEbNo= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-136-f86VfUR4MquqKV0uZzzIEw-1; Tue, 27 Feb 2024 18:21:06 -0500 X-MC-Unique: f86VfUR4MquqKV0uZzzIEw-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (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 mimecast-mx02.redhat.com (Postfix) with ESMTPS id C495C185A780; Tue, 27 Feb 2024 23:21:05 +0000 (UTC) Received: from virtlab511.virt.lab.eng.bos.redhat.com (virtlab511.virt.lab.eng.bos.redhat.com [10.19.152.198]) by smtp.corp.redhat.com (Postfix) with ESMTP id 97B2C492BCF; Tue, 27 Feb 2024 23:21:05 +0000 (UTC) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: seanjc@google.com, michael.roth@amd.com, isaku.yamahata@intel.com, thomas.lendacky@amd.com Subject: [PATCH 21/21] KVM: x86: Add gmem hook for determining max NPT mapping level Date: Tue, 27 Feb 2024 18:21:00 -0500 Message-Id: <20240227232100.478238-22-pbonzini@redhat.com> In-Reply-To: <20240227232100.478238-1-pbonzini@redhat.com> References: <20240227232100.478238-1-pbonzini@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.9 From: Michael Roth In the case of SEV-SNP, whether or not a 2MB page can be mapped via a 2MB mapping in the guest's nested page table depends on whether or not any subpages within the range have already been initialized as private in the RMP table. The existing mixed-attribute tracking in KVM is insufficient here, for instance: - gmem allocates 2MB page - guest issues PVALIDATE on 2MB page - guest later converts a subpage to shared - SNP host code issues PSMASH to split 2MB RMP mapping to 4K - KVM MMU splits NPT mapping to 4K At this point there are no mixed attributes, and KVM would normally allow for 2MB NPT mappings again, but this is actually not allowed because the RMP table mappings are 4K and cannot be promoted on the hypervisor side, so the NPT mappings must still be limited to 4K to match this. Add a hook to determine the max NPT mapping size in situations like this. Signed-off-by: Michael Roth Message-Id: <20231230172351.574091-31-michael.roth@amd.com> Signed-off-by: Paolo Bonzini --- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/mmu/mmu.c | 7 +++++++ 3 files changed, 9 insertions(+) diff --git a/arch/x86/include/asm/kvm-x86-ops.h b/arch/x86/include/asm/kvm-x86-ops.h index 42474acb7375..436e3c157fae 100644 --- a/arch/x86/include/asm/kvm-x86-ops.h +++ b/arch/x86/include/asm/kvm-x86-ops.h @@ -140,6 +140,7 @@ KVM_X86_OP(vcpu_deliver_sipi_vector) KVM_X86_OP_OPTIONAL_RET0(vcpu_get_apicv_inhibit_reasons); KVM_X86_OP_OPTIONAL(get_untagged_addr) KVM_X86_OP_OPTIONAL_RET0(gmem_prepare) +KVM_X86_OP_OPTIONAL_RET0(gmem_validate_fault) KVM_X86_OP_OPTIONAL(gmem_invalidate) #undef KVM_X86_OP diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index e523b204697d..259e6bb1e447 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1806,6 +1806,7 @@ struct kvm_x86_ops { gva_t (*get_untagged_addr)(struct kvm_vcpu *vcpu, gva_t gva, unsigned int flags); int (*gmem_prepare)(struct kvm *kvm, kvm_pfn_t pfn, gfn_t gfn, int max_order); void (*gmem_invalidate)(kvm_pfn_t start, kvm_pfn_t end); + int (*gmem_validate_fault)(struct kvm *kvm, kvm_pfn_t pfn, gfn_t gfn, u8 *max_level); }; struct kvm_x86_nested_ops { diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 6b4cb71668df..bcf12ac489f9 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -4339,6 +4339,13 @@ static int kvm_faultin_pfn_private(struct kvm_vcpu *vcpu, fault->max_level); fault->map_writable = !(fault->slot->flags & KVM_MEM_READONLY); + r = static_call(kvm_x86_gmem_validate_fault)(vcpu->kvm, fault->pfn, + fault->gfn, &fault->max_level); + if (r) { + kvm_release_pfn_clean(fault->pfn); + return r; + } + return RET_PF_CONTINUE; } -- 2.39.0