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