From patchwork Thu Jan 9 20:49:17 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Houghton X-Patchwork-Id: 13933216 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 5CBE8E77197 for ; Thu, 9 Jan 2025 20:53: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=4wiJ4r6U+4DKwl7a0Sm0LZeZPdZmNUbmXckiwF/fu60=; b=AwYTyufSEXmPy5bv+HXoOW2Erd fGW30ZcCVCHFxTWJpRAuVJA2dQcFm28jy+aq+HSO8NYBf8AEkZKGc3c4lFvtuIRgC+qTdRM46DqIx MWIBkJDoo8RBKaZ5SnOYPtQU00JrRSZPrkzPzZ55Z5P9VuZgh0sD2WDAUZ34ZpMnRgF27fNWPiNIi e/zYQlydGjsUj/DMMxx1T5LFrdPV3kI2TSdwsy0XcYXlT4cmV1948QBtyzekTwXHyvL0QSyshDFMU O47MU+U8jIostYBFa+E8LGs+G+HP5YrxaRln8UeVz+7VKHsv3eP5Eloe66W0T9Hl+XC4pUS0sBMn1 KBWr+HOg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tVzWy-0000000DHpo-1jpm; Thu, 09 Jan 2025 20:53: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 1tVzTZ-0000000DGUv-48PD for linux-arm-kernel@lists.infradead.org; Thu, 09 Jan 2025 20:49:59 +0000 Received: by mail-qk1-x749.google.com with SMTP id af79cd13be357-7b864496708so312522185a.2 for ; Thu, 09 Jan 2025 12:49:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736455796; x=1737060596; 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=4wiJ4r6U+4DKwl7a0Sm0LZeZPdZmNUbmXckiwF/fu60=; b=Fo2wADit8FqpFy46QbTgE0lqXNJS9pFZ2QDGqGx3l39ALPd1cNWIqfPSbiwWDa2bZM M3xK5e7gZk9f9zTfg7L5lpkB2+0a8+djKYzBHsyxT/9CL6Y6WEIHYasj1n3cDtAxHrCj vByx6sWS6+ENGJXqAc4gb6fopoXGCG7mQsAg/eZF0fbCj4a4mF02qpmpoS8F9P/vdU5i FzDBdFnzSFur24Q7IP9+/zgaZrlTx1iuu8G7/DqcfuKztB+dTxd9gOCc2lEVzmiVmbPy BbYJ0GMilNDKgc12tuRYe15KFrTopKROb0KKmKQtzxmoYBVohC0bd13fPdv/BSOi8uX4 i1lw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736455796; x=1737060596; 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=4wiJ4r6U+4DKwl7a0Sm0LZeZPdZmNUbmXckiwF/fu60=; b=PZ9Ez3z9W5ckRJkWP/1CBbfZPP8c26pfPfIs/T/F36mDVqnunhW8QyRWA4FfoG3o0r yw7aup4+oZT9Qteule9Zj/nO6JgFGbqc3QtD1lV8A1oyaV33GVRy6WEwv0pKK/4OnXsF xc2slek62EBTwel2sL2EVj/mBPKsz274knx7PTsnSclDdCcfrLN8AMb+dmpCwEWXuUNm +58lKTsQeTgzn536ENt2qNbZritJWxZ8aPMO/8K9pTRXn1VP0pANbdQjkiLlXnW0P+RZ VMDzVbUlXLs1s7A3rJZ4WpAkjHPmmBoGoLaf7CvT7te2Lsm/VXqfEkdVSfkAM7zUj63d qlRw== X-Forwarded-Encrypted: i=1; AJvYcCW25bi6Eo+fTv/6iC7sb6s44PDR3Pw5Cqtz3aGRtzvn2Ap0oAEb/vrwn540fLyJZ30R8bqUsMPZOrYgLKYFI0Iq@lists.infradead.org X-Gm-Message-State: AOJu0YyErV/Qrdai8o1c86k39ko7CotB27pRgxSvw8Q+uBhA84cD0X1z TS1ttnWf1qh4PEbpZMCTx5lXupzkZPcLLS85Mdh5xWgGAESwiuxJHJ9cCvfJD2QCY+kQULMLIHB 6WiRap6SLB1BbDzRA+w== X-Google-Smtp-Source: AGHT+IG26IPp3qbl2E13g/CbVexbYg/eWO5E84o5Ua+axOZx93ciuXt5lAWy60cQgF+PsXPiSlHQOVHo5aFaWyuK X-Received: from qkkl1.prod.google.com ([2002:a37:f501:0:b0:7b6:e209:1c29]) (user=jthoughton job=prod-delivery.src-stubby-dispatcher) by 2002:a05:620a:d87:b0:7b6:d632:37cf with SMTP id af79cd13be357-7bcd9729affmr1146945885a.3.1736455796215; Thu, 09 Jan 2025 12:49:56 -0800 (PST) Date: Thu, 9 Jan 2025 20:49:17 +0000 In-Reply-To: <20250109204929.1106563-1-jthoughton@google.com> Mime-Version: 1.0 References: <20250109204929.1106563-1-jthoughton@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20250109204929.1106563-2-jthoughton@google.com> Subject: [PATCH v2 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 , wei.w.wang@intel.com, 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-20250109_124958_023038_8CBC9F74 X-CRM114-Status: GOOD ( 19.42 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org 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 | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 55 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 343de0a51797..7ade5169d373 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..4bceae6a6401 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; @@ -1974,6 +1977,12 @@ int __kvm_set_memory_region(struct kvm *kvm, return -EINVAL; if ((mem->memory_size >> PAGE_SHIFT) > KVM_MEM_MAX_NR_PAGES) return -EINVAL; + if (mem->flags & KVM_MEM_USERFAULT && + ((mem->userfault_bitmap != untagged_addr(mem->userfault_bitmap)) || + !access_ok((void __user *)(unsigned long)mem->userfault_bitmap, + DIV_ROUND_UP(mem->memory_size >> PAGE_SHIFT, BITS_PER_LONG) + * sizeof(long)))) + return -EINVAL; slots = __kvm_memslots(kvm, as_id); @@ -2042,6 +2051,9 @@ int __kvm_set_memory_region(struct kvm *kvm, if (r) goto out; } + if (mem->flags & KVM_MEM_USERFAULT) + new->userfault_bitmap = + (unsigned long __user *)(unsigned long)mem->userfault_bitmap; r = kvm_set_memslot(kvm, old, new, change); if (r) @@ -6426,3 +6438,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 Thu Jan 9 20:49:18 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Houghton X-Patchwork-Id: 13933231 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 367F8E77199 for ; Thu, 9 Jan 2025 20:54: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=inJmWA70Ac+hZHw9fDPSAlORU93rduBYUNSCVpvyraE=; b=28of0m6DJmTt3gD72T0F1X5uN6 WNBArSpJ7osyjk6wv4Ocel5PFKcg7/dft6NeDthwcr1TSREPoK3EvudNpZx32ZZ0jaELuruaytlpx w/tEPkaAQT1LKQymHMIHizp4syb/LT4HfdSEFu0do9tHUWUN+pcBo2KTT7YrVmxzs0dlzUIuQ5OWC r6OGbcQPpm6B9Y+QemxVZYpNzIHWV4qKlxBq7I6ZlpINZ+lsf67aNwvtjaSVeWynjFq3kTTClhUK2 dGmAbZ9aX386ML9YHgYtbzTJjW8A2SqzBshnsG36IoNXMF+DgLem9LOtptSk+EuQ1+NyJ/jJLlXxQ XoDtbOOQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tVzYA-0000000DI9M-0Fo9; Thu, 09 Jan 2025 20:54:42 +0000 Received: from mail-vs1-xe49.google.com ([2607:f8b0:4864:20::e49]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tVzTa-0000000DGVC-48xS for linux-arm-kernel@lists.infradead.org; Thu, 09 Jan 2025 20:50:00 +0000 Received: by mail-vs1-xe49.google.com with SMTP id ada2fe7eead31-4b126bb6f2dso201438137.2 for ; Thu, 09 Jan 2025 12:49:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736455797; x=1737060597; 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=inJmWA70Ac+hZHw9fDPSAlORU93rduBYUNSCVpvyraE=; b=hurw4OSp2Hvu6F/SfVbpzw/dLbGxwnFDo5gaiIfj1gYpS3+bPOdJP8FspVmesTMsuB xZHCoBDuSZs8QikiuDhrISnU+tI38qyxYk2UaHTZ2z1HLUELff9KRSzhHtUNVmt4r25K VV/ZnjaUBuJrEMMipfzvFE4YckR0pHK6047x7YijmcJvPZ4CRDlRBds1PjqBNP1fO5y5 4ddm2lHjDqo4iD9davXq9PpGMedyK6pUEmRJCtpSpR3BCvaQ1Za57jLBW93rtCbKvc4p irgdHZlS3bwGjxjTD6K5sAOxdS6Jt22/W0dKR91ARXSwjl2fDGuwWlO3BWz48Eh6rOgg 3ivQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736455797; x=1737060597; 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=inJmWA70Ac+hZHw9fDPSAlORU93rduBYUNSCVpvyraE=; b=uUMifp53UK6RSvcq30QrG0CXCp0AKP8CnUvJltCTQEJYKPnDLrWQc1vdrEf6FsYLVZ zTUyI5pEMiK1iYqmmn0hJ6Wg1LfMDW1jfSddQQLyxslbSG01SCVCJxUQAUscsFpGS7/O KEqbjHRH4b2kwqDIZ92e3K7HWPSJmbnMOVT9dVXs9Nc4ZsupZIKOHOancJfmtgGMuqDL 7umaRKgaojsQWHq5Tyup6NDzgmXEDpZg4PcFhoc1QazPhzuz4Tb38ZG7a5rhioSgiVcv di/jXHUQ+D1z/+G9ApiUklOCIIWdhp3UXngbnVh3sC5tTpxQuviLk31nuN2tkG93ek0t DqZg== X-Forwarded-Encrypted: i=1; AJvYcCVHSniLo2ypLU8mDz9Habxr7V3sSqhTxqqO8tv09lmoixM5h0nKCKgB5cmjMWckIyndPTDzFLRwi9MYYFCM2uoH@lists.infradead.org X-Gm-Message-State: AOJu0YwVhGCRtSNyRPN4tY3WtlbzmoaZM4DqQXJdj7lgLQO0jUr0YMth tYcpdJIVMp5+cSF+4DQCAzzL1iSFF5fvQEkSlas6hmcSYbdz/MpiQiFFF59IS1cT7Q2xYhjNoH3 1jaUtK/pLZyKZDD/zsg== X-Google-Smtp-Source: AGHT+IGmWC0I9uhGcwdr2Gd8ZWKOxj9FqNl2pt1I3pHNzlZaCTTUBXu0xZZz+qkK1VqDCdKw56Aw4mYDSrKY0XUr X-Received: from vsbka9.prod.google.com ([2002:a05:6102:8009:b0:4b4:eb7c:c900]) (user=jthoughton job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6102:38cd:b0:4b2:adca:c13a with SMTP id ada2fe7eead31-4b3d0f05fbamr8311073137.12.1736455797065; Thu, 09 Jan 2025 12:49:57 -0800 (PST) Date: Thu, 9 Jan 2025 20:49:18 +0000 In-Reply-To: <20250109204929.1106563-1-jthoughton@google.com> Mime-Version: 1.0 References: <20250109204929.1106563-1-jthoughton@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20250109204929.1106563-3-jthoughton@google.com> Subject: [PATCH v2 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 , wei.w.wang@intel.com, 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-20250109_124959_034683_F71C3E29 X-CRM114-Status: GOOD ( 10.01 ) 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 7ade5169d373..c302edf1c984 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 Thu Jan 9 20:49:19 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Houghton X-Patchwork-Id: 13933232 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 D9C31E77199 for ; Thu, 9 Jan 2025 20:56:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To:From: Subject:Message-ID:References:Mime-Version:In-Reply-To:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=sy46ZKdMOYPNfFozinwrK5U4YnAl6mgCHSprJv2I5EE=; b=kSrCi4rNQLqDQIFNXaYSsHk5aZ mdh2svuVj04DmJOCNXnnuPgZ3YFJvT+hq+rH7UjxSiZgxoQLtKKbiVrFCQjmUlBilV1h7K4Rtv/sW FcBKz1q7cgxC9TFKPsDffQs3GG0DOqbEV2kqfMEn8hFLFcTrTsqPLIW0E2kiKngKCndZOW4YMCPDI sRrSt8OuejSFyks1r07cPVnjGFhc44VqjcPf2eWSJwyqhAspbxz0AMzvsZmm6d8LRxXEu31Nkp/tU q0rBjWB9a9zqaPCH38+nXX1DsDix5euNh2cPYBu3p0ShjXGStdzUwj+KEmBKBDGT4oTgfYqwJ0uhY 17DnVYzQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tVzZL-0000000DISN-2oAS; Thu, 09 Jan 2025 20:55:55 +0000 Received: from mail-ua1-x949.google.com ([2607:f8b0:4864:20::949]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tVzTb-0000000DGVh-09jz for linux-arm-kernel@lists.infradead.org; Thu, 09 Jan 2025 20:50:00 +0000 Received: by mail-ua1-x949.google.com with SMTP id a1e0cc1a2514c-85ba1d9dcf8so359751241.3 for ; Thu, 09 Jan 2025 12:49:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736455798; x=1737060598; 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=sy46ZKdMOYPNfFozinwrK5U4YnAl6mgCHSprJv2I5EE=; b=RY0h/ymjiFbLpXMnFxucUPqUIH18EPhigGzOXN7JyeFJG/Ysiy8jjOUp+BWxkVVHLu oEOE+WgR1XvzTZT1gHz7TOZxhJofTtSb345JaaBfCgQO2bf8PV/6bBD98Jv7honpzr18 F3XUcELWLlLw5xCtxa+y9AAjxcjxs81MF/Z3HgCoVdEFInsjFVgSi3WzINiR1DMnF0jC 1/mTUeYljT+A2vkDQrqs0TFDkOLfgNVlhLPiaH/ktSFFlDj+5csTALSGwMURk/0knWV9 orYinLMVPSNLrheiLgNybgsOfq/5kCM+3sYi3ICZKwyASwijX/XXAdzR6wB0UdlhL+Lb iiSw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736455798; x=1737060598; 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=sy46ZKdMOYPNfFozinwrK5U4YnAl6mgCHSprJv2I5EE=; b=TQT8HVGLZtWQjc6Qw1zCJOTqfz5Jy5efWo2vudVtxj1pg2Da1bNI3Pv0IHhBITM/ER xRvhrjGqQOatztcFMIt+XisMTTZ6ESqY1ylHa+IrqdLTaI5Qab1KrAvCo+iTlDXLZ56R +loch0nF9amkW5RcfyexkSfLuEE+91q4BNIDdDSAfSQz+ECXFeGK/rKr6ICReug69UQT ER3/2F7i7lFZl1I+NlZKX4tGRTUGef6y3XGLcrJ8wcAmlpjoKTTxc3/K7AsYqsm8lKQH vVM0Et4giBpQHriM4AQsWBpMRfd/1Aqo5Ln+8w6I5YrGGJoRETsVVoQ+9OcvrDn17ohS SEyg== X-Forwarded-Encrypted: i=1; AJvYcCWDAiFHF1hhXnZ+1UEvVUoEB6IeBffHsLoKdBROyD1tyheIqi/oXHkGtwPk+d8nDkGMyhMI4kRNonJrA427ZNEQ@lists.infradead.org X-Gm-Message-State: AOJu0YwHjI81D3XwBz3EISyMkW4ufe2WUQyghOBLIAUU3VOm1zdtUXBr TDe2KRGvwwCNXsg4pxySS+JzS143mM9LpYjzUMTHl5Lp4Il6c1vCwVCzVdaDSRs7UHq9fDOWThq P45fDpl3C0H3aq8TGoQ== X-Google-Smtp-Source: AGHT+IGxjoRseI0ptp1PpNGkamYSZpMVzGlMOrPqkiBhNVZ0uPSquo0Jh6/07xFgWyNiCB/pmTP7I9swsH2tkGpZ X-Received: from vsvj16.prod.google.com ([2002:a05:6102:3e10:b0:4b4:f067:c6f]) (user=jthoughton job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6102:c12:b0:4b1:1a11:fe3 with SMTP id ada2fe7eead31-4b3d0f15e35mr7763898137.8.1736455797824; Thu, 09 Jan 2025 12:49:57 -0800 (PST) Date: Thu, 9 Jan 2025 20:49:19 +0000 In-Reply-To: <20250109204929.1106563-1-jthoughton@google.com> Mime-Version: 1.0 References: <20250109204929.1106563-1-jthoughton@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20250109204929.1106563-4-jthoughton@google.com> Subject: [PATCH v2 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 , wei.w.wang@intel.com, 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-20250109_124959_096387_9A747092 X-CRM114-Status: GOOD ( 14.96 ) 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 4bceae6a6401..882c1f7b4aa8 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -2015,9 +2015,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)) @@ -2031,6 +2028,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; @@ -2046,7 +2053,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 Thu Jan 9 20:49:20 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Houghton X-Patchwork-Id: 13933233 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 171C4E77197 for ; Thu, 9 Jan 2025 20:57:22 +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=1DrcQjgptnhIcToWnmFksBp0MRaAX2CW6X5qNo+eD6A=; b=mCGWKCKXtOBDmiVgJQM5l4CMmk gd8HnnskeQ5xyQlrSXEFDWSuIkhz3luyPJuyxy2BfelpEDugphd0qpBAxNKpsw+niQ2ePpm4z51Yo IpEna+M0T0mYot89LdMY2H4igceOAn1sKRsxu6mHVEkOis+zBP9CFz+YOuzL11BK7McAqZuXdmAIh 7S+PQMeAcHz4CXdudh6GgyyfGwQxC9/RmkcoOkUkEURMsxqz4k4qq7rh7pgFh9ZnZTkHZw58Z+u4k J+f++O5xqI/S4IeNSOh1HwfQFvNU6VvfsFyLk5Y0RkS5TI4C/IgpACjVolyvTvatLzHOsZ6u5VJtI 2u9XBiPQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tVzaX-0000000DIZ1-1RQp; Thu, 09 Jan 2025 20:57:09 +0000 Received: from mail-ua1-x94a.google.com ([2607:f8b0:4864:20::94a]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tVzTc-0000000DGVt-0SVA for linux-arm-kernel@lists.infradead.org; Thu, 09 Jan 2025 20:50:01 +0000 Received: by mail-ua1-x94a.google.com with SMTP id a1e0cc1a2514c-85ba1d9dcf8so359752241.3 for ; Thu, 09 Jan 2025 12:49:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736455798; x=1737060598; 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=1DrcQjgptnhIcToWnmFksBp0MRaAX2CW6X5qNo+eD6A=; b=rBv/bhBkU9zPSo0JBcfODO6q3wjfDEp3D6+/VvglDeMCa6Bu3dNi/dAOQjUPRMYUqA sogYREeECFhDe4RNDcOodjc4FpPKA5jkvsqMxcnmNOpDemo9DHGzn1kTD4n4fxbGGKOc JwImFnWAS/hxyDwZSvcunaHyrHoIbTLAjA//KL4BySjkfrRFgGC9CsxPlyCyNZG/ih7l EtHPBgD+JySO0ETwVdhTwedOrDYLqgVyMInb8GCoIEeddG8NwiOet+UaUCFaqP8ZHUZv JFkLI+BwjG2PxEOX7TC4l3vIDAWFYzQqMa/xPKG4FpyZnlQGj7xYORAt08f+JdO81j4O R19g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736455798; x=1737060598; 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=1DrcQjgptnhIcToWnmFksBp0MRaAX2CW6X5qNo+eD6A=; b=pAsfTg7NkG0TXOsnp8vJV3FxPRJ5JdU6jtKeHT8DqIHIep6ZXXwlO3WEFKf2Xr8CuW R9p2oWwt8O8+jGp2M9oC1XkPf7w7eUzGbsIOluT6r0zoto6UX6sPOnMjdctYKdk85eZ8 jHvXE6ttu3AuFevcPPGpeqvC2xyaM88N+6RQGX9AXBHDaHAzg6ku7c4NXKv+qEzy7UHl 8xz+dLxIi1JNsQM41Xx5dRXw4quAkC2IfUWGr+O3pNmL3mRjRRNjAaqSv/VFGEfgVHj3 UHct58DRS6Vjg4rdVYNhTCfS+ZkWUPIOZvpTrXrQihgDSjY1hDo6UVggVaZCDXauwlwP gGsw== X-Forwarded-Encrypted: i=1; AJvYcCWDiXLPqrZ9wppiXsSE1hdUpc0Vh6MdRx6PO7qTuFJgBnkOwNEsl0vUsMjrA9kFFE0kEwEOspk7VckNCzv+Ze6k@lists.infradead.org X-Gm-Message-State: AOJu0Yx+2CWnGbHcCFIg5Enfi89GNEyQUBAZcMOrh0DJDK8Nsgw7K4rq 71kQA4sz5O9L24UfKucEPey1O1oSc2CftSQi64cie4uXfoUq7T45dViVvEsmGHGrq4JGeLqtAcg WVjRhya9tN5OX5V1/pw== X-Google-Smtp-Source: AGHT+IEK0449o9KbC3fWak3S16jydYyIqzxOV/2eZ44AIR7w7I0DQuvF+HeqMnZYowZ6lm/VqNUOgc0IX+KhZU1E X-Received: from vscv18.prod.google.com ([2002:a05:6102:3312:b0:4af:df7b:f439]) (user=jthoughton job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6102:2c02:b0:4af:d487:45f3 with SMTP id ada2fe7eead31-4b3d0ffc73fmr8314563137.23.1736455798514; Thu, 09 Jan 2025 12:49:58 -0800 (PST) Date: Thu, 9 Jan 2025 20:49:20 +0000 In-Reply-To: <20250109204929.1106563-1-jthoughton@google.com> Mime-Version: 1.0 References: <20250109204929.1106563-1-jthoughton@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20250109204929.1106563-5-jthoughton@google.com> Subject: [PATCH v2 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 , wei.w.wang@intel.com, 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-20250109_125000_144699_4B4F21CE X-CRM114-Status: GOOD ( 10.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 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 c302edf1c984..defcad38d423 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 882c1f7b4aa8..30f09141df64 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -4811,6 +4811,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 Thu Jan 9 20:49:21 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Houghton X-Patchwork-Id: 13933234 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 4930DE77197 for ; Thu, 9 Jan 2025 20:58:32 +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=HQeKkaeKNAINyBb7M+Oe0wr1NoWCGD3LtUC6EAysWvA=; b=BvR0xXIq5donZ83GrOwLOzyKys pML6HkeMFD0grIm2kStJ6Qn+iSVg0rxA5kYKCa/yBNGzsO5qyBTjfeV0XotAz8Osc6KJPA1q5LJlK MXEGv6eOOSdR/7/D1SKMWp0hJcfx41BWSpLImGufe8iBuUl6a5Y30oN+9O3pzMTnVH9h0fy5VdO8u Q6W/nQkYoVQiGmTBHnMNDXh+um+JqzWuLfkc+Hogryeud6dJ/iBYTse9XJIXJnzAoU7LHL/VcBolF ZU3tg6g0I9exWpJC91dcOSnrYN9+gjhSR/UdNHJoDcbR2cVycMeuEkO0aMohO3FQzrVmYPPUOgjGt UUbIc9LA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tVzbi-0000000DIfT-0H3o; Thu, 09 Jan 2025 20:58:22 +0000 Received: from mail-ua1-x94a.google.com ([2607:f8b0:4864:20::94a]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tVzTc-0000000DGWj-1VNt for linux-arm-kernel@lists.infradead.org; Thu, 09 Jan 2025 20:50:01 +0000 Received: by mail-ua1-x94a.google.com with SMTP id a1e0cc1a2514c-8611c7e6c05so215931241.2 for ; Thu, 09 Jan 2025 12:49:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736455799; x=1737060599; 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=HQeKkaeKNAINyBb7M+Oe0wr1NoWCGD3LtUC6EAysWvA=; b=RcJoYbBXCwCeZoxYREcAsNrfl1SUGaNLJyuTKoKSkplXwLZwBL7wENvWp/FdnnWGLy lDd3KP62CwVxRPtZgm2z2TctQa1wSJqSteMAtQYrdn5xbLIUPUuJuD7n2wzE7l1P+MZE GrTrexKC05/ypvTaw5C/ALbAwHIbLmJd77OI6J2ZqCO3IuAhzzrvdj0AcUIn5CXo7vNE 0wsLRbUgn5SXW8A+wFNxo7MzIVvWpLouog6gcIhhQ6alG94FyjovfOdVbPSnCpY3/+8i ALaYvJsh+oMHzCD6GxzrKfgBZXg1Jyt+csBx8dlfnzQnxLO2GXqoKY5FYbqwTO6XNKzJ 2cDg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736455799; x=1737060599; 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=HQeKkaeKNAINyBb7M+Oe0wr1NoWCGD3LtUC6EAysWvA=; b=p0BN3LCTCd5lbT0yViBKE8nJtAoRcCD2ERukmnk9hIqIJ7RqV88Q1OkEXcxUZU/hrA 3QK9o/R3nXiL9ygoh+nZEKixkDU4uZNtqxfxnHt6mdBacv87Aol6yFPnnF5KNI3igFif Xks45/yteSpBna8b5BsoCsjZufNT5udUUo0MvOoecVNVYP6zjVmTXBB/Vatz0KnuReu9 IlKF4vmBlX6076YzWOs2igefQ23a8+dtzaBo31LzEu59Rz+BFEnSTQT/PsJM512I4Vje y717j+F+eN2Ry5g9ooMQOgXo65i8nj7LdUUfIfMw+dvO+F4EUnRwyr3m0c6CyFqKHcvY bl1w== X-Forwarded-Encrypted: i=1; AJvYcCXB+AD0HdByzFlKrSys6OiekkBrIlvr2cGb+s15HSWXpLhMh0a07XwqAxzUY9+htgBG8ABiTM3W0ExFpbNGBjP7@lists.infradead.org X-Gm-Message-State: AOJu0YyivefmmHouOaec1DbY/qlavDPZzaZ1cbwUY/V4xSIpudPUTfyr RDzYq/dLG1/IlDFy3xB+9Kcd38/6fAdk9FDFN/W/NZ11qeqPyPYOeZQrisWpOqNlwqgh8F63XBI iSSaRZkhdp2YxaQxmfQ== X-Google-Smtp-Source: AGHT+IE1KR8Jo0njtEmz2wd6kPBO+ZySo59zWwIng47TZxzkw4plAazjZ+t5031NO6mP2E11kmCWbmDTOkkQK6kQ X-Received: from vsig20.prod.google.com ([2002:a05:6102:9d4:b0:4b2:cc7a:f725]) (user=jthoughton job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6102:509f:b0:4b2:5d10:58f1 with SMTP id ada2fe7eead31-4b3d0f9d056mr8432287137.11.1736455799217; Thu, 09 Jan 2025 12:49:59 -0800 (PST) Date: Thu, 9 Jan 2025 20:49:21 +0000 In-Reply-To: <20250109204929.1106563-1-jthoughton@google.com> Mime-Version: 1.0 References: <20250109204929.1106563-1-jthoughton@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20250109204929.1106563-6-jthoughton@google.com> Subject: [PATCH v2 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 , wei.w.wang@intel.com, 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-20250109_125000_420112_8166377A X-CRM114-Status: GOOD ( 21.81 ) 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 when 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(). This is the behavior when dirty logging is disabled; remain consistent with it. 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 2401606db260..5cab2785b97f 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -4280,14 +4280,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; @@ -4324,8 +4329,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; } @@ -4334,6 +4341,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 1b04092ec76a..2abb425a6514 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 Thu Jan 9 20:49:22 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Houghton X-Patchwork-Id: 13933235 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 1852DE77197 for ; Thu, 9 Jan 2025 20:59:46 +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=Ej4vKCsRcXi3JRFU5yXFClNg+jWCgVgQEDR3f6Axo50=; b=g6oU30YI4v7j4RUQsr53xOKRzl KpRZus6ginuaVBe/Bmd8M01ScH7vm4Zzp2SYy4i2fY0xHQLhcFC6UzxwwebRh38rMI07Ud8JZ32rf dZTsAbO7eJIoXghbQwdhrugb+PtatsNScsOee/AIsltrjezminkEw4tgUwPDpxwoBu5XFz/9eypvG ISGc+LR0Ewvz7CbmVYYWgmXFv/2G8TQU1vePlf1QEUbxumDP3J9QMb2D4U4uKd8ykEG8IUgsViLgb WU5MqjZVnQg4th9qbcuRZDvgX1neGaC/vSS7RR3DaIztYdTUwrhs3MG0K7yADaNV6j3fXsLl4Rwqf nQ/ZRJIg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tVzcs-0000000DImZ-2nce; Thu, 09 Jan 2025 20:59:34 +0000 Received: from mail-qt1-x849.google.com ([2607:f8b0:4864:20::849]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tVzTd-0000000DGXt-2tHG for linux-arm-kernel@lists.infradead.org; Thu, 09 Jan 2025 20:50:02 +0000 Received: by mail-qt1-x849.google.com with SMTP id d75a77b69052e-4675749a982so22458061cf.1 for ; Thu, 09 Jan 2025 12:50:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736455800; x=1737060600; 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=Ej4vKCsRcXi3JRFU5yXFClNg+jWCgVgQEDR3f6Axo50=; b=c2laMfxL8favRXEMQgpXimsq9MAzGWYUo8O7JRIx+HQkggctFlH/EPUexGn8MWCet6 fPZa9pekLidNnRIYv2VNFY4sibnFLEMcOOxRzp0HLa8+SsbNqXxqeuG8YIZNOOkNQKff ghaIj1YvISJVqGqZF7vfjR3Gt0y2cPFTDAE3CxpQ4XG4ktzK1pDhQ339yO7AwJtQjH3o c0N5suSXs1N0Az639aWaB4Qs5uWJggmpXz+Q4GxX+hZgRzYbpL7+3ND4YKFQwJZKPJtn AyShcN3sPuDZmoL/2OPeRuzwnFoK3ZVe9MDKq//RaSTjXJaHr11vbeDqlqo7ONPDoj09 a0Sw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736455800; x=1737060600; 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=Ej4vKCsRcXi3JRFU5yXFClNg+jWCgVgQEDR3f6Axo50=; b=fFvEwjo1PHTrlBBClBfAio8z4iEFzKlrLoYn0Jcf7m3djMUbxlErTysigt8vhypdkG 929VpZbAn7HR5rOsh9JiZ98/hujPWts2W7aMqW/bD7gqXYZ1UMfb3KM8ji4NXN4d+/8o 6XnaXua9Wo7k3bydNfb+RQljy8rEatrZw+tGJItVc3l3rKlrsth5G5tegLX6GFLOM+X+ MNodGLBfIS2seS8Nl/jmttNqWtBCAj47jh2388I/r+28vmc8en7ZPUN75V2u1LE795WA /cORtZ983sZ5Gp3TAE6JbFbRxoLUWWr10yz8fmhGw2SDLrPoTDGwVV5brVLuHIYksqHg ayCA== X-Forwarded-Encrypted: i=1; AJvYcCXFvOhT6eSg5L39yhjiyVtHKenxeLgIvE/VFfNb7M0UQxJAsbdvG9aU14TE3ilH+fjJrmT0v1qsRcg9tIVWKbjM@lists.infradead.org X-Gm-Message-State: AOJu0Yz6+fwjROv8LKpVCCSS5ZpxESWayvTdwAwm2spSbVFp2Yy3SnQv ur4jzK2yTi2UbfJNsd8W34YjCCkS0NnPKV6c/Un2ZhIIJuX4IhgMlYNot85PxKj+1t8lhOrdzAw cRkZ0eewjohDECD75Qg== X-Google-Smtp-Source: AGHT+IEjWh1ifMEjbhq2t/4s1LokEzVaJxOMILKR5p4NmJRJSQEQbDzHfJF1YycGc5qCqnm1VGVZZj4bYy7TorUf X-Received: from qtbiw5.prod.google.com ([2002:a05:622a:6f85:b0:466:928b:3b7c]) (user=jthoughton job=prod-delivery.src-stubby-dispatcher) by 2002:a05:622a:14e:b0:467:5926:fcf2 with SMTP id d75a77b69052e-46c7107e0b4mr103936581cf.9.1736455800057; Thu, 09 Jan 2025 12:50:00 -0800 (PST) Date: Thu, 9 Jan 2025 20:49:22 +0000 In-Reply-To: <20250109204929.1106563-1-jthoughton@google.com> Mime-Version: 1.0 References: <20250109204929.1106563-1-jthoughton@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20250109204929.1106563-7-jthoughton@google.com> Subject: [PATCH v2 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 , wei.w.wang@intel.com, 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-20250109_125001_736194_DA7A6A57 X-CRM114-Status: GOOD ( 17.16 ) 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 on, zap the second stage with kvm_arch_flush_shadow_memslot(). This is to respect userfault-ness. 2. When KVM_MEM_USERFAULT is enabled, restrict new second-stage mappings to be PAGE_SIZE, just like when dirty logging is enabled. Do not zap the second stage when KVM_MEM_USERFAULT is disabled to remain consistent with the behavior when dirty logging is disabled. Signed-off-by: James Houghton --- arch/arm64/kvm/Kconfig | 1 + arch/arm64/kvm/mmu.c | 26 +++++++++++++++++++++++++- 2 files changed, 26 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 c9d46ad57e52..e099bdcfac42 100644 --- a/arch/arm64/kvm/mmu.c +++ b/arch/arm64/kvm/mmu.c @@ -1493,7 +1493,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 { @@ -1582,6 +1582,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) { @@ -2073,6 +2080,23 @@ 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 new_flags = new ? new->flags : 0; + u32 changed_flags = (new_flags) ^ (old ? old->flags : 0); + + /* + * If KVM_MEM_USERFAULT has been enabled, drop all the stage-2 mappings + * so that we can respect userfault-ness. + */ + if ((changed_flags & KVM_MEM_USERFAULT) && + (new_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 Thu Jan 9 20:49:23 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Houghton X-Patchwork-Id: 13933236 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 96CBCE77197 for ; Thu, 9 Jan 2025 21:01: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=9jDqHEv7geetsXAPhiE7vHc+NbQmOip57zkXZiw1WEE=; b=pgFlvJd41BI/8tHt0dsAb0qe31 tV/qVJmH5J9kayRlZu9g38FtDCNA6ITofTytMNFV/4vHhbgMcWpjRAd7p4tjyFDMFIsRrrEh5Fjgm yKhXKr5giF8RQnzLM3HTgXhI/GXG5UDjZtITX3i/uAOCzeyxgsfOEuScm4kk+kKV8QMJsKZz72ZMs k/kdCXin2WzN8B4CVb6qtctDPdBjuP658H6wGJqrR17JgYhfOrekuMaZP6eXODhq+VnhIdD6yatHy n/qX0TmPqDJ6vjFXPCaCFLaHAZ1vqkORzM61SkozPAggZTaF9lpPumR4Niy4XKjl8OrbdCoWxiPMH Y76rC1dg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tVze4-0000000DIyp-1GVZ; Thu, 09 Jan 2025 21:00:48 +0000 Received: from mail-vs1-xe49.google.com ([2607:f8b0:4864:20::e49]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tVzTe-0000000DGYa-2YhV for linux-arm-kernel@lists.infradead.org; Thu, 09 Jan 2025 20:50:03 +0000 Received: by mail-vs1-xe49.google.com with SMTP id ada2fe7eead31-4afd9b88ac2so195545137.0 for ; Thu, 09 Jan 2025 12:50:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736455801; x=1737060601; 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=9jDqHEv7geetsXAPhiE7vHc+NbQmOip57zkXZiw1WEE=; b=ypmyAaQmBF1tcM940kT7VDZvMg3ooQFkjpBztUsjyJwk32AdJ2FvXujjaez56tkYB7 teJS9fytBdnB2+22n8YW1WIgK0K/BFCoNDzq22m3Uc7y+/I/l9xma0kNVeAoZXzQgwEo evhNjLssVpS2Ep5bV1RFCIyrO07qOo6EF1nfp77gM+G35JUKzRtPDVH1b3P5+yjz+HAl bZR5SFFN842kuD4g+1iOqKGg0ym3Xr0+bq8uot3qELl8SBc8TGzEXAe+tdTijEZ5Y/N1 nS+oLRmTKdnMQajMLROQwST60PB0KA9Xj73QUC24lBle8PWvtIIvTpMEkd3XWl8OlxCd rZZg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736455801; x=1737060601; 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=9jDqHEv7geetsXAPhiE7vHc+NbQmOip57zkXZiw1WEE=; b=UcTJ/3/SJka+g5Gl2Uhi77tntGrDkjd9KuvDCexX+/eHRv3CvIv/GZisFZwYb3lhb2 dA1SpQoLc8lz0NdYxJqB/Veu3NotQScu73seulz/7qxztxq+myyVwjgV1Ewfc9MvGrZe bMGtjn3yvtYhTnuE2uEPyXjwlOvLbZCgXK0iPSXiqnM30QGTtGzgmDfazJJjR0OJSV8D rxn+EmalcHmxwO0ZHJk7yyXXElB/NUsckY3nCjDMDjC4yUmToIdQh/oCk4u5WkxyHJQW a+R8DFvXrx0A7c17/3T1o4RcmFNp8MY6dEezWmuUHhZ7n/18IoUcqZ4uqmzJwj9Gsj5z qd3Q== X-Forwarded-Encrypted: i=1; AJvYcCXhLy3Y/PlGJeTtSqRb0/XNCkf7IDV5lfAl8vf02NpyLJHQ+N31PG9PMNA99lpWooH4BYVNrwG3Cweh5v1wzzuR@lists.infradead.org X-Gm-Message-State: AOJu0Yz7FyVK1r/vLCdf5dc3PUEWebxPTC0OQwEV+mkv+nX+t+N1q1Aa DiT2NqiixNn2xzfByn1VM9NKE+LtUiG5cPT/hJtYepXfcD6/ogZqv4vYZ6u4VlLR6G7ZIcqFmCb IbcelnWEcBX1p6Kx76Q== X-Google-Smtp-Source: AGHT+IFNTRpr21NUpgVvBV6RDqL9+2vcXAZWltadh1brmBLANlrw+WVMeHKDcwijSskNQ4NVBvhEHS4SPdBJrOsp X-Received: from vsbih13.prod.google.com ([2002:a05:6102:2d0d:b0:4b6:1ac7:43de]) (user=jthoughton job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6102:3c8e:b0:4b1:1a9d:ecbc with SMTP id ada2fe7eead31-4b3d0fb68c2mr7939857137.20.1736455801194; Thu, 09 Jan 2025 12:50:01 -0800 (PST) Date: Thu, 9 Jan 2025 20:49:23 +0000 In-Reply-To: <20250109204929.1106563-1-jthoughton@google.com> Mime-Version: 1.0 References: <20250109204929.1106563-1-jthoughton@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20250109204929.1106563-8-jthoughton@google.com> Subject: [PATCH v2 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 , wei.w.wang@intel.com, 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-20250109_125002_644212_C8DAAD48 X-CRM114-Status: GOOD ( 10.67 ) 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 33fefeb3ca44..a87988a162f1 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 Thu Jan 9 20:49:24 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Houghton X-Patchwork-Id: 13933237 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 EC52CE77197 for ; Thu, 9 Jan 2025 21:02:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To:From: Subject:Message-ID:References:Mime-Version:In-Reply-To:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=O767jE59AeAMsTglDWemPeR1CkI5EMgHAt7K2sI2JAg=; b=Y3LxgdCcFiEajXdU2ex1roxs8P 6Lmx334XQR+XDqVR/IxUUALE/F2/oi38JkxNC16tN84+zrx+KBfwMoTEqeZDSjUksZAPsUwVoj1U/ zXrrm/xp6C6qhVN30MWvhx3yZD9RWn6DAU81MtBBEoJ1E/5eIwfznjNqrvq+OcN8DnhdVD+Hkk/wC Ti47MYn2hk53FVPOb+k8t7xI3zNT0hDdhT60NIqt9idsAGvO6+e9T99BZZAaeC61LamCI8p+k/DXm BKeLbBLIjKgN2A6Qx5he+pzbJSnB94rOxs/i7VvdDEAsewQvqGUvPESl8RXMCqXmYPp5qgi8Dferq DBc0rDuA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tVzfF-0000000DJCK-46ul; Thu, 09 Jan 2025 21:02:01 +0000 Received: from mail-qv1-xf4a.google.com ([2607:f8b0:4864:20::f4a]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tVzTf-0000000DGZB-2pMr for linux-arm-kernel@lists.infradead.org; Thu, 09 Jan 2025 20:50:05 +0000 Received: by mail-qv1-xf4a.google.com with SMTP id 6a1803df08f44-6d8edc021f9so20742826d6.3 for ; Thu, 09 Jan 2025 12:50:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736455802; x=1737060602; 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=O767jE59AeAMsTglDWemPeR1CkI5EMgHAt7K2sI2JAg=; b=m8dxgQ0tWtGpne8PxZmu9xKQzM/gw9ThIzrS59xnSzR28tfiaK4bjDHEnGBozpYAYJ Rj9zqX7BK0YG0nGaT+AgmH4tYQsglwCX+mbPQ4GZo0NGgjYXfueTaxPjSOmWiqiaDwXw F52Qr3rHM/3+2ivDM0Y99mg89+t4IO3IsJe9Z79j+4ZFwLrpgp1AqwVHwcWAan6PgOKw yhB6FHhhpZ3WAQsWv5DbjqNkC7svs6iAktoTnkbk9FCRufY+8oj4c+SmpWQeNGC8H6vS Deo4pPZAV9LSxQNrPj9ZOYucPUKtn6XjfijPYSZZnJ36pUOroijXXjPOnXGp4K25S0Ep gl1Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736455802; x=1737060602; 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=O767jE59AeAMsTglDWemPeR1CkI5EMgHAt7K2sI2JAg=; b=nWpk9M+xHWLrRhSO8VRDhqlaaNz14uNJ9SyDATWOmxXZSgdKYDEP+C+brSCGZpqACu P75XHeavaISPW3LKk6v85NKWCcA/XSoBqW7v81JJD8/vj5A/I3KZQoLP20hK17y6mkT+ ZOoclK3mKug/4IF+e95pNpi7m99LhnmocLzGmHovqJVS+fdwuGsJK/lKxIfVdxGwwoB/ 9Ap5SjD1UGR9H5k9LoiOLIeplOJcd9GwPAXzXJ1ZkbUdGqLSO5IxZN6nX1E3hticI0+6 BtBr5+qdMW0F03QLrgAbElKLUHz96KgWJaWFBqNPc+k410lPhHV+gkQuzytw7PPXVxON KvOQ== X-Forwarded-Encrypted: i=1; AJvYcCUHljnq+aOaXBhSF+hONptYHueEoT3MfbwoNOMe09jqemMHXPuynxg7HP4X4/EAd5CctRkmomkQMJGqbiOVFyFf@lists.infradead.org X-Gm-Message-State: AOJu0Yzay1optbadnoFGBTJsxq3y1+wxQXnEtPQkig/b0izVHN8QpHuA SRQNN5CCMW2UWT7YjZ9Y+qbi1zG+XxxNlO7GhodN7YW/0DfgkxLsxzJt0CoRBhvPXul+ccbyA8w I9j4W35cQ7Q4oMzv6XA== X-Google-Smtp-Source: AGHT+IFgx6gAPenNPcGigBK2pqFto4Hhpu9WmCDI55SJzVOesvWzc1cj0nZb4kbFOEWQTb6nHd4wPYt7OTvCG9XJ X-Received: from qvboq1.prod.google.com ([2002:a05:6214:4601:b0:6d8:f326:1f33]) (user=jthoughton job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6214:2688:b0:6d4:1c9d:4f47 with SMTP id 6a1803df08f44-6df9b238643mr125150226d6.13.1736455802071; Thu, 09 Jan 2025 12:50:02 -0800 (PST) Date: Thu, 9 Jan 2025 20:49:24 +0000 In-Reply-To: <20250109204929.1106563-1-jthoughton@google.com> Mime-Version: 1.0 References: <20250109204929.1106563-1-jthoughton@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20250109204929.1106563-9-jthoughton@google.com> Subject: [PATCH v2 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 , wei.w.wang@intel.com, 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-20250109_125003_721945_3B8CB4F3 X-CRM114-Status: GOOD ( 14.04 ) 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 Thu Jan 9 20:49:25 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Houghton X-Patchwork-Id: 13933238 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 B37C0E77197 for ; Thu, 9 Jan 2025 21:03:24 +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=OvbYHGNl+8d7DJVwo06D1EaOJgLS4cwG8AKsUnFt2VU=; b=dPPzHRw2/ZfeV+pcys78TCjI5q 3X9Ez0rPW6VM1g3G9vsDir3GY9w4DGHK0T0lY+MxHs0ynx0EULCVmbZr9Er+6y+Lg6wL5ixe336Gk IDZjw6XtLXUyN9ffccDHFEBiYbAwqEzkd77eiS57Cjp4IEe7w7J1HY0QP33rZ8bFziLXBQX0Z9mDS fbstM3Bkn9k9JZj0sgWp9NIpzoXxULY19SKF80mPkDRHfaREUmgMC8cTu4oFTYcB8lz2eXaTD+KOq B/MZOvtZFxvBdEdvcz0V9cQJJ3iTeIapr6p3OAqnWpbdt7lsaroJnbHJsaCqs6q+Ap6c+cqxYYVPI Y5U1u3jA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tVzgQ-0000000DJLo-36R6; Thu, 09 Jan 2025 21:03:14 +0000 Received: from mail-ua1-x949.google.com ([2607:f8b0:4864:20::949]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tVzTf-0000000DGZW-2xLT for linux-arm-kernel@lists.infradead.org; Thu, 09 Jan 2025 20:50:06 +0000 Received: by mail-ua1-x949.google.com with SMTP id a1e0cc1a2514c-861169e1057so201534241.0 for ; Thu, 09 Jan 2025 12:50:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736455803; x=1737060603; 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=OvbYHGNl+8d7DJVwo06D1EaOJgLS4cwG8AKsUnFt2VU=; b=U0/IRU+UHOSSrWTVjWA2O/zcCIu21eJC63Fum6KdthsvydFFp1Jf054T1F/QBIlACB WC4CEsWRoVGUrhxG5617OICIoR2v14Tw4VRzHqAzBoWl9fOhTQOYxwlr11i97R+2sodf hNZRi43eKf29uUDEdbYv3AtfvHxasHY37ynQqhUWTmIYZaCVUbA5WguUWqVE8uT1PtZH 2x0c4t5wgOBBnKlzW1jZIl59f79Tv7/sI8Zs4qHvhiix5+DRuahHpQI3IM0Bhxre1e7Y ffHSq01ZbhUgGdLthiLQ/6d+eWksfaiz8oeIwmEXWOSfy+sNPXLSSpiUVx05ZwEpi1E2 RoHA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736455803; x=1737060603; 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=OvbYHGNl+8d7DJVwo06D1EaOJgLS4cwG8AKsUnFt2VU=; b=M1GDbNvfgIYnJ+hSCYpjlCPtsYTzePXnmqPbc1LaA8XFfP4I2S5cbgr/sFrvyCQ/xi 06UDso5vQbQpb27RZN88lIq3hNTUIbk5UVrnwxnrUGvAx/bCusxBGVhLMOYbUFGdPUps DLEJ+v0N6x+tKPlOgruQcEPIPsUVr9+EDCS+bCTWvquOB5VkKoY15ESxP3AYiZDfw9Xj YBqF/unkbz/EVV+bNVtLN959V45jmkfUaLmp4cxU9Bw3FZuHtnknUGGKxsvxsrrjmCo4 cgVJJ2luubbZoKW8snaHZfjjM7npOcmJGnyEW21Oc4F5k0x0Q4KDm4G1Y43PT/9BRK61 R8EA== X-Forwarded-Encrypted: i=1; AJvYcCWEJTtthkDoUYFhtKh+Tu21cYGSM5SFLlmYP3W1c7Vj+NaOpTPyRLauPXtHt2LcTqXnNt4yCdcE8c+Cqph7aqHr@lists.infradead.org X-Gm-Message-State: AOJu0YzW2QFwMzOjXHzwYpB98IsRIeHeCKCYfgyk4K/nqUuW0bGA4OHX tw9bdOFdr9xUTtt+ZOxJNbAvULeN0kDTqvCaqBdGSV/Wmu10aw4LIC0REx6CnFIOIs/S0KFIGaN T1K8yHnMNZ+VkhzJDIg== X-Google-Smtp-Source: AGHT+IE5vJKdBIol6yLO/bCx7LzYtkzGMj0nrEKYhdyPGNNYWWWO3Zt70S5/gUk7SHW9elNAbwwPb9/d1PnA+b6V X-Received: from vsvj21.prod.google.com ([2002:a05:6102:3e15:b0:4af:dea8:7367]) (user=jthoughton job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6102:41a7:b0:4b1:1a11:95f8 with SMTP id ada2fe7eead31-4b3d0da8f06mr8495356137.11.1736455802892; Thu, 09 Jan 2025 12:50:02 -0800 (PST) Date: Thu, 9 Jan 2025 20:49:25 +0000 In-Reply-To: <20250109204929.1106563-1-jthoughton@google.com> Mime-Version: 1.0 References: <20250109204929.1106563-1-jthoughton@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20250109204929.1106563-10-jthoughton@google.com> Subject: [PATCH v2 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 , wei.w.wang@intel.com, 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-20250109_125003_751865_6D625A1B X-CRM114-Status: GOOD ( 11.57 ) 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 Thu Jan 9 20:49:26 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Houghton X-Patchwork-Id: 13933243 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 01B8CE77197 for ; Thu, 9 Jan 2025 21:04: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=Z8gbNSyyPwETvpRi8nU7lhBfk6CAq4ixhNTmiBrenco=; b=ACq3kW62hu7k2TTL3EiIZMgdew 3swv2h9GXR0liZn3n0Wj9IXC1jKus66cs107RCazQ1ucnTx/2yso0s5BfoLwAo17SypmYyW/TxxyN 1utlRS7IVSwllNaktV58ntTYEPH3tz7q5NPEp+Fp5yOoyB6L7dgTVuvglxkjeMe8107DIp8rmiWAZ qDA8zA7aMEwN3peQNGJT/cT3stbmJF7AUJwr42u459zhQ+/gKOh+T3EvsnSQxLZ5oFIC2lXHVJPoF kQ2GiQA0bM6BVMvSXUQ4tI/c1uwQmlrTXP/1miozL4O6RT8G6V2nj66iQZ22B8nVAePVtnXWNXpHo JFbumP+w==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tVzhc-0000000DJVK-1Qx4; Thu, 09 Jan 2025 21:04:28 +0000 Received: from mail-qv1-xf4a.google.com ([2607:f8b0:4864:20::f4a]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tVzTh-0000000DGa7-0m9p for linux-arm-kernel@lists.infradead.org; Thu, 09 Jan 2025 20:50:06 +0000 Received: by mail-qv1-xf4a.google.com with SMTP id 6a1803df08f44-6d87d6c09baso18867566d6.3 for ; Thu, 09 Jan 2025 12:50:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736455804; x=1737060604; 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=Z8gbNSyyPwETvpRi8nU7lhBfk6CAq4ixhNTmiBrenco=; b=HJ/H4vNcbdw92D0yRckC3IMojVJjP+tsJlg1s22iB3HlRAYp4NnshCFMpcDMJqtjhz fsbJ5Ccwf5KfDTXgRu/BB+j/NLzrniOJwlI7jDWPgeCmnNvvlFZKn0wmzKtNjalCisT5 KkNxGR9EuArVXZU/028rxGJj++b9IY4xt5MZFY4oGekSxyrNqf04+y6u2SpVlzVZCL2t Kg8t1QmT5FKfGaFEAbgLuLMzhrYffvZUEFPEZF6zpJ4iD0w15GLwQ+Dc8/iH15V6ywhJ Xr/Xoeyajj4OnL8N4KLkNedDlJMOKNuZr8f/OaU3krDilFZ0YvkfItjXZGCK2qfqy+uX a6LA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736455804; x=1737060604; 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=Z8gbNSyyPwETvpRi8nU7lhBfk6CAq4ixhNTmiBrenco=; b=tAMXfXUpSt1pt6/X/CtGk8NzmU99PqnrqBc7TmV3N2FlQP80QgxB9ZW3Ydqxc6oO7f EI6/YrjnRXBV/+5hsqwc1JKFNiP7RWfuEv5Te+lEqWLkAQEiKjwUdGxvlxKqRJkKAlK8 G8k4ul9OEWyb5V+7hogPT1G2v9C7cVd4ZbZS8h9TcrhY1oycd4rZd3SbCRRFPiuAP9PE u4+QIVhTsqQt/yXJ0yCXgwqcLnOBtDqILSZr4HnqxNUeE203GgM43kgFFbx3OoCMvtfG P0J1/C4wCYhRbf8Bupm6oAB9uqUfNB8Td22FahkdgbD4Gn7pjTwUcjoici1Ez3eqiJin 9uAw== X-Forwarded-Encrypted: i=1; AJvYcCVM2XjMC5v8R9ireX1pi4LROUX0cgUQpwlt5YuPVi8dFS4eeYGPIIoCdwF+99/tZ1CTW4xyfYl0Znu1TEWAU6KB@lists.infradead.org X-Gm-Message-State: AOJu0YyLdl5zfwx38svyGSX2qIAQpzxNHH/yvVxi8UpbVXMQ8LGYyHVI QCkpyKcWW626HJjAH+WavC90Lh1DuGaA3HOrJAzJF5n+qjNBuk58RawVhaspWfoCgTqPMPPOEzR Y23z6Qrq6dp+jvSZV4A== X-Google-Smtp-Source: AGHT+IEi3mzHkXPZiL5KdytvEtxQWpAfAta5Mi5rbpufEknhZSKdy69JHtvBwhVHRWx68M/7Cxqd+lUk9mWkjayn X-Received: from qvlh7.prod.google.com ([2002:a0c:f407:0:b0:6dd:3c13:842]) (user=jthoughton job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6214:53c6:b0:6d4:586:6291 with SMTP id 6a1803df08f44-6df9b232c31mr147596196d6.25.1736455803946; Thu, 09 Jan 2025 12:50:03 -0800 (PST) Date: Thu, 9 Jan 2025 20:49:26 +0000 In-Reply-To: <20250109204929.1106563-1-jthoughton@google.com> Mime-Version: 1.0 References: <20250109204929.1106563-1-jthoughton@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20250109204929.1106563-11-jthoughton@google.com> Subject: [PATCH v2 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 , wei.w.wang@intel.com, 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-20250109_125005_228176_88157297 X-CRM114-Status: GOOD ( 30.19 ) 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..183c70731093 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((_Atomic unsigned long *)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 4c4e5a847f67..0d49a9ce832a 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 a87988a162f1..a8f6b949ac59 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 Thu Jan 9 20:49:27 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Houghton X-Patchwork-Id: 13933244 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 2C7E6E77197 for ; Thu, 9 Jan 2025 21:05: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=A4uSddURoLEbUkGuIhSA7RvXzffvl1Ath1WbpEvQHEc=; b=G4f0VXl5gXbc888xHIKlugjx+J +eWrpYorDad8PXC2TymsXscYB/ZS6Y8jMJzAsTJAaPmLbnINcgVWHYg2A+37AIOPTR0mwN5YuaEwO h/uRwQ7AClwM8LyEWlnSycXqCIfTHZDsfs49ayWqZL4bLN+QZOHFLJYxSxau7oDJplH5mcwMLRbUb huPUt+nO83MXqfuHT8RZdzwZoFHP1Jris8CpQs6z6OxR2CxEKL6yU5FLTdfRSCux5AoPaoCEDub7b 6XeP0xvh0qQsBgoTeNnRlGkR1OpKSgftKWIJDUsiVxfGPCsl+xaj2t68mcZn2Dqm5l/zIWvQOgDTd 9AN6fLhg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tVzio-0000000DJcg-0KfV; Thu, 09 Jan 2025 21:05:42 +0000 Received: from mail-vs1-xe49.google.com ([2607:f8b0:4864:20::e49]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tVzTh-0000000DGaA-3NH4 for linux-arm-kernel@lists.infradead.org; Thu, 09 Jan 2025 20:50:06 +0000 Received: by mail-vs1-xe49.google.com with SMTP id ada2fe7eead31-4b3cc537766so264822137.2 for ; Thu, 09 Jan 2025 12:50:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736455804; x=1737060604; 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=A4uSddURoLEbUkGuIhSA7RvXzffvl1Ath1WbpEvQHEc=; b=eCqFgyFzNY62yBASutPGg8R+VJMarrGXzoIaVvp4llj56xCU18GuFZRrewcibXl/W8 VPBWtQQzz+IM6JLGTzNjnP/JTCKxDB1AzlcKXWcTd6c38mUt8LsvX+Ven2TrG3YH6SPt 6K8qfgpzDYOe1Nl/pMFja2rWkj6SgxLdKFVtLekHUiuGPHTnJbAlXsghDyc6gtZ6vX2C pUilZExxOFdrWRaCSk2y9Y8HYD2TDa1Dy+syqBS6PTbW+aQbcGNZ8El7zD/c5yE2XA/P UpvfmRmaf8e8yBUHyQuMfy8tlm47gn5VhAJpGB2t6xySaizqQCQ0yn/wD4cMZvOyiS6u K7gQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736455804; x=1737060604; 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=A4uSddURoLEbUkGuIhSA7RvXzffvl1Ath1WbpEvQHEc=; b=ZDUahEEwy1FRPK/wOC6yYwcOzAq9mbaCe4UuvldNE2ynoVmfO67VWDcpSeFT749KE/ gTD91alzcv13Nl8O3bQ6k7DYT5wM/e/b0UCSnZkYV9xnldTjNAQvz9QGnSvV6fFIm62M RKxzKKqumWR/h5npzRBANQaXKqUpAk3QzhPKYmHUwQ8NrXU8pWuAAr4biv0sZLehFiBg HQRjn+WRBpx8kbj26pDGwn2ubS/w4J5E3IQXVJn6SjkD3WR/7aKi8kRIChONHPyNi+OR 4MwmOffO8fsRUBO/haz2Vi5y8lPJRopIBOXjJ12nl2qFIme0y3xoUzBIp1ka61nPtqp3 YBEg== X-Forwarded-Encrypted: i=1; AJvYcCWLwXx6y3b7WYVFWYW49aiBGDn4oFoLKmp3xK/+8LMqeCY1YmxcVDqany6V63pSk9WzTj2VnpA2Q2Wgx+GcIGm2@lists.infradead.org X-Gm-Message-State: AOJu0Ywv2v0vvOcqQnBOJJQYpC8HRRJ76gQIoPxEeMG63K3pBwKam8Xz wET+tv19UiWJs3BfdtuGi13qg/O9B6ebI96E1fHoQZDTwSMA5y54d/xnY72zp063SIDiYBeDKI0 UYRN2vfvRW+GT2XCr2Q== X-Google-Smtp-Source: AGHT+IHUcz3SJhDUBT3AFAphG25I/PCLrMSFE5oskoAYquhM4FPeQ8hRbnzHYl6jdbwhQgIl3dB0klJ9OTTcUSfO X-Received: from vsbic11.prod.google.com ([2002:a05:6102:4b8b:b0:4af:dad1:fc51]) (user=jthoughton job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6102:3e20:b0:4af:e5fd:77fc with SMTP id ada2fe7eead31-4b3d0d75fcfmr8791219137.3.1736455804705; Thu, 09 Jan 2025 12:50:04 -0800 (PST) Date: Thu, 9 Jan 2025 20:49:27 +0000 In-Reply-To: <20250109204929.1106563-1-jthoughton@google.com> Mime-Version: 1.0 References: <20250109204929.1106563-1-jthoughton@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20250109204929.1106563-12-jthoughton@google.com> Subject: [PATCH v2 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 , wei.w.wang@intel.com, 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-20250109_125005_841359_4C140A1F X-CRM114-Status: GOOD ( 10.77 ) 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 86ee3385e860..adce75720cc1 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 Thu Jan 9 20:49:28 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Houghton X-Patchwork-Id: 13933245 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 ACC1BE77197 for ; Thu, 9 Jan 2025 21:07:06 +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=A6d9SLIy3ba7m9amfCG873jUIu7Rw+T0rUlSTA7+tzA=; b=pbzGnLmIOW4tGdhkRXBqIYjJgD 37F4fK2i0rEGo1JQHGBTdud7zTKHHLixycxZoZJa2eMfIIanEwUgbDNWT5oLn/Jc/Tt3hJ0f/wO9U C7AHD5Sh7rLMNTybi/+fTKFL9jPkzkLzsK/vEvjy9LrlgIj/eMVkYCrEyoIcsVyjD9hckNAsj4cLN dta8Cyv+cqhdC4+Q05LE1Mf17H7H/LtAoB9czShhyzZcrw3pDpzzc1oJuWiTM6sI7B2GwdhVG/8nn Zn1RoUo+qFySHKsUucmQeurPGmwtWOtVkOHXvvXFmNvP6y3usDfm7TQzJr18PbI6H/oAcqrpKW2UX BYqO/xKw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tVzjy-0000000DJif-2s25; Thu, 09 Jan 2025 21:06:54 +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 1tVzTi-0000000DGao-2fuR for linux-arm-kernel@lists.infradead.org; Thu, 09 Jan 2025 20:50:07 +0000 Received: by mail-qk1-x749.google.com with SMTP id af79cd13be357-7b6f1595887so246959085a.1 for ; Thu, 09 Jan 2025 12:50:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736455805; x=1737060605; 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=A6d9SLIy3ba7m9amfCG873jUIu7Rw+T0rUlSTA7+tzA=; b=tiRULIGVAEC+k1uEIfuss84UVT8wjdRRTVjQsUed61daZjQLvb8+ELQlA2/fDQSGy+ 7VHSoyrv2S73qegTpCkCVCHBW7wmSqf5ZMttMUCE69p26tfxhrbADtZ/AiugYoUV0S0p Q48SBxnKgR5FVnHkDSs1MSIkz9L/rhCamuQcQ048F/oFlJPfG/HDSsmnbwWq2H7/fwPP zFWaST2L/YSXVnmRntnsGkrKKugdddn629zK+TjoZkCSbIoX/pQRl7Ia4TTKFdOtr+UW PCJcq3k8T9xw2Ntds0mzWYqJv4S3afiR83e2oqPzpSHwWClpiwOhOyaO8xskw2rD7I2e WP7w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736455805; x=1737060605; 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=A6d9SLIy3ba7m9amfCG873jUIu7Rw+T0rUlSTA7+tzA=; b=qErU3rh2KYrFrLSeU8SgfxmnparmPJ+tl9Mea+UCIGD1c7waI9Bvqc+eZZctgRGsSg Q6UxF820dgBbczzJqa/ckMOasLvHi29plghByrF0biV8oamzXjVYaoRO4MlU/+XlLf7i LldDeUsphbr5SqxlGonZCCTTAsA9OH2eIZkXMlPmzd2pjQp3AtKvvdbWFMEf/h8vNJFT SCK2iAnq0qgsAnxJl7/cL7IbbqkI6MLDzWfzCZllwVGHuQA4zKRdaaFJvGdnzVmFA8WP ZOTQnxxylhSQbVDRjyZBr19uRahXMSgK1vuMSsTeQApTFxS4G9bfNJTrviboj8hD6VYt Nevg== X-Forwarded-Encrypted: i=1; AJvYcCXZfVwxc5F2VMiZvHEQzg99wjHZzRiyyFyIyRBYufVEwFk6Fl3rrJLJNFWzvK5Hf+m6bpaHhH2SkyUGC6NXZuJK@lists.infradead.org X-Gm-Message-State: AOJu0YzY9yiq55Erj1b65PHrCPDuyQqNCol4OZ8mv1JzIArK/IbaDt9d /2UbhzybhOgDwBdPHrmKIIli3XT0pmexUnpZz+tJqvGAzXTno8QY7WDHsEVxqRe5sMRbIBECcGJ aQ8jREI2+8CDA7TpqWg== X-Google-Smtp-Source: AGHT+IGuiGH+ZZwCsi1PGFRN10vipIun+GV8y4B9awYjMBGGjqu8CtE272hh3gh4o7UZqaJM/jxBCOqhOtlKtS/V X-Received: from qkpr8.prod.google.com ([2002:a05:620a:2988:b0:7b6:e6cf:180e]) (user=jthoughton job=prod-delivery.src-stubby-dispatcher) by 2002:a05:620a:240d:b0:7b1:e0f:bf9b with SMTP id af79cd13be357-7bcd97afd59mr1217697485a.45.1736455805434; Thu, 09 Jan 2025 12:50:05 -0800 (PST) Date: Thu, 9 Jan 2025 20:49:28 +0000 In-Reply-To: <20250109204929.1106563-1-jthoughton@google.com> Mime-Version: 1.0 References: <20250109204929.1106563-1-jthoughton@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20250109204929.1106563-13-jthoughton@google.com> Subject: [PATCH v2 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 , wei.w.wang@intel.com, 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-20250109_125006_680584_7E0F1DC9 X-CRM114-Status: GOOD ( 10.51 ) 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 adce75720cc1..1fea8ff0fe74 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 Thu Jan 9 20:49:29 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Houghton X-Patchwork-Id: 13933246 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 3D8FCE77197 for ; Thu, 9 Jan 2025 21:08:17 +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=1FPGE2VUglE9CFYJaRkZs5d1Qv8okwjTkQX7zeGY738=; b=TlSa8FfmdLtDzaHMTlG0hfmIbo eaWskmxJOaXYgRvkTBl7dFNKRFrod3wczlCuEk8bfYiVuJV/QCka2ITq+H3r9sy/2ze8qJINoLix7 c39kFuP4yigMcUtWwPVyjf0fNGT6BOG2wImKS40OnokeJULYZlbevrs2eoZKWcW2EIF1YTCb71ACM I+kTfcqg0qH3B75GA86exXNMDleh9GPkaXP3L+wZWgtLs9xYgHpZ9NOkmsaxNYtBfx9Mj8pJcgSD2 TbQ3OMkPEWhqmtk8ZRlZa1uqooYhoYxhpAFh+J7tnXIOxOGrmlz/gxhxnSDvVWDnE+KVS6f1bmzxD dOiGKZKA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tVzl9-0000000DJqQ-1Ean; Thu, 09 Jan 2025 21:08:07 +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 1tVzTj-0000000DGbV-3AgD for linux-arm-kernel@lists.infradead.org; Thu, 09 Jan 2025 20:50:08 +0000 Received: by mail-vs1-xe4a.google.com with SMTP id ada2fe7eead31-4aff533abadso835579137.1 for ; Thu, 09 Jan 2025 12:50:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736455806; x=1737060606; 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=1FPGE2VUglE9CFYJaRkZs5d1Qv8okwjTkQX7zeGY738=; b=Xjl0BjwpdhW6/wPUMLsZ0xpD1+mL5qjGwcDWdeorRxCkLh+03BV19psONFeQUHuifD pBe2iYy1PUYHo54CjplqbfWl/JsIiVOuu076G9Hv+eeWqEJf7IjFDuEDuRyXGDoCuEhJ x9YWy1e6tg733MeekrLJGGgmCUhqjqwpEkpse8EdAXXuptCeVD11KeD6epnfgWVN8nV5 2b9ALZJIeFUdQepZoTtDr3KAYhWGaup3nGUCu7jhM3bSPvwqVbyfuyF6IuC9CuXifxBx V0k1maFKM1/qnUMuu47RPzuApVxYIhyBjGbE+HOflxnsij7pDHLO3vhtl8uj++LRQ/4Z scQA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736455806; x=1737060606; 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=1FPGE2VUglE9CFYJaRkZs5d1Qv8okwjTkQX7zeGY738=; b=GzDr+98l+uGgc15XzWFlqvnqcvitet6xyTpLUFccsi1XVVcCdG6Nv7ht8DyJmMTWvt LvPiiHWJSGw+YpwlNRgFTc2DeKh0N/Ap4MzDQw2dRje970BKhfSjOL+oT7Nv8XVuehkL bIaLV5sFkNC4HgX7zd6JcgdK+3xobBd6uK5z24Rxe0dkU3SWkLcbmFpSkuye6KMsfiQz vmFnE3hcywNmwD0hAG8Aq/3Mv3XrhCZlgDiinCfC0MElYdXvoLst04vVbITAXhAvtIyU oIceUJ6lh6TAU++ieLGg1K3Q7UKjjjPTCteBLrVYBvxK4Nqtg0j2C/odIISCE4eUTPbx xhrA== X-Forwarded-Encrypted: i=1; AJvYcCXcDFrE8OTd9zGpzStX2BztkAxEHT34BTQ8De8V6Y/vLpyYgzJMSXmsAmOZgf1hU3Ca3op96k8MMVPlO4LPpytM@lists.infradead.org X-Gm-Message-State: AOJu0YwWY4H9QVSGZptGvY5SiPfk0aG9+JuVNN/IcsfDQ4J4NkmLb/0y 52ILR1b4lTHVFTA4PicH1CByUXnlUErILq7W4lMAR18i9xc4u5016+ePKeHJoKrqZpuMwdnMp1D JMZnqfp7PiFT04YtyTQ== X-Google-Smtp-Source: AGHT+IER4UDdWkNRD2ZPfHth4m0AOq+dYEUlBJOggH5PvPrkTbjMUbHW/yGSl0VACauqUoUnO5tUTjFJuH8L321Q X-Received: from vsvg20.prod.google.com ([2002:a05:6102:1594:b0:4af:fda4:ed12]) (user=jthoughton job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6102:6e88:b0:4b4:e7e7:56c0 with SMTP id ada2fe7eead31-4b4e7e76563mr5145199137.3.1736455806482; Thu, 09 Jan 2025 12:50:06 -0800 (PST) Date: Thu, 9 Jan 2025 20:49:29 +0000 In-Reply-To: <20250109204929.1106563-1-jthoughton@google.com> Mime-Version: 1.0 References: <20250109204929.1106563-1-jthoughton@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20250109204929.1106563-14-jthoughton@google.com> Subject: [PATCH v2 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 , wei.w.wang@intel.com, kvm@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, Bagas Sanjaya X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250109_125007_803582_370C9FD1 X-CRM114-Status: GOOD ( 14.99 ) 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 --- 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. ======================