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 | ||
3c798398 | 24 | static struct blkcg_policy blkcg_policy_throtl; |
0381411e | 25 | |
450adcbe VG |
26 | /* A workqueue to queue throttle related work */ |
27 | static struct workqueue_struct *kthrotld_workqueue; | |
450adcbe | 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 | ||
8a3d2615 TH |
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 | ||
e43473b7 | 49 | struct throtl_grp { |
f95a04af TH |
50 | /* must be the first member */ |
51 | struct blkg_policy_data pd; | |
52 | ||
e43473b7 VG |
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 | ||
e43473b7 VG |
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 | ||
8e89d13f VG |
74 | /* IOPS limits */ |
75 | unsigned int iops[2]; | |
76 | ||
e43473b7 VG |
77 | /* Number of bytes disptached in current slice */ |
78 | uint64_t bytes_disp[2]; | |
8e89d13f VG |
79 | /* Number of bio's dispatched in current slice */ |
80 | unsigned int io_disp[2]; | |
e43473b7 VG |
81 | |
82 | /* When did we start a new slice */ | |
83 | unsigned long slice_start[2]; | |
84 | unsigned long slice_end[2]; | |
fe071437 | 85 | |
8a3d2615 TH |
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; | |
e43473b7 VG |
91 | }; |
92 | ||
93 | struct throtl_data | |
94 | { | |
e43473b7 VG |
95 | /* service tree for active throtl groups */ |
96 | struct throtl_rb_root tg_service_tree; | |
97 | ||
e43473b7 VG |
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 | /* | |
02977e4a | 104 | * number of total undestroyed groups |
e43473b7 VG |
105 | */ |
106 | unsigned int nr_undestroyed_grps; | |
107 | ||
108 | /* Work for dispatching throttled bios */ | |
cb76199c | 109 | struct delayed_work dispatch_work; |
e43473b7 VG |
110 | }; |
111 | ||
8a3d2615 TH |
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 | ||
f95a04af TH |
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 | ||
3c798398 | 124 | static inline struct throtl_grp *blkg_to_tg(struct blkcg_gq *blkg) |
0381411e | 125 | { |
f95a04af | 126 | return pd_to_tg(blkg_to_pd(blkg, &blkcg_policy_throtl)); |
0381411e TH |
127 | } |
128 | ||
3c798398 | 129 | static inline struct blkcg_gq *tg_to_blkg(struct throtl_grp *tg) |
0381411e | 130 | { |
f95a04af | 131 | return pd_to_blkg(&tg->pd); |
0381411e TH |
132 | } |
133 | ||
03d8e111 TH |
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 | ||
e43473b7 VG |
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 | ||
54e7ed12 TH |
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) | |
e43473b7 VG |
165 | |
166 | #define throtl_log(td, fmt, args...) \ | |
167 | blk_add_trace_msg((td)->queue, "throtl " fmt, ##args) | |
168 | ||
d2f31a5f | 169 | static inline unsigned int total_nr_queued(struct throtl_data *td) |
e43473b7 | 170 | { |
d2f31a5f | 171 | return td->nr_queued[0] + td->nr_queued[1]; |
e43473b7 VG |
172 | } |
173 | ||
8a3d2615 TH |
174 | /* |
175 | * Worker for allocating per cpu stat for tgs. This is scheduled on the | |
3b07e9ca | 176 | * system_wq once there are some groups on the alloc_list waiting for |
8a3d2615 TH |
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 */ | |
3b07e9ca | 190 | schedule_delayed_work(dwork, msecs_to_jiffies(10)); |
8a3d2615 TH |
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 | ||
3c798398 | 211 | static void throtl_pd_init(struct blkcg_gq *blkg) |
a29a171e | 212 | { |
0381411e | 213 | struct throtl_grp *tg = blkg_to_tg(blkg); |
ff26eaad | 214 | unsigned long flags; |
cd1604fa | 215 | |
a29a171e VG |
216 | RB_CLEAR_NODE(&tg->rb_node); |
217 | bio_list_init(&tg->bio_lists[0]); | |
218 | bio_list_init(&tg->bio_lists[1]); | |
a29a171e | 219 | |
e56da7e2 TH |
220 | tg->bps[READ] = -1; |
221 | tg->bps[WRITE] = -1; | |
222 | tg->iops[READ] = -1; | |
223 | tg->iops[WRITE] = -1; | |
8a3d2615 TH |
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 | */ | |
ff26eaad | 230 | spin_lock_irqsave(&tg_stats_alloc_lock, flags); |
8a3d2615 | 231 | list_add(&tg->stats_alloc_node, &tg_stats_alloc_list); |
3b07e9ca | 232 | schedule_delayed_work(&tg_stats_alloc_work, 0); |
ff26eaad | 233 | spin_unlock_irqrestore(&tg_stats_alloc_lock, flags); |
8a3d2615 TH |
234 | } |
235 | ||
3c798398 | 236 | static void throtl_pd_exit(struct blkcg_gq *blkg) |
8a3d2615 TH |
237 | { |
238 | struct throtl_grp *tg = blkg_to_tg(blkg); | |
ff26eaad | 239 | unsigned long flags; |
8a3d2615 | 240 | |
ff26eaad | 241 | spin_lock_irqsave(&tg_stats_alloc_lock, flags); |
8a3d2615 | 242 | list_del_init(&tg->stats_alloc_node); |
ff26eaad | 243 | spin_unlock_irqrestore(&tg_stats_alloc_lock, flags); |
8a3d2615 TH |
244 | |
245 | free_percpu(tg->stats_cpu); | |
246 | } | |
247 | ||
3c798398 | 248 | static void throtl_pd_reset_stats(struct blkcg_gq *blkg) |
8a3d2615 TH |
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 | } | |
a29a171e VG |
262 | } |
263 | ||
3c798398 TH |
264 | static struct throtl_grp *throtl_lookup_tg(struct throtl_data *td, |
265 | struct blkcg *blkcg) | |
e43473b7 | 266 | { |
be2c6b19 | 267 | /* |
3c798398 TH |
268 | * This is the common case when there are no blkcgs. Avoid lookup |
269 | * in this case | |
cd1604fa | 270 | */ |
3c798398 | 271 | if (blkcg == &blkcg_root) |
03d8e111 | 272 | return td_root_tg(td); |
e43473b7 | 273 | |
e8989fae | 274 | return blkg_to_tg(blkg_lookup(blkcg, td->queue)); |
e43473b7 VG |
275 | } |
276 | ||
cd1604fa | 277 | static struct throtl_grp *throtl_lookup_create_tg(struct throtl_data *td, |
3c798398 | 278 | struct blkcg *blkcg) |
e43473b7 | 279 | { |
f469a7b4 | 280 | struct request_queue *q = td->queue; |
cd1604fa | 281 | struct throtl_grp *tg = NULL; |
bc16a4f9 | 282 | |
f469a7b4 | 283 | /* |
3c798398 TH |
284 | * This is the common case when there are no blkcgs. Avoid lookup |
285 | * in this case | |
f469a7b4 | 286 | */ |
3c798398 | 287 | if (blkcg == &blkcg_root) { |
03d8e111 | 288 | tg = td_root_tg(td); |
cd1604fa | 289 | } else { |
3c798398 | 290 | struct blkcg_gq *blkg; |
f469a7b4 | 291 | |
3c96cb32 | 292 | blkg = blkg_lookup_create(blkcg, q); |
f469a7b4 | 293 | |
cd1604fa TH |
294 | /* if %NULL and @q is alive, fall back to root_tg */ |
295 | if (!IS_ERR(blkg)) | |
0381411e | 296 | tg = blkg_to_tg(blkg); |
3f3299d5 | 297 | else if (!blk_queue_dying(q)) |
03d8e111 | 298 | tg = td_root_tg(td); |
f469a7b4 VG |
299 | } |
300 | ||
e43473b7 VG |
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 | ||
a9131a27 TH |
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 | ||
e43473b7 VG |
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)) | |
450adcbe | 427 | throtl_schedule_delayed_work(td, 0); |
e43473b7 | 428 | else |
450adcbe | 429 | throtl_schedule_delayed_work(td, (st->min_disptime - jiffies)); |
e43473b7 VG |
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; | |
8e89d13f | 436 | tg->io_disp[rw] = 0; |
e43473b7 VG |
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 | ||
d1ae8ffd VG |
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 | ||
e43473b7 VG |
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 | { | |
3aad5d3e VG |
473 | unsigned long nr_slices, time_elapsed, io_trim; |
474 | u64 bytes_trim, tmp; | |
e43473b7 VG |
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 | ||
d1ae8ffd VG |
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 | ||
e43473b7 VG |
496 | time_elapsed = jiffies - tg->slice_start[rw]; |
497 | ||
498 | nr_slices = time_elapsed / throtl_slice; | |
499 | ||
500 | if (!nr_slices) | |
501 | return; | |
3aad5d3e VG |
502 | tmp = tg->bps[rw] * throtl_slice * nr_slices; |
503 | do_div(tmp, HZ); | |
504 | bytes_trim = tmp; | |
e43473b7 | 505 | |
8e89d13f | 506 | io_trim = (tg->iops[rw] * throtl_slice * nr_slices)/HZ; |
e43473b7 | 507 | |
8e89d13f | 508 | if (!bytes_trim && !io_trim) |
e43473b7 VG |
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 | ||
8e89d13f VG |
516 | if (tg->io_disp[rw] >= io_trim) |
517 | tg->io_disp[rw] -= io_trim; | |
518 | else | |
519 | tg->io_disp[rw] = 0; | |
520 | ||
e43473b7 VG |
521 | tg->slice_start[rw] += nr_slices * throtl_slice; |
522 | ||
3aad5d3e | 523 | throtl_log_tg(td, tg, "[%c] trim slice nr=%lu bytes=%llu io=%lu" |
e43473b7 | 524 | " start=%lu end=%lu jiffies=%lu", |
8e89d13f | 525 | rw == READ ? 'R' : 'W', nr_slices, bytes_trim, io_trim, |
e43473b7 VG |
526 | tg->slice_start[rw], tg->slice_end[rw], jiffies); |
527 | } | |
528 | ||
8e89d13f VG |
529 | static bool tg_with_in_iops_limit(struct throtl_data *td, struct throtl_grp *tg, |
530 | struct bio *bio, unsigned long *wait) | |
e43473b7 VG |
531 | { |
532 | bool rw = bio_data_dir(bio); | |
8e89d13f | 533 | unsigned int io_allowed; |
e43473b7 | 534 | unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd; |
c49c06e4 | 535 | u64 tmp; |
e43473b7 | 536 | |
8e89d13f | 537 | jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw]; |
e43473b7 | 538 | |
8e89d13f VG |
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 | ||
c49c06e4 VG |
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; | |
8e89d13f VG |
559 | |
560 | if (tg->io_disp[rw] + 1 <= io_allowed) { | |
e43473b7 VG |
561 | if (wait) |
562 | *wait = 0; | |
563 | return 1; | |
564 | } | |
565 | ||
8e89d13f VG |
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); | |
3aad5d3e | 583 | u64 bytes_allowed, extra_bytes, tmp; |
8e89d13f | 584 | unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd; |
e43473b7 VG |
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 | ||
5e901a2b VG |
594 | tmp = tg->bps[rw] * jiffy_elapsed_rnd; |
595 | do_div(tmp, HZ); | |
3aad5d3e | 596 | bytes_allowed = tmp; |
e43473b7 VG |
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); | |
e43473b7 VG |
616 | if (wait) |
617 | *wait = jiffy_wait; | |
8e89d13f VG |
618 | return 0; |
619 | } | |
620 | ||
af75cd3c VG |
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 | ||
8e89d13f VG |
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])); | |
e43473b7 | 644 | |
8e89d13f VG |
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); | |
e43473b7 VG |
678 | |
679 | return 0; | |
680 | } | |
681 | ||
3c798398 | 682 | static void throtl_update_dispatch_stats(struct blkcg_gq *blkg, u64 bytes, |
629ed0b1 TH |
683 | int rw) |
684 | { | |
8a3d2615 TH |
685 | struct throtl_grp *tg = blkg_to_tg(blkg); |
686 | struct tg_stats_cpu *stats_cpu; | |
629ed0b1 TH |
687 | unsigned long flags; |
688 | ||
689 | /* If per cpu stats are not allocated yet, don't do any accounting. */ | |
8a3d2615 | 690 | if (tg->stats_cpu == NULL) |
629ed0b1 TH |
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 | ||
8a3d2615 | 700 | stats_cpu = this_cpu_ptr(tg->stats_cpu); |
629ed0b1 | 701 | |
629ed0b1 TH |
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 | ||
e43473b7 VG |
708 | static void throtl_charge_bio(struct throtl_grp *tg, struct bio *bio) |
709 | { | |
710 | bool rw = bio_data_dir(bio); | |
e43473b7 VG |
711 | |
712 | /* Charge the bio to the group */ | |
713 | tg->bytes_disp[rw] += bio->bi_size; | |
8e89d13f | 714 | tg->io_disp[rw]++; |
e43473b7 | 715 | |
629ed0b1 | 716 | throtl_update_dispatch_stats(tg_to_blkg(tg), bio->bi_size, bio->bi_rw); |
e43473b7 VG |
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 */ | |
1adaf3dd | 726 | blkg_get(tg_to_blkg(tg)); |
e43473b7 VG |
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 | ||
e43473b7 VG |
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]--; | |
1adaf3dd TH |
759 | /* Drop bio reference on blkg */ |
760 | blkg_put(tg_to_blkg(tg)); | |
e43473b7 VG |
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; | |
c2f6805d | 777 | unsigned int max_nr_writes = throtl_grp_quantum - max_nr_reads; |
e43473b7 VG |
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 | ||
2db6314c | 824 | if (tg->nr_queued[0] || tg->nr_queued[1]) |
e43473b7 | 825 | tg_update_disptime(td, tg); |
e43473b7 VG |
826 | |
827 | if (nr_disp >= throtl_quantum) | |
828 | break; | |
829 | } | |
830 | ||
831 | return nr_disp; | |
832 | } | |
833 | ||
cb76199c TH |
834 | /* work function to dispatch throttled bios */ |
835 | void blk_throtl_dispatch_work_fn(struct work_struct *work) | |
e43473b7 | 836 | { |
cb76199c TH |
837 | struct throtl_data *td = container_of(to_delayed_work(work), |
838 | struct throtl_data, dispatch_work); | |
839 | struct request_queue *q = td->queue; | |
e43473b7 VG |
840 | unsigned int nr_disp = 0; |
841 | struct bio_list bio_list_on_stack; | |
842 | struct bio *bio; | |
69d60eb9 | 843 | struct blk_plug plug; |
e43473b7 VG |
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 | ||
d2f31a5f | 852 | throtl_log(td, "dispatch nr_queued=%u read=%u write=%u", |
e43473b7 VG |
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) { | |
69d60eb9 | 870 | blk_start_plug(&plug); |
e43473b7 VG |
871 | while((bio = bio_list_pop(&bio_list_on_stack))) |
872 | generic_make_request(bio); | |
69d60eb9 | 873 | blk_finish_plug(&plug); |
e43473b7 | 874 | } |
e43473b7 VG |
875 | } |
876 | ||
f95a04af TH |
877 | static u64 tg_prfill_cpu_rwstat(struct seq_file *sf, |
878 | struct blkg_policy_data *pd, int off) | |
41b38b6d | 879 | { |
f95a04af | 880 | struct throtl_grp *tg = pd_to_tg(pd); |
41b38b6d TH |
881 | struct blkg_rwstat rwstat = { }, tmp; |
882 | int i, cpu; | |
883 | ||
884 | for_each_possible_cpu(cpu) { | |
8a3d2615 | 885 | struct tg_stats_cpu *sc = per_cpu_ptr(tg->stats_cpu, cpu); |
41b38b6d TH |
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 | ||
f95a04af | 892 | return __blkg_prfill_rwstat(sf, pd, &rwstat); |
41b38b6d TH |
893 | } |
894 | ||
8a3d2615 TH |
895 | static int tg_print_cpu_rwstat(struct cgroup *cgrp, struct cftype *cft, |
896 | struct seq_file *sf) | |
41b38b6d | 897 | { |
3c798398 | 898 | struct blkcg *blkcg = cgroup_to_blkcg(cgrp); |
41b38b6d | 899 | |
3c798398 | 900 | blkcg_print_blkgs(sf, blkcg, tg_prfill_cpu_rwstat, &blkcg_policy_throtl, |
5bc4afb1 | 901 | cft->private, true); |
41b38b6d TH |
902 | return 0; |
903 | } | |
904 | ||
f95a04af TH |
905 | static u64 tg_prfill_conf_u64(struct seq_file *sf, struct blkg_policy_data *pd, |
906 | int off) | |
60c2bc2d | 907 | { |
f95a04af TH |
908 | struct throtl_grp *tg = pd_to_tg(pd); |
909 | u64 v = *(u64 *)((void *)tg + off); | |
60c2bc2d | 910 | |
af133ceb | 911 | if (v == -1) |
60c2bc2d | 912 | return 0; |
f95a04af | 913 | return __blkg_prfill_u64(sf, pd, v); |
60c2bc2d TH |
914 | } |
915 | ||
f95a04af TH |
916 | static u64 tg_prfill_conf_uint(struct seq_file *sf, struct blkg_policy_data *pd, |
917 | int off) | |
e43473b7 | 918 | { |
f95a04af TH |
919 | struct throtl_grp *tg = pd_to_tg(pd); |
920 | unsigned int v = *(unsigned int *)((void *)tg + off); | |
fe071437 | 921 | |
af133ceb TH |
922 | if (v == -1) |
923 | return 0; | |
f95a04af | 924 | return __blkg_prfill_u64(sf, pd, v); |
e43473b7 VG |
925 | } |
926 | ||
af133ceb TH |
927 | static int tg_print_conf_u64(struct cgroup *cgrp, struct cftype *cft, |
928 | struct seq_file *sf) | |
8e89d13f | 929 | { |
3c798398 TH |
930 | blkcg_print_blkgs(sf, cgroup_to_blkcg(cgrp), tg_prfill_conf_u64, |
931 | &blkcg_policy_throtl, cft->private, false); | |
af133ceb | 932 | return 0; |
8e89d13f VG |
933 | } |
934 | ||
af133ceb TH |
935 | static int tg_print_conf_uint(struct cgroup *cgrp, struct cftype *cft, |
936 | struct seq_file *sf) | |
8e89d13f | 937 | { |
3c798398 TH |
938 | blkcg_print_blkgs(sf, cgroup_to_blkcg(cgrp), tg_prfill_conf_uint, |
939 | &blkcg_policy_throtl, cft->private, false); | |
af133ceb | 940 | return 0; |
60c2bc2d TH |
941 | } |
942 | ||
af133ceb TH |
943 | static int tg_set_conf(struct cgroup *cgrp, struct cftype *cft, const char *buf, |
944 | bool is_u64) | |
60c2bc2d | 945 | { |
3c798398 | 946 | struct blkcg *blkcg = cgroup_to_blkcg(cgrp); |
60c2bc2d | 947 | struct blkg_conf_ctx ctx; |
af133ceb | 948 | struct throtl_grp *tg; |
a2b1693b | 949 | struct throtl_data *td; |
60c2bc2d TH |
950 | int ret; |
951 | ||
3c798398 | 952 | ret = blkg_conf_prep(blkcg, &blkcg_policy_throtl, buf, &ctx); |
60c2bc2d TH |
953 | if (ret) |
954 | return ret; | |
955 | ||
af133ceb | 956 | tg = blkg_to_tg(ctx.blkg); |
a2b1693b | 957 | td = ctx.blkg->q->td; |
af133ceb | 958 | |
a2b1693b TH |
959 | if (!ctx.v) |
960 | ctx.v = -1; | |
af133ceb | 961 | |
a2b1693b TH |
962 | if (is_u64) |
963 | *(u64 *)((void *)tg + cft->private) = ctx.v; | |
964 | else | |
965 | *(unsigned int *)((void *)tg + cft->private) = ctx.v; | |
af133ceb | 966 | |
632b4493 TH |
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 | } | |
60c2bc2d TH |
986 | |
987 | blkg_conf_finish(&ctx); | |
a2b1693b | 988 | return 0; |
8e89d13f VG |
989 | } |
990 | ||
af133ceb TH |
991 | static int tg_set_conf_u64(struct cgroup *cgrp, struct cftype *cft, |
992 | const char *buf) | |
60c2bc2d | 993 | { |
af133ceb | 994 | return tg_set_conf(cgrp, cft, buf, true); |
60c2bc2d TH |
995 | } |
996 | ||
af133ceb TH |
997 | static int tg_set_conf_uint(struct cgroup *cgrp, struct cftype *cft, |
998 | const char *buf) | |
60c2bc2d | 999 | { |
af133ceb | 1000 | return tg_set_conf(cgrp, cft, buf, false); |
60c2bc2d TH |
1001 | } |
1002 | ||
1003 | static struct cftype throtl_files[] = { | |
1004 | { | |
1005 | .name = "throttle.read_bps_device", | |
af133ceb TH |
1006 | .private = offsetof(struct throtl_grp, bps[READ]), |
1007 | .read_seq_string = tg_print_conf_u64, | |
1008 | .write_string = tg_set_conf_u64, | |
60c2bc2d TH |
1009 | .max_write_len = 256, |
1010 | }, | |
1011 | { | |
1012 | .name = "throttle.write_bps_device", | |
af133ceb TH |
1013 | .private = offsetof(struct throtl_grp, bps[WRITE]), |
1014 | .read_seq_string = tg_print_conf_u64, | |
1015 | .write_string = tg_set_conf_u64, | |
60c2bc2d TH |
1016 | .max_write_len = 256, |
1017 | }, | |
1018 | { | |
1019 | .name = "throttle.read_iops_device", | |
af133ceb TH |
1020 | .private = offsetof(struct throtl_grp, iops[READ]), |
1021 | .read_seq_string = tg_print_conf_uint, | |
1022 | .write_string = tg_set_conf_uint, | |
60c2bc2d TH |
1023 | .max_write_len = 256, |
1024 | }, | |
1025 | { | |
1026 | .name = "throttle.write_iops_device", | |
af133ceb TH |
1027 | .private = offsetof(struct throtl_grp, iops[WRITE]), |
1028 | .read_seq_string = tg_print_conf_uint, | |
1029 | .write_string = tg_set_conf_uint, | |
60c2bc2d TH |
1030 | .max_write_len = 256, |
1031 | }, | |
1032 | { | |
1033 | .name = "throttle.io_service_bytes", | |
5bc4afb1 | 1034 | .private = offsetof(struct tg_stats_cpu, service_bytes), |
8a3d2615 | 1035 | .read_seq_string = tg_print_cpu_rwstat, |
60c2bc2d TH |
1036 | }, |
1037 | { | |
1038 | .name = "throttle.io_serviced", | |
5bc4afb1 | 1039 | .private = offsetof(struct tg_stats_cpu, serviced), |
8a3d2615 | 1040 | .read_seq_string = tg_print_cpu_rwstat, |
60c2bc2d TH |
1041 | }, |
1042 | { } /* terminate */ | |
1043 | }; | |
1044 | ||
da527770 | 1045 | static void throtl_shutdown_wq(struct request_queue *q) |
e43473b7 VG |
1046 | { |
1047 | struct throtl_data *td = q->td; | |
1048 | ||
cb76199c | 1049 | cancel_delayed_work_sync(&td->dispatch_work); |
e43473b7 VG |
1050 | } |
1051 | ||
3c798398 | 1052 | static struct blkcg_policy blkcg_policy_throtl = { |
f9fcc2d3 TH |
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, | |
e43473b7 VG |
1059 | }; |
1060 | ||
bc16a4f9 | 1061 | bool blk_throtl_bio(struct request_queue *q, struct bio *bio) |
e43473b7 VG |
1062 | { |
1063 | struct throtl_data *td = q->td; | |
1064 | struct throtl_grp *tg; | |
e43473b7 | 1065 | bool rw = bio_data_dir(bio), update_disptime = true; |
3c798398 | 1066 | struct blkcg *blkcg; |
bc16a4f9 | 1067 | bool throttled = false; |
e43473b7 VG |
1068 | |
1069 | if (bio->bi_rw & REQ_THROTTLED) { | |
1070 | bio->bi_rw &= ~REQ_THROTTLED; | |
bc16a4f9 | 1071 | goto out; |
e43473b7 VG |
1072 | } |
1073 | ||
af75cd3c VG |
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 | */ | |
af75cd3c | 1079 | rcu_read_lock(); |
3c798398 | 1080 | blkcg = bio_blkcg(bio); |
cd1604fa | 1081 | tg = throtl_lookup_tg(td, blkcg); |
af75cd3c | 1082 | if (tg) { |
af75cd3c | 1083 | if (tg_no_rule_group(tg, rw)) { |
629ed0b1 TH |
1084 | throtl_update_dispatch_stats(tg_to_blkg(tg), |
1085 | bio->bi_size, bio->bi_rw); | |
2a7f1244 | 1086 | goto out_unlock_rcu; |
af75cd3c VG |
1087 | } |
1088 | } | |
af75cd3c VG |
1089 | |
1090 | /* | |
1091 | * Either group has not been allocated yet or it is not an unlimited | |
1092 | * IO group | |
1093 | */ | |
e43473b7 | 1094 | spin_lock_irq(q->queue_lock); |
cd1604fa | 1095 | tg = throtl_lookup_create_tg(td, blkcg); |
bc16a4f9 TH |
1096 | if (unlikely(!tg)) |
1097 | goto out_unlock; | |
f469a7b4 | 1098 | |
e43473b7 VG |
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 | */ | |
231d704b | 1104 | update_disptime = false; |
e43473b7 | 1105 | goto queue_bio; |
de701c74 | 1106 | |
e43473b7 VG |
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); | |
04521db0 VG |
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); | |
bc16a4f9 | 1125 | goto out_unlock; |
e43473b7 VG |
1126 | } |
1127 | ||
1128 | queue_bio: | |
fd16d263 | 1129 | throtl_log_tg(td, tg, "[%c] bio. bdisp=%llu sz=%u bps=%llu" |
8e89d13f VG |
1130 | " iodisp=%u iops=%u queued=%d/%d", |
1131 | rw == READ ? 'R' : 'W', | |
e43473b7 | 1132 | tg->bytes_disp[rw], bio->bi_size, tg->bps[rw], |
8e89d13f | 1133 | tg->io_disp[rw], tg->iops[rw], |
e43473b7 VG |
1134 | tg->nr_queued[READ], tg->nr_queued[WRITE]); |
1135 | ||
671058fb | 1136 | bio_associate_current(bio); |
e43473b7 | 1137 | throtl_add_bio_tg(q->td, tg, bio); |
bc16a4f9 | 1138 | throttled = true; |
e43473b7 VG |
1139 | |
1140 | if (update_disptime) { | |
1141 | tg_update_disptime(td, tg); | |
1142 | throtl_schedule_next_dispatch(td); | |
1143 | } | |
1144 | ||
bc16a4f9 | 1145 | out_unlock: |
e43473b7 | 1146 | spin_unlock_irq(q->queue_lock); |
2a7f1244 TH |
1147 | out_unlock_rcu: |
1148 | rcu_read_unlock(); | |
bc16a4f9 TH |
1149 | out: |
1150 | return throttled; | |
e43473b7 VG |
1151 | } |
1152 | ||
c9a929dd TH |
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 | ||
8bcb6c7d | 1168 | queue_lockdep_assert_held(q); |
c9a929dd TH |
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 | ||
e43473b7 VG |
1188 | int blk_throtl_init(struct request_queue *q) |
1189 | { | |
1190 | struct throtl_data *td; | |
a2b1693b | 1191 | int ret; |
e43473b7 VG |
1192 | |
1193 | td = kzalloc_node(sizeof(*td), GFP_KERNEL, q->node); | |
1194 | if (!td) | |
1195 | return -ENOMEM; | |
1196 | ||
e43473b7 | 1197 | td->tg_service_tree = THROTL_RB_ROOT; |
cb76199c | 1198 | INIT_DELAYED_WORK(&td->dispatch_work, blk_throtl_dispatch_work_fn); |
e43473b7 | 1199 | |
cd1604fa | 1200 | q->td = td; |
29b12589 | 1201 | td->queue = q; |
02977e4a | 1202 | |
a2b1693b | 1203 | /* activate policy */ |
3c798398 | 1204 | ret = blkcg_activate_policy(q, &blkcg_policy_throtl); |
a2b1693b | 1205 | if (ret) |
f51b802c | 1206 | kfree(td); |
a2b1693b | 1207 | return ret; |
e43473b7 VG |
1208 | } |
1209 | ||
1210 | void blk_throtl_exit(struct request_queue *q) | |
1211 | { | |
c875f4d0 | 1212 | BUG_ON(!q->td); |
da527770 | 1213 | throtl_shutdown_wq(q); |
3c798398 | 1214 | blkcg_deactivate_policy(q, &blkcg_policy_throtl); |
c9a929dd | 1215 | kfree(q->td); |
e43473b7 VG |
1216 | } |
1217 | ||
1218 | static int __init throtl_init(void) | |
1219 | { | |
450adcbe VG |
1220 | kthrotld_workqueue = alloc_workqueue("kthrotld", WQ_MEM_RECLAIM, 0); |
1221 | if (!kthrotld_workqueue) | |
1222 | panic("Failed to create kthrotld\n"); | |
1223 | ||
3c798398 | 1224 | return blkcg_policy_register(&blkcg_policy_throtl); |
e43473b7 VG |
1225 | } |
1226 | ||
1227 | module_init(throtl_init); |