Skip to content

Commit 00bcfcd

Browse files
DonetTom1akpm00
authored andcommitted
selftests: mm: hugepage-vmemmap fails on 64K page size systems
The kernel sefltest mm/hugepage-vmemmap fails on architectures which has different page size other than 4K. In hugepage-vmemmap page size used is 4k so the pfn calculation will go wrong on systems which has different page size .The length of MAP_HUGETLB memory must be hugepage aligned but in hugepage-vmemmap map length is 2M so this will not get aligned if the system has differnet hugepage size. Added psize() to get the page size and default_huge_page_size() to get the default hugepage size at run time, hugepage-vmemmap test pass on powerpc with 64K page size and x86 with 4K page size. Result on powerpc without patch (page size 64K) *# ./hugepage-vmemmap Returned address is 0x7effff000000 whose pfn is 0 Head page flags (100000000) is invalid check_page_flags: Invalid argument *# Result on powerpc with patch (page size 64K) *# ./hugepage-vmemmap Returned address is 0x7effff000000 whose pfn is 600 *# Result on x86 with patch (page size 4K) *# ./hugepage-vmemmap Returned address is 0x7fc7c2c00000 whose pfn is 1dac00 *# Link: https://lkml.kernel.org/r/3b3a3ae37ba21218481c482a872bbf7526031600.1704865754.git.donettom@linux.vnet.ibm.com Fixes: b147c89 ("selftests: vm: add a hugetlb test case") Signed-off-by: Donet Tom <donettom@linux.vnet.ibm.com> Reported-by: Geetika Moolchandani <geetika@linux.ibm.com> Tested-by: Geetika Moolchandani <geetika@linux.ibm.com> Acked-by: Muchun Song <muchun.song@linux.dev> Cc: <stable@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent 1168413 commit 00bcfcd

1 file changed

Lines changed: 18 additions & 11 deletions

File tree

tools/testing/selftests/mm/hugepage-vmemmap.c

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@
1010
#include <unistd.h>
1111
#include <sys/mman.h>
1212
#include <fcntl.h>
13-
14-
#define MAP_LENGTH (2UL * 1024 * 1024)
15-
16-
#define PAGE_SIZE 4096
13+
#include "vm_util.h"
1714

1815
#define PAGE_COMPOUND_HEAD (1UL << 15)
1916
#define PAGE_COMPOUND_TAIL (1UL << 16)
@@ -39,6 +36,9 @@
3936
#define MAP_FLAGS (MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB)
4037
#endif
4138

39+
static size_t pagesize;
40+
static size_t maplength;
41+
4242
static void write_bytes(char *addr, size_t length)
4343
{
4444
unsigned long i;
@@ -56,7 +56,7 @@ static unsigned long virt_to_pfn(void *addr)
5656
if (fd < 0)
5757
return -1UL;
5858

59-
lseek(fd, (unsigned long)addr / PAGE_SIZE * sizeof(pagemap), SEEK_SET);
59+
lseek(fd, (unsigned long)addr / pagesize * sizeof(pagemap), SEEK_SET);
6060
read(fd, &pagemap, sizeof(pagemap));
6161
close(fd);
6262

@@ -86,7 +86,7 @@ static int check_page_flags(unsigned long pfn)
8686
* this also verifies kernel has correctly set the fake page_head to tail
8787
* while hugetlb_free_vmemmap is enabled.
8888
*/
89-
for (i = 1; i < MAP_LENGTH / PAGE_SIZE; i++) {
89+
for (i = 1; i < maplength / pagesize; i++) {
9090
read(fd, &pageflags, sizeof(pageflags));
9191
if ((pageflags & TAIL_PAGE_FLAGS) != TAIL_PAGE_FLAGS ||
9292
(pageflags & HEAD_PAGE_FLAGS) == HEAD_PAGE_FLAGS) {
@@ -106,32 +106,39 @@ int main(int argc, char **argv)
106106
void *addr;
107107
unsigned long pfn;
108108

109-
addr = mmap(MAP_ADDR, MAP_LENGTH, PROT_READ | PROT_WRITE, MAP_FLAGS, -1, 0);
109+
pagesize = psize();
110+
maplength = default_huge_page_size();
111+
if (!maplength) {
112+
printf("Unable to determine huge page size\n");
113+
exit(1);
114+
}
115+
116+
addr = mmap(MAP_ADDR, maplength, PROT_READ | PROT_WRITE, MAP_FLAGS, -1, 0);
110117
if (addr == MAP_FAILED) {
111118
perror("mmap");
112119
exit(1);
113120
}
114121

115122
/* Trigger allocation of HugeTLB page. */
116-
write_bytes(addr, MAP_LENGTH);
123+
write_bytes(addr, maplength);
117124

118125
pfn = virt_to_pfn(addr);
119126
if (pfn == -1UL) {
120-
munmap(addr, MAP_LENGTH);
127+
munmap(addr, maplength);
121128
perror("virt_to_pfn");
122129
exit(1);
123130
}
124131

125132
printf("Returned address is %p whose pfn is %lx\n", addr, pfn);
126133

127134
if (check_page_flags(pfn) < 0) {
128-
munmap(addr, MAP_LENGTH);
135+
munmap(addr, maplength);
129136
perror("check_page_flags");
130137
exit(1);
131138
}
132139

133140
/* munmap() length of MAP_HUGETLB memory must be hugepage aligned */
134-
if (munmap(addr, MAP_LENGTH)) {
141+
if (munmap(addr, maplength)) {
135142
perror("munmap");
136143
exit(1);
137144
}

0 commit comments

Comments
 (0)