Skip to contentSkip to conversation

mm/gup: add make_dirty arg to put_user_pages_dirty_lock()

PATCH Superseded [email protected] 5 participants 9 messages Jul 30, 2019 – Aug 1, 2019

Subsystems (14)

v4 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v4 cover mm/gup: add make_dirty arg to put_user_pages_dirty_lock()

From: John Hubbard <[email protected]>

Changes since v3:

* Fixed an unused variable warning in siw_mem.c

Changes since v2:

* Critical bug fix: remove a stray "break;" from the new routine.

Changes since v1:

* Instead of providing __put_user_pages(), add an argument to put_user_pages_dirty_lock(), and delete put_user_pages_dirty(). This is based on the following points:

1. Lots of call sites become simpler if a bool is passed into put_user_page*(), instead of making the call site choose which put_user_page*() variant to call.

2. Christoph Hellwig's observation that set_page_dirty_lock() is usually correct, and set_page_dirty() is usually a bug, or at least questionable, within a put_user_page*() calling chain.

* Added the Infiniband driver back to the patch series, because it is a caller of put_user_pages_dirty_lock().

Unchanged parts from the v1 cover letter (except for the diffstat):

Notes about the remaining patches to come:

There are about 50+ patches in my tree [2], and I'll be sending out the remaining ones in a few more groups:

* The block/bio related changes (Jerome mostly wrote those, but I've had to move stuff around extensively, and add a little code)

* mm/ changes

* other subsystem patches

* an RFC that shows the current state of the tracking patch set. That can only be applied after all call sites are converted, but it's good to get an early look at it.

This is part a tree-wide conversion, as described in commit fc1d8e7cca2d ("mm: introduce put_user_page*(), placeholder versions").

John Hubbard (3):
mm/gup: add make_dirty arg to put_user_pages_dirty_lock() drivers/gpu/drm/via: convert put_page() to put_user_page*() net/xdp: convert put_page() to put_user_page*()

 drivers/gpu/drm/via/via_dmablit.c          |  10 +-
 drivers/infiniband/core/umem.c             |   5 +-
 drivers/infiniband/hw/hfi1/user_pages.c    |   5 +-
 drivers/infiniband/hw/qib/qib_user_pages.c |   5 +-
 drivers/infiniband/hw/usnic/usnic_uiom.c   |   5 +-
 drivers/infiniband/sw/siw/siw_mem.c        |  10 +-
 include/linux/mm.h                         |   5 +-
 mm/gup.c                                   | 115 +++++++++------------
 net/xdp/xdp_umem.c                         |   9 +-
 9 files changed, 61 insertions(+), 108 deletions(-)
Signature · 1 line
2.22.0
#1 lore raw

PATCH v4 1/3 mm/gup: add make_dirty arg to put_user_pages_dirty_lock()

From: John Hubbard <[email protected]>

Provide more capable variation of put_user_pages_dirty_lock(), and delete put_user_pages_dirty(). This is based on the following:

1. Lots of call sites become simpler if a bool is passed into put_user_page*(), instead of making the call site choose which put_user_page*() variant to call.

2. Christoph Hellwig's observation that set_page_dirty_lock() is usually correct, and set_page_dirty() is usually a bug, or at least questionable, within a put_user_page*() calling chain.

This leads to the following API choices:

* put_user_pages_dirty_lock(page, npages, make_dirty)

* There is no put_user_pages_dirty(). You have to hand code that, in the rare case that it's required.

Cc Matthew Wilcox <[email protected]>
Cc Jan Kara <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc Ira Weiny <[email protected]>
Cc Jason Gunthorpe <[email protected]>
---
 drivers/infiniband/core/umem.c             |   5 +-
 drivers/infiniband/hw/hfi1/user_pages.c    |   5 +-
 drivers/infiniband/hw/qib/qib_user_pages.c |   5 +-
 drivers/infiniband/hw/usnic/usnic_uiom.c   |   5 +-
 drivers/infiniband/sw/siw/siw_mem.c        |  10 +-
 include/linux/mm.h                         |   5 +-
 mm/gup.c                                   | 115 +++++++++------------
 7 files changed, 58 insertions(+), 92 deletions(-)
drivers/infiniband/core/umem.c +1 -4
index 08da840ed7ee..965cf9dea71a 100644
@@ -54,10 +54,7 @@ static void __ib_umem_release(struct ib_device *dev, struct ib_umem *umem, int d
 
 	for_each_sg_page(umem->sg_head.sgl, &sg_iter, umem->sg_nents, 0) {
 		page = sg_page_iter_page(&sg_iter);
-		if (umem->writable && dirty)
-			put_user_pages_dirty_lock(&page, 1);
-		else
-			put_user_page(page);
+		put_user_pages_dirty_lock(&page, 1, umem->writable && dirty);
 	}
 
 	sg_free_table(&umem->sg_head);
drivers/infiniband/hw/hfi1/user_pages.c +1 -4
index b89a9b9aef7a..469acb961fbd 100644
@@ -118,10 +118,7 @@ int hfi1_acquire_user_pages(struct mm_struct *mm, unsigned long vaddr, size_t np
 void hfi1_release_user_pages(struct mm_struct *mm, struct page **p,
 			     size_t npages, bool dirty)
 {
-	if (dirty)
-		put_user_pages_dirty_lock(p, npages);
-	else
-		put_user_pages(p, npages);
+	put_user_pages_dirty_lock(p, npages, dirty);
 
 	if (mm) { /* during close after signal, mm can be NULL */
 		atomic64_sub(npages, &mm->pinned_vm);
drivers/infiniband/hw/qib/qib_user_pages.c +1 -4
index bfbfbb7e0ff4..6bf764e41891 100644
@@ -40,10 +40,7 @@
 static void __qib_release_user_pages(struct page **p, size_t num_pages,
 				     int dirty)
 {
-	if (dirty)
-		put_user_pages_dirty_lock(p, num_pages);
-	else
-		put_user_pages(p, num_pages);
+	put_user_pages_dirty_lock(p, num_pages, dirty);
 }
 
 /**
drivers/infiniband/hw/usnic/usnic_uiom.c +1 -4
index 0b0237d41613..62e6ffa9ad78 100644
@@ -75,10 +75,7 @@ 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 (dirty)
-				put_user_pages_dirty_lock(&page, 1);
-			else
-				put_user_page(page);
+			put_user_pages_dirty_lock(&page, 1, dirty);
 			usnic_dbg("pa: %pa\n", &pa);
 		}
 		kfree(chunk);
drivers/infiniband/sw/siw/siw_mem.c +1 -9
index 67171c82b0c4..ab83a9cec562 100644
@@ -63,15 +63,7 @@ struct siw_mem *siw_mem_id2obj(struct siw_device *sdev, int stag_index)
 static void siw_free_plist(struct siw_page_chunk *chunk, int num_pages,
 			   bool dirty)
 {
-	struct page **p = chunk->plist;
-
-	while (num_pages--) {
-		if (!PageDirty(*p) && dirty)
-			put_user_pages_dirty_lock(p, 1);
-		else
-			put_user_page(*p);
-		p++;
-	}
+	put_user_pages_dirty_lock(chunk->plist, num_pages, dirty);
 }
 
 void siw_umem_release(struct siw_umem *umem, bool dirty)
include/linux/mm.h +3 -2
index 0334ca97c584..9759b6a24420 100644
@@ -1057,8 +1057,9 @@ 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_dirty_lock(struct page **pages, unsigned long npages,
+			       bool make_dirty);
+
 void put_user_pages(struct page **pages, unsigned long npages);
 
 #if defined(CONFIG_SPARSEMEM) && !defined(CONFIG_SPARSEMEM_VMEMMAP)
mm/gup.c +50 -65

135 lines not on this page · raw message

Signature · 1 line
2.22.0
#2 lore raw

drivers/infiniband/hw/qib/qib_user_pages.c line 44

Quoted 10 lines · On Tue, Jul 30, 2019 at 01:57:03PM -0700, [email protected] wrote:
drivers/infiniband/hw/qib/qib_user_pages.c +1 -4
@@ -40,10 +40,7 @@
 static void __qib_release_user_pages(struct page **p, size_t num_pages,
 				     int dirty)
 {
-	if (dirty)
-		put_user_pages_dirty_lock(p, num_pages);
-	else
-		put_user_pages(p, num_pages);
+	put_user_pages_dirty_lock(p, num_pages, dirty);
 }

__qib_release_user_pages should be removed now as a direct call to put_user_pages_dirty_lock is a lot more clear.

drivers/infiniband/hw/usnic/usnic_uiom.c static void usnic_uiom_put_pages(struct list_head *chunk_list, int dirty) · line 79

Quoted 13 lines

index 0b0237d41613..62e6ffa9ad78 100644

drivers/infiniband/hw/usnic/usnic_uiom.c +1 -4
@@ -75,10 +75,7 @@ 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 (dirty)
-				put_user_pages_dirty_lock(&page, 1);
-			else
-				put_user_page(page);
+			put_user_pages_dirty_lock(&page, 1, dirty);
 			usnic_dbg("pa: %pa\n", &pa);

There is a pre-existing bug here, as this needs to use the sg_page iterator. Probably worth throwing in a fix into your series while you are at it.

Quoted 4 lines
drivers/infiniband/hw/usnic/usnic_uiom.c +0 -0
@@ -63,15 +63,7 @@ struct siw_mem *siw_mem_id2obj(struct siw_device *sdev, int stag_index)
 static void siw_free_plist(struct siw_page_chunk *chunk, int num_pages,
 			   bool dirty)
 {

drivers/infiniband/hw/usnic/usnic_uiom.c struct siw_mem *siw_mem_id2obj(struct siw_device *sdev, int stag_index) · line 66

Quoted 10 lines
drivers/infiniband/hw/usnic/usnic_uiom.c +1 -9
-	struct page **p = chunk->plist;
-
-	while (num_pages--) {
-		if (!PageDirty(*p) && dirty)
-			put_user_pages_dirty_lock(p, 1);
-		else
-			put_user_page(*p);
-		p++;
-	}
+	put_user_pages_dirty_lock(chunk->plist, num_pages, dirty);

siw_free_plist should just go away now.

Otherwise this looks good to me.

reviewer
#3 lore raw
Quoted 14 lines · Christoph Hellwig

drivers/infiniband/hw/qib/qib_user_pages.c line 44

Quoted 10 lines · On Tue, Jul 30, 2019 at 01:57:03PM -0700, [email protected] wrote:
drivers/infiniband/hw/qib/qib_user_pages.c +1 -4
@@ -40,10 +40,7 @@
 static void __qib_release_user_pages(struct page **p, size_t num_pages,
 				     int dirty)
 {
-	if (dirty)
-		put_user_pages_dirty_lock(p, num_pages);
-	else
-		put_user_pages(p, num_pages);
+	put_user_pages_dirty_lock(p, num_pages, dirty);
 }

__qib_release_user_pages should be removed now as a direct call to put_user_pages_dirty_lock is a lot more clear.

OK.

Quoted 10 lines
Quoted 10 lines

index 0b0237d41613..62e6ffa9ad78 100644

drivers/infiniband/hw/usnic/usnic_uiom.c +0 -2
@@ -75,10 +75,7 @@ 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 (dirty)
-				put_user_pages_dirty_lock(&page, 1);
Quoted 8 lines

drivers/infiniband/hw/usnic/usnic_uiom.c static void usnic_uiom_put_pages(struct list_head *chunk_list, int dirty) · line 79

Quoted 4 lines
drivers/infiniband/hw/usnic/usnic_uiom.c +1 -2
-			else
-				put_user_page(page);
+			put_user_pages_dirty_lock(&page, 1, dirty);
 			usnic_dbg("pa: %pa\n", &pa);

There is a pre-existing bug here, as this needs to use the sg_page iterator. Probably worth throwing in a fix into your series while you are at it.

The amount of scatterlist code I've written is approximately zero lines, +/- a few lines. :) I thought for_each_sg() *was* the sg_page iterator...

I'll be glad to post a fix, but I'm not yet actually spotting the bug! heh

Quoted 17 lines
Quoted 5 lines
drivers/infiniband/hw/usnic/usnic_uiom.c +0 -0
@@ -63,15 +63,7 @@ struct siw_mem *siw_mem_id2obj(struct siw_device *sdev, int stag_index)
 static void siw_free_plist(struct siw_page_chunk *chunk, int num_pages,
 			   bool dirty)
 {

drivers/infiniband/hw/usnic/usnic_uiom.c struct siw_mem *siw_mem_id2obj(struct siw_device *sdev, int stag_index) · line 66

Quoted 10 lines
drivers/infiniband/hw/usnic/usnic_uiom.c +1 -9
-	struct page **p = chunk->plist;
-
-	while (num_pages--) {
-		if (!PageDirty(*p) && dirty)
-			put_user_pages_dirty_lock(p, 1);
-		else
-			put_user_page(*p);
-		p++;
-	}
+	put_user_pages_dirty_lock(chunk->plist, num_pages, dirty);

siw_free_plist should just go away now.

OK, yes.

Quoted 3 lines

Otherwise this looks good to me.

Great, I'll make the above changes and post an updated series with your Reviewed-by, and Bjorn's ACK for patch #3.

Next: I've just finished sweeping through a bunch of patches and applying this where applicable, so now that this API seems acceptable, I'll post another chunk of put_user_page*() conversions.

thanks,

Signature · 2 lines
John Hubbard
NVIDIA
#4 lore raw
In reply to Christoph Hellwig
Quoted 15 lines · Christoph Hellwig

drivers/infiniband/hw/qib/qib_user_pages.c line 44

Quoted 10 lines · On Tue, Jul 30, 2019 at 01:57:03PM -0700, [email protected] wrote:
drivers/infiniband/hw/qib/qib_user_pages.c +1 -4
@@ -40,10 +40,7 @@
 static void __qib_release_user_pages(struct page **p, size_t num_pages,
 				     int dirty)
 {
-	if (dirty)
-		put_user_pages_dirty_lock(p, num_pages);
-	else
-		put_user_pages(p, num_pages);
+	put_user_pages_dirty_lock(p, num_pages, dirty);
 }

__qib_release_user_pages should be removed now as a direct call to put_user_pages_dirty_lock is a lot more clear.

Quoted 16 lines

drivers/infiniband/hw/usnic/usnic_uiom.c static void usnic_uiom_put_pages(struct list_head *chunk_list, int dirty) · line 79

Quoted 12 lines

index 0b0237d41613..62e6ffa9ad78 100644
+++ b/drivers/infiniband/hw/usnic/usnic_uiom.c

drivers/infiniband/hw/usnic/usnic_uiom.c +1 -4
@@ -75,10 +75,7 @@ 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 (dirty)
-				put_user_pages_dirty_lock(&page, 1);
-			else
-				put_user_page(page);
+			put_user_pages_dirty_lock(&page, 1, dirty);
 			usnic_dbg("pa: %pa\n", &pa);

There is a pre-existing bug here, as this needs to use the sg_page iterator. Probably worth throwing in a fix into your series while you are at it.

Sadly usnic does not use the core rdma umem abstraction but open codes an old version of it.

In this version each sge in the sgl is exactly one page. See usnic_uiom_get_pages - so I think this loop is not a bug?

Jason

#5 lore raw
Quoted 5 lines · Jason Gunthorpe

Sadly usnic does not use the core rdma umem abstraction but open codes an old version of it.

In this version each sge in the sgl is exactly one page. See usnic_uiom_get_pages - so I think this loop is not a bug?

Actually, yes - I think we are fine given that we pass in the number of elements. Thus merging by iommus won't affect the list.

#6 lore raw

PATCH v4 2/3 drivers/gpu/drm/via: convert put_page() to put_user_page*()

In reply to [email protected]

From: John Hubbard <[email protected]>

For pages that were retained via get_user_pages*(), release those pages via the new put_user_page*() routines, instead of via put_page() or release_pages().

This is part a tree-wide conversion, as described in commit fc1d8e7cca2d ("mm: introduce put_user_page*(), placeholder versions").

Also reverse the order of a comparison, in order to placate checkpatch.pl.

---
 drivers/gpu/drm/via/via_dmablit.c | 10 ++--------
 1 file changed, 2 insertions(+), 8 deletions(-)
drivers/gpu/drm/via/via_dmablit.c +2 -8
index 062067438f1d..b5b5bf0ba65e 100644
@@ -171,7 +171,6 @@ via_map_blit_for_device(struct pci_dev *pdev,
 static void
 via_free_sg_info(struct pci_dev *pdev, drm_via_sg_info_t *vsg)
 {
-	struct page *page;
 	int i;
 
 	switch (vsg->state) {
@@ -186,13 +185,8 @@ via_free_sg_info(struct pci_dev *pdev, drm_via_sg_info_t *vsg)
 		kfree(vsg->desc_pages);
 		/* fall through */
 	case dr_via_pages_locked:
-		for (i = 0; i < vsg->num_pages; ++i) {
-			if (NULL != (page = vsg->pages[i])) {
-				if (!PageReserved(page) && (DMA_FROM_DEVICE == vsg->direction))
-					SetPageDirty(page);
-				put_page(page);
-			}
-		}
+		put_user_pages_dirty_lock(vsg->pages, vsg->num_pages,
+					  (vsg->direction == DMA_FROM_DEVICE));
 		/* fall through */
 	case dr_via_pages_alloc:
 		vfree(vsg->pages);
Signature · 1 line
2.22.0
#7 lore raw

PATCH v4 3/3 net/xdp: convert put_page() to put_user_page*()

In reply to [email protected]

From: John Hubbard <[email protected]>

For pages that were retained via get_user_pages*(), release those pages via the new put_user_page*() routines, instead of via put_page() or release_pages().

This is part a tree-wide conversion, as described in commit fc1d8e7cca2d ("mm: introduce put_user_page*(), placeholder versions").

Cc Björn Töpel <[email protected]>
Cc Magnus Karlsson <[email protected]>
Cc David S. Miller <[email protected]>
---
 net/xdp/xdp_umem.c | 9 +--------
 1 file changed, 1 insertion(+), 8 deletions(-)
net/xdp/xdp_umem.c +1 -8
index 83de74ca729a..17c4b3d3dc34 100644
@@ -166,14 +166,7 @@ void xdp_umem_clear_dev(struct xdp_umem *umem)
 
 static void xdp_umem_unpin_pages(struct xdp_umem *umem)
 {
-	unsigned int i;
-
-	for (i = 0; i < umem->npgs; i++) {
-		struct page *page = umem->pgs[i];
-
-		set_page_dirty_lock(page);
-		put_page(page);
-	}
+	put_user_pages_dirty_lock(umem->pgs, umem->npgs, true);
 
 	kfree(umem->pgs);
 	umem->pgs = NULL;
Signature · 1 line
2.22.0
Acked-by on v4 3/3
#8 lore raw

Re: [PATCH v4 3/3] net/xdp: convert put_page() to put_user_page*()

Quoted 6 lines · On 2019-07-30 22:57, [email protected] wrote:

From: John Hubbard <[email protected]>

For pages that were retained via get_user_pages*(), release those pages via the new put_user_page*() routines, instead of via put_page() or release_pages().

Quoted 8 lines

This is part a tree-wide conversion, as described in commit fc1d8e7cca2d ("mm: introduce put_user_page*(), placeholder versions").

Cc Björn Töpel <[email protected]>
Cc Magnus Karlsson <[email protected]>
Cc David S. Miller <[email protected]>
Quoted 25 lines
---
  net/xdp/xdp_umem.c | 9 +--------
  1 file changed, 1 insertion(+), 8 deletions(-)
net/xdp/xdp_umem.c +1 -8
index 83de74ca729a..17c4b3d3dc34 100644
@@ -166,14 +166,7 @@ void xdp_umem_clear_dev(struct xdp_umem *umem)
 
  static void xdp_umem_unpin_pages(struct xdp_umem *umem)
  {
-	unsigned int i;
-
-	for (i = 0; i < umem->npgs; i++) {
-		struct page *page = umem->pgs[i];
-
-		set_page_dirty_lock(page);
-		put_page(page);
-	}
+	put_user_pages_dirty_lock(umem->pgs, umem->npgs, true);
 
  	kfree(umem->pgs);
  	umem->pgs = NULL;