drivers/net: Call netif_carrier_off at the end of the probe
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / block / blk-throttle.c
blobb93ffbce1f2a95c519f22bc12192049eb1ee0f82
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 /* A workqueue to queue throttle related work */
24 static struct workqueue_struct *kthrotld_workqueue;
25 static void throtl_schedule_delayed_work(struct throtl_data *td,
26 unsigned long delay);
28 struct throtl_rb_root {
29 struct rb_root rb;
30 struct rb_node *left;
31 unsigned int count;
32 unsigned long min_disptime;
35 #define THROTL_RB_ROOT (struct throtl_rb_root) { .rb = RB_ROOT, .left = NULL, \
36 .count = 0, .min_disptime = 0}
38 #define rb_entry_tg(node) rb_entry((node), struct throtl_grp, rb_node)
40 struct throtl_grp {
41 /* List of throtl groups on the request queue*/
42 struct hlist_node tg_node;
44 /* active throtl group service_tree member */
45 struct rb_node rb_node;
48 * Dispatch time in jiffies. This is the estimated time when group
49 * will unthrottle and is ready to dispatch more bio. It is used as
50 * key to sort active groups in service tree.
52 unsigned long disptime;
54 struct blkio_group blkg;
55 atomic_t ref;
56 unsigned int flags;
58 /* Two lists for READ and WRITE */
59 struct bio_list bio_lists[2];
61 /* Number of queued bios on READ and WRITE lists */
62 unsigned int nr_queued[2];
64 /* bytes per second rate limits */
65 uint64_t bps[2];
67 /* IOPS limits */
68 unsigned int iops[2];
70 /* Number of bytes disptached in current slice */
71 uint64_t bytes_disp[2];
72 /* Number of bio's dispatched in current slice */
73 unsigned int io_disp[2];
75 /* When did we start a new slice */
76 unsigned long slice_start[2];
77 unsigned long slice_end[2];
79 /* Some throttle limits got updated for the group */
80 bool limits_changed;
83 struct throtl_data
85 /* List of throtl groups */
86 struct hlist_head tg_list;
88 /* service tree for active throtl groups */
89 struct throtl_rb_root tg_service_tree;
91 struct throtl_grp root_tg;
92 struct request_queue *queue;
94 /* Total Number of queued bios on READ and WRITE lists */
95 unsigned int nr_queued[2];
98 * number of total undestroyed groups
100 unsigned int nr_undestroyed_grps;
102 /* Work for dispatching throttled bios */
103 struct delayed_work throtl_work;
105 atomic_t limits_changed;
108 enum tg_state_flags {
109 THROTL_TG_FLAG_on_rr = 0, /* on round-robin busy list */
112 #define THROTL_TG_FNS(name) \
113 static inline void throtl_mark_tg_##name(struct throtl_grp *tg) \
115 (tg)->flags |= (1 << THROTL_TG_FLAG_##name); \
117 static inline void throtl_clear_tg_##name(struct throtl_grp *tg) \
119 (tg)->flags &= ~(1 << THROTL_TG_FLAG_##name); \
121 static inline int throtl_tg_##name(const struct throtl_grp *tg) \
123 return ((tg)->flags & (1 << THROTL_TG_FLAG_##name)) != 0; \
126 THROTL_TG_FNS(on_rr);
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); \
132 #define throtl_log(td, fmt, args...) \
133 blk_add_trace_msg((td)->queue, "throtl " fmt, ##args)
135 static inline struct throtl_grp *tg_of_blkg(struct blkio_group *blkg)
137 if (blkg)
138 return container_of(blkg, struct throtl_grp, blkg);
140 return NULL;
143 static inline int total_nr_queued(struct throtl_data *td)
145 return (td->nr_queued[0] + td->nr_queued[1]);
148 static inline struct throtl_grp *throtl_ref_get_tg(struct throtl_grp *tg)
150 atomic_inc(&tg->ref);
151 return tg;
154 static void throtl_put_tg(struct throtl_grp *tg)
156 BUG_ON(atomic_read(&tg->ref) <= 0);
157 if (!atomic_dec_and_test(&tg->ref))
158 return;
159 kfree(tg);
162 static struct throtl_grp * throtl_find_alloc_tg(struct throtl_data *td,
163 struct cgroup *cgroup)
165 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
166 struct throtl_grp *tg = NULL;
167 void *key = td;
168 struct backing_dev_info *bdi = &td->queue->backing_dev_info;
169 unsigned int major, minor;
172 * TODO: Speed up blkiocg_lookup_group() by maintaining a radix
173 * tree of blkg (instead of traversing through hash list all
174 * the time.
176 tg = tg_of_blkg(blkiocg_lookup_group(blkcg, key));
178 /* Fill in device details for root group */
179 if (tg && !tg->blkg.dev && bdi->dev && dev_name(bdi->dev)) {
180 sscanf(dev_name(bdi->dev), "%u:%u", &major, &minor);
181 tg->blkg.dev = MKDEV(major, minor);
182 goto done;
185 if (tg)
186 goto done;
188 tg = kzalloc_node(sizeof(*tg), GFP_ATOMIC, td->queue->node);
189 if (!tg)
190 goto done;
192 INIT_HLIST_NODE(&tg->tg_node);
193 RB_CLEAR_NODE(&tg->rb_node);
194 bio_list_init(&tg->bio_lists[0]);
195 bio_list_init(&tg->bio_lists[1]);
198 * Take the initial reference that will be released on destroy
199 * This can be thought of a joint reference by cgroup and
200 * request queue which will be dropped by either request queue
201 * exit or cgroup deletion path depending on who is exiting first.
203 atomic_set(&tg->ref, 1);
205 /* Add group onto cgroup list */
206 sscanf(dev_name(bdi->dev), "%u:%u", &major, &minor);
207 blkiocg_add_blkio_group(blkcg, &tg->blkg, (void *)td,
208 MKDEV(major, minor), BLKIO_POLICY_THROTL);
210 tg->bps[READ] = blkcg_get_read_bps(blkcg, tg->blkg.dev);
211 tg->bps[WRITE] = blkcg_get_write_bps(blkcg, tg->blkg.dev);
212 tg->iops[READ] = blkcg_get_read_iops(blkcg, tg->blkg.dev);
213 tg->iops[WRITE] = blkcg_get_write_iops(blkcg, tg->blkg.dev);
215 hlist_add_head(&tg->tg_node, &td->tg_list);
216 td->nr_undestroyed_grps++;
217 done:
218 return tg;
221 static struct throtl_grp * throtl_get_tg(struct throtl_data *td)
223 struct cgroup *cgroup;
224 struct throtl_grp *tg = NULL;
226 rcu_read_lock();
227 cgroup = task_cgroup(current, blkio_subsys_id);
228 tg = throtl_find_alloc_tg(td, cgroup);
229 if (!tg)
230 tg = &td->root_tg;
231 rcu_read_unlock();
232 return tg;
235 static struct throtl_grp *throtl_rb_first(struct throtl_rb_root *root)
237 /* Service tree is empty */
238 if (!root->count)
239 return NULL;
241 if (!root->left)
242 root->left = rb_first(&root->rb);
244 if (root->left)
245 return rb_entry_tg(root->left);
247 return NULL;
250 static void rb_erase_init(struct rb_node *n, struct rb_root *root)
252 rb_erase(n, root);
253 RB_CLEAR_NODE(n);
256 static void throtl_rb_erase(struct rb_node *n, struct throtl_rb_root *root)
258 if (root->left == n)
259 root->left = NULL;
260 rb_erase_init(n, &root->rb);
261 --root->count;
264 static void update_min_dispatch_time(struct throtl_rb_root *st)
266 struct throtl_grp *tg;
268 tg = throtl_rb_first(st);
269 if (!tg)
270 return;
272 st->min_disptime = tg->disptime;
275 static void
276 tg_service_tree_add(struct throtl_rb_root *st, struct throtl_grp *tg)
278 struct rb_node **node = &st->rb.rb_node;
279 struct rb_node *parent = NULL;
280 struct throtl_grp *__tg;
281 unsigned long key = tg->disptime;
282 int left = 1;
284 while (*node != NULL) {
285 parent = *node;
286 __tg = rb_entry_tg(parent);
288 if (time_before(key, __tg->disptime))
289 node = &parent->rb_left;
290 else {
291 node = &parent->rb_right;
292 left = 0;
296 if (left)
297 st->left = &tg->rb_node;
299 rb_link_node(&tg->rb_node, parent, node);
300 rb_insert_color(&tg->rb_node, &st->rb);
303 static void __throtl_enqueue_tg(struct throtl_data *td, struct throtl_grp *tg)
305 struct throtl_rb_root *st = &td->tg_service_tree;
307 tg_service_tree_add(st, tg);
308 throtl_mark_tg_on_rr(tg);
309 st->count++;
312 static void throtl_enqueue_tg(struct throtl_data *td, struct throtl_grp *tg)
314 if (!throtl_tg_on_rr(tg))
315 __throtl_enqueue_tg(td, tg);
318 static void __throtl_dequeue_tg(struct throtl_data *td, struct throtl_grp *tg)
320 throtl_rb_erase(&tg->rb_node, &td->tg_service_tree);
321 throtl_clear_tg_on_rr(tg);
324 static void throtl_dequeue_tg(struct throtl_data *td, struct throtl_grp *tg)
326 if (throtl_tg_on_rr(tg))
327 __throtl_dequeue_tg(td, tg);
330 static void throtl_schedule_next_dispatch(struct throtl_data *td)
332 struct throtl_rb_root *st = &td->tg_service_tree;
335 * If there are more bios pending, schedule more work.
337 if (!total_nr_queued(td))
338 return;
340 BUG_ON(!st->count);
342 update_min_dispatch_time(st);
344 if (time_before_eq(st->min_disptime, jiffies))
345 throtl_schedule_delayed_work(td, 0);
346 else
347 throtl_schedule_delayed_work(td, (st->min_disptime - jiffies));
350 static inline void
351 throtl_start_new_slice(struct throtl_data *td, struct throtl_grp *tg, bool rw)
353 tg->bytes_disp[rw] = 0;
354 tg->io_disp[rw] = 0;
355 tg->slice_start[rw] = jiffies;
356 tg->slice_end[rw] = jiffies + throtl_slice;
357 throtl_log_tg(td, tg, "[%c] new slice start=%lu end=%lu jiffies=%lu",
358 rw == READ ? 'R' : 'W', tg->slice_start[rw],
359 tg->slice_end[rw], jiffies);
362 static inline void throtl_set_slice_end(struct throtl_data *td,
363 struct throtl_grp *tg, bool rw, unsigned long jiffy_end)
365 tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
368 static inline void throtl_extend_slice(struct throtl_data *td,
369 struct throtl_grp *tg, bool rw, unsigned long jiffy_end)
371 tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
372 throtl_log_tg(td, tg, "[%c] extend slice start=%lu end=%lu jiffies=%lu",
373 rw == READ ? 'R' : 'W', tg->slice_start[rw],
374 tg->slice_end[rw], jiffies);
377 /* Determine if previously allocated or extended slice is complete or not */
378 static bool
379 throtl_slice_used(struct throtl_data *td, struct throtl_grp *tg, bool rw)
381 if (time_in_range(jiffies, tg->slice_start[rw], tg->slice_end[rw]))
382 return 0;
384 return 1;
387 /* Trim the used slices and adjust slice start accordingly */
388 static inline void
389 throtl_trim_slice(struct throtl_data *td, struct throtl_grp *tg, bool rw)
391 unsigned long nr_slices, time_elapsed, io_trim;
392 u64 bytes_trim, tmp;
394 BUG_ON(time_before(tg->slice_end[rw], tg->slice_start[rw]));
397 * If bps are unlimited (-1), then time slice don't get
398 * renewed. Don't try to trim the slice if slice is used. A new
399 * slice will start when appropriate.
401 if (throtl_slice_used(td, tg, rw))
402 return;
405 * A bio has been dispatched. Also adjust slice_end. It might happen
406 * that initially cgroup limit was very low resulting in high
407 * slice_end, but later limit was bumped up and bio was dispached
408 * sooner, then we need to reduce slice_end. A high bogus slice_end
409 * is bad because it does not allow new slice to start.
412 throtl_set_slice_end(td, tg, rw, jiffies + throtl_slice);
414 time_elapsed = jiffies - tg->slice_start[rw];
416 nr_slices = time_elapsed / throtl_slice;
418 if (!nr_slices)
419 return;
420 tmp = tg->bps[rw] * throtl_slice * nr_slices;
421 do_div(tmp, HZ);
422 bytes_trim = tmp;
424 io_trim = (tg->iops[rw] * throtl_slice * nr_slices)/HZ;
426 if (!bytes_trim && !io_trim)
427 return;
429 if (tg->bytes_disp[rw] >= bytes_trim)
430 tg->bytes_disp[rw] -= bytes_trim;
431 else
432 tg->bytes_disp[rw] = 0;
434 if (tg->io_disp[rw] >= io_trim)
435 tg->io_disp[rw] -= io_trim;
436 else
437 tg->io_disp[rw] = 0;
439 tg->slice_start[rw] += nr_slices * throtl_slice;
441 throtl_log_tg(td, tg, "[%c] trim slice nr=%lu bytes=%llu io=%lu"
442 " start=%lu end=%lu jiffies=%lu",
443 rw == READ ? 'R' : 'W', nr_slices, bytes_trim, io_trim,
444 tg->slice_start[rw], tg->slice_end[rw], jiffies);
447 static bool tg_with_in_iops_limit(struct throtl_data *td, struct throtl_grp *tg,
448 struct bio *bio, unsigned long *wait)
450 bool rw = bio_data_dir(bio);
451 unsigned int io_allowed;
452 unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
453 u64 tmp;
455 jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
457 /* Slice has just started. Consider one slice interval */
458 if (!jiffy_elapsed)
459 jiffy_elapsed_rnd = throtl_slice;
461 jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
464 * jiffy_elapsed_rnd should not be a big value as minimum iops can be
465 * 1 then at max jiffy elapsed should be equivalent of 1 second as we
466 * will allow dispatch after 1 second and after that slice should
467 * have been trimmed.
470 tmp = (u64)tg->iops[rw] * jiffy_elapsed_rnd;
471 do_div(tmp, HZ);
473 if (tmp > UINT_MAX)
474 io_allowed = UINT_MAX;
475 else
476 io_allowed = tmp;
478 if (tg->io_disp[rw] + 1 <= io_allowed) {
479 if (wait)
480 *wait = 0;
481 return 1;
484 /* Calc approx time to dispatch */
485 jiffy_wait = ((tg->io_disp[rw] + 1) * HZ)/tg->iops[rw] + 1;
487 if (jiffy_wait > jiffy_elapsed)
488 jiffy_wait = jiffy_wait - jiffy_elapsed;
489 else
490 jiffy_wait = 1;
492 if (wait)
493 *wait = jiffy_wait;
494 return 0;
497 static bool tg_with_in_bps_limit(struct throtl_data *td, struct throtl_grp *tg,
498 struct bio *bio, unsigned long *wait)
500 bool rw = bio_data_dir(bio);
501 u64 bytes_allowed, extra_bytes, tmp;
502 unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
504 jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
506 /* Slice has just started. Consider one slice interval */
507 if (!jiffy_elapsed)
508 jiffy_elapsed_rnd = throtl_slice;
510 jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
512 tmp = tg->bps[rw] * jiffy_elapsed_rnd;
513 do_div(tmp, HZ);
514 bytes_allowed = tmp;
516 if (tg->bytes_disp[rw] + bio->bi_size <= bytes_allowed) {
517 if (wait)
518 *wait = 0;
519 return 1;
522 /* Calc approx time to dispatch */
523 extra_bytes = tg->bytes_disp[rw] + bio->bi_size - bytes_allowed;
524 jiffy_wait = div64_u64(extra_bytes * HZ, tg->bps[rw]);
526 if (!jiffy_wait)
527 jiffy_wait = 1;
530 * This wait time is without taking into consideration the rounding
531 * up we did. Add that time also.
533 jiffy_wait = jiffy_wait + (jiffy_elapsed_rnd - jiffy_elapsed);
534 if (wait)
535 *wait = jiffy_wait;
536 return 0;
540 * Returns whether one can dispatch a bio or not. Also returns approx number
541 * of jiffies to wait before this bio is with-in IO rate and can be dispatched
543 static bool tg_may_dispatch(struct throtl_data *td, struct throtl_grp *tg,
544 struct bio *bio, unsigned long *wait)
546 bool rw = bio_data_dir(bio);
547 unsigned long bps_wait = 0, iops_wait = 0, max_wait = 0;
550 * Currently whole state machine of group depends on first bio
551 * queued in the group bio list. So one should not be calling
552 * this function with a different bio if there are other bios
553 * queued.
555 BUG_ON(tg->nr_queued[rw] && bio != bio_list_peek(&tg->bio_lists[rw]));
557 /* If tg->bps = -1, then BW is unlimited */
558 if (tg->bps[rw] == -1 && tg->iops[rw] == -1) {
559 if (wait)
560 *wait = 0;
561 return 1;
565 * If previous slice expired, start a new one otherwise renew/extend
566 * existing slice to make sure it is at least throtl_slice interval
567 * long since now.
569 if (throtl_slice_used(td, tg, rw))
570 throtl_start_new_slice(td, tg, rw);
571 else {
572 if (time_before(tg->slice_end[rw], jiffies + throtl_slice))
573 throtl_extend_slice(td, tg, rw, jiffies + throtl_slice);
576 if (tg_with_in_bps_limit(td, tg, bio, &bps_wait)
577 && tg_with_in_iops_limit(td, tg, bio, &iops_wait)) {
578 if (wait)
579 *wait = 0;
580 return 1;
583 max_wait = max(bps_wait, iops_wait);
585 if (wait)
586 *wait = max_wait;
588 if (time_before(tg->slice_end[rw], jiffies + max_wait))
589 throtl_extend_slice(td, tg, rw, jiffies + max_wait);
591 return 0;
594 static void throtl_charge_bio(struct throtl_grp *tg, struct bio *bio)
596 bool rw = bio_data_dir(bio);
597 bool sync = bio->bi_rw & REQ_SYNC;
599 /* Charge the bio to the group */
600 tg->bytes_disp[rw] += bio->bi_size;
601 tg->io_disp[rw]++;
604 * TODO: This will take blkg->stats_lock. Figure out a way
605 * to avoid this cost.
607 blkiocg_update_dispatch_stats(&tg->blkg, bio->bi_size, rw, sync);
610 static void throtl_add_bio_tg(struct throtl_data *td, struct throtl_grp *tg,
611 struct bio *bio)
613 bool rw = bio_data_dir(bio);
615 bio_list_add(&tg->bio_lists[rw], bio);
616 /* Take a bio reference on tg */
617 throtl_ref_get_tg(tg);
618 tg->nr_queued[rw]++;
619 td->nr_queued[rw]++;
620 throtl_enqueue_tg(td, tg);
623 static void tg_update_disptime(struct throtl_data *td, struct throtl_grp *tg)
625 unsigned long read_wait = -1, write_wait = -1, min_wait = -1, disptime;
626 struct bio *bio;
628 if ((bio = bio_list_peek(&tg->bio_lists[READ])))
629 tg_may_dispatch(td, tg, bio, &read_wait);
631 if ((bio = bio_list_peek(&tg->bio_lists[WRITE])))
632 tg_may_dispatch(td, tg, bio, &write_wait);
634 min_wait = min(read_wait, write_wait);
635 disptime = jiffies + min_wait;
637 /* Update dispatch time */
638 throtl_dequeue_tg(td, tg);
639 tg->disptime = disptime;
640 throtl_enqueue_tg(td, tg);
643 static void tg_dispatch_one_bio(struct throtl_data *td, struct throtl_grp *tg,
644 bool rw, struct bio_list *bl)
646 struct bio *bio;
648 bio = bio_list_pop(&tg->bio_lists[rw]);
649 tg->nr_queued[rw]--;
650 /* Drop bio reference on tg */
651 throtl_put_tg(tg);
653 BUG_ON(td->nr_queued[rw] <= 0);
654 td->nr_queued[rw]--;
656 throtl_charge_bio(tg, bio);
657 bio_list_add(bl, bio);
658 bio->bi_rw |= REQ_THROTTLED;
660 throtl_trim_slice(td, tg, rw);
663 static int throtl_dispatch_tg(struct throtl_data *td, struct throtl_grp *tg,
664 struct bio_list *bl)
666 unsigned int nr_reads = 0, nr_writes = 0;
667 unsigned int max_nr_reads = throtl_grp_quantum*3/4;
668 unsigned int max_nr_writes = throtl_grp_quantum - max_nr_reads;
669 struct bio *bio;
671 /* Try to dispatch 75% READS and 25% WRITES */
673 while ((bio = bio_list_peek(&tg->bio_lists[READ]))
674 && tg_may_dispatch(td, tg, bio, NULL)) {
676 tg_dispatch_one_bio(td, tg, bio_data_dir(bio), bl);
677 nr_reads++;
679 if (nr_reads >= max_nr_reads)
680 break;
683 while ((bio = bio_list_peek(&tg->bio_lists[WRITE]))
684 && tg_may_dispatch(td, tg, bio, NULL)) {
686 tg_dispatch_one_bio(td, tg, bio_data_dir(bio), bl);
687 nr_writes++;
689 if (nr_writes >= max_nr_writes)
690 break;
693 return nr_reads + nr_writes;
696 static int throtl_select_dispatch(struct throtl_data *td, struct bio_list *bl)
698 unsigned int nr_disp = 0;
699 struct throtl_grp *tg;
700 struct throtl_rb_root *st = &td->tg_service_tree;
702 while (1) {
703 tg = throtl_rb_first(st);
705 if (!tg)
706 break;
708 if (time_before(jiffies, tg->disptime))
709 break;
711 throtl_dequeue_tg(td, tg);
713 nr_disp += throtl_dispatch_tg(td, tg, bl);
715 if (tg->nr_queued[0] || tg->nr_queued[1]) {
716 tg_update_disptime(td, tg);
717 throtl_enqueue_tg(td, tg);
720 if (nr_disp >= throtl_quantum)
721 break;
724 return nr_disp;
727 static void throtl_process_limit_change(struct throtl_data *td)
729 struct throtl_grp *tg;
730 struct hlist_node *pos, *n;
732 if (!atomic_read(&td->limits_changed))
733 return;
735 throtl_log(td, "limit changed =%d", atomic_read(&td->limits_changed));
738 * Make sure updates from throtl_update_blkio_group_read_bps() group
739 * of functions to tg->limits_changed are visible. We do not
740 * want update td->limits_changed to be visible but update to
741 * tg->limits_changed not being visible yet on this cpu. Hence
742 * the read barrier.
744 smp_rmb();
746 hlist_for_each_entry_safe(tg, pos, n, &td->tg_list, tg_node) {
747 if (throtl_tg_on_rr(tg) && tg->limits_changed) {
748 throtl_log_tg(td, tg, "limit change rbps=%llu wbps=%llu"
749 " riops=%u wiops=%u", tg->bps[READ],
750 tg->bps[WRITE], tg->iops[READ],
751 tg->iops[WRITE]);
752 tg_update_disptime(td, tg);
753 tg->limits_changed = false;
757 smp_mb__before_atomic_dec();
758 atomic_dec(&td->limits_changed);
759 smp_mb__after_atomic_dec();
762 /* Dispatch throttled bios. Should be called without queue lock held. */
763 static int throtl_dispatch(struct request_queue *q)
765 struct throtl_data *td = q->td;
766 unsigned int nr_disp = 0;
767 struct bio_list bio_list_on_stack;
768 struct bio *bio;
770 spin_lock_irq(q->queue_lock);
772 throtl_process_limit_change(td);
774 if (!total_nr_queued(td))
775 goto out;
777 bio_list_init(&bio_list_on_stack);
779 throtl_log(td, "dispatch nr_queued=%lu read=%u write=%u",
780 total_nr_queued(td), td->nr_queued[READ],
781 td->nr_queued[WRITE]);
783 nr_disp = throtl_select_dispatch(td, &bio_list_on_stack);
785 if (nr_disp)
786 throtl_log(td, "bios disp=%u", nr_disp);
788 throtl_schedule_next_dispatch(td);
789 out:
790 spin_unlock_irq(q->queue_lock);
793 * If we dispatched some requests, unplug the queue to make sure
794 * immediate dispatch
796 if (nr_disp) {
797 while((bio = bio_list_pop(&bio_list_on_stack)))
798 generic_make_request(bio);
799 blk_unplug(q);
801 return nr_disp;
804 void blk_throtl_work(struct work_struct *work)
806 struct throtl_data *td = container_of(work, struct throtl_data,
807 throtl_work.work);
808 struct request_queue *q = td->queue;
810 throtl_dispatch(q);
813 /* Call with queue lock held */
814 static void
815 throtl_schedule_delayed_work(struct throtl_data *td, unsigned long delay)
818 struct delayed_work *dwork = &td->throtl_work;
820 if (total_nr_queued(td) > 0) {
822 * We might have a work scheduled to be executed in future.
823 * Cancel that and schedule a new one.
825 __cancel_delayed_work(dwork);
826 queue_delayed_work(kthrotld_workqueue, dwork, delay);
827 throtl_log(td, "schedule work. delay=%lu jiffies=%lu",
828 delay, jiffies);
832 static void
833 throtl_destroy_tg(struct throtl_data *td, struct throtl_grp *tg)
835 /* Something wrong if we are trying to remove same group twice */
836 BUG_ON(hlist_unhashed(&tg->tg_node));
838 hlist_del_init(&tg->tg_node);
841 * Put the reference taken at the time of creation so that when all
842 * queues are gone, group can be destroyed.
844 throtl_put_tg(tg);
845 td->nr_undestroyed_grps--;
848 static void throtl_release_tgs(struct throtl_data *td)
850 struct hlist_node *pos, *n;
851 struct throtl_grp *tg;
853 hlist_for_each_entry_safe(tg, pos, n, &td->tg_list, tg_node) {
855 * If cgroup removal path got to blk_group first and removed
856 * it from cgroup list, then it will take care of destroying
857 * cfqg also.
859 if (!blkiocg_del_blkio_group(&tg->blkg))
860 throtl_destroy_tg(td, tg);
864 static void throtl_td_free(struct throtl_data *td)
866 kfree(td);
870 * Blk cgroup controller notification saying that blkio_group object is being
871 * delinked as associated cgroup object is going away. That also means that
872 * no new IO will come in this group. So get rid of this group as soon as
873 * any pending IO in the group is finished.
875 * This function is called under rcu_read_lock(). key is the rcu protected
876 * pointer. That means "key" is a valid throtl_data pointer as long as we are
877 * rcu read lock.
879 * "key" was fetched from blkio_group under blkio_cgroup->lock. That means
880 * it should not be NULL as even if queue was going away, cgroup deltion
881 * path got to it first.
883 void throtl_unlink_blkio_group(void *key, struct blkio_group *blkg)
885 unsigned long flags;
886 struct throtl_data *td = key;
888 spin_lock_irqsave(td->queue->queue_lock, flags);
889 throtl_destroy_tg(td, tg_of_blkg(blkg));
890 spin_unlock_irqrestore(td->queue->queue_lock, flags);
894 * For all update functions, key should be a valid pointer because these
895 * update functions are called under blkcg_lock, that means, blkg is
896 * valid and in turn key is valid. queue exit path can not race becuase
897 * of blkcg_lock
899 * Can not take queue lock in update functions as queue lock under blkcg_lock
900 * is not allowed. Under other paths we take blkcg_lock under queue_lock.
902 static void throtl_update_blkio_group_read_bps(void *key,
903 struct blkio_group *blkg, u64 read_bps)
905 struct throtl_data *td = key;
907 tg_of_blkg(blkg)->bps[READ] = read_bps;
908 /* Make sure read_bps is updated before setting limits_changed */
909 smp_wmb();
910 tg_of_blkg(blkg)->limits_changed = true;
912 /* Make sure tg->limits_changed is updated before td->limits_changed */
913 smp_mb__before_atomic_inc();
914 atomic_inc(&td->limits_changed);
915 smp_mb__after_atomic_inc();
917 /* Schedule a work now to process the limit change */
918 throtl_schedule_delayed_work(td, 0);
921 static void throtl_update_blkio_group_write_bps(void *key,
922 struct blkio_group *blkg, u64 write_bps)
924 struct throtl_data *td = key;
926 tg_of_blkg(blkg)->bps[WRITE] = write_bps;
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, 0);
935 static void throtl_update_blkio_group_read_iops(void *key,
936 struct blkio_group *blkg, unsigned int read_iops)
938 struct throtl_data *td = key;
940 tg_of_blkg(blkg)->iops[READ] = read_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, 0);
949 static void throtl_update_blkio_group_write_iops(void *key,
950 struct blkio_group *blkg, unsigned int write_iops)
952 struct throtl_data *td = key;
954 tg_of_blkg(blkg)->iops[WRITE] = write_iops;
955 smp_wmb();
956 tg_of_blkg(blkg)->limits_changed = true;
957 smp_mb__before_atomic_inc();
958 atomic_inc(&td->limits_changed);
959 smp_mb__after_atomic_inc();
960 throtl_schedule_delayed_work(td, 0);
963 void throtl_shutdown_timer_wq(struct request_queue *q)
965 struct throtl_data *td = q->td;
967 cancel_delayed_work_sync(&td->throtl_work);
970 static struct blkio_policy_type blkio_policy_throtl = {
971 .ops = {
972 .blkio_unlink_group_fn = throtl_unlink_blkio_group,
973 .blkio_update_group_read_bps_fn =
974 throtl_update_blkio_group_read_bps,
975 .blkio_update_group_write_bps_fn =
976 throtl_update_blkio_group_write_bps,
977 .blkio_update_group_read_iops_fn =
978 throtl_update_blkio_group_read_iops,
979 .blkio_update_group_write_iops_fn =
980 throtl_update_blkio_group_write_iops,
982 .plid = BLKIO_POLICY_THROTL,
985 int blk_throtl_bio(struct request_queue *q, struct bio **biop)
987 struct throtl_data *td = q->td;
988 struct throtl_grp *tg;
989 struct bio *bio = *biop;
990 bool rw = bio_data_dir(bio), update_disptime = true;
992 if (bio->bi_rw & REQ_THROTTLED) {
993 bio->bi_rw &= ~REQ_THROTTLED;
994 return 0;
997 spin_lock_irq(q->queue_lock);
998 tg = throtl_get_tg(td);
1000 if (tg->nr_queued[rw]) {
1002 * There is already another bio queued in same dir. No
1003 * need to update dispatch time.
1004 * Still update the disptime if rate limits on this group
1005 * were changed.
1007 if (!tg->limits_changed)
1008 update_disptime = false;
1009 else
1010 tg->limits_changed = false;
1012 goto queue_bio;
1015 /* Bio is with-in rate limit of group */
1016 if (tg_may_dispatch(td, tg, bio, NULL)) {
1017 throtl_charge_bio(tg, bio);
1018 goto out;
1021 queue_bio:
1022 throtl_log_tg(td, tg, "[%c] bio. bdisp=%u sz=%u bps=%llu"
1023 " iodisp=%u iops=%u queued=%d/%d",
1024 rw == READ ? 'R' : 'W',
1025 tg->bytes_disp[rw], bio->bi_size, tg->bps[rw],
1026 tg->io_disp[rw], tg->iops[rw],
1027 tg->nr_queued[READ], tg->nr_queued[WRITE]);
1029 throtl_add_bio_tg(q->td, tg, bio);
1030 *biop = NULL;
1032 if (update_disptime) {
1033 tg_update_disptime(td, tg);
1034 throtl_schedule_next_dispatch(td);
1037 out:
1038 spin_unlock_irq(q->queue_lock);
1039 return 0;
1042 int blk_throtl_init(struct request_queue *q)
1044 struct throtl_data *td;
1045 struct throtl_grp *tg;
1047 td = kzalloc_node(sizeof(*td), GFP_KERNEL, q->node);
1048 if (!td)
1049 return -ENOMEM;
1051 INIT_HLIST_HEAD(&td->tg_list);
1052 td->tg_service_tree = THROTL_RB_ROOT;
1053 atomic_set(&td->limits_changed, 0);
1055 /* Init root group */
1056 tg = &td->root_tg;
1057 INIT_HLIST_NODE(&tg->tg_node);
1058 RB_CLEAR_NODE(&tg->rb_node);
1059 bio_list_init(&tg->bio_lists[0]);
1060 bio_list_init(&tg->bio_lists[1]);
1062 /* Practically unlimited BW */
1063 tg->bps[0] = tg->bps[1] = -1;
1064 tg->iops[0] = tg->iops[1] = -1;
1067 * Set root group reference to 2. One reference will be dropped when
1068 * all groups on tg_list are being deleted during queue exit. Other
1069 * reference will remain there as we don't want to delete this group
1070 * as it is statically allocated and gets destroyed when throtl_data
1071 * goes away.
1073 atomic_set(&tg->ref, 2);
1074 hlist_add_head(&tg->tg_node, &td->tg_list);
1075 td->nr_undestroyed_grps++;
1077 INIT_DELAYED_WORK(&td->throtl_work, blk_throtl_work);
1079 rcu_read_lock();
1080 blkiocg_add_blkio_group(&blkio_root_cgroup, &tg->blkg, (void *)td,
1081 0, BLKIO_POLICY_THROTL);
1082 rcu_read_unlock();
1084 /* Attach throtl data to request queue */
1085 td->queue = q;
1086 q->td = td;
1087 return 0;
1090 void blk_throtl_exit(struct request_queue *q)
1092 struct throtl_data *td = q->td;
1093 bool wait = false;
1095 BUG_ON(!td);
1097 throtl_shutdown_timer_wq(q);
1099 spin_lock_irq(q->queue_lock);
1100 throtl_release_tgs(td);
1102 /* If there are other groups */
1103 if (td->nr_undestroyed_grps > 0)
1104 wait = true;
1106 spin_unlock_irq(q->queue_lock);
1109 * Wait for tg->blkg->key accessors to exit their grace periods.
1110 * Do this wait only if there are other undestroyed groups out
1111 * there (other than root group). This can happen if cgroup deletion
1112 * path claimed the responsibility of cleaning up a group before
1113 * queue cleanup code get to the group.
1115 * Do not call synchronize_rcu() unconditionally as there are drivers
1116 * which create/delete request queue hundreds of times during scan/boot
1117 * and synchronize_rcu() can take significant time and slow down boot.
1119 if (wait)
1120 synchronize_rcu();
1123 * Just being safe to make sure after previous flush if some body did
1124 * update limits through cgroup and another work got queued, cancel
1125 * it.
1127 throtl_shutdown_timer_wq(q);
1128 throtl_td_free(td);
1131 static int __init throtl_init(void)
1133 kthrotld_workqueue = alloc_workqueue("kthrotld", WQ_MEM_RECLAIM, 0);
1134 if (!kthrotld_workqueue)
1135 panic("Failed to create kthrotld\n");
1137 blkio_policy_register(&blkio_policy_throtl);
1138 return 0;
1141 module_init(throtl_init);