blob: 1fade5078fc16a902aa8c81342dc14c3871b0417 [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{
Shaohua Lib22c4172017-03-27 10:51:33 -0700215 struct blkcg_gq *blkg = tg_to_blkg(tg);
216 uint64_t ret;
217
218 if (cgroup_subsys_on_dfl(io_cgrp_subsys) && !blkg->parent)
219 return U64_MAX;
220 ret = tg->bps[rw][tg->td->limit_index];
221 if (ret == 0 && tg->td->limit_index == LIMIT_LOW)
222 return tg->bps[rw][LIMIT_MAX];
223 return ret;
Shaohua Li9f626e32017-03-27 10:51:30 -0700224}
225
226static unsigned int tg_iops_limit(struct throtl_grp *tg, int rw)
227{
Shaohua Lib22c4172017-03-27 10:51:33 -0700228 struct blkcg_gq *blkg = tg_to_blkg(tg);
229 unsigned int ret;
230
231 if (cgroup_subsys_on_dfl(io_cgrp_subsys) && !blkg->parent)
232 return UINT_MAX;
233 ret = tg->iops[rw][tg->td->limit_index];
234 if (ret == 0 && tg->td->limit_index == LIMIT_LOW)
235 return tg->iops[rw][LIMIT_MAX];
236 return ret;
Shaohua Li9f626e32017-03-27 10:51:30 -0700237}
238
Tejun Heofda6f272013-05-14 13:52:36 -0700239/**
240 * throtl_log - log debug message via blktrace
241 * @sq: the service_queue being reported
242 * @fmt: printf format string
243 * @args: printf args
244 *
245 * The messages are prefixed with "throtl BLKG_NAME" if @sq belongs to a
246 * throtl_grp; otherwise, just "throtl".
Tejun Heofda6f272013-05-14 13:52:36 -0700247 */
248#define throtl_log(sq, fmt, args...) do { \
249 struct throtl_grp *__tg = sq_to_tg((sq)); \
250 struct throtl_data *__td = sq_to_td((sq)); \
251 \
252 (void)__td; \
Shaohua Li59fa0222016-05-09 17:22:15 -0700253 if (likely(!blk_trace_note_message_enabled(__td->queue))) \
254 break; \
Tejun Heofda6f272013-05-14 13:52:36 -0700255 if ((__tg)) { \
256 char __pbuf[128]; \
257 \
258 blkg_path(tg_to_blkg(__tg), __pbuf, sizeof(__pbuf)); \
259 blk_add_trace_msg(__td->queue, "throtl %s " fmt, __pbuf, ##args); \
260 } else { \
261 blk_add_trace_msg(__td->queue, "throtl " fmt, ##args); \
262 } \
263} while (0)
Vivek Goyale43473b2010-09-15 17:06:35 -0400264
Tejun Heoc5cc2072013-05-14 13:52:38 -0700265static void throtl_qnode_init(struct throtl_qnode *qn, struct throtl_grp *tg)
266{
267 INIT_LIST_HEAD(&qn->node);
268 bio_list_init(&qn->bios);
269 qn->tg = tg;
270}
271
272/**
273 * throtl_qnode_add_bio - add a bio to a throtl_qnode and activate it
274 * @bio: bio being added
275 * @qn: qnode to add bio to
276 * @queued: the service_queue->queued[] list @qn belongs to
277 *
278 * Add @bio to @qn and put @qn on @queued if it's not already on.
279 * @qn->tg's reference count is bumped when @qn is activated. See the
280 * comment on top of throtl_qnode definition for details.
281 */
282static void throtl_qnode_add_bio(struct bio *bio, struct throtl_qnode *qn,
283 struct list_head *queued)
284{
285 bio_list_add(&qn->bios, bio);
286 if (list_empty(&qn->node)) {
287 list_add_tail(&qn->node, queued);
288 blkg_get(tg_to_blkg(qn->tg));
289 }
290}
291
292/**
293 * throtl_peek_queued - peek the first bio on a qnode list
294 * @queued: the qnode list to peek
295 */
296static struct bio *throtl_peek_queued(struct list_head *queued)
297{
298 struct throtl_qnode *qn = list_first_entry(queued, struct throtl_qnode, node);
299 struct bio *bio;
300
301 if (list_empty(queued))
302 return NULL;
303
304 bio = bio_list_peek(&qn->bios);
305 WARN_ON_ONCE(!bio);
306 return bio;
307}
308
309/**
310 * throtl_pop_queued - pop the first bio form a qnode list
311 * @queued: the qnode list to pop a bio from
312 * @tg_to_put: optional out argument for throtl_grp to put
313 *
314 * Pop the first bio from the qnode list @queued. After popping, the first
315 * qnode is removed from @queued if empty or moved to the end of @queued so
316 * that the popping order is round-robin.
317 *
318 * When the first qnode is removed, its associated throtl_grp should be put
319 * too. If @tg_to_put is NULL, this function automatically puts it;
320 * otherwise, *@tg_to_put is set to the throtl_grp to put and the caller is
321 * responsible for putting it.
322 */
323static struct bio *throtl_pop_queued(struct list_head *queued,
324 struct throtl_grp **tg_to_put)
325{
326 struct throtl_qnode *qn = list_first_entry(queued, struct throtl_qnode, node);
327 struct bio *bio;
328
329 if (list_empty(queued))
330 return NULL;
331
332 bio = bio_list_pop(&qn->bios);
333 WARN_ON_ONCE(!bio);
334
335 if (bio_list_empty(&qn->bios)) {
336 list_del_init(&qn->node);
337 if (tg_to_put)
338 *tg_to_put = qn->tg;
339 else
340 blkg_put(tg_to_blkg(qn->tg));
341 } else {
342 list_move_tail(&qn->node, queued);
343 }
344
345 return bio;
346}
347
Tejun Heo49a2f1e2013-05-14 13:52:34 -0700348/* init a service_queue, assumes the caller zeroed it */
Tejun Heob2ce2642015-08-18 14:55:13 -0700349static void throtl_service_queue_init(struct throtl_service_queue *sq)
Tejun Heo49a2f1e2013-05-14 13:52:34 -0700350{
Tejun Heoc5cc2072013-05-14 13:52:38 -0700351 INIT_LIST_HEAD(&sq->queued[0]);
352 INIT_LIST_HEAD(&sq->queued[1]);
Tejun Heo49a2f1e2013-05-14 13:52:34 -0700353 sq->pending_tree = RB_ROOT;
Tejun Heo69df0ab2013-05-14 13:52:36 -0700354 setup_timer(&sq->pending_timer, throtl_pending_timer_fn,
355 (unsigned long)sq);
356}
357
Tejun Heo001bea72015-08-18 14:55:11 -0700358static struct blkg_policy_data *throtl_pd_alloc(gfp_t gfp, int node)
359{
Tejun Heo4fb72032015-08-18 14:55:12 -0700360 struct throtl_grp *tg;
Tejun Heo24bdb8e2015-08-18 14:55:22 -0700361 int rw;
Tejun Heo4fb72032015-08-18 14:55:12 -0700362
363 tg = kzalloc_node(sizeof(*tg), gfp, node);
364 if (!tg)
Tejun Heo77ea7332015-08-18 14:55:24 -0700365 return NULL;
Tejun Heo4fb72032015-08-18 14:55:12 -0700366
Tejun Heob2ce2642015-08-18 14:55:13 -0700367 throtl_service_queue_init(&tg->service_queue);
368
369 for (rw = READ; rw <= WRITE; rw++) {
370 throtl_qnode_init(&tg->qnode_on_self[rw], tg);
371 throtl_qnode_init(&tg->qnode_on_parent[rw], tg);
372 }
373
374 RB_CLEAR_NODE(&tg->rb_node);
Shaohua Li9f626e32017-03-27 10:51:30 -0700375 tg->bps[READ][LIMIT_MAX] = U64_MAX;
376 tg->bps[WRITE][LIMIT_MAX] = U64_MAX;
377 tg->iops[READ][LIMIT_MAX] = UINT_MAX;
378 tg->iops[WRITE][LIMIT_MAX] = UINT_MAX;
Shaohua Licd5ab1b2017-03-27 10:51:32 -0700379 tg->bps_conf[READ][LIMIT_MAX] = U64_MAX;
380 tg->bps_conf[WRITE][LIMIT_MAX] = U64_MAX;
381 tg->iops_conf[READ][LIMIT_MAX] = UINT_MAX;
382 tg->iops_conf[WRITE][LIMIT_MAX] = UINT_MAX;
383 /* LIMIT_LOW will have default value 0 */
Tejun Heob2ce2642015-08-18 14:55:13 -0700384
Tejun Heo4fb72032015-08-18 14:55:12 -0700385 return &tg->pd;
Tejun Heo001bea72015-08-18 14:55:11 -0700386}
387
Tejun Heoa9520cd2015-08-18 14:55:14 -0700388static void throtl_pd_init(struct blkg_policy_data *pd)
Vivek Goyala29a1712011-05-19 15:38:19 -0400389{
Tejun Heoa9520cd2015-08-18 14:55:14 -0700390 struct throtl_grp *tg = pd_to_tg(pd);
391 struct blkcg_gq *blkg = tg_to_blkg(tg);
Tejun Heo77216b02013-05-14 13:52:36 -0700392 struct throtl_data *td = blkg->q->td;
Tejun Heob2ce2642015-08-18 14:55:13 -0700393 struct throtl_service_queue *sq = &tg->service_queue;
Tejun Heocd1604f2012-03-05 13:15:06 -0800394
Tejun Heo91381252013-05-14 13:52:38 -0700395 /*
Tejun Heoaa6ec292014-07-09 10:08:08 -0400396 * If on the default hierarchy, we switch to properly hierarchical
Tejun Heo91381252013-05-14 13:52:38 -0700397 * behavior where limits on a given throtl_grp are applied to the
398 * whole subtree rather than just the group itself. e.g. If 16M
399 * read_bps limit is set on the root group, the whole system can't
400 * exceed 16M for the device.
401 *
Tejun Heoaa6ec292014-07-09 10:08:08 -0400402 * If not on the default hierarchy, the broken flat hierarchy
Tejun Heo91381252013-05-14 13:52:38 -0700403 * behavior is retained where all throtl_grps are treated as if
404 * they're all separate root groups right below throtl_data.
405 * Limits of a group don't interact with limits of other groups
406 * regardless of the position of the group in the hierarchy.
407 */
Tejun Heob2ce2642015-08-18 14:55:13 -0700408 sq->parent_sq = &td->service_queue;
Tejun Heo9e10a132015-09-18 11:56:28 -0400409 if (cgroup_subsys_on_dfl(io_cgrp_subsys) && blkg->parent)
Tejun Heob2ce2642015-08-18 14:55:13 -0700410 sq->parent_sq = &blkg_to_tg(blkg->parent)->service_queue;
Tejun Heo77216b02013-05-14 13:52:36 -0700411 tg->td = td;
Tejun Heo8a3d2612012-04-01 14:38:44 -0700412}
413
Tejun Heo693e7512013-05-14 13:52:38 -0700414/*
415 * Set has_rules[] if @tg or any of its parents have limits configured.
416 * This doesn't require walking up to the top of the hierarchy as the
417 * parent's has_rules[] is guaranteed to be correct.
418 */
419static void tg_update_has_rules(struct throtl_grp *tg)
420{
421 struct throtl_grp *parent_tg = sq_to_tg(tg->service_queue.parent_sq);
Shaohua Li9f626e32017-03-27 10:51:30 -0700422 struct throtl_data *td = tg->td;
Tejun Heo693e7512013-05-14 13:52:38 -0700423 int rw;
424
425 for (rw = READ; rw <= WRITE; rw++)
426 tg->has_rules[rw] = (parent_tg && parent_tg->has_rules[rw]) ||
Shaohua Li9f626e32017-03-27 10:51:30 -0700427 (td->limit_valid[td->limit_index] &&
428 (tg_bps_limit(tg, rw) != U64_MAX ||
429 tg_iops_limit(tg, rw) != UINT_MAX));
Tejun Heo693e7512013-05-14 13:52:38 -0700430}
431
Tejun Heoa9520cd2015-08-18 14:55:14 -0700432static void throtl_pd_online(struct blkg_policy_data *pd)
Tejun Heo693e7512013-05-14 13:52:38 -0700433{
434 /*
435 * We don't want new groups to escape the limits of its ancestors.
436 * Update has_rules[] after a new group is brought online.
437 */
Tejun Heoa9520cd2015-08-18 14:55:14 -0700438 tg_update_has_rules(pd_to_tg(pd));
Tejun Heo693e7512013-05-14 13:52:38 -0700439}
440
Shaohua Licd5ab1b2017-03-27 10:51:32 -0700441static void blk_throtl_update_limit_valid(struct throtl_data *td)
442{
443 struct cgroup_subsys_state *pos_css;
444 struct blkcg_gq *blkg;
445 bool low_valid = false;
446
447 rcu_read_lock();
448 blkg_for_each_descendant_post(blkg, pos_css, td->queue->root_blkg) {
449 struct throtl_grp *tg = blkg_to_tg(blkg);
450
451 if (tg->bps[READ][LIMIT_LOW] || tg->bps[WRITE][LIMIT_LOW] ||
452 tg->iops[READ][LIMIT_LOW] || tg->iops[WRITE][LIMIT_LOW])
453 low_valid = true;
454 }
455 rcu_read_unlock();
456
457 td->limit_valid[LIMIT_LOW] = low_valid;
458}
459
460static void throtl_pd_offline(struct blkg_policy_data *pd)
461{
462 struct throtl_grp *tg = pd_to_tg(pd);
463
464 tg->bps[READ][LIMIT_LOW] = 0;
465 tg->bps[WRITE][LIMIT_LOW] = 0;
466 tg->iops[READ][LIMIT_LOW] = 0;
467 tg->iops[WRITE][LIMIT_LOW] = 0;
468
469 blk_throtl_update_limit_valid(tg->td);
470
471 if (tg->td->limit_index == LIMIT_LOW &&
472 !tg->td->limit_valid[LIMIT_LOW])
473 tg->td->limit_index = LIMIT_MAX;
474}
475
Tejun Heo001bea72015-08-18 14:55:11 -0700476static void throtl_pd_free(struct blkg_policy_data *pd)
477{
Tejun Heo4fb72032015-08-18 14:55:12 -0700478 struct throtl_grp *tg = pd_to_tg(pd);
479
Tejun Heob2ce2642015-08-18 14:55:13 -0700480 del_timer_sync(&tg->service_queue.pending_timer);
Tejun Heo4fb72032015-08-18 14:55:12 -0700481 kfree(tg);
Tejun Heo001bea72015-08-18 14:55:11 -0700482}
483
Tejun Heo0049af72013-05-14 13:52:33 -0700484static struct throtl_grp *
485throtl_rb_first(struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -0400486{
487 /* Service tree is empty */
Tejun Heo0049af72013-05-14 13:52:33 -0700488 if (!parent_sq->nr_pending)
Vivek Goyale43473b2010-09-15 17:06:35 -0400489 return NULL;
490
Tejun Heo0049af72013-05-14 13:52:33 -0700491 if (!parent_sq->first_pending)
492 parent_sq->first_pending = rb_first(&parent_sq->pending_tree);
Vivek Goyale43473b2010-09-15 17:06:35 -0400493
Tejun Heo0049af72013-05-14 13:52:33 -0700494 if (parent_sq->first_pending)
495 return rb_entry_tg(parent_sq->first_pending);
Vivek Goyale43473b2010-09-15 17:06:35 -0400496
497 return NULL;
498}
499
500static void rb_erase_init(struct rb_node *n, struct rb_root *root)
501{
502 rb_erase(n, root);
503 RB_CLEAR_NODE(n);
504}
505
Tejun Heo0049af72013-05-14 13:52:33 -0700506static void throtl_rb_erase(struct rb_node *n,
507 struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -0400508{
Tejun Heo0049af72013-05-14 13:52:33 -0700509 if (parent_sq->first_pending == n)
510 parent_sq->first_pending = NULL;
511 rb_erase_init(n, &parent_sq->pending_tree);
512 --parent_sq->nr_pending;
Vivek Goyale43473b2010-09-15 17:06:35 -0400513}
514
Tejun Heo0049af72013-05-14 13:52:33 -0700515static void update_min_dispatch_time(struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -0400516{
517 struct throtl_grp *tg;
518
Tejun Heo0049af72013-05-14 13:52:33 -0700519 tg = throtl_rb_first(parent_sq);
Vivek Goyale43473b2010-09-15 17:06:35 -0400520 if (!tg)
521 return;
522
Tejun Heo0049af72013-05-14 13:52:33 -0700523 parent_sq->first_pending_disptime = tg->disptime;
Vivek Goyale43473b2010-09-15 17:06:35 -0400524}
525
Tejun Heo77216b02013-05-14 13:52:36 -0700526static void tg_service_queue_add(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400527{
Tejun Heo77216b02013-05-14 13:52:36 -0700528 struct throtl_service_queue *parent_sq = tg->service_queue.parent_sq;
Tejun Heo0049af72013-05-14 13:52:33 -0700529 struct rb_node **node = &parent_sq->pending_tree.rb_node;
Vivek Goyale43473b2010-09-15 17:06:35 -0400530 struct rb_node *parent = NULL;
531 struct throtl_grp *__tg;
532 unsigned long key = tg->disptime;
533 int left = 1;
534
535 while (*node != NULL) {
536 parent = *node;
537 __tg = rb_entry_tg(parent);
538
539 if (time_before(key, __tg->disptime))
540 node = &parent->rb_left;
541 else {
542 node = &parent->rb_right;
543 left = 0;
544 }
545 }
546
547 if (left)
Tejun Heo0049af72013-05-14 13:52:33 -0700548 parent_sq->first_pending = &tg->rb_node;
Vivek Goyale43473b2010-09-15 17:06:35 -0400549
550 rb_link_node(&tg->rb_node, parent, node);
Tejun Heo0049af72013-05-14 13:52:33 -0700551 rb_insert_color(&tg->rb_node, &parent_sq->pending_tree);
Vivek Goyale43473b2010-09-15 17:06:35 -0400552}
553
Tejun Heo77216b02013-05-14 13:52:36 -0700554static void __throtl_enqueue_tg(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400555{
Tejun Heo77216b02013-05-14 13:52:36 -0700556 tg_service_queue_add(tg);
Tejun Heo5b2c16a2013-05-14 13:52:32 -0700557 tg->flags |= THROTL_TG_PENDING;
Tejun Heo77216b02013-05-14 13:52:36 -0700558 tg->service_queue.parent_sq->nr_pending++;
Vivek Goyale43473b2010-09-15 17:06:35 -0400559}
560
Tejun Heo77216b02013-05-14 13:52:36 -0700561static void throtl_enqueue_tg(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400562{
Tejun Heo5b2c16a2013-05-14 13:52:32 -0700563 if (!(tg->flags & THROTL_TG_PENDING))
Tejun Heo77216b02013-05-14 13:52:36 -0700564 __throtl_enqueue_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -0400565}
566
Tejun Heo77216b02013-05-14 13:52:36 -0700567static void __throtl_dequeue_tg(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400568{
Tejun Heo77216b02013-05-14 13:52:36 -0700569 throtl_rb_erase(&tg->rb_node, tg->service_queue.parent_sq);
Tejun Heo5b2c16a2013-05-14 13:52:32 -0700570 tg->flags &= ~THROTL_TG_PENDING;
Vivek Goyale43473b2010-09-15 17:06:35 -0400571}
572
Tejun Heo77216b02013-05-14 13:52:36 -0700573static void throtl_dequeue_tg(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400574{
Tejun Heo5b2c16a2013-05-14 13:52:32 -0700575 if (tg->flags & THROTL_TG_PENDING)
Tejun Heo77216b02013-05-14 13:52:36 -0700576 __throtl_dequeue_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -0400577}
578
Tejun Heoa9131a22013-05-14 13:52:31 -0700579/* Call with queue lock held */
Tejun Heo69df0ab2013-05-14 13:52:36 -0700580static void throtl_schedule_pending_timer(struct throtl_service_queue *sq,
581 unsigned long expires)
Tejun Heoa9131a22013-05-14 13:52:31 -0700582{
Tejun Heo69df0ab2013-05-14 13:52:36 -0700583 mod_timer(&sq->pending_timer, expires);
584 throtl_log(sq, "schedule timer. delay=%lu jiffies=%lu",
585 expires - jiffies, jiffies);
Tejun Heoa9131a22013-05-14 13:52:31 -0700586}
587
Tejun Heo7f52f982013-05-14 13:52:37 -0700588/**
589 * throtl_schedule_next_dispatch - schedule the next dispatch cycle
590 * @sq: the service_queue to schedule dispatch for
591 * @force: force scheduling
592 *
593 * Arm @sq->pending_timer so that the next dispatch cycle starts on the
594 * dispatch time of the first pending child. Returns %true if either timer
595 * is armed or there's no pending child left. %false if the current
596 * dispatch window is still open and the caller should continue
597 * dispatching.
598 *
599 * If @force is %true, the dispatch timer is always scheduled and this
600 * function is guaranteed to return %true. This is to be used when the
601 * caller can't dispatch itself and needs to invoke pending_timer
602 * unconditionally. Note that forced scheduling is likely to induce short
603 * delay before dispatch starts even if @sq->first_pending_disptime is not
604 * in the future and thus shouldn't be used in hot paths.
605 */
606static bool throtl_schedule_next_dispatch(struct throtl_service_queue *sq,
607 bool force)
Vivek Goyale43473b2010-09-15 17:06:35 -0400608{
Tejun Heo6a525602013-05-14 13:52:32 -0700609 /* any pending children left? */
Tejun Heoc9e03322013-05-14 13:52:32 -0700610 if (!sq->nr_pending)
Tejun Heo7f52f982013-05-14 13:52:37 -0700611 return true;
Vivek Goyale43473b2010-09-15 17:06:35 -0400612
Tejun Heoc9e03322013-05-14 13:52:32 -0700613 update_min_dispatch_time(sq);
Vivek Goyale43473b2010-09-15 17:06:35 -0400614
Tejun Heo69df0ab2013-05-14 13:52:36 -0700615 /* is the next dispatch time in the future? */
Tejun Heo7f52f982013-05-14 13:52:37 -0700616 if (force || time_after(sq->first_pending_disptime, jiffies)) {
Tejun Heo69df0ab2013-05-14 13:52:36 -0700617 throtl_schedule_pending_timer(sq, sq->first_pending_disptime);
Tejun Heo7f52f982013-05-14 13:52:37 -0700618 return true;
Tejun Heo69df0ab2013-05-14 13:52:36 -0700619 }
620
Tejun Heo7f52f982013-05-14 13:52:37 -0700621 /* tell the caller to continue dispatching */
622 return false;
Vivek Goyale43473b2010-09-15 17:06:35 -0400623}
624
Vivek Goyal32ee5bc2013-05-14 13:52:38 -0700625static inline void throtl_start_new_slice_with_credit(struct throtl_grp *tg,
626 bool rw, unsigned long start)
627{
628 tg->bytes_disp[rw] = 0;
629 tg->io_disp[rw] = 0;
630
631 /*
632 * Previous slice has expired. We must have trimmed it after last
633 * bio dispatch. That means since start of last slice, we never used
634 * that bandwidth. Do try to make use of that bandwidth while giving
635 * credit.
636 */
637 if (time_after_eq(start, tg->slice_start[rw]))
638 tg->slice_start[rw] = start;
639
640 tg->slice_end[rw] = jiffies + throtl_slice;
641 throtl_log(&tg->service_queue,
642 "[%c] new slice with credit start=%lu end=%lu jiffies=%lu",
643 rw == READ ? 'R' : 'W', tg->slice_start[rw],
644 tg->slice_end[rw], jiffies);
645}
646
Tejun Heo0f3457f2013-05-14 13:52:32 -0700647static inline void throtl_start_new_slice(struct throtl_grp *tg, bool rw)
Vivek Goyale43473b2010-09-15 17:06:35 -0400648{
649 tg->bytes_disp[rw] = 0;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400650 tg->io_disp[rw] = 0;
Vivek Goyale43473b2010-09-15 17:06:35 -0400651 tg->slice_start[rw] = jiffies;
652 tg->slice_end[rw] = jiffies + throtl_slice;
Tejun Heofda6f272013-05-14 13:52:36 -0700653 throtl_log(&tg->service_queue,
654 "[%c] new 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
Tejun Heo0f3457f2013-05-14 13:52:32 -0700659static inline void throtl_set_slice_end(struct throtl_grp *tg, bool rw,
660 unsigned long jiffy_end)
Vivek Goyald1ae8ff2010-12-01 19:34:46 +0100661{
662 tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
663}
664
Tejun Heo0f3457f2013-05-14 13:52:32 -0700665static inline void throtl_extend_slice(struct throtl_grp *tg, bool rw,
666 unsigned long jiffy_end)
Vivek Goyale43473b2010-09-15 17:06:35 -0400667{
668 tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
Tejun Heofda6f272013-05-14 13:52:36 -0700669 throtl_log(&tg->service_queue,
670 "[%c] extend slice start=%lu end=%lu jiffies=%lu",
671 rw == READ ? 'R' : 'W', tg->slice_start[rw],
672 tg->slice_end[rw], jiffies);
Vivek Goyale43473b2010-09-15 17:06:35 -0400673}
674
675/* Determine if previously allocated or extended slice is complete or not */
Tejun Heo0f3457f2013-05-14 13:52:32 -0700676static bool throtl_slice_used(struct throtl_grp *tg, bool rw)
Vivek Goyale43473b2010-09-15 17:06:35 -0400677{
678 if (time_in_range(jiffies, tg->slice_start[rw], tg->slice_end[rw]))
Fabian Frederick5cf8c222014-05-02 18:28:17 +0200679 return false;
Vivek Goyale43473b2010-09-15 17:06:35 -0400680
681 return 1;
682}
683
684/* Trim the used slices and adjust slice start accordingly */
Tejun Heo0f3457f2013-05-14 13:52:32 -0700685static inline void throtl_trim_slice(struct throtl_grp *tg, bool rw)
Vivek Goyale43473b2010-09-15 17:06:35 -0400686{
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200687 unsigned long nr_slices, time_elapsed, io_trim;
688 u64 bytes_trim, tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400689
690 BUG_ON(time_before(tg->slice_end[rw], tg->slice_start[rw]));
691
692 /*
693 * If bps are unlimited (-1), then time slice don't get
694 * renewed. Don't try to trim the slice if slice is used. A new
695 * slice will start when appropriate.
696 */
Tejun Heo0f3457f2013-05-14 13:52:32 -0700697 if (throtl_slice_used(tg, rw))
Vivek Goyale43473b2010-09-15 17:06:35 -0400698 return;
699
Vivek Goyald1ae8ff2010-12-01 19:34:46 +0100700 /*
701 * A bio has been dispatched. Also adjust slice_end. It might happen
702 * that initially cgroup limit was very low resulting in high
703 * slice_end, but later limit was bumped up and bio was dispached
704 * sooner, then we need to reduce slice_end. A high bogus slice_end
705 * is bad because it does not allow new slice to start.
706 */
707
Tejun Heo0f3457f2013-05-14 13:52:32 -0700708 throtl_set_slice_end(tg, rw, jiffies + throtl_slice);
Vivek Goyald1ae8ff2010-12-01 19:34:46 +0100709
Vivek Goyale43473b2010-09-15 17:06:35 -0400710 time_elapsed = jiffies - tg->slice_start[rw];
711
712 nr_slices = time_elapsed / throtl_slice;
713
714 if (!nr_slices)
715 return;
Shaohua Li9f626e32017-03-27 10:51:30 -0700716 tmp = tg_bps_limit(tg, rw) * throtl_slice * nr_slices;
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200717 do_div(tmp, HZ);
718 bytes_trim = tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400719
Shaohua Li9f626e32017-03-27 10:51:30 -0700720 io_trim = (tg_iops_limit(tg, rw) * throtl_slice * nr_slices) / HZ;
Vivek Goyale43473b2010-09-15 17:06:35 -0400721
Vivek Goyal8e89d132010-09-15 17:06:37 -0400722 if (!bytes_trim && !io_trim)
Vivek Goyale43473b2010-09-15 17:06:35 -0400723 return;
724
725 if (tg->bytes_disp[rw] >= bytes_trim)
726 tg->bytes_disp[rw] -= bytes_trim;
727 else
728 tg->bytes_disp[rw] = 0;
729
Vivek Goyal8e89d132010-09-15 17:06:37 -0400730 if (tg->io_disp[rw] >= io_trim)
731 tg->io_disp[rw] -= io_trim;
732 else
733 tg->io_disp[rw] = 0;
734
Vivek Goyale43473b2010-09-15 17:06:35 -0400735 tg->slice_start[rw] += nr_slices * throtl_slice;
736
Tejun Heofda6f272013-05-14 13:52:36 -0700737 throtl_log(&tg->service_queue,
738 "[%c] trim slice nr=%lu bytes=%llu io=%lu start=%lu end=%lu jiffies=%lu",
739 rw == READ ? 'R' : 'W', nr_slices, bytes_trim, io_trim,
740 tg->slice_start[rw], tg->slice_end[rw], jiffies);
Vivek Goyale43473b2010-09-15 17:06:35 -0400741}
742
Tejun Heo0f3457f2013-05-14 13:52:32 -0700743static bool tg_with_in_iops_limit(struct throtl_grp *tg, struct bio *bio,
744 unsigned long *wait)
Vivek Goyale43473b2010-09-15 17:06:35 -0400745{
746 bool rw = bio_data_dir(bio);
Vivek Goyal8e89d132010-09-15 17:06:37 -0400747 unsigned int io_allowed;
Vivek Goyale43473b2010-09-15 17:06:35 -0400748 unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
Vivek Goyalc49c06e2010-10-01 21:16:42 +0200749 u64 tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400750
Vivek Goyal8e89d132010-09-15 17:06:37 -0400751 jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
Vivek Goyale43473b2010-09-15 17:06:35 -0400752
Vivek Goyal8e89d132010-09-15 17:06:37 -0400753 /* Slice has just started. Consider one slice interval */
754 if (!jiffy_elapsed)
755 jiffy_elapsed_rnd = throtl_slice;
756
757 jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
758
Vivek Goyalc49c06e2010-10-01 21:16:42 +0200759 /*
760 * jiffy_elapsed_rnd should not be a big value as minimum iops can be
761 * 1 then at max jiffy elapsed should be equivalent of 1 second as we
762 * will allow dispatch after 1 second and after that slice should
763 * have been trimmed.
764 */
765
Shaohua Li9f626e32017-03-27 10:51:30 -0700766 tmp = (u64)tg_iops_limit(tg, rw) * jiffy_elapsed_rnd;
Vivek Goyalc49c06e2010-10-01 21:16:42 +0200767 do_div(tmp, HZ);
768
769 if (tmp > UINT_MAX)
770 io_allowed = UINT_MAX;
771 else
772 io_allowed = tmp;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400773
774 if (tg->io_disp[rw] + 1 <= io_allowed) {
Vivek Goyale43473b2010-09-15 17:06:35 -0400775 if (wait)
776 *wait = 0;
Fabian Frederick5cf8c222014-05-02 18:28:17 +0200777 return true;
Vivek Goyale43473b2010-09-15 17:06:35 -0400778 }
779
Vivek Goyal8e89d132010-09-15 17:06:37 -0400780 /* Calc approx time to dispatch */
Shaohua Li9f626e32017-03-27 10:51:30 -0700781 jiffy_wait = ((tg->io_disp[rw] + 1) * HZ) / tg_iops_limit(tg, rw) + 1;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400782
783 if (jiffy_wait > jiffy_elapsed)
784 jiffy_wait = jiffy_wait - jiffy_elapsed;
785 else
786 jiffy_wait = 1;
787
788 if (wait)
789 *wait = jiffy_wait;
790 return 0;
791}
792
Tejun Heo0f3457f2013-05-14 13:52:32 -0700793static bool tg_with_in_bps_limit(struct throtl_grp *tg, struct bio *bio,
794 unsigned long *wait)
Vivek Goyal8e89d132010-09-15 17:06:37 -0400795{
796 bool rw = bio_data_dir(bio);
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200797 u64 bytes_allowed, extra_bytes, tmp;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400798 unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
Vivek Goyale43473b2010-09-15 17:06:35 -0400799
800 jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
801
802 /* Slice has just started. Consider one slice interval */
803 if (!jiffy_elapsed)
804 jiffy_elapsed_rnd = throtl_slice;
805
806 jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
807
Shaohua Li9f626e32017-03-27 10:51:30 -0700808 tmp = tg_bps_limit(tg, rw) * jiffy_elapsed_rnd;
Vivek Goyal5e901a22010-10-01 21:16:38 +0200809 do_div(tmp, HZ);
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200810 bytes_allowed = tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400811
Kent Overstreet4f024f32013-10-11 15:44:27 -0700812 if (tg->bytes_disp[rw] + bio->bi_iter.bi_size <= bytes_allowed) {
Vivek Goyale43473b2010-09-15 17:06:35 -0400813 if (wait)
814 *wait = 0;
Fabian Frederick5cf8c222014-05-02 18:28:17 +0200815 return true;
Vivek Goyale43473b2010-09-15 17:06:35 -0400816 }
817
818 /* Calc approx time to dispatch */
Kent Overstreet4f024f32013-10-11 15:44:27 -0700819 extra_bytes = tg->bytes_disp[rw] + bio->bi_iter.bi_size - bytes_allowed;
Shaohua Li9f626e32017-03-27 10:51:30 -0700820 jiffy_wait = div64_u64(extra_bytes * HZ, tg_bps_limit(tg, rw));
Vivek Goyale43473b2010-09-15 17:06:35 -0400821
822 if (!jiffy_wait)
823 jiffy_wait = 1;
824
825 /*
826 * This wait time is without taking into consideration the rounding
827 * up we did. Add that time also.
828 */
829 jiffy_wait = jiffy_wait + (jiffy_elapsed_rnd - jiffy_elapsed);
Vivek Goyale43473b2010-09-15 17:06:35 -0400830 if (wait)
831 *wait = jiffy_wait;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400832 return 0;
833}
Vivek Goyale43473b2010-09-15 17:06:35 -0400834
Vivek Goyal8e89d132010-09-15 17:06:37 -0400835/*
836 * Returns whether one can dispatch a bio or not. Also returns approx number
837 * of jiffies to wait before this bio is with-in IO rate and can be dispatched
838 */
Tejun Heo0f3457f2013-05-14 13:52:32 -0700839static bool tg_may_dispatch(struct throtl_grp *tg, struct bio *bio,
840 unsigned long *wait)
Vivek Goyal8e89d132010-09-15 17:06:37 -0400841{
842 bool rw = bio_data_dir(bio);
843 unsigned long bps_wait = 0, iops_wait = 0, max_wait = 0;
844
845 /*
846 * Currently whole state machine of group depends on first bio
847 * queued in the group bio list. So one should not be calling
848 * this function with a different bio if there are other bios
849 * queued.
850 */
Tejun Heo73f0d492013-05-14 13:52:35 -0700851 BUG_ON(tg->service_queue.nr_queued[rw] &&
Tejun Heoc5cc2072013-05-14 13:52:38 -0700852 bio != throtl_peek_queued(&tg->service_queue.queued[rw]));
Vivek Goyal8e89d132010-09-15 17:06:37 -0400853
854 /* If tg->bps = -1, then BW is unlimited */
Shaohua Li9f626e32017-03-27 10:51:30 -0700855 if (tg_bps_limit(tg, rw) == U64_MAX &&
856 tg_iops_limit(tg, rw) == UINT_MAX) {
Vivek Goyal8e89d132010-09-15 17:06:37 -0400857 if (wait)
858 *wait = 0;
Fabian Frederick5cf8c222014-05-02 18:28:17 +0200859 return true;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400860 }
861
862 /*
863 * If previous slice expired, start a new one otherwise renew/extend
864 * existing slice to make sure it is at least throtl_slice interval
Vivek Goyal164c80e2016-09-19 15:12:41 -0600865 * long since now. New slice is started only for empty throttle group.
866 * If there is queued bio, that means there should be an active
867 * slice and it should be extended instead.
Vivek Goyal8e89d132010-09-15 17:06:37 -0400868 */
Vivek Goyal164c80e2016-09-19 15:12:41 -0600869 if (throtl_slice_used(tg, rw) && !(tg->service_queue.nr_queued[rw]))
Tejun Heo0f3457f2013-05-14 13:52:32 -0700870 throtl_start_new_slice(tg, rw);
Vivek Goyal8e89d132010-09-15 17:06:37 -0400871 else {
872 if (time_before(tg->slice_end[rw], jiffies + throtl_slice))
Tejun Heo0f3457f2013-05-14 13:52:32 -0700873 throtl_extend_slice(tg, rw, jiffies + throtl_slice);
Vivek Goyal8e89d132010-09-15 17:06:37 -0400874 }
875
Tejun Heo0f3457f2013-05-14 13:52:32 -0700876 if (tg_with_in_bps_limit(tg, bio, &bps_wait) &&
877 tg_with_in_iops_limit(tg, bio, &iops_wait)) {
Vivek Goyal8e89d132010-09-15 17:06:37 -0400878 if (wait)
879 *wait = 0;
880 return 1;
881 }
882
883 max_wait = max(bps_wait, iops_wait);
884
885 if (wait)
886 *wait = max_wait;
887
888 if (time_before(tg->slice_end[rw], jiffies + max_wait))
Tejun Heo0f3457f2013-05-14 13:52:32 -0700889 throtl_extend_slice(tg, rw, jiffies + max_wait);
Vivek Goyale43473b2010-09-15 17:06:35 -0400890
891 return 0;
892}
893
894static void throtl_charge_bio(struct throtl_grp *tg, struct bio *bio)
895{
896 bool rw = bio_data_dir(bio);
Vivek Goyale43473b2010-09-15 17:06:35 -0400897
898 /* Charge the bio to the group */
Kent Overstreet4f024f32013-10-11 15:44:27 -0700899 tg->bytes_disp[rw] += bio->bi_iter.bi_size;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400900 tg->io_disp[rw]++;
Vivek Goyale43473b2010-09-15 17:06:35 -0400901
Tejun Heo2a0f61e2013-05-14 13:52:36 -0700902 /*
Christoph Hellwig8d2bbd42016-10-20 15:12:12 +0200903 * BIO_THROTTLED is used to prevent the same bio to be throttled
Tejun Heo2a0f61e2013-05-14 13:52:36 -0700904 * more than once as a throttled bio will go through blk-throtl the
905 * second time when it eventually gets issued. Set it when a bio
906 * is being charged to a tg.
Tejun Heo2a0f61e2013-05-14 13:52:36 -0700907 */
Christoph Hellwig8d2bbd42016-10-20 15:12:12 +0200908 if (!bio_flagged(bio, BIO_THROTTLED))
909 bio_set_flag(bio, BIO_THROTTLED);
Vivek Goyale43473b2010-09-15 17:06:35 -0400910}
911
Tejun Heoc5cc2072013-05-14 13:52:38 -0700912/**
913 * throtl_add_bio_tg - add a bio to the specified throtl_grp
914 * @bio: bio to add
915 * @qn: qnode to use
916 * @tg: the target throtl_grp
917 *
918 * Add @bio to @tg's service_queue using @qn. If @qn is not specified,
919 * tg->qnode_on_self[] is used.
920 */
921static void throtl_add_bio_tg(struct bio *bio, struct throtl_qnode *qn,
922 struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400923{
Tejun Heo73f0d492013-05-14 13:52:35 -0700924 struct throtl_service_queue *sq = &tg->service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -0400925 bool rw = bio_data_dir(bio);
926
Tejun Heoc5cc2072013-05-14 13:52:38 -0700927 if (!qn)
928 qn = &tg->qnode_on_self[rw];
929
Tejun Heo0e9f4162013-05-14 13:52:35 -0700930 /*
931 * If @tg doesn't currently have any bios queued in the same
932 * direction, queueing @bio can change when @tg should be
933 * dispatched. Mark that @tg was empty. This is automatically
934 * cleaered on the next tg_update_disptime().
935 */
936 if (!sq->nr_queued[rw])
937 tg->flags |= THROTL_TG_WAS_EMPTY;
938
Tejun Heoc5cc2072013-05-14 13:52:38 -0700939 throtl_qnode_add_bio(bio, qn, &sq->queued[rw]);
940
Tejun Heo73f0d492013-05-14 13:52:35 -0700941 sq->nr_queued[rw]++;
Tejun Heo77216b02013-05-14 13:52:36 -0700942 throtl_enqueue_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -0400943}
944
Tejun Heo77216b02013-05-14 13:52:36 -0700945static void tg_update_disptime(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400946{
Tejun Heo73f0d492013-05-14 13:52:35 -0700947 struct throtl_service_queue *sq = &tg->service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -0400948 unsigned long read_wait = -1, write_wait = -1, min_wait = -1, disptime;
949 struct bio *bio;
950
Markus Elfringd609af32017-01-21 22:15:33 +0100951 bio = throtl_peek_queued(&sq->queued[READ]);
952 if (bio)
Tejun Heo0f3457f2013-05-14 13:52:32 -0700953 tg_may_dispatch(tg, bio, &read_wait);
Vivek Goyale43473b2010-09-15 17:06:35 -0400954
Markus Elfringd609af32017-01-21 22:15:33 +0100955 bio = throtl_peek_queued(&sq->queued[WRITE]);
956 if (bio)
Tejun Heo0f3457f2013-05-14 13:52:32 -0700957 tg_may_dispatch(tg, bio, &write_wait);
Vivek Goyale43473b2010-09-15 17:06:35 -0400958
959 min_wait = min(read_wait, write_wait);
960 disptime = jiffies + min_wait;
961
Vivek Goyale43473b2010-09-15 17:06:35 -0400962 /* Update dispatch time */
Tejun Heo77216b02013-05-14 13:52:36 -0700963 throtl_dequeue_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -0400964 tg->disptime = disptime;
Tejun Heo77216b02013-05-14 13:52:36 -0700965 throtl_enqueue_tg(tg);
Tejun Heo0e9f4162013-05-14 13:52:35 -0700966
967 /* see throtl_add_bio_tg() */
968 tg->flags &= ~THROTL_TG_WAS_EMPTY;
Vivek Goyale43473b2010-09-15 17:06:35 -0400969}
970
Vivek Goyal32ee5bc2013-05-14 13:52:38 -0700971static void start_parent_slice_with_credit(struct throtl_grp *child_tg,
972 struct throtl_grp *parent_tg, bool rw)
973{
974 if (throtl_slice_used(parent_tg, rw)) {
975 throtl_start_new_slice_with_credit(parent_tg, rw,
976 child_tg->slice_start[rw]);
977 }
978
979}
980
Tejun Heo77216b02013-05-14 13:52:36 -0700981static void tg_dispatch_one_bio(struct throtl_grp *tg, bool rw)
Vivek Goyale43473b2010-09-15 17:06:35 -0400982{
Tejun Heo73f0d492013-05-14 13:52:35 -0700983 struct throtl_service_queue *sq = &tg->service_queue;
Tejun Heo6bc9c2b2013-05-14 13:52:38 -0700984 struct throtl_service_queue *parent_sq = sq->parent_sq;
985 struct throtl_grp *parent_tg = sq_to_tg(parent_sq);
Tejun Heoc5cc2072013-05-14 13:52:38 -0700986 struct throtl_grp *tg_to_put = NULL;
Vivek Goyale43473b2010-09-15 17:06:35 -0400987 struct bio *bio;
988
Tejun Heoc5cc2072013-05-14 13:52:38 -0700989 /*
990 * @bio is being transferred from @tg to @parent_sq. Popping a bio
991 * from @tg may put its reference and @parent_sq might end up
992 * getting released prematurely. Remember the tg to put and put it
993 * after @bio is transferred to @parent_sq.
994 */
995 bio = throtl_pop_queued(&sq->queued[rw], &tg_to_put);
Tejun Heo73f0d492013-05-14 13:52:35 -0700996 sq->nr_queued[rw]--;
Vivek Goyale43473b2010-09-15 17:06:35 -0400997
998 throtl_charge_bio(tg, bio);
Tejun Heo6bc9c2b2013-05-14 13:52:38 -0700999
1000 /*
1001 * If our parent is another tg, we just need to transfer @bio to
1002 * the parent using throtl_add_bio_tg(). If our parent is
1003 * @td->service_queue, @bio is ready to be issued. Put it on its
1004 * bio_lists[] and decrease total number queued. The caller is
1005 * responsible for issuing these bios.
1006 */
1007 if (parent_tg) {
Tejun Heoc5cc2072013-05-14 13:52:38 -07001008 throtl_add_bio_tg(bio, &tg->qnode_on_parent[rw], parent_tg);
Vivek Goyal32ee5bc2013-05-14 13:52:38 -07001009 start_parent_slice_with_credit(tg, parent_tg, rw);
Tejun Heo6bc9c2b2013-05-14 13:52:38 -07001010 } else {
Tejun Heoc5cc2072013-05-14 13:52:38 -07001011 throtl_qnode_add_bio(bio, &tg->qnode_on_parent[rw],
1012 &parent_sq->queued[rw]);
Tejun Heo6bc9c2b2013-05-14 13:52:38 -07001013 BUG_ON(tg->td->nr_queued[rw] <= 0);
1014 tg->td->nr_queued[rw]--;
1015 }
Vivek Goyale43473b2010-09-15 17:06:35 -04001016
Tejun Heo0f3457f2013-05-14 13:52:32 -07001017 throtl_trim_slice(tg, rw);
Tejun Heo6bc9c2b2013-05-14 13:52:38 -07001018
Tejun Heoc5cc2072013-05-14 13:52:38 -07001019 if (tg_to_put)
1020 blkg_put(tg_to_blkg(tg_to_put));
Vivek Goyale43473b2010-09-15 17:06:35 -04001021}
1022
Tejun Heo77216b02013-05-14 13:52:36 -07001023static int throtl_dispatch_tg(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -04001024{
Tejun Heo73f0d492013-05-14 13:52:35 -07001025 struct throtl_service_queue *sq = &tg->service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -04001026 unsigned int nr_reads = 0, nr_writes = 0;
1027 unsigned int max_nr_reads = throtl_grp_quantum*3/4;
Vivek Goyalc2f68052010-11-15 19:32:42 +01001028 unsigned int max_nr_writes = throtl_grp_quantum - max_nr_reads;
Vivek Goyale43473b2010-09-15 17:06:35 -04001029 struct bio *bio;
1030
1031 /* Try to dispatch 75% READS and 25% WRITES */
1032
Tejun Heoc5cc2072013-05-14 13:52:38 -07001033 while ((bio = throtl_peek_queued(&sq->queued[READ])) &&
Tejun Heo0f3457f2013-05-14 13:52:32 -07001034 tg_may_dispatch(tg, bio, NULL)) {
Vivek Goyale43473b2010-09-15 17:06:35 -04001035
Tejun Heo77216b02013-05-14 13:52:36 -07001036 tg_dispatch_one_bio(tg, bio_data_dir(bio));
Vivek Goyale43473b2010-09-15 17:06:35 -04001037 nr_reads++;
1038
1039 if (nr_reads >= max_nr_reads)
1040 break;
1041 }
1042
Tejun Heoc5cc2072013-05-14 13:52:38 -07001043 while ((bio = throtl_peek_queued(&sq->queued[WRITE])) &&
Tejun Heo0f3457f2013-05-14 13:52:32 -07001044 tg_may_dispatch(tg, bio, NULL)) {
Vivek Goyale43473b2010-09-15 17:06:35 -04001045
Tejun Heo77216b02013-05-14 13:52:36 -07001046 tg_dispatch_one_bio(tg, bio_data_dir(bio));
Vivek Goyale43473b2010-09-15 17:06:35 -04001047 nr_writes++;
1048
1049 if (nr_writes >= max_nr_writes)
1050 break;
1051 }
1052
1053 return nr_reads + nr_writes;
1054}
1055
Tejun Heo651930b2013-05-14 13:52:35 -07001056static int throtl_select_dispatch(struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -04001057{
1058 unsigned int nr_disp = 0;
Vivek Goyale43473b2010-09-15 17:06:35 -04001059
1060 while (1) {
Tejun Heo73f0d492013-05-14 13:52:35 -07001061 struct throtl_grp *tg = throtl_rb_first(parent_sq);
1062 struct throtl_service_queue *sq = &tg->service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -04001063
1064 if (!tg)
1065 break;
1066
1067 if (time_before(jiffies, tg->disptime))
1068 break;
1069
Tejun Heo77216b02013-05-14 13:52:36 -07001070 throtl_dequeue_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -04001071
Tejun Heo77216b02013-05-14 13:52:36 -07001072 nr_disp += throtl_dispatch_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -04001073
Tejun Heo73f0d492013-05-14 13:52:35 -07001074 if (sq->nr_queued[0] || sq->nr_queued[1])
Tejun Heo77216b02013-05-14 13:52:36 -07001075 tg_update_disptime(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -04001076
1077 if (nr_disp >= throtl_quantum)
1078 break;
1079 }
1080
1081 return nr_disp;
1082}
1083
Tejun Heo6e1a5702013-05-14 13:52:37 -07001084/**
1085 * throtl_pending_timer_fn - timer function for service_queue->pending_timer
1086 * @arg: the throtl_service_queue being serviced
1087 *
1088 * This timer is armed when a child throtl_grp with active bio's become
1089 * pending and queued on the service_queue's pending_tree and expires when
1090 * the first child throtl_grp should be dispatched. This function
Tejun Heo2e48a532013-05-14 13:52:38 -07001091 * dispatches bio's from the children throtl_grps to the parent
1092 * service_queue.
1093 *
1094 * If the parent's parent is another throtl_grp, dispatching is propagated
1095 * by either arming its pending_timer or repeating dispatch directly. If
1096 * the top-level service_tree is reached, throtl_data->dispatch_work is
1097 * kicked so that the ready bio's are issued.
Tejun Heo6e1a5702013-05-14 13:52:37 -07001098 */
Tejun Heo69df0ab2013-05-14 13:52:36 -07001099static void throtl_pending_timer_fn(unsigned long arg)
1100{
1101 struct throtl_service_queue *sq = (void *)arg;
Tejun Heo2e48a532013-05-14 13:52:38 -07001102 struct throtl_grp *tg = sq_to_tg(sq);
Tejun Heo69df0ab2013-05-14 13:52:36 -07001103 struct throtl_data *td = sq_to_td(sq);
Tejun Heocb761992013-05-14 13:52:31 -07001104 struct request_queue *q = td->queue;
Tejun Heo2e48a532013-05-14 13:52:38 -07001105 struct throtl_service_queue *parent_sq;
1106 bool dispatched;
Tejun Heo6e1a5702013-05-14 13:52:37 -07001107 int ret;
Vivek Goyale43473b2010-09-15 17:06:35 -04001108
1109 spin_lock_irq(q->queue_lock);
Tejun Heo2e48a532013-05-14 13:52:38 -07001110again:
1111 parent_sq = sq->parent_sq;
1112 dispatched = false;
Vivek Goyale43473b2010-09-15 17:06:35 -04001113
Tejun Heo7f52f982013-05-14 13:52:37 -07001114 while (true) {
1115 throtl_log(sq, "dispatch nr_queued=%u read=%u write=%u",
Tejun Heo2e48a532013-05-14 13:52:38 -07001116 sq->nr_queued[READ] + sq->nr_queued[WRITE],
1117 sq->nr_queued[READ], sq->nr_queued[WRITE]);
Vivek Goyale43473b2010-09-15 17:06:35 -04001118
Tejun Heo7f52f982013-05-14 13:52:37 -07001119 ret = throtl_select_dispatch(sq);
1120 if (ret) {
Tejun Heo7f52f982013-05-14 13:52:37 -07001121 throtl_log(sq, "bios disp=%u", ret);
1122 dispatched = true;
Tejun Heo651930b2013-05-14 13:52:35 -07001123 }
Vivek Goyale43473b2010-09-15 17:06:35 -04001124
Tejun Heo7f52f982013-05-14 13:52:37 -07001125 if (throtl_schedule_next_dispatch(sq, false))
1126 break;
1127
1128 /* this dispatch windows is still open, relax and repeat */
1129 spin_unlock_irq(q->queue_lock);
1130 cpu_relax();
1131 spin_lock_irq(q->queue_lock);
1132 }
Tejun Heo6a525602013-05-14 13:52:32 -07001133
Tejun Heo2e48a532013-05-14 13:52:38 -07001134 if (!dispatched)
1135 goto out_unlock;
Tejun Heo6e1a5702013-05-14 13:52:37 -07001136
Tejun Heo2e48a532013-05-14 13:52:38 -07001137 if (parent_sq) {
1138 /* @parent_sq is another throl_grp, propagate dispatch */
1139 if (tg->flags & THROTL_TG_WAS_EMPTY) {
1140 tg_update_disptime(tg);
1141 if (!throtl_schedule_next_dispatch(parent_sq, false)) {
1142 /* window is already open, repeat dispatching */
1143 sq = parent_sq;
1144 tg = sq_to_tg(sq);
1145 goto again;
1146 }
1147 }
1148 } else {
1149 /* reached the top-level, queue issueing */
1150 queue_work(kthrotld_workqueue, &td->dispatch_work);
1151 }
1152out_unlock:
Tejun Heo6e1a5702013-05-14 13:52:37 -07001153 spin_unlock_irq(q->queue_lock);
1154}
1155
1156/**
1157 * blk_throtl_dispatch_work_fn - work function for throtl_data->dispatch_work
1158 * @work: work item being executed
1159 *
1160 * This function is queued for execution when bio's reach the bio_lists[]
1161 * of throtl_data->service_queue. Those bio's are ready and issued by this
1162 * function.
1163 */
Fabian Frederick8876e142014-04-17 21:41:16 +02001164static void blk_throtl_dispatch_work_fn(struct work_struct *work)
Tejun Heo6e1a5702013-05-14 13:52:37 -07001165{
1166 struct throtl_data *td = container_of(work, struct throtl_data,
1167 dispatch_work);
1168 struct throtl_service_queue *td_sq = &td->service_queue;
1169 struct request_queue *q = td->queue;
1170 struct bio_list bio_list_on_stack;
1171 struct bio *bio;
1172 struct blk_plug plug;
1173 int rw;
1174
1175 bio_list_init(&bio_list_on_stack);
1176
1177 spin_lock_irq(q->queue_lock);
Tejun Heoc5cc2072013-05-14 13:52:38 -07001178 for (rw = READ; rw <= WRITE; rw++)
1179 while ((bio = throtl_pop_queued(&td_sq->queued[rw], NULL)))
1180 bio_list_add(&bio_list_on_stack, bio);
Vivek Goyale43473b2010-09-15 17:06:35 -04001181 spin_unlock_irq(q->queue_lock);
1182
Tejun Heo6e1a5702013-05-14 13:52:37 -07001183 if (!bio_list_empty(&bio_list_on_stack)) {
Vivek Goyal69d60eb2011-03-09 08:27:37 +01001184 blk_start_plug(&plug);
Vivek Goyale43473b2010-09-15 17:06:35 -04001185 while((bio = bio_list_pop(&bio_list_on_stack)))
1186 generic_make_request(bio);
Vivek Goyal69d60eb2011-03-09 08:27:37 +01001187 blk_finish_plug(&plug);
Vivek Goyale43473b2010-09-15 17:06:35 -04001188 }
Vivek Goyale43473b2010-09-15 17:06:35 -04001189}
1190
Tejun Heof95a04a2012-04-16 13:57:26 -07001191static u64 tg_prfill_conf_u64(struct seq_file *sf, struct blkg_policy_data *pd,
1192 int off)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001193{
Tejun Heof95a04a2012-04-16 13:57:26 -07001194 struct throtl_grp *tg = pd_to_tg(pd);
1195 u64 v = *(u64 *)((void *)tg + off);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001196
Shaohua Li2ab54922017-03-27 10:51:29 -07001197 if (v == U64_MAX)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001198 return 0;
Tejun Heof95a04a2012-04-16 13:57:26 -07001199 return __blkg_prfill_u64(sf, pd, v);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001200}
1201
Tejun Heof95a04a2012-04-16 13:57:26 -07001202static u64 tg_prfill_conf_uint(struct seq_file *sf, struct blkg_policy_data *pd,
1203 int off)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001204{
Tejun Heof95a04a2012-04-16 13:57:26 -07001205 struct throtl_grp *tg = pd_to_tg(pd);
1206 unsigned int v = *(unsigned int *)((void *)tg + off);
Tejun Heoaf133ce2012-04-01 14:38:44 -07001207
Shaohua Li2ab54922017-03-27 10:51:29 -07001208 if (v == UINT_MAX)
Tejun Heoaf133ce2012-04-01 14:38:44 -07001209 return 0;
Tejun Heof95a04a2012-04-16 13:57:26 -07001210 return __blkg_prfill_u64(sf, pd, v);
Tejun Heoaf133ce2012-04-01 14:38:44 -07001211}
1212
Tejun Heo2da8ca82013-12-05 12:28:04 -05001213static int tg_print_conf_u64(struct seq_file *sf, void *v)
Tejun Heoaf133ce2012-04-01 14:38:44 -07001214{
Tejun Heo2da8ca82013-12-05 12:28:04 -05001215 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), tg_prfill_conf_u64,
1216 &blkcg_policy_throtl, seq_cft(sf)->private, false);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001217 return 0;
1218}
1219
Tejun Heo2da8ca82013-12-05 12:28:04 -05001220static int tg_print_conf_uint(struct seq_file *sf, void *v)
Vivek Goyale43473b2010-09-15 17:06:35 -04001221{
Tejun Heo2da8ca82013-12-05 12:28:04 -05001222 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), tg_prfill_conf_uint,
1223 &blkcg_policy_throtl, seq_cft(sf)->private, false);
Tejun Heoaf133ce2012-04-01 14:38:44 -07001224 return 0;
Vivek Goyale43473b2010-09-15 17:06:35 -04001225}
1226
Tejun Heo69948b02015-08-18 14:55:32 -07001227static void tg_conf_updated(struct throtl_grp *tg)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001228{
Tejun Heo69948b02015-08-18 14:55:32 -07001229 struct throtl_service_queue *sq = &tg->service_queue;
Tejun Heo492eb212013-08-08 20:11:25 -04001230 struct cgroup_subsys_state *pos_css;
Tejun Heo69948b02015-08-18 14:55:32 -07001231 struct blkcg_gq *blkg;
Tejun Heoaf133ce2012-04-01 14:38:44 -07001232
Tejun Heofda6f272013-05-14 13:52:36 -07001233 throtl_log(&tg->service_queue,
1234 "limit change rbps=%llu wbps=%llu riops=%u wiops=%u",
Shaohua Li9f626e32017-03-27 10:51:30 -07001235 tg_bps_limit(tg, READ), tg_bps_limit(tg, WRITE),
1236 tg_iops_limit(tg, READ), tg_iops_limit(tg, WRITE));
Tejun Heo632b4492013-05-14 13:52:31 -07001237
1238 /*
Tejun Heo693e7512013-05-14 13:52:38 -07001239 * Update has_rules[] flags for the updated tg's subtree. A tg is
1240 * considered to have rules if either the tg itself or any of its
1241 * ancestors has rules. This identifies groups without any
1242 * restrictions in the whole hierarchy and allows them to bypass
1243 * blk-throttle.
1244 */
Tejun Heo69948b02015-08-18 14:55:32 -07001245 blkg_for_each_descendant_pre(blkg, pos_css, tg_to_blkg(tg))
Tejun Heo693e7512013-05-14 13:52:38 -07001246 tg_update_has_rules(blkg_to_tg(blkg));
1247
1248 /*
Tejun Heo632b4492013-05-14 13:52:31 -07001249 * We're already holding queue_lock and know @tg is valid. Let's
1250 * apply the new config directly.
1251 *
1252 * Restart the slices for both READ and WRITES. It might happen
1253 * that a group's limit are dropped suddenly and we don't want to
1254 * account recently dispatched IO with new low rate.
1255 */
Tejun Heo0f3457f2013-05-14 13:52:32 -07001256 throtl_start_new_slice(tg, 0);
1257 throtl_start_new_slice(tg, 1);
Tejun Heo632b4492013-05-14 13:52:31 -07001258
Tejun Heo5b2c16a2013-05-14 13:52:32 -07001259 if (tg->flags & THROTL_TG_PENDING) {
Tejun Heo77216b02013-05-14 13:52:36 -07001260 tg_update_disptime(tg);
Tejun Heo7f52f982013-05-14 13:52:37 -07001261 throtl_schedule_next_dispatch(sq->parent_sq, true);
Tejun Heo632b4492013-05-14 13:52:31 -07001262 }
Tejun Heo69948b02015-08-18 14:55:32 -07001263}
Tejun Heo60c2bc22012-04-01 14:38:43 -07001264
Tejun Heo69948b02015-08-18 14:55:32 -07001265static ssize_t tg_set_conf(struct kernfs_open_file *of,
1266 char *buf, size_t nbytes, loff_t off, bool is_u64)
1267{
1268 struct blkcg *blkcg = css_to_blkcg(of_css(of));
1269 struct blkg_conf_ctx ctx;
1270 struct throtl_grp *tg;
1271 int ret;
1272 u64 v;
1273
1274 ret = blkg_conf_prep(blkcg, &blkcg_policy_throtl, buf, &ctx);
1275 if (ret)
1276 return ret;
1277
1278 ret = -EINVAL;
1279 if (sscanf(ctx.body, "%llu", &v) != 1)
1280 goto out_finish;
1281 if (!v)
Shaohua Li2ab54922017-03-27 10:51:29 -07001282 v = U64_MAX;
Tejun Heo69948b02015-08-18 14:55:32 -07001283
1284 tg = blkg_to_tg(ctx.blkg);
1285
1286 if (is_u64)
1287 *(u64 *)((void *)tg + of_cft(of)->private) = v;
1288 else
1289 *(unsigned int *)((void *)tg + of_cft(of)->private) = v;
1290
1291 tg_conf_updated(tg);
Tejun Heo36aa9e52015-08-18 14:55:31 -07001292 ret = 0;
1293out_finish:
Tejun Heo60c2bc22012-04-01 14:38:43 -07001294 blkg_conf_finish(&ctx);
Tejun Heo36aa9e52015-08-18 14:55:31 -07001295 return ret ?: nbytes;
Tejun Heo60c2bc22012-04-01 14:38:43 -07001296}
1297
Tejun Heo451af502014-05-13 12:16:21 -04001298static ssize_t tg_set_conf_u64(struct kernfs_open_file *of,
1299 char *buf, size_t nbytes, loff_t off)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001300{
Tejun Heo451af502014-05-13 12:16:21 -04001301 return tg_set_conf(of, buf, nbytes, off, true);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001302}
1303
Tejun Heo451af502014-05-13 12:16:21 -04001304static ssize_t tg_set_conf_uint(struct kernfs_open_file *of,
1305 char *buf, size_t nbytes, loff_t off)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001306{
Tejun Heo451af502014-05-13 12:16:21 -04001307 return tg_set_conf(of, buf, nbytes, off, false);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001308}
1309
Tejun Heo880f50e2015-08-18 14:55:30 -07001310static struct cftype throtl_legacy_files[] = {
Tejun Heo60c2bc22012-04-01 14:38:43 -07001311 {
1312 .name = "throttle.read_bps_device",
Shaohua Li9f626e32017-03-27 10:51:30 -07001313 .private = offsetof(struct throtl_grp, bps[READ][LIMIT_MAX]),
Tejun Heo2da8ca82013-12-05 12:28:04 -05001314 .seq_show = tg_print_conf_u64,
Tejun Heo451af502014-05-13 12:16:21 -04001315 .write = tg_set_conf_u64,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001316 },
1317 {
1318 .name = "throttle.write_bps_device",
Shaohua Li9f626e32017-03-27 10:51:30 -07001319 .private = offsetof(struct throtl_grp, bps[WRITE][LIMIT_MAX]),
Tejun Heo2da8ca82013-12-05 12:28:04 -05001320 .seq_show = tg_print_conf_u64,
Tejun Heo451af502014-05-13 12:16:21 -04001321 .write = tg_set_conf_u64,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001322 },
1323 {
1324 .name = "throttle.read_iops_device",
Shaohua Li9f626e32017-03-27 10:51:30 -07001325 .private = offsetof(struct throtl_grp, iops[READ][LIMIT_MAX]),
Tejun Heo2da8ca82013-12-05 12:28:04 -05001326 .seq_show = tg_print_conf_uint,
Tejun Heo451af502014-05-13 12:16:21 -04001327 .write = tg_set_conf_uint,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001328 },
1329 {
1330 .name = "throttle.write_iops_device",
Shaohua Li9f626e32017-03-27 10:51:30 -07001331 .private = offsetof(struct throtl_grp, iops[WRITE][LIMIT_MAX]),
Tejun Heo2da8ca82013-12-05 12:28:04 -05001332 .seq_show = tg_print_conf_uint,
Tejun Heo451af502014-05-13 12:16:21 -04001333 .write = tg_set_conf_uint,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001334 },
1335 {
1336 .name = "throttle.io_service_bytes",
Tejun Heo77ea7332015-08-18 14:55:24 -07001337 .private = (unsigned long)&blkcg_policy_throtl,
1338 .seq_show = blkg_print_stat_bytes,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001339 },
1340 {
1341 .name = "throttle.io_serviced",
Tejun Heo77ea7332015-08-18 14:55:24 -07001342 .private = (unsigned long)&blkcg_policy_throtl,
1343 .seq_show = blkg_print_stat_ios,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001344 },
1345 { } /* terminate */
1346};
1347
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001348static u64 tg_prfill_limit(struct seq_file *sf, struct blkg_policy_data *pd,
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001349 int off)
1350{
1351 struct throtl_grp *tg = pd_to_tg(pd);
1352 const char *dname = blkg_dev_name(pd->blkg);
1353 char bufs[4][21] = { "max", "max", "max", "max" };
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001354 u64 bps_dft;
1355 unsigned int iops_dft;
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001356
1357 if (!dname)
1358 return 0;
Shaohua Li9f626e32017-03-27 10:51:30 -07001359
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001360 if (off == LIMIT_LOW) {
1361 bps_dft = 0;
1362 iops_dft = 0;
1363 } else {
1364 bps_dft = U64_MAX;
1365 iops_dft = UINT_MAX;
1366 }
1367
1368 if (tg->bps_conf[READ][off] == bps_dft &&
1369 tg->bps_conf[WRITE][off] == bps_dft &&
1370 tg->iops_conf[READ][off] == iops_dft &&
1371 tg->iops_conf[WRITE][off] == iops_dft)
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001372 return 0;
1373
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001374 if (tg->bps_conf[READ][off] != bps_dft)
Shaohua Li9f626e32017-03-27 10:51:30 -07001375 snprintf(bufs[0], sizeof(bufs[0]), "%llu",
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001376 tg->bps_conf[READ][off]);
1377 if (tg->bps_conf[WRITE][off] != bps_dft)
Shaohua Li9f626e32017-03-27 10:51:30 -07001378 snprintf(bufs[1], sizeof(bufs[1]), "%llu",
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001379 tg->bps_conf[WRITE][off]);
1380 if (tg->iops_conf[READ][off] != iops_dft)
Shaohua Li9f626e32017-03-27 10:51:30 -07001381 snprintf(bufs[2], sizeof(bufs[2]), "%u",
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001382 tg->iops_conf[READ][off]);
1383 if (tg->iops_conf[WRITE][off] != iops_dft)
Shaohua Li9f626e32017-03-27 10:51:30 -07001384 snprintf(bufs[3], sizeof(bufs[3]), "%u",
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001385 tg->iops_conf[WRITE][off]);
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001386
1387 seq_printf(sf, "%s rbps=%s wbps=%s riops=%s wiops=%s\n",
1388 dname, bufs[0], bufs[1], bufs[2], bufs[3]);
1389 return 0;
1390}
1391
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001392static int tg_print_limit(struct seq_file *sf, void *v)
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001393{
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001394 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), tg_prfill_limit,
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001395 &blkcg_policy_throtl, seq_cft(sf)->private, false);
1396 return 0;
1397}
1398
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001399static ssize_t tg_set_limit(struct kernfs_open_file *of,
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001400 char *buf, size_t nbytes, loff_t off)
1401{
1402 struct blkcg *blkcg = css_to_blkcg(of_css(of));
1403 struct blkg_conf_ctx ctx;
1404 struct throtl_grp *tg;
1405 u64 v[4];
1406 int ret;
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001407 int index = of_cft(of)->private;
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001408
1409 ret = blkg_conf_prep(blkcg, &blkcg_policy_throtl, buf, &ctx);
1410 if (ret)
1411 return ret;
1412
1413 tg = blkg_to_tg(ctx.blkg);
1414
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001415 v[0] = tg->bps_conf[READ][index];
1416 v[1] = tg->bps_conf[WRITE][index];
1417 v[2] = tg->iops_conf[READ][index];
1418 v[3] = tg->iops_conf[WRITE][index];
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001419
1420 while (true) {
1421 char tok[27]; /* wiops=18446744073709551616 */
1422 char *p;
Shaohua Li2ab54922017-03-27 10:51:29 -07001423 u64 val = U64_MAX;
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001424 int len;
1425
1426 if (sscanf(ctx.body, "%26s%n", tok, &len) != 1)
1427 break;
1428 if (tok[0] == '\0')
1429 break;
1430 ctx.body += len;
1431
1432 ret = -EINVAL;
1433 p = tok;
1434 strsep(&p, "=");
1435 if (!p || (sscanf(p, "%llu", &val) != 1 && strcmp(p, "max")))
1436 goto out_finish;
1437
1438 ret = -ERANGE;
1439 if (!val)
1440 goto out_finish;
1441
1442 ret = -EINVAL;
1443 if (!strcmp(tok, "rbps"))
1444 v[0] = val;
1445 else if (!strcmp(tok, "wbps"))
1446 v[1] = val;
1447 else if (!strcmp(tok, "riops"))
1448 v[2] = min_t(u64, val, UINT_MAX);
1449 else if (!strcmp(tok, "wiops"))
1450 v[3] = min_t(u64, val, UINT_MAX);
1451 else
1452 goto out_finish;
1453 }
1454
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001455 tg->bps_conf[READ][index] = v[0];
1456 tg->bps_conf[WRITE][index] = v[1];
1457 tg->iops_conf[READ][index] = v[2];
1458 tg->iops_conf[WRITE][index] = v[3];
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001459
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001460 if (index == LIMIT_MAX) {
1461 tg->bps[READ][index] = v[0];
1462 tg->bps[WRITE][index] = v[1];
1463 tg->iops[READ][index] = v[2];
1464 tg->iops[WRITE][index] = v[3];
1465 }
1466 tg->bps[READ][LIMIT_LOW] = min(tg->bps_conf[READ][LIMIT_LOW],
1467 tg->bps_conf[READ][LIMIT_MAX]);
1468 tg->bps[WRITE][LIMIT_LOW] = min(tg->bps_conf[WRITE][LIMIT_LOW],
1469 tg->bps_conf[WRITE][LIMIT_MAX]);
1470 tg->iops[READ][LIMIT_LOW] = min(tg->iops_conf[READ][LIMIT_LOW],
1471 tg->iops_conf[READ][LIMIT_MAX]);
1472 tg->iops[WRITE][LIMIT_LOW] = min(tg->iops_conf[WRITE][LIMIT_LOW],
1473 tg->iops_conf[WRITE][LIMIT_MAX]);
1474
1475 if (index == LIMIT_LOW) {
1476 blk_throtl_update_limit_valid(tg->td);
1477 if (tg->td->limit_valid[LIMIT_LOW])
1478 tg->td->limit_index = LIMIT_LOW;
1479 }
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001480 tg_conf_updated(tg);
1481 ret = 0;
1482out_finish:
1483 blkg_conf_finish(&ctx);
1484 return ret ?: nbytes;
1485}
1486
1487static struct cftype throtl_files[] = {
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001488#ifdef CONFIG_BLK_DEV_THROTTLING_LOW
1489 {
1490 .name = "low",
1491 .flags = CFTYPE_NOT_ON_ROOT,
1492 .seq_show = tg_print_limit,
1493 .write = tg_set_limit,
1494 .private = LIMIT_LOW,
1495 },
1496#endif
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001497 {
1498 .name = "max",
1499 .flags = CFTYPE_NOT_ON_ROOT,
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001500 .seq_show = tg_print_limit,
1501 .write = tg_set_limit,
1502 .private = LIMIT_MAX,
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001503 },
1504 { } /* terminate */
1505};
1506
Vivek Goyalda527772011-03-02 19:05:33 -05001507static void throtl_shutdown_wq(struct request_queue *q)
Vivek Goyale43473b2010-09-15 17:06:35 -04001508{
1509 struct throtl_data *td = q->td;
1510
Tejun Heo69df0ab2013-05-14 13:52:36 -07001511 cancel_work_sync(&td->dispatch_work);
Vivek Goyale43473b2010-09-15 17:06:35 -04001512}
1513
Tejun Heo3c798392012-04-16 13:57:25 -07001514static struct blkcg_policy blkcg_policy_throtl = {
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001515 .dfl_cftypes = throtl_files,
Tejun Heo880f50e2015-08-18 14:55:30 -07001516 .legacy_cftypes = throtl_legacy_files,
Tejun Heof9fcc2d2012-04-16 13:57:27 -07001517
Tejun Heo001bea72015-08-18 14:55:11 -07001518 .pd_alloc_fn = throtl_pd_alloc,
Tejun Heof9fcc2d2012-04-16 13:57:27 -07001519 .pd_init_fn = throtl_pd_init,
Tejun Heo693e7512013-05-14 13:52:38 -07001520 .pd_online_fn = throtl_pd_online,
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001521 .pd_offline_fn = throtl_pd_offline,
Tejun Heo001bea72015-08-18 14:55:11 -07001522 .pd_free_fn = throtl_pd_free,
Vivek Goyale43473b2010-09-15 17:06:35 -04001523};
1524
Tejun Heoae118892015-08-18 14:55:20 -07001525bool blk_throtl_bio(struct request_queue *q, struct blkcg_gq *blkg,
1526 struct bio *bio)
Vivek Goyale43473b2010-09-15 17:06:35 -04001527{
Tejun Heoc5cc2072013-05-14 13:52:38 -07001528 struct throtl_qnode *qn = NULL;
Tejun Heoae118892015-08-18 14:55:20 -07001529 struct throtl_grp *tg = blkg_to_tg(blkg ?: q->root_blkg);
Tejun Heo73f0d492013-05-14 13:52:35 -07001530 struct throtl_service_queue *sq;
Tejun Heo0e9f4162013-05-14 13:52:35 -07001531 bool rw = bio_data_dir(bio);
Tejun Heobc16a4f2011-10-19 14:33:01 +02001532 bool throttled = false;
Vivek Goyale43473b2010-09-15 17:06:35 -04001533
Tejun Heoae118892015-08-18 14:55:20 -07001534 WARN_ON_ONCE(!rcu_read_lock_held());
1535
Tejun Heo2a0f61e2013-05-14 13:52:36 -07001536 /* see throtl_charge_bio() */
Christoph Hellwig8d2bbd42016-10-20 15:12:12 +02001537 if (bio_flagged(bio, BIO_THROTTLED) || !tg->has_rules[rw])
Tejun Heobc16a4f2011-10-19 14:33:01 +02001538 goto out;
Vivek Goyale43473b2010-09-15 17:06:35 -04001539
1540 spin_lock_irq(q->queue_lock);
Tejun Heoc9589f02015-08-18 14:55:19 -07001541
1542 if (unlikely(blk_queue_bypass(q)))
Tejun Heobc16a4f2011-10-19 14:33:01 +02001543 goto out_unlock;
Vivek Goyalf469a7b2011-05-19 15:38:23 -04001544
Tejun Heo73f0d492013-05-14 13:52:35 -07001545 sq = &tg->service_queue;
1546
Tejun Heo9e660ac2013-05-14 13:52:38 -07001547 while (true) {
1548 /* throtl is FIFO - if bios are already queued, should queue */
1549 if (sq->nr_queued[rw])
1550 break;
Vivek Goyalde701c72011-03-07 21:09:32 +01001551
Tejun Heo9e660ac2013-05-14 13:52:38 -07001552 /* if above limits, break to queue */
1553 if (!tg_may_dispatch(tg, bio, NULL))
1554 break;
1555
1556 /* within limits, let's charge and dispatch directly */
Vivek Goyale43473b2010-09-15 17:06:35 -04001557 throtl_charge_bio(tg, bio);
Vivek Goyal04521db2011-03-22 21:54:29 +01001558
1559 /*
1560 * We need to trim slice even when bios are not being queued
1561 * otherwise it might happen that a bio is not queued for
1562 * a long time and slice keeps on extending and trim is not
1563 * called for a long time. Now if limits are reduced suddenly
1564 * we take into account all the IO dispatched so far at new
1565 * low rate and * newly queued IO gets a really long dispatch
1566 * time.
1567 *
1568 * So keep on trimming slice even if bio is not queued.
1569 */
Tejun Heo0f3457f2013-05-14 13:52:32 -07001570 throtl_trim_slice(tg, rw);
Tejun Heo9e660ac2013-05-14 13:52:38 -07001571
1572 /*
1573 * @bio passed through this layer without being throttled.
1574 * Climb up the ladder. If we''re already at the top, it
1575 * can be executed directly.
1576 */
Tejun Heoc5cc2072013-05-14 13:52:38 -07001577 qn = &tg->qnode_on_parent[rw];
Tejun Heo9e660ac2013-05-14 13:52:38 -07001578 sq = sq->parent_sq;
1579 tg = sq_to_tg(sq);
1580 if (!tg)
1581 goto out_unlock;
Vivek Goyale43473b2010-09-15 17:06:35 -04001582 }
1583
Tejun Heo9e660ac2013-05-14 13:52:38 -07001584 /* out-of-limit, queue to @tg */
Tejun Heofda6f272013-05-14 13:52:36 -07001585 throtl_log(sq, "[%c] bio. bdisp=%llu sz=%u bps=%llu iodisp=%u iops=%u queued=%d/%d",
1586 rw == READ ? 'R' : 'W',
Shaohua Li9f626e32017-03-27 10:51:30 -07001587 tg->bytes_disp[rw], bio->bi_iter.bi_size,
1588 tg_bps_limit(tg, rw),
1589 tg->io_disp[rw], tg_iops_limit(tg, rw),
Tejun Heofda6f272013-05-14 13:52:36 -07001590 sq->nr_queued[READ], sq->nr_queued[WRITE]);
Vivek Goyale43473b2010-09-15 17:06:35 -04001591
Tejun Heo671058f2012-03-05 13:15:29 -08001592 bio_associate_current(bio);
Tejun Heo6bc9c2b2013-05-14 13:52:38 -07001593 tg->td->nr_queued[rw]++;
Tejun Heoc5cc2072013-05-14 13:52:38 -07001594 throtl_add_bio_tg(bio, qn, tg);
Tejun Heobc16a4f2011-10-19 14:33:01 +02001595 throttled = true;
Vivek Goyale43473b2010-09-15 17:06:35 -04001596
Tejun Heo7f52f982013-05-14 13:52:37 -07001597 /*
1598 * Update @tg's dispatch time and force schedule dispatch if @tg
1599 * was empty before @bio. The forced scheduling isn't likely to
1600 * cause undue delay as @bio is likely to be dispatched directly if
1601 * its @tg's disptime is not in the future.
1602 */
Tejun Heo0e9f4162013-05-14 13:52:35 -07001603 if (tg->flags & THROTL_TG_WAS_EMPTY) {
Tejun Heo77216b02013-05-14 13:52:36 -07001604 tg_update_disptime(tg);
Tejun Heo7f52f982013-05-14 13:52:37 -07001605 throtl_schedule_next_dispatch(tg->service_queue.parent_sq, true);
Vivek Goyale43473b2010-09-15 17:06:35 -04001606 }
1607
Tejun Heobc16a4f2011-10-19 14:33:01 +02001608out_unlock:
Vivek Goyale43473b2010-09-15 17:06:35 -04001609 spin_unlock_irq(q->queue_lock);
Tejun Heobc16a4f2011-10-19 14:33:01 +02001610out:
Tejun Heo2a0f61e2013-05-14 13:52:36 -07001611 /*
1612 * As multiple blk-throtls may stack in the same issue path, we
1613 * don't want bios to leave with the flag set. Clear the flag if
1614 * being issued.
1615 */
1616 if (!throttled)
Christoph Hellwig8d2bbd42016-10-20 15:12:12 +02001617 bio_clear_flag(bio, BIO_THROTTLED);
Tejun Heobc16a4f2011-10-19 14:33:01 +02001618 return throttled;
Vivek Goyale43473b2010-09-15 17:06:35 -04001619}
1620
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001621/*
1622 * Dispatch all bios from all children tg's queued on @parent_sq. On
1623 * return, @parent_sq is guaranteed to not have any active children tg's
1624 * and all bios from previously active tg's are on @parent_sq->bio_lists[].
1625 */
1626static void tg_drain_bios(struct throtl_service_queue *parent_sq)
1627{
1628 struct throtl_grp *tg;
1629
1630 while ((tg = throtl_rb_first(parent_sq))) {
1631 struct throtl_service_queue *sq = &tg->service_queue;
1632 struct bio *bio;
1633
1634 throtl_dequeue_tg(tg);
1635
Tejun Heoc5cc2072013-05-14 13:52:38 -07001636 while ((bio = throtl_peek_queued(&sq->queued[READ])))
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001637 tg_dispatch_one_bio(tg, bio_data_dir(bio));
Tejun Heoc5cc2072013-05-14 13:52:38 -07001638 while ((bio = throtl_peek_queued(&sq->queued[WRITE])))
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001639 tg_dispatch_one_bio(tg, bio_data_dir(bio));
1640 }
1641}
1642
Tejun Heoc9a929d2011-10-19 14:42:16 +02001643/**
1644 * blk_throtl_drain - drain throttled bios
1645 * @q: request_queue to drain throttled bios for
1646 *
1647 * Dispatch all currently throttled bios on @q through ->make_request_fn().
1648 */
1649void blk_throtl_drain(struct request_queue *q)
1650 __releases(q->queue_lock) __acquires(q->queue_lock)
1651{
1652 struct throtl_data *td = q->td;
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001653 struct blkcg_gq *blkg;
Tejun Heo492eb212013-08-08 20:11:25 -04001654 struct cgroup_subsys_state *pos_css;
Tejun Heoc9a929d2011-10-19 14:42:16 +02001655 struct bio *bio;
Tejun Heo651930b2013-05-14 13:52:35 -07001656 int rw;
Tejun Heoc9a929d2011-10-19 14:42:16 +02001657
Andi Kleen8bcb6c72012-03-30 12:33:28 +02001658 queue_lockdep_assert_held(q);
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001659 rcu_read_lock();
Tejun Heoc9a929d2011-10-19 14:42:16 +02001660
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001661 /*
1662 * Drain each tg while doing post-order walk on the blkg tree, so
1663 * that all bios are propagated to td->service_queue. It'd be
1664 * better to walk service_queue tree directly but blkg walk is
1665 * easier.
1666 */
Tejun Heo492eb212013-08-08 20:11:25 -04001667 blkg_for_each_descendant_post(blkg, pos_css, td->queue->root_blkg)
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001668 tg_drain_bios(&blkg_to_tg(blkg)->service_queue);
Tejun Heo73f0d492013-05-14 13:52:35 -07001669
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001670 /* finally, transfer bios from top-level tg's into the td */
1671 tg_drain_bios(&td->service_queue);
1672
1673 rcu_read_unlock();
Tejun Heoc9a929d2011-10-19 14:42:16 +02001674 spin_unlock_irq(q->queue_lock);
1675
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001676 /* all bios now should be in td->service_queue, issue them */
Tejun Heo651930b2013-05-14 13:52:35 -07001677 for (rw = READ; rw <= WRITE; rw++)
Tejun Heoc5cc2072013-05-14 13:52:38 -07001678 while ((bio = throtl_pop_queued(&td->service_queue.queued[rw],
1679 NULL)))
Tejun Heo651930b2013-05-14 13:52:35 -07001680 generic_make_request(bio);
Tejun Heoc9a929d2011-10-19 14:42:16 +02001681
1682 spin_lock_irq(q->queue_lock);
1683}
1684
Vivek Goyale43473b2010-09-15 17:06:35 -04001685int blk_throtl_init(struct request_queue *q)
1686{
1687 struct throtl_data *td;
Tejun Heoa2b16932012-04-13 13:11:33 -07001688 int ret;
Vivek Goyale43473b2010-09-15 17:06:35 -04001689
1690 td = kzalloc_node(sizeof(*td), GFP_KERNEL, q->node);
1691 if (!td)
1692 return -ENOMEM;
1693
Tejun Heo69df0ab2013-05-14 13:52:36 -07001694 INIT_WORK(&td->dispatch_work, blk_throtl_dispatch_work_fn);
Tejun Heob2ce2642015-08-18 14:55:13 -07001695 throtl_service_queue_init(&td->service_queue);
Vivek Goyale43473b2010-09-15 17:06:35 -04001696
Tejun Heocd1604f2012-03-05 13:15:06 -08001697 q->td = td;
Vivek Goyal29b12582011-05-19 15:38:24 -04001698 td->queue = q;
Vivek Goyal02977e42010-10-01 14:49:48 +02001699
Shaohua Li9f626e32017-03-27 10:51:30 -07001700 td->limit_valid[LIMIT_MAX] = true;
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001701 td->limit_index = LIMIT_MAX;
Tejun Heoa2b16932012-04-13 13:11:33 -07001702 /* activate policy */
Tejun Heo3c798392012-04-16 13:57:25 -07001703 ret = blkcg_activate_policy(q, &blkcg_policy_throtl);
Tejun Heoa2b16932012-04-13 13:11:33 -07001704 if (ret)
Vivek Goyal29b12582011-05-19 15:38:24 -04001705 kfree(td);
Tejun Heoa2b16932012-04-13 13:11:33 -07001706 return ret;
Vivek Goyale43473b2010-09-15 17:06:35 -04001707}
1708
1709void blk_throtl_exit(struct request_queue *q)
1710{
Tejun Heoc875f4d2012-03-05 13:15:22 -08001711 BUG_ON(!q->td);
Vivek Goyalda527772011-03-02 19:05:33 -05001712 throtl_shutdown_wq(q);
Tejun Heo3c798392012-04-16 13:57:25 -07001713 blkcg_deactivate_policy(q, &blkcg_policy_throtl);
Tejun Heoc9a929d2011-10-19 14:42:16 +02001714 kfree(q->td);
Vivek Goyale43473b2010-09-15 17:06:35 -04001715}
1716
1717static int __init throtl_init(void)
1718{
Vivek Goyal450adcb2011-03-01 13:40:54 -05001719 kthrotld_workqueue = alloc_workqueue("kthrotld", WQ_MEM_RECLAIM, 0);
1720 if (!kthrotld_workqueue)
1721 panic("Failed to create kthrotld\n");
1722
Tejun Heo3c798392012-04-16 13:57:25 -07001723 return blkcg_policy_register(&blkcg_policy_throtl);
Vivek Goyale43473b2010-09-15 17:06:35 -04001724}
1725
1726module_init(throtl_init);