blob: c3a235b8ec7e0f8b068159151b1996158ea5098e [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>
Tejun Heoeea8f412015-05-22 17:13:17 -040012#include <linux/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 Heoc5cc2072013-05-14 13:52:38 -070029/*
30 * To implement hierarchical throttling, throtl_grps form a tree and bios
31 * are dispatched upwards level by level until they reach the top and get
32 * issued. When dispatching bios from the children and local group at each
33 * level, if the bios are dispatched into a single bio_list, there's a risk
34 * of a local or child group which can queue many bios at once filling up
35 * the list starving others.
36 *
37 * To avoid such starvation, dispatched bios are queued separately
38 * according to where they came from. When they are again dispatched to
39 * the parent, they're popped in round-robin order so that no single source
40 * hogs the dispatch window.
41 *
42 * throtl_qnode is used to keep the queued bios separated by their sources.
43 * Bios are queued to throtl_qnode which in turn is queued to
44 * throtl_service_queue and then dispatched in round-robin order.
45 *
46 * It's also used to track the reference counts on blkg's. A qnode always
47 * belongs to a throtl_grp and gets queued on itself or the parent, so
48 * incrementing the reference of the associated throtl_grp when a qnode is
49 * queued and decrementing when dequeued is enough to keep the whole blkg
50 * tree pinned while bios are in flight.
51 */
52struct throtl_qnode {
53 struct list_head node; /* service_queue->queued[] */
54 struct bio_list bios; /* queued bios */
55 struct throtl_grp *tg; /* tg this qnode belongs to */
56};
57
Tejun Heoc9e03322013-05-14 13:52:32 -070058struct throtl_service_queue {
Tejun Heo77216b02013-05-14 13:52:36 -070059 struct throtl_service_queue *parent_sq; /* the parent service_queue */
60
Tejun Heo73f0d492013-05-14 13:52:35 -070061 /*
62 * Bios queued directly to this service_queue or dispatched from
63 * children throtl_grp's.
64 */
Tejun Heoc5cc2072013-05-14 13:52:38 -070065 struct list_head queued[2]; /* throtl_qnode [READ/WRITE] */
Tejun Heo73f0d492013-05-14 13:52:35 -070066 unsigned int nr_queued[2]; /* number of queued bios */
67
68 /*
69 * RB tree of active children throtl_grp's, which are sorted by
70 * their ->disptime.
71 */
Tejun Heoc9e03322013-05-14 13:52:32 -070072 struct rb_root pending_tree; /* RB tree of active tgs */
73 struct rb_node *first_pending; /* first node in the tree */
74 unsigned int nr_pending; /* # queued in the tree */
75 unsigned long first_pending_disptime; /* disptime of the first tg */
Tejun Heo69df0ab2013-05-14 13:52:36 -070076 struct timer_list pending_timer; /* fires on first_pending_disptime */
Vivek Goyale43473b2010-09-15 17:06:35 -040077};
78
Tejun Heo5b2c16a2013-05-14 13:52:32 -070079enum tg_state_flags {
80 THROTL_TG_PENDING = 1 << 0, /* on parent's pending tree */
Tejun Heo0e9f4162013-05-14 13:52:35 -070081 THROTL_TG_WAS_EMPTY = 1 << 1, /* bio_lists[] became non-empty */
Tejun Heo5b2c16a2013-05-14 13:52:32 -070082};
83
Vivek Goyale43473b2010-09-15 17:06:35 -040084#define rb_entry_tg(node) rb_entry((node), struct throtl_grp, rb_node)
85
Tejun Heo8a3d2612012-04-01 14:38:44 -070086/* Per-cpu group stats */
87struct tg_stats_cpu {
88 /* total bytes transferred */
89 struct blkg_rwstat service_bytes;
90 /* total IOs serviced, post merge */
91 struct blkg_rwstat serviced;
92};
93
Vivek Goyale43473b2010-09-15 17:06:35 -040094struct throtl_grp {
Tejun Heof95a04a2012-04-16 13:57:26 -070095 /* must be the first member */
96 struct blkg_policy_data pd;
97
Tejun Heoc9e03322013-05-14 13:52:32 -070098 /* active throtl group service_queue member */
Vivek Goyale43473b2010-09-15 17:06:35 -040099 struct rb_node rb_node;
100
Tejun Heo0f3457f2013-05-14 13:52:32 -0700101 /* throtl_data this group belongs to */
102 struct throtl_data *td;
103
Tejun Heo49a2f1e2013-05-14 13:52:34 -0700104 /* this group's service queue */
105 struct throtl_service_queue service_queue;
106
Vivek Goyale43473b2010-09-15 17:06:35 -0400107 /*
Tejun Heoc5cc2072013-05-14 13:52:38 -0700108 * qnode_on_self is used when bios are directly queued to this
109 * throtl_grp so that local bios compete fairly with bios
110 * dispatched from children. qnode_on_parent is used when bios are
111 * dispatched from this throtl_grp into its parent and will compete
112 * with the sibling qnode_on_parents and the parent's
113 * qnode_on_self.
114 */
115 struct throtl_qnode qnode_on_self[2];
116 struct throtl_qnode qnode_on_parent[2];
117
118 /*
Vivek Goyale43473b2010-09-15 17:06:35 -0400119 * Dispatch time in jiffies. This is the estimated time when group
120 * will unthrottle and is ready to dispatch more bio. It is used as
121 * key to sort active groups in service tree.
122 */
123 unsigned long disptime;
124
Vivek Goyale43473b2010-09-15 17:06:35 -0400125 unsigned int flags;
126
Tejun Heo693e7512013-05-14 13:52:38 -0700127 /* are there any throtl rules between this group and td? */
128 bool has_rules[2];
129
Vivek Goyale43473b2010-09-15 17:06:35 -0400130 /* bytes per second rate limits */
131 uint64_t bps[2];
132
Vivek Goyal8e89d132010-09-15 17:06:37 -0400133 /* IOPS limits */
134 unsigned int iops[2];
135
Vivek Goyale43473b2010-09-15 17:06:35 -0400136 /* Number of bytes disptached in current slice */
137 uint64_t bytes_disp[2];
Vivek Goyal8e89d132010-09-15 17:06:37 -0400138 /* Number of bio's dispatched in current slice */
139 unsigned int io_disp[2];
Vivek Goyale43473b2010-09-15 17:06:35 -0400140
141 /* When did we start a new slice */
142 unsigned long slice_start[2];
143 unsigned long slice_end[2];
Vivek Goyalfe071432010-10-01 14:49:49 +0200144
Tejun Heo8a3d2612012-04-01 14:38:44 -0700145 /* Per cpu stats pointer */
146 struct tg_stats_cpu __percpu *stats_cpu;
Vivek Goyale43473b2010-09-15 17:06:35 -0400147};
148
149struct throtl_data
150{
Vivek Goyale43473b2010-09-15 17:06:35 -0400151 /* service tree for active throtl groups */
Tejun Heoc9e03322013-05-14 13:52:32 -0700152 struct throtl_service_queue service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -0400153
Vivek Goyale43473b2010-09-15 17:06:35 -0400154 struct request_queue *queue;
155
156 /* Total Number of queued bios on READ and WRITE lists */
157 unsigned int nr_queued[2];
158
159 /*
Vivek Goyal02977e42010-10-01 14:49:48 +0200160 * number of total undestroyed groups
Vivek Goyale43473b2010-09-15 17:06:35 -0400161 */
162 unsigned int nr_undestroyed_grps;
163
164 /* Work for dispatching throttled bios */
Tejun Heo69df0ab2013-05-14 13:52:36 -0700165 struct work_struct dispatch_work;
Vivek Goyale43473b2010-09-15 17:06:35 -0400166};
167
Tejun Heo69df0ab2013-05-14 13:52:36 -0700168static void throtl_pending_timer_fn(unsigned long arg);
169
Tejun Heof95a04a2012-04-16 13:57:26 -0700170static inline struct throtl_grp *pd_to_tg(struct blkg_policy_data *pd)
171{
172 return pd ? container_of(pd, struct throtl_grp, pd) : NULL;
173}
174
Tejun Heo3c798392012-04-16 13:57:25 -0700175static inline struct throtl_grp *blkg_to_tg(struct blkcg_gq *blkg)
Tejun Heo03814112012-03-05 13:15:14 -0800176{
Tejun Heof95a04a2012-04-16 13:57:26 -0700177 return pd_to_tg(blkg_to_pd(blkg, &blkcg_policy_throtl));
Tejun Heo03814112012-03-05 13:15:14 -0800178}
179
Tejun Heo3c798392012-04-16 13:57:25 -0700180static inline struct blkcg_gq *tg_to_blkg(struct throtl_grp *tg)
Tejun Heo03814112012-03-05 13:15:14 -0800181{
Tejun Heof95a04a2012-04-16 13:57:26 -0700182 return pd_to_blkg(&tg->pd);
Tejun Heo03814112012-03-05 13:15:14 -0800183}
184
Tejun Heo03d8e112012-04-13 13:11:32 -0700185static inline struct throtl_grp *td_root_tg(struct throtl_data *td)
186{
187 return blkg_to_tg(td->queue->root_blkg);
188}
189
Tejun Heofda6f272013-05-14 13:52:36 -0700190/**
191 * sq_to_tg - return the throl_grp the specified service queue belongs to
192 * @sq: the throtl_service_queue of interest
193 *
194 * Return the throtl_grp @sq belongs to. If @sq is the top-level one
195 * embedded in throtl_data, %NULL is returned.
196 */
197static struct throtl_grp *sq_to_tg(struct throtl_service_queue *sq)
198{
199 if (sq && sq->parent_sq)
200 return container_of(sq, struct throtl_grp, service_queue);
201 else
202 return NULL;
203}
Vivek Goyale43473b2010-09-15 17:06:35 -0400204
Tejun Heofda6f272013-05-14 13:52:36 -0700205/**
206 * sq_to_td - return throtl_data the specified service queue belongs to
207 * @sq: the throtl_service_queue of interest
208 *
209 * A service_queue can be embeded in either a throtl_grp or throtl_data.
210 * Determine the associated throtl_data accordingly and return it.
211 */
212static struct throtl_data *sq_to_td(struct throtl_service_queue *sq)
213{
214 struct throtl_grp *tg = sq_to_tg(sq);
215
216 if (tg)
217 return tg->td;
218 else
219 return container_of(sq, struct throtl_data, service_queue);
220}
221
222/**
223 * throtl_log - log debug message via blktrace
224 * @sq: the service_queue being reported
225 * @fmt: printf format string
226 * @args: printf args
227 *
228 * The messages are prefixed with "throtl BLKG_NAME" if @sq belongs to a
229 * throtl_grp; otherwise, just "throtl".
230 *
231 * TODO: this should be made a function and name formatting should happen
232 * after testing whether blktrace is enabled.
233 */
234#define throtl_log(sq, fmt, args...) do { \
235 struct throtl_grp *__tg = sq_to_tg((sq)); \
236 struct throtl_data *__td = sq_to_td((sq)); \
237 \
238 (void)__td; \
239 if ((__tg)) { \
240 char __pbuf[128]; \
241 \
242 blkg_path(tg_to_blkg(__tg), __pbuf, sizeof(__pbuf)); \
243 blk_add_trace_msg(__td->queue, "throtl %s " fmt, __pbuf, ##args); \
244 } else { \
245 blk_add_trace_msg(__td->queue, "throtl " fmt, ##args); \
246 } \
247} while (0)
Vivek Goyale43473b2010-09-15 17:06:35 -0400248
Tejun Heoc5cc2072013-05-14 13:52:38 -0700249static void throtl_qnode_init(struct throtl_qnode *qn, struct throtl_grp *tg)
250{
251 INIT_LIST_HEAD(&qn->node);
252 bio_list_init(&qn->bios);
253 qn->tg = tg;
254}
255
256/**
257 * throtl_qnode_add_bio - add a bio to a throtl_qnode and activate it
258 * @bio: bio being added
259 * @qn: qnode to add bio to
260 * @queued: the service_queue->queued[] list @qn belongs to
261 *
262 * Add @bio to @qn and put @qn on @queued if it's not already on.
263 * @qn->tg's reference count is bumped when @qn is activated. See the
264 * comment on top of throtl_qnode definition for details.
265 */
266static void throtl_qnode_add_bio(struct bio *bio, struct throtl_qnode *qn,
267 struct list_head *queued)
268{
269 bio_list_add(&qn->bios, bio);
270 if (list_empty(&qn->node)) {
271 list_add_tail(&qn->node, queued);
272 blkg_get(tg_to_blkg(qn->tg));
273 }
274}
275
276/**
277 * throtl_peek_queued - peek the first bio on a qnode list
278 * @queued: the qnode list to peek
279 */
280static struct bio *throtl_peek_queued(struct list_head *queued)
281{
282 struct throtl_qnode *qn = list_first_entry(queued, struct throtl_qnode, node);
283 struct bio *bio;
284
285 if (list_empty(queued))
286 return NULL;
287
288 bio = bio_list_peek(&qn->bios);
289 WARN_ON_ONCE(!bio);
290 return bio;
291}
292
293/**
294 * throtl_pop_queued - pop the first bio form a qnode list
295 * @queued: the qnode list to pop a bio from
296 * @tg_to_put: optional out argument for throtl_grp to put
297 *
298 * Pop the first bio from the qnode list @queued. After popping, the first
299 * qnode is removed from @queued if empty or moved to the end of @queued so
300 * that the popping order is round-robin.
301 *
302 * When the first qnode is removed, its associated throtl_grp should be put
303 * too. If @tg_to_put is NULL, this function automatically puts it;
304 * otherwise, *@tg_to_put is set to the throtl_grp to put and the caller is
305 * responsible for putting it.
306 */
307static struct bio *throtl_pop_queued(struct list_head *queued,
308 struct throtl_grp **tg_to_put)
309{
310 struct throtl_qnode *qn = list_first_entry(queued, struct throtl_qnode, node);
311 struct bio *bio;
312
313 if (list_empty(queued))
314 return NULL;
315
316 bio = bio_list_pop(&qn->bios);
317 WARN_ON_ONCE(!bio);
318
319 if (bio_list_empty(&qn->bios)) {
320 list_del_init(&qn->node);
321 if (tg_to_put)
322 *tg_to_put = qn->tg;
323 else
324 blkg_put(tg_to_blkg(qn->tg));
325 } else {
326 list_move_tail(&qn->node, queued);
327 }
328
329 return bio;
330}
331
Tejun Heo49a2f1e2013-05-14 13:52:34 -0700332/* init a service_queue, assumes the caller zeroed it */
Tejun Heob2ce2642015-08-18 14:55:13 -0700333static void throtl_service_queue_init(struct throtl_service_queue *sq)
Tejun Heo49a2f1e2013-05-14 13:52:34 -0700334{
Tejun Heoc5cc2072013-05-14 13:52:38 -0700335 INIT_LIST_HEAD(&sq->queued[0]);
336 INIT_LIST_HEAD(&sq->queued[1]);
Tejun Heo49a2f1e2013-05-14 13:52:34 -0700337 sq->pending_tree = RB_ROOT;
Tejun Heo69df0ab2013-05-14 13:52:36 -0700338 setup_timer(&sq->pending_timer, throtl_pending_timer_fn,
339 (unsigned long)sq);
340}
341
Tejun Heo001bea72015-08-18 14:55:11 -0700342static struct blkg_policy_data *throtl_pd_alloc(gfp_t gfp, int node)
343{
Tejun Heo4fb72032015-08-18 14:55:12 -0700344 struct throtl_grp *tg;
Tejun Heob2ce2642015-08-18 14:55:13 -0700345 int rw, cpu;
Tejun Heo4fb72032015-08-18 14:55:12 -0700346
347 tg = kzalloc_node(sizeof(*tg), gfp, node);
348 if (!tg)
349 return NULL;
350
351 tg->stats_cpu = alloc_percpu_gfp(struct tg_stats_cpu, gfp);
352 if (!tg->stats_cpu) {
353 kfree(tg);
354 return NULL;
355 }
356
Tejun Heob2ce2642015-08-18 14:55:13 -0700357 throtl_service_queue_init(&tg->service_queue);
358
359 for (rw = READ; rw <= WRITE; rw++) {
360 throtl_qnode_init(&tg->qnode_on_self[rw], tg);
361 throtl_qnode_init(&tg->qnode_on_parent[rw], tg);
362 }
363
364 RB_CLEAR_NODE(&tg->rb_node);
365 tg->bps[READ] = -1;
366 tg->bps[WRITE] = -1;
367 tg->iops[READ] = -1;
368 tg->iops[WRITE] = -1;
369
Tejun Heo4fb72032015-08-18 14:55:12 -0700370 for_each_possible_cpu(cpu) {
371 struct tg_stats_cpu *stats_cpu = per_cpu_ptr(tg->stats_cpu, cpu);
372
373 blkg_rwstat_init(&stats_cpu->service_bytes);
374 blkg_rwstat_init(&stats_cpu->serviced);
375 }
376
377 return &tg->pd;
Tejun Heo001bea72015-08-18 14:55:11 -0700378}
379
Tejun Heo3c798392012-04-16 13:57:25 -0700380static void throtl_pd_init(struct blkcg_gq *blkg)
Vivek Goyala29a1712011-05-19 15:38:19 -0400381{
Tejun Heo03814112012-03-05 13:15:14 -0800382 struct throtl_grp *tg = blkg_to_tg(blkg);
Tejun Heo77216b02013-05-14 13:52:36 -0700383 struct throtl_data *td = blkg->q->td;
Tejun Heob2ce2642015-08-18 14:55:13 -0700384 struct throtl_service_queue *sq = &tg->service_queue;
Tejun Heocd1604f2012-03-05 13:15:06 -0800385
Tejun Heo91381252013-05-14 13:52:38 -0700386 /*
Tejun Heoaa6ec292014-07-09 10:08:08 -0400387 * If on the default hierarchy, we switch to properly hierarchical
Tejun Heo91381252013-05-14 13:52:38 -0700388 * behavior where limits on a given throtl_grp are applied to the
389 * whole subtree rather than just the group itself. e.g. If 16M
390 * read_bps limit is set on the root group, the whole system can't
391 * exceed 16M for the device.
392 *
Tejun Heoaa6ec292014-07-09 10:08:08 -0400393 * If not on the default hierarchy, the broken flat hierarchy
Tejun Heo91381252013-05-14 13:52:38 -0700394 * behavior is retained where all throtl_grps are treated as if
395 * they're all separate root groups right below throtl_data.
396 * Limits of a group don't interact with limits of other groups
397 * regardless of the position of the group in the hierarchy.
398 */
Tejun Heob2ce2642015-08-18 14:55:13 -0700399 sq->parent_sq = &td->service_queue;
Tejun Heoaa6ec292014-07-09 10:08:08 -0400400 if (cgroup_on_dfl(blkg->blkcg->css.cgroup) && blkg->parent)
Tejun Heob2ce2642015-08-18 14:55:13 -0700401 sq->parent_sq = &blkg_to_tg(blkg->parent)->service_queue;
Tejun Heo77216b02013-05-14 13:52:36 -0700402 tg->td = td;
Tejun Heo8a3d2612012-04-01 14:38:44 -0700403}
404
Tejun Heo693e7512013-05-14 13:52:38 -0700405/*
406 * Set has_rules[] if @tg or any of its parents have limits configured.
407 * This doesn't require walking up to the top of the hierarchy as the
408 * parent's has_rules[] is guaranteed to be correct.
409 */
410static void tg_update_has_rules(struct throtl_grp *tg)
411{
412 struct throtl_grp *parent_tg = sq_to_tg(tg->service_queue.parent_sq);
413 int rw;
414
415 for (rw = READ; rw <= WRITE; rw++)
416 tg->has_rules[rw] = (parent_tg && parent_tg->has_rules[rw]) ||
417 (tg->bps[rw] != -1 || tg->iops[rw] != -1);
418}
419
420static void throtl_pd_online(struct blkcg_gq *blkg)
421{
422 /*
423 * We don't want new groups to escape the limits of its ancestors.
424 * Update has_rules[] after a new group is brought online.
425 */
426 tg_update_has_rules(blkg_to_tg(blkg));
427}
428
Tejun Heo001bea72015-08-18 14:55:11 -0700429static void throtl_pd_free(struct blkg_policy_data *pd)
430{
Tejun Heo4fb72032015-08-18 14:55:12 -0700431 struct throtl_grp *tg = pd_to_tg(pd);
432
Tejun Heob2ce2642015-08-18 14:55:13 -0700433 del_timer_sync(&tg->service_queue.pending_timer);
Tejun Heo4fb72032015-08-18 14:55:12 -0700434 free_percpu(tg->stats_cpu);
435 kfree(tg);
Tejun Heo001bea72015-08-18 14:55:11 -0700436}
437
Tejun Heo3c798392012-04-16 13:57:25 -0700438static void throtl_pd_reset_stats(struct blkcg_gq *blkg)
Tejun Heo8a3d2612012-04-01 14:38:44 -0700439{
440 struct throtl_grp *tg = blkg_to_tg(blkg);
441 int cpu;
442
Tejun Heo8a3d2612012-04-01 14:38:44 -0700443 for_each_possible_cpu(cpu) {
444 struct tg_stats_cpu *sc = per_cpu_ptr(tg->stats_cpu, cpu);
445
446 blkg_rwstat_reset(&sc->service_bytes);
447 blkg_rwstat_reset(&sc->serviced);
448 }
Vivek Goyala29a1712011-05-19 15:38:19 -0400449}
450
Tejun Heo3c798392012-04-16 13:57:25 -0700451static struct throtl_grp *throtl_lookup_tg(struct throtl_data *td,
452 struct blkcg *blkcg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400453{
Vivek Goyale43473b2010-09-15 17:06:35 -0400454 /*
Tejun Heo3c798392012-04-16 13:57:25 -0700455 * This is the common case when there are no blkcgs. Avoid lookup
456 * in this case
Tejun Heocd1604f2012-03-05 13:15:06 -0800457 */
Tejun Heo3c798392012-04-16 13:57:25 -0700458 if (blkcg == &blkcg_root)
Tejun Heo03d8e112012-04-13 13:11:32 -0700459 return td_root_tg(td);
Vivek Goyale43473b2010-09-15 17:06:35 -0400460
Tejun Heoe8989fa2012-03-05 13:15:20 -0800461 return blkg_to_tg(blkg_lookup(blkcg, td->queue));
Vivek Goyale43473b2010-09-15 17:06:35 -0400462}
463
Tejun Heocd1604f2012-03-05 13:15:06 -0800464static struct throtl_grp *throtl_lookup_create_tg(struct throtl_data *td,
Tejun Heo3c798392012-04-16 13:57:25 -0700465 struct blkcg *blkcg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400466{
Vivek Goyalf469a7b2011-05-19 15:38:23 -0400467 struct request_queue *q = td->queue;
Tejun Heocd1604f2012-03-05 13:15:06 -0800468 struct throtl_grp *tg = NULL;
Tejun Heo0a5a7d02012-03-05 13:15:02 -0800469
Vivek Goyalf469a7b2011-05-19 15:38:23 -0400470 /*
Tejun Heo3c798392012-04-16 13:57:25 -0700471 * This is the common case when there are no blkcgs. Avoid lookup
472 * in this case
Vivek Goyalf469a7b2011-05-19 15:38:23 -0400473 */
Tejun Heo3c798392012-04-16 13:57:25 -0700474 if (blkcg == &blkcg_root) {
Tejun Heo03d8e112012-04-13 13:11:32 -0700475 tg = td_root_tg(td);
Tejun Heocd1604f2012-03-05 13:15:06 -0800476 } else {
Tejun Heo3c798392012-04-16 13:57:25 -0700477 struct blkcg_gq *blkg;
Tejun Heocd1604f2012-03-05 13:15:06 -0800478
Tejun Heo3c96cb32012-04-13 13:11:34 -0700479 blkg = blkg_lookup_create(blkcg, q);
Tejun Heocd1604f2012-03-05 13:15:06 -0800480
481 /* if %NULL and @q is alive, fall back to root_tg */
482 if (!IS_ERR(blkg))
Tejun Heo03814112012-03-05 13:15:14 -0800483 tg = blkg_to_tg(blkg);
Bart Van Assche3f3299d2012-11-28 13:42:38 +0100484 else if (!blk_queue_dying(q))
Tejun Heo03d8e112012-04-13 13:11:32 -0700485 tg = td_root_tg(td);
Vivek Goyalf469a7b2011-05-19 15:38:23 -0400486 }
487
Vivek Goyale43473b2010-09-15 17:06:35 -0400488 return tg;
489}
490
Tejun Heo0049af72013-05-14 13:52:33 -0700491static struct throtl_grp *
492throtl_rb_first(struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -0400493{
494 /* Service tree is empty */
Tejun Heo0049af72013-05-14 13:52:33 -0700495 if (!parent_sq->nr_pending)
Vivek Goyale43473b2010-09-15 17:06:35 -0400496 return NULL;
497
Tejun Heo0049af72013-05-14 13:52:33 -0700498 if (!parent_sq->first_pending)
499 parent_sq->first_pending = rb_first(&parent_sq->pending_tree);
Vivek Goyale43473b2010-09-15 17:06:35 -0400500
Tejun Heo0049af72013-05-14 13:52:33 -0700501 if (parent_sq->first_pending)
502 return rb_entry_tg(parent_sq->first_pending);
Vivek Goyale43473b2010-09-15 17:06:35 -0400503
504 return NULL;
505}
506
507static void rb_erase_init(struct rb_node *n, struct rb_root *root)
508{
509 rb_erase(n, root);
510 RB_CLEAR_NODE(n);
511}
512
Tejun Heo0049af72013-05-14 13:52:33 -0700513static void throtl_rb_erase(struct rb_node *n,
514 struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -0400515{
Tejun Heo0049af72013-05-14 13:52:33 -0700516 if (parent_sq->first_pending == n)
517 parent_sq->first_pending = NULL;
518 rb_erase_init(n, &parent_sq->pending_tree);
519 --parent_sq->nr_pending;
Vivek Goyale43473b2010-09-15 17:06:35 -0400520}
521
Tejun Heo0049af72013-05-14 13:52:33 -0700522static void update_min_dispatch_time(struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -0400523{
524 struct throtl_grp *tg;
525
Tejun Heo0049af72013-05-14 13:52:33 -0700526 tg = throtl_rb_first(parent_sq);
Vivek Goyale43473b2010-09-15 17:06:35 -0400527 if (!tg)
528 return;
529
Tejun Heo0049af72013-05-14 13:52:33 -0700530 parent_sq->first_pending_disptime = tg->disptime;
Vivek Goyale43473b2010-09-15 17:06:35 -0400531}
532
Tejun Heo77216b02013-05-14 13:52:36 -0700533static void tg_service_queue_add(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400534{
Tejun Heo77216b02013-05-14 13:52:36 -0700535 struct throtl_service_queue *parent_sq = tg->service_queue.parent_sq;
Tejun Heo0049af72013-05-14 13:52:33 -0700536 struct rb_node **node = &parent_sq->pending_tree.rb_node;
Vivek Goyale43473b2010-09-15 17:06:35 -0400537 struct rb_node *parent = NULL;
538 struct throtl_grp *__tg;
539 unsigned long key = tg->disptime;
540 int left = 1;
541
542 while (*node != NULL) {
543 parent = *node;
544 __tg = rb_entry_tg(parent);
545
546 if (time_before(key, __tg->disptime))
547 node = &parent->rb_left;
548 else {
549 node = &parent->rb_right;
550 left = 0;
551 }
552 }
553
554 if (left)
Tejun Heo0049af72013-05-14 13:52:33 -0700555 parent_sq->first_pending = &tg->rb_node;
Vivek Goyale43473b2010-09-15 17:06:35 -0400556
557 rb_link_node(&tg->rb_node, parent, node);
Tejun Heo0049af72013-05-14 13:52:33 -0700558 rb_insert_color(&tg->rb_node, &parent_sq->pending_tree);
Vivek Goyale43473b2010-09-15 17:06:35 -0400559}
560
Tejun Heo77216b02013-05-14 13:52:36 -0700561static void __throtl_enqueue_tg(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400562{
Tejun Heo77216b02013-05-14 13:52:36 -0700563 tg_service_queue_add(tg);
Tejun Heo5b2c16a2013-05-14 13:52:32 -0700564 tg->flags |= THROTL_TG_PENDING;
Tejun Heo77216b02013-05-14 13:52:36 -0700565 tg->service_queue.parent_sq->nr_pending++;
Vivek Goyale43473b2010-09-15 17:06:35 -0400566}
567
Tejun Heo77216b02013-05-14 13:52:36 -0700568static void throtl_enqueue_tg(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400569{
Tejun Heo5b2c16a2013-05-14 13:52:32 -0700570 if (!(tg->flags & THROTL_TG_PENDING))
Tejun Heo77216b02013-05-14 13:52:36 -0700571 __throtl_enqueue_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -0400572}
573
Tejun Heo77216b02013-05-14 13:52:36 -0700574static void __throtl_dequeue_tg(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400575{
Tejun Heo77216b02013-05-14 13:52:36 -0700576 throtl_rb_erase(&tg->rb_node, tg->service_queue.parent_sq);
Tejun Heo5b2c16a2013-05-14 13:52:32 -0700577 tg->flags &= ~THROTL_TG_PENDING;
Vivek Goyale43473b2010-09-15 17:06:35 -0400578}
579
Tejun Heo77216b02013-05-14 13:52:36 -0700580static void throtl_dequeue_tg(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400581{
Tejun Heo5b2c16a2013-05-14 13:52:32 -0700582 if (tg->flags & THROTL_TG_PENDING)
Tejun Heo77216b02013-05-14 13:52:36 -0700583 __throtl_dequeue_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -0400584}
585
Tejun Heoa9131a22013-05-14 13:52:31 -0700586/* Call with queue lock held */
Tejun Heo69df0ab2013-05-14 13:52:36 -0700587static void throtl_schedule_pending_timer(struct throtl_service_queue *sq,
588 unsigned long expires)
Tejun Heoa9131a22013-05-14 13:52:31 -0700589{
Tejun Heo69df0ab2013-05-14 13:52:36 -0700590 mod_timer(&sq->pending_timer, expires);
591 throtl_log(sq, "schedule timer. delay=%lu jiffies=%lu",
592 expires - jiffies, jiffies);
Tejun Heoa9131a22013-05-14 13:52:31 -0700593}
594
Tejun Heo7f52f982013-05-14 13:52:37 -0700595/**
596 * throtl_schedule_next_dispatch - schedule the next dispatch cycle
597 * @sq: the service_queue to schedule dispatch for
598 * @force: force scheduling
599 *
600 * Arm @sq->pending_timer so that the next dispatch cycle starts on the
601 * dispatch time of the first pending child. Returns %true if either timer
602 * is armed or there's no pending child left. %false if the current
603 * dispatch window is still open and the caller should continue
604 * dispatching.
605 *
606 * If @force is %true, the dispatch timer is always scheduled and this
607 * function is guaranteed to return %true. This is to be used when the
608 * caller can't dispatch itself and needs to invoke pending_timer
609 * unconditionally. Note that forced scheduling is likely to induce short
610 * delay before dispatch starts even if @sq->first_pending_disptime is not
611 * in the future and thus shouldn't be used in hot paths.
612 */
613static bool throtl_schedule_next_dispatch(struct throtl_service_queue *sq,
614 bool force)
Vivek Goyale43473b2010-09-15 17:06:35 -0400615{
Tejun Heo6a525602013-05-14 13:52:32 -0700616 /* any pending children left? */
Tejun Heoc9e03322013-05-14 13:52:32 -0700617 if (!sq->nr_pending)
Tejun Heo7f52f982013-05-14 13:52:37 -0700618 return true;
Vivek Goyale43473b2010-09-15 17:06:35 -0400619
Tejun Heoc9e03322013-05-14 13:52:32 -0700620 update_min_dispatch_time(sq);
Vivek Goyale43473b2010-09-15 17:06:35 -0400621
Tejun Heo69df0ab2013-05-14 13:52:36 -0700622 /* is the next dispatch time in the future? */
Tejun Heo7f52f982013-05-14 13:52:37 -0700623 if (force || time_after(sq->first_pending_disptime, jiffies)) {
Tejun Heo69df0ab2013-05-14 13:52:36 -0700624 throtl_schedule_pending_timer(sq, sq->first_pending_disptime);
Tejun Heo7f52f982013-05-14 13:52:37 -0700625 return true;
Tejun Heo69df0ab2013-05-14 13:52:36 -0700626 }
627
Tejun Heo7f52f982013-05-14 13:52:37 -0700628 /* tell the caller to continue dispatching */
629 return false;
Vivek Goyale43473b2010-09-15 17:06:35 -0400630}
631
Vivek Goyal32ee5bc2013-05-14 13:52:38 -0700632static inline void throtl_start_new_slice_with_credit(struct throtl_grp *tg,
633 bool rw, unsigned long start)
634{
635 tg->bytes_disp[rw] = 0;
636 tg->io_disp[rw] = 0;
637
638 /*
639 * Previous slice has expired. We must have trimmed it after last
640 * bio dispatch. That means since start of last slice, we never used
641 * that bandwidth. Do try to make use of that bandwidth while giving
642 * credit.
643 */
644 if (time_after_eq(start, tg->slice_start[rw]))
645 tg->slice_start[rw] = start;
646
647 tg->slice_end[rw] = jiffies + throtl_slice;
648 throtl_log(&tg->service_queue,
649 "[%c] new slice with credit start=%lu end=%lu jiffies=%lu",
650 rw == READ ? 'R' : 'W', tg->slice_start[rw],
651 tg->slice_end[rw], jiffies);
652}
653
Tejun Heo0f3457f2013-05-14 13:52:32 -0700654static inline void throtl_start_new_slice(struct throtl_grp *tg, bool rw)
Vivek Goyale43473b2010-09-15 17:06:35 -0400655{
656 tg->bytes_disp[rw] = 0;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400657 tg->io_disp[rw] = 0;
Vivek Goyale43473b2010-09-15 17:06:35 -0400658 tg->slice_start[rw] = jiffies;
659 tg->slice_end[rw] = jiffies + throtl_slice;
Tejun Heofda6f272013-05-14 13:52:36 -0700660 throtl_log(&tg->service_queue,
661 "[%c] new slice start=%lu end=%lu jiffies=%lu",
662 rw == READ ? 'R' : 'W', tg->slice_start[rw],
663 tg->slice_end[rw], jiffies);
Vivek Goyale43473b2010-09-15 17:06:35 -0400664}
665
Tejun Heo0f3457f2013-05-14 13:52:32 -0700666static inline void throtl_set_slice_end(struct throtl_grp *tg, bool rw,
667 unsigned long jiffy_end)
Vivek Goyald1ae8ff2010-12-01 19:34:46 +0100668{
669 tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
670}
671
Tejun Heo0f3457f2013-05-14 13:52:32 -0700672static inline void throtl_extend_slice(struct throtl_grp *tg, bool rw,
673 unsigned long jiffy_end)
Vivek Goyale43473b2010-09-15 17:06:35 -0400674{
675 tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
Tejun Heofda6f272013-05-14 13:52:36 -0700676 throtl_log(&tg->service_queue,
677 "[%c] extend slice start=%lu end=%lu jiffies=%lu",
678 rw == READ ? 'R' : 'W', tg->slice_start[rw],
679 tg->slice_end[rw], jiffies);
Vivek Goyale43473b2010-09-15 17:06:35 -0400680}
681
682/* Determine if previously allocated or extended slice is complete or not */
Tejun Heo0f3457f2013-05-14 13:52:32 -0700683static bool throtl_slice_used(struct throtl_grp *tg, bool rw)
Vivek Goyale43473b2010-09-15 17:06:35 -0400684{
685 if (time_in_range(jiffies, tg->slice_start[rw], tg->slice_end[rw]))
Fabian Frederick5cf8c222014-05-02 18:28:17 +0200686 return false;
Vivek Goyale43473b2010-09-15 17:06:35 -0400687
688 return 1;
689}
690
691/* Trim the used slices and adjust slice start accordingly */
Tejun Heo0f3457f2013-05-14 13:52:32 -0700692static inline void throtl_trim_slice(struct throtl_grp *tg, bool rw)
Vivek Goyale43473b2010-09-15 17:06:35 -0400693{
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200694 unsigned long nr_slices, time_elapsed, io_trim;
695 u64 bytes_trim, tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400696
697 BUG_ON(time_before(tg->slice_end[rw], tg->slice_start[rw]));
698
699 /*
700 * If bps are unlimited (-1), then time slice don't get
701 * renewed. Don't try to trim the slice if slice is used. A new
702 * slice will start when appropriate.
703 */
Tejun Heo0f3457f2013-05-14 13:52:32 -0700704 if (throtl_slice_used(tg, rw))
Vivek Goyale43473b2010-09-15 17:06:35 -0400705 return;
706
Vivek Goyald1ae8ff2010-12-01 19:34:46 +0100707 /*
708 * A bio has been dispatched. Also adjust slice_end. It might happen
709 * that initially cgroup limit was very low resulting in high
710 * slice_end, but later limit was bumped up and bio was dispached
711 * sooner, then we need to reduce slice_end. A high bogus slice_end
712 * is bad because it does not allow new slice to start.
713 */
714
Tejun Heo0f3457f2013-05-14 13:52:32 -0700715 throtl_set_slice_end(tg, rw, jiffies + throtl_slice);
Vivek Goyald1ae8ff2010-12-01 19:34:46 +0100716
Vivek Goyale43473b2010-09-15 17:06:35 -0400717 time_elapsed = jiffies - tg->slice_start[rw];
718
719 nr_slices = time_elapsed / throtl_slice;
720
721 if (!nr_slices)
722 return;
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200723 tmp = tg->bps[rw] * throtl_slice * nr_slices;
724 do_div(tmp, HZ);
725 bytes_trim = tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400726
Vivek Goyal8e89d132010-09-15 17:06:37 -0400727 io_trim = (tg->iops[rw] * throtl_slice * nr_slices)/HZ;
Vivek Goyale43473b2010-09-15 17:06:35 -0400728
Vivek Goyal8e89d132010-09-15 17:06:37 -0400729 if (!bytes_trim && !io_trim)
Vivek Goyale43473b2010-09-15 17:06:35 -0400730 return;
731
732 if (tg->bytes_disp[rw] >= bytes_trim)
733 tg->bytes_disp[rw] -= bytes_trim;
734 else
735 tg->bytes_disp[rw] = 0;
736
Vivek Goyal8e89d132010-09-15 17:06:37 -0400737 if (tg->io_disp[rw] >= io_trim)
738 tg->io_disp[rw] -= io_trim;
739 else
740 tg->io_disp[rw] = 0;
741
Vivek Goyale43473b2010-09-15 17:06:35 -0400742 tg->slice_start[rw] += nr_slices * throtl_slice;
743
Tejun Heofda6f272013-05-14 13:52:36 -0700744 throtl_log(&tg->service_queue,
745 "[%c] trim slice nr=%lu bytes=%llu io=%lu start=%lu end=%lu jiffies=%lu",
746 rw == READ ? 'R' : 'W', nr_slices, bytes_trim, io_trim,
747 tg->slice_start[rw], tg->slice_end[rw], jiffies);
Vivek Goyale43473b2010-09-15 17:06:35 -0400748}
749
Tejun Heo0f3457f2013-05-14 13:52:32 -0700750static bool tg_with_in_iops_limit(struct throtl_grp *tg, struct bio *bio,
751 unsigned long *wait)
Vivek Goyale43473b2010-09-15 17:06:35 -0400752{
753 bool rw = bio_data_dir(bio);
Vivek Goyal8e89d132010-09-15 17:06:37 -0400754 unsigned int io_allowed;
Vivek Goyale43473b2010-09-15 17:06:35 -0400755 unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
Vivek Goyalc49c06e2010-10-01 21:16:42 +0200756 u64 tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400757
Vivek Goyal8e89d132010-09-15 17:06:37 -0400758 jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
Vivek Goyale43473b2010-09-15 17:06:35 -0400759
Vivek Goyal8e89d132010-09-15 17:06:37 -0400760 /* Slice has just started. Consider one slice interval */
761 if (!jiffy_elapsed)
762 jiffy_elapsed_rnd = throtl_slice;
763
764 jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
765
Vivek Goyalc49c06e2010-10-01 21:16:42 +0200766 /*
767 * jiffy_elapsed_rnd should not be a big value as minimum iops can be
768 * 1 then at max jiffy elapsed should be equivalent of 1 second as we
769 * will allow dispatch after 1 second and after that slice should
770 * have been trimmed.
771 */
772
773 tmp = (u64)tg->iops[rw] * jiffy_elapsed_rnd;
774 do_div(tmp, HZ);
775
776 if (tmp > UINT_MAX)
777 io_allowed = UINT_MAX;
778 else
779 io_allowed = tmp;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400780
781 if (tg->io_disp[rw] + 1 <= io_allowed) {
Vivek Goyale43473b2010-09-15 17:06:35 -0400782 if (wait)
783 *wait = 0;
Fabian Frederick5cf8c222014-05-02 18:28:17 +0200784 return true;
Vivek Goyale43473b2010-09-15 17:06:35 -0400785 }
786
Vivek Goyal8e89d132010-09-15 17:06:37 -0400787 /* Calc approx time to dispatch */
788 jiffy_wait = ((tg->io_disp[rw] + 1) * HZ)/tg->iops[rw] + 1;
789
790 if (jiffy_wait > jiffy_elapsed)
791 jiffy_wait = jiffy_wait - jiffy_elapsed;
792 else
793 jiffy_wait = 1;
794
795 if (wait)
796 *wait = jiffy_wait;
797 return 0;
798}
799
Tejun Heo0f3457f2013-05-14 13:52:32 -0700800static bool tg_with_in_bps_limit(struct throtl_grp *tg, struct bio *bio,
801 unsigned long *wait)
Vivek Goyal8e89d132010-09-15 17:06:37 -0400802{
803 bool rw = bio_data_dir(bio);
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200804 u64 bytes_allowed, extra_bytes, tmp;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400805 unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
Vivek Goyale43473b2010-09-15 17:06:35 -0400806
807 jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
808
809 /* Slice has just started. Consider one slice interval */
810 if (!jiffy_elapsed)
811 jiffy_elapsed_rnd = throtl_slice;
812
813 jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
814
Vivek Goyal5e901a22010-10-01 21:16:38 +0200815 tmp = tg->bps[rw] * jiffy_elapsed_rnd;
816 do_div(tmp, HZ);
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200817 bytes_allowed = tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400818
Kent Overstreet4f024f32013-10-11 15:44:27 -0700819 if (tg->bytes_disp[rw] + bio->bi_iter.bi_size <= bytes_allowed) {
Vivek Goyale43473b2010-09-15 17:06:35 -0400820 if (wait)
821 *wait = 0;
Fabian Frederick5cf8c222014-05-02 18:28:17 +0200822 return true;
Vivek Goyale43473b2010-09-15 17:06:35 -0400823 }
824
825 /* Calc approx time to dispatch */
Kent Overstreet4f024f32013-10-11 15:44:27 -0700826 extra_bytes = tg->bytes_disp[rw] + bio->bi_iter.bi_size - bytes_allowed;
Vivek Goyale43473b2010-09-15 17:06:35 -0400827 jiffy_wait = div64_u64(extra_bytes * HZ, tg->bps[rw]);
828
829 if (!jiffy_wait)
830 jiffy_wait = 1;
831
832 /*
833 * This wait time is without taking into consideration the rounding
834 * up we did. Add that time also.
835 */
836 jiffy_wait = jiffy_wait + (jiffy_elapsed_rnd - jiffy_elapsed);
Vivek Goyale43473b2010-09-15 17:06:35 -0400837 if (wait)
838 *wait = jiffy_wait;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400839 return 0;
840}
Vivek Goyale43473b2010-09-15 17:06:35 -0400841
Vivek Goyal8e89d132010-09-15 17:06:37 -0400842/*
843 * Returns whether one can dispatch a bio or not. Also returns approx number
844 * of jiffies to wait before this bio is with-in IO rate and can be dispatched
845 */
Tejun Heo0f3457f2013-05-14 13:52:32 -0700846static bool tg_may_dispatch(struct throtl_grp *tg, struct bio *bio,
847 unsigned long *wait)
Vivek Goyal8e89d132010-09-15 17:06:37 -0400848{
849 bool rw = bio_data_dir(bio);
850 unsigned long bps_wait = 0, iops_wait = 0, max_wait = 0;
851
852 /*
853 * Currently whole state machine of group depends on first bio
854 * queued in the group bio list. So one should not be calling
855 * this function with a different bio if there are other bios
856 * queued.
857 */
Tejun Heo73f0d492013-05-14 13:52:35 -0700858 BUG_ON(tg->service_queue.nr_queued[rw] &&
Tejun Heoc5cc2072013-05-14 13:52:38 -0700859 bio != throtl_peek_queued(&tg->service_queue.queued[rw]));
Vivek Goyal8e89d132010-09-15 17:06:37 -0400860
861 /* If tg->bps = -1, then BW is unlimited */
862 if (tg->bps[rw] == -1 && tg->iops[rw] == -1) {
863 if (wait)
864 *wait = 0;
Fabian Frederick5cf8c222014-05-02 18:28:17 +0200865 return true;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400866 }
867
868 /*
869 * If previous slice expired, start a new one otherwise renew/extend
870 * existing slice to make sure it is at least throtl_slice interval
871 * long since now.
872 */
Tejun Heo0f3457f2013-05-14 13:52:32 -0700873 if (throtl_slice_used(tg, rw))
874 throtl_start_new_slice(tg, rw);
Vivek Goyal8e89d132010-09-15 17:06:37 -0400875 else {
876 if (time_before(tg->slice_end[rw], jiffies + throtl_slice))
Tejun Heo0f3457f2013-05-14 13:52:32 -0700877 throtl_extend_slice(tg, rw, jiffies + throtl_slice);
Vivek Goyal8e89d132010-09-15 17:06:37 -0400878 }
879
Tejun Heo0f3457f2013-05-14 13:52:32 -0700880 if (tg_with_in_bps_limit(tg, bio, &bps_wait) &&
881 tg_with_in_iops_limit(tg, bio, &iops_wait)) {
Vivek Goyal8e89d132010-09-15 17:06:37 -0400882 if (wait)
883 *wait = 0;
884 return 1;
885 }
886
887 max_wait = max(bps_wait, iops_wait);
888
889 if (wait)
890 *wait = max_wait;
891
892 if (time_before(tg->slice_end[rw], jiffies + max_wait))
Tejun Heo0f3457f2013-05-14 13:52:32 -0700893 throtl_extend_slice(tg, rw, jiffies + max_wait);
Vivek Goyale43473b2010-09-15 17:06:35 -0400894
895 return 0;
896}
897
Tejun Heo3c798392012-04-16 13:57:25 -0700898static void throtl_update_dispatch_stats(struct blkcg_gq *blkg, u64 bytes,
Tejun Heo629ed0b2012-04-01 14:38:44 -0700899 int rw)
900{
Tejun Heo8a3d2612012-04-01 14:38:44 -0700901 struct throtl_grp *tg = blkg_to_tg(blkg);
902 struct tg_stats_cpu *stats_cpu;
Tejun Heo629ed0b2012-04-01 14:38:44 -0700903 unsigned long flags;
904
Tejun Heo629ed0b2012-04-01 14:38:44 -0700905 /*
906 * Disabling interrupts to provide mutual exclusion between two
907 * writes on same cpu. It probably is not needed for 64bit. Not
908 * optimizing that case yet.
909 */
910 local_irq_save(flags);
911
Tejun Heo8a3d2612012-04-01 14:38:44 -0700912 stats_cpu = this_cpu_ptr(tg->stats_cpu);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700913
Tejun Heo629ed0b2012-04-01 14:38:44 -0700914 blkg_rwstat_add(&stats_cpu->serviced, rw, 1);
915 blkg_rwstat_add(&stats_cpu->service_bytes, rw, bytes);
916
917 local_irq_restore(flags);
918}
919
Vivek Goyale43473b2010-09-15 17:06:35 -0400920static void throtl_charge_bio(struct throtl_grp *tg, struct bio *bio)
921{
922 bool rw = bio_data_dir(bio);
Vivek Goyale43473b2010-09-15 17:06:35 -0400923
924 /* Charge the bio to the group */
Kent Overstreet4f024f32013-10-11 15:44:27 -0700925 tg->bytes_disp[rw] += bio->bi_iter.bi_size;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400926 tg->io_disp[rw]++;
Vivek Goyale43473b2010-09-15 17:06:35 -0400927
Tejun Heo2a0f61e2013-05-14 13:52:36 -0700928 /*
929 * REQ_THROTTLED is used to prevent the same bio to be throttled
930 * more than once as a throttled bio will go through blk-throtl the
931 * second time when it eventually gets issued. Set it when a bio
932 * is being charged to a tg.
933 *
934 * Dispatch stats aren't recursive and each @bio should only be
935 * accounted by the @tg it was originally associated with. Let's
936 * update the stats when setting REQ_THROTTLED for the first time
937 * which is guaranteed to be for the @bio's original tg.
938 */
939 if (!(bio->bi_rw & REQ_THROTTLED)) {
940 bio->bi_rw |= REQ_THROTTLED;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700941 throtl_update_dispatch_stats(tg_to_blkg(tg),
942 bio->bi_iter.bi_size, bio->bi_rw);
Tejun Heo2a0f61e2013-05-14 13:52:36 -0700943 }
Vivek Goyale43473b2010-09-15 17:06:35 -0400944}
945
Tejun Heoc5cc2072013-05-14 13:52:38 -0700946/**
947 * throtl_add_bio_tg - add a bio to the specified throtl_grp
948 * @bio: bio to add
949 * @qn: qnode to use
950 * @tg: the target throtl_grp
951 *
952 * Add @bio to @tg's service_queue using @qn. If @qn is not specified,
953 * tg->qnode_on_self[] is used.
954 */
955static void throtl_add_bio_tg(struct bio *bio, struct throtl_qnode *qn,
956 struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400957{
Tejun Heo73f0d492013-05-14 13:52:35 -0700958 struct throtl_service_queue *sq = &tg->service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -0400959 bool rw = bio_data_dir(bio);
960
Tejun Heoc5cc2072013-05-14 13:52:38 -0700961 if (!qn)
962 qn = &tg->qnode_on_self[rw];
963
Tejun Heo0e9f4162013-05-14 13:52:35 -0700964 /*
965 * If @tg doesn't currently have any bios queued in the same
966 * direction, queueing @bio can change when @tg should be
967 * dispatched. Mark that @tg was empty. This is automatically
968 * cleaered on the next tg_update_disptime().
969 */
970 if (!sq->nr_queued[rw])
971 tg->flags |= THROTL_TG_WAS_EMPTY;
972
Tejun Heoc5cc2072013-05-14 13:52:38 -0700973 throtl_qnode_add_bio(bio, qn, &sq->queued[rw]);
974
Tejun Heo73f0d492013-05-14 13:52:35 -0700975 sq->nr_queued[rw]++;
Tejun Heo77216b02013-05-14 13:52:36 -0700976 throtl_enqueue_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -0400977}
978
Tejun Heo77216b02013-05-14 13:52:36 -0700979static void tg_update_disptime(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400980{
Tejun Heo73f0d492013-05-14 13:52:35 -0700981 struct throtl_service_queue *sq = &tg->service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -0400982 unsigned long read_wait = -1, write_wait = -1, min_wait = -1, disptime;
983 struct bio *bio;
984
Tejun Heoc5cc2072013-05-14 13:52:38 -0700985 if ((bio = throtl_peek_queued(&sq->queued[READ])))
Tejun Heo0f3457f2013-05-14 13:52:32 -0700986 tg_may_dispatch(tg, bio, &read_wait);
Vivek Goyale43473b2010-09-15 17:06:35 -0400987
Tejun Heoc5cc2072013-05-14 13:52:38 -0700988 if ((bio = throtl_peek_queued(&sq->queued[WRITE])))
Tejun Heo0f3457f2013-05-14 13:52:32 -0700989 tg_may_dispatch(tg, bio, &write_wait);
Vivek Goyale43473b2010-09-15 17:06:35 -0400990
991 min_wait = min(read_wait, write_wait);
992 disptime = jiffies + min_wait;
993
Vivek Goyale43473b2010-09-15 17:06:35 -0400994 /* Update dispatch time */
Tejun Heo77216b02013-05-14 13:52:36 -0700995 throtl_dequeue_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -0400996 tg->disptime = disptime;
Tejun Heo77216b02013-05-14 13:52:36 -0700997 throtl_enqueue_tg(tg);
Tejun Heo0e9f4162013-05-14 13:52:35 -0700998
999 /* see throtl_add_bio_tg() */
1000 tg->flags &= ~THROTL_TG_WAS_EMPTY;
Vivek Goyale43473b2010-09-15 17:06:35 -04001001}
1002
Vivek Goyal32ee5bc2013-05-14 13:52:38 -07001003static void start_parent_slice_with_credit(struct throtl_grp *child_tg,
1004 struct throtl_grp *parent_tg, bool rw)
1005{
1006 if (throtl_slice_used(parent_tg, rw)) {
1007 throtl_start_new_slice_with_credit(parent_tg, rw,
1008 child_tg->slice_start[rw]);
1009 }
1010
1011}
1012
Tejun Heo77216b02013-05-14 13:52:36 -07001013static void tg_dispatch_one_bio(struct throtl_grp *tg, bool rw)
Vivek Goyale43473b2010-09-15 17:06:35 -04001014{
Tejun Heo73f0d492013-05-14 13:52:35 -07001015 struct throtl_service_queue *sq = &tg->service_queue;
Tejun Heo6bc9c2b2013-05-14 13:52:38 -07001016 struct throtl_service_queue *parent_sq = sq->parent_sq;
1017 struct throtl_grp *parent_tg = sq_to_tg(parent_sq);
Tejun Heoc5cc2072013-05-14 13:52:38 -07001018 struct throtl_grp *tg_to_put = NULL;
Vivek Goyale43473b2010-09-15 17:06:35 -04001019 struct bio *bio;
1020
Tejun Heoc5cc2072013-05-14 13:52:38 -07001021 /*
1022 * @bio is being transferred from @tg to @parent_sq. Popping a bio
1023 * from @tg may put its reference and @parent_sq might end up
1024 * getting released prematurely. Remember the tg to put and put it
1025 * after @bio is transferred to @parent_sq.
1026 */
1027 bio = throtl_pop_queued(&sq->queued[rw], &tg_to_put);
Tejun Heo73f0d492013-05-14 13:52:35 -07001028 sq->nr_queued[rw]--;
Vivek Goyale43473b2010-09-15 17:06:35 -04001029
1030 throtl_charge_bio(tg, bio);
Tejun Heo6bc9c2b2013-05-14 13:52:38 -07001031
1032 /*
1033 * If our parent is another tg, we just need to transfer @bio to
1034 * the parent using throtl_add_bio_tg(). If our parent is
1035 * @td->service_queue, @bio is ready to be issued. Put it on its
1036 * bio_lists[] and decrease total number queued. The caller is
1037 * responsible for issuing these bios.
1038 */
1039 if (parent_tg) {
Tejun Heoc5cc2072013-05-14 13:52:38 -07001040 throtl_add_bio_tg(bio, &tg->qnode_on_parent[rw], parent_tg);
Vivek Goyal32ee5bc2013-05-14 13:52:38 -07001041 start_parent_slice_with_credit(tg, parent_tg, rw);
Tejun Heo6bc9c2b2013-05-14 13:52:38 -07001042 } else {
Tejun Heoc5cc2072013-05-14 13:52:38 -07001043 throtl_qnode_add_bio(bio, &tg->qnode_on_parent[rw],
1044 &parent_sq->queued[rw]);
Tejun Heo6bc9c2b2013-05-14 13:52:38 -07001045 BUG_ON(tg->td->nr_queued[rw] <= 0);
1046 tg->td->nr_queued[rw]--;
1047 }
Vivek Goyale43473b2010-09-15 17:06:35 -04001048
Tejun Heo0f3457f2013-05-14 13:52:32 -07001049 throtl_trim_slice(tg, rw);
Tejun Heo6bc9c2b2013-05-14 13:52:38 -07001050
Tejun Heoc5cc2072013-05-14 13:52:38 -07001051 if (tg_to_put)
1052 blkg_put(tg_to_blkg(tg_to_put));
Vivek Goyale43473b2010-09-15 17:06:35 -04001053}
1054
Tejun Heo77216b02013-05-14 13:52:36 -07001055static int throtl_dispatch_tg(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -04001056{
Tejun Heo73f0d492013-05-14 13:52:35 -07001057 struct throtl_service_queue *sq = &tg->service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -04001058 unsigned int nr_reads = 0, nr_writes = 0;
1059 unsigned int max_nr_reads = throtl_grp_quantum*3/4;
Vivek Goyalc2f68052010-11-15 19:32:42 +01001060 unsigned int max_nr_writes = throtl_grp_quantum - max_nr_reads;
Vivek Goyale43473b2010-09-15 17:06:35 -04001061 struct bio *bio;
1062
1063 /* Try to dispatch 75% READS and 25% WRITES */
1064
Tejun Heoc5cc2072013-05-14 13:52:38 -07001065 while ((bio = throtl_peek_queued(&sq->queued[READ])) &&
Tejun Heo0f3457f2013-05-14 13:52:32 -07001066 tg_may_dispatch(tg, bio, NULL)) {
Vivek Goyale43473b2010-09-15 17:06:35 -04001067
Tejun Heo77216b02013-05-14 13:52:36 -07001068 tg_dispatch_one_bio(tg, bio_data_dir(bio));
Vivek Goyale43473b2010-09-15 17:06:35 -04001069 nr_reads++;
1070
1071 if (nr_reads >= max_nr_reads)
1072 break;
1073 }
1074
Tejun Heoc5cc2072013-05-14 13:52:38 -07001075 while ((bio = throtl_peek_queued(&sq->queued[WRITE])) &&
Tejun Heo0f3457f2013-05-14 13:52:32 -07001076 tg_may_dispatch(tg, bio, NULL)) {
Vivek Goyale43473b2010-09-15 17:06:35 -04001077
Tejun Heo77216b02013-05-14 13:52:36 -07001078 tg_dispatch_one_bio(tg, bio_data_dir(bio));
Vivek Goyale43473b2010-09-15 17:06:35 -04001079 nr_writes++;
1080
1081 if (nr_writes >= max_nr_writes)
1082 break;
1083 }
1084
1085 return nr_reads + nr_writes;
1086}
1087
Tejun Heo651930b2013-05-14 13:52:35 -07001088static int throtl_select_dispatch(struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -04001089{
1090 unsigned int nr_disp = 0;
Vivek Goyale43473b2010-09-15 17:06:35 -04001091
1092 while (1) {
Tejun Heo73f0d492013-05-14 13:52:35 -07001093 struct throtl_grp *tg = throtl_rb_first(parent_sq);
1094 struct throtl_service_queue *sq = &tg->service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -04001095
1096 if (!tg)
1097 break;
1098
1099 if (time_before(jiffies, tg->disptime))
1100 break;
1101
Tejun Heo77216b02013-05-14 13:52:36 -07001102 throtl_dequeue_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -04001103
Tejun Heo77216b02013-05-14 13:52:36 -07001104 nr_disp += throtl_dispatch_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -04001105
Tejun Heo73f0d492013-05-14 13:52:35 -07001106 if (sq->nr_queued[0] || sq->nr_queued[1])
Tejun Heo77216b02013-05-14 13:52:36 -07001107 tg_update_disptime(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -04001108
1109 if (nr_disp >= throtl_quantum)
1110 break;
1111 }
1112
1113 return nr_disp;
1114}
1115
Tejun Heo6e1a5702013-05-14 13:52:37 -07001116/**
1117 * throtl_pending_timer_fn - timer function for service_queue->pending_timer
1118 * @arg: the throtl_service_queue being serviced
1119 *
1120 * This timer is armed when a child throtl_grp with active bio's become
1121 * pending and queued on the service_queue's pending_tree and expires when
1122 * the first child throtl_grp should be dispatched. This function
Tejun Heo2e48a532013-05-14 13:52:38 -07001123 * dispatches bio's from the children throtl_grps to the parent
1124 * service_queue.
1125 *
1126 * If the parent's parent is another throtl_grp, dispatching is propagated
1127 * by either arming its pending_timer or repeating dispatch directly. If
1128 * the top-level service_tree is reached, throtl_data->dispatch_work is
1129 * kicked so that the ready bio's are issued.
Tejun Heo6e1a5702013-05-14 13:52:37 -07001130 */
Tejun Heo69df0ab2013-05-14 13:52:36 -07001131static void throtl_pending_timer_fn(unsigned long arg)
1132{
1133 struct throtl_service_queue *sq = (void *)arg;
Tejun Heo2e48a532013-05-14 13:52:38 -07001134 struct throtl_grp *tg = sq_to_tg(sq);
Tejun Heo69df0ab2013-05-14 13:52:36 -07001135 struct throtl_data *td = sq_to_td(sq);
Tejun Heocb761992013-05-14 13:52:31 -07001136 struct request_queue *q = td->queue;
Tejun Heo2e48a532013-05-14 13:52:38 -07001137 struct throtl_service_queue *parent_sq;
1138 bool dispatched;
Tejun Heo6e1a5702013-05-14 13:52:37 -07001139 int ret;
Vivek Goyale43473b2010-09-15 17:06:35 -04001140
1141 spin_lock_irq(q->queue_lock);
Tejun Heo2e48a532013-05-14 13:52:38 -07001142again:
1143 parent_sq = sq->parent_sq;
1144 dispatched = false;
Vivek Goyale43473b2010-09-15 17:06:35 -04001145
Tejun Heo7f52f982013-05-14 13:52:37 -07001146 while (true) {
1147 throtl_log(sq, "dispatch nr_queued=%u read=%u write=%u",
Tejun Heo2e48a532013-05-14 13:52:38 -07001148 sq->nr_queued[READ] + sq->nr_queued[WRITE],
1149 sq->nr_queued[READ], sq->nr_queued[WRITE]);
Vivek Goyale43473b2010-09-15 17:06:35 -04001150
Tejun Heo7f52f982013-05-14 13:52:37 -07001151 ret = throtl_select_dispatch(sq);
1152 if (ret) {
Tejun Heo7f52f982013-05-14 13:52:37 -07001153 throtl_log(sq, "bios disp=%u", ret);
1154 dispatched = true;
Tejun Heo651930b2013-05-14 13:52:35 -07001155 }
Vivek Goyale43473b2010-09-15 17:06:35 -04001156
Tejun Heo7f52f982013-05-14 13:52:37 -07001157 if (throtl_schedule_next_dispatch(sq, false))
1158 break;
1159
1160 /* this dispatch windows is still open, relax and repeat */
1161 spin_unlock_irq(q->queue_lock);
1162 cpu_relax();
1163 spin_lock_irq(q->queue_lock);
1164 }
Tejun Heo6a525602013-05-14 13:52:32 -07001165
Tejun Heo2e48a532013-05-14 13:52:38 -07001166 if (!dispatched)
1167 goto out_unlock;
Tejun Heo6e1a5702013-05-14 13:52:37 -07001168
Tejun Heo2e48a532013-05-14 13:52:38 -07001169 if (parent_sq) {
1170 /* @parent_sq is another throl_grp, propagate dispatch */
1171 if (tg->flags & THROTL_TG_WAS_EMPTY) {
1172 tg_update_disptime(tg);
1173 if (!throtl_schedule_next_dispatch(parent_sq, false)) {
1174 /* window is already open, repeat dispatching */
1175 sq = parent_sq;
1176 tg = sq_to_tg(sq);
1177 goto again;
1178 }
1179 }
1180 } else {
1181 /* reached the top-level, queue issueing */
1182 queue_work(kthrotld_workqueue, &td->dispatch_work);
1183 }
1184out_unlock:
Tejun Heo6e1a5702013-05-14 13:52:37 -07001185 spin_unlock_irq(q->queue_lock);
1186}
1187
1188/**
1189 * blk_throtl_dispatch_work_fn - work function for throtl_data->dispatch_work
1190 * @work: work item being executed
1191 *
1192 * This function is queued for execution when bio's reach the bio_lists[]
1193 * of throtl_data->service_queue. Those bio's are ready and issued by this
1194 * function.
1195 */
Fabian Frederick8876e142014-04-17 21:41:16 +02001196static void blk_throtl_dispatch_work_fn(struct work_struct *work)
Tejun Heo6e1a5702013-05-14 13:52:37 -07001197{
1198 struct throtl_data *td = container_of(work, struct throtl_data,
1199 dispatch_work);
1200 struct throtl_service_queue *td_sq = &td->service_queue;
1201 struct request_queue *q = td->queue;
1202 struct bio_list bio_list_on_stack;
1203 struct bio *bio;
1204 struct blk_plug plug;
1205 int rw;
1206
1207 bio_list_init(&bio_list_on_stack);
1208
1209 spin_lock_irq(q->queue_lock);
Tejun Heoc5cc2072013-05-14 13:52:38 -07001210 for (rw = READ; rw <= WRITE; rw++)
1211 while ((bio = throtl_pop_queued(&td_sq->queued[rw], NULL)))
1212 bio_list_add(&bio_list_on_stack, bio);
Vivek Goyale43473b2010-09-15 17:06:35 -04001213 spin_unlock_irq(q->queue_lock);
1214
Tejun Heo6e1a5702013-05-14 13:52:37 -07001215 if (!bio_list_empty(&bio_list_on_stack)) {
Vivek Goyal69d60eb2011-03-09 08:27:37 +01001216 blk_start_plug(&plug);
Vivek Goyale43473b2010-09-15 17:06:35 -04001217 while((bio = bio_list_pop(&bio_list_on_stack)))
1218 generic_make_request(bio);
Vivek Goyal69d60eb2011-03-09 08:27:37 +01001219 blk_finish_plug(&plug);
Vivek Goyale43473b2010-09-15 17:06:35 -04001220 }
Vivek Goyale43473b2010-09-15 17:06:35 -04001221}
1222
Tejun Heof95a04a2012-04-16 13:57:26 -07001223static u64 tg_prfill_cpu_rwstat(struct seq_file *sf,
1224 struct blkg_policy_data *pd, int off)
Tejun Heo41b38b62012-04-01 14:38:44 -07001225{
Tejun Heof95a04a2012-04-16 13:57:26 -07001226 struct throtl_grp *tg = pd_to_tg(pd);
Tejun Heo41b38b62012-04-01 14:38:44 -07001227 struct blkg_rwstat rwstat = { }, tmp;
1228 int i, cpu;
1229
1230 for_each_possible_cpu(cpu) {
Tejun Heo8a3d2612012-04-01 14:38:44 -07001231 struct tg_stats_cpu *sc = per_cpu_ptr(tg->stats_cpu, cpu);
Tejun Heo41b38b62012-04-01 14:38:44 -07001232
1233 tmp = blkg_rwstat_read((void *)sc + off);
1234 for (i = 0; i < BLKG_RWSTAT_NR; i++)
1235 rwstat.cnt[i] += tmp.cnt[i];
1236 }
1237
Tejun Heof95a04a2012-04-16 13:57:26 -07001238 return __blkg_prfill_rwstat(sf, pd, &rwstat);
Tejun Heo41b38b62012-04-01 14:38:44 -07001239}
1240
Tejun Heo2da8ca82013-12-05 12:28:04 -05001241static int tg_print_cpu_rwstat(struct seq_file *sf, void *v)
Tejun Heo41b38b62012-04-01 14:38:44 -07001242{
Tejun Heo2da8ca82013-12-05 12:28:04 -05001243 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), tg_prfill_cpu_rwstat,
1244 &blkcg_policy_throtl, seq_cft(sf)->private, true);
Tejun Heo41b38b62012-04-01 14:38:44 -07001245 return 0;
1246}
1247
Tejun Heof95a04a2012-04-16 13:57:26 -07001248static u64 tg_prfill_conf_u64(struct seq_file *sf, struct blkg_policy_data *pd,
1249 int off)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001250{
Tejun Heof95a04a2012-04-16 13:57:26 -07001251 struct throtl_grp *tg = pd_to_tg(pd);
1252 u64 v = *(u64 *)((void *)tg + off);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001253
Tejun Heoaf133ce2012-04-01 14:38:44 -07001254 if (v == -1)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001255 return 0;
Tejun Heof95a04a2012-04-16 13:57:26 -07001256 return __blkg_prfill_u64(sf, pd, v);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001257}
1258
Tejun Heof95a04a2012-04-16 13:57:26 -07001259static u64 tg_prfill_conf_uint(struct seq_file *sf, struct blkg_policy_data *pd,
1260 int off)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001261{
Tejun Heof95a04a2012-04-16 13:57:26 -07001262 struct throtl_grp *tg = pd_to_tg(pd);
1263 unsigned int v = *(unsigned int *)((void *)tg + off);
Tejun Heoaf133ce2012-04-01 14:38:44 -07001264
1265 if (v == -1)
1266 return 0;
Tejun Heof95a04a2012-04-16 13:57:26 -07001267 return __blkg_prfill_u64(sf, pd, v);
Tejun Heoaf133ce2012-04-01 14:38:44 -07001268}
1269
Tejun Heo2da8ca82013-12-05 12:28:04 -05001270static int tg_print_conf_u64(struct seq_file *sf, void *v)
Tejun Heoaf133ce2012-04-01 14:38:44 -07001271{
Tejun Heo2da8ca82013-12-05 12:28:04 -05001272 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), tg_prfill_conf_u64,
1273 &blkcg_policy_throtl, seq_cft(sf)->private, false);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001274 return 0;
1275}
1276
Tejun Heo2da8ca82013-12-05 12:28:04 -05001277static int tg_print_conf_uint(struct seq_file *sf, void *v)
Vivek Goyale43473b2010-09-15 17:06:35 -04001278{
Tejun Heo2da8ca82013-12-05 12:28:04 -05001279 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), tg_prfill_conf_uint,
1280 &blkcg_policy_throtl, seq_cft(sf)->private, false);
Tejun Heoaf133ce2012-04-01 14:38:44 -07001281 return 0;
Vivek Goyale43473b2010-09-15 17:06:35 -04001282}
1283
Tejun Heo451af502014-05-13 12:16:21 -04001284static ssize_t tg_set_conf(struct kernfs_open_file *of,
1285 char *buf, size_t nbytes, loff_t off, bool is_u64)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001286{
Tejun Heo451af502014-05-13 12:16:21 -04001287 struct blkcg *blkcg = css_to_blkcg(of_css(of));
Tejun Heo60c2bc22012-04-01 14:38:43 -07001288 struct blkg_conf_ctx ctx;
Tejun Heoaf133ce2012-04-01 14:38:44 -07001289 struct throtl_grp *tg;
Tejun Heo69df0ab2013-05-14 13:52:36 -07001290 struct throtl_service_queue *sq;
Tejun Heo693e7512013-05-14 13:52:38 -07001291 struct blkcg_gq *blkg;
Tejun Heo492eb212013-08-08 20:11:25 -04001292 struct cgroup_subsys_state *pos_css;
Tejun Heo60c2bc22012-04-01 14:38:43 -07001293 int ret;
1294
Tejun Heo3c798392012-04-16 13:57:25 -07001295 ret = blkg_conf_prep(blkcg, &blkcg_policy_throtl, buf, &ctx);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001296 if (ret)
1297 return ret;
1298
Tejun Heoaf133ce2012-04-01 14:38:44 -07001299 tg = blkg_to_tg(ctx.blkg);
Tejun Heo69df0ab2013-05-14 13:52:36 -07001300 sq = &tg->service_queue;
Tejun Heoaf133ce2012-04-01 14:38:44 -07001301
Tejun Heoa2b16932012-04-13 13:11:33 -07001302 if (!ctx.v)
1303 ctx.v = -1;
Tejun Heoaf133ce2012-04-01 14:38:44 -07001304
Tejun Heoa2b16932012-04-13 13:11:33 -07001305 if (is_u64)
Tejun Heo451af502014-05-13 12:16:21 -04001306 *(u64 *)((void *)tg + of_cft(of)->private) = ctx.v;
Tejun Heoa2b16932012-04-13 13:11:33 -07001307 else
Tejun Heo451af502014-05-13 12:16:21 -04001308 *(unsigned int *)((void *)tg + of_cft(of)->private) = ctx.v;
Tejun Heoaf133ce2012-04-01 14:38:44 -07001309
Tejun Heofda6f272013-05-14 13:52:36 -07001310 throtl_log(&tg->service_queue,
1311 "limit change rbps=%llu wbps=%llu riops=%u wiops=%u",
1312 tg->bps[READ], tg->bps[WRITE],
1313 tg->iops[READ], tg->iops[WRITE]);
Tejun Heo632b4492013-05-14 13:52:31 -07001314
1315 /*
Tejun Heo693e7512013-05-14 13:52:38 -07001316 * Update has_rules[] flags for the updated tg's subtree. A tg is
1317 * considered to have rules if either the tg itself or any of its
1318 * ancestors has rules. This identifies groups without any
1319 * restrictions in the whole hierarchy and allows them to bypass
1320 * blk-throttle.
1321 */
Tejun Heo492eb212013-08-08 20:11:25 -04001322 blkg_for_each_descendant_pre(blkg, pos_css, ctx.blkg)
Tejun Heo693e7512013-05-14 13:52:38 -07001323 tg_update_has_rules(blkg_to_tg(blkg));
1324
1325 /*
Tejun Heo632b4492013-05-14 13:52:31 -07001326 * We're already holding queue_lock and know @tg is valid. Let's
1327 * apply the new config directly.
1328 *
1329 * Restart the slices for both READ and WRITES. It might happen
1330 * that a group's limit are dropped suddenly and we don't want to
1331 * account recently dispatched IO with new low rate.
1332 */
Tejun Heo0f3457f2013-05-14 13:52:32 -07001333 throtl_start_new_slice(tg, 0);
1334 throtl_start_new_slice(tg, 1);
Tejun Heo632b4492013-05-14 13:52:31 -07001335
Tejun Heo5b2c16a2013-05-14 13:52:32 -07001336 if (tg->flags & THROTL_TG_PENDING) {
Tejun Heo77216b02013-05-14 13:52:36 -07001337 tg_update_disptime(tg);
Tejun Heo7f52f982013-05-14 13:52:37 -07001338 throtl_schedule_next_dispatch(sq->parent_sq, true);
Tejun Heo632b4492013-05-14 13:52:31 -07001339 }
Tejun Heo60c2bc22012-04-01 14:38:43 -07001340
1341 blkg_conf_finish(&ctx);
Tejun Heo451af502014-05-13 12:16:21 -04001342 return nbytes;
Tejun Heo60c2bc22012-04-01 14:38:43 -07001343}
1344
Tejun Heo451af502014-05-13 12:16:21 -04001345static ssize_t tg_set_conf_u64(struct kernfs_open_file *of,
1346 char *buf, size_t nbytes, loff_t off)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001347{
Tejun Heo451af502014-05-13 12:16:21 -04001348 return tg_set_conf(of, buf, nbytes, off, true);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001349}
1350
Tejun Heo451af502014-05-13 12:16:21 -04001351static ssize_t tg_set_conf_uint(struct kernfs_open_file *of,
1352 char *buf, size_t nbytes, loff_t off)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001353{
Tejun Heo451af502014-05-13 12:16:21 -04001354 return tg_set_conf(of, buf, nbytes, off, false);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001355}
1356
1357static struct cftype throtl_files[] = {
1358 {
1359 .name = "throttle.read_bps_device",
Tejun Heoaf133ce2012-04-01 14:38:44 -07001360 .private = offsetof(struct throtl_grp, bps[READ]),
Tejun Heo2da8ca82013-12-05 12:28:04 -05001361 .seq_show = tg_print_conf_u64,
Tejun Heo451af502014-05-13 12:16:21 -04001362 .write = tg_set_conf_u64,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001363 },
1364 {
1365 .name = "throttle.write_bps_device",
Tejun Heoaf133ce2012-04-01 14:38:44 -07001366 .private = offsetof(struct throtl_grp, bps[WRITE]),
Tejun Heo2da8ca82013-12-05 12:28:04 -05001367 .seq_show = tg_print_conf_u64,
Tejun Heo451af502014-05-13 12:16:21 -04001368 .write = tg_set_conf_u64,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001369 },
1370 {
1371 .name = "throttle.read_iops_device",
Tejun Heoaf133ce2012-04-01 14:38:44 -07001372 .private = offsetof(struct throtl_grp, iops[READ]),
Tejun Heo2da8ca82013-12-05 12:28:04 -05001373 .seq_show = tg_print_conf_uint,
Tejun Heo451af502014-05-13 12:16:21 -04001374 .write = tg_set_conf_uint,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001375 },
1376 {
1377 .name = "throttle.write_iops_device",
Tejun Heoaf133ce2012-04-01 14:38:44 -07001378 .private = offsetof(struct throtl_grp, iops[WRITE]),
Tejun Heo2da8ca82013-12-05 12:28:04 -05001379 .seq_show = tg_print_conf_uint,
Tejun Heo451af502014-05-13 12:16:21 -04001380 .write = tg_set_conf_uint,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001381 },
1382 {
1383 .name = "throttle.io_service_bytes",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001384 .private = offsetof(struct tg_stats_cpu, service_bytes),
Tejun Heo2da8ca82013-12-05 12:28:04 -05001385 .seq_show = tg_print_cpu_rwstat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001386 },
1387 {
1388 .name = "throttle.io_serviced",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001389 .private = offsetof(struct tg_stats_cpu, serviced),
Tejun Heo2da8ca82013-12-05 12:28:04 -05001390 .seq_show = tg_print_cpu_rwstat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001391 },
1392 { } /* terminate */
1393};
1394
Vivek Goyalda527772011-03-02 19:05:33 -05001395static void throtl_shutdown_wq(struct request_queue *q)
Vivek Goyale43473b2010-09-15 17:06:35 -04001396{
1397 struct throtl_data *td = q->td;
1398
Tejun Heo69df0ab2013-05-14 13:52:36 -07001399 cancel_work_sync(&td->dispatch_work);
Vivek Goyale43473b2010-09-15 17:06:35 -04001400}
1401
Tejun Heo3c798392012-04-16 13:57:25 -07001402static struct blkcg_policy blkcg_policy_throtl = {
Tejun Heof9fcc2d2012-04-16 13:57:27 -07001403 .cftypes = throtl_files,
1404
Tejun Heo001bea72015-08-18 14:55:11 -07001405 .pd_alloc_fn = throtl_pd_alloc,
Tejun Heof9fcc2d2012-04-16 13:57:27 -07001406 .pd_init_fn = throtl_pd_init,
Tejun Heo693e7512013-05-14 13:52:38 -07001407 .pd_online_fn = throtl_pd_online,
Tejun Heo001bea72015-08-18 14:55:11 -07001408 .pd_free_fn = throtl_pd_free,
Tejun Heof9fcc2d2012-04-16 13:57:27 -07001409 .pd_reset_stats_fn = throtl_pd_reset_stats,
Vivek Goyale43473b2010-09-15 17:06:35 -04001410};
1411
Tejun Heobc16a4f2011-10-19 14:33:01 +02001412bool blk_throtl_bio(struct request_queue *q, struct bio *bio)
Vivek Goyale43473b2010-09-15 17:06:35 -04001413{
1414 struct throtl_data *td = q->td;
Tejun Heoc5cc2072013-05-14 13:52:38 -07001415 struct throtl_qnode *qn = NULL;
Vivek Goyale43473b2010-09-15 17:06:35 -04001416 struct throtl_grp *tg;
Tejun Heo73f0d492013-05-14 13:52:35 -07001417 struct throtl_service_queue *sq;
Tejun Heo0e9f4162013-05-14 13:52:35 -07001418 bool rw = bio_data_dir(bio);
Tejun Heo3c798392012-04-16 13:57:25 -07001419 struct blkcg *blkcg;
Tejun Heobc16a4f2011-10-19 14:33:01 +02001420 bool throttled = false;
Vivek Goyale43473b2010-09-15 17:06:35 -04001421
Tejun Heo2a0f61e2013-05-14 13:52:36 -07001422 /* see throtl_charge_bio() */
1423 if (bio->bi_rw & REQ_THROTTLED)
Tejun Heobc16a4f2011-10-19 14:33:01 +02001424 goto out;
Vivek Goyale43473b2010-09-15 17:06:35 -04001425
Vivek Goyalaf75cd32011-05-19 15:38:31 -04001426 /*
1427 * A throtl_grp pointer retrieved under rcu can be used to access
1428 * basic fields like stats and io rates. If a group has no rules,
1429 * just update the dispatch stats in lockless manner and return.
1430 */
Vivek Goyalaf75cd32011-05-19 15:38:31 -04001431 rcu_read_lock();
Tejun Heo3c798392012-04-16 13:57:25 -07001432 blkcg = bio_blkcg(bio);
Tejun Heocd1604f2012-03-05 13:15:06 -08001433 tg = throtl_lookup_tg(td, blkcg);
Vivek Goyalaf75cd32011-05-19 15:38:31 -04001434 if (tg) {
Tejun Heo693e7512013-05-14 13:52:38 -07001435 if (!tg->has_rules[rw]) {
Tejun Heo629ed0b2012-04-01 14:38:44 -07001436 throtl_update_dispatch_stats(tg_to_blkg(tg),
Kent Overstreet4f024f32013-10-11 15:44:27 -07001437 bio->bi_iter.bi_size, bio->bi_rw);
Tejun Heo2a7f1242012-03-05 13:15:01 -08001438 goto out_unlock_rcu;
Vivek Goyalaf75cd32011-05-19 15:38:31 -04001439 }
1440 }
Vivek Goyalaf75cd32011-05-19 15:38:31 -04001441
1442 /*
1443 * Either group has not been allocated yet or it is not an unlimited
1444 * IO group
1445 */
Vivek Goyale43473b2010-09-15 17:06:35 -04001446 spin_lock_irq(q->queue_lock);
Tejun Heocd1604f2012-03-05 13:15:06 -08001447 tg = throtl_lookup_create_tg(td, blkcg);
Tejun Heobc16a4f2011-10-19 14:33:01 +02001448 if (unlikely(!tg))
1449 goto out_unlock;
Vivek Goyalf469a7b2011-05-19 15:38:23 -04001450
Tejun Heo73f0d492013-05-14 13:52:35 -07001451 sq = &tg->service_queue;
1452
Tejun Heo9e660ac2013-05-14 13:52:38 -07001453 while (true) {
1454 /* throtl is FIFO - if bios are already queued, should queue */
1455 if (sq->nr_queued[rw])
1456 break;
Vivek Goyalde701c72011-03-07 21:09:32 +01001457
Tejun Heo9e660ac2013-05-14 13:52:38 -07001458 /* if above limits, break to queue */
1459 if (!tg_may_dispatch(tg, bio, NULL))
1460 break;
1461
1462 /* within limits, let's charge and dispatch directly */
Vivek Goyale43473b2010-09-15 17:06:35 -04001463 throtl_charge_bio(tg, bio);
Vivek Goyal04521db2011-03-22 21:54:29 +01001464
1465 /*
1466 * We need to trim slice even when bios are not being queued
1467 * otherwise it might happen that a bio is not queued for
1468 * a long time and slice keeps on extending and trim is not
1469 * called for a long time. Now if limits are reduced suddenly
1470 * we take into account all the IO dispatched so far at new
1471 * low rate and * newly queued IO gets a really long dispatch
1472 * time.
1473 *
1474 * So keep on trimming slice even if bio is not queued.
1475 */
Tejun Heo0f3457f2013-05-14 13:52:32 -07001476 throtl_trim_slice(tg, rw);
Tejun Heo9e660ac2013-05-14 13:52:38 -07001477
1478 /*
1479 * @bio passed through this layer without being throttled.
1480 * Climb up the ladder. If we''re already at the top, it
1481 * can be executed directly.
1482 */
Tejun Heoc5cc2072013-05-14 13:52:38 -07001483 qn = &tg->qnode_on_parent[rw];
Tejun Heo9e660ac2013-05-14 13:52:38 -07001484 sq = sq->parent_sq;
1485 tg = sq_to_tg(sq);
1486 if (!tg)
1487 goto out_unlock;
Vivek Goyale43473b2010-09-15 17:06:35 -04001488 }
1489
Tejun Heo9e660ac2013-05-14 13:52:38 -07001490 /* out-of-limit, queue to @tg */
Tejun Heofda6f272013-05-14 13:52:36 -07001491 throtl_log(sq, "[%c] bio. bdisp=%llu sz=%u bps=%llu iodisp=%u iops=%u queued=%d/%d",
1492 rw == READ ? 'R' : 'W',
Kent Overstreet4f024f32013-10-11 15:44:27 -07001493 tg->bytes_disp[rw], bio->bi_iter.bi_size, tg->bps[rw],
Tejun Heofda6f272013-05-14 13:52:36 -07001494 tg->io_disp[rw], tg->iops[rw],
1495 sq->nr_queued[READ], sq->nr_queued[WRITE]);
Vivek Goyale43473b2010-09-15 17:06:35 -04001496
Tejun Heo671058f2012-03-05 13:15:29 -08001497 bio_associate_current(bio);
Tejun Heo6bc9c2b2013-05-14 13:52:38 -07001498 tg->td->nr_queued[rw]++;
Tejun Heoc5cc2072013-05-14 13:52:38 -07001499 throtl_add_bio_tg(bio, qn, tg);
Tejun Heobc16a4f2011-10-19 14:33:01 +02001500 throttled = true;
Vivek Goyale43473b2010-09-15 17:06:35 -04001501
Tejun Heo7f52f982013-05-14 13:52:37 -07001502 /*
1503 * Update @tg's dispatch time and force schedule dispatch if @tg
1504 * was empty before @bio. The forced scheduling isn't likely to
1505 * cause undue delay as @bio is likely to be dispatched directly if
1506 * its @tg's disptime is not in the future.
1507 */
Tejun Heo0e9f4162013-05-14 13:52:35 -07001508 if (tg->flags & THROTL_TG_WAS_EMPTY) {
Tejun Heo77216b02013-05-14 13:52:36 -07001509 tg_update_disptime(tg);
Tejun Heo7f52f982013-05-14 13:52:37 -07001510 throtl_schedule_next_dispatch(tg->service_queue.parent_sq, true);
Vivek Goyale43473b2010-09-15 17:06:35 -04001511 }
1512
Tejun Heobc16a4f2011-10-19 14:33:01 +02001513out_unlock:
Vivek Goyale43473b2010-09-15 17:06:35 -04001514 spin_unlock_irq(q->queue_lock);
Tejun Heo2a7f1242012-03-05 13:15:01 -08001515out_unlock_rcu:
1516 rcu_read_unlock();
Tejun Heobc16a4f2011-10-19 14:33:01 +02001517out:
Tejun Heo2a0f61e2013-05-14 13:52:36 -07001518 /*
1519 * As multiple blk-throtls may stack in the same issue path, we
1520 * don't want bios to leave with the flag set. Clear the flag if
1521 * being issued.
1522 */
1523 if (!throttled)
1524 bio->bi_rw &= ~REQ_THROTTLED;
Tejun Heobc16a4f2011-10-19 14:33:01 +02001525 return throttled;
Vivek Goyale43473b2010-09-15 17:06:35 -04001526}
1527
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001528/*
1529 * Dispatch all bios from all children tg's queued on @parent_sq. On
1530 * return, @parent_sq is guaranteed to not have any active children tg's
1531 * and all bios from previously active tg's are on @parent_sq->bio_lists[].
1532 */
1533static void tg_drain_bios(struct throtl_service_queue *parent_sq)
1534{
1535 struct throtl_grp *tg;
1536
1537 while ((tg = throtl_rb_first(parent_sq))) {
1538 struct throtl_service_queue *sq = &tg->service_queue;
1539 struct bio *bio;
1540
1541 throtl_dequeue_tg(tg);
1542
Tejun Heoc5cc2072013-05-14 13:52:38 -07001543 while ((bio = throtl_peek_queued(&sq->queued[READ])))
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001544 tg_dispatch_one_bio(tg, bio_data_dir(bio));
Tejun Heoc5cc2072013-05-14 13:52:38 -07001545 while ((bio = throtl_peek_queued(&sq->queued[WRITE])))
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001546 tg_dispatch_one_bio(tg, bio_data_dir(bio));
1547 }
1548}
1549
Tejun Heoc9a929d2011-10-19 14:42:16 +02001550/**
1551 * blk_throtl_drain - drain throttled bios
1552 * @q: request_queue to drain throttled bios for
1553 *
1554 * Dispatch all currently throttled bios on @q through ->make_request_fn().
1555 */
1556void blk_throtl_drain(struct request_queue *q)
1557 __releases(q->queue_lock) __acquires(q->queue_lock)
1558{
1559 struct throtl_data *td = q->td;
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001560 struct blkcg_gq *blkg;
Tejun Heo492eb212013-08-08 20:11:25 -04001561 struct cgroup_subsys_state *pos_css;
Tejun Heoc9a929d2011-10-19 14:42:16 +02001562 struct bio *bio;
Tejun Heo651930b2013-05-14 13:52:35 -07001563 int rw;
Tejun Heoc9a929d2011-10-19 14:42:16 +02001564
Andi Kleen8bcb6c72012-03-30 12:33:28 +02001565 queue_lockdep_assert_held(q);
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001566 rcu_read_lock();
Tejun Heoc9a929d2011-10-19 14:42:16 +02001567
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001568 /*
1569 * Drain each tg while doing post-order walk on the blkg tree, so
1570 * that all bios are propagated to td->service_queue. It'd be
1571 * better to walk service_queue tree directly but blkg walk is
1572 * easier.
1573 */
Tejun Heo492eb212013-08-08 20:11:25 -04001574 blkg_for_each_descendant_post(blkg, pos_css, td->queue->root_blkg)
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001575 tg_drain_bios(&blkg_to_tg(blkg)->service_queue);
Tejun Heo73f0d492013-05-14 13:52:35 -07001576
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001577 /* finally, transfer bios from top-level tg's into the td */
1578 tg_drain_bios(&td->service_queue);
1579
1580 rcu_read_unlock();
Tejun Heoc9a929d2011-10-19 14:42:16 +02001581 spin_unlock_irq(q->queue_lock);
1582
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001583 /* all bios now should be in td->service_queue, issue them */
Tejun Heo651930b2013-05-14 13:52:35 -07001584 for (rw = READ; rw <= WRITE; rw++)
Tejun Heoc5cc2072013-05-14 13:52:38 -07001585 while ((bio = throtl_pop_queued(&td->service_queue.queued[rw],
1586 NULL)))
Tejun Heo651930b2013-05-14 13:52:35 -07001587 generic_make_request(bio);
Tejun Heoc9a929d2011-10-19 14:42:16 +02001588
1589 spin_lock_irq(q->queue_lock);
1590}
1591
Vivek Goyale43473b2010-09-15 17:06:35 -04001592int blk_throtl_init(struct request_queue *q)
1593{
1594 struct throtl_data *td;
Tejun Heoa2b16932012-04-13 13:11:33 -07001595 int ret;
Vivek Goyale43473b2010-09-15 17:06:35 -04001596
1597 td = kzalloc_node(sizeof(*td), GFP_KERNEL, q->node);
1598 if (!td)
1599 return -ENOMEM;
1600
Tejun Heo69df0ab2013-05-14 13:52:36 -07001601 INIT_WORK(&td->dispatch_work, blk_throtl_dispatch_work_fn);
Tejun Heob2ce2642015-08-18 14:55:13 -07001602 throtl_service_queue_init(&td->service_queue);
Vivek Goyale43473b2010-09-15 17:06:35 -04001603
Tejun Heocd1604f2012-03-05 13:15:06 -08001604 q->td = td;
Vivek Goyal29b12582011-05-19 15:38:24 -04001605 td->queue = q;
Vivek Goyal02977e42010-10-01 14:49:48 +02001606
Tejun Heoa2b16932012-04-13 13:11:33 -07001607 /* activate policy */
Tejun Heo3c798392012-04-16 13:57:25 -07001608 ret = blkcg_activate_policy(q, &blkcg_policy_throtl);
Tejun Heoa2b16932012-04-13 13:11:33 -07001609 if (ret)
Vivek Goyal29b12582011-05-19 15:38:24 -04001610 kfree(td);
Tejun Heoa2b16932012-04-13 13:11:33 -07001611 return ret;
Vivek Goyale43473b2010-09-15 17:06:35 -04001612}
1613
1614void blk_throtl_exit(struct request_queue *q)
1615{
Tejun Heoc875f4d2012-03-05 13:15:22 -08001616 BUG_ON(!q->td);
Vivek Goyalda527772011-03-02 19:05:33 -05001617 throtl_shutdown_wq(q);
Tejun Heo3c798392012-04-16 13:57:25 -07001618 blkcg_deactivate_policy(q, &blkcg_policy_throtl);
Tejun Heoc9a929d2011-10-19 14:42:16 +02001619 kfree(q->td);
Vivek Goyale43473b2010-09-15 17:06:35 -04001620}
1621
1622static int __init throtl_init(void)
1623{
Vivek Goyal450adcb2011-03-01 13:40:54 -05001624 kthrotld_workqueue = alloc_workqueue("kthrotld", WQ_MEM_RECLAIM, 0);
1625 if (!kthrotld_workqueue)
1626 panic("Failed to create kthrotld\n");
1627
Tejun Heo3c798392012-04-16 13:57:25 -07001628 return blkcg_policy_register(&blkcg_policy_throtl);
Vivek Goyale43473b2010-09-15 17:06:35 -04001629}
1630
1631module_init(throtl_init);