From patchwork Fri Oct 14 14:39:18 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13007064 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 C080EC4332F for ; Fri, 14 Oct 2022 14:39:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 20A3F6B0075; Fri, 14 Oct 2022 10:39:28 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 145CB8E0001; Fri, 14 Oct 2022 10:39:28 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E3C776B007B; Fri, 14 Oct 2022 10:39:27 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id CE3086B0075 for ; Fri, 14 Oct 2022 10:39:27 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id F0D87C0B5A for ; Fri, 14 Oct 2022 14:39:26 +0000 (UTC) X-FDA: 80019813132.25.F9A5ABA Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf16.hostedemail.com (Postfix) with ESMTP id 9B05B18001E for ; Fri, 14 Oct 2022 14:39:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1665758366; 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=CEy7pKIEsfMMFzj4vyOzQVpkWI0TxrmvN3NLo9J44z8=; b=iepYJhttJwhqIGc6BDa2CKrYQKQSHTWGZcrN9v8IMO6Ngab5cIWXky27aNI4NmuNNYOSci bSf0nvG2q5zt0ych081OKFa68KQDieNsDiLXthPTHCB/NucxMfOIHcV3Sl6Zq5Ytq5HSPf 9cUoITp/c97tRaOiwJktQ35/6tRlOYs= Received: from mail-qk1-f200.google.com (mail-qk1-f200.google.com [209.85.222.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-101-VTRN07k4NZ2WbSvESY5wqA-1; Fri, 14 Oct 2022 10:39:24 -0400 X-MC-Unique: VTRN07k4NZ2WbSvESY5wqA-1 Received: by mail-qk1-f200.google.com with SMTP id t1-20020a05620a450100b006ee79ceeb6fso3572861qkp.11 for ; Fri, 14 Oct 2022 07:39:24 -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:message-id:reply-to; bh=CEy7pKIEsfMMFzj4vyOzQVpkWI0TxrmvN3NLo9J44z8=; b=swxFV9kxbY5opSz0H4XYD4NEtgk/ZiBowlxC69Mj/djU75cqXuLXuU38xFniDGhg3Y 7mTq5p94UYf2cYqiZecrnrysb74U4hQV/npvmL1+XQIJnOXMKo1j4er54YZCIV+kr8os 1K23GIzyvtXyUOzk5EWm4RsfoyPjKnkLY+sMOZJ7LrlVi6K8Qp2eWoWHXTmEAdSF0qun KV+zZTHNe7b5iJyFyDby31OnoqCO1tPxxKmAdr/wUNwaCLBTBe8krhXFLp8wl+vddnFf 2kBdzF/Y9ksN6HhRoywbSvS0SFBOKHK+eWyV2QOUb3l7x0ciLTP+jeXKj3gtx3PnTS8Q S1gw== X-Gm-Message-State: ACrzQf0o+J0Bs/786HPBpqId8gTv1cb3GhizdgqPcEb12q6pq6rfRbIr Qy5kZYVxrd3eKvI/ySPaowokPyjd8fNDv5/FR4evjYGFZD8p3eykgOV6LbvCo1IKCv4amKGBDnn eSJeSYZuyOe0= X-Received: by 2002:a05:6214:500f:b0:4b1:d2b1:54c with SMTP id jo15-20020a056214500f00b004b1d2b1054cmr4236930qvb.54.1665758364325; Fri, 14 Oct 2022 07:39:24 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5hcHtcB11MKZ36Ka9jJq2b9wUzusa/zVJ0ne6bkmpItRnjhCW0Ngl0EeWdKQA1NKrIZ/bLuw== X-Received: by 2002:a05:6214:500f:b0:4b1:d2b1:54c with SMTP id jo15-20020a056214500f00b004b1d2b1054cmr4236905qvb.54.1665758364028; Fri, 14 Oct 2022 07:39:24 -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 q4-20020a05620a2a4400b006ec09d7d357sm2675541qkp.47.2022.10.14.07.39.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Oct 2022 07:39:23 -0700 (PDT) From: Peter Xu To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Axel Rasmussen , Andrew Morton , peterx@redhat.com, Mike Kravetz Subject: [PATCH v2 1/4] selftests/vm: Use memfd for uffd hugetlb tests Date: Fri, 14 Oct 2022 10:39:18 -0400 Message-Id: <20221014143921.93887-2-peterx@redhat.com> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20221014143921.93887-1-peterx@redhat.com> References: <20221014143921.93887-1-peterx@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-type: text/plain ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1665758366; a=rsa-sha256; cv=none; b=xFHo+GWMXMrRcQ0cOM9jS2Wsuoh1JJH8BFDPcqvEizeCoPcuMbUNx+jXt72AzUulLWCu2x ThogLVOg00wL2gQD9NMQZ6HWpzQ3Ej+W8HLKMH11xL6HQtkZHTYCpMquX+vhwuqmf/znvj 2P0kqRpHldkIgGWE3Qpjw+cHuSw6ukY= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=iepYJhtt; spf=pass (imf16.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-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1665758366; 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=CEy7pKIEsfMMFzj4vyOzQVpkWI0TxrmvN3NLo9J44z8=; b=FIJOAuOO9UoYEIyKndBDeF1ORrODDGEyemojDyMD0GN0j/ly1GfxnyJpKoF8XFNNfZcYZx Cl+m0W025gHjy5JS88nKhZSYO9sBV+aZ37YL8mM0DBPOu4ndQBf3GNBjSgV+FaFNucTOAe ZB7jiYjAtdoOgTBpDjtIlw6/NozI6nM= X-Rspam-User: X-Stat-Signature: dx7hru7baunaipnwc5s6hg1zbkqxfkyn X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: 9B05B18001E Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=iepYJhtt; spf=pass (imf16.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-HE-Tag: 1665758366-435164 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. Reviewed-by: Axel Rasmussen Signed-off-by: Peter Xu --- tools/testing/selftests/vm/userfaultfd.c | 62 ++++++++---------------- 1 file changed, 21 insertions(+), 41 deletions(-) diff --git a/tools/testing/selftests/vm/userfaultfd.c b/tools/testing/selftests/vm/userfaultfd.c index 74babdbc02e5..58f70d81e630 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; @@ -143,7 +141,7 @@ const char *examples = "# Run hugetlb memory test on 256MiB region with 50 bounces:\n" "./userfaultfd hugetlb 256 50\n\n" "# Run the same hugetlb test but using shared file:\n" - "./userfaultfd hugetlb_shared 256 50 /dev/hugepages/hugefile\n\n" + "./userfaultfd hugetlb_shared 256 50\n\n" "# 10MiB-~6GiB 999 bounces anonymous test, " "continue forever unless an error triggers\n" "while ./userfaultfd anon $[RANDOM % 6000 + 10] 999; do true; done\n\n"; @@ -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");