提交 fbb19217 编写于 作者: Z zhangwensheng 提交者: Yang Yingliang

md: Fix undefined behaviour in is_mddev_idle

hulk inclusion
category: bugfix
bugzilla: 185891
CVE: NA

--------------------------------

UBSAN reports this problem:

[ 5984.281385] UBSAN: Undefined behaviour in drivers/md/md.c:8175:15
[ 5984.281390] signed integer overflow:
[ 5984.281393] -2147483291 - 2072033152 cannot be represented in type 'int'
[ 5984.281400] CPU: 25 PID: 1854 Comm: md101_resync Kdump: loaded Not tainted 4.19.90
[ 5984.281404] Hardware name: Huawei TaiShan 200 (Model 5280)/BC82AMDDA
[ 5984.281406] Call trace:
[ 5984.281415]  dump_backtrace+0x0/0x310
[ 5984.281418]  show_stack+0x28/0x38
[ 5984.281425]  dump_stack+0xec/0x15c
[ 5984.281430]  ubsan_epilogue+0x18/0x84
[ 5984.281434]  handle_overflow+0x14c/0x19c
[ 5984.281439]  __ubsan_handle_sub_overflow+0x34/0x44
[ 5984.281445]  is_mddev_idle+0x338/0x3d8
[ 5984.281449]  md_do_sync+0x1bb8/0x1cf8
[ 5984.281452]  md_thread+0x220/0x288
[ 5984.281457]  kthread+0x1d8/0x1e0
[ 5984.281461]  ret_from_fork+0x10/0x18

When the stat aacum of the disk is greater than INT_MAX, its value
becomes negative after casting to 'int', which may lead to overflow
after subtracting a positive number. In the same way, when the value
of sync_io is greater than INT_MAX,overflow may also occur. These
situations will lead to undefined behavior.

Otherwise, if the stat accum of the disk is close to INT_MAX when
creating raid arrays, the initial value of last_events would be set
close to INT_MAX when mddev initializes IO event counters.
'curr_events - rdev->last_events > 64' will always false during
synchronization. If all the disks of mddev are in this case,
is_mddev_idle() will always return 1, which may cause non-sync IO
is very slow.

To address these problems, need to use 64bit signed integer type
for sync_io,last_events, and curr_events.
Signed-off-by: Nzhangwensheng <zhangwensheng5@huawei.com>
Reviewed-by: NHou Tao <houtao1@huawei.com>
Signed-off-by: NYang Yingliang <yangyingliang@huawei.com>
上级 7859e7ea
...@@ -8161,14 +8161,15 @@ static int is_mddev_idle(struct mddev *mddev, int init) ...@@ -8161,14 +8161,15 @@ static int is_mddev_idle(struct mddev *mddev, int init)
{ {
struct md_rdev *rdev; struct md_rdev *rdev;
int idle; int idle;
int curr_events; long long curr_events;
idle = 1; idle = 1;
rcu_read_lock(); rcu_read_lock();
rdev_for_each_rcu(rdev, mddev) { rdev_for_each_rcu(rdev, mddev) {
struct gendisk *disk = rdev->bdev->bd_contains->bd_disk; struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
curr_events = (int)part_stat_read_accum(&disk->part0, sectors) - curr_events =
atomic_read(&disk->sync_io); (long long)part_stat_read_accum(&disk->part0, sectors) -
atomic64_read(&disk->sync_io_sectors);
/* sync IO will cause sync_io to increase before the disk_stats /* sync IO will cause sync_io to increase before the disk_stats
* as sync_io is counted when a request starts, and * as sync_io is counted when a request starts, and
* disk_stats is counted when it completes. * disk_stats is counted when it completes.
......
...@@ -47,7 +47,7 @@ struct md_rdev { ...@@ -47,7 +47,7 @@ struct md_rdev {
sector_t sectors; /* Device size (in 512bytes sectors) */ sector_t sectors; /* Device size (in 512bytes sectors) */
struct mddev *mddev; /* RAID array if running */ struct mddev *mddev; /* RAID array if running */
int last_events; /* IO event timestamp */ long long last_events; /* IO event timestamp */
/* /*
* If meta_bdev is non-NULL, it means that a separate device is * If meta_bdev is non-NULL, it means that a separate device is
...@@ -528,12 +528,12 @@ extern void mddev_unlock(struct mddev *mddev); ...@@ -528,12 +528,12 @@ extern void mddev_unlock(struct mddev *mddev);
static inline void md_sync_acct(struct block_device *bdev, unsigned long nr_sectors) static inline void md_sync_acct(struct block_device *bdev, unsigned long nr_sectors)
{ {
atomic_add(nr_sectors, &bdev->bd_contains->bd_disk->sync_io); atomic64_add(nr_sectors, &bdev->bd_contains->bd_disk->sync_io_sectors);
} }
static inline void md_sync_acct_bio(struct bio *bio, unsigned long nr_sectors) static inline void md_sync_acct_bio(struct bio *bio, unsigned long nr_sectors)
{ {
atomic_add(nr_sectors, &bio->bi_disk->sync_io); atomic64_add(nr_sectors, &bio->bi_disk->sync_io_sectors);
} }
struct md_personality struct md_personality
......
...@@ -212,7 +212,6 @@ struct gendisk { ...@@ -212,7 +212,6 @@ struct gendisk {
struct kobject *slave_dir; struct kobject *slave_dir;
struct timer_rand_state *random; struct timer_rand_state *random;
atomic_t sync_io; /* RAID */
struct disk_events *ev; struct disk_events *ev;
#ifdef CONFIG_BLK_DEV_INTEGRITY #ifdef CONFIG_BLK_DEV_INTEGRITY
struct kobject integrity_kobj; struct kobject integrity_kobj;
...@@ -220,6 +219,7 @@ struct gendisk { ...@@ -220,6 +219,7 @@ struct gendisk {
int node_id; int node_id;
struct badblocks *bb; struct badblocks *bb;
struct lockdep_map lockdep_map; struct lockdep_map lockdep_map;
atomic64_t sync_io_sectors; /* RAID */
#ifndef __GENKSYMS__ #ifndef __GENKSYMS__
unsigned long *user_ro_bitmap; unsigned long *user_ro_bitmap;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册