blob: 93841da808a1b7746c450cec62a978cf9bf8fbce [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 */
Shaohua Li297e3d82017-03-27 10:51:37 -070022#define DFL_THROTL_SLICE (HZ / 10)
23#define MAX_THROTL_SLICE (HZ)
Vivek Goyale43473b2010-09-15 17:06:35 -040024
Tejun Heo3c798392012-04-16 13:57:25 -070025static struct blkcg_policy blkcg_policy_throtl;
Tejun Heo03814112012-03-05 13:15:14 -080026
Vivek Goyal450adcb2011-03-01 13:40:54 -050027/* A workqueue to queue throttle related work */
28static struct workqueue_struct *kthrotld_workqueue;
Vivek Goyal450adcb2011-03-01 13:40:54 -050029
Tejun Heoc5cc2072013-05-14 13:52:38 -070030/*
31 * To implement hierarchical throttling, throtl_grps form a tree and bios
32 * are dispatched upwards level by level until they reach the top and get
33 * issued. When dispatching bios from the children and local group at each
34 * level, if the bios are dispatched into a single bio_list, there's a risk
35 * of a local or child group which can queue many bios at once filling up
36 * the list starving others.
37 *
38 * To avoid such starvation, dispatched bios are queued separately
39 * according to where they came from. When they are again dispatched to
40 * the parent, they're popped in round-robin order so that no single source
41 * hogs the dispatch window.
42 *
43 * throtl_qnode is used to keep the queued bios separated by their sources.
44 * Bios are queued to throtl_qnode which in turn is queued to
45 * throtl_service_queue and then dispatched in round-robin order.
46 *
47 * It's also used to track the reference counts on blkg's. A qnode always
48 * belongs to a throtl_grp and gets queued on itself or the parent, so
49 * incrementing the reference of the associated throtl_grp when a qnode is
50 * queued and decrementing when dequeued is enough to keep the whole blkg
51 * tree pinned while bios are in flight.
52 */
53struct throtl_qnode {
54 struct list_head node; /* service_queue->queued[] */
55 struct bio_list bios; /* queued bios */
56 struct throtl_grp *tg; /* tg this qnode belongs to */
57};
58
Tejun Heoc9e03322013-05-14 13:52:32 -070059struct throtl_service_queue {
Tejun Heo77216b02013-05-14 13:52:36 -070060 struct throtl_service_queue *parent_sq; /* the parent service_queue */
61
Tejun Heo73f0d492013-05-14 13:52:35 -070062 /*
63 * Bios queued directly to this service_queue or dispatched from
64 * children throtl_grp's.
65 */
Tejun Heoc5cc2072013-05-14 13:52:38 -070066 struct list_head queued[2]; /* throtl_qnode [READ/WRITE] */
Tejun Heo73f0d492013-05-14 13:52:35 -070067 unsigned int nr_queued[2]; /* number of queued bios */
68
69 /*
70 * RB tree of active children throtl_grp's, which are sorted by
71 * their ->disptime.
72 */
Tejun Heoc9e03322013-05-14 13:52:32 -070073 struct rb_root pending_tree; /* RB tree of active tgs */
74 struct rb_node *first_pending; /* first node in the tree */
75 unsigned int nr_pending; /* # queued in the tree */
76 unsigned long first_pending_disptime; /* disptime of the first tg */
Tejun Heo69df0ab2013-05-14 13:52:36 -070077 struct timer_list pending_timer; /* fires on first_pending_disptime */
Vivek Goyale43473b2010-09-15 17:06:35 -040078};
79
Tejun Heo5b2c16a2013-05-14 13:52:32 -070080enum tg_state_flags {
81 THROTL_TG_PENDING = 1 << 0, /* on parent's pending tree */
Tejun Heo0e9f4162013-05-14 13:52:35 -070082 THROTL_TG_WAS_EMPTY = 1 << 1, /* bio_lists[] became non-empty */
Tejun Heo5b2c16a2013-05-14 13:52:32 -070083};
84
Vivek Goyale43473b2010-09-15 17:06:35 -040085#define rb_entry_tg(node) rb_entry((node), struct throtl_grp, rb_node)
86
Shaohua Li9f626e32017-03-27 10:51:30 -070087enum {
Shaohua Licd5ab1b2017-03-27 10:51:32 -070088 LIMIT_LOW,
Shaohua Li9f626e32017-03-27 10:51:30 -070089 LIMIT_MAX,
90 LIMIT_CNT,
91};
92
Vivek Goyale43473b2010-09-15 17:06:35 -040093struct throtl_grp {
Tejun Heof95a04a2012-04-16 13:57:26 -070094 /* must be the first member */
95 struct blkg_policy_data pd;
96
Tejun Heoc9e03322013-05-14 13:52:32 -070097 /* active throtl group service_queue member */
Vivek Goyale43473b2010-09-15 17:06:35 -040098 struct rb_node rb_node;
99
Tejun Heo0f3457f2013-05-14 13:52:32 -0700100 /* throtl_data this group belongs to */
101 struct throtl_data *td;
102
Tejun Heo49a2f1e2013-05-14 13:52:34 -0700103 /* this group's service queue */
104 struct throtl_service_queue service_queue;
105
Vivek Goyale43473b2010-09-15 17:06:35 -0400106 /*
Tejun Heoc5cc2072013-05-14 13:52:38 -0700107 * qnode_on_self is used when bios are directly queued to this
108 * throtl_grp so that local bios compete fairly with bios
109 * dispatched from children. qnode_on_parent is used when bios are
110 * dispatched from this throtl_grp into its parent and will compete
111 * with the sibling qnode_on_parents and the parent's
112 * qnode_on_self.
113 */
114 struct throtl_qnode qnode_on_self[2];
115 struct throtl_qnode qnode_on_parent[2];
116
117 /*
Vivek Goyale43473b2010-09-15 17:06:35 -0400118 * Dispatch time in jiffies. This is the estimated time when group
119 * will unthrottle and is ready to dispatch more bio. It is used as
120 * key to sort active groups in service tree.
121 */
122 unsigned long disptime;
123
Vivek Goyale43473b2010-09-15 17:06:35 -0400124 unsigned int flags;
125
Tejun Heo693e7512013-05-14 13:52:38 -0700126 /* are there any throtl rules between this group and td? */
127 bool has_rules[2];
128
Shaohua Licd5ab1b2017-03-27 10:51:32 -0700129 /* internally used bytes per second rate limits */
Shaohua Li9f626e32017-03-27 10:51:30 -0700130 uint64_t bps[2][LIMIT_CNT];
Shaohua Licd5ab1b2017-03-27 10:51:32 -0700131 /* user configured bps limits */
132 uint64_t bps_conf[2][LIMIT_CNT];
Vivek Goyale43473b2010-09-15 17:06:35 -0400133
Shaohua Licd5ab1b2017-03-27 10:51:32 -0700134 /* internally used IOPS limits */
Shaohua Li9f626e32017-03-27 10:51:30 -0700135 unsigned int iops[2][LIMIT_CNT];
Shaohua Licd5ab1b2017-03-27 10:51:32 -0700136 /* user configured IOPS limits */
137 unsigned int iops_conf[2][LIMIT_CNT];
Vivek Goyal8e89d132010-09-15 17:06:37 -0400138
Vivek Goyale43473b2010-09-15 17:06:35 -0400139 /* Number of bytes disptached in current slice */
140 uint64_t bytes_disp[2];
Vivek Goyal8e89d132010-09-15 17:06:37 -0400141 /* Number of bio's dispatched in current slice */
142 unsigned int io_disp[2];
Vivek Goyale43473b2010-09-15 17:06:35 -0400143
Shaohua Li3f0abd82017-03-27 10:51:35 -0700144 unsigned long last_low_overflow_time[2];
145
146 uint64_t last_bytes_disp[2];
147 unsigned int last_io_disp[2];
148
149 unsigned long last_check_time;
150
Vivek Goyale43473b2010-09-15 17:06:35 -0400151 /* When did we start a new slice */
152 unsigned long slice_start[2];
153 unsigned long slice_end[2];
154};
155
156struct throtl_data
157{
Vivek Goyale43473b2010-09-15 17:06:35 -0400158 /* service tree for active throtl groups */
Tejun Heoc9e03322013-05-14 13:52:32 -0700159 struct throtl_service_queue service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -0400160
Vivek Goyale43473b2010-09-15 17:06:35 -0400161 struct request_queue *queue;
162
163 /* Total Number of queued bios on READ and WRITE lists */
164 unsigned int nr_queued[2];
165
Shaohua Li297e3d82017-03-27 10:51:37 -0700166 unsigned int throtl_slice;
167
Vivek Goyale43473b2010-09-15 17:06:35 -0400168 /* Work for dispatching throttled bios */
Tejun Heo69df0ab2013-05-14 13:52:36 -0700169 struct work_struct dispatch_work;
Shaohua Li9f626e32017-03-27 10:51:30 -0700170 unsigned int limit_index;
171 bool limit_valid[LIMIT_CNT];
Shaohua Li3f0abd82017-03-27 10:51:35 -0700172
173 unsigned long low_upgrade_time;
174 unsigned long low_downgrade_time;
Vivek Goyale43473b2010-09-15 17:06:35 -0400175};
176
Tejun Heo69df0ab2013-05-14 13:52:36 -0700177static void throtl_pending_timer_fn(unsigned long arg);
178
Tejun Heof95a04a2012-04-16 13:57:26 -0700179static inline struct throtl_grp *pd_to_tg(struct blkg_policy_data *pd)
180{
181 return pd ? container_of(pd, struct throtl_grp, pd) : NULL;
182}
183
Tejun Heo3c798392012-04-16 13:57:25 -0700184static inline struct throtl_grp *blkg_to_tg(struct blkcg_gq *blkg)
Tejun Heo03814112012-03-05 13:15:14 -0800185{
Tejun Heof95a04a2012-04-16 13:57:26 -0700186 return pd_to_tg(blkg_to_pd(blkg, &blkcg_policy_throtl));
Tejun Heo03814112012-03-05 13:15:14 -0800187}
188
Tejun Heo3c798392012-04-16 13:57:25 -0700189static inline struct blkcg_gq *tg_to_blkg(struct throtl_grp *tg)
Tejun Heo03814112012-03-05 13:15:14 -0800190{
Tejun Heof95a04a2012-04-16 13:57:26 -0700191 return pd_to_blkg(&tg->pd);
Tejun Heo03814112012-03-05 13:15:14 -0800192}
193
Tejun Heofda6f272013-05-14 13:52:36 -0700194/**
195 * sq_to_tg - return the throl_grp the specified service queue belongs to
196 * @sq: the throtl_service_queue of interest
197 *
198 * Return the throtl_grp @sq belongs to. If @sq is the top-level one
199 * embedded in throtl_data, %NULL is returned.
200 */
201static struct throtl_grp *sq_to_tg(struct throtl_service_queue *sq)
202{
203 if (sq && sq->parent_sq)
204 return container_of(sq, struct throtl_grp, service_queue);
205 else
206 return NULL;
207}
Vivek Goyale43473b2010-09-15 17:06:35 -0400208
Tejun Heofda6f272013-05-14 13:52:36 -0700209/**
210 * sq_to_td - return throtl_data the specified service queue belongs to
211 * @sq: the throtl_service_queue of interest
212 *
Masahiro Yamadab43daed2017-02-27 14:29:09 -0800213 * A service_queue can be embedded in either a throtl_grp or throtl_data.
Tejun Heofda6f272013-05-14 13:52:36 -0700214 * Determine the associated throtl_data accordingly and return it.
215 */
216static struct throtl_data *sq_to_td(struct throtl_service_queue *sq)
217{
218 struct throtl_grp *tg = sq_to_tg(sq);
219
220 if (tg)
221 return tg->td;
222 else
223 return container_of(sq, struct throtl_data, service_queue);
224}
225
Shaohua Li9f626e32017-03-27 10:51:30 -0700226static uint64_t tg_bps_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 uint64_t ret;
230
231 if (cgroup_subsys_on_dfl(io_cgrp_subsys) && !blkg->parent)
232 return U64_MAX;
233 ret = tg->bps[rw][tg->td->limit_index];
234 if (ret == 0 && tg->td->limit_index == LIMIT_LOW)
235 return tg->bps[rw][LIMIT_MAX];
236 return ret;
Shaohua Li9f626e32017-03-27 10:51:30 -0700237}
238
239static unsigned int tg_iops_limit(struct throtl_grp *tg, int rw)
240{
Shaohua Lib22c4172017-03-27 10:51:33 -0700241 struct blkcg_gq *blkg = tg_to_blkg(tg);
242 unsigned int ret;
243
244 if (cgroup_subsys_on_dfl(io_cgrp_subsys) && !blkg->parent)
245 return UINT_MAX;
246 ret = tg->iops[rw][tg->td->limit_index];
247 if (ret == 0 && tg->td->limit_index == LIMIT_LOW)
248 return tg->iops[rw][LIMIT_MAX];
249 return ret;
Shaohua Li9f626e32017-03-27 10:51:30 -0700250}
251
Tejun Heofda6f272013-05-14 13:52:36 -0700252/**
253 * throtl_log - log debug message via blktrace
254 * @sq: the service_queue being reported
255 * @fmt: printf format string
256 * @args: printf args
257 *
258 * The messages are prefixed with "throtl BLKG_NAME" if @sq belongs to a
259 * throtl_grp; otherwise, just "throtl".
Tejun Heofda6f272013-05-14 13:52:36 -0700260 */
261#define throtl_log(sq, fmt, args...) do { \
262 struct throtl_grp *__tg = sq_to_tg((sq)); \
263 struct throtl_data *__td = sq_to_td((sq)); \
264 \
265 (void)__td; \
Shaohua Li59fa0222016-05-09 17:22:15 -0700266 if (likely(!blk_trace_note_message_enabled(__td->queue))) \
267 break; \
Tejun Heofda6f272013-05-14 13:52:36 -0700268 if ((__tg)) { \
269 char __pbuf[128]; \
270 \
271 blkg_path(tg_to_blkg(__tg), __pbuf, sizeof(__pbuf)); \
272 blk_add_trace_msg(__td->queue, "throtl %s " fmt, __pbuf, ##args); \
273 } else { \
274 blk_add_trace_msg(__td->queue, "throtl " fmt, ##args); \
275 } \
276} while (0)
Vivek Goyale43473b2010-09-15 17:06:35 -0400277
Tejun Heoc5cc2072013-05-14 13:52:38 -0700278static void throtl_qnode_init(struct throtl_qnode *qn, struct throtl_grp *tg)
279{
280 INIT_LIST_HEAD(&qn->node);
281 bio_list_init(&qn->bios);
282 qn->tg = tg;
283}
284
285/**
286 * throtl_qnode_add_bio - add a bio to a throtl_qnode and activate it
287 * @bio: bio being added
288 * @qn: qnode to add bio to
289 * @queued: the service_queue->queued[] list @qn belongs to
290 *
291 * Add @bio to @qn and put @qn on @queued if it's not already on.
292 * @qn->tg's reference count is bumped when @qn is activated. See the
293 * comment on top of throtl_qnode definition for details.
294 */
295static void throtl_qnode_add_bio(struct bio *bio, struct throtl_qnode *qn,
296 struct list_head *queued)
297{
298 bio_list_add(&qn->bios, bio);
299 if (list_empty(&qn->node)) {
300 list_add_tail(&qn->node, queued);
301 blkg_get(tg_to_blkg(qn->tg));
302 }
303}
304
305/**
306 * throtl_peek_queued - peek the first bio on a qnode list
307 * @queued: the qnode list to peek
308 */
309static struct bio *throtl_peek_queued(struct list_head *queued)
310{
311 struct throtl_qnode *qn = list_first_entry(queued, struct throtl_qnode, node);
312 struct bio *bio;
313
314 if (list_empty(queued))
315 return NULL;
316
317 bio = bio_list_peek(&qn->bios);
318 WARN_ON_ONCE(!bio);
319 return bio;
320}
321
322/**
323 * throtl_pop_queued - pop the first bio form a qnode list
324 * @queued: the qnode list to pop a bio from
325 * @tg_to_put: optional out argument for throtl_grp to put
326 *
327 * Pop the first bio from the qnode list @queued. After popping, the first
328 * qnode is removed from @queued if empty or moved to the end of @queued so
329 * that the popping order is round-robin.
330 *
331 * When the first qnode is removed, its associated throtl_grp should be put
332 * too. If @tg_to_put is NULL, this function automatically puts it;
333 * otherwise, *@tg_to_put is set to the throtl_grp to put and the caller is
334 * responsible for putting it.
335 */
336static struct bio *throtl_pop_queued(struct list_head *queued,
337 struct throtl_grp **tg_to_put)
338{
339 struct throtl_qnode *qn = list_first_entry(queued, struct throtl_qnode, node);
340 struct bio *bio;
341
342 if (list_empty(queued))
343 return NULL;
344
345 bio = bio_list_pop(&qn->bios);
346 WARN_ON_ONCE(!bio);
347
348 if (bio_list_empty(&qn->bios)) {
349 list_del_init(&qn->node);
350 if (tg_to_put)
351 *tg_to_put = qn->tg;
352 else
353 blkg_put(tg_to_blkg(qn->tg));
354 } else {
355 list_move_tail(&qn->node, queued);
356 }
357
358 return bio;
359}
360
Tejun Heo49a2f1e2013-05-14 13:52:34 -0700361/* init a service_queue, assumes the caller zeroed it */
Tejun Heob2ce2642015-08-18 14:55:13 -0700362static void throtl_service_queue_init(struct throtl_service_queue *sq)
Tejun Heo49a2f1e2013-05-14 13:52:34 -0700363{
Tejun Heoc5cc2072013-05-14 13:52:38 -0700364 INIT_LIST_HEAD(&sq->queued[0]);
365 INIT_LIST_HEAD(&sq->queued[1]);
Tejun Heo49a2f1e2013-05-14 13:52:34 -0700366 sq->pending_tree = RB_ROOT;
Tejun Heo69df0ab2013-05-14 13:52:36 -0700367 setup_timer(&sq->pending_timer, throtl_pending_timer_fn,
368 (unsigned long)sq);
369}
370
Tejun Heo001bea72015-08-18 14:55:11 -0700371static struct blkg_policy_data *throtl_pd_alloc(gfp_t gfp, int node)
372{
Tejun Heo4fb72032015-08-18 14:55:12 -0700373 struct throtl_grp *tg;
Tejun Heo24bdb8e2015-08-18 14:55:22 -0700374 int rw;
Tejun Heo4fb72032015-08-18 14:55:12 -0700375
376 tg = kzalloc_node(sizeof(*tg), gfp, node);
377 if (!tg)
Tejun Heo77ea7332015-08-18 14:55:24 -0700378 return NULL;
Tejun Heo4fb72032015-08-18 14:55:12 -0700379
Tejun Heob2ce2642015-08-18 14:55:13 -0700380 throtl_service_queue_init(&tg->service_queue);
381
382 for (rw = READ; rw <= WRITE; rw++) {
383 throtl_qnode_init(&tg->qnode_on_self[rw], tg);
384 throtl_qnode_init(&tg->qnode_on_parent[rw], tg);
385 }
386
387 RB_CLEAR_NODE(&tg->rb_node);
Shaohua Li9f626e32017-03-27 10:51:30 -0700388 tg->bps[READ][LIMIT_MAX] = U64_MAX;
389 tg->bps[WRITE][LIMIT_MAX] = U64_MAX;
390 tg->iops[READ][LIMIT_MAX] = UINT_MAX;
391 tg->iops[WRITE][LIMIT_MAX] = UINT_MAX;
Shaohua Licd5ab1b2017-03-27 10:51:32 -0700392 tg->bps_conf[READ][LIMIT_MAX] = U64_MAX;
393 tg->bps_conf[WRITE][LIMIT_MAX] = U64_MAX;
394 tg->iops_conf[READ][LIMIT_MAX] = UINT_MAX;
395 tg->iops_conf[WRITE][LIMIT_MAX] = UINT_MAX;
396 /* LIMIT_LOW will have default value 0 */
Tejun Heob2ce2642015-08-18 14:55:13 -0700397
Tejun Heo4fb72032015-08-18 14:55:12 -0700398 return &tg->pd;
Tejun Heo001bea72015-08-18 14:55:11 -0700399}
400
Tejun Heoa9520cd2015-08-18 14:55:14 -0700401static void throtl_pd_init(struct blkg_policy_data *pd)
Vivek Goyala29a1712011-05-19 15:38:19 -0400402{
Tejun Heoa9520cd2015-08-18 14:55:14 -0700403 struct throtl_grp *tg = pd_to_tg(pd);
404 struct blkcg_gq *blkg = tg_to_blkg(tg);
Tejun Heo77216b02013-05-14 13:52:36 -0700405 struct throtl_data *td = blkg->q->td;
Tejun Heob2ce2642015-08-18 14:55:13 -0700406 struct throtl_service_queue *sq = &tg->service_queue;
Tejun Heocd1604f2012-03-05 13:15:06 -0800407
Tejun Heo91381252013-05-14 13:52:38 -0700408 /*
Tejun Heoaa6ec292014-07-09 10:08:08 -0400409 * If on the default hierarchy, we switch to properly hierarchical
Tejun Heo91381252013-05-14 13:52:38 -0700410 * behavior where limits on a given throtl_grp are applied to the
411 * whole subtree rather than just the group itself. e.g. If 16M
412 * read_bps limit is set on the root group, the whole system can't
413 * exceed 16M for the device.
414 *
Tejun Heoaa6ec292014-07-09 10:08:08 -0400415 * If not on the default hierarchy, the broken flat hierarchy
Tejun Heo91381252013-05-14 13:52:38 -0700416 * behavior is retained where all throtl_grps are treated as if
417 * they're all separate root groups right below throtl_data.
418 * Limits of a group don't interact with limits of other groups
419 * regardless of the position of the group in the hierarchy.
420 */
Tejun Heob2ce2642015-08-18 14:55:13 -0700421 sq->parent_sq = &td->service_queue;
Tejun Heo9e10a132015-09-18 11:56:28 -0400422 if (cgroup_subsys_on_dfl(io_cgrp_subsys) && blkg->parent)
Tejun Heob2ce2642015-08-18 14:55:13 -0700423 sq->parent_sq = &blkg_to_tg(blkg->parent)->service_queue;
Tejun Heo77216b02013-05-14 13:52:36 -0700424 tg->td = td;
Tejun Heo8a3d2612012-04-01 14:38:44 -0700425}
426
Tejun Heo693e7512013-05-14 13:52:38 -0700427/*
428 * Set has_rules[] if @tg or any of its parents have limits configured.
429 * This doesn't require walking up to the top of the hierarchy as the
430 * parent's has_rules[] is guaranteed to be correct.
431 */
432static void tg_update_has_rules(struct throtl_grp *tg)
433{
434 struct throtl_grp *parent_tg = sq_to_tg(tg->service_queue.parent_sq);
Shaohua Li9f626e32017-03-27 10:51:30 -0700435 struct throtl_data *td = tg->td;
Tejun Heo693e7512013-05-14 13:52:38 -0700436 int rw;
437
438 for (rw = READ; rw <= WRITE; rw++)
439 tg->has_rules[rw] = (parent_tg && parent_tg->has_rules[rw]) ||
Shaohua Li9f626e32017-03-27 10:51:30 -0700440 (td->limit_valid[td->limit_index] &&
441 (tg_bps_limit(tg, rw) != U64_MAX ||
442 tg_iops_limit(tg, rw) != UINT_MAX));
Tejun Heo693e7512013-05-14 13:52:38 -0700443}
444
Tejun Heoa9520cd2015-08-18 14:55:14 -0700445static void throtl_pd_online(struct blkg_policy_data *pd)
Tejun Heo693e7512013-05-14 13:52:38 -0700446{
447 /*
448 * We don't want new groups to escape the limits of its ancestors.
449 * Update has_rules[] after a new group is brought online.
450 */
Tejun Heoa9520cd2015-08-18 14:55:14 -0700451 tg_update_has_rules(pd_to_tg(pd));
Tejun Heo693e7512013-05-14 13:52:38 -0700452}
453
Shaohua Licd5ab1b2017-03-27 10:51:32 -0700454static void blk_throtl_update_limit_valid(struct throtl_data *td)
455{
456 struct cgroup_subsys_state *pos_css;
457 struct blkcg_gq *blkg;
458 bool low_valid = false;
459
460 rcu_read_lock();
461 blkg_for_each_descendant_post(blkg, pos_css, td->queue->root_blkg) {
462 struct throtl_grp *tg = blkg_to_tg(blkg);
463
464 if (tg->bps[READ][LIMIT_LOW] || tg->bps[WRITE][LIMIT_LOW] ||
465 tg->iops[READ][LIMIT_LOW] || tg->iops[WRITE][LIMIT_LOW])
466 low_valid = true;
467 }
468 rcu_read_unlock();
469
470 td->limit_valid[LIMIT_LOW] = low_valid;
471}
472
Shaohua Lic79892c2017-03-27 10:51:34 -0700473static void throtl_upgrade_state(struct throtl_data *td);
Shaohua Licd5ab1b2017-03-27 10:51:32 -0700474static void throtl_pd_offline(struct blkg_policy_data *pd)
475{
476 struct throtl_grp *tg = pd_to_tg(pd);
477
478 tg->bps[READ][LIMIT_LOW] = 0;
479 tg->bps[WRITE][LIMIT_LOW] = 0;
480 tg->iops[READ][LIMIT_LOW] = 0;
481 tg->iops[WRITE][LIMIT_LOW] = 0;
482
483 blk_throtl_update_limit_valid(tg->td);
484
Shaohua Lic79892c2017-03-27 10:51:34 -0700485 if (!tg->td->limit_valid[tg->td->limit_index])
486 throtl_upgrade_state(tg->td);
Shaohua Licd5ab1b2017-03-27 10:51:32 -0700487}
488
Tejun Heo001bea72015-08-18 14:55:11 -0700489static void throtl_pd_free(struct blkg_policy_data *pd)
490{
Tejun Heo4fb72032015-08-18 14:55:12 -0700491 struct throtl_grp *tg = pd_to_tg(pd);
492
Tejun Heob2ce2642015-08-18 14:55:13 -0700493 del_timer_sync(&tg->service_queue.pending_timer);
Tejun Heo4fb72032015-08-18 14:55:12 -0700494 kfree(tg);
Tejun Heo001bea72015-08-18 14:55:11 -0700495}
496
Tejun Heo0049af72013-05-14 13:52:33 -0700497static struct throtl_grp *
498throtl_rb_first(struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -0400499{
500 /* Service tree is empty */
Tejun Heo0049af72013-05-14 13:52:33 -0700501 if (!parent_sq->nr_pending)
Vivek Goyale43473b2010-09-15 17:06:35 -0400502 return NULL;
503
Tejun Heo0049af72013-05-14 13:52:33 -0700504 if (!parent_sq->first_pending)
505 parent_sq->first_pending = rb_first(&parent_sq->pending_tree);
Vivek Goyale43473b2010-09-15 17:06:35 -0400506
Tejun Heo0049af72013-05-14 13:52:33 -0700507 if (parent_sq->first_pending)
508 return rb_entry_tg(parent_sq->first_pending);
Vivek Goyale43473b2010-09-15 17:06:35 -0400509
510 return NULL;
511}
512
513static void rb_erase_init(struct rb_node *n, struct rb_root *root)
514{
515 rb_erase(n, root);
516 RB_CLEAR_NODE(n);
517}
518
Tejun Heo0049af72013-05-14 13:52:33 -0700519static void throtl_rb_erase(struct rb_node *n,
520 struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -0400521{
Tejun Heo0049af72013-05-14 13:52:33 -0700522 if (parent_sq->first_pending == n)
523 parent_sq->first_pending = NULL;
524 rb_erase_init(n, &parent_sq->pending_tree);
525 --parent_sq->nr_pending;
Vivek Goyale43473b2010-09-15 17:06:35 -0400526}
527
Tejun Heo0049af72013-05-14 13:52:33 -0700528static void update_min_dispatch_time(struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -0400529{
530 struct throtl_grp *tg;
531
Tejun Heo0049af72013-05-14 13:52:33 -0700532 tg = throtl_rb_first(parent_sq);
Vivek Goyale43473b2010-09-15 17:06:35 -0400533 if (!tg)
534 return;
535
Tejun Heo0049af72013-05-14 13:52:33 -0700536 parent_sq->first_pending_disptime = tg->disptime;
Vivek Goyale43473b2010-09-15 17:06:35 -0400537}
538
Tejun Heo77216b02013-05-14 13:52:36 -0700539static void tg_service_queue_add(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400540{
Tejun Heo77216b02013-05-14 13:52:36 -0700541 struct throtl_service_queue *parent_sq = tg->service_queue.parent_sq;
Tejun Heo0049af72013-05-14 13:52:33 -0700542 struct rb_node **node = &parent_sq->pending_tree.rb_node;
Vivek Goyale43473b2010-09-15 17:06:35 -0400543 struct rb_node *parent = NULL;
544 struct throtl_grp *__tg;
545 unsigned long key = tg->disptime;
546 int left = 1;
547
548 while (*node != NULL) {
549 parent = *node;
550 __tg = rb_entry_tg(parent);
551
552 if (time_before(key, __tg->disptime))
553 node = &parent->rb_left;
554 else {
555 node = &parent->rb_right;
556 left = 0;
557 }
558 }
559
560 if (left)
Tejun Heo0049af72013-05-14 13:52:33 -0700561 parent_sq->first_pending = &tg->rb_node;
Vivek Goyale43473b2010-09-15 17:06:35 -0400562
563 rb_link_node(&tg->rb_node, parent, node);
Tejun Heo0049af72013-05-14 13:52:33 -0700564 rb_insert_color(&tg->rb_node, &parent_sq->pending_tree);
Vivek Goyale43473b2010-09-15 17:06:35 -0400565}
566
Tejun Heo77216b02013-05-14 13:52:36 -0700567static void __throtl_enqueue_tg(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400568{
Tejun Heo77216b02013-05-14 13:52:36 -0700569 tg_service_queue_add(tg);
Tejun Heo5b2c16a2013-05-14 13:52:32 -0700570 tg->flags |= THROTL_TG_PENDING;
Tejun Heo77216b02013-05-14 13:52:36 -0700571 tg->service_queue.parent_sq->nr_pending++;
Vivek Goyale43473b2010-09-15 17:06:35 -0400572}
573
Tejun Heo77216b02013-05-14 13:52:36 -0700574static void throtl_enqueue_tg(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400575{
Tejun Heo5b2c16a2013-05-14 13:52:32 -0700576 if (!(tg->flags & THROTL_TG_PENDING))
Tejun Heo77216b02013-05-14 13:52:36 -0700577 __throtl_enqueue_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -0400578}
579
Tejun Heo77216b02013-05-14 13:52:36 -0700580static void __throtl_dequeue_tg(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400581{
Tejun Heo77216b02013-05-14 13:52:36 -0700582 throtl_rb_erase(&tg->rb_node, tg->service_queue.parent_sq);
Tejun Heo5b2c16a2013-05-14 13:52:32 -0700583 tg->flags &= ~THROTL_TG_PENDING;
Vivek Goyale43473b2010-09-15 17:06:35 -0400584}
585
Tejun Heo77216b02013-05-14 13:52:36 -0700586static void throtl_dequeue_tg(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400587{
Tejun Heo5b2c16a2013-05-14 13:52:32 -0700588 if (tg->flags & THROTL_TG_PENDING)
Tejun Heo77216b02013-05-14 13:52:36 -0700589 __throtl_dequeue_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -0400590}
591
Tejun Heoa9131a22013-05-14 13:52:31 -0700592/* Call with queue lock held */
Tejun Heo69df0ab2013-05-14 13:52:36 -0700593static void throtl_schedule_pending_timer(struct throtl_service_queue *sq,
594 unsigned long expires)
Tejun Heoa9131a22013-05-14 13:52:31 -0700595{
Shaohua Li297e3d82017-03-27 10:51:37 -0700596 unsigned long max_expire = jiffies + 8 * sq_to_tg(sq)->td->throtl_slice;
Shaohua Li06cceed2017-03-27 10:51:36 -0700597
598 /*
599 * Since we are adjusting the throttle limit dynamically, the sleep
600 * time calculated according to previous limit might be invalid. It's
601 * possible the cgroup sleep time is very long and no other cgroups
602 * have IO running so notify the limit changes. Make sure the cgroup
603 * doesn't sleep too long to avoid the missed notification.
604 */
605 if (time_after(expires, max_expire))
606 expires = max_expire;
Tejun Heo69df0ab2013-05-14 13:52:36 -0700607 mod_timer(&sq->pending_timer, expires);
608 throtl_log(sq, "schedule timer. delay=%lu jiffies=%lu",
609 expires - jiffies, jiffies);
Tejun Heoa9131a22013-05-14 13:52:31 -0700610}
611
Tejun Heo7f52f982013-05-14 13:52:37 -0700612/**
613 * throtl_schedule_next_dispatch - schedule the next dispatch cycle
614 * @sq: the service_queue to schedule dispatch for
615 * @force: force scheduling
616 *
617 * Arm @sq->pending_timer so that the next dispatch cycle starts on the
618 * dispatch time of the first pending child. Returns %true if either timer
619 * is armed or there's no pending child left. %false if the current
620 * dispatch window is still open and the caller should continue
621 * dispatching.
622 *
623 * If @force is %true, the dispatch timer is always scheduled and this
624 * function is guaranteed to return %true. This is to be used when the
625 * caller can't dispatch itself and needs to invoke pending_timer
626 * unconditionally. Note that forced scheduling is likely to induce short
627 * delay before dispatch starts even if @sq->first_pending_disptime is not
628 * in the future and thus shouldn't be used in hot paths.
629 */
630static bool throtl_schedule_next_dispatch(struct throtl_service_queue *sq,
631 bool force)
Vivek Goyale43473b2010-09-15 17:06:35 -0400632{
Tejun Heo6a525602013-05-14 13:52:32 -0700633 /* any pending children left? */
Tejun Heoc9e03322013-05-14 13:52:32 -0700634 if (!sq->nr_pending)
Tejun Heo7f52f982013-05-14 13:52:37 -0700635 return true;
Vivek Goyale43473b2010-09-15 17:06:35 -0400636
Tejun Heoc9e03322013-05-14 13:52:32 -0700637 update_min_dispatch_time(sq);
Vivek Goyale43473b2010-09-15 17:06:35 -0400638
Tejun Heo69df0ab2013-05-14 13:52:36 -0700639 /* is the next dispatch time in the future? */
Tejun Heo7f52f982013-05-14 13:52:37 -0700640 if (force || time_after(sq->first_pending_disptime, jiffies)) {
Tejun Heo69df0ab2013-05-14 13:52:36 -0700641 throtl_schedule_pending_timer(sq, sq->first_pending_disptime);
Tejun Heo7f52f982013-05-14 13:52:37 -0700642 return true;
Tejun Heo69df0ab2013-05-14 13:52:36 -0700643 }
644
Tejun Heo7f52f982013-05-14 13:52:37 -0700645 /* tell the caller to continue dispatching */
646 return false;
Vivek Goyale43473b2010-09-15 17:06:35 -0400647}
648
Vivek Goyal32ee5bc2013-05-14 13:52:38 -0700649static inline void throtl_start_new_slice_with_credit(struct throtl_grp *tg,
650 bool rw, unsigned long start)
651{
652 tg->bytes_disp[rw] = 0;
653 tg->io_disp[rw] = 0;
654
655 /*
656 * Previous slice has expired. We must have trimmed it after last
657 * bio dispatch. That means since start of last slice, we never used
658 * that bandwidth. Do try to make use of that bandwidth while giving
659 * credit.
660 */
661 if (time_after_eq(start, tg->slice_start[rw]))
662 tg->slice_start[rw] = start;
663
Shaohua Li297e3d82017-03-27 10:51:37 -0700664 tg->slice_end[rw] = jiffies + tg->td->throtl_slice;
Vivek Goyal32ee5bc2013-05-14 13:52:38 -0700665 throtl_log(&tg->service_queue,
666 "[%c] new slice with credit start=%lu end=%lu jiffies=%lu",
667 rw == READ ? 'R' : 'W', tg->slice_start[rw],
668 tg->slice_end[rw], jiffies);
669}
670
Tejun Heo0f3457f2013-05-14 13:52:32 -0700671static inline void throtl_start_new_slice(struct throtl_grp *tg, bool rw)
Vivek Goyale43473b2010-09-15 17:06:35 -0400672{
673 tg->bytes_disp[rw] = 0;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400674 tg->io_disp[rw] = 0;
Vivek Goyale43473b2010-09-15 17:06:35 -0400675 tg->slice_start[rw] = jiffies;
Shaohua Li297e3d82017-03-27 10:51:37 -0700676 tg->slice_end[rw] = jiffies + tg->td->throtl_slice;
Tejun Heofda6f272013-05-14 13:52:36 -0700677 throtl_log(&tg->service_queue,
678 "[%c] new slice start=%lu end=%lu jiffies=%lu",
679 rw == READ ? 'R' : 'W', tg->slice_start[rw],
680 tg->slice_end[rw], jiffies);
Vivek Goyale43473b2010-09-15 17:06:35 -0400681}
682
Tejun Heo0f3457f2013-05-14 13:52:32 -0700683static inline void throtl_set_slice_end(struct throtl_grp *tg, bool rw,
684 unsigned long jiffy_end)
Vivek Goyald1ae8ff2010-12-01 19:34:46 +0100685{
Shaohua Li297e3d82017-03-27 10:51:37 -0700686 tg->slice_end[rw] = roundup(jiffy_end, tg->td->throtl_slice);
Vivek Goyald1ae8ff2010-12-01 19:34:46 +0100687}
688
Tejun Heo0f3457f2013-05-14 13:52:32 -0700689static inline void throtl_extend_slice(struct throtl_grp *tg, bool rw,
690 unsigned long jiffy_end)
Vivek Goyale43473b2010-09-15 17:06:35 -0400691{
Shaohua Li297e3d82017-03-27 10:51:37 -0700692 tg->slice_end[rw] = roundup(jiffy_end, tg->td->throtl_slice);
Tejun Heofda6f272013-05-14 13:52:36 -0700693 throtl_log(&tg->service_queue,
694 "[%c] extend slice start=%lu end=%lu jiffies=%lu",
695 rw == READ ? 'R' : 'W', tg->slice_start[rw],
696 tg->slice_end[rw], jiffies);
Vivek Goyale43473b2010-09-15 17:06:35 -0400697}
698
699/* Determine if previously allocated or extended slice is complete or not */
Tejun Heo0f3457f2013-05-14 13:52:32 -0700700static bool throtl_slice_used(struct throtl_grp *tg, bool rw)
Vivek Goyale43473b2010-09-15 17:06:35 -0400701{
702 if (time_in_range(jiffies, tg->slice_start[rw], tg->slice_end[rw]))
Fabian Frederick5cf8c222014-05-02 18:28:17 +0200703 return false;
Vivek Goyale43473b2010-09-15 17:06:35 -0400704
705 return 1;
706}
707
708/* Trim the used slices and adjust slice start accordingly */
Tejun Heo0f3457f2013-05-14 13:52:32 -0700709static inline void throtl_trim_slice(struct throtl_grp *tg, bool rw)
Vivek Goyale43473b2010-09-15 17:06:35 -0400710{
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200711 unsigned long nr_slices, time_elapsed, io_trim;
712 u64 bytes_trim, tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400713
714 BUG_ON(time_before(tg->slice_end[rw], tg->slice_start[rw]));
715
716 /*
717 * If bps are unlimited (-1), then time slice don't get
718 * renewed. Don't try to trim the slice if slice is used. A new
719 * slice will start when appropriate.
720 */
Tejun Heo0f3457f2013-05-14 13:52:32 -0700721 if (throtl_slice_used(tg, rw))
Vivek Goyale43473b2010-09-15 17:06:35 -0400722 return;
723
Vivek Goyald1ae8ff2010-12-01 19:34:46 +0100724 /*
725 * A bio has been dispatched. Also adjust slice_end. It might happen
726 * that initially cgroup limit was very low resulting in high
727 * slice_end, but later limit was bumped up and bio was dispached
728 * sooner, then we need to reduce slice_end. A high bogus slice_end
729 * is bad because it does not allow new slice to start.
730 */
731
Shaohua Li297e3d82017-03-27 10:51:37 -0700732 throtl_set_slice_end(tg, rw, jiffies + tg->td->throtl_slice);
Vivek Goyald1ae8ff2010-12-01 19:34:46 +0100733
Vivek Goyale43473b2010-09-15 17:06:35 -0400734 time_elapsed = jiffies - tg->slice_start[rw];
735
Shaohua Li297e3d82017-03-27 10:51:37 -0700736 nr_slices = time_elapsed / tg->td->throtl_slice;
Vivek Goyale43473b2010-09-15 17:06:35 -0400737
738 if (!nr_slices)
739 return;
Shaohua Li297e3d82017-03-27 10:51:37 -0700740 tmp = tg_bps_limit(tg, rw) * tg->td->throtl_slice * nr_slices;
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200741 do_div(tmp, HZ);
742 bytes_trim = tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400743
Shaohua Li297e3d82017-03-27 10:51:37 -0700744 io_trim = (tg_iops_limit(tg, rw) * tg->td->throtl_slice * nr_slices) /
745 HZ;
Vivek Goyale43473b2010-09-15 17:06:35 -0400746
Vivek Goyal8e89d132010-09-15 17:06:37 -0400747 if (!bytes_trim && !io_trim)
Vivek Goyale43473b2010-09-15 17:06:35 -0400748 return;
749
750 if (tg->bytes_disp[rw] >= bytes_trim)
751 tg->bytes_disp[rw] -= bytes_trim;
752 else
753 tg->bytes_disp[rw] = 0;
754
Vivek Goyal8e89d132010-09-15 17:06:37 -0400755 if (tg->io_disp[rw] >= io_trim)
756 tg->io_disp[rw] -= io_trim;
757 else
758 tg->io_disp[rw] = 0;
759
Shaohua Li297e3d82017-03-27 10:51:37 -0700760 tg->slice_start[rw] += nr_slices * tg->td->throtl_slice;
Vivek Goyale43473b2010-09-15 17:06:35 -0400761
Tejun Heofda6f272013-05-14 13:52:36 -0700762 throtl_log(&tg->service_queue,
763 "[%c] trim slice nr=%lu bytes=%llu io=%lu start=%lu end=%lu jiffies=%lu",
764 rw == READ ? 'R' : 'W', nr_slices, bytes_trim, io_trim,
765 tg->slice_start[rw], tg->slice_end[rw], jiffies);
Vivek Goyale43473b2010-09-15 17:06:35 -0400766}
767
Tejun Heo0f3457f2013-05-14 13:52:32 -0700768static bool tg_with_in_iops_limit(struct throtl_grp *tg, struct bio *bio,
769 unsigned long *wait)
Vivek Goyale43473b2010-09-15 17:06:35 -0400770{
771 bool rw = bio_data_dir(bio);
Vivek Goyal8e89d132010-09-15 17:06:37 -0400772 unsigned int io_allowed;
Vivek Goyale43473b2010-09-15 17:06:35 -0400773 unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
Vivek Goyalc49c06e2010-10-01 21:16:42 +0200774 u64 tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400775
Vivek Goyal8e89d132010-09-15 17:06:37 -0400776 jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
Vivek Goyale43473b2010-09-15 17:06:35 -0400777
Vivek Goyal8e89d132010-09-15 17:06:37 -0400778 /* Slice has just started. Consider one slice interval */
779 if (!jiffy_elapsed)
Shaohua Li297e3d82017-03-27 10:51:37 -0700780 jiffy_elapsed_rnd = tg->td->throtl_slice;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400781
Shaohua Li297e3d82017-03-27 10:51:37 -0700782 jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, tg->td->throtl_slice);
Vivek Goyal8e89d132010-09-15 17:06:37 -0400783
Vivek Goyalc49c06e2010-10-01 21:16:42 +0200784 /*
785 * jiffy_elapsed_rnd should not be a big value as minimum iops can be
786 * 1 then at max jiffy elapsed should be equivalent of 1 second as we
787 * will allow dispatch after 1 second and after that slice should
788 * have been trimmed.
789 */
790
Shaohua Li9f626e32017-03-27 10:51:30 -0700791 tmp = (u64)tg_iops_limit(tg, rw) * jiffy_elapsed_rnd;
Vivek Goyalc49c06e2010-10-01 21:16:42 +0200792 do_div(tmp, HZ);
793
794 if (tmp > UINT_MAX)
795 io_allowed = UINT_MAX;
796 else
797 io_allowed = tmp;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400798
799 if (tg->io_disp[rw] + 1 <= io_allowed) {
Vivek Goyale43473b2010-09-15 17:06:35 -0400800 if (wait)
801 *wait = 0;
Fabian Frederick5cf8c222014-05-02 18:28:17 +0200802 return true;
Vivek Goyale43473b2010-09-15 17:06:35 -0400803 }
804
Vivek Goyal8e89d132010-09-15 17:06:37 -0400805 /* Calc approx time to dispatch */
Shaohua Li9f626e32017-03-27 10:51:30 -0700806 jiffy_wait = ((tg->io_disp[rw] + 1) * HZ) / tg_iops_limit(tg, rw) + 1;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400807
808 if (jiffy_wait > jiffy_elapsed)
809 jiffy_wait = jiffy_wait - jiffy_elapsed;
810 else
811 jiffy_wait = 1;
812
813 if (wait)
814 *wait = jiffy_wait;
815 return 0;
816}
817
Tejun Heo0f3457f2013-05-14 13:52:32 -0700818static bool tg_with_in_bps_limit(struct throtl_grp *tg, struct bio *bio,
819 unsigned long *wait)
Vivek Goyal8e89d132010-09-15 17:06:37 -0400820{
821 bool rw = bio_data_dir(bio);
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200822 u64 bytes_allowed, extra_bytes, tmp;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400823 unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
Vivek Goyale43473b2010-09-15 17:06:35 -0400824
825 jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
826
827 /* Slice has just started. Consider one slice interval */
828 if (!jiffy_elapsed)
Shaohua Li297e3d82017-03-27 10:51:37 -0700829 jiffy_elapsed_rnd = tg->td->throtl_slice;
Vivek Goyale43473b2010-09-15 17:06:35 -0400830
Shaohua Li297e3d82017-03-27 10:51:37 -0700831 jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, tg->td->throtl_slice);
Vivek Goyale43473b2010-09-15 17:06:35 -0400832
Shaohua Li9f626e32017-03-27 10:51:30 -0700833 tmp = tg_bps_limit(tg, rw) * jiffy_elapsed_rnd;
Vivek Goyal5e901a22010-10-01 21:16:38 +0200834 do_div(tmp, HZ);
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200835 bytes_allowed = tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400836
Kent Overstreet4f024f32013-10-11 15:44:27 -0700837 if (tg->bytes_disp[rw] + bio->bi_iter.bi_size <= bytes_allowed) {
Vivek Goyale43473b2010-09-15 17:06:35 -0400838 if (wait)
839 *wait = 0;
Fabian Frederick5cf8c222014-05-02 18:28:17 +0200840 return true;
Vivek Goyale43473b2010-09-15 17:06:35 -0400841 }
842
843 /* Calc approx time to dispatch */
Kent Overstreet4f024f32013-10-11 15:44:27 -0700844 extra_bytes = tg->bytes_disp[rw] + bio->bi_iter.bi_size - bytes_allowed;
Shaohua Li9f626e32017-03-27 10:51:30 -0700845 jiffy_wait = div64_u64(extra_bytes * HZ, tg_bps_limit(tg, rw));
Vivek Goyale43473b2010-09-15 17:06:35 -0400846
847 if (!jiffy_wait)
848 jiffy_wait = 1;
849
850 /*
851 * This wait time is without taking into consideration the rounding
852 * up we did. Add that time also.
853 */
854 jiffy_wait = jiffy_wait + (jiffy_elapsed_rnd - jiffy_elapsed);
Vivek Goyale43473b2010-09-15 17:06:35 -0400855 if (wait)
856 *wait = jiffy_wait;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400857 return 0;
858}
Vivek Goyale43473b2010-09-15 17:06:35 -0400859
Vivek Goyal8e89d132010-09-15 17:06:37 -0400860/*
861 * Returns whether one can dispatch a bio or not. Also returns approx number
862 * of jiffies to wait before this bio is with-in IO rate and can be dispatched
863 */
Tejun Heo0f3457f2013-05-14 13:52:32 -0700864static bool tg_may_dispatch(struct throtl_grp *tg, struct bio *bio,
865 unsigned long *wait)
Vivek Goyal8e89d132010-09-15 17:06:37 -0400866{
867 bool rw = bio_data_dir(bio);
868 unsigned long bps_wait = 0, iops_wait = 0, max_wait = 0;
869
870 /*
871 * Currently whole state machine of group depends on first bio
872 * queued in the group bio list. So one should not be calling
873 * this function with a different bio if there are other bios
874 * queued.
875 */
Tejun Heo73f0d492013-05-14 13:52:35 -0700876 BUG_ON(tg->service_queue.nr_queued[rw] &&
Tejun Heoc5cc2072013-05-14 13:52:38 -0700877 bio != throtl_peek_queued(&tg->service_queue.queued[rw]));
Vivek Goyal8e89d132010-09-15 17:06:37 -0400878
879 /* If tg->bps = -1, then BW is unlimited */
Shaohua Li9f626e32017-03-27 10:51:30 -0700880 if (tg_bps_limit(tg, rw) == U64_MAX &&
881 tg_iops_limit(tg, rw) == UINT_MAX) {
Vivek Goyal8e89d132010-09-15 17:06:37 -0400882 if (wait)
883 *wait = 0;
Fabian Frederick5cf8c222014-05-02 18:28:17 +0200884 return true;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400885 }
886
887 /*
888 * If previous slice expired, start a new one otherwise renew/extend
889 * existing slice to make sure it is at least throtl_slice interval
Vivek Goyal164c80e2016-09-19 15:12:41 -0600890 * long since now. New slice is started only for empty throttle group.
891 * If there is queued bio, that means there should be an active
892 * slice and it should be extended instead.
Vivek Goyal8e89d132010-09-15 17:06:37 -0400893 */
Vivek Goyal164c80e2016-09-19 15:12:41 -0600894 if (throtl_slice_used(tg, rw) && !(tg->service_queue.nr_queued[rw]))
Tejun Heo0f3457f2013-05-14 13:52:32 -0700895 throtl_start_new_slice(tg, rw);
Vivek Goyal8e89d132010-09-15 17:06:37 -0400896 else {
Shaohua Li297e3d82017-03-27 10:51:37 -0700897 if (time_before(tg->slice_end[rw],
898 jiffies + tg->td->throtl_slice))
899 throtl_extend_slice(tg, rw,
900 jiffies + tg->td->throtl_slice);
Vivek Goyal8e89d132010-09-15 17:06:37 -0400901 }
902
Tejun Heo0f3457f2013-05-14 13:52:32 -0700903 if (tg_with_in_bps_limit(tg, bio, &bps_wait) &&
904 tg_with_in_iops_limit(tg, bio, &iops_wait)) {
Vivek Goyal8e89d132010-09-15 17:06:37 -0400905 if (wait)
906 *wait = 0;
907 return 1;
908 }
909
910 max_wait = max(bps_wait, iops_wait);
911
912 if (wait)
913 *wait = max_wait;
914
915 if (time_before(tg->slice_end[rw], jiffies + max_wait))
Tejun Heo0f3457f2013-05-14 13:52:32 -0700916 throtl_extend_slice(tg, rw, jiffies + max_wait);
Vivek Goyale43473b2010-09-15 17:06:35 -0400917
918 return 0;
919}
920
921static void throtl_charge_bio(struct throtl_grp *tg, struct bio *bio)
922{
923 bool rw = bio_data_dir(bio);
Vivek Goyale43473b2010-09-15 17:06:35 -0400924
925 /* Charge the bio to the group */
Kent Overstreet4f024f32013-10-11 15:44:27 -0700926 tg->bytes_disp[rw] += bio->bi_iter.bi_size;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400927 tg->io_disp[rw]++;
Shaohua Li3f0abd82017-03-27 10:51:35 -0700928 tg->last_bytes_disp[rw] += bio->bi_iter.bi_size;
929 tg->last_io_disp[rw]++;
Vivek Goyale43473b2010-09-15 17:06:35 -0400930
Tejun Heo2a0f61e2013-05-14 13:52:36 -0700931 /*
Christoph Hellwig8d2bbd42016-10-20 15:12:12 +0200932 * BIO_THROTTLED is used to prevent the same bio to be throttled
Tejun Heo2a0f61e2013-05-14 13:52:36 -0700933 * more than once as a throttled bio will go through blk-throtl the
934 * second time when it eventually gets issued. Set it when a bio
935 * is being charged to a tg.
Tejun Heo2a0f61e2013-05-14 13:52:36 -0700936 */
Christoph Hellwig8d2bbd42016-10-20 15:12:12 +0200937 if (!bio_flagged(bio, BIO_THROTTLED))
938 bio_set_flag(bio, BIO_THROTTLED);
Vivek Goyale43473b2010-09-15 17:06:35 -0400939}
940
Tejun Heoc5cc2072013-05-14 13:52:38 -0700941/**
942 * throtl_add_bio_tg - add a bio to the specified throtl_grp
943 * @bio: bio to add
944 * @qn: qnode to use
945 * @tg: the target throtl_grp
946 *
947 * Add @bio to @tg's service_queue using @qn. If @qn is not specified,
948 * tg->qnode_on_self[] is used.
949 */
950static void throtl_add_bio_tg(struct bio *bio, struct throtl_qnode *qn,
951 struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400952{
Tejun Heo73f0d492013-05-14 13:52:35 -0700953 struct throtl_service_queue *sq = &tg->service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -0400954 bool rw = bio_data_dir(bio);
955
Tejun Heoc5cc2072013-05-14 13:52:38 -0700956 if (!qn)
957 qn = &tg->qnode_on_self[rw];
958
Tejun Heo0e9f4162013-05-14 13:52:35 -0700959 /*
960 * If @tg doesn't currently have any bios queued in the same
961 * direction, queueing @bio can change when @tg should be
962 * dispatched. Mark that @tg was empty. This is automatically
963 * cleaered on the next tg_update_disptime().
964 */
965 if (!sq->nr_queued[rw])
966 tg->flags |= THROTL_TG_WAS_EMPTY;
967
Tejun Heoc5cc2072013-05-14 13:52:38 -0700968 throtl_qnode_add_bio(bio, qn, &sq->queued[rw]);
969
Tejun Heo73f0d492013-05-14 13:52:35 -0700970 sq->nr_queued[rw]++;
Tejun Heo77216b02013-05-14 13:52:36 -0700971 throtl_enqueue_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -0400972}
973
Tejun Heo77216b02013-05-14 13:52:36 -0700974static void tg_update_disptime(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400975{
Tejun Heo73f0d492013-05-14 13:52:35 -0700976 struct throtl_service_queue *sq = &tg->service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -0400977 unsigned long read_wait = -1, write_wait = -1, min_wait = -1, disptime;
978 struct bio *bio;
979
Markus Elfringd609af32017-01-21 22:15:33 +0100980 bio = throtl_peek_queued(&sq->queued[READ]);
981 if (bio)
Tejun Heo0f3457f2013-05-14 13:52:32 -0700982 tg_may_dispatch(tg, bio, &read_wait);
Vivek Goyale43473b2010-09-15 17:06:35 -0400983
Markus Elfringd609af32017-01-21 22:15:33 +0100984 bio = throtl_peek_queued(&sq->queued[WRITE]);
985 if (bio)
Tejun Heo0f3457f2013-05-14 13:52:32 -0700986 tg_may_dispatch(tg, bio, &write_wait);
Vivek Goyale43473b2010-09-15 17:06:35 -0400987
988 min_wait = min(read_wait, write_wait);
989 disptime = jiffies + min_wait;
990
Vivek Goyale43473b2010-09-15 17:06:35 -0400991 /* Update dispatch time */
Tejun Heo77216b02013-05-14 13:52:36 -0700992 throtl_dequeue_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -0400993 tg->disptime = disptime;
Tejun Heo77216b02013-05-14 13:52:36 -0700994 throtl_enqueue_tg(tg);
Tejun Heo0e9f4162013-05-14 13:52:35 -0700995
996 /* see throtl_add_bio_tg() */
997 tg->flags &= ~THROTL_TG_WAS_EMPTY;
Vivek Goyale43473b2010-09-15 17:06:35 -0400998}
999
Vivek Goyal32ee5bc2013-05-14 13:52:38 -07001000static void start_parent_slice_with_credit(struct throtl_grp *child_tg,
1001 struct throtl_grp *parent_tg, bool rw)
1002{
1003 if (throtl_slice_used(parent_tg, rw)) {
1004 throtl_start_new_slice_with_credit(parent_tg, rw,
1005 child_tg->slice_start[rw]);
1006 }
1007
1008}
1009
Tejun Heo77216b02013-05-14 13:52:36 -07001010static void tg_dispatch_one_bio(struct throtl_grp *tg, bool rw)
Vivek Goyale43473b2010-09-15 17:06:35 -04001011{
Tejun Heo73f0d492013-05-14 13:52:35 -07001012 struct throtl_service_queue *sq = &tg->service_queue;
Tejun Heo6bc9c2b2013-05-14 13:52:38 -07001013 struct throtl_service_queue *parent_sq = sq->parent_sq;
1014 struct throtl_grp *parent_tg = sq_to_tg(parent_sq);
Tejun Heoc5cc2072013-05-14 13:52:38 -07001015 struct throtl_grp *tg_to_put = NULL;
Vivek Goyale43473b2010-09-15 17:06:35 -04001016 struct bio *bio;
1017
Tejun Heoc5cc2072013-05-14 13:52:38 -07001018 /*
1019 * @bio is being transferred from @tg to @parent_sq. Popping a bio
1020 * from @tg may put its reference and @parent_sq might end up
1021 * getting released prematurely. Remember the tg to put and put it
1022 * after @bio is transferred to @parent_sq.
1023 */
1024 bio = throtl_pop_queued(&sq->queued[rw], &tg_to_put);
Tejun Heo73f0d492013-05-14 13:52:35 -07001025 sq->nr_queued[rw]--;
Vivek Goyale43473b2010-09-15 17:06:35 -04001026
1027 throtl_charge_bio(tg, bio);
Tejun Heo6bc9c2b2013-05-14 13:52:38 -07001028
1029 /*
1030 * If our parent is another tg, we just need to transfer @bio to
1031 * the parent using throtl_add_bio_tg(). If our parent is
1032 * @td->service_queue, @bio is ready to be issued. Put it on its
1033 * bio_lists[] and decrease total number queued. The caller is
1034 * responsible for issuing these bios.
1035 */
1036 if (parent_tg) {
Tejun Heoc5cc2072013-05-14 13:52:38 -07001037 throtl_add_bio_tg(bio, &tg->qnode_on_parent[rw], parent_tg);
Vivek Goyal32ee5bc2013-05-14 13:52:38 -07001038 start_parent_slice_with_credit(tg, parent_tg, rw);
Tejun Heo6bc9c2b2013-05-14 13:52:38 -07001039 } else {
Tejun Heoc5cc2072013-05-14 13:52:38 -07001040 throtl_qnode_add_bio(bio, &tg->qnode_on_parent[rw],
1041 &parent_sq->queued[rw]);
Tejun Heo6bc9c2b2013-05-14 13:52:38 -07001042 BUG_ON(tg->td->nr_queued[rw] <= 0);
1043 tg->td->nr_queued[rw]--;
1044 }
Vivek Goyale43473b2010-09-15 17:06:35 -04001045
Tejun Heo0f3457f2013-05-14 13:52:32 -07001046 throtl_trim_slice(tg, rw);
Tejun Heo6bc9c2b2013-05-14 13:52:38 -07001047
Tejun Heoc5cc2072013-05-14 13:52:38 -07001048 if (tg_to_put)
1049 blkg_put(tg_to_blkg(tg_to_put));
Vivek Goyale43473b2010-09-15 17:06:35 -04001050}
1051
Tejun Heo77216b02013-05-14 13:52:36 -07001052static int throtl_dispatch_tg(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -04001053{
Tejun Heo73f0d492013-05-14 13:52:35 -07001054 struct throtl_service_queue *sq = &tg->service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -04001055 unsigned int nr_reads = 0, nr_writes = 0;
1056 unsigned int max_nr_reads = throtl_grp_quantum*3/4;
Vivek Goyalc2f68052010-11-15 19:32:42 +01001057 unsigned int max_nr_writes = throtl_grp_quantum - max_nr_reads;
Vivek Goyale43473b2010-09-15 17:06:35 -04001058 struct bio *bio;
1059
1060 /* Try to dispatch 75% READS and 25% WRITES */
1061
Tejun Heoc5cc2072013-05-14 13:52:38 -07001062 while ((bio = throtl_peek_queued(&sq->queued[READ])) &&
Tejun Heo0f3457f2013-05-14 13:52:32 -07001063 tg_may_dispatch(tg, bio, NULL)) {
Vivek Goyale43473b2010-09-15 17:06:35 -04001064
Tejun Heo77216b02013-05-14 13:52:36 -07001065 tg_dispatch_one_bio(tg, bio_data_dir(bio));
Vivek Goyale43473b2010-09-15 17:06:35 -04001066 nr_reads++;
1067
1068 if (nr_reads >= max_nr_reads)
1069 break;
1070 }
1071
Tejun Heoc5cc2072013-05-14 13:52:38 -07001072 while ((bio = throtl_peek_queued(&sq->queued[WRITE])) &&
Tejun Heo0f3457f2013-05-14 13:52:32 -07001073 tg_may_dispatch(tg, bio, NULL)) {
Vivek Goyale43473b2010-09-15 17:06:35 -04001074
Tejun Heo77216b02013-05-14 13:52:36 -07001075 tg_dispatch_one_bio(tg, bio_data_dir(bio));
Vivek Goyale43473b2010-09-15 17:06:35 -04001076 nr_writes++;
1077
1078 if (nr_writes >= max_nr_writes)
1079 break;
1080 }
1081
1082 return nr_reads + nr_writes;
1083}
1084
Tejun Heo651930b2013-05-14 13:52:35 -07001085static int throtl_select_dispatch(struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -04001086{
1087 unsigned int nr_disp = 0;
Vivek Goyale43473b2010-09-15 17:06:35 -04001088
1089 while (1) {
Tejun Heo73f0d492013-05-14 13:52:35 -07001090 struct throtl_grp *tg = throtl_rb_first(parent_sq);
1091 struct throtl_service_queue *sq = &tg->service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -04001092
1093 if (!tg)
1094 break;
1095
1096 if (time_before(jiffies, tg->disptime))
1097 break;
1098
Tejun Heo77216b02013-05-14 13:52:36 -07001099 throtl_dequeue_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -04001100
Tejun Heo77216b02013-05-14 13:52:36 -07001101 nr_disp += throtl_dispatch_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -04001102
Tejun Heo73f0d492013-05-14 13:52:35 -07001103 if (sq->nr_queued[0] || sq->nr_queued[1])
Tejun Heo77216b02013-05-14 13:52:36 -07001104 tg_update_disptime(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -04001105
1106 if (nr_disp >= throtl_quantum)
1107 break;
1108 }
1109
1110 return nr_disp;
1111}
1112
Shaohua Lic79892c2017-03-27 10:51:34 -07001113static bool throtl_can_upgrade(struct throtl_data *td,
1114 struct throtl_grp *this_tg);
Tejun Heo6e1a5702013-05-14 13:52:37 -07001115/**
1116 * throtl_pending_timer_fn - timer function for service_queue->pending_timer
1117 * @arg: the throtl_service_queue being serviced
1118 *
1119 * This timer is armed when a child throtl_grp with active bio's become
1120 * pending and queued on the service_queue's pending_tree and expires when
1121 * the first child throtl_grp should be dispatched. This function
Tejun Heo2e48a532013-05-14 13:52:38 -07001122 * dispatches bio's from the children throtl_grps to the parent
1123 * service_queue.
1124 *
1125 * If the parent's parent is another throtl_grp, dispatching is propagated
1126 * by either arming its pending_timer or repeating dispatch directly. If
1127 * the top-level service_tree is reached, throtl_data->dispatch_work is
1128 * kicked so that the ready bio's are issued.
Tejun Heo6e1a5702013-05-14 13:52:37 -07001129 */
Tejun Heo69df0ab2013-05-14 13:52:36 -07001130static void throtl_pending_timer_fn(unsigned long arg)
1131{
1132 struct throtl_service_queue *sq = (void *)arg;
Tejun Heo2e48a532013-05-14 13:52:38 -07001133 struct throtl_grp *tg = sq_to_tg(sq);
Tejun Heo69df0ab2013-05-14 13:52:36 -07001134 struct throtl_data *td = sq_to_td(sq);
Tejun Heocb761992013-05-14 13:52:31 -07001135 struct request_queue *q = td->queue;
Tejun Heo2e48a532013-05-14 13:52:38 -07001136 struct throtl_service_queue *parent_sq;
1137 bool dispatched;
Tejun Heo6e1a5702013-05-14 13:52:37 -07001138 int ret;
Vivek Goyale43473b2010-09-15 17:06:35 -04001139
1140 spin_lock_irq(q->queue_lock);
Shaohua Lic79892c2017-03-27 10:51:34 -07001141 if (throtl_can_upgrade(td, NULL))
1142 throtl_upgrade_state(td);
1143
Tejun Heo2e48a532013-05-14 13:52:38 -07001144again:
1145 parent_sq = sq->parent_sq;
1146 dispatched = false;
Vivek Goyale43473b2010-09-15 17:06:35 -04001147
Tejun Heo7f52f982013-05-14 13:52:37 -07001148 while (true) {
1149 throtl_log(sq, "dispatch nr_queued=%u read=%u write=%u",
Tejun Heo2e48a532013-05-14 13:52:38 -07001150 sq->nr_queued[READ] + sq->nr_queued[WRITE],
1151 sq->nr_queued[READ], sq->nr_queued[WRITE]);
Vivek Goyale43473b2010-09-15 17:06:35 -04001152
Tejun Heo7f52f982013-05-14 13:52:37 -07001153 ret = throtl_select_dispatch(sq);
1154 if (ret) {
Tejun Heo7f52f982013-05-14 13:52:37 -07001155 throtl_log(sq, "bios disp=%u", ret);
1156 dispatched = true;
Tejun Heo651930b2013-05-14 13:52:35 -07001157 }
Vivek Goyale43473b2010-09-15 17:06:35 -04001158
Tejun Heo7f52f982013-05-14 13:52:37 -07001159 if (throtl_schedule_next_dispatch(sq, false))
1160 break;
1161
1162 /* this dispatch windows is still open, relax and repeat */
1163 spin_unlock_irq(q->queue_lock);
1164 cpu_relax();
1165 spin_lock_irq(q->queue_lock);
1166 }
Tejun Heo6a525602013-05-14 13:52:32 -07001167
Tejun Heo2e48a532013-05-14 13:52:38 -07001168 if (!dispatched)
1169 goto out_unlock;
Tejun Heo6e1a5702013-05-14 13:52:37 -07001170
Tejun Heo2e48a532013-05-14 13:52:38 -07001171 if (parent_sq) {
1172 /* @parent_sq is another throl_grp, propagate dispatch */
1173 if (tg->flags & THROTL_TG_WAS_EMPTY) {
1174 tg_update_disptime(tg);
1175 if (!throtl_schedule_next_dispatch(parent_sq, false)) {
1176 /* window is already open, repeat dispatching */
1177 sq = parent_sq;
1178 tg = sq_to_tg(sq);
1179 goto again;
1180 }
1181 }
1182 } else {
1183 /* reached the top-level, queue issueing */
1184 queue_work(kthrotld_workqueue, &td->dispatch_work);
1185 }
1186out_unlock:
Tejun Heo6e1a5702013-05-14 13:52:37 -07001187 spin_unlock_irq(q->queue_lock);
1188}
1189
1190/**
1191 * blk_throtl_dispatch_work_fn - work function for throtl_data->dispatch_work
1192 * @work: work item being executed
1193 *
1194 * This function is queued for execution when bio's reach the bio_lists[]
1195 * of throtl_data->service_queue. Those bio's are ready and issued by this
1196 * function.
1197 */
Fabian Frederick8876e1402014-04-17 21:41:16 +02001198static void blk_throtl_dispatch_work_fn(struct work_struct *work)
Tejun Heo6e1a5702013-05-14 13:52:37 -07001199{
1200 struct throtl_data *td = container_of(work, struct throtl_data,
1201 dispatch_work);
1202 struct throtl_service_queue *td_sq = &td->service_queue;
1203 struct request_queue *q = td->queue;
1204 struct bio_list bio_list_on_stack;
1205 struct bio *bio;
1206 struct blk_plug plug;
1207 int rw;
1208
1209 bio_list_init(&bio_list_on_stack);
1210
1211 spin_lock_irq(q->queue_lock);
Tejun Heoc5cc2072013-05-14 13:52:38 -07001212 for (rw = READ; rw <= WRITE; rw++)
1213 while ((bio = throtl_pop_queued(&td_sq->queued[rw], NULL)))
1214 bio_list_add(&bio_list_on_stack, bio);
Vivek Goyale43473b2010-09-15 17:06:35 -04001215 spin_unlock_irq(q->queue_lock);
1216
Tejun Heo6e1a5702013-05-14 13:52:37 -07001217 if (!bio_list_empty(&bio_list_on_stack)) {
Vivek Goyal69d60eb2011-03-09 08:27:37 +01001218 blk_start_plug(&plug);
Vivek Goyale43473b2010-09-15 17:06:35 -04001219 while((bio = bio_list_pop(&bio_list_on_stack)))
1220 generic_make_request(bio);
Vivek Goyal69d60eb2011-03-09 08:27:37 +01001221 blk_finish_plug(&plug);
Vivek Goyale43473b2010-09-15 17:06:35 -04001222 }
Vivek Goyale43473b2010-09-15 17:06:35 -04001223}
1224
Tejun Heof95a04a2012-04-16 13:57:26 -07001225static u64 tg_prfill_conf_u64(struct seq_file *sf, struct blkg_policy_data *pd,
1226 int off)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001227{
Tejun Heof95a04a2012-04-16 13:57:26 -07001228 struct throtl_grp *tg = pd_to_tg(pd);
1229 u64 v = *(u64 *)((void *)tg + off);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001230
Shaohua Li2ab54922017-03-27 10:51:29 -07001231 if (v == U64_MAX)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001232 return 0;
Tejun Heof95a04a2012-04-16 13:57:26 -07001233 return __blkg_prfill_u64(sf, pd, v);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001234}
1235
Tejun Heof95a04a2012-04-16 13:57:26 -07001236static u64 tg_prfill_conf_uint(struct seq_file *sf, struct blkg_policy_data *pd,
1237 int off)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001238{
Tejun Heof95a04a2012-04-16 13:57:26 -07001239 struct throtl_grp *tg = pd_to_tg(pd);
1240 unsigned int v = *(unsigned int *)((void *)tg + off);
Tejun Heoaf133ce2012-04-01 14:38:44 -07001241
Shaohua Li2ab54922017-03-27 10:51:29 -07001242 if (v == UINT_MAX)
Tejun Heoaf133ce2012-04-01 14:38:44 -07001243 return 0;
Tejun Heof95a04a2012-04-16 13:57:26 -07001244 return __blkg_prfill_u64(sf, pd, v);
Tejun Heoaf133ce2012-04-01 14:38:44 -07001245}
1246
Tejun Heo2da8ca82013-12-05 12:28:04 -05001247static int tg_print_conf_u64(struct seq_file *sf, void *v)
Tejun Heoaf133ce2012-04-01 14:38:44 -07001248{
Tejun Heo2da8ca82013-12-05 12:28:04 -05001249 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), tg_prfill_conf_u64,
1250 &blkcg_policy_throtl, seq_cft(sf)->private, false);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001251 return 0;
1252}
1253
Tejun Heo2da8ca82013-12-05 12:28:04 -05001254static int tg_print_conf_uint(struct seq_file *sf, void *v)
Vivek Goyale43473b2010-09-15 17:06:35 -04001255{
Tejun Heo2da8ca82013-12-05 12:28:04 -05001256 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), tg_prfill_conf_uint,
1257 &blkcg_policy_throtl, seq_cft(sf)->private, false);
Tejun Heoaf133ce2012-04-01 14:38:44 -07001258 return 0;
Vivek Goyale43473b2010-09-15 17:06:35 -04001259}
1260
Tejun Heo69948b02015-08-18 14:55:32 -07001261static void tg_conf_updated(struct throtl_grp *tg)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001262{
Tejun Heo69948b02015-08-18 14:55:32 -07001263 struct throtl_service_queue *sq = &tg->service_queue;
Tejun Heo492eb212013-08-08 20:11:25 -04001264 struct cgroup_subsys_state *pos_css;
Tejun Heo69948b02015-08-18 14:55:32 -07001265 struct blkcg_gq *blkg;
Tejun Heoaf133ce2012-04-01 14:38:44 -07001266
Tejun Heofda6f272013-05-14 13:52:36 -07001267 throtl_log(&tg->service_queue,
1268 "limit change rbps=%llu wbps=%llu riops=%u wiops=%u",
Shaohua Li9f626e32017-03-27 10:51:30 -07001269 tg_bps_limit(tg, READ), tg_bps_limit(tg, WRITE),
1270 tg_iops_limit(tg, READ), tg_iops_limit(tg, WRITE));
Tejun Heo632b4492013-05-14 13:52:31 -07001271
1272 /*
Tejun Heo693e7512013-05-14 13:52:38 -07001273 * Update has_rules[] flags for the updated tg's subtree. A tg is
1274 * considered to have rules if either the tg itself or any of its
1275 * ancestors has rules. This identifies groups without any
1276 * restrictions in the whole hierarchy and allows them to bypass
1277 * blk-throttle.
1278 */
Tejun Heo69948b02015-08-18 14:55:32 -07001279 blkg_for_each_descendant_pre(blkg, pos_css, tg_to_blkg(tg))
Tejun Heo693e7512013-05-14 13:52:38 -07001280 tg_update_has_rules(blkg_to_tg(blkg));
1281
1282 /*
Tejun Heo632b4492013-05-14 13:52:31 -07001283 * We're already holding queue_lock and know @tg is valid. Let's
1284 * apply the new config directly.
1285 *
1286 * Restart the slices for both READ and WRITES. It might happen
1287 * that a group's limit are dropped suddenly and we don't want to
1288 * account recently dispatched IO with new low rate.
1289 */
Tejun Heo0f3457f2013-05-14 13:52:32 -07001290 throtl_start_new_slice(tg, 0);
1291 throtl_start_new_slice(tg, 1);
Tejun Heo632b4492013-05-14 13:52:31 -07001292
Tejun Heo5b2c16a2013-05-14 13:52:32 -07001293 if (tg->flags & THROTL_TG_PENDING) {
Tejun Heo77216b02013-05-14 13:52:36 -07001294 tg_update_disptime(tg);
Tejun Heo7f52f982013-05-14 13:52:37 -07001295 throtl_schedule_next_dispatch(sq->parent_sq, true);
Tejun Heo632b4492013-05-14 13:52:31 -07001296 }
Tejun Heo69948b02015-08-18 14:55:32 -07001297}
Tejun Heo60c2bc22012-04-01 14:38:43 -07001298
Tejun Heo69948b02015-08-18 14:55:32 -07001299static ssize_t tg_set_conf(struct kernfs_open_file *of,
1300 char *buf, size_t nbytes, loff_t off, bool is_u64)
1301{
1302 struct blkcg *blkcg = css_to_blkcg(of_css(of));
1303 struct blkg_conf_ctx ctx;
1304 struct throtl_grp *tg;
1305 int ret;
1306 u64 v;
1307
1308 ret = blkg_conf_prep(blkcg, &blkcg_policy_throtl, buf, &ctx);
1309 if (ret)
1310 return ret;
1311
1312 ret = -EINVAL;
1313 if (sscanf(ctx.body, "%llu", &v) != 1)
1314 goto out_finish;
1315 if (!v)
Shaohua Li2ab54922017-03-27 10:51:29 -07001316 v = U64_MAX;
Tejun Heo69948b02015-08-18 14:55:32 -07001317
1318 tg = blkg_to_tg(ctx.blkg);
1319
1320 if (is_u64)
1321 *(u64 *)((void *)tg + of_cft(of)->private) = v;
1322 else
1323 *(unsigned int *)((void *)tg + of_cft(of)->private) = v;
1324
1325 tg_conf_updated(tg);
Tejun Heo36aa9e52015-08-18 14:55:31 -07001326 ret = 0;
1327out_finish:
Tejun Heo60c2bc22012-04-01 14:38:43 -07001328 blkg_conf_finish(&ctx);
Tejun Heo36aa9e52015-08-18 14:55:31 -07001329 return ret ?: nbytes;
Tejun Heo60c2bc22012-04-01 14:38:43 -07001330}
1331
Tejun Heo451af502014-05-13 12:16:21 -04001332static ssize_t tg_set_conf_u64(struct kernfs_open_file *of,
1333 char *buf, size_t nbytes, loff_t off)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001334{
Tejun Heo451af502014-05-13 12:16:21 -04001335 return tg_set_conf(of, buf, nbytes, off, true);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001336}
1337
Tejun Heo451af502014-05-13 12:16:21 -04001338static ssize_t tg_set_conf_uint(struct kernfs_open_file *of,
1339 char *buf, size_t nbytes, loff_t off)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001340{
Tejun Heo451af502014-05-13 12:16:21 -04001341 return tg_set_conf(of, buf, nbytes, off, false);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001342}
1343
Tejun Heo880f50e2015-08-18 14:55:30 -07001344static struct cftype throtl_legacy_files[] = {
Tejun Heo60c2bc22012-04-01 14:38:43 -07001345 {
1346 .name = "throttle.read_bps_device",
Shaohua Li9f626e32017-03-27 10:51:30 -07001347 .private = offsetof(struct throtl_grp, bps[READ][LIMIT_MAX]),
Tejun Heo2da8ca82013-12-05 12:28:04 -05001348 .seq_show = tg_print_conf_u64,
Tejun Heo451af502014-05-13 12:16:21 -04001349 .write = tg_set_conf_u64,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001350 },
1351 {
1352 .name = "throttle.write_bps_device",
Shaohua Li9f626e32017-03-27 10:51:30 -07001353 .private = offsetof(struct throtl_grp, bps[WRITE][LIMIT_MAX]),
Tejun Heo2da8ca82013-12-05 12:28:04 -05001354 .seq_show = tg_print_conf_u64,
Tejun Heo451af502014-05-13 12:16:21 -04001355 .write = tg_set_conf_u64,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001356 },
1357 {
1358 .name = "throttle.read_iops_device",
Shaohua Li9f626e32017-03-27 10:51:30 -07001359 .private = offsetof(struct throtl_grp, iops[READ][LIMIT_MAX]),
Tejun Heo2da8ca82013-12-05 12:28:04 -05001360 .seq_show = tg_print_conf_uint,
Tejun Heo451af502014-05-13 12:16:21 -04001361 .write = tg_set_conf_uint,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001362 },
1363 {
1364 .name = "throttle.write_iops_device",
Shaohua Li9f626e32017-03-27 10:51:30 -07001365 .private = offsetof(struct throtl_grp, iops[WRITE][LIMIT_MAX]),
Tejun Heo2da8ca82013-12-05 12:28:04 -05001366 .seq_show = tg_print_conf_uint,
Tejun Heo451af502014-05-13 12:16:21 -04001367 .write = tg_set_conf_uint,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001368 },
1369 {
1370 .name = "throttle.io_service_bytes",
Tejun Heo77ea7332015-08-18 14:55:24 -07001371 .private = (unsigned long)&blkcg_policy_throtl,
1372 .seq_show = blkg_print_stat_bytes,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001373 },
1374 {
1375 .name = "throttle.io_serviced",
Tejun Heo77ea7332015-08-18 14:55:24 -07001376 .private = (unsigned long)&blkcg_policy_throtl,
1377 .seq_show = blkg_print_stat_ios,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001378 },
1379 { } /* terminate */
1380};
1381
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001382static u64 tg_prfill_limit(struct seq_file *sf, struct blkg_policy_data *pd,
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001383 int off)
1384{
1385 struct throtl_grp *tg = pd_to_tg(pd);
1386 const char *dname = blkg_dev_name(pd->blkg);
1387 char bufs[4][21] = { "max", "max", "max", "max" };
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001388 u64 bps_dft;
1389 unsigned int iops_dft;
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001390
1391 if (!dname)
1392 return 0;
Shaohua Li9f626e32017-03-27 10:51:30 -07001393
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001394 if (off == LIMIT_LOW) {
1395 bps_dft = 0;
1396 iops_dft = 0;
1397 } else {
1398 bps_dft = U64_MAX;
1399 iops_dft = UINT_MAX;
1400 }
1401
1402 if (tg->bps_conf[READ][off] == bps_dft &&
1403 tg->bps_conf[WRITE][off] == bps_dft &&
1404 tg->iops_conf[READ][off] == iops_dft &&
1405 tg->iops_conf[WRITE][off] == iops_dft)
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001406 return 0;
1407
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001408 if (tg->bps_conf[READ][off] != bps_dft)
Shaohua Li9f626e32017-03-27 10:51:30 -07001409 snprintf(bufs[0], sizeof(bufs[0]), "%llu",
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001410 tg->bps_conf[READ][off]);
1411 if (tg->bps_conf[WRITE][off] != bps_dft)
Shaohua Li9f626e32017-03-27 10:51:30 -07001412 snprintf(bufs[1], sizeof(bufs[1]), "%llu",
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001413 tg->bps_conf[WRITE][off]);
1414 if (tg->iops_conf[READ][off] != iops_dft)
Shaohua Li9f626e32017-03-27 10:51:30 -07001415 snprintf(bufs[2], sizeof(bufs[2]), "%u",
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001416 tg->iops_conf[READ][off]);
1417 if (tg->iops_conf[WRITE][off] != iops_dft)
Shaohua Li9f626e32017-03-27 10:51:30 -07001418 snprintf(bufs[3], sizeof(bufs[3]), "%u",
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001419 tg->iops_conf[WRITE][off]);
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001420
1421 seq_printf(sf, "%s rbps=%s wbps=%s riops=%s wiops=%s\n",
1422 dname, bufs[0], bufs[1], bufs[2], bufs[3]);
1423 return 0;
1424}
1425
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001426static int tg_print_limit(struct seq_file *sf, void *v)
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001427{
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001428 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), tg_prfill_limit,
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001429 &blkcg_policy_throtl, seq_cft(sf)->private, false);
1430 return 0;
1431}
1432
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001433static ssize_t tg_set_limit(struct kernfs_open_file *of,
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001434 char *buf, size_t nbytes, loff_t off)
1435{
1436 struct blkcg *blkcg = css_to_blkcg(of_css(of));
1437 struct blkg_conf_ctx ctx;
1438 struct throtl_grp *tg;
1439 u64 v[4];
1440 int ret;
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001441 int index = of_cft(of)->private;
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001442
1443 ret = blkg_conf_prep(blkcg, &blkcg_policy_throtl, buf, &ctx);
1444 if (ret)
1445 return ret;
1446
1447 tg = blkg_to_tg(ctx.blkg);
1448
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001449 v[0] = tg->bps_conf[READ][index];
1450 v[1] = tg->bps_conf[WRITE][index];
1451 v[2] = tg->iops_conf[READ][index];
1452 v[3] = tg->iops_conf[WRITE][index];
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001453
1454 while (true) {
1455 char tok[27]; /* wiops=18446744073709551616 */
1456 char *p;
Shaohua Li2ab54922017-03-27 10:51:29 -07001457 u64 val = U64_MAX;
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001458 int len;
1459
1460 if (sscanf(ctx.body, "%26s%n", tok, &len) != 1)
1461 break;
1462 if (tok[0] == '\0')
1463 break;
1464 ctx.body += len;
1465
1466 ret = -EINVAL;
1467 p = tok;
1468 strsep(&p, "=");
1469 if (!p || (sscanf(p, "%llu", &val) != 1 && strcmp(p, "max")))
1470 goto out_finish;
1471
1472 ret = -ERANGE;
1473 if (!val)
1474 goto out_finish;
1475
1476 ret = -EINVAL;
1477 if (!strcmp(tok, "rbps"))
1478 v[0] = val;
1479 else if (!strcmp(tok, "wbps"))
1480 v[1] = val;
1481 else if (!strcmp(tok, "riops"))
1482 v[2] = min_t(u64, val, UINT_MAX);
1483 else if (!strcmp(tok, "wiops"))
1484 v[3] = min_t(u64, val, UINT_MAX);
1485 else
1486 goto out_finish;
1487 }
1488
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001489 tg->bps_conf[READ][index] = v[0];
1490 tg->bps_conf[WRITE][index] = v[1];
1491 tg->iops_conf[READ][index] = v[2];
1492 tg->iops_conf[WRITE][index] = v[3];
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001493
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001494 if (index == LIMIT_MAX) {
1495 tg->bps[READ][index] = v[0];
1496 tg->bps[WRITE][index] = v[1];
1497 tg->iops[READ][index] = v[2];
1498 tg->iops[WRITE][index] = v[3];
1499 }
1500 tg->bps[READ][LIMIT_LOW] = min(tg->bps_conf[READ][LIMIT_LOW],
1501 tg->bps_conf[READ][LIMIT_MAX]);
1502 tg->bps[WRITE][LIMIT_LOW] = min(tg->bps_conf[WRITE][LIMIT_LOW],
1503 tg->bps_conf[WRITE][LIMIT_MAX]);
1504 tg->iops[READ][LIMIT_LOW] = min(tg->iops_conf[READ][LIMIT_LOW],
1505 tg->iops_conf[READ][LIMIT_MAX]);
1506 tg->iops[WRITE][LIMIT_LOW] = min(tg->iops_conf[WRITE][LIMIT_LOW],
1507 tg->iops_conf[WRITE][LIMIT_MAX]);
1508
1509 if (index == LIMIT_LOW) {
1510 blk_throtl_update_limit_valid(tg->td);
1511 if (tg->td->limit_valid[LIMIT_LOW])
1512 tg->td->limit_index = LIMIT_LOW;
1513 }
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001514 tg_conf_updated(tg);
1515 ret = 0;
1516out_finish:
1517 blkg_conf_finish(&ctx);
1518 return ret ?: nbytes;
1519}
1520
1521static struct cftype throtl_files[] = {
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001522#ifdef CONFIG_BLK_DEV_THROTTLING_LOW
1523 {
1524 .name = "low",
1525 .flags = CFTYPE_NOT_ON_ROOT,
1526 .seq_show = tg_print_limit,
1527 .write = tg_set_limit,
1528 .private = LIMIT_LOW,
1529 },
1530#endif
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001531 {
1532 .name = "max",
1533 .flags = CFTYPE_NOT_ON_ROOT,
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001534 .seq_show = tg_print_limit,
1535 .write = tg_set_limit,
1536 .private = LIMIT_MAX,
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001537 },
1538 { } /* terminate */
1539};
1540
Vivek Goyalda527772011-03-02 19:05:33 -05001541static void throtl_shutdown_wq(struct request_queue *q)
Vivek Goyale43473b2010-09-15 17:06:35 -04001542{
1543 struct throtl_data *td = q->td;
1544
Tejun Heo69df0ab2013-05-14 13:52:36 -07001545 cancel_work_sync(&td->dispatch_work);
Vivek Goyale43473b2010-09-15 17:06:35 -04001546}
1547
Tejun Heo3c798392012-04-16 13:57:25 -07001548static struct blkcg_policy blkcg_policy_throtl = {
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001549 .dfl_cftypes = throtl_files,
Tejun Heo880f50e2015-08-18 14:55:30 -07001550 .legacy_cftypes = throtl_legacy_files,
Tejun Heof9fcc2d2012-04-16 13:57:27 -07001551
Tejun Heo001bea72015-08-18 14:55:11 -07001552 .pd_alloc_fn = throtl_pd_alloc,
Tejun Heof9fcc2d2012-04-16 13:57:27 -07001553 .pd_init_fn = throtl_pd_init,
Tejun Heo693e7512013-05-14 13:52:38 -07001554 .pd_online_fn = throtl_pd_online,
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001555 .pd_offline_fn = throtl_pd_offline,
Tejun Heo001bea72015-08-18 14:55:11 -07001556 .pd_free_fn = throtl_pd_free,
Vivek Goyale43473b2010-09-15 17:06:35 -04001557};
1558
Shaohua Li3f0abd82017-03-27 10:51:35 -07001559static unsigned long __tg_last_low_overflow_time(struct throtl_grp *tg)
1560{
1561 unsigned long rtime = jiffies, wtime = jiffies;
1562
1563 if (tg->bps[READ][LIMIT_LOW] || tg->iops[READ][LIMIT_LOW])
1564 rtime = tg->last_low_overflow_time[READ];
1565 if (tg->bps[WRITE][LIMIT_LOW] || tg->iops[WRITE][LIMIT_LOW])
1566 wtime = tg->last_low_overflow_time[WRITE];
1567 return min(rtime, wtime);
1568}
1569
1570/* tg should not be an intermediate node */
1571static unsigned long tg_last_low_overflow_time(struct throtl_grp *tg)
1572{
1573 struct throtl_service_queue *parent_sq;
1574 struct throtl_grp *parent = tg;
1575 unsigned long ret = __tg_last_low_overflow_time(tg);
1576
1577 while (true) {
1578 parent_sq = parent->service_queue.parent_sq;
1579 parent = sq_to_tg(parent_sq);
1580 if (!parent)
1581 break;
1582
1583 /*
1584 * The parent doesn't have low limit, it always reaches low
1585 * limit. Its overflow time is useless for children
1586 */
1587 if (!parent->bps[READ][LIMIT_LOW] &&
1588 !parent->iops[READ][LIMIT_LOW] &&
1589 !parent->bps[WRITE][LIMIT_LOW] &&
1590 !parent->iops[WRITE][LIMIT_LOW])
1591 continue;
1592 if (time_after(__tg_last_low_overflow_time(parent), ret))
1593 ret = __tg_last_low_overflow_time(parent);
1594 }
1595 return ret;
1596}
1597
Shaohua Lic79892c2017-03-27 10:51:34 -07001598static bool throtl_tg_can_upgrade(struct throtl_grp *tg)
1599{
1600 struct throtl_service_queue *sq = &tg->service_queue;
1601 bool read_limit, write_limit;
1602
1603 /*
1604 * if cgroup reaches low limit (if low limit is 0, the cgroup always
1605 * reaches), it's ok to upgrade to next limit
1606 */
1607 read_limit = tg->bps[READ][LIMIT_LOW] || tg->iops[READ][LIMIT_LOW];
1608 write_limit = tg->bps[WRITE][LIMIT_LOW] || tg->iops[WRITE][LIMIT_LOW];
1609 if (!read_limit && !write_limit)
1610 return true;
1611 if (read_limit && sq->nr_queued[READ] &&
1612 (!write_limit || sq->nr_queued[WRITE]))
1613 return true;
1614 if (write_limit && sq->nr_queued[WRITE] &&
1615 (!read_limit || sq->nr_queued[READ]))
1616 return true;
1617 return false;
1618}
1619
1620static bool throtl_hierarchy_can_upgrade(struct throtl_grp *tg)
1621{
1622 while (true) {
1623 if (throtl_tg_can_upgrade(tg))
1624 return true;
1625 tg = sq_to_tg(tg->service_queue.parent_sq);
1626 if (!tg || !tg_to_blkg(tg)->parent)
1627 return false;
1628 }
1629 return false;
1630}
1631
1632static bool throtl_can_upgrade(struct throtl_data *td,
1633 struct throtl_grp *this_tg)
1634{
1635 struct cgroup_subsys_state *pos_css;
1636 struct blkcg_gq *blkg;
1637
1638 if (td->limit_index != LIMIT_LOW)
1639 return false;
1640
Shaohua Li297e3d82017-03-27 10:51:37 -07001641 if (time_before(jiffies, td->low_downgrade_time + td->throtl_slice))
Shaohua Li3f0abd82017-03-27 10:51:35 -07001642 return false;
1643
Shaohua Lic79892c2017-03-27 10:51:34 -07001644 rcu_read_lock();
1645 blkg_for_each_descendant_post(blkg, pos_css, td->queue->root_blkg) {
1646 struct throtl_grp *tg = blkg_to_tg(blkg);
1647
1648 if (tg == this_tg)
1649 continue;
1650 if (!list_empty(&tg_to_blkg(tg)->blkcg->css.children))
1651 continue;
1652 if (!throtl_hierarchy_can_upgrade(tg)) {
1653 rcu_read_unlock();
1654 return false;
1655 }
1656 }
1657 rcu_read_unlock();
1658 return true;
1659}
1660
1661static void throtl_upgrade_state(struct throtl_data *td)
1662{
1663 struct cgroup_subsys_state *pos_css;
1664 struct blkcg_gq *blkg;
1665
1666 td->limit_index = LIMIT_MAX;
Shaohua Li3f0abd82017-03-27 10:51:35 -07001667 td->low_upgrade_time = jiffies;
Shaohua Lic79892c2017-03-27 10:51:34 -07001668 rcu_read_lock();
1669 blkg_for_each_descendant_post(blkg, pos_css, td->queue->root_blkg) {
1670 struct throtl_grp *tg = blkg_to_tg(blkg);
1671 struct throtl_service_queue *sq = &tg->service_queue;
1672
1673 tg->disptime = jiffies - 1;
1674 throtl_select_dispatch(sq);
1675 throtl_schedule_next_dispatch(sq, false);
1676 }
1677 rcu_read_unlock();
1678 throtl_select_dispatch(&td->service_queue);
1679 throtl_schedule_next_dispatch(&td->service_queue, false);
1680 queue_work(kthrotld_workqueue, &td->dispatch_work);
1681}
1682
Shaohua Li3f0abd82017-03-27 10:51:35 -07001683static void throtl_downgrade_state(struct throtl_data *td, int new)
1684{
1685 td->limit_index = new;
1686 td->low_downgrade_time = jiffies;
1687}
1688
1689static bool throtl_tg_can_downgrade(struct throtl_grp *tg)
1690{
1691 struct throtl_data *td = tg->td;
1692 unsigned long now = jiffies;
1693
1694 /*
1695 * If cgroup is below low limit, consider downgrade and throttle other
1696 * cgroups
1697 */
Shaohua Li297e3d82017-03-27 10:51:37 -07001698 if (time_after_eq(now, td->low_upgrade_time + td->throtl_slice) &&
1699 time_after_eq(now, tg_last_low_overflow_time(tg) +
1700 td->throtl_slice))
Shaohua Li3f0abd82017-03-27 10:51:35 -07001701 return true;
1702 return false;
1703}
1704
1705static bool throtl_hierarchy_can_downgrade(struct throtl_grp *tg)
1706{
1707 while (true) {
1708 if (!throtl_tg_can_downgrade(tg))
1709 return false;
1710 tg = sq_to_tg(tg->service_queue.parent_sq);
1711 if (!tg || !tg_to_blkg(tg)->parent)
1712 break;
1713 }
1714 return true;
1715}
1716
1717static void throtl_downgrade_check(struct throtl_grp *tg)
1718{
1719 uint64_t bps;
1720 unsigned int iops;
1721 unsigned long elapsed_time;
1722 unsigned long now = jiffies;
1723
1724 if (tg->td->limit_index != LIMIT_MAX ||
1725 !tg->td->limit_valid[LIMIT_LOW])
1726 return;
1727 if (!list_empty(&tg_to_blkg(tg)->blkcg->css.children))
1728 return;
Shaohua Li297e3d82017-03-27 10:51:37 -07001729 if (time_after(tg->last_check_time + tg->td->throtl_slice, now))
Shaohua Li3f0abd82017-03-27 10:51:35 -07001730 return;
1731
1732 elapsed_time = now - tg->last_check_time;
1733 tg->last_check_time = now;
1734
Shaohua Li297e3d82017-03-27 10:51:37 -07001735 if (time_before(now, tg_last_low_overflow_time(tg) +
1736 tg->td->throtl_slice))
Shaohua Li3f0abd82017-03-27 10:51:35 -07001737 return;
1738
1739 if (tg->bps[READ][LIMIT_LOW]) {
1740 bps = tg->last_bytes_disp[READ] * HZ;
1741 do_div(bps, elapsed_time);
1742 if (bps >= tg->bps[READ][LIMIT_LOW])
1743 tg->last_low_overflow_time[READ] = now;
1744 }
1745
1746 if (tg->bps[WRITE][LIMIT_LOW]) {
1747 bps = tg->last_bytes_disp[WRITE] * HZ;
1748 do_div(bps, elapsed_time);
1749 if (bps >= tg->bps[WRITE][LIMIT_LOW])
1750 tg->last_low_overflow_time[WRITE] = now;
1751 }
1752
1753 if (tg->iops[READ][LIMIT_LOW]) {
1754 iops = tg->last_io_disp[READ] * HZ / elapsed_time;
1755 if (iops >= tg->iops[READ][LIMIT_LOW])
1756 tg->last_low_overflow_time[READ] = now;
1757 }
1758
1759 if (tg->iops[WRITE][LIMIT_LOW]) {
1760 iops = tg->last_io_disp[WRITE] * HZ / elapsed_time;
1761 if (iops >= tg->iops[WRITE][LIMIT_LOW])
1762 tg->last_low_overflow_time[WRITE] = now;
1763 }
1764
1765 /*
1766 * If cgroup is below low limit, consider downgrade and throttle other
1767 * cgroups
1768 */
1769 if (throtl_hierarchy_can_downgrade(tg))
1770 throtl_downgrade_state(tg->td, LIMIT_LOW);
1771
1772 tg->last_bytes_disp[READ] = 0;
1773 tg->last_bytes_disp[WRITE] = 0;
1774 tg->last_io_disp[READ] = 0;
1775 tg->last_io_disp[WRITE] = 0;
1776}
1777
Tejun Heoae118892015-08-18 14:55:20 -07001778bool blk_throtl_bio(struct request_queue *q, struct blkcg_gq *blkg,
1779 struct bio *bio)
Vivek Goyale43473b2010-09-15 17:06:35 -04001780{
Tejun Heoc5cc2072013-05-14 13:52:38 -07001781 struct throtl_qnode *qn = NULL;
Tejun Heoae118892015-08-18 14:55:20 -07001782 struct throtl_grp *tg = blkg_to_tg(blkg ?: q->root_blkg);
Tejun Heo73f0d492013-05-14 13:52:35 -07001783 struct throtl_service_queue *sq;
Tejun Heo0e9f4162013-05-14 13:52:35 -07001784 bool rw = bio_data_dir(bio);
Tejun Heobc16a4f2011-10-19 14:33:01 +02001785 bool throttled = false;
Vivek Goyale43473b2010-09-15 17:06:35 -04001786
Tejun Heoae118892015-08-18 14:55:20 -07001787 WARN_ON_ONCE(!rcu_read_lock_held());
1788
Tejun Heo2a0f61e2013-05-14 13:52:36 -07001789 /* see throtl_charge_bio() */
Christoph Hellwig8d2bbd42016-10-20 15:12:12 +02001790 if (bio_flagged(bio, BIO_THROTTLED) || !tg->has_rules[rw])
Tejun Heobc16a4f2011-10-19 14:33:01 +02001791 goto out;
Vivek Goyale43473b2010-09-15 17:06:35 -04001792
1793 spin_lock_irq(q->queue_lock);
Tejun Heoc9589f02015-08-18 14:55:19 -07001794
1795 if (unlikely(blk_queue_bypass(q)))
Tejun Heobc16a4f2011-10-19 14:33:01 +02001796 goto out_unlock;
Vivek Goyalf469a7b2011-05-19 15:38:23 -04001797
Tejun Heo73f0d492013-05-14 13:52:35 -07001798 sq = &tg->service_queue;
1799
Shaohua Lic79892c2017-03-27 10:51:34 -07001800again:
Tejun Heo9e660ac2013-05-14 13:52:38 -07001801 while (true) {
Shaohua Li3f0abd82017-03-27 10:51:35 -07001802 if (tg->last_low_overflow_time[rw] == 0)
1803 tg->last_low_overflow_time[rw] = jiffies;
1804 throtl_downgrade_check(tg);
Tejun Heo9e660ac2013-05-14 13:52:38 -07001805 /* throtl is FIFO - if bios are already queued, should queue */
1806 if (sq->nr_queued[rw])
1807 break;
Vivek Goyalde701c72011-03-07 21:09:32 +01001808
Tejun Heo9e660ac2013-05-14 13:52:38 -07001809 /* if above limits, break to queue */
Shaohua Lic79892c2017-03-27 10:51:34 -07001810 if (!tg_may_dispatch(tg, bio, NULL)) {
Shaohua Li3f0abd82017-03-27 10:51:35 -07001811 tg->last_low_overflow_time[rw] = jiffies;
Shaohua Lic79892c2017-03-27 10:51:34 -07001812 if (throtl_can_upgrade(tg->td, tg)) {
1813 throtl_upgrade_state(tg->td);
1814 goto again;
1815 }
Tejun Heo9e660ac2013-05-14 13:52:38 -07001816 break;
Shaohua Lic79892c2017-03-27 10:51:34 -07001817 }
Tejun Heo9e660ac2013-05-14 13:52:38 -07001818
1819 /* within limits, let's charge and dispatch directly */
Vivek Goyale43473b2010-09-15 17:06:35 -04001820 throtl_charge_bio(tg, bio);
Vivek Goyal04521db2011-03-22 21:54:29 +01001821
1822 /*
1823 * We need to trim slice even when bios are not being queued
1824 * otherwise it might happen that a bio is not queued for
1825 * a long time and slice keeps on extending and trim is not
1826 * called for a long time. Now if limits are reduced suddenly
1827 * we take into account all the IO dispatched so far at new
1828 * low rate and * newly queued IO gets a really long dispatch
1829 * time.
1830 *
1831 * So keep on trimming slice even if bio is not queued.
1832 */
Tejun Heo0f3457f2013-05-14 13:52:32 -07001833 throtl_trim_slice(tg, rw);
Tejun Heo9e660ac2013-05-14 13:52:38 -07001834
1835 /*
1836 * @bio passed through this layer without being throttled.
1837 * Climb up the ladder. If we''re already at the top, it
1838 * can be executed directly.
1839 */
Tejun Heoc5cc2072013-05-14 13:52:38 -07001840 qn = &tg->qnode_on_parent[rw];
Tejun Heo9e660ac2013-05-14 13:52:38 -07001841 sq = sq->parent_sq;
1842 tg = sq_to_tg(sq);
1843 if (!tg)
1844 goto out_unlock;
Vivek Goyale43473b2010-09-15 17:06:35 -04001845 }
1846
Tejun Heo9e660ac2013-05-14 13:52:38 -07001847 /* out-of-limit, queue to @tg */
Tejun Heofda6f272013-05-14 13:52:36 -07001848 throtl_log(sq, "[%c] bio. bdisp=%llu sz=%u bps=%llu iodisp=%u iops=%u queued=%d/%d",
1849 rw == READ ? 'R' : 'W',
Shaohua Li9f626e32017-03-27 10:51:30 -07001850 tg->bytes_disp[rw], bio->bi_iter.bi_size,
1851 tg_bps_limit(tg, rw),
1852 tg->io_disp[rw], tg_iops_limit(tg, rw),
Tejun Heofda6f272013-05-14 13:52:36 -07001853 sq->nr_queued[READ], sq->nr_queued[WRITE]);
Vivek Goyale43473b2010-09-15 17:06:35 -04001854
Shaohua Li3f0abd82017-03-27 10:51:35 -07001855 tg->last_low_overflow_time[rw] = jiffies;
1856
Tejun Heo671058f2012-03-05 13:15:29 -08001857 bio_associate_current(bio);
Tejun Heo6bc9c2b2013-05-14 13:52:38 -07001858 tg->td->nr_queued[rw]++;
Tejun Heoc5cc2072013-05-14 13:52:38 -07001859 throtl_add_bio_tg(bio, qn, tg);
Tejun Heobc16a4f2011-10-19 14:33:01 +02001860 throttled = true;
Vivek Goyale43473b2010-09-15 17:06:35 -04001861
Tejun Heo7f52f982013-05-14 13:52:37 -07001862 /*
1863 * Update @tg's dispatch time and force schedule dispatch if @tg
1864 * was empty before @bio. The forced scheduling isn't likely to
1865 * cause undue delay as @bio is likely to be dispatched directly if
1866 * its @tg's disptime is not in the future.
1867 */
Tejun Heo0e9f4162013-05-14 13:52:35 -07001868 if (tg->flags & THROTL_TG_WAS_EMPTY) {
Tejun Heo77216b02013-05-14 13:52:36 -07001869 tg_update_disptime(tg);
Tejun Heo7f52f982013-05-14 13:52:37 -07001870 throtl_schedule_next_dispatch(tg->service_queue.parent_sq, true);
Vivek Goyale43473b2010-09-15 17:06:35 -04001871 }
1872
Tejun Heobc16a4f2011-10-19 14:33:01 +02001873out_unlock:
Vivek Goyale43473b2010-09-15 17:06:35 -04001874 spin_unlock_irq(q->queue_lock);
Tejun Heobc16a4f2011-10-19 14:33:01 +02001875out:
Tejun Heo2a0f61e2013-05-14 13:52:36 -07001876 /*
1877 * As multiple blk-throtls may stack in the same issue path, we
1878 * don't want bios to leave with the flag set. Clear the flag if
1879 * being issued.
1880 */
1881 if (!throttled)
Christoph Hellwig8d2bbd42016-10-20 15:12:12 +02001882 bio_clear_flag(bio, BIO_THROTTLED);
Tejun Heobc16a4f2011-10-19 14:33:01 +02001883 return throttled;
Vivek Goyale43473b2010-09-15 17:06:35 -04001884}
1885
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001886/*
1887 * Dispatch all bios from all children tg's queued on @parent_sq. On
1888 * return, @parent_sq is guaranteed to not have any active children tg's
1889 * and all bios from previously active tg's are on @parent_sq->bio_lists[].
1890 */
1891static void tg_drain_bios(struct throtl_service_queue *parent_sq)
1892{
1893 struct throtl_grp *tg;
1894
1895 while ((tg = throtl_rb_first(parent_sq))) {
1896 struct throtl_service_queue *sq = &tg->service_queue;
1897 struct bio *bio;
1898
1899 throtl_dequeue_tg(tg);
1900
Tejun Heoc5cc2072013-05-14 13:52:38 -07001901 while ((bio = throtl_peek_queued(&sq->queued[READ])))
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001902 tg_dispatch_one_bio(tg, bio_data_dir(bio));
Tejun Heoc5cc2072013-05-14 13:52:38 -07001903 while ((bio = throtl_peek_queued(&sq->queued[WRITE])))
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001904 tg_dispatch_one_bio(tg, bio_data_dir(bio));
1905 }
1906}
1907
Tejun Heoc9a929d2011-10-19 14:42:16 +02001908/**
1909 * blk_throtl_drain - drain throttled bios
1910 * @q: request_queue to drain throttled bios for
1911 *
1912 * Dispatch all currently throttled bios on @q through ->make_request_fn().
1913 */
1914void blk_throtl_drain(struct request_queue *q)
1915 __releases(q->queue_lock) __acquires(q->queue_lock)
1916{
1917 struct throtl_data *td = q->td;
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001918 struct blkcg_gq *blkg;
Tejun Heo492eb212013-08-08 20:11:25 -04001919 struct cgroup_subsys_state *pos_css;
Tejun Heoc9a929d2011-10-19 14:42:16 +02001920 struct bio *bio;
Tejun Heo651930b2013-05-14 13:52:35 -07001921 int rw;
Tejun Heoc9a929d2011-10-19 14:42:16 +02001922
Andi Kleen8bcb6c72012-03-30 12:33:28 +02001923 queue_lockdep_assert_held(q);
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001924 rcu_read_lock();
Tejun Heoc9a929d2011-10-19 14:42:16 +02001925
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001926 /*
1927 * Drain each tg while doing post-order walk on the blkg tree, so
1928 * that all bios are propagated to td->service_queue. It'd be
1929 * better to walk service_queue tree directly but blkg walk is
1930 * easier.
1931 */
Tejun Heo492eb212013-08-08 20:11:25 -04001932 blkg_for_each_descendant_post(blkg, pos_css, td->queue->root_blkg)
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001933 tg_drain_bios(&blkg_to_tg(blkg)->service_queue);
Tejun Heo73f0d492013-05-14 13:52:35 -07001934
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001935 /* finally, transfer bios from top-level tg's into the td */
1936 tg_drain_bios(&td->service_queue);
1937
1938 rcu_read_unlock();
Tejun Heoc9a929d2011-10-19 14:42:16 +02001939 spin_unlock_irq(q->queue_lock);
1940
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001941 /* all bios now should be in td->service_queue, issue them */
Tejun Heo651930b2013-05-14 13:52:35 -07001942 for (rw = READ; rw <= WRITE; rw++)
Tejun Heoc5cc2072013-05-14 13:52:38 -07001943 while ((bio = throtl_pop_queued(&td->service_queue.queued[rw],
1944 NULL)))
Tejun Heo651930b2013-05-14 13:52:35 -07001945 generic_make_request(bio);
Tejun Heoc9a929d2011-10-19 14:42:16 +02001946
1947 spin_lock_irq(q->queue_lock);
1948}
1949
Vivek Goyale43473b2010-09-15 17:06:35 -04001950int blk_throtl_init(struct request_queue *q)
1951{
1952 struct throtl_data *td;
Tejun Heoa2b16932012-04-13 13:11:33 -07001953 int ret;
Vivek Goyale43473b2010-09-15 17:06:35 -04001954
1955 td = kzalloc_node(sizeof(*td), GFP_KERNEL, q->node);
1956 if (!td)
1957 return -ENOMEM;
1958
Tejun Heo69df0ab2013-05-14 13:52:36 -07001959 INIT_WORK(&td->dispatch_work, blk_throtl_dispatch_work_fn);
Tejun Heob2ce2642015-08-18 14:55:13 -07001960 throtl_service_queue_init(&td->service_queue);
Vivek Goyale43473b2010-09-15 17:06:35 -04001961
Tejun Heocd1604f2012-03-05 13:15:06 -08001962 q->td = td;
Vivek Goyal29b12582011-05-19 15:38:24 -04001963 td->queue = q;
Shaohua Li297e3d82017-03-27 10:51:37 -07001964 td->throtl_slice = DFL_THROTL_SLICE;
Vivek Goyal02977e42010-10-01 14:49:48 +02001965
Shaohua Li9f626e32017-03-27 10:51:30 -07001966 td->limit_valid[LIMIT_MAX] = true;
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001967 td->limit_index = LIMIT_MAX;
Shaohua Li3f0abd82017-03-27 10:51:35 -07001968 td->low_upgrade_time = jiffies;
1969 td->low_downgrade_time = jiffies;
Tejun Heoa2b16932012-04-13 13:11:33 -07001970 /* activate policy */
Tejun Heo3c798392012-04-16 13:57:25 -07001971 ret = blkcg_activate_policy(q, &blkcg_policy_throtl);
Tejun Heoa2b16932012-04-13 13:11:33 -07001972 if (ret)
Vivek Goyal29b12582011-05-19 15:38:24 -04001973 kfree(td);
Tejun Heoa2b16932012-04-13 13:11:33 -07001974 return ret;
Vivek Goyale43473b2010-09-15 17:06:35 -04001975}
1976
1977void blk_throtl_exit(struct request_queue *q)
1978{
Tejun Heoc875f4d2012-03-05 13:15:22 -08001979 BUG_ON(!q->td);
Vivek Goyalda527772011-03-02 19:05:33 -05001980 throtl_shutdown_wq(q);
Tejun Heo3c798392012-04-16 13:57:25 -07001981 blkcg_deactivate_policy(q, &blkcg_policy_throtl);
Tejun Heoc9a929d2011-10-19 14:42:16 +02001982 kfree(q->td);
Vivek Goyale43473b2010-09-15 17:06:35 -04001983}
1984
Shaohua Li297e3d82017-03-27 10:51:37 -07001985#ifdef CONFIG_BLK_DEV_THROTTLING_LOW
1986ssize_t blk_throtl_sample_time_show(struct request_queue *q, char *page)
1987{
1988 if (!q->td)
1989 return -EINVAL;
1990 return sprintf(page, "%u\n", jiffies_to_msecs(q->td->throtl_slice));
1991}
1992
1993ssize_t blk_throtl_sample_time_store(struct request_queue *q,
1994 const char *page, size_t count)
1995{
1996 unsigned long v;
1997 unsigned long t;
1998
1999 if (!q->td)
2000 return -EINVAL;
2001 if (kstrtoul(page, 10, &v))
2002 return -EINVAL;
2003 t = msecs_to_jiffies(v);
2004 if (t == 0 || t > MAX_THROTL_SLICE)
2005 return -EINVAL;
2006 q->td->throtl_slice = t;
2007 return count;
2008}
2009#endif
2010
Vivek Goyale43473b2010-09-15 17:06:35 -04002011static int __init throtl_init(void)
2012{
Vivek Goyal450adcb2011-03-01 13:40:54 -05002013 kthrotld_workqueue = alloc_workqueue("kthrotld", WQ_MEM_RECLAIM, 0);
2014 if (!kthrotld_workqueue)
2015 panic("Failed to create kthrotld\n");
2016
Tejun Heo3c798392012-04-16 13:57:25 -07002017 return blkcg_policy_register(&blkcg_policy_throtl);
Vivek Goyale43473b2010-09-15 17:06:35 -04002018}
2019
2020module_init(throtl_init);