Skip to content

Commit fa2c2b5

Browse files
CmdrMoozytorvalds
authored andcommitted
userfaultfd/selftests: use memfd_create for shmem test type
This is a preparatory commit. In the future, we want to be able to setup alias mappings for area_src and area_dst in the shmem test, like we do in the hugetlb_shared test. With a VMA obtained via mmap(MAP_ANONYMOUS | MAP_SHARED), it isn't clear how to do this. So, mmap() with an fd, so we can create alias mappings. Use memfd_create instead of actually passing in a tmpfs path like hugetlb does, since it's more convenient / simpler to run, and works just as well. Future commits will: 1. Setup the alias mappings. 2. Extend our tests to actually take advantage of this, to test new userfaultfd behavior being introduced in this series. Also, a small fix in the area we're changing: when the hugetlb setup fails in main(), pass in the right argv[] so we actually print out the hugetlb file path. Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Axel Rasmussen <[email protected]> Reviewed-by: Peter Xu <[email protected]> Cc: Alexander Viro <[email protected]> Cc: Andrea Arcangeli <[email protected]> Cc: Brian Geffon <[email protected]> Cc: "Dr . David Alan Gilbert" <[email protected]> Cc: Hugh Dickins <[email protected]> Cc: Jerome Glisse <[email protected]> Cc: Joe Perches <[email protected]> Cc: Kirill A. Shutemov <[email protected]> Cc: Lokesh Gidra <[email protected]> Cc: Mike Kravetz <[email protected]> Cc: Mike Rapoport <[email protected]> Cc: Mina Almasry <[email protected]> Cc: Oliver Upton <[email protected]> Cc: Shaohua Li <[email protected]> Cc: Shuah Khan <[email protected]> Cc: Stephen Rothwell <[email protected]> Cc: Wang Qing <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 7d64ae3 commit fa2c2b5

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

tools/testing/selftests/vm/userfaultfd.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ static bool test_uffdio_wp = false;
8585
static bool test_uffdio_minor = false;
8686

8787
static bool map_shared;
88+
static int shm_fd;
8889
static int huge_fd;
8990
static char *huge_fd_off0;
9091
static unsigned long long *count_verify;
@@ -277,8 +278,11 @@ static void shmem_release_pages(char *rel_area)
277278

278279
static void shmem_allocate_area(void **alloc_area)
279280
{
281+
unsigned long offset =
282+
alloc_area == (void **)&area_src ? 0 : nr_pages * page_size;
283+
280284
*alloc_area = mmap(NULL, nr_pages * page_size, PROT_READ | PROT_WRITE,
281-
MAP_ANONYMOUS | MAP_SHARED, -1, 0);
285+
MAP_SHARED, shm_fd, offset);
282286
if (*alloc_area == MAP_FAILED)
283287
err("mmap of memfd failed");
284288
}
@@ -1602,6 +1606,16 @@ int main(int argc, char **argv)
16021606
err("Open of %s failed", argv[4]);
16031607
if (ftruncate(huge_fd, 0))
16041608
err("ftruncate %s to size 0 failed", argv[4]);
1609+
} else if (test_type == TEST_SHMEM) {
1610+
shm_fd = memfd_create(argv[0], 0);
1611+
if (shm_fd < 0)
1612+
err("memfd_create");
1613+
if (ftruncate(shm_fd, nr_pages * page_size * 2))
1614+
err("ftruncate");
1615+
if (fallocate(shm_fd,
1616+
FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, 0,
1617+
nr_pages * page_size * 2))
1618+
err("fallocate");
16051619
}
16061620
printf("nr_pages: %lu, nr_pages_per_cpu: %lu\n",
16071621
nr_pages, nr_pages_per_cpu);

0 commit comments

Comments
 (0)