@@ -89,7 +89,6 @@ static bool test_uffdio_minor = false;
8989static bool map_shared ;
9090static int shm_fd ;
9191static int huge_fd ;
92- static char * huge_fd_off0 ;
9392static unsigned long long * count_verify ;
9493static int uffd = -1 ;
9594static 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
228227static 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
236238static 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