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