[PATCH] dm: Support turning off io stat accounting

From: linan666
Date: Mon Jun 12 2023 - 21:38:15 EST


From: Li Nan <linan122@xxxxxxxxxx>

Commit bc58ba9468d9 ("block: add sysfs file for controlling io stats
accounting") allowed user to turn off disk stat accounting completely by
queue flag QUEUE_FLAG_IO_STAT. In dm, this flag is not set. io stats
is continuously counted and cannot be turn off.

Support turning off io stat accounting for dm. Set QUEUE_FLAG_IO_STAT for
dm request_queue. When the io starts, we account the io using DM_IO_STAT
dm_io flag to avoid io stats disable in the middle of the io. DM statistics
is independent of block io stat and remains unchanged.

Signed-off-by: Li Nan <linan122@xxxxxxxxxx>
---
drivers/md/dm-core.h | 3 ++-
drivers/md/dm-table.c | 1 +
drivers/md/dm.c | 15 ++++++++++-----
3 files changed, 13 insertions(+), 6 deletions(-)

diff --git a/drivers/md/dm-core.h b/drivers/md/dm-core.h
index aecab0c0720f..8b0af3e1331a 100644
--- a/drivers/md/dm-core.h
+++ b/drivers/md/dm-core.h
@@ -307,7 +307,8 @@ struct dm_io {
*/
enum {
DM_IO_ACCOUNTED,
- DM_IO_WAS_SPLIT
+ DM_IO_WAS_SPLIT,
+ DM_IO_STAT
};

static inline bool dm_io_flagged(struct dm_io *io, unsigned int bit)
diff --git a/drivers/md/dm-table.c b/drivers/md/dm-table.c
index 1398f1d6e83e..bd5e16a3f2b1 100644
--- a/drivers/md/dm-table.c
+++ b/drivers/md/dm-table.c
@@ -1936,6 +1936,7 @@ int dm_table_set_restrictions(struct dm_table *t, struct request_queue *q,
*/
q->limits = *limits;

+ blk_queue_flag_set(QUEUE_FLAG_IO_STAT, q);
if (dm_table_supports_nowait(t))
blk_queue_flag_set(QUEUE_FLAG_NOWAIT, q);
else
diff --git a/drivers/md/dm.c b/drivers/md/dm.c
index 3b694ba3a106..82b73536bd93 100644
--- a/drivers/md/dm.c
+++ b/drivers/md/dm.c
@@ -511,11 +511,14 @@ static void dm_io_acct(struct dm_io *io, bool end)
else
sectors = io->sectors;

- if (!end)
- bdev_start_io_acct(bio->bi_bdev, bio_op(bio), start_time);
- else
- bdev_end_io_acct(bio->bi_bdev, bio_op(bio), sectors,
- start_time);
+ if (dm_io_flagged(io, DM_IO_STAT)) {
+ if (!end)
+ bdev_start_io_acct(bio->bi_bdev, bio_op(bio),
+ start_time);
+ else
+ bdev_end_io_acct(bio->bi_bdev, bio_op(bio),
+ sectors, start_time);
+ }

if (static_branch_unlikely(&stats_enabled) &&
unlikely(dm_stats_used(&md->stats))) {
@@ -592,6 +595,8 @@ static struct dm_io *alloc_io(struct mapped_device *md, struct bio *bio)
spin_lock_init(&io->lock);
io->start_time = jiffies;
io->flags = 0;
+ if (blk_queue_io_stat(bio->bi_bdev->bd_disk->queue))
+ dm_io_set_flag(io, DM_IO_STAT);

if (static_branch_unlikely(&stats_enabled))
dm_stats_record_start(&md->stats, &io->stats_aux);
--
2.39.2