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); }