From patchwork Wed Jul 10 23:42:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Houghton X-Patchwork-Id: 13729870 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 A7F58C3DA41 for ; Wed, 10 Jul 2024 23:43:23 +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=/kOa+bY4z73FQuKac3t3a5fniJTGUgyq//kzfHQQRXg=; b=EDWRDQxtYOu2NqR5GC2Jp7u/h/ uUkz5jxoDMWKOr2VAMhn8a+Jck8Jj8WvB0JxPioEU4SQNJG3NDvW1WN0oAWvh8QP/1RTXM6E1Kj3s BF2I72TuQd+3XVtpSvH2cUxrUolmJw2M8fOI58/oM82MEWge1qMxM32D6ELtsHGcJVIoj0eJFCPO/ OlkVCNT8KLcGr3XPAdyu0UJLXjUsegWTb3eNq2Fzvn+v/hYVtDCrf9zf9l0kZ0Oi5vas+xJnrWAsq 656YC5+5BHn0B3XszKdprQ+zrS5BS5cEK75uQxqX9pALFaQGKztI6xT9qel/h2LyXXWz/ypawvoWQ HR/ED0Jg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sRgxr-0000000C7S3-37HQ; Wed, 10 Jul 2024 23:43:11 +0000 Received: from mail-yw1-x1149.google.com ([2607:f8b0:4864:20::1149]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sRgxL-0000000C76q-32AG for linux-arm-kernel@lists.infradead.org; Wed, 10 Jul 2024 23:42:41 +0000 Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-65a14a9241bso5236087b3.0 for ; Wed, 10 Jul 2024 16:42:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1720654958; x=1721259758; 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=/kOa+bY4z73FQuKac3t3a5fniJTGUgyq//kzfHQQRXg=; b=t6yz+EG3v0gK/+UMBjhwa1scZEwRQIerUoSvxFN8KzovrFjNwufuZNp+mstvG1LsDD 3HO7e4cnLchBbNiQegElVU0fBQKk4d5lHffAYMn1etox/TIXQ7hlZ2CR55bDmqi3Oo5y L65MIwgI8D5+sLFGQ4ETafBFensKDxQ9cvebP7FLJj0M6CfqaC6ng1zW4V5WIGa0azNu G6zWdGrpGVqinMasTe/N2ykjOHBl5+DRgTStMf3RdI0YyrvnyltKq48x0ve196brmqsH aR0xHXXATjD1EBMprEwdzurtDeT5mcS1GNDgcRlvLgZhygnnE5wNYR4t8gHJoOhcxeYr 7wUQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720654958; x=1721259758; 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=/kOa+bY4z73FQuKac3t3a5fniJTGUgyq//kzfHQQRXg=; b=bx85j5JeGALXqJlqhcbdfwqxphYdCIvUXvS/Kci4Qlyn48VulRdFd15fs+93QFritl 9N2kI6jmJNI2qBEXfzU1WWFmvhp2Ebu+dhbDruFXYgUq9EikQM7jPqvuM1TvHrbSNCBC tKEjuC0al+SsC3QFBShjggz7W8n8nHXVISAzbTM6FMXF52vpXsTjeK7tgOD5InG448W0 SwiRH61pJqvnL0lPvY6X+9na9jpd3B0RCLvwr5qRqQc1eW3x2E5dX0Rrtj/LjzgLu2aP yzK4G7LQs3/H3TmjoCrGFaveNe3VRIXdhDtELGIHCH+a4A0CWXp8KtaHJVZj0PqZ9oqm XpCg== X-Forwarded-Encrypted: i=1; AJvYcCUO7p+QxL0yrJc7rPljWZX/BXtJib2MV7FiCE7GMiWEutgkF9FhJJkmmP706XoSgxV97njunekvsJyeQhhSPvNIh589B2kh9zK+75MhaNzZcWrVLSI= X-Gm-Message-State: AOJu0YwQXGcOjHqVoayM1o4KqpotmB3Ka5KkXSg6Es4OyhZOQ1XWlPEL LAtqpNRME5U5PzBLGV/RRWGwfIZIsP+jcI0rG0KlOwEXK6VDAx02c6WdFw0v7r7MbVtpWX1qGV8 47occA1JPu0zDn0uF8w== X-Google-Smtp-Source: AGHT+IEQILe/Q46nVYjT6VvqMQTMH6KcT24WPu+37xvorf2J37tSktUkMizR+hLJi1VSG+Olb8BNJuoCHzqVyuyh X-Received: from jthoughton.c.googlers.com ([fda3:e722:ac3:cc00:14:4d90:c0a8:2a4f]) (user=jthoughton job=sendgmr) by 2002:a05:6902:161a:b0:e02:6dcf:e9ab with SMTP id 3f1490d57ef6-e041b0688b2mr15353276.6.1720654958570; Wed, 10 Jul 2024 16:42:38 -0700 (PDT) Date: Wed, 10 Jul 2024 23:42:05 +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-2-jthoughton@google.com> Subject: [RFC PATCH 01/18] KVM: Add KVM_USERFAULT build option 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_164239_847108_65549CC6 X-CRM114-Status: UNSURE ( 9.69 ) X-CRM114-Notice: Please train this message. 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 Some architectures will not support KVM_USERFAULT, so we need to have a build option to avoid including it for those architectures. Signed-off-by: James Houghton --- virt/kvm/Kconfig | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/virt/kvm/Kconfig b/virt/kvm/Kconfig index 754c6c923427..f1b660d593e4 100644 --- a/virt/kvm/Kconfig +++ b/virt/kvm/Kconfig @@ -117,3 +117,7 @@ config HAVE_KVM_GMEM_PREPARE config HAVE_KVM_GMEM_INVALIDATE bool depends on KVM_PRIVATE_MEM + +config KVM_USERFAULT + select KVM_GENERIC_MEMORY_ATTRIBUTES + bool From patchwork Wed Jul 10 23:42:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Houghton X-Patchwork-Id: 13729872 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 6947BC3DA42 for ; Wed, 10 Jul 2024 23:43:57 +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=HCYoX0wE5GpicSTYT42iy6iZfDY9W+9PX613SFW91R8=; b=AxxO/FQOOwwAKF7iY3mw5UfNk1 l5XOI1aAk2tgs4O9boKBCjWkYOYlGr43h3aTBqKNggjlh8UX6eKj1DH7N09BRSY22i85vHhCQw+ZU mlxMA5Ba/jIIhCL1iUtymtaTrgyAhraTOD02od4T54JfsSx20nLnp3BF4z8sXK2j1yOJdavSAW+0D yvXOEGOuv5Rmnaz3gIJPkM74evWDkF/0kAiwS+m25ikiAUJovmCv+atUYtTiWwhHyU8zWjg51EO0a H9rI1uQd9Jl6VFNSwIUpdNsqInWzagJzq1CZnLaDkIsGz4Pa07tdWjVbb2bRa/YiO9xPUX7C2mSrX /Alzzvvw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sRgyP-0000000C7jj-1pcf; Wed, 10 Jul 2024 23:43:45 +0000 Received: from mail-vs1-xe4a.google.com ([2607:f8b0:4864:20::e4a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sRgxN-0000000C78E-40sy for linux-arm-kernel@lists.infradead.org; Wed, 10 Jul 2024 23:42:43 +0000 Received: by mail-vs1-xe4a.google.com with SMTP id ada2fe7eead31-48ffc7dc42fso87210137.1 for ; Wed, 10 Jul 2024 16:42:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1720654960; x=1721259760; 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=HCYoX0wE5GpicSTYT42iy6iZfDY9W+9PX613SFW91R8=; b=aMKIohpNBFV1QY0T9z+F5earIg6oyyznNhoCLVhhc1uk9F2zhYd55DkqWwVGeQ6EVo O48iP2un5wz2BYxxk+c4QV5BDE08vsWSsYBp1zOU0QFhyQmMF9UR427rKIbzFbLcXv8y J+Hy8aYQ1rKfEZY+x5r5vhpIQExXMv2LUew7KWPvEAggIbQ2rt4y0zNE4sAjg/urxPgh 2CZPFHSYlscSmAREpwDp42mtms8ZJQY5SFjJC34RPbjpOpCmeHe1zvCOPetWqNgosJDX XxxOqkPrjfijyQdm7adRjt0A9qcxE8Tr9ONDFFNpG46KsbSE2kIxrrVfcykr8YBVSnko xg5g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720654960; x=1721259760; 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=HCYoX0wE5GpicSTYT42iy6iZfDY9W+9PX613SFW91R8=; b=J363dm98cnKazedoTGBknGoY27rnwFdDVzhpuH7wxqA+aPKn05AKg3imvsUom8cj96 V+R2UVj5NeuWPrjOKWXxhdZnyP08MXXD1/DVvETyhR+ff6YSE09eX89Bk12e7J3Dlepw v10xr0QLetAKGTPV9QpRiDHmHEWhY18Nyu1pQseL9Aeop2lec3L1O9WzV/hhIJOtRTJW Ts+7A4YW0AAgJhoRP9xxBr5t3MX6bVT0Ir0xagF0gQCRGXRcsOpGQKxKomfTEKTC8Fii 6HxwH5snDIj2BfeDCccS6dxsF0opvntbES562hzBsjR6XeOS/Rwfis2QHqShe4AmMb0E 21Rw== X-Forwarded-Encrypted: i=1; AJvYcCUt+xyKsXMW5+Cjbrq41/jgg+V7+vDAKK96xm9LmArF2Qg9Z8HRGTTnkGO57bn4CkauGzUtNOVSMPgt7Lw9CtZdutSHeC6WPqXq8KOcTInQBKhHbs8= X-Gm-Message-State: AOJu0YwJUafy/MsOME+JrApqUUYLJOWEYN8i74bzw3fr9VjMxQR/dMF8 YNP303VWzTAVu5EjAJMRWvqhjxMgBYNYCwiSaYn7pNFV2GOTX4+a5zObDlCMCZPZKjeq+i9eTKP rHUWAcMRnTQI2TE0USA== X-Google-Smtp-Source: AGHT+IG6wA2gw3HKgkdoyUxrFHR7N0Dl00lUttDnuqK5Ufpq56dpOwAB+SOcWAyxOYR+FLktKLLLdIod/BQqZoEF X-Received: from jthoughton.c.googlers.com ([fda3:e722:ac3:cc00:14:4d90:c0a8:2a4f]) (user=jthoughton job=sendgmr) by 2002:a05:6102:38cd:b0:48b:e1a1:e57 with SMTP id ada2fe7eead31-49031dbef0emr134053137.0.1720654959859; Wed, 10 Jul 2024 16:42:39 -0700 (PDT) Date: Wed, 10 Jul 2024 23:42:06 +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-3-jthoughton@google.com> Subject: [RFC PATCH 02/18] KVM: Add KVM_CAP_USERFAULT and KVM_MEMORY_ATTRIBUTE_USERFAULT 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_164242_062839_B839C047 X-CRM114-Status: GOOD ( 22.59 ) 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 Add the ability to enable and disable KVM Userfault, and add KVM_MEMORY_ATTRIBUTE_USERFAULT to control whether or not pages should trigger userfaults. The presence of a kvm_userfault_ctx in the struct kvm is what signifies whether KVM Userfault is enabled or not. To make sure that this struct is non-empty, include a struct eventfd_ctx pointer, although it is not used in this patch. Signed-off-by: James Houghton --- Documentation/virt/kvm/api.rst | 23 ++++++++ include/linux/kvm_host.h | 14 +++++ include/uapi/linux/kvm.h | 5 ++ virt/kvm/kvm_main.c | 96 +++++++++++++++++++++++++++++++++- 4 files changed, 136 insertions(+), 2 deletions(-) diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst index a71d91978d9e..26a98fea718c 100644 --- a/Documentation/virt/kvm/api.rst +++ b/Documentation/virt/kvm/api.rst @@ -8070,6 +8070,29 @@ error/annotated fault. See KVM_EXIT_MEMORY_FAULT for more information. +7.35 KVM_CAP_USERFAULT +------------------------------ + +:Architectures: none +:Parameters: args[0] - whether or not to enable KVM Userfault. To enable, + pass KVM_USERFAULT_ENABLE, and to disable pass + KVM_USERFAULT_DISABLE. + args[1] - the eventfd to be notified when asynchronous userfaults + occur. + +:Returns: 0 on success, -EINVAL if args[0] is not KVM_USERFAULT_ENABLE + or KVM_USERFAULT_DISABLE, or if KVM Userfault is not supported. + +This capability, if enabled with KVM_ENABLE_CAP, allows userspace to mark +regions of memory as KVM_MEMORY_ATTRIBUTE_USERFAULT, in which case, attempted +accesses to these regions of memory by KVM_RUN will fail with +KVM_EXIT_MEMORY_FAULT. Attempted accesses by other ioctls will fail with +EFAULT. + +Enabling this capability will cause all future faults to create +small-page-sized sptes. Collapsing these sptes back into their optimal size +is done with KVM_COLLAPSE_PAGE_TABLES. + 8. Other capabilities. ====================== diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 7b57878c8c18..f0d4db2d64af 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -730,6 +730,10 @@ struct kvm_memslots { int node_idx; }; +struct kvm_userfault_ctx { + struct eventfd_ctx *ev_fd; +}; + struct kvm { #ifdef KVM_HAVE_MMU_RWLOCK rwlock_t mmu_lock; @@ -831,6 +835,7 @@ struct kvm { bool dirty_ring_with_bitmap; bool vm_bugged; bool vm_dead; + struct kvm_userfault_ctx __rcu *userfault_ctx; #ifdef CONFIG_HAVE_KVM_PM_NOTIFIER struct notifier_block pm_notifier; @@ -2477,4 +2482,13 @@ long kvm_gmem_populate(struct kvm *kvm, gfn_t gfn, void __user *src, long npages void kvm_arch_gmem_invalidate(kvm_pfn_t start, kvm_pfn_t end); #endif +static inline bool kvm_userfault_enabled(struct kvm *kvm) +{ +#ifdef CONFIG_KVM_USERFAULT + return !!rcu_access_pointer(kvm->userfault_ctx); +#else + return false; +#endif +} + #endif diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index d03842abae57..c84c24a9678e 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -917,6 +917,7 @@ struct kvm_enable_cap { #define KVM_CAP_MEMORY_ATTRIBUTES 233 #define KVM_CAP_GUEST_MEMFD 234 #define KVM_CAP_VM_TYPES 235 +#define KVM_CAP_USERFAULT 236 struct kvm_irq_routing_irqchip { __u32 irqchip; @@ -1539,6 +1540,7 @@ struct kvm_memory_attributes { }; #define KVM_MEMORY_ATTRIBUTE_PRIVATE (1ULL << 3) +#define KVM_MEMORY_ATTRIBUTE_USERFAULT (1ULL << 4) #define KVM_CREATE_GUEST_MEMFD _IOWR(KVMIO, 0xd4, struct kvm_create_guest_memfd) @@ -1548,4 +1550,7 @@ struct kvm_create_guest_memfd { __u64 reserved[6]; }; +#define KVM_USERFAULT_ENABLE (1ULL << 0) +#define KVM_USERFAULT_DISABLE (1ULL << 1) + #endif /* __LINUX_KVM_H */ diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 8e422c2c9450..fb7972e61439 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -2430,10 +2430,16 @@ bool kvm_range_has_memory_attributes(struct kvm *kvm, gfn_t start, gfn_t end, static u64 kvm_supported_mem_attributes(struct kvm *kvm) { + u64 attributes = 0; if (!kvm || kvm_arch_has_private_mem(kvm)) - return KVM_MEMORY_ATTRIBUTE_PRIVATE; + attributes |= KVM_MEMORY_ATTRIBUTE_PRIVATE; - return 0; +#ifdef CONFIG_KVM_USERFAULT + if (!kvm || kvm_userfault_enabled(kvm)) + attributes |= KVM_MEMORY_ATTRIBUTE_USERFAULT; +#endif + + return attributes; } static __always_inline void kvm_handle_gfn_range(struct kvm *kvm, @@ -4946,6 +4952,84 @@ bool kvm_are_all_memslots_empty(struct kvm *kvm) } EXPORT_SYMBOL_GPL(kvm_are_all_memslots_empty); +#ifdef CONFIG_KVM_USERFAULT +static int kvm_disable_userfault(struct kvm *kvm) +{ + struct kvm_userfault_ctx *ctx; + + mutex_lock(&kvm->slots_lock); + + ctx = rcu_replace_pointer(kvm->userfault_ctx, NULL, + mutex_is_locked(&kvm->slots_lock)); + + mutex_unlock(&kvm->slots_lock); + + if (!ctx) + return 0; + + /* Wait for everyone to stop using userfault. */ + synchronize_srcu(&kvm->srcu); + + eventfd_ctx_put(ctx->ev_fd); + kfree(ctx); + return 0; +} + +static int kvm_enable_userfault(struct kvm *kvm, int event_fd) +{ + struct kvm_userfault_ctx *userfault_ctx; + struct eventfd_ctx *ev_fd; + int ret; + + mutex_lock(&kvm->slots_lock); + + ret = -EEXIST; + if (kvm_userfault_enabled(kvm)) + goto out; + + ret = -ENOMEM; + userfault_ctx = kmalloc(sizeof(*userfault_ctx), GFP_KERNEL); + if (!userfault_ctx) + goto out; + + ev_fd = eventfd_ctx_fdget(event_fd); + if (IS_ERR(ev_fd)) { + ret = PTR_ERR(ev_fd); + kfree(userfault_ctx); + goto out; + } + + ret = 0; + userfault_ctx->ev_fd = ev_fd; + + rcu_assign_pointer(kvm->userfault_ctx, userfault_ctx); +out: + mutex_unlock(&kvm->slots_lock); + return ret; +} + +static int kvm_vm_ioctl_enable_userfault(struct kvm *kvm, int options, + int event_fd) +{ + u64 allowed_options = KVM_USERFAULT_ENABLE | + KVM_USERFAULT_DISABLE; + bool enable; + + if (options & ~allowed_options) + return -EINVAL; + /* Exactly one of ENABLE or DISABLE must be set. */ + if (options == allowed_options || !options) + return -EINVAL; + + enable = options & KVM_USERFAULT_ENABLE; + + if (enable) + return kvm_enable_userfault(kvm, event_fd); + else + return kvm_disable_userfault(kvm); +} +#endif + static int kvm_vm_ioctl_enable_cap_generic(struct kvm *kvm, struct kvm_enable_cap *cap) { @@ -5009,6 +5093,14 @@ static int kvm_vm_ioctl_enable_cap_generic(struct kvm *kvm, return r; } +#ifdef CONFIG_KVM_USERFAULT + case KVM_CAP_USERFAULT: + if (cap->flags) + return -EINVAL; + + return kvm_vm_ioctl_enable_userfault(kvm, cap->args[0], + cap->args[1]); +#endif default: return kvm_vm_ioctl_enable_cap(kvm, cap); } From patchwork Wed Jul 10 23:42:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Houghton X-Patchwork-Id: 13729873 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 0BA85C3DA42 for ; Wed, 10 Jul 2024 23:44:11 +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=jsFdXvbgPwi63ncubEeloKTN1KDlxflVjQmCEhWWdzo=; b=vGM3aEbDUAV0+vMXhdQ/veGdq5 vgLT/980X4yOSlL7gSLe66Zqpsks5ucxyRJeNEkLtdgXHH2ZMv4iXxT+HrLKApwyhYC99VvfkSg/0 EQCiqxL+y7shBDQbpDm09/dIhjv7GEwapHUmJVMc/c1qmgUTlOrTpwSq6fcUTvK971WNE1BEB9bEe p570zwaPurXcfkJHpNvPeKSuyAte9oeD5jW04CfigbHWOQBKRDOyaoTpuDF4DgwAb36Kuuzc06EXZ pGXvIxi89xCJ+OGDiuF1I0npCFNteiMubjChv7ykg5iqdXkOrsdUbgh7b1Ouple1SL68LrYbhAI+L cBYC9fSA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sRgyf-0000000C7s7-4AZg; Wed, 10 Jul 2024 23:44:01 +0000 Received: from mail-yb1-xb49.google.com ([2607:f8b0:4864:20::b49]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sRgxO-0000000C78d-1VgH for linux-arm-kernel@lists.infradead.org; Wed, 10 Jul 2024 23:42:44 +0000 Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-e0561513c21so1392350276.0 for ; Wed, 10 Jul 2024 16:42:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1720654961; x=1721259761; 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=jsFdXvbgPwi63ncubEeloKTN1KDlxflVjQmCEhWWdzo=; b=HInhozWz1XC6v0Rmk2MhpSQet9KbySXnVIiI/Sa8P1gPdI8XEyc3FxSKKGpDZT0cUI ZiTTNS8v5GEzFKzmErPpfUZTz4sK9XKs1FlDz6rsidO5wKvOPH7M5U8OFxXE1yX/OWMh ouAW9GWXQpxt+YR/VlKoZGAoKT/vjPek9ff+ZJP9mcVBnwm9Q6m6RrHxGaYpFp+LX70B 1BeTcO38ysbhrHzGjKRSHpNWbv5AlDxaU/zbCx/OKrnd8A+EzVHPBtgdjZoVBU+al7zi yGvqXQsyQ5Cnbqw0lpOPsWiPsUPU4SwkqpD6yJtugLRu3Jj5dRYD6hBeUlKtZAY8f7++ KOdQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720654961; x=1721259761; 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=jsFdXvbgPwi63ncubEeloKTN1KDlxflVjQmCEhWWdzo=; b=TBAzuDrh7MOLPTAhsgRsabwoQJU2K8ohuejya1vbL1C+hI7+im4K+O/MJZTdcfogXQ LEsf1FhHpd6qzE/jV8Fp0JM4A4QN8Hq93sPFF1lm/68FR0ejcgSi9OfjFGQQMuumfZSZ 623bhwbZ8pbX0ghYds/1UymqOMRxS2eJPHbwFRVxd27aky0jV/ZbYV8BPGcZrb0ipb5A G+xvAZ0b5rHq+bTb6cxJxyMnzpSnv4fbgw1+iDREexdoEvjuxxpVMZEluk5cfhCEWgPi 5IQdkapUiA59pFgGx+X5Wqh3w0P3CjdQrJLshvxFtw639Q/dCvLx5geyXLaYOFlw4533 TjIw== X-Forwarded-Encrypted: i=1; AJvYcCXsfuRDsRoC2udB4lRiJ7zdYL/Qy5rk1vQIi/tEdQ5omyMl15XU5DoR5msGHTqFENZ6jNTLFlOfj/UEmG+lSAXveyYZh4tq4y5LlId5cFdWa3h5Kus= X-Gm-Message-State: AOJu0YxbA0YSI62d4HDiTKGCjEMcY+tqqpJM7+fJ+C7hA6xbTJlsloJ8 wJO8jY21DERQlJ7G5kAH+NiFp3NA9JUgcJ39V5b9gbdQbVS26j6asKsFKoIZqDNP/d4L/Ga8dJI /S1NTY/pnS0Ab/iJqJw== X-Google-Smtp-Source: AGHT+IG3Vk6X5wTdtR4KaDZB2edBlrlcDT80/rqBxWjm1e2N+7Y128jb+W0Gi/uqye/vInLab8G3pghPeGswAEHn X-Received: from jthoughton.c.googlers.com ([fda3:e722:ac3:cc00:14:4d90:c0a8:2a4f]) (user=jthoughton job=sendgmr) by 2002:a05:6902:2309:b0:e05:6532:aa4b with SMTP id 3f1490d57ef6-e05780396ccmr53350276.2.1720654960837; Wed, 10 Jul 2024 16:42:40 -0700 (PDT) Date: Wed, 10 Jul 2024 23:42:07 +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-4-jthoughton@google.com> Subject: [RFC PATCH 03/18] KVM: Put struct kvm pointer in memslot 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_164242_470891_8284C5DD X-CRM114-Status: GOOD ( 12.22 ) 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 Because a gfn having userfault enabled is tied to a struct kvm, we need a pointer to it. We could pass the kvm pointer around in the routines we need it, but that is a lot of churn, and there isn't much of a downside to simply storing the pointer in the memslot. Signed-off-by: James Houghton --- include/linux/kvm_host.h | 2 ++ virt/kvm/kvm_main.c | 2 ++ 2 files changed, 4 insertions(+) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index f0d4db2d64af..c1eb59a3141b 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -596,6 +596,8 @@ struct kvm_memory_slot { pgoff_t pgoff; } gmem; #endif + + struct kvm *kvm; }; static inline bool kvm_slot_can_be_private(const struct kvm_memory_slot *slot) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index fb7972e61439..ffa452a13672 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -1769,6 +1769,7 @@ static void kvm_copy_memslot(struct kvm_memory_slot *dest, dest->flags = src->flags; dest->id = src->id; dest->as_id = src->as_id; + dest->kvm = src->kvm; } static void kvm_invalidate_memslot(struct kvm *kvm, @@ -2078,6 +2079,7 @@ int __kvm_set_memory_region(struct kvm *kvm, new->npages = npages; new->flags = mem->flags; new->userspace_addr = mem->userspace_addr; + new->kvm = kvm; if (mem->flags & KVM_MEM_GUEST_MEMFD) { r = kvm_gmem_bind(kvm, new, mem->guest_memfd, mem->guest_memfd_offset); if (r) From patchwork Wed Jul 10 23:42:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Houghton X-Patchwork-Id: 13729874 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 AC56BC3DA42 for ; Wed, 10 Jul 2024 23:44:27 +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=6BWcXKAsCbgqy4w2nNCeUW6tFbXjuDYwJZkZfwSoSpo=; b=A2biPlDsPvnMXGDv7mKlUNqza+ UPldhxBSnV1Syc9Bc70GWqILHHR9w2M4ILBKvqZOI/ux2LUmslANzHcCUdLO4ier0TLHwOdXunZRS Vty6M31nig2t1TpjdCf9eHkl/XBawwQc/3r67ARqQrlYYs2cpCI2QraAaV0czaJjhj8xsBFtLS5gu WzJ0I5OeOXggUWBQvgm45f1kmMfs67LCf06i+iN2O23QRwpSrNLko3llms3BnU8DUDQnagj5QNnIn 2f7LG1PrOn9xAAQg3qtC3dyiH7/dD7Ed2jhWP8OrqLgeSYv5ErwhmxmobNuzhes1yUx6ezrdvzFbu 4HBf53DA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sRgyv-0000000C80t-0hNk; Wed, 10 Jul 2024 23:44:17 +0000 Received: from mail-ua1-x94a.google.com ([2607:f8b0:4864:20::94a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sRgxP-0000000C79s-3OHW for linux-arm-kernel@lists.infradead.org; Wed, 10 Jul 2024 23:42:45 +0000 Received: by mail-ua1-x94a.google.com with SMTP id a1e0cc1a2514c-80fe896d0e4so84797241.3 for ; Wed, 10 Jul 2024 16:42:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1720654962; x=1721259762; 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=6BWcXKAsCbgqy4w2nNCeUW6tFbXjuDYwJZkZfwSoSpo=; b=CXvB2QlcbuKSWY9U4vccsKq/hZQ9y9TDTDVqE3hzD6eqLwH7jGmMSIVEZND9fym/IE tYLrjr+wjF5suJxAp8cXJfwmGwVaKYQMn5EnPWE8U57W3z1+ym50YI3GBygcd8jZhvNd WugF0WqQ4fois7sbY31wE/tT5Cp5oBWufYY9NEbq4dNDjKLevio6oqM6RmUHo/y0/wV7 nbEfD1cp05Y97qoug5jL7kngD6Pk2PTfR+FtQgJySGiU3NJpwbhyqIePm2Aehq+pYz0B HFhKe63ZZC/xZhx0NNu4fR+JvlyLxx8AL01ar1VIho2qptiFApV//1qpL+5l2eyHLNFT pXag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720654962; x=1721259762; 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=6BWcXKAsCbgqy4w2nNCeUW6tFbXjuDYwJZkZfwSoSpo=; b=Hzk+/H64aOh8L5B4r3DV4tU+N4ZhS26VyqkuDo5CIkv2nxn3jD3lj76xfKzjhH7b2z TmCBeuUvka2Hz6XbsVWDIqqh5n1WhxC1VP2Q73Dj6H7G4LW35qno96LHHUqkrzkqcVr5 WXOy0WwZmNBx1YVsFKihnw2eDUaxQD7IiKrImQ6Sz4MV4jxpg4JnGvyE71XxULKb+afx 9TyZEOox5pQb4p8s7Moh7X43bBhy8tdKjBV1wGPgWk0u4zOwA8mkQp4eMzgCSSqo5Fsx sGSaT8WZZLcQn81teE87ldUthIuFPxDFUAAtUqvVmKIhC6eizxeXVXzQlg/C8/A5rBkT IVqQ== X-Forwarded-Encrypted: i=1; AJvYcCUPDzxvO/a4Ag+z6OLtxKkSeRRJxepROLhpLaH6vMw37q/k2H0MZAvmeHEsOOcYEzq7xvijIVNZ+5CX0IGEQgFcQhmFGAwj3zbFlhE65LPbDAbg48A= X-Gm-Message-State: AOJu0YxVHfpLTGjDXDbQm2Dc9lCwbEmztUDHGYbiioQcWMpLzYDbNC6M Vmra0/IXkRQ7y9+iUjoIpdlpYEy97lTjkGt4DveQRm0amzyjruKobu6f0j9hRphvoTfGTt38FPr lHRpHMJ4SxH+sVsh+OQ== X-Google-Smtp-Source: AGHT+IEs5vUQHQ0lGxrPeXnI8U3zg0scUB3VU7/qVip/3trOIT/81ZfcXpykTilnpI2xQFZASFVWimG+3mGsVHMv X-Received: from jthoughton.c.googlers.com ([fda3:e722:ac3:cc00:14:4d90:c0a8:2a4f]) (user=jthoughton job=sendgmr) by 2002:a05:6130:2b07:b0:810:2714:60fd with SMTP id a1e0cc1a2514c-81076dc9388mr47870241.1.1720654962027; Wed, 10 Jul 2024 16:42:42 -0700 (PDT) Date: Wed, 10 Jul 2024 23:42:08 +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-5-jthoughton@google.com> Subject: [RFC PATCH 04/18] KVM: Fail __gfn_to_hva_many for userfault gfns. 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_164243_929920_1644AEE4 X-CRM114-Status: GOOD ( 11.42 ) 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 Add gfn_has_userfault() that (1) checks that KVM Userfault is enabled, and (2) that our particular gfn is a userfault gfn. Check gfn_has_userfault() as part of __gfn_to_hva_many to prevent gfn->hva translations for userfault gfns. Signed-off-by: James Houghton --- include/linux/kvm_host.h | 12 ++++++++++++ virt/kvm/kvm_main.c | 3 +++ 2 files changed, 15 insertions(+) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index c1eb59a3141b..4cca896fb44a 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -140,6 +140,7 @@ static inline bool is_noslot_pfn(kvm_pfn_t pfn) #define KVM_HVA_ERR_BAD (PAGE_OFFSET) #define KVM_HVA_ERR_RO_BAD (PAGE_OFFSET + PAGE_SIZE) +#define KVM_HVA_ERR_USERFAULT (PAGE_OFFSET + 2 * PAGE_SIZE) static inline bool kvm_is_error_hva(unsigned long addr) { @@ -2493,4 +2494,15 @@ static inline bool kvm_userfault_enabled(struct kvm *kvm) #endif } +static inline bool gfn_has_userfault(struct kvm *kvm, gfn_t gfn) +{ +#ifdef CONFIG_KVM_USERFAULT + return kvm_userfault_enabled(kvm) && + (kvm_get_memory_attributes(kvm, gfn) & + KVM_MEMORY_ATTRIBUTE_USERFAULT); +#else + return false; +#endif +} + #endif diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index ffa452a13672..758deb90a050 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -2686,6 +2686,9 @@ static unsigned long __gfn_to_hva_many(const struct kvm_memory_slot *slot, gfn_t if (memslot_is_readonly(slot) && write) return KVM_HVA_ERR_RO_BAD; + if (gfn_has_userfault(slot->kvm, gfn)) + return KVM_HVA_ERR_USERFAULT; + if (nr_pages) *nr_pages = slot->npages - (gfn - slot->base_gfn); From patchwork Wed Jul 10 23:42:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Houghton X-Patchwork-Id: 13729875 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 4A3BAC3DA41 for ; Wed, 10 Jul 2024 23:44:42 +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=d8Rmzp/FnvTnt3i+tgLuvGyYDZdc9771iECvmROSKp8=; b=Nr9CTnSrSEnD3uz+zRmcA3Gub7 1NH3nUAJqw0i4u8zJ3yRnyqULYfZuQyNGwIeTkaNXg9AOio0FgmrTmgBmQEYpYVXGDf/V8Lak6BX7 ousQvNLX6UFpspBS+DdHJLDwgG18wYUGq42X6NDsKomrNvAcbF4QBaJa8GMv91dhokttFxvRIXox3 DCx6AsKvJGht2PuKZF03spXipz1+KWPJ7WhUcTbuwqPh5P2bu/COiKzvbuV7pG/yRuuNJUfzDsVVW QftBuzpI82s0kxAfrDXakqlcM73xhMS0g03As6bln3WvIwKVpUPAAENGWErfLV/SUl5VeaOLGrsHs PDt1sHvg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sRgzA-0000000C88w-1meS; Wed, 10 Jul 2024 23:44:32 +0000 Received: from mail-yb1-xb49.google.com ([2607:f8b0:4864:20::b49]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sRgxQ-0000000C7AH-2VyT for linux-arm-kernel@lists.infradead.org; Wed, 10 Jul 2024 23:42:46 +0000 Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-e02b7adfb95so493874276.2 for ; Wed, 10 Jul 2024 16:42:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1720654963; x=1721259763; 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=d8Rmzp/FnvTnt3i+tgLuvGyYDZdc9771iECvmROSKp8=; b=Mni6drhQumZe175IOiQX8ttzYIR+YDAM9mNqAfXFiOHmjVzbxjQQp13eshBex8yhe0 QSDecFia1Uu7bh16Jbr/mz6VMZ+jUfye/q82f+NvlnLiY9305cV1HGP0CEOjqkUuOkvb 9bq0XPSlRMJ3QMJY9QAD3KJQSXMrjj6aO6gE9itu225XeZAU6voF3Kma9UX/073/AG5r GcPTtclTZjSEhxzoydyy9J9Y+HqZRhmDx/hG1xENNSG89EtGGzhoLrDeVtcWReacCLtc JfIc6VqmR3fPvf8uygm9P44IB9UEEVfhCMrweCAoYhIQfFBLXoA6TBMq+Lrink45xLTT pr7Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720654963; x=1721259763; 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=d8Rmzp/FnvTnt3i+tgLuvGyYDZdc9771iECvmROSKp8=; b=LgIuJ7riOGCZoYnLOrHAd4u1n/07jOL0ij2u1mspLY/KSShHVckMutbb7HeXVYwkNP VjUqt2f/2bvVZE1MBJPNulAt8yYl1M+zqBhztzawz6J0tqBtif1UTSw0s40a6kzObGzb KPfXJh8szDt/sWgscwWouDVH2sJNQDWCOxfoRYprEYDsGB/ZYaSfNQo4qcGw3F/Jauez EAtzWSzcqtRcGEyZF0zc1GHw6fzyaohXvGJvwSh1IwGJfeBu1xObRa2UHY3LZGpRTg2+ /sHQigrdNpyrthO4/TPfF3E54HEfSr0ZJTTM0TZxJR2Kz7lH+hqGuvLFefWOzAAsj7kp /6Aw== X-Forwarded-Encrypted: i=1; AJvYcCXv+rwgoQtynRRvXJ8IeWHD4KgS5r2J2UOTkAyby25M8J9G0C/AbI9OLfgOaVw1iWpwT4xsOmhx2M/dO3n7jJw7rxy9cBnhb7ZDVclr0E3KPDsj4p0= X-Gm-Message-State: AOJu0YztZzer4ONL07/97jX7Z5fnkVg0wqF3ZjgUfPXOiDi36zwTB+iZ CGLh6vckL2N/yc/4F7T0JCEnPvk7WZ1q1hLYjM9iEVXLgMpsQLQy5y1hIPXyqlMLZki+RusPq2c 36ltwmVuLIq0B/85N6Q== X-Google-Smtp-Source: AGHT+IGzfu/is15/zbZN55ThZYoCmQ5oYPE0p+29NB0c/bvGkmqxURl5E0ogcz1hwkdaxZ/C24+OiRdX8VZRICmx X-Received: from jthoughton.c.googlers.com ([fda3:e722:ac3:cc00:14:4d90:c0a8:2a4f]) (user=jthoughton job=sendgmr) by 2002:a05:6902:2101:b0:e03:31ec:8a24 with SMTP id 3f1490d57ef6-e041b17dfb5mr382792276.8.1720654962919; Wed, 10 Jul 2024 16:42:42 -0700 (PDT) Date: Wed, 10 Jul 2024 23:42:09 +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-6-jthoughton@google.com> Subject: [RFC PATCH 05/18] KVM: Add KVM_PFN_ERR_USERFAULT 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_164244_701519_71F2EF13 X-CRM114-Status: GOOD ( 13.15 ) 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 When a GFN -> HVA translation (__gfn_to_hva_many()) fails with KVM_HVA_ERR_USERFAULT as part of a GFN -> PFN conversion (__gfn_to_pfn_memslot()), we need to return some kind of KVM_PFN_ERR. Introduce a new KVM_PFN_ERR_USERFAULT so that callers know that it is a userfault. Signed-off-by: James Houghton --- include/linux/kvm_host.h | 1 + virt/kvm/kvm_main.c | 8 +++++--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 4cca896fb44a..2005906c78c8 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -97,6 +97,7 @@ #define KVM_PFN_ERR_HWPOISON (KVM_PFN_ERR_MASK + 1) #define KVM_PFN_ERR_RO_FAULT (KVM_PFN_ERR_MASK + 2) #define KVM_PFN_ERR_SIGPENDING (KVM_PFN_ERR_MASK + 3) +#define KVM_PFN_ERR_USERFAULT (KVM_PFN_ERR_MASK + 4) /* * error pfns indicate that the gfn is in slot but faild to diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 758deb90a050..840e02c75fe3 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -3009,9 +3009,11 @@ kvm_pfn_t __gfn_to_pfn_memslot(const struct kvm_memory_slot *slot, gfn_t gfn, if (kvm_is_error_hva(addr)) { if (writable) *writable = false; - - return addr == KVM_HVA_ERR_RO_BAD ? KVM_PFN_ERR_RO_FAULT : - KVM_PFN_NOSLOT; + if (addr == KVM_HVA_ERR_RO_BAD) + return KVM_PFN_ERR_RO_FAULT; + if (addr == KVM_HVA_ERR_USERFAULT) + return KVM_PFN_ERR_USERFAULT; + return KVM_PFN_NOSLOT; } /* Do not map writable pfn in the readonly memslot. */ From patchwork Wed Jul 10 23:42:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Houghton X-Patchwork-Id: 13729876 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 C60D2C3DA42 for ; Wed, 10 Jul 2024 23:44:58 +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=W1e4HHE4z+5Knov3l2/RvFGqy44M6oXcmkh1U5rufQM=; b=TNLgoowvuZR+9O/+zAFJ/q87Zr 39kFGXiD5SxhgRVlZCKKnFiai4sSoXrWSTcQLrKt3nS24ItsPQT/CMLuTfieEFY7zeVqsMxhZ4jSG 1ZvnBf8kvNEoRKubM+v3BqbwQ18V4HVaQnb1qYX5OEfnVcwFlcCbPA2YTPA2LttUrOzfNEVs30SU+ MEa5chZTHrrQFHEk2Kol/Ve2EINOCq3mXBNHc8pGS4G/W7p/GNrGL8eyW99EKH4MFVE37ObT6z9Kg 3u/d7TOugiZ97+l4p8SwOicC47UqQLneF6Wu9wtkBAlNOyPXUS5iHga6jPbjY+I9ZwReetfSTT4vJ ePyKuhrg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sRgzP-0000000C8HM-3FAP; Wed, 10 Jul 2024 23:44:47 +0000 Received: from mail-yw1-x114a.google.com ([2607:f8b0:4864:20::114a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sRgxR-0000000C7BK-2N8f for linux-arm-kernel@lists.infradead.org; Wed, 10 Jul 2024 23:42:47 +0000 Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-64b5588bcafso5499597b3.1 for ; Wed, 10 Jul 2024 16:42:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1720654964; x=1721259764; 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=W1e4HHE4z+5Knov3l2/RvFGqy44M6oXcmkh1U5rufQM=; b=iFIBbgPvdvZ2YtR00jSJENVZ/gmREyioUC87er5oxwCUiSHQZNnc+FQjk3C+ZhtxII fxxrdCDtJedWsSF+NugQXC45cagcVm4Cm5J0Mih7Qv2mgb9treHMgqEJThdHKmlyGo9q ukwvXThenDg3iygO2NtoahcfxWgoQuKZ91zxAI+kz5EkSxLCZJKZuodj4AcUea45SFau 01CTEsY4C6NOh0IBrzggSNwNfwiD2OnUgG/x5C8Wzegd7p3ynDZTh1rsRa//IKooYOjV Bl7vf6iOdvqRihLGLO4lfLLi382eeyR5ZtGf3hjGft10cKMr67qxHJk8uNjjb6Tn8NC2 eYaw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720654964; x=1721259764; 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=W1e4HHE4z+5Knov3l2/RvFGqy44M6oXcmkh1U5rufQM=; b=f7SCl5iQeSgmk8bVDkOBCLuAH3WHWkpja+Gb3wQtLSBACRwFFUvjUGWG4vf19Iz2cF Gq2Erkk2VAk9bGstEPzL2E5rz2cSayPggdCM+oqiQS6d9HRbBqsAvSyv+ZjF/upb67kk uK/IqwRlUVZeIsDEpQBePimtPYU9wpNJ+wRf6lyT/fmZ6eE346uIG1ldpLeLrosBP2/H imR/2xk9qReB7cRNpdYu8nhXWAdZVTcRm+6omnUZwJNhJnAqjoVHSjyM71Ktgz/1k2XD HRdrlrUs++pU+4CQhZiVsaivmLm1kHCnGDNuERcCbK6CDtVkVOyaGZ7RkVNkjgTy3p1P oE2Q== X-Forwarded-Encrypted: i=1; AJvYcCXUDxe4VyvxJjKMPNUTLA6X+xccVe0LXv8hxR919V+yefety3s6+Fw7QBp+gQ05Eaae3gafAD60q/wRUTUisQdkii18R9U6Ru5tCR13LsUkaaUlcrI= X-Gm-Message-State: AOJu0Yye0SHR0rkkBf+SZdF+L3CgJCEmLkzpFd7uVAhHuIQB1oPaGdc1 /sghoDuUNNrY0/5J+iDWy4ICpzWh8NhUq8n5bL3NZItG2yL+sNJbfMBKp4qsRu55Iy4cJY33DVM WSVmI+SGLOkBphbiRPQ== X-Google-Smtp-Source: AGHT+IH5NTISR7DBZbSKRJUHRXFWyLuVrVBePrqFfNwytKF1BLIU+MNslDD3lgHeZpjWWwkTnE/TGMGwZ5jaEHpH X-Received: from jthoughton.c.googlers.com ([fda3:e722:ac3:cc00:14:4d90:c0a8:2a4f]) (user=jthoughton job=sendgmr) by 2002:a05:690c:7244:b0:62f:22cd:7082 with SMTP id 00721157ae682-658f01f530bmr1591717b3.5.1720654963909; Wed, 10 Jul 2024 16:42:43 -0700 (PDT) Date: Wed, 10 Jul 2024 23:42:10 +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-7-jthoughton@google.com> Subject: [RFC PATCH 06/18] KVM: Add KVM_MEMORY_EXIT_FLAG_USERFAULT 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_164245_665486_51B092E0 X-CRM114-Status: GOOD ( 10.21 ) 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 This is needed to support returning to userspace when a vCPU attempts to access a userfault gfn. Signed-off-by: James Houghton --- include/uapi/linux/kvm.h | 1 + 1 file changed, 1 insertion(+) diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index c84c24a9678e..6aa99b4587c6 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -429,6 +429,7 @@ struct kvm_run { /* KVM_EXIT_MEMORY_FAULT */ struct { #define KVM_MEMORY_EXIT_FLAG_PRIVATE (1ULL << 3) +#define KVM_MEMORY_EXIT_FLAG_USERFAULT (1ULL << 4) __u64 flags; __u64 gpa; __u64 size; From patchwork Wed Jul 10 23:42:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Houghton X-Patchwork-Id: 13729877 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 C1A4CC3DA41 for ; Wed, 10 Jul 2024 23:45:14 +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=TURU+uj4JjtASnposo8ch9F78eTbKidxkEMdeXOCkww=; b=ZqEP5MVSO7CSU62dc97vawXAV2 Uy31KZ97D0uoMvO7AvSbwO/Vjk3mmehfve6LACkOHsC3yHUZ63gNl4t88eCV2JzT2BbQDuQnHKnNU pMso471JBvHBneLWrtk1pOR1Pq2hmGJYFlfx7yE5/86C0e+lYwsOpPB0ZPlPXfrQjeRfiSZQ4/D2Y 8tlWCFu83x4+Oe/SKi53xvTxVdp5j8hLxRHzJlTF/GPM+ErRkYTVwKZOmrY5qMJ2asuVcQLc38c5y 5rV/viVrXBoqCm6NSd/vXUOTQKts+qmev84cFkXLe/lxE+wSynQyAbybZYrbkMXAF4/PnibgL0Z/T CFkQQpOg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sRgzh-0000000C8SB-2GRb; Wed, 10 Jul 2024 23:45:05 +0000 Received: from mail-vs1-xe4a.google.com ([2607:f8b0:4864:20::e4a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sRgxS-0000000C7By-3GB9 for linux-arm-kernel@lists.infradead.org; Wed, 10 Jul 2024 23:42:48 +0000 Received: by mail-vs1-xe4a.google.com with SMTP id ada2fe7eead31-48fde1e8d72so86321137.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=1720654965; x=1721259765; 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=TURU+uj4JjtASnposo8ch9F78eTbKidxkEMdeXOCkww=; b=oy1A0rDv39k44/6DCtXg1gcgxmLCmJamiLiSnC9+f37NDQMubejIq2u1mQsK1gLnrN KeqVbXOoalP2bMC640uSSScpTR6plqF038IHSgZNBJnQiz06Va0+Aa7KhiZrZu4k9BYR pA1RRFQx6sMB5BtRx3kg0W/Y+Zu3l2MEoznRD7dHOFo/84RT2+0ZQ+DTIGiRO8no4C/h BU0u/p+W5dIZh52DElmMgqfb9jW+OcL0vKxi/b2b7j1nqVY8xlxClrIN0sqR9OgbfbYa 6nX9+MC+Xp1v6MkCgR+cA+fwOIvGgLDw0h8M28xsncKk2HXyCOgKGu1EwLZ2ffh16Yd8 BnTw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720654965; x=1721259765; 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=TURU+uj4JjtASnposo8ch9F78eTbKidxkEMdeXOCkww=; b=p1dhbSQ+RUFrlv0FdUQtoAg8H/fdGoxwu0n2qbsk6wXP6NAKuEhEbufU/MVWuTYEcD Jys+b1UMBScSQfEJ7Fs+2HobsVg8OGvHIkdQcIRndQ+VdhVtXwtfTMzQG+JQpquf99oI gZHjgm0yxpcbAl5xAnTWotOHzR/+Xf+As/QSBMl1Ny5PzCh02FbRHb/r314qIXlBdgdm C3vGjRRumCfkY5h1jG9Z7yBj7clXuvuK3BVnn8CFkJUw5wvGejrU8++VCiazmKZbi/NY PDkvjWA+BjVh0ZrZ6wE/FsxDhd19WKH59VsnuzuRZadmvbBwp5bGnzm5nINAC0lghXbA E5IQ== X-Forwarded-Encrypted: i=1; AJvYcCXNUtJwEHqmWlkFzycHqFMtepx5QP+L03S09UZNclc3gR6/K6EiyOpL9e4Y+VFstHHvA7NzqPV19iUQZ6O0w7YQjdIhdVOzP0PsJ10O6bb3fTUbn9w= X-Gm-Message-State: AOJu0Yzye0Eyh4KLNMRUbosAgEH9To8FjcsU+EcY7BbkSzMXoB7aUhKf S1TqS5rzkl5wmRYLKxciXsQpQ0mI2y6iWDV3LJrHmxkCJnHvVNNykA7/hr0brXp7YwGbVw6E3+Q 2AcDu4AyPCCe3Ep2XuA== X-Google-Smtp-Source: AGHT+IHwjQCn8pz3/iqiomOrjrLQUXcW8j3Cmfdd9IBi0Npw4AjyC0JLSC59kn9LukO94PmnEj6ZVNwxW2iE6Npb X-Received: from jthoughton.c.googlers.com ([fda3:e722:ac3:cc00:14:4d90:c0a8:2a4f]) (user=jthoughton job=sendgmr) by 2002:a05:6102:2929:b0:48f:137a:78b2 with SMTP id ada2fe7eead31-490321d3690mr113577137.5.1720654965123; Wed, 10 Jul 2024 16:42:45 -0700 (PDT) Date: Wed, 10 Jul 2024 23:42:11 +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-8-jthoughton@google.com> Subject: [RFC PATCH 07/18] KVM: Provide attributes to kvm_arch_pre_set_memory_attributes 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_164246_843342_086C8B12 X-CRM114-Status: UNSURE ( 9.95 ) X-CRM114-Notice: Please train this message. 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 This is needed so that architectures can do the right thing (including at least unmapping newly userfault-enabled gfns) when KVM Userfault is enabled for a particular range. Signed-off-by: James Houghton --- virt/kvm/kvm_main.c | 1 + 1 file changed, 1 insertion(+) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 840e02c75fe3..77eb9f0de02d 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -2514,6 +2514,7 @@ static int kvm_vm_set_mem_attributes(struct kvm *kvm, gfn_t start, gfn_t end, struct kvm_mmu_notifier_range pre_set_range = { .start = start, .end = end, + .arg.attributes = attributes, .handler = kvm_pre_set_memory_attributes, .on_lock = kvm_mmu_invalidate_begin, .flush_on_ret = true, 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 From patchwork Wed Jul 10 23:42:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Houghton X-Patchwork-Id: 13729879 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 84097C3DA42 for ; Wed, 10 Jul 2024 23:45:49 +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=45lnTGeGmxXRWR2sOGpDAv49wjAnxo1Jn2Fj4ejBtf8=; b=KLXUlIo+ee7yrrRS2jOWj1tVe8 k/Q+OP1mfpsB3Mriy8DxObbdqq3R9Ov+Rbmd/Edb2dJmJn0PDS6vNCx7yYzttUEIrKWc1Hbz6WJcV LJVPMW6ktJ4v83+1Xpr0/V5ISz55Utl5yyhiywTzhZb1QCR4BULgkByWApDc6ZBKDQeAfn7Zrqnt3 mAiNX+hbMkvxkTQStJy5k7dc43TeSeMGhU65uVAc8kRvjQ9DTfAjKON7Y5S1dvN7ba2d+mLrdWz5m rMPql0tSO78Y6oLjkQ5J5nIyaZ+EDsY/G9bPbeAZWPLo7sLQE5kAV3kKDpoxCgkcwBY+075ren/gz Gh3D+BuQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sRh0F-0000000C8kI-1Rrf; Wed, 10 Jul 2024 23:45:39 +0000 Received: from mail-yb1-xb49.google.com ([2607:f8b0:4864:20::b49]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sRgxU-0000000C7DD-2q4J for linux-arm-kernel@lists.infradead.org; Wed, 10 Jul 2024 23:42:50 +0000 Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-e03fe45dc0bso527097276.0 for ; Wed, 10 Jul 2024 16:42:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1720654967; x=1721259767; 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=45lnTGeGmxXRWR2sOGpDAv49wjAnxo1Jn2Fj4ejBtf8=; b=US+xWvwnoLF+D75C0q98iz5zn4WZ92gMoFsTmRtc5byP5X1t9J7Ek4V7XtbZIaX8qC ERrK2NXOq62QltNfKZw/cgFMA6ZUkdWtYUlICO/ZDhDiNIktStrSC3RncWatUGwKcPZE Juuj9vpEYnEBleE9uIjQ6BOetayvP9BPq3cttia74zKr+f+qSXW6/g79YR5wVAES70UB XH7Hda5wSbSCYQZAnooDJDXbX8jXWb0RCtB8wZ0VBn5CiLUEbzNUFnfebT5NwGTM8Jfc PU1iXxwQYDCIAZLbDZbKxE7UHMkf9cWIV12vGV4HoLGpF6Ymgi4XftnRpRF2s3PpY9OV flFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720654967; x=1721259767; 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=45lnTGeGmxXRWR2sOGpDAv49wjAnxo1Jn2Fj4ejBtf8=; b=r2sCBYPQLTHyqo5cs9mCY9yT9r1TRbYOYRyB9JaRmqx2kcuMupzGteMJibDsFFAdbl LqlxYqPhqa/9Q76A4g6LeBwlhrii9LqMycRqNzzRwEp/pGyTKpmWt7rPGVEr/GIESvK7 dBmiK0xDzShuVb2MLgIHKLHeqM3EpCEjbUz+lwECXGSi/iudKWO62TUPdUoQVz5G/Ns5 6dP1EC26aIfhHkpho0UuECCnJ1cLUhsarnkP3toJL17SfLIxbDY0/kxWn1LnNW4lJioe 6D5zi3iao6gz1qBUU99YFzLX5rpC/A0GmofXrX09U+NXI4HvXJ8FhveZjDfle0sAp2ol X3pg== X-Forwarded-Encrypted: i=1; AJvYcCVGwaQL+VBxFDsy+0NtqPCjZMs0yMrFYftZTINircTSGOqFIQ5pz2QKRPOMLz1QN2SNioysX8mON9btdeQgFFAhqWDzyG72ZgLacmU6GFsSOvxW7To= X-Gm-Message-State: AOJu0Yw9K0zaT85Dkb9KoGjdsAo07+ZeNrKhrbciYGRimKDWiMZmIzWl Ux9hUYXYrgZCbxgX/EhR6KoOaQZQwUGepk5ouZsQHOo/GmK76hbLxSiyRkYXMMKtSb1WjE2ki0q /nabxo6+TYbRlRsmWrA== X-Google-Smtp-Source: AGHT+IFCxPs0hzUflzzz70jRYwVRx4Gd4jDUqm7Yw06oBbmfCbRIBM9zwU8x8QPsTa/dYviA394CpxXkoxFldKzv X-Received: from jthoughton.c.googlers.com ([fda3:e722:ac3:cc00:14:4d90:c0a8:2a4f]) (user=jthoughton job=sendgmr) by 2002:a05:6902:2381:b0:e05:65b7:32d9 with SMTP id 3f1490d57ef6-e0565b7352bmr91773276.6.1720654966908; Wed, 10 Jul 2024 16:42:46 -0700 (PDT) Date: Wed, 10 Jul 2024 23:42:13 +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-10-jthoughton@google.com> Subject: [RFC PATCH 09/18] KVM: x86: Add vCPU fault fast-path for Userfault 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_164248_756066_01A3660F X-CRM114-Status: GOOD ( 10.60 ) 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 Without this fast-path, we will take the asynchronous userfault path every time, which is inefficient. As implemented today, KVM Userfault isn't well optimized at all, but I'm providing this optimization because something like this will be required to significantly improve post-copy performance. Memory fault exits for userfaultfd were proposed for the same reason[1]. [1]: https://lore.kernel.org/kvm/20240215235405.368539-7-amoorthy@google.com/ Signed-off-by: James Houghton --- arch/x86/kvm/mmu/mmu.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 6b6a053758ec..f0dbc3c68e5c 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -4380,6 +4380,13 @@ static int __kvm_faultin_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault if (fault->is_private) return kvm_faultin_pfn_private(vcpu, fault); + /* Pre-check for userfault and bail out early. */ + if (gfn_has_userfault(fault->slot->kvm, fault->gfn)) { + fault->pfn = KVM_PFN_ERR_USERFAULT; + kvm_mmu_prepare_memory_fault_exit(vcpu, fault); + return -EFAULT; + } + async = false; fault->pfn = __gfn_to_pfn_memslot(fault->slot, fault->gfn, false, false, &async, fault->write, From patchwork Wed Jul 10 23:42:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Houghton X-Patchwork-Id: 13729880 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 A094DC3DA42 for ; Wed, 10 Jul 2024 23:46:05 +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=m+X5KVuFwrRiCUCCxSkgB4VRafKsM+RRSJ9rZMEadTc=; b=E0aOvKkJEUxX6nDG1kvTmTSLLw a3jALJZy9KIiGRJGy/4YSz8ukTNZS4CcGM8UFEh7ww8HnwHbQvBwh/LJIhFeAU9RBihZ0ibcv4n5Z dXLHc8ziqfWtGDFO06Dl0u8y/YREpMrSGGxEZ+CnmiKyDtuvAIvIuXTdGDWw33d0MwhPiRgMpLhVo GMGeFzs54jXxFuSZXD4jqpf8NunyqxjSTDszZvJP8jcDAj8qE8Tmp7KHXkrgZmaqJFWm/5VP/Xs0o Cks3VPAvdBh5zg+T6WIEp+cNOMj/l6WPAZw6yxUwJnIAO5gqyjMkibcO1RTn0vO1UJM81EeUWSmZR D2+4fYcg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sRh0U-0000000C8t5-2iuw; Wed, 10 Jul 2024 23:45:54 +0000 Received: from mail-yb1-xb49.google.com ([2607:f8b0:4864:20::b49]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sRgxV-0000000C7Dv-3Rxw for linux-arm-kernel@lists.infradead.org; Wed, 10 Jul 2024 23:42:51 +0000 Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-e037c3d20a6so518303276.0 for ; Wed, 10 Jul 2024 16:42:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1720654968; x=1721259768; 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=m+X5KVuFwrRiCUCCxSkgB4VRafKsM+RRSJ9rZMEadTc=; b=Vqe67zC+xfM1GN3bvPRSkGfTaTbZMsv55U0ZDlnFYNkUWYS0MCeu2Mzkgx39+R3ERj O1S//tGf+bDCAs2hXZJ1/wlrZeKrWR9Ig/qUOVPrbxRpO2caVFAj+1HEhPszp3z+dw1/ rW3ZzB9ckepSqPdneGByRotwwd3fkHPh08V6R/d7f3VtJuMbGSmUD9hHrzi38bHk61Bp Brue8/DcT2JhIFb+aYlSCcotEDwuHPue08W5j9gmZPvhe6INAtwHrV7KFOTQp+ciPnvp VIkPetPTlHMM/q/Rpruco63hZLYVFcnu+545QSAP/nJPZyIxNlQhPBMZJHSc9Ixdy0mI b7sw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720654968; x=1721259768; 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=m+X5KVuFwrRiCUCCxSkgB4VRafKsM+RRSJ9rZMEadTc=; b=dDE32ECzEGoMeWZr12o4eM4UmxY2gsuuzlmkJmkF0N5xADzYipoARVFA+sg8UKA8es GVxssuRbFy7p0wucdvYwwooZKJxgXpzzLXFiVsW8+z7san+iY1pddTX4m66rVgyPfGFu Ua7yuO7vrzUoHpTTSgulVbLkkFwa63G/Dq1qtRN1/zVSFJ0v3rrV/qsY1IYxmdqgNYbP ObC4ANELNTIMezQ5RR09DvgxpeDg0mrcl2wo2hTMt8IM7nQJqvFZrWmuzjdHUWoVX7yi AApJXmN+8u8vHrqHKQo7OqLsCoyg+gNt2msduRPlbqyTrl4jvK17/SMk2KTnCK/sY53k /ujg== X-Forwarded-Encrypted: i=1; AJvYcCVXBKREApdHE59dflDNhN30zimpLifZB1IpsW5GYUxbA8tLgwT72btGe4G9eXbJ/lLcP65AfKsMiLElkKqGTaCfrOanuIVwXc2MHZQzO4mCkSIaKVw= X-Gm-Message-State: AOJu0YwKUX2oKsxQldqxTb45vwiJW8ukjIvrkO08tLAaW5HSnYtGqay1 DIy4H5lMTPieEhdVzJJADVPNQSl90EXBqCvzAcAE6QpJSlhmiRGHT3YO+tvY6oQvknqRgcwnm6N /iPAlWK4atPOJHPBFwA== X-Google-Smtp-Source: AGHT+IEiMSeqlL0E6QDriJWFXsS3O5CZdgADUq/m5UJK+f9Pzpz6qFNvBqMVhziD7REowA6fLBLwvMzZP6ooxfuZ X-Received: from jthoughton.c.googlers.com ([fda3:e722:ac3:cc00:14:4d90:c0a8:2a4f]) (user=jthoughton job=sendgmr) by 2002:a05:6902:2089:b0:e03:2bfe:40c9 with SMTP id 3f1490d57ef6-e041b032f48mr13750276.3.1720654967914; Wed, 10 Jul 2024 16:42:47 -0700 (PDT) Date: Wed, 10 Jul 2024 23:42:14 +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-11-jthoughton@google.com> Subject: [RFC PATCH 10/18] KVM: arm64: 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_164249_943139_904062DF X-CRM114-Status: GOOD ( 17.36 ) 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 Support comes in three parts: 1. When KVM Userfault is enabled, only install PAGE_SIZE PTEs. This prevents us from being able to map a userfault-enabled pfn with a huge PTE in response to a fault on a non-userfault pfn. 2. When we get KVM_PFN_ERR_USERFAULT from __gfn_to_pfn_memslot, return a memory fault to userspace. 3. When KVM Userfault is enabled for a particular kvm_gfn_range, unmap it, so that we can get faults on it. Signed-off-by: James Houghton --- arch/arm64/kvm/Kconfig | 1 + arch/arm64/kvm/mmu.c | 36 ++++++++++++++++++++++++++++++++++-- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/arch/arm64/kvm/Kconfig b/arch/arm64/kvm/Kconfig index 58f09370d17e..358153d91d58 100644 --- a/arch/arm64/kvm/Kconfig +++ b/arch/arm64/kvm/Kconfig @@ -37,6 +37,7 @@ menuconfig KVM select HAVE_KVM_VCPU_RUN_PID_CHANGE select SCHED_INFO select GUEST_PERF_EVENTS if PERF_EVENTS + select KVM_USERFAULT help Support hosting virtualized guest machines. diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c index 8bcab0cc3fe9..ac283e606516 100644 --- a/arch/arm64/kvm/mmu.c +++ b/arch/arm64/kvm/mmu.c @@ -1434,7 +1434,7 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, * logging_active is guaranteed to never be true for VM_PFNMAP * memslots. */ - if (logging_active) { + if (logging_active || kvm->userfault) { force_pte = true; vma_shift = PAGE_SHIFT; } else { @@ -1494,8 +1494,15 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, kvm_send_hwpoison_signal(hva, vma_shift); return 0; } - if (is_error_noslot_pfn(pfn)) + if (is_error_noslot_pfn(pfn)) { + if (pfn == KVM_PFN_ERR_USERFAULT) + kvm_prepare_memory_fault_exit(vcpu, gfn << PAGE_SHIFT, + PAGE_SIZE, write_fault, + /*exec=*/false, + /*private=*/false, + /*userfault=*/true); return -EFAULT; + } if (kvm_is_device_pfn(pfn)) { /* @@ -2105,3 +2112,28 @@ void kvm_toggle_cache(struct kvm_vcpu *vcpu, bool was_enabled) trace_kvm_toggle_cache(*vcpu_pc(vcpu), was_enabled, now_enabled); } + +#ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES +bool kvm_arch_pre_set_memory_attributes(struct kvm *kvm, + struct kvm_gfn_range *range) +{ + unsigned long attrs = range->arg.attributes; + + /* + * We only need to unmap if we're enabling userfault. Disabling it + * does not need an unmap. An unmap to get huge mappings will come + * later. + */ + if (attrs & KVM_MEMORY_ATTRIBUTE_USERFAULT) + kvm_unmap_gfn_range(kvm, range); + + return false; +} + +bool kvm_arch_post_set_memory_attributes(struct kvm *kvm, + struct kvm_gfn_range *range) +{ + /* Nothing to do! */ + return false; +} +#endif From patchwork Wed Jul 10 23:42:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Houghton X-Patchwork-Id: 13729881 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 338E8C3DA41 for ; Wed, 10 Jul 2024 23:46:20 +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=M0Yr0VzVLlHnBy7TTzGiqxJ6wuwWaRwmTmvFlX0dIWg=; b=2XHrZrNSpIs6j4VXyQBX3dYa58 Btk3W7+vzWmf/Sw7oobL9R4j03t6h/e8h5mssVvoZzFsZo4YCq470BOzBiQyihX9HnPn7K/BvknVY igUJ/UVIm4npCO9BKnBtK5aguJ0lL9GkossNlpuBHXr0wDm7U4F1QXQY+VouL8zRxSjpdAu6FN7e8 oV7C5fiGIsn9T2iT2UO1Oc+Kj3wj3ReYiI+limcZSGWRhjtv8HyfqFfHBuYv+yQuePvryg1L8HWaD Jucl6iA/Ub5ngxciTpRxgR8A6sYO4DPwxn/P/YTxyv/ohyQI8cW49OeOEWPrxlvNmc32+tWP6SgJ8 9f9vC6VA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sRh0l-0000000C92T-0dBg; Wed, 10 Jul 2024 23:46:11 +0000 Received: from mail-yb1-xb4a.google.com ([2607:f8b0:4864:20::b4a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sRgxW-0000000C7El-2oRY for linux-arm-kernel@lists.infradead.org; Wed, 10 Jul 2024 23:42:52 +0000 Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-e057ae54529so423094276.2 for ; Wed, 10 Jul 2024 16:42:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1720654969; x=1721259769; 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=M0Yr0VzVLlHnBy7TTzGiqxJ6wuwWaRwmTmvFlX0dIWg=; b=FV+m3BtIhe1fsmcn+VmmIo+UmYvfgP3qq/u55COxkx+CBqF6lSt+RGq4n4PL1uBIPs XD0asKhTXE9Db4u/vDkmreULV8fJYbj8utsR3sHqemaJ2Nxr7wt8KwukmPIF41yFSHCt YKL/6892sQL3vExJ/R22mkL9SB1pUtV9tNf24txpYVlxmVleAEOA8oELakR1yIXZdQi9 VXUdg9syF8jGk9G0Qddejz+tuTyD0OJEb5MnfP+rNI78DLBGNW/bgrBGP09cO/Onj8Fw laQif+W+Y1bvT0eP1/pcsS+6dtKqOrRETzYCVjzAlj44FLQUBH0fdsFE360LxdOL89NJ Pb9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720654969; x=1721259769; 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=M0Yr0VzVLlHnBy7TTzGiqxJ6wuwWaRwmTmvFlX0dIWg=; b=iUEerEEVxm2RB+nvZw4TRK5S1BRMqZoi3y4xtLBHKCHQv9eh+QABrUvgk78EVfI31s trLYlEUjKrP3yWhmquqBGsy8Jpy7sCXyD53GCUB8FhIs3AwCsBhFl8PMiQw3tMjL6Ip0 DtljlPazLeynt61p/GQUwW4ps/Y251Q91UzCC3jTpp67CI+s1xl1RtBHkUM++n7Reao/ NPqpLkP7ykK0SNXi6lvjmTsmSeH+/KtSXGIIQmKXuOfdz4J0GYnCHMrl7/b8PeOnpunu KUDg9zhweFfg3c2p9CX3dJLV69s1jAD7PKHvDZp4ikxP7iZG9I4MR5YxNCGc7Fh5JbJ4 TezQ== X-Forwarded-Encrypted: i=1; AJvYcCXHMKBBbzicf1unP8mTXYDJmZr9P0tPaK8Z3rHGcBFL3sC/edCNjAiI1cIn78LAz4edS8D2yi6VL5//0JJmD370NhM5tcwzFvhux7U6xLuGlO3QqOw= X-Gm-Message-State: AOJu0YziDu1F7z0s5KxerP/ku6I7N3nIYrBJHY8WebqErpOi79qlIaka s8jylYSvd8Kd5jcZf+zzPthm0g/c6ELQ5p6kckGqIJvxxlYqlF7JdNBRuYsLsZUo9NPxnwuuR/G ujGdGyN9R2mPWlq6c1g== X-Google-Smtp-Source: AGHT+IEnfc9JNyCNV09EBc2D+TZxXa8AOYJGFkqDSB6HeXipR+uC/GqUScItBcKVfj4YhKzC1lFukBy8dAilrvDh X-Received: from jthoughton.c.googlers.com ([fda3:e722:ac3:cc00:14:4d90:c0a8:2a4f]) (user=jthoughton job=sendgmr) by 2002:a05:6902:154c:b0:de5:2694:45ba with SMTP id 3f1490d57ef6-e041af3e29emr358942276.0.1720654968877; Wed, 10 Jul 2024 16:42:48 -0700 (PDT) Date: Wed, 10 Jul 2024 23:42:15 +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-12-jthoughton@google.com> Subject: [RFC PATCH 11/18] KVM: arm64: Add vCPU memory fault fast-path for Userfault 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_164250_890880_56E6C71C X-CRM114-Status: UNSURE ( 9.99 ) X-CRM114-Notice: Please train this message. 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 Make this optimization for the same reason we make it for x86: because it necessary for sufficient post-copy performance when scaling up to hundreds of cores (even though KVM Userfault today doesn't scale very well). Signed-off-by: James Houghton --- arch/arm64/kvm/mmu.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c index ac283e606516..c84633c9ab98 100644 --- a/arch/arm64/kvm/mmu.c +++ b/arch/arm64/kvm/mmu.c @@ -1488,6 +1488,15 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, mmu_seq = vcpu->kvm->mmu_invalidate_seq; mmap_read_unlock(current->mm); + if (gfn_has_userfault(memslot->kvm, gfn)) { + kvm_prepare_memory_fault_exit(vcpu, gfn << PAGE_SHIFT, + PAGE_SIZE, write_fault, + /*exec=*/false, + /*private=*/false, + /*userfault=*/true); + return -EFAULT; + } + pfn = __gfn_to_pfn_memslot(memslot, gfn, false, false, NULL, write_fault, &writable, NULL); if (pfn == KVM_PFN_ERR_HWPOISON) { From patchwork Wed Jul 10 23:42:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Houghton X-Patchwork-Id: 13729882 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 CDC63C3DA42 for ; Wed, 10 Jul 2024 23:46:38 +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=wAPg+uoGyE7jrnqbrfnqp+qBfKQe/ElheERXpCs/y30=; b=HUM7y9KBeIlw24pj01ESaXGrOB q5iFMt3G+uDQNJQSYli3RPxYgGM6wB3oreB+EHLCGea6PvV4xOBhjX0h7t1FBEiDW1MyZL7q6MJ4A zZ27oKKH8q5H32v//imTpNbgZDv3Og0+3h/kxqKqIbZF7hDpO/JJy2YqVViIak8aUlyeFxDsHC6hr sKpQE/KMskodur2npvlJkN2R8pYrNQHWcLKetc0NiVW98h18tjugnuqWmK1PsH4UJFPgPZtDR4e1G NwLyK+uvXgvXIuAMNhy9pikHQx4PvAasCL1lVPDF8Htr73d10wEQ3qmtTpke9xW4GLUUwhxPC/Dn6 lIoNhnsQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sRh10-0000000C9A1-41C4; Wed, 10 Jul 2024 23:46:26 +0000 Received: from mail-yw1-x114a.google.com ([2607:f8b0:4864:20::114a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sRgxW-0000000C7FJ-47xW for linux-arm-kernel@lists.infradead.org; Wed, 10 Jul 2024 23:42:54 +0000 Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-651e54bb41bso4323107b3.1 for ; Wed, 10 Jul 2024 16:42:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1720654970; x=1721259770; 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=wAPg+uoGyE7jrnqbrfnqp+qBfKQe/ElheERXpCs/y30=; b=fMepc5pBpQOaav4+GrlhFN5nPwuN66elxOsRso4KObIsJmvF+RDc5SYadThjR35j3r WPURhc3mrW94M9raCzHiPZ5j59REvOVqKT0N38Pe9V1WTjRhisVzlNWG1W51IS6cX3pW VJtFrVqQAPK9L5vteASgflJoWqdaXyG5UKD2p6n4CmL7RiHQJ0gSnEIuCgDlx64ppl61 VP8mc7gApjnEQT+QGefGEulFD2PcgyGr4ErRukvBUYJC5OXO97ZTeZcn/I716nJgxUoH Dh1lwF4Em4sF7JNrPW41TpczApE/gDO1g5o8+Q3jpaAgA7P0JvhMz7YEONAJ27KLgd1q 9Ylg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720654970; x=1721259770; 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=wAPg+uoGyE7jrnqbrfnqp+qBfKQe/ElheERXpCs/y30=; b=hy8zsgECN/EYpu1PC0WKx1hwFIzDzYs1kOReZGBWH1DfFvGGlbieUkD2UFz6GqJrPY PZMuMDRb35/fYRAqU69X33oHvrBUUnpVE2JIWns6rjFme61w8ahg6dTcUGuNZn9hqwo8 I53+m29Uo9hiZEYuOT/GwrYWegHYCq54HHsp6DgmNQz1DTjV1DEj1qE4cGLMuijqo2Dk LCnysTa+s6RgAce36uBL4XWx8QgurQU22FrZaodVlqRnr3jmxjlbdk1fpR+IATdDCk3w lC3C75yhUxuOWHO2jAqfHAi/7A3i19slsL+BI+1FR71ox8TtBnKGdX1z3TNvgBgQ6exh TYTA== X-Forwarded-Encrypted: i=1; AJvYcCVjkI6pBZOKNesIigh0eYrkdzkMujY9tAHXmsDmhlK1XaUCiYR5fj8oXozrsLpGRvknFjzYbO6iFyQbjO7ZFk2n9NPsr5gKrfrCq9Tjf+5/ZQKXZOA= X-Gm-Message-State: AOJu0Ywlzb296fX8aN/srvhNs/qFsyGAWTSGlTIkFVDsx4XGuoGt8G6R nh/TyyYegTL6+1ekiAEqAw2K8G5UGn7m0Rv5bKmXeMsYKmjVLV+yU18PfVx9TCrDq9tHZPb8cYQ YSix4iepkQxPoQeoVkQ== X-Google-Smtp-Source: AGHT+IEUTEcOd07Ngb0BqkggJw4ozgt7Za8itkm1dvA0VYqtERMoRdOJmQ0AmWvr6/7REaf9c4XX0hx8Il26mfc6 X-Received: from jthoughton.c.googlers.com ([fda3:e722:ac3:cc00:14:4d90:c0a8:2a4f]) (user=jthoughton job=sendgmr) by 2002:a05:6902:2101:b0:e03:31ec:8a24 with SMTP id 3f1490d57ef6-e041b17dfb5mr382800276.8.1720654969766; Wed, 10 Jul 2024 16:42:49 -0700 (PDT) Date: Wed, 10 Jul 2024 23:42:16 +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-13-jthoughton@google.com> Subject: [RFC PATCH 12/18] KVM: arm64: Add userfault support for steal-time 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_164251_149308_074EF465 X-CRM114-Status: GOOD ( 14.25 ) 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 As part of KVM_RUN, we may need to write steal-time information to guest memory. In the case that the gfn we are writing to is userfault-enabled, we should return to userspace with fault information. With asynchronous userfaults, this change is not necessary and merely acts as an optimization. Signed-off-by: James Houghton --- arch/arm64/include/asm/kvm_host.h | 2 +- arch/arm64/kvm/arm.c | 8 ++++++-- arch/arm64/kvm/pvtime.c | 11 +++++++++-- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h index 36b8e97bf49e..4c7bd72ba9e8 100644 --- a/arch/arm64/include/asm/kvm_host.h +++ b/arch/arm64/include/asm/kvm_host.h @@ -1166,7 +1166,7 @@ static inline bool kvm_arch_pmi_in_guest(struct kvm_vcpu *vcpu) long kvm_hypercall_pv_features(struct kvm_vcpu *vcpu); gpa_t kvm_init_stolen_time(struct kvm_vcpu *vcpu); -void kvm_update_stolen_time(struct kvm_vcpu *vcpu); +int kvm_update_stolen_time(struct kvm_vcpu *vcpu); bool kvm_arm_pvtime_supported(void); int kvm_arm_pvtime_set_attr(struct kvm_vcpu *vcpu, diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c index 59716789fe0f..4c7994e44217 100644 --- a/arch/arm64/kvm/arm.c +++ b/arch/arm64/kvm/arm.c @@ -974,8 +974,12 @@ static int check_vcpu_requests(struct kvm_vcpu *vcpu) */ kvm_check_request(KVM_REQ_IRQ_PENDING, vcpu); - if (kvm_check_request(KVM_REQ_RECORD_STEAL, vcpu)) - kvm_update_stolen_time(vcpu); + if (kvm_check_request(KVM_REQ_RECORD_STEAL, vcpu)) { + int ret = kvm_update_stolen_time(vcpu); + + if (ret <= 0) + return ret; + } if (kvm_check_request(KVM_REQ_RELOAD_GICv4, vcpu)) { /* The distributor enable bits were changed */ diff --git a/arch/arm64/kvm/pvtime.c b/arch/arm64/kvm/pvtime.c index 4ceabaa4c30b..ba0164726310 100644 --- a/arch/arm64/kvm/pvtime.c +++ b/arch/arm64/kvm/pvtime.c @@ -10,7 +10,7 @@ #include -void kvm_update_stolen_time(struct kvm_vcpu *vcpu) +int kvm_update_stolen_time(struct kvm_vcpu *vcpu) { struct kvm *kvm = vcpu->kvm; u64 base = vcpu->arch.steal.base; @@ -20,9 +20,14 @@ void kvm_update_stolen_time(struct kvm_vcpu *vcpu) int idx; if (base == INVALID_GPA) - return; + return 1; idx = srcu_read_lock(&kvm->srcu); + if (gfn_to_hva(kvm, base + offset) == KVM_HVA_ERR_USERFAULT) { + kvm_prepare_memory_fault_exit(vcpu, base + offset, PAGE_SIZE, + true, false, false, true); + return -EFAULT; + } if (!kvm_get_guest(kvm, base + offset, steal)) { steal = le64_to_cpu(steal); vcpu->arch.steal.last_steal = READ_ONCE(current->sched_info.run_delay); @@ -30,6 +35,8 @@ void kvm_update_stolen_time(struct kvm_vcpu *vcpu) kvm_put_guest(kvm, base + offset, cpu_to_le64(steal)); } srcu_read_unlock(&kvm->srcu, idx); + + return 1; } long kvm_hypercall_pv_features(struct kvm_vcpu *vcpu) From patchwork Wed Jul 10 23:42:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Houghton X-Patchwork-Id: 13729883 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 9550BC3DA42 for ; Wed, 10 Jul 2024 23:46:52 +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=QcajhSgtnKDY4bKjbf4ZVWmvqiJNI58ACmEJvGd9BEA=; b=v/j4boyqLrQkSHhCJV2G9nVHs0 vIUr5XI4wpmFq6yN4dQvsyiItRnzd3eLN3GSv+p4LnlV1mnUdirQp7M+uzu3tpc4UyTIDd8YoPm0U xz2UKNj0uAEKDbTU8ZnTpzGhpTSj6TA/Ax0uoyfyhqILgiCzYzeucuq4sAYSWq+SkImujuRsgWX86 545o/kz+V5rB/VfI+4eUsMVameakdeOIe5y4YQJHj+FRcWNBseom41UnAfb1OjxlSXqPsZzF4RoVT 0GB3rtkGMizfXY/UddgJ56i7xUsaKL74N75zEmstcgnbZXsayksBOEjY7OVrxblr44CguBXsBkjaq YB65rMig==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sRh1G-0000000C9HP-225x; Wed, 10 Jul 2024 23:46:42 +0000 Received: from mail-yw1-x114a.google.com ([2607:f8b0:4864:20::114a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sRgxY-0000000C7Gs-2Z7S for linux-arm-kernel@lists.infradead.org; Wed, 10 Jul 2024 23:42:55 +0000 Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-653b4b01872so5683677b3.1 for ; Wed, 10 Jul 2024 16:42:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1720654971; x=1721259771; 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=QcajhSgtnKDY4bKjbf4ZVWmvqiJNI58ACmEJvGd9BEA=; b=bvss2ujwTiIlWFczO5c8BDqSBc9mjCQ2a3ZbHCyaIbey3Hbz+zi3JkejcCcDIg34zE erzyvMZZiCXAC+bltFUg9Y+5fPkBzpMwtRRgCXpWwSWMJcDl7z7hQyDUB385qppWVXpN tG3ZM6gvO/H8sBWCg0auBseM8LKxUJk8TfUixvPufVw0idJsbLGJlvRENza6XNPmX+4R K0BMDvAb8FOGJLHf87Bh9+nL7X2oJSUljHrGqK/4xzV28/0O9B8pFyImIbbh/eok0UBA q92vec6nDibNN4o5C8c5VENBiBV7YAgwJsL7tzKk2FUzDj9qlaPZNw8czagGh+/rYNFA ufpw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720654971; x=1721259771; 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=QcajhSgtnKDY4bKjbf4ZVWmvqiJNI58ACmEJvGd9BEA=; b=Y7cCH5u0oHXeYWa4OG/1CQFDYNaHyCb+IR1XgqtmJmqERzbX/gxLRA1AbvM0GXz177 t17WDRo0GPFs3CaDIdOHZXU9hl/e1wS0ORB9yAXQ3XKbk9z8HOR/trpvF8Me6EJnWa7n hUtcC1tzuVxGZvvWOBNou6vwvVlUhuXFHYfV06Zee00EV4Vn9KuJ36VhPTEUjIEHRZcL 2ZMyK6x4yBLu2MSx/fbd2MX0PV/GKHmGBWlVUc1Cw4d4ZXG4Aiy21/PMZZ46CD66YHNE N7OA6K1F/Co8GkAh5KFyiAAKI3Z3fS7T+10rIYatxzplYHH1cPlGbQWA2ZD/1fzam7w8 j39A== X-Forwarded-Encrypted: i=1; AJvYcCU7GpG22cmrLmE6ReOduVg4tZSx2bwmeIseH7RhEU2RhUYAbh18vpg4uMrdPmhADvIAfyTAR4519Zc4G1V4aIAabTIIIVQphU7wwKGl91Px2W0JiAM= X-Gm-Message-State: AOJu0YxGXQKOjJpKzZJY54+v2+fKSUckkLQNR0Oa5iL8YanIdC+sXrQl iCF4ybS9AlN75pM6KLcDb16IYOe7gbgjkzGSwGUgzQK8JJzjiGjtdOKYMcL340yjjoVAYQPZ9ST x9pWuZl7khhujdYGyYQ== X-Google-Smtp-Source: AGHT+IEC/EQtrNezpt+9blgHEpHxd1YhFnUWh0/2vBf3Usnz5Irc0nFTjIjjYgCU/UmvBPArza1QUIUaWp5gpwOj X-Received: from jthoughton.c.googlers.com ([fda3:e722:ac3:cc00:14:4d90:c0a8:2a4f]) (user=jthoughton job=sendgmr) by 2002:a05:690c:6d0d:b0:61c:89a4:dd5f with SMTP id 00721157ae682-658ebabc90amr1126767b3.0.1720654970940; Wed, 10 Jul 2024 16:42:50 -0700 (PDT) Date: Wed, 10 Jul 2024 23:42:17 +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-14-jthoughton@google.com> Subject: [RFC PATCH 13/18] KVM: Add atomic parameter to __gfn_to_hva_many 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_164252_792361_CC9675C7 X-CRM114-Status: GOOD ( 11.25 ) 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 Atomic translations cannot wait for userspace to resolve a userfault. Add an atomic parameter for later use with KVM Userfault. Signed-off-by: James Houghton --- virt/kvm/kvm_main.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 77eb9f0de02d..4ac018cac704 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -2679,7 +2679,7 @@ static bool memslot_is_readonly(const struct kvm_memory_slot *slot) } static unsigned long __gfn_to_hva_many(const struct kvm_memory_slot *slot, gfn_t gfn, - gfn_t *nr_pages, bool write) + gfn_t *nr_pages, bool write, bool atomic) { if (!slot || slot->flags & KVM_MEMSLOT_INVALID) return KVM_HVA_ERR_BAD; @@ -2699,7 +2699,7 @@ static unsigned long __gfn_to_hva_many(const struct kvm_memory_slot *slot, gfn_t static unsigned long gfn_to_hva_many(struct kvm_memory_slot *slot, gfn_t gfn, gfn_t *nr_pages) { - return __gfn_to_hva_many(slot, gfn, nr_pages, true); + return __gfn_to_hva_many(slot, gfn, nr_pages, true, false); } unsigned long gfn_to_hva_memslot(struct kvm_memory_slot *slot, @@ -2732,7 +2732,7 @@ EXPORT_SYMBOL_GPL(kvm_vcpu_gfn_to_hva); unsigned long gfn_to_hva_memslot_prot(struct kvm_memory_slot *slot, gfn_t gfn, bool *writable) { - unsigned long hva = __gfn_to_hva_many(slot, gfn, NULL, false); + unsigned long hva = __gfn_to_hva_many(slot, gfn, NULL, false, false); if (!kvm_is_error_hva(hva) && writable) *writable = !memslot_is_readonly(slot); @@ -3002,7 +3002,8 @@ kvm_pfn_t __gfn_to_pfn_memslot(const struct kvm_memory_slot *slot, gfn_t gfn, bool atomic, bool interruptible, bool *async, bool write_fault, bool *writable, hva_t *hva) { - unsigned long addr = __gfn_to_hva_many(slot, gfn, NULL, write_fault); + unsigned long addr = __gfn_to_hva_many(slot, gfn, NULL, write_fault, + atomic); if (hva) *hva = addr; @@ -3074,7 +3075,7 @@ int gfn_to_page_many_atomic(struct kvm_memory_slot *slot, gfn_t gfn, unsigned long addr; gfn_t entry = 0; - addr = gfn_to_hva_many(slot, gfn, &entry); + addr = __gfn_to_hva_many(slot, gfn, &entry, true, true); if (kvm_is_error_hva(addr)) return -1; From patchwork Wed Jul 10 23:42:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Houghton X-Patchwork-Id: 13729884 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 95E51C3DA47 for ; Wed, 10 Jul 2024 23:47:13 +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=jKPHohqFL0IhECSvPRQXiNGrzNnrPvfhaSYPnkkePdA=; b=sGutVJKd7woUzPka6LX9agZC9I UoSN9NSoGMqpR4bo8Qj1pD6KwV8sif+xzYmFkAG7U1h1quzUR7JrOZPlRO3EV2pfmTl7z4QZcpBXl LFWlPPFDlI2MrHjjOlxYjDjQVtEwlTlu8q325Ik1Y53BDmnYIhVn/zSOytSmjdoZc7HiftQ8yQfHS kt6/8oIgok+EC4E3zcdNiE9nvv+/nvNKTSy1lAe7rmljJz7vejBn8rr0ap5tTQJ6e1MeqRJN+45uD /m/t2yM3xcFjbIyDgFREyDIgfaquroUTP2zUAMQRZEUAjksVEoTjtlJvaRyeOi69ADk9a7Ff1h3je 8StHpxaw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sRh1a-0000000C9WC-1XJF; Wed, 10 Jul 2024 23:47:02 +0000 Received: from mail-ua1-x949.google.com ([2607:f8b0:4864:20::949]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sRgxZ-0000000C7HN-2YpT for linux-arm-kernel@lists.infradead.org; Wed, 10 Jul 2024 23:42:57 +0000 Received: by mail-ua1-x949.google.com with SMTP id a1e0cc1a2514c-810558cc76bso232285241.0 for ; Wed, 10 Jul 2024 16:42:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1720654972; x=1721259772; 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=jKPHohqFL0IhECSvPRQXiNGrzNnrPvfhaSYPnkkePdA=; b=m0Z+uc0TcFZJ8PXgEBWGxoTT++aRNhD+vFmCI7xWJve8v56B8uX4zS3oHkESJDkH0S gIEA3H+xpq5k5Cu3Nluqcr5EeeGDpMRbG7Y94pFdfYVKPbq28L8644Tjp2QgmflkqyvK wiUuwB8Jcj0uLWlR6PKg2E1q2EXu/j5r9sbEeie9ZQ9MSKS4CdJlU4F0OtvvbzKAzy/b 4vQFSBySVXkUOPXaQJOgA2fIy/S1WSZ+qQvfVKcV6h9SoHWx2RxawaglleC/+c4wu94E lN1vegNS5uHmWl0bWs5RKTfWjfgcqjLg7s1eRBCts5TA+C1cHt2qMtoCIjQkv9fP0CCm wIvw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720654972; x=1721259772; 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=jKPHohqFL0IhECSvPRQXiNGrzNnrPvfhaSYPnkkePdA=; b=ig+TQvjikPOIjUkzWEKWnRQaSwkT+3LrFogyQSLJZOahL6ddU3CXB/1Ag40sPwTp5Q cVkRS5k+oOI0KFbwtIP0ADOk1WuC7puQWXdwI3YbjmUrX4MkvU+xGG2m2c0/L2i37N6u 34GxaNhnqarKSvsC/Hk+wnJLvl+vj8CZd4ZS87bTgMAOA1NIikTNYBvPRdE47HkXnXpV VjfMiVkue81kfRvYFRbpwt5XBMf6sBsMVTZbnJRA6j4foR4Q2I3H0NpISMjqR76l5go3 8vQ/U9+jtQ2LQnxciM1uPSUfKeMYN/RS/xm1rztXYmBjNlsJV+L6q7Fc6QUcuQ1LFvZf g3uw== X-Forwarded-Encrypted: i=1; AJvYcCVq4mEi46lbyXoZUr+pkF1vCEGnhO0Sh0zzo0JNKR5GU1ptEAYanvDSYGaC4RNoOla6NnPhVMgAFjE3RztlYb3pXpLM0zsQzTo8RM6EEVUNCB/J8uo= X-Gm-Message-State: AOJu0Yx5D0JZkufQTGXBBfCg0iZWZ6iqh2fMWzg5YvKLJPp5pS4KKlRQ m4aGnP8meu2nEn1dhvWqIqnrz6BGtQ+s52uYPH1deWoLgZWNpOvAIPZW4OClmneeF2S+Dj0HqAi JwZt0NNk5voYHv6J76g== X-Google-Smtp-Source: AGHT+IHgRj71HIHo+Vr3YRAiQ2me1afxQxUXle9EqleBoMEZbhkar+2O7/FONbR7df58PgwPGZmAiebsYvwdmxCn X-Received: from jthoughton.c.googlers.com ([fda3:e722:ac3:cc00:14:4d90:c0a8:2a4f]) (user=jthoughton job=sendgmr) by 2002:a05:6130:288d:b0:80f:b1c4:fcef with SMTP id a1e0cc1a2514c-81185662fe2mr17887241.1.1720654971766; Wed, 10 Jul 2024 16:42:51 -0700 (PDT) Date: Wed, 10 Jul 2024 23:42:18 +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-15-jthoughton@google.com> Subject: [RFC PATCH 14/18] KVM: Add asynchronous userfaults, KVM_READ_USERFAULT 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_164253_868709_D8AE5C06 X-CRM114-Status: GOOD ( 21.07 ) 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 It is possible that KVM wants to access a userfault-enabled GFN in a path where it is difficult to return out to userspace with the fault information. For these cases, add a mechanism for KVM to wait for a GFN to not be userfault-enabled. The mechanism introduced in this patch uses an eventfd to signal that a userfault is ready to be read. Userspace then reads the userfault with KVM_READ_USERFAULT. The fault itself is stored in a list, and KVM will busy-wait for the gfn to not be userfault-enabled. The implementation of this mechanism is certain to change before KVM Userfault could possibly be merged. Really the main concerns are whether or not this kind of asynchronous userfault system is required and if the UAPI for reading faults works. Signed-off-by: James Houghton --- include/linux/kvm_host.h | 7 +++ include/uapi/linux/kvm.h | 7 +++ virt/kvm/kvm_main.c | 92 +++++++++++++++++++++++++++++++++++++++- 3 files changed, 104 insertions(+), 2 deletions(-) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index dc12d0a5498b..3b9780d85877 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -734,8 +734,15 @@ struct kvm_memslots { int node_idx; }; +struct kvm_userfault_list_entry { + struct list_head list; + gfn_t gfn; +}; + struct kvm_userfault_ctx { struct eventfd_ctx *ev_fd; + spinlock_t list_lock; + struct list_head gfn_list; }; struct kvm { diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 6aa99b4587c6..8cd8e08f11e1 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -1554,4 +1554,11 @@ struct kvm_create_guest_memfd { #define KVM_USERFAULT_ENABLE (1ULL << 0) #define KVM_USERFAULT_DISABLE (1ULL << 1) +struct kvm_fault { + __u64 address; + /* TODO: reserved fields */ +}; + +#define KVM_READ_USERFAULT _IOR(KVMIO, 0xd5, struct kvm_fault) + #endif /* __LINUX_KVM_H */ diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 4ac018cac704..d2ca16ddcaa1 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -2678,6 +2678,43 @@ static bool memslot_is_readonly(const struct kvm_memory_slot *slot) return slot->flags & KVM_MEM_READONLY; } +static int read_userfault(struct kvm_userfault_ctx __rcu *ctx, gfn_t *gfn) +{ + struct kvm_userfault_list_entry *entry; + + spin_lock(&ctx->list_lock); + + entry = list_first_entry_or_null(&ctx->gfn_list, + struct kvm_userfault_list_entry, + list); + + list_del(&entry->list); + + spin_unlock(&ctx->list_lock); + + if (!entry) + return -ENOENT; + + *gfn = entry->gfn; + return 0; +} + +static void signal_userfault(struct kvm *kvm, gfn_t gfn) +{ + struct kvm_userfault_ctx __rcu *ctx = + srcu_dereference(kvm->userfault_ctx, &kvm->srcu); + struct kvm_userfault_list_entry entry; + + entry.gfn = gfn; + INIT_LIST_HEAD(&entry.list); + + spin_lock(&ctx->list_lock); + list_add(&entry.list, &ctx->gfn_list); + spin_unlock(&ctx->list_lock); + + eventfd_signal(ctx->ev_fd); +} + static unsigned long __gfn_to_hva_many(const struct kvm_memory_slot *slot, gfn_t gfn, gfn_t *nr_pages, bool write, bool atomic) { @@ -2687,8 +2724,14 @@ static unsigned long __gfn_to_hva_many(const struct kvm_memory_slot *slot, gfn_t if (memslot_is_readonly(slot) && write) return KVM_HVA_ERR_RO_BAD; - if (gfn_has_userfault(slot->kvm, gfn)) - return KVM_HVA_ERR_USERFAULT; + if (gfn_has_userfault(slot->kvm, gfn)) { + if (atomic) + return KVM_HVA_ERR_USERFAULT; + signal_userfault(slot->kvm, gfn); + while (gfn_has_userfault(slot->kvm, gfn)) + /* TODO: don't busy-wait */ + cpu_relax(); + } if (nr_pages) *nr_pages = slot->npages - (gfn - slot->base_gfn); @@ -5009,6 +5052,10 @@ static int kvm_enable_userfault(struct kvm *kvm, int event_fd) } ret = 0; + + INIT_LIST_HEAD(&userfault_ctx->gfn_list); + spin_lock_init(&userfault_ctx->list_lock); + userfault_ctx->ev_fd = ev_fd; rcu_assign_pointer(kvm->userfault_ctx, userfault_ctx); @@ -5037,6 +5084,27 @@ static int kvm_vm_ioctl_enable_userfault(struct kvm *kvm, int options, else return kvm_disable_userfault(kvm); } + +static int kvm_vm_ioctl_read_userfault(struct kvm *kvm, gfn_t *gfn) +{ + int ret; + int idx; + struct kvm_userfault_ctx __rcu *ctx; + + idx = srcu_read_lock(&kvm->srcu); + + ctx = srcu_dereference(kvm->userfault_ctx, &kvm->srcu); + + ret = -ENOENT; + if (!ctx) + goto out; + + ret = read_userfault(ctx, gfn); + +out: + srcu_read_unlock(&kvm->srcu, idx); + return ret; +} #endif static int kvm_vm_ioctl_enable_cap_generic(struct kvm *kvm, @@ -5403,6 +5471,26 @@ static long kvm_vm_ioctl(struct file *filp, r = kvm_gmem_create(kvm, &guest_memfd); break; } +#endif +#ifdef CONFIG_KVM_USERFAULT + case KVM_READ_USERFAULT: { + struct kvm_fault fault; + gfn_t gfn; + + r = kvm_vm_ioctl_read_userfault(kvm, &gfn); + if (r) + goto out; + + fault.address = gfn; + + /* TODO: if this fails, this gfn is lost. */ + r = -EFAULT; + if (copy_to_user(&fault, argp, sizeof(fault))) + goto out; + + r = 0; + break; + } #endif default: r = kvm_arch_vm_ioctl(filp, ioctl, arg); From patchwork Wed Jul 10 23:42:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Houghton X-Patchwork-Id: 13729885 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 01C48C3DA41 for ; Wed, 10 Jul 2024 23:47:27 +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=86wUnUhZHhvP2m3KtD2mzNoNsfKZM3y2BVe++0uTyFA=; b=k71sbKq5HB0biR5SpHsQ3PqNo3 6BJFAKqgknHLikVy9CD59NQ4wXSfQuSPmAlmIy55G+7GkGGlMdVjGN1G26qoR0qoDVg3NzQMIipEW 0SkNQoCcekEOpehhxaYrN0DoTtzgnomfWPaoBwceB6zjMRjTA/Lke6UB8kvHf+94dcyDSpKjnXE5S Cgcfs/SdvQt8fWPyCpPAHGaymZUkh84Tha0syj0krsYyMMbW0yZKgefTm/jbv6zZR5DWqB+EMNJI3 LAP5F5L1+JMDQArsa63J0KZIAtTfSSKKK4r/gr8ZhcAZILOWcwLjO9/lyYYqvBVdzHspCVZbUv4av B+vabVfg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sRh1q-0000000C9gR-2ivW; Wed, 10 Jul 2024 23:47:18 +0000 Received: from mail-ua1-x949.google.com ([2607:f8b0:4864:20::949]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sRgxa-0000000C7Iy-1nMd for linux-arm-kernel@lists.infradead.org; Wed, 10 Jul 2024 23:42:58 +0000 Received: by mail-ua1-x949.google.com with SMTP id a1e0cc1a2514c-8107b03e1ccso56383241.1 for ; Wed, 10 Jul 2024 16:42:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1720654973; x=1721259773; 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=86wUnUhZHhvP2m3KtD2mzNoNsfKZM3y2BVe++0uTyFA=; b=KlTvuru78aMLPgtK/IvhEKU2YY0cSv+8M/4zQwBbAUD6FLgpHcxG3zYLQ0pqtz+vIi jTOukui8IfTdvis2eqYCvizecfJqYt3CtaKS1b79HmhAKg1BcWsSoU/Y+bc2miMixAwJ hH66jh0PfxN+afEbBgh/fbmJyVrpOQwB+uwRC2boLl79cRMWd78b+dOvrnwA1+O20GhI NhlN4TfiDhYzu6P70I7XHx5MVfS+D+cBE+kZk6gxAXZ7PYuq4LRZbhKHtIc7YSlHbbc+ eARHjVqd/sG0MYp3noTa0uInoAbMGaods4W8/nCHMU+l7oT1zMlSwvVYWeSYM6klNYCL 7kKg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720654973; x=1721259773; 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=86wUnUhZHhvP2m3KtD2mzNoNsfKZM3y2BVe++0uTyFA=; b=p0iqtJwNdioItPF3ZcjSjDArjIe4YFUZMUicgCK00Q3h766KdZTxltxP4t6OGIT9YB 8ZyXNw2Tbc25IXXNwO1SsKYpeBPD1BQWnusLnHr3Y9z2boIe/IDK3GLlf2oGGSj6oaCK OOlQo2nmB+DEDPpQ7SR4KbXLarCto51ROIv0z+iDU4u6PMBAlVy6Lz4q0+hjmYvu5Uhh JP13m5kAtRHXsWR3fckCR2tm0qGKloy0ongmTFrfkXXeDKZ9cuJ4QsTIZqUNyLkIVZN9 ncsazyTqTxY+SPnPqdt2KAbOgzU078b1lP2a/snD3oKlpFVqpstpy09RVNNYPts7uZrm +9GA== X-Forwarded-Encrypted: i=1; AJvYcCUkCUsgaE2X7bFgRYCqKvDTUxHhXk1MNyx0imb9iw56bVtC8Vv01gd4xwsdOvHdQJbBqyq4SbsEfXJGy2DePQjaas50ufWQWQzygjvNCEuC4wbn4/s= X-Gm-Message-State: AOJu0YytVvEquLa50PFE144WK+lFAlBdiwfVMz8Wx7Ev0KorCDpg1DQH ZKU8QXIMsPJMkYgahugoxT5zPRr22d2FczE3BkXvbWQCzsOlVWdbHHZyCxUxjFGsf5Td785SiXe VOLPax3bvUl+Ke4wVwA== X-Google-Smtp-Source: AGHT+IFuuD8TX5cMTXqfbBITU1Rk5gbgp8/gjNqtfzjUDzjMtEhdHehLKT0HBMd1mO2mStlwTdwfc0NYjYZ51/Zs X-Received: from jthoughton.c.googlers.com ([fda3:e722:ac3:cc00:14:4d90:c0a8:2a4f]) (user=jthoughton job=sendgmr) by 2002:a05:6130:2c07:b0:80f:e89a:dc21 with SMTP id a1e0cc1a2514c-81076f04ee0mr30519241.2.1720654972976; Wed, 10 Jul 2024 16:42:52 -0700 (PDT) Date: Wed, 10 Jul 2024 23:42:19 +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-16-jthoughton@google.com> Subject: [RFC PATCH 15/18] KVM: guest_memfd: 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_164254_710092_BB847FF6 X-CRM114-Status: GOOD ( 16.52 ) 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 We now have to pass our struct kvm into __kvm_gmem_get_pfn to know if a gfn is userfault-enabled or not. For faults on userfault-enabled gfns, indicate this to the caller by setting *pfn to KVM_PFN_ERR_USERFAULT. Architectures may use this to know to return a userfault to userspace, though they should be careful to set a value for *pfn before calling (e.g. KVM_PFN_ERR_FAULT). While we're at it, set *pfn to KVM_PFN_ERR_HWPOISON for accesses to poisoned gfns. Signed-off-by: James Houghton --- virt/kvm/guest_memfd.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c index 9148b9679bb1..ba7a981e3396 100644 --- a/virt/kvm/guest_memfd.c +++ b/virt/kvm/guest_memfd.c @@ -542,8 +542,9 @@ void kvm_gmem_unbind(struct kvm_memory_slot *slot) fput(file); } -static int __kvm_gmem_get_pfn(struct file *file, struct kvm_memory_slot *slot, - gfn_t gfn, kvm_pfn_t *pfn, int *max_order, bool prepare) +static int __kvm_gmem_get_pfn(struct kvm *kvm, struct file *file, + struct kvm_memory_slot *slot, gfn_t gfn, kvm_pfn_t *pfn, + int *max_order, bool prepare) { pgoff_t index = gfn - slot->base_gfn + slot->gmem.pgoff; struct kvm_gmem *gmem = file->private_data; @@ -551,6 +552,11 @@ static int __kvm_gmem_get_pfn(struct file *file, struct kvm_memory_slot *slot, struct page *page; int r; + if (gfn_has_userfault(kvm, gfn)) { + *pfn = KVM_PFN_ERR_USERFAULT; + return -EFAULT; + } + if (file != slot->gmem.file) { WARN_ON_ONCE(slot->gmem.file); return -EFAULT; @@ -567,6 +573,7 @@ static int __kvm_gmem_get_pfn(struct file *file, struct kvm_memory_slot *slot, return PTR_ERR(folio); if (folio_test_hwpoison(folio)) { + *pfn = KVM_PFN_ERR_HWPOISON; folio_unlock(folio); folio_put(folio); return -EHWPOISON; @@ -594,7 +601,7 @@ int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, if (!file) return -EFAULT; - r = __kvm_gmem_get_pfn(file, slot, gfn, pfn, max_order, true); + r = __kvm_gmem_get_pfn(kvm, file, slot, gfn, pfn, max_order, true); fput(file); return r; } @@ -634,7 +641,8 @@ long kvm_gmem_populate(struct kvm *kvm, gfn_t start_gfn, void __user *src, long break; } - ret = __kvm_gmem_get_pfn(file, slot, gfn, &pfn, &max_order, false); + ret = __kvm_gmem_get_pfn(kvm, file, slot, gfn, &pfn, + &max_order, false); if (ret) break; From patchwork Wed Jul 10 23:42:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Houghton X-Patchwork-Id: 13729886 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 1B825C3DA42 for ; Wed, 10 Jul 2024 23:47:48 +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=KrWHeRDFVlfzCLm2guf0VTYM9h/W1rUjUnfwvHRBaDY=; b=Sjr/ibO2m5U2prlTRs0dgopedv 9d/ZaFMyYBGxpwnSNBr5ph+FDb0KW49H38IOTi1TBb3GteEl0dmQGa+eExOi63twOcWl3Zc3I19ez 9FyxG9N/HamCoOYids3frXWtKxIDTteGkJ2eyhmMq01c2YCYH11fjizNhApTwRlZh1dkV4Proljwq epfCwGO3/Eii9MNwXRNf8wYrGKo9V84eyRLEM+BIpZvylFyUphzZs5DpXFOXSoecqu5XWPOu9CLeh +1T21PUSZs2kU0EHj5xKyHsse9n2+vp3oUqlrbksjer5k1+QCz6xB1cVsIhhvIznW6yUGsvOeLtwe B8EsZQFw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sRh27-0000000C9p1-2FG1; Wed, 10 Jul 2024 23:47:35 +0000 Received: from mail-vs1-xe4a.google.com ([2607:f8b0:4864:20::e4a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sRgxb-0000000C7Jk-1EP2 for linux-arm-kernel@lists.infradead.org; Wed, 10 Jul 2024 23:42:58 +0000 Received: by mail-vs1-xe4a.google.com with SMTP id ada2fe7eead31-48ff6c5d2e3so81461137.1 for ; Wed, 10 Jul 2024 16:42:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1720654974; x=1721259774; 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=KrWHeRDFVlfzCLm2guf0VTYM9h/W1rUjUnfwvHRBaDY=; b=p4lvRezLKxNvotvUfBmcPx7KeDMPBP1qgY82iZrNCX7jlfLRrpY7Kjva23ljHy0JDv 066lwoxjbphxxjvXhUKgIQ24X35aY3RQVoe0jMQnBis694rNg+ElzWGfbduJYnl9EnRj 0/jefKN7zsQPgFN+qaAAoYcWdDZeozyzV78TyqYTiyt+vals0lX/lEGO4gPOpGQRhkGV x79m6OGZOyZbZqoxNjrHdTi3kfyCzIFTpA7817kRDMRo4e9wis2WGBqo2Dto1yA5ucJu 86VKeJ41IZ6W1nsMmBqdqUzlWKXwNiYOBCEPViozWAPyb1QtrtRgXWhzg1jZRlD+YF4k x9DA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720654974; x=1721259774; 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=KrWHeRDFVlfzCLm2guf0VTYM9h/W1rUjUnfwvHRBaDY=; b=wbLoYwgZhkAGgrGGT65s7KPsVPiM+yKR1nF7Mmr8VIScx8YsdFmRvYGMQiYiN9Jr86 yyefGoE41X9Akz7msTulzB12YDqvvdjJ0j7rNyg8GRHzPEo3SQ6E1llNA00Hx6tKio0l JW00Xv4R7ePr5l+HFdKx8bhLlXLm6ayshLfGlfxcb+v9Cws6AEuPFZ3eDpacdOFGhkoG RbKJuKB4soHSJwXjF3Gvf7sWJBu8oRJL8doUfjS/pdohSU18ZaVbqPxizQPJK/0scXh1 UiZ9kRC7Dk+sk3WG1ZccCNt2gRoK2lS3uJHdIq7WQE5rgkpJZ3c4lG9pD/QpWDAQn/Ss CukQ== X-Forwarded-Encrypted: i=1; AJvYcCVDMkJhe1QL8aEg27hVsht146WrvE+puY//8mJvQeEsuBl5NBkM977l01NVLchkB4A3euN9Lguo2M7U2NAo8XLVvWB8QO7RAS7OtdsZnl/01RnyVkU= X-Gm-Message-State: AOJu0Yy4IBUfeBPY1BF3sBAQ1mz5lCO6fkjJk9pGP2uD5ePO+LCvh17L U20D3+IWE9dyr9FXM1rWs8M1ZjoepBwfHi2tJtZJcKgyY6jsDQKfZiP2dRoEk5GsRd78S0YQVVf dMkhX4WjEuV/7vfvNeQ== X-Google-Smtp-Source: AGHT+IEk1FL65vqiK6UE5KirHXI/3mEOw3BUqoQ0HFsCZaEXrmaSc1CqWM95u18pik8VbBJeKyVeyhW9FRI1OD+8 X-Received: from jthoughton.c.googlers.com ([fda3:e722:ac3:cc00:14:4d90:c0a8:2a4f]) (user=jthoughton job=sendgmr) by 2002:a05:6102:5490:b0:48d:89b6:85d2 with SMTP id ada2fe7eead31-49031dbea2dmr435474137.0.1720654973979; Wed, 10 Jul 2024 16:42:53 -0700 (PDT) Date: Wed, 10 Jul 2024 23:42:20 +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-17-jthoughton@google.com> Subject: [RFC PATCH 16/18] KVM: Advertise KVM_CAP_USERFAULT in KVM_CHECK_EXTENSION 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_164255_448567_C90ADA89 X-CRM114-Status: UNSURE ( 9.54 ) X-CRM114-Notice: Please train this message. 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 Advertise support for KVM_CAP_USERFAULT when CONFIG_KVM_USERFAULT is enabled. Signed-off-by: James Houghton --- virt/kvm/kvm_main.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index d2ca16ddcaa1..90ce6b8ff0ab 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -4916,6 +4916,10 @@ static int kvm_vm_ioctl_check_extension_generic(struct kvm *kvm, long arg) #ifdef CONFIG_KVM_PRIVATE_MEM case KVM_CAP_GUEST_MEMFD: return !kvm || kvm_arch_has_private_mem(kvm); +#endif +#ifdef CONFIG_KVM_USERFAULT + case KVM_CAP_USERFAULT: + return 1; #endif default: break; From patchwork Wed Jul 10 23:42:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Houghton X-Patchwork-Id: 13729887 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 B5F8AC3DA42 for ; Wed, 10 Jul 2024 23:48:02 +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=23lW1dYHwRXqrRIzh0tt4ShqtRWFD2EoChbCYcPjw+M=; b=LR348izzv6qM081rqf33TTzzyZ 71AtLye6IMsjjOMwhuGxlD1Beox8sjnbcs98CIqwqZy6COIi3MQFGW35RaMaj0A+1soLUgvCJWCUu d8eb6DTviaBO/X6NVHbWLoO5BrkUGfOO8rDO1XWpsRgZNpKamLIQrasYq6bWIltev4n11xQqyc3bf GklunhCeyi0xX3iYAqb/pfJEeDleRIR20Z94O3nVWMUxoOO1p3Fx7JDfIDoDrEe+jwUlYiQpMNDrz x447x8WXDtlF8eB0UZEwv9fWAa8JXpEZzKPed85MERF2zbpkCYVBcoRmoDz5lX9+9PVsPqe0c/8mw /9wlFR9w==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sRh2P-0000000CA02-1oz1; Wed, 10 Jul 2024 23:47:53 +0000 Received: from mail-yb1-xb4a.google.com ([2607:f8b0:4864:20::b4a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sRgxb-0000000C7KJ-402c for linux-arm-kernel@lists.infradead.org; Wed, 10 Jul 2024 23:42:59 +0000 Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-e035949cc4eso539166276.1 for ; Wed, 10 Jul 2024 16:42:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1720654975; x=1721259775; 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=23lW1dYHwRXqrRIzh0tt4ShqtRWFD2EoChbCYcPjw+M=; b=udtYkoF5pyJ5jsWcVt/tus//LPDp1ZWKbGAQwk/sauuWhl63zMnFEtbRVjLUZ04aOA U0HmhNAuBi5RyBIjbISdC2fbYogYfxSZaMvAg3xxwDoRj0lSKoWGwhKAEz9nyaeQZmcH 1bvzs5Rw/P8kDy6ANJrC9D0kRJiW9EwHmP+u2zopPBb42t7unVUa7n2Zl9LTQAL0EMqr gERgi7IoYqr9K5U5EcNPmMsFQA+VZBLMWNxYb3N4oKHsVX0mXrA47VIaeJayl3l2BdC1 /imPhPRZd0Ac3W8ShdOfpEg8ewT7AAC1w0/tcRRyJ1Wq2NvzzvKNax3hjghdj+hiLbSN Lzgw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720654975; x=1721259775; 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=23lW1dYHwRXqrRIzh0tt4ShqtRWFD2EoChbCYcPjw+M=; b=C08IOjYMITIGClehkhFcob4FXHxPXBmUY+ZAoc3v/ShXk+QHBlrEU5FS/17X1TIcg2 GpWG7vVL7Rrwq0g58rS7b+vgE2jp9MvwSDhQSxd2RA87RRMHh0Jd/a1CtHA4o7ssJPVl CyxYEUYikKVI/R7BFF5M+9QpNcoM2yYnPNJLMbhniev0ry9tLKPNbENeOpkrNkDQWkXp SaVgTOFidam49WILmi/c/6Z9+VTiM06xPLCYmZLQKq02OLwKu76MD6a+NFt5bFwTSy4P mdB02MaaN4HNFe0reVJs4a5GpW9G1Na5zrVrL9bAQX5DWKFqFdvo3aMRca14gEohl/SJ AdjA== X-Forwarded-Encrypted: i=1; AJvYcCXxss3yB6oC0eWFns71gMjt5/7OouEPyYArlWcJCYydZhEor/gN5ApZgdhMR8t0z/k12WE69AQwvcCras3s5/S4KAfUbxKjq8jQM2gqbUpBArduYMc= X-Gm-Message-State: AOJu0Yzj3HlXews7/HWEsam5L9kuIwFZ54M9vtGMHB8KKdF35++DNXqM 7G0nipAC3QgqJUVAejPptNbV3Elp/vM70vF2kxcrCxM4DQA/pOOP9e5r59lv3yluuNengAcL1Gz x4QJz/eS8UN+Mhd0/og== X-Google-Smtp-Source: AGHT+IH2x5mDHjhnJd4Mu/PM7RGmCTtDANjq+WihoofI52RvaYh1X5nH2Cnmm44GTYY+PggisrUn2Jo0WRfC2acE X-Received: from jthoughton.c.googlers.com ([fda3:e722:ac3:cc00:14:4d90:c0a8:2a4f]) (user=jthoughton job=sendgmr) by 2002:a05:6902:1ac1:b0:e03:62dc:63de with SMTP id 3f1490d57ef6-e041b0593efmr582356276.6.1720654974739; Wed, 10 Jul 2024 16:42:54 -0700 (PDT) Date: Wed, 10 Jul 2024 23:42:21 +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-18-jthoughton@google.com> Subject: [RFC PATCH 17/18] KVM: selftests: Add KVM Userfault mode to demand_paging_test 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_164256_124994_CAD24B76 X-CRM114-Status: GOOD ( 16.49 ) 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 KVM Userfault mode checks that we are able to resolve KVM Userfaults and the vCPUs will continue to make progress. It initially sets all of guest memory as KVM_MEMORY_ATTRIBUTE_USERFAULT, then, as the test runs, clears the attribute from pages as they are faulted on. This test does not currently check for asynchronous page faults. Signed-off-by: James Houghton --- .../selftests/kvm/demand_paging_test.c | 46 ++++++++++++++++++- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/kvm/demand_paging_test.c b/tools/testing/selftests/kvm/demand_paging_test.c index 0202b78f8680..8654b58091b2 100644 --- a/tools/testing/selftests/kvm/demand_paging_test.c +++ b/tools/testing/selftests/kvm/demand_paging_test.c @@ -28,6 +28,13 @@ static uint64_t guest_percpu_mem_size = DEFAULT_PER_VCPU_MEM_SIZE; static size_t demand_paging_size; static char *guest_data_prototype; +bool userfault_enabled = false; + +static void resolve_kvm_userfault(u64 gpa, u64 size) +{ + /* Toggle KVM_MEMORY_ATTRIBUTE_USERFAULT off */ + vm_set_memory_attributes(memstress_args.vm, gpa, size, 0); +} static void vcpu_worker(struct memstress_vcpu_args *vcpu_args) { @@ -41,8 +48,22 @@ static void vcpu_worker(struct memstress_vcpu_args *vcpu_args) clock_gettime(CLOCK_MONOTONIC, &start); /* Let the guest access its memory */ +restart: ret = _vcpu_run(vcpu); - TEST_ASSERT(ret == 0, "vcpu_run failed: %d", ret); + if (ret < 0 && errno == EFAULT && userfault_enabled) { + /* Check for userfault. */ + TEST_ASSERT(run->exit_reason == KVM_EXIT_MEMORY_FAULT, + "Got invalid exit reason: %llx", run->exit_reason); + TEST_ASSERT(run->memory_fault.flags == + KVM_MEMORY_EXIT_FLAG_USERFAULT, + "Got invalid memory fault exit: %llx", + run->memory_fault.flags); + resolve_kvm_userfault(run->memory_fault.gpa, + run->memory_fault.size); + goto restart; + } else + TEST_ASSERT(ret == 0, "vcpu_run failed: %d", ret); + if (get_ucall(vcpu, NULL) != UCALL_SYNC) { TEST_ASSERT(false, "Invalid guest sync status: exit_reason=%s", @@ -136,6 +157,7 @@ struct test_params { int readers_per_uffd; enum vm_mem_backing_src_type src_type; bool partition_vcpu_memory_access; + bool kvm_userfault; }; static void prefault_mem(void *alias, uint64_t len) @@ -206,6 +228,17 @@ static void run_test(enum vm_guest_mode mode, void *arg) } } + if (p->kvm_userfault) { + TEST_REQUIRE(kvm_has_cap(KVM_CAP_USERFAULT)); + vm_enable_cap(vm, KVM_CAP_USERFAULT, KVM_USERFAULT_ENABLE); + TEST_REQUIRE(kvm_check_cap(KVM_CAP_MEMORY_ATTRIBUTES) & + KVM_MEMORY_ATTRIBUTE_USERFAULT); + vm_set_memory_attributes(vm, memstress_args.gpa, + memstress_args.size, + KVM_MEMORY_ATTRIBUTE_USERFAULT); + userfault_enabled = true; + } + pr_info("Finished creating vCPUs and starting uffd threads\n"); clock_gettime(CLOCK_MONOTONIC, &start); @@ -232,6 +265,11 @@ static void run_test(enum vm_guest_mode mode, void *arg) pr_info("Overall demand paging rate:\t%f pgs/sec\n", vcpu_paging_rate * nr_vcpus); + if (p->kvm_userfault) { + vm_enable_cap(vm, KVM_CAP_USERFAULT, KVM_USERFAULT_DISABLE); + userfault_enabled = false; + } + memstress_destroy_vm(vm); free(guest_data_prototype); @@ -263,6 +301,7 @@ static void help(char *name) printf(" -v: specify the number of vCPUs to run.\n"); printf(" -o: Overlap guest memory accesses instead of partitioning\n" " them into a separate region of memory for each vCPU.\n"); + printf(" -k: Use KVM Userfault\n"); puts(""); exit(0); } @@ -281,7 +320,7 @@ int main(int argc, char *argv[]) guest_modes_append_default(); - while ((opt = getopt(argc, argv, "ahom:u:d:b:s:v:c:r:")) != -1) { + while ((opt = getopt(argc, argv, "ahokm:u:d:b:s:v:c:r:")) != -1) { switch (opt) { case 'm': guest_modes_cmdline(optarg); @@ -324,6 +363,9 @@ int main(int argc, char *argv[]) "Invalid number of readers per uffd %d: must be >=1", p.readers_per_uffd); break; + case 'k': + p.kvm_userfault = true; + break; case 'h': default: help(argv[0]); From patchwork Wed Jul 10 23:42:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Houghton X-Patchwork-Id: 13729892 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 BBFFAC3DA42 for ; Wed, 10 Jul 2024 23:48:20 +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=x2NbmmZjpjJMl8d0fx4CDZHX67FcpWijiAzz4EJchKg=; b=4+z8Cl68oJQkmLAEnhzPRt8COv 3IHHaNI1vs0PCN6GnI2dhnHWWA9Sc8NU2zmtNE6F1e/aRVTe0PUunXnMqqOBX21AMO2HqhsRYN4gC wkbYwJRfR6+avDYgDACYRHtFT2eWYth2SN6aUIkRTjx9ar+0bWU9/LwsVJgBst8BMSSbagKcj9Xjt DMhDxIJ0lT8oMP5n5iX2nrXBCn/BCDs48FdnABP69XGiUd5q1BpGCYqJPT+2H7hanirf5ghI+A0dj GnHGPzFeqHa5LY0XERn/UWCzMYhyTOE/YkgI1J+yEJAbIdOUftgc2sA18OSgoSMNdY8TTJ/2pJQfB kksyEv1g==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sRh2e-0000000CA70-2coZ; Wed, 10 Jul 2024 23:48:08 +0000 Received: from mail-yw1-x1149.google.com ([2607:f8b0:4864:20::1149]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sRgxc-0000000C7LW-3chQ for linux-arm-kernel@lists.infradead.org; Wed, 10 Jul 2024 23:42:59 +0000 Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-655c7fa147bso4939947b3.3 for ; Wed, 10 Jul 2024 16:42:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1720654975; x=1721259775; 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=x2NbmmZjpjJMl8d0fx4CDZHX67FcpWijiAzz4EJchKg=; b=s1cjktjUiuGTt5KnBRY68i2zIFGEqH8qQBUsM2LxvnwLRr8POil1fbTfFDfneNXPZU pZCfU0Xp/a5TpXYcSygM8m+tiKyrHmyuzurdlyddavCSmt7HkQKzlJ5OBKj49SBxO2Uh Fx0M9aMPrWLYmwSb64n8VnaWJLP2jT7S/OSpfxMLCI67+G/RFVPRfGC8IK+bVWOXau88 vQFAmHPNJvhtBOX5LQuPnqpaoyfHS9+0OQMrV5CwmDXKiGW83bA/58nBi9leLOJaKyLs zFWke8qwvyySCF6vUSxxz4URClMuInOrcVGnT2i2pRtRb/IMJOQE7tnrOY0oCOqkX1jS vxhg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720654975; x=1721259775; 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=x2NbmmZjpjJMl8d0fx4CDZHX67FcpWijiAzz4EJchKg=; b=SpOVPQirzC2ijCeiB+w6Drx1jaYxK7NgbCa6BDho82bwGcNBHxMFYyYgZ7oxPzIHUx DkAzGkF+ac4inB095AJslj2U2ey/57tyzAemf2nOfMk9p6nEPwTmk3gJl/5rEBwVwsSa u6n4I9ZBwfx2h6V54j/NyByQIe+vti9ZWO8NRu2+xZw+LQs2vVRaGtJF6Q7GcBZQhlUn 7gGiHflNMZDomnbvHlgnpRTkWAomtDxjSFjWbOYbdtpHoSnmLUEx0mescmGfn1xh174P dNVnFvp/uh5Rj2x0qFPcoxN2n8dXekVH7dZaa5R3IIuxUY64W8p407wvmZ0wT3qXdyzQ dCjA== X-Forwarded-Encrypted: i=1; AJvYcCVz2juJ/SrHIqeJJ5vprHe+yWc8CfEHITPYXY+kHl58Oto02abbPHXZUr66w2t2o9B+YO0npN7oUE5Xg/4OnrKN0g3U+KjCez7yjA1gaNEvoI3iHiI= X-Gm-Message-State: AOJu0Ywb83PpRBA9/DOkzyWK/aKvPNaTqADSUXrD7Qt2mXumCMcKGjXE T+nUQdCK/WVS9/hIfb1XdrxMTJTIxEzNQp7hbBI9vwadh5sPMfIA3JptbGRNKXzeoODxOK488HK IwZo9W7ND6l43UpEZrw== X-Google-Smtp-Source: AGHT+IHxJ11Jc4Pg7WkmO3SlYHVbY6IW+Wqfxro8+OBFjX0tT/erDtqX8VfrApEznbWBw0W1kCfLjttZO6MW2F1P X-Received: from jthoughton.c.googlers.com ([fda3:e722:ac3:cc00:14:4d90:c0a8:2a4f]) (user=jthoughton job=sendgmr) by 2002:a05:6902:2604:b0:dfa:8ed1:8f1b with SMTP id 3f1490d57ef6-e041b039d25mr256249276.1.1720654975664; Wed, 10 Jul 2024 16:42:55 -0700 (PDT) Date: Wed, 10 Jul 2024 23:42:22 +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-19-jthoughton@google.com> Subject: [RFC PATCH 18/18] KVM: selftests: Remove restriction in vm_set_memory_attributes 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_164257_028166_BCF5C592 X-CRM114-Status: UNSURE ( 9.51 ) X-CRM114-Notice: Please train this message. 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 Allow the test to run with a new attribute (USERFAULT). The flows could very well need changing, but we can at least demonstrate functionality without further changes. Signed-off-by: James Houghton --- tools/testing/selftests/kvm/include/kvm_util.h | 7 ------- 1 file changed, 7 deletions(-) diff --git a/tools/testing/selftests/kvm/include/kvm_util.h b/tools/testing/selftests/kvm/include/kvm_util.h index 63c2aaae51f3..12876268780c 100644 --- a/tools/testing/selftests/kvm/include/kvm_util.h +++ b/tools/testing/selftests/kvm/include/kvm_util.h @@ -384,13 +384,6 @@ static inline void vm_set_memory_attributes(struct kvm_vm *vm, uint64_t gpa, .flags = 0, }; - /* - * KVM_SET_MEMORY_ATTRIBUTES overwrites _all_ attributes. These flows - * need significant enhancements to support multiple attributes. - */ - TEST_ASSERT(!attributes || attributes == KVM_MEMORY_ATTRIBUTE_PRIVATE, - "Update me to support multiple attributes!"); - vm_ioctl(vm, KVM_SET_MEMORY_ATTRIBUTES, &attr); }