blob: 5d647edc02a1e5fae50c607d9e4623c528f19e56 [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
Tejun Heo03814112012-03-05 13:15:14 -080024static struct blkio_policy_type blkio_policy_throtl;
25
Vivek Goyal450adcb2011-03-01 13:40:54 -050026/* A workqueue to queue throttle related work */
27static struct workqueue_struct *kthrotld_workqueue;
28static void throtl_schedule_delayed_work(struct throtl_data *td,
29 unsigned long delay);
30
Vivek Goyale43473b2010-09-15 17:06:35 -040031struct throtl_rb_root {
32 struct rb_root rb;
33 struct rb_node *left;
34 unsigned int count;
35 unsigned long min_disptime;
36};
37
38#define THROTL_RB_ROOT (struct throtl_rb_root) { .rb = RB_ROOT, .left = NULL, \
39 .count = 0, .min_disptime = 0}
40
41#define rb_entry_tg(node) rb_entry((node), struct throtl_grp, rb_node)
42
43struct throtl_grp {
Vivek Goyale43473b2010-09-15 17:06:35 -040044 /* 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
Vivek Goyale43473b2010-09-15 17:06:35 -040054 unsigned int flags;
55
56 /* Two lists for READ and WRITE */
57 struct bio_list bio_lists[2];
58
59 /* Number of queued bios on READ and WRITE lists */
60 unsigned int nr_queued[2];
61
62 /* bytes per second rate limits */
63 uint64_t bps[2];
64
Vivek Goyal8e89d132010-09-15 17:06:37 -040065 /* IOPS limits */
66 unsigned int iops[2];
67
Vivek Goyale43473b2010-09-15 17:06:35 -040068 /* Number of bytes disptached in current slice */
69 uint64_t bytes_disp[2];
Vivek Goyal8e89d132010-09-15 17:06:37 -040070 /* Number of bio's dispatched in current slice */
71 unsigned int io_disp[2];
Vivek Goyale43473b2010-09-15 17:06:35 -040072
73 /* When did we start a new slice */
74 unsigned long slice_start[2];
75 unsigned long slice_end[2];
Vivek Goyalfe071432010-10-01 14:49:49 +020076
77 /* Some throttle limits got updated for the group */
Andreas Schwab6f037932011-03-30 12:21:56 +020078 int limits_changed;
Vivek Goyale43473b2010-09-15 17:06:35 -040079};
80
81struct throtl_data
82{
Vivek Goyale43473b2010-09-15 17:06:35 -040083 /* service tree for active throtl groups */
84 struct throtl_rb_root tg_service_tree;
85
Vivek Goyal29b12582011-05-19 15:38:24 -040086 struct throtl_grp *root_tg;
Vivek Goyale43473b2010-09-15 17:06:35 -040087 struct request_queue *queue;
88
89 /* Total Number of queued bios on READ and WRITE lists */
90 unsigned int nr_queued[2];
91
92 /*
Vivek Goyal02977e42010-10-01 14:49:48 +020093 * number of total undestroyed groups
Vivek Goyale43473b2010-09-15 17:06:35 -040094 */
95 unsigned int nr_undestroyed_grps;
96
97 /* Work for dispatching throttled bios */
98 struct delayed_work throtl_work;
Vivek Goyalfe071432010-10-01 14:49:49 +020099
Andreas Schwab6f037932011-03-30 12:21:56 +0200100 int limits_changed;
Vivek Goyale43473b2010-09-15 17:06:35 -0400101};
102
Tejun Heo03814112012-03-05 13:15:14 -0800103static inline struct throtl_grp *blkg_to_tg(struct blkio_group *blkg)
104{
105 return blkg_to_pdata(blkg, &blkio_policy_throtl);
106}
107
108static inline struct blkio_group *tg_to_blkg(struct throtl_grp *tg)
109{
Tejun Heoaaec55a2012-04-01 14:38:42 -0700110 return pdata_to_blkg(tg);
Tejun Heo03814112012-03-05 13:15:14 -0800111}
112
Vivek Goyale43473b2010-09-15 17:06:35 -0400113enum tg_state_flags {
114 THROTL_TG_FLAG_on_rr = 0, /* on round-robin busy list */
115};
116
117#define THROTL_TG_FNS(name) \
118static inline void throtl_mark_tg_##name(struct throtl_grp *tg) \
119{ \
120 (tg)->flags |= (1 << THROTL_TG_FLAG_##name); \
121} \
122static inline void throtl_clear_tg_##name(struct throtl_grp *tg) \
123{ \
124 (tg)->flags &= ~(1 << THROTL_TG_FLAG_##name); \
125} \
126static inline int throtl_tg_##name(const struct throtl_grp *tg) \
127{ \
128 return ((tg)->flags & (1 << THROTL_TG_FLAG_##name)) != 0; \
129}
130
131THROTL_TG_FNS(on_rr);
132
133#define throtl_log_tg(td, tg, fmt, args...) \
134 blk_add_trace_msg((td)->queue, "throtl %s " fmt, \
Tejun Heo03814112012-03-05 13:15:14 -0800135 blkg_path(tg_to_blkg(tg)), ##args); \
Vivek Goyale43473b2010-09-15 17:06:35 -0400136
137#define throtl_log(td, fmt, args...) \
138 blk_add_trace_msg((td)->queue, "throtl " fmt, ##args)
139
Joe Perchesd2f31a52011-06-13 20:19:27 +0200140static inline unsigned int total_nr_queued(struct throtl_data *td)
Vivek Goyale43473b2010-09-15 17:06:35 -0400141{
Joe Perchesd2f31a52011-06-13 20:19:27 +0200142 return td->nr_queued[0] + td->nr_queued[1];
Vivek Goyale43473b2010-09-15 17:06:35 -0400143}
144
Tejun Heo03814112012-03-05 13:15:14 -0800145static void throtl_init_blkio_group(struct blkio_group *blkg)
Vivek Goyala29a1712011-05-19 15:38:19 -0400146{
Tejun Heo03814112012-03-05 13:15:14 -0800147 struct throtl_grp *tg = blkg_to_tg(blkg);
Tejun Heocd1604f2012-03-05 13:15:06 -0800148
Vivek Goyala29a1712011-05-19 15:38:19 -0400149 RB_CLEAR_NODE(&tg->rb_node);
150 bio_list_init(&tg->bio_lists[0]);
151 bio_list_init(&tg->bio_lists[1]);
152 tg->limits_changed = false;
153
Tejun Heoe56da7e2012-03-05 13:15:07 -0800154 tg->bps[READ] = -1;
155 tg->bps[WRITE] = -1;
156 tg->iops[READ] = -1;
157 tg->iops[WRITE] = -1;
Vivek Goyala29a1712011-05-19 15:38:19 -0400158}
159
Vivek Goyalf469a7b2011-05-19 15:38:23 -0400160static struct
Tejun Heocd1604f2012-03-05 13:15:06 -0800161throtl_grp *throtl_lookup_tg(struct throtl_data *td, struct blkio_cgroup *blkcg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400162{
Vivek Goyale43473b2010-09-15 17:06:35 -0400163 /*
Vivek Goyalbe2c6b12011-01-19 08:25:02 -0700164 * This is the common case when there are no blkio cgroups.
Tejun Heocd1604f2012-03-05 13:15:06 -0800165 * Avoid lookup in this case
166 */
Vivek Goyalbe2c6b12011-01-19 08:25:02 -0700167 if (blkcg == &blkio_root_cgroup)
Tejun Heo7a4dd282012-03-05 13:15:09 -0800168 return td->root_tg;
Vivek Goyale43473b2010-09-15 17:06:35 -0400169
Tejun Heoe8989fa2012-03-05 13:15:20 -0800170 return blkg_to_tg(blkg_lookup(blkcg, td->queue));
Vivek Goyale43473b2010-09-15 17:06:35 -0400171}
172
Tejun Heocd1604f2012-03-05 13:15:06 -0800173static struct throtl_grp *throtl_lookup_create_tg(struct throtl_data *td,
174 struct blkio_cgroup *blkcg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400175{
Vivek Goyalf469a7b2011-05-19 15:38:23 -0400176 struct request_queue *q = td->queue;
Tejun Heocd1604f2012-03-05 13:15:06 -0800177 struct throtl_grp *tg = NULL;
Tejun Heo0a5a7d02012-03-05 13:15:02 -0800178
Vivek Goyalf469a7b2011-05-19 15:38:23 -0400179 /*
Tejun Heocd1604f2012-03-05 13:15:06 -0800180 * This is the common case when there are no blkio cgroups.
181 * Avoid lookup in this case
Vivek Goyalf469a7b2011-05-19 15:38:23 -0400182 */
Tejun Heocd1604f2012-03-05 13:15:06 -0800183 if (blkcg == &blkio_root_cgroup) {
Vivek Goyal29b12582011-05-19 15:38:24 -0400184 tg = td->root_tg;
Tejun Heocd1604f2012-03-05 13:15:06 -0800185 } else {
186 struct blkio_group *blkg;
187
Tejun Heoaaec55a2012-04-01 14:38:42 -0700188 blkg = blkg_lookup_create(blkcg, q, false);
Tejun Heocd1604f2012-03-05 13:15:06 -0800189
190 /* if %NULL and @q is alive, fall back to root_tg */
191 if (!IS_ERR(blkg))
Tejun Heo03814112012-03-05 13:15:14 -0800192 tg = blkg_to_tg(blkg);
Tejun Heocd1604f2012-03-05 13:15:06 -0800193 else if (!blk_queue_dead(q))
194 tg = td->root_tg;
Vivek Goyalf469a7b2011-05-19 15:38:23 -0400195 }
196
Vivek Goyale43473b2010-09-15 17:06:35 -0400197 return tg;
198}
199
200static struct throtl_grp *throtl_rb_first(struct throtl_rb_root *root)
201{
202 /* Service tree is empty */
203 if (!root->count)
204 return NULL;
205
206 if (!root->left)
207 root->left = rb_first(&root->rb);
208
209 if (root->left)
210 return rb_entry_tg(root->left);
211
212 return NULL;
213}
214
215static void rb_erase_init(struct rb_node *n, struct rb_root *root)
216{
217 rb_erase(n, root);
218 RB_CLEAR_NODE(n);
219}
220
221static void throtl_rb_erase(struct rb_node *n, struct throtl_rb_root *root)
222{
223 if (root->left == n)
224 root->left = NULL;
225 rb_erase_init(n, &root->rb);
226 --root->count;
227}
228
229static void update_min_dispatch_time(struct throtl_rb_root *st)
230{
231 struct throtl_grp *tg;
232
233 tg = throtl_rb_first(st);
234 if (!tg)
235 return;
236
237 st->min_disptime = tg->disptime;
238}
239
240static void
241tg_service_tree_add(struct throtl_rb_root *st, struct throtl_grp *tg)
242{
243 struct rb_node **node = &st->rb.rb_node;
244 struct rb_node *parent = NULL;
245 struct throtl_grp *__tg;
246 unsigned long key = tg->disptime;
247 int left = 1;
248
249 while (*node != NULL) {
250 parent = *node;
251 __tg = rb_entry_tg(parent);
252
253 if (time_before(key, __tg->disptime))
254 node = &parent->rb_left;
255 else {
256 node = &parent->rb_right;
257 left = 0;
258 }
259 }
260
261 if (left)
262 st->left = &tg->rb_node;
263
264 rb_link_node(&tg->rb_node, parent, node);
265 rb_insert_color(&tg->rb_node, &st->rb);
266}
267
268static void __throtl_enqueue_tg(struct throtl_data *td, struct throtl_grp *tg)
269{
270 struct throtl_rb_root *st = &td->tg_service_tree;
271
272 tg_service_tree_add(st, tg);
273 throtl_mark_tg_on_rr(tg);
274 st->count++;
275}
276
277static void throtl_enqueue_tg(struct throtl_data *td, struct throtl_grp *tg)
278{
279 if (!throtl_tg_on_rr(tg))
280 __throtl_enqueue_tg(td, tg);
281}
282
283static void __throtl_dequeue_tg(struct throtl_data *td, struct throtl_grp *tg)
284{
285 throtl_rb_erase(&tg->rb_node, &td->tg_service_tree);
286 throtl_clear_tg_on_rr(tg);
287}
288
289static void throtl_dequeue_tg(struct throtl_data *td, struct throtl_grp *tg)
290{
291 if (throtl_tg_on_rr(tg))
292 __throtl_dequeue_tg(td, tg);
293}
294
295static void throtl_schedule_next_dispatch(struct throtl_data *td)
296{
297 struct throtl_rb_root *st = &td->tg_service_tree;
298
299 /*
300 * If there are more bios pending, schedule more work.
301 */
302 if (!total_nr_queued(td))
303 return;
304
305 BUG_ON(!st->count);
306
307 update_min_dispatch_time(st);
308
309 if (time_before_eq(st->min_disptime, jiffies))
Vivek Goyal450adcb2011-03-01 13:40:54 -0500310 throtl_schedule_delayed_work(td, 0);
Vivek Goyale43473b2010-09-15 17:06:35 -0400311 else
Vivek Goyal450adcb2011-03-01 13:40:54 -0500312 throtl_schedule_delayed_work(td, (st->min_disptime - jiffies));
Vivek Goyale43473b2010-09-15 17:06:35 -0400313}
314
315static inline void
316throtl_start_new_slice(struct throtl_data *td, struct throtl_grp *tg, bool rw)
317{
318 tg->bytes_disp[rw] = 0;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400319 tg->io_disp[rw] = 0;
Vivek Goyale43473b2010-09-15 17:06:35 -0400320 tg->slice_start[rw] = jiffies;
321 tg->slice_end[rw] = jiffies + throtl_slice;
322 throtl_log_tg(td, tg, "[%c] new slice start=%lu end=%lu jiffies=%lu",
323 rw == READ ? 'R' : 'W', tg->slice_start[rw],
324 tg->slice_end[rw], jiffies);
325}
326
Vivek Goyald1ae8ff2010-12-01 19:34:46 +0100327static inline void throtl_set_slice_end(struct throtl_data *td,
328 struct throtl_grp *tg, bool rw, unsigned long jiffy_end)
329{
330 tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
331}
332
Vivek Goyale43473b2010-09-15 17:06:35 -0400333static inline void throtl_extend_slice(struct throtl_data *td,
334 struct throtl_grp *tg, bool rw, unsigned long jiffy_end)
335{
336 tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
337 throtl_log_tg(td, tg, "[%c] extend slice start=%lu end=%lu jiffies=%lu",
338 rw == READ ? 'R' : 'W', tg->slice_start[rw],
339 tg->slice_end[rw], jiffies);
340}
341
342/* Determine if previously allocated or extended slice is complete or not */
343static bool
344throtl_slice_used(struct throtl_data *td, struct throtl_grp *tg, bool rw)
345{
346 if (time_in_range(jiffies, tg->slice_start[rw], tg->slice_end[rw]))
347 return 0;
348
349 return 1;
350}
351
352/* Trim the used slices and adjust slice start accordingly */
353static inline void
354throtl_trim_slice(struct throtl_data *td, struct throtl_grp *tg, bool rw)
355{
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200356 unsigned long nr_slices, time_elapsed, io_trim;
357 u64 bytes_trim, tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400358
359 BUG_ON(time_before(tg->slice_end[rw], tg->slice_start[rw]));
360
361 /*
362 * If bps are unlimited (-1), then time slice don't get
363 * renewed. Don't try to trim the slice if slice is used. A new
364 * slice will start when appropriate.
365 */
366 if (throtl_slice_used(td, tg, rw))
367 return;
368
Vivek Goyald1ae8ff2010-12-01 19:34:46 +0100369 /*
370 * A bio has been dispatched. Also adjust slice_end. It might happen
371 * that initially cgroup limit was very low resulting in high
372 * slice_end, but later limit was bumped up and bio was dispached
373 * sooner, then we need to reduce slice_end. A high bogus slice_end
374 * is bad because it does not allow new slice to start.
375 */
376
377 throtl_set_slice_end(td, tg, rw, jiffies + throtl_slice);
378
Vivek Goyale43473b2010-09-15 17:06:35 -0400379 time_elapsed = jiffies - tg->slice_start[rw];
380
381 nr_slices = time_elapsed / throtl_slice;
382
383 if (!nr_slices)
384 return;
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200385 tmp = tg->bps[rw] * throtl_slice * nr_slices;
386 do_div(tmp, HZ);
387 bytes_trim = tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400388
Vivek Goyal8e89d132010-09-15 17:06:37 -0400389 io_trim = (tg->iops[rw] * throtl_slice * nr_slices)/HZ;
Vivek Goyale43473b2010-09-15 17:06:35 -0400390
Vivek Goyal8e89d132010-09-15 17:06:37 -0400391 if (!bytes_trim && !io_trim)
Vivek Goyale43473b2010-09-15 17:06:35 -0400392 return;
393
394 if (tg->bytes_disp[rw] >= bytes_trim)
395 tg->bytes_disp[rw] -= bytes_trim;
396 else
397 tg->bytes_disp[rw] = 0;
398
Vivek Goyal8e89d132010-09-15 17:06:37 -0400399 if (tg->io_disp[rw] >= io_trim)
400 tg->io_disp[rw] -= io_trim;
401 else
402 tg->io_disp[rw] = 0;
403
Vivek Goyale43473b2010-09-15 17:06:35 -0400404 tg->slice_start[rw] += nr_slices * throtl_slice;
405
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200406 throtl_log_tg(td, tg, "[%c] trim slice nr=%lu bytes=%llu io=%lu"
Vivek Goyale43473b2010-09-15 17:06:35 -0400407 " start=%lu end=%lu jiffies=%lu",
Vivek Goyal8e89d132010-09-15 17:06:37 -0400408 rw == READ ? 'R' : 'W', nr_slices, bytes_trim, io_trim,
Vivek Goyale43473b2010-09-15 17:06:35 -0400409 tg->slice_start[rw], tg->slice_end[rw], jiffies);
410}
411
Vivek Goyal8e89d132010-09-15 17:06:37 -0400412static bool tg_with_in_iops_limit(struct throtl_data *td, struct throtl_grp *tg,
413 struct bio *bio, unsigned long *wait)
Vivek Goyale43473b2010-09-15 17:06:35 -0400414{
415 bool rw = bio_data_dir(bio);
Vivek Goyal8e89d132010-09-15 17:06:37 -0400416 unsigned int io_allowed;
Vivek Goyale43473b2010-09-15 17:06:35 -0400417 unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
Vivek Goyalc49c06e2010-10-01 21:16:42 +0200418 u64 tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400419
Vivek Goyal8e89d132010-09-15 17:06:37 -0400420 jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
Vivek Goyale43473b2010-09-15 17:06:35 -0400421
Vivek Goyal8e89d132010-09-15 17:06:37 -0400422 /* Slice has just started. Consider one slice interval */
423 if (!jiffy_elapsed)
424 jiffy_elapsed_rnd = throtl_slice;
425
426 jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
427
Vivek Goyalc49c06e2010-10-01 21:16:42 +0200428 /*
429 * jiffy_elapsed_rnd should not be a big value as minimum iops can be
430 * 1 then at max jiffy elapsed should be equivalent of 1 second as we
431 * will allow dispatch after 1 second and after that slice should
432 * have been trimmed.
433 */
434
435 tmp = (u64)tg->iops[rw] * jiffy_elapsed_rnd;
436 do_div(tmp, HZ);
437
438 if (tmp > UINT_MAX)
439 io_allowed = UINT_MAX;
440 else
441 io_allowed = tmp;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400442
443 if (tg->io_disp[rw] + 1 <= io_allowed) {
Vivek Goyale43473b2010-09-15 17:06:35 -0400444 if (wait)
445 *wait = 0;
446 return 1;
447 }
448
Vivek Goyal8e89d132010-09-15 17:06:37 -0400449 /* Calc approx time to dispatch */
450 jiffy_wait = ((tg->io_disp[rw] + 1) * HZ)/tg->iops[rw] + 1;
451
452 if (jiffy_wait > jiffy_elapsed)
453 jiffy_wait = jiffy_wait - jiffy_elapsed;
454 else
455 jiffy_wait = 1;
456
457 if (wait)
458 *wait = jiffy_wait;
459 return 0;
460}
461
462static bool tg_with_in_bps_limit(struct throtl_data *td, struct throtl_grp *tg,
463 struct bio *bio, unsigned long *wait)
464{
465 bool rw = bio_data_dir(bio);
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200466 u64 bytes_allowed, extra_bytes, tmp;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400467 unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
Vivek Goyale43473b2010-09-15 17:06:35 -0400468
469 jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
470
471 /* Slice has just started. Consider one slice interval */
472 if (!jiffy_elapsed)
473 jiffy_elapsed_rnd = throtl_slice;
474
475 jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
476
Vivek Goyal5e901a22010-10-01 21:16:38 +0200477 tmp = tg->bps[rw] * jiffy_elapsed_rnd;
478 do_div(tmp, HZ);
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200479 bytes_allowed = tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400480
481 if (tg->bytes_disp[rw] + bio->bi_size <= bytes_allowed) {
482 if (wait)
483 *wait = 0;
484 return 1;
485 }
486
487 /* Calc approx time to dispatch */
488 extra_bytes = tg->bytes_disp[rw] + bio->bi_size - bytes_allowed;
489 jiffy_wait = div64_u64(extra_bytes * HZ, tg->bps[rw]);
490
491 if (!jiffy_wait)
492 jiffy_wait = 1;
493
494 /*
495 * This wait time is without taking into consideration the rounding
496 * up we did. Add that time also.
497 */
498 jiffy_wait = jiffy_wait + (jiffy_elapsed_rnd - jiffy_elapsed);
Vivek Goyale43473b2010-09-15 17:06:35 -0400499 if (wait)
500 *wait = jiffy_wait;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400501 return 0;
502}
Vivek Goyale43473b2010-09-15 17:06:35 -0400503
Vivek Goyalaf75cd32011-05-19 15:38:31 -0400504static bool tg_no_rule_group(struct throtl_grp *tg, bool rw) {
505 if (tg->bps[rw] == -1 && tg->iops[rw] == -1)
506 return 1;
507 return 0;
508}
509
Vivek Goyal8e89d132010-09-15 17:06:37 -0400510/*
511 * Returns whether one can dispatch a bio or not. Also returns approx number
512 * of jiffies to wait before this bio is with-in IO rate and can be dispatched
513 */
514static bool tg_may_dispatch(struct throtl_data *td, struct throtl_grp *tg,
515 struct bio *bio, unsigned long *wait)
516{
517 bool rw = bio_data_dir(bio);
518 unsigned long bps_wait = 0, iops_wait = 0, max_wait = 0;
519
520 /*
521 * Currently whole state machine of group depends on first bio
522 * queued in the group bio list. So one should not be calling
523 * this function with a different bio if there are other bios
524 * queued.
525 */
526 BUG_ON(tg->nr_queued[rw] && bio != bio_list_peek(&tg->bio_lists[rw]));
527
528 /* If tg->bps = -1, then BW is unlimited */
529 if (tg->bps[rw] == -1 && tg->iops[rw] == -1) {
530 if (wait)
531 *wait = 0;
532 return 1;
533 }
534
535 /*
536 * If previous slice expired, start a new one otherwise renew/extend
537 * existing slice to make sure it is at least throtl_slice interval
538 * long since now.
539 */
540 if (throtl_slice_used(td, tg, rw))
541 throtl_start_new_slice(td, tg, rw);
542 else {
543 if (time_before(tg->slice_end[rw], jiffies + throtl_slice))
544 throtl_extend_slice(td, tg, rw, jiffies + throtl_slice);
545 }
546
547 if (tg_with_in_bps_limit(td, tg, bio, &bps_wait)
548 && tg_with_in_iops_limit(td, tg, bio, &iops_wait)) {
549 if (wait)
550 *wait = 0;
551 return 1;
552 }
553
554 max_wait = max(bps_wait, iops_wait);
555
556 if (wait)
557 *wait = max_wait;
558
559 if (time_before(tg->slice_end[rw], jiffies + max_wait))
560 throtl_extend_slice(td, tg, rw, jiffies + max_wait);
Vivek Goyale43473b2010-09-15 17:06:35 -0400561
562 return 0;
563}
564
Tejun Heo629ed0b2012-04-01 14:38:44 -0700565static void throtl_update_dispatch_stats(struct blkio_group *blkg, u64 bytes,
566 int rw)
567{
568 struct blkg_policy_data *pd = blkg->pd[BLKIO_POLICY_THROTL];
569 struct blkio_group_stats_cpu *stats_cpu;
570 unsigned long flags;
571
572 /* If per cpu stats are not allocated yet, don't do any accounting. */
573 if (pd->stats_cpu == NULL)
574 return;
575
576 /*
577 * Disabling interrupts to provide mutual exclusion between two
578 * writes on same cpu. It probably is not needed for 64bit. Not
579 * optimizing that case yet.
580 */
581 local_irq_save(flags);
582
583 stats_cpu = this_cpu_ptr(pd->stats_cpu);
584
585 blkg_stat_add(&stats_cpu->sectors, bytes >> 9);
586 blkg_rwstat_add(&stats_cpu->serviced, rw, 1);
587 blkg_rwstat_add(&stats_cpu->service_bytes, rw, bytes);
588
589 local_irq_restore(flags);
590}
591
Vivek Goyale43473b2010-09-15 17:06:35 -0400592static void throtl_charge_bio(struct throtl_grp *tg, struct bio *bio)
593{
594 bool rw = bio_data_dir(bio);
Vivek Goyale43473b2010-09-15 17:06:35 -0400595
596 /* Charge the bio to the group */
597 tg->bytes_disp[rw] += bio->bi_size;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400598 tg->io_disp[rw]++;
Vivek Goyale43473b2010-09-15 17:06:35 -0400599
Tejun Heo629ed0b2012-04-01 14:38:44 -0700600 throtl_update_dispatch_stats(tg_to_blkg(tg), bio->bi_size, bio->bi_rw);
Vivek Goyale43473b2010-09-15 17:06:35 -0400601}
602
603static void throtl_add_bio_tg(struct throtl_data *td, struct throtl_grp *tg,
604 struct bio *bio)
605{
606 bool rw = bio_data_dir(bio);
607
608 bio_list_add(&tg->bio_lists[rw], bio);
609 /* Take a bio reference on tg */
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800610 blkg_get(tg_to_blkg(tg));
Vivek Goyale43473b2010-09-15 17:06:35 -0400611 tg->nr_queued[rw]++;
612 td->nr_queued[rw]++;
613 throtl_enqueue_tg(td, tg);
614}
615
616static void tg_update_disptime(struct throtl_data *td, struct throtl_grp *tg)
617{
618 unsigned long read_wait = -1, write_wait = -1, min_wait = -1, disptime;
619 struct bio *bio;
620
621 if ((bio = bio_list_peek(&tg->bio_lists[READ])))
622 tg_may_dispatch(td, tg, bio, &read_wait);
623
624 if ((bio = bio_list_peek(&tg->bio_lists[WRITE])))
625 tg_may_dispatch(td, tg, bio, &write_wait);
626
627 min_wait = min(read_wait, write_wait);
628 disptime = jiffies + min_wait;
629
Vivek Goyale43473b2010-09-15 17:06:35 -0400630 /* Update dispatch time */
631 throtl_dequeue_tg(td, tg);
632 tg->disptime = disptime;
633 throtl_enqueue_tg(td, tg);
634}
635
636static void tg_dispatch_one_bio(struct throtl_data *td, struct throtl_grp *tg,
637 bool rw, struct bio_list *bl)
638{
639 struct bio *bio;
640
641 bio = bio_list_pop(&tg->bio_lists[rw]);
642 tg->nr_queued[rw]--;
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800643 /* Drop bio reference on blkg */
644 blkg_put(tg_to_blkg(tg));
Vivek Goyale43473b2010-09-15 17:06:35 -0400645
646 BUG_ON(td->nr_queued[rw] <= 0);
647 td->nr_queued[rw]--;
648
649 throtl_charge_bio(tg, bio);
650 bio_list_add(bl, bio);
651 bio->bi_rw |= REQ_THROTTLED;
652
653 throtl_trim_slice(td, tg, rw);
654}
655
656static int throtl_dispatch_tg(struct throtl_data *td, struct throtl_grp *tg,
657 struct bio_list *bl)
658{
659 unsigned int nr_reads = 0, nr_writes = 0;
660 unsigned int max_nr_reads = throtl_grp_quantum*3/4;
Vivek Goyalc2f68052010-11-15 19:32:42 +0100661 unsigned int max_nr_writes = throtl_grp_quantum - max_nr_reads;
Vivek Goyale43473b2010-09-15 17:06:35 -0400662 struct bio *bio;
663
664 /* Try to dispatch 75% READS and 25% WRITES */
665
666 while ((bio = bio_list_peek(&tg->bio_lists[READ]))
667 && tg_may_dispatch(td, tg, bio, NULL)) {
668
669 tg_dispatch_one_bio(td, tg, bio_data_dir(bio), bl);
670 nr_reads++;
671
672 if (nr_reads >= max_nr_reads)
673 break;
674 }
675
676 while ((bio = bio_list_peek(&tg->bio_lists[WRITE]))
677 && tg_may_dispatch(td, tg, bio, NULL)) {
678
679 tg_dispatch_one_bio(td, tg, bio_data_dir(bio), bl);
680 nr_writes++;
681
682 if (nr_writes >= max_nr_writes)
683 break;
684 }
685
686 return nr_reads + nr_writes;
687}
688
689static int throtl_select_dispatch(struct throtl_data *td, struct bio_list *bl)
690{
691 unsigned int nr_disp = 0;
692 struct throtl_grp *tg;
693 struct throtl_rb_root *st = &td->tg_service_tree;
694
695 while (1) {
696 tg = throtl_rb_first(st);
697
698 if (!tg)
699 break;
700
701 if (time_before(jiffies, tg->disptime))
702 break;
703
704 throtl_dequeue_tg(td, tg);
705
706 nr_disp += throtl_dispatch_tg(td, tg, bl);
707
708 if (tg->nr_queued[0] || tg->nr_queued[1]) {
709 tg_update_disptime(td, tg);
710 throtl_enqueue_tg(td, tg);
711 }
712
713 if (nr_disp >= throtl_quantum)
714 break;
715 }
716
717 return nr_disp;
718}
719
Vivek Goyalfe071432010-10-01 14:49:49 +0200720static void throtl_process_limit_change(struct throtl_data *td)
721{
Tejun Heo4eef3042012-03-05 13:15:18 -0800722 struct request_queue *q = td->queue;
723 struct blkio_group *blkg, *n;
Vivek Goyalfe071432010-10-01 14:49:49 +0200724
Vivek Goyalde701c72011-03-07 21:09:32 +0100725 if (!td->limits_changed)
Vivek Goyalfe071432010-10-01 14:49:49 +0200726 return;
727
Vivek Goyalde701c72011-03-07 21:09:32 +0100728 xchg(&td->limits_changed, false);
Vivek Goyalfe071432010-10-01 14:49:49 +0200729
Vivek Goyalde701c72011-03-07 21:09:32 +0100730 throtl_log(td, "limits changed");
Vivek Goyalfe071432010-10-01 14:49:49 +0200731
Tejun Heoe8989fa2012-03-05 13:15:20 -0800732 list_for_each_entry_safe(blkg, n, &q->blkg_list, q_node) {
Tejun Heo4eef3042012-03-05 13:15:18 -0800733 struct throtl_grp *tg = blkg_to_tg(blkg);
734
Vivek Goyalde701c72011-03-07 21:09:32 +0100735 if (!tg->limits_changed)
736 continue;
Vivek Goyalfe071432010-10-01 14:49:49 +0200737
Vivek Goyalde701c72011-03-07 21:09:32 +0100738 if (!xchg(&tg->limits_changed, false))
739 continue;
740
741 throtl_log_tg(td, tg, "limit change rbps=%llu wbps=%llu"
742 " riops=%u wiops=%u", tg->bps[READ], tg->bps[WRITE],
743 tg->iops[READ], tg->iops[WRITE]);
744
Vivek Goyal04521db2011-03-22 21:54:29 +0100745 /*
746 * Restart the slices for both READ and WRITES. It
747 * might happen that a group's limit are dropped
748 * suddenly and we don't want to account recently
749 * dispatched IO with new low rate
750 */
751 throtl_start_new_slice(td, tg, 0);
752 throtl_start_new_slice(td, tg, 1);
753
Vivek Goyalde701c72011-03-07 21:09:32 +0100754 if (throtl_tg_on_rr(tg))
755 tg_update_disptime(td, tg);
756 }
Vivek Goyalfe071432010-10-01 14:49:49 +0200757}
758
Vivek Goyale43473b2010-09-15 17:06:35 -0400759/* Dispatch throttled bios. Should be called without queue lock held. */
760static int throtl_dispatch(struct request_queue *q)
761{
762 struct throtl_data *td = q->td;
763 unsigned int nr_disp = 0;
764 struct bio_list bio_list_on_stack;
765 struct bio *bio;
Vivek Goyal69d60eb2011-03-09 08:27:37 +0100766 struct blk_plug plug;
Vivek Goyale43473b2010-09-15 17:06:35 -0400767
768 spin_lock_irq(q->queue_lock);
769
Vivek Goyalfe071432010-10-01 14:49:49 +0200770 throtl_process_limit_change(td);
771
Vivek Goyale43473b2010-09-15 17:06:35 -0400772 if (!total_nr_queued(td))
773 goto out;
774
775 bio_list_init(&bio_list_on_stack);
776
Joe Perchesd2f31a52011-06-13 20:19:27 +0200777 throtl_log(td, "dispatch nr_queued=%u read=%u write=%u",
Vivek Goyale43473b2010-09-15 17:06:35 -0400778 total_nr_queued(td), td->nr_queued[READ],
779 td->nr_queued[WRITE]);
780
781 nr_disp = throtl_select_dispatch(td, &bio_list_on_stack);
782
783 if (nr_disp)
784 throtl_log(td, "bios disp=%u", nr_disp);
785
786 throtl_schedule_next_dispatch(td);
787out:
788 spin_unlock_irq(q->queue_lock);
789
790 /*
791 * If we dispatched some requests, unplug the queue to make sure
792 * immediate dispatch
793 */
794 if (nr_disp) {
Vivek Goyal69d60eb2011-03-09 08:27:37 +0100795 blk_start_plug(&plug);
Vivek Goyale43473b2010-09-15 17:06:35 -0400796 while((bio = bio_list_pop(&bio_list_on_stack)))
797 generic_make_request(bio);
Vivek Goyal69d60eb2011-03-09 08:27:37 +0100798 blk_finish_plug(&plug);
Vivek Goyale43473b2010-09-15 17:06:35 -0400799 }
800 return nr_disp;
801}
802
803void blk_throtl_work(struct work_struct *work)
804{
805 struct throtl_data *td = container_of(work, struct throtl_data,
806 throtl_work.work);
807 struct request_queue *q = td->queue;
808
809 throtl_dispatch(q);
810}
811
812/* Call with queue lock held */
Vivek Goyal450adcb2011-03-01 13:40:54 -0500813static void
814throtl_schedule_delayed_work(struct throtl_data *td, unsigned long delay)
Vivek Goyale43473b2010-09-15 17:06:35 -0400815{
816
Vivek Goyale43473b2010-09-15 17:06:35 -0400817 struct delayed_work *dwork = &td->throtl_work;
818
Vivek Goyal04521db2011-03-22 21:54:29 +0100819 /* schedule work if limits changed even if no bio is queued */
Joe Perchesd2f31a52011-06-13 20:19:27 +0200820 if (total_nr_queued(td) || td->limits_changed) {
Vivek Goyale43473b2010-09-15 17:06:35 -0400821 /*
822 * We might have a work scheduled to be executed in future.
823 * Cancel that and schedule a new one.
824 */
825 __cancel_delayed_work(dwork);
Vivek Goyal450adcb2011-03-01 13:40:54 -0500826 queue_delayed_work(kthrotld_workqueue, dwork, delay);
Vivek Goyale43473b2010-09-15 17:06:35 -0400827 throtl_log(td, "schedule work. delay=%lu jiffies=%lu",
828 delay, jiffies);
829 }
830}
Vivek Goyale43473b2010-09-15 17:06:35 -0400831
Tejun Heo60c2bc22012-04-01 14:38:43 -0700832/*
833 * Can not take queue lock in update functions as queue lock under
834 * blkcg_lock is not allowed. Under other paths we take blkcg_lock under
835 * queue_lock.
836 */
Vivek Goyalde701c72011-03-07 21:09:32 +0100837static void throtl_update_blkio_group_common(struct throtl_data *td,
838 struct throtl_grp *tg)
839{
840 xchg(&tg->limits_changed, true);
841 xchg(&td->limits_changed, true);
842 /* Schedule a work now to process the limit change */
843 throtl_schedule_delayed_work(td, 0);
844}
845
Tejun Heo60c2bc22012-04-01 14:38:43 -0700846static u64 blkg_prfill_conf_u64(struct seq_file *sf,
847 struct blkg_policy_data *pd, int off)
848{
849 u64 v = *(u64 *)((void *)&pd->conf + off);
850
851 if (!v)
852 return 0;
853 return __blkg_prfill_u64(sf, pd, v);
854}
855
856static int blkcg_print_conf_u64(struct cgroup *cgrp, struct cftype *cft,
857 struct seq_file *sf)
858{
859 blkcg_print_blkgs(sf, cgroup_to_blkio_cgroup(cgrp),
860 blkg_prfill_conf_u64, BLKIO_POLICY_THROTL,
861 cft->private, false);
862 return 0;
863}
864
865static void throtl_update_blkio_group_read_bps(struct blkio_group *blkg,
866 u64 read_bps)
Vivek Goyale43473b2010-09-15 17:06:35 -0400867{
Tejun Heo03814112012-03-05 13:15:14 -0800868 struct throtl_grp *tg = blkg_to_tg(blkg);
Vivek Goyalfe071432010-10-01 14:49:49 +0200869
Vivek Goyalde701c72011-03-07 21:09:32 +0100870 tg->bps[READ] = read_bps;
Tejun Heo60c2bc22012-04-01 14:38:43 -0700871 throtl_update_blkio_group_common(blkg->q->td, tg);
Vivek Goyale43473b2010-09-15 17:06:35 -0400872}
873
Tejun Heo60c2bc22012-04-01 14:38:43 -0700874static void throtl_update_blkio_group_write_bps(struct blkio_group *blkg,
875 u64 write_bps)
Vivek Goyale43473b2010-09-15 17:06:35 -0400876{
Tejun Heo03814112012-03-05 13:15:14 -0800877 struct throtl_grp *tg = blkg_to_tg(blkg);
Vivek Goyalfe071432010-10-01 14:49:49 +0200878
Vivek Goyalde701c72011-03-07 21:09:32 +0100879 tg->bps[WRITE] = write_bps;
Tejun Heo60c2bc22012-04-01 14:38:43 -0700880 throtl_update_blkio_group_common(blkg->q->td, tg);
Vivek Goyale43473b2010-09-15 17:06:35 -0400881}
882
Tejun Heo60c2bc22012-04-01 14:38:43 -0700883static void throtl_update_blkio_group_read_iops(struct blkio_group *blkg,
884 u64 read_iops)
Vivek Goyal8e89d132010-09-15 17:06:37 -0400885{
Tejun Heo03814112012-03-05 13:15:14 -0800886 struct throtl_grp *tg = blkg_to_tg(blkg);
Vivek Goyalfe071432010-10-01 14:49:49 +0200887
Vivek Goyalde701c72011-03-07 21:09:32 +0100888 tg->iops[READ] = read_iops;
Tejun Heo60c2bc22012-04-01 14:38:43 -0700889 throtl_update_blkio_group_common(blkg->q->td, tg);
Vivek Goyal8e89d132010-09-15 17:06:37 -0400890}
891
Tejun Heo60c2bc22012-04-01 14:38:43 -0700892static void throtl_update_blkio_group_write_iops(struct blkio_group *blkg,
893 u64 write_iops)
Vivek Goyal8e89d132010-09-15 17:06:37 -0400894{
Tejun Heo03814112012-03-05 13:15:14 -0800895 struct throtl_grp *tg = blkg_to_tg(blkg);
Vivek Goyalfe071432010-10-01 14:49:49 +0200896
Vivek Goyalde701c72011-03-07 21:09:32 +0100897 tg->iops[WRITE] = write_iops;
Tejun Heo60c2bc22012-04-01 14:38:43 -0700898 throtl_update_blkio_group_common(blkg->q->td, tg);
Vivek Goyal8e89d132010-09-15 17:06:37 -0400899}
900
Tejun Heo60c2bc22012-04-01 14:38:43 -0700901static int blkcg_set_conf_u64(struct cgroup *cgrp, struct cftype *cft,
902 const char *buf,
903 void (*update)(struct blkio_group *, u64))
904{
905 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp);
906 struct blkg_policy_data *pd;
907 struct blkg_conf_ctx ctx;
908 int ret;
909
910 ret = blkg_conf_prep(blkcg, buf, &ctx);
911 if (ret)
912 return ret;
913
914 ret = -EINVAL;
915 pd = ctx.blkg->pd[BLKIO_POLICY_THROTL];
916 if (pd) {
917 *(u64 *)((void *)&pd->conf + cft->private) = ctx.v;
918 update(ctx.blkg, ctx.v ?: -1);
919 ret = 0;
920 }
921
922 blkg_conf_finish(&ctx);
923 return ret;
924}
925
926static int blkcg_set_conf_bps_r(struct cgroup *cgrp, struct cftype *cft,
927 const char *buf)
928{
929 return blkcg_set_conf_u64(cgrp, cft, buf,
930 throtl_update_blkio_group_read_bps);
931}
932
933static int blkcg_set_conf_bps_w(struct cgroup *cgrp, struct cftype *cft,
934 const char *buf)
935{
936 return blkcg_set_conf_u64(cgrp, cft, buf,
937 throtl_update_blkio_group_write_bps);
938}
939
940static int blkcg_set_conf_iops_r(struct cgroup *cgrp, struct cftype *cft,
941 const char *buf)
942{
943 return blkcg_set_conf_u64(cgrp, cft, buf,
944 throtl_update_blkio_group_read_iops);
945}
946
947static int blkcg_set_conf_iops_w(struct cgroup *cgrp, struct cftype *cft,
948 const char *buf)
949{
950 return blkcg_set_conf_u64(cgrp, cft, buf,
951 throtl_update_blkio_group_write_iops);
952}
953
954static struct cftype throtl_files[] = {
955 {
956 .name = "throttle.read_bps_device",
957 .private = offsetof(struct blkio_group_conf, bps[READ]),
958 .read_seq_string = blkcg_print_conf_u64,
959 .write_string = blkcg_set_conf_bps_r,
960 .max_write_len = 256,
961 },
962 {
963 .name = "throttle.write_bps_device",
964 .private = offsetof(struct blkio_group_conf, bps[WRITE]),
965 .read_seq_string = blkcg_print_conf_u64,
966 .write_string = blkcg_set_conf_bps_w,
967 .max_write_len = 256,
968 },
969 {
970 .name = "throttle.read_iops_device",
971 .private = offsetof(struct blkio_group_conf, iops[READ]),
972 .read_seq_string = blkcg_print_conf_u64,
973 .write_string = blkcg_set_conf_iops_r,
974 .max_write_len = 256,
975 },
976 {
977 .name = "throttle.write_iops_device",
978 .private = offsetof(struct blkio_group_conf, iops[WRITE]),
979 .read_seq_string = blkcg_print_conf_u64,
980 .write_string = blkcg_set_conf_iops_w,
981 .max_write_len = 256,
982 },
983 {
984 .name = "throttle.io_service_bytes",
985 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_THROTL,
986 offsetof(struct blkio_group_stats_cpu, service_bytes)),
987 .read_seq_string = blkcg_print_cpu_rwstat,
988 },
989 {
990 .name = "throttle.io_serviced",
991 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_THROTL,
992 offsetof(struct blkio_group_stats_cpu, serviced)),
993 .read_seq_string = blkcg_print_cpu_rwstat,
994 },
995 { } /* terminate */
996};
997
Vivek Goyalda527772011-03-02 19:05:33 -0500998static void throtl_shutdown_wq(struct request_queue *q)
Vivek Goyale43473b2010-09-15 17:06:35 -0400999{
1000 struct throtl_data *td = q->td;
1001
1002 cancel_delayed_work_sync(&td->throtl_work);
1003}
1004
1005static struct blkio_policy_type blkio_policy_throtl = {
1006 .ops = {
Tejun Heo03814112012-03-05 13:15:14 -08001007 .blkio_init_group_fn = throtl_init_blkio_group,
Vivek Goyale43473b2010-09-15 17:06:35 -04001008 },
Vivek Goyal8e89d132010-09-15 17:06:37 -04001009 .plid = BLKIO_POLICY_THROTL,
Tejun Heo03814112012-03-05 13:15:14 -08001010 .pdata_size = sizeof(struct throtl_grp),
Tejun Heo60c2bc22012-04-01 14:38:43 -07001011 .cftypes = throtl_files,
Vivek Goyale43473b2010-09-15 17:06:35 -04001012};
1013
Tejun Heobc16a4f2011-10-19 14:33:01 +02001014bool blk_throtl_bio(struct request_queue *q, struct bio *bio)
Vivek Goyale43473b2010-09-15 17:06:35 -04001015{
1016 struct throtl_data *td = q->td;
1017 struct throtl_grp *tg;
Vivek Goyale43473b2010-09-15 17:06:35 -04001018 bool rw = bio_data_dir(bio), update_disptime = true;
Vivek Goyalaf75cd32011-05-19 15:38:31 -04001019 struct blkio_cgroup *blkcg;
Tejun Heobc16a4f2011-10-19 14:33:01 +02001020 bool throttled = false;
Vivek Goyale43473b2010-09-15 17:06:35 -04001021
1022 if (bio->bi_rw & REQ_THROTTLED) {
1023 bio->bi_rw &= ~REQ_THROTTLED;
Tejun Heobc16a4f2011-10-19 14:33:01 +02001024 goto out;
Vivek Goyale43473b2010-09-15 17:06:35 -04001025 }
1026
Tejun Heo671058f2012-03-05 13:15:29 -08001027 /* bio_associate_current() needs ioc, try creating */
1028 create_io_context(GFP_ATOMIC, q->node);
1029
Vivek Goyalaf75cd32011-05-19 15:38:31 -04001030 /*
1031 * A throtl_grp pointer retrieved under rcu can be used to access
1032 * basic fields like stats and io rates. If a group has no rules,
1033 * just update the dispatch stats in lockless manner and return.
1034 */
Vivek Goyalaf75cd32011-05-19 15:38:31 -04001035 rcu_read_lock();
Tejun Heo4f85cb92012-03-05 13:15:28 -08001036 blkcg = bio_blkio_cgroup(bio);
Tejun Heocd1604f2012-03-05 13:15:06 -08001037 tg = throtl_lookup_tg(td, blkcg);
Vivek Goyalaf75cd32011-05-19 15:38:31 -04001038 if (tg) {
Vivek Goyalaf75cd32011-05-19 15:38:31 -04001039 if (tg_no_rule_group(tg, rw)) {
Tejun Heo629ed0b2012-04-01 14:38:44 -07001040 throtl_update_dispatch_stats(tg_to_blkg(tg),
1041 bio->bi_size, bio->bi_rw);
Tejun Heo2a7f1242012-03-05 13:15:01 -08001042 goto out_unlock_rcu;
Vivek Goyalaf75cd32011-05-19 15:38:31 -04001043 }
1044 }
Vivek Goyalaf75cd32011-05-19 15:38:31 -04001045
1046 /*
1047 * Either group has not been allocated yet or it is not an unlimited
1048 * IO group
1049 */
Vivek Goyale43473b2010-09-15 17:06:35 -04001050 spin_lock_irq(q->queue_lock);
Tejun Heocd1604f2012-03-05 13:15:06 -08001051 tg = throtl_lookup_create_tg(td, blkcg);
Tejun Heobc16a4f2011-10-19 14:33:01 +02001052 if (unlikely(!tg))
1053 goto out_unlock;
Vivek Goyalf469a7b2011-05-19 15:38:23 -04001054
Vivek Goyale43473b2010-09-15 17:06:35 -04001055 if (tg->nr_queued[rw]) {
1056 /*
1057 * There is already another bio queued in same dir. No
1058 * need to update dispatch time.
1059 */
Vivek Goyal231d7042011-03-07 21:05:14 +01001060 update_disptime = false;
Vivek Goyale43473b2010-09-15 17:06:35 -04001061 goto queue_bio;
Vivek Goyalde701c72011-03-07 21:09:32 +01001062
Vivek Goyale43473b2010-09-15 17:06:35 -04001063 }
1064
1065 /* Bio is with-in rate limit of group */
1066 if (tg_may_dispatch(td, tg, bio, NULL)) {
1067 throtl_charge_bio(tg, bio);
Vivek Goyal04521db2011-03-22 21:54:29 +01001068
1069 /*
1070 * We need to trim slice even when bios are not being queued
1071 * otherwise it might happen that a bio is not queued for
1072 * a long time and slice keeps on extending and trim is not
1073 * called for a long time. Now if limits are reduced suddenly
1074 * we take into account all the IO dispatched so far at new
1075 * low rate and * newly queued IO gets a really long dispatch
1076 * time.
1077 *
1078 * So keep on trimming slice even if bio is not queued.
1079 */
1080 throtl_trim_slice(td, tg, rw);
Tejun Heobc16a4f2011-10-19 14:33:01 +02001081 goto out_unlock;
Vivek Goyale43473b2010-09-15 17:06:35 -04001082 }
1083
1084queue_bio:
Joe Perchesfd16d262011-06-13 10:42:49 +02001085 throtl_log_tg(td, tg, "[%c] bio. bdisp=%llu sz=%u bps=%llu"
Vivek Goyal8e89d132010-09-15 17:06:37 -04001086 " iodisp=%u iops=%u queued=%d/%d",
1087 rw == READ ? 'R' : 'W',
Vivek Goyale43473b2010-09-15 17:06:35 -04001088 tg->bytes_disp[rw], bio->bi_size, tg->bps[rw],
Vivek Goyal8e89d132010-09-15 17:06:37 -04001089 tg->io_disp[rw], tg->iops[rw],
Vivek Goyale43473b2010-09-15 17:06:35 -04001090 tg->nr_queued[READ], tg->nr_queued[WRITE]);
1091
Tejun Heo671058f2012-03-05 13:15:29 -08001092 bio_associate_current(bio);
Vivek Goyale43473b2010-09-15 17:06:35 -04001093 throtl_add_bio_tg(q->td, tg, bio);
Tejun Heobc16a4f2011-10-19 14:33:01 +02001094 throttled = true;
Vivek Goyale43473b2010-09-15 17:06:35 -04001095
1096 if (update_disptime) {
1097 tg_update_disptime(td, tg);
1098 throtl_schedule_next_dispatch(td);
1099 }
1100
Tejun Heobc16a4f2011-10-19 14:33:01 +02001101out_unlock:
Vivek Goyale43473b2010-09-15 17:06:35 -04001102 spin_unlock_irq(q->queue_lock);
Tejun Heo2a7f1242012-03-05 13:15:01 -08001103out_unlock_rcu:
1104 rcu_read_unlock();
Tejun Heobc16a4f2011-10-19 14:33:01 +02001105out:
1106 return throttled;
Vivek Goyale43473b2010-09-15 17:06:35 -04001107}
1108
Tejun Heoc9a929d2011-10-19 14:42:16 +02001109/**
1110 * blk_throtl_drain - drain throttled bios
1111 * @q: request_queue to drain throttled bios for
1112 *
1113 * Dispatch all currently throttled bios on @q through ->make_request_fn().
1114 */
1115void blk_throtl_drain(struct request_queue *q)
1116 __releases(q->queue_lock) __acquires(q->queue_lock)
1117{
1118 struct throtl_data *td = q->td;
1119 struct throtl_rb_root *st = &td->tg_service_tree;
1120 struct throtl_grp *tg;
1121 struct bio_list bl;
1122 struct bio *bio;
1123
Jens Axboe334c2b02011-10-25 15:51:48 +02001124 WARN_ON_ONCE(!queue_is_locked(q));
Tejun Heoc9a929d2011-10-19 14:42:16 +02001125
1126 bio_list_init(&bl);
1127
1128 while ((tg = throtl_rb_first(st))) {
1129 throtl_dequeue_tg(td, tg);
1130
1131 while ((bio = bio_list_peek(&tg->bio_lists[READ])))
1132 tg_dispatch_one_bio(td, tg, bio_data_dir(bio), &bl);
1133 while ((bio = bio_list_peek(&tg->bio_lists[WRITE])))
1134 tg_dispatch_one_bio(td, tg, bio_data_dir(bio), &bl);
1135 }
1136 spin_unlock_irq(q->queue_lock);
1137
1138 while ((bio = bio_list_pop(&bl)))
1139 generic_make_request(bio);
1140
1141 spin_lock_irq(q->queue_lock);
1142}
1143
Vivek Goyale43473b2010-09-15 17:06:35 -04001144int blk_throtl_init(struct request_queue *q)
1145{
1146 struct throtl_data *td;
Tejun Heocd1604f2012-03-05 13:15:06 -08001147 struct blkio_group *blkg;
Vivek Goyale43473b2010-09-15 17:06:35 -04001148
1149 td = kzalloc_node(sizeof(*td), GFP_KERNEL, q->node);
1150 if (!td)
1151 return -ENOMEM;
1152
Vivek Goyale43473b2010-09-15 17:06:35 -04001153 td->tg_service_tree = THROTL_RB_ROOT;
Vivek Goyalde701c72011-03-07 21:09:32 +01001154 td->limits_changed = false;
Vivek Goyala29a1712011-05-19 15:38:19 -04001155 INIT_DELAYED_WORK(&td->throtl_work, blk_throtl_work);
Vivek Goyale43473b2010-09-15 17:06:35 -04001156
Tejun Heocd1604f2012-03-05 13:15:06 -08001157 q->td = td;
Vivek Goyal29b12582011-05-19 15:38:24 -04001158 td->queue = q;
Vivek Goyal02977e42010-10-01 14:49:48 +02001159
Tejun Heocd1604f2012-03-05 13:15:06 -08001160 /* alloc and init root group. */
Tejun Heof51b8022012-03-05 13:15:05 -08001161 rcu_read_lock();
1162 spin_lock_irq(q->queue_lock);
1163
Tejun Heoaaec55a2012-04-01 14:38:42 -07001164 blkg = blkg_lookup_create(&blkio_root_cgroup, q, true);
Tejun Heocd1604f2012-03-05 13:15:06 -08001165 if (!IS_ERR(blkg))
Tejun Heo03814112012-03-05 13:15:14 -08001166 td->root_tg = blkg_to_tg(blkg);
Tejun Heof51b8022012-03-05 13:15:05 -08001167
1168 spin_unlock_irq(q->queue_lock);
1169 rcu_read_unlock();
1170
1171 if (!td->root_tg) {
Vivek Goyal29b12582011-05-19 15:38:24 -04001172 kfree(td);
1173 return -ENOMEM;
1174 }
Vivek Goyale43473b2010-09-15 17:06:35 -04001175 return 0;
1176}
1177
1178void blk_throtl_exit(struct request_queue *q)
1179{
Tejun Heoc875f4d2012-03-05 13:15:22 -08001180 BUG_ON(!q->td);
Vivek Goyalda527772011-03-02 19:05:33 -05001181 throtl_shutdown_wq(q);
Tejun Heoc9a929d2011-10-19 14:42:16 +02001182 kfree(q->td);
Vivek Goyale43473b2010-09-15 17:06:35 -04001183}
1184
1185static int __init throtl_init(void)
1186{
Vivek Goyal450adcb2011-03-01 13:40:54 -05001187 kthrotld_workqueue = alloc_workqueue("kthrotld", WQ_MEM_RECLAIM, 0);
1188 if (!kthrotld_workqueue)
1189 panic("Failed to create kthrotld\n");
1190
Vivek Goyale43473b2010-09-15 17:06:35 -04001191 blkio_policy_register(&blkio_policy_throtl);
1192 return 0;
1193}
1194
1195module_init(throtl_init);