From: Souptick Joarder <jrdr.li...@gmail.com>

[ Upstream commit fa63f083b3492b5ed5332b8d7c90b03b5ef24a1d ]

rio_dma_transfer() attempts to clamp the return value of
pin_user_pages_fast() to be >= 0.  However, the attempt fails because
nr_pages is overridden a few lines later, and restored to the undesirable
-ERRNO value.

The return value is ultimately stored in nr_pages, which in turn is passed
to unpin_user_pages(), which expects nr_pages >= 0, else, disaster.

Fix this by fixing the nesting of the assignment to nr_pages: nr_pages
should be clamped to zero if pin_user_pages_fast() returns -ERRNO, or set
to the return value of pin_user_pages_fast(), otherwise.

[jhubb...@nvidia.com: new changelog]

Fixes: e8de370188d09 ("rapidio: add mport char device driver")
Signed-off-by: Souptick Joarder <jrdr.li...@gmail.com>
Signed-off-by: Andrew Morton <a...@linux-foundation.org>
Reviewed-by: Ira Weiny <ira.we...@intel.com>
Reviewed-by: John Hubbard <jhubb...@nvidia.com>
Cc: Matthew Wilcox <wi...@infradead.org>
Cc: Matt Porter <mpor...@kernel.crashing.org>
Cc: Alexandre Bounine <alex.b...@gmail.com>
Cc: Gustavo A. R. Silva <gustavo...@kernel.org>
Cc: Madhuparna Bhowmik <madhuparnabhowmi...@gmail.com>
Cc: Dan Carpenter <dan.carpen...@oracle.com>
Link: 
https://lkml.kernel.org/r/1600227737-20785-1-git-send-email-jrdr.li...@gmail.com
Signed-off-by: Linus Torvalds <torva...@linux-foundation.org>
Signed-off-by: Sasha Levin <sas...@kernel.org>
---
 drivers/rapidio/devices/rio_mport_cdev.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

diff --git a/drivers/rapidio/devices/rio_mport_cdev.c 
b/drivers/rapidio/devices/rio_mport_cdev.c
index f36a8a5261a13..c3ca1cf0e1bb7 100644
--- a/drivers/rapidio/devices/rio_mport_cdev.c
+++ b/drivers/rapidio/devices/rio_mport_cdev.c
@@ -875,15 +875,16 @@ rio_dma_transfer(struct file *filp, u32 transfer_mode,
                                rmcd_error("get_user_pages_unlocked err=%ld",
                                           pinned);
                                nr_pages = 0;
-                       } else
+                       } else {
                                rmcd_error("pinned %ld out of %ld pages",
                                           pinned, nr_pages);
+                               /*
+                                * Set nr_pages up to mean "how many pages to 
unpin, in
+                                * the error handler:
+                                */
+                               nr_pages = pinned;
+                       }
                        ret = -EFAULT;
-                       /*
-                        * Set nr_pages up to mean "how many pages to unpin, in
-                        * the error handler:
-                        */
-                       nr_pages = pinned;
                        goto err_pg;
                }
 
-- 
2.25.1



Reply via email to