From 8609c63fce58e94d82f6b6bf29c7806062e2e867 Mon Sep 17 00:00:00 2001 From: Bart Van Assche Date: Fri, 2 Apr 2021 18:58:20 +0200 Subject: [PATCH] nvme: fix handling of large MDTS values 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 Reviewed-by: Keith Busch [hch: rebased to account for the new nvme_mps_to_sectors helper] Signed-off-by: Christoph Hellwig --- drivers/nvme/host/core.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/drivers/nvme/host/core.c b/drivers/nvme/host/core.c index e37e2ecd574c4..314705da2c107 100644 --- a/drivers/nvme/host/core.c +++ b/drivers/nvme/host/core.c @@ -3049,9 +3049,11 @@ static int nvme_get_effects_log(struct nvme_ctrl *ctrl, u8 csi, 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)