Skip to contentSkip to conversation

selftest/vm: Use correct PAGE_SHIFT value for ppc64

PATCH In review Aneesh Kumar K.V 2 participants 6 messages Feb 11, 2022

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 selftest/vm: Use correct PAGE_SHIFT value for ppc64

Keep it simple by using a #define and limiting hugepage size to 2M. This keeps the test simpler instead of dynamically finding the page size and huge page size.

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");
---
 tools/testing/selftests/vm/ksm_tests.c        | 9 ++++++++-
 tools/testing/selftests/vm/transhuge-stress.c | 9 ++++++++-
 2 files changed, 16 insertions(+), 2 deletions(-)
tools/testing/selftests/vm/ksm_tests.c +8 -1
index 1436e1a9a3d3..cae72872152b 100644
@@ -22,7 +22,14 @@
 #define KSM_MERGE_ACROSS_NODES_DEFAULT true
 #define MB (1ul << 20)
 
-#define PAGE_SHIFT 12
+#ifdef __powerpc64__
+#define PAGE_SHIFT	16
+#else
+#define PAGE_SHIFT	12
+#endif
+/*
+ * On ppc64 this will only work with radix 2M hugepage size
+ */
 #define HPAGE_SHIFT 21
 
 #define PAGE_SIZE (1 << PAGE_SHIFT)
tools/testing/selftests/vm/transhuge-stress.c +8 -1
index 5e4c036f6ad3..b1f8d98355c5 100644
@@ -16,7 +16,14 @@
 #include <string.h>
 #include <sys/mman.h>
 
-#define PAGE_SHIFT 12
+#ifdef __powerpc64__
+#define PAGE_SHIFT	16
+#else
+#define PAGE_SHIFT	12
+#endif
+/*
+ * On ppc64 this will only work with radix 2M hugepage size
+ */
 #define HPAGE_SHIFT 21
 
 #define PAGE_SIZE (1 << PAGE_SHIFT)
Signature · 1 line
2.34.1
#1 lore raw

PATCH v2 2/2 selftest/vm: Add util.h and and move helper functions there

Avoid code duplication by adding util.h

---
 tools/testing/selftests/vm/ksm_tests.c        | 45 +---------------
 tools/testing/selftests/vm/transhuge-stress.c | 49 ++---------------
 tools/testing/selftests/vm/util.h             | 54 +++++++++++++++++++
 3 files changed, 58 insertions(+), 90 deletions(-)
 create mode 100644 tools/testing/selftests/vm/util.h
tools/testing/selftests/vm/ksm_tests.c +1 -44
index cae72872152b..fd85f15869d1 100644
@@ -12,6 +12,7 @@
 
 #include "../kselftest.h"
 #include "../../../../include/vdso/time64.h"
+#include "util.h"
 
 #define KSM_SYSFS_PATH "/sys/kernel/mm/ksm/"
 #define KSM_FP(s) (KSM_SYSFS_PATH s)
@@ -22,22 +23,6 @@
 #define KSM_MERGE_ACROSS_NODES_DEFAULT true
 #define MB (1ul << 20)
 
-#ifdef __powerpc64__
-#define PAGE_SHIFT	16
-#else
-#define PAGE_SHIFT	12
-#endif
-/*
- * On ppc64 this will only work with radix 2M hugepage size
- */
-#define HPAGE_SHIFT 21
-
-#define PAGE_SIZE (1 << PAGE_SHIFT)
-#define HPAGE_SIZE (1 << HPAGE_SHIFT)
-
-#define PAGEMAP_PRESENT(ent)	(((ent) & (1ull << 63)) != 0)
-#define PAGEMAP_PFN(ent)	((ent) & ((1ull << 55) - 1))
-
 struct ksm_sysfs {
 	unsigned long max_page_sharing;
 	unsigned long merge_across_nodes;
@@ -463,34 +448,6 @@ static int check_ksm_numa_merge(int mapping, int prot, int timeout, bool merge_a
 	return KSFT_FAIL;
 }
 
-int64_t allocate_transhuge(void *ptr, int pagemap_fd)
-{
-	uint64_t ent[2];
-
-	/* drop pmd */
-	if (mmap(ptr, HPAGE_SIZE, PROT_READ | PROT_WRITE,
-				MAP_FIXED | MAP_ANONYMOUS |
-				MAP_NORESERVE | MAP_PRIVATE, -1, 0) != ptr)
-		errx(2, "mmap transhuge");
-
-	if (madvise(ptr, HPAGE_SIZE, MADV_HUGEPAGE))
-		err(2, "MADV_HUGEPAGE");
-
-	/* allocate transparent huge page */
-	*(volatile void **)ptr = ptr;
-
-	if (pread(pagemap_fd, ent, sizeof(ent),
-			(uintptr_t)ptr >> (PAGE_SHIFT - 3)) != sizeof(ent))
-		err(2, "read pagemap");
-
-	if (PAGEMAP_PRESENT(ent[0]) && PAGEMAP_PRESENT(ent[1]) &&
-	    PAGEMAP_PFN(ent[0]) + 1 == PAGEMAP_PFN(ent[1]) &&
-	    !(PAGEMAP_PFN(ent[0]) & ((1 << (HPAGE_SHIFT - PAGE_SHIFT)) - 1)))
-		return PAGEMAP_PFN(ent[0]);
-
-	return -1;
-}
-
 static int ksm_merge_hugepages_time(int mapping, int prot, int timeout, size_t map_size)
 {
 	void *map_ptr, *map_ptr_orig;
tools/testing/selftests/vm/transhuge-stress.c +3 -46

67 lines not on this page · raw message

tools/testing/selftests/vm/util.h new file +54 -0

54 lines not on this page · raw message

Signature · 1 line
2.34.1
#2 lore raw
In reply to Aneesh Kumar K.V
Quoted 23 lines · Aneesh Kumar K.V

Keep it simple by using a #define and limiting hugepage size to 2M. This keeps the test simpler instead of dynamically finding the page size and huge page size.

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");
---
 tools/testing/selftests/vm/ksm_tests.c        | 9 ++++++++-
 tools/testing/selftests/vm/transhuge-stress.c | 9 ++++++++-
 2 files changed, 16 insertions(+), 2 deletions(-)
tools/testing/selftests/vm/ksm_tests.c +0 -0
index 1436e1a9a3d3..cae72872152b 100644

tools/testing/selftests/vm/ksm_tests.c line 29

Quoted 9 lines
tools/testing/selftests/vm/ksm_tests.c +5 -1
@@ -22,7 +22,14 @@
 #define KSM_MERGE_ACROSS_NODES_DEFAULT true
 #define MB (1ul << 20)
 
-#define PAGE_SHIFT 12
+#ifdef __powerpc64__
+#define PAGE_SHIFT	16
+#else
+#define PAGE_SHIFT	12
+#endif

Page size can be other than 4096 for other configurations as well. And even on ppc64 it's not necessarily 64k.

Ideally page size in selftests/vm should be sysconf(_SC_PAGESIZE)

Quoted 30 lines
tools/testing/selftests/vm/ksm_tests.c +3 -0
+/*
+ * On ppc64 this will only work with radix 2M hugepage size
+ */
 #define HPAGE_SHIFT 21
 
 #define PAGE_SIZE (1 << PAGE_SHIFT)
tools/testing/selftests/vm/transhuge-stress.c +8 -1
index 5e4c036f6ad3..b1f8d98355c5 100644
@@ -16,7 +16,14 @@
 #include <string.h>
 #include <sys/mman.h>
 
-#define PAGE_SHIFT 12
+#ifdef __powerpc64__
+#define PAGE_SHIFT	16
+#else
+#define PAGE_SHIFT	12
+#endif
+/*
+ * On ppc64 this will only work with radix 2M hugepage size
+ */
 #define HPAGE_SHIFT 21
 
 #define PAGE_SIZE (1 << PAGE_SHIFT)
Signature · 1 line
2.34.1
Signature · 2 lines
Sincerely yours,
Mike.
#3 lore raw
Quoted 27 lines · Mike Rapoport
Quoted 26 lines · Aneesh Kumar K.V

Keep it simple by using a #define and limiting hugepage size to 2M. This keeps the test simpler instead of dynamically finding the page size and huge page size.

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");
---
  tools/testing/selftests/vm/ksm_tests.c        | 9 ++++++++-
  tools/testing/selftests/vm/transhuge-stress.c | 9 ++++++++-
  2 files changed, 16 insertions(+), 2 deletions(-)
tools/testing/selftests/vm/ksm_tests.c +0 -0
index 1436e1a9a3d3..cae72872152b 100644
@@ -22,7 +22,14 @@
  #define KSM_MERGE_ACROSS_NODES_DEFAULT true
  #define MB (1ul << 20)
 
Quoted 10 lines

tools/testing/selftests/vm/ksm_tests.c line 29

Quoted 6 lines
tools/testing/selftests/vm/ksm_tests.c +5 -1
-#define PAGE_SHIFT 12
+#ifdef __powerpc64__
+#define PAGE_SHIFT	16
+#else
+#define PAGE_SHIFT	12
+#endif

Page size can be other than 4096 for other configurations as well. And even on ppc64 it's not necessarily 64k.

But most common test config is with 64K page size.

Quoted 1 line

Ideally page size in selftests/vm should be sysconf(_SC_PAGESIZE)

yes. As explained in commit message, the idea was to keep it simpler.

"Keep it simple by using a #define and limiting hugepage size to 2M. This keeps the test simpler instead of dynamically finding the page size and huge page size.

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

We can definitely look at updating multiple tests in selftest/vm to work with dynamic value of pagesize and huagepage size. But that can be outside this patch?

Quoted 32 lines
Quoted 31 lines
tools/testing/selftests/vm/ksm_tests.c +3 -0
+/*
+ * On ppc64 this will only work with radix 2M hugepage size
+ */
  #define HPAGE_SHIFT 21
 
  #define PAGE_SIZE (1 << PAGE_SHIFT)
tools/testing/selftests/vm/transhuge-stress.c +8 -1
index 5e4c036f6ad3..b1f8d98355c5 100644
@@ -16,7 +16,14 @@
  #include <string.h>
  #include <sys/mman.h>
 
-#define PAGE_SHIFT 12
+#ifdef __powerpc64__
+#define PAGE_SHIFT	16
+#else
+#define PAGE_SHIFT	12
+#endif
+/*
+ * On ppc64 this will only work with radix 2M hugepage size
+ */
  #define HPAGE_SHIFT 21
 
  #define PAGE_SIZE (1 << PAGE_SHIFT)
Signature · 1 line
2.34.1
#4 lore raw

Hi Aneesh,

Quoted 38 lines · Aneesh Kumar K V
Quoted 19 lines · Mike Rapoport
Quoted 18 lines · Aneesh Kumar K.V

Keep it simple by using a #define and limiting hugepage size to 2M. This keeps the test simpler instead of dynamically finding the page size and huge page size.

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");
---
  tools/testing/selftests/vm/ksm_tests.c        | 9 ++++++++-
  tools/testing/selftests/vm/transhuge-stress.c | 9 ++++++++-
  2 files changed, 16 insertions(+), 2 deletions(-)
Quoted 17 lines

tools/testing/selftests/vm/ksm_tests.c line 28

Quoted 13 lines
tools/testing/selftests/vm/ksm_tests.c +5 -1
index 1436e1a9a3d3..cae72872152b 100644
@@ -22,7 +22,14 @@
  #define KSM_MERGE_ACROSS_NODES_DEFAULT true
  #define MB (1ul << 20)
-#define PAGE_SHIFT 12
+#ifdef __powerpc64__
+#define PAGE_SHIFT	16
+#else
+#define PAGE_SHIFT	12
+#endif

Page size can be other than 4096 for other configurations as well. And even on ppc64 it's not necessarily 64k.

Quoted 6 lines

But most common test config is with 64K page size.

Quoted 1 line

Ideally page size in selftests/vm should be sysconf(_SC_PAGESIZE)

yes. As explained in commit message, the idea was to keep it simpler.

I think it's simple enough (compile tested on x86 only):

From 219577d87041f19f2c00dc7c23e0fd5aad8b02d5 Mon Sep 17 00:00:00 2001
From: Mike Rapoport <[email protected]>
Date: Fri, 11 Feb 2022 15:24:13 +0200
Subject: [PATCH] selftest/vm: add helpers to detect PAGE_SIZE and PAGE_SHIFT

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

Add helpers to detect PAGE_SIZE and PAGE_SHIFT dynamically.

---
 tools/testing/selftests/vm/gup_test.c         |  3 +-
 tools/testing/selftests/vm/ksm_tests.c        |  8 +----
 tools/testing/selftests/vm/transhuge-stress.c |  9 ++----
 tools/testing/selftests/vm/util.h             | 29 +++++++++++++++++++
 4 files changed, 34 insertions(+), 15 deletions(-)
 create mode 100644 tools/testing/selftests/vm/util.h
tools/testing/selftests/vm/gup_test.c +2 -1
index fe043f67798b..cda837a14736 100644
@@ -10,8 +10,9 @@
 #include <assert.h>
 #include "../../../../mm/gup_test.h"
 
+#include "util.h"
+
 #define MB (1UL << 20)
-#define PAGE_SIZE sysconf(_SC_PAGESIZE)
 
 /* Just the flags we need, copied from mm.h: */
 #define FOLL_WRITE	0x01	/* check pte is writable */
tools/testing/selftests/vm/ksm_tests.c +1 -7
index cae72872152b..7faafd24446f 100644
@@ -12,6 +12,7 @@
 
 #include "../kselftest.h"
 #include "../../../../include/vdso/time64.h"
+#include "util.h"
 
 #define KSM_SYSFS_PATH "/sys/kernel/mm/ksm/"
 #define KSM_FP(s) (KSM_SYSFS_PATH s)
@@ -22,17 +23,10 @@
 #define KSM_MERGE_ACROSS_NODES_DEFAULT true
 #define MB (1ul << 20)
 
-#ifdef __powerpc64__
-#define PAGE_SHIFT	16
-#else
-#define PAGE_SHIFT	12
-#endif
 /*
  * On ppc64 this will only work with radix 2M hugepage size
  */
 #define HPAGE_SHIFT 21
-
-#define PAGE_SIZE (1 << PAGE_SHIFT)
 #define HPAGE_SIZE (1 << HPAGE_SHIFT)
 
 #define PAGEMAP_PRESENT(ent)	(((ent) & (1ull << 63)) != 0)
tools/testing/selftests/vm/transhuge-stress.c +2 -7
index b1f8d98355c5..baf90a745d28 100644
@@ -16,17 +16,12 @@
 #include <string.h>
 #include <sys/mman.h>
 
-#ifdef __powerpc64__
-#define PAGE_SHIFT	16
-#else
-#define PAGE_SHIFT	12
-#endif
+#include "util.h"
+
 /*
  * On ppc64 this will only work with radix 2M hugepage size
  */
 #define HPAGE_SHIFT 21
-
-#define PAGE_SIZE (1 << PAGE_SHIFT)
 #define HPAGE_SIZE (1 << HPAGE_SHIFT)
 
 #define PAGEMAP_PRESENT(ent)	(((ent) & (1ull << 63)) != 0)
tools/testing/selftests/vm/util.h new file +29 -0
new file mode 100644
index 000000000000..1c85d7583bac
@@ -0,0 +1,29 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+#ifndef __KSELFTEST_VM_UTIL_H
+#define __KSELFTEST_VM_UTIL_H
+
+#include <string.h> /* ffsl() */
+#include <unistd.h> /* _SC_PAGESIZE */
+
+static unsigned __page_size;
+static unsigned __page_shift;
+
+static inline unsigned page_size(void)
+{
+	if (!__page_size)
+		__page_size = sysconf(_SC_PAGESIZE);
+	return __page_size;
+}
+
+static inline unsigned page_shift(void)
+{
+	if (!__page_shift)
+		__page_shift = (ffsl(page_size()) - 1);
+	return __page_shift;
+}
+
+#define PAGE_SHIFT	(page_shift())
+#define PAGE_SIZE	(page_size())
+
+#endif /* __KSELFTEST_VM_UTIL_H */
Signature · 5 lines
2.34.1

-- 
Sincerely yours,
Mike.
#5 lore raw
Quoted 163 lines · Mike Rapoport

Hi Aneesh,

Quoted 38 lines · Aneesh Kumar K V
Quoted 19 lines · Mike Rapoport
Quoted 18 lines · Aneesh Kumar K.V

Keep it simple by using a #define and limiting hugepage size to 2M. This keeps the test simpler instead of dynamically finding the page size and huge page size.

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");
---
   tools/testing/selftests/vm/ksm_tests.c        | 9 ++++++++-
   tools/testing/selftests/vm/transhuge-stress.c | 9 ++++++++-
   2 files changed, 16 insertions(+), 2 deletions(-)
Quoted 17 lines

tools/testing/selftests/vm/ksm_tests.c line 28

Quoted 13 lines
tools/testing/selftests/vm/ksm_tests.c +5 -1
index 1436e1a9a3d3..cae72872152b 100644
@@ -22,7 +22,14 @@
   #define KSM_MERGE_ACROSS_NODES_DEFAULT true
   #define MB (1ul << 20)
-#define PAGE_SHIFT 12
+#ifdef __powerpc64__
+#define PAGE_SHIFT	16
+#else
+#define PAGE_SHIFT	12
+#endif

Page size can be other than 4096 for other configurations as well. And even on ppc64 it's not necessarily 64k.

Quoted 6 lines

But most common test config is with 64K page size.

Quoted 1 line

Ideally page size in selftests/vm should be sysconf(_SC_PAGESIZE)

yes. As explained in commit message, the idea was to keep it simpler.

I think it's simple enough (compile tested on x86 only):

From 219577d87041f19f2c00dc7c23e0fd5aad8b02d5 Mon Sep 17 00:00:00 2001
From: Mike Rapoport <[email protected]>
Date: Fri, 11 Feb 2022 15:24:13 +0200
Subject: [PATCH] selftest/vm: add helpers to detect PAGE_SIZE and PAGE_SHIFT

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

Add helpers to detect PAGE_SIZE and PAGE_SHIFT dynamically.

---
  tools/testing/selftests/vm/gup_test.c         |  3 +-
  tools/testing/selftests/vm/ksm_tests.c        |  8 +----
  tools/testing/selftests/vm/transhuge-stress.c |  9 ++----
  tools/testing/selftests/vm/util.h             | 29 +++++++++++++++++++
  4 files changed, 34 insertions(+), 15 deletions(-)

create mode 100644 tools/testing/selftests/vm/util.h

tools/testing/selftests/vm/gup_test.c +2 -1
index fe043f67798b..cda837a14736 100644
@@ -10,8 +10,9 @@
  #include <assert.h>
  #include "../../../../mm/gup_test.h"
 
+#include "util.h"
+
  #define MB (1UL << 20)
-#define PAGE_SIZE sysconf(_SC_PAGESIZE)
 
  /* Just the flags we need, copied from mm.h: */
  #define FOLL_WRITE	0x01	/* check pte is writable */
tools/testing/selftests/vm/ksm_tests.c +1 -7
index cae72872152b..7faafd24446f 100644
@@ -12,6 +12,7 @@
 
  #include "../kselftest.h"
  #include "../../../../include/vdso/time64.h"
+#include "util.h"
 
  #define KSM_SYSFS_PATH "/sys/kernel/mm/ksm/"
  #define KSM_FP(s) (KSM_SYSFS_PATH s)
@@ -22,17 +23,10 @@
  #define KSM_MERGE_ACROSS_NODES_DEFAULT true
  #define MB (1ul << 20)
 
-#ifdef __powerpc64__
-#define PAGE_SHIFT	16
-#else
-#define PAGE_SHIFT	12
-#endif
  /*
   * On ppc64 this will only work with radix 2M hugepage size
   */
  #define HPAGE_SHIFT 21
-
-#define PAGE_SIZE (1 << PAGE_SHIFT)
  #define HPAGE_SIZE (1 << HPAGE_SHIFT)
 
  #define PAGEMAP_PRESENT(ent)	(((ent) & (1ull << 63)) != 0)
tools/testing/selftests/vm/transhuge-stress.c +2 -7
index b1f8d98355c5..baf90a745d28 100644
@@ -16,17 +16,12 @@
  #include <string.h>
  #include <sys/mman.h>
 
-#ifdef __powerpc64__
-#define PAGE_SHIFT	16
-#else
-#define PAGE_SHIFT	12
-#endif
+#include "util.h"
+
  /*
   * On ppc64 this will only work with radix 2M hugepage size
   */
  #define HPAGE_SHIFT 21
-
-#define PAGE_SIZE (1 << PAGE_SHIFT)
  #define HPAGE_SIZE (1 << HPAGE_SHIFT)
 
  #define PAGEMAP_PRESENT(ent)	(((ent) & (1ull << 63)) != 0)
tools/testing/selftests/vm/util.h new file +19 -0
new file mode 100644
index 000000000000..1c85d7583bac
@@ -0,0 +1,29 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+#ifndef __KSELFTEST_VM_UTIL_H
+#define __KSELFTEST_VM_UTIL_H
+
+#include <string.h> /* ffsl() */
+#include <unistd.h> /* _SC_PAGESIZE */
+
+static unsigned __page_size;
+static unsigned __page_shift;
+
+static inline unsigned page_size(void)
+{
+	if (!__page_size)
+		__page_size = sysconf(_SC_PAGESIZE);
+	return __page_size;
+}
+
+static inline unsigned page_shift(void)

tools/testing/selftests/vm/util.h line 29

Quoted 10 lines
tools/testing/selftests/vm/util.h new file +10 -0
+{
+	if (!__page_shift)
+		__page_shift = (ffsl(page_size()) - 1);
+	return __page_shift;
+}
+
+#define PAGE_SHIFT	(page_shift())
+#define PAGE_SIZE	(page_size())
+
+#endif /* __KSELFTEST_VM_UTIL_H */

This is on top of patch 1 I sent i guess. Should I add it to the series because v2 also consolidate allocate_transhuge()? Or do you want to sent it as an independent patch?

-aneesh