TTY: open/hangup race fixup
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / block / blk-throttle.c
blob56ad4531b41234f87485e4f4bca5ca84d4d10dc1
1 /*
2 * Interface for controlling IO bandwidth on a request queue
4 * Copyright (C) 2010 Vivek Goyal <vgoyal@redhat.com>
5 */
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"
14 /* Max dispatch from a group in 1 round */
15 static int throtl_grp_quantum = 8;
17 /* Total max dispatch from all groups in one round */
18 static int throtl_quantum = 32;
20 /* Throttling is performed over 100ms slice and after that slice is renewed */
21 static unsigned long throtl_slice = HZ/10; /* 100 ms */
23 struct throtl_rb_root {
24 struct rb_root rb;
25 struct rb_node *left;
26 unsigned int count;
27 unsigned long min_disptime;
30 #define THROTL_RB_ROOT (struct throtl_rb_root) { .rb = RB_ROOT, .left = NULL, \
31 .count = 0, .min_disptime = 0}
33 #define rb_entry_tg(node) rb_entry((node), struct throtl_grp, rb_node)
35 struct throtl_grp {
36 /* List of throtl groups on the request queue*/
37 struct hlist_node tg_node;
39 /* active throtl group service_tree member */
40 struct rb_node rb_node;
43 * Dispatch time in jiffies. This is the estimated time when group
44 * will unthrottle and is ready to dispatch more bio. It is used as
45 * key to sort active groups in service tree.
47 unsigned long disptime;
49 struct blkio_group blkg;
50 atomic_t ref;
51 unsigned int flags;
53 /* Two lists for READ and WRITE */
54 struct bio_list bio_lists[2];
56 /* Number of queued bios on READ and WRITE lists */
57 unsigned int nr_queued[2];
59 /* bytes per second rate limits */
60 uint64_t bps[2];
62 /* IOPS limits */
63 unsigned int iops[2];
65 /* Number of bytes disptached in current slice */
66 uint64_t bytes_disp[2];
67 /* Number of bio's dispatched in current slice */
68 unsigned int io_disp[2];
70 /* When did we start a new slice */
71 unsigned long slice_start[2];
72 unsigned long slice_end[2];
74 /* Some throttle limits got updated for the group */
75 bool limits_changed;
78 struct throtl_data
80 /* List of throtl groups */
81 struct hlist_head tg_list;
83 /* service tree for active throtl groups */
84 struct throtl_rb_root tg_service_tree;
86 struct throtl_grp root_tg;
87 struct request_queue *queue;
89 /* Total Number of queued bios on READ and WRITE lists */
90 unsigned int nr_queued[2];
93 * number of total undestroyed groups
95 unsigned int nr_undestroyed_grps;
97 /* Work for dispatching throttled bios */
98 struct delayed_work throtl_work;
100 atomic_t limits_changed;
103 enum tg_state_flags {
104 THROTL_TG_FLAG_on_rr = 0, /* on round-robin busy list */
107 #define THROTL_TG_FNS(name) \
108 static inline void throtl_mark_tg_##name(struct throtl_grp *tg) \
110 (tg)->flags |= (1 << THROTL_TG_FLAG_##name); \
112 static inline void throtl_clear_tg_##name(struct throtl_grp *tg) \
114 (tg)->flags &= ~(1 << THROTL_TG_FLAG_##name); \
116 static inline int throtl_tg_##name(const struct throtl_grp *tg) \
118 return ((tg)->flags & (1 << THROTL_TG_FLAG_##name)) != 0; \
121 THROTL_TG_FNS(on_rr);
123 #define throtl_log_tg(td, tg, fmt, args...) \
124 blk_add_trace_msg((td)->queue, "throtl %s " fmt, \
125 blkg_path(&(tg)->blkg), ##args); \
127 #define throtl_log(td, fmt, args...) \
128 blk_add_trace_msg((td)->queue, "throtl " fmt, ##args)
130 static inline struct throtl_grp *tg_of_blkg(struct blkio_group *blkg)
132 if (blkg)
133 return container_of(blkg, struct throtl_grp, blkg);
135 return NULL;
138 static inline int total_nr_queued(struct throtl_data *td)
140 return (td->nr_queued[0] + td->nr_queued[1]);
143 static inline struct throtl_grp *throtl_ref_get_tg(struct throtl_grp *tg)
145 atomic_inc(&tg->ref);
146 return tg;
149 static void throtl_put_tg(struct throtl_grp *tg)
151 BUG_ON(atomic_read(&tg->ref) <= 0);
152 if (!atomic_dec_and_test(&tg->ref))
153 return;
154 kfree(tg);
157 static struct throtl_grp * throtl_find_alloc_tg(struct throtl_data *td,
158 struct cgroup *cgroup)
160 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
161 struct throtl_grp *tg = NULL;
162 void *key = td;
163 struct backing_dev_info *bdi = &td->queue->backing_dev_info;
164 unsigned int major, minor;
167 * TODO: Speed up blkiocg_lookup_group() by maintaining a radix
168 * tree of blkg (instead of traversing through hash list all
169 * the time.
171 tg = tg_of_blkg(blkiocg_lookup_group(blkcg, key));
173 /* Fill in device details for root group */
174 if (tg && !tg->blkg.dev && bdi->dev && dev_name(bdi->dev)) {
175 sscanf(dev_name(bdi->dev), "%u:%u", &major, &minor);
176 tg->blkg.dev = MKDEV(major, minor);
177 goto done;
180 if (tg)
181 goto done;
183 tg = kzalloc_node(sizeof(*tg), GFP_ATOMIC, td->queue->node);
184 if (!tg)
185 goto done;
187 INIT_HLIST_NODE(&tg->tg_node);
188 RB_CLEAR_NODE(&tg->rb_node);
189 bio_list_init(&tg->bio_lists[0]);
190 bio_list_init(&tg->bio_lists[1]);
193 * Take the initial reference that will be released on destroy
194 * This can be thought of a joint reference by cgroup and
195 * request queue which will be dropped by either request queue
196 * exit or cgroup deletion path depending on who is exiting first.
198 atomic_set(&tg->ref, 1);
200 /* Add group onto cgroup list */
201 sscanf(dev_name(bdi->dev), "%u:%u", &major, &minor);
202 blkiocg_add_blkio_group(blkcg, &tg->blkg, (void *)td,
203 MKDEV(major, minor), BLKIO_POLICY_THROTL);
205 tg->bps[READ] = blkcg_get_read_bps(blkcg, tg->blkg.dev);
206 tg->bps[WRITE] = blkcg_get_write_bps(blkcg, tg->blkg.dev);
207 tg->iops[READ] = blkcg_get_read_iops(blkcg, tg->blkg.dev);
208 tg->iops[WRITE] = blkcg_get_write_iops(blkcg, tg->blkg.dev);
210 hlist_add_head(&tg->tg_node, &td->tg_list);
211 td->nr_undestroyed_grps++;
212 done:
213 return tg;
216 static struct throtl_grp * throtl_get_tg(struct throtl_data *td)
218 struct cgroup *cgroup;
219 struct throtl_grp *tg = NULL;
221 rcu_read_lock();
222 cgroup = task_cgroup(current, blkio_subsys_id);
223 tg = throtl_find_alloc_tg(td, cgroup);
224 if (!tg)
225 tg = &td->root_tg;
226 rcu_read_unlock();
227 return tg;
230 static struct throtl_grp *throtl_rb_first(struct throtl_rb_root *root)
232 /* Service tree is empty */
233 if (!root->count)
234 return NULL;
236 if (!root->left)
237 root->left = rb_first(&root->rb);
239 if (root->left)
240 return rb_entry_tg(root->left);
242 return NULL;
245 static void rb_erase_init(struct rb_node *n, struct rb_root *root)
247 rb_erase(n, root);
248 RB_CLEAR_NODE(n);
251 static void throtl_rb_erase(struct rb_node *n, struct throtl_rb_root *root)
253 if (root->left == n)
254 root->left = NULL;
255 rb_erase_init(n, &root->rb);
256 --root->count;
259 static void update_min_dispatch_time(struct throtl_rb_root *st)
261 struct throtl_grp *tg;
263 tg = throtl_rb_first(st);
264 if (!tg)
265 return;
267 st->min_disptime = tg->disptime;
270 static void
271 tg_service_tree_add(struct throtl_rb_root *st, struct throtl_grp *tg)
273 struct rb_node **node = &st->rb.rb_node;
274 struct rb_node *parent = NULL;
275 struct throtl_grp *__tg;
276 unsigned long key = tg->disptime;
277 int left = 1;
279 while (*node != NULL) {
280 parent = *node;
281 __tg = rb_entry_tg(parent);
283 if (time_before(key, __tg->disptime))
284 node = &parent->rb_left;
285 else {
286 node = &parent->rb_right;
287 left = 0;
291 if (left)
292 st->left = &tg->rb_node;
294 rb_link_node(&tg->rb_node, parent, node);
295 rb_insert_color(&tg->rb_node, &st->rb);
298 static void __throtl_enqueue_tg(struct throtl_data *td, struct throtl_grp *tg)
300 struct throtl_rb_root *st = &td->tg_service_tree;
302 tg_service_tree_add(st, tg);
303 throtl_mark_tg_on_rr(tg);
304 st->count++;
307 static void throtl_enqueue_tg(struct throtl_data *td, struct throtl_grp *tg)
309 if (!throtl_tg_on_rr(tg))
310 __throtl_enqueue_tg(td, tg);
313 static void __throtl_dequeue_tg(struct throtl_data *td, struct throtl_grp *tg)
315 throtl_rb_erase(&tg->rb_node, &td->tg_service_tree);
316 throtl_clear_tg_on_rr(tg);
319 static void throtl_dequeue_tg(struct throtl_data *td, struct throtl_grp *tg)
321 if (throtl_tg_on_rr(tg))
322 __throtl_dequeue_tg(td, tg);
325 static void throtl_schedule_next_dispatch(struct throtl_data *td)
327 struct throtl_rb_root *st = &td->tg_service_tree;
330 * If there are more bios pending, schedule more work.
332 if (!total_nr_queued(td))
333 return;
335 BUG_ON(!st->count);
337 update_min_dispatch_time(st);
339 if (time_before_eq(st->min_disptime, jiffies))
340 throtl_schedule_delayed_work(td->queue, 0);
341 else
342 throtl_schedule_delayed_work(td->queue,
343 (st->min_disptime - jiffies));
346 static inline void
347 throtl_start_new_slice(struct throtl_data *td, struct throtl_grp *tg, bool rw)
349 tg->bytes_disp[rw] = 0;
350 tg->io_disp[rw] = 0;
351 tg->slice_start[rw] = jiffies;
352 tg->slice_end[rw] = jiffies + throtl_slice;
353 throtl_log_tg(td, tg, "[%c] new slice start=%lu end=%lu jiffies=%lu",
354 rw == READ ? 'R' : 'W', tg->slice_start[rw],
355 tg->slice_end[rw], jiffies);
358 static inline void throtl_extend_slice(struct throtl_data *td,
359 struct throtl_grp *tg, bool rw, unsigned long jiffy_end)
361 tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
362 throtl_log_tg(td, tg, "[%c] extend slice start=%lu end=%lu jiffies=%lu",
363 rw == READ ? 'R' : 'W', tg->slice_start[rw],
364 tg->slice_end[rw], jiffies);
367 /* Determine if previously allocated or extended slice is complete or not */
368 static bool
369 throtl_slice_used(struct throtl_data *td, struct throtl_grp *tg, bool rw)
371 if (time_in_range(jiffies, tg->slice_start[rw], tg->slice_end[rw]))
372 return 0;
374 return 1;
377 /* Trim the used slices and adjust slice start accordingly */
378 static inline void
379 throtl_trim_slice(struct throtl_data *td, struct throtl_grp *tg, bool rw)
381 unsigned long nr_slices, time_elapsed, io_trim;
382 u64 bytes_trim, tmp;
384 BUG_ON(time_before(tg->slice_end[rw], tg->slice_start[rw]));
387 * If bps are unlimited (-1), then time slice don't get
388 * renewed. Don't try to trim the slice if slice is used. A new
389 * slice will start when appropriate.
391 if (throtl_slice_used(td, tg, rw))
392 return;
394 time_elapsed = jiffies - tg->slice_start[rw];
396 nr_slices = time_elapsed / throtl_slice;
398 if (!nr_slices)
399 return;
400 tmp = tg->bps[rw] * throtl_slice * nr_slices;
401 do_div(tmp, HZ);
402 bytes_trim = tmp;
404 io_trim = (tg->iops[rw] * throtl_slice * nr_slices)/HZ;
406 if (!bytes_trim && !io_trim)
407 return;
409 if (tg->bytes_disp[rw] >= bytes_trim)
410 tg->bytes_disp[rw] -= bytes_trim;
411 else
412 tg->bytes_disp[rw] = 0;
414 if (tg->io_disp[rw] >= io_trim)
415 tg->io_disp[rw] -= io_trim;
416 else
417 tg->io_disp[rw] = 0;
419 tg->slice_start[rw] += nr_slices * throtl_slice;
421 throtl_log_tg(td, tg, "[%c] trim slice nr=%lu bytes=%llu io=%lu"
422 " start=%lu end=%lu jiffies=%lu",
423 rw == READ ? 'R' : 'W', nr_slices, bytes_trim, io_trim,
424 tg->slice_start[rw], tg->slice_end[rw], jiffies);
427 static bool tg_with_in_iops_limit(struct throtl_data *td, struct throtl_grp *tg,
428 struct bio *bio, unsigned long *wait)
430 bool rw = bio_data_dir(bio);
431 unsigned int io_allowed;
432 unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
433 u64 tmp;
435 jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
437 /* Slice has just started. Consider one slice interval */
438 if (!jiffy_elapsed)
439 jiffy_elapsed_rnd = throtl_slice;
441 jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
444 * jiffy_elapsed_rnd should not be a big value as minimum iops can be
445 * 1 then at max jiffy elapsed should be equivalent of 1 second as we
446 * will allow dispatch after 1 second and after that slice should
447 * have been trimmed.
450 tmp = (u64)tg->iops[rw] * jiffy_elapsed_rnd;
451 do_div(tmp, HZ);
453 if (tmp > UINT_MAX)
454 io_allowed = UINT_MAX;
455 else
456 io_allowed = tmp;
458 if (tg->io_disp[rw] + 1 <= io_allowed) {
459 if (wait)
460 *wait = 0;
461 return 1;
464 /* Calc approx time to dispatch */
465 jiffy_wait = ((tg->io_disp[rw] + 1) * HZ)/tg->iops[rw] + 1;
467 if (jiffy_wait > jiffy_elapsed)
468 jiffy_wait = jiffy_wait - jiffy_elapsed;
469 else
470 jiffy_wait = 1;
472 if (wait)
473 *wait = jiffy_wait;
474 return 0;
477 static bool tg_with_in_bps_limit(struct throtl_data *td, struct throtl_grp *tg,
478 struct bio *bio, unsigned long *wait)
480 bool rw = bio_data_dir(bio);
481 u64 bytes_allowed, extra_bytes, tmp;
482 unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
484 jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
486 /* Slice has just started. Consider one slice interval */
487 if (!jiffy_elapsed)
488 jiffy_elapsed_rnd = throtl_slice;
490 jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
492 tmp = tg->bps[rw] * jiffy_elapsed_rnd;
493 do_div(tmp, HZ);
494 bytes_allowed = tmp;
496 if (tg->bytes_disp[rw] + bio->bi_size <= bytes_allowed) {
497 if (wait)
498 *wait = 0;
499 return 1;
502 /* Calc approx time to dispatch */
503 extra_bytes = tg->bytes_disp[rw] + bio->bi_size - bytes_allowed;
504 jiffy_wait = div64_u64(extra_bytes * HZ, tg->bps[rw]);
506 if (!jiffy_wait)
507 jiffy_wait = 1;
510 * This wait time is without taking into consideration the rounding
511 * up we did. Add that time also.
513 jiffy_wait = jiffy_wait + (jiffy_elapsed_rnd - jiffy_elapsed);
514 if (wait)
515 *wait = jiffy_wait;
516 return 0;
520 * Returns whether one can dispatch a bio or not. Also returns approx number
521 * of jiffies to wait before this bio is with-in IO rate and can be dispatched
523 static bool tg_may_dispatch(struct throtl_data *td, struct throtl_grp *tg,
524 struct bio *bio, unsigned long *wait)
526 bool rw = bio_data_dir(bio);
527 unsigned long bps_wait = 0, iops_wait = 0, max_wait = 0;
530 * Currently whole state machine of group depends on first bio
531 * queued in the group bio list. So one should not be calling
532 * this function with a different bio if there are other bios
533 * queued.
535 BUG_ON(tg->nr_queued[rw] && bio != bio_list_peek(&tg->bio_lists[rw]));
537 /* If tg->bps = -1, then BW is unlimited */
538 if (tg->bps[rw] == -1 && tg->iops[rw] == -1) {
539 if (wait)
540 *wait = 0;
541 return 1;
545 * If previous slice expired, start a new one otherwise renew/extend
546 * existing slice to make sure it is at least throtl_slice interval
547 * long since now.
549 if (throtl_slice_used(td, tg, rw))
550 throtl_start_new_slice(td, tg, rw);
551 else {
552 if (time_before(tg->slice_end[rw], jiffies + throtl_slice))
553 throtl_extend_slice(td, tg, rw, jiffies + throtl_slice);
556 if (tg_with_in_bps_limit(td, tg, bio, &bps_wait)
557 && tg_with_in_iops_limit(td, tg, bio, &iops_wait)) {
558 if (wait)
559 *wait = 0;
560 return 1;
563 max_wait = max(bps_wait, iops_wait);
565 if (wait)
566 *wait = max_wait;
568 if (time_before(tg->slice_end[rw], jiffies + max_wait))
569 throtl_extend_slice(td, tg, rw, jiffies + max_wait);
571 return 0;
574 static void throtl_charge_bio(struct throtl_grp *tg, struct bio *bio)
576 bool rw = bio_data_dir(bio);
577 bool sync = bio->bi_rw & REQ_SYNC;
579 /* Charge the bio to the group */
580 tg->bytes_disp[rw] += bio->bi_size;
581 tg->io_disp[rw]++;
584 * TODO: This will take blkg->stats_lock. Figure out a way
585 * to avoid this cost.
587 blkiocg_update_dispatch_stats(&tg->blkg, bio->bi_size, rw, sync);
590 static void throtl_add_bio_tg(struct throtl_data *td, struct throtl_grp *tg,
591 struct bio *bio)
593 bool rw = bio_data_dir(bio);
595 bio_list_add(&tg->bio_lists[rw], bio);
596 /* Take a bio reference on tg */
597 throtl_ref_get_tg(tg);
598 tg->nr_queued[rw]++;
599 td->nr_queued[rw]++;
600 throtl_enqueue_tg(td, tg);
603 static void tg_update_disptime(struct throtl_data *td, struct throtl_grp *tg)
605 unsigned long read_wait = -1, write_wait = -1, min_wait = -1, disptime;
606 struct bio *bio;
608 if ((bio = bio_list_peek(&tg->bio_lists[READ])))
609 tg_may_dispatch(td, tg, bio, &read_wait);
611 if ((bio = bio_list_peek(&tg->bio_lists[WRITE])))
612 tg_may_dispatch(td, tg, bio, &write_wait);
614 min_wait = min(read_wait, write_wait);
615 disptime = jiffies + min_wait;
617 /* Update dispatch time */
618 throtl_dequeue_tg(td, tg);
619 tg->disptime = disptime;
620 throtl_enqueue_tg(td, tg);
623 static void tg_dispatch_one_bio(struct throtl_data *td, struct throtl_grp *tg,
624 bool rw, struct bio_list *bl)
626 struct bio *bio;
628 bio = bio_list_pop(&tg->bio_lists[rw]);
629 tg->nr_queued[rw]--;
630 /* Drop bio reference on tg */
631 throtl_put_tg(tg);
633 BUG_ON(td->nr_queued[rw] <= 0);
634 td->nr_queued[rw]--;
636 throtl_charge_bio(tg, bio);
637 bio_list_add(bl, bio);
638 bio->bi_rw |= REQ_THROTTLED;
640 throtl_trim_slice(td, tg, rw);
643 static int throtl_dispatch_tg(struct throtl_data *td, struct throtl_grp *tg,
644 struct bio_list *bl)
646 unsigned int nr_reads = 0, nr_writes = 0;
647 unsigned int max_nr_reads = throtl_grp_quantum*3/4;
648 unsigned int max_nr_writes = throtl_grp_quantum - nr_reads;
649 struct bio *bio;
651 /* Try to dispatch 75% READS and 25% WRITES */
653 while ((bio = bio_list_peek(&tg->bio_lists[READ]))
654 && tg_may_dispatch(td, tg, bio, NULL)) {
656 tg_dispatch_one_bio(td, tg, bio_data_dir(bio), bl);
657 nr_reads++;
659 if (nr_reads >= max_nr_reads)
660 break;
663 while ((bio = bio_list_peek(&tg->bio_lists[WRITE]))
664 && tg_may_dispatch(td, tg, bio, NULL)) {
666 tg_dispatch_one_bio(td, tg, bio_data_dir(bio), bl);
667 nr_writes++;
669 if (nr_writes >= max_nr_writes)
670 break;
673 return nr_reads + nr_writes;
676 static int throtl_select_dispatch(struct throtl_data *td, struct bio_list *bl)
678 unsigned int nr_disp = 0;
679 struct throtl_grp *tg;
680 struct throtl_rb_root *st = &td->tg_service_tree;
682 while (1) {
683 tg = throtl_rb_first(st);
685 if (!tg)
686 break;
688 if (time_before(jiffies, tg->disptime))
689 break;
691 throtl_dequeue_tg(td, tg);
693 nr_disp += throtl_dispatch_tg(td, tg, bl);
695 if (tg->nr_queued[0] || tg->nr_queued[1]) {
696 tg_update_disptime(td, tg);
697 throtl_enqueue_tg(td, tg);
700 if (nr_disp >= throtl_quantum)
701 break;
704 return nr_disp;
707 static void throtl_process_limit_change(struct throtl_data *td)
709 struct throtl_grp *tg;
710 struct hlist_node *pos, *n;
713 * Make sure atomic_inc() effects from
714 * throtl_update_blkio_group_read_bps(), group of functions are
715 * visible.
716 * Is this required or smp_mb__after_atomic_inc() was suffcient
717 * after the atomic_inc().
719 smp_rmb();
720 if (!atomic_read(&td->limits_changed))
721 return;
723 throtl_log(td, "limit changed =%d", atomic_read(&td->limits_changed));
725 hlist_for_each_entry_safe(tg, pos, n, &td->tg_list, tg_node) {
727 * Do I need an smp_rmb() here to make sure tg->limits_changed
728 * update is visible. I am relying on smp_rmb() at the
729 * beginning of function and not putting a new one here.
732 if (throtl_tg_on_rr(tg) && tg->limits_changed) {
733 throtl_log_tg(td, tg, "limit change rbps=%llu wbps=%llu"
734 " riops=%u wiops=%u", tg->bps[READ],
735 tg->bps[WRITE], tg->iops[READ],
736 tg->iops[WRITE]);
737 tg_update_disptime(td, tg);
738 tg->limits_changed = false;
742 smp_mb__before_atomic_dec();
743 atomic_dec(&td->limits_changed);
744 smp_mb__after_atomic_dec();
747 /* Dispatch throttled bios. Should be called without queue lock held. */
748 static int throtl_dispatch(struct request_queue *q)
750 struct throtl_data *td = q->td;
751 unsigned int nr_disp = 0;
752 struct bio_list bio_list_on_stack;
753 struct bio *bio;
755 spin_lock_irq(q->queue_lock);
757 throtl_process_limit_change(td);
759 if (!total_nr_queued(td))
760 goto out;
762 bio_list_init(&bio_list_on_stack);
764 throtl_log(td, "dispatch nr_queued=%lu read=%u write=%u",
765 total_nr_queued(td), td->nr_queued[READ],
766 td->nr_queued[WRITE]);
768 nr_disp = throtl_select_dispatch(td, &bio_list_on_stack);
770 if (nr_disp)
771 throtl_log(td, "bios disp=%u", nr_disp);
773 throtl_schedule_next_dispatch(td);
774 out:
775 spin_unlock_irq(q->queue_lock);
778 * If we dispatched some requests, unplug the queue to make sure
779 * immediate dispatch
781 if (nr_disp) {
782 while((bio = bio_list_pop(&bio_list_on_stack)))
783 generic_make_request(bio);
784 blk_unplug(q);
786 return nr_disp;
789 void blk_throtl_work(struct work_struct *work)
791 struct throtl_data *td = container_of(work, struct throtl_data,
792 throtl_work.work);
793 struct request_queue *q = td->queue;
795 throtl_dispatch(q);
798 /* Call with queue lock held */
799 void throtl_schedule_delayed_work(struct request_queue *q, unsigned long delay)
802 struct throtl_data *td = q->td;
803 struct delayed_work *dwork = &td->throtl_work;
805 if (total_nr_queued(td) > 0) {
807 * We might have a work scheduled to be executed in future.
808 * Cancel that and schedule a new one.
810 __cancel_delayed_work(dwork);
811 kblockd_schedule_delayed_work(q, dwork, delay);
812 throtl_log(td, "schedule work. delay=%lu jiffies=%lu",
813 delay, jiffies);
816 EXPORT_SYMBOL(throtl_schedule_delayed_work);
818 static void
819 throtl_destroy_tg(struct throtl_data *td, struct throtl_grp *tg)
821 /* Something wrong if we are trying to remove same group twice */
822 BUG_ON(hlist_unhashed(&tg->tg_node));
824 hlist_del_init(&tg->tg_node);
827 * Put the reference taken at the time of creation so that when all
828 * queues are gone, group can be destroyed.
830 throtl_put_tg(tg);
831 td->nr_undestroyed_grps--;
834 static void throtl_release_tgs(struct throtl_data *td)
836 struct hlist_node *pos, *n;
837 struct throtl_grp *tg;
839 hlist_for_each_entry_safe(tg, pos, n, &td->tg_list, tg_node) {
841 * If cgroup removal path got to blk_group first and removed
842 * it from cgroup list, then it will take care of destroying
843 * cfqg also.
845 if (!blkiocg_del_blkio_group(&tg->blkg))
846 throtl_destroy_tg(td, tg);
850 static void throtl_td_free(struct throtl_data *td)
852 kfree(td);
856 * Blk cgroup controller notification saying that blkio_group object is being
857 * delinked as associated cgroup object is going away. That also means that
858 * no new IO will come in this group. So get rid of this group as soon as
859 * any pending IO in the group is finished.
861 * This function is called under rcu_read_lock(). key is the rcu protected
862 * pointer. That means "key" is a valid throtl_data pointer as long as we are
863 * rcu read lock.
865 * "key" was fetched from blkio_group under blkio_cgroup->lock. That means
866 * it should not be NULL as even if queue was going away, cgroup deltion
867 * path got to it first.
869 void throtl_unlink_blkio_group(void *key, struct blkio_group *blkg)
871 unsigned long flags;
872 struct throtl_data *td = key;
874 spin_lock_irqsave(td->queue->queue_lock, flags);
875 throtl_destroy_tg(td, tg_of_blkg(blkg));
876 spin_unlock_irqrestore(td->queue->queue_lock, flags);
880 * For all update functions, key should be a valid pointer because these
881 * update functions are called under blkcg_lock, that means, blkg is
882 * valid and in turn key is valid. queue exit path can not race becuase
883 * of blkcg_lock
885 * Can not take queue lock in update functions as queue lock under blkcg_lock
886 * is not allowed. Under other paths we take blkcg_lock under queue_lock.
888 static void throtl_update_blkio_group_read_bps(void *key,
889 struct blkio_group *blkg, u64 read_bps)
891 struct throtl_data *td = key;
893 tg_of_blkg(blkg)->bps[READ] = read_bps;
894 /* Make sure read_bps is updated before setting limits_changed */
895 smp_wmb();
896 tg_of_blkg(blkg)->limits_changed = true;
898 /* Make sure tg->limits_changed is updated before td->limits_changed */
899 smp_mb__before_atomic_inc();
900 atomic_inc(&td->limits_changed);
901 smp_mb__after_atomic_inc();
903 /* Schedule a work now to process the limit change */
904 throtl_schedule_delayed_work(td->queue, 0);
907 static void throtl_update_blkio_group_write_bps(void *key,
908 struct blkio_group *blkg, u64 write_bps)
910 struct throtl_data *td = key;
912 tg_of_blkg(blkg)->bps[WRITE] = write_bps;
913 smp_wmb();
914 tg_of_blkg(blkg)->limits_changed = true;
915 smp_mb__before_atomic_inc();
916 atomic_inc(&td->limits_changed);
917 smp_mb__after_atomic_inc();
918 throtl_schedule_delayed_work(td->queue, 0);
921 static void throtl_update_blkio_group_read_iops(void *key,
922 struct blkio_group *blkg, unsigned int read_iops)
924 struct throtl_data *td = key;
926 tg_of_blkg(blkg)->iops[READ] = read_iops;
927 smp_wmb();
928 tg_of_blkg(blkg)->limits_changed = true;
929 smp_mb__before_atomic_inc();
930 atomic_inc(&td->limits_changed);
931 smp_mb__after_atomic_inc();
932 throtl_schedule_delayed_work(td->queue, 0);
935 static void throtl_update_blkio_group_write_iops(void *key,
936 struct blkio_group *blkg, unsigned int write_iops)
938 struct throtl_data *td = key;
940 tg_of_blkg(blkg)->iops[WRITE] = write_iops;
941 smp_wmb();
942 tg_of_blkg(blkg)->limits_changed = true;
943 smp_mb__before_atomic_inc();
944 atomic_inc(&td->limits_changed);
945 smp_mb__after_atomic_inc();
946 throtl_schedule_delayed_work(td->queue, 0);
949 void throtl_shutdown_timer_wq(struct request_queue *q)
951 struct throtl_data *td = q->td;
953 cancel_delayed_work_sync(&td->throtl_work);
956 static struct blkio_policy_type blkio_policy_throtl = {
957 .ops = {
958 .blkio_unlink_group_fn = throtl_unlink_blkio_group,
959 .blkio_update_group_read_bps_fn =
960 throtl_update_blkio_group_read_bps,
961 .blkio_update_group_write_bps_fn =
962 throtl_update_blkio_group_write_bps,
963 .blkio_update_group_read_iops_fn =
964 throtl_update_blkio_group_read_iops,
965 .blkio_update_group_write_iops_fn =
966 throtl_update_blkio_group_write_iops,
968 .plid = BLKIO_POLICY_THROTL,
971 int blk_throtl_bio(struct request_queue *q, struct bio **biop)
973 struct throtl_data *td = q->td;
974 struct throtl_grp *tg;
975 struct bio *bio = *biop;
976 bool rw = bio_data_dir(bio), update_disptime = true;
978 if (bio->bi_rw & REQ_THROTTLED) {
979 bio->bi_rw &= ~REQ_THROTTLED;
980 return 0;
983 spin_lock_irq(q->queue_lock);
984 tg = throtl_get_tg(td);
986 if (tg->nr_queued[rw]) {
988 * There is already another bio queued in same dir. No
989 * need to update dispatch time.
990 * Still update the disptime if rate limits on this group
991 * were changed.
993 if (!tg->limits_changed)
994 update_disptime = false;
995 else
996 tg->limits_changed = false;
998 goto queue_bio;
1001 /* Bio is with-in rate limit of group */
1002 if (tg_may_dispatch(td, tg, bio, NULL)) {
1003 throtl_charge_bio(tg, bio);
1004 goto out;
1007 queue_bio:
1008 throtl_log_tg(td, tg, "[%c] bio. bdisp=%u sz=%u bps=%llu"
1009 " iodisp=%u iops=%u queued=%d/%d",
1010 rw == READ ? 'R' : 'W',
1011 tg->bytes_disp[rw], bio->bi_size, tg->bps[rw],
1012 tg->io_disp[rw], tg->iops[rw],
1013 tg->nr_queued[READ], tg->nr_queued[WRITE]);
1015 throtl_add_bio_tg(q->td, tg, bio);
1016 *biop = NULL;
1018 if (update_disptime) {
1019 tg_update_disptime(td, tg);
1020 throtl_schedule_next_dispatch(td);
1023 out:
1024 spin_unlock_irq(q->queue_lock);
1025 return 0;
1028 int blk_throtl_init(struct request_queue *q)
1030 struct throtl_data *td;
1031 struct throtl_grp *tg;
1033 td = kzalloc_node(sizeof(*td), GFP_KERNEL, q->node);
1034 if (!td)
1035 return -ENOMEM;
1037 INIT_HLIST_HEAD(&td->tg_list);
1038 td->tg_service_tree = THROTL_RB_ROOT;
1039 atomic_set(&td->limits_changed, 0);
1041 /* Init root group */
1042 tg = &td->root_tg;
1043 INIT_HLIST_NODE(&tg->tg_node);
1044 RB_CLEAR_NODE(&tg->rb_node);
1045 bio_list_init(&tg->bio_lists[0]);
1046 bio_list_init(&tg->bio_lists[1]);
1048 /* Practically unlimited BW */
1049 tg->bps[0] = tg->bps[1] = -1;
1050 tg->iops[0] = tg->iops[1] = -1;
1053 * Set root group reference to 2. One reference will be dropped when
1054 * all groups on tg_list are being deleted during queue exit. Other
1055 * reference will remain there as we don't want to delete this group
1056 * as it is statically allocated and gets destroyed when throtl_data
1057 * goes away.
1059 atomic_set(&tg->ref, 2);
1060 hlist_add_head(&tg->tg_node, &td->tg_list);
1061 td->nr_undestroyed_grps++;
1063 INIT_DELAYED_WORK(&td->throtl_work, blk_throtl_work);
1065 rcu_read_lock();
1066 blkiocg_add_blkio_group(&blkio_root_cgroup, &tg->blkg, (void *)td,
1067 0, BLKIO_POLICY_THROTL);
1068 rcu_read_unlock();
1070 /* Attach throtl data to request queue */
1071 td->queue = q;
1072 q->td = td;
1073 return 0;
1076 void blk_throtl_exit(struct request_queue *q)
1078 struct throtl_data *td = q->td;
1079 bool wait = false;
1081 BUG_ON(!td);
1083 throtl_shutdown_timer_wq(q);
1085 spin_lock_irq(q->queue_lock);
1086 throtl_release_tgs(td);
1088 /* If there are other groups */
1089 if (td->nr_undestroyed_grps > 0)
1090 wait = true;
1092 spin_unlock_irq(q->queue_lock);
1095 * Wait for tg->blkg->key accessors to exit their grace periods.
1096 * Do this wait only if there are other undestroyed groups out
1097 * there (other than root group). This can happen if cgroup deletion
1098 * path claimed the responsibility of cleaning up a group before
1099 * queue cleanup code get to the group.
1101 * Do not call synchronize_rcu() unconditionally as there are drivers
1102 * which create/delete request queue hundreds of times during scan/boot
1103 * and synchronize_rcu() can take significant time and slow down boot.
1105 if (wait)
1106 synchronize_rcu();
1109 * Just being safe to make sure after previous flush if some body did
1110 * update limits through cgroup and another work got queued, cancel
1111 * it.
1113 throtl_shutdown_timer_wq(q);
1114 throtl_td_free(td);
1117 static int __init throtl_init(void)
1119 blkio_policy_register(&blkio_policy_throtl);
1120 return 0;
1123 module_init(throtl_init);