Skip to content

Commit c5cc207

Browse files
committed
blk-throttle: add throtl_qnode for dispatch fairness
With flat hierarchy, there's only single level of dispatching happening and fairness beyond that point is the responsibility of the rest of the block layer and driver, which usually works out okay; however, with the planned hierarchy support, service_queue->bio_lists[] can be filled up by bios from a single source. While the limits would still be honored, it'd be very easy to starve IOs from siblings or children. To avoid such starvation, this patch implements throtl_qnode and converts service_queue->bio_lists[] to lists of per-source qnodes which in turn contains the bio's. For example, when a bio is dispatched from a child group, the bio doesn't get queued on ->bio_lists[] directly but it first gets queued on the group's qnode which in turn gets queued on service_queue->queued[]. When dispatching for the upper level, the ->queued[] list is consumed in round-robing order so that the dispatch windows is consumed fairly by all IO sources. There are two ways a bio can come to a throtl_grp - directly queued to the group or dispatched from a child. For the former throtl_grp->qnode_on_self[rw] is used. For the latter, the child's ->qnode_on_parent[rw]. Note that this means that the child which is contributing a bio to its parent should stay pinned until all its bios are dispatched to its grand-parent. This patch moves blkg refcnting from bio add/remove spots to qnode activation/deactivation so that the blkg containing an active qnode is always pinned. As child pins the parent, this is sufficient for keeping the relevant sub-tree pinned while bios are in flight. The starvation issue was spotted by Vivek Goyal. v2: The original patch used the same throtl_grp->qnode_on_self/parent for reads and writes causing RWs to be queued incorrectly if there already are outstanding IOs in the other direction. They should be throtl_grp->qnode_on_self/parent[2] so that READs and WRITEs can use different qnodes. Spotted by Vivek Goyal. Signed-off-by: Tejun Heo <tj@kernel.org> Acked-by: Vivek Goyal <vgoyal@redhat.com>
1 parent 2e48a53 commit c5cc207

1 file changed

Lines changed: 176 additions & 25 deletions

File tree

‎block/blk-throttle.c‎

Lines changed: 176 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,43 @@ static struct blkcg_policy blkcg_policy_throtl;
2626
/* A workqueue to queue throttle related work */
2727
static struct workqueue_struct *kthrotld_workqueue;
2828

29+
/*
30+
* To implement hierarchical throttling, throtl_grps form a tree and bios
31+
* are dispatched upwards level by level until they reach the top and get
32+
* issued. When dispatching bios from the children and local group at each
33+
* level, if the bios are dispatched into a single bio_list, there's a risk
34+
* of a local or child group which can queue many bios at once filling up
35+
* the list starving others.
36+
*
37+
* To avoid such starvation, dispatched bios are queued separately
38+
* according to where they came from. When they are again dispatched to
39+
* the parent, they're popped in round-robin order so that no single source
40+
* hogs the dispatch window.
41+
*
42+
* throtl_qnode is used to keep the queued bios separated by their sources.
43+
* Bios are queued to throtl_qnode which in turn is queued to
44+
* throtl_service_queue and then dispatched in round-robin order.
45+
*
46+
* It's also used to track the reference counts on blkg's. A qnode always
47+
* belongs to a throtl_grp and gets queued on itself or the parent, so
48+
* incrementing the reference of the associated throtl_grp when a qnode is
49+
* queued and decrementing when dequeued is enough to keep the whole blkg
50+
* tree pinned while bios are in flight.
51+
*/
52+
struct throtl_qnode {
53+
struct list_head node; /* service_queue->queued[] */
54+
struct bio_list bios; /* queued bios */
55+
struct throtl_grp *tg; /* tg this qnode belongs to */
56+
};
57+
2958
struct throtl_service_queue {
3059
struct throtl_service_queue *parent_sq; /* the parent service_queue */
3160

3261
/*
3362
* Bios queued directly to this service_queue or dispatched from
3463
* children throtl_grp's.
3564
*/
36-
struct bio_list bio_lists[2]; /* queued bios [READ/WRITE] */
65+
struct list_head queued[2]; /* throtl_qnode [READ/WRITE] */
3766
unsigned int nr_queued[2]; /* number of queued bios */
3867

3968
/*
@@ -75,6 +104,17 @@ struct throtl_grp {
75104
/* this group's service queue */
76105
struct throtl_service_queue service_queue;
77106

107+
/*
108+
* qnode_on_self is used when bios are directly queued to this
109+
* throtl_grp so that local bios compete fairly with bios
110+
* dispatched from children. qnode_on_parent is used when bios are
111+
* dispatched from this throtl_grp into its parent and will compete
112+
* with the sibling qnode_on_parents and the parent's
113+
* qnode_on_self.
114+
*/
115+
struct throtl_qnode qnode_on_self[2];
116+
struct throtl_qnode qnode_on_parent[2];
117+
78118
/*
79119
* Dispatch time in jiffies. This is the estimated time when group
80120
* will unthrottle and is ready to dispatch more bio. It is used as
@@ -250,12 +290,95 @@ static void tg_stats_alloc_fn(struct work_struct *work)
250290
goto alloc_stats;
251291
}
252292

293+
static void throtl_qnode_init(struct throtl_qnode *qn, struct throtl_grp *tg)
294+
{
295+
INIT_LIST_HEAD(&qn->node);
296+
bio_list_init(&qn->bios);
297+
qn->tg = tg;
298+
}
299+
300+
/**
301+
* throtl_qnode_add_bio - add a bio to a throtl_qnode and activate it
302+
* @bio: bio being added
303+
* @qn: qnode to add bio to
304+
* @queued: the service_queue->queued[] list @qn belongs to
305+
*
306+
* Add @bio to @qn and put @qn on @queued if it's not already on.
307+
* @qn->tg's reference count is bumped when @qn is activated. See the
308+
* comment on top of throtl_qnode definition for details.
309+
*/
310+
static void throtl_qnode_add_bio(struct bio *bio, struct throtl_qnode *qn,
311+
struct list_head *queued)
312+
{
313+
bio_list_add(&qn->bios, bio);
314+
if (list_empty(&qn->node)) {
315+
list_add_tail(&qn->node, queued);
316+
blkg_get(tg_to_blkg(qn->tg));
317+
}
318+
}
319+
320+
/**
321+
* throtl_peek_queued - peek the first bio on a qnode list
322+
* @queued: the qnode list to peek
323+
*/
324+
static struct bio *throtl_peek_queued(struct list_head *queued)
325+
{
326+
struct throtl_qnode *qn = list_first_entry(queued, struct throtl_qnode, node);
327+
struct bio *bio;
328+
329+
if (list_empty(queued))
330+
return NULL;
331+
332+
bio = bio_list_peek(&qn->bios);
333+
WARN_ON_ONCE(!bio);
334+
return bio;
335+
}
336+
337+
/**
338+
* throtl_pop_queued - pop the first bio form a qnode list
339+
* @queued: the qnode list to pop a bio from
340+
* @tg_to_put: optional out argument for throtl_grp to put
341+
*
342+
* Pop the first bio from the qnode list @queued. After popping, the first
343+
* qnode is removed from @queued if empty or moved to the end of @queued so
344+
* that the popping order is round-robin.
345+
*
346+
* When the first qnode is removed, its associated throtl_grp should be put
347+
* too. If @tg_to_put is NULL, this function automatically puts it;
348+
* otherwise, *@tg_to_put is set to the throtl_grp to put and the caller is
349+
* responsible for putting it.
350+
*/
351+
static struct bio *throtl_pop_queued(struct list_head *queued,
352+
struct throtl_grp **tg_to_put)
353+
{
354+
struct throtl_qnode *qn = list_first_entry(queued, struct throtl_qnode, node);
355+
struct bio *bio;
356+
357+
if (list_empty(queued))
358+
return NULL;
359+
360+
bio = bio_list_pop(&qn->bios);
361+
WARN_ON_ONCE(!bio);
362+
363+
if (bio_list_empty(&qn->bios)) {
364+
list_del_init(&qn->node);
365+
if (tg_to_put)
366+
*tg_to_put = qn->tg;
367+
else
368+
blkg_put(tg_to_blkg(qn->tg));
369+
} else {
370+
list_move_tail(&qn->node, queued);
371+
}
372+
373+
return bio;
374+
}
375+
253376
/* init a service_queue, assumes the caller zeroed it */
254377
static void throtl_service_queue_init(struct throtl_service_queue *sq,
255378
struct throtl_service_queue *parent_sq)
256379
{
257-
bio_list_init(&sq->bio_lists[0]);
258-
bio_list_init(&sq->bio_lists[1]);
380+
INIT_LIST_HEAD(&sq->queued[0]);
381+
INIT_LIST_HEAD(&sq->queued[1]);
259382
sq->pending_tree = RB_ROOT;
260383
sq->parent_sq = parent_sq;
261384
setup_timer(&sq->pending_timer, throtl_pending_timer_fn,
@@ -272,8 +395,14 @@ static void throtl_pd_init(struct blkcg_gq *blkg)
272395
struct throtl_grp *tg = blkg_to_tg(blkg);
273396
struct throtl_data *td = blkg->q->td;
274397
unsigned long flags;
398+
int rw;
275399

276400
throtl_service_queue_init(&tg->service_queue, &td->service_queue);
401+
for (rw = READ; rw <= WRITE; rw++) {
402+
throtl_qnode_init(&tg->qnode_on_self[rw], tg);
403+
throtl_qnode_init(&tg->qnode_on_parent[rw], tg);
404+
}
405+
277406
RB_CLEAR_NODE(&tg->rb_node);
278407
tg->td = td;
279408

@@ -715,7 +844,7 @@ static bool tg_may_dispatch(struct throtl_grp *tg, struct bio *bio,
715844
* queued.
716845
*/
717846
BUG_ON(tg->service_queue.nr_queued[rw] &&
718-
bio != bio_list_peek(&tg->service_queue.bio_lists[rw]));
847+
bio != throtl_peek_queued(&tg->service_queue.queued[rw]));
719848

720849
/* If tg->bps = -1, then BW is unlimited */
721850
if (tg->bps[rw] == -1 && tg->iops[rw] == -1) {
@@ -806,11 +935,24 @@ static void throtl_charge_bio(struct throtl_grp *tg, struct bio *bio)
806935
}
807936
}
808937

809-
static void throtl_add_bio_tg(struct bio *bio, struct throtl_grp *tg)
938+
/**
939+
* throtl_add_bio_tg - add a bio to the specified throtl_grp
940+
* @bio: bio to add
941+
* @qn: qnode to use
942+
* @tg: the target throtl_grp
943+
*
944+
* Add @bio to @tg's service_queue using @qn. If @qn is not specified,
945+
* tg->qnode_on_self[] is used.
946+
*/
947+
static void throtl_add_bio_tg(struct bio *bio, struct throtl_qnode *qn,
948+
struct throtl_grp *tg)
810949
{
811950
struct throtl_service_queue *sq = &tg->service_queue;
812951
bool rw = bio_data_dir(bio);
813952

953+
if (!qn)
954+
qn = &tg->qnode_on_self[rw];
955+
814956
/*
815957
* If @tg doesn't currently have any bios queued in the same
816958
* direction, queueing @bio can change when @tg should be
@@ -820,9 +962,8 @@ static void throtl_add_bio_tg(struct bio *bio, struct throtl_grp *tg)
820962
if (!sq->nr_queued[rw])
821963
tg->flags |= THROTL_TG_WAS_EMPTY;
822964

823-
bio_list_add(&sq->bio_lists[rw], bio);
824-
/* Take a bio reference on tg */
825-
blkg_get(tg_to_blkg(tg));
965+
throtl_qnode_add_bio(bio, qn, &sq->queued[rw]);
966+
826967
sq->nr_queued[rw]++;
827968
throtl_enqueue_tg(tg);
828969
}
@@ -833,10 +974,10 @@ static void tg_update_disptime(struct throtl_grp *tg)
833974
unsigned long read_wait = -1, write_wait = -1, min_wait = -1, disptime;
834975
struct bio *bio;
835976

836-
if ((bio = bio_list_peek(&sq->bio_lists[READ])))
977+
if ((bio = throtl_peek_queued(&sq->queued[READ])))
837978
tg_may_dispatch(tg, bio, &read_wait);
838979

839-
if ((bio = bio_list_peek(&sq->bio_lists[WRITE])))
980+
if ((bio = throtl_peek_queued(&sq->queued[WRITE])))
840981
tg_may_dispatch(tg, bio, &write_wait);
841982

842983
min_wait = min(read_wait, write_wait);
@@ -856,9 +997,16 @@ static void tg_dispatch_one_bio(struct throtl_grp *tg, bool rw)
856997
struct throtl_service_queue *sq = &tg->service_queue;
857998
struct throtl_service_queue *parent_sq = sq->parent_sq;
858999
struct throtl_grp *parent_tg = sq_to_tg(parent_sq);
1000+
struct throtl_grp *tg_to_put = NULL;
8591001
struct bio *bio;
8601002

861-
bio = bio_list_pop(&sq->bio_lists[rw]);
1003+
/*
1004+
* @bio is being transferred from @tg to @parent_sq. Popping a bio
1005+
* from @tg may put its reference and @parent_sq might end up
1006+
* getting released prematurely. Remember the tg to put and put it
1007+
* after @bio is transferred to @parent_sq.
1008+
*/
1009+
bio = throtl_pop_queued(&sq->queued[rw], &tg_to_put);
8621010
sq->nr_queued[rw]--;
8631011

8641012
throtl_charge_bio(tg, bio);
@@ -871,17 +1019,18 @@ static void tg_dispatch_one_bio(struct throtl_grp *tg, bool rw)
8711019
* responsible for issuing these bios.
8721020
*/
8731021
if (parent_tg) {
874-
throtl_add_bio_tg(bio, parent_tg);
1022+
throtl_add_bio_tg(bio, &tg->qnode_on_parent[rw], parent_tg);
8751023
} else {
876-
bio_list_add(&parent_sq->bio_lists[rw], bio);
1024+
throtl_qnode_add_bio(bio, &tg->qnode_on_parent[rw],
1025+
&parent_sq->queued[rw]);
8771026
BUG_ON(tg->td->nr_queued[rw] <= 0);
8781027
tg->td->nr_queued[rw]--;
8791028
}
8801029

8811030
throtl_trim_slice(tg, rw);
8821031

883-
/* @bio is transferred to parent, drop its blkg reference */
884-
blkg_put(tg_to_blkg(tg));
1032+
if (tg_to_put)
1033+
blkg_put(tg_to_blkg(tg_to_put));
8851034
}
8861035

8871036
static int throtl_dispatch_tg(struct throtl_grp *tg)
@@ -894,7 +1043,7 @@ static int throtl_dispatch_tg(struct throtl_grp *tg)
8941043

8951044
/* Try to dispatch 75% READS and 25% WRITES */
8961045

897-
while ((bio = bio_list_peek(&sq->bio_lists[READ])) &&
1046+
while ((bio = throtl_peek_queued(&sq->queued[READ])) &&
8981047
tg_may_dispatch(tg, bio, NULL)) {
8991048

9001049
tg_dispatch_one_bio(tg, bio_data_dir(bio));
@@ -904,7 +1053,7 @@ static int throtl_dispatch_tg(struct throtl_grp *tg)
9041053
break;
9051054
}
9061055

907-
while ((bio = bio_list_peek(&sq->bio_lists[WRITE])) &&
1056+
while ((bio = throtl_peek_queued(&sq->queued[WRITE])) &&
9081057
tg_may_dispatch(tg, bio, NULL)) {
9091058

9101059
tg_dispatch_one_bio(tg, bio_data_dir(bio));
@@ -1039,10 +1188,9 @@ void blk_throtl_dispatch_work_fn(struct work_struct *work)
10391188
bio_list_init(&bio_list_on_stack);
10401189

10411190
spin_lock_irq(q->queue_lock);
1042-
for (rw = READ; rw <= WRITE; rw++) {
1043-
bio_list_merge(&bio_list_on_stack, &td_sq->bio_lists[rw]);
1044-
bio_list_init(&td_sq->bio_lists[rw]);
1045-
}
1191+
for (rw = READ; rw <= WRITE; rw++)
1192+
while ((bio = throtl_pop_queued(&td_sq->queued[rw], NULL)))
1193+
bio_list_add(&bio_list_on_stack, bio);
10461194
spin_unlock_irq(q->queue_lock);
10471195

10481196
if (!bio_list_empty(&bio_list_on_stack)) {
@@ -1241,6 +1389,7 @@ static struct blkcg_policy blkcg_policy_throtl = {
12411389
bool blk_throtl_bio(struct request_queue *q, struct bio *bio)
12421390
{
12431391
struct throtl_data *td = q->td;
1392+
struct throtl_qnode *qn = NULL;
12441393
struct throtl_grp *tg;
12451394
struct throtl_service_queue *sq;
12461395
bool rw = bio_data_dir(bio);
@@ -1308,6 +1457,7 @@ bool blk_throtl_bio(struct request_queue *q, struct bio *bio)
13081457
* Climb up the ladder. If we''re already at the top, it
13091458
* can be executed directly.
13101459
*/
1460+
qn = &tg->qnode_on_parent[rw];
13111461
sq = sq->parent_sq;
13121462
tg = sq_to_tg(sq);
13131463
if (!tg)
@@ -1323,7 +1473,7 @@ bool blk_throtl_bio(struct request_queue *q, struct bio *bio)
13231473

13241474
bio_associate_current(bio);
13251475
tg->td->nr_queued[rw]++;
1326-
throtl_add_bio_tg(bio, tg);
1476+
throtl_add_bio_tg(bio, qn, tg);
13271477
throttled = true;
13281478

13291479
/*
@@ -1367,9 +1517,9 @@ static void tg_drain_bios(struct throtl_service_queue *parent_sq)
13671517

13681518
throtl_dequeue_tg(tg);
13691519

1370-
while ((bio = bio_list_peek(&sq->bio_lists[READ])))
1520+
while ((bio = throtl_peek_queued(&sq->queued[READ])))
13711521
tg_dispatch_one_bio(tg, bio_data_dir(bio));
1372-
while ((bio = bio_list_peek(&sq->bio_lists[WRITE])))
1522+
while ((bio = throtl_peek_queued(&sq->queued[WRITE])))
13731523
tg_dispatch_one_bio(tg, bio_data_dir(bio));
13741524
}
13751525
}
@@ -1411,7 +1561,8 @@ void blk_throtl_drain(struct request_queue *q)
14111561

14121562
/* all bios now should be in td->service_queue, issue them */
14131563
for (rw = READ; rw <= WRITE; rw++)
1414-
while ((bio = bio_list_pop(&td->service_queue.bio_lists[rw])))
1564+
while ((bio = throtl_pop_queued(&td->service_queue.queued[rw],
1565+
NULL)))
14151566
generic_make_request(bio);
14161567

14171568
spin_lock_irq(q->queue_lock);

0 commit comments

Comments
 (0)