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

Configure Feed

Select the types of activity you want to include in your feed.

selftests/mm: hmm-tests: don't hardcode THP size to 2MB

Several HMM tests hardcode TWOMEG as the THP size. This is wrong on
architectures where the PMD size is not 2MB such as arm64 with 64K base
pages where THP is 512MB. Fix this by using read_pmd_pagesize() from
vm_util instead.

While here also replace the custom file_read_ulong() helper used to
parse the default hugetlbfs page size from /proc/meminfo with the
existing default_huge_page_size() from vm_util.

Link: https://lore.kernel.org/20260331063445.3551404-3-apopple@nvidia.com
Link: https://lore.kernel.org/linux-mm/8bd0396a-8997-4d2e-a13f-5aac033083d7@linux.dev/
Fixes: fee9f6d1b8df ("mm/hmm/test: add selftests for HMM")
Fixes: 519071529d2a ("selftests/mm/hmm-tests: new tests for zone device THP migration")
Signed-off-by: Alistair Popple <apopple@nvidia.com>
Reported-by: Zenghui Yu <zenghui.yu@linux.dev>
Closes: https://lore.kernel.org/linux-mm/8bd0396a-8997-4d2e-a13f-5aac033083d7@linux.dev/
Reviewed-by: Balbir Singh <balbirs@nvidia.com>
Cc: Matthew Brost <matthew.brost@intel.com>
Cc: David Hildenbrand <david@kernel.org>
Cc: Jason Gunthorpe <jgg@ziepe.ca>
Cc: Leon Romanovsky <leon@kernel.org>
Cc: Liam Howlett <liam.howlett@oracle.com>
Cc: Lorenzo Stoakes (Oracle) <ljs@kernel.org>
Cc: Michal Hocko <mhocko@suse.com>
Cc: Mike Rapoport <rppt@kernel.org>
Cc: Suren Baghdasaryan <surenb@google.com>
Cc: <stable@vger,kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Alistair Popple and committed by
Andrew Morton
f9d7975c 744dd977

+16 -67
+16 -67
tools/testing/selftests/mm/hmm-tests.c
··· 34 34 */ 35 35 #include <lib/test_hmm_uapi.h> 36 36 #include <mm/gup_test.h> 37 + #include <mm/vm_util.h> 37 38 38 39 struct hmm_buffer { 39 40 void *ptr; ··· 549 548 550 549 for (migrate = 0; migrate < 2; ++migrate) { 551 550 for (use_thp = 0; use_thp < 2; ++use_thp) { 552 - npages = ALIGN(use_thp ? TWOMEG : HMM_BUFFER_SIZE, 551 + npages = ALIGN(use_thp ? read_pmd_pagesize() : HMM_BUFFER_SIZE, 553 552 self->page_size) >> self->page_shift; 554 553 ASSERT_NE(npages, 0); 555 554 size = npages << self->page_shift; ··· 729 728 int *ptr; 730 729 int ret; 731 730 732 - size = 2 * TWOMEG; 731 + size = 2 * read_pmd_pagesize(); 733 732 734 733 buffer = malloc(sizeof(*buffer)); 735 734 ASSERT_NE(buffer, NULL); ··· 745 744 buffer->fd, 0); 746 745 ASSERT_NE(buffer->ptr, MAP_FAILED); 747 746 748 - size = TWOMEG; 747 + size /= 2; 749 748 npages = size >> self->page_shift; 750 749 map = (void *)ALIGN((uintptr_t)buffer->ptr, size); 751 750 ret = madvise(map, size, MADV_HUGEPAGE); ··· 772 771 } 773 772 774 773 /* 775 - * Read numeric data from raw and tagged kernel status files. Used to read 776 - * /proc and /sys data (without a tag) and from /proc/meminfo (with a tag). 777 - */ 778 - static long file_read_ulong(char *file, const char *tag) 779 - { 780 - int fd; 781 - char buf[2048]; 782 - int len; 783 - char *p, *q; 784 - long val; 785 - 786 - fd = open(file, O_RDONLY); 787 - if (fd < 0) { 788 - /* Error opening the file */ 789 - return -1; 790 - } 791 - 792 - len = read(fd, buf, sizeof(buf)); 793 - close(fd); 794 - if (len < 0) { 795 - /* Error in reading the file */ 796 - return -1; 797 - } 798 - if (len == sizeof(buf)) { 799 - /* Error file is too large */ 800 - return -1; 801 - } 802 - buf[len] = '\0'; 803 - 804 - /* Search for a tag if provided */ 805 - if (tag) { 806 - p = strstr(buf, tag); 807 - if (!p) 808 - return -1; /* looks like the line we want isn't there */ 809 - p += strlen(tag); 810 - } else 811 - p = buf; 812 - 813 - val = strtol(p, &q, 0); 814 - if (*q != ' ') { 815 - /* Error parsing the file */ 816 - return -1; 817 - } 818 - 819 - return val; 820 - } 821 - 822 - /* 823 774 * Write huge TLBFS page. 824 775 */ 825 776 TEST_F(hmm, anon_write_hugetlbfs) ··· 779 826 struct hmm_buffer *buffer; 780 827 unsigned long npages; 781 828 unsigned long size; 782 - unsigned long default_hsize; 829 + unsigned long default_hsize = default_huge_page_size(); 783 830 unsigned long i; 784 831 int *ptr; 785 832 int ret; 786 833 787 - default_hsize = file_read_ulong("/proc/meminfo", "Hugepagesize:"); 788 - if (default_hsize < 0 || default_hsize*1024 < default_hsize) 834 + if (!default_hsize) 789 835 SKIP(return, "Huge page size could not be determined"); 790 - default_hsize = default_hsize*1024; /* KB to B */ 791 836 792 837 size = ALIGN(TWOMEG, default_hsize); 793 838 npages = size >> self->page_shift; ··· 1557 1606 struct hmm_buffer *buffer; 1558 1607 unsigned long npages; 1559 1608 unsigned long size; 1560 - unsigned long default_hsize; 1609 + unsigned long default_hsize = default_huge_page_size(); 1561 1610 int *ptr; 1562 1611 unsigned char *m; 1563 1612 int ret; ··· 1565 1614 1566 1615 /* Skip test if we can't allocate a hugetlbfs page. */ 1567 1616 1568 - default_hsize = file_read_ulong("/proc/meminfo", "Hugepagesize:"); 1569 - if (default_hsize < 0 || default_hsize*1024 < default_hsize) 1617 + if (!default_hsize) 1570 1618 SKIP(return, "Huge page size could not be determined"); 1571 - default_hsize = default_hsize*1024; /* KB to B */ 1572 1619 1573 1620 size = ALIGN(TWOMEG, default_hsize); 1574 1621 npages = size >> self->page_shift; ··· 2055 2106 int *ptr; 2056 2107 int ret; 2057 2108 2058 - size = TWOMEG; 2109 + size = read_pmd_pagesize(); 2059 2110 2060 2111 buffer = malloc(sizeof(*buffer)); 2061 2112 ASSERT_NE(buffer, NULL); ··· 2107 2158 int ret; 2108 2159 int val; 2109 2160 2110 - size = TWOMEG; 2161 + size = read_pmd_pagesize(); 2111 2162 2112 2163 buffer = malloc(sizeof(*buffer)); 2113 2164 ASSERT_NE(buffer, NULL); ··· 2170 2221 int *ptr; 2171 2222 int ret; 2172 2223 2173 - size = TWOMEG; 2224 + size = read_pmd_pagesize(); 2174 2225 2175 2226 buffer = malloc(sizeof(*buffer)); 2176 2227 ASSERT_NE(buffer, NULL); ··· 2229 2280 int *ptr; 2230 2281 int ret; 2231 2282 2232 - size = TWOMEG; 2283 + size = read_pmd_pagesize(); 2233 2284 2234 2285 buffer = malloc(sizeof(*buffer)); 2235 2286 ASSERT_NE(buffer, NULL); ··· 2281 2332 { 2282 2333 struct hmm_buffer *buffer; 2283 2334 unsigned long npages; 2284 - unsigned long size = TWOMEG; 2335 + unsigned long size = read_pmd_pagesize(); 2285 2336 unsigned long i; 2286 2337 void *old_ptr; 2287 2338 void *map; ··· 2347 2398 { 2348 2399 struct hmm_buffer *buffer; 2349 2400 unsigned long npages; 2350 - unsigned long size = TWOMEG; 2401 + unsigned long size = read_pmd_pagesize(); 2351 2402 unsigned long i; 2352 2403 void *old_ptr, *new_ptr = NULL; 2353 2404 void *map; ··· 2447 2498 int *ptr; 2448 2499 int ret; 2449 2500 2450 - size = TWOMEG; 2501 + size = read_pmd_pagesize(); 2451 2502 2452 2503 buffer = malloc(sizeof(*buffer)); 2453 2504 ASSERT_NE(buffer, NULL); ··· 2542 2593 int *ptr; 2543 2594 int ret; 2544 2595 2545 - size = TWOMEG; 2596 + size = read_pmd_pagesize(); 2546 2597 2547 2598 buffer = malloc(sizeof(*buffer)); 2548 2599 ASSERT_NE(buffer, NULL);