2024-05-13 12:19:39

by Yu Kuai

[permalink] [raw]
Subject: [PATCH -next 0/2] blk-throttle: fix lower control under super low iops limit

From: Yu Kuai <[email protected]>

Yu Kuai (2):
blk-throttle: factor out a helper to get throtl_slice from tg
blk-throttle: fix lower control under super low iops limit

block/blk-throttle.c | 52 ++++++++++++++++++++++++++++++++------------
block/blk-throttle.h | 6 +++++
2 files changed, 44 insertions(+), 14 deletions(-)

--
2.39.2



2024-05-13 12:19:41

by Yu Kuai

[permalink] [raw]
Subject: [PATCH -next 2/2] blk-throttle: fix lower control under super low iops limit

From: Yu Kuai <[email protected]>

User will configure allowed iops limit in 1s, and calculate_io_allowed()
will calculate allowed iops in the slice by:

limit * HZ / throtl_slice

However, if limit is quite low, the result can be 0, then
allowed IO in the slice is 0, this will cause missing dispatch and
control will be lower than limit.

For example, set iops_limit to 5 with HD disk, and test will found that
iops will be 3.

This is usually not a big deal, because user will unlikely to configure
such low iops limit, however, this is still a problem in the extreme
scene.

Fix the problem by using MAX_THROTL_SLICE in this case, so that
calculate_io_allowed() is guaranteed not to return 0, since we don't care
about more smoother control effect in this case.

Signed-off-by: Yu Kuai <[email protected]>
---
block/blk-throttle.c | 18 ++++++++++++++++++
block/blk-throttle.h | 6 ++++++
2 files changed, 24 insertions(+)

diff --git a/block/blk-throttle.c b/block/blk-throttle.c
index 69f1bb91ea78..69429288b50b 100644
--- a/block/blk-throttle.c
+++ b/block/blk-throttle.c
@@ -491,6 +491,8 @@ static bool throtl_schedule_next_dispatch(struct throtl_service_queue *sq,

static unsigned int tg_throtl_slice(struct throtl_grp *tg, int rw)
{
+ if (tg->throtl_slice[rw])
+ return tg->throtl_slice[rw];
return tg->td->throtl_slice;
}

@@ -1169,6 +1171,21 @@ static int tg_print_conf_uint(struct seq_file *sf, void *v)
return 0;
}

+static void tg_set_throtl_slice(struct throtl_grp *tg)
+{
+ int rw;
+
+ for (rw = READ; rw <= WRITE; rw++) {
+ u32 limit = tg_iops_limit(tg, rw);
+
+ if (limit == UINT_MAX ||
+ calculate_io_allowed(limit, tg->td->throtl_slice) != 0)
+ tg->throtl_slice[rw] = tg->td->throtl_slice;
+ else
+ tg->throtl_slice[rw] = MAX_THROTL_SLICE;
+ }
+}
+
static void tg_conf_updated(struct throtl_grp *tg, bool global)
{
struct throtl_service_queue *sq = &tg->service_queue;
@@ -1200,6 +1217,7 @@ static void tg_conf_updated(struct throtl_grp *tg, bool global)
}
rcu_read_unlock();

+ tg_set_throtl_slice(tg);
/*
* We're already holding queue_lock and know @tg is valid. Let's
* apply the new config directly.
diff --git a/block/blk-throttle.h b/block/blk-throttle.h
index 393c3d134b96..0424d2be90ff 100644
--- a/block/blk-throttle.h
+++ b/block/blk-throttle.h
@@ -126,6 +126,12 @@ struct throtl_grp {

unsigned long last_check_time;

+ /*
+ * This is usually td->throtl_slice, however, if iops limit is quite
+ * low that allowed io in that slice is 0, throtl_slice in this tg will
+ * be set to MAX_THROTL_SLICE.
+ */
+ unsigned int throtl_slice[2];
/* When did we start a new slice */
unsigned long slice_start[2];
unsigned long slice_end[2];
--
2.39.2


2024-05-13 12:19:43

by Yu Kuai

[permalink] [raw]
Subject: [PATCH -next 1/2] blk-throttle: factor out a helper to get throtl_slice from tg

From: Yu Kuai <[email protected]>

There are no functional changes, prepare to use bigger slice in the case
that iops limit is quite low in the next patch.

Signed-off-by: Yu Kuai <[email protected]>
---
block/blk-throttle.c | 34 ++++++++++++++++++++--------------
1 file changed, 20 insertions(+), 14 deletions(-)

diff --git a/block/blk-throttle.c b/block/blk-throttle.c
index 80aaca18bfb0..69f1bb91ea78 100644
--- a/block/blk-throttle.c
+++ b/block/blk-throttle.c
@@ -489,6 +489,11 @@ static bool throtl_schedule_next_dispatch(struct throtl_service_queue *sq,
return false;
}

+static unsigned int tg_throtl_slice(struct throtl_grp *tg, int rw)
+{
+ return tg->td->throtl_slice;
+}
+
static inline void throtl_start_new_slice_with_credit(struct throtl_grp *tg,
bool rw, unsigned long start)
{
@@ -506,7 +511,7 @@ static inline void throtl_start_new_slice_with_credit(struct throtl_grp *tg,
if (time_after(start, tg->slice_start[rw]))
tg->slice_start[rw] = start;

- tg->slice_end[rw] = jiffies + tg->td->throtl_slice;
+ tg->slice_end[rw] = jiffies + tg_throtl_slice(tg, rw);
throtl_log(&tg->service_queue,
"[%c] new slice with credit start=%lu end=%lu jiffies=%lu",
rw == READ ? 'R' : 'W', tg->slice_start[rw],
@@ -519,7 +524,7 @@ static inline void throtl_start_new_slice(struct throtl_grp *tg, bool rw,
tg->bytes_disp[rw] = 0;
tg->io_disp[rw] = 0;
tg->slice_start[rw] = jiffies;
- tg->slice_end[rw] = jiffies + tg->td->throtl_slice;
+ tg->slice_end[rw] = jiffies + tg_throtl_slice(tg, rw);
if (clear_carryover) {
tg->carryover_bytes[rw] = 0;
tg->carryover_ios[rw] = 0;
@@ -534,7 +539,7 @@ static inline void throtl_start_new_slice(struct throtl_grp *tg, bool rw,
static inline void throtl_set_slice_end(struct throtl_grp *tg, bool rw,
unsigned long jiffy_end)
{
- tg->slice_end[rw] = roundup(jiffy_end, tg->td->throtl_slice);
+ tg->slice_end[rw] = roundup(jiffy_end, tg_throtl_slice(tg, rw));
}

static inline void throtl_extend_slice(struct throtl_grp *tg, bool rw,
@@ -594,6 +599,7 @@ static u64 calculate_bytes_allowed(u64 bps_limit, unsigned long jiffy_elapsed)
/* Trim the used slices and adjust slice start accordingly */
static inline void throtl_trim_slice(struct throtl_grp *tg, bool rw)
{
+ unsigned int slice;
unsigned long time_elapsed;
long long bytes_trim;
int io_trim;
@@ -616,10 +622,10 @@ static inline void throtl_trim_slice(struct throtl_grp *tg, bool rw)
* is bad because it does not allow new slice to start.
*/

- throtl_set_slice_end(tg, rw, jiffies + tg->td->throtl_slice);
+ slice = tg_throtl_slice(tg, rw);
+ throtl_set_slice_end(tg, rw, jiffies + slice);

- time_elapsed = rounddown(jiffies - tg->slice_start[rw],
- tg->td->throtl_slice);
+ time_elapsed = rounddown(jiffies - tg->slice_start[rw], slice);
if (!time_elapsed)
return;

@@ -647,7 +653,7 @@ static inline void throtl_trim_slice(struct throtl_grp *tg, bool rw)

throtl_log(&tg->service_queue,
"[%c] trim slice nr=%lu bytes=%lld io=%d start=%lu end=%lu jiffies=%lu",
- rw == READ ? 'R' : 'W', time_elapsed / tg->td->throtl_slice,
+ rw == READ ? 'R' : 'W', time_elapsed / slice,
bytes_trim, io_trim, tg->slice_start[rw], tg->slice_end[rw],
jiffies);
}
@@ -701,7 +707,7 @@ static unsigned long tg_within_iops_limit(struct throtl_grp *tg, struct bio *bio
jiffy_elapsed = jiffies - tg->slice_start[rw];

/* Round up to the next throttle slice, wait time must be nonzero */
- jiffy_elapsed_rnd = roundup(jiffy_elapsed + 1, tg->td->throtl_slice);
+ jiffy_elapsed_rnd = roundup(jiffy_elapsed + 1, tg_throtl_slice(tg, rw));
io_allowed = calculate_io_allowed(iops_limit, jiffy_elapsed_rnd) +
tg->carryover_ios[rw];
if (io_allowed > 0 && tg->io_disp[rw] + 1 <= io_allowed)
@@ -730,9 +736,9 @@ static unsigned long tg_within_bps_limit(struct throtl_grp *tg, struct bio *bio,

/* Slice has just started. Consider one slice interval */
if (!jiffy_elapsed)
- jiffy_elapsed_rnd = tg->td->throtl_slice;
+ jiffy_elapsed_rnd = tg_throtl_slice(tg, rw);

- jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, tg->td->throtl_slice);
+ jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, tg_throtl_slice(tg, rw));
bytes_allowed = calculate_bytes_allowed(bps_limit, jiffy_elapsed_rnd) +
tg->carryover_bytes[rw];
if (bytes_allowed > 0 && tg->bytes_disp[rw] + bio_size <= bytes_allowed)
@@ -792,10 +798,10 @@ static bool tg_may_dispatch(struct throtl_grp *tg, struct bio *bio,
if (throtl_slice_used(tg, rw) && !(tg->service_queue.nr_queued[rw]))
throtl_start_new_slice(tg, rw, true);
else {
- if (time_before(tg->slice_end[rw],
- jiffies + tg->td->throtl_slice))
- throtl_extend_slice(tg, rw,
- jiffies + tg->td->throtl_slice);
+ unsigned int slice = tg_throtl_slice(tg, rw);
+
+ if (time_before(tg->slice_end[rw], jiffies + slice))
+ throtl_extend_slice(tg, rw, jiffies + slice);
}

bps_wait = tg_within_bps_limit(tg, bio, bps_limit);
--
2.39.2


2024-06-13 02:27:00

by Yu Kuai

[permalink] [raw]
Subject: Re: [PATCH -next 0/2] blk-throttle: fix lower control under super low iops limit

Friendly ping ...

?? 2024/05/13 20:08, Yu Kuai д??:
> From: Yu Kuai <[email protected]>
>
> Yu Kuai (2):
> blk-throttle: factor out a helper to get throtl_slice from tg
> blk-throttle: fix lower control under super low iops limit
>
> block/blk-throttle.c | 52 ++++++++++++++++++++++++++++++++------------
> block/blk-throttle.h | 6 +++++
> 2 files changed, 44 insertions(+), 14 deletions(-)
>


2024-06-13 22:30:34

by Tejun Heo

[permalink] [raw]
Subject: Re: [PATCH -next 1/2] blk-throttle: factor out a helper to get throtl_slice from tg

Sorry about the delay.

On Mon, May 13, 2024 at 08:08:47PM +0800, Yu Kuai wrote:
> From: Yu Kuai <[email protected]>
>
> There are no functional changes, prepare to use bigger slice in the case
> that iops limit is quite low in the next patch.
>
> Signed-off-by: Yu Kuai <[email protected]>

Acked-by: Tejun Heo <[email protected]>

Thanks.

--
tejun

2024-06-13 22:32:19

by Tejun Heo

[permalink] [raw]
Subject: Re: [PATCH -next 2/2] blk-throttle: fix lower control under super low iops limit

Hello,

On Mon, May 13, 2024 at 08:08:48PM +0800, Yu Kuai wrote:
...
> However, if limit is quite low, the result can be 0, then
> allowed IO in the slice is 0, this will cause missing dispatch and
> control will be lower than limit.
>
> For example, set iops_limit to 5 with HD disk, and test will found that
> iops will be 3.

Hmm... can't this be solved by starting the next slice with the right
credit?

> static unsigned int tg_throtl_slice(struct throtl_grp *tg, int rw)
> {
> + if (tg->throtl_slice[rw])
> + return tg->throtl_slice[rw];
> return tg->td->throtl_slice;

Because this is a bit nasty. If we want to use difference throttling slices
for different cgroups, we might as well do it universally.

Thanks.

--
tejun

2024-06-14 01:09:34

by Yu Kuai

[permalink] [raw]
Subject: Re: [PATCH -next 2/2] blk-throttle: fix lower control under super low iops limit

Hi,

?? 2024/06/14 6:32, Tejun Heo д??:
> Hello,
>
> On Mon, May 13, 2024 at 08:08:48PM +0800, Yu Kuai wrote:
> ...
>> However, if limit is quite low, the result can be 0, then
>> allowed IO in the slice is 0, this will cause missing dispatch and
>> control will be lower than limit.
>>
>> For example, set iops_limit to 5 with HD disk, and test will found that
>> iops will be 3.
>
> Hmm... can't this be solved by starting the next slice with the right
> credit?

Of course, this looks like feasible. I'll look into this.
>
>> static unsigned int tg_throtl_slice(struct throtl_grp *tg, int rw)
>> {
>> + if (tg->throtl_slice[rw])
>> + return tg->throtl_slice[rw];
>> return tg->td->throtl_slice;
>
> Because this is a bit nasty. If we want to use difference throttling slices
> for different cgroups, we might as well do it universally.

I do thought about this, however, because different cgroups have
different slice start, I convinced myself this is fine to have different
slice as well.

Thanks,
Kuai

>
> Thanks.
>