nvme: fix handling of large MDTS values
authorBart Van Assche <bvanassche@acm.org>
Fri, 2 Apr 2021 16:58:20 +0000 (18:58 +0200)
committerChristoph Hellwig <hch@lst.de>
Tue, 6 Apr 2021 06:34:39 +0000 (08:34 +0200)
Instead of triggering an integer overflow and undefined behavior if MDTS is
large, set max_hw_sectors to UINT_MAX.

Signed-off-by: Bart Van Assche <bvanassche@acm.org>
Reviewed-by: Keith Busch <kbusch@kernel.org>
[hch: rebased to account for the new nvme_mps_to_sectors helper]
Signed-off-by: Christoph Hellwig <hch@lst.de>
drivers/nvme/host/core.c

index e37e2ec..314705d 100644 (file)
@@ -3049,9 +3049,11 @@ out:
 
 static inline u32 nvme_mps_to_sectors(struct nvme_ctrl *ctrl, u32 units)
 {
-       u32 page_shift = NVME_CAP_MPSMIN(ctrl->cap) + 12;
+       u32 page_shift = NVME_CAP_MPSMIN(ctrl->cap) + 12, val;
 
-       return 1 << (units + page_shift - 9);
+       if (check_shl_overflow(1U, units + page_shift - 9, &val))
+               return UINT_MAX;
+       return val;
 }
 
 static int nvme_init_non_mdts_limits(struct nvme_ctrl *ctrl)