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

selftests/vm: hmm-tests: remove the libhugetlbfs dependency

HMM selftests are incredibly useful, but they are only effective if people
actually build and run them. All the other tests in selftests/vm can be
built with very standard, always-available libraries: libpthread, librt.
The hmm-tests.c program, on the other hand, requires something that is
(much) less readily available: libhugetlbfs. And so the build will
typically fail for many developers.

A simple attempt to install libhugetlbfs will also run into complications
on some common distros these days: Fedora and Arch Linux (yes, Arch AUR
has it, but that's fragile, as always with AUR). The library is not
maintained actively enough at the moment, for distros to deal with it. I
had to build it from source, for Fedora, and that didn't go too smoothly
either.

It turns out that, out of 21 tests in hmm-tests.c, only 2 actually require
functionality from libhugetlbfs. Therefore, if libhugetlbfs is missing,
simply ifdef those two tests out and allow the developer to at least have
the other 19 tests, if they don't want to pause to work through the above
issues. Also issue a warning, so that it's clear that there is an
imperfection in the build.

In order to do that, a tiny shell script (check_config.sh) runs a quick
compile (not link, that's too prone to false failures with library paths),
and basically, if the compiler doesn't find hugetlbfs.h in its standard
locations, then the script concludes that libhugetlbfs is not available.
The output is in two files, one for inclusion in hmm-test.c
(local_config.h), and one for inclusion in the Makefile (local_config.mk).

Link: https://lkml.kernel.org/r/20201026064021.3545418-9-jhubbard@nvidia.com
Signed-off-by: John Hubbard <jhubbard@nvidia.com>
Cc: Ralph Campbell <rcampbell@nvidia.com>
Cc: Jérôme Glisse <jglisse@redhat.com>
Cc: Jonathan Corbet <corbet@lwn.net>
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

John Hubbard and committed by
Linus Torvalds
f3a45709 d943fe81

+63 -3
+1
tools/testing/selftests/vm/.gitignore
··· 20 20 map_fixed_noreplace 21 21 write_to_hugetlbfs 22 22 hmm-tests 23 + local_config.*
+22 -2
tools/testing/selftests/vm/Makefile
··· 1 1 # SPDX-License-Identifier: GPL-2.0 2 2 # Makefile for vm selftests 3 + 4 + include local_config.mk 5 + 3 6 uname_M := $(shell uname -m 2>/dev/null || echo not) 4 7 MACHINE ?= $(shell echo $(uname_M) | sed -e 's/aarch64.*/arm64/') 5 8 ··· 83 80 KSFT_KHDR_INSTALL := 1 84 81 include ../lib.mk 85 82 86 - $(OUTPUT)/hmm-tests: LDLIBS += -lhugetlbfs 87 - 88 83 ifeq ($(ARCH),x86_64) 89 84 BINARIES_32 := $(patsubst %,$(OUTPUT)/%,$(BINARIES_32)) 90 85 BINARIES_64 := $(patsubst %,$(OUTPUT)/%,$(BINARIES_64)) ··· 135 134 $(OUTPUT)/mlock-random-test: LDLIBS += -lcap 136 135 137 136 $(OUTPUT)/gup_test: ../../../../mm/gup_test.h 137 + 138 + $(OUTPUT)/hmm-tests: local_config.h 139 + 140 + # HMM_EXTRA_LIBS may get set in local_config.mk, or it may be left empty. 141 + $(OUTPUT)/hmm-tests: LDLIBS += $(HMM_EXTRA_LIBS) 142 + 143 + local_config.mk local_config.h: check_config.sh 144 + /bin/sh ./check_config.sh $(CC) 145 + 146 + EXTRA_CLEAN += local_config.mk local_config.h 147 + 148 + ifeq ($(HMM_EXTRA_LIBS),) 149 + all: warn_missing_hugelibs 150 + 151 + warn_missing_hugelibs: 152 + @echo ; \ 153 + echo "Warning: missing libhugetlbfs support. Some HMM tests will be skipped." ; \ 154 + echo 155 + endif
+31
tools/testing/selftests/vm/check_config.sh
··· 1 + #!/bin/sh 2 + # SPDX-License-Identifier: GPL-2.0 3 + # 4 + # Probe for libraries and create header files to record the results. Both C 5 + # header files and Makefile include fragments are created. 6 + 7 + OUTPUT_H_FILE=local_config.h 8 + OUTPUT_MKFILE=local_config.mk 9 + 10 + # libhugetlbfs 11 + tmpname=$(mktemp) 12 + tmpfile_c=${tmpname}.c 13 + tmpfile_o=${tmpname}.o 14 + 15 + echo "#include <sys/types.h>" > $tmpfile_c 16 + echo "#include <hugetlbfs.h>" >> $tmpfile_c 17 + echo "int func(void) { return 0; }" >> $tmpfile_c 18 + 19 + CC=${1:?"Usage: $0 <compiler> # example compiler: gcc"} 20 + $CC -c $tmpfile_c -o $tmpfile_o >/dev/null 2>&1 21 + 22 + if [ -f $tmpfile_o ]; then 23 + echo "#define LOCAL_CONFIG_HAVE_LIBHUGETLBFS 1" > $OUTPUT_H_FILE 24 + echo "HMM_EXTRA_LIBS = -lhugetlbfs" > $OUTPUT_MKFILE 25 + else 26 + echo "// No libhugetlbfs support found" > $OUTPUT_H_FILE 27 + echo "# No libhugetlbfs support found, so:" > $OUTPUT_MKFILE 28 + echo "HMM_EXTRA_LIBS = " >> $OUTPUT_MKFILE 29 + fi 30 + 31 + rm ${tmpname}.*
+9 -1
tools/testing/selftests/vm/hmm-tests.c
··· 21 21 #include <strings.h> 22 22 #include <time.h> 23 23 #include <pthread.h> 24 - #include <hugetlbfs.h> 25 24 #include <sys/types.h> 26 25 #include <sys/stat.h> 27 26 #include <sys/mman.h> 28 27 #include <sys/ioctl.h> 28 + 29 + #include "./local_config.h" 30 + #ifdef LOCAL_CONFIG_HAVE_LIBHUGETLBFS 31 + #include <hugetlbfs.h> 32 + #endif 29 33 30 34 /* 31 35 * This is a private UAPI to the kernel test module so it isn't exported ··· 666 662 hmm_buffer_free(buffer); 667 663 } 668 664 665 + #ifdef LOCAL_CONFIG_HAVE_LIBHUGETLBFS 669 666 /* 670 667 * Write huge TLBFS page. 671 668 */ ··· 725 720 buffer->ptr = NULL; 726 721 hmm_buffer_free(buffer); 727 722 } 723 + #endif /* LOCAL_CONFIG_HAVE_LIBHUGETLBFS */ 728 724 729 725 /* 730 726 * Read mmap'ed file memory. ··· 1342 1336 hmm_buffer_free(buffer); 1343 1337 } 1344 1338 1339 + #ifdef LOCAL_CONFIG_HAVE_LIBHUGETLBFS 1345 1340 /* 1346 1341 * Test the hmm_range_fault() HMM_PFN_PMD flag for large pages that 1347 1342 * should be mapped by a large page table entry. ··· 1418 1411 buffer->ptr = NULL; 1419 1412 hmm_buffer_free(buffer); 1420 1413 } 1414 + #endif /* LOCAL_CONFIG_HAVE_LIBHUGETLBFS */ 1421 1415 1422 1416 /* 1423 1417 * Test two devices reading the same memory (double mapped).