Skip to contentSkip to conversation

mdraid: fix read/write bytes accounting

PATCH In review [email protected] 4 participants 7 messages Jun 5, 2020 – Jun 25, 2020 MD RAID

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mdraid: fix read/write bytes accounting

From: Jeff Mahoney <[email protected]>

The i/o accounting published in /proc/diskstats for mdraid is currently broken. md_make_request does the accounting for every bio passed but when a bio needs to be split, all the split bios are also submitted through md_make_request, resulting in multiple accounting.

As a result, a quick test on a RAID6 volume displayed the following behavior:

131072+0 records in
131072+0 records out
67108864 bytes (67 MB, 64 MiB) copied, 13.9227 s, 4.8 MB/s

... shows 131072 sectors read -- 64 MiB. Correct.

512+0 records in
512+0 records out
67108864 bytes (67 MB, 64 MiB) copied, 0.287365 s, 234 MB/s

... shows 196608 sectors read -- 96 MiB. With a 64k stripe size, we're seeing some splitting.

512+0 records in
512+0 records out
536870912 bytes (537 MB, 512 MiB) copied, 0.705004 s, 762 MB/s

... shows 4718624 sectors read -- 2.25 GiB transferred. Lots of splitting and a stats explosion.

The fix is to push the accounting into the personality make_request callbacks so that only the bios actually submitted for I/O will be accounted.

---
 drivers/md/md-faulty.c    |  1 +
 drivers/md/md-linear.c    |  1 +
 drivers/md/md-multipath.c |  1 +
 drivers/md/md.c           | 21 +++++++++++++++------
 drivers/md/md.h           |  1 +
 drivers/md/raid0.c        |  1 +
 drivers/md/raid1.c        |  3 +++
 drivers/md/raid10.c       |  3 +++
 drivers/md/raid5.c        |  3 +++
 9 files changed, 29 insertions(+), 6 deletions(-)
drivers/md/md-faulty.c +1 -0
index 50ad4ba86f0e..25e2c72ef920 100644
@@ -214,6 +214,7 @@ static bool faulty_make_request(struct mddev *mddev, struct bio *bio)
 	} else
 		bio_set_dev(bio, conf->rdev->bdev);
 
+	md_io_acct(mddev, bio_op(bio), bio_sectors(bio));
 	generic_make_request(bio);
 	return true;
 }
drivers/md/md-linear.c +1 -0
index 26c75c0199fa..ec0dbc0f1d76 100644
@@ -271,6 +271,7 @@ static bool linear_make_request(struct mddev *mddev, struct bio *bio)
 		bio = split;
 	}
 
+	md_io_acct(mddev, bio_op(bio), bio_sectors(bio));
 	bio_set_dev(bio, tmp_dev->rdev->bdev);
 	bio->bi_iter.bi_sector = bio->bi_iter.bi_sector -
 		start_sector + data_offset;
drivers/md/md-multipath.c +1 -0
index 152f9e65a226..c3afe3f62a88 100644
@@ -131,6 +131,7 @@ static bool multipath_make_request(struct mddev *mddev, struct bio * bio)
 	mp_bh->bio.bi_private = mp_bh;
 	mddev_check_writesame(mddev, &mp_bh->bio);
 	mddev_check_write_zeroes(mddev, &mp_bh->bio);
+	md_io_acct(mddev, bio_op(bio), bio_sectors(bio));
 	generic_make_request(&mp_bh->bio);
 	return true;
 }
drivers/md/md.c +15 -6
index f567f536b529..e8078a16419b 100644
@@ -463,10 +463,24 @@ void md_handle_request(struct mddev *mddev, struct bio *bio)
 }
 EXPORT_SYMBOL(md_handle_request);
 
+/*
+ * This is generic_start_io_acct without the inflight tracking.  Since
+ * we can't reliably call generic_end_io_acct, the inflight counter
+ * would also be unreliable and it's better to keep it at 0.
+ */
+void md_io_acct(struct mddev *mddev, int sgrp, unsigned int sectors)
+{
+	part_stat_lock();
+	part_stat_inc(&mddev->gendisk->part0, ios[sgrp]);
+	part_stat_add(&mddev->gendisk->part0, sectors[sgrp], sectors);
+	part_stat_unlock();
+
+}
+EXPORT_SYMBOL_GPL(md_io_acct);
+
 static blk_qc_t md_make_request(struct request_queue *q, struct bio *bio)
 {
 	const int rw = bio_data_dir(bio);
-	const int sgrp = op_stat_group(bio_op(bio));
 	struct mddev *mddev = bio->bi_disk->private_data;
 	unsigned int sectors;
 
@@ -498,11 +512,6 @@ static blk_qc_t md_make_request(struct request_queue *q, struct bio *bio)
 
 	md_handle_request(mddev, bio);
 
-	part_stat_lock();
-	part_stat_inc(&mddev->gendisk->part0, ios[sgrp]);
-	part_stat_add(&mddev->gendisk->part0, sectors[sgrp], sectors);
-	part_stat_unlock();
-
 	return BLK_QC_T_NONE;
 }
 
drivers/md/md.h +1 -0
index 612814d07d35..5834427f7557 100644
@@ -741,6 +741,7 @@ extern void mddev_suspend(struct mddev *mddev);
 extern void mddev_resume(struct mddev *mddev);
 extern struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs,
 				   struct mddev *mddev);
+extern void md_io_acct(struct mddev *mddev, int sgrp, unsigned int sectors);
 
 extern void md_reload_sb(struct mddev *mddev, int raid_disk);
 extern void md_update_sb(struct mddev *mddev, int force);
drivers/md/raid0.c +1 -0
index 322386ff5d22..ac1109d1d48d 100644
@@ -633,6 +633,7 @@ static bool raid0_make_request(struct mddev *mddev, struct bio *bio)
 				disk_devt(mddev->gendisk), bio_sector);
 	mddev_check_writesame(mddev, bio);
 	mddev_check_write_zeroes(mddev, bio);
+	md_io_acct(mddev, bio_op(bio), bio_sectors(bio));
 	generic_make_request(bio);
 	return true;
 }
drivers/md/raid1.c +3 -0
index dcd27f3da84e..f0d620e2b90f 100644
@@ -1318,6 +1318,7 @@ static void raid1_read_request(struct mddev *mddev, struct bio *bio,
 		r1_bio->sectors = max_sectors;
 	}
 
+	md_io_acct(mddev, bio_op(bio), bio_sectors(bio));
 	r1_bio->read_disk = rdisk;
 
 	read_bio = bio_clone_fast(bio, gfp, &mddev->bio_set);
@@ -1489,6 +1490,8 @@ static void raid1_write_request(struct mddev *mddev, struct bio *bio,
 		r1_bio->sectors = max_sectors;
 	}
 
+	md_io_acct(mddev, bio_op(bio), bio_sectors(bio));
+
 	atomic_set(&r1_bio->remaining, 1);
 	atomic_set(&r1_bio->behind_remaining, 0);
 
drivers/md/raid10.c +3 -0
index ec136e44aef7..b8e8d7f67f65 100644
@@ -1202,6 +1202,8 @@ static void raid10_read_request(struct mddev *mddev, struct bio *bio,
 	}
 	slot = r10_bio->read_slot;
 
+	md_io_acct(mddev, bio_op(bio), bio_sectors(bio));
+
 	read_bio = bio_clone_fast(bio, gfp, &mddev->bio_set);
 
 	r10_bio->devs[slot].bio = read_bio;
@@ -1485,6 +1487,7 @@ static void raid10_write_request(struct mddev *mddev, struct bio *bio,
 		r10_bio->master_bio = bio;
 	}
 
+	md_io_acct(mddev, bio_op(bio), bio_sectors(bio));
 	atomic_set(&r10_bio->remaining, 1);
 	md_bitmap_startwrite(mddev->bitmap, r10_bio->sector, r10_bio->sectors, 0);
 
drivers/md/raid5.c +3 -0
index ab8067f9ce8c..e57109e39fcd 100644
@@ -5316,6 +5316,7 @@ static struct bio *chunk_aligned_read(struct mddev *mddev, struct bio *raid_bio)
 	if (!raid5_read_one_chunk(mddev, raid_bio))
 		return raid_bio;
 
+	md_io_acct(mddev, bio_op(raid_bio), bio_sectors(raid_bio));
 	return NULL;
 }
 
@@ -5634,6 +5635,8 @@ static bool raid5_make_request(struct mddev *mddev, struct bio * bi)
 	last_sector = bio_end_sector(bi);
 	bi->bi_next = NULL;
 
+	md_io_acct(mddev, bio_op(bi), bio_sectors(bi));
+
 	prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
 	for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
 		int previous;
Signature · 1 line
2.16.4
#1 lore raw
Quoted 4 lines · On 6/5/20 10:19 PM, [email protected] wrote:

The i/o accounting published in /proc/diskstats for mdraid is currently broken. md_make_request does the accounting for every bio passed but when a bio needs to be split, all the split bios are also submitted through md_make_request, resulting in multiple accounting.

Hi Jeff,

I sent a patch a few days ago which should fix this issue. Can you check it out?

https://marc.info/?l=linux-raid&m=159102814820539

Thanks,
Artur

#2 lore raw

Hi Artur,

Quoted 11 lines · Artur Paszkiewicz
Quoted 4 lines · On 6/5/20 10:19 PM, [email protected] wrote:

The i/o accounting published in /proc/diskstats for mdraid is currently broken. md_make_request does the accounting for every bio passed but when a bio needs to be split, all the split bios are also submitted through md_make_request, resulting in multiple accounting.

Hi Jeff,

I sent a patch a few days ago which should fix this issue. Can you check it out?

https://marc.info/?l=linux-raid&m=159102814820539

I need to account some extra statistics for bio such as latency and size, so it is kind of relies on your patch, then I read the code again.

And besides my previous comment. I think you don't need clone bio for all personalities. For md-multipath, raid1 and raid10, you can track the start time by add it to those structures (multipath_bh, r1bio and r10bio), then one extra copy could be avoided.

What do you think?

Thanks,
Guoqing

#3 lore raw
Quoted 18 lines · Guoqing Jiang

Hi Artur,

Quoted 11 lines · Artur Paszkiewicz
Quoted 4 lines · On 6/5/20 10:19 PM, [email protected] wrote:

The i/o accounting published in /proc/diskstats for mdraid is currently broken. md_make_request does the accounting for every bio passed but when a bio needs to be split, all the split bios are also submitted through md_make_request, resulting in multiple accounting.

Hi Jeff,

I sent a patch a few days ago which should fix this issue. Can you check it out?

https://marc.info/?l=linux-raid&m=159102814820539

I need to account some extra statistics for bio such as latency and size, so it is kind of relies on your patch, then I read the code again.

Quoted 6 lines

And besides my previous comment. I think you don't need clone bio for all personalities. For md-multipath, raid1 and raid10, you can track the start time by add it to those structures (multipath_bh, r1bio and r10bio), then one extra copy could be avoided.

What do you think?

You're right, cloning can be avoided for those personalities. I wanted to keep it clean and centralized in the generic md code. I think we should have a common completion callback and some common request structure for all md personalities, like struct md_io which I'm using in my patch. How about we add it to the existing stucts like r1bio etc. and use that for io accounting, and clone the bio with the struct otherwise? Or maybe remove the cloning from the personalities and instead make the bio cloned in md_make_request available to them? Does this make sense?

Thanks,
Artur

#4 lore raw
Quoted 24 lines · Artur Paszkiewicz
Quoted 17 lines · Guoqing Jiang

Hi Artur,

Quoted 11 lines · Artur Paszkiewicz
Quoted 4 lines · On 6/5/20 10:19 PM, [email protected] wrote:

The i/o accounting published in /proc/diskstats for mdraid is currently broken. md_make_request does the accounting for every bio passed but when a bio needs to be split, all the split bios are also submitted through md_make_request, resulting in multiple accounting.

Hi Jeff,

I sent a patch a few days ago which should fix this issue. Can you check it out?

https://marc.info/?l=linux-raid&m=159102814820539

I need to account some extra statistics for bio such as latency and size, so it is kind of relies on your patch, then I read the code again.

Quoted 6 lines

And besides my previous comment. I think you don't need clone bio for all personalities. For md-multipath, raid1 and raid10, you can track the start time by add it to those structures (multipath_bh, r1bio and r10bio), then one extra copy could be avoided.

What do you think?

Quoted 8 lines

You're right, cloning can be avoided for those personalities. I wanted to keep it clean and centralized in the generic md code. I think we should have a common completion callback and some common request structure for all md personalities, like struct md_io which I'm using in my patch.How about we add it to the existing stucts like r1bio etc. and use that for io accounting, and clone the bio with the struct otherwise? Or maybe remove the cloning from the personalities and instead make the bio cloned in md_make_request available to them? Does this make sense?

Centralization definitely has it's advantage, but given the difference among personalities, not sure it is possible or make sense to clone bio in common md layer.

For raid1/10, we at least need to care about barrier and read balance before clone bio, and I would prefer to not change current mechanism since it is really a big modification from my understanding.

Thanks,
Guoqing

#5 lore raw
In reply to Artur Paszkiewicz
Quoted 6 lines · Artur Paszkiewicz
Quoted 4 lines · On 6/5/20 10:19 PM, [email protected] wrote:

The i/o accounting published in /proc/diskstats for mdraid is currently broken. md_make_request does the accounting for every bio passed but when a bio needs to be split, all the split bios are also submitted through md_make_request, resulting in multiple accounting.

Quoted 6 lines

Hi Jeff,

I sent a patch a few days ago which should fix this issue. Can you check it out?

https://marc.info/?l=linux-raid&m=159102814820539

It scratches the same itch. I just didn't want to clone every bio just to get stats.

-Jeff

Signature · 2 lines
Jeff Mahoney
Director, SUSE Labs Data & Performance
#6 lore raw
In reply to [email protected]
Thank you for the patch! Perhaps something to improve:

Hi,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on linus/master]
[also build test WARNING on next-20200608]
[cannot apply to v5.7]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please see https://stackoverflow.com/a/37406982]

url:    https://github.com/0day-ci/linux/commits/jeffm-suse-com/mdraid-fix-read-write-bytes-accounting/20200607-085457
base:   https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git b170290c2836c40ab565736ba37681eb3dfd79b8
config: m68k-randconfig-r011-20200607 (attached as .config)
compiler: m68k-linux-gcc (GCC) 9.3.0

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross ARCH=m68k

If you fix the issue, kindly add following tag as appropriate

All warnings (new ones prefixed by >>, old ones prefixed by <<):

drivers/md/md.c: In function 'md_make_request':

>> drivers/md/md.c:485:15: warning: variable 'sectors' set but not used [-Wunused-but-set-variable]

485 | unsigned int sectors;
| ^~~~~~~
drivers/md/md.c: In function 'bind_rdev_to_array':
drivers/md/md.c:2454:27: warning: suggest braces around empty body in an 'if' statement [-Wempty-body]
2454 | /* failure here is OK */;
| ^
drivers/md/md.c: In function 'slot_store':
drivers/md/md.c:3215:28: warning: suggest braces around empty body in an 'if' statement [-Wempty-body]
3215 | /* failure here is OK */;
| ^
drivers/md/md.c: In function 'remove_and_add_spares':
drivers/md/md.c:9073:29: warning: suggest braces around empty body in an 'if' statement [-Wempty-body]
9073 | /* failure here is OK */;
| ^

vim +/sectors +485 drivers/md/md.c

eb17e42dfd959c Jeff Mahoney         2020-06-05  480  
dece16353ef47d Jens Axboe           2015-11-05  481  static blk_qc_t md_make_request(struct request_queue *q, struct bio *bio)
^1da177e4c3f41 Linus Torvalds       2005-04-16  482  {
490773268cf64f NeilBrown            2010-03-25  483  	const int rw = bio_data_dir(bio);
e4fc5a74293fbe Christoph Hellwig    2020-05-08  484  	struct mddev *mddev = bio->bi_disk->private_data;
e91ece5590b3c7 Chris Mason          2011-02-07 @485  	unsigned int sectors;
490773268cf64f NeilBrown            2010-03-25  486  
62f7b1989c02fe Guilherme G. Piccoli 2019-09-03  487  	if (unlikely(test_bit(MD_BROKEN, &mddev->flags)) && (rw == WRITE)) {
62f7b1989c02fe Guilherme G. Piccoli 2019-09-03  488  		bio_io_error(bio);
62f7b1989c02fe Guilherme G. Piccoli 2019-09-03  489  		return BLK_QC_T_NONE;
62f7b1989c02fe Guilherme G. Piccoli 2019-09-03  490  	}
62f7b1989c02fe Guilherme G. Piccoli 2019-09-03  491  
af67c31fba3b87 NeilBrown            2017-06-18  492  	blk_queue_split(q, &bio);
54efd50bfd873e Kent Overstreet      2015-04-23  493  
274d8cbde1bc3b NeilBrown            2016-01-04  494  	if (mddev == NULL || mddev->pers == NULL) {
6712ecf8f64811 NeilBrown            2007-09-27  495  		bio_io_error(bio);
dece16353ef47d Jens Axboe           2015-11-05  496  		return BLK_QC_T_NONE;
^1da177e4c3f41 Linus Torvalds       2005-04-16  497  	}
bbfa57c0f2243a Sebastian Riemer     2013-02-21  498  	if (mddev->ro == 1 && unlikely(rw == WRITE)) {
4246a0b63bd8f5 Christoph Hellwig    2015-07-20  499  		if (bio_sectors(bio) != 0)
4e4cbee93d5613 Christoph Hellwig    2017-06-03  500  			bio->bi_status = BLK_STS_IOERR;
4246a0b63bd8f5 Christoph Hellwig    2015-07-20  501  		bio_endio(bio);
dece16353ef47d Jens Axboe           2015-11-05  502  		return BLK_QC_T_NONE;
bbfa57c0f2243a Sebastian Riemer     2013-02-21  503  	}
490773268cf64f NeilBrown            2010-03-25  504  
e91ece5590b3c7 Chris Mason          2011-02-07  505  	/*
e91ece5590b3c7 Chris Mason          2011-02-07  506  	 * save the sectors now since our bio can
e91ece5590b3c7 Chris Mason          2011-02-07  507  	 * go away inside make_request
e91ece5590b3c7 Chris Mason          2011-02-07  508  	 */
e91ece5590b3c7 Chris Mason          2011-02-07  509  	sectors = bio_sectors(bio);
9c573de3283af0 Shaohua Li           2016-04-25  510  	/* bio could be mergeable after passing to underlayer */
1eff9d322a4442 Jens Axboe           2016-08-05  511  	bio->bi_opf &= ~REQ_NOMERGE;
393debc23c7820 Shaohua Li           2017-09-21  512  
393debc23c7820 Shaohua Li           2017-09-21  513  	md_handle_request(mddev, bio);
490773268cf64f NeilBrown            2010-03-25  514  
dece16353ef47d Jens Axboe           2015-11-05  515  	return BLK_QC_T_NONE;
409c57f3801701 NeilBrown            2009-03-31  516  }
409c57f3801701 NeilBrown            2009-03-31  517  

:::::: The code at line 485 was first introduced by commit
:::::: e91ece5590b3c728624ab57043fc7a05069c604a md_make_request: don't touch the bio after calling make_request

:::::: TO: Chris Mason <[email protected]>
:::::: CC: NeilBrown <[email protected]>

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/[email protected]