From patchwork Tue Oct 4 21:41:22 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 12998715 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id B3BC7C433F5 for ; Tue, 4 Oct 2022 21:41:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 40E4C6B0073; Tue, 4 Oct 2022 17:41:33 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3BEE16B0074; Tue, 4 Oct 2022 17:41:33 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2ADCD6B0075; Tue, 4 Oct 2022 17:41:33 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 187C16B0073 for ; Tue, 4 Oct 2022 17:41:33 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id E61251602D5 for ; Tue, 4 Oct 2022 21:41:32 +0000 (UTC) X-FDA: 79984588824.28.C573C56 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf30.hostedemail.com (Postfix) with ESMTP id 77A948000C for ; Tue, 4 Oct 2022 21:41:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1664919692; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=/w2JwM8JeFqa2ztqMSbd+xEgiAESxi1m1hIt+DBhlCQ=; b=VR1RROPqQmZ1PUyJTTCx5GBHtugFwZhbkzY/XoeFttWi4i+V65UCFdbpeqEs1P/U8s8BYy 3IsWDYak8uF+0zLoOWwa/DN/wacWhy8Z8CP1U3FTIXi32HzVOORsa0UzLOyihH6rIhev5U QCQjEgC1wKhRTZbK/z2qsXAXzDxNaws= Received: from mail-qt1-f200.google.com (mail-qt1-f200.google.com [209.85.160.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-643-DC-E6Yl-PHeyG5--Z0_gEw-1; Tue, 04 Oct 2022 17:41:29 -0400 X-MC-Unique: DC-E6Yl-PHeyG5--Z0_gEw-1 Received: by mail-qt1-f200.google.com with SMTP id d1-20020ac80601000000b00388b0fc84beso1822926qth.3 for ; Tue, 04 Oct 2022 14:41:29 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=/w2JwM8JeFqa2ztqMSbd+xEgiAESxi1m1hIt+DBhlCQ=; b=eyXAd9uAhuQNPQKa6sj08Y2D9wnCdMFzpqOpl/x9HBHT5wroYTL33uYcV6boCr6slo EkMggnws7KeMxa2IOxdNLe2y6EF24SCSRengvUHHvdoqBBK0LoJGSQKKlfeTvF3E+PpI HYJn5LbpoR3w0nxFM7cjOFixlFeehVxubzTuEOQ3DYtl60c3jCJsQ/fsFoyvfsICzcG4 uY3KV38S+fYn9sZmAe89RQ0W8+EFWtd8rU0fXAWJR2902gXKOqPzJp/u19drJ+BB1hGL jKYGyOcKwrJf1NLSTDt2Tmq9rhcCWGijuPPdR8i4ZiyAVRdduY3zrxmEPZmNnhkydi7S Yyhw== X-Gm-Message-State: ACrzQf2cWTjRH8YSuEA5H1k4smkyLoolLv68kDKnb1RKN5DdIyAGRdKz MWPNRoVCFWAYFiLD5JtgaNALGLeUcyeG02ydHeVkWBtJe/ZNWnNASmCX3dSMfU+LvS4elr2qDUi Ui9KZvqnd0U0= X-Received: by 2002:ac8:5cd5:0:b0:35b:bbd1:20ea with SMTP id s21-20020ac85cd5000000b0035bbbd120eamr21123619qta.549.1664919688608; Tue, 04 Oct 2022 14:41:28 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6qF+0TqymelkGkoObEdmZR55zlKAQ3919cYMgIiCESflQCNd2+DsttJ2F1dVn6tCX0I6g9Yg== X-Received: by 2002:ac8:5cd5:0:b0:35b:bbd1:20ea with SMTP id s21-20020ac85cd5000000b0035bbbd120eamr21123607qta.549.1664919688375; Tue, 04 Oct 2022 14:41:28 -0700 (PDT) Received: from x1n.redhat.com (bras-base-aurron9127w-grc-46-70-31-27-79.dsl.bell.ca. [70.31.27.79]) by smtp.gmail.com with ESMTPSA id y15-20020a05620a25cf00b006bbf85cad0fsm15519956qko.20.2022.10.04.14.41.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Oct 2022 14:41:27 -0700 (PDT) From: Peter Xu To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Andrew Morton , peterx@redhat.com, Axel Rasmussen , Mike Kravetz Subject: [PATCH 1/4] selftests/vm: Use memfd for hugetlb tests Date: Tue, 4 Oct 2022 17:41:22 -0400 Message-Id: <20221004214125.120993-2-peterx@redhat.com> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20221004214125.120993-1-peterx@redhat.com> References: <20221004214125.120993-1-peterx@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-type: text/plain ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1664919692; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=/w2JwM8JeFqa2ztqMSbd+xEgiAESxi1m1hIt+DBhlCQ=; b=mLz/9EVeYaauo0vYqmFXaxgWMq9Wq+fSlw0Cc9B1lE6tg4hUcixrOxLG8gOSVpGPDTLSqy qTYVqQG1CRYQxauSR/SlXoIiZVmGy5gAL1hqEiykS9gGXj3G/l0s/x+uHhIPu0AReCIZoD S92JAJ/Lfmf1+xGRnnvxXsybCK0Yk3s= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=VR1RROPq; spf=pass (imf30.hostedemail.com: domain of peterx@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1664919692; a=rsa-sha256; cv=none; b=jR+WmsGydkJUvLamXcjjuJOz+rMQ47iDkRQojZwDQjwH1SoC22SaNjlXOs36Jzijc8sAYy ANRVOIN3adgDTpRkDLANEHaEgpzlhMXnQgtMpO1I3TqrZyeN/j0np0pKC+IIBgv+p8u2eA hoYB75ERiOf/5XIVWmOGUoKrtMFRrQA= X-Rspamd-Queue-Id: 77A948000C Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=VR1RROPq; spf=pass (imf30.hostedemail.com: domain of peterx@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-Rspamd-Server: rspam06 X-Rspam-User: X-Stat-Signature: 8knxhqz4boejc36pt3ktjo8pph4gqhte X-HE-Tag: 1664919692-343976 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: We already used memfd for shmem test, move it forward with hugetlb too so that we don't need user to specify the hugetlb file path explicitly when running hugetlb shared tests. Signed-off-by: Peter Xu Reviewed-by: Axel Rasmussen --- tools/testing/selftests/vm/userfaultfd.c | 60 ++++++++---------------- 1 file changed, 20 insertions(+), 40 deletions(-) diff --git a/tools/testing/selftests/vm/userfaultfd.c b/tools/testing/selftests/vm/userfaultfd.c index 74babdbc02e5..c0c6853cdce5 100644 --- a/tools/testing/selftests/vm/userfaultfd.c +++ b/tools/testing/selftests/vm/userfaultfd.c @@ -93,10 +93,8 @@ static volatile bool test_uffdio_zeropage_eexist = true; static bool test_uffdio_wp = true; /* Whether to test uffd minor faults */ static bool test_uffdio_minor = false; - static bool map_shared; -static int shm_fd; -static int huge_fd; +static int mem_fd; static unsigned long long *count_verify; static int uffd = -1; static int uffd_flags, finished, *pipefd; @@ -260,35 +258,21 @@ static void hugetlb_release_pages(char *rel_area) static void hugetlb_allocate_area(void **alloc_area, bool is_src) { + off_t size = nr_pages * page_size; + off_t offset = is_src ? 0 : size; void *area_alias = NULL; char **alloc_area_alias; - if (!map_shared) - *alloc_area = mmap(NULL, - nr_pages * page_size, - PROT_READ | PROT_WRITE, - MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB | - (is_src ? 0 : MAP_NORESERVE), - -1, - 0); - else - *alloc_area = mmap(NULL, - nr_pages * page_size, - PROT_READ | PROT_WRITE, - MAP_SHARED | - (is_src ? 0 : MAP_NORESERVE), - huge_fd, - is_src ? 0 : nr_pages * page_size); + *alloc_area = mmap(NULL, size, PROT_READ | PROT_WRITE, + (map_shared ? MAP_SHARED : MAP_PRIVATE) | + (is_src ? 0 : MAP_NORESERVE), + mem_fd, offset); if (*alloc_area == MAP_FAILED) err("mmap of hugetlbfs file failed"); if (map_shared) { - area_alias = mmap(NULL, - nr_pages * page_size, - PROT_READ | PROT_WRITE, - MAP_SHARED, - huge_fd, - is_src ? 0 : nr_pages * page_size); + area_alias = mmap(NULL, size, PROT_READ | PROT_WRITE, + MAP_SHARED, mem_fd, offset); if (area_alias == MAP_FAILED) err("mmap of hugetlb file alias failed"); } @@ -334,14 +318,14 @@ static void shmem_allocate_area(void **alloc_area, bool is_src) } *alloc_area = mmap(p, bytes, PROT_READ | PROT_WRITE, MAP_SHARED, - shm_fd, offset); + mem_fd, offset); if (*alloc_area == MAP_FAILED) err("mmap of memfd failed"); if (test_collapse && *alloc_area != p) err("mmap of memfd failed at %p", p); area_alias = mmap(p_alias, bytes, PROT_READ | PROT_WRITE, MAP_SHARED, - shm_fd, offset); + mem_fd, offset); if (area_alias == MAP_FAILED) err("mmap of memfd alias failed"); if (test_collapse && area_alias != p_alias) @@ -1821,21 +1805,17 @@ int main(int argc, char **argv) } nr_pages = nr_pages_per_cpu * nr_cpus; - if (test_type == TEST_HUGETLB && map_shared) { - if (argc < 5) - usage(); - huge_fd = open(argv[4], O_CREAT | O_RDWR, 0755); - if (huge_fd < 0) - 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) + if (test_type == TEST_SHMEM || test_type == TEST_HUGETLB) { + unsigned int memfd_flags = 0; + + if (test_type == TEST_HUGETLB) + memfd_flags = MFD_HUGETLB; + mem_fd = memfd_create(argv[0], memfd_flags); + if (mem_fd < 0) err("memfd_create"); - if (ftruncate(shm_fd, nr_pages * page_size * 2)) + if (ftruncate(mem_fd, nr_pages * page_size * 2)) err("ftruncate"); - if (fallocate(shm_fd, + if (fallocate(mem_fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, 0, nr_pages * page_size * 2)) err("fallocate");