]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - block/blk-throttle.c
blk-throttle: implement sq_to_tg(), sq_to_td() and throtl_log()
[mirror_ubuntu-bionic-kernel.git] / block / blk-throttle.c
CommitLineData
e43473b7
VG
1/*
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>
12#include "blk-cgroup.h"
bc9fcbf9 13#include "blk.h"
e43473b7
VG
14
15/* Max dispatch from a group in 1 round */
16static int throtl_grp_quantum = 8;
17
18/* Total max dispatch from all groups in one round */
19static int throtl_quantum = 32;
20
21/* Throttling is performed over 100ms slice and after that slice is renewed */
22static unsigned long throtl_slice = HZ/10; /* 100 ms */
23
3c798398 24static struct blkcg_policy blkcg_policy_throtl;
0381411e 25
450adcbe
VG
26/* A workqueue to queue throttle related work */
27static struct workqueue_struct *kthrotld_workqueue;
450adcbe 28
c9e0332e 29struct throtl_service_queue {
77216b04
TH
30 struct throtl_service_queue *parent_sq; /* the parent service_queue */
31
73f0d49a
TH
32 /*
33 * Bios queued directly to this service_queue or dispatched from
34 * children throtl_grp's.
35 */
36 struct bio_list bio_lists[2]; /* queued bios [READ/WRITE] */
37 unsigned int nr_queued[2]; /* number of queued bios */
38
39 /*
40 * RB tree of active children throtl_grp's, which are sorted by
41 * their ->disptime.
42 */
c9e0332e
TH
43 struct rb_root pending_tree; /* RB tree of active tgs */
44 struct rb_node *first_pending; /* first node in the tree */
45 unsigned int nr_pending; /* # queued in the tree */
46 unsigned long first_pending_disptime; /* disptime of the first tg */
e43473b7
VG
47};
48
5b2c16aa
TH
49enum tg_state_flags {
50 THROTL_TG_PENDING = 1 << 0, /* on parent's pending tree */
0e9f4164 51 THROTL_TG_WAS_EMPTY = 1 << 1, /* bio_lists[] became non-empty */
5b2c16aa
TH
52};
53
e43473b7
VG
54#define rb_entry_tg(node) rb_entry((node), struct throtl_grp, rb_node)
55
8a3d2615
TH
56/* Per-cpu group stats */
57struct tg_stats_cpu {
58 /* total bytes transferred */
59 struct blkg_rwstat service_bytes;
60 /* total IOs serviced, post merge */
61 struct blkg_rwstat serviced;
62};
63
e43473b7 64struct throtl_grp {
f95a04af
TH
65 /* must be the first member */
66 struct blkg_policy_data pd;
67
c9e0332e 68 /* active throtl group service_queue member */
e43473b7
VG
69 struct rb_node rb_node;
70
0f3457f6
TH
71 /* throtl_data this group belongs to */
72 struct throtl_data *td;
73
49a2f1e3
TH
74 /* this group's service queue */
75 struct throtl_service_queue service_queue;
76
e43473b7
VG
77 /*
78 * Dispatch time in jiffies. This is the estimated time when group
79 * will unthrottle and is ready to dispatch more bio. It is used as
80 * key to sort active groups in service tree.
81 */
82 unsigned long disptime;
83
e43473b7
VG
84 unsigned int flags;
85
e43473b7
VG
86 /* bytes per second rate limits */
87 uint64_t bps[2];
88
8e89d13f
VG
89 /* IOPS limits */
90 unsigned int iops[2];
91
e43473b7
VG
92 /* Number of bytes disptached in current slice */
93 uint64_t bytes_disp[2];
8e89d13f
VG
94 /* Number of bio's dispatched in current slice */
95 unsigned int io_disp[2];
e43473b7
VG
96
97 /* When did we start a new slice */
98 unsigned long slice_start[2];
99 unsigned long slice_end[2];
fe071437 100
8a3d2615
TH
101 /* Per cpu stats pointer */
102 struct tg_stats_cpu __percpu *stats_cpu;
103
104 /* List of tgs waiting for per cpu stats memory to be allocated */
105 struct list_head stats_alloc_node;
e43473b7
VG
106};
107
108struct throtl_data
109{
e43473b7 110 /* service tree for active throtl groups */
c9e0332e 111 struct throtl_service_queue service_queue;
e43473b7 112
e43473b7
VG
113 struct request_queue *queue;
114
115 /* Total Number of queued bios on READ and WRITE lists */
116 unsigned int nr_queued[2];
117
118 /*
02977e4a 119 * number of total undestroyed groups
e43473b7
VG
120 */
121 unsigned int nr_undestroyed_grps;
122
123 /* Work for dispatching throttled bios */
cb76199c 124 struct delayed_work dispatch_work;
e43473b7
VG
125};
126
8a3d2615
TH
127/* list and work item to allocate percpu group stats */
128static DEFINE_SPINLOCK(tg_stats_alloc_lock);
129static LIST_HEAD(tg_stats_alloc_list);
130
131static void tg_stats_alloc_fn(struct work_struct *);
132static DECLARE_DELAYED_WORK(tg_stats_alloc_work, tg_stats_alloc_fn);
133
f95a04af
TH
134static inline struct throtl_grp *pd_to_tg(struct blkg_policy_data *pd)
135{
136 return pd ? container_of(pd, struct throtl_grp, pd) : NULL;
137}
138
3c798398 139static inline struct throtl_grp *blkg_to_tg(struct blkcg_gq *blkg)
0381411e 140{
f95a04af 141 return pd_to_tg(blkg_to_pd(blkg, &blkcg_policy_throtl));
0381411e
TH
142}
143
3c798398 144static inline struct blkcg_gq *tg_to_blkg(struct throtl_grp *tg)
0381411e 145{
f95a04af 146 return pd_to_blkg(&tg->pd);
0381411e
TH
147}
148
03d8e111
TH
149static inline struct throtl_grp *td_root_tg(struct throtl_data *td)
150{
151 return blkg_to_tg(td->queue->root_blkg);
152}
153
fda6f272
TH
154/**
155 * sq_to_tg - return the throl_grp the specified service queue belongs to
156 * @sq: the throtl_service_queue of interest
157 *
158 * Return the throtl_grp @sq belongs to. If @sq is the top-level one
159 * embedded in throtl_data, %NULL is returned.
160 */
161static struct throtl_grp *sq_to_tg(struct throtl_service_queue *sq)
162{
163 if (sq && sq->parent_sq)
164 return container_of(sq, struct throtl_grp, service_queue);
165 else
166 return NULL;
167}
168
169/**
170 * sq_to_td - return throtl_data the specified service queue belongs to
171 * @sq: the throtl_service_queue of interest
172 *
173 * A service_queue can be embeded in either a throtl_grp or throtl_data.
174 * Determine the associated throtl_data accordingly and return it.
175 */
176static struct throtl_data *sq_to_td(struct throtl_service_queue *sq)
177{
178 struct throtl_grp *tg = sq_to_tg(sq);
179
180 if (tg)
181 return tg->td;
182 else
183 return container_of(sq, struct throtl_data, service_queue);
184}
185
186/**
187 * throtl_log - log debug message via blktrace
188 * @sq: the service_queue being reported
189 * @fmt: printf format string
190 * @args: printf args
191 *
192 * The messages are prefixed with "throtl BLKG_NAME" if @sq belongs to a
193 * throtl_grp; otherwise, just "throtl".
194 *
195 * TODO: this should be made a function and name formatting should happen
196 * after testing whether blktrace is enabled.
197 */
198#define throtl_log(sq, fmt, args...) do { \
199 struct throtl_grp *__tg = sq_to_tg((sq)); \
200 struct throtl_data *__td = sq_to_td((sq)); \
201 \
202 (void)__td; \
203 if ((__tg)) { \
204 char __pbuf[128]; \
54e7ed12 205 \
fda6f272
TH
206 blkg_path(tg_to_blkg(__tg), __pbuf, sizeof(__pbuf)); \
207 blk_add_trace_msg(__td->queue, "throtl %s " fmt, __pbuf, ##args); \
208 } else { \
209 blk_add_trace_msg(__td->queue, "throtl " fmt, ##args); \
210 } \
54e7ed12 211} while (0)
e43473b7 212
8a3d2615
TH
213/*
214 * Worker for allocating per cpu stat for tgs. This is scheduled on the
3b07e9ca 215 * system_wq once there are some groups on the alloc_list waiting for
8a3d2615
TH
216 * allocation.
217 */
218static void tg_stats_alloc_fn(struct work_struct *work)
219{
220 static struct tg_stats_cpu *stats_cpu; /* this fn is non-reentrant */
221 struct delayed_work *dwork = to_delayed_work(work);
222 bool empty = false;
223
224alloc_stats:
225 if (!stats_cpu) {
226 stats_cpu = alloc_percpu(struct tg_stats_cpu);
227 if (!stats_cpu) {
228 /* allocation failed, try again after some time */
3b07e9ca 229 schedule_delayed_work(dwork, msecs_to_jiffies(10));
8a3d2615
TH
230 return;
231 }
232 }
233
234 spin_lock_irq(&tg_stats_alloc_lock);
235
236 if (!list_empty(&tg_stats_alloc_list)) {
237 struct throtl_grp *tg = list_first_entry(&tg_stats_alloc_list,
238 struct throtl_grp,
239 stats_alloc_node);
240 swap(tg->stats_cpu, stats_cpu);
241 list_del_init(&tg->stats_alloc_node);
242 }
243
244 empty = list_empty(&tg_stats_alloc_list);
245 spin_unlock_irq(&tg_stats_alloc_lock);
246 if (!empty)
247 goto alloc_stats;
248}
249
49a2f1e3 250/* init a service_queue, assumes the caller zeroed it */
77216b04
TH
251static void throtl_service_queue_init(struct throtl_service_queue *sq,
252 struct throtl_service_queue *parent_sq)
49a2f1e3 253{
73f0d49a
TH
254 bio_list_init(&sq->bio_lists[0]);
255 bio_list_init(&sq->bio_lists[1]);
49a2f1e3 256 sq->pending_tree = RB_ROOT;
77216b04 257 sq->parent_sq = parent_sq;
49a2f1e3
TH
258}
259
3c798398 260static void throtl_pd_init(struct blkcg_gq *blkg)
a29a171e 261{
0381411e 262 struct throtl_grp *tg = blkg_to_tg(blkg);
77216b04 263 struct throtl_data *td = blkg->q->td;
ff26eaad 264 unsigned long flags;
cd1604fa 265
77216b04 266 throtl_service_queue_init(&tg->service_queue, &td->service_queue);
a29a171e 267 RB_CLEAR_NODE(&tg->rb_node);
77216b04 268 tg->td = td;
a29a171e 269
e56da7e2
TH
270 tg->bps[READ] = -1;
271 tg->bps[WRITE] = -1;
272 tg->iops[READ] = -1;
273 tg->iops[WRITE] = -1;
8a3d2615
TH
274
275 /*
276 * Ugh... We need to perform per-cpu allocation for tg->stats_cpu
277 * but percpu allocator can't be called from IO path. Queue tg on
278 * tg_stats_alloc_list and allocate from work item.
279 */
ff26eaad 280 spin_lock_irqsave(&tg_stats_alloc_lock, flags);
8a3d2615 281 list_add(&tg->stats_alloc_node, &tg_stats_alloc_list);
3b07e9ca 282 schedule_delayed_work(&tg_stats_alloc_work, 0);
ff26eaad 283 spin_unlock_irqrestore(&tg_stats_alloc_lock, flags);
8a3d2615
TH
284}
285
3c798398 286static void throtl_pd_exit(struct blkcg_gq *blkg)
8a3d2615
TH
287{
288 struct throtl_grp *tg = blkg_to_tg(blkg);
ff26eaad 289 unsigned long flags;
8a3d2615 290
ff26eaad 291 spin_lock_irqsave(&tg_stats_alloc_lock, flags);
8a3d2615 292 list_del_init(&tg->stats_alloc_node);
ff26eaad 293 spin_unlock_irqrestore(&tg_stats_alloc_lock, flags);
8a3d2615
TH
294
295 free_percpu(tg->stats_cpu);
296}
297
3c798398 298static void throtl_pd_reset_stats(struct blkcg_gq *blkg)
8a3d2615
TH
299{
300 struct throtl_grp *tg = blkg_to_tg(blkg);
301 int cpu;
302
303 if (tg->stats_cpu == NULL)
304 return;
305
306 for_each_possible_cpu(cpu) {
307 struct tg_stats_cpu *sc = per_cpu_ptr(tg->stats_cpu, cpu);
308
309 blkg_rwstat_reset(&sc->service_bytes);
310 blkg_rwstat_reset(&sc->serviced);
311 }
a29a171e
VG
312}
313
3c798398
TH
314static struct throtl_grp *throtl_lookup_tg(struct throtl_data *td,
315 struct blkcg *blkcg)
e43473b7 316{
be2c6b19 317 /*
3c798398
TH
318 * This is the common case when there are no blkcgs. Avoid lookup
319 * in this case
cd1604fa 320 */
3c798398 321 if (blkcg == &blkcg_root)
03d8e111 322 return td_root_tg(td);
e43473b7 323
e8989fae 324 return blkg_to_tg(blkg_lookup(blkcg, td->queue));
e43473b7
VG
325}
326
cd1604fa 327static struct throtl_grp *throtl_lookup_create_tg(struct throtl_data *td,
3c798398 328 struct blkcg *blkcg)
e43473b7 329{
f469a7b4 330 struct request_queue *q = td->queue;
cd1604fa 331 struct throtl_grp *tg = NULL;
bc16a4f9 332
f469a7b4 333 /*
3c798398
TH
334 * This is the common case when there are no blkcgs. Avoid lookup
335 * in this case
f469a7b4 336 */
3c798398 337 if (blkcg == &blkcg_root) {
03d8e111 338 tg = td_root_tg(td);
cd1604fa 339 } else {
3c798398 340 struct blkcg_gq *blkg;
f469a7b4 341
3c96cb32 342 blkg = blkg_lookup_create(blkcg, q);
f469a7b4 343
cd1604fa
TH
344 /* if %NULL and @q is alive, fall back to root_tg */
345 if (!IS_ERR(blkg))
0381411e 346 tg = blkg_to_tg(blkg);
3f3299d5 347 else if (!blk_queue_dying(q))
03d8e111 348 tg = td_root_tg(td);
f469a7b4
VG
349 }
350
e43473b7
VG
351 return tg;
352}
353
0049af73
TH
354static struct throtl_grp *
355throtl_rb_first(struct throtl_service_queue *parent_sq)
e43473b7
VG
356{
357 /* Service tree is empty */
0049af73 358 if (!parent_sq->nr_pending)
e43473b7
VG
359 return NULL;
360
0049af73
TH
361 if (!parent_sq->first_pending)
362 parent_sq->first_pending = rb_first(&parent_sq->pending_tree);
e43473b7 363
0049af73
TH
364 if (parent_sq->first_pending)
365 return rb_entry_tg(parent_sq->first_pending);
e43473b7
VG
366
367 return NULL;
368}
369
370static void rb_erase_init(struct rb_node *n, struct rb_root *root)
371{
372 rb_erase(n, root);
373 RB_CLEAR_NODE(n);
374}
375
0049af73
TH
376static void throtl_rb_erase(struct rb_node *n,
377 struct throtl_service_queue *parent_sq)
e43473b7 378{
0049af73
TH
379 if (parent_sq->first_pending == n)
380 parent_sq->first_pending = NULL;
381 rb_erase_init(n, &parent_sq->pending_tree);
382 --parent_sq->nr_pending;
e43473b7
VG
383}
384
0049af73 385static void update_min_dispatch_time(struct throtl_service_queue *parent_sq)
e43473b7
VG
386{
387 struct throtl_grp *tg;
388
0049af73 389 tg = throtl_rb_first(parent_sq);
e43473b7
VG
390 if (!tg)
391 return;
392
0049af73 393 parent_sq->first_pending_disptime = tg->disptime;
e43473b7
VG
394}
395
77216b04 396static void tg_service_queue_add(struct throtl_grp *tg)
e43473b7 397{
77216b04 398 struct throtl_service_queue *parent_sq = tg->service_queue.parent_sq;
0049af73 399 struct rb_node **node = &parent_sq->pending_tree.rb_node;
e43473b7
VG
400 struct rb_node *parent = NULL;
401 struct throtl_grp *__tg;
402 unsigned long key = tg->disptime;
403 int left = 1;
404
405 while (*node != NULL) {
406 parent = *node;
407 __tg = rb_entry_tg(parent);
408
409 if (time_before(key, __tg->disptime))
410 node = &parent->rb_left;
411 else {
412 node = &parent->rb_right;
413 left = 0;
414 }
415 }
416
417 if (left)
0049af73 418 parent_sq->first_pending = &tg->rb_node;
e43473b7
VG
419
420 rb_link_node(&tg->rb_node, parent, node);
0049af73 421 rb_insert_color(&tg->rb_node, &parent_sq->pending_tree);
e43473b7
VG
422}
423
77216b04 424static void __throtl_enqueue_tg(struct throtl_grp *tg)
e43473b7 425{
77216b04 426 tg_service_queue_add(tg);
5b2c16aa 427 tg->flags |= THROTL_TG_PENDING;
77216b04 428 tg->service_queue.parent_sq->nr_pending++;
e43473b7
VG
429}
430
77216b04 431static void throtl_enqueue_tg(struct throtl_grp *tg)
e43473b7 432{
5b2c16aa 433 if (!(tg->flags & THROTL_TG_PENDING))
77216b04 434 __throtl_enqueue_tg(tg);
e43473b7
VG
435}
436
77216b04 437static void __throtl_dequeue_tg(struct throtl_grp *tg)
e43473b7 438{
77216b04 439 throtl_rb_erase(&tg->rb_node, tg->service_queue.parent_sq);
5b2c16aa 440 tg->flags &= ~THROTL_TG_PENDING;
e43473b7
VG
441}
442
77216b04 443static void throtl_dequeue_tg(struct throtl_grp *tg)
e43473b7 444{
5b2c16aa 445 if (tg->flags & THROTL_TG_PENDING)
77216b04 446 __throtl_dequeue_tg(tg);
e43473b7
VG
447}
448
a9131a27
TH
449/* Call with queue lock held */
450static void throtl_schedule_delayed_work(struct throtl_data *td,
451 unsigned long delay)
452{
453 struct delayed_work *dwork = &td->dispatch_work;
fda6f272 454 struct throtl_service_queue *sq = &td->service_queue;
a9131a27 455
6a525600 456 mod_delayed_work(kthrotld_workqueue, dwork, delay);
fda6f272 457 throtl_log(sq, "schedule work. delay=%lu jiffies=%lu", delay, jiffies);
a9131a27
TH
458}
459
e43473b7
VG
460static void throtl_schedule_next_dispatch(struct throtl_data *td)
461{
c9e0332e 462 struct throtl_service_queue *sq = &td->service_queue;
e43473b7 463
6a525600 464 /* any pending children left? */
c9e0332e 465 if (!sq->nr_pending)
e43473b7
VG
466 return;
467
c9e0332e 468 update_min_dispatch_time(sq);
e43473b7 469
c9e0332e 470 if (time_before_eq(sq->first_pending_disptime, jiffies))
450adcbe 471 throtl_schedule_delayed_work(td, 0);
e43473b7 472 else
c9e0332e 473 throtl_schedule_delayed_work(td, sq->first_pending_disptime - jiffies);
e43473b7
VG
474}
475
0f3457f6 476static inline void throtl_start_new_slice(struct throtl_grp *tg, bool rw)
e43473b7
VG
477{
478 tg->bytes_disp[rw] = 0;
8e89d13f 479 tg->io_disp[rw] = 0;
e43473b7
VG
480 tg->slice_start[rw] = jiffies;
481 tg->slice_end[rw] = jiffies + throtl_slice;
fda6f272
TH
482 throtl_log(&tg->service_queue,
483 "[%c] new slice start=%lu end=%lu jiffies=%lu",
484 rw == READ ? 'R' : 'W', tg->slice_start[rw],
485 tg->slice_end[rw], jiffies);
e43473b7
VG
486}
487
0f3457f6
TH
488static inline void throtl_set_slice_end(struct throtl_grp *tg, bool rw,
489 unsigned long jiffy_end)
d1ae8ffd
VG
490{
491 tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
492}
493
0f3457f6
TH
494static inline void throtl_extend_slice(struct throtl_grp *tg, bool rw,
495 unsigned long jiffy_end)
e43473b7
VG
496{
497 tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
fda6f272
TH
498 throtl_log(&tg->service_queue,
499 "[%c] extend slice start=%lu end=%lu jiffies=%lu",
500 rw == READ ? 'R' : 'W', tg->slice_start[rw],
501 tg->slice_end[rw], jiffies);
e43473b7
VG
502}
503
504/* Determine if previously allocated or extended slice is complete or not */
0f3457f6 505static bool throtl_slice_used(struct throtl_grp *tg, bool rw)
e43473b7
VG
506{
507 if (time_in_range(jiffies, tg->slice_start[rw], tg->slice_end[rw]))
508 return 0;
509
510 return 1;
511}
512
513/* Trim the used slices and adjust slice start accordingly */
0f3457f6 514static inline void throtl_trim_slice(struct throtl_grp *tg, bool rw)
e43473b7 515{
3aad5d3e
VG
516 unsigned long nr_slices, time_elapsed, io_trim;
517 u64 bytes_trim, tmp;
e43473b7
VG
518
519 BUG_ON(time_before(tg->slice_end[rw], tg->slice_start[rw]));
520
521 /*
522 * If bps are unlimited (-1), then time slice don't get
523 * renewed. Don't try to trim the slice if slice is used. A new
524 * slice will start when appropriate.
525 */
0f3457f6 526 if (throtl_slice_used(tg, rw))
e43473b7
VG
527 return;
528
d1ae8ffd
VG
529 /*
530 * A bio has been dispatched. Also adjust slice_end. It might happen
531 * that initially cgroup limit was very low resulting in high
532 * slice_end, but later limit was bumped up and bio was dispached
533 * sooner, then we need to reduce slice_end. A high bogus slice_end
534 * is bad because it does not allow new slice to start.
535 */
536
0f3457f6 537 throtl_set_slice_end(tg, rw, jiffies + throtl_slice);
d1ae8ffd 538
e43473b7
VG
539 time_elapsed = jiffies - tg->slice_start[rw];
540
541 nr_slices = time_elapsed / throtl_slice;
542
543 if (!nr_slices)
544 return;
3aad5d3e
VG
545 tmp = tg->bps[rw] * throtl_slice * nr_slices;
546 do_div(tmp, HZ);
547 bytes_trim = tmp;
e43473b7 548
8e89d13f 549 io_trim = (tg->iops[rw] * throtl_slice * nr_slices)/HZ;
e43473b7 550
8e89d13f 551 if (!bytes_trim && !io_trim)
e43473b7
VG
552 return;
553
554 if (tg->bytes_disp[rw] >= bytes_trim)
555 tg->bytes_disp[rw] -= bytes_trim;
556 else
557 tg->bytes_disp[rw] = 0;
558
8e89d13f
VG
559 if (tg->io_disp[rw] >= io_trim)
560 tg->io_disp[rw] -= io_trim;
561 else
562 tg->io_disp[rw] = 0;
563
e43473b7
VG
564 tg->slice_start[rw] += nr_slices * throtl_slice;
565
fda6f272
TH
566 throtl_log(&tg->service_queue,
567 "[%c] trim slice nr=%lu bytes=%llu io=%lu start=%lu end=%lu jiffies=%lu",
568 rw == READ ? 'R' : 'W', nr_slices, bytes_trim, io_trim,
569 tg->slice_start[rw], tg->slice_end[rw], jiffies);
e43473b7
VG
570}
571
0f3457f6
TH
572static bool tg_with_in_iops_limit(struct throtl_grp *tg, struct bio *bio,
573 unsigned long *wait)
e43473b7
VG
574{
575 bool rw = bio_data_dir(bio);
8e89d13f 576 unsigned int io_allowed;
e43473b7 577 unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
c49c06e4 578 u64 tmp;
e43473b7 579
8e89d13f 580 jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
e43473b7 581
8e89d13f
VG
582 /* Slice has just started. Consider one slice interval */
583 if (!jiffy_elapsed)
584 jiffy_elapsed_rnd = throtl_slice;
585
586 jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
587
c49c06e4
VG
588 /*
589 * jiffy_elapsed_rnd should not be a big value as minimum iops can be
590 * 1 then at max jiffy elapsed should be equivalent of 1 second as we
591 * will allow dispatch after 1 second and after that slice should
592 * have been trimmed.
593 */
594
595 tmp = (u64)tg->iops[rw] * jiffy_elapsed_rnd;
596 do_div(tmp, HZ);
597
598 if (tmp > UINT_MAX)
599 io_allowed = UINT_MAX;
600 else
601 io_allowed = tmp;
8e89d13f
VG
602
603 if (tg->io_disp[rw] + 1 <= io_allowed) {
e43473b7
VG
604 if (wait)
605 *wait = 0;
606 return 1;
607 }
608
8e89d13f
VG
609 /* Calc approx time to dispatch */
610 jiffy_wait = ((tg->io_disp[rw] + 1) * HZ)/tg->iops[rw] + 1;
611
612 if (jiffy_wait > jiffy_elapsed)
613 jiffy_wait = jiffy_wait - jiffy_elapsed;
614 else
615 jiffy_wait = 1;
616
617 if (wait)
618 *wait = jiffy_wait;
619 return 0;
620}
621
0f3457f6
TH
622static bool tg_with_in_bps_limit(struct throtl_grp *tg, struct bio *bio,
623 unsigned long *wait)
8e89d13f
VG
624{
625 bool rw = bio_data_dir(bio);
3aad5d3e 626 u64 bytes_allowed, extra_bytes, tmp;
8e89d13f 627 unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
e43473b7
VG
628
629 jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
630
631 /* Slice has just started. Consider one slice interval */
632 if (!jiffy_elapsed)
633 jiffy_elapsed_rnd = throtl_slice;
634
635 jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
636
5e901a2b
VG
637 tmp = tg->bps[rw] * jiffy_elapsed_rnd;
638 do_div(tmp, HZ);
3aad5d3e 639 bytes_allowed = tmp;
e43473b7
VG
640
641 if (tg->bytes_disp[rw] + bio->bi_size <= bytes_allowed) {
642 if (wait)
643 *wait = 0;
644 return 1;
645 }
646
647 /* Calc approx time to dispatch */
648 extra_bytes = tg->bytes_disp[rw] + bio->bi_size - bytes_allowed;
649 jiffy_wait = div64_u64(extra_bytes * HZ, tg->bps[rw]);
650
651 if (!jiffy_wait)
652 jiffy_wait = 1;
653
654 /*
655 * This wait time is without taking into consideration the rounding
656 * up we did. Add that time also.
657 */
658 jiffy_wait = jiffy_wait + (jiffy_elapsed_rnd - jiffy_elapsed);
e43473b7
VG
659 if (wait)
660 *wait = jiffy_wait;
8e89d13f
VG
661 return 0;
662}
663
af75cd3c
VG
664static bool tg_no_rule_group(struct throtl_grp *tg, bool rw) {
665 if (tg->bps[rw] == -1 && tg->iops[rw] == -1)
666 return 1;
667 return 0;
668}
669
8e89d13f
VG
670/*
671 * Returns whether one can dispatch a bio or not. Also returns approx number
672 * of jiffies to wait before this bio is with-in IO rate and can be dispatched
673 */
0f3457f6
TH
674static bool tg_may_dispatch(struct throtl_grp *tg, struct bio *bio,
675 unsigned long *wait)
8e89d13f
VG
676{
677 bool rw = bio_data_dir(bio);
678 unsigned long bps_wait = 0, iops_wait = 0, max_wait = 0;
679
680 /*
681 * Currently whole state machine of group depends on first bio
682 * queued in the group bio list. So one should not be calling
683 * this function with a different bio if there are other bios
684 * queued.
685 */
73f0d49a
TH
686 BUG_ON(tg->service_queue.nr_queued[rw] &&
687 bio != bio_list_peek(&tg->service_queue.bio_lists[rw]));
e43473b7 688
8e89d13f
VG
689 /* If tg->bps = -1, then BW is unlimited */
690 if (tg->bps[rw] == -1 && tg->iops[rw] == -1) {
691 if (wait)
692 *wait = 0;
693 return 1;
694 }
695
696 /*
697 * If previous slice expired, start a new one otherwise renew/extend
698 * existing slice to make sure it is at least throtl_slice interval
699 * long since now.
700 */
0f3457f6
TH
701 if (throtl_slice_used(tg, rw))
702 throtl_start_new_slice(tg, rw);
8e89d13f
VG
703 else {
704 if (time_before(tg->slice_end[rw], jiffies + throtl_slice))
0f3457f6 705 throtl_extend_slice(tg, rw, jiffies + throtl_slice);
8e89d13f
VG
706 }
707
0f3457f6
TH
708 if (tg_with_in_bps_limit(tg, bio, &bps_wait) &&
709 tg_with_in_iops_limit(tg, bio, &iops_wait)) {
8e89d13f
VG
710 if (wait)
711 *wait = 0;
712 return 1;
713 }
714
715 max_wait = max(bps_wait, iops_wait);
716
717 if (wait)
718 *wait = max_wait;
719
720 if (time_before(tg->slice_end[rw], jiffies + max_wait))
0f3457f6 721 throtl_extend_slice(tg, rw, jiffies + max_wait);
e43473b7
VG
722
723 return 0;
724}
725
3c798398 726static void throtl_update_dispatch_stats(struct blkcg_gq *blkg, u64 bytes,
629ed0b1
TH
727 int rw)
728{
8a3d2615
TH
729 struct throtl_grp *tg = blkg_to_tg(blkg);
730 struct tg_stats_cpu *stats_cpu;
629ed0b1
TH
731 unsigned long flags;
732
733 /* If per cpu stats are not allocated yet, don't do any accounting. */
8a3d2615 734 if (tg->stats_cpu == NULL)
629ed0b1
TH
735 return;
736
737 /*
738 * Disabling interrupts to provide mutual exclusion between two
739 * writes on same cpu. It probably is not needed for 64bit. Not
740 * optimizing that case yet.
741 */
742 local_irq_save(flags);
743
8a3d2615 744 stats_cpu = this_cpu_ptr(tg->stats_cpu);
629ed0b1 745
629ed0b1
TH
746 blkg_rwstat_add(&stats_cpu->serviced, rw, 1);
747 blkg_rwstat_add(&stats_cpu->service_bytes, rw, bytes);
748
749 local_irq_restore(flags);
750}
751
e43473b7
VG
752static void throtl_charge_bio(struct throtl_grp *tg, struct bio *bio)
753{
754 bool rw = bio_data_dir(bio);
e43473b7
VG
755
756 /* Charge the bio to the group */
757 tg->bytes_disp[rw] += bio->bi_size;
8e89d13f 758 tg->io_disp[rw]++;
e43473b7 759
629ed0b1 760 throtl_update_dispatch_stats(tg_to_blkg(tg), bio->bi_size, bio->bi_rw);
e43473b7
VG
761}
762
77216b04 763static void throtl_add_bio_tg(struct bio *bio, struct throtl_grp *tg)
e43473b7 764{
73f0d49a 765 struct throtl_service_queue *sq = &tg->service_queue;
e43473b7
VG
766 bool rw = bio_data_dir(bio);
767
0e9f4164
TH
768 /*
769 * If @tg doesn't currently have any bios queued in the same
770 * direction, queueing @bio can change when @tg should be
771 * dispatched. Mark that @tg was empty. This is automatically
772 * cleaered on the next tg_update_disptime().
773 */
774 if (!sq->nr_queued[rw])
775 tg->flags |= THROTL_TG_WAS_EMPTY;
776
73f0d49a 777 bio_list_add(&sq->bio_lists[rw], bio);
e43473b7 778 /* Take a bio reference on tg */
1adaf3dd 779 blkg_get(tg_to_blkg(tg));
73f0d49a 780 sq->nr_queued[rw]++;
e2d57e60 781 tg->td->nr_queued[rw]++;
77216b04 782 throtl_enqueue_tg(tg);
e43473b7
VG
783}
784
77216b04 785static void tg_update_disptime(struct throtl_grp *tg)
e43473b7 786{
73f0d49a 787 struct throtl_service_queue *sq = &tg->service_queue;
e43473b7
VG
788 unsigned long read_wait = -1, write_wait = -1, min_wait = -1, disptime;
789 struct bio *bio;
790
73f0d49a 791 if ((bio = bio_list_peek(&sq->bio_lists[READ])))
0f3457f6 792 tg_may_dispatch(tg, bio, &read_wait);
e43473b7 793
73f0d49a 794 if ((bio = bio_list_peek(&sq->bio_lists[WRITE])))
0f3457f6 795 tg_may_dispatch(tg, bio, &write_wait);
e43473b7
VG
796
797 min_wait = min(read_wait, write_wait);
798 disptime = jiffies + min_wait;
799
e43473b7 800 /* Update dispatch time */
77216b04 801 throtl_dequeue_tg(tg);
e43473b7 802 tg->disptime = disptime;
77216b04 803 throtl_enqueue_tg(tg);
0e9f4164
TH
804
805 /* see throtl_add_bio_tg() */
806 tg->flags &= ~THROTL_TG_WAS_EMPTY;
e43473b7
VG
807}
808
77216b04 809static void tg_dispatch_one_bio(struct throtl_grp *tg, bool rw)
e43473b7 810{
73f0d49a 811 struct throtl_service_queue *sq = &tg->service_queue;
e43473b7
VG
812 struct bio *bio;
813
73f0d49a
TH
814 bio = bio_list_pop(&sq->bio_lists[rw]);
815 sq->nr_queued[rw]--;
1adaf3dd
TH
816 /* Drop bio reference on blkg */
817 blkg_put(tg_to_blkg(tg));
e43473b7 818
0f3457f6
TH
819 BUG_ON(tg->td->nr_queued[rw] <= 0);
820 tg->td->nr_queued[rw]--;
e43473b7
VG
821
822 throtl_charge_bio(tg, bio);
77216b04 823 bio_list_add(&sq->parent_sq->bio_lists[rw], bio);
e43473b7
VG
824 bio->bi_rw |= REQ_THROTTLED;
825
0f3457f6 826 throtl_trim_slice(tg, rw);
e43473b7
VG
827}
828
77216b04 829static int throtl_dispatch_tg(struct throtl_grp *tg)
e43473b7 830{
73f0d49a 831 struct throtl_service_queue *sq = &tg->service_queue;
e43473b7
VG
832 unsigned int nr_reads = 0, nr_writes = 0;
833 unsigned int max_nr_reads = throtl_grp_quantum*3/4;
c2f6805d 834 unsigned int max_nr_writes = throtl_grp_quantum - max_nr_reads;
e43473b7
VG
835 struct bio *bio;
836
837 /* Try to dispatch 75% READS and 25% WRITES */
838
73f0d49a 839 while ((bio = bio_list_peek(&sq->bio_lists[READ])) &&
0f3457f6 840 tg_may_dispatch(tg, bio, NULL)) {
e43473b7 841
77216b04 842 tg_dispatch_one_bio(tg, bio_data_dir(bio));
e43473b7
VG
843 nr_reads++;
844
845 if (nr_reads >= max_nr_reads)
846 break;
847 }
848
73f0d49a 849 while ((bio = bio_list_peek(&sq->bio_lists[WRITE])) &&
0f3457f6 850 tg_may_dispatch(tg, bio, NULL)) {
e43473b7 851
77216b04 852 tg_dispatch_one_bio(tg, bio_data_dir(bio));
e43473b7
VG
853 nr_writes++;
854
855 if (nr_writes >= max_nr_writes)
856 break;
857 }
858
859 return nr_reads + nr_writes;
860}
861
651930bc 862static int throtl_select_dispatch(struct throtl_service_queue *parent_sq)
e43473b7
VG
863{
864 unsigned int nr_disp = 0;
e43473b7
VG
865
866 while (1) {
73f0d49a
TH
867 struct throtl_grp *tg = throtl_rb_first(parent_sq);
868 struct throtl_service_queue *sq = &tg->service_queue;
e43473b7
VG
869
870 if (!tg)
871 break;
872
873 if (time_before(jiffies, tg->disptime))
874 break;
875
77216b04 876 throtl_dequeue_tg(tg);
e43473b7 877
77216b04 878 nr_disp += throtl_dispatch_tg(tg);
e43473b7 879
73f0d49a 880 if (sq->nr_queued[0] || sq->nr_queued[1])
77216b04 881 tg_update_disptime(tg);
e43473b7
VG
882
883 if (nr_disp >= throtl_quantum)
884 break;
885 }
886
887 return nr_disp;
888}
889
cb76199c
TH
890/* work function to dispatch throttled bios */
891void blk_throtl_dispatch_work_fn(struct work_struct *work)
e43473b7 892{
cb76199c
TH
893 struct throtl_data *td = container_of(to_delayed_work(work),
894 struct throtl_data, dispatch_work);
651930bc 895 struct throtl_service_queue *sq = &td->service_queue;
cb76199c 896 struct request_queue *q = td->queue;
e43473b7
VG
897 unsigned int nr_disp = 0;
898 struct bio_list bio_list_on_stack;
899 struct bio *bio;
69d60eb9 900 struct blk_plug plug;
651930bc 901 int rw;
e43473b7
VG
902
903 spin_lock_irq(q->queue_lock);
904
e43473b7
VG
905 bio_list_init(&bio_list_on_stack);
906
fda6f272 907 throtl_log(sq, "dispatch nr_queued=%u read=%u write=%u",
6a525600
TH
908 td->nr_queued[READ] + td->nr_queued[WRITE],
909 td->nr_queued[READ], td->nr_queued[WRITE]);
e43473b7 910
651930bc 911 nr_disp = throtl_select_dispatch(sq);
e43473b7 912
651930bc
TH
913 if (nr_disp) {
914 for (rw = READ; rw <= WRITE; rw++) {
915 bio_list_merge(&bio_list_on_stack, &sq->bio_lists[rw]);
916 bio_list_init(&sq->bio_lists[rw]);
917 }
fda6f272 918 throtl_log(sq, "bios disp=%u", nr_disp);
651930bc 919 }
e43473b7
VG
920
921 throtl_schedule_next_dispatch(td);
6a525600 922
e43473b7
VG
923 spin_unlock_irq(q->queue_lock);
924
925 /*
926 * If we dispatched some requests, unplug the queue to make sure
927 * immediate dispatch
928 */
929 if (nr_disp) {
69d60eb9 930 blk_start_plug(&plug);
e43473b7
VG
931 while((bio = bio_list_pop(&bio_list_on_stack)))
932 generic_make_request(bio);
69d60eb9 933 blk_finish_plug(&plug);
e43473b7 934 }
e43473b7
VG
935}
936
f95a04af
TH
937static u64 tg_prfill_cpu_rwstat(struct seq_file *sf,
938 struct blkg_policy_data *pd, int off)
41b38b6d 939{
f95a04af 940 struct throtl_grp *tg = pd_to_tg(pd);
41b38b6d
TH
941 struct blkg_rwstat rwstat = { }, tmp;
942 int i, cpu;
943
944 for_each_possible_cpu(cpu) {
8a3d2615 945 struct tg_stats_cpu *sc = per_cpu_ptr(tg->stats_cpu, cpu);
41b38b6d
TH
946
947 tmp = blkg_rwstat_read((void *)sc + off);
948 for (i = 0; i < BLKG_RWSTAT_NR; i++)
949 rwstat.cnt[i] += tmp.cnt[i];
950 }
951
f95a04af 952 return __blkg_prfill_rwstat(sf, pd, &rwstat);
41b38b6d
TH
953}
954
8a3d2615
TH
955static int tg_print_cpu_rwstat(struct cgroup *cgrp, struct cftype *cft,
956 struct seq_file *sf)
41b38b6d 957{
3c798398 958 struct blkcg *blkcg = cgroup_to_blkcg(cgrp);
41b38b6d 959
3c798398 960 blkcg_print_blkgs(sf, blkcg, tg_prfill_cpu_rwstat, &blkcg_policy_throtl,
5bc4afb1 961 cft->private, true);
41b38b6d
TH
962 return 0;
963}
964
f95a04af
TH
965static u64 tg_prfill_conf_u64(struct seq_file *sf, struct blkg_policy_data *pd,
966 int off)
60c2bc2d 967{
f95a04af
TH
968 struct throtl_grp *tg = pd_to_tg(pd);
969 u64 v = *(u64 *)((void *)tg + off);
60c2bc2d 970
af133ceb 971 if (v == -1)
60c2bc2d 972 return 0;
f95a04af 973 return __blkg_prfill_u64(sf, pd, v);
60c2bc2d
TH
974}
975
f95a04af
TH
976static u64 tg_prfill_conf_uint(struct seq_file *sf, struct blkg_policy_data *pd,
977 int off)
e43473b7 978{
f95a04af
TH
979 struct throtl_grp *tg = pd_to_tg(pd);
980 unsigned int v = *(unsigned int *)((void *)tg + off);
fe071437 981
af133ceb
TH
982 if (v == -1)
983 return 0;
f95a04af 984 return __blkg_prfill_u64(sf, pd, v);
e43473b7
VG
985}
986
af133ceb
TH
987static int tg_print_conf_u64(struct cgroup *cgrp, struct cftype *cft,
988 struct seq_file *sf)
8e89d13f 989{
3c798398
TH
990 blkcg_print_blkgs(sf, cgroup_to_blkcg(cgrp), tg_prfill_conf_u64,
991 &blkcg_policy_throtl, cft->private, false);
af133ceb 992 return 0;
8e89d13f
VG
993}
994
af133ceb
TH
995static int tg_print_conf_uint(struct cgroup *cgrp, struct cftype *cft,
996 struct seq_file *sf)
8e89d13f 997{
3c798398
TH
998 blkcg_print_blkgs(sf, cgroup_to_blkcg(cgrp), tg_prfill_conf_uint,
999 &blkcg_policy_throtl, cft->private, false);
af133ceb 1000 return 0;
60c2bc2d
TH
1001}
1002
af133ceb
TH
1003static int tg_set_conf(struct cgroup *cgrp, struct cftype *cft, const char *buf,
1004 bool is_u64)
60c2bc2d 1005{
3c798398 1006 struct blkcg *blkcg = cgroup_to_blkcg(cgrp);
60c2bc2d 1007 struct blkg_conf_ctx ctx;
af133ceb 1008 struct throtl_grp *tg;
a2b1693b 1009 struct throtl_data *td;
60c2bc2d
TH
1010 int ret;
1011
3c798398 1012 ret = blkg_conf_prep(blkcg, &blkcg_policy_throtl, buf, &ctx);
60c2bc2d
TH
1013 if (ret)
1014 return ret;
1015
af133ceb 1016 tg = blkg_to_tg(ctx.blkg);
a2b1693b 1017 td = ctx.blkg->q->td;
af133ceb 1018
a2b1693b
TH
1019 if (!ctx.v)
1020 ctx.v = -1;
af133ceb 1021
a2b1693b
TH
1022 if (is_u64)
1023 *(u64 *)((void *)tg + cft->private) = ctx.v;
1024 else
1025 *(unsigned int *)((void *)tg + cft->private) = ctx.v;
af133ceb 1026
fda6f272
TH
1027 throtl_log(&tg->service_queue,
1028 "limit change rbps=%llu wbps=%llu riops=%u wiops=%u",
1029 tg->bps[READ], tg->bps[WRITE],
1030 tg->iops[READ], tg->iops[WRITE]);
632b4493
TH
1031
1032 /*
1033 * We're already holding queue_lock and know @tg is valid. Let's
1034 * apply the new config directly.
1035 *
1036 * Restart the slices for both READ and WRITES. It might happen
1037 * that a group's limit are dropped suddenly and we don't want to
1038 * account recently dispatched IO with new low rate.
1039 */
0f3457f6
TH
1040 throtl_start_new_slice(tg, 0);
1041 throtl_start_new_slice(tg, 1);
632b4493 1042
5b2c16aa 1043 if (tg->flags & THROTL_TG_PENDING) {
77216b04 1044 tg_update_disptime(tg);
632b4493
TH
1045 throtl_schedule_next_dispatch(td);
1046 }
60c2bc2d
TH
1047
1048 blkg_conf_finish(&ctx);
a2b1693b 1049 return 0;
8e89d13f
VG
1050}
1051
af133ceb
TH
1052static int tg_set_conf_u64(struct cgroup *cgrp, struct cftype *cft,
1053 const char *buf)
60c2bc2d 1054{
af133ceb 1055 return tg_set_conf(cgrp, cft, buf, true);
60c2bc2d
TH
1056}
1057
af133ceb
TH
1058static int tg_set_conf_uint(struct cgroup *cgrp, struct cftype *cft,
1059 const char *buf)
60c2bc2d 1060{
af133ceb 1061 return tg_set_conf(cgrp, cft, buf, false);
60c2bc2d
TH
1062}
1063
1064static struct cftype throtl_files[] = {
1065 {
1066 .name = "throttle.read_bps_device",
af133ceb
TH
1067 .private = offsetof(struct throtl_grp, bps[READ]),
1068 .read_seq_string = tg_print_conf_u64,
1069 .write_string = tg_set_conf_u64,
60c2bc2d
TH
1070 .max_write_len = 256,
1071 },
1072 {
1073 .name = "throttle.write_bps_device",
af133ceb
TH
1074 .private = offsetof(struct throtl_grp, bps[WRITE]),
1075 .read_seq_string = tg_print_conf_u64,
1076 .write_string = tg_set_conf_u64,
60c2bc2d
TH
1077 .max_write_len = 256,
1078 },
1079 {
1080 .name = "throttle.read_iops_device",
af133ceb
TH
1081 .private = offsetof(struct throtl_grp, iops[READ]),
1082 .read_seq_string = tg_print_conf_uint,
1083 .write_string = tg_set_conf_uint,
60c2bc2d
TH
1084 .max_write_len = 256,
1085 },
1086 {
1087 .name = "throttle.write_iops_device",
af133ceb
TH
1088 .private = offsetof(struct throtl_grp, iops[WRITE]),
1089 .read_seq_string = tg_print_conf_uint,
1090 .write_string = tg_set_conf_uint,
60c2bc2d
TH
1091 .max_write_len = 256,
1092 },
1093 {
1094 .name = "throttle.io_service_bytes",
5bc4afb1 1095 .private = offsetof(struct tg_stats_cpu, service_bytes),
8a3d2615 1096 .read_seq_string = tg_print_cpu_rwstat,
60c2bc2d
TH
1097 },
1098 {
1099 .name = "throttle.io_serviced",
5bc4afb1 1100 .private = offsetof(struct tg_stats_cpu, serviced),
8a3d2615 1101 .read_seq_string = tg_print_cpu_rwstat,
60c2bc2d
TH
1102 },
1103 { } /* terminate */
1104};
1105
da527770 1106static void throtl_shutdown_wq(struct request_queue *q)
e43473b7
VG
1107{
1108 struct throtl_data *td = q->td;
1109
cb76199c 1110 cancel_delayed_work_sync(&td->dispatch_work);
e43473b7
VG
1111}
1112
3c798398 1113static struct blkcg_policy blkcg_policy_throtl = {
f9fcc2d3
TH
1114 .pd_size = sizeof(struct throtl_grp),
1115 .cftypes = throtl_files,
1116
1117 .pd_init_fn = throtl_pd_init,
1118 .pd_exit_fn = throtl_pd_exit,
1119 .pd_reset_stats_fn = throtl_pd_reset_stats,
e43473b7
VG
1120};
1121
bc16a4f9 1122bool blk_throtl_bio(struct request_queue *q, struct bio *bio)
e43473b7
VG
1123{
1124 struct throtl_data *td = q->td;
1125 struct throtl_grp *tg;
73f0d49a 1126 struct throtl_service_queue *sq;
0e9f4164 1127 bool rw = bio_data_dir(bio);
3c798398 1128 struct blkcg *blkcg;
bc16a4f9 1129 bool throttled = false;
e43473b7
VG
1130
1131 if (bio->bi_rw & REQ_THROTTLED) {
1132 bio->bi_rw &= ~REQ_THROTTLED;
bc16a4f9 1133 goto out;
e43473b7
VG
1134 }
1135
af75cd3c
VG
1136 /*
1137 * A throtl_grp pointer retrieved under rcu can be used to access
1138 * basic fields like stats and io rates. If a group has no rules,
1139 * just update the dispatch stats in lockless manner and return.
1140 */
af75cd3c 1141 rcu_read_lock();
3c798398 1142 blkcg = bio_blkcg(bio);
cd1604fa 1143 tg = throtl_lookup_tg(td, blkcg);
af75cd3c 1144 if (tg) {
af75cd3c 1145 if (tg_no_rule_group(tg, rw)) {
629ed0b1
TH
1146 throtl_update_dispatch_stats(tg_to_blkg(tg),
1147 bio->bi_size, bio->bi_rw);
2a7f1244 1148 goto out_unlock_rcu;
af75cd3c
VG
1149 }
1150 }
af75cd3c
VG
1151
1152 /*
1153 * Either group has not been allocated yet or it is not an unlimited
1154 * IO group
1155 */
e43473b7 1156 spin_lock_irq(q->queue_lock);
cd1604fa 1157 tg = throtl_lookup_create_tg(td, blkcg);
bc16a4f9
TH
1158 if (unlikely(!tg))
1159 goto out_unlock;
f469a7b4 1160
73f0d49a
TH
1161 sq = &tg->service_queue;
1162
0e9f4164
TH
1163 /* throtl is FIFO - if other bios are already queued, should queue */
1164 if (sq->nr_queued[rw])
e43473b7 1165 goto queue_bio;
de701c74 1166
e43473b7 1167 /* Bio is with-in rate limit of group */
0f3457f6 1168 if (tg_may_dispatch(tg, bio, NULL)) {
e43473b7 1169 throtl_charge_bio(tg, bio);
04521db0
VG
1170
1171 /*
1172 * We need to trim slice even when bios are not being queued
1173 * otherwise it might happen that a bio is not queued for
1174 * a long time and slice keeps on extending and trim is not
1175 * called for a long time. Now if limits are reduced suddenly
1176 * we take into account all the IO dispatched so far at new
1177 * low rate and * newly queued IO gets a really long dispatch
1178 * time.
1179 *
1180 * So keep on trimming slice even if bio is not queued.
1181 */
0f3457f6 1182 throtl_trim_slice(tg, rw);
bc16a4f9 1183 goto out_unlock;
e43473b7
VG
1184 }
1185
1186queue_bio:
fda6f272
TH
1187 throtl_log(sq, "[%c] bio. bdisp=%llu sz=%u bps=%llu iodisp=%u iops=%u queued=%d/%d",
1188 rw == READ ? 'R' : 'W',
1189 tg->bytes_disp[rw], bio->bi_size, tg->bps[rw],
1190 tg->io_disp[rw], tg->iops[rw],
1191 sq->nr_queued[READ], sq->nr_queued[WRITE]);
e43473b7 1192
671058fb 1193 bio_associate_current(bio);
77216b04 1194 throtl_add_bio_tg(bio, tg);
bc16a4f9 1195 throttled = true;
e43473b7 1196
0e9f4164
TH
1197 /* update @tg's dispatch time if @tg was empty before @bio */
1198 if (tg->flags & THROTL_TG_WAS_EMPTY) {
77216b04 1199 tg_update_disptime(tg);
e43473b7
VG
1200 throtl_schedule_next_dispatch(td);
1201 }
1202
bc16a4f9 1203out_unlock:
e43473b7 1204 spin_unlock_irq(q->queue_lock);
2a7f1244
TH
1205out_unlock_rcu:
1206 rcu_read_unlock();
bc16a4f9
TH
1207out:
1208 return throttled;
e43473b7
VG
1209}
1210
c9a929dd
TH
1211/**
1212 * blk_throtl_drain - drain throttled bios
1213 * @q: request_queue to drain throttled bios for
1214 *
1215 * Dispatch all currently throttled bios on @q through ->make_request_fn().
1216 */
1217void blk_throtl_drain(struct request_queue *q)
1218 __releases(q->queue_lock) __acquires(q->queue_lock)
1219{
1220 struct throtl_data *td = q->td;
0049af73 1221 struct throtl_service_queue *parent_sq = &td->service_queue;
c9a929dd 1222 struct throtl_grp *tg;
c9a929dd 1223 struct bio *bio;
651930bc 1224 int rw;
c9a929dd 1225
8bcb6c7d 1226 queue_lockdep_assert_held(q);
c9a929dd 1227
0049af73 1228 while ((tg = throtl_rb_first(parent_sq))) {
73f0d49a
TH
1229 struct throtl_service_queue *sq = &tg->service_queue;
1230
77216b04 1231 throtl_dequeue_tg(tg);
c9a929dd 1232
73f0d49a 1233 while ((bio = bio_list_peek(&sq->bio_lists[READ])))
77216b04 1234 tg_dispatch_one_bio(tg, bio_data_dir(bio));
73f0d49a 1235 while ((bio = bio_list_peek(&sq->bio_lists[WRITE])))
77216b04 1236 tg_dispatch_one_bio(tg, bio_data_dir(bio));
c9a929dd
TH
1237 }
1238 spin_unlock_irq(q->queue_lock);
1239
651930bc
TH
1240 for (rw = READ; rw <= WRITE; rw++)
1241 while ((bio = bio_list_pop(&parent_sq->bio_lists[rw])))
1242 generic_make_request(bio);
c9a929dd
TH
1243
1244 spin_lock_irq(q->queue_lock);
1245}
1246
e43473b7
VG
1247int blk_throtl_init(struct request_queue *q)
1248{
1249 struct throtl_data *td;
a2b1693b 1250 int ret;
e43473b7
VG
1251
1252 td = kzalloc_node(sizeof(*td), GFP_KERNEL, q->node);
1253 if (!td)
1254 return -ENOMEM;
1255
cb76199c 1256 INIT_DELAYED_WORK(&td->dispatch_work, blk_throtl_dispatch_work_fn);
77216b04 1257 throtl_service_queue_init(&td->service_queue, NULL);
e43473b7 1258
cd1604fa 1259 q->td = td;
29b12589 1260 td->queue = q;
02977e4a 1261
a2b1693b 1262 /* activate policy */
3c798398 1263 ret = blkcg_activate_policy(q, &blkcg_policy_throtl);
a2b1693b 1264 if (ret)
f51b802c 1265 kfree(td);
a2b1693b 1266 return ret;
e43473b7
VG
1267}
1268
1269void blk_throtl_exit(struct request_queue *q)
1270{
c875f4d0 1271 BUG_ON(!q->td);
da527770 1272 throtl_shutdown_wq(q);
3c798398 1273 blkcg_deactivate_policy(q, &blkcg_policy_throtl);
c9a929dd 1274 kfree(q->td);
e43473b7
VG
1275}
1276
1277static int __init throtl_init(void)
1278{
450adcbe
VG
1279 kthrotld_workqueue = alloc_workqueue("kthrotld", WQ_MEM_RECLAIM, 0);
1280 if (!kthrotld_workqueue)
1281 panic("Failed to create kthrotld\n");
1282
3c798398 1283 return blkcg_policy_register(&blkcg_policy_throtl);
e43473b7
VG
1284}
1285
1286module_init(throtl_init);