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