+ mm-gup-introduce-pin_user_pages_fd-for-pinning-shmem-hugetlbfs-file-pages.patch added to mm-unstable branch

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



The patch titled
     Subject: mm/gup: introduce pin_user_pages_fd() for pinning shmem/hugetlbfs file pages
has been added to the -mm mm-unstable branch.  Its filename is
     mm-gup-introduce-pin_user_pages_fd-for-pinning-shmem-hugetlbfs-file-pages.patch

This patch will shortly appear at
     https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/mm-gup-introduce-pin_user_pages_fd-for-pinning-shmem-hugetlbfs-file-pages.patch

This patch will later appear in the mm-unstable branch at
    git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm

Before you just go and hit "reply", please:
   a) Consider who else should be cc'ed
   b) Prefer to cc a suitable mailing list as well
   c) Ideally: find the original patch on the mailing list and do a
      reply-to-all to that, adding suitable additional cc's

*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***

The -mm tree is included into linux-next via the mm-everything
branch at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there every 2-3 working days

------------------------------------------------------
From: Vivek Kasireddy <vivek.kasireddy@xxxxxxxxx>
Subject: mm/gup: introduce pin_user_pages_fd() for pinning shmem/hugetlbfs file pages
Date: Tue, 3 Oct 2023 00:44:45 -0700

Patch series "mm/gup: Introduce pin_user_pages_fd() for pinning
shmem/hugetlbfs file pages".

The first patch introduces pin_user_pages_fd() API and the second patch
shows how the udmabuf driver can use it to longterm-pin the the pages. 
The last patch adds two new udmabuf selftests to verify data coherency
after page migration.


This patch (of 3):

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.

Link: https://lkml.kernel.org/r/20231003074447.3245729-1-vivek.kasireddy@xxxxxxxxx
Link: https://lkml.kernel.org/r/20231003074447.3245729-2-vivek.kasireddy@xxxxxxxxx
Signed-off-by: Vivek Kasireddy <vivek.kasireddy@xxxxxxxxx>
Suggested-by: Jason Gunthorpe <jgg@xxxxxxxxxx>
Cc: David Hildenbrand <david@xxxxxxxxxx>
Cc: Daniel Vetter <daniel.vetter@xxxxxxxx>
Cc: Mike Kravetz <mike.kravetz@xxxxxxxxxx>
Cc: Hugh Dickins <hughd@xxxxxxxxxx>
Cc: Peter Xu <peterx@xxxxxxxxxx>
Cc: Gerd Hoffmann <kraxel@xxxxxxxxxx>
Cc: Dongwon Kim <dongwon.kim@xxxxxxxxx>
Cc: Junxiao Chang <junxiao.chang@xxxxxxxxx>
Cc: Shuah Khan <shuah@xxxxxxxxxx>
Signed-off-by: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx>
---

 include/linux/mm.h |    2 
 mm/gup.c           |   87 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 89 insertions(+)

--- a/include/linux/mm.h~mm-gup-introduce-pin_user_pages_fd-for-pinning-shmem-hugetlbfs-file-pages
+++ a/include/linux/mm.h
@@ -2463,6 +2463,8 @@ long get_user_pages_unlocked(unsigned lo
 		    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);
--- a/mm/gup.c~mm-gup-introduce-pin_user_pages_fd-for-pinning-shmem-hugetlbfs-file-pages
+++ a/mm/gup.c
@@ -3410,3 +3410,90 @@ long pin_user_pages_unlocked(unsigned lo
 				     &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);
+
_

Patches currently in -mm which might be from vivek.kasireddy@xxxxxxxxx are

mm-gup-introduce-pin_user_pages_fd-for-pinning-shmem-hugetlbfs-file-pages.patch
udmabuf-pin-the-pages-using-pin_user_pages_fd-api.patch
selftests-dma-buf-udmabuf-add-tests-to-verify-data-after-page-migration.patch




[Index of Archives]     [Kernel Archive]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]

  Powered by Linux