VSM will keep track of each VTL's memory protections in a separate
mem_attr_array. Access to these arrays will happen by issuing
KVM_SET_MEMORY_ATTRIBUTES ioctls to their respective KVM VTL devices
(which is also introduced in subsequent patches). Let the VTL devices
reuse kvm_ioctl_set_mem_attributes() by decoupling it from struct kvm's
mem_attr_array. The xarray is now input as a function argument as well
as the list of supported memory attributes.

Signed-off-by: Nicolas Saenz Julienne <nsa...@amazon.com>
---
 include/linux/kvm_host.h |  3 +++
 virt/kvm/kvm_main.c      | 32 ++++++++++++++++++++++----------
 2 files changed, 25 insertions(+), 10 deletions(-)

diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
index 652656444c45..ad104794037f 100644
--- a/include/linux/kvm_host.h
+++ b/include/linux/kvm_host.h
@@ -2394,6 +2394,9 @@ kvm_get_memory_attributes(struct xarray *mem_attr_array, 
gfn_t gfn)
        return xa_to_value(xa_load(mem_attr_array, gfn));
 }
 
+int kvm_ioctl_set_mem_attributes(struct kvm *kvm, struct xarray 
*mem_attr_array,
+                                u64 supported_attrs,
+                                struct kvm_memory_attributes *attrs);
 bool kvm_range_has_memory_attributes(struct xarray *mem_attr_array, gfn_t 
start,
                                     gfn_t end, unsigned long attrs);
 bool kvm_arch_pre_set_memory_attributes(struct kvm *kvm,
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index f20dafaedc72..74c4c42b2126 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -2554,8 +2554,9 @@ static bool kvm_pre_set_memory_attributes(struct kvm *kvm,
 }
 
 /* Set @attributes for the gfn range [@start, @end). */
-static int kvm_vm_set_mem_attributes(struct kvm *kvm, gfn_t start, gfn_t end,
-                                    unsigned long attributes)
+static int kvm_set_mem_attributes(struct kvm *kvm,
+                                 struct xarray *mem_attr_array, gfn_t start,
+                                 gfn_t end, unsigned long attributes)
 {
        struct kvm_mmu_notifier_range pre_set_range = {
                .start = start,
@@ -2569,7 +2570,7 @@ static int kvm_vm_set_mem_attributes(struct kvm *kvm, 
gfn_t start, gfn_t end,
                .start = start,
                .end = end,
                .arg.attributes = attributes,
-               .arg.mem_attr_array = &kvm->mem_attr_array,
+               .arg.mem_attr_array = mem_attr_array,
                .handler = kvm_arch_post_set_memory_attributes,
                .on_lock = kvm_mmu_invalidate_end,
                .may_block = true,
@@ -2583,7 +2584,7 @@ static int kvm_vm_set_mem_attributes(struct kvm *kvm, 
gfn_t start, gfn_t end,
        mutex_lock(&kvm->slots_lock);
 
        /* Nothing to do if the entire range as the desired attributes. */
-       if (kvm_range_has_memory_attributes(&kvm->mem_attr_array, start, end,
+       if (kvm_range_has_memory_attributes(mem_attr_array, start, end,
                                            attributes))
                goto out_unlock;
 
@@ -2592,7 +2593,7 @@ static int kvm_vm_set_mem_attributes(struct kvm *kvm, 
gfn_t start, gfn_t end,
         * partway through setting the new attributes.
         */
        for (i = start; i < end; i++) {
-               r = xa_reserve(&kvm->mem_attr_array, i, GFP_KERNEL_ACCOUNT);
+               r = xa_reserve(mem_attr_array, i, GFP_KERNEL_ACCOUNT);
                if (r)
                        goto out_unlock;
        }
@@ -2600,7 +2601,7 @@ static int kvm_vm_set_mem_attributes(struct kvm *kvm, 
gfn_t start, gfn_t end,
        kvm_handle_gfn_range(kvm, &pre_set_range);
 
        for (i = start; i < end; i++) {
-               r = xa_err(xa_store(&kvm->mem_attr_array, i, entry,
+               r = xa_err(xa_store(mem_attr_array, i, entry,
                                    GFP_KERNEL_ACCOUNT));
                KVM_BUG_ON(r, kvm);
        }
@@ -2612,15 +2613,17 @@ static int kvm_vm_set_mem_attributes(struct kvm *kvm, 
gfn_t start, gfn_t end,
 
        return r;
 }
-static int kvm_vm_ioctl_set_mem_attributes(struct kvm *kvm,
-                                          struct kvm_memory_attributes *attrs)
+
+int kvm_ioctl_set_mem_attributes(struct kvm *kvm, struct xarray 
*mem_attr_array,
+                                u64 supported_attrs,
+                                struct kvm_memory_attributes *attrs)
 {
        gfn_t start, end;
 
        /* flags is currently not used. */
        if (attrs->flags)
                return -EINVAL;
-       if (attrs->attributes & ~kvm_supported_mem_attributes(kvm))
+       if (attrs->attributes & ~supported_attrs)
                return -EINVAL;
        if (attrs->size == 0 || attrs->address + attrs->size < attrs->address)
                return -EINVAL;
@@ -2637,7 +2640,16 @@ static int kvm_vm_ioctl_set_mem_attributes(struct kvm 
*kvm,
         */
        BUILD_BUG_ON(sizeof(attrs->attributes) != sizeof(unsigned long));
 
-       return kvm_vm_set_mem_attributes(kvm, start, end, attrs->attributes);
+       return kvm_set_mem_attributes(kvm, mem_attr_array, start, end,
+                                     attrs->attributes);
+}
+
+static int kvm_vm_ioctl_set_mem_attributes(struct kvm *kvm,
+                                          struct kvm_memory_attributes *attrs)
+{
+       return kvm_ioctl_set_mem_attributes(kvm, &kvm->mem_attr_array,
+                                           kvm_supported_mem_attributes(kvm),
+                                           attrs);
 }
 #endif /* CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES */
 
-- 
2.40.1


Reply via email to