]>
Commit | Line | Data |
---|---|---|
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 | #include "blk.h" | |
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 | ||
24 | static struct blkcg_policy blkcg_policy_throtl; | |
25 | ||
26 | /* A workqueue to queue throttle related work */ | |
27 | static struct workqueue_struct *kthrotld_workqueue; | |
28 | ||
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 | /* Per-cpu group stats */ | |
42 | struct tg_stats_cpu { | |
43 | /* total bytes transferred */ | |
44 | struct blkg_rwstat service_bytes; | |
45 | /* total IOs serviced, post merge */ | |
46 | struct blkg_rwstat serviced; | |
47 | }; | |
48 | ||
49 | struct throtl_grp { | |
50 | /* must be the first member */ | |
51 | struct blkg_policy_data pd; | |
52 | ||
53 | /* active throtl group service_tree member */ | |
54 | struct rb_node rb_node; | |
55 | ||
56 | /* | |
57 | * Dispatch time in jiffies. This is the estimated time when group | |
58 | * will unthrottle and is ready to dispatch more bio. It is used as | |
59 | * key to sort active groups in service tree. | |
60 | */ | |
61 | unsigned long disptime; | |
62 | ||
63 | unsigned int flags; | |
64 | ||
65 | /* Two lists for READ and WRITE */ | |
66 | struct bio_list bio_lists[2]; | |
67 | ||
68 | /* Number of queued bios on READ and WRITE lists */ | |
69 | unsigned int nr_queued[2]; | |
70 | ||
71 | /* bytes per second rate limits */ | |
72 | uint64_t bps[2]; | |
73 | ||
74 | /* IOPS limits */ | |
75 | unsigned int iops[2]; | |
76 | ||
77 | /* Number of bytes disptached in current slice */ | |
78 | uint64_t bytes_disp[2]; | |
79 | /* Number of bio's dispatched in current slice */ | |
80 | unsigned int io_disp[2]; | |
81 | ||
82 | /* When did we start a new slice */ | |
83 | unsigned long slice_start[2]; | |
84 | unsigned long slice_end[2]; | |
85 | ||
86 | /* Per cpu stats pointer */ | |
87 | struct tg_stats_cpu __percpu *stats_cpu; | |
88 | ||
89 | /* List of tgs waiting for per cpu stats memory to be allocated */ | |
90 | struct list_head stats_alloc_node; | |
91 | }; | |
92 | ||
93 | struct throtl_data | |
94 | { | |
95 | /* service tree for active throtl groups */ | |
96 | struct throtl_rb_root tg_service_tree; | |
97 | ||
98 | struct request_queue *queue; | |
99 | ||
100 | /* Total Number of queued bios on READ and WRITE lists */ | |
101 | unsigned int nr_queued[2]; | |
102 | ||
103 | /* | |
104 | * number of total undestroyed groups | |
105 | */ | |
106 | unsigned int nr_undestroyed_grps; | |
107 | ||
108 | /* Work for dispatching throttled bios */ | |
109 | struct delayed_work dispatch_work; | |
110 | }; | |
111 | ||
112 | /* list and work item to allocate percpu group stats */ | |
113 | static DEFINE_SPINLOCK(tg_stats_alloc_lock); | |
114 | static LIST_HEAD(tg_stats_alloc_list); | |
115 | ||
116 | static void tg_stats_alloc_fn(struct work_struct *); | |
117 | static DECLARE_DELAYED_WORK(tg_stats_alloc_work, tg_stats_alloc_fn); | |
118 | ||
119 | static inline struct throtl_grp *pd_to_tg(struct blkg_policy_data *pd) | |
120 | { | |
121 | return pd ? container_of(pd, struct throtl_grp, pd) : NULL; | |
122 | } | |
123 | ||
124 | static inline struct throtl_grp *blkg_to_tg(struct blkcg_gq *blkg) | |
125 | { | |
126 | return pd_to_tg(blkg_to_pd(blkg, &blkcg_policy_throtl)); | |
127 | } | |
128 | ||
129 | static inline struct blkcg_gq *tg_to_blkg(struct throtl_grp *tg) | |
130 | { | |
131 | return pd_to_blkg(&tg->pd); | |
132 | } | |
133 | ||
134 | static inline struct throtl_grp *td_root_tg(struct throtl_data *td) | |
135 | { | |
136 | return blkg_to_tg(td->queue->root_blkg); | |
137 | } | |
138 | ||
139 | enum tg_state_flags { | |
140 | THROTL_TG_FLAG_on_rr = 0, /* on round-robin busy list */ | |
141 | }; | |
142 | ||
143 | #define THROTL_TG_FNS(name) \ | |
144 | static inline void throtl_mark_tg_##name(struct throtl_grp *tg) \ | |
145 | { \ | |
146 | (tg)->flags |= (1 << THROTL_TG_FLAG_##name); \ | |
147 | } \ | |
148 | static inline void throtl_clear_tg_##name(struct throtl_grp *tg) \ | |
149 | { \ | |
150 | (tg)->flags &= ~(1 << THROTL_TG_FLAG_##name); \ | |
151 | } \ | |
152 | static inline int throtl_tg_##name(const struct throtl_grp *tg) \ | |
153 | { \ | |
154 | return ((tg)->flags & (1 << THROTL_TG_FLAG_##name)) != 0; \ | |
155 | } | |
156 | ||
157 | THROTL_TG_FNS(on_rr); | |
158 | ||
159 | #define throtl_log_tg(td, tg, fmt, args...) do { \ | |
160 | char __pbuf[128]; \ | |
161 | \ | |
162 | blkg_path(tg_to_blkg(tg), __pbuf, sizeof(__pbuf)); \ | |
163 | blk_add_trace_msg((td)->queue, "throtl %s " fmt, __pbuf, ##args); \ | |
164 | } while (0) | |
165 | ||
166 | #define throtl_log(td, fmt, args...) \ | |
167 | blk_add_trace_msg((td)->queue, "throtl " fmt, ##args) | |
168 | ||
169 | static inline unsigned int total_nr_queued(struct throtl_data *td) | |
170 | { | |
171 | return td->nr_queued[0] + td->nr_queued[1]; | |
172 | } | |
173 | ||
174 | /* | |
175 | * Worker for allocating per cpu stat for tgs. This is scheduled on the | |
176 | * system_wq once there are some groups on the alloc_list waiting for | |
177 | * allocation. | |
178 | */ | |
179 | static void tg_stats_alloc_fn(struct work_struct *work) | |
180 | { | |
181 | static struct tg_stats_cpu *stats_cpu; /* this fn is non-reentrant */ | |
182 | struct delayed_work *dwork = to_delayed_work(work); | |
183 | bool empty = false; | |
184 | ||
185 | alloc_stats: | |
186 | if (!stats_cpu) { | |
187 | stats_cpu = alloc_percpu(struct tg_stats_cpu); | |
188 | if (!stats_cpu) { | |
189 | /* allocation failed, try again after some time */ | |
190 | schedule_delayed_work(dwork, msecs_to_jiffies(10)); | |
191 | return; | |
192 | } | |
193 | } | |
194 | ||
195 | spin_lock_irq(&tg_stats_alloc_lock); | |
196 | ||
197 | if (!list_empty(&tg_stats_alloc_list)) { | |
198 | struct throtl_grp *tg = list_first_entry(&tg_stats_alloc_list, | |
199 | struct throtl_grp, | |
200 | stats_alloc_node); | |
201 | swap(tg->stats_cpu, stats_cpu); | |
202 | list_del_init(&tg->stats_alloc_node); | |
203 | } | |
204 | ||
205 | empty = list_empty(&tg_stats_alloc_list); | |
206 | spin_unlock_irq(&tg_stats_alloc_lock); | |
207 | if (!empty) | |
208 | goto alloc_stats; | |
209 | } | |
210 | ||
211 | static void throtl_pd_init(struct blkcg_gq *blkg) | |
212 | { | |
213 | struct throtl_grp *tg = blkg_to_tg(blkg); | |
214 | unsigned long flags; | |
215 | ||
216 | RB_CLEAR_NODE(&tg->rb_node); | |
217 | bio_list_init(&tg->bio_lists[0]); | |
218 | bio_list_init(&tg->bio_lists[1]); | |
219 | ||
220 | tg->bps[READ] = -1; | |
221 | tg->bps[WRITE] = -1; | |
222 | tg->iops[READ] = -1; | |
223 | tg->iops[WRITE] = -1; | |
224 | ||
225 | /* | |
226 | * Ugh... We need to perform per-cpu allocation for tg->stats_cpu | |
227 | * but percpu allocator can't be called from IO path. Queue tg on | |
228 | * tg_stats_alloc_list and allocate from work item. | |
229 | */ | |
230 | spin_lock_irqsave(&tg_stats_alloc_lock, flags); | |
231 | list_add(&tg->stats_alloc_node, &tg_stats_alloc_list); | |
232 | schedule_delayed_work(&tg_stats_alloc_work, 0); | |
233 | spin_unlock_irqrestore(&tg_stats_alloc_lock, flags); | |
234 | } | |
235 | ||
236 | static void throtl_pd_exit(struct blkcg_gq *blkg) | |
237 | { | |
238 | struct throtl_grp *tg = blkg_to_tg(blkg); | |
239 | unsigned long flags; | |
240 | ||
241 | spin_lock_irqsave(&tg_stats_alloc_lock, flags); | |
242 | list_del_init(&tg->stats_alloc_node); | |
243 | spin_unlock_irqrestore(&tg_stats_alloc_lock, flags); | |
244 | ||
245 | free_percpu(tg->stats_cpu); | |
246 | } | |
247 | ||
248 | static void throtl_pd_reset_stats(struct blkcg_gq *blkg) | |
249 | { | |
250 | struct throtl_grp *tg = blkg_to_tg(blkg); | |
251 | int cpu; | |
252 | ||
253 | if (tg->stats_cpu == NULL) | |
254 | return; | |
255 | ||
256 | for_each_possible_cpu(cpu) { | |
257 | struct tg_stats_cpu *sc = per_cpu_ptr(tg->stats_cpu, cpu); | |
258 | ||
259 | blkg_rwstat_reset(&sc->service_bytes); | |
260 | blkg_rwstat_reset(&sc->serviced); | |
261 | } | |
262 | } | |
263 | ||
264 | static struct throtl_grp *throtl_lookup_tg(struct throtl_data *td, | |
265 | struct blkcg *blkcg) | |
266 | { | |
267 | /* | |
268 | * This is the common case when there are no blkcgs. Avoid lookup | |
269 | * in this case | |
270 | */ | |
271 | if (blkcg == &blkcg_root) | |
272 | return td_root_tg(td); | |
273 | ||
274 | return blkg_to_tg(blkg_lookup(blkcg, td->queue)); | |
275 | } | |
276 | ||
277 | static struct throtl_grp *throtl_lookup_create_tg(struct throtl_data *td, | |
278 | struct blkcg *blkcg) | |
279 | { | |
280 | struct request_queue *q = td->queue; | |
281 | struct throtl_grp *tg = NULL; | |
282 | ||
283 | /* | |
284 | * This is the common case when there are no blkcgs. Avoid lookup | |
285 | * in this case | |
286 | */ | |
287 | if (blkcg == &blkcg_root) { | |
288 | tg = td_root_tg(td); | |
289 | } else { | |
290 | struct blkcg_gq *blkg; | |
291 | ||
292 | blkg = blkg_lookup_create(blkcg, q); | |
293 | ||
294 | /* if %NULL and @q is alive, fall back to root_tg */ | |
295 | if (!IS_ERR(blkg)) | |
296 | tg = blkg_to_tg(blkg); | |
297 | else if (!blk_queue_dying(q)) | |
298 | tg = td_root_tg(td); | |
299 | } | |
300 | ||
301 | return tg; | |
302 | } | |
303 | ||
304 | static struct throtl_grp *throtl_rb_first(struct throtl_rb_root *root) | |
305 | { | |
306 | /* Service tree is empty */ | |
307 | if (!root->count) | |
308 | return NULL; | |
309 | ||
310 | if (!root->left) | |
311 | root->left = rb_first(&root->rb); | |
312 | ||
313 | if (root->left) | |
314 | return rb_entry_tg(root->left); | |
315 | ||
316 | return NULL; | |
317 | } | |
318 | ||
319 | static void rb_erase_init(struct rb_node *n, struct rb_root *root) | |
320 | { | |
321 | rb_erase(n, root); | |
322 | RB_CLEAR_NODE(n); | |
323 | } | |
324 | ||
325 | static void throtl_rb_erase(struct rb_node *n, struct throtl_rb_root *root) | |
326 | { | |
327 | if (root->left == n) | |
328 | root->left = NULL; | |
329 | rb_erase_init(n, &root->rb); | |
330 | --root->count; | |
331 | } | |
332 | ||
333 | static void update_min_dispatch_time(struct throtl_rb_root *st) | |
334 | { | |
335 | struct throtl_grp *tg; | |
336 | ||
337 | tg = throtl_rb_first(st); | |
338 | if (!tg) | |
339 | return; | |
340 | ||
341 | st->min_disptime = tg->disptime; | |
342 | } | |
343 | ||
344 | static void | |
345 | tg_service_tree_add(struct throtl_rb_root *st, struct throtl_grp *tg) | |
346 | { | |
347 | struct rb_node **node = &st->rb.rb_node; | |
348 | struct rb_node *parent = NULL; | |
349 | struct throtl_grp *__tg; | |
350 | unsigned long key = tg->disptime; | |
351 | int left = 1; | |
352 | ||
353 | while (*node != NULL) { | |
354 | parent = *node; | |
355 | __tg = rb_entry_tg(parent); | |
356 | ||
357 | if (time_before(key, __tg->disptime)) | |
358 | node = &parent->rb_left; | |
359 | else { | |
360 | node = &parent->rb_right; | |
361 | left = 0; | |
362 | } | |
363 | } | |
364 | ||
365 | if (left) | |
366 | st->left = &tg->rb_node; | |
367 | ||
368 | rb_link_node(&tg->rb_node, parent, node); | |
369 | rb_insert_color(&tg->rb_node, &st->rb); | |
370 | } | |
371 | ||
372 | static void __throtl_enqueue_tg(struct throtl_data *td, struct throtl_grp *tg) | |
373 | { | |
374 | struct throtl_rb_root *st = &td->tg_service_tree; | |
375 | ||
376 | tg_service_tree_add(st, tg); | |
377 | throtl_mark_tg_on_rr(tg); | |
378 | st->count++; | |
379 | } | |
380 | ||
381 | static void throtl_enqueue_tg(struct throtl_data *td, struct throtl_grp *tg) | |
382 | { | |
383 | if (!throtl_tg_on_rr(tg)) | |
384 | __throtl_enqueue_tg(td, tg); | |
385 | } | |
386 | ||
387 | static void __throtl_dequeue_tg(struct throtl_data *td, struct throtl_grp *tg) | |
388 | { | |
389 | throtl_rb_erase(&tg->rb_node, &td->tg_service_tree); | |
390 | throtl_clear_tg_on_rr(tg); | |
391 | } | |
392 | ||
393 | static void throtl_dequeue_tg(struct throtl_data *td, struct throtl_grp *tg) | |
394 | { | |
395 | if (throtl_tg_on_rr(tg)) | |
396 | __throtl_dequeue_tg(td, tg); | |
397 | } | |
398 | ||
399 | /* Call with queue lock held */ | |
400 | static void throtl_schedule_delayed_work(struct throtl_data *td, | |
401 | unsigned long delay) | |
402 | { | |
403 | struct delayed_work *dwork = &td->dispatch_work; | |
404 | ||
405 | if (total_nr_queued(td)) { | |
406 | mod_delayed_work(kthrotld_workqueue, dwork, delay); | |
407 | throtl_log(td, "schedule work. delay=%lu jiffies=%lu", | |
408 | delay, jiffies); | |
409 | } | |
410 | } | |
411 | ||
412 | static void throtl_schedule_next_dispatch(struct throtl_data *td) | |
413 | { | |
414 | struct throtl_rb_root *st = &td->tg_service_tree; | |
415 | ||
416 | /* | |
417 | * If there are more bios pending, schedule more work. | |
418 | */ | |
419 | if (!total_nr_queued(td)) | |
420 | return; | |
421 | ||
422 | BUG_ON(!st->count); | |
423 | ||
424 | update_min_dispatch_time(st); | |
425 | ||
426 | if (time_before_eq(st->min_disptime, jiffies)) | |
427 | throtl_schedule_delayed_work(td, 0); | |
428 | else | |
429 | throtl_schedule_delayed_work(td, (st->min_disptime - jiffies)); | |
430 | } | |
431 | ||
432 | static inline void | |
433 | throtl_start_new_slice(struct throtl_data *td, struct throtl_grp *tg, bool rw) | |
434 | { | |
435 | tg->bytes_disp[rw] = 0; | |
436 | tg->io_disp[rw] = 0; | |
437 | tg->slice_start[rw] = jiffies; | |
438 | tg->slice_end[rw] = jiffies + throtl_slice; | |
439 | throtl_log_tg(td, tg, "[%c] new slice start=%lu end=%lu jiffies=%lu", | |
440 | rw == READ ? 'R' : 'W', tg->slice_start[rw], | |
441 | tg->slice_end[rw], jiffies); | |
442 | } | |
443 | ||
444 | static inline void throtl_set_slice_end(struct throtl_data *td, | |
445 | struct throtl_grp *tg, bool rw, unsigned long jiffy_end) | |
446 | { | |
447 | tg->slice_end[rw] = roundup(jiffy_end, throtl_slice); | |
448 | } | |
449 | ||
450 | static inline void throtl_extend_slice(struct throtl_data *td, | |
451 | struct throtl_grp *tg, bool rw, unsigned long jiffy_end) | |
452 | { | |
453 | tg->slice_end[rw] = roundup(jiffy_end, throtl_slice); | |
454 | throtl_log_tg(td, tg, "[%c] extend slice start=%lu end=%lu jiffies=%lu", | |
455 | rw == READ ? 'R' : 'W', tg->slice_start[rw], | |
456 | tg->slice_end[rw], jiffies); | |
457 | } | |
458 | ||
459 | /* Determine if previously allocated or extended slice is complete or not */ | |
460 | static bool | |
461 | throtl_slice_used(struct throtl_data *td, struct throtl_grp *tg, bool rw) | |
462 | { | |
463 | if (time_in_range(jiffies, tg->slice_start[rw], tg->slice_end[rw])) | |
464 | return 0; | |
465 | ||
466 | return 1; | |
467 | } | |
468 | ||
469 | /* Trim the used slices and adjust slice start accordingly */ | |
470 | static inline void | |
471 | throtl_trim_slice(struct throtl_data *td, struct throtl_grp *tg, bool rw) | |
472 | { | |
473 | unsigned long nr_slices, time_elapsed, io_trim; | |
474 | u64 bytes_trim, tmp; | |
475 | ||
476 | BUG_ON(time_before(tg->slice_end[rw], tg->slice_start[rw])); | |
477 | ||
478 | /* | |
479 | * If bps are unlimited (-1), then time slice don't get | |
480 | * renewed. Don't try to trim the slice if slice is used. A new | |
481 | * slice will start when appropriate. | |
482 | */ | |
483 | if (throtl_slice_used(td, tg, rw)) | |
484 | return; | |
485 | ||
486 | /* | |
487 | * A bio has been dispatched. Also adjust slice_end. It might happen | |
488 | * that initially cgroup limit was very low resulting in high | |
489 | * slice_end, but later limit was bumped up and bio was dispached | |
490 | * sooner, then we need to reduce slice_end. A high bogus slice_end | |
491 | * is bad because it does not allow new slice to start. | |
492 | */ | |
493 | ||
494 | throtl_set_slice_end(td, tg, rw, jiffies + throtl_slice); | |
495 | ||
496 | time_elapsed = jiffies - tg->slice_start[rw]; | |
497 | ||
498 | nr_slices = time_elapsed / throtl_slice; | |
499 | ||
500 | if (!nr_slices) | |
501 | return; | |
502 | tmp = tg->bps[rw] * throtl_slice * nr_slices; | |
503 | do_div(tmp, HZ); | |
504 | bytes_trim = tmp; | |
505 | ||
506 | io_trim = (tg->iops[rw] * throtl_slice * nr_slices)/HZ; | |
507 | ||
508 | if (!bytes_trim && !io_trim) | |
509 | return; | |
510 | ||
511 | if (tg->bytes_disp[rw] >= bytes_trim) | |
512 | tg->bytes_disp[rw] -= bytes_trim; | |
513 | else | |
514 | tg->bytes_disp[rw] = 0; | |
515 | ||
516 | if (tg->io_disp[rw] >= io_trim) | |
517 | tg->io_disp[rw] -= io_trim; | |
518 | else | |
519 | tg->io_disp[rw] = 0; | |
520 | ||
521 | tg->slice_start[rw] += nr_slices * throtl_slice; | |
522 | ||
523 | throtl_log_tg(td, tg, "[%c] trim slice nr=%lu bytes=%llu io=%lu" | |
524 | " start=%lu end=%lu jiffies=%lu", | |
525 | rw == READ ? 'R' : 'W', nr_slices, bytes_trim, io_trim, | |
526 | tg->slice_start[rw], tg->slice_end[rw], jiffies); | |
527 | } | |
528 | ||
529 | static bool tg_with_in_iops_limit(struct throtl_data *td, struct throtl_grp *tg, | |
530 | struct bio *bio, unsigned long *wait) | |
531 | { | |
532 | bool rw = bio_data_dir(bio); | |
533 | unsigned int io_allowed; | |
534 | unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd; | |
535 | u64 tmp; | |
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 | ||
545 | /* | |
546 | * jiffy_elapsed_rnd should not be a big value as minimum iops can be | |
547 | * 1 then at max jiffy elapsed should be equivalent of 1 second as we | |
548 | * will allow dispatch after 1 second and after that slice should | |
549 | * have been trimmed. | |
550 | */ | |
551 | ||
552 | tmp = (u64)tg->iops[rw] * jiffy_elapsed_rnd; | |
553 | do_div(tmp, HZ); | |
554 | ||
555 | if (tmp > UINT_MAX) | |
556 | io_allowed = UINT_MAX; | |
557 | else | |
558 | io_allowed = tmp; | |
559 | ||
560 | if (tg->io_disp[rw] + 1 <= io_allowed) { | |
561 | if (wait) | |
562 | *wait = 0; | |
563 | return 1; | |
564 | } | |
565 | ||
566 | /* Calc approx time to dispatch */ | |
567 | jiffy_wait = ((tg->io_disp[rw] + 1) * HZ)/tg->iops[rw] + 1; | |
568 | ||
569 | if (jiffy_wait > jiffy_elapsed) | |
570 | jiffy_wait = jiffy_wait - jiffy_elapsed; | |
571 | else | |
572 | jiffy_wait = 1; | |
573 | ||
574 | if (wait) | |
575 | *wait = jiffy_wait; | |
576 | return 0; | |
577 | } | |
578 | ||
579 | static bool tg_with_in_bps_limit(struct throtl_data *td, struct throtl_grp *tg, | |
580 | struct bio *bio, unsigned long *wait) | |
581 | { | |
582 | bool rw = bio_data_dir(bio); | |
583 | u64 bytes_allowed, extra_bytes, tmp; | |
584 | unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd; | |
585 | ||
586 | jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw]; | |
587 | ||
588 | /* Slice has just started. Consider one slice interval */ | |
589 | if (!jiffy_elapsed) | |
590 | jiffy_elapsed_rnd = throtl_slice; | |
591 | ||
592 | jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice); | |
593 | ||
594 | tmp = tg->bps[rw] * jiffy_elapsed_rnd; | |
595 | do_div(tmp, HZ); | |
596 | bytes_allowed = tmp; | |
597 | ||
598 | if (tg->bytes_disp[rw] + bio->bi_size <= bytes_allowed) { | |
599 | if (wait) | |
600 | *wait = 0; | |
601 | return 1; | |
602 | } | |
603 | ||
604 | /* Calc approx time to dispatch */ | |
605 | extra_bytes = tg->bytes_disp[rw] + bio->bi_size - bytes_allowed; | |
606 | jiffy_wait = div64_u64(extra_bytes * HZ, tg->bps[rw]); | |
607 | ||
608 | if (!jiffy_wait) | |
609 | jiffy_wait = 1; | |
610 | ||
611 | /* | |
612 | * This wait time is without taking into consideration the rounding | |
613 | * up we did. Add that time also. | |
614 | */ | |
615 | jiffy_wait = jiffy_wait + (jiffy_elapsed_rnd - jiffy_elapsed); | |
616 | if (wait) | |
617 | *wait = jiffy_wait; | |
618 | return 0; | |
619 | } | |
620 | ||
621 | static bool tg_no_rule_group(struct throtl_grp *tg, bool rw) { | |
622 | if (tg->bps[rw] == -1 && tg->iops[rw] == -1) | |
623 | return 1; | |
624 | return 0; | |
625 | } | |
626 | ||
627 | /* | |
628 | * Returns whether one can dispatch a bio or not. Also returns approx number | |
629 | * of jiffies to wait before this bio is with-in IO rate and can be dispatched | |
630 | */ | |
631 | static bool tg_may_dispatch(struct throtl_data *td, struct throtl_grp *tg, | |
632 | struct bio *bio, unsigned long *wait) | |
633 | { | |
634 | bool rw = bio_data_dir(bio); | |
635 | unsigned long bps_wait = 0, iops_wait = 0, max_wait = 0; | |
636 | ||
637 | /* | |
638 | * Currently whole state machine of group depends on first bio | |
639 | * queued in the group bio list. So one should not be calling | |
640 | * this function with a different bio if there are other bios | |
641 | * queued. | |
642 | */ | |
643 | BUG_ON(tg->nr_queued[rw] && bio != bio_list_peek(&tg->bio_lists[rw])); | |
644 | ||
645 | /* If tg->bps = -1, then BW is unlimited */ | |
646 | if (tg->bps[rw] == -1 && tg->iops[rw] == -1) { | |
647 | if (wait) | |
648 | *wait = 0; | |
649 | return 1; | |
650 | } | |
651 | ||
652 | /* | |
653 | * If previous slice expired, start a new one otherwise renew/extend | |
654 | * existing slice to make sure it is at least throtl_slice interval | |
655 | * long since now. | |
656 | */ | |
657 | if (throtl_slice_used(td, tg, rw)) | |
658 | throtl_start_new_slice(td, tg, rw); | |
659 | else { | |
660 | if (time_before(tg->slice_end[rw], jiffies + throtl_slice)) | |
661 | throtl_extend_slice(td, tg, rw, jiffies + throtl_slice); | |
662 | } | |
663 | ||
664 | if (tg_with_in_bps_limit(td, tg, bio, &bps_wait) | |
665 | && tg_with_in_iops_limit(td, tg, bio, &iops_wait)) { | |
666 | if (wait) | |
667 | *wait = 0; | |
668 | return 1; | |
669 | } | |
670 | ||
671 | max_wait = max(bps_wait, iops_wait); | |
672 | ||
673 | if (wait) | |
674 | *wait = max_wait; | |
675 | ||
676 | if (time_before(tg->slice_end[rw], jiffies + max_wait)) | |
677 | throtl_extend_slice(td, tg, rw, jiffies + max_wait); | |
678 | ||
679 | return 0; | |
680 | } | |
681 | ||
682 | static void throtl_update_dispatch_stats(struct blkcg_gq *blkg, u64 bytes, | |
683 | int rw) | |
684 | { | |
685 | struct throtl_grp *tg = blkg_to_tg(blkg); | |
686 | struct tg_stats_cpu *stats_cpu; | |
687 | unsigned long flags; | |
688 | ||
689 | /* If per cpu stats are not allocated yet, don't do any accounting. */ | |
690 | if (tg->stats_cpu == NULL) | |
691 | return; | |
692 | ||
693 | /* | |
694 | * Disabling interrupts to provide mutual exclusion between two | |
695 | * writes on same cpu. It probably is not needed for 64bit. Not | |
696 | * optimizing that case yet. | |
697 | */ | |
698 | local_irq_save(flags); | |
699 | ||
700 | stats_cpu = this_cpu_ptr(tg->stats_cpu); | |
701 | ||
702 | blkg_rwstat_add(&stats_cpu->serviced, rw, 1); | |
703 | blkg_rwstat_add(&stats_cpu->service_bytes, rw, bytes); | |
704 | ||
705 | local_irq_restore(flags); | |
706 | } | |
707 | ||
708 | static void throtl_charge_bio(struct throtl_grp *tg, struct bio *bio) | |
709 | { | |
710 | bool rw = bio_data_dir(bio); | |
711 | ||
712 | /* Charge the bio to the group */ | |
713 | tg->bytes_disp[rw] += bio->bi_size; | |
714 | tg->io_disp[rw]++; | |
715 | ||
716 | throtl_update_dispatch_stats(tg_to_blkg(tg), bio->bi_size, bio->bi_rw); | |
717 | } | |
718 | ||
719 | static void throtl_add_bio_tg(struct throtl_data *td, struct throtl_grp *tg, | |
720 | struct bio *bio) | |
721 | { | |
722 | bool rw = bio_data_dir(bio); | |
723 | ||
724 | bio_list_add(&tg->bio_lists[rw], bio); | |
725 | /* Take a bio reference on tg */ | |
726 | blkg_get(tg_to_blkg(tg)); | |
727 | tg->nr_queued[rw]++; | |
728 | td->nr_queued[rw]++; | |
729 | throtl_enqueue_tg(td, tg); | |
730 | } | |
731 | ||
732 | static void tg_update_disptime(struct throtl_data *td, struct throtl_grp *tg) | |
733 | { | |
734 | unsigned long read_wait = -1, write_wait = -1, min_wait = -1, disptime; | |
735 | struct bio *bio; | |
736 | ||
737 | if ((bio = bio_list_peek(&tg->bio_lists[READ]))) | |
738 | tg_may_dispatch(td, tg, bio, &read_wait); | |
739 | ||
740 | if ((bio = bio_list_peek(&tg->bio_lists[WRITE]))) | |
741 | tg_may_dispatch(td, tg, bio, &write_wait); | |
742 | ||
743 | min_wait = min(read_wait, write_wait); | |
744 | disptime = jiffies + min_wait; | |
745 | ||
746 | /* Update dispatch time */ | |
747 | throtl_dequeue_tg(td, tg); | |
748 | tg->disptime = disptime; | |
749 | throtl_enqueue_tg(td, tg); | |
750 | } | |
751 | ||
752 | static void tg_dispatch_one_bio(struct throtl_data *td, struct throtl_grp *tg, | |
753 | bool rw, struct bio_list *bl) | |
754 | { | |
755 | struct bio *bio; | |
756 | ||
757 | bio = bio_list_pop(&tg->bio_lists[rw]); | |
758 | tg->nr_queued[rw]--; | |
759 | /* Drop bio reference on blkg */ | |
760 | blkg_put(tg_to_blkg(tg)); | |
761 | ||
762 | BUG_ON(td->nr_queued[rw] <= 0); | |
763 | td->nr_queued[rw]--; | |
764 | ||
765 | throtl_charge_bio(tg, bio); | |
766 | bio_list_add(bl, bio); | |
767 | bio->bi_rw |= REQ_THROTTLED; | |
768 | ||
769 | throtl_trim_slice(td, tg, rw); | |
770 | } | |
771 | ||
772 | static int throtl_dispatch_tg(struct throtl_data *td, struct throtl_grp *tg, | |
773 | struct bio_list *bl) | |
774 | { | |
775 | unsigned int nr_reads = 0, nr_writes = 0; | |
776 | unsigned int max_nr_reads = throtl_grp_quantum*3/4; | |
777 | unsigned int max_nr_writes = throtl_grp_quantum - max_nr_reads; | |
778 | struct bio *bio; | |
779 | ||
780 | /* Try to dispatch 75% READS and 25% WRITES */ | |
781 | ||
782 | while ((bio = bio_list_peek(&tg->bio_lists[READ])) | |
783 | && tg_may_dispatch(td, tg, bio, NULL)) { | |
784 | ||
785 | tg_dispatch_one_bio(td, tg, bio_data_dir(bio), bl); | |
786 | nr_reads++; | |
787 | ||
788 | if (nr_reads >= max_nr_reads) | |
789 | break; | |
790 | } | |
791 | ||
792 | while ((bio = bio_list_peek(&tg->bio_lists[WRITE])) | |
793 | && tg_may_dispatch(td, tg, bio, NULL)) { | |
794 | ||
795 | tg_dispatch_one_bio(td, tg, bio_data_dir(bio), bl); | |
796 | nr_writes++; | |
797 | ||
798 | if (nr_writes >= max_nr_writes) | |
799 | break; | |
800 | } | |
801 | ||
802 | return nr_reads + nr_writes; | |
803 | } | |
804 | ||
805 | static int throtl_select_dispatch(struct throtl_data *td, struct bio_list *bl) | |
806 | { | |
807 | unsigned int nr_disp = 0; | |
808 | struct throtl_grp *tg; | |
809 | struct throtl_rb_root *st = &td->tg_service_tree; | |
810 | ||
811 | while (1) { | |
812 | tg = throtl_rb_first(st); | |
813 | ||
814 | if (!tg) | |
815 | break; | |
816 | ||
817 | if (time_before(jiffies, tg->disptime)) | |
818 | break; | |
819 | ||
820 | throtl_dequeue_tg(td, tg); | |
821 | ||
822 | nr_disp += throtl_dispatch_tg(td, tg, bl); | |
823 | ||
824 | if (tg->nr_queued[0] || tg->nr_queued[1]) | |
825 | tg_update_disptime(td, tg); | |
826 | ||
827 | if (nr_disp >= throtl_quantum) | |
828 | break; | |
829 | } | |
830 | ||
831 | return nr_disp; | |
832 | } | |
833 | ||
834 | /* work function to dispatch throttled bios */ | |
835 | void blk_throtl_dispatch_work_fn(struct work_struct *work) | |
836 | { | |
837 | struct throtl_data *td = container_of(to_delayed_work(work), | |
838 | struct throtl_data, dispatch_work); | |
839 | struct request_queue *q = td->queue; | |
840 | unsigned int nr_disp = 0; | |
841 | struct bio_list bio_list_on_stack; | |
842 | struct bio *bio; | |
843 | struct blk_plug plug; | |
844 | ||
845 | spin_lock_irq(q->queue_lock); | |
846 | ||
847 | if (!total_nr_queued(td)) | |
848 | goto out; | |
849 | ||
850 | bio_list_init(&bio_list_on_stack); | |
851 | ||
852 | throtl_log(td, "dispatch nr_queued=%u read=%u write=%u", | |
853 | total_nr_queued(td), td->nr_queued[READ], | |
854 | td->nr_queued[WRITE]); | |
855 | ||
856 | nr_disp = throtl_select_dispatch(td, &bio_list_on_stack); | |
857 | ||
858 | if (nr_disp) | |
859 | throtl_log(td, "bios disp=%u", nr_disp); | |
860 | ||
861 | throtl_schedule_next_dispatch(td); | |
862 | out: | |
863 | spin_unlock_irq(q->queue_lock); | |
864 | ||
865 | /* | |
866 | * If we dispatched some requests, unplug the queue to make sure | |
867 | * immediate dispatch | |
868 | */ | |
869 | if (nr_disp) { | |
870 | blk_start_plug(&plug); | |
871 | while((bio = bio_list_pop(&bio_list_on_stack))) | |
872 | generic_make_request(bio); | |
873 | blk_finish_plug(&plug); | |
874 | } | |
875 | } | |
876 | ||
877 | static u64 tg_prfill_cpu_rwstat(struct seq_file *sf, | |
878 | struct blkg_policy_data *pd, int off) | |
879 | { | |
880 | struct throtl_grp *tg = pd_to_tg(pd); | |
881 | struct blkg_rwstat rwstat = { }, tmp; | |
882 | int i, cpu; | |
883 | ||
884 | for_each_possible_cpu(cpu) { | |
885 | struct tg_stats_cpu *sc = per_cpu_ptr(tg->stats_cpu, cpu); | |
886 | ||
887 | tmp = blkg_rwstat_read((void *)sc + off); | |
888 | for (i = 0; i < BLKG_RWSTAT_NR; i++) | |
889 | rwstat.cnt[i] += tmp.cnt[i]; | |
890 | } | |
891 | ||
892 | return __blkg_prfill_rwstat(sf, pd, &rwstat); | |
893 | } | |
894 | ||
895 | static int tg_print_cpu_rwstat(struct cgroup *cgrp, struct cftype *cft, | |
896 | struct seq_file *sf) | |
897 | { | |
898 | struct blkcg *blkcg = cgroup_to_blkcg(cgrp); | |
899 | ||
900 | blkcg_print_blkgs(sf, blkcg, tg_prfill_cpu_rwstat, &blkcg_policy_throtl, | |
901 | cft->private, true); | |
902 | return 0; | |
903 | } | |
904 | ||
905 | static u64 tg_prfill_conf_u64(struct seq_file *sf, struct blkg_policy_data *pd, | |
906 | int off) | |
907 | { | |
908 | struct throtl_grp *tg = pd_to_tg(pd); | |
909 | u64 v = *(u64 *)((void *)tg + off); | |
910 | ||
911 | if (v == -1) | |
912 | return 0; | |
913 | return __blkg_prfill_u64(sf, pd, v); | |
914 | } | |
915 | ||
916 | static u64 tg_prfill_conf_uint(struct seq_file *sf, struct blkg_policy_data *pd, | |
917 | int off) | |
918 | { | |
919 | struct throtl_grp *tg = pd_to_tg(pd); | |
920 | unsigned int v = *(unsigned int *)((void *)tg + off); | |
921 | ||
922 | if (v == -1) | |
923 | return 0; | |
924 | return __blkg_prfill_u64(sf, pd, v); | |
925 | } | |
926 | ||
927 | static int tg_print_conf_u64(struct cgroup *cgrp, struct cftype *cft, | |
928 | struct seq_file *sf) | |
929 | { | |
930 | blkcg_print_blkgs(sf, cgroup_to_blkcg(cgrp), tg_prfill_conf_u64, | |
931 | &blkcg_policy_throtl, cft->private, false); | |
932 | return 0; | |
933 | } | |
934 | ||
935 | static int tg_print_conf_uint(struct cgroup *cgrp, struct cftype *cft, | |
936 | struct seq_file *sf) | |
937 | { | |
938 | blkcg_print_blkgs(sf, cgroup_to_blkcg(cgrp), tg_prfill_conf_uint, | |
939 | &blkcg_policy_throtl, cft->private, false); | |
940 | return 0; | |
941 | } | |
942 | ||
943 | static int tg_set_conf(struct cgroup *cgrp, struct cftype *cft, const char *buf, | |
944 | bool is_u64) | |
945 | { | |
946 | struct blkcg *blkcg = cgroup_to_blkcg(cgrp); | |
947 | struct blkg_conf_ctx ctx; | |
948 | struct throtl_grp *tg; | |
949 | struct throtl_data *td; | |
950 | int ret; | |
951 | ||
952 | ret = blkg_conf_prep(blkcg, &blkcg_policy_throtl, buf, &ctx); | |
953 | if (ret) | |
954 | return ret; | |
955 | ||
956 | tg = blkg_to_tg(ctx.blkg); | |
957 | td = ctx.blkg->q->td; | |
958 | ||
959 | if (!ctx.v) | |
960 | ctx.v = -1; | |
961 | ||
962 | if (is_u64) | |
963 | *(u64 *)((void *)tg + cft->private) = ctx.v; | |
964 | else | |
965 | *(unsigned int *)((void *)tg + cft->private) = ctx.v; | |
966 | ||
967 | throtl_log_tg(td, tg, "limit change rbps=%llu wbps=%llu riops=%u wiops=%u", | |
968 | tg->bps[READ], tg->bps[WRITE], | |
969 | tg->iops[READ], tg->iops[WRITE]); | |
970 | ||
971 | /* | |
972 | * We're already holding queue_lock and know @tg is valid. Let's | |
973 | * apply the new config directly. | |
974 | * | |
975 | * Restart the slices for both READ and WRITES. It might happen | |
976 | * that a group's limit are dropped suddenly and we don't want to | |
977 | * account recently dispatched IO with new low rate. | |
978 | */ | |
979 | throtl_start_new_slice(td, tg, 0); | |
980 | throtl_start_new_slice(td, tg, 1); | |
981 | ||
982 | if (throtl_tg_on_rr(tg)) { | |
983 | tg_update_disptime(td, tg); | |
984 | throtl_schedule_next_dispatch(td); | |
985 | } | |
986 | ||
987 | blkg_conf_finish(&ctx); | |
988 | return 0; | |
989 | } | |
990 | ||
991 | static int tg_set_conf_u64(struct cgroup *cgrp, struct cftype *cft, | |
992 | const char *buf) | |
993 | { | |
994 | return tg_set_conf(cgrp, cft, buf, true); | |
995 | } | |
996 | ||
997 | static int tg_set_conf_uint(struct cgroup *cgrp, struct cftype *cft, | |
998 | const char *buf) | |
999 | { | |
1000 | return tg_set_conf(cgrp, cft, buf, false); | |
1001 | } | |
1002 | ||
1003 | static struct cftype throtl_files[] = { | |
1004 | { | |
1005 | .name = "throttle.read_bps_device", | |
1006 | .private = offsetof(struct throtl_grp, bps[READ]), | |
1007 | .read_seq_string = tg_print_conf_u64, | |
1008 | .write_string = tg_set_conf_u64, | |
1009 | .max_write_len = 256, | |
1010 | }, | |
1011 | { | |
1012 | .name = "throttle.write_bps_device", | |
1013 | .private = offsetof(struct throtl_grp, bps[WRITE]), | |
1014 | .read_seq_string = tg_print_conf_u64, | |
1015 | .write_string = tg_set_conf_u64, | |
1016 | .max_write_len = 256, | |
1017 | }, | |
1018 | { | |
1019 | .name = "throttle.read_iops_device", | |
1020 | .private = offsetof(struct throtl_grp, iops[READ]), | |
1021 | .read_seq_string = tg_print_conf_uint, | |
1022 | .write_string = tg_set_conf_uint, | |
1023 | .max_write_len = 256, | |
1024 | }, | |
1025 | { | |
1026 | .name = "throttle.write_iops_device", | |
1027 | .private = offsetof(struct throtl_grp, iops[WRITE]), | |
1028 | .read_seq_string = tg_print_conf_uint, | |
1029 | .write_string = tg_set_conf_uint, | |
1030 | .max_write_len = 256, | |
1031 | }, | |
1032 | { | |
1033 | .name = "throttle.io_service_bytes", | |
1034 | .private = offsetof(struct tg_stats_cpu, service_bytes), | |
1035 | .read_seq_string = tg_print_cpu_rwstat, | |
1036 | }, | |
1037 | { | |
1038 | .name = "throttle.io_serviced", | |
1039 | .private = offsetof(struct tg_stats_cpu, serviced), | |
1040 | .read_seq_string = tg_print_cpu_rwstat, | |
1041 | }, | |
1042 | { } /* terminate */ | |
1043 | }; | |
1044 | ||
1045 | static void throtl_shutdown_wq(struct request_queue *q) | |
1046 | { | |
1047 | struct throtl_data *td = q->td; | |
1048 | ||
1049 | cancel_delayed_work_sync(&td->dispatch_work); | |
1050 | } | |
1051 | ||
1052 | static struct blkcg_policy blkcg_policy_throtl = { | |
1053 | .pd_size = sizeof(struct throtl_grp), | |
1054 | .cftypes = throtl_files, | |
1055 | ||
1056 | .pd_init_fn = throtl_pd_init, | |
1057 | .pd_exit_fn = throtl_pd_exit, | |
1058 | .pd_reset_stats_fn = throtl_pd_reset_stats, | |
1059 | }; | |
1060 | ||
1061 | bool blk_throtl_bio(struct request_queue *q, struct bio *bio) | |
1062 | { | |
1063 | struct throtl_data *td = q->td; | |
1064 | struct throtl_grp *tg; | |
1065 | bool rw = bio_data_dir(bio), update_disptime = true; | |
1066 | struct blkcg *blkcg; | |
1067 | bool throttled = false; | |
1068 | ||
1069 | if (bio->bi_rw & REQ_THROTTLED) { | |
1070 | bio->bi_rw &= ~REQ_THROTTLED; | |
1071 | goto out; | |
1072 | } | |
1073 | ||
1074 | /* | |
1075 | * A throtl_grp pointer retrieved under rcu can be used to access | |
1076 | * basic fields like stats and io rates. If a group has no rules, | |
1077 | * just update the dispatch stats in lockless manner and return. | |
1078 | */ | |
1079 | rcu_read_lock(); | |
1080 | blkcg = bio_blkcg(bio); | |
1081 | tg = throtl_lookup_tg(td, blkcg); | |
1082 | if (tg) { | |
1083 | if (tg_no_rule_group(tg, rw)) { | |
1084 | throtl_update_dispatch_stats(tg_to_blkg(tg), | |
1085 | bio->bi_size, bio->bi_rw); | |
1086 | goto out_unlock_rcu; | |
1087 | } | |
1088 | } | |
1089 | ||
1090 | /* | |
1091 | * Either group has not been allocated yet or it is not an unlimited | |
1092 | * IO group | |
1093 | */ | |
1094 | spin_lock_irq(q->queue_lock); | |
1095 | tg = throtl_lookup_create_tg(td, blkcg); | |
1096 | if (unlikely(!tg)) | |
1097 | goto out_unlock; | |
1098 | ||
1099 | if (tg->nr_queued[rw]) { | |
1100 | /* | |
1101 | * There is already another bio queued in same dir. No | |
1102 | * need to update dispatch time. | |
1103 | */ | |
1104 | update_disptime = false; | |
1105 | goto queue_bio; | |
1106 | ||
1107 | } | |
1108 | ||
1109 | /* Bio is with-in rate limit of group */ | |
1110 | if (tg_may_dispatch(td, tg, bio, NULL)) { | |
1111 | throtl_charge_bio(tg, bio); | |
1112 | ||
1113 | /* | |
1114 | * We need to trim slice even when bios are not being queued | |
1115 | * otherwise it might happen that a bio is not queued for | |
1116 | * a long time and slice keeps on extending and trim is not | |
1117 | * called for a long time. Now if limits are reduced suddenly | |
1118 | * we take into account all the IO dispatched so far at new | |
1119 | * low rate and * newly queued IO gets a really long dispatch | |
1120 | * time. | |
1121 | * | |
1122 | * So keep on trimming slice even if bio is not queued. | |
1123 | */ | |
1124 | throtl_trim_slice(td, tg, rw); | |
1125 | goto out_unlock; | |
1126 | } | |
1127 | ||
1128 | queue_bio: | |
1129 | throtl_log_tg(td, tg, "[%c] bio. bdisp=%llu sz=%u bps=%llu" | |
1130 | " iodisp=%u iops=%u queued=%d/%d", | |
1131 | rw == READ ? 'R' : 'W', | |
1132 | tg->bytes_disp[rw], bio->bi_size, tg->bps[rw], | |
1133 | tg->io_disp[rw], tg->iops[rw], | |
1134 | tg->nr_queued[READ], tg->nr_queued[WRITE]); | |
1135 | ||
1136 | bio_associate_current(bio); | |
1137 | throtl_add_bio_tg(q->td, tg, bio); | |
1138 | throttled = true; | |
1139 | ||
1140 | if (update_disptime) { | |
1141 | tg_update_disptime(td, tg); | |
1142 | throtl_schedule_next_dispatch(td); | |
1143 | } | |
1144 | ||
1145 | out_unlock: | |
1146 | spin_unlock_irq(q->queue_lock); | |
1147 | out_unlock_rcu: | |
1148 | rcu_read_unlock(); | |
1149 | out: | |
1150 | return throttled; | |
1151 | } | |
1152 | ||
1153 | /** | |
1154 | * blk_throtl_drain - drain throttled bios | |
1155 | * @q: request_queue to drain throttled bios for | |
1156 | * | |
1157 | * Dispatch all currently throttled bios on @q through ->make_request_fn(). | |
1158 | */ | |
1159 | void blk_throtl_drain(struct request_queue *q) | |
1160 | __releases(q->queue_lock) __acquires(q->queue_lock) | |
1161 | { | |
1162 | struct throtl_data *td = q->td; | |
1163 | struct throtl_rb_root *st = &td->tg_service_tree; | |
1164 | struct throtl_grp *tg; | |
1165 | struct bio_list bl; | |
1166 | struct bio *bio; | |
1167 | ||
1168 | queue_lockdep_assert_held(q); | |
1169 | ||
1170 | bio_list_init(&bl); | |
1171 | ||
1172 | while ((tg = throtl_rb_first(st))) { | |
1173 | throtl_dequeue_tg(td, tg); | |
1174 | ||
1175 | while ((bio = bio_list_peek(&tg->bio_lists[READ]))) | |
1176 | tg_dispatch_one_bio(td, tg, bio_data_dir(bio), &bl); | |
1177 | while ((bio = bio_list_peek(&tg->bio_lists[WRITE]))) | |
1178 | tg_dispatch_one_bio(td, tg, bio_data_dir(bio), &bl); | |
1179 | } | |
1180 | spin_unlock_irq(q->queue_lock); | |
1181 | ||
1182 | while ((bio = bio_list_pop(&bl))) | |
1183 | generic_make_request(bio); | |
1184 | ||
1185 | spin_lock_irq(q->queue_lock); | |
1186 | } | |
1187 | ||
1188 | int blk_throtl_init(struct request_queue *q) | |
1189 | { | |
1190 | struct throtl_data *td; | |
1191 | int ret; | |
1192 | ||
1193 | td = kzalloc_node(sizeof(*td), GFP_KERNEL, q->node); | |
1194 | if (!td) | |
1195 | return -ENOMEM; | |
1196 | ||
1197 | td->tg_service_tree = THROTL_RB_ROOT; | |
1198 | INIT_DELAYED_WORK(&td->dispatch_work, blk_throtl_dispatch_work_fn); | |
1199 | ||
1200 | q->td = td; | |
1201 | td->queue = q; | |
1202 | ||
1203 | /* activate policy */ | |
1204 | ret = blkcg_activate_policy(q, &blkcg_policy_throtl); | |
1205 | if (ret) | |
1206 | kfree(td); | |
1207 | return ret; | |
1208 | } | |
1209 | ||
1210 | void blk_throtl_exit(struct request_queue *q) | |
1211 | { | |
1212 | BUG_ON(!q->td); | |
1213 | throtl_shutdown_wq(q); | |
1214 | blkcg_deactivate_policy(q, &blkcg_policy_throtl); | |
1215 | kfree(q->td); | |
1216 | } | |
1217 | ||
1218 | static int __init throtl_init(void) | |
1219 | { | |
1220 | kthrotld_workqueue = alloc_workqueue("kthrotld", WQ_MEM_RECLAIM, 0); | |
1221 | if (!kthrotld_workqueue) | |
1222 | panic("Failed to create kthrotld\n"); | |
1223 | ||
1224 | return blkcg_policy_register(&blkcg_policy_throtl); | |
1225 | } | |
1226 | ||
1227 | module_init(throtl_init); |