From patchwork Wed Jul 10 23:42:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Houghton X-Patchwork-Id: 13729878 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id E1E4FC3DA41 for ; Wed, 10 Jul 2024 23:45:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To:From: Subject:Message-ID:References:Mime-Version:In-Reply-To:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=vMX//RNaH4NkIl2m+HJXagx9p57k7/J+D9F5NIPrc7U=; b=nMaICJL5QvPJm+vgQshu4++eiz YmU6QzOaHiD1ihA4KABt/+VqbuPTnMkOo5lE2MpKaitDbmg+IPuZS4m52mE5/wlZ71rNiTqdxuoAE OdSefD0UEz5GxF1CtKskMC+HigiBQ29wsd4XPdkKZcDFJccUDL0QzEfCit6yr01hsKKuss5h7EHZM QGhi4GlzaCXIO7N7n0e2y2LuX3BkYbza0xjt7Nnwy3WqrywchLY4ZORV2vy8U4g2Fr9bKBpJ9aHl9 ajaJmy7FYimKhyMSXHxMdPgsrTD43Z8jsCr976Ds58+V+qJp1ib7Pk2/VYJUS6AUReIbZid8gLJc4 EqAvkQ0A==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sRgzy-0000000C8dU-3ul3; Wed, 10 Jul 2024 23:45:22 +0000 Received: from mail-vk1-xa49.google.com ([2607:f8b0:4864:20::a49]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sRgxT-0000000C7Ck-1ENU for linux-arm-kernel@lists.infradead.org; Wed, 10 Jul 2024 23:42:49 +0000 Received: by mail-vk1-xa49.google.com with SMTP id 71dfb90a1353d-4f2f51ef721so92080e0c.2 for ; Wed, 10 Jul 2024 16:42:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1720654966; x=1721259766; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=vMX//RNaH4NkIl2m+HJXagx9p57k7/J+D9F5NIPrc7U=; b=rmKF4tc+uOf2ejN4dmL8nClmX3HyKUgDPGSRZDp1yPhBT8smkTZiXxUh2cPA81T9cg 0zdLcl98Je1CsHyMmT7g4oyeT9rpcVdmZeE8xvwXXU/tSJ85sjJ9CJJlr0P6wmT9I+2X tH5LghKznJkMatBJ5HjwnHufaGxTT3QxRyLFJVz6HbaK7X0xmPPkMVLNTC0X0JzvK01A JJjXH95JFwJVTm/LbpMoRDP0N+5wE+RET0sxPkKzNbYKSqjiQoY7FSG3Il1358FbBgkb ik1YZ53FFXE7ml6a2Iqm8K7/q1BvmdA6hxIkdj1+o0xx+nG+1oSrh0TGPEA2EkK4lgHU jK9g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720654966; x=1721259766; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=vMX//RNaH4NkIl2m+HJXagx9p57k7/J+D9F5NIPrc7U=; b=DKFJY2V7R6q4gBcXKeE+oCD4UfcrBh3fi9Xhjr5+r6EY6PYH75K2scdEmD45OXxzOw N7IDFu3mQ3BcOa59FOZSuJ+dGzxJ8/xZ4jNotGD7b8xmQ5VUj0d5fc+vxeafDW1SNNCn TzMBzsSF0GrKeOP+mXhrfIPf8Dd8EgjTpzX7nemKrIGtx3CQQ68010jP1x/1IYjPVOeT gVL/pqy8068sylgEVO10ygYa0DD9mOtkfamRQoZ24InD+s4UGmrz356fpXVX0b14Shik 6BhLEoccH6zV9l7KHXAU0LvYjNcujgkMMr5ZyFlEyWX1ZJCSubcFF2DPYik38j1gEyen p51g== X-Forwarded-Encrypted: i=1; AJvYcCU34gi5KJRrkdjYY1UYBGZQ1VaAYWjUoad95lOGT1fbmhrMZocqSEN2RQ5kL4+I6EohswDC4VvZSa1+B52mFBRgOCZerN0/i1KXLJmoSebkhtLRUKA= X-Gm-Message-State: AOJu0YyPF0q9WemOf5w5liWzyPTl9ldNQ5wY6HCrsOn2+3AjyEtTKE1w ePdS1Hl0S+/Cz5aI/M3Wc1n4iztY5VZw6/fwPBLPtCkE/b4Xa3xGTjodq5JUd2lwyOowp6gaLHT eVMLg4i5rlGtuyoKwSA== X-Google-Smtp-Source: AGHT+IEugpK0Yfh/FPCI48ugZ9OJOyQKqaQUSbGH7U/GzTR99sI48hv2h3UP5y+olkBHP+PFInBjfeGEoeNdcaay X-Received: from jthoughton.c.googlers.com ([fda3:e722:ac3:cc00:14:4d90:c0a8:2a4f]) (user=jthoughton job=sendgmr) by 2002:a05:6122:a03:b0:4ef:5477:6a49 with SMTP id 71dfb90a1353d-4f33f2e712bmr190008e0c.2.1720654965985; Wed, 10 Jul 2024 16:42:45 -0700 (PDT) Date: Wed, 10 Jul 2024 23:42:12 +0000 In-Reply-To: <20240710234222.2333120-1-jthoughton@google.com> Mime-Version: 1.0 References: <20240710234222.2333120-1-jthoughton@google.com> X-Mailer: git-send-email 2.45.2.993.g49e7a77208-goog Message-ID: <20240710234222.2333120-9-jthoughton@google.com> Subject: [RFC PATCH 08/18] KVM: x86: Add KVM Userfault support From: James Houghton To: Paolo Bonzini Cc: Marc Zyngier , Oliver Upton , James Morse , Suzuki K Poulose , Zenghui Yu , Sean Christopherson , Shuah Khan , Peter Xu , Axel Rasmussen , David Matlack , James Houghton , kvm@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240710_164247_373760_1CD44347 X-CRM114-Status: GOOD ( 22.87 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org The first prong for enabling KVM Userfault support for x86 is to be able to inform userspace of userfaults. We know when userfaults occurs when fault->pfn comes back as KVM_PFN_ERR_FAULT, so in kvm_mmu_prepare_memory_fault_exit(), simply check if fault->pfn is indeed KVM_PFN_ERR_FAULT. This means always setting fault->pfn to a known value (I have chosen KVM_PFN_ERR_FAULT) before calling kvm_mmu_prepare_memory_fault_exit(). The next prong is to unmap pages that are newly userfault-enabled. Do this in kvm_arch_pre_set_memory_attributes(). The final prong is to only allow PAGE_SIZE mappings when KVM Userfault is enabled. This prevents us from mapping a userfault-enabled gfn with a fault on a non-userfault-enabled gfn. Signed-off-by: James Houghton --- arch/x86/kvm/Kconfig | 1 + arch/x86/kvm/mmu/mmu.c | 60 ++++++++++++++++++++++++++------- arch/x86/kvm/mmu/mmu_internal.h | 3 +- include/linux/kvm_host.h | 5 ++- 4 files changed, 55 insertions(+), 14 deletions(-) diff --git a/arch/x86/kvm/Kconfig b/arch/x86/kvm/Kconfig index 80e5afde69f4..ebd1ec6600bc 100644 --- a/arch/x86/kvm/Kconfig +++ b/arch/x86/kvm/Kconfig @@ -45,6 +45,7 @@ config KVM select HAVE_KVM_PM_NOTIFIER if PM select KVM_GENERIC_HARDWARE_ENABLING select KVM_WERROR if WERROR + select KVM_USERFAULT 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 1432deb75cbb..6b6a053758ec 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -3110,6 +3110,13 @@ static int __kvm_mmu_max_mapping_level(struct kvm *kvm, struct kvm_lpage_info *linfo; int host_level; + /* + * KVM Userfault requires new mappings to be 4K, as userfault check was + * done only for the particular page was faulted on. + */ + if (kvm_userfault_enabled(kvm)) + return PG_LEVEL_4K; + max_level = min(max_level, max_huge_page_level); for ( ; max_level > PG_LEVEL_4K; max_level--) { linfo = lpage_info_slot(gfn, slot, max_level); @@ -3265,6 +3272,9 @@ static int kvm_handle_error_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *fa return RET_PF_RETRY; } + if (fault->pfn == KVM_PFN_ERR_USERFAULT) + kvm_mmu_prepare_memory_fault_exit(vcpu, fault); + return -EFAULT; } @@ -4316,6 +4326,9 @@ static u8 kvm_max_private_mapping_level(struct kvm *kvm, kvm_pfn_t pfn, { u8 req_max_level; + if (kvm_userfault_enabled(kvm)) + return PG_LEVEL_4K; + if (max_level == PG_LEVEL_4K) return PG_LEVEL_4K; @@ -4335,6 +4348,12 @@ static int kvm_faultin_pfn_private(struct kvm_vcpu *vcpu, { int max_order, r; + /* + * Make sure a pfn is set so that kvm_mmu_prepare_memory_fault_exit + * doesn't read uninitialized memory. + */ + fault->pfn = KVM_PFN_ERR_FAULT; + if (!kvm_slot_can_be_private(fault->slot)) { kvm_mmu_prepare_memory_fault_exit(vcpu, fault); return -EFAULT; @@ -7390,21 +7409,37 @@ void kvm_mmu_pre_destroy_vm(struct kvm *kvm) bool kvm_arch_pre_set_memory_attributes(struct kvm *kvm, struct kvm_gfn_range *range) { + unsigned long attrs = range->arg.attributes; + /* - * Zap SPTEs even if the slot can't be mapped PRIVATE. KVM x86 only - * supports KVM_MEMORY_ATTRIBUTE_PRIVATE, and so it *seems* like KVM - * can simply ignore such slots. But if userspace is making memory - * PRIVATE, then KVM must prevent the guest from accessing the memory - * as shared. And if userspace is making memory SHARED and this point - * is reached, then at least one page within the range was previously - * PRIVATE, i.e. the slot's possible hugepage ranges are changing. - * Zapping SPTEs in this case ensures KVM will reassess whether or not - * a hugepage can be used for affected ranges. + * For KVM_MEMORY_ATTRIBUTE_PRIVATE: + * Zap SPTEs even if the slot can't be mapped PRIVATE. It *seems* like + * KVM can simply ignore such slots. But if userspace is making memory + * PRIVATE, then KVM must prevent the guest from accessing the memory + * as shared. And if userspace is making memory SHARED and this point + * is reached, then at least one page within the range was previously + * PRIVATE, i.e. the slot's possible hugepage ranges are changing. + * Zapping SPTEs in this case ensures KVM will reassess whether or not + * a hugepage can be used for affected ranges. + * + * For KVM_MEMORY_ATTRIBUTE_USERFAULT: + * When enabling, we want to zap the mappings that land in @range, + * otherwise we will not be able to trap vCPU accesses. + * When disabling, we don't need to zap anything. */ - if (WARN_ON_ONCE(!kvm_arch_has_private_mem(kvm))) + if (WARN_ON_ONCE(!kvm_userfault_enabled(kvm) && + !kvm_arch_has_private_mem(kvm))) return false; - return kvm_unmap_gfn_range(kvm, range); + if (kvm_arch_has_private_mem(kvm) || + (attrs & KVM_MEMORY_ATTRIBUTE_USERFAULT)) + return kvm_unmap_gfn_range(kvm, range); + + /* + * We are disabling USERFAULT. No unmap necessary. An unmap to get + * huge mappings again will come later. + */ + return false; } static bool hugepage_test_mixed(struct kvm_memory_slot *slot, gfn_t gfn, @@ -7458,7 +7493,8 @@ bool kvm_arch_post_set_memory_attributes(struct kvm *kvm, * a range that has PRIVATE GFNs, and conversely converting a range to * SHARED may now allow hugepages. */ - if (WARN_ON_ONCE(!kvm_arch_has_private_mem(kvm))) + if (WARN_ON_ONCE(!kvm_userfault_enabled(kvm) && + !kvm_arch_has_private_mem(kvm))) return false; /* diff --git a/arch/x86/kvm/mmu/mmu_internal.h b/arch/x86/kvm/mmu/mmu_internal.h index ce2fcd19ba6b..9d8c8c3e00a1 100644 --- a/arch/x86/kvm/mmu/mmu_internal.h +++ b/arch/x86/kvm/mmu/mmu_internal.h @@ -284,7 +284,8 @@ static inline void kvm_mmu_prepare_memory_fault_exit(struct kvm_vcpu *vcpu, { kvm_prepare_memory_fault_exit(vcpu, fault->gfn << PAGE_SHIFT, PAGE_SIZE, fault->write, fault->exec, - fault->is_private); + fault->is_private, + fault->pfn == KVM_PFN_ERR_USERFAULT); } static inline int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 2005906c78c8..dc12d0a5498b 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -2400,7 +2400,8 @@ static inline void kvm_account_pgtable_pages(void *virt, int nr) static inline void kvm_prepare_memory_fault_exit(struct kvm_vcpu *vcpu, gpa_t gpa, gpa_t size, bool is_write, bool is_exec, - bool is_private) + bool is_private, + bool is_userfault) { vcpu->run->exit_reason = KVM_EXIT_MEMORY_FAULT; vcpu->run->memory_fault.gpa = gpa; @@ -2410,6 +2411,8 @@ static inline void kvm_prepare_memory_fault_exit(struct kvm_vcpu *vcpu, vcpu->run->memory_fault.flags = 0; if (is_private) vcpu->run->memory_fault.flags |= KVM_MEMORY_EXIT_FLAG_PRIVATE; + if (is_userfault) + vcpu->run->memory_fault.flags |= KVM_MEMORY_EXIT_FLAG_USERFAULT; } #ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES