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