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