Received: by 2002:ab2:6203:0:b0:1f5:f2ab:c469 with SMTP id o3csp495306lqt; Fri, 19 Apr 2024 02:00:00 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUVbpJOaiFrZP8smXELWnJhber21CT/eMplot8/H0UVA1OfsF8kBog492E4WQCujDKEWm1JShWLd1ujhnDW+VtRK25QGEnxSRD9xuoncg== X-Google-Smtp-Source: AGHT+IHR21NQNUCSj/VcK5w0oqUuGznzIQ+EksHyc3HZ9OlxTKmoCDKA+5vrSGwmmKO9sqEy0yIH X-Received: by 2002:a17:906:fb99:b0:a51:b0e1:863a with SMTP id lr25-20020a170906fb9900b00a51b0e1863amr1287680ejb.4.1713517199923; Fri, 19 Apr 2024 01:59:59 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1713517199; cv=pass; d=google.com; s=arc-20160816; b=j4s+uVOwKST6um1W2WX9wTDT9lcB5M7Te3H0qRqnZgx6wpshS6FbywOYWtGKeo2Wl7 PxUZjE+WNIEaZmCOuOHGFZrcVM8glZ/s11vzyOAAL3KrMy9lU7vDL/lNkfoWZ1h6nyp+ VUMtp05WmofYuObMBjyFRyTRiWJ7gcssPS/NpJ1EapMKZPpA7r5OqDePS1g16JvOpE/R h4W89j2ZYtqEYvLWEFFLRp/8nOuoV/JFbJnrHfIDOKb8tUK85IBeEX54odHzih2I+W2X Cx6UDdnkbWiRhNehDqgdBMWuAGeIV4q8/uVM6HCwzNIxIRBIfjr95cRaj/0bp2A3Tg9I A3hA== 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=nBDAnD8rwFNbWbPY9L0LNNm2QshByyJ5ytYISZegYXU=; fh=fhNDg/rgS8N1tOOUFS+PxlxZhbe2WE6Cy8nO5yBhacU=; b=OUsovue2UExyz61tAbIh+opnU9GvCBwLJ9rSaC1Bhr2JnS2EY7JILDqGtnvL36H8Y2 u7Rl+fn3jsDGlxp67HcKyjad8x7WkHZ+wd3BZOWrVcnO7aA3foqJI1i6VkaFPCIl9vpP d7DPIK0NxwNkUMpda5/L7zMkRdAOpL2/ASn85/etnlKxssmbEGwTPED0qQsP0BNbJI5S Rf5Ww5EGARiXbMrWUVmHDx4wjw2PUFqbuWly0IuhNDd7fYgvGENfk8XSigsfavd6EHLv hGNuHCCzuLWK5zSol0psy7J285UC4/hdjpPAJYs4vhJErN01BK7KfFABZeGXVOCOkC6t VtBA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Z0S0s0qx; 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-151198-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-151198-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. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id qk35-20020a1709077fa300b00a55800f28d8si1052528ejc.273.2024.04.19.01.59.59 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Apr 2024 01:59:59 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-151198-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Z0S0s0qx; 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-151198-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-151198-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 A536E1F21B80 for ; Fri, 19 Apr 2024 08:59:59 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 39BD07BB07; Fri, 19 Apr 2024 08:59:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="Z0S0s0qx" Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.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 7B33473528 for ; Fri, 19 Apr 2024 08:59:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713517175; cv=none; b=NBQY63gRCckzH3/937BgA2x+yf8SgKwpnJxOvNjcjt07zwNgrUZsJn/litVqRmUGvazvNVk4IjhI9EG7AwFGz8LDhjNik8+HIuOT6s1bgTbhpHTqkpQHAZAr7v8bYnAacUqDKtIymz9ymVG2+RIKrAhX98bo1y7g+zR8ChZcp/Y= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713517175; c=relaxed/simple; bh=p0m1HZy9UGhgenBFAgKeYWbF5X6HK5fP4nR7XaO2UKE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=FKKvFfhlJ4XkbvMVKycAt6oJJgX/hgv52W6RxCskE5lA8DIAkA4JlBH8dVCOlAJBO8Pa2Wx1JWUy7fJy8242Iq4CEzD8FTAalUt/oMBoeyh7tjsQ3hn138z4tFx1B3bg5BD+tTNnucxGtvt7EtPJ8SZQuLNJsm+Wmk33aWIe1KM= 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=Z0S0s0qx; arc=none smtp.client-ip=170.10.129.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=1713517172; 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=nBDAnD8rwFNbWbPY9L0LNNm2QshByyJ5ytYISZegYXU=; b=Z0S0s0qxBibJCYFdtLR6jbBBW8o59V/TK5Zeovgoa8AKEHUNUauXXCodrwivl2IeO1w9AK IQAxC4CBkmr0ujNVmQl/mf6dIcOgFnnYpWrdjdt7+dZ5yoDKRaOHcxtdGc3rDfDDibmaui 9aI2uzMOwO/9ZNRd3AIB/TbR0nrrTzg= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-155-YpT6iWRLNxe--xzwEC86Ng-1; Fri, 19 Apr 2024 04:59:30 -0400 X-MC-Unique: YpT6iWRLNxe--xzwEC86Ng-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 A4B702812FE9; Fri, 19 Apr 2024 08:59:29 +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 7389F20368A4; Fri, 19 Apr 2024 08:59:29 +0000 (UTC) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: isaku.yamahata@intel.com, xiaoyao.li@intel.com, binbin.wu@linux.intel.com, seanjc@google.com, rick.p.edgecombe@intel.com Subject: [PATCH 5/6] KVM: x86: Implement kvm_arch_vcpu_pre_fault_memory() Date: Fri, 19 Apr 2024 04:59:26 -0400 Message-ID: <20240419085927.3648704-6-pbonzini@redhat.com> In-Reply-To: <20240419085927.3648704-1-pbonzini@redhat.com> References: <20240419085927.3648704-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: Isaku Yamahata Wire KVM_PRE_FAULT_MEMORY ioctl to __kvm_mmu_do_page_fault() to populate guest memory. It can be called right after KVM_CREATE_VCPU creates a vCPU, since at that point kvm_mmu_create() and kvm_init_mmu() are called and the vCPU is ready to invoke the KVM page fault handler. The helper function kvm_mmu_map_tdp_page take care of the logic to process RET_PF_* return values and convert them to success or errno. Signed-off-by: Isaku Yamahata Message-ID: <9b866a0ae7147f96571c439e75429a03dcb659b6.1712785629.git.isaku.yamahata@intel.com> Signed-off-by: Paolo Bonzini --- arch/x86/kvm/Kconfig | 1 + arch/x86/kvm/mmu/mmu.c | 72 ++++++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/x86.c | 3 ++ 3 files changed, 76 insertions(+) diff --git a/arch/x86/kvm/Kconfig b/arch/x86/kvm/Kconfig index 7632fe6e4db9..54c155432793 100644 --- a/arch/x86/kvm/Kconfig +++ b/arch/x86/kvm/Kconfig @@ -44,6 +44,7 @@ config KVM select KVM_VFIO select HAVE_KVM_PM_NOTIFIER if PM select KVM_GENERIC_HARDWARE_ENABLING + select KVM_GENERIC_PRE_FAULT_MEMORY help Support hosting fully virtualized guest machines using hardware virtualization extensions. You will need a fairly recent diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 10e90788b263..a045b23964c0 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -4647,6 +4647,78 @@ int kvm_tdp_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) return direct_page_fault(vcpu, fault); } +static int kvm_tdp_map_page(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code, + u8 *level) +{ + int r; + + /* Restrict to TDP page fault. */ + if (vcpu->arch.mmu->page_fault != kvm_tdp_page_fault) + return -EOPNOTSUPP; + +retry: + r = __kvm_mmu_do_page_fault(vcpu, gpa, error_code, true, NULL, level); + if (r < 0) + return r; + + switch (r) { + case RET_PF_RETRY: + if (signal_pending(current)) + return -EINTR; + cond_resched(); + goto retry; + + case RET_PF_FIXED: + case RET_PF_SPURIOUS: + break; + + case RET_PF_EMULATE: + return -ENOENT; + + case RET_PF_CONTINUE: + case RET_PF_INVALID: + default: + WARN_ON_ONCE(r); + return -EIO; + } + + return 0; +} + +long kvm_arch_vcpu_pre_fault_memory(struct kvm_vcpu *vcpu, + struct kvm_pre_fault_memory *range) +{ + u64 error_code = PFERR_GUEST_FINAL_MASK; + u8 level = PG_LEVEL_4K; + u64 end; + int r; + + /* + * reload is efficient when called repeatedly, so we can do it on + * every iteration. + */ + kvm_mmu_reload(vcpu); + + if (kvm_arch_has_private_mem(vcpu->kvm) && + kvm_mem_is_private(vcpu->kvm, gpa_to_gfn(range->gpa))) + error_code |= PFERR_PRIVATE_ACCESS; + + /* + * Shadow paging uses GVA for kvm page fault, so restrict to + * two-dimensional paging. + */ + r = kvm_tdp_map_page(vcpu, range->gpa, error_code, &level); + if (r < 0) + return r; + + /* + * If the mapping that covers range->gpa can use a huge page, it + * may start below it or end after range->gpa + range->size. + */ + end = (range->gpa & KVM_HPAGE_MASK(level)) + KVM_HPAGE_SIZE(level); + return min(range->size, end - range->gpa); +} + static void nonpaging_init_context(struct kvm_mmu *context) { context->page_fault = nonpaging_page_fault; diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 83b8260443a3..619ad713254e 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -4715,6 +4715,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) case KVM_CAP_MEMORY_FAULT_INFO: r = 1; break; + case KVM_CAP_PRE_FAULT_MEMORY: + r = tdp_enabled; + break; case KVM_CAP_EXIT_HYPERCALL: r = KVM_EXIT_HYPERCALL_VALID_MASK; break; -- 2.43.0