From patchwork Wed Dec 4 19:13:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Houghton X-Patchwork-Id: 13894196 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id B6B82E7716D for ; Wed, 4 Dec 2024 19:17:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To:From: Subject:Message-ID:References:Mime-Version:In-Reply-To:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=Zrj0l+L1/XMejjcdAt6Cg23oGRx5OMh6hw9EKc02Jsg=; b=Uhtgp0Xds0HxPX6vqEDhjhYeOf R7VDaxewB2jfa6HQ8IQ31oQMXqQNXK0WmO4IHSwd/RDYhAfClZ5HprNYb3aXb2z6VADhdUiaiwicy CwwPEZnJ/f3fqlB6nPcun8yr6LMaUTZGKtEfX5efJ8B6ZDCHC0bU67qke7vk8JJLIn+e9YttnHuun JBkXDjiHt5lsLoRd2qDw3ll2oQPc+9fKH8AWsz4DU9xs6Ufu8JkkHuHzu2O4QXIL2UcvBUDbqoL3m d7D7dPuTcjVGleVD8S6g2RIdCt3/J7MLpEQBHDrpTN3Gw051snRr7jG/BJLFif7GyieFhqVI+yQQ+ IsXfCmFA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tIusH-0000000Dedy-0u1D; Wed, 04 Dec 2024 19:17:25 +0000 Received: from mail-qk1-x749.google.com ([2607:f8b0:4864:20::749]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tIupG-0000000Ddfl-0jLf for linux-arm-kernel@lists.infradead.org; Wed, 04 Dec 2024 19:14:19 +0000 Received: by mail-qk1-x749.google.com with SMTP id af79cd13be357-7b674755f8eso20367685a.2 for ; Wed, 04 Dec 2024 11:14:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1733339656; x=1733944456; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=Zrj0l+L1/XMejjcdAt6Cg23oGRx5OMh6hw9EKc02Jsg=; b=bbej1dcRpf9ru1HjUUX+dInLstD0suztd8pJjz9aBZ0AEGDj98DfN9b22rbYkoEY4g /ev8BYuGcslfRvBl1i0ckZQtouK52+wPxl2zWAv1Y7v9neEVM8OJID5iXz+S/O1R/NqJ l7IjwbCrKJBT9sUpRXO3Bxcfxd01cct4y12cPdAYGPeO/xf+PpZWy8BsuwBhyTJEtbb0 lAp1rmEKDfDy5mucHtSw/NLh+acbg4GbMIxpI+Xj+/e4hOw/6q3v8L+gXNoZH8AB4hFi 7NLG/kdsctknekJWKguxrBl8+ODOywPpML9SvuywlLomIFtEL4IxYfwBbvWOvS28xtMm E3qA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733339656; x=1733944456; 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=Zrj0l+L1/XMejjcdAt6Cg23oGRx5OMh6hw9EKc02Jsg=; b=p+Dus2iGcEypr/LagTmbS2ABAn1v8HX9aXKYMzrCtx6y3JRdZg06qDCB6MDxb8qLNk 2li1fd7jYTiK3UOB24rMHv9foRM/TFiUGt2D+/LDYfAysWA7ZVmVQuInPjqIplSF2o6W ff3wy6gE4cqeRc9IU4tufG16+vpQ9cA9kb7rKNwi4ajvV4CyELF7tQtoH+NdxIikyP/x aHDr9rvApr1eKtAFdS+eHXvcGG59VngypilrdOVT6vWc5tVgOFXQ3aIt8BWVJHj0QZJZ 5RxNW4LLouVj8GLpw0wDjUTRs4T9uEb2Lbrgj+r2pd0NN2msnkcspagOmJWUdp8H9bir DZ7A== X-Forwarded-Encrypted: i=1; AJvYcCUHCGamAUIFbwHCSEVmmvalCcr3iYu0pifHT4QUljKuQA3g8b3hSVdVfeljOM7YXePQAux3Njs6QDm15OUj0OAl@lists.infradead.org X-Gm-Message-State: AOJu0Yx9EwAe8gAXdC6b5qpEV2XI8oPuQSw0Hm2SAerRRVrMwpFX+iBy KFjYK/rG+Nuoh9yCMeh/Ox064DMFunRczsPu7O1hBiqxQ4ufpy9NgtOduE+XhWpk+8KKMzlE9Kq VXvroM6oEHKTRe7CZDw== X-Google-Smtp-Source: AGHT+IEsH4AhFxEPswA6HSpr3bvQoscIWPYhswjH6B0ZmAUwG6NVAuJoyTvkHIFeYc2jG+fxyhqARW7A5Wn5aFvO X-Received: from uabib8.prod.google.com ([2002:a05:6130:1c88:b0:85b:d490:f925]) (user=jthoughton job=prod-delivery.src-stubby-dispatcher) by 2002:a05:620a:2685:b0:7a9:acd5:1b21 with SMTP id af79cd13be357-7b6a61cb6e2mr738681685a.50.1733339656100; Wed, 04 Dec 2024 11:14:16 -0800 (PST) Date: Wed, 4 Dec 2024 19:13:36 +0000 In-Reply-To: <20241204191349.1730936-1-jthoughton@google.com> Mime-Version: 1.0 References: <20241204191349.1730936-1-jthoughton@google.com> X-Mailer: git-send-email 2.47.0.338.g60cca15819-goog Message-ID: <20241204191349.1730936-2-jthoughton@google.com> Subject: [PATCH v1 01/13] KVM: Add KVM_MEM_USERFAULT memslot flag and bitmap From: James Houghton To: Paolo Bonzini , Sean Christopherson Cc: Jonathan Corbet , Marc Zyngier , Oliver Upton , Yan Zhao , James Houghton , Nikita Kalyazin , Anish Moorthy , Peter Gonda , Peter Xu , David Matlack , Wang@google.com, Wei W , kvm@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241204_111418_210141_0174B3DB X-CRM114-Status: GOOD ( 19.28 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Use one of the 14 reserved u64s in struct kvm_userspace_memory_region2 for the user to provide `userfault_bitmap`. The memslot flag indicates if KVM should be reading from the `userfault_bitmap` field from the memslot. The user is permitted to provide a bogus pointer. If the pointer cannot be read from, we will return -EFAULT (with no other information) back to the user. Signed-off-by: James Houghton --- include/linux/kvm_host.h | 14 ++++++++++++++ include/uapi/linux/kvm.h | 4 +++- virt/kvm/Kconfig | 3 +++ virt/kvm/kvm_main.c | 28 ++++++++++++++++++++++++++++ 4 files changed, 48 insertions(+), 1 deletion(-) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 401439bb21e3..f7a3dfd5e224 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -590,6 +590,7 @@ struct kvm_memory_slot { unsigned long *dirty_bitmap; struct kvm_arch_memory_slot arch; unsigned long userspace_addr; + unsigned long __user *userfault_bitmap; u32 flags; short id; u16 as_id; @@ -724,6 +725,11 @@ static inline bool kvm_arch_has_readonly_mem(struct kvm *kvm) } #endif +static inline bool kvm_has_userfault(struct kvm *kvm) +{ + return IS_ENABLED(CONFIG_HAVE_KVM_USERFAULT); +} + struct kvm_memslots { u64 generation; atomic_long_t last_used_slot; @@ -2553,4 +2559,12 @@ long kvm_arch_vcpu_pre_fault_memory(struct kvm_vcpu *vcpu, struct kvm_pre_fault_memory *range); #endif +int kvm_gfn_userfault(struct kvm *kvm, struct kvm_memory_slot *memslot, + gfn_t gfn); + +static inline bool kvm_memslot_userfault(struct kvm_memory_slot *memslot) +{ + return memslot->flags & KVM_MEM_USERFAULT; +} + #endif diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 502ea63b5d2e..94be7e8b46a4 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -40,7 +40,8 @@ struct kvm_userspace_memory_region2 { __u64 guest_memfd_offset; __u32 guest_memfd; __u32 pad1; - __u64 pad2[14]; + __u64 userfault_bitmap; + __u64 pad2[13]; }; /* @@ -51,6 +52,7 @@ struct kvm_userspace_memory_region2 { #define KVM_MEM_LOG_DIRTY_PAGES (1UL << 0) #define KVM_MEM_READONLY (1UL << 1) #define KVM_MEM_GUEST_MEMFD (1UL << 2) +#define KVM_MEM_USERFAULT (1UL << 3) /* for KVM_IRQ_LINE */ struct kvm_irq_level { diff --git a/virt/kvm/Kconfig b/virt/kvm/Kconfig index 54e959e7d68f..9eb1fae238b1 100644 --- a/virt/kvm/Kconfig +++ b/virt/kvm/Kconfig @@ -124,3 +124,6 @@ config HAVE_KVM_ARCH_GMEM_PREPARE config HAVE_KVM_ARCH_GMEM_INVALIDATE bool depends on KVM_PRIVATE_MEM + +config HAVE_KVM_USERFAULT + bool diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index de2c11dae231..23fa3e911c4e 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -1541,6 +1541,9 @@ static int check_memory_region_flags(struct kvm *kvm, !(mem->flags & KVM_MEM_GUEST_MEMFD)) valid_flags |= KVM_MEM_READONLY; + if (kvm_has_userfault(kvm)) + valid_flags |= KVM_MEM_USERFAULT; + if (mem->flags & ~valid_flags) return -EINVAL; @@ -2042,6 +2045,8 @@ int __kvm_set_memory_region(struct kvm *kvm, if (r) goto out; } + if (mem->flags & KVM_MEM_USERFAULT) + new->userfault_bitmap = (unsigned long *)mem->userfault_bitmap; r = kvm_set_memslot(kvm, old, new, change); if (r) @@ -6426,3 +6431,26 @@ void kvm_exit(void) kvm_irqfd_exit(); } EXPORT_SYMBOL_GPL(kvm_exit); + +int kvm_gfn_userfault(struct kvm *kvm, struct kvm_memory_slot *memslot, + gfn_t gfn) +{ + unsigned long bitmap_chunk = 0; + off_t offset; + + if (!kvm_memslot_userfault(memslot)) + return 0; + + if (WARN_ON_ONCE(!memslot->userfault_bitmap)) + return 0; + + offset = gfn - memslot->base_gfn; + + if (copy_from_user(&bitmap_chunk, + memslot->userfault_bitmap + offset / BITS_PER_LONG, + sizeof(bitmap_chunk))) + return -EFAULT; + + /* Set in the bitmap means that the gfn is userfault */ + return !!(bitmap_chunk & (1ul << (offset % BITS_PER_LONG))); +} From patchwork Wed Dec 4 19:13:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Houghton X-Patchwork-Id: 13894197 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 383B4E7716D for ; Wed, 4 Dec 2024 19:18:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To:From: Subject:Message-ID:References:Mime-Version:In-Reply-To:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=ifppD8XOVrlj0Y18bnChdCi7ngFpMAIfTyMrOgFm5Gc=; b=Vjfgkt9oKn9kMD12tij17Ns1dC uiEJ+OsGKKYYOtgOwRlwho8Fd5IG9aFoysfDWee3neX+c2kKsZLk3hqK3euByN6BMZMR2oDBKGi5l yy590Yd9GG+Du/7auqnP3B2n3CRWdMPpBiAcmSExeemKtDIE92F6mjVQnL6ojhD2Nyr6X3WAvUM1S Xqy57SN/retXLgWFqhvz6T6kgNADqpU4fa75cJjwzWZ6KKRkPebj8ws88nm0LeeEgttW8qJEoQUQW PFXDMRCV52vjOeNqq5GePulT8aMtC9/EPr49s6cOuPEYyaoYWaJX5iYISdVocyljJUjvGAeeIgZC5 2hNuJFYg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tIutF-0000000DesU-3vLk; Wed, 04 Dec 2024 19:18:25 +0000 Received: from mail-oo1-xc49.google.com ([2607:f8b0:4864:20::c49]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tIupG-0000000Ddg2-1qMu for linux-arm-kernel@lists.infradead.org; Wed, 04 Dec 2024 19:14:19 +0000 Received: by mail-oo1-xc49.google.com with SMTP id 006d021491bc7-5f1ee87f67aso136491eaf.1 for ; Wed, 04 Dec 2024 11:14:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1733339657; x=1733944457; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=ifppD8XOVrlj0Y18bnChdCi7ngFpMAIfTyMrOgFm5Gc=; b=1A7tH3iuBJsZNVd1cV4yyt5AAmFXwvTbTsvUYwTXWeiU97B+6p+zZS/L7q55oZQ2c7 hqRAJ7t6PbbVB56kZ3UeAL1zPXBzNnw84k1qD1TFBuzp0wKvAl/niEcwH/lGTjvVdHHv bk6asrYky+UDLzJO6v79Tba9aYjnWzpbD3D6l9epLOfkwuDlmxBURoXO8X9SVb6nQ7Rx PMC7shoOnTVYO9GAl5Wc4YeIGCdeccLOy29KxpEiqDYjrZN22MBRYYc1OSjoxvSrAGAE qh5nAzPTyEMcUf6R3igeBrm+0wvNl75C0ZPfRzXtR1c+PKuDRJs3AZjTiZ26LrZNgg87 WHig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733339657; x=1733944457; 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=ifppD8XOVrlj0Y18bnChdCi7ngFpMAIfTyMrOgFm5Gc=; b=T5yW14Sw3HV1snmcZQMMmKCZWs0mm0u+QQgxIPbFMfIkcoFE2R1NDcISoZw3mK00F2 0byfRRpCY3OF3otB+D4YpEK0ho9jSSL9CHSlDPl35Zuc4MtRkOiHtIFds0LxC2+QfVOE bNl14QmyQtJWckJdB3ar5F6MQccdOgm9IkZXN86Xz5UoTFwfRiyy3DXb28vHbXbxk0td V/AfkKPbyTWAs3QaD88wjoQDMYkQw5qsgPEROnPT1dSpAARxF9qXar4/tc94m/C2Y0k2 stBuHW1DNyVCJgh7dRimmchWF8ffWHw2g5lekGR3Y5zwuS6xbPVYmQ+osiGZMN2zpyWZ yx8w== X-Forwarded-Encrypted: i=1; AJvYcCU3EX98mmUbNe19bKWRX5hzFAdTblhsZDmcNe1LpcocMB4+FHMOMFOljYKcfLNfdupDeoV8vYPAi+GR1BntDqNC@lists.infradead.org X-Gm-Message-State: AOJu0YwgNxBWk6f3hoKOkKmUZpj1zf0xCONiMf1Wu3PFV8sk0nlflD1l QD1C0kkrpyU0ltwPCey5ttLMR3HbPMnfR6KtWedOVZI5k6TTILUBhCdUz24gKgyhc7y8zGliWP0 nKELxrY2jflKZFac3Zg== X-Google-Smtp-Source: AGHT+IGqVM1yUneXF0DiEvK0H0RPVa3NLvh9Vv5/V4X8bp9PJGOl+SW8n3O2HwvzvAHA/QzfxuBonL+2sIMiSocO X-Received: from uaf26.prod.google.com ([2002:a05:6130:6d1a:b0:850:15b2:33d7]) (user=jthoughton job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6359:230b:b0:1c3:94:8ffa with SMTP id e5c5f4694b2df-1caeabc2133mr709229655d.22.1733339656984; Wed, 04 Dec 2024 11:14:16 -0800 (PST) Date: Wed, 4 Dec 2024 19:13:37 +0000 In-Reply-To: <20241204191349.1730936-1-jthoughton@google.com> Mime-Version: 1.0 References: <20241204191349.1730936-1-jthoughton@google.com> X-Mailer: git-send-email 2.47.0.338.g60cca15819-goog Message-ID: <20241204191349.1730936-3-jthoughton@google.com> Subject: [PATCH v1 02/13] KVM: Add KVM_MEMORY_EXIT_FLAG_USERFAULT From: James Houghton To: Paolo Bonzini , Sean Christopherson Cc: Jonathan Corbet , Marc Zyngier , Oliver Upton , Yan Zhao , James Houghton , Nikita Kalyazin , Anish Moorthy , Peter Gonda , Peter Xu , David Matlack , Wang@google.com, Wei W , kvm@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241204_111418_472634_B9CF511B X-CRM114-Status: GOOD ( 10.43 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org This flag is used for vCPU memory faults caused by KVM Userfault; i.e., the bit in `userfault_bitmap` corresponding to the faulting gfn was set. 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 94be7e8b46a4..641a2e580441 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -444,6 +444,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 Dec 4 19:13:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Houghton X-Patchwork-Id: 13894212 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 9A09CE7716D for ; Wed, 4 Dec 2024 19:19:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To:From: Subject:Message-ID:References:Mime-Version:In-Reply-To:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=rnZJyT6N3r8vTjoa4UcLa/XGdQ+PqihxUkrotorxiRo=; b=rtghKAB7N+Y4rC59dY/n3EDAOM rmOF75l34MvxT3hye9ormAO1XdiyaljPgaZrAZnYILQqU+kHSDbTC4dYcPynL+Xi4NugvX7fZ6S0Q LsV8/U96qclc526NK8sk1hYByU/Qm5ngiLkOjB3jUHMtwlMg9jY1xGMn69FRPs5l3tKmYkU5LY1E6 uM9qB0OBlatJOLzTeNNCw8sc4bgEX6J15nT1S8xL/uCeDTGGfVCYU9Bsw8kpfGsDZY5ybKPLTqQVh Nd2rr8aaL9NMblO/pGzXaQSGCUAXCkVhzYEhzZUrLRAAVFxVVL0ZWX2xScAe6HhSdPJunTl86s2y7 77qwk0mA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tIuuE-0000000Df5I-2a7b; Wed, 04 Dec 2024 19:19:26 +0000 Received: from mail-vk1-xa4a.google.com ([2607:f8b0:4864:20::a4a]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tIupI-0000000Ddgf-0pyW for linux-arm-kernel@lists.infradead.org; Wed, 04 Dec 2024 19:14:21 +0000 Received: by mail-vk1-xa4a.google.com with SMTP id 71dfb90a1353d-515cb33556eso18351e0c.3 for ; Wed, 04 Dec 2024 11:14:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1733339658; x=1733944458; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=rnZJyT6N3r8vTjoa4UcLa/XGdQ+PqihxUkrotorxiRo=; b=J5/JqZthe4nyV75XAbN0M2nxMbL1/7qTU0IMla5WCiRZt6Bpqln3v79bQx1eiLIrGo 6a7+LVVIMLvVkff6OHJldh7Gb33p0jyCROtF3lG0rn7lgw5e7joPTwu56x4UvFaIhRLc AZGXTdjNLOMmbupOMNtjDjLGk3j0clxAlwYE8kUf0nlIFs8L6oY5t+XFQ0zF/9grBezR UIbokCyVkH0lGrxt4D8Hlr8M+Mf8PKIdoECkcgNXbFnXJqjsAYNB0BTUwIPi/ZjhAunw BxPgGQ+LsoMB4A3tLB3/43sS6Rvq8EjvJZUXiH1TMpTUU6hQ500Zt3TdiECFqOxk0nYO xpqw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733339658; x=1733944458; 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=rnZJyT6N3r8vTjoa4UcLa/XGdQ+PqihxUkrotorxiRo=; b=IqewtFI9QvUzfpD1ijh1CCv1Wk2dohMKR1h438v5yP2yL9S0/PCdbxsn/OsEJbEvka FKi6D9ZQpH1b33zenQsXOtPoxCuENLSAXW2nShvujgRnoRAUIuvGkpuOEKnZtu99Vlrx pP7Qsui+R53d9DCZtMZ/VMzFTJMaU6MGkczP5Tfpa7H4xx+LFuzX4cVt8GWm0xAWytL0 ljHtv2P9YlukDimYYGlWKBAjCveF5rjIrT1Ehcstk0Bu3w8p1BUR+aVSB5VjEPRQNYMO BzQzmsRXN97jrFGD/P61TnYxa0KE/pb8R/XDPN1BrF6yeJwFXfy/XtQ/xds1SvRe381m CPVQ== X-Forwarded-Encrypted: i=1; AJvYcCWF9dj0qW5dthcztnetD7gXsPUu9ZkSzilfNqlr6z0YY0a/h00wEzDDpihADGKwo/WL0z8gsPKMfArRCLeAe5LX@lists.infradead.org X-Gm-Message-State: AOJu0YxU0QP4LExTB+Ylsy8zITH8/fuCvMZfhgjoxuAlYaZM/CoAumrB abDx1LgppBr+lS0wbgfAPlvkBtfywFDPJjWO7pDMflSJayfTJqqv0BQOFTvZVp6JZJmjo3bpWN7 H7vdXtj2c2tVdM1zJnw== X-Google-Smtp-Source: AGHT+IHeflh53wCdtG/eOTj3NVvzDZ1TR16bpwDBvuZyMyTF5KFF0ZrmPFZ5n9dphqqxj6PK08wZjYTOW77Q8bPv X-Received: from vkbfc18.prod.google.com ([2002:a05:6122:4b12:b0:50d:6d20:c212]) (user=jthoughton job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6122:8d0:b0:50d:4cb8:5aef with SMTP id 71dfb90a1353d-515bf2ff82amr10640137e0c.6.1733339657905; Wed, 04 Dec 2024 11:14:17 -0800 (PST) Date: Wed, 4 Dec 2024 19:13:38 +0000 In-Reply-To: <20241204191349.1730936-1-jthoughton@google.com> Mime-Version: 1.0 References: <20241204191349.1730936-1-jthoughton@google.com> X-Mailer: git-send-email 2.47.0.338.g60cca15819-goog Message-ID: <20241204191349.1730936-4-jthoughton@google.com> Subject: [PATCH v1 03/13] KVM: Allow late setting of KVM_MEM_USERFAULT on guest_memfd memslot From: James Houghton To: Paolo Bonzini , Sean Christopherson Cc: Jonathan Corbet , Marc Zyngier , Oliver Upton , Yan Zhao , James Houghton , Nikita Kalyazin , Anish Moorthy , Peter Gonda , Peter Xu , David Matlack , Wang@google.com, Wei W , kvm@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241204_111420_236791_4C3BA90D X-CRM114-Status: GOOD ( 15.38 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Currently guest_memfd memslots can only be deleted. Slightly change the logic to allow KVM_MR_FLAGS_ONLY changes when the only flag being changed is KVM_MEM_USERFAULT. Signed-off-by: James Houghton --- virt/kvm/kvm_main.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 23fa3e911c4e..fa851704db94 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -2009,9 +2009,6 @@ int __kvm_set_memory_region(struct kvm *kvm, if ((kvm->nr_memslot_pages + npages) < kvm->nr_memslot_pages) return -EINVAL; } else { /* Modify an existing slot. */ - /* Private memslots are immutable, they can only be deleted. */ - if (mem->flags & KVM_MEM_GUEST_MEMFD) - return -EINVAL; if ((mem->userspace_addr != old->userspace_addr) || (npages != old->npages) || ((mem->flags ^ old->flags) & KVM_MEM_READONLY)) @@ -2025,6 +2022,16 @@ int __kvm_set_memory_region(struct kvm *kvm, return 0; } + /* + * Except for being able to set KVM_MEM_USERFAULT, private memslots are + * immutable, they can only be deleted. + */ + if (mem->flags & KVM_MEM_GUEST_MEMFD && + !(change == KVM_MR_CREATE || + (change == KVM_MR_FLAGS_ONLY && + (mem->flags ^ old->flags) == KVM_MEM_USERFAULT))) + return -EINVAL; + if ((change == KVM_MR_CREATE || change == KVM_MR_MOVE) && kvm_check_memslot_overlap(slots, id, base_gfn, base_gfn + npages)) return -EEXIST; @@ -2040,7 +2047,7 @@ int __kvm_set_memory_region(struct kvm *kvm, new->npages = npages; new->flags = mem->flags; new->userspace_addr = mem->userspace_addr; - if (mem->flags & KVM_MEM_GUEST_MEMFD) { + if (mem->flags & KVM_MEM_GUEST_MEMFD && change == KVM_MR_CREATE) { r = kvm_gmem_bind(kvm, new, mem->guest_memfd, mem->guest_memfd_offset); if (r) goto out; From patchwork Wed Dec 4 19:13:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Houghton X-Patchwork-Id: 13894213 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 47497E7716D for ; Wed, 4 Dec 2024 19:20:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To:From: Subject:Message-ID:References:Mime-Version:In-Reply-To:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=QAQf3CS/PWwFToEYvErsd8wUXXWpCS3gaH7WeW/jjGQ=; b=wSR0XIzhJyf7m+fKavmWHfDTqF eaC046pyVmL/9CYRRAskFeqGKi21OhdmSaBdY8lkB/+4m8toUD74XJZIIgRAmHWoD0MVlS5qOzcEO iOw7CtrhXCosjztKSWJxAe9Df/UZe70xENhxvF+aq6DXw9ev4N8juBCtcosVv72rfncnJYSxFEL8D D+TDNlCa5z7FfwaQcNaa6mgMYvgfqrbRTWAyUTxIgQXbyayzEcpLGKa/jClWWfQ7NGDiSbR5QZ2Zi SBE7eqDuZZ5bTKl/AgUv6h67Dr+Xza4jRpr3edUmOKU8OYd/fwKN3t4ukZZEYDm0fF938xIm4gk+I kqifQejQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tIuvD-0000000DfER-1BnN; Wed, 04 Dec 2024 19:20:27 +0000 Received: from mail-ot1-x349.google.com ([2607:f8b0:4864:20::349]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tIupJ-0000000DdhF-0LUs for linux-arm-kernel@lists.infradead.org; Wed, 04 Dec 2024 19:14:22 +0000 Received: by mail-ot1-x349.google.com with SMTP id 46e09a7af769-71d3fac4b52so116065a34.2 for ; Wed, 04 Dec 2024 11:14:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1733339659; x=1733944459; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=QAQf3CS/PWwFToEYvErsd8wUXXWpCS3gaH7WeW/jjGQ=; b=HSP2kdKp60D1fMG4FOSJORq2vvFgugFDTd/w9vw0lLfePhcE7v4pQbhmKDeKVufjlX faBkBGlLJrc1IgK+AgrGKiGT9+JIvA9Fg+icn65Annn+4BH8uWJO2244wP1x8MNyRLgy waz2otHusN8mLJDouEIt2+hfkTVofIL69gLjRTa2rtwvReqLQMKvRvChsJKV45G5f6TD NMQclTZECtnEnSsFMCLhPt4KH9U3IyubNDi9szWVFFzfoydzZrAx1OpEncFpA+A/qxc1 ZRjKsuvHkGs99mmLZnSUbY/GAS9yTPi3XO/w1TJMJctyQbr4rmJw/zo11C14v57U5Aov 2+7w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733339659; x=1733944459; 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=QAQf3CS/PWwFToEYvErsd8wUXXWpCS3gaH7WeW/jjGQ=; b=XQHc5Q1QrZK5T71n3lm40g2qm27Ei/rQbhXTTeGwFXNOwGYc/2PxqotjH1FBu9ybph tCNTXKIV7EcQkvvkknJmp8m1RQcWi8TaLwaJoJj+0uc8VMIBvFNu6KU6y8IfAe7P4Eiw xiGY0RU5MH/PRe6Vbsphu0u9WT/b0FSAyko3SLFyonUS7MmERvt/ddApbu1PfyPu8Cqy kDsGDlpw/7NhdoQv5uf0VyvbDsYdJAJFdxTqDK+EO0wz/k/3txmooAjb0f74I+aja+QS EX7NM1d9fVYICSuWBpdm2LAlrbm9zhkCmnUtlucdqb6k9EOrSSXssZsV6JxI0g1fq1au nECA== X-Forwarded-Encrypted: i=1; AJvYcCX/u7W1w6TCSKENBkDvuNBDGnm4y068AIjHb7xljh59vZ3bQfJXctwxv/LsAhVheh2jBSPLNsMt5BcpZZY+YYO/@lists.infradead.org X-Gm-Message-State: AOJu0YyRFpr/8KtDubJGaOxBJaj+ILcF0mE2a1EpQZHByNLV2RRgpCgC k75npT2eMgr8qPN6S4lKhXXDDn/u1IuLkaM6mMijwbfSJd4mV/i3aYD4KFuPrONazap4+JP8TqN yyHiI4Abe+WRtF1dwFQ== X-Google-Smtp-Source: AGHT+IF4lgVNFWtAABhlVyCvDTypvCqwQE4E76pJaa2ei5llPSfzgOLBadwb4XjVCcPSe7j0KBf+vwviXHvnVJ9Y X-Received: from vsvj2.prod.google.com ([2002:a05:6102:3e02:b0:4af:5a5c:cdaa]) (user=jthoughton job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6830:348b:b0:71d:4c3b:f464 with SMTP id 46e09a7af769-71dad63c3e4mr7692549a34.13.1733339659221; Wed, 04 Dec 2024 11:14:19 -0800 (PST) Date: Wed, 4 Dec 2024 19:13:39 +0000 In-Reply-To: <20241204191349.1730936-1-jthoughton@google.com> Mime-Version: 1.0 References: <20241204191349.1730936-1-jthoughton@google.com> X-Mailer: git-send-email 2.47.0.338.g60cca15819-goog Message-ID: <20241204191349.1730936-5-jthoughton@google.com> Subject: [PATCH v1 04/13] KVM: Advertise KVM_CAP_USERFAULT in KVM_CHECK_EXTENSION From: James Houghton To: Paolo Bonzini , Sean Christopherson Cc: Jonathan Corbet , Marc Zyngier , Oliver Upton , Yan Zhao , James Houghton , Nikita Kalyazin , Anish Moorthy , Peter Gonda , Peter Xu , David Matlack , Wang@google.com, Wei W , kvm@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241204_111421_122429_14859705 X-CRM114-Status: GOOD ( 11.29 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Advertise support for KVM_CAP_USERFAULT when kvm_has_userfault() returns true. Currently this is merely IS_ENABLED(CONFIG_HAVE_KVM_USERFAULT), so it is somewhat redundant. Signed-off-by: James Houghton --- include/uapi/linux/kvm.h | 1 + virt/kvm/kvm_main.c | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 641a2e580441..d9a135c895d7 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -936,6 +936,7 @@ struct kvm_enable_cap { #define KVM_CAP_PRE_FAULT_MEMORY 236 #define KVM_CAP_X86_APIC_BUS_CYCLES_NS 237 #define KVM_CAP_X86_GUEST_MODE 238 +#define KVM_CAP_USERFAULT 239 struct kvm_irq_routing_irqchip { __u32 irqchip; diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index fa851704db94..b552cdef2850 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -4804,6 +4804,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_HAVE_KVM_USERFAULT + case KVM_CAP_USERFAULT: + return kvm_has_userfault(kvm); #endif default: break; From patchwork Wed Dec 4 19:13:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Houghton X-Patchwork-Id: 13894214 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id A1121E77172 for ; Wed, 4 Dec 2024 19:21:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To:From: Subject:Message-ID:References:Mime-Version:In-Reply-To:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=tFj98HJEqxlM2yS1YScvzRxxJLlbahOUdL6PgedEnEc=; b=KdhBq+/H+/KAI4qHJmKqoxpxkR ATx0uyraTcMz874Df6MD3/Ua/34HQUHFn22Kpf9wxMZM/q1muTKGPobObkGLS/7GR6HrrD96PRxqy cpu5P8dcxzkF9WEXGIFqi64mwwhdhztAQmqx7pH6/3fCW3xRd1QkzYwxbsvoDCpzwyGvaBcYnsczb ixY9xeYNqy9X7weo4wgE9EjZGxd1RyplUdh60eWFINPqq5rNOD5p9/TGzqlFcsrM2+UWquP4J5/uE Dj1f+pfsFPeajVSm2Oq0+ZIt/t1/FBIv6cr1hrakeDWOiMX1NGmXJlHYY15SooKHws1GEqxUuxIXm vMoOyiYg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tIuwC-0000000DfNb-0v9k; Wed, 04 Dec 2024 19:21:28 +0000 Received: from mail-qk1-x749.google.com ([2607:f8b0:4864:20::749]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tIupJ-0000000Ddi6-3ulM for linux-arm-kernel@lists.infradead.org; Wed, 04 Dec 2024 19:14:23 +0000 Received: by mail-qk1-x749.google.com with SMTP id af79cd13be357-7b67a9aea08so11345985a.2 for ; Wed, 04 Dec 2024 11:14:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1733339660; x=1733944460; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=tFj98HJEqxlM2yS1YScvzRxxJLlbahOUdL6PgedEnEc=; b=BABYEbCgK+3HxLlXxkoPaMFgatrRx6+XrcvdEe/qU+r02Q74bcO0yhLsgg2KJKjdPo 3vKz08A/k3HayDxIJvOo4Z0GiYEOVEjglwdIbC5JyuG3J4P4deYyDn54fnqT1tT971Au VZjAnbndyv8d9zqNHIrojP0IViWlrtiRsVRo1BNpWT8MDgMvhOjg6LQZxixlwaO4dZ4G CrOY9nLGRPcOz3RbVRTxEk33in3xuFLusfOzopJo7oY1hlHdil3c7H8OgPScnvcRm/Nk XgpE/OCOuTU+pjhIuR6T/zaGuNdAjtz8P8Woco2mGtBi1uxX+IM6oOkvj2MPYJ4MPT3g 2xgA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733339660; x=1733944460; 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=tFj98HJEqxlM2yS1YScvzRxxJLlbahOUdL6PgedEnEc=; b=WJKrlJZXLzjQUbzLPfEMKbUEdEVjaRBXYaUliWEqH6nCg8gEDjHNLorD1SXxvE2VX3 b00vSKSUoajTCpxfKOUEyLHI5k2Y8fTwGQYhBGbOPXEacZ6mA38dI46rf1rhoGBwJNni 4BbH0pAAnF8VmZMxmTb1hdjPXO/JFzmdYXVSOZj1J/cK9daDBaS7AXC22AEr+2Iidjiy ONJXef3ddXue+MoXLAJmooL4ndIwCj1CE+JSx85djAHRh9nDgQDuzlprH8H9km2H1W1x 732VOMfn7PIicAxRELAei2uSRUOoop2RguRdyLatZscG9kPE0sdGXXWnpacHLj9n7tJN e+6g== X-Forwarded-Encrypted: i=1; AJvYcCW8A/nKz5FHKBhop71qRSVEspuBe3hr7dNPCSMG3eCiERsGtUOTOe11cgzavj/VxzZ4hAGuQUcWal41Qq+63NLF@lists.infradead.org X-Gm-Message-State: AOJu0Yy9nahbiPI5yxBRzc3hhl7n1YEbd6+EKuYNVcIrHuwe6BNouplA 5fj5rw0Rs5c/mLoHdjfK9ENG0SN8ebTkr0kAWe1HOsPTUP7YOhp3rBDC4rdayI1TazpM6MXdQO2 t3Rd0pclz+WN9HHO82w== X-Google-Smtp-Source: AGHT+IEjAFXSEtfmGOodffFw31TXE80vwkJvDLAFbZ3yUQDUssUZgM+UMQkcUbluyRy21fc1aO4FFwapwVcSSi9j X-Received: from qtbci19.prod.google.com ([2002:a05:622a:2613:b0:462:aa34:aae2]) (user=jthoughton job=prod-delivery.src-stubby-dispatcher) by 2002:a05:620a:3188:b0:7b6:73f5:2867 with SMTP id af79cd13be357-7b6a61c819cmr926705785a.44.1733339660438; Wed, 04 Dec 2024 11:14:20 -0800 (PST) Date: Wed, 4 Dec 2024 19:13:40 +0000 In-Reply-To: <20241204191349.1730936-1-jthoughton@google.com> Mime-Version: 1.0 References: <20241204191349.1730936-1-jthoughton@google.com> X-Mailer: git-send-email 2.47.0.338.g60cca15819-goog Message-ID: <20241204191349.1730936-6-jthoughton@google.com> Subject: [PATCH v1 05/13] KVM: x86/mmu: Add support for KVM_MEM_USERFAULT From: James Houghton To: Paolo Bonzini , Sean Christopherson Cc: Jonathan Corbet , Marc Zyngier , Oliver Upton , Yan Zhao , James Houghton , Nikita Kalyazin , Anish Moorthy , Peter Gonda , Peter Xu , David Matlack , Wang@google.com, Wei W , kvm@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241204_111421_977022_C1FF2823 X-CRM114-Status: GOOD ( 22.08 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Adhering to the requirements of KVM Userfault: 1. Zap all sptes for the memslot when KVM_MEM_USERFAULT is toggled on with kvm_arch_flush_shadow_memslot(). 2. Only all PAGE_SIZE sptes while KVM_MEM_USERFAULT is enabled (for both normal/GUP memory and guest_memfd memory). 3. Reconstruct huge mappings when KVM_MEM_USERFAULT is toggled off with kvm_mmu_recover_huge_pages(). With the new logic in kvm_mmu_slot_apply_flags(), I've simplified the two dirty-logging-toggle checks into one, and I have dropped the WARN_ON() that was there. Signed-off-by: James Houghton --- arch/x86/kvm/Kconfig | 1 + arch/x86/kvm/mmu/mmu.c | 27 +++++++++++++++++++++---- arch/x86/kvm/mmu/mmu_internal.h | 20 +++++++++++++++--- arch/x86/kvm/x86.c | 36 ++++++++++++++++++++++++--------- include/linux/kvm_host.h | 5 ++++- 5 files changed, 71 insertions(+), 18 deletions(-) diff --git a/arch/x86/kvm/Kconfig b/arch/x86/kvm/Kconfig index ea2c4f21c1ca..286c6825cd1c 100644 --- a/arch/x86/kvm/Kconfig +++ b/arch/x86/kvm/Kconfig @@ -47,6 +47,7 @@ config KVM_X86 select KVM_GENERIC_PRE_FAULT_MEMORY select KVM_GENERIC_PRIVATE_MEM if KVM_SW_PROTECTED_VM select KVM_WERROR if WERROR + select HAVE_KVM_USERFAULT config KVM tristate "Kernel-based Virtual Machine (KVM) support" diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 22e7ad235123..2f7381255d11 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -4292,14 +4292,19 @@ static inline u8 kvm_max_level_for_order(int order) return PG_LEVEL_4K; } -static u8 kvm_max_private_mapping_level(struct kvm *kvm, kvm_pfn_t pfn, - u8 max_level, int gmem_order) +static u8 kvm_max_private_mapping_level(struct kvm *kvm, + struct kvm_memory_slot *slot, + kvm_pfn_t pfn, u8 max_level, + int gmem_order) { u8 req_max_level; if (max_level == PG_LEVEL_4K) return PG_LEVEL_4K; + if (kvm_memslot_userfault(slot)) + return PG_LEVEL_4K; + max_level = min(kvm_max_level_for_order(gmem_order), max_level); if (max_level == PG_LEVEL_4K) return PG_LEVEL_4K; @@ -4336,8 +4341,10 @@ static int kvm_mmu_faultin_pfn_private(struct kvm_vcpu *vcpu, } fault->map_writable = !(fault->slot->flags & KVM_MEM_READONLY); - fault->max_level = kvm_max_private_mapping_level(vcpu->kvm, fault->pfn, - fault->max_level, max_order); + fault->max_level = kvm_max_private_mapping_level(vcpu->kvm, fault->slot, + fault->pfn, + fault->max_level, + max_order); return RET_PF_CONTINUE; } @@ -4346,6 +4353,18 @@ static int __kvm_mmu_faultin_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) { unsigned int foll = fault->write ? FOLL_WRITE : 0; + int userfault; + + userfault = kvm_gfn_userfault(vcpu->kvm, fault->slot, fault->gfn); + if (userfault < 0) + return userfault; + if (userfault) { + kvm_mmu_prepare_userfault_exit(vcpu, fault); + return -EFAULT; + } + + if (kvm_memslot_userfault(fault->slot)) + fault->max_level = PG_LEVEL_4K; if (fault->is_private) return kvm_mmu_faultin_pfn_private(vcpu, fault); diff --git a/arch/x86/kvm/mmu/mmu_internal.h b/arch/x86/kvm/mmu/mmu_internal.h index b00abbe3f6cf..15705faa3b67 100644 --- a/arch/x86/kvm/mmu/mmu_internal.h +++ b/arch/x86/kvm/mmu/mmu_internal.h @@ -282,12 +282,26 @@ enum { RET_PF_SPURIOUS, }; -static inline void kvm_mmu_prepare_memory_fault_exit(struct kvm_vcpu *vcpu, - struct kvm_page_fault *fault) +static inline void __kvm_mmu_prepare_memory_fault_exit(struct kvm_vcpu *vcpu, + struct kvm_page_fault *fault, + bool is_userfault) { kvm_prepare_memory_fault_exit(vcpu, fault->gfn << PAGE_SHIFT, PAGE_SIZE, fault->write, fault->exec, - fault->is_private); + fault->is_private, + is_userfault); +} + +static inline void kvm_mmu_prepare_memory_fault_exit(struct kvm_vcpu *vcpu, + struct kvm_page_fault *fault) +{ + __kvm_mmu_prepare_memory_fault_exit(vcpu, fault, false); +} + +static inline void kvm_mmu_prepare_userfault_exit(struct kvm_vcpu *vcpu, + struct kvm_page_fault *fault) +{ + __kvm_mmu_prepare_memory_fault_exit(vcpu, fault, true); } static inline int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 2e713480933a..2f7080fd6218 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -13053,12 +13053,36 @@ static void kvm_mmu_slot_apply_flags(struct kvm *kvm, u32 new_flags = new ? new->flags : 0; bool log_dirty_pages = new_flags & KVM_MEM_LOG_DIRTY_PAGES; + /* + * When toggling KVM Userfault on, zap all sptes so that userfault-ness + * will be respected at refault time. All new faults will only install + * small sptes. Therefore, when toggling it off, recover hugepages. + * + * For MOVE and DELETE, there will be nothing to do, as the old + * mappings will have already been deleted by + * kvm_arch_flush_shadow_memslot(). + * + * For CREATE, no mappings will have been created yet. + */ + if ((old_flags ^ new_flags) & KVM_MEM_USERFAULT && + (change == KVM_MR_FLAGS_ONLY)) { + if (old_flags & KVM_MEM_USERFAULT) + kvm_mmu_recover_huge_pages(kvm, new); + else + kvm_arch_flush_shadow_memslot(kvm, old); + } + + /* + * Nothing more to do if dirty logging isn't being toggled. + */ + if (!((old_flags ^ new_flags) & KVM_MEM_LOG_DIRTY_PAGES)) + return; + /* * Update CPU dirty logging if dirty logging is being toggled. This * applies to all operations. */ - if ((old_flags ^ new_flags) & KVM_MEM_LOG_DIRTY_PAGES) - kvm_mmu_update_cpu_dirty_logging(kvm, log_dirty_pages); + kvm_mmu_update_cpu_dirty_logging(kvm, log_dirty_pages); /* * Nothing more to do for RO slots (which can't be dirtied and can't be @@ -13078,14 +13102,6 @@ static void kvm_mmu_slot_apply_flags(struct kvm *kvm, if ((change != KVM_MR_FLAGS_ONLY) || (new_flags & KVM_MEM_READONLY)) return; - /* - * READONLY and non-flags changes were filtered out above, and the only - * other flag is LOG_DIRTY_PAGES, i.e. something is wrong if dirty - * logging isn't being toggled on or off. - */ - if (WARN_ON_ONCE(!((old_flags ^ new_flags) & KVM_MEM_LOG_DIRTY_PAGES))) - return; - if (!log_dirty_pages) { /* * Recover huge page mappings in the slot now that dirty logging diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index f7a3dfd5e224..9e8a8dcf2b73 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -2465,7 +2465,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; @@ -2475,6 +2476,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 Dec 4 19:13:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Houghton X-Patchwork-Id: 13894225 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 10930E7716D for ; Wed, 4 Dec 2024 19:30:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To:From: Subject:Message-ID:References:Mime-Version:In-Reply-To:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=3LkXqlJAd4fQvrkyb/GIMYTRcu+OGHQVMPLjSgCJbVo=; b=FLmgyqxSyNY6Cwe2mWsgLbxuma Ivax/T+BAO/44Qfmab7/f1NI83bNmv5/ab2xATHZQI+TUT7Z+Cz3vJ3Iiz9s3xL6ttqb0UvW2hL9g AS1mQXtMjA4GE013+c0ufIf6B7QGVZ1cS6pF/8lBjdOeHncSVuXa5w5h9DxPEY4Up+0HGFI10Y+hB cGKoYvSUMyAJ+A7ZaUgG6qa0ymDPRr+AhW2cyqsTDfoVjLKvAzpnFnR28GSglb54YRy4gDu+GDTnw 0jGiPoPRY3OLkCcl1DSiaskkqDT8a3Pb2FRMEhsWeYvAWer8po3KoozXfuCCoqYQKR+4RciSq7Li2 UEZjOsRw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tIv4F-0000000DgbE-2IgN; Wed, 04 Dec 2024 19:29:47 +0000 Received: from mail-qk1-x749.google.com ([2607:f8b0:4864:20::749]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tIupK-0000000DdiW-3iKT for linux-arm-kernel@lists.infradead.org; Wed, 04 Dec 2024 19:14:23 +0000 Received: by mail-qk1-x749.google.com with SMTP id af79cd13be357-7b677762665so13213985a.2 for ; Wed, 04 Dec 2024 11:14:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1733339661; x=1733944461; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=3LkXqlJAd4fQvrkyb/GIMYTRcu+OGHQVMPLjSgCJbVo=; b=VCwtdkQ+oujk2Xs4iASlv+04WCwBzbWV8ZAcJJbiGqowmckp0pRinHGb5lytP2ersP /Hqi0UIz+wjDPS1pMCBIP/dhs402boW75oMQgg2LqxBC+Bj73ZrPzvNvuD5S0QIfK2Vb Gj5vBFm33hQ2L+5XxOkEe1yhB1SRMsRfoRJLGbQnKO+YSUPZvMTGoK7cdO2c5UHzdyhz WLc4Ls+0WD2SN8/OE0cOQ3H0DqeKm8sbpNnLoSFw/WiZV6hEV7xkU2SP9L+ajYqJCoSz /SdCC23B6b94OhZWUl6DmXoHmG81YZh5EzHJiqt0XoaTAFny8ALm9qsDi8qxpJL71JiI eknQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733339661; x=1733944461; 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=3LkXqlJAd4fQvrkyb/GIMYTRcu+OGHQVMPLjSgCJbVo=; b=PhjgDVzLx98Uo278h5EPxMKC28ya+Xr/8utW/TJt1kOqV1cwcYPc0eS6BerCMSh317 qrhSmP4QnNMbmdS1RgmxDHHTMJKG03okU6EHVj8N3/2JJ73JU/oJMSq4dg+P5b8MUygj XgLB1gzVPznQ5/Ao3pQGp+0u0I5zxolMxtxJX6tUGIgrjv1EQII3Rp8J7Y7WuLLZN6dD XDPmJ/hnL7eeJcjfLVTpURSIYDNhYzxGwd87cEwXncyoMY4hgDTCGffGznsOcGdxoHma +IlpIAsQbFhZHrWY8hWCbSEuF2iX7Tx3DJFBCo+DGHTmBsNCUG0KxM4Bxies/6KNv27V BjVw== X-Forwarded-Encrypted: i=1; AJvYcCUFpvdRLvpqapQkKWUUpDjz7ruZhbOwKCzMegKFHY20cAfAsa+vZshEqmFQuEmL0IyEw6iR8eEUoASQFgF1+v9r@lists.infradead.org X-Gm-Message-State: AOJu0Yz6ONKcphmPNB9ZBxqME9mRB47Zealdh7TT5PvjxkDv4O7Pdjhb m4LCi8LJBl5PzOoUHuVi5AcCdf95gDNlIyoLiH6NZfEBxmrE/YlbgcjH2bZCRoDtF4fnCFi7Iv+ GcnYe9t0p2KPENGqb/w== X-Google-Smtp-Source: AGHT+IG2YC7xA0GwWXk+cMxHq3A6sTNXchADXjRqs7ScJbz/ng/FU66WmJEiNslyAxyIF4C+tDiCnDtGEeEHeuoK X-Received: from uah9.prod.google.com ([2002:a05:6130:5209:b0:856:f0b0:717]) (user=jthoughton job=prod-delivery.src-stubby-dispatcher) by 2002:a05:620a:40c2:b0:7b6:6b07:e80d with SMTP id af79cd13be357-7b6abbb7350mr835946485a.57.1733339661300; Wed, 04 Dec 2024 11:14:21 -0800 (PST) Date: Wed, 4 Dec 2024 19:13:41 +0000 In-Reply-To: <20241204191349.1730936-1-jthoughton@google.com> Mime-Version: 1.0 References: <20241204191349.1730936-1-jthoughton@google.com> X-Mailer: git-send-email 2.47.0.338.g60cca15819-goog Message-ID: <20241204191349.1730936-7-jthoughton@google.com> Subject: [PATCH v1 06/13] KVM: arm64: Add support for KVM_MEM_USERFAULT From: James Houghton To: Paolo Bonzini , Sean Christopherson Cc: Jonathan Corbet , Marc Zyngier , Oliver Upton , Yan Zhao , James Houghton , Nikita Kalyazin , Anish Moorthy , Peter Gonda , Peter Xu , David Matlack , Wang@google.com, Wei W , kvm@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241204_111422_932422_9905F392 X-CRM114-Status: GOOD ( 18.12 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Adhering to the requirements of KVM Userfault: 1. When it is toggled (either on or off), zap the second stage with kvm_arch_flush_shadow_memslot(). This is to (1) respect userfault-ness and (2) to reconstruct block mappings. 2. While KVM_MEM_USERFAULT is enabled, restrict new second-stage mappings to be PAGE_SIZE, just like when dirty logging is enabled. Signed-off-by: James Houghton --- I'm not 100% sure if kvm_arch_flush_shadow_memslot() is correct in this case (like if the host does not have S2FWB). --- arch/arm64/kvm/Kconfig | 1 + arch/arm64/kvm/mmu.c | 23 ++++++++++++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/arch/arm64/kvm/Kconfig b/arch/arm64/kvm/Kconfig index ead632ad01b4..d89b4088b580 100644 --- a/arch/arm64/kvm/Kconfig +++ b/arch/arm64/kvm/Kconfig @@ -38,6 +38,7 @@ menuconfig KVM select HAVE_KVM_VCPU_RUN_PID_CHANGE select SCHED_INFO select GUEST_PERF_EVENTS if PERF_EVENTS + select HAVE_KVM_USERFAULT help Support hosting virtualized guest machines. diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c index a71fe6f6bd90..53cee0bacb75 100644 --- a/arch/arm64/kvm/mmu.c +++ b/arch/arm64/kvm/mmu.c @@ -1482,7 +1482,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_memslot_userfault(memslot)) { force_pte = true; vma_shift = PAGE_SHIFT; } else { @@ -1571,6 +1571,13 @@ 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 (kvm_gfn_userfault(kvm, memslot, gfn)) { + kvm_prepare_memory_fault_exit(vcpu, gfn << PAGE_SHIFT, + PAGE_SIZE, write_fault, + exec_fault, false, true); + return -EFAULT; + } + pfn = __kvm_faultin_pfn(memslot, gfn, write_fault ? FOLL_WRITE : 0, &writable, &page); if (pfn == KVM_PFN_ERR_HWPOISON) { @@ -2062,6 +2069,20 @@ void kvm_arch_commit_memory_region(struct kvm *kvm, enum kvm_mr_change change) { bool log_dirty_pages = new && new->flags & KVM_MEM_LOG_DIRTY_PAGES; + u32 changed_flags = (new ? new->flags : 0) ^ (old ? old->flags : 0); + + /* + * If KVM_MEM_USERFAULT changed, drop all the stage-2 mappings so that + * we can (1) respect userfault-ness or (2) create block mappings. + */ + if ((changed_flags & KVM_MEM_USERFAULT) && change == KVM_MR_FLAGS_ONLY) + kvm_arch_flush_shadow_memslot(kvm, old); + + /* + * Nothing left to do if not toggling dirty logging. + */ + if (!(changed_flags & KVM_MEM_LOG_DIRTY_PAGES)) + return; /* * At this point memslot has been committed and there is an From patchwork Wed Dec 4 19:13:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Houghton X-Patchwork-Id: 13894215 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id C1FECE7716D for ; Wed, 4 Dec 2024 19:22:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To:From: Subject:Message-ID:References:Mime-Version:In-Reply-To:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=D0Bh/8/p57GgUO0KFv6z2qw3B4ssWvPHnpaRRwgHkKo=; b=OkJF6i/sbwHNLButhMiJHnHkMP M+5YFOoWAFhZOJEmYJP/w24om23qN32DkKgkORSlmnIY/skxkgZQV+v6O3Xi++zpJlJIto/WxCZY9 yCNUIDwRMrqFR1OE8dRfZo0IjL5j8lcpl80x2SUjbzIYlm8xFQDG3MVvix+jIf/+P1vtdSAKKrsSW SPl+Sga/e/gYi7HukIKQBMlOUGBTwR/JajtjPw3y5lvTkC2nQCnRvKoRf3YWgPRcLndbOhQGEfjFF eV8iVfkcIsxwDp7GcHFQzaEitMiQYOVEj2pU0qRETD8GCgnpnXtN6dZleYKMy4Sq5xMM/qvJX3cZO nSe3eD5w==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tIuxO-0000000DfW2-3ayT; Wed, 04 Dec 2024 19:22:42 +0000 Received: from mail-qt1-x84a.google.com ([2607:f8b0:4864:20::84a]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tIupM-0000000DdjJ-2bKF for linux-arm-kernel@lists.infradead.org; Wed, 04 Dec 2024 19:14:25 +0000 Received: by mail-qt1-x84a.google.com with SMTP id d75a77b69052e-466c499185cso1443451cf.3 for ; Wed, 04 Dec 2024 11:14:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1733339662; x=1733944462; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=D0Bh/8/p57GgUO0KFv6z2qw3B4ssWvPHnpaRRwgHkKo=; b=cSS/Krw9giEjzRvU7PlX4PKDWg8/CmQBzjSYgLBDRk+tRgPsW2fbMG8jB9NN6ogPmv Sn71qjxbg3Qql4qE1jE0fwCIGHSgAtpaKx+g+QGhRIi41bjCtJHU1xIJi01eWE8SJIfi 61ofRct848wYrD/3jOz4I1CqXIS0uelboaXGPBZRQ8BzcXpUxKztac62Ih5ZikXXQ6oz 0jiS4c7bUX/vcnRhfTx9fWXWUAzQQ+Joidnx7D8avRPrzdPHirIoiUttdeBiTHODm8rK ag8p7Hn5xkCBxPR5vOMW6bmsei3a0SmWYXhaY4xtc1Cy3G0IqIS2SQqbwk1Nj6/QVurB Ha4w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733339662; x=1733944462; 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=D0Bh/8/p57GgUO0KFv6z2qw3B4ssWvPHnpaRRwgHkKo=; b=K56L4V+T4optuyMXzxnsU6p0GprNsTrEg0LOPKowh1It3FJAaGnRV7e4k8Lym1uqzN GSmaMdojz7fdJNox0mTigBUCP6w5c69bjF66q7JrjxXiTelH4tzB1kZ71C0kimQYqUwz G9mXiX6BNRvc6Mf2fGLoVhI1eBpvqFvYz3UDrbaHbyXVM/g2ezAGOR78ZjKZ0IJdeePA sJ2u6s/ge8PUA35OAXKV2rCf60543wx37ja0ViHh2uGNIvUU5eXbNjKm7IjLjLQ/dSC9 bl/PYJ4NFZR3LlujzFqtQ/gyiz5IMnMnSEly9xuKoINgPzwV1xgHpDC8MX+hor0VxGPa Zd/Q== X-Forwarded-Encrypted: i=1; AJvYcCXozbc6Xb5+nEYvbQJLT+9Cia1FCF5v8uIp1SzZXxxco77s4cPoozD3RmdA2vVOePYQqPeZc8cgl4LSVtT7CcY+@lists.infradead.org X-Gm-Message-State: AOJu0YxXsF9YwJRt+bAf1qs+1llzBHmF8GdAFAOdqVMXh3OYpRz7eDwE OsmTM5tyQtHR6vipgKv1vg+UxgBSqGmsDyRECMlx91muBGe5z9GQnud3fKgBAamDVbj6zPQgG3k IvDnipAXobWuPOuXgSA== X-Google-Smtp-Source: AGHT+IFL6Yzt0JAsezCjVnC1Ege7Dcb8t0Na16LNTuDgarqYjisKQzOlRXRbl7knWaQbVhi5EaZ6me/AizxIjPXb X-Received: from qtbcf26.prod.google.com ([2002:a05:622a:401a:b0:466:a2a5:c51f]) (user=jthoughton job=prod-delivery.src-stubby-dispatcher) by 2002:ac8:7fc4:0:b0:466:7a01:372b with SMTP id d75a77b69052e-4670c0c1884mr95272281cf.30.1733339662158; Wed, 04 Dec 2024 11:14:22 -0800 (PST) Date: Wed, 4 Dec 2024 19:13:42 +0000 In-Reply-To: <20241204191349.1730936-1-jthoughton@google.com> Mime-Version: 1.0 References: <20241204191349.1730936-1-jthoughton@google.com> X-Mailer: git-send-email 2.47.0.338.g60cca15819-goog Message-ID: <20241204191349.1730936-8-jthoughton@google.com> Subject: [PATCH v1 07/13] KVM: selftests: Fix vm_mem_region_set_flags docstring From: James Houghton To: Paolo Bonzini , Sean Christopherson Cc: Jonathan Corbet , Marc Zyngier , Oliver Upton , Yan Zhao , James Houghton , Nikita Kalyazin , Anish Moorthy , Peter Gonda , Peter Xu , David Matlack , Wang@google.com, Wei W , kvm@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241204_111424_662670_4F01824F X-CRM114-Status: GOOD ( 11.08 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org `flags` is what region->region.flags gets set to. Signed-off-by: James Houghton --- tools/testing/selftests/kvm/lib/kvm_util.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c index 480e3a40d197..9603f99d3247 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -1124,7 +1124,7 @@ memslot2region(struct kvm_vm *vm, uint32_t memslot) * * Input Args: * vm - Virtual Machine - * flags - Starting guest physical address + * flags - Flags for the memslot * * Output Args: None * From patchwork Wed Dec 4 19:13:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Houghton X-Patchwork-Id: 13894216 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id B12D2E7716D for ; Wed, 4 Dec 2024 19:23:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To:From: Subject:Message-ID:References:Mime-Version:In-Reply-To:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=zFzCtU30qR2oi/Is6fdl592GY3fZRLXfeluJtZi8G84=; b=dA/4Eav59Lpgr7iJ5M93Op0duA 04J1Eq5dwK5yxdDbAGLL432O1Qn0XuIcnxUVAVqv6qR8JnyyVxU5ufc6nNl7ERClSWn049ZNbK4lp hzq8Nf1yhlIuFptirmbC+aGmXkNW49QM7EyE3DONZ0jt/sb2K25yWJSy0nIuUNd1vyOoAtf0Rro7G OjShtLztbVHCHDa7i9nekiDHo/GNUJzfqYh9LPEMAQewzvjkz5S2MWSaKXXl1qJga1qzxWXdIuDXW TZsvUMOo0SaAIEk8gbiNt7ePLrOH5oPkGnnnegbSBXVZzloCWA351iPrKd9/gOad7Ui8awFSW4mFL z6d0DR6w==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tIuyN-0000000DffG-2CMf; Wed, 04 Dec 2024 19:23:43 +0000 Received: from mail-oo1-xc4a.google.com ([2607:f8b0:4864:20::c4a]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tIupN-0000000Ddjv-0WmP for linux-arm-kernel@lists.infradead.org; Wed, 04 Dec 2024 19:14:26 +0000 Received: by mail-oo1-xc4a.google.com with SMTP id 006d021491bc7-5f1ee87f67aso136714eaf.1 for ; Wed, 04 Dec 2024 11:14:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1733339663; x=1733944463; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=zFzCtU30qR2oi/Is6fdl592GY3fZRLXfeluJtZi8G84=; b=zJ18V22d1A6J7Xjo7Y5Ptt/AqmEtc32On6h1ITSLjd1q+FtPoR/l9LlEGk0HL87zdU Er4UsP5cqfepF0Xz0ZDdoJy8uueKO3TZeJMz9ywufzuhUuKsiWXWO89yeLWE2LwWG9Wc tSvlpNAfBJYeJXTZR0zn7QgMKrSjjIJ4Qgd53+nwEj5X+sp+ozlMiVgaDJI8QZ2mpQe1 Uo15XnN+G1g3RVkPbNlRwHo0AJaMuFJffgZ6qWGUv4U8q0WIchvm4I/WeYYUbgq5UxqC SEEmKIXwy9arxPdWO64zYcdah4n5Su1jBYvw5KluRdvyUsJxChzhtDpy4MEB3joxHDwV keyg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733339663; x=1733944463; 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=zFzCtU30qR2oi/Is6fdl592GY3fZRLXfeluJtZi8G84=; b=bzMON8JqhAQKQBuNVhJbe7pIUSw0xp4YOgZx6oKVENLuQjeY9Vofl/K3AxRFmyJ5Qy IxXNturuH6ETdq/k5RjpLwid/PZrNehfTkjdJRD6GoEsaHjMBdG93ik7er64uXvFPaIA Wp/AtyqxKZGEr1lgwd5dQuLYtg7IkkxjR1QRnXkTOHwZDq4op1ADpg2pkVbY5XbLeMEa qzeGtVWPy23EQWo5F8Vvi4bsRO5T5+x89RkLPWoTe3IKY05FHidDJYZII+vlFKIvkZnR XogAQySBdL29Te9aeQV2MM1yAPp1qI1m3E3cwC5X8ph1MV/9fYdgEiEsLyIwb+giNc7g vp+A== X-Forwarded-Encrypted: i=1; AJvYcCXv7RfGIES8oN5biV3ONQfdxgpzZSv9qPAHhQCjQ1psd+oUNxW1UPM73T8UDP20l5MPn/LLTgxTJ3qMqnRrUw2z@lists.infradead.org X-Gm-Message-State: AOJu0YzQuDqsf0mVkmz7LhZla6h8HGTRkEPi7for5Huyoz0+oLzo3SXn KHnEtvJCNYyfPFUqYEF2cffp/W87oo6SECnGaMkWko2mfqMWL1ISMdj+eRQxdYc/rbVJDHAC1No 2CewzlBtu6WAS5otq4g== X-Google-Smtp-Source: AGHT+IE0Bvu2wb5sZ62wPHnkPebE9DLvFVJFuHpqNaMFqI9+9IIU97LvrniH728q/X5uxBPvfLsjs32/EpieLb++ X-Received: from uad11.prod.google.com ([2002:a05:6130:800b:b0:855:e8c8:6d6c]) (user=jthoughton job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6218:2812:b0:1bc:45bc:81f0 with SMTP id e5c5f4694b2df-1caeaab608amr694161055d.11.1733339663073; Wed, 04 Dec 2024 11:14:23 -0800 (PST) Date: Wed, 4 Dec 2024 19:13:43 +0000 In-Reply-To: <20241204191349.1730936-1-jthoughton@google.com> Mime-Version: 1.0 References: <20241204191349.1730936-1-jthoughton@google.com> X-Mailer: git-send-email 2.47.0.338.g60cca15819-goog Message-ID: <20241204191349.1730936-9-jthoughton@google.com> Subject: [PATCH v1 08/13] KVM: selftests: Fix prefault_mem logic From: James Houghton To: Paolo Bonzini , Sean Christopherson Cc: Jonathan Corbet , Marc Zyngier , Oliver Upton , Yan Zhao , James Houghton , Nikita Kalyazin , Anish Moorthy , Peter Gonda , Peter Xu , David Matlack , Wang@google.com, Wei W , kvm@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241204_111425_184064_2961D98A X-CRM114-Status: GOOD ( 14.45 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org The previous logic didn't handle the case where memory was partitioned AND we were using a single userfaultfd. It would only prefault the first vCPU's memory and not the rest. Signed-off-by: James Houghton --- tools/testing/selftests/kvm/demand_paging_test.c | 6 ++++-- 1 file changed, 4 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..315f5c9037b4 100644 --- a/tools/testing/selftests/kvm/demand_paging_test.c +++ b/tools/testing/selftests/kvm/demand_paging_test.c @@ -172,11 +172,13 @@ static void run_test(enum vm_guest_mode mode, void *arg) memset(guest_data_prototype, 0xAB, demand_paging_size); if (p->uffd_mode == UFFDIO_REGISTER_MODE_MINOR) { - num_uffds = p->single_uffd ? 1 : nr_vcpus; - for (i = 0; i < num_uffds; i++) { + for (i = 0; i < nr_vcpus; i++) { vcpu_args = &memstress_args.vcpu_args[i]; prefault_mem(addr_gpa2alias(vm, vcpu_args->gpa), vcpu_args->pages * memstress_args.guest_page_size); + if (!p->partition_vcpu_memory_access) + /* We prefaulted everything */ + break; } } From patchwork Wed Dec 4 19:13:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Houghton X-Patchwork-Id: 13894220 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id B9892E7716D for ; Wed, 4 Dec 2024 19:24:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To:From: Subject:Message-ID:References:Mime-Version:In-Reply-To:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=gP/1ZRcuFjEEMybT5UMVVNt/puu4fP/kE3PemhzVNvM=; b=0f2a9gVCWlcF4jMpVISUjfdO8q UbrOQVDmuZFnY6a0X/7/u403Yczo8nNR8qyDpSEJHmTQBMqyV1Kl5USPLlJIajqFoMLPV9RBmRs+z qP03F2czdvEO6IUNUyOPfITCQwdBHy8404rcnIVGu5pMPoExr7fBMk7SxlDsKKn0K6zLl7R93+/0M rq8z5YA6K+b7fKCHM3XrrMjPeaNFpCxn9/6CD03rZC26mae1x4+rEcRx6OJgFpmwqBMiol84yDf2D k1mPiXI33oiHz3LaKm9Ho0NDUs4nWsuSb4i4AbLRLdub8KFnC7BaiHSQ6Uf4D5kYFvZo+/UUrDceD 5eDLibQw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tIuzM-0000000Dfnq-1cEe; Wed, 04 Dec 2024 19:24:44 +0000 Received: from mail-qk1-x74a.google.com ([2607:f8b0:4864:20::74a]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tIupN-0000000Ddkm-27UU for linux-arm-kernel@lists.infradead.org; Wed, 04 Dec 2024 19:14:26 +0000 Received: by mail-qk1-x74a.google.com with SMTP id af79cd13be357-7b674755f8eso20394485a.2 for ; Wed, 04 Dec 2024 11:14:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1733339664; x=1733944464; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=gP/1ZRcuFjEEMybT5UMVVNt/puu4fP/kE3PemhzVNvM=; b=N+XTI6hXK47l9nqQJ8tiiyu2O6Pr7nXn0OTuRqsEMnU2j457EYAz8pAi8O3hZYQP+Z ilkVT7LZBvqLegyiDu3kICBUbl0509PpylmRb6aMryolmvtSfiulrAC4AGVlo9LMhmiJ jPu+inVtRKeNTQkWaVpeqgjuKjSeCWwYg/lwOHvBmEXSNSMi3WLdi2s371+jadbrg+W2 djwYcjOBW4lg77Lz5yfHaoldiGNEmUYHphHuDCxKXFEVHj2vuUCGzBC9HPNKtML6d0R1 J5/0PZMh6ZaXan/zk/Fa7yIIPk2ugDmfnWifuJTrHeBHjZ7BF5UJ38j/pq8fsMohwhIJ io6A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733339664; x=1733944464; 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=gP/1ZRcuFjEEMybT5UMVVNt/puu4fP/kE3PemhzVNvM=; b=H5DjKDpXCUlh6UTkY+H9eKpdZLo36HV7Wpttv1xgTtlZhWPMipk4pSxZi+OsMBtmvs zdsaenkI49T4EgZkNwOx6scwFMXz/uznbE4k0CDohvqQnahg0aak/yJRNnkeqkzVH51k nzjQM5VxI6fkOgU/i+ouap9ARmbCeGqgc1T3y8WQIMFyjo96hkz43VWX2k7Ab7a/Kx9d NuYPHuya2dYFhdxZXkFSNjn6cNMm2UOJLscEOf+5XGknsRvajAA3s1HP09d6ddzXg1w9 hsMQNzufeL0Tc2Sw+RrdYEh0nSvY0TxWNBy1FpZBuaJPK4KxYP5zem353JpYVW1AMtFn lYnQ== X-Forwarded-Encrypted: i=1; AJvYcCU+GUCDAmhxsUjv4IGh82kPMs89DBB05bLQC/x6q/5xcLRZcSSL1mXetZS1abK6BpF1LWbSRjFpLOR5ZNdgOCKV@lists.infradead.org X-Gm-Message-State: AOJu0Yzh1sSMsiSi91h4qoCF2zNb/6lahytkshQ775FtBWHB9GvEeeRL g7rUqGT2/HavgaOPv5mq7IeeHDBzxDwRYbqNL0WQ0LpxcOkqJYFFfO8R5aR0an7zM86IOPIhO5W 0UJAYlZ+FuyTmaGhuUw== X-Google-Smtp-Source: AGHT+IGl8+Y6jXlfDxpaeYQWcz7MkBYaeVCfdRzcMWgZ4EOOrzmqxMS37+BVzh9ZHq4dtxMyhyVDFBoslVL+V2Wp X-Received: from uabih6.prod.google.com ([2002:a05:6130:1f06:b0:85b:c307:6bbd]) (user=jthoughton job=prod-delivery.src-stubby-dispatcher) by 2002:a05:620a:44c4:b0:7b1:52a9:ae1a with SMTP id af79cd13be357-7b6a5d2aa5bmr858557685a.4.1733339663968; Wed, 04 Dec 2024 11:14:23 -0800 (PST) Date: Wed, 4 Dec 2024 19:13:44 +0000 In-Reply-To: <20241204191349.1730936-1-jthoughton@google.com> Mime-Version: 1.0 References: <20241204191349.1730936-1-jthoughton@google.com> X-Mailer: git-send-email 2.47.0.338.g60cca15819-goog Message-ID: <20241204191349.1730936-10-jthoughton@google.com> Subject: [PATCH v1 09/13] KVM: selftests: Add va_start/end into uffd_desc From: James Houghton To: Paolo Bonzini , Sean Christopherson Cc: Jonathan Corbet , Marc Zyngier , Oliver Upton , Yan Zhao , James Houghton , Nikita Kalyazin , Anish Moorthy , Peter Gonda , Peter Xu , David Matlack , Wang@google.com, Wei W , kvm@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241204_111425_558920_A120186A X-CRM114-Status: GOOD ( 11.87 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org This will be used for the self-test to look up which userfaultfd we should be using when handling a KVM Userfault (in the event KVM Userfault and userfaultfd are being used together). Signed-off-by: James Houghton --- tools/testing/selftests/kvm/include/userfaultfd_util.h | 2 ++ tools/testing/selftests/kvm/lib/userfaultfd_util.c | 2 ++ 2 files changed, 4 insertions(+) diff --git a/tools/testing/selftests/kvm/include/userfaultfd_util.h b/tools/testing/selftests/kvm/include/userfaultfd_util.h index 60f7f9d435dc..b62fecdfe745 100644 --- a/tools/testing/selftests/kvm/include/userfaultfd_util.h +++ b/tools/testing/selftests/kvm/include/userfaultfd_util.h @@ -30,6 +30,8 @@ struct uffd_desc { int *pipefds; pthread_t *readers; struct uffd_reader_args *reader_args; + void *va_start; + void *va_end; }; struct uffd_desc *uffd_setup_demand_paging(int uffd_mode, useconds_t delay, diff --git a/tools/testing/selftests/kvm/lib/userfaultfd_util.c b/tools/testing/selftests/kvm/lib/userfaultfd_util.c index 7c9de8414462..93004c85bcdc 100644 --- a/tools/testing/selftests/kvm/lib/userfaultfd_util.c +++ b/tools/testing/selftests/kvm/lib/userfaultfd_util.c @@ -152,6 +152,8 @@ struct uffd_desc *uffd_setup_demand_paging(int uffd_mode, useconds_t delay, expected_ioctls, "missing userfaultfd ioctls"); uffd_desc->uffd = uffd; + uffd_desc->va_start = hva; + uffd_desc->va_end = (char *)hva + len; for (i = 0; i < uffd_desc->num_readers; ++i) { int pipes[2]; From patchwork Wed Dec 4 19:13:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Houghton X-Patchwork-Id: 13894221 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id B448AE77170 for ; Wed, 4 Dec 2024 19:25:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To:From: Subject:Message-ID:References:Mime-Version:In-Reply-To:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=KKPTY6BDHY4CW5kRnLCY0vf32xyzYU5qegQAP5cDTvQ=; b=b1Cp9KlWUIUW7N71EXW9C9bg7s Eqptey1ujOlB4C1CvfKzH1xXlD0XyHivIb3t/w410RNywDVD11Ktm06HSbJiqfbAasi7hcrRy2VKL I9q8pN3ANo+p0kZ0Z+2cwdQHZul6wTjrlYokepWDz5jDubZcqLBgFcGNydXtU9dpRgWYEWaJwU2j8 JJbMFdSNpeOs4w7tLOk6wPI/om1phGVZNOim1k6iYRTS6OKvv9sh8NGLjToxSlKt9pb6VmxH1gnbX dL2NIKeiJNvBXDhNLFYj103XKSTlJkFa1V8bGqcLRlPCPZ56d8pskLrq2FqYRVeu6pQdxDN6wETki EQ0simZg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tIv0K-0000000DfxF-4BbF; Wed, 04 Dec 2024 19:25:45 +0000 Received: from mail-oo1-xc4a.google.com ([2607:f8b0:4864:20::c4a]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tIupN-0000000Ddm7-3uDG for linux-arm-kernel@lists.infradead.org; Wed, 04 Dec 2024 19:14:27 +0000 Received: by mail-oo1-xc4a.google.com with SMTP id 006d021491bc7-5eb78268680so109660eaf.3 for ; Wed, 04 Dec 2024 11:14:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1733339665; x=1733944465; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=KKPTY6BDHY4CW5kRnLCY0vf32xyzYU5qegQAP5cDTvQ=; b=MQiQoiVxuHXTA3bh/M7qZk+zxUtVC/Y/t9nK4myGifIZvs3fJcec91MIE6CwPuolGg GKYvMVUAbl1tczOuImJP8nf3d5pwt2XhG2LmBXG1KRgUlr8ehLdJ07Mig8nTCgyo+xh5 ixr41CnIbo8EJpg6n6isoAnsicgtVwO4BszUcB9qwZFHahbddjVWZhH20QbZYHNt7oDd j3SNghGZQi12O4BrqG4Hl520U2UmkPkJpfKlZP8Fmg1IeQS1rKBQja46d7JirITOOLFS C/+PK/P1iPfH8e9Xwdn+EHiMn8cduJz79W+B/EZ6yQM2+4iNq/PVsWR65tJxT9r4+Juy YrBQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733339665; x=1733944465; 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=KKPTY6BDHY4CW5kRnLCY0vf32xyzYU5qegQAP5cDTvQ=; b=n012+mtuWe5hZsb75Dma6hjkD6H/zI5O/bACoFslcGu+P2cpBqywBwrkbkYhgZ+41E ulYnC8Gegz9w7kqHtzSk6YBIGtWl3nrj1nXG63/uVXAva3nzSLyp4UQbwTILEGuWtCIt bogDIdF02/3sM6hXle0h2aoHTiy/CLmyPcb1fruNgQoWHrqEV6jZDCBsv7OvDO7+whlq qWs0IlCkpkP65HhuRv47xaNElKdqMZ4OmG7S24lR+2SE/02daqRE+fovYA8+mbJi3u8R 3DXgLI9k7u1buwTXL/kLlfDZnJ0S8vtBOrdIXVjzKQw9Aqi9SkKpWBg3pf7JwrOqElks 4VzA== X-Forwarded-Encrypted: i=1; AJvYcCWAVUlBK7NLSakov1eZOU5i/fhgPS7EytgND8TykR9yTkfrcfS6gyeH8liTcAMLGAX1uz605lul0MZ9ucvhC9lZ@lists.infradead.org X-Gm-Message-State: AOJu0Yzvw7AlJ1Wg7fVxfhaYJ9+CcBQY1lYoQlStstc1eIpBjl1N2S1B kE5qtQeyJuTae6n86anW6ivg4qM5ytOddgMY/P/WPlVRjffLLxVeZg1I2UgZQaXgm7rD75KF7Pc N49L7uMubvIXWSsgdAw== X-Google-Smtp-Source: AGHT+IGRmJAl02GEWZ0VoWDM6bNhmNgX+yqmM2tYJtx0vayqcYvuWF0Zwi1GxJiTBLfcAQSnK2xzxxMPkd5JZHvn X-Received: from uabix20.prod.google.com ([2002:a05:6130:6514:b0:85b:6555:97ec]) (user=jthoughton job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6358:5bc8:b0:1ca:c972:190d with SMTP id e5c5f4694b2df-1caeabfbddamr693736855d.25.1733339664949; Wed, 04 Dec 2024 11:14:24 -0800 (PST) Date: Wed, 4 Dec 2024 19:13:45 +0000 In-Reply-To: <20241204191349.1730936-1-jthoughton@google.com> Mime-Version: 1.0 References: <20241204191349.1730936-1-jthoughton@google.com> X-Mailer: git-send-email 2.47.0.338.g60cca15819-goog Message-ID: <20241204191349.1730936-11-jthoughton@google.com> Subject: [PATCH v1 10/13] KVM: selftests: Add KVM Userfault mode to demand_paging_test From: James Houghton To: Paolo Bonzini , Sean Christopherson Cc: Jonathan Corbet , Marc Zyngier , Oliver Upton , Yan Zhao , James Houghton , Nikita Kalyazin , Anish Moorthy , Peter Gonda , Peter Xu , David Matlack , Wang@google.com, Wei W , kvm@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241204_111425_974952_8F068701 X-CRM114-Status: GOOD ( 30.39 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Add a way for the KVM_RUN loop to handle -EFAULT exits when they are for KVM_MEMORY_EXIT_FLAG_USERFAULT. In this case, preemptively handle the UFFDIO_COPY or UFFDIO_CONTINUE if userfaultfd is also in use. This saves the trip through the userfaultfd poll/read/WAKE loop. When preemptively handling UFFDIO_COPY/CONTINUE, do so with MODE_DONTWAKE, as there will not be a thread to wake. If a thread *does* take the userfaultfd slow path, we will get a regular userfault, and we will call handle_uffd_page_request() which will do a full wake-up. In the EEXIST case, a wake-up will not occur. Make sure to call UFFDIO_WAKE explicitly in this case. When handling KVM userfaults, make sure to set the bitmap with memory_order_release. Although it wouldn't affect the functionality of the test (because memstress doesn't actually require any particular guest memory contents), it is what userspace normally needs to do. Add `-k` to set the test to use KVM Userfault. Add the vm_mem_region_set_flags_userfault() helper for setting `userfault_bitmap` and KVM_MEM_USERFAULT at the same time. Signed-off-by: James Houghton --- .../selftests/kvm/demand_paging_test.c | 139 +++++++++++++++++- .../testing/selftests/kvm/include/kvm_util.h | 5 + tools/testing/selftests/kvm/lib/kvm_util.c | 40 ++++- 3 files changed, 176 insertions(+), 8 deletions(-) diff --git a/tools/testing/selftests/kvm/demand_paging_test.c b/tools/testing/selftests/kvm/demand_paging_test.c index 315f5c9037b4..e7ea1c57264d 100644 --- a/tools/testing/selftests/kvm/demand_paging_test.c +++ b/tools/testing/selftests/kvm/demand_paging_test.c @@ -12,7 +12,9 @@ #include #include #include +#include #include +#include #include "kvm_util.h" #include "test_util.h" @@ -24,11 +26,21 @@ #ifdef __NR_userfaultfd static int nr_vcpus = 1; +static int num_uffds; static uint64_t guest_percpu_mem_size = DEFAULT_PER_VCPU_MEM_SIZE; static size_t demand_paging_size; +static size_t host_page_size; static char *guest_data_prototype; +static struct { + bool enabled; + int uffd_mode; /* set if userfaultfd is also in use */ + struct uffd_desc **uffd_descs; +} kvm_userfault_data; + +static void resolve_kvm_userfault(u64 gpa, u64 size); + static void vcpu_worker(struct memstress_vcpu_args *vcpu_args) { struct kvm_vcpu *vcpu = vcpu_args->vcpu; @@ -41,8 +53,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 && kvm_userfault_data.enabled) { + /* Check for userfault. */ + TEST_ASSERT(run->exit_reason == KVM_EXIT_MEMORY_FAULT, + "Got invalid exit reason: %x", 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", @@ -54,11 +80,10 @@ static void vcpu_worker(struct memstress_vcpu_args *vcpu_args) ts_diff.tv_sec, ts_diff.tv_nsec); } -static int handle_uffd_page_request(int uffd_mode, int uffd, - struct uffd_msg *msg) +static int resolve_uffd_page_request(int uffd_mode, int uffd, uint64_t addr, + bool wake) { pid_t tid = syscall(__NR_gettid); - uint64_t addr = msg->arg.pagefault.address; struct timespec start; struct timespec ts_diff; int r; @@ -71,7 +96,7 @@ static int handle_uffd_page_request(int uffd_mode, int uffd, copy.src = (uint64_t)guest_data_prototype; copy.dst = addr; copy.len = demand_paging_size; - copy.mode = 0; + copy.mode = wake ? 0 : UFFDIO_COPY_MODE_DONTWAKE; r = ioctl(uffd, UFFDIO_COPY, ©); /* @@ -96,6 +121,7 @@ static int handle_uffd_page_request(int uffd_mode, int uffd, cont.range.start = addr; cont.range.len = demand_paging_size; + cont.mode = wake ? 0 : UFFDIO_CONTINUE_MODE_DONTWAKE; r = ioctl(uffd, UFFDIO_CONTINUE, &cont); /* @@ -119,6 +145,20 @@ static int handle_uffd_page_request(int uffd_mode, int uffd, TEST_FAIL("Invalid uffd mode %d", uffd_mode); } + if (r < 0 && wake) { + /* + * No wake-up occurs when UFFDIO_COPY/CONTINUE fails, but we + * have a thread waiting. Wake it up. + */ + struct uffdio_range range = {0}; + + range.start = addr; + range.len = demand_paging_size; + + TEST_ASSERT(ioctl(uffd, UFFDIO_WAKE, &range) == 0, + "UFFDIO_WAKE failed: 0x%lx", addr); + } + ts_diff = timespec_elapsed(start); PER_PAGE_DEBUG("UFFD page-in %d \t%ld ns\n", tid, @@ -129,6 +169,58 @@ static int handle_uffd_page_request(int uffd_mode, int uffd, return 0; } +static int handle_uffd_page_request(int uffd_mode, int uffd, + struct uffd_msg *msg) +{ + uint64_t addr = msg->arg.pagefault.address; + + return resolve_uffd_page_request(uffd_mode, uffd, addr, true); +} + +static void resolve_kvm_userfault(u64 gpa, u64 size) +{ + struct kvm_vm *vm = memstress_args.vm; + struct userspace_mem_region *region; + unsigned long *bitmap_chunk; + u64 page, gpa_offset; + + region = (struct userspace_mem_region *) userspace_mem_region_find( + vm, gpa, (gpa + size - 1)); + + if (kvm_userfault_data.uffd_mode) { + /* + * Resolve userfaults early, without needing to read them + * off the userfaultfd. + */ + uint64_t hva = (uint64_t)addr_gpa2hva(vm, gpa); + struct uffd_desc **descs = kvm_userfault_data.uffd_descs; + int i, fd; + + for (i = 0; i < num_uffds; ++i) + if (hva >= (uint64_t)descs[i]->va_start && + hva < (uint64_t)descs[i]->va_end) + break; + + TEST_ASSERT(i < num_uffds, + "Did not find userfaultfd for hva: %lx", hva); + + fd = kvm_userfault_data.uffd_descs[i]->uffd; + resolve_uffd_page_request(kvm_userfault_data.uffd_mode, fd, + hva, false); + } else { + uint64_t hva = (uint64_t)addr_gpa2hva(vm, gpa); + + memcpy((char *)hva, guest_data_prototype, demand_paging_size); + } + + gpa_offset = gpa - region->region.guest_phys_addr; + page = gpa_offset / host_page_size; + bitmap_chunk = (unsigned long *)region->region.userfault_bitmap + + page / BITS_PER_LONG; + atomic_fetch_and_explicit(bitmap_chunk, + ~(1ul << (page % BITS_PER_LONG)), memory_order_release); +} + struct test_params { int uffd_mode; bool single_uffd; @@ -136,6 +228,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) @@ -149,6 +242,25 @@ static void prefault_mem(void *alias, uint64_t len) } } +static void enable_userfault(struct kvm_vm *vm, int slots) +{ + for (int i = 0; i < slots; ++i) { + int slot = MEMSTRESS_MEM_SLOT_INDEX + i; + struct userspace_mem_region *region; + unsigned long *userfault_bitmap; + int flags = KVM_MEM_USERFAULT; + + region = memslot2region(vm, slot); + userfault_bitmap = bitmap_zalloc(region->mmap_size / + host_page_size); + /* everything is userfault initially */ + memset(userfault_bitmap, -1, region->mmap_size / host_page_size / CHAR_BIT); + printf("Setting bitmap: %p\n", userfault_bitmap); + vm_mem_region_set_flags_userfault(vm, slot, flags, + userfault_bitmap); + } +} + static void run_test(enum vm_guest_mode mode, void *arg) { struct memstress_vcpu_args *vcpu_args; @@ -159,12 +271,13 @@ static void run_test(enum vm_guest_mode mode, void *arg) struct timespec ts_diff; double vcpu_paging_rate; struct kvm_vm *vm; - int i, num_uffds = 0; + int i; vm = memstress_create_vm(mode, nr_vcpus, guest_percpu_mem_size, 1, p->src_type, p->partition_vcpu_memory_access); demand_paging_size = get_backing_src_pagesz(p->src_type); + host_page_size = getpagesize(); guest_data_prototype = malloc(demand_paging_size); TEST_ASSERT(guest_data_prototype, @@ -208,6 +321,14 @@ static void run_test(enum vm_guest_mode mode, void *arg) } } + if (p->kvm_userfault) { + TEST_REQUIRE(kvm_has_cap(KVM_CAP_USERFAULT)); + kvm_userfault_data.enabled = true; + kvm_userfault_data.uffd_mode = p->uffd_mode; + kvm_userfault_data.uffd_descs = uffd_descs; + enable_userfault(vm, 1); + } + pr_info("Finished creating vCPUs and starting uffd threads\n"); clock_gettime(CLOCK_MONOTONIC, &start); @@ -265,6 +386,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); } @@ -283,7 +405,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); @@ -326,6 +448,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]); diff --git a/tools/testing/selftests/kvm/include/kvm_util.h b/tools/testing/selftests/kvm/include/kvm_util.h index bc7c242480d6..7fec3559aa64 100644 --- a/tools/testing/selftests/kvm/include/kvm_util.h +++ b/tools/testing/selftests/kvm/include/kvm_util.h @@ -582,6 +582,8 @@ void vm_userspace_mem_region_add(struct kvm_vm *vm, void vm_mem_add(struct kvm_vm *vm, enum vm_mem_backing_src_type src_type, uint64_t guest_paddr, uint32_t slot, uint64_t npages, uint32_t flags, int guest_memfd_fd, uint64_t guest_memfd_offset); +struct userspace_mem_region * +userspace_mem_region_find(struct kvm_vm *vm, uint64_t start, uint64_t end); #ifndef vm_arch_has_protected_memory static inline bool vm_arch_has_protected_memory(struct kvm_vm *vm) @@ -591,6 +593,9 @@ static inline bool vm_arch_has_protected_memory(struct kvm_vm *vm) #endif void vm_mem_region_set_flags(struct kvm_vm *vm, uint32_t slot, uint32_t flags); +void vm_mem_region_set_flags_userfault(struct kvm_vm *vm, uint32_t slot, + uint32_t flags, + unsigned long *userfault_bitmap); void vm_mem_region_move(struct kvm_vm *vm, uint32_t slot, uint64_t new_gpa); void vm_mem_region_delete(struct kvm_vm *vm, uint32_t slot); struct kvm_vcpu *__vm_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id); diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c index 9603f99d3247..7195dd3db5df 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -634,7 +634,7 @@ void kvm_parse_vcpu_pinning(const char *pcpus_string, uint32_t vcpu_to_pcpu[], * of the regions is returned. Null is returned only when no overlapping * region exists. */ -static struct userspace_mem_region * +struct userspace_mem_region * userspace_mem_region_find(struct kvm_vm *vm, uint64_t start, uint64_t end) { struct rb_node *node; @@ -1149,6 +1149,44 @@ void vm_mem_region_set_flags(struct kvm_vm *vm, uint32_t slot, uint32_t flags) ret, errno, slot, flags); } +/* + * VM Memory Region Flags Set with a userfault bitmap + * + * Input Args: + * vm - Virtual Machine + * flags - Flags for the memslot + * userfault_bitmap - The bitmap to use for KVM_MEM_USERFAULT + * + * Output Args: None + * + * Return: None + * + * Sets the flags of the memory region specified by the value of slot, + * to the values given by flags. This helper adds a way to provide a + * userfault_bitmap. + */ +void vm_mem_region_set_flags_userfault(struct kvm_vm *vm, uint32_t slot, + uint32_t flags, + unsigned long *userfault_bitmap) +{ + int ret; + struct userspace_mem_region *region; + + region = memslot2region(vm, slot); + + TEST_ASSERT(!userfault_bitmap ^ (flags & KVM_MEM_USERFAULT), + "KVM_MEM_USERFAULT must be specified with a bitmap"); + + region->region.flags = flags; + region->region.userfault_bitmap = (__u64)userfault_bitmap; + + ret = __vm_ioctl(vm, KVM_SET_USER_MEMORY_REGION2, ®ion->region); + + TEST_ASSERT(ret == 0, "KVM_SET_USER_MEMORY_REGION2 IOCTL failed,\n" + " rc: %i errno: %i slot: %u flags: 0x%x", + ret, errno, slot, flags); +} + /* * VM Memory Region Move * From patchwork Wed Dec 4 19:13:46 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Houghton X-Patchwork-Id: 13894222 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id B03FCE77170 for ; Wed, 4 Dec 2024 19:26:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To:From: Subject:Message-ID:References:Mime-Version:In-Reply-To:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=s5zlVcABwgcAqDrk/hYCqATZ71WnZcE4eWZqt1Al9mM=; b=ZCd3xqCpzd6t4xGO05uozfGBDp XcFChRefesZyGeORWXOS1AHz1DnIYB4wE51FaINcXphYuFUClLZbGIIOWn6i+QI/FioMLieUJhkBt +qlaHyNs+AaS8hoM5rLKUFT9MJBPklWI+6LTCpIEhsC+Xb7J4FpidLzvUPy30fHs+FcMilX3UQ4K5 qJOaBC9zypLnmtyEWK9e4idldcpGVDp5UktjE2Qj+JqiFXh7ws4D0LdRvlbt1wC/lF4UA7dTWXZT8 Un1iwBtk0EoMu6J4dWb8HtR3x3IUFhiSdqy2V6cen4JDTM1jnEE56xg9sqbzS/3ZUmEyC7hdR9JkQ atNzqPaA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tIv1J-0000000Dg7l-2WVT; Wed, 04 Dec 2024 19:26:45 +0000 Received: from mail-vs1-xe4a.google.com ([2607:f8b0:4864:20::e4a]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tIupQ-0000000Ddn6-05VP for linux-arm-kernel@lists.infradead.org; Wed, 04 Dec 2024 19:14:29 +0000 Received: by mail-vs1-xe4a.google.com with SMTP id ada2fe7eead31-4afa80fda63so10659137.2 for ; Wed, 04 Dec 2024 11:14:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1733339666; x=1733944466; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=s5zlVcABwgcAqDrk/hYCqATZ71WnZcE4eWZqt1Al9mM=; b=3fngGQe663BGMUt+iQpvjVg4RjeEfOiZrjEAtQWMY5CFemmb7MUOKKIfbGQH39iva+ zrO2J2LdZ9mofwnRd5y3NUd2xOYlfeGOg7BDplCaMJ9I6tWPzkTwS5EVWS7/Q65QXKRg wvZaz2L2UDwEf+BtAn+3pNftjqHn6GCE+jdsA8cb8eUoV1vICivxTxaMA/w0KHKE03Lv qoSL10kmXYUvRTxwhdqb2PV++d+OsOQgpNt992y6gnvKlRp30CZO2c+wICwZF1sdzmVY RjEcB8ulCCxQ9k1htP/1hbHHru+huY6xQwlcFk6HwyCOWPUo74IvUyb3IjmdgICEo3Jl 60CA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733339666; x=1733944466; 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=s5zlVcABwgcAqDrk/hYCqATZ71WnZcE4eWZqt1Al9mM=; b=e85/PutlAbPkfAzZ2jxi+5ksbFUwMrsdlS7+5JnEEw2upM9HuzK9RYb6+q2WmDif0u STZnF8M28VlQZWup7wFv25utJQLv0wSCxrvMh39uQxRx9wEtvJ+DzNfkAP8coLFCmQFb xDr/e06OdxI8YEdoZAlDDS+nxrZu6OhanuSbx+N1I/NzPB2NdVDEnaEmn+kw2NYj+Dhb W7NFNrnt8HXL8YN1A+Eu6TiRwhr2GO32BMEf6n2biZGxXfFVDmyJsPgHvppqm2KnmHUO COj1AXYnSt57NmL1SsJpeBO3sKbbEfHiez6iJAvgA3l3XjnwKBwKZGJi1+WMSg7FqL7i iK9A== X-Forwarded-Encrypted: i=1; AJvYcCU1iIy73JqA8PlDbJzKlMABk3X/x9TxTFs/stP25wugJP0UxVJ05Z3t8IcYaJgsRK86TJAiOdqyECXDXvI729wa@lists.infradead.org X-Gm-Message-State: AOJu0YxhDiVIQsMTnwZvKZkADWyhNM2XDN/EArPAiu63pjXol/yMDwfc p/9ayc+8Wj2Rac0ElyJRmPvI4mx7AX7/3C1rYRYxvjv0Uo9S9j7Nz0ceIxALT0cAzfaeqoAd1hR hVgQW9ks7z5SX665qmQ== X-Google-Smtp-Source: AGHT+IF2FEM1OG8guhI8P1BVhXWIFhfslfAcmBKz5oOAxjNRJSTj9A5XfYD8J6BgPkOCk8O5eB0FX2IaOdHwSH+I X-Received: from vsvj20.prod.google.com ([2002:a05:6102:3e14:b0:4af:5a9b:90bd]) (user=jthoughton job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6102:3ed0:b0:4a3:ab95:9637 with SMTP id ada2fe7eead31-4af97243dc7mr11657456137.12.1733339666111; Wed, 04 Dec 2024 11:14:26 -0800 (PST) Date: Wed, 4 Dec 2024 19:13:46 +0000 In-Reply-To: <20241204191349.1730936-1-jthoughton@google.com> Mime-Version: 1.0 References: <20241204191349.1730936-1-jthoughton@google.com> X-Mailer: git-send-email 2.47.0.338.g60cca15819-goog Message-ID: <20241204191349.1730936-12-jthoughton@google.com> Subject: [PATCH v1 11/13] KVM: selftests: Inform set_memory_region_test of KVM_MEM_USERFAULT From: James Houghton To: Paolo Bonzini , Sean Christopherson Cc: Jonathan Corbet , Marc Zyngier , Oliver Upton , Yan Zhao , James Houghton , Nikita Kalyazin , Anish Moorthy , Peter Gonda , Peter Xu , David Matlack , Wang@google.com, Wei W , kvm@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241204_111428_054723_F8B218FA X-CRM114-Status: GOOD ( 11.18 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org The KVM_MEM_USERFAULT flag is supported iff KVM_CAP_USERFAULT is available. Signed-off-by: James Houghton --- tools/testing/selftests/kvm/set_memory_region_test.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tools/testing/selftests/kvm/set_memory_region_test.c b/tools/testing/selftests/kvm/set_memory_region_test.c index a8267628e9ed..d233cdfb0241 100644 --- a/tools/testing/selftests/kvm/set_memory_region_test.c +++ b/tools/testing/selftests/kvm/set_memory_region_test.c @@ -364,6 +364,9 @@ static void test_invalid_memory_region_flags(void) if (kvm_check_cap(KVM_CAP_MEMORY_ATTRIBUTES) & KVM_MEMORY_ATTRIBUTE_PRIVATE) supported_flags |= KVM_MEM_GUEST_MEMFD; + if (kvm_check_cap(KVM_CAP_USERFAULT)) + supported_flags |= KVM_MEM_USERFAULT; + for (i = 0; i < 32; i++) { if ((supported_flags & BIT(i)) && !(v2_only_flags & BIT(i))) continue; From patchwork Wed Dec 4 19:13:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Houghton X-Patchwork-Id: 13894223 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 1B22FE77170 for ; Wed, 4 Dec 2024 19:27:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To:From: Subject:Message-ID:References:Mime-Version:In-Reply-To:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=X68RCfWU06ROg+bUYFUfNwQoohzZnJeYoYKE3JS2eGc=; b=j3pu6UzwAWvdba9yhTy4np9ngD 2sBNWfoAf3Kyg4Z1fOCmw4yWQxIXGpskwsmW/SPMqVJNJmGHWETZkqzrDj7ekOIUORim8i6rSdGo5 WBJkNqAmBcd++lrqeO7B9ulBlu0HQitfY4wsLvGDnn0W7bLuXm0fCmBUieBAMucZF6H3LLFxBxktY SWrkc+5we3GmcTyPV/FITBjGziS55G2ZJWXjMYa7oYTduIB5UtwXvatnNOMijetJQ1L9BdfnU9TpX wJGTLCnj4ZI3KpOroPyYEAcq0xHEGY8fMprDx9HYpOBi4RaJ5RDqKnIE4zjoJgAzjghUIA4yCZmUi ygtaFBaA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tIv2I-0000000DgIL-15QW; Wed, 04 Dec 2024 19:27:46 +0000 Received: from mail-vk1-xa4a.google.com ([2607:f8b0:4864:20::a4a]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tIupQ-0000000Ddo8-1icA for linux-arm-kernel@lists.infradead.org; Wed, 04 Dec 2024 19:14:29 +0000 Received: by mail-vk1-xa4a.google.com with SMTP id 71dfb90a1353d-515d07b1251so54671e0c.0 for ; Wed, 04 Dec 2024 11:14:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1733339667; x=1733944467; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=X68RCfWU06ROg+bUYFUfNwQoohzZnJeYoYKE3JS2eGc=; b=lphSWCurRDHFWBGoSHfp/jH3A6z6ENNhmGycxy05U/3liRozy9ASXsQya+an+bsxBT 9aJnjrp73huiTou5EZDAg4HlTd2YH5fUM39o62F/7uvFjk1JwiJh32UrhDIRWJ3mSB08 Nhqxg+31J37Grtr8c/7qMCvZyhVKtz27lyH1oitMV8lDiJvPECi/oUbaGMX7cXIgiTZW zhA9Z2Jt1JErg6M1IIB5D0gyZ4UOnKEh+IAcgTWFbR7Hipd+ZVdaEQ/EvQ5nJ/g/nb79 ripRYo1pYuUav7Sm2n1gM/ILhq8qErrOGRa7au72jOuabxd5O04Bct+0enoOX/B9FZbj FN9w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733339667; x=1733944467; 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=X68RCfWU06ROg+bUYFUfNwQoohzZnJeYoYKE3JS2eGc=; b=DOo9I6zrkDxboptS6uFaaR6j8bTJDSDbGSGDJx89dBIZeqQyFq6Q3m4KtsJ2SaOtWP rFRjmjspp1Lv+mz26+8lyP8/Sm0w5HLUfuqFUxWNZFyJdy2Pd2RfmFRKV53LdNDZ/+pL 3QbgEahpr1+CZaVyMr3b8AJqpklxoW7Zh6xt7cOzTFKK1SsyMdk81YxpxqLH8crjeA17 l3WL4rs0VkpVzwMiYI3OZ/W10/hPd+8JALVJj2pd35znH3UCforlcxSKTLJwhy+9IzM3 YdXfCoUO0szU0mbv+KOTSXn2CTYSkfZwqtmfQNUdBNCtSLHxjkdFegG3FspgwQVmMv4z fQkQ== X-Forwarded-Encrypted: i=1; AJvYcCXuQ43g/2We4AC5SwIcMoHgpDHzHAYRdGbo3ldoB/TuHOxqEKiFrFDRmmbOCjWYc/R067OLbbZfu6CJgTSbZTzQ@lists.infradead.org X-Gm-Message-State: AOJu0YwqcQjrs1VwkoC2+/9jm+rRYQI1bCgsfTcTo15AIVa863REbG8v X4F9QTk4oKBzUvTNrxBpqvK13AdzEXxtD3GMDcGqYkEuljgC782Ltv6eBKdx7TbC5o/eMgtNx4P /Vcu6XRpYT7pt8TO13w== X-Google-Smtp-Source: AGHT+IGSL6/LfQGJ9Uj3DkWwK1IsrYPhpONzXFegfmhrDCtj+kw22erCa4lpakX9RjG5PxsHges9rOrFeGcBxOQ3 X-Received: from vkbr6.prod.google.com ([2002:a05:6122:6606:b0:515:3ac1:ec42]) (user=jthoughton job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6122:a25:b0:515:5008:118d with SMTP id 71dfb90a1353d-515bf2a393bmr10094302e0c.2.1733339666972; Wed, 04 Dec 2024 11:14:26 -0800 (PST) Date: Wed, 4 Dec 2024 19:13:47 +0000 In-Reply-To: <20241204191349.1730936-1-jthoughton@google.com> Mime-Version: 1.0 References: <20241204191349.1730936-1-jthoughton@google.com> X-Mailer: git-send-email 2.47.0.338.g60cca15819-goog Message-ID: <20241204191349.1730936-13-jthoughton@google.com> Subject: [PATCH v1 12/13] KVM: selftests: Add KVM_MEM_USERFAULT + guest_memfd toggle tests From: James Houghton To: Paolo Bonzini , Sean Christopherson Cc: Jonathan Corbet , Marc Zyngier , Oliver Upton , Yan Zhao , James Houghton , Nikita Kalyazin , Anish Moorthy , Peter Gonda , Peter Xu , David Matlack , Wang@google.com, Wei W , kvm@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241204_111428_449477_80AF4F11 X-CRM114-Status: GOOD ( 10.84 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Make sure KVM_MEM_USERFAULT can be toggled on and off for KVM_MEM_GUEST_MEMFD memslots. Signed-off-by: James Houghton --- .../selftests/kvm/set_memory_region_test.c | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/tools/testing/selftests/kvm/set_memory_region_test.c b/tools/testing/selftests/kvm/set_memory_region_test.c index d233cdfb0241..57b7032d7cc3 100644 --- a/tools/testing/selftests/kvm/set_memory_region_test.c +++ b/tools/testing/selftests/kvm/set_memory_region_test.c @@ -556,6 +556,35 @@ static void test_add_overlapping_private_memory_regions(void) close(memfd); kvm_vm_free(vm); } + +static void test_private_memory_region_userfault(void) +{ + struct kvm_vm *vm; + int memfd; + + pr_info("Testing toggling KVM_MEM_USERFAULT on KVM_MEM_GUEST_MEMFD memory regions\n"); + + vm = vm_create_barebones_type(KVM_X86_SW_PROTECTED_VM); + + test_invalid_guest_memfd(vm, vm->kvm_fd, 0, "KVM fd should fail"); + test_invalid_guest_memfd(vm, vm->fd, 0, "VM's fd should fail"); + + memfd = vm_create_guest_memfd(vm, MEM_REGION_SIZE, 0); + + vm_set_user_memory_region2(vm, MEM_REGION_SLOT, KVM_MEM_GUEST_MEMFD, + MEM_REGION_GPA, MEM_REGION_SIZE, 0, memfd, 0); + + vm_set_user_memory_region2(vm, MEM_REGION_SLOT, + KVM_MEM_GUEST_MEMFD | KVM_MEM_USERFAULT, + MEM_REGION_GPA, MEM_REGION_SIZE, 0, memfd, 0); + + vm_set_user_memory_region2(vm, MEM_REGION_SLOT, KVM_MEM_GUEST_MEMFD, + MEM_REGION_GPA, MEM_REGION_SIZE, 0, memfd, 0); + + close(memfd); + + kvm_vm_free(vm); +} #endif int main(int argc, char *argv[]) @@ -582,6 +611,7 @@ int main(int argc, char *argv[]) (kvm_check_cap(KVM_CAP_VM_TYPES) & BIT(KVM_X86_SW_PROTECTED_VM))) { test_add_private_memory_region(); test_add_overlapping_private_memory_regions(); + test_private_memory_region_userfault(); } else { pr_info("Skipping tests for KVM_MEM_GUEST_MEMFD memory regions\n"); } From patchwork Wed Dec 4 19:13:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Houghton X-Patchwork-Id: 13894224 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 4A7B8E7716D for ; Wed, 4 Dec 2024 19:29:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To:From: Subject:Message-ID:References:Mime-Version:In-Reply-To:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=FVmBABSFHx5R5MTquILKgGxl08w4PCC+wHr0khmGJK8=; b=zEg965TyKVCJPTReKc/wnxHla3 stx7yfp5i/7stjunOPDcZufnpe9UxzZ9lSI0t7rpZc0VTzpjuBohnMTJBCc/sRp7mC+G4X616iydq 8xFGN1anKGQLQMInLV3uKk3gJM91cx1Y+p0aHe3fAmzZ7nzD5cSbWOpnwzlPyg5sYFQktBg8M7LZv rN/pn7S2QplNVmREgqpTbAMN59+foHuBaq4KGmg+W4xeJAZh+Fkn/wE8Xzua38GGhTgUfp14JD0n7 y+kLvKG6xdjGtlUBLtggzaWHaWLucR8PlheDt4jGCTdRKGWFiEHlPxGdgBGWXwH7vb5U2Huo1M/oo 3lB5el0Q==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tIv3G-0000000DgRQ-43dV; Wed, 04 Dec 2024 19:28:46 +0000 Received: from mail-vk1-xa4a.google.com ([2607:f8b0:4864:20::a4a]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tIupR-0000000Ddor-0aeE for linux-arm-kernel@lists.infradead.org; Wed, 04 Dec 2024 19:14:30 +0000 Received: by mail-vk1-xa4a.google.com with SMTP id 71dfb90a1353d-5152909933bso41989e0c.2 for ; Wed, 04 Dec 2024 11:14:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1733339668; x=1733944468; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=FVmBABSFHx5R5MTquILKgGxl08w4PCC+wHr0khmGJK8=; b=z3blW8PIRKYHnXXl0bTyaeT037krXI2WXM8+j1GnPbIwB8n3aHCqTtiCOqDHkQ1UyO MqOYNJcOwOY+CZxHT2kWYsyenjbr+9HUHB2jBWmMnvkdJDpmSAhiSZ/e62wQ/yX4S7Er MxdQWLgt8GhpmfYKKliXLKNpF+HpxPN/aM4cBFDEFcbzVNsU9SJ/BwvrcL2ushr7/IN5 E7ZqC+mgKrBs12GhLaP4JpAgnsNGQpKoj9gqMWCcwbNgXPFn4whfQNmK4wifQ4bvC1r8 ZX+kQbJCztneeZaxms6H7EJTQKJjybcEkFG7NGdgZEWZvOBIYg5z63rdoy3yCOrY6q6x mboA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733339668; x=1733944468; 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=FVmBABSFHx5R5MTquILKgGxl08w4PCC+wHr0khmGJK8=; b=ao9+FA9/fWJ8R+RMwHn5H4GH+XcrMefoYPF+2010k/DCXpE3evOqLcZhnodNT9KWNh ktOgFBsrZyA9mCGwtLsvZMAX8g05v5g4u01ymMG+GxVgO6oiWyxWfwPV10tKKIrzlNgn eURiRL4VSJwFMq6eJ9pJq8Pn0UuOHqaCEL4NCnhST7N5wf+U2ndRBZLKoUK7j686ZC1d 3Mc9/dfCT6UVxY3sNJi+adlm5dJPLc8dlOL1MZC5EtQ07GOn+OBAfClrAwsJppdLsyth bD19j6EL63HrLs9LihtXg9FwO/EPotp2toKQ2GEqe7u4XzsdeGbPkbKWWiCcaoNpQWqZ M8Vg== X-Forwarded-Encrypted: i=1; AJvYcCXbhSDNYx1IXOPre+TriK7vATBzVL7j8IgQDlR+52VUk2lg/EfdotctDtkZxilIirrgL43UBss9HYc4UEMnHBRF@lists.infradead.org X-Gm-Message-State: AOJu0YwZxCLj3KIsKzICeG1SzCcolNMnQFBuwXsX5bkSy8C/NaZp2sCX VkxCAiP6hoRDuPUy4J1JpYF8kBhOt9aI4O+4XBbuaV8XOxS1to7vNJ4qxK9o/QtLGf82qF/WiNt dBN54yBH/D9xnm0HUNQ== X-Google-Smtp-Source: AGHT+IH+tCG51tTfToGtalOfr4jm1GPLIEdYYz0WZu1LFU6o/sXkUUif2KKj20JmKczb7eMhosGsEIxjk4+0fihs X-Received: from vkbfs3.prod.google.com ([2002:a05:6122:3b83:b0:50d:9196:e944]) (user=jthoughton job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6122:4203:b0:515:26e7:736 with SMTP id 71dfb90a1353d-515bf307c0emr11029687e0c.6.1733339668025; Wed, 04 Dec 2024 11:14:28 -0800 (PST) Date: Wed, 4 Dec 2024 19:13:48 +0000 In-Reply-To: <20241204191349.1730936-1-jthoughton@google.com> Mime-Version: 1.0 References: <20241204191349.1730936-1-jthoughton@google.com> X-Mailer: git-send-email 2.47.0.338.g60cca15819-goog Message-ID: <20241204191349.1730936-14-jthoughton@google.com> Subject: [PATCH v1 13/13] KVM: Documentation: Add KVM_CAP_USERFAULT and KVM_MEM_USERFAULT details From: James Houghton To: Paolo Bonzini , Sean Christopherson Cc: Jonathan Corbet , Marc Zyngier , Oliver Upton , Yan Zhao , James Houghton , Nikita Kalyazin , Anish Moorthy , Peter Gonda , Peter Xu , David Matlack , Wang@google.com, Wei W , kvm@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241204_111429_196022_39B38D49 X-CRM114-Status: GOOD ( 16.49 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Include the note about memory ordering when clearing bits in userfault_bitmap, as it may not be obvious for users. Signed-off-by: James Houghton Reviewed-by: Bagas Sanjaya --- I would like to include the new -EFAULT reason in the documentation for KVM_RUN (the case where userfault_bitmap could not be read), as -EFAULT usually means that GUP failed. --- Documentation/virt/kvm/api.rst | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst index 454c2aaa155e..eec485dcf0bc 100644 --- a/Documentation/virt/kvm/api.rst +++ b/Documentation/virt/kvm/api.rst @@ -6281,7 +6281,8 @@ bounds checks apply (use common sense). __u64 guest_memfd_offset; __u32 guest_memfd; __u32 pad1; - __u64 pad2[14]; + __u64 userfault_bitmap; + __u64 pad2[13]; }; A KVM_MEM_GUEST_MEMFD region _must_ have a valid guest_memfd (private memory) and @@ -6297,6 +6298,25 @@ state. At VM creation time, all memory is shared, i.e. the PRIVATE attribute is '0' for all gfns. Userspace can control whether memory is shared/private by toggling KVM_MEMORY_ATTRIBUTE_PRIVATE via KVM_SET_MEMORY_ATTRIBUTES as needed. +When the KVM_MEM_USERFAULT flag is set, userfault_bitmap points to the starting +address for the bitmap that controls if vCPU memory faults should immediately +exit to userspace. If an invalid pointer is provided, at fault time, KVM_RUN +will return -EFAULT. KVM_MEM_USERFAULT is only supported when +KVM_CAP_USERFAULT is supported. + +userfault_bitmap should point to an array of longs where each bit in the array +linearly corresponds to a single gfn. Bit 0 in userfault_bitmap corresponds to +guest_phys_addr, bit 1 corresponds to guest_phys_addr + PAGE_SIZE, etc. If the +bit for a page is set, any vCPU access to that page will exit to userspace with +KVM_MEMORY_EXIT_FLAG_USERFAULT. + +Setting bits in userfault_bitmap has no effect on pages that have already been +mapped by KVM until KVM_MEM_USERFAULT is disabled and re-enabled again. + +Clearing bits in userfault_bitmap should usually be done with a store-release +if changes to guest memory are being made available to the guest via +userfault_bitmap. + S390: ^^^^^ @@ -8251,6 +8271,17 @@ KVM exits with the register state of either the L1 or L2 guest depending on which executed at the time of an exit. Userspace must take care to differentiate between these cases. +7.37 KVM_CAP_USERFAULT +---------------------- + +:Architectures: x86, arm64 +:Returns: Informational only, -EINVAL on direct KVM_ENABLE_CAP. + +The presence of this capability indicates that KVM_SET_USER_MEMORY_REGION2 will +accept KVM_MEM_USERFAULT as a valid memslot flag. + +See KVM_SET_USER_MEMORY_REGION2 for more details. + 8. Other capabilities. ======================