blob: 1da9f30cc3d10363a7fe0fd1a7ffae307423c12e [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
Shaohua Li9f626e32017-03-27 10:51:30 -070086enum {
87 LIMIT_MAX,
88 LIMIT_CNT,
89};
90
Vivek Goyale43473b2010-09-15 17:06:35 -040091struct throtl_grp {
Tejun Heof95a04a2012-04-16 13:57:26 -070092 /* must be the first member */
93 struct blkg_policy_data pd;
94
Tejun Heoc9e03322013-05-14 13:52:32 -070095 /* active throtl group service_queue member */
Vivek Goyale43473b2010-09-15 17:06:35 -040096 struct rb_node rb_node;
97
Tejun Heo0f3457f2013-05-14 13:52:32 -070098 /* throtl_data this group belongs to */
99 struct throtl_data *td;
100
Tejun Heo49a2f1e2013-05-14 13:52:34 -0700101 /* this group's service queue */
102 struct throtl_service_queue service_queue;
103
Vivek Goyale43473b2010-09-15 17:06:35 -0400104 /*
Tejun Heoc5cc2072013-05-14 13:52:38 -0700105 * qnode_on_self is used when bios are directly queued to this
106 * throtl_grp so that local bios compete fairly with bios
107 * dispatched from children. qnode_on_parent is used when bios are
108 * dispatched from this throtl_grp into its parent and will compete
109 * with the sibling qnode_on_parents and the parent's
110 * qnode_on_self.
111 */
112 struct throtl_qnode qnode_on_self[2];
113 struct throtl_qnode qnode_on_parent[2];
114
115 /*
Vivek Goyale43473b2010-09-15 17:06:35 -0400116 * Dispatch time in jiffies. This is the estimated time when group
117 * will unthrottle and is ready to dispatch more bio. It is used as
118 * key to sort active groups in service tree.
119 */
120 unsigned long disptime;
121
Vivek Goyale43473b2010-09-15 17:06:35 -0400122 unsigned int flags;
123
Tejun Heo693e7512013-05-14 13:52:38 -0700124 /* are there any throtl rules between this group and td? */
125 bool has_rules[2];
126
Vivek Goyale43473b2010-09-15 17:06:35 -0400127 /* bytes per second rate limits */
Shaohua Li9f626e32017-03-27 10:51:30 -0700128 uint64_t bps[2][LIMIT_CNT];
Vivek Goyale43473b2010-09-15 17:06:35 -0400129
Vivek Goyal8e89d132010-09-15 17:06:37 -0400130 /* IOPS limits */
Shaohua Li9f626e32017-03-27 10:51:30 -0700131 unsigned int iops[2][LIMIT_CNT];
Vivek Goyal8e89d132010-09-15 17:06:37 -0400132
Vivek Goyale43473b2010-09-15 17:06:35 -0400133 /* Number of bytes disptached in current slice */
134 uint64_t bytes_disp[2];
Vivek Goyal8e89d132010-09-15 17:06:37 -0400135 /* Number of bio's dispatched in current slice */
136 unsigned int io_disp[2];
Vivek Goyale43473b2010-09-15 17:06:35 -0400137
138 /* When did we start a new slice */
139 unsigned long slice_start[2];
140 unsigned long slice_end[2];
141};
142
143struct throtl_data
144{
Vivek Goyale43473b2010-09-15 17:06:35 -0400145 /* service tree for active throtl groups */
Tejun Heoc9e03322013-05-14 13:52:32 -0700146 struct throtl_service_queue service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -0400147
Vivek Goyale43473b2010-09-15 17:06:35 -0400148 struct request_queue *queue;
149
150 /* Total Number of queued bios on READ and WRITE lists */
151 unsigned int nr_queued[2];
152
Vivek Goyale43473b2010-09-15 17:06:35 -0400153 /* Work for dispatching throttled bios */
Tejun Heo69df0ab2013-05-14 13:52:36 -0700154 struct work_struct dispatch_work;
Shaohua Li9f626e32017-03-27 10:51:30 -0700155 unsigned int limit_index;
156 bool limit_valid[LIMIT_CNT];
Vivek Goyale43473b2010-09-15 17:06:35 -0400157};
158
Tejun Heo69df0ab2013-05-14 13:52:36 -0700159static void throtl_pending_timer_fn(unsigned long arg);
160
Tejun Heof95a04a2012-04-16 13:57:26 -0700161static inline struct throtl_grp *pd_to_tg(struct blkg_policy_data *pd)
162{
163 return pd ? container_of(pd, struct throtl_grp, pd) : NULL;
164}
165
Tejun Heo3c798392012-04-16 13:57:25 -0700166static inline struct throtl_grp *blkg_to_tg(struct blkcg_gq *blkg)
Tejun Heo03814112012-03-05 13:15:14 -0800167{
Tejun Heof95a04a2012-04-16 13:57:26 -0700168 return pd_to_tg(blkg_to_pd(blkg, &blkcg_policy_throtl));
Tejun Heo03814112012-03-05 13:15:14 -0800169}
170
Tejun Heo3c798392012-04-16 13:57:25 -0700171static inline struct blkcg_gq *tg_to_blkg(struct throtl_grp *tg)
Tejun Heo03814112012-03-05 13:15:14 -0800172{
Tejun Heof95a04a2012-04-16 13:57:26 -0700173 return pd_to_blkg(&tg->pd);
Tejun Heo03814112012-03-05 13:15:14 -0800174}
175
Tejun Heofda6f272013-05-14 13:52:36 -0700176/**
177 * sq_to_tg - return the throl_grp the specified service queue belongs to
178 * @sq: the throtl_service_queue of interest
179 *
180 * Return the throtl_grp @sq belongs to. If @sq is the top-level one
181 * embedded in throtl_data, %NULL is returned.
182 */
183static struct throtl_grp *sq_to_tg(struct throtl_service_queue *sq)
184{
185 if (sq && sq->parent_sq)
186 return container_of(sq, struct throtl_grp, service_queue);
187 else
188 return NULL;
189}
Vivek Goyale43473b2010-09-15 17:06:35 -0400190
Tejun Heofda6f272013-05-14 13:52:36 -0700191/**
192 * sq_to_td - return throtl_data the specified service queue belongs to
193 * @sq: the throtl_service_queue of interest
194 *
Masahiro Yamadab43daed2017-02-27 14:29:09 -0800195 * A service_queue can be embedded in either a throtl_grp or throtl_data.
Tejun Heofda6f272013-05-14 13:52:36 -0700196 * Determine the associated throtl_data accordingly and return it.
197 */
198static struct throtl_data *sq_to_td(struct throtl_service_queue *sq)
199{
200 struct throtl_grp *tg = sq_to_tg(sq);
201
202 if (tg)
203 return tg->td;
204 else
205 return container_of(sq, struct throtl_data, service_queue);
206}
207
Shaohua Li9f626e32017-03-27 10:51:30 -0700208static uint64_t tg_bps_limit(struct throtl_grp *tg, int rw)
209{
210 return tg->bps[rw][tg->td->limit_index];
211}
212
213static unsigned int tg_iops_limit(struct throtl_grp *tg, int rw)
214{
215 return tg->iops[rw][tg->td->limit_index];
216}
217
Tejun Heofda6f272013-05-14 13:52:36 -0700218/**
219 * throtl_log - log debug message via blktrace
220 * @sq: the service_queue being reported
221 * @fmt: printf format string
222 * @args: printf args
223 *
224 * The messages are prefixed with "throtl BLKG_NAME" if @sq belongs to a
225 * throtl_grp; otherwise, just "throtl".
Tejun Heofda6f272013-05-14 13:52:36 -0700226 */
227#define throtl_log(sq, fmt, args...) do { \
228 struct throtl_grp *__tg = sq_to_tg((sq)); \
229 struct throtl_data *__td = sq_to_td((sq)); \
230 \
231 (void)__td; \
Shaohua Li59fa0222016-05-09 17:22:15 -0700232 if (likely(!blk_trace_note_message_enabled(__td->queue))) \
233 break; \
Tejun Heofda6f272013-05-14 13:52:36 -0700234 if ((__tg)) { \
235 char __pbuf[128]; \
236 \
237 blkg_path(tg_to_blkg(__tg), __pbuf, sizeof(__pbuf)); \
238 blk_add_trace_msg(__td->queue, "throtl %s " fmt, __pbuf, ##args); \
239 } else { \
240 blk_add_trace_msg(__td->queue, "throtl " fmt, ##args); \
241 } \
242} while (0)
Vivek Goyale43473b2010-09-15 17:06:35 -0400243
Tejun Heoc5cc2072013-05-14 13:52:38 -0700244static void throtl_qnode_init(struct throtl_qnode *qn, struct throtl_grp *tg)
245{
246 INIT_LIST_HEAD(&qn->node);
247 bio_list_init(&qn->bios);
248 qn->tg = tg;
249}
250
251/**
252 * throtl_qnode_add_bio - add a bio to a throtl_qnode and activate it
253 * @bio: bio being added
254 * @qn: qnode to add bio to
255 * @queued: the service_queue->queued[] list @qn belongs to
256 *
257 * Add @bio to @qn and put @qn on @queued if it's not already on.
258 * @qn->tg's reference count is bumped when @qn is activated. See the
259 * comment on top of throtl_qnode definition for details.
260 */
261static void throtl_qnode_add_bio(struct bio *bio, struct throtl_qnode *qn,
262 struct list_head *queued)
263{
264 bio_list_add(&qn->bios, bio);
265 if (list_empty(&qn->node)) {
266 list_add_tail(&qn->node, queued);
267 blkg_get(tg_to_blkg(qn->tg));
268 }
269}
270
271/**
272 * throtl_peek_queued - peek the first bio on a qnode list
273 * @queued: the qnode list to peek
274 */
275static struct bio *throtl_peek_queued(struct list_head *queued)
276{
277 struct throtl_qnode *qn = list_first_entry(queued, struct throtl_qnode, node);
278 struct bio *bio;
279
280 if (list_empty(queued))
281 return NULL;
282
283 bio = bio_list_peek(&qn->bios);
284 WARN_ON_ONCE(!bio);
285 return bio;
286}
287
288/**
289 * throtl_pop_queued - pop the first bio form a qnode list
290 * @queued: the qnode list to pop a bio from
291 * @tg_to_put: optional out argument for throtl_grp to put
292 *
293 * Pop the first bio from the qnode list @queued. After popping, the first
294 * qnode is removed from @queued if empty or moved to the end of @queued so
295 * that the popping order is round-robin.
296 *
297 * When the first qnode is removed, its associated throtl_grp should be put
298 * too. If @tg_to_put is NULL, this function automatically puts it;
299 * otherwise, *@tg_to_put is set to the throtl_grp to put and the caller is
300 * responsible for putting it.
301 */
302static struct bio *throtl_pop_queued(struct list_head *queued,
303 struct throtl_grp **tg_to_put)
304{
305 struct throtl_qnode *qn = list_first_entry(queued, struct throtl_qnode, node);
306 struct bio *bio;
307
308 if (list_empty(queued))
309 return NULL;
310
311 bio = bio_list_pop(&qn->bios);
312 WARN_ON_ONCE(!bio);
313
314 if (bio_list_empty(&qn->bios)) {
315 list_del_init(&qn->node);
316 if (tg_to_put)
317 *tg_to_put = qn->tg;
318 else
319 blkg_put(tg_to_blkg(qn->tg));
320 } else {
321 list_move_tail(&qn->node, queued);
322 }
323
324 return bio;
325}
326
Tejun Heo49a2f1e2013-05-14 13:52:34 -0700327/* init a service_queue, assumes the caller zeroed it */
Tejun Heob2ce2642015-08-18 14:55:13 -0700328static void throtl_service_queue_init(struct throtl_service_queue *sq)
Tejun Heo49a2f1e2013-05-14 13:52:34 -0700329{
Tejun Heoc5cc2072013-05-14 13:52:38 -0700330 INIT_LIST_HEAD(&sq->queued[0]);
331 INIT_LIST_HEAD(&sq->queued[1]);
Tejun Heo49a2f1e2013-05-14 13:52:34 -0700332 sq->pending_tree = RB_ROOT;
Tejun Heo69df0ab2013-05-14 13:52:36 -0700333 setup_timer(&sq->pending_timer, throtl_pending_timer_fn,
334 (unsigned long)sq);
335}
336
Tejun Heo001bea72015-08-18 14:55:11 -0700337static struct blkg_policy_data *throtl_pd_alloc(gfp_t gfp, int node)
338{
Tejun Heo4fb72032015-08-18 14:55:12 -0700339 struct throtl_grp *tg;
Tejun Heo24bdb8e2015-08-18 14:55:22 -0700340 int rw;
Tejun Heo4fb72032015-08-18 14:55:12 -0700341
342 tg = kzalloc_node(sizeof(*tg), gfp, node);
343 if (!tg)
Tejun Heo77ea7332015-08-18 14:55:24 -0700344 return NULL;
Tejun Heo4fb72032015-08-18 14:55:12 -0700345
Tejun Heob2ce2642015-08-18 14:55:13 -0700346 throtl_service_queue_init(&tg->service_queue);
347
348 for (rw = READ; rw <= WRITE; rw++) {
349 throtl_qnode_init(&tg->qnode_on_self[rw], tg);
350 throtl_qnode_init(&tg->qnode_on_parent[rw], tg);
351 }
352
353 RB_CLEAR_NODE(&tg->rb_node);
Shaohua Li9f626e32017-03-27 10:51:30 -0700354 tg->bps[READ][LIMIT_MAX] = U64_MAX;
355 tg->bps[WRITE][LIMIT_MAX] = U64_MAX;
356 tg->iops[READ][LIMIT_MAX] = UINT_MAX;
357 tg->iops[WRITE][LIMIT_MAX] = UINT_MAX;
Tejun Heob2ce2642015-08-18 14:55:13 -0700358
Tejun Heo4fb72032015-08-18 14:55:12 -0700359 return &tg->pd;
Tejun Heo001bea72015-08-18 14:55:11 -0700360}
361
Tejun Heoa9520cd2015-08-18 14:55:14 -0700362static void throtl_pd_init(struct blkg_policy_data *pd)
Vivek Goyala29a1712011-05-19 15:38:19 -0400363{
Tejun Heoa9520cd2015-08-18 14:55:14 -0700364 struct throtl_grp *tg = pd_to_tg(pd);
365 struct blkcg_gq *blkg = tg_to_blkg(tg);
Tejun Heo77216b02013-05-14 13:52:36 -0700366 struct throtl_data *td = blkg->q->td;
Tejun Heob2ce2642015-08-18 14:55:13 -0700367 struct throtl_service_queue *sq = &tg->service_queue;
Tejun Heocd1604f2012-03-05 13:15:06 -0800368
Tejun Heo91381252013-05-14 13:52:38 -0700369 /*
Tejun Heoaa6ec292014-07-09 10:08:08 -0400370 * If on the default hierarchy, we switch to properly hierarchical
Tejun Heo91381252013-05-14 13:52:38 -0700371 * behavior where limits on a given throtl_grp are applied to the
372 * whole subtree rather than just the group itself. e.g. If 16M
373 * read_bps limit is set on the root group, the whole system can't
374 * exceed 16M for the device.
375 *
Tejun Heoaa6ec292014-07-09 10:08:08 -0400376 * If not on the default hierarchy, the broken flat hierarchy
Tejun Heo91381252013-05-14 13:52:38 -0700377 * behavior is retained where all throtl_grps are treated as if
378 * they're all separate root groups right below throtl_data.
379 * Limits of a group don't interact with limits of other groups
380 * regardless of the position of the group in the hierarchy.
381 */
Tejun Heob2ce2642015-08-18 14:55:13 -0700382 sq->parent_sq = &td->service_queue;
Tejun Heo9e10a132015-09-18 11:56:28 -0400383 if (cgroup_subsys_on_dfl(io_cgrp_subsys) && blkg->parent)
Tejun Heob2ce2642015-08-18 14:55:13 -0700384 sq->parent_sq = &blkg_to_tg(blkg->parent)->service_queue;
Tejun Heo77216b02013-05-14 13:52:36 -0700385 tg->td = td;
Tejun Heo8a3d2612012-04-01 14:38:44 -0700386}
387
Tejun Heo693e7512013-05-14 13:52:38 -0700388/*
389 * Set has_rules[] if @tg or any of its parents have limits configured.
390 * This doesn't require walking up to the top of the hierarchy as the
391 * parent's has_rules[] is guaranteed to be correct.
392 */
393static void tg_update_has_rules(struct throtl_grp *tg)
394{
395 struct throtl_grp *parent_tg = sq_to_tg(tg->service_queue.parent_sq);
Shaohua Li9f626e32017-03-27 10:51:30 -0700396 struct throtl_data *td = tg->td;
Tejun Heo693e7512013-05-14 13:52:38 -0700397 int rw;
398
399 for (rw = READ; rw <= WRITE; rw++)
400 tg->has_rules[rw] = (parent_tg && parent_tg->has_rules[rw]) ||
Shaohua Li9f626e32017-03-27 10:51:30 -0700401 (td->limit_valid[td->limit_index] &&
402 (tg_bps_limit(tg, rw) != U64_MAX ||
403 tg_iops_limit(tg, rw) != UINT_MAX));
Tejun Heo693e7512013-05-14 13:52:38 -0700404}
405
Tejun Heoa9520cd2015-08-18 14:55:14 -0700406static void throtl_pd_online(struct blkg_policy_data *pd)
Tejun Heo693e7512013-05-14 13:52:38 -0700407{
408 /*
409 * We don't want new groups to escape the limits of its ancestors.
410 * Update has_rules[] after a new group is brought online.
411 */
Tejun Heoa9520cd2015-08-18 14:55:14 -0700412 tg_update_has_rules(pd_to_tg(pd));
Tejun Heo693e7512013-05-14 13:52:38 -0700413}
414
Tejun Heo001bea72015-08-18 14:55:11 -0700415static void throtl_pd_free(struct blkg_policy_data *pd)
416{
Tejun Heo4fb72032015-08-18 14:55:12 -0700417 struct throtl_grp *tg = pd_to_tg(pd);
418
Tejun Heob2ce2642015-08-18 14:55:13 -0700419 del_timer_sync(&tg->service_queue.pending_timer);
Tejun Heo4fb72032015-08-18 14:55:12 -0700420 kfree(tg);
Tejun Heo001bea72015-08-18 14:55:11 -0700421}
422
Tejun Heo0049af72013-05-14 13:52:33 -0700423static struct throtl_grp *
424throtl_rb_first(struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -0400425{
426 /* Service tree is empty */
Tejun Heo0049af72013-05-14 13:52:33 -0700427 if (!parent_sq->nr_pending)
Vivek Goyale43473b2010-09-15 17:06:35 -0400428 return NULL;
429
Tejun Heo0049af72013-05-14 13:52:33 -0700430 if (!parent_sq->first_pending)
431 parent_sq->first_pending = rb_first(&parent_sq->pending_tree);
Vivek Goyale43473b2010-09-15 17:06:35 -0400432
Tejun Heo0049af72013-05-14 13:52:33 -0700433 if (parent_sq->first_pending)
434 return rb_entry_tg(parent_sq->first_pending);
Vivek Goyale43473b2010-09-15 17:06:35 -0400435
436 return NULL;
437}
438
439static void rb_erase_init(struct rb_node *n, struct rb_root *root)
440{
441 rb_erase(n, root);
442 RB_CLEAR_NODE(n);
443}
444
Tejun Heo0049af72013-05-14 13:52:33 -0700445static void throtl_rb_erase(struct rb_node *n,
446 struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -0400447{
Tejun Heo0049af72013-05-14 13:52:33 -0700448 if (parent_sq->first_pending == n)
449 parent_sq->first_pending = NULL;
450 rb_erase_init(n, &parent_sq->pending_tree);
451 --parent_sq->nr_pending;
Vivek Goyale43473b2010-09-15 17:06:35 -0400452}
453
Tejun Heo0049af72013-05-14 13:52:33 -0700454static void update_min_dispatch_time(struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -0400455{
456 struct throtl_grp *tg;
457
Tejun Heo0049af72013-05-14 13:52:33 -0700458 tg = throtl_rb_first(parent_sq);
Vivek Goyale43473b2010-09-15 17:06:35 -0400459 if (!tg)
460 return;
461
Tejun Heo0049af72013-05-14 13:52:33 -0700462 parent_sq->first_pending_disptime = tg->disptime;
Vivek Goyale43473b2010-09-15 17:06:35 -0400463}
464
Tejun Heo77216b02013-05-14 13:52:36 -0700465static void tg_service_queue_add(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400466{
Tejun Heo77216b02013-05-14 13:52:36 -0700467 struct throtl_service_queue *parent_sq = tg->service_queue.parent_sq;
Tejun Heo0049af72013-05-14 13:52:33 -0700468 struct rb_node **node = &parent_sq->pending_tree.rb_node;
Vivek Goyale43473b2010-09-15 17:06:35 -0400469 struct rb_node *parent = NULL;
470 struct throtl_grp *__tg;
471 unsigned long key = tg->disptime;
472 int left = 1;
473
474 while (*node != NULL) {
475 parent = *node;
476 __tg = rb_entry_tg(parent);
477
478 if (time_before(key, __tg->disptime))
479 node = &parent->rb_left;
480 else {
481 node = &parent->rb_right;
482 left = 0;
483 }
484 }
485
486 if (left)
Tejun Heo0049af72013-05-14 13:52:33 -0700487 parent_sq->first_pending = &tg->rb_node;
Vivek Goyale43473b2010-09-15 17:06:35 -0400488
489 rb_link_node(&tg->rb_node, parent, node);
Tejun Heo0049af72013-05-14 13:52:33 -0700490 rb_insert_color(&tg->rb_node, &parent_sq->pending_tree);
Vivek Goyale43473b2010-09-15 17:06:35 -0400491}
492
Tejun Heo77216b02013-05-14 13:52:36 -0700493static void __throtl_enqueue_tg(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400494{
Tejun Heo77216b02013-05-14 13:52:36 -0700495 tg_service_queue_add(tg);
Tejun Heo5b2c16a2013-05-14 13:52:32 -0700496 tg->flags |= THROTL_TG_PENDING;
Tejun Heo77216b02013-05-14 13:52:36 -0700497 tg->service_queue.parent_sq->nr_pending++;
Vivek Goyale43473b2010-09-15 17:06:35 -0400498}
499
Tejun Heo77216b02013-05-14 13:52:36 -0700500static void throtl_enqueue_tg(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400501{
Tejun Heo5b2c16a2013-05-14 13:52:32 -0700502 if (!(tg->flags & THROTL_TG_PENDING))
Tejun Heo77216b02013-05-14 13:52:36 -0700503 __throtl_enqueue_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -0400504}
505
Tejun Heo77216b02013-05-14 13:52:36 -0700506static void __throtl_dequeue_tg(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400507{
Tejun Heo77216b02013-05-14 13:52:36 -0700508 throtl_rb_erase(&tg->rb_node, tg->service_queue.parent_sq);
Tejun Heo5b2c16a2013-05-14 13:52:32 -0700509 tg->flags &= ~THROTL_TG_PENDING;
Vivek Goyale43473b2010-09-15 17:06:35 -0400510}
511
Tejun Heo77216b02013-05-14 13:52:36 -0700512static void throtl_dequeue_tg(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400513{
Tejun Heo5b2c16a2013-05-14 13:52:32 -0700514 if (tg->flags & THROTL_TG_PENDING)
Tejun Heo77216b02013-05-14 13:52:36 -0700515 __throtl_dequeue_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -0400516}
517
Tejun Heoa9131a22013-05-14 13:52:31 -0700518/* Call with queue lock held */
Tejun Heo69df0ab2013-05-14 13:52:36 -0700519static void throtl_schedule_pending_timer(struct throtl_service_queue *sq,
520 unsigned long expires)
Tejun Heoa9131a22013-05-14 13:52:31 -0700521{
Tejun Heo69df0ab2013-05-14 13:52:36 -0700522 mod_timer(&sq->pending_timer, expires);
523 throtl_log(sq, "schedule timer. delay=%lu jiffies=%lu",
524 expires - jiffies, jiffies);
Tejun Heoa9131a22013-05-14 13:52:31 -0700525}
526
Tejun Heo7f52f982013-05-14 13:52:37 -0700527/**
528 * throtl_schedule_next_dispatch - schedule the next dispatch cycle
529 * @sq: the service_queue to schedule dispatch for
530 * @force: force scheduling
531 *
532 * Arm @sq->pending_timer so that the next dispatch cycle starts on the
533 * dispatch time of the first pending child. Returns %true if either timer
534 * is armed or there's no pending child left. %false if the current
535 * dispatch window is still open and the caller should continue
536 * dispatching.
537 *
538 * If @force is %true, the dispatch timer is always scheduled and this
539 * function is guaranteed to return %true. This is to be used when the
540 * caller can't dispatch itself and needs to invoke pending_timer
541 * unconditionally. Note that forced scheduling is likely to induce short
542 * delay before dispatch starts even if @sq->first_pending_disptime is not
543 * in the future and thus shouldn't be used in hot paths.
544 */
545static bool throtl_schedule_next_dispatch(struct throtl_service_queue *sq,
546 bool force)
Vivek Goyale43473b2010-09-15 17:06:35 -0400547{
Tejun Heo6a525602013-05-14 13:52:32 -0700548 /* any pending children left? */
Tejun Heoc9e03322013-05-14 13:52:32 -0700549 if (!sq->nr_pending)
Tejun Heo7f52f982013-05-14 13:52:37 -0700550 return true;
Vivek Goyale43473b2010-09-15 17:06:35 -0400551
Tejun Heoc9e03322013-05-14 13:52:32 -0700552 update_min_dispatch_time(sq);
Vivek Goyale43473b2010-09-15 17:06:35 -0400553
Tejun Heo69df0ab2013-05-14 13:52:36 -0700554 /* is the next dispatch time in the future? */
Tejun Heo7f52f982013-05-14 13:52:37 -0700555 if (force || time_after(sq->first_pending_disptime, jiffies)) {
Tejun Heo69df0ab2013-05-14 13:52:36 -0700556 throtl_schedule_pending_timer(sq, sq->first_pending_disptime);
Tejun Heo7f52f982013-05-14 13:52:37 -0700557 return true;
Tejun Heo69df0ab2013-05-14 13:52:36 -0700558 }
559
Tejun Heo7f52f982013-05-14 13:52:37 -0700560 /* tell the caller to continue dispatching */
561 return false;
Vivek Goyale43473b2010-09-15 17:06:35 -0400562}
563
Vivek Goyal32ee5bc2013-05-14 13:52:38 -0700564static inline void throtl_start_new_slice_with_credit(struct throtl_grp *tg,
565 bool rw, unsigned long start)
566{
567 tg->bytes_disp[rw] = 0;
568 tg->io_disp[rw] = 0;
569
570 /*
571 * Previous slice has expired. We must have trimmed it after last
572 * bio dispatch. That means since start of last slice, we never used
573 * that bandwidth. Do try to make use of that bandwidth while giving
574 * credit.
575 */
576 if (time_after_eq(start, tg->slice_start[rw]))
577 tg->slice_start[rw] = start;
578
579 tg->slice_end[rw] = jiffies + throtl_slice;
580 throtl_log(&tg->service_queue,
581 "[%c] new slice with credit start=%lu end=%lu jiffies=%lu",
582 rw == READ ? 'R' : 'W', tg->slice_start[rw],
583 tg->slice_end[rw], jiffies);
584}
585
Tejun Heo0f3457f2013-05-14 13:52:32 -0700586static inline void throtl_start_new_slice(struct throtl_grp *tg, bool rw)
Vivek Goyale43473b2010-09-15 17:06:35 -0400587{
588 tg->bytes_disp[rw] = 0;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400589 tg->io_disp[rw] = 0;
Vivek Goyale43473b2010-09-15 17:06:35 -0400590 tg->slice_start[rw] = jiffies;
591 tg->slice_end[rw] = jiffies + throtl_slice;
Tejun Heofda6f272013-05-14 13:52:36 -0700592 throtl_log(&tg->service_queue,
593 "[%c] new slice start=%lu end=%lu jiffies=%lu",
594 rw == READ ? 'R' : 'W', tg->slice_start[rw],
595 tg->slice_end[rw], jiffies);
Vivek Goyale43473b2010-09-15 17:06:35 -0400596}
597
Tejun Heo0f3457f2013-05-14 13:52:32 -0700598static inline void throtl_set_slice_end(struct throtl_grp *tg, bool rw,
599 unsigned long jiffy_end)
Vivek Goyald1ae8ff2010-12-01 19:34:46 +0100600{
601 tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
602}
603
Tejun Heo0f3457f2013-05-14 13:52:32 -0700604static inline void throtl_extend_slice(struct throtl_grp *tg, bool rw,
605 unsigned long jiffy_end)
Vivek Goyale43473b2010-09-15 17:06:35 -0400606{
607 tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
Tejun Heofda6f272013-05-14 13:52:36 -0700608 throtl_log(&tg->service_queue,
609 "[%c] extend slice start=%lu end=%lu jiffies=%lu",
610 rw == READ ? 'R' : 'W', tg->slice_start[rw],
611 tg->slice_end[rw], jiffies);
Vivek Goyale43473b2010-09-15 17:06:35 -0400612}
613
614/* Determine if previously allocated or extended slice is complete or not */
Tejun Heo0f3457f2013-05-14 13:52:32 -0700615static bool throtl_slice_used(struct throtl_grp *tg, bool rw)
Vivek Goyale43473b2010-09-15 17:06:35 -0400616{
617 if (time_in_range(jiffies, tg->slice_start[rw], tg->slice_end[rw]))
Fabian Frederick5cf8c222014-05-02 18:28:17 +0200618 return false;
Vivek Goyale43473b2010-09-15 17:06:35 -0400619
620 return 1;
621}
622
623/* Trim the used slices and adjust slice start accordingly */
Tejun Heo0f3457f2013-05-14 13:52:32 -0700624static inline void throtl_trim_slice(struct throtl_grp *tg, bool rw)
Vivek Goyale43473b2010-09-15 17:06:35 -0400625{
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200626 unsigned long nr_slices, time_elapsed, io_trim;
627 u64 bytes_trim, tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400628
629 BUG_ON(time_before(tg->slice_end[rw], tg->slice_start[rw]));
630
631 /*
632 * If bps are unlimited (-1), then time slice don't get
633 * renewed. Don't try to trim the slice if slice is used. A new
634 * slice will start when appropriate.
635 */
Tejun Heo0f3457f2013-05-14 13:52:32 -0700636 if (throtl_slice_used(tg, rw))
Vivek Goyale43473b2010-09-15 17:06:35 -0400637 return;
638
Vivek Goyald1ae8ff2010-12-01 19:34:46 +0100639 /*
640 * A bio has been dispatched. Also adjust slice_end. It might happen
641 * that initially cgroup limit was very low resulting in high
642 * slice_end, but later limit was bumped up and bio was dispached
643 * sooner, then we need to reduce slice_end. A high bogus slice_end
644 * is bad because it does not allow new slice to start.
645 */
646
Tejun Heo0f3457f2013-05-14 13:52:32 -0700647 throtl_set_slice_end(tg, rw, jiffies + throtl_slice);
Vivek Goyald1ae8ff2010-12-01 19:34:46 +0100648
Vivek Goyale43473b2010-09-15 17:06:35 -0400649 time_elapsed = jiffies - tg->slice_start[rw];
650
651 nr_slices = time_elapsed / throtl_slice;
652
653 if (!nr_slices)
654 return;
Shaohua Li9f626e32017-03-27 10:51:30 -0700655 tmp = tg_bps_limit(tg, rw) * throtl_slice * nr_slices;
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200656 do_div(tmp, HZ);
657 bytes_trim = tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400658
Shaohua Li9f626e32017-03-27 10:51:30 -0700659 io_trim = (tg_iops_limit(tg, rw) * throtl_slice * nr_slices) / HZ;
Vivek Goyale43473b2010-09-15 17:06:35 -0400660
Vivek Goyal8e89d132010-09-15 17:06:37 -0400661 if (!bytes_trim && !io_trim)
Vivek Goyale43473b2010-09-15 17:06:35 -0400662 return;
663
664 if (tg->bytes_disp[rw] >= bytes_trim)
665 tg->bytes_disp[rw] -= bytes_trim;
666 else
667 tg->bytes_disp[rw] = 0;
668
Vivek Goyal8e89d132010-09-15 17:06:37 -0400669 if (tg->io_disp[rw] >= io_trim)
670 tg->io_disp[rw] -= io_trim;
671 else
672 tg->io_disp[rw] = 0;
673
Vivek Goyale43473b2010-09-15 17:06:35 -0400674 tg->slice_start[rw] += nr_slices * throtl_slice;
675
Tejun Heofda6f272013-05-14 13:52:36 -0700676 throtl_log(&tg->service_queue,
677 "[%c] trim slice nr=%lu bytes=%llu io=%lu start=%lu end=%lu jiffies=%lu",
678 rw == READ ? 'R' : 'W', nr_slices, bytes_trim, io_trim,
679 tg->slice_start[rw], tg->slice_end[rw], jiffies);
Vivek Goyale43473b2010-09-15 17:06:35 -0400680}
681
Tejun Heo0f3457f2013-05-14 13:52:32 -0700682static bool tg_with_in_iops_limit(struct throtl_grp *tg, struct bio *bio,
683 unsigned long *wait)
Vivek Goyale43473b2010-09-15 17:06:35 -0400684{
685 bool rw = bio_data_dir(bio);
Vivek Goyal8e89d132010-09-15 17:06:37 -0400686 unsigned int io_allowed;
Vivek Goyale43473b2010-09-15 17:06:35 -0400687 unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
Vivek Goyalc49c06e2010-10-01 21:16:42 +0200688 u64 tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400689
Vivek Goyal8e89d132010-09-15 17:06:37 -0400690 jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
Vivek Goyale43473b2010-09-15 17:06:35 -0400691
Vivek Goyal8e89d132010-09-15 17:06:37 -0400692 /* Slice has just started. Consider one slice interval */
693 if (!jiffy_elapsed)
694 jiffy_elapsed_rnd = throtl_slice;
695
696 jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
697
Vivek Goyalc49c06e2010-10-01 21:16:42 +0200698 /*
699 * jiffy_elapsed_rnd should not be a big value as minimum iops can be
700 * 1 then at max jiffy elapsed should be equivalent of 1 second as we
701 * will allow dispatch after 1 second and after that slice should
702 * have been trimmed.
703 */
704
Shaohua Li9f626e32017-03-27 10:51:30 -0700705 tmp = (u64)tg_iops_limit(tg, rw) * jiffy_elapsed_rnd;
Vivek Goyalc49c06e2010-10-01 21:16:42 +0200706 do_div(tmp, HZ);
707
708 if (tmp > UINT_MAX)
709 io_allowed = UINT_MAX;
710 else
711 io_allowed = tmp;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400712
713 if (tg->io_disp[rw] + 1 <= io_allowed) {
Vivek Goyale43473b2010-09-15 17:06:35 -0400714 if (wait)
715 *wait = 0;
Fabian Frederick5cf8c222014-05-02 18:28:17 +0200716 return true;
Vivek Goyale43473b2010-09-15 17:06:35 -0400717 }
718
Vivek Goyal8e89d132010-09-15 17:06:37 -0400719 /* Calc approx time to dispatch */
Shaohua Li9f626e32017-03-27 10:51:30 -0700720 jiffy_wait = ((tg->io_disp[rw] + 1) * HZ) / tg_iops_limit(tg, rw) + 1;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400721
722 if (jiffy_wait > jiffy_elapsed)
723 jiffy_wait = jiffy_wait - jiffy_elapsed;
724 else
725 jiffy_wait = 1;
726
727 if (wait)
728 *wait = jiffy_wait;
729 return 0;
730}
731
Tejun Heo0f3457f2013-05-14 13:52:32 -0700732static bool tg_with_in_bps_limit(struct throtl_grp *tg, struct bio *bio,
733 unsigned long *wait)
Vivek Goyal8e89d132010-09-15 17:06:37 -0400734{
735 bool rw = bio_data_dir(bio);
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200736 u64 bytes_allowed, extra_bytes, tmp;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400737 unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
Vivek Goyale43473b2010-09-15 17:06:35 -0400738
739 jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
740
741 /* Slice has just started. Consider one slice interval */
742 if (!jiffy_elapsed)
743 jiffy_elapsed_rnd = throtl_slice;
744
745 jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
746
Shaohua Li9f626e32017-03-27 10:51:30 -0700747 tmp = tg_bps_limit(tg, rw) * jiffy_elapsed_rnd;
Vivek Goyal5e901a22010-10-01 21:16:38 +0200748 do_div(tmp, HZ);
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200749 bytes_allowed = tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400750
Kent Overstreet4f024f32013-10-11 15:44:27 -0700751 if (tg->bytes_disp[rw] + bio->bi_iter.bi_size <= bytes_allowed) {
Vivek Goyale43473b2010-09-15 17:06:35 -0400752 if (wait)
753 *wait = 0;
Fabian Frederick5cf8c222014-05-02 18:28:17 +0200754 return true;
Vivek Goyale43473b2010-09-15 17:06:35 -0400755 }
756
757 /* Calc approx time to dispatch */
Kent Overstreet4f024f32013-10-11 15:44:27 -0700758 extra_bytes = tg->bytes_disp[rw] + bio->bi_iter.bi_size - bytes_allowed;
Shaohua Li9f626e32017-03-27 10:51:30 -0700759 jiffy_wait = div64_u64(extra_bytes * HZ, tg_bps_limit(tg, rw));
Vivek Goyale43473b2010-09-15 17:06:35 -0400760
761 if (!jiffy_wait)
762 jiffy_wait = 1;
763
764 /*
765 * This wait time is without taking into consideration the rounding
766 * up we did. Add that time also.
767 */
768 jiffy_wait = jiffy_wait + (jiffy_elapsed_rnd - jiffy_elapsed);
Vivek Goyale43473b2010-09-15 17:06:35 -0400769 if (wait)
770 *wait = jiffy_wait;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400771 return 0;
772}
Vivek Goyale43473b2010-09-15 17:06:35 -0400773
Vivek Goyal8e89d132010-09-15 17:06:37 -0400774/*
775 * Returns whether one can dispatch a bio or not. Also returns approx number
776 * of jiffies to wait before this bio is with-in IO rate and can be dispatched
777 */
Tejun Heo0f3457f2013-05-14 13:52:32 -0700778static bool tg_may_dispatch(struct throtl_grp *tg, struct bio *bio,
779 unsigned long *wait)
Vivek Goyal8e89d132010-09-15 17:06:37 -0400780{
781 bool rw = bio_data_dir(bio);
782 unsigned long bps_wait = 0, iops_wait = 0, max_wait = 0;
783
784 /*
785 * Currently whole state machine of group depends on first bio
786 * queued in the group bio list. So one should not be calling
787 * this function with a different bio if there are other bios
788 * queued.
789 */
Tejun Heo73f0d492013-05-14 13:52:35 -0700790 BUG_ON(tg->service_queue.nr_queued[rw] &&
Tejun Heoc5cc2072013-05-14 13:52:38 -0700791 bio != throtl_peek_queued(&tg->service_queue.queued[rw]));
Vivek Goyal8e89d132010-09-15 17:06:37 -0400792
793 /* If tg->bps = -1, then BW is unlimited */
Shaohua Li9f626e32017-03-27 10:51:30 -0700794 if (tg_bps_limit(tg, rw) == U64_MAX &&
795 tg_iops_limit(tg, rw) == UINT_MAX) {
Vivek Goyal8e89d132010-09-15 17:06:37 -0400796 if (wait)
797 *wait = 0;
Fabian Frederick5cf8c222014-05-02 18:28:17 +0200798 return true;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400799 }
800
801 /*
802 * If previous slice expired, start a new one otherwise renew/extend
803 * existing slice to make sure it is at least throtl_slice interval
Vivek Goyal164c80e2016-09-19 15:12:41 -0600804 * long since now. New slice is started only for empty throttle group.
805 * If there is queued bio, that means there should be an active
806 * slice and it should be extended instead.
Vivek Goyal8e89d132010-09-15 17:06:37 -0400807 */
Vivek Goyal164c80e2016-09-19 15:12:41 -0600808 if (throtl_slice_used(tg, rw) && !(tg->service_queue.nr_queued[rw]))
Tejun Heo0f3457f2013-05-14 13:52:32 -0700809 throtl_start_new_slice(tg, rw);
Vivek Goyal8e89d132010-09-15 17:06:37 -0400810 else {
811 if (time_before(tg->slice_end[rw], jiffies + throtl_slice))
Tejun Heo0f3457f2013-05-14 13:52:32 -0700812 throtl_extend_slice(tg, rw, jiffies + throtl_slice);
Vivek Goyal8e89d132010-09-15 17:06:37 -0400813 }
814
Tejun Heo0f3457f2013-05-14 13:52:32 -0700815 if (tg_with_in_bps_limit(tg, bio, &bps_wait) &&
816 tg_with_in_iops_limit(tg, bio, &iops_wait)) {
Vivek Goyal8e89d132010-09-15 17:06:37 -0400817 if (wait)
818 *wait = 0;
819 return 1;
820 }
821
822 max_wait = max(bps_wait, iops_wait);
823
824 if (wait)
825 *wait = max_wait;
826
827 if (time_before(tg->slice_end[rw], jiffies + max_wait))
Tejun Heo0f3457f2013-05-14 13:52:32 -0700828 throtl_extend_slice(tg, rw, jiffies + max_wait);
Vivek Goyale43473b2010-09-15 17:06:35 -0400829
830 return 0;
831}
832
833static void throtl_charge_bio(struct throtl_grp *tg, struct bio *bio)
834{
835 bool rw = bio_data_dir(bio);
Vivek Goyale43473b2010-09-15 17:06:35 -0400836
837 /* Charge the bio to the group */
Kent Overstreet4f024f32013-10-11 15:44:27 -0700838 tg->bytes_disp[rw] += bio->bi_iter.bi_size;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400839 tg->io_disp[rw]++;
Vivek Goyale43473b2010-09-15 17:06:35 -0400840
Tejun Heo2a0f61e2013-05-14 13:52:36 -0700841 /*
Christoph Hellwig8d2bbd42016-10-20 15:12:12 +0200842 * BIO_THROTTLED is used to prevent the same bio to be throttled
Tejun Heo2a0f61e2013-05-14 13:52:36 -0700843 * more than once as a throttled bio will go through blk-throtl the
844 * second time when it eventually gets issued. Set it when a bio
845 * is being charged to a tg.
Tejun Heo2a0f61e2013-05-14 13:52:36 -0700846 */
Christoph Hellwig8d2bbd42016-10-20 15:12:12 +0200847 if (!bio_flagged(bio, BIO_THROTTLED))
848 bio_set_flag(bio, BIO_THROTTLED);
Vivek Goyale43473b2010-09-15 17:06:35 -0400849}
850
Tejun Heoc5cc2072013-05-14 13:52:38 -0700851/**
852 * throtl_add_bio_tg - add a bio to the specified throtl_grp
853 * @bio: bio to add
854 * @qn: qnode to use
855 * @tg: the target throtl_grp
856 *
857 * Add @bio to @tg's service_queue using @qn. If @qn is not specified,
858 * tg->qnode_on_self[] is used.
859 */
860static void throtl_add_bio_tg(struct bio *bio, struct throtl_qnode *qn,
861 struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400862{
Tejun Heo73f0d492013-05-14 13:52:35 -0700863 struct throtl_service_queue *sq = &tg->service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -0400864 bool rw = bio_data_dir(bio);
865
Tejun Heoc5cc2072013-05-14 13:52:38 -0700866 if (!qn)
867 qn = &tg->qnode_on_self[rw];
868
Tejun Heo0e9f4162013-05-14 13:52:35 -0700869 /*
870 * If @tg doesn't currently have any bios queued in the same
871 * direction, queueing @bio can change when @tg should be
872 * dispatched. Mark that @tg was empty. This is automatically
873 * cleaered on the next tg_update_disptime().
874 */
875 if (!sq->nr_queued[rw])
876 tg->flags |= THROTL_TG_WAS_EMPTY;
877
Tejun Heoc5cc2072013-05-14 13:52:38 -0700878 throtl_qnode_add_bio(bio, qn, &sq->queued[rw]);
879
Tejun Heo73f0d492013-05-14 13:52:35 -0700880 sq->nr_queued[rw]++;
Tejun Heo77216b02013-05-14 13:52:36 -0700881 throtl_enqueue_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -0400882}
883
Tejun Heo77216b02013-05-14 13:52:36 -0700884static void tg_update_disptime(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400885{
Tejun Heo73f0d492013-05-14 13:52:35 -0700886 struct throtl_service_queue *sq = &tg->service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -0400887 unsigned long read_wait = -1, write_wait = -1, min_wait = -1, disptime;
888 struct bio *bio;
889
Markus Elfringd609af32017-01-21 22:15:33 +0100890 bio = throtl_peek_queued(&sq->queued[READ]);
891 if (bio)
Tejun Heo0f3457f2013-05-14 13:52:32 -0700892 tg_may_dispatch(tg, bio, &read_wait);
Vivek Goyale43473b2010-09-15 17:06:35 -0400893
Markus Elfringd609af32017-01-21 22:15:33 +0100894 bio = throtl_peek_queued(&sq->queued[WRITE]);
895 if (bio)
Tejun Heo0f3457f2013-05-14 13:52:32 -0700896 tg_may_dispatch(tg, bio, &write_wait);
Vivek Goyale43473b2010-09-15 17:06:35 -0400897
898 min_wait = min(read_wait, write_wait);
899 disptime = jiffies + min_wait;
900
Vivek Goyale43473b2010-09-15 17:06:35 -0400901 /* Update dispatch time */
Tejun Heo77216b02013-05-14 13:52:36 -0700902 throtl_dequeue_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -0400903 tg->disptime = disptime;
Tejun Heo77216b02013-05-14 13:52:36 -0700904 throtl_enqueue_tg(tg);
Tejun Heo0e9f4162013-05-14 13:52:35 -0700905
906 /* see throtl_add_bio_tg() */
907 tg->flags &= ~THROTL_TG_WAS_EMPTY;
Vivek Goyale43473b2010-09-15 17:06:35 -0400908}
909
Vivek Goyal32ee5bc2013-05-14 13:52:38 -0700910static void start_parent_slice_with_credit(struct throtl_grp *child_tg,
911 struct throtl_grp *parent_tg, bool rw)
912{
913 if (throtl_slice_used(parent_tg, rw)) {
914 throtl_start_new_slice_with_credit(parent_tg, rw,
915 child_tg->slice_start[rw]);
916 }
917
918}
919
Tejun Heo77216b02013-05-14 13:52:36 -0700920static void tg_dispatch_one_bio(struct throtl_grp *tg, bool rw)
Vivek Goyale43473b2010-09-15 17:06:35 -0400921{
Tejun Heo73f0d492013-05-14 13:52:35 -0700922 struct throtl_service_queue *sq = &tg->service_queue;
Tejun Heo6bc9c2b2013-05-14 13:52:38 -0700923 struct throtl_service_queue *parent_sq = sq->parent_sq;
924 struct throtl_grp *parent_tg = sq_to_tg(parent_sq);
Tejun Heoc5cc2072013-05-14 13:52:38 -0700925 struct throtl_grp *tg_to_put = NULL;
Vivek Goyale43473b2010-09-15 17:06:35 -0400926 struct bio *bio;
927
Tejun Heoc5cc2072013-05-14 13:52:38 -0700928 /*
929 * @bio is being transferred from @tg to @parent_sq. Popping a bio
930 * from @tg may put its reference and @parent_sq might end up
931 * getting released prematurely. Remember the tg to put and put it
932 * after @bio is transferred to @parent_sq.
933 */
934 bio = throtl_pop_queued(&sq->queued[rw], &tg_to_put);
Tejun Heo73f0d492013-05-14 13:52:35 -0700935 sq->nr_queued[rw]--;
Vivek Goyale43473b2010-09-15 17:06:35 -0400936
937 throtl_charge_bio(tg, bio);
Tejun Heo6bc9c2b2013-05-14 13:52:38 -0700938
939 /*
940 * If our parent is another tg, we just need to transfer @bio to
941 * the parent using throtl_add_bio_tg(). If our parent is
942 * @td->service_queue, @bio is ready to be issued. Put it on its
943 * bio_lists[] and decrease total number queued. The caller is
944 * responsible for issuing these bios.
945 */
946 if (parent_tg) {
Tejun Heoc5cc2072013-05-14 13:52:38 -0700947 throtl_add_bio_tg(bio, &tg->qnode_on_parent[rw], parent_tg);
Vivek Goyal32ee5bc2013-05-14 13:52:38 -0700948 start_parent_slice_with_credit(tg, parent_tg, rw);
Tejun Heo6bc9c2b2013-05-14 13:52:38 -0700949 } else {
Tejun Heoc5cc2072013-05-14 13:52:38 -0700950 throtl_qnode_add_bio(bio, &tg->qnode_on_parent[rw],
951 &parent_sq->queued[rw]);
Tejun Heo6bc9c2b2013-05-14 13:52:38 -0700952 BUG_ON(tg->td->nr_queued[rw] <= 0);
953 tg->td->nr_queued[rw]--;
954 }
Vivek Goyale43473b2010-09-15 17:06:35 -0400955
Tejun Heo0f3457f2013-05-14 13:52:32 -0700956 throtl_trim_slice(tg, rw);
Tejun Heo6bc9c2b2013-05-14 13:52:38 -0700957
Tejun Heoc5cc2072013-05-14 13:52:38 -0700958 if (tg_to_put)
959 blkg_put(tg_to_blkg(tg_to_put));
Vivek Goyale43473b2010-09-15 17:06:35 -0400960}
961
Tejun Heo77216b02013-05-14 13:52:36 -0700962static int throtl_dispatch_tg(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400963{
Tejun Heo73f0d492013-05-14 13:52:35 -0700964 struct throtl_service_queue *sq = &tg->service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -0400965 unsigned int nr_reads = 0, nr_writes = 0;
966 unsigned int max_nr_reads = throtl_grp_quantum*3/4;
Vivek Goyalc2f68052010-11-15 19:32:42 +0100967 unsigned int max_nr_writes = throtl_grp_quantum - max_nr_reads;
Vivek Goyale43473b2010-09-15 17:06:35 -0400968 struct bio *bio;
969
970 /* Try to dispatch 75% READS and 25% WRITES */
971
Tejun Heoc5cc2072013-05-14 13:52:38 -0700972 while ((bio = throtl_peek_queued(&sq->queued[READ])) &&
Tejun Heo0f3457f2013-05-14 13:52:32 -0700973 tg_may_dispatch(tg, bio, NULL)) {
Vivek Goyale43473b2010-09-15 17:06:35 -0400974
Tejun Heo77216b02013-05-14 13:52:36 -0700975 tg_dispatch_one_bio(tg, bio_data_dir(bio));
Vivek Goyale43473b2010-09-15 17:06:35 -0400976 nr_reads++;
977
978 if (nr_reads >= max_nr_reads)
979 break;
980 }
981
Tejun Heoc5cc2072013-05-14 13:52:38 -0700982 while ((bio = throtl_peek_queued(&sq->queued[WRITE])) &&
Tejun Heo0f3457f2013-05-14 13:52:32 -0700983 tg_may_dispatch(tg, bio, NULL)) {
Vivek Goyale43473b2010-09-15 17:06:35 -0400984
Tejun Heo77216b02013-05-14 13:52:36 -0700985 tg_dispatch_one_bio(tg, bio_data_dir(bio));
Vivek Goyale43473b2010-09-15 17:06:35 -0400986 nr_writes++;
987
988 if (nr_writes >= max_nr_writes)
989 break;
990 }
991
992 return nr_reads + nr_writes;
993}
994
Tejun Heo651930b2013-05-14 13:52:35 -0700995static int throtl_select_dispatch(struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -0400996{
997 unsigned int nr_disp = 0;
Vivek Goyale43473b2010-09-15 17:06:35 -0400998
999 while (1) {
Tejun Heo73f0d492013-05-14 13:52:35 -07001000 struct throtl_grp *tg = throtl_rb_first(parent_sq);
1001 struct throtl_service_queue *sq = &tg->service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -04001002
1003 if (!tg)
1004 break;
1005
1006 if (time_before(jiffies, tg->disptime))
1007 break;
1008
Tejun Heo77216b02013-05-14 13:52:36 -07001009 throtl_dequeue_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -04001010
Tejun Heo77216b02013-05-14 13:52:36 -07001011 nr_disp += throtl_dispatch_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -04001012
Tejun Heo73f0d492013-05-14 13:52:35 -07001013 if (sq->nr_queued[0] || sq->nr_queued[1])
Tejun Heo77216b02013-05-14 13:52:36 -07001014 tg_update_disptime(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -04001015
1016 if (nr_disp >= throtl_quantum)
1017 break;
1018 }
1019
1020 return nr_disp;
1021}
1022
Tejun Heo6e1a5702013-05-14 13:52:37 -07001023/**
1024 * throtl_pending_timer_fn - timer function for service_queue->pending_timer
1025 * @arg: the throtl_service_queue being serviced
1026 *
1027 * This timer is armed when a child throtl_grp with active bio's become
1028 * pending and queued on the service_queue's pending_tree and expires when
1029 * the first child throtl_grp should be dispatched. This function
Tejun Heo2e48a532013-05-14 13:52:38 -07001030 * dispatches bio's from the children throtl_grps to the parent
1031 * service_queue.
1032 *
1033 * If the parent's parent is another throtl_grp, dispatching is propagated
1034 * by either arming its pending_timer or repeating dispatch directly. If
1035 * the top-level service_tree is reached, throtl_data->dispatch_work is
1036 * kicked so that the ready bio's are issued.
Tejun Heo6e1a5702013-05-14 13:52:37 -07001037 */
Tejun Heo69df0ab2013-05-14 13:52:36 -07001038static void throtl_pending_timer_fn(unsigned long arg)
1039{
1040 struct throtl_service_queue *sq = (void *)arg;
Tejun Heo2e48a532013-05-14 13:52:38 -07001041 struct throtl_grp *tg = sq_to_tg(sq);
Tejun Heo69df0ab2013-05-14 13:52:36 -07001042 struct throtl_data *td = sq_to_td(sq);
Tejun Heocb761992013-05-14 13:52:31 -07001043 struct request_queue *q = td->queue;
Tejun Heo2e48a532013-05-14 13:52:38 -07001044 struct throtl_service_queue *parent_sq;
1045 bool dispatched;
Tejun Heo6e1a5702013-05-14 13:52:37 -07001046 int ret;
Vivek Goyale43473b2010-09-15 17:06:35 -04001047
1048 spin_lock_irq(q->queue_lock);
Tejun Heo2e48a532013-05-14 13:52:38 -07001049again:
1050 parent_sq = sq->parent_sq;
1051 dispatched = false;
Vivek Goyale43473b2010-09-15 17:06:35 -04001052
Tejun Heo7f52f982013-05-14 13:52:37 -07001053 while (true) {
1054 throtl_log(sq, "dispatch nr_queued=%u read=%u write=%u",
Tejun Heo2e48a532013-05-14 13:52:38 -07001055 sq->nr_queued[READ] + sq->nr_queued[WRITE],
1056 sq->nr_queued[READ], sq->nr_queued[WRITE]);
Vivek Goyale43473b2010-09-15 17:06:35 -04001057
Tejun Heo7f52f982013-05-14 13:52:37 -07001058 ret = throtl_select_dispatch(sq);
1059 if (ret) {
Tejun Heo7f52f982013-05-14 13:52:37 -07001060 throtl_log(sq, "bios disp=%u", ret);
1061 dispatched = true;
Tejun Heo651930b2013-05-14 13:52:35 -07001062 }
Vivek Goyale43473b2010-09-15 17:06:35 -04001063
Tejun Heo7f52f982013-05-14 13:52:37 -07001064 if (throtl_schedule_next_dispatch(sq, false))
1065 break;
1066
1067 /* this dispatch windows is still open, relax and repeat */
1068 spin_unlock_irq(q->queue_lock);
1069 cpu_relax();
1070 spin_lock_irq(q->queue_lock);
1071 }
Tejun Heo6a525602013-05-14 13:52:32 -07001072
Tejun Heo2e48a532013-05-14 13:52:38 -07001073 if (!dispatched)
1074 goto out_unlock;
Tejun Heo6e1a5702013-05-14 13:52:37 -07001075
Tejun Heo2e48a532013-05-14 13:52:38 -07001076 if (parent_sq) {
1077 /* @parent_sq is another throl_grp, propagate dispatch */
1078 if (tg->flags & THROTL_TG_WAS_EMPTY) {
1079 tg_update_disptime(tg);
1080 if (!throtl_schedule_next_dispatch(parent_sq, false)) {
1081 /* window is already open, repeat dispatching */
1082 sq = parent_sq;
1083 tg = sq_to_tg(sq);
1084 goto again;
1085 }
1086 }
1087 } else {
1088 /* reached the top-level, queue issueing */
1089 queue_work(kthrotld_workqueue, &td->dispatch_work);
1090 }
1091out_unlock:
Tejun Heo6e1a5702013-05-14 13:52:37 -07001092 spin_unlock_irq(q->queue_lock);
1093}
1094
1095/**
1096 * blk_throtl_dispatch_work_fn - work function for throtl_data->dispatch_work
1097 * @work: work item being executed
1098 *
1099 * This function is queued for execution when bio's reach the bio_lists[]
1100 * of throtl_data->service_queue. Those bio's are ready and issued by this
1101 * function.
1102 */
Fabian Frederick8876e142014-04-17 21:41:16 +02001103static void blk_throtl_dispatch_work_fn(struct work_struct *work)
Tejun Heo6e1a5702013-05-14 13:52:37 -07001104{
1105 struct throtl_data *td = container_of(work, struct throtl_data,
1106 dispatch_work);
1107 struct throtl_service_queue *td_sq = &td->service_queue;
1108 struct request_queue *q = td->queue;
1109 struct bio_list bio_list_on_stack;
1110 struct bio *bio;
1111 struct blk_plug plug;
1112 int rw;
1113
1114 bio_list_init(&bio_list_on_stack);
1115
1116 spin_lock_irq(q->queue_lock);
Tejun Heoc5cc2072013-05-14 13:52:38 -07001117 for (rw = READ; rw <= WRITE; rw++)
1118 while ((bio = throtl_pop_queued(&td_sq->queued[rw], NULL)))
1119 bio_list_add(&bio_list_on_stack, bio);
Vivek Goyale43473b2010-09-15 17:06:35 -04001120 spin_unlock_irq(q->queue_lock);
1121
Tejun Heo6e1a5702013-05-14 13:52:37 -07001122 if (!bio_list_empty(&bio_list_on_stack)) {
Vivek Goyal69d60eb2011-03-09 08:27:37 +01001123 blk_start_plug(&plug);
Vivek Goyale43473b2010-09-15 17:06:35 -04001124 while((bio = bio_list_pop(&bio_list_on_stack)))
1125 generic_make_request(bio);
Vivek Goyal69d60eb2011-03-09 08:27:37 +01001126 blk_finish_plug(&plug);
Vivek Goyale43473b2010-09-15 17:06:35 -04001127 }
Vivek Goyale43473b2010-09-15 17:06:35 -04001128}
1129
Tejun Heof95a04a2012-04-16 13:57:26 -07001130static u64 tg_prfill_conf_u64(struct seq_file *sf, struct blkg_policy_data *pd,
1131 int off)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001132{
Tejun Heof95a04a2012-04-16 13:57:26 -07001133 struct throtl_grp *tg = pd_to_tg(pd);
1134 u64 v = *(u64 *)((void *)tg + off);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001135
Shaohua Li2ab54922017-03-27 10:51:29 -07001136 if (v == U64_MAX)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001137 return 0;
Tejun Heof95a04a2012-04-16 13:57:26 -07001138 return __blkg_prfill_u64(sf, pd, v);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001139}
1140
Tejun Heof95a04a2012-04-16 13:57:26 -07001141static u64 tg_prfill_conf_uint(struct seq_file *sf, struct blkg_policy_data *pd,
1142 int off)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001143{
Tejun Heof95a04a2012-04-16 13:57:26 -07001144 struct throtl_grp *tg = pd_to_tg(pd);
1145 unsigned int v = *(unsigned int *)((void *)tg + off);
Tejun Heoaf133ce2012-04-01 14:38:44 -07001146
Shaohua Li2ab54922017-03-27 10:51:29 -07001147 if (v == UINT_MAX)
Tejun Heoaf133ce2012-04-01 14:38:44 -07001148 return 0;
Tejun Heof95a04a2012-04-16 13:57:26 -07001149 return __blkg_prfill_u64(sf, pd, v);
Tejun Heoaf133ce2012-04-01 14:38:44 -07001150}
1151
Tejun Heo2da8ca82013-12-05 12:28:04 -05001152static int tg_print_conf_u64(struct seq_file *sf, void *v)
Tejun Heoaf133ce2012-04-01 14:38:44 -07001153{
Tejun Heo2da8ca82013-12-05 12:28:04 -05001154 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), tg_prfill_conf_u64,
1155 &blkcg_policy_throtl, seq_cft(sf)->private, false);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001156 return 0;
1157}
1158
Tejun Heo2da8ca82013-12-05 12:28:04 -05001159static int tg_print_conf_uint(struct seq_file *sf, void *v)
Vivek Goyale43473b2010-09-15 17:06:35 -04001160{
Tejun Heo2da8ca82013-12-05 12:28:04 -05001161 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), tg_prfill_conf_uint,
1162 &blkcg_policy_throtl, seq_cft(sf)->private, false);
Tejun Heoaf133ce2012-04-01 14:38:44 -07001163 return 0;
Vivek Goyale43473b2010-09-15 17:06:35 -04001164}
1165
Tejun Heo69948b02015-08-18 14:55:32 -07001166static void tg_conf_updated(struct throtl_grp *tg)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001167{
Tejun Heo69948b02015-08-18 14:55:32 -07001168 struct throtl_service_queue *sq = &tg->service_queue;
Tejun Heo492eb212013-08-08 20:11:25 -04001169 struct cgroup_subsys_state *pos_css;
Tejun Heo69948b02015-08-18 14:55:32 -07001170 struct blkcg_gq *blkg;
Tejun Heoaf133ce2012-04-01 14:38:44 -07001171
Tejun Heofda6f272013-05-14 13:52:36 -07001172 throtl_log(&tg->service_queue,
1173 "limit change rbps=%llu wbps=%llu riops=%u wiops=%u",
Shaohua Li9f626e32017-03-27 10:51:30 -07001174 tg_bps_limit(tg, READ), tg_bps_limit(tg, WRITE),
1175 tg_iops_limit(tg, READ), tg_iops_limit(tg, WRITE));
Tejun Heo632b4492013-05-14 13:52:31 -07001176
1177 /*
Tejun Heo693e7512013-05-14 13:52:38 -07001178 * Update has_rules[] flags for the updated tg's subtree. A tg is
1179 * considered to have rules if either the tg itself or any of its
1180 * ancestors has rules. This identifies groups without any
1181 * restrictions in the whole hierarchy and allows them to bypass
1182 * blk-throttle.
1183 */
Tejun Heo69948b02015-08-18 14:55:32 -07001184 blkg_for_each_descendant_pre(blkg, pos_css, tg_to_blkg(tg))
Tejun Heo693e7512013-05-14 13:52:38 -07001185 tg_update_has_rules(blkg_to_tg(blkg));
1186
1187 /*
Tejun Heo632b4492013-05-14 13:52:31 -07001188 * We're already holding queue_lock and know @tg is valid. Let's
1189 * apply the new config directly.
1190 *
1191 * Restart the slices for both READ and WRITES. It might happen
1192 * that a group's limit are dropped suddenly and we don't want to
1193 * account recently dispatched IO with new low rate.
1194 */
Tejun Heo0f3457f2013-05-14 13:52:32 -07001195 throtl_start_new_slice(tg, 0);
1196 throtl_start_new_slice(tg, 1);
Tejun Heo632b4492013-05-14 13:52:31 -07001197
Tejun Heo5b2c16a2013-05-14 13:52:32 -07001198 if (tg->flags & THROTL_TG_PENDING) {
Tejun Heo77216b02013-05-14 13:52:36 -07001199 tg_update_disptime(tg);
Tejun Heo7f52f982013-05-14 13:52:37 -07001200 throtl_schedule_next_dispatch(sq->parent_sq, true);
Tejun Heo632b4492013-05-14 13:52:31 -07001201 }
Tejun Heo69948b02015-08-18 14:55:32 -07001202}
Tejun Heo60c2bc22012-04-01 14:38:43 -07001203
Tejun Heo69948b02015-08-18 14:55:32 -07001204static ssize_t tg_set_conf(struct kernfs_open_file *of,
1205 char *buf, size_t nbytes, loff_t off, bool is_u64)
1206{
1207 struct blkcg *blkcg = css_to_blkcg(of_css(of));
1208 struct blkg_conf_ctx ctx;
1209 struct throtl_grp *tg;
1210 int ret;
1211 u64 v;
1212
1213 ret = blkg_conf_prep(blkcg, &blkcg_policy_throtl, buf, &ctx);
1214 if (ret)
1215 return ret;
1216
1217 ret = -EINVAL;
1218 if (sscanf(ctx.body, "%llu", &v) != 1)
1219 goto out_finish;
1220 if (!v)
Shaohua Li2ab54922017-03-27 10:51:29 -07001221 v = U64_MAX;
Tejun Heo69948b02015-08-18 14:55:32 -07001222
1223 tg = blkg_to_tg(ctx.blkg);
1224
1225 if (is_u64)
1226 *(u64 *)((void *)tg + of_cft(of)->private) = v;
1227 else
1228 *(unsigned int *)((void *)tg + of_cft(of)->private) = v;
1229
1230 tg_conf_updated(tg);
Tejun Heo36aa9e52015-08-18 14:55:31 -07001231 ret = 0;
1232out_finish:
Tejun Heo60c2bc22012-04-01 14:38:43 -07001233 blkg_conf_finish(&ctx);
Tejun Heo36aa9e52015-08-18 14:55:31 -07001234 return ret ?: nbytes;
Tejun Heo60c2bc22012-04-01 14:38:43 -07001235}
1236
Tejun Heo451af502014-05-13 12:16:21 -04001237static ssize_t tg_set_conf_u64(struct kernfs_open_file *of,
1238 char *buf, size_t nbytes, loff_t off)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001239{
Tejun Heo451af502014-05-13 12:16:21 -04001240 return tg_set_conf(of, buf, nbytes, off, true);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001241}
1242
Tejun Heo451af502014-05-13 12:16:21 -04001243static ssize_t tg_set_conf_uint(struct kernfs_open_file *of,
1244 char *buf, size_t nbytes, loff_t off)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001245{
Tejun Heo451af502014-05-13 12:16:21 -04001246 return tg_set_conf(of, buf, nbytes, off, false);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001247}
1248
Tejun Heo880f50e2015-08-18 14:55:30 -07001249static struct cftype throtl_legacy_files[] = {
Tejun Heo60c2bc22012-04-01 14:38:43 -07001250 {
1251 .name = "throttle.read_bps_device",
Shaohua Li9f626e32017-03-27 10:51:30 -07001252 .private = offsetof(struct throtl_grp, bps[READ][LIMIT_MAX]),
Tejun Heo2da8ca82013-12-05 12:28:04 -05001253 .seq_show = tg_print_conf_u64,
Tejun Heo451af502014-05-13 12:16:21 -04001254 .write = tg_set_conf_u64,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001255 },
1256 {
1257 .name = "throttle.write_bps_device",
Shaohua Li9f626e32017-03-27 10:51:30 -07001258 .private = offsetof(struct throtl_grp, bps[WRITE][LIMIT_MAX]),
Tejun Heo2da8ca82013-12-05 12:28:04 -05001259 .seq_show = tg_print_conf_u64,
Tejun Heo451af502014-05-13 12:16:21 -04001260 .write = tg_set_conf_u64,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001261 },
1262 {
1263 .name = "throttle.read_iops_device",
Shaohua Li9f626e32017-03-27 10:51:30 -07001264 .private = offsetof(struct throtl_grp, iops[READ][LIMIT_MAX]),
Tejun Heo2da8ca82013-12-05 12:28:04 -05001265 .seq_show = tg_print_conf_uint,
Tejun Heo451af502014-05-13 12:16:21 -04001266 .write = tg_set_conf_uint,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001267 },
1268 {
1269 .name = "throttle.write_iops_device",
Shaohua Li9f626e32017-03-27 10:51:30 -07001270 .private = offsetof(struct throtl_grp, iops[WRITE][LIMIT_MAX]),
Tejun Heo2da8ca82013-12-05 12:28:04 -05001271 .seq_show = tg_print_conf_uint,
Tejun Heo451af502014-05-13 12:16:21 -04001272 .write = tg_set_conf_uint,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001273 },
1274 {
1275 .name = "throttle.io_service_bytes",
Tejun Heo77ea7332015-08-18 14:55:24 -07001276 .private = (unsigned long)&blkcg_policy_throtl,
1277 .seq_show = blkg_print_stat_bytes,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001278 },
1279 {
1280 .name = "throttle.io_serviced",
Tejun Heo77ea7332015-08-18 14:55:24 -07001281 .private = (unsigned long)&blkcg_policy_throtl,
1282 .seq_show = blkg_print_stat_ios,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001283 },
1284 { } /* terminate */
1285};
1286
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001287static u64 tg_prfill_max(struct seq_file *sf, struct blkg_policy_data *pd,
1288 int off)
1289{
1290 struct throtl_grp *tg = pd_to_tg(pd);
1291 const char *dname = blkg_dev_name(pd->blkg);
1292 char bufs[4][21] = { "max", "max", "max", "max" };
1293
1294 if (!dname)
1295 return 0;
Shaohua Li9f626e32017-03-27 10:51:30 -07001296
1297 if (tg->bps[READ][LIMIT_MAX] == U64_MAX &&
1298 tg->bps[WRITE][LIMIT_MAX] == U64_MAX &&
1299 tg->iops[READ][LIMIT_MAX] == UINT_MAX &&
1300 tg->iops[WRITE][LIMIT_MAX] == UINT_MAX)
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001301 return 0;
1302
Shaohua Li9f626e32017-03-27 10:51:30 -07001303 if (tg->bps[READ][LIMIT_MAX] != U64_MAX)
1304 snprintf(bufs[0], sizeof(bufs[0]), "%llu",
1305 tg->bps[READ][LIMIT_MAX]);
1306 if (tg->bps[WRITE][LIMIT_MAX] != U64_MAX)
1307 snprintf(bufs[1], sizeof(bufs[1]), "%llu",
1308 tg->bps[WRITE][LIMIT_MAX]);
1309 if (tg->iops[READ][LIMIT_MAX] != UINT_MAX)
1310 snprintf(bufs[2], sizeof(bufs[2]), "%u",
1311 tg->iops[READ][LIMIT_MAX]);
1312 if (tg->iops[WRITE][LIMIT_MAX] != UINT_MAX)
1313 snprintf(bufs[3], sizeof(bufs[3]), "%u",
1314 tg->iops[WRITE][LIMIT_MAX]);
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001315
1316 seq_printf(sf, "%s rbps=%s wbps=%s riops=%s wiops=%s\n",
1317 dname, bufs[0], bufs[1], bufs[2], bufs[3]);
1318 return 0;
1319}
1320
1321static int tg_print_max(struct seq_file *sf, void *v)
1322{
1323 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), tg_prfill_max,
1324 &blkcg_policy_throtl, seq_cft(sf)->private, false);
1325 return 0;
1326}
1327
1328static ssize_t tg_set_max(struct kernfs_open_file *of,
1329 char *buf, size_t nbytes, loff_t off)
1330{
1331 struct blkcg *blkcg = css_to_blkcg(of_css(of));
1332 struct blkg_conf_ctx ctx;
1333 struct throtl_grp *tg;
1334 u64 v[4];
1335 int ret;
1336
1337 ret = blkg_conf_prep(blkcg, &blkcg_policy_throtl, buf, &ctx);
1338 if (ret)
1339 return ret;
1340
1341 tg = blkg_to_tg(ctx.blkg);
1342
Shaohua Li9f626e32017-03-27 10:51:30 -07001343 v[0] = tg->bps[READ][LIMIT_MAX];
1344 v[1] = tg->bps[WRITE][LIMIT_MAX];
1345 v[2] = tg->iops[READ][LIMIT_MAX];
1346 v[3] = tg->iops[WRITE][LIMIT_MAX];
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001347
1348 while (true) {
1349 char tok[27]; /* wiops=18446744073709551616 */
1350 char *p;
Shaohua Li2ab54922017-03-27 10:51:29 -07001351 u64 val = U64_MAX;
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001352 int len;
1353
1354 if (sscanf(ctx.body, "%26s%n", tok, &len) != 1)
1355 break;
1356 if (tok[0] == '\0')
1357 break;
1358 ctx.body += len;
1359
1360 ret = -EINVAL;
1361 p = tok;
1362 strsep(&p, "=");
1363 if (!p || (sscanf(p, "%llu", &val) != 1 && strcmp(p, "max")))
1364 goto out_finish;
1365
1366 ret = -ERANGE;
1367 if (!val)
1368 goto out_finish;
1369
1370 ret = -EINVAL;
1371 if (!strcmp(tok, "rbps"))
1372 v[0] = val;
1373 else if (!strcmp(tok, "wbps"))
1374 v[1] = val;
1375 else if (!strcmp(tok, "riops"))
1376 v[2] = min_t(u64, val, UINT_MAX);
1377 else if (!strcmp(tok, "wiops"))
1378 v[3] = min_t(u64, val, UINT_MAX);
1379 else
1380 goto out_finish;
1381 }
1382
Shaohua Li9f626e32017-03-27 10:51:30 -07001383 tg->bps[READ][LIMIT_MAX] = v[0];
1384 tg->bps[WRITE][LIMIT_MAX] = v[1];
1385 tg->iops[READ][LIMIT_MAX] = v[2];
1386 tg->iops[WRITE][LIMIT_MAX] = v[3];
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001387
1388 tg_conf_updated(tg);
1389 ret = 0;
1390out_finish:
1391 blkg_conf_finish(&ctx);
1392 return ret ?: nbytes;
1393}
1394
1395static struct cftype throtl_files[] = {
1396 {
1397 .name = "max",
1398 .flags = CFTYPE_NOT_ON_ROOT,
1399 .seq_show = tg_print_max,
1400 .write = tg_set_max,
1401 },
1402 { } /* terminate */
1403};
1404
Vivek Goyalda527772011-03-02 19:05:33 -05001405static void throtl_shutdown_wq(struct request_queue *q)
Vivek Goyale43473b2010-09-15 17:06:35 -04001406{
1407 struct throtl_data *td = q->td;
1408
Tejun Heo69df0ab2013-05-14 13:52:36 -07001409 cancel_work_sync(&td->dispatch_work);
Vivek Goyale43473b2010-09-15 17:06:35 -04001410}
1411
Tejun Heo3c798392012-04-16 13:57:25 -07001412static struct blkcg_policy blkcg_policy_throtl = {
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001413 .dfl_cftypes = throtl_files,
Tejun Heo880f50e2015-08-18 14:55:30 -07001414 .legacy_cftypes = throtl_legacy_files,
Tejun Heof9fcc2d2012-04-16 13:57:27 -07001415
Tejun Heo001bea72015-08-18 14:55:11 -07001416 .pd_alloc_fn = throtl_pd_alloc,
Tejun Heof9fcc2d2012-04-16 13:57:27 -07001417 .pd_init_fn = throtl_pd_init,
Tejun Heo693e7512013-05-14 13:52:38 -07001418 .pd_online_fn = throtl_pd_online,
Tejun Heo001bea72015-08-18 14:55:11 -07001419 .pd_free_fn = throtl_pd_free,
Vivek Goyale43473b2010-09-15 17:06:35 -04001420};
1421
Tejun Heoae118892015-08-18 14:55:20 -07001422bool blk_throtl_bio(struct request_queue *q, struct blkcg_gq *blkg,
1423 struct bio *bio)
Vivek Goyale43473b2010-09-15 17:06:35 -04001424{
Tejun Heoc5cc2072013-05-14 13:52:38 -07001425 struct throtl_qnode *qn = NULL;
Tejun Heoae118892015-08-18 14:55:20 -07001426 struct throtl_grp *tg = blkg_to_tg(blkg ?: q->root_blkg);
Tejun Heo73f0d492013-05-14 13:52:35 -07001427 struct throtl_service_queue *sq;
Tejun Heo0e9f4162013-05-14 13:52:35 -07001428 bool rw = bio_data_dir(bio);
Tejun Heobc16a4f2011-10-19 14:33:01 +02001429 bool throttled = false;
Vivek Goyale43473b2010-09-15 17:06:35 -04001430
Tejun Heoae118892015-08-18 14:55:20 -07001431 WARN_ON_ONCE(!rcu_read_lock_held());
1432
Tejun Heo2a0f61e2013-05-14 13:52:36 -07001433 /* see throtl_charge_bio() */
Christoph Hellwig8d2bbd42016-10-20 15:12:12 +02001434 if (bio_flagged(bio, BIO_THROTTLED) || !tg->has_rules[rw])
Tejun Heobc16a4f2011-10-19 14:33:01 +02001435 goto out;
Vivek Goyale43473b2010-09-15 17:06:35 -04001436
1437 spin_lock_irq(q->queue_lock);
Tejun Heoc9589f02015-08-18 14:55:19 -07001438
1439 if (unlikely(blk_queue_bypass(q)))
Tejun Heobc16a4f2011-10-19 14:33:01 +02001440 goto out_unlock;
Vivek Goyalf469a7b2011-05-19 15:38:23 -04001441
Tejun Heo73f0d492013-05-14 13:52:35 -07001442 sq = &tg->service_queue;
1443
Tejun Heo9e660ac2013-05-14 13:52:38 -07001444 while (true) {
1445 /* throtl is FIFO - if bios are already queued, should queue */
1446 if (sq->nr_queued[rw])
1447 break;
Vivek Goyalde701c72011-03-07 21:09:32 +01001448
Tejun Heo9e660ac2013-05-14 13:52:38 -07001449 /* if above limits, break to queue */
1450 if (!tg_may_dispatch(tg, bio, NULL))
1451 break;
1452
1453 /* within limits, let's charge and dispatch directly */
Vivek Goyale43473b2010-09-15 17:06:35 -04001454 throtl_charge_bio(tg, bio);
Vivek Goyal04521db2011-03-22 21:54:29 +01001455
1456 /*
1457 * We need to trim slice even when bios are not being queued
1458 * otherwise it might happen that a bio is not queued for
1459 * a long time and slice keeps on extending and trim is not
1460 * called for a long time. Now if limits are reduced suddenly
1461 * we take into account all the IO dispatched so far at new
1462 * low rate and * newly queued IO gets a really long dispatch
1463 * time.
1464 *
1465 * So keep on trimming slice even if bio is not queued.
1466 */
Tejun Heo0f3457f2013-05-14 13:52:32 -07001467 throtl_trim_slice(tg, rw);
Tejun Heo9e660ac2013-05-14 13:52:38 -07001468
1469 /*
1470 * @bio passed through this layer without being throttled.
1471 * Climb up the ladder. If we''re already at the top, it
1472 * can be executed directly.
1473 */
Tejun Heoc5cc2072013-05-14 13:52:38 -07001474 qn = &tg->qnode_on_parent[rw];
Tejun Heo9e660ac2013-05-14 13:52:38 -07001475 sq = sq->parent_sq;
1476 tg = sq_to_tg(sq);
1477 if (!tg)
1478 goto out_unlock;
Vivek Goyale43473b2010-09-15 17:06:35 -04001479 }
1480
Tejun Heo9e660ac2013-05-14 13:52:38 -07001481 /* out-of-limit, queue to @tg */
Tejun Heofda6f272013-05-14 13:52:36 -07001482 throtl_log(sq, "[%c] bio. bdisp=%llu sz=%u bps=%llu iodisp=%u iops=%u queued=%d/%d",
1483 rw == READ ? 'R' : 'W',
Shaohua Li9f626e32017-03-27 10:51:30 -07001484 tg->bytes_disp[rw], bio->bi_iter.bi_size,
1485 tg_bps_limit(tg, rw),
1486 tg->io_disp[rw], tg_iops_limit(tg, rw),
Tejun Heofda6f272013-05-14 13:52:36 -07001487 sq->nr_queued[READ], sq->nr_queued[WRITE]);
Vivek Goyale43473b2010-09-15 17:06:35 -04001488
Tejun Heo671058f2012-03-05 13:15:29 -08001489 bio_associate_current(bio);
Tejun Heo6bc9c2b2013-05-14 13:52:38 -07001490 tg->td->nr_queued[rw]++;
Tejun Heoc5cc2072013-05-14 13:52:38 -07001491 throtl_add_bio_tg(bio, qn, tg);
Tejun Heobc16a4f2011-10-19 14:33:01 +02001492 throttled = true;
Vivek Goyale43473b2010-09-15 17:06:35 -04001493
Tejun Heo7f52f982013-05-14 13:52:37 -07001494 /*
1495 * Update @tg's dispatch time and force schedule dispatch if @tg
1496 * was empty before @bio. The forced scheduling isn't likely to
1497 * cause undue delay as @bio is likely to be dispatched directly if
1498 * its @tg's disptime is not in the future.
1499 */
Tejun Heo0e9f4162013-05-14 13:52:35 -07001500 if (tg->flags & THROTL_TG_WAS_EMPTY) {
Tejun Heo77216b02013-05-14 13:52:36 -07001501 tg_update_disptime(tg);
Tejun Heo7f52f982013-05-14 13:52:37 -07001502 throtl_schedule_next_dispatch(tg->service_queue.parent_sq, true);
Vivek Goyale43473b2010-09-15 17:06:35 -04001503 }
1504
Tejun Heobc16a4f2011-10-19 14:33:01 +02001505out_unlock:
Vivek Goyale43473b2010-09-15 17:06:35 -04001506 spin_unlock_irq(q->queue_lock);
Tejun Heobc16a4f2011-10-19 14:33:01 +02001507out:
Tejun Heo2a0f61e2013-05-14 13:52:36 -07001508 /*
1509 * As multiple blk-throtls may stack in the same issue path, we
1510 * don't want bios to leave with the flag set. Clear the flag if
1511 * being issued.
1512 */
1513 if (!throttled)
Christoph Hellwig8d2bbd42016-10-20 15:12:12 +02001514 bio_clear_flag(bio, BIO_THROTTLED);
Tejun Heobc16a4f2011-10-19 14:33:01 +02001515 return throttled;
Vivek Goyale43473b2010-09-15 17:06:35 -04001516}
1517
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001518/*
1519 * Dispatch all bios from all children tg's queued on @parent_sq. On
1520 * return, @parent_sq is guaranteed to not have any active children tg's
1521 * and all bios from previously active tg's are on @parent_sq->bio_lists[].
1522 */
1523static void tg_drain_bios(struct throtl_service_queue *parent_sq)
1524{
1525 struct throtl_grp *tg;
1526
1527 while ((tg = throtl_rb_first(parent_sq))) {
1528 struct throtl_service_queue *sq = &tg->service_queue;
1529 struct bio *bio;
1530
1531 throtl_dequeue_tg(tg);
1532
Tejun Heoc5cc2072013-05-14 13:52:38 -07001533 while ((bio = throtl_peek_queued(&sq->queued[READ])))
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001534 tg_dispatch_one_bio(tg, bio_data_dir(bio));
Tejun Heoc5cc2072013-05-14 13:52:38 -07001535 while ((bio = throtl_peek_queued(&sq->queued[WRITE])))
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001536 tg_dispatch_one_bio(tg, bio_data_dir(bio));
1537 }
1538}
1539
Tejun Heoc9a929d2011-10-19 14:42:16 +02001540/**
1541 * blk_throtl_drain - drain throttled bios
1542 * @q: request_queue to drain throttled bios for
1543 *
1544 * Dispatch all currently throttled bios on @q through ->make_request_fn().
1545 */
1546void blk_throtl_drain(struct request_queue *q)
1547 __releases(q->queue_lock) __acquires(q->queue_lock)
1548{
1549 struct throtl_data *td = q->td;
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001550 struct blkcg_gq *blkg;
Tejun Heo492eb212013-08-08 20:11:25 -04001551 struct cgroup_subsys_state *pos_css;
Tejun Heoc9a929d2011-10-19 14:42:16 +02001552 struct bio *bio;
Tejun Heo651930b2013-05-14 13:52:35 -07001553 int rw;
Tejun Heoc9a929d2011-10-19 14:42:16 +02001554
Andi Kleen8bcb6c72012-03-30 12:33:28 +02001555 queue_lockdep_assert_held(q);
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001556 rcu_read_lock();
Tejun Heoc9a929d2011-10-19 14:42:16 +02001557
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001558 /*
1559 * Drain each tg while doing post-order walk on the blkg tree, so
1560 * that all bios are propagated to td->service_queue. It'd be
1561 * better to walk service_queue tree directly but blkg walk is
1562 * easier.
1563 */
Tejun Heo492eb212013-08-08 20:11:25 -04001564 blkg_for_each_descendant_post(blkg, pos_css, td->queue->root_blkg)
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001565 tg_drain_bios(&blkg_to_tg(blkg)->service_queue);
Tejun Heo73f0d492013-05-14 13:52:35 -07001566
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001567 /* finally, transfer bios from top-level tg's into the td */
1568 tg_drain_bios(&td->service_queue);
1569
1570 rcu_read_unlock();
Tejun Heoc9a929d2011-10-19 14:42:16 +02001571 spin_unlock_irq(q->queue_lock);
1572
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001573 /* all bios now should be in td->service_queue, issue them */
Tejun Heo651930b2013-05-14 13:52:35 -07001574 for (rw = READ; rw <= WRITE; rw++)
Tejun Heoc5cc2072013-05-14 13:52:38 -07001575 while ((bio = throtl_pop_queued(&td->service_queue.queued[rw],
1576 NULL)))
Tejun Heo651930b2013-05-14 13:52:35 -07001577 generic_make_request(bio);
Tejun Heoc9a929d2011-10-19 14:42:16 +02001578
1579 spin_lock_irq(q->queue_lock);
1580}
1581
Vivek Goyale43473b2010-09-15 17:06:35 -04001582int blk_throtl_init(struct request_queue *q)
1583{
1584 struct throtl_data *td;
Tejun Heoa2b16932012-04-13 13:11:33 -07001585 int ret;
Vivek Goyale43473b2010-09-15 17:06:35 -04001586
1587 td = kzalloc_node(sizeof(*td), GFP_KERNEL, q->node);
1588 if (!td)
1589 return -ENOMEM;
1590
Tejun Heo69df0ab2013-05-14 13:52:36 -07001591 INIT_WORK(&td->dispatch_work, blk_throtl_dispatch_work_fn);
Tejun Heob2ce2642015-08-18 14:55:13 -07001592 throtl_service_queue_init(&td->service_queue);
Vivek Goyale43473b2010-09-15 17:06:35 -04001593
Tejun Heocd1604f2012-03-05 13:15:06 -08001594 q->td = td;
Vivek Goyal29b12582011-05-19 15:38:24 -04001595 td->queue = q;
Vivek Goyal02977e42010-10-01 14:49:48 +02001596
Shaohua Li9f626e32017-03-27 10:51:30 -07001597 td->limit_valid[LIMIT_MAX] = true;
Tejun Heoa2b16932012-04-13 13:11:33 -07001598 /* activate policy */
Tejun Heo3c798392012-04-16 13:57:25 -07001599 ret = blkcg_activate_policy(q, &blkcg_policy_throtl);
Tejun Heoa2b16932012-04-13 13:11:33 -07001600 if (ret)
Vivek Goyal29b12582011-05-19 15:38:24 -04001601 kfree(td);
Tejun Heoa2b16932012-04-13 13:11:33 -07001602 return ret;
Vivek Goyale43473b2010-09-15 17:06:35 -04001603}
1604
1605void blk_throtl_exit(struct request_queue *q)
1606{
Tejun Heoc875f4d2012-03-05 13:15:22 -08001607 BUG_ON(!q->td);
Vivek Goyalda527772011-03-02 19:05:33 -05001608 throtl_shutdown_wq(q);
Tejun Heo3c798392012-04-16 13:57:25 -07001609 blkcg_deactivate_policy(q, &blkcg_policy_throtl);
Tejun Heoc9a929d2011-10-19 14:42:16 +02001610 kfree(q->td);
Vivek Goyale43473b2010-09-15 17:06:35 -04001611}
1612
1613static int __init throtl_init(void)
1614{
Vivek Goyal450adcb2011-03-01 13:40:54 -05001615 kthrotld_workqueue = alloc_workqueue("kthrotld", WQ_MEM_RECLAIM, 0);
1616 if (!kthrotld_workqueue)
1617 panic("Failed to create kthrotld\n");
1618
Tejun Heo3c798392012-04-16 13:57:25 -07001619 return blkcg_policy_register(&blkcg_policy_throtl);
Vivek Goyale43473b2010-09-15 17:06:35 -04001620}
1621
1622module_init(throtl_init);