Skip to contentSkip to conversation

blk-throtl: optimize IOPS throttle for large IO scenarios

PATCH Superseded brookxu 2 participants 9 messages Jul 16, 2021 – Jul 28, 2021

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

brookxu

author
#0 lore raw

PATCH 1/1 blk-throtl: optimize IOPS throttle for large IO scenarios

From: Chunguang Xu <[email protected]>

After patch 54efd50 (block: make generic_make_request handle arbitrarily sized bios), the IO through io-throttle may be larger, and these IOs may be further split into more small IOs. However, IOPS throttle does not seem to be aware of this change, which makes the calculation of IOPS of large IOs incomplete, resulting in disk-side IOPS that does not meet expectations. Maybe we should fix this problem.

We can reproduce it by set max_sectors_kb of disk to 128, set blkio.write_iops_throttle to 100, run a dd instance inside blkio and use iostat to watch IOPS:

dd if=/dev/zero of=/dev/sdb bs=1M count=1000 oflag=direct

As a result, without this change the average IOPS is 1995, with this change the IOPS is 98.

---
 block/blk-merge.c    |  2 ++
 block/blk-throttle.c | 34 ++++++++++++++++++++++++++++++++++
 block/blk.h          |  2 ++
 3 files changed, 38 insertions(+)
block/blk-merge.c +2 -0
index a11b3b5..86ff943 100644
@@ -348,6 +348,8 @@ void __blk_queue_split(struct bio **bio, unsigned int *nr_segs)
 		trace_block_split(split, (*bio)->bi_iter.bi_sector);
 		submit_bio_noacct(*bio);
 		*bio = split;
+
+		blk_throtl_recharge_bio(*bio);
 	}
 }
 
block/blk-throttle.c +34 -0
index b1b22d8..1967438 100644
@@ -2176,6 +2176,40 @@ static inline void throtl_update_latency_buckets(struct throtl_data *td)
 }
 #endif
 
+void blk_throtl_recharge_bio(struct bio *bio)
+{
+	bool rw = bio_data_dir(bio);
+	struct blkcg_gq *blkg = bio->bi_blkg;
+	struct throtl_grp *tg = blkg_to_tg(blkg);
+	u32 iops_limit = tg_iops_limit(tg, rw);
+
+	if (iops_limit == UINT_MAX)
+		return;
+
+	/*
+	 * If previous slice expired, start a new one otherwise renew/extend
+	 * existing slice to make sure it is at least throtl_slice interval
+	 * long since now. New slice is started only for empty throttle group.
+	 * If there is queued bio, that means there should be an active
+	 * slice and it should be extended instead.
+	 */
+	if (throtl_slice_used(tg, rw) && !(tg->service_queue.nr_queued[rw]))
+		throtl_start_new_slice(tg, rw);
+	else {
+		if (time_before(tg->slice_end[rw],
+		    jiffies + tg->td->throtl_slice))
+			throtl_extend_slice(tg, rw,
+				jiffies + tg->td->throtl_slice);
+	}
+
+	/* Recharge the bio to the group, as some BIOs will be further split
+	 * after passing through the throttle, causing the actual IOPS to
+	 * be greater than the expected value.
+	 */
+	tg->last_io_disp[rw]++;
+	tg->io_disp[rw]++;
+}
+
 bool blk_throtl_bio(struct bio *bio)
 {
 	struct request_queue *q = bio->bi_bdev->bd_disk->queue;
block/blk.h +2 -0
index 4b885c0..067634a 100644
@@ -293,12 +293,14 @@ struct io_cq *ioc_create_icq(struct io_context *ioc, struct request_queue *q,
 extern int blk_throtl_init(struct request_queue *q);
 extern void blk_throtl_exit(struct request_queue *q);
 extern void blk_throtl_register_queue(struct request_queue *q);
+extern void blk_throtl_recharge_bio(struct bio *bio);
 bool blk_throtl_bio(struct bio *bio);
 #else /* CONFIG_BLK_DEV_THROTTLING */
 static inline int blk_throtl_init(struct request_queue *q) { return 0; }
 static inline void blk_throtl_exit(struct request_queue *q) { }
 static inline void blk_throtl_register_queue(struct request_queue *q) { }
 static inline bool blk_throtl_bio(struct bio *bio) { return false; }
+static inline void blk_throtl_recharge_bio(struct bio *bio) { }
 #endif /* CONFIG_BLK_DEV_THROTTLING */
 #ifdef CONFIG_BLK_DEV_THROTTLING_LOW
 extern ssize_t blk_throtl_sample_time_show(struct request_queue *q, char *page);
Signature · 1 line
1.8.3.1
maintainer
#1 lore raw

Hello,

block/blk-merge.c void __blk_queue_split(struct bio **bio, unsigned int *nr_segs) · line 352

Quoted 10 lines · brookxu
block/blk-merge.c +2 -0
index a11b3b5..86ff943 100644
@@ -348,6 +348,8 @@ void __blk_queue_split(struct bio **bio, unsigned int *nr_segs)
 		trace_block_split(split, (*bio)->bi_iter.bi_sector);
 		submit_bio_noacct(*bio);
 		*bio = split;
+
+		blk_throtl_recharge_bio(*bio);

I don't think we're holding the queue lock here.

Quoted 34 lines
block/blk-merge.c +0 -0
 	}
 }
 
block/blk-throttle.c +23 -0
index b1b22d8..1967438 100644
@@ -2176,6 +2176,40 @@ static inline void throtl_update_latency_buckets(struct throtl_data *td)
 }
 #endif
 
+void blk_throtl_recharge_bio(struct bio *bio)
+{
+	bool rw = bio_data_dir(bio);
+	struct blkcg_gq *blkg = bio->bi_blkg;
+	struct throtl_grp *tg = blkg_to_tg(blkg);
+	u32 iops_limit = tg_iops_limit(tg, rw);
+
+	if (iops_limit == UINT_MAX)
+		return;
+
+	/*
+	 * If previous slice expired, start a new one otherwise renew/extend
+	 * existing slice to make sure it is at least throtl_slice interval
+	 * long since now. New slice is started only for empty throttle group.
+	 * If there is queued bio, that means there should be an active
+	 * slice and it should be extended instead.
+	 */
+	if (throtl_slice_used(tg, rw) && !(tg->service_queue.nr_queued[rw]))
+		throtl_start_new_slice(tg, rw);
+	else {
+		if (time_before(tg->slice_end[rw],
+		    jiffies + tg->td->throtl_slice))
+			throtl_extend_slice(tg, rw,

block/blk-throttle.c static inline void throtl_update_latency_buckets(struct throtl_data *td) · line 2211

Quoted 10 lines
block/blk-throttle.c +10 -0
+				jiffies + tg->td->throtl_slice);
+	}
+
+	/* Recharge the bio to the group, as some BIOs will be further split
+	 * after passing through the throttle, causing the actual IOPS to
+	 * be greater than the expected value.
+	 */
+	tg->last_io_disp[rw]++;
+	tg->io_disp[rw]++;
+}

But blk-throtl expects queue lock to be held.

How about doing something simpler? Just estimate how many bios a given bio is gonna be and charge it outright? The calculation will be duplicated between the split path but that seems like the path of least resistance here.

Thanks.

Signature · 1 line
tejun
author
#2 lore raw

Tejun Heo wrote on 2021/7/17 0:09:

Quoted 15 lines

Hello,

block/blk-merge.c void __blk_queue_split(struct bio **bio, unsigned int *nr_segs) · line 352

Quoted 10 lines · brookxu
block/blk-merge.c +2 -0
index a11b3b5..86ff943 100644
@@ -348,6 +348,8 @@ void __blk_queue_split(struct bio **bio, unsigned int *nr_segs)
 		trace_block_split(split, (*bio)->bi_iter.bi_sector);
 		submit_bio_noacct(*bio);
 		*bio = split;
+
+		blk_throtl_recharge_bio(*bio);

I don't think we're holding the queue lock here.

sorry, some kind of synchronization mechanism is really needed here. But the use of queue_lock here may be unsafe, since it is difficult for us to control the lock on the split path.

Quoted 47 lines
Quoted 34 lines
block/blk-merge.c +0 -0
 	}
 }
 
block/blk-throttle.c +23 -0
index b1b22d8..1967438 100644
@@ -2176,6 +2176,40 @@ static inline void throtl_update_latency_buckets(struct throtl_data *td)
 }
 #endif
 
+void blk_throtl_recharge_bio(struct bio *bio)
+{
+	bool rw = bio_data_dir(bio);
+	struct blkcg_gq *blkg = bio->bi_blkg;
+	struct throtl_grp *tg = blkg_to_tg(blkg);
+	u32 iops_limit = tg_iops_limit(tg, rw);
+
+	if (iops_limit == UINT_MAX)
+		return;
+
+	/*
+	 * If previous slice expired, start a new one otherwise renew/extend
+	 * existing slice to make sure it is at least throtl_slice interval
+	 * long since now. New slice is started only for empty throttle group.
+	 * If there is queued bio, that means there should be an active
+	 * slice and it should be extended instead.
+	 */
+	if (throtl_slice_used(tg, rw) && !(tg->service_queue.nr_queued[rw]))
+		throtl_start_new_slice(tg, rw);
+	else {
+		if (time_before(tg->slice_end[rw],
+		    jiffies + tg->td->throtl_slice))
+			throtl_extend_slice(tg, rw,

block/blk-throttle.c static inline void throtl_update_latency_buckets(struct throtl_data *td) · line 2211

Quoted 10 lines
block/blk-throttle.c +10 -0
+				jiffies + tg->td->throtl_slice);
+	}
+
+	/* Recharge the bio to the group, as some BIOs will be further split
+	 * after passing through the throttle, causing the actual IOPS to
+	 * be greater than the expected value.
+	 */
+	tg->last_io_disp[rw]++;
+	tg->io_disp[rw]++;
+}

But blk-throtl expects queue lock to be held.

Quoted 4 lines

How about doing something simpler? Just estimate how many bios a given bio is gonna be and charge it outright? The calculation will be duplicated between the split path but that seems like the path of least resistance here.

I have tried this method, the code redundancy is indeed a bit high, it may not be very convenient for code maintenance. In addition to this problem, since we add a large value at a time, the fluctuation of IOPS will be relatively large. Since blk_throtl_recharge_bio() does not need to participate in the maintenance of the state machine, we only need to protect some fields of tg, so can we add a new spin_lock to tg instead of queue_lock to solve the synchronization problem ? Just a idea, Thanks.

Quoted 2 lines

Thanks.

author
#3 lore raw
In reply to Tejun Heo

Hi Tejun:

In order to avoid code duplication and IOPS stability problems caused by estimating the equivalent number of IOs, and to avoid potential deadlock problems caused by synchronization through queue_lock. I tried to count the number of splited IOs in the current window through two atomic counters. Add the value of the atomic variable when calculating io_disp[rw], which can also avoid the problem of inaccurate IOPS in large IO scenarios. How do you think of this approach? Thanks for your time.

The following are related changes. If that is ok, I will send the v2 version later.

---
 block/blk-merge.c    |  2 ++
 block/blk-throttle.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++-----
 block/blk.h          |  2 ++
 3 files changed, 64 insertions(+), 6 deletions(-)
block/blk-merge.c +2 -0
index a11b3b5..86ff943 100644
@@ -348,6 +348,8 @@ void __blk_queue_split(struct bio **bio, unsigned int *nr_segs)
 		trace_block_split(split, (*bio)->bi_iter.bi_sector);
 		submit_bio_noacct(*bio);
 		*bio = split;
+
+		blk_throtl_recharge_bio(*bio);
 	}
 }
 
block/blk-throttle.c +60 -6

139 lines not on this page · raw message

block/blk.h +2 -0
index 4b885c0..9e925bf 100644
@@ -293,11 +293,13 @@ struct io_cq *ioc_create_icq(struct io_context *ioc, struct request_queue *q,
 extern int blk_throtl_init(struct request_queue *q);
 extern void blk_throtl_exit(struct request_queue *q);
 extern void blk_throtl_register_queue(struct request_queue *q);
+extern void blk_throtl_recharge_bio(struct bio *bio);
 bool blk_throtl_bio(struct bio *bio);
 #else /* CONFIG_BLK_DEV_THROTTLING */
 static inline int blk_throtl_init(struct request_queue *q) { return 0; }
 static inline void blk_throtl_exit(struct request_queue *q) { }
 static inline void blk_throtl_register_queue(struct request_queue *q) { }
+static inline void blk_throtl_recharge_bio(struct bio *bio) { }
 static inline bool blk_throtl_bio(struct bio *bio) { return false; }
 #endif /* CONFIG_BLK_DEV_THROTTLING */
 #ifdef CONFIG_BLK_DEV_THROTTLING_LOW
Signature · 1 line
1.8.3.1
maintainer
#4 lore raw

Hello,

Quoted 6 lines · brookxu

In order to avoid code duplication and IOPS stability problems caused by estimating the equivalent number of IOs, and to avoid potential deadlock problems caused by synchronization through queue_lock. I tried to count the number of splited IOs in the current window through two atomic counters. Add the value of the atomic variable when calculating io_disp[rw], which can also avoid the problem of inaccurate IOPS in large IO scenarios. How do you think of this approach? Thanks for your time.

I guess it's okay but am still not a big fan of adding another hook. This is primarily because blk-throtl is sitting too early in the stack - e.g. rq_qos is doing the same thing but sits after the split path - and it's a bit nasty to add an additional hook for it.

Do you think it can be an option to relocate the blk-throtl hooks to the same spots as rq-qos or, even better, make it use rq-qos?

Thanks.

Signature · 1 line
tejun
author
#5 lore raw

Tejun Heo wrote on 2021/7/27 5:46:

Quoted 10 lines

Hello,

Quoted 6 lines · brookxu

In order to avoid code duplication and IOPS stability problems caused by estimating the equivalent number of IOs, and to avoid potential deadlock problems caused by synchronization through queue_lock. I tried to count the number of splited IOs in the current window through two atomic counters. Add the value of the atomic variable when calculating io_disp[rw], which can also avoid the problem of inaccurate IOPS in large IO scenarios. How do you think of this approach? Thanks for your time.

Quoted 7 lines

I guess it's okay but am still not a big fan of adding another hook. This is primarily because blk-throtl is sitting too early in the stack - e.g. rq_qos is doing the same thing but sits after the split path - and it's a bit nasty to add an additional hook for it.

Do you think it can be an option to relocate the blk-throtl hooks to the same spots as rq-qos or, even better, make it use rq-qos?

Make blk-throttle use rq-qos may be more elegant. But I found that there may be at least one problem that is difficult to solve. blk-throttle supports separate throttle for read and write IOs, which means that we cannot suspend tasks during throttle, but rq-qos throttle IOs by suspending tasks.

We may be able to relocate the blk-throttle hooks to the rq-qos hooks. Since we may not be able to replace the throttle hook, in this case, if we register a rq-qos to the system, part of the blk-throttle hooks is in rq-qos and part hooks not, which feels a bit confusing. In addition, we may need to implement more hooks, such as IO merge hook.

Thanks for you time.

Quoted 2 lines

Thanks.

maintainer
#6 lore raw

Hello,

Quoted 4 lines · brookxu

Make blk-throttle use rq-qos may be more elegant. But I found that there may be at least one problem that is difficult to solve. blk-throttle supports separate throttle for read and write IOs, which means that we cannot suspend tasks during throttle, but rq-qos throttle IOs by suspending tasks.

Ah, right, I forgot about that.

Quoted 4 lines

We may be able to relocate the blk-throttle hooks to the rq-qos hooks. Since we may not be able to replace the throttle hook, in this case, if we register a rq-qos to the system, part of the blk-throttle hooks is in rq-qos and part hooks not, which feels a bit confusing. In addition, we may need to implement more hooks, such as IO merge hook.

Would it be possible to just move the blk-throtl throttling hook right next to the rq-qos hook so that it gets throttled after splitting?

Thanks.

Signature · 1 line
tejun
author
#7 lore raw

Thanks for your time.

Tejun Heo wrote on 2021/7/28 12:21 上午:

Quoted 10 lines

Hello,

Quoted 4 lines · brookxu

Make blk-throttle use rq-qos may be more elegant. But I found that there may be at least one problem that is difficult to solve. blk-throttle supports separate throttle for read and write IOs, which means that we cannot suspend tasks during throttle, but rq-qos throttle IOs by suspending tasks.

Ah, right, I forgot about that.

Quoted 7 lines
Quoted 4 lines

We may be able to relocate the blk-throttle hooks to the rq-qos hooks. Since we may not be able to replace the throttle hook, in this case, if we register a rq-qos to the system, part of the blk-throttle hooks is in rq-qos and part hooks not, which feels a bit confusing. In addition, we may need to implement more hooks, such as IO merge hook.

Would it be possible to just move the blk-throtl throttling hook right next to the rq-qos hook so that it gets throttled after splitting?

If we do this, I think we will encounter some problems, as follows:
1. blk-throttle is now at the top of the IO stack. Changing the position of the throttle hook will make this mechanism invalid for some devices.
2. We may also need to add a new hook to handle back-merge, otherwise I think the isolation effect will be worse in the sequential IO scene.
3. Since bio has entered the IO stack, if it is suspended and resubmitted by blk-throttle, then there is an IO stack reentry problem, which I think may cause many small problems.

Quoted 2 lines

Thanks.

maintainer
#8 lore raw

Hello,

Quoted 7 lines · brookxu

If we do this, I think we will encounter some problems, as follows:
1. blk-throttle is now at the top of the IO stack. Changing the position of the throttle hook will make this mechanism invalid for some devices.
2. We may also need to add a new hook to handle back-merge, otherwise I think the isolation effect will be worse in the sequential IO scene.
3. Since bio has entered the IO stack, if it is suspended and resubmitted by blk-throttle, then there is an IO stack reentry problem, which I think may cause many small problems.

Yeah, you're right. I don't think it's worth rocking the boat with blk-throtl at this point. An extra callback it is then.

Thanks.

Signature · 1 line
tejun