]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
block: limit disk max sectors to (LLONG_MAX >> 9)
authorMing Lei <ming.lei@redhat.com>
Wed, 15 Jan 2025 09:26:48 +0000 (17:26 +0800)
committerJens Axboe <axboe@kernel.dk>
Wed, 15 Jan 2025 22:46:56 +0000 (15:46 -0700)
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.

Signed-off-by: Ming Lei <ming.lei@redhat.com>
Link: https://lore.kernel.org/r/20250115092648.1104452-1-ming.lei@redhat.com
Signed-off-by: Jens Axboe <axboe@kernel.dk>
block/blk.h
block/genhd.c

index 4904b86d5feca05130f0112f37c97995f5eb0180..90fa5f28ccabfd71af26245071805907d7a7c61b 100644 (file)
@@ -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)
 
index befb7a516bcf2cecb9f66dc2c91bc0f14528408f..e9375e20d866ededa95d1f76fd78c5df330eab01 100644 (file)
@@ -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.