[PATCH RFC v8 06/56] KVM: Split out memory attribute xarray updates to helper function
From: Michael Roth
Date: Mon Feb 20 2023 - 14:12:20 EST
This will be useful to other callers that need to update memory
attributes for things like setting up the initial private memory payload
for a guest.
Signed-off-by: Michael Roth <michael.roth@xxxxxxx>
---
include/linux/kvm_host.h | 1 +
virt/kvm/kvm_main.c | 26 ++++++++++++++++++--------
2 files changed, 19 insertions(+), 8 deletions(-)
diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
index c615650ed256..57d56cd09a61 100644
--- a/include/linux/kvm_host.h
+++ b/include/linux/kvm_host.h
@@ -993,6 +993,7 @@ int kvm_init(unsigned vcpu_size, unsigned vcpu_align, struct module *module);
void kvm_exit(void);
void kvm_get_kvm(struct kvm *kvm);
+int kvm_vm_set_region_attr(struct kvm *kvm, gfn_t start, gfn_t end, u64 attributes);
bool kvm_get_kvm_safe(struct kvm *kvm);
void kvm_put_kvm(struct kvm *kvm);
bool file_is_kvm(struct file *file);
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 4ccd655dd5af..c740b56d6ba4 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -2645,12 +2645,28 @@ static void kvm_post_mem_attrs_changed(struct kvm *kvm, unsigned long attrs,
}
}
+int kvm_vm_set_region_attr(struct kvm *kvm, gfn_t start, gfn_t end,
+ u64 attributes)
+{
+ gfn_t index;
+ void *entry;
+
+ entry = attributes ? xa_mk_value(attributes) : NULL;
+
+ for (index = start; index < end; index++)
+ if (xa_err(xa_store(&kvm->mem_attr_array, index, entry,
+ GFP_KERNEL_ACCOUNT)))
+ break;
+
+ return index;
+}
+EXPORT_SYMBOL_GPL(kvm_vm_set_region_attr);
+
static int kvm_vm_ioctl_set_mem_attributes(struct kvm *kvm,
struct kvm_memory_attributes *attrs)
{
gfn_t start, end;
unsigned long i;
- void *entry;
/* flags is currently not used. */
if (attrs->flags)
@@ -2665,8 +2681,6 @@ static int kvm_vm_ioctl_set_mem_attributes(struct kvm *kvm,
start = attrs->address >> PAGE_SHIFT;
end = (attrs->address + attrs->size - 1 + PAGE_SIZE) >> PAGE_SHIFT;
- entry = attrs->attributes ? xa_mk_value(attrs->attributes) : NULL;
-
mutex_lock(&kvm->slots_lock);
KVM_MMU_LOCK(kvm);
@@ -2674,11 +2688,7 @@ static int kvm_vm_ioctl_set_mem_attributes(struct kvm *kvm,
kvm_mmu_invalidate_range_add(kvm, start, end);
KVM_MMU_UNLOCK(kvm);
- for (i = start; i < end; i++)
- if (xa_err(xa_store(&kvm->mem_attr_array, i, entry,
- GFP_KERNEL_ACCOUNT)))
- break;
-
+ i = kvm_vm_set_region_attr(kvm, start, end, attrs->attributes);
KVM_MMU_LOCK(kvm);
if (i > start)
--
2.25.1