blk-throttle: rename throtl_rb_root to throtl_service_queue
[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
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
c9e0332e
TH
29struct throtl_service_queue {
30 struct rb_root pending_tree; /* RB tree of active tgs */
31 struct rb_node *first_pending; /* first node in the tree */
32 unsigned int nr_pending; /* # queued in the tree */
33 unsigned long first_pending_disptime; /* disptime of the first tg */
e43473b7
VG
34};
35
c9e0332e
TH
36#define THROTL_SERVICE_QUEUE_INITIALIZER \
37 (struct throtl_service_queue){ .pending_tree = RB_ROOT }
e43473b7
VG
38
39#define rb_entry_tg(node) rb_entry((node), struct throtl_grp, rb_node)
40
8a3d2615
TH
41/* Per-cpu group stats */
42struct tg_stats_cpu {
43 /* total bytes transferred */
44 struct blkg_rwstat service_bytes;
45 /* total IOs serviced, post merge */
46 struct blkg_rwstat serviced;
47};
48
e43473b7 49struct throtl_grp {
f95a04af
TH
50 /* must be the first member */
51 struct blkg_policy_data pd;
52
c9e0332e 53 /* active throtl group service_queue member */
e43473b7
VG
54 struct rb_node rb_node;
55
56 /*
57 * Dispatch time in jiffies. This is the estimated time when group
58 * will unthrottle and is ready to dispatch more bio. It is used as
59 * key to sort active groups in service tree.
60 */
61 unsigned long disptime;
62
e43473b7
VG
63 unsigned int flags;
64
65 /* Two lists for READ and WRITE */
66 struct bio_list bio_lists[2];
67
68 /* Number of queued bios on READ and WRITE lists */
69 unsigned int nr_queued[2];
70
71 /* bytes per second rate limits */
72 uint64_t bps[2];
73
8e89d13f
VG
74 /* IOPS limits */
75 unsigned int iops[2];
76
e43473b7
VG
77 /* Number of bytes disptached in current slice */
78 uint64_t bytes_disp[2];
8e89d13f
VG
79 /* Number of bio's dispatched in current slice */
80 unsigned int io_disp[2];
e43473b7
VG
81
82 /* When did we start a new slice */
83 unsigned long slice_start[2];
84 unsigned long slice_end[2];
fe071437 85
8a3d2615
TH
86 /* Per cpu stats pointer */
87 struct tg_stats_cpu __percpu *stats_cpu;
88
89 /* List of tgs waiting for per cpu stats memory to be allocated */
90 struct list_head stats_alloc_node;
e43473b7
VG
91};
92
93struct throtl_data
94{
e43473b7 95 /* service tree for active throtl groups */
c9e0332e 96 struct throtl_service_queue service_queue;
e43473b7 97
e43473b7
VG
98 struct request_queue *queue;
99
100 /* Total Number of queued bios on READ and WRITE lists */
101 unsigned int nr_queued[2];
102
103 /*
02977e4a 104 * number of total undestroyed groups
e43473b7
VG
105 */
106 unsigned int nr_undestroyed_grps;
107
108 /* Work for dispatching throttled bios */
cb76199c 109 struct delayed_work dispatch_work;
e43473b7
VG
110};
111
8a3d2615
TH
112/* list and work item to allocate percpu group stats */
113static DEFINE_SPINLOCK(tg_stats_alloc_lock);
114static LIST_HEAD(tg_stats_alloc_list);
115
116static void tg_stats_alloc_fn(struct work_struct *);
117static DECLARE_DELAYED_WORK(tg_stats_alloc_work, tg_stats_alloc_fn);
118
f95a04af
TH
119static inline struct throtl_grp *pd_to_tg(struct blkg_policy_data *pd)
120{
121 return pd ? container_of(pd, struct throtl_grp, pd) : NULL;
122}
123
3c798398 124static inline struct throtl_grp *blkg_to_tg(struct blkcg_gq *blkg)
0381411e 125{
f95a04af 126 return pd_to_tg(blkg_to_pd(blkg, &blkcg_policy_throtl));
0381411e
TH
127}
128
3c798398 129static inline struct blkcg_gq *tg_to_blkg(struct throtl_grp *tg)
0381411e 130{
f95a04af 131 return pd_to_blkg(&tg->pd);
0381411e
TH
132}
133
03d8e111
TH
134static inline struct throtl_grp *td_root_tg(struct throtl_data *td)
135{
136 return blkg_to_tg(td->queue->root_blkg);
137}
138
e43473b7
VG
139enum tg_state_flags {
140 THROTL_TG_FLAG_on_rr = 0, /* on round-robin busy list */
141};
142
143#define THROTL_TG_FNS(name) \
144static inline void throtl_mark_tg_##name(struct throtl_grp *tg) \
145{ \
146 (tg)->flags |= (1 << THROTL_TG_FLAG_##name); \
147} \
148static inline void throtl_clear_tg_##name(struct throtl_grp *tg) \
149{ \
150 (tg)->flags &= ~(1 << THROTL_TG_FLAG_##name); \
151} \
152static inline int throtl_tg_##name(const struct throtl_grp *tg) \
153{ \
154 return ((tg)->flags & (1 << THROTL_TG_FLAG_##name)) != 0; \
155}
156
157THROTL_TG_FNS(on_rr);
158
54e7ed12
TH
159#define throtl_log_tg(td, tg, fmt, args...) do { \
160 char __pbuf[128]; \
161 \
162 blkg_path(tg_to_blkg(tg), __pbuf, sizeof(__pbuf)); \
163 blk_add_trace_msg((td)->queue, "throtl %s " fmt, __pbuf, ##args); \
164} while (0)
e43473b7
VG
165
166#define throtl_log(td, fmt, args...) \
167 blk_add_trace_msg((td)->queue, "throtl " fmt, ##args)
168
8a3d2615
TH
169/*
170 * Worker for allocating per cpu stat for tgs. This is scheduled on the
3b07e9ca 171 * system_wq once there are some groups on the alloc_list waiting for
8a3d2615
TH
172 * allocation.
173 */
174static void tg_stats_alloc_fn(struct work_struct *work)
175{
176 static struct tg_stats_cpu *stats_cpu; /* this fn is non-reentrant */
177 struct delayed_work *dwork = to_delayed_work(work);
178 bool empty = false;
179
180alloc_stats:
181 if (!stats_cpu) {
182 stats_cpu = alloc_percpu(struct tg_stats_cpu);
183 if (!stats_cpu) {
184 /* allocation failed, try again after some time */
3b07e9ca 185 schedule_delayed_work(dwork, msecs_to_jiffies(10));
8a3d2615
TH
186 return;
187 }
188 }
189
190 spin_lock_irq(&tg_stats_alloc_lock);
191
192 if (!list_empty(&tg_stats_alloc_list)) {
193 struct throtl_grp *tg = list_first_entry(&tg_stats_alloc_list,
194 struct throtl_grp,
195 stats_alloc_node);
196 swap(tg->stats_cpu, stats_cpu);
197 list_del_init(&tg->stats_alloc_node);
198 }
199
200 empty = list_empty(&tg_stats_alloc_list);
201 spin_unlock_irq(&tg_stats_alloc_lock);
202 if (!empty)
203 goto alloc_stats;
204}
205
3c798398 206static void throtl_pd_init(struct blkcg_gq *blkg)
a29a171e 207{
0381411e 208 struct throtl_grp *tg = blkg_to_tg(blkg);
ff26eaad 209 unsigned long flags;
cd1604fa 210
a29a171e
VG
211 RB_CLEAR_NODE(&tg->rb_node);
212 bio_list_init(&tg->bio_lists[0]);
213 bio_list_init(&tg->bio_lists[1]);
a29a171e 214
e56da7e2
TH
215 tg->bps[READ] = -1;
216 tg->bps[WRITE] = -1;
217 tg->iops[READ] = -1;
218 tg->iops[WRITE] = -1;
8a3d2615
TH
219
220 /*
221 * Ugh... We need to perform per-cpu allocation for tg->stats_cpu
222 * but percpu allocator can't be called from IO path. Queue tg on
223 * tg_stats_alloc_list and allocate from work item.
224 */
ff26eaad 225 spin_lock_irqsave(&tg_stats_alloc_lock, flags);
8a3d2615 226 list_add(&tg->stats_alloc_node, &tg_stats_alloc_list);
3b07e9ca 227 schedule_delayed_work(&tg_stats_alloc_work, 0);
ff26eaad 228 spin_unlock_irqrestore(&tg_stats_alloc_lock, flags);
8a3d2615
TH
229}
230
3c798398 231static void throtl_pd_exit(struct blkcg_gq *blkg)
8a3d2615
TH
232{
233 struct throtl_grp *tg = blkg_to_tg(blkg);
ff26eaad 234 unsigned long flags;
8a3d2615 235
ff26eaad 236 spin_lock_irqsave(&tg_stats_alloc_lock, flags);
8a3d2615 237 list_del_init(&tg->stats_alloc_node);
ff26eaad 238 spin_unlock_irqrestore(&tg_stats_alloc_lock, flags);
8a3d2615
TH
239
240 free_percpu(tg->stats_cpu);
241}
242
3c798398 243static void throtl_pd_reset_stats(struct blkcg_gq *blkg)
8a3d2615
TH
244{
245 struct throtl_grp *tg = blkg_to_tg(blkg);
246 int cpu;
247
248 if (tg->stats_cpu == NULL)
249 return;
250
251 for_each_possible_cpu(cpu) {
252 struct tg_stats_cpu *sc = per_cpu_ptr(tg->stats_cpu, cpu);
253
254 blkg_rwstat_reset(&sc->service_bytes);
255 blkg_rwstat_reset(&sc->serviced);
256 }
a29a171e
VG
257}
258
3c798398
TH
259static struct throtl_grp *throtl_lookup_tg(struct throtl_data *td,
260 struct blkcg *blkcg)
e43473b7 261{
be2c6b19 262 /*
3c798398
TH
263 * This is the common case when there are no blkcgs. Avoid lookup
264 * in this case
cd1604fa 265 */
3c798398 266 if (blkcg == &blkcg_root)
03d8e111 267 return td_root_tg(td);
e43473b7 268
e8989fae 269 return blkg_to_tg(blkg_lookup(blkcg, td->queue));
e43473b7
VG
270}
271
cd1604fa 272static struct throtl_grp *throtl_lookup_create_tg(struct throtl_data *td,
3c798398 273 struct blkcg *blkcg)
e43473b7 274{
f469a7b4 275 struct request_queue *q = td->queue;
cd1604fa 276 struct throtl_grp *tg = NULL;
bc16a4f9 277
f469a7b4 278 /*
3c798398
TH
279 * This is the common case when there are no blkcgs. Avoid lookup
280 * in this case
f469a7b4 281 */
3c798398 282 if (blkcg == &blkcg_root) {
03d8e111 283 tg = td_root_tg(td);
cd1604fa 284 } else {
3c798398 285 struct blkcg_gq *blkg;
f469a7b4 286
3c96cb32 287 blkg = blkg_lookup_create(blkcg, q);
f469a7b4 288
cd1604fa
TH
289 /* if %NULL and @q is alive, fall back to root_tg */
290 if (!IS_ERR(blkg))
0381411e 291 tg = blkg_to_tg(blkg);
3f3299d5 292 else if (!blk_queue_dying(q))
03d8e111 293 tg = td_root_tg(td);
f469a7b4
VG
294 }
295
e43473b7
VG
296 return tg;
297}
298
c9e0332e 299static struct throtl_grp *throtl_rb_first(struct throtl_service_queue *sq)
e43473b7
VG
300{
301 /* Service tree is empty */
c9e0332e 302 if (!sq->nr_pending)
e43473b7
VG
303 return NULL;
304
c9e0332e
TH
305 if (!sq->first_pending)
306 sq->first_pending = rb_first(&sq->pending_tree);
e43473b7 307
c9e0332e
TH
308 if (sq->first_pending)
309 return rb_entry_tg(sq->first_pending);
e43473b7
VG
310
311 return NULL;
312}
313
314static void rb_erase_init(struct rb_node *n, struct rb_root *root)
315{
316 rb_erase(n, root);
317 RB_CLEAR_NODE(n);
318}
319
c9e0332e 320static void throtl_rb_erase(struct rb_node *n, struct throtl_service_queue *sq)
e43473b7 321{
c9e0332e
TH
322 if (sq->first_pending == n)
323 sq->first_pending = NULL;
324 rb_erase_init(n, &sq->pending_tree);
325 --sq->nr_pending;
e43473b7
VG
326}
327
c9e0332e 328static void update_min_dispatch_time(struct throtl_service_queue *sq)
e43473b7
VG
329{
330 struct throtl_grp *tg;
331
c9e0332e 332 tg = throtl_rb_first(sq);
e43473b7
VG
333 if (!tg)
334 return;
335
c9e0332e 336 sq->first_pending_disptime = tg->disptime;
e43473b7
VG
337}
338
c9e0332e
TH
339static void tg_service_queue_add(struct throtl_service_queue *sq,
340 struct throtl_grp *tg)
e43473b7 341{
c9e0332e 342 struct rb_node **node = &sq->pending_tree.rb_node;
e43473b7
VG
343 struct rb_node *parent = NULL;
344 struct throtl_grp *__tg;
345 unsigned long key = tg->disptime;
346 int left = 1;
347
348 while (*node != NULL) {
349 parent = *node;
350 __tg = rb_entry_tg(parent);
351
352 if (time_before(key, __tg->disptime))
353 node = &parent->rb_left;
354 else {
355 node = &parent->rb_right;
356 left = 0;
357 }
358 }
359
360 if (left)
c9e0332e 361 sq->first_pending = &tg->rb_node;
e43473b7
VG
362
363 rb_link_node(&tg->rb_node, parent, node);
c9e0332e 364 rb_insert_color(&tg->rb_node, &sq->pending_tree);
e43473b7
VG
365}
366
367static void __throtl_enqueue_tg(struct throtl_data *td, struct throtl_grp *tg)
368{
c9e0332e 369 struct throtl_service_queue *sq = &td->service_queue;
e43473b7 370
c9e0332e 371 tg_service_queue_add(sq, tg);
e43473b7 372 throtl_mark_tg_on_rr(tg);
c9e0332e 373 sq->nr_pending++;
e43473b7
VG
374}
375
376static void throtl_enqueue_tg(struct throtl_data *td, struct throtl_grp *tg)
377{
378 if (!throtl_tg_on_rr(tg))
379 __throtl_enqueue_tg(td, tg);
380}
381
382static void __throtl_dequeue_tg(struct throtl_data *td, struct throtl_grp *tg)
383{
c9e0332e 384 throtl_rb_erase(&tg->rb_node, &td->service_queue);
e43473b7
VG
385 throtl_clear_tg_on_rr(tg);
386}
387
388static void throtl_dequeue_tg(struct throtl_data *td, struct throtl_grp *tg)
389{
390 if (throtl_tg_on_rr(tg))
391 __throtl_dequeue_tg(td, tg);
392}
393
a9131a27
TH
394/* Call with queue lock held */
395static void throtl_schedule_delayed_work(struct throtl_data *td,
396 unsigned long delay)
397{
398 struct delayed_work *dwork = &td->dispatch_work;
399
6a525600
TH
400 mod_delayed_work(kthrotld_workqueue, dwork, delay);
401 throtl_log(td, "schedule work. delay=%lu jiffies=%lu", delay, jiffies);
a9131a27
TH
402}
403
e43473b7
VG
404static void throtl_schedule_next_dispatch(struct throtl_data *td)
405{
c9e0332e 406 struct throtl_service_queue *sq = &td->service_queue;
e43473b7 407
6a525600 408 /* any pending children left? */
c9e0332e 409 if (!sq->nr_pending)
e43473b7
VG
410 return;
411
c9e0332e 412 update_min_dispatch_time(sq);
e43473b7 413
c9e0332e 414 if (time_before_eq(sq->first_pending_disptime, jiffies))
450adcbe 415 throtl_schedule_delayed_work(td, 0);
e43473b7 416 else
c9e0332e 417 throtl_schedule_delayed_work(td, sq->first_pending_disptime - jiffies);
e43473b7
VG
418}
419
420static inline void
421throtl_start_new_slice(struct throtl_data *td, struct throtl_grp *tg, bool rw)
422{
423 tg->bytes_disp[rw] = 0;
8e89d13f 424 tg->io_disp[rw] = 0;
e43473b7
VG
425 tg->slice_start[rw] = jiffies;
426 tg->slice_end[rw] = jiffies + throtl_slice;
427 throtl_log_tg(td, tg, "[%c] new slice start=%lu end=%lu jiffies=%lu",
428 rw == READ ? 'R' : 'W', tg->slice_start[rw],
429 tg->slice_end[rw], jiffies);
430}
431
d1ae8ffd
VG
432static inline void throtl_set_slice_end(struct throtl_data *td,
433 struct throtl_grp *tg, bool rw, unsigned long jiffy_end)
434{
435 tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
436}
437
e43473b7
VG
438static inline void throtl_extend_slice(struct throtl_data *td,
439 struct throtl_grp *tg, bool rw, unsigned long jiffy_end)
440{
441 tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
442 throtl_log_tg(td, tg, "[%c] extend slice start=%lu end=%lu jiffies=%lu",
443 rw == READ ? 'R' : 'W', tg->slice_start[rw],
444 tg->slice_end[rw], jiffies);
445}
446
447/* Determine if previously allocated or extended slice is complete or not */
448static bool
449throtl_slice_used(struct throtl_data *td, struct throtl_grp *tg, bool rw)
450{
451 if (time_in_range(jiffies, tg->slice_start[rw], tg->slice_end[rw]))
452 return 0;
453
454 return 1;
455}
456
457/* Trim the used slices and adjust slice start accordingly */
458static inline void
459throtl_trim_slice(struct throtl_data *td, struct throtl_grp *tg, bool rw)
460{
3aad5d3e
VG
461 unsigned long nr_slices, time_elapsed, io_trim;
462 u64 bytes_trim, tmp;
e43473b7
VG
463
464 BUG_ON(time_before(tg->slice_end[rw], tg->slice_start[rw]));
465
466 /*
467 * If bps are unlimited (-1), then time slice don't get
468 * renewed. Don't try to trim the slice if slice is used. A new
469 * slice will start when appropriate.
470 */
471 if (throtl_slice_used(td, tg, rw))
472 return;
473
d1ae8ffd
VG
474 /*
475 * A bio has been dispatched. Also adjust slice_end. It might happen
476 * that initially cgroup limit was very low resulting in high
477 * slice_end, but later limit was bumped up and bio was dispached
478 * sooner, then we need to reduce slice_end. A high bogus slice_end
479 * is bad because it does not allow new slice to start.
480 */
481
482 throtl_set_slice_end(td, tg, rw, jiffies + throtl_slice);
483
e43473b7
VG
484 time_elapsed = jiffies - tg->slice_start[rw];
485
486 nr_slices = time_elapsed / throtl_slice;
487
488 if (!nr_slices)
489 return;
3aad5d3e
VG
490 tmp = tg->bps[rw] * throtl_slice * nr_slices;
491 do_div(tmp, HZ);
492 bytes_trim = tmp;
e43473b7 493
8e89d13f 494 io_trim = (tg->iops[rw] * throtl_slice * nr_slices)/HZ;
e43473b7 495
8e89d13f 496 if (!bytes_trim && !io_trim)
e43473b7
VG
497 return;
498
499 if (tg->bytes_disp[rw] >= bytes_trim)
500 tg->bytes_disp[rw] -= bytes_trim;
501 else
502 tg->bytes_disp[rw] = 0;
503
8e89d13f
VG
504 if (tg->io_disp[rw] >= io_trim)
505 tg->io_disp[rw] -= io_trim;
506 else
507 tg->io_disp[rw] = 0;
508
e43473b7
VG
509 tg->slice_start[rw] += nr_slices * throtl_slice;
510
3aad5d3e 511 throtl_log_tg(td, tg, "[%c] trim slice nr=%lu bytes=%llu io=%lu"
e43473b7 512 " start=%lu end=%lu jiffies=%lu",
8e89d13f 513 rw == READ ? 'R' : 'W', nr_slices, bytes_trim, io_trim,
e43473b7
VG
514 tg->slice_start[rw], tg->slice_end[rw], jiffies);
515}
516
8e89d13f
VG
517static bool tg_with_in_iops_limit(struct throtl_data *td, struct throtl_grp *tg,
518 struct bio *bio, unsigned long *wait)
e43473b7
VG
519{
520 bool rw = bio_data_dir(bio);
8e89d13f 521 unsigned int io_allowed;
e43473b7 522 unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
c49c06e4 523 u64 tmp;
e43473b7 524
8e89d13f 525 jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
e43473b7 526
8e89d13f
VG
527 /* Slice has just started. Consider one slice interval */
528 if (!jiffy_elapsed)
529 jiffy_elapsed_rnd = throtl_slice;
530
531 jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
532
c49c06e4
VG
533 /*
534 * jiffy_elapsed_rnd should not be a big value as minimum iops can be
535 * 1 then at max jiffy elapsed should be equivalent of 1 second as we
536 * will allow dispatch after 1 second and after that slice should
537 * have been trimmed.
538 */
539
540 tmp = (u64)tg->iops[rw] * jiffy_elapsed_rnd;
541 do_div(tmp, HZ);
542
543 if (tmp > UINT_MAX)
544 io_allowed = UINT_MAX;
545 else
546 io_allowed = tmp;
8e89d13f
VG
547
548 if (tg->io_disp[rw] + 1 <= io_allowed) {
e43473b7
VG
549 if (wait)
550 *wait = 0;
551 return 1;
552 }
553
8e89d13f
VG
554 /* Calc approx time to dispatch */
555 jiffy_wait = ((tg->io_disp[rw] + 1) * HZ)/tg->iops[rw] + 1;
556
557 if (jiffy_wait > jiffy_elapsed)
558 jiffy_wait = jiffy_wait - jiffy_elapsed;
559 else
560 jiffy_wait = 1;
561
562 if (wait)
563 *wait = jiffy_wait;
564 return 0;
565}
566
567static bool tg_with_in_bps_limit(struct throtl_data *td, struct throtl_grp *tg,
568 struct bio *bio, unsigned long *wait)
569{
570 bool rw = bio_data_dir(bio);
3aad5d3e 571 u64 bytes_allowed, extra_bytes, tmp;
8e89d13f 572 unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
e43473b7
VG
573
574 jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
575
576 /* Slice has just started. Consider one slice interval */
577 if (!jiffy_elapsed)
578 jiffy_elapsed_rnd = throtl_slice;
579
580 jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
581
5e901a2b
VG
582 tmp = tg->bps[rw] * jiffy_elapsed_rnd;
583 do_div(tmp, HZ);
3aad5d3e 584 bytes_allowed = tmp;
e43473b7
VG
585
586 if (tg->bytes_disp[rw] + bio->bi_size <= bytes_allowed) {
587 if (wait)
588 *wait = 0;
589 return 1;
590 }
591
592 /* Calc approx time to dispatch */
593 extra_bytes = tg->bytes_disp[rw] + bio->bi_size - bytes_allowed;
594 jiffy_wait = div64_u64(extra_bytes * HZ, tg->bps[rw]);
595
596 if (!jiffy_wait)
597 jiffy_wait = 1;
598
599 /*
600 * This wait time is without taking into consideration the rounding
601 * up we did. Add that time also.
602 */
603 jiffy_wait = jiffy_wait + (jiffy_elapsed_rnd - jiffy_elapsed);
e43473b7
VG
604 if (wait)
605 *wait = jiffy_wait;
8e89d13f
VG
606 return 0;
607}
608
af75cd3c
VG
609static bool tg_no_rule_group(struct throtl_grp *tg, bool rw) {
610 if (tg->bps[rw] == -1 && tg->iops[rw] == -1)
611 return 1;
612 return 0;
613}
614
8e89d13f
VG
615/*
616 * Returns whether one can dispatch a bio or not. Also returns approx number
617 * of jiffies to wait before this bio is with-in IO rate and can be dispatched
618 */
619static bool tg_may_dispatch(struct throtl_data *td, struct throtl_grp *tg,
620 struct bio *bio, unsigned long *wait)
621{
622 bool rw = bio_data_dir(bio);
623 unsigned long bps_wait = 0, iops_wait = 0, max_wait = 0;
624
625 /*
626 * Currently whole state machine of group depends on first bio
627 * queued in the group bio list. So one should not be calling
628 * this function with a different bio if there are other bios
629 * queued.
630 */
631 BUG_ON(tg->nr_queued[rw] && bio != bio_list_peek(&tg->bio_lists[rw]));
e43473b7 632
8e89d13f
VG
633 /* If tg->bps = -1, then BW is unlimited */
634 if (tg->bps[rw] == -1 && tg->iops[rw] == -1) {
635 if (wait)
636 *wait = 0;
637 return 1;
638 }
639
640 /*
641 * If previous slice expired, start a new one otherwise renew/extend
642 * existing slice to make sure it is at least throtl_slice interval
643 * long since now.
644 */
645 if (throtl_slice_used(td, tg, rw))
646 throtl_start_new_slice(td, tg, rw);
647 else {
648 if (time_before(tg->slice_end[rw], jiffies + throtl_slice))
649 throtl_extend_slice(td, tg, rw, jiffies + throtl_slice);
650 }
651
652 if (tg_with_in_bps_limit(td, tg, bio, &bps_wait)
653 && tg_with_in_iops_limit(td, tg, bio, &iops_wait)) {
654 if (wait)
655 *wait = 0;
656 return 1;
657 }
658
659 max_wait = max(bps_wait, iops_wait);
660
661 if (wait)
662 *wait = max_wait;
663
664 if (time_before(tg->slice_end[rw], jiffies + max_wait))
665 throtl_extend_slice(td, tg, rw, jiffies + max_wait);
e43473b7
VG
666
667 return 0;
668}
669
3c798398 670static void throtl_update_dispatch_stats(struct blkcg_gq *blkg, u64 bytes,
629ed0b1
TH
671 int rw)
672{
8a3d2615
TH
673 struct throtl_grp *tg = blkg_to_tg(blkg);
674 struct tg_stats_cpu *stats_cpu;
629ed0b1
TH
675 unsigned long flags;
676
677 /* If per cpu stats are not allocated yet, don't do any accounting. */
8a3d2615 678 if (tg->stats_cpu == NULL)
629ed0b1
TH
679 return;
680
681 /*
682 * Disabling interrupts to provide mutual exclusion between two
683 * writes on same cpu. It probably is not needed for 64bit. Not
684 * optimizing that case yet.
685 */
686 local_irq_save(flags);
687
8a3d2615 688 stats_cpu = this_cpu_ptr(tg->stats_cpu);
629ed0b1 689
629ed0b1
TH
690 blkg_rwstat_add(&stats_cpu->serviced, rw, 1);
691 blkg_rwstat_add(&stats_cpu->service_bytes, rw, bytes);
692
693 local_irq_restore(flags);
694}
695
e43473b7
VG
696static void throtl_charge_bio(struct throtl_grp *tg, struct bio *bio)
697{
698 bool rw = bio_data_dir(bio);
e43473b7
VG
699
700 /* Charge the bio to the group */
701 tg->bytes_disp[rw] += bio->bi_size;
8e89d13f 702 tg->io_disp[rw]++;
e43473b7 703
629ed0b1 704 throtl_update_dispatch_stats(tg_to_blkg(tg), bio->bi_size, bio->bi_rw);
e43473b7
VG
705}
706
707static void throtl_add_bio_tg(struct throtl_data *td, struct throtl_grp *tg,
708 struct bio *bio)
709{
710 bool rw = bio_data_dir(bio);
711
712 bio_list_add(&tg->bio_lists[rw], bio);
713 /* Take a bio reference on tg */
1adaf3dd 714 blkg_get(tg_to_blkg(tg));
e43473b7
VG
715 tg->nr_queued[rw]++;
716 td->nr_queued[rw]++;
717 throtl_enqueue_tg(td, tg);
718}
719
720static void tg_update_disptime(struct throtl_data *td, struct throtl_grp *tg)
721{
722 unsigned long read_wait = -1, write_wait = -1, min_wait = -1, disptime;
723 struct bio *bio;
724
725 if ((bio = bio_list_peek(&tg->bio_lists[READ])))
726 tg_may_dispatch(td, tg, bio, &read_wait);
727
728 if ((bio = bio_list_peek(&tg->bio_lists[WRITE])))
729 tg_may_dispatch(td, tg, bio, &write_wait);
730
731 min_wait = min(read_wait, write_wait);
732 disptime = jiffies + min_wait;
733
e43473b7
VG
734 /* Update dispatch time */
735 throtl_dequeue_tg(td, tg);
736 tg->disptime = disptime;
737 throtl_enqueue_tg(td, tg);
738}
739
740static void tg_dispatch_one_bio(struct throtl_data *td, struct throtl_grp *tg,
741 bool rw, struct bio_list *bl)
742{
743 struct bio *bio;
744
745 bio = bio_list_pop(&tg->bio_lists[rw]);
746 tg->nr_queued[rw]--;
1adaf3dd
TH
747 /* Drop bio reference on blkg */
748 blkg_put(tg_to_blkg(tg));
e43473b7
VG
749
750 BUG_ON(td->nr_queued[rw] <= 0);
751 td->nr_queued[rw]--;
752
753 throtl_charge_bio(tg, bio);
754 bio_list_add(bl, bio);
755 bio->bi_rw |= REQ_THROTTLED;
756
757 throtl_trim_slice(td, tg, rw);
758}
759
760static int throtl_dispatch_tg(struct throtl_data *td, struct throtl_grp *tg,
761 struct bio_list *bl)
762{
763 unsigned int nr_reads = 0, nr_writes = 0;
764 unsigned int max_nr_reads = throtl_grp_quantum*3/4;
c2f6805d 765 unsigned int max_nr_writes = throtl_grp_quantum - max_nr_reads;
e43473b7
VG
766 struct bio *bio;
767
768 /* Try to dispatch 75% READS and 25% WRITES */
769
770 while ((bio = bio_list_peek(&tg->bio_lists[READ]))
771 && tg_may_dispatch(td, tg, bio, NULL)) {
772
773 tg_dispatch_one_bio(td, tg, bio_data_dir(bio), bl);
774 nr_reads++;
775
776 if (nr_reads >= max_nr_reads)
777 break;
778 }
779
780 while ((bio = bio_list_peek(&tg->bio_lists[WRITE]))
781 && tg_may_dispatch(td, tg, bio, NULL)) {
782
783 tg_dispatch_one_bio(td, tg, bio_data_dir(bio), bl);
784 nr_writes++;
785
786 if (nr_writes >= max_nr_writes)
787 break;
788 }
789
790 return nr_reads + nr_writes;
791}
792
793static int throtl_select_dispatch(struct throtl_data *td, struct bio_list *bl)
794{
795 unsigned int nr_disp = 0;
796 struct throtl_grp *tg;
c9e0332e 797 struct throtl_service_queue *sq = &td->service_queue;
e43473b7
VG
798
799 while (1) {
c9e0332e 800 tg = throtl_rb_first(sq);
e43473b7
VG
801
802 if (!tg)
803 break;
804
805 if (time_before(jiffies, tg->disptime))
806 break;
807
808 throtl_dequeue_tg(td, tg);
809
810 nr_disp += throtl_dispatch_tg(td, tg, bl);
811
2db6314c 812 if (tg->nr_queued[0] || tg->nr_queued[1])
e43473b7 813 tg_update_disptime(td, tg);
e43473b7
VG
814
815 if (nr_disp >= throtl_quantum)
816 break;
817 }
818
819 return nr_disp;
820}
821
cb76199c
TH
822/* work function to dispatch throttled bios */
823void blk_throtl_dispatch_work_fn(struct work_struct *work)
e43473b7 824{
cb76199c
TH
825 struct throtl_data *td = container_of(to_delayed_work(work),
826 struct throtl_data, dispatch_work);
827 struct request_queue *q = td->queue;
e43473b7
VG
828 unsigned int nr_disp = 0;
829 struct bio_list bio_list_on_stack;
830 struct bio *bio;
69d60eb9 831 struct blk_plug plug;
e43473b7
VG
832
833 spin_lock_irq(q->queue_lock);
834
e43473b7
VG
835 bio_list_init(&bio_list_on_stack);
836
d2f31a5f 837 throtl_log(td, "dispatch nr_queued=%u read=%u write=%u",
6a525600
TH
838 td->nr_queued[READ] + td->nr_queued[WRITE],
839 td->nr_queued[READ], td->nr_queued[WRITE]);
e43473b7
VG
840
841 nr_disp = throtl_select_dispatch(td, &bio_list_on_stack);
842
843 if (nr_disp)
844 throtl_log(td, "bios disp=%u", nr_disp);
845
846 throtl_schedule_next_dispatch(td);
6a525600 847
e43473b7
VG
848 spin_unlock_irq(q->queue_lock);
849
850 /*
851 * If we dispatched some requests, unplug the queue to make sure
852 * immediate dispatch
853 */
854 if (nr_disp) {
69d60eb9 855 blk_start_plug(&plug);
e43473b7
VG
856 while((bio = bio_list_pop(&bio_list_on_stack)))
857 generic_make_request(bio);
69d60eb9 858 blk_finish_plug(&plug);
e43473b7 859 }
e43473b7
VG
860}
861
f95a04af
TH
862static u64 tg_prfill_cpu_rwstat(struct seq_file *sf,
863 struct blkg_policy_data *pd, int off)
41b38b6d 864{
f95a04af 865 struct throtl_grp *tg = pd_to_tg(pd);
41b38b6d
TH
866 struct blkg_rwstat rwstat = { }, tmp;
867 int i, cpu;
868
869 for_each_possible_cpu(cpu) {
8a3d2615 870 struct tg_stats_cpu *sc = per_cpu_ptr(tg->stats_cpu, cpu);
41b38b6d
TH
871
872 tmp = blkg_rwstat_read((void *)sc + off);
873 for (i = 0; i < BLKG_RWSTAT_NR; i++)
874 rwstat.cnt[i] += tmp.cnt[i];
875 }
876
f95a04af 877 return __blkg_prfill_rwstat(sf, pd, &rwstat);
41b38b6d
TH
878}
879
8a3d2615
TH
880static int tg_print_cpu_rwstat(struct cgroup *cgrp, struct cftype *cft,
881 struct seq_file *sf)
41b38b6d 882{
3c798398 883 struct blkcg *blkcg = cgroup_to_blkcg(cgrp);
41b38b6d 884
3c798398 885 blkcg_print_blkgs(sf, blkcg, tg_prfill_cpu_rwstat, &blkcg_policy_throtl,
5bc4afb1 886 cft->private, true);
41b38b6d
TH
887 return 0;
888}
889
f95a04af
TH
890static u64 tg_prfill_conf_u64(struct seq_file *sf, struct blkg_policy_data *pd,
891 int off)
60c2bc2d 892{
f95a04af
TH
893 struct throtl_grp *tg = pd_to_tg(pd);
894 u64 v = *(u64 *)((void *)tg + off);
60c2bc2d 895
af133ceb 896 if (v == -1)
60c2bc2d 897 return 0;
f95a04af 898 return __blkg_prfill_u64(sf, pd, v);
60c2bc2d
TH
899}
900
f95a04af
TH
901static u64 tg_prfill_conf_uint(struct seq_file *sf, struct blkg_policy_data *pd,
902 int off)
e43473b7 903{
f95a04af
TH
904 struct throtl_grp *tg = pd_to_tg(pd);
905 unsigned int v = *(unsigned int *)((void *)tg + off);
fe071437 906
af133ceb
TH
907 if (v == -1)
908 return 0;
f95a04af 909 return __blkg_prfill_u64(sf, pd, v);
e43473b7
VG
910}
911
af133ceb
TH
912static int tg_print_conf_u64(struct cgroup *cgrp, struct cftype *cft,
913 struct seq_file *sf)
8e89d13f 914{
3c798398
TH
915 blkcg_print_blkgs(sf, cgroup_to_blkcg(cgrp), tg_prfill_conf_u64,
916 &blkcg_policy_throtl, cft->private, false);
af133ceb 917 return 0;
8e89d13f
VG
918}
919
af133ceb
TH
920static int tg_print_conf_uint(struct cgroup *cgrp, struct cftype *cft,
921 struct seq_file *sf)
8e89d13f 922{
3c798398
TH
923 blkcg_print_blkgs(sf, cgroup_to_blkcg(cgrp), tg_prfill_conf_uint,
924 &blkcg_policy_throtl, cft->private, false);
af133ceb 925 return 0;
60c2bc2d
TH
926}
927
af133ceb
TH
928static int tg_set_conf(struct cgroup *cgrp, struct cftype *cft, const char *buf,
929 bool is_u64)
60c2bc2d 930{
3c798398 931 struct blkcg *blkcg = cgroup_to_blkcg(cgrp);
60c2bc2d 932 struct blkg_conf_ctx ctx;
af133ceb 933 struct throtl_grp *tg;
a2b1693b 934 struct throtl_data *td;
60c2bc2d
TH
935 int ret;
936
3c798398 937 ret = blkg_conf_prep(blkcg, &blkcg_policy_throtl, buf, &ctx);
60c2bc2d
TH
938 if (ret)
939 return ret;
940
af133ceb 941 tg = blkg_to_tg(ctx.blkg);
a2b1693b 942 td = ctx.blkg->q->td;
af133ceb 943
a2b1693b
TH
944 if (!ctx.v)
945 ctx.v = -1;
af133ceb 946
a2b1693b
TH
947 if (is_u64)
948 *(u64 *)((void *)tg + cft->private) = ctx.v;
949 else
950 *(unsigned int *)((void *)tg + cft->private) = ctx.v;
af133ceb 951
632b4493
TH
952 throtl_log_tg(td, tg, "limit change rbps=%llu wbps=%llu riops=%u wiops=%u",
953 tg->bps[READ], tg->bps[WRITE],
954 tg->iops[READ], tg->iops[WRITE]);
955
956 /*
957 * We're already holding queue_lock and know @tg is valid. Let's
958 * apply the new config directly.
959 *
960 * Restart the slices for both READ and WRITES. It might happen
961 * that a group's limit are dropped suddenly and we don't want to
962 * account recently dispatched IO with new low rate.
963 */
964 throtl_start_new_slice(td, tg, 0);
965 throtl_start_new_slice(td, tg, 1);
966
967 if (throtl_tg_on_rr(tg)) {
968 tg_update_disptime(td, tg);
969 throtl_schedule_next_dispatch(td);
970 }
60c2bc2d
TH
971
972 blkg_conf_finish(&ctx);
a2b1693b 973 return 0;
8e89d13f
VG
974}
975
af133ceb
TH
976static int tg_set_conf_u64(struct cgroup *cgrp, struct cftype *cft,
977 const char *buf)
60c2bc2d 978{
af133ceb 979 return tg_set_conf(cgrp, cft, buf, true);
60c2bc2d
TH
980}
981
af133ceb
TH
982static int tg_set_conf_uint(struct cgroup *cgrp, struct cftype *cft,
983 const char *buf)
60c2bc2d 984{
af133ceb 985 return tg_set_conf(cgrp, cft, buf, false);
60c2bc2d
TH
986}
987
988static struct cftype throtl_files[] = {
989 {
990 .name = "throttle.read_bps_device",
af133ceb
TH
991 .private = offsetof(struct throtl_grp, bps[READ]),
992 .read_seq_string = tg_print_conf_u64,
993 .write_string = tg_set_conf_u64,
60c2bc2d
TH
994 .max_write_len = 256,
995 },
996 {
997 .name = "throttle.write_bps_device",
af133ceb
TH
998 .private = offsetof(struct throtl_grp, bps[WRITE]),
999 .read_seq_string = tg_print_conf_u64,
1000 .write_string = tg_set_conf_u64,
60c2bc2d
TH
1001 .max_write_len = 256,
1002 },
1003 {
1004 .name = "throttle.read_iops_device",
af133ceb
TH
1005 .private = offsetof(struct throtl_grp, iops[READ]),
1006 .read_seq_string = tg_print_conf_uint,
1007 .write_string = tg_set_conf_uint,
60c2bc2d
TH
1008 .max_write_len = 256,
1009 },
1010 {
1011 .name = "throttle.write_iops_device",
af133ceb
TH
1012 .private = offsetof(struct throtl_grp, iops[WRITE]),
1013 .read_seq_string = tg_print_conf_uint,
1014 .write_string = tg_set_conf_uint,
60c2bc2d
TH
1015 .max_write_len = 256,
1016 },
1017 {
1018 .name = "throttle.io_service_bytes",
5bc4afb1 1019 .private = offsetof(struct tg_stats_cpu, service_bytes),
8a3d2615 1020 .read_seq_string = tg_print_cpu_rwstat,
60c2bc2d
TH
1021 },
1022 {
1023 .name = "throttle.io_serviced",
5bc4afb1 1024 .private = offsetof(struct tg_stats_cpu, serviced),
8a3d2615 1025 .read_seq_string = tg_print_cpu_rwstat,
60c2bc2d
TH
1026 },
1027 { } /* terminate */
1028};
1029
da527770 1030static void throtl_shutdown_wq(struct request_queue *q)
e43473b7
VG
1031{
1032 struct throtl_data *td = q->td;
1033
cb76199c 1034 cancel_delayed_work_sync(&td->dispatch_work);
e43473b7
VG
1035}
1036
3c798398 1037static struct blkcg_policy blkcg_policy_throtl = {
f9fcc2d3
TH
1038 .pd_size = sizeof(struct throtl_grp),
1039 .cftypes = throtl_files,
1040
1041 .pd_init_fn = throtl_pd_init,
1042 .pd_exit_fn = throtl_pd_exit,
1043 .pd_reset_stats_fn = throtl_pd_reset_stats,
e43473b7
VG
1044};
1045
bc16a4f9 1046bool blk_throtl_bio(struct request_queue *q, struct bio *bio)
e43473b7
VG
1047{
1048 struct throtl_data *td = q->td;
1049 struct throtl_grp *tg;
e43473b7 1050 bool rw = bio_data_dir(bio), update_disptime = true;
3c798398 1051 struct blkcg *blkcg;
bc16a4f9 1052 bool throttled = false;
e43473b7
VG
1053
1054 if (bio->bi_rw & REQ_THROTTLED) {
1055 bio->bi_rw &= ~REQ_THROTTLED;
bc16a4f9 1056 goto out;
e43473b7
VG
1057 }
1058
af75cd3c
VG
1059 /*
1060 * A throtl_grp pointer retrieved under rcu can be used to access
1061 * basic fields like stats and io rates. If a group has no rules,
1062 * just update the dispatch stats in lockless manner and return.
1063 */
af75cd3c 1064 rcu_read_lock();
3c798398 1065 blkcg = bio_blkcg(bio);
cd1604fa 1066 tg = throtl_lookup_tg(td, blkcg);
af75cd3c 1067 if (tg) {
af75cd3c 1068 if (tg_no_rule_group(tg, rw)) {
629ed0b1
TH
1069 throtl_update_dispatch_stats(tg_to_blkg(tg),
1070 bio->bi_size, bio->bi_rw);
2a7f1244 1071 goto out_unlock_rcu;
af75cd3c
VG
1072 }
1073 }
af75cd3c
VG
1074
1075 /*
1076 * Either group has not been allocated yet or it is not an unlimited
1077 * IO group
1078 */
e43473b7 1079 spin_lock_irq(q->queue_lock);
cd1604fa 1080 tg = throtl_lookup_create_tg(td, blkcg);
bc16a4f9
TH
1081 if (unlikely(!tg))
1082 goto out_unlock;
f469a7b4 1083
e43473b7
VG
1084 if (tg->nr_queued[rw]) {
1085 /*
1086 * There is already another bio queued in same dir. No
1087 * need to update dispatch time.
1088 */
231d704b 1089 update_disptime = false;
e43473b7 1090 goto queue_bio;
de701c74 1091
e43473b7
VG
1092 }
1093
1094 /* Bio is with-in rate limit of group */
1095 if (tg_may_dispatch(td, tg, bio, NULL)) {
1096 throtl_charge_bio(tg, bio);
04521db0
VG
1097
1098 /*
1099 * We need to trim slice even when bios are not being queued
1100 * otherwise it might happen that a bio is not queued for
1101 * a long time and slice keeps on extending and trim is not
1102 * called for a long time. Now if limits are reduced suddenly
1103 * we take into account all the IO dispatched so far at new
1104 * low rate and * newly queued IO gets a really long dispatch
1105 * time.
1106 *
1107 * So keep on trimming slice even if bio is not queued.
1108 */
1109 throtl_trim_slice(td, tg, rw);
bc16a4f9 1110 goto out_unlock;
e43473b7
VG
1111 }
1112
1113queue_bio:
fd16d263 1114 throtl_log_tg(td, tg, "[%c] bio. bdisp=%llu sz=%u bps=%llu"
8e89d13f
VG
1115 " iodisp=%u iops=%u queued=%d/%d",
1116 rw == READ ? 'R' : 'W',
e43473b7 1117 tg->bytes_disp[rw], bio->bi_size, tg->bps[rw],
8e89d13f 1118 tg->io_disp[rw], tg->iops[rw],
e43473b7
VG
1119 tg->nr_queued[READ], tg->nr_queued[WRITE]);
1120
671058fb 1121 bio_associate_current(bio);
e43473b7 1122 throtl_add_bio_tg(q->td, tg, bio);
bc16a4f9 1123 throttled = true;
e43473b7
VG
1124
1125 if (update_disptime) {
1126 tg_update_disptime(td, tg);
1127 throtl_schedule_next_dispatch(td);
1128 }
1129
bc16a4f9 1130out_unlock:
e43473b7 1131 spin_unlock_irq(q->queue_lock);
2a7f1244
TH
1132out_unlock_rcu:
1133 rcu_read_unlock();
bc16a4f9
TH
1134out:
1135 return throttled;
e43473b7
VG
1136}
1137
c9a929dd
TH
1138/**
1139 * blk_throtl_drain - drain throttled bios
1140 * @q: request_queue to drain throttled bios for
1141 *
1142 * Dispatch all currently throttled bios on @q through ->make_request_fn().
1143 */
1144void blk_throtl_drain(struct request_queue *q)
1145 __releases(q->queue_lock) __acquires(q->queue_lock)
1146{
1147 struct throtl_data *td = q->td;
c9e0332e 1148 struct throtl_service_queue *sq = &td->service_queue;
c9a929dd
TH
1149 struct throtl_grp *tg;
1150 struct bio_list bl;
1151 struct bio *bio;
1152
8bcb6c7d 1153 queue_lockdep_assert_held(q);
c9a929dd
TH
1154
1155 bio_list_init(&bl);
1156
c9e0332e 1157 while ((tg = throtl_rb_first(sq))) {
c9a929dd
TH
1158 throtl_dequeue_tg(td, tg);
1159
1160 while ((bio = bio_list_peek(&tg->bio_lists[READ])))
1161 tg_dispatch_one_bio(td, tg, bio_data_dir(bio), &bl);
1162 while ((bio = bio_list_peek(&tg->bio_lists[WRITE])))
1163 tg_dispatch_one_bio(td, tg, bio_data_dir(bio), &bl);
1164 }
1165 spin_unlock_irq(q->queue_lock);
1166
1167 while ((bio = bio_list_pop(&bl)))
1168 generic_make_request(bio);
1169
1170 spin_lock_irq(q->queue_lock);
1171}
1172
e43473b7
VG
1173int blk_throtl_init(struct request_queue *q)
1174{
1175 struct throtl_data *td;
a2b1693b 1176 int ret;
e43473b7
VG
1177
1178 td = kzalloc_node(sizeof(*td), GFP_KERNEL, q->node);
1179 if (!td)
1180 return -ENOMEM;
1181
c9e0332e 1182 td->service_queue = THROTL_SERVICE_QUEUE_INITIALIZER;
cb76199c 1183 INIT_DELAYED_WORK(&td->dispatch_work, blk_throtl_dispatch_work_fn);
e43473b7 1184
cd1604fa 1185 q->td = td;
29b12589 1186 td->queue = q;
02977e4a 1187
a2b1693b 1188 /* activate policy */
3c798398 1189 ret = blkcg_activate_policy(q, &blkcg_policy_throtl);
a2b1693b 1190 if (ret)
f51b802c 1191 kfree(td);
a2b1693b 1192 return ret;
e43473b7
VG
1193}
1194
1195void blk_throtl_exit(struct request_queue *q)
1196{
c875f4d0 1197 BUG_ON(!q->td);
da527770 1198 throtl_shutdown_wq(q);
3c798398 1199 blkcg_deactivate_policy(q, &blkcg_policy_throtl);
c9a929dd 1200 kfree(q->td);
e43473b7
VG
1201}
1202
1203static int __init throtl_init(void)
1204{
450adcbe
VG
1205 kthrotld_workqueue = alloc_workqueue("kthrotld", WQ_MEM_RECLAIM, 0);
1206 if (!kthrotld_workqueue)
1207 panic("Failed to create kthrotld\n");
1208
3c798398 1209 return blkcg_policy_register(&blkcg_policy_throtl);
e43473b7
VG
1210}
1211
1212module_init(throtl_init);