[RFC PATCH 3/8] bfq: keep the minimun bandwidth for be_class

From: brookxu
Date: Mon Mar 08 2021 - 07:01:21 EST


From: Chunguang Xu <brookxu@xxxxxxxxxxx>

From: Chunguang Xu <brookxu@xxxxxxxxxxx>

rt_class will preempt other classes, which may cause other
classes to starve to death. At present, idle_class has
alleviated the starvation problem through the minimum
bandwidth mechanism. Similarly, we should do the same for
be_class.

Signed-off-by: Chunguang Xu <brookxu@xxxxxxxxxxx>
---
block/bfq-iosched.c | 2 +-
block/bfq-iosched.h | 9 +++++----
block/bfq-wf2q.c | 46 ++++++++++++++++++++++++++++-----------------
3 files changed, 35 insertions(+), 22 deletions(-)

diff --git a/block/bfq-iosched.c b/block/bfq-iosched.c
index ea9d7f6f4e3d..b639cdbb4192 100644
--- a/block/bfq-iosched.c
+++ b/block/bfq-iosched.c
@@ -6539,7 +6539,7 @@ static void bfq_init_root_group(struct bfq_group *root_group,
root_group->rq_pos_tree = RB_ROOT;
for (i = 0; i < BFQ_IOPRIO_CLASSES; i++)
root_group->sched_data.service_tree[i] = BFQ_SERVICE_TREE_INIT;
- root_group->sched_data.bfq_class_idle_last_service = jiffies;
+ root_group->sched_data.class_timeout_last_check = jiffies;
}

static int bfq_init_queue(struct request_queue *q, struct elevator_type *e)
diff --git a/block/bfq-iosched.h b/block/bfq-iosched.h
index a6f98e9e14b5..2fe7456aa7bc 100644
--- a/block/bfq-iosched.h
+++ b/block/bfq-iosched.h
@@ -13,7 +13,7 @@
#include "blk-cgroup-rwstat.h"

#define BFQ_IOPRIO_CLASSES 3
-#define BFQ_CL_IDLE_TIMEOUT (HZ/5)
+#define BFQ_CLASS_TIMEOUT (HZ/5)

#define BFQ_MIN_WEIGHT 1
#define BFQ_MAX_WEIGHT 1000
@@ -97,9 +97,10 @@ struct bfq_sched_data {
struct bfq_entity *next_in_service;
/* array of service trees, one per ioprio_class */
struct bfq_service_tree service_tree[BFQ_IOPRIO_CLASSES];
- /* last time CLASS_IDLE was served */
- unsigned long bfq_class_idle_last_service;
-
+ /* last time the class timeout was checked */
+ unsigned long class_timeout_last_check;
+ /* the position to start class timeout check next time */
+ unsigned int next_class_index;
};

/**
diff --git a/block/bfq-wf2q.c b/block/bfq-wf2q.c
index 5ff0028920a2..0d10eb3ed555 100644
--- a/block/bfq-wf2q.c
+++ b/block/bfq-wf2q.c
@@ -1435,6 +1435,34 @@ __bfq_lookup_next_entity(struct bfq_service_tree *st, bool in_service)
return entity;
}

+static int bfq_select_next_class(struct bfq_sched_data *sd)
+{
+ struct bfq_service_tree *st = sd->service_tree;
+ int i, class_idx, next_class = 0;
+ unsigned long last_check;
+
+ /*
+ * we needed to guarantee a minimum bandwidth for each class (if
+ * there is some active entity in this class). This should also
+ * mitigate priority-inversion problems in case a low priority
+ * task is holding file system resources.
+ */
+ for (i = 0; i < BFQ_IOPRIO_CLASSES; i++) {
+ class_idx = (sd->next_class_index + i) % BFQ_IOPRIO_CLASSES;
+ last_check = sd->class_timeout_last_check;
+ if (time_is_before_jiffies(last_check + BFQ_CLASS_TIMEOUT)) {
+ sd->class_timeout_last_check = jiffies;
+ if (!RB_EMPTY_ROOT(&(st + class_idx)->active)) {
+ next_class = class_idx++;
+ class_idx %= BFQ_IOPRIO_CLASSES;
+ sd->next_class_index = class_idx;
+ break;
+ }
+ }
+ }
+ return next_class;
+}
+
/**
* bfq_lookup_next_entity - return the first eligible entity in @sd.
* @sd: the sched_data.
@@ -1448,24 +1476,8 @@ static struct bfq_entity *bfq_lookup_next_entity(struct bfq_sched_data *sd,
bool expiration)
{
struct bfq_service_tree *st = sd->service_tree;
- struct bfq_service_tree *idle_class_st = st + (BFQ_IOPRIO_CLASSES - 1);
struct bfq_entity *entity = NULL;
- int class_idx = 0;
-
- /*
- * Choose from idle class, if needed to guarantee a minimum
- * bandwidth to this class (and if there is some active entity
- * in idle class). This should also mitigate
- * priority-inversion problems in case a low priority task is
- * holding file system resources.
- */
- if (time_is_before_jiffies(sd->bfq_class_idle_last_service +
- BFQ_CL_IDLE_TIMEOUT)) {
- if (!RB_EMPTY_ROOT(&idle_class_st->active))
- class_idx = BFQ_IOPRIO_CLASSES - 1;
- /* About to be served if backlogged, or not yet backlogged */
- sd->bfq_class_idle_last_service = jiffies;
- }
+ int class_idx = bfq_select_next_class(sd);

/*
* Find the next entity to serve for the highest-priority
--
2.30.0