Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftest/vm: add helpers to detect PAGE_SIZE and PAGE_SHIFT

PAGE_SIZE is not 4096 in many configurations, particularly ppc64 uses 64K
pages in majority of cases.

Add helpers to detect PAGE_SIZE and PAGE_SHIFT dynamically.

Without this tests are broken w.r.t reading /proc/self/pagemap

if (pread(pagemap_fd, ent, sizeof(ent),
(uintptr_t)ptr >> (PAGE_SHIFT - 3)) != sizeof(ent))
err(2, "read pagemap");

Link: https://lkml.kernel.org/r/20220307054355.149820-2-aneesh.kumar@linux.ibm.com
Signed-off-by: Mike Rapoport <rppt@linux.ibm.com>
Signed-off-by: Aneesh Kumar K.V <aneesh.kumar@linux.ibm.com>
Cc: Shuah Khan <shuah@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by

Mike Rapoport and committed by
Linus Torvalds
6f6a841f 90647d9d

+26 -4
+2 -1
tools/testing/selftests/vm/gup_test.c
··· 10 10 #include <assert.h> 11 11 #include "../../../../mm/gup_test.h" 12 12 13 + #include "util.h" 14 + 13 15 #define MB (1UL << 20) 14 - #define PAGE_SIZE sysconf(_SC_PAGESIZE) 15 16 16 17 /* Just the flags we need, copied from mm.h: */ 17 18 #define FOLL_WRITE 0x01 /* check pte is writable */
+24 -3
tools/testing/selftests/vm/util.h
··· 6 6 #include <stdint.h> 7 7 #include <sys/mman.h> 8 8 #include <err.h> 9 + #include <string.h> /* ffsl() */ 10 + #include <unistd.h> /* _SC_PAGESIZE */ 9 11 10 - #define PAGE_SHIFT 12 11 - #define HPAGE_SHIFT 21 12 + static unsigned int __page_size; 13 + static unsigned int __page_shift; 12 14 13 - #define PAGE_SIZE (1 << PAGE_SHIFT) 15 + static inline unsigned int page_size(void) 16 + { 17 + if (!__page_size) 18 + __page_size = sysconf(_SC_PAGESIZE); 19 + return __page_size; 20 + } 21 + 22 + static inline unsigned int page_shift(void) 23 + { 24 + if (!__page_shift) 25 + __page_shift = (ffsl(page_size()) - 1); 26 + return __page_shift; 27 + } 28 + 29 + #define PAGE_SHIFT (page_shift()) 30 + #define PAGE_SIZE (page_size()) 31 + /* 32 + * On ppc64 this will only work with radix 2M hugepage size 33 + */ 34 + #define HPAGE_SHIFT 21 14 35 #define HPAGE_SIZE (1 << HPAGE_SHIFT) 15 36 16 37 #define PAGEMAP_PRESENT(ent) (((ent) & (1ull << 63)) != 0)