As of today, trying to enable dirty-ring with a size bigger than the
maximum will return an "argument list too long" error.

Change vm_enable_dirty_ring() to get the maximum size, then compare it to
the desired size before enabling. If the value is invalid, print a more
precise error message.

Signed-off-by: Leonardo Bras <[email protected]>
---
 tools/testing/selftests/kvm/lib/kvm_util.c | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)

diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c 
b/tools/testing/selftests/kvm/lib/kvm_util.c
index 195f3fdae1e3..237f0ce0ae60 100644
--- a/tools/testing/selftests/kvm/lib/kvm_util.c
+++ b/tools/testing/selftests/kvm/lib/kvm_util.c
@@ -160,24 +160,34 @@ unsigned int kvm_check_cap(long cap)
        ret = __kvm_ioctl(kvm_fd, KVM_CHECK_EXTENSION, (void *)cap);
        TEST_ASSERT(ret >= 0, KVM_IOCTL_ERROR(KVM_CHECK_EXTENSION, ret));
 
        kvm_free_fd(kvm_fd);
 
        return (unsigned int)ret;
 }
 
 void vm_enable_dirty_ring(struct kvm_vm *vm, u32 ring_size)
 {
-       if (vm_check_cap(vm, KVM_CAP_DIRTY_LOG_RING_ACQ_REL))
-               vm_enable_cap(vm, KVM_CAP_DIRTY_LOG_RING_ACQ_REL, ring_size);
-       else
-               vm_enable_cap(vm, KVM_CAP_DIRTY_LOG_RING, ring_size);
+       long cap = KVM_CAP_DIRTY_LOG_RING_ACQ_REL;
+       int max_size = vm_check_cap(vm, cap);
+
+       if (!max_size) {
+               cap = KVM_CAP_DIRTY_LOG_RING;
+               max_size = vm_check_cap(vm, cap);
+       }
+
+       TEST_ASSERT(ring_size <= max_size && is_power_of_2(ring_size),
+                   "Invalid dirty-ring size: Should be a power of two "
+                   "smaller than %d entries\n",
+                   max_size / sizeof(struct kvm_dirty_gfn));
+
+       vm_enable_cap(vm, cap, ring_size);
        vm->dirty_ring_size = ring_size;
 }
 
 static void vm_open(struct kvm_vm *vm)
 {
        vm->kvm_fd = _open_kvm_dev_path_or_exit(O_RDWR);
 
        TEST_REQUIRE(kvm_has_cap(KVM_CAP_IMMEDIATE_EXIT));
 
        vm->fd = __kvm_ioctl(vm->kvm_fd, KVM_CREATE_VM, (void *)vm->type);
-- 
2.54.0


Reply via email to