blkcg: use the usual get blkg path for root blkio_group
[linux-2.6-block.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>
12#include "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
450adcbe
VG
24/* A workqueue to queue throttle related work */
25static struct workqueue_struct *kthrotld_workqueue;
26static void throtl_schedule_delayed_work(struct throtl_data *td,
27 unsigned long delay);
28
e43473b7
VG
29struct throtl_rb_root {
30 struct rb_root rb;
31 struct rb_node *left;
32 unsigned int count;
33 unsigned long min_disptime;
34};
35
36#define THROTL_RB_ROOT (struct throtl_rb_root) { .rb = RB_ROOT, .left = NULL, \
37 .count = 0, .min_disptime = 0}
38
39#define rb_entry_tg(node) rb_entry((node), struct throtl_grp, rb_node)
40
41struct throtl_grp {
42 /* List of throtl groups on the request queue*/
43 struct hlist_node tg_node;
44
45 /* active throtl group service_tree member */
46 struct rb_node rb_node;
47
48 /*
49 * Dispatch time in jiffies. This is the estimated time when group
50 * will unthrottle and is ready to dispatch more bio. It is used as
51 * key to sort active groups in service tree.
52 */
53 unsigned long disptime;
54
55 struct blkio_group blkg;
56 atomic_t ref;
57 unsigned int flags;
58
59 /* Two lists for READ and WRITE */
60 struct bio_list bio_lists[2];
61
62 /* Number of queued bios on READ and WRITE lists */
63 unsigned int nr_queued[2];
64
65 /* bytes per second rate limits */
66 uint64_t bps[2];
67
8e89d13f
VG
68 /* IOPS limits */
69 unsigned int iops[2];
70
e43473b7
VG
71 /* Number of bytes disptached in current slice */
72 uint64_t bytes_disp[2];
8e89d13f
VG
73 /* Number of bio's dispatched in current slice */
74 unsigned int io_disp[2];
e43473b7
VG
75
76 /* When did we start a new slice */
77 unsigned long slice_start[2];
78 unsigned long slice_end[2];
fe071437
VG
79
80 /* Some throttle limits got updated for the group */
6f037937 81 int limits_changed;
4843c69d
VG
82
83 struct rcu_head rcu_head;
e43473b7
VG
84};
85
86struct throtl_data
87{
88 /* List of throtl groups */
89 struct hlist_head tg_list;
90
91 /* service tree for active throtl groups */
92 struct throtl_rb_root tg_service_tree;
93
29b12589 94 struct throtl_grp *root_tg;
e43473b7
VG
95 struct request_queue *queue;
96
97 /* Total Number of queued bios on READ and WRITE lists */
98 unsigned int nr_queued[2];
99
100 /*
02977e4a 101 * number of total undestroyed groups
e43473b7
VG
102 */
103 unsigned int nr_undestroyed_grps;
104
105 /* Work for dispatching throttled bios */
106 struct delayed_work throtl_work;
fe071437 107
6f037937 108 int limits_changed;
e43473b7
VG
109};
110
111enum tg_state_flags {
112 THROTL_TG_FLAG_on_rr = 0, /* on round-robin busy list */
113};
114
115#define THROTL_TG_FNS(name) \
116static inline void throtl_mark_tg_##name(struct throtl_grp *tg) \
117{ \
118 (tg)->flags |= (1 << THROTL_TG_FLAG_##name); \
119} \
120static inline void throtl_clear_tg_##name(struct throtl_grp *tg) \
121{ \
122 (tg)->flags &= ~(1 << THROTL_TG_FLAG_##name); \
123} \
124static inline int throtl_tg_##name(const struct throtl_grp *tg) \
125{ \
126 return ((tg)->flags & (1 << THROTL_TG_FLAG_##name)) != 0; \
127}
128
129THROTL_TG_FNS(on_rr);
130
131#define throtl_log_tg(td, tg, fmt, args...) \
132 blk_add_trace_msg((td)->queue, "throtl %s " fmt, \
133 blkg_path(&(tg)->blkg), ##args); \
134
135#define throtl_log(td, fmt, args...) \
136 blk_add_trace_msg((td)->queue, "throtl " fmt, ##args)
137
138static inline struct throtl_grp *tg_of_blkg(struct blkio_group *blkg)
139{
140 if (blkg)
141 return container_of(blkg, struct throtl_grp, blkg);
142
143 return NULL;
144}
145
d2f31a5f 146static inline unsigned int total_nr_queued(struct throtl_data *td)
e43473b7 147{
d2f31a5f 148 return td->nr_queued[0] + td->nr_queued[1];
e43473b7
VG
149}
150
151static inline struct throtl_grp *throtl_ref_get_tg(struct throtl_grp *tg)
152{
153 atomic_inc(&tg->ref);
154 return tg;
155}
156
4843c69d
VG
157static void throtl_free_tg(struct rcu_head *head)
158{
159 struct throtl_grp *tg;
160
161 tg = container_of(head, struct throtl_grp, rcu_head);
5624a4e4 162 free_percpu(tg->blkg.stats_cpu);
4843c69d
VG
163 kfree(tg);
164}
165
e43473b7
VG
166static void throtl_put_tg(struct throtl_grp *tg)
167{
168 BUG_ON(atomic_read(&tg->ref) <= 0);
169 if (!atomic_dec_and_test(&tg->ref))
170 return;
4843c69d
VG
171
172 /*
173 * A group is freed in rcu manner. But having an rcu lock does not
174 * mean that one can access all the fields of blkg and assume these
175 * are valid. For example, don't try to follow throtl_data and
176 * request queue links.
177 *
178 * Having a reference to blkg under an rcu allows acess to only
179 * values local to groups like group stats and group rate limits
180 */
181 call_rcu(&tg->rcu_head, throtl_free_tg);
e43473b7
VG
182}
183
a29a171e
VG
184static void throtl_init_group(struct throtl_grp *tg)
185{
186 INIT_HLIST_NODE(&tg->tg_node);
187 RB_CLEAR_NODE(&tg->rb_node);
188 bio_list_init(&tg->bio_lists[0]);
189 bio_list_init(&tg->bio_lists[1]);
190 tg->limits_changed = false;
191
192 /* Practically unlimited BW */
193 tg->bps[0] = tg->bps[1] = -1;
194 tg->iops[0] = tg->iops[1] = -1;
195
196 /*
197 * Take the initial reference that will be released on destroy
198 * This can be thought of a joint reference by cgroup and
199 * request queue which will be dropped by either request queue
200 * exit or cgroup deletion path depending on who is exiting first.
201 */
202 atomic_set(&tg->ref, 1);
203}
204
205/* Should be called with rcu read lock held (needed for blkcg) */
206static void
207throtl_add_group_to_td_list(struct throtl_data *td, struct throtl_grp *tg)
208{
209 hlist_add_head(&tg->tg_node, &td->tg_list);
210 td->nr_undestroyed_grps++;
211}
212
269f5415
VG
213static void
214__throtl_tg_fill_dev_details(struct throtl_data *td, struct throtl_grp *tg)
f469a7b4
VG
215{
216 struct backing_dev_info *bdi = &td->queue->backing_dev_info;
217 unsigned int major, minor;
218
269f5415
VG
219 if (!tg || tg->blkg.dev)
220 return;
221
222 /*
223 * Fill in device details for a group which might not have been
224 * filled at group creation time as queue was being instantiated
225 * and driver had not attached a device yet
226 */
227 if (bdi->dev && dev_name(bdi->dev)) {
228 sscanf(dev_name(bdi->dev), "%u:%u", &major, &minor);
229 tg->blkg.dev = MKDEV(major, minor);
230 }
231}
232
af75cd3c
VG
233/*
234 * Should be called with without queue lock held. Here queue lock will be
235 * taken rarely. It will be taken only once during life time of a group
236 * if need be
237 */
238static void
239throtl_tg_fill_dev_details(struct throtl_data *td, struct throtl_grp *tg)
240{
241 if (!tg || tg->blkg.dev)
242 return;
243
244 spin_lock_irq(td->queue->queue_lock);
245 __throtl_tg_fill_dev_details(td, tg);
246 spin_unlock_irq(td->queue->queue_lock);
247}
248
269f5415
VG
249static void throtl_init_add_tg_lists(struct throtl_data *td,
250 struct throtl_grp *tg, struct blkio_cgroup *blkcg)
251{
252 __throtl_tg_fill_dev_details(td, tg);
253
f469a7b4 254 /* Add group onto cgroup list */
ca32aefc 255 blkiocg_add_blkio_group(blkcg, &tg->blkg, td->queue,
269f5415 256 tg->blkg.dev, BLKIO_POLICY_THROTL);
f469a7b4
VG
257
258 tg->bps[READ] = blkcg_get_read_bps(blkcg, tg->blkg.dev);
259 tg->bps[WRITE] = blkcg_get_write_bps(blkcg, tg->blkg.dev);
260 tg->iops[READ] = blkcg_get_read_iops(blkcg, tg->blkg.dev);
261 tg->iops[WRITE] = blkcg_get_write_iops(blkcg, tg->blkg.dev);
262
263 throtl_add_group_to_td_list(td, tg);
264}
265
266/* Should be called without queue lock and outside of rcu period */
267static struct throtl_grp *throtl_alloc_tg(struct throtl_data *td)
268{
269 struct throtl_grp *tg = NULL;
5624a4e4 270 int ret;
f469a7b4
VG
271
272 tg = kzalloc_node(sizeof(*tg), GFP_ATOMIC, td->queue->node);
273 if (!tg)
274 return NULL;
275
5624a4e4
VG
276 ret = blkio_alloc_blkg_stats(&tg->blkg);
277
278 if (ret) {
279 kfree(tg);
280 return NULL;
281 }
282
f469a7b4
VG
283 throtl_init_group(tg);
284 return tg;
285}
286
287static struct
288throtl_grp *throtl_find_tg(struct throtl_data *td, struct blkio_cgroup *blkcg)
e43473b7 289{
e43473b7 290 struct throtl_grp *tg = NULL;
e43473b7 291
be2c6b19
VG
292 /*
293 * This is the common case when there are no blkio cgroups.
294 * Avoid lookup in this case
295 */
296 if (blkcg == &blkio_root_cgroup)
29b12589 297 tg = td->root_tg;
be2c6b19 298 else
ca32aefc
TH
299 tg = tg_of_blkg(blkiocg_lookup_group(blkcg, td->queue,
300 BLKIO_POLICY_THROTL));
e43473b7 301
269f5415 302 __throtl_tg_fill_dev_details(td, tg);
e43473b7
VG
303 return tg;
304}
305
0a5a7d0e
TH
306static struct throtl_grp *throtl_get_tg(struct throtl_data *td,
307 struct blkio_cgroup *blkcg)
e43473b7 308{
f469a7b4 309 struct throtl_grp *tg = NULL, *__tg = NULL;
f469a7b4 310 struct request_queue *q = td->queue;
e43473b7 311
c9a929dd 312 /* no throttling for dead queue */
6ecf23af 313 if (unlikely(blk_queue_bypass(q)))
c9a929dd
TH
314 return NULL;
315
f469a7b4 316 tg = throtl_find_tg(td, blkcg);
2a7f1244 317 if (tg)
f469a7b4 318 return tg;
f469a7b4 319
0a5a7d0e
TH
320 if (!css_tryget(&blkcg->css))
321 return NULL;
322
f469a7b4
VG
323 /*
324 * Need to allocate a group. Allocation of group also needs allocation
325 * of per cpu stats which in-turn takes a mutex() and can block. Hence
315fceee 326 * we need to drop rcu lock and queue_lock before we call alloc.
f469a7b4 327 */
f469a7b4 328 spin_unlock_irq(q->queue_lock);
2a7f1244 329 rcu_read_unlock();
f469a7b4
VG
330
331 tg = throtl_alloc_tg(td);
f469a7b4
VG
332
333 /* Group allocated and queue is still alive. take the lock */
2a7f1244 334 rcu_read_lock();
f469a7b4 335 spin_lock_irq(q->queue_lock);
0a5a7d0e 336 css_put(&blkcg->css);
f469a7b4 337
bc16a4f9 338 /* Make sure @q is still alive */
6ecf23af 339 if (unlikely(blk_queue_bypass(q))) {
bc16a4f9
TH
340 kfree(tg);
341 return NULL;
342 }
343
f469a7b4
VG
344 /*
345 * If some other thread already allocated the group while we were
346 * not holding queue lock, free up the group
347 */
348 __tg = throtl_find_tg(td, blkcg);
349
350 if (__tg) {
351 kfree(tg);
f469a7b4
VG
352 return __tg;
353 }
354
355 /* Group allocation failed. Account the IO to root group */
356 if (!tg) {
29b12589 357 tg = td->root_tg;
f469a7b4
VG
358 return tg;
359 }
360
361 throtl_init_add_tg_lists(td, tg, blkcg);
e43473b7
VG
362 return tg;
363}
364
365static struct throtl_grp *throtl_rb_first(struct throtl_rb_root *root)
366{
367 /* Service tree is empty */
368 if (!root->count)
369 return NULL;
370
371 if (!root->left)
372 root->left = rb_first(&root->rb);
373
374 if (root->left)
375 return rb_entry_tg(root->left);
376
377 return NULL;
378}
379
380static void rb_erase_init(struct rb_node *n, struct rb_root *root)
381{
382 rb_erase(n, root);
383 RB_CLEAR_NODE(n);
384}
385
386static void throtl_rb_erase(struct rb_node *n, struct throtl_rb_root *root)
387{
388 if (root->left == n)
389 root->left = NULL;
390 rb_erase_init(n, &root->rb);
391 --root->count;
392}
393
394static void update_min_dispatch_time(struct throtl_rb_root *st)
395{
396 struct throtl_grp *tg;
397
398 tg = throtl_rb_first(st);
399 if (!tg)
400 return;
401
402 st->min_disptime = tg->disptime;
403}
404
405static void
406tg_service_tree_add(struct throtl_rb_root *st, struct throtl_grp *tg)
407{
408 struct rb_node **node = &st->rb.rb_node;
409 struct rb_node *parent = NULL;
410 struct throtl_grp *__tg;
411 unsigned long key = tg->disptime;
412 int left = 1;
413
414 while (*node != NULL) {
415 parent = *node;
416 __tg = rb_entry_tg(parent);
417
418 if (time_before(key, __tg->disptime))
419 node = &parent->rb_left;
420 else {
421 node = &parent->rb_right;
422 left = 0;
423 }
424 }
425
426 if (left)
427 st->left = &tg->rb_node;
428
429 rb_link_node(&tg->rb_node, parent, node);
430 rb_insert_color(&tg->rb_node, &st->rb);
431}
432
433static void __throtl_enqueue_tg(struct throtl_data *td, struct throtl_grp *tg)
434{
435 struct throtl_rb_root *st = &td->tg_service_tree;
436
437 tg_service_tree_add(st, tg);
438 throtl_mark_tg_on_rr(tg);
439 st->count++;
440}
441
442static void throtl_enqueue_tg(struct throtl_data *td, struct throtl_grp *tg)
443{
444 if (!throtl_tg_on_rr(tg))
445 __throtl_enqueue_tg(td, tg);
446}
447
448static void __throtl_dequeue_tg(struct throtl_data *td, struct throtl_grp *tg)
449{
450 throtl_rb_erase(&tg->rb_node, &td->tg_service_tree);
451 throtl_clear_tg_on_rr(tg);
452}
453
454static void throtl_dequeue_tg(struct throtl_data *td, struct throtl_grp *tg)
455{
456 if (throtl_tg_on_rr(tg))
457 __throtl_dequeue_tg(td, tg);
458}
459
460static void throtl_schedule_next_dispatch(struct throtl_data *td)
461{
462 struct throtl_rb_root *st = &td->tg_service_tree;
463
464 /*
465 * If there are more bios pending, schedule more work.
466 */
467 if (!total_nr_queued(td))
468 return;
469
470 BUG_ON(!st->count);
471
472 update_min_dispatch_time(st);
473
474 if (time_before_eq(st->min_disptime, jiffies))
450adcbe 475 throtl_schedule_delayed_work(td, 0);
e43473b7 476 else
450adcbe 477 throtl_schedule_delayed_work(td, (st->min_disptime - jiffies));
e43473b7
VG
478}
479
480static inline void
481throtl_start_new_slice(struct throtl_data *td, struct throtl_grp *tg, bool rw)
482{
483 tg->bytes_disp[rw] = 0;
8e89d13f 484 tg->io_disp[rw] = 0;
e43473b7
VG
485 tg->slice_start[rw] = jiffies;
486 tg->slice_end[rw] = jiffies + throtl_slice;
487 throtl_log_tg(td, tg, "[%c] new slice start=%lu end=%lu jiffies=%lu",
488 rw == READ ? 'R' : 'W', tg->slice_start[rw],
489 tg->slice_end[rw], jiffies);
490}
491
d1ae8ffd
VG
492static inline void throtl_set_slice_end(struct throtl_data *td,
493 struct throtl_grp *tg, bool rw, unsigned long jiffy_end)
494{
495 tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
496}
497
e43473b7
VG
498static inline void throtl_extend_slice(struct throtl_data *td,
499 struct throtl_grp *tg, bool rw, unsigned long jiffy_end)
500{
501 tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
502 throtl_log_tg(td, tg, "[%c] extend slice start=%lu end=%lu jiffies=%lu",
503 rw == READ ? 'R' : 'W', tg->slice_start[rw],
504 tg->slice_end[rw], jiffies);
505}
506
507/* Determine if previously allocated or extended slice is complete or not */
508static bool
509throtl_slice_used(struct throtl_data *td, struct throtl_grp *tg, bool rw)
510{
511 if (time_in_range(jiffies, tg->slice_start[rw], tg->slice_end[rw]))
512 return 0;
513
514 return 1;
515}
516
517/* Trim the used slices and adjust slice start accordingly */
518static inline void
519throtl_trim_slice(struct throtl_data *td, struct throtl_grp *tg, bool rw)
520{
3aad5d3e
VG
521 unsigned long nr_slices, time_elapsed, io_trim;
522 u64 bytes_trim, tmp;
e43473b7
VG
523
524 BUG_ON(time_before(tg->slice_end[rw], tg->slice_start[rw]));
525
526 /*
527 * If bps are unlimited (-1), then time slice don't get
528 * renewed. Don't try to trim the slice if slice is used. A new
529 * slice will start when appropriate.
530 */
531 if (throtl_slice_used(td, tg, rw))
532 return;
533
d1ae8ffd
VG
534 /*
535 * A bio has been dispatched. Also adjust slice_end. It might happen
536 * that initially cgroup limit was very low resulting in high
537 * slice_end, but later limit was bumped up and bio was dispached
538 * sooner, then we need to reduce slice_end. A high bogus slice_end
539 * is bad because it does not allow new slice to start.
540 */
541
542 throtl_set_slice_end(td, tg, rw, jiffies + throtl_slice);
543
e43473b7
VG
544 time_elapsed = jiffies - tg->slice_start[rw];
545
546 nr_slices = time_elapsed / throtl_slice;
547
548 if (!nr_slices)
549 return;
3aad5d3e
VG
550 tmp = tg->bps[rw] * throtl_slice * nr_slices;
551 do_div(tmp, HZ);
552 bytes_trim = tmp;
e43473b7 553
8e89d13f 554 io_trim = (tg->iops[rw] * throtl_slice * nr_slices)/HZ;
e43473b7 555
8e89d13f 556 if (!bytes_trim && !io_trim)
e43473b7
VG
557 return;
558
559 if (tg->bytes_disp[rw] >= bytes_trim)
560 tg->bytes_disp[rw] -= bytes_trim;
561 else
562 tg->bytes_disp[rw] = 0;
563
8e89d13f
VG
564 if (tg->io_disp[rw] >= io_trim)
565 tg->io_disp[rw] -= io_trim;
566 else
567 tg->io_disp[rw] = 0;
568
e43473b7
VG
569 tg->slice_start[rw] += nr_slices * throtl_slice;
570
3aad5d3e 571 throtl_log_tg(td, tg, "[%c] trim slice nr=%lu bytes=%llu io=%lu"
e43473b7 572 " start=%lu end=%lu jiffies=%lu",
8e89d13f 573 rw == READ ? 'R' : 'W', nr_slices, bytes_trim, io_trim,
e43473b7
VG
574 tg->slice_start[rw], tg->slice_end[rw], jiffies);
575}
576
8e89d13f
VG
577static bool tg_with_in_iops_limit(struct throtl_data *td, struct throtl_grp *tg,
578 struct bio *bio, unsigned long *wait)
e43473b7
VG
579{
580 bool rw = bio_data_dir(bio);
8e89d13f 581 unsigned int io_allowed;
e43473b7 582 unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
c49c06e4 583 u64 tmp;
e43473b7 584
8e89d13f 585 jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
e43473b7 586
8e89d13f
VG
587 /* Slice has just started. Consider one slice interval */
588 if (!jiffy_elapsed)
589 jiffy_elapsed_rnd = throtl_slice;
590
591 jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
592
c49c06e4
VG
593 /*
594 * jiffy_elapsed_rnd should not be a big value as minimum iops can be
595 * 1 then at max jiffy elapsed should be equivalent of 1 second as we
596 * will allow dispatch after 1 second and after that slice should
597 * have been trimmed.
598 */
599
600 tmp = (u64)tg->iops[rw] * jiffy_elapsed_rnd;
601 do_div(tmp, HZ);
602
603 if (tmp > UINT_MAX)
604 io_allowed = UINT_MAX;
605 else
606 io_allowed = tmp;
8e89d13f
VG
607
608 if (tg->io_disp[rw] + 1 <= io_allowed) {
e43473b7
VG
609 if (wait)
610 *wait = 0;
611 return 1;
612 }
613
8e89d13f
VG
614 /* Calc approx time to dispatch */
615 jiffy_wait = ((tg->io_disp[rw] + 1) * HZ)/tg->iops[rw] + 1;
616
617 if (jiffy_wait > jiffy_elapsed)
618 jiffy_wait = jiffy_wait - jiffy_elapsed;
619 else
620 jiffy_wait = 1;
621
622 if (wait)
623 *wait = jiffy_wait;
624 return 0;
625}
626
627static bool tg_with_in_bps_limit(struct throtl_data *td, struct throtl_grp *tg,
628 struct bio *bio, unsigned long *wait)
629{
630 bool rw = bio_data_dir(bio);
3aad5d3e 631 u64 bytes_allowed, extra_bytes, tmp;
8e89d13f 632 unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
e43473b7
VG
633
634 jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
635
636 /* Slice has just started. Consider one slice interval */
637 if (!jiffy_elapsed)
638 jiffy_elapsed_rnd = throtl_slice;
639
640 jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
641
5e901a2b
VG
642 tmp = tg->bps[rw] * jiffy_elapsed_rnd;
643 do_div(tmp, HZ);
3aad5d3e 644 bytes_allowed = tmp;
e43473b7
VG
645
646 if (tg->bytes_disp[rw] + bio->bi_size <= bytes_allowed) {
647 if (wait)
648 *wait = 0;
649 return 1;
650 }
651
652 /* Calc approx time to dispatch */
653 extra_bytes = tg->bytes_disp[rw] + bio->bi_size - bytes_allowed;
654 jiffy_wait = div64_u64(extra_bytes * HZ, tg->bps[rw]);
655
656 if (!jiffy_wait)
657 jiffy_wait = 1;
658
659 /*
660 * This wait time is without taking into consideration the rounding
661 * up we did. Add that time also.
662 */
663 jiffy_wait = jiffy_wait + (jiffy_elapsed_rnd - jiffy_elapsed);
e43473b7
VG
664 if (wait)
665 *wait = jiffy_wait;
8e89d13f
VG
666 return 0;
667}
668
af75cd3c
VG
669static bool tg_no_rule_group(struct throtl_grp *tg, bool rw) {
670 if (tg->bps[rw] == -1 && tg->iops[rw] == -1)
671 return 1;
672 return 0;
673}
674
8e89d13f
VG
675/*
676 * Returns whether one can dispatch a bio or not. Also returns approx number
677 * of jiffies to wait before this bio is with-in IO rate and can be dispatched
678 */
679static bool tg_may_dispatch(struct throtl_data *td, struct throtl_grp *tg,
680 struct bio *bio, unsigned long *wait)
681{
682 bool rw = bio_data_dir(bio);
683 unsigned long bps_wait = 0, iops_wait = 0, max_wait = 0;
684
685 /*
686 * Currently whole state machine of group depends on first bio
687 * queued in the group bio list. So one should not be calling
688 * this function with a different bio if there are other bios
689 * queued.
690 */
691 BUG_ON(tg->nr_queued[rw] && bio != bio_list_peek(&tg->bio_lists[rw]));
e43473b7 692
8e89d13f
VG
693 /* If tg->bps = -1, then BW is unlimited */
694 if (tg->bps[rw] == -1 && tg->iops[rw] == -1) {
695 if (wait)
696 *wait = 0;
697 return 1;
698 }
699
700 /*
701 * If previous slice expired, start a new one otherwise renew/extend
702 * existing slice to make sure it is at least throtl_slice interval
703 * long since now.
704 */
705 if (throtl_slice_used(td, tg, rw))
706 throtl_start_new_slice(td, tg, rw);
707 else {
708 if (time_before(tg->slice_end[rw], jiffies + throtl_slice))
709 throtl_extend_slice(td, tg, rw, jiffies + throtl_slice);
710 }
711
712 if (tg_with_in_bps_limit(td, tg, bio, &bps_wait)
713 && tg_with_in_iops_limit(td, tg, bio, &iops_wait)) {
714 if (wait)
715 *wait = 0;
716 return 1;
717 }
718
719 max_wait = max(bps_wait, iops_wait);
720
721 if (wait)
722 *wait = max_wait;
723
724 if (time_before(tg->slice_end[rw], jiffies + max_wait))
725 throtl_extend_slice(td, tg, rw, jiffies + max_wait);
e43473b7
VG
726
727 return 0;
728}
729
730static void throtl_charge_bio(struct throtl_grp *tg, struct bio *bio)
731{
732 bool rw = bio_data_dir(bio);
e5a94f56 733 bool sync = rw_is_sync(bio->bi_rw);
e43473b7
VG
734
735 /* Charge the bio to the group */
736 tg->bytes_disp[rw] += bio->bi_size;
8e89d13f 737 tg->io_disp[rw]++;
e43473b7 738
e43473b7 739 blkiocg_update_dispatch_stats(&tg->blkg, bio->bi_size, rw, sync);
e43473b7
VG
740}
741
742static void throtl_add_bio_tg(struct throtl_data *td, struct throtl_grp *tg,
743 struct bio *bio)
744{
745 bool rw = bio_data_dir(bio);
746
747 bio_list_add(&tg->bio_lists[rw], bio);
748 /* Take a bio reference on tg */
749 throtl_ref_get_tg(tg);
750 tg->nr_queued[rw]++;
751 td->nr_queued[rw]++;
752 throtl_enqueue_tg(td, tg);
753}
754
755static void tg_update_disptime(struct throtl_data *td, struct throtl_grp *tg)
756{
757 unsigned long read_wait = -1, write_wait = -1, min_wait = -1, disptime;
758 struct bio *bio;
759
760 if ((bio = bio_list_peek(&tg->bio_lists[READ])))
761 tg_may_dispatch(td, tg, bio, &read_wait);
762
763 if ((bio = bio_list_peek(&tg->bio_lists[WRITE])))
764 tg_may_dispatch(td, tg, bio, &write_wait);
765
766 min_wait = min(read_wait, write_wait);
767 disptime = jiffies + min_wait;
768
e43473b7
VG
769 /* Update dispatch time */
770 throtl_dequeue_tg(td, tg);
771 tg->disptime = disptime;
772 throtl_enqueue_tg(td, tg);
773}
774
775static void tg_dispatch_one_bio(struct throtl_data *td, struct throtl_grp *tg,
776 bool rw, struct bio_list *bl)
777{
778 struct bio *bio;
779
780 bio = bio_list_pop(&tg->bio_lists[rw]);
781 tg->nr_queued[rw]--;
782 /* Drop bio reference on tg */
783 throtl_put_tg(tg);
784
785 BUG_ON(td->nr_queued[rw] <= 0);
786 td->nr_queued[rw]--;
787
788 throtl_charge_bio(tg, bio);
789 bio_list_add(bl, bio);
790 bio->bi_rw |= REQ_THROTTLED;
791
792 throtl_trim_slice(td, tg, rw);
793}
794
795static int throtl_dispatch_tg(struct throtl_data *td, struct throtl_grp *tg,
796 struct bio_list *bl)
797{
798 unsigned int nr_reads = 0, nr_writes = 0;
799 unsigned int max_nr_reads = throtl_grp_quantum*3/4;
c2f6805d 800 unsigned int max_nr_writes = throtl_grp_quantum - max_nr_reads;
e43473b7
VG
801 struct bio *bio;
802
803 /* Try to dispatch 75% READS and 25% WRITES */
804
805 while ((bio = bio_list_peek(&tg->bio_lists[READ]))
806 && tg_may_dispatch(td, tg, bio, NULL)) {
807
808 tg_dispatch_one_bio(td, tg, bio_data_dir(bio), bl);
809 nr_reads++;
810
811 if (nr_reads >= max_nr_reads)
812 break;
813 }
814
815 while ((bio = bio_list_peek(&tg->bio_lists[WRITE]))
816 && tg_may_dispatch(td, tg, bio, NULL)) {
817
818 tg_dispatch_one_bio(td, tg, bio_data_dir(bio), bl);
819 nr_writes++;
820
821 if (nr_writes >= max_nr_writes)
822 break;
823 }
824
825 return nr_reads + nr_writes;
826}
827
828static int throtl_select_dispatch(struct throtl_data *td, struct bio_list *bl)
829{
830 unsigned int nr_disp = 0;
831 struct throtl_grp *tg;
832 struct throtl_rb_root *st = &td->tg_service_tree;
833
834 while (1) {
835 tg = throtl_rb_first(st);
836
837 if (!tg)
838 break;
839
840 if (time_before(jiffies, tg->disptime))
841 break;
842
843 throtl_dequeue_tg(td, tg);
844
845 nr_disp += throtl_dispatch_tg(td, tg, bl);
846
847 if (tg->nr_queued[0] || tg->nr_queued[1]) {
848 tg_update_disptime(td, tg);
849 throtl_enqueue_tg(td, tg);
850 }
851
852 if (nr_disp >= throtl_quantum)
853 break;
854 }
855
856 return nr_disp;
857}
858
fe071437
VG
859static void throtl_process_limit_change(struct throtl_data *td)
860{
861 struct throtl_grp *tg;
862 struct hlist_node *pos, *n;
863
de701c74 864 if (!td->limits_changed)
fe071437
VG
865 return;
866
de701c74 867 xchg(&td->limits_changed, false);
fe071437 868
de701c74 869 throtl_log(td, "limits changed");
fe071437 870
04a6b516 871 hlist_for_each_entry_safe(tg, pos, n, &td->tg_list, tg_node) {
de701c74
VG
872 if (!tg->limits_changed)
873 continue;
874
875 if (!xchg(&tg->limits_changed, false))
876 continue;
877
878 throtl_log_tg(td, tg, "limit change rbps=%llu wbps=%llu"
879 " riops=%u wiops=%u", tg->bps[READ], tg->bps[WRITE],
880 tg->iops[READ], tg->iops[WRITE]);
881
04521db0
VG
882 /*
883 * Restart the slices for both READ and WRITES. It
884 * might happen that a group's limit are dropped
885 * suddenly and we don't want to account recently
886 * dispatched IO with new low rate
887 */
888 throtl_start_new_slice(td, tg, 0);
889 throtl_start_new_slice(td, tg, 1);
890
de701c74 891 if (throtl_tg_on_rr(tg))
fe071437 892 tg_update_disptime(td, tg);
fe071437 893 }
fe071437
VG
894}
895
e43473b7
VG
896/* Dispatch throttled bios. Should be called without queue lock held. */
897static int throtl_dispatch(struct request_queue *q)
898{
899 struct throtl_data *td = q->td;
900 unsigned int nr_disp = 0;
901 struct bio_list bio_list_on_stack;
902 struct bio *bio;
69d60eb9 903 struct blk_plug plug;
e43473b7
VG
904
905 spin_lock_irq(q->queue_lock);
906
fe071437
VG
907 throtl_process_limit_change(td);
908
e43473b7
VG
909 if (!total_nr_queued(td))
910 goto out;
911
912 bio_list_init(&bio_list_on_stack);
913
d2f31a5f 914 throtl_log(td, "dispatch nr_queued=%u read=%u write=%u",
e43473b7
VG
915 total_nr_queued(td), td->nr_queued[READ],
916 td->nr_queued[WRITE]);
917
918 nr_disp = throtl_select_dispatch(td, &bio_list_on_stack);
919
920 if (nr_disp)
921 throtl_log(td, "bios disp=%u", nr_disp);
922
923 throtl_schedule_next_dispatch(td);
924out:
925 spin_unlock_irq(q->queue_lock);
926
927 /*
928 * If we dispatched some requests, unplug the queue to make sure
929 * immediate dispatch
930 */
931 if (nr_disp) {
69d60eb9 932 blk_start_plug(&plug);
e43473b7
VG
933 while((bio = bio_list_pop(&bio_list_on_stack)))
934 generic_make_request(bio);
69d60eb9 935 blk_finish_plug(&plug);
e43473b7
VG
936 }
937 return nr_disp;
938}
939
940void blk_throtl_work(struct work_struct *work)
941{
942 struct throtl_data *td = container_of(work, struct throtl_data,
943 throtl_work.work);
944 struct request_queue *q = td->queue;
945
946 throtl_dispatch(q);
947}
948
949/* Call with queue lock held */
450adcbe
VG
950static void
951throtl_schedule_delayed_work(struct throtl_data *td, unsigned long delay)
e43473b7
VG
952{
953
e43473b7
VG
954 struct delayed_work *dwork = &td->throtl_work;
955
04521db0 956 /* schedule work if limits changed even if no bio is queued */
d2f31a5f 957 if (total_nr_queued(td) || td->limits_changed) {
e43473b7
VG
958 /*
959 * We might have a work scheduled to be executed in future.
960 * Cancel that and schedule a new one.
961 */
962 __cancel_delayed_work(dwork);
450adcbe 963 queue_delayed_work(kthrotld_workqueue, dwork, delay);
e43473b7
VG
964 throtl_log(td, "schedule work. delay=%lu jiffies=%lu",
965 delay, jiffies);
966 }
967}
e43473b7
VG
968
969static void
970throtl_destroy_tg(struct throtl_data *td, struct throtl_grp *tg)
971{
972 /* Something wrong if we are trying to remove same group twice */
973 BUG_ON(hlist_unhashed(&tg->tg_node));
974
975 hlist_del_init(&tg->tg_node);
976
977 /*
978 * Put the reference taken at the time of creation so that when all
979 * queues are gone, group can be destroyed.
980 */
981 throtl_put_tg(tg);
982 td->nr_undestroyed_grps--;
983}
984
72e06c25 985static bool throtl_release_tgs(struct throtl_data *td, bool release_root)
e43473b7
VG
986{
987 struct hlist_node *pos, *n;
988 struct throtl_grp *tg;
72e06c25 989 bool empty = true;
e43473b7
VG
990
991 hlist_for_each_entry_safe(tg, pos, n, &td->tg_list, tg_node) {
72e06c25
TH
992 /* skip root? */
993 if (!release_root && tg == td->root_tg)
994 continue;
995
e43473b7
VG
996 /*
997 * If cgroup removal path got to blk_group first and removed
998 * it from cgroup list, then it will take care of destroying
999 * cfqg also.
1000 */
1001 if (!blkiocg_del_blkio_group(&tg->blkg))
1002 throtl_destroy_tg(td, tg);
72e06c25
TH
1003 else
1004 empty = false;
e43473b7 1005 }
72e06c25 1006 return empty;
e43473b7
VG
1007}
1008
e43473b7
VG
1009/*
1010 * Blk cgroup controller notification saying that blkio_group object is being
1011 * delinked as associated cgroup object is going away. That also means that
1012 * no new IO will come in this group. So get rid of this group as soon as
1013 * any pending IO in the group is finished.
1014 *
ca32aefc
TH
1015 * This function is called under rcu_read_lock(). @q is the rcu protected
1016 * pointer. That means @q is a valid request_queue pointer as long as we
1017 * are rcu read lock.
e43473b7 1018 *
ca32aefc 1019 * @q was fetched from blkio_group under blkio_cgroup->lock. That means
e43473b7
VG
1020 * it should not be NULL as even if queue was going away, cgroup deltion
1021 * path got to it first.
1022 */
ca32aefc
TH
1023void throtl_unlink_blkio_group(struct request_queue *q,
1024 struct blkio_group *blkg)
e43473b7
VG
1025{
1026 unsigned long flags;
e43473b7 1027
ca32aefc
TH
1028 spin_lock_irqsave(q->queue_lock, flags);
1029 throtl_destroy_tg(q->td, tg_of_blkg(blkg));
1030 spin_unlock_irqrestore(q->queue_lock, flags);
e43473b7
VG
1031}
1032
72e06c25
TH
1033static bool throtl_clear_queue(struct request_queue *q)
1034{
1035 lockdep_assert_held(q->queue_lock);
1036
1037 /*
1038 * Clear tgs but leave the root one alone. This is necessary
1039 * because root_tg is expected to be persistent and safe because
1040 * blk-throtl can never be disabled while @q is alive. This is a
1041 * kludge to prepare for unified blkg. This whole function will be
1042 * removed soon.
1043 */
1044 return throtl_release_tgs(q->td, false);
1045}
1046
de701c74
VG
1047static void throtl_update_blkio_group_common(struct throtl_data *td,
1048 struct throtl_grp *tg)
1049{
1050 xchg(&tg->limits_changed, true);
1051 xchg(&td->limits_changed, true);
1052 /* Schedule a work now to process the limit change */
1053 throtl_schedule_delayed_work(td, 0);
1054}
1055
fe071437 1056/*
ca32aefc 1057 * For all update functions, @q should be a valid pointer because these
fe071437 1058 * update functions are called under blkcg_lock, that means, blkg is
ca32aefc 1059 * valid and in turn @q is valid. queue exit path can not race because
fe071437
VG
1060 * of blkcg_lock
1061 *
1062 * Can not take queue lock in update functions as queue lock under blkcg_lock
1063 * is not allowed. Under other paths we take blkcg_lock under queue_lock.
1064 */
ca32aefc 1065static void throtl_update_blkio_group_read_bps(struct request_queue *q,
fe071437 1066 struct blkio_group *blkg, u64 read_bps)
e43473b7 1067{
de701c74 1068 struct throtl_grp *tg = tg_of_blkg(blkg);
fe071437 1069
de701c74 1070 tg->bps[READ] = read_bps;
ca32aefc 1071 throtl_update_blkio_group_common(q->td, tg);
e43473b7
VG
1072}
1073
ca32aefc 1074static void throtl_update_blkio_group_write_bps(struct request_queue *q,
fe071437 1075 struct blkio_group *blkg, u64 write_bps)
e43473b7 1076{
de701c74 1077 struct throtl_grp *tg = tg_of_blkg(blkg);
fe071437 1078
de701c74 1079 tg->bps[WRITE] = write_bps;
ca32aefc 1080 throtl_update_blkio_group_common(q->td, tg);
e43473b7
VG
1081}
1082
ca32aefc 1083static void throtl_update_blkio_group_read_iops(struct request_queue *q,
fe071437 1084 struct blkio_group *blkg, unsigned int read_iops)
8e89d13f 1085{
de701c74 1086 struct throtl_grp *tg = tg_of_blkg(blkg);
fe071437 1087
de701c74 1088 tg->iops[READ] = read_iops;
ca32aefc 1089 throtl_update_blkio_group_common(q->td, tg);
8e89d13f
VG
1090}
1091
ca32aefc 1092static void throtl_update_blkio_group_write_iops(struct request_queue *q,
fe071437 1093 struct blkio_group *blkg, unsigned int write_iops)
8e89d13f 1094{
de701c74 1095 struct throtl_grp *tg = tg_of_blkg(blkg);
fe071437 1096
de701c74 1097 tg->iops[WRITE] = write_iops;
ca32aefc 1098 throtl_update_blkio_group_common(q->td, tg);
8e89d13f
VG
1099}
1100
da527770 1101static void throtl_shutdown_wq(struct request_queue *q)
e43473b7
VG
1102{
1103 struct throtl_data *td = q->td;
1104
1105 cancel_delayed_work_sync(&td->throtl_work);
1106}
1107
1108static struct blkio_policy_type blkio_policy_throtl = {
1109 .ops = {
1110 .blkio_unlink_group_fn = throtl_unlink_blkio_group,
72e06c25 1111 .blkio_clear_queue_fn = throtl_clear_queue,
e43473b7
VG
1112 .blkio_update_group_read_bps_fn =
1113 throtl_update_blkio_group_read_bps,
1114 .blkio_update_group_write_bps_fn =
1115 throtl_update_blkio_group_write_bps,
8e89d13f
VG
1116 .blkio_update_group_read_iops_fn =
1117 throtl_update_blkio_group_read_iops,
1118 .blkio_update_group_write_iops_fn =
1119 throtl_update_blkio_group_write_iops,
e43473b7 1120 },
8e89d13f 1121 .plid = BLKIO_POLICY_THROTL,
e43473b7
VG
1122};
1123
bc16a4f9 1124bool blk_throtl_bio(struct request_queue *q, struct bio *bio)
e43473b7
VG
1125{
1126 struct throtl_data *td = q->td;
1127 struct throtl_grp *tg;
e43473b7 1128 bool rw = bio_data_dir(bio), update_disptime = true;
af75cd3c 1129 struct blkio_cgroup *blkcg;
bc16a4f9 1130 bool throttled = false;
e43473b7
VG
1131
1132 if (bio->bi_rw & REQ_THROTTLED) {
1133 bio->bi_rw &= ~REQ_THROTTLED;
bc16a4f9 1134 goto out;
e43473b7
VG
1135 }
1136
af75cd3c
VG
1137 /*
1138 * A throtl_grp pointer retrieved under rcu can be used to access
1139 * basic fields like stats and io rates. If a group has no rules,
1140 * just update the dispatch stats in lockless manner and return.
1141 */
af75cd3c
VG
1142 rcu_read_lock();
1143 blkcg = task_blkio_cgroup(current);
1144 tg = throtl_find_tg(td, blkcg);
1145 if (tg) {
1146 throtl_tg_fill_dev_details(td, tg);
1147
1148 if (tg_no_rule_group(tg, rw)) {
1149 blkiocg_update_dispatch_stats(&tg->blkg, bio->bi_size,
e5a94f56 1150 rw, rw_is_sync(bio->bi_rw));
2a7f1244 1151 goto out_unlock_rcu;
af75cd3c
VG
1152 }
1153 }
af75cd3c
VG
1154
1155 /*
1156 * Either group has not been allocated yet or it is not an unlimited
1157 * IO group
1158 */
e43473b7 1159 spin_lock_irq(q->queue_lock);
0a5a7d0e 1160 tg = throtl_get_tg(td, blkcg);
bc16a4f9
TH
1161 if (unlikely(!tg))
1162 goto out_unlock;
f469a7b4 1163
e43473b7
VG
1164 if (tg->nr_queued[rw]) {
1165 /*
1166 * There is already another bio queued in same dir. No
1167 * need to update dispatch time.
1168 */
231d704b 1169 update_disptime = false;
e43473b7 1170 goto queue_bio;
de701c74 1171
e43473b7
VG
1172 }
1173
1174 /* Bio is with-in rate limit of group */
1175 if (tg_may_dispatch(td, tg, bio, NULL)) {
1176 throtl_charge_bio(tg, bio);
04521db0
VG
1177
1178 /*
1179 * We need to trim slice even when bios are not being queued
1180 * otherwise it might happen that a bio is not queued for
1181 * a long time and slice keeps on extending and trim is not
1182 * called for a long time. Now if limits are reduced suddenly
1183 * we take into account all the IO dispatched so far at new
1184 * low rate and * newly queued IO gets a really long dispatch
1185 * time.
1186 *
1187 * So keep on trimming slice even if bio is not queued.
1188 */
1189 throtl_trim_slice(td, tg, rw);
bc16a4f9 1190 goto out_unlock;
e43473b7
VG
1191 }
1192
1193queue_bio:
fd16d263 1194 throtl_log_tg(td, tg, "[%c] bio. bdisp=%llu sz=%u bps=%llu"
8e89d13f
VG
1195 " iodisp=%u iops=%u queued=%d/%d",
1196 rw == READ ? 'R' : 'W',
e43473b7 1197 tg->bytes_disp[rw], bio->bi_size, tg->bps[rw],
8e89d13f 1198 tg->io_disp[rw], tg->iops[rw],
e43473b7
VG
1199 tg->nr_queued[READ], tg->nr_queued[WRITE]);
1200
1201 throtl_add_bio_tg(q->td, tg, bio);
bc16a4f9 1202 throttled = true;
e43473b7
VG
1203
1204 if (update_disptime) {
1205 tg_update_disptime(td, tg);
1206 throtl_schedule_next_dispatch(td);
1207 }
1208
bc16a4f9 1209out_unlock:
e43473b7 1210 spin_unlock_irq(q->queue_lock);
2a7f1244
TH
1211out_unlock_rcu:
1212 rcu_read_unlock();
bc16a4f9
TH
1213out:
1214 return throttled;
e43473b7
VG
1215}
1216
c9a929dd
TH
1217/**
1218 * blk_throtl_drain - drain throttled bios
1219 * @q: request_queue to drain throttled bios for
1220 *
1221 * Dispatch all currently throttled bios on @q through ->make_request_fn().
1222 */
1223void blk_throtl_drain(struct request_queue *q)
1224 __releases(q->queue_lock) __acquires(q->queue_lock)
1225{
1226 struct throtl_data *td = q->td;
1227 struct throtl_rb_root *st = &td->tg_service_tree;
1228 struct throtl_grp *tg;
1229 struct bio_list bl;
1230 struct bio *bio;
1231
334c2b0b 1232 WARN_ON_ONCE(!queue_is_locked(q));
c9a929dd
TH
1233
1234 bio_list_init(&bl);
1235
1236 while ((tg = throtl_rb_first(st))) {
1237 throtl_dequeue_tg(td, tg);
1238
1239 while ((bio = bio_list_peek(&tg->bio_lists[READ])))
1240 tg_dispatch_one_bio(td, tg, bio_data_dir(bio), &bl);
1241 while ((bio = bio_list_peek(&tg->bio_lists[WRITE])))
1242 tg_dispatch_one_bio(td, tg, bio_data_dir(bio), &bl);
1243 }
1244 spin_unlock_irq(q->queue_lock);
1245
1246 while ((bio = bio_list_pop(&bl)))
1247 generic_make_request(bio);
1248
1249 spin_lock_irq(q->queue_lock);
1250}
1251
e43473b7
VG
1252int blk_throtl_init(struct request_queue *q)
1253{
1254 struct throtl_data *td;
e43473b7
VG
1255
1256 td = kzalloc_node(sizeof(*td), GFP_KERNEL, q->node);
1257 if (!td)
1258 return -ENOMEM;
1259
1260 INIT_HLIST_HEAD(&td->tg_list);
1261 td->tg_service_tree = THROTL_RB_ROOT;
de701c74 1262 td->limits_changed = false;
a29a171e 1263 INIT_DELAYED_WORK(&td->throtl_work, blk_throtl_work);
e43473b7 1264
29b12589
VG
1265 /* alloc and Init root group. */
1266 td->queue = q;
02977e4a 1267
f51b802c
TH
1268 rcu_read_lock();
1269 spin_lock_irq(q->queue_lock);
29b12589 1270
f51b802c 1271 td->root_tg = throtl_get_tg(td, &blkio_root_cgroup);
e43473b7 1272
f51b802c 1273 spin_unlock_irq(q->queue_lock);
e43473b7
VG
1274 rcu_read_unlock();
1275
f51b802c
TH
1276 if (!td->root_tg) {
1277 kfree(td);
1278 return -ENOMEM;
1279 }
1280
e43473b7 1281 /* Attach throtl data to request queue */
e43473b7
VG
1282 q->td = td;
1283 return 0;
1284}
1285
1286void blk_throtl_exit(struct request_queue *q)
1287{
1288 struct throtl_data *td = q->td;
1289 bool wait = false;
1290
1291 BUG_ON(!td);
1292
da527770 1293 throtl_shutdown_wq(q);
e43473b7
VG
1294
1295 spin_lock_irq(q->queue_lock);
72e06c25 1296 throtl_release_tgs(td, true);
e43473b7
VG
1297
1298 /* If there are other groups */
02977e4a 1299 if (td->nr_undestroyed_grps > 0)
e43473b7
VG
1300 wait = true;
1301
1302 spin_unlock_irq(q->queue_lock);
1303
1304 /*
ca32aefc 1305 * Wait for tg->blkg->q accessors to exit their grace periods.
e43473b7
VG
1306 * Do this wait only if there are other undestroyed groups out
1307 * there (other than root group). This can happen if cgroup deletion
1308 * path claimed the responsibility of cleaning up a group before
1309 * queue cleanup code get to the group.
1310 *
1311 * Do not call synchronize_rcu() unconditionally as there are drivers
1312 * which create/delete request queue hundreds of times during scan/boot
1313 * and synchronize_rcu() can take significant time and slow down boot.
1314 */
1315 if (wait)
1316 synchronize_rcu();
fe071437
VG
1317
1318 /*
1319 * Just being safe to make sure after previous flush if some body did
1320 * update limits through cgroup and another work got queued, cancel
1321 * it.
1322 */
da527770 1323 throtl_shutdown_wq(q);
c9a929dd
TH
1324}
1325
1326void blk_throtl_release(struct request_queue *q)
1327{
1328 kfree(q->td);
e43473b7
VG
1329}
1330
1331static int __init throtl_init(void)
1332{
450adcbe
VG
1333 kthrotld_workqueue = alloc_workqueue("kthrotld", WQ_MEM_RECLAIM, 0);
1334 if (!kthrotld_workqueue)
1335 panic("Failed to create kthrotld\n");
1336
e43473b7
VG
1337 blkio_policy_register(&blkio_policy_throtl);
1338 return 0;
1339}
1340
1341module_init(throtl_init);