blob: b9412d1cea9e34a7fe74543dc4d883c5843ff8ec [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 */
Andreas Schwab6f037932011-03-30 12:21:56 +020080 int 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
Vivek Goyal29b12582011-05-19 15:38:24 -040091 struct throtl_grp *root_tg;
Vivek Goyale43473b2010-09-15 17:06:35 -040092 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
Andreas Schwab6f037932011-03-30 12:21:56 +0200105 int 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
Vivek Goyala29a1712011-05-19 15:38:19 -0400162static void throtl_init_group(struct throtl_grp *tg)
163{
164 INIT_HLIST_NODE(&tg->tg_node);
165 RB_CLEAR_NODE(&tg->rb_node);
166 bio_list_init(&tg->bio_lists[0]);
167 bio_list_init(&tg->bio_lists[1]);
168 tg->limits_changed = false;
169
170 /* Practically unlimited BW */
171 tg->bps[0] = tg->bps[1] = -1;
172 tg->iops[0] = tg->iops[1] = -1;
173
174 /*
175 * Take the initial reference that will be released on destroy
176 * This can be thought of a joint reference by cgroup and
177 * request queue which will be dropped by either request queue
178 * exit or cgroup deletion path depending on who is exiting first.
179 */
180 atomic_set(&tg->ref, 1);
181}
182
183/* Should be called with rcu read lock held (needed for blkcg) */
184static void
185throtl_add_group_to_td_list(struct throtl_data *td, struct throtl_grp *tg)
186{
187 hlist_add_head(&tg->tg_node, &td->tg_list);
188 td->nr_undestroyed_grps++;
189}
190
Vivek Goyal269f5412011-05-19 15:38:25 -0400191static void
192__throtl_tg_fill_dev_details(struct throtl_data *td, struct throtl_grp *tg)
Vivek Goyalf469a7b2011-05-19 15:38:23 -0400193{
194 struct backing_dev_info *bdi = &td->queue->backing_dev_info;
195 unsigned int major, minor;
196
Vivek Goyal269f5412011-05-19 15:38:25 -0400197 if (!tg || tg->blkg.dev)
198 return;
199
200 /*
201 * Fill in device details for a group which might not have been
202 * filled at group creation time as queue was being instantiated
203 * and driver had not attached a device yet
204 */
205 if (bdi->dev && dev_name(bdi->dev)) {
206 sscanf(dev_name(bdi->dev), "%u:%u", &major, &minor);
207 tg->blkg.dev = MKDEV(major, minor);
208 }
209}
210
211static void throtl_init_add_tg_lists(struct throtl_data *td,
212 struct throtl_grp *tg, struct blkio_cgroup *blkcg)
213{
214 __throtl_tg_fill_dev_details(td, tg);
215
Vivek Goyalf469a7b2011-05-19 15:38:23 -0400216 /* Add group onto cgroup list */
Vivek Goyalf469a7b2011-05-19 15:38:23 -0400217 blkiocg_add_blkio_group(blkcg, &tg->blkg, (void *)td,
Vivek Goyal269f5412011-05-19 15:38:25 -0400218 tg->blkg.dev, BLKIO_POLICY_THROTL);
Vivek Goyalf469a7b2011-05-19 15:38:23 -0400219
220 tg->bps[READ] = blkcg_get_read_bps(blkcg, tg->blkg.dev);
221 tg->bps[WRITE] = blkcg_get_write_bps(blkcg, tg->blkg.dev);
222 tg->iops[READ] = blkcg_get_read_iops(blkcg, tg->blkg.dev);
223 tg->iops[WRITE] = blkcg_get_write_iops(blkcg, tg->blkg.dev);
224
225 throtl_add_group_to_td_list(td, tg);
226}
227
228/* Should be called without queue lock and outside of rcu period */
229static struct throtl_grp *throtl_alloc_tg(struct throtl_data *td)
230{
231 struct throtl_grp *tg = NULL;
232
233 tg = kzalloc_node(sizeof(*tg), GFP_ATOMIC, td->queue->node);
234 if (!tg)
235 return NULL;
236
237 throtl_init_group(tg);
238 return tg;
239}
240
241static struct
242throtl_grp *throtl_find_tg(struct throtl_data *td, struct blkio_cgroup *blkcg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400243{
Vivek Goyale43473b2010-09-15 17:06:35 -0400244 struct throtl_grp *tg = NULL;
245 void *key = td;
Vivek Goyale43473b2010-09-15 17:06:35 -0400246
247 /*
Vivek Goyalbe2c6b12011-01-19 08:25:02 -0700248 * This is the common case when there are no blkio cgroups.
249 * Avoid lookup in this case
250 */
251 if (blkcg == &blkio_root_cgroup)
Vivek Goyal29b12582011-05-19 15:38:24 -0400252 tg = td->root_tg;
Vivek Goyalbe2c6b12011-01-19 08:25:02 -0700253 else
254 tg = tg_of_blkg(blkiocg_lookup_group(blkcg, key));
Vivek Goyale43473b2010-09-15 17:06:35 -0400255
Vivek Goyal269f5412011-05-19 15:38:25 -0400256 __throtl_tg_fill_dev_details(td, tg);
Vivek Goyale43473b2010-09-15 17:06:35 -0400257 return tg;
258}
259
Vivek Goyalf469a7b2011-05-19 15:38:23 -0400260/*
261 * This function returns with queue lock unlocked in case of error, like
262 * request queue is no more
263 */
Vivek Goyale43473b2010-09-15 17:06:35 -0400264static struct throtl_grp * throtl_get_tg(struct throtl_data *td)
265{
Vivek Goyalf469a7b2011-05-19 15:38:23 -0400266 struct throtl_grp *tg = NULL, *__tg = NULL;
Vivek Goyal70087dc2011-05-16 15:24:08 +0200267 struct blkio_cgroup *blkcg;
Vivek Goyalf469a7b2011-05-19 15:38:23 -0400268 struct request_queue *q = td->queue;
Vivek Goyale43473b2010-09-15 17:06:35 -0400269
270 rcu_read_lock();
Vivek Goyal70087dc2011-05-16 15:24:08 +0200271 blkcg = task_blkio_cgroup(current);
Vivek Goyalf469a7b2011-05-19 15:38:23 -0400272 tg = throtl_find_tg(td, blkcg);
273 if (tg) {
274 rcu_read_unlock();
275 return tg;
276 }
277
278 /*
279 * Need to allocate a group. Allocation of group also needs allocation
280 * of per cpu stats which in-turn takes a mutex() and can block. Hence
281 * we need to drop rcu lock and queue_lock before we call alloc
282 *
283 * Take the request queue reference to make sure queue does not
284 * go away once we return from allocation.
285 */
286 blk_get_queue(q);
287 rcu_read_unlock();
288 spin_unlock_irq(q->queue_lock);
289
290 tg = throtl_alloc_tg(td);
291 /*
292 * We might have slept in group allocation. Make sure queue is not
293 * dead
294 */
295 if (unlikely(test_bit(QUEUE_FLAG_DEAD, &q->queue_flags))) {
296 blk_put_queue(q);
297 if (tg)
298 kfree(tg);
299
300 return ERR_PTR(-ENODEV);
301 }
302 blk_put_queue(q);
303
304 /* Group allocated and queue is still alive. take the lock */
305 spin_lock_irq(q->queue_lock);
306
307 /*
308 * Initialize the new group. After sleeping, read the blkcg again.
309 */
310 rcu_read_lock();
311 blkcg = task_blkio_cgroup(current);
312
313 /*
314 * If some other thread already allocated the group while we were
315 * not holding queue lock, free up the group
316 */
317 __tg = throtl_find_tg(td, blkcg);
318
319 if (__tg) {
320 kfree(tg);
321 rcu_read_unlock();
322 return __tg;
323 }
324
325 /* Group allocation failed. Account the IO to root group */
326 if (!tg) {
Vivek Goyal29b12582011-05-19 15:38:24 -0400327 tg = td->root_tg;
Vivek Goyalf469a7b2011-05-19 15:38:23 -0400328 return tg;
329 }
330
331 throtl_init_add_tg_lists(td, tg, blkcg);
Vivek Goyale43473b2010-09-15 17:06:35 -0400332 rcu_read_unlock();
333 return tg;
334}
335
336static struct throtl_grp *throtl_rb_first(struct throtl_rb_root *root)
337{
338 /* Service tree is empty */
339 if (!root->count)
340 return NULL;
341
342 if (!root->left)
343 root->left = rb_first(&root->rb);
344
345 if (root->left)
346 return rb_entry_tg(root->left);
347
348 return NULL;
349}
350
351static void rb_erase_init(struct rb_node *n, struct rb_root *root)
352{
353 rb_erase(n, root);
354 RB_CLEAR_NODE(n);
355}
356
357static void throtl_rb_erase(struct rb_node *n, struct throtl_rb_root *root)
358{
359 if (root->left == n)
360 root->left = NULL;
361 rb_erase_init(n, &root->rb);
362 --root->count;
363}
364
365static void update_min_dispatch_time(struct throtl_rb_root *st)
366{
367 struct throtl_grp *tg;
368
369 tg = throtl_rb_first(st);
370 if (!tg)
371 return;
372
373 st->min_disptime = tg->disptime;
374}
375
376static void
377tg_service_tree_add(struct throtl_rb_root *st, struct throtl_grp *tg)
378{
379 struct rb_node **node = &st->rb.rb_node;
380 struct rb_node *parent = NULL;
381 struct throtl_grp *__tg;
382 unsigned long key = tg->disptime;
383 int left = 1;
384
385 while (*node != NULL) {
386 parent = *node;
387 __tg = rb_entry_tg(parent);
388
389 if (time_before(key, __tg->disptime))
390 node = &parent->rb_left;
391 else {
392 node = &parent->rb_right;
393 left = 0;
394 }
395 }
396
397 if (left)
398 st->left = &tg->rb_node;
399
400 rb_link_node(&tg->rb_node, parent, node);
401 rb_insert_color(&tg->rb_node, &st->rb);
402}
403
404static void __throtl_enqueue_tg(struct throtl_data *td, struct throtl_grp *tg)
405{
406 struct throtl_rb_root *st = &td->tg_service_tree;
407
408 tg_service_tree_add(st, tg);
409 throtl_mark_tg_on_rr(tg);
410 st->count++;
411}
412
413static void throtl_enqueue_tg(struct throtl_data *td, struct throtl_grp *tg)
414{
415 if (!throtl_tg_on_rr(tg))
416 __throtl_enqueue_tg(td, tg);
417}
418
419static void __throtl_dequeue_tg(struct throtl_data *td, struct throtl_grp *tg)
420{
421 throtl_rb_erase(&tg->rb_node, &td->tg_service_tree);
422 throtl_clear_tg_on_rr(tg);
423}
424
425static void throtl_dequeue_tg(struct throtl_data *td, struct throtl_grp *tg)
426{
427 if (throtl_tg_on_rr(tg))
428 __throtl_dequeue_tg(td, tg);
429}
430
431static void throtl_schedule_next_dispatch(struct throtl_data *td)
432{
433 struct throtl_rb_root *st = &td->tg_service_tree;
434
435 /*
436 * If there are more bios pending, schedule more work.
437 */
438 if (!total_nr_queued(td))
439 return;
440
441 BUG_ON(!st->count);
442
443 update_min_dispatch_time(st);
444
445 if (time_before_eq(st->min_disptime, jiffies))
Vivek Goyal450adcb2011-03-01 13:40:54 -0500446 throtl_schedule_delayed_work(td, 0);
Vivek Goyale43473b2010-09-15 17:06:35 -0400447 else
Vivek Goyal450adcb2011-03-01 13:40:54 -0500448 throtl_schedule_delayed_work(td, (st->min_disptime - jiffies));
Vivek Goyale43473b2010-09-15 17:06:35 -0400449}
450
451static inline void
452throtl_start_new_slice(struct throtl_data *td, struct throtl_grp *tg, bool rw)
453{
454 tg->bytes_disp[rw] = 0;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400455 tg->io_disp[rw] = 0;
Vivek Goyale43473b2010-09-15 17:06:35 -0400456 tg->slice_start[rw] = jiffies;
457 tg->slice_end[rw] = jiffies + throtl_slice;
458 throtl_log_tg(td, tg, "[%c] new slice start=%lu end=%lu jiffies=%lu",
459 rw == READ ? 'R' : 'W', tg->slice_start[rw],
460 tg->slice_end[rw], jiffies);
461}
462
Vivek Goyald1ae8ff2010-12-01 19:34:46 +0100463static inline void throtl_set_slice_end(struct throtl_data *td,
464 struct throtl_grp *tg, bool rw, unsigned long jiffy_end)
465{
466 tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
467}
468
Vivek Goyale43473b2010-09-15 17:06:35 -0400469static inline void throtl_extend_slice(struct throtl_data *td,
470 struct throtl_grp *tg, bool rw, unsigned long jiffy_end)
471{
472 tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
473 throtl_log_tg(td, tg, "[%c] extend slice start=%lu end=%lu jiffies=%lu",
474 rw == READ ? 'R' : 'W', tg->slice_start[rw],
475 tg->slice_end[rw], jiffies);
476}
477
478/* Determine if previously allocated or extended slice is complete or not */
479static bool
480throtl_slice_used(struct throtl_data *td, struct throtl_grp *tg, bool rw)
481{
482 if (time_in_range(jiffies, tg->slice_start[rw], tg->slice_end[rw]))
483 return 0;
484
485 return 1;
486}
487
488/* Trim the used slices and adjust slice start accordingly */
489static inline void
490throtl_trim_slice(struct throtl_data *td, struct throtl_grp *tg, bool rw)
491{
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200492 unsigned long nr_slices, time_elapsed, io_trim;
493 u64 bytes_trim, tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400494
495 BUG_ON(time_before(tg->slice_end[rw], tg->slice_start[rw]));
496
497 /*
498 * If bps are unlimited (-1), then time slice don't get
499 * renewed. Don't try to trim the slice if slice is used. A new
500 * slice will start when appropriate.
501 */
502 if (throtl_slice_used(td, tg, rw))
503 return;
504
Vivek Goyald1ae8ff2010-12-01 19:34:46 +0100505 /*
506 * A bio has been dispatched. Also adjust slice_end. It might happen
507 * that initially cgroup limit was very low resulting in high
508 * slice_end, but later limit was bumped up and bio was dispached
509 * sooner, then we need to reduce slice_end. A high bogus slice_end
510 * is bad because it does not allow new slice to start.
511 */
512
513 throtl_set_slice_end(td, tg, rw, jiffies + throtl_slice);
514
Vivek Goyale43473b2010-09-15 17:06:35 -0400515 time_elapsed = jiffies - tg->slice_start[rw];
516
517 nr_slices = time_elapsed / throtl_slice;
518
519 if (!nr_slices)
520 return;
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200521 tmp = tg->bps[rw] * throtl_slice * nr_slices;
522 do_div(tmp, HZ);
523 bytes_trim = tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400524
Vivek Goyal8e89d132010-09-15 17:06:37 -0400525 io_trim = (tg->iops[rw] * throtl_slice * nr_slices)/HZ;
Vivek Goyale43473b2010-09-15 17:06:35 -0400526
Vivek Goyal8e89d132010-09-15 17:06:37 -0400527 if (!bytes_trim && !io_trim)
Vivek Goyale43473b2010-09-15 17:06:35 -0400528 return;
529
530 if (tg->bytes_disp[rw] >= bytes_trim)
531 tg->bytes_disp[rw] -= bytes_trim;
532 else
533 tg->bytes_disp[rw] = 0;
534
Vivek Goyal8e89d132010-09-15 17:06:37 -0400535 if (tg->io_disp[rw] >= io_trim)
536 tg->io_disp[rw] -= io_trim;
537 else
538 tg->io_disp[rw] = 0;
539
Vivek Goyale43473b2010-09-15 17:06:35 -0400540 tg->slice_start[rw] += nr_slices * throtl_slice;
541
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200542 throtl_log_tg(td, tg, "[%c] trim slice nr=%lu bytes=%llu io=%lu"
Vivek Goyale43473b2010-09-15 17:06:35 -0400543 " start=%lu end=%lu jiffies=%lu",
Vivek Goyal8e89d132010-09-15 17:06:37 -0400544 rw == READ ? 'R' : 'W', nr_slices, bytes_trim, io_trim,
Vivek Goyale43473b2010-09-15 17:06:35 -0400545 tg->slice_start[rw], tg->slice_end[rw], jiffies);
546}
547
Vivek Goyal8e89d132010-09-15 17:06:37 -0400548static bool tg_with_in_iops_limit(struct throtl_data *td, struct throtl_grp *tg,
549 struct bio *bio, unsigned long *wait)
Vivek Goyale43473b2010-09-15 17:06:35 -0400550{
551 bool rw = bio_data_dir(bio);
Vivek Goyal8e89d132010-09-15 17:06:37 -0400552 unsigned int io_allowed;
Vivek Goyale43473b2010-09-15 17:06:35 -0400553 unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
Vivek Goyalc49c06e2010-10-01 21:16:42 +0200554 u64 tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400555
Vivek Goyal8e89d132010-09-15 17:06:37 -0400556 jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
Vivek Goyale43473b2010-09-15 17:06:35 -0400557
Vivek Goyal8e89d132010-09-15 17:06:37 -0400558 /* Slice has just started. Consider one slice interval */
559 if (!jiffy_elapsed)
560 jiffy_elapsed_rnd = throtl_slice;
561
562 jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
563
Vivek Goyalc49c06e2010-10-01 21:16:42 +0200564 /*
565 * jiffy_elapsed_rnd should not be a big value as minimum iops can be
566 * 1 then at max jiffy elapsed should be equivalent of 1 second as we
567 * will allow dispatch after 1 second and after that slice should
568 * have been trimmed.
569 */
570
571 tmp = (u64)tg->iops[rw] * jiffy_elapsed_rnd;
572 do_div(tmp, HZ);
573
574 if (tmp > UINT_MAX)
575 io_allowed = UINT_MAX;
576 else
577 io_allowed = tmp;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400578
579 if (tg->io_disp[rw] + 1 <= io_allowed) {
Vivek Goyale43473b2010-09-15 17:06:35 -0400580 if (wait)
581 *wait = 0;
582 return 1;
583 }
584
Vivek Goyal8e89d132010-09-15 17:06:37 -0400585 /* Calc approx time to dispatch */
586 jiffy_wait = ((tg->io_disp[rw] + 1) * HZ)/tg->iops[rw] + 1;
587
588 if (jiffy_wait > jiffy_elapsed)
589 jiffy_wait = jiffy_wait - jiffy_elapsed;
590 else
591 jiffy_wait = 1;
592
593 if (wait)
594 *wait = jiffy_wait;
595 return 0;
596}
597
598static bool tg_with_in_bps_limit(struct throtl_data *td, struct throtl_grp *tg,
599 struct bio *bio, unsigned long *wait)
600{
601 bool rw = bio_data_dir(bio);
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200602 u64 bytes_allowed, extra_bytes, tmp;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400603 unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
Vivek Goyale43473b2010-09-15 17:06:35 -0400604
605 jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
606
607 /* Slice has just started. Consider one slice interval */
608 if (!jiffy_elapsed)
609 jiffy_elapsed_rnd = throtl_slice;
610
611 jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
612
Vivek Goyal5e901a22010-10-01 21:16:38 +0200613 tmp = tg->bps[rw] * jiffy_elapsed_rnd;
614 do_div(tmp, HZ);
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200615 bytes_allowed = tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400616
617 if (tg->bytes_disp[rw] + bio->bi_size <= bytes_allowed) {
618 if (wait)
619 *wait = 0;
620 return 1;
621 }
622
623 /* Calc approx time to dispatch */
624 extra_bytes = tg->bytes_disp[rw] + bio->bi_size - bytes_allowed;
625 jiffy_wait = div64_u64(extra_bytes * HZ, tg->bps[rw]);
626
627 if (!jiffy_wait)
628 jiffy_wait = 1;
629
630 /*
631 * This wait time is without taking into consideration the rounding
632 * up we did. Add that time also.
633 */
634 jiffy_wait = jiffy_wait + (jiffy_elapsed_rnd - jiffy_elapsed);
Vivek Goyale43473b2010-09-15 17:06:35 -0400635 if (wait)
636 *wait = jiffy_wait;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400637 return 0;
638}
Vivek Goyale43473b2010-09-15 17:06:35 -0400639
Vivek Goyal8e89d132010-09-15 17:06:37 -0400640/*
641 * Returns whether one can dispatch a bio or not. Also returns approx number
642 * of jiffies to wait before this bio is with-in IO rate and can be dispatched
643 */
644static bool tg_may_dispatch(struct throtl_data *td, struct throtl_grp *tg,
645 struct bio *bio, unsigned long *wait)
646{
647 bool rw = bio_data_dir(bio);
648 unsigned long bps_wait = 0, iops_wait = 0, max_wait = 0;
649
650 /*
651 * Currently whole state machine of group depends on first bio
652 * queued in the group bio list. So one should not be calling
653 * this function with a different bio if there are other bios
654 * queued.
655 */
656 BUG_ON(tg->nr_queued[rw] && bio != bio_list_peek(&tg->bio_lists[rw]));
657
658 /* If tg->bps = -1, then BW is unlimited */
659 if (tg->bps[rw] == -1 && tg->iops[rw] == -1) {
660 if (wait)
661 *wait = 0;
662 return 1;
663 }
664
665 /*
666 * If previous slice expired, start a new one otherwise renew/extend
667 * existing slice to make sure it is at least throtl_slice interval
668 * long since now.
669 */
670 if (throtl_slice_used(td, tg, rw))
671 throtl_start_new_slice(td, tg, rw);
672 else {
673 if (time_before(tg->slice_end[rw], jiffies + throtl_slice))
674 throtl_extend_slice(td, tg, rw, jiffies + throtl_slice);
675 }
676
677 if (tg_with_in_bps_limit(td, tg, bio, &bps_wait)
678 && tg_with_in_iops_limit(td, tg, bio, &iops_wait)) {
679 if (wait)
680 *wait = 0;
681 return 1;
682 }
683
684 max_wait = max(bps_wait, iops_wait);
685
686 if (wait)
687 *wait = max_wait;
688
689 if (time_before(tg->slice_end[rw], jiffies + max_wait))
690 throtl_extend_slice(td, tg, rw, jiffies + max_wait);
Vivek Goyale43473b2010-09-15 17:06:35 -0400691
692 return 0;
693}
694
695static void throtl_charge_bio(struct throtl_grp *tg, struct bio *bio)
696{
697 bool rw = bio_data_dir(bio);
698 bool sync = bio->bi_rw & REQ_SYNC;
699
700 /* Charge the bio to the group */
701 tg->bytes_disp[rw] += bio->bi_size;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400702 tg->io_disp[rw]++;
Vivek Goyale43473b2010-09-15 17:06:35 -0400703
704 /*
705 * TODO: This will take blkg->stats_lock. Figure out a way
706 * to avoid this cost.
707 */
708 blkiocg_update_dispatch_stats(&tg->blkg, bio->bi_size, rw, sync);
Vivek Goyale43473b2010-09-15 17:06:35 -0400709}
710
711static void throtl_add_bio_tg(struct throtl_data *td, struct throtl_grp *tg,
712 struct bio *bio)
713{
714 bool rw = bio_data_dir(bio);
715
716 bio_list_add(&tg->bio_lists[rw], bio);
717 /* Take a bio reference on tg */
718 throtl_ref_get_tg(tg);
719 tg->nr_queued[rw]++;
720 td->nr_queued[rw]++;
721 throtl_enqueue_tg(td, tg);
722}
723
724static void tg_update_disptime(struct throtl_data *td, struct throtl_grp *tg)
725{
726 unsigned long read_wait = -1, write_wait = -1, min_wait = -1, disptime;
727 struct bio *bio;
728
729 if ((bio = bio_list_peek(&tg->bio_lists[READ])))
730 tg_may_dispatch(td, tg, bio, &read_wait);
731
732 if ((bio = bio_list_peek(&tg->bio_lists[WRITE])))
733 tg_may_dispatch(td, tg, bio, &write_wait);
734
735 min_wait = min(read_wait, write_wait);
736 disptime = jiffies + min_wait;
737
Vivek Goyale43473b2010-09-15 17:06:35 -0400738 /* Update dispatch time */
739 throtl_dequeue_tg(td, tg);
740 tg->disptime = disptime;
741 throtl_enqueue_tg(td, tg);
742}
743
744static void tg_dispatch_one_bio(struct throtl_data *td, struct throtl_grp *tg,
745 bool rw, struct bio_list *bl)
746{
747 struct bio *bio;
748
749 bio = bio_list_pop(&tg->bio_lists[rw]);
750 tg->nr_queued[rw]--;
751 /* Drop bio reference on tg */
752 throtl_put_tg(tg);
753
754 BUG_ON(td->nr_queued[rw] <= 0);
755 td->nr_queued[rw]--;
756
757 throtl_charge_bio(tg, bio);
758 bio_list_add(bl, bio);
759 bio->bi_rw |= REQ_THROTTLED;
760
761 throtl_trim_slice(td, tg, rw);
762}
763
764static int throtl_dispatch_tg(struct throtl_data *td, struct throtl_grp *tg,
765 struct bio_list *bl)
766{
767 unsigned int nr_reads = 0, nr_writes = 0;
768 unsigned int max_nr_reads = throtl_grp_quantum*3/4;
Vivek Goyalc2f68052010-11-15 19:32:42 +0100769 unsigned int max_nr_writes = throtl_grp_quantum - max_nr_reads;
Vivek Goyale43473b2010-09-15 17:06:35 -0400770 struct bio *bio;
771
772 /* Try to dispatch 75% READS and 25% WRITES */
773
774 while ((bio = bio_list_peek(&tg->bio_lists[READ]))
775 && tg_may_dispatch(td, tg, bio, NULL)) {
776
777 tg_dispatch_one_bio(td, tg, bio_data_dir(bio), bl);
778 nr_reads++;
779
780 if (nr_reads >= max_nr_reads)
781 break;
782 }
783
784 while ((bio = bio_list_peek(&tg->bio_lists[WRITE]))
785 && tg_may_dispatch(td, tg, bio, NULL)) {
786
787 tg_dispatch_one_bio(td, tg, bio_data_dir(bio), bl);
788 nr_writes++;
789
790 if (nr_writes >= max_nr_writes)
791 break;
792 }
793
794 return nr_reads + nr_writes;
795}
796
797static int throtl_select_dispatch(struct throtl_data *td, struct bio_list *bl)
798{
799 unsigned int nr_disp = 0;
800 struct throtl_grp *tg;
801 struct throtl_rb_root *st = &td->tg_service_tree;
802
803 while (1) {
804 tg = throtl_rb_first(st);
805
806 if (!tg)
807 break;
808
809 if (time_before(jiffies, tg->disptime))
810 break;
811
812 throtl_dequeue_tg(td, tg);
813
814 nr_disp += throtl_dispatch_tg(td, tg, bl);
815
816 if (tg->nr_queued[0] || tg->nr_queued[1]) {
817 tg_update_disptime(td, tg);
818 throtl_enqueue_tg(td, tg);
819 }
820
821 if (nr_disp >= throtl_quantum)
822 break;
823 }
824
825 return nr_disp;
826}
827
Vivek Goyalfe071432010-10-01 14:49:49 +0200828static void throtl_process_limit_change(struct throtl_data *td)
829{
830 struct throtl_grp *tg;
831 struct hlist_node *pos, *n;
832
Vivek Goyalde701c72011-03-07 21:09:32 +0100833 if (!td->limits_changed)
Vivek Goyalfe071432010-10-01 14:49:49 +0200834 return;
835
Vivek Goyalde701c72011-03-07 21:09:32 +0100836 xchg(&td->limits_changed, false);
Vivek Goyalfe071432010-10-01 14:49:49 +0200837
Vivek Goyalde701c72011-03-07 21:09:32 +0100838 throtl_log(td, "limits changed");
Vivek Goyalfe071432010-10-01 14:49:49 +0200839
Vivek Goyal04a6b512010-12-01 19:34:52 +0100840 hlist_for_each_entry_safe(tg, pos, n, &td->tg_list, tg_node) {
Vivek Goyalde701c72011-03-07 21:09:32 +0100841 if (!tg->limits_changed)
842 continue;
Vivek Goyalfe071432010-10-01 14:49:49 +0200843
Vivek Goyalde701c72011-03-07 21:09:32 +0100844 if (!xchg(&tg->limits_changed, false))
845 continue;
846
847 throtl_log_tg(td, tg, "limit change rbps=%llu wbps=%llu"
848 " riops=%u wiops=%u", tg->bps[READ], tg->bps[WRITE],
849 tg->iops[READ], tg->iops[WRITE]);
850
Vivek Goyal04521db2011-03-22 21:54:29 +0100851 /*
852 * Restart the slices for both READ and WRITES. It
853 * might happen that a group's limit are dropped
854 * suddenly and we don't want to account recently
855 * dispatched IO with new low rate
856 */
857 throtl_start_new_slice(td, tg, 0);
858 throtl_start_new_slice(td, tg, 1);
859
Vivek Goyalde701c72011-03-07 21:09:32 +0100860 if (throtl_tg_on_rr(tg))
861 tg_update_disptime(td, tg);
862 }
Vivek Goyalfe071432010-10-01 14:49:49 +0200863}
864
Vivek Goyale43473b2010-09-15 17:06:35 -0400865/* Dispatch throttled bios. Should be called without queue lock held. */
866static int throtl_dispatch(struct request_queue *q)
867{
868 struct throtl_data *td = q->td;
869 unsigned int nr_disp = 0;
870 struct bio_list bio_list_on_stack;
871 struct bio *bio;
Vivek Goyal69d60eb2011-03-09 08:27:37 +0100872 struct blk_plug plug;
Vivek Goyale43473b2010-09-15 17:06:35 -0400873
874 spin_lock_irq(q->queue_lock);
875
Vivek Goyalfe071432010-10-01 14:49:49 +0200876 throtl_process_limit_change(td);
877
Vivek Goyale43473b2010-09-15 17:06:35 -0400878 if (!total_nr_queued(td))
879 goto out;
880
881 bio_list_init(&bio_list_on_stack);
882
883 throtl_log(td, "dispatch nr_queued=%lu read=%u write=%u",
884 total_nr_queued(td), td->nr_queued[READ],
885 td->nr_queued[WRITE]);
886
887 nr_disp = throtl_select_dispatch(td, &bio_list_on_stack);
888
889 if (nr_disp)
890 throtl_log(td, "bios disp=%u", nr_disp);
891
892 throtl_schedule_next_dispatch(td);
893out:
894 spin_unlock_irq(q->queue_lock);
895
896 /*
897 * If we dispatched some requests, unplug the queue to make sure
898 * immediate dispatch
899 */
900 if (nr_disp) {
Vivek Goyal69d60eb2011-03-09 08:27:37 +0100901 blk_start_plug(&plug);
Vivek Goyale43473b2010-09-15 17:06:35 -0400902 while((bio = bio_list_pop(&bio_list_on_stack)))
903 generic_make_request(bio);
Vivek Goyal69d60eb2011-03-09 08:27:37 +0100904 blk_finish_plug(&plug);
Vivek Goyale43473b2010-09-15 17:06:35 -0400905 }
906 return nr_disp;
907}
908
909void blk_throtl_work(struct work_struct *work)
910{
911 struct throtl_data *td = container_of(work, struct throtl_data,
912 throtl_work.work);
913 struct request_queue *q = td->queue;
914
915 throtl_dispatch(q);
916}
917
918/* Call with queue lock held */
Vivek Goyal450adcb2011-03-01 13:40:54 -0500919static void
920throtl_schedule_delayed_work(struct throtl_data *td, unsigned long delay)
Vivek Goyale43473b2010-09-15 17:06:35 -0400921{
922
Vivek Goyale43473b2010-09-15 17:06:35 -0400923 struct delayed_work *dwork = &td->throtl_work;
924
Vivek Goyal04521db2011-03-22 21:54:29 +0100925 /* schedule work if limits changed even if no bio is queued */
926 if (total_nr_queued(td) > 0 || td->limits_changed) {
Vivek Goyale43473b2010-09-15 17:06:35 -0400927 /*
928 * We might have a work scheduled to be executed in future.
929 * Cancel that and schedule a new one.
930 */
931 __cancel_delayed_work(dwork);
Vivek Goyal450adcb2011-03-01 13:40:54 -0500932 queue_delayed_work(kthrotld_workqueue, dwork, delay);
Vivek Goyale43473b2010-09-15 17:06:35 -0400933 throtl_log(td, "schedule work. delay=%lu jiffies=%lu",
934 delay, jiffies);
935 }
936}
Vivek Goyale43473b2010-09-15 17:06:35 -0400937
938static void
939throtl_destroy_tg(struct throtl_data *td, struct throtl_grp *tg)
940{
941 /* Something wrong if we are trying to remove same group twice */
942 BUG_ON(hlist_unhashed(&tg->tg_node));
943
944 hlist_del_init(&tg->tg_node);
945
946 /*
947 * Put the reference taken at the time of creation so that when all
948 * queues are gone, group can be destroyed.
949 */
950 throtl_put_tg(tg);
951 td->nr_undestroyed_grps--;
952}
953
954static void throtl_release_tgs(struct throtl_data *td)
955{
956 struct hlist_node *pos, *n;
957 struct throtl_grp *tg;
958
959 hlist_for_each_entry_safe(tg, pos, n, &td->tg_list, tg_node) {
960 /*
961 * If cgroup removal path got to blk_group first and removed
962 * it from cgroup list, then it will take care of destroying
963 * cfqg also.
964 */
965 if (!blkiocg_del_blkio_group(&tg->blkg))
966 throtl_destroy_tg(td, tg);
967 }
968}
969
970static void throtl_td_free(struct throtl_data *td)
971{
972 kfree(td);
973}
974
975/*
976 * Blk cgroup controller notification saying that blkio_group object is being
977 * delinked as associated cgroup object is going away. That also means that
978 * no new IO will come in this group. So get rid of this group as soon as
979 * any pending IO in the group is finished.
980 *
981 * This function is called under rcu_read_lock(). key is the rcu protected
982 * pointer. That means "key" is a valid throtl_data pointer as long as we are
983 * rcu read lock.
984 *
985 * "key" was fetched from blkio_group under blkio_cgroup->lock. That means
986 * it should not be NULL as even if queue was going away, cgroup deltion
987 * path got to it first.
988 */
989void throtl_unlink_blkio_group(void *key, struct blkio_group *blkg)
990{
991 unsigned long flags;
992 struct throtl_data *td = key;
993
994 spin_lock_irqsave(td->queue->queue_lock, flags);
995 throtl_destroy_tg(td, tg_of_blkg(blkg));
996 spin_unlock_irqrestore(td->queue->queue_lock, flags);
997}
998
Vivek Goyalde701c72011-03-07 21:09:32 +0100999static void throtl_update_blkio_group_common(struct throtl_data *td,
1000 struct throtl_grp *tg)
1001{
1002 xchg(&tg->limits_changed, true);
1003 xchg(&td->limits_changed, true);
1004 /* Schedule a work now to process the limit change */
1005 throtl_schedule_delayed_work(td, 0);
1006}
1007
Vivek Goyalfe071432010-10-01 14:49:49 +02001008/*
1009 * For all update functions, key should be a valid pointer because these
1010 * update functions are called under blkcg_lock, that means, blkg is
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001011 * valid and in turn key is valid. queue exit path can not race because
Vivek Goyalfe071432010-10-01 14:49:49 +02001012 * of blkcg_lock
1013 *
1014 * Can not take queue lock in update functions as queue lock under blkcg_lock
1015 * is not allowed. Under other paths we take blkcg_lock under queue_lock.
1016 */
1017static void throtl_update_blkio_group_read_bps(void *key,
1018 struct blkio_group *blkg, u64 read_bps)
Vivek Goyale43473b2010-09-15 17:06:35 -04001019{
Vivek Goyalfe071432010-10-01 14:49:49 +02001020 struct throtl_data *td = key;
Vivek Goyalde701c72011-03-07 21:09:32 +01001021 struct throtl_grp *tg = tg_of_blkg(blkg);
Vivek Goyalfe071432010-10-01 14:49:49 +02001022
Vivek Goyalde701c72011-03-07 21:09:32 +01001023 tg->bps[READ] = read_bps;
1024 throtl_update_blkio_group_common(td, tg);
Vivek Goyale43473b2010-09-15 17:06:35 -04001025}
1026
Vivek Goyalfe071432010-10-01 14:49:49 +02001027static void throtl_update_blkio_group_write_bps(void *key,
1028 struct blkio_group *blkg, u64 write_bps)
Vivek Goyale43473b2010-09-15 17:06:35 -04001029{
Vivek Goyalfe071432010-10-01 14:49:49 +02001030 struct throtl_data *td = key;
Vivek Goyalde701c72011-03-07 21:09:32 +01001031 struct throtl_grp *tg = tg_of_blkg(blkg);
Vivek Goyalfe071432010-10-01 14:49:49 +02001032
Vivek Goyalde701c72011-03-07 21:09:32 +01001033 tg->bps[WRITE] = write_bps;
1034 throtl_update_blkio_group_common(td, tg);
Vivek Goyale43473b2010-09-15 17:06:35 -04001035}
1036
Vivek Goyalfe071432010-10-01 14:49:49 +02001037static void throtl_update_blkio_group_read_iops(void *key,
1038 struct blkio_group *blkg, unsigned int read_iops)
Vivek Goyal8e89d132010-09-15 17:06:37 -04001039{
Vivek Goyalfe071432010-10-01 14:49:49 +02001040 struct throtl_data *td = key;
Vivek Goyalde701c72011-03-07 21:09:32 +01001041 struct throtl_grp *tg = tg_of_blkg(blkg);
Vivek Goyalfe071432010-10-01 14:49:49 +02001042
Vivek Goyalde701c72011-03-07 21:09:32 +01001043 tg->iops[READ] = read_iops;
1044 throtl_update_blkio_group_common(td, tg);
Vivek Goyal8e89d132010-09-15 17:06:37 -04001045}
1046
Vivek Goyalfe071432010-10-01 14:49:49 +02001047static void throtl_update_blkio_group_write_iops(void *key,
1048 struct blkio_group *blkg, unsigned int write_iops)
Vivek Goyal8e89d132010-09-15 17:06:37 -04001049{
Vivek Goyalfe071432010-10-01 14:49:49 +02001050 struct throtl_data *td = key;
Vivek Goyalde701c72011-03-07 21:09:32 +01001051 struct throtl_grp *tg = tg_of_blkg(blkg);
Vivek Goyalfe071432010-10-01 14:49:49 +02001052
Vivek Goyalde701c72011-03-07 21:09:32 +01001053 tg->iops[WRITE] = write_iops;
1054 throtl_update_blkio_group_common(td, tg);
Vivek Goyal8e89d132010-09-15 17:06:37 -04001055}
1056
Vivek Goyalda527772011-03-02 19:05:33 -05001057static void throtl_shutdown_wq(struct request_queue *q)
Vivek Goyale43473b2010-09-15 17:06:35 -04001058{
1059 struct throtl_data *td = q->td;
1060
1061 cancel_delayed_work_sync(&td->throtl_work);
1062}
1063
1064static struct blkio_policy_type blkio_policy_throtl = {
1065 .ops = {
1066 .blkio_unlink_group_fn = throtl_unlink_blkio_group,
1067 .blkio_update_group_read_bps_fn =
1068 throtl_update_blkio_group_read_bps,
1069 .blkio_update_group_write_bps_fn =
1070 throtl_update_blkio_group_write_bps,
Vivek Goyal8e89d132010-09-15 17:06:37 -04001071 .blkio_update_group_read_iops_fn =
1072 throtl_update_blkio_group_read_iops,
1073 .blkio_update_group_write_iops_fn =
1074 throtl_update_blkio_group_write_iops,
Vivek Goyale43473b2010-09-15 17:06:35 -04001075 },
Vivek Goyal8e89d132010-09-15 17:06:37 -04001076 .plid = BLKIO_POLICY_THROTL,
Vivek Goyale43473b2010-09-15 17:06:35 -04001077};
1078
1079int blk_throtl_bio(struct request_queue *q, struct bio **biop)
1080{
1081 struct throtl_data *td = q->td;
1082 struct throtl_grp *tg;
1083 struct bio *bio = *biop;
1084 bool rw = bio_data_dir(bio), update_disptime = true;
1085
1086 if (bio->bi_rw & REQ_THROTTLED) {
1087 bio->bi_rw &= ~REQ_THROTTLED;
1088 return 0;
1089 }
1090
1091 spin_lock_irq(q->queue_lock);
1092 tg = throtl_get_tg(td);
1093
Vivek Goyalf469a7b2011-05-19 15:38:23 -04001094 if (IS_ERR(tg)) {
1095 if (PTR_ERR(tg) == -ENODEV) {
1096 /*
1097 * Queue is gone. No queue lock held here.
1098 */
1099 return -ENODEV;
1100 }
1101 }
1102
Vivek Goyale43473b2010-09-15 17:06:35 -04001103 if (tg->nr_queued[rw]) {
1104 /*
1105 * There is already another bio queued in same dir. No
1106 * need to update dispatch time.
1107 */
Vivek Goyal231d7042011-03-07 21:05:14 +01001108 update_disptime = false;
Vivek Goyale43473b2010-09-15 17:06:35 -04001109 goto queue_bio;
Vivek Goyalde701c72011-03-07 21:09:32 +01001110
Vivek Goyale43473b2010-09-15 17:06:35 -04001111 }
1112
1113 /* Bio is with-in rate limit of group */
1114 if (tg_may_dispatch(td, tg, bio, NULL)) {
1115 throtl_charge_bio(tg, bio);
Vivek Goyal04521db2011-03-22 21:54:29 +01001116
1117 /*
1118 * We need to trim slice even when bios are not being queued
1119 * otherwise it might happen that a bio is not queued for
1120 * a long time and slice keeps on extending and trim is not
1121 * called for a long time. Now if limits are reduced suddenly
1122 * we take into account all the IO dispatched so far at new
1123 * low rate and * newly queued IO gets a really long dispatch
1124 * time.
1125 *
1126 * So keep on trimming slice even if bio is not queued.
1127 */
1128 throtl_trim_slice(td, tg, rw);
Vivek Goyale43473b2010-09-15 17:06:35 -04001129 goto out;
1130 }
1131
1132queue_bio:
Vivek Goyal8e89d132010-09-15 17:06:37 -04001133 throtl_log_tg(td, tg, "[%c] bio. bdisp=%u sz=%u bps=%llu"
1134 " iodisp=%u iops=%u queued=%d/%d",
1135 rw == READ ? 'R' : 'W',
Vivek Goyale43473b2010-09-15 17:06:35 -04001136 tg->bytes_disp[rw], bio->bi_size, tg->bps[rw],
Vivek Goyal8e89d132010-09-15 17:06:37 -04001137 tg->io_disp[rw], tg->iops[rw],
Vivek Goyale43473b2010-09-15 17:06:35 -04001138 tg->nr_queued[READ], tg->nr_queued[WRITE]);
1139
1140 throtl_add_bio_tg(q->td, tg, bio);
1141 *biop = NULL;
1142
1143 if (update_disptime) {
1144 tg_update_disptime(td, tg);
1145 throtl_schedule_next_dispatch(td);
1146 }
1147
1148out:
1149 spin_unlock_irq(q->queue_lock);
1150 return 0;
1151}
1152
1153int blk_throtl_init(struct request_queue *q)
1154{
1155 struct throtl_data *td;
1156 struct throtl_grp *tg;
1157
1158 td = kzalloc_node(sizeof(*td), GFP_KERNEL, q->node);
1159 if (!td)
1160 return -ENOMEM;
1161
1162 INIT_HLIST_HEAD(&td->tg_list);
1163 td->tg_service_tree = THROTL_RB_ROOT;
Vivek Goyalde701c72011-03-07 21:09:32 +01001164 td->limits_changed = false;
Vivek Goyala29a1712011-05-19 15:38:19 -04001165 INIT_DELAYED_WORK(&td->throtl_work, blk_throtl_work);
Vivek Goyale43473b2010-09-15 17:06:35 -04001166
Vivek Goyal29b12582011-05-19 15:38:24 -04001167 /* alloc and Init root group. */
1168 td->queue = q;
1169 tg = throtl_alloc_tg(td);
Vivek Goyal02977e42010-10-01 14:49:48 +02001170
Vivek Goyal29b12582011-05-19 15:38:24 -04001171 if (!tg) {
1172 kfree(td);
1173 return -ENOMEM;
1174 }
1175
1176 td->root_tg = tg;
Vivek Goyale43473b2010-09-15 17:06:35 -04001177
1178 rcu_read_lock();
Vivek Goyal5617cbe2011-05-19 15:38:26 -04001179 throtl_init_add_tg_lists(td, tg, &blkio_root_cgroup);
Vivek Goyale43473b2010-09-15 17:06:35 -04001180 rcu_read_unlock();
1181
1182 /* Attach throtl data to request queue */
Vivek Goyale43473b2010-09-15 17:06:35 -04001183 q->td = td;
1184 return 0;
1185}
1186
1187void blk_throtl_exit(struct request_queue *q)
1188{
1189 struct throtl_data *td = q->td;
1190 bool wait = false;
1191
1192 BUG_ON(!td);
1193
Vivek Goyalda527772011-03-02 19:05:33 -05001194 throtl_shutdown_wq(q);
Vivek Goyale43473b2010-09-15 17:06:35 -04001195
1196 spin_lock_irq(q->queue_lock);
1197 throtl_release_tgs(td);
Vivek Goyale43473b2010-09-15 17:06:35 -04001198
1199 /* If there are other groups */
Vivek Goyal02977e42010-10-01 14:49:48 +02001200 if (td->nr_undestroyed_grps > 0)
Vivek Goyale43473b2010-09-15 17:06:35 -04001201 wait = true;
1202
1203 spin_unlock_irq(q->queue_lock);
1204
1205 /*
1206 * Wait for tg->blkg->key accessors to exit their grace periods.
1207 * Do this wait only if there are other undestroyed groups out
1208 * there (other than root group). This can happen if cgroup deletion
1209 * path claimed the responsibility of cleaning up a group before
1210 * queue cleanup code get to the group.
1211 *
1212 * Do not call synchronize_rcu() unconditionally as there are drivers
1213 * which create/delete request queue hundreds of times during scan/boot
1214 * and synchronize_rcu() can take significant time and slow down boot.
1215 */
1216 if (wait)
1217 synchronize_rcu();
Vivek Goyalfe071432010-10-01 14:49:49 +02001218
1219 /*
1220 * Just being safe to make sure after previous flush if some body did
1221 * update limits through cgroup and another work got queued, cancel
1222 * it.
1223 */
Vivek Goyalda527772011-03-02 19:05:33 -05001224 throtl_shutdown_wq(q);
Vivek Goyale43473b2010-09-15 17:06:35 -04001225 throtl_td_free(td);
1226}
1227
1228static int __init throtl_init(void)
1229{
Vivek Goyal450adcb2011-03-01 13:40:54 -05001230 kthrotld_workqueue = alloc_workqueue("kthrotld", WQ_MEM_RECLAIM, 0);
1231 if (!kthrotld_workqueue)
1232 panic("Failed to create kthrotld\n");
1233
Vivek Goyale43473b2010-09-15 17:06:35 -04001234 blkio_policy_register(&blkio_policy_throtl);
1235 return 0;
1236}
1237
1238module_init(throtl_init);