From patchwork Fri May 19 10:27:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13248151 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 C8CBEC77B7F for ; Fri, 19 May 2023 10:27:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2EFC2280001; Fri, 19 May 2023 06:27:35 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2DFD0900004; Fri, 19 May 2023 06:27:35 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 180E8280001; Fri, 19 May 2023 06:27:35 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 04CC6900003 for ; Fri, 19 May 2023 06:27:35 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id A87371A0A14 for ; Fri, 19 May 2023 10:27:34 +0000 (UTC) X-FDA: 80806628028.29.FC1E151 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf13.hostedemail.com (Postfix) with ESMTP id B42C72000F for ; Fri, 19 May 2023 10:27:32 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="Mzsq6O/H"; spf=pass (imf13.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1684492053; a=rsa-sha256; cv=none; b=S6z3cvtymVUC2wGFtE2Xb/SNXOtzzgG1WqZC6h2oR4dkuBMmq4YshKAZr7VZmTDVk35kid +MUrcVN1AhhsCYOVWB8/5JpHt7d3E2BiwJwWGnUfgEfq7Oq+kyFDnLikm5BicvD/E2wUxD y2CXO4rZlUwAoLT90VAblr9Xze7lR7k= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="Mzsq6O/H"; spf=pass (imf13.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@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=1684492053; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=wLvUTdqG6kUaomJpPJAHiks49sHH2kzed8PToxtufB8=; b=f9yefkpwNhnfNNKLlmSD0IEUhanhDqqF7HLVAizfgSrmH/Z7CY0YekfYILMgKpjdKIny9a qFyk5cxGa4cYgx9XkVc9JJt8ShsjxwfltkNQiuB5nmytNOl01Itor0SWS3+YkLAJqajdIg qfRMTJNeltUZyqSVPXjrbT+Eu8jIICk= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1684492052; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=wLvUTdqG6kUaomJpPJAHiks49sHH2kzed8PToxtufB8=; b=Mzsq6O/HoSRdTWA6T1sXSFedcSUiob02WxM6T+VET/5Fm7KpOrSWjd2If+2n4rzuJ4dGSG bchTZH5BXhMI1fOnzyspNagyzqORQvh1mCwsyujzHdlwoT9nbzZ+YYr2BTznaKkzIVmJYJ TAM3dHHVZFm98NwAgBePGd6WEGwWYvU= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-9-9sijunIrNxO-3wCpyPKkqg-1; Fri, 19 May 2023 06:27:30 -0400 X-MC-Unique: 9sijunIrNxO-3wCpyPKkqg-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 172C7185A792; Fri, 19 May 2023 10:27:30 +0000 (UTC) Received: from t480s.redhat.com (unknown [10.39.194.84]) by smtp.corp.redhat.com (Postfix) with ESMTP id AA9222166B25; Fri, 19 May 2023 10:27:26 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, linux-kselftest@vger.kernel.org, David Hildenbrand , Andrew Morton , Shuah Khan , Lorenzo Stoakes , Jens Axboe , Peter Xu , Jason Gunthorpe , John Hubbard , Jan Kara Subject: [PATCH v1 1/3] selftests/mm: factor out detection of hugetlb page sizes into vm_util Date: Fri, 19 May 2023 12:27:21 +0200 Message-Id: <20230519102723.185721-2-david@redhat.com> In-Reply-To: <20230519102723.185721-1-david@redhat.com> References: <20230519102723.185721-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.6 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: B42C72000F X-Stat-Signature: powp5hcz4xtjuof4qsbj7yrnjoms5o8n X-Rspam-User: X-HE-Tag: 1684492052-163075 X-HE-Meta: U2FsdGVkX1/pQEXYuMvNACeHbT0hxbciawNpsddpy/6uDv2ReGFC3mypy4ajI6LDzmA9Znh76i+uMtHdYWvi8ZGCsNHOpgXvYjy1SXQ6NWgidkA1bRvWcFNjBrgRNI95D3zCXq1+Uc1OKS0cDGlNnLGd8U4ZWZAJwC2/esJyc9BqMb2mUBV7vboq/CNz9g/A847Ix62kPhA+K836oka/q0SRnd9YDhat9Zl2YcrYz6Q9AuNpyrDY7vyXx+MUVUc7CJ8hLKQMc8My9YtI7tgjBDrnF0hWQF49ZVIh8fANiVZDgPzkRwrIeo94WJOHCgIvbHYU5x51+LBvvIhYbQ5+1YN/G7kT7Sw7CJbq6T82HJJfjTe6+YRWPWcIX1bs8YOQLNGk/O+r69Mcn6VvStnBVEOwA/5CD2JwjZ5sC6h2rz7v2yzvkku5NlCDJFGhu6M4DPW8JDHwH5zCW+nScf4Mrqh0NQZW61NZlTaLSIZo0pmd8wz7pXwMRaNphcUQKGfMrAO0kqGm4EvyNwRyMlXF6FnCw/Zk9tJrvhdZe7hvqrctMZVicmVVJ0QX2DD4RfyoyH+boBRRnXICmMrTX0F1JCxq80jp8qTmI4ZJEdC8A0h+zDRtG3cR2ZOR0JVM7UzSRrOTyKPVtDXokP0UKoFsJMGax1xgAywX7FpPYQpOQ6JtRxgarlZereLTqmcI/TlhJsaKpSzVftmSpWPADcypMTVj179EB3phOmVMOX8yosg3VR+hLoGjINDiYBgo0nOcme8mJ2FDmXtoTHHKAEyU6m7/PLpwQzl34cR/e6Xy/+FgulUXi2b99UcUHPT5L3Pw2iSu43NwJE5TkGXeeDXLv2N8fB6b9P7sxP7vHG1pyhAKm1mie2NucqT9U5oiSXISJxqAD7/zb2RWwWZZcyJ1PnWQh1zke3tpdxyi6+ZAjfJoZEYiBKjO7Ws57v2xqwD39BL8l5lXrwlCeNa+fL/ wN9hA7fL 4fPicQfkorYbM4byzzYyIjJM0FcXsnFSUTStABvyhNGywjltr2Jw3olKJ4TXGIOehWyeTSWu/8ZYXyhIqDNV6ykWKwyrX9bkffMngVx6F99zSRkR+/KzYpQNsam3L9Fce381t4mVcFQRxvF7U2k1LZZ3HaatadC5xvNAT/yA8Rbqc1RB/RpOsyNSxtZvr9Y+wUlt+ssu83flHIhWTfUg6tk7MD8gYDzLusXCG 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: Let's factor detection out into vm_util, to be reused by a new test. Signed-off-by: David Hildenbrand Reviewed-by: Lorenzo Stoakes --- tools/testing/selftests/mm/cow.c | 29 ++-------------------------- tools/testing/selftests/mm/vm_util.c | 27 ++++++++++++++++++++++++++ tools/testing/selftests/mm/vm_util.h | 1 + 3 files changed, 30 insertions(+), 27 deletions(-) diff --git a/tools/testing/selftests/mm/cow.c b/tools/testing/selftests/mm/cow.c index dc9d6fe86028..7f3b620d9cb7 100644 --- a/tools/testing/selftests/mm/cow.c +++ b/tools/testing/selftests/mm/cow.c @@ -14,7 +14,6 @@ #include #include #include -#include #include #include #include @@ -70,31 +69,6 @@ static void detect_huge_zeropage(void) close(fd); } -static void detect_hugetlbsizes(void) -{ - DIR *dir = opendir("/sys/kernel/mm/hugepages/"); - - if (!dir) - return; - - while (nr_hugetlbsizes < ARRAY_SIZE(hugetlbsizes)) { - struct dirent *entry = readdir(dir); - size_t kb; - - if (!entry) - break; - if (entry->d_type != DT_DIR) - continue; - if (sscanf(entry->d_name, "hugepages-%zukB", &kb) != 1) - continue; - hugetlbsizes[nr_hugetlbsizes] = kb * 1024; - nr_hugetlbsizes++; - ksft_print_msg("[INFO] detected hugetlb size: %zu KiB\n", - kb); - } - closedir(dir); -} - static bool range_is_swapped(void *addr, size_t size) { for (; size; addr += pagesize, size -= pagesize) @@ -1717,7 +1691,8 @@ int main(int argc, char **argv) if (thpsize) ksft_print_msg("[INFO] detected THP size: %zu KiB\n", thpsize / 1024); - detect_hugetlbsizes(); + nr_hugetlbsizes = detect_hugetlb_page_sizes(hugetlbsizes, + ARRAY_SIZE(hugetlbsizes)); detect_huge_zeropage(); ksft_print_header(); diff --git a/tools/testing/selftests/mm/vm_util.c b/tools/testing/selftests/mm/vm_util.c index 9b06a5034808..5cf84d860076 100644 --- a/tools/testing/selftests/mm/vm_util.c +++ b/tools/testing/selftests/mm/vm_util.c @@ -1,6 +1,7 @@ // SPDX-License-Identifier: GPL-2.0 #include #include +#include #include #include #include @@ -198,6 +199,32 @@ unsigned long default_huge_page_size(void) return hps; } +int detect_hugetlb_page_sizes(size_t sizes[], int max) +{ + DIR *dir = opendir("/sys/kernel/mm/hugepages/"); + int count = 0; + + if (!dir) + return 0; + + while (count < max) { + struct dirent *entry = readdir(dir); + size_t kb; + + if (!entry) + break; + if (entry->d_type != DT_DIR) + continue; + if (sscanf(entry->d_name, "hugepages-%zukB", &kb) != 1) + continue; + sizes[count++] = kb * 1024; + ksft_print_msg("[INFO] detected hugetlb page size: %zu KiB\n", + kb); + } + closedir(dir); + return count; +} + /* If `ioctls' non-NULL, the allowed ioctls will be returned into the var */ int uffd_register_with_ioctls(int uffd, void *addr, uint64_t len, bool miss, bool wp, bool minor, uint64_t *ioctls) diff --git a/tools/testing/selftests/mm/vm_util.h b/tools/testing/selftests/mm/vm_util.h index b950bd16083a..99b795528716 100644 --- a/tools/testing/selftests/mm/vm_util.h +++ b/tools/testing/selftests/mm/vm_util.h @@ -44,6 +44,7 @@ bool check_huge_file(void *addr, int nr_hpages, uint64_t hpage_size); bool check_huge_shmem(void *addr, int nr_hpages, uint64_t hpage_size); int64_t allocate_transhuge(void *ptr, int pagemap_fd); unsigned long default_huge_page_size(void); +int detect_hugetlb_page_sizes(size_t sizes[], int max); int uffd_register(int uffd, void *addr, uint64_t len, bool miss, bool wp, bool minor); From patchwork Fri May 19 10:27:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13248153 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 AEE7CC77B7F for ; Fri, 19 May 2023 10:27:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3087B900006; Fri, 19 May 2023 06:27:39 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2B9D9900003; Fri, 19 May 2023 06:27:39 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 15976900006; Fri, 19 May 2023 06:27:39 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 00956900003 for ; Fri, 19 May 2023 06:27:38 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id CAA87C0960 for ; Fri, 19 May 2023 10:27:38 +0000 (UTC) X-FDA: 80806628196.26.1C4F861 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf06.hostedemail.com (Postfix) with ESMTP id 1E396180011 for ; Fri, 19 May 2023 10:27:36 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="NdW/KPss"; spf=pass (imf06.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@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=1684492057; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=3oFFNl9yD569q1EZgkcCjlzDNeWHDMhv48L/SWvEAMo=; b=b84bWdtFC10pM4p/EYkpbGx2Ax9C7Mtp6Whl0pUnyjAQR5pXKPPQQFydiOeQ4CF0HvRwhQ k/ohTXBlW7MnA++1nlHVNC2LZ2dkOtvntDS+wBC5uxH8fk8MnGSbQWJme2f6pn0m0EDLhP Fmk7TRHofCJiATbjmXbxqeJsvZan+qY= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="NdW/KPss"; spf=pass (imf06.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1684492057; a=rsa-sha256; cv=none; b=OLDENvGQRP577+d9ArV/yqAV3nQG3DYhsYOFkcCvuAGfaArxNNV6hL0evM6rA7kS1+w5Dj 3qNG8fv42T7Ryzf6/9ndYMsqsDFtkyEA1L4BLLgMVd+hT9fmZ2oN3JCPwoht4l0zwc58ld Wex7CBOBM+Sl/BJWZ/yt7QhJdpbEl6s= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1684492056; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=3oFFNl9yD569q1EZgkcCjlzDNeWHDMhv48L/SWvEAMo=; b=NdW/KPssrHvvOfStZua06pO6+4fw8SJBA86ZabfMoqvmlQ1UEFlRtvB+qHVaXZVODggchH C61gCKfu6n/xUQlhAbcGTDFW+b7nhMMSYH6ihV0hyKQUaYrloit5t8lq9xoE+0gLwA4vfp gmuOKhRqay56Y5EoqCES3pMc1cVAF34= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-524-_j-yRkuWMQObvAistRd5Ag-1; Fri, 19 May 2023 06:27:33 -0400 X-MC-Unique: _j-yRkuWMQObvAistRd5Ag-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id B402985A5A8; Fri, 19 May 2023 10:27:32 +0000 (UTC) Received: from t480s.redhat.com (unknown [10.39.194.84]) by smtp.corp.redhat.com (Postfix) with ESMTP id 792CA2166B25; Fri, 19 May 2023 10:27:30 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, linux-kselftest@vger.kernel.org, David Hildenbrand , Andrew Morton , Shuah Khan , Lorenzo Stoakes , Jens Axboe , Peter Xu , Jason Gunthorpe , John Hubbard , Jan Kara Subject: [PATCH v1 2/3] selftests/mm: gup_longterm: new functional test for FOLL_LONGTERM Date: Fri, 19 May 2023 12:27:22 +0200 Message-Id: <20230519102723.185721-3-david@redhat.com> In-Reply-To: <20230519102723.185721-1-david@redhat.com> References: <20230519102723.185721-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.6 X-Rspamd-Queue-Id: 1E396180011 X-Rspam-User: X-Stat-Signature: q5ikx9i3m1xtd7o66mhbf34kuzhhzc3h X-Rspamd-Server: rspam01 X-HE-Tag: 1684492056-658362 X-HE-Meta: U2FsdGVkX1+mpD8U1RGjYxM9uEYR++T+hfXJQ+v3M6cSJhrJG83w8I+L8xn0BLk9b03PES277Dt2zKCXLoAla6XsN1nlTJ2k4XPPPCChMYFr1qvS/oqs//24sXo+pR77VOzu4kg9N3K+ih3j16QZXcUAX1LHhcD6NC4PIB6bIJtZaWJpckJyHDPdPp/0Aeo2Brtd1aCNN1pDbGIg4osDz1hzZm+tOfesapLoyDtGHTbwdMFKintuoSsbOeMmz5yP+B33uUlOTA+st9MEDVcUJGeIkkUIwredZCHHp+ki23zIEAXC5pdp8svgn87w0Td0+1u3vhqT8e8YBI44quFkxDgcCnYvkfFCFrnPuX/vXGWxQf6IWAEYmiVUaG1FKQZQIcZtYBHf8gabdRSDq258MJ4nZQvClx0PrX22LDQtz/woS/vAS7RxhGO7+9+BSQ8BX8rBR7e4XsKmLeytTxxAST1yw+YUseAIBtGGkkblKNj8Ybezqw04UJYlMUygNvTSIhKYCFXbkrjEg5SamYBxpMZqh74sI+FY65BbbA1Ug+eRyhUMxrX1cP0QLiuFlV1v5dE73XcYZVGNC6B1/4bO7mD3mQO1FKJYcOhDliJUezToBc6XAndeW0DxOHtA7sIN9vkPClM7yJIIeqDOKezi24ZSspHfMFTlMkn7+/W7eFCb88S1XtsXbnbTzEmi8jIAKW1sCv8o2G/Qv032TuyFly2XUzx4MdYVJOjK4QJdRE7ltY/PxLeZ1kvt/pOICbeD6QkRwbsoP/zCDqO6Xe9ig6V7g4TxLvSBl6qEdhsYS3Ijix2pe0Xb5nRZo730OpezTZAckvVSBee65OqsWpzyNQMZT8OLLqbX5FEJWPzlgsWPRGcPwwKDEtBNBx7GN/qs90dyRabGDXxc3oqQy9D1zomkMx2Be6yMdVXmn057WZ38PUXBF3gwcO+GtV7zgDmY23pW9aT10Dvyvmu87Fw mpTcHBKI A0C1FEMKy9PUkK6Xa/0tl4SbTLmWIDmwLELTs64QRNSLD4K8nwx1ObQc1ItBvTlDjzkjCYuVB7/4+5H50SCirv+etd3E1rOLtXZYxp6hkNztpDACKDugHS3vBxSjjqOFneUw6rbeqgTwDTu+AGtGPQsTjnw/CN3Ei85NGnfgvawABo418KqShVrK0bYl6gq7AV4zmY4rQMYqn82i0WxC5hdzSHg2OjmrnkRJj 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: Let's add a new test for checking whether GUP long-term page pinning works as expected (R/O vs. R/W, MAP_PRIVATE vs. MAP_SHARED, GUP vs. GUP-fast). Note that COW handling with long-term R/O pinning in private mappings, and pinning of anonymous memory in general, is tested by the COW selftest. This test, therefore, focuses on page pinning in file mappings. The most interesting case is probably the "local tmpfile" case, as that will likely end up on a "real" filesystem such as ext4 or xfs, not on a virtual one like tmpfs or hugetlb where any long-term page pinning is always expected to succeed. For now, only add tests that use the "/sys/kernel/debug/gup_test" interface. We'll add tests based on liburing separately next. Signed-off-by: David Hildenbrand Reviewed-by: Lorenzo Stoakes Signed-off-by: Andrew Morton --- tools/testing/selftests/mm/Makefile | 3 + tools/testing/selftests/mm/gup_longterm.c | 386 ++++++++++++++++++++++ tools/testing/selftests/mm/run_vmtests.sh | 4 +- 3 files changed, 392 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/mm/gup_longterm.c diff --git a/tools/testing/selftests/mm/Makefile b/tools/testing/selftests/mm/Makefile index 23af4633f0f4..95acb099315e 100644 --- a/tools/testing/selftests/mm/Makefile +++ b/tools/testing/selftests/mm/Makefile @@ -34,6 +34,7 @@ LDLIBS = -lrt -lpthread TEST_GEN_PROGS = cow TEST_GEN_PROGS += compaction_test +TEST_GEN_PROGS += gup_longterm TEST_GEN_PROGS += gup_test TEST_GEN_PROGS += hmm-tests TEST_GEN_PROGS += hugetlb-madvise @@ -164,6 +165,8 @@ endif # IOURING_EXTRA_LIBS may get set in local_config.mk, or it may be left empty. $(OUTPUT)/cow: LDLIBS += $(IOURING_EXTRA_LIBS) +$(OUTPUT)/gup_longterm: LDLIBS += $(IOURING_EXTRA_LIBS) + $(OUTPUT)/mlock-random-test $(OUTPUT)/memfd_secret: LDLIBS += -lcap $(OUTPUT)/ksm_tests: LDLIBS += -lnuma diff --git a/tools/testing/selftests/mm/gup_longterm.c b/tools/testing/selftests/mm/gup_longterm.c new file mode 100644 index 000000000000..44a3617fd423 --- /dev/null +++ b/tools/testing/selftests/mm/gup_longterm.c @@ -0,0 +1,386 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * GUP long-term page pinning tests. + * + * Copyright 2023, Red Hat, Inc. + * + * Author(s): David Hildenbrand + */ +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "local_config.h" + +#include "../../../../mm/gup_test.h" +#include "../kselftest.h" +#include "vm_util.h" + +static size_t pagesize; +static int nr_hugetlbsizes; +static size_t hugetlbsizes[10]; +static int gup_fd; + +static __fsword_t get_fs_type(int fd) +{ + struct statfs fs; + int ret; + + do { + ret = fstatfs(fd, &fs); + } while (ret && errno == EINTR); + + return ret ? 0 : fs.f_type; +} + +static bool fs_is_unknown(__fsword_t fs_type) +{ + /* + * We only support some filesystems in our tests when dealing with + * R/W long-term pinning. For these filesystems, we can be fairly sure + * whether they support it or not. + */ + switch (fs_type) { + case TMPFS_MAGIC: + case HUGETLBFS_MAGIC: + case BTRFS_SUPER_MAGIC: + case EXT4_SUPER_MAGIC: + case XFS_SUPER_MAGIC: + return false; + default: + return true; + } +} + +static bool fs_supports_writable_longterm_pinning(__fsword_t fs_type) +{ + assert(!fs_is_unknown(fs_type)); + switch (fs_type) { + case TMPFS_MAGIC: + case HUGETLBFS_MAGIC: + return true; + default: + return false; + } +} + +enum test_type { + TEST_TYPE_RO, + TEST_TYPE_RO_FAST, + TEST_TYPE_RW, + TEST_TYPE_RW_FAST, +}; + +static void do_test(int fd, size_t size, enum test_type type, bool shared) +{ + __fsword_t fs_type = get_fs_type(fd); + bool should_work; + char *mem; + int ret; + + if (ftruncate(fd, size)) { + ksft_test_result_fail("ftruncate() failed\n"); + return; + } + + if (fallocate(fd, 0, 0, size)) { + if (size == pagesize) + ksft_test_result_fail("fallocate() failed\n"); + else + ksft_test_result_skip("need more free huge pages\n"); + return; + } + + mem = mmap(NULL, size, PROT_READ | PROT_WRITE, + shared ? MAP_SHARED : MAP_PRIVATE, fd, 0); + if (mem == MAP_FAILED) { + if (size == pagesize || shared) + ksft_test_result_fail("mmap() failed\n"); + else + ksft_test_result_skip("need more free huge pages\n"); + return; + } + + /* + * Fault in the page writable such that GUP-fast can eventually pin + * it immediately. + */ + memset(mem, 0, size); + + switch (type) { + case TEST_TYPE_RO: + case TEST_TYPE_RO_FAST: + case TEST_TYPE_RW: + case TEST_TYPE_RW_FAST: { + struct pin_longterm_test args; + const bool fast = type == TEST_TYPE_RO_FAST || + type == TEST_TYPE_RW_FAST; + const bool rw = type == TEST_TYPE_RW || + type == TEST_TYPE_RW_FAST; + + if (gup_fd < 0) { + ksft_test_result_skip("gup_test not available\n"); + break; + } + + if (rw && shared && fs_is_unknown(fs_type)) { + ksft_test_result_skip("Unknown filesystem\n"); + return; + } + /* + * R/O pinning or pinning in a private mapping is always + * expected to work. Otherwise, we expect long-term R/W pinning + * to only succeed for special fielesystems. + */ + should_work = !shared || !rw || + fs_supports_writable_longterm_pinning(fs_type); + + args.addr = (__u64)(uintptr_t)mem; + args.size = size; + args.flags = fast ? PIN_LONGTERM_TEST_FLAG_USE_FAST : 0; + args.flags |= rw ? PIN_LONGTERM_TEST_FLAG_USE_WRITE : 0; + ret = ioctl(gup_fd, PIN_LONGTERM_TEST_START, &args); + if (ret && errno == EINVAL) { + ksft_test_result_skip("PIN_LONGTERM_TEST_START failed\n"); + break; + } else if (ret && errno == EFAULT) { + ksft_test_result(!should_work, "Should have failed\n"); + break; + } else if (ret) { + ksft_test_result_fail("PIN_LONGTERM_TEST_START failed\n"); + break; + } + + if (ioctl(gup_fd, PIN_LONGTERM_TEST_STOP)) + ksft_print_msg("[INFO] PIN_LONGTERM_TEST_STOP failed\n"); + + /* + * TODO: if the kernel ever supports long-term R/W pinning on + * some previously unsupported filesystems, we might want to + * perform some additional tests for possible data corruptions. + */ + ksft_test_result(should_work, "Should have worked\n"); + break; + } + default: + assert(false); + } + + munmap(mem, size); +} + +typedef void (*test_fn)(int fd, size_t size); + +static void run_with_memfd(test_fn fn, const char *desc) +{ + int fd; + + ksft_print_msg("[RUN] %s ... with memfd\n", desc); + + fd = memfd_create("test", 0); + if (fd < 0) { + ksft_test_result_fail("memfd_create() failed\n"); + return; + } + + fn(fd, pagesize); + close(fd); +} + +static void run_with_tmpfile(test_fn fn, const char *desc) +{ + FILE *file; + int fd; + + ksft_print_msg("[RUN] %s ... with tmpfile\n", desc); + + file = tmpfile(); + if (!file) { + ksft_test_result_fail("tmpfile() failed\n"); + return; + } + + fd = fileno(file); + if (fd < 0) { + ksft_test_result_fail("fileno() failed\n"); + return; + } + + fn(fd, pagesize); + fclose(file); +} + +static void run_with_local_tmpfile(test_fn fn, const char *desc) +{ + char filename[] = __FILE__"_tmpfile_XXXXXX"; + int fd; + + ksft_print_msg("[RUN] %s ... with local tmpfile\n", desc); + + fd = mkstemp(filename); + if (fd < 0) { + ksft_test_result_fail("mkstemp() failed\n"); + return; + } + + if (unlink(filename)) { + ksft_test_result_fail("unlink() failed\n"); + goto close; + } + + fn(fd, pagesize); +close: + close(fd); +} + +static void run_with_memfd_hugetlb(test_fn fn, const char *desc, + size_t hugetlbsize) +{ + int flags = MFD_HUGETLB; + int fd; + + ksft_print_msg("[RUN] %s ... with memfd hugetlb (%zu kB)\n", desc, + hugetlbsize / 1024); + + flags |= __builtin_ctzll(hugetlbsize) << MFD_HUGE_SHIFT; + + fd = memfd_create("test", flags); + if (fd < 0) { + ksft_test_result_skip("memfd_create() failed\n"); + return; + } + + fn(fd, hugetlbsize); + close(fd); +} + +struct test_case { + const char *desc; + test_fn fn; +}; + +static void test_shared_rw_pin(int fd, size_t size) +{ + do_test(fd, size, TEST_TYPE_RW, true); +} + +static void test_shared_rw_fast_pin(int fd, size_t size) +{ + do_test(fd, size, TEST_TYPE_RW_FAST, true); +} + +static void test_shared_ro_pin(int fd, size_t size) +{ + do_test(fd, size, TEST_TYPE_RO, true); +} + +static void test_shared_ro_fast_pin(int fd, size_t size) +{ + do_test(fd, size, TEST_TYPE_RO_FAST, true); +} + +static void test_private_rw_pin(int fd, size_t size) +{ + do_test(fd, size, TEST_TYPE_RW, false); +} + +static void test_private_rw_fast_pin(int fd, size_t size) +{ + do_test(fd, size, TEST_TYPE_RW_FAST, false); +} + +static void test_private_ro_pin(int fd, size_t size) +{ + do_test(fd, size, TEST_TYPE_RO, false); +} + +static void test_private_ro_fast_pin(int fd, size_t size) +{ + do_test(fd, size, TEST_TYPE_RO_FAST, false); +} + +static const struct test_case test_cases[] = { + { + "R/W longterm GUP pin in MAP_SHARED file mapping", + test_shared_rw_pin, + }, + { + "R/W longterm GUP-fast pin in MAP_SHARED file mapping", + test_shared_rw_fast_pin, + }, + { + "R/O longterm GUP pin in MAP_SHARED file mapping", + test_shared_ro_pin, + }, + { + "R/O longterm GUP-fast pin in MAP_SHARED file mapping", + test_shared_ro_fast_pin, + }, + { + "R/W longterm GUP pin in MAP_PRIVATE file mapping", + test_private_rw_pin, + }, + { + "R/W longterm GUP-fast pin in MAP_PRIVATE file mapping", + test_private_rw_fast_pin, + }, + { + "R/O longterm GUP pin in MAP_PRIVATE file mapping", + test_private_ro_pin, + }, + { + "R/O longterm GUP-fast pin in MAP_PRIVATE file mapping", + test_private_ro_fast_pin, + }, +}; + +static void run_test_case(struct test_case const *test_case) +{ + int i; + + run_with_memfd(test_case->fn, test_case->desc); + run_with_tmpfile(test_case->fn, test_case->desc); + run_with_local_tmpfile(test_case->fn, test_case->desc); + for (i = 0; i < nr_hugetlbsizes; i++) + run_with_memfd_hugetlb(test_case->fn, test_case->desc, + hugetlbsizes[i]); +} + +static int tests_per_test_case(void) +{ + return 3 + nr_hugetlbsizes; +} + +int main(int argc, char **argv) +{ + int i, err; + + pagesize = getpagesize(); + nr_hugetlbsizes = detect_hugetlb_page_sizes(hugetlbsizes, + ARRAY_SIZE(hugetlbsizes)); + + ksft_print_header(); + ksft_set_plan(ARRAY_SIZE(test_cases) * tests_per_test_case()); + + gup_fd = open("/sys/kernel/debug/gup_test", O_RDWR); + + for (i = 0; i < ARRAY_SIZE(test_cases); i++) + run_test_case(&test_cases[i]); + + err = ksft_get_fail_cnt(); + if (err) + ksft_exit_fail_msg("%d out of %d tests failed\n", + err, ksft_test_num()); + return ksft_exit_pass(); +} diff --git a/tools/testing/selftests/mm/run_vmtests.sh b/tools/testing/selftests/mm/run_vmtests.sh index 4893eb60d96d..b6b1eb6a8a6b 100644 --- a/tools/testing/selftests/mm/run_vmtests.sh +++ b/tools/testing/selftests/mm/run_vmtests.sh @@ -24,7 +24,7 @@ separated by spaces: - mmap tests for mmap(2) - gup_test - tests for gup using gup_test interface + tests for gup - userfaultfd tests for userfaultfd(2) - compaction @@ -196,6 +196,8 @@ CATEGORY="gup_test" run_test ./gup_test -a # Dump pages 0, 19, and 4096, using pin_user_pages: CATEGORY="gup_test" run_test ./gup_test -ct -F 0x1 0 19 0x1000 +CATEGORY="gup_test" run_test ./gup_longterm + CATEGORY="userfaultfd" run_test ./uffd-unit-tests uffd_stress_bin=./uffd-stress CATEGORY="userfaultfd" run_test ${uffd_stress_bin} anon 20 16 From patchwork Fri May 19 10:27:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13248154 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 DC646C77B75 for ; Fri, 19 May 2023 10:27:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7EACB900007; Fri, 19 May 2023 06:27:44 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7C2C2900003; Fri, 19 May 2023 06:27:44 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6B09E900007; Fri, 19 May 2023 06:27:44 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 5AFB6900003 for ; Fri, 19 May 2023 06:27:44 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 31E4F160A09 for ; Fri, 19 May 2023 10:27:44 +0000 (UTC) X-FDA: 80806628448.14.DDADF90 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf12.hostedemail.com (Postfix) with ESMTP id 7075940005 for ; Fri, 19 May 2023 10:27:42 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=WsxnrUPf; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf12.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1684492062; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=dpVUKoV13qmSlxZhl7UVQfhf3HY6y+j5TROYuDuVGKc=; b=TsnHKvLYb5Y6FZZOFYpx3XGHXH9CeGlEdNiBa6nyTHgiEG6bb48VFiH6p9NYIRVskUAsKt f8gyihpGR3jWR+IsiXdsqfuQG8QWU6UthCkX5LmiYHmjTDdb1GPNeMaYlm2cZ2GAdyD7hh rhw4DGYelNyVmoRdeEFy/mFczNX1oDE= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=WsxnrUPf; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf12.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1684492062; a=rsa-sha256; cv=none; b=F+Jkwt4qQKpZWjuJF+w1hnZXI/ScYexWxvZDG3cMzevi2o4um4fIOSTpErRxq5y1L3xKd2 OLs5wEyvm9X5UnofKkYG495TwQwmjq6Uwv66UkGkauXsGl9MzvMwTgHD+QZGWOrt+f5+MZ eMnHDJ7hZZXsL8n37BXk5t71bLjYXeA= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1684492061; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=dpVUKoV13qmSlxZhl7UVQfhf3HY6y+j5TROYuDuVGKc=; b=WsxnrUPfAsFuaBC8d8LXGwedJcnxeTtcVBqlNh8bOrlYuFWjcdcV7+HJyX18SD9zEwvLDu BMR1w+7gecLOKGuN/FLsAiQpO5A/MOYJk2XA+6VW/YAjznmlkQXKn69aVlxyPeqtVLNtQ+ QFIS/V6EfBsT5dGpKX/XKUhezdec9ck= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-282-noILZ21kOdC-g25uQy8reA-1; Fri, 19 May 2023 06:27:36 -0400 X-MC-Unique: noILZ21kOdC-g25uQy8reA-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id CB8361C04B50; Fri, 19 May 2023 10:27:35 +0000 (UTC) Received: from t480s.redhat.com (unknown [10.39.194.84]) by smtp.corp.redhat.com (Postfix) with ESMTP id F08C62166B25; Fri, 19 May 2023 10:27:32 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, linux-kselftest@vger.kernel.org, David Hildenbrand , Andrew Morton , Shuah Khan , Lorenzo Stoakes , Jens Axboe , Peter Xu , Jason Gunthorpe , John Hubbard , Jan Kara Subject: [PATCH v1 3/3] selftests/mm: gup_longterm: add liburing tests Date: Fri, 19 May 2023 12:27:23 +0200 Message-Id: <20230519102723.185721-4-david@redhat.com> In-Reply-To: <20230519102723.185721-1-david@redhat.com> References: <20230519102723.185721-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.6 X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 7075940005 X-Stat-Signature: ewfgjzm3ydjabxog7hd46fdmmta1ot3y X-Rspam-User: X-HE-Tag: 1684492062-352524 X-HE-Meta: U2FsdGVkX1+3oCJ7RoscJFOHUj0QzPNBYpN29+wn4f8ML38d7u3iYYUYT2ppK7xZ/xkwQQDuAG+iDkFpp06UV741HJ5Ddbe8tmpCJHJSHX38qXuWgch/P4ieVtnKZp4Jn6mK4yGSkgw/w0zuZ+FHPRW3IFiNu+aeP1oXXcfoSdpUtvrG/QS4oxk5BYdS9RdI895J4091fL+ZwaT/g7kqteZ04nYsFUoDaaWi0ZUsPwEZDoBBYnMh82lCl0w1XZXNWnJ2+Gu0Iz5BvRffAn2xIeehTlKtC75T4z9AsFvymHL0lk1z55P5q9mteiddQEmivPVb3F1baR0DiXQDvG4JSFu6WrV4/3bbQ/sxap/g+jcCSVSVuMWNXpBWAz9Dm1Gcc8SDf37SLrm9J4KIguuQ8ceSqQnPPk6tpoUY0YuUVW8tO0uFuwqJCXmQ0pvbTi7swx8s/w0wmNMxL4KwxuDY9ACpBks9NrU6/E7ckew6foG1d4KEWkaJeBlt3Uiz7BuJCnFg5dDiE++rSMoAP+eH+pJ49zBqo4xFlJKtr7SIm+XkaVkifVLJwc9MWoZtVNjb+FqSUsBrmwBXrsCEXmL5O1Nc/IclrdkS8rDHAiilk+AFH0UUvFVrSKZkF9aezNkRou8NFe1a1VrZRzR09a2ck5JDT1k9eYSj6TTjeM5DnwRigQsbFaZ/1qqSgDOdxm+JcTCRWFfONMwB/ayhd3XhxH9vduKNFEi22hcwEDyicOI15f/t08WNt/EbLVs+zrTR8c8SQO8NIliOjkttCT90uTv44UwVMAbL2Vwrj5JrhSPnDsYcWqoloLygRzZhVvND0ZvGFPScTfWcUK7pOLRQKRSlGK9qD7gavf0/dzcAi6tt+FKIz3i4ANryzMtPEhQPVPSHCnKsi+g6fW0Gdd0Z+vHYsXNSyfADMMnm40DJSi6S/vIb+uPNEtalAisKSvXdsoLnNf/EEkrB+drrRTn XVCowaRw MNXyq9lDFFWPJW1gVcwqg6ZuJhOP3x7FPD2U2b2bUTUcVuA/5CB+BoC8dmbFOuLggkfuB8Buank7n2zADY75X+ZEqeAnJXRMyZynJP7iLtPoaknh9XXxa9HXfgxtD1tGLzC0aByIHwHrZ4iRLli9Kp1051u8G16gIy6IJJcxVrxUKYimmM59P9DtbUeJSc0FvYx4P93wGzaHpgGmcHe+Y6HG16litJzaHAwRU 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: Similar to the COW selftests, also use io_uring fixed buffers to test if long-term page pinning works as expected. Signed-off-by: David Hildenbrand Reviewed-by: Lorenzo Stoakes --- tools/testing/selftests/mm/gup_longterm.c | 73 +++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/tools/testing/selftests/mm/gup_longterm.c b/tools/testing/selftests/mm/gup_longterm.c index 44a3617fd423..d33d3e68ffab 100644 --- a/tools/testing/selftests/mm/gup_longterm.c +++ b/tools/testing/selftests/mm/gup_longterm.c @@ -22,6 +22,9 @@ #include #include "local_config.h" +#ifdef LOCAL_CONFIG_HAVE_LIBURING +#include +#endif /* LOCAL_CONFIG_HAVE_LIBURING */ #include "../../../../mm/gup_test.h" #include "../kselftest.h" @@ -80,6 +83,9 @@ enum test_type { TEST_TYPE_RO_FAST, TEST_TYPE_RW, TEST_TYPE_RW_FAST, +#ifdef LOCAL_CONFIG_HAVE_LIBURING + TEST_TYPE_IOURING, +#endif /* LOCAL_CONFIG_HAVE_LIBURING */ }; static void do_test(int fd, size_t size, enum test_type type, bool shared) @@ -173,6 +179,51 @@ static void do_test(int fd, size_t size, enum test_type type, bool shared) ksft_test_result(should_work, "Should have worked\n"); break; } +#ifdef LOCAL_CONFIG_HAVE_LIBURING + case TEST_TYPE_IOURING: { + struct io_uring ring; + struct iovec iov; + + /* io_uring always pins pages writable. */ + if (shared && fs_is_unknown(fs_type)) { + ksft_test_result_skip("Unknown filesystem\n"); + return; + } + should_work = !shared || + fs_supports_writable_longterm_pinning(fs_type); + + /* Skip on errors, as we might just lack kernel support. */ + ret = io_uring_queue_init(1, &ring, 0); + if (ret < 0) { + ksft_test_result_skip("io_uring_queue_init() failed\n"); + break; + } + /* + * Register the range as a fixed buffer. This will FOLL_WRITE | + * FOLL_PIN | FOLL_LONGTERM the range. + */ + iov.iov_base = mem; + iov.iov_len = size; + ret = io_uring_register_buffers(&ring, &iov, 1); + /* Only new kernels return EFAULT. */ + if (ret && (errno == ENOSPC || errno == EOPNOTSUPP || + errno == EFAULT)) { + ksft_test_result(!should_work, "Should have failed\n"); + } else if (ret) { + /* + * We might just lack support or have insufficient + * MEMLOCK limits. + */ + ksft_test_result_skip("io_uring_register_buffers() failed\n"); + } else { + ksft_test_result(should_work, "Should have worked\n"); + io_uring_unregister_buffers(&ring); + } + + io_uring_queue_exit(&ring); + break; + } +#endif /* LOCAL_CONFIG_HAVE_LIBURING */ default: assert(false); } @@ -310,6 +361,18 @@ static void test_private_ro_fast_pin(int fd, size_t size) do_test(fd, size, TEST_TYPE_RO_FAST, false); } +#ifdef LOCAL_CONFIG_HAVE_LIBURING +static void test_shared_iouring(int fd, size_t size) +{ + do_test(fd, size, TEST_TYPE_IOURING, true); +} + +static void test_private_iouring(int fd, size_t size) +{ + do_test(fd, size, TEST_TYPE_IOURING, false); +} +#endif /* LOCAL_CONFIG_HAVE_LIBURING */ + static const struct test_case test_cases[] = { { "R/W longterm GUP pin in MAP_SHARED file mapping", @@ -343,6 +406,16 @@ static const struct test_case test_cases[] = { "R/O longterm GUP-fast pin in MAP_PRIVATE file mapping", test_private_ro_fast_pin, }, +#ifdef LOCAL_CONFIG_HAVE_LIBURING + { + "io_uring fixed buffer with MAP_SHARED file mapping", + test_shared_iouring, + }, + { + "io_uring fixed buffer with MAP_PRIVATE file mapping", + test_private_iouring, + }, +#endif /* LOCAL_CONFIG_HAVE_LIBURING */ }; static void run_test_case(struct test_case const *test_case)