From patchwork Tue Oct 3 07:44:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13406980 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 84BB0E75437 for ; Tue, 3 Oct 2023 08:05:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1CA088D0060; Tue, 3 Oct 2023 04:05:30 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 179A08D005D; Tue, 3 Oct 2023 04:05:30 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F37D48D0060; Tue, 3 Oct 2023 04:05:29 -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 E59DE8D005D for ; Tue, 3 Oct 2023 04:05:29 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id C4FF41601BF for ; Tue, 3 Oct 2023 08:05:29 +0000 (UTC) X-FDA: 81303415578.15.86EC606 Received: from mgamail.intel.com (mgamail.intel.com [134.134.136.100]) by imf26.hostedemail.com (Postfix) with ESMTP id BCE9F14001D for ; Tue, 3 Oct 2023 08:05:27 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=H0CwpFDD; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf26.hostedemail.com: domain of vivek.kasireddy@intel.com designates 134.134.136.100 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=1696320327; 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=dwnrxfUvAYoyWCIfLVEopVveMSGDqTSy237H5Iqy56k=; b=tXSvxEqVLqE/U+EqECWeDlq82gckRQ5jsm3kyFWQR6slo5YuUss1i9PkdsDxK+Ne2ZEJiC pTKlUSX/mDsx9sI5wwfS8fPQ1UAquxGxorbPnyz8mEd4sBUz11zmod6K9/KK4R1jVdmVSH u7BueOqXjvLACJlmX4IuTG4ZX190iJI= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=H0CwpFDD; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf26.hostedemail.com: domain of vivek.kasireddy@intel.com designates 134.134.136.100 as permitted sender) smtp.mailfrom=vivek.kasireddy@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1696320327; a=rsa-sha256; cv=none; b=oz7SRCJrcb/0xGLw61Dv6IR1fccGx0NdUHpNz1iEcz/LRxq0m0iCoUxWQZ9L3gmZwV8vfE jwznTTRr/4MumPcIktPtrOL4t0J+HU6wyrIcUkJwUy21LrzB/nNul1toYArhaLonYaWAvv qCmjsJzk3VXvGBW5z1FP8Mb6HCJgX58= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1696320327; x=1727856327; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Ocd1PVVgB8FbEUS5R3uP96wQnUqQO7DOI3QH4iInWew=; b=H0CwpFDD+zsSUj9xEtgGNRL034t7O8fFCl1sBtwadWnk+LxZJfYfAHKS pUy+A4aALvCeFip0O8quRJcGledDxOfqZV+ws0iikxPAwOqtYEkOQvvsk nRXye9v9a0+CCEmZ2pVxbb0oRZkfvj4u5eEwuVbKS3N7LNTfPPSINXSon YJhtGWGALz0hQSFyy5RIGnG+vajNlHh5oHmZGQ70aXKTI3kf1LTYFHnzS H3lpWuJ12EE5K+gxi4YOStRM2T2XxTUzUaKgZErTFMLKTSLGZYkKAT9Er DL+xTt7fdEgur0V+Kf6xa1BpzQ3YjtmN24QHETKZFYBIclHnBH/pduUz6 A==; X-IronPort-AV: E=McAfee;i="6600,9927,10851"; a="449306983" X-IronPort-AV: E=Sophos;i="6.03,196,1694761200"; d="scan'208";a="449306983" Received: from orsmga003.jf.intel.com ([10.7.209.27]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Oct 2023 01:05:25 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10851"; a="700615287" X-IronPort-AV: E=Sophos;i="6.03,196,1694761200"; d="scan'208";a="700615287" Received: from vkasired-desk2.fm.intel.com ([10.105.128.127]) by orsmga003-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Oct 2023 01:05:25 -0700 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 v1 1/3] mm/gup: Introduce pin_user_pages_fd() for pinning shmem/hugetlbfs file pages Date: Tue, 3 Oct 2023 00:44:45 -0700 Message-Id: <20231003074447.3245729-2-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231003074447.3245729-1-vivek.kasireddy@intel.com> References: <20231003074447.3245729-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: BCE9F14001D X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: j6a1fwfatwbz5ysiduii5qp669ncaxwq X-HE-Tag: 1696320327-456218 X-HE-Meta: U2Fs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BQAMpoPD8kbaztGZHq4b5HxqwOzRX3oHnlTM22HNi78UYoC6XfA1dx+LT3ZAEF9DWQZ2ELwOqBWE/aj8X+1wLgyJJ97dYk9x7Jkr5lpwjrwU1YC7ZxiZnJ4gKZ25WGMj0rZWLqy2YRQrvYiXgYaL4aTfEBVKuT3jIeRdLDDKuX9L0ZZxAA/lucdyPEwMkjMreVurMo0kXpiRixAhPPaVt4uWnttnrFDw= 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: 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 FOLL_PIN the pages but also to check and migrate them if they reside in movable zone or CMA block. For now, this API can only work with files belonging to shmem or hugetlbfs given that the udmabuf driver is the only user. It must be noted that the pages associated with hugetlbfs files are expected to be found in the page cache. An error is returned if they are not found. However, shmem pages can be swapped in or allocated if they are not present in the page cache. 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 Signed-off-by: Vivek Kasireddy --- include/linux/mm.h | 2 ++ mm/gup.c | 87 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) diff --git a/include/linux/mm.h b/include/linux/mm.h index bf5d0b1b16f4..af2121fb8101 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2457,6 +2457,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(int fd, pgoff_t start, unsigned long nr_pages, + unsigned int gup_flags, 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 2f8a2d89fde1..e34b77a15fa8 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -3400,3 +3400,90 @@ long pin_user_pages_unlocked(unsigned long start, unsigned long nr_pages, &locked, gup_flags); } EXPORT_SYMBOL(pin_user_pages_unlocked); + +/** + * pin_user_pages_fd() - pin user pages associated with a file + * @fd: the fd whose pages are to be pinned + * @start: starting file offset + * @nr_pages: number of pages from start to pin + * @gup_flags: flags modifying pin behaviour + * @pages: array that receives pointers to the pages pinned. + * Should be at least nr_pages long. + * + * Attempt to pin (and migrate) pages associated with a file belonging to + * either shmem or hugetlbfs. An error is returned if pages associated with + * hugetlbfs files are not present in the page cache. However, shmem pages + * are swapped in or allocated if they are not present in the page cache. + * + * Returns number of pages pinned. This would be equal to the number of + * pages requested. + * If nr_pages is 0 or negative, returns 0. If no pages were pinned, returns + * -errno. + */ +long pin_user_pages_fd(int fd, pgoff_t start, unsigned long nr_pages, + unsigned int gup_flags, struct page **pages) +{ + struct page *page; + struct file *filep; + unsigned int flags, i; + long ret; + + if (nr_pages <= 0) + return 0; + if (!is_valid_gup_args(pages, NULL, &gup_flags, FOLL_PIN)) + return 0; + + if (start < 0) + return -EINVAL; + + filep = fget(fd); + if (!filep) + return -EINVAL; + + if (!shmem_file(filep) && !is_file_hugepages(filep)) + return -EINVAL; + + flags = memalloc_pin_save(); + do { + for (i = 0; i < nr_pages; i++) { + if (shmem_mapping(filep->f_mapping)) { + page = shmem_read_mapping_page(filep->f_mapping, + start + i); + if (IS_ERR(page)) { + ret = PTR_ERR(page); + goto err; + } + } else { + page = find_get_page_flags(filep->f_mapping, + start + i, + FGP_ACCESSED); + if (!page) { + ret = -EINVAL; + 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); + +err: + memalloc_pin_restore(flags); + fput(filep); + if (!ret) + return nr_pages; + + while (i > 0 && pages[--i]) { + unpin_user_page(pages[i]); + pages[i] = NULL; + } + return ret; +} +EXPORT_SYMBOL_GPL(pin_user_pages_fd); +