FROMGIT: 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/20210503180737.2487560-8-axelrasmussen@google.com Signed-off-by: Axel Rasmussen <axelrasmussen@google.com> Reviewed-by: Peter Xu <peterx@redhat.com> Cc: Alexander Viro <viro@zeniv.linux.org.uk> Cc: Andrea Arcangeli <aarcange@redhat.com> Cc: Brian Geffon <bgeffon@google.com> Cc: "Dr . David Alan Gilbert" <dgilbert@redhat.com> Cc: Hugh Dickins <hughd@google.com> Cc: Jerome Glisse <jglisse@redhat.com> Cc: Joe Perches <joe@perches.com> Cc: Kirill A. Shutemov <kirill@shutemov.name> Cc: Lokesh Gidra <lokeshgidra@google.com> Cc: Mike Kravetz <mike.kravetz@oracle.com> Cc: Mike Rapoport <rppt@linux.vnet.ibm.com> Cc: Mina Almasry <almasrymina@google.com> Cc: Oliver Upton <oupton@google.com> Cc: Shaohua Li <shli@fb.com> Cc: Shuah Khan <shuah@kernel.org> Cc: Stephen Rothwell <sfr@canb.auug.org.au> Cc: Wang Qing <wangqing@vivo.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Stephen Rothwell <sfr@canb.auug.org.au> (cherry picked from commit 1c0385e8406d9cc180275955f41e60f6d9e2a6af https: //git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git akpm) Link: https://lore.kernel.org/patchwork/patch/1420976/ Signed-off-by: Lokesh Gidra <lokeshgidra@google.com> Bug: 187930641 Change-Id: Id8fc463cef73e8e99190f0d748e4d92ada8541a4
This commit is contained in:
parent
0b161cf6fc
commit
9f837b909a
1 changed files with 15 additions and 1 deletions
|
|
@ -83,6 +83,7 @@ static bool test_uffdio_wp = false;
|
|||
static bool test_uffdio_minor = false;
|
||||
|
||||
static bool map_shared;
|
||||
static int shm_fd;
|
||||
static int huge_fd;
|
||||
static char *huge_fd_off0;
|
||||
static unsigned long long *count_verify;
|
||||
|
|
@ -276,8 +277,11 @@ static void shmem_release_pages(char *rel_area)
|
|||
|
||||
static void shmem_allocate_area(void **alloc_area)
|
||||
{
|
||||
unsigned long offset =
|
||||
alloc_area == (void **)&area_src ? 0 : nr_pages * page_size;
|
||||
|
||||
*alloc_area = mmap(NULL, nr_pages * page_size, PROT_READ | PROT_WRITE,
|
||||
MAP_ANONYMOUS | MAP_SHARED, -1, 0);
|
||||
MAP_SHARED, shm_fd, offset);
|
||||
if (*alloc_area == MAP_FAILED)
|
||||
err("mmap of memfd failed");
|
||||
}
|
||||
|
|
@ -1437,6 +1441,16 @@ int main(int argc, char **argv)
|
|||
err("Open of %s failed", argv[4]);
|
||||
if (ftruncate(huge_fd, 0))
|
||||
err("ftruncate %s to size 0 failed", argv[4]);
|
||||
} else if (test_type == TEST_SHMEM) {
|
||||
shm_fd = memfd_create(argv[0], 0);
|
||||
if (shm_fd < 0)
|
||||
err("memfd_create");
|
||||
if (ftruncate(shm_fd, nr_pages * page_size * 2))
|
||||
err("ftruncate");
|
||||
if (fallocate(shm_fd,
|
||||
FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, 0,
|
||||
nr_pages * page_size * 2))
|
||||
err("fallocate");
|
||||
}
|
||||
printf("nr_pages: %lu, nr_pages_per_cpu: %lu\n",
|
||||
nr_pages, nr_pages_per_cpu);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue