[PATCH v4 3/3] kselftest: mm: introduce size_to_shift() helper

From: Yeoreum Yun

Date: Mon Sep 21 2026 - 06:43:40 EST


Introduce size_to_shift() for future users that need to
obtain shift of a huge-page which is other than pmd_psize().

Suggested-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>
Reviewed-by: Lorenzo Stoakes (ARM) <ljs@xxxxxxxxxx>
Signed-off-by: Yeoreum Yun <yeoreum.yun@xxxxxxx>
---
tools/testing/selftests/mm/vm_util.c | 2 +-
tools/testing/selftests/mm/vm_util.h | 12 ++++++++++++
2 files changed, 13 insertions(+), 1 deletion(-)

diff --git a/tools/testing/selftests/mm/vm_util.c b/tools/testing/selftests/mm/vm_util.c
index 8f80e73dc22a5..970671058397d 100644
--- a/tools/testing/selftests/mm/vm_util.c
+++ b/tools/testing/selftests/mm/vm_util.c
@@ -194,7 +194,7 @@ uint64_t pmd_pshift(void)
if (__pmd_psize != -1ull)
__pmd_psize = pmd_psize();

- __pmd_pshift = (__pmd_psize > 0) ? ffsl(__pmd_psize) - 1 : 0;
+ __pmd_pshift = size_to_shift(__pmd_psize);

return __pmd_pshift;
}
diff --git a/tools/testing/selftests/mm/vm_util.h b/tools/testing/selftests/mm/vm_util.h
index 71af4804f8f24..6e37c90f3c3a5 100644
--- a/tools/testing/selftests/mm/vm_util.h
+++ b/tools/testing/selftests/mm/vm_util.h
@@ -148,6 +148,18 @@ static inline int sz2ord(size_t size, size_t pagesize)
return __builtin_ctzll(size / pagesize);
}

+static inline uint64_t size_to_shift(uint64_t size)
+{
+ /*
+ * The function returns the corresponding shift only when the size
+ * is a power of two; otherwise, it returns 0.
+ */
+ if (!size || (size & (size - 1)))
+ return 0;
+
+ return ffsl(size) - 1;
+}
+
void *sys_mremap(void *old_address, unsigned long old_size,
unsigned long new_size, int flags, void *new_address);


--
2.43.0