blob: 6300f3ed70d2c1134b4fa94f7b9b235cc04fcae2 [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
Shaohua Lid61fcfa2017-03-27 10:51:38 -070021/* Throttling is performed over a slice and after that slice is renewed */
22#define DFL_THROTL_SLICE_HD (HZ / 10)
23#define DFL_THROTL_SLICE_SSD (HZ / 50)
Shaohua Li297e3d82017-03-27 10:51:37 -070024#define MAX_THROTL_SLICE (HZ)
Shaohua Li9e234ee2017-03-27 10:51:41 -070025#define DFL_IDLE_THRESHOLD_SSD (1000L) /* 1 ms */
26#define DFL_IDLE_THRESHOLD_HD (100L * 1000) /* 100 ms */
27#define MAX_IDLE_TIME (5L * 1000 * 1000) /* 5 s */
Vivek Goyale43473b2010-09-15 17:06:35 -040028
Tejun Heo3c798392012-04-16 13:57:25 -070029static struct blkcg_policy blkcg_policy_throtl;
Tejun Heo03814112012-03-05 13:15:14 -080030
Vivek Goyal450adcb2011-03-01 13:40:54 -050031/* A workqueue to queue throttle related work */
32static struct workqueue_struct *kthrotld_workqueue;
Vivek Goyal450adcb2011-03-01 13:40:54 -050033
Tejun Heoc5cc2072013-05-14 13:52:38 -070034/*
35 * To implement hierarchical throttling, throtl_grps form a tree and bios
36 * are dispatched upwards level by level until they reach the top and get
37 * issued. When dispatching bios from the children and local group at each
38 * level, if the bios are dispatched into a single bio_list, there's a risk
39 * of a local or child group which can queue many bios at once filling up
40 * the list starving others.
41 *
42 * To avoid such starvation, dispatched bios are queued separately
43 * according to where they came from. When they are again dispatched to
44 * the parent, they're popped in round-robin order so that no single source
45 * hogs the dispatch window.
46 *
47 * throtl_qnode is used to keep the queued bios separated by their sources.
48 * Bios are queued to throtl_qnode which in turn is queued to
49 * throtl_service_queue and then dispatched in round-robin order.
50 *
51 * It's also used to track the reference counts on blkg's. A qnode always
52 * belongs to a throtl_grp and gets queued on itself or the parent, so
53 * incrementing the reference of the associated throtl_grp when a qnode is
54 * queued and decrementing when dequeued is enough to keep the whole blkg
55 * tree pinned while bios are in flight.
56 */
57struct throtl_qnode {
58 struct list_head node; /* service_queue->queued[] */
59 struct bio_list bios; /* queued bios */
60 struct throtl_grp *tg; /* tg this qnode belongs to */
61};
62
Tejun Heoc9e03322013-05-14 13:52:32 -070063struct throtl_service_queue {
Tejun Heo77216b02013-05-14 13:52:36 -070064 struct throtl_service_queue *parent_sq; /* the parent service_queue */
65
Tejun Heo73f0d492013-05-14 13:52:35 -070066 /*
67 * Bios queued directly to this service_queue or dispatched from
68 * children throtl_grp's.
69 */
Tejun Heoc5cc2072013-05-14 13:52:38 -070070 struct list_head queued[2]; /* throtl_qnode [READ/WRITE] */
Tejun Heo73f0d492013-05-14 13:52:35 -070071 unsigned int nr_queued[2]; /* number of queued bios */
72
73 /*
74 * RB tree of active children throtl_grp's, which are sorted by
75 * their ->disptime.
76 */
Tejun Heoc9e03322013-05-14 13:52:32 -070077 struct rb_root pending_tree; /* RB tree of active tgs */
78 struct rb_node *first_pending; /* first node in the tree */
79 unsigned int nr_pending; /* # queued in the tree */
80 unsigned long first_pending_disptime; /* disptime of the first tg */
Tejun Heo69df0ab2013-05-14 13:52:36 -070081 struct timer_list pending_timer; /* fires on first_pending_disptime */
Vivek Goyale43473b2010-09-15 17:06:35 -040082};
83
Tejun Heo5b2c16a2013-05-14 13:52:32 -070084enum tg_state_flags {
85 THROTL_TG_PENDING = 1 << 0, /* on parent's pending tree */
Tejun Heo0e9f4162013-05-14 13:52:35 -070086 THROTL_TG_WAS_EMPTY = 1 << 1, /* bio_lists[] became non-empty */
Tejun Heo5b2c16a2013-05-14 13:52:32 -070087};
88
Vivek Goyale43473b2010-09-15 17:06:35 -040089#define rb_entry_tg(node) rb_entry((node), struct throtl_grp, rb_node)
90
Shaohua Li9f626e32017-03-27 10:51:30 -070091enum {
Shaohua Licd5ab1b2017-03-27 10:51:32 -070092 LIMIT_LOW,
Shaohua Li9f626e32017-03-27 10:51:30 -070093 LIMIT_MAX,
94 LIMIT_CNT,
95};
96
Vivek Goyale43473b2010-09-15 17:06:35 -040097struct throtl_grp {
Tejun Heof95a04a2012-04-16 13:57:26 -070098 /* must be the first member */
99 struct blkg_policy_data pd;
100
Tejun Heoc9e03322013-05-14 13:52:32 -0700101 /* active throtl group service_queue member */
Vivek Goyale43473b2010-09-15 17:06:35 -0400102 struct rb_node rb_node;
103
Tejun Heo0f3457f2013-05-14 13:52:32 -0700104 /* throtl_data this group belongs to */
105 struct throtl_data *td;
106
Tejun Heo49a2f1e2013-05-14 13:52:34 -0700107 /* this group's service queue */
108 struct throtl_service_queue service_queue;
109
Vivek Goyale43473b2010-09-15 17:06:35 -0400110 /*
Tejun Heoc5cc2072013-05-14 13:52:38 -0700111 * qnode_on_self is used when bios are directly queued to this
112 * throtl_grp so that local bios compete fairly with bios
113 * dispatched from children. qnode_on_parent is used when bios are
114 * dispatched from this throtl_grp into its parent and will compete
115 * with the sibling qnode_on_parents and the parent's
116 * qnode_on_self.
117 */
118 struct throtl_qnode qnode_on_self[2];
119 struct throtl_qnode qnode_on_parent[2];
120
121 /*
Vivek Goyale43473b2010-09-15 17:06:35 -0400122 * Dispatch time in jiffies. This is the estimated time when group
123 * will unthrottle and is ready to dispatch more bio. It is used as
124 * key to sort active groups in service tree.
125 */
126 unsigned long disptime;
127
Vivek Goyale43473b2010-09-15 17:06:35 -0400128 unsigned int flags;
129
Tejun Heo693e7512013-05-14 13:52:38 -0700130 /* are there any throtl rules between this group and td? */
131 bool has_rules[2];
132
Shaohua Licd5ab1b2017-03-27 10:51:32 -0700133 /* internally used bytes per second rate limits */
Shaohua Li9f626e32017-03-27 10:51:30 -0700134 uint64_t bps[2][LIMIT_CNT];
Shaohua Licd5ab1b2017-03-27 10:51:32 -0700135 /* user configured bps limits */
136 uint64_t bps_conf[2][LIMIT_CNT];
Vivek Goyale43473b2010-09-15 17:06:35 -0400137
Shaohua Licd5ab1b2017-03-27 10:51:32 -0700138 /* internally used IOPS limits */
Shaohua Li9f626e32017-03-27 10:51:30 -0700139 unsigned int iops[2][LIMIT_CNT];
Shaohua Licd5ab1b2017-03-27 10:51:32 -0700140 /* user configured IOPS limits */
141 unsigned int iops_conf[2][LIMIT_CNT];
Vivek Goyal8e89d132010-09-15 17:06:37 -0400142
Vivek Goyale43473b2010-09-15 17:06:35 -0400143 /* Number of bytes disptached in current slice */
144 uint64_t bytes_disp[2];
Vivek Goyal8e89d132010-09-15 17:06:37 -0400145 /* Number of bio's dispatched in current slice */
146 unsigned int io_disp[2];
Vivek Goyale43473b2010-09-15 17:06:35 -0400147
Shaohua Li3f0abd82017-03-27 10:51:35 -0700148 unsigned long last_low_overflow_time[2];
149
150 uint64_t last_bytes_disp[2];
151 unsigned int last_io_disp[2];
152
153 unsigned long last_check_time;
154
Shaohua Liaec24242017-03-27 10:51:39 -0700155 unsigned long last_dispatch_time[2];
156
Vivek Goyale43473b2010-09-15 17:06:35 -0400157 /* When did we start a new slice */
158 unsigned long slice_start[2];
159 unsigned long slice_end[2];
Shaohua Li9e234ee2017-03-27 10:51:41 -0700160
161 unsigned long last_finish_time; /* ns / 1024 */
162 unsigned long checked_last_finish_time; /* ns / 1024 */
163 unsigned long avg_idletime; /* ns / 1024 */
164 unsigned long idletime_threshold; /* us */
Vivek Goyale43473b2010-09-15 17:06:35 -0400165};
166
167struct throtl_data
168{
Vivek Goyale43473b2010-09-15 17:06:35 -0400169 /* service tree for active throtl groups */
Tejun Heoc9e03322013-05-14 13:52:32 -0700170 struct throtl_service_queue service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -0400171
Vivek Goyale43473b2010-09-15 17:06:35 -0400172 struct request_queue *queue;
173
174 /* Total Number of queued bios on READ and WRITE lists */
175 unsigned int nr_queued[2];
176
Shaohua Li297e3d82017-03-27 10:51:37 -0700177 unsigned int throtl_slice;
178
Vivek Goyale43473b2010-09-15 17:06:35 -0400179 /* Work for dispatching throttled bios */
Tejun Heo69df0ab2013-05-14 13:52:36 -0700180 struct work_struct dispatch_work;
Shaohua Li9f626e32017-03-27 10:51:30 -0700181 unsigned int limit_index;
182 bool limit_valid[LIMIT_CNT];
Shaohua Li3f0abd82017-03-27 10:51:35 -0700183
184 unsigned long low_upgrade_time;
185 unsigned long low_downgrade_time;
Shaohua Li7394e312017-03-27 10:51:40 -0700186
187 unsigned int scale;
Vivek Goyale43473b2010-09-15 17:06:35 -0400188};
189
Tejun Heo69df0ab2013-05-14 13:52:36 -0700190static void throtl_pending_timer_fn(unsigned long arg);
191
Tejun Heof95a04a2012-04-16 13:57:26 -0700192static inline struct throtl_grp *pd_to_tg(struct blkg_policy_data *pd)
193{
194 return pd ? container_of(pd, struct throtl_grp, pd) : NULL;
195}
196
Tejun Heo3c798392012-04-16 13:57:25 -0700197static inline struct throtl_grp *blkg_to_tg(struct blkcg_gq *blkg)
Tejun Heo03814112012-03-05 13:15:14 -0800198{
Tejun Heof95a04a2012-04-16 13:57:26 -0700199 return pd_to_tg(blkg_to_pd(blkg, &blkcg_policy_throtl));
Tejun Heo03814112012-03-05 13:15:14 -0800200}
201
Tejun Heo3c798392012-04-16 13:57:25 -0700202static inline struct blkcg_gq *tg_to_blkg(struct throtl_grp *tg)
Tejun Heo03814112012-03-05 13:15:14 -0800203{
Tejun Heof95a04a2012-04-16 13:57:26 -0700204 return pd_to_blkg(&tg->pd);
Tejun Heo03814112012-03-05 13:15:14 -0800205}
206
Tejun Heofda6f272013-05-14 13:52:36 -0700207/**
208 * sq_to_tg - return the throl_grp the specified service queue belongs to
209 * @sq: the throtl_service_queue of interest
210 *
211 * Return the throtl_grp @sq belongs to. If @sq is the top-level one
212 * embedded in throtl_data, %NULL is returned.
213 */
214static struct throtl_grp *sq_to_tg(struct throtl_service_queue *sq)
215{
216 if (sq && sq->parent_sq)
217 return container_of(sq, struct throtl_grp, service_queue);
218 else
219 return NULL;
220}
Vivek Goyale43473b2010-09-15 17:06:35 -0400221
Tejun Heofda6f272013-05-14 13:52:36 -0700222/**
223 * sq_to_td - return throtl_data the specified service queue belongs to
224 * @sq: the throtl_service_queue of interest
225 *
Masahiro Yamadab43daed2017-02-27 14:29:09 -0800226 * A service_queue can be embedded in either a throtl_grp or throtl_data.
Tejun Heofda6f272013-05-14 13:52:36 -0700227 * Determine the associated throtl_data accordingly and return it.
228 */
229static struct throtl_data *sq_to_td(struct throtl_service_queue *sq)
230{
231 struct throtl_grp *tg = sq_to_tg(sq);
232
233 if (tg)
234 return tg->td;
235 else
236 return container_of(sq, struct throtl_data, service_queue);
237}
238
Shaohua Li7394e312017-03-27 10:51:40 -0700239/*
240 * cgroup's limit in LIMIT_MAX is scaled if low limit is set. This scale is to
241 * make the IO dispatch more smooth.
242 * Scale up: linearly scale up according to lapsed time since upgrade. For
243 * every throtl_slice, the limit scales up 1/2 .low limit till the
244 * limit hits .max limit
245 * Scale down: exponentially scale down if a cgroup doesn't hit its .low limit
246 */
247static uint64_t throtl_adjusted_limit(uint64_t low, struct throtl_data *td)
248{
249 /* arbitrary value to avoid too big scale */
250 if (td->scale < 4096 && time_after_eq(jiffies,
251 td->low_upgrade_time + td->scale * td->throtl_slice))
252 td->scale = (jiffies - td->low_upgrade_time) / td->throtl_slice;
253
254 return low + (low >> 1) * td->scale;
255}
256
Shaohua Li9f626e32017-03-27 10:51:30 -0700257static uint64_t tg_bps_limit(struct throtl_grp *tg, int rw)
258{
Shaohua Lib22c4172017-03-27 10:51:33 -0700259 struct blkcg_gq *blkg = tg_to_blkg(tg);
Shaohua Li7394e312017-03-27 10:51:40 -0700260 struct throtl_data *td;
Shaohua Lib22c4172017-03-27 10:51:33 -0700261 uint64_t ret;
262
263 if (cgroup_subsys_on_dfl(io_cgrp_subsys) && !blkg->parent)
264 return U64_MAX;
Shaohua Li7394e312017-03-27 10:51:40 -0700265
266 td = tg->td;
267 ret = tg->bps[rw][td->limit_index];
268 if (ret == 0 && td->limit_index == LIMIT_LOW)
Shaohua Lib22c4172017-03-27 10:51:33 -0700269 return tg->bps[rw][LIMIT_MAX];
Shaohua Li7394e312017-03-27 10:51:40 -0700270
271 if (td->limit_index == LIMIT_MAX && tg->bps[rw][LIMIT_LOW] &&
272 tg->bps[rw][LIMIT_LOW] != tg->bps[rw][LIMIT_MAX]) {
273 uint64_t adjusted;
274
275 adjusted = throtl_adjusted_limit(tg->bps[rw][LIMIT_LOW], td);
276 ret = min(tg->bps[rw][LIMIT_MAX], adjusted);
277 }
Shaohua Lib22c4172017-03-27 10:51:33 -0700278 return ret;
Shaohua Li9f626e32017-03-27 10:51:30 -0700279}
280
281static unsigned int tg_iops_limit(struct throtl_grp *tg, int rw)
282{
Shaohua Lib22c4172017-03-27 10:51:33 -0700283 struct blkcg_gq *blkg = tg_to_blkg(tg);
Shaohua Li7394e312017-03-27 10:51:40 -0700284 struct throtl_data *td;
Shaohua Lib22c4172017-03-27 10:51:33 -0700285 unsigned int ret;
286
287 if (cgroup_subsys_on_dfl(io_cgrp_subsys) && !blkg->parent)
288 return UINT_MAX;
Shaohua Li7394e312017-03-27 10:51:40 -0700289 td = tg->td;
290 ret = tg->iops[rw][td->limit_index];
Shaohua Lib22c4172017-03-27 10:51:33 -0700291 if (ret == 0 && tg->td->limit_index == LIMIT_LOW)
292 return tg->iops[rw][LIMIT_MAX];
Shaohua Li7394e312017-03-27 10:51:40 -0700293
294 if (td->limit_index == LIMIT_MAX && tg->iops[rw][LIMIT_LOW] &&
295 tg->iops[rw][LIMIT_LOW] != tg->iops[rw][LIMIT_MAX]) {
296 uint64_t adjusted;
297
298 adjusted = throtl_adjusted_limit(tg->iops[rw][LIMIT_LOW], td);
299 if (adjusted > UINT_MAX)
300 adjusted = UINT_MAX;
301 ret = min_t(unsigned int, tg->iops[rw][LIMIT_MAX], adjusted);
302 }
Shaohua Lib22c4172017-03-27 10:51:33 -0700303 return ret;
Shaohua Li9f626e32017-03-27 10:51:30 -0700304}
305
Tejun Heofda6f272013-05-14 13:52:36 -0700306/**
307 * throtl_log - log debug message via blktrace
308 * @sq: the service_queue being reported
309 * @fmt: printf format string
310 * @args: printf args
311 *
312 * The messages are prefixed with "throtl BLKG_NAME" if @sq belongs to a
313 * throtl_grp; otherwise, just "throtl".
Tejun Heofda6f272013-05-14 13:52:36 -0700314 */
315#define throtl_log(sq, fmt, args...) do { \
316 struct throtl_grp *__tg = sq_to_tg((sq)); \
317 struct throtl_data *__td = sq_to_td((sq)); \
318 \
319 (void)__td; \
Shaohua Li59fa0222016-05-09 17:22:15 -0700320 if (likely(!blk_trace_note_message_enabled(__td->queue))) \
321 break; \
Tejun Heofda6f272013-05-14 13:52:36 -0700322 if ((__tg)) { \
323 char __pbuf[128]; \
324 \
325 blkg_path(tg_to_blkg(__tg), __pbuf, sizeof(__pbuf)); \
326 blk_add_trace_msg(__td->queue, "throtl %s " fmt, __pbuf, ##args); \
327 } else { \
328 blk_add_trace_msg(__td->queue, "throtl " fmt, ##args); \
329 } \
330} while (0)
Vivek Goyale43473b2010-09-15 17:06:35 -0400331
Tejun Heoc5cc2072013-05-14 13:52:38 -0700332static void throtl_qnode_init(struct throtl_qnode *qn, struct throtl_grp *tg)
333{
334 INIT_LIST_HEAD(&qn->node);
335 bio_list_init(&qn->bios);
336 qn->tg = tg;
337}
338
339/**
340 * throtl_qnode_add_bio - add a bio to a throtl_qnode and activate it
341 * @bio: bio being added
342 * @qn: qnode to add bio to
343 * @queued: the service_queue->queued[] list @qn belongs to
344 *
345 * Add @bio to @qn and put @qn on @queued if it's not already on.
346 * @qn->tg's reference count is bumped when @qn is activated. See the
347 * comment on top of throtl_qnode definition for details.
348 */
349static void throtl_qnode_add_bio(struct bio *bio, struct throtl_qnode *qn,
350 struct list_head *queued)
351{
352 bio_list_add(&qn->bios, bio);
353 if (list_empty(&qn->node)) {
354 list_add_tail(&qn->node, queued);
355 blkg_get(tg_to_blkg(qn->tg));
356 }
357}
358
359/**
360 * throtl_peek_queued - peek the first bio on a qnode list
361 * @queued: the qnode list to peek
362 */
363static struct bio *throtl_peek_queued(struct list_head *queued)
364{
365 struct throtl_qnode *qn = list_first_entry(queued, struct throtl_qnode, node);
366 struct bio *bio;
367
368 if (list_empty(queued))
369 return NULL;
370
371 bio = bio_list_peek(&qn->bios);
372 WARN_ON_ONCE(!bio);
373 return bio;
374}
375
376/**
377 * throtl_pop_queued - pop the first bio form a qnode list
378 * @queued: the qnode list to pop a bio from
379 * @tg_to_put: optional out argument for throtl_grp to put
380 *
381 * Pop the first bio from the qnode list @queued. After popping, the first
382 * qnode is removed from @queued if empty or moved to the end of @queued so
383 * that the popping order is round-robin.
384 *
385 * When the first qnode is removed, its associated throtl_grp should be put
386 * too. If @tg_to_put is NULL, this function automatically puts it;
387 * otherwise, *@tg_to_put is set to the throtl_grp to put and the caller is
388 * responsible for putting it.
389 */
390static struct bio *throtl_pop_queued(struct list_head *queued,
391 struct throtl_grp **tg_to_put)
392{
393 struct throtl_qnode *qn = list_first_entry(queued, struct throtl_qnode, node);
394 struct bio *bio;
395
396 if (list_empty(queued))
397 return NULL;
398
399 bio = bio_list_pop(&qn->bios);
400 WARN_ON_ONCE(!bio);
401
402 if (bio_list_empty(&qn->bios)) {
403 list_del_init(&qn->node);
404 if (tg_to_put)
405 *tg_to_put = qn->tg;
406 else
407 blkg_put(tg_to_blkg(qn->tg));
408 } else {
409 list_move_tail(&qn->node, queued);
410 }
411
412 return bio;
413}
414
Tejun Heo49a2f1e2013-05-14 13:52:34 -0700415/* init a service_queue, assumes the caller zeroed it */
Tejun Heob2ce2642015-08-18 14:55:13 -0700416static void throtl_service_queue_init(struct throtl_service_queue *sq)
Tejun Heo49a2f1e2013-05-14 13:52:34 -0700417{
Tejun Heoc5cc2072013-05-14 13:52:38 -0700418 INIT_LIST_HEAD(&sq->queued[0]);
419 INIT_LIST_HEAD(&sq->queued[1]);
Tejun Heo49a2f1e2013-05-14 13:52:34 -0700420 sq->pending_tree = RB_ROOT;
Tejun Heo69df0ab2013-05-14 13:52:36 -0700421 setup_timer(&sq->pending_timer, throtl_pending_timer_fn,
422 (unsigned long)sq);
423}
424
Tejun Heo001bea72015-08-18 14:55:11 -0700425static struct blkg_policy_data *throtl_pd_alloc(gfp_t gfp, int node)
426{
Tejun Heo4fb72032015-08-18 14:55:12 -0700427 struct throtl_grp *tg;
Tejun Heo24bdb8e2015-08-18 14:55:22 -0700428 int rw;
Tejun Heo4fb72032015-08-18 14:55:12 -0700429
430 tg = kzalloc_node(sizeof(*tg), gfp, node);
431 if (!tg)
Tejun Heo77ea7332015-08-18 14:55:24 -0700432 return NULL;
Tejun Heo4fb72032015-08-18 14:55:12 -0700433
Tejun Heob2ce2642015-08-18 14:55:13 -0700434 throtl_service_queue_init(&tg->service_queue);
435
436 for (rw = READ; rw <= WRITE; rw++) {
437 throtl_qnode_init(&tg->qnode_on_self[rw], tg);
438 throtl_qnode_init(&tg->qnode_on_parent[rw], tg);
439 }
440
441 RB_CLEAR_NODE(&tg->rb_node);
Shaohua Li9f626e32017-03-27 10:51:30 -0700442 tg->bps[READ][LIMIT_MAX] = U64_MAX;
443 tg->bps[WRITE][LIMIT_MAX] = U64_MAX;
444 tg->iops[READ][LIMIT_MAX] = UINT_MAX;
445 tg->iops[WRITE][LIMIT_MAX] = UINT_MAX;
Shaohua Licd5ab1b2017-03-27 10:51:32 -0700446 tg->bps_conf[READ][LIMIT_MAX] = U64_MAX;
447 tg->bps_conf[WRITE][LIMIT_MAX] = U64_MAX;
448 tg->iops_conf[READ][LIMIT_MAX] = UINT_MAX;
449 tg->iops_conf[WRITE][LIMIT_MAX] = UINT_MAX;
450 /* LIMIT_LOW will have default value 0 */
Tejun Heob2ce2642015-08-18 14:55:13 -0700451
Tejun Heo4fb72032015-08-18 14:55:12 -0700452 return &tg->pd;
Tejun Heo001bea72015-08-18 14:55:11 -0700453}
454
Tejun Heoa9520cd2015-08-18 14:55:14 -0700455static void throtl_pd_init(struct blkg_policy_data *pd)
Vivek Goyala29a1712011-05-19 15:38:19 -0400456{
Tejun Heoa9520cd2015-08-18 14:55:14 -0700457 struct throtl_grp *tg = pd_to_tg(pd);
458 struct blkcg_gq *blkg = tg_to_blkg(tg);
Tejun Heo77216b02013-05-14 13:52:36 -0700459 struct throtl_data *td = blkg->q->td;
Tejun Heob2ce2642015-08-18 14:55:13 -0700460 struct throtl_service_queue *sq = &tg->service_queue;
Tejun Heocd1604f2012-03-05 13:15:06 -0800461
Tejun Heo91381252013-05-14 13:52:38 -0700462 /*
Tejun Heoaa6ec292014-07-09 10:08:08 -0400463 * If on the default hierarchy, we switch to properly hierarchical
Tejun Heo91381252013-05-14 13:52:38 -0700464 * behavior where limits on a given throtl_grp are applied to the
465 * whole subtree rather than just the group itself. e.g. If 16M
466 * read_bps limit is set on the root group, the whole system can't
467 * exceed 16M for the device.
468 *
Tejun Heoaa6ec292014-07-09 10:08:08 -0400469 * If not on the default hierarchy, the broken flat hierarchy
Tejun Heo91381252013-05-14 13:52:38 -0700470 * behavior is retained where all throtl_grps are treated as if
471 * they're all separate root groups right below throtl_data.
472 * Limits of a group don't interact with limits of other groups
473 * regardless of the position of the group in the hierarchy.
474 */
Tejun Heob2ce2642015-08-18 14:55:13 -0700475 sq->parent_sq = &td->service_queue;
Tejun Heo9e10a132015-09-18 11:56:28 -0400476 if (cgroup_subsys_on_dfl(io_cgrp_subsys) && blkg->parent)
Tejun Heob2ce2642015-08-18 14:55:13 -0700477 sq->parent_sq = &blkg_to_tg(blkg->parent)->service_queue;
Tejun Heo77216b02013-05-14 13:52:36 -0700478 tg->td = td;
Shaohua Li9e234ee2017-03-27 10:51:41 -0700479
480 if (blk_queue_nonrot(td->queue))
481 tg->idletime_threshold = DFL_IDLE_THRESHOLD_SSD;
482 else
483 tg->idletime_threshold = DFL_IDLE_THRESHOLD_HD;
Tejun Heo8a3d2612012-04-01 14:38:44 -0700484}
485
Tejun Heo693e7512013-05-14 13:52:38 -0700486/*
487 * Set has_rules[] if @tg or any of its parents have limits configured.
488 * This doesn't require walking up to the top of the hierarchy as the
489 * parent's has_rules[] is guaranteed to be correct.
490 */
491static void tg_update_has_rules(struct throtl_grp *tg)
492{
493 struct throtl_grp *parent_tg = sq_to_tg(tg->service_queue.parent_sq);
Shaohua Li9f626e32017-03-27 10:51:30 -0700494 struct throtl_data *td = tg->td;
Tejun Heo693e7512013-05-14 13:52:38 -0700495 int rw;
496
497 for (rw = READ; rw <= WRITE; rw++)
498 tg->has_rules[rw] = (parent_tg && parent_tg->has_rules[rw]) ||
Shaohua Li9f626e32017-03-27 10:51:30 -0700499 (td->limit_valid[td->limit_index] &&
500 (tg_bps_limit(tg, rw) != U64_MAX ||
501 tg_iops_limit(tg, rw) != UINT_MAX));
Tejun Heo693e7512013-05-14 13:52:38 -0700502}
503
Tejun Heoa9520cd2015-08-18 14:55:14 -0700504static void throtl_pd_online(struct blkg_policy_data *pd)
Tejun Heo693e7512013-05-14 13:52:38 -0700505{
Shaohua Liaec24242017-03-27 10:51:39 -0700506 struct throtl_grp *tg = pd_to_tg(pd);
Tejun Heo693e7512013-05-14 13:52:38 -0700507 /*
508 * We don't want new groups to escape the limits of its ancestors.
509 * Update has_rules[] after a new group is brought online.
510 */
Shaohua Liaec24242017-03-27 10:51:39 -0700511 tg_update_has_rules(tg);
512 tg->last_dispatch_time[READ] = jiffies;
513 tg->last_dispatch_time[WRITE] = jiffies;
Tejun Heo693e7512013-05-14 13:52:38 -0700514}
515
Shaohua Licd5ab1b2017-03-27 10:51:32 -0700516static void blk_throtl_update_limit_valid(struct throtl_data *td)
517{
518 struct cgroup_subsys_state *pos_css;
519 struct blkcg_gq *blkg;
520 bool low_valid = false;
521
522 rcu_read_lock();
523 blkg_for_each_descendant_post(blkg, pos_css, td->queue->root_blkg) {
524 struct throtl_grp *tg = blkg_to_tg(blkg);
525
526 if (tg->bps[READ][LIMIT_LOW] || tg->bps[WRITE][LIMIT_LOW] ||
527 tg->iops[READ][LIMIT_LOW] || tg->iops[WRITE][LIMIT_LOW])
528 low_valid = true;
529 }
530 rcu_read_unlock();
531
532 td->limit_valid[LIMIT_LOW] = low_valid;
533}
534
Shaohua Lic79892c2017-03-27 10:51:34 -0700535static void throtl_upgrade_state(struct throtl_data *td);
Shaohua Licd5ab1b2017-03-27 10:51:32 -0700536static void throtl_pd_offline(struct blkg_policy_data *pd)
537{
538 struct throtl_grp *tg = pd_to_tg(pd);
539
540 tg->bps[READ][LIMIT_LOW] = 0;
541 tg->bps[WRITE][LIMIT_LOW] = 0;
542 tg->iops[READ][LIMIT_LOW] = 0;
543 tg->iops[WRITE][LIMIT_LOW] = 0;
544
545 blk_throtl_update_limit_valid(tg->td);
546
Shaohua Lic79892c2017-03-27 10:51:34 -0700547 if (!tg->td->limit_valid[tg->td->limit_index])
548 throtl_upgrade_state(tg->td);
Shaohua Licd5ab1b2017-03-27 10:51:32 -0700549}
550
Tejun Heo001bea72015-08-18 14:55:11 -0700551static void throtl_pd_free(struct blkg_policy_data *pd)
552{
Tejun Heo4fb72032015-08-18 14:55:12 -0700553 struct throtl_grp *tg = pd_to_tg(pd);
554
Tejun Heob2ce2642015-08-18 14:55:13 -0700555 del_timer_sync(&tg->service_queue.pending_timer);
Tejun Heo4fb72032015-08-18 14:55:12 -0700556 kfree(tg);
Tejun Heo001bea72015-08-18 14:55:11 -0700557}
558
Tejun Heo0049af72013-05-14 13:52:33 -0700559static struct throtl_grp *
560throtl_rb_first(struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -0400561{
562 /* Service tree is empty */
Tejun Heo0049af72013-05-14 13:52:33 -0700563 if (!parent_sq->nr_pending)
Vivek Goyale43473b2010-09-15 17:06:35 -0400564 return NULL;
565
Tejun Heo0049af72013-05-14 13:52:33 -0700566 if (!parent_sq->first_pending)
567 parent_sq->first_pending = rb_first(&parent_sq->pending_tree);
Vivek Goyale43473b2010-09-15 17:06:35 -0400568
Tejun Heo0049af72013-05-14 13:52:33 -0700569 if (parent_sq->first_pending)
570 return rb_entry_tg(parent_sq->first_pending);
Vivek Goyale43473b2010-09-15 17:06:35 -0400571
572 return NULL;
573}
574
575static void rb_erase_init(struct rb_node *n, struct rb_root *root)
576{
577 rb_erase(n, root);
578 RB_CLEAR_NODE(n);
579}
580
Tejun Heo0049af72013-05-14 13:52:33 -0700581static void throtl_rb_erase(struct rb_node *n,
582 struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -0400583{
Tejun Heo0049af72013-05-14 13:52:33 -0700584 if (parent_sq->first_pending == n)
585 parent_sq->first_pending = NULL;
586 rb_erase_init(n, &parent_sq->pending_tree);
587 --parent_sq->nr_pending;
Vivek Goyale43473b2010-09-15 17:06:35 -0400588}
589
Tejun Heo0049af72013-05-14 13:52:33 -0700590static void update_min_dispatch_time(struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -0400591{
592 struct throtl_grp *tg;
593
Tejun Heo0049af72013-05-14 13:52:33 -0700594 tg = throtl_rb_first(parent_sq);
Vivek Goyale43473b2010-09-15 17:06:35 -0400595 if (!tg)
596 return;
597
Tejun Heo0049af72013-05-14 13:52:33 -0700598 parent_sq->first_pending_disptime = tg->disptime;
Vivek Goyale43473b2010-09-15 17:06:35 -0400599}
600
Tejun Heo77216b02013-05-14 13:52:36 -0700601static void tg_service_queue_add(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400602{
Tejun Heo77216b02013-05-14 13:52:36 -0700603 struct throtl_service_queue *parent_sq = tg->service_queue.parent_sq;
Tejun Heo0049af72013-05-14 13:52:33 -0700604 struct rb_node **node = &parent_sq->pending_tree.rb_node;
Vivek Goyale43473b2010-09-15 17:06:35 -0400605 struct rb_node *parent = NULL;
606 struct throtl_grp *__tg;
607 unsigned long key = tg->disptime;
608 int left = 1;
609
610 while (*node != NULL) {
611 parent = *node;
612 __tg = rb_entry_tg(parent);
613
614 if (time_before(key, __tg->disptime))
615 node = &parent->rb_left;
616 else {
617 node = &parent->rb_right;
618 left = 0;
619 }
620 }
621
622 if (left)
Tejun Heo0049af72013-05-14 13:52:33 -0700623 parent_sq->first_pending = &tg->rb_node;
Vivek Goyale43473b2010-09-15 17:06:35 -0400624
625 rb_link_node(&tg->rb_node, parent, node);
Tejun Heo0049af72013-05-14 13:52:33 -0700626 rb_insert_color(&tg->rb_node, &parent_sq->pending_tree);
Vivek Goyale43473b2010-09-15 17:06:35 -0400627}
628
Tejun Heo77216b02013-05-14 13:52:36 -0700629static void __throtl_enqueue_tg(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400630{
Tejun Heo77216b02013-05-14 13:52:36 -0700631 tg_service_queue_add(tg);
Tejun Heo5b2c16a2013-05-14 13:52:32 -0700632 tg->flags |= THROTL_TG_PENDING;
Tejun Heo77216b02013-05-14 13:52:36 -0700633 tg->service_queue.parent_sq->nr_pending++;
Vivek Goyale43473b2010-09-15 17:06:35 -0400634}
635
Tejun Heo77216b02013-05-14 13:52:36 -0700636static void throtl_enqueue_tg(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400637{
Tejun Heo5b2c16a2013-05-14 13:52:32 -0700638 if (!(tg->flags & THROTL_TG_PENDING))
Tejun Heo77216b02013-05-14 13:52:36 -0700639 __throtl_enqueue_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -0400640}
641
Tejun Heo77216b02013-05-14 13:52:36 -0700642static void __throtl_dequeue_tg(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400643{
Tejun Heo77216b02013-05-14 13:52:36 -0700644 throtl_rb_erase(&tg->rb_node, tg->service_queue.parent_sq);
Tejun Heo5b2c16a2013-05-14 13:52:32 -0700645 tg->flags &= ~THROTL_TG_PENDING;
Vivek Goyale43473b2010-09-15 17:06:35 -0400646}
647
Tejun Heo77216b02013-05-14 13:52:36 -0700648static void throtl_dequeue_tg(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400649{
Tejun Heo5b2c16a2013-05-14 13:52:32 -0700650 if (tg->flags & THROTL_TG_PENDING)
Tejun Heo77216b02013-05-14 13:52:36 -0700651 __throtl_dequeue_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -0400652}
653
Tejun Heoa9131a22013-05-14 13:52:31 -0700654/* Call with queue lock held */
Tejun Heo69df0ab2013-05-14 13:52:36 -0700655static void throtl_schedule_pending_timer(struct throtl_service_queue *sq,
656 unsigned long expires)
Tejun Heoa9131a22013-05-14 13:52:31 -0700657{
Shaohua Li297e3d82017-03-27 10:51:37 -0700658 unsigned long max_expire = jiffies + 8 * sq_to_tg(sq)->td->throtl_slice;
Shaohua Li06cceed2017-03-27 10:51:36 -0700659
660 /*
661 * Since we are adjusting the throttle limit dynamically, the sleep
662 * time calculated according to previous limit might be invalid. It's
663 * possible the cgroup sleep time is very long and no other cgroups
664 * have IO running so notify the limit changes. Make sure the cgroup
665 * doesn't sleep too long to avoid the missed notification.
666 */
667 if (time_after(expires, max_expire))
668 expires = max_expire;
Tejun Heo69df0ab2013-05-14 13:52:36 -0700669 mod_timer(&sq->pending_timer, expires);
670 throtl_log(sq, "schedule timer. delay=%lu jiffies=%lu",
671 expires - jiffies, jiffies);
Tejun Heoa9131a22013-05-14 13:52:31 -0700672}
673
Tejun Heo7f52f982013-05-14 13:52:37 -0700674/**
675 * throtl_schedule_next_dispatch - schedule the next dispatch cycle
676 * @sq: the service_queue to schedule dispatch for
677 * @force: force scheduling
678 *
679 * Arm @sq->pending_timer so that the next dispatch cycle starts on the
680 * dispatch time of the first pending child. Returns %true if either timer
681 * is armed or there's no pending child left. %false if the current
682 * dispatch window is still open and the caller should continue
683 * dispatching.
684 *
685 * If @force is %true, the dispatch timer is always scheduled and this
686 * function is guaranteed to return %true. This is to be used when the
687 * caller can't dispatch itself and needs to invoke pending_timer
688 * unconditionally. Note that forced scheduling is likely to induce short
689 * delay before dispatch starts even if @sq->first_pending_disptime is not
690 * in the future and thus shouldn't be used in hot paths.
691 */
692static bool throtl_schedule_next_dispatch(struct throtl_service_queue *sq,
693 bool force)
Vivek Goyale43473b2010-09-15 17:06:35 -0400694{
Tejun Heo6a525602013-05-14 13:52:32 -0700695 /* any pending children left? */
Tejun Heoc9e03322013-05-14 13:52:32 -0700696 if (!sq->nr_pending)
Tejun Heo7f52f982013-05-14 13:52:37 -0700697 return true;
Vivek Goyale43473b2010-09-15 17:06:35 -0400698
Tejun Heoc9e03322013-05-14 13:52:32 -0700699 update_min_dispatch_time(sq);
Vivek Goyale43473b2010-09-15 17:06:35 -0400700
Tejun Heo69df0ab2013-05-14 13:52:36 -0700701 /* is the next dispatch time in the future? */
Tejun Heo7f52f982013-05-14 13:52:37 -0700702 if (force || time_after(sq->first_pending_disptime, jiffies)) {
Tejun Heo69df0ab2013-05-14 13:52:36 -0700703 throtl_schedule_pending_timer(sq, sq->first_pending_disptime);
Tejun Heo7f52f982013-05-14 13:52:37 -0700704 return true;
Tejun Heo69df0ab2013-05-14 13:52:36 -0700705 }
706
Tejun Heo7f52f982013-05-14 13:52:37 -0700707 /* tell the caller to continue dispatching */
708 return false;
Vivek Goyale43473b2010-09-15 17:06:35 -0400709}
710
Vivek Goyal32ee5bc2013-05-14 13:52:38 -0700711static inline void throtl_start_new_slice_with_credit(struct throtl_grp *tg,
712 bool rw, unsigned long start)
713{
714 tg->bytes_disp[rw] = 0;
715 tg->io_disp[rw] = 0;
716
717 /*
718 * Previous slice has expired. We must have trimmed it after last
719 * bio dispatch. That means since start of last slice, we never used
720 * that bandwidth. Do try to make use of that bandwidth while giving
721 * credit.
722 */
723 if (time_after_eq(start, tg->slice_start[rw]))
724 tg->slice_start[rw] = start;
725
Shaohua Li297e3d82017-03-27 10:51:37 -0700726 tg->slice_end[rw] = jiffies + tg->td->throtl_slice;
Vivek Goyal32ee5bc2013-05-14 13:52:38 -0700727 throtl_log(&tg->service_queue,
728 "[%c] new slice with credit start=%lu end=%lu jiffies=%lu",
729 rw == READ ? 'R' : 'W', tg->slice_start[rw],
730 tg->slice_end[rw], jiffies);
731}
732
Tejun Heo0f3457f2013-05-14 13:52:32 -0700733static inline void throtl_start_new_slice(struct throtl_grp *tg, bool rw)
Vivek Goyale43473b2010-09-15 17:06:35 -0400734{
735 tg->bytes_disp[rw] = 0;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400736 tg->io_disp[rw] = 0;
Vivek Goyale43473b2010-09-15 17:06:35 -0400737 tg->slice_start[rw] = jiffies;
Shaohua Li297e3d82017-03-27 10:51:37 -0700738 tg->slice_end[rw] = jiffies + tg->td->throtl_slice;
Tejun Heofda6f272013-05-14 13:52:36 -0700739 throtl_log(&tg->service_queue,
740 "[%c] new slice start=%lu end=%lu jiffies=%lu",
741 rw == READ ? 'R' : 'W', tg->slice_start[rw],
742 tg->slice_end[rw], jiffies);
Vivek Goyale43473b2010-09-15 17:06:35 -0400743}
744
Tejun Heo0f3457f2013-05-14 13:52:32 -0700745static inline void throtl_set_slice_end(struct throtl_grp *tg, bool rw,
746 unsigned long jiffy_end)
Vivek Goyald1ae8ff2010-12-01 19:34:46 +0100747{
Shaohua Li297e3d82017-03-27 10:51:37 -0700748 tg->slice_end[rw] = roundup(jiffy_end, tg->td->throtl_slice);
Vivek Goyald1ae8ff2010-12-01 19:34:46 +0100749}
750
Tejun Heo0f3457f2013-05-14 13:52:32 -0700751static inline void throtl_extend_slice(struct throtl_grp *tg, bool rw,
752 unsigned long jiffy_end)
Vivek Goyale43473b2010-09-15 17:06:35 -0400753{
Shaohua Li297e3d82017-03-27 10:51:37 -0700754 tg->slice_end[rw] = roundup(jiffy_end, tg->td->throtl_slice);
Tejun Heofda6f272013-05-14 13:52:36 -0700755 throtl_log(&tg->service_queue,
756 "[%c] extend slice start=%lu end=%lu jiffies=%lu",
757 rw == READ ? 'R' : 'W', tg->slice_start[rw],
758 tg->slice_end[rw], jiffies);
Vivek Goyale43473b2010-09-15 17:06:35 -0400759}
760
761/* Determine if previously allocated or extended slice is complete or not */
Tejun Heo0f3457f2013-05-14 13:52:32 -0700762static bool throtl_slice_used(struct throtl_grp *tg, bool rw)
Vivek Goyale43473b2010-09-15 17:06:35 -0400763{
764 if (time_in_range(jiffies, tg->slice_start[rw], tg->slice_end[rw]))
Fabian Frederick5cf8c222014-05-02 18:28:17 +0200765 return false;
Vivek Goyale43473b2010-09-15 17:06:35 -0400766
767 return 1;
768}
769
770/* Trim the used slices and adjust slice start accordingly */
Tejun Heo0f3457f2013-05-14 13:52:32 -0700771static inline void throtl_trim_slice(struct throtl_grp *tg, bool rw)
Vivek Goyale43473b2010-09-15 17:06:35 -0400772{
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200773 unsigned long nr_slices, time_elapsed, io_trim;
774 u64 bytes_trim, tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400775
776 BUG_ON(time_before(tg->slice_end[rw], tg->slice_start[rw]));
777
778 /*
779 * If bps are unlimited (-1), then time slice don't get
780 * renewed. Don't try to trim the slice if slice is used. A new
781 * slice will start when appropriate.
782 */
Tejun Heo0f3457f2013-05-14 13:52:32 -0700783 if (throtl_slice_used(tg, rw))
Vivek Goyale43473b2010-09-15 17:06:35 -0400784 return;
785
Vivek Goyald1ae8ff2010-12-01 19:34:46 +0100786 /*
787 * A bio has been dispatched. Also adjust slice_end. It might happen
788 * that initially cgroup limit was very low resulting in high
789 * slice_end, but later limit was bumped up and bio was dispached
790 * sooner, then we need to reduce slice_end. A high bogus slice_end
791 * is bad because it does not allow new slice to start.
792 */
793
Shaohua Li297e3d82017-03-27 10:51:37 -0700794 throtl_set_slice_end(tg, rw, jiffies + tg->td->throtl_slice);
Vivek Goyald1ae8ff2010-12-01 19:34:46 +0100795
Vivek Goyale43473b2010-09-15 17:06:35 -0400796 time_elapsed = jiffies - tg->slice_start[rw];
797
Shaohua Li297e3d82017-03-27 10:51:37 -0700798 nr_slices = time_elapsed / tg->td->throtl_slice;
Vivek Goyale43473b2010-09-15 17:06:35 -0400799
800 if (!nr_slices)
801 return;
Shaohua Li297e3d82017-03-27 10:51:37 -0700802 tmp = tg_bps_limit(tg, rw) * tg->td->throtl_slice * nr_slices;
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200803 do_div(tmp, HZ);
804 bytes_trim = tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400805
Shaohua Li297e3d82017-03-27 10:51:37 -0700806 io_trim = (tg_iops_limit(tg, rw) * tg->td->throtl_slice * nr_slices) /
807 HZ;
Vivek Goyale43473b2010-09-15 17:06:35 -0400808
Vivek Goyal8e89d132010-09-15 17:06:37 -0400809 if (!bytes_trim && !io_trim)
Vivek Goyale43473b2010-09-15 17:06:35 -0400810 return;
811
812 if (tg->bytes_disp[rw] >= bytes_trim)
813 tg->bytes_disp[rw] -= bytes_trim;
814 else
815 tg->bytes_disp[rw] = 0;
816
Vivek Goyal8e89d132010-09-15 17:06:37 -0400817 if (tg->io_disp[rw] >= io_trim)
818 tg->io_disp[rw] -= io_trim;
819 else
820 tg->io_disp[rw] = 0;
821
Shaohua Li297e3d82017-03-27 10:51:37 -0700822 tg->slice_start[rw] += nr_slices * tg->td->throtl_slice;
Vivek Goyale43473b2010-09-15 17:06:35 -0400823
Tejun Heofda6f272013-05-14 13:52:36 -0700824 throtl_log(&tg->service_queue,
825 "[%c] trim slice nr=%lu bytes=%llu io=%lu start=%lu end=%lu jiffies=%lu",
826 rw == READ ? 'R' : 'W', nr_slices, bytes_trim, io_trim,
827 tg->slice_start[rw], tg->slice_end[rw], jiffies);
Vivek Goyale43473b2010-09-15 17:06:35 -0400828}
829
Tejun Heo0f3457f2013-05-14 13:52:32 -0700830static bool tg_with_in_iops_limit(struct throtl_grp *tg, struct bio *bio,
831 unsigned long *wait)
Vivek Goyale43473b2010-09-15 17:06:35 -0400832{
833 bool rw = bio_data_dir(bio);
Vivek Goyal8e89d132010-09-15 17:06:37 -0400834 unsigned int io_allowed;
Vivek Goyale43473b2010-09-15 17:06:35 -0400835 unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
Vivek Goyalc49c06e2010-10-01 21:16:42 +0200836 u64 tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400837
Vivek Goyal8e89d132010-09-15 17:06:37 -0400838 jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
Vivek Goyale43473b2010-09-15 17:06:35 -0400839
Vivek Goyal8e89d132010-09-15 17:06:37 -0400840 /* Slice has just started. Consider one slice interval */
841 if (!jiffy_elapsed)
Shaohua Li297e3d82017-03-27 10:51:37 -0700842 jiffy_elapsed_rnd = tg->td->throtl_slice;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400843
Shaohua Li297e3d82017-03-27 10:51:37 -0700844 jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, tg->td->throtl_slice);
Vivek Goyal8e89d132010-09-15 17:06:37 -0400845
Vivek Goyalc49c06e2010-10-01 21:16:42 +0200846 /*
847 * jiffy_elapsed_rnd should not be a big value as minimum iops can be
848 * 1 then at max jiffy elapsed should be equivalent of 1 second as we
849 * will allow dispatch after 1 second and after that slice should
850 * have been trimmed.
851 */
852
Shaohua Li9f626e32017-03-27 10:51:30 -0700853 tmp = (u64)tg_iops_limit(tg, rw) * jiffy_elapsed_rnd;
Vivek Goyalc49c06e2010-10-01 21:16:42 +0200854 do_div(tmp, HZ);
855
856 if (tmp > UINT_MAX)
857 io_allowed = UINT_MAX;
858 else
859 io_allowed = tmp;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400860
861 if (tg->io_disp[rw] + 1 <= io_allowed) {
Vivek Goyale43473b2010-09-15 17:06:35 -0400862 if (wait)
863 *wait = 0;
Fabian Frederick5cf8c222014-05-02 18:28:17 +0200864 return true;
Vivek Goyale43473b2010-09-15 17:06:35 -0400865 }
866
Vivek Goyal8e89d132010-09-15 17:06:37 -0400867 /* Calc approx time to dispatch */
Shaohua Li9f626e32017-03-27 10:51:30 -0700868 jiffy_wait = ((tg->io_disp[rw] + 1) * HZ) / tg_iops_limit(tg, rw) + 1;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400869
870 if (jiffy_wait > jiffy_elapsed)
871 jiffy_wait = jiffy_wait - jiffy_elapsed;
872 else
873 jiffy_wait = 1;
874
875 if (wait)
876 *wait = jiffy_wait;
877 return 0;
878}
879
Tejun Heo0f3457f2013-05-14 13:52:32 -0700880static bool tg_with_in_bps_limit(struct throtl_grp *tg, struct bio *bio,
881 unsigned long *wait)
Vivek Goyal8e89d132010-09-15 17:06:37 -0400882{
883 bool rw = bio_data_dir(bio);
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200884 u64 bytes_allowed, extra_bytes, tmp;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400885 unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
Vivek Goyale43473b2010-09-15 17:06:35 -0400886
887 jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
888
889 /* Slice has just started. Consider one slice interval */
890 if (!jiffy_elapsed)
Shaohua Li297e3d82017-03-27 10:51:37 -0700891 jiffy_elapsed_rnd = tg->td->throtl_slice;
Vivek Goyale43473b2010-09-15 17:06:35 -0400892
Shaohua Li297e3d82017-03-27 10:51:37 -0700893 jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, tg->td->throtl_slice);
Vivek Goyale43473b2010-09-15 17:06:35 -0400894
Shaohua Li9f626e32017-03-27 10:51:30 -0700895 tmp = tg_bps_limit(tg, rw) * jiffy_elapsed_rnd;
Vivek Goyal5e901a22010-10-01 21:16:38 +0200896 do_div(tmp, HZ);
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200897 bytes_allowed = tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400898
Kent Overstreet4f024f32013-10-11 15:44:27 -0700899 if (tg->bytes_disp[rw] + bio->bi_iter.bi_size <= bytes_allowed) {
Vivek Goyale43473b2010-09-15 17:06:35 -0400900 if (wait)
901 *wait = 0;
Fabian Frederick5cf8c222014-05-02 18:28:17 +0200902 return true;
Vivek Goyale43473b2010-09-15 17:06:35 -0400903 }
904
905 /* Calc approx time to dispatch */
Kent Overstreet4f024f32013-10-11 15:44:27 -0700906 extra_bytes = tg->bytes_disp[rw] + bio->bi_iter.bi_size - bytes_allowed;
Shaohua Li9f626e32017-03-27 10:51:30 -0700907 jiffy_wait = div64_u64(extra_bytes * HZ, tg_bps_limit(tg, rw));
Vivek Goyale43473b2010-09-15 17:06:35 -0400908
909 if (!jiffy_wait)
910 jiffy_wait = 1;
911
912 /*
913 * This wait time is without taking into consideration the rounding
914 * up we did. Add that time also.
915 */
916 jiffy_wait = jiffy_wait + (jiffy_elapsed_rnd - jiffy_elapsed);
Vivek Goyale43473b2010-09-15 17:06:35 -0400917 if (wait)
918 *wait = jiffy_wait;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400919 return 0;
920}
Vivek Goyale43473b2010-09-15 17:06:35 -0400921
Vivek Goyal8e89d132010-09-15 17:06:37 -0400922/*
923 * Returns whether one can dispatch a bio or not. Also returns approx number
924 * of jiffies to wait before this bio is with-in IO rate and can be dispatched
925 */
Tejun Heo0f3457f2013-05-14 13:52:32 -0700926static bool tg_may_dispatch(struct throtl_grp *tg, struct bio *bio,
927 unsigned long *wait)
Vivek Goyal8e89d132010-09-15 17:06:37 -0400928{
929 bool rw = bio_data_dir(bio);
930 unsigned long bps_wait = 0, iops_wait = 0, max_wait = 0;
931
932 /*
933 * Currently whole state machine of group depends on first bio
934 * queued in the group bio list. So one should not be calling
935 * this function with a different bio if there are other bios
936 * queued.
937 */
Tejun Heo73f0d492013-05-14 13:52:35 -0700938 BUG_ON(tg->service_queue.nr_queued[rw] &&
Tejun Heoc5cc2072013-05-14 13:52:38 -0700939 bio != throtl_peek_queued(&tg->service_queue.queued[rw]));
Vivek Goyal8e89d132010-09-15 17:06:37 -0400940
941 /* If tg->bps = -1, then BW is unlimited */
Shaohua Li9f626e32017-03-27 10:51:30 -0700942 if (tg_bps_limit(tg, rw) == U64_MAX &&
943 tg_iops_limit(tg, rw) == UINT_MAX) {
Vivek Goyal8e89d132010-09-15 17:06:37 -0400944 if (wait)
945 *wait = 0;
Fabian Frederick5cf8c222014-05-02 18:28:17 +0200946 return true;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400947 }
948
949 /*
950 * If previous slice expired, start a new one otherwise renew/extend
951 * existing slice to make sure it is at least throtl_slice interval
Vivek Goyal164c80e2016-09-19 15:12:41 -0600952 * long since now. New slice is started only for empty throttle group.
953 * If there is queued bio, that means there should be an active
954 * slice and it should be extended instead.
Vivek Goyal8e89d132010-09-15 17:06:37 -0400955 */
Vivek Goyal164c80e2016-09-19 15:12:41 -0600956 if (throtl_slice_used(tg, rw) && !(tg->service_queue.nr_queued[rw]))
Tejun Heo0f3457f2013-05-14 13:52:32 -0700957 throtl_start_new_slice(tg, rw);
Vivek Goyal8e89d132010-09-15 17:06:37 -0400958 else {
Shaohua Li297e3d82017-03-27 10:51:37 -0700959 if (time_before(tg->slice_end[rw],
960 jiffies + tg->td->throtl_slice))
961 throtl_extend_slice(tg, rw,
962 jiffies + tg->td->throtl_slice);
Vivek Goyal8e89d132010-09-15 17:06:37 -0400963 }
964
Tejun Heo0f3457f2013-05-14 13:52:32 -0700965 if (tg_with_in_bps_limit(tg, bio, &bps_wait) &&
966 tg_with_in_iops_limit(tg, bio, &iops_wait)) {
Vivek Goyal8e89d132010-09-15 17:06:37 -0400967 if (wait)
968 *wait = 0;
969 return 1;
970 }
971
972 max_wait = max(bps_wait, iops_wait);
973
974 if (wait)
975 *wait = max_wait;
976
977 if (time_before(tg->slice_end[rw], jiffies + max_wait))
Tejun Heo0f3457f2013-05-14 13:52:32 -0700978 throtl_extend_slice(tg, rw, jiffies + max_wait);
Vivek Goyale43473b2010-09-15 17:06:35 -0400979
980 return 0;
981}
982
983static void throtl_charge_bio(struct throtl_grp *tg, struct bio *bio)
984{
985 bool rw = bio_data_dir(bio);
Vivek Goyale43473b2010-09-15 17:06:35 -0400986
987 /* Charge the bio to the group */
Kent Overstreet4f024f32013-10-11 15:44:27 -0700988 tg->bytes_disp[rw] += bio->bi_iter.bi_size;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400989 tg->io_disp[rw]++;
Shaohua Li3f0abd82017-03-27 10:51:35 -0700990 tg->last_bytes_disp[rw] += bio->bi_iter.bi_size;
991 tg->last_io_disp[rw]++;
Vivek Goyale43473b2010-09-15 17:06:35 -0400992
Tejun Heo2a0f61e2013-05-14 13:52:36 -0700993 /*
Christoph Hellwig8d2bbd42016-10-20 15:12:12 +0200994 * BIO_THROTTLED is used to prevent the same bio to be throttled
Tejun Heo2a0f61e2013-05-14 13:52:36 -0700995 * more than once as a throttled bio will go through blk-throtl the
996 * second time when it eventually gets issued. Set it when a bio
997 * is being charged to a tg.
Tejun Heo2a0f61e2013-05-14 13:52:36 -0700998 */
Christoph Hellwig8d2bbd42016-10-20 15:12:12 +0200999 if (!bio_flagged(bio, BIO_THROTTLED))
1000 bio_set_flag(bio, BIO_THROTTLED);
Vivek Goyale43473b2010-09-15 17:06:35 -04001001}
1002
Tejun Heoc5cc2072013-05-14 13:52:38 -07001003/**
1004 * throtl_add_bio_tg - add a bio to the specified throtl_grp
1005 * @bio: bio to add
1006 * @qn: qnode to use
1007 * @tg: the target throtl_grp
1008 *
1009 * Add @bio to @tg's service_queue using @qn. If @qn is not specified,
1010 * tg->qnode_on_self[] is used.
1011 */
1012static void throtl_add_bio_tg(struct bio *bio, struct throtl_qnode *qn,
1013 struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -04001014{
Tejun Heo73f0d492013-05-14 13:52:35 -07001015 struct throtl_service_queue *sq = &tg->service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -04001016 bool rw = bio_data_dir(bio);
1017
Tejun Heoc5cc2072013-05-14 13:52:38 -07001018 if (!qn)
1019 qn = &tg->qnode_on_self[rw];
1020
Tejun Heo0e9f4162013-05-14 13:52:35 -07001021 /*
1022 * If @tg doesn't currently have any bios queued in the same
1023 * direction, queueing @bio can change when @tg should be
1024 * dispatched. Mark that @tg was empty. This is automatically
1025 * cleaered on the next tg_update_disptime().
1026 */
1027 if (!sq->nr_queued[rw])
1028 tg->flags |= THROTL_TG_WAS_EMPTY;
1029
Tejun Heoc5cc2072013-05-14 13:52:38 -07001030 throtl_qnode_add_bio(bio, qn, &sq->queued[rw]);
1031
Tejun Heo73f0d492013-05-14 13:52:35 -07001032 sq->nr_queued[rw]++;
Tejun Heo77216b02013-05-14 13:52:36 -07001033 throtl_enqueue_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -04001034}
1035
Tejun Heo77216b02013-05-14 13:52:36 -07001036static void tg_update_disptime(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -04001037{
Tejun Heo73f0d492013-05-14 13:52:35 -07001038 struct throtl_service_queue *sq = &tg->service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -04001039 unsigned long read_wait = -1, write_wait = -1, min_wait = -1, disptime;
1040 struct bio *bio;
1041
Markus Elfringd609af32017-01-21 22:15:33 +01001042 bio = throtl_peek_queued(&sq->queued[READ]);
1043 if (bio)
Tejun Heo0f3457f2013-05-14 13:52:32 -07001044 tg_may_dispatch(tg, bio, &read_wait);
Vivek Goyale43473b2010-09-15 17:06:35 -04001045
Markus Elfringd609af32017-01-21 22:15:33 +01001046 bio = throtl_peek_queued(&sq->queued[WRITE]);
1047 if (bio)
Tejun Heo0f3457f2013-05-14 13:52:32 -07001048 tg_may_dispatch(tg, bio, &write_wait);
Vivek Goyale43473b2010-09-15 17:06:35 -04001049
1050 min_wait = min(read_wait, write_wait);
1051 disptime = jiffies + min_wait;
1052
Vivek Goyale43473b2010-09-15 17:06:35 -04001053 /* Update dispatch time */
Tejun Heo77216b02013-05-14 13:52:36 -07001054 throtl_dequeue_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -04001055 tg->disptime = disptime;
Tejun Heo77216b02013-05-14 13:52:36 -07001056 throtl_enqueue_tg(tg);
Tejun Heo0e9f4162013-05-14 13:52:35 -07001057
1058 /* see throtl_add_bio_tg() */
1059 tg->flags &= ~THROTL_TG_WAS_EMPTY;
Vivek Goyale43473b2010-09-15 17:06:35 -04001060}
1061
Vivek Goyal32ee5bc2013-05-14 13:52:38 -07001062static void start_parent_slice_with_credit(struct throtl_grp *child_tg,
1063 struct throtl_grp *parent_tg, bool rw)
1064{
1065 if (throtl_slice_used(parent_tg, rw)) {
1066 throtl_start_new_slice_with_credit(parent_tg, rw,
1067 child_tg->slice_start[rw]);
1068 }
1069
1070}
1071
Tejun Heo77216b02013-05-14 13:52:36 -07001072static void tg_dispatch_one_bio(struct throtl_grp *tg, bool rw)
Vivek Goyale43473b2010-09-15 17:06:35 -04001073{
Tejun Heo73f0d492013-05-14 13:52:35 -07001074 struct throtl_service_queue *sq = &tg->service_queue;
Tejun Heo6bc9c2b2013-05-14 13:52:38 -07001075 struct throtl_service_queue *parent_sq = sq->parent_sq;
1076 struct throtl_grp *parent_tg = sq_to_tg(parent_sq);
Tejun Heoc5cc2072013-05-14 13:52:38 -07001077 struct throtl_grp *tg_to_put = NULL;
Vivek Goyale43473b2010-09-15 17:06:35 -04001078 struct bio *bio;
1079
Tejun Heoc5cc2072013-05-14 13:52:38 -07001080 /*
1081 * @bio is being transferred from @tg to @parent_sq. Popping a bio
1082 * from @tg may put its reference and @parent_sq might end up
1083 * getting released prematurely. Remember the tg to put and put it
1084 * after @bio is transferred to @parent_sq.
1085 */
1086 bio = throtl_pop_queued(&sq->queued[rw], &tg_to_put);
Tejun Heo73f0d492013-05-14 13:52:35 -07001087 sq->nr_queued[rw]--;
Vivek Goyale43473b2010-09-15 17:06:35 -04001088
1089 throtl_charge_bio(tg, bio);
Tejun Heo6bc9c2b2013-05-14 13:52:38 -07001090
1091 /*
1092 * If our parent is another tg, we just need to transfer @bio to
1093 * the parent using throtl_add_bio_tg(). If our parent is
1094 * @td->service_queue, @bio is ready to be issued. Put it on its
1095 * bio_lists[] and decrease total number queued. The caller is
1096 * responsible for issuing these bios.
1097 */
1098 if (parent_tg) {
Tejun Heoc5cc2072013-05-14 13:52:38 -07001099 throtl_add_bio_tg(bio, &tg->qnode_on_parent[rw], parent_tg);
Vivek Goyal32ee5bc2013-05-14 13:52:38 -07001100 start_parent_slice_with_credit(tg, parent_tg, rw);
Tejun Heo6bc9c2b2013-05-14 13:52:38 -07001101 } else {
Tejun Heoc5cc2072013-05-14 13:52:38 -07001102 throtl_qnode_add_bio(bio, &tg->qnode_on_parent[rw],
1103 &parent_sq->queued[rw]);
Tejun Heo6bc9c2b2013-05-14 13:52:38 -07001104 BUG_ON(tg->td->nr_queued[rw] <= 0);
1105 tg->td->nr_queued[rw]--;
1106 }
Vivek Goyale43473b2010-09-15 17:06:35 -04001107
Tejun Heo0f3457f2013-05-14 13:52:32 -07001108 throtl_trim_slice(tg, rw);
Tejun Heo6bc9c2b2013-05-14 13:52:38 -07001109
Tejun Heoc5cc2072013-05-14 13:52:38 -07001110 if (tg_to_put)
1111 blkg_put(tg_to_blkg(tg_to_put));
Vivek Goyale43473b2010-09-15 17:06:35 -04001112}
1113
Tejun Heo77216b02013-05-14 13:52:36 -07001114static int throtl_dispatch_tg(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -04001115{
Tejun Heo73f0d492013-05-14 13:52:35 -07001116 struct throtl_service_queue *sq = &tg->service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -04001117 unsigned int nr_reads = 0, nr_writes = 0;
1118 unsigned int max_nr_reads = throtl_grp_quantum*3/4;
Vivek Goyalc2f68052010-11-15 19:32:42 +01001119 unsigned int max_nr_writes = throtl_grp_quantum - max_nr_reads;
Vivek Goyale43473b2010-09-15 17:06:35 -04001120 struct bio *bio;
1121
1122 /* Try to dispatch 75% READS and 25% WRITES */
1123
Tejun Heoc5cc2072013-05-14 13:52:38 -07001124 while ((bio = throtl_peek_queued(&sq->queued[READ])) &&
Tejun Heo0f3457f2013-05-14 13:52:32 -07001125 tg_may_dispatch(tg, bio, NULL)) {
Vivek Goyale43473b2010-09-15 17:06:35 -04001126
Tejun Heo77216b02013-05-14 13:52:36 -07001127 tg_dispatch_one_bio(tg, bio_data_dir(bio));
Vivek Goyale43473b2010-09-15 17:06:35 -04001128 nr_reads++;
1129
1130 if (nr_reads >= max_nr_reads)
1131 break;
1132 }
1133
Tejun Heoc5cc2072013-05-14 13:52:38 -07001134 while ((bio = throtl_peek_queued(&sq->queued[WRITE])) &&
Tejun Heo0f3457f2013-05-14 13:52:32 -07001135 tg_may_dispatch(tg, bio, NULL)) {
Vivek Goyale43473b2010-09-15 17:06:35 -04001136
Tejun Heo77216b02013-05-14 13:52:36 -07001137 tg_dispatch_one_bio(tg, bio_data_dir(bio));
Vivek Goyale43473b2010-09-15 17:06:35 -04001138 nr_writes++;
1139
1140 if (nr_writes >= max_nr_writes)
1141 break;
1142 }
1143
1144 return nr_reads + nr_writes;
1145}
1146
Tejun Heo651930b2013-05-14 13:52:35 -07001147static int throtl_select_dispatch(struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -04001148{
1149 unsigned int nr_disp = 0;
Vivek Goyale43473b2010-09-15 17:06:35 -04001150
1151 while (1) {
Tejun Heo73f0d492013-05-14 13:52:35 -07001152 struct throtl_grp *tg = throtl_rb_first(parent_sq);
1153 struct throtl_service_queue *sq = &tg->service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -04001154
1155 if (!tg)
1156 break;
1157
1158 if (time_before(jiffies, tg->disptime))
1159 break;
1160
Tejun Heo77216b02013-05-14 13:52:36 -07001161 throtl_dequeue_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -04001162
Tejun Heo77216b02013-05-14 13:52:36 -07001163 nr_disp += throtl_dispatch_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -04001164
Tejun Heo73f0d492013-05-14 13:52:35 -07001165 if (sq->nr_queued[0] || sq->nr_queued[1])
Tejun Heo77216b02013-05-14 13:52:36 -07001166 tg_update_disptime(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -04001167
1168 if (nr_disp >= throtl_quantum)
1169 break;
1170 }
1171
1172 return nr_disp;
1173}
1174
Shaohua Lic79892c2017-03-27 10:51:34 -07001175static bool throtl_can_upgrade(struct throtl_data *td,
1176 struct throtl_grp *this_tg);
Tejun Heo6e1a5702013-05-14 13:52:37 -07001177/**
1178 * throtl_pending_timer_fn - timer function for service_queue->pending_timer
1179 * @arg: the throtl_service_queue being serviced
1180 *
1181 * This timer is armed when a child throtl_grp with active bio's become
1182 * pending and queued on the service_queue's pending_tree and expires when
1183 * the first child throtl_grp should be dispatched. This function
Tejun Heo2e48a532013-05-14 13:52:38 -07001184 * dispatches bio's from the children throtl_grps to the parent
1185 * service_queue.
1186 *
1187 * If the parent's parent is another throtl_grp, dispatching is propagated
1188 * by either arming its pending_timer or repeating dispatch directly. If
1189 * the top-level service_tree is reached, throtl_data->dispatch_work is
1190 * kicked so that the ready bio's are issued.
Tejun Heo6e1a5702013-05-14 13:52:37 -07001191 */
Tejun Heo69df0ab2013-05-14 13:52:36 -07001192static void throtl_pending_timer_fn(unsigned long arg)
1193{
1194 struct throtl_service_queue *sq = (void *)arg;
Tejun Heo2e48a532013-05-14 13:52:38 -07001195 struct throtl_grp *tg = sq_to_tg(sq);
Tejun Heo69df0ab2013-05-14 13:52:36 -07001196 struct throtl_data *td = sq_to_td(sq);
Tejun Heocb761992013-05-14 13:52:31 -07001197 struct request_queue *q = td->queue;
Tejun Heo2e48a532013-05-14 13:52:38 -07001198 struct throtl_service_queue *parent_sq;
1199 bool dispatched;
Tejun Heo6e1a5702013-05-14 13:52:37 -07001200 int ret;
Vivek Goyale43473b2010-09-15 17:06:35 -04001201
1202 spin_lock_irq(q->queue_lock);
Shaohua Lic79892c2017-03-27 10:51:34 -07001203 if (throtl_can_upgrade(td, NULL))
1204 throtl_upgrade_state(td);
1205
Tejun Heo2e48a532013-05-14 13:52:38 -07001206again:
1207 parent_sq = sq->parent_sq;
1208 dispatched = false;
Vivek Goyale43473b2010-09-15 17:06:35 -04001209
Tejun Heo7f52f982013-05-14 13:52:37 -07001210 while (true) {
1211 throtl_log(sq, "dispatch nr_queued=%u read=%u write=%u",
Tejun Heo2e48a532013-05-14 13:52:38 -07001212 sq->nr_queued[READ] + sq->nr_queued[WRITE],
1213 sq->nr_queued[READ], sq->nr_queued[WRITE]);
Vivek Goyale43473b2010-09-15 17:06:35 -04001214
Tejun Heo7f52f982013-05-14 13:52:37 -07001215 ret = throtl_select_dispatch(sq);
1216 if (ret) {
Tejun Heo7f52f982013-05-14 13:52:37 -07001217 throtl_log(sq, "bios disp=%u", ret);
1218 dispatched = true;
Tejun Heo651930b2013-05-14 13:52:35 -07001219 }
Vivek Goyale43473b2010-09-15 17:06:35 -04001220
Tejun Heo7f52f982013-05-14 13:52:37 -07001221 if (throtl_schedule_next_dispatch(sq, false))
1222 break;
1223
1224 /* this dispatch windows is still open, relax and repeat */
1225 spin_unlock_irq(q->queue_lock);
1226 cpu_relax();
1227 spin_lock_irq(q->queue_lock);
1228 }
Tejun Heo6a525602013-05-14 13:52:32 -07001229
Tejun Heo2e48a532013-05-14 13:52:38 -07001230 if (!dispatched)
1231 goto out_unlock;
Tejun Heo6e1a5702013-05-14 13:52:37 -07001232
Tejun Heo2e48a532013-05-14 13:52:38 -07001233 if (parent_sq) {
1234 /* @parent_sq is another throl_grp, propagate dispatch */
1235 if (tg->flags & THROTL_TG_WAS_EMPTY) {
1236 tg_update_disptime(tg);
1237 if (!throtl_schedule_next_dispatch(parent_sq, false)) {
1238 /* window is already open, repeat dispatching */
1239 sq = parent_sq;
1240 tg = sq_to_tg(sq);
1241 goto again;
1242 }
1243 }
1244 } else {
1245 /* reached the top-level, queue issueing */
1246 queue_work(kthrotld_workqueue, &td->dispatch_work);
1247 }
1248out_unlock:
Tejun Heo6e1a5702013-05-14 13:52:37 -07001249 spin_unlock_irq(q->queue_lock);
1250}
1251
1252/**
1253 * blk_throtl_dispatch_work_fn - work function for throtl_data->dispatch_work
1254 * @work: work item being executed
1255 *
1256 * This function is queued for execution when bio's reach the bio_lists[]
1257 * of throtl_data->service_queue. Those bio's are ready and issued by this
1258 * function.
1259 */
Fabian Frederick8876e1402014-04-17 21:41:16 +02001260static void blk_throtl_dispatch_work_fn(struct work_struct *work)
Tejun Heo6e1a5702013-05-14 13:52:37 -07001261{
1262 struct throtl_data *td = container_of(work, struct throtl_data,
1263 dispatch_work);
1264 struct throtl_service_queue *td_sq = &td->service_queue;
1265 struct request_queue *q = td->queue;
1266 struct bio_list bio_list_on_stack;
1267 struct bio *bio;
1268 struct blk_plug plug;
1269 int rw;
1270
1271 bio_list_init(&bio_list_on_stack);
1272
1273 spin_lock_irq(q->queue_lock);
Tejun Heoc5cc2072013-05-14 13:52:38 -07001274 for (rw = READ; rw <= WRITE; rw++)
1275 while ((bio = throtl_pop_queued(&td_sq->queued[rw], NULL)))
1276 bio_list_add(&bio_list_on_stack, bio);
Vivek Goyale43473b2010-09-15 17:06:35 -04001277 spin_unlock_irq(q->queue_lock);
1278
Tejun Heo6e1a5702013-05-14 13:52:37 -07001279 if (!bio_list_empty(&bio_list_on_stack)) {
Vivek Goyal69d60eb2011-03-09 08:27:37 +01001280 blk_start_plug(&plug);
Vivek Goyale43473b2010-09-15 17:06:35 -04001281 while((bio = bio_list_pop(&bio_list_on_stack)))
1282 generic_make_request(bio);
Vivek Goyal69d60eb2011-03-09 08:27:37 +01001283 blk_finish_plug(&plug);
Vivek Goyale43473b2010-09-15 17:06:35 -04001284 }
Vivek Goyale43473b2010-09-15 17:06:35 -04001285}
1286
Tejun Heof95a04a2012-04-16 13:57:26 -07001287static u64 tg_prfill_conf_u64(struct seq_file *sf, struct blkg_policy_data *pd,
1288 int off)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001289{
Tejun Heof95a04a2012-04-16 13:57:26 -07001290 struct throtl_grp *tg = pd_to_tg(pd);
1291 u64 v = *(u64 *)((void *)tg + off);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001292
Shaohua Li2ab54922017-03-27 10:51:29 -07001293 if (v == U64_MAX)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001294 return 0;
Tejun Heof95a04a2012-04-16 13:57:26 -07001295 return __blkg_prfill_u64(sf, pd, v);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001296}
1297
Tejun Heof95a04a2012-04-16 13:57:26 -07001298static u64 tg_prfill_conf_uint(struct seq_file *sf, struct blkg_policy_data *pd,
1299 int off)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001300{
Tejun Heof95a04a2012-04-16 13:57:26 -07001301 struct throtl_grp *tg = pd_to_tg(pd);
1302 unsigned int v = *(unsigned int *)((void *)tg + off);
Tejun Heoaf133ce2012-04-01 14:38:44 -07001303
Shaohua Li2ab54922017-03-27 10:51:29 -07001304 if (v == UINT_MAX)
Tejun Heoaf133ce2012-04-01 14:38:44 -07001305 return 0;
Tejun Heof95a04a2012-04-16 13:57:26 -07001306 return __blkg_prfill_u64(sf, pd, v);
Tejun Heoaf133ce2012-04-01 14:38:44 -07001307}
1308
Tejun Heo2da8ca82013-12-05 12:28:04 -05001309static int tg_print_conf_u64(struct seq_file *sf, void *v)
Tejun Heoaf133ce2012-04-01 14:38:44 -07001310{
Tejun Heo2da8ca82013-12-05 12:28:04 -05001311 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), tg_prfill_conf_u64,
1312 &blkcg_policy_throtl, seq_cft(sf)->private, false);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001313 return 0;
1314}
1315
Tejun Heo2da8ca82013-12-05 12:28:04 -05001316static int tg_print_conf_uint(struct seq_file *sf, void *v)
Vivek Goyale43473b2010-09-15 17:06:35 -04001317{
Tejun Heo2da8ca82013-12-05 12:28:04 -05001318 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), tg_prfill_conf_uint,
1319 &blkcg_policy_throtl, seq_cft(sf)->private, false);
Tejun Heoaf133ce2012-04-01 14:38:44 -07001320 return 0;
Vivek Goyale43473b2010-09-15 17:06:35 -04001321}
1322
Tejun Heo69948b02015-08-18 14:55:32 -07001323static void tg_conf_updated(struct throtl_grp *tg)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001324{
Tejun Heo69948b02015-08-18 14:55:32 -07001325 struct throtl_service_queue *sq = &tg->service_queue;
Tejun Heo492eb212013-08-08 20:11:25 -04001326 struct cgroup_subsys_state *pos_css;
Tejun Heo69948b02015-08-18 14:55:32 -07001327 struct blkcg_gq *blkg;
Tejun Heoaf133ce2012-04-01 14:38:44 -07001328
Tejun Heofda6f272013-05-14 13:52:36 -07001329 throtl_log(&tg->service_queue,
1330 "limit change rbps=%llu wbps=%llu riops=%u wiops=%u",
Shaohua Li9f626e32017-03-27 10:51:30 -07001331 tg_bps_limit(tg, READ), tg_bps_limit(tg, WRITE),
1332 tg_iops_limit(tg, READ), tg_iops_limit(tg, WRITE));
Tejun Heo632b4492013-05-14 13:52:31 -07001333
1334 /*
Tejun Heo693e7512013-05-14 13:52:38 -07001335 * Update has_rules[] flags for the updated tg's subtree. A tg is
1336 * considered to have rules if either the tg itself or any of its
1337 * ancestors has rules. This identifies groups without any
1338 * restrictions in the whole hierarchy and allows them to bypass
1339 * blk-throttle.
1340 */
Tejun Heo69948b02015-08-18 14:55:32 -07001341 blkg_for_each_descendant_pre(blkg, pos_css, tg_to_blkg(tg))
Tejun Heo693e7512013-05-14 13:52:38 -07001342 tg_update_has_rules(blkg_to_tg(blkg));
1343
1344 /*
Tejun Heo632b4492013-05-14 13:52:31 -07001345 * We're already holding queue_lock and know @tg is valid. Let's
1346 * apply the new config directly.
1347 *
1348 * Restart the slices for both READ and WRITES. It might happen
1349 * that a group's limit are dropped suddenly and we don't want to
1350 * account recently dispatched IO with new low rate.
1351 */
Tejun Heo0f3457f2013-05-14 13:52:32 -07001352 throtl_start_new_slice(tg, 0);
1353 throtl_start_new_slice(tg, 1);
Tejun Heo632b4492013-05-14 13:52:31 -07001354
Tejun Heo5b2c16a2013-05-14 13:52:32 -07001355 if (tg->flags & THROTL_TG_PENDING) {
Tejun Heo77216b02013-05-14 13:52:36 -07001356 tg_update_disptime(tg);
Tejun Heo7f52f982013-05-14 13:52:37 -07001357 throtl_schedule_next_dispatch(sq->parent_sq, true);
Tejun Heo632b4492013-05-14 13:52:31 -07001358 }
Tejun Heo69948b02015-08-18 14:55:32 -07001359}
Tejun Heo60c2bc22012-04-01 14:38:43 -07001360
Tejun Heo69948b02015-08-18 14:55:32 -07001361static ssize_t tg_set_conf(struct kernfs_open_file *of,
1362 char *buf, size_t nbytes, loff_t off, bool is_u64)
1363{
1364 struct blkcg *blkcg = css_to_blkcg(of_css(of));
1365 struct blkg_conf_ctx ctx;
1366 struct throtl_grp *tg;
1367 int ret;
1368 u64 v;
1369
1370 ret = blkg_conf_prep(blkcg, &blkcg_policy_throtl, buf, &ctx);
1371 if (ret)
1372 return ret;
1373
1374 ret = -EINVAL;
1375 if (sscanf(ctx.body, "%llu", &v) != 1)
1376 goto out_finish;
1377 if (!v)
Shaohua Li2ab54922017-03-27 10:51:29 -07001378 v = U64_MAX;
Tejun Heo69948b02015-08-18 14:55:32 -07001379
1380 tg = blkg_to_tg(ctx.blkg);
1381
1382 if (is_u64)
1383 *(u64 *)((void *)tg + of_cft(of)->private) = v;
1384 else
1385 *(unsigned int *)((void *)tg + of_cft(of)->private) = v;
1386
1387 tg_conf_updated(tg);
Tejun Heo36aa9e52015-08-18 14:55:31 -07001388 ret = 0;
1389out_finish:
Tejun Heo60c2bc22012-04-01 14:38:43 -07001390 blkg_conf_finish(&ctx);
Tejun Heo36aa9e52015-08-18 14:55:31 -07001391 return ret ?: nbytes;
Tejun Heo60c2bc22012-04-01 14:38:43 -07001392}
1393
Tejun Heo451af502014-05-13 12:16:21 -04001394static ssize_t tg_set_conf_u64(struct kernfs_open_file *of,
1395 char *buf, size_t nbytes, loff_t off)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001396{
Tejun Heo451af502014-05-13 12:16:21 -04001397 return tg_set_conf(of, buf, nbytes, off, true);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001398}
1399
Tejun Heo451af502014-05-13 12:16:21 -04001400static ssize_t tg_set_conf_uint(struct kernfs_open_file *of,
1401 char *buf, size_t nbytes, loff_t off)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001402{
Tejun Heo451af502014-05-13 12:16:21 -04001403 return tg_set_conf(of, buf, nbytes, off, false);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001404}
1405
Tejun Heo880f50e2015-08-18 14:55:30 -07001406static struct cftype throtl_legacy_files[] = {
Tejun Heo60c2bc22012-04-01 14:38:43 -07001407 {
1408 .name = "throttle.read_bps_device",
Shaohua Li9f626e32017-03-27 10:51:30 -07001409 .private = offsetof(struct throtl_grp, bps[READ][LIMIT_MAX]),
Tejun Heo2da8ca82013-12-05 12:28:04 -05001410 .seq_show = tg_print_conf_u64,
Tejun Heo451af502014-05-13 12:16:21 -04001411 .write = tg_set_conf_u64,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001412 },
1413 {
1414 .name = "throttle.write_bps_device",
Shaohua Li9f626e32017-03-27 10:51:30 -07001415 .private = offsetof(struct throtl_grp, bps[WRITE][LIMIT_MAX]),
Tejun Heo2da8ca82013-12-05 12:28:04 -05001416 .seq_show = tg_print_conf_u64,
Tejun Heo451af502014-05-13 12:16:21 -04001417 .write = tg_set_conf_u64,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001418 },
1419 {
1420 .name = "throttle.read_iops_device",
Shaohua Li9f626e32017-03-27 10:51:30 -07001421 .private = offsetof(struct throtl_grp, iops[READ][LIMIT_MAX]),
Tejun Heo2da8ca82013-12-05 12:28:04 -05001422 .seq_show = tg_print_conf_uint,
Tejun Heo451af502014-05-13 12:16:21 -04001423 .write = tg_set_conf_uint,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001424 },
1425 {
1426 .name = "throttle.write_iops_device",
Shaohua Li9f626e32017-03-27 10:51:30 -07001427 .private = offsetof(struct throtl_grp, iops[WRITE][LIMIT_MAX]),
Tejun Heo2da8ca82013-12-05 12:28:04 -05001428 .seq_show = tg_print_conf_uint,
Tejun Heo451af502014-05-13 12:16:21 -04001429 .write = tg_set_conf_uint,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001430 },
1431 {
1432 .name = "throttle.io_service_bytes",
Tejun Heo77ea7332015-08-18 14:55:24 -07001433 .private = (unsigned long)&blkcg_policy_throtl,
1434 .seq_show = blkg_print_stat_bytes,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001435 },
1436 {
1437 .name = "throttle.io_serviced",
Tejun Heo77ea7332015-08-18 14:55:24 -07001438 .private = (unsigned long)&blkcg_policy_throtl,
1439 .seq_show = blkg_print_stat_ios,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001440 },
1441 { } /* terminate */
1442};
1443
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001444static u64 tg_prfill_limit(struct seq_file *sf, struct blkg_policy_data *pd,
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001445 int off)
1446{
1447 struct throtl_grp *tg = pd_to_tg(pd);
1448 const char *dname = blkg_dev_name(pd->blkg);
1449 char bufs[4][21] = { "max", "max", "max", "max" };
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001450 u64 bps_dft;
1451 unsigned int iops_dft;
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001452
1453 if (!dname)
1454 return 0;
Shaohua Li9f626e32017-03-27 10:51:30 -07001455
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001456 if (off == LIMIT_LOW) {
1457 bps_dft = 0;
1458 iops_dft = 0;
1459 } else {
1460 bps_dft = U64_MAX;
1461 iops_dft = UINT_MAX;
1462 }
1463
1464 if (tg->bps_conf[READ][off] == bps_dft &&
1465 tg->bps_conf[WRITE][off] == bps_dft &&
1466 tg->iops_conf[READ][off] == iops_dft &&
1467 tg->iops_conf[WRITE][off] == iops_dft)
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001468 return 0;
1469
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001470 if (tg->bps_conf[READ][off] != bps_dft)
Shaohua Li9f626e32017-03-27 10:51:30 -07001471 snprintf(bufs[0], sizeof(bufs[0]), "%llu",
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001472 tg->bps_conf[READ][off]);
1473 if (tg->bps_conf[WRITE][off] != bps_dft)
Shaohua Li9f626e32017-03-27 10:51:30 -07001474 snprintf(bufs[1], sizeof(bufs[1]), "%llu",
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001475 tg->bps_conf[WRITE][off]);
1476 if (tg->iops_conf[READ][off] != iops_dft)
Shaohua Li9f626e32017-03-27 10:51:30 -07001477 snprintf(bufs[2], sizeof(bufs[2]), "%u",
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001478 tg->iops_conf[READ][off]);
1479 if (tg->iops_conf[WRITE][off] != iops_dft)
Shaohua Li9f626e32017-03-27 10:51:30 -07001480 snprintf(bufs[3], sizeof(bufs[3]), "%u",
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001481 tg->iops_conf[WRITE][off]);
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001482
1483 seq_printf(sf, "%s rbps=%s wbps=%s riops=%s wiops=%s\n",
1484 dname, bufs[0], bufs[1], bufs[2], bufs[3]);
1485 return 0;
1486}
1487
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001488static int tg_print_limit(struct seq_file *sf, void *v)
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001489{
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001490 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), tg_prfill_limit,
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001491 &blkcg_policy_throtl, seq_cft(sf)->private, false);
1492 return 0;
1493}
1494
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001495static ssize_t tg_set_limit(struct kernfs_open_file *of,
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001496 char *buf, size_t nbytes, loff_t off)
1497{
1498 struct blkcg *blkcg = css_to_blkcg(of_css(of));
1499 struct blkg_conf_ctx ctx;
1500 struct throtl_grp *tg;
1501 u64 v[4];
1502 int ret;
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001503 int index = of_cft(of)->private;
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001504
1505 ret = blkg_conf_prep(blkcg, &blkcg_policy_throtl, buf, &ctx);
1506 if (ret)
1507 return ret;
1508
1509 tg = blkg_to_tg(ctx.blkg);
1510
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001511 v[0] = tg->bps_conf[READ][index];
1512 v[1] = tg->bps_conf[WRITE][index];
1513 v[2] = tg->iops_conf[READ][index];
1514 v[3] = tg->iops_conf[WRITE][index];
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001515
1516 while (true) {
1517 char tok[27]; /* wiops=18446744073709551616 */
1518 char *p;
Shaohua Li2ab54922017-03-27 10:51:29 -07001519 u64 val = U64_MAX;
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001520 int len;
1521
1522 if (sscanf(ctx.body, "%26s%n", tok, &len) != 1)
1523 break;
1524 if (tok[0] == '\0')
1525 break;
1526 ctx.body += len;
1527
1528 ret = -EINVAL;
1529 p = tok;
1530 strsep(&p, "=");
1531 if (!p || (sscanf(p, "%llu", &val) != 1 && strcmp(p, "max")))
1532 goto out_finish;
1533
1534 ret = -ERANGE;
1535 if (!val)
1536 goto out_finish;
1537
1538 ret = -EINVAL;
1539 if (!strcmp(tok, "rbps"))
1540 v[0] = val;
1541 else if (!strcmp(tok, "wbps"))
1542 v[1] = val;
1543 else if (!strcmp(tok, "riops"))
1544 v[2] = min_t(u64, val, UINT_MAX);
1545 else if (!strcmp(tok, "wiops"))
1546 v[3] = min_t(u64, val, UINT_MAX);
1547 else
1548 goto out_finish;
1549 }
1550
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001551 tg->bps_conf[READ][index] = v[0];
1552 tg->bps_conf[WRITE][index] = v[1];
1553 tg->iops_conf[READ][index] = v[2];
1554 tg->iops_conf[WRITE][index] = v[3];
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001555
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001556 if (index == LIMIT_MAX) {
1557 tg->bps[READ][index] = v[0];
1558 tg->bps[WRITE][index] = v[1];
1559 tg->iops[READ][index] = v[2];
1560 tg->iops[WRITE][index] = v[3];
1561 }
1562 tg->bps[READ][LIMIT_LOW] = min(tg->bps_conf[READ][LIMIT_LOW],
1563 tg->bps_conf[READ][LIMIT_MAX]);
1564 tg->bps[WRITE][LIMIT_LOW] = min(tg->bps_conf[WRITE][LIMIT_LOW],
1565 tg->bps_conf[WRITE][LIMIT_MAX]);
1566 tg->iops[READ][LIMIT_LOW] = min(tg->iops_conf[READ][LIMIT_LOW],
1567 tg->iops_conf[READ][LIMIT_MAX]);
1568 tg->iops[WRITE][LIMIT_LOW] = min(tg->iops_conf[WRITE][LIMIT_LOW],
1569 tg->iops_conf[WRITE][LIMIT_MAX]);
1570
1571 if (index == LIMIT_LOW) {
1572 blk_throtl_update_limit_valid(tg->td);
1573 if (tg->td->limit_valid[LIMIT_LOW])
1574 tg->td->limit_index = LIMIT_LOW;
1575 }
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001576 tg_conf_updated(tg);
1577 ret = 0;
1578out_finish:
1579 blkg_conf_finish(&ctx);
1580 return ret ?: nbytes;
1581}
1582
1583static struct cftype throtl_files[] = {
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001584#ifdef CONFIG_BLK_DEV_THROTTLING_LOW
1585 {
1586 .name = "low",
1587 .flags = CFTYPE_NOT_ON_ROOT,
1588 .seq_show = tg_print_limit,
1589 .write = tg_set_limit,
1590 .private = LIMIT_LOW,
1591 },
1592#endif
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001593 {
1594 .name = "max",
1595 .flags = CFTYPE_NOT_ON_ROOT,
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001596 .seq_show = tg_print_limit,
1597 .write = tg_set_limit,
1598 .private = LIMIT_MAX,
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001599 },
1600 { } /* terminate */
1601};
1602
Vivek Goyalda527772011-03-02 19:05:33 -05001603static void throtl_shutdown_wq(struct request_queue *q)
Vivek Goyale43473b2010-09-15 17:06:35 -04001604{
1605 struct throtl_data *td = q->td;
1606
Tejun Heo69df0ab2013-05-14 13:52:36 -07001607 cancel_work_sync(&td->dispatch_work);
Vivek Goyale43473b2010-09-15 17:06:35 -04001608}
1609
Tejun Heo3c798392012-04-16 13:57:25 -07001610static struct blkcg_policy blkcg_policy_throtl = {
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001611 .dfl_cftypes = throtl_files,
Tejun Heo880f50e2015-08-18 14:55:30 -07001612 .legacy_cftypes = throtl_legacy_files,
Tejun Heof9fcc2d2012-04-16 13:57:27 -07001613
Tejun Heo001bea72015-08-18 14:55:11 -07001614 .pd_alloc_fn = throtl_pd_alloc,
Tejun Heof9fcc2d2012-04-16 13:57:27 -07001615 .pd_init_fn = throtl_pd_init,
Tejun Heo693e7512013-05-14 13:52:38 -07001616 .pd_online_fn = throtl_pd_online,
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001617 .pd_offline_fn = throtl_pd_offline,
Tejun Heo001bea72015-08-18 14:55:11 -07001618 .pd_free_fn = throtl_pd_free,
Vivek Goyale43473b2010-09-15 17:06:35 -04001619};
1620
Shaohua Li3f0abd82017-03-27 10:51:35 -07001621static unsigned long __tg_last_low_overflow_time(struct throtl_grp *tg)
1622{
1623 unsigned long rtime = jiffies, wtime = jiffies;
1624
1625 if (tg->bps[READ][LIMIT_LOW] || tg->iops[READ][LIMIT_LOW])
1626 rtime = tg->last_low_overflow_time[READ];
1627 if (tg->bps[WRITE][LIMIT_LOW] || tg->iops[WRITE][LIMIT_LOW])
1628 wtime = tg->last_low_overflow_time[WRITE];
1629 return min(rtime, wtime);
1630}
1631
1632/* tg should not be an intermediate node */
1633static unsigned long tg_last_low_overflow_time(struct throtl_grp *tg)
1634{
1635 struct throtl_service_queue *parent_sq;
1636 struct throtl_grp *parent = tg;
1637 unsigned long ret = __tg_last_low_overflow_time(tg);
1638
1639 while (true) {
1640 parent_sq = parent->service_queue.parent_sq;
1641 parent = sq_to_tg(parent_sq);
1642 if (!parent)
1643 break;
1644
1645 /*
1646 * The parent doesn't have low limit, it always reaches low
1647 * limit. Its overflow time is useless for children
1648 */
1649 if (!parent->bps[READ][LIMIT_LOW] &&
1650 !parent->iops[READ][LIMIT_LOW] &&
1651 !parent->bps[WRITE][LIMIT_LOW] &&
1652 !parent->iops[WRITE][LIMIT_LOW])
1653 continue;
1654 if (time_after(__tg_last_low_overflow_time(parent), ret))
1655 ret = __tg_last_low_overflow_time(parent);
1656 }
1657 return ret;
1658}
1659
Shaohua Li9e234ee2017-03-27 10:51:41 -07001660static bool throtl_tg_is_idle(struct throtl_grp *tg)
1661{
1662 /*
1663 * cgroup is idle if:
1664 * - single idle is too long, longer than a fixed value (in case user
1665 * configure a too big threshold) or 4 times of slice
1666 * - average think time is more than threshold
1667 */
1668 unsigned long time = jiffies_to_usecs(4 * tg->td->throtl_slice);
1669
1670 time = min_t(unsigned long, MAX_IDLE_TIME, time);
1671 return (ktime_get_ns() >> 10) - tg->last_finish_time > time ||
1672 tg->avg_idletime > tg->idletime_threshold;
1673}
1674
Shaohua Lic79892c2017-03-27 10:51:34 -07001675static bool throtl_tg_can_upgrade(struct throtl_grp *tg)
1676{
1677 struct throtl_service_queue *sq = &tg->service_queue;
1678 bool read_limit, write_limit;
1679
1680 /*
1681 * if cgroup reaches low limit (if low limit is 0, the cgroup always
1682 * reaches), it's ok to upgrade to next limit
1683 */
1684 read_limit = tg->bps[READ][LIMIT_LOW] || tg->iops[READ][LIMIT_LOW];
1685 write_limit = tg->bps[WRITE][LIMIT_LOW] || tg->iops[WRITE][LIMIT_LOW];
1686 if (!read_limit && !write_limit)
1687 return true;
1688 if (read_limit && sq->nr_queued[READ] &&
1689 (!write_limit || sq->nr_queued[WRITE]))
1690 return true;
1691 if (write_limit && sq->nr_queued[WRITE] &&
1692 (!read_limit || sq->nr_queued[READ]))
1693 return true;
Shaohua Liaec24242017-03-27 10:51:39 -07001694
1695 if (time_after_eq(jiffies,
1696 tg->last_dispatch_time[READ] + tg->td->throtl_slice) &&
1697 time_after_eq(jiffies,
1698 tg->last_dispatch_time[WRITE] + tg->td->throtl_slice))
1699 return true;
Shaohua Lic79892c2017-03-27 10:51:34 -07001700 return false;
1701}
1702
1703static bool throtl_hierarchy_can_upgrade(struct throtl_grp *tg)
1704{
1705 while (true) {
1706 if (throtl_tg_can_upgrade(tg))
1707 return true;
1708 tg = sq_to_tg(tg->service_queue.parent_sq);
1709 if (!tg || !tg_to_blkg(tg)->parent)
1710 return false;
1711 }
1712 return false;
1713}
1714
1715static bool throtl_can_upgrade(struct throtl_data *td,
1716 struct throtl_grp *this_tg)
1717{
1718 struct cgroup_subsys_state *pos_css;
1719 struct blkcg_gq *blkg;
1720
1721 if (td->limit_index != LIMIT_LOW)
1722 return false;
1723
Shaohua Li297e3d82017-03-27 10:51:37 -07001724 if (time_before(jiffies, td->low_downgrade_time + td->throtl_slice))
Shaohua Li3f0abd82017-03-27 10:51:35 -07001725 return false;
1726
Shaohua Lic79892c2017-03-27 10:51:34 -07001727 rcu_read_lock();
1728 blkg_for_each_descendant_post(blkg, pos_css, td->queue->root_blkg) {
1729 struct throtl_grp *tg = blkg_to_tg(blkg);
1730
1731 if (tg == this_tg)
1732 continue;
1733 if (!list_empty(&tg_to_blkg(tg)->blkcg->css.children))
1734 continue;
1735 if (!throtl_hierarchy_can_upgrade(tg)) {
1736 rcu_read_unlock();
1737 return false;
1738 }
1739 }
1740 rcu_read_unlock();
1741 return true;
1742}
1743
1744static void throtl_upgrade_state(struct throtl_data *td)
1745{
1746 struct cgroup_subsys_state *pos_css;
1747 struct blkcg_gq *blkg;
1748
1749 td->limit_index = LIMIT_MAX;
Shaohua Li3f0abd82017-03-27 10:51:35 -07001750 td->low_upgrade_time = jiffies;
Shaohua Li7394e312017-03-27 10:51:40 -07001751 td->scale = 0;
Shaohua Lic79892c2017-03-27 10:51:34 -07001752 rcu_read_lock();
1753 blkg_for_each_descendant_post(blkg, pos_css, td->queue->root_blkg) {
1754 struct throtl_grp *tg = blkg_to_tg(blkg);
1755 struct throtl_service_queue *sq = &tg->service_queue;
1756
1757 tg->disptime = jiffies - 1;
1758 throtl_select_dispatch(sq);
1759 throtl_schedule_next_dispatch(sq, false);
1760 }
1761 rcu_read_unlock();
1762 throtl_select_dispatch(&td->service_queue);
1763 throtl_schedule_next_dispatch(&td->service_queue, false);
1764 queue_work(kthrotld_workqueue, &td->dispatch_work);
1765}
1766
Shaohua Li3f0abd82017-03-27 10:51:35 -07001767static void throtl_downgrade_state(struct throtl_data *td, int new)
1768{
Shaohua Li7394e312017-03-27 10:51:40 -07001769 td->scale /= 2;
1770
1771 if (td->scale) {
1772 td->low_upgrade_time = jiffies - td->scale * td->throtl_slice;
1773 return;
1774 }
1775
Shaohua Li3f0abd82017-03-27 10:51:35 -07001776 td->limit_index = new;
1777 td->low_downgrade_time = jiffies;
1778}
1779
1780static bool throtl_tg_can_downgrade(struct throtl_grp *tg)
1781{
1782 struct throtl_data *td = tg->td;
1783 unsigned long now = jiffies;
1784
Shaohua Liaec24242017-03-27 10:51:39 -07001785 if (time_after_eq(now, tg->last_dispatch_time[READ] +
1786 td->throtl_slice) &&
1787 time_after_eq(now, tg->last_dispatch_time[WRITE] +
1788 td->throtl_slice))
1789 return false;
Shaohua Li3f0abd82017-03-27 10:51:35 -07001790 /*
1791 * If cgroup is below low limit, consider downgrade and throttle other
1792 * cgroups
1793 */
Shaohua Li297e3d82017-03-27 10:51:37 -07001794 if (time_after_eq(now, td->low_upgrade_time + td->throtl_slice) &&
1795 time_after_eq(now, tg_last_low_overflow_time(tg) +
1796 td->throtl_slice))
Shaohua Li3f0abd82017-03-27 10:51:35 -07001797 return true;
1798 return false;
1799}
1800
1801static bool throtl_hierarchy_can_downgrade(struct throtl_grp *tg)
1802{
1803 while (true) {
1804 if (!throtl_tg_can_downgrade(tg))
1805 return false;
1806 tg = sq_to_tg(tg->service_queue.parent_sq);
1807 if (!tg || !tg_to_blkg(tg)->parent)
1808 break;
1809 }
1810 return true;
1811}
1812
1813static void throtl_downgrade_check(struct throtl_grp *tg)
1814{
1815 uint64_t bps;
1816 unsigned int iops;
1817 unsigned long elapsed_time;
1818 unsigned long now = jiffies;
1819
1820 if (tg->td->limit_index != LIMIT_MAX ||
1821 !tg->td->limit_valid[LIMIT_LOW])
1822 return;
1823 if (!list_empty(&tg_to_blkg(tg)->blkcg->css.children))
1824 return;
Shaohua Li297e3d82017-03-27 10:51:37 -07001825 if (time_after(tg->last_check_time + tg->td->throtl_slice, now))
Shaohua Li3f0abd82017-03-27 10:51:35 -07001826 return;
1827
1828 elapsed_time = now - tg->last_check_time;
1829 tg->last_check_time = now;
1830
Shaohua Li297e3d82017-03-27 10:51:37 -07001831 if (time_before(now, tg_last_low_overflow_time(tg) +
1832 tg->td->throtl_slice))
Shaohua Li3f0abd82017-03-27 10:51:35 -07001833 return;
1834
1835 if (tg->bps[READ][LIMIT_LOW]) {
1836 bps = tg->last_bytes_disp[READ] * HZ;
1837 do_div(bps, elapsed_time);
1838 if (bps >= tg->bps[READ][LIMIT_LOW])
1839 tg->last_low_overflow_time[READ] = now;
1840 }
1841
1842 if (tg->bps[WRITE][LIMIT_LOW]) {
1843 bps = tg->last_bytes_disp[WRITE] * HZ;
1844 do_div(bps, elapsed_time);
1845 if (bps >= tg->bps[WRITE][LIMIT_LOW])
1846 tg->last_low_overflow_time[WRITE] = now;
1847 }
1848
1849 if (tg->iops[READ][LIMIT_LOW]) {
1850 iops = tg->last_io_disp[READ] * HZ / elapsed_time;
1851 if (iops >= tg->iops[READ][LIMIT_LOW])
1852 tg->last_low_overflow_time[READ] = now;
1853 }
1854
1855 if (tg->iops[WRITE][LIMIT_LOW]) {
1856 iops = tg->last_io_disp[WRITE] * HZ / elapsed_time;
1857 if (iops >= tg->iops[WRITE][LIMIT_LOW])
1858 tg->last_low_overflow_time[WRITE] = now;
1859 }
1860
1861 /*
1862 * If cgroup is below low limit, consider downgrade and throttle other
1863 * cgroups
1864 */
1865 if (throtl_hierarchy_can_downgrade(tg))
1866 throtl_downgrade_state(tg->td, LIMIT_LOW);
1867
1868 tg->last_bytes_disp[READ] = 0;
1869 tg->last_bytes_disp[WRITE] = 0;
1870 tg->last_io_disp[READ] = 0;
1871 tg->last_io_disp[WRITE] = 0;
1872}
1873
Shaohua Li9e234ee2017-03-27 10:51:41 -07001874static void blk_throtl_update_idletime(struct throtl_grp *tg)
1875{
1876 unsigned long now = ktime_get_ns() >> 10;
1877 unsigned long last_finish_time = tg->last_finish_time;
1878
1879 if (now <= last_finish_time || last_finish_time == 0 ||
1880 last_finish_time == tg->checked_last_finish_time)
1881 return;
1882
1883 tg->avg_idletime = (tg->avg_idletime * 7 + now - last_finish_time) >> 3;
1884 tg->checked_last_finish_time = last_finish_time;
1885}
1886
Tejun Heoae118892015-08-18 14:55:20 -07001887bool blk_throtl_bio(struct request_queue *q, struct blkcg_gq *blkg,
1888 struct bio *bio)
Vivek Goyale43473b2010-09-15 17:06:35 -04001889{
Tejun Heoc5cc2072013-05-14 13:52:38 -07001890 struct throtl_qnode *qn = NULL;
Tejun Heoae118892015-08-18 14:55:20 -07001891 struct throtl_grp *tg = blkg_to_tg(blkg ?: q->root_blkg);
Tejun Heo73f0d492013-05-14 13:52:35 -07001892 struct throtl_service_queue *sq;
Tejun Heo0e9f4162013-05-14 13:52:35 -07001893 bool rw = bio_data_dir(bio);
Tejun Heobc16a4f2011-10-19 14:33:01 +02001894 bool throttled = false;
Shaohua Li9e234ee2017-03-27 10:51:41 -07001895 int ret;
Vivek Goyale43473b2010-09-15 17:06:35 -04001896
Tejun Heoae118892015-08-18 14:55:20 -07001897 WARN_ON_ONCE(!rcu_read_lock_held());
1898
Tejun Heo2a0f61e2013-05-14 13:52:36 -07001899 /* see throtl_charge_bio() */
Christoph Hellwig8d2bbd42016-10-20 15:12:12 +02001900 if (bio_flagged(bio, BIO_THROTTLED) || !tg->has_rules[rw])
Tejun Heobc16a4f2011-10-19 14:33:01 +02001901 goto out;
Vivek Goyale43473b2010-09-15 17:06:35 -04001902
1903 spin_lock_irq(q->queue_lock);
Tejun Heoc9589f02015-08-18 14:55:19 -07001904
1905 if (unlikely(blk_queue_bypass(q)))
Tejun Heobc16a4f2011-10-19 14:33:01 +02001906 goto out_unlock;
Vivek Goyalf469a7b2011-05-19 15:38:23 -04001907
Shaohua Li9e234ee2017-03-27 10:51:41 -07001908 ret = bio_associate_current(bio);
1909#ifdef CONFIG_BLK_DEV_THROTTLING_LOW
1910 if (ret == 0 || ret == -EBUSY)
1911 bio->bi_cg_private = tg;
1912#endif
1913 blk_throtl_update_idletime(tg);
1914
Tejun Heo73f0d492013-05-14 13:52:35 -07001915 sq = &tg->service_queue;
1916
Shaohua Lic79892c2017-03-27 10:51:34 -07001917again:
Tejun Heo9e660ac2013-05-14 13:52:38 -07001918 while (true) {
Shaohua Liaec24242017-03-27 10:51:39 -07001919 tg->last_dispatch_time[rw] = jiffies;
Shaohua Li3f0abd82017-03-27 10:51:35 -07001920 if (tg->last_low_overflow_time[rw] == 0)
1921 tg->last_low_overflow_time[rw] = jiffies;
1922 throtl_downgrade_check(tg);
Tejun Heo9e660ac2013-05-14 13:52:38 -07001923 /* throtl is FIFO - if bios are already queued, should queue */
1924 if (sq->nr_queued[rw])
1925 break;
Vivek Goyalde701c72011-03-07 21:09:32 +01001926
Tejun Heo9e660ac2013-05-14 13:52:38 -07001927 /* if above limits, break to queue */
Shaohua Lic79892c2017-03-27 10:51:34 -07001928 if (!tg_may_dispatch(tg, bio, NULL)) {
Shaohua Li3f0abd82017-03-27 10:51:35 -07001929 tg->last_low_overflow_time[rw] = jiffies;
Shaohua Lic79892c2017-03-27 10:51:34 -07001930 if (throtl_can_upgrade(tg->td, tg)) {
1931 throtl_upgrade_state(tg->td);
1932 goto again;
1933 }
Tejun Heo9e660ac2013-05-14 13:52:38 -07001934 break;
Shaohua Lic79892c2017-03-27 10:51:34 -07001935 }
Tejun Heo9e660ac2013-05-14 13:52:38 -07001936
1937 /* within limits, let's charge and dispatch directly */
Vivek Goyale43473b2010-09-15 17:06:35 -04001938 throtl_charge_bio(tg, bio);
Vivek Goyal04521db2011-03-22 21:54:29 +01001939
1940 /*
1941 * We need to trim slice even when bios are not being queued
1942 * otherwise it might happen that a bio is not queued for
1943 * a long time and slice keeps on extending and trim is not
1944 * called for a long time. Now if limits are reduced suddenly
1945 * we take into account all the IO dispatched so far at new
1946 * low rate and * newly queued IO gets a really long dispatch
1947 * time.
1948 *
1949 * So keep on trimming slice even if bio is not queued.
1950 */
Tejun Heo0f3457f2013-05-14 13:52:32 -07001951 throtl_trim_slice(tg, rw);
Tejun Heo9e660ac2013-05-14 13:52:38 -07001952
1953 /*
1954 * @bio passed through this layer without being throttled.
1955 * Climb up the ladder. If we''re already at the top, it
1956 * can be executed directly.
1957 */
Tejun Heoc5cc2072013-05-14 13:52:38 -07001958 qn = &tg->qnode_on_parent[rw];
Tejun Heo9e660ac2013-05-14 13:52:38 -07001959 sq = sq->parent_sq;
1960 tg = sq_to_tg(sq);
1961 if (!tg)
1962 goto out_unlock;
Vivek Goyale43473b2010-09-15 17:06:35 -04001963 }
1964
Tejun Heo9e660ac2013-05-14 13:52:38 -07001965 /* out-of-limit, queue to @tg */
Tejun Heofda6f272013-05-14 13:52:36 -07001966 throtl_log(sq, "[%c] bio. bdisp=%llu sz=%u bps=%llu iodisp=%u iops=%u queued=%d/%d",
1967 rw == READ ? 'R' : 'W',
Shaohua Li9f626e32017-03-27 10:51:30 -07001968 tg->bytes_disp[rw], bio->bi_iter.bi_size,
1969 tg_bps_limit(tg, rw),
1970 tg->io_disp[rw], tg_iops_limit(tg, rw),
Tejun Heofda6f272013-05-14 13:52:36 -07001971 sq->nr_queued[READ], sq->nr_queued[WRITE]);
Vivek Goyale43473b2010-09-15 17:06:35 -04001972
Shaohua Li3f0abd82017-03-27 10:51:35 -07001973 tg->last_low_overflow_time[rw] = jiffies;
1974
Tejun Heo6bc9c2b2013-05-14 13:52:38 -07001975 tg->td->nr_queued[rw]++;
Tejun Heoc5cc2072013-05-14 13:52:38 -07001976 throtl_add_bio_tg(bio, qn, tg);
Tejun Heobc16a4f2011-10-19 14:33:01 +02001977 throttled = true;
Vivek Goyale43473b2010-09-15 17:06:35 -04001978
Tejun Heo7f52f982013-05-14 13:52:37 -07001979 /*
1980 * Update @tg's dispatch time and force schedule dispatch if @tg
1981 * was empty before @bio. The forced scheduling isn't likely to
1982 * cause undue delay as @bio is likely to be dispatched directly if
1983 * its @tg's disptime is not in the future.
1984 */
Tejun Heo0e9f4162013-05-14 13:52:35 -07001985 if (tg->flags & THROTL_TG_WAS_EMPTY) {
Tejun Heo77216b02013-05-14 13:52:36 -07001986 tg_update_disptime(tg);
Tejun Heo7f52f982013-05-14 13:52:37 -07001987 throtl_schedule_next_dispatch(tg->service_queue.parent_sq, true);
Vivek Goyale43473b2010-09-15 17:06:35 -04001988 }
1989
Tejun Heobc16a4f2011-10-19 14:33:01 +02001990out_unlock:
Vivek Goyale43473b2010-09-15 17:06:35 -04001991 spin_unlock_irq(q->queue_lock);
Tejun Heobc16a4f2011-10-19 14:33:01 +02001992out:
Tejun Heo2a0f61e2013-05-14 13:52:36 -07001993 /*
1994 * As multiple blk-throtls may stack in the same issue path, we
1995 * don't want bios to leave with the flag set. Clear the flag if
1996 * being issued.
1997 */
1998 if (!throttled)
Christoph Hellwig8d2bbd42016-10-20 15:12:12 +02001999 bio_clear_flag(bio, BIO_THROTTLED);
Tejun Heobc16a4f2011-10-19 14:33:01 +02002000 return throttled;
Vivek Goyale43473b2010-09-15 17:06:35 -04002001}
2002
Shaohua Li9e234ee2017-03-27 10:51:41 -07002003#ifdef CONFIG_BLK_DEV_THROTTLING_LOW
2004void blk_throtl_bio_endio(struct bio *bio)
2005{
2006 struct throtl_grp *tg;
2007
2008 tg = bio->bi_cg_private;
2009 if (!tg)
2010 return;
2011 bio->bi_cg_private = NULL;
2012
2013 tg->last_finish_time = ktime_get_ns() >> 10;
2014}
2015#endif
2016
Tejun Heo2a12f0d2013-05-14 13:52:37 -07002017/*
2018 * Dispatch all bios from all children tg's queued on @parent_sq. On
2019 * return, @parent_sq is guaranteed to not have any active children tg's
2020 * and all bios from previously active tg's are on @parent_sq->bio_lists[].
2021 */
2022static void tg_drain_bios(struct throtl_service_queue *parent_sq)
2023{
2024 struct throtl_grp *tg;
2025
2026 while ((tg = throtl_rb_first(parent_sq))) {
2027 struct throtl_service_queue *sq = &tg->service_queue;
2028 struct bio *bio;
2029
2030 throtl_dequeue_tg(tg);
2031
Tejun Heoc5cc2072013-05-14 13:52:38 -07002032 while ((bio = throtl_peek_queued(&sq->queued[READ])))
Tejun Heo2a12f0d2013-05-14 13:52:37 -07002033 tg_dispatch_one_bio(tg, bio_data_dir(bio));
Tejun Heoc5cc2072013-05-14 13:52:38 -07002034 while ((bio = throtl_peek_queued(&sq->queued[WRITE])))
Tejun Heo2a12f0d2013-05-14 13:52:37 -07002035 tg_dispatch_one_bio(tg, bio_data_dir(bio));
2036 }
2037}
2038
Tejun Heoc9a929d2011-10-19 14:42:16 +02002039/**
2040 * blk_throtl_drain - drain throttled bios
2041 * @q: request_queue to drain throttled bios for
2042 *
2043 * Dispatch all currently throttled bios on @q through ->make_request_fn().
2044 */
2045void blk_throtl_drain(struct request_queue *q)
2046 __releases(q->queue_lock) __acquires(q->queue_lock)
2047{
2048 struct throtl_data *td = q->td;
Tejun Heo2a12f0d2013-05-14 13:52:37 -07002049 struct blkcg_gq *blkg;
Tejun Heo492eb212013-08-08 20:11:25 -04002050 struct cgroup_subsys_state *pos_css;
Tejun Heoc9a929d2011-10-19 14:42:16 +02002051 struct bio *bio;
Tejun Heo651930b2013-05-14 13:52:35 -07002052 int rw;
Tejun Heoc9a929d2011-10-19 14:42:16 +02002053
Andi Kleen8bcb6c72012-03-30 12:33:28 +02002054 queue_lockdep_assert_held(q);
Tejun Heo2a12f0d2013-05-14 13:52:37 -07002055 rcu_read_lock();
Tejun Heoc9a929d2011-10-19 14:42:16 +02002056
Tejun Heo2a12f0d2013-05-14 13:52:37 -07002057 /*
2058 * Drain each tg while doing post-order walk on the blkg tree, so
2059 * that all bios are propagated to td->service_queue. It'd be
2060 * better to walk service_queue tree directly but blkg walk is
2061 * easier.
2062 */
Tejun Heo492eb212013-08-08 20:11:25 -04002063 blkg_for_each_descendant_post(blkg, pos_css, td->queue->root_blkg)
Tejun Heo2a12f0d2013-05-14 13:52:37 -07002064 tg_drain_bios(&blkg_to_tg(blkg)->service_queue);
Tejun Heo73f0d492013-05-14 13:52:35 -07002065
Tejun Heo2a12f0d2013-05-14 13:52:37 -07002066 /* finally, transfer bios from top-level tg's into the td */
2067 tg_drain_bios(&td->service_queue);
2068
2069 rcu_read_unlock();
Tejun Heoc9a929d2011-10-19 14:42:16 +02002070 spin_unlock_irq(q->queue_lock);
2071
Tejun Heo2a12f0d2013-05-14 13:52:37 -07002072 /* all bios now should be in td->service_queue, issue them */
Tejun Heo651930b2013-05-14 13:52:35 -07002073 for (rw = READ; rw <= WRITE; rw++)
Tejun Heoc5cc2072013-05-14 13:52:38 -07002074 while ((bio = throtl_pop_queued(&td->service_queue.queued[rw],
2075 NULL)))
Tejun Heo651930b2013-05-14 13:52:35 -07002076 generic_make_request(bio);
Tejun Heoc9a929d2011-10-19 14:42:16 +02002077
2078 spin_lock_irq(q->queue_lock);
2079}
2080
Vivek Goyale43473b2010-09-15 17:06:35 -04002081int blk_throtl_init(struct request_queue *q)
2082{
2083 struct throtl_data *td;
Tejun Heoa2b16932012-04-13 13:11:33 -07002084 int ret;
Vivek Goyale43473b2010-09-15 17:06:35 -04002085
2086 td = kzalloc_node(sizeof(*td), GFP_KERNEL, q->node);
2087 if (!td)
2088 return -ENOMEM;
2089
Tejun Heo69df0ab2013-05-14 13:52:36 -07002090 INIT_WORK(&td->dispatch_work, blk_throtl_dispatch_work_fn);
Tejun Heob2ce2642015-08-18 14:55:13 -07002091 throtl_service_queue_init(&td->service_queue);
Vivek Goyale43473b2010-09-15 17:06:35 -04002092
Tejun Heocd1604f2012-03-05 13:15:06 -08002093 q->td = td;
Vivek Goyal29b12582011-05-19 15:38:24 -04002094 td->queue = q;
Vivek Goyal02977e42010-10-01 14:49:48 +02002095
Shaohua Li9f626e32017-03-27 10:51:30 -07002096 td->limit_valid[LIMIT_MAX] = true;
Shaohua Licd5ab1b2017-03-27 10:51:32 -07002097 td->limit_index = LIMIT_MAX;
Shaohua Li3f0abd82017-03-27 10:51:35 -07002098 td->low_upgrade_time = jiffies;
2099 td->low_downgrade_time = jiffies;
Shaohua Li9e234ee2017-03-27 10:51:41 -07002100
Tejun Heoa2b16932012-04-13 13:11:33 -07002101 /* activate policy */
Tejun Heo3c798392012-04-16 13:57:25 -07002102 ret = blkcg_activate_policy(q, &blkcg_policy_throtl);
Tejun Heoa2b16932012-04-13 13:11:33 -07002103 if (ret)
Vivek Goyal29b12582011-05-19 15:38:24 -04002104 kfree(td);
Tejun Heoa2b16932012-04-13 13:11:33 -07002105 return ret;
Vivek Goyale43473b2010-09-15 17:06:35 -04002106}
2107
2108void blk_throtl_exit(struct request_queue *q)
2109{
Tejun Heoc875f4d2012-03-05 13:15:22 -08002110 BUG_ON(!q->td);
Vivek Goyalda527772011-03-02 19:05:33 -05002111 throtl_shutdown_wq(q);
Tejun Heo3c798392012-04-16 13:57:25 -07002112 blkcg_deactivate_policy(q, &blkcg_policy_throtl);
Tejun Heoc9a929d2011-10-19 14:42:16 +02002113 kfree(q->td);
Vivek Goyale43473b2010-09-15 17:06:35 -04002114}
2115
Shaohua Lid61fcfa2017-03-27 10:51:38 -07002116void blk_throtl_register_queue(struct request_queue *q)
2117{
2118 struct throtl_data *td;
Shaohua Li9e234ee2017-03-27 10:51:41 -07002119 struct cgroup_subsys_state *pos_css;
2120 struct blkcg_gq *blkg;
Shaohua Lid61fcfa2017-03-27 10:51:38 -07002121
2122 td = q->td;
2123 BUG_ON(!td);
2124
2125 if (blk_queue_nonrot(q))
2126 td->throtl_slice = DFL_THROTL_SLICE_SSD;
2127 else
2128 td->throtl_slice = DFL_THROTL_SLICE_HD;
2129#ifndef CONFIG_BLK_DEV_THROTTLING_LOW
2130 /* if no low limit, use previous default */
2131 td->throtl_slice = DFL_THROTL_SLICE_HD;
2132#endif
Shaohua Li9e234ee2017-03-27 10:51:41 -07002133
2134 /*
2135 * some tg are created before queue is fully initialized, eg, nonrot
2136 * isn't initialized yet
2137 */
2138 rcu_read_lock();
2139 blkg_for_each_descendant_post(blkg, pos_css, q->root_blkg) {
2140 struct throtl_grp *tg = blkg_to_tg(blkg);
2141
2142 if (blk_queue_nonrot(q))
2143 tg->idletime_threshold = DFL_IDLE_THRESHOLD_SSD;
2144 else
2145 tg->idletime_threshold = DFL_IDLE_THRESHOLD_HD;
2146 }
2147 rcu_read_unlock();
Shaohua Lid61fcfa2017-03-27 10:51:38 -07002148}
2149
Shaohua Li297e3d82017-03-27 10:51:37 -07002150#ifdef CONFIG_BLK_DEV_THROTTLING_LOW
2151ssize_t blk_throtl_sample_time_show(struct request_queue *q, char *page)
2152{
2153 if (!q->td)
2154 return -EINVAL;
2155 return sprintf(page, "%u\n", jiffies_to_msecs(q->td->throtl_slice));
2156}
2157
2158ssize_t blk_throtl_sample_time_store(struct request_queue *q,
2159 const char *page, size_t count)
2160{
2161 unsigned long v;
2162 unsigned long t;
2163
2164 if (!q->td)
2165 return -EINVAL;
2166 if (kstrtoul(page, 10, &v))
2167 return -EINVAL;
2168 t = msecs_to_jiffies(v);
2169 if (t == 0 || t > MAX_THROTL_SLICE)
2170 return -EINVAL;
2171 q->td->throtl_slice = t;
2172 return count;
2173}
2174#endif
2175
Vivek Goyale43473b2010-09-15 17:06:35 -04002176static int __init throtl_init(void)
2177{
Vivek Goyal450adcb2011-03-01 13:40:54 -05002178 kthrotld_workqueue = alloc_workqueue("kthrotld", WQ_MEM_RECLAIM, 0);
2179 if (!kthrotld_workqueue)
2180 panic("Failed to create kthrotld\n");
2181
Tejun Heo3c798392012-04-16 13:57:25 -07002182 return blkcg_policy_register(&blkcg_policy_throtl);
Vivek Goyale43473b2010-09-15 17:06:35 -04002183}
2184
2185module_init(throtl_init);