blob: 507b1c60894169ea2a3892b669bafede8213d184 [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 Heo3c798392012-04-16 13:57:25 -070024static struct blkcg_policy blkcg_policy_throtl;
Tejun Heo03814112012-03-05 13:15:14 -080025
Vivek Goyal450adcb2011-03-01 13:40:54 -050026/* A workqueue to queue throttle related work */
27static struct workqueue_struct *kthrotld_workqueue;
Vivek Goyal450adcb2011-03-01 13:40:54 -050028
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
Tejun Heo8a3d2612012-04-01 14:38:44 -070041/* Per-cpu group stats */
42struct tg_stats_cpu {
43 /* total bytes transferred */
44 struct blkg_rwstat service_bytes;
45 /* total IOs serviced, post merge */
46 struct blkg_rwstat serviced;
47};
48
Vivek Goyale43473b2010-09-15 17:06:35 -040049struct throtl_grp {
Tejun Heof95a04a2012-04-16 13:57:26 -070050 /* must be the first member */
51 struct blkg_policy_data pd;
52
Vivek Goyale43473b2010-09-15 17:06:35 -040053 /* active throtl group service_tree member */
54 struct rb_node rb_node;
55
56 /*
57 * Dispatch time in jiffies. This is the estimated time when group
58 * will unthrottle and is ready to dispatch more bio. It is used as
59 * key to sort active groups in service tree.
60 */
61 unsigned long disptime;
62
Vivek Goyale43473b2010-09-15 17:06:35 -040063 unsigned int flags;
64
65 /* Two lists for READ and WRITE */
66 struct bio_list bio_lists[2];
67
68 /* Number of queued bios on READ and WRITE lists */
69 unsigned int nr_queued[2];
70
71 /* bytes per second rate limits */
72 uint64_t bps[2];
73
Vivek Goyal8e89d132010-09-15 17:06:37 -040074 /* IOPS limits */
75 unsigned int iops[2];
76
Vivek Goyale43473b2010-09-15 17:06:35 -040077 /* Number of bytes disptached in current slice */
78 uint64_t bytes_disp[2];
Vivek Goyal8e89d132010-09-15 17:06:37 -040079 /* Number of bio's dispatched in current slice */
80 unsigned int io_disp[2];
Vivek Goyale43473b2010-09-15 17:06:35 -040081
82 /* When did we start a new slice */
83 unsigned long slice_start[2];
84 unsigned long slice_end[2];
Vivek Goyalfe071432010-10-01 14:49:49 +020085
Tejun Heo8a3d2612012-04-01 14:38:44 -070086 /* Per cpu stats pointer */
87 struct tg_stats_cpu __percpu *stats_cpu;
88
89 /* List of tgs waiting for per cpu stats memory to be allocated */
90 struct list_head stats_alloc_node;
Vivek Goyale43473b2010-09-15 17:06:35 -040091};
92
93struct throtl_data
94{
Vivek Goyale43473b2010-09-15 17:06:35 -040095 /* service tree for active throtl groups */
96 struct throtl_rb_root tg_service_tree;
97
Vivek Goyale43473b2010-09-15 17:06:35 -040098 struct request_queue *queue;
99
100 /* Total Number of queued bios on READ and WRITE lists */
101 unsigned int nr_queued[2];
102
103 /*
Vivek Goyal02977e42010-10-01 14:49:48 +0200104 * number of total undestroyed groups
Vivek Goyale43473b2010-09-15 17:06:35 -0400105 */
106 unsigned int nr_undestroyed_grps;
107
108 /* Work for dispatching throttled bios */
Tejun Heocb761992013-05-14 13:52:31 -0700109 struct delayed_work dispatch_work;
Vivek Goyale43473b2010-09-15 17:06:35 -0400110};
111
Tejun Heo8a3d2612012-04-01 14:38:44 -0700112/* list and work item to allocate percpu group stats */
113static DEFINE_SPINLOCK(tg_stats_alloc_lock);
114static LIST_HEAD(tg_stats_alloc_list);
115
116static void tg_stats_alloc_fn(struct work_struct *);
117static DECLARE_DELAYED_WORK(tg_stats_alloc_work, tg_stats_alloc_fn);
118
Tejun Heof95a04a2012-04-16 13:57:26 -0700119static inline struct throtl_grp *pd_to_tg(struct blkg_policy_data *pd)
120{
121 return pd ? container_of(pd, struct throtl_grp, pd) : NULL;
122}
123
Tejun Heo3c798392012-04-16 13:57:25 -0700124static inline struct throtl_grp *blkg_to_tg(struct blkcg_gq *blkg)
Tejun Heo03814112012-03-05 13:15:14 -0800125{
Tejun Heof95a04a2012-04-16 13:57:26 -0700126 return pd_to_tg(blkg_to_pd(blkg, &blkcg_policy_throtl));
Tejun Heo03814112012-03-05 13:15:14 -0800127}
128
Tejun Heo3c798392012-04-16 13:57:25 -0700129static inline struct blkcg_gq *tg_to_blkg(struct throtl_grp *tg)
Tejun Heo03814112012-03-05 13:15:14 -0800130{
Tejun Heof95a04a2012-04-16 13:57:26 -0700131 return pd_to_blkg(&tg->pd);
Tejun Heo03814112012-03-05 13:15:14 -0800132}
133
Tejun Heo03d8e112012-04-13 13:11:32 -0700134static inline struct throtl_grp *td_root_tg(struct throtl_data *td)
135{
136 return blkg_to_tg(td->queue->root_blkg);
137}
138
Vivek Goyale43473b2010-09-15 17:06:35 -0400139enum tg_state_flags {
140 THROTL_TG_FLAG_on_rr = 0, /* on round-robin busy list */
141};
142
143#define THROTL_TG_FNS(name) \
144static inline void throtl_mark_tg_##name(struct throtl_grp *tg) \
145{ \
146 (tg)->flags |= (1 << THROTL_TG_FLAG_##name); \
147} \
148static inline void throtl_clear_tg_##name(struct throtl_grp *tg) \
149{ \
150 (tg)->flags &= ~(1 << THROTL_TG_FLAG_##name); \
151} \
152static inline int throtl_tg_##name(const struct throtl_grp *tg) \
153{ \
154 return ((tg)->flags & (1 << THROTL_TG_FLAG_##name)) != 0; \
155}
156
157THROTL_TG_FNS(on_rr);
158
Tejun Heo54e7ed12012-04-16 13:57:23 -0700159#define throtl_log_tg(td, tg, fmt, args...) do { \
160 char __pbuf[128]; \
161 \
162 blkg_path(tg_to_blkg(tg), __pbuf, sizeof(__pbuf)); \
163 blk_add_trace_msg((td)->queue, "throtl %s " fmt, __pbuf, ##args); \
164} while (0)
Vivek Goyale43473b2010-09-15 17:06:35 -0400165
166#define throtl_log(td, fmt, args...) \
167 blk_add_trace_msg((td)->queue, "throtl " fmt, ##args)
168
Joe Perchesd2f31a52011-06-13 20:19:27 +0200169static inline unsigned int total_nr_queued(struct throtl_data *td)
Vivek Goyale43473b2010-09-15 17:06:35 -0400170{
Joe Perchesd2f31a52011-06-13 20:19:27 +0200171 return td->nr_queued[0] + td->nr_queued[1];
Vivek Goyale43473b2010-09-15 17:06:35 -0400172}
173
Tejun Heo8a3d2612012-04-01 14:38:44 -0700174/*
175 * Worker for allocating per cpu stat for tgs. This is scheduled on the
Tejun Heo3b07e9c2012-08-20 14:51:24 -0700176 * system_wq once there are some groups on the alloc_list waiting for
Tejun Heo8a3d2612012-04-01 14:38:44 -0700177 * allocation.
178 */
179static void tg_stats_alloc_fn(struct work_struct *work)
180{
181 static struct tg_stats_cpu *stats_cpu; /* this fn is non-reentrant */
182 struct delayed_work *dwork = to_delayed_work(work);
183 bool empty = false;
184
185alloc_stats:
186 if (!stats_cpu) {
187 stats_cpu = alloc_percpu(struct tg_stats_cpu);
188 if (!stats_cpu) {
189 /* allocation failed, try again after some time */
Tejun Heo3b07e9c2012-08-20 14:51:24 -0700190 schedule_delayed_work(dwork, msecs_to_jiffies(10));
Tejun Heo8a3d2612012-04-01 14:38:44 -0700191 return;
192 }
193 }
194
195 spin_lock_irq(&tg_stats_alloc_lock);
196
197 if (!list_empty(&tg_stats_alloc_list)) {
198 struct throtl_grp *tg = list_first_entry(&tg_stats_alloc_list,
199 struct throtl_grp,
200 stats_alloc_node);
201 swap(tg->stats_cpu, stats_cpu);
202 list_del_init(&tg->stats_alloc_node);
203 }
204
205 empty = list_empty(&tg_stats_alloc_list);
206 spin_unlock_irq(&tg_stats_alloc_lock);
207 if (!empty)
208 goto alloc_stats;
209}
210
Tejun Heo3c798392012-04-16 13:57:25 -0700211static void throtl_pd_init(struct blkcg_gq *blkg)
Vivek Goyala29a1712011-05-19 15:38:19 -0400212{
Tejun Heo03814112012-03-05 13:15:14 -0800213 struct throtl_grp *tg = blkg_to_tg(blkg);
Tejun Heoff26eaa2012-05-23 12:16:21 +0200214 unsigned long flags;
Tejun Heocd1604f2012-03-05 13:15:06 -0800215
Vivek Goyala29a1712011-05-19 15:38:19 -0400216 RB_CLEAR_NODE(&tg->rb_node);
217 bio_list_init(&tg->bio_lists[0]);
218 bio_list_init(&tg->bio_lists[1]);
Vivek Goyala29a1712011-05-19 15:38:19 -0400219
Tejun Heoe56da7e2012-03-05 13:15:07 -0800220 tg->bps[READ] = -1;
221 tg->bps[WRITE] = -1;
222 tg->iops[READ] = -1;
223 tg->iops[WRITE] = -1;
Tejun Heo8a3d2612012-04-01 14:38:44 -0700224
225 /*
226 * Ugh... We need to perform per-cpu allocation for tg->stats_cpu
227 * but percpu allocator can't be called from IO path. Queue tg on
228 * tg_stats_alloc_list and allocate from work item.
229 */
Tejun Heoff26eaa2012-05-23 12:16:21 +0200230 spin_lock_irqsave(&tg_stats_alloc_lock, flags);
Tejun Heo8a3d2612012-04-01 14:38:44 -0700231 list_add(&tg->stats_alloc_node, &tg_stats_alloc_list);
Tejun Heo3b07e9c2012-08-20 14:51:24 -0700232 schedule_delayed_work(&tg_stats_alloc_work, 0);
Tejun Heoff26eaa2012-05-23 12:16:21 +0200233 spin_unlock_irqrestore(&tg_stats_alloc_lock, flags);
Tejun Heo8a3d2612012-04-01 14:38:44 -0700234}
235
Tejun Heo3c798392012-04-16 13:57:25 -0700236static void throtl_pd_exit(struct blkcg_gq *blkg)
Tejun Heo8a3d2612012-04-01 14:38:44 -0700237{
238 struct throtl_grp *tg = blkg_to_tg(blkg);
Tejun Heoff26eaa2012-05-23 12:16:21 +0200239 unsigned long flags;
Tejun Heo8a3d2612012-04-01 14:38:44 -0700240
Tejun Heoff26eaa2012-05-23 12:16:21 +0200241 spin_lock_irqsave(&tg_stats_alloc_lock, flags);
Tejun Heo8a3d2612012-04-01 14:38:44 -0700242 list_del_init(&tg->stats_alloc_node);
Tejun Heoff26eaa2012-05-23 12:16:21 +0200243 spin_unlock_irqrestore(&tg_stats_alloc_lock, flags);
Tejun Heo8a3d2612012-04-01 14:38:44 -0700244
245 free_percpu(tg->stats_cpu);
246}
247
Tejun Heo3c798392012-04-16 13:57:25 -0700248static void throtl_pd_reset_stats(struct blkcg_gq *blkg)
Tejun Heo8a3d2612012-04-01 14:38:44 -0700249{
250 struct throtl_grp *tg = blkg_to_tg(blkg);
251 int cpu;
252
253 if (tg->stats_cpu == NULL)
254 return;
255
256 for_each_possible_cpu(cpu) {
257 struct tg_stats_cpu *sc = per_cpu_ptr(tg->stats_cpu, cpu);
258
259 blkg_rwstat_reset(&sc->service_bytes);
260 blkg_rwstat_reset(&sc->serviced);
261 }
Vivek Goyala29a1712011-05-19 15:38:19 -0400262}
263
Tejun Heo3c798392012-04-16 13:57:25 -0700264static struct throtl_grp *throtl_lookup_tg(struct throtl_data *td,
265 struct blkcg *blkcg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400266{
Vivek Goyale43473b2010-09-15 17:06:35 -0400267 /*
Tejun Heo3c798392012-04-16 13:57:25 -0700268 * This is the common case when there are no blkcgs. Avoid lookup
269 * in this case
Tejun Heocd1604f2012-03-05 13:15:06 -0800270 */
Tejun Heo3c798392012-04-16 13:57:25 -0700271 if (blkcg == &blkcg_root)
Tejun Heo03d8e112012-04-13 13:11:32 -0700272 return td_root_tg(td);
Vivek Goyale43473b2010-09-15 17:06:35 -0400273
Tejun Heoe8989fa2012-03-05 13:15:20 -0800274 return blkg_to_tg(blkg_lookup(blkcg, td->queue));
Vivek Goyale43473b2010-09-15 17:06:35 -0400275}
276
Tejun Heocd1604f2012-03-05 13:15:06 -0800277static struct throtl_grp *throtl_lookup_create_tg(struct throtl_data *td,
Tejun Heo3c798392012-04-16 13:57:25 -0700278 struct blkcg *blkcg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400279{
Vivek Goyalf469a7b2011-05-19 15:38:23 -0400280 struct request_queue *q = td->queue;
Tejun Heocd1604f2012-03-05 13:15:06 -0800281 struct throtl_grp *tg = NULL;
Tejun Heo0a5a7d02012-03-05 13:15:02 -0800282
Vivek Goyalf469a7b2011-05-19 15:38:23 -0400283 /*
Tejun Heo3c798392012-04-16 13:57:25 -0700284 * This is the common case when there are no blkcgs. Avoid lookup
285 * in this case
Vivek Goyalf469a7b2011-05-19 15:38:23 -0400286 */
Tejun Heo3c798392012-04-16 13:57:25 -0700287 if (blkcg == &blkcg_root) {
Tejun Heo03d8e112012-04-13 13:11:32 -0700288 tg = td_root_tg(td);
Tejun Heocd1604f2012-03-05 13:15:06 -0800289 } else {
Tejun Heo3c798392012-04-16 13:57:25 -0700290 struct blkcg_gq *blkg;
Tejun Heocd1604f2012-03-05 13:15:06 -0800291
Tejun Heo3c96cb32012-04-13 13:11:34 -0700292 blkg = blkg_lookup_create(blkcg, q);
Tejun Heocd1604f2012-03-05 13:15:06 -0800293
294 /* if %NULL and @q is alive, fall back to root_tg */
295 if (!IS_ERR(blkg))
Tejun Heo03814112012-03-05 13:15:14 -0800296 tg = blkg_to_tg(blkg);
Bart Van Assche3f3299d2012-11-28 13:42:38 +0100297 else if (!blk_queue_dying(q))
Tejun Heo03d8e112012-04-13 13:11:32 -0700298 tg = td_root_tg(td);
Vivek Goyalf469a7b2011-05-19 15:38:23 -0400299 }
300
Vivek Goyale43473b2010-09-15 17:06:35 -0400301 return tg;
302}
303
304static struct throtl_grp *throtl_rb_first(struct throtl_rb_root *root)
305{
306 /* Service tree is empty */
307 if (!root->count)
308 return NULL;
309
310 if (!root->left)
311 root->left = rb_first(&root->rb);
312
313 if (root->left)
314 return rb_entry_tg(root->left);
315
316 return NULL;
317}
318
319static void rb_erase_init(struct rb_node *n, struct rb_root *root)
320{
321 rb_erase(n, root);
322 RB_CLEAR_NODE(n);
323}
324
325static void throtl_rb_erase(struct rb_node *n, struct throtl_rb_root *root)
326{
327 if (root->left == n)
328 root->left = NULL;
329 rb_erase_init(n, &root->rb);
330 --root->count;
331}
332
333static void update_min_dispatch_time(struct throtl_rb_root *st)
334{
335 struct throtl_grp *tg;
336
337 tg = throtl_rb_first(st);
338 if (!tg)
339 return;
340
341 st->min_disptime = tg->disptime;
342}
343
344static void
345tg_service_tree_add(struct throtl_rb_root *st, struct throtl_grp *tg)
346{
347 struct rb_node **node = &st->rb.rb_node;
348 struct rb_node *parent = NULL;
349 struct throtl_grp *__tg;
350 unsigned long key = tg->disptime;
351 int left = 1;
352
353 while (*node != NULL) {
354 parent = *node;
355 __tg = rb_entry_tg(parent);
356
357 if (time_before(key, __tg->disptime))
358 node = &parent->rb_left;
359 else {
360 node = &parent->rb_right;
361 left = 0;
362 }
363 }
364
365 if (left)
366 st->left = &tg->rb_node;
367
368 rb_link_node(&tg->rb_node, parent, node);
369 rb_insert_color(&tg->rb_node, &st->rb);
370}
371
372static void __throtl_enqueue_tg(struct throtl_data *td, struct throtl_grp *tg)
373{
374 struct throtl_rb_root *st = &td->tg_service_tree;
375
376 tg_service_tree_add(st, tg);
377 throtl_mark_tg_on_rr(tg);
378 st->count++;
379}
380
381static void throtl_enqueue_tg(struct throtl_data *td, struct throtl_grp *tg)
382{
383 if (!throtl_tg_on_rr(tg))
384 __throtl_enqueue_tg(td, tg);
385}
386
387static void __throtl_dequeue_tg(struct throtl_data *td, struct throtl_grp *tg)
388{
389 throtl_rb_erase(&tg->rb_node, &td->tg_service_tree);
390 throtl_clear_tg_on_rr(tg);
391}
392
393static void throtl_dequeue_tg(struct throtl_data *td, struct throtl_grp *tg)
394{
395 if (throtl_tg_on_rr(tg))
396 __throtl_dequeue_tg(td, tg);
397}
398
Tejun Heoa9131a22013-05-14 13:52:31 -0700399/* Call with queue lock held */
400static void throtl_schedule_delayed_work(struct throtl_data *td,
401 unsigned long delay)
402{
403 struct delayed_work *dwork = &td->dispatch_work;
404
405 if (total_nr_queued(td)) {
406 mod_delayed_work(kthrotld_workqueue, dwork, delay);
407 throtl_log(td, "schedule work. delay=%lu jiffies=%lu",
408 delay, jiffies);
409 }
410}
411
Vivek Goyale43473b2010-09-15 17:06:35 -0400412static void throtl_schedule_next_dispatch(struct throtl_data *td)
413{
414 struct throtl_rb_root *st = &td->tg_service_tree;
415
416 /*
417 * If there are more bios pending, schedule more work.
418 */
419 if (!total_nr_queued(td))
420 return;
421
422 BUG_ON(!st->count);
423
424 update_min_dispatch_time(st);
425
426 if (time_before_eq(st->min_disptime, jiffies))
Vivek Goyal450adcb2011-03-01 13:40:54 -0500427 throtl_schedule_delayed_work(td, 0);
Vivek Goyale43473b2010-09-15 17:06:35 -0400428 else
Vivek Goyal450adcb2011-03-01 13:40:54 -0500429 throtl_schedule_delayed_work(td, (st->min_disptime - jiffies));
Vivek Goyale43473b2010-09-15 17:06:35 -0400430}
431
432static inline void
433throtl_start_new_slice(struct throtl_data *td, struct throtl_grp *tg, bool rw)
434{
435 tg->bytes_disp[rw] = 0;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400436 tg->io_disp[rw] = 0;
Vivek Goyale43473b2010-09-15 17:06:35 -0400437 tg->slice_start[rw] = jiffies;
438 tg->slice_end[rw] = jiffies + throtl_slice;
439 throtl_log_tg(td, tg, "[%c] new slice start=%lu end=%lu jiffies=%lu",
440 rw == READ ? 'R' : 'W', tg->slice_start[rw],
441 tg->slice_end[rw], jiffies);
442}
443
Vivek Goyald1ae8ff2010-12-01 19:34:46 +0100444static inline void throtl_set_slice_end(struct throtl_data *td,
445 struct throtl_grp *tg, bool rw, unsigned long jiffy_end)
446{
447 tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
448}
449
Vivek Goyale43473b2010-09-15 17:06:35 -0400450static inline void throtl_extend_slice(struct throtl_data *td,
451 struct throtl_grp *tg, bool rw, unsigned long jiffy_end)
452{
453 tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
454 throtl_log_tg(td, tg, "[%c] extend slice start=%lu end=%lu jiffies=%lu",
455 rw == READ ? 'R' : 'W', tg->slice_start[rw],
456 tg->slice_end[rw], jiffies);
457}
458
459/* Determine if previously allocated or extended slice is complete or not */
460static bool
461throtl_slice_used(struct throtl_data *td, struct throtl_grp *tg, bool rw)
462{
463 if (time_in_range(jiffies, tg->slice_start[rw], tg->slice_end[rw]))
464 return 0;
465
466 return 1;
467}
468
469/* Trim the used slices and adjust slice start accordingly */
470static inline void
471throtl_trim_slice(struct throtl_data *td, struct throtl_grp *tg, bool rw)
472{
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200473 unsigned long nr_slices, time_elapsed, io_trim;
474 u64 bytes_trim, tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400475
476 BUG_ON(time_before(tg->slice_end[rw], tg->slice_start[rw]));
477
478 /*
479 * If bps are unlimited (-1), then time slice don't get
480 * renewed. Don't try to trim the slice if slice is used. A new
481 * slice will start when appropriate.
482 */
483 if (throtl_slice_used(td, tg, rw))
484 return;
485
Vivek Goyald1ae8ff2010-12-01 19:34:46 +0100486 /*
487 * A bio has been dispatched. Also adjust slice_end. It might happen
488 * that initially cgroup limit was very low resulting in high
489 * slice_end, but later limit was bumped up and bio was dispached
490 * sooner, then we need to reduce slice_end. A high bogus slice_end
491 * is bad because it does not allow new slice to start.
492 */
493
494 throtl_set_slice_end(td, tg, rw, jiffies + throtl_slice);
495
Vivek Goyale43473b2010-09-15 17:06:35 -0400496 time_elapsed = jiffies - tg->slice_start[rw];
497
498 nr_slices = time_elapsed / throtl_slice;
499
500 if (!nr_slices)
501 return;
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200502 tmp = tg->bps[rw] * throtl_slice * nr_slices;
503 do_div(tmp, HZ);
504 bytes_trim = tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400505
Vivek Goyal8e89d132010-09-15 17:06:37 -0400506 io_trim = (tg->iops[rw] * throtl_slice * nr_slices)/HZ;
Vivek Goyale43473b2010-09-15 17:06:35 -0400507
Vivek Goyal8e89d132010-09-15 17:06:37 -0400508 if (!bytes_trim && !io_trim)
Vivek Goyale43473b2010-09-15 17:06:35 -0400509 return;
510
511 if (tg->bytes_disp[rw] >= bytes_trim)
512 tg->bytes_disp[rw] -= bytes_trim;
513 else
514 tg->bytes_disp[rw] = 0;
515
Vivek Goyal8e89d132010-09-15 17:06:37 -0400516 if (tg->io_disp[rw] >= io_trim)
517 tg->io_disp[rw] -= io_trim;
518 else
519 tg->io_disp[rw] = 0;
520
Vivek Goyale43473b2010-09-15 17:06:35 -0400521 tg->slice_start[rw] += nr_slices * throtl_slice;
522
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200523 throtl_log_tg(td, tg, "[%c] trim slice nr=%lu bytes=%llu io=%lu"
Vivek Goyale43473b2010-09-15 17:06:35 -0400524 " start=%lu end=%lu jiffies=%lu",
Vivek Goyal8e89d132010-09-15 17:06:37 -0400525 rw == READ ? 'R' : 'W', nr_slices, bytes_trim, io_trim,
Vivek Goyale43473b2010-09-15 17:06:35 -0400526 tg->slice_start[rw], tg->slice_end[rw], jiffies);
527}
528
Vivek Goyal8e89d132010-09-15 17:06:37 -0400529static bool tg_with_in_iops_limit(struct throtl_data *td, struct throtl_grp *tg,
530 struct bio *bio, unsigned long *wait)
Vivek Goyale43473b2010-09-15 17:06:35 -0400531{
532 bool rw = bio_data_dir(bio);
Vivek Goyal8e89d132010-09-15 17:06:37 -0400533 unsigned int io_allowed;
Vivek Goyale43473b2010-09-15 17:06:35 -0400534 unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
Vivek Goyalc49c06e2010-10-01 21:16:42 +0200535 u64 tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400536
Vivek Goyal8e89d132010-09-15 17:06:37 -0400537 jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
Vivek Goyale43473b2010-09-15 17:06:35 -0400538
Vivek Goyal8e89d132010-09-15 17:06:37 -0400539 /* Slice has just started. Consider one slice interval */
540 if (!jiffy_elapsed)
541 jiffy_elapsed_rnd = throtl_slice;
542
543 jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
544
Vivek Goyalc49c06e2010-10-01 21:16:42 +0200545 /*
546 * jiffy_elapsed_rnd should not be a big value as minimum iops can be
547 * 1 then at max jiffy elapsed should be equivalent of 1 second as we
548 * will allow dispatch after 1 second and after that slice should
549 * have been trimmed.
550 */
551
552 tmp = (u64)tg->iops[rw] * jiffy_elapsed_rnd;
553 do_div(tmp, HZ);
554
555 if (tmp > UINT_MAX)
556 io_allowed = UINT_MAX;
557 else
558 io_allowed = tmp;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400559
560 if (tg->io_disp[rw] + 1 <= io_allowed) {
Vivek Goyale43473b2010-09-15 17:06:35 -0400561 if (wait)
562 *wait = 0;
563 return 1;
564 }
565
Vivek Goyal8e89d132010-09-15 17:06:37 -0400566 /* Calc approx time to dispatch */
567 jiffy_wait = ((tg->io_disp[rw] + 1) * HZ)/tg->iops[rw] + 1;
568
569 if (jiffy_wait > jiffy_elapsed)
570 jiffy_wait = jiffy_wait - jiffy_elapsed;
571 else
572 jiffy_wait = 1;
573
574 if (wait)
575 *wait = jiffy_wait;
576 return 0;
577}
578
579static bool tg_with_in_bps_limit(struct throtl_data *td, struct throtl_grp *tg,
580 struct bio *bio, unsigned long *wait)
581{
582 bool rw = bio_data_dir(bio);
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200583 u64 bytes_allowed, extra_bytes, tmp;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400584 unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
Vivek Goyale43473b2010-09-15 17:06:35 -0400585
586 jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
587
588 /* Slice has just started. Consider one slice interval */
589 if (!jiffy_elapsed)
590 jiffy_elapsed_rnd = throtl_slice;
591
592 jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
593
Vivek Goyal5e901a22010-10-01 21:16:38 +0200594 tmp = tg->bps[rw] * jiffy_elapsed_rnd;
595 do_div(tmp, HZ);
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200596 bytes_allowed = tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400597
598 if (tg->bytes_disp[rw] + bio->bi_size <= bytes_allowed) {
599 if (wait)
600 *wait = 0;
601 return 1;
602 }
603
604 /* Calc approx time to dispatch */
605 extra_bytes = tg->bytes_disp[rw] + bio->bi_size - bytes_allowed;
606 jiffy_wait = div64_u64(extra_bytes * HZ, tg->bps[rw]);
607
608 if (!jiffy_wait)
609 jiffy_wait = 1;
610
611 /*
612 * This wait time is without taking into consideration the rounding
613 * up we did. Add that time also.
614 */
615 jiffy_wait = jiffy_wait + (jiffy_elapsed_rnd - jiffy_elapsed);
Vivek Goyale43473b2010-09-15 17:06:35 -0400616 if (wait)
617 *wait = jiffy_wait;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400618 return 0;
619}
Vivek Goyale43473b2010-09-15 17:06:35 -0400620
Vivek Goyalaf75cd32011-05-19 15:38:31 -0400621static bool tg_no_rule_group(struct throtl_grp *tg, bool rw) {
622 if (tg->bps[rw] == -1 && tg->iops[rw] == -1)
623 return 1;
624 return 0;
625}
626
Vivek Goyal8e89d132010-09-15 17:06:37 -0400627/*
628 * Returns whether one can dispatch a bio or not. Also returns approx number
629 * of jiffies to wait before this bio is with-in IO rate and can be dispatched
630 */
631static bool tg_may_dispatch(struct throtl_data *td, struct throtl_grp *tg,
632 struct bio *bio, unsigned long *wait)
633{
634 bool rw = bio_data_dir(bio);
635 unsigned long bps_wait = 0, iops_wait = 0, max_wait = 0;
636
637 /*
638 * Currently whole state machine of group depends on first bio
639 * queued in the group bio list. So one should not be calling
640 * this function with a different bio if there are other bios
641 * queued.
642 */
643 BUG_ON(tg->nr_queued[rw] && bio != bio_list_peek(&tg->bio_lists[rw]));
644
645 /* If tg->bps = -1, then BW is unlimited */
646 if (tg->bps[rw] == -1 && tg->iops[rw] == -1) {
647 if (wait)
648 *wait = 0;
649 return 1;
650 }
651
652 /*
653 * If previous slice expired, start a new one otherwise renew/extend
654 * existing slice to make sure it is at least throtl_slice interval
655 * long since now.
656 */
657 if (throtl_slice_used(td, tg, rw))
658 throtl_start_new_slice(td, tg, rw);
659 else {
660 if (time_before(tg->slice_end[rw], jiffies + throtl_slice))
661 throtl_extend_slice(td, tg, rw, jiffies + throtl_slice);
662 }
663
664 if (tg_with_in_bps_limit(td, tg, bio, &bps_wait)
665 && tg_with_in_iops_limit(td, tg, bio, &iops_wait)) {
666 if (wait)
667 *wait = 0;
668 return 1;
669 }
670
671 max_wait = max(bps_wait, iops_wait);
672
673 if (wait)
674 *wait = max_wait;
675
676 if (time_before(tg->slice_end[rw], jiffies + max_wait))
677 throtl_extend_slice(td, tg, rw, jiffies + max_wait);
Vivek Goyale43473b2010-09-15 17:06:35 -0400678
679 return 0;
680}
681
Tejun Heo3c798392012-04-16 13:57:25 -0700682static void throtl_update_dispatch_stats(struct blkcg_gq *blkg, u64 bytes,
Tejun Heo629ed0b2012-04-01 14:38:44 -0700683 int rw)
684{
Tejun Heo8a3d2612012-04-01 14:38:44 -0700685 struct throtl_grp *tg = blkg_to_tg(blkg);
686 struct tg_stats_cpu *stats_cpu;
Tejun Heo629ed0b2012-04-01 14:38:44 -0700687 unsigned long flags;
688
689 /* If per cpu stats are not allocated yet, don't do any accounting. */
Tejun Heo8a3d2612012-04-01 14:38:44 -0700690 if (tg->stats_cpu == NULL)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700691 return;
692
693 /*
694 * Disabling interrupts to provide mutual exclusion between two
695 * writes on same cpu. It probably is not needed for 64bit. Not
696 * optimizing that case yet.
697 */
698 local_irq_save(flags);
699
Tejun Heo8a3d2612012-04-01 14:38:44 -0700700 stats_cpu = this_cpu_ptr(tg->stats_cpu);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700701
Tejun Heo629ed0b2012-04-01 14:38:44 -0700702 blkg_rwstat_add(&stats_cpu->serviced, rw, 1);
703 blkg_rwstat_add(&stats_cpu->service_bytes, rw, bytes);
704
705 local_irq_restore(flags);
706}
707
Vivek Goyale43473b2010-09-15 17:06:35 -0400708static void throtl_charge_bio(struct throtl_grp *tg, struct bio *bio)
709{
710 bool rw = bio_data_dir(bio);
Vivek Goyale43473b2010-09-15 17:06:35 -0400711
712 /* Charge the bio to the group */
713 tg->bytes_disp[rw] += bio->bi_size;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400714 tg->io_disp[rw]++;
Vivek Goyale43473b2010-09-15 17:06:35 -0400715
Tejun Heo629ed0b2012-04-01 14:38:44 -0700716 throtl_update_dispatch_stats(tg_to_blkg(tg), bio->bi_size, bio->bi_rw);
Vivek Goyale43473b2010-09-15 17:06:35 -0400717}
718
719static void throtl_add_bio_tg(struct throtl_data *td, struct throtl_grp *tg,
720 struct bio *bio)
721{
722 bool rw = bio_data_dir(bio);
723
724 bio_list_add(&tg->bio_lists[rw], bio);
725 /* Take a bio reference on tg */
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800726 blkg_get(tg_to_blkg(tg));
Vivek Goyale43473b2010-09-15 17:06:35 -0400727 tg->nr_queued[rw]++;
728 td->nr_queued[rw]++;
729 throtl_enqueue_tg(td, tg);
730}
731
732static void tg_update_disptime(struct throtl_data *td, struct throtl_grp *tg)
733{
734 unsigned long read_wait = -1, write_wait = -1, min_wait = -1, disptime;
735 struct bio *bio;
736
737 if ((bio = bio_list_peek(&tg->bio_lists[READ])))
738 tg_may_dispatch(td, tg, bio, &read_wait);
739
740 if ((bio = bio_list_peek(&tg->bio_lists[WRITE])))
741 tg_may_dispatch(td, tg, bio, &write_wait);
742
743 min_wait = min(read_wait, write_wait);
744 disptime = jiffies + min_wait;
745
Vivek Goyale43473b2010-09-15 17:06:35 -0400746 /* Update dispatch time */
747 throtl_dequeue_tg(td, tg);
748 tg->disptime = disptime;
749 throtl_enqueue_tg(td, tg);
750}
751
752static void tg_dispatch_one_bio(struct throtl_data *td, struct throtl_grp *tg,
753 bool rw, struct bio_list *bl)
754{
755 struct bio *bio;
756
757 bio = bio_list_pop(&tg->bio_lists[rw]);
758 tg->nr_queued[rw]--;
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800759 /* Drop bio reference on blkg */
760 blkg_put(tg_to_blkg(tg));
Vivek Goyale43473b2010-09-15 17:06:35 -0400761
762 BUG_ON(td->nr_queued[rw] <= 0);
763 td->nr_queued[rw]--;
764
765 throtl_charge_bio(tg, bio);
766 bio_list_add(bl, bio);
767 bio->bi_rw |= REQ_THROTTLED;
768
769 throtl_trim_slice(td, tg, rw);
770}
771
772static int throtl_dispatch_tg(struct throtl_data *td, struct throtl_grp *tg,
773 struct bio_list *bl)
774{
775 unsigned int nr_reads = 0, nr_writes = 0;
776 unsigned int max_nr_reads = throtl_grp_quantum*3/4;
Vivek Goyalc2f68052010-11-15 19:32:42 +0100777 unsigned int max_nr_writes = throtl_grp_quantum - max_nr_reads;
Vivek Goyale43473b2010-09-15 17:06:35 -0400778 struct bio *bio;
779
780 /* Try to dispatch 75% READS and 25% WRITES */
781
782 while ((bio = bio_list_peek(&tg->bio_lists[READ]))
783 && tg_may_dispatch(td, tg, bio, NULL)) {
784
785 tg_dispatch_one_bio(td, tg, bio_data_dir(bio), bl);
786 nr_reads++;
787
788 if (nr_reads >= max_nr_reads)
789 break;
790 }
791
792 while ((bio = bio_list_peek(&tg->bio_lists[WRITE]))
793 && tg_may_dispatch(td, tg, bio, NULL)) {
794
795 tg_dispatch_one_bio(td, tg, bio_data_dir(bio), bl);
796 nr_writes++;
797
798 if (nr_writes >= max_nr_writes)
799 break;
800 }
801
802 return nr_reads + nr_writes;
803}
804
805static int throtl_select_dispatch(struct throtl_data *td, struct bio_list *bl)
806{
807 unsigned int nr_disp = 0;
808 struct throtl_grp *tg;
809 struct throtl_rb_root *st = &td->tg_service_tree;
810
811 while (1) {
812 tg = throtl_rb_first(st);
813
814 if (!tg)
815 break;
816
817 if (time_before(jiffies, tg->disptime))
818 break;
819
820 throtl_dequeue_tg(td, tg);
821
822 nr_disp += throtl_dispatch_tg(td, tg, bl);
823
Tejun Heo2db63142013-05-14 13:52:31 -0700824 if (tg->nr_queued[0] || tg->nr_queued[1])
Vivek Goyale43473b2010-09-15 17:06:35 -0400825 tg_update_disptime(td, tg);
Vivek Goyale43473b2010-09-15 17:06:35 -0400826
827 if (nr_disp >= throtl_quantum)
828 break;
829 }
830
831 return nr_disp;
832}
833
Tejun Heocb761992013-05-14 13:52:31 -0700834/* work function to dispatch throttled bios */
835void blk_throtl_dispatch_work_fn(struct work_struct *work)
Vivek Goyale43473b2010-09-15 17:06:35 -0400836{
Tejun Heocb761992013-05-14 13:52:31 -0700837 struct throtl_data *td = container_of(to_delayed_work(work),
838 struct throtl_data, dispatch_work);
839 struct request_queue *q = td->queue;
Vivek Goyale43473b2010-09-15 17:06:35 -0400840 unsigned int nr_disp = 0;
841 struct bio_list bio_list_on_stack;
842 struct bio *bio;
Vivek Goyal69d60eb2011-03-09 08:27:37 +0100843 struct blk_plug plug;
Vivek Goyale43473b2010-09-15 17:06:35 -0400844
845 spin_lock_irq(q->queue_lock);
846
847 if (!total_nr_queued(td))
848 goto out;
849
850 bio_list_init(&bio_list_on_stack);
851
Joe Perchesd2f31a52011-06-13 20:19:27 +0200852 throtl_log(td, "dispatch nr_queued=%u read=%u write=%u",
Vivek Goyale43473b2010-09-15 17:06:35 -0400853 total_nr_queued(td), td->nr_queued[READ],
854 td->nr_queued[WRITE]);
855
856 nr_disp = throtl_select_dispatch(td, &bio_list_on_stack);
857
858 if (nr_disp)
859 throtl_log(td, "bios disp=%u", nr_disp);
860
861 throtl_schedule_next_dispatch(td);
862out:
863 spin_unlock_irq(q->queue_lock);
864
865 /*
866 * If we dispatched some requests, unplug the queue to make sure
867 * immediate dispatch
868 */
869 if (nr_disp) {
Vivek Goyal69d60eb2011-03-09 08:27:37 +0100870 blk_start_plug(&plug);
Vivek Goyale43473b2010-09-15 17:06:35 -0400871 while((bio = bio_list_pop(&bio_list_on_stack)))
872 generic_make_request(bio);
Vivek Goyal69d60eb2011-03-09 08:27:37 +0100873 blk_finish_plug(&plug);
Vivek Goyale43473b2010-09-15 17:06:35 -0400874 }
Vivek Goyale43473b2010-09-15 17:06:35 -0400875}
876
Tejun Heof95a04a2012-04-16 13:57:26 -0700877static u64 tg_prfill_cpu_rwstat(struct seq_file *sf,
878 struct blkg_policy_data *pd, int off)
Tejun Heo41b38b62012-04-01 14:38:44 -0700879{
Tejun Heof95a04a2012-04-16 13:57:26 -0700880 struct throtl_grp *tg = pd_to_tg(pd);
Tejun Heo41b38b62012-04-01 14:38:44 -0700881 struct blkg_rwstat rwstat = { }, tmp;
882 int i, cpu;
883
884 for_each_possible_cpu(cpu) {
Tejun Heo8a3d2612012-04-01 14:38:44 -0700885 struct tg_stats_cpu *sc = per_cpu_ptr(tg->stats_cpu, cpu);
Tejun Heo41b38b62012-04-01 14:38:44 -0700886
887 tmp = blkg_rwstat_read((void *)sc + off);
888 for (i = 0; i < BLKG_RWSTAT_NR; i++)
889 rwstat.cnt[i] += tmp.cnt[i];
890 }
891
Tejun Heof95a04a2012-04-16 13:57:26 -0700892 return __blkg_prfill_rwstat(sf, pd, &rwstat);
Tejun Heo41b38b62012-04-01 14:38:44 -0700893}
894
Tejun Heo8a3d2612012-04-01 14:38:44 -0700895static int tg_print_cpu_rwstat(struct cgroup *cgrp, struct cftype *cft,
896 struct seq_file *sf)
Tejun Heo41b38b62012-04-01 14:38:44 -0700897{
Tejun Heo3c798392012-04-16 13:57:25 -0700898 struct blkcg *blkcg = cgroup_to_blkcg(cgrp);
Tejun Heo41b38b62012-04-01 14:38:44 -0700899
Tejun Heo3c798392012-04-16 13:57:25 -0700900 blkcg_print_blkgs(sf, blkcg, tg_prfill_cpu_rwstat, &blkcg_policy_throtl,
Tejun Heo5bc4afb12012-04-01 14:38:45 -0700901 cft->private, true);
Tejun Heo41b38b62012-04-01 14:38:44 -0700902 return 0;
903}
904
Tejun Heof95a04a2012-04-16 13:57:26 -0700905static u64 tg_prfill_conf_u64(struct seq_file *sf, struct blkg_policy_data *pd,
906 int off)
Tejun Heo60c2bc22012-04-01 14:38:43 -0700907{
Tejun Heof95a04a2012-04-16 13:57:26 -0700908 struct throtl_grp *tg = pd_to_tg(pd);
909 u64 v = *(u64 *)((void *)tg + off);
Tejun Heo60c2bc22012-04-01 14:38:43 -0700910
Tejun Heoaf133ce2012-04-01 14:38:44 -0700911 if (v == -1)
Tejun Heo60c2bc22012-04-01 14:38:43 -0700912 return 0;
Tejun Heof95a04a2012-04-16 13:57:26 -0700913 return __blkg_prfill_u64(sf, pd, v);
Tejun Heo60c2bc22012-04-01 14:38:43 -0700914}
915
Tejun Heof95a04a2012-04-16 13:57:26 -0700916static u64 tg_prfill_conf_uint(struct seq_file *sf, struct blkg_policy_data *pd,
917 int off)
Tejun Heo60c2bc22012-04-01 14:38:43 -0700918{
Tejun Heof95a04a2012-04-16 13:57:26 -0700919 struct throtl_grp *tg = pd_to_tg(pd);
920 unsigned int v = *(unsigned int *)((void *)tg + off);
Tejun Heoaf133ce2012-04-01 14:38:44 -0700921
922 if (v == -1)
923 return 0;
Tejun Heof95a04a2012-04-16 13:57:26 -0700924 return __blkg_prfill_u64(sf, pd, v);
Tejun Heoaf133ce2012-04-01 14:38:44 -0700925}
926
927static int tg_print_conf_u64(struct cgroup *cgrp, struct cftype *cft,
928 struct seq_file *sf)
929{
Tejun Heo3c798392012-04-16 13:57:25 -0700930 blkcg_print_blkgs(sf, cgroup_to_blkcg(cgrp), tg_prfill_conf_u64,
931 &blkcg_policy_throtl, cft->private, false);
Tejun Heo60c2bc22012-04-01 14:38:43 -0700932 return 0;
933}
934
Tejun Heoaf133ce2012-04-01 14:38:44 -0700935static int tg_print_conf_uint(struct cgroup *cgrp, struct cftype *cft,
936 struct seq_file *sf)
Vivek Goyale43473b2010-09-15 17:06:35 -0400937{
Tejun Heo3c798392012-04-16 13:57:25 -0700938 blkcg_print_blkgs(sf, cgroup_to_blkcg(cgrp), tg_prfill_conf_uint,
939 &blkcg_policy_throtl, cft->private, false);
Tejun Heoaf133ce2012-04-01 14:38:44 -0700940 return 0;
Vivek Goyale43473b2010-09-15 17:06:35 -0400941}
942
Tejun Heoaf133ce2012-04-01 14:38:44 -0700943static int tg_set_conf(struct cgroup *cgrp, struct cftype *cft, const char *buf,
944 bool is_u64)
Tejun Heo60c2bc22012-04-01 14:38:43 -0700945{
Tejun Heo3c798392012-04-16 13:57:25 -0700946 struct blkcg *blkcg = cgroup_to_blkcg(cgrp);
Tejun Heo60c2bc22012-04-01 14:38:43 -0700947 struct blkg_conf_ctx ctx;
Tejun Heoaf133ce2012-04-01 14:38:44 -0700948 struct throtl_grp *tg;
Tejun Heoa2b16932012-04-13 13:11:33 -0700949 struct throtl_data *td;
Tejun Heo60c2bc22012-04-01 14:38:43 -0700950 int ret;
951
Tejun Heo3c798392012-04-16 13:57:25 -0700952 ret = blkg_conf_prep(blkcg, &blkcg_policy_throtl, buf, &ctx);
Tejun Heo60c2bc22012-04-01 14:38:43 -0700953 if (ret)
954 return ret;
955
Tejun Heoaf133ce2012-04-01 14:38:44 -0700956 tg = blkg_to_tg(ctx.blkg);
Tejun Heoa2b16932012-04-13 13:11:33 -0700957 td = ctx.blkg->q->td;
Tejun Heoaf133ce2012-04-01 14:38:44 -0700958
Tejun Heoa2b16932012-04-13 13:11:33 -0700959 if (!ctx.v)
960 ctx.v = -1;
Tejun Heoaf133ce2012-04-01 14:38:44 -0700961
Tejun Heoa2b16932012-04-13 13:11:33 -0700962 if (is_u64)
963 *(u64 *)((void *)tg + cft->private) = ctx.v;
964 else
965 *(unsigned int *)((void *)tg + cft->private) = ctx.v;
Tejun Heoaf133ce2012-04-01 14:38:44 -0700966
Tejun Heo632b4492013-05-14 13:52:31 -0700967 throtl_log_tg(td, tg, "limit change rbps=%llu wbps=%llu riops=%u wiops=%u",
968 tg->bps[READ], tg->bps[WRITE],
969 tg->iops[READ], tg->iops[WRITE]);
970
971 /*
972 * We're already holding queue_lock and know @tg is valid. Let's
973 * apply the new config directly.
974 *
975 * Restart the slices for both READ and WRITES. It might happen
976 * that a group's limit are dropped suddenly and we don't want to
977 * account recently dispatched IO with new low rate.
978 */
979 throtl_start_new_slice(td, tg, 0);
980 throtl_start_new_slice(td, tg, 1);
981
982 if (throtl_tg_on_rr(tg)) {
983 tg_update_disptime(td, tg);
984 throtl_schedule_next_dispatch(td);
985 }
Tejun Heo60c2bc22012-04-01 14:38:43 -0700986
987 blkg_conf_finish(&ctx);
Tejun Heoa2b16932012-04-13 13:11:33 -0700988 return 0;
Tejun Heo60c2bc22012-04-01 14:38:43 -0700989}
990
Tejun Heoaf133ce2012-04-01 14:38:44 -0700991static int tg_set_conf_u64(struct cgroup *cgrp, struct cftype *cft,
992 const char *buf)
Tejun Heo60c2bc22012-04-01 14:38:43 -0700993{
Tejun Heoaf133ce2012-04-01 14:38:44 -0700994 return tg_set_conf(cgrp, cft, buf, true);
Tejun Heo60c2bc22012-04-01 14:38:43 -0700995}
996
Tejun Heoaf133ce2012-04-01 14:38:44 -0700997static int tg_set_conf_uint(struct cgroup *cgrp, struct cftype *cft,
998 const char *buf)
Tejun Heo60c2bc22012-04-01 14:38:43 -0700999{
Tejun Heoaf133ce2012-04-01 14:38:44 -07001000 return tg_set_conf(cgrp, cft, buf, false);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001001}
1002
1003static struct cftype throtl_files[] = {
1004 {
1005 .name = "throttle.read_bps_device",
Tejun Heoaf133ce2012-04-01 14:38:44 -07001006 .private = offsetof(struct throtl_grp, bps[READ]),
1007 .read_seq_string = tg_print_conf_u64,
1008 .write_string = tg_set_conf_u64,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001009 .max_write_len = 256,
1010 },
1011 {
1012 .name = "throttle.write_bps_device",
Tejun Heoaf133ce2012-04-01 14:38:44 -07001013 .private = offsetof(struct throtl_grp, bps[WRITE]),
1014 .read_seq_string = tg_print_conf_u64,
1015 .write_string = tg_set_conf_u64,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001016 .max_write_len = 256,
1017 },
1018 {
1019 .name = "throttle.read_iops_device",
Tejun Heoaf133ce2012-04-01 14:38:44 -07001020 .private = offsetof(struct throtl_grp, iops[READ]),
1021 .read_seq_string = tg_print_conf_uint,
1022 .write_string = tg_set_conf_uint,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001023 .max_write_len = 256,
1024 },
1025 {
1026 .name = "throttle.write_iops_device",
Tejun Heoaf133ce2012-04-01 14:38:44 -07001027 .private = offsetof(struct throtl_grp, iops[WRITE]),
1028 .read_seq_string = tg_print_conf_uint,
1029 .write_string = tg_set_conf_uint,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001030 .max_write_len = 256,
1031 },
1032 {
1033 .name = "throttle.io_service_bytes",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001034 .private = offsetof(struct tg_stats_cpu, service_bytes),
Tejun Heo8a3d2612012-04-01 14:38:44 -07001035 .read_seq_string = tg_print_cpu_rwstat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001036 },
1037 {
1038 .name = "throttle.io_serviced",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001039 .private = offsetof(struct tg_stats_cpu, serviced),
Tejun Heo8a3d2612012-04-01 14:38:44 -07001040 .read_seq_string = tg_print_cpu_rwstat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001041 },
1042 { } /* terminate */
1043};
1044
Vivek Goyalda527772011-03-02 19:05:33 -05001045static void throtl_shutdown_wq(struct request_queue *q)
Vivek Goyale43473b2010-09-15 17:06:35 -04001046{
1047 struct throtl_data *td = q->td;
1048
Tejun Heocb761992013-05-14 13:52:31 -07001049 cancel_delayed_work_sync(&td->dispatch_work);
Vivek Goyale43473b2010-09-15 17:06:35 -04001050}
1051
Tejun Heo3c798392012-04-16 13:57:25 -07001052static struct blkcg_policy blkcg_policy_throtl = {
Tejun Heof9fcc2d2012-04-16 13:57:27 -07001053 .pd_size = sizeof(struct throtl_grp),
1054 .cftypes = throtl_files,
1055
1056 .pd_init_fn = throtl_pd_init,
1057 .pd_exit_fn = throtl_pd_exit,
1058 .pd_reset_stats_fn = throtl_pd_reset_stats,
Vivek Goyale43473b2010-09-15 17:06:35 -04001059};
1060
Tejun Heobc16a4f2011-10-19 14:33:01 +02001061bool blk_throtl_bio(struct request_queue *q, struct bio *bio)
Vivek Goyale43473b2010-09-15 17:06:35 -04001062{
1063 struct throtl_data *td = q->td;
1064 struct throtl_grp *tg;
Vivek Goyale43473b2010-09-15 17:06:35 -04001065 bool rw = bio_data_dir(bio), update_disptime = true;
Tejun Heo3c798392012-04-16 13:57:25 -07001066 struct blkcg *blkcg;
Tejun Heobc16a4f2011-10-19 14:33:01 +02001067 bool throttled = false;
Vivek Goyale43473b2010-09-15 17:06:35 -04001068
1069 if (bio->bi_rw & REQ_THROTTLED) {
1070 bio->bi_rw &= ~REQ_THROTTLED;
Tejun Heobc16a4f2011-10-19 14:33:01 +02001071 goto out;
Vivek Goyale43473b2010-09-15 17:06:35 -04001072 }
1073
Vivek Goyalaf75cd32011-05-19 15:38:31 -04001074 /*
1075 * A throtl_grp pointer retrieved under rcu can be used to access
1076 * basic fields like stats and io rates. If a group has no rules,
1077 * just update the dispatch stats in lockless manner and return.
1078 */
Vivek Goyalaf75cd32011-05-19 15:38:31 -04001079 rcu_read_lock();
Tejun Heo3c798392012-04-16 13:57:25 -07001080 blkcg = bio_blkcg(bio);
Tejun Heocd1604f2012-03-05 13:15:06 -08001081 tg = throtl_lookup_tg(td, blkcg);
Vivek Goyalaf75cd32011-05-19 15:38:31 -04001082 if (tg) {
Vivek Goyalaf75cd32011-05-19 15:38:31 -04001083 if (tg_no_rule_group(tg, rw)) {
Tejun Heo629ed0b2012-04-01 14:38:44 -07001084 throtl_update_dispatch_stats(tg_to_blkg(tg),
1085 bio->bi_size, bio->bi_rw);
Tejun Heo2a7f1242012-03-05 13:15:01 -08001086 goto out_unlock_rcu;
Vivek Goyalaf75cd32011-05-19 15:38:31 -04001087 }
1088 }
Vivek Goyalaf75cd32011-05-19 15:38:31 -04001089
1090 /*
1091 * Either group has not been allocated yet or it is not an unlimited
1092 * IO group
1093 */
Vivek Goyale43473b2010-09-15 17:06:35 -04001094 spin_lock_irq(q->queue_lock);
Tejun Heocd1604f2012-03-05 13:15:06 -08001095 tg = throtl_lookup_create_tg(td, blkcg);
Tejun Heobc16a4f2011-10-19 14:33:01 +02001096 if (unlikely(!tg))
1097 goto out_unlock;
Vivek Goyalf469a7b2011-05-19 15:38:23 -04001098
Vivek Goyale43473b2010-09-15 17:06:35 -04001099 if (tg->nr_queued[rw]) {
1100 /*
1101 * There is already another bio queued in same dir. No
1102 * need to update dispatch time.
1103 */
Vivek Goyal231d7042011-03-07 21:05:14 +01001104 update_disptime = false;
Vivek Goyale43473b2010-09-15 17:06:35 -04001105 goto queue_bio;
Vivek Goyalde701c72011-03-07 21:09:32 +01001106
Vivek Goyale43473b2010-09-15 17:06:35 -04001107 }
1108
1109 /* Bio is with-in rate limit of group */
1110 if (tg_may_dispatch(td, tg, bio, NULL)) {
1111 throtl_charge_bio(tg, bio);
Vivek Goyal04521db2011-03-22 21:54:29 +01001112
1113 /*
1114 * We need to trim slice even when bios are not being queued
1115 * otherwise it might happen that a bio is not queued for
1116 * a long time and slice keeps on extending and trim is not
1117 * called for a long time. Now if limits are reduced suddenly
1118 * we take into account all the IO dispatched so far at new
1119 * low rate and * newly queued IO gets a really long dispatch
1120 * time.
1121 *
1122 * So keep on trimming slice even if bio is not queued.
1123 */
1124 throtl_trim_slice(td, tg, rw);
Tejun Heobc16a4f2011-10-19 14:33:01 +02001125 goto out_unlock;
Vivek Goyale43473b2010-09-15 17:06:35 -04001126 }
1127
1128queue_bio:
Joe Perchesfd16d262011-06-13 10:42:49 +02001129 throtl_log_tg(td, tg, "[%c] bio. bdisp=%llu sz=%u bps=%llu"
Vivek Goyal8e89d132010-09-15 17:06:37 -04001130 " iodisp=%u iops=%u queued=%d/%d",
1131 rw == READ ? 'R' : 'W',
Vivek Goyale43473b2010-09-15 17:06:35 -04001132 tg->bytes_disp[rw], bio->bi_size, tg->bps[rw],
Vivek Goyal8e89d132010-09-15 17:06:37 -04001133 tg->io_disp[rw], tg->iops[rw],
Vivek Goyale43473b2010-09-15 17:06:35 -04001134 tg->nr_queued[READ], tg->nr_queued[WRITE]);
1135
Tejun Heo671058f2012-03-05 13:15:29 -08001136 bio_associate_current(bio);
Vivek Goyale43473b2010-09-15 17:06:35 -04001137 throtl_add_bio_tg(q->td, tg, bio);
Tejun Heobc16a4f2011-10-19 14:33:01 +02001138 throttled = true;
Vivek Goyale43473b2010-09-15 17:06:35 -04001139
1140 if (update_disptime) {
1141 tg_update_disptime(td, tg);
1142 throtl_schedule_next_dispatch(td);
1143 }
1144
Tejun Heobc16a4f2011-10-19 14:33:01 +02001145out_unlock:
Vivek Goyale43473b2010-09-15 17:06:35 -04001146 spin_unlock_irq(q->queue_lock);
Tejun Heo2a7f1242012-03-05 13:15:01 -08001147out_unlock_rcu:
1148 rcu_read_unlock();
Tejun Heobc16a4f2011-10-19 14:33:01 +02001149out:
1150 return throttled;
Vivek Goyale43473b2010-09-15 17:06:35 -04001151}
1152
Tejun Heoc9a929d2011-10-19 14:42:16 +02001153/**
1154 * blk_throtl_drain - drain throttled bios
1155 * @q: request_queue to drain throttled bios for
1156 *
1157 * Dispatch all currently throttled bios on @q through ->make_request_fn().
1158 */
1159void blk_throtl_drain(struct request_queue *q)
1160 __releases(q->queue_lock) __acquires(q->queue_lock)
1161{
1162 struct throtl_data *td = q->td;
1163 struct throtl_rb_root *st = &td->tg_service_tree;
1164 struct throtl_grp *tg;
1165 struct bio_list bl;
1166 struct bio *bio;
1167
Andi Kleen8bcb6c72012-03-30 12:33:28 +02001168 queue_lockdep_assert_held(q);
Tejun Heoc9a929d2011-10-19 14:42:16 +02001169
1170 bio_list_init(&bl);
1171
1172 while ((tg = throtl_rb_first(st))) {
1173 throtl_dequeue_tg(td, tg);
1174
1175 while ((bio = bio_list_peek(&tg->bio_lists[READ])))
1176 tg_dispatch_one_bio(td, tg, bio_data_dir(bio), &bl);
1177 while ((bio = bio_list_peek(&tg->bio_lists[WRITE])))
1178 tg_dispatch_one_bio(td, tg, bio_data_dir(bio), &bl);
1179 }
1180 spin_unlock_irq(q->queue_lock);
1181
1182 while ((bio = bio_list_pop(&bl)))
1183 generic_make_request(bio);
1184
1185 spin_lock_irq(q->queue_lock);
1186}
1187
Vivek Goyale43473b2010-09-15 17:06:35 -04001188int blk_throtl_init(struct request_queue *q)
1189{
1190 struct throtl_data *td;
Tejun Heoa2b16932012-04-13 13:11:33 -07001191 int ret;
Vivek Goyale43473b2010-09-15 17:06:35 -04001192
1193 td = kzalloc_node(sizeof(*td), GFP_KERNEL, q->node);
1194 if (!td)
1195 return -ENOMEM;
1196
Vivek Goyale43473b2010-09-15 17:06:35 -04001197 td->tg_service_tree = THROTL_RB_ROOT;
Tejun Heocb761992013-05-14 13:52:31 -07001198 INIT_DELAYED_WORK(&td->dispatch_work, blk_throtl_dispatch_work_fn);
Vivek Goyale43473b2010-09-15 17:06:35 -04001199
Tejun Heocd1604f2012-03-05 13:15:06 -08001200 q->td = td;
Vivek Goyal29b12582011-05-19 15:38:24 -04001201 td->queue = q;
Vivek Goyal02977e42010-10-01 14:49:48 +02001202
Tejun Heoa2b16932012-04-13 13:11:33 -07001203 /* activate policy */
Tejun Heo3c798392012-04-16 13:57:25 -07001204 ret = blkcg_activate_policy(q, &blkcg_policy_throtl);
Tejun Heoa2b16932012-04-13 13:11:33 -07001205 if (ret)
Vivek Goyal29b12582011-05-19 15:38:24 -04001206 kfree(td);
Tejun Heoa2b16932012-04-13 13:11:33 -07001207 return ret;
Vivek Goyale43473b2010-09-15 17:06:35 -04001208}
1209
1210void blk_throtl_exit(struct request_queue *q)
1211{
Tejun Heoc875f4d2012-03-05 13:15:22 -08001212 BUG_ON(!q->td);
Vivek Goyalda527772011-03-02 19:05:33 -05001213 throtl_shutdown_wq(q);
Tejun Heo3c798392012-04-16 13:57:25 -07001214 blkcg_deactivate_policy(q, &blkcg_policy_throtl);
Tejun Heoc9a929d2011-10-19 14:42:16 +02001215 kfree(q->td);
Vivek Goyale43473b2010-09-15 17:06:35 -04001216}
1217
1218static int __init throtl_init(void)
1219{
Vivek Goyal450adcb2011-03-01 13:40:54 -05001220 kthrotld_workqueue = alloc_workqueue("kthrotld", WQ_MEM_RECLAIM, 0);
1221 if (!kthrotld_workqueue)
1222 panic("Failed to create kthrotld\n");
1223
Tejun Heo3c798392012-04-16 13:57:25 -07001224 return blkcg_policy_register(&blkcg_policy_throtl);
Vivek Goyale43473b2010-09-15 17:06:35 -04001225}
1226
1227module_init(throtl_init);