Skip to content

Commit 3d9a9e9

Browse files
Ming Leiaxboe
authored andcommitted
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. 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>
1 parent 5d1f7ee commit 3d9a9e9

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

block/blk.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
struct elevator_type;
1515

16+
#define BLK_DEV_MAX_SECTORS (LLONG_MAX >> 9)
17+
1618
/* Max future timer expiry for timeouts */
1719
#define BLK_MAX_TIMEOUT (5 * HZ)
1820

block/genhd.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ static DEFINE_IDA(ext_devt_ida);
5858

5959
void set_capacity(struct gendisk *disk, sector_t sectors)
6060
{
61+
if (sectors > BLK_DEV_MAX_SECTORS) {
62+
pr_warn_once("%s: truncate capacity from %lld to %lld\n",
63+
disk->disk_name, sectors,
64+
BLK_DEV_MAX_SECTORS);
65+
sectors = BLK_DEV_MAX_SECTORS;
66+
}
67+
6168
bdev_set_nr_sectors(disk->part0, sectors);
6269
}
6370
EXPORT_SYMBOL(set_capacity);
@@ -400,6 +407,9 @@ int __must_check add_disk_fwnode(struct device *parent, struct gendisk *disk,
400407
struct device *ddev = disk_to_dev(disk);
401408
int ret;
402409

410+
if (WARN_ON_ONCE(bdev_nr_sectors(disk->part0) > BLK_DEV_MAX_SECTORS))
411+
return -EINVAL;
412+
403413
if (queue_is_mq(disk->queue)) {
404414
/*
405415
* ->submit_bio and ->poll_bio are bypassed for blk-mq drivers.

0 commit comments

Comments
 (0)