Received: by 2002:ab2:7a55:0:b0:1f4:4a7d:290d with SMTP id u21csp245980lqp; Thu, 4 Apr 2024 11:53:55 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXKCK3AGcl+KOzoHntmP1JbV8oCmnSbgXi6G8ceUSC5aYG/RIZmbWzkLmFaizB3vB1x3hPPx+X8Qk5qlFgHfS7l1r7fBp5ZSiPQn4zMCg== X-Google-Smtp-Source: AGHT+IERhVkpv0iwCN+JG4LNjCOJj1XaxFPXm15D9KPkvXGY8Abl9OSX8/qRjhCmRRrZm2qic85K X-Received: by 2002:a05:6512:60b:b0:513:bf23:ce3 with SMTP id b11-20020a056512060b00b00513bf230ce3mr2168947lfe.62.1712256835616; Thu, 04 Apr 2024 11:53:55 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712256835; cv=pass; d=google.com; s=arc-20160816; b=jEavC88bHXwXkieOnD/MF74L+5BnViizmBnEP9WRIg+ATiEG7WLjY8E4BAMhMuSD/8 1EUPKvvqrbLPq+6k3asNDAQIelC6wufxuHIdSKG7438m1ZeMF5ghyda9yZZOpGxp8GMZ bqcpwfVql5jFa0xfY4LDTaFh4vQtbhQ+72ZYqn7KxY1swmI2QwAfPjBs53B+5fEMKZX/ 2xsa356Y8U0ex3DgkO62Ty7FzrVddD768bFQigf9ebUEPD57ZMojqAnQsDoAM/b8Kdoh qnfknUhZTZsdnXOs8B9cqvqqAAiGhEGane+ImFdQURjfb8OfeO6kdxiC8eDhOOtP0iJB KqSw== 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=Nc3SKOOYPHg3uIsI8oCYk9QWrQ5YHEK98RfXdPCT6Bs=; fh=YodCu8Vte8qk6mmfvotnw4P3XuROgs3fniKbwtpv3JY=; b=bjg5dvRwaGxtYYWaDyHq2snL4lJ4Ljpj4w2ZS0adoh4wobm89OBKnDqsrkGjrNmQue G432xba6MSDwAoJ2U/rkAMlIfuiGCLZMIQHf3KNm50Qd9fNhhszdLPFmfWV+oz9Hxqmg XaLpcoTNIrpnAb1dPdgxK2Uz7caNFycnxYReanmAqsstVOHReyDl0FZlWHhfI4tOL/VJ ZAbjZ8OgJfw3EIHAAKLrduU2Z+YzvKY2axh1TQmYpe2BZFb11F0afFByDJIFUdBHDVC6 aC8bSLRgH9ajcdbcOYOBGNBuPoUlG9h9SqVbXybu0Ue2guRTZq7gvbZwy0oEUBwzkBP9 wICg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=IRaR7EQv; 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-131994-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-131994-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id q7-20020a50cc87000000b0056c0520541asi8027976edi.466.2024.04.04.11.53.55 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Apr 2024 11:53:55 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-131994-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=IRaR7EQv; 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-131994-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-131994-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 am.mirrors.kernel.org (Postfix) with ESMTPS id 56AE21F2C050 for ; Thu, 4 Apr 2024 18:53:18 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id CDF23131759; Thu, 4 Apr 2024 18:50:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="IRaR7EQv" 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 B9B5912D201 for ; Thu, 4 Apr 2024 18:50:42 +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=1712256645; cv=none; b=VbIomPR1PSGrbQteO/TnAljdYwiyoyJzN4VvGs/LIxDNbF0bQM9OqmLOMbLAcL5ciR5ui/CrDAj1tT47cMG1VR6EJYvd6XtsvL1woM2j2OEsTNEdF8fVCYjaz5C+mJ/KIYlTFZ44CX5s2qK+SEMUmjwIzJBSFZUThj9x2qsooFg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712256645; c=relaxed/simple; bh=JhCLT6R3dEV4Z8kPXKqhhzXcGFUKttw3pZHa+JT0OpU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=MUmaj49pPbHx5VoQvJKL+pQ58WQRXgZScx0Zin7aj0aMZ05HDA+2XmFDAu9MoK+bWwIwd+qTmts7qZACCb5ZBrWqjKsTmDyimc6Gi7BkW4nAu67x7qC3TtmUHdE2/vcShTlrfrvk7kXcQPdMbmzvFxcCh+SRhg+q/u0+iDHA5Ew= 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=IRaR7EQv; 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=1712256641; 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=Nc3SKOOYPHg3uIsI8oCYk9QWrQ5YHEK98RfXdPCT6Bs=; b=IRaR7EQvNyoSCQhx4m7+QqrfNg9+p0xFlhIylouUEjbtPbyeKpEwKA3/Uuib29TPp107Jq Hw4kUCKrpOSv0VuuntM02coESBssYfFxSIP4eDG4yi/DE0z7S1EAqxKZFgnF7QAcEEBPaj UcN2iWZXgUccmzGLqCgUhNZqEcROBF8= 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-58-9DwWGaL3OZ2V6bZuCBaOTQ-1; Thu, 04 Apr 2024 14:50:37 -0400 X-MC-Unique: 9DwWGaL3OZ2V6bZuCBaOTQ-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (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 157CE85A5B7; Thu, 4 Apr 2024 18:50:37 +0000 (UTC) Received: from virtlab701.virt.lab.eng.bos.redhat.com (virtlab701.virt.lab.eng.bos.redhat.com [10.19.152.228]) by smtp.corp.redhat.com (Postfix) with ESMTP id E2B8D2024517; Thu, 4 Apr 2024 18:50:36 +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 Subject: [PATCH 11/11] KVM: x86: Add gmem hook for determining max NPT mapping level Date: Thu, 4 Apr 2024 14:50:33 -0400 Message-ID: <20240404185034.3184582-12-pbonzini@redhat.com> In-Reply-To: <20240404185034.3184582-1-pbonzini@redhat.com> References: <20240404185034.3184582-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.4 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 | 2 ++ arch/x86/kvm/mmu/mmu.c | 8 ++++++++ 3 files changed, 11 insertions(+) diff --git a/arch/x86/include/asm/kvm-x86-ops.h b/arch/x86/include/asm/kvm-x86-ops.h index c81990937ab4..2db87a6fd52a 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_OPTIONAL_RET0(vcpu_get_apicv_inhibit_reasons); KVM_X86_OP_OPTIONAL(get_untagged_addr) KVM_X86_OP_OPTIONAL(alloc_apic_backing_page) 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 59c7b95034fc..67dc108dd366 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1811,6 +1811,8 @@ struct kvm_x86_ops { void *(*alloc_apic_backing_page)(struct kvm_vcpu *vcpu); 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, bool is_private, + 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 992e651540e8..13dd367b8af1 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -4338,6 +4338,14 @@ 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->is_private, + &fault->max_level); + if (r) { + kvm_release_pfn_clean(fault->pfn); + return r; + } + return RET_PF_CONTINUE; } -- 2.43.0