Skip to contentSkip to conversation

block/dm: fix bio-based DM IO accounting

PATCH Superseded Mike Snitzer 2 participants 5 messages Jan 27, 2022

Subsystems (2)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover block/dm: fix bio-based DM IO accounting

Hi Jens,

Just over 3 years ago, with commit a1e1cb72d9649 ("dm: fix redundant IO accounting for bios that need splitting") I focused too narrowly on fixing the reported potential for redundant accounting for IO totals. Which, at least mentally for me, papered over how inaccurate all other bio-based DM's IO accounting is for bios that get split.

This set fixes things up properly by allowing DM to start IO accounting _after_ IO is submitted and a split is occurred. The proper start_time is still established (prior to submission), it is passed in to a new __bio_start_io_acct(). This eliminates the need for any DM hack to rewind block core's excessive accounting in the face of a split bio recursing back to block core.

All said: If you'd provide your Acked-by(s) I'm happy to send this set to Linus for v5.17-rc (and shepherd the changes into stable@ kernels). Or you're welcome to pickup this set to send along (I'd obviously still do any stable@ backports). NOTE: the 3rd patch references the linux-dm.git commit id for the 1st patch.. so that'll require tweaking no matter who sends the changes to Linus.

Please advise, thanks.
Mike

Mike Snitzer (3):
block: add __bio_start_io_acct() to control start_time
dm: revert partial fix for redundant bio-based IO accounting
dm: properly fix redundant bio-based IO accounting

 block/blk-core.c       | 27 ++++++++++++++++++++-------
 drivers/md/dm.c        | 20 +++-----------------
 include/linux/blkdev.h |  1 +
 3 files changed, 24 insertions(+), 24 deletions(-)
Signature · 1 line
2.15.0
#1 lore raw

PATCH 1/3 block: add __bio_start_io_acct() to control start_time

__bio_start_io_acct() interface is like bio_start_io_acct() that allows start_time to be passed in. This gives drivers the ability to defer starting accounting until after IO is issued (but possibily not entirely due to bio splitting).

---
 block/blk-core.c       | 27 ++++++++++++++++++++-------
 include/linux/blkdev.h |  1 +
 2 files changed, 21 insertions(+), 7 deletions(-)
block/blk-core.c +20 -7
index 97f8bc8d3a79..18cd12fee67d 100644
@@ -1060,21 +1060,30 @@ void update_io_ticks(struct block_device *part, unsigned long now, bool end)
 	}
 }
 
-static unsigned long __part_start_io_acct(struct block_device *part,
-					  unsigned int sectors, unsigned int op)
+static void __part_start_io_acct(struct block_device *part, unsigned int sectors,
+				 unsigned int op, unsigned long start_time)
 {
 	const int sgrp = op_stat_group(op);
-	unsigned long now = READ_ONCE(jiffies);
 
 	part_stat_lock();
-	update_io_ticks(part, now, false);
+	update_io_ticks(part, start_time, false);
 	part_stat_inc(part, ios[sgrp]);
 	part_stat_add(part, sectors[sgrp], sectors);
 	part_stat_local_inc(part, in_flight[op_is_write(op)]);
 	part_stat_unlock();
+}
 
-	return now;
+/**
+ * __bio_start_io_acct - start I/O accounting for bio based drivers
+ * @bio:	bio to start account for
+ * @start_time:	start time that should be passed back to bio_end_io_acct().
+ */
+void __bio_start_io_acct(struct bio *bio, unsigned long start_time)
+{
+	__part_start_io_acct(bio->bi_bdev, bio_sectors(bio),
+			     bio_op(bio), start_time);
 }
+EXPORT_SYMBOL_GPL(__bio_start_io_acct);
 
 /**
  * bio_start_io_acct - start I/O accounting for bio based drivers
@@ -1084,14 +1093,18 @@ static unsigned long __part_start_io_acct(struct block_device *part,
  */
 unsigned long bio_start_io_acct(struct bio *bio)
 {
-	return __part_start_io_acct(bio->bi_bdev, bio_sectors(bio), bio_op(bio));
+	unsigned long now = READ_ONCE(jiffies);
+	__bio_start_io_acct(bio, now);
+	return now;
 }
 EXPORT_SYMBOL_GPL(bio_start_io_acct);
 
 unsigned long disk_start_io_acct(struct gendisk *disk, unsigned int sectors,
 				 unsigned int op)
 {
-	return __part_start_io_acct(disk->part0, sectors, op);
+	unsigned long now = READ_ONCE(jiffies);
+	__part_start_io_acct(disk->part0, sectors, op, now);
+	return now;
 }
 EXPORT_SYMBOL(disk_start_io_acct);
 
include/linux/blkdev.h +1 -0
index 9c95df26fc26..ed3cd5f7f984 100644
@@ -1258,6 +1258,7 @@ unsigned long disk_start_io_acct(struct gendisk *disk, unsigned int sectors,
 void disk_end_io_acct(struct gendisk *disk, unsigned int op,
 		unsigned long start_time);
 
+void __bio_start_io_acct(struct bio *bio, unsigned long start_time);
 unsigned long bio_start_io_acct(struct bio *bio);
 void bio_end_io_acct_remapped(struct bio *bio, unsigned long start_time,
 		struct block_device *orig_bdev);
Signature · 1 line
2.15.0
#3 lore raw

PATCH 2/3 dm: revert partial fix for redundant bio-based IO accounting

In reply to Mike Snitzer

Reverts a1e1cb72d9649 ("dm: fix redundant IO accounting for bios that need splitting") because it was too narrow in scope (only addressed redundant 'sectors[]' accounting and not ios, nsecs[], etc).

---
 drivers/md/dm.c | 15 ---------------
 1 file changed, 15 deletions(-)
drivers/md/dm.c +0 -15
index c0ae8087c602..9849114b3c08 100644
@@ -1442,9 +1442,6 @@ static void init_clone_info(struct clone_info *ci, struct mapped_device *md,
 	ci->sector = bio->bi_iter.bi_sector;
 }
 
-#define __dm_part_stat_sub(part, field, subnd)	\
-	(part_stat_get(part, field) -= (subnd))
-
 /*
  * Entry point to split a bio into clones and submit them to the targets.
  */
@@ -1480,18 +1477,6 @@ static void __split_and_process_bio(struct mapped_device *md,
 						  GFP_NOIO, &md->queue->bio_split);
 			ci.io->orig_bio = b;
 
-			/*
-			 * Adjust IO stats for each split, otherwise upon queue
-			 * reentry there will be redundant IO accounting.
-			 * NOTE: this is a stop-gap fix, a proper fix involves
-			 * significant refactoring of DM core's bio splitting
-			 * (by eliminating DM's splitting and just using bio_split)
-			 */
-			part_stat_lock();
-			__dm_part_stat_sub(dm_disk(md)->part0,
-					   sectors[op_stat_group(bio_op(bio))], ci.sector_count);
-			part_stat_unlock();
-
 			bio_chain(b, bio);
 			trace_block_split(b, bio->bi_iter.bi_sector);
 			submit_bio_noacct(bio);
Signature · 1 line
2.15.0
#4 lore raw

PATCH 3/3 dm: properly fix redundant bio-based IO accounting

In reply to Mike Snitzer

Record the start_time for a bio but defer the starting block core's IO accounting until after IO is submitted using __bio_start_io_acct().

This approach avoids the need to mess around with any of the individual IO stats in response to a bio_split() that follows bio submission.

Depends-on: e8d7405fccc6 ("block: add __bio_start_io_acct() to control start_time")

---
 drivers/md/dm.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/md/dm.c +3 -2
index 9849114b3c08..144436301a57 100644
@@ -489,7 +489,7 @@ static void start_io_acct(struct dm_io *io)
 	struct mapped_device *md = io->md;
 	struct bio *bio = io->orig_bio;
 
-	io->start_time = bio_start_io_acct(bio);
+	__bio_start_io_acct(bio, io->start_time);
 	if (unlikely(dm_stats_used(&md->stats)))
 		dm_stats_account_io(&md->stats, bio_data_dir(bio),
 				    bio->bi_iter.bi_sector, bio_sectors(bio),
@@ -535,7 +535,7 @@ static struct dm_io *alloc_io(struct mapped_device *md, struct bio *bio)
 	io->md = md;
 	spin_lock_init(&io->endio_lock);
 
-	start_io_acct(io);
+	io->start_time = READ_ONCE(jiffies);
 
 	return io;
 }
@@ -1482,6 +1482,7 @@ static void __split_and_process_bio(struct mapped_device *md,
 			submit_bio_noacct(bio);
 		}
 	}
+	start_io_acct(ci.io);
 
 	/* drop the extra reference count */
 	dm_io_dec_pending(ci.io, errno_to_blk_status(error));
Signature · 1 line
2.15.0
#2 lore raw

Re: [dm-devel] [PATCH 1/3] block: add __bio_start_io_acct() to control start_time

In reply to Mike Snitzer

block/blk-core.c void update_io_ticks(struct block_device *part, unsigned long now, bool end) · line 1064

Quoted 4 lines
block/blk-core.c +2 -2
-static unsigned long __part_start_io_acct(struct block_device *part,
-					  unsigned int sectors, unsigned int op)
+static void __part_start_io_acct(struct block_device *part, unsigned int sectors,
+				 unsigned int op, unsigned long start_time)

Please avoid the overly long line.

block/blk-core.c void update_io_ticks(struct block_device *part, unsigned long now, bool end) · line 1081

Quoted 6 lines
block/blk-core.c +6 -0
+/**
+ * __bio_start_io_acct - start I/O accounting for bio based drivers
+ * @bio:	bio to start account for
+ * @start_time:	start time that should be passed back to bio_end_io_acct().
+ */
+void __bio_start_io_acct(struct bio *bio, unsigned long start_time)

I'd name this something like bio_start_io_acct_time to be a little more descriptive

block/blk-core.c static unsigned long __part_start_io_acct(struct block_device *part, · line 1098

Quoted 3 lines
block/blk-core.c +3 -0
+	unsigned long now = READ_ONCE(jiffies);
+	__bio_start_io_acct(bio, now);
+	return now;

Plase add an empty line after the variable declaration.

block/blk-core.c static unsigned long __part_start_io_acct(struct block_device *part, · line 1107

Quoted 10 lines

}
EXPORT_SYMBOL_GPL(bio_start_io_acct);

unsigned long disk_start_io_acct(struct gendisk *disk, unsigned int sectors,

block/blk-core.c +3 -1
 				 unsigned int op)
 {
-	return __part_start_io_acct(disk->part0, sectors, op);
+	unsigned long now = READ_ONCE(jiffies);
+	__part_start_io_acct(disk->part0, sectors, op, now);
+	return now;

I wonder if just returning the passed in time from __part_start_io_acct wouldn't be a bit cleaner to avoid all the extra local variables.