Re: [PATCH v10 2/9] KVM: Introduce per-page memory attributes
From: Isaku Yamahata <hidden>
Date: 2023-02-09 07:25:54
Also in:
kvm, linux-arch, linux-doc, linux-fsdevel, linux-mm, lkml, qemu-devel
On Fri, Dec 02, 2022 at 02:13:40PM +0800, Chao Peng [off-list ref] wrote:
+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;
+ u64 supported_attrs = kvm_supported_mem_attributes(kvm);
+
+ /* flags is currently not used. */
+ if (attrs->flags)
+ return -EINVAL;
+ if (attrs->attributes & ~supported_attrs)
+ return -EINVAL;
+ if (attrs->size == 0 || attrs->address + attrs->size < attrs->address)
+ return -EINVAL;
+ if (!PAGE_ALIGNED(attrs->address) || !PAGE_ALIGNED(attrs->size))
+ return -EINVAL;
+
+ 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->lock);
+ for (i = start; i < end; i++)
+ if (xa_err(xa_store(&kvm->mem_attr_array, i, entry,
+ GFP_KERNEL_ACCOUNT)))
+ break;
+ mutex_unlock(&kvm->lock);
+
+ attrs->address = i << PAGE_SHIFT;
+ attrs->size = (end - i) << PAGE_SHIFT;
+
+ return 0;
+}
+#endif /* CONFIG_HAVE_KVM_MEMORY_ATTRIBUTES */
+
If memslot isn't private, it should return error if private attribute is set.
Something like following check is needed.
+ if (attrs->flags & KVM_MEM_PRIVATE) {
+ /* non-private memory slot doesn't allow KVM_MEM_PRIVATE */
+ for (i = 0; i < kvm_arch_nr_memslot_as_ids(kvm); i++) {
+ struct kvm_memslot_iter iter;
+ struct kvm_memslots *slots;
+
+ slots = __kvm_memslots(kvm, i);
+ kvm_for_each_memslot_in_gfn_range(&iter, slots, start, end) {
+ if (!kvm_slot_can_be_private(iter.slot)) {
+ mutex_unlock(&kvm->slots_lock);
+ return -EINVAL;
+ }
+ }
+ }
+ }
+
--
Isaku Yamahata [off-list ref]