]> git.karo-electronics.de Git - karo-tx-linux.git/blob - block/blk-throttle.c
blk-throttle: add .low interface
[karo-tx-linux.git] / block / blk-throttle.c
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 <linux/blk-cgroup.h>
13 #include "blk.h"
14
15 /* Max dispatch from a group in 1 round */
16 static int throtl_grp_quantum = 8;
17
18 /* Total max dispatch from all groups in one round */
19 static int throtl_quantum = 32;
20
21 /* Throttling is performed over 100ms slice and after that slice is renewed */
22 static unsigned long throtl_slice = HZ/10;      /* 100 ms */
23
24 static struct blkcg_policy blkcg_policy_throtl;
25
26 /* A workqueue to queue throttle related work */
27 static struct workqueue_struct *kthrotld_workqueue;
28
29 /*
30  * To implement hierarchical throttling, throtl_grps form a tree and bios
31  * are dispatched upwards level by level until they reach the top and get
32  * issued.  When dispatching bios from the children and local group at each
33  * level, if the bios are dispatched into a single bio_list, there's a risk
34  * of a local or child group which can queue many bios at once filling up
35  * the list starving others.
36  *
37  * To avoid such starvation, dispatched bios are queued separately
38  * according to where they came from.  When they are again dispatched to
39  * the parent, they're popped in round-robin order so that no single source
40  * hogs the dispatch window.
41  *
42  * throtl_qnode is used to keep the queued bios separated by their sources.
43  * Bios are queued to throtl_qnode which in turn is queued to
44  * throtl_service_queue and then dispatched in round-robin order.
45  *
46  * It's also used to track the reference counts on blkg's.  A qnode always
47  * belongs to a throtl_grp and gets queued on itself or the parent, so
48  * incrementing the reference of the associated throtl_grp when a qnode is
49  * queued and decrementing when dequeued is enough to keep the whole blkg
50  * tree pinned while bios are in flight.
51  */
52 struct throtl_qnode {
53         struct list_head        node;           /* service_queue->queued[] */
54         struct bio_list         bios;           /* queued bios */
55         struct throtl_grp       *tg;            /* tg this qnode belongs to */
56 };
57
58 struct throtl_service_queue {
59         struct throtl_service_queue *parent_sq; /* the parent service_queue */
60
61         /*
62          * Bios queued directly to this service_queue or dispatched from
63          * children throtl_grp's.
64          */
65         struct list_head        queued[2];      /* throtl_qnode [READ/WRITE] */
66         unsigned int            nr_queued[2];   /* number of queued bios */
67
68         /*
69          * RB tree of active children throtl_grp's, which are sorted by
70          * their ->disptime.
71          */
72         struct rb_root          pending_tree;   /* RB tree of active tgs */
73         struct rb_node          *first_pending; /* first node in the tree */
74         unsigned int            nr_pending;     /* # queued in the tree */
75         unsigned long           first_pending_disptime; /* disptime of the first tg */
76         struct timer_list       pending_timer;  /* fires on first_pending_disptime */
77 };
78
79 enum tg_state_flags {
80         THROTL_TG_PENDING       = 1 << 0,       /* on parent's pending tree */
81         THROTL_TG_WAS_EMPTY     = 1 << 1,       /* bio_lists[] became non-empty */
82 };
83
84 #define rb_entry_tg(node)       rb_entry((node), struct throtl_grp, rb_node)
85
86 enum {
87         LIMIT_LOW,
88         LIMIT_MAX,
89         LIMIT_CNT,
90 };
91
92 struct throtl_grp {
93         /* must be the first member */
94         struct blkg_policy_data pd;
95
96         /* active throtl group service_queue member */
97         struct rb_node rb_node;
98
99         /* throtl_data this group belongs to */
100         struct throtl_data *td;
101
102         /* this group's service queue */
103         struct throtl_service_queue service_queue;
104
105         /*
106          * qnode_on_self is used when bios are directly queued to this
107          * throtl_grp so that local bios compete fairly with bios
108          * dispatched from children.  qnode_on_parent is used when bios are
109          * dispatched from this throtl_grp into its parent and will compete
110          * with the sibling qnode_on_parents and the parent's
111          * qnode_on_self.
112          */
113         struct throtl_qnode qnode_on_self[2];
114         struct throtl_qnode qnode_on_parent[2];
115
116         /*
117          * Dispatch time in jiffies. This is the estimated time when group
118          * will unthrottle and is ready to dispatch more bio. It is used as
119          * key to sort active groups in service tree.
120          */
121         unsigned long disptime;
122
123         unsigned int flags;
124
125         /* are there any throtl rules between this group and td? */
126         bool has_rules[2];
127
128         /* internally used bytes per second rate limits */
129         uint64_t bps[2][LIMIT_CNT];
130         /* user configured bps limits */
131         uint64_t bps_conf[2][LIMIT_CNT];
132
133         /* internally used IOPS limits */
134         unsigned int iops[2][LIMIT_CNT];
135         /* user configured IOPS limits */
136         unsigned int iops_conf[2][LIMIT_CNT];
137
138         /* Number of bytes disptached in current slice */
139         uint64_t bytes_disp[2];
140         /* Number of bio's dispatched in current slice */
141         unsigned int io_disp[2];
142
143         /* When did we start a new slice */
144         unsigned long slice_start[2];
145         unsigned long slice_end[2];
146 };
147
148 struct throtl_data
149 {
150         /* service tree for active throtl groups */
151         struct throtl_service_queue service_queue;
152
153         struct request_queue *queue;
154
155         /* Total Number of queued bios on READ and WRITE lists */
156         unsigned int nr_queued[2];
157
158         /* Work for dispatching throttled bios */
159         struct work_struct dispatch_work;
160         unsigned int limit_index;
161         bool limit_valid[LIMIT_CNT];
162 };
163
164 static void throtl_pending_timer_fn(unsigned long arg);
165
166 static inline struct throtl_grp *pd_to_tg(struct blkg_policy_data *pd)
167 {
168         return pd ? container_of(pd, struct throtl_grp, pd) : NULL;
169 }
170
171 static inline struct throtl_grp *blkg_to_tg(struct blkcg_gq *blkg)
172 {
173         return pd_to_tg(blkg_to_pd(blkg, &blkcg_policy_throtl));
174 }
175
176 static inline struct blkcg_gq *tg_to_blkg(struct throtl_grp *tg)
177 {
178         return pd_to_blkg(&tg->pd);
179 }
180
181 /**
182  * sq_to_tg - return the throl_grp the specified service queue belongs to
183  * @sq: the throtl_service_queue of interest
184  *
185  * Return the throtl_grp @sq belongs to.  If @sq is the top-level one
186  * embedded in throtl_data, %NULL is returned.
187  */
188 static struct throtl_grp *sq_to_tg(struct throtl_service_queue *sq)
189 {
190         if (sq && sq->parent_sq)
191                 return container_of(sq, struct throtl_grp, service_queue);
192         else
193                 return NULL;
194 }
195
196 /**
197  * sq_to_td - return throtl_data the specified service queue belongs to
198  * @sq: the throtl_service_queue of interest
199  *
200  * A service_queue can be embedded in either a throtl_grp or throtl_data.
201  * Determine the associated throtl_data accordingly and return it.
202  */
203 static struct throtl_data *sq_to_td(struct throtl_service_queue *sq)
204 {
205         struct throtl_grp *tg = sq_to_tg(sq);
206
207         if (tg)
208                 return tg->td;
209         else
210                 return container_of(sq, struct throtl_data, service_queue);
211 }
212
213 static uint64_t tg_bps_limit(struct throtl_grp *tg, int rw)
214 {
215         return tg->bps[rw][tg->td->limit_index];
216 }
217
218 static unsigned int tg_iops_limit(struct throtl_grp *tg, int rw)
219 {
220         return tg->iops[rw][tg->td->limit_index];
221 }
222
223 /**
224  * throtl_log - log debug message via blktrace
225  * @sq: the service_queue being reported
226  * @fmt: printf format string
227  * @args: printf args
228  *
229  * The messages are prefixed with "throtl BLKG_NAME" if @sq belongs to a
230  * throtl_grp; otherwise, just "throtl".
231  */
232 #define throtl_log(sq, fmt, args...)    do {                            \
233         struct throtl_grp *__tg = sq_to_tg((sq));                       \
234         struct throtl_data *__td = sq_to_td((sq));                      \
235                                                                         \
236         (void)__td;                                                     \
237         if (likely(!blk_trace_note_message_enabled(__td->queue)))       \
238                 break;                                                  \
239         if ((__tg)) {                                                   \
240                 char __pbuf[128];                                       \
241                                                                         \
242                 blkg_path(tg_to_blkg(__tg), __pbuf, sizeof(__pbuf));    \
243                 blk_add_trace_msg(__td->queue, "throtl %s " fmt, __pbuf, ##args); \
244         } else {                                                        \
245                 blk_add_trace_msg(__td->queue, "throtl " fmt, ##args);  \
246         }                                                               \
247 } while (0)
248
249 static void throtl_qnode_init(struct throtl_qnode *qn, struct throtl_grp *tg)
250 {
251         INIT_LIST_HEAD(&qn->node);
252         bio_list_init(&qn->bios);
253         qn->tg = tg;
254 }
255
256 /**
257  * throtl_qnode_add_bio - add a bio to a throtl_qnode and activate it
258  * @bio: bio being added
259  * @qn: qnode to add bio to
260  * @queued: the service_queue->queued[] list @qn belongs to
261  *
262  * Add @bio to @qn and put @qn on @queued if it's not already on.
263  * @qn->tg's reference count is bumped when @qn is activated.  See the
264  * comment on top of throtl_qnode definition for details.
265  */
266 static void throtl_qnode_add_bio(struct bio *bio, struct throtl_qnode *qn,
267                                  struct list_head *queued)
268 {
269         bio_list_add(&qn->bios, bio);
270         if (list_empty(&qn->node)) {
271                 list_add_tail(&qn->node, queued);
272                 blkg_get(tg_to_blkg(qn->tg));
273         }
274 }
275
276 /**
277  * throtl_peek_queued - peek the first bio on a qnode list
278  * @queued: the qnode list to peek
279  */
280 static struct bio *throtl_peek_queued(struct list_head *queued)
281 {
282         struct throtl_qnode *qn = list_first_entry(queued, struct throtl_qnode, node);
283         struct bio *bio;
284
285         if (list_empty(queued))
286                 return NULL;
287
288         bio = bio_list_peek(&qn->bios);
289         WARN_ON_ONCE(!bio);
290         return bio;
291 }
292
293 /**
294  * throtl_pop_queued - pop the first bio form a qnode list
295  * @queued: the qnode list to pop a bio from
296  * @tg_to_put: optional out argument for throtl_grp to put
297  *
298  * Pop the first bio from the qnode list @queued.  After popping, the first
299  * qnode is removed from @queued if empty or moved to the end of @queued so
300  * that the popping order is round-robin.
301  *
302  * When the first qnode is removed, its associated throtl_grp should be put
303  * too.  If @tg_to_put is NULL, this function automatically puts it;
304  * otherwise, *@tg_to_put is set to the throtl_grp to put and the caller is
305  * responsible for putting it.
306  */
307 static struct bio *throtl_pop_queued(struct list_head *queued,
308                                      struct throtl_grp **tg_to_put)
309 {
310         struct throtl_qnode *qn = list_first_entry(queued, struct throtl_qnode, node);
311         struct bio *bio;
312
313         if (list_empty(queued))
314                 return NULL;
315
316         bio = bio_list_pop(&qn->bios);
317         WARN_ON_ONCE(!bio);
318
319         if (bio_list_empty(&qn->bios)) {
320                 list_del_init(&qn->node);
321                 if (tg_to_put)
322                         *tg_to_put = qn->tg;
323                 else
324                         blkg_put(tg_to_blkg(qn->tg));
325         } else {
326                 list_move_tail(&qn->node, queued);
327         }
328
329         return bio;
330 }
331
332 /* init a service_queue, assumes the caller zeroed it */
333 static void throtl_service_queue_init(struct throtl_service_queue *sq)
334 {
335         INIT_LIST_HEAD(&sq->queued[0]);
336         INIT_LIST_HEAD(&sq->queued[1]);
337         sq->pending_tree = RB_ROOT;
338         setup_timer(&sq->pending_timer, throtl_pending_timer_fn,
339                     (unsigned long)sq);
340 }
341
342 static struct blkg_policy_data *throtl_pd_alloc(gfp_t gfp, int node)
343 {
344         struct throtl_grp *tg;
345         int rw;
346
347         tg = kzalloc_node(sizeof(*tg), gfp, node);
348         if (!tg)
349                 return NULL;
350
351         throtl_service_queue_init(&tg->service_queue);
352
353         for (rw = READ; rw <= WRITE; rw++) {
354                 throtl_qnode_init(&tg->qnode_on_self[rw], tg);
355                 throtl_qnode_init(&tg->qnode_on_parent[rw], tg);
356         }
357
358         RB_CLEAR_NODE(&tg->rb_node);
359         tg->bps[READ][LIMIT_MAX] = U64_MAX;
360         tg->bps[WRITE][LIMIT_MAX] = U64_MAX;
361         tg->iops[READ][LIMIT_MAX] = UINT_MAX;
362         tg->iops[WRITE][LIMIT_MAX] = UINT_MAX;
363         tg->bps_conf[READ][LIMIT_MAX] = U64_MAX;
364         tg->bps_conf[WRITE][LIMIT_MAX] = U64_MAX;
365         tg->iops_conf[READ][LIMIT_MAX] = UINT_MAX;
366         tg->iops_conf[WRITE][LIMIT_MAX] = UINT_MAX;
367         /* LIMIT_LOW will have default value 0 */
368
369         return &tg->pd;
370 }
371
372 static void throtl_pd_init(struct blkg_policy_data *pd)
373 {
374         struct throtl_grp *tg = pd_to_tg(pd);
375         struct blkcg_gq *blkg = tg_to_blkg(tg);
376         struct throtl_data *td = blkg->q->td;
377         struct throtl_service_queue *sq = &tg->service_queue;
378
379         /*
380          * If on the default hierarchy, we switch to properly hierarchical
381          * behavior where limits on a given throtl_grp are applied to the
382          * whole subtree rather than just the group itself.  e.g. If 16M
383          * read_bps limit is set on the root group, the whole system can't
384          * exceed 16M for the device.
385          *
386          * If not on the default hierarchy, the broken flat hierarchy
387          * behavior is retained where all throtl_grps are treated as if
388          * they're all separate root groups right below throtl_data.
389          * Limits of a group don't interact with limits of other groups
390          * regardless of the position of the group in the hierarchy.
391          */
392         sq->parent_sq = &td->service_queue;
393         if (cgroup_subsys_on_dfl(io_cgrp_subsys) && blkg->parent)
394                 sq->parent_sq = &blkg_to_tg(blkg->parent)->service_queue;
395         tg->td = td;
396 }
397
398 /*
399  * Set has_rules[] if @tg or any of its parents have limits configured.
400  * This doesn't require walking up to the top of the hierarchy as the
401  * parent's has_rules[] is guaranteed to be correct.
402  */
403 static void tg_update_has_rules(struct throtl_grp *tg)
404 {
405         struct throtl_grp *parent_tg = sq_to_tg(tg->service_queue.parent_sq);
406         struct throtl_data *td = tg->td;
407         int rw;
408
409         for (rw = READ; rw <= WRITE; rw++)
410                 tg->has_rules[rw] = (parent_tg && parent_tg->has_rules[rw]) ||
411                         (td->limit_valid[td->limit_index] &&
412                          (tg_bps_limit(tg, rw) != U64_MAX ||
413                           tg_iops_limit(tg, rw) != UINT_MAX));
414 }
415
416 static void throtl_pd_online(struct blkg_policy_data *pd)
417 {
418         /*
419          * We don't want new groups to escape the limits of its ancestors.
420          * Update has_rules[] after a new group is brought online.
421          */
422         tg_update_has_rules(pd_to_tg(pd));
423 }
424
425 static void blk_throtl_update_limit_valid(struct throtl_data *td)
426 {
427         struct cgroup_subsys_state *pos_css;
428         struct blkcg_gq *blkg;
429         bool low_valid = false;
430
431         rcu_read_lock();
432         blkg_for_each_descendant_post(blkg, pos_css, td->queue->root_blkg) {
433                 struct throtl_grp *tg = blkg_to_tg(blkg);
434
435                 if (tg->bps[READ][LIMIT_LOW] || tg->bps[WRITE][LIMIT_LOW] ||
436                     tg->iops[READ][LIMIT_LOW] || tg->iops[WRITE][LIMIT_LOW])
437                         low_valid = true;
438         }
439         rcu_read_unlock();
440
441         td->limit_valid[LIMIT_LOW] = low_valid;
442 }
443
444 static void throtl_pd_offline(struct blkg_policy_data *pd)
445 {
446         struct throtl_grp *tg = pd_to_tg(pd);
447
448         tg->bps[READ][LIMIT_LOW] = 0;
449         tg->bps[WRITE][LIMIT_LOW] = 0;
450         tg->iops[READ][LIMIT_LOW] = 0;
451         tg->iops[WRITE][LIMIT_LOW] = 0;
452
453         blk_throtl_update_limit_valid(tg->td);
454
455         if (tg->td->limit_index == LIMIT_LOW &&
456             !tg->td->limit_valid[LIMIT_LOW])
457                 tg->td->limit_index = LIMIT_MAX;
458 }
459
460 static void throtl_pd_free(struct blkg_policy_data *pd)
461 {
462         struct throtl_grp *tg = pd_to_tg(pd);
463
464         del_timer_sync(&tg->service_queue.pending_timer);
465         kfree(tg);
466 }
467
468 static struct throtl_grp *
469 throtl_rb_first(struct throtl_service_queue *parent_sq)
470 {
471         /* Service tree is empty */
472         if (!parent_sq->nr_pending)
473                 return NULL;
474
475         if (!parent_sq->first_pending)
476                 parent_sq->first_pending = rb_first(&parent_sq->pending_tree);
477
478         if (parent_sq->first_pending)
479                 return rb_entry_tg(parent_sq->first_pending);
480
481         return NULL;
482 }
483
484 static void rb_erase_init(struct rb_node *n, struct rb_root *root)
485 {
486         rb_erase(n, root);
487         RB_CLEAR_NODE(n);
488 }
489
490 static void throtl_rb_erase(struct rb_node *n,
491                             struct throtl_service_queue *parent_sq)
492 {
493         if (parent_sq->first_pending == n)
494                 parent_sq->first_pending = NULL;
495         rb_erase_init(n, &parent_sq->pending_tree);
496         --parent_sq->nr_pending;
497 }
498
499 static void update_min_dispatch_time(struct throtl_service_queue *parent_sq)
500 {
501         struct throtl_grp *tg;
502
503         tg = throtl_rb_first(parent_sq);
504         if (!tg)
505                 return;
506
507         parent_sq->first_pending_disptime = tg->disptime;
508 }
509
510 static void tg_service_queue_add(struct throtl_grp *tg)
511 {
512         struct throtl_service_queue *parent_sq = tg->service_queue.parent_sq;
513         struct rb_node **node = &parent_sq->pending_tree.rb_node;
514         struct rb_node *parent = NULL;
515         struct throtl_grp *__tg;
516         unsigned long key = tg->disptime;
517         int left = 1;
518
519         while (*node != NULL) {
520                 parent = *node;
521                 __tg = rb_entry_tg(parent);
522
523                 if (time_before(key, __tg->disptime))
524                         node = &parent->rb_left;
525                 else {
526                         node = &parent->rb_right;
527                         left = 0;
528                 }
529         }
530
531         if (left)
532                 parent_sq->first_pending = &tg->rb_node;
533
534         rb_link_node(&tg->rb_node, parent, node);
535         rb_insert_color(&tg->rb_node, &parent_sq->pending_tree);
536 }
537
538 static void __throtl_enqueue_tg(struct throtl_grp *tg)
539 {
540         tg_service_queue_add(tg);
541         tg->flags |= THROTL_TG_PENDING;
542         tg->service_queue.parent_sq->nr_pending++;
543 }
544
545 static void throtl_enqueue_tg(struct throtl_grp *tg)
546 {
547         if (!(tg->flags & THROTL_TG_PENDING))
548                 __throtl_enqueue_tg(tg);
549 }
550
551 static void __throtl_dequeue_tg(struct throtl_grp *tg)
552 {
553         throtl_rb_erase(&tg->rb_node, tg->service_queue.parent_sq);
554         tg->flags &= ~THROTL_TG_PENDING;
555 }
556
557 static void throtl_dequeue_tg(struct throtl_grp *tg)
558 {
559         if (tg->flags & THROTL_TG_PENDING)
560                 __throtl_dequeue_tg(tg);
561 }
562
563 /* Call with queue lock held */
564 static void throtl_schedule_pending_timer(struct throtl_service_queue *sq,
565                                           unsigned long expires)
566 {
567         mod_timer(&sq->pending_timer, expires);
568         throtl_log(sq, "schedule timer. delay=%lu jiffies=%lu",
569                    expires - jiffies, jiffies);
570 }
571
572 /**
573  * throtl_schedule_next_dispatch - schedule the next dispatch cycle
574  * @sq: the service_queue to schedule dispatch for
575  * @force: force scheduling
576  *
577  * Arm @sq->pending_timer so that the next dispatch cycle starts on the
578  * dispatch time of the first pending child.  Returns %true if either timer
579  * is armed or there's no pending child left.  %false if the current
580  * dispatch window is still open and the caller should continue
581  * dispatching.
582  *
583  * If @force is %true, the dispatch timer is always scheduled and this
584  * function is guaranteed to return %true.  This is to be used when the
585  * caller can't dispatch itself and needs to invoke pending_timer
586  * unconditionally.  Note that forced scheduling is likely to induce short
587  * delay before dispatch starts even if @sq->first_pending_disptime is not
588  * in the future and thus shouldn't be used in hot paths.
589  */
590 static bool throtl_schedule_next_dispatch(struct throtl_service_queue *sq,
591                                           bool force)
592 {
593         /* any pending children left? */
594         if (!sq->nr_pending)
595                 return true;
596
597         update_min_dispatch_time(sq);
598
599         /* is the next dispatch time in the future? */
600         if (force || time_after(sq->first_pending_disptime, jiffies)) {
601                 throtl_schedule_pending_timer(sq, sq->first_pending_disptime);
602                 return true;
603         }
604
605         /* tell the caller to continue dispatching */
606         return false;
607 }
608
609 static inline void throtl_start_new_slice_with_credit(struct throtl_grp *tg,
610                 bool rw, unsigned long start)
611 {
612         tg->bytes_disp[rw] = 0;
613         tg->io_disp[rw] = 0;
614
615         /*
616          * Previous slice has expired. We must have trimmed it after last
617          * bio dispatch. That means since start of last slice, we never used
618          * that bandwidth. Do try to make use of that bandwidth while giving
619          * credit.
620          */
621         if (time_after_eq(start, tg->slice_start[rw]))
622                 tg->slice_start[rw] = start;
623
624         tg->slice_end[rw] = jiffies + throtl_slice;
625         throtl_log(&tg->service_queue,
626                    "[%c] new slice with credit start=%lu end=%lu jiffies=%lu",
627                    rw == READ ? 'R' : 'W', tg->slice_start[rw],
628                    tg->slice_end[rw], jiffies);
629 }
630
631 static inline void throtl_start_new_slice(struct throtl_grp *tg, bool rw)
632 {
633         tg->bytes_disp[rw] = 0;
634         tg->io_disp[rw] = 0;
635         tg->slice_start[rw] = jiffies;
636         tg->slice_end[rw] = jiffies + throtl_slice;
637         throtl_log(&tg->service_queue,
638                    "[%c] new slice start=%lu end=%lu jiffies=%lu",
639                    rw == READ ? 'R' : 'W', tg->slice_start[rw],
640                    tg->slice_end[rw], jiffies);
641 }
642
643 static inline void throtl_set_slice_end(struct throtl_grp *tg, bool rw,
644                                         unsigned long jiffy_end)
645 {
646         tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
647 }
648
649 static inline void throtl_extend_slice(struct throtl_grp *tg, bool rw,
650                                        unsigned long jiffy_end)
651 {
652         tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
653         throtl_log(&tg->service_queue,
654                    "[%c] extend slice start=%lu end=%lu jiffies=%lu",
655                    rw == READ ? 'R' : 'W', tg->slice_start[rw],
656                    tg->slice_end[rw], jiffies);
657 }
658
659 /* Determine if previously allocated or extended slice is complete or not */
660 static bool throtl_slice_used(struct throtl_grp *tg, bool rw)
661 {
662         if (time_in_range(jiffies, tg->slice_start[rw], tg->slice_end[rw]))
663                 return false;
664
665         return 1;
666 }
667
668 /* Trim the used slices and adjust slice start accordingly */
669 static inline void throtl_trim_slice(struct throtl_grp *tg, bool rw)
670 {
671         unsigned long nr_slices, time_elapsed, io_trim;
672         u64 bytes_trim, tmp;
673
674         BUG_ON(time_before(tg->slice_end[rw], tg->slice_start[rw]));
675
676         /*
677          * If bps are unlimited (-1), then time slice don't get
678          * renewed. Don't try to trim the slice if slice is used. A new
679          * slice will start when appropriate.
680          */
681         if (throtl_slice_used(tg, rw))
682                 return;
683
684         /*
685          * A bio has been dispatched. Also adjust slice_end. It might happen
686          * that initially cgroup limit was very low resulting in high
687          * slice_end, but later limit was bumped up and bio was dispached
688          * sooner, then we need to reduce slice_end. A high bogus slice_end
689          * is bad because it does not allow new slice to start.
690          */
691
692         throtl_set_slice_end(tg, rw, jiffies + throtl_slice);
693
694         time_elapsed = jiffies - tg->slice_start[rw];
695
696         nr_slices = time_elapsed / throtl_slice;
697
698         if (!nr_slices)
699                 return;
700         tmp = tg_bps_limit(tg, rw) * throtl_slice * nr_slices;
701         do_div(tmp, HZ);
702         bytes_trim = tmp;
703
704         io_trim = (tg_iops_limit(tg, rw) * throtl_slice * nr_slices) / HZ;
705
706         if (!bytes_trim && !io_trim)
707                 return;
708
709         if (tg->bytes_disp[rw] >= bytes_trim)
710                 tg->bytes_disp[rw] -= bytes_trim;
711         else
712                 tg->bytes_disp[rw] = 0;
713
714         if (tg->io_disp[rw] >= io_trim)
715                 tg->io_disp[rw] -= io_trim;
716         else
717                 tg->io_disp[rw] = 0;
718
719         tg->slice_start[rw] += nr_slices * throtl_slice;
720
721         throtl_log(&tg->service_queue,
722                    "[%c] trim slice nr=%lu bytes=%llu io=%lu start=%lu end=%lu jiffies=%lu",
723                    rw == READ ? 'R' : 'W', nr_slices, bytes_trim, io_trim,
724                    tg->slice_start[rw], tg->slice_end[rw], jiffies);
725 }
726
727 static bool tg_with_in_iops_limit(struct throtl_grp *tg, struct bio *bio,
728                                   unsigned long *wait)
729 {
730         bool rw = bio_data_dir(bio);
731         unsigned int io_allowed;
732         unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
733         u64 tmp;
734
735         jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
736
737         /* Slice has just started. Consider one slice interval */
738         if (!jiffy_elapsed)
739                 jiffy_elapsed_rnd = throtl_slice;
740
741         jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
742
743         /*
744          * jiffy_elapsed_rnd should not be a big value as minimum iops can be
745          * 1 then at max jiffy elapsed should be equivalent of 1 second as we
746          * will allow dispatch after 1 second and after that slice should
747          * have been trimmed.
748          */
749
750         tmp = (u64)tg_iops_limit(tg, rw) * jiffy_elapsed_rnd;
751         do_div(tmp, HZ);
752
753         if (tmp > UINT_MAX)
754                 io_allowed = UINT_MAX;
755         else
756                 io_allowed = tmp;
757
758         if (tg->io_disp[rw] + 1 <= io_allowed) {
759                 if (wait)
760                         *wait = 0;
761                 return true;
762         }
763
764         /* Calc approx time to dispatch */
765         jiffy_wait = ((tg->io_disp[rw] + 1) * HZ) / tg_iops_limit(tg, rw) + 1;
766
767         if (jiffy_wait > jiffy_elapsed)
768                 jiffy_wait = jiffy_wait - jiffy_elapsed;
769         else
770                 jiffy_wait = 1;
771
772         if (wait)
773                 *wait = jiffy_wait;
774         return 0;
775 }
776
777 static bool tg_with_in_bps_limit(struct throtl_grp *tg, struct bio *bio,
778                                  unsigned long *wait)
779 {
780         bool rw = bio_data_dir(bio);
781         u64 bytes_allowed, extra_bytes, tmp;
782         unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
783
784         jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
785
786         /* Slice has just started. Consider one slice interval */
787         if (!jiffy_elapsed)
788                 jiffy_elapsed_rnd = throtl_slice;
789
790         jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
791
792         tmp = tg_bps_limit(tg, rw) * jiffy_elapsed_rnd;
793         do_div(tmp, HZ);
794         bytes_allowed = tmp;
795
796         if (tg->bytes_disp[rw] + bio->bi_iter.bi_size <= bytes_allowed) {
797                 if (wait)
798                         *wait = 0;
799                 return true;
800         }
801
802         /* Calc approx time to dispatch */
803         extra_bytes = tg->bytes_disp[rw] + bio->bi_iter.bi_size - bytes_allowed;
804         jiffy_wait = div64_u64(extra_bytes * HZ, tg_bps_limit(tg, rw));
805
806         if (!jiffy_wait)
807                 jiffy_wait = 1;
808
809         /*
810          * This wait time is without taking into consideration the rounding
811          * up we did. Add that time also.
812          */
813         jiffy_wait = jiffy_wait + (jiffy_elapsed_rnd - jiffy_elapsed);
814         if (wait)
815                 *wait = jiffy_wait;
816         return 0;
817 }
818
819 /*
820  * Returns whether one can dispatch a bio or not. Also returns approx number
821  * of jiffies to wait before this bio is with-in IO rate and can be dispatched
822  */
823 static bool tg_may_dispatch(struct throtl_grp *tg, struct bio *bio,
824                             unsigned long *wait)
825 {
826         bool rw = bio_data_dir(bio);
827         unsigned long bps_wait = 0, iops_wait = 0, max_wait = 0;
828
829         /*
830          * Currently whole state machine of group depends on first bio
831          * queued in the group bio list. So one should not be calling
832          * this function with a different bio if there are other bios
833          * queued.
834          */
835         BUG_ON(tg->service_queue.nr_queued[rw] &&
836                bio != throtl_peek_queued(&tg->service_queue.queued[rw]));
837
838         /* If tg->bps = -1, then BW is unlimited */
839         if (tg_bps_limit(tg, rw) == U64_MAX &&
840             tg_iops_limit(tg, rw) == UINT_MAX) {
841                 if (wait)
842                         *wait = 0;
843                 return true;
844         }
845
846         /*
847          * If previous slice expired, start a new one otherwise renew/extend
848          * existing slice to make sure it is at least throtl_slice interval
849          * long since now. New slice is started only for empty throttle group.
850          * If there is queued bio, that means there should be an active
851          * slice and it should be extended instead.
852          */
853         if (throtl_slice_used(tg, rw) && !(tg->service_queue.nr_queued[rw]))
854                 throtl_start_new_slice(tg, rw);
855         else {
856                 if (time_before(tg->slice_end[rw], jiffies + throtl_slice))
857                         throtl_extend_slice(tg, rw, jiffies + throtl_slice);
858         }
859
860         if (tg_with_in_bps_limit(tg, bio, &bps_wait) &&
861             tg_with_in_iops_limit(tg, bio, &iops_wait)) {
862                 if (wait)
863                         *wait = 0;
864                 return 1;
865         }
866
867         max_wait = max(bps_wait, iops_wait);
868
869         if (wait)
870                 *wait = max_wait;
871
872         if (time_before(tg->slice_end[rw], jiffies + max_wait))
873                 throtl_extend_slice(tg, rw, jiffies + max_wait);
874
875         return 0;
876 }
877
878 static void throtl_charge_bio(struct throtl_grp *tg, struct bio *bio)
879 {
880         bool rw = bio_data_dir(bio);
881
882         /* Charge the bio to the group */
883         tg->bytes_disp[rw] += bio->bi_iter.bi_size;
884         tg->io_disp[rw]++;
885
886         /*
887          * BIO_THROTTLED is used to prevent the same bio to be throttled
888          * more than once as a throttled bio will go through blk-throtl the
889          * second time when it eventually gets issued.  Set it when a bio
890          * is being charged to a tg.
891          */
892         if (!bio_flagged(bio, BIO_THROTTLED))
893                 bio_set_flag(bio, BIO_THROTTLED);
894 }
895
896 /**
897  * throtl_add_bio_tg - add a bio to the specified throtl_grp
898  * @bio: bio to add
899  * @qn: qnode to use
900  * @tg: the target throtl_grp
901  *
902  * Add @bio to @tg's service_queue using @qn.  If @qn is not specified,
903  * tg->qnode_on_self[] is used.
904  */
905 static void throtl_add_bio_tg(struct bio *bio, struct throtl_qnode *qn,
906                               struct throtl_grp *tg)
907 {
908         struct throtl_service_queue *sq = &tg->service_queue;
909         bool rw = bio_data_dir(bio);
910
911         if (!qn)
912                 qn = &tg->qnode_on_self[rw];
913
914         /*
915          * If @tg doesn't currently have any bios queued in the same
916          * direction, queueing @bio can change when @tg should be
917          * dispatched.  Mark that @tg was empty.  This is automatically
918          * cleaered on the next tg_update_disptime().
919          */
920         if (!sq->nr_queued[rw])
921                 tg->flags |= THROTL_TG_WAS_EMPTY;
922
923         throtl_qnode_add_bio(bio, qn, &sq->queued[rw]);
924
925         sq->nr_queued[rw]++;
926         throtl_enqueue_tg(tg);
927 }
928
929 static void tg_update_disptime(struct throtl_grp *tg)
930 {
931         struct throtl_service_queue *sq = &tg->service_queue;
932         unsigned long read_wait = -1, write_wait = -1, min_wait = -1, disptime;
933         struct bio *bio;
934
935         bio = throtl_peek_queued(&sq->queued[READ]);
936         if (bio)
937                 tg_may_dispatch(tg, bio, &read_wait);
938
939         bio = throtl_peek_queued(&sq->queued[WRITE]);
940         if (bio)
941                 tg_may_dispatch(tg, bio, &write_wait);
942
943         min_wait = min(read_wait, write_wait);
944         disptime = jiffies + min_wait;
945
946         /* Update dispatch time */
947         throtl_dequeue_tg(tg);
948         tg->disptime = disptime;
949         throtl_enqueue_tg(tg);
950
951         /* see throtl_add_bio_tg() */
952         tg->flags &= ~THROTL_TG_WAS_EMPTY;
953 }
954
955 static void start_parent_slice_with_credit(struct throtl_grp *child_tg,
956                                         struct throtl_grp *parent_tg, bool rw)
957 {
958         if (throtl_slice_used(parent_tg, rw)) {
959                 throtl_start_new_slice_with_credit(parent_tg, rw,
960                                 child_tg->slice_start[rw]);
961         }
962
963 }
964
965 static void tg_dispatch_one_bio(struct throtl_grp *tg, bool rw)
966 {
967         struct throtl_service_queue *sq = &tg->service_queue;
968         struct throtl_service_queue *parent_sq = sq->parent_sq;
969         struct throtl_grp *parent_tg = sq_to_tg(parent_sq);
970         struct throtl_grp *tg_to_put = NULL;
971         struct bio *bio;
972
973         /*
974          * @bio is being transferred from @tg to @parent_sq.  Popping a bio
975          * from @tg may put its reference and @parent_sq might end up
976          * getting released prematurely.  Remember the tg to put and put it
977          * after @bio is transferred to @parent_sq.
978          */
979         bio = throtl_pop_queued(&sq->queued[rw], &tg_to_put);
980         sq->nr_queued[rw]--;
981
982         throtl_charge_bio(tg, bio);
983
984         /*
985          * If our parent is another tg, we just need to transfer @bio to
986          * the parent using throtl_add_bio_tg().  If our parent is
987          * @td->service_queue, @bio is ready to be issued.  Put it on its
988          * bio_lists[] and decrease total number queued.  The caller is
989          * responsible for issuing these bios.
990          */
991         if (parent_tg) {
992                 throtl_add_bio_tg(bio, &tg->qnode_on_parent[rw], parent_tg);
993                 start_parent_slice_with_credit(tg, parent_tg, rw);
994         } else {
995                 throtl_qnode_add_bio(bio, &tg->qnode_on_parent[rw],
996                                      &parent_sq->queued[rw]);
997                 BUG_ON(tg->td->nr_queued[rw] <= 0);
998                 tg->td->nr_queued[rw]--;
999         }
1000
1001         throtl_trim_slice(tg, rw);
1002
1003         if (tg_to_put)
1004                 blkg_put(tg_to_blkg(tg_to_put));
1005 }
1006
1007 static int throtl_dispatch_tg(struct throtl_grp *tg)
1008 {
1009         struct throtl_service_queue *sq = &tg->service_queue;
1010         unsigned int nr_reads = 0, nr_writes = 0;
1011         unsigned int max_nr_reads = throtl_grp_quantum*3/4;
1012         unsigned int max_nr_writes = throtl_grp_quantum - max_nr_reads;
1013         struct bio *bio;
1014
1015         /* Try to dispatch 75% READS and 25% WRITES */
1016
1017         while ((bio = throtl_peek_queued(&sq->queued[READ])) &&
1018                tg_may_dispatch(tg, bio, NULL)) {
1019
1020                 tg_dispatch_one_bio(tg, bio_data_dir(bio));
1021                 nr_reads++;
1022
1023                 if (nr_reads >= max_nr_reads)
1024                         break;
1025         }
1026
1027         while ((bio = throtl_peek_queued(&sq->queued[WRITE])) &&
1028                tg_may_dispatch(tg, bio, NULL)) {
1029
1030                 tg_dispatch_one_bio(tg, bio_data_dir(bio));
1031                 nr_writes++;
1032
1033                 if (nr_writes >= max_nr_writes)
1034                         break;
1035         }
1036
1037         return nr_reads + nr_writes;
1038 }
1039
1040 static int throtl_select_dispatch(struct throtl_service_queue *parent_sq)
1041 {
1042         unsigned int nr_disp = 0;
1043
1044         while (1) {
1045                 struct throtl_grp *tg = throtl_rb_first(parent_sq);
1046                 struct throtl_service_queue *sq = &tg->service_queue;
1047
1048                 if (!tg)
1049                         break;
1050
1051                 if (time_before(jiffies, tg->disptime))
1052                         break;
1053
1054                 throtl_dequeue_tg(tg);
1055
1056                 nr_disp += throtl_dispatch_tg(tg);
1057
1058                 if (sq->nr_queued[0] || sq->nr_queued[1])
1059                         tg_update_disptime(tg);
1060
1061                 if (nr_disp >= throtl_quantum)
1062                         break;
1063         }
1064
1065         return nr_disp;
1066 }
1067
1068 /**
1069  * throtl_pending_timer_fn - timer function for service_queue->pending_timer
1070  * @arg: the throtl_service_queue being serviced
1071  *
1072  * This timer is armed when a child throtl_grp with active bio's become
1073  * pending and queued on the service_queue's pending_tree and expires when
1074  * the first child throtl_grp should be dispatched.  This function
1075  * dispatches bio's from the children throtl_grps to the parent
1076  * service_queue.
1077  *
1078  * If the parent's parent is another throtl_grp, dispatching is propagated
1079  * by either arming its pending_timer or repeating dispatch directly.  If
1080  * the top-level service_tree is reached, throtl_data->dispatch_work is
1081  * kicked so that the ready bio's are issued.
1082  */
1083 static void throtl_pending_timer_fn(unsigned long arg)
1084 {
1085         struct throtl_service_queue *sq = (void *)arg;
1086         struct throtl_grp *tg = sq_to_tg(sq);
1087         struct throtl_data *td = sq_to_td(sq);
1088         struct request_queue *q = td->queue;
1089         struct throtl_service_queue *parent_sq;
1090         bool dispatched;
1091         int ret;
1092
1093         spin_lock_irq(q->queue_lock);
1094 again:
1095         parent_sq = sq->parent_sq;
1096         dispatched = false;
1097
1098         while (true) {
1099                 throtl_log(sq, "dispatch nr_queued=%u read=%u write=%u",
1100                            sq->nr_queued[READ] + sq->nr_queued[WRITE],
1101                            sq->nr_queued[READ], sq->nr_queued[WRITE]);
1102
1103                 ret = throtl_select_dispatch(sq);
1104                 if (ret) {
1105                         throtl_log(sq, "bios disp=%u", ret);
1106                         dispatched = true;
1107                 }
1108
1109                 if (throtl_schedule_next_dispatch(sq, false))
1110                         break;
1111
1112                 /* this dispatch windows is still open, relax and repeat */
1113                 spin_unlock_irq(q->queue_lock);
1114                 cpu_relax();
1115                 spin_lock_irq(q->queue_lock);
1116         }
1117
1118         if (!dispatched)
1119                 goto out_unlock;
1120
1121         if (parent_sq) {
1122                 /* @parent_sq is another throl_grp, propagate dispatch */
1123                 if (tg->flags & THROTL_TG_WAS_EMPTY) {
1124                         tg_update_disptime(tg);
1125                         if (!throtl_schedule_next_dispatch(parent_sq, false)) {
1126                                 /* window is already open, repeat dispatching */
1127                                 sq = parent_sq;
1128                                 tg = sq_to_tg(sq);
1129                                 goto again;
1130                         }
1131                 }
1132         } else {
1133                 /* reached the top-level, queue issueing */
1134                 queue_work(kthrotld_workqueue, &td->dispatch_work);
1135         }
1136 out_unlock:
1137         spin_unlock_irq(q->queue_lock);
1138 }
1139
1140 /**
1141  * blk_throtl_dispatch_work_fn - work function for throtl_data->dispatch_work
1142  * @work: work item being executed
1143  *
1144  * This function is queued for execution when bio's reach the bio_lists[]
1145  * of throtl_data->service_queue.  Those bio's are ready and issued by this
1146  * function.
1147  */
1148 static void blk_throtl_dispatch_work_fn(struct work_struct *work)
1149 {
1150         struct throtl_data *td = container_of(work, struct throtl_data,
1151                                               dispatch_work);
1152         struct throtl_service_queue *td_sq = &td->service_queue;
1153         struct request_queue *q = td->queue;
1154         struct bio_list bio_list_on_stack;
1155         struct bio *bio;
1156         struct blk_plug plug;
1157         int rw;
1158
1159         bio_list_init(&bio_list_on_stack);
1160
1161         spin_lock_irq(q->queue_lock);
1162         for (rw = READ; rw <= WRITE; rw++)
1163                 while ((bio = throtl_pop_queued(&td_sq->queued[rw], NULL)))
1164                         bio_list_add(&bio_list_on_stack, bio);
1165         spin_unlock_irq(q->queue_lock);
1166
1167         if (!bio_list_empty(&bio_list_on_stack)) {
1168                 blk_start_plug(&plug);
1169                 while((bio = bio_list_pop(&bio_list_on_stack)))
1170                         generic_make_request(bio);
1171                 blk_finish_plug(&plug);
1172         }
1173 }
1174
1175 static u64 tg_prfill_conf_u64(struct seq_file *sf, struct blkg_policy_data *pd,
1176                               int off)
1177 {
1178         struct throtl_grp *tg = pd_to_tg(pd);
1179         u64 v = *(u64 *)((void *)tg + off);
1180
1181         if (v == U64_MAX)
1182                 return 0;
1183         return __blkg_prfill_u64(sf, pd, v);
1184 }
1185
1186 static u64 tg_prfill_conf_uint(struct seq_file *sf, struct blkg_policy_data *pd,
1187                                int off)
1188 {
1189         struct throtl_grp *tg = pd_to_tg(pd);
1190         unsigned int v = *(unsigned int *)((void *)tg + off);
1191
1192         if (v == UINT_MAX)
1193                 return 0;
1194         return __blkg_prfill_u64(sf, pd, v);
1195 }
1196
1197 static int tg_print_conf_u64(struct seq_file *sf, void *v)
1198 {
1199         blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), tg_prfill_conf_u64,
1200                           &blkcg_policy_throtl, seq_cft(sf)->private, false);
1201         return 0;
1202 }
1203
1204 static int tg_print_conf_uint(struct seq_file *sf, void *v)
1205 {
1206         blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), tg_prfill_conf_uint,
1207                           &blkcg_policy_throtl, seq_cft(sf)->private, false);
1208         return 0;
1209 }
1210
1211 static void tg_conf_updated(struct throtl_grp *tg)
1212 {
1213         struct throtl_service_queue *sq = &tg->service_queue;
1214         struct cgroup_subsys_state *pos_css;
1215         struct blkcg_gq *blkg;
1216
1217         throtl_log(&tg->service_queue,
1218                    "limit change rbps=%llu wbps=%llu riops=%u wiops=%u",
1219                    tg_bps_limit(tg, READ), tg_bps_limit(tg, WRITE),
1220                    tg_iops_limit(tg, READ), tg_iops_limit(tg, WRITE));
1221
1222         /*
1223          * Update has_rules[] flags for the updated tg's subtree.  A tg is
1224          * considered to have rules if either the tg itself or any of its
1225          * ancestors has rules.  This identifies groups without any
1226          * restrictions in the whole hierarchy and allows them to bypass
1227          * blk-throttle.
1228          */
1229         blkg_for_each_descendant_pre(blkg, pos_css, tg_to_blkg(tg))
1230                 tg_update_has_rules(blkg_to_tg(blkg));
1231
1232         /*
1233          * We're already holding queue_lock and know @tg is valid.  Let's
1234          * apply the new config directly.
1235          *
1236          * Restart the slices for both READ and WRITES. It might happen
1237          * that a group's limit are dropped suddenly and we don't want to
1238          * account recently dispatched IO with new low rate.
1239          */
1240         throtl_start_new_slice(tg, 0);
1241         throtl_start_new_slice(tg, 1);
1242
1243         if (tg->flags & THROTL_TG_PENDING) {
1244                 tg_update_disptime(tg);
1245                 throtl_schedule_next_dispatch(sq->parent_sq, true);
1246         }
1247 }
1248
1249 static ssize_t tg_set_conf(struct kernfs_open_file *of,
1250                            char *buf, size_t nbytes, loff_t off, bool is_u64)
1251 {
1252         struct blkcg *blkcg = css_to_blkcg(of_css(of));
1253         struct blkg_conf_ctx ctx;
1254         struct throtl_grp *tg;
1255         int ret;
1256         u64 v;
1257
1258         ret = blkg_conf_prep(blkcg, &blkcg_policy_throtl, buf, &ctx);
1259         if (ret)
1260                 return ret;
1261
1262         ret = -EINVAL;
1263         if (sscanf(ctx.body, "%llu", &v) != 1)
1264                 goto out_finish;
1265         if (!v)
1266                 v = U64_MAX;
1267
1268         tg = blkg_to_tg(ctx.blkg);
1269
1270         if (is_u64)
1271                 *(u64 *)((void *)tg + of_cft(of)->private) = v;
1272         else
1273                 *(unsigned int *)((void *)tg + of_cft(of)->private) = v;
1274
1275         tg_conf_updated(tg);
1276         ret = 0;
1277 out_finish:
1278         blkg_conf_finish(&ctx);
1279         return ret ?: nbytes;
1280 }
1281
1282 static ssize_t tg_set_conf_u64(struct kernfs_open_file *of,
1283                                char *buf, size_t nbytes, loff_t off)
1284 {
1285         return tg_set_conf(of, buf, nbytes, off, true);
1286 }
1287
1288 static ssize_t tg_set_conf_uint(struct kernfs_open_file *of,
1289                                 char *buf, size_t nbytes, loff_t off)
1290 {
1291         return tg_set_conf(of, buf, nbytes, off, false);
1292 }
1293
1294 static struct cftype throtl_legacy_files[] = {
1295         {
1296                 .name = "throttle.read_bps_device",
1297                 .private = offsetof(struct throtl_grp, bps[READ][LIMIT_MAX]),
1298                 .seq_show = tg_print_conf_u64,
1299                 .write = tg_set_conf_u64,
1300         },
1301         {
1302                 .name = "throttle.write_bps_device",
1303                 .private = offsetof(struct throtl_grp, bps[WRITE][LIMIT_MAX]),
1304                 .seq_show = tg_print_conf_u64,
1305                 .write = tg_set_conf_u64,
1306         },
1307         {
1308                 .name = "throttle.read_iops_device",
1309                 .private = offsetof(struct throtl_grp, iops[READ][LIMIT_MAX]),
1310                 .seq_show = tg_print_conf_uint,
1311                 .write = tg_set_conf_uint,
1312         },
1313         {
1314                 .name = "throttle.write_iops_device",
1315                 .private = offsetof(struct throtl_grp, iops[WRITE][LIMIT_MAX]),
1316                 .seq_show = tg_print_conf_uint,
1317                 .write = tg_set_conf_uint,
1318         },
1319         {
1320                 .name = "throttle.io_service_bytes",
1321                 .private = (unsigned long)&blkcg_policy_throtl,
1322                 .seq_show = blkg_print_stat_bytes,
1323         },
1324         {
1325                 .name = "throttle.io_serviced",
1326                 .private = (unsigned long)&blkcg_policy_throtl,
1327                 .seq_show = blkg_print_stat_ios,
1328         },
1329         { }     /* terminate */
1330 };
1331
1332 static u64 tg_prfill_limit(struct seq_file *sf, struct blkg_policy_data *pd,
1333                          int off)
1334 {
1335         struct throtl_grp *tg = pd_to_tg(pd);
1336         const char *dname = blkg_dev_name(pd->blkg);
1337         char bufs[4][21] = { "max", "max", "max", "max" };
1338         u64 bps_dft;
1339         unsigned int iops_dft;
1340
1341         if (!dname)
1342                 return 0;
1343
1344         if (off == LIMIT_LOW) {
1345                 bps_dft = 0;
1346                 iops_dft = 0;
1347         } else {
1348                 bps_dft = U64_MAX;
1349                 iops_dft = UINT_MAX;
1350         }
1351
1352         if (tg->bps_conf[READ][off] == bps_dft &&
1353             tg->bps_conf[WRITE][off] == bps_dft &&
1354             tg->iops_conf[READ][off] == iops_dft &&
1355             tg->iops_conf[WRITE][off] == iops_dft)
1356                 return 0;
1357
1358         if (tg->bps_conf[READ][off] != bps_dft)
1359                 snprintf(bufs[0], sizeof(bufs[0]), "%llu",
1360                         tg->bps_conf[READ][off]);
1361         if (tg->bps_conf[WRITE][off] != bps_dft)
1362                 snprintf(bufs[1], sizeof(bufs[1]), "%llu",
1363                         tg->bps_conf[WRITE][off]);
1364         if (tg->iops_conf[READ][off] != iops_dft)
1365                 snprintf(bufs[2], sizeof(bufs[2]), "%u",
1366                         tg->iops_conf[READ][off]);
1367         if (tg->iops_conf[WRITE][off] != iops_dft)
1368                 snprintf(bufs[3], sizeof(bufs[3]), "%u",
1369                         tg->iops_conf[WRITE][off]);
1370
1371         seq_printf(sf, "%s rbps=%s wbps=%s riops=%s wiops=%s\n",
1372                    dname, bufs[0], bufs[1], bufs[2], bufs[3]);
1373         return 0;
1374 }
1375
1376 static int tg_print_limit(struct seq_file *sf, void *v)
1377 {
1378         blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), tg_prfill_limit,
1379                           &blkcg_policy_throtl, seq_cft(sf)->private, false);
1380         return 0;
1381 }
1382
1383 static ssize_t tg_set_limit(struct kernfs_open_file *of,
1384                           char *buf, size_t nbytes, loff_t off)
1385 {
1386         struct blkcg *blkcg = css_to_blkcg(of_css(of));
1387         struct blkg_conf_ctx ctx;
1388         struct throtl_grp *tg;
1389         u64 v[4];
1390         int ret;
1391         int index = of_cft(of)->private;
1392
1393         ret = blkg_conf_prep(blkcg, &blkcg_policy_throtl, buf, &ctx);
1394         if (ret)
1395                 return ret;
1396
1397         tg = blkg_to_tg(ctx.blkg);
1398
1399         v[0] = tg->bps_conf[READ][index];
1400         v[1] = tg->bps_conf[WRITE][index];
1401         v[2] = tg->iops_conf[READ][index];
1402         v[3] = tg->iops_conf[WRITE][index];
1403
1404         while (true) {
1405                 char tok[27];   /* wiops=18446744073709551616 */
1406                 char *p;
1407                 u64 val = U64_MAX;
1408                 int len;
1409
1410                 if (sscanf(ctx.body, "%26s%n", tok, &len) != 1)
1411                         break;
1412                 if (tok[0] == '\0')
1413                         break;
1414                 ctx.body += len;
1415
1416                 ret = -EINVAL;
1417                 p = tok;
1418                 strsep(&p, "=");
1419                 if (!p || (sscanf(p, "%llu", &val) != 1 && strcmp(p, "max")))
1420                         goto out_finish;
1421
1422                 ret = -ERANGE;
1423                 if (!val)
1424                         goto out_finish;
1425
1426                 ret = -EINVAL;
1427                 if (!strcmp(tok, "rbps"))
1428                         v[0] = val;
1429                 else if (!strcmp(tok, "wbps"))
1430                         v[1] = val;
1431                 else if (!strcmp(tok, "riops"))
1432                         v[2] = min_t(u64, val, UINT_MAX);
1433                 else if (!strcmp(tok, "wiops"))
1434                         v[3] = min_t(u64, val, UINT_MAX);
1435                 else
1436                         goto out_finish;
1437         }
1438
1439         tg->bps_conf[READ][index] = v[0];
1440         tg->bps_conf[WRITE][index] = v[1];
1441         tg->iops_conf[READ][index] = v[2];
1442         tg->iops_conf[WRITE][index] = v[3];
1443
1444         if (index == LIMIT_MAX) {
1445                 tg->bps[READ][index] = v[0];
1446                 tg->bps[WRITE][index] = v[1];
1447                 tg->iops[READ][index] = v[2];
1448                 tg->iops[WRITE][index] = v[3];
1449         }
1450         tg->bps[READ][LIMIT_LOW] = min(tg->bps_conf[READ][LIMIT_LOW],
1451                 tg->bps_conf[READ][LIMIT_MAX]);
1452         tg->bps[WRITE][LIMIT_LOW] = min(tg->bps_conf[WRITE][LIMIT_LOW],
1453                 tg->bps_conf[WRITE][LIMIT_MAX]);
1454         tg->iops[READ][LIMIT_LOW] = min(tg->iops_conf[READ][LIMIT_LOW],
1455                 tg->iops_conf[READ][LIMIT_MAX]);
1456         tg->iops[WRITE][LIMIT_LOW] = min(tg->iops_conf[WRITE][LIMIT_LOW],
1457                 tg->iops_conf[WRITE][LIMIT_MAX]);
1458
1459         if (index == LIMIT_LOW) {
1460                 blk_throtl_update_limit_valid(tg->td);
1461                 if (tg->td->limit_valid[LIMIT_LOW])
1462                         tg->td->limit_index = LIMIT_LOW;
1463         }
1464         tg_conf_updated(tg);
1465         ret = 0;
1466 out_finish:
1467         blkg_conf_finish(&ctx);
1468         return ret ?: nbytes;
1469 }
1470
1471 static struct cftype throtl_files[] = {
1472 #ifdef CONFIG_BLK_DEV_THROTTLING_LOW
1473         {
1474                 .name = "low",
1475                 .flags = CFTYPE_NOT_ON_ROOT,
1476                 .seq_show = tg_print_limit,
1477                 .write = tg_set_limit,
1478                 .private = LIMIT_LOW,
1479         },
1480 #endif
1481         {
1482                 .name = "max",
1483                 .flags = CFTYPE_NOT_ON_ROOT,
1484                 .seq_show = tg_print_limit,
1485                 .write = tg_set_limit,
1486                 .private = LIMIT_MAX,
1487         },
1488         { }     /* terminate */
1489 };
1490
1491 static void throtl_shutdown_wq(struct request_queue *q)
1492 {
1493         struct throtl_data *td = q->td;
1494
1495         cancel_work_sync(&td->dispatch_work);
1496 }
1497
1498 static struct blkcg_policy blkcg_policy_throtl = {
1499         .dfl_cftypes            = throtl_files,
1500         .legacy_cftypes         = throtl_legacy_files,
1501
1502         .pd_alloc_fn            = throtl_pd_alloc,
1503         .pd_init_fn             = throtl_pd_init,
1504         .pd_online_fn           = throtl_pd_online,
1505         .pd_offline_fn          = throtl_pd_offline,
1506         .pd_free_fn             = throtl_pd_free,
1507 };
1508
1509 bool blk_throtl_bio(struct request_queue *q, struct blkcg_gq *blkg,
1510                     struct bio *bio)
1511 {
1512         struct throtl_qnode *qn = NULL;
1513         struct throtl_grp *tg = blkg_to_tg(blkg ?: q->root_blkg);
1514         struct throtl_service_queue *sq;
1515         bool rw = bio_data_dir(bio);
1516         bool throttled = false;
1517
1518         WARN_ON_ONCE(!rcu_read_lock_held());
1519
1520         /* see throtl_charge_bio() */
1521         if (bio_flagged(bio, BIO_THROTTLED) || !tg->has_rules[rw])
1522                 goto out;
1523
1524         spin_lock_irq(q->queue_lock);
1525
1526         if (unlikely(blk_queue_bypass(q)))
1527                 goto out_unlock;
1528
1529         sq = &tg->service_queue;
1530
1531         while (true) {
1532                 /* throtl is FIFO - if bios are already queued, should queue */
1533                 if (sq->nr_queued[rw])
1534                         break;
1535
1536                 /* if above limits, break to queue */
1537                 if (!tg_may_dispatch(tg, bio, NULL))
1538                         break;
1539
1540                 /* within limits, let's charge and dispatch directly */
1541                 throtl_charge_bio(tg, bio);
1542
1543                 /*
1544                  * We need to trim slice even when bios are not being queued
1545                  * otherwise it might happen that a bio is not queued for
1546                  * a long time and slice keeps on extending and trim is not
1547                  * called for a long time. Now if limits are reduced suddenly
1548                  * we take into account all the IO dispatched so far at new
1549                  * low rate and * newly queued IO gets a really long dispatch
1550                  * time.
1551                  *
1552                  * So keep on trimming slice even if bio is not queued.
1553                  */
1554                 throtl_trim_slice(tg, rw);
1555
1556                 /*
1557                  * @bio passed through this layer without being throttled.
1558                  * Climb up the ladder.  If we''re already at the top, it
1559                  * can be executed directly.
1560                  */
1561                 qn = &tg->qnode_on_parent[rw];
1562                 sq = sq->parent_sq;
1563                 tg = sq_to_tg(sq);
1564                 if (!tg)
1565                         goto out_unlock;
1566         }
1567
1568         /* out-of-limit, queue to @tg */
1569         throtl_log(sq, "[%c] bio. bdisp=%llu sz=%u bps=%llu iodisp=%u iops=%u queued=%d/%d",
1570                    rw == READ ? 'R' : 'W',
1571                    tg->bytes_disp[rw], bio->bi_iter.bi_size,
1572                    tg_bps_limit(tg, rw),
1573                    tg->io_disp[rw], tg_iops_limit(tg, rw),
1574                    sq->nr_queued[READ], sq->nr_queued[WRITE]);
1575
1576         bio_associate_current(bio);
1577         tg->td->nr_queued[rw]++;
1578         throtl_add_bio_tg(bio, qn, tg);
1579         throttled = true;
1580
1581         /*
1582          * Update @tg's dispatch time and force schedule dispatch if @tg
1583          * was empty before @bio.  The forced scheduling isn't likely to
1584          * cause undue delay as @bio is likely to be dispatched directly if
1585          * its @tg's disptime is not in the future.
1586          */
1587         if (tg->flags & THROTL_TG_WAS_EMPTY) {
1588                 tg_update_disptime(tg);
1589                 throtl_schedule_next_dispatch(tg->service_queue.parent_sq, true);
1590         }
1591
1592 out_unlock:
1593         spin_unlock_irq(q->queue_lock);
1594 out:
1595         /*
1596          * As multiple blk-throtls may stack in the same issue path, we
1597          * don't want bios to leave with the flag set.  Clear the flag if
1598          * being issued.
1599          */
1600         if (!throttled)
1601                 bio_clear_flag(bio, BIO_THROTTLED);
1602         return throttled;
1603 }
1604
1605 /*
1606  * Dispatch all bios from all children tg's queued on @parent_sq.  On
1607  * return, @parent_sq is guaranteed to not have any active children tg's
1608  * and all bios from previously active tg's are on @parent_sq->bio_lists[].
1609  */
1610 static void tg_drain_bios(struct throtl_service_queue *parent_sq)
1611 {
1612         struct throtl_grp *tg;
1613
1614         while ((tg = throtl_rb_first(parent_sq))) {
1615                 struct throtl_service_queue *sq = &tg->service_queue;
1616                 struct bio *bio;
1617
1618                 throtl_dequeue_tg(tg);
1619
1620                 while ((bio = throtl_peek_queued(&sq->queued[READ])))
1621                         tg_dispatch_one_bio(tg, bio_data_dir(bio));
1622                 while ((bio = throtl_peek_queued(&sq->queued[WRITE])))
1623                         tg_dispatch_one_bio(tg, bio_data_dir(bio));
1624         }
1625 }
1626
1627 /**
1628  * blk_throtl_drain - drain throttled bios
1629  * @q: request_queue to drain throttled bios for
1630  *
1631  * Dispatch all currently throttled bios on @q through ->make_request_fn().
1632  */
1633 void blk_throtl_drain(struct request_queue *q)
1634         __releases(q->queue_lock) __acquires(q->queue_lock)
1635 {
1636         struct throtl_data *td = q->td;
1637         struct blkcg_gq *blkg;
1638         struct cgroup_subsys_state *pos_css;
1639         struct bio *bio;
1640         int rw;
1641
1642         queue_lockdep_assert_held(q);
1643         rcu_read_lock();
1644
1645         /*
1646          * Drain each tg while doing post-order walk on the blkg tree, so
1647          * that all bios are propagated to td->service_queue.  It'd be
1648          * better to walk service_queue tree directly but blkg walk is
1649          * easier.
1650          */
1651         blkg_for_each_descendant_post(blkg, pos_css, td->queue->root_blkg)
1652                 tg_drain_bios(&blkg_to_tg(blkg)->service_queue);
1653
1654         /* finally, transfer bios from top-level tg's into the td */
1655         tg_drain_bios(&td->service_queue);
1656
1657         rcu_read_unlock();
1658         spin_unlock_irq(q->queue_lock);
1659
1660         /* all bios now should be in td->service_queue, issue them */
1661         for (rw = READ; rw <= WRITE; rw++)
1662                 while ((bio = throtl_pop_queued(&td->service_queue.queued[rw],
1663                                                 NULL)))
1664                         generic_make_request(bio);
1665
1666         spin_lock_irq(q->queue_lock);
1667 }
1668
1669 int blk_throtl_init(struct request_queue *q)
1670 {
1671         struct throtl_data *td;
1672         int ret;
1673
1674         td = kzalloc_node(sizeof(*td), GFP_KERNEL, q->node);
1675         if (!td)
1676                 return -ENOMEM;
1677
1678         INIT_WORK(&td->dispatch_work, blk_throtl_dispatch_work_fn);
1679         throtl_service_queue_init(&td->service_queue);
1680
1681         q->td = td;
1682         td->queue = q;
1683
1684         td->limit_valid[LIMIT_MAX] = true;
1685         td->limit_index = LIMIT_MAX;
1686         /* activate policy */
1687         ret = blkcg_activate_policy(q, &blkcg_policy_throtl);
1688         if (ret)
1689                 kfree(td);
1690         return ret;
1691 }
1692
1693 void blk_throtl_exit(struct request_queue *q)
1694 {
1695         BUG_ON(!q->td);
1696         throtl_shutdown_wq(q);
1697         blkcg_deactivate_policy(q, &blkcg_policy_throtl);
1698         kfree(q->td);
1699 }
1700
1701 static int __init throtl_init(void)
1702 {
1703         kthrotld_workqueue = alloc_workqueue("kthrotld", WQ_MEM_RECLAIM, 0);
1704         if (!kthrotld_workqueue)
1705                 panic("Failed to create kthrotld\n");
1706
1707         return blkcg_policy_register(&blkcg_policy_throtl);
1708 }
1709
1710 module_init(throtl_init);