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> | |
12 | #include "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 | ||
0381411e TH |
24 | static struct blkio_policy_type blkio_policy_throtl; |
25 | ||
450adcbe VG |
26 | /* A workqueue to queue throttle related work */ |
27 | static struct workqueue_struct *kthrotld_workqueue; | |
28 | static void throtl_schedule_delayed_work(struct throtl_data *td, | |
29 | unsigned long delay); | |
30 | ||
e43473b7 VG |
31 | struct throtl_rb_root { |
32 | struct rb_root rb; | |
33 | struct rb_node *left; | |
34 | unsigned int count; | |
35 | unsigned long min_disptime; | |
36 | }; | |
37 | ||
38 | #define THROTL_RB_ROOT (struct throtl_rb_root) { .rb = RB_ROOT, .left = NULL, \ | |
39 | .count = 0, .min_disptime = 0} | |
40 | ||
41 | #define rb_entry_tg(node) rb_entry((node), struct throtl_grp, rb_node) | |
42 | ||
43 | struct throtl_grp { | |
e43473b7 VG |
44 | /* active throtl group service_tree member */ |
45 | struct rb_node rb_node; | |
46 | ||
47 | /* | |
48 | * Dispatch time in jiffies. This is the estimated time when group | |
49 | * will unthrottle and is ready to dispatch more bio. It is used as | |
50 | * key to sort active groups in service tree. | |
51 | */ | |
52 | unsigned long disptime; | |
53 | ||
e43473b7 VG |
54 | unsigned int flags; |
55 | ||
56 | /* Two lists for READ and WRITE */ | |
57 | struct bio_list bio_lists[2]; | |
58 | ||
59 | /* Number of queued bios on READ and WRITE lists */ | |
60 | unsigned int nr_queued[2]; | |
61 | ||
62 | /* bytes per second rate limits */ | |
63 | uint64_t bps[2]; | |
64 | ||
8e89d13f VG |
65 | /* IOPS limits */ |
66 | unsigned int iops[2]; | |
67 | ||
e43473b7 VG |
68 | /* Number of bytes disptached in current slice */ |
69 | uint64_t bytes_disp[2]; | |
8e89d13f VG |
70 | /* Number of bio's dispatched in current slice */ |
71 | unsigned int io_disp[2]; | |
e43473b7 VG |
72 | |
73 | /* When did we start a new slice */ | |
74 | unsigned long slice_start[2]; | |
75 | unsigned long slice_end[2]; | |
fe071437 VG |
76 | |
77 | /* Some throttle limits got updated for the group */ | |
6f037937 | 78 | int limits_changed; |
e43473b7 VG |
79 | }; |
80 | ||
81 | struct throtl_data | |
82 | { | |
e43473b7 VG |
83 | /* service tree for active throtl groups */ |
84 | struct throtl_rb_root tg_service_tree; | |
85 | ||
29b12589 | 86 | struct throtl_grp *root_tg; |
e43473b7 VG |
87 | struct request_queue *queue; |
88 | ||
89 | /* Total Number of queued bios on READ and WRITE lists */ | |
90 | unsigned int nr_queued[2]; | |
91 | ||
92 | /* | |
02977e4a | 93 | * number of total undestroyed groups |
e43473b7 VG |
94 | */ |
95 | unsigned int nr_undestroyed_grps; | |
96 | ||
97 | /* Work for dispatching throttled bios */ | |
98 | struct delayed_work throtl_work; | |
fe071437 | 99 | |
6f037937 | 100 | int limits_changed; |
e43473b7 VG |
101 | }; |
102 | ||
0381411e TH |
103 | static inline struct throtl_grp *blkg_to_tg(struct blkio_group *blkg) |
104 | { | |
105 | return blkg_to_pdata(blkg, &blkio_policy_throtl); | |
106 | } | |
107 | ||
108 | static inline struct blkio_group *tg_to_blkg(struct throtl_grp *tg) | |
109 | { | |
110 | return pdata_to_blkg(tg, &blkio_policy_throtl); | |
111 | } | |
112 | ||
e43473b7 VG |
113 | enum tg_state_flags { |
114 | THROTL_TG_FLAG_on_rr = 0, /* on round-robin busy list */ | |
115 | }; | |
116 | ||
117 | #define THROTL_TG_FNS(name) \ | |
118 | static inline void throtl_mark_tg_##name(struct throtl_grp *tg) \ | |
119 | { \ | |
120 | (tg)->flags |= (1 << THROTL_TG_FLAG_##name); \ | |
121 | } \ | |
122 | static inline void throtl_clear_tg_##name(struct throtl_grp *tg) \ | |
123 | { \ | |
124 | (tg)->flags &= ~(1 << THROTL_TG_FLAG_##name); \ | |
125 | } \ | |
126 | static inline int throtl_tg_##name(const struct throtl_grp *tg) \ | |
127 | { \ | |
128 | return ((tg)->flags & (1 << THROTL_TG_FLAG_##name)) != 0; \ | |
129 | } | |
130 | ||
131 | THROTL_TG_FNS(on_rr); | |
132 | ||
133 | #define throtl_log_tg(td, tg, fmt, args...) \ | |
134 | blk_add_trace_msg((td)->queue, "throtl %s " fmt, \ | |
0381411e | 135 | blkg_path(tg_to_blkg(tg)), ##args); \ |
e43473b7 VG |
136 | |
137 | #define throtl_log(td, fmt, args...) \ | |
138 | blk_add_trace_msg((td)->queue, "throtl " fmt, ##args) | |
139 | ||
d2f31a5f | 140 | static inline unsigned int total_nr_queued(struct throtl_data *td) |
e43473b7 | 141 | { |
d2f31a5f | 142 | return td->nr_queued[0] + td->nr_queued[1]; |
e43473b7 VG |
143 | } |
144 | ||
0381411e | 145 | static void throtl_init_blkio_group(struct blkio_group *blkg) |
a29a171e | 146 | { |
0381411e | 147 | struct throtl_grp *tg = blkg_to_tg(blkg); |
cd1604fa | 148 | |
a29a171e VG |
149 | RB_CLEAR_NODE(&tg->rb_node); |
150 | bio_list_init(&tg->bio_lists[0]); | |
151 | bio_list_init(&tg->bio_lists[1]); | |
152 | tg->limits_changed = false; | |
153 | ||
e56da7e2 TH |
154 | tg->bps[READ] = -1; |
155 | tg->bps[WRITE] = -1; | |
156 | tg->iops[READ] = -1; | |
157 | tg->iops[WRITE] = -1; | |
a29a171e VG |
158 | } |
159 | ||
cd1604fa TH |
160 | static void throtl_link_blkio_group(struct request_queue *q, |
161 | struct blkio_group *blkg) | |
269f5415 | 162 | { |
4eef3049 TH |
163 | list_add(&blkg->q_node[BLKIO_POLICY_THROTL], |
164 | &q->blkg_list[BLKIO_POLICY_THROTL]); | |
165 | q->nr_blkgs[BLKIO_POLICY_THROTL]++; | |
f469a7b4 VG |
166 | } |
167 | ||
168 | static struct | |
cd1604fa | 169 | throtl_grp *throtl_lookup_tg(struct throtl_data *td, struct blkio_cgroup *blkcg) |
e43473b7 | 170 | { |
be2c6b19 VG |
171 | /* |
172 | * This is the common case when there are no blkio cgroups. | |
cd1604fa TH |
173 | * Avoid lookup in this case |
174 | */ | |
be2c6b19 | 175 | if (blkcg == &blkio_root_cgroup) |
7a4dd281 | 176 | return td->root_tg; |
e43473b7 | 177 | |
0381411e | 178 | return blkg_to_tg(blkg_lookup(blkcg, td->queue, BLKIO_POLICY_THROTL)); |
e43473b7 VG |
179 | } |
180 | ||
cd1604fa TH |
181 | static struct throtl_grp *throtl_lookup_create_tg(struct throtl_data *td, |
182 | struct blkio_cgroup *blkcg) | |
e43473b7 | 183 | { |
f469a7b4 | 184 | struct request_queue *q = td->queue; |
cd1604fa | 185 | struct throtl_grp *tg = NULL; |
bc16a4f9 | 186 | |
f469a7b4 | 187 | /* |
cd1604fa TH |
188 | * This is the common case when there are no blkio cgroups. |
189 | * Avoid lookup in this case | |
f469a7b4 | 190 | */ |
cd1604fa TH |
191 | if (blkcg == &blkio_root_cgroup) { |
192 | tg = td->root_tg; | |
193 | } else { | |
194 | struct blkio_group *blkg; | |
f469a7b4 | 195 | |
cd1604fa | 196 | blkg = blkg_lookup_create(blkcg, q, BLKIO_POLICY_THROTL, false); |
f469a7b4 | 197 | |
cd1604fa TH |
198 | /* if %NULL and @q is alive, fall back to root_tg */ |
199 | if (!IS_ERR(blkg)) | |
0381411e | 200 | tg = blkg_to_tg(blkg); |
cd1604fa TH |
201 | else if (!blk_queue_dead(q)) |
202 | tg = td->root_tg; | |
f469a7b4 VG |
203 | } |
204 | ||
e43473b7 VG |
205 | return tg; |
206 | } | |
207 | ||
208 | static struct throtl_grp *throtl_rb_first(struct throtl_rb_root *root) | |
209 | { | |
210 | /* Service tree is empty */ | |
211 | if (!root->count) | |
212 | return NULL; | |
213 | ||
214 | if (!root->left) | |
215 | root->left = rb_first(&root->rb); | |
216 | ||
217 | if (root->left) | |
218 | return rb_entry_tg(root->left); | |
219 | ||
220 | return NULL; | |
221 | } | |
222 | ||
223 | static void rb_erase_init(struct rb_node *n, struct rb_root *root) | |
224 | { | |
225 | rb_erase(n, root); | |
226 | RB_CLEAR_NODE(n); | |
227 | } | |
228 | ||
229 | static void throtl_rb_erase(struct rb_node *n, struct throtl_rb_root *root) | |
230 | { | |
231 | if (root->left == n) | |
232 | root->left = NULL; | |
233 | rb_erase_init(n, &root->rb); | |
234 | --root->count; | |
235 | } | |
236 | ||
237 | static void update_min_dispatch_time(struct throtl_rb_root *st) | |
238 | { | |
239 | struct throtl_grp *tg; | |
240 | ||
241 | tg = throtl_rb_first(st); | |
242 | if (!tg) | |
243 | return; | |
244 | ||
245 | st->min_disptime = tg->disptime; | |
246 | } | |
247 | ||
248 | static void | |
249 | tg_service_tree_add(struct throtl_rb_root *st, struct throtl_grp *tg) | |
250 | { | |
251 | struct rb_node **node = &st->rb.rb_node; | |
252 | struct rb_node *parent = NULL; | |
253 | struct throtl_grp *__tg; | |
254 | unsigned long key = tg->disptime; | |
255 | int left = 1; | |
256 | ||
257 | while (*node != NULL) { | |
258 | parent = *node; | |
259 | __tg = rb_entry_tg(parent); | |
260 | ||
261 | if (time_before(key, __tg->disptime)) | |
262 | node = &parent->rb_left; | |
263 | else { | |
264 | node = &parent->rb_right; | |
265 | left = 0; | |
266 | } | |
267 | } | |
268 | ||
269 | if (left) | |
270 | st->left = &tg->rb_node; | |
271 | ||
272 | rb_link_node(&tg->rb_node, parent, node); | |
273 | rb_insert_color(&tg->rb_node, &st->rb); | |
274 | } | |
275 | ||
276 | static void __throtl_enqueue_tg(struct throtl_data *td, struct throtl_grp *tg) | |
277 | { | |
278 | struct throtl_rb_root *st = &td->tg_service_tree; | |
279 | ||
280 | tg_service_tree_add(st, tg); | |
281 | throtl_mark_tg_on_rr(tg); | |
282 | st->count++; | |
283 | } | |
284 | ||
285 | static void throtl_enqueue_tg(struct throtl_data *td, struct throtl_grp *tg) | |
286 | { | |
287 | if (!throtl_tg_on_rr(tg)) | |
288 | __throtl_enqueue_tg(td, tg); | |
289 | } | |
290 | ||
291 | static void __throtl_dequeue_tg(struct throtl_data *td, struct throtl_grp *tg) | |
292 | { | |
293 | throtl_rb_erase(&tg->rb_node, &td->tg_service_tree); | |
294 | throtl_clear_tg_on_rr(tg); | |
295 | } | |
296 | ||
297 | static void throtl_dequeue_tg(struct throtl_data *td, struct throtl_grp *tg) | |
298 | { | |
299 | if (throtl_tg_on_rr(tg)) | |
300 | __throtl_dequeue_tg(td, tg); | |
301 | } | |
302 | ||
303 | static void throtl_schedule_next_dispatch(struct throtl_data *td) | |
304 | { | |
305 | struct throtl_rb_root *st = &td->tg_service_tree; | |
306 | ||
307 | /* | |
308 | * If there are more bios pending, schedule more work. | |
309 | */ | |
310 | if (!total_nr_queued(td)) | |
311 | return; | |
312 | ||
313 | BUG_ON(!st->count); | |
314 | ||
315 | update_min_dispatch_time(st); | |
316 | ||
317 | if (time_before_eq(st->min_disptime, jiffies)) | |
450adcbe | 318 | throtl_schedule_delayed_work(td, 0); |
e43473b7 | 319 | else |
450adcbe | 320 | throtl_schedule_delayed_work(td, (st->min_disptime - jiffies)); |
e43473b7 VG |
321 | } |
322 | ||
323 | static inline void | |
324 | throtl_start_new_slice(struct throtl_data *td, struct throtl_grp *tg, bool rw) | |
325 | { | |
326 | tg->bytes_disp[rw] = 0; | |
8e89d13f | 327 | tg->io_disp[rw] = 0; |
e43473b7 VG |
328 | tg->slice_start[rw] = jiffies; |
329 | tg->slice_end[rw] = jiffies + throtl_slice; | |
330 | throtl_log_tg(td, tg, "[%c] new slice start=%lu end=%lu jiffies=%lu", | |
331 | rw == READ ? 'R' : 'W', tg->slice_start[rw], | |
332 | tg->slice_end[rw], jiffies); | |
333 | } | |
334 | ||
d1ae8ffd VG |
335 | static inline void throtl_set_slice_end(struct throtl_data *td, |
336 | struct throtl_grp *tg, bool rw, unsigned long jiffy_end) | |
337 | { | |
338 | tg->slice_end[rw] = roundup(jiffy_end, throtl_slice); | |
339 | } | |
340 | ||
e43473b7 VG |
341 | static inline void throtl_extend_slice(struct throtl_data *td, |
342 | struct throtl_grp *tg, bool rw, unsigned long jiffy_end) | |
343 | { | |
344 | tg->slice_end[rw] = roundup(jiffy_end, throtl_slice); | |
345 | throtl_log_tg(td, tg, "[%c] extend slice start=%lu end=%lu jiffies=%lu", | |
346 | rw == READ ? 'R' : 'W', tg->slice_start[rw], | |
347 | tg->slice_end[rw], jiffies); | |
348 | } | |
349 | ||
350 | /* Determine if previously allocated or extended slice is complete or not */ | |
351 | static bool | |
352 | throtl_slice_used(struct throtl_data *td, struct throtl_grp *tg, bool rw) | |
353 | { | |
354 | if (time_in_range(jiffies, tg->slice_start[rw], tg->slice_end[rw])) | |
355 | return 0; | |
356 | ||
357 | return 1; | |
358 | } | |
359 | ||
360 | /* Trim the used slices and adjust slice start accordingly */ | |
361 | static inline void | |
362 | throtl_trim_slice(struct throtl_data *td, struct throtl_grp *tg, bool rw) | |
363 | { | |
3aad5d3e VG |
364 | unsigned long nr_slices, time_elapsed, io_trim; |
365 | u64 bytes_trim, tmp; | |
e43473b7 VG |
366 | |
367 | BUG_ON(time_before(tg->slice_end[rw], tg->slice_start[rw])); | |
368 | ||
369 | /* | |
370 | * If bps are unlimited (-1), then time slice don't get | |
371 | * renewed. Don't try to trim the slice if slice is used. A new | |
372 | * slice will start when appropriate. | |
373 | */ | |
374 | if (throtl_slice_used(td, tg, rw)) | |
375 | return; | |
376 | ||
d1ae8ffd VG |
377 | /* |
378 | * A bio has been dispatched. Also adjust slice_end. It might happen | |
379 | * that initially cgroup limit was very low resulting in high | |
380 | * slice_end, but later limit was bumped up and bio was dispached | |
381 | * sooner, then we need to reduce slice_end. A high bogus slice_end | |
382 | * is bad because it does not allow new slice to start. | |
383 | */ | |
384 | ||
385 | throtl_set_slice_end(td, tg, rw, jiffies + throtl_slice); | |
386 | ||
e43473b7 VG |
387 | time_elapsed = jiffies - tg->slice_start[rw]; |
388 | ||
389 | nr_slices = time_elapsed / throtl_slice; | |
390 | ||
391 | if (!nr_slices) | |
392 | return; | |
3aad5d3e VG |
393 | tmp = tg->bps[rw] * throtl_slice * nr_slices; |
394 | do_div(tmp, HZ); | |
395 | bytes_trim = tmp; | |
e43473b7 | 396 | |
8e89d13f | 397 | io_trim = (tg->iops[rw] * throtl_slice * nr_slices)/HZ; |
e43473b7 | 398 | |
8e89d13f | 399 | if (!bytes_trim && !io_trim) |
e43473b7 VG |
400 | return; |
401 | ||
402 | if (tg->bytes_disp[rw] >= bytes_trim) | |
403 | tg->bytes_disp[rw] -= bytes_trim; | |
404 | else | |
405 | tg->bytes_disp[rw] = 0; | |
406 | ||
8e89d13f VG |
407 | if (tg->io_disp[rw] >= io_trim) |
408 | tg->io_disp[rw] -= io_trim; | |
409 | else | |
410 | tg->io_disp[rw] = 0; | |
411 | ||
e43473b7 VG |
412 | tg->slice_start[rw] += nr_slices * throtl_slice; |
413 | ||
3aad5d3e | 414 | throtl_log_tg(td, tg, "[%c] trim slice nr=%lu bytes=%llu io=%lu" |
e43473b7 | 415 | " start=%lu end=%lu jiffies=%lu", |
8e89d13f | 416 | rw == READ ? 'R' : 'W', nr_slices, bytes_trim, io_trim, |
e43473b7 VG |
417 | tg->slice_start[rw], tg->slice_end[rw], jiffies); |
418 | } | |
419 | ||
8e89d13f VG |
420 | static bool tg_with_in_iops_limit(struct throtl_data *td, struct throtl_grp *tg, |
421 | struct bio *bio, unsigned long *wait) | |
e43473b7 VG |
422 | { |
423 | bool rw = bio_data_dir(bio); | |
8e89d13f | 424 | unsigned int io_allowed; |
e43473b7 | 425 | unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd; |
c49c06e4 | 426 | u64 tmp; |
e43473b7 | 427 | |
8e89d13f | 428 | jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw]; |
e43473b7 | 429 | |
8e89d13f VG |
430 | /* Slice has just started. Consider one slice interval */ |
431 | if (!jiffy_elapsed) | |
432 | jiffy_elapsed_rnd = throtl_slice; | |
433 | ||
434 | jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice); | |
435 | ||
c49c06e4 VG |
436 | /* |
437 | * jiffy_elapsed_rnd should not be a big value as minimum iops can be | |
438 | * 1 then at max jiffy elapsed should be equivalent of 1 second as we | |
439 | * will allow dispatch after 1 second and after that slice should | |
440 | * have been trimmed. | |
441 | */ | |
442 | ||
443 | tmp = (u64)tg->iops[rw] * jiffy_elapsed_rnd; | |
444 | do_div(tmp, HZ); | |
445 | ||
446 | if (tmp > UINT_MAX) | |
447 | io_allowed = UINT_MAX; | |
448 | else | |
449 | io_allowed = tmp; | |
8e89d13f VG |
450 | |
451 | if (tg->io_disp[rw] + 1 <= io_allowed) { | |
e43473b7 VG |
452 | if (wait) |
453 | *wait = 0; | |
454 | return 1; | |
455 | } | |
456 | ||
8e89d13f VG |
457 | /* Calc approx time to dispatch */ |
458 | jiffy_wait = ((tg->io_disp[rw] + 1) * HZ)/tg->iops[rw] + 1; | |
459 | ||
460 | if (jiffy_wait > jiffy_elapsed) | |
461 | jiffy_wait = jiffy_wait - jiffy_elapsed; | |
462 | else | |
463 | jiffy_wait = 1; | |
464 | ||
465 | if (wait) | |
466 | *wait = jiffy_wait; | |
467 | return 0; | |
468 | } | |
469 | ||
470 | static bool tg_with_in_bps_limit(struct throtl_data *td, struct throtl_grp *tg, | |
471 | struct bio *bio, unsigned long *wait) | |
472 | { | |
473 | bool rw = bio_data_dir(bio); | |
3aad5d3e | 474 | u64 bytes_allowed, extra_bytes, tmp; |
8e89d13f | 475 | unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd; |
e43473b7 VG |
476 | |
477 | jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw]; | |
478 | ||
479 | /* Slice has just started. Consider one slice interval */ | |
480 | if (!jiffy_elapsed) | |
481 | jiffy_elapsed_rnd = throtl_slice; | |
482 | ||
483 | jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice); | |
484 | ||
5e901a2b VG |
485 | tmp = tg->bps[rw] * jiffy_elapsed_rnd; |
486 | do_div(tmp, HZ); | |
3aad5d3e | 487 | bytes_allowed = tmp; |
e43473b7 VG |
488 | |
489 | if (tg->bytes_disp[rw] + bio->bi_size <= bytes_allowed) { | |
490 | if (wait) | |
491 | *wait = 0; | |
492 | return 1; | |
493 | } | |
494 | ||
495 | /* Calc approx time to dispatch */ | |
496 | extra_bytes = tg->bytes_disp[rw] + bio->bi_size - bytes_allowed; | |
497 | jiffy_wait = div64_u64(extra_bytes * HZ, tg->bps[rw]); | |
498 | ||
499 | if (!jiffy_wait) | |
500 | jiffy_wait = 1; | |
501 | ||
502 | /* | |
503 | * This wait time is without taking into consideration the rounding | |
504 | * up we did. Add that time also. | |
505 | */ | |
506 | jiffy_wait = jiffy_wait + (jiffy_elapsed_rnd - jiffy_elapsed); | |
e43473b7 VG |
507 | if (wait) |
508 | *wait = jiffy_wait; | |
8e89d13f VG |
509 | return 0; |
510 | } | |
511 | ||
af75cd3c VG |
512 | static bool tg_no_rule_group(struct throtl_grp *tg, bool rw) { |
513 | if (tg->bps[rw] == -1 && tg->iops[rw] == -1) | |
514 | return 1; | |
515 | return 0; | |
516 | } | |
517 | ||
8e89d13f VG |
518 | /* |
519 | * Returns whether one can dispatch a bio or not. Also returns approx number | |
520 | * of jiffies to wait before this bio is with-in IO rate and can be dispatched | |
521 | */ | |
522 | static bool tg_may_dispatch(struct throtl_data *td, struct throtl_grp *tg, | |
523 | struct bio *bio, unsigned long *wait) | |
524 | { | |
525 | bool rw = bio_data_dir(bio); | |
526 | unsigned long bps_wait = 0, iops_wait = 0, max_wait = 0; | |
527 | ||
528 | /* | |
529 | * Currently whole state machine of group depends on first bio | |
530 | * queued in the group bio list. So one should not be calling | |
531 | * this function with a different bio if there are other bios | |
532 | * queued. | |
533 | */ | |
534 | BUG_ON(tg->nr_queued[rw] && bio != bio_list_peek(&tg->bio_lists[rw])); | |
e43473b7 | 535 | |
8e89d13f VG |
536 | /* If tg->bps = -1, then BW is unlimited */ |
537 | if (tg->bps[rw] == -1 && tg->iops[rw] == -1) { | |
538 | if (wait) | |
539 | *wait = 0; | |
540 | return 1; | |
541 | } | |
542 | ||
543 | /* | |
544 | * If previous slice expired, start a new one otherwise renew/extend | |
545 | * existing slice to make sure it is at least throtl_slice interval | |
546 | * long since now. | |
547 | */ | |
548 | if (throtl_slice_used(td, tg, rw)) | |
549 | throtl_start_new_slice(td, tg, rw); | |
550 | else { | |
551 | if (time_before(tg->slice_end[rw], jiffies + throtl_slice)) | |
552 | throtl_extend_slice(td, tg, rw, jiffies + throtl_slice); | |
553 | } | |
554 | ||
555 | if (tg_with_in_bps_limit(td, tg, bio, &bps_wait) | |
556 | && tg_with_in_iops_limit(td, tg, bio, &iops_wait)) { | |
557 | if (wait) | |
558 | *wait = 0; | |
559 | return 1; | |
560 | } | |
561 | ||
562 | max_wait = max(bps_wait, iops_wait); | |
563 | ||
564 | if (wait) | |
565 | *wait = max_wait; | |
566 | ||
567 | if (time_before(tg->slice_end[rw], jiffies + max_wait)) | |
568 | throtl_extend_slice(td, tg, rw, jiffies + max_wait); | |
e43473b7 VG |
569 | |
570 | return 0; | |
571 | } | |
572 | ||
573 | static void throtl_charge_bio(struct throtl_grp *tg, struct bio *bio) | |
574 | { | |
575 | bool rw = bio_data_dir(bio); | |
e5a94f56 | 576 | bool sync = rw_is_sync(bio->bi_rw); |
e43473b7 VG |
577 | |
578 | /* Charge the bio to the group */ | |
579 | tg->bytes_disp[rw] += bio->bi_size; | |
8e89d13f | 580 | tg->io_disp[rw]++; |
e43473b7 | 581 | |
c1768268 TH |
582 | blkiocg_update_dispatch_stats(tg_to_blkg(tg), &blkio_policy_throtl, |
583 | bio->bi_size, rw, sync); | |
e43473b7 VG |
584 | } |
585 | ||
586 | static void throtl_add_bio_tg(struct throtl_data *td, struct throtl_grp *tg, | |
587 | struct bio *bio) | |
588 | { | |
589 | bool rw = bio_data_dir(bio); | |
590 | ||
591 | bio_list_add(&tg->bio_lists[rw], bio); | |
592 | /* Take a bio reference on tg */ | |
1adaf3dd | 593 | blkg_get(tg_to_blkg(tg)); |
e43473b7 VG |
594 | tg->nr_queued[rw]++; |
595 | td->nr_queued[rw]++; | |
596 | throtl_enqueue_tg(td, tg); | |
597 | } | |
598 | ||
599 | static void tg_update_disptime(struct throtl_data *td, struct throtl_grp *tg) | |
600 | { | |
601 | unsigned long read_wait = -1, write_wait = -1, min_wait = -1, disptime; | |
602 | struct bio *bio; | |
603 | ||
604 | if ((bio = bio_list_peek(&tg->bio_lists[READ]))) | |
605 | tg_may_dispatch(td, tg, bio, &read_wait); | |
606 | ||
607 | if ((bio = bio_list_peek(&tg->bio_lists[WRITE]))) | |
608 | tg_may_dispatch(td, tg, bio, &write_wait); | |
609 | ||
610 | min_wait = min(read_wait, write_wait); | |
611 | disptime = jiffies + min_wait; | |
612 | ||
e43473b7 VG |
613 | /* Update dispatch time */ |
614 | throtl_dequeue_tg(td, tg); | |
615 | tg->disptime = disptime; | |
616 | throtl_enqueue_tg(td, tg); | |
617 | } | |
618 | ||
619 | static void tg_dispatch_one_bio(struct throtl_data *td, struct throtl_grp *tg, | |
620 | bool rw, struct bio_list *bl) | |
621 | { | |
622 | struct bio *bio; | |
623 | ||
624 | bio = bio_list_pop(&tg->bio_lists[rw]); | |
625 | tg->nr_queued[rw]--; | |
1adaf3dd TH |
626 | /* Drop bio reference on blkg */ |
627 | blkg_put(tg_to_blkg(tg)); | |
e43473b7 VG |
628 | |
629 | BUG_ON(td->nr_queued[rw] <= 0); | |
630 | td->nr_queued[rw]--; | |
631 | ||
632 | throtl_charge_bio(tg, bio); | |
633 | bio_list_add(bl, bio); | |
634 | bio->bi_rw |= REQ_THROTTLED; | |
635 | ||
636 | throtl_trim_slice(td, tg, rw); | |
637 | } | |
638 | ||
639 | static int throtl_dispatch_tg(struct throtl_data *td, struct throtl_grp *tg, | |
640 | struct bio_list *bl) | |
641 | { | |
642 | unsigned int nr_reads = 0, nr_writes = 0; | |
643 | unsigned int max_nr_reads = throtl_grp_quantum*3/4; | |
c2f6805d | 644 | unsigned int max_nr_writes = throtl_grp_quantum - max_nr_reads; |
e43473b7 VG |
645 | struct bio *bio; |
646 | ||
647 | /* Try to dispatch 75% READS and 25% WRITES */ | |
648 | ||
649 | while ((bio = bio_list_peek(&tg->bio_lists[READ])) | |
650 | && tg_may_dispatch(td, tg, bio, NULL)) { | |
651 | ||
652 | tg_dispatch_one_bio(td, tg, bio_data_dir(bio), bl); | |
653 | nr_reads++; | |
654 | ||
655 | if (nr_reads >= max_nr_reads) | |
656 | break; | |
657 | } | |
658 | ||
659 | while ((bio = bio_list_peek(&tg->bio_lists[WRITE])) | |
660 | && tg_may_dispatch(td, tg, bio, NULL)) { | |
661 | ||
662 | tg_dispatch_one_bio(td, tg, bio_data_dir(bio), bl); | |
663 | nr_writes++; | |
664 | ||
665 | if (nr_writes >= max_nr_writes) | |
666 | break; | |
667 | } | |
668 | ||
669 | return nr_reads + nr_writes; | |
670 | } | |
671 | ||
672 | static int throtl_select_dispatch(struct throtl_data *td, struct bio_list *bl) | |
673 | { | |
674 | unsigned int nr_disp = 0; | |
675 | struct throtl_grp *tg; | |
676 | struct throtl_rb_root *st = &td->tg_service_tree; | |
677 | ||
678 | while (1) { | |
679 | tg = throtl_rb_first(st); | |
680 | ||
681 | if (!tg) | |
682 | break; | |
683 | ||
684 | if (time_before(jiffies, tg->disptime)) | |
685 | break; | |
686 | ||
687 | throtl_dequeue_tg(td, tg); | |
688 | ||
689 | nr_disp += throtl_dispatch_tg(td, tg, bl); | |
690 | ||
691 | if (tg->nr_queued[0] || tg->nr_queued[1]) { | |
692 | tg_update_disptime(td, tg); | |
693 | throtl_enqueue_tg(td, tg); | |
694 | } | |
695 | ||
696 | if (nr_disp >= throtl_quantum) | |
697 | break; | |
698 | } | |
699 | ||
700 | return nr_disp; | |
701 | } | |
702 | ||
fe071437 VG |
703 | static void throtl_process_limit_change(struct throtl_data *td) |
704 | { | |
4eef3049 TH |
705 | struct request_queue *q = td->queue; |
706 | struct blkio_group *blkg, *n; | |
fe071437 | 707 | |
de701c74 | 708 | if (!td->limits_changed) |
fe071437 VG |
709 | return; |
710 | ||
de701c74 | 711 | xchg(&td->limits_changed, false); |
fe071437 | 712 | |
de701c74 | 713 | throtl_log(td, "limits changed"); |
fe071437 | 714 | |
4eef3049 TH |
715 | list_for_each_entry_safe(blkg, n, &q->blkg_list[BLKIO_POLICY_THROTL], |
716 | q_node[BLKIO_POLICY_THROTL]) { | |
717 | struct throtl_grp *tg = blkg_to_tg(blkg); | |
718 | ||
de701c74 VG |
719 | if (!tg->limits_changed) |
720 | continue; | |
721 | ||
722 | if (!xchg(&tg->limits_changed, false)) | |
723 | continue; | |
724 | ||
725 | throtl_log_tg(td, tg, "limit change rbps=%llu wbps=%llu" | |
726 | " riops=%u wiops=%u", tg->bps[READ], tg->bps[WRITE], | |
727 | tg->iops[READ], tg->iops[WRITE]); | |
728 | ||
04521db0 VG |
729 | /* |
730 | * Restart the slices for both READ and WRITES. It | |
731 | * might happen that a group's limit are dropped | |
732 | * suddenly and we don't want to account recently | |
733 | * dispatched IO with new low rate | |
734 | */ | |
735 | throtl_start_new_slice(td, tg, 0); | |
736 | throtl_start_new_slice(td, tg, 1); | |
737 | ||
de701c74 | 738 | if (throtl_tg_on_rr(tg)) |
fe071437 | 739 | tg_update_disptime(td, tg); |
fe071437 | 740 | } |
fe071437 VG |
741 | } |
742 | ||
e43473b7 VG |
743 | /* Dispatch throttled bios. Should be called without queue lock held. */ |
744 | static int throtl_dispatch(struct request_queue *q) | |
745 | { | |
746 | struct throtl_data *td = q->td; | |
747 | unsigned int nr_disp = 0; | |
748 | struct bio_list bio_list_on_stack; | |
749 | struct bio *bio; | |
69d60eb9 | 750 | struct blk_plug plug; |
e43473b7 VG |
751 | |
752 | spin_lock_irq(q->queue_lock); | |
753 | ||
fe071437 VG |
754 | throtl_process_limit_change(td); |
755 | ||
e43473b7 VG |
756 | if (!total_nr_queued(td)) |
757 | goto out; | |
758 | ||
759 | bio_list_init(&bio_list_on_stack); | |
760 | ||
d2f31a5f | 761 | throtl_log(td, "dispatch nr_queued=%u read=%u write=%u", |
e43473b7 VG |
762 | total_nr_queued(td), td->nr_queued[READ], |
763 | td->nr_queued[WRITE]); | |
764 | ||
765 | nr_disp = throtl_select_dispatch(td, &bio_list_on_stack); | |
766 | ||
767 | if (nr_disp) | |
768 | throtl_log(td, "bios disp=%u", nr_disp); | |
769 | ||
770 | throtl_schedule_next_dispatch(td); | |
771 | out: | |
772 | spin_unlock_irq(q->queue_lock); | |
773 | ||
774 | /* | |
775 | * If we dispatched some requests, unplug the queue to make sure | |
776 | * immediate dispatch | |
777 | */ | |
778 | if (nr_disp) { | |
69d60eb9 | 779 | blk_start_plug(&plug); |
e43473b7 VG |
780 | while((bio = bio_list_pop(&bio_list_on_stack))) |
781 | generic_make_request(bio); | |
69d60eb9 | 782 | blk_finish_plug(&plug); |
e43473b7 VG |
783 | } |
784 | return nr_disp; | |
785 | } | |
786 | ||
787 | void blk_throtl_work(struct work_struct *work) | |
788 | { | |
789 | struct throtl_data *td = container_of(work, struct throtl_data, | |
790 | throtl_work.work); | |
791 | struct request_queue *q = td->queue; | |
792 | ||
793 | throtl_dispatch(q); | |
794 | } | |
795 | ||
796 | /* Call with queue lock held */ | |
450adcbe VG |
797 | static void |
798 | throtl_schedule_delayed_work(struct throtl_data *td, unsigned long delay) | |
e43473b7 VG |
799 | { |
800 | ||
e43473b7 VG |
801 | struct delayed_work *dwork = &td->throtl_work; |
802 | ||
04521db0 | 803 | /* schedule work if limits changed even if no bio is queued */ |
d2f31a5f | 804 | if (total_nr_queued(td) || td->limits_changed) { |
e43473b7 VG |
805 | /* |
806 | * We might have a work scheduled to be executed in future. | |
807 | * Cancel that and schedule a new one. | |
808 | */ | |
809 | __cancel_delayed_work(dwork); | |
450adcbe | 810 | queue_delayed_work(kthrotld_workqueue, dwork, delay); |
e43473b7 VG |
811 | throtl_log(td, "schedule work. delay=%lu jiffies=%lu", |
812 | delay, jiffies); | |
813 | } | |
814 | } | |
e43473b7 VG |
815 | |
816 | static void | |
817 | throtl_destroy_tg(struct throtl_data *td, struct throtl_grp *tg) | |
818 | { | |
4eef3049 TH |
819 | struct blkio_group *blkg = tg_to_blkg(tg); |
820 | ||
e43473b7 | 821 | /* Something wrong if we are trying to remove same group twice */ |
4eef3049 | 822 | WARN_ON_ONCE(list_empty(&blkg->q_node[BLKIO_POLICY_THROTL])); |
e43473b7 | 823 | |
4eef3049 | 824 | list_del_init(&blkg->q_node[BLKIO_POLICY_THROTL]); |
e43473b7 VG |
825 | |
826 | /* | |
827 | * Put the reference taken at the time of creation so that when all | |
828 | * queues are gone, group can be destroyed. | |
829 | */ | |
1adaf3dd | 830 | blkg_put(tg_to_blkg(tg)); |
4eef3049 | 831 | td->queue->nr_blkgs[BLKIO_POLICY_THROTL]--; |
e43473b7 VG |
832 | } |
833 | ||
72e06c25 | 834 | static bool throtl_release_tgs(struct throtl_data *td, bool release_root) |
e43473b7 | 835 | { |
4eef3049 TH |
836 | struct request_queue *q = td->queue; |
837 | struct blkio_group *blkg, *n; | |
72e06c25 | 838 | bool empty = true; |
e43473b7 | 839 | |
4eef3049 TH |
840 | list_for_each_entry_safe(blkg, n, &q->blkg_list[BLKIO_POLICY_THROTL], |
841 | q_node[BLKIO_POLICY_THROTL]) { | |
842 | struct throtl_grp *tg = blkg_to_tg(blkg); | |
843 | ||
72e06c25 TH |
844 | /* skip root? */ |
845 | if (!release_root && tg == td->root_tg) | |
846 | continue; | |
847 | ||
e43473b7 VG |
848 | /* |
849 | * If cgroup removal path got to blk_group first and removed | |
850 | * it from cgroup list, then it will take care of destroying | |
851 | * cfqg also. | |
852 | */ | |
4eef3049 | 853 | if (!blkiocg_del_blkio_group(blkg)) |
e43473b7 | 854 | throtl_destroy_tg(td, tg); |
72e06c25 TH |
855 | else |
856 | empty = false; | |
e43473b7 | 857 | } |
72e06c25 | 858 | return empty; |
e43473b7 VG |
859 | } |
860 | ||
e43473b7 VG |
861 | /* |
862 | * Blk cgroup controller notification saying that blkio_group object is being | |
863 | * delinked as associated cgroup object is going away. That also means that | |
864 | * no new IO will come in this group. So get rid of this group as soon as | |
865 | * any pending IO in the group is finished. | |
866 | * | |
ca32aefc TH |
867 | * This function is called under rcu_read_lock(). @q is the rcu protected |
868 | * pointer. That means @q is a valid request_queue pointer as long as we | |
869 | * are rcu read lock. | |
e43473b7 | 870 | * |
ca32aefc | 871 | * @q was fetched from blkio_group under blkio_cgroup->lock. That means |
e43473b7 VG |
872 | * it should not be NULL as even if queue was going away, cgroup deltion |
873 | * path got to it first. | |
874 | */ | |
ca32aefc TH |
875 | void throtl_unlink_blkio_group(struct request_queue *q, |
876 | struct blkio_group *blkg) | |
e43473b7 VG |
877 | { |
878 | unsigned long flags; | |
e43473b7 | 879 | |
ca32aefc | 880 | spin_lock_irqsave(q->queue_lock, flags); |
0381411e | 881 | throtl_destroy_tg(q->td, blkg_to_tg(blkg)); |
ca32aefc | 882 | spin_unlock_irqrestore(q->queue_lock, flags); |
e43473b7 VG |
883 | } |
884 | ||
72e06c25 TH |
885 | static bool throtl_clear_queue(struct request_queue *q) |
886 | { | |
887 | lockdep_assert_held(q->queue_lock); | |
888 | ||
889 | /* | |
890 | * Clear tgs but leave the root one alone. This is necessary | |
891 | * because root_tg is expected to be persistent and safe because | |
892 | * blk-throtl can never be disabled while @q is alive. This is a | |
893 | * kludge to prepare for unified blkg. This whole function will be | |
894 | * removed soon. | |
895 | */ | |
896 | return throtl_release_tgs(q->td, false); | |
897 | } | |
898 | ||
de701c74 VG |
899 | static void throtl_update_blkio_group_common(struct throtl_data *td, |
900 | struct throtl_grp *tg) | |
901 | { | |
902 | xchg(&tg->limits_changed, true); | |
903 | xchg(&td->limits_changed, true); | |
904 | /* Schedule a work now to process the limit change */ | |
905 | throtl_schedule_delayed_work(td, 0); | |
906 | } | |
907 | ||
fe071437 | 908 | /* |
ca32aefc | 909 | * For all update functions, @q should be a valid pointer because these |
fe071437 | 910 | * update functions are called under blkcg_lock, that means, blkg is |
ca32aefc | 911 | * valid and in turn @q is valid. queue exit path can not race because |
fe071437 VG |
912 | * of blkcg_lock |
913 | * | |
914 | * Can not take queue lock in update functions as queue lock under blkcg_lock | |
915 | * is not allowed. Under other paths we take blkcg_lock under queue_lock. | |
916 | */ | |
ca32aefc | 917 | static void throtl_update_blkio_group_read_bps(struct request_queue *q, |
fe071437 | 918 | struct blkio_group *blkg, u64 read_bps) |
e43473b7 | 919 | { |
0381411e | 920 | struct throtl_grp *tg = blkg_to_tg(blkg); |
fe071437 | 921 | |
de701c74 | 922 | tg->bps[READ] = read_bps; |
ca32aefc | 923 | throtl_update_blkio_group_common(q->td, tg); |
e43473b7 VG |
924 | } |
925 | ||
ca32aefc | 926 | static void throtl_update_blkio_group_write_bps(struct request_queue *q, |
fe071437 | 927 | struct blkio_group *blkg, u64 write_bps) |
e43473b7 | 928 | { |
0381411e | 929 | struct throtl_grp *tg = blkg_to_tg(blkg); |
fe071437 | 930 | |
de701c74 | 931 | tg->bps[WRITE] = write_bps; |
ca32aefc | 932 | throtl_update_blkio_group_common(q->td, tg); |
e43473b7 VG |
933 | } |
934 | ||
ca32aefc | 935 | static void throtl_update_blkio_group_read_iops(struct request_queue *q, |
fe071437 | 936 | struct blkio_group *blkg, unsigned int read_iops) |
8e89d13f | 937 | { |
0381411e | 938 | struct throtl_grp *tg = blkg_to_tg(blkg); |
fe071437 | 939 | |
de701c74 | 940 | tg->iops[READ] = read_iops; |
ca32aefc | 941 | throtl_update_blkio_group_common(q->td, tg); |
8e89d13f VG |
942 | } |
943 | ||
ca32aefc | 944 | static void throtl_update_blkio_group_write_iops(struct request_queue *q, |
fe071437 | 945 | struct blkio_group *blkg, unsigned int write_iops) |
8e89d13f | 946 | { |
0381411e | 947 | struct throtl_grp *tg = blkg_to_tg(blkg); |
fe071437 | 948 | |
de701c74 | 949 | tg->iops[WRITE] = write_iops; |
ca32aefc | 950 | throtl_update_blkio_group_common(q->td, tg); |
8e89d13f VG |
951 | } |
952 | ||
da527770 | 953 | static void throtl_shutdown_wq(struct request_queue *q) |
e43473b7 VG |
954 | { |
955 | struct throtl_data *td = q->td; | |
956 | ||
957 | cancel_delayed_work_sync(&td->throtl_work); | |
958 | } | |
959 | ||
960 | static struct blkio_policy_type blkio_policy_throtl = { | |
961 | .ops = { | |
0381411e | 962 | .blkio_init_group_fn = throtl_init_blkio_group, |
cd1604fa | 963 | .blkio_link_group_fn = throtl_link_blkio_group, |
e43473b7 | 964 | .blkio_unlink_group_fn = throtl_unlink_blkio_group, |
72e06c25 | 965 | .blkio_clear_queue_fn = throtl_clear_queue, |
e43473b7 VG |
966 | .blkio_update_group_read_bps_fn = |
967 | throtl_update_blkio_group_read_bps, | |
968 | .blkio_update_group_write_bps_fn = | |
969 | throtl_update_blkio_group_write_bps, | |
8e89d13f VG |
970 | .blkio_update_group_read_iops_fn = |
971 | throtl_update_blkio_group_read_iops, | |
972 | .blkio_update_group_write_iops_fn = | |
973 | throtl_update_blkio_group_write_iops, | |
e43473b7 | 974 | }, |
8e89d13f | 975 | .plid = BLKIO_POLICY_THROTL, |
0381411e | 976 | .pdata_size = sizeof(struct throtl_grp), |
e43473b7 VG |
977 | }; |
978 | ||
bc16a4f9 | 979 | bool blk_throtl_bio(struct request_queue *q, struct bio *bio) |
e43473b7 VG |
980 | { |
981 | struct throtl_data *td = q->td; | |
982 | struct throtl_grp *tg; | |
e43473b7 | 983 | bool rw = bio_data_dir(bio), update_disptime = true; |
af75cd3c | 984 | struct blkio_cgroup *blkcg; |
bc16a4f9 | 985 | bool throttled = false; |
e43473b7 VG |
986 | |
987 | if (bio->bi_rw & REQ_THROTTLED) { | |
988 | bio->bi_rw &= ~REQ_THROTTLED; | |
bc16a4f9 | 989 | goto out; |
e43473b7 VG |
990 | } |
991 | ||
af75cd3c VG |
992 | /* |
993 | * A throtl_grp pointer retrieved under rcu can be used to access | |
994 | * basic fields like stats and io rates. If a group has no rules, | |
995 | * just update the dispatch stats in lockless manner and return. | |
996 | */ | |
af75cd3c VG |
997 | rcu_read_lock(); |
998 | blkcg = task_blkio_cgroup(current); | |
cd1604fa | 999 | tg = throtl_lookup_tg(td, blkcg); |
af75cd3c | 1000 | if (tg) { |
af75cd3c | 1001 | if (tg_no_rule_group(tg, rw)) { |
0381411e | 1002 | blkiocg_update_dispatch_stats(tg_to_blkg(tg), |
c1768268 | 1003 | &blkio_policy_throtl, |
0381411e TH |
1004 | bio->bi_size, rw, |
1005 | rw_is_sync(bio->bi_rw)); | |
2a7f1244 | 1006 | goto out_unlock_rcu; |
af75cd3c VG |
1007 | } |
1008 | } | |
af75cd3c VG |
1009 | |
1010 | /* | |
1011 | * Either group has not been allocated yet or it is not an unlimited | |
1012 | * IO group | |
1013 | */ | |
e43473b7 | 1014 | spin_lock_irq(q->queue_lock); |
cd1604fa | 1015 | tg = throtl_lookup_create_tg(td, blkcg); |
bc16a4f9 TH |
1016 | if (unlikely(!tg)) |
1017 | goto out_unlock; | |
f469a7b4 | 1018 | |
e43473b7 VG |
1019 | if (tg->nr_queued[rw]) { |
1020 | /* | |
1021 | * There is already another bio queued in same dir. No | |
1022 | * need to update dispatch time. | |
1023 | */ | |
231d704b | 1024 | update_disptime = false; |
e43473b7 | 1025 | goto queue_bio; |
de701c74 | 1026 | |
e43473b7 VG |
1027 | } |
1028 | ||
1029 | /* Bio is with-in rate limit of group */ | |
1030 | if (tg_may_dispatch(td, tg, bio, NULL)) { | |
1031 | throtl_charge_bio(tg, bio); | |
04521db0 VG |
1032 | |
1033 | /* | |
1034 | * We need to trim slice even when bios are not being queued | |
1035 | * otherwise it might happen that a bio is not queued for | |
1036 | * a long time and slice keeps on extending and trim is not | |
1037 | * called for a long time. Now if limits are reduced suddenly | |
1038 | * we take into account all the IO dispatched so far at new | |
1039 | * low rate and * newly queued IO gets a really long dispatch | |
1040 | * time. | |
1041 | * | |
1042 | * So keep on trimming slice even if bio is not queued. | |
1043 | */ | |
1044 | throtl_trim_slice(td, tg, rw); | |
bc16a4f9 | 1045 | goto out_unlock; |
e43473b7 VG |
1046 | } |
1047 | ||
1048 | queue_bio: | |
fd16d263 | 1049 | throtl_log_tg(td, tg, "[%c] bio. bdisp=%llu sz=%u bps=%llu" |
8e89d13f VG |
1050 | " iodisp=%u iops=%u queued=%d/%d", |
1051 | rw == READ ? 'R' : 'W', | |
e43473b7 | 1052 | tg->bytes_disp[rw], bio->bi_size, tg->bps[rw], |
8e89d13f | 1053 | tg->io_disp[rw], tg->iops[rw], |
e43473b7 VG |
1054 | tg->nr_queued[READ], tg->nr_queued[WRITE]); |
1055 | ||
1056 | throtl_add_bio_tg(q->td, tg, bio); | |
bc16a4f9 | 1057 | throttled = true; |
e43473b7 VG |
1058 | |
1059 | if (update_disptime) { | |
1060 | tg_update_disptime(td, tg); | |
1061 | throtl_schedule_next_dispatch(td); | |
1062 | } | |
1063 | ||
bc16a4f9 | 1064 | out_unlock: |
e43473b7 | 1065 | spin_unlock_irq(q->queue_lock); |
2a7f1244 TH |
1066 | out_unlock_rcu: |
1067 | rcu_read_unlock(); | |
bc16a4f9 TH |
1068 | out: |
1069 | return throttled; | |
e43473b7 VG |
1070 | } |
1071 | ||
c9a929dd TH |
1072 | /** |
1073 | * blk_throtl_drain - drain throttled bios | |
1074 | * @q: request_queue to drain throttled bios for | |
1075 | * | |
1076 | * Dispatch all currently throttled bios on @q through ->make_request_fn(). | |
1077 | */ | |
1078 | void blk_throtl_drain(struct request_queue *q) | |
1079 | __releases(q->queue_lock) __acquires(q->queue_lock) | |
1080 | { | |
1081 | struct throtl_data *td = q->td; | |
1082 | struct throtl_rb_root *st = &td->tg_service_tree; | |
1083 | struct throtl_grp *tg; | |
1084 | struct bio_list bl; | |
1085 | struct bio *bio; | |
1086 | ||
334c2b0b | 1087 | WARN_ON_ONCE(!queue_is_locked(q)); |
c9a929dd TH |
1088 | |
1089 | bio_list_init(&bl); | |
1090 | ||
1091 | while ((tg = throtl_rb_first(st))) { | |
1092 | throtl_dequeue_tg(td, tg); | |
1093 | ||
1094 | while ((bio = bio_list_peek(&tg->bio_lists[READ]))) | |
1095 | tg_dispatch_one_bio(td, tg, bio_data_dir(bio), &bl); | |
1096 | while ((bio = bio_list_peek(&tg->bio_lists[WRITE]))) | |
1097 | tg_dispatch_one_bio(td, tg, bio_data_dir(bio), &bl); | |
1098 | } | |
1099 | spin_unlock_irq(q->queue_lock); | |
1100 | ||
1101 | while ((bio = bio_list_pop(&bl))) | |
1102 | generic_make_request(bio); | |
1103 | ||
1104 | spin_lock_irq(q->queue_lock); | |
1105 | } | |
1106 | ||
e43473b7 VG |
1107 | int blk_throtl_init(struct request_queue *q) |
1108 | { | |
1109 | struct throtl_data *td; | |
cd1604fa | 1110 | struct blkio_group *blkg; |
e43473b7 VG |
1111 | |
1112 | td = kzalloc_node(sizeof(*td), GFP_KERNEL, q->node); | |
1113 | if (!td) | |
1114 | return -ENOMEM; | |
1115 | ||
e43473b7 | 1116 | td->tg_service_tree = THROTL_RB_ROOT; |
de701c74 | 1117 | td->limits_changed = false; |
a29a171e | 1118 | INIT_DELAYED_WORK(&td->throtl_work, blk_throtl_work); |
e43473b7 | 1119 | |
cd1604fa | 1120 | q->td = td; |
29b12589 | 1121 | td->queue = q; |
02977e4a | 1122 | |
cd1604fa | 1123 | /* alloc and init root group. */ |
f51b802c TH |
1124 | rcu_read_lock(); |
1125 | spin_lock_irq(q->queue_lock); | |
29b12589 | 1126 | |
cd1604fa TH |
1127 | blkg = blkg_lookup_create(&blkio_root_cgroup, q, BLKIO_POLICY_THROTL, |
1128 | true); | |
1129 | if (!IS_ERR(blkg)) | |
0381411e | 1130 | td->root_tg = blkg_to_tg(blkg); |
e43473b7 | 1131 | |
f51b802c | 1132 | spin_unlock_irq(q->queue_lock); |
e43473b7 VG |
1133 | rcu_read_unlock(); |
1134 | ||
f51b802c TH |
1135 | if (!td->root_tg) { |
1136 | kfree(td); | |
1137 | return -ENOMEM; | |
1138 | } | |
e43473b7 VG |
1139 | return 0; |
1140 | } | |
1141 | ||
1142 | void blk_throtl_exit(struct request_queue *q) | |
1143 | { | |
1144 | struct throtl_data *td = q->td; | |
4eef3049 | 1145 | bool wait; |
e43473b7 VG |
1146 | |
1147 | BUG_ON(!td); | |
1148 | ||
da527770 | 1149 | throtl_shutdown_wq(q); |
e43473b7 VG |
1150 | |
1151 | spin_lock_irq(q->queue_lock); | |
72e06c25 | 1152 | throtl_release_tgs(td, true); |
e43473b7 VG |
1153 | |
1154 | /* If there are other groups */ | |
4eef3049 | 1155 | wait = q->nr_blkgs[BLKIO_POLICY_THROTL]; |
e43473b7 VG |
1156 | |
1157 | spin_unlock_irq(q->queue_lock); | |
1158 | ||
1159 | /* | |
0381411e | 1160 | * Wait for tg_to_blkg(tg)->q accessors to exit their grace periods. |
e43473b7 VG |
1161 | * Do this wait only if there are other undestroyed groups out |
1162 | * there (other than root group). This can happen if cgroup deletion | |
1163 | * path claimed the responsibility of cleaning up a group before | |
1164 | * queue cleanup code get to the group. | |
1165 | * | |
1166 | * Do not call synchronize_rcu() unconditionally as there are drivers | |
1167 | * which create/delete request queue hundreds of times during scan/boot | |
1168 | * and synchronize_rcu() can take significant time and slow down boot. | |
1169 | */ | |
1170 | if (wait) | |
1171 | synchronize_rcu(); | |
fe071437 VG |
1172 | |
1173 | /* | |
1174 | * Just being safe to make sure after previous flush if some body did | |
1175 | * update limits through cgroup and another work got queued, cancel | |
1176 | * it. | |
1177 | */ | |
da527770 | 1178 | throtl_shutdown_wq(q); |
c9a929dd | 1179 | |
c9a929dd | 1180 | kfree(q->td); |
e43473b7 VG |
1181 | } |
1182 | ||
1183 | static int __init throtl_init(void) | |
1184 | { | |
450adcbe VG |
1185 | kthrotld_workqueue = alloc_workqueue("kthrotld", WQ_MEM_RECLAIM, 0); |
1186 | if (!kthrotld_workqueue) | |
1187 | panic("Failed to create kthrotld\n"); | |
1188 | ||
e43473b7 VG |
1189 | blkio_policy_register(&blkio_policy_throtl); |
1190 | return 0; | |
1191 | } | |
1192 | ||
1193 | module_init(throtl_init); |