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