Skip to content

Commit 9ae8f2b

Browse files
mjkravetztorvalds
authored andcommitted
userfaultfd/selftests: enable hugetlb remap and remove event testing
With MADV_DONTNEED support added to hugetlb mappings, mremap testing can also be enabled for hugetlb. Modify the tests to use madvise MADV_DONTNEED and MADV_REMOVE instead of fallocate hole puch for releasing hugetlb pages. Link: https://lkml.kernel.org/r/20220215002348.128823-4-mike.kravetz@oracle.com Signed-off-by: Mike Kravetz <mike.kravetz@oracle.com> Reviewed-by: Axel Rasmussen <axelrasmussen@google.com> Cc: Andrea Arcangeli <aarcange@redhat.com> Cc: David Hildenbrand <david@redhat.com> Cc: Michal Hocko <mhocko@suse.com> Cc: Mike Rapoport <rppt@kernel.org> Cc: Mina Almasry <almasrymina@google.com> Cc: Naoya Horiguchi <naoya.horiguchi@linux.dev> Cc: Peter Xu <peterx@redhat.com> Cc: Shuah Khan <skhan@linuxfoundation.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent c4b6cb8 commit 9ae8f2b

2 files changed

Lines changed: 36 additions & 36 deletions

File tree

tools/testing/selftests/vm/run_vmtests.sh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,14 +208,13 @@ echo "running userfaultfd_hugetlb"
208208
echo "---------------------------"
209209
# Test requires source and destination huge pages. Size of source
210210
# (half_ufd_size_MB) is passed as argument to test.
211-
./userfaultfd hugetlb $half_ufd_size_MB 32 $mnt/ufd_test_file
211+
./userfaultfd hugetlb $half_ufd_size_MB 32
212212
if [ $? -ne 0 ]; then
213213
echo "[FAIL]"
214214
exitcode=1
215215
else
216216
echo "[PASS]"
217217
fi
218-
rm -f $mnt/ufd_test_file
219218

220219
echo "-------------------------"
221220
echo "running userfaultfd_shmem"

tools/testing/selftests/vm/userfaultfd.c

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ static bool test_uffdio_minor = false;
8989
static bool map_shared;
9090
static int shm_fd;
9191
static int huge_fd;
92-
static char *huge_fd_off0;
9392
static unsigned long long *count_verify;
9493
static int uffd = -1;
9594
static int uffd_flags, finished, *pipefd;
@@ -128,9 +127,9 @@ const char *examples =
128127
"./userfaultfd anon 100 99999\n\n"
129128
"# Run share memory test on 1GiB region with 99 bounces:\n"
130129
"./userfaultfd shmem 1000 99\n\n"
131-
"# Run hugetlb memory test on 256MiB region with 50 bounces (using /dev/hugepages/hugefile):\n"
132-
"./userfaultfd hugetlb 256 50 /dev/hugepages/hugefile\n\n"
133-
"# Run the same hugetlb test but using shmem:\n"
130+
"# Run hugetlb memory test on 256MiB region with 50 bounces:\n"
131+
"./userfaultfd hugetlb 256 50\n\n"
132+
"# Run the same hugetlb test but using shared file:\n"
134133
"./userfaultfd hugetlb_shared 256 50 /dev/hugepages/hugefile\n\n"
135134
"# 10MiB-~6GiB 999 bounces anonymous test, "
136135
"continue forever unless an error triggers\n"
@@ -227,37 +226,51 @@ static void noop_alias_mapping(__u64 *start, size_t len, unsigned long offset)
227226

228227
static void hugetlb_release_pages(char *rel_area)
229228
{
230-
if (fallocate(huge_fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
231-
rel_area == huge_fd_off0 ? 0 : nr_pages * page_size,
232-
nr_pages * page_size))
233-
err("fallocate() failed");
229+
if (!map_shared) {
230+
if (madvise(rel_area, nr_pages * page_size, MADV_DONTNEED))
231+
err("madvise(MADV_DONTNEED) failed");
232+
} else {
233+
if (madvise(rel_area, nr_pages * page_size, MADV_REMOVE))
234+
err("madvise(MADV_REMOVE) failed");
235+
}
234236
}
235237

236238
static void hugetlb_allocate_area(void **alloc_area)
237239
{
238240
void *area_alias = NULL;
239241
char **alloc_area_alias;
240242

241-
*alloc_area = mmap(NULL, nr_pages * page_size, PROT_READ | PROT_WRITE,
242-
(map_shared ? MAP_SHARED : MAP_PRIVATE) |
243-
MAP_HUGETLB |
244-
(*alloc_area == area_src ? 0 : MAP_NORESERVE),
245-
huge_fd, *alloc_area == area_src ? 0 :
246-
nr_pages * page_size);
243+
if (!map_shared)
244+
*alloc_area = mmap(NULL,
245+
nr_pages * page_size,
246+
PROT_READ | PROT_WRITE,
247+
MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB |
248+
(*alloc_area == area_src ? 0 : MAP_NORESERVE),
249+
-1,
250+
0);
251+
else
252+
*alloc_area = mmap(NULL,
253+
nr_pages * page_size,
254+
PROT_READ | PROT_WRITE,
255+
MAP_SHARED |
256+
(*alloc_area == area_src ? 0 : MAP_NORESERVE),
257+
huge_fd,
258+
*alloc_area == area_src ? 0 : nr_pages * page_size);
247259
if (*alloc_area == MAP_FAILED)
248260
err("mmap of hugetlbfs file failed");
249261

250262
if (map_shared) {
251-
area_alias = mmap(NULL, nr_pages * page_size, PROT_READ | PROT_WRITE,
252-
MAP_SHARED | MAP_HUGETLB,
253-
huge_fd, *alloc_area == area_src ? 0 :
254-
nr_pages * page_size);
263+
area_alias = mmap(NULL,
264+
nr_pages * page_size,
265+
PROT_READ | PROT_WRITE,
266+
MAP_SHARED,
267+
huge_fd,
268+
*alloc_area == area_src ? 0 : nr_pages * page_size);
255269
if (area_alias == MAP_FAILED)
256270
err("mmap of hugetlb file alias failed");
257271
}
258272

259273
if (*alloc_area == area_src) {
260-
huge_fd_off0 = *alloc_area;
261274
alloc_area_alias = &area_src_alias;
262275
} else {
263276
alloc_area_alias = &area_dst_alias;
@@ -270,12 +283,7 @@ static void hugetlb_alias_mapping(__u64 *start, size_t len, unsigned long offset
270283
{
271284
if (!map_shared)
272285
return;
273-
/*
274-
* We can't zap just the pagetable with hugetlbfs because
275-
* MADV_DONTEED won't work. So exercise -EEXIST on a alias
276-
* mapping where the pagetables are not established initially,
277-
* this way we'll exercise the -EEXEC at the fs level.
278-
*/
286+
279287
*start = (unsigned long) area_dst_alias + offset;
280288
}
281289

@@ -428,7 +436,6 @@ static void uffd_test_ctx_clear(void)
428436
uffd = -1;
429437
}
430438

431-
huge_fd_off0 = NULL;
432439
munmap_area((void **)&area_src);
433440
munmap_area((void **)&area_src_alias);
434441
munmap_area((void **)&area_dst);
@@ -926,10 +933,7 @@ static int faulting_process(int signal_test)
926933
struct sigaction act;
927934
unsigned long signalled = 0;
928935

929-
if (test_type != TEST_HUGETLB)
930-
split_nr_pages = (nr_pages + 1) / 2;
931-
else
932-
split_nr_pages = nr_pages;
936+
split_nr_pages = (nr_pages + 1) / 2;
933937

934938
if (signal_test) {
935939
sigbuf = &jbuf;
@@ -986,9 +990,6 @@ static int faulting_process(int signal_test)
986990
if (signal_test)
987991
return signalled != split_nr_pages;
988992

989-
if (test_type == TEST_HUGETLB)
990-
return 0;
991-
992993
area_dst = mremap(area_dst, nr_pages * page_size, nr_pages * page_size,
993994
MREMAP_MAYMOVE | MREMAP_FIXED, area_src);
994995
if (area_dst == MAP_FAILED)
@@ -1676,7 +1677,7 @@ int main(int argc, char **argv)
16761677
}
16771678
nr_pages = nr_pages_per_cpu * nr_cpus;
16781679

1679-
if (test_type == TEST_HUGETLB) {
1680+
if (test_type == TEST_HUGETLB && map_shared) {
16801681
if (argc < 5)
16811682
usage();
16821683
huge_fd = open(argv[4], O_CREAT | O_RDWR, 0755);

0 commit comments

Comments
 (0)