]>
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> | |
eea8f41c | 12 | #include <linux/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 | ||
d61fcfa4 SL |
21 | /* Throttling is performed over a slice and after that slice is renewed */ |
22 | #define DFL_THROTL_SLICE_HD (HZ / 10) | |
23 | #define DFL_THROTL_SLICE_SSD (HZ / 50) | |
297e3d85 | 24 | #define MAX_THROTL_SLICE (HZ) |
9e234eea SL |
25 | #define DFL_IDLE_THRESHOLD_SSD (1000L) /* 1 ms */ |
26 | #define DFL_IDLE_THRESHOLD_HD (100L * 1000) /* 100 ms */ | |
27 | #define MAX_IDLE_TIME (5L * 1000 * 1000) /* 5 s */ | |
e43473b7 | 28 | |
3c798398 | 29 | static struct blkcg_policy blkcg_policy_throtl; |
0381411e | 30 | |
450adcbe VG |
31 | /* A workqueue to queue throttle related work */ |
32 | static struct workqueue_struct *kthrotld_workqueue; | |
450adcbe | 33 | |
c5cc2070 TH |
34 | /* |
35 | * To implement hierarchical throttling, throtl_grps form a tree and bios | |
36 | * are dispatched upwards level by level until they reach the top and get | |
37 | * issued. When dispatching bios from the children and local group at each | |
38 | * level, if the bios are dispatched into a single bio_list, there's a risk | |
39 | * of a local or child group which can queue many bios at once filling up | |
40 | * the list starving others. | |
41 | * | |
42 | * To avoid such starvation, dispatched bios are queued separately | |
43 | * according to where they came from. When they are again dispatched to | |
44 | * the parent, they're popped in round-robin order so that no single source | |
45 | * hogs the dispatch window. | |
46 | * | |
47 | * throtl_qnode is used to keep the queued bios separated by their sources. | |
48 | * Bios are queued to throtl_qnode which in turn is queued to | |
49 | * throtl_service_queue and then dispatched in round-robin order. | |
50 | * | |
51 | * It's also used to track the reference counts on blkg's. A qnode always | |
52 | * belongs to a throtl_grp and gets queued on itself or the parent, so | |
53 | * incrementing the reference of the associated throtl_grp when a qnode is | |
54 | * queued and decrementing when dequeued is enough to keep the whole blkg | |
55 | * tree pinned while bios are in flight. | |
56 | */ | |
57 | struct throtl_qnode { | |
58 | struct list_head node; /* service_queue->queued[] */ | |
59 | struct bio_list bios; /* queued bios */ | |
60 | struct throtl_grp *tg; /* tg this qnode belongs to */ | |
61 | }; | |
62 | ||
c9e0332e | 63 | struct throtl_service_queue { |
77216b04 TH |
64 | struct throtl_service_queue *parent_sq; /* the parent service_queue */ |
65 | ||
73f0d49a TH |
66 | /* |
67 | * Bios queued directly to this service_queue or dispatched from | |
68 | * children throtl_grp's. | |
69 | */ | |
c5cc2070 | 70 | struct list_head queued[2]; /* throtl_qnode [READ/WRITE] */ |
73f0d49a TH |
71 | unsigned int nr_queued[2]; /* number of queued bios */ |
72 | ||
73 | /* | |
74 | * RB tree of active children throtl_grp's, which are sorted by | |
75 | * their ->disptime. | |
76 | */ | |
c9e0332e TH |
77 | struct rb_root pending_tree; /* RB tree of active tgs */ |
78 | struct rb_node *first_pending; /* first node in the tree */ | |
79 | unsigned int nr_pending; /* # queued in the tree */ | |
80 | unsigned long first_pending_disptime; /* disptime of the first tg */ | |
69df0ab0 | 81 | struct timer_list pending_timer; /* fires on first_pending_disptime */ |
e43473b7 VG |
82 | }; |
83 | ||
5b2c16aa TH |
84 | enum tg_state_flags { |
85 | THROTL_TG_PENDING = 1 << 0, /* on parent's pending tree */ | |
0e9f4164 | 86 | THROTL_TG_WAS_EMPTY = 1 << 1, /* bio_lists[] became non-empty */ |
5b2c16aa TH |
87 | }; |
88 | ||
e43473b7 VG |
89 | #define rb_entry_tg(node) rb_entry((node), struct throtl_grp, rb_node) |
90 | ||
9f626e37 | 91 | enum { |
cd5ab1b0 | 92 | LIMIT_LOW, |
9f626e37 SL |
93 | LIMIT_MAX, |
94 | LIMIT_CNT, | |
95 | }; | |
96 | ||
e43473b7 | 97 | struct throtl_grp { |
f95a04af TH |
98 | /* must be the first member */ |
99 | struct blkg_policy_data pd; | |
100 | ||
c9e0332e | 101 | /* active throtl group service_queue member */ |
e43473b7 VG |
102 | struct rb_node rb_node; |
103 | ||
0f3457f6 TH |
104 | /* throtl_data this group belongs to */ |
105 | struct throtl_data *td; | |
106 | ||
49a2f1e3 TH |
107 | /* this group's service queue */ |
108 | struct throtl_service_queue service_queue; | |
109 | ||
c5cc2070 TH |
110 | /* |
111 | * qnode_on_self is used when bios are directly queued to this | |
112 | * throtl_grp so that local bios compete fairly with bios | |
113 | * dispatched from children. qnode_on_parent is used when bios are | |
114 | * dispatched from this throtl_grp into its parent and will compete | |
115 | * with the sibling qnode_on_parents and the parent's | |
116 | * qnode_on_self. | |
117 | */ | |
118 | struct throtl_qnode qnode_on_self[2]; | |
119 | struct throtl_qnode qnode_on_parent[2]; | |
120 | ||
e43473b7 VG |
121 | /* |
122 | * Dispatch time in jiffies. This is the estimated time when group | |
123 | * will unthrottle and is ready to dispatch more bio. It is used as | |
124 | * key to sort active groups in service tree. | |
125 | */ | |
126 | unsigned long disptime; | |
127 | ||
e43473b7 VG |
128 | unsigned int flags; |
129 | ||
693e751e TH |
130 | /* are there any throtl rules between this group and td? */ |
131 | bool has_rules[2]; | |
132 | ||
cd5ab1b0 | 133 | /* internally used bytes per second rate limits */ |
9f626e37 | 134 | uint64_t bps[2][LIMIT_CNT]; |
cd5ab1b0 SL |
135 | /* user configured bps limits */ |
136 | uint64_t bps_conf[2][LIMIT_CNT]; | |
e43473b7 | 137 | |
cd5ab1b0 | 138 | /* internally used IOPS limits */ |
9f626e37 | 139 | unsigned int iops[2][LIMIT_CNT]; |
cd5ab1b0 SL |
140 | /* user configured IOPS limits */ |
141 | unsigned int iops_conf[2][LIMIT_CNT]; | |
8e89d13f | 142 | |
e43473b7 VG |
143 | /* Number of bytes disptached in current slice */ |
144 | uint64_t bytes_disp[2]; | |
8e89d13f VG |
145 | /* Number of bio's dispatched in current slice */ |
146 | unsigned int io_disp[2]; | |
e43473b7 | 147 | |
3f0abd80 SL |
148 | unsigned long last_low_overflow_time[2]; |
149 | ||
150 | uint64_t last_bytes_disp[2]; | |
151 | unsigned int last_io_disp[2]; | |
152 | ||
153 | unsigned long last_check_time; | |
154 | ||
aec24246 SL |
155 | unsigned long last_dispatch_time[2]; |
156 | ||
e43473b7 VG |
157 | /* When did we start a new slice */ |
158 | unsigned long slice_start[2]; | |
159 | unsigned long slice_end[2]; | |
9e234eea SL |
160 | |
161 | unsigned long last_finish_time; /* ns / 1024 */ | |
162 | unsigned long checked_last_finish_time; /* ns / 1024 */ | |
163 | unsigned long avg_idletime; /* ns / 1024 */ | |
164 | unsigned long idletime_threshold; /* us */ | |
e43473b7 VG |
165 | }; |
166 | ||
167 | struct throtl_data | |
168 | { | |
e43473b7 | 169 | /* service tree for active throtl groups */ |
c9e0332e | 170 | struct throtl_service_queue service_queue; |
e43473b7 | 171 | |
e43473b7 VG |
172 | struct request_queue *queue; |
173 | ||
174 | /* Total Number of queued bios on READ and WRITE lists */ | |
175 | unsigned int nr_queued[2]; | |
176 | ||
297e3d85 SL |
177 | unsigned int throtl_slice; |
178 | ||
e43473b7 | 179 | /* Work for dispatching throttled bios */ |
69df0ab0 | 180 | struct work_struct dispatch_work; |
9f626e37 SL |
181 | unsigned int limit_index; |
182 | bool limit_valid[LIMIT_CNT]; | |
3f0abd80 | 183 | |
ada75b6e SL |
184 | unsigned long dft_idletime_threshold; /* us */ |
185 | ||
3f0abd80 SL |
186 | unsigned long low_upgrade_time; |
187 | unsigned long low_downgrade_time; | |
7394e31f SL |
188 | |
189 | unsigned int scale; | |
e43473b7 VG |
190 | }; |
191 | ||
69df0ab0 TH |
192 | static void throtl_pending_timer_fn(unsigned long arg); |
193 | ||
f95a04af TH |
194 | static inline struct throtl_grp *pd_to_tg(struct blkg_policy_data *pd) |
195 | { | |
196 | return pd ? container_of(pd, struct throtl_grp, pd) : NULL; | |
197 | } | |
198 | ||
3c798398 | 199 | static inline struct throtl_grp *blkg_to_tg(struct blkcg_gq *blkg) |
0381411e | 200 | { |
f95a04af | 201 | return pd_to_tg(blkg_to_pd(blkg, &blkcg_policy_throtl)); |
0381411e TH |
202 | } |
203 | ||
3c798398 | 204 | static inline struct blkcg_gq *tg_to_blkg(struct throtl_grp *tg) |
0381411e | 205 | { |
f95a04af | 206 | return pd_to_blkg(&tg->pd); |
0381411e TH |
207 | } |
208 | ||
fda6f272 TH |
209 | /** |
210 | * sq_to_tg - return the throl_grp the specified service queue belongs to | |
211 | * @sq: the throtl_service_queue of interest | |
212 | * | |
213 | * Return the throtl_grp @sq belongs to. If @sq is the top-level one | |
214 | * embedded in throtl_data, %NULL is returned. | |
215 | */ | |
216 | static struct throtl_grp *sq_to_tg(struct throtl_service_queue *sq) | |
217 | { | |
218 | if (sq && sq->parent_sq) | |
219 | return container_of(sq, struct throtl_grp, service_queue); | |
220 | else | |
221 | return NULL; | |
222 | } | |
223 | ||
224 | /** | |
225 | * sq_to_td - return throtl_data the specified service queue belongs to | |
226 | * @sq: the throtl_service_queue of interest | |
227 | * | |
b43daedc | 228 | * A service_queue can be embedded in either a throtl_grp or throtl_data. |
fda6f272 TH |
229 | * Determine the associated throtl_data accordingly and return it. |
230 | */ | |
231 | static struct throtl_data *sq_to_td(struct throtl_service_queue *sq) | |
232 | { | |
233 | struct throtl_grp *tg = sq_to_tg(sq); | |
234 | ||
235 | if (tg) | |
236 | return tg->td; | |
237 | else | |
238 | return container_of(sq, struct throtl_data, service_queue); | |
239 | } | |
240 | ||
7394e31f SL |
241 | /* |
242 | * cgroup's limit in LIMIT_MAX is scaled if low limit is set. This scale is to | |
243 | * make the IO dispatch more smooth. | |
244 | * Scale up: linearly scale up according to lapsed time since upgrade. For | |
245 | * every throtl_slice, the limit scales up 1/2 .low limit till the | |
246 | * limit hits .max limit | |
247 | * Scale down: exponentially scale down if a cgroup doesn't hit its .low limit | |
248 | */ | |
249 | static uint64_t throtl_adjusted_limit(uint64_t low, struct throtl_data *td) | |
250 | { | |
251 | /* arbitrary value to avoid too big scale */ | |
252 | if (td->scale < 4096 && time_after_eq(jiffies, | |
253 | td->low_upgrade_time + td->scale * td->throtl_slice)) | |
254 | td->scale = (jiffies - td->low_upgrade_time) / td->throtl_slice; | |
255 | ||
256 | return low + (low >> 1) * td->scale; | |
257 | } | |
258 | ||
9f626e37 SL |
259 | static uint64_t tg_bps_limit(struct throtl_grp *tg, int rw) |
260 | { | |
b22c417c | 261 | struct blkcg_gq *blkg = tg_to_blkg(tg); |
7394e31f | 262 | struct throtl_data *td; |
b22c417c SL |
263 | uint64_t ret; |
264 | ||
265 | if (cgroup_subsys_on_dfl(io_cgrp_subsys) && !blkg->parent) | |
266 | return U64_MAX; | |
7394e31f SL |
267 | |
268 | td = tg->td; | |
269 | ret = tg->bps[rw][td->limit_index]; | |
270 | if (ret == 0 && td->limit_index == LIMIT_LOW) | |
b22c417c | 271 | return tg->bps[rw][LIMIT_MAX]; |
7394e31f SL |
272 | |
273 | if (td->limit_index == LIMIT_MAX && tg->bps[rw][LIMIT_LOW] && | |
274 | tg->bps[rw][LIMIT_LOW] != tg->bps[rw][LIMIT_MAX]) { | |
275 | uint64_t adjusted; | |
276 | ||
277 | adjusted = throtl_adjusted_limit(tg->bps[rw][LIMIT_LOW], td); | |
278 | ret = min(tg->bps[rw][LIMIT_MAX], adjusted); | |
279 | } | |
b22c417c | 280 | return ret; |
9f626e37 SL |
281 | } |
282 | ||
283 | static unsigned int tg_iops_limit(struct throtl_grp *tg, int rw) | |
284 | { | |
b22c417c | 285 | struct blkcg_gq *blkg = tg_to_blkg(tg); |
7394e31f | 286 | struct throtl_data *td; |
b22c417c SL |
287 | unsigned int ret; |
288 | ||
289 | if (cgroup_subsys_on_dfl(io_cgrp_subsys) && !blkg->parent) | |
290 | return UINT_MAX; | |
7394e31f SL |
291 | td = tg->td; |
292 | ret = tg->iops[rw][td->limit_index]; | |
b22c417c SL |
293 | if (ret == 0 && tg->td->limit_index == LIMIT_LOW) |
294 | return tg->iops[rw][LIMIT_MAX]; | |
7394e31f SL |
295 | |
296 | if (td->limit_index == LIMIT_MAX && tg->iops[rw][LIMIT_LOW] && | |
297 | tg->iops[rw][LIMIT_LOW] != tg->iops[rw][LIMIT_MAX]) { | |
298 | uint64_t adjusted; | |
299 | ||
300 | adjusted = throtl_adjusted_limit(tg->iops[rw][LIMIT_LOW], td); | |
301 | if (adjusted > UINT_MAX) | |
302 | adjusted = UINT_MAX; | |
303 | ret = min_t(unsigned int, tg->iops[rw][LIMIT_MAX], adjusted); | |
304 | } | |
b22c417c | 305 | return ret; |
9f626e37 SL |
306 | } |
307 | ||
fda6f272 TH |
308 | /** |
309 | * throtl_log - log debug message via blktrace | |
310 | * @sq: the service_queue being reported | |
311 | * @fmt: printf format string | |
312 | * @args: printf args | |
313 | * | |
314 | * The messages are prefixed with "throtl BLKG_NAME" if @sq belongs to a | |
315 | * throtl_grp; otherwise, just "throtl". | |
fda6f272 TH |
316 | */ |
317 | #define throtl_log(sq, fmt, args...) do { \ | |
318 | struct throtl_grp *__tg = sq_to_tg((sq)); \ | |
319 | struct throtl_data *__td = sq_to_td((sq)); \ | |
320 | \ | |
321 | (void)__td; \ | |
59fa0224 SL |
322 | if (likely(!blk_trace_note_message_enabled(__td->queue))) \ |
323 | break; \ | |
fda6f272 TH |
324 | if ((__tg)) { \ |
325 | char __pbuf[128]; \ | |
54e7ed12 | 326 | \ |
fda6f272 TH |
327 | blkg_path(tg_to_blkg(__tg), __pbuf, sizeof(__pbuf)); \ |
328 | blk_add_trace_msg(__td->queue, "throtl %s " fmt, __pbuf, ##args); \ | |
329 | } else { \ | |
330 | blk_add_trace_msg(__td->queue, "throtl " fmt, ##args); \ | |
331 | } \ | |
54e7ed12 | 332 | } while (0) |
e43473b7 | 333 | |
c5cc2070 TH |
334 | static void throtl_qnode_init(struct throtl_qnode *qn, struct throtl_grp *tg) |
335 | { | |
336 | INIT_LIST_HEAD(&qn->node); | |
337 | bio_list_init(&qn->bios); | |
338 | qn->tg = tg; | |
339 | } | |
340 | ||
341 | /** | |
342 | * throtl_qnode_add_bio - add a bio to a throtl_qnode and activate it | |
343 | * @bio: bio being added | |
344 | * @qn: qnode to add bio to | |
345 | * @queued: the service_queue->queued[] list @qn belongs to | |
346 | * | |
347 | * Add @bio to @qn and put @qn on @queued if it's not already on. | |
348 | * @qn->tg's reference count is bumped when @qn is activated. See the | |
349 | * comment on top of throtl_qnode definition for details. | |
350 | */ | |
351 | static void throtl_qnode_add_bio(struct bio *bio, struct throtl_qnode *qn, | |
352 | struct list_head *queued) | |
353 | { | |
354 | bio_list_add(&qn->bios, bio); | |
355 | if (list_empty(&qn->node)) { | |
356 | list_add_tail(&qn->node, queued); | |
357 | blkg_get(tg_to_blkg(qn->tg)); | |
358 | } | |
359 | } | |
360 | ||
361 | /** | |
362 | * throtl_peek_queued - peek the first bio on a qnode list | |
363 | * @queued: the qnode list to peek | |
364 | */ | |
365 | static struct bio *throtl_peek_queued(struct list_head *queued) | |
366 | { | |
367 | struct throtl_qnode *qn = list_first_entry(queued, struct throtl_qnode, node); | |
368 | struct bio *bio; | |
369 | ||
370 | if (list_empty(queued)) | |
371 | return NULL; | |
372 | ||
373 | bio = bio_list_peek(&qn->bios); | |
374 | WARN_ON_ONCE(!bio); | |
375 | return bio; | |
376 | } | |
377 | ||
378 | /** | |
379 | * throtl_pop_queued - pop the first bio form a qnode list | |
380 | * @queued: the qnode list to pop a bio from | |
381 | * @tg_to_put: optional out argument for throtl_grp to put | |
382 | * | |
383 | * Pop the first bio from the qnode list @queued. After popping, the first | |
384 | * qnode is removed from @queued if empty or moved to the end of @queued so | |
385 | * that the popping order is round-robin. | |
386 | * | |
387 | * When the first qnode is removed, its associated throtl_grp should be put | |
388 | * too. If @tg_to_put is NULL, this function automatically puts it; | |
389 | * otherwise, *@tg_to_put is set to the throtl_grp to put and the caller is | |
390 | * responsible for putting it. | |
391 | */ | |
392 | static struct bio *throtl_pop_queued(struct list_head *queued, | |
393 | struct throtl_grp **tg_to_put) | |
394 | { | |
395 | struct throtl_qnode *qn = list_first_entry(queued, struct throtl_qnode, node); | |
396 | struct bio *bio; | |
397 | ||
398 | if (list_empty(queued)) | |
399 | return NULL; | |
400 | ||
401 | bio = bio_list_pop(&qn->bios); | |
402 | WARN_ON_ONCE(!bio); | |
403 | ||
404 | if (bio_list_empty(&qn->bios)) { | |
405 | list_del_init(&qn->node); | |
406 | if (tg_to_put) | |
407 | *tg_to_put = qn->tg; | |
408 | else | |
409 | blkg_put(tg_to_blkg(qn->tg)); | |
410 | } else { | |
411 | list_move_tail(&qn->node, queued); | |
412 | } | |
413 | ||
414 | return bio; | |
415 | } | |
416 | ||
49a2f1e3 | 417 | /* init a service_queue, assumes the caller zeroed it */ |
b2ce2643 | 418 | static void throtl_service_queue_init(struct throtl_service_queue *sq) |
49a2f1e3 | 419 | { |
c5cc2070 TH |
420 | INIT_LIST_HEAD(&sq->queued[0]); |
421 | INIT_LIST_HEAD(&sq->queued[1]); | |
49a2f1e3 | 422 | sq->pending_tree = RB_ROOT; |
69df0ab0 TH |
423 | setup_timer(&sq->pending_timer, throtl_pending_timer_fn, |
424 | (unsigned long)sq); | |
425 | } | |
426 | ||
001bea73 TH |
427 | static struct blkg_policy_data *throtl_pd_alloc(gfp_t gfp, int node) |
428 | { | |
4fb72036 | 429 | struct throtl_grp *tg; |
24bdb8ef | 430 | int rw; |
4fb72036 TH |
431 | |
432 | tg = kzalloc_node(sizeof(*tg), gfp, node); | |
433 | if (!tg) | |
77ea7338 | 434 | return NULL; |
4fb72036 | 435 | |
b2ce2643 TH |
436 | throtl_service_queue_init(&tg->service_queue); |
437 | ||
438 | for (rw = READ; rw <= WRITE; rw++) { | |
439 | throtl_qnode_init(&tg->qnode_on_self[rw], tg); | |
440 | throtl_qnode_init(&tg->qnode_on_parent[rw], tg); | |
441 | } | |
442 | ||
443 | RB_CLEAR_NODE(&tg->rb_node); | |
9f626e37 SL |
444 | tg->bps[READ][LIMIT_MAX] = U64_MAX; |
445 | tg->bps[WRITE][LIMIT_MAX] = U64_MAX; | |
446 | tg->iops[READ][LIMIT_MAX] = UINT_MAX; | |
447 | tg->iops[WRITE][LIMIT_MAX] = UINT_MAX; | |
cd5ab1b0 SL |
448 | tg->bps_conf[READ][LIMIT_MAX] = U64_MAX; |
449 | tg->bps_conf[WRITE][LIMIT_MAX] = U64_MAX; | |
450 | tg->iops_conf[READ][LIMIT_MAX] = UINT_MAX; | |
451 | tg->iops_conf[WRITE][LIMIT_MAX] = UINT_MAX; | |
452 | /* LIMIT_LOW will have default value 0 */ | |
b2ce2643 | 453 | |
4fb72036 | 454 | return &tg->pd; |
001bea73 TH |
455 | } |
456 | ||
a9520cd6 | 457 | static void throtl_pd_init(struct blkg_policy_data *pd) |
a29a171e | 458 | { |
a9520cd6 TH |
459 | struct throtl_grp *tg = pd_to_tg(pd); |
460 | struct blkcg_gq *blkg = tg_to_blkg(tg); | |
77216b04 | 461 | struct throtl_data *td = blkg->q->td; |
b2ce2643 | 462 | struct throtl_service_queue *sq = &tg->service_queue; |
cd1604fa | 463 | |
9138125b | 464 | /* |
aa6ec29b | 465 | * If on the default hierarchy, we switch to properly hierarchical |
9138125b TH |
466 | * behavior where limits on a given throtl_grp are applied to the |
467 | * whole subtree rather than just the group itself. e.g. If 16M | |
468 | * read_bps limit is set on the root group, the whole system can't | |
469 | * exceed 16M for the device. | |
470 | * | |
aa6ec29b | 471 | * If not on the default hierarchy, the broken flat hierarchy |
9138125b TH |
472 | * behavior is retained where all throtl_grps are treated as if |
473 | * they're all separate root groups right below throtl_data. | |
474 | * Limits of a group don't interact with limits of other groups | |
475 | * regardless of the position of the group in the hierarchy. | |
476 | */ | |
b2ce2643 | 477 | sq->parent_sq = &td->service_queue; |
9e10a130 | 478 | if (cgroup_subsys_on_dfl(io_cgrp_subsys) && blkg->parent) |
b2ce2643 | 479 | sq->parent_sq = &blkg_to_tg(blkg->parent)->service_queue; |
77216b04 | 480 | tg->td = td; |
9e234eea | 481 | |
ada75b6e | 482 | tg->idletime_threshold = td->dft_idletime_threshold; |
8a3d2615 TH |
483 | } |
484 | ||
693e751e TH |
485 | /* |
486 | * Set has_rules[] if @tg or any of its parents have limits configured. | |
487 | * This doesn't require walking up to the top of the hierarchy as the | |
488 | * parent's has_rules[] is guaranteed to be correct. | |
489 | */ | |
490 | static void tg_update_has_rules(struct throtl_grp *tg) | |
491 | { | |
492 | struct throtl_grp *parent_tg = sq_to_tg(tg->service_queue.parent_sq); | |
9f626e37 | 493 | struct throtl_data *td = tg->td; |
693e751e TH |
494 | int rw; |
495 | ||
496 | for (rw = READ; rw <= WRITE; rw++) | |
497 | tg->has_rules[rw] = (parent_tg && parent_tg->has_rules[rw]) || | |
9f626e37 SL |
498 | (td->limit_valid[td->limit_index] && |
499 | (tg_bps_limit(tg, rw) != U64_MAX || | |
500 | tg_iops_limit(tg, rw) != UINT_MAX)); | |
693e751e TH |
501 | } |
502 | ||
a9520cd6 | 503 | static void throtl_pd_online(struct blkg_policy_data *pd) |
693e751e | 504 | { |
aec24246 | 505 | struct throtl_grp *tg = pd_to_tg(pd); |
693e751e TH |
506 | /* |
507 | * We don't want new groups to escape the limits of its ancestors. | |
508 | * Update has_rules[] after a new group is brought online. | |
509 | */ | |
aec24246 SL |
510 | tg_update_has_rules(tg); |
511 | tg->last_dispatch_time[READ] = jiffies; | |
512 | tg->last_dispatch_time[WRITE] = jiffies; | |
693e751e TH |
513 | } |
514 | ||
cd5ab1b0 SL |
515 | static void blk_throtl_update_limit_valid(struct throtl_data *td) |
516 | { | |
517 | struct cgroup_subsys_state *pos_css; | |
518 | struct blkcg_gq *blkg; | |
519 | bool low_valid = false; | |
520 | ||
521 | rcu_read_lock(); | |
522 | blkg_for_each_descendant_post(blkg, pos_css, td->queue->root_blkg) { | |
523 | struct throtl_grp *tg = blkg_to_tg(blkg); | |
524 | ||
525 | if (tg->bps[READ][LIMIT_LOW] || tg->bps[WRITE][LIMIT_LOW] || | |
526 | tg->iops[READ][LIMIT_LOW] || tg->iops[WRITE][LIMIT_LOW]) | |
527 | low_valid = true; | |
528 | } | |
529 | rcu_read_unlock(); | |
530 | ||
531 | td->limit_valid[LIMIT_LOW] = low_valid; | |
532 | } | |
533 | ||
c79892c5 | 534 | static void throtl_upgrade_state(struct throtl_data *td); |
cd5ab1b0 SL |
535 | static void throtl_pd_offline(struct blkg_policy_data *pd) |
536 | { | |
537 | struct throtl_grp *tg = pd_to_tg(pd); | |
538 | ||
539 | tg->bps[READ][LIMIT_LOW] = 0; | |
540 | tg->bps[WRITE][LIMIT_LOW] = 0; | |
541 | tg->iops[READ][LIMIT_LOW] = 0; | |
542 | tg->iops[WRITE][LIMIT_LOW] = 0; | |
543 | ||
544 | blk_throtl_update_limit_valid(tg->td); | |
545 | ||
c79892c5 SL |
546 | if (!tg->td->limit_valid[tg->td->limit_index]) |
547 | throtl_upgrade_state(tg->td); | |
cd5ab1b0 SL |
548 | } |
549 | ||
001bea73 TH |
550 | static void throtl_pd_free(struct blkg_policy_data *pd) |
551 | { | |
4fb72036 TH |
552 | struct throtl_grp *tg = pd_to_tg(pd); |
553 | ||
b2ce2643 | 554 | del_timer_sync(&tg->service_queue.pending_timer); |
4fb72036 | 555 | kfree(tg); |
001bea73 TH |
556 | } |
557 | ||
0049af73 TH |
558 | static struct throtl_grp * |
559 | throtl_rb_first(struct throtl_service_queue *parent_sq) | |
e43473b7 VG |
560 | { |
561 | /* Service tree is empty */ | |
0049af73 | 562 | if (!parent_sq->nr_pending) |
e43473b7 VG |
563 | return NULL; |
564 | ||
0049af73 TH |
565 | if (!parent_sq->first_pending) |
566 | parent_sq->first_pending = rb_first(&parent_sq->pending_tree); | |
e43473b7 | 567 | |
0049af73 TH |
568 | if (parent_sq->first_pending) |
569 | return rb_entry_tg(parent_sq->first_pending); | |
e43473b7 VG |
570 | |
571 | return NULL; | |
572 | } | |
573 | ||
574 | static void rb_erase_init(struct rb_node *n, struct rb_root *root) | |
575 | { | |
576 | rb_erase(n, root); | |
577 | RB_CLEAR_NODE(n); | |
578 | } | |
579 | ||
0049af73 TH |
580 | static void throtl_rb_erase(struct rb_node *n, |
581 | struct throtl_service_queue *parent_sq) | |
e43473b7 | 582 | { |
0049af73 TH |
583 | if (parent_sq->first_pending == n) |
584 | parent_sq->first_pending = NULL; | |
585 | rb_erase_init(n, &parent_sq->pending_tree); | |
586 | --parent_sq->nr_pending; | |
e43473b7 VG |
587 | } |
588 | ||
0049af73 | 589 | static void update_min_dispatch_time(struct throtl_service_queue *parent_sq) |
e43473b7 VG |
590 | { |
591 | struct throtl_grp *tg; | |
592 | ||
0049af73 | 593 | tg = throtl_rb_first(parent_sq); |
e43473b7 VG |
594 | if (!tg) |
595 | return; | |
596 | ||
0049af73 | 597 | parent_sq->first_pending_disptime = tg->disptime; |
e43473b7 VG |
598 | } |
599 | ||
77216b04 | 600 | static void tg_service_queue_add(struct throtl_grp *tg) |
e43473b7 | 601 | { |
77216b04 | 602 | struct throtl_service_queue *parent_sq = tg->service_queue.parent_sq; |
0049af73 | 603 | struct rb_node **node = &parent_sq->pending_tree.rb_node; |
e43473b7 VG |
604 | struct rb_node *parent = NULL; |
605 | struct throtl_grp *__tg; | |
606 | unsigned long key = tg->disptime; | |
607 | int left = 1; | |
608 | ||
609 | while (*node != NULL) { | |
610 | parent = *node; | |
611 | __tg = rb_entry_tg(parent); | |
612 | ||
613 | if (time_before(key, __tg->disptime)) | |
614 | node = &parent->rb_left; | |
615 | else { | |
616 | node = &parent->rb_right; | |
617 | left = 0; | |
618 | } | |
619 | } | |
620 | ||
621 | if (left) | |
0049af73 | 622 | parent_sq->first_pending = &tg->rb_node; |
e43473b7 VG |
623 | |
624 | rb_link_node(&tg->rb_node, parent, node); | |
0049af73 | 625 | rb_insert_color(&tg->rb_node, &parent_sq->pending_tree); |
e43473b7 VG |
626 | } |
627 | ||
77216b04 | 628 | static void __throtl_enqueue_tg(struct throtl_grp *tg) |
e43473b7 | 629 | { |
77216b04 | 630 | tg_service_queue_add(tg); |
5b2c16aa | 631 | tg->flags |= THROTL_TG_PENDING; |
77216b04 | 632 | tg->service_queue.parent_sq->nr_pending++; |
e43473b7 VG |
633 | } |
634 | ||
77216b04 | 635 | static void throtl_enqueue_tg(struct throtl_grp *tg) |
e43473b7 | 636 | { |
5b2c16aa | 637 | if (!(tg->flags & THROTL_TG_PENDING)) |
77216b04 | 638 | __throtl_enqueue_tg(tg); |
e43473b7 VG |
639 | } |
640 | ||
77216b04 | 641 | static void __throtl_dequeue_tg(struct throtl_grp *tg) |
e43473b7 | 642 | { |
77216b04 | 643 | throtl_rb_erase(&tg->rb_node, tg->service_queue.parent_sq); |
5b2c16aa | 644 | tg->flags &= ~THROTL_TG_PENDING; |
e43473b7 VG |
645 | } |
646 | ||
77216b04 | 647 | static void throtl_dequeue_tg(struct throtl_grp *tg) |
e43473b7 | 648 | { |
5b2c16aa | 649 | if (tg->flags & THROTL_TG_PENDING) |
77216b04 | 650 | __throtl_dequeue_tg(tg); |
e43473b7 VG |
651 | } |
652 | ||
a9131a27 | 653 | /* Call with queue lock held */ |
69df0ab0 TH |
654 | static void throtl_schedule_pending_timer(struct throtl_service_queue *sq, |
655 | unsigned long expires) | |
a9131a27 | 656 | { |
297e3d85 | 657 | unsigned long max_expire = jiffies + 8 * sq_to_tg(sq)->td->throtl_slice; |
06cceedc SL |
658 | |
659 | /* | |
660 | * Since we are adjusting the throttle limit dynamically, the sleep | |
661 | * time calculated according to previous limit might be invalid. It's | |
662 | * possible the cgroup sleep time is very long and no other cgroups | |
663 | * have IO running so notify the limit changes. Make sure the cgroup | |
664 | * doesn't sleep too long to avoid the missed notification. | |
665 | */ | |
666 | if (time_after(expires, max_expire)) | |
667 | expires = max_expire; | |
69df0ab0 TH |
668 | mod_timer(&sq->pending_timer, expires); |
669 | throtl_log(sq, "schedule timer. delay=%lu jiffies=%lu", | |
670 | expires - jiffies, jiffies); | |
a9131a27 TH |
671 | } |
672 | ||
7f52f98c TH |
673 | /** |
674 | * throtl_schedule_next_dispatch - schedule the next dispatch cycle | |
675 | * @sq: the service_queue to schedule dispatch for | |
676 | * @force: force scheduling | |
677 | * | |
678 | * Arm @sq->pending_timer so that the next dispatch cycle starts on the | |
679 | * dispatch time of the first pending child. Returns %true if either timer | |
680 | * is armed or there's no pending child left. %false if the current | |
681 | * dispatch window is still open and the caller should continue | |
682 | * dispatching. | |
683 | * | |
684 | * If @force is %true, the dispatch timer is always scheduled and this | |
685 | * function is guaranteed to return %true. This is to be used when the | |
686 | * caller can't dispatch itself and needs to invoke pending_timer | |
687 | * unconditionally. Note that forced scheduling is likely to induce short | |
688 | * delay before dispatch starts even if @sq->first_pending_disptime is not | |
689 | * in the future and thus shouldn't be used in hot paths. | |
690 | */ | |
691 | static bool throtl_schedule_next_dispatch(struct throtl_service_queue *sq, | |
692 | bool force) | |
e43473b7 | 693 | { |
6a525600 | 694 | /* any pending children left? */ |
c9e0332e | 695 | if (!sq->nr_pending) |
7f52f98c | 696 | return true; |
e43473b7 | 697 | |
c9e0332e | 698 | update_min_dispatch_time(sq); |
e43473b7 | 699 | |
69df0ab0 | 700 | /* is the next dispatch time in the future? */ |
7f52f98c | 701 | if (force || time_after(sq->first_pending_disptime, jiffies)) { |
69df0ab0 | 702 | throtl_schedule_pending_timer(sq, sq->first_pending_disptime); |
7f52f98c | 703 | return true; |
69df0ab0 TH |
704 | } |
705 | ||
7f52f98c TH |
706 | /* tell the caller to continue dispatching */ |
707 | return false; | |
e43473b7 VG |
708 | } |
709 | ||
32ee5bc4 VG |
710 | static inline void throtl_start_new_slice_with_credit(struct throtl_grp *tg, |
711 | bool rw, unsigned long start) | |
712 | { | |
713 | tg->bytes_disp[rw] = 0; | |
714 | tg->io_disp[rw] = 0; | |
715 | ||
716 | /* | |
717 | * Previous slice has expired. We must have trimmed it after last | |
718 | * bio dispatch. That means since start of last slice, we never used | |
719 | * that bandwidth. Do try to make use of that bandwidth while giving | |
720 | * credit. | |
721 | */ | |
722 | if (time_after_eq(start, tg->slice_start[rw])) | |
723 | tg->slice_start[rw] = start; | |
724 | ||
297e3d85 | 725 | tg->slice_end[rw] = jiffies + tg->td->throtl_slice; |
32ee5bc4 VG |
726 | throtl_log(&tg->service_queue, |
727 | "[%c] new slice with credit start=%lu end=%lu jiffies=%lu", | |
728 | rw == READ ? 'R' : 'W', tg->slice_start[rw], | |
729 | tg->slice_end[rw], jiffies); | |
730 | } | |
731 | ||
0f3457f6 | 732 | static inline void throtl_start_new_slice(struct throtl_grp *tg, bool rw) |
e43473b7 VG |
733 | { |
734 | tg->bytes_disp[rw] = 0; | |
8e89d13f | 735 | tg->io_disp[rw] = 0; |
e43473b7 | 736 | tg->slice_start[rw] = jiffies; |
297e3d85 | 737 | tg->slice_end[rw] = jiffies + tg->td->throtl_slice; |
fda6f272 TH |
738 | throtl_log(&tg->service_queue, |
739 | "[%c] new slice start=%lu end=%lu jiffies=%lu", | |
740 | rw == READ ? 'R' : 'W', tg->slice_start[rw], | |
741 | tg->slice_end[rw], jiffies); | |
e43473b7 VG |
742 | } |
743 | ||
0f3457f6 TH |
744 | static inline void throtl_set_slice_end(struct throtl_grp *tg, bool rw, |
745 | unsigned long jiffy_end) | |
d1ae8ffd | 746 | { |
297e3d85 | 747 | tg->slice_end[rw] = roundup(jiffy_end, tg->td->throtl_slice); |
d1ae8ffd VG |
748 | } |
749 | ||
0f3457f6 TH |
750 | static inline void throtl_extend_slice(struct throtl_grp *tg, bool rw, |
751 | unsigned long jiffy_end) | |
e43473b7 | 752 | { |
297e3d85 | 753 | tg->slice_end[rw] = roundup(jiffy_end, tg->td->throtl_slice); |
fda6f272 TH |
754 | throtl_log(&tg->service_queue, |
755 | "[%c] extend slice start=%lu end=%lu jiffies=%lu", | |
756 | rw == READ ? 'R' : 'W', tg->slice_start[rw], | |
757 | tg->slice_end[rw], jiffies); | |
e43473b7 VG |
758 | } |
759 | ||
760 | /* Determine if previously allocated or extended slice is complete or not */ | |
0f3457f6 | 761 | static bool throtl_slice_used(struct throtl_grp *tg, bool rw) |
e43473b7 VG |
762 | { |
763 | if (time_in_range(jiffies, tg->slice_start[rw], tg->slice_end[rw])) | |
5cf8c227 | 764 | return false; |
e43473b7 VG |
765 | |
766 | return 1; | |
767 | } | |
768 | ||
769 | /* Trim the used slices and adjust slice start accordingly */ | |
0f3457f6 | 770 | static inline void throtl_trim_slice(struct throtl_grp *tg, bool rw) |
e43473b7 | 771 | { |
3aad5d3e VG |
772 | unsigned long nr_slices, time_elapsed, io_trim; |
773 | u64 bytes_trim, tmp; | |
e43473b7 VG |
774 | |
775 | BUG_ON(time_before(tg->slice_end[rw], tg->slice_start[rw])); | |
776 | ||
777 | /* | |
778 | * If bps are unlimited (-1), then time slice don't get | |
779 | * renewed. Don't try to trim the slice if slice is used. A new | |
780 | * slice will start when appropriate. | |
781 | */ | |
0f3457f6 | 782 | if (throtl_slice_used(tg, rw)) |
e43473b7 VG |
783 | return; |
784 | ||
d1ae8ffd VG |
785 | /* |
786 | * A bio has been dispatched. Also adjust slice_end. It might happen | |
787 | * that initially cgroup limit was very low resulting in high | |
788 | * slice_end, but later limit was bumped up and bio was dispached | |
789 | * sooner, then we need to reduce slice_end. A high bogus slice_end | |
790 | * is bad because it does not allow new slice to start. | |
791 | */ | |
792 | ||
297e3d85 | 793 | throtl_set_slice_end(tg, rw, jiffies + tg->td->throtl_slice); |
d1ae8ffd | 794 | |
e43473b7 VG |
795 | time_elapsed = jiffies - tg->slice_start[rw]; |
796 | ||
297e3d85 | 797 | nr_slices = time_elapsed / tg->td->throtl_slice; |
e43473b7 VG |
798 | |
799 | if (!nr_slices) | |
800 | return; | |
297e3d85 | 801 | tmp = tg_bps_limit(tg, rw) * tg->td->throtl_slice * nr_slices; |
3aad5d3e VG |
802 | do_div(tmp, HZ); |
803 | bytes_trim = tmp; | |
e43473b7 | 804 | |
297e3d85 SL |
805 | io_trim = (tg_iops_limit(tg, rw) * tg->td->throtl_slice * nr_slices) / |
806 | HZ; | |
e43473b7 | 807 | |
8e89d13f | 808 | if (!bytes_trim && !io_trim) |
e43473b7 VG |
809 | return; |
810 | ||
811 | if (tg->bytes_disp[rw] >= bytes_trim) | |
812 | tg->bytes_disp[rw] -= bytes_trim; | |
813 | else | |
814 | tg->bytes_disp[rw] = 0; | |
815 | ||
8e89d13f VG |
816 | if (tg->io_disp[rw] >= io_trim) |
817 | tg->io_disp[rw] -= io_trim; | |
818 | else | |
819 | tg->io_disp[rw] = 0; | |
820 | ||
297e3d85 | 821 | tg->slice_start[rw] += nr_slices * tg->td->throtl_slice; |
e43473b7 | 822 | |
fda6f272 TH |
823 | throtl_log(&tg->service_queue, |
824 | "[%c] trim slice nr=%lu bytes=%llu io=%lu start=%lu end=%lu jiffies=%lu", | |
825 | rw == READ ? 'R' : 'W', nr_slices, bytes_trim, io_trim, | |
826 | tg->slice_start[rw], tg->slice_end[rw], jiffies); | |
e43473b7 VG |
827 | } |
828 | ||
0f3457f6 TH |
829 | static bool tg_with_in_iops_limit(struct throtl_grp *tg, struct bio *bio, |
830 | unsigned long *wait) | |
e43473b7 VG |
831 | { |
832 | bool rw = bio_data_dir(bio); | |
8e89d13f | 833 | unsigned int io_allowed; |
e43473b7 | 834 | unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd; |
c49c06e4 | 835 | u64 tmp; |
e43473b7 | 836 | |
8e89d13f | 837 | jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw]; |
e43473b7 | 838 | |
8e89d13f VG |
839 | /* Slice has just started. Consider one slice interval */ |
840 | if (!jiffy_elapsed) | |
297e3d85 | 841 | jiffy_elapsed_rnd = tg->td->throtl_slice; |
8e89d13f | 842 | |
297e3d85 | 843 | jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, tg->td->throtl_slice); |
8e89d13f | 844 | |
c49c06e4 VG |
845 | /* |
846 | * jiffy_elapsed_rnd should not be a big value as minimum iops can be | |
847 | * 1 then at max jiffy elapsed should be equivalent of 1 second as we | |
848 | * will allow dispatch after 1 second and after that slice should | |
849 | * have been trimmed. | |
850 | */ | |
851 | ||
9f626e37 | 852 | tmp = (u64)tg_iops_limit(tg, rw) * jiffy_elapsed_rnd; |
c49c06e4 VG |
853 | do_div(tmp, HZ); |
854 | ||
855 | if (tmp > UINT_MAX) | |
856 | io_allowed = UINT_MAX; | |
857 | else | |
858 | io_allowed = tmp; | |
8e89d13f VG |
859 | |
860 | if (tg->io_disp[rw] + 1 <= io_allowed) { | |
e43473b7 VG |
861 | if (wait) |
862 | *wait = 0; | |
5cf8c227 | 863 | return true; |
e43473b7 VG |
864 | } |
865 | ||
8e89d13f | 866 | /* Calc approx time to dispatch */ |
9f626e37 | 867 | jiffy_wait = ((tg->io_disp[rw] + 1) * HZ) / tg_iops_limit(tg, rw) + 1; |
8e89d13f VG |
868 | |
869 | if (jiffy_wait > jiffy_elapsed) | |
870 | jiffy_wait = jiffy_wait - jiffy_elapsed; | |
871 | else | |
872 | jiffy_wait = 1; | |
873 | ||
874 | if (wait) | |
875 | *wait = jiffy_wait; | |
876 | return 0; | |
877 | } | |
878 | ||
0f3457f6 TH |
879 | static bool tg_with_in_bps_limit(struct throtl_grp *tg, struct bio *bio, |
880 | unsigned long *wait) | |
8e89d13f VG |
881 | { |
882 | bool rw = bio_data_dir(bio); | |
3aad5d3e | 883 | u64 bytes_allowed, extra_bytes, tmp; |
8e89d13f | 884 | unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd; |
e43473b7 VG |
885 | |
886 | jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw]; | |
887 | ||
888 | /* Slice has just started. Consider one slice interval */ | |
889 | if (!jiffy_elapsed) | |
297e3d85 | 890 | jiffy_elapsed_rnd = tg->td->throtl_slice; |
e43473b7 | 891 | |
297e3d85 | 892 | jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, tg->td->throtl_slice); |
e43473b7 | 893 | |
9f626e37 | 894 | tmp = tg_bps_limit(tg, rw) * jiffy_elapsed_rnd; |
5e901a2b | 895 | do_div(tmp, HZ); |
3aad5d3e | 896 | bytes_allowed = tmp; |
e43473b7 | 897 | |
4f024f37 | 898 | if (tg->bytes_disp[rw] + bio->bi_iter.bi_size <= bytes_allowed) { |
e43473b7 VG |
899 | if (wait) |
900 | *wait = 0; | |
5cf8c227 | 901 | return true; |
e43473b7 VG |
902 | } |
903 | ||
904 | /* Calc approx time to dispatch */ | |
4f024f37 | 905 | extra_bytes = tg->bytes_disp[rw] + bio->bi_iter.bi_size - bytes_allowed; |
9f626e37 | 906 | jiffy_wait = div64_u64(extra_bytes * HZ, tg_bps_limit(tg, rw)); |
e43473b7 VG |
907 | |
908 | if (!jiffy_wait) | |
909 | jiffy_wait = 1; | |
910 | ||
911 | /* | |
912 | * This wait time is without taking into consideration the rounding | |
913 | * up we did. Add that time also. | |
914 | */ | |
915 | jiffy_wait = jiffy_wait + (jiffy_elapsed_rnd - jiffy_elapsed); | |
e43473b7 VG |
916 | if (wait) |
917 | *wait = jiffy_wait; | |
8e89d13f VG |
918 | return 0; |
919 | } | |
920 | ||
921 | /* | |
922 | * Returns whether one can dispatch a bio or not. Also returns approx number | |
923 | * of jiffies to wait before this bio is with-in IO rate and can be dispatched | |
924 | */ | |
0f3457f6 TH |
925 | static bool tg_may_dispatch(struct throtl_grp *tg, struct bio *bio, |
926 | unsigned long *wait) | |
8e89d13f VG |
927 | { |
928 | bool rw = bio_data_dir(bio); | |
929 | unsigned long bps_wait = 0, iops_wait = 0, max_wait = 0; | |
930 | ||
931 | /* | |
932 | * Currently whole state machine of group depends on first bio | |
933 | * queued in the group bio list. So one should not be calling | |
934 | * this function with a different bio if there are other bios | |
935 | * queued. | |
936 | */ | |
73f0d49a | 937 | BUG_ON(tg->service_queue.nr_queued[rw] && |
c5cc2070 | 938 | bio != throtl_peek_queued(&tg->service_queue.queued[rw])); |
e43473b7 | 939 | |
8e89d13f | 940 | /* If tg->bps = -1, then BW is unlimited */ |
9f626e37 SL |
941 | if (tg_bps_limit(tg, rw) == U64_MAX && |
942 | tg_iops_limit(tg, rw) == UINT_MAX) { | |
8e89d13f VG |
943 | if (wait) |
944 | *wait = 0; | |
5cf8c227 | 945 | return true; |
8e89d13f VG |
946 | } |
947 | ||
948 | /* | |
949 | * If previous slice expired, start a new one otherwise renew/extend | |
950 | * existing slice to make sure it is at least throtl_slice interval | |
164c80ed VG |
951 | * long since now. New slice is started only for empty throttle group. |
952 | * If there is queued bio, that means there should be an active | |
953 | * slice and it should be extended instead. | |
8e89d13f | 954 | */ |
164c80ed | 955 | if (throtl_slice_used(tg, rw) && !(tg->service_queue.nr_queued[rw])) |
0f3457f6 | 956 | throtl_start_new_slice(tg, rw); |
8e89d13f | 957 | else { |
297e3d85 SL |
958 | if (time_before(tg->slice_end[rw], |
959 | jiffies + tg->td->throtl_slice)) | |
960 | throtl_extend_slice(tg, rw, | |
961 | jiffies + tg->td->throtl_slice); | |
8e89d13f VG |
962 | } |
963 | ||
0f3457f6 TH |
964 | if (tg_with_in_bps_limit(tg, bio, &bps_wait) && |
965 | tg_with_in_iops_limit(tg, bio, &iops_wait)) { | |
8e89d13f VG |
966 | if (wait) |
967 | *wait = 0; | |
968 | return 1; | |
969 | } | |
970 | ||
971 | max_wait = max(bps_wait, iops_wait); | |
972 | ||
973 | if (wait) | |
974 | *wait = max_wait; | |
975 | ||
976 | if (time_before(tg->slice_end[rw], jiffies + max_wait)) | |
0f3457f6 | 977 | throtl_extend_slice(tg, rw, jiffies + max_wait); |
e43473b7 VG |
978 | |
979 | return 0; | |
980 | } | |
981 | ||
982 | static void throtl_charge_bio(struct throtl_grp *tg, struct bio *bio) | |
983 | { | |
984 | bool rw = bio_data_dir(bio); | |
e43473b7 VG |
985 | |
986 | /* Charge the bio to the group */ | |
4f024f37 | 987 | tg->bytes_disp[rw] += bio->bi_iter.bi_size; |
8e89d13f | 988 | tg->io_disp[rw]++; |
3f0abd80 SL |
989 | tg->last_bytes_disp[rw] += bio->bi_iter.bi_size; |
990 | tg->last_io_disp[rw]++; | |
e43473b7 | 991 | |
2a0f61e6 | 992 | /* |
8d2bbd4c | 993 | * BIO_THROTTLED is used to prevent the same bio to be throttled |
2a0f61e6 TH |
994 | * more than once as a throttled bio will go through blk-throtl the |
995 | * second time when it eventually gets issued. Set it when a bio | |
996 | * is being charged to a tg. | |
2a0f61e6 | 997 | */ |
8d2bbd4c CH |
998 | if (!bio_flagged(bio, BIO_THROTTLED)) |
999 | bio_set_flag(bio, BIO_THROTTLED); | |
e43473b7 VG |
1000 | } |
1001 | ||
c5cc2070 TH |
1002 | /** |
1003 | * throtl_add_bio_tg - add a bio to the specified throtl_grp | |
1004 | * @bio: bio to add | |
1005 | * @qn: qnode to use | |
1006 | * @tg: the target throtl_grp | |
1007 | * | |
1008 | * Add @bio to @tg's service_queue using @qn. If @qn is not specified, | |
1009 | * tg->qnode_on_self[] is used. | |
1010 | */ | |
1011 | static void throtl_add_bio_tg(struct bio *bio, struct throtl_qnode *qn, | |
1012 | struct throtl_grp *tg) | |
e43473b7 | 1013 | { |
73f0d49a | 1014 | struct throtl_service_queue *sq = &tg->service_queue; |
e43473b7 VG |
1015 | bool rw = bio_data_dir(bio); |
1016 | ||
c5cc2070 TH |
1017 | if (!qn) |
1018 | qn = &tg->qnode_on_self[rw]; | |
1019 | ||
0e9f4164 TH |
1020 | /* |
1021 | * If @tg doesn't currently have any bios queued in the same | |
1022 | * direction, queueing @bio can change when @tg should be | |
1023 | * dispatched. Mark that @tg was empty. This is automatically | |
1024 | * cleaered on the next tg_update_disptime(). | |
1025 | */ | |
1026 | if (!sq->nr_queued[rw]) | |
1027 | tg->flags |= THROTL_TG_WAS_EMPTY; | |
1028 | ||
c5cc2070 TH |
1029 | throtl_qnode_add_bio(bio, qn, &sq->queued[rw]); |
1030 | ||
73f0d49a | 1031 | sq->nr_queued[rw]++; |
77216b04 | 1032 | throtl_enqueue_tg(tg); |
e43473b7 VG |
1033 | } |
1034 | ||
77216b04 | 1035 | static void tg_update_disptime(struct throtl_grp *tg) |
e43473b7 | 1036 | { |
73f0d49a | 1037 | struct throtl_service_queue *sq = &tg->service_queue; |
e43473b7 VG |
1038 | unsigned long read_wait = -1, write_wait = -1, min_wait = -1, disptime; |
1039 | struct bio *bio; | |
1040 | ||
d609af3a ME |
1041 | bio = throtl_peek_queued(&sq->queued[READ]); |
1042 | if (bio) | |
0f3457f6 | 1043 | tg_may_dispatch(tg, bio, &read_wait); |
e43473b7 | 1044 | |
d609af3a ME |
1045 | bio = throtl_peek_queued(&sq->queued[WRITE]); |
1046 | if (bio) | |
0f3457f6 | 1047 | tg_may_dispatch(tg, bio, &write_wait); |
e43473b7 VG |
1048 | |
1049 | min_wait = min(read_wait, write_wait); | |
1050 | disptime = jiffies + min_wait; | |
1051 | ||
e43473b7 | 1052 | /* Update dispatch time */ |
77216b04 | 1053 | throtl_dequeue_tg(tg); |
e43473b7 | 1054 | tg->disptime = disptime; |
77216b04 | 1055 | throtl_enqueue_tg(tg); |
0e9f4164 TH |
1056 | |
1057 | /* see throtl_add_bio_tg() */ | |
1058 | tg->flags &= ~THROTL_TG_WAS_EMPTY; | |
e43473b7 VG |
1059 | } |
1060 | ||
32ee5bc4 VG |
1061 | static void start_parent_slice_with_credit(struct throtl_grp *child_tg, |
1062 | struct throtl_grp *parent_tg, bool rw) | |
1063 | { | |
1064 | if (throtl_slice_used(parent_tg, rw)) { | |
1065 | throtl_start_new_slice_with_credit(parent_tg, rw, | |
1066 | child_tg->slice_start[rw]); | |
1067 | } | |
1068 | ||
1069 | } | |
1070 | ||
77216b04 | 1071 | static void tg_dispatch_one_bio(struct throtl_grp *tg, bool rw) |
e43473b7 | 1072 | { |
73f0d49a | 1073 | struct throtl_service_queue *sq = &tg->service_queue; |
6bc9c2b4 TH |
1074 | struct throtl_service_queue *parent_sq = sq->parent_sq; |
1075 | struct throtl_grp *parent_tg = sq_to_tg(parent_sq); | |
c5cc2070 | 1076 | struct throtl_grp *tg_to_put = NULL; |
e43473b7 VG |
1077 | struct bio *bio; |
1078 | ||
c5cc2070 TH |
1079 | /* |
1080 | * @bio is being transferred from @tg to @parent_sq. Popping a bio | |
1081 | * from @tg may put its reference and @parent_sq might end up | |
1082 | * getting released prematurely. Remember the tg to put and put it | |
1083 | * after @bio is transferred to @parent_sq. | |
1084 | */ | |
1085 | bio = throtl_pop_queued(&sq->queued[rw], &tg_to_put); | |
73f0d49a | 1086 | sq->nr_queued[rw]--; |
e43473b7 VG |
1087 | |
1088 | throtl_charge_bio(tg, bio); | |
6bc9c2b4 TH |
1089 | |
1090 | /* | |
1091 | * If our parent is another tg, we just need to transfer @bio to | |
1092 | * the parent using throtl_add_bio_tg(). If our parent is | |
1093 | * @td->service_queue, @bio is ready to be issued. Put it on its | |
1094 | * bio_lists[] and decrease total number queued. The caller is | |
1095 | * responsible for issuing these bios. | |
1096 | */ | |
1097 | if (parent_tg) { | |
c5cc2070 | 1098 | throtl_add_bio_tg(bio, &tg->qnode_on_parent[rw], parent_tg); |
32ee5bc4 | 1099 | start_parent_slice_with_credit(tg, parent_tg, rw); |
6bc9c2b4 | 1100 | } else { |
c5cc2070 TH |
1101 | throtl_qnode_add_bio(bio, &tg->qnode_on_parent[rw], |
1102 | &parent_sq->queued[rw]); | |
6bc9c2b4 TH |
1103 | BUG_ON(tg->td->nr_queued[rw] <= 0); |
1104 | tg->td->nr_queued[rw]--; | |
1105 | } | |
e43473b7 | 1106 | |
0f3457f6 | 1107 | throtl_trim_slice(tg, rw); |
6bc9c2b4 | 1108 | |
c5cc2070 TH |
1109 | if (tg_to_put) |
1110 | blkg_put(tg_to_blkg(tg_to_put)); | |
e43473b7 VG |
1111 | } |
1112 | ||
77216b04 | 1113 | static int throtl_dispatch_tg(struct throtl_grp *tg) |
e43473b7 | 1114 | { |
73f0d49a | 1115 | struct throtl_service_queue *sq = &tg->service_queue; |
e43473b7 VG |
1116 | unsigned int nr_reads = 0, nr_writes = 0; |
1117 | unsigned int max_nr_reads = throtl_grp_quantum*3/4; | |
c2f6805d | 1118 | unsigned int max_nr_writes = throtl_grp_quantum - max_nr_reads; |
e43473b7 VG |
1119 | struct bio *bio; |
1120 | ||
1121 | /* Try to dispatch 75% READS and 25% WRITES */ | |
1122 | ||
c5cc2070 | 1123 | while ((bio = throtl_peek_queued(&sq->queued[READ])) && |
0f3457f6 | 1124 | tg_may_dispatch(tg, bio, NULL)) { |
e43473b7 | 1125 | |
77216b04 | 1126 | tg_dispatch_one_bio(tg, bio_data_dir(bio)); |
e43473b7 VG |
1127 | nr_reads++; |
1128 | ||
1129 | if (nr_reads >= max_nr_reads) | |
1130 | break; | |
1131 | } | |
1132 | ||
c5cc2070 | 1133 | while ((bio = throtl_peek_queued(&sq->queued[WRITE])) && |
0f3457f6 | 1134 | tg_may_dispatch(tg, bio, NULL)) { |
e43473b7 | 1135 | |
77216b04 | 1136 | tg_dispatch_one_bio(tg, bio_data_dir(bio)); |
e43473b7 VG |
1137 | nr_writes++; |
1138 | ||
1139 | if (nr_writes >= max_nr_writes) | |
1140 | break; | |
1141 | } | |
1142 | ||
1143 | return nr_reads + nr_writes; | |
1144 | } | |
1145 | ||
651930bc | 1146 | static int throtl_select_dispatch(struct throtl_service_queue *parent_sq) |
e43473b7 VG |
1147 | { |
1148 | unsigned int nr_disp = 0; | |
e43473b7 VG |
1149 | |
1150 | while (1) { | |
73f0d49a TH |
1151 | struct throtl_grp *tg = throtl_rb_first(parent_sq); |
1152 | struct throtl_service_queue *sq = &tg->service_queue; | |
e43473b7 VG |
1153 | |
1154 | if (!tg) | |
1155 | break; | |
1156 | ||
1157 | if (time_before(jiffies, tg->disptime)) | |
1158 | break; | |
1159 | ||
77216b04 | 1160 | throtl_dequeue_tg(tg); |
e43473b7 | 1161 | |
77216b04 | 1162 | nr_disp += throtl_dispatch_tg(tg); |
e43473b7 | 1163 | |
73f0d49a | 1164 | if (sq->nr_queued[0] || sq->nr_queued[1]) |
77216b04 | 1165 | tg_update_disptime(tg); |
e43473b7 VG |
1166 | |
1167 | if (nr_disp >= throtl_quantum) | |
1168 | break; | |
1169 | } | |
1170 | ||
1171 | return nr_disp; | |
1172 | } | |
1173 | ||
c79892c5 SL |
1174 | static bool throtl_can_upgrade(struct throtl_data *td, |
1175 | struct throtl_grp *this_tg); | |
6e1a5704 TH |
1176 | /** |
1177 | * throtl_pending_timer_fn - timer function for service_queue->pending_timer | |
1178 | * @arg: the throtl_service_queue being serviced | |
1179 | * | |
1180 | * This timer is armed when a child throtl_grp with active bio's become | |
1181 | * pending and queued on the service_queue's pending_tree and expires when | |
1182 | * the first child throtl_grp should be dispatched. This function | |
2e48a530 TH |
1183 | * dispatches bio's from the children throtl_grps to the parent |
1184 | * service_queue. | |
1185 | * | |
1186 | * If the parent's parent is another throtl_grp, dispatching is propagated | |
1187 | * by either arming its pending_timer or repeating dispatch directly. If | |
1188 | * the top-level service_tree is reached, throtl_data->dispatch_work is | |
1189 | * kicked so that the ready bio's are issued. | |
6e1a5704 | 1190 | */ |
69df0ab0 TH |
1191 | static void throtl_pending_timer_fn(unsigned long arg) |
1192 | { | |
1193 | struct throtl_service_queue *sq = (void *)arg; | |
2e48a530 | 1194 | struct throtl_grp *tg = sq_to_tg(sq); |
69df0ab0 | 1195 | struct throtl_data *td = sq_to_td(sq); |
cb76199c | 1196 | struct request_queue *q = td->queue; |
2e48a530 TH |
1197 | struct throtl_service_queue *parent_sq; |
1198 | bool dispatched; | |
6e1a5704 | 1199 | int ret; |
e43473b7 VG |
1200 | |
1201 | spin_lock_irq(q->queue_lock); | |
c79892c5 SL |
1202 | if (throtl_can_upgrade(td, NULL)) |
1203 | throtl_upgrade_state(td); | |
1204 | ||
2e48a530 TH |
1205 | again: |
1206 | parent_sq = sq->parent_sq; | |
1207 | dispatched = false; | |
e43473b7 | 1208 | |
7f52f98c TH |
1209 | while (true) { |
1210 | throtl_log(sq, "dispatch nr_queued=%u read=%u write=%u", | |
2e48a530 TH |
1211 | sq->nr_queued[READ] + sq->nr_queued[WRITE], |
1212 | sq->nr_queued[READ], sq->nr_queued[WRITE]); | |
7f52f98c TH |
1213 | |
1214 | ret = throtl_select_dispatch(sq); | |
1215 | if (ret) { | |
7f52f98c TH |
1216 | throtl_log(sq, "bios disp=%u", ret); |
1217 | dispatched = true; | |
1218 | } | |
e43473b7 | 1219 | |
7f52f98c TH |
1220 | if (throtl_schedule_next_dispatch(sq, false)) |
1221 | break; | |
e43473b7 | 1222 | |
7f52f98c TH |
1223 | /* this dispatch windows is still open, relax and repeat */ |
1224 | spin_unlock_irq(q->queue_lock); | |
1225 | cpu_relax(); | |
1226 | spin_lock_irq(q->queue_lock); | |
651930bc | 1227 | } |
e43473b7 | 1228 | |
2e48a530 TH |
1229 | if (!dispatched) |
1230 | goto out_unlock; | |
6e1a5704 | 1231 | |
2e48a530 TH |
1232 | if (parent_sq) { |
1233 | /* @parent_sq is another throl_grp, propagate dispatch */ | |
1234 | if (tg->flags & THROTL_TG_WAS_EMPTY) { | |
1235 | tg_update_disptime(tg); | |
1236 | if (!throtl_schedule_next_dispatch(parent_sq, false)) { | |
1237 | /* window is already open, repeat dispatching */ | |
1238 | sq = parent_sq; | |
1239 | tg = sq_to_tg(sq); | |
1240 | goto again; | |
1241 | } | |
1242 | } | |
1243 | } else { | |
1244 | /* reached the top-level, queue issueing */ | |
1245 | queue_work(kthrotld_workqueue, &td->dispatch_work); | |
1246 | } | |
1247 | out_unlock: | |
e43473b7 | 1248 | spin_unlock_irq(q->queue_lock); |
6e1a5704 | 1249 | } |
e43473b7 | 1250 | |
6e1a5704 TH |
1251 | /** |
1252 | * blk_throtl_dispatch_work_fn - work function for throtl_data->dispatch_work | |
1253 | * @work: work item being executed | |
1254 | * | |
1255 | * This function is queued for execution when bio's reach the bio_lists[] | |
1256 | * of throtl_data->service_queue. Those bio's are ready and issued by this | |
1257 | * function. | |
1258 | */ | |
8876e140 | 1259 | static void blk_throtl_dispatch_work_fn(struct work_struct *work) |
6e1a5704 TH |
1260 | { |
1261 | struct throtl_data *td = container_of(work, struct throtl_data, | |
1262 | dispatch_work); | |
1263 | struct throtl_service_queue *td_sq = &td->service_queue; | |
1264 | struct request_queue *q = td->queue; | |
1265 | struct bio_list bio_list_on_stack; | |
1266 | struct bio *bio; | |
1267 | struct blk_plug plug; | |
1268 | int rw; | |
1269 | ||
1270 | bio_list_init(&bio_list_on_stack); | |
1271 | ||
1272 | spin_lock_irq(q->queue_lock); | |
c5cc2070 TH |
1273 | for (rw = READ; rw <= WRITE; rw++) |
1274 | while ((bio = throtl_pop_queued(&td_sq->queued[rw], NULL))) | |
1275 | bio_list_add(&bio_list_on_stack, bio); | |
6e1a5704 TH |
1276 | spin_unlock_irq(q->queue_lock); |
1277 | ||
1278 | if (!bio_list_empty(&bio_list_on_stack)) { | |
69d60eb9 | 1279 | blk_start_plug(&plug); |
e43473b7 VG |
1280 | while((bio = bio_list_pop(&bio_list_on_stack))) |
1281 | generic_make_request(bio); | |
69d60eb9 | 1282 | blk_finish_plug(&plug); |
e43473b7 | 1283 | } |
e43473b7 VG |
1284 | } |
1285 | ||
f95a04af TH |
1286 | static u64 tg_prfill_conf_u64(struct seq_file *sf, struct blkg_policy_data *pd, |
1287 | int off) | |
60c2bc2d | 1288 | { |
f95a04af TH |
1289 | struct throtl_grp *tg = pd_to_tg(pd); |
1290 | u64 v = *(u64 *)((void *)tg + off); | |
60c2bc2d | 1291 | |
2ab5492d | 1292 | if (v == U64_MAX) |
60c2bc2d | 1293 | return 0; |
f95a04af | 1294 | return __blkg_prfill_u64(sf, pd, v); |
60c2bc2d TH |
1295 | } |
1296 | ||
f95a04af TH |
1297 | static u64 tg_prfill_conf_uint(struct seq_file *sf, struct blkg_policy_data *pd, |
1298 | int off) | |
e43473b7 | 1299 | { |
f95a04af TH |
1300 | struct throtl_grp *tg = pd_to_tg(pd); |
1301 | unsigned int v = *(unsigned int *)((void *)tg + off); | |
fe071437 | 1302 | |
2ab5492d | 1303 | if (v == UINT_MAX) |
af133ceb | 1304 | return 0; |
f95a04af | 1305 | return __blkg_prfill_u64(sf, pd, v); |
e43473b7 VG |
1306 | } |
1307 | ||
2da8ca82 | 1308 | static int tg_print_conf_u64(struct seq_file *sf, void *v) |
8e89d13f | 1309 | { |
2da8ca82 TH |
1310 | blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), tg_prfill_conf_u64, |
1311 | &blkcg_policy_throtl, seq_cft(sf)->private, false); | |
af133ceb | 1312 | return 0; |
8e89d13f VG |
1313 | } |
1314 | ||
2da8ca82 | 1315 | static int tg_print_conf_uint(struct seq_file *sf, void *v) |
8e89d13f | 1316 | { |
2da8ca82 TH |
1317 | blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), tg_prfill_conf_uint, |
1318 | &blkcg_policy_throtl, seq_cft(sf)->private, false); | |
af133ceb | 1319 | return 0; |
60c2bc2d TH |
1320 | } |
1321 | ||
69948b07 | 1322 | static void tg_conf_updated(struct throtl_grp *tg) |
60c2bc2d | 1323 | { |
69948b07 | 1324 | struct throtl_service_queue *sq = &tg->service_queue; |
492eb21b | 1325 | struct cgroup_subsys_state *pos_css; |
69948b07 | 1326 | struct blkcg_gq *blkg; |
af133ceb | 1327 | |
fda6f272 TH |
1328 | throtl_log(&tg->service_queue, |
1329 | "limit change rbps=%llu wbps=%llu riops=%u wiops=%u", | |
9f626e37 SL |
1330 | tg_bps_limit(tg, READ), tg_bps_limit(tg, WRITE), |
1331 | tg_iops_limit(tg, READ), tg_iops_limit(tg, WRITE)); | |
632b4493 | 1332 | |
693e751e TH |
1333 | /* |
1334 | * Update has_rules[] flags for the updated tg's subtree. A tg is | |
1335 | * considered to have rules if either the tg itself or any of its | |
1336 | * ancestors has rules. This identifies groups without any | |
1337 | * restrictions in the whole hierarchy and allows them to bypass | |
1338 | * blk-throttle. | |
1339 | */ | |
69948b07 | 1340 | blkg_for_each_descendant_pre(blkg, pos_css, tg_to_blkg(tg)) |
693e751e TH |
1341 | tg_update_has_rules(blkg_to_tg(blkg)); |
1342 | ||
632b4493 TH |
1343 | /* |
1344 | * We're already holding queue_lock and know @tg is valid. Let's | |
1345 | * apply the new config directly. | |
1346 | * | |
1347 | * Restart the slices for both READ and WRITES. It might happen | |
1348 | * that a group's limit are dropped suddenly and we don't want to | |
1349 | * account recently dispatched IO with new low rate. | |
1350 | */ | |
0f3457f6 TH |
1351 | throtl_start_new_slice(tg, 0); |
1352 | throtl_start_new_slice(tg, 1); | |
632b4493 | 1353 | |
5b2c16aa | 1354 | if (tg->flags & THROTL_TG_PENDING) { |
77216b04 | 1355 | tg_update_disptime(tg); |
7f52f98c | 1356 | throtl_schedule_next_dispatch(sq->parent_sq, true); |
632b4493 | 1357 | } |
69948b07 TH |
1358 | } |
1359 | ||
1360 | static ssize_t tg_set_conf(struct kernfs_open_file *of, | |
1361 | char *buf, size_t nbytes, loff_t off, bool is_u64) | |
1362 | { | |
1363 | struct blkcg *blkcg = css_to_blkcg(of_css(of)); | |
1364 | struct blkg_conf_ctx ctx; | |
1365 | struct throtl_grp *tg; | |
1366 | int ret; | |
1367 | u64 v; | |
1368 | ||
1369 | ret = blkg_conf_prep(blkcg, &blkcg_policy_throtl, buf, &ctx); | |
1370 | if (ret) | |
1371 | return ret; | |
1372 | ||
1373 | ret = -EINVAL; | |
1374 | if (sscanf(ctx.body, "%llu", &v) != 1) | |
1375 | goto out_finish; | |
1376 | if (!v) | |
2ab5492d | 1377 | v = U64_MAX; |
69948b07 TH |
1378 | |
1379 | tg = blkg_to_tg(ctx.blkg); | |
1380 | ||
1381 | if (is_u64) | |
1382 | *(u64 *)((void *)tg + of_cft(of)->private) = v; | |
1383 | else | |
1384 | *(unsigned int *)((void *)tg + of_cft(of)->private) = v; | |
60c2bc2d | 1385 | |
69948b07 | 1386 | tg_conf_updated(tg); |
36aa9e5f TH |
1387 | ret = 0; |
1388 | out_finish: | |
60c2bc2d | 1389 | blkg_conf_finish(&ctx); |
36aa9e5f | 1390 | return ret ?: nbytes; |
8e89d13f VG |
1391 | } |
1392 | ||
451af504 TH |
1393 | static ssize_t tg_set_conf_u64(struct kernfs_open_file *of, |
1394 | char *buf, size_t nbytes, loff_t off) | |
60c2bc2d | 1395 | { |
451af504 | 1396 | return tg_set_conf(of, buf, nbytes, off, true); |
60c2bc2d TH |
1397 | } |
1398 | ||
451af504 TH |
1399 | static ssize_t tg_set_conf_uint(struct kernfs_open_file *of, |
1400 | char *buf, size_t nbytes, loff_t off) | |
60c2bc2d | 1401 | { |
451af504 | 1402 | return tg_set_conf(of, buf, nbytes, off, false); |
60c2bc2d TH |
1403 | } |
1404 | ||
880f50e2 | 1405 | static struct cftype throtl_legacy_files[] = { |
60c2bc2d TH |
1406 | { |
1407 | .name = "throttle.read_bps_device", | |
9f626e37 | 1408 | .private = offsetof(struct throtl_grp, bps[READ][LIMIT_MAX]), |
2da8ca82 | 1409 | .seq_show = tg_print_conf_u64, |
451af504 | 1410 | .write = tg_set_conf_u64, |
60c2bc2d TH |
1411 | }, |
1412 | { | |
1413 | .name = "throttle.write_bps_device", | |
9f626e37 | 1414 | .private = offsetof(struct throtl_grp, bps[WRITE][LIMIT_MAX]), |
2da8ca82 | 1415 | .seq_show = tg_print_conf_u64, |
451af504 | 1416 | .write = tg_set_conf_u64, |
60c2bc2d TH |
1417 | }, |
1418 | { | |
1419 | .name = "throttle.read_iops_device", | |
9f626e37 | 1420 | .private = offsetof(struct throtl_grp, iops[READ][LIMIT_MAX]), |
2da8ca82 | 1421 | .seq_show = tg_print_conf_uint, |
451af504 | 1422 | .write = tg_set_conf_uint, |
60c2bc2d TH |
1423 | }, |
1424 | { | |
1425 | .name = "throttle.write_iops_device", | |
9f626e37 | 1426 | .private = offsetof(struct throtl_grp, iops[WRITE][LIMIT_MAX]), |
2da8ca82 | 1427 | .seq_show = tg_print_conf_uint, |
451af504 | 1428 | .write = tg_set_conf_uint, |
60c2bc2d TH |
1429 | }, |
1430 | { | |
1431 | .name = "throttle.io_service_bytes", | |
77ea7338 TH |
1432 | .private = (unsigned long)&blkcg_policy_throtl, |
1433 | .seq_show = blkg_print_stat_bytes, | |
60c2bc2d TH |
1434 | }, |
1435 | { | |
1436 | .name = "throttle.io_serviced", | |
77ea7338 TH |
1437 | .private = (unsigned long)&blkcg_policy_throtl, |
1438 | .seq_show = blkg_print_stat_ios, | |
60c2bc2d TH |
1439 | }, |
1440 | { } /* terminate */ | |
1441 | }; | |
1442 | ||
cd5ab1b0 | 1443 | static u64 tg_prfill_limit(struct seq_file *sf, struct blkg_policy_data *pd, |
2ee867dc TH |
1444 | int off) |
1445 | { | |
1446 | struct throtl_grp *tg = pd_to_tg(pd); | |
1447 | const char *dname = blkg_dev_name(pd->blkg); | |
1448 | char bufs[4][21] = { "max", "max", "max", "max" }; | |
cd5ab1b0 SL |
1449 | u64 bps_dft; |
1450 | unsigned int iops_dft; | |
ada75b6e | 1451 | char idle_time[26] = ""; |
2ee867dc TH |
1452 | |
1453 | if (!dname) | |
1454 | return 0; | |
9f626e37 | 1455 | |
cd5ab1b0 SL |
1456 | if (off == LIMIT_LOW) { |
1457 | bps_dft = 0; | |
1458 | iops_dft = 0; | |
1459 | } else { | |
1460 | bps_dft = U64_MAX; | |
1461 | iops_dft = UINT_MAX; | |
1462 | } | |
1463 | ||
1464 | if (tg->bps_conf[READ][off] == bps_dft && | |
1465 | tg->bps_conf[WRITE][off] == bps_dft && | |
1466 | tg->iops_conf[READ][off] == iops_dft && | |
ada75b6e SL |
1467 | tg->iops_conf[WRITE][off] == iops_dft && |
1468 | (off != LIMIT_LOW || tg->idletime_threshold == | |
1469 | tg->td->dft_idletime_threshold)) | |
2ee867dc TH |
1470 | return 0; |
1471 | ||
cd5ab1b0 | 1472 | if (tg->bps_conf[READ][off] != bps_dft) |
9f626e37 | 1473 | snprintf(bufs[0], sizeof(bufs[0]), "%llu", |
cd5ab1b0 SL |
1474 | tg->bps_conf[READ][off]); |
1475 | if (tg->bps_conf[WRITE][off] != bps_dft) | |
9f626e37 | 1476 | snprintf(bufs[1], sizeof(bufs[1]), "%llu", |
cd5ab1b0 SL |
1477 | tg->bps_conf[WRITE][off]); |
1478 | if (tg->iops_conf[READ][off] != iops_dft) | |
9f626e37 | 1479 | snprintf(bufs[2], sizeof(bufs[2]), "%u", |
cd5ab1b0 SL |
1480 | tg->iops_conf[READ][off]); |
1481 | if (tg->iops_conf[WRITE][off] != iops_dft) | |
9f626e37 | 1482 | snprintf(bufs[3], sizeof(bufs[3]), "%u", |
cd5ab1b0 | 1483 | tg->iops_conf[WRITE][off]); |
ada75b6e SL |
1484 | if (off == LIMIT_LOW) { |
1485 | if (tg->idletime_threshold == ULONG_MAX) | |
1486 | strcpy(idle_time, " idle=max"); | |
1487 | else | |
1488 | snprintf(idle_time, sizeof(idle_time), " idle=%lu", | |
1489 | tg->idletime_threshold); | |
1490 | } | |
2ee867dc | 1491 | |
ada75b6e SL |
1492 | seq_printf(sf, "%s rbps=%s wbps=%s riops=%s wiops=%s%s\n", |
1493 | dname, bufs[0], bufs[1], bufs[2], bufs[3], idle_time); | |
2ee867dc TH |
1494 | return 0; |
1495 | } | |
1496 | ||
cd5ab1b0 | 1497 | static int tg_print_limit(struct seq_file *sf, void *v) |
2ee867dc | 1498 | { |
cd5ab1b0 | 1499 | blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), tg_prfill_limit, |
2ee867dc TH |
1500 | &blkcg_policy_throtl, seq_cft(sf)->private, false); |
1501 | return 0; | |
1502 | } | |
1503 | ||
cd5ab1b0 | 1504 | static ssize_t tg_set_limit(struct kernfs_open_file *of, |
2ee867dc TH |
1505 | char *buf, size_t nbytes, loff_t off) |
1506 | { | |
1507 | struct blkcg *blkcg = css_to_blkcg(of_css(of)); | |
1508 | struct blkg_conf_ctx ctx; | |
1509 | struct throtl_grp *tg; | |
1510 | u64 v[4]; | |
ada75b6e | 1511 | unsigned long idle_time; |
2ee867dc | 1512 | int ret; |
cd5ab1b0 | 1513 | int index = of_cft(of)->private; |
2ee867dc TH |
1514 | |
1515 | ret = blkg_conf_prep(blkcg, &blkcg_policy_throtl, buf, &ctx); | |
1516 | if (ret) | |
1517 | return ret; | |
1518 | ||
1519 | tg = blkg_to_tg(ctx.blkg); | |
1520 | ||
cd5ab1b0 SL |
1521 | v[0] = tg->bps_conf[READ][index]; |
1522 | v[1] = tg->bps_conf[WRITE][index]; | |
1523 | v[2] = tg->iops_conf[READ][index]; | |
1524 | v[3] = tg->iops_conf[WRITE][index]; | |
2ee867dc | 1525 | |
ada75b6e | 1526 | idle_time = tg->idletime_threshold; |
2ee867dc TH |
1527 | while (true) { |
1528 | char tok[27]; /* wiops=18446744073709551616 */ | |
1529 | char *p; | |
2ab5492d | 1530 | u64 val = U64_MAX; |
2ee867dc TH |
1531 | int len; |
1532 | ||
1533 | if (sscanf(ctx.body, "%26s%n", tok, &len) != 1) | |
1534 | break; | |
1535 | if (tok[0] == '\0') | |
1536 | break; | |
1537 | ctx.body += len; | |
1538 | ||
1539 | ret = -EINVAL; | |
1540 | p = tok; | |
1541 | strsep(&p, "="); | |
1542 | if (!p || (sscanf(p, "%llu", &val) != 1 && strcmp(p, "max"))) | |
1543 | goto out_finish; | |
1544 | ||
1545 | ret = -ERANGE; | |
1546 | if (!val) | |
1547 | goto out_finish; | |
1548 | ||
1549 | ret = -EINVAL; | |
1550 | if (!strcmp(tok, "rbps")) | |
1551 | v[0] = val; | |
1552 | else if (!strcmp(tok, "wbps")) | |
1553 | v[1] = val; | |
1554 | else if (!strcmp(tok, "riops")) | |
1555 | v[2] = min_t(u64, val, UINT_MAX); | |
1556 | else if (!strcmp(tok, "wiops")) | |
1557 | v[3] = min_t(u64, val, UINT_MAX); | |
ada75b6e SL |
1558 | else if (off == LIMIT_LOW && !strcmp(tok, "idle")) |
1559 | idle_time = val; | |
2ee867dc TH |
1560 | else |
1561 | goto out_finish; | |
1562 | } | |
1563 | ||
cd5ab1b0 SL |
1564 | tg->bps_conf[READ][index] = v[0]; |
1565 | tg->bps_conf[WRITE][index] = v[1]; | |
1566 | tg->iops_conf[READ][index] = v[2]; | |
1567 | tg->iops_conf[WRITE][index] = v[3]; | |
2ee867dc | 1568 | |
cd5ab1b0 SL |
1569 | if (index == LIMIT_MAX) { |
1570 | tg->bps[READ][index] = v[0]; | |
1571 | tg->bps[WRITE][index] = v[1]; | |
1572 | tg->iops[READ][index] = v[2]; | |
1573 | tg->iops[WRITE][index] = v[3]; | |
1574 | } | |
1575 | tg->bps[READ][LIMIT_LOW] = min(tg->bps_conf[READ][LIMIT_LOW], | |
1576 | tg->bps_conf[READ][LIMIT_MAX]); | |
1577 | tg->bps[WRITE][LIMIT_LOW] = min(tg->bps_conf[WRITE][LIMIT_LOW], | |
1578 | tg->bps_conf[WRITE][LIMIT_MAX]); | |
1579 | tg->iops[READ][LIMIT_LOW] = min(tg->iops_conf[READ][LIMIT_LOW], | |
1580 | tg->iops_conf[READ][LIMIT_MAX]); | |
1581 | tg->iops[WRITE][LIMIT_LOW] = min(tg->iops_conf[WRITE][LIMIT_LOW], | |
1582 | tg->iops_conf[WRITE][LIMIT_MAX]); | |
1583 | ||
1584 | if (index == LIMIT_LOW) { | |
1585 | blk_throtl_update_limit_valid(tg->td); | |
1586 | if (tg->td->limit_valid[LIMIT_LOW]) | |
1587 | tg->td->limit_index = LIMIT_LOW; | |
ada75b6e SL |
1588 | tg->idletime_threshold = (idle_time == ULONG_MAX) ? |
1589 | ULONG_MAX : idle_time; | |
cd5ab1b0 | 1590 | } |
2ee867dc TH |
1591 | tg_conf_updated(tg); |
1592 | ret = 0; | |
1593 | out_finish: | |
1594 | blkg_conf_finish(&ctx); | |
1595 | return ret ?: nbytes; | |
1596 | } | |
1597 | ||
1598 | static struct cftype throtl_files[] = { | |
cd5ab1b0 SL |
1599 | #ifdef CONFIG_BLK_DEV_THROTTLING_LOW |
1600 | { | |
1601 | .name = "low", | |
1602 | .flags = CFTYPE_NOT_ON_ROOT, | |
1603 | .seq_show = tg_print_limit, | |
1604 | .write = tg_set_limit, | |
1605 | .private = LIMIT_LOW, | |
1606 | }, | |
1607 | #endif | |
2ee867dc TH |
1608 | { |
1609 | .name = "max", | |
1610 | .flags = CFTYPE_NOT_ON_ROOT, | |
cd5ab1b0 SL |
1611 | .seq_show = tg_print_limit, |
1612 | .write = tg_set_limit, | |
1613 | .private = LIMIT_MAX, | |
2ee867dc TH |
1614 | }, |
1615 | { } /* terminate */ | |
1616 | }; | |
1617 | ||
da527770 | 1618 | static void throtl_shutdown_wq(struct request_queue *q) |
e43473b7 VG |
1619 | { |
1620 | struct throtl_data *td = q->td; | |
1621 | ||
69df0ab0 | 1622 | cancel_work_sync(&td->dispatch_work); |
e43473b7 VG |
1623 | } |
1624 | ||
3c798398 | 1625 | static struct blkcg_policy blkcg_policy_throtl = { |
2ee867dc | 1626 | .dfl_cftypes = throtl_files, |
880f50e2 | 1627 | .legacy_cftypes = throtl_legacy_files, |
f9fcc2d3 | 1628 | |
001bea73 | 1629 | .pd_alloc_fn = throtl_pd_alloc, |
f9fcc2d3 | 1630 | .pd_init_fn = throtl_pd_init, |
693e751e | 1631 | .pd_online_fn = throtl_pd_online, |
cd5ab1b0 | 1632 | .pd_offline_fn = throtl_pd_offline, |
001bea73 | 1633 | .pd_free_fn = throtl_pd_free, |
e43473b7 VG |
1634 | }; |
1635 | ||
3f0abd80 SL |
1636 | static unsigned long __tg_last_low_overflow_time(struct throtl_grp *tg) |
1637 | { | |
1638 | unsigned long rtime = jiffies, wtime = jiffies; | |
1639 | ||
1640 | if (tg->bps[READ][LIMIT_LOW] || tg->iops[READ][LIMIT_LOW]) | |
1641 | rtime = tg->last_low_overflow_time[READ]; | |
1642 | if (tg->bps[WRITE][LIMIT_LOW] || tg->iops[WRITE][LIMIT_LOW]) | |
1643 | wtime = tg->last_low_overflow_time[WRITE]; | |
1644 | return min(rtime, wtime); | |
1645 | } | |
1646 | ||
1647 | /* tg should not be an intermediate node */ | |
1648 | static unsigned long tg_last_low_overflow_time(struct throtl_grp *tg) | |
1649 | { | |
1650 | struct throtl_service_queue *parent_sq; | |
1651 | struct throtl_grp *parent = tg; | |
1652 | unsigned long ret = __tg_last_low_overflow_time(tg); | |
1653 | ||
1654 | while (true) { | |
1655 | parent_sq = parent->service_queue.parent_sq; | |
1656 | parent = sq_to_tg(parent_sq); | |
1657 | if (!parent) | |
1658 | break; | |
1659 | ||
1660 | /* | |
1661 | * The parent doesn't have low limit, it always reaches low | |
1662 | * limit. Its overflow time is useless for children | |
1663 | */ | |
1664 | if (!parent->bps[READ][LIMIT_LOW] && | |
1665 | !parent->iops[READ][LIMIT_LOW] && | |
1666 | !parent->bps[WRITE][LIMIT_LOW] && | |
1667 | !parent->iops[WRITE][LIMIT_LOW]) | |
1668 | continue; | |
1669 | if (time_after(__tg_last_low_overflow_time(parent), ret)) | |
1670 | ret = __tg_last_low_overflow_time(parent); | |
1671 | } | |
1672 | return ret; | |
1673 | } | |
1674 | ||
9e234eea SL |
1675 | static bool throtl_tg_is_idle(struct throtl_grp *tg) |
1676 | { | |
1677 | /* | |
1678 | * cgroup is idle if: | |
1679 | * - single idle is too long, longer than a fixed value (in case user | |
1680 | * configure a too big threshold) or 4 times of slice | |
1681 | * - average think time is more than threshold | |
1682 | */ | |
1683 | unsigned long time = jiffies_to_usecs(4 * tg->td->throtl_slice); | |
1684 | ||
1685 | time = min_t(unsigned long, MAX_IDLE_TIME, time); | |
1686 | return (ktime_get_ns() >> 10) - tg->last_finish_time > time || | |
1687 | tg->avg_idletime > tg->idletime_threshold; | |
1688 | } | |
1689 | ||
c79892c5 SL |
1690 | static bool throtl_tg_can_upgrade(struct throtl_grp *tg) |
1691 | { | |
1692 | struct throtl_service_queue *sq = &tg->service_queue; | |
1693 | bool read_limit, write_limit; | |
1694 | ||
1695 | /* | |
1696 | * if cgroup reaches low limit (if low limit is 0, the cgroup always | |
1697 | * reaches), it's ok to upgrade to next limit | |
1698 | */ | |
1699 | read_limit = tg->bps[READ][LIMIT_LOW] || tg->iops[READ][LIMIT_LOW]; | |
1700 | write_limit = tg->bps[WRITE][LIMIT_LOW] || tg->iops[WRITE][LIMIT_LOW]; | |
1701 | if (!read_limit && !write_limit) | |
1702 | return true; | |
1703 | if (read_limit && sq->nr_queued[READ] && | |
1704 | (!write_limit || sq->nr_queued[WRITE])) | |
1705 | return true; | |
1706 | if (write_limit && sq->nr_queued[WRITE] && | |
1707 | (!read_limit || sq->nr_queued[READ])) | |
1708 | return true; | |
aec24246 SL |
1709 | |
1710 | if (time_after_eq(jiffies, | |
1711 | tg->last_dispatch_time[READ] + tg->td->throtl_slice) && | |
1712 | time_after_eq(jiffies, | |
1713 | tg->last_dispatch_time[WRITE] + tg->td->throtl_slice)) | |
1714 | return true; | |
c79892c5 SL |
1715 | return false; |
1716 | } | |
1717 | ||
1718 | static bool throtl_hierarchy_can_upgrade(struct throtl_grp *tg) | |
1719 | { | |
1720 | while (true) { | |
1721 | if (throtl_tg_can_upgrade(tg)) | |
1722 | return true; | |
1723 | tg = sq_to_tg(tg->service_queue.parent_sq); | |
1724 | if (!tg || !tg_to_blkg(tg)->parent) | |
1725 | return false; | |
1726 | } | |
1727 | return false; | |
1728 | } | |
1729 | ||
1730 | static bool throtl_can_upgrade(struct throtl_data *td, | |
1731 | struct throtl_grp *this_tg) | |
1732 | { | |
1733 | struct cgroup_subsys_state *pos_css; | |
1734 | struct blkcg_gq *blkg; | |
1735 | ||
1736 | if (td->limit_index != LIMIT_LOW) | |
1737 | return false; | |
1738 | ||
297e3d85 | 1739 | if (time_before(jiffies, td->low_downgrade_time + td->throtl_slice)) |
3f0abd80 SL |
1740 | return false; |
1741 | ||
c79892c5 SL |
1742 | rcu_read_lock(); |
1743 | blkg_for_each_descendant_post(blkg, pos_css, td->queue->root_blkg) { | |
1744 | struct throtl_grp *tg = blkg_to_tg(blkg); | |
1745 | ||
1746 | if (tg == this_tg) | |
1747 | continue; | |
1748 | if (!list_empty(&tg_to_blkg(tg)->blkcg->css.children)) | |
1749 | continue; | |
1750 | if (!throtl_hierarchy_can_upgrade(tg)) { | |
1751 | rcu_read_unlock(); | |
1752 | return false; | |
1753 | } | |
1754 | } | |
1755 | rcu_read_unlock(); | |
1756 | return true; | |
1757 | } | |
1758 | ||
1759 | static void throtl_upgrade_state(struct throtl_data *td) | |
1760 | { | |
1761 | struct cgroup_subsys_state *pos_css; | |
1762 | struct blkcg_gq *blkg; | |
1763 | ||
1764 | td->limit_index = LIMIT_MAX; | |
3f0abd80 | 1765 | td->low_upgrade_time = jiffies; |
7394e31f | 1766 | td->scale = 0; |
c79892c5 SL |
1767 | rcu_read_lock(); |
1768 | blkg_for_each_descendant_post(blkg, pos_css, td->queue->root_blkg) { | |
1769 | struct throtl_grp *tg = blkg_to_tg(blkg); | |
1770 | struct throtl_service_queue *sq = &tg->service_queue; | |
1771 | ||
1772 | tg->disptime = jiffies - 1; | |
1773 | throtl_select_dispatch(sq); | |
1774 | throtl_schedule_next_dispatch(sq, false); | |
1775 | } | |
1776 | rcu_read_unlock(); | |
1777 | throtl_select_dispatch(&td->service_queue); | |
1778 | throtl_schedule_next_dispatch(&td->service_queue, false); | |
1779 | queue_work(kthrotld_workqueue, &td->dispatch_work); | |
1780 | } | |
1781 | ||
3f0abd80 SL |
1782 | static void throtl_downgrade_state(struct throtl_data *td, int new) |
1783 | { | |
7394e31f SL |
1784 | td->scale /= 2; |
1785 | ||
1786 | if (td->scale) { | |
1787 | td->low_upgrade_time = jiffies - td->scale * td->throtl_slice; | |
1788 | return; | |
1789 | } | |
1790 | ||
3f0abd80 SL |
1791 | td->limit_index = new; |
1792 | td->low_downgrade_time = jiffies; | |
1793 | } | |
1794 | ||
1795 | static bool throtl_tg_can_downgrade(struct throtl_grp *tg) | |
1796 | { | |
1797 | struct throtl_data *td = tg->td; | |
1798 | unsigned long now = jiffies; | |
1799 | ||
aec24246 SL |
1800 | if (time_after_eq(now, tg->last_dispatch_time[READ] + |
1801 | td->throtl_slice) && | |
1802 | time_after_eq(now, tg->last_dispatch_time[WRITE] + | |
1803 | td->throtl_slice)) | |
1804 | return false; | |
3f0abd80 SL |
1805 | /* |
1806 | * If cgroup is below low limit, consider downgrade and throttle other | |
1807 | * cgroups | |
1808 | */ | |
297e3d85 SL |
1809 | if (time_after_eq(now, td->low_upgrade_time + td->throtl_slice) && |
1810 | time_after_eq(now, tg_last_low_overflow_time(tg) + | |
1811 | td->throtl_slice)) | |
3f0abd80 SL |
1812 | return true; |
1813 | return false; | |
1814 | } | |
1815 | ||
1816 | static bool throtl_hierarchy_can_downgrade(struct throtl_grp *tg) | |
1817 | { | |
1818 | while (true) { | |
1819 | if (!throtl_tg_can_downgrade(tg)) | |
1820 | return false; | |
1821 | tg = sq_to_tg(tg->service_queue.parent_sq); | |
1822 | if (!tg || !tg_to_blkg(tg)->parent) | |
1823 | break; | |
1824 | } | |
1825 | return true; | |
1826 | } | |
1827 | ||
1828 | static void throtl_downgrade_check(struct throtl_grp *tg) | |
1829 | { | |
1830 | uint64_t bps; | |
1831 | unsigned int iops; | |
1832 | unsigned long elapsed_time; | |
1833 | unsigned long now = jiffies; | |
1834 | ||
1835 | if (tg->td->limit_index != LIMIT_MAX || | |
1836 | !tg->td->limit_valid[LIMIT_LOW]) | |
1837 | return; | |
1838 | if (!list_empty(&tg_to_blkg(tg)->blkcg->css.children)) | |
1839 | return; | |
297e3d85 | 1840 | if (time_after(tg->last_check_time + tg->td->throtl_slice, now)) |
3f0abd80 SL |
1841 | return; |
1842 | ||
1843 | elapsed_time = now - tg->last_check_time; | |
1844 | tg->last_check_time = now; | |
1845 | ||
297e3d85 SL |
1846 | if (time_before(now, tg_last_low_overflow_time(tg) + |
1847 | tg->td->throtl_slice)) | |
3f0abd80 SL |
1848 | return; |
1849 | ||
1850 | if (tg->bps[READ][LIMIT_LOW]) { | |
1851 | bps = tg->last_bytes_disp[READ] * HZ; | |
1852 | do_div(bps, elapsed_time); | |
1853 | if (bps >= tg->bps[READ][LIMIT_LOW]) | |
1854 | tg->last_low_overflow_time[READ] = now; | |
1855 | } | |
1856 | ||
1857 | if (tg->bps[WRITE][LIMIT_LOW]) { | |
1858 | bps = tg->last_bytes_disp[WRITE] * HZ; | |
1859 | do_div(bps, elapsed_time); | |
1860 | if (bps >= tg->bps[WRITE][LIMIT_LOW]) | |
1861 | tg->last_low_overflow_time[WRITE] = now; | |
1862 | } | |
1863 | ||
1864 | if (tg->iops[READ][LIMIT_LOW]) { | |
1865 | iops = tg->last_io_disp[READ] * HZ / elapsed_time; | |
1866 | if (iops >= tg->iops[READ][LIMIT_LOW]) | |
1867 | tg->last_low_overflow_time[READ] = now; | |
1868 | } | |
1869 | ||
1870 | if (tg->iops[WRITE][LIMIT_LOW]) { | |
1871 | iops = tg->last_io_disp[WRITE] * HZ / elapsed_time; | |
1872 | if (iops >= tg->iops[WRITE][LIMIT_LOW]) | |
1873 | tg->last_low_overflow_time[WRITE] = now; | |
1874 | } | |
1875 | ||
1876 | /* | |
1877 | * If cgroup is below low limit, consider downgrade and throttle other | |
1878 | * cgroups | |
1879 | */ | |
1880 | if (throtl_hierarchy_can_downgrade(tg)) | |
1881 | throtl_downgrade_state(tg->td, LIMIT_LOW); | |
1882 | ||
1883 | tg->last_bytes_disp[READ] = 0; | |
1884 | tg->last_bytes_disp[WRITE] = 0; | |
1885 | tg->last_io_disp[READ] = 0; | |
1886 | tg->last_io_disp[WRITE] = 0; | |
1887 | } | |
1888 | ||
9e234eea SL |
1889 | static void blk_throtl_update_idletime(struct throtl_grp *tg) |
1890 | { | |
1891 | unsigned long now = ktime_get_ns() >> 10; | |
1892 | unsigned long last_finish_time = tg->last_finish_time; | |
1893 | ||
1894 | if (now <= last_finish_time || last_finish_time == 0 || | |
1895 | last_finish_time == tg->checked_last_finish_time) | |
1896 | return; | |
1897 | ||
1898 | tg->avg_idletime = (tg->avg_idletime * 7 + now - last_finish_time) >> 3; | |
1899 | tg->checked_last_finish_time = last_finish_time; | |
1900 | } | |
1901 | ||
ae118896 TH |
1902 | bool blk_throtl_bio(struct request_queue *q, struct blkcg_gq *blkg, |
1903 | struct bio *bio) | |
e43473b7 | 1904 | { |
c5cc2070 | 1905 | struct throtl_qnode *qn = NULL; |
ae118896 | 1906 | struct throtl_grp *tg = blkg_to_tg(blkg ?: q->root_blkg); |
73f0d49a | 1907 | struct throtl_service_queue *sq; |
0e9f4164 | 1908 | bool rw = bio_data_dir(bio); |
bc16a4f9 | 1909 | bool throttled = false; |
9e234eea | 1910 | int ret; |
e43473b7 | 1911 | |
ae118896 TH |
1912 | WARN_ON_ONCE(!rcu_read_lock_held()); |
1913 | ||
2a0f61e6 | 1914 | /* see throtl_charge_bio() */ |
8d2bbd4c | 1915 | if (bio_flagged(bio, BIO_THROTTLED) || !tg->has_rules[rw]) |
bc16a4f9 | 1916 | goto out; |
e43473b7 VG |
1917 | |
1918 | spin_lock_irq(q->queue_lock); | |
c9589f03 TH |
1919 | |
1920 | if (unlikely(blk_queue_bypass(q))) | |
bc16a4f9 | 1921 | goto out_unlock; |
f469a7b4 | 1922 | |
9e234eea SL |
1923 | ret = bio_associate_current(bio); |
1924 | #ifdef CONFIG_BLK_DEV_THROTTLING_LOW | |
1925 | if (ret == 0 || ret == -EBUSY) | |
1926 | bio->bi_cg_private = tg; | |
1927 | #endif | |
1928 | blk_throtl_update_idletime(tg); | |
1929 | ||
73f0d49a TH |
1930 | sq = &tg->service_queue; |
1931 | ||
c79892c5 | 1932 | again: |
9e660acf | 1933 | while (true) { |
aec24246 | 1934 | tg->last_dispatch_time[rw] = jiffies; |
3f0abd80 SL |
1935 | if (tg->last_low_overflow_time[rw] == 0) |
1936 | tg->last_low_overflow_time[rw] = jiffies; | |
1937 | throtl_downgrade_check(tg); | |
9e660acf TH |
1938 | /* throtl is FIFO - if bios are already queued, should queue */ |
1939 | if (sq->nr_queued[rw]) | |
1940 | break; | |
de701c74 | 1941 | |
9e660acf | 1942 | /* if above limits, break to queue */ |
c79892c5 | 1943 | if (!tg_may_dispatch(tg, bio, NULL)) { |
3f0abd80 | 1944 | tg->last_low_overflow_time[rw] = jiffies; |
c79892c5 SL |
1945 | if (throtl_can_upgrade(tg->td, tg)) { |
1946 | throtl_upgrade_state(tg->td); | |
1947 | goto again; | |
1948 | } | |
9e660acf | 1949 | break; |
c79892c5 | 1950 | } |
9e660acf TH |
1951 | |
1952 | /* within limits, let's charge and dispatch directly */ | |
e43473b7 | 1953 | throtl_charge_bio(tg, bio); |
04521db0 VG |
1954 | |
1955 | /* | |
1956 | * We need to trim slice even when bios are not being queued | |
1957 | * otherwise it might happen that a bio is not queued for | |
1958 | * a long time and slice keeps on extending and trim is not | |
1959 | * called for a long time. Now if limits are reduced suddenly | |
1960 | * we take into account all the IO dispatched so far at new | |
1961 | * low rate and * newly queued IO gets a really long dispatch | |
1962 | * time. | |
1963 | * | |
1964 | * So keep on trimming slice even if bio is not queued. | |
1965 | */ | |
0f3457f6 | 1966 | throtl_trim_slice(tg, rw); |
9e660acf TH |
1967 | |
1968 | /* | |
1969 | * @bio passed through this layer without being throttled. | |
1970 | * Climb up the ladder. If we''re already at the top, it | |
1971 | * can be executed directly. | |
1972 | */ | |
c5cc2070 | 1973 | qn = &tg->qnode_on_parent[rw]; |
9e660acf TH |
1974 | sq = sq->parent_sq; |
1975 | tg = sq_to_tg(sq); | |
1976 | if (!tg) | |
1977 | goto out_unlock; | |
e43473b7 VG |
1978 | } |
1979 | ||
9e660acf | 1980 | /* out-of-limit, queue to @tg */ |
fda6f272 TH |
1981 | throtl_log(sq, "[%c] bio. bdisp=%llu sz=%u bps=%llu iodisp=%u iops=%u queued=%d/%d", |
1982 | rw == READ ? 'R' : 'W', | |
9f626e37 SL |
1983 | tg->bytes_disp[rw], bio->bi_iter.bi_size, |
1984 | tg_bps_limit(tg, rw), | |
1985 | tg->io_disp[rw], tg_iops_limit(tg, rw), | |
fda6f272 | 1986 | sq->nr_queued[READ], sq->nr_queued[WRITE]); |
e43473b7 | 1987 | |
3f0abd80 SL |
1988 | tg->last_low_overflow_time[rw] = jiffies; |
1989 | ||
6bc9c2b4 | 1990 | tg->td->nr_queued[rw]++; |
c5cc2070 | 1991 | throtl_add_bio_tg(bio, qn, tg); |
bc16a4f9 | 1992 | throttled = true; |
e43473b7 | 1993 | |
7f52f98c TH |
1994 | /* |
1995 | * Update @tg's dispatch time and force schedule dispatch if @tg | |
1996 | * was empty before @bio. The forced scheduling isn't likely to | |
1997 | * cause undue delay as @bio is likely to be dispatched directly if | |
1998 | * its @tg's disptime is not in the future. | |
1999 | */ | |
0e9f4164 | 2000 | if (tg->flags & THROTL_TG_WAS_EMPTY) { |
77216b04 | 2001 | tg_update_disptime(tg); |
7f52f98c | 2002 | throtl_schedule_next_dispatch(tg->service_queue.parent_sq, true); |
e43473b7 VG |
2003 | } |
2004 | ||
bc16a4f9 | 2005 | out_unlock: |
e43473b7 | 2006 | spin_unlock_irq(q->queue_lock); |
bc16a4f9 | 2007 | out: |
2a0f61e6 TH |
2008 | /* |
2009 | * As multiple blk-throtls may stack in the same issue path, we | |
2010 | * don't want bios to leave with the flag set. Clear the flag if | |
2011 | * being issued. | |
2012 | */ | |
2013 | if (!throttled) | |
8d2bbd4c | 2014 | bio_clear_flag(bio, BIO_THROTTLED); |
bc16a4f9 | 2015 | return throttled; |
e43473b7 VG |
2016 | } |
2017 | ||
9e234eea SL |
2018 | #ifdef CONFIG_BLK_DEV_THROTTLING_LOW |
2019 | void blk_throtl_bio_endio(struct bio *bio) | |
2020 | { | |
2021 | struct throtl_grp *tg; | |
2022 | ||
2023 | tg = bio->bi_cg_private; | |
2024 | if (!tg) | |
2025 | return; | |
2026 | bio->bi_cg_private = NULL; | |
2027 | ||
2028 | tg->last_finish_time = ktime_get_ns() >> 10; | |
2029 | } | |
2030 | #endif | |
2031 | ||
2a12f0dc TH |
2032 | /* |
2033 | * Dispatch all bios from all children tg's queued on @parent_sq. On | |
2034 | * return, @parent_sq is guaranteed to not have any active children tg's | |
2035 | * and all bios from previously active tg's are on @parent_sq->bio_lists[]. | |
2036 | */ | |
2037 | static void tg_drain_bios(struct throtl_service_queue *parent_sq) | |
2038 | { | |
2039 | struct throtl_grp *tg; | |
2040 | ||
2041 | while ((tg = throtl_rb_first(parent_sq))) { | |
2042 | struct throtl_service_queue *sq = &tg->service_queue; | |
2043 | struct bio *bio; | |
2044 | ||
2045 | throtl_dequeue_tg(tg); | |
2046 | ||
c5cc2070 | 2047 | while ((bio = throtl_peek_queued(&sq->queued[READ]))) |
2a12f0dc | 2048 | tg_dispatch_one_bio(tg, bio_data_dir(bio)); |
c5cc2070 | 2049 | while ((bio = throtl_peek_queued(&sq->queued[WRITE]))) |
2a12f0dc TH |
2050 | tg_dispatch_one_bio(tg, bio_data_dir(bio)); |
2051 | } | |
2052 | } | |
2053 | ||
c9a929dd TH |
2054 | /** |
2055 | * blk_throtl_drain - drain throttled bios | |
2056 | * @q: request_queue to drain throttled bios for | |
2057 | * | |
2058 | * Dispatch all currently throttled bios on @q through ->make_request_fn(). | |
2059 | */ | |
2060 | void blk_throtl_drain(struct request_queue *q) | |
2061 | __releases(q->queue_lock) __acquires(q->queue_lock) | |
2062 | { | |
2063 | struct throtl_data *td = q->td; | |
2a12f0dc | 2064 | struct blkcg_gq *blkg; |
492eb21b | 2065 | struct cgroup_subsys_state *pos_css; |
c9a929dd | 2066 | struct bio *bio; |
651930bc | 2067 | int rw; |
c9a929dd | 2068 | |
8bcb6c7d | 2069 | queue_lockdep_assert_held(q); |
2a12f0dc | 2070 | rcu_read_lock(); |
c9a929dd | 2071 | |
2a12f0dc TH |
2072 | /* |
2073 | * Drain each tg while doing post-order walk on the blkg tree, so | |
2074 | * that all bios are propagated to td->service_queue. It'd be | |
2075 | * better to walk service_queue tree directly but blkg walk is | |
2076 | * easier. | |
2077 | */ | |
492eb21b | 2078 | blkg_for_each_descendant_post(blkg, pos_css, td->queue->root_blkg) |
2a12f0dc | 2079 | tg_drain_bios(&blkg_to_tg(blkg)->service_queue); |
73f0d49a | 2080 | |
2a12f0dc TH |
2081 | /* finally, transfer bios from top-level tg's into the td */ |
2082 | tg_drain_bios(&td->service_queue); | |
2083 | ||
2084 | rcu_read_unlock(); | |
c9a929dd TH |
2085 | spin_unlock_irq(q->queue_lock); |
2086 | ||
2a12f0dc | 2087 | /* all bios now should be in td->service_queue, issue them */ |
651930bc | 2088 | for (rw = READ; rw <= WRITE; rw++) |
c5cc2070 TH |
2089 | while ((bio = throtl_pop_queued(&td->service_queue.queued[rw], |
2090 | NULL))) | |
651930bc | 2091 | generic_make_request(bio); |
c9a929dd TH |
2092 | |
2093 | spin_lock_irq(q->queue_lock); | |
2094 | } | |
2095 | ||
e43473b7 VG |
2096 | int blk_throtl_init(struct request_queue *q) |
2097 | { | |
2098 | struct throtl_data *td; | |
a2b1693b | 2099 | int ret; |
e43473b7 VG |
2100 | |
2101 | td = kzalloc_node(sizeof(*td), GFP_KERNEL, q->node); | |
2102 | if (!td) | |
2103 | return -ENOMEM; | |
2104 | ||
69df0ab0 | 2105 | INIT_WORK(&td->dispatch_work, blk_throtl_dispatch_work_fn); |
b2ce2643 | 2106 | throtl_service_queue_init(&td->service_queue); |
e43473b7 | 2107 | |
cd1604fa | 2108 | q->td = td; |
29b12589 | 2109 | td->queue = q; |
02977e4a | 2110 | |
9f626e37 | 2111 | td->limit_valid[LIMIT_MAX] = true; |
cd5ab1b0 | 2112 | td->limit_index = LIMIT_MAX; |
3f0abd80 SL |
2113 | td->low_upgrade_time = jiffies; |
2114 | td->low_downgrade_time = jiffies; | |
9e234eea | 2115 | |
a2b1693b | 2116 | /* activate policy */ |
3c798398 | 2117 | ret = blkcg_activate_policy(q, &blkcg_policy_throtl); |
a2b1693b | 2118 | if (ret) |
f51b802c | 2119 | kfree(td); |
a2b1693b | 2120 | return ret; |
e43473b7 VG |
2121 | } |
2122 | ||
2123 | void blk_throtl_exit(struct request_queue *q) | |
2124 | { | |
c875f4d0 | 2125 | BUG_ON(!q->td); |
da527770 | 2126 | throtl_shutdown_wq(q); |
3c798398 | 2127 | blkcg_deactivate_policy(q, &blkcg_policy_throtl); |
c9a929dd | 2128 | kfree(q->td); |
e43473b7 VG |
2129 | } |
2130 | ||
d61fcfa4 SL |
2131 | void blk_throtl_register_queue(struct request_queue *q) |
2132 | { | |
2133 | struct throtl_data *td; | |
9e234eea SL |
2134 | struct cgroup_subsys_state *pos_css; |
2135 | struct blkcg_gq *blkg; | |
d61fcfa4 SL |
2136 | |
2137 | td = q->td; | |
2138 | BUG_ON(!td); | |
2139 | ||
ada75b6e | 2140 | if (blk_queue_nonrot(q)) { |
d61fcfa4 | 2141 | td->throtl_slice = DFL_THROTL_SLICE_SSD; |
ada75b6e SL |
2142 | td->dft_idletime_threshold = DFL_IDLE_THRESHOLD_SSD; |
2143 | } else { | |
d61fcfa4 | 2144 | td->throtl_slice = DFL_THROTL_SLICE_HD; |
ada75b6e SL |
2145 | td->dft_idletime_threshold = DFL_IDLE_THRESHOLD_HD; |
2146 | } | |
d61fcfa4 SL |
2147 | #ifndef CONFIG_BLK_DEV_THROTTLING_LOW |
2148 | /* if no low limit, use previous default */ | |
2149 | td->throtl_slice = DFL_THROTL_SLICE_HD; | |
2150 | #endif | |
9e234eea SL |
2151 | |
2152 | /* | |
2153 | * some tg are created before queue is fully initialized, eg, nonrot | |
2154 | * isn't initialized yet | |
2155 | */ | |
2156 | rcu_read_lock(); | |
2157 | blkg_for_each_descendant_post(blkg, pos_css, q->root_blkg) { | |
2158 | struct throtl_grp *tg = blkg_to_tg(blkg); | |
2159 | ||
ada75b6e | 2160 | tg->idletime_threshold = td->dft_idletime_threshold; |
9e234eea SL |
2161 | } |
2162 | rcu_read_unlock(); | |
d61fcfa4 SL |
2163 | } |
2164 | ||
297e3d85 SL |
2165 | #ifdef CONFIG_BLK_DEV_THROTTLING_LOW |
2166 | ssize_t blk_throtl_sample_time_show(struct request_queue *q, char *page) | |
2167 | { | |
2168 | if (!q->td) | |
2169 | return -EINVAL; | |
2170 | return sprintf(page, "%u\n", jiffies_to_msecs(q->td->throtl_slice)); | |
2171 | } | |
2172 | ||
2173 | ssize_t blk_throtl_sample_time_store(struct request_queue *q, | |
2174 | const char *page, size_t count) | |
2175 | { | |
2176 | unsigned long v; | |
2177 | unsigned long t; | |
2178 | ||
2179 | if (!q->td) | |
2180 | return -EINVAL; | |
2181 | if (kstrtoul(page, 10, &v)) | |
2182 | return -EINVAL; | |
2183 | t = msecs_to_jiffies(v); | |
2184 | if (t == 0 || t > MAX_THROTL_SLICE) | |
2185 | return -EINVAL; | |
2186 | q->td->throtl_slice = t; | |
2187 | return count; | |
2188 | } | |
2189 | #endif | |
2190 | ||
e43473b7 VG |
2191 | static int __init throtl_init(void) |
2192 | { | |
450adcbe VG |
2193 | kthrotld_workqueue = alloc_workqueue("kthrotld", WQ_MEM_RECLAIM, 0); |
2194 | if (!kthrotld_workqueue) | |
2195 | panic("Failed to create kthrotld\n"); | |
2196 | ||
3c798398 | 2197 | return blkcg_policy_register(&blkcg_policy_throtl); |
e43473b7 VG |
2198 | } |
2199 | ||
2200 | module_init(throtl_init); |