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