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