From patchwork Thu Nov 23 06:44:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13465856 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 CBAE3C61D85 for ; Thu, 23 Nov 2023 07:08:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4F52D6B05AB; Thu, 23 Nov 2023 02:08:33 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4A4CE6B063F; Thu, 23 Nov 2023 02:08:33 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 36E806B0641; Thu, 23 Nov 2023 02:08:33 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 24DCB6B05AB for ; Thu, 23 Nov 2023 02:08:33 -0500 (EST) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id CCDF4160372 for ; Thu, 23 Nov 2023 07:08:32 +0000 (UTC) X-FDA: 81488340864.23.98BD7B3 Received: from mgamail.intel.com (mgamail.intel.com [134.134.136.20]) by imf09.hostedemail.com (Postfix) with ESMTP id ACF87140004 for ; Thu, 23 Nov 2023 07:08:29 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=RBGiaZRV; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf09.hostedemail.com: domain of vivek.kasireddy@intel.com designates 134.134.136.20 as permitted sender) smtp.mailfrom=vivek.kasireddy@intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1700723310; 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=cShyoQNOKFiAdLKHd8/KIzSL7Do4nyLKbs95L25GBXQ=; b=JPdUrAcEFaAlHXzEoMVMHTQ940S8BZ827DlZ7VNJ82c+0UlUw6APw11wOZrM0xoSVK+ptb 1YvhsoKEDArmWcKXPf2v4kPHQzuhy0BOwVxvPGsxFXbfzZcXNBR7nOL0XKACzM1npKENyf aQSl9gflZqNIHlwcGfTk2FHp9/w6F4Q= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=RBGiaZRV; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf09.hostedemail.com: domain of vivek.kasireddy@intel.com designates 134.134.136.20 as permitted sender) smtp.mailfrom=vivek.kasireddy@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700723310; a=rsa-sha256; cv=none; b=xFzXOBtjBCaP06+wAgd9ywtPTSJoQDrJX4rtpDGg5qEOjctUzNZShtBWD9e8Cvr87X7ALC W4C+9AOKjSY7pAUqo1jTpcMzrRk9TNrvVPG+VUDhlUpYufL0ORIzHQUAIqRJ3mRwsMEjTM 96e0I734dgfRp0b9v6/pGhL3y7se7q0= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1700723309; x=1732259309; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Dk8F7Q/oBhEAmsAwkSHXM8PeNKfeERmLQrUShteflOg=; b=RBGiaZRVWR7db53oH+DutqPWdxLbw9uiuOdcUZIPkUcqF9InKsLyYGBu FQh/VnwoctveKQLn5BeD0wjdtnnL24qmgL907T9FNAITpr87WiTX4tBQW COpqm0AxM9ZTgYJfpB2uRElef3isvx6+hRTfjtFyfvmQr04PVois5ByR/ xE8PCixn9r8VST3Yhoy5FrN5yhmGA2uQh4OxilSvDf5py5rgJBKvur2SK eQAXUC3b2lbcg6eTVG84SL/bc5q3KEVKS8h6zoGgaVmEQGtGVA9FyZO+I Dx09q4zqI+/6KMGIGuHMHx3YC8PnmNU3cWZGQ9rn0te6DV4MXO9qOaXAK w==; X-IronPort-AV: E=McAfee;i="6600,9927,10902"; a="382612329" X-IronPort-AV: E=Sophos;i="6.04,220,1695711600"; d="scan'208";a="382612329" Received: from fmviesa002.fm.intel.com ([10.60.135.142]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Nov 2023 23:08:24 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.04,220,1695711600"; d="scan'208";a="8757720" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by fmviesa002-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Nov 2023 23:08:23 -0800 From: Vivek Kasireddy To: dri-devel@lists.freedesktop.org, linux-mm@kvack.org Cc: Vivek Kasireddy , David Hildenbrand , Daniel Vetter , Mike Kravetz , Hugh Dickins , Peter Xu , Gerd Hoffmann , Dongwon Kim , Junxiao Chang , Jason Gunthorpe Subject: [PATCH v5 3/5] mm/gup: Introduce pin_user_pages_fd() for pinning shmem/hugetlbfs file pages (v5) Date: Wed, 22 Nov 2023 22:44:41 -0800 Message-Id: <20231123064443.1035709-4-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231123064443.1035709-1-vivek.kasireddy@intel.com> References: <20231123064443.1035709-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: ACF87140004 X-Stat-Signature: eo8tj4erooc413ftg1c3h6p94747soqy X-Rspam-User: X-HE-Tag: 1700723309-908652 X-HE-Meta: U2FsdGVkX1/pADiW2Za2j+nNJ+b5bp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SdU7iSVV3DC9mlZWx/x+7CVAi5mhgc4mkccX8DneTp6ttO7EtRzvQlIEIxP3ZgnvUA9jg2rc6susNePiwqc0iKq0+76TK5kF57neXPQsLFrB0xpUNYJT6ITt6jf4FRXqzXDzHCm0bSTUe8howL2+2pipmS8Xu6/EMY0luX2YvB5g85BdP3+5nJE1wSSLSVwayftlFzjj5VE= 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 pages associated with a file, the pin_user_pages_fd() API provides an option to not only pin the pages via FOLL_PIN but also to check and migrate them if they reside in movable zone or CMA block. This API currently works with files that belong to either shmem or hugetlbfs. Files belonging to other filesystems are rejected for now. The pages 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. v2: - Drop gup_flags and improve comments and commit message (David) - Allocate a page if we cannot find in page cache for the hugetlbfs case as well (David) - Don't unpin pages if there is a migration related failure (David) - Drop the unnecessary nr_pages <= 0 check (Jason) - Have the caller of the API pass in file * instead of fd (Jason) v3: (David) - Enclose the huge page allocation code with #ifdef CONFIG_HUGETLB_PAGE (Build error reported by kernel test robot ) - Don't forget memalloc_pin_restore() on non-migration related errors - Improve the readability of the cleanup code associated with non-migration related errors - Augment the comments by describing FOLL_LONGTERM like behavior - Include the R-b tag from Jason v4: - Remove the local variable "page" and instead use 3 return statements in alloc_file_page() (David) - Add the R-b tag from David v5: (David) - For hugetlb case, ensure that we only obtain head pages from the mapping by using __filemap_get_folio() instead of find_get_page_flags() - Handle -EEXIST when two or more potential users try to simultaneously add a huge page to the mapping by forcing them to retry on failure Cc: David Hildenbrand 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) Signed-off-by: Vivek Kasireddy --- include/linux/mm.h | 2 + mm/gup.c | 114 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+) diff --git a/include/linux/mm.h b/include/linux/mm.h index 418d26608ece..1b675fa35059 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2472,6 +2472,8 @@ 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 pin_user_pages_fd(struct file *file, pgoff_t start, + unsigned long nr_pages, struct page **pages); 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 231711efa390..ab1056b3e75b 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -3410,3 +3410,117 @@ long pin_user_pages_unlocked(unsigned long start, unsigned long nr_pages, &locked, gup_flags); } EXPORT_SYMBOL(pin_user_pages_unlocked); + +static struct page *alloc_file_page(struct file *file, pgoff_t idx) +{ +#ifdef CONFIG_HUGETLB_PAGE + struct folio *folio; + int err; + + if (is_file_hugepages(file)) { + folio = alloc_hugetlb_folio_nodemask(hstate_file(file), + NUMA_NO_NODE, + NULL, + GFP_USER); + if (folio && folio_try_get(folio)) { + err = hugetlb_add_to_page_cache(folio, + file->f_mapping, + idx); + if (err) { + folio_put(folio); + free_huge_folio(folio); + return ERR_PTR(err); + } + return &folio->page; + } + return ERR_PTR(-ENOMEM); + } +#endif + return shmem_read_mapping_page(file->f_mapping, idx); +} + +/** + * pin_user_pages_fd() - pin user pages associated with a file + * @file: the file whose pages are to be pinned + * @start: starting file offset + * @nr_pages: number of pages from start to pin + * @pages: array that receives pointers to the pages pinned. + * Should be at-least nr_pages long. + * + * Attempt to pin pages associated with a file that belongs to either shmem + * or hugetlb. The pages are either found in the page cache or allocated if + * necessary. Once the pages are located, they are all pinned via FOLL_PIN. + * And, these pinned pages need to be released either using unpin_user_pages() + * or unpin_user_page(). + * + * It must be noted that the pages 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 pages pinned. This would be equal to the number of + * pages requested. If no pages were pinned, it returns -errno. + */ +long pin_user_pages_fd(struct file *file, pgoff_t start, + unsigned long nr_pages, struct page **pages) +{ + struct page *page; + struct folio *folio; + unsigned int flags, i; + long ret; + + if (start < 0) + return -EINVAL; + + if (!file) + return -EINVAL; + + if (!shmem_file(file) && !is_file_hugepages(file)) + return -EINVAL; + + flags = memalloc_pin_save(); + do { + for (i = 0; i < nr_pages; i++) { + /* + * In most cases, we should be able to find the page + * in the page cache. If we cannot find it, we try to + * allocate one and add it to the page cache. + */ +retry: + folio = __filemap_get_folio(file->f_mapping, + start + i, + FGP_ACCESSED, 0); + + page = IS_ERR(folio) ? NULL: &folio->page; + if (!page) { + page = alloc_file_page(file, start + i); + if (IS_ERR(page)) { + ret = PTR_ERR(page); + if (ret == -EEXIST) + goto retry; + goto err; + } + } + ret = try_grab_page(page, FOLL_PIN); + if (unlikely(ret)) + goto err; + + pages[i] = page; + put_page(pages[i]); + } + + ret = check_and_migrate_movable_pages(nr_pages, pages); + } while (ret == -EAGAIN); + + memalloc_pin_restore(flags); + return ret ? ret : nr_pages; +err: + memalloc_pin_restore(flags); + while (i-- > 0) + if (pages[i]) + unpin_user_page(pages[i]); + + return ret; +} +EXPORT_SYMBOL_GPL(pin_user_pages_fd); +