nvme: fix max_segments integer truncation
authorChristoph Hellwig <hch@lst.de>
Wed, 2 Mar 2016 17:07:12 +0000 (18:07 +0100)
committerJens Axboe <axboe@fb.com>
Thu, 3 Mar 2016 21:43:10 +0000 (14:43 -0700)
The block layer uses an unsigned short for max_segments.  The way we
calculate the value for NVMe tends to generate very large 32-bit values,
which after integer truncation may lead to a zero value instead of
the desired outcome.

Signed-off-by: Christoph Hellwig <hch@lst.de>
Reported-by: Jeff Lien <Jeff.Lien@hgst.com>
Tested-by: Jeff Lien <Jeff.Lien@hgst.com>
Reviewed-by: Keith Busch <keith.busch@intel.com>
Signed-off-by: Jens Axboe <axboe@fb.com>
drivers/nvme/host/core.c

index cfee6ac399a3ab58fd4838545ea41b6db28a22ab..03c46412fff4d602477239d0672cbdeb54c13dfb 100644 (file)
@@ -844,9 +844,11 @@ static void nvme_set_queue_limits(struct nvme_ctrl *ctrl,
                struct request_queue *q)
 {
        if (ctrl->max_hw_sectors) {
+               u32 max_segments =
+                       (ctrl->max_hw_sectors / (ctrl->page_size >> 9)) + 1;
+
                blk_queue_max_hw_sectors(q, ctrl->max_hw_sectors);
-               blk_queue_max_segments(q,
-                       (ctrl->max_hw_sectors / (ctrl->page_size >> 9)) + 1);
+               blk_queue_max_segments(q, min_t(u32, max_segments, USHRT_MAX));
        }
        if (ctrl->stripe_size)
                blk_queue_chunk_sectors(q, ctrl->stripe_size >> 9);