From: Emanuele Giuseppe Esposito <eesposit@redhat.com>
To: kvm@vger.kernel.org
Cc: Paolo Bonzini <pbonzini@redhat.com>,
Sean Christopherson <seanjc@google.com>,
Vitaly Kuznetsov <vkuznets@redhat.com>,
Wanpeng Li <wanpengli@tencent.com>,
Jim Mattson <jmattson@google.com>, Joerg Roedel <joro@8bytes.org>,
Thomas Gleixner <tglx@linutronix.de>,
Ingo Molnar <mingo@redhat.com>, Borislav Petkov <bp@alien8.de>,
Dave Hansen <dave.hansen@linux.intel.com>,
David Hildenbrand <david@redhat.com>,
Maxim Levitsky <mlevitsk@redhat.com>,
x86@kernel.org, "H. Peter Anvin" <hpa@zytor.com>,
linux-kernel@vger.kernel.org,
Emanuele Giuseppe Esposito <eesposit@redhat.com>
Subject: [RFC PATCH 5/9] kvm_main.c: split __kvm_set_memory_region logic in kvm_check_mem and kvm_prepare_batch
Date: Fri, 9 Sep 2022 06:45:02 -0400 [thread overview]
Message-ID: <20220909104506.738478-6-eesposit@redhat.com> (raw)
In-Reply-To: <20220909104506.738478-1-eesposit@redhat.com>
Just a function split. No functional change intended,
except for the fact that kvm_prepare_batch() does not
immediately call kvm_set_memslot() if batch->change is
KVM_MR_DELETE, but delegates the caller (__kvm_set_memory_region).
Signed-off-by: Emanuele Giuseppe Esposito <eesposit@redhat.com>
---
virt/kvm/kvm_main.c | 120 +++++++++++++++++++++++++++++---------------
1 file changed, 79 insertions(+), 41 deletions(-)
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 17f07546d591..9d917af30593 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -1927,19 +1927,9 @@ static bool kvm_check_memslot_overlap(struct kvm_memslots *slots, int id,
return false;
}
-/*
- * Allocate some memory and give it an address in the guest physical address
- * space.
- *
- * Discontiguous memory is allowed, mostly for framebuffers.
- * This function takes also care of initializing batch->new/old/invalid/change
- * fields.
- *
- * Must be called holding kvm->slots_lock for write.
- */
-int __kvm_set_memory_region(struct kvm *kvm,
- const struct kvm_userspace_memory_region *mem,
- struct kvm_internal_memory_region_list *batch)
+static int kvm_prepare_batch(struct kvm *kvm,
+ const struct kvm_userspace_memory_region *mem,
+ struct kvm_internal_memory_region_list *batch)
{
struct kvm_memory_slot *old, *new;
struct kvm_memslots *slots;
@@ -1947,34 +1937,10 @@ int __kvm_set_memory_region(struct kvm *kvm,
unsigned long npages;
gfn_t base_gfn;
int as_id, id;
- int r;
-
- r = check_memory_region_flags(mem);
- if (r)
- return r;
as_id = mem->slot >> 16;
id = (u16)mem->slot;
- /* General sanity checks */
- if ((mem->memory_size & (PAGE_SIZE - 1)) ||
- (mem->memory_size != (unsigned long)mem->memory_size))
- return -EINVAL;
- if (mem->guest_phys_addr & (PAGE_SIZE - 1))
- return -EINVAL;
- /* We can read the guest memory with __xxx_user() later on. */
- if ((mem->userspace_addr & (PAGE_SIZE - 1)) ||
- (mem->userspace_addr != untagged_addr(mem->userspace_addr)) ||
- !access_ok((void __user *)(unsigned long)mem->userspace_addr,
- mem->memory_size))
- return -EINVAL;
- if (as_id >= KVM_ADDRESS_SPACE_NUM || id >= KVM_MEM_SLOTS_NUM)
- return -EINVAL;
- if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
- return -EINVAL;
- if ((mem->memory_size >> PAGE_SHIFT) > KVM_MEM_MAX_NR_PAGES)
- return -EINVAL;
-
slots = __kvm_memslots(kvm, as_id);
/*
@@ -1993,7 +1959,7 @@ int __kvm_set_memory_region(struct kvm *kvm,
batch->change = KVM_MR_DELETE;
batch->new = NULL;
- return kvm_set_memslot(kvm, batch);
+ return 0;
}
base_gfn = (mem->guest_phys_addr >> PAGE_SHIFT);
@@ -2020,7 +1986,7 @@ int __kvm_set_memory_region(struct kvm *kvm,
else if (mem->flags != old->flags)
change = KVM_MR_FLAGS_ONLY;
else /* Nothing to change. */
- return 0;
+ return 1;
}
if ((change == KVM_MR_CREATE || change == KVM_MR_MOVE) &&
@@ -2041,12 +2007,81 @@ int __kvm_set_memory_region(struct kvm *kvm,
batch->new = new;
batch->change = change;
+ return 0;
+}
+
+static int kvm_check_mem(const struct kvm_userspace_memory_region *mem)
+{
+ int as_id, id;
+
+ as_id = mem->slot >> 16;
+ id = (u16)mem->slot;
+
+ /* General sanity checks */
+ if ((mem->memory_size & (PAGE_SIZE - 1)) ||
+ (mem->memory_size != (unsigned long)mem->memory_size))
+ return -EINVAL;
+ if (mem->guest_phys_addr & (PAGE_SIZE - 1))
+ return -EINVAL;
+ /* We can read the guest memory with __xxx_user() later on. */
+ if ((mem->userspace_addr & (PAGE_SIZE - 1)) ||
+ (mem->userspace_addr != untagged_addr(mem->userspace_addr)) ||
+ !access_ok((void __user *)(unsigned long)mem->userspace_addr,
+ mem->memory_size))
+ return -EINVAL;
+ if (as_id >= KVM_ADDRESS_SPACE_NUM || id >= KVM_MEM_SLOTS_NUM)
+ return -EINVAL;
+ if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
+ return -EINVAL;
+ if ((mem->memory_size >> PAGE_SHIFT) > KVM_MEM_MAX_NR_PAGES)
+ return -EINVAL;
+
+ return 0;
+}
+
+static int kvm_check_memory_region(struct kvm *kvm,
+ const struct kvm_userspace_memory_region *mem,
+ struct kvm_internal_memory_region_list *batch)
+{
+ int r;
+
+ r = check_memory_region_flags(mem);
+ if (r)
+ return r;
- r = kvm_set_memslot(kvm, batch);
+ r = kvm_check_mem(mem);
if (r)
- kfree(new);
+ return r;
+
+ r = kvm_prepare_batch(kvm, mem, batch);
+ if (r && batch->new)
+ kfree(batch->new);
+
return r;
}
+
+/*
+ * Allocate some memory and give it an address in the guest physical address
+ * space.
+ *
+ * Discontiguous memory is allowed, mostly for framebuffers.
+ * This function takes also care of initializing batch->new/old/invalid/change
+ * fields.
+ *
+ * Must be called holding kvm->slots_lock for write.
+ */
+int __kvm_set_memory_region(struct kvm *kvm,
+ const struct kvm_userspace_memory_region *mem,
+ struct kvm_internal_memory_region_list *batch)
+{
+ int r;
+
+ r = kvm_check_memory_region(kvm, mem, batch);
+ if (r)
+ return r;
+
+ return kvm_set_memslot(kvm, batch);
+}
EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
static int kvm_set_memory_region(struct kvm *kvm,
@@ -2061,6 +2096,9 @@ static int kvm_set_memory_region(struct kvm *kvm,
mutex_lock(&kvm->slots_lock);
r = __kvm_set_memory_region(kvm, mem, batch);
mutex_unlock(&kvm->slots_lock);
+ /* r == 1 means empty request, nothing to do but no error */
+ if (r == 1)
+ r = 0;
return r;
}
--
2.31.1
next prev parent reply other threads:[~2022-09-09 10:45 UTC|newest]
Thread overview: 58+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-09-09 10:44 [RFC PATCH 0/9] kvm: implement atomic memslot updates Emanuele Giuseppe Esposito
2022-09-09 10:44 ` [RFC PATCH 1/9] kvm_main.c: move slot check in kvm_set_memory_region Emanuele Giuseppe Esposito
2022-09-28 16:41 ` Paolo Bonzini
2022-09-09 10:44 ` [RFC PATCH 2/9] kvm.h: introduce KVM_SET_USER_MEMORY_REGION_LIST ioctl Emanuele Giuseppe Esposito
2022-09-28 16:42 ` Paolo Bonzini
2022-09-09 10:45 ` [RFC PATCH 3/9] kvm_main.c: introduce kvm_internal_memory_region_list Emanuele Giuseppe Esposito
2022-09-28 16:48 ` Paolo Bonzini
2022-09-09 10:45 ` [RFC PATCH 4/9] kvm_main.c: split logic in kvm_set_memslots Emanuele Giuseppe Esposito
2022-09-28 17:04 ` Paolo Bonzini
2022-09-09 10:45 ` Emanuele Giuseppe Esposito [this message]
2022-09-13 2:56 ` [RFC PATCH 5/9] kvm_main.c: split __kvm_set_memory_region logic in kvm_check_mem and kvm_prepare_batch Yang, Weijiang
2022-09-18 16:22 ` Emanuele Giuseppe Esposito
2022-09-28 17:11 ` Paolo Bonzini
2022-09-09 10:45 ` [RFC PATCH 6/9] kvm_main.c: simplify change-specific callbacks Emanuele Giuseppe Esposito
2022-09-09 10:45 ` [RFC PATCH 7/9] kvm_main.c: duplicate invalid memslot also in inactive list Emanuele Giuseppe Esposito
2022-09-28 17:18 ` Paolo Bonzini
2022-09-09 10:45 ` [RFC PATCH 8/9] kvm_main.c: find memslots from the inactive memslot list Emanuele Giuseppe Esposito
2022-09-09 10:45 ` [RFC PATCH 9/9] kvm_main.c: handle atomic memslot update Emanuele Giuseppe Esposito
2022-09-13 2:30 ` Yang, Weijiang
2022-09-18 16:18 ` Emanuele Giuseppe Esposito
2022-09-27 7:46 ` David Hildenbrand
2022-09-27 8:35 ` Emanuele Giuseppe Esposito
2022-09-27 9:22 ` David Hildenbrand
2022-09-27 9:32 ` Emanuele Giuseppe Esposito
2022-09-27 14:52 ` David Hildenbrand
2022-09-28 17:29 ` Paolo Bonzini
2022-09-09 14:30 ` [RFC PATCH 0/9] kvm: implement atomic memslot updates Sean Christopherson
2022-09-18 16:13 ` Emanuele Giuseppe Esposito
2022-09-19 7:38 ` Like Xu
2022-09-19 7:53 ` David Hildenbrand
2022-09-19 17:30 ` David Hildenbrand
2022-09-23 13:10 ` Emanuele Giuseppe Esposito
2022-09-23 13:21 ` David Hildenbrand
2022-09-23 13:38 ` Emanuele Giuseppe Esposito
2022-09-26 9:03 ` David Hildenbrand
2022-09-26 21:28 ` Sean Christopherson
2022-09-27 7:38 ` Emanuele Giuseppe Esposito
2022-09-27 15:58 ` Sean Christopherson
2022-09-28 9:11 ` Emanuele Giuseppe Esposito
2022-09-28 11:14 ` Maxim Levitsky
2022-09-28 12:52 ` David Hildenbrand
2022-09-28 15:07 ` Paolo Bonzini
2022-09-28 15:33 ` David Hildenbrand
2022-09-28 15:58 ` Sean Christopherson
2022-09-28 16:38 ` Paolo Bonzini
2022-09-28 20:41 ` Sean Christopherson
2022-09-29 8:05 ` Emanuele Giuseppe Esposito
2022-09-29 8:24 ` David Hildenbrand
2022-09-29 15:18 ` Sean Christopherson
2022-09-29 15:41 ` Paolo Bonzini
2022-09-29 15:28 ` Paolo Bonzini
2022-09-29 15:40 ` Maxim Levitsky
2022-09-29 16:00 ` David Hildenbrand
2022-09-29 21:39 ` Sean Christopherson
2022-10-13 7:43 ` Emanuele Giuseppe Esposito
2022-10-13 8:44 ` David Hildenbrand
2022-10-13 11:12 ` Emanuele Giuseppe Esposito
2022-10-13 14:45 ` David Hildenbrand
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220909104506.738478-6-eesposit@redhat.com \
--to=eesposit@redhat.com \
--cc=bp@alien8.de \
--cc=dave.hansen@linux.intel.com \
--cc=david@redhat.com \
--cc=hpa@zytor.com \
--cc=jmattson@google.com \
--cc=joro@8bytes.org \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@redhat.com \
--cc=mlevitsk@redhat.com \
--cc=pbonzini@redhat.com \
--cc=seanjc@google.com \
--cc=tglx@linutronix.de \
--cc=vkuznets@redhat.com \
--cc=wanpengli@tencent.com \
--cc=x86@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®