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