Skip to contentSkip to conversation

blk-iocost: remove the second superfluous current_hweight

PATCH In review Chengming Zhou 2 participants 3 messages Jun 9, 2022 – Jun 10, 2022

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 blk-iocost: remove the second superfluous current_hweight

The commit ac33e91e2dac ("blk-iocost: implement vtime loss compensation") add the second current_hweight() in the loop of active iocgs list to get old_hwi to calculate the vtime loss of the iocg.

Since the hwi can't change and the first current_hweight already get hwa and hwi, so this second superfluous current_hweight() can be removed. There should be no functional changes.

---
 block/blk-iocost.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
block/blk-iocost.c +5 -6
index 33a11ba971ea..3cda08224d51 100644
@@ -2238,7 +2238,7 @@ static void ioc_timer_fn(struct timer_list *timer)
 	/* calc usage and see whether some weights need to be moved around */
 	list_for_each_entry(iocg, &ioc->active_iocgs, active_list) {
 		u64 vdone, vtime, usage_us;
-		u32 hw_active, hw_inuse;
+		u32 hwa, old_hwi;
 
 		/*
 		 * Collect unused and wind vtime closer to vnow to prevent
@@ -2246,7 +2246,7 @@ static void ioc_timer_fn(struct timer_list *timer)
 		 */
 		vdone = atomic64_read(&iocg->done_vtime);
 		vtime = atomic64_read(&iocg->vtime);
-		current_hweight(iocg, &hw_active, &hw_inuse);
+		current_hweight(iocg, &hwa, &old_hwi);
 
 		/*
 		 * Latency QoS detection doesn't account for IOs which are
@@ -2271,15 +2271,15 @@ static void ioc_timer_fn(struct timer_list *timer)
 
 		/* see whether there's surplus vtime */
 		WARN_ON_ONCE(!list_empty(&iocg->surplus_list));
-		if (hw_inuse < hw_active ||
+		if (old_hwi < hwa ||
 		    (!waitqueue_active(&iocg->waitq) &&
 		     time_before64(vtime, now.vnow - ioc->margins.low))) {
-			u32 hwa, old_hwi, hwm, new_hwi, usage;
+			u32 hwm, new_hwi, usage;
 			u64 usage_dur;
 
 			if (vdone != vtime) {
 				u64 inflight_us = DIV64_U64_ROUND_UP(
-					cost_to_abs_cost(vtime - vdone, hw_inuse),
+					cost_to_abs_cost(vtime - vdone, old_hwi),
 					ioc->vtime_base_rate);
 
 				usage_us = max(usage_us, inflight_us);
@@ -2300,7 +2300,6 @@ static void ioc_timer_fn(struct timer_list *timer)
 			 * Already donating or accumulated enough to start.
 			 * Determine the donation amount.
 			 */
-			current_hweight(iocg, &hwa, &old_hwi);
 			hwm = current_hweight_max(iocg);
 			new_hwi = hweight_after_donation(iocg, old_hwi, hwm,
 							 usage, &now);
Signature · 1 line
2.36.1
#1 lore raw

PATCH 2/2 blk-iocost: fix vtime loss calculation when iocg deactivate

The commit ac33e91e2dac ("blk-iocost: implement vtime loss compensation") will accumulate vtime loss of iocgs on the period, to compensate the vtime loss we throw away, which is good for device utilization.

But the vtime loss calculation of iocg is wrong because of different hweight_gen when having some iocgs deactivated.

ioc_check_iocgs()
...

  } else if (iocg_is_idle(iocg)) {
    ioc->vtime_err -= div64_u64(excess * old_hwi,	--> old_hwi_1
                                WEIGHT_ONE);

}

commit_weights(ioc); --> hweight_gen increase

hweight_after_donation()
...
ioc->vtime_err -= div64_u64(excess * old_hwi, --> old_hwi_2

                              WEIGHT_ONE);

The old_hwi_2 of active iocgs is in fact not of the same hweight_gen as the old_hwi_1 of idle iocgs. After idle iocgs deactivate and hweight_gen increase, the old_hwi_2 become larger than it should be, which cause the calculated vtime_err more than it should be.

I found this problem by noticing some abnormal vtime loss compensation when having some cgroups with intermittent IO.

Since we already have recorded the usage_delta_us of each iocg, and usage_us_sum is the sum of them, so the vtime loss calculation of the period is straightforward and more accurate.

---
 block/blk-iocost.c | 21 ++++-----------------
 1 file changed, 4 insertions(+), 17 deletions(-)
block/blk-iocost.c +4 -17
index 3cda08224d51..6c55c69d4aad 100644
@@ -1730,7 +1730,6 @@ static u32 hweight_after_donation(struct ioc_gq *iocg, u32 old_hwi, u32 hwm,
 		atomic64_add(excess, &iocg->vtime);
 		atomic64_add(excess, &iocg->done_vtime);
 		vtime += excess;
-		ioc->vtime_err -= div64_u64(excess * old_hwi, WEIGHT_ONE);
 	}
 
 	/*
@@ -2168,22 +2167,6 @@ static int ioc_check_iocgs(struct ioc *ioc, struct ioc_now *now)
 		} else if (iocg_is_idle(iocg)) {
 			/* no waiter and idle, deactivate */
 			u64 vtime = atomic64_read(&iocg->vtime);
-			s64 excess;
-
-			/*
-			 * @iocg has been inactive for a full duration and will
-			 * have a high budget. Account anything above target as
-			 * error and throw away. On reactivation, it'll start
-			 * with the target budget.
-			 */
-			excess = now->vnow - vtime - ioc->margins.target;
-			if (excess > 0) {
-				u32 old_hwi;
-
-				current_hweight(iocg, NULL, &old_hwi);
-				ioc->vtime_err -= div64_u64(excess * old_hwi,
-							    WEIGHT_ONE);
-			}
 
 			TRACE_IOCG_PATH(iocg_idle, iocg, now,
 					atomic64_read(&iocg->active_period),
@@ -2348,6 +2331,10 @@ static void ioc_timer_fn(struct timer_list *timer)
 	list_for_each_entry_safe(iocg, tiocg, &surpluses, surplus_list)
 		list_del_init(&iocg->surplus_list);
 
+	/* calculate vtime loss in this period */
+	if (period_vtime > usage_us_sum * ioc->vtime_base_rate)
+		ioc->vtime_err -= period_vtime - usage_us_sum * ioc->vtime_base_rate;
+
 	/*
 	 * If q is getting clogged or we're missing too much, we're issuing
 	 * too much IO and should lower vtime rate.  If we're not missing
Signature · 1 line
2.36.1
Reviewed-by on v1 1/2
#2 lore raw
In reply to Chengming Zhou
Quoted 9 lines · Chengming Zhou

The commit ac33e91e2dac ("blk-iocost: implement vtime loss compensation") add the second current_hweight() in the loop of active iocgs list to get old_hwi to calculate the vtime loss of the iocg.

Since the hwi can't change and the first current_hweight already get hwa and hwi, so this second superfluous current_hweight() can be removed. There should be no functional changes.

FWIW. Looks ok.