1
2
3
4
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"
13
14
15static int throtl_grp_quantum = 8;
16
17
18static int throtl_quantum = 32;
19
20
21static unsigned long throtl_slice = HZ/10;
22
23
24static struct workqueue_struct *kthrotld_workqueue;
25static void throtl_schedule_delayed_work(struct throtl_data *td,
26 unsigned long delay);
27
28struct throtl_rb_root {
29 struct rb_root rb;
30 struct rb_node *left;
31 unsigned int count;
32 unsigned long min_disptime;
33};
34
35#define THROTL_RB_ROOT (struct throtl_rb_root) { .rb = RB_ROOT, .left = NULL, \
36 .count = 0, .min_disptime = 0}
37
38#define rb_entry_tg(node) rb_entry((node), struct throtl_grp, rb_node)
39
40struct throtl_grp {
41
42 struct hlist_node tg_node;
43
44
45 struct rb_node rb_node;
46
47
48
49
50
51
52 unsigned long disptime;
53
54 struct blkio_group blkg;
55 atomic_t ref;
56 unsigned int flags;
57
58
59 struct bio_list bio_lists[2];
60
61
62 unsigned int nr_queued[2];
63
64
65 uint64_t bps[2];
66
67
68 unsigned int iops[2];
69
70
71 uint64_t bytes_disp[2];
72
73 unsigned int io_disp[2];
74
75
76 unsigned long slice_start[2];
77 unsigned long slice_end[2];
78
79
80 int limits_changed;
81};
82
83struct throtl_data
84{
85
86 struct hlist_head tg_list;
87
88
89 struct throtl_rb_root tg_service_tree;
90
91 struct throtl_grp root_tg;
92 struct request_queue *queue;
93
94
95 unsigned int nr_queued[2];
96
97
98
99
100 unsigned int nr_undestroyed_grps;
101
102
103 struct delayed_work throtl_work;
104
105 int limits_changed;
106};
107
108enum tg_state_flags {
109 THROTL_TG_FLAG_on_rr = 0,
110};
111
112#define THROTL_TG_FNS(name) \
113static inline void throtl_mark_tg_##name(struct throtl_grp *tg) \
114{ \
115 (tg)->flags |= (1 << THROTL_TG_FLAG_##name); \
116} \
117static inline void throtl_clear_tg_##name(struct throtl_grp *tg) \
118{ \
119 (tg)->flags &= ~(1 << THROTL_TG_FLAG_##name); \
120} \
121static inline int throtl_tg_##name(const struct throtl_grp *tg) \
122{ \
123 return ((tg)->flags & (1 << THROTL_TG_FLAG_##name)) != 0; \
124}
125
126THROTL_TG_FNS(on_rr);
127
128#define throtl_log_tg(td, tg, fmt, args...) \
129 blk_add_trace_msg((td)->queue, "throtl %s " fmt, \
130 blkg_path(&(tg)->blkg), ##args); \
131
132#define throtl_log(td, fmt, args...) \
133 blk_add_trace_msg((td)->queue, "throtl " fmt, ##args)
134
135static inline struct throtl_grp *tg_of_blkg(struct blkio_group *blkg)
136{
137 if (blkg)
138 return container_of(blkg, struct throtl_grp, blkg);
139
140 return NULL;
141}
142
143static inline int total_nr_queued(struct throtl_data *td)
144{
145 return (td->nr_queued[0] + td->nr_queued[1]);
146}
147
148static inline struct throtl_grp *throtl_ref_get_tg(struct throtl_grp *tg)
149{
150 atomic_inc(&tg->ref);
151 return tg;
152}
153
154static void throtl_put_tg(struct throtl_grp *tg)
155{
156 BUG_ON(atomic_read(&tg->ref) <= 0);
157 if (!atomic_dec_and_test(&tg->ref))
158 return;
159 kfree(tg);
160}
161
162static struct throtl_grp * throtl_find_alloc_tg(struct throtl_data *td,
163 struct blkio_cgroup *blkcg)
164{
165 struct throtl_grp *tg = NULL;
166 void *key = td;
167 struct backing_dev_info *bdi = &td->queue->backing_dev_info;
168 unsigned int major, minor;
169
170
171
172
173
174
175
176
177
178
179
180 if (blkcg == &blkio_root_cgroup)
181 tg = &td->root_tg;
182 else
183 tg = tg_of_blkg(blkiocg_lookup_group(blkcg, key));
184
185
186 if (tg && !tg->blkg.dev && bdi->dev && dev_name(bdi->dev)) {
187 sscanf(dev_name(bdi->dev), "%u:%u", &major, &minor);
188 tg->blkg.dev = MKDEV(major, minor);
189 goto done;
190 }
191
192 if (tg)
193 goto done;
194
195 tg = kzalloc_node(sizeof(*tg), GFP_ATOMIC, td->queue->node);
196 if (!tg)
197 goto done;
198
199 INIT_HLIST_NODE(&tg->tg_node);
200 RB_CLEAR_NODE(&tg->rb_node);
201 bio_list_init(&tg->bio_lists[0]);
202 bio_list_init(&tg->bio_lists[1]);
203 td->limits_changed = false;
204
205
206
207
208
209
210
211 atomic_set(&tg->ref, 1);
212
213
214 sscanf(dev_name(bdi->dev), "%u:%u", &major, &minor);
215 blkiocg_add_blkio_group(blkcg, &tg->blkg, (void *)td,
216 MKDEV(major, minor), BLKIO_POLICY_THROTL);
217
218 tg->bps[READ] = blkcg_get_read_bps(blkcg, tg->blkg.dev);
219 tg->bps[WRITE] = blkcg_get_write_bps(blkcg, tg->blkg.dev);
220 tg->iops[READ] = blkcg_get_read_iops(blkcg, tg->blkg.dev);
221 tg->iops[WRITE] = blkcg_get_write_iops(blkcg, tg->blkg.dev);
222
223 hlist_add_head(&tg->tg_node, &td->tg_list);
224 td->nr_undestroyed_grps++;
225done:
226 return tg;
227}
228
229static struct throtl_grp * throtl_get_tg(struct throtl_data *td)
230{
231 struct throtl_grp *tg = NULL;
232 struct blkio_cgroup *blkcg;
233
234 rcu_read_lock();
235 blkcg = task_blkio_cgroup(current);
236 tg = throtl_find_alloc_tg(td, blkcg);
237 if (!tg)
238 tg = &td->root_tg;
239 rcu_read_unlock();
240 return tg;
241}
242
243static struct throtl_grp *throtl_rb_first(struct throtl_rb_root *root)
244{
245
246 if (!root->count)
247 return NULL;
248
249 if (!root->left)
250 root->left = rb_first(&root->rb);
251
252 if (root->left)
253 return rb_entry_tg(root->left);
254
255 return NULL;
256}
257
258static void rb_erase_init(struct rb_node *n, struct rb_root *root)
259{
260 rb_erase(n, root);
261 RB_CLEAR_NODE(n);
262}
263
264static void throtl_rb_erase(struct rb_node *n, struct throtl_rb_root *root)
265{
266 if (root->left == n)
267 root->left = NULL;
268 rb_erase_init(n, &root->rb);
269 --root->count;
270}
271
272static void update_min_dispatch_time(struct throtl_rb_root *st)
273{
274 struct throtl_grp *tg;
275
276 tg = throtl_rb_first(st);
277 if (!tg)
278 return;
279
280 st->min_disptime = tg->disptime;
281}
282
283static void
284tg_service_tree_add(struct throtl_rb_root *st, struct throtl_grp *tg)
285{
286 struct rb_node **node = &st->rb.rb_node;
287 struct rb_node *parent = NULL;
288 struct throtl_grp *__tg;
289 unsigned long key = tg->disptime;
290 int left = 1;
291
292 while (*node != NULL) {
293 parent = *node;
294 __tg = rb_entry_tg(parent);
295
296 if (time_before(key, __tg->disptime))
297 node = &parent->rb_left;
298 else {
299 node = &parent->rb_right;
300 left = 0;
301 }
302 }
303
304 if (left)
305 st->left = &tg->rb_node;
306
307 rb_link_node(&tg->rb_node, parent, node);
308 rb_insert_color(&tg->rb_node, &st->rb);
309}
310
311static void __throtl_enqueue_tg(struct throtl_data *td, struct throtl_grp *tg)
312{
313 struct throtl_rb_root *st = &td->tg_service_tree;
314
315 tg_service_tree_add(st, tg);
316 throtl_mark_tg_on_rr(tg);
317 st->count++;
318}
319
320static void throtl_enqueue_tg(struct throtl_data *td, struct throtl_grp *tg)
321{
322 if (!throtl_tg_on_rr(tg))
323 __throtl_enqueue_tg(td, tg);
324}
325
326static void __throtl_dequeue_tg(struct throtl_data *td, struct throtl_grp *tg)
327{
328 throtl_rb_erase(&tg->rb_node, &td->tg_service_tree);
329 throtl_clear_tg_on_rr(tg);
330}
331
332static void throtl_dequeue_tg(struct throtl_data *td, struct throtl_grp *tg)
333{
334 if (throtl_tg_on_rr(tg))
335 __throtl_dequeue_tg(td, tg);
336}
337
338static void throtl_schedule_next_dispatch(struct throtl_data *td)
339{
340 struct throtl_rb_root *st = &td->tg_service_tree;
341
342
343
344
345 if (!total_nr_queued(td))
346 return;
347
348 BUG_ON(!st->count);
349
350 update_min_dispatch_time(st);
351
352 if (time_before_eq(st->min_disptime, jiffies))
353 throtl_schedule_delayed_work(td, 0);
354 else
355 throtl_schedule_delayed_work(td, (st->min_disptime - jiffies));
356}
357
358static inline void
359throtl_start_new_slice(struct throtl_data *td, struct throtl_grp *tg, bool rw)
360{
361 tg->bytes_disp[rw] = 0;
362 tg->io_disp[rw] = 0;
363 tg->slice_start[rw] = jiffies;
364 tg->slice_end[rw] = jiffies + throtl_slice;
365 throtl_log_tg(td, tg, "[%c] new slice start=%lu end=%lu jiffies=%lu",
366 rw == READ ? 'R' : 'W', tg->slice_start[rw],
367 tg->slice_end[rw], jiffies);
368}
369
370static inline void throtl_set_slice_end(struct throtl_data *td,
371 struct throtl_grp *tg, bool rw, unsigned long jiffy_end)
372{
373 tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
374}
375
376static inline void throtl_extend_slice(struct throtl_data *td,
377 struct throtl_grp *tg, bool rw, unsigned long jiffy_end)
378{
379 tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
380 throtl_log_tg(td, tg, "[%c] extend slice start=%lu end=%lu jiffies=%lu",
381 rw == READ ? 'R' : 'W', tg->slice_start[rw],
382 tg->slice_end[rw], jiffies);
383}
384
385
386static bool
387throtl_slice_used(struct throtl_data *td, struct throtl_grp *tg, bool rw)
388{
389 if (time_in_range(jiffies, tg->slice_start[rw], tg->slice_end[rw]))
390 return 0;
391
392 return 1;
393}
394
395
396static inline void
397throtl_trim_slice(struct throtl_data *td, struct throtl_grp *tg, bool rw)
398{
399 unsigned long nr_slices, time_elapsed, io_trim;
400 u64 bytes_trim, tmp;
401
402 BUG_ON(time_before(tg->slice_end[rw], tg->slice_start[rw]));
403
404
405
406
407
408
409 if (throtl_slice_used(td, tg, rw))
410 return;
411
412
413
414
415
416
417
418
419
420 throtl_set_slice_end(td, tg, rw, jiffies + throtl_slice);
421
422 time_elapsed = jiffies - tg->slice_start[rw];
423
424 nr_slices = time_elapsed / throtl_slice;
425
426 if (!nr_slices)
427 return;
428 tmp = tg->bps[rw] * throtl_slice * nr_slices;
429 do_div(tmp, HZ);
430 bytes_trim = tmp;
431
432 io_trim = (tg->iops[rw] * throtl_slice * nr_slices)/HZ;
433
434 if (!bytes_trim && !io_trim)
435 return;
436
437 if (tg->bytes_disp[rw] >= bytes_trim)
438 tg->bytes_disp[rw] -= bytes_trim;
439 else
440 tg->bytes_disp[rw] = 0;
441
442 if (tg->io_disp[rw] >= io_trim)
443 tg->io_disp[rw] -= io_trim;
444 else
445 tg->io_disp[rw] = 0;
446
447 tg->slice_start[rw] += nr_slices * throtl_slice;
448
449 throtl_log_tg(td, tg, "[%c] trim slice nr=%lu bytes=%llu io=%lu"
450 " start=%lu end=%lu jiffies=%lu",
451 rw == READ ? 'R' : 'W', nr_slices, bytes_trim, io_trim,
452 tg->slice_start[rw], tg->slice_end[rw], jiffies);
453}
454
455static bool tg_with_in_iops_limit(struct throtl_data *td, struct throtl_grp *tg,
456 struct bio *bio, unsigned long *wait)
457{
458 bool rw = bio_data_dir(bio);
459 unsigned int io_allowed;
460 unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
461 u64 tmp;
462
463 jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
464
465
466 if (!jiffy_elapsed)
467 jiffy_elapsed_rnd = throtl_slice;
468
469 jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
470
471
472
473
474
475
476
477
478 tmp = (u64)tg->iops[rw] * jiffy_elapsed_rnd;
479 do_div(tmp, HZ);
480
481 if (tmp > UINT_MAX)
482 io_allowed = UINT_MAX;
483 else
484 io_allowed = tmp;
485
486 if (tg->io_disp[rw] + 1 <= io_allowed) {
487 if (wait)
488 *wait = 0;
489 return 1;
490 }
491
492
493 jiffy_wait = ((tg->io_disp[rw] + 1) * HZ)/tg->iops[rw] + 1;
494
495 if (jiffy_wait > jiffy_elapsed)
496 jiffy_wait = jiffy_wait - jiffy_elapsed;
497 else
498 jiffy_wait = 1;
499
500 if (wait)
501 *wait = jiffy_wait;
502 return 0;
503}
504
505static bool tg_with_in_bps_limit(struct throtl_data *td, struct throtl_grp *tg,
506 struct bio *bio, unsigned long *wait)
507{
508 bool rw = bio_data_dir(bio);
509 u64 bytes_allowed, extra_bytes, tmp;
510 unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
511
512 jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
513
514
515 if (!jiffy_elapsed)
516 jiffy_elapsed_rnd = throtl_slice;
517
518 jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
519
520 tmp = tg->bps[rw] * jiffy_elapsed_rnd;
521 do_div(tmp, HZ);
522 bytes_allowed = tmp;
523
524 if (tg->bytes_disp[rw] + bio->bi_size <= bytes_allowed) {
525 if (wait)
526 *wait = 0;
527 return 1;
528 }
529
530
531 extra_bytes = tg->bytes_disp[rw] + bio->bi_size - bytes_allowed;
532 jiffy_wait = div64_u64(extra_bytes * HZ, tg->bps[rw]);
533
534 if (!jiffy_wait)
535 jiffy_wait = 1;
536
537
538
539
540
541 jiffy_wait = jiffy_wait + (jiffy_elapsed_rnd - jiffy_elapsed);
542 if (wait)
543 *wait = jiffy_wait;
544 return 0;
545}
546
547
548
549
550
551static bool tg_may_dispatch(struct throtl_data *td, struct throtl_grp *tg,
552 struct bio *bio, unsigned long *wait)
553{
554 bool rw = bio_data_dir(bio);
555 unsigned long bps_wait = 0, iops_wait = 0, max_wait = 0;
556
557
558
559
560
561
562
563 BUG_ON(tg->nr_queued[rw] && bio != bio_list_peek(&tg->bio_lists[rw]));
564
565
566 if (tg->bps[rw] == -1 && tg->iops[rw] == -1) {
567 if (wait)
568 *wait = 0;
569 return 1;
570 }
571
572
573
574
575
576
577 if (throtl_slice_used(td, tg, rw))
578 throtl_start_new_slice(td, tg, rw);
579 else {
580 if (time_before(tg->slice_end[rw], jiffies + throtl_slice))
581 throtl_extend_slice(td, tg, rw, jiffies + throtl_slice);
582 }
583
584 if (tg_with_in_bps_limit(td, tg, bio, &bps_wait)
585 && tg_with_in_iops_limit(td, tg, bio, &iops_wait)) {
586 if (wait)
587 *wait = 0;
588 return 1;
589 }
590
591 max_wait = max(bps_wait, iops_wait);
592
593 if (wait)
594 *wait = max_wait;
595
596 if (time_before(tg->slice_end[rw], jiffies + max_wait))
597 throtl_extend_slice(td, tg, rw, jiffies + max_wait);
598
599 return 0;
600}
601
602static void throtl_charge_bio(struct throtl_grp *tg, struct bio *bio)
603{
604 bool rw = bio_data_dir(bio);
605 bool sync = bio->bi_rw & REQ_SYNC;
606
607
608 tg->bytes_disp[rw] += bio->bi_size;
609 tg->io_disp[rw]++;
610
611
612
613
614
615 blkiocg_update_dispatch_stats(&tg->blkg, bio->bi_size, rw, sync);
616}
617
618static void throtl_add_bio_tg(struct throtl_data *td, struct throtl_grp *tg,
619 struct bio *bio)
620{
621 bool rw = bio_data_dir(bio);
622
623 bio_list_add(&tg->bio_lists[rw], bio);
624
625 throtl_ref_get_tg(tg);
626 tg->nr_queued[rw]++;
627 td->nr_queued[rw]++;
628 throtl_enqueue_tg(td, tg);
629}
630
631static void tg_update_disptime(struct throtl_data *td, struct throtl_grp *tg)
632{
633 unsigned long read_wait = -1, write_wait = -1, min_wait = -1, disptime;
634 struct bio *bio;
635
636 if ((bio = bio_list_peek(&tg->bio_lists[READ])))
637 tg_may_dispatch(td, tg, bio, &read_wait);
638
639 if ((bio = bio_list_peek(&tg->bio_lists[WRITE])))
640 tg_may_dispatch(td, tg, bio, &write_wait);
641
642 min_wait = min(read_wait, write_wait);
643 disptime = jiffies + min_wait;
644
645
646 throtl_dequeue_tg(td, tg);
647 tg->disptime = disptime;
648 throtl_enqueue_tg(td, tg);
649}
650
651static void tg_dispatch_one_bio(struct throtl_data *td, struct throtl_grp *tg,
652 bool rw, struct bio_list *bl)
653{
654 struct bio *bio;
655
656 bio = bio_list_pop(&tg->bio_lists[rw]);
657 tg->nr_queued[rw]--;
658
659 throtl_put_tg(tg);
660
661 BUG_ON(td->nr_queued[rw] <= 0);
662 td->nr_queued[rw]--;
663
664 throtl_charge_bio(tg, bio);
665 bio_list_add(bl, bio);
666 bio->bi_rw |= REQ_THROTTLED;
667
668 throtl_trim_slice(td, tg, rw);
669}
670
671static int throtl_dispatch_tg(struct throtl_data *td, struct throtl_grp *tg,
672 struct bio_list *bl)
673{
674 unsigned int nr_reads = 0, nr_writes = 0;
675 unsigned int max_nr_reads = throtl_grp_quantum*3/4;
676 unsigned int max_nr_writes = throtl_grp_quantum - max_nr_reads;
677 struct bio *bio;
678
679
680
681 while ((bio = bio_list_peek(&tg->bio_lists[READ]))
682 && tg_may_dispatch(td, tg, bio, NULL)) {
683
684 tg_dispatch_one_bio(td, tg, bio_data_dir(bio), bl);
685 nr_reads++;
686
687 if (nr_reads >= max_nr_reads)
688 break;
689 }
690
691 while ((bio = bio_list_peek(&tg->bio_lists[WRITE]))
692 && tg_may_dispatch(td, tg, bio, NULL)) {
693
694 tg_dispatch_one_bio(td, tg, bio_data_dir(bio), bl);
695 nr_writes++;
696
697 if (nr_writes >= max_nr_writes)
698 break;
699 }
700
701 return nr_reads + nr_writes;
702}
703
704static int throtl_select_dispatch(struct throtl_data *td, struct bio_list *bl)
705{
706 unsigned int nr_disp = 0;
707 struct throtl_grp *tg;
708 struct throtl_rb_root *st = &td->tg_service_tree;
709
710 while (1) {
711 tg = throtl_rb_first(st);
712
713 if (!tg)
714 break;
715
716 if (time_before(jiffies, tg->disptime))
717 break;
718
719 throtl_dequeue_tg(td, tg);
720
721 nr_disp += throtl_dispatch_tg(td, tg, bl);
722
723 if (tg->nr_queued[0] || tg->nr_queued[1]) {
724 tg_update_disptime(td, tg);
725 throtl_enqueue_tg(td, tg);
726 }
727
728 if (nr_disp >= throtl_quantum)
729 break;
730 }
731
732 return nr_disp;
733}
734
735static void throtl_process_limit_change(struct throtl_data *td)
736{
737 struct throtl_grp *tg;
738 struct hlist_node *pos, *n;
739
740 if (!td->limits_changed)
741 return;
742
743 xchg(&td->limits_changed, false);
744
745 throtl_log(td, "limits changed");
746
747 hlist_for_each_entry_safe(tg, pos, n, &td->tg_list, tg_node) {
748 if (!tg->limits_changed)
749 continue;
750
751 if (!xchg(&tg->limits_changed, false))
752 continue;
753
754 throtl_log_tg(td, tg, "limit change rbps=%llu wbps=%llu"
755 " riops=%u wiops=%u", tg->bps[READ], tg->bps[WRITE],
756 tg->iops[READ], tg->iops[WRITE]);
757
758
759
760
761
762
763
764 throtl_start_new_slice(td, tg, 0);
765 throtl_start_new_slice(td, tg, 1);
766
767 if (throtl_tg_on_rr(tg))
768 tg_update_disptime(td, tg);
769 }
770}
771
772
773static int throtl_dispatch(struct request_queue *q)
774{
775 struct throtl_data *td = q->td;
776 unsigned int nr_disp = 0;
777 struct bio_list bio_list_on_stack;
778 struct bio *bio;
779 struct blk_plug plug;
780
781 spin_lock_irq(q->queue_lock);
782
783 throtl_process_limit_change(td);
784
785 if (!total_nr_queued(td))
786 goto out;
787
788 bio_list_init(&bio_list_on_stack);
789
790 throtl_log(td, "dispatch nr_queued=%lu read=%u write=%u",
791 total_nr_queued(td), td->nr_queued[READ],
792 td->nr_queued[WRITE]);
793
794 nr_disp = throtl_select_dispatch(td, &bio_list_on_stack);
795
796 if (nr_disp)
797 throtl_log(td, "bios disp=%u", nr_disp);
798
799 throtl_schedule_next_dispatch(td);
800out:
801 spin_unlock_irq(q->queue_lock);
802
803
804
805
806
807 if (nr_disp) {
808 blk_start_plug(&plug);
809 while((bio = bio_list_pop(&bio_list_on_stack)))
810 generic_make_request(bio);
811 blk_finish_plug(&plug);
812 }
813 return nr_disp;
814}
815
816void blk_throtl_work(struct work_struct *work)
817{
818 struct throtl_data *td = container_of(work, struct throtl_data,
819 throtl_work.work);
820 struct request_queue *q = td->queue;
821
822 throtl_dispatch(q);
823}
824
825
826static void
827throtl_schedule_delayed_work(struct throtl_data *td, unsigned long delay)
828{
829
830 struct delayed_work *dwork = &td->throtl_work;
831
832
833 if (total_nr_queued(td) > 0 || td->limits_changed) {
834
835
836
837
838 __cancel_delayed_work(dwork);
839 queue_delayed_work(kthrotld_workqueue, dwork, delay);
840 throtl_log(td, "schedule work. delay=%lu jiffies=%lu",
841 delay, jiffies);
842 }
843}
844
845static void
846throtl_destroy_tg(struct throtl_data *td, struct throtl_grp *tg)
847{
848
849 BUG_ON(hlist_unhashed(&tg->tg_node));
850
851 hlist_del_init(&tg->tg_node);
852
853
854
855
856
857 throtl_put_tg(tg);
858 td->nr_undestroyed_grps--;
859}
860
861static void throtl_release_tgs(struct throtl_data *td)
862{
863 struct hlist_node *pos, *n;
864 struct throtl_grp *tg;
865
866 hlist_for_each_entry_safe(tg, pos, n, &td->tg_list, tg_node) {
867
868
869
870
871
872 if (!blkiocg_del_blkio_group(&tg->blkg))
873 throtl_destroy_tg(td, tg);
874 }
875}
876
877static void throtl_td_free(struct throtl_data *td)
878{
879 kfree(td);
880}
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896void throtl_unlink_blkio_group(void *key, struct blkio_group *blkg)
897{
898 unsigned long flags;
899 struct throtl_data *td = key;
900
901 spin_lock_irqsave(td->queue->queue_lock, flags);
902 throtl_destroy_tg(td, tg_of_blkg(blkg));
903 spin_unlock_irqrestore(td->queue->queue_lock, flags);
904}
905
906static void throtl_update_blkio_group_common(struct throtl_data *td,
907 struct throtl_grp *tg)
908{
909 xchg(&tg->limits_changed, true);
910 xchg(&td->limits_changed, true);
911
912 throtl_schedule_delayed_work(td, 0);
913}
914
915
916
917
918
919
920
921
922
923
924static void throtl_update_blkio_group_read_bps(void *key,
925 struct blkio_group *blkg, u64 read_bps)
926{
927 struct throtl_data *td = key;
928 struct throtl_grp *tg = tg_of_blkg(blkg);
929
930 tg->bps[READ] = read_bps;
931 throtl_update_blkio_group_common(td, tg);
932}
933
934static void throtl_update_blkio_group_write_bps(void *key,
935 struct blkio_group *blkg, u64 write_bps)
936{
937 struct throtl_data *td = key;
938 struct throtl_grp *tg = tg_of_blkg(blkg);
939
940 tg->bps[WRITE] = write_bps;
941 throtl_update_blkio_group_common(td, tg);
942}
943
944static void throtl_update_blkio_group_read_iops(void *key,
945 struct blkio_group *blkg, unsigned int read_iops)
946{
947 struct throtl_data *td = key;
948 struct throtl_grp *tg = tg_of_blkg(blkg);
949
950 tg->iops[READ] = read_iops;
951 throtl_update_blkio_group_common(td, tg);
952}
953
954static void throtl_update_blkio_group_write_iops(void *key,
955 struct blkio_group *blkg, unsigned int write_iops)
956{
957 struct throtl_data *td = key;
958 struct throtl_grp *tg = tg_of_blkg(blkg);
959
960 tg->iops[WRITE] = write_iops;
961 throtl_update_blkio_group_common(td, tg);
962}
963
964static void throtl_shutdown_wq(struct request_queue *q)
965{
966 struct throtl_data *td = q->td;
967
968 cancel_delayed_work_sync(&td->throtl_work);
969}
970
971static struct blkio_policy_type blkio_policy_throtl = {
972 .ops = {
973 .blkio_unlink_group_fn = throtl_unlink_blkio_group,
974 .blkio_update_group_read_bps_fn =
975 throtl_update_blkio_group_read_bps,
976 .blkio_update_group_write_bps_fn =
977 throtl_update_blkio_group_write_bps,
978 .blkio_update_group_read_iops_fn =
979 throtl_update_blkio_group_read_iops,
980 .blkio_update_group_write_iops_fn =
981 throtl_update_blkio_group_write_iops,
982 },
983 .plid = BLKIO_POLICY_THROTL,
984};
985
986int blk_throtl_bio(struct request_queue *q, struct bio **biop)
987{
988 struct throtl_data *td = q->td;
989 struct throtl_grp *tg;
990 struct bio *bio = *biop;
991 bool rw = bio_data_dir(bio), update_disptime = true;
992
993 if (bio->bi_rw & REQ_THROTTLED) {
994 bio->bi_rw &= ~REQ_THROTTLED;
995 return 0;
996 }
997
998 spin_lock_irq(q->queue_lock);
999 tg = throtl_get_tg(td);
1000
1001 if (tg->nr_queued[rw]) {
1002
1003
1004
1005
1006 update_disptime = false;
1007 goto queue_bio;
1008
1009 }
1010
1011
1012 if (tg_may_dispatch(td, tg, bio, NULL)) {
1013 throtl_charge_bio(tg, bio);
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026 throtl_trim_slice(td, tg, rw);
1027 goto out;
1028 }
1029
1030queue_bio:
1031 throtl_log_tg(td, tg, "[%c] bio. bdisp=%u sz=%u bps=%llu"
1032 " iodisp=%u iops=%u queued=%d/%d",
1033 rw == READ ? 'R' : 'W',
1034 tg->bytes_disp[rw], bio->bi_size, tg->bps[rw],
1035 tg->io_disp[rw], tg->iops[rw],
1036 tg->nr_queued[READ], tg->nr_queued[WRITE]);
1037
1038 throtl_add_bio_tg(q->td, tg, bio);
1039 *biop = NULL;
1040
1041 if (update_disptime) {
1042 tg_update_disptime(td, tg);
1043 throtl_schedule_next_dispatch(td);
1044 }
1045
1046out:
1047 spin_unlock_irq(q->queue_lock);
1048 return 0;
1049}
1050
1051int blk_throtl_init(struct request_queue *q)
1052{
1053 struct throtl_data *td;
1054 struct throtl_grp *tg;
1055
1056 td = kzalloc_node(sizeof(*td), GFP_KERNEL, q->node);
1057 if (!td)
1058 return -ENOMEM;
1059
1060 INIT_HLIST_HEAD(&td->tg_list);
1061 td->tg_service_tree = THROTL_RB_ROOT;
1062 td->limits_changed = false;
1063
1064
1065 tg = &td->root_tg;
1066 INIT_HLIST_NODE(&tg->tg_node);
1067 RB_CLEAR_NODE(&tg->rb_node);
1068 bio_list_init(&tg->bio_lists[0]);
1069 bio_list_init(&tg->bio_lists[1]);
1070
1071
1072 tg->bps[0] = tg->bps[1] = -1;
1073 tg->iops[0] = tg->iops[1] = -1;
1074 td->limits_changed = false;
1075
1076
1077
1078
1079
1080
1081
1082
1083 atomic_set(&tg->ref, 2);
1084 hlist_add_head(&tg->tg_node, &td->tg_list);
1085 td->nr_undestroyed_grps++;
1086
1087 INIT_DELAYED_WORK(&td->throtl_work, blk_throtl_work);
1088
1089 rcu_read_lock();
1090 blkiocg_add_blkio_group(&blkio_root_cgroup, &tg->blkg, (void *)td,
1091 0, BLKIO_POLICY_THROTL);
1092 rcu_read_unlock();
1093
1094
1095 td->queue = q;
1096 q->td = td;
1097 return 0;
1098}
1099
1100void blk_throtl_exit(struct request_queue *q)
1101{
1102 struct throtl_data *td = q->td;
1103 bool wait = false;
1104
1105 BUG_ON(!td);
1106
1107 throtl_shutdown_wq(q);
1108
1109 spin_lock_irq(q->queue_lock);
1110 throtl_release_tgs(td);
1111
1112
1113 if (td->nr_undestroyed_grps > 0)
1114 wait = true;
1115
1116 spin_unlock_irq(q->queue_lock);
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129 if (wait)
1130 synchronize_rcu();
1131
1132
1133
1134
1135
1136
1137 throtl_shutdown_wq(q);
1138 throtl_td_free(td);
1139}
1140
1141static int __init throtl_init(void)
1142{
1143 kthrotld_workqueue = alloc_workqueue("kthrotld", WQ_MEM_RECLAIM, 0);
1144 if (!kthrotld_workqueue)
1145 panic("Failed to create kthrotld\n");
1146
1147 blkio_policy_register(&blkio_policy_throtl);
1148 return 0;
1149}
1150
1151module_init(throtl_init);
1152