get_user_pages*() and RDMA: first steps
Subsystems (7)
v5 cover get_user_pages*() and RDMA: first steps
From: John Hubbard <[email protected]>
Changes since v4:
-- Changed the new put_user_page*() functions to operate only on the head
page, because that's how the final version of those functions will work. (Andrew Morton's feedback prompted this, thanks!)
-- Added proper documentation of the new put_user_page*() functions.
-- Moved most of the new put_user_page*() functions out of the header file,
and into swap.c, because they have grown a little bigger than static inline functions should be. The trivial put_user_page() was left as a static inline for now, though.
-- Picked up Andrew Morton's Reviewed-by, for the first patch. I left
Jan's Reviewed-by in place for now, but we should verify that it still holds, with the various changes above. The main difference is the change to use the head page, the rest is just code movement and documentation.
-- Fixed a bug in the infiniband patch, found by the kbuild bot.
-- Rewrote the changelogs (and part of this cover letter) to be clearer.
Part of that is less reliance on links, and instead, just writing the steps directly.
Changes since v3:
-- Picks up Reviewed-by tags from Jan Kara and Dennis Dalessandro.
-- Picks up Acked-by tag from Jason Gunthorpe, in case this ends up *not*
going in via the RDMA tree.
-- Fixes formatting of a comment.
Changes since v2:
-- Absorbed more dirty page handling logic into the put_user_page*(), and
handled some page releasing loops in infiniband more thoroughly, as per Jason Gunthorpe's feedback.
-- Fixed a bug in the put_user_pages*() routines' loops (thanks to
Ralph Campbell for spotting it).
Changes since v1:
-- Renamed release_user_pages*() to put_user_pages*(), from Jan's feedback.
-- Removed the goldfish.c changes, and instead, only included a single
user (infiniband) of the new functions. That is because goldfish.c no longer has a name collision (it has a release_user_pages() routine), and also because infiniband exercises both the put_user_page() and put_user_pages*() paths.
-- Updated links to discussions and plans, so as to be sure to include
bounce buffers, thanks to Jerome's feedback.
Also:
This short series prepares for eventually fixing the problem described in [1]. The steps are:
1) (This patchset): Provide put_user_page*() routines, intended to be used for releasing pages that were pinned via get_user_pages*().
2) Convert all of the call sites for get_user_pages*(), to invoke put_user_page*(), instead of put_page(). This involves dozens of call sites, any will take some time. Patch 3/3 here kicks off the effort, by applying it to infiniband.
3) After (2) is complete, use get_user_pages*() and put_user_page*() to implement tracking of these pages. This tracking will be separate from the existing struct page refcounting.
4) Use the tracking and identification of these pages, to implement special handling (especially in writeback paths) when the pages are backed by a filesystem. Again, [1] provides details as to why that is desirable.
Patch 1, although not technically critical to do now, is still nice to have, because it's already been reviewed by Jan (and Andrew, now), and it's just one more thing on the long TODO list here, that is ready to be checked off.
Patch 2 is required in order to allow me (and others, if I'm lucky) to start submitting changes to convert all of the callsites of get_user_pages*() and put_page(). I think this will work a lot better than trying to maintain a massive patchset and submitting all at once.
Patch 3 converts infiniband drivers: put_page() --> put_user_page(), and also exercises put_user_pages_dirty_locked().
Once these are all in, then the floodgates can open up to convert the large number of remaining get_user_pages*() callsites.
[1] https://lwn.net/Articles/753027/ : "The Trouble with get_user_pages()"
[2] https://lkml.kernel.org/r/[email protected]
Proposed steps for fixing get_user_pages() + DMA problems.
[3]https://lkml.kernel.org/r/[email protected]
Bounce buffers (otherwise [2] is not really viable).
[4] https://lkml.kernel.org/r/[email protected]
Follow-up discussions.
John Hubbard (3):
mm: get_user_pages: consolidate error handling
mm: introduce put_user_page*(), placeholder versions infiniband/mm: convert put_page() to put_user_page*()
drivers/infiniband/core/umem.c | 7 +- drivers/infiniband/core/umem_odp.c | 2 +- drivers/infiniband/hw/hfi1/user_pages.c | 11 +-- drivers/infiniband/hw/mthca/mthca_memfree.c | 6 +- drivers/infiniband/hw/qib/qib_user_pages.c | 11 +-- drivers/infiniband/hw/qib/qib_user_sdma.c | 6 +- drivers/infiniband/hw/usnic/usnic_uiom.c | 7 +- include/linux/mm.h | 22 ++++++ mm/gup.c | 37 +++++---- mm/swap.c | 83 +++++++++++++++++++++ 10 files changed, 150 insertions(+), 42 deletions(-)
Signature · 1 line
2.19.1
PATCH v5 1/3 mm: get_user_pages: consolidate error handling
From: John Hubbard <[email protected]>
An upcoming patch requires a way to operate on each page that any of the get_user_pages_*() variants returns.
In preparation for that, consolidate the error handling for __get_user_pages(). This provides a single location (the "out:" label) for operating on the collected set of pages that are about to be returned.
As long every use of the "ret" variable is being edited, rename "ret" --> "err", so that its name matches its true role. This also gets rid of two shadowed variable declarations, as a tiny beneficial a side effect.
--- mm/gup.c | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-)
mm/gup.c +22 -15
@@ -660,6 +660,7 @@ static long __get_user_pages(struct task_struct *tsk, struct mm_struct *mm, struct vm_area_struct **vmas, int *nonblocking) { long i = 0; +int err =0 ;unsigned int page_mask;struct vm_area_struct *vma =NULL ;
@@ -685,18 +686,19 @@ static long __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,if (!vma || start >= vma->vm_end) { vma =find_extend_vma (mm, start);if (!vma &&in_gate_area (mm, start)) { -int ret; - ret =get_gate_page (mm, start &PAGE_MASK , + err =get_gate_page (mm, start &PAGE_MASK , gup_flags, &vma, pages ? &pages[i] :NULL ); -if (ret) -return i ? : ret; +if (err) + goto out; page_mask =0 ; goto next_page; } -if (!vma ||check_vma_flags (vma, gup_flags)) -return i ? : -EFAULT ; +if (!vma ||check_vma_flags (vma, gup_flags)) { + err = -EFAULT ; + goto out; + }if (is_vm_hugetlb_page (vma)) { i = follow_hugetlb_page(mm, vma, pages, vmas, &start, &nr_pages, i,
@@ -709,23 +711,25 @@ static long __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,* If we have a pending SIGKILL, don't keep faulting pages and * potentially allocating memory. */ -if (unlikely (fatal_signal_pending (current))) -return i ? i : -ERESTARTSYS ; +if (unlikely (fatal_signal_pending (current))) { + err = -ERESTARTSYS ; + goto out; + }cond_resched (); page =follow_page_mask (vma, start, foll_flags, &page_mask);if (!page) { -int ret; - ret =faultin_page (tsk, vma, start, &foll_flags, + err =faultin_page (tsk, vma, start, &foll_flags, nonblocking); -switch (ret) { +switch (err) {case 0 : goto retry;case -EFAULT :case -ENOMEM :case -EHWPOISON : -return i ? i : ret; + goto out;case -EBUSY : -return i; + err =0 ; + goto out;case -ENOENT : goto next_page; }
@@ -737,7 +741,8 @@ static long __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,*/ goto next_page; }else if (IS_ERR (page)) { -return i ? i :PTR_ERR (page); + err =PTR_ERR (page); + goto out; }if (pages) { pages[i] = page;
@@ -757,7 +762,9 @@ static long __get_user_pages(struct task_struct *tsk, struct mm_struct *mm, start += page_increm *PAGE_SIZE ; nr_pages -= page_increm; }while (nr_pages); -return i; + +out: +return i ? i : err; } static bool vma_permits_fault(struct vm_area_struct *vma,
Signature · 1 line
2.19.1
PATCH v5 2/3 mm: introduce put_user_page*(), placeholder versions
In reply to [email protected]From: John Hubbard <[email protected]>
Introduces put_user_page(), which simply calls put_page(). This provides a way to update all get_user_pages*() callers, so that they call put_user_page(), instead of put_page().
Also introduces put_user_pages(), and a few dirty/locked variations, as a replacement for release_pages(), and also as a replacement for open-coded loops that release multiple pages.
These may be used for subsequent performance improvements, via batching of pages to be released.
This is the first step of fixing the problem described in [1]. The steps are:
1) (This patch): provide put_user_page*() routines, intended to be used for releasing pages that were pinned via get_user_pages*().
2) Convert all of the call sites for get_user_pages*(), to invoke put_user_page*(), instead of put_page(). This involves dozens of call sites, any will take some time.
3) After (2) is complete, use get_user_pages*() and put_user_page*() to implement tracking of these pages. This tracking will be separate from the existing struct page refcounting.
4) Use the tracking and identification of these pages, to implement special handling (especially in writeback paths) when the pages are backed by a filesystem. Again, [1] provides details as to why that is desirable.
[1] https://lwn.net/Articles/753027/ : "The Trouble with get_user_pages()"
--- include/linux/mm.h | 20 +++++++++++ mm/swap.c | 83 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+)
include/linux/mm.h +20 -0
@@ -943,6 +943,26 @@ static inline void put_page(struct page *page)__put_page (page); } +/* +* put_user_page() - release a page that had previously been acquired via +* a call to one of the get_user_pages*() functions. +* +* Pages that were pinned via get_user_pages*() must be released via +* either put_user_page(), or one of the put_user_pages*() routines +* below. This is so that eventually, pages that are pinned via +* get_user_pages*() can be separately tracked and uniquely handled. In +* particular, interactions with RDMA and filesystems need special +* handling. +*/ +static inline void put_user_page (struct page *page) +{ +put_page (page); +} + +void put_user_pages_dirty (struct page **pages,unsigned long npages); +void put_user_pages_dirty_lock (struct page **pages,unsigned long npages); +void put_user_pages (struct page **pages,unsigned long npages); +#if defined(CONFIG_SPARSEMEM ) && !defined(CONFIG_SPARSEMEM_VMEMMAP )#define SECTION_IN_PAGE_FLAGS #endif
mm/swap.c +83 -0
89 lines not on this page · raw message
Signature · 1 line
2.19.1
Jan Kara
reviewerRe: [PATCH v5 2/3] mm: introduce put_user_page*(), placeholder versions
Quoted 8 lines · On Tue 09-10-18 21:11:33, [email protected] wrote:
+/*
+ *put_user_pages()- for each page in the @pages array, release the page
+ * usingput_user_page().
+ *
+ * Please see theput_user_page()documentation for details.
+ *
+ * This is just likeput_user_pages_dirty(), except that it invokes
+ *set_page_dirty_lock(), instead ofset_page_dirty().
This paragraph should be deleted. Other than that the patch looks good.
Honza
Quoted 20 lines
+ *
+ * @pages: array of pages to be marked dirty and released.
+ * @npages: number of pages in the @pages array.
+ *
+ */mm/swap.c +9
-0+void put_user_pages (struct page **pages,unsigned long npages) +{ +unsigned long index; + +for (index =0 ; index < npages; index++) +put_user_page (pages[index]); +} +EXPORT_SYMBOL (put_user_pages); +/* * get_kernel_pages() - pin kernel pages in memory * @kiov: An array of struct kvec structures Signature · 1 line
2.19.1
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
John Hubbard
reviewerRe: [PATCH v5 2/3] mm: introduce put_user_page*(), placeholder versions
Quoted 10 lines · Jan Kara
Quoted 8 lines · On Tue 09-10-18 21:11:33, [email protected] wrote:
+/*
+ *put_user_pages()- for each page in the @pages array, release the page
+ * usingput_user_page().
+ *
+ * Please see theput_user_page()documentation for details.
+ *
+ * This is just likeput_user_pages_dirty(), except that it invokes
+ *set_page_dirty_lock(), instead ofset_page_dirty().
Quoted 2 lines
This paragraph should be deleted. Other than that the patch looks good.
Good catch. Fixed locally, and it will go up with the next spin.
thanks,
Signature · 2 lines
John Hubbard NVIDIA
PATCH v5 3/3 infiniband/mm: convert put_page() to put_user_page*()
In reply to [email protected]From: John Hubbard <[email protected]>
For infiniband code that retains pages via get_user_pages*(), release those pages via the new put_user_page(), or put_user_pages*(), instead of put_page()
This is a tiny part of the second step of fixing the problem described in [1]. The steps are:
1) Provide put_user_page*() routines, intended to be used for releasing pages that were pinned via get_user_pages*().
2) Convert all of the call sites for get_user_pages*(), to invoke put_user_page*(), instead of put_page(). This involves dozens of call sites, any will take some time.
3) After (2) is complete, use get_user_pages*() and put_user_page*() to implement tracking of these pages. This tracking will be separate from the existing struct page refcounting.
4) Use the tracking and identification of these pages, to implement special handling (especially in writeback paths) when the pages are backed by a filesystem. Again, [1] provides details as to why that is desirable.
[1] https://lwn.net/Articles/753027/ : "The Trouble with get_user_pages()"
--- drivers/infiniband/core/umem.c | 7 ++++--- drivers/infiniband/core/umem_odp.c | 2 +- drivers/infiniband/hw/hfi1/user_pages.c | 11 ++++------- drivers/infiniband/hw/mthca/mthca_memfree.c | 6 +++--- drivers/infiniband/hw/qib/qib_user_pages.c | 11 ++++------- drivers/infiniband/hw/qib/qib_user_sdma.c | 6 +++--- drivers/infiniband/hw/usnic/usnic_uiom.c | 7 ++++--- 7 files changed, 23 insertions(+), 27 deletions(-)
drivers/infiniband/core/umem.c +4 -3
@@ -58,9 +58,10 @@ static void __ib_umem_release(struct ib_device *dev, struct ib_umem *umem, int dfor_each_sg (umem->sg_head.sgl, sg, umem->npages, i) { page =sg_page (sg); -if (!PageDirty (page) && umem->writable && dirty) -set_page_dirty_lock (page); -put_page (page); +if (umem->writable && dirty) +put_user_pages_dirty_lock (&page,1 ); +else +put_user_page (page); }sg_free_table (&umem->sg_head);
drivers/infiniband/core/umem_odp.c +1 -1
@@ -717,7 +717,7 @@ int ib_umem_odp_map_dma_pages(struct ib_umem *umem, u64 user_virt, u64 bcnt, ret = -EFAULT ;break ; } -put_page (local_page_list[j]); +put_user_page (local_page_list[j]);continue ; }
drivers/infiniband/hw/hfi1/user_pages.c +4 -7
@@ -121,13 +121,10 @@ int hfi1_acquire_user_pages(struct mm_struct *mm, unsigned long vaddr, size_t npvoid hfi1_release_user_pages (struct mm_struct *mm,struct page **p,size_t npages,bool dirty) { -size_t i; - -for (i =0 ; i < npages; i++) { -if (dirty) -set_page_dirty_lock (p[i]); -put_page (p[i]); - } +if (dirty) +put_user_pages_dirty_lock (p, npages); +else +put_user_pages (p, npages);if (mm) {/* during close after signal, mm can be NULL */ down_write (&mm->mmap_sem);
drivers/infiniband/hw/mthca/mthca_memfree.c +3 -3
@@ -481,7 +481,7 @@ int mthca_map_user_db(struct mthca_dev *dev, struct mthca_uar *uar, ret =pci_map_sg (dev->pdev, &db_tab->page[i].mem,1 ,PCI_DMA_TODEVICE );if (ret <0 ) { -put_page (pages[0 ]); +put_user_page (pages[0 ]); goto out; }
@@ -489,7 +489,7 @@ int mthca_map_user_db(struct mthca_dev *dev, struct mthca_uar *uar, mthca_uarc_virt(dev, uar, i));if (ret) {pci_unmap_sg (dev->pdev, &db_tab->page[i].mem,1 ,PCI_DMA_TODEVICE ); -put_page (sg_page (&db_tab->page[i].mem)); +put_user_page (sg_page (&db_tab->page[i].mem)); goto out; }
@@ -555,7 +555,7 @@ void mthca_cleanup_user_db_tab(struct mthca_dev *dev, struct mthca_uar *uar,if (db_tab->page[i].uvirt) {mthca_UNMAP_ICM (dev,mthca_uarc_virt (dev, uar, i),1 );pci_unmap_sg (dev->pdev, &db_tab->page[i].mem,1 ,PCI_DMA_TODEVICE ); -put_page (sg_page (&db_tab->page[i].mem)); +put_user_page (sg_page (&db_tab->page[i].mem)); } }
drivers/infiniband/hw/qib/qib_user_pages.c +4 -7
@@ -40,13 +40,10 @@static void __qib_release_user_pages (struct page **p,size_t num_pages,int dirty) { -size_t i; - -for (i =0 ; i < num_pages; i++) { -if (dirty) -set_page_dirty_lock (p[i]); -put_page (p[i]); - } +if (dirty) +put_user_pages_dirty_lock (p, num_pages); +else +put_user_pages (p, num_pages); }/*
drivers/infiniband/hw/qib/qib_user_sdma.c +3 -3
@@ -321,7 +321,7 @@ static int qib_user_sdma_page_to_frags(const struct qib_devdata *dd,* the caller can ignore this page. */ if (put) { -put_page (page); +put_user_page (page); }else {/* coalesce case */ kunmap (page);
@@ -635,7 +635,7 @@ static void qib_user_sdma_free_pkt_frag(struct device *dev,kunmap (pkt->addr[i].page);if (pkt->addr[i].put_page) -put_page (pkt->addr[i].page); +put_user_page (pkt->addr[i].page);else __free_page (pkt->addr[i].page); }else if (pkt->addr[i].kvaddr) {
@@ -710,7 +710,7 @@ static int qib_user_sdma_pin_pages(const struct qib_devdata *dd,/* if error, return all pages not managed by pkt */ free_pages:while (i < j) -put_page (pages[i++]); +put_user_page (pages[i++]); done:return ret;
drivers/infiniband/hw/usnic/usnic_uiom.c +4 -3
@@ -89,9 +89,10 @@ static void usnic_uiom_put_pages(struct list_head *chunk_list, int dirty)for_each_sg (chunk->page_list, sg, chunk->nents, i) { page =sg_page (sg); pa =sg_phys (sg); -if (!PageDirty (page) && dirty) -set_page_dirty_lock (page); -put_page (page); +if (dirty) +put_user_pages_dirty_lock (&page,1 ); +else +put_user_page (page);usnic_dbg ("pa: %pa\n" , &pa); }kfree (chunk);
Signature · 1 line
2.19.1