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