Skip to contentSkip to conversation

rapidio: convert to pin_user_pages(), plus a small fix

PATCH Applied John Hubbard 2 participants 4 messages May 17, 2020 – May 18, 2020

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover rapidio: convert to pin_user_pages(), plus a small fix

While converting rapidio from get_user_pages() to pin_user_pages(), I noticed a small problem in the error handling, so that is fixed first. As such, the fix has -stable on CC, and can be separately applied.

Note that I have only compile-tested these patches, although that does also include cross-compiling for half a dozen arches.

John Hubbard (2):
rapidio: fix an error in get_user_pages_fast() error handling
rapidio: convert get_user_pages() --> pin_user_pages()

 drivers/rapidio/devices/rio_mport_cdev.c | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)
Cc Matt Porter <[email protected]>
Cc Alexandre Bounine <[email protected]>
Cc Sumit Semwal <[email protected]>
Cc Dan Carpenter <[email protected]>
Cc Andrew Morton <[email protected]>

John Hubbard (2):
rapidio: fix an error in get_user_pages_fast() error handling
rapidio: convert get_user_pages() --> pin_user_pages()

 drivers/rapidio/devices/rio_mport_cdev.c | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)

base-commit: 5a9ffb954a3933d7867f4341684a23e008d6839b

Signature · 1 line
2.26.2
#1 lore raw

PATCH 1/2 rapidio: fix an error in get_user_pages_fast() error handling

In the case of get_user_pages_fast() returning fewer pages than requested, rio_dma_transfer() does not quite do the right thing. It attempts to release all the pages that were requested, rather than just the pages that were pinned.

Fix the error handling so that only the pages that were successfully pinned are released.

Fixes e8de370188d0 ("rapidio: add mport char device driver")
Cc Matt Porter <[email protected]>
Cc Alexandre Bounine <[email protected]>
Cc Sumit Semwal <[email protected]>
Cc Dan Carpenter <[email protected]>
Cc Andrew Morton <[email protected]>
---
 drivers/rapidio/devices/rio_mport_cdev.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/rapidio/devices/rio_mport_cdev.c +5 -0
index 8155f59ece38..10af330153b5 100644
@@ -877,6 +877,11 @@ rio_dma_transfer(struct file *filp, u32 transfer_mode,
 				rmcd_error("pinned %ld out of %ld pages",
 					   pinned, nr_pages);
 			ret = -EFAULT;
+			/*
+			 * Set nr_pages up to mean "how many pages to unpin, in
+			 * the error handler:
+			 */
+			nr_pages = pinned;
 			goto err_pg;
 		}
 
Signature · 1 line
2.26.2
#2 lore raw

Re: [PATCH 1/2] rapidio: fix an error in get_user_pages_fast() error handling

Quoted 13 lines · On Sun, 17 May 2020 16:56:19 -0700 John Hubbard <[email protected]> wrote:

In the case of get_user_pages_fast() returning fewer pages than requested, rio_dma_transfer() does not quite do the right thing. It attempts to release all the pages that were requested, rather than just the pages that were pinned.

Fix the error handling so that only the pages that were successfully pinned are released.

...

drivers/rapidio/devices/rio_mport_cdev.c rio_dma_transfer(struct file *filp, u32 transfer_mode, · line 886

Quoted 10 lines
drivers/rapidio/devices/rio_mport_cdev.c +5 -0
@@ -877,6 +877,11 @@ rio_dma_transfer(struct file *filp, u32 transfer_mode,
 				rmcd_error("pinned %ld out of %ld pages",
 					   pinned, nr_pages);
 			ret = -EFAULT;
+			/*
+			 * Set nr_pages up to mean "how many pages to unpin, in
+			 * the error handler:
+			 */
+			nr_pages = pinned;
 			goto err_pg;
 		}

The code is a bit odd. If (xfer->loc_addr == 0) then we do the `else' stuff then fall through to

err_pg:

	if (!req->page_list) {
		for (i = 0; i < nr_pages; i++)
			put_page(page_list[i]);
		kfree(page_list);
	}

all of which is a big no-op because nr_pages==0 and page_list==NULL, but it could all be easily avoided.

Oh well. Reviewed-by:me.

#3 lore raw

PATCH 2/2 rapidio: convert get_user_pages() --> pin_user_pages()

In reply to John Hubbard

This code was using get_user_pages_fast(), in a "Case 2" scenario (DMA/RDMA), using the categorization from [1]. That means that it's time to convert the get_user_pages_fast() + put_page() calls to pin_user_pages_fast() + unpin_user_pages() calls.

There is some helpful background in [2]: basically, this is a small part of fixing a long-standing disconnect between pinning pages, and file systems' use of those pages.

[1] Documentation/core-api/pin_user_pages.rst

[2] "Explicit pinning of user-space pages":

    https://lwn.net/Articles/807108/
Cc Matt Porter <[email protected]>
Cc Alexandre Bounine <[email protected]>
Cc Sumit Semwal <[email protected]>
Cc Dan Carpenter <[email protected]>
Cc Andrew Morton <[email protected]>
---
 drivers/rapidio/devices/rio_mport_cdev.c | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)
drivers/rapidio/devices/rio_mport_cdev.c +5 -8
index 10af330153b5..0ddd94d6f1e9 100644
@@ -572,14 +572,12 @@ static void dma_req_free(struct kref *ref)
 	struct mport_dma_req *req = container_of(ref, struct mport_dma_req,
 			refcount);
 	struct mport_cdev_priv *priv = req->priv;
-	unsigned int i;
 
 	dma_unmap_sg(req->dmach->device->dev,
 		     req->sgt.sgl, req->sgt.nents, req->dir);
 	sg_free_table(&req->sgt);
 	if (req->page_list) {
-		for (i = 0; i < req->nr_pages; i++)
-			put_page(req->page_list[i]);
+		unpin_user_pages(req->page_list, req->nr_pages);
 		kfree(req->page_list);
 	}
 
@@ -815,7 +813,7 @@ rio_dma_transfer(struct file *filp, u32 transfer_mode,
 	struct mport_dma_req *req;
 	struct mport_dev *md = priv->md;
 	struct dma_chan *chan;
-	int i, ret;
+	int ret;
 	int nents;
 
 	if (xfer->length == 0)
@@ -862,7 +860,7 @@ rio_dma_transfer(struct file *filp, u32 transfer_mode,
 			goto err_req;
 		}
 
-		pinned = get_user_pages_fast(
+		pinned = pin_user_pages_fast(
 				(unsigned long)xfer->loc_addr & PAGE_MASK,
 				nr_pages,
 				dir == DMA_FROM_DEVICE ? FOLL_WRITE : 0,
@@ -870,7 +868,7 @@ rio_dma_transfer(struct file *filp, u32 transfer_mode,
 
 		if (pinned != nr_pages) {
 			if (pinned < 0) {
-				rmcd_error("get_user_pages_unlocked err=%ld",
+				rmcd_error("pin_user_pages_fast err=%ld",
 					   pinned);
 				nr_pages = 0;
 			} else
@@ -951,8 +949,7 @@ rio_dma_transfer(struct file *filp, u32 transfer_mode,
 
 err_pg:
 	if (!req->page_list) {
-		for (i = 0; i < nr_pages; i++)
-			put_page(page_list[i]);
+		unpin_user_pages(page_list, nr_pages);
 		kfree(page_list);
 	}
 err_req:
Signature · 1 line
2.26.2