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