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