Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp4248228pxb; Tue, 2 Mar 2021 10:09:15 -0800 (PST) X-Google-Smtp-Source: ABdhPJybUdBtbxSv7UEiE1BwiVP5HyTLkFC9BVCA2fMTqK3dpKxZoeLB/ttE6BsjOYTSjzGOF6ER X-Received: by 2002:a17:906:a147:: with SMTP id bu7mr21552072ejb.383.1614708555588; Tue, 02 Mar 2021 10:09:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614708555; cv=none; d=google.com; s=arc-20160816; b=NPe2G/+uDrUR3WaIDz3ZYlSRg/Cior0S3P29kLAfoNvYOidvPKWxZ20SXSccIRpm1I hzXB/2Bw8JlIOUlzVXaGgkMGdv/MU6fNGluN6uJecMAw8ldaQra6782xsRxoFtelLC2A QlfjRj2/AudiddnIUGyDR2lEDy2ATfKIAKybDsDa+yIFy8yik45Px7Fj4BXiF08iv6vN bZf2efIkSAeNkKBtDTrZQ6fC/kW1Fhq/OuvyJttY9ZPZryVBtcPhrYQp0M3tYrNfR9be Y3QzBe2u1fBA/EjZawP0nFK7mWDnNvDfdRHiRZmbkZOobzJgNOMrf6lTzZY+TR+uy8Cc SuPw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:sender:dkim-signature; bh=y5AgFuKFjXcZgGVnYccTGhQxqZidPq3Pqfu1gkT294w=; b=q0GOqvdA6vaGaKT4E/oHGFN1Rc0SY/4bg5T8debwFNDcWfmrZB8BCSzNfrIl9HJKqS DX/LYDXKTpoIOBdaPWE9GVaUPGw1vAVrjHJSOklrXIQpuBptIDG4T0nOrB675cYp2Cz0 pKIpr/bnJtfBZb3iVELePjWjMTpK8Wv69qDJgpQ0OV1iYpR+oSznGANCPt38pvrzdXgu icYMBya4FKG+RKsMq9KkjmdnR9HIbqst3XVDt+JgV8RK3LlLb+5B1tlvWtJ0hLkw69W5 g0tGCKteBpqHMNBOMR+4m+8RumwTaLenaJgnpWGvCvPQoAZaSk/bPVYKaiI+akvv7APB SMCA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=VSKi8Uaz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id g8si13253433eje.538.2021.03.02.10.08.51; Tue, 02 Mar 2021 10:09:15 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=VSKi8Uaz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1381058AbhCBEUN (ORCPT + 99 others); Mon, 1 Mar 2021 23:20:13 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49778 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238695AbhCBADO (ORCPT ); Mon, 1 Mar 2021 19:03:14 -0500 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2454BC0617A9 for ; Mon, 1 Mar 2021 16:01:42 -0800 (PST) Received: by mail-yb1-xb49.google.com with SMTP id p136so20821958ybc.21 for ; Mon, 01 Mar 2021 16:01:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:in-reply-to:message-id:mime-version:references:subject :from:to:cc; bh=y5AgFuKFjXcZgGVnYccTGhQxqZidPq3Pqfu1gkT294w=; b=VSKi8Uazg5pOrsOQDwoO0e8L17WYuvQ9E516op7e0cs2yyo8d2NNBPqObvyWHLOAwn XE9nJvogR4M6YcywOisP0eWKlk4psXjlBtES03cZe0F/IvaM1jHxKq+XuzmIeaxMX/M5 VXTzbwEAZ46raW4Vyj73iOzSTqHNSZvKuj6FDVFj9StOGR5RMqPKiV9L+w2BcpRtnJCc yE2OhGlJNXpCm5tCJyVeZpUMs39A3IzURZD54wKhTKgDpEySBpdIyH82zQr896FTrRwL 2G7KUAfHqIJQIzQyE+YEFjXgyxYBKshaJ+ND9MQjmzLTDp4P88KohT2HRMlSQ97SsYWE GyYA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=y5AgFuKFjXcZgGVnYccTGhQxqZidPq3Pqfu1gkT294w=; b=gMST2YJkIKz2TmZL9UZvg1mtPwtSz6DgNLs8tQmifHqA2OnPzYd/DMo22rt7X5v0kg NoDgRZFf4JdyMrtuD4jsxRTp8wH3+5lumA425SkluzWLmpcmx7FVnhNjvXhfDJWr8dgy sWp7eMEBiedPgjlBHVW21P9V9JsXYnBww+1PGMn4yekCj6PGozT649oxbK4iawx3USds XB2/zY2OhD8cW0lhCCWFZ0tDgE7EIiXpp1WGiZIkWQ9+brzZGkotTKi5ofnUot9dAnBk kM9TAs9rEihhCzuQx5vm/bqFJCaOU63pU8UDP4NnA7S4Ux85zE4wnHuCs8jR/TlNS68J KG5A== X-Gm-Message-State: AOAM533dkxHXDuuQl0d5y/OjjvvwrnipbbvUBRSohBJbX5s+/HlUH/W7 JLEJMDv2pNQZRMMXYE8mkUdNoC4zV+yCQP6Anyaz Sender: "axelrasmussen via sendgmr" X-Received: from ajr0.svl.corp.google.com ([2620:15c:2cd:203:1998:8165:ca50:ab8d]) (user=axelrasmussen job=sendgmr) by 2002:a5b:851:: with SMTP id v17mr27988049ybq.55.1614643301329; Mon, 01 Mar 2021 16:01:41 -0800 (PST) Date: Mon, 1 Mar 2021 16:01:30 -0800 In-Reply-To: <20210302000133.272579-1-axelrasmussen@google.com> Message-Id: <20210302000133.272579-3-axelrasmussen@google.com> Mime-Version: 1.0 References: <20210302000133.272579-1-axelrasmussen@google.com> X-Mailer: git-send-email 2.30.1.766.gb4fecdf3b7-goog Subject: [PATCH v2 2/5] userfaultfd/selftests: use memfd_create for shmem test type From: Axel Rasmussen To: Alexander Viro , Andrea Arcangeli , Andrew Morton , Hugh Dickins , Jerome Glisse , Joe Perches , Lokesh Gidra , Mike Rapoport , Peter Xu , Shaohua Li , Shuah Khan , Wang Qing Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org, Axel Rasmussen , Brian Geffon , Cannon Matthews , "Dr . David Alan Gilbert" , David Rientjes , Michel Lespinasse , Mina Almasry , Oliver Upton Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. Signed-off-by: Axel Rasmussen --- tools/testing/selftests/vm/userfaultfd.c | 35 ++++++++++++++++++++---- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/tools/testing/selftests/vm/userfaultfd.c b/tools/testing/selftests/vm/userfaultfd.c index f5ab5e0312e7..859398efb4fe 100644 --- a/tools/testing/selftests/vm/userfaultfd.c +++ b/tools/testing/selftests/vm/userfaultfd.c @@ -85,6 +85,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; @@ -297,12 +298,20 @@ static int 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) { - fprintf(stderr, "shared memory mmap failed\n"); - *alloc_area = NULL; + perror("mmap of memfd failed"); + goto fail; } + + return; + +fail: + *alloc_area = NULL; } struct uffd_test_ops { @@ -1672,15 +1681,31 @@ int main(int argc, char **argv) usage(); huge_fd = open(argv[4], O_CREAT | O_RDWR, 0755); if (huge_fd < 0) { - fprintf(stderr, "Open of %s failed", argv[3]); + fprintf(stderr, "Open of %s failed", argv[4]); perror("open"); exit(1); } if (ftruncate(huge_fd, 0)) { - fprintf(stderr, "ftruncate %s to size 0 failed", argv[3]); + fprintf(stderr, "ftruncate %s to size 0 failed", argv[4]); perror("ftruncate"); exit(1); } + } else if (test_type == TEST_SHMEM) { + shm_fd = memfd_create(argv[0], 0); + if (shm_fd < 0) { + perror("memfd_create"); + exit(1); + } + if (ftruncate(shm_fd, nr_pages * page_size * 2)) { + perror("ftruncate"); + exit(1); + } + if (fallocate(shm_fd, + FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, 0, + nr_pages * page_size * 2)) { + perror("fallocate"); + exit(1); + } } printf("nr_pages: %lu, nr_pages_per_cpu: %lu\n", nr_pages, nr_pages_per_cpu); -- 2.30.1.766.gb4fecdf3b7-goog