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