]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - block/blk-throttle.c
blk-throttle: add upgrade logic for LIMIT_LOW state
[mirror_ubuntu-bionic-kernel.git] / block / blk-throttle.c
CommitLineData
e43473b7
VG
1/*
2 * Interface for controlling IO bandwidth on a request queue
3 *
4 * Copyright (C) 2010 Vivek Goyal <vgoyal@redhat.com>
5 */
6
7#include <linux/module.h>
8#include <linux/slab.h>
9#include <linux/blkdev.h>
10#include <linux/bio.h>
11#include <linux/blktrace_api.h>
eea8f41c 12#include <linux/blk-cgroup.h>
bc9fcbf9 13#include "blk.h"
e43473b7
VG
14
15/* Max dispatch from a group in 1 round */
16static int throtl_grp_quantum = 8;
17
18/* Total max dispatch from all groups in one round */
19static int throtl_quantum = 32;
20
21/* Throttling is performed over 100ms slice and after that slice is renewed */
22static unsigned long throtl_slice = HZ/10; /* 100 ms */
23
3c798398 24static struct blkcg_policy blkcg_policy_throtl;
0381411e 25
450adcbe
VG
26/* A workqueue to queue throttle related work */
27static struct workqueue_struct *kthrotld_workqueue;
450adcbe 28
c5cc2070
TH
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 */
52struct 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
c9e0332e 58struct throtl_service_queue {
77216b04
TH
59 struct throtl_service_queue *parent_sq; /* the parent service_queue */
60
73f0d49a
TH
61 /*
62 * Bios queued directly to this service_queue or dispatched from
63 * children throtl_grp's.
64 */
c5cc2070 65 struct list_head queued[2]; /* throtl_qnode [READ/WRITE] */
73f0d49a
TH
66 unsigned int nr_queued[2]; /* number of queued bios */
67
68 /*
69 * RB tree of active children throtl_grp's, which are sorted by
70 * their ->disptime.
71 */
c9e0332e
TH
72 struct rb_root pending_tree; /* RB tree of active tgs */
73 struct rb_node *first_pending; /* first node in the tree */
74 unsigned int nr_pending; /* # queued in the tree */
75 unsigned long first_pending_disptime; /* disptime of the first tg */
69df0ab0 76 struct timer_list pending_timer; /* fires on first_pending_disptime */
e43473b7
VG
77};
78
5b2c16aa
TH
79enum tg_state_flags {
80 THROTL_TG_PENDING = 1 << 0, /* on parent's pending tree */
0e9f4164 81 THROTL_TG_WAS_EMPTY = 1 << 1, /* bio_lists[] became non-empty */
5b2c16aa
TH
82};
83
e43473b7
VG
84#define rb_entry_tg(node) rb_entry((node), struct throtl_grp, rb_node)
85
9f626e37 86enum {
cd5ab1b0 87 LIMIT_LOW,
9f626e37
SL
88 LIMIT_MAX,
89 LIMIT_CNT,
90};
91
e43473b7 92struct throtl_grp {
f95a04af
TH
93 /* must be the first member */
94 struct blkg_policy_data pd;
95
c9e0332e 96 /* active throtl group service_queue member */
e43473b7
VG
97 struct rb_node rb_node;
98
0f3457f6
TH
99 /* throtl_data this group belongs to */
100 struct throtl_data *td;
101
49a2f1e3
TH
102 /* this group's service queue */
103 struct throtl_service_queue service_queue;
104
c5cc2070
TH
105 /*
106 * qnode_on_self is used when bios are directly queued to this
107 * throtl_grp so that local bios compete fairly with bios
108 * dispatched from children. qnode_on_parent is used when bios are
109 * dispatched from this throtl_grp into its parent and will compete
110 * with the sibling qnode_on_parents and the parent's
111 * qnode_on_self.
112 */
113 struct throtl_qnode qnode_on_self[2];
114 struct throtl_qnode qnode_on_parent[2];
115
e43473b7
VG
116 /*
117 * Dispatch time in jiffies. This is the estimated time when group
118 * will unthrottle and is ready to dispatch more bio. It is used as
119 * key to sort active groups in service tree.
120 */
121 unsigned long disptime;
122
e43473b7
VG
123 unsigned int flags;
124
693e751e
TH
125 /* are there any throtl rules between this group and td? */
126 bool has_rules[2];
127
cd5ab1b0 128 /* internally used bytes per second rate limits */
9f626e37 129 uint64_t bps[2][LIMIT_CNT];
cd5ab1b0
SL
130 /* user configured bps limits */
131 uint64_t bps_conf[2][LIMIT_CNT];
e43473b7 132
cd5ab1b0 133 /* internally used IOPS limits */
9f626e37 134 unsigned int iops[2][LIMIT_CNT];
cd5ab1b0
SL
135 /* user configured IOPS limits */
136 unsigned int iops_conf[2][LIMIT_CNT];
8e89d13f 137
e43473b7
VG
138 /* Number of bytes disptached in current slice */
139 uint64_t bytes_disp[2];
8e89d13f
VG
140 /* Number of bio's dispatched in current slice */
141 unsigned int io_disp[2];
e43473b7
VG
142
143 /* When did we start a new slice */
144 unsigned long slice_start[2];
145 unsigned long slice_end[2];
146};
147
148struct throtl_data
149{
e43473b7 150 /* service tree for active throtl groups */
c9e0332e 151 struct throtl_service_queue service_queue;
e43473b7 152
e43473b7
VG
153 struct request_queue *queue;
154
155 /* Total Number of queued bios on READ and WRITE lists */
156 unsigned int nr_queued[2];
157
e43473b7 158 /* Work for dispatching throttled bios */
69df0ab0 159 struct work_struct dispatch_work;
9f626e37
SL
160 unsigned int limit_index;
161 bool limit_valid[LIMIT_CNT];
e43473b7
VG
162};
163
69df0ab0
TH
164static void throtl_pending_timer_fn(unsigned long arg);
165
f95a04af
TH
166static inline struct throtl_grp *pd_to_tg(struct blkg_policy_data *pd)
167{
168 return pd ? container_of(pd, struct throtl_grp, pd) : NULL;
169}
170
3c798398 171static inline struct throtl_grp *blkg_to_tg(struct blkcg_gq *blkg)
0381411e 172{
f95a04af 173 return pd_to_tg(blkg_to_pd(blkg, &blkcg_policy_throtl));
0381411e
TH
174}
175
3c798398 176static inline struct blkcg_gq *tg_to_blkg(struct throtl_grp *tg)
0381411e 177{
f95a04af 178 return pd_to_blkg(&tg->pd);
0381411e
TH
179}
180
fda6f272
TH
181/**
182 * sq_to_tg - return the throl_grp the specified service queue belongs to
183 * @sq: the throtl_service_queue of interest
184 *
185 * Return the throtl_grp @sq belongs to. If @sq is the top-level one
186 * embedded in throtl_data, %NULL is returned.
187 */
188static struct throtl_grp *sq_to_tg(struct throtl_service_queue *sq)
189{
190 if (sq && sq->parent_sq)
191 return container_of(sq, struct throtl_grp, service_queue);
192 else
193 return NULL;
194}
195
196/**
197 * sq_to_td - return throtl_data the specified service queue belongs to
198 * @sq: the throtl_service_queue of interest
199 *
b43daedc 200 * A service_queue can be embedded in either a throtl_grp or throtl_data.
fda6f272
TH
201 * Determine the associated throtl_data accordingly and return it.
202 */
203static struct throtl_data *sq_to_td(struct throtl_service_queue *sq)
204{
205 struct throtl_grp *tg = sq_to_tg(sq);
206
207 if (tg)
208 return tg->td;
209 else
210 return container_of(sq, struct throtl_data, service_queue);
211}
212
9f626e37
SL
213static uint64_t tg_bps_limit(struct throtl_grp *tg, int rw)
214{
b22c417c
SL
215 struct blkcg_gq *blkg = tg_to_blkg(tg);
216 uint64_t ret;
217
218 if (cgroup_subsys_on_dfl(io_cgrp_subsys) && !blkg->parent)
219 return U64_MAX;
220 ret = tg->bps[rw][tg->td->limit_index];
221 if (ret == 0 && tg->td->limit_index == LIMIT_LOW)
222 return tg->bps[rw][LIMIT_MAX];
223 return ret;
9f626e37
SL
224}
225
226static unsigned int tg_iops_limit(struct throtl_grp *tg, int rw)
227{
b22c417c
SL
228 struct blkcg_gq *blkg = tg_to_blkg(tg);
229 unsigned int ret;
230
231 if (cgroup_subsys_on_dfl(io_cgrp_subsys) && !blkg->parent)
232 return UINT_MAX;
233 ret = tg->iops[rw][tg->td->limit_index];
234 if (ret == 0 && tg->td->limit_index == LIMIT_LOW)
235 return tg->iops[rw][LIMIT_MAX];
236 return ret;
9f626e37
SL
237}
238
fda6f272
TH
239/**
240 * throtl_log - log debug message via blktrace
241 * @sq: the service_queue being reported
242 * @fmt: printf format string
243 * @args: printf args
244 *
245 * The messages are prefixed with "throtl BLKG_NAME" if @sq belongs to a
246 * throtl_grp; otherwise, just "throtl".
fda6f272
TH
247 */
248#define throtl_log(sq, fmt, args...) do { \
249 struct throtl_grp *__tg = sq_to_tg((sq)); \
250 struct throtl_data *__td = sq_to_td((sq)); \
251 \
252 (void)__td; \
59fa0224
SL
253 if (likely(!blk_trace_note_message_enabled(__td->queue))) \
254 break; \
fda6f272
TH
255 if ((__tg)) { \
256 char __pbuf[128]; \
54e7ed12 257 \
fda6f272
TH
258 blkg_path(tg_to_blkg(__tg), __pbuf, sizeof(__pbuf)); \
259 blk_add_trace_msg(__td->queue, "throtl %s " fmt, __pbuf, ##args); \
260 } else { \
261 blk_add_trace_msg(__td->queue, "throtl " fmt, ##args); \
262 } \
54e7ed12 263} while (0)
e43473b7 264
c5cc2070
TH
265static void throtl_qnode_init(struct throtl_qnode *qn, struct throtl_grp *tg)
266{
267 INIT_LIST_HEAD(&qn->node);
268 bio_list_init(&qn->bios);
269 qn->tg = tg;
270}
271
272/**
273 * throtl_qnode_add_bio - add a bio to a throtl_qnode and activate it
274 * @bio: bio being added
275 * @qn: qnode to add bio to
276 * @queued: the service_queue->queued[] list @qn belongs to
277 *
278 * Add @bio to @qn and put @qn on @queued if it's not already on.
279 * @qn->tg's reference count is bumped when @qn is activated. See the
280 * comment on top of throtl_qnode definition for details.
281 */
282static void throtl_qnode_add_bio(struct bio *bio, struct throtl_qnode *qn,
283 struct list_head *queued)
284{
285 bio_list_add(&qn->bios, bio);
286 if (list_empty(&qn->node)) {
287 list_add_tail(&qn->node, queued);
288 blkg_get(tg_to_blkg(qn->tg));
289 }
290}
291
292/**
293 * throtl_peek_queued - peek the first bio on a qnode list
294 * @queued: the qnode list to peek
295 */
296static struct bio *throtl_peek_queued(struct list_head *queued)
297{
298 struct throtl_qnode *qn = list_first_entry(queued, struct throtl_qnode, node);
299 struct bio *bio;
300
301 if (list_empty(queued))
302 return NULL;
303
304 bio = bio_list_peek(&qn->bios);
305 WARN_ON_ONCE(!bio);
306 return bio;
307}
308
309/**
310 * throtl_pop_queued - pop the first bio form a qnode list
311 * @queued: the qnode list to pop a bio from
312 * @tg_to_put: optional out argument for throtl_grp to put
313 *
314 * Pop the first bio from the qnode list @queued. After popping, the first
315 * qnode is removed from @queued if empty or moved to the end of @queued so
316 * that the popping order is round-robin.
317 *
318 * When the first qnode is removed, its associated throtl_grp should be put
319 * too. If @tg_to_put is NULL, this function automatically puts it;
320 * otherwise, *@tg_to_put is set to the throtl_grp to put and the caller is
321 * responsible for putting it.
322 */
323static struct bio *throtl_pop_queued(struct list_head *queued,
324 struct throtl_grp **tg_to_put)
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_pop(&qn->bios);
333 WARN_ON_ONCE(!bio);
334
335 if (bio_list_empty(&qn->bios)) {
336 list_del_init(&qn->node);
337 if (tg_to_put)
338 *tg_to_put = qn->tg;
339 else
340 blkg_put(tg_to_blkg(qn->tg));
341 } else {
342 list_move_tail(&qn->node, queued);
343 }
344
345 return bio;
346}
347
49a2f1e3 348/* init a service_queue, assumes the caller zeroed it */
b2ce2643 349static void throtl_service_queue_init(struct throtl_service_queue *sq)
49a2f1e3 350{
c5cc2070
TH
351 INIT_LIST_HEAD(&sq->queued[0]);
352 INIT_LIST_HEAD(&sq->queued[1]);
49a2f1e3 353 sq->pending_tree = RB_ROOT;
69df0ab0
TH
354 setup_timer(&sq->pending_timer, throtl_pending_timer_fn,
355 (unsigned long)sq);
356}
357
001bea73
TH
358static struct blkg_policy_data *throtl_pd_alloc(gfp_t gfp, int node)
359{
4fb72036 360 struct throtl_grp *tg;
24bdb8ef 361 int rw;
4fb72036
TH
362
363 tg = kzalloc_node(sizeof(*tg), gfp, node);
364 if (!tg)
77ea7338 365 return NULL;
4fb72036 366
b2ce2643
TH
367 throtl_service_queue_init(&tg->service_queue);
368
369 for (rw = READ; rw <= WRITE; rw++) {
370 throtl_qnode_init(&tg->qnode_on_self[rw], tg);
371 throtl_qnode_init(&tg->qnode_on_parent[rw], tg);
372 }
373
374 RB_CLEAR_NODE(&tg->rb_node);
9f626e37
SL
375 tg->bps[READ][LIMIT_MAX] = U64_MAX;
376 tg->bps[WRITE][LIMIT_MAX] = U64_MAX;
377 tg->iops[READ][LIMIT_MAX] = UINT_MAX;
378 tg->iops[WRITE][LIMIT_MAX] = UINT_MAX;
cd5ab1b0
SL
379 tg->bps_conf[READ][LIMIT_MAX] = U64_MAX;
380 tg->bps_conf[WRITE][LIMIT_MAX] = U64_MAX;
381 tg->iops_conf[READ][LIMIT_MAX] = UINT_MAX;
382 tg->iops_conf[WRITE][LIMIT_MAX] = UINT_MAX;
383 /* LIMIT_LOW will have default value 0 */
b2ce2643 384
4fb72036 385 return &tg->pd;
001bea73
TH
386}
387
a9520cd6 388static void throtl_pd_init(struct blkg_policy_data *pd)
a29a171e 389{
a9520cd6
TH
390 struct throtl_grp *tg = pd_to_tg(pd);
391 struct blkcg_gq *blkg = tg_to_blkg(tg);
77216b04 392 struct throtl_data *td = blkg->q->td;
b2ce2643 393 struct throtl_service_queue *sq = &tg->service_queue;
cd1604fa 394
9138125b 395 /*
aa6ec29b 396 * If on the default hierarchy, we switch to properly hierarchical
9138125b
TH
397 * behavior where limits on a given throtl_grp are applied to the
398 * whole subtree rather than just the group itself. e.g. If 16M
399 * read_bps limit is set on the root group, the whole system can't
400 * exceed 16M for the device.
401 *
aa6ec29b 402 * If not on the default hierarchy, the broken flat hierarchy
9138125b
TH
403 * behavior is retained where all throtl_grps are treated as if
404 * they're all separate root groups right below throtl_data.
405 * Limits of a group don't interact with limits of other groups
406 * regardless of the position of the group in the hierarchy.
407 */
b2ce2643 408 sq->parent_sq = &td->service_queue;
9e10a130 409 if (cgroup_subsys_on_dfl(io_cgrp_subsys) && blkg->parent)
b2ce2643 410 sq->parent_sq = &blkg_to_tg(blkg->parent)->service_queue;
77216b04 411 tg->td = td;
8a3d2615
TH
412}
413
693e751e
TH
414/*
415 * Set has_rules[] if @tg or any of its parents have limits configured.
416 * This doesn't require walking up to the top of the hierarchy as the
417 * parent's has_rules[] is guaranteed to be correct.
418 */
419static void tg_update_has_rules(struct throtl_grp *tg)
420{
421 struct throtl_grp *parent_tg = sq_to_tg(tg->service_queue.parent_sq);
9f626e37 422 struct throtl_data *td = tg->td;
693e751e
TH
423 int rw;
424
425 for (rw = READ; rw <= WRITE; rw++)
426 tg->has_rules[rw] = (parent_tg && parent_tg->has_rules[rw]) ||
9f626e37
SL
427 (td->limit_valid[td->limit_index] &&
428 (tg_bps_limit(tg, rw) != U64_MAX ||
429 tg_iops_limit(tg, rw) != UINT_MAX));
693e751e
TH
430}
431
a9520cd6 432static void throtl_pd_online(struct blkg_policy_data *pd)
693e751e
TH
433{
434 /*
435 * We don't want new groups to escape the limits of its ancestors.
436 * Update has_rules[] after a new group is brought online.
437 */
a9520cd6 438 tg_update_has_rules(pd_to_tg(pd));
693e751e
TH
439}
440
cd5ab1b0
SL
441static void blk_throtl_update_limit_valid(struct throtl_data *td)
442{
443 struct cgroup_subsys_state *pos_css;
444 struct blkcg_gq *blkg;
445 bool low_valid = false;
446
447 rcu_read_lock();
448 blkg_for_each_descendant_post(blkg, pos_css, td->queue->root_blkg) {
449 struct throtl_grp *tg = blkg_to_tg(blkg);
450
451 if (tg->bps[READ][LIMIT_LOW] || tg->bps[WRITE][LIMIT_LOW] ||
452 tg->iops[READ][LIMIT_LOW] || tg->iops[WRITE][LIMIT_LOW])
453 low_valid = true;
454 }
455 rcu_read_unlock();
456
457 td->limit_valid[LIMIT_LOW] = low_valid;
458}
459
c79892c5 460static void throtl_upgrade_state(struct throtl_data *td);
cd5ab1b0
SL
461static void throtl_pd_offline(struct blkg_policy_data *pd)
462{
463 struct throtl_grp *tg = pd_to_tg(pd);
464
465 tg->bps[READ][LIMIT_LOW] = 0;
466 tg->bps[WRITE][LIMIT_LOW] = 0;
467 tg->iops[READ][LIMIT_LOW] = 0;
468 tg->iops[WRITE][LIMIT_LOW] = 0;
469
470 blk_throtl_update_limit_valid(tg->td);
471
c79892c5
SL
472 if (!tg->td->limit_valid[tg->td->limit_index])
473 throtl_upgrade_state(tg->td);
cd5ab1b0
SL
474}
475
001bea73
TH
476static void throtl_pd_free(struct blkg_policy_data *pd)
477{
4fb72036
TH
478 struct throtl_grp *tg = pd_to_tg(pd);
479
b2ce2643 480 del_timer_sync(&tg->service_queue.pending_timer);
4fb72036 481 kfree(tg);
001bea73
TH
482}
483
0049af73
TH
484static struct throtl_grp *
485throtl_rb_first(struct throtl_service_queue *parent_sq)
e43473b7
VG
486{
487 /* Service tree is empty */
0049af73 488 if (!parent_sq->nr_pending)
e43473b7
VG
489 return NULL;
490
0049af73
TH
491 if (!parent_sq->first_pending)
492 parent_sq->first_pending = rb_first(&parent_sq->pending_tree);
e43473b7 493
0049af73
TH
494 if (parent_sq->first_pending)
495 return rb_entry_tg(parent_sq->first_pending);
e43473b7
VG
496
497 return NULL;
498}
499
500static void rb_erase_init(struct rb_node *n, struct rb_root *root)
501{
502 rb_erase(n, root);
503 RB_CLEAR_NODE(n);
504}
505
0049af73
TH
506static void throtl_rb_erase(struct rb_node *n,
507 struct throtl_service_queue *parent_sq)
e43473b7 508{
0049af73
TH
509 if (parent_sq->first_pending == n)
510 parent_sq->first_pending = NULL;
511 rb_erase_init(n, &parent_sq->pending_tree);
512 --parent_sq->nr_pending;
e43473b7
VG
513}
514
0049af73 515static void update_min_dispatch_time(struct throtl_service_queue *parent_sq)
e43473b7
VG
516{
517 struct throtl_grp *tg;
518
0049af73 519 tg = throtl_rb_first(parent_sq);
e43473b7
VG
520 if (!tg)
521 return;
522
0049af73 523 parent_sq->first_pending_disptime = tg->disptime;
e43473b7
VG
524}
525
77216b04 526static void tg_service_queue_add(struct throtl_grp *tg)
e43473b7 527{
77216b04 528 struct throtl_service_queue *parent_sq = tg->service_queue.parent_sq;
0049af73 529 struct rb_node **node = &parent_sq->pending_tree.rb_node;
e43473b7
VG
530 struct rb_node *parent = NULL;
531 struct throtl_grp *__tg;
532 unsigned long key = tg->disptime;
533 int left = 1;
534
535 while (*node != NULL) {
536 parent = *node;
537 __tg = rb_entry_tg(parent);
538
539 if (time_before(key, __tg->disptime))
540 node = &parent->rb_left;
541 else {
542 node = &parent->rb_right;
543 left = 0;
544 }
545 }
546
547 if (left)
0049af73 548 parent_sq->first_pending = &tg->rb_node;
e43473b7
VG
549
550 rb_link_node(&tg->rb_node, parent, node);
0049af73 551 rb_insert_color(&tg->rb_node, &parent_sq->pending_tree);
e43473b7
VG
552}
553
77216b04 554static void __throtl_enqueue_tg(struct throtl_grp *tg)
e43473b7 555{
77216b04 556 tg_service_queue_add(tg);
5b2c16aa 557 tg->flags |= THROTL_TG_PENDING;
77216b04 558 tg->service_queue.parent_sq->nr_pending++;
e43473b7
VG
559}
560
77216b04 561static void throtl_enqueue_tg(struct throtl_grp *tg)
e43473b7 562{
5b2c16aa 563 if (!(tg->flags & THROTL_TG_PENDING))
77216b04 564 __throtl_enqueue_tg(tg);
e43473b7
VG
565}
566
77216b04 567static void __throtl_dequeue_tg(struct throtl_grp *tg)
e43473b7 568{
77216b04 569 throtl_rb_erase(&tg->rb_node, tg->service_queue.parent_sq);
5b2c16aa 570 tg->flags &= ~THROTL_TG_PENDING;
e43473b7
VG
571}
572
77216b04 573static void throtl_dequeue_tg(struct throtl_grp *tg)
e43473b7 574{
5b2c16aa 575 if (tg->flags & THROTL_TG_PENDING)
77216b04 576 __throtl_dequeue_tg(tg);
e43473b7
VG
577}
578
a9131a27 579/* Call with queue lock held */
69df0ab0
TH
580static void throtl_schedule_pending_timer(struct throtl_service_queue *sq,
581 unsigned long expires)
a9131a27 582{
69df0ab0
TH
583 mod_timer(&sq->pending_timer, expires);
584 throtl_log(sq, "schedule timer. delay=%lu jiffies=%lu",
585 expires - jiffies, jiffies);
a9131a27
TH
586}
587
7f52f98c
TH
588/**
589 * throtl_schedule_next_dispatch - schedule the next dispatch cycle
590 * @sq: the service_queue to schedule dispatch for
591 * @force: force scheduling
592 *
593 * Arm @sq->pending_timer so that the next dispatch cycle starts on the
594 * dispatch time of the first pending child. Returns %true if either timer
595 * is armed or there's no pending child left. %false if the current
596 * dispatch window is still open and the caller should continue
597 * dispatching.
598 *
599 * If @force is %true, the dispatch timer is always scheduled and this
600 * function is guaranteed to return %true. This is to be used when the
601 * caller can't dispatch itself and needs to invoke pending_timer
602 * unconditionally. Note that forced scheduling is likely to induce short
603 * delay before dispatch starts even if @sq->first_pending_disptime is not
604 * in the future and thus shouldn't be used in hot paths.
605 */
606static bool throtl_schedule_next_dispatch(struct throtl_service_queue *sq,
607 bool force)
e43473b7 608{
6a525600 609 /* any pending children left? */
c9e0332e 610 if (!sq->nr_pending)
7f52f98c 611 return true;
e43473b7 612
c9e0332e 613 update_min_dispatch_time(sq);
e43473b7 614
69df0ab0 615 /* is the next dispatch time in the future? */
7f52f98c 616 if (force || time_after(sq->first_pending_disptime, jiffies)) {
69df0ab0 617 throtl_schedule_pending_timer(sq, sq->first_pending_disptime);
7f52f98c 618 return true;
69df0ab0
TH
619 }
620
7f52f98c
TH
621 /* tell the caller to continue dispatching */
622 return false;
e43473b7
VG
623}
624
32ee5bc4
VG
625static inline void throtl_start_new_slice_with_credit(struct throtl_grp *tg,
626 bool rw, unsigned long start)
627{
628 tg->bytes_disp[rw] = 0;
629 tg->io_disp[rw] = 0;
630
631 /*
632 * Previous slice has expired. We must have trimmed it after last
633 * bio dispatch. That means since start of last slice, we never used
634 * that bandwidth. Do try to make use of that bandwidth while giving
635 * credit.
636 */
637 if (time_after_eq(start, tg->slice_start[rw]))
638 tg->slice_start[rw] = start;
639
640 tg->slice_end[rw] = jiffies + throtl_slice;
641 throtl_log(&tg->service_queue,
642 "[%c] new slice with credit start=%lu end=%lu jiffies=%lu",
643 rw == READ ? 'R' : 'W', tg->slice_start[rw],
644 tg->slice_end[rw], jiffies);
645}
646
0f3457f6 647static inline void throtl_start_new_slice(struct throtl_grp *tg, bool rw)
e43473b7
VG
648{
649 tg->bytes_disp[rw] = 0;
8e89d13f 650 tg->io_disp[rw] = 0;
e43473b7
VG
651 tg->slice_start[rw] = jiffies;
652 tg->slice_end[rw] = jiffies + throtl_slice;
fda6f272
TH
653 throtl_log(&tg->service_queue,
654 "[%c] new slice start=%lu end=%lu jiffies=%lu",
655 rw == READ ? 'R' : 'W', tg->slice_start[rw],
656 tg->slice_end[rw], jiffies);
e43473b7
VG
657}
658
0f3457f6
TH
659static inline void throtl_set_slice_end(struct throtl_grp *tg, bool rw,
660 unsigned long jiffy_end)
d1ae8ffd
VG
661{
662 tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
663}
664
0f3457f6
TH
665static inline void throtl_extend_slice(struct throtl_grp *tg, bool rw,
666 unsigned long jiffy_end)
e43473b7
VG
667{
668 tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
fda6f272
TH
669 throtl_log(&tg->service_queue,
670 "[%c] extend slice start=%lu end=%lu jiffies=%lu",
671 rw == READ ? 'R' : 'W', tg->slice_start[rw],
672 tg->slice_end[rw], jiffies);
e43473b7
VG
673}
674
675/* Determine if previously allocated or extended slice is complete or not */
0f3457f6 676static bool throtl_slice_used(struct throtl_grp *tg, bool rw)
e43473b7
VG
677{
678 if (time_in_range(jiffies, tg->slice_start[rw], tg->slice_end[rw]))
5cf8c227 679 return false;
e43473b7
VG
680
681 return 1;
682}
683
684/* Trim the used slices and adjust slice start accordingly */
0f3457f6 685static inline void throtl_trim_slice(struct throtl_grp *tg, bool rw)
e43473b7 686{
3aad5d3e
VG
687 unsigned long nr_slices, time_elapsed, io_trim;
688 u64 bytes_trim, tmp;
e43473b7
VG
689
690 BUG_ON(time_before(tg->slice_end[rw], tg->slice_start[rw]));
691
692 /*
693 * If bps are unlimited (-1), then time slice don't get
694 * renewed. Don't try to trim the slice if slice is used. A new
695 * slice will start when appropriate.
696 */
0f3457f6 697 if (throtl_slice_used(tg, rw))
e43473b7
VG
698 return;
699
d1ae8ffd
VG
700 /*
701 * A bio has been dispatched. Also adjust slice_end. It might happen
702 * that initially cgroup limit was very low resulting in high
703 * slice_end, but later limit was bumped up and bio was dispached
704 * sooner, then we need to reduce slice_end. A high bogus slice_end
705 * is bad because it does not allow new slice to start.
706 */
707
0f3457f6 708 throtl_set_slice_end(tg, rw, jiffies + throtl_slice);
d1ae8ffd 709
e43473b7
VG
710 time_elapsed = jiffies - tg->slice_start[rw];
711
712 nr_slices = time_elapsed / throtl_slice;
713
714 if (!nr_slices)
715 return;
9f626e37 716 tmp = tg_bps_limit(tg, rw) * throtl_slice * nr_slices;
3aad5d3e
VG
717 do_div(tmp, HZ);
718 bytes_trim = tmp;
e43473b7 719
9f626e37 720 io_trim = (tg_iops_limit(tg, rw) * throtl_slice * nr_slices) / HZ;
e43473b7 721
8e89d13f 722 if (!bytes_trim && !io_trim)
e43473b7
VG
723 return;
724
725 if (tg->bytes_disp[rw] >= bytes_trim)
726 tg->bytes_disp[rw] -= bytes_trim;
727 else
728 tg->bytes_disp[rw] = 0;
729
8e89d13f
VG
730 if (tg->io_disp[rw] >= io_trim)
731 tg->io_disp[rw] -= io_trim;
732 else
733 tg->io_disp[rw] = 0;
734
e43473b7
VG
735 tg->slice_start[rw] += nr_slices * throtl_slice;
736
fda6f272
TH
737 throtl_log(&tg->service_queue,
738 "[%c] trim slice nr=%lu bytes=%llu io=%lu start=%lu end=%lu jiffies=%lu",
739 rw == READ ? 'R' : 'W', nr_slices, bytes_trim, io_trim,
740 tg->slice_start[rw], tg->slice_end[rw], jiffies);
e43473b7
VG
741}
742
0f3457f6
TH
743static bool tg_with_in_iops_limit(struct throtl_grp *tg, struct bio *bio,
744 unsigned long *wait)
e43473b7
VG
745{
746 bool rw = bio_data_dir(bio);
8e89d13f 747 unsigned int io_allowed;
e43473b7 748 unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
c49c06e4 749 u64 tmp;
e43473b7 750
8e89d13f 751 jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
e43473b7 752
8e89d13f
VG
753 /* Slice has just started. Consider one slice interval */
754 if (!jiffy_elapsed)
755 jiffy_elapsed_rnd = throtl_slice;
756
757 jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
758
c49c06e4
VG
759 /*
760 * jiffy_elapsed_rnd should not be a big value as minimum iops can be
761 * 1 then at max jiffy elapsed should be equivalent of 1 second as we
762 * will allow dispatch after 1 second and after that slice should
763 * have been trimmed.
764 */
765
9f626e37 766 tmp = (u64)tg_iops_limit(tg, rw) * jiffy_elapsed_rnd;
c49c06e4
VG
767 do_div(tmp, HZ);
768
769 if (tmp > UINT_MAX)
770 io_allowed = UINT_MAX;
771 else
772 io_allowed = tmp;
8e89d13f
VG
773
774 if (tg->io_disp[rw] + 1 <= io_allowed) {
e43473b7
VG
775 if (wait)
776 *wait = 0;
5cf8c227 777 return true;
e43473b7
VG
778 }
779
8e89d13f 780 /* Calc approx time to dispatch */
9f626e37 781 jiffy_wait = ((tg->io_disp[rw] + 1) * HZ) / tg_iops_limit(tg, rw) + 1;
8e89d13f
VG
782
783 if (jiffy_wait > jiffy_elapsed)
784 jiffy_wait = jiffy_wait - jiffy_elapsed;
785 else
786 jiffy_wait = 1;
787
788 if (wait)
789 *wait = jiffy_wait;
790 return 0;
791}
792
0f3457f6
TH
793static bool tg_with_in_bps_limit(struct throtl_grp *tg, struct bio *bio,
794 unsigned long *wait)
8e89d13f
VG
795{
796 bool rw = bio_data_dir(bio);
3aad5d3e 797 u64 bytes_allowed, extra_bytes, tmp;
8e89d13f 798 unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
e43473b7
VG
799
800 jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
801
802 /* Slice has just started. Consider one slice interval */
803 if (!jiffy_elapsed)
804 jiffy_elapsed_rnd = throtl_slice;
805
806 jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
807
9f626e37 808 tmp = tg_bps_limit(tg, rw) * jiffy_elapsed_rnd;
5e901a2b 809 do_div(tmp, HZ);
3aad5d3e 810 bytes_allowed = tmp;
e43473b7 811
4f024f37 812 if (tg->bytes_disp[rw] + bio->bi_iter.bi_size <= bytes_allowed) {
e43473b7
VG
813 if (wait)
814 *wait = 0;
5cf8c227 815 return true;
e43473b7
VG
816 }
817
818 /* Calc approx time to dispatch */
4f024f37 819 extra_bytes = tg->bytes_disp[rw] + bio->bi_iter.bi_size - bytes_allowed;
9f626e37 820 jiffy_wait = div64_u64(extra_bytes * HZ, tg_bps_limit(tg, rw));
e43473b7
VG
821
822 if (!jiffy_wait)
823 jiffy_wait = 1;
824
825 /*
826 * This wait time is without taking into consideration the rounding
827 * up we did. Add that time also.
828 */
829 jiffy_wait = jiffy_wait + (jiffy_elapsed_rnd - jiffy_elapsed);
e43473b7
VG
830 if (wait)
831 *wait = jiffy_wait;
8e89d13f
VG
832 return 0;
833}
834
835/*
836 * Returns whether one can dispatch a bio or not. Also returns approx number
837 * of jiffies to wait before this bio is with-in IO rate and can be dispatched
838 */
0f3457f6
TH
839static bool tg_may_dispatch(struct throtl_grp *tg, struct bio *bio,
840 unsigned long *wait)
8e89d13f
VG
841{
842 bool rw = bio_data_dir(bio);
843 unsigned long bps_wait = 0, iops_wait = 0, max_wait = 0;
844
845 /*
846 * Currently whole state machine of group depends on first bio
847 * queued in the group bio list. So one should not be calling
848 * this function with a different bio if there are other bios
849 * queued.
850 */
73f0d49a 851 BUG_ON(tg->service_queue.nr_queued[rw] &&
c5cc2070 852 bio != throtl_peek_queued(&tg->service_queue.queued[rw]));
e43473b7 853
8e89d13f 854 /* If tg->bps = -1, then BW is unlimited */
9f626e37
SL
855 if (tg_bps_limit(tg, rw) == U64_MAX &&
856 tg_iops_limit(tg, rw) == UINT_MAX) {
8e89d13f
VG
857 if (wait)
858 *wait = 0;
5cf8c227 859 return true;
8e89d13f
VG
860 }
861
862 /*
863 * If previous slice expired, start a new one otherwise renew/extend
864 * existing slice to make sure it is at least throtl_slice interval
164c80ed
VG
865 * long since now. New slice is started only for empty throttle group.
866 * If there is queued bio, that means there should be an active
867 * slice and it should be extended instead.
8e89d13f 868 */
164c80ed 869 if (throtl_slice_used(tg, rw) && !(tg->service_queue.nr_queued[rw]))
0f3457f6 870 throtl_start_new_slice(tg, rw);
8e89d13f
VG
871 else {
872 if (time_before(tg->slice_end[rw], jiffies + throtl_slice))
0f3457f6 873 throtl_extend_slice(tg, rw, jiffies + throtl_slice);
8e89d13f
VG
874 }
875
0f3457f6
TH
876 if (tg_with_in_bps_limit(tg, bio, &bps_wait) &&
877 tg_with_in_iops_limit(tg, bio, &iops_wait)) {
8e89d13f
VG
878 if (wait)
879 *wait = 0;
880 return 1;
881 }
882
883 max_wait = max(bps_wait, iops_wait);
884
885 if (wait)
886 *wait = max_wait;
887
888 if (time_before(tg->slice_end[rw], jiffies + max_wait))
0f3457f6 889 throtl_extend_slice(tg, rw, jiffies + max_wait);
e43473b7
VG
890
891 return 0;
892}
893
894static void throtl_charge_bio(struct throtl_grp *tg, struct bio *bio)
895{
896 bool rw = bio_data_dir(bio);
e43473b7
VG
897
898 /* Charge the bio to the group */
4f024f37 899 tg->bytes_disp[rw] += bio->bi_iter.bi_size;
8e89d13f 900 tg->io_disp[rw]++;
e43473b7 901
2a0f61e6 902 /*
8d2bbd4c 903 * BIO_THROTTLED is used to prevent the same bio to be throttled
2a0f61e6
TH
904 * more than once as a throttled bio will go through blk-throtl the
905 * second time when it eventually gets issued. Set it when a bio
906 * is being charged to a tg.
2a0f61e6 907 */
8d2bbd4c
CH
908 if (!bio_flagged(bio, BIO_THROTTLED))
909 bio_set_flag(bio, BIO_THROTTLED);
e43473b7
VG
910}
911
c5cc2070
TH
912/**
913 * throtl_add_bio_tg - add a bio to the specified throtl_grp
914 * @bio: bio to add
915 * @qn: qnode to use
916 * @tg: the target throtl_grp
917 *
918 * Add @bio to @tg's service_queue using @qn. If @qn is not specified,
919 * tg->qnode_on_self[] is used.
920 */
921static void throtl_add_bio_tg(struct bio *bio, struct throtl_qnode *qn,
922 struct throtl_grp *tg)
e43473b7 923{
73f0d49a 924 struct throtl_service_queue *sq = &tg->service_queue;
e43473b7
VG
925 bool rw = bio_data_dir(bio);
926
c5cc2070
TH
927 if (!qn)
928 qn = &tg->qnode_on_self[rw];
929
0e9f4164
TH
930 /*
931 * If @tg doesn't currently have any bios queued in the same
932 * direction, queueing @bio can change when @tg should be
933 * dispatched. Mark that @tg was empty. This is automatically
934 * cleaered on the next tg_update_disptime().
935 */
936 if (!sq->nr_queued[rw])
937 tg->flags |= THROTL_TG_WAS_EMPTY;
938
c5cc2070
TH
939 throtl_qnode_add_bio(bio, qn, &sq->queued[rw]);
940
73f0d49a 941 sq->nr_queued[rw]++;
77216b04 942 throtl_enqueue_tg(tg);
e43473b7
VG
943}
944
77216b04 945static void tg_update_disptime(struct throtl_grp *tg)
e43473b7 946{
73f0d49a 947 struct throtl_service_queue *sq = &tg->service_queue;
e43473b7
VG
948 unsigned long read_wait = -1, write_wait = -1, min_wait = -1, disptime;
949 struct bio *bio;
950
d609af3a
ME
951 bio = throtl_peek_queued(&sq->queued[READ]);
952 if (bio)
0f3457f6 953 tg_may_dispatch(tg, bio, &read_wait);
e43473b7 954
d609af3a
ME
955 bio = throtl_peek_queued(&sq->queued[WRITE]);
956 if (bio)
0f3457f6 957 tg_may_dispatch(tg, bio, &write_wait);
e43473b7
VG
958
959 min_wait = min(read_wait, write_wait);
960 disptime = jiffies + min_wait;
961
e43473b7 962 /* Update dispatch time */
77216b04 963 throtl_dequeue_tg(tg);
e43473b7 964 tg->disptime = disptime;
77216b04 965 throtl_enqueue_tg(tg);
0e9f4164
TH
966
967 /* see throtl_add_bio_tg() */
968 tg->flags &= ~THROTL_TG_WAS_EMPTY;
e43473b7
VG
969}
970
32ee5bc4
VG
971static void start_parent_slice_with_credit(struct throtl_grp *child_tg,
972 struct throtl_grp *parent_tg, bool rw)
973{
974 if (throtl_slice_used(parent_tg, rw)) {
975 throtl_start_new_slice_with_credit(parent_tg, rw,
976 child_tg->slice_start[rw]);
977 }
978
979}
980
77216b04 981static void tg_dispatch_one_bio(struct throtl_grp *tg, bool rw)
e43473b7 982{
73f0d49a 983 struct throtl_service_queue *sq = &tg->service_queue;
6bc9c2b4
TH
984 struct throtl_service_queue *parent_sq = sq->parent_sq;
985 struct throtl_grp *parent_tg = sq_to_tg(parent_sq);
c5cc2070 986 struct throtl_grp *tg_to_put = NULL;
e43473b7
VG
987 struct bio *bio;
988
c5cc2070
TH
989 /*
990 * @bio is being transferred from @tg to @parent_sq. Popping a bio
991 * from @tg may put its reference and @parent_sq might end up
992 * getting released prematurely. Remember the tg to put and put it
993 * after @bio is transferred to @parent_sq.
994 */
995 bio = throtl_pop_queued(&sq->queued[rw], &tg_to_put);
73f0d49a 996 sq->nr_queued[rw]--;
e43473b7
VG
997
998 throtl_charge_bio(tg, bio);
6bc9c2b4
TH
999
1000 /*
1001 * If our parent is another tg, we just need to transfer @bio to
1002 * the parent using throtl_add_bio_tg(). If our parent is
1003 * @td->service_queue, @bio is ready to be issued. Put it on its
1004 * bio_lists[] and decrease total number queued. The caller is
1005 * responsible for issuing these bios.
1006 */
1007 if (parent_tg) {
c5cc2070 1008 throtl_add_bio_tg(bio, &tg->qnode_on_parent[rw], parent_tg);
32ee5bc4 1009 start_parent_slice_with_credit(tg, parent_tg, rw);
6bc9c2b4 1010 } else {
c5cc2070
TH
1011 throtl_qnode_add_bio(bio, &tg->qnode_on_parent[rw],
1012 &parent_sq->queued[rw]);
6bc9c2b4
TH
1013 BUG_ON(tg->td->nr_queued[rw] <= 0);
1014 tg->td->nr_queued[rw]--;
1015 }
e43473b7 1016
0f3457f6 1017 throtl_trim_slice(tg, rw);
6bc9c2b4 1018
c5cc2070
TH
1019 if (tg_to_put)
1020 blkg_put(tg_to_blkg(tg_to_put));
e43473b7
VG
1021}
1022
77216b04 1023static int throtl_dispatch_tg(struct throtl_grp *tg)
e43473b7 1024{
73f0d49a 1025 struct throtl_service_queue *sq = &tg->service_queue;
e43473b7
VG
1026 unsigned int nr_reads = 0, nr_writes = 0;
1027 unsigned int max_nr_reads = throtl_grp_quantum*3/4;
c2f6805d 1028 unsigned int max_nr_writes = throtl_grp_quantum - max_nr_reads;
e43473b7
VG
1029 struct bio *bio;
1030
1031 /* Try to dispatch 75% READS and 25% WRITES */
1032
c5cc2070 1033 while ((bio = throtl_peek_queued(&sq->queued[READ])) &&
0f3457f6 1034 tg_may_dispatch(tg, bio, NULL)) {
e43473b7 1035
77216b04 1036 tg_dispatch_one_bio(tg, bio_data_dir(bio));
e43473b7
VG
1037 nr_reads++;
1038
1039 if (nr_reads >= max_nr_reads)
1040 break;
1041 }
1042
c5cc2070 1043 while ((bio = throtl_peek_queued(&sq->queued[WRITE])) &&
0f3457f6 1044 tg_may_dispatch(tg, bio, NULL)) {
e43473b7 1045
77216b04 1046 tg_dispatch_one_bio(tg, bio_data_dir(bio));
e43473b7
VG
1047 nr_writes++;
1048
1049 if (nr_writes >= max_nr_writes)
1050 break;
1051 }
1052
1053 return nr_reads + nr_writes;
1054}
1055
651930bc 1056static int throtl_select_dispatch(struct throtl_service_queue *parent_sq)
e43473b7
VG
1057{
1058 unsigned int nr_disp = 0;
e43473b7
VG
1059
1060 while (1) {
73f0d49a
TH
1061 struct throtl_grp *tg = throtl_rb_first(parent_sq);
1062 struct throtl_service_queue *sq = &tg->service_queue;
e43473b7
VG
1063
1064 if (!tg)
1065 break;
1066
1067 if (time_before(jiffies, tg->disptime))
1068 break;
1069
77216b04 1070 throtl_dequeue_tg(tg);
e43473b7 1071
77216b04 1072 nr_disp += throtl_dispatch_tg(tg);
e43473b7 1073
73f0d49a 1074 if (sq->nr_queued[0] || sq->nr_queued[1])
77216b04 1075 tg_update_disptime(tg);
e43473b7
VG
1076
1077 if (nr_disp >= throtl_quantum)
1078 break;
1079 }
1080
1081 return nr_disp;
1082}
1083
c79892c5
SL
1084static bool throtl_can_upgrade(struct throtl_data *td,
1085 struct throtl_grp *this_tg);
6e1a5704
TH
1086/**
1087 * throtl_pending_timer_fn - timer function for service_queue->pending_timer
1088 * @arg: the throtl_service_queue being serviced
1089 *
1090 * This timer is armed when a child throtl_grp with active bio's become
1091 * pending and queued on the service_queue's pending_tree and expires when
1092 * the first child throtl_grp should be dispatched. This function
2e48a530
TH
1093 * dispatches bio's from the children throtl_grps to the parent
1094 * service_queue.
1095 *
1096 * If the parent's parent is another throtl_grp, dispatching is propagated
1097 * by either arming its pending_timer or repeating dispatch directly. If
1098 * the top-level service_tree is reached, throtl_data->dispatch_work is
1099 * kicked so that the ready bio's are issued.
6e1a5704 1100 */
69df0ab0
TH
1101static void throtl_pending_timer_fn(unsigned long arg)
1102{
1103 struct throtl_service_queue *sq = (void *)arg;
2e48a530 1104 struct throtl_grp *tg = sq_to_tg(sq);
69df0ab0 1105 struct throtl_data *td = sq_to_td(sq);
cb76199c 1106 struct request_queue *q = td->queue;
2e48a530
TH
1107 struct throtl_service_queue *parent_sq;
1108 bool dispatched;
6e1a5704 1109 int ret;
e43473b7
VG
1110
1111 spin_lock_irq(q->queue_lock);
c79892c5
SL
1112 if (throtl_can_upgrade(td, NULL))
1113 throtl_upgrade_state(td);
1114
2e48a530
TH
1115again:
1116 parent_sq = sq->parent_sq;
1117 dispatched = false;
e43473b7 1118
7f52f98c
TH
1119 while (true) {
1120 throtl_log(sq, "dispatch nr_queued=%u read=%u write=%u",
2e48a530
TH
1121 sq->nr_queued[READ] + sq->nr_queued[WRITE],
1122 sq->nr_queued[READ], sq->nr_queued[WRITE]);
7f52f98c
TH
1123
1124 ret = throtl_select_dispatch(sq);
1125 if (ret) {
7f52f98c
TH
1126 throtl_log(sq, "bios disp=%u", ret);
1127 dispatched = true;
1128 }
e43473b7 1129
7f52f98c
TH
1130 if (throtl_schedule_next_dispatch(sq, false))
1131 break;
e43473b7 1132
7f52f98c
TH
1133 /* this dispatch windows is still open, relax and repeat */
1134 spin_unlock_irq(q->queue_lock);
1135 cpu_relax();
1136 spin_lock_irq(q->queue_lock);
651930bc 1137 }
e43473b7 1138
2e48a530
TH
1139 if (!dispatched)
1140 goto out_unlock;
6e1a5704 1141
2e48a530
TH
1142 if (parent_sq) {
1143 /* @parent_sq is another throl_grp, propagate dispatch */
1144 if (tg->flags & THROTL_TG_WAS_EMPTY) {
1145 tg_update_disptime(tg);
1146 if (!throtl_schedule_next_dispatch(parent_sq, false)) {
1147 /* window is already open, repeat dispatching */
1148 sq = parent_sq;
1149 tg = sq_to_tg(sq);
1150 goto again;
1151 }
1152 }
1153 } else {
1154 /* reached the top-level, queue issueing */
1155 queue_work(kthrotld_workqueue, &td->dispatch_work);
1156 }
1157out_unlock:
e43473b7 1158 spin_unlock_irq(q->queue_lock);
6e1a5704 1159}
e43473b7 1160
6e1a5704
TH
1161/**
1162 * blk_throtl_dispatch_work_fn - work function for throtl_data->dispatch_work
1163 * @work: work item being executed
1164 *
1165 * This function is queued for execution when bio's reach the bio_lists[]
1166 * of throtl_data->service_queue. Those bio's are ready and issued by this
1167 * function.
1168 */
8876e140 1169static void blk_throtl_dispatch_work_fn(struct work_struct *work)
6e1a5704
TH
1170{
1171 struct throtl_data *td = container_of(work, struct throtl_data,
1172 dispatch_work);
1173 struct throtl_service_queue *td_sq = &td->service_queue;
1174 struct request_queue *q = td->queue;
1175 struct bio_list bio_list_on_stack;
1176 struct bio *bio;
1177 struct blk_plug plug;
1178 int rw;
1179
1180 bio_list_init(&bio_list_on_stack);
1181
1182 spin_lock_irq(q->queue_lock);
c5cc2070
TH
1183 for (rw = READ; rw <= WRITE; rw++)
1184 while ((bio = throtl_pop_queued(&td_sq->queued[rw], NULL)))
1185 bio_list_add(&bio_list_on_stack, bio);
6e1a5704
TH
1186 spin_unlock_irq(q->queue_lock);
1187
1188 if (!bio_list_empty(&bio_list_on_stack)) {
69d60eb9 1189 blk_start_plug(&plug);
e43473b7
VG
1190 while((bio = bio_list_pop(&bio_list_on_stack)))
1191 generic_make_request(bio);
69d60eb9 1192 blk_finish_plug(&plug);
e43473b7 1193 }
e43473b7
VG
1194}
1195
f95a04af
TH
1196static u64 tg_prfill_conf_u64(struct seq_file *sf, struct blkg_policy_data *pd,
1197 int off)
60c2bc2d 1198{
f95a04af
TH
1199 struct throtl_grp *tg = pd_to_tg(pd);
1200 u64 v = *(u64 *)((void *)tg + off);
60c2bc2d 1201
2ab5492d 1202 if (v == U64_MAX)
60c2bc2d 1203 return 0;
f95a04af 1204 return __blkg_prfill_u64(sf, pd, v);
60c2bc2d
TH
1205}
1206
f95a04af
TH
1207static u64 tg_prfill_conf_uint(struct seq_file *sf, struct blkg_policy_data *pd,
1208 int off)
e43473b7 1209{
f95a04af
TH
1210 struct throtl_grp *tg = pd_to_tg(pd);
1211 unsigned int v = *(unsigned int *)((void *)tg + off);
fe071437 1212
2ab5492d 1213 if (v == UINT_MAX)
af133ceb 1214 return 0;
f95a04af 1215 return __blkg_prfill_u64(sf, pd, v);
e43473b7
VG
1216}
1217
2da8ca82 1218static int tg_print_conf_u64(struct seq_file *sf, void *v)
8e89d13f 1219{
2da8ca82
TH
1220 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), tg_prfill_conf_u64,
1221 &blkcg_policy_throtl, seq_cft(sf)->private, false);
af133ceb 1222 return 0;
8e89d13f
VG
1223}
1224
2da8ca82 1225static int tg_print_conf_uint(struct seq_file *sf, void *v)
8e89d13f 1226{
2da8ca82
TH
1227 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), tg_prfill_conf_uint,
1228 &blkcg_policy_throtl, seq_cft(sf)->private, false);
af133ceb 1229 return 0;
60c2bc2d
TH
1230}
1231
69948b07 1232static void tg_conf_updated(struct throtl_grp *tg)
60c2bc2d 1233{
69948b07 1234 struct throtl_service_queue *sq = &tg->service_queue;
492eb21b 1235 struct cgroup_subsys_state *pos_css;
69948b07 1236 struct blkcg_gq *blkg;
af133ceb 1237
fda6f272
TH
1238 throtl_log(&tg->service_queue,
1239 "limit change rbps=%llu wbps=%llu riops=%u wiops=%u",
9f626e37
SL
1240 tg_bps_limit(tg, READ), tg_bps_limit(tg, WRITE),
1241 tg_iops_limit(tg, READ), tg_iops_limit(tg, WRITE));
632b4493 1242
693e751e
TH
1243 /*
1244 * Update has_rules[] flags for the updated tg's subtree. A tg is
1245 * considered to have rules if either the tg itself or any of its
1246 * ancestors has rules. This identifies groups without any
1247 * restrictions in the whole hierarchy and allows them to bypass
1248 * blk-throttle.
1249 */
69948b07 1250 blkg_for_each_descendant_pre(blkg, pos_css, tg_to_blkg(tg))
693e751e
TH
1251 tg_update_has_rules(blkg_to_tg(blkg));
1252
632b4493
TH
1253 /*
1254 * We're already holding queue_lock and know @tg is valid. Let's
1255 * apply the new config directly.
1256 *
1257 * Restart the slices for both READ and WRITES. It might happen
1258 * that a group's limit are dropped suddenly and we don't want to
1259 * account recently dispatched IO with new low rate.
1260 */
0f3457f6
TH
1261 throtl_start_new_slice(tg, 0);
1262 throtl_start_new_slice(tg, 1);
632b4493 1263
5b2c16aa 1264 if (tg->flags & THROTL_TG_PENDING) {
77216b04 1265 tg_update_disptime(tg);
7f52f98c 1266 throtl_schedule_next_dispatch(sq->parent_sq, true);
632b4493 1267 }
69948b07
TH
1268}
1269
1270static ssize_t tg_set_conf(struct kernfs_open_file *of,
1271 char *buf, size_t nbytes, loff_t off, bool is_u64)
1272{
1273 struct blkcg *blkcg = css_to_blkcg(of_css(of));
1274 struct blkg_conf_ctx ctx;
1275 struct throtl_grp *tg;
1276 int ret;
1277 u64 v;
1278
1279 ret = blkg_conf_prep(blkcg, &blkcg_policy_throtl, buf, &ctx);
1280 if (ret)
1281 return ret;
1282
1283 ret = -EINVAL;
1284 if (sscanf(ctx.body, "%llu", &v) != 1)
1285 goto out_finish;
1286 if (!v)
2ab5492d 1287 v = U64_MAX;
69948b07
TH
1288
1289 tg = blkg_to_tg(ctx.blkg);
1290
1291 if (is_u64)
1292 *(u64 *)((void *)tg + of_cft(of)->private) = v;
1293 else
1294 *(unsigned int *)((void *)tg + of_cft(of)->private) = v;
60c2bc2d 1295
69948b07 1296 tg_conf_updated(tg);
36aa9e5f
TH
1297 ret = 0;
1298out_finish:
60c2bc2d 1299 blkg_conf_finish(&ctx);
36aa9e5f 1300 return ret ?: nbytes;
8e89d13f
VG
1301}
1302
451af504
TH
1303static ssize_t tg_set_conf_u64(struct kernfs_open_file *of,
1304 char *buf, size_t nbytes, loff_t off)
60c2bc2d 1305{
451af504 1306 return tg_set_conf(of, buf, nbytes, off, true);
60c2bc2d
TH
1307}
1308
451af504
TH
1309static ssize_t tg_set_conf_uint(struct kernfs_open_file *of,
1310 char *buf, size_t nbytes, loff_t off)
60c2bc2d 1311{
451af504 1312 return tg_set_conf(of, buf, nbytes, off, false);
60c2bc2d
TH
1313}
1314
880f50e2 1315static struct cftype throtl_legacy_files[] = {
60c2bc2d
TH
1316 {
1317 .name = "throttle.read_bps_device",
9f626e37 1318 .private = offsetof(struct throtl_grp, bps[READ][LIMIT_MAX]),
2da8ca82 1319 .seq_show = tg_print_conf_u64,
451af504 1320 .write = tg_set_conf_u64,
60c2bc2d
TH
1321 },
1322 {
1323 .name = "throttle.write_bps_device",
9f626e37 1324 .private = offsetof(struct throtl_grp, bps[WRITE][LIMIT_MAX]),
2da8ca82 1325 .seq_show = tg_print_conf_u64,
451af504 1326 .write = tg_set_conf_u64,
60c2bc2d
TH
1327 },
1328 {
1329 .name = "throttle.read_iops_device",
9f626e37 1330 .private = offsetof(struct throtl_grp, iops[READ][LIMIT_MAX]),
2da8ca82 1331 .seq_show = tg_print_conf_uint,
451af504 1332 .write = tg_set_conf_uint,
60c2bc2d
TH
1333 },
1334 {
1335 .name = "throttle.write_iops_device",
9f626e37 1336 .private = offsetof(struct throtl_grp, iops[WRITE][LIMIT_MAX]),
2da8ca82 1337 .seq_show = tg_print_conf_uint,
451af504 1338 .write = tg_set_conf_uint,
60c2bc2d
TH
1339 },
1340 {
1341 .name = "throttle.io_service_bytes",
77ea7338
TH
1342 .private = (unsigned long)&blkcg_policy_throtl,
1343 .seq_show = blkg_print_stat_bytes,
60c2bc2d
TH
1344 },
1345 {
1346 .name = "throttle.io_serviced",
77ea7338
TH
1347 .private = (unsigned long)&blkcg_policy_throtl,
1348 .seq_show = blkg_print_stat_ios,
60c2bc2d
TH
1349 },
1350 { } /* terminate */
1351};
1352
cd5ab1b0 1353static u64 tg_prfill_limit(struct seq_file *sf, struct blkg_policy_data *pd,
2ee867dc
TH
1354 int off)
1355{
1356 struct throtl_grp *tg = pd_to_tg(pd);
1357 const char *dname = blkg_dev_name(pd->blkg);
1358 char bufs[4][21] = { "max", "max", "max", "max" };
cd5ab1b0
SL
1359 u64 bps_dft;
1360 unsigned int iops_dft;
2ee867dc
TH
1361
1362 if (!dname)
1363 return 0;
9f626e37 1364
cd5ab1b0
SL
1365 if (off == LIMIT_LOW) {
1366 bps_dft = 0;
1367 iops_dft = 0;
1368 } else {
1369 bps_dft = U64_MAX;
1370 iops_dft = UINT_MAX;
1371 }
1372
1373 if (tg->bps_conf[READ][off] == bps_dft &&
1374 tg->bps_conf[WRITE][off] == bps_dft &&
1375 tg->iops_conf[READ][off] == iops_dft &&
1376 tg->iops_conf[WRITE][off] == iops_dft)
2ee867dc
TH
1377 return 0;
1378
cd5ab1b0 1379 if (tg->bps_conf[READ][off] != bps_dft)
9f626e37 1380 snprintf(bufs[0], sizeof(bufs[0]), "%llu",
cd5ab1b0
SL
1381 tg->bps_conf[READ][off]);
1382 if (tg->bps_conf[WRITE][off] != bps_dft)
9f626e37 1383 snprintf(bufs[1], sizeof(bufs[1]), "%llu",
cd5ab1b0
SL
1384 tg->bps_conf[WRITE][off]);
1385 if (tg->iops_conf[READ][off] != iops_dft)
9f626e37 1386 snprintf(bufs[2], sizeof(bufs[2]), "%u",
cd5ab1b0
SL
1387 tg->iops_conf[READ][off]);
1388 if (tg->iops_conf[WRITE][off] != iops_dft)
9f626e37 1389 snprintf(bufs[3], sizeof(bufs[3]), "%u",
cd5ab1b0 1390 tg->iops_conf[WRITE][off]);
2ee867dc
TH
1391
1392 seq_printf(sf, "%s rbps=%s wbps=%s riops=%s wiops=%s\n",
1393 dname, bufs[0], bufs[1], bufs[2], bufs[3]);
1394 return 0;
1395}
1396
cd5ab1b0 1397static int tg_print_limit(struct seq_file *sf, void *v)
2ee867dc 1398{
cd5ab1b0 1399 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), tg_prfill_limit,
2ee867dc
TH
1400 &blkcg_policy_throtl, seq_cft(sf)->private, false);
1401 return 0;
1402}
1403
cd5ab1b0 1404static ssize_t tg_set_limit(struct kernfs_open_file *of,
2ee867dc
TH
1405 char *buf, size_t nbytes, loff_t off)
1406{
1407 struct blkcg *blkcg = css_to_blkcg(of_css(of));
1408 struct blkg_conf_ctx ctx;
1409 struct throtl_grp *tg;
1410 u64 v[4];
1411 int ret;
cd5ab1b0 1412 int index = of_cft(of)->private;
2ee867dc
TH
1413
1414 ret = blkg_conf_prep(blkcg, &blkcg_policy_throtl, buf, &ctx);
1415 if (ret)
1416 return ret;
1417
1418 tg = blkg_to_tg(ctx.blkg);
1419
cd5ab1b0
SL
1420 v[0] = tg->bps_conf[READ][index];
1421 v[1] = tg->bps_conf[WRITE][index];
1422 v[2] = tg->iops_conf[READ][index];
1423 v[3] = tg->iops_conf[WRITE][index];
2ee867dc
TH
1424
1425 while (true) {
1426 char tok[27]; /* wiops=18446744073709551616 */
1427 char *p;
2ab5492d 1428 u64 val = U64_MAX;
2ee867dc
TH
1429 int len;
1430
1431 if (sscanf(ctx.body, "%26s%n", tok, &len) != 1)
1432 break;
1433 if (tok[0] == '\0')
1434 break;
1435 ctx.body += len;
1436
1437 ret = -EINVAL;
1438 p = tok;
1439 strsep(&p, "=");
1440 if (!p || (sscanf(p, "%llu", &val) != 1 && strcmp(p, "max")))
1441 goto out_finish;
1442
1443 ret = -ERANGE;
1444 if (!val)
1445 goto out_finish;
1446
1447 ret = -EINVAL;
1448 if (!strcmp(tok, "rbps"))
1449 v[0] = val;
1450 else if (!strcmp(tok, "wbps"))
1451 v[1] = val;
1452 else if (!strcmp(tok, "riops"))
1453 v[2] = min_t(u64, val, UINT_MAX);
1454 else if (!strcmp(tok, "wiops"))
1455 v[3] = min_t(u64, val, UINT_MAX);
1456 else
1457 goto out_finish;
1458 }
1459
cd5ab1b0
SL
1460 tg->bps_conf[READ][index] = v[0];
1461 tg->bps_conf[WRITE][index] = v[1];
1462 tg->iops_conf[READ][index] = v[2];
1463 tg->iops_conf[WRITE][index] = v[3];
2ee867dc 1464
cd5ab1b0
SL
1465 if (index == LIMIT_MAX) {
1466 tg->bps[READ][index] = v[0];
1467 tg->bps[WRITE][index] = v[1];
1468 tg->iops[READ][index] = v[2];
1469 tg->iops[WRITE][index] = v[3];
1470 }
1471 tg->bps[READ][LIMIT_LOW] = min(tg->bps_conf[READ][LIMIT_LOW],
1472 tg->bps_conf[READ][LIMIT_MAX]);
1473 tg->bps[WRITE][LIMIT_LOW] = min(tg->bps_conf[WRITE][LIMIT_LOW],
1474 tg->bps_conf[WRITE][LIMIT_MAX]);
1475 tg->iops[READ][LIMIT_LOW] = min(tg->iops_conf[READ][LIMIT_LOW],
1476 tg->iops_conf[READ][LIMIT_MAX]);
1477 tg->iops[WRITE][LIMIT_LOW] = min(tg->iops_conf[WRITE][LIMIT_LOW],
1478 tg->iops_conf[WRITE][LIMIT_MAX]);
1479
1480 if (index == LIMIT_LOW) {
1481 blk_throtl_update_limit_valid(tg->td);
1482 if (tg->td->limit_valid[LIMIT_LOW])
1483 tg->td->limit_index = LIMIT_LOW;
1484 }
2ee867dc
TH
1485 tg_conf_updated(tg);
1486 ret = 0;
1487out_finish:
1488 blkg_conf_finish(&ctx);
1489 return ret ?: nbytes;
1490}
1491
1492static struct cftype throtl_files[] = {
cd5ab1b0
SL
1493#ifdef CONFIG_BLK_DEV_THROTTLING_LOW
1494 {
1495 .name = "low",
1496 .flags = CFTYPE_NOT_ON_ROOT,
1497 .seq_show = tg_print_limit,
1498 .write = tg_set_limit,
1499 .private = LIMIT_LOW,
1500 },
1501#endif
2ee867dc
TH
1502 {
1503 .name = "max",
1504 .flags = CFTYPE_NOT_ON_ROOT,
cd5ab1b0
SL
1505 .seq_show = tg_print_limit,
1506 .write = tg_set_limit,
1507 .private = LIMIT_MAX,
2ee867dc
TH
1508 },
1509 { } /* terminate */
1510};
1511
da527770 1512static void throtl_shutdown_wq(struct request_queue *q)
e43473b7
VG
1513{
1514 struct throtl_data *td = q->td;
1515
69df0ab0 1516 cancel_work_sync(&td->dispatch_work);
e43473b7
VG
1517}
1518
3c798398 1519static struct blkcg_policy blkcg_policy_throtl = {
2ee867dc 1520 .dfl_cftypes = throtl_files,
880f50e2 1521 .legacy_cftypes = throtl_legacy_files,
f9fcc2d3 1522
001bea73 1523 .pd_alloc_fn = throtl_pd_alloc,
f9fcc2d3 1524 .pd_init_fn = throtl_pd_init,
693e751e 1525 .pd_online_fn = throtl_pd_online,
cd5ab1b0 1526 .pd_offline_fn = throtl_pd_offline,
001bea73 1527 .pd_free_fn = throtl_pd_free,
e43473b7
VG
1528};
1529
c79892c5
SL
1530static bool throtl_tg_can_upgrade(struct throtl_grp *tg)
1531{
1532 struct throtl_service_queue *sq = &tg->service_queue;
1533 bool read_limit, write_limit;
1534
1535 /*
1536 * if cgroup reaches low limit (if low limit is 0, the cgroup always
1537 * reaches), it's ok to upgrade to next limit
1538 */
1539 read_limit = tg->bps[READ][LIMIT_LOW] || tg->iops[READ][LIMIT_LOW];
1540 write_limit = tg->bps[WRITE][LIMIT_LOW] || tg->iops[WRITE][LIMIT_LOW];
1541 if (!read_limit && !write_limit)
1542 return true;
1543 if (read_limit && sq->nr_queued[READ] &&
1544 (!write_limit || sq->nr_queued[WRITE]))
1545 return true;
1546 if (write_limit && sq->nr_queued[WRITE] &&
1547 (!read_limit || sq->nr_queued[READ]))
1548 return true;
1549 return false;
1550}
1551
1552static bool throtl_hierarchy_can_upgrade(struct throtl_grp *tg)
1553{
1554 while (true) {
1555 if (throtl_tg_can_upgrade(tg))
1556 return true;
1557 tg = sq_to_tg(tg->service_queue.parent_sq);
1558 if (!tg || !tg_to_blkg(tg)->parent)
1559 return false;
1560 }
1561 return false;
1562}
1563
1564static bool throtl_can_upgrade(struct throtl_data *td,
1565 struct throtl_grp *this_tg)
1566{
1567 struct cgroup_subsys_state *pos_css;
1568 struct blkcg_gq *blkg;
1569
1570 if (td->limit_index != LIMIT_LOW)
1571 return false;
1572
1573 rcu_read_lock();
1574 blkg_for_each_descendant_post(blkg, pos_css, td->queue->root_blkg) {
1575 struct throtl_grp *tg = blkg_to_tg(blkg);
1576
1577 if (tg == this_tg)
1578 continue;
1579 if (!list_empty(&tg_to_blkg(tg)->blkcg->css.children))
1580 continue;
1581 if (!throtl_hierarchy_can_upgrade(tg)) {
1582 rcu_read_unlock();
1583 return false;
1584 }
1585 }
1586 rcu_read_unlock();
1587 return true;
1588}
1589
1590static void throtl_upgrade_state(struct throtl_data *td)
1591{
1592 struct cgroup_subsys_state *pos_css;
1593 struct blkcg_gq *blkg;
1594
1595 td->limit_index = LIMIT_MAX;
1596 rcu_read_lock();
1597 blkg_for_each_descendant_post(blkg, pos_css, td->queue->root_blkg) {
1598 struct throtl_grp *tg = blkg_to_tg(blkg);
1599 struct throtl_service_queue *sq = &tg->service_queue;
1600
1601 tg->disptime = jiffies - 1;
1602 throtl_select_dispatch(sq);
1603 throtl_schedule_next_dispatch(sq, false);
1604 }
1605 rcu_read_unlock();
1606 throtl_select_dispatch(&td->service_queue);
1607 throtl_schedule_next_dispatch(&td->service_queue, false);
1608 queue_work(kthrotld_workqueue, &td->dispatch_work);
1609}
1610
ae118896
TH
1611bool blk_throtl_bio(struct request_queue *q, struct blkcg_gq *blkg,
1612 struct bio *bio)
e43473b7 1613{
c5cc2070 1614 struct throtl_qnode *qn = NULL;
ae118896 1615 struct throtl_grp *tg = blkg_to_tg(blkg ?: q->root_blkg);
73f0d49a 1616 struct throtl_service_queue *sq;
0e9f4164 1617 bool rw = bio_data_dir(bio);
bc16a4f9 1618 bool throttled = false;
e43473b7 1619
ae118896
TH
1620 WARN_ON_ONCE(!rcu_read_lock_held());
1621
2a0f61e6 1622 /* see throtl_charge_bio() */
8d2bbd4c 1623 if (bio_flagged(bio, BIO_THROTTLED) || !tg->has_rules[rw])
bc16a4f9 1624 goto out;
e43473b7
VG
1625
1626 spin_lock_irq(q->queue_lock);
c9589f03
TH
1627
1628 if (unlikely(blk_queue_bypass(q)))
bc16a4f9 1629 goto out_unlock;
f469a7b4 1630
73f0d49a
TH
1631 sq = &tg->service_queue;
1632
c79892c5 1633again:
9e660acf
TH
1634 while (true) {
1635 /* throtl is FIFO - if bios are already queued, should queue */
1636 if (sq->nr_queued[rw])
1637 break;
de701c74 1638
9e660acf 1639 /* if above limits, break to queue */
c79892c5
SL
1640 if (!tg_may_dispatch(tg, bio, NULL)) {
1641 if (throtl_can_upgrade(tg->td, tg)) {
1642 throtl_upgrade_state(tg->td);
1643 goto again;
1644 }
9e660acf 1645 break;
c79892c5 1646 }
9e660acf
TH
1647
1648 /* within limits, let's charge and dispatch directly */
e43473b7 1649 throtl_charge_bio(tg, bio);
04521db0
VG
1650
1651 /*
1652 * We need to trim slice even when bios are not being queued
1653 * otherwise it might happen that a bio is not queued for
1654 * a long time and slice keeps on extending and trim is not
1655 * called for a long time. Now if limits are reduced suddenly
1656 * we take into account all the IO dispatched so far at new
1657 * low rate and * newly queued IO gets a really long dispatch
1658 * time.
1659 *
1660 * So keep on trimming slice even if bio is not queued.
1661 */
0f3457f6 1662 throtl_trim_slice(tg, rw);
9e660acf
TH
1663
1664 /*
1665 * @bio passed through this layer without being throttled.
1666 * Climb up the ladder. If we''re already at the top, it
1667 * can be executed directly.
1668 */
c5cc2070 1669 qn = &tg->qnode_on_parent[rw];
9e660acf
TH
1670 sq = sq->parent_sq;
1671 tg = sq_to_tg(sq);
1672 if (!tg)
1673 goto out_unlock;
e43473b7
VG
1674 }
1675
9e660acf 1676 /* out-of-limit, queue to @tg */
fda6f272
TH
1677 throtl_log(sq, "[%c] bio. bdisp=%llu sz=%u bps=%llu iodisp=%u iops=%u queued=%d/%d",
1678 rw == READ ? 'R' : 'W',
9f626e37
SL
1679 tg->bytes_disp[rw], bio->bi_iter.bi_size,
1680 tg_bps_limit(tg, rw),
1681 tg->io_disp[rw], tg_iops_limit(tg, rw),
fda6f272 1682 sq->nr_queued[READ], sq->nr_queued[WRITE]);
e43473b7 1683
671058fb 1684 bio_associate_current(bio);
6bc9c2b4 1685 tg->td->nr_queued[rw]++;
c5cc2070 1686 throtl_add_bio_tg(bio, qn, tg);
bc16a4f9 1687 throttled = true;
e43473b7 1688
7f52f98c
TH
1689 /*
1690 * Update @tg's dispatch time and force schedule dispatch if @tg
1691 * was empty before @bio. The forced scheduling isn't likely to
1692 * cause undue delay as @bio is likely to be dispatched directly if
1693 * its @tg's disptime is not in the future.
1694 */
0e9f4164 1695 if (tg->flags & THROTL_TG_WAS_EMPTY) {
77216b04 1696 tg_update_disptime(tg);
7f52f98c 1697 throtl_schedule_next_dispatch(tg->service_queue.parent_sq, true);
e43473b7
VG
1698 }
1699
bc16a4f9 1700out_unlock:
e43473b7 1701 spin_unlock_irq(q->queue_lock);
bc16a4f9 1702out:
2a0f61e6
TH
1703 /*
1704 * As multiple blk-throtls may stack in the same issue path, we
1705 * don't want bios to leave with the flag set. Clear the flag if
1706 * being issued.
1707 */
1708 if (!throttled)
8d2bbd4c 1709 bio_clear_flag(bio, BIO_THROTTLED);
bc16a4f9 1710 return throttled;
e43473b7
VG
1711}
1712
2a12f0dc
TH
1713/*
1714 * Dispatch all bios from all children tg's queued on @parent_sq. On
1715 * return, @parent_sq is guaranteed to not have any active children tg's
1716 * and all bios from previously active tg's are on @parent_sq->bio_lists[].
1717 */
1718static void tg_drain_bios(struct throtl_service_queue *parent_sq)
1719{
1720 struct throtl_grp *tg;
1721
1722 while ((tg = throtl_rb_first(parent_sq))) {
1723 struct throtl_service_queue *sq = &tg->service_queue;
1724 struct bio *bio;
1725
1726 throtl_dequeue_tg(tg);
1727
c5cc2070 1728 while ((bio = throtl_peek_queued(&sq->queued[READ])))
2a12f0dc 1729 tg_dispatch_one_bio(tg, bio_data_dir(bio));
c5cc2070 1730 while ((bio = throtl_peek_queued(&sq->queued[WRITE])))
2a12f0dc
TH
1731 tg_dispatch_one_bio(tg, bio_data_dir(bio));
1732 }
1733}
1734
c9a929dd
TH
1735/**
1736 * blk_throtl_drain - drain throttled bios
1737 * @q: request_queue to drain throttled bios for
1738 *
1739 * Dispatch all currently throttled bios on @q through ->make_request_fn().
1740 */
1741void blk_throtl_drain(struct request_queue *q)
1742 __releases(q->queue_lock) __acquires(q->queue_lock)
1743{
1744 struct throtl_data *td = q->td;
2a12f0dc 1745 struct blkcg_gq *blkg;
492eb21b 1746 struct cgroup_subsys_state *pos_css;
c9a929dd 1747 struct bio *bio;
651930bc 1748 int rw;
c9a929dd 1749
8bcb6c7d 1750 queue_lockdep_assert_held(q);
2a12f0dc 1751 rcu_read_lock();
c9a929dd 1752
2a12f0dc
TH
1753 /*
1754 * Drain each tg while doing post-order walk on the blkg tree, so
1755 * that all bios are propagated to td->service_queue. It'd be
1756 * better to walk service_queue tree directly but blkg walk is
1757 * easier.
1758 */
492eb21b 1759 blkg_for_each_descendant_post(blkg, pos_css, td->queue->root_blkg)
2a12f0dc 1760 tg_drain_bios(&blkg_to_tg(blkg)->service_queue);
73f0d49a 1761
2a12f0dc
TH
1762 /* finally, transfer bios from top-level tg's into the td */
1763 tg_drain_bios(&td->service_queue);
1764
1765 rcu_read_unlock();
c9a929dd
TH
1766 spin_unlock_irq(q->queue_lock);
1767
2a12f0dc 1768 /* all bios now should be in td->service_queue, issue them */
651930bc 1769 for (rw = READ; rw <= WRITE; rw++)
c5cc2070
TH
1770 while ((bio = throtl_pop_queued(&td->service_queue.queued[rw],
1771 NULL)))
651930bc 1772 generic_make_request(bio);
c9a929dd
TH
1773
1774 spin_lock_irq(q->queue_lock);
1775}
1776
e43473b7
VG
1777int blk_throtl_init(struct request_queue *q)
1778{
1779 struct throtl_data *td;
a2b1693b 1780 int ret;
e43473b7
VG
1781
1782 td = kzalloc_node(sizeof(*td), GFP_KERNEL, q->node);
1783 if (!td)
1784 return -ENOMEM;
1785
69df0ab0 1786 INIT_WORK(&td->dispatch_work, blk_throtl_dispatch_work_fn);
b2ce2643 1787 throtl_service_queue_init(&td->service_queue);
e43473b7 1788
cd1604fa 1789 q->td = td;
29b12589 1790 td->queue = q;
02977e4a 1791
9f626e37 1792 td->limit_valid[LIMIT_MAX] = true;
cd5ab1b0 1793 td->limit_index = LIMIT_MAX;
a2b1693b 1794 /* activate policy */
3c798398 1795 ret = blkcg_activate_policy(q, &blkcg_policy_throtl);
a2b1693b 1796 if (ret)
f51b802c 1797 kfree(td);
a2b1693b 1798 return ret;
e43473b7
VG
1799}
1800
1801void blk_throtl_exit(struct request_queue *q)
1802{
c875f4d0 1803 BUG_ON(!q->td);
da527770 1804 throtl_shutdown_wq(q);
3c798398 1805 blkcg_deactivate_policy(q, &blkcg_policy_throtl);
c9a929dd 1806 kfree(q->td);
e43473b7
VG
1807}
1808
1809static int __init throtl_init(void)
1810{
450adcbe
VG
1811 kthrotld_workqueue = alloc_workqueue("kthrotld", WQ_MEM_RECLAIM, 0);
1812 if (!kthrotld_workqueue)
1813 panic("Failed to create kthrotld\n");
1814
3c798398 1815 return blkcg_policy_register(&blkcg_policy_throtl);
e43473b7
VG
1816}
1817
1818module_init(throtl_init);