Skip to contentSkip to conversation

mm/gup: introduce vaddr_pin_pages_remote(), FOLL_PIN

PATCH In review John Hubbard 2 participants 6 messages Aug 21, 2019 – Aug 23, 2019

Subsystems (6)
lore
Share & tools
lore

John Hubbard

author reviewer
#0 lore raw

v2 cover mm/gup: introduce vaddr_pin_pages_remote(), FOLL_PIN

Hi Ira,

This is for your tree. I'm dropping the RFC because this aspect is starting to firm up pretty well.

I've moved FOLL_PIN inside the vaddr_pin_*() routines, and moved FOLL_LONGTERM outside, based on our recent discussions. This is documented pretty well within the patches.

Note that there are a lot of references in comments and commit logs, to vaddr_pin_pages(). We'll want to catch all of those if we rename that. I am pushing pretty hard to rename it to vaddr_pin_user_pages().

v1 of this may be found here:
https://lore.kernel.org/r/[email protected]

John Hubbard (3):
For Ira: tiny formatting tweak to kerneldoc mm/gup: introduce FOLL_PIN flag for get_user_pages() mm/gup: introduce vaddr_pin_pages_remote(), and invoke it

 drivers/infiniband/core/umem.c |  1 +
 include/linux/mm.h             | 61 ++++++++++++++++++++++++++++++----
 mm/gup.c                       | 40 ++++++++++++++++++++--
 mm/process_vm_access.c         | 23 +++++++------
 4 files changed, 106 insertions(+), 19 deletions(-)
Signature · 1 line
2.22.1
author reviewer
#1 lore raw

PATCH v2 1/3 For Ira: tiny formatting tweak to kerneldoc

For your vaddr_pin_pages() and vaddr_unpin_pages(). Just merge it into wherever it goes please. Didn't want to cause merge problems so it's a separate patch-let.

---
 mm/gup.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
mm/gup.c +2 -2
index 56421b880325..e49096d012ea 100644
@@ -2465,7 +2465,7 @@ int get_user_pages_fast(unsigned long start, int nr_pages,
 EXPORT_SYMBOL_GPL(get_user_pages_fast);
 
 /**
- * vaddr_pin_pages pin pages by virtual address and return the pages to the
+ * vaddr_pin_pages() - pin pages by virtual address and return the pages to the
  * user.
  *
  * @addr: start address
@@ -2505,7 +2505,7 @@ long vaddr_pin_pages(unsigned long addr, unsigned long nr_pages,
 EXPORT_SYMBOL(vaddr_pin_pages);
 
 /**
- * vaddr_unpin_pages - counterpart to vaddr_pin_pages
+ * vaddr_unpin_pages() - counterpart to vaddr_pin_pages
  *
  * @pages: array of pages returned
  * @nr_pages: number of pages in pages
Signature · 1 line
2.22.1
author reviewer
#2 lore raw

PATCH v2 2/3 mm/gup: introduce FOLL_PIN flag for get_user_pages()

In reply to John Hubbard

As explained in the newly added documentation for FOLL_PIN and FOLL_LONGTERM, in every case where vaddr_pin_pages() is required, FOLL_PIN must be set. That reason, plus a desire to keep FOLL_PIN an internal (to get_user_pages() and follow_page()) detail, is why vaddr_pin_pages() sets FOLL_PIN.

FOLL_LONGTERM, on the other hand, in only set in *some* cases, but not all. For that reason, this patch moves the setting of FOLL_LONGTERM out to the caller.

Also add fairly extensive documentation of the meaning and use of both FOLL_PIN and FOLL_LONGTERM.

Thanks to Jan Kara and Vlastimil Babka for explaining the 4 cases in this documentation. (I've reworded it and expanded on it slightly.)

The motivation behind moving away from "bare" get_user_pages() calls is described in more detail in commit fc1d8e7cca2d
("mm: introduce put_user_page*(), placeholder versions").

Cc Vlastimil Babka <[email protected]>
Cc Jan Kara <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Ira Weiny <[email protected]>
---
 drivers/infiniband/core/umem.c |  1 +
 include/linux/mm.h             | 56 ++++++++++++++++++++++++++++++----
 mm/gup.c                       |  2 +-
 3 files changed, 52 insertions(+), 7 deletions(-)
drivers/infiniband/core/umem.c +1 -0
index e69eecb0023f..d84f1bfb8d21 100644
@@ -300,6 +300,7 @@ struct ib_umem *ib_umem_get(struct ib_udata *udata, unsigned long addr,
 
 	while (npages) {
 		down_read(&mm->mmap_sem);
+		gup_flags |= FOLL_LONGTERM;
 		ret = vaddr_pin_pages(cur_base,
 				     min_t(unsigned long, npages,
 					   PAGE_SIZE / sizeof (struct page *)),
include/linux/mm.h +50 -6

80 lines not on this page · raw message

mm/gup.c +1 -1
index e49096d012ea..ba316d960d7a 100644
@@ -2490,7 +2490,7 @@ long vaddr_pin_pages(unsigned long addr, unsigned long nr_pages,
 {
 	long ret;
 
-	gup_flags |= FOLL_LONGTERM;
+	gup_flags |= FOLL_PIN;
 
 	if (!vaddr_pin || (!vaddr_pin->mm && !vaddr_pin->f_owner))
 		return -EINVAL;
Signature · 1 line
2.22.1
author reviewer
#3 lore raw

PATCH v2 3/3 mm/gup: introduce vaddr_pin_pages_remote(), and invoke it

In reply to John Hubbard

vaddr_pin_user_pages_remote() is the "vaddr_pin_pages" corresponding variant to get_user_pages_remote(), except that:
a) it sets FOLL_PIN, and
b) it can handle FOLL_LONGTERM (and the associated vaddr_pin arg).

Change process_vm_rw_single_vec() to invoke the new function.

---
 include/linux/mm.h     |  5 +++++
 mm/gup.c               | 34 ++++++++++++++++++++++++++++++++++
 mm/process_vm_access.c | 23 +++++++++++++----------
 3 files changed, 52 insertions(+), 10 deletions(-)
include/linux/mm.h +5 -0
index 6e7de424bf5e..849b509e9f89 100644
@@ -1606,6 +1606,11 @@ int __account_locked_vm(struct mm_struct *mm, unsigned long pages, bool inc,
 long vaddr_pin_pages(unsigned long addr, unsigned long nr_pages,
 		     unsigned int gup_flags, struct page **pages,
 		     struct vaddr_pin *vaddr_pin);
+long vaddr_pin_user_pages_remote(struct task_struct *tsk, struct mm_struct *mm,
+				 unsigned long start, unsigned long nr_pages,
+				 unsigned int gup_flags, struct page **pages,
+				 struct vm_area_struct **vmas, int *locked,
+				 struct vaddr_pin *vaddr_pin);
 void vaddr_unpin_pages(struct page **pages, unsigned long nr_pages,
 		       struct vaddr_pin *vaddr_pin, bool make_dirty);
 bool mapping_inode_has_layout(struct vaddr_pin *vaddr_pin, struct page *page);
mm/gup.c +34 -0
index ba316d960d7a..d713ed9d4b9a 100644
@@ -2522,3 +2522,37 @@ void vaddr_unpin_pages(struct page **pages, unsigned long nr_pages,
 	__put_user_pages_dirty_lock(pages, nr_pages, make_dirty, vaddr_pin);
 }
 EXPORT_SYMBOL(vaddr_unpin_pages);
+
+/**
+ * vaddr_pin_user_pages_remote() - pin pages by virtual address and return the
+ * pages to the user.
+ *
+ * @tsk:	the task_struct to use for page fault accounting, or
+ *		NULL if faults are not to be recorded.
+ * @mm:		mm_struct of target mm
+ * @addr:	start address
+ * @nr_pages:	number of pages to pin
+ * @gup_flags:	flags to use for the pin. Please see FOLL_* documentation in
+ *		mm.h.
+ * @pages:	array of pages returned
+ * @vaddr_pin:  If FOLL_LONGTERM is set, then vaddr_pin should point to an
+ * initialized struct that contains the owning mm and file. Otherwise, vaddr_pin
+ * should be set to NULL.
+ *
+ * This is the "vaddr_pin_pages" corresponding variant to
+ * get_user_pages_remote(), except that:
+ *    a) it sets FOLL_PIN, and
+ *    b) it can handle FOLL_LONGTERM (and the associated vaddr_pin arg).
+ */
+long vaddr_pin_user_pages_remote(struct task_struct *tsk, struct mm_struct *mm,
+				 unsigned long start, unsigned long nr_pages,
+				 unsigned int gup_flags, struct page **pages,
+				 struct vm_area_struct **vmas, int *locked,
+				 struct vaddr_pin *vaddr_pin)
+{
+	gup_flags |= FOLL_TOUCH | FOLL_REMOTE | FOLL_PIN;
+
+	return __get_user_pages_locked(tsk, mm, start, nr_pages, pages, vmas,
+				       locked, gup_flags, vaddr_pin);
+}
+EXPORT_SYMBOL(vaddr_pin_user_pages_remote);
mm/process_vm_access.c +13 -10
index 357aa7bef6c0..28e0a17b6080 100644
@@ -44,7 +44,6 @@ static int process_vm_rw_pages(struct page **pages,
 
 		if (vm_write) {
 			copied = copy_page_from_iter(page, offset, copy, iter);
-			set_page_dirty_lock(page);
 		} else {
 			copied = copy_page_to_iter(page, offset, copy, iter);
 		}
@@ -96,7 +95,7 @@ static int process_vm_rw_single_vec(unsigned long addr,
 		flags |= FOLL_WRITE;
 
 	while (!rc && nr_pages && iov_iter_count(iter)) {
-		int pages = min(nr_pages, max_pages_per_loop);
+		int pinned_pages = min(nr_pages, max_pages_per_loop);
 		int locked = 1;
 		size_t bytes;
 
@@ -106,14 +105,17 @@ static int process_vm_rw_single_vec(unsigned long addr,
 		 * current/current->mm
 		 */
 		down_read(&mm->mmap_sem);
-		pages = get_user_pages_remote(task, mm, pa, pages, flags,
-					      process_pages, NULL, &locked);
+
+		pinned_pages = vaddr_pin_user_pages_remote(task, mm, pa,
+							   pinned_pages, flags,
+							   process_pages, NULL,
+							   &locked, NULL);
 		if (locked)
 			up_read(&mm->mmap_sem);
-		if (pages <= 0)
+		if (pinned_pages <= 0)
 			return -EFAULT;
 
-		bytes = pages * PAGE_SIZE - start_offset;
+		bytes = pinned_pages * PAGE_SIZE - start_offset;
 		if (bytes > len)
 			bytes = len;
 
@@ -122,10 +124,11 @@ static int process_vm_rw_single_vec(unsigned long addr,
 					 vm_write);
 		len -= bytes;
 		start_offset = 0;
-		nr_pages -= pages;
-		pa += pages * PAGE_SIZE;
-		while (pages)
-			put_page(process_pages[--pages]);
+		nr_pages -= pinned_pages;
+		pa += pinned_pages * PAGE_SIZE;
+
+		/* If vm_write is set, the pages need to be made dirty: */
+		vaddr_unpin_pages(process_pages, pinned_pages, NULL, vm_write);
 	}
 
 	return rc;
Signature · 1 line
2.22.1
#4 lore raw
In reply to John Hubbard
Quoted 9 lines · John Hubbard

Hi Ira,

This is for your tree. I'm dropping the RFC because this aspect is starting to firm up pretty well.

I've moved FOLL_PIN inside the vaddr_pin_*() routines, and moved FOLL_LONGTERM outside, based on our recent discussions. This is documented pretty well within the patches.

Quoted 7 lines

Note that there are a lot of references in comments and commit logs, to vaddr_pin_pages(). We'll want to catch all of those if we rename that. I am pushing pretty hard to rename it to vaddr_pin_user_pages().

v1 of this may be found here:
https://lore.kernel.org/r/[email protected]

I am really sorry about this...

I think it is fine to pull these in... There are some nits which are wrong but I think with the XDP complication and Daves' objection I think the vaddr_pin information is going to need reworking. So the documentation there is probably wrong. But until we know what it is going to be we should just take this.

Do you have a branch with this on it?

The patches don't seem to apply. Looks like they got corrupted somewhere...

:-/

Thanks,
Ira

Quoted 16 lines

John Hubbard (3):
For Ira: tiny formatting tweak to kerneldoc mm/gup: introduce FOLL_PIN flag for get_user_pages() mm/gup: introduce vaddr_pin_pages_remote(), and invoke it

 drivers/infiniband/core/umem.c |  1 +
 include/linux/mm.h             | 61 ++++++++++++++++++++++++++++++----
 mm/gup.c                       | 40 ++++++++++++++++++++--
 mm/process_vm_access.c         | 23 +++++++------
 4 files changed, 106 insertions(+), 19 deletions(-)
Signature · 1 line
2.22.1
author reviewer
#5 lore raw
Quoted 18 lines · Ira Weiny
Quoted 9 lines · John Hubbard

Hi Ira,

This is for your tree. I'm dropping the RFC because this aspect is starting to firm up pretty well.

I've moved FOLL_PIN inside the vaddr_pin_*() routines, and moved FOLL_LONGTERM outside, based on our recent discussions. This is documented pretty well within the patches.

Quoted 7 lines

Note that there are a lot of references in comments and commit logs, to vaddr_pin_pages(). We'll want to catch all of those if we rename that. I am pushing pretty hard to rename it to vaddr_pin_user_pages().

v1 of this may be found here:
https://lore.kernel.org/r/[email protected]

Quoted 7 lines

I am really sorry about this...

I think it is fine to pull these in... There are some nits which are wrong but I think with the XDP complication and Daves' objection I think the vaddr_pin information is going to need reworking. So the documentation there is probably wrong. But until we know what it is going to be we should just take this.

Sure, I was thinking the same thing: FOLL_PIN is clearing up, but vaddr_pin_pages() is still under heavy discussion.

Quoted 2 lines

Do you have a branch with this on it?

Yes, it's on: [email protected]:johnhubbard/linux.git , branch: vaddr_FOLL_PIN_next

Quoted 2 lines

The patches don't seem to apply. Looks like they got corrupted somewhere...

Lately I'm trying out .nvidia.com outgoing servers for git-send-email, so I'm still nervous about potential email-based patch problems. I suspect, though, that it's really just a "must be on exactly the right commit in order to apply" situation. Please let me know, so I can make any corrections necessary on this end.

thanks,

Signature · 2 lines
John Hubbard
NVIDIA