Skip to content

Commit 3f9f231

Browse files
Li Nanliu-song-6
authored andcommitted
md: Fix overflow in is_mddev_idle
UBSAN reports this problem: UBSAN: Undefined behaviour in drivers/md/md.c:8175:15 signed integer overflow: -2147483291 - 2072033152 cannot be represented in type 'int' Call trace: dump_backtrace+0x0/0x310 show_stack+0x28/0x38 dump_stack+0xec/0x15c ubsan_epilogue+0x18/0x84 handle_overflow+0x14c/0x19c __ubsan_handle_sub_overflow+0x34/0x44 is_mddev_idle+0x338/0x3d8 md_do_sync+0x1bb8/0x1cf8 md_thread+0x220/0x288 kthread+0x1d8/0x1e0 ret_from_fork+0x10/0x18 'curr_events' will overflow when stat accum or 'sync_io' is greater than INT_MAX. Fix it by changing sync_io, last_events and curr_events to 64bit. Signed-off-by: Li Nan <[email protected]> Reviewed-by: Yu Kuai <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Song Liu <[email protected]>
1 parent 3821bba commit 3f9f231

File tree

3 files changed

+7
-6
lines changed

3 files changed

+7
-6
lines changed

drivers/md/md.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8577,14 +8577,15 @@ static int is_mddev_idle(struct mddev *mddev, int init)
85778577
{
85788578
struct md_rdev *rdev;
85798579
int idle;
8580-
int curr_events;
8580+
long long curr_events;
85818581

85828582
idle = 1;
85838583
rcu_read_lock();
85848584
rdev_for_each_rcu(rdev, mddev) {
85858585
struct gendisk *disk = rdev->bdev->bd_disk;
8586-
curr_events = (int)part_stat_read_accum(disk->part0, sectors) -
8587-
atomic_read(&disk->sync_io);
8586+
curr_events =
8587+
(long long)part_stat_read_accum(disk->part0, sectors) -
8588+
atomic64_read(&disk->sync_io);
85888589
/* sync IO will cause sync_io to increase before the disk_stats
85898590
* as sync_io is counted when a request starts, and
85908591
* disk_stats is counted when it completes.

drivers/md/md.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ struct md_rdev {
5151

5252
sector_t sectors; /* Device size (in 512bytes sectors) */
5353
struct mddev *mddev; /* RAID array if running */
54-
int last_events; /* IO event timestamp */
54+
long long last_events; /* IO event timestamp */
5555

5656
/*
5757
* If meta_bdev is non-NULL, it means that a separate device is
@@ -621,7 +621,7 @@ extern void mddev_unlock(struct mddev *mddev);
621621

622622
static inline void md_sync_acct(struct block_device *bdev, unsigned long nr_sectors)
623623
{
624-
atomic_add(nr_sectors, &bdev->bd_disk->sync_io);
624+
atomic64_add(nr_sectors, &bdev->bd_disk->sync_io);
625625
}
626626

627627
static inline void md_sync_acct_bio(struct bio *bio, unsigned long nr_sectors)

include/linux/blkdev.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ struct gendisk {
172172
struct list_head slave_bdevs;
173173
#endif
174174
struct timer_rand_state *random;
175-
atomic_t sync_io; /* RAID */
175+
atomic64_t sync_io; /* RAID */
176176
struct disk_events *ev;
177177

178178
#ifdef CONFIG_BLK_DEV_ZONED

0 commit comments

Comments
 (0)