Merge tag 'sunxi-fixes-for-4.0' of https://git.kernel.org/pub/scm/linux/kernel/git...
[firefly-linux-kernel-4.4.55.git] / drivers / scsi / sg.c
index 208bf3c8a16cb5f84d06d780a7f3e747783d7503..2270bd51f9c2c240c669e562eb77052f89425a83 100644 (file)
@@ -1680,7 +1680,22 @@ sg_start_req(Sg_request *srp, unsigned char *cmd)
                        return -ENOMEM;
        }
 
-       rq = blk_get_request(q, rw, GFP_ATOMIC);
+       /*
+        * NOTE
+        *
+        * With scsi-mq enabled, there are a fixed number of preallocated
+        * requests equal in number to shost->can_queue.  If all of the
+        * preallocated requests are already in use, then using GFP_ATOMIC with
+        * blk_get_request() will return -EWOULDBLOCK, whereas using GFP_KERNEL
+        * will cause blk_get_request() to sleep until an active command
+        * completes, freeing up a request.  Neither option is ideal, but
+        * GFP_KERNEL is the better choice to prevent userspace from getting an
+        * unexpected EWOULDBLOCK.
+        *
+        * With scsi-mq disabled, blk_get_request() with GFP_KERNEL usually
+        * does not sleep except under memory pressure.
+        */
+       rq = blk_get_request(q, rw, GFP_KERNEL);
        if (IS_ERR(rq)) {
                kfree(long_cmdp);
                return PTR_ERR(rq);
@@ -1730,22 +1745,19 @@ sg_start_req(Sg_request *srp, unsigned char *cmd)
        }
 
        if (iov_count) {
-               int len, size = sizeof(struct sg_iovec) * iov_count;
+               int size = sizeof(struct iovec) * iov_count;
                struct iovec *iov;
+               struct iov_iter i;
 
                iov = memdup_user(hp->dxferp, size);
                if (IS_ERR(iov))
                        return PTR_ERR(iov);
 
-               len = iov_length(iov, iov_count);
-               if (hp->dxfer_len < len) {
-                       iov_count = iov_shorten(iov, iov_count, hp->dxfer_len);
-                       len = hp->dxfer_len;
-               }
+               iov_iter_init(&i, rw, iov, iov_count,
+                             min_t(size_t, hp->dxfer_len,
+                                   iov_length(iov, iov_count)));
 
-               res = blk_rq_map_user_iov(q, rq, md, (struct sg_iovec *)iov,
-                                         iov_count,
-                                         len, GFP_ATOMIC);
+               res = blk_rq_map_user_iov(q, rq, md, &i, GFP_ATOMIC);
                kfree(iov);
        } else
                res = blk_rq_map_user(q, rq, md, hp->dxferp,