mm/gup: introduce vaddr_pin_pages_remote(), FOLL_PIN
Subsystems (6)
[email protected]
reviewerv2 cover mm/gup: introduce vaddr_pin_pages_remote(), FOLL_PIN
From: John Hubbard <[email protected]>
Hi Ira,
As requested, this is for your tree:
https://github.com/weiny2/linux-kernel.git (mmotm-rdmafsdax-b0-v4), to be applied at your last authored commit, which is: commit f625f92ecfb4 ("mm/gup: Remove FOLL_LONGTERM DAX exclusion"). In other words, please delete my previous patches from the tree, and apply these replacement patches.
This now has a user for the new vaddr_pin_user_pages_remote() call. And it also moves the gup flag setting out to the caller.
I'm pretty pleased to be able to include a bit of documentation (see the FOLL_PIN patch) that covers those four cases. This should really help clarify things. Thanks to Jan Kara and Vlastimil Babka for providing the meaingful core of that documentation.
The naming can of course be tweaked to match whatever the final is. For now, I've used vaddr_pin_user_pages_remote(). That addresses Jason's request for a "user" in the name, and it also makes it very clear that it's a replacement for get_user_pages_remote().
v1 of this RFC is 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
include/linux/mm.h | 61 +++++++++++++++++++++++++++++++++++++----- mm/gup.c | 37 +++++++++++++++++++++++-- mm/process_vm_access.c | 23 +++++++++------- 3 files changed, 104 insertions(+), 17 deletions(-)
Signature · 1 line
2.22.1
[email protected]
reviewerRFC PATCH v2 1/3 For Ira: tiny formatting tweak to kerneldoc
From: John Hubbard <[email protected]>
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
@@ -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
[email protected]
reviewerRFC PATCH v2 2/3 mm/gup: introduce FOLL_PIN flag for get_user_pages()
In reply to [email protected]From: John Hubbard <[email protected]>
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nvidia.com; s=n1;
t=1566008674; bh=g5Mai0va6k/z2enpQJ4Nfvbj5WByFxGAO1JwdIBbXio=; h=X-PGP-Universal:From:To:CC:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:X-NVConfidentiality: Content-Transfer-Encoding:Content-Type; b=d6UDSde9XF/IsNteBaYOBWeKiHhWmeU9ekUJNvCviHssBDCtw0T+M/2TlEPEzomIT fGXzIQNlGN6MXFbaBoyBmF/zjCu02TmTNExbVJ3/5N6PTyOuJFCx9ZN1/5gXsB11m1 xAHIWE+VOZs4qqDeHDBqKZq+FaxQHNvGz0j6lyVBA70TfseNoZqZZrSil8uvaKJwKd TQ1ht+AGWbw9p610JmaPb4u6o/eV6Ns8Sl3EVnjWWu94T6ISNIaWCiC6wQQF6L1YCH G5Pjn+0rEjhk6XG4TyLudi5lWp3IVBHd8+WlWlnl+bvLCC55RUAjPJLn7LaVyVdh0F nLHwm3bN2Jotg==
FOLL_PIN is set by callers of vaddr_pin_pages(). This is different than FOLL_LONGTERM, because even short term page pins need a new kind of tracking, if those pinned pages' data is going to potentially be modified.
This situation is described in more detail in commit fc1d8e7cca2d ("mm: introduce put_user_page*(), placeholder versions").
FOLL_PIN is added now, rather than waiting until there is code that takes action based on FOLL_PIN. That's because having FOLL_PIN in the code helps to highlight the differences between:
a) get_user_pages(): soon to be deprecated. Used to pin pages, but without awareness of file systems that might use those pages,
b) The original vaddr_pin_pages(): intended only for FOLL_LONGTERM and DAX use cases. This assumes direct IO and therefore is not applicable the most of the other callers of get_user_pages(), and
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.)
--- include/linux/mm.h | 56 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 50 insertions(+), 6 deletions(-)
include/linux/mm.h +0 -0
@@ -2644,6 +2644,8 @@ static inline vm_fault_t vmf_error(int err) struct page *follow_page(struct vm_area_struct *vma, unsigned long address=
,
unsigned int foll_flags);
=20
+/* Flags for follow_page(), get_user_pages ("GUP"), and vaddr_pin_pages():=
*/
+
#define FOLL_WRITE 0x01 /* check pte is writable */
#define FOLL_TOUCH 0x02 /* mark page accessed */
#define FOLL_GET 0x04 /* do get_page on page */
@@ -2663,13 +2665,15 @@ struct page *follow_page(struct vm_area_struct *vma= , unsigned long address,
#define FOLL_ANON 0x8000 /* don't do file mappings */
#define FOLL_LONGTERM 0x10000 /* mapping lifetime is indefinite: see below=
*/
#define FOLL_SPLIT_PMD 0x20000 /* split huge pmd before returning */
+#define FOLL_PIN 0x40000 /* pages must be released via put_user_page() */
=20
/*
- * NOTE on FOLL_LONGTERM:
+ * FOLL_PIN and FOLL_LONGTERM may be used in various combinations with eac= h
+ * other. Here is what they mean, and how to use them:
*
* FOLL_LONGTERM indicates that the page will be held for an indefinite ti=
me
- * period _often_ under userspace control. This is contrasted with
- * iov_iter_get_pages() where usages which are transient.
+ * period _often_ under userspace control. This is in contrast to
+ * iov_iter_get_pages(), where usages which are transient.
*
* FIXME: For pages which are part of a filesystem, mappings are subject t=
o the
* lifetime enforced by the filesystem and we need guarantees that longter=
m
@@ -2684,11 +2688,51 @@ struct page *follow_page(struct vm_area_struct *vma= , unsigned long address,
* Currently only get_user_pages() and get_user_pages_fast() support this =
flag
* and calls to get_user_pages_[un]locked are specifically not allowed. T=
his
* is due to an incompatibility with the FS DAX check and
- * FAULT_FLAG_ALLOW_RETRY
+ * FAULT_FLAG_ALLOW_RETRY.
*
- * In the CMA case: longterm pins in a CMA region would unnecessarily frag= ment
- * that region. And so CMA attempts to migrate the page before pinning wh= en
+ * In the CMA case: long term pins in a CMA region would unnecessarily fra= gment
+ * that region. And so, CMA attempts to migrate the page before pinning, = when
* FOLL_LONGTERM is specified.
+ *
+ * FOLL_PIN indicates that a special kind of tracking (not just page->_ref= count,
+ * but an additional pin counting system) will be invoked. This is intende= d for
+ * anything that gets a page reference and then touches page data (for exa= mple,
+ * Direct IO). This lets the filesystem know that some non-file-system ent= ity is
+ * potentially changing the pages' data. FOLL_PIN pages must be released,
+ * ultimately, by a call to put_user_page(). Typically that will be via on= e of
+ * the vaddr_unpin_pages() variants.
+ *
+ * FIXME: note that this special tracking is not in place yet. However, th= e
+ * pages should still be released by put_user_page().
+ *
+ * When and where to use each flag:
+ *
+ * CASE 1: Direct IO (DIO). There are GUP references to pages that are ser= ving
+ * as DIO buffers. These buffers are needed for a relatively short time (s=
o they
+ * are not "long term"). No special synchronization with page_mkclean() or
+ * munmap() is provided. Therefore, flags to set at the call site are:
+ *
+ * FOLL_PIN
+ *
+ * CASE 2: RDMA. There are GUP references to pages that are serving as DMA
+ * buffers. These buffers are needed for a long time ("long term"). No spe= cial
+ * synchronization with page_mkclean() or munmap() is provided. Therefore,= flags
+ * to set at the call site are:
+ *
+ * FOLL_PIN | FOLL_LONGTERM
+ *
+ * There is also a special case when the pages are DAX pages: in addition = to the
+ * above flags, the caller needs a file lease. This is provided via the st= ruct
+ * vaddr_pin argument to vaddr_pin_pages().
+ *
+ * CASE 3: ODP (Mellanox/Infiniband On Demand Paging: the hardware support= s
+ * replayable page faulting). There are GUP references to pages serving as= DMA
+ * buffers. For ODP, MMU notifiers are used to synchronize with page_mkcle= an()
+ * and munmap(). Therefore, normal GUP calls are sufficient, so neither fl= ag
+ * needs to be set.
+ *
+ * CASE 4: pinning for struct page manipulation only. Here, normal GUP cal= ls are
+ * sufficient, so neither flag needs to be set.
*/
=20
static inline int vm_fault_to_errno(vm_fault_t vm_fault, int foll_flags)
--=20
2.22.1
[email protected]
reviewerRFC PATCH v2 3/3 mm/gup: introduce vaddr_pin_pages_remote(), and invoke it
In reply to [email protected]From: John Hubbard <[email protected]>
vaddr_pin_user_pages_remote() is the "vaddr_pin_pages" corresponding variant to get_user_pages_remote(): it adds the ability to handle FOLL_PIN, FOLL_LONGTERM, or both.
Note that the put_user_page*() requirement won't be truly required until all of the call sites have been converted, and the tracking of pages is activated.
Also, change process_vm_rw_single_vec() to invoke the new function.
--- include/linux/mm.h | 5 +++++ mm/gup.c | 33 +++++++++++++++++++++++++++++++++ mm/process_vm_access.c | 23 ++++++++++++++--------- 3 files changed, 52 insertions(+), 9 deletions(-)
include/linux/mm.h +5 -0
@@ -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 +33 -0
@@ -2522,3 +2522,36 @@ 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(), but with the ability to handle FOLL_PIN, +* FOLL_LONGTERM, or both. +*/ +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 ; + +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 +14 -9
@@ -96,7 +96,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 +106,18 @@ 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); + + flags |=FOLL_PIN ; + 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 +126,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
John Hubbard
reviewerRe: [RFC PATCH v2 2/3] mm/gup: introduce FOLL_PIN flag for get_user_pages()
In reply to [email protected]Quoted 11 lines · On 8/16/19 7:24 PM, [email protected] wrote:
From: John Hubbard <[email protected]>
DKIM-Signature: v a a-sha256; claxed/relaxed; d idia.com; s;t66008674; bhMai0va6k/z2enpQJ4Nfvbj5WByFxGAO1JwdIBbXio h PGP-Universal:From:To:CC:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:X-NVConfidentiality: Content-Transfer-Encoding:Content-Type; bÖUDSde9XF/IsNteBaYOBWeKiHhWmeU9ekUJNvCviHssBDCtw0T+M/2TlEPEzomIT fGXzIQNlGN6MXFbaBoyBmF/zjCu02TmTNExbVJ3/5N6PTyOuJFCx9ZN1/5gXsB11m1 xAHIWE+VOZs4qqDeHDBqKZq+FaxQHNvGz0j6lyVBA70TfseNoZqZZrSil8uvaKJwKd TQ1ht+AGWbw9p610JmaPb4u6o/eV6Ns8Sl3EVnjWWu94T6ISNIaWCiC6wQQF6L1YCH G5Pjn+0rEjhk6XG4TyLudi5lWp3IVBHd8+WlWlnl+bvLCC55RUAjPJLn7LaVyVdh0F nLHwm3bN2Jotg
I cannot readily explain the above email glitch, but I did just now switch back to mailgw.nvidia.com for this patchset, in order to get the nice behavior of having "From:" really be my native NVIDIA email address. That's very nice, but if the glitches happen again, I'll switch back to using gmail for git-send-email.
Sorry about the weirdness. It does still let you apply the patch, I just now checked on that.
thanks,
Signature · 2 lines
John Hubbard NVIDIA
John Hubbard
reviewerRe: [RFC PATCH v2 2/3] mm/gup: introduce FOLL_PIN flag for get_user_pages()
Quoted 13 lines · John Hubbard
Quoted 11 lines · On 8/16/19 7:24 PM, [email protected] wrote:
From: John Hubbard <[email protected]>
DKIM-Signature: v a a-sha256; claxed/relaxed; d idia.com; s;t66008674; bhMai0va6k/z2enpQJ4Nfvbj5WByFxGAO1JwdIBbXio h PGP-Universal:From:To:CC:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:X-NVConfidentiality: Content-Transfer-Encoding:Content-Type; bÖUDSde9XF/IsNteBaYOBWeKiHhWmeU9ekUJNvCviHssBDCtw0T+M/2TlEPEzomIT fGXzIQNlGN6MXFbaBoyBmF/zjCu02TmTNExbVJ3/5N6PTyOuJFCx9ZN1/5gXsB11m1 xAHIWE+VOZs4qqDeHDBqKZq+FaxQHNvGz0j6lyVBA70TfseNoZqZZrSil8uvaKJwKd TQ1ht+AGWbw9p610JmaPb4u6o/eV6Ns8Sl3EVnjWWu94T6ISNIaWCiC6wQQF6L1YCH G5Pjn+0rEjhk6XG4TyLudi5lWp3IVBHd8+WlWlnl+bvLCC55RUAjPJLn7LaVyVdh0F nLHwm3bN2Jotg
Quoted 9 lines
I cannot readily explain the above email glitch, but I did just now switch back to mailgw.nvidia.com for this patchset, in order to get the nice behavior of having "From:" really be my native NVIDIA email address. That's very nice, but if the glitches happen again, I'll switch back to using gmail for git-send-email.
Sorry about the weirdness. It does still let you apply the patch, I just now checked on that.
Hi Ira, could you please let me know if you'd like me to repost this patch, or the entire patchset, or if you're able to deal with it as-is? As it stands, the DKIM-Signature cruft above needs to be manually removed, either from the patch, or from the commit log after applying the patch.
Also, as noted in the email thread involving Bharath and sgi-gru [1], I'm currently planning on branching from your tree, and continuing the misc call site conversions from there. And then just adapting to whatever API changes are made to vaddr_*() functions. And the biovec call site conversions should be based on that as well.
[1] https://lore.kernel.org/r/[email protected]
thanks,
Signature · 2 lines
John Hubbard NVIDIA