blob: 900a0c98745bb5168352002cf0a5cd23f795c18b [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"
Tejun Heobc9fcbf2011-10-19 14:31:18 +020013#include "blk.h"
Vivek Goyale43473b2010-09-15 17:06:35 -040014
15/* Max dispatch from a group in 1 round */
16static int throtl_grp_quantum = 8;
17
18/* Total max dispatch from all groups in one round */
19static int throtl_quantum = 32;
20
21/* Throttling is performed over 100ms slice and after that slice is renewed */
22static unsigned long throtl_slice = HZ/10; /* 100 ms */
23
Vivek Goyal450adcb2011-03-01 13:40:54 -050024/* A workqueue to queue throttle related work */
25static struct workqueue_struct *kthrotld_workqueue;
26static void throtl_schedule_delayed_work(struct throtl_data *td,
27 unsigned long delay);
28
Vivek Goyale43473b2010-09-15 17:06:35 -040029struct throtl_rb_root {
30 struct rb_root rb;
31 struct rb_node *left;
32 unsigned int count;
33 unsigned long min_disptime;
34};
35
36#define THROTL_RB_ROOT (struct throtl_rb_root) { .rb = RB_ROOT, .left = NULL, \
37 .count = 0, .min_disptime = 0}
38
39#define rb_entry_tg(node) rb_entry((node), struct throtl_grp, rb_node)
40
41struct throtl_grp {
42 /* List of throtl groups on the request queue*/
43 struct hlist_node tg_node;
44
45 /* active throtl group service_tree member */
46 struct rb_node rb_node;
47
48 /*
49 * Dispatch time in jiffies. This is the estimated time when group
50 * will unthrottle and is ready to dispatch more bio. It is used as
51 * key to sort active groups in service tree.
52 */
53 unsigned long disptime;
54
55 struct blkio_group blkg;
56 atomic_t ref;
57 unsigned int flags;
58
59 /* Two lists for READ and WRITE */
60 struct bio_list bio_lists[2];
61
62 /* Number of queued bios on READ and WRITE lists */
63 unsigned int nr_queued[2];
64
65 /* bytes per second rate limits */
66 uint64_t bps[2];
67
Vivek Goyal8e89d132010-09-15 17:06:37 -040068 /* IOPS limits */
69 unsigned int iops[2];
70
Vivek Goyale43473b2010-09-15 17:06:35 -040071 /* Number of bytes disptached in current slice */
72 uint64_t bytes_disp[2];
Vivek Goyal8e89d132010-09-15 17:06:37 -040073 /* Number of bio's dispatched in current slice */
74 unsigned int io_disp[2];
Vivek Goyale43473b2010-09-15 17:06:35 -040075
76 /* When did we start a new slice */
77 unsigned long slice_start[2];
78 unsigned long slice_end[2];
Vivek Goyalfe071432010-10-01 14:49:49 +020079
80 /* Some throttle limits got updated for the group */
Andreas Schwab6f037932011-03-30 12:21:56 +020081 int limits_changed;
Vivek Goyal4843c692011-05-19 15:38:27 -040082
83 struct rcu_head rcu_head;
Vivek Goyale43473b2010-09-15 17:06:35 -040084};
85
86struct throtl_data
87{
88 /* List of throtl groups */
89 struct hlist_head tg_list;
90
91 /* service tree for active throtl groups */
92 struct throtl_rb_root tg_service_tree;
93
Vivek Goyal29b12582011-05-19 15:38:24 -040094 struct throtl_grp *root_tg;
Vivek Goyale43473b2010-09-15 17:06:35 -040095 struct request_queue *queue;
96
97 /* Total Number of queued bios on READ and WRITE lists */
98 unsigned int nr_queued[2];
99
100 /*
Vivek Goyal02977e42010-10-01 14:49:48 +0200101 * number of total undestroyed groups
Vivek Goyale43473b2010-09-15 17:06:35 -0400102 */
103 unsigned int nr_undestroyed_grps;
104
105 /* Work for dispatching throttled bios */
106 struct delayed_work throtl_work;
Vivek Goyalfe071432010-10-01 14:49:49 +0200107
Andreas Schwab6f037932011-03-30 12:21:56 +0200108 int limits_changed;
Vivek Goyale43473b2010-09-15 17:06:35 -0400109};
110
111enum tg_state_flags {
112 THROTL_TG_FLAG_on_rr = 0, /* on round-robin busy list */
113};
114
115#define THROTL_TG_FNS(name) \
116static inline void throtl_mark_tg_##name(struct throtl_grp *tg) \
117{ \
118 (tg)->flags |= (1 << THROTL_TG_FLAG_##name); \
119} \
120static inline void throtl_clear_tg_##name(struct throtl_grp *tg) \
121{ \
122 (tg)->flags &= ~(1 << THROTL_TG_FLAG_##name); \
123} \
124static inline int throtl_tg_##name(const struct throtl_grp *tg) \
125{ \
126 return ((tg)->flags & (1 << THROTL_TG_FLAG_##name)) != 0; \
127}
128
129THROTL_TG_FNS(on_rr);
130
131#define throtl_log_tg(td, tg, fmt, args...) \
132 blk_add_trace_msg((td)->queue, "throtl %s " fmt, \
133 blkg_path(&(tg)->blkg), ##args); \
134
135#define throtl_log(td, fmt, args...) \
136 blk_add_trace_msg((td)->queue, "throtl " fmt, ##args)
137
138static inline struct throtl_grp *tg_of_blkg(struct blkio_group *blkg)
139{
140 if (blkg)
141 return container_of(blkg, struct throtl_grp, blkg);
142
143 return NULL;
144}
145
Joe Perchesd2f31a52011-06-13 20:19:27 +0200146static inline unsigned int total_nr_queued(struct throtl_data *td)
Vivek Goyale43473b2010-09-15 17:06:35 -0400147{
Joe Perchesd2f31a52011-06-13 20:19:27 +0200148 return td->nr_queued[0] + td->nr_queued[1];
Vivek Goyale43473b2010-09-15 17:06:35 -0400149}
150
151static inline struct throtl_grp *throtl_ref_get_tg(struct throtl_grp *tg)
152{
153 atomic_inc(&tg->ref);
154 return tg;
155}
156
Vivek Goyal4843c692011-05-19 15:38:27 -0400157static void throtl_free_tg(struct rcu_head *head)
158{
159 struct throtl_grp *tg;
160
161 tg = container_of(head, struct throtl_grp, rcu_head);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400162 free_percpu(tg->blkg.stats_cpu);
Vivek Goyal4843c692011-05-19 15:38:27 -0400163 kfree(tg);
164}
165
Vivek Goyale43473b2010-09-15 17:06:35 -0400166static void throtl_put_tg(struct throtl_grp *tg)
167{
168 BUG_ON(atomic_read(&tg->ref) <= 0);
169 if (!atomic_dec_and_test(&tg->ref))
170 return;
Vivek Goyal4843c692011-05-19 15:38:27 -0400171
172 /*
173 * A group is freed in rcu manner. But having an rcu lock does not
174 * mean that one can access all the fields of blkg and assume these
175 * are valid. For example, don't try to follow throtl_data and
176 * request queue links.
177 *
178 * Having a reference to blkg under an rcu allows acess to only
179 * values local to groups like group stats and group rate limits
180 */
181 call_rcu(&tg->rcu_head, throtl_free_tg);
Vivek Goyale43473b2010-09-15 17:06:35 -0400182}
183
Vivek Goyala29a1712011-05-19 15:38:19 -0400184static void throtl_init_group(struct throtl_grp *tg)
185{
186 INIT_HLIST_NODE(&tg->tg_node);
187 RB_CLEAR_NODE(&tg->rb_node);
188 bio_list_init(&tg->bio_lists[0]);
189 bio_list_init(&tg->bio_lists[1]);
190 tg->limits_changed = false;
191
192 /* Practically unlimited BW */
193 tg->bps[0] = tg->bps[1] = -1;
194 tg->iops[0] = tg->iops[1] = -1;
195
196 /*
197 * Take the initial reference that will be released on destroy
198 * This can be thought of a joint reference by cgroup and
199 * request queue which will be dropped by either request queue
200 * exit or cgroup deletion path depending on who is exiting first.
201 */
202 atomic_set(&tg->ref, 1);
203}
204
205/* Should be called with rcu read lock held (needed for blkcg) */
206static void
207throtl_add_group_to_td_list(struct throtl_data *td, struct throtl_grp *tg)
208{
209 hlist_add_head(&tg->tg_node, &td->tg_list);
210 td->nr_undestroyed_grps++;
211}
212
Vivek Goyal269f5412011-05-19 15:38:25 -0400213static void
214__throtl_tg_fill_dev_details(struct throtl_data *td, struct throtl_grp *tg)
Vivek Goyalf469a7b2011-05-19 15:38:23 -0400215{
216 struct backing_dev_info *bdi = &td->queue->backing_dev_info;
217 unsigned int major, minor;
218
Vivek Goyal269f5412011-05-19 15:38:25 -0400219 if (!tg || tg->blkg.dev)
220 return;
221
222 /*
223 * Fill in device details for a group which might not have been
224 * filled at group creation time as queue was being instantiated
225 * and driver had not attached a device yet
226 */
227 if (bdi->dev && dev_name(bdi->dev)) {
228 sscanf(dev_name(bdi->dev), "%u:%u", &major, &minor);
229 tg->blkg.dev = MKDEV(major, minor);
230 }
231}
232
Vivek Goyalaf75cd32011-05-19 15:38:31 -0400233/*
234 * Should be called with without queue lock held. Here queue lock will be
235 * taken rarely. It will be taken only once during life time of a group
236 * if need be
237 */
238static void
239throtl_tg_fill_dev_details(struct throtl_data *td, struct throtl_grp *tg)
240{
241 if (!tg || tg->blkg.dev)
242 return;
243
244 spin_lock_irq(td->queue->queue_lock);
245 __throtl_tg_fill_dev_details(td, tg);
246 spin_unlock_irq(td->queue->queue_lock);
247}
248
Vivek Goyal269f5412011-05-19 15:38:25 -0400249static void throtl_init_add_tg_lists(struct throtl_data *td,
250 struct throtl_grp *tg, struct blkio_cgroup *blkcg)
251{
252 __throtl_tg_fill_dev_details(td, tg);
253
Vivek Goyalf469a7b2011-05-19 15:38:23 -0400254 /* Add group onto cgroup list */
Vivek Goyalf469a7b2011-05-19 15:38:23 -0400255 blkiocg_add_blkio_group(blkcg, &tg->blkg, (void *)td,
Vivek Goyal269f5412011-05-19 15:38:25 -0400256 tg->blkg.dev, BLKIO_POLICY_THROTL);
Vivek Goyalf469a7b2011-05-19 15:38:23 -0400257
258 tg->bps[READ] = blkcg_get_read_bps(blkcg, tg->blkg.dev);
259 tg->bps[WRITE] = blkcg_get_write_bps(blkcg, tg->blkg.dev);
260 tg->iops[READ] = blkcg_get_read_iops(blkcg, tg->blkg.dev);
261 tg->iops[WRITE] = blkcg_get_write_iops(blkcg, tg->blkg.dev);
262
263 throtl_add_group_to_td_list(td, tg);
264}
265
266/* Should be called without queue lock and outside of rcu period */
267static struct throtl_grp *throtl_alloc_tg(struct throtl_data *td)
268{
269 struct throtl_grp *tg = NULL;
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400270 int ret;
Vivek Goyalf469a7b2011-05-19 15:38:23 -0400271
272 tg = kzalloc_node(sizeof(*tg), GFP_ATOMIC, td->queue->node);
273 if (!tg)
274 return NULL;
275
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400276 ret = blkio_alloc_blkg_stats(&tg->blkg);
277
278 if (ret) {
279 kfree(tg);
280 return NULL;
281 }
282
Vivek Goyalf469a7b2011-05-19 15:38:23 -0400283 throtl_init_group(tg);
284 return tg;
285}
286
287static struct
288throtl_grp *throtl_find_tg(struct throtl_data *td, struct blkio_cgroup *blkcg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400289{
Vivek Goyale43473b2010-09-15 17:06:35 -0400290 struct throtl_grp *tg = NULL;
291 void *key = td;
Vivek Goyale43473b2010-09-15 17:06:35 -0400292
293 /*
Vivek Goyalbe2c6b12011-01-19 08:25:02 -0700294 * This is the common case when there are no blkio cgroups.
295 * Avoid lookup in this case
296 */
297 if (blkcg == &blkio_root_cgroup)
Vivek Goyal29b12582011-05-19 15:38:24 -0400298 tg = td->root_tg;
Vivek Goyalbe2c6b12011-01-19 08:25:02 -0700299 else
300 tg = tg_of_blkg(blkiocg_lookup_group(blkcg, key));
Vivek Goyale43473b2010-09-15 17:06:35 -0400301
Vivek Goyal269f5412011-05-19 15:38:25 -0400302 __throtl_tg_fill_dev_details(td, tg);
Vivek Goyale43473b2010-09-15 17:06:35 -0400303 return tg;
304}
305
306static struct throtl_grp * throtl_get_tg(struct throtl_data *td)
307{
Vivek Goyalf469a7b2011-05-19 15:38:23 -0400308 struct throtl_grp *tg = NULL, *__tg = NULL;
Vivek Goyal70087dc2011-05-16 15:24:08 +0200309 struct blkio_cgroup *blkcg;
Vivek Goyalf469a7b2011-05-19 15:38:23 -0400310 struct request_queue *q = td->queue;
Vivek Goyale43473b2010-09-15 17:06:35 -0400311
312 rcu_read_lock();
Vivek Goyal70087dc2011-05-16 15:24:08 +0200313 blkcg = task_blkio_cgroup(current);
Vivek Goyalf469a7b2011-05-19 15:38:23 -0400314 tg = throtl_find_tg(td, blkcg);
315 if (tg) {
316 rcu_read_unlock();
317 return tg;
318 }
319
320 /*
321 * Need to allocate a group. Allocation of group also needs allocation
322 * of per cpu stats which in-turn takes a mutex() and can block. Hence
Tejun Heo315fcee2011-10-19 14:31:25 +0200323 * we need to drop rcu lock and queue_lock before we call alloc.
Vivek Goyalf469a7b2011-05-19 15:38:23 -0400324 */
Vivek Goyalf469a7b2011-05-19 15:38:23 -0400325 rcu_read_unlock();
326 spin_unlock_irq(q->queue_lock);
327
328 tg = throtl_alloc_tg(td);
Vivek Goyalf469a7b2011-05-19 15:38:23 -0400329
330 /* Group allocated and queue is still alive. take the lock */
331 spin_lock_irq(q->queue_lock);
332
Tejun Heobc16a4f2011-10-19 14:33:01 +0200333 /* Make sure @q is still alive */
334 if (unlikely(test_bit(QUEUE_FLAG_DEAD, &q->queue_flags))) {
335 kfree(tg);
336 return NULL;
337 }
338
Vivek Goyalf469a7b2011-05-19 15:38:23 -0400339 /*
340 * Initialize the new group. After sleeping, read the blkcg again.
341 */
342 rcu_read_lock();
343 blkcg = task_blkio_cgroup(current);
344
345 /*
346 * If some other thread already allocated the group while we were
347 * not holding queue lock, free up the group
348 */
349 __tg = throtl_find_tg(td, blkcg);
350
351 if (__tg) {
352 kfree(tg);
353 rcu_read_unlock();
354 return __tg;
355 }
356
357 /* Group allocation failed. Account the IO to root group */
358 if (!tg) {
Vivek Goyal29b12582011-05-19 15:38:24 -0400359 tg = td->root_tg;
Vivek Goyalf469a7b2011-05-19 15:38:23 -0400360 return tg;
361 }
362
363 throtl_init_add_tg_lists(td, tg, blkcg);
Vivek Goyale43473b2010-09-15 17:06:35 -0400364 rcu_read_unlock();
365 return tg;
366}
367
368static struct throtl_grp *throtl_rb_first(struct throtl_rb_root *root)
369{
370 /* Service tree is empty */
371 if (!root->count)
372 return NULL;
373
374 if (!root->left)
375 root->left = rb_first(&root->rb);
376
377 if (root->left)
378 return rb_entry_tg(root->left);
379
380 return NULL;
381}
382
383static void rb_erase_init(struct rb_node *n, struct rb_root *root)
384{
385 rb_erase(n, root);
386 RB_CLEAR_NODE(n);
387}
388
389static void throtl_rb_erase(struct rb_node *n, struct throtl_rb_root *root)
390{
391 if (root->left == n)
392 root->left = NULL;
393 rb_erase_init(n, &root->rb);
394 --root->count;
395}
396
397static void update_min_dispatch_time(struct throtl_rb_root *st)
398{
399 struct throtl_grp *tg;
400
401 tg = throtl_rb_first(st);
402 if (!tg)
403 return;
404
405 st->min_disptime = tg->disptime;
406}
407
408static void
409tg_service_tree_add(struct throtl_rb_root *st, struct throtl_grp *tg)
410{
411 struct rb_node **node = &st->rb.rb_node;
412 struct rb_node *parent = NULL;
413 struct throtl_grp *__tg;
414 unsigned long key = tg->disptime;
415 int left = 1;
416
417 while (*node != NULL) {
418 parent = *node;
419 __tg = rb_entry_tg(parent);
420
421 if (time_before(key, __tg->disptime))
422 node = &parent->rb_left;
423 else {
424 node = &parent->rb_right;
425 left = 0;
426 }
427 }
428
429 if (left)
430 st->left = &tg->rb_node;
431
432 rb_link_node(&tg->rb_node, parent, node);
433 rb_insert_color(&tg->rb_node, &st->rb);
434}
435
436static void __throtl_enqueue_tg(struct throtl_data *td, struct throtl_grp *tg)
437{
438 struct throtl_rb_root *st = &td->tg_service_tree;
439
440 tg_service_tree_add(st, tg);
441 throtl_mark_tg_on_rr(tg);
442 st->count++;
443}
444
445static void throtl_enqueue_tg(struct throtl_data *td, struct throtl_grp *tg)
446{
447 if (!throtl_tg_on_rr(tg))
448 __throtl_enqueue_tg(td, tg);
449}
450
451static void __throtl_dequeue_tg(struct throtl_data *td, struct throtl_grp *tg)
452{
453 throtl_rb_erase(&tg->rb_node, &td->tg_service_tree);
454 throtl_clear_tg_on_rr(tg);
455}
456
457static void throtl_dequeue_tg(struct throtl_data *td, struct throtl_grp *tg)
458{
459 if (throtl_tg_on_rr(tg))
460 __throtl_dequeue_tg(td, tg);
461}
462
463static void throtl_schedule_next_dispatch(struct throtl_data *td)
464{
465 struct throtl_rb_root *st = &td->tg_service_tree;
466
467 /*
468 * If there are more bios pending, schedule more work.
469 */
470 if (!total_nr_queued(td))
471 return;
472
473 BUG_ON(!st->count);
474
475 update_min_dispatch_time(st);
476
477 if (time_before_eq(st->min_disptime, jiffies))
Vivek Goyal450adcb2011-03-01 13:40:54 -0500478 throtl_schedule_delayed_work(td, 0);
Vivek Goyale43473b2010-09-15 17:06:35 -0400479 else
Vivek Goyal450adcb2011-03-01 13:40:54 -0500480 throtl_schedule_delayed_work(td, (st->min_disptime - jiffies));
Vivek Goyale43473b2010-09-15 17:06:35 -0400481}
482
483static inline void
484throtl_start_new_slice(struct throtl_data *td, struct throtl_grp *tg, bool rw)
485{
486 tg->bytes_disp[rw] = 0;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400487 tg->io_disp[rw] = 0;
Vivek Goyale43473b2010-09-15 17:06:35 -0400488 tg->slice_start[rw] = jiffies;
489 tg->slice_end[rw] = jiffies + throtl_slice;
490 throtl_log_tg(td, tg, "[%c] new slice start=%lu end=%lu jiffies=%lu",
491 rw == READ ? 'R' : 'W', tg->slice_start[rw],
492 tg->slice_end[rw], jiffies);
493}
494
Vivek Goyald1ae8ff2010-12-01 19:34:46 +0100495static inline void throtl_set_slice_end(struct throtl_data *td,
496 struct throtl_grp *tg, bool rw, unsigned long jiffy_end)
497{
498 tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
499}
500
Vivek Goyale43473b2010-09-15 17:06:35 -0400501static inline void throtl_extend_slice(struct throtl_data *td,
502 struct throtl_grp *tg, bool rw, unsigned long jiffy_end)
503{
504 tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
505 throtl_log_tg(td, tg, "[%c] extend slice start=%lu end=%lu jiffies=%lu",
506 rw == READ ? 'R' : 'W', tg->slice_start[rw],
507 tg->slice_end[rw], jiffies);
508}
509
510/* Determine if previously allocated or extended slice is complete or not */
511static bool
512throtl_slice_used(struct throtl_data *td, struct throtl_grp *tg, bool rw)
513{
514 if (time_in_range(jiffies, tg->slice_start[rw], tg->slice_end[rw]))
515 return 0;
516
517 return 1;
518}
519
520/* Trim the used slices and adjust slice start accordingly */
521static inline void
522throtl_trim_slice(struct throtl_data *td, struct throtl_grp *tg, bool rw)
523{
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200524 unsigned long nr_slices, time_elapsed, io_trim;
525 u64 bytes_trim, tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400526
527 BUG_ON(time_before(tg->slice_end[rw], tg->slice_start[rw]));
528
529 /*
530 * If bps are unlimited (-1), then time slice don't get
531 * renewed. Don't try to trim the slice if slice is used. A new
532 * slice will start when appropriate.
533 */
534 if (throtl_slice_used(td, tg, rw))
535 return;
536
Vivek Goyald1ae8ff2010-12-01 19:34:46 +0100537 /*
538 * A bio has been dispatched. Also adjust slice_end. It might happen
539 * that initially cgroup limit was very low resulting in high
540 * slice_end, but later limit was bumped up and bio was dispached
541 * sooner, then we need to reduce slice_end. A high bogus slice_end
542 * is bad because it does not allow new slice to start.
543 */
544
545 throtl_set_slice_end(td, tg, rw, jiffies + throtl_slice);
546
Vivek Goyale43473b2010-09-15 17:06:35 -0400547 time_elapsed = jiffies - tg->slice_start[rw];
548
549 nr_slices = time_elapsed / throtl_slice;
550
551 if (!nr_slices)
552 return;
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200553 tmp = tg->bps[rw] * throtl_slice * nr_slices;
554 do_div(tmp, HZ);
555 bytes_trim = tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400556
Vivek Goyal8e89d132010-09-15 17:06:37 -0400557 io_trim = (tg->iops[rw] * throtl_slice * nr_slices)/HZ;
Vivek Goyale43473b2010-09-15 17:06:35 -0400558
Vivek Goyal8e89d132010-09-15 17:06:37 -0400559 if (!bytes_trim && !io_trim)
Vivek Goyale43473b2010-09-15 17:06:35 -0400560 return;
561
562 if (tg->bytes_disp[rw] >= bytes_trim)
563 tg->bytes_disp[rw] -= bytes_trim;
564 else
565 tg->bytes_disp[rw] = 0;
566
Vivek Goyal8e89d132010-09-15 17:06:37 -0400567 if (tg->io_disp[rw] >= io_trim)
568 tg->io_disp[rw] -= io_trim;
569 else
570 tg->io_disp[rw] = 0;
571
Vivek Goyale43473b2010-09-15 17:06:35 -0400572 tg->slice_start[rw] += nr_slices * throtl_slice;
573
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200574 throtl_log_tg(td, tg, "[%c] trim slice nr=%lu bytes=%llu io=%lu"
Vivek Goyale43473b2010-09-15 17:06:35 -0400575 " start=%lu end=%lu jiffies=%lu",
Vivek Goyal8e89d132010-09-15 17:06:37 -0400576 rw == READ ? 'R' : 'W', nr_slices, bytes_trim, io_trim,
Vivek Goyale43473b2010-09-15 17:06:35 -0400577 tg->slice_start[rw], tg->slice_end[rw], jiffies);
578}
579
Vivek Goyal8e89d132010-09-15 17:06:37 -0400580static bool tg_with_in_iops_limit(struct throtl_data *td, struct throtl_grp *tg,
581 struct bio *bio, unsigned long *wait)
Vivek Goyale43473b2010-09-15 17:06:35 -0400582{
583 bool rw = bio_data_dir(bio);
Vivek Goyal8e89d132010-09-15 17:06:37 -0400584 unsigned int io_allowed;
Vivek Goyale43473b2010-09-15 17:06:35 -0400585 unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
Vivek Goyalc49c06e2010-10-01 21:16:42 +0200586 u64 tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400587
Vivek Goyal8e89d132010-09-15 17:06:37 -0400588 jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
Vivek Goyale43473b2010-09-15 17:06:35 -0400589
Vivek Goyal8e89d132010-09-15 17:06:37 -0400590 /* Slice has just started. Consider one slice interval */
591 if (!jiffy_elapsed)
592 jiffy_elapsed_rnd = throtl_slice;
593
594 jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
595
Vivek Goyalc49c06e2010-10-01 21:16:42 +0200596 /*
597 * jiffy_elapsed_rnd should not be a big value as minimum iops can be
598 * 1 then at max jiffy elapsed should be equivalent of 1 second as we
599 * will allow dispatch after 1 second and after that slice should
600 * have been trimmed.
601 */
602
603 tmp = (u64)tg->iops[rw] * jiffy_elapsed_rnd;
604 do_div(tmp, HZ);
605
606 if (tmp > UINT_MAX)
607 io_allowed = UINT_MAX;
608 else
609 io_allowed = tmp;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400610
611 if (tg->io_disp[rw] + 1 <= io_allowed) {
Vivek Goyale43473b2010-09-15 17:06:35 -0400612 if (wait)
613 *wait = 0;
614 return 1;
615 }
616
Vivek Goyal8e89d132010-09-15 17:06:37 -0400617 /* Calc approx time to dispatch */
618 jiffy_wait = ((tg->io_disp[rw] + 1) * HZ)/tg->iops[rw] + 1;
619
620 if (jiffy_wait > jiffy_elapsed)
621 jiffy_wait = jiffy_wait - jiffy_elapsed;
622 else
623 jiffy_wait = 1;
624
625 if (wait)
626 *wait = jiffy_wait;
627 return 0;
628}
629
630static bool tg_with_in_bps_limit(struct throtl_data *td, struct throtl_grp *tg,
631 struct bio *bio, unsigned long *wait)
632{
633 bool rw = bio_data_dir(bio);
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200634 u64 bytes_allowed, extra_bytes, tmp;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400635 unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
Vivek Goyale43473b2010-09-15 17:06:35 -0400636
637 jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
638
639 /* Slice has just started. Consider one slice interval */
640 if (!jiffy_elapsed)
641 jiffy_elapsed_rnd = throtl_slice;
642
643 jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
644
Vivek Goyal5e901a22010-10-01 21:16:38 +0200645 tmp = tg->bps[rw] * jiffy_elapsed_rnd;
646 do_div(tmp, HZ);
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200647 bytes_allowed = tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400648
649 if (tg->bytes_disp[rw] + bio->bi_size <= bytes_allowed) {
650 if (wait)
651 *wait = 0;
652 return 1;
653 }
654
655 /* Calc approx time to dispatch */
656 extra_bytes = tg->bytes_disp[rw] + bio->bi_size - bytes_allowed;
657 jiffy_wait = div64_u64(extra_bytes * HZ, tg->bps[rw]);
658
659 if (!jiffy_wait)
660 jiffy_wait = 1;
661
662 /*
663 * This wait time is without taking into consideration the rounding
664 * up we did. Add that time also.
665 */
666 jiffy_wait = jiffy_wait + (jiffy_elapsed_rnd - jiffy_elapsed);
Vivek Goyale43473b2010-09-15 17:06:35 -0400667 if (wait)
668 *wait = jiffy_wait;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400669 return 0;
670}
Vivek Goyale43473b2010-09-15 17:06:35 -0400671
Vivek Goyalaf75cd32011-05-19 15:38:31 -0400672static bool tg_no_rule_group(struct throtl_grp *tg, bool rw) {
673 if (tg->bps[rw] == -1 && tg->iops[rw] == -1)
674 return 1;
675 return 0;
676}
677
Vivek Goyal8e89d132010-09-15 17:06:37 -0400678/*
679 * Returns whether one can dispatch a bio or not. Also returns approx number
680 * of jiffies to wait before this bio is with-in IO rate and can be dispatched
681 */
682static bool tg_may_dispatch(struct throtl_data *td, struct throtl_grp *tg,
683 struct bio *bio, unsigned long *wait)
684{
685 bool rw = bio_data_dir(bio);
686 unsigned long bps_wait = 0, iops_wait = 0, max_wait = 0;
687
688 /*
689 * Currently whole state machine of group depends on first bio
690 * queued in the group bio list. So one should not be calling
691 * this function with a different bio if there are other bios
692 * queued.
693 */
694 BUG_ON(tg->nr_queued[rw] && bio != bio_list_peek(&tg->bio_lists[rw]));
695
696 /* If tg->bps = -1, then BW is unlimited */
697 if (tg->bps[rw] == -1 && tg->iops[rw] == -1) {
698 if (wait)
699 *wait = 0;
700 return 1;
701 }
702
703 /*
704 * If previous slice expired, start a new one otherwise renew/extend
705 * existing slice to make sure it is at least throtl_slice interval
706 * long since now.
707 */
708 if (throtl_slice_used(td, tg, rw))
709 throtl_start_new_slice(td, tg, rw);
710 else {
711 if (time_before(tg->slice_end[rw], jiffies + throtl_slice))
712 throtl_extend_slice(td, tg, rw, jiffies + throtl_slice);
713 }
714
715 if (tg_with_in_bps_limit(td, tg, bio, &bps_wait)
716 && tg_with_in_iops_limit(td, tg, bio, &iops_wait)) {
717 if (wait)
718 *wait = 0;
719 return 1;
720 }
721
722 max_wait = max(bps_wait, iops_wait);
723
724 if (wait)
725 *wait = max_wait;
726
727 if (time_before(tg->slice_end[rw], jiffies + max_wait))
728 throtl_extend_slice(td, tg, rw, jiffies + max_wait);
Vivek Goyale43473b2010-09-15 17:06:35 -0400729
730 return 0;
731}
732
733static void throtl_charge_bio(struct throtl_grp *tg, struct bio *bio)
734{
735 bool rw = bio_data_dir(bio);
Shaohua Lie5a94f52011-08-01 10:31:06 +0200736 bool sync = rw_is_sync(bio->bi_rw);
Vivek Goyale43473b2010-09-15 17:06:35 -0400737
738 /* Charge the bio to the group */
739 tg->bytes_disp[rw] += bio->bi_size;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400740 tg->io_disp[rw]++;
Vivek Goyale43473b2010-09-15 17:06:35 -0400741
Vivek Goyale43473b2010-09-15 17:06:35 -0400742 blkiocg_update_dispatch_stats(&tg->blkg, bio->bi_size, rw, sync);
Vivek Goyale43473b2010-09-15 17:06:35 -0400743}
744
745static void throtl_add_bio_tg(struct throtl_data *td, struct throtl_grp *tg,
746 struct bio *bio)
747{
748 bool rw = bio_data_dir(bio);
749
750 bio_list_add(&tg->bio_lists[rw], bio);
751 /* Take a bio reference on tg */
752 throtl_ref_get_tg(tg);
753 tg->nr_queued[rw]++;
754 td->nr_queued[rw]++;
755 throtl_enqueue_tg(td, tg);
756}
757
758static void tg_update_disptime(struct throtl_data *td, struct throtl_grp *tg)
759{
760 unsigned long read_wait = -1, write_wait = -1, min_wait = -1, disptime;
761 struct bio *bio;
762
763 if ((bio = bio_list_peek(&tg->bio_lists[READ])))
764 tg_may_dispatch(td, tg, bio, &read_wait);
765
766 if ((bio = bio_list_peek(&tg->bio_lists[WRITE])))
767 tg_may_dispatch(td, tg, bio, &write_wait);
768
769 min_wait = min(read_wait, write_wait);
770 disptime = jiffies + min_wait;
771
Vivek Goyale43473b2010-09-15 17:06:35 -0400772 /* Update dispatch time */
773 throtl_dequeue_tg(td, tg);
774 tg->disptime = disptime;
775 throtl_enqueue_tg(td, tg);
776}
777
778static void tg_dispatch_one_bio(struct throtl_data *td, struct throtl_grp *tg,
779 bool rw, struct bio_list *bl)
780{
781 struct bio *bio;
782
783 bio = bio_list_pop(&tg->bio_lists[rw]);
784 tg->nr_queued[rw]--;
785 /* Drop bio reference on tg */
786 throtl_put_tg(tg);
787
788 BUG_ON(td->nr_queued[rw] <= 0);
789 td->nr_queued[rw]--;
790
791 throtl_charge_bio(tg, bio);
792 bio_list_add(bl, bio);
793 bio->bi_rw |= REQ_THROTTLED;
794
795 throtl_trim_slice(td, tg, rw);
796}
797
798static int throtl_dispatch_tg(struct throtl_data *td, struct throtl_grp *tg,
799 struct bio_list *bl)
800{
801 unsigned int nr_reads = 0, nr_writes = 0;
802 unsigned int max_nr_reads = throtl_grp_quantum*3/4;
Vivek Goyalc2f68052010-11-15 19:32:42 +0100803 unsigned int max_nr_writes = throtl_grp_quantum - max_nr_reads;
Vivek Goyale43473b2010-09-15 17:06:35 -0400804 struct bio *bio;
805
806 /* Try to dispatch 75% READS and 25% WRITES */
807
808 while ((bio = bio_list_peek(&tg->bio_lists[READ]))
809 && tg_may_dispatch(td, tg, bio, NULL)) {
810
811 tg_dispatch_one_bio(td, tg, bio_data_dir(bio), bl);
812 nr_reads++;
813
814 if (nr_reads >= max_nr_reads)
815 break;
816 }
817
818 while ((bio = bio_list_peek(&tg->bio_lists[WRITE]))
819 && tg_may_dispatch(td, tg, bio, NULL)) {
820
821 tg_dispatch_one_bio(td, tg, bio_data_dir(bio), bl);
822 nr_writes++;
823
824 if (nr_writes >= max_nr_writes)
825 break;
826 }
827
828 return nr_reads + nr_writes;
829}
830
831static int throtl_select_dispatch(struct throtl_data *td, struct bio_list *bl)
832{
833 unsigned int nr_disp = 0;
834 struct throtl_grp *tg;
835 struct throtl_rb_root *st = &td->tg_service_tree;
836
837 while (1) {
838 tg = throtl_rb_first(st);
839
840 if (!tg)
841 break;
842
843 if (time_before(jiffies, tg->disptime))
844 break;
845
846 throtl_dequeue_tg(td, tg);
847
848 nr_disp += throtl_dispatch_tg(td, tg, bl);
849
850 if (tg->nr_queued[0] || tg->nr_queued[1]) {
851 tg_update_disptime(td, tg);
852 throtl_enqueue_tg(td, tg);
853 }
854
855 if (nr_disp >= throtl_quantum)
856 break;
857 }
858
859 return nr_disp;
860}
861
Vivek Goyalfe071432010-10-01 14:49:49 +0200862static void throtl_process_limit_change(struct throtl_data *td)
863{
864 struct throtl_grp *tg;
865 struct hlist_node *pos, *n;
866
Vivek Goyalde701c72011-03-07 21:09:32 +0100867 if (!td->limits_changed)
Vivek Goyalfe071432010-10-01 14:49:49 +0200868 return;
869
Vivek Goyalde701c72011-03-07 21:09:32 +0100870 xchg(&td->limits_changed, false);
Vivek Goyalfe071432010-10-01 14:49:49 +0200871
Vivek Goyalde701c72011-03-07 21:09:32 +0100872 throtl_log(td, "limits changed");
Vivek Goyalfe071432010-10-01 14:49:49 +0200873
Vivek Goyal04a6b512010-12-01 19:34:52 +0100874 hlist_for_each_entry_safe(tg, pos, n, &td->tg_list, tg_node) {
Vivek Goyalde701c72011-03-07 21:09:32 +0100875 if (!tg->limits_changed)
876 continue;
Vivek Goyalfe071432010-10-01 14:49:49 +0200877
Vivek Goyalde701c72011-03-07 21:09:32 +0100878 if (!xchg(&tg->limits_changed, false))
879 continue;
880
881 throtl_log_tg(td, tg, "limit change rbps=%llu wbps=%llu"
882 " riops=%u wiops=%u", tg->bps[READ], tg->bps[WRITE],
883 tg->iops[READ], tg->iops[WRITE]);
884
Vivek Goyal04521db2011-03-22 21:54:29 +0100885 /*
886 * Restart the slices for both READ and WRITES. It
887 * might happen that a group's limit are dropped
888 * suddenly and we don't want to account recently
889 * dispatched IO with new low rate
890 */
891 throtl_start_new_slice(td, tg, 0);
892 throtl_start_new_slice(td, tg, 1);
893
Vivek Goyalde701c72011-03-07 21:09:32 +0100894 if (throtl_tg_on_rr(tg))
895 tg_update_disptime(td, tg);
896 }
Vivek Goyalfe071432010-10-01 14:49:49 +0200897}
898
Vivek Goyale43473b2010-09-15 17:06:35 -0400899/* Dispatch throttled bios. Should be called without queue lock held. */
900static int throtl_dispatch(struct request_queue *q)
901{
902 struct throtl_data *td = q->td;
903 unsigned int nr_disp = 0;
904 struct bio_list bio_list_on_stack;
905 struct bio *bio;
Vivek Goyal69d60eb2011-03-09 08:27:37 +0100906 struct blk_plug plug;
Vivek Goyale43473b2010-09-15 17:06:35 -0400907
908 spin_lock_irq(q->queue_lock);
909
Vivek Goyalfe071432010-10-01 14:49:49 +0200910 throtl_process_limit_change(td);
911
Vivek Goyale43473b2010-09-15 17:06:35 -0400912 if (!total_nr_queued(td))
913 goto out;
914
915 bio_list_init(&bio_list_on_stack);
916
Joe Perchesd2f31a52011-06-13 20:19:27 +0200917 throtl_log(td, "dispatch nr_queued=%u read=%u write=%u",
Vivek Goyale43473b2010-09-15 17:06:35 -0400918 total_nr_queued(td), td->nr_queued[READ],
919 td->nr_queued[WRITE]);
920
921 nr_disp = throtl_select_dispatch(td, &bio_list_on_stack);
922
923 if (nr_disp)
924 throtl_log(td, "bios disp=%u", nr_disp);
925
926 throtl_schedule_next_dispatch(td);
927out:
928 spin_unlock_irq(q->queue_lock);
929
930 /*
931 * If we dispatched some requests, unplug the queue to make sure
932 * immediate dispatch
933 */
934 if (nr_disp) {
Vivek Goyal69d60eb2011-03-09 08:27:37 +0100935 blk_start_plug(&plug);
Vivek Goyale43473b2010-09-15 17:06:35 -0400936 while((bio = bio_list_pop(&bio_list_on_stack)))
937 generic_make_request(bio);
Vivek Goyal69d60eb2011-03-09 08:27:37 +0100938 blk_finish_plug(&plug);
Vivek Goyale43473b2010-09-15 17:06:35 -0400939 }
940 return nr_disp;
941}
942
943void blk_throtl_work(struct work_struct *work)
944{
945 struct throtl_data *td = container_of(work, struct throtl_data,
946 throtl_work.work);
947 struct request_queue *q = td->queue;
948
949 throtl_dispatch(q);
950}
951
952/* Call with queue lock held */
Vivek Goyal450adcb2011-03-01 13:40:54 -0500953static void
954throtl_schedule_delayed_work(struct throtl_data *td, unsigned long delay)
Vivek Goyale43473b2010-09-15 17:06:35 -0400955{
956
Vivek Goyale43473b2010-09-15 17:06:35 -0400957 struct delayed_work *dwork = &td->throtl_work;
958
Vivek Goyal04521db2011-03-22 21:54:29 +0100959 /* schedule work if limits changed even if no bio is queued */
Joe Perchesd2f31a52011-06-13 20:19:27 +0200960 if (total_nr_queued(td) || td->limits_changed) {
Vivek Goyale43473b2010-09-15 17:06:35 -0400961 /*
962 * We might have a work scheduled to be executed in future.
963 * Cancel that and schedule a new one.
964 */
965 __cancel_delayed_work(dwork);
Vivek Goyal450adcb2011-03-01 13:40:54 -0500966 queue_delayed_work(kthrotld_workqueue, dwork, delay);
Vivek Goyale43473b2010-09-15 17:06:35 -0400967 throtl_log(td, "schedule work. delay=%lu jiffies=%lu",
968 delay, jiffies);
969 }
970}
Vivek Goyale43473b2010-09-15 17:06:35 -0400971
972static void
973throtl_destroy_tg(struct throtl_data *td, struct throtl_grp *tg)
974{
975 /* Something wrong if we are trying to remove same group twice */
976 BUG_ON(hlist_unhashed(&tg->tg_node));
977
978 hlist_del_init(&tg->tg_node);
979
980 /*
981 * Put the reference taken at the time of creation so that when all
982 * queues are gone, group can be destroyed.
983 */
984 throtl_put_tg(tg);
985 td->nr_undestroyed_grps--;
986}
987
988static void throtl_release_tgs(struct throtl_data *td)
989{
990 struct hlist_node *pos, *n;
991 struct throtl_grp *tg;
992
993 hlist_for_each_entry_safe(tg, pos, n, &td->tg_list, tg_node) {
994 /*
995 * If cgroup removal path got to blk_group first and removed
996 * it from cgroup list, then it will take care of destroying
997 * cfqg also.
998 */
999 if (!blkiocg_del_blkio_group(&tg->blkg))
1000 throtl_destroy_tg(td, tg);
1001 }
1002}
1003
1004static void throtl_td_free(struct throtl_data *td)
1005{
1006 kfree(td);
1007}
1008
1009/*
1010 * Blk cgroup controller notification saying that blkio_group object is being
1011 * delinked as associated cgroup object is going away. That also means that
1012 * no new IO will come in this group. So get rid of this group as soon as
1013 * any pending IO in the group is finished.
1014 *
1015 * This function is called under rcu_read_lock(). key is the rcu protected
1016 * pointer. That means "key" is a valid throtl_data pointer as long as we are
1017 * rcu read lock.
1018 *
1019 * "key" was fetched from blkio_group under blkio_cgroup->lock. That means
1020 * it should not be NULL as even if queue was going away, cgroup deltion
1021 * path got to it first.
1022 */
1023void throtl_unlink_blkio_group(void *key, struct blkio_group *blkg)
1024{
1025 unsigned long flags;
1026 struct throtl_data *td = key;
1027
1028 spin_lock_irqsave(td->queue->queue_lock, flags);
1029 throtl_destroy_tg(td, tg_of_blkg(blkg));
1030 spin_unlock_irqrestore(td->queue->queue_lock, flags);
1031}
1032
Vivek Goyalde701c72011-03-07 21:09:32 +01001033static void throtl_update_blkio_group_common(struct throtl_data *td,
1034 struct throtl_grp *tg)
1035{
1036 xchg(&tg->limits_changed, true);
1037 xchg(&td->limits_changed, true);
1038 /* Schedule a work now to process the limit change */
1039 throtl_schedule_delayed_work(td, 0);
1040}
1041
Vivek Goyalfe071432010-10-01 14:49:49 +02001042/*
1043 * For all update functions, key should be a valid pointer because these
1044 * update functions are called under blkcg_lock, that means, blkg is
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001045 * valid and in turn key is valid. queue exit path can not race because
Vivek Goyalfe071432010-10-01 14:49:49 +02001046 * of blkcg_lock
1047 *
1048 * Can not take queue lock in update functions as queue lock under blkcg_lock
1049 * is not allowed. Under other paths we take blkcg_lock under queue_lock.
1050 */
1051static void throtl_update_blkio_group_read_bps(void *key,
1052 struct blkio_group *blkg, u64 read_bps)
Vivek Goyale43473b2010-09-15 17:06:35 -04001053{
Vivek Goyalfe071432010-10-01 14:49:49 +02001054 struct throtl_data *td = key;
Vivek Goyalde701c72011-03-07 21:09:32 +01001055 struct throtl_grp *tg = tg_of_blkg(blkg);
Vivek Goyalfe071432010-10-01 14:49:49 +02001056
Vivek Goyalde701c72011-03-07 21:09:32 +01001057 tg->bps[READ] = read_bps;
1058 throtl_update_blkio_group_common(td, tg);
Vivek Goyale43473b2010-09-15 17:06:35 -04001059}
1060
Vivek Goyalfe071432010-10-01 14:49:49 +02001061static void throtl_update_blkio_group_write_bps(void *key,
1062 struct blkio_group *blkg, u64 write_bps)
Vivek Goyale43473b2010-09-15 17:06:35 -04001063{
Vivek Goyalfe071432010-10-01 14:49:49 +02001064 struct throtl_data *td = key;
Vivek Goyalde701c72011-03-07 21:09:32 +01001065 struct throtl_grp *tg = tg_of_blkg(blkg);
Vivek Goyalfe071432010-10-01 14:49:49 +02001066
Vivek Goyalde701c72011-03-07 21:09:32 +01001067 tg->bps[WRITE] = write_bps;
1068 throtl_update_blkio_group_common(td, tg);
Vivek Goyale43473b2010-09-15 17:06:35 -04001069}
1070
Vivek Goyalfe071432010-10-01 14:49:49 +02001071static void throtl_update_blkio_group_read_iops(void *key,
1072 struct blkio_group *blkg, unsigned int read_iops)
Vivek Goyal8e89d132010-09-15 17:06:37 -04001073{
Vivek Goyalfe071432010-10-01 14:49:49 +02001074 struct throtl_data *td = key;
Vivek Goyalde701c72011-03-07 21:09:32 +01001075 struct throtl_grp *tg = tg_of_blkg(blkg);
Vivek Goyalfe071432010-10-01 14:49:49 +02001076
Vivek Goyalde701c72011-03-07 21:09:32 +01001077 tg->iops[READ] = read_iops;
1078 throtl_update_blkio_group_common(td, tg);
Vivek Goyal8e89d132010-09-15 17:06:37 -04001079}
1080
Vivek Goyalfe071432010-10-01 14:49:49 +02001081static void throtl_update_blkio_group_write_iops(void *key,
1082 struct blkio_group *blkg, unsigned int write_iops)
Vivek Goyal8e89d132010-09-15 17:06:37 -04001083{
Vivek Goyalfe071432010-10-01 14:49:49 +02001084 struct throtl_data *td = key;
Vivek Goyalde701c72011-03-07 21:09:32 +01001085 struct throtl_grp *tg = tg_of_blkg(blkg);
Vivek Goyalfe071432010-10-01 14:49:49 +02001086
Vivek Goyalde701c72011-03-07 21:09:32 +01001087 tg->iops[WRITE] = write_iops;
1088 throtl_update_blkio_group_common(td, tg);
Vivek Goyal8e89d132010-09-15 17:06:37 -04001089}
1090
Vivek Goyalda527772011-03-02 19:05:33 -05001091static void throtl_shutdown_wq(struct request_queue *q)
Vivek Goyale43473b2010-09-15 17:06:35 -04001092{
1093 struct throtl_data *td = q->td;
1094
1095 cancel_delayed_work_sync(&td->throtl_work);
1096}
1097
1098static struct blkio_policy_type blkio_policy_throtl = {
1099 .ops = {
1100 .blkio_unlink_group_fn = throtl_unlink_blkio_group,
1101 .blkio_update_group_read_bps_fn =
1102 throtl_update_blkio_group_read_bps,
1103 .blkio_update_group_write_bps_fn =
1104 throtl_update_blkio_group_write_bps,
Vivek Goyal8e89d132010-09-15 17:06:37 -04001105 .blkio_update_group_read_iops_fn =
1106 throtl_update_blkio_group_read_iops,
1107 .blkio_update_group_write_iops_fn =
1108 throtl_update_blkio_group_write_iops,
Vivek Goyale43473b2010-09-15 17:06:35 -04001109 },
Vivek Goyal8e89d132010-09-15 17:06:37 -04001110 .plid = BLKIO_POLICY_THROTL,
Vivek Goyale43473b2010-09-15 17:06:35 -04001111};
1112
Tejun Heobc16a4f2011-10-19 14:33:01 +02001113bool blk_throtl_bio(struct request_queue *q, struct bio *bio)
Vivek Goyale43473b2010-09-15 17:06:35 -04001114{
1115 struct throtl_data *td = q->td;
1116 struct throtl_grp *tg;
Vivek Goyale43473b2010-09-15 17:06:35 -04001117 bool rw = bio_data_dir(bio), update_disptime = true;
Vivek Goyalaf75cd32011-05-19 15:38:31 -04001118 struct blkio_cgroup *blkcg;
Tejun Heobc16a4f2011-10-19 14:33:01 +02001119 bool throttled = false;
Vivek Goyale43473b2010-09-15 17:06:35 -04001120
1121 if (bio->bi_rw & REQ_THROTTLED) {
1122 bio->bi_rw &= ~REQ_THROTTLED;
Tejun Heobc16a4f2011-10-19 14:33:01 +02001123 goto out;
Vivek Goyale43473b2010-09-15 17:06:35 -04001124 }
1125
Vivek Goyalaf75cd32011-05-19 15:38:31 -04001126 /*
1127 * A throtl_grp pointer retrieved under rcu can be used to access
1128 * basic fields like stats and io rates. If a group has no rules,
1129 * just update the dispatch stats in lockless manner and return.
1130 */
1131
1132 rcu_read_lock();
1133 blkcg = task_blkio_cgroup(current);
1134 tg = throtl_find_tg(td, blkcg);
1135 if (tg) {
1136 throtl_tg_fill_dev_details(td, tg);
1137
1138 if (tg_no_rule_group(tg, rw)) {
1139 blkiocg_update_dispatch_stats(&tg->blkg, bio->bi_size,
Shaohua Lie5a94f52011-08-01 10:31:06 +02001140 rw, rw_is_sync(bio->bi_rw));
Vivek Goyalaf75cd32011-05-19 15:38:31 -04001141 rcu_read_unlock();
Tejun Heobc16a4f2011-10-19 14:33:01 +02001142 goto out;
Vivek Goyalaf75cd32011-05-19 15:38:31 -04001143 }
1144 }
1145 rcu_read_unlock();
1146
1147 /*
1148 * Either group has not been allocated yet or it is not an unlimited
1149 * IO group
1150 */
Vivek Goyale43473b2010-09-15 17:06:35 -04001151 spin_lock_irq(q->queue_lock);
1152 tg = throtl_get_tg(td);
Tejun Heobc16a4f2011-10-19 14:33:01 +02001153 if (unlikely(!tg))
1154 goto out_unlock;
Vivek Goyalf469a7b2011-05-19 15:38:23 -04001155
Vivek Goyale43473b2010-09-15 17:06:35 -04001156 if (tg->nr_queued[rw]) {
1157 /*
1158 * There is already another bio queued in same dir. No
1159 * need to update dispatch time.
1160 */
Vivek Goyal231d7042011-03-07 21:05:14 +01001161 update_disptime = false;
Vivek Goyale43473b2010-09-15 17:06:35 -04001162 goto queue_bio;
Vivek Goyalde701c72011-03-07 21:09:32 +01001163
Vivek Goyale43473b2010-09-15 17:06:35 -04001164 }
1165
1166 /* Bio is with-in rate limit of group */
1167 if (tg_may_dispatch(td, tg, bio, NULL)) {
1168 throtl_charge_bio(tg, bio);
Vivek Goyal04521db2011-03-22 21:54:29 +01001169
1170 /*
1171 * We need to trim slice even when bios are not being queued
1172 * otherwise it might happen that a bio is not queued for
1173 * a long time and slice keeps on extending and trim is not
1174 * called for a long time. Now if limits are reduced suddenly
1175 * we take into account all the IO dispatched so far at new
1176 * low rate and * newly queued IO gets a really long dispatch
1177 * time.
1178 *
1179 * So keep on trimming slice even if bio is not queued.
1180 */
1181 throtl_trim_slice(td, tg, rw);
Tejun Heobc16a4f2011-10-19 14:33:01 +02001182 goto out_unlock;
Vivek Goyale43473b2010-09-15 17:06:35 -04001183 }
1184
1185queue_bio:
Joe Perchesfd16d262011-06-13 10:42:49 +02001186 throtl_log_tg(td, tg, "[%c] bio. bdisp=%llu sz=%u bps=%llu"
Vivek Goyal8e89d132010-09-15 17:06:37 -04001187 " iodisp=%u iops=%u queued=%d/%d",
1188 rw == READ ? 'R' : 'W',
Vivek Goyale43473b2010-09-15 17:06:35 -04001189 tg->bytes_disp[rw], bio->bi_size, tg->bps[rw],
Vivek Goyal8e89d132010-09-15 17:06:37 -04001190 tg->io_disp[rw], tg->iops[rw],
Vivek Goyale43473b2010-09-15 17:06:35 -04001191 tg->nr_queued[READ], tg->nr_queued[WRITE]);
1192
1193 throtl_add_bio_tg(q->td, tg, bio);
Tejun Heobc16a4f2011-10-19 14:33:01 +02001194 throttled = true;
Vivek Goyale43473b2010-09-15 17:06:35 -04001195
1196 if (update_disptime) {
1197 tg_update_disptime(td, tg);
1198 throtl_schedule_next_dispatch(td);
1199 }
1200
Tejun Heobc16a4f2011-10-19 14:33:01 +02001201out_unlock:
Vivek Goyale43473b2010-09-15 17:06:35 -04001202 spin_unlock_irq(q->queue_lock);
Tejun Heobc16a4f2011-10-19 14:33:01 +02001203out:
1204 return throttled;
Vivek Goyale43473b2010-09-15 17:06:35 -04001205}
1206
1207int blk_throtl_init(struct request_queue *q)
1208{
1209 struct throtl_data *td;
1210 struct throtl_grp *tg;
1211
1212 td = kzalloc_node(sizeof(*td), GFP_KERNEL, q->node);
1213 if (!td)
1214 return -ENOMEM;
1215
1216 INIT_HLIST_HEAD(&td->tg_list);
1217 td->tg_service_tree = THROTL_RB_ROOT;
Vivek Goyalde701c72011-03-07 21:09:32 +01001218 td->limits_changed = false;
Vivek Goyala29a1712011-05-19 15:38:19 -04001219 INIT_DELAYED_WORK(&td->throtl_work, blk_throtl_work);
Vivek Goyale43473b2010-09-15 17:06:35 -04001220
Vivek Goyal29b12582011-05-19 15:38:24 -04001221 /* alloc and Init root group. */
1222 td->queue = q;
1223 tg = throtl_alloc_tg(td);
Vivek Goyal02977e42010-10-01 14:49:48 +02001224
Vivek Goyal29b12582011-05-19 15:38:24 -04001225 if (!tg) {
1226 kfree(td);
1227 return -ENOMEM;
1228 }
1229
1230 td->root_tg = tg;
Vivek Goyale43473b2010-09-15 17:06:35 -04001231
1232 rcu_read_lock();
Vivek Goyal5617cbe2011-05-19 15:38:26 -04001233 throtl_init_add_tg_lists(td, tg, &blkio_root_cgroup);
Vivek Goyale43473b2010-09-15 17:06:35 -04001234 rcu_read_unlock();
1235
1236 /* Attach throtl data to request queue */
Vivek Goyale43473b2010-09-15 17:06:35 -04001237 q->td = td;
1238 return 0;
1239}
1240
1241void blk_throtl_exit(struct request_queue *q)
1242{
1243 struct throtl_data *td = q->td;
1244 bool wait = false;
1245
1246 BUG_ON(!td);
1247
Vivek Goyalda527772011-03-02 19:05:33 -05001248 throtl_shutdown_wq(q);
Vivek Goyale43473b2010-09-15 17:06:35 -04001249
1250 spin_lock_irq(q->queue_lock);
1251 throtl_release_tgs(td);
Vivek Goyale43473b2010-09-15 17:06:35 -04001252
1253 /* If there are other groups */
Vivek Goyal02977e42010-10-01 14:49:48 +02001254 if (td->nr_undestroyed_grps > 0)
Vivek Goyale43473b2010-09-15 17:06:35 -04001255 wait = true;
1256
1257 spin_unlock_irq(q->queue_lock);
1258
1259 /*
1260 * Wait for tg->blkg->key accessors to exit their grace periods.
1261 * Do this wait only if there are other undestroyed groups out
1262 * there (other than root group). This can happen if cgroup deletion
1263 * path claimed the responsibility of cleaning up a group before
1264 * queue cleanup code get to the group.
1265 *
1266 * Do not call synchronize_rcu() unconditionally as there are drivers
1267 * which create/delete request queue hundreds of times during scan/boot
1268 * and synchronize_rcu() can take significant time and slow down boot.
1269 */
1270 if (wait)
1271 synchronize_rcu();
Vivek Goyalfe071432010-10-01 14:49:49 +02001272
1273 /*
1274 * Just being safe to make sure after previous flush if some body did
1275 * update limits through cgroup and another work got queued, cancel
1276 * it.
1277 */
Vivek Goyalda527772011-03-02 19:05:33 -05001278 throtl_shutdown_wq(q);
Vivek Goyale43473b2010-09-15 17:06:35 -04001279 throtl_td_free(td);
1280}
1281
1282static int __init throtl_init(void)
1283{
Vivek Goyal450adcb2011-03-01 13:40:54 -05001284 kthrotld_workqueue = alloc_workqueue("kthrotld", WQ_MEM_RECLAIM, 0);
1285 if (!kthrotld_workqueue)
1286 panic("Failed to create kthrotld\n");
1287
Vivek Goyale43473b2010-09-15 17:06:35 -04001288 blkio_policy_register(&blkio_policy_throtl);
1289 return 0;
1290}
1291
1292module_init(throtl_init);