Refactor align() to work with non-pointers, add align_ptr() for use with
pointers, and expose both helpers so that they can be used by tests
and/or other utilities.  The align() helper in particular will be used
to ensure gpa alignment for hugepages.

No functional change intended.

Signed-off-by: Sean Christopherson <sea...@google.com>
---
 tools/testing/selftests/kvm/include/kvm_util.h | 15 +++++++++++++++
 tools/testing/selftests/kvm/lib/kvm_util.c     | 11 +----------
 2 files changed, 16 insertions(+), 10 deletions(-)

diff --git a/tools/testing/selftests/kvm/include/kvm_util.h 
b/tools/testing/selftests/kvm/include/kvm_util.h
index 2d7eb6989e83..4b5d2362a68a 100644
--- a/tools/testing/selftests/kvm/include/kvm_util.h
+++ b/tools/testing/selftests/kvm/include/kvm_util.h
@@ -79,6 +79,21 @@ struct vm_guest_mode_params {
 };
 extern const struct vm_guest_mode_params vm_guest_mode_params[];
 
+/* Aligns x up to the next multiple of size. Size must be a power of 2. */
+static inline uint64_t align(uint64_t x, uint64_t size)
+{
+       uint64_t mask = size - 1;
+
+       TEST_ASSERT(size != 0 && !(size & (size - 1)),
+                   "size not a power of 2: %lu", size);
+       return ((x + mask) & ~mask);
+}
+
+static inline void *align_ptr(void *x, size_t size)
+{
+       return (void *)align((unsigned long)x, size);
+}
+
 int kvm_check_cap(long cap);
 int vm_enable_cap(struct kvm_vm *vm, struct kvm_enable_cap *cap);
 int vcpu_enable_cap(struct kvm_vm *vm, uint32_t vcpu_id,
diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c 
b/tools/testing/selftests/kvm/lib/kvm_util.c
index 960f4c5129ff..584167c6dbc7 100644
--- a/tools/testing/selftests/kvm/lib/kvm_util.c
+++ b/tools/testing/selftests/kvm/lib/kvm_util.c
@@ -21,15 +21,6 @@
 #define KVM_UTIL_PGS_PER_HUGEPG 512
 #define KVM_UTIL_MIN_PFN       2
 
-/* Aligns x up to the next multiple of size. Size must be a power of 2. */
-static void *align(void *x, size_t size)
-{
-       size_t mask = size - 1;
-       TEST_ASSERT(size != 0 && !(size & (size - 1)),
-                   "size not a power of 2: %lu", size);
-       return (void *) (((size_t) x + mask) & ~mask);
-}
-
 /*
  * Capability
  *
@@ -757,7 +748,7 @@ void vm_userspace_mem_region_add(struct kvm_vm *vm,
                    region->mmap_start, errno);
 
        /* Align host address */
-       region->host_mem = align(region->mmap_start, alignment);
+       region->host_mem = align_ptr(region->mmap_start, alignment);
 
        /* As needed perform madvise */
        if (src_type == VM_MEM_SRC_ANONYMOUS || src_type == 
VM_MEM_SRC_ANONYMOUS_THP) {
-- 
2.30.0.478.g8a0d178c01-goog

Reply via email to