blob: a29f09240c0faa6cae5aa37a229959a6c801089f [file] [log] [blame]
Vivek Goyale43473b2010-09-15 17:06:35 -04001/*
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"
13
14/* Max dispatch from a group in 1 round */
15static int throtl_grp_quantum = 8;
16
17/* Total max dispatch from all groups in one round */
18static int throtl_quantum = 32;
19
20/* Throttling is performed over 100ms slice and after that slice is renewed */
21static unsigned long throtl_slice = HZ/10; /* 100 ms */
22
Vivek Goyal450adcb2011-03-01 13:40:54 -050023/* A workqueue to queue throttle related work */
24static struct workqueue_struct *kthrotld_workqueue;
25static void throtl_schedule_delayed_work(struct throtl_data *td,
26 unsigned long delay);
27
Vivek Goyale43473b2010-09-15 17:06:35 -040028struct 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 /* List of throtl groups on the request queue*/
42 struct hlist_node tg_node;
43
44 /* active throtl group service_tree member */
45 struct rb_node rb_node;
46
47 /*
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.
51 */
52 unsigned long disptime;
53
54 struct blkio_group blkg;
55 atomic_t ref;
56 unsigned int flags;
57
58 /* Two lists for READ and WRITE */
59 struct bio_list bio_lists[2];
60
61 /* Number of queued bios on READ and WRITE lists */
62 unsigned int nr_queued[2];
63
64 /* bytes per second rate limits */
65 uint64_t bps[2];
66
Vivek Goyal8e89d132010-09-15 17:06:37 -040067 /* IOPS limits */
68 unsigned int iops[2];
69
Vivek Goyale43473b2010-09-15 17:06:35 -040070 /* Number of bytes disptached in current slice */
71 uint64_t bytes_disp[2];
Vivek Goyal8e89d132010-09-15 17:06:37 -040072 /* Number of bio's dispatched in current slice */
73 unsigned int io_disp[2];
Vivek Goyale43473b2010-09-15 17:06:35 -040074
75 /* When did we start a new slice */
76 unsigned long slice_start[2];
77 unsigned long slice_end[2];
Vivek Goyalfe071432010-10-01 14:49:49 +020078
79 /* Some throttle limits got updated for the group */
80 bool limits_changed;
Vivek Goyale43473b2010-09-15 17:06:35 -040081};
82
83struct throtl_data
84{
85 /* List of throtl groups */
86 struct hlist_head tg_list;
87
88 /* service tree for active throtl groups */
89 struct throtl_rb_root tg_service_tree;
90
91 struct throtl_grp root_tg;
92 struct request_queue *queue;
93
94 /* Total Number of queued bios on READ and WRITE lists */
95 unsigned int nr_queued[2];
96
97 /*
Vivek Goyal02977e42010-10-01 14:49:48 +020098 * number of total undestroyed groups
Vivek Goyale43473b2010-09-15 17:06:35 -040099 */
100 unsigned int nr_undestroyed_grps;
101
102 /* Work for dispatching throttled bios */
103 struct delayed_work throtl_work;
Vivek Goyalfe071432010-10-01 14:49:49 +0200104
105 atomic_t limits_changed;
Vivek Goyale43473b2010-09-15 17:06:35 -0400106};
107
108enum tg_state_flags {
109 THROTL_TG_FLAG_on_rr = 0, /* on round-robin busy list */
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 cgroup *cgroup)
164{
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;
170
171 /*
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.
175 */
Vivek Goyalbe2c6b12011-01-19 08:25:02 -0700176
177 /*
178 * This is the common case when there are no blkio cgroups.
179 * Avoid lookup in this case
180 */
181 if (blkcg == &blkio_root_cgroup)
182 tg = &td->root_tg;
183 else
184 tg = tg_of_blkg(blkiocg_lookup_group(blkcg, key));
Vivek Goyale43473b2010-09-15 17:06:35 -0400185
186 /* Fill in device details for root group */
187 if (tg && !tg->blkg.dev && bdi->dev && dev_name(bdi->dev)) {
188 sscanf(dev_name(bdi->dev), "%u:%u", &major, &minor);
189 tg->blkg.dev = MKDEV(major, minor);
190 goto done;
191 }
192
193 if (tg)
194 goto done;
195
196 tg = kzalloc_node(sizeof(*tg), GFP_ATOMIC, td->queue->node);
197 if (!tg)
198 goto done;
199
200 INIT_HLIST_NODE(&tg->tg_node);
201 RB_CLEAR_NODE(&tg->rb_node);
202 bio_list_init(&tg->bio_lists[0]);
203 bio_list_init(&tg->bio_lists[1]);
204
205 /*
206 * Take the initial reference that will be released on destroy
207 * This can be thought of a joint reference by cgroup and
208 * request queue which will be dropped by either request queue
209 * exit or cgroup deletion path depending on who is exiting first.
210 */
211 atomic_set(&tg->ref, 1);
212
213 /* Add group onto cgroup list */
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);
Vivek Goyal8e89d132010-09-15 17:06:37 -0400220 tg->iops[READ] = blkcg_get_read_iops(blkcg, tg->blkg.dev);
221 tg->iops[WRITE] = blkcg_get_write_iops(blkcg, tg->blkg.dev);
Vivek Goyale43473b2010-09-15 17:06:35 -0400222
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 cgroup *cgroup;
232 struct throtl_grp *tg = NULL;
233
234 rcu_read_lock();
235 cgroup = task_cgroup(current, blkio_subsys_id);
236 tg = throtl_find_alloc_tg(td, cgroup);
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 /* Service tree is empty */
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 * If there are more bios pending, schedule more work.
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))
Vivek Goyal450adcb2011-03-01 13:40:54 -0500353 throtl_schedule_delayed_work(td, 0);
Vivek Goyale43473b2010-09-15 17:06:35 -0400354 else
Vivek Goyal450adcb2011-03-01 13:40:54 -0500355 throtl_schedule_delayed_work(td, (st->min_disptime - jiffies));
Vivek Goyale43473b2010-09-15 17:06:35 -0400356}
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;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400362 tg->io_disp[rw] = 0;
Vivek Goyale43473b2010-09-15 17:06:35 -0400363 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
Vivek Goyald1ae8ff2010-12-01 19:34:46 +0100370static 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
Vivek Goyale43473b2010-09-15 17:06:35 -0400376static 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/* Determine if previously allocated or extended slice is complete or not */
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/* Trim the used slices and adjust slice start accordingly */
396static inline void
397throtl_trim_slice(struct throtl_data *td, struct throtl_grp *tg, bool rw)
398{
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200399 unsigned long nr_slices, time_elapsed, io_trim;
400 u64 bytes_trim, tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400401
402 BUG_ON(time_before(tg->slice_end[rw], tg->slice_start[rw]));
403
404 /*
405 * If bps are unlimited (-1), then time slice don't get
406 * renewed. Don't try to trim the slice if slice is used. A new
407 * slice will start when appropriate.
408 */
409 if (throtl_slice_used(td, tg, rw))
410 return;
411
Vivek Goyald1ae8ff2010-12-01 19:34:46 +0100412 /*
413 * A bio has been dispatched. Also adjust slice_end. It might happen
414 * that initially cgroup limit was very low resulting in high
415 * slice_end, but later limit was bumped up and bio was dispached
416 * sooner, then we need to reduce slice_end. A high bogus slice_end
417 * is bad because it does not allow new slice to start.
418 */
419
420 throtl_set_slice_end(td, tg, rw, jiffies + throtl_slice);
421
Vivek Goyale43473b2010-09-15 17:06:35 -0400422 time_elapsed = jiffies - tg->slice_start[rw];
423
424 nr_slices = time_elapsed / throtl_slice;
425
426 if (!nr_slices)
427 return;
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200428 tmp = tg->bps[rw] * throtl_slice * nr_slices;
429 do_div(tmp, HZ);
430 bytes_trim = tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400431
Vivek Goyal8e89d132010-09-15 17:06:37 -0400432 io_trim = (tg->iops[rw] * throtl_slice * nr_slices)/HZ;
Vivek Goyale43473b2010-09-15 17:06:35 -0400433
Vivek Goyal8e89d132010-09-15 17:06:37 -0400434 if (!bytes_trim && !io_trim)
Vivek Goyale43473b2010-09-15 17:06:35 -0400435 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
Vivek Goyal8e89d132010-09-15 17:06:37 -0400442 if (tg->io_disp[rw] >= io_trim)
443 tg->io_disp[rw] -= io_trim;
444 else
445 tg->io_disp[rw] = 0;
446
Vivek Goyale43473b2010-09-15 17:06:35 -0400447 tg->slice_start[rw] += nr_slices * throtl_slice;
448
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200449 throtl_log_tg(td, tg, "[%c] trim slice nr=%lu bytes=%llu io=%lu"
Vivek Goyale43473b2010-09-15 17:06:35 -0400450 " start=%lu end=%lu jiffies=%lu",
Vivek Goyal8e89d132010-09-15 17:06:37 -0400451 rw == READ ? 'R' : 'W', nr_slices, bytes_trim, io_trim,
Vivek Goyale43473b2010-09-15 17:06:35 -0400452 tg->slice_start[rw], tg->slice_end[rw], jiffies);
453}
454
Vivek Goyal8e89d132010-09-15 17:06:37 -0400455static bool tg_with_in_iops_limit(struct throtl_data *td, struct throtl_grp *tg,
456 struct bio *bio, unsigned long *wait)
Vivek Goyale43473b2010-09-15 17:06:35 -0400457{
458 bool rw = bio_data_dir(bio);
Vivek Goyal8e89d132010-09-15 17:06:37 -0400459 unsigned int io_allowed;
Vivek Goyale43473b2010-09-15 17:06:35 -0400460 unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
Vivek Goyalc49c06e2010-10-01 21:16:42 +0200461 u64 tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400462
Vivek Goyal8e89d132010-09-15 17:06:37 -0400463 jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
Vivek Goyale43473b2010-09-15 17:06:35 -0400464
Vivek Goyal8e89d132010-09-15 17:06:37 -0400465 /* Slice has just started. Consider one slice interval */
466 if (!jiffy_elapsed)
467 jiffy_elapsed_rnd = throtl_slice;
468
469 jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
470
Vivek Goyalc49c06e2010-10-01 21:16:42 +0200471 /*
472 * jiffy_elapsed_rnd should not be a big value as minimum iops can be
473 * 1 then at max jiffy elapsed should be equivalent of 1 second as we
474 * will allow dispatch after 1 second and after that slice should
475 * have been trimmed.
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;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400485
486 if (tg->io_disp[rw] + 1 <= io_allowed) {
Vivek Goyale43473b2010-09-15 17:06:35 -0400487 if (wait)
488 *wait = 0;
489 return 1;
490 }
491
Vivek Goyal8e89d132010-09-15 17:06:37 -0400492 /* Calc approx time to dispatch */
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);
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200509 u64 bytes_allowed, extra_bytes, tmp;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400510 unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
Vivek Goyale43473b2010-09-15 17:06:35 -0400511
512 jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
513
514 /* Slice has just started. Consider one slice interval */
515 if (!jiffy_elapsed)
516 jiffy_elapsed_rnd = throtl_slice;
517
518 jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
519
Vivek Goyal5e901a22010-10-01 21:16:38 +0200520 tmp = tg->bps[rw] * jiffy_elapsed_rnd;
521 do_div(tmp, HZ);
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200522 bytes_allowed = tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400523
524 if (tg->bytes_disp[rw] + bio->bi_size <= bytes_allowed) {
525 if (wait)
526 *wait = 0;
527 return 1;
528 }
529
530 /* Calc approx time to dispatch */
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 * This wait time is without taking into consideration the rounding
539 * up we did. Add that time also.
540 */
541 jiffy_wait = jiffy_wait + (jiffy_elapsed_rnd - jiffy_elapsed);
Vivek Goyale43473b2010-09-15 17:06:35 -0400542 if (wait)
543 *wait = jiffy_wait;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400544 return 0;
545}
Vivek Goyale43473b2010-09-15 17:06:35 -0400546
Vivek Goyal8e89d132010-09-15 17:06:37 -0400547/*
548 * Returns whether one can dispatch a bio or not. Also returns approx number
549 * of jiffies to wait before this bio is with-in IO rate and can be dispatched
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 * Currently whole state machine of group depends on first bio
559 * queued in the group bio list. So one should not be calling
560 * this function with a different bio if there are other bios
561 * queued.
562 */
563 BUG_ON(tg->nr_queued[rw] && bio != bio_list_peek(&tg->bio_lists[rw]));
564
565 /* If tg->bps = -1, then BW is unlimited */
566 if (tg->bps[rw] == -1 && tg->iops[rw] == -1) {
567 if (wait)
568 *wait = 0;
569 return 1;
570 }
571
572 /*
573 * If previous slice expired, start a new one otherwise renew/extend
574 * existing slice to make sure it is at least throtl_slice interval
575 * long since now.
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);
Vivek Goyale43473b2010-09-15 17:06:35 -0400598
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 /* Charge the bio to the group */
608 tg->bytes_disp[rw] += bio->bi_size;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400609 tg->io_disp[rw]++;
Vivek Goyale43473b2010-09-15 17:06:35 -0400610
611 /*
612 * TODO: This will take blkg->stats_lock. Figure out a way
613 * to avoid this cost.
614 */
615 blkiocg_update_dispatch_stats(&tg->blkg, bio->bi_size, rw, sync);
Vivek Goyale43473b2010-09-15 17:06:35 -0400616}
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 /* Take a bio reference on tg */
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
Vivek Goyale43473b2010-09-15 17:06:35 -0400645 /* Update dispatch time */
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 /* Drop bio reference on tg */
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;
Vivek Goyalc2f68052010-11-15 19:32:42 +0100676 unsigned int max_nr_writes = throtl_grp_quantum - max_nr_reads;
Vivek Goyale43473b2010-09-15 17:06:35 -0400677 struct bio *bio;
678
679 /* Try to dispatch 75% READS and 25% WRITES */
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
Vivek Goyalfe071432010-10-01 14:49:49 +0200735static void throtl_process_limit_change(struct throtl_data *td)
736{
737 struct throtl_grp *tg;
738 struct hlist_node *pos, *n;
739
Vivek Goyalfe071432010-10-01 14:49:49 +0200740 if (!atomic_read(&td->limits_changed))
741 return;
742
743 throtl_log(td, "limit changed =%d", atomic_read(&td->limits_changed));
744
Vivek Goyal04a6b512010-12-01 19:34:52 +0100745 /*
746 * Make sure updates from throtl_update_blkio_group_read_bps() group
747 * of functions to tg->limits_changed are visible. We do not
748 * want update td->limits_changed to be visible but update to
749 * tg->limits_changed not being visible yet on this cpu. Hence
750 * the read barrier.
751 */
752 smp_rmb();
Vivek Goyalfe071432010-10-01 14:49:49 +0200753
Vivek Goyal04a6b512010-12-01 19:34:52 +0100754 hlist_for_each_entry_safe(tg, pos, n, &td->tg_list, tg_node) {
Vivek Goyalfe071432010-10-01 14:49:49 +0200755 if (throtl_tg_on_rr(tg) && tg->limits_changed) {
756 throtl_log_tg(td, tg, "limit change rbps=%llu wbps=%llu"
757 " riops=%u wiops=%u", tg->bps[READ],
758 tg->bps[WRITE], tg->iops[READ],
759 tg->iops[WRITE]);
760 tg_update_disptime(td, tg);
761 tg->limits_changed = false;
762 }
763 }
764
765 smp_mb__before_atomic_dec();
766 atomic_dec(&td->limits_changed);
767 smp_mb__after_atomic_dec();
768}
769
Vivek Goyale43473b2010-09-15 17:06:35 -0400770/* Dispatch throttled bios. Should be called without queue lock held. */
771static int throtl_dispatch(struct request_queue *q)
772{
773 struct throtl_data *td = q->td;
774 unsigned int nr_disp = 0;
775 struct bio_list bio_list_on_stack;
776 struct bio *bio;
777
778 spin_lock_irq(q->queue_lock);
779
Vivek Goyalfe071432010-10-01 14:49:49 +0200780 throtl_process_limit_change(td);
781
Vivek Goyale43473b2010-09-15 17:06:35 -0400782 if (!total_nr_queued(td))
783 goto out;
784
785 bio_list_init(&bio_list_on_stack);
786
787 throtl_log(td, "dispatch nr_queued=%lu read=%u write=%u",
788 total_nr_queued(td), td->nr_queued[READ],
789 td->nr_queued[WRITE]);
790
791 nr_disp = throtl_select_dispatch(td, &bio_list_on_stack);
792
793 if (nr_disp)
794 throtl_log(td, "bios disp=%u", nr_disp);
795
796 throtl_schedule_next_dispatch(td);
797out:
798 spin_unlock_irq(q->queue_lock);
799
800 /*
801 * If we dispatched some requests, unplug the queue to make sure
802 * immediate dispatch
803 */
804 if (nr_disp) {
805 while((bio = bio_list_pop(&bio_list_on_stack)))
806 generic_make_request(bio);
807 blk_unplug(q);
808 }
809 return nr_disp;
810}
811
812void blk_throtl_work(struct work_struct *work)
813{
814 struct throtl_data *td = container_of(work, struct throtl_data,
815 throtl_work.work);
816 struct request_queue *q = td->queue;
817
818 throtl_dispatch(q);
819}
820
821/* Call with queue lock held */
Vivek Goyal450adcb2011-03-01 13:40:54 -0500822static void
823throtl_schedule_delayed_work(struct throtl_data *td, unsigned long delay)
Vivek Goyale43473b2010-09-15 17:06:35 -0400824{
825
Vivek Goyale43473b2010-09-15 17:06:35 -0400826 struct delayed_work *dwork = &td->throtl_work;
827
828 if (total_nr_queued(td) > 0) {
829 /*
830 * We might have a work scheduled to be executed in future.
831 * Cancel that and schedule a new one.
832 */
833 __cancel_delayed_work(dwork);
Vivek Goyal450adcb2011-03-01 13:40:54 -0500834 queue_delayed_work(kthrotld_workqueue, dwork, delay);
Vivek Goyale43473b2010-09-15 17:06:35 -0400835 throtl_log(td, "schedule work. delay=%lu jiffies=%lu",
836 delay, jiffies);
837 }
838}
Vivek Goyale43473b2010-09-15 17:06:35 -0400839
840static void
841throtl_destroy_tg(struct throtl_data *td, struct throtl_grp *tg)
842{
843 /* Something wrong if we are trying to remove same group twice */
844 BUG_ON(hlist_unhashed(&tg->tg_node));
845
846 hlist_del_init(&tg->tg_node);
847
848 /*
849 * Put the reference taken at the time of creation so that when all
850 * queues are gone, group can be destroyed.
851 */
852 throtl_put_tg(tg);
853 td->nr_undestroyed_grps--;
854}
855
856static void throtl_release_tgs(struct throtl_data *td)
857{
858 struct hlist_node *pos, *n;
859 struct throtl_grp *tg;
860
861 hlist_for_each_entry_safe(tg, pos, n, &td->tg_list, tg_node) {
862 /*
863 * If cgroup removal path got to blk_group first and removed
864 * it from cgroup list, then it will take care of destroying
865 * cfqg also.
866 */
867 if (!blkiocg_del_blkio_group(&tg->blkg))
868 throtl_destroy_tg(td, tg);
869 }
870}
871
872static void throtl_td_free(struct throtl_data *td)
873{
874 kfree(td);
875}
876
877/*
878 * Blk cgroup controller notification saying that blkio_group object is being
879 * delinked as associated cgroup object is going away. That also means that
880 * no new IO will come in this group. So get rid of this group as soon as
881 * any pending IO in the group is finished.
882 *
883 * This function is called under rcu_read_lock(). key is the rcu protected
884 * pointer. That means "key" is a valid throtl_data pointer as long as we are
885 * rcu read lock.
886 *
887 * "key" was fetched from blkio_group under blkio_cgroup->lock. That means
888 * it should not be NULL as even if queue was going away, cgroup deltion
889 * path got to it first.
890 */
891void throtl_unlink_blkio_group(void *key, struct blkio_group *blkg)
892{
893 unsigned long flags;
894 struct throtl_data *td = key;
895
896 spin_lock_irqsave(td->queue->queue_lock, flags);
897 throtl_destroy_tg(td, tg_of_blkg(blkg));
898 spin_unlock_irqrestore(td->queue->queue_lock, flags);
899}
900
Vivek Goyalfe071432010-10-01 14:49:49 +0200901/*
902 * For all update functions, key should be a valid pointer because these
903 * update functions are called under blkcg_lock, that means, blkg is
904 * valid and in turn key is valid. queue exit path can not race becuase
905 * of blkcg_lock
906 *
907 * Can not take queue lock in update functions as queue lock under blkcg_lock
908 * is not allowed. Under other paths we take blkcg_lock under queue_lock.
909 */
910static void throtl_update_blkio_group_read_bps(void *key,
911 struct blkio_group *blkg, u64 read_bps)
Vivek Goyale43473b2010-09-15 17:06:35 -0400912{
Vivek Goyalfe071432010-10-01 14:49:49 +0200913 struct throtl_data *td = key;
914
Vivek Goyale43473b2010-09-15 17:06:35 -0400915 tg_of_blkg(blkg)->bps[READ] = read_bps;
Vivek Goyalfe071432010-10-01 14:49:49 +0200916 /* Make sure read_bps is updated before setting limits_changed */
917 smp_wmb();
918 tg_of_blkg(blkg)->limits_changed = true;
919
920 /* Make sure tg->limits_changed is updated before td->limits_changed */
921 smp_mb__before_atomic_inc();
922 atomic_inc(&td->limits_changed);
923 smp_mb__after_atomic_inc();
924
925 /* Schedule a work now to process the limit change */
Vivek Goyal450adcb2011-03-01 13:40:54 -0500926 throtl_schedule_delayed_work(td, 0);
Vivek Goyale43473b2010-09-15 17:06:35 -0400927}
928
Vivek Goyalfe071432010-10-01 14:49:49 +0200929static void throtl_update_blkio_group_write_bps(void *key,
930 struct blkio_group *blkg, u64 write_bps)
Vivek Goyale43473b2010-09-15 17:06:35 -0400931{
Vivek Goyalfe071432010-10-01 14:49:49 +0200932 struct throtl_data *td = key;
933
Vivek Goyale43473b2010-09-15 17:06:35 -0400934 tg_of_blkg(blkg)->bps[WRITE] = write_bps;
Vivek Goyalfe071432010-10-01 14:49:49 +0200935 smp_wmb();
936 tg_of_blkg(blkg)->limits_changed = true;
937 smp_mb__before_atomic_inc();
938 atomic_inc(&td->limits_changed);
939 smp_mb__after_atomic_inc();
Vivek Goyal450adcb2011-03-01 13:40:54 -0500940 throtl_schedule_delayed_work(td, 0);
Vivek Goyale43473b2010-09-15 17:06:35 -0400941}
942
Vivek Goyalfe071432010-10-01 14:49:49 +0200943static void throtl_update_blkio_group_read_iops(void *key,
944 struct blkio_group *blkg, unsigned int read_iops)
Vivek Goyal8e89d132010-09-15 17:06:37 -0400945{
Vivek Goyalfe071432010-10-01 14:49:49 +0200946 struct throtl_data *td = key;
947
Vivek Goyal8e89d132010-09-15 17:06:37 -0400948 tg_of_blkg(blkg)->iops[READ] = read_iops;
Vivek Goyalfe071432010-10-01 14:49:49 +0200949 smp_wmb();
950 tg_of_blkg(blkg)->limits_changed = true;
951 smp_mb__before_atomic_inc();
952 atomic_inc(&td->limits_changed);
953 smp_mb__after_atomic_inc();
Vivek Goyal450adcb2011-03-01 13:40:54 -0500954 throtl_schedule_delayed_work(td, 0);
Vivek Goyal8e89d132010-09-15 17:06:37 -0400955}
956
Vivek Goyalfe071432010-10-01 14:49:49 +0200957static void throtl_update_blkio_group_write_iops(void *key,
958 struct blkio_group *blkg, unsigned int write_iops)
Vivek Goyal8e89d132010-09-15 17:06:37 -0400959{
Vivek Goyalfe071432010-10-01 14:49:49 +0200960 struct throtl_data *td = key;
961
Vivek Goyal8e89d132010-09-15 17:06:37 -0400962 tg_of_blkg(blkg)->iops[WRITE] = write_iops;
Vivek Goyalfe071432010-10-01 14:49:49 +0200963 smp_wmb();
964 tg_of_blkg(blkg)->limits_changed = true;
965 smp_mb__before_atomic_inc();
966 atomic_inc(&td->limits_changed);
967 smp_mb__after_atomic_inc();
Vivek Goyal450adcb2011-03-01 13:40:54 -0500968 throtl_schedule_delayed_work(td, 0);
Vivek Goyal8e89d132010-09-15 17:06:37 -0400969}
970
Vivek Goyalda527772011-03-02 19:05:33 -0500971static void throtl_shutdown_wq(struct request_queue *q)
Vivek Goyale43473b2010-09-15 17:06:35 -0400972{
973 struct throtl_data *td = q->td;
974
975 cancel_delayed_work_sync(&td->throtl_work);
976}
977
978static struct blkio_policy_type blkio_policy_throtl = {
979 .ops = {
980 .blkio_unlink_group_fn = throtl_unlink_blkio_group,
981 .blkio_update_group_read_bps_fn =
982 throtl_update_blkio_group_read_bps,
983 .blkio_update_group_write_bps_fn =
984 throtl_update_blkio_group_write_bps,
Vivek Goyal8e89d132010-09-15 17:06:37 -0400985 .blkio_update_group_read_iops_fn =
986 throtl_update_blkio_group_read_iops,
987 .blkio_update_group_write_iops_fn =
988 throtl_update_blkio_group_write_iops,
Vivek Goyale43473b2010-09-15 17:06:35 -0400989 },
Vivek Goyal8e89d132010-09-15 17:06:37 -0400990 .plid = BLKIO_POLICY_THROTL,
Vivek Goyale43473b2010-09-15 17:06:35 -0400991};
992
993int blk_throtl_bio(struct request_queue *q, struct bio **biop)
994{
995 struct throtl_data *td = q->td;
996 struct throtl_grp *tg;
997 struct bio *bio = *biop;
998 bool rw = bio_data_dir(bio), update_disptime = true;
999
1000 if (bio->bi_rw & REQ_THROTTLED) {
1001 bio->bi_rw &= ~REQ_THROTTLED;
1002 return 0;
1003 }
1004
1005 spin_lock_irq(q->queue_lock);
1006 tg = throtl_get_tg(td);
1007
1008 if (tg->nr_queued[rw]) {
1009 /*
1010 * There is already another bio queued in same dir. No
1011 * need to update dispatch time.
1012 */
Vivek Goyal231d7042011-03-07 21:05:14 +01001013 update_disptime = false;
Vivek Goyale43473b2010-09-15 17:06:35 -04001014 goto queue_bio;
1015 }
1016
1017 /* Bio is with-in rate limit of group */
1018 if (tg_may_dispatch(td, tg, bio, NULL)) {
1019 throtl_charge_bio(tg, bio);
1020 goto out;
1021 }
1022
1023queue_bio:
Vivek Goyal8e89d132010-09-15 17:06:37 -04001024 throtl_log_tg(td, tg, "[%c] bio. bdisp=%u sz=%u bps=%llu"
1025 " iodisp=%u iops=%u queued=%d/%d",
1026 rw == READ ? 'R' : 'W',
Vivek Goyale43473b2010-09-15 17:06:35 -04001027 tg->bytes_disp[rw], bio->bi_size, tg->bps[rw],
Vivek Goyal8e89d132010-09-15 17:06:37 -04001028 tg->io_disp[rw], tg->iops[rw],
Vivek Goyale43473b2010-09-15 17:06:35 -04001029 tg->nr_queued[READ], tg->nr_queued[WRITE]);
1030
1031 throtl_add_bio_tg(q->td, tg, bio);
1032 *biop = NULL;
1033
1034 if (update_disptime) {
1035 tg_update_disptime(td, tg);
1036 throtl_schedule_next_dispatch(td);
1037 }
1038
1039out:
1040 spin_unlock_irq(q->queue_lock);
1041 return 0;
1042}
1043
1044int blk_throtl_init(struct request_queue *q)
1045{
1046 struct throtl_data *td;
1047 struct throtl_grp *tg;
1048
1049 td = kzalloc_node(sizeof(*td), GFP_KERNEL, q->node);
1050 if (!td)
1051 return -ENOMEM;
1052
1053 INIT_HLIST_HEAD(&td->tg_list);
1054 td->tg_service_tree = THROTL_RB_ROOT;
Vivek Goyalfe071432010-10-01 14:49:49 +02001055 atomic_set(&td->limits_changed, 0);
Vivek Goyale43473b2010-09-15 17:06:35 -04001056
1057 /* Init root group */
1058 tg = &td->root_tg;
1059 INIT_HLIST_NODE(&tg->tg_node);
1060 RB_CLEAR_NODE(&tg->rb_node);
1061 bio_list_init(&tg->bio_lists[0]);
1062 bio_list_init(&tg->bio_lists[1]);
1063
1064 /* Practically unlimited BW */
1065 tg->bps[0] = tg->bps[1] = -1;
Vivek Goyal8e89d132010-09-15 17:06:37 -04001066 tg->iops[0] = tg->iops[1] = -1;
Vivek Goyal02977e42010-10-01 14:49:48 +02001067
1068 /*
1069 * Set root group reference to 2. One reference will be dropped when
1070 * all groups on tg_list are being deleted during queue exit. Other
1071 * reference will remain there as we don't want to delete this group
1072 * as it is statically allocated and gets destroyed when throtl_data
1073 * goes away.
1074 */
1075 atomic_set(&tg->ref, 2);
1076 hlist_add_head(&tg->tg_node, &td->tg_list);
1077 td->nr_undestroyed_grps++;
Vivek Goyale43473b2010-09-15 17:06:35 -04001078
1079 INIT_DELAYED_WORK(&td->throtl_work, blk_throtl_work);
1080
1081 rcu_read_lock();
1082 blkiocg_add_blkio_group(&blkio_root_cgroup, &tg->blkg, (void *)td,
1083 0, BLKIO_POLICY_THROTL);
1084 rcu_read_unlock();
1085
1086 /* Attach throtl data to request queue */
1087 td->queue = q;
1088 q->td = td;
1089 return 0;
1090}
1091
1092void blk_throtl_exit(struct request_queue *q)
1093{
1094 struct throtl_data *td = q->td;
1095 bool wait = false;
1096
1097 BUG_ON(!td);
1098
Vivek Goyalda527772011-03-02 19:05:33 -05001099 throtl_shutdown_wq(q);
Vivek Goyale43473b2010-09-15 17:06:35 -04001100
1101 spin_lock_irq(q->queue_lock);
1102 throtl_release_tgs(td);
Vivek Goyale43473b2010-09-15 17:06:35 -04001103
1104 /* If there are other groups */
Vivek Goyal02977e42010-10-01 14:49:48 +02001105 if (td->nr_undestroyed_grps > 0)
Vivek Goyale43473b2010-09-15 17:06:35 -04001106 wait = true;
1107
1108 spin_unlock_irq(q->queue_lock);
1109
1110 /*
1111 * Wait for tg->blkg->key accessors to exit their grace periods.
1112 * Do this wait only if there are other undestroyed groups out
1113 * there (other than root group). This can happen if cgroup deletion
1114 * path claimed the responsibility of cleaning up a group before
1115 * queue cleanup code get to the group.
1116 *
1117 * Do not call synchronize_rcu() unconditionally as there are drivers
1118 * which create/delete request queue hundreds of times during scan/boot
1119 * and synchronize_rcu() can take significant time and slow down boot.
1120 */
1121 if (wait)
1122 synchronize_rcu();
Vivek Goyalfe071432010-10-01 14:49:49 +02001123
1124 /*
1125 * Just being safe to make sure after previous flush if some body did
1126 * update limits through cgroup and another work got queued, cancel
1127 * it.
1128 */
Vivek Goyalda527772011-03-02 19:05:33 -05001129 throtl_shutdown_wq(q);
Vivek Goyale43473b2010-09-15 17:06:35 -04001130 throtl_td_free(td);
1131}
1132
1133static int __init throtl_init(void)
1134{
Vivek Goyal450adcb2011-03-01 13:40:54 -05001135 kthrotld_workqueue = alloc_workqueue("kthrotld", WQ_MEM_RECLAIM, 0);
1136 if (!kthrotld_workqueue)
1137 panic("Failed to create kthrotld\n");
1138
Vivek Goyale43473b2010-09-15 17:06:35 -04001139 blkio_policy_register(&blkio_policy_throtl);
1140 return 0;
1141}
1142
1143module_init(throtl_init);