Skip to contentSkip to conversation

block: limit disk max sectors to (LLONG_MAX >> 9)

PATCH Applied Ming Lei 2 participants 2 messages Jan 15, 2025 – Jan 16, 2025 Block layer

lore
Share & tools
lore

Ming Lei

author
#0 lore raw

PATCH 1/1 block: limit disk max sectors to (LLONG_MAX >> 9)

Kernel loff_t is defined as long long int, so we can't support disk which size is > LLONG_MAX.

There are many virtual block drivers, and hardware may report bad capacity too, so limit max sectors to (LLONG_MAX >> 9) for avoiding potential trouble.

---
 block/blk.h   |  2 ++
 block/genhd.c | 10 ++++++++++
 2 files changed, 12 insertions(+)
block/blk.h +2 -0
index 4904b86d5fec..90fa5f28ccab 100644
@@ -13,6 +13,8 @@
 
 struct elevator_type;
 
+#define	BLK_DEV_MAX_SECTORS	(LLONG_MAX >> 9)
+
 /* Max future timer expiry for timeouts */
 #define BLK_MAX_TIMEOUT		(5 * HZ)
 
block/genhd.c +10 -0
index befb7a516bcf..e9375e20d866 100644
@@ -58,6 +58,13 @@ static DEFINE_IDA(ext_devt_ida);
 
 void set_capacity(struct gendisk *disk, sector_t sectors)
 {
+	if (sectors > BLK_DEV_MAX_SECTORS) {
+		pr_warn_once("%s: truncate capacity from %lld to %lld\n",
+				disk->disk_name, sectors,
+				BLK_DEV_MAX_SECTORS);
+		sectors = BLK_DEV_MAX_SECTORS;
+	}
+
 	bdev_set_nr_sectors(disk->part0, sectors);
 }
 EXPORT_SYMBOL(set_capacity);
@@ -400,6 +407,9 @@ int __must_check add_disk_fwnode(struct device *parent, struct gendisk *disk,
 	struct device *ddev = disk_to_dev(disk);
 	int ret;
 
+	if (WARN_ON_ONCE(bdev_nr_sectors(disk->part0) > BLK_DEV_MAX_SECTORS))
+		return -EINVAL;
+
 	if (queue_is_mq(disk->queue)) {
 		/*
 		 * ->submit_bio and ->poll_bio are bypassed for blk-mq drivers.
Signature · 1 line
2.44.0
maintainer Applied
#1 lore raw
Quoted 8 lines · Ming Lei

Kernel loff_t is defined as long long int, so we can't support disk which size is > LLONG_MAX.

There are many virtual block drivers, and hardware may report bad capacity too, so limit max sectors to (LLONG_MAX >> 9) for avoiding potential trouble.

[...]

Applied, thanks!

[1/1] block: limit disk max sectors to (LLONG_MAX >> 9)

      commit: 3d9a9e9a77c5ebecda43b514f2b9659644b904d0

Best regards,

Signature · 1 line
Jens Axboe