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