1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * buffered writeback throttling. loosely based on CoDel. We can't drop
4 * packets for IO scheduling, so the logic is something like this:
5 *
6 * - Monitor latencies in a defined window of time.
7 * - If the minimum latency in the above window exceeds some target, increment
8 * scaling step and scale down queue depth by a factor of 2x. The monitoring
9 * window is then shrunk to 100 / sqrt(scaling step + 1).
10 * - For any window where we don't have solid data on what the latencies
11 * look like, retain status quo.
12 * - If latencies look good, decrement scaling step.
13 * - If we're only doing writes, allow the scaling step to go negative. This
14 * will temporarily boost write performance, snapping back to a stable
15 * scaling step of 0 if reads show up or the heavy writers finish. Unlike
16 * positive scaling steps where we shrink the monitoring window, a negative
17 * scaling step retains the default step==0 window size.
18 *
19 * Copyright (C) 2016 Jens Axboe
20 *
21 */
22 #include <linux/kernel.h>
23 #include <linux/blk_types.h>
24 #include <linux/slab.h>
25 #include <linux/backing-dev.h>
26 #include <linux/swap.h>
27
28 #include "blk-stat.h"
29 #include "blk-wbt.h"
30 #include "blk-rq-qos.h"
31 #include "elevator.h"
32 #include "blk.h"
33
34 #define CREATE_TRACE_POINTS
35 #include <trace/events/wbt.h>
36
37 enum wbt_flags {
38 WBT_TRACKED = 1, /* write, tracked for throttling */
39 WBT_READ = 2, /* read */
40 WBT_SWAP = 4, /* write, from swap_writeout() */
41 WBT_DISCARD = 8, /* discard */
42
43 WBT_NR_BITS = 4, /* number of bits */
44 };
45
46 enum {
47 WBT_RWQ_BG = 0,
48 WBT_RWQ_SWAP,
49 WBT_RWQ_DISCARD,
50 WBT_NUM_RWQ,
51 };
52
53 /*
54 * If current state is WBT_STATE_ON/OFF_DEFAULT, it can be covered to any other
55 * state, if current state is WBT_STATE_ON/OFF_MANUAL, it can only be covered
56 * to WBT_STATE_OFF/ON_MANUAL.
57 */
58 enum {
59 WBT_STATE_ON_DEFAULT = 1, /* on by default */
60 WBT_STATE_ON_MANUAL = 2, /* on manually by sysfs */
61 WBT_STATE_OFF_DEFAULT = 3, /* off by default */
62 WBT_STATE_OFF_MANUAL = 4, /* off manually by sysfs */
63 };
64
65 struct rq_wb {
66 /*
67 * Settings that govern how we throttle
68 */
69 unsigned int wb_background; /* background writeback */
70 unsigned int wb_normal; /* normal writeback */
71
72 short enable_state; /* WBT_STATE_* */
73
74 /*
75 * Number of consecutive periods where we don't have enough
76 * information to make a firm scale up/down decision.
77 */
78 unsigned int unknown_cnt;
79
80 u64 win_nsec; /* default window size */
81 u64 cur_win_nsec; /* current window size */
82
83 struct blk_stat_callback *cb;
84
85 u64 sync_issue;
86 void *sync_cookie;
87
88 unsigned long last_issue; /* issue time of last read rq */
89 unsigned long last_comp; /* completion time of last read rq */
90 unsigned long min_lat_nsec;
91 struct rq_qos rqos;
92 struct rq_wait rq_wait[WBT_NUM_RWQ];
93 struct rq_depth rq_depth;
94 };
95
RQWB(struct rq_qos * rqos)96 static inline struct rq_wb *RQWB(struct rq_qos *rqos)
97 {
98 return container_of(rqos, struct rq_wb, rqos);
99 }
100
wbt_clear_state(struct request * rq)101 static inline void wbt_clear_state(struct request *rq)
102 {
103 rq->wbt_flags = 0;
104 }
105
wbt_flags(struct request * rq)106 static inline enum wbt_flags wbt_flags(struct request *rq)
107 {
108 return rq->wbt_flags;
109 }
110
wbt_is_tracked(struct request * rq)111 static inline bool wbt_is_tracked(struct request *rq)
112 {
113 return rq->wbt_flags & WBT_TRACKED;
114 }
115
wbt_is_read(struct request * rq)116 static inline bool wbt_is_read(struct request *rq)
117 {
118 return rq->wbt_flags & WBT_READ;
119 }
120
121 enum {
122 /*
123 * Default setting, we'll scale up (to 75% of QD max) or down (min 1)
124 * from here depending on device stats
125 */
126 RWB_DEF_DEPTH = 16,
127
128 /*
129 * 100msec window
130 */
131 RWB_WINDOW_NSEC = 100 * 1000 * 1000ULL,
132
133 /*
134 * Disregard stats, if we don't meet this minimum
135 */
136 RWB_MIN_WRITE_SAMPLES = 3,
137
138 /*
139 * If we have this number of consecutive windows without enough
140 * information to scale up or down, slowly return to center state
141 * (step == 0).
142 */
143 RWB_UNKNOWN_BUMP = 5,
144 };
145
rwb_enabled(struct rq_wb * rwb)146 static inline bool rwb_enabled(struct rq_wb *rwb)
147 {
148 return rwb && rwb->enable_state != WBT_STATE_OFF_DEFAULT &&
149 rwb->enable_state != WBT_STATE_OFF_MANUAL;
150 }
151
wb_timestamp(struct rq_wb * rwb,unsigned long * var)152 static void wb_timestamp(struct rq_wb *rwb, unsigned long *var)
153 {
154 if (rwb_enabled(rwb)) {
155 const unsigned long cur = jiffies;
156
157 if (cur != *var)
158 *var = cur;
159 }
160 }
161
162 /*
163 * If a task was rate throttled in balance_dirty_pages() within the last
164 * second or so, use that to indicate a higher cleaning rate.
165 */
wb_recent_wait(struct rq_wb * rwb)166 static bool wb_recent_wait(struct rq_wb *rwb)
167 {
168 struct backing_dev_info *bdi = rwb->rqos.disk->bdi;
169
170 return time_before(jiffies, bdi->last_bdp_sleep + HZ);
171 }
172
get_rq_wait(struct rq_wb * rwb,enum wbt_flags wb_acct)173 static inline struct rq_wait *get_rq_wait(struct rq_wb *rwb,
174 enum wbt_flags wb_acct)
175 {
176 if (wb_acct & WBT_SWAP)
177 return &rwb->rq_wait[WBT_RWQ_SWAP];
178 else if (wb_acct & WBT_DISCARD)
179 return &rwb->rq_wait[WBT_RWQ_DISCARD];
180
181 return &rwb->rq_wait[WBT_RWQ_BG];
182 }
183
rwb_wake_all(struct rq_wb * rwb)184 static void rwb_wake_all(struct rq_wb *rwb)
185 {
186 int i;
187
188 for (i = 0; i < WBT_NUM_RWQ; i++) {
189 struct rq_wait *rqw = &rwb->rq_wait[i];
190
191 if (wq_has_sleeper(&rqw->wait))
192 wake_up_all(&rqw->wait);
193 }
194 }
195
wbt_rqw_done(struct rq_wb * rwb,struct rq_wait * rqw,enum wbt_flags wb_acct)196 static void wbt_rqw_done(struct rq_wb *rwb, struct rq_wait *rqw,
197 enum wbt_flags wb_acct)
198 {
199 int inflight, limit;
200
201 inflight = atomic_dec_return(&rqw->inflight);
202
203 /*
204 * For discards, our limit is always the background. For writes, if
205 * the device does write back caching, drop further down before we
206 * wake people up.
207 */
208 if (wb_acct & WBT_DISCARD)
209 limit = rwb->wb_background;
210 else if (blk_queue_write_cache(rwb->rqos.disk->queue) &&
211 !wb_recent_wait(rwb))
212 limit = 0;
213 else
214 limit = rwb->wb_normal;
215
216 /*
217 * Don't wake anyone up if we are above the normal limit.
218 */
219 if (inflight && inflight >= limit)
220 return;
221
222 if (wq_has_sleeper(&rqw->wait)) {
223 int diff = limit - inflight;
224
225 if (!inflight || diff >= rwb->wb_background / 2)
226 wake_up_all(&rqw->wait);
227 }
228 }
229
__wbt_done(struct rq_qos * rqos,enum wbt_flags wb_acct)230 static void __wbt_done(struct rq_qos *rqos, enum wbt_flags wb_acct)
231 {
232 struct rq_wb *rwb = RQWB(rqos);
233 struct rq_wait *rqw;
234
235 if (!(wb_acct & WBT_TRACKED))
236 return;
237
238 rqw = get_rq_wait(rwb, wb_acct);
239 wbt_rqw_done(rwb, rqw, wb_acct);
240 }
241
242 /*
243 * Called on completion of a request. Note that it's also called when
244 * a request is merged, when the request gets freed.
245 */
wbt_done(struct rq_qos * rqos,struct request * rq)246 static void wbt_done(struct rq_qos *rqos, struct request *rq)
247 {
248 struct rq_wb *rwb = RQWB(rqos);
249
250 if (!wbt_is_tracked(rq)) {
251 if (wbt_is_read(rq)) {
252 if (rwb->sync_cookie == rq) {
253 rwb->sync_issue = 0;
254 rwb->sync_cookie = NULL;
255 }
256
257 wb_timestamp(rwb, &rwb->last_comp);
258 }
259 } else {
260 WARN_ON_ONCE(rq == rwb->sync_cookie);
261 __wbt_done(rqos, wbt_flags(rq));
262 }
263 wbt_clear_state(rq);
264 }
265
stat_sample_valid(struct blk_rq_stat * stat)266 static inline bool stat_sample_valid(struct blk_rq_stat *stat)
267 {
268 /*
269 * We need at least one read sample, and a minimum of
270 * RWB_MIN_WRITE_SAMPLES. We require some write samples to know
271 * that it's writes impacting us, and not just some sole read on
272 * a device that is in a lower power state.
273 */
274 return (stat[READ].nr_samples >= 1 &&
275 stat[WRITE].nr_samples >= RWB_MIN_WRITE_SAMPLES);
276 }
277
rwb_sync_issue_lat(struct rq_wb * rwb)278 static u64 rwb_sync_issue_lat(struct rq_wb *rwb)
279 {
280 u64 issue = READ_ONCE(rwb->sync_issue);
281
282 if (!issue || !rwb->sync_cookie)
283 return 0;
284
285 return blk_time_get_ns() - issue;
286 }
287
wbt_inflight(struct rq_wb * rwb)288 static inline unsigned int wbt_inflight(struct rq_wb *rwb)
289 {
290 unsigned int i, ret = 0;
291
292 for (i = 0; i < WBT_NUM_RWQ; i++)
293 ret += atomic_read(&rwb->rq_wait[i].inflight);
294
295 return ret;
296 }
297
298 enum {
299 LAT_OK = 1,
300 LAT_UNKNOWN,
301 LAT_UNKNOWN_WRITES,
302 LAT_EXCEEDED,
303 };
304
latency_exceeded(struct rq_wb * rwb,struct blk_rq_stat * stat)305 static int latency_exceeded(struct rq_wb *rwb, struct blk_rq_stat *stat)
306 {
307 struct backing_dev_info *bdi = rwb->rqos.disk->bdi;
308 struct rq_depth *rqd = &rwb->rq_depth;
309 u64 thislat;
310
311 /*
312 * If our stored sync issue exceeds the window size, or it
313 * exceeds our min target AND we haven't logged any entries,
314 * flag the latency as exceeded. wbt works off completion latencies,
315 * but for a flooded device, a single sync IO can take a long time
316 * to complete after being issued. If this time exceeds our
317 * monitoring window AND we didn't see any other completions in that
318 * window, then count that sync IO as a violation of the latency.
319 */
320 thislat = rwb_sync_issue_lat(rwb);
321 if (thislat > rwb->cur_win_nsec ||
322 (thislat > rwb->min_lat_nsec && !stat[READ].nr_samples)) {
323 trace_wbt_lat(bdi, thislat);
324 return LAT_EXCEEDED;
325 }
326
327 /*
328 * No read/write mix, if stat isn't valid
329 */
330 if (!stat_sample_valid(stat)) {
331 /*
332 * If we had writes in this stat window and the window is
333 * current, we're only doing writes. If a task recently
334 * waited or still has writes in flights, consider us doing
335 * just writes as well.
336 */
337 if (stat[WRITE].nr_samples || wb_recent_wait(rwb) ||
338 wbt_inflight(rwb))
339 return LAT_UNKNOWN_WRITES;
340 return LAT_UNKNOWN;
341 }
342
343 /*
344 * If the 'min' latency exceeds our target, step down.
345 */
346 if (stat[READ].min > rwb->min_lat_nsec) {
347 trace_wbt_lat(bdi, stat[READ].min);
348 trace_wbt_stat(bdi, stat);
349 return LAT_EXCEEDED;
350 }
351
352 if (rqd->scale_step)
353 trace_wbt_stat(bdi, stat);
354
355 return LAT_OK;
356 }
357
rwb_trace_step(struct rq_wb * rwb,const char * msg)358 static void rwb_trace_step(struct rq_wb *rwb, const char *msg)
359 {
360 struct backing_dev_info *bdi = rwb->rqos.disk->bdi;
361 struct rq_depth *rqd = &rwb->rq_depth;
362
363 trace_wbt_step(bdi, msg, rqd->scale_step, rwb->cur_win_nsec,
364 rwb->wb_background, rwb->wb_normal, rqd->max_depth);
365 }
366
calc_wb_limits(struct rq_wb * rwb)367 static void calc_wb_limits(struct rq_wb *rwb)
368 {
369 if (rwb->min_lat_nsec == 0) {
370 rwb->wb_normal = rwb->wb_background = 0;
371 } else if (rwb->rq_depth.max_depth <= 2) {
372 rwb->wb_normal = rwb->rq_depth.max_depth;
373 rwb->wb_background = 1;
374 } else {
375 rwb->wb_normal = (rwb->rq_depth.max_depth + 1) / 2;
376 rwb->wb_background = (rwb->rq_depth.max_depth + 3) / 4;
377 }
378 }
379
scale_up(struct rq_wb * rwb)380 static void scale_up(struct rq_wb *rwb)
381 {
382 if (!rq_depth_scale_up(&rwb->rq_depth))
383 return;
384 calc_wb_limits(rwb);
385 rwb->unknown_cnt = 0;
386 rwb_wake_all(rwb);
387 rwb_trace_step(rwb, tracepoint_string("scale up"));
388 }
389
scale_down(struct rq_wb * rwb,bool hard_throttle)390 static void scale_down(struct rq_wb *rwb, bool hard_throttle)
391 {
392 if (!rq_depth_scale_down(&rwb->rq_depth, hard_throttle))
393 return;
394 calc_wb_limits(rwb);
395 rwb->unknown_cnt = 0;
396 rwb_trace_step(rwb, tracepoint_string("scale down"));
397 }
398
rwb_arm_timer(struct rq_wb * rwb)399 static void rwb_arm_timer(struct rq_wb *rwb)
400 {
401 struct rq_depth *rqd = &rwb->rq_depth;
402
403 if (rqd->scale_step > 0) {
404 /*
405 * We should speed this up, using some variant of a fast
406 * integer inverse square root calculation. Since we only do
407 * this for every window expiration, it's not a huge deal,
408 * though.
409 */
410 rwb->cur_win_nsec = div_u64(rwb->win_nsec << 4,
411 int_sqrt((rqd->scale_step + 1) << 8));
412 } else {
413 /*
414 * For step < 0, we don't want to increase/decrease the
415 * window size.
416 */
417 rwb->cur_win_nsec = rwb->win_nsec;
418 }
419
420 blk_stat_activate_nsecs(rwb->cb, rwb->cur_win_nsec);
421 }
422
wb_timer_fn(struct blk_stat_callback * cb)423 static void wb_timer_fn(struct blk_stat_callback *cb)
424 {
425 struct rq_wb *rwb = cb->data;
426 struct rq_depth *rqd = &rwb->rq_depth;
427 unsigned int inflight = wbt_inflight(rwb);
428 int status;
429
430 if (!rwb->rqos.disk)
431 return;
432
433 status = latency_exceeded(rwb, cb->stat);
434
435 trace_wbt_timer(rwb->rqos.disk->bdi, status, rqd->scale_step, inflight);
436
437 /*
438 * If we exceeded the latency target, step down. If we did not,
439 * step one level up. If we don't know enough to say either exceeded
440 * or ok, then don't do anything.
441 */
442 switch (status) {
443 case LAT_EXCEEDED:
444 scale_down(rwb, true);
445 break;
446 case LAT_OK:
447 scale_up(rwb);
448 break;
449 case LAT_UNKNOWN_WRITES:
450 /*
451 * We don't have a valid read/write sample, but we do have
452 * writes going on. Allow step to go negative, to increase
453 * write performance.
454 */
455 scale_up(rwb);
456 break;
457 case LAT_UNKNOWN:
458 if (++rwb->unknown_cnt < RWB_UNKNOWN_BUMP)
459 break;
460 /*
461 * We get here when previously scaled reduced depth, and we
462 * currently don't have a valid read/write sample. For that
463 * case, slowly return to center state (step == 0).
464 */
465 if (rqd->scale_step > 0)
466 scale_up(rwb);
467 else if (rqd->scale_step < 0)
468 scale_down(rwb, false);
469 break;
470 default:
471 break;
472 }
473
474 /*
475 * Re-arm timer, if we have IO in flight
476 */
477 if (rqd->scale_step || inflight)
478 rwb_arm_timer(rwb);
479 }
480
wbt_update_limits(struct rq_wb * rwb)481 static void wbt_update_limits(struct rq_wb *rwb)
482 {
483 struct rq_depth *rqd = &rwb->rq_depth;
484
485 rqd->scale_step = 0;
486 rqd->scaled_max = false;
487
488 rq_depth_calc_max_depth(rqd);
489 calc_wb_limits(rwb);
490
491 rwb_wake_all(rwb);
492 }
493
wbt_disabled(struct request_queue * q)494 bool wbt_disabled(struct request_queue *q)
495 {
496 struct rq_qos *rqos = wbt_rq_qos(q);
497
498 return !rqos || !rwb_enabled(RQWB(rqos));
499 }
500
wbt_get_min_lat(struct request_queue * q)501 u64 wbt_get_min_lat(struct request_queue *q)
502 {
503 struct rq_qos *rqos = wbt_rq_qos(q);
504 if (!rqos)
505 return 0;
506 return RQWB(rqos)->min_lat_nsec;
507 }
508
wbt_set_min_lat(struct request_queue * q,u64 val)509 void wbt_set_min_lat(struct request_queue *q, u64 val)
510 {
511 struct rq_qos *rqos = wbt_rq_qos(q);
512 if (!rqos)
513 return;
514
515 RQWB(rqos)->min_lat_nsec = val;
516 if (val)
517 RQWB(rqos)->enable_state = WBT_STATE_ON_MANUAL;
518 else
519 RQWB(rqos)->enable_state = WBT_STATE_OFF_MANUAL;
520
521 wbt_update_limits(RQWB(rqos));
522 }
523
524
close_io(struct rq_wb * rwb)525 static bool close_io(struct rq_wb *rwb)
526 {
527 const unsigned long now = jiffies;
528
529 return time_before(now, rwb->last_issue + HZ / 10) ||
530 time_before(now, rwb->last_comp + HZ / 10);
531 }
532
533 #define REQ_HIPRIO (REQ_SYNC | REQ_META | REQ_PRIO | REQ_SWAP)
534
get_limit(struct rq_wb * rwb,blk_opf_t opf)535 static inline unsigned int get_limit(struct rq_wb *rwb, blk_opf_t opf)
536 {
537 unsigned int limit;
538
539 if ((opf & REQ_OP_MASK) == REQ_OP_DISCARD)
540 return rwb->wb_background;
541
542 /*
543 * At this point we know it's a buffered write. If this is
544 * swap trying to free memory, or REQ_SYNC is set, then
545 * it's WB_SYNC_ALL writeback, and we'll use the max limit for
546 * that. If the write is marked as a background write, then use
547 * the idle limit, or go to normal if we haven't had competing
548 * IO for a bit.
549 */
550 if ((opf & REQ_HIPRIO) || wb_recent_wait(rwb))
551 limit = rwb->rq_depth.max_depth;
552 else if ((opf & REQ_BACKGROUND) || close_io(rwb)) {
553 /*
554 * If less than 100ms since we completed unrelated IO,
555 * limit us to half the depth for background writeback.
556 */
557 limit = rwb->wb_background;
558 } else
559 limit = rwb->wb_normal;
560
561 return limit;
562 }
563
564 struct wbt_wait_data {
565 struct rq_wb *rwb;
566 enum wbt_flags wb_acct;
567 blk_opf_t opf;
568 };
569
wbt_inflight_cb(struct rq_wait * rqw,void * private_data)570 static bool wbt_inflight_cb(struct rq_wait *rqw, void *private_data)
571 {
572 struct wbt_wait_data *data = private_data;
573 return rq_wait_inc_below(rqw, get_limit(data->rwb, data->opf));
574 }
575
wbt_cleanup_cb(struct rq_wait * rqw,void * private_data)576 static void wbt_cleanup_cb(struct rq_wait *rqw, void *private_data)
577 {
578 struct wbt_wait_data *data = private_data;
579 wbt_rqw_done(data->rwb, rqw, data->wb_acct);
580 }
581
582 /*
583 * Block if we will exceed our limit, or if we are currently waiting for
584 * the timer to kick off queuing again.
585 */
__wbt_wait(struct rq_wb * rwb,enum wbt_flags wb_acct,blk_opf_t opf)586 static void __wbt_wait(struct rq_wb *rwb, enum wbt_flags wb_acct,
587 blk_opf_t opf)
588 {
589 struct rq_wait *rqw = get_rq_wait(rwb, wb_acct);
590 struct wbt_wait_data data = {
591 .rwb = rwb,
592 .wb_acct = wb_acct,
593 .opf = opf,
594 };
595
596 rq_qos_wait(rqw, &data, wbt_inflight_cb, wbt_cleanup_cb);
597 }
598
wbt_should_throttle(struct bio * bio)599 static inline bool wbt_should_throttle(struct bio *bio)
600 {
601 switch (bio_op(bio)) {
602 case REQ_OP_WRITE:
603 /*
604 * Don't throttle WRITE_ODIRECT
605 */
606 if ((bio->bi_opf & (REQ_SYNC | REQ_IDLE)) ==
607 (REQ_SYNC | REQ_IDLE))
608 return false;
609 fallthrough;
610 case REQ_OP_DISCARD:
611 return true;
612 default:
613 return false;
614 }
615 }
616
bio_to_wbt_flags(struct rq_wb * rwb,struct bio * bio)617 static enum wbt_flags bio_to_wbt_flags(struct rq_wb *rwb, struct bio *bio)
618 {
619 enum wbt_flags flags = 0;
620
621 if (!rwb_enabled(rwb))
622 return 0;
623
624 if (bio_op(bio) == REQ_OP_READ) {
625 flags = WBT_READ;
626 } else if (wbt_should_throttle(bio)) {
627 if (bio->bi_opf & REQ_SWAP)
628 flags |= WBT_SWAP;
629 if (bio_op(bio) == REQ_OP_DISCARD)
630 flags |= WBT_DISCARD;
631 flags |= WBT_TRACKED;
632 }
633 return flags;
634 }
635
wbt_cleanup(struct rq_qos * rqos,struct bio * bio)636 static void wbt_cleanup(struct rq_qos *rqos, struct bio *bio)
637 {
638 struct rq_wb *rwb = RQWB(rqos);
639 enum wbt_flags flags = bio_to_wbt_flags(rwb, bio);
640 __wbt_done(rqos, flags);
641 }
642
643 /* May sleep, if we have exceeded the writeback limits. */
wbt_wait(struct rq_qos * rqos,struct bio * bio)644 static void wbt_wait(struct rq_qos *rqos, struct bio *bio)
645 {
646 struct rq_wb *rwb = RQWB(rqos);
647 enum wbt_flags flags;
648
649 flags = bio_to_wbt_flags(rwb, bio);
650 if (!(flags & WBT_TRACKED)) {
651 if (flags & WBT_READ)
652 wb_timestamp(rwb, &rwb->last_issue);
653 return;
654 }
655
656 __wbt_wait(rwb, flags, bio->bi_opf);
657
658 if (!blk_stat_is_active(rwb->cb))
659 rwb_arm_timer(rwb);
660 }
661
wbt_track(struct rq_qos * rqos,struct request * rq,struct bio * bio)662 static void wbt_track(struct rq_qos *rqos, struct request *rq, struct bio *bio)
663 {
664 struct rq_wb *rwb = RQWB(rqos);
665 rq->wbt_flags |= bio_to_wbt_flags(rwb, bio);
666 }
667
wbt_issue(struct rq_qos * rqos,struct request * rq)668 static void wbt_issue(struct rq_qos *rqos, struct request *rq)
669 {
670 struct rq_wb *rwb = RQWB(rqos);
671
672 if (!rwb_enabled(rwb))
673 return;
674
675 /*
676 * Track sync issue, in case it takes a long time to complete. Allows us
677 * to react quicker, if a sync IO takes a long time to complete. Note
678 * that this is just a hint. The request can go away when it completes,
679 * so it's important we never dereference it. We only use the address to
680 * compare with, which is why we store the sync_issue time locally.
681 */
682 if (wbt_is_read(rq) && !rwb->sync_issue) {
683 rwb->sync_cookie = rq;
684 rwb->sync_issue = rq->io_start_time_ns;
685 }
686 }
687
wbt_requeue(struct rq_qos * rqos,struct request * rq)688 static void wbt_requeue(struct rq_qos *rqos, struct request *rq)
689 {
690 struct rq_wb *rwb = RQWB(rqos);
691 if (!rwb_enabled(rwb))
692 return;
693 if (rq == rwb->sync_cookie) {
694 rwb->sync_issue = 0;
695 rwb->sync_cookie = NULL;
696 }
697 }
698
699 /*
700 * Enable wbt if defaults are configured that way
701 */
wbt_enable_default(struct gendisk * disk)702 void wbt_enable_default(struct gendisk *disk)
703 {
704 struct request_queue *q = disk->queue;
705 struct rq_qos *rqos;
706 bool enable = IS_ENABLED(CONFIG_BLK_WBT_MQ);
707
708 mutex_lock(&disk->rqos_state_mutex);
709
710 if (blk_queue_disable_wbt(q))
711 enable = false;
712
713 /* Throttling already enabled? */
714 rqos = wbt_rq_qos(q);
715 if (rqos) {
716 if (enable && RQWB(rqos)->enable_state == WBT_STATE_OFF_DEFAULT)
717 RQWB(rqos)->enable_state = WBT_STATE_ON_DEFAULT;
718 mutex_unlock(&disk->rqos_state_mutex);
719 return;
720 }
721 mutex_unlock(&disk->rqos_state_mutex);
722
723 /* Queue not registered? Maybe shutting down... */
724 if (!blk_queue_registered(q))
725 return;
726
727 if (queue_is_mq(q) && enable)
728 wbt_init(disk);
729 }
730 EXPORT_SYMBOL_GPL(wbt_enable_default);
731
wbt_default_latency_nsec(struct request_queue * q)732 u64 wbt_default_latency_nsec(struct request_queue *q)
733 {
734 /*
735 * We default to 2msec for non-rotational storage, and 75msec
736 * for rotational storage.
737 */
738 if (blk_queue_nonrot(q))
739 return 2000000ULL;
740 else
741 return 75000000ULL;
742 }
743
wbt_data_dir(const struct request * rq)744 static int wbt_data_dir(const struct request *rq)
745 {
746 const enum req_op op = req_op(rq);
747
748 if (op == REQ_OP_READ)
749 return READ;
750 else if (op_is_write(op))
751 return WRITE;
752
753 /* don't account */
754 return -1;
755 }
756
wbt_queue_depth_changed(struct rq_qos * rqos)757 static void wbt_queue_depth_changed(struct rq_qos *rqos)
758 {
759 RQWB(rqos)->rq_depth.queue_depth = blk_queue_depth(rqos->disk->queue);
760 wbt_update_limits(RQWB(rqos));
761 }
762
wbt_exit(struct rq_qos * rqos)763 static void wbt_exit(struct rq_qos *rqos)
764 {
765 struct rq_wb *rwb = RQWB(rqos);
766
767 blk_stat_remove_callback(rqos->disk->queue, rwb->cb);
768 blk_stat_free_callback(rwb->cb);
769 kfree(rwb);
770 }
771
772 /*
773 * Disable wbt, if enabled by default.
774 */
wbt_disable_default(struct gendisk * disk)775 void wbt_disable_default(struct gendisk *disk)
776 {
777 struct rq_qos *rqos = wbt_rq_qos(disk->queue);
778 struct rq_wb *rwb;
779 if (!rqos)
780 return;
781 mutex_lock(&disk->rqos_state_mutex);
782 rwb = RQWB(rqos);
783 if (rwb->enable_state == WBT_STATE_ON_DEFAULT) {
784 blk_stat_deactivate(rwb->cb);
785 rwb->enable_state = WBT_STATE_OFF_DEFAULT;
786 }
787 mutex_unlock(&disk->rqos_state_mutex);
788 }
789 EXPORT_SYMBOL_GPL(wbt_disable_default);
790
791 #ifdef CONFIG_BLK_DEBUG_FS
wbt_curr_win_nsec_show(void * data,struct seq_file * m)792 static int wbt_curr_win_nsec_show(void *data, struct seq_file *m)
793 {
794 struct rq_qos *rqos = data;
795 struct rq_wb *rwb = RQWB(rqos);
796
797 seq_printf(m, "%llu\n", rwb->cur_win_nsec);
798 return 0;
799 }
800
wbt_enabled_show(void * data,struct seq_file * m)801 static int wbt_enabled_show(void *data, struct seq_file *m)
802 {
803 struct rq_qos *rqos = data;
804 struct rq_wb *rwb = RQWB(rqos);
805
806 seq_printf(m, "%d\n", rwb->enable_state);
807 return 0;
808 }
809
wbt_id_show(void * data,struct seq_file * m)810 static int wbt_id_show(void *data, struct seq_file *m)
811 {
812 struct rq_qos *rqos = data;
813
814 seq_printf(m, "%u\n", rqos->id);
815 return 0;
816 }
817
wbt_inflight_show(void * data,struct seq_file * m)818 static int wbt_inflight_show(void *data, struct seq_file *m)
819 {
820 struct rq_qos *rqos = data;
821 struct rq_wb *rwb = RQWB(rqos);
822 int i;
823
824 for (i = 0; i < WBT_NUM_RWQ; i++)
825 seq_printf(m, "%d: inflight %d\n", i,
826 atomic_read(&rwb->rq_wait[i].inflight));
827 return 0;
828 }
829
wbt_min_lat_nsec_show(void * data,struct seq_file * m)830 static int wbt_min_lat_nsec_show(void *data, struct seq_file *m)
831 {
832 struct rq_qos *rqos = data;
833 struct rq_wb *rwb = RQWB(rqos);
834
835 seq_printf(m, "%lu\n", rwb->min_lat_nsec);
836 return 0;
837 }
838
wbt_unknown_cnt_show(void * data,struct seq_file * m)839 static int wbt_unknown_cnt_show(void *data, struct seq_file *m)
840 {
841 struct rq_qos *rqos = data;
842 struct rq_wb *rwb = RQWB(rqos);
843
844 seq_printf(m, "%u\n", rwb->unknown_cnt);
845 return 0;
846 }
847
wbt_normal_show(void * data,struct seq_file * m)848 static int wbt_normal_show(void *data, struct seq_file *m)
849 {
850 struct rq_qos *rqos = data;
851 struct rq_wb *rwb = RQWB(rqos);
852
853 seq_printf(m, "%u\n", rwb->wb_normal);
854 return 0;
855 }
856
wbt_background_show(void * data,struct seq_file * m)857 static int wbt_background_show(void *data, struct seq_file *m)
858 {
859 struct rq_qos *rqos = data;
860 struct rq_wb *rwb = RQWB(rqos);
861
862 seq_printf(m, "%u\n", rwb->wb_background);
863 return 0;
864 }
865
866 static const struct blk_mq_debugfs_attr wbt_debugfs_attrs[] = {
867 {"curr_win_nsec", 0400, wbt_curr_win_nsec_show},
868 {"enabled", 0400, wbt_enabled_show},
869 {"id", 0400, wbt_id_show},
870 {"inflight", 0400, wbt_inflight_show},
871 {"min_lat_nsec", 0400, wbt_min_lat_nsec_show},
872 {"unknown_cnt", 0400, wbt_unknown_cnt_show},
873 {"wb_normal", 0400, wbt_normal_show},
874 {"wb_background", 0400, wbt_background_show},
875 {},
876 };
877 #endif
878
879 static const struct rq_qos_ops wbt_rqos_ops = {
880 .throttle = wbt_wait,
881 .issue = wbt_issue,
882 .track = wbt_track,
883 .requeue = wbt_requeue,
884 .done = wbt_done,
885 .cleanup = wbt_cleanup,
886 .queue_depth_changed = wbt_queue_depth_changed,
887 .exit = wbt_exit,
888 #ifdef CONFIG_BLK_DEBUG_FS
889 .debugfs_attrs = wbt_debugfs_attrs,
890 #endif
891 };
892
wbt_init(struct gendisk * disk)893 int wbt_init(struct gendisk *disk)
894 {
895 struct request_queue *q = disk->queue;
896 struct rq_wb *rwb;
897 int i;
898 int ret;
899
900 rwb = kzalloc(sizeof(*rwb), GFP_KERNEL);
901 if (!rwb)
902 return -ENOMEM;
903
904 rwb->cb = blk_stat_alloc_callback(wb_timer_fn, wbt_data_dir, 2, rwb);
905 if (!rwb->cb) {
906 kfree(rwb);
907 return -ENOMEM;
908 }
909
910 for (i = 0; i < WBT_NUM_RWQ; i++)
911 rq_wait_init(&rwb->rq_wait[i]);
912
913 rwb->last_comp = rwb->last_issue = jiffies;
914 rwb->win_nsec = RWB_WINDOW_NSEC;
915 rwb->enable_state = WBT_STATE_ON_DEFAULT;
916 rwb->rq_depth.default_depth = RWB_DEF_DEPTH;
917 rwb->min_lat_nsec = wbt_default_latency_nsec(q);
918 rwb->rq_depth.queue_depth = blk_queue_depth(q);
919 wbt_update_limits(rwb);
920
921 /*
922 * Assign rwb and add the stats callback.
923 */
924 mutex_lock(&q->rq_qos_mutex);
925 ret = rq_qos_add(&rwb->rqos, disk, RQ_QOS_WBT, &wbt_rqos_ops);
926 mutex_unlock(&q->rq_qos_mutex);
927 if (ret)
928 goto err_free;
929
930 blk_stat_add_callback(q, rwb->cb);
931
932 return 0;
933
934 err_free:
935 blk_stat_free_callback(rwb->cb);
936 kfree(rwb);
937 return ret;
938
939 }
940