From patchwork Thu Apr 4 07:26:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13617393 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 E2C28CD129A for ; Thu, 4 Apr 2024 07:54:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A56626B008C; Thu, 4 Apr 2024 03:54:37 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9B88F6B0092; Thu, 4 Apr 2024 03:54:37 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8087C6B0095; Thu, 4 Apr 2024 03:54:37 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 608E46B008C for ; Thu, 4 Apr 2024 03:54:37 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id E5996120895 for ; Thu, 4 Apr 2024 07:54:36 +0000 (UTC) X-FDA: 81971087352.18.B90AD25 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.13]) by imf25.hostedemail.com (Postfix) with ESMTP id BB672A001A for ; Thu, 4 Apr 2024 07:54:34 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=cPE2G0te; spf=pass (imf25.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.13 as permitted sender) smtp.mailfrom=vivek.kasireddy@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1712217275; 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=V6kCHyWZKqn7mR1PNWai0TB/t0/0xfCN0prTWy3MkRg=; b=r1VcgoSjCP53H8f6jgLx/c7LV0dryNLj9/iNk2EhJp35F0UAtn8jsaB2xzcXVHDfViwwP4 Mr8Ey12vPohlhiHns31abHxjuWlw0fGiOg+jjmxBJR3aOdc4tiflCL0xAi5YaNCO90DJFg ZJm1Kg13U21gScWBzZdxPiIBgkPTZV0= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712217275; a=rsa-sha256; cv=none; b=QG9J/ESkWh9JalzK7wz1mXuHb1ODKnWBSy2wnSe/vo0m7pVPMLpMEohXvJSp2e6xVLkrJH 1LT58a0RXJuNasX8AnroB3khdz4eDBQsGwm/2ENmsJw9LFEM11hbesz5Gxp2O4Za78xMEN zrt+wJ7LQqvs2V3p1THqYsbXunwy8Bk= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=cPE2G0te; spf=pass (imf25.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.13 as permitted sender) smtp.mailfrom=vivek.kasireddy@intel.com; dmarc=pass (policy=none) header.from=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1712217274; x=1743753274; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Yb7amEGe1VBc3b0oe6v93Jw3wp7VQuVAA8DFhsG6G1g=; b=cPE2G0tebq2KYc96uQoyTGnBkFGuHkLWZBFHhq94RzH8yM7kvuOzBrKs qicvOYROrx4xzuL0UqClEhGspgAWyaTQQI7ZOB5iEinq/26nsCHCvg42Z gp0rX6wXHn+ANua1y5FiRYbjRL6+S3Ztx7AAk7X2rVMpkGkCuxFNJCohF tSlcFpbMZb9w1oOz9S2tUlvy+k8WTzSWv+4M/eS+LeJfxZRvVN8fHbeOU JbIaW4OVYpXOYajr80w503+W5HddAII5IZflnxIl4sNX4Sr39W78Dp1iU K4YPJIK0VBKeudBBTw7jRfJShB14tVaiUHoR8DnDqKDge7GiYuBPLnAk/ Q==; X-CSE-ConnectionGUID: 2d4kx6HDRp+BxLBUQAEH/A== X-CSE-MsgGUID: A3kEcpGcQSS5VFAT40BmZA== X-IronPort-AV: E=McAfee;i="6600,9927,11033"; a="10450788" X-IronPort-AV: E=Sophos;i="6.07,178,1708416000"; d="scan'208";a="10450788" Received: from orviesa008.jf.intel.com ([10.64.159.148]) by fmvoesa107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Apr 2024 00:54:27 -0700 X-CSE-ConnectionGUID: oyRgjK/WTKuzsJABcC3ksA== X-CSE-MsgGUID: vozHhhVGQIeScfbT58z8VA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,178,1708416000"; d="scan'208";a="19298762" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by orviesa008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Apr 2024 00:54:28 -0700 From: Vivek Kasireddy To: dri-devel@lists.freedesktop.org, linux-mm@kvack.org Cc: Vivek Kasireddy , David Hildenbrand , Matthew Wilcox , Christoph Hellwig , Daniel Vetter , Mike Kravetz , Hugh Dickins , Peter Xu , Gerd Hoffmann , Dongwon Kim , Junxiao Chang , Jason Gunthorpe , Christoph Hellwig Subject: [PATCH v13 3/8] mm/gup: Introduce memfd_pin_folios() for pinning memfd folios Date: Thu, 4 Apr 2024 00:26:10 -0700 Message-ID: <20240404073053.3073706-4-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240404073053.3073706-1-vivek.kasireddy@intel.com> References: <20240404073053.3073706-1-vivek.kasireddy@intel.com>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zZh6bcICpkMrzknuX2Z8YjXzkWETceR7zaX zDDTo0pq 6LX/q/AeR9vYLlWcSj2Ie4qFbEO74bjkGnv4VJQ6lU3YC1LDbdrwc4sNiHaIEVh+q8UdIYEUuLO4ZYN5eqXA/PvCgAeOpec5B5N3wcEN4EjlW7Zvsf+OM0UDQkiYhVY1hgusEXzPl/rIpBNeRRDiAHrSe0nizb+vap5sqagHJWa96vKSqHk5YcidE4Tp1Y4z34g1EBxf/sKeIv97FIA/qvjH1cxQIJWlKMDEGwvhbrNrG6HocUOk0ozq5PW7jwPyxElAx/R5UmFIDvltO+aC1Q30kLFc6nK7gxXbUx6Z8/4FsuK4fk/OrxyzRaftfS+fevOZBYSaGDUuyEvechhkBoqdXMkMCnddqj3zQLvnI+of4wvo= 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: List-Subscribe: List-Unsubscribe: For drivers that would like to longterm-pin the folios associated with a memfd, the memfd_pin_folios() API provides an option to not only pin the folios via FOLL_PIN but also to check and migrate them if they reside in movable zone or CMA block. This API currently works with memfds but it should work with any files that belong to either shmemfs or hugetlbfs. Files belonging to other filesystems are rejected for now. The folios need to be located first before pinning them via FOLL_PIN. If they are found in the page cache, they can be immediately pinned. Otherwise, they need to be allocated using the filesystem specific APIs and then pinned. Cc: David Hildenbrand Cc: Matthew Wilcox (Oracle) Cc: Christoph Hellwig Cc: Daniel Vetter Cc: Mike Kravetz Cc: Hugh Dickins Cc: Peter Xu Cc: Gerd Hoffmann Cc: Dongwon Kim Cc: Junxiao Chang Suggested-by: Jason Gunthorpe Reviewed-by: Jason Gunthorpe (v2) Reviewed-by: David Hildenbrand (v3) Reviewed-by: Christoph Hellwig (v6) Signed-off-by: Vivek Kasireddy --- include/linux/memfd.h | 5 ++ include/linux/mm.h | 3 + mm/gup.c | 136 ++++++++++++++++++++++++++++++++++++++++++ mm/memfd.c | 35 +++++++++++ 4 files changed, 179 insertions(+) diff --git a/include/linux/memfd.h b/include/linux/memfd.h index e7abf6fa4c52..3f2cf339ceaf 100644 --- a/include/linux/memfd.h +++ b/include/linux/memfd.h @@ -6,11 +6,16 @@ #ifdef CONFIG_MEMFD_CREATE extern long memfd_fcntl(struct file *file, unsigned int cmd, unsigned int arg); +struct folio *memfd_alloc_folio(struct file *memfd, pgoff_t idx); #else static inline long memfd_fcntl(struct file *f, unsigned int c, unsigned int a) { return -EINVAL; } +static inline struct folio *memfd_alloc_folio(struct file *memfd, pgoff_t idx) +{ + return ERR_PTR(-EINVAL); +} #endif #endif /* __LINUX_MEMFD_H */ diff --git a/include/linux/mm.h b/include/linux/mm.h index f2ffd54e1736..9e5055f11021 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2533,6 +2533,9 @@ long get_user_pages_unlocked(unsigned long start, unsigned long nr_pages, struct page **pages, unsigned int gup_flags); long pin_user_pages_unlocked(unsigned long start, unsigned long nr_pages, struct page **pages, unsigned int gup_flags); +long memfd_pin_folios(struct file *memfd, loff_t start, loff_t end, + struct folio **folios, unsigned int max_folios, + pgoff_t *offset); int get_user_pages_fast(unsigned long start, int nr_pages, unsigned int gup_flags, struct page **pages); diff --git a/mm/gup.c b/mm/gup.c index 00ee3b987307..9e64a89182b5 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -5,6 +5,7 @@ #include #include +#include #include #include #include @@ -17,6 +18,7 @@ #include #include #include +#include #include #include @@ -3746,3 +3748,137 @@ long pin_user_pages_unlocked(unsigned long start, unsigned long nr_pages, &locked, gup_flags); } EXPORT_SYMBOL(pin_user_pages_unlocked); + +/** + * memfd_pin_folios() - pin folios associated with a memfd + * @memfd: the memfd whose folios are to be pinned + * @start: the first memfd offset + * @end: the last memfd offset (inclusive) + * @folios: array that receives pointers to the folios pinned + * @max_folios: maximum number of entries in @folios + * @offset: the offset into the first folio + * + * Attempt to pin folios associated with a memfd in the contiguous range + * [start, end]. Given that a memfd is either backed by shmem or hugetlb, + * the folios can either be found in the page cache or need to be allocated + * if necessary. Once the folios are located, they are all pinned via + * FOLL_PIN and @offset is populatedwith the offset into the first folio. + * And, eventually, these pinned folios must be released either using + * unpin_folios() or unpin_folio(). + * + * It must be noted that the folios may be pinned for an indefinite amount + * of time. And, in most cases, the duration of time they may stay pinned + * would be controlled by the userspace. This behavior is effectively the + * same as using FOLL_LONGTERM with other GUP APIs. + * + * Returns number of folios pinned, which could be less than @max_folios + * as it depends on the folio sizes that cover the range [start, end]. + * If no folios were pinned, it returns -errno. + */ +long memfd_pin_folios(struct file *memfd, loff_t start, loff_t end, + struct folio **folios, unsigned int max_folios, + pgoff_t *offset) +{ + unsigned int flags, nr_folios, nr_found; + unsigned int i, pgshift = PAGE_SHIFT; + pgoff_t start_idx, end_idx, next_idx; + struct folio *folio = NULL; + struct folio_batch fbatch; + struct hstate *h; + long ret; + + if (start > end || !max_folios) + return -EINVAL; + + if (!memfd) + return -EINVAL; + + if (!shmem_file(memfd) && !is_file_hugepages(memfd)) + return -EINVAL; + + if (is_file_hugepages(memfd)) { + h = hstate_file(memfd); + pgshift = huge_page_shift(h); + } + + flags = memalloc_pin_save(); + do { + nr_folios = 0; + start_idx = start >> pgshift; + end_idx = end >> pgshift; + if (is_file_hugepages(memfd)) { + start_idx <<= huge_page_order(h); + end_idx <<= huge_page_order(h); + } + + folio_batch_init(&fbatch); + while (start_idx <= end_idx && nr_folios < max_folios) { + /* + * In most cases, we should be able to find the folios + * in the page cache. If we cannot find them for some + * reason, we try to allocate them and add them to the + * page cache. + */ + nr_found = filemap_get_folios_contig(memfd->f_mapping, + &start_idx, + end_idx, + &fbatch); + if (folio) { + folio_put(folio); + folio = NULL; + } + + next_idx = 0; + for (i = 0; i < nr_found; i++) { + /* + * As there can be multiple entries for a + * given folio in the batch returned by + * filemap_get_folios_contig(), the below + * check is to ensure that we pin and return a + * unique set of folios between start and end. + */ + if (next_idx && + next_idx != folio_index(fbatch.folios[i])) + continue; + + folio = try_grab_folio(&fbatch.folios[i]->page, + 1, FOLL_PIN); + if (!folio) { + folio_batch_release(&fbatch); + goto err; + } + + if (nr_folios == 0) + *offset = offset_in_folio(folio, start); + + folios[nr_folios] = folio; + next_idx = folio_next_index(folio); + if (++nr_folios == max_folios) + break; + } + + folio = NULL; + folio_batch_release(&fbatch); + if (!nr_found) { + folio = memfd_alloc_folio(memfd, start_idx); + if (IS_ERR(folio)) { + ret = PTR_ERR(folio); + if (ret != -EEXIST) + goto err; + } + } + } + + ret = check_and_migrate_movable_folios(nr_folios, folios); + } while (ret == -EAGAIN); + + memalloc_pin_restore(flags); + return ret ? ret : nr_folios; +err: + memalloc_pin_restore(flags); + unpin_folios(folios, nr_folios); + + return ret; +} +EXPORT_SYMBOL_GPL(memfd_pin_folios); + diff --git a/mm/memfd.c b/mm/memfd.c index 7d8d3ab3fa37..6ce2abb22c2d 100644 --- a/mm/memfd.c +++ b/mm/memfd.c @@ -59,6 +59,41 @@ static void memfd_tag_pins(struct xa_state *xas) xas_unlock_irq(xas); } +/* + * This is a helper function used by memfd_pin_user_pages() in GUP (gup.c). + * It is mainly called to allocate a page in a memfd when the caller + * (memfd_pin_folios()) cannot find a page in the page cache at a given + * index in the mapping. + */ +struct folio *memfd_alloc_folio(struct file *memfd, pgoff_t idx) +{ +#ifdef CONFIG_HUGETLB_PAGE + struct folio *folio; + int err; + + if (is_file_hugepages(memfd)) { + folio = alloc_hugetlb_folio_nodemask(hstate_file(memfd), + NUMA_NO_NODE, + NULL, + GFP_USER, + false); + if (folio && folio_try_get(folio)) { + err = hugetlb_add_to_page_cache(folio, + memfd->f_mapping, + idx); + if (err) { + folio_put(folio); + free_huge_folio(folio); + return ERR_PTR(err); + } + return folio; + } + return ERR_PTR(-ENOMEM); + } +#endif + return shmem_read_folio(memfd->f_mapping, idx); +} + /* * Setting SEAL_WRITE requires us to verify there's no pending writer. However, * via get_user_pages(), drivers might have some pending I/O without any active