blob: b7b69ecb6e9679ff7cc35e58f2ecc4c3d4a47ee9 [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 {
Shaohua Licd5ab1b2017-03-27 10:51:32 -070087 LIMIT_LOW,
Shaohua Li9f626e32017-03-27 10:51:30 -070088 LIMIT_MAX,
89 LIMIT_CNT,
90};
91
Vivek Goyale43473b2010-09-15 17:06:35 -040092struct throtl_grp {
Tejun Heof95a04a2012-04-16 13:57:26 -070093 /* must be the first member */
94 struct blkg_policy_data pd;
95
Tejun Heoc9e03322013-05-14 13:52:32 -070096 /* active throtl group service_queue member */
Vivek Goyale43473b2010-09-15 17:06:35 -040097 struct rb_node rb_node;
98
Tejun Heo0f3457f2013-05-14 13:52:32 -070099 /* throtl_data this group belongs to */
100 struct throtl_data *td;
101
Tejun Heo49a2f1e2013-05-14 13:52:34 -0700102 /* this group's service queue */
103 struct throtl_service_queue service_queue;
104
Vivek Goyale43473b2010-09-15 17:06:35 -0400105 /*
Tejun Heoc5cc2072013-05-14 13:52:38 -0700106 * qnode_on_self is used when bios are directly queued to this
107 * throtl_grp so that local bios compete fairly with bios
108 * dispatched from children. qnode_on_parent is used when bios are
109 * dispatched from this throtl_grp into its parent and will compete
110 * with the sibling qnode_on_parents and the parent's
111 * qnode_on_self.
112 */
113 struct throtl_qnode qnode_on_self[2];
114 struct throtl_qnode qnode_on_parent[2];
115
116 /*
Vivek Goyale43473b2010-09-15 17:06:35 -0400117 * Dispatch time in jiffies. This is the estimated time when group
118 * will unthrottle and is ready to dispatch more bio. It is used as
119 * key to sort active groups in service tree.
120 */
121 unsigned long disptime;
122
Vivek Goyale43473b2010-09-15 17:06:35 -0400123 unsigned int flags;
124
Tejun Heo693e7512013-05-14 13:52:38 -0700125 /* are there any throtl rules between this group and td? */
126 bool has_rules[2];
127
Shaohua Licd5ab1b2017-03-27 10:51:32 -0700128 /* internally used bytes per second rate limits */
Shaohua Li9f626e32017-03-27 10:51:30 -0700129 uint64_t bps[2][LIMIT_CNT];
Shaohua Licd5ab1b2017-03-27 10:51:32 -0700130 /* user configured bps limits */
131 uint64_t bps_conf[2][LIMIT_CNT];
Vivek Goyale43473b2010-09-15 17:06:35 -0400132
Shaohua Licd5ab1b2017-03-27 10:51:32 -0700133 /* internally used IOPS limits */
Shaohua Li9f626e32017-03-27 10:51:30 -0700134 unsigned int iops[2][LIMIT_CNT];
Shaohua Licd5ab1b2017-03-27 10:51:32 -0700135 /* user configured IOPS limits */
136 unsigned int iops_conf[2][LIMIT_CNT];
Vivek Goyal8e89d132010-09-15 17:06:37 -0400137
Vivek Goyale43473b2010-09-15 17:06:35 -0400138 /* Number of bytes disptached in current slice */
139 uint64_t bytes_disp[2];
Vivek Goyal8e89d132010-09-15 17:06:37 -0400140 /* Number of bio's dispatched in current slice */
141 unsigned int io_disp[2];
Vivek Goyale43473b2010-09-15 17:06:35 -0400142
143 /* When did we start a new slice */
144 unsigned long slice_start[2];
145 unsigned long slice_end[2];
146};
147
148struct throtl_data
149{
Vivek Goyale43473b2010-09-15 17:06:35 -0400150 /* service tree for active throtl groups */
Tejun Heoc9e03322013-05-14 13:52:32 -0700151 struct throtl_service_queue service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -0400152
Vivek Goyale43473b2010-09-15 17:06:35 -0400153 struct request_queue *queue;
154
155 /* Total Number of queued bios on READ and WRITE lists */
156 unsigned int nr_queued[2];
157
Vivek Goyale43473b2010-09-15 17:06:35 -0400158 /* Work for dispatching throttled bios */
Tejun Heo69df0ab2013-05-14 13:52:36 -0700159 struct work_struct dispatch_work;
Shaohua Li9f626e32017-03-27 10:51:30 -0700160 unsigned int limit_index;
161 bool limit_valid[LIMIT_CNT];
Vivek Goyale43473b2010-09-15 17:06:35 -0400162};
163
Tejun Heo69df0ab2013-05-14 13:52:36 -0700164static void throtl_pending_timer_fn(unsigned long arg);
165
Tejun Heof95a04a2012-04-16 13:57:26 -0700166static inline struct throtl_grp *pd_to_tg(struct blkg_policy_data *pd)
167{
168 return pd ? container_of(pd, struct throtl_grp, pd) : NULL;
169}
170
Tejun Heo3c798392012-04-16 13:57:25 -0700171static inline struct throtl_grp *blkg_to_tg(struct blkcg_gq *blkg)
Tejun Heo03814112012-03-05 13:15:14 -0800172{
Tejun Heof95a04a2012-04-16 13:57:26 -0700173 return pd_to_tg(blkg_to_pd(blkg, &blkcg_policy_throtl));
Tejun Heo03814112012-03-05 13:15:14 -0800174}
175
Tejun Heo3c798392012-04-16 13:57:25 -0700176static inline struct blkcg_gq *tg_to_blkg(struct throtl_grp *tg)
Tejun Heo03814112012-03-05 13:15:14 -0800177{
Tejun Heof95a04a2012-04-16 13:57:26 -0700178 return pd_to_blkg(&tg->pd);
Tejun Heo03814112012-03-05 13:15:14 -0800179}
180
Tejun Heofda6f272013-05-14 13:52:36 -0700181/**
182 * sq_to_tg - return the throl_grp the specified service queue belongs to
183 * @sq: the throtl_service_queue of interest
184 *
185 * Return the throtl_grp @sq belongs to. If @sq is the top-level one
186 * embedded in throtl_data, %NULL is returned.
187 */
188static struct throtl_grp *sq_to_tg(struct throtl_service_queue *sq)
189{
190 if (sq && sq->parent_sq)
191 return container_of(sq, struct throtl_grp, service_queue);
192 else
193 return NULL;
194}
Vivek Goyale43473b2010-09-15 17:06:35 -0400195
Tejun Heofda6f272013-05-14 13:52:36 -0700196/**
197 * sq_to_td - return throtl_data the specified service queue belongs to
198 * @sq: the throtl_service_queue of interest
199 *
Masahiro Yamadab43daed2017-02-27 14:29:09 -0800200 * A service_queue can be embedded in either a throtl_grp or throtl_data.
Tejun Heofda6f272013-05-14 13:52:36 -0700201 * Determine the associated throtl_data accordingly and return it.
202 */
203static struct throtl_data *sq_to_td(struct throtl_service_queue *sq)
204{
205 struct throtl_grp *tg = sq_to_tg(sq);
206
207 if (tg)
208 return tg->td;
209 else
210 return container_of(sq, struct throtl_data, service_queue);
211}
212
Shaohua Li9f626e32017-03-27 10:51:30 -0700213static uint64_t tg_bps_limit(struct throtl_grp *tg, int rw)
214{
215 return tg->bps[rw][tg->td->limit_index];
216}
217
218static unsigned int tg_iops_limit(struct throtl_grp *tg, int rw)
219{
220 return tg->iops[rw][tg->td->limit_index];
221}
222
Tejun Heofda6f272013-05-14 13:52:36 -0700223/**
224 * throtl_log - log debug message via blktrace
225 * @sq: the service_queue being reported
226 * @fmt: printf format string
227 * @args: printf args
228 *
229 * The messages are prefixed with "throtl BLKG_NAME" if @sq belongs to a
230 * throtl_grp; otherwise, just "throtl".
Tejun Heofda6f272013-05-14 13:52:36 -0700231 */
232#define throtl_log(sq, fmt, args...) do { \
233 struct throtl_grp *__tg = sq_to_tg((sq)); \
234 struct throtl_data *__td = sq_to_td((sq)); \
235 \
236 (void)__td; \
Shaohua Li59fa0222016-05-09 17:22:15 -0700237 if (likely(!blk_trace_note_message_enabled(__td->queue))) \
238 break; \
Tejun Heofda6f272013-05-14 13:52:36 -0700239 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 Heo24bdb8e2015-08-18 14:55:22 -0700345 int rw;
Tejun Heo4fb72032015-08-18 14:55:12 -0700346
347 tg = kzalloc_node(sizeof(*tg), gfp, node);
348 if (!tg)
Tejun Heo77ea7332015-08-18 14:55:24 -0700349 return NULL;
Tejun Heo4fb72032015-08-18 14:55:12 -0700350
Tejun Heob2ce2642015-08-18 14:55:13 -0700351 throtl_service_queue_init(&tg->service_queue);
352
353 for (rw = READ; rw <= WRITE; rw++) {
354 throtl_qnode_init(&tg->qnode_on_self[rw], tg);
355 throtl_qnode_init(&tg->qnode_on_parent[rw], tg);
356 }
357
358 RB_CLEAR_NODE(&tg->rb_node);
Shaohua Li9f626e32017-03-27 10:51:30 -0700359 tg->bps[READ][LIMIT_MAX] = U64_MAX;
360 tg->bps[WRITE][LIMIT_MAX] = U64_MAX;
361 tg->iops[READ][LIMIT_MAX] = UINT_MAX;
362 tg->iops[WRITE][LIMIT_MAX] = UINT_MAX;
Shaohua Licd5ab1b2017-03-27 10:51:32 -0700363 tg->bps_conf[READ][LIMIT_MAX] = U64_MAX;
364 tg->bps_conf[WRITE][LIMIT_MAX] = U64_MAX;
365 tg->iops_conf[READ][LIMIT_MAX] = UINT_MAX;
366 tg->iops_conf[WRITE][LIMIT_MAX] = UINT_MAX;
367 /* LIMIT_LOW will have default value 0 */
Tejun Heob2ce2642015-08-18 14:55:13 -0700368
Tejun Heo4fb72032015-08-18 14:55:12 -0700369 return &tg->pd;
Tejun Heo001bea72015-08-18 14:55:11 -0700370}
371
Tejun Heoa9520cd2015-08-18 14:55:14 -0700372static void throtl_pd_init(struct blkg_policy_data *pd)
Vivek Goyala29a1712011-05-19 15:38:19 -0400373{
Tejun Heoa9520cd2015-08-18 14:55:14 -0700374 struct throtl_grp *tg = pd_to_tg(pd);
375 struct blkcg_gq *blkg = tg_to_blkg(tg);
Tejun Heo77216b02013-05-14 13:52:36 -0700376 struct throtl_data *td = blkg->q->td;
Tejun Heob2ce2642015-08-18 14:55:13 -0700377 struct throtl_service_queue *sq = &tg->service_queue;
Tejun Heocd1604f2012-03-05 13:15:06 -0800378
Tejun Heo91381252013-05-14 13:52:38 -0700379 /*
Tejun Heoaa6ec292014-07-09 10:08:08 -0400380 * If on the default hierarchy, we switch to properly hierarchical
Tejun Heo91381252013-05-14 13:52:38 -0700381 * behavior where limits on a given throtl_grp are applied to the
382 * whole subtree rather than just the group itself. e.g. If 16M
383 * read_bps limit is set on the root group, the whole system can't
384 * exceed 16M for the device.
385 *
Tejun Heoaa6ec292014-07-09 10:08:08 -0400386 * If not on the default hierarchy, the broken flat hierarchy
Tejun Heo91381252013-05-14 13:52:38 -0700387 * behavior is retained where all throtl_grps are treated as if
388 * they're all separate root groups right below throtl_data.
389 * Limits of a group don't interact with limits of other groups
390 * regardless of the position of the group in the hierarchy.
391 */
Tejun Heob2ce2642015-08-18 14:55:13 -0700392 sq->parent_sq = &td->service_queue;
Tejun Heo9e10a132015-09-18 11:56:28 -0400393 if (cgroup_subsys_on_dfl(io_cgrp_subsys) && blkg->parent)
Tejun Heob2ce2642015-08-18 14:55:13 -0700394 sq->parent_sq = &blkg_to_tg(blkg->parent)->service_queue;
Tejun Heo77216b02013-05-14 13:52:36 -0700395 tg->td = td;
Tejun Heo8a3d2612012-04-01 14:38:44 -0700396}
397
Tejun Heo693e7512013-05-14 13:52:38 -0700398/*
399 * Set has_rules[] if @tg or any of its parents have limits configured.
400 * This doesn't require walking up to the top of the hierarchy as the
401 * parent's has_rules[] is guaranteed to be correct.
402 */
403static void tg_update_has_rules(struct throtl_grp *tg)
404{
405 struct throtl_grp *parent_tg = sq_to_tg(tg->service_queue.parent_sq);
Shaohua Li9f626e32017-03-27 10:51:30 -0700406 struct throtl_data *td = tg->td;
Tejun Heo693e7512013-05-14 13:52:38 -0700407 int rw;
408
409 for (rw = READ; rw <= WRITE; rw++)
410 tg->has_rules[rw] = (parent_tg && parent_tg->has_rules[rw]) ||
Shaohua Li9f626e32017-03-27 10:51:30 -0700411 (td->limit_valid[td->limit_index] &&
412 (tg_bps_limit(tg, rw) != U64_MAX ||
413 tg_iops_limit(tg, rw) != UINT_MAX));
Tejun Heo693e7512013-05-14 13:52:38 -0700414}
415
Tejun Heoa9520cd2015-08-18 14:55:14 -0700416static void throtl_pd_online(struct blkg_policy_data *pd)
Tejun Heo693e7512013-05-14 13:52:38 -0700417{
418 /*
419 * We don't want new groups to escape the limits of its ancestors.
420 * Update has_rules[] after a new group is brought online.
421 */
Tejun Heoa9520cd2015-08-18 14:55:14 -0700422 tg_update_has_rules(pd_to_tg(pd));
Tejun Heo693e7512013-05-14 13:52:38 -0700423}
424
Shaohua Licd5ab1b2017-03-27 10:51:32 -0700425static void blk_throtl_update_limit_valid(struct throtl_data *td)
426{
427 struct cgroup_subsys_state *pos_css;
428 struct blkcg_gq *blkg;
429 bool low_valid = false;
430
431 rcu_read_lock();
432 blkg_for_each_descendant_post(blkg, pos_css, td->queue->root_blkg) {
433 struct throtl_grp *tg = blkg_to_tg(blkg);
434
435 if (tg->bps[READ][LIMIT_LOW] || tg->bps[WRITE][LIMIT_LOW] ||
436 tg->iops[READ][LIMIT_LOW] || tg->iops[WRITE][LIMIT_LOW])
437 low_valid = true;
438 }
439 rcu_read_unlock();
440
441 td->limit_valid[LIMIT_LOW] = low_valid;
442}
443
444static void throtl_pd_offline(struct blkg_policy_data *pd)
445{
446 struct throtl_grp *tg = pd_to_tg(pd);
447
448 tg->bps[READ][LIMIT_LOW] = 0;
449 tg->bps[WRITE][LIMIT_LOW] = 0;
450 tg->iops[READ][LIMIT_LOW] = 0;
451 tg->iops[WRITE][LIMIT_LOW] = 0;
452
453 blk_throtl_update_limit_valid(tg->td);
454
455 if (tg->td->limit_index == LIMIT_LOW &&
456 !tg->td->limit_valid[LIMIT_LOW])
457 tg->td->limit_index = LIMIT_MAX;
458}
459
Tejun Heo001bea72015-08-18 14:55:11 -0700460static void throtl_pd_free(struct blkg_policy_data *pd)
461{
Tejun Heo4fb72032015-08-18 14:55:12 -0700462 struct throtl_grp *tg = pd_to_tg(pd);
463
Tejun Heob2ce2642015-08-18 14:55:13 -0700464 del_timer_sync(&tg->service_queue.pending_timer);
Tejun Heo4fb72032015-08-18 14:55:12 -0700465 kfree(tg);
Tejun Heo001bea72015-08-18 14:55:11 -0700466}
467
Tejun Heo0049af72013-05-14 13:52:33 -0700468static struct throtl_grp *
469throtl_rb_first(struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -0400470{
471 /* Service tree is empty */
Tejun Heo0049af72013-05-14 13:52:33 -0700472 if (!parent_sq->nr_pending)
Vivek Goyale43473b2010-09-15 17:06:35 -0400473 return NULL;
474
Tejun Heo0049af72013-05-14 13:52:33 -0700475 if (!parent_sq->first_pending)
476 parent_sq->first_pending = rb_first(&parent_sq->pending_tree);
Vivek Goyale43473b2010-09-15 17:06:35 -0400477
Tejun Heo0049af72013-05-14 13:52:33 -0700478 if (parent_sq->first_pending)
479 return rb_entry_tg(parent_sq->first_pending);
Vivek Goyale43473b2010-09-15 17:06:35 -0400480
481 return NULL;
482}
483
484static void rb_erase_init(struct rb_node *n, struct rb_root *root)
485{
486 rb_erase(n, root);
487 RB_CLEAR_NODE(n);
488}
489
Tejun Heo0049af72013-05-14 13:52:33 -0700490static void throtl_rb_erase(struct rb_node *n,
491 struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -0400492{
Tejun Heo0049af72013-05-14 13:52:33 -0700493 if (parent_sq->first_pending == n)
494 parent_sq->first_pending = NULL;
495 rb_erase_init(n, &parent_sq->pending_tree);
496 --parent_sq->nr_pending;
Vivek Goyale43473b2010-09-15 17:06:35 -0400497}
498
Tejun Heo0049af72013-05-14 13:52:33 -0700499static void update_min_dispatch_time(struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -0400500{
501 struct throtl_grp *tg;
502
Tejun Heo0049af72013-05-14 13:52:33 -0700503 tg = throtl_rb_first(parent_sq);
Vivek Goyale43473b2010-09-15 17:06:35 -0400504 if (!tg)
505 return;
506
Tejun Heo0049af72013-05-14 13:52:33 -0700507 parent_sq->first_pending_disptime = tg->disptime;
Vivek Goyale43473b2010-09-15 17:06:35 -0400508}
509
Tejun Heo77216b02013-05-14 13:52:36 -0700510static void tg_service_queue_add(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400511{
Tejun Heo77216b02013-05-14 13:52:36 -0700512 struct throtl_service_queue *parent_sq = tg->service_queue.parent_sq;
Tejun Heo0049af72013-05-14 13:52:33 -0700513 struct rb_node **node = &parent_sq->pending_tree.rb_node;
Vivek Goyale43473b2010-09-15 17:06:35 -0400514 struct rb_node *parent = NULL;
515 struct throtl_grp *__tg;
516 unsigned long key = tg->disptime;
517 int left = 1;
518
519 while (*node != NULL) {
520 parent = *node;
521 __tg = rb_entry_tg(parent);
522
523 if (time_before(key, __tg->disptime))
524 node = &parent->rb_left;
525 else {
526 node = &parent->rb_right;
527 left = 0;
528 }
529 }
530
531 if (left)
Tejun Heo0049af72013-05-14 13:52:33 -0700532 parent_sq->first_pending = &tg->rb_node;
Vivek Goyale43473b2010-09-15 17:06:35 -0400533
534 rb_link_node(&tg->rb_node, parent, node);
Tejun Heo0049af72013-05-14 13:52:33 -0700535 rb_insert_color(&tg->rb_node, &parent_sq->pending_tree);
Vivek Goyale43473b2010-09-15 17:06:35 -0400536}
537
Tejun Heo77216b02013-05-14 13:52:36 -0700538static void __throtl_enqueue_tg(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400539{
Tejun Heo77216b02013-05-14 13:52:36 -0700540 tg_service_queue_add(tg);
Tejun Heo5b2c16a2013-05-14 13:52:32 -0700541 tg->flags |= THROTL_TG_PENDING;
Tejun Heo77216b02013-05-14 13:52:36 -0700542 tg->service_queue.parent_sq->nr_pending++;
Vivek Goyale43473b2010-09-15 17:06:35 -0400543}
544
Tejun Heo77216b02013-05-14 13:52:36 -0700545static void throtl_enqueue_tg(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400546{
Tejun Heo5b2c16a2013-05-14 13:52:32 -0700547 if (!(tg->flags & THROTL_TG_PENDING))
Tejun Heo77216b02013-05-14 13:52:36 -0700548 __throtl_enqueue_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -0400549}
550
Tejun Heo77216b02013-05-14 13:52:36 -0700551static void __throtl_dequeue_tg(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400552{
Tejun Heo77216b02013-05-14 13:52:36 -0700553 throtl_rb_erase(&tg->rb_node, tg->service_queue.parent_sq);
Tejun Heo5b2c16a2013-05-14 13:52:32 -0700554 tg->flags &= ~THROTL_TG_PENDING;
Vivek Goyale43473b2010-09-15 17:06:35 -0400555}
556
Tejun Heo77216b02013-05-14 13:52:36 -0700557static void throtl_dequeue_tg(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400558{
Tejun Heo5b2c16a2013-05-14 13:52:32 -0700559 if (tg->flags & THROTL_TG_PENDING)
Tejun Heo77216b02013-05-14 13:52:36 -0700560 __throtl_dequeue_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -0400561}
562
Tejun Heoa9131a22013-05-14 13:52:31 -0700563/* Call with queue lock held */
Tejun Heo69df0ab2013-05-14 13:52:36 -0700564static void throtl_schedule_pending_timer(struct throtl_service_queue *sq,
565 unsigned long expires)
Tejun Heoa9131a22013-05-14 13:52:31 -0700566{
Tejun Heo69df0ab2013-05-14 13:52:36 -0700567 mod_timer(&sq->pending_timer, expires);
568 throtl_log(sq, "schedule timer. delay=%lu jiffies=%lu",
569 expires - jiffies, jiffies);
Tejun Heoa9131a22013-05-14 13:52:31 -0700570}
571
Tejun Heo7f52f982013-05-14 13:52:37 -0700572/**
573 * throtl_schedule_next_dispatch - schedule the next dispatch cycle
574 * @sq: the service_queue to schedule dispatch for
575 * @force: force scheduling
576 *
577 * Arm @sq->pending_timer so that the next dispatch cycle starts on the
578 * dispatch time of the first pending child. Returns %true if either timer
579 * is armed or there's no pending child left. %false if the current
580 * dispatch window is still open and the caller should continue
581 * dispatching.
582 *
583 * If @force is %true, the dispatch timer is always scheduled and this
584 * function is guaranteed to return %true. This is to be used when the
585 * caller can't dispatch itself and needs to invoke pending_timer
586 * unconditionally. Note that forced scheduling is likely to induce short
587 * delay before dispatch starts even if @sq->first_pending_disptime is not
588 * in the future and thus shouldn't be used in hot paths.
589 */
590static bool throtl_schedule_next_dispatch(struct throtl_service_queue *sq,
591 bool force)
Vivek Goyale43473b2010-09-15 17:06:35 -0400592{
Tejun Heo6a525602013-05-14 13:52:32 -0700593 /* any pending children left? */
Tejun Heoc9e03322013-05-14 13:52:32 -0700594 if (!sq->nr_pending)
Tejun Heo7f52f982013-05-14 13:52:37 -0700595 return true;
Vivek Goyale43473b2010-09-15 17:06:35 -0400596
Tejun Heoc9e03322013-05-14 13:52:32 -0700597 update_min_dispatch_time(sq);
Vivek Goyale43473b2010-09-15 17:06:35 -0400598
Tejun Heo69df0ab2013-05-14 13:52:36 -0700599 /* is the next dispatch time in the future? */
Tejun Heo7f52f982013-05-14 13:52:37 -0700600 if (force || time_after(sq->first_pending_disptime, jiffies)) {
Tejun Heo69df0ab2013-05-14 13:52:36 -0700601 throtl_schedule_pending_timer(sq, sq->first_pending_disptime);
Tejun Heo7f52f982013-05-14 13:52:37 -0700602 return true;
Tejun Heo69df0ab2013-05-14 13:52:36 -0700603 }
604
Tejun Heo7f52f982013-05-14 13:52:37 -0700605 /* tell the caller to continue dispatching */
606 return false;
Vivek Goyale43473b2010-09-15 17:06:35 -0400607}
608
Vivek Goyal32ee5bc2013-05-14 13:52:38 -0700609static inline void throtl_start_new_slice_with_credit(struct throtl_grp *tg,
610 bool rw, unsigned long start)
611{
612 tg->bytes_disp[rw] = 0;
613 tg->io_disp[rw] = 0;
614
615 /*
616 * Previous slice has expired. We must have trimmed it after last
617 * bio dispatch. That means since start of last slice, we never used
618 * that bandwidth. Do try to make use of that bandwidth while giving
619 * credit.
620 */
621 if (time_after_eq(start, tg->slice_start[rw]))
622 tg->slice_start[rw] = start;
623
624 tg->slice_end[rw] = jiffies + throtl_slice;
625 throtl_log(&tg->service_queue,
626 "[%c] new slice with credit start=%lu end=%lu jiffies=%lu",
627 rw == READ ? 'R' : 'W', tg->slice_start[rw],
628 tg->slice_end[rw], jiffies);
629}
630
Tejun Heo0f3457f2013-05-14 13:52:32 -0700631static inline void throtl_start_new_slice(struct throtl_grp *tg, bool rw)
Vivek Goyale43473b2010-09-15 17:06:35 -0400632{
633 tg->bytes_disp[rw] = 0;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400634 tg->io_disp[rw] = 0;
Vivek Goyale43473b2010-09-15 17:06:35 -0400635 tg->slice_start[rw] = jiffies;
636 tg->slice_end[rw] = jiffies + throtl_slice;
Tejun Heofda6f272013-05-14 13:52:36 -0700637 throtl_log(&tg->service_queue,
638 "[%c] new slice start=%lu end=%lu jiffies=%lu",
639 rw == READ ? 'R' : 'W', tg->slice_start[rw],
640 tg->slice_end[rw], jiffies);
Vivek Goyale43473b2010-09-15 17:06:35 -0400641}
642
Tejun Heo0f3457f2013-05-14 13:52:32 -0700643static inline void throtl_set_slice_end(struct throtl_grp *tg, bool rw,
644 unsigned long jiffy_end)
Vivek Goyald1ae8ff2010-12-01 19:34:46 +0100645{
646 tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
647}
648
Tejun Heo0f3457f2013-05-14 13:52:32 -0700649static inline void throtl_extend_slice(struct throtl_grp *tg, bool rw,
650 unsigned long jiffy_end)
Vivek Goyale43473b2010-09-15 17:06:35 -0400651{
652 tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
Tejun Heofda6f272013-05-14 13:52:36 -0700653 throtl_log(&tg->service_queue,
654 "[%c] extend slice start=%lu end=%lu jiffies=%lu",
655 rw == READ ? 'R' : 'W', tg->slice_start[rw],
656 tg->slice_end[rw], jiffies);
Vivek Goyale43473b2010-09-15 17:06:35 -0400657}
658
659/* Determine if previously allocated or extended slice is complete or not */
Tejun Heo0f3457f2013-05-14 13:52:32 -0700660static bool throtl_slice_used(struct throtl_grp *tg, bool rw)
Vivek Goyale43473b2010-09-15 17:06:35 -0400661{
662 if (time_in_range(jiffies, tg->slice_start[rw], tg->slice_end[rw]))
Fabian Frederick5cf8c222014-05-02 18:28:17 +0200663 return false;
Vivek Goyale43473b2010-09-15 17:06:35 -0400664
665 return 1;
666}
667
668/* Trim the used slices and adjust slice start accordingly */
Tejun Heo0f3457f2013-05-14 13:52:32 -0700669static inline void throtl_trim_slice(struct throtl_grp *tg, bool rw)
Vivek Goyale43473b2010-09-15 17:06:35 -0400670{
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200671 unsigned long nr_slices, time_elapsed, io_trim;
672 u64 bytes_trim, tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400673
674 BUG_ON(time_before(tg->slice_end[rw], tg->slice_start[rw]));
675
676 /*
677 * If bps are unlimited (-1), then time slice don't get
678 * renewed. Don't try to trim the slice if slice is used. A new
679 * slice will start when appropriate.
680 */
Tejun Heo0f3457f2013-05-14 13:52:32 -0700681 if (throtl_slice_used(tg, rw))
Vivek Goyale43473b2010-09-15 17:06:35 -0400682 return;
683
Vivek Goyald1ae8ff2010-12-01 19:34:46 +0100684 /*
685 * A bio has been dispatched. Also adjust slice_end. It might happen
686 * that initially cgroup limit was very low resulting in high
687 * slice_end, but later limit was bumped up and bio was dispached
688 * sooner, then we need to reduce slice_end. A high bogus slice_end
689 * is bad because it does not allow new slice to start.
690 */
691
Tejun Heo0f3457f2013-05-14 13:52:32 -0700692 throtl_set_slice_end(tg, rw, jiffies + throtl_slice);
Vivek Goyald1ae8ff2010-12-01 19:34:46 +0100693
Vivek Goyale43473b2010-09-15 17:06:35 -0400694 time_elapsed = jiffies - tg->slice_start[rw];
695
696 nr_slices = time_elapsed / throtl_slice;
697
698 if (!nr_slices)
699 return;
Shaohua Li9f626e32017-03-27 10:51:30 -0700700 tmp = tg_bps_limit(tg, rw) * throtl_slice * nr_slices;
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200701 do_div(tmp, HZ);
702 bytes_trim = tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400703
Shaohua Li9f626e32017-03-27 10:51:30 -0700704 io_trim = (tg_iops_limit(tg, rw) * throtl_slice * nr_slices) / HZ;
Vivek Goyale43473b2010-09-15 17:06:35 -0400705
Vivek Goyal8e89d132010-09-15 17:06:37 -0400706 if (!bytes_trim && !io_trim)
Vivek Goyale43473b2010-09-15 17:06:35 -0400707 return;
708
709 if (tg->bytes_disp[rw] >= bytes_trim)
710 tg->bytes_disp[rw] -= bytes_trim;
711 else
712 tg->bytes_disp[rw] = 0;
713
Vivek Goyal8e89d132010-09-15 17:06:37 -0400714 if (tg->io_disp[rw] >= io_trim)
715 tg->io_disp[rw] -= io_trim;
716 else
717 tg->io_disp[rw] = 0;
718
Vivek Goyale43473b2010-09-15 17:06:35 -0400719 tg->slice_start[rw] += nr_slices * throtl_slice;
720
Tejun Heofda6f272013-05-14 13:52:36 -0700721 throtl_log(&tg->service_queue,
722 "[%c] trim slice nr=%lu bytes=%llu io=%lu start=%lu end=%lu jiffies=%lu",
723 rw == READ ? 'R' : 'W', nr_slices, bytes_trim, io_trim,
724 tg->slice_start[rw], tg->slice_end[rw], jiffies);
Vivek Goyale43473b2010-09-15 17:06:35 -0400725}
726
Tejun Heo0f3457f2013-05-14 13:52:32 -0700727static bool tg_with_in_iops_limit(struct throtl_grp *tg, struct bio *bio,
728 unsigned long *wait)
Vivek Goyale43473b2010-09-15 17:06:35 -0400729{
730 bool rw = bio_data_dir(bio);
Vivek Goyal8e89d132010-09-15 17:06:37 -0400731 unsigned int io_allowed;
Vivek Goyale43473b2010-09-15 17:06:35 -0400732 unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
Vivek Goyalc49c06e2010-10-01 21:16:42 +0200733 u64 tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400734
Vivek Goyal8e89d132010-09-15 17:06:37 -0400735 jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
Vivek Goyale43473b2010-09-15 17:06:35 -0400736
Vivek Goyal8e89d132010-09-15 17:06:37 -0400737 /* Slice has just started. Consider one slice interval */
738 if (!jiffy_elapsed)
739 jiffy_elapsed_rnd = throtl_slice;
740
741 jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
742
Vivek Goyalc49c06e2010-10-01 21:16:42 +0200743 /*
744 * jiffy_elapsed_rnd should not be a big value as minimum iops can be
745 * 1 then at max jiffy elapsed should be equivalent of 1 second as we
746 * will allow dispatch after 1 second and after that slice should
747 * have been trimmed.
748 */
749
Shaohua Li9f626e32017-03-27 10:51:30 -0700750 tmp = (u64)tg_iops_limit(tg, rw) * jiffy_elapsed_rnd;
Vivek Goyalc49c06e2010-10-01 21:16:42 +0200751 do_div(tmp, HZ);
752
753 if (tmp > UINT_MAX)
754 io_allowed = UINT_MAX;
755 else
756 io_allowed = tmp;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400757
758 if (tg->io_disp[rw] + 1 <= io_allowed) {
Vivek Goyale43473b2010-09-15 17:06:35 -0400759 if (wait)
760 *wait = 0;
Fabian Frederick5cf8c222014-05-02 18:28:17 +0200761 return true;
Vivek Goyale43473b2010-09-15 17:06:35 -0400762 }
763
Vivek Goyal8e89d132010-09-15 17:06:37 -0400764 /* Calc approx time to dispatch */
Shaohua Li9f626e32017-03-27 10:51:30 -0700765 jiffy_wait = ((tg->io_disp[rw] + 1) * HZ) / tg_iops_limit(tg, rw) + 1;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400766
767 if (jiffy_wait > jiffy_elapsed)
768 jiffy_wait = jiffy_wait - jiffy_elapsed;
769 else
770 jiffy_wait = 1;
771
772 if (wait)
773 *wait = jiffy_wait;
774 return 0;
775}
776
Tejun Heo0f3457f2013-05-14 13:52:32 -0700777static bool tg_with_in_bps_limit(struct throtl_grp *tg, struct bio *bio,
778 unsigned long *wait)
Vivek Goyal8e89d132010-09-15 17:06:37 -0400779{
780 bool rw = bio_data_dir(bio);
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200781 u64 bytes_allowed, extra_bytes, tmp;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400782 unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
Vivek Goyale43473b2010-09-15 17:06:35 -0400783
784 jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
785
786 /* Slice has just started. Consider one slice interval */
787 if (!jiffy_elapsed)
788 jiffy_elapsed_rnd = throtl_slice;
789
790 jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
791
Shaohua Li9f626e32017-03-27 10:51:30 -0700792 tmp = tg_bps_limit(tg, rw) * jiffy_elapsed_rnd;
Vivek Goyal5e901a22010-10-01 21:16:38 +0200793 do_div(tmp, HZ);
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200794 bytes_allowed = tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400795
Kent Overstreet4f024f32013-10-11 15:44:27 -0700796 if (tg->bytes_disp[rw] + bio->bi_iter.bi_size <= bytes_allowed) {
Vivek Goyale43473b2010-09-15 17:06:35 -0400797 if (wait)
798 *wait = 0;
Fabian Frederick5cf8c222014-05-02 18:28:17 +0200799 return true;
Vivek Goyale43473b2010-09-15 17:06:35 -0400800 }
801
802 /* Calc approx time to dispatch */
Kent Overstreet4f024f32013-10-11 15:44:27 -0700803 extra_bytes = tg->bytes_disp[rw] + bio->bi_iter.bi_size - bytes_allowed;
Shaohua Li9f626e32017-03-27 10:51:30 -0700804 jiffy_wait = div64_u64(extra_bytes * HZ, tg_bps_limit(tg, rw));
Vivek Goyale43473b2010-09-15 17:06:35 -0400805
806 if (!jiffy_wait)
807 jiffy_wait = 1;
808
809 /*
810 * This wait time is without taking into consideration the rounding
811 * up we did. Add that time also.
812 */
813 jiffy_wait = jiffy_wait + (jiffy_elapsed_rnd - jiffy_elapsed);
Vivek Goyale43473b2010-09-15 17:06:35 -0400814 if (wait)
815 *wait = jiffy_wait;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400816 return 0;
817}
Vivek Goyale43473b2010-09-15 17:06:35 -0400818
Vivek Goyal8e89d132010-09-15 17:06:37 -0400819/*
820 * Returns whether one can dispatch a bio or not. Also returns approx number
821 * of jiffies to wait before this bio is with-in IO rate and can be dispatched
822 */
Tejun Heo0f3457f2013-05-14 13:52:32 -0700823static bool tg_may_dispatch(struct throtl_grp *tg, struct bio *bio,
824 unsigned long *wait)
Vivek Goyal8e89d132010-09-15 17:06:37 -0400825{
826 bool rw = bio_data_dir(bio);
827 unsigned long bps_wait = 0, iops_wait = 0, max_wait = 0;
828
829 /*
830 * Currently whole state machine of group depends on first bio
831 * queued in the group bio list. So one should not be calling
832 * this function with a different bio if there are other bios
833 * queued.
834 */
Tejun Heo73f0d492013-05-14 13:52:35 -0700835 BUG_ON(tg->service_queue.nr_queued[rw] &&
Tejun Heoc5cc2072013-05-14 13:52:38 -0700836 bio != throtl_peek_queued(&tg->service_queue.queued[rw]));
Vivek Goyal8e89d132010-09-15 17:06:37 -0400837
838 /* If tg->bps = -1, then BW is unlimited */
Shaohua Li9f626e32017-03-27 10:51:30 -0700839 if (tg_bps_limit(tg, rw) == U64_MAX &&
840 tg_iops_limit(tg, rw) == UINT_MAX) {
Vivek Goyal8e89d132010-09-15 17:06:37 -0400841 if (wait)
842 *wait = 0;
Fabian Frederick5cf8c222014-05-02 18:28:17 +0200843 return true;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400844 }
845
846 /*
847 * If previous slice expired, start a new one otherwise renew/extend
848 * existing slice to make sure it is at least throtl_slice interval
Vivek Goyal164c80e2016-09-19 15:12:41 -0600849 * long since now. New slice is started only for empty throttle group.
850 * If there is queued bio, that means there should be an active
851 * slice and it should be extended instead.
Vivek Goyal8e89d132010-09-15 17:06:37 -0400852 */
Vivek Goyal164c80e2016-09-19 15:12:41 -0600853 if (throtl_slice_used(tg, rw) && !(tg->service_queue.nr_queued[rw]))
Tejun Heo0f3457f2013-05-14 13:52:32 -0700854 throtl_start_new_slice(tg, rw);
Vivek Goyal8e89d132010-09-15 17:06:37 -0400855 else {
856 if (time_before(tg->slice_end[rw], jiffies + throtl_slice))
Tejun Heo0f3457f2013-05-14 13:52:32 -0700857 throtl_extend_slice(tg, rw, jiffies + throtl_slice);
Vivek Goyal8e89d132010-09-15 17:06:37 -0400858 }
859
Tejun Heo0f3457f2013-05-14 13:52:32 -0700860 if (tg_with_in_bps_limit(tg, bio, &bps_wait) &&
861 tg_with_in_iops_limit(tg, bio, &iops_wait)) {
Vivek Goyal8e89d132010-09-15 17:06:37 -0400862 if (wait)
863 *wait = 0;
864 return 1;
865 }
866
867 max_wait = max(bps_wait, iops_wait);
868
869 if (wait)
870 *wait = max_wait;
871
872 if (time_before(tg->slice_end[rw], jiffies + max_wait))
Tejun Heo0f3457f2013-05-14 13:52:32 -0700873 throtl_extend_slice(tg, rw, jiffies + max_wait);
Vivek Goyale43473b2010-09-15 17:06:35 -0400874
875 return 0;
876}
877
878static void throtl_charge_bio(struct throtl_grp *tg, struct bio *bio)
879{
880 bool rw = bio_data_dir(bio);
Vivek Goyale43473b2010-09-15 17:06:35 -0400881
882 /* Charge the bio to the group */
Kent Overstreet4f024f32013-10-11 15:44:27 -0700883 tg->bytes_disp[rw] += bio->bi_iter.bi_size;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400884 tg->io_disp[rw]++;
Vivek Goyale43473b2010-09-15 17:06:35 -0400885
Tejun Heo2a0f61e2013-05-14 13:52:36 -0700886 /*
Christoph Hellwig8d2bbd42016-10-20 15:12:12 +0200887 * BIO_THROTTLED is used to prevent the same bio to be throttled
Tejun Heo2a0f61e2013-05-14 13:52:36 -0700888 * more than once as a throttled bio will go through blk-throtl the
889 * second time when it eventually gets issued. Set it when a bio
890 * is being charged to a tg.
Tejun Heo2a0f61e2013-05-14 13:52:36 -0700891 */
Christoph Hellwig8d2bbd42016-10-20 15:12:12 +0200892 if (!bio_flagged(bio, BIO_THROTTLED))
893 bio_set_flag(bio, BIO_THROTTLED);
Vivek Goyale43473b2010-09-15 17:06:35 -0400894}
895
Tejun Heoc5cc2072013-05-14 13:52:38 -0700896/**
897 * throtl_add_bio_tg - add a bio to the specified throtl_grp
898 * @bio: bio to add
899 * @qn: qnode to use
900 * @tg: the target throtl_grp
901 *
902 * Add @bio to @tg's service_queue using @qn. If @qn is not specified,
903 * tg->qnode_on_self[] is used.
904 */
905static void throtl_add_bio_tg(struct bio *bio, struct throtl_qnode *qn,
906 struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400907{
Tejun Heo73f0d492013-05-14 13:52:35 -0700908 struct throtl_service_queue *sq = &tg->service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -0400909 bool rw = bio_data_dir(bio);
910
Tejun Heoc5cc2072013-05-14 13:52:38 -0700911 if (!qn)
912 qn = &tg->qnode_on_self[rw];
913
Tejun Heo0e9f4162013-05-14 13:52:35 -0700914 /*
915 * If @tg doesn't currently have any bios queued in the same
916 * direction, queueing @bio can change when @tg should be
917 * dispatched. Mark that @tg was empty. This is automatically
918 * cleaered on the next tg_update_disptime().
919 */
920 if (!sq->nr_queued[rw])
921 tg->flags |= THROTL_TG_WAS_EMPTY;
922
Tejun Heoc5cc2072013-05-14 13:52:38 -0700923 throtl_qnode_add_bio(bio, qn, &sq->queued[rw]);
924
Tejun Heo73f0d492013-05-14 13:52:35 -0700925 sq->nr_queued[rw]++;
Tejun Heo77216b02013-05-14 13:52:36 -0700926 throtl_enqueue_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -0400927}
928
Tejun Heo77216b02013-05-14 13:52:36 -0700929static void tg_update_disptime(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400930{
Tejun Heo73f0d492013-05-14 13:52:35 -0700931 struct throtl_service_queue *sq = &tg->service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -0400932 unsigned long read_wait = -1, write_wait = -1, min_wait = -1, disptime;
933 struct bio *bio;
934
Markus Elfringd609af32017-01-21 22:15:33 +0100935 bio = throtl_peek_queued(&sq->queued[READ]);
936 if (bio)
Tejun Heo0f3457f2013-05-14 13:52:32 -0700937 tg_may_dispatch(tg, bio, &read_wait);
Vivek Goyale43473b2010-09-15 17:06:35 -0400938
Markus Elfringd609af32017-01-21 22:15:33 +0100939 bio = throtl_peek_queued(&sq->queued[WRITE]);
940 if (bio)
Tejun Heo0f3457f2013-05-14 13:52:32 -0700941 tg_may_dispatch(tg, bio, &write_wait);
Vivek Goyale43473b2010-09-15 17:06:35 -0400942
943 min_wait = min(read_wait, write_wait);
944 disptime = jiffies + min_wait;
945
Vivek Goyale43473b2010-09-15 17:06:35 -0400946 /* Update dispatch time */
Tejun Heo77216b02013-05-14 13:52:36 -0700947 throtl_dequeue_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -0400948 tg->disptime = disptime;
Tejun Heo77216b02013-05-14 13:52:36 -0700949 throtl_enqueue_tg(tg);
Tejun Heo0e9f4162013-05-14 13:52:35 -0700950
951 /* see throtl_add_bio_tg() */
952 tg->flags &= ~THROTL_TG_WAS_EMPTY;
Vivek Goyale43473b2010-09-15 17:06:35 -0400953}
954
Vivek Goyal32ee5bc2013-05-14 13:52:38 -0700955static void start_parent_slice_with_credit(struct throtl_grp *child_tg,
956 struct throtl_grp *parent_tg, bool rw)
957{
958 if (throtl_slice_used(parent_tg, rw)) {
959 throtl_start_new_slice_with_credit(parent_tg, rw,
960 child_tg->slice_start[rw]);
961 }
962
963}
964
Tejun Heo77216b02013-05-14 13:52:36 -0700965static void tg_dispatch_one_bio(struct throtl_grp *tg, bool rw)
Vivek Goyale43473b2010-09-15 17:06:35 -0400966{
Tejun Heo73f0d492013-05-14 13:52:35 -0700967 struct throtl_service_queue *sq = &tg->service_queue;
Tejun Heo6bc9c2b2013-05-14 13:52:38 -0700968 struct throtl_service_queue *parent_sq = sq->parent_sq;
969 struct throtl_grp *parent_tg = sq_to_tg(parent_sq);
Tejun Heoc5cc2072013-05-14 13:52:38 -0700970 struct throtl_grp *tg_to_put = NULL;
Vivek Goyale43473b2010-09-15 17:06:35 -0400971 struct bio *bio;
972
Tejun Heoc5cc2072013-05-14 13:52:38 -0700973 /*
974 * @bio is being transferred from @tg to @parent_sq. Popping a bio
975 * from @tg may put its reference and @parent_sq might end up
976 * getting released prematurely. Remember the tg to put and put it
977 * after @bio is transferred to @parent_sq.
978 */
979 bio = throtl_pop_queued(&sq->queued[rw], &tg_to_put);
Tejun Heo73f0d492013-05-14 13:52:35 -0700980 sq->nr_queued[rw]--;
Vivek Goyale43473b2010-09-15 17:06:35 -0400981
982 throtl_charge_bio(tg, bio);
Tejun Heo6bc9c2b2013-05-14 13:52:38 -0700983
984 /*
985 * If our parent is another tg, we just need to transfer @bio to
986 * the parent using throtl_add_bio_tg(). If our parent is
987 * @td->service_queue, @bio is ready to be issued. Put it on its
988 * bio_lists[] and decrease total number queued. The caller is
989 * responsible for issuing these bios.
990 */
991 if (parent_tg) {
Tejun Heoc5cc2072013-05-14 13:52:38 -0700992 throtl_add_bio_tg(bio, &tg->qnode_on_parent[rw], parent_tg);
Vivek Goyal32ee5bc2013-05-14 13:52:38 -0700993 start_parent_slice_with_credit(tg, parent_tg, rw);
Tejun Heo6bc9c2b2013-05-14 13:52:38 -0700994 } else {
Tejun Heoc5cc2072013-05-14 13:52:38 -0700995 throtl_qnode_add_bio(bio, &tg->qnode_on_parent[rw],
996 &parent_sq->queued[rw]);
Tejun Heo6bc9c2b2013-05-14 13:52:38 -0700997 BUG_ON(tg->td->nr_queued[rw] <= 0);
998 tg->td->nr_queued[rw]--;
999 }
Vivek Goyale43473b2010-09-15 17:06:35 -04001000
Tejun Heo0f3457f2013-05-14 13:52:32 -07001001 throtl_trim_slice(tg, rw);
Tejun Heo6bc9c2b2013-05-14 13:52:38 -07001002
Tejun Heoc5cc2072013-05-14 13:52:38 -07001003 if (tg_to_put)
1004 blkg_put(tg_to_blkg(tg_to_put));
Vivek Goyale43473b2010-09-15 17:06:35 -04001005}
1006
Tejun Heo77216b02013-05-14 13:52:36 -07001007static int throtl_dispatch_tg(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -04001008{
Tejun Heo73f0d492013-05-14 13:52:35 -07001009 struct throtl_service_queue *sq = &tg->service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -04001010 unsigned int nr_reads = 0, nr_writes = 0;
1011 unsigned int max_nr_reads = throtl_grp_quantum*3/4;
Vivek Goyalc2f68052010-11-15 19:32:42 +01001012 unsigned int max_nr_writes = throtl_grp_quantum - max_nr_reads;
Vivek Goyale43473b2010-09-15 17:06:35 -04001013 struct bio *bio;
1014
1015 /* Try to dispatch 75% READS and 25% WRITES */
1016
Tejun Heoc5cc2072013-05-14 13:52:38 -07001017 while ((bio = throtl_peek_queued(&sq->queued[READ])) &&
Tejun Heo0f3457f2013-05-14 13:52:32 -07001018 tg_may_dispatch(tg, bio, NULL)) {
Vivek Goyale43473b2010-09-15 17:06:35 -04001019
Tejun Heo77216b02013-05-14 13:52:36 -07001020 tg_dispatch_one_bio(tg, bio_data_dir(bio));
Vivek Goyale43473b2010-09-15 17:06:35 -04001021 nr_reads++;
1022
1023 if (nr_reads >= max_nr_reads)
1024 break;
1025 }
1026
Tejun Heoc5cc2072013-05-14 13:52:38 -07001027 while ((bio = throtl_peek_queued(&sq->queued[WRITE])) &&
Tejun Heo0f3457f2013-05-14 13:52:32 -07001028 tg_may_dispatch(tg, bio, NULL)) {
Vivek Goyale43473b2010-09-15 17:06:35 -04001029
Tejun Heo77216b02013-05-14 13:52:36 -07001030 tg_dispatch_one_bio(tg, bio_data_dir(bio));
Vivek Goyale43473b2010-09-15 17:06:35 -04001031 nr_writes++;
1032
1033 if (nr_writes >= max_nr_writes)
1034 break;
1035 }
1036
1037 return nr_reads + nr_writes;
1038}
1039
Tejun Heo651930b2013-05-14 13:52:35 -07001040static int throtl_select_dispatch(struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -04001041{
1042 unsigned int nr_disp = 0;
Vivek Goyale43473b2010-09-15 17:06:35 -04001043
1044 while (1) {
Tejun Heo73f0d492013-05-14 13:52:35 -07001045 struct throtl_grp *tg = throtl_rb_first(parent_sq);
1046 struct throtl_service_queue *sq = &tg->service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -04001047
1048 if (!tg)
1049 break;
1050
1051 if (time_before(jiffies, tg->disptime))
1052 break;
1053
Tejun Heo77216b02013-05-14 13:52:36 -07001054 throtl_dequeue_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -04001055
Tejun Heo77216b02013-05-14 13:52:36 -07001056 nr_disp += throtl_dispatch_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -04001057
Tejun Heo73f0d492013-05-14 13:52:35 -07001058 if (sq->nr_queued[0] || sq->nr_queued[1])
Tejun Heo77216b02013-05-14 13:52:36 -07001059 tg_update_disptime(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -04001060
1061 if (nr_disp >= throtl_quantum)
1062 break;
1063 }
1064
1065 return nr_disp;
1066}
1067
Tejun Heo6e1a5702013-05-14 13:52:37 -07001068/**
1069 * throtl_pending_timer_fn - timer function for service_queue->pending_timer
1070 * @arg: the throtl_service_queue being serviced
1071 *
1072 * This timer is armed when a child throtl_grp with active bio's become
1073 * pending and queued on the service_queue's pending_tree and expires when
1074 * the first child throtl_grp should be dispatched. This function
Tejun Heo2e48a532013-05-14 13:52:38 -07001075 * dispatches bio's from the children throtl_grps to the parent
1076 * service_queue.
1077 *
1078 * If the parent's parent is another throtl_grp, dispatching is propagated
1079 * by either arming its pending_timer or repeating dispatch directly. If
1080 * the top-level service_tree is reached, throtl_data->dispatch_work is
1081 * kicked so that the ready bio's are issued.
Tejun Heo6e1a5702013-05-14 13:52:37 -07001082 */
Tejun Heo69df0ab2013-05-14 13:52:36 -07001083static void throtl_pending_timer_fn(unsigned long arg)
1084{
1085 struct throtl_service_queue *sq = (void *)arg;
Tejun Heo2e48a532013-05-14 13:52:38 -07001086 struct throtl_grp *tg = sq_to_tg(sq);
Tejun Heo69df0ab2013-05-14 13:52:36 -07001087 struct throtl_data *td = sq_to_td(sq);
Tejun Heocb761992013-05-14 13:52:31 -07001088 struct request_queue *q = td->queue;
Tejun Heo2e48a532013-05-14 13:52:38 -07001089 struct throtl_service_queue *parent_sq;
1090 bool dispatched;
Tejun Heo6e1a5702013-05-14 13:52:37 -07001091 int ret;
Vivek Goyale43473b2010-09-15 17:06:35 -04001092
1093 spin_lock_irq(q->queue_lock);
Tejun Heo2e48a532013-05-14 13:52:38 -07001094again:
1095 parent_sq = sq->parent_sq;
1096 dispatched = false;
Vivek Goyale43473b2010-09-15 17:06:35 -04001097
Tejun Heo7f52f982013-05-14 13:52:37 -07001098 while (true) {
1099 throtl_log(sq, "dispatch nr_queued=%u read=%u write=%u",
Tejun Heo2e48a532013-05-14 13:52:38 -07001100 sq->nr_queued[READ] + sq->nr_queued[WRITE],
1101 sq->nr_queued[READ], sq->nr_queued[WRITE]);
Vivek Goyale43473b2010-09-15 17:06:35 -04001102
Tejun Heo7f52f982013-05-14 13:52:37 -07001103 ret = throtl_select_dispatch(sq);
1104 if (ret) {
Tejun Heo7f52f982013-05-14 13:52:37 -07001105 throtl_log(sq, "bios disp=%u", ret);
1106 dispatched = true;
Tejun Heo651930b2013-05-14 13:52:35 -07001107 }
Vivek Goyale43473b2010-09-15 17:06:35 -04001108
Tejun Heo7f52f982013-05-14 13:52:37 -07001109 if (throtl_schedule_next_dispatch(sq, false))
1110 break;
1111
1112 /* this dispatch windows is still open, relax and repeat */
1113 spin_unlock_irq(q->queue_lock);
1114 cpu_relax();
1115 spin_lock_irq(q->queue_lock);
1116 }
Tejun Heo6a525602013-05-14 13:52:32 -07001117
Tejun Heo2e48a532013-05-14 13:52:38 -07001118 if (!dispatched)
1119 goto out_unlock;
Tejun Heo6e1a5702013-05-14 13:52:37 -07001120
Tejun Heo2e48a532013-05-14 13:52:38 -07001121 if (parent_sq) {
1122 /* @parent_sq is another throl_grp, propagate dispatch */
1123 if (tg->flags & THROTL_TG_WAS_EMPTY) {
1124 tg_update_disptime(tg);
1125 if (!throtl_schedule_next_dispatch(parent_sq, false)) {
1126 /* window is already open, repeat dispatching */
1127 sq = parent_sq;
1128 tg = sq_to_tg(sq);
1129 goto again;
1130 }
1131 }
1132 } else {
1133 /* reached the top-level, queue issueing */
1134 queue_work(kthrotld_workqueue, &td->dispatch_work);
1135 }
1136out_unlock:
Tejun Heo6e1a5702013-05-14 13:52:37 -07001137 spin_unlock_irq(q->queue_lock);
1138}
1139
1140/**
1141 * blk_throtl_dispatch_work_fn - work function for throtl_data->dispatch_work
1142 * @work: work item being executed
1143 *
1144 * This function is queued for execution when bio's reach the bio_lists[]
1145 * of throtl_data->service_queue. Those bio's are ready and issued by this
1146 * function.
1147 */
Fabian Frederick8876e142014-04-17 21:41:16 +02001148static void blk_throtl_dispatch_work_fn(struct work_struct *work)
Tejun Heo6e1a5702013-05-14 13:52:37 -07001149{
1150 struct throtl_data *td = container_of(work, struct throtl_data,
1151 dispatch_work);
1152 struct throtl_service_queue *td_sq = &td->service_queue;
1153 struct request_queue *q = td->queue;
1154 struct bio_list bio_list_on_stack;
1155 struct bio *bio;
1156 struct blk_plug plug;
1157 int rw;
1158
1159 bio_list_init(&bio_list_on_stack);
1160
1161 spin_lock_irq(q->queue_lock);
Tejun Heoc5cc2072013-05-14 13:52:38 -07001162 for (rw = READ; rw <= WRITE; rw++)
1163 while ((bio = throtl_pop_queued(&td_sq->queued[rw], NULL)))
1164 bio_list_add(&bio_list_on_stack, bio);
Vivek Goyale43473b2010-09-15 17:06:35 -04001165 spin_unlock_irq(q->queue_lock);
1166
Tejun Heo6e1a5702013-05-14 13:52:37 -07001167 if (!bio_list_empty(&bio_list_on_stack)) {
Vivek Goyal69d60eb2011-03-09 08:27:37 +01001168 blk_start_plug(&plug);
Vivek Goyale43473b2010-09-15 17:06:35 -04001169 while((bio = bio_list_pop(&bio_list_on_stack)))
1170 generic_make_request(bio);
Vivek Goyal69d60eb2011-03-09 08:27:37 +01001171 blk_finish_plug(&plug);
Vivek Goyale43473b2010-09-15 17:06:35 -04001172 }
Vivek Goyale43473b2010-09-15 17:06:35 -04001173}
1174
Tejun Heof95a04a2012-04-16 13:57:26 -07001175static u64 tg_prfill_conf_u64(struct seq_file *sf, struct blkg_policy_data *pd,
1176 int off)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001177{
Tejun Heof95a04a2012-04-16 13:57:26 -07001178 struct throtl_grp *tg = pd_to_tg(pd);
1179 u64 v = *(u64 *)((void *)tg + off);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001180
Shaohua Li2ab54922017-03-27 10:51:29 -07001181 if (v == U64_MAX)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001182 return 0;
Tejun Heof95a04a2012-04-16 13:57:26 -07001183 return __blkg_prfill_u64(sf, pd, v);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001184}
1185
Tejun Heof95a04a2012-04-16 13:57:26 -07001186static u64 tg_prfill_conf_uint(struct seq_file *sf, struct blkg_policy_data *pd,
1187 int off)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001188{
Tejun Heof95a04a2012-04-16 13:57:26 -07001189 struct throtl_grp *tg = pd_to_tg(pd);
1190 unsigned int v = *(unsigned int *)((void *)tg + off);
Tejun Heoaf133ce2012-04-01 14:38:44 -07001191
Shaohua Li2ab54922017-03-27 10:51:29 -07001192 if (v == UINT_MAX)
Tejun Heoaf133ce2012-04-01 14:38:44 -07001193 return 0;
Tejun Heof95a04a2012-04-16 13:57:26 -07001194 return __blkg_prfill_u64(sf, pd, v);
Tejun Heoaf133ce2012-04-01 14:38:44 -07001195}
1196
Tejun Heo2da8ca82013-12-05 12:28:04 -05001197static int tg_print_conf_u64(struct seq_file *sf, void *v)
Tejun Heoaf133ce2012-04-01 14:38:44 -07001198{
Tejun Heo2da8ca82013-12-05 12:28:04 -05001199 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), tg_prfill_conf_u64,
1200 &blkcg_policy_throtl, seq_cft(sf)->private, false);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001201 return 0;
1202}
1203
Tejun Heo2da8ca82013-12-05 12:28:04 -05001204static int tg_print_conf_uint(struct seq_file *sf, void *v)
Vivek Goyale43473b2010-09-15 17:06:35 -04001205{
Tejun Heo2da8ca82013-12-05 12:28:04 -05001206 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), tg_prfill_conf_uint,
1207 &blkcg_policy_throtl, seq_cft(sf)->private, false);
Tejun Heoaf133ce2012-04-01 14:38:44 -07001208 return 0;
Vivek Goyale43473b2010-09-15 17:06:35 -04001209}
1210
Tejun Heo69948b02015-08-18 14:55:32 -07001211static void tg_conf_updated(struct throtl_grp *tg)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001212{
Tejun Heo69948b02015-08-18 14:55:32 -07001213 struct throtl_service_queue *sq = &tg->service_queue;
Tejun Heo492eb212013-08-08 20:11:25 -04001214 struct cgroup_subsys_state *pos_css;
Tejun Heo69948b02015-08-18 14:55:32 -07001215 struct blkcg_gq *blkg;
Tejun Heoaf133ce2012-04-01 14:38:44 -07001216
Tejun Heofda6f272013-05-14 13:52:36 -07001217 throtl_log(&tg->service_queue,
1218 "limit change rbps=%llu wbps=%llu riops=%u wiops=%u",
Shaohua Li9f626e32017-03-27 10:51:30 -07001219 tg_bps_limit(tg, READ), tg_bps_limit(tg, WRITE),
1220 tg_iops_limit(tg, READ), tg_iops_limit(tg, WRITE));
Tejun Heo632b4492013-05-14 13:52:31 -07001221
1222 /*
Tejun Heo693e7512013-05-14 13:52:38 -07001223 * Update has_rules[] flags for the updated tg's subtree. A tg is
1224 * considered to have rules if either the tg itself or any of its
1225 * ancestors has rules. This identifies groups without any
1226 * restrictions in the whole hierarchy and allows them to bypass
1227 * blk-throttle.
1228 */
Tejun Heo69948b02015-08-18 14:55:32 -07001229 blkg_for_each_descendant_pre(blkg, pos_css, tg_to_blkg(tg))
Tejun Heo693e7512013-05-14 13:52:38 -07001230 tg_update_has_rules(blkg_to_tg(blkg));
1231
1232 /*
Tejun Heo632b4492013-05-14 13:52:31 -07001233 * We're already holding queue_lock and know @tg is valid. Let's
1234 * apply the new config directly.
1235 *
1236 * Restart the slices for both READ and WRITES. It might happen
1237 * that a group's limit are dropped suddenly and we don't want to
1238 * account recently dispatched IO with new low rate.
1239 */
Tejun Heo0f3457f2013-05-14 13:52:32 -07001240 throtl_start_new_slice(tg, 0);
1241 throtl_start_new_slice(tg, 1);
Tejun Heo632b4492013-05-14 13:52:31 -07001242
Tejun Heo5b2c16a2013-05-14 13:52:32 -07001243 if (tg->flags & THROTL_TG_PENDING) {
Tejun Heo77216b02013-05-14 13:52:36 -07001244 tg_update_disptime(tg);
Tejun Heo7f52f982013-05-14 13:52:37 -07001245 throtl_schedule_next_dispatch(sq->parent_sq, true);
Tejun Heo632b4492013-05-14 13:52:31 -07001246 }
Tejun Heo69948b02015-08-18 14:55:32 -07001247}
Tejun Heo60c2bc22012-04-01 14:38:43 -07001248
Tejun Heo69948b02015-08-18 14:55:32 -07001249static ssize_t tg_set_conf(struct kernfs_open_file *of,
1250 char *buf, size_t nbytes, loff_t off, bool is_u64)
1251{
1252 struct blkcg *blkcg = css_to_blkcg(of_css(of));
1253 struct blkg_conf_ctx ctx;
1254 struct throtl_grp *tg;
1255 int ret;
1256 u64 v;
1257
1258 ret = blkg_conf_prep(blkcg, &blkcg_policy_throtl, buf, &ctx);
1259 if (ret)
1260 return ret;
1261
1262 ret = -EINVAL;
1263 if (sscanf(ctx.body, "%llu", &v) != 1)
1264 goto out_finish;
1265 if (!v)
Shaohua Li2ab54922017-03-27 10:51:29 -07001266 v = U64_MAX;
Tejun Heo69948b02015-08-18 14:55:32 -07001267
1268 tg = blkg_to_tg(ctx.blkg);
1269
1270 if (is_u64)
1271 *(u64 *)((void *)tg + of_cft(of)->private) = v;
1272 else
1273 *(unsigned int *)((void *)tg + of_cft(of)->private) = v;
1274
1275 tg_conf_updated(tg);
Tejun Heo36aa9e52015-08-18 14:55:31 -07001276 ret = 0;
1277out_finish:
Tejun Heo60c2bc22012-04-01 14:38:43 -07001278 blkg_conf_finish(&ctx);
Tejun Heo36aa9e52015-08-18 14:55:31 -07001279 return ret ?: nbytes;
Tejun Heo60c2bc22012-04-01 14:38:43 -07001280}
1281
Tejun Heo451af502014-05-13 12:16:21 -04001282static ssize_t tg_set_conf_u64(struct kernfs_open_file *of,
1283 char *buf, size_t nbytes, loff_t off)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001284{
Tejun Heo451af502014-05-13 12:16:21 -04001285 return tg_set_conf(of, buf, nbytes, off, true);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001286}
1287
Tejun Heo451af502014-05-13 12:16:21 -04001288static ssize_t tg_set_conf_uint(struct kernfs_open_file *of,
1289 char *buf, size_t nbytes, loff_t off)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001290{
Tejun Heo451af502014-05-13 12:16:21 -04001291 return tg_set_conf(of, buf, nbytes, off, false);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001292}
1293
Tejun Heo880f50e2015-08-18 14:55:30 -07001294static struct cftype throtl_legacy_files[] = {
Tejun Heo60c2bc22012-04-01 14:38:43 -07001295 {
1296 .name = "throttle.read_bps_device",
Shaohua Li9f626e32017-03-27 10:51:30 -07001297 .private = offsetof(struct throtl_grp, bps[READ][LIMIT_MAX]),
Tejun Heo2da8ca82013-12-05 12:28:04 -05001298 .seq_show = tg_print_conf_u64,
Tejun Heo451af502014-05-13 12:16:21 -04001299 .write = tg_set_conf_u64,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001300 },
1301 {
1302 .name = "throttle.write_bps_device",
Shaohua Li9f626e32017-03-27 10:51:30 -07001303 .private = offsetof(struct throtl_grp, bps[WRITE][LIMIT_MAX]),
Tejun Heo2da8ca82013-12-05 12:28:04 -05001304 .seq_show = tg_print_conf_u64,
Tejun Heo451af502014-05-13 12:16:21 -04001305 .write = tg_set_conf_u64,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001306 },
1307 {
1308 .name = "throttle.read_iops_device",
Shaohua Li9f626e32017-03-27 10:51:30 -07001309 .private = offsetof(struct throtl_grp, iops[READ][LIMIT_MAX]),
Tejun Heo2da8ca82013-12-05 12:28:04 -05001310 .seq_show = tg_print_conf_uint,
Tejun Heo451af502014-05-13 12:16:21 -04001311 .write = tg_set_conf_uint,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001312 },
1313 {
1314 .name = "throttle.write_iops_device",
Shaohua Li9f626e32017-03-27 10:51:30 -07001315 .private = offsetof(struct throtl_grp, iops[WRITE][LIMIT_MAX]),
Tejun Heo2da8ca82013-12-05 12:28:04 -05001316 .seq_show = tg_print_conf_uint,
Tejun Heo451af502014-05-13 12:16:21 -04001317 .write = tg_set_conf_uint,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001318 },
1319 {
1320 .name = "throttle.io_service_bytes",
Tejun Heo77ea7332015-08-18 14:55:24 -07001321 .private = (unsigned long)&blkcg_policy_throtl,
1322 .seq_show = blkg_print_stat_bytes,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001323 },
1324 {
1325 .name = "throttle.io_serviced",
Tejun Heo77ea7332015-08-18 14:55:24 -07001326 .private = (unsigned long)&blkcg_policy_throtl,
1327 .seq_show = blkg_print_stat_ios,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001328 },
1329 { } /* terminate */
1330};
1331
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001332static u64 tg_prfill_limit(struct seq_file *sf, struct blkg_policy_data *pd,
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001333 int off)
1334{
1335 struct throtl_grp *tg = pd_to_tg(pd);
1336 const char *dname = blkg_dev_name(pd->blkg);
1337 char bufs[4][21] = { "max", "max", "max", "max" };
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001338 u64 bps_dft;
1339 unsigned int iops_dft;
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001340
1341 if (!dname)
1342 return 0;
Shaohua Li9f626e32017-03-27 10:51:30 -07001343
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001344 if (off == LIMIT_LOW) {
1345 bps_dft = 0;
1346 iops_dft = 0;
1347 } else {
1348 bps_dft = U64_MAX;
1349 iops_dft = UINT_MAX;
1350 }
1351
1352 if (tg->bps_conf[READ][off] == bps_dft &&
1353 tg->bps_conf[WRITE][off] == bps_dft &&
1354 tg->iops_conf[READ][off] == iops_dft &&
1355 tg->iops_conf[WRITE][off] == iops_dft)
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001356 return 0;
1357
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001358 if (tg->bps_conf[READ][off] != bps_dft)
Shaohua Li9f626e32017-03-27 10:51:30 -07001359 snprintf(bufs[0], sizeof(bufs[0]), "%llu",
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001360 tg->bps_conf[READ][off]);
1361 if (tg->bps_conf[WRITE][off] != bps_dft)
Shaohua Li9f626e32017-03-27 10:51:30 -07001362 snprintf(bufs[1], sizeof(bufs[1]), "%llu",
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001363 tg->bps_conf[WRITE][off]);
1364 if (tg->iops_conf[READ][off] != iops_dft)
Shaohua Li9f626e32017-03-27 10:51:30 -07001365 snprintf(bufs[2], sizeof(bufs[2]), "%u",
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001366 tg->iops_conf[READ][off]);
1367 if (tg->iops_conf[WRITE][off] != iops_dft)
Shaohua Li9f626e32017-03-27 10:51:30 -07001368 snprintf(bufs[3], sizeof(bufs[3]), "%u",
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001369 tg->iops_conf[WRITE][off]);
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001370
1371 seq_printf(sf, "%s rbps=%s wbps=%s riops=%s wiops=%s\n",
1372 dname, bufs[0], bufs[1], bufs[2], bufs[3]);
1373 return 0;
1374}
1375
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001376static int tg_print_limit(struct seq_file *sf, void *v)
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001377{
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001378 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), tg_prfill_limit,
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001379 &blkcg_policy_throtl, seq_cft(sf)->private, false);
1380 return 0;
1381}
1382
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001383static ssize_t tg_set_limit(struct kernfs_open_file *of,
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001384 char *buf, size_t nbytes, loff_t off)
1385{
1386 struct blkcg *blkcg = css_to_blkcg(of_css(of));
1387 struct blkg_conf_ctx ctx;
1388 struct throtl_grp *tg;
1389 u64 v[4];
1390 int ret;
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001391 int index = of_cft(of)->private;
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001392
1393 ret = blkg_conf_prep(blkcg, &blkcg_policy_throtl, buf, &ctx);
1394 if (ret)
1395 return ret;
1396
1397 tg = blkg_to_tg(ctx.blkg);
1398
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001399 v[0] = tg->bps_conf[READ][index];
1400 v[1] = tg->bps_conf[WRITE][index];
1401 v[2] = tg->iops_conf[READ][index];
1402 v[3] = tg->iops_conf[WRITE][index];
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001403
1404 while (true) {
1405 char tok[27]; /* wiops=18446744073709551616 */
1406 char *p;
Shaohua Li2ab54922017-03-27 10:51:29 -07001407 u64 val = U64_MAX;
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001408 int len;
1409
1410 if (sscanf(ctx.body, "%26s%n", tok, &len) != 1)
1411 break;
1412 if (tok[0] == '\0')
1413 break;
1414 ctx.body += len;
1415
1416 ret = -EINVAL;
1417 p = tok;
1418 strsep(&p, "=");
1419 if (!p || (sscanf(p, "%llu", &val) != 1 && strcmp(p, "max")))
1420 goto out_finish;
1421
1422 ret = -ERANGE;
1423 if (!val)
1424 goto out_finish;
1425
1426 ret = -EINVAL;
1427 if (!strcmp(tok, "rbps"))
1428 v[0] = val;
1429 else if (!strcmp(tok, "wbps"))
1430 v[1] = val;
1431 else if (!strcmp(tok, "riops"))
1432 v[2] = min_t(u64, val, UINT_MAX);
1433 else if (!strcmp(tok, "wiops"))
1434 v[3] = min_t(u64, val, UINT_MAX);
1435 else
1436 goto out_finish;
1437 }
1438
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001439 tg->bps_conf[READ][index] = v[0];
1440 tg->bps_conf[WRITE][index] = v[1];
1441 tg->iops_conf[READ][index] = v[2];
1442 tg->iops_conf[WRITE][index] = v[3];
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001443
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001444 if (index == LIMIT_MAX) {
1445 tg->bps[READ][index] = v[0];
1446 tg->bps[WRITE][index] = v[1];
1447 tg->iops[READ][index] = v[2];
1448 tg->iops[WRITE][index] = v[3];
1449 }
1450 tg->bps[READ][LIMIT_LOW] = min(tg->bps_conf[READ][LIMIT_LOW],
1451 tg->bps_conf[READ][LIMIT_MAX]);
1452 tg->bps[WRITE][LIMIT_LOW] = min(tg->bps_conf[WRITE][LIMIT_LOW],
1453 tg->bps_conf[WRITE][LIMIT_MAX]);
1454 tg->iops[READ][LIMIT_LOW] = min(tg->iops_conf[READ][LIMIT_LOW],
1455 tg->iops_conf[READ][LIMIT_MAX]);
1456 tg->iops[WRITE][LIMIT_LOW] = min(tg->iops_conf[WRITE][LIMIT_LOW],
1457 tg->iops_conf[WRITE][LIMIT_MAX]);
1458
1459 if (index == LIMIT_LOW) {
1460 blk_throtl_update_limit_valid(tg->td);
1461 if (tg->td->limit_valid[LIMIT_LOW])
1462 tg->td->limit_index = LIMIT_LOW;
1463 }
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001464 tg_conf_updated(tg);
1465 ret = 0;
1466out_finish:
1467 blkg_conf_finish(&ctx);
1468 return ret ?: nbytes;
1469}
1470
1471static struct cftype throtl_files[] = {
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001472#ifdef CONFIG_BLK_DEV_THROTTLING_LOW
1473 {
1474 .name = "low",
1475 .flags = CFTYPE_NOT_ON_ROOT,
1476 .seq_show = tg_print_limit,
1477 .write = tg_set_limit,
1478 .private = LIMIT_LOW,
1479 },
1480#endif
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001481 {
1482 .name = "max",
1483 .flags = CFTYPE_NOT_ON_ROOT,
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001484 .seq_show = tg_print_limit,
1485 .write = tg_set_limit,
1486 .private = LIMIT_MAX,
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001487 },
1488 { } /* terminate */
1489};
1490
Vivek Goyalda527772011-03-02 19:05:33 -05001491static void throtl_shutdown_wq(struct request_queue *q)
Vivek Goyale43473b2010-09-15 17:06:35 -04001492{
1493 struct throtl_data *td = q->td;
1494
Tejun Heo69df0ab2013-05-14 13:52:36 -07001495 cancel_work_sync(&td->dispatch_work);
Vivek Goyale43473b2010-09-15 17:06:35 -04001496}
1497
Tejun Heo3c798392012-04-16 13:57:25 -07001498static struct blkcg_policy blkcg_policy_throtl = {
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001499 .dfl_cftypes = throtl_files,
Tejun Heo880f50e2015-08-18 14:55:30 -07001500 .legacy_cftypes = throtl_legacy_files,
Tejun Heof9fcc2d2012-04-16 13:57:27 -07001501
Tejun Heo001bea72015-08-18 14:55:11 -07001502 .pd_alloc_fn = throtl_pd_alloc,
Tejun Heof9fcc2d2012-04-16 13:57:27 -07001503 .pd_init_fn = throtl_pd_init,
Tejun Heo693e7512013-05-14 13:52:38 -07001504 .pd_online_fn = throtl_pd_online,
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001505 .pd_offline_fn = throtl_pd_offline,
Tejun Heo001bea72015-08-18 14:55:11 -07001506 .pd_free_fn = throtl_pd_free,
Vivek Goyale43473b2010-09-15 17:06:35 -04001507};
1508
Tejun Heoae118892015-08-18 14:55:20 -07001509bool blk_throtl_bio(struct request_queue *q, struct blkcg_gq *blkg,
1510 struct bio *bio)
Vivek Goyale43473b2010-09-15 17:06:35 -04001511{
Tejun Heoc5cc2072013-05-14 13:52:38 -07001512 struct throtl_qnode *qn = NULL;
Tejun Heoae118892015-08-18 14:55:20 -07001513 struct throtl_grp *tg = blkg_to_tg(blkg ?: q->root_blkg);
Tejun Heo73f0d492013-05-14 13:52:35 -07001514 struct throtl_service_queue *sq;
Tejun Heo0e9f4162013-05-14 13:52:35 -07001515 bool rw = bio_data_dir(bio);
Tejun Heobc16a4f2011-10-19 14:33:01 +02001516 bool throttled = false;
Vivek Goyale43473b2010-09-15 17:06:35 -04001517
Tejun Heoae118892015-08-18 14:55:20 -07001518 WARN_ON_ONCE(!rcu_read_lock_held());
1519
Tejun Heo2a0f61e2013-05-14 13:52:36 -07001520 /* see throtl_charge_bio() */
Christoph Hellwig8d2bbd42016-10-20 15:12:12 +02001521 if (bio_flagged(bio, BIO_THROTTLED) || !tg->has_rules[rw])
Tejun Heobc16a4f2011-10-19 14:33:01 +02001522 goto out;
Vivek Goyale43473b2010-09-15 17:06:35 -04001523
1524 spin_lock_irq(q->queue_lock);
Tejun Heoc9589f02015-08-18 14:55:19 -07001525
1526 if (unlikely(blk_queue_bypass(q)))
Tejun Heobc16a4f2011-10-19 14:33:01 +02001527 goto out_unlock;
Vivek Goyalf469a7b2011-05-19 15:38:23 -04001528
Tejun Heo73f0d492013-05-14 13:52:35 -07001529 sq = &tg->service_queue;
1530
Tejun Heo9e660ac2013-05-14 13:52:38 -07001531 while (true) {
1532 /* throtl is FIFO - if bios are already queued, should queue */
1533 if (sq->nr_queued[rw])
1534 break;
Vivek Goyalde701c72011-03-07 21:09:32 +01001535
Tejun Heo9e660ac2013-05-14 13:52:38 -07001536 /* if above limits, break to queue */
1537 if (!tg_may_dispatch(tg, bio, NULL))
1538 break;
1539
1540 /* within limits, let's charge and dispatch directly */
Vivek Goyale43473b2010-09-15 17:06:35 -04001541 throtl_charge_bio(tg, bio);
Vivek Goyal04521db2011-03-22 21:54:29 +01001542
1543 /*
1544 * We need to trim slice even when bios are not being queued
1545 * otherwise it might happen that a bio is not queued for
1546 * a long time and slice keeps on extending and trim is not
1547 * called for a long time. Now if limits are reduced suddenly
1548 * we take into account all the IO dispatched so far at new
1549 * low rate and * newly queued IO gets a really long dispatch
1550 * time.
1551 *
1552 * So keep on trimming slice even if bio is not queued.
1553 */
Tejun Heo0f3457f2013-05-14 13:52:32 -07001554 throtl_trim_slice(tg, rw);
Tejun Heo9e660ac2013-05-14 13:52:38 -07001555
1556 /*
1557 * @bio passed through this layer without being throttled.
1558 * Climb up the ladder. If we''re already at the top, it
1559 * can be executed directly.
1560 */
Tejun Heoc5cc2072013-05-14 13:52:38 -07001561 qn = &tg->qnode_on_parent[rw];
Tejun Heo9e660ac2013-05-14 13:52:38 -07001562 sq = sq->parent_sq;
1563 tg = sq_to_tg(sq);
1564 if (!tg)
1565 goto out_unlock;
Vivek Goyale43473b2010-09-15 17:06:35 -04001566 }
1567
Tejun Heo9e660ac2013-05-14 13:52:38 -07001568 /* out-of-limit, queue to @tg */
Tejun Heofda6f272013-05-14 13:52:36 -07001569 throtl_log(sq, "[%c] bio. bdisp=%llu sz=%u bps=%llu iodisp=%u iops=%u queued=%d/%d",
1570 rw == READ ? 'R' : 'W',
Shaohua Li9f626e32017-03-27 10:51:30 -07001571 tg->bytes_disp[rw], bio->bi_iter.bi_size,
1572 tg_bps_limit(tg, rw),
1573 tg->io_disp[rw], tg_iops_limit(tg, rw),
Tejun Heofda6f272013-05-14 13:52:36 -07001574 sq->nr_queued[READ], sq->nr_queued[WRITE]);
Vivek Goyale43473b2010-09-15 17:06:35 -04001575
Tejun Heo671058f2012-03-05 13:15:29 -08001576 bio_associate_current(bio);
Tejun Heo6bc9c2b2013-05-14 13:52:38 -07001577 tg->td->nr_queued[rw]++;
Tejun Heoc5cc2072013-05-14 13:52:38 -07001578 throtl_add_bio_tg(bio, qn, tg);
Tejun Heobc16a4f2011-10-19 14:33:01 +02001579 throttled = true;
Vivek Goyale43473b2010-09-15 17:06:35 -04001580
Tejun Heo7f52f982013-05-14 13:52:37 -07001581 /*
1582 * Update @tg's dispatch time and force schedule dispatch if @tg
1583 * was empty before @bio. The forced scheduling isn't likely to
1584 * cause undue delay as @bio is likely to be dispatched directly if
1585 * its @tg's disptime is not in the future.
1586 */
Tejun Heo0e9f4162013-05-14 13:52:35 -07001587 if (tg->flags & THROTL_TG_WAS_EMPTY) {
Tejun Heo77216b02013-05-14 13:52:36 -07001588 tg_update_disptime(tg);
Tejun Heo7f52f982013-05-14 13:52:37 -07001589 throtl_schedule_next_dispatch(tg->service_queue.parent_sq, true);
Vivek Goyale43473b2010-09-15 17:06:35 -04001590 }
1591
Tejun Heobc16a4f2011-10-19 14:33:01 +02001592out_unlock:
Vivek Goyale43473b2010-09-15 17:06:35 -04001593 spin_unlock_irq(q->queue_lock);
Tejun Heobc16a4f2011-10-19 14:33:01 +02001594out:
Tejun Heo2a0f61e2013-05-14 13:52:36 -07001595 /*
1596 * As multiple blk-throtls may stack in the same issue path, we
1597 * don't want bios to leave with the flag set. Clear the flag if
1598 * being issued.
1599 */
1600 if (!throttled)
Christoph Hellwig8d2bbd42016-10-20 15:12:12 +02001601 bio_clear_flag(bio, BIO_THROTTLED);
Tejun Heobc16a4f2011-10-19 14:33:01 +02001602 return throttled;
Vivek Goyale43473b2010-09-15 17:06:35 -04001603}
1604
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001605/*
1606 * Dispatch all bios from all children tg's queued on @parent_sq. On
1607 * return, @parent_sq is guaranteed to not have any active children tg's
1608 * and all bios from previously active tg's are on @parent_sq->bio_lists[].
1609 */
1610static void tg_drain_bios(struct throtl_service_queue *parent_sq)
1611{
1612 struct throtl_grp *tg;
1613
1614 while ((tg = throtl_rb_first(parent_sq))) {
1615 struct throtl_service_queue *sq = &tg->service_queue;
1616 struct bio *bio;
1617
1618 throtl_dequeue_tg(tg);
1619
Tejun Heoc5cc2072013-05-14 13:52:38 -07001620 while ((bio = throtl_peek_queued(&sq->queued[READ])))
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001621 tg_dispatch_one_bio(tg, bio_data_dir(bio));
Tejun Heoc5cc2072013-05-14 13:52:38 -07001622 while ((bio = throtl_peek_queued(&sq->queued[WRITE])))
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001623 tg_dispatch_one_bio(tg, bio_data_dir(bio));
1624 }
1625}
1626
Tejun Heoc9a929d2011-10-19 14:42:16 +02001627/**
1628 * blk_throtl_drain - drain throttled bios
1629 * @q: request_queue to drain throttled bios for
1630 *
1631 * Dispatch all currently throttled bios on @q through ->make_request_fn().
1632 */
1633void blk_throtl_drain(struct request_queue *q)
1634 __releases(q->queue_lock) __acquires(q->queue_lock)
1635{
1636 struct throtl_data *td = q->td;
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001637 struct blkcg_gq *blkg;
Tejun Heo492eb212013-08-08 20:11:25 -04001638 struct cgroup_subsys_state *pos_css;
Tejun Heoc9a929d2011-10-19 14:42:16 +02001639 struct bio *bio;
Tejun Heo651930b2013-05-14 13:52:35 -07001640 int rw;
Tejun Heoc9a929d2011-10-19 14:42:16 +02001641
Andi Kleen8bcb6c72012-03-30 12:33:28 +02001642 queue_lockdep_assert_held(q);
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001643 rcu_read_lock();
Tejun Heoc9a929d2011-10-19 14:42:16 +02001644
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001645 /*
1646 * Drain each tg while doing post-order walk on the blkg tree, so
1647 * that all bios are propagated to td->service_queue. It'd be
1648 * better to walk service_queue tree directly but blkg walk is
1649 * easier.
1650 */
Tejun Heo492eb212013-08-08 20:11:25 -04001651 blkg_for_each_descendant_post(blkg, pos_css, td->queue->root_blkg)
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001652 tg_drain_bios(&blkg_to_tg(blkg)->service_queue);
Tejun Heo73f0d492013-05-14 13:52:35 -07001653
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001654 /* finally, transfer bios from top-level tg's into the td */
1655 tg_drain_bios(&td->service_queue);
1656
1657 rcu_read_unlock();
Tejun Heoc9a929d2011-10-19 14:42:16 +02001658 spin_unlock_irq(q->queue_lock);
1659
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001660 /* all bios now should be in td->service_queue, issue them */
Tejun Heo651930b2013-05-14 13:52:35 -07001661 for (rw = READ; rw <= WRITE; rw++)
Tejun Heoc5cc2072013-05-14 13:52:38 -07001662 while ((bio = throtl_pop_queued(&td->service_queue.queued[rw],
1663 NULL)))
Tejun Heo651930b2013-05-14 13:52:35 -07001664 generic_make_request(bio);
Tejun Heoc9a929d2011-10-19 14:42:16 +02001665
1666 spin_lock_irq(q->queue_lock);
1667}
1668
Vivek Goyale43473b2010-09-15 17:06:35 -04001669int blk_throtl_init(struct request_queue *q)
1670{
1671 struct throtl_data *td;
Tejun Heoa2b16932012-04-13 13:11:33 -07001672 int ret;
Vivek Goyale43473b2010-09-15 17:06:35 -04001673
1674 td = kzalloc_node(sizeof(*td), GFP_KERNEL, q->node);
1675 if (!td)
1676 return -ENOMEM;
1677
Tejun Heo69df0ab2013-05-14 13:52:36 -07001678 INIT_WORK(&td->dispatch_work, blk_throtl_dispatch_work_fn);
Tejun Heob2ce2642015-08-18 14:55:13 -07001679 throtl_service_queue_init(&td->service_queue);
Vivek Goyale43473b2010-09-15 17:06:35 -04001680
Tejun Heocd1604f2012-03-05 13:15:06 -08001681 q->td = td;
Vivek Goyal29b12582011-05-19 15:38:24 -04001682 td->queue = q;
Vivek Goyal02977e42010-10-01 14:49:48 +02001683
Shaohua Li9f626e32017-03-27 10:51:30 -07001684 td->limit_valid[LIMIT_MAX] = true;
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001685 td->limit_index = LIMIT_MAX;
Tejun Heoa2b16932012-04-13 13:11:33 -07001686 /* activate policy */
Tejun Heo3c798392012-04-16 13:57:25 -07001687 ret = blkcg_activate_policy(q, &blkcg_policy_throtl);
Tejun Heoa2b16932012-04-13 13:11:33 -07001688 if (ret)
Vivek Goyal29b12582011-05-19 15:38:24 -04001689 kfree(td);
Tejun Heoa2b16932012-04-13 13:11:33 -07001690 return ret;
Vivek Goyale43473b2010-09-15 17:06:35 -04001691}
1692
1693void blk_throtl_exit(struct request_queue *q)
1694{
Tejun Heoc875f4d2012-03-05 13:15:22 -08001695 BUG_ON(!q->td);
Vivek Goyalda527772011-03-02 19:05:33 -05001696 throtl_shutdown_wq(q);
Tejun Heo3c798392012-04-16 13:57:25 -07001697 blkcg_deactivate_policy(q, &blkcg_policy_throtl);
Tejun Heoc9a929d2011-10-19 14:42:16 +02001698 kfree(q->td);
Vivek Goyale43473b2010-09-15 17:06:35 -04001699}
1700
1701static int __init throtl_init(void)
1702{
Vivek Goyal450adcb2011-03-01 13:40:54 -05001703 kthrotld_workqueue = alloc_workqueue("kthrotld", WQ_MEM_RECLAIM, 0);
1704 if (!kthrotld_workqueue)
1705 panic("Failed to create kthrotld\n");
1706
Tejun Heo3c798392012-04-16 13:57:25 -07001707 return blkcg_policy_register(&blkcg_policy_throtl);
Vivek Goyale43473b2010-09-15 17:06:35 -04001708}
1709
1710module_init(throtl_init);