linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v4 0/2] md: fix is_mddev_idle()
@ 2024-01-17  3:19 linan666
  2024-01-17  3:19 ` [PATCH v4 1/2] md: Fix overflow in is_mddev_idle linan666
                   ` (3 more replies)
  0 siblings, 4 replies; 7+ messages in thread
From: linan666 @ 2024-01-17  3:19 UTC (permalink / raw)
  To: song, axboe
  Cc: linux-raid, linux-kernel, linux-block, linan666, yukuai3,
	yi.zhang, houtao1, yangerkun

From: Li Nan <linan122@huawei.com>

Changes in v4:
 - patch 2, add the check of 'init', update last_events even if iostat
   is disabled.

Li Nan (2):
  md: Fix overflow in is_mddev_idle
  md: don't account sync_io if iostats of the disk is disabled

 drivers/md/md.h        |  5 +++--
 include/linux/blkdev.h |  2 +-
 drivers/md/md.c        | 11 ++++++++---
 3 files changed, 12 insertions(+), 6 deletions(-)

-- 
2.39.2


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH v4 1/2] md: Fix overflow in is_mddev_idle
  2024-01-17  3:19 [PATCH v4 0/2] md: fix is_mddev_idle() linan666
@ 2024-01-17  3:19 ` linan666
  2024-01-17  3:19 ` [PATCH v4 2/2] md: don't account sync_io if iostats of the disk is disabled linan666
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 7+ messages in thread
From: linan666 @ 2024-01-17  3:19 UTC (permalink / raw)
  To: song, axboe
  Cc: linux-raid, linux-kernel, linux-block, linan666, yukuai3,
	yi.zhang, houtao1, yangerkun

From: Li Nan <linan122@huawei.com>

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 <linan122@huawei.com>
Reviewed-by: Yu Kuai <yukuai3@huawei.com>
---
 drivers/md/md.h        | 4 ++--
 include/linux/blkdev.h | 2 +-
 drivers/md/md.c        | 7 ++++---
 3 files changed, 7 insertions(+), 6 deletions(-)

diff --git a/drivers/md/md.h b/drivers/md/md.h
index ade83af123a2..1a4f976951c1 100644
--- a/drivers/md/md.h
+++ b/drivers/md/md.h
@@ -50,7 +50,7 @@ struct md_rdev {
 
 	sector_t sectors;		/* Device size (in 512bytes sectors) */
 	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
@@ -584,7 +584,7 @@ extern void mddev_unlock(struct mddev *mddev);
 
 static inline void md_sync_acct(struct block_device *bdev, unsigned long nr_sectors)
 {
-	atomic_add(nr_sectors, &bdev->bd_disk->sync_io);
+	atomic64_add(nr_sectors, &bdev->bd_disk->sync_io);
 }
 
 static inline void md_sync_acct_bio(struct bio *bio, unsigned long nr_sectors)
diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h
index 3f8a21cd9233..d28b98adf457 100644
--- a/include/linux/blkdev.h
+++ b/include/linux/blkdev.h
@@ -170,7 +170,7 @@ struct gendisk {
 	struct list_head slave_bdevs;
 #endif
 	struct timer_rand_state *random;
-	atomic_t sync_io;		/* RAID */
+	atomic64_t sync_io;		/* RAID */
 	struct disk_events *ev;
 
 #ifdef CONFIG_BLK_DEV_ZONED
diff --git a/drivers/md/md.c b/drivers/md/md.c
index c94373d64f2c..a6829ea5b560 100644
--- a/drivers/md/md.c
+++ b/drivers/md/md.c
@@ -8496,14 +8496,15 @@ static int is_mddev_idle(struct mddev *mddev, int init)
 {
 	struct md_rdev *rdev;
 	int idle;
-	int curr_events;
+	long long curr_events;
 
 	idle = 1;
 	rcu_read_lock();
 	rdev_for_each_rcu(rdev, mddev) {
 		struct gendisk *disk = rdev->bdev->bd_disk;
-		curr_events = (int)part_stat_read_accum(disk->part0, sectors) -
-			      atomic_read(&disk->sync_io);
+		curr_events =
+			(long long)part_stat_read_accum(disk->part0, sectors) -
+			atomic64_read(&disk->sync_io);
 		/* sync IO will cause sync_io to increase before the disk_stats
 		 * as sync_io is counted when a request starts, and
 		 * disk_stats is counted when it completes.
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH v4 2/2] md: don't account sync_io if iostats of the disk is disabled
  2024-01-17  3:19 [PATCH v4 0/2] md: fix is_mddev_idle() linan666
  2024-01-17  3:19 ` [PATCH v4 1/2] md: Fix overflow in is_mddev_idle linan666
@ 2024-01-17  3:19 ` linan666
  2024-01-17  9:35   ` Yu Kuai
  2024-03-08  1:12 ` [PATCH v4 0/2] md: fix is_mddev_idle() Li Nan
  2024-04-10 21:35 ` Song Liu
  3 siblings, 1 reply; 7+ messages in thread
From: linan666 @ 2024-01-17  3:19 UTC (permalink / raw)
  To: song, axboe
  Cc: linux-raid, linux-kernel, linux-block, linan666, yukuai3,
	yi.zhang, houtao1, yangerkun

From: Li Nan <linan122@huawei.com>

If iostats is disabled, disk_stats will not be updated and
part_stat_read_accum() only returns a constant value. In this case,
continuing to count sync_io and to check is_mddev_idle() is no longer
meaningful.

Signed-off-by: Li Nan <linan122@huawei.com>
---
 drivers/md/md.h | 3 ++-
 drivers/md/md.c | 4 ++++
 2 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/drivers/md/md.h b/drivers/md/md.h
index 1a4f976951c1..e2d03a7a858c 100644
--- a/drivers/md/md.h
+++ b/drivers/md/md.h
@@ -584,7 +584,8 @@ extern void mddev_unlock(struct mddev *mddev);
 
 static inline void md_sync_acct(struct block_device *bdev, unsigned long nr_sectors)
 {
-	atomic64_add(nr_sectors, &bdev->bd_disk->sync_io);
+	if (blk_queue_io_stat(bdev->bd_disk->queue))
+		atomic64_add(nr_sectors, &bdev->bd_disk->sync_io);
 }
 
 static inline void md_sync_acct_bio(struct bio *bio, unsigned long nr_sectors)
diff --git a/drivers/md/md.c b/drivers/md/md.c
index a6829ea5b560..919d6affc0ac 100644
--- a/drivers/md/md.c
+++ b/drivers/md/md.c
@@ -8502,6 +8502,10 @@ static int is_mddev_idle(struct mddev *mddev, int init)
 	rcu_read_lock();
 	rdev_for_each_rcu(rdev, mddev) {
 		struct gendisk *disk = rdev->bdev->bd_disk;
+
+		if (!init && !blk_queue_io_stat(disk->queue))
+			continue;
+
 		curr_events =
 			(long long)part_stat_read_accum(disk->part0, sectors) -
 			atomic64_read(&disk->sync_io);
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH v4 2/2] md: don't account sync_io if iostats of the disk is disabled
  2024-01-17  3:19 ` [PATCH v4 2/2] md: don't account sync_io if iostats of the disk is disabled linan666
@ 2024-01-17  9:35   ` Yu Kuai
  0 siblings, 0 replies; 7+ messages in thread
From: Yu Kuai @ 2024-01-17  9:35 UTC (permalink / raw)
  To: linan666, song, axboe
  Cc: linux-raid, linux-kernel, linux-block, yi.zhang, houtao1,
	yangerkun, yukuai (C)

在 2024/01/17 11:19, linan666@huaweicloud.com 写道:
> From: Li Nan <linan122@huawei.com>
> 
> If iostats is disabled, disk_stats will not be updated and
> part_stat_read_accum() only returns a constant value. In this case,
> continuing to count sync_io and to check is_mddev_idle() is no longer
> meaningful.

LGTM
Reviewed-by: Yu Kuai <yukuai3@huawei.com>

> 
> Signed-off-by: Li Nan <linan122@huawei.com>
> ---
>   drivers/md/md.h | 3 ++-
>   drivers/md/md.c | 4 ++++
>   2 files changed, 6 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/md/md.h b/drivers/md/md.h
> index 1a4f976951c1..e2d03a7a858c 100644
> --- a/drivers/md/md.h
> +++ b/drivers/md/md.h
> @@ -584,7 +584,8 @@ extern void mddev_unlock(struct mddev *mddev);
>   
>   static inline void md_sync_acct(struct block_device *bdev, unsigned long nr_sectors)
>   {
> -	atomic64_add(nr_sectors, &bdev->bd_disk->sync_io);
> +	if (blk_queue_io_stat(bdev->bd_disk->queue))
> +		atomic64_add(nr_sectors, &bdev->bd_disk->sync_io);
>   }
>   
>   static inline void md_sync_acct_bio(struct bio *bio, unsigned long nr_sectors)
> diff --git a/drivers/md/md.c b/drivers/md/md.c
> index a6829ea5b560..919d6affc0ac 100644
> --- a/drivers/md/md.c
> +++ b/drivers/md/md.c
> @@ -8502,6 +8502,10 @@ static int is_mddev_idle(struct mddev *mddev, int init)
>   	rcu_read_lock();
>   	rdev_for_each_rcu(rdev, mddev) {
>   		struct gendisk *disk = rdev->bdev->bd_disk;
> +
> +		if (!init && !blk_queue_io_stat(disk->queue))
> +			continue;
> +
>   		curr_events =
>   			(long long)part_stat_read_accum(disk->part0, sectors) -
>   			atomic64_read(&disk->sync_io);
> 


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v4 0/2] md: fix is_mddev_idle()
  2024-01-17  3:19 [PATCH v4 0/2] md: fix is_mddev_idle() linan666
  2024-01-17  3:19 ` [PATCH v4 1/2] md: Fix overflow in is_mddev_idle linan666
  2024-01-17  3:19 ` [PATCH v4 2/2] md: don't account sync_io if iostats of the disk is disabled linan666
@ 2024-03-08  1:12 ` Li Nan
  2024-03-08  1:48   ` Song Liu
  2024-04-10 21:35 ` Song Liu
  3 siblings, 1 reply; 7+ messages in thread
From: Li Nan @ 2024-03-08  1:12 UTC (permalink / raw)
  To: linan666, song, axboe
  Cc: linux-raid, linux-kernel, linux-block, yukuai3, yi.zhang,
	houtao1, yangerkun

friendly ping ...

在 2024/1/17 11:19, linan666@huaweicloud.com 写道:
> From: Li Nan <linan122@huawei.com>
> 
> Changes in v4:
>   - patch 2, add the check of 'init', update last_events even if iostat
>     is disabled.
> 
> Li Nan (2):
>    md: Fix overflow in is_mddev_idle
>    md: don't account sync_io if iostats of the disk is disabled
> 
>   drivers/md/md.h        |  5 +++--
>   include/linux/blkdev.h |  2 +-
>   drivers/md/md.c        | 11 ++++++++---
>   3 files changed, 12 insertions(+), 6 deletions(-)
> 

-- 
Thanks,
Nan


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v4 0/2] md: fix is_mddev_idle()
  2024-03-08  1:12 ` [PATCH v4 0/2] md: fix is_mddev_idle() Li Nan
@ 2024-03-08  1:48   ` Song Liu
  0 siblings, 0 replies; 7+ messages in thread
From: Song Liu @ 2024-03-08  1:48 UTC (permalink / raw)
  To: Li Nan
  Cc: axboe, linux-raid, linux-kernel, linux-block, yukuai3, yi.zhang,
	houtao1, yangerkun

On Thu, Mar 7, 2024 at 5:12 PM Li Nan <linan666@huaweicloud.com> wrote:
>
> friendly ping ...

I am sorry that I somehow missed this (or archived it in patchwork). I think
we gonna ship this to 6.10 kernel. I will work on it later.

Thanks,
Song

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v4 0/2] md: fix is_mddev_idle()
  2024-01-17  3:19 [PATCH v4 0/2] md: fix is_mddev_idle() linan666
                   ` (2 preceding siblings ...)
  2024-03-08  1:12 ` [PATCH v4 0/2] md: fix is_mddev_idle() Li Nan
@ 2024-04-10 21:35 ` Song Liu
  3 siblings, 0 replies; 7+ messages in thread
From: Song Liu @ 2024-04-10 21:35 UTC (permalink / raw)
  To: linan666
  Cc: axboe, linux-raid, linux-kernel, linux-block, yukuai3, yi.zhang,
	houtao1, yangerkun

On Tue, Jan 16, 2024 at 7:23 PM <linan666@huaweicloud.com> wrote:
>
> From: Li Nan <linan122@huawei.com>
>
> Changes in v4:
>  - patch 2, add the check of 'init', update last_events even if iostat
>    is disabled.
>
> Li Nan (2):
>   md: Fix overflow in is_mddev_idle
>   md: don't account sync_io if iostats of the disk is disabled

Applied to md-6.10. Thanks!

Song

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2024-04-10 21:35 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-01-17  3:19 [PATCH v4 0/2] md: fix is_mddev_idle() linan666
2024-01-17  3:19 ` [PATCH v4 1/2] md: Fix overflow in is_mddev_idle linan666
2024-01-17  3:19 ` [PATCH v4 2/2] md: don't account sync_io if iostats of the disk is disabled linan666
2024-01-17  9:35   ` Yu Kuai
2024-03-08  1:12 ` [PATCH v4 0/2] md: fix is_mddev_idle() Li Nan
2024-03-08  1:48   ` Song Liu
2024-04-10 21:35 ` Song Liu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).