Linux SCSI subsystem development
 help / color / mirror / Atom feed
From: Damien Le Moal <dlemoal@kernel.org>
To: "Martin K . Petersen" <martin.petersen@oracle.com>,
	linux-scsi@vger.kernel.org
Subject: [PATCH v4 1/2] scsi: sd: Prevent logical_to_bytes() from returning overflowed values
Date: Fri, 13 Jun 2025 15:29:08 +0900	[thread overview]
Message-ID: <20250613062909.2505759-2-dlemoal@kernel.org> (raw)
In-Reply-To: <20250613062909.2505759-1-dlemoal@kernel.org>

Make sure that logical_to_bytes() does not return an overflowed value
by changing its return type from unsigned int (32-bits) to u64
(64-bits). And while at it, also use a bit-shift instead of a
multiplication, similar to logical_to_sectors() and bytes_to_logical().

Signed-off-by: Damien Le Moal <dlemoal@kernel.org>
---
 drivers/scsi/sd.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/scsi/sd.h b/drivers/scsi/sd.h
index 36382eca941c..53658679e063 100644
--- a/drivers/scsi/sd.h
+++ b/drivers/scsi/sd.h
@@ -213,9 +213,9 @@ static inline sector_t logical_to_sectors(struct scsi_device *sdev, sector_t blo
 	return blocks << (ilog2(sdev->sector_size) - 9);
 }
 
-static inline unsigned int logical_to_bytes(struct scsi_device *sdev, sector_t blocks)
+static inline u64 logical_to_bytes(struct scsi_device *sdev, sector_t blocks)
 {
-	return blocks * sdev->sector_size;
+	return (u64)blocks << ilog2(sdev->sector_size);
 }
 
 static inline sector_t bytes_to_logical(struct scsi_device *sdev, unsigned int bytes)
-- 
2.49.0


  reply	other threads:[~2025-06-13  6:30 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-13  6:29 [PATCH v4 0/2] Improve optimal IO size initialization Damien Le Moal
2025-06-13  6:29 ` Damien Le Moal [this message]
2025-06-13 16:17   ` [PATCH v4 1/2] scsi: sd: Prevent logical_to_bytes() from returning overflowed values Bart Van Assche
2025-06-13  6:29 ` [PATCH v4 2/2] scsi: sd: Set a default optimal IO size if one is not defined Damien Le Moal
2025-06-13 14:31   ` John Garry
2025-06-16  5:34     ` Damien Le Moal
2025-06-16  6:26       ` Damien Le Moal

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250613062909.2505759-2-dlemoal@kernel.org \
    --to=dlemoal@kernel.org \
    --cc=linux-scsi@vger.kernel.org \
    --cc=martin.petersen@oracle.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox