blob: 918d222408560dc413c7c47cecf758a10ef5939a [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
Tejun Heoc9e03322013-05-14 13:52:32 -070029struct throtl_service_queue {
Tejun Heo77216b02013-05-14 13:52:36 -070030 struct throtl_service_queue *parent_sq; /* the parent service_queue */
31
Tejun Heo73f0d492013-05-14 13:52:35 -070032 /*
33 * Bios queued directly to this service_queue or dispatched from
34 * children throtl_grp's.
35 */
36 struct bio_list bio_lists[2]; /* queued bios [READ/WRITE] */
37 unsigned int nr_queued[2]; /* number of queued bios */
38
39 /*
40 * RB tree of active children throtl_grp's, which are sorted by
41 * their ->disptime.
42 */
Tejun Heoc9e03322013-05-14 13:52:32 -070043 struct rb_root pending_tree; /* RB tree of active tgs */
44 struct rb_node *first_pending; /* first node in the tree */
45 unsigned int nr_pending; /* # queued in the tree */
46 unsigned long first_pending_disptime; /* disptime of the first tg */
Tejun Heo69df0ab2013-05-14 13:52:36 -070047 struct timer_list pending_timer; /* fires on first_pending_disptime */
Vivek Goyale43473b2010-09-15 17:06:35 -040048};
49
Tejun Heo5b2c16a2013-05-14 13:52:32 -070050enum tg_state_flags {
51 THROTL_TG_PENDING = 1 << 0, /* on parent's pending tree */
Tejun Heo0e9f4162013-05-14 13:52:35 -070052 THROTL_TG_WAS_EMPTY = 1 << 1, /* bio_lists[] became non-empty */
Tejun Heo5b2c16a2013-05-14 13:52:32 -070053};
54
Vivek Goyale43473b2010-09-15 17:06:35 -040055#define rb_entry_tg(node) rb_entry((node), struct throtl_grp, rb_node)
56
Tejun Heo8a3d2612012-04-01 14:38:44 -070057/* Per-cpu group stats */
58struct tg_stats_cpu {
59 /* total bytes transferred */
60 struct blkg_rwstat service_bytes;
61 /* total IOs serviced, post merge */
62 struct blkg_rwstat serviced;
63};
64
Vivek Goyale43473b2010-09-15 17:06:35 -040065struct throtl_grp {
Tejun Heof95a04a2012-04-16 13:57:26 -070066 /* must be the first member */
67 struct blkg_policy_data pd;
68
Tejun Heoc9e03322013-05-14 13:52:32 -070069 /* active throtl group service_queue member */
Vivek Goyale43473b2010-09-15 17:06:35 -040070 struct rb_node rb_node;
71
Tejun Heo0f3457f2013-05-14 13:52:32 -070072 /* throtl_data this group belongs to */
73 struct throtl_data *td;
74
Tejun Heo49a2f1e2013-05-14 13:52:34 -070075 /* this group's service queue */
76 struct throtl_service_queue service_queue;
77
Vivek Goyale43473b2010-09-15 17:06:35 -040078 /*
79 * Dispatch time in jiffies. This is the estimated time when group
80 * will unthrottle and is ready to dispatch more bio. It is used as
81 * key to sort active groups in service tree.
82 */
83 unsigned long disptime;
84
Vivek Goyale43473b2010-09-15 17:06:35 -040085 unsigned int flags;
86
Vivek Goyale43473b2010-09-15 17:06:35 -040087 /* bytes per second rate limits */
88 uint64_t bps[2];
89
Vivek Goyal8e89d132010-09-15 17:06:37 -040090 /* IOPS limits */
91 unsigned int iops[2];
92
Vivek Goyale43473b2010-09-15 17:06:35 -040093 /* Number of bytes disptached in current slice */
94 uint64_t bytes_disp[2];
Vivek Goyal8e89d132010-09-15 17:06:37 -040095 /* Number of bio's dispatched in current slice */
96 unsigned int io_disp[2];
Vivek Goyale43473b2010-09-15 17:06:35 -040097
98 /* When did we start a new slice */
99 unsigned long slice_start[2];
100 unsigned long slice_end[2];
Vivek Goyalfe071432010-10-01 14:49:49 +0200101
Tejun Heo8a3d2612012-04-01 14:38:44 -0700102 /* Per cpu stats pointer */
103 struct tg_stats_cpu __percpu *stats_cpu;
104
105 /* List of tgs waiting for per cpu stats memory to be allocated */
106 struct list_head stats_alloc_node;
Vivek Goyale43473b2010-09-15 17:06:35 -0400107};
108
109struct throtl_data
110{
Vivek Goyale43473b2010-09-15 17:06:35 -0400111 /* service tree for active throtl groups */
Tejun Heoc9e03322013-05-14 13:52:32 -0700112 struct throtl_service_queue service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -0400113
Vivek Goyale43473b2010-09-15 17:06:35 -0400114 struct request_queue *queue;
115
116 /* Total Number of queued bios on READ and WRITE lists */
117 unsigned int nr_queued[2];
118
119 /*
Vivek Goyal02977e42010-10-01 14:49:48 +0200120 * number of total undestroyed groups
Vivek Goyale43473b2010-09-15 17:06:35 -0400121 */
122 unsigned int nr_undestroyed_grps;
123
124 /* Work for dispatching throttled bios */
Tejun Heo69df0ab2013-05-14 13:52:36 -0700125 struct work_struct dispatch_work;
Vivek Goyale43473b2010-09-15 17:06:35 -0400126};
127
Tejun Heo8a3d2612012-04-01 14:38:44 -0700128/* list and work item to allocate percpu group stats */
129static DEFINE_SPINLOCK(tg_stats_alloc_lock);
130static LIST_HEAD(tg_stats_alloc_list);
131
132static void tg_stats_alloc_fn(struct work_struct *);
133static DECLARE_DELAYED_WORK(tg_stats_alloc_work, tg_stats_alloc_fn);
134
Tejun Heo69df0ab2013-05-14 13:52:36 -0700135static void throtl_pending_timer_fn(unsigned long arg);
136
Tejun Heof95a04a2012-04-16 13:57:26 -0700137static inline struct throtl_grp *pd_to_tg(struct blkg_policy_data *pd)
138{
139 return pd ? container_of(pd, struct throtl_grp, pd) : NULL;
140}
141
Tejun Heo3c798392012-04-16 13:57:25 -0700142static inline struct throtl_grp *blkg_to_tg(struct blkcg_gq *blkg)
Tejun Heo03814112012-03-05 13:15:14 -0800143{
Tejun Heof95a04a2012-04-16 13:57:26 -0700144 return pd_to_tg(blkg_to_pd(blkg, &blkcg_policy_throtl));
Tejun Heo03814112012-03-05 13:15:14 -0800145}
146
Tejun Heo3c798392012-04-16 13:57:25 -0700147static inline struct blkcg_gq *tg_to_blkg(struct throtl_grp *tg)
Tejun Heo03814112012-03-05 13:15:14 -0800148{
Tejun Heof95a04a2012-04-16 13:57:26 -0700149 return pd_to_blkg(&tg->pd);
Tejun Heo03814112012-03-05 13:15:14 -0800150}
151
Tejun Heo03d8e112012-04-13 13:11:32 -0700152static inline struct throtl_grp *td_root_tg(struct throtl_data *td)
153{
154 return blkg_to_tg(td->queue->root_blkg);
155}
156
Tejun Heofda6f272013-05-14 13:52:36 -0700157/**
158 * sq_to_tg - return the throl_grp the specified service queue belongs to
159 * @sq: the throtl_service_queue of interest
160 *
161 * Return the throtl_grp @sq belongs to. If @sq is the top-level one
162 * embedded in throtl_data, %NULL is returned.
163 */
164static struct throtl_grp *sq_to_tg(struct throtl_service_queue *sq)
165{
166 if (sq && sq->parent_sq)
167 return container_of(sq, struct throtl_grp, service_queue);
168 else
169 return NULL;
170}
Vivek Goyale43473b2010-09-15 17:06:35 -0400171
Tejun Heofda6f272013-05-14 13:52:36 -0700172/**
173 * sq_to_td - return throtl_data the specified service queue belongs to
174 * @sq: the throtl_service_queue of interest
175 *
176 * A service_queue can be embeded in either a throtl_grp or throtl_data.
177 * Determine the associated throtl_data accordingly and return it.
178 */
179static struct throtl_data *sq_to_td(struct throtl_service_queue *sq)
180{
181 struct throtl_grp *tg = sq_to_tg(sq);
182
183 if (tg)
184 return tg->td;
185 else
186 return container_of(sq, struct throtl_data, service_queue);
187}
188
189/**
190 * throtl_log - log debug message via blktrace
191 * @sq: the service_queue being reported
192 * @fmt: printf format string
193 * @args: printf args
194 *
195 * The messages are prefixed with "throtl BLKG_NAME" if @sq belongs to a
196 * throtl_grp; otherwise, just "throtl".
197 *
198 * TODO: this should be made a function and name formatting should happen
199 * after testing whether blktrace is enabled.
200 */
201#define throtl_log(sq, fmt, args...) do { \
202 struct throtl_grp *__tg = sq_to_tg((sq)); \
203 struct throtl_data *__td = sq_to_td((sq)); \
204 \
205 (void)__td; \
206 if ((__tg)) { \
207 char __pbuf[128]; \
208 \
209 blkg_path(tg_to_blkg(__tg), __pbuf, sizeof(__pbuf)); \
210 blk_add_trace_msg(__td->queue, "throtl %s " fmt, __pbuf, ##args); \
211 } else { \
212 blk_add_trace_msg(__td->queue, "throtl " fmt, ##args); \
213 } \
214} while (0)
Vivek Goyale43473b2010-09-15 17:06:35 -0400215
Tejun Heo8a3d2612012-04-01 14:38:44 -0700216/*
217 * Worker for allocating per cpu stat for tgs. This is scheduled on the
Tejun Heo3b07e9c2012-08-20 14:51:24 -0700218 * system_wq once there are some groups on the alloc_list waiting for
Tejun Heo8a3d2612012-04-01 14:38:44 -0700219 * allocation.
220 */
221static void tg_stats_alloc_fn(struct work_struct *work)
222{
223 static struct tg_stats_cpu *stats_cpu; /* this fn is non-reentrant */
224 struct delayed_work *dwork = to_delayed_work(work);
225 bool empty = false;
226
227alloc_stats:
228 if (!stats_cpu) {
229 stats_cpu = alloc_percpu(struct tg_stats_cpu);
230 if (!stats_cpu) {
231 /* allocation failed, try again after some time */
Tejun Heo3b07e9c2012-08-20 14:51:24 -0700232 schedule_delayed_work(dwork, msecs_to_jiffies(10));
Tejun Heo8a3d2612012-04-01 14:38:44 -0700233 return;
234 }
235 }
236
237 spin_lock_irq(&tg_stats_alloc_lock);
238
239 if (!list_empty(&tg_stats_alloc_list)) {
240 struct throtl_grp *tg = list_first_entry(&tg_stats_alloc_list,
241 struct throtl_grp,
242 stats_alloc_node);
243 swap(tg->stats_cpu, stats_cpu);
244 list_del_init(&tg->stats_alloc_node);
245 }
246
247 empty = list_empty(&tg_stats_alloc_list);
248 spin_unlock_irq(&tg_stats_alloc_lock);
249 if (!empty)
250 goto alloc_stats;
251}
252
Tejun Heo49a2f1e2013-05-14 13:52:34 -0700253/* init a service_queue, assumes the caller zeroed it */
Tejun Heo77216b02013-05-14 13:52:36 -0700254static void throtl_service_queue_init(struct throtl_service_queue *sq,
255 struct throtl_service_queue *parent_sq)
Tejun Heo49a2f1e2013-05-14 13:52:34 -0700256{
Tejun Heo73f0d492013-05-14 13:52:35 -0700257 bio_list_init(&sq->bio_lists[0]);
258 bio_list_init(&sq->bio_lists[1]);
Tejun Heo49a2f1e2013-05-14 13:52:34 -0700259 sq->pending_tree = RB_ROOT;
Tejun Heo77216b02013-05-14 13:52:36 -0700260 sq->parent_sq = parent_sq;
Tejun Heo69df0ab2013-05-14 13:52:36 -0700261 setup_timer(&sq->pending_timer, throtl_pending_timer_fn,
262 (unsigned long)sq);
263}
264
265static void throtl_service_queue_exit(struct throtl_service_queue *sq)
266{
267 del_timer_sync(&sq->pending_timer);
Tejun Heo49a2f1e2013-05-14 13:52:34 -0700268}
269
Tejun Heo3c798392012-04-16 13:57:25 -0700270static void throtl_pd_init(struct blkcg_gq *blkg)
Vivek Goyala29a1712011-05-19 15:38:19 -0400271{
Tejun Heo03814112012-03-05 13:15:14 -0800272 struct throtl_grp *tg = blkg_to_tg(blkg);
Tejun Heo77216b02013-05-14 13:52:36 -0700273 struct throtl_data *td = blkg->q->td;
Tejun Heoff26eaa2012-05-23 12:16:21 +0200274 unsigned long flags;
Tejun Heocd1604f2012-03-05 13:15:06 -0800275
Tejun Heo77216b02013-05-14 13:52:36 -0700276 throtl_service_queue_init(&tg->service_queue, &td->service_queue);
Vivek Goyala29a1712011-05-19 15:38:19 -0400277 RB_CLEAR_NODE(&tg->rb_node);
Tejun Heo77216b02013-05-14 13:52:36 -0700278 tg->td = td;
Vivek Goyala29a1712011-05-19 15:38:19 -0400279
Tejun Heoe56da7e2012-03-05 13:15:07 -0800280 tg->bps[READ] = -1;
281 tg->bps[WRITE] = -1;
282 tg->iops[READ] = -1;
283 tg->iops[WRITE] = -1;
Tejun Heo8a3d2612012-04-01 14:38:44 -0700284
285 /*
286 * Ugh... We need to perform per-cpu allocation for tg->stats_cpu
287 * but percpu allocator can't be called from IO path. Queue tg on
288 * tg_stats_alloc_list and allocate from work item.
289 */
Tejun Heoff26eaa2012-05-23 12:16:21 +0200290 spin_lock_irqsave(&tg_stats_alloc_lock, flags);
Tejun Heo8a3d2612012-04-01 14:38:44 -0700291 list_add(&tg->stats_alloc_node, &tg_stats_alloc_list);
Tejun Heo3b07e9c2012-08-20 14:51:24 -0700292 schedule_delayed_work(&tg_stats_alloc_work, 0);
Tejun Heoff26eaa2012-05-23 12:16:21 +0200293 spin_unlock_irqrestore(&tg_stats_alloc_lock, flags);
Tejun Heo8a3d2612012-04-01 14:38:44 -0700294}
295
Tejun Heo3c798392012-04-16 13:57:25 -0700296static void throtl_pd_exit(struct blkcg_gq *blkg)
Tejun Heo8a3d2612012-04-01 14:38:44 -0700297{
298 struct throtl_grp *tg = blkg_to_tg(blkg);
Tejun Heoff26eaa2012-05-23 12:16:21 +0200299 unsigned long flags;
Tejun Heo8a3d2612012-04-01 14:38:44 -0700300
Tejun Heoff26eaa2012-05-23 12:16:21 +0200301 spin_lock_irqsave(&tg_stats_alloc_lock, flags);
Tejun Heo8a3d2612012-04-01 14:38:44 -0700302 list_del_init(&tg->stats_alloc_node);
Tejun Heoff26eaa2012-05-23 12:16:21 +0200303 spin_unlock_irqrestore(&tg_stats_alloc_lock, flags);
Tejun Heo8a3d2612012-04-01 14:38:44 -0700304
305 free_percpu(tg->stats_cpu);
Tejun Heo69df0ab2013-05-14 13:52:36 -0700306
307 throtl_service_queue_exit(&tg->service_queue);
Tejun Heo8a3d2612012-04-01 14:38:44 -0700308}
309
Tejun Heo3c798392012-04-16 13:57:25 -0700310static void throtl_pd_reset_stats(struct blkcg_gq *blkg)
Tejun Heo8a3d2612012-04-01 14:38:44 -0700311{
312 struct throtl_grp *tg = blkg_to_tg(blkg);
313 int cpu;
314
315 if (tg->stats_cpu == NULL)
316 return;
317
318 for_each_possible_cpu(cpu) {
319 struct tg_stats_cpu *sc = per_cpu_ptr(tg->stats_cpu, cpu);
320
321 blkg_rwstat_reset(&sc->service_bytes);
322 blkg_rwstat_reset(&sc->serviced);
323 }
Vivek Goyala29a1712011-05-19 15:38:19 -0400324}
325
Tejun Heo3c798392012-04-16 13:57:25 -0700326static struct throtl_grp *throtl_lookup_tg(struct throtl_data *td,
327 struct blkcg *blkcg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400328{
Vivek Goyale43473b2010-09-15 17:06:35 -0400329 /*
Tejun Heo3c798392012-04-16 13:57:25 -0700330 * This is the common case when there are no blkcgs. Avoid lookup
331 * in this case
Tejun Heocd1604f2012-03-05 13:15:06 -0800332 */
Tejun Heo3c798392012-04-16 13:57:25 -0700333 if (blkcg == &blkcg_root)
Tejun Heo03d8e112012-04-13 13:11:32 -0700334 return td_root_tg(td);
Vivek Goyale43473b2010-09-15 17:06:35 -0400335
Tejun Heoe8989fa2012-03-05 13:15:20 -0800336 return blkg_to_tg(blkg_lookup(blkcg, td->queue));
Vivek Goyale43473b2010-09-15 17:06:35 -0400337}
338
Tejun Heocd1604f2012-03-05 13:15:06 -0800339static struct throtl_grp *throtl_lookup_create_tg(struct throtl_data *td,
Tejun Heo3c798392012-04-16 13:57:25 -0700340 struct blkcg *blkcg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400341{
Vivek Goyalf469a7b2011-05-19 15:38:23 -0400342 struct request_queue *q = td->queue;
Tejun Heocd1604f2012-03-05 13:15:06 -0800343 struct throtl_grp *tg = NULL;
Tejun Heo0a5a7d02012-03-05 13:15:02 -0800344
Vivek Goyalf469a7b2011-05-19 15:38:23 -0400345 /*
Tejun Heo3c798392012-04-16 13:57:25 -0700346 * This is the common case when there are no blkcgs. Avoid lookup
347 * in this case
Vivek Goyalf469a7b2011-05-19 15:38:23 -0400348 */
Tejun Heo3c798392012-04-16 13:57:25 -0700349 if (blkcg == &blkcg_root) {
Tejun Heo03d8e112012-04-13 13:11:32 -0700350 tg = td_root_tg(td);
Tejun Heocd1604f2012-03-05 13:15:06 -0800351 } else {
Tejun Heo3c798392012-04-16 13:57:25 -0700352 struct blkcg_gq *blkg;
Tejun Heocd1604f2012-03-05 13:15:06 -0800353
Tejun Heo3c96cb32012-04-13 13:11:34 -0700354 blkg = blkg_lookup_create(blkcg, q);
Tejun Heocd1604f2012-03-05 13:15:06 -0800355
356 /* if %NULL and @q is alive, fall back to root_tg */
357 if (!IS_ERR(blkg))
Tejun Heo03814112012-03-05 13:15:14 -0800358 tg = blkg_to_tg(blkg);
Bart Van Assche3f3299d2012-11-28 13:42:38 +0100359 else if (!blk_queue_dying(q))
Tejun Heo03d8e112012-04-13 13:11:32 -0700360 tg = td_root_tg(td);
Vivek Goyalf469a7b2011-05-19 15:38:23 -0400361 }
362
Vivek Goyale43473b2010-09-15 17:06:35 -0400363 return tg;
364}
365
Tejun Heo0049af72013-05-14 13:52:33 -0700366static struct throtl_grp *
367throtl_rb_first(struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -0400368{
369 /* Service tree is empty */
Tejun Heo0049af72013-05-14 13:52:33 -0700370 if (!parent_sq->nr_pending)
Vivek Goyale43473b2010-09-15 17:06:35 -0400371 return NULL;
372
Tejun Heo0049af72013-05-14 13:52:33 -0700373 if (!parent_sq->first_pending)
374 parent_sq->first_pending = rb_first(&parent_sq->pending_tree);
Vivek Goyale43473b2010-09-15 17:06:35 -0400375
Tejun Heo0049af72013-05-14 13:52:33 -0700376 if (parent_sq->first_pending)
377 return rb_entry_tg(parent_sq->first_pending);
Vivek Goyale43473b2010-09-15 17:06:35 -0400378
379 return NULL;
380}
381
382static void rb_erase_init(struct rb_node *n, struct rb_root *root)
383{
384 rb_erase(n, root);
385 RB_CLEAR_NODE(n);
386}
387
Tejun Heo0049af72013-05-14 13:52:33 -0700388static void throtl_rb_erase(struct rb_node *n,
389 struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -0400390{
Tejun Heo0049af72013-05-14 13:52:33 -0700391 if (parent_sq->first_pending == n)
392 parent_sq->first_pending = NULL;
393 rb_erase_init(n, &parent_sq->pending_tree);
394 --parent_sq->nr_pending;
Vivek Goyale43473b2010-09-15 17:06:35 -0400395}
396
Tejun Heo0049af72013-05-14 13:52:33 -0700397static void update_min_dispatch_time(struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -0400398{
399 struct throtl_grp *tg;
400
Tejun Heo0049af72013-05-14 13:52:33 -0700401 tg = throtl_rb_first(parent_sq);
Vivek Goyale43473b2010-09-15 17:06:35 -0400402 if (!tg)
403 return;
404
Tejun Heo0049af72013-05-14 13:52:33 -0700405 parent_sq->first_pending_disptime = tg->disptime;
Vivek Goyale43473b2010-09-15 17:06:35 -0400406}
407
Tejun Heo77216b02013-05-14 13:52:36 -0700408static void tg_service_queue_add(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400409{
Tejun Heo77216b02013-05-14 13:52:36 -0700410 struct throtl_service_queue *parent_sq = tg->service_queue.parent_sq;
Tejun Heo0049af72013-05-14 13:52:33 -0700411 struct rb_node **node = &parent_sq->pending_tree.rb_node;
Vivek Goyale43473b2010-09-15 17:06:35 -0400412 struct rb_node *parent = NULL;
413 struct throtl_grp *__tg;
414 unsigned long key = tg->disptime;
415 int left = 1;
416
417 while (*node != NULL) {
418 parent = *node;
419 __tg = rb_entry_tg(parent);
420
421 if (time_before(key, __tg->disptime))
422 node = &parent->rb_left;
423 else {
424 node = &parent->rb_right;
425 left = 0;
426 }
427 }
428
429 if (left)
Tejun Heo0049af72013-05-14 13:52:33 -0700430 parent_sq->first_pending = &tg->rb_node;
Vivek Goyale43473b2010-09-15 17:06:35 -0400431
432 rb_link_node(&tg->rb_node, parent, node);
Tejun Heo0049af72013-05-14 13:52:33 -0700433 rb_insert_color(&tg->rb_node, &parent_sq->pending_tree);
Vivek Goyale43473b2010-09-15 17:06:35 -0400434}
435
Tejun Heo77216b02013-05-14 13:52:36 -0700436static void __throtl_enqueue_tg(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400437{
Tejun Heo77216b02013-05-14 13:52:36 -0700438 tg_service_queue_add(tg);
Tejun Heo5b2c16a2013-05-14 13:52:32 -0700439 tg->flags |= THROTL_TG_PENDING;
Tejun Heo77216b02013-05-14 13:52:36 -0700440 tg->service_queue.parent_sq->nr_pending++;
Vivek Goyale43473b2010-09-15 17:06:35 -0400441}
442
Tejun Heo77216b02013-05-14 13:52:36 -0700443static void throtl_enqueue_tg(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400444{
Tejun Heo5b2c16a2013-05-14 13:52:32 -0700445 if (!(tg->flags & THROTL_TG_PENDING))
Tejun Heo77216b02013-05-14 13:52:36 -0700446 __throtl_enqueue_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -0400447}
448
Tejun Heo77216b02013-05-14 13:52:36 -0700449static void __throtl_dequeue_tg(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400450{
Tejun Heo77216b02013-05-14 13:52:36 -0700451 throtl_rb_erase(&tg->rb_node, tg->service_queue.parent_sq);
Tejun Heo5b2c16a2013-05-14 13:52:32 -0700452 tg->flags &= ~THROTL_TG_PENDING;
Vivek Goyale43473b2010-09-15 17:06:35 -0400453}
454
Tejun Heo77216b02013-05-14 13:52:36 -0700455static void throtl_dequeue_tg(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400456{
Tejun Heo5b2c16a2013-05-14 13:52:32 -0700457 if (tg->flags & THROTL_TG_PENDING)
Tejun Heo77216b02013-05-14 13:52:36 -0700458 __throtl_dequeue_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -0400459}
460
Tejun Heoa9131a22013-05-14 13:52:31 -0700461/* Call with queue lock held */
Tejun Heo69df0ab2013-05-14 13:52:36 -0700462static void throtl_schedule_pending_timer(struct throtl_service_queue *sq,
463 unsigned long expires)
Tejun Heoa9131a22013-05-14 13:52:31 -0700464{
Tejun Heo69df0ab2013-05-14 13:52:36 -0700465 mod_timer(&sq->pending_timer, expires);
466 throtl_log(sq, "schedule timer. delay=%lu jiffies=%lu",
467 expires - jiffies, jiffies);
Tejun Heoa9131a22013-05-14 13:52:31 -0700468}
469
Tejun Heo7f52f982013-05-14 13:52:37 -0700470/**
471 * throtl_schedule_next_dispatch - schedule the next dispatch cycle
472 * @sq: the service_queue to schedule dispatch for
473 * @force: force scheduling
474 *
475 * Arm @sq->pending_timer so that the next dispatch cycle starts on the
476 * dispatch time of the first pending child. Returns %true if either timer
477 * is armed or there's no pending child left. %false if the current
478 * dispatch window is still open and the caller should continue
479 * dispatching.
480 *
481 * If @force is %true, the dispatch timer is always scheduled and this
482 * function is guaranteed to return %true. This is to be used when the
483 * caller can't dispatch itself and needs to invoke pending_timer
484 * unconditionally. Note that forced scheduling is likely to induce short
485 * delay before dispatch starts even if @sq->first_pending_disptime is not
486 * in the future and thus shouldn't be used in hot paths.
487 */
488static bool throtl_schedule_next_dispatch(struct throtl_service_queue *sq,
489 bool force)
Vivek Goyale43473b2010-09-15 17:06:35 -0400490{
Tejun Heo6a525602013-05-14 13:52:32 -0700491 /* any pending children left? */
Tejun Heoc9e03322013-05-14 13:52:32 -0700492 if (!sq->nr_pending)
Tejun Heo7f52f982013-05-14 13:52:37 -0700493 return true;
Vivek Goyale43473b2010-09-15 17:06:35 -0400494
Tejun Heoc9e03322013-05-14 13:52:32 -0700495 update_min_dispatch_time(sq);
Vivek Goyale43473b2010-09-15 17:06:35 -0400496
Tejun Heo69df0ab2013-05-14 13:52:36 -0700497 /* is the next dispatch time in the future? */
Tejun Heo7f52f982013-05-14 13:52:37 -0700498 if (force || time_after(sq->first_pending_disptime, jiffies)) {
Tejun Heo69df0ab2013-05-14 13:52:36 -0700499 throtl_schedule_pending_timer(sq, sq->first_pending_disptime);
Tejun Heo7f52f982013-05-14 13:52:37 -0700500 return true;
Tejun Heo69df0ab2013-05-14 13:52:36 -0700501 }
502
Tejun Heo7f52f982013-05-14 13:52:37 -0700503 /* tell the caller to continue dispatching */
504 return false;
Vivek Goyale43473b2010-09-15 17:06:35 -0400505}
506
Tejun Heo0f3457f2013-05-14 13:52:32 -0700507static inline void throtl_start_new_slice(struct throtl_grp *tg, bool rw)
Vivek Goyale43473b2010-09-15 17:06:35 -0400508{
509 tg->bytes_disp[rw] = 0;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400510 tg->io_disp[rw] = 0;
Vivek Goyale43473b2010-09-15 17:06:35 -0400511 tg->slice_start[rw] = jiffies;
512 tg->slice_end[rw] = jiffies + throtl_slice;
Tejun Heofda6f272013-05-14 13:52:36 -0700513 throtl_log(&tg->service_queue,
514 "[%c] new slice start=%lu end=%lu jiffies=%lu",
515 rw == READ ? 'R' : 'W', tg->slice_start[rw],
516 tg->slice_end[rw], jiffies);
Vivek Goyale43473b2010-09-15 17:06:35 -0400517}
518
Tejun Heo0f3457f2013-05-14 13:52:32 -0700519static inline void throtl_set_slice_end(struct throtl_grp *tg, bool rw,
520 unsigned long jiffy_end)
Vivek Goyald1ae8ff2010-12-01 19:34:46 +0100521{
522 tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
523}
524
Tejun Heo0f3457f2013-05-14 13:52:32 -0700525static inline void throtl_extend_slice(struct throtl_grp *tg, bool rw,
526 unsigned long jiffy_end)
Vivek Goyale43473b2010-09-15 17:06:35 -0400527{
528 tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
Tejun Heofda6f272013-05-14 13:52:36 -0700529 throtl_log(&tg->service_queue,
530 "[%c] extend slice start=%lu end=%lu jiffies=%lu",
531 rw == READ ? 'R' : 'W', tg->slice_start[rw],
532 tg->slice_end[rw], jiffies);
Vivek Goyale43473b2010-09-15 17:06:35 -0400533}
534
535/* Determine if previously allocated or extended slice is complete or not */
Tejun Heo0f3457f2013-05-14 13:52:32 -0700536static bool throtl_slice_used(struct throtl_grp *tg, bool rw)
Vivek Goyale43473b2010-09-15 17:06:35 -0400537{
538 if (time_in_range(jiffies, tg->slice_start[rw], tg->slice_end[rw]))
539 return 0;
540
541 return 1;
542}
543
544/* Trim the used slices and adjust slice start accordingly */
Tejun Heo0f3457f2013-05-14 13:52:32 -0700545static inline void throtl_trim_slice(struct throtl_grp *tg, bool rw)
Vivek Goyale43473b2010-09-15 17:06:35 -0400546{
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200547 unsigned long nr_slices, time_elapsed, io_trim;
548 u64 bytes_trim, tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400549
550 BUG_ON(time_before(tg->slice_end[rw], tg->slice_start[rw]));
551
552 /*
553 * If bps are unlimited (-1), then time slice don't get
554 * renewed. Don't try to trim the slice if slice is used. A new
555 * slice will start when appropriate.
556 */
Tejun Heo0f3457f2013-05-14 13:52:32 -0700557 if (throtl_slice_used(tg, rw))
Vivek Goyale43473b2010-09-15 17:06:35 -0400558 return;
559
Vivek Goyald1ae8ff2010-12-01 19:34:46 +0100560 /*
561 * A bio has been dispatched. Also adjust slice_end. It might happen
562 * that initially cgroup limit was very low resulting in high
563 * slice_end, but later limit was bumped up and bio was dispached
564 * sooner, then we need to reduce slice_end. A high bogus slice_end
565 * is bad because it does not allow new slice to start.
566 */
567
Tejun Heo0f3457f2013-05-14 13:52:32 -0700568 throtl_set_slice_end(tg, rw, jiffies + throtl_slice);
Vivek Goyald1ae8ff2010-12-01 19:34:46 +0100569
Vivek Goyale43473b2010-09-15 17:06:35 -0400570 time_elapsed = jiffies - tg->slice_start[rw];
571
572 nr_slices = time_elapsed / throtl_slice;
573
574 if (!nr_slices)
575 return;
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200576 tmp = tg->bps[rw] * throtl_slice * nr_slices;
577 do_div(tmp, HZ);
578 bytes_trim = tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400579
Vivek Goyal8e89d132010-09-15 17:06:37 -0400580 io_trim = (tg->iops[rw] * throtl_slice * nr_slices)/HZ;
Vivek Goyale43473b2010-09-15 17:06:35 -0400581
Vivek Goyal8e89d132010-09-15 17:06:37 -0400582 if (!bytes_trim && !io_trim)
Vivek Goyale43473b2010-09-15 17:06:35 -0400583 return;
584
585 if (tg->bytes_disp[rw] >= bytes_trim)
586 tg->bytes_disp[rw] -= bytes_trim;
587 else
588 tg->bytes_disp[rw] = 0;
589
Vivek Goyal8e89d132010-09-15 17:06:37 -0400590 if (tg->io_disp[rw] >= io_trim)
591 tg->io_disp[rw] -= io_trim;
592 else
593 tg->io_disp[rw] = 0;
594
Vivek Goyale43473b2010-09-15 17:06:35 -0400595 tg->slice_start[rw] += nr_slices * throtl_slice;
596
Tejun Heofda6f272013-05-14 13:52:36 -0700597 throtl_log(&tg->service_queue,
598 "[%c] trim slice nr=%lu bytes=%llu io=%lu start=%lu end=%lu jiffies=%lu",
599 rw == READ ? 'R' : 'W', nr_slices, bytes_trim, io_trim,
600 tg->slice_start[rw], tg->slice_end[rw], jiffies);
Vivek Goyale43473b2010-09-15 17:06:35 -0400601}
602
Tejun Heo0f3457f2013-05-14 13:52:32 -0700603static bool tg_with_in_iops_limit(struct throtl_grp *tg, struct bio *bio,
604 unsigned long *wait)
Vivek Goyale43473b2010-09-15 17:06:35 -0400605{
606 bool rw = bio_data_dir(bio);
Vivek Goyal8e89d132010-09-15 17:06:37 -0400607 unsigned int io_allowed;
Vivek Goyale43473b2010-09-15 17:06:35 -0400608 unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
Vivek Goyalc49c06e2010-10-01 21:16:42 +0200609 u64 tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400610
Vivek Goyal8e89d132010-09-15 17:06:37 -0400611 jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
Vivek Goyale43473b2010-09-15 17:06:35 -0400612
Vivek Goyal8e89d132010-09-15 17:06:37 -0400613 /* Slice has just started. Consider one slice interval */
614 if (!jiffy_elapsed)
615 jiffy_elapsed_rnd = throtl_slice;
616
617 jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
618
Vivek Goyalc49c06e2010-10-01 21:16:42 +0200619 /*
620 * jiffy_elapsed_rnd should not be a big value as minimum iops can be
621 * 1 then at max jiffy elapsed should be equivalent of 1 second as we
622 * will allow dispatch after 1 second and after that slice should
623 * have been trimmed.
624 */
625
626 tmp = (u64)tg->iops[rw] * jiffy_elapsed_rnd;
627 do_div(tmp, HZ);
628
629 if (tmp > UINT_MAX)
630 io_allowed = UINT_MAX;
631 else
632 io_allowed = tmp;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400633
634 if (tg->io_disp[rw] + 1 <= io_allowed) {
Vivek Goyale43473b2010-09-15 17:06:35 -0400635 if (wait)
636 *wait = 0;
637 return 1;
638 }
639
Vivek Goyal8e89d132010-09-15 17:06:37 -0400640 /* Calc approx time to dispatch */
641 jiffy_wait = ((tg->io_disp[rw] + 1) * HZ)/tg->iops[rw] + 1;
642
643 if (jiffy_wait > jiffy_elapsed)
644 jiffy_wait = jiffy_wait - jiffy_elapsed;
645 else
646 jiffy_wait = 1;
647
648 if (wait)
649 *wait = jiffy_wait;
650 return 0;
651}
652
Tejun Heo0f3457f2013-05-14 13:52:32 -0700653static bool tg_with_in_bps_limit(struct throtl_grp *tg, struct bio *bio,
654 unsigned long *wait)
Vivek Goyal8e89d132010-09-15 17:06:37 -0400655{
656 bool rw = bio_data_dir(bio);
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200657 u64 bytes_allowed, extra_bytes, tmp;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400658 unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
Vivek Goyale43473b2010-09-15 17:06:35 -0400659
660 jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
661
662 /* Slice has just started. Consider one slice interval */
663 if (!jiffy_elapsed)
664 jiffy_elapsed_rnd = throtl_slice;
665
666 jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
667
Vivek Goyal5e901a22010-10-01 21:16:38 +0200668 tmp = tg->bps[rw] * jiffy_elapsed_rnd;
669 do_div(tmp, HZ);
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200670 bytes_allowed = tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400671
672 if (tg->bytes_disp[rw] + bio->bi_size <= bytes_allowed) {
673 if (wait)
674 *wait = 0;
675 return 1;
676 }
677
678 /* Calc approx time to dispatch */
679 extra_bytes = tg->bytes_disp[rw] + bio->bi_size - bytes_allowed;
680 jiffy_wait = div64_u64(extra_bytes * HZ, tg->bps[rw]);
681
682 if (!jiffy_wait)
683 jiffy_wait = 1;
684
685 /*
686 * This wait time is without taking into consideration the rounding
687 * up we did. Add that time also.
688 */
689 jiffy_wait = jiffy_wait + (jiffy_elapsed_rnd - jiffy_elapsed);
Vivek Goyale43473b2010-09-15 17:06:35 -0400690 if (wait)
691 *wait = jiffy_wait;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400692 return 0;
693}
Vivek Goyale43473b2010-09-15 17:06:35 -0400694
Vivek Goyalaf75cd32011-05-19 15:38:31 -0400695static bool tg_no_rule_group(struct throtl_grp *tg, bool rw) {
696 if (tg->bps[rw] == -1 && tg->iops[rw] == -1)
697 return 1;
698 return 0;
699}
700
Vivek Goyal8e89d132010-09-15 17:06:37 -0400701/*
702 * Returns whether one can dispatch a bio or not. Also returns approx number
703 * of jiffies to wait before this bio is with-in IO rate and can be dispatched
704 */
Tejun Heo0f3457f2013-05-14 13:52:32 -0700705static bool tg_may_dispatch(struct throtl_grp *tg, struct bio *bio,
706 unsigned long *wait)
Vivek Goyal8e89d132010-09-15 17:06:37 -0400707{
708 bool rw = bio_data_dir(bio);
709 unsigned long bps_wait = 0, iops_wait = 0, max_wait = 0;
710
711 /*
712 * Currently whole state machine of group depends on first bio
713 * queued in the group bio list. So one should not be calling
714 * this function with a different bio if there are other bios
715 * queued.
716 */
Tejun Heo73f0d492013-05-14 13:52:35 -0700717 BUG_ON(tg->service_queue.nr_queued[rw] &&
718 bio != bio_list_peek(&tg->service_queue.bio_lists[rw]));
Vivek Goyal8e89d132010-09-15 17:06:37 -0400719
720 /* If tg->bps = -1, then BW is unlimited */
721 if (tg->bps[rw] == -1 && tg->iops[rw] == -1) {
722 if (wait)
723 *wait = 0;
724 return 1;
725 }
726
727 /*
728 * If previous slice expired, start a new one otherwise renew/extend
729 * existing slice to make sure it is at least throtl_slice interval
730 * long since now.
731 */
Tejun Heo0f3457f2013-05-14 13:52:32 -0700732 if (throtl_slice_used(tg, rw))
733 throtl_start_new_slice(tg, rw);
Vivek Goyal8e89d132010-09-15 17:06:37 -0400734 else {
735 if (time_before(tg->slice_end[rw], jiffies + throtl_slice))
Tejun Heo0f3457f2013-05-14 13:52:32 -0700736 throtl_extend_slice(tg, rw, jiffies + throtl_slice);
Vivek Goyal8e89d132010-09-15 17:06:37 -0400737 }
738
Tejun Heo0f3457f2013-05-14 13:52:32 -0700739 if (tg_with_in_bps_limit(tg, bio, &bps_wait) &&
740 tg_with_in_iops_limit(tg, bio, &iops_wait)) {
Vivek Goyal8e89d132010-09-15 17:06:37 -0400741 if (wait)
742 *wait = 0;
743 return 1;
744 }
745
746 max_wait = max(bps_wait, iops_wait);
747
748 if (wait)
749 *wait = max_wait;
750
751 if (time_before(tg->slice_end[rw], jiffies + max_wait))
Tejun Heo0f3457f2013-05-14 13:52:32 -0700752 throtl_extend_slice(tg, rw, jiffies + max_wait);
Vivek Goyale43473b2010-09-15 17:06:35 -0400753
754 return 0;
755}
756
Tejun Heo3c798392012-04-16 13:57:25 -0700757static void throtl_update_dispatch_stats(struct blkcg_gq *blkg, u64 bytes,
Tejun Heo629ed0b2012-04-01 14:38:44 -0700758 int rw)
759{
Tejun Heo8a3d2612012-04-01 14:38:44 -0700760 struct throtl_grp *tg = blkg_to_tg(blkg);
761 struct tg_stats_cpu *stats_cpu;
Tejun Heo629ed0b2012-04-01 14:38:44 -0700762 unsigned long flags;
763
764 /* If per cpu stats are not allocated yet, don't do any accounting. */
Tejun Heo8a3d2612012-04-01 14:38:44 -0700765 if (tg->stats_cpu == NULL)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700766 return;
767
768 /*
769 * Disabling interrupts to provide mutual exclusion between two
770 * writes on same cpu. It probably is not needed for 64bit. Not
771 * optimizing that case yet.
772 */
773 local_irq_save(flags);
774
Tejun Heo8a3d2612012-04-01 14:38:44 -0700775 stats_cpu = this_cpu_ptr(tg->stats_cpu);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700776
Tejun Heo629ed0b2012-04-01 14:38:44 -0700777 blkg_rwstat_add(&stats_cpu->serviced, rw, 1);
778 blkg_rwstat_add(&stats_cpu->service_bytes, rw, bytes);
779
780 local_irq_restore(flags);
781}
782
Vivek Goyale43473b2010-09-15 17:06:35 -0400783static void throtl_charge_bio(struct throtl_grp *tg, struct bio *bio)
784{
785 bool rw = bio_data_dir(bio);
Vivek Goyale43473b2010-09-15 17:06:35 -0400786
787 /* Charge the bio to the group */
788 tg->bytes_disp[rw] += bio->bi_size;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400789 tg->io_disp[rw]++;
Vivek Goyale43473b2010-09-15 17:06:35 -0400790
Tejun Heo2a0f61e2013-05-14 13:52:36 -0700791 /*
792 * REQ_THROTTLED is used to prevent the same bio to be throttled
793 * more than once as a throttled bio will go through blk-throtl the
794 * second time when it eventually gets issued. Set it when a bio
795 * is being charged to a tg.
796 *
797 * Dispatch stats aren't recursive and each @bio should only be
798 * accounted by the @tg it was originally associated with. Let's
799 * update the stats when setting REQ_THROTTLED for the first time
800 * which is guaranteed to be for the @bio's original tg.
801 */
802 if (!(bio->bi_rw & REQ_THROTTLED)) {
803 bio->bi_rw |= REQ_THROTTLED;
804 throtl_update_dispatch_stats(tg_to_blkg(tg), bio->bi_size,
805 bio->bi_rw);
806 }
Vivek Goyale43473b2010-09-15 17:06:35 -0400807}
808
Tejun Heo77216b02013-05-14 13:52:36 -0700809static void throtl_add_bio_tg(struct bio *bio, struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400810{
Tejun Heo73f0d492013-05-14 13:52:35 -0700811 struct throtl_service_queue *sq = &tg->service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -0400812 bool rw = bio_data_dir(bio);
813
Tejun Heo0e9f4162013-05-14 13:52:35 -0700814 /*
815 * If @tg doesn't currently have any bios queued in the same
816 * direction, queueing @bio can change when @tg should be
817 * dispatched. Mark that @tg was empty. This is automatically
818 * cleaered on the next tg_update_disptime().
819 */
820 if (!sq->nr_queued[rw])
821 tg->flags |= THROTL_TG_WAS_EMPTY;
822
Tejun Heo73f0d492013-05-14 13:52:35 -0700823 bio_list_add(&sq->bio_lists[rw], bio);
Vivek Goyale43473b2010-09-15 17:06:35 -0400824 /* Take a bio reference on tg */
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800825 blkg_get(tg_to_blkg(tg));
Tejun Heo73f0d492013-05-14 13:52:35 -0700826 sq->nr_queued[rw]++;
Tejun Heoe2d57e62013-05-14 13:52:33 -0700827 tg->td->nr_queued[rw]++;
Tejun Heo77216b02013-05-14 13:52:36 -0700828 throtl_enqueue_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -0400829}
830
Tejun Heo77216b02013-05-14 13:52:36 -0700831static void tg_update_disptime(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400832{
Tejun Heo73f0d492013-05-14 13:52:35 -0700833 struct throtl_service_queue *sq = &tg->service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -0400834 unsigned long read_wait = -1, write_wait = -1, min_wait = -1, disptime;
835 struct bio *bio;
836
Tejun Heo73f0d492013-05-14 13:52:35 -0700837 if ((bio = bio_list_peek(&sq->bio_lists[READ])))
Tejun Heo0f3457f2013-05-14 13:52:32 -0700838 tg_may_dispatch(tg, bio, &read_wait);
Vivek Goyale43473b2010-09-15 17:06:35 -0400839
Tejun Heo73f0d492013-05-14 13:52:35 -0700840 if ((bio = bio_list_peek(&sq->bio_lists[WRITE])))
Tejun Heo0f3457f2013-05-14 13:52:32 -0700841 tg_may_dispatch(tg, bio, &write_wait);
Vivek Goyale43473b2010-09-15 17:06:35 -0400842
843 min_wait = min(read_wait, write_wait);
844 disptime = jiffies + min_wait;
845
Vivek Goyale43473b2010-09-15 17:06:35 -0400846 /* Update dispatch time */
Tejun Heo77216b02013-05-14 13:52:36 -0700847 throtl_dequeue_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -0400848 tg->disptime = disptime;
Tejun Heo77216b02013-05-14 13:52:36 -0700849 throtl_enqueue_tg(tg);
Tejun Heo0e9f4162013-05-14 13:52:35 -0700850
851 /* see throtl_add_bio_tg() */
852 tg->flags &= ~THROTL_TG_WAS_EMPTY;
Vivek Goyale43473b2010-09-15 17:06:35 -0400853}
854
Tejun Heo77216b02013-05-14 13:52:36 -0700855static void tg_dispatch_one_bio(struct throtl_grp *tg, bool rw)
Vivek Goyale43473b2010-09-15 17:06:35 -0400856{
Tejun Heo73f0d492013-05-14 13:52:35 -0700857 struct throtl_service_queue *sq = &tg->service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -0400858 struct bio *bio;
859
Tejun Heo73f0d492013-05-14 13:52:35 -0700860 bio = bio_list_pop(&sq->bio_lists[rw]);
861 sq->nr_queued[rw]--;
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800862 /* Drop bio reference on blkg */
863 blkg_put(tg_to_blkg(tg));
Vivek Goyale43473b2010-09-15 17:06:35 -0400864
Tejun Heo0f3457f2013-05-14 13:52:32 -0700865 BUG_ON(tg->td->nr_queued[rw] <= 0);
866 tg->td->nr_queued[rw]--;
Vivek Goyale43473b2010-09-15 17:06:35 -0400867
868 throtl_charge_bio(tg, bio);
Tejun Heo77216b02013-05-14 13:52:36 -0700869 bio_list_add(&sq->parent_sq->bio_lists[rw], bio);
Vivek Goyale43473b2010-09-15 17:06:35 -0400870
Tejun Heo0f3457f2013-05-14 13:52:32 -0700871 throtl_trim_slice(tg, rw);
Vivek Goyale43473b2010-09-15 17:06:35 -0400872}
873
Tejun Heo77216b02013-05-14 13:52:36 -0700874static int throtl_dispatch_tg(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400875{
Tejun Heo73f0d492013-05-14 13:52:35 -0700876 struct throtl_service_queue *sq = &tg->service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -0400877 unsigned int nr_reads = 0, nr_writes = 0;
878 unsigned int max_nr_reads = throtl_grp_quantum*3/4;
Vivek Goyalc2f68052010-11-15 19:32:42 +0100879 unsigned int max_nr_writes = throtl_grp_quantum - max_nr_reads;
Vivek Goyale43473b2010-09-15 17:06:35 -0400880 struct bio *bio;
881
882 /* Try to dispatch 75% READS and 25% WRITES */
883
Tejun Heo73f0d492013-05-14 13:52:35 -0700884 while ((bio = bio_list_peek(&sq->bio_lists[READ])) &&
Tejun Heo0f3457f2013-05-14 13:52:32 -0700885 tg_may_dispatch(tg, bio, NULL)) {
Vivek Goyale43473b2010-09-15 17:06:35 -0400886
Tejun Heo77216b02013-05-14 13:52:36 -0700887 tg_dispatch_one_bio(tg, bio_data_dir(bio));
Vivek Goyale43473b2010-09-15 17:06:35 -0400888 nr_reads++;
889
890 if (nr_reads >= max_nr_reads)
891 break;
892 }
893
Tejun Heo73f0d492013-05-14 13:52:35 -0700894 while ((bio = bio_list_peek(&sq->bio_lists[WRITE])) &&
Tejun Heo0f3457f2013-05-14 13:52:32 -0700895 tg_may_dispatch(tg, bio, NULL)) {
Vivek Goyale43473b2010-09-15 17:06:35 -0400896
Tejun Heo77216b02013-05-14 13:52:36 -0700897 tg_dispatch_one_bio(tg, bio_data_dir(bio));
Vivek Goyale43473b2010-09-15 17:06:35 -0400898 nr_writes++;
899
900 if (nr_writes >= max_nr_writes)
901 break;
902 }
903
904 return nr_reads + nr_writes;
905}
906
Tejun Heo651930b2013-05-14 13:52:35 -0700907static int throtl_select_dispatch(struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -0400908{
909 unsigned int nr_disp = 0;
Vivek Goyale43473b2010-09-15 17:06:35 -0400910
911 while (1) {
Tejun Heo73f0d492013-05-14 13:52:35 -0700912 struct throtl_grp *tg = throtl_rb_first(parent_sq);
913 struct throtl_service_queue *sq = &tg->service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -0400914
915 if (!tg)
916 break;
917
918 if (time_before(jiffies, tg->disptime))
919 break;
920
Tejun Heo77216b02013-05-14 13:52:36 -0700921 throtl_dequeue_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -0400922
Tejun Heo77216b02013-05-14 13:52:36 -0700923 nr_disp += throtl_dispatch_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -0400924
Tejun Heo73f0d492013-05-14 13:52:35 -0700925 if (sq->nr_queued[0] || sq->nr_queued[1])
Tejun Heo77216b02013-05-14 13:52:36 -0700926 tg_update_disptime(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -0400927
928 if (nr_disp >= throtl_quantum)
929 break;
930 }
931
932 return nr_disp;
933}
934
Tejun Heo6e1a5702013-05-14 13:52:37 -0700935/**
936 * throtl_pending_timer_fn - timer function for service_queue->pending_timer
937 * @arg: the throtl_service_queue being serviced
938 *
939 * This timer is armed when a child throtl_grp with active bio's become
940 * pending and queued on the service_queue's pending_tree and expires when
941 * the first child throtl_grp should be dispatched. This function
942 * dispatches bio's from the children throtl_grps and kicks
943 * throtl_data->dispatch_work if there are bio's ready to be issued.
944 */
Tejun Heo69df0ab2013-05-14 13:52:36 -0700945static void throtl_pending_timer_fn(unsigned long arg)
946{
947 struct throtl_service_queue *sq = (void *)arg;
948 struct throtl_data *td = sq_to_td(sq);
Tejun Heocb761992013-05-14 13:52:31 -0700949 struct request_queue *q = td->queue;
Tejun Heo7f52f982013-05-14 13:52:37 -0700950 bool dispatched = false;
Tejun Heo6e1a5702013-05-14 13:52:37 -0700951 int ret;
Vivek Goyale43473b2010-09-15 17:06:35 -0400952
953 spin_lock_irq(q->queue_lock);
954
Tejun Heo7f52f982013-05-14 13:52:37 -0700955 while (true) {
956 throtl_log(sq, "dispatch nr_queued=%u read=%u write=%u",
957 td->nr_queued[READ] + td->nr_queued[WRITE],
958 td->nr_queued[READ], td->nr_queued[WRITE]);
Vivek Goyale43473b2010-09-15 17:06:35 -0400959
Tejun Heo7f52f982013-05-14 13:52:37 -0700960 ret = throtl_select_dispatch(sq);
961 if (ret) {
Tejun Heo7f52f982013-05-14 13:52:37 -0700962 throtl_log(sq, "bios disp=%u", ret);
963 dispatched = true;
Tejun Heo651930b2013-05-14 13:52:35 -0700964 }
Vivek Goyale43473b2010-09-15 17:06:35 -0400965
Tejun Heo7f52f982013-05-14 13:52:37 -0700966 if (throtl_schedule_next_dispatch(sq, false))
967 break;
968
969 /* this dispatch windows is still open, relax and repeat */
970 spin_unlock_irq(q->queue_lock);
971 cpu_relax();
972 spin_lock_irq(q->queue_lock);
973 }
Tejun Heo6a525602013-05-14 13:52:32 -0700974
Tejun Heo6e1a5702013-05-14 13:52:37 -0700975 if (dispatched)
976 queue_work(kthrotld_workqueue, &td->dispatch_work);
977
978 spin_unlock_irq(q->queue_lock);
979}
980
981/**
982 * blk_throtl_dispatch_work_fn - work function for throtl_data->dispatch_work
983 * @work: work item being executed
984 *
985 * This function is queued for execution when bio's reach the bio_lists[]
986 * of throtl_data->service_queue. Those bio's are ready and issued by this
987 * function.
988 */
989void blk_throtl_dispatch_work_fn(struct work_struct *work)
990{
991 struct throtl_data *td = container_of(work, struct throtl_data,
992 dispatch_work);
993 struct throtl_service_queue *td_sq = &td->service_queue;
994 struct request_queue *q = td->queue;
995 struct bio_list bio_list_on_stack;
996 struct bio *bio;
997 struct blk_plug plug;
998 int rw;
999
1000 bio_list_init(&bio_list_on_stack);
1001
1002 spin_lock_irq(q->queue_lock);
1003 for (rw = READ; rw <= WRITE; rw++) {
1004 bio_list_merge(&bio_list_on_stack, &td_sq->bio_lists[rw]);
1005 bio_list_init(&td_sq->bio_lists[rw]);
1006 }
Vivek Goyale43473b2010-09-15 17:06:35 -04001007 spin_unlock_irq(q->queue_lock);
1008
Tejun Heo6e1a5702013-05-14 13:52:37 -07001009 if (!bio_list_empty(&bio_list_on_stack)) {
Vivek Goyal69d60eb2011-03-09 08:27:37 +01001010 blk_start_plug(&plug);
Vivek Goyale43473b2010-09-15 17:06:35 -04001011 while((bio = bio_list_pop(&bio_list_on_stack)))
1012 generic_make_request(bio);
Vivek Goyal69d60eb2011-03-09 08:27:37 +01001013 blk_finish_plug(&plug);
Vivek Goyale43473b2010-09-15 17:06:35 -04001014 }
Vivek Goyale43473b2010-09-15 17:06:35 -04001015}
1016
Tejun Heof95a04a2012-04-16 13:57:26 -07001017static u64 tg_prfill_cpu_rwstat(struct seq_file *sf,
1018 struct blkg_policy_data *pd, int off)
Tejun Heo41b38b62012-04-01 14:38:44 -07001019{
Tejun Heof95a04a2012-04-16 13:57:26 -07001020 struct throtl_grp *tg = pd_to_tg(pd);
Tejun Heo41b38b62012-04-01 14:38:44 -07001021 struct blkg_rwstat rwstat = { }, tmp;
1022 int i, cpu;
1023
1024 for_each_possible_cpu(cpu) {
Tejun Heo8a3d2612012-04-01 14:38:44 -07001025 struct tg_stats_cpu *sc = per_cpu_ptr(tg->stats_cpu, cpu);
Tejun Heo41b38b62012-04-01 14:38:44 -07001026
1027 tmp = blkg_rwstat_read((void *)sc + off);
1028 for (i = 0; i < BLKG_RWSTAT_NR; i++)
1029 rwstat.cnt[i] += tmp.cnt[i];
1030 }
1031
Tejun Heof95a04a2012-04-16 13:57:26 -07001032 return __blkg_prfill_rwstat(sf, pd, &rwstat);
Tejun Heo41b38b62012-04-01 14:38:44 -07001033}
1034
Tejun Heo8a3d2612012-04-01 14:38:44 -07001035static int tg_print_cpu_rwstat(struct cgroup *cgrp, struct cftype *cft,
1036 struct seq_file *sf)
Tejun Heo41b38b62012-04-01 14:38:44 -07001037{
Tejun Heo3c798392012-04-16 13:57:25 -07001038 struct blkcg *blkcg = cgroup_to_blkcg(cgrp);
Tejun Heo41b38b62012-04-01 14:38:44 -07001039
Tejun Heo3c798392012-04-16 13:57:25 -07001040 blkcg_print_blkgs(sf, blkcg, tg_prfill_cpu_rwstat, &blkcg_policy_throtl,
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001041 cft->private, true);
Tejun Heo41b38b62012-04-01 14:38:44 -07001042 return 0;
1043}
1044
Tejun Heof95a04a2012-04-16 13:57:26 -07001045static u64 tg_prfill_conf_u64(struct seq_file *sf, struct blkg_policy_data *pd,
1046 int off)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001047{
Tejun Heof95a04a2012-04-16 13:57:26 -07001048 struct throtl_grp *tg = pd_to_tg(pd);
1049 u64 v = *(u64 *)((void *)tg + off);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001050
Tejun Heoaf133ce2012-04-01 14:38:44 -07001051 if (v == -1)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001052 return 0;
Tejun Heof95a04a2012-04-16 13:57:26 -07001053 return __blkg_prfill_u64(sf, pd, v);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001054}
1055
Tejun Heof95a04a2012-04-16 13:57:26 -07001056static u64 tg_prfill_conf_uint(struct seq_file *sf, struct blkg_policy_data *pd,
1057 int off)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001058{
Tejun Heof95a04a2012-04-16 13:57:26 -07001059 struct throtl_grp *tg = pd_to_tg(pd);
1060 unsigned int v = *(unsigned int *)((void *)tg + off);
Tejun Heoaf133ce2012-04-01 14:38:44 -07001061
1062 if (v == -1)
1063 return 0;
Tejun Heof95a04a2012-04-16 13:57:26 -07001064 return __blkg_prfill_u64(sf, pd, v);
Tejun Heoaf133ce2012-04-01 14:38:44 -07001065}
1066
1067static int tg_print_conf_u64(struct cgroup *cgrp, struct cftype *cft,
1068 struct seq_file *sf)
1069{
Tejun Heo3c798392012-04-16 13:57:25 -07001070 blkcg_print_blkgs(sf, cgroup_to_blkcg(cgrp), tg_prfill_conf_u64,
1071 &blkcg_policy_throtl, cft->private, false);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001072 return 0;
1073}
1074
Tejun Heoaf133ce2012-04-01 14:38:44 -07001075static int tg_print_conf_uint(struct cgroup *cgrp, struct cftype *cft,
1076 struct seq_file *sf)
Vivek Goyale43473b2010-09-15 17:06:35 -04001077{
Tejun Heo3c798392012-04-16 13:57:25 -07001078 blkcg_print_blkgs(sf, cgroup_to_blkcg(cgrp), tg_prfill_conf_uint,
1079 &blkcg_policy_throtl, cft->private, false);
Tejun Heoaf133ce2012-04-01 14:38:44 -07001080 return 0;
Vivek Goyale43473b2010-09-15 17:06:35 -04001081}
1082
Tejun Heoaf133ce2012-04-01 14:38:44 -07001083static int tg_set_conf(struct cgroup *cgrp, struct cftype *cft, const char *buf,
1084 bool is_u64)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001085{
Tejun Heo3c798392012-04-16 13:57:25 -07001086 struct blkcg *blkcg = cgroup_to_blkcg(cgrp);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001087 struct blkg_conf_ctx ctx;
Tejun Heoaf133ce2012-04-01 14:38:44 -07001088 struct throtl_grp *tg;
Tejun Heo69df0ab2013-05-14 13:52:36 -07001089 struct throtl_service_queue *sq;
Tejun Heo60c2bc22012-04-01 14:38:43 -07001090 int ret;
1091
Tejun Heo3c798392012-04-16 13:57:25 -07001092 ret = blkg_conf_prep(blkcg, &blkcg_policy_throtl, buf, &ctx);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001093 if (ret)
1094 return ret;
1095
Tejun Heoaf133ce2012-04-01 14:38:44 -07001096 tg = blkg_to_tg(ctx.blkg);
Tejun Heo69df0ab2013-05-14 13:52:36 -07001097 sq = &tg->service_queue;
Tejun Heoaf133ce2012-04-01 14:38:44 -07001098
Tejun Heoa2b16932012-04-13 13:11:33 -07001099 if (!ctx.v)
1100 ctx.v = -1;
Tejun Heoaf133ce2012-04-01 14:38:44 -07001101
Tejun Heoa2b16932012-04-13 13:11:33 -07001102 if (is_u64)
1103 *(u64 *)((void *)tg + cft->private) = ctx.v;
1104 else
1105 *(unsigned int *)((void *)tg + cft->private) = ctx.v;
Tejun Heoaf133ce2012-04-01 14:38:44 -07001106
Tejun Heofda6f272013-05-14 13:52:36 -07001107 throtl_log(&tg->service_queue,
1108 "limit change rbps=%llu wbps=%llu riops=%u wiops=%u",
1109 tg->bps[READ], tg->bps[WRITE],
1110 tg->iops[READ], tg->iops[WRITE]);
Tejun Heo632b4492013-05-14 13:52:31 -07001111
1112 /*
1113 * We're already holding queue_lock and know @tg is valid. Let's
1114 * apply the new config directly.
1115 *
1116 * Restart the slices for both READ and WRITES. It might happen
1117 * that a group's limit are dropped suddenly and we don't want to
1118 * account recently dispatched IO with new low rate.
1119 */
Tejun Heo0f3457f2013-05-14 13:52:32 -07001120 throtl_start_new_slice(tg, 0);
1121 throtl_start_new_slice(tg, 1);
Tejun Heo632b4492013-05-14 13:52:31 -07001122
Tejun Heo5b2c16a2013-05-14 13:52:32 -07001123 if (tg->flags & THROTL_TG_PENDING) {
Tejun Heo77216b02013-05-14 13:52:36 -07001124 tg_update_disptime(tg);
Tejun Heo7f52f982013-05-14 13:52:37 -07001125 throtl_schedule_next_dispatch(sq->parent_sq, true);
Tejun Heo632b4492013-05-14 13:52:31 -07001126 }
Tejun Heo60c2bc22012-04-01 14:38:43 -07001127
1128 blkg_conf_finish(&ctx);
Tejun Heoa2b16932012-04-13 13:11:33 -07001129 return 0;
Tejun Heo60c2bc22012-04-01 14:38:43 -07001130}
1131
Tejun Heoaf133ce2012-04-01 14:38:44 -07001132static int tg_set_conf_u64(struct cgroup *cgrp, struct cftype *cft,
1133 const char *buf)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001134{
Tejun Heoaf133ce2012-04-01 14:38:44 -07001135 return tg_set_conf(cgrp, cft, buf, true);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001136}
1137
Tejun Heoaf133ce2012-04-01 14:38:44 -07001138static int tg_set_conf_uint(struct cgroup *cgrp, struct cftype *cft,
1139 const char *buf)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001140{
Tejun Heoaf133ce2012-04-01 14:38:44 -07001141 return tg_set_conf(cgrp, cft, buf, false);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001142}
1143
1144static struct cftype throtl_files[] = {
1145 {
1146 .name = "throttle.read_bps_device",
Tejun Heoaf133ce2012-04-01 14:38:44 -07001147 .private = offsetof(struct throtl_grp, bps[READ]),
1148 .read_seq_string = tg_print_conf_u64,
1149 .write_string = tg_set_conf_u64,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001150 .max_write_len = 256,
1151 },
1152 {
1153 .name = "throttle.write_bps_device",
Tejun Heoaf133ce2012-04-01 14:38:44 -07001154 .private = offsetof(struct throtl_grp, bps[WRITE]),
1155 .read_seq_string = tg_print_conf_u64,
1156 .write_string = tg_set_conf_u64,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001157 .max_write_len = 256,
1158 },
1159 {
1160 .name = "throttle.read_iops_device",
Tejun Heoaf133ce2012-04-01 14:38:44 -07001161 .private = offsetof(struct throtl_grp, iops[READ]),
1162 .read_seq_string = tg_print_conf_uint,
1163 .write_string = tg_set_conf_uint,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001164 .max_write_len = 256,
1165 },
1166 {
1167 .name = "throttle.write_iops_device",
Tejun Heoaf133ce2012-04-01 14:38:44 -07001168 .private = offsetof(struct throtl_grp, iops[WRITE]),
1169 .read_seq_string = tg_print_conf_uint,
1170 .write_string = tg_set_conf_uint,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001171 .max_write_len = 256,
1172 },
1173 {
1174 .name = "throttle.io_service_bytes",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001175 .private = offsetof(struct tg_stats_cpu, service_bytes),
Tejun Heo8a3d2612012-04-01 14:38:44 -07001176 .read_seq_string = tg_print_cpu_rwstat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001177 },
1178 {
1179 .name = "throttle.io_serviced",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001180 .private = offsetof(struct tg_stats_cpu, serviced),
Tejun Heo8a3d2612012-04-01 14:38:44 -07001181 .read_seq_string = tg_print_cpu_rwstat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001182 },
1183 { } /* terminate */
1184};
1185
Vivek Goyalda527772011-03-02 19:05:33 -05001186static void throtl_shutdown_wq(struct request_queue *q)
Vivek Goyale43473b2010-09-15 17:06:35 -04001187{
1188 struct throtl_data *td = q->td;
1189
Tejun Heo69df0ab2013-05-14 13:52:36 -07001190 cancel_work_sync(&td->dispatch_work);
Vivek Goyale43473b2010-09-15 17:06:35 -04001191}
1192
Tejun Heo3c798392012-04-16 13:57:25 -07001193static struct blkcg_policy blkcg_policy_throtl = {
Tejun Heof9fcc2d2012-04-16 13:57:27 -07001194 .pd_size = sizeof(struct throtl_grp),
1195 .cftypes = throtl_files,
1196
1197 .pd_init_fn = throtl_pd_init,
1198 .pd_exit_fn = throtl_pd_exit,
1199 .pd_reset_stats_fn = throtl_pd_reset_stats,
Vivek Goyale43473b2010-09-15 17:06:35 -04001200};
1201
Tejun Heobc16a4f2011-10-19 14:33:01 +02001202bool blk_throtl_bio(struct request_queue *q, struct bio *bio)
Vivek Goyale43473b2010-09-15 17:06:35 -04001203{
1204 struct throtl_data *td = q->td;
1205 struct throtl_grp *tg;
Tejun Heo73f0d492013-05-14 13:52:35 -07001206 struct throtl_service_queue *sq;
Tejun Heo0e9f4162013-05-14 13:52:35 -07001207 bool rw = bio_data_dir(bio);
Tejun Heo3c798392012-04-16 13:57:25 -07001208 struct blkcg *blkcg;
Tejun Heobc16a4f2011-10-19 14:33:01 +02001209 bool throttled = false;
Vivek Goyale43473b2010-09-15 17:06:35 -04001210
Tejun Heo2a0f61e2013-05-14 13:52:36 -07001211 /* see throtl_charge_bio() */
1212 if (bio->bi_rw & REQ_THROTTLED)
Tejun Heobc16a4f2011-10-19 14:33:01 +02001213 goto out;
Vivek Goyale43473b2010-09-15 17:06:35 -04001214
Vivek Goyalaf75cd32011-05-19 15:38:31 -04001215 /*
1216 * A throtl_grp pointer retrieved under rcu can be used to access
1217 * basic fields like stats and io rates. If a group has no rules,
1218 * just update the dispatch stats in lockless manner and return.
1219 */
Vivek Goyalaf75cd32011-05-19 15:38:31 -04001220 rcu_read_lock();
Tejun Heo3c798392012-04-16 13:57:25 -07001221 blkcg = bio_blkcg(bio);
Tejun Heocd1604f2012-03-05 13:15:06 -08001222 tg = throtl_lookup_tg(td, blkcg);
Vivek Goyalaf75cd32011-05-19 15:38:31 -04001223 if (tg) {
Vivek Goyalaf75cd32011-05-19 15:38:31 -04001224 if (tg_no_rule_group(tg, rw)) {
Tejun Heo629ed0b2012-04-01 14:38:44 -07001225 throtl_update_dispatch_stats(tg_to_blkg(tg),
1226 bio->bi_size, bio->bi_rw);
Tejun Heo2a7f1242012-03-05 13:15:01 -08001227 goto out_unlock_rcu;
Vivek Goyalaf75cd32011-05-19 15:38:31 -04001228 }
1229 }
Vivek Goyalaf75cd32011-05-19 15:38:31 -04001230
1231 /*
1232 * Either group has not been allocated yet or it is not an unlimited
1233 * IO group
1234 */
Vivek Goyale43473b2010-09-15 17:06:35 -04001235 spin_lock_irq(q->queue_lock);
Tejun Heocd1604f2012-03-05 13:15:06 -08001236 tg = throtl_lookup_create_tg(td, blkcg);
Tejun Heobc16a4f2011-10-19 14:33:01 +02001237 if (unlikely(!tg))
1238 goto out_unlock;
Vivek Goyalf469a7b2011-05-19 15:38:23 -04001239
Tejun Heo73f0d492013-05-14 13:52:35 -07001240 sq = &tg->service_queue;
1241
Tejun Heo0e9f4162013-05-14 13:52:35 -07001242 /* throtl is FIFO - if other bios are already queued, should queue */
1243 if (sq->nr_queued[rw])
Vivek Goyale43473b2010-09-15 17:06:35 -04001244 goto queue_bio;
Vivek Goyalde701c72011-03-07 21:09:32 +01001245
Vivek Goyale43473b2010-09-15 17:06:35 -04001246 /* Bio is with-in rate limit of group */
Tejun Heo0f3457f2013-05-14 13:52:32 -07001247 if (tg_may_dispatch(tg, bio, NULL)) {
Vivek Goyale43473b2010-09-15 17:06:35 -04001248 throtl_charge_bio(tg, bio);
Vivek Goyal04521db2011-03-22 21:54:29 +01001249
1250 /*
1251 * We need to trim slice even when bios are not being queued
1252 * otherwise it might happen that a bio is not queued for
1253 * a long time and slice keeps on extending and trim is not
1254 * called for a long time. Now if limits are reduced suddenly
1255 * we take into account all the IO dispatched so far at new
1256 * low rate and * newly queued IO gets a really long dispatch
1257 * time.
1258 *
1259 * So keep on trimming slice even if bio is not queued.
1260 */
Tejun Heo0f3457f2013-05-14 13:52:32 -07001261 throtl_trim_slice(tg, rw);
Tejun Heobc16a4f2011-10-19 14:33:01 +02001262 goto out_unlock;
Vivek Goyale43473b2010-09-15 17:06:35 -04001263 }
1264
1265queue_bio:
Tejun Heofda6f272013-05-14 13:52:36 -07001266 throtl_log(sq, "[%c] bio. bdisp=%llu sz=%u bps=%llu iodisp=%u iops=%u queued=%d/%d",
1267 rw == READ ? 'R' : 'W',
1268 tg->bytes_disp[rw], bio->bi_size, tg->bps[rw],
1269 tg->io_disp[rw], tg->iops[rw],
1270 sq->nr_queued[READ], sq->nr_queued[WRITE]);
Vivek Goyale43473b2010-09-15 17:06:35 -04001271
Tejun Heo671058f2012-03-05 13:15:29 -08001272 bio_associate_current(bio);
Tejun Heo77216b02013-05-14 13:52:36 -07001273 throtl_add_bio_tg(bio, tg);
Tejun Heobc16a4f2011-10-19 14:33:01 +02001274 throttled = true;
Vivek Goyale43473b2010-09-15 17:06:35 -04001275
Tejun Heo7f52f982013-05-14 13:52:37 -07001276 /*
1277 * Update @tg's dispatch time and force schedule dispatch if @tg
1278 * was empty before @bio. The forced scheduling isn't likely to
1279 * cause undue delay as @bio is likely to be dispatched directly if
1280 * its @tg's disptime is not in the future.
1281 */
Tejun Heo0e9f4162013-05-14 13:52:35 -07001282 if (tg->flags & THROTL_TG_WAS_EMPTY) {
Tejun Heo77216b02013-05-14 13:52:36 -07001283 tg_update_disptime(tg);
Tejun Heo7f52f982013-05-14 13:52:37 -07001284 throtl_schedule_next_dispatch(tg->service_queue.parent_sq, true);
Vivek Goyale43473b2010-09-15 17:06:35 -04001285 }
1286
Tejun Heobc16a4f2011-10-19 14:33:01 +02001287out_unlock:
Vivek Goyale43473b2010-09-15 17:06:35 -04001288 spin_unlock_irq(q->queue_lock);
Tejun Heo2a7f1242012-03-05 13:15:01 -08001289out_unlock_rcu:
1290 rcu_read_unlock();
Tejun Heobc16a4f2011-10-19 14:33:01 +02001291out:
Tejun Heo2a0f61e2013-05-14 13:52:36 -07001292 /*
1293 * As multiple blk-throtls may stack in the same issue path, we
1294 * don't want bios to leave with the flag set. Clear the flag if
1295 * being issued.
1296 */
1297 if (!throttled)
1298 bio->bi_rw &= ~REQ_THROTTLED;
Tejun Heobc16a4f2011-10-19 14:33:01 +02001299 return throttled;
Vivek Goyale43473b2010-09-15 17:06:35 -04001300}
1301
Tejun Heoc9a929d2011-10-19 14:42:16 +02001302/**
1303 * blk_throtl_drain - drain throttled bios
1304 * @q: request_queue to drain throttled bios for
1305 *
1306 * Dispatch all currently throttled bios on @q through ->make_request_fn().
1307 */
1308void blk_throtl_drain(struct request_queue *q)
1309 __releases(q->queue_lock) __acquires(q->queue_lock)
1310{
1311 struct throtl_data *td = q->td;
Tejun Heo0049af72013-05-14 13:52:33 -07001312 struct throtl_service_queue *parent_sq = &td->service_queue;
Tejun Heoc9a929d2011-10-19 14:42:16 +02001313 struct throtl_grp *tg;
Tejun Heoc9a929d2011-10-19 14:42:16 +02001314 struct bio *bio;
Tejun Heo651930b2013-05-14 13:52:35 -07001315 int rw;
Tejun Heoc9a929d2011-10-19 14:42:16 +02001316
Andi Kleen8bcb6c72012-03-30 12:33:28 +02001317 queue_lockdep_assert_held(q);
Tejun Heoc9a929d2011-10-19 14:42:16 +02001318
Tejun Heo0049af72013-05-14 13:52:33 -07001319 while ((tg = throtl_rb_first(parent_sq))) {
Tejun Heo73f0d492013-05-14 13:52:35 -07001320 struct throtl_service_queue *sq = &tg->service_queue;
1321
Tejun Heo77216b02013-05-14 13:52:36 -07001322 throtl_dequeue_tg(tg);
Tejun Heoc9a929d2011-10-19 14:42:16 +02001323
Tejun Heo73f0d492013-05-14 13:52:35 -07001324 while ((bio = bio_list_peek(&sq->bio_lists[READ])))
Tejun Heo77216b02013-05-14 13:52:36 -07001325 tg_dispatch_one_bio(tg, bio_data_dir(bio));
Tejun Heo73f0d492013-05-14 13:52:35 -07001326 while ((bio = bio_list_peek(&sq->bio_lists[WRITE])))
Tejun Heo77216b02013-05-14 13:52:36 -07001327 tg_dispatch_one_bio(tg, bio_data_dir(bio));
Tejun Heoc9a929d2011-10-19 14:42:16 +02001328 }
1329 spin_unlock_irq(q->queue_lock);
1330
Tejun Heo651930b2013-05-14 13:52:35 -07001331 for (rw = READ; rw <= WRITE; rw++)
1332 while ((bio = bio_list_pop(&parent_sq->bio_lists[rw])))
1333 generic_make_request(bio);
Tejun Heoc9a929d2011-10-19 14:42:16 +02001334
1335 spin_lock_irq(q->queue_lock);
1336}
1337
Vivek Goyale43473b2010-09-15 17:06:35 -04001338int blk_throtl_init(struct request_queue *q)
1339{
1340 struct throtl_data *td;
Tejun Heoa2b16932012-04-13 13:11:33 -07001341 int ret;
Vivek Goyale43473b2010-09-15 17:06:35 -04001342
1343 td = kzalloc_node(sizeof(*td), GFP_KERNEL, q->node);
1344 if (!td)
1345 return -ENOMEM;
1346
Tejun Heo69df0ab2013-05-14 13:52:36 -07001347 INIT_WORK(&td->dispatch_work, blk_throtl_dispatch_work_fn);
Tejun Heo77216b02013-05-14 13:52:36 -07001348 throtl_service_queue_init(&td->service_queue, NULL);
Vivek Goyale43473b2010-09-15 17:06:35 -04001349
Tejun Heocd1604f2012-03-05 13:15:06 -08001350 q->td = td;
Vivek Goyal29b12582011-05-19 15:38:24 -04001351 td->queue = q;
Vivek Goyal02977e42010-10-01 14:49:48 +02001352
Tejun Heoa2b16932012-04-13 13:11:33 -07001353 /* activate policy */
Tejun Heo3c798392012-04-16 13:57:25 -07001354 ret = blkcg_activate_policy(q, &blkcg_policy_throtl);
Tejun Heoa2b16932012-04-13 13:11:33 -07001355 if (ret)
Vivek Goyal29b12582011-05-19 15:38:24 -04001356 kfree(td);
Tejun Heoa2b16932012-04-13 13:11:33 -07001357 return ret;
Vivek Goyale43473b2010-09-15 17:06:35 -04001358}
1359
1360void blk_throtl_exit(struct request_queue *q)
1361{
Tejun Heoc875f4d2012-03-05 13:15:22 -08001362 BUG_ON(!q->td);
Vivek Goyalda527772011-03-02 19:05:33 -05001363 throtl_shutdown_wq(q);
Tejun Heo3c798392012-04-16 13:57:25 -07001364 blkcg_deactivate_policy(q, &blkcg_policy_throtl);
Tejun Heoc9a929d2011-10-19 14:42:16 +02001365 kfree(q->td);
Vivek Goyale43473b2010-09-15 17:06:35 -04001366}
1367
1368static int __init throtl_init(void)
1369{
Vivek Goyal450adcb2011-03-01 13:40:54 -05001370 kthrotld_workqueue = alloc_workqueue("kthrotld", WQ_MEM_RECLAIM, 0);
1371 if (!kthrotld_workqueue)
1372 panic("Failed to create kthrotld\n");
1373
Tejun Heo3c798392012-04-16 13:57:25 -07001374 return blkcg_policy_register(&blkcg_policy_throtl);
Vivek Goyale43473b2010-09-15 17:06:35 -04001375}
1376
1377module_init(throtl_init);