Skip to content

Commit 57bcb57

Browse files
sandip4ntorvalds
authored andcommitted
selftests: vm: pkeys: use the correct huge page size
The huge page size can vary across architectures. This will ensure that the correct huge page size is used when accessing the hugetlb controls under sysfs. Instead of using a hardcoded page size (i.e. 2MB), this now uses the HPAGE_SIZE macro which is arch-specific. Signed-off-by: Sandipan Das <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Acked-by: Dave Hansen <[email protected]> Cc: "Desnes A. Nunes do Rosario" <[email protected]> Cc: Florian Weimer <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: Ram Pai <[email protected]> Cc: Thiago Jung Bauermann <[email protected]> Cc: "Aneesh Kumar K.V" <[email protected]> Cc: Michael Ellerman <[email protected]> Cc: Michal Hocko <[email protected]> Cc: Michal Suchanek <[email protected]> Cc: Shuah Khan <[email protected]> Link: http://lkml.kernel.org/r/66882a5d6e45c73c3a52bc4aef9754e48afa4f88.1585646528.git.sandipan@linux.ibm.com Signed-off-by: Linus Torvalds <[email protected]>
1 parent 6e37326 commit 57bcb57

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

tools/testing/selftests/vm/protection_keys.c

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -739,12 +739,15 @@ void *malloc_pkey_anon_huge(long size, int prot, u16 pkey)
739739
}
740740

741741
int hugetlb_setup_ok;
742+
#define SYSFS_FMT_NR_HUGE_PAGES "/sys/kernel/mm/hugepages/hugepages-%ldkB/nr_hugepages"
742743
#define GET_NR_HUGE_PAGES 10
743744
void setup_hugetlbfs(void)
744745
{
745746
int err;
746747
int fd;
747-
char buf[] = "123";
748+
char buf[256];
749+
long hpagesz_kb;
750+
long hpagesz_mb;
748751

749752
if (geteuid() != 0) {
750753
fprintf(stderr, "WARNING: not run as root, can not do hugetlb test\n");
@@ -755,25 +758,31 @@ void setup_hugetlbfs(void)
755758

756759
/*
757760
* Now go make sure that we got the pages and that they
758-
* are 2M pages. Someone might have made 1G the default.
761+
* are PMD-level pages. Someone might have made PUD-level
762+
* pages the default.
759763
*/
760-
fd = open("/sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages", O_RDONLY);
764+
hpagesz_kb = HPAGE_SIZE / 1024;
765+
hpagesz_mb = hpagesz_kb / 1024;
766+
sprintf(buf, SYSFS_FMT_NR_HUGE_PAGES, hpagesz_kb);
767+
fd = open(buf, O_RDONLY);
761768
if (fd < 0) {
762-
perror("opening sysfs 2M hugetlb config");
769+
fprintf(stderr, "opening sysfs %ldM hugetlb config: %s\n",
770+
hpagesz_mb, strerror(errno));
763771
return;
764772
}
765773

766774
/* -1 to guarantee leaving the trailing \0 */
767775
err = read(fd, buf, sizeof(buf)-1);
768776
close(fd);
769777
if (err <= 0) {
770-
perror("reading sysfs 2M hugetlb config");
778+
fprintf(stderr, "reading sysfs %ldM hugetlb config: %s\n",
779+
hpagesz_mb, strerror(errno));
771780
return;
772781
}
773782

774783
if (atoi(buf) != GET_NR_HUGE_PAGES) {
775-
fprintf(stderr, "could not confirm 2M pages, got: '%s' expected %d\n",
776-
buf, GET_NR_HUGE_PAGES);
784+
fprintf(stderr, "could not confirm %ldM pages, got: '%s' expected %d\n",
785+
hpagesz_mb, buf, GET_NR_HUGE_PAGES);
777786
return;
778787
}
779788

0 commit comments

Comments
 (0)