blob: 900a777e01c26a511fca0230d7c0f9edf4fb3111 [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 Heoa9520cd2015-08-18 14:55:14 -0700380static void throtl_pd_init(struct blkg_policy_data *pd)
Vivek Goyala29a1712011-05-19 15:38:19 -0400381{
Tejun Heoa9520cd2015-08-18 14:55:14 -0700382 struct throtl_grp *tg = pd_to_tg(pd);
383 struct blkcg_gq *blkg = tg_to_blkg(tg);
Tejun Heo77216b02013-05-14 13:52:36 -0700384 struct throtl_data *td = blkg->q->td;
Tejun Heob2ce2642015-08-18 14:55:13 -0700385 struct throtl_service_queue *sq = &tg->service_queue;
Tejun Heocd1604f2012-03-05 13:15:06 -0800386
Tejun Heo91381252013-05-14 13:52:38 -0700387 /*
Tejun Heoaa6ec292014-07-09 10:08:08 -0400388 * If on the default hierarchy, we switch to properly hierarchical
Tejun Heo91381252013-05-14 13:52:38 -0700389 * behavior where limits on a given throtl_grp are applied to the
390 * whole subtree rather than just the group itself. e.g. If 16M
391 * read_bps limit is set on the root group, the whole system can't
392 * exceed 16M for the device.
393 *
Tejun Heoaa6ec292014-07-09 10:08:08 -0400394 * If not on the default hierarchy, the broken flat hierarchy
Tejun Heo91381252013-05-14 13:52:38 -0700395 * behavior is retained where all throtl_grps are treated as if
396 * they're all separate root groups right below throtl_data.
397 * Limits of a group don't interact with limits of other groups
398 * regardless of the position of the group in the hierarchy.
399 */
Tejun Heob2ce2642015-08-18 14:55:13 -0700400 sq->parent_sq = &td->service_queue;
Tejun Heoaa6ec292014-07-09 10:08:08 -0400401 if (cgroup_on_dfl(blkg->blkcg->css.cgroup) && blkg->parent)
Tejun Heob2ce2642015-08-18 14:55:13 -0700402 sq->parent_sq = &blkg_to_tg(blkg->parent)->service_queue;
Tejun Heo77216b02013-05-14 13:52:36 -0700403 tg->td = td;
Tejun Heo8a3d2612012-04-01 14:38:44 -0700404}
405
Tejun Heo693e7512013-05-14 13:52:38 -0700406/*
407 * Set has_rules[] if @tg or any of its parents have limits configured.
408 * This doesn't require walking up to the top of the hierarchy as the
409 * parent's has_rules[] is guaranteed to be correct.
410 */
411static void tg_update_has_rules(struct throtl_grp *tg)
412{
413 struct throtl_grp *parent_tg = sq_to_tg(tg->service_queue.parent_sq);
414 int rw;
415
416 for (rw = READ; rw <= WRITE; rw++)
417 tg->has_rules[rw] = (parent_tg && parent_tg->has_rules[rw]) ||
418 (tg->bps[rw] != -1 || tg->iops[rw] != -1);
419}
420
Tejun Heoa9520cd2015-08-18 14:55:14 -0700421static void throtl_pd_online(struct blkg_policy_data *pd)
Tejun Heo693e7512013-05-14 13:52:38 -0700422{
423 /*
424 * We don't want new groups to escape the limits of its ancestors.
425 * Update has_rules[] after a new group is brought online.
426 */
Tejun Heoa9520cd2015-08-18 14:55:14 -0700427 tg_update_has_rules(pd_to_tg(pd));
Tejun Heo693e7512013-05-14 13:52:38 -0700428}
429
Tejun Heo001bea72015-08-18 14:55:11 -0700430static void throtl_pd_free(struct blkg_policy_data *pd)
431{
Tejun Heo4fb72032015-08-18 14:55:12 -0700432 struct throtl_grp *tg = pd_to_tg(pd);
433
Tejun Heob2ce2642015-08-18 14:55:13 -0700434 del_timer_sync(&tg->service_queue.pending_timer);
Tejun Heo4fb72032015-08-18 14:55:12 -0700435 free_percpu(tg->stats_cpu);
436 kfree(tg);
Tejun Heo001bea72015-08-18 14:55:11 -0700437}
438
Tejun Heoa9520cd2015-08-18 14:55:14 -0700439static void throtl_pd_reset_stats(struct blkg_policy_data *pd)
Tejun Heo8a3d2612012-04-01 14:38:44 -0700440{
Tejun Heoa9520cd2015-08-18 14:55:14 -0700441 struct throtl_grp *tg = pd_to_tg(pd);
Tejun Heo8a3d2612012-04-01 14:38:44 -0700442 int cpu;
443
Tejun Heo8a3d2612012-04-01 14:38:44 -0700444 for_each_possible_cpu(cpu) {
445 struct tg_stats_cpu *sc = per_cpu_ptr(tg->stats_cpu, cpu);
446
447 blkg_rwstat_reset(&sc->service_bytes);
448 blkg_rwstat_reset(&sc->serviced);
449 }
Vivek Goyala29a1712011-05-19 15:38:19 -0400450}
451
Tejun Heo3c798392012-04-16 13:57:25 -0700452static struct throtl_grp *throtl_lookup_tg(struct throtl_data *td,
453 struct blkcg *blkcg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400454{
Tejun Heoe8989fa2012-03-05 13:15:20 -0800455 return blkg_to_tg(blkg_lookup(blkcg, td->queue));
Vivek Goyale43473b2010-09-15 17:06:35 -0400456}
457
Tejun Heocd1604f2012-03-05 13:15:06 -0800458static struct throtl_grp *throtl_lookup_create_tg(struct throtl_data *td,
Tejun Heo3c798392012-04-16 13:57:25 -0700459 struct blkcg *blkcg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400460{
Vivek Goyalf469a7b2011-05-19 15:38:23 -0400461 struct request_queue *q = td->queue;
Tejun Heocd1604f2012-03-05 13:15:06 -0800462 struct throtl_grp *tg = NULL;
Tejun Heo0a5a7d02012-03-05 13:15:02 -0800463
Vivek Goyalf469a7b2011-05-19 15:38:23 -0400464 /*
Tejun Heo3c798392012-04-16 13:57:25 -0700465 * This is the common case when there are no blkcgs. Avoid lookup
466 * in this case
Vivek Goyalf469a7b2011-05-19 15:38:23 -0400467 */
Tejun Heo3c798392012-04-16 13:57:25 -0700468 if (blkcg == &blkcg_root) {
Tejun Heo03d8e112012-04-13 13:11:32 -0700469 tg = td_root_tg(td);
Tejun Heocd1604f2012-03-05 13:15:06 -0800470 } else {
Tejun Heo3c798392012-04-16 13:57:25 -0700471 struct blkcg_gq *blkg;
Tejun Heocd1604f2012-03-05 13:15:06 -0800472
Tejun Heo3c96cb32012-04-13 13:11:34 -0700473 blkg = blkg_lookup_create(blkcg, q);
Tejun Heocd1604f2012-03-05 13:15:06 -0800474
475 /* if %NULL and @q is alive, fall back to root_tg */
476 if (!IS_ERR(blkg))
Tejun Heo03814112012-03-05 13:15:14 -0800477 tg = blkg_to_tg(blkg);
Tejun Heoc9589f02015-08-18 14:55:19 -0700478 else
Tejun Heo03d8e112012-04-13 13:11:32 -0700479 tg = td_root_tg(td);
Vivek Goyalf469a7b2011-05-19 15:38:23 -0400480 }
481
Vivek Goyale43473b2010-09-15 17:06:35 -0400482 return tg;
483}
484
Tejun Heo0049af72013-05-14 13:52:33 -0700485static struct throtl_grp *
486throtl_rb_first(struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -0400487{
488 /* Service tree is empty */
Tejun Heo0049af72013-05-14 13:52:33 -0700489 if (!parent_sq->nr_pending)
Vivek Goyale43473b2010-09-15 17:06:35 -0400490 return NULL;
491
Tejun Heo0049af72013-05-14 13:52:33 -0700492 if (!parent_sq->first_pending)
493 parent_sq->first_pending = rb_first(&parent_sq->pending_tree);
Vivek Goyale43473b2010-09-15 17:06:35 -0400494
Tejun Heo0049af72013-05-14 13:52:33 -0700495 if (parent_sq->first_pending)
496 return rb_entry_tg(parent_sq->first_pending);
Vivek Goyale43473b2010-09-15 17:06:35 -0400497
498 return NULL;
499}
500
501static void rb_erase_init(struct rb_node *n, struct rb_root *root)
502{
503 rb_erase(n, root);
504 RB_CLEAR_NODE(n);
505}
506
Tejun Heo0049af72013-05-14 13:52:33 -0700507static void throtl_rb_erase(struct rb_node *n,
508 struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -0400509{
Tejun Heo0049af72013-05-14 13:52:33 -0700510 if (parent_sq->first_pending == n)
511 parent_sq->first_pending = NULL;
512 rb_erase_init(n, &parent_sq->pending_tree);
513 --parent_sq->nr_pending;
Vivek Goyale43473b2010-09-15 17:06:35 -0400514}
515
Tejun Heo0049af72013-05-14 13:52:33 -0700516static void update_min_dispatch_time(struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -0400517{
518 struct throtl_grp *tg;
519
Tejun Heo0049af72013-05-14 13:52:33 -0700520 tg = throtl_rb_first(parent_sq);
Vivek Goyale43473b2010-09-15 17:06:35 -0400521 if (!tg)
522 return;
523
Tejun Heo0049af72013-05-14 13:52:33 -0700524 parent_sq->first_pending_disptime = tg->disptime;
Vivek Goyale43473b2010-09-15 17:06:35 -0400525}
526
Tejun Heo77216b02013-05-14 13:52:36 -0700527static void tg_service_queue_add(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400528{
Tejun Heo77216b02013-05-14 13:52:36 -0700529 struct throtl_service_queue *parent_sq = tg->service_queue.parent_sq;
Tejun Heo0049af72013-05-14 13:52:33 -0700530 struct rb_node **node = &parent_sq->pending_tree.rb_node;
Vivek Goyale43473b2010-09-15 17:06:35 -0400531 struct rb_node *parent = NULL;
532 struct throtl_grp *__tg;
533 unsigned long key = tg->disptime;
534 int left = 1;
535
536 while (*node != NULL) {
537 parent = *node;
538 __tg = rb_entry_tg(parent);
539
540 if (time_before(key, __tg->disptime))
541 node = &parent->rb_left;
542 else {
543 node = &parent->rb_right;
544 left = 0;
545 }
546 }
547
548 if (left)
Tejun Heo0049af72013-05-14 13:52:33 -0700549 parent_sq->first_pending = &tg->rb_node;
Vivek Goyale43473b2010-09-15 17:06:35 -0400550
551 rb_link_node(&tg->rb_node, parent, node);
Tejun Heo0049af72013-05-14 13:52:33 -0700552 rb_insert_color(&tg->rb_node, &parent_sq->pending_tree);
Vivek Goyale43473b2010-09-15 17:06:35 -0400553}
554
Tejun Heo77216b02013-05-14 13:52:36 -0700555static void __throtl_enqueue_tg(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400556{
Tejun Heo77216b02013-05-14 13:52:36 -0700557 tg_service_queue_add(tg);
Tejun Heo5b2c16a2013-05-14 13:52:32 -0700558 tg->flags |= THROTL_TG_PENDING;
Tejun Heo77216b02013-05-14 13:52:36 -0700559 tg->service_queue.parent_sq->nr_pending++;
Vivek Goyale43473b2010-09-15 17:06:35 -0400560}
561
Tejun Heo77216b02013-05-14 13:52:36 -0700562static void throtl_enqueue_tg(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400563{
Tejun Heo5b2c16a2013-05-14 13:52:32 -0700564 if (!(tg->flags & THROTL_TG_PENDING))
Tejun Heo77216b02013-05-14 13:52:36 -0700565 __throtl_enqueue_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -0400566}
567
Tejun Heo77216b02013-05-14 13:52:36 -0700568static void __throtl_dequeue_tg(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400569{
Tejun Heo77216b02013-05-14 13:52:36 -0700570 throtl_rb_erase(&tg->rb_node, tg->service_queue.parent_sq);
Tejun Heo5b2c16a2013-05-14 13:52:32 -0700571 tg->flags &= ~THROTL_TG_PENDING;
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 Heo5b2c16a2013-05-14 13:52:32 -0700576 if (tg->flags & THROTL_TG_PENDING)
Tejun Heo77216b02013-05-14 13:52:36 -0700577 __throtl_dequeue_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -0400578}
579
Tejun Heoa9131a22013-05-14 13:52:31 -0700580/* Call with queue lock held */
Tejun Heo69df0ab2013-05-14 13:52:36 -0700581static void throtl_schedule_pending_timer(struct throtl_service_queue *sq,
582 unsigned long expires)
Tejun Heoa9131a22013-05-14 13:52:31 -0700583{
Tejun Heo69df0ab2013-05-14 13:52:36 -0700584 mod_timer(&sq->pending_timer, expires);
585 throtl_log(sq, "schedule timer. delay=%lu jiffies=%lu",
586 expires - jiffies, jiffies);
Tejun Heoa9131a22013-05-14 13:52:31 -0700587}
588
Tejun Heo7f52f982013-05-14 13:52:37 -0700589/**
590 * throtl_schedule_next_dispatch - schedule the next dispatch cycle
591 * @sq: the service_queue to schedule dispatch for
592 * @force: force scheduling
593 *
594 * Arm @sq->pending_timer so that the next dispatch cycle starts on the
595 * dispatch time of the first pending child. Returns %true if either timer
596 * is armed or there's no pending child left. %false if the current
597 * dispatch window is still open and the caller should continue
598 * dispatching.
599 *
600 * If @force is %true, the dispatch timer is always scheduled and this
601 * function is guaranteed to return %true. This is to be used when the
602 * caller can't dispatch itself and needs to invoke pending_timer
603 * unconditionally. Note that forced scheduling is likely to induce short
604 * delay before dispatch starts even if @sq->first_pending_disptime is not
605 * in the future and thus shouldn't be used in hot paths.
606 */
607static bool throtl_schedule_next_dispatch(struct throtl_service_queue *sq,
608 bool force)
Vivek Goyale43473b2010-09-15 17:06:35 -0400609{
Tejun Heo6a525602013-05-14 13:52:32 -0700610 /* any pending children left? */
Tejun Heoc9e03322013-05-14 13:52:32 -0700611 if (!sq->nr_pending)
Tejun Heo7f52f982013-05-14 13:52:37 -0700612 return true;
Vivek Goyale43473b2010-09-15 17:06:35 -0400613
Tejun Heoc9e03322013-05-14 13:52:32 -0700614 update_min_dispatch_time(sq);
Vivek Goyale43473b2010-09-15 17:06:35 -0400615
Tejun Heo69df0ab2013-05-14 13:52:36 -0700616 /* is the next dispatch time in the future? */
Tejun Heo7f52f982013-05-14 13:52:37 -0700617 if (force || time_after(sq->first_pending_disptime, jiffies)) {
Tejun Heo69df0ab2013-05-14 13:52:36 -0700618 throtl_schedule_pending_timer(sq, sq->first_pending_disptime);
Tejun Heo7f52f982013-05-14 13:52:37 -0700619 return true;
Tejun Heo69df0ab2013-05-14 13:52:36 -0700620 }
621
Tejun Heo7f52f982013-05-14 13:52:37 -0700622 /* tell the caller to continue dispatching */
623 return false;
Vivek Goyale43473b2010-09-15 17:06:35 -0400624}
625
Vivek Goyal32ee5bc2013-05-14 13:52:38 -0700626static inline void throtl_start_new_slice_with_credit(struct throtl_grp *tg,
627 bool rw, unsigned long start)
628{
629 tg->bytes_disp[rw] = 0;
630 tg->io_disp[rw] = 0;
631
632 /*
633 * Previous slice has expired. We must have trimmed it after last
634 * bio dispatch. That means since start of last slice, we never used
635 * that bandwidth. Do try to make use of that bandwidth while giving
636 * credit.
637 */
638 if (time_after_eq(start, tg->slice_start[rw]))
639 tg->slice_start[rw] = start;
640
641 tg->slice_end[rw] = jiffies + throtl_slice;
642 throtl_log(&tg->service_queue,
643 "[%c] new slice with credit start=%lu end=%lu jiffies=%lu",
644 rw == READ ? 'R' : 'W', tg->slice_start[rw],
645 tg->slice_end[rw], jiffies);
646}
647
Tejun Heo0f3457f2013-05-14 13:52:32 -0700648static inline void throtl_start_new_slice(struct throtl_grp *tg, bool rw)
Vivek Goyale43473b2010-09-15 17:06:35 -0400649{
650 tg->bytes_disp[rw] = 0;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400651 tg->io_disp[rw] = 0;
Vivek Goyale43473b2010-09-15 17:06:35 -0400652 tg->slice_start[rw] = jiffies;
653 tg->slice_end[rw] = jiffies + throtl_slice;
Tejun Heofda6f272013-05-14 13:52:36 -0700654 throtl_log(&tg->service_queue,
655 "[%c] new slice start=%lu end=%lu jiffies=%lu",
656 rw == READ ? 'R' : 'W', tg->slice_start[rw],
657 tg->slice_end[rw], jiffies);
Vivek Goyale43473b2010-09-15 17:06:35 -0400658}
659
Tejun Heo0f3457f2013-05-14 13:52:32 -0700660static inline void throtl_set_slice_end(struct throtl_grp *tg, bool rw,
661 unsigned long jiffy_end)
Vivek Goyald1ae8ff2010-12-01 19:34:46 +0100662{
663 tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
664}
665
Tejun Heo0f3457f2013-05-14 13:52:32 -0700666static inline void throtl_extend_slice(struct throtl_grp *tg, bool rw,
667 unsigned long jiffy_end)
Vivek Goyale43473b2010-09-15 17:06:35 -0400668{
669 tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
Tejun Heofda6f272013-05-14 13:52:36 -0700670 throtl_log(&tg->service_queue,
671 "[%c] extend slice start=%lu end=%lu jiffies=%lu",
672 rw == READ ? 'R' : 'W', tg->slice_start[rw],
673 tg->slice_end[rw], jiffies);
Vivek Goyale43473b2010-09-15 17:06:35 -0400674}
675
676/* Determine if previously allocated or extended slice is complete or not */
Tejun Heo0f3457f2013-05-14 13:52:32 -0700677static bool throtl_slice_used(struct throtl_grp *tg, bool rw)
Vivek Goyale43473b2010-09-15 17:06:35 -0400678{
679 if (time_in_range(jiffies, tg->slice_start[rw], tg->slice_end[rw]))
Fabian Frederick5cf8c222014-05-02 18:28:17 +0200680 return false;
Vivek Goyale43473b2010-09-15 17:06:35 -0400681
682 return 1;
683}
684
685/* Trim the used slices and adjust slice start accordingly */
Tejun Heo0f3457f2013-05-14 13:52:32 -0700686static inline void throtl_trim_slice(struct throtl_grp *tg, bool rw)
Vivek Goyale43473b2010-09-15 17:06:35 -0400687{
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200688 unsigned long nr_slices, time_elapsed, io_trim;
689 u64 bytes_trim, tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400690
691 BUG_ON(time_before(tg->slice_end[rw], tg->slice_start[rw]));
692
693 /*
694 * If bps are unlimited (-1), then time slice don't get
695 * renewed. Don't try to trim the slice if slice is used. A new
696 * slice will start when appropriate.
697 */
Tejun Heo0f3457f2013-05-14 13:52:32 -0700698 if (throtl_slice_used(tg, rw))
Vivek Goyale43473b2010-09-15 17:06:35 -0400699 return;
700
Vivek Goyald1ae8ff2010-12-01 19:34:46 +0100701 /*
702 * A bio has been dispatched. Also adjust slice_end. It might happen
703 * that initially cgroup limit was very low resulting in high
704 * slice_end, but later limit was bumped up and bio was dispached
705 * sooner, then we need to reduce slice_end. A high bogus slice_end
706 * is bad because it does not allow new slice to start.
707 */
708
Tejun Heo0f3457f2013-05-14 13:52:32 -0700709 throtl_set_slice_end(tg, rw, jiffies + throtl_slice);
Vivek Goyald1ae8ff2010-12-01 19:34:46 +0100710
Vivek Goyale43473b2010-09-15 17:06:35 -0400711 time_elapsed = jiffies - tg->slice_start[rw];
712
713 nr_slices = time_elapsed / throtl_slice;
714
715 if (!nr_slices)
716 return;
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200717 tmp = tg->bps[rw] * throtl_slice * nr_slices;
718 do_div(tmp, HZ);
719 bytes_trim = tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400720
Vivek Goyal8e89d132010-09-15 17:06:37 -0400721 io_trim = (tg->iops[rw] * throtl_slice * nr_slices)/HZ;
Vivek Goyale43473b2010-09-15 17:06:35 -0400722
Vivek Goyal8e89d132010-09-15 17:06:37 -0400723 if (!bytes_trim && !io_trim)
Vivek Goyale43473b2010-09-15 17:06:35 -0400724 return;
725
726 if (tg->bytes_disp[rw] >= bytes_trim)
727 tg->bytes_disp[rw] -= bytes_trim;
728 else
729 tg->bytes_disp[rw] = 0;
730
Vivek Goyal8e89d132010-09-15 17:06:37 -0400731 if (tg->io_disp[rw] >= io_trim)
732 tg->io_disp[rw] -= io_trim;
733 else
734 tg->io_disp[rw] = 0;
735
Vivek Goyale43473b2010-09-15 17:06:35 -0400736 tg->slice_start[rw] += nr_slices * throtl_slice;
737
Tejun Heofda6f272013-05-14 13:52:36 -0700738 throtl_log(&tg->service_queue,
739 "[%c] trim slice nr=%lu bytes=%llu io=%lu start=%lu end=%lu jiffies=%lu",
740 rw == READ ? 'R' : 'W', nr_slices, bytes_trim, io_trim,
741 tg->slice_start[rw], tg->slice_end[rw], jiffies);
Vivek Goyale43473b2010-09-15 17:06:35 -0400742}
743
Tejun Heo0f3457f2013-05-14 13:52:32 -0700744static bool tg_with_in_iops_limit(struct throtl_grp *tg, struct bio *bio,
745 unsigned long *wait)
Vivek Goyale43473b2010-09-15 17:06:35 -0400746{
747 bool rw = bio_data_dir(bio);
Vivek Goyal8e89d132010-09-15 17:06:37 -0400748 unsigned int io_allowed;
Vivek Goyale43473b2010-09-15 17:06:35 -0400749 unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
Vivek Goyalc49c06e2010-10-01 21:16:42 +0200750 u64 tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400751
Vivek Goyal8e89d132010-09-15 17:06:37 -0400752 jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
Vivek Goyale43473b2010-09-15 17:06:35 -0400753
Vivek Goyal8e89d132010-09-15 17:06:37 -0400754 /* Slice has just started. Consider one slice interval */
755 if (!jiffy_elapsed)
756 jiffy_elapsed_rnd = throtl_slice;
757
758 jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
759
Vivek Goyalc49c06e2010-10-01 21:16:42 +0200760 /*
761 * jiffy_elapsed_rnd should not be a big value as minimum iops can be
762 * 1 then at max jiffy elapsed should be equivalent of 1 second as we
763 * will allow dispatch after 1 second and after that slice should
764 * have been trimmed.
765 */
766
767 tmp = (u64)tg->iops[rw] * jiffy_elapsed_rnd;
768 do_div(tmp, HZ);
769
770 if (tmp > UINT_MAX)
771 io_allowed = UINT_MAX;
772 else
773 io_allowed = tmp;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400774
775 if (tg->io_disp[rw] + 1 <= io_allowed) {
Vivek Goyale43473b2010-09-15 17:06:35 -0400776 if (wait)
777 *wait = 0;
Fabian Frederick5cf8c222014-05-02 18:28:17 +0200778 return true;
Vivek Goyale43473b2010-09-15 17:06:35 -0400779 }
780
Vivek Goyal8e89d132010-09-15 17:06:37 -0400781 /* Calc approx time to dispatch */
782 jiffy_wait = ((tg->io_disp[rw] + 1) * HZ)/tg->iops[rw] + 1;
783
784 if (jiffy_wait > jiffy_elapsed)
785 jiffy_wait = jiffy_wait - jiffy_elapsed;
786 else
787 jiffy_wait = 1;
788
789 if (wait)
790 *wait = jiffy_wait;
791 return 0;
792}
793
Tejun Heo0f3457f2013-05-14 13:52:32 -0700794static bool tg_with_in_bps_limit(struct throtl_grp *tg, struct bio *bio,
795 unsigned long *wait)
Vivek Goyal8e89d132010-09-15 17:06:37 -0400796{
797 bool rw = bio_data_dir(bio);
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200798 u64 bytes_allowed, extra_bytes, tmp;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400799 unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
Vivek Goyale43473b2010-09-15 17:06:35 -0400800
801 jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
802
803 /* Slice has just started. Consider one slice interval */
804 if (!jiffy_elapsed)
805 jiffy_elapsed_rnd = throtl_slice;
806
807 jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
808
Vivek Goyal5e901a22010-10-01 21:16:38 +0200809 tmp = tg->bps[rw] * jiffy_elapsed_rnd;
810 do_div(tmp, HZ);
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200811 bytes_allowed = tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400812
Kent Overstreet4f024f32013-10-11 15:44:27 -0700813 if (tg->bytes_disp[rw] + bio->bi_iter.bi_size <= bytes_allowed) {
Vivek Goyale43473b2010-09-15 17:06:35 -0400814 if (wait)
815 *wait = 0;
Fabian Frederick5cf8c222014-05-02 18:28:17 +0200816 return true;
Vivek Goyale43473b2010-09-15 17:06:35 -0400817 }
818
819 /* Calc approx time to dispatch */
Kent Overstreet4f024f32013-10-11 15:44:27 -0700820 extra_bytes = tg->bytes_disp[rw] + bio->bi_iter.bi_size - bytes_allowed;
Vivek Goyale43473b2010-09-15 17:06:35 -0400821 jiffy_wait = div64_u64(extra_bytes * HZ, tg->bps[rw]);
822
823 if (!jiffy_wait)
824 jiffy_wait = 1;
825
826 /*
827 * This wait time is without taking into consideration the rounding
828 * up we did. Add that time also.
829 */
830 jiffy_wait = jiffy_wait + (jiffy_elapsed_rnd - jiffy_elapsed);
Vivek Goyale43473b2010-09-15 17:06:35 -0400831 if (wait)
832 *wait = jiffy_wait;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400833 return 0;
834}
Vivek Goyale43473b2010-09-15 17:06:35 -0400835
Vivek Goyal8e89d132010-09-15 17:06:37 -0400836/*
837 * Returns whether one can dispatch a bio or not. Also returns approx number
838 * of jiffies to wait before this bio is with-in IO rate and can be dispatched
839 */
Tejun Heo0f3457f2013-05-14 13:52:32 -0700840static bool tg_may_dispatch(struct throtl_grp *tg, struct bio *bio,
841 unsigned long *wait)
Vivek Goyal8e89d132010-09-15 17:06:37 -0400842{
843 bool rw = bio_data_dir(bio);
844 unsigned long bps_wait = 0, iops_wait = 0, max_wait = 0;
845
846 /*
847 * Currently whole state machine of group depends on first bio
848 * queued in the group bio list. So one should not be calling
849 * this function with a different bio if there are other bios
850 * queued.
851 */
Tejun Heo73f0d492013-05-14 13:52:35 -0700852 BUG_ON(tg->service_queue.nr_queued[rw] &&
Tejun Heoc5cc2072013-05-14 13:52:38 -0700853 bio != throtl_peek_queued(&tg->service_queue.queued[rw]));
Vivek Goyal8e89d132010-09-15 17:06:37 -0400854
855 /* If tg->bps = -1, then BW is unlimited */
856 if (tg->bps[rw] == -1 && tg->iops[rw] == -1) {
857 if (wait)
858 *wait = 0;
Fabian Frederick5cf8c222014-05-02 18:28:17 +0200859 return true;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400860 }
861
862 /*
863 * If previous slice expired, start a new one otherwise renew/extend
864 * existing slice to make sure it is at least throtl_slice interval
865 * long since now.
866 */
Tejun Heo0f3457f2013-05-14 13:52:32 -0700867 if (throtl_slice_used(tg, rw))
868 throtl_start_new_slice(tg, rw);
Vivek Goyal8e89d132010-09-15 17:06:37 -0400869 else {
870 if (time_before(tg->slice_end[rw], jiffies + throtl_slice))
Tejun Heo0f3457f2013-05-14 13:52:32 -0700871 throtl_extend_slice(tg, rw, jiffies + throtl_slice);
Vivek Goyal8e89d132010-09-15 17:06:37 -0400872 }
873
Tejun Heo0f3457f2013-05-14 13:52:32 -0700874 if (tg_with_in_bps_limit(tg, bio, &bps_wait) &&
875 tg_with_in_iops_limit(tg, bio, &iops_wait)) {
Vivek Goyal8e89d132010-09-15 17:06:37 -0400876 if (wait)
877 *wait = 0;
878 return 1;
879 }
880
881 max_wait = max(bps_wait, iops_wait);
882
883 if (wait)
884 *wait = max_wait;
885
886 if (time_before(tg->slice_end[rw], jiffies + max_wait))
Tejun Heo0f3457f2013-05-14 13:52:32 -0700887 throtl_extend_slice(tg, rw, jiffies + max_wait);
Vivek Goyale43473b2010-09-15 17:06:35 -0400888
889 return 0;
890}
891
Tejun Heo3c798392012-04-16 13:57:25 -0700892static void throtl_update_dispatch_stats(struct blkcg_gq *blkg, u64 bytes,
Tejun Heo629ed0b2012-04-01 14:38:44 -0700893 int rw)
894{
Tejun Heo8a3d2612012-04-01 14:38:44 -0700895 struct throtl_grp *tg = blkg_to_tg(blkg);
896 struct tg_stats_cpu *stats_cpu;
Tejun Heo629ed0b2012-04-01 14:38:44 -0700897 unsigned long flags;
898
Tejun Heo629ed0b2012-04-01 14:38:44 -0700899 /*
900 * Disabling interrupts to provide mutual exclusion between two
901 * writes on same cpu. It probably is not needed for 64bit. Not
902 * optimizing that case yet.
903 */
904 local_irq_save(flags);
905
Tejun Heo8a3d2612012-04-01 14:38:44 -0700906 stats_cpu = this_cpu_ptr(tg->stats_cpu);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700907
Tejun Heo629ed0b2012-04-01 14:38:44 -0700908 blkg_rwstat_add(&stats_cpu->serviced, rw, 1);
909 blkg_rwstat_add(&stats_cpu->service_bytes, rw, bytes);
910
911 local_irq_restore(flags);
912}
913
Vivek Goyale43473b2010-09-15 17:06:35 -0400914static void throtl_charge_bio(struct throtl_grp *tg, struct bio *bio)
915{
916 bool rw = bio_data_dir(bio);
Vivek Goyale43473b2010-09-15 17:06:35 -0400917
918 /* Charge the bio to the group */
Kent Overstreet4f024f32013-10-11 15:44:27 -0700919 tg->bytes_disp[rw] += bio->bi_iter.bi_size;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400920 tg->io_disp[rw]++;
Vivek Goyale43473b2010-09-15 17:06:35 -0400921
Tejun Heo2a0f61e2013-05-14 13:52:36 -0700922 /*
923 * REQ_THROTTLED is used to prevent the same bio to be throttled
924 * more than once as a throttled bio will go through blk-throtl the
925 * second time when it eventually gets issued. Set it when a bio
926 * is being charged to a tg.
927 *
928 * Dispatch stats aren't recursive and each @bio should only be
929 * accounted by the @tg it was originally associated with. Let's
930 * update the stats when setting REQ_THROTTLED for the first time
931 * which is guaranteed to be for the @bio's original tg.
932 */
933 if (!(bio->bi_rw & REQ_THROTTLED)) {
934 bio->bi_rw |= REQ_THROTTLED;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700935 throtl_update_dispatch_stats(tg_to_blkg(tg),
936 bio->bi_iter.bi_size, bio->bi_rw);
Tejun Heo2a0f61e2013-05-14 13:52:36 -0700937 }
Vivek Goyale43473b2010-09-15 17:06:35 -0400938}
939
Tejun Heoc5cc2072013-05-14 13:52:38 -0700940/**
941 * throtl_add_bio_tg - add a bio to the specified throtl_grp
942 * @bio: bio to add
943 * @qn: qnode to use
944 * @tg: the target throtl_grp
945 *
946 * Add @bio to @tg's service_queue using @qn. If @qn is not specified,
947 * tg->qnode_on_self[] is used.
948 */
949static void throtl_add_bio_tg(struct bio *bio, struct throtl_qnode *qn,
950 struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400951{
Tejun Heo73f0d492013-05-14 13:52:35 -0700952 struct throtl_service_queue *sq = &tg->service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -0400953 bool rw = bio_data_dir(bio);
954
Tejun Heoc5cc2072013-05-14 13:52:38 -0700955 if (!qn)
956 qn = &tg->qnode_on_self[rw];
957
Tejun Heo0e9f4162013-05-14 13:52:35 -0700958 /*
959 * If @tg doesn't currently have any bios queued in the same
960 * direction, queueing @bio can change when @tg should be
961 * dispatched. Mark that @tg was empty. This is automatically
962 * cleaered on the next tg_update_disptime().
963 */
964 if (!sq->nr_queued[rw])
965 tg->flags |= THROTL_TG_WAS_EMPTY;
966
Tejun Heoc5cc2072013-05-14 13:52:38 -0700967 throtl_qnode_add_bio(bio, qn, &sq->queued[rw]);
968
Tejun Heo73f0d492013-05-14 13:52:35 -0700969 sq->nr_queued[rw]++;
Tejun Heo77216b02013-05-14 13:52:36 -0700970 throtl_enqueue_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -0400971}
972
Tejun Heo77216b02013-05-14 13:52:36 -0700973static void tg_update_disptime(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400974{
Tejun Heo73f0d492013-05-14 13:52:35 -0700975 struct throtl_service_queue *sq = &tg->service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -0400976 unsigned long read_wait = -1, write_wait = -1, min_wait = -1, disptime;
977 struct bio *bio;
978
Tejun Heoc5cc2072013-05-14 13:52:38 -0700979 if ((bio = throtl_peek_queued(&sq->queued[READ])))
Tejun Heo0f3457f2013-05-14 13:52:32 -0700980 tg_may_dispatch(tg, bio, &read_wait);
Vivek Goyale43473b2010-09-15 17:06:35 -0400981
Tejun Heoc5cc2072013-05-14 13:52:38 -0700982 if ((bio = throtl_peek_queued(&sq->queued[WRITE])))
Tejun Heo0f3457f2013-05-14 13:52:32 -0700983 tg_may_dispatch(tg, bio, &write_wait);
Vivek Goyale43473b2010-09-15 17:06:35 -0400984
985 min_wait = min(read_wait, write_wait);
986 disptime = jiffies + min_wait;
987
Vivek Goyale43473b2010-09-15 17:06:35 -0400988 /* Update dispatch time */
Tejun Heo77216b02013-05-14 13:52:36 -0700989 throtl_dequeue_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -0400990 tg->disptime = disptime;
Tejun Heo77216b02013-05-14 13:52:36 -0700991 throtl_enqueue_tg(tg);
Tejun Heo0e9f4162013-05-14 13:52:35 -0700992
993 /* see throtl_add_bio_tg() */
994 tg->flags &= ~THROTL_TG_WAS_EMPTY;
Vivek Goyale43473b2010-09-15 17:06:35 -0400995}
996
Vivek Goyal32ee5bc2013-05-14 13:52:38 -0700997static void start_parent_slice_with_credit(struct throtl_grp *child_tg,
998 struct throtl_grp *parent_tg, bool rw)
999{
1000 if (throtl_slice_used(parent_tg, rw)) {
1001 throtl_start_new_slice_with_credit(parent_tg, rw,
1002 child_tg->slice_start[rw]);
1003 }
1004
1005}
1006
Tejun Heo77216b02013-05-14 13:52:36 -07001007static void tg_dispatch_one_bio(struct throtl_grp *tg, bool rw)
Vivek Goyale43473b2010-09-15 17:06:35 -04001008{
Tejun Heo73f0d492013-05-14 13:52:35 -07001009 struct throtl_service_queue *sq = &tg->service_queue;
Tejun Heo6bc9c2b2013-05-14 13:52:38 -07001010 struct throtl_service_queue *parent_sq = sq->parent_sq;
1011 struct throtl_grp *parent_tg = sq_to_tg(parent_sq);
Tejun Heoc5cc2072013-05-14 13:52:38 -07001012 struct throtl_grp *tg_to_put = NULL;
Vivek Goyale43473b2010-09-15 17:06:35 -04001013 struct bio *bio;
1014
Tejun Heoc5cc2072013-05-14 13:52:38 -07001015 /*
1016 * @bio is being transferred from @tg to @parent_sq. Popping a bio
1017 * from @tg may put its reference and @parent_sq might end up
1018 * getting released prematurely. Remember the tg to put and put it
1019 * after @bio is transferred to @parent_sq.
1020 */
1021 bio = throtl_pop_queued(&sq->queued[rw], &tg_to_put);
Tejun Heo73f0d492013-05-14 13:52:35 -07001022 sq->nr_queued[rw]--;
Vivek Goyale43473b2010-09-15 17:06:35 -04001023
1024 throtl_charge_bio(tg, bio);
Tejun Heo6bc9c2b2013-05-14 13:52:38 -07001025
1026 /*
1027 * If our parent is another tg, we just need to transfer @bio to
1028 * the parent using throtl_add_bio_tg(). If our parent is
1029 * @td->service_queue, @bio is ready to be issued. Put it on its
1030 * bio_lists[] and decrease total number queued. The caller is
1031 * responsible for issuing these bios.
1032 */
1033 if (parent_tg) {
Tejun Heoc5cc2072013-05-14 13:52:38 -07001034 throtl_add_bio_tg(bio, &tg->qnode_on_parent[rw], parent_tg);
Vivek Goyal32ee5bc2013-05-14 13:52:38 -07001035 start_parent_slice_with_credit(tg, parent_tg, rw);
Tejun Heo6bc9c2b2013-05-14 13:52:38 -07001036 } else {
Tejun Heoc5cc2072013-05-14 13:52:38 -07001037 throtl_qnode_add_bio(bio, &tg->qnode_on_parent[rw],
1038 &parent_sq->queued[rw]);
Tejun Heo6bc9c2b2013-05-14 13:52:38 -07001039 BUG_ON(tg->td->nr_queued[rw] <= 0);
1040 tg->td->nr_queued[rw]--;
1041 }
Vivek Goyale43473b2010-09-15 17:06:35 -04001042
Tejun Heo0f3457f2013-05-14 13:52:32 -07001043 throtl_trim_slice(tg, rw);
Tejun Heo6bc9c2b2013-05-14 13:52:38 -07001044
Tejun Heoc5cc2072013-05-14 13:52:38 -07001045 if (tg_to_put)
1046 blkg_put(tg_to_blkg(tg_to_put));
Vivek Goyale43473b2010-09-15 17:06:35 -04001047}
1048
Tejun Heo77216b02013-05-14 13:52:36 -07001049static int throtl_dispatch_tg(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -04001050{
Tejun Heo73f0d492013-05-14 13:52:35 -07001051 struct throtl_service_queue *sq = &tg->service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -04001052 unsigned int nr_reads = 0, nr_writes = 0;
1053 unsigned int max_nr_reads = throtl_grp_quantum*3/4;
Vivek Goyalc2f68052010-11-15 19:32:42 +01001054 unsigned int max_nr_writes = throtl_grp_quantum - max_nr_reads;
Vivek Goyale43473b2010-09-15 17:06:35 -04001055 struct bio *bio;
1056
1057 /* Try to dispatch 75% READS and 25% WRITES */
1058
Tejun Heoc5cc2072013-05-14 13:52:38 -07001059 while ((bio = throtl_peek_queued(&sq->queued[READ])) &&
Tejun Heo0f3457f2013-05-14 13:52:32 -07001060 tg_may_dispatch(tg, bio, NULL)) {
Vivek Goyale43473b2010-09-15 17:06:35 -04001061
Tejun Heo77216b02013-05-14 13:52:36 -07001062 tg_dispatch_one_bio(tg, bio_data_dir(bio));
Vivek Goyale43473b2010-09-15 17:06:35 -04001063 nr_reads++;
1064
1065 if (nr_reads >= max_nr_reads)
1066 break;
1067 }
1068
Tejun Heoc5cc2072013-05-14 13:52:38 -07001069 while ((bio = throtl_peek_queued(&sq->queued[WRITE])) &&
Tejun Heo0f3457f2013-05-14 13:52:32 -07001070 tg_may_dispatch(tg, bio, NULL)) {
Vivek Goyale43473b2010-09-15 17:06:35 -04001071
Tejun Heo77216b02013-05-14 13:52:36 -07001072 tg_dispatch_one_bio(tg, bio_data_dir(bio));
Vivek Goyale43473b2010-09-15 17:06:35 -04001073 nr_writes++;
1074
1075 if (nr_writes >= max_nr_writes)
1076 break;
1077 }
1078
1079 return nr_reads + nr_writes;
1080}
1081
Tejun Heo651930b2013-05-14 13:52:35 -07001082static int throtl_select_dispatch(struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -04001083{
1084 unsigned int nr_disp = 0;
Vivek Goyale43473b2010-09-15 17:06:35 -04001085
1086 while (1) {
Tejun Heo73f0d492013-05-14 13:52:35 -07001087 struct throtl_grp *tg = throtl_rb_first(parent_sq);
1088 struct throtl_service_queue *sq = &tg->service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -04001089
1090 if (!tg)
1091 break;
1092
1093 if (time_before(jiffies, tg->disptime))
1094 break;
1095
Tejun Heo77216b02013-05-14 13:52:36 -07001096 throtl_dequeue_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -04001097
Tejun Heo77216b02013-05-14 13:52:36 -07001098 nr_disp += throtl_dispatch_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -04001099
Tejun Heo73f0d492013-05-14 13:52:35 -07001100 if (sq->nr_queued[0] || sq->nr_queued[1])
Tejun Heo77216b02013-05-14 13:52:36 -07001101 tg_update_disptime(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -04001102
1103 if (nr_disp >= throtl_quantum)
1104 break;
1105 }
1106
1107 return nr_disp;
1108}
1109
Tejun Heo6e1a5702013-05-14 13:52:37 -07001110/**
1111 * throtl_pending_timer_fn - timer function for service_queue->pending_timer
1112 * @arg: the throtl_service_queue being serviced
1113 *
1114 * This timer is armed when a child throtl_grp with active bio's become
1115 * pending and queued on the service_queue's pending_tree and expires when
1116 * the first child throtl_grp should be dispatched. This function
Tejun Heo2e48a532013-05-14 13:52:38 -07001117 * dispatches bio's from the children throtl_grps to the parent
1118 * service_queue.
1119 *
1120 * If the parent's parent is another throtl_grp, dispatching is propagated
1121 * by either arming its pending_timer or repeating dispatch directly. If
1122 * the top-level service_tree is reached, throtl_data->dispatch_work is
1123 * kicked so that the ready bio's are issued.
Tejun Heo6e1a5702013-05-14 13:52:37 -07001124 */
Tejun Heo69df0ab2013-05-14 13:52:36 -07001125static void throtl_pending_timer_fn(unsigned long arg)
1126{
1127 struct throtl_service_queue *sq = (void *)arg;
Tejun Heo2e48a532013-05-14 13:52:38 -07001128 struct throtl_grp *tg = sq_to_tg(sq);
Tejun Heo69df0ab2013-05-14 13:52:36 -07001129 struct throtl_data *td = sq_to_td(sq);
Tejun Heocb761992013-05-14 13:52:31 -07001130 struct request_queue *q = td->queue;
Tejun Heo2e48a532013-05-14 13:52:38 -07001131 struct throtl_service_queue *parent_sq;
1132 bool dispatched;
Tejun Heo6e1a5702013-05-14 13:52:37 -07001133 int ret;
Vivek Goyale43473b2010-09-15 17:06:35 -04001134
1135 spin_lock_irq(q->queue_lock);
Tejun Heo2e48a532013-05-14 13:52:38 -07001136again:
1137 parent_sq = sq->parent_sq;
1138 dispatched = false;
Vivek Goyale43473b2010-09-15 17:06:35 -04001139
Tejun Heo7f52f982013-05-14 13:52:37 -07001140 while (true) {
1141 throtl_log(sq, "dispatch nr_queued=%u read=%u write=%u",
Tejun Heo2e48a532013-05-14 13:52:38 -07001142 sq->nr_queued[READ] + sq->nr_queued[WRITE],
1143 sq->nr_queued[READ], sq->nr_queued[WRITE]);
Vivek Goyale43473b2010-09-15 17:06:35 -04001144
Tejun Heo7f52f982013-05-14 13:52:37 -07001145 ret = throtl_select_dispatch(sq);
1146 if (ret) {
Tejun Heo7f52f982013-05-14 13:52:37 -07001147 throtl_log(sq, "bios disp=%u", ret);
1148 dispatched = true;
Tejun Heo651930b2013-05-14 13:52:35 -07001149 }
Vivek Goyale43473b2010-09-15 17:06:35 -04001150
Tejun Heo7f52f982013-05-14 13:52:37 -07001151 if (throtl_schedule_next_dispatch(sq, false))
1152 break;
1153
1154 /* this dispatch windows is still open, relax and repeat */
1155 spin_unlock_irq(q->queue_lock);
1156 cpu_relax();
1157 spin_lock_irq(q->queue_lock);
1158 }
Tejun Heo6a525602013-05-14 13:52:32 -07001159
Tejun Heo2e48a532013-05-14 13:52:38 -07001160 if (!dispatched)
1161 goto out_unlock;
Tejun Heo6e1a5702013-05-14 13:52:37 -07001162
Tejun Heo2e48a532013-05-14 13:52:38 -07001163 if (parent_sq) {
1164 /* @parent_sq is another throl_grp, propagate dispatch */
1165 if (tg->flags & THROTL_TG_WAS_EMPTY) {
1166 tg_update_disptime(tg);
1167 if (!throtl_schedule_next_dispatch(parent_sq, false)) {
1168 /* window is already open, repeat dispatching */
1169 sq = parent_sq;
1170 tg = sq_to_tg(sq);
1171 goto again;
1172 }
1173 }
1174 } else {
1175 /* reached the top-level, queue issueing */
1176 queue_work(kthrotld_workqueue, &td->dispatch_work);
1177 }
1178out_unlock:
Tejun Heo6e1a5702013-05-14 13:52:37 -07001179 spin_unlock_irq(q->queue_lock);
1180}
1181
1182/**
1183 * blk_throtl_dispatch_work_fn - work function for throtl_data->dispatch_work
1184 * @work: work item being executed
1185 *
1186 * This function is queued for execution when bio's reach the bio_lists[]
1187 * of throtl_data->service_queue. Those bio's are ready and issued by this
1188 * function.
1189 */
Fabian Frederick8876e142014-04-17 21:41:16 +02001190static void blk_throtl_dispatch_work_fn(struct work_struct *work)
Tejun Heo6e1a5702013-05-14 13:52:37 -07001191{
1192 struct throtl_data *td = container_of(work, struct throtl_data,
1193 dispatch_work);
1194 struct throtl_service_queue *td_sq = &td->service_queue;
1195 struct request_queue *q = td->queue;
1196 struct bio_list bio_list_on_stack;
1197 struct bio *bio;
1198 struct blk_plug plug;
1199 int rw;
1200
1201 bio_list_init(&bio_list_on_stack);
1202
1203 spin_lock_irq(q->queue_lock);
Tejun Heoc5cc2072013-05-14 13:52:38 -07001204 for (rw = READ; rw <= WRITE; rw++)
1205 while ((bio = throtl_pop_queued(&td_sq->queued[rw], NULL)))
1206 bio_list_add(&bio_list_on_stack, bio);
Vivek Goyale43473b2010-09-15 17:06:35 -04001207 spin_unlock_irq(q->queue_lock);
1208
Tejun Heo6e1a5702013-05-14 13:52:37 -07001209 if (!bio_list_empty(&bio_list_on_stack)) {
Vivek Goyal69d60eb2011-03-09 08:27:37 +01001210 blk_start_plug(&plug);
Vivek Goyale43473b2010-09-15 17:06:35 -04001211 while((bio = bio_list_pop(&bio_list_on_stack)))
1212 generic_make_request(bio);
Vivek Goyal69d60eb2011-03-09 08:27:37 +01001213 blk_finish_plug(&plug);
Vivek Goyale43473b2010-09-15 17:06:35 -04001214 }
Vivek Goyale43473b2010-09-15 17:06:35 -04001215}
1216
Tejun Heof95a04a2012-04-16 13:57:26 -07001217static u64 tg_prfill_cpu_rwstat(struct seq_file *sf,
1218 struct blkg_policy_data *pd, int off)
Tejun Heo41b38b62012-04-01 14:38:44 -07001219{
Tejun Heof95a04a2012-04-16 13:57:26 -07001220 struct throtl_grp *tg = pd_to_tg(pd);
Tejun Heo41b38b62012-04-01 14:38:44 -07001221 struct blkg_rwstat rwstat = { }, tmp;
1222 int i, cpu;
1223
1224 for_each_possible_cpu(cpu) {
Tejun Heo8a3d2612012-04-01 14:38:44 -07001225 struct tg_stats_cpu *sc = per_cpu_ptr(tg->stats_cpu, cpu);
Tejun Heo41b38b62012-04-01 14:38:44 -07001226
1227 tmp = blkg_rwstat_read((void *)sc + off);
1228 for (i = 0; i < BLKG_RWSTAT_NR; i++)
1229 rwstat.cnt[i] += tmp.cnt[i];
1230 }
1231
Tejun Heof95a04a2012-04-16 13:57:26 -07001232 return __blkg_prfill_rwstat(sf, pd, &rwstat);
Tejun Heo41b38b62012-04-01 14:38:44 -07001233}
1234
Tejun Heo2da8ca82013-12-05 12:28:04 -05001235static int tg_print_cpu_rwstat(struct seq_file *sf, void *v)
Tejun Heo41b38b62012-04-01 14:38:44 -07001236{
Tejun Heo2da8ca82013-12-05 12:28:04 -05001237 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), tg_prfill_cpu_rwstat,
1238 &blkcg_policy_throtl, seq_cft(sf)->private, true);
Tejun Heo41b38b62012-04-01 14:38:44 -07001239 return 0;
1240}
1241
Tejun Heof95a04a2012-04-16 13:57:26 -07001242static u64 tg_prfill_conf_u64(struct seq_file *sf, struct blkg_policy_data *pd,
1243 int off)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001244{
Tejun Heof95a04a2012-04-16 13:57:26 -07001245 struct throtl_grp *tg = pd_to_tg(pd);
1246 u64 v = *(u64 *)((void *)tg + off);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001247
Tejun Heoaf133ce2012-04-01 14:38:44 -07001248 if (v == -1)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001249 return 0;
Tejun Heof95a04a2012-04-16 13:57:26 -07001250 return __blkg_prfill_u64(sf, pd, v);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001251}
1252
Tejun Heof95a04a2012-04-16 13:57:26 -07001253static u64 tg_prfill_conf_uint(struct seq_file *sf, struct blkg_policy_data *pd,
1254 int off)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001255{
Tejun Heof95a04a2012-04-16 13:57:26 -07001256 struct throtl_grp *tg = pd_to_tg(pd);
1257 unsigned int v = *(unsigned int *)((void *)tg + off);
Tejun Heoaf133ce2012-04-01 14:38:44 -07001258
1259 if (v == -1)
1260 return 0;
Tejun Heof95a04a2012-04-16 13:57:26 -07001261 return __blkg_prfill_u64(sf, pd, v);
Tejun Heoaf133ce2012-04-01 14:38:44 -07001262}
1263
Tejun Heo2da8ca82013-12-05 12:28:04 -05001264static int tg_print_conf_u64(struct seq_file *sf, void *v)
Tejun Heoaf133ce2012-04-01 14:38:44 -07001265{
Tejun Heo2da8ca82013-12-05 12:28:04 -05001266 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), tg_prfill_conf_u64,
1267 &blkcg_policy_throtl, seq_cft(sf)->private, false);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001268 return 0;
1269}
1270
Tejun Heo2da8ca82013-12-05 12:28:04 -05001271static int tg_print_conf_uint(struct seq_file *sf, void *v)
Vivek Goyale43473b2010-09-15 17:06:35 -04001272{
Tejun Heo2da8ca82013-12-05 12:28:04 -05001273 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), tg_prfill_conf_uint,
1274 &blkcg_policy_throtl, seq_cft(sf)->private, false);
Tejun Heoaf133ce2012-04-01 14:38:44 -07001275 return 0;
Vivek Goyale43473b2010-09-15 17:06:35 -04001276}
1277
Tejun Heo451af502014-05-13 12:16:21 -04001278static ssize_t tg_set_conf(struct kernfs_open_file *of,
1279 char *buf, size_t nbytes, loff_t off, bool is_u64)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001280{
Tejun Heo451af502014-05-13 12:16:21 -04001281 struct blkcg *blkcg = css_to_blkcg(of_css(of));
Tejun Heo60c2bc22012-04-01 14:38:43 -07001282 struct blkg_conf_ctx ctx;
Tejun Heoaf133ce2012-04-01 14:38:44 -07001283 struct throtl_grp *tg;
Tejun Heo69df0ab2013-05-14 13:52:36 -07001284 struct throtl_service_queue *sq;
Tejun Heo693e7512013-05-14 13:52:38 -07001285 struct blkcg_gq *blkg;
Tejun Heo492eb212013-08-08 20:11:25 -04001286 struct cgroup_subsys_state *pos_css;
Tejun Heo60c2bc22012-04-01 14:38:43 -07001287 int ret;
1288
Tejun Heo3c798392012-04-16 13:57:25 -07001289 ret = blkg_conf_prep(blkcg, &blkcg_policy_throtl, buf, &ctx);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001290 if (ret)
1291 return ret;
1292
Tejun Heoaf133ce2012-04-01 14:38:44 -07001293 tg = blkg_to_tg(ctx.blkg);
Tejun Heo69df0ab2013-05-14 13:52:36 -07001294 sq = &tg->service_queue;
Tejun Heoaf133ce2012-04-01 14:38:44 -07001295
Tejun Heoa2b16932012-04-13 13:11:33 -07001296 if (!ctx.v)
1297 ctx.v = -1;
Tejun Heoaf133ce2012-04-01 14:38:44 -07001298
Tejun Heoa2b16932012-04-13 13:11:33 -07001299 if (is_u64)
Tejun Heo451af502014-05-13 12:16:21 -04001300 *(u64 *)((void *)tg + of_cft(of)->private) = ctx.v;
Tejun Heoa2b16932012-04-13 13:11:33 -07001301 else
Tejun Heo451af502014-05-13 12:16:21 -04001302 *(unsigned int *)((void *)tg + of_cft(of)->private) = ctx.v;
Tejun Heoaf133ce2012-04-01 14:38:44 -07001303
Tejun Heofda6f272013-05-14 13:52:36 -07001304 throtl_log(&tg->service_queue,
1305 "limit change rbps=%llu wbps=%llu riops=%u wiops=%u",
1306 tg->bps[READ], tg->bps[WRITE],
1307 tg->iops[READ], tg->iops[WRITE]);
Tejun Heo632b4492013-05-14 13:52:31 -07001308
1309 /*
Tejun Heo693e7512013-05-14 13:52:38 -07001310 * Update has_rules[] flags for the updated tg's subtree. A tg is
1311 * considered to have rules if either the tg itself or any of its
1312 * ancestors has rules. This identifies groups without any
1313 * restrictions in the whole hierarchy and allows them to bypass
1314 * blk-throttle.
1315 */
Tejun Heo492eb212013-08-08 20:11:25 -04001316 blkg_for_each_descendant_pre(blkg, pos_css, ctx.blkg)
Tejun Heo693e7512013-05-14 13:52:38 -07001317 tg_update_has_rules(blkg_to_tg(blkg));
1318
1319 /*
Tejun Heo632b4492013-05-14 13:52:31 -07001320 * We're already holding queue_lock and know @tg is valid. Let's
1321 * apply the new config directly.
1322 *
1323 * Restart the slices for both READ and WRITES. It might happen
1324 * that a group's limit are dropped suddenly and we don't want to
1325 * account recently dispatched IO with new low rate.
1326 */
Tejun Heo0f3457f2013-05-14 13:52:32 -07001327 throtl_start_new_slice(tg, 0);
1328 throtl_start_new_slice(tg, 1);
Tejun Heo632b4492013-05-14 13:52:31 -07001329
Tejun Heo5b2c16a2013-05-14 13:52:32 -07001330 if (tg->flags & THROTL_TG_PENDING) {
Tejun Heo77216b02013-05-14 13:52:36 -07001331 tg_update_disptime(tg);
Tejun Heo7f52f982013-05-14 13:52:37 -07001332 throtl_schedule_next_dispatch(sq->parent_sq, true);
Tejun Heo632b4492013-05-14 13:52:31 -07001333 }
Tejun Heo60c2bc22012-04-01 14:38:43 -07001334
1335 blkg_conf_finish(&ctx);
Tejun Heo451af502014-05-13 12:16:21 -04001336 return nbytes;
Tejun Heo60c2bc22012-04-01 14:38:43 -07001337}
1338
Tejun Heo451af502014-05-13 12:16:21 -04001339static ssize_t tg_set_conf_u64(struct kernfs_open_file *of,
1340 char *buf, size_t nbytes, loff_t off)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001341{
Tejun Heo451af502014-05-13 12:16:21 -04001342 return tg_set_conf(of, buf, nbytes, off, true);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001343}
1344
Tejun Heo451af502014-05-13 12:16:21 -04001345static ssize_t tg_set_conf_uint(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, false);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001349}
1350
1351static struct cftype throtl_files[] = {
1352 {
1353 .name = "throttle.read_bps_device",
Tejun Heoaf133ce2012-04-01 14:38:44 -07001354 .private = offsetof(struct throtl_grp, bps[READ]),
Tejun Heo2da8ca82013-12-05 12:28:04 -05001355 .seq_show = tg_print_conf_u64,
Tejun Heo451af502014-05-13 12:16:21 -04001356 .write = tg_set_conf_u64,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001357 },
1358 {
1359 .name = "throttle.write_bps_device",
Tejun Heoaf133ce2012-04-01 14:38:44 -07001360 .private = offsetof(struct throtl_grp, bps[WRITE]),
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.read_iops_device",
Tejun Heoaf133ce2012-04-01 14:38:44 -07001366 .private = offsetof(struct throtl_grp, iops[READ]),
Tejun Heo2da8ca82013-12-05 12:28:04 -05001367 .seq_show = tg_print_conf_uint,
Tejun Heo451af502014-05-13 12:16:21 -04001368 .write = tg_set_conf_uint,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001369 },
1370 {
1371 .name = "throttle.write_iops_device",
Tejun Heoaf133ce2012-04-01 14:38:44 -07001372 .private = offsetof(struct throtl_grp, iops[WRITE]),
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.io_service_bytes",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001378 .private = offsetof(struct tg_stats_cpu, service_bytes),
Tejun Heo2da8ca82013-12-05 12:28:04 -05001379 .seq_show = tg_print_cpu_rwstat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001380 },
1381 {
1382 .name = "throttle.io_serviced",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001383 .private = offsetof(struct tg_stats_cpu, serviced),
Tejun Heo2da8ca82013-12-05 12:28:04 -05001384 .seq_show = tg_print_cpu_rwstat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001385 },
1386 { } /* terminate */
1387};
1388
Vivek Goyalda527772011-03-02 19:05:33 -05001389static void throtl_shutdown_wq(struct request_queue *q)
Vivek Goyale43473b2010-09-15 17:06:35 -04001390{
1391 struct throtl_data *td = q->td;
1392
Tejun Heo69df0ab2013-05-14 13:52:36 -07001393 cancel_work_sync(&td->dispatch_work);
Vivek Goyale43473b2010-09-15 17:06:35 -04001394}
1395
Tejun Heo3c798392012-04-16 13:57:25 -07001396static struct blkcg_policy blkcg_policy_throtl = {
Tejun Heof9fcc2d2012-04-16 13:57:27 -07001397 .cftypes = throtl_files,
1398
Tejun Heo001bea72015-08-18 14:55:11 -07001399 .pd_alloc_fn = throtl_pd_alloc,
Tejun Heof9fcc2d2012-04-16 13:57:27 -07001400 .pd_init_fn = throtl_pd_init,
Tejun Heo693e7512013-05-14 13:52:38 -07001401 .pd_online_fn = throtl_pd_online,
Tejun Heo001bea72015-08-18 14:55:11 -07001402 .pd_free_fn = throtl_pd_free,
Tejun Heof9fcc2d2012-04-16 13:57:27 -07001403 .pd_reset_stats_fn = throtl_pd_reset_stats,
Vivek Goyale43473b2010-09-15 17:06:35 -04001404};
1405
Tejun Heobc16a4f2011-10-19 14:33:01 +02001406bool blk_throtl_bio(struct request_queue *q, struct bio *bio)
Vivek Goyale43473b2010-09-15 17:06:35 -04001407{
1408 struct throtl_data *td = q->td;
Tejun Heoc5cc2072013-05-14 13:52:38 -07001409 struct throtl_qnode *qn = NULL;
Vivek Goyale43473b2010-09-15 17:06:35 -04001410 struct throtl_grp *tg;
Tejun Heo73f0d492013-05-14 13:52:35 -07001411 struct throtl_service_queue *sq;
Tejun Heo0e9f4162013-05-14 13:52:35 -07001412 bool rw = bio_data_dir(bio);
Tejun Heo3c798392012-04-16 13:57:25 -07001413 struct blkcg *blkcg;
Tejun Heobc16a4f2011-10-19 14:33:01 +02001414 bool throttled = false;
Vivek Goyale43473b2010-09-15 17:06:35 -04001415
Tejun Heo2a0f61e2013-05-14 13:52:36 -07001416 /* see throtl_charge_bio() */
1417 if (bio->bi_rw & REQ_THROTTLED)
Tejun Heobc16a4f2011-10-19 14:33:01 +02001418 goto out;
Vivek Goyale43473b2010-09-15 17:06:35 -04001419
Vivek Goyalaf75cd32011-05-19 15:38:31 -04001420 /*
1421 * A throtl_grp pointer retrieved under rcu can be used to access
1422 * basic fields like stats and io rates. If a group has no rules,
1423 * just update the dispatch stats in lockless manner and return.
1424 */
Vivek Goyalaf75cd32011-05-19 15:38:31 -04001425 rcu_read_lock();
Tejun Heo3c798392012-04-16 13:57:25 -07001426 blkcg = bio_blkcg(bio);
Tejun Heocd1604f2012-03-05 13:15:06 -08001427 tg = throtl_lookup_tg(td, blkcg);
Vivek Goyalaf75cd32011-05-19 15:38:31 -04001428 if (tg) {
Tejun Heo693e7512013-05-14 13:52:38 -07001429 if (!tg->has_rules[rw]) {
Tejun Heo629ed0b2012-04-01 14:38:44 -07001430 throtl_update_dispatch_stats(tg_to_blkg(tg),
Kent Overstreet4f024f32013-10-11 15:44:27 -07001431 bio->bi_iter.bi_size, bio->bi_rw);
Tejun Heo2a7f1242012-03-05 13:15:01 -08001432 goto out_unlock_rcu;
Vivek Goyalaf75cd32011-05-19 15:38:31 -04001433 }
1434 }
Vivek Goyalaf75cd32011-05-19 15:38:31 -04001435
1436 /*
1437 * Either group has not been allocated yet or it is not an unlimited
1438 * IO group
1439 */
Vivek Goyale43473b2010-09-15 17:06:35 -04001440 spin_lock_irq(q->queue_lock);
Tejun Heoc9589f02015-08-18 14:55:19 -07001441
1442 if (unlikely(blk_queue_bypass(q)))
Tejun Heobc16a4f2011-10-19 14:33:01 +02001443 goto out_unlock;
Vivek Goyalf469a7b2011-05-19 15:38:23 -04001444
Tejun Heoc9589f02015-08-18 14:55:19 -07001445 tg = throtl_lookup_create_tg(td, blkcg);
Tejun Heo73f0d492013-05-14 13:52:35 -07001446 sq = &tg->service_queue;
1447
Tejun Heo9e660ac2013-05-14 13:52:38 -07001448 while (true) {
1449 /* throtl is FIFO - if bios are already queued, should queue */
1450 if (sq->nr_queued[rw])
1451 break;
Vivek Goyalde701c72011-03-07 21:09:32 +01001452
Tejun Heo9e660ac2013-05-14 13:52:38 -07001453 /* if above limits, break to queue */
1454 if (!tg_may_dispatch(tg, bio, NULL))
1455 break;
1456
1457 /* within limits, let's charge and dispatch directly */
Vivek Goyale43473b2010-09-15 17:06:35 -04001458 throtl_charge_bio(tg, bio);
Vivek Goyal04521db2011-03-22 21:54:29 +01001459
1460 /*
1461 * We need to trim slice even when bios are not being queued
1462 * otherwise it might happen that a bio is not queued for
1463 * a long time and slice keeps on extending and trim is not
1464 * called for a long time. Now if limits are reduced suddenly
1465 * we take into account all the IO dispatched so far at new
1466 * low rate and * newly queued IO gets a really long dispatch
1467 * time.
1468 *
1469 * So keep on trimming slice even if bio is not queued.
1470 */
Tejun Heo0f3457f2013-05-14 13:52:32 -07001471 throtl_trim_slice(tg, rw);
Tejun Heo9e660ac2013-05-14 13:52:38 -07001472
1473 /*
1474 * @bio passed through this layer without being throttled.
1475 * Climb up the ladder. If we''re already at the top, it
1476 * can be executed directly.
1477 */
Tejun Heoc5cc2072013-05-14 13:52:38 -07001478 qn = &tg->qnode_on_parent[rw];
Tejun Heo9e660ac2013-05-14 13:52:38 -07001479 sq = sq->parent_sq;
1480 tg = sq_to_tg(sq);
1481 if (!tg)
1482 goto out_unlock;
Vivek Goyale43473b2010-09-15 17:06:35 -04001483 }
1484
Tejun Heo9e660ac2013-05-14 13:52:38 -07001485 /* out-of-limit, queue to @tg */
Tejun Heofda6f272013-05-14 13:52:36 -07001486 throtl_log(sq, "[%c] bio. bdisp=%llu sz=%u bps=%llu iodisp=%u iops=%u queued=%d/%d",
1487 rw == READ ? 'R' : 'W',
Kent Overstreet4f024f32013-10-11 15:44:27 -07001488 tg->bytes_disp[rw], bio->bi_iter.bi_size, tg->bps[rw],
Tejun Heofda6f272013-05-14 13:52:36 -07001489 tg->io_disp[rw], tg->iops[rw],
1490 sq->nr_queued[READ], sq->nr_queued[WRITE]);
Vivek Goyale43473b2010-09-15 17:06:35 -04001491
Tejun Heo671058f2012-03-05 13:15:29 -08001492 bio_associate_current(bio);
Tejun Heo6bc9c2b2013-05-14 13:52:38 -07001493 tg->td->nr_queued[rw]++;
Tejun Heoc5cc2072013-05-14 13:52:38 -07001494 throtl_add_bio_tg(bio, qn, tg);
Tejun Heobc16a4f2011-10-19 14:33:01 +02001495 throttled = true;
Vivek Goyale43473b2010-09-15 17:06:35 -04001496
Tejun Heo7f52f982013-05-14 13:52:37 -07001497 /*
1498 * Update @tg's dispatch time and force schedule dispatch if @tg
1499 * was empty before @bio. The forced scheduling isn't likely to
1500 * cause undue delay as @bio is likely to be dispatched directly if
1501 * its @tg's disptime is not in the future.
1502 */
Tejun Heo0e9f4162013-05-14 13:52:35 -07001503 if (tg->flags & THROTL_TG_WAS_EMPTY) {
Tejun Heo77216b02013-05-14 13:52:36 -07001504 tg_update_disptime(tg);
Tejun Heo7f52f982013-05-14 13:52:37 -07001505 throtl_schedule_next_dispatch(tg->service_queue.parent_sq, true);
Vivek Goyale43473b2010-09-15 17:06:35 -04001506 }
1507
Tejun Heobc16a4f2011-10-19 14:33:01 +02001508out_unlock:
Vivek Goyale43473b2010-09-15 17:06:35 -04001509 spin_unlock_irq(q->queue_lock);
Tejun Heo2a7f1242012-03-05 13:15:01 -08001510out_unlock_rcu:
1511 rcu_read_unlock();
Tejun Heobc16a4f2011-10-19 14:33:01 +02001512out:
Tejun Heo2a0f61e2013-05-14 13:52:36 -07001513 /*
1514 * As multiple blk-throtls may stack in the same issue path, we
1515 * don't want bios to leave with the flag set. Clear the flag if
1516 * being issued.
1517 */
1518 if (!throttled)
1519 bio->bi_rw &= ~REQ_THROTTLED;
Tejun Heobc16a4f2011-10-19 14:33:01 +02001520 return throttled;
Vivek Goyale43473b2010-09-15 17:06:35 -04001521}
1522
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001523/*
1524 * Dispatch all bios from all children tg's queued on @parent_sq. On
1525 * return, @parent_sq is guaranteed to not have any active children tg's
1526 * and all bios from previously active tg's are on @parent_sq->bio_lists[].
1527 */
1528static void tg_drain_bios(struct throtl_service_queue *parent_sq)
1529{
1530 struct throtl_grp *tg;
1531
1532 while ((tg = throtl_rb_first(parent_sq))) {
1533 struct throtl_service_queue *sq = &tg->service_queue;
1534 struct bio *bio;
1535
1536 throtl_dequeue_tg(tg);
1537
Tejun Heoc5cc2072013-05-14 13:52:38 -07001538 while ((bio = throtl_peek_queued(&sq->queued[READ])))
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001539 tg_dispatch_one_bio(tg, bio_data_dir(bio));
Tejun Heoc5cc2072013-05-14 13:52:38 -07001540 while ((bio = throtl_peek_queued(&sq->queued[WRITE])))
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001541 tg_dispatch_one_bio(tg, bio_data_dir(bio));
1542 }
1543}
1544
Tejun Heoc9a929d2011-10-19 14:42:16 +02001545/**
1546 * blk_throtl_drain - drain throttled bios
1547 * @q: request_queue to drain throttled bios for
1548 *
1549 * Dispatch all currently throttled bios on @q through ->make_request_fn().
1550 */
1551void blk_throtl_drain(struct request_queue *q)
1552 __releases(q->queue_lock) __acquires(q->queue_lock)
1553{
1554 struct throtl_data *td = q->td;
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001555 struct blkcg_gq *blkg;
Tejun Heo492eb212013-08-08 20:11:25 -04001556 struct cgroup_subsys_state *pos_css;
Tejun Heoc9a929d2011-10-19 14:42:16 +02001557 struct bio *bio;
Tejun Heo651930b2013-05-14 13:52:35 -07001558 int rw;
Tejun Heoc9a929d2011-10-19 14:42:16 +02001559
Andi Kleen8bcb6c72012-03-30 12:33:28 +02001560 queue_lockdep_assert_held(q);
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001561 rcu_read_lock();
Tejun Heoc9a929d2011-10-19 14:42:16 +02001562
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001563 /*
1564 * Drain each tg while doing post-order walk on the blkg tree, so
1565 * that all bios are propagated to td->service_queue. It'd be
1566 * better to walk service_queue tree directly but blkg walk is
1567 * easier.
1568 */
Tejun Heo492eb212013-08-08 20:11:25 -04001569 blkg_for_each_descendant_post(blkg, pos_css, td->queue->root_blkg)
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001570 tg_drain_bios(&blkg_to_tg(blkg)->service_queue);
Tejun Heo73f0d492013-05-14 13:52:35 -07001571
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001572 /* finally, transfer bios from top-level tg's into the td */
1573 tg_drain_bios(&td->service_queue);
1574
1575 rcu_read_unlock();
Tejun Heoc9a929d2011-10-19 14:42:16 +02001576 spin_unlock_irq(q->queue_lock);
1577
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001578 /* all bios now should be in td->service_queue, issue them */
Tejun Heo651930b2013-05-14 13:52:35 -07001579 for (rw = READ; rw <= WRITE; rw++)
Tejun Heoc5cc2072013-05-14 13:52:38 -07001580 while ((bio = throtl_pop_queued(&td->service_queue.queued[rw],
1581 NULL)))
Tejun Heo651930b2013-05-14 13:52:35 -07001582 generic_make_request(bio);
Tejun Heoc9a929d2011-10-19 14:42:16 +02001583
1584 spin_lock_irq(q->queue_lock);
1585}
1586
Vivek Goyale43473b2010-09-15 17:06:35 -04001587int blk_throtl_init(struct request_queue *q)
1588{
1589 struct throtl_data *td;
Tejun Heoa2b16932012-04-13 13:11:33 -07001590 int ret;
Vivek Goyale43473b2010-09-15 17:06:35 -04001591
1592 td = kzalloc_node(sizeof(*td), GFP_KERNEL, q->node);
1593 if (!td)
1594 return -ENOMEM;
1595
Tejun Heo69df0ab2013-05-14 13:52:36 -07001596 INIT_WORK(&td->dispatch_work, blk_throtl_dispatch_work_fn);
Tejun Heob2ce2642015-08-18 14:55:13 -07001597 throtl_service_queue_init(&td->service_queue);
Vivek Goyale43473b2010-09-15 17:06:35 -04001598
Tejun Heocd1604f2012-03-05 13:15:06 -08001599 q->td = td;
Vivek Goyal29b12582011-05-19 15:38:24 -04001600 td->queue = q;
Vivek Goyal02977e42010-10-01 14:49:48 +02001601
Tejun Heoa2b16932012-04-13 13:11:33 -07001602 /* activate policy */
Tejun Heo3c798392012-04-16 13:57:25 -07001603 ret = blkcg_activate_policy(q, &blkcg_policy_throtl);
Tejun Heoa2b16932012-04-13 13:11:33 -07001604 if (ret)
Vivek Goyal29b12582011-05-19 15:38:24 -04001605 kfree(td);
Tejun Heoa2b16932012-04-13 13:11:33 -07001606 return ret;
Vivek Goyale43473b2010-09-15 17:06:35 -04001607}
1608
1609void blk_throtl_exit(struct request_queue *q)
1610{
Tejun Heoc875f4d2012-03-05 13:15:22 -08001611 BUG_ON(!q->td);
Vivek Goyalda527772011-03-02 19:05:33 -05001612 throtl_shutdown_wq(q);
Tejun Heo3c798392012-04-16 13:57:25 -07001613 blkcg_deactivate_policy(q, &blkcg_policy_throtl);
Tejun Heoc9a929d2011-10-19 14:42:16 +02001614 kfree(q->td);
Vivek Goyale43473b2010-09-15 17:06:35 -04001615}
1616
1617static int __init throtl_init(void)
1618{
Vivek Goyal450adcb2011-03-01 13:40:54 -05001619 kthrotld_workqueue = alloc_workqueue("kthrotld", WQ_MEM_RECLAIM, 0);
1620 if (!kthrotld_workqueue)
1621 panic("Failed to create kthrotld\n");
1622
Tejun Heo3c798392012-04-16 13:57:25 -07001623 return blkcg_policy_register(&blkcg_policy_throtl);
Vivek Goyale43473b2010-09-15 17:06:35 -04001624}
1625
1626module_init(throtl_init);