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: 13729852 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9D40E158870 for ; Wed, 10 Jul 2024 23:42:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720654961; cv=none; b=NkKclZeZSGIL/iPzs4GFa+ya2M1RtIKqiy/bt2hXLJSP7VdRU09xhABa8+8hDn4mYIVy5JGs2V+pss/HYtVujciFvyTAcbPuvNGTHku/rVluYjgmUWTPXfVWCxKemdDmo5BYPyZsUFs+Wnxb7UgfqGy0LGX7/AeODlW5kupeM6o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720654961; c=relaxed/simple; bh=xxkhVIBeGXY8pX54WQHQVUoU/lEQy1GRiLeaV2vAb5k=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=XeYlo7Tdfr88uYNlqz+1P5dsLhw8xBaNRh40tCMNolkvmakTV5Ye3tDxxT1JZwKenfmP7NpvWtRAQHksCzbKoX6HIysFZpq2HeSI/vpGQUvzaYoM603sf8PWlMbqhKO9o9E504e7jqT5JojoukCy2VIApb6Cagi3SaHHQc5IxEM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jthoughton.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Lyh8M3Vd; arc=none smtp.client-ip=209.85.219.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jthoughton.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Lyh8M3Vd" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e03a544b9c8so403159276.2 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=vger.kernel.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=Lyh8M3VdZs3SrnF22+yc6jjwGOAByZI/gs4WKQyaPWDQ/dZGlw7hv3Qt+tt7a8n0Ck FIsjNM39OA3GttUqKtaxtRf8ZHHimuA6Qe3rrPBKvjAk83Zyo3NTyfA+oB/941aPi1uS ifwteOT577EadUZswco3QaM87ObQzX6tq/iyn+XjuuHpWoIrz+8IlJB+mlo2XXR58G+Q 4C8dDQAHHF3ek5t4JFIQ15ymqnBw9ErC+GfACcRKLqObBQiQ8iHvF5KRkp3wMBPW15fu anfXxeZkwx56Nzjm5g2DFeQmPRY9g6J+2R9vIKDPNGu/x6BChGsjg0rZEI0EFTh9vbo0 O7Fg== 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=pyPfPgx2kVYSNNktMpJuTRQtIhZKhTPtehxUOnCPCqu+lXLPVj/Riz9EQChcsf7Cu9 eSsLlSZ700q5B4hlbfXziI1ZYNvT8rwxZYIM+k/3PkRJxGWwdeY5Qqpjmd7A6OE9uP8A AVgZUoyY/TStTkCUSgnWrFUnapxwG3dgJ1j/orZVoK+k/9mBhcmtBVQWg91y0FTX8YCH xvoYlSm7Tv6a4rdfIekDoAk1zpgDD4mrEZzDRgM7jp8bhtmPng6xPb7P+V4dKQ+z4D0V 5duPbCdj1nkTbNQ0x83fI3T6FcfjvhIO55sKZKaThdyRXWco7hj5lgEwTk/f47hGkkx0 zpog== X-Forwarded-Encrypted: i=1; AJvYcCUQIcG5/2f5hhQzcXQ9+AJS+Eq4yXqcelZMVGEe2dedsf+mp5NkyLFxvB8i8MBCYadXUvLuOwogCED3eo1MTLkE5z7W X-Gm-Message-State: AOJu0YxT/4IjovclqWYI3gqwYX1U/qLjfCEWrY7OJ9fxcov9PTrdSLnS HUTV4Gq9ACnEkV6tcC5EH1GdaDz4bGCc30uyEzOSjZ6U5xVLOUoDPnDqpEkBwe1YPqJH+RwRf0z BvwZoH7S0bxG8pD9HYA== 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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: 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 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: 13729853 Received: from mail-vs1-f74.google.com (mail-vs1-f74.google.com [209.85.217.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1097E158D7B for ; Wed, 10 Jul 2024 23:42:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.217.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720654963; cv=none; b=Ty5JX8V2kK9KXUrXQNIdRpbUsqOtNCYvZQZX7LB7XEzLqXCaMPINL8gxJOlXh4RB3g1kIdAiDz8mxszA+jjE80iPilNqGYmJM9akt/q7wu+N1oRwRA5ihB/yhc2bFEYRcm6xT+V/w8ZkpHQmWZiDk/kpgPAcl2hnTOG6OXUWnog= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720654963; c=relaxed/simple; bh=fMhhiLsPFRJdblFtX9Nm1DkhL+INhnd2gLXtlR8Pjcg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=IR+9dQ+v/bXgj49JQpEeSsBopSCjEjIdh1iviSYvTin9r53zKcz1DApFIZmd0ZskoYfV2GrbAJpLGUZmh32GYz0jKtAwWyApgcJcneHj7slyEG1xiABbCZ6ckkKmYLlzGZCih4C+iXMG2zp6S7SoDXMpEa3pWfKzuzmlZWg1j04= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jthoughton.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=dj74Obhg; arc=none smtp.client-ip=209.85.217.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jthoughton.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="dj74Obhg" Received: by mail-vs1-f74.google.com with SMTP id ada2fe7eead31-48ffc7dc42fso87211137.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=vger.kernel.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=dj74ObhgmWglBTjsfK24/p/zFxK/NLqxsuzJZxMGjIDuN6BRlDBVzp/PZ/qerDyaTl PJvz00daKKTwpNK1QWID9KeLWDLbSX0S4vU1hE/hL/7J4ZG9Sh5Kq1mTot6X4HYDPtjR BOMllJJ/zqUvKT0tKJe//KTi3LQTISG6UTnO4ypJrtu0vxYu3EwQOwmETlKyBC0xQHVV t4/134slOqYF/ZpW7yziaV9SWOjwOSsawiKns5xt0r+WvYicWd8QbRAoshffsgIs11gc QoejyqYazDYu4fblIv3FyLA+MOt8qTpQzR0TuE90AhR6WUi+NTzAfLHzrvmG0OwLwiiZ CTEQ== 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=PMxXh4BkHCE971bnzZG2wf/yJ6DOSiVkoT4kakXJtCjo4SrUmqk3FDhZa23RCpSI9f TNihfNdXWvKPDvbtlIRYahg1R6Ap29kYZPUzXDLxgBO3ziKqkKJVJfoXGR/ZMzEq3vqo 7ESfMMgLkASmOFWr87oxa+OHf8v1QPxmG5geM26BLKCoivD3bW56xv1u8JQIolWSsRbM m2ZDN3RJ7QSM6VknVhMDWqaP27Drys01ADIBHopH2Ok5OWv5sErycVtrefhxJUOo++vm hI338DJnehyVL4YaiCJ7xSixIu3Qui9KO7XMZpmw/UfGVJYaWyXDryEnpcSPD7qU84RS Madg== X-Forwarded-Encrypted: i=1; AJvYcCU+58Sq8pEFtPFRSnA+wIAw5T+WWpOcclmHj7/yeIEvwnn8CgJfBiaH8mMX4kyZKiPlSFpur2NAqoK6Ev6NpVtO4PP6 X-Gm-Message-State: AOJu0YzAP+QdIFfB3+vWyzQTaD2hJEPMD1RvX0b82AZ1hUigDaLdmSLG 5h5fky1a/u3nDbnrEOgr4j4kNlnFeyzwGEq+Cz8/HZCo0jSlfQVipyD3hzFS5eI6M2XgNTPXtaK mnOoK38/vF/DuicZIyQ== 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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: 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 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: 13729854 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C83B2158D82 for ; Wed, 10 Jul 2024 23:42:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720654963; cv=none; b=SPjP3oixJuRRV/u2AA5LB9V49wMCSpjFavjCCe8DbI6xSuDuZyvH2ezDWiV+PJTbgApl4gD1P8+WTlYI1n1XD26lbNFekzV1S3pNS0mhD/1m0UiW0GYB88nTAxK4rObjFOUnAyS5xY6lmLA217LtsQTU3gOrEKXhSHNtO5g+TxI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720654963; c=relaxed/simple; bh=IdpdyzI/6CbiUVqWLoyfJDrAPLlE8GztB50eBsdtlK8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=nB5PUJ4jny1zf1a5VyFpXzYC6ihFeoZIJ+H0mfPycqionFezcOTZN17xubokQWFnJJqAK31FGmcop3b3N+tb1IvLQgMyohBlAd231qOISbxAngFSMaqeXQTiCz1ULG5aqsA1vyCJpSWjSSFHG5+PugBOmuGQafpKWykClOVd7Cw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jthoughton.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=z6xVYPuY; arc=none smtp.client-ip=209.85.219.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jthoughton.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="z6xVYPuY" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-e0561513c21so1392349276.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=vger.kernel.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=z6xVYPuY9u9DnW1D08N6UbeW4WZ3lUpA0I54oa32ZS+jwfhopM+OARVmeh/DzyA0Ez KFaZFARK1UBLXqQSP/uJ6HN/yKtjRP2WDCGu5y984nR1Gx5xnC6ILzS92NJGzSzYKLd6 zC0HT4uFaPdIOPkpumTOqIAc8bth+T1YosTp3WusDi9xffnpvMU/xKFnELAx2SzUH2cO 4kf1NAuphTdbP5Y8p4TkC2xEpSPiJV184S33CjBr24Zobjdu+vOVnP92S2c8LlpmcAF6 8zF5+zlQ2FwaxxHQo6UXUB2lYiS8pS7FsWlUVBqSTBU0+pfGWJg8yjGtG+M5WnsBXw/q XAMw== 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=Bn5XZsH8pRQkLswK2gU/bRfJfJsOyYGLziHrKuEB+GaQab/lqpo25DkngqlPbmAXYJ 6lUJyy7PbzXK+tclriZV5BvDiwKeZptnRp/J2A3WR/7Kkk+8JbzHnDzqdJZadnwRCl2S VCNdw7e6f3FL//aeTJspAmo0q163tPMqc4GFtByLFIHd48d7RgF9fR05Yyxe9F8tryu6 tcPYrXumM5CehX/IBCN4MEA0ftrMXsd4eJAbU8NY5gm8xtOKgFyuki6Hi1XBHjSzEatS hBmO36o1ww7S374po5hViINIhRXJUSLWjijv0SdVJ1RZTFJRp3CInxP0P9KRtOn5Tya5 hbkQ== X-Forwarded-Encrypted: i=1; AJvYcCW8x4fUsroheUuUUmfjDBCcSaJNTyluz/ukWu+doXKULy/A65bMPPwoe9gFkgFNl1qG+VRcEWHZC7xnJyXoYben+5sh X-Gm-Message-State: AOJu0YwOMjeZXgCjbzUzkpRIe2sahcZ0ypdTDw6wVUnDd6JxnzvcaM4r bZA3tiVxR7nmnykY46jeGA54gK7oEa8mCdV4019spv59srtYwxm/0YDjiXSMWxdSRrA2DaX70XZ 8WZTxSLo2ZhWK2cgfhw== 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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: 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 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: 13729855 Received: from mail-ua1-f73.google.com (mail-ua1-f73.google.com [209.85.222.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 20D0515A87C for ; Wed, 10 Jul 2024 23:42:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720654964; cv=none; b=Qy7QWBZI0OvXzJ1JfdSVLf90jez2ayUKHCvRV1vd+fBDyLTPFkIeetkkeeEtrT+ZJN5LDpW3TCYlU8NeFets9bKM/uyM8njt8UWnJxSfX3LHglfGQMYHFcBAeJa17VDbbPUlaEdPPLjIqUWPi9nkniXlYvFQxgU6wTrZrIxnMW4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720654964; c=relaxed/simple; bh=3Qf2g5mWL+5xmgWA+ouxgQR+hBm6JQ6o96+DWwhG9sw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=cJQIu1xB695GqWpEit5dAjnY9l65uHsf8dRqiluFYXGuQ5VxsI1OooDf856XeWqudlnc2O4P5ofVxZlaRTyJ0+RMRPehbwkgJP+0mnUgPEeX+wv05KktZkHkYSF/1k7IVLMOxbDhX6ksITvyC/ERPY0T9KDZ/Z7H3sFY/169B+s= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jthoughton.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=rt39n1a/; arc=none smtp.client-ip=209.85.222.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jthoughton.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="rt39n1a/" Received: by mail-ua1-f73.google.com with SMTP id a1e0cc1a2514c-80fe896d0e4so84796241.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=vger.kernel.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=rt39n1a/jCW7i9o7gJNi2GZa8fssrydnZa9nAtRUhhf9FCdc24COshDrTp9lCmXX++ cTJ6tUWD6D29mCRi/SjJdk2weJJZzId7/Jb7a638bzmHkASEbrim4Du4IX8O1ldfEUrC X2D8ZDbs5ngCo4Tbyx/8NDU906CZ2b0PHv5PFJeA2VY4K8iDd40JmRpMp78DjsUebw2/ 7AhXU0Q5R4WvK+4GZwuQRalJLAwbNOBo4d9eqyDpQEhYTlgpodDMUwEcLY8vpHich0Wi qt2iJw18xtapVp6MeeL+XXVuwyFhIKuxErgUbt7k0ulskNyxZr5nqrhhZkf9TKriL5vs GHKw== 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=Jku8QC77jjtxKrc7uL5EdZMgX7ll0v3r4okGjzce3h3w6h73FVQMDKZteMpAeEQ14B QPUDnnRt+TmhiX5QmY5NLlbTY1T+A56pGQdmuUScF+rj8zinbkJE+3AjP65hvrcmimna j0o0UcyBD7/eqmtmjlS9A0ARYkURFDmfWEhe/rFlBe86Jz/Il6bpwMH6fUVX+vjTp0Ob EG0dPiWn0yBie3v7vbVDil5Hb6E3ZNrVtYIRUqzgnQQX8iZk/SsoHYhPEQeDk+U/4oU6 DmF/lz2hGw1xmYk6r49o49iVQQRQvT/wU5lN2uq0m77L/zOkpccPLgeudlwahwsPqXQU Xzaw== X-Forwarded-Encrypted: i=1; AJvYcCVPEEgCx50yh8uZ3Rvj4XZR07OFVtM3pND6noAyp/QyPBtIA4ie3cAHqi4G2LODyxi7FbOF3Xow+30mrrz0oOaY0z8s X-Gm-Message-State: AOJu0Yy+X+gxsdx4Yy+7wugipSDPJLiZ1c9Xzywbdfm4EjQPm05uiv2c 2EWCYaGdXu5gRvTRv1P86oCvagTqBppuOt8yD50EEa5npynk4j0AOOjQ2Q9OHIDjYlTc5UGXqRc LgVOEioEeL3YVszweMQ== 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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: 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 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: 13729856 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F0B5C158A35 for ; Wed, 10 Jul 2024 23:42:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720654965; cv=none; b=WuunuAYyGwP6jsdJdvpffyvpmxTeO6m1oP4NrypU2IJI3pRViTkbiS8dd2/UF8QaGNkmgXNFT9Srf9knatkMIxstY4cZHjFjy4Wjr0L3n7+ESi+e0i7qkQjB6QOpk0PEPdLPuOYifSAouNkcq2p/9X/2qj6QwAuw8J8pmxasgTM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720654965; c=relaxed/simple; bh=9qd4I5Nt4ZpEq1/DgTQG1WlNzpdV6maR7Ymqr0dBes4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=qx7+S6qPTbeVPOUpsX0I5lfNuAoaEwyKoZYiG7hcB1LRLTxa4l4heSrLikXJYSXk6BzMsSyKmk6hsKCA+8V3zC72cZ4XJPelY/UPiBH+wnymB4YbEckCOn7LRoblcwbM8VY+e3nom5TMhaG0iI4vUWl56ruyWLqkrzlSRGppUEs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jthoughton.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=eEvDKZC0; arc=none smtp.client-ip=209.85.219.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jthoughton.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="eEvDKZC0" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-e032d4cf26aso518572276.3 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=vger.kernel.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=eEvDKZC0qcAwFW44NHp9Ty3319OenqXh6hjnUhLIDiyiwOJH8lphTbC+E1LPBwmDom 46LBH4m3KC8SMAX1CKZOsUWQiJb0q8OJWZJ1Jk6Ez+RJW5DeoVTY1M2EtUOQZ6x/871M 9/mTORt4Z78JiWwyLY3T8LWQW1UMFKLfSP9iKUD4JLiEeOBJbB+C42y4s5CPhQQSUfck ZzdpNELz7exs9jZ/y4Gr+B8R3V6mWIzPXgoN5eIgH2KEHCPsfUcZiEy+aWLE8EebuCu2 629ewLBI/nvpxdjEkS7UATUHmt8Yh6K1/qqbPGpSZ+3tvKn+z0oBYn11CZtUbCfk/eE6 /lzw== 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=fuFqxODsux8uzvvgoPmfs8fOq0W+0Vy9bvIYu76Y5I0Jkn7OP/dtUhJwqaiif8IDcQ iThaq5VX/zvrQIul4wMQhZ/mIEKT/RDOvdLVtGxAUFYqJidWBTXUr+6bWUIBONC2k0bv P8Ks6vbrfq19YxQvYC4Q7MpUWI+nzSj9A4KCTGQvkzMrys5QvH2RG9uEvUFFacE/5O4B v2CzIz7lHU4Xhh1C9lOKHO2YZU7822lvxWuIn/w4ymP2rbTPHYeIihoYbZRLrOl+dEeX Qgb3GOCwbh9vgXe4AeNOJRZP5Qvpg7a4iOJL3cftMXjtdtOAl/t+SsuSHRSF4LipIc86 XiRQ== X-Forwarded-Encrypted: i=1; AJvYcCWKYhSOS07rjGmyEnqWtQwOIyNNKJkNkjIMhqw1fMHj65rqrMNsail7OvVgdjL4amnE7S7evMaKXc0MqyJdB4hPLKUF X-Gm-Message-State: AOJu0YysOoaS3/3pv8advBbHJh9RPdOOtl4Zq0w2PMLjrSvtL16WkDPn awCfCKbq4qthr4RNJx6+sAoAFHpcuEGHUJ/ckmRhcDbcyV8TB5TYRgfxOQ0panpQ4/wFCBTf++m tBHOTF7yoO0YCylpKRA== 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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: 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 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: 13729857 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0695815ECC0 for ; Wed, 10 Jul 2024 23:42:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720654966; cv=none; b=Hshv5KyQ/cA2BrEzcOPbejnAUBcbiWAGe+WaE9MNZw+SyoSydns0r3rnuuA+AuWwgSvUOjLr1fydumStWzBV9X4HDzgT1Le8dpVqNseK8aj/ZRs+WydSHtTbAsDv+zS3Em4KCxL9Ly5jhJtzIsK6W5xxT43fETGyEg1EbrRD6BA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720654966; c=relaxed/simple; bh=oWnYdn0BgDHSxRNKslgfesdd9yEejwT/yhYAWEJx0EU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=nNPMy21RBqXvtMUU4yRCYOv9Elixnw2KaiSoozSfkd6glTLTgJjJAJLT0jIuNBc+aDhRSmUucDI9NCD0Y0ruyeE5Et93BXyyOzCWlI1RQkTVt6OkPXwl+pDe21zmLcBzhM07R+1IBsYOj9xRM9H87Q5jViQHtuDwQybV0lPXqNQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jthoughton.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=cQ37ehsA; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jthoughton.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="cQ37ehsA" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-62d054b1ceeso4632767b3.2 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=vger.kernel.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=cQ37ehsAZHZ/fS5YDkSexvaRxXJno5PLOWbF8zdHMICrqwfOM6TRgcGsE89x2vuS72 qTVVdk4N0P//WDrPT0pDJTlOK+RrwdLAGNlO552d0ONRNWj/YLYAS18nI8inrML6kk1R hgYgHZmesf7EtVfrnnZhNCURHJlggTCyyvHELrCfOKc8W7GX1ToTkM3HAoKtfcFQKtRe uwoA01PReifS6nRjBcQqOLDYh81N1ussNCyn7BidgbzR3Pdg3TJ87VPyVb35DSw0E5Bp uOmuVaKQayhqV+LX2NXp0cof9ZefM2SSQLQNeCQC9y/++tmRAcMQukN5VYYJkCXOcrzR /AHg== 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=NEMQq9f2+jMmPaWFEhVx5hcvpFSwrfNFYAFW31LR12qnwqw77QEoXsJT+/ZSZHA1h9 BdEkmVOcVVWkaoQtDdI0hZRvg+OMKK/D2HpNoNmOXJ0y9MZNAYtDroH0fNt10+wHovVJ +QD0synHi4bHsOtY8IrjBOfEs3dU05sy3xlWuVm91UuOlL8M4ZkB+Kz5rprK0n9sfsCl zUryvdjBNm+WZEAGXBqaWGKDjFxH1nlrzkcaC+nfpiZH2ijJP6gObCLmBBfRtIfaJ0nw Qv5DjdL5LPMYSU5yHbEu/L6EZ8yuNSkv/EkeioRGcMZLZHP7/v8xeIGZIHdc9Q9AeScg ZQKg== X-Forwarded-Encrypted: i=1; AJvYcCXKN2PO3SEpek4fMgt48U2PIve4UREAAVpulXydgdVJucTcB5LxdrcDJy44xcSFWJQZvrWjDehAaOQF5m9dD+qfmBrA X-Gm-Message-State: AOJu0Yxk+x/xKO1MJQkygExWrWeFnRizIed/k05yJSuGLuxSQhmRZWx3 PInmHnPP4fsZTC+tXYM30XKETstFhebocQTKMGTGfFSQH7ALgj634HlGrc0nXW36+Netza8vZoZ YWpWozmihXAqEzibhxQ== 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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: 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 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: 13729858 Received: from mail-vs1-f73.google.com (mail-vs1-f73.google.com [209.85.217.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2E93B15EFAE for ; Wed, 10 Jul 2024 23:42:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.217.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720654967; cv=none; b=NW0ffloaYIQFyXupcmVAsAmrTzBs793+zRdm9IIOo5H+ayNfwKq2h++Go3he0/RlRQWyie5vLX9ey/PQP++n5GFG6yVE0lQuk3fZ7f+gCD3QrEZSMoEgdaBCw50tGqEgnYPotNIZzM/OUUppXX0lW+x8HuHwm8og7oGsAgMpnsw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720654967; c=relaxed/simple; bh=0X56a4gN8b7VQgThnVkXYUH34LPfOMnmnpB6+EHKoT8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=lvO3odYEocRHhgIw7Dox+Kepp23YB+waLK3zmRi/i45C4o86ty/TKsMyV42Kd2bbMpE+YSkMjwaXvEflaDMeVsk9aT9FDWua+l9oc142J6rpnXm9Hw1UYnt/VUC5wjHP6pMNhVDq5mQvJ5mbHNUr3daQraB7577bV1GnLrdU6Ng= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jthoughton.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=4Z2/FJYK; arc=none smtp.client-ip=209.85.217.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jthoughton.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="4Z2/FJYK" Received: by mail-vs1-f73.google.com with SMTP id ada2fe7eead31-48ffc7dc42fso87231137.1 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=vger.kernel.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=4Z2/FJYKjWb84XWgA4jz6adOCqXSO4ooYNzTgDHYNBGqj0E9z42IvXZnNdXQKNxBum jcKCUM3z8KOnd5wJ6ui3CbRqBDUsgUJxziiXiPB/zifpnk/VQjQhxMh/y+PuaVr/F/Us lRWbcVWEEFZ5dWzasCENbA/TX3DR5FBP0gU8C45OMuslryx/Y8c5cxxKYXgBPCKAo5ck m8L3XgxLOYRXHGtPZL4CiSQoC17DUn0V2xmj/OoEohSpqapaQxzmChQxiaRfXAlWxgyV GueGUp9AeTIfH0uBlAKKZtpU6yGEuBpMXuBSxBJoZwbJl4wnR7mfGa2Wa03Rh3CptsZJ I4hg== 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=MHc9DMCSbq11wKXTSyzEsOBsK1TOxCtSjkAFXOMActYPKJ4vfajZ/HUlaqwwFocxPl K7Eeiu+YueAs+mYfe4ClgcX8/4p6ynMwQl0ehUjo9//rXwzrx7+AVQ9sPrAAlaP2yYYq GJ5z4DrSCfO4pu6X7wJOTie4BkajkD35a86Yx7UtJyMQyNeMZAYqyUw8VkVpEFuFNxl3 I6SOHy0G/FXOD5q96p4FsVcsRLhbzslSL/nNNSN3mYovtQbJBnuQ0nOZr+NYrIAyjlq8 WKz+QgmNR7i8EFG5jk9leQvqPtXlgJCCytLd5nNjuApd6ZXLhl4cdmLyou18kXSoeZlV OwzA== X-Forwarded-Encrypted: i=1; AJvYcCWJPdsMZoYLqepXgL5urOmtv/sLUXZ5uXiMXBTbvHKsMKhFifdAjjaarzaLMNDDAo2+veTlEvDSavcm6KPj5X0SFdjA X-Gm-Message-State: AOJu0YyGipsUjYmpzFFfFThFURV+nD+CcBhdPiDmZbbJbJz2/9zH3wFs BkSD8KWy7fUB9QUw1pid9Vkoyd0zaNjqgvZimscn6t6c6yMGO4oOea2kICm93BqpVeRfxAmr/NU 47snlR2d6jU7OiNwb1Q== 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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: 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 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: 13729860 Received: from mail-vk1-f201.google.com (mail-vk1-f201.google.com [209.85.221.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2F14015F409 for ; Wed, 10 Jul 2024 23:42:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720654970; cv=none; b=I6S79aGE4GN/ESAS28SBBCTOlfe/LtcLQRGG9wKkM1ig63M3PofdIL0EdEmfILrhBvN9f21y6As7QPLSv0VsHo/KN1o/91DiHsbY2r54zpMx09dDoS5Xt76yeFXvYlNyOEsus5EdO2UEGaefjxxS6D6OMbuJBHw77gqhb/U/sNw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720654970; c=relaxed/simple; bh=E7C1PVOtD+i0CMt4EuXJKTOZhuRKpDD4D1ufbjsXsgA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=f0MuvOw9a8bsPmDTO/eQDvwsj9CN79aHWq0JrlkylAg+6KIHxbCO6GBEs4kR1UI3kexu0bgFBbDWJv8RWccEjPiKxIctAOf07IbpzYzQFwEDKlHG28CxxTC0HAqGKQHH5lC77fJEvZXHfa4Key8CA2hEm4c/EJ6YQcii1DpU8c4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jthoughton.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=tTPY3qEG; arc=none smtp.client-ip=209.85.221.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jthoughton.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="tTPY3qEG" Received: by mail-vk1-f201.google.com with SMTP id 71dfb90a1353d-4f2fbc2cca9so113047e0c.0 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=vger.kernel.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=tTPY3qEGpyKrDknDQYOzinf0Eg8wEp7DbObXjd72K+D7hWCBHVE6NvexE+GVdB9Nh8 ST8gTDtT6oEcbBrru5DUi3pbq1qWV5/j3EqUPl8XMcasT6UOGro4nkQSMItXf3BGvqGv Q4wDsJbd17Uut42SdcTb8HFQVaNObFdl+AbkwwRWE6gU2kRCTbWTyVhlclkZUOl7hWvV USy+dZcA26D4P1ERqttWZYog3ZEyrNA/HhRZRF5PlFjGg/M/Lf0V/BfhOoAkd9dTsUkR oXNPovlMhLIPpow/EEh8jFX/7QIFt//ILkqpcPv5pWdyB2Kxw7vE/EAej2VihOX7Xake 6UWw== 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=Sd04BINWJ5sRKPimk4FwK7J8jgZgcf2/v5+mXf+WADO3Mv6/6DBM7wBWsmh2xinhNA fpgeYEkiuajTt834W5U7YU1iq7NYGxEgoQEejFzGqDOP0CRtEJL8NXjXkZlsSmhGmoKC Xyd6tEJg2W+Mwv00rkp/IfBcWfGfagPZrgp4yTRYYJlHteUXeRljWkH3ymZ94EWcIkVx NpOYxYxkH6D4et9Q61rCkKhlPWv6drzBDQCb5qLmZ0qx/xv7sksSqIswXBuS4KPA/5Ch hxcrPjA3c33ZGx1JlMkHd4+wL6jlAWn1BIGiiKXSvMvW2w+jALcnUIRZXmHy46poMfZt eamA== X-Forwarded-Encrypted: i=1; AJvYcCXpNQn/vLRlPA7PYriS5eB5wDyaaiVQE8gkdDXHG+ZoKe6EZtIP4GE9GK4uDELzb5sQo2JkYuANFYZR670B2vZBOxC3 X-Gm-Message-State: AOJu0YxL9IE5SoV3t6JEiu2llWjbyzPCzNR1amMeWz151aL1MQlt0EtG cBec0pEjKkGm2uGLJfpTgmgl398Oz3KMOBbiZpomn6NKUcayKP6eGurzckQXndyIxR9Y9dDk0HT /8FZ7zjkAcQNn2JOpdA== 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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: 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 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: 13729859 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E4D691607A1 for ; Wed, 10 Jul 2024 23:42:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720654970; cv=none; b=M2AsFDDjXH7k1GIUP+Y4D5aq+E7s99BPh3g6mNb86VMAdJqCOGzLUAvOxaK6gs4qQcqLusq6ku6kJhh7Ilz3lrYRzeRX7YfsIjMbWru0QIU191p9A7GhPcC8E65BHuPlIX6idk8Vzgbq7DrnWTENTnLcWXnBKG6tI6k3231Cmow= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720654970; c=relaxed/simple; bh=EiaFG/eYwx8Esdk1XHkxgkjzifP+kflmXrizk7/imlQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=VroGlwkog9q1d5w8Wh00e8zbb7iSNqveztJ3FbUEkaQ9N3/tqZY1r6JtIPmZFfJjD1XiBXdklQyfzEr9R5AhpF9FwS82WFH9xKOb8DiHY+rhQYGz2D9Ag4Snsm5l8yOiptkK6tqPBCL8/AyuDiVKgjUZSKLxS01rAEz/EplOxRE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jthoughton.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=rMFUNTKL; arc=none smtp.client-ip=209.85.219.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jthoughton.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="rMFUNTKL" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-e0360f8d773so448006276.3 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=vger.kernel.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=rMFUNTKLofPpwYducIF7Cw1iPUXYaUMhIvA4pJK+FJsMhuBbEfYeGicZCi2TAepbxP /gfUoP17B9rXWJ04a8cN6dpwPorrae1HDEr0DaMvofo0rxTKVYyJk9uUPiW5jtycP6sM pZCFGBncyRsJ84unXBEqIZeqeIws7SW9Aowz3p9Z4Xi6IVfFcdNkG+NI1RgmFuLsV6lL UmM+1Q0Svtf4n6oRyE3aVLRupiIw93GNtJDF0o8h9JlpvB5VAcPLnJj2TRcnb92fpZhf 4B7SwgKMUOCpAGuicSrQ9lZgLXgduUWTfR31z++P+QGZH+8fD6eMyxul0aWQ/fd1+Pht 1qfA== 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=LR7bYYQ41i6WLiQH8KAPKrNuSH36aq4JuWUUZRM3V2y801xWSZyiNN4nEbv84z6KKM /9z7J4hCaq326PypC4tzPvd8Mr5wR+I+KLHiZCb4YhNsANEn2EjIYZuGvpgCqMTLnAQE vDAv6mUNrbodHOSE/eUoJlH8A6MOQKyiGE9XY6vlq0yGhAcLRDxMF0jysW2l/pQ+ZD3d nRCdKEQ8iztIoam12Q+gd1SA0OvezM4aUUPelbAjdxNIIgdAT9G0ORG3FXhAyGlQhThM v2Zte1vJkW1Fija9oa7uNLiS83qpGJSEZYDXBSP0cPPr+HYwoJ9MbMAnVP5mqngT9ZU2 Rwqg== X-Forwarded-Encrypted: i=1; AJvYcCU1UB9UW+g1KfyFOWpOVDXAfthiWKcJOc6PHJ3a90qEYRMH93l8fEKLkHxx68KuTeOqzRmn6NaJH2Y6+T2yQS7bUK1a X-Gm-Message-State: AOJu0YwbwAFKiA5VodsnrTPl9PGIzt3gMQS56LwZ8TGH2wxk33dx7sGJ sAQhv7ArOcS8PozYBYEB4RDhr57QnthtdyR1wdAib2phNNyNUSVMXWq8Q5DpH5RgSwidmHotL4F wKic3muX7KcN+YLVigQ== 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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: 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 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: 13729861 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EFD5E15F3E2 for ; Wed, 10 Jul 2024 23:42:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720654971; cv=none; b=Cczk0zxiCN1Yoy8Y5CPsKzEj9DFM/SSRGPVq1n56qPnTCLXY5z951pdmFR4jiAmaQAP1fW1g/YPO1BUBbeon5VbwnBZMpl3w2C2StS0Pfbfu476HxlKeEetqDLdiWScba+Ty3vFT1csbXjVDIuF8VGKwCXQDMztb8+tpge6OGAg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720654971; c=relaxed/simple; bh=51xqz35xJjNqxl6o9J1L1+yTufTjMwvN6j0qSUdwPuQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=JJujOoTWwTVUy0vhw6lYy/T5MwJl1CMUjoFw7BoFldsc+hb4uCk4uaKPv+QvHD3CtlpcqUUM9ZaDH/csFyXwiGBMUMwCD6Jpjowit7uFWMY8BO2tzBIwHES50MGGPZKDBBYW9jywbm21d7fFRDrTFRUhLro9YTyTZHX7Xwp21rg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jthoughton.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=DWy7bWTF; arc=none smtp.client-ip=209.85.219.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jthoughton.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="DWy7bWTF" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e03a694ba5aso439258276.3 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=vger.kernel.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=DWy7bWTFhe/KIs13Ct3SsUbD+oF93GBAGRkLqujDeMAfUYNC12irf/BXHA7+3oTOx4 4Fpwge7jjhMAE+omuvcFPSamQ/Q+FundE26AFNHWZ+TyrVhXwEZgu5nASqzHrPmZQqBp jwNYETChRg+l0zLY/dduL+CRaYYdWCfHnTVFR/hr7speNuvW2qFeNXHgpjzatRI4Hljf B980UHG+z1+ukIYI+53h60THD/CUgre1E4gv0S5dmAI8kNFLoczV0HLOJIUu20HV0jhV nThjBZO8gAQL9cgx6a7vznQJQpk1IBIkPWTvDYbETqPveDo7579rtaQRFKFBoOvpoBuG fhig== 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=YplTrHWf2UEAuAzBLGLibImyZIv5F5nYuQV760XqtAS/+reeZLthE7KHtuUvIcF3Jn o5EXqhzTesjwqtHCU4TDAfiA4/2JU14nql7LRkc3LX0psFCt3EhtRtlrM6ZuwpmStqnq aLicfjUMHNlxWZHxWBrEJoTqFiP6u2OUjf00QANOjPGDYmwb456AplOy6tH5uBEjVQZ7 Df7nBs8a1Q5DsUPfxZ6G6OZSeXJ5gSlcqMCL2tIA1Em9fh7W23Gua6jJTIoruYkDFjv7 z+yeg0MYWoFlWoCRKFPJfVP/O/AV01xeJGI6AQDRKVWtE/ug6+oDQaLYwDUbAe/Bvqd/ /2Bw== X-Forwarded-Encrypted: i=1; AJvYcCW3stGaLxb1UnG5uNMOHE/wON9LgCzXZiX33+h0eGoiY8BmiJGpbzrpfmSUQ4o+HViQxAcMeQO1NujgkciqBgO8XkPq X-Gm-Message-State: AOJu0Yx7Dmcyhp/ieuzL/vvraTLpbn01tIRSkZWOa6dtVk+Lc/m6V44u LLq4esxrJg78vA2iW45Jv/FgZMHcntctCa+IwUxWWW2I2+Fu834J7DGa0NHgLNsz2mGgo8z4b7Z SCQ3zs80w4DupWYfenw== 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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: 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 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: 13729862 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 088631662FD for ; Wed, 10 Jul 2024 23:42:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720654971; cv=none; b=V02syxOfbvr5OernZ7MnrapPHrQfVKgsuYPNBzF13Nx+Hk4Du3ThNYtsmSsaFdsHH2MyDJYBgO0mgjWnUEtBLVEaGfdo4+8XDnk28rBR8T4zAM6rB1Pw1PHU+P4jkOWYcVQza6Wmzd+SUnmY8yY3PLP7b7Eb0a4GR2ARkQS5dAM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720654971; c=relaxed/simple; bh=IjXnmRFVjtDWx5DdntFXxWYNolKMFz9eFCdi9O/NlGk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Pt6HzSa9bJDuLt4NMK1nJ86aoSCTeoXAE4Dkm7RcgztFbfQ1S4+FBCVGeZWb4dmNE85+bTIgiO5NucSf/SBHltAjTMEscCSdNHyz/Bpxx/2FWOYVnPiBNITFq/p11L3MnVaoV+nv+R4Auu6VYJoPAwg1GFP/gd7MG/qvZNfkQ1w= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jthoughton.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=1Kxcusgv; arc=none smtp.client-ip=209.85.219.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jthoughton.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="1Kxcusgv" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e03b3f48c65so455157276.0 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=vger.kernel.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=1KxcusgvBBHitV4s1u7TQQ7LVMI2G0L8r5abXPHb8fs/1gW78CwYAl/ZbKiX/DWkRE S3LDZ62juwvgbxFm3VWxRQLQ7bmsah08T7z9aVVz7siJtESBEU0YU+tEuFp/EsRKp+I/ 3OUnUK2VbacifgaEuqWKWxPV6VaXSLQu/Ry+LeBoeGskfucFtHQqKepS+9vQ+P82fK03 24qF6bZ+HdXGZ0tKQ5hLSeYFiIvG555CzZkmhJWIIpN9zhyPA0rI8DmXgD902LZnbEo6 V/FumXQm2R+oAqqNKNTyUW9QOomG2shlLqFypw0TNsn1mT+wrQEpYZb+feMpe3Okxvxf Df8Q== 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=xG9WraY/xgxiiwsA/LoN+hcsWyDvZ0t1F0V2lr1SaT3RGtb9Rcd4Zkm0R7af701FWH eR1tatlkBz2VWcFr/YD3OLsc5MSHfT0JcZAziWbo1O3gj2jnq0A95CkO1+un/ZUsGLDk 5ON4eoWS8vKAKWThxlwR4f9Aqv+JOf2dxzRIWMPw7BsDEoSdZEIRKMKO5pFHxSBZ/Htx PEiUrhtEuuSprWjYi7RLXOwLRnHR69aXC8u0FKTzaZpkVLwMfqdGpE+NKAK5j1jm9+x1 /mzfUzRv/ww/NDgqgSX6gxOpyk/ACcwoyRCOTzFYtWm6O5fkgnlKHu+W9hwmk/QY1ijY QpZQ== X-Forwarded-Encrypted: i=1; AJvYcCWTLp9gaEjuPkXPY50IF/BpH8xzF+oqauFSyXxXIHFzCqk5euw7MLwYoXde5qJYTg/hXtCNZTGipGajC99jUU8g0VLi X-Gm-Message-State: AOJu0YzmXqW4T3lIqmp/CBJTSbD+5muyzhHsRMi14dYg9OjdkGH8EId3 UXPh93mA21oOomMTZ+ZgNAHdm7PMubLY6PCtZRgtiJ/Y2QyeqVajREmVoar4OSpnQXJU9F9soBb mmwYBZctx/ti6vn1jvw== 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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: 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 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: 13729863 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A304F167D98 for ; Wed, 10 Jul 2024 23:42:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720654972; cv=none; b=Rgl8IcmnF+EI1WMRwRAKIdoecV1iDMlpE5WMWm1pWZj4d/kAqKhucg/zSxHB3HEmOPYH+wJ/YCDKF7sdKtA0AuqL+rJpVfOYHtwxjP5Cg4IPHOBXapt+cNLqPzx0Evk1OmHvv40AVDulHuH3Y1RD+ZOMjmMaQKuK/xZnMRjnjBY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720654972; c=relaxed/simple; bh=bFngUTjVBtg5CbyG25RDmTc6wZ0g8Z4dE05PWolnMuA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=u9PudOaAUbYBdzXwYwrRnMvFM7nE/6CBTa1je5BjbKzaOGL2O5eGSTRa4Tk66cOKMPp5ZJK2Xtk80ElqrUX/KLsMoagwGRIrusjNyN8pVvWmoM1hgy/g5t5OtSZWvigbOu2IEzicx8LoWZZyqrA4LPHsB1xVuojx5WdvSTbp1bw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jthoughton.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=NraxoE88; arc=none smtp.client-ip=209.85.219.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jthoughton.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="NraxoE88" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-e056d640abbso495589276.0 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=vger.kernel.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=NraxoE884i5UGeFLgyX6LV7967m3VShM5RJlHCzHa1BM3Ym/+8c83CzC7IvyEpbJez 7udLHgCXg3ao8cBo31uGB2qPMrJE1WjFX7XI5FibKYcJ61K8Sa9qMuDgouX0kvZnFQmD qMCdCCLZciixrn7zTiTTNIfSjaKVjL8imwQEF0h1iJPrjxkJybpE+Q7yzSWS5ixF9UmG MZtuaM8JLBvfieo1nWP5Wzf2eVf9dWGUAtDEIQQSbHOUUOurd5cuj84uR2FPDCzOVo1Q nuwy7CbjpJhIkMuTCUo0jTlUUFiDE6gAcJRoM4GftShi3884dKF9Jz1RC63GHwKVSfaM 9dDA== 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=SaWtUd4/EtPekwjVIHC8IHSLOZqSW2xXvaqykp3HZzWZg+POdSQcMt/uMOH3wgj2tB h+dcRfGqH5STO8ixWGz4cg/X2T36OG/sF+zG+Nx8fTM15zlOnv5uMGTnWMHuemHq5tRb Vz8AGsQVp9R/WcRKur29DvS7TTSI6CCs8+RGg6pS00eRWOYs02dwbEp+b2BIVifS3VWW N7ahrBPixymhaGDYnd+BrLOxYDWlswbN5pgkxAs9+glpSYue9WWUnhTSM9m0KkHaowP9 J8VotXxB3CxQ7NOcg6fHMgZPd+mJHCmWqslFPPosxQ2Pmd1XPXwUK4CqR/FKudKcEbYW bSxA== X-Forwarded-Encrypted: i=1; AJvYcCUxymrKO1u3EiuYVbcxJbhnsZA4evs6UpS+yto3wSSLJRV7Dom2NT/AYeJLDnrnVji4ieYx2HEidSs0U/2fI32vmbv4 X-Gm-Message-State: AOJu0Yx516hO0JKZ4/cGMaXNKyB/s14pSZmQqO4XNNxvXQvuF0uH2mQD KgQGiMpn4fo3VkrOSBlIsxy6HLeEUUS6tGvRQS++wwD3b8hjJ/SPemrILzVRKJoAeak6yFBcVQm +SBVnYiunaTxUk8+AXA== 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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: 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 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: 13729864 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E981316A95E for ; Wed, 10 Jul 2024 23:42:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720654973; cv=none; b=UCUqDbJR3AnAyeipGF7MnG4s9S3wzLR7FMvKQLH9t+vYUN6HuCEG2cm8iaAKapW1tLFnNTZtVWINIKAusT74IrbUXaa9Q6PqR6pgsiqukXpC/lWwgQIwvvnLDw/T7Ts2x3Xsloo12OgczPI46NWF0xjrnKdocrjSaTgiJvEBMXc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720654973; c=relaxed/simple; bh=u+awbVx9aoPKS0xGDQreI8aQ8m7YRSIdZYPdiuLJprA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=twkceT7Zsz5mp5W9q7QyF59f/5U5J2/gdSwe8UkCNULmJF+ULtokkqSP7caR5QrIZQU/2hZGZNBw1jMRycz0A0XyRVmjb6/X1zXofOpwZvuGt/rHyhz0tUOg9QyxanxjPnPGRR31yrDPUWXpe915/ybteilQUu9qRgElNCOvmRA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jthoughton.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=e6NELnNh; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jthoughton.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="e6NELnNh" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-653b4b01872so5683667b3.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=vger.kernel.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=e6NELnNh7DbSwWwtHHEldQJH7sr8xKLGh90dcELCZM/kGuCX6P9o53v/cw1f2kDu4b ZfUSQ5xYn+DP5swZ5BsTWD6leZ5xcV3edNNxeM56kih9lKIfKzuPMfP3AplfhTZ9M8xc XTsgy54wbezX6YiaA+3FcTyEcZeCmbgBvIOcU0jp4srrEoMRIwPtWUxHGsYwKfOnYPS+ u0qdLlyKasM4cdWBA8wdzKKFlq2uFHP7KRZwiKwXohYbP9I2jJRtDhy/aOjnn/KOtWRz okVqjQr+kEPPOVxGWZWhninG8zhSy92CU8wHG+ifGpWL2auqJsCVBRwFH2XKk1+ife46 JpzQ== 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=wNhSWtHnggy6HFVmeublaCvv9NLyhDEr+1FR/WD/txgBsEymFG52ZCrQqbcbBIWW7d 47isHF/nMrhIKbjC4M/5of/DiZsnsGHriMi7FWfbyZhvL7px16Fr0ynH+8RzkaerGVNn 5XXtDMMogLAcQNfl+4OsVBZLXdiHQhMkRceXb5h9IH4A7NJnrnhZL9x3RMFMc5NVBo2C g05RZofCCMtPq0juccGE+Ogyv79mbmaekbhg1f2w5CjNb/c6evcmA4e1aUHeNkLavhXM F/IYn+gxBARAhhXy5eJEJJPqV2PzejS56dR1edMpKwwT/frl9f4OhJXEJYsQZxr6Skdr P/AA== X-Forwarded-Encrypted: i=1; AJvYcCWE/ZPnUo1p+riNlUwxuZt7lOxSiK3zNFCu+59dH1zPOwJCGqec3tcnSHyQKEqroYTmHNhGlMCFM+Zjbpt92o/EMS+q X-Gm-Message-State: AOJu0YykO0jth7JtpEKUyRLYphyjWnsVmu4pKs6fLEwIiGofIpPqiNGY SvqGdfsB2QBZoH66wmU5lDrNKSQ4MWOB3/7TnJpvVFvb9fhLwkPODeLS6qsc9AG5pIzbdUrjYYy Al5eYyTwqq/W7FxqJbg== 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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: 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 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: 13729865 Received: from mail-ua1-f74.google.com (mail-ua1-f74.google.com [209.85.222.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B0362158D9C for ; Wed, 10 Jul 2024 23:42:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720654975; cv=none; b=YL2QcMGslBMTTiWBt1T6ANrXGBDZpBDaPfayDhHbwxd9GJ+LUIjVknKMaM5iwPOHfPMmVDSnC6TehQdvXgGFCPTKJklCYAQGAINR8KWn80SC5QYH38QGIeoTPAXhbxrQljPXmJfSWwarugUilfrgQrWtT7HR0iwbLcDEL+t2Z1U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720654975; c=relaxed/simple; bh=eTv+8mtXv9pcwUVaOkiFS2pyxVlBIejV0U8K7ZFMz7Q=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=eo5ZXDI6eEK/oPu04Sj4Fi2BRGDdPJOPKA9NiqwG7yYdehh1g3LnwJFW8v85oguK767iN/DIEpBtasgKDsLdMw37tcQqcACJkfwISTbUKEALHu0Dodj+vwTLy/M9tPyVGY72ihd0w5v4VrVpPJDKNria4Y+lDX+rmge4+DhvOhk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jthoughton.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Yt+IaSSB; arc=none smtp.client-ip=209.85.222.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jthoughton.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Yt+IaSSB" Received: by mail-ua1-f74.google.com with SMTP id a1e0cc1a2514c-810558cc76bso232286241.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=vger.kernel.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=Yt+IaSSBMbGS+IafqDzS6nVUHKzASFutAlsF8viGS8g8/0qK7zNvQ5Gon20P4DJij9 Xp3+z577g3+G8sI+wHNMJLlGWB+72IslupSJ3/KivD3jZJdlI0hW0n9omk2FDFeYNXiz sR6gBeUg6K0wDk4mbC9oF8Z9TaAPYd78aiq/dNDmrNkXE30o4ECHwfl3Sz9LTejMoW/Y xW0EyEroLdkQE2qu7ITqjEkjudpCN2oOOi+Zl9H8CKluK38kJZIb4zsARs+ByEB3y2T3 86gK3HMyTi9byNbDVFuTjNTIfOApqyPih5qF4CAlqr7zVZ9p4JW9JnpHh012jrVpRNUg /Bgg== 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=Jk+X+bXQD5ReDp44qmqQ1WDIrt45eZa239gE6Kprp5FV47DMWOIR3tqxJSloV6K53A EA2PuVDgQjA+NZP8sjO8fcMmGmom7YnnRE/e8ODCf5TZObP3c2viNbQlqCO8Wxkmzjh5 /c6B2nqoaOUG59oC52vqmewDRu9gusOYwxI4KsUkrrZZ46aSe0jLky8Mlfcqki20KJ+z Lp1yNJnHmqJjmNBJdvJ1ETxHrJOYNZfH9EpkHBhdyvKQsWrirSsqgTXYU1odoi5KX+y6 lFM7S2BxAYN7qA3aJ4j2akCNFDkNOdItU2Xt0run631hnHpPth0nQJd3QVwm8Rq1hUZc 4c/A== X-Forwarded-Encrypted: i=1; AJvYcCWgfPfbx48gRYdbXlQNRMBwz4JjG7wdXOLV0XFFlgANqbTocTyu/cHUZbW2F5/TB3mTVpFHBIHNOrly5PbTzxXRoFKo X-Gm-Message-State: AOJu0Yzovis6thbk2p/lQBaVn4oOWxcjHJOky5J+XielGxQ0REw8f3kw 9JCNIM0JsKvToQAPfr0MGv55zF+J11Re339VkXXJkHd9flwwu8AvGJlwZwjq00zdRKwfivmGsSw 1/psjor5dE2nB8ljTaA== 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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: 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 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: 13729866 Received: from mail-ua1-f73.google.com (mail-ua1-f73.google.com [209.85.222.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1784B16C866 for ; Wed, 10 Jul 2024 23:42:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720654976; cv=none; b=L99c3/H+E2yf/ep0MuW2jT8tKcZ0uhJDYe64DlSEzIkKPlPgf/hiUkFWERUYEKfCLILsFkbaGBMMmK14ls0M9rF4fTgdVmhso+o7BQraEnNbs2WdvPdqaCk7rKAVu3t5mGMoswFlV/RYJ9+yPskDuEwAg/j+rMCi7oNV8ltxnxg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720654976; c=relaxed/simple; bh=9nGVvjTtRrzXeDKQT/PGApCposmumXI12BdKtg64Aj8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=sr0MBajdDkIvG7PwjziK7ZMJZz5b7PbNJe8Uib896oF+5WF32ycObu9mTUFboxSxXOHgzuAM4eFggLn9/lxg68HkkKG4NzSEEsRD4Br35hmCqp40DUD+aXC9mWDSUIiOYVb444Gs3novJ5BBfb6FKoTVeIUaIFHl2dJgv+QuGSs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jthoughton.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=gDXVRsCx; arc=none smtp.client-ip=209.85.222.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jthoughton.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="gDXVRsCx" Received: by mail-ua1-f73.google.com with SMTP id a1e0cc1a2514c-8107b03e1ccso56384241.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=vger.kernel.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=gDXVRsCx4zAb+aXQVU500pfuxdZC1fBEA1iLF/nd/5kaJzmrZvl9SDY2pHegNlDieX hd8gLixD2Jl+++LLsALferKZDuhbA1PzIJmVwFM8Wxmp/scWJlgpfU4AOkD3t3ObKRst OenkqnPb3tJdCOiWX2bVSr6jwWzDTB9i8nH3LgleIWgOGXl/Rttrre+zbPV0eS4/jV7t GxzpanKaknmyn08V6EwDg4rLCLeQnOEpfQJwuOgWAkBzMg1JhsnWXn8AFPs44QJP8Z5X 4VhbUFc2EcLF1yjqiDWBwv3TuwVIiYFgdsjHaSSKoewzwiQlIBfCLxwee5mFwULOgDOb 40LA== 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=Xu2CQoI/gxei/CoNrq73ZRARbaDwTv6+bgw5SWE+XwvrFPOlxBApVKj9/s4R2DGZlS l6PMWlMG+gDQM7TE/A4UAC96MzOVqWjKIV7iuMsA0c5ibEfjWxCtEJ/MGAzWdYr5PyeX vtFMgYSk5REFTByO67yW8vrz9OmQX3yCdFFjo833z4tGvNY9WYFhRA50zq3HXfS8RBi0 khGiQMr7Ba4Fekl1L9Eq61LnWLBSrB06KrFpYdWoicidKxpztlVDJGmVCknDCdjckb1M y4DjYDs21FcBRIQpR+HwbpumiLrgteeJeSSEgCZPLq1bYsnq9HTP67O0Thpk4AN5BRMr S0ug== X-Forwarded-Encrypted: i=1; AJvYcCVH6IIg5R/qlihv/qZ9Y2yEhfFInuUWyJR+6fBZb9nyhsecZNdmC+waZ/1cdjiHEILOH92gFPzMSEq4x0MUwDTCR2Kk X-Gm-Message-State: AOJu0Yzt49jcbP/tpoBPG+Dkz43CUyxQwIFxfe4h2Ixprvn46VAFngp1 z9c8czZlpK0G9IrGmy1++mjAimPNhThv3FA2PQSxsDaen9tlZp/F+005ToP+EUJkBiGiHR2Drgo Oj8UqKAB30luDvx031w== 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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: 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 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: 13729867 Received: from mail-ua1-f73.google.com (mail-ua1-f73.google.com [209.85.222.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D6B8616CD0E for ; Wed, 10 Jul 2024 23:42:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720654976; cv=none; b=ZU7ekY8lR95V92sfNGksBRuHAuYm1Ku4pEWXRHaCAT5CldZgMZxotmIuw1piOUjHyDNuwq7lmg0kSRtEcJhAcAvYXIhnE+YSaNjJlU3NECq+A6NUAs3EOll1Ge2AKyLN86+KrGlcsWz6P46fcC9+L0I8zOLeT6z8+DmO31LaIYQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720654976; c=relaxed/simple; bh=8593VQW+qY6IG/jYYpHCncdh+HTnZZx/ZSFesd4ewL0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=cJ1QfJ5QVvqtBZT6FJgj/WV+UmHq53XvN+zNoIOsBXB4NYMNpgPXIwt+hLIhUtBekgYCgI9CcmMG1n19vV7wRKl3qINBiSIcZe4OGde33wy4cfaaRIgstcYDLmtP0kEvFXqrM1aKSxpgtSXYhc629nREwmOQnShm7HFN4od64/o= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jthoughton.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Xxpa8ncT; arc=none smtp.client-ip=209.85.222.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jthoughton.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Xxpa8ncT" Received: by mail-ua1-f73.google.com with SMTP id a1e0cc1a2514c-810549fde5fso65814241.2 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=vger.kernel.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=Xxpa8ncTdgyuNQ996Gw0ta5/yYEut2AJIhDg3+yQwm/UPIA274rODqe0+bAvn++daV z7pi3hj55R3y+anqcc1egnqgM1VMRqWyqqUXTzM8b04EKorwb2qPzLgmC2S0VOer6rEM uvewnx3ThRcFoOhqtBy9P74tgDmnyIlu+hRI9NiGwBP3hx2dD1l2Kf4MkfoiA9nkMdmy b2EUeR3mUD+MdNVZ5jpEZ4S4W/XYdjha7U1FQuxb1YxTdERolyRmzr5+yCz8Y+9tXEO+ jYEPOhQ6Xq/JHeaih7VRmfmZJsmkJdH122A6LrAUVw5ULn81szmZKaXUkiwNmeAhOaJH r09w== 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=ABmF5w5pV2bcZsKlUTtFZHi6OcEQhG/1JQIzMM4MogR+c6L2n7+efaAS46Yhv3jCCG iIeov+OfLa6zLtuB6Kq0fWgRkHqyiB2B6Q4IvmisSLGPra5VmIOwn8Nw4iZyIUFLwDdF Mj6VnX23od0QAMx+IxLEEAXGbFMdPrXB8cvS4dgy+6tRJQQvYa9CUvd3M+Ojv5Kl4rk2 o8SIKwY7F0GpCdy4SFXiCrm+7yWwWdrMHPD6Wy4fi1bH0enY0T2rHhuK/dDQDrTBLpAf ZHL9o6kaz/3iTMDRYYZWIpGD2TgTJxtXTFAxqOjlCwGdq4gZzTZbzIQAkYrjrjgSseIh X9rQ== X-Forwarded-Encrypted: i=1; AJvYcCVVcMczCm0wj7LaxwzYN++3TMUl1pgPUJlcJ6QGQ5msN5M1uPJUYN30zoCchg4RmKEug8cVo2FrCslOxvlIvN7FWTy9 X-Gm-Message-State: AOJu0Yztu/6Pee5dxT6P3sifx5WLNW6CnZ5I4FRB/Nu5IhZ52zuL01cL 0Z9Rv63Db72ot6/O2MPtwN7v8qCm/ul89au9lnnfUCR/XLOoA9C2AK6mXN7b/ts3ykfrwQOixUn xSdQJvMEzNQDQsOuz4A== 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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: 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 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: 13729868 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9E65D16CD2C for ; Wed, 10 Jul 2024 23:42:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720654977; cv=none; b=uDadeTzWgXThgOU21hHXc1DvYmWbspGtl0p1mM3JEQ1SHUMs+Oje6SLlSE9F8m9eB9izLx8hHOCSw4EtYNu4PiV+rxbYQqqCBvtmNN+RFopITKXqiSAVHY7/nqzvSD/K+0QhgH7H/IdHt0JSc7LXl1pn8YCY+R+xkEN3p8RynD8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720654977; c=relaxed/simple; bh=eBdL3QVT/FXe2eEdF9QRtRlL8ltbcelrbHtqXfp5W3Q=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=PuVYnUq1wbUL5oBC2RxfxQmiN6j1PDZqloZdK7FuUx6nEaOQZ9RSwE3nOa9PN/4nWjh8d7gmnGnGAA7stHK54gFfPB6tEOfV4jAIqKkMt+8jIrXVSK2aqOGbYN5TcxSJaDFG2IYgJmFeFdITDDWwch7zxdLWJXXerKQv8vZ2/GA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jthoughton.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=olFEjrFU; arc=none smtp.client-ip=209.85.219.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jthoughton.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="olFEjrFU" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e03a59172dbso416090276.3 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=vger.kernel.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=olFEjrFURi3qSy42gzywIicXy4QwpEGsf6PYG/HOpvjB3nyrBTHPoIl96wDyMAhzbw 0FrEFUj1qFhhe+y7CRVpTXqKxRMeldvuJ6bQ4Y9x0WWvtSxchu3YLvEvbpDfTmbEPX/Z fm+5PbxW721I2b+4cARVQXSh98dMAjDipUdSFC44cW2eQ/+rzzxjxuelRiEhZGNic92T J2xvoQJD4qNMdgdJo8nbmkDsuJSKAUewX5VTOMtL0bADcTCCNK8gjRDkyZxlDJ+AmBOY JDcYLcQU4ayrwMUKZbM6nEGDJLNnK5aXCX3CULBs2IcOMsxyLwE9ldRDJzcrKFxa6iO/ KvIQ== 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=J3uR3JX1VRcgmThzv3ivW4+H2JgfvZHZE6NsZ+vWPU2fVKBaTq+HL7LAiLP0YexUFw 27VAYt9F2XULhx6Q/4GbApS35IxEHpZuWz0Xuztd54BltFhIAIUlvltQ2j+xyjrB5e5N FkjpvJytyAGFo3ytqbxWyyU1ZQyWEcnjaTBgcnBUqiLm0GOaVtuk4JJWuUAZQNbc82Ey NMVdwi3Br++xteqdjjkC6Dl03jbFyc0Cm6XBKOq3j+r9HyiAotOde5juKAlZgEiJIoCp 2YZdxVxWrAml/J19ueeR/8gwsGGtzAZj+mkm0CtQM6D8I/yiDyO6Oe90UqXWK9FaTL3j 1L7A== X-Forwarded-Encrypted: i=1; AJvYcCVm5EV3vw1ceC7f4LEF+RsyYN8+QZESEagz0zMs5/BORbVQJuqwqwvO2ZDiclO3j7UTm4fW/eF232q1cezBlxgnJxMk X-Gm-Message-State: AOJu0YyRFSJJJZJzx6DroH9PP67oAi6s7RSC0DCvmm+yuuK/4wPASJ3f qms/+i9GuCMlp0ijbjtMiVOt0lPgDacflZ+QLxzmr8Up13GUbtZ7FbhPgAq7XzQGPxlFiGB73t/ l+Mp37eSmBJkaXbSq5g== 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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: 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 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: 13729869 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 815FF16D31A for ; Wed, 10 Jul 2024 23:42:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720654979; cv=none; b=ZPSPN+f/AqF1lDnY0+Zb1Zu1SE17NhgZQh1nCLKW4U+vAgxiZGQOfqPGPn7YK3z9DOBGza551K8JQTdXa+4HcZlvyqpUzd4WwnkCXiMPKMqZbSLz/+BZ80WzGxAqpEo5AwgXRDnPgcIXkPfwXZMFn0YWSSovi7QJaoAOj4IP6kU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720654979; c=relaxed/simple; bh=iHtMvGL4vF0q+lVnlkIHLSBIYFMj+gNaWi3Gs2hOk8E=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=YiEQTOkPfUlMIGPAQ1VbkLJe33ZTHRg0xJ5duk7y2EPLelYz9mS283AwHeCR3rPBRTxPmRme9RXHPrmSxPb+ZtlWQICEFav063Rt4fF7infuOh5VMhLJaqUkdQXxBiAQCRtv576AhLfdqKrwfjXXomiphA0ec+TI+NsHuh8oaLI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jthoughton.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=SiNK7uLO; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jthoughton.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="SiNK7uLO" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6522c6e5ed9so6416417b3.0 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=vger.kernel.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=SiNK7uLOX8sumtAb7yl27cqs0OHIVqIv1p2Id4nqTJufixjPgISkGXCE4yOeXipwJU RsM0jTf1Turqukq2i1ej5FjE6ot7dx4aEql2yJdiHShzNbmcO9kwlCTpD/AmRj4FX2u9 hpsdi86UjiwTTU0lNHJwfqcoDCCr34iupDpGkn7BV+0lG1Wm5ii5YYe2fJ8rzDT1yZgu /P0TEKgctg0YDDqIP1C79/0pUKC6FJCr7c0ei/mvVnLXo5xj2XlJ/fWjbkvRjGsTkPEe G+Mu6jI3/SUEf0uBAxKpDF2GvWHTv5ARSJhPOPNSL7qoBBjafrDgQTsUm6yYizG6z3h1 VTbw== 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=Ftkj53gZ7mZrdBJJt3tiMWFMb8GL46hnDzwXns7MTXMDW25Ciue6X59sSYfXpAQdwf vfcBl+6qwxoz4aVSkVGQmmj4Ta/YR6D2EKTKR4oW0jAasPV93EavSWaYdUYiZZseHaZS kufqgrjn7iQpwDXvqTRyRxAVWjCSc6rzqNed0zFjnW9iaWILX4lHNnndsYZz3KwN1pje 0srVn5pGzNIN3DuZJGWNnIasFQUYrb8YG3EQ6wshgN7iMo3WxmB8oGogzM+uGiiz4xSQ pRIPGYZGNkF74xbW/vaYlM0KYopMAUyKU9OQO3iwwvI9iJgdgv58Q66B9lX3N1JLz0GK zjyA== X-Forwarded-Encrypted: i=1; AJvYcCU25yAnRw83EsfyX6nY0Fk5HqjqSb0icaDtdjP4HUAp4iFiEZEd+q469L8lyA8vxtfpwfYg/HdtYEcjV7MkUa/9vSP7 X-Gm-Message-State: AOJu0Yx46jGXUY0w91Bx974Gughjz93z2513pIB+7Fw9IUNfeVJbPkhe Hc483ExtAdzavGvpS0o8Es2ivTsSM/ZIdcWZX8pPKF68UUwK3aLONxMTvRVMpquvRQqI4+FB8dt o+LA2/SxK/Eh+JRoVmg== 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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: 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 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); }