blob: 4f89b28fa6524d117b5ad6a53a605ebda8e57b93 [file] [log] [blame]
Jens Axboee34cbd32016-11-09 12:36:15 -07001/*
2 * buffered writeback throttling. loosely based on CoDel. We can't drop
3 * packets for IO scheduling, so the logic is something like this:
4 *
5 * - Monitor latencies in a defined window of time.
6 * - If the minimum latency in the above window exceeds some target, increment
7 * scaling step and scale down queue depth by a factor of 2x. The monitoring
8 * window is then shrunk to 100 / sqrt(scaling step + 1).
9 * - For any window where we don't have solid data on what the latencies
10 * look like, retain status quo.
11 * - If latencies look good, decrement scaling step.
12 * - If we're only doing writes, allow the scaling step to go negative. This
13 * will temporarily boost write performance, snapping back to a stable
14 * scaling step of 0 if reads show up or the heavy writers finish. Unlike
15 * positive scaling steps where we shrink the monitoring window, a negative
16 * scaling step retains the default step==0 window size.
17 *
18 * Copyright (C) 2016 Jens Axboe
19 *
20 */
21#include <linux/kernel.h>
22#include <linux/blk_types.h>
23#include <linux/slab.h>
24#include <linux/backing-dev.h>
25#include <linux/swap.h>
26
27#include "blk-wbt.h"
28
29#define CREATE_TRACE_POINTS
30#include <trace/events/wbt.h>
31
Omar Sandovala8a45942018-05-09 02:08:48 -070032static inline void wbt_clear_state(struct request *rq)
Omar Sandoval934031a2018-05-09 02:08:47 -070033{
Omar Sandoval544ccc8d2018-05-09 02:08:50 -070034 rq->wbt_flags = 0;
Omar Sandoval934031a2018-05-09 02:08:47 -070035}
36
Omar Sandovala8a45942018-05-09 02:08:48 -070037static inline enum wbt_flags wbt_flags(struct request *rq)
Omar Sandoval934031a2018-05-09 02:08:47 -070038{
Omar Sandoval544ccc8d2018-05-09 02:08:50 -070039 return rq->wbt_flags;
Omar Sandoval934031a2018-05-09 02:08:47 -070040}
41
Omar Sandovala8a45942018-05-09 02:08:48 -070042static inline bool wbt_is_tracked(struct request *rq)
Omar Sandoval934031a2018-05-09 02:08:47 -070043{
Omar Sandoval544ccc8d2018-05-09 02:08:50 -070044 return rq->wbt_flags & WBT_TRACKED;
Omar Sandoval934031a2018-05-09 02:08:47 -070045}
46
Omar Sandovala8a45942018-05-09 02:08:48 -070047static inline bool wbt_is_read(struct request *rq)
Omar Sandoval934031a2018-05-09 02:08:47 -070048{
Omar Sandoval544ccc8d2018-05-09 02:08:50 -070049 return rq->wbt_flags & WBT_READ;
Omar Sandoval934031a2018-05-09 02:08:47 -070050}
51
Jens Axboee34cbd32016-11-09 12:36:15 -070052enum {
53 /*
54 * Default setting, we'll scale up (to 75% of QD max) or down (min 1)
55 * from here depending on device stats
56 */
57 RWB_DEF_DEPTH = 16,
58
59 /*
60 * 100msec window
61 */
62 RWB_WINDOW_NSEC = 100 * 1000 * 1000ULL,
63
64 /*
65 * Disregard stats, if we don't meet this minimum
66 */
67 RWB_MIN_WRITE_SAMPLES = 3,
68
69 /*
70 * If we have this number of consecutive windows with not enough
71 * information to scale up or down, scale up.
72 */
73 RWB_UNKNOWN_BUMP = 5,
74};
75
76static inline bool rwb_enabled(struct rq_wb *rwb)
77{
78 return rwb && rwb->wb_normal != 0;
79}
80
81/*
82 * Increment 'v', if 'v' is below 'below'. Returns true if we succeeded,
83 * false if 'v' + 1 would be bigger than 'below'.
84 */
85static bool atomic_inc_below(atomic_t *v, int below)
86{
87 int cur = atomic_read(v);
88
89 for (;;) {
90 int old;
91
92 if (cur >= below)
93 return false;
94 old = atomic_cmpxchg(v, cur, cur + 1);
95 if (old == cur)
96 break;
97 cur = old;
98 }
99
100 return true;
101}
102
103static void wb_timestamp(struct rq_wb *rwb, unsigned long *var)
104{
105 if (rwb_enabled(rwb)) {
106 const unsigned long cur = jiffies;
107
108 if (cur != *var)
109 *var = cur;
110 }
111}
112
113/*
114 * If a task was rate throttled in balance_dirty_pages() within the last
115 * second or so, use that to indicate a higher cleaning rate.
116 */
117static bool wb_recent_wait(struct rq_wb *rwb)
118{
Jan Karadc3b17c2017-02-02 15:56:50 +0100119 struct bdi_writeback *wb = &rwb->queue->backing_dev_info->wb;
Jens Axboee34cbd32016-11-09 12:36:15 -0700120
121 return time_before(jiffies, wb->dirty_sleep + HZ);
122}
123
Jens Axboe8bea6092018-05-07 09:57:08 -0600124static inline struct rq_wait *get_rq_wait(struct rq_wb *rwb,
125 enum wbt_flags wb_acct)
Jens Axboee34cbd32016-11-09 12:36:15 -0700126{
Jens Axboe8bea6092018-05-07 09:57:08 -0600127 if (wb_acct & WBT_KSWAPD)
128 return &rwb->rq_wait[WBT_RWQ_KSWAPD];
Jens Axboe782f5692018-05-07 10:03:23 -0600129 else if (wb_acct & WBT_DISCARD)
130 return &rwb->rq_wait[WBT_RWQ_DISCARD];
Jens Axboe8bea6092018-05-07 09:57:08 -0600131
132 return &rwb->rq_wait[WBT_RWQ_BG];
Jens Axboee34cbd32016-11-09 12:36:15 -0700133}
134
135static void rwb_wake_all(struct rq_wb *rwb)
136{
137 int i;
138
139 for (i = 0; i < WBT_NUM_RWQ; i++) {
140 struct rq_wait *rqw = &rwb->rq_wait[i];
141
142 if (waitqueue_active(&rqw->wait))
143 wake_up_all(&rqw->wait);
144 }
145}
146
147void __wbt_done(struct rq_wb *rwb, enum wbt_flags wb_acct)
148{
149 struct rq_wait *rqw;
150 int inflight, limit;
151
152 if (!(wb_acct & WBT_TRACKED))
153 return;
154
Jens Axboe8bea6092018-05-07 09:57:08 -0600155 rqw = get_rq_wait(rwb, wb_acct);
Jens Axboee34cbd32016-11-09 12:36:15 -0700156 inflight = atomic_dec_return(&rqw->inflight);
157
158 /*
159 * wbt got disabled with IO in flight. Wake up any potential
160 * waiters, we don't have to do more than that.
161 */
162 if (unlikely(!rwb_enabled(rwb))) {
163 rwb_wake_all(rwb);
164 return;
165 }
166
167 /*
Jens Axboe782f5692018-05-07 10:03:23 -0600168 * For discards, our limit is always the background. For writes, if
169 * the device does write back caching, drop further down before we
170 * wake people up.
Jens Axboee34cbd32016-11-09 12:36:15 -0700171 */
Jens Axboe782f5692018-05-07 10:03:23 -0600172 if (wb_acct & WBT_DISCARD)
173 limit = rwb->wb_background;
174 else if (rwb->wc && !wb_recent_wait(rwb))
Jens Axboee34cbd32016-11-09 12:36:15 -0700175 limit = 0;
176 else
177 limit = rwb->wb_normal;
178
179 /*
180 * Don't wake anyone up if we are above the normal limit.
181 */
182 if (inflight && inflight >= limit)
183 return;
184
185 if (waitqueue_active(&rqw->wait)) {
186 int diff = limit - inflight;
187
188 if (!inflight || diff >= rwb->wb_background / 2)
189 wake_up_all(&rqw->wait);
190 }
191}
192
193/*
194 * Called on completion of a request. Note that it's also called when
195 * a request is merged, when the request gets freed.
196 */
Omar Sandovala8a45942018-05-09 02:08:48 -0700197void wbt_done(struct rq_wb *rwb, struct request *rq)
Jens Axboee34cbd32016-11-09 12:36:15 -0700198{
199 if (!rwb)
200 return;
201
Omar Sandovala8a45942018-05-09 02:08:48 -0700202 if (!wbt_is_tracked(rq)) {
203 if (rwb->sync_cookie == rq) {
Jens Axboee34cbd32016-11-09 12:36:15 -0700204 rwb->sync_issue = 0;
205 rwb->sync_cookie = NULL;
206 }
207
Omar Sandovala8a45942018-05-09 02:08:48 -0700208 if (wbt_is_read(rq))
Jens Axboee34cbd32016-11-09 12:36:15 -0700209 wb_timestamp(rwb, &rwb->last_comp);
Jens Axboee34cbd32016-11-09 12:36:15 -0700210 } else {
Omar Sandovala8a45942018-05-09 02:08:48 -0700211 WARN_ON_ONCE(rq == rwb->sync_cookie);
212 __wbt_done(rwb, wbt_flags(rq));
Jens Axboee34cbd32016-11-09 12:36:15 -0700213 }
Omar Sandovala8a45942018-05-09 02:08:48 -0700214 wbt_clear_state(rq);
Jens Axboee34cbd32016-11-09 12:36:15 -0700215}
216
217/*
218 * Return true, if we can't increase the depth further by scaling
219 */
220static bool calc_wb_limits(struct rq_wb *rwb)
221{
222 unsigned int depth;
223 bool ret = false;
224
225 if (!rwb->min_lat_nsec) {
226 rwb->wb_max = rwb->wb_normal = rwb->wb_background = 0;
227 return false;
228 }
229
230 /*
231 * For QD=1 devices, this is a special case. It's important for those
232 * to have one request ready when one completes, so force a depth of
233 * 2 for those devices. On the backend, it'll be a depth of 1 anyway,
234 * since the device can't have more than that in flight. If we're
235 * scaling down, then keep a setting of 1/1/1.
236 */
237 if (rwb->queue_depth == 1) {
238 if (rwb->scale_step > 0)
239 rwb->wb_max = rwb->wb_normal = 1;
240 else {
241 rwb->wb_max = rwb->wb_normal = 2;
242 ret = true;
243 }
244 rwb->wb_background = 1;
245 } else {
246 /*
247 * scale_step == 0 is our default state. If we have suffered
248 * latency spikes, step will be > 0, and we shrink the
249 * allowed write depths. If step is < 0, we're only doing
250 * writes, and we allow a temporarily higher depth to
251 * increase performance.
252 */
253 depth = min_t(unsigned int, RWB_DEF_DEPTH, rwb->queue_depth);
254 if (rwb->scale_step > 0)
255 depth = 1 + ((depth - 1) >> min(31, rwb->scale_step));
256 else if (rwb->scale_step < 0) {
257 unsigned int maxd = 3 * rwb->queue_depth / 4;
258
259 depth = 1 + ((depth - 1) << -rwb->scale_step);
260 if (depth > maxd) {
261 depth = maxd;
262 ret = true;
263 }
264 }
265
266 /*
267 * Set our max/normal/bg queue depths based on how far
268 * we have scaled down (->scale_step).
269 */
270 rwb->wb_max = depth;
271 rwb->wb_normal = (rwb->wb_max + 1) / 2;
272 rwb->wb_background = (rwb->wb_max + 3) / 4;
273 }
274
275 return ret;
276}
277
Arnd Bergmann4121d382016-11-16 16:29:57 +0100278static inline bool stat_sample_valid(struct blk_rq_stat *stat)
Jens Axboee34cbd32016-11-09 12:36:15 -0700279{
280 /*
281 * We need at least one read sample, and a minimum of
282 * RWB_MIN_WRITE_SAMPLES. We require some write samples to know
283 * that it's writes impacting us, and not just some sole read on
284 * a device that is in a lower power state.
285 */
Omar Sandovalfa2e39c2017-03-21 08:56:06 -0700286 return (stat[READ].nr_samples >= 1 &&
287 stat[WRITE].nr_samples >= RWB_MIN_WRITE_SAMPLES);
Jens Axboee34cbd32016-11-09 12:36:15 -0700288}
289
290static u64 rwb_sync_issue_lat(struct rq_wb *rwb)
291{
Mark Rutland6aa7de02017-10-23 14:07:29 -0700292 u64 now, issue = READ_ONCE(rwb->sync_issue);
Jens Axboee34cbd32016-11-09 12:36:15 -0700293
294 if (!issue || !rwb->sync_cookie)
295 return 0;
296
297 now = ktime_to_ns(ktime_get());
298 return now - issue;
299}
300
301enum {
302 LAT_OK = 1,
303 LAT_UNKNOWN,
304 LAT_UNKNOWN_WRITES,
305 LAT_EXCEEDED,
306};
307
Omar Sandoval34dbad52017-03-21 08:56:08 -0700308static int latency_exceeded(struct rq_wb *rwb, struct blk_rq_stat *stat)
Jens Axboee34cbd32016-11-09 12:36:15 -0700309{
Jan Karadc3b17c2017-02-02 15:56:50 +0100310 struct backing_dev_info *bdi = rwb->queue->backing_dev_info;
Jens Axboee34cbd32016-11-09 12:36:15 -0700311 u64 thislat;
312
313 /*
314 * If our stored sync issue exceeds the window size, or it
315 * exceeds our min target AND we haven't logged any entries,
316 * flag the latency as exceeded. wbt works off completion latencies,
317 * but for a flooded device, a single sync IO can take a long time
318 * to complete after being issued. If this time exceeds our
319 * monitoring window AND we didn't see any other completions in that
320 * window, then count that sync IO as a violation of the latency.
321 */
322 thislat = rwb_sync_issue_lat(rwb);
323 if (thislat > rwb->cur_win_nsec ||
Omar Sandovalfa2e39c2017-03-21 08:56:06 -0700324 (thislat > rwb->min_lat_nsec && !stat[READ].nr_samples)) {
Jens Axboed8a0cbf2016-11-10 21:52:53 -0700325 trace_wbt_lat(bdi, thislat);
Jens Axboee34cbd32016-11-09 12:36:15 -0700326 return LAT_EXCEEDED;
327 }
328
329 /*
330 * No read/write mix, if stat isn't valid
331 */
332 if (!stat_sample_valid(stat)) {
333 /*
334 * If we had writes in this stat window and the window is
335 * current, we're only doing writes. If a task recently
336 * waited or still has writes in flights, consider us doing
337 * just writes as well.
338 */
Omar Sandoval34dbad52017-03-21 08:56:08 -0700339 if (stat[WRITE].nr_samples || wb_recent_wait(rwb) ||
340 wbt_inflight(rwb))
Jens Axboee34cbd32016-11-09 12:36:15 -0700341 return LAT_UNKNOWN_WRITES;
342 return LAT_UNKNOWN;
343 }
344
345 /*
346 * If the 'min' latency exceeds our target, step down.
347 */
Omar Sandovalfa2e39c2017-03-21 08:56:06 -0700348 if (stat[READ].min > rwb->min_lat_nsec) {
349 trace_wbt_lat(bdi, stat[READ].min);
Jens Axboed8a0cbf2016-11-10 21:52:53 -0700350 trace_wbt_stat(bdi, stat);
Jens Axboee34cbd32016-11-09 12:36:15 -0700351 return LAT_EXCEEDED;
352 }
353
354 if (rwb->scale_step)
Jens Axboed8a0cbf2016-11-10 21:52:53 -0700355 trace_wbt_stat(bdi, stat);
Jens Axboee34cbd32016-11-09 12:36:15 -0700356
357 return LAT_OK;
358}
359
Jens Axboee34cbd32016-11-09 12:36:15 -0700360static void rwb_trace_step(struct rq_wb *rwb, const char *msg)
361{
Jan Karadc3b17c2017-02-02 15:56:50 +0100362 struct backing_dev_info *bdi = rwb->queue->backing_dev_info;
Jens Axboed8a0cbf2016-11-10 21:52:53 -0700363
364 trace_wbt_step(bdi, msg, rwb->scale_step, rwb->cur_win_nsec,
Jens Axboee34cbd32016-11-09 12:36:15 -0700365 rwb->wb_background, rwb->wb_normal, rwb->wb_max);
366}
367
368static void scale_up(struct rq_wb *rwb)
369{
370 /*
371 * Hit max in previous round, stop here
372 */
373 if (rwb->scaled_max)
374 return;
375
376 rwb->scale_step--;
377 rwb->unknown_cnt = 0;
Jens Axboee34cbd32016-11-09 12:36:15 -0700378
379 rwb->scaled_max = calc_wb_limits(rwb);
380
381 rwb_wake_all(rwb);
382
383 rwb_trace_step(rwb, "step up");
384}
385
386/*
387 * Scale rwb down. If 'hard_throttle' is set, do it quicker, since we
388 * had a latency violation.
389 */
390static void scale_down(struct rq_wb *rwb, bool hard_throttle)
391{
392 /*
393 * Stop scaling down when we've hit the limit. This also prevents
394 * ->scale_step from going to crazy values, if the device can't
395 * keep up.
396 */
397 if (rwb->wb_max == 1)
398 return;
399
400 if (rwb->scale_step < 0 && hard_throttle)
401 rwb->scale_step = 0;
402 else
403 rwb->scale_step++;
404
405 rwb->scaled_max = false;
406 rwb->unknown_cnt = 0;
Jens Axboee34cbd32016-11-09 12:36:15 -0700407 calc_wb_limits(rwb);
408 rwb_trace_step(rwb, "step down");
409}
410
411static void rwb_arm_timer(struct rq_wb *rwb)
412{
Jens Axboee34cbd32016-11-09 12:36:15 -0700413 if (rwb->scale_step > 0) {
414 /*
415 * We should speed this up, using some variant of a fast
416 * integer inverse square root calculation. Since we only do
417 * this for every window expiration, it's not a huge deal,
418 * though.
419 */
420 rwb->cur_win_nsec = div_u64(rwb->win_nsec << 4,
421 int_sqrt((rwb->scale_step + 1) << 8));
422 } else {
423 /*
424 * For step < 0, we don't want to increase/decrease the
425 * window size.
426 */
427 rwb->cur_win_nsec = rwb->win_nsec;
428 }
429
Omar Sandoval34dbad52017-03-21 08:56:08 -0700430 blk_stat_activate_nsecs(rwb->cb, rwb->cur_win_nsec);
Jens Axboee34cbd32016-11-09 12:36:15 -0700431}
432
Omar Sandoval34dbad52017-03-21 08:56:08 -0700433static void wb_timer_fn(struct blk_stat_callback *cb)
Jens Axboee34cbd32016-11-09 12:36:15 -0700434{
Omar Sandoval34dbad52017-03-21 08:56:08 -0700435 struct rq_wb *rwb = cb->data;
Jens Axboee34cbd32016-11-09 12:36:15 -0700436 unsigned int inflight = wbt_inflight(rwb);
437 int status;
438
Omar Sandoval34dbad52017-03-21 08:56:08 -0700439 status = latency_exceeded(rwb, cb->stat);
Jens Axboee34cbd32016-11-09 12:36:15 -0700440
Jan Karadc3b17c2017-02-02 15:56:50 +0100441 trace_wbt_timer(rwb->queue->backing_dev_info, status, rwb->scale_step,
Jens Axboed8a0cbf2016-11-10 21:52:53 -0700442 inflight);
Jens Axboee34cbd32016-11-09 12:36:15 -0700443
444 /*
445 * If we exceeded the latency target, step down. If we did not,
446 * step one level up. If we don't know enough to say either exceeded
447 * or ok, then don't do anything.
448 */
449 switch (status) {
450 case LAT_EXCEEDED:
451 scale_down(rwb, true);
452 break;
453 case LAT_OK:
454 scale_up(rwb);
455 break;
456 case LAT_UNKNOWN_WRITES:
457 /*
458 * We started a the center step, but don't have a valid
459 * read/write sample, but we do have writes going on.
460 * Allow step to go negative, to increase write perf.
461 */
462 scale_up(rwb);
463 break;
464 case LAT_UNKNOWN:
465 if (++rwb->unknown_cnt < RWB_UNKNOWN_BUMP)
466 break;
467 /*
468 * We get here when previously scaled reduced depth, and we
469 * currently don't have a valid read/write sample. For that
470 * case, slowly return to center state (step == 0).
471 */
472 if (rwb->scale_step > 0)
473 scale_up(rwb);
474 else if (rwb->scale_step < 0)
475 scale_down(rwb, false);
476 break;
477 default:
478 break;
479 }
480
481 /*
482 * Re-arm timer, if we have IO in flight
483 */
484 if (rwb->scale_step || inflight)
485 rwb_arm_timer(rwb);
486}
487
488void wbt_update_limits(struct rq_wb *rwb)
489{
490 rwb->scale_step = 0;
491 rwb->scaled_max = false;
492 calc_wb_limits(rwb);
493
494 rwb_wake_all(rwb);
495}
496
497static bool close_io(struct rq_wb *rwb)
498{
499 const unsigned long now = jiffies;
500
501 return time_before(now, rwb->last_issue + HZ / 10) ||
502 time_before(now, rwb->last_comp + HZ / 10);
503}
504
505#define REQ_HIPRIO (REQ_SYNC | REQ_META | REQ_PRIO)
506
507static inline unsigned int get_limit(struct rq_wb *rwb, unsigned long rw)
508{
509 unsigned int limit;
510
Jens Axboe782f5692018-05-07 10:03:23 -0600511 if ((rw & REQ_OP_MASK) == REQ_OP_DISCARD)
512 return rwb->wb_background;
513
Jens Axboee34cbd32016-11-09 12:36:15 -0700514 /*
515 * At this point we know it's a buffered write. If this is
weiping zhang3dfbdc42017-11-23 21:40:10 +0800516 * kswapd trying to free memory, or REQ_SYNC is set, then
Jens Axboee34cbd32016-11-09 12:36:15 -0700517 * it's WB_SYNC_ALL writeback, and we'll use the max limit for
518 * that. If the write is marked as a background write, then use
519 * the idle limit, or go to normal if we haven't had competing
520 * IO for a bit.
521 */
522 if ((rw & REQ_HIPRIO) || wb_recent_wait(rwb) || current_is_kswapd())
523 limit = rwb->wb_max;
524 else if ((rw & REQ_BACKGROUND) || close_io(rwb)) {
525 /*
526 * If less than 100ms since we completed unrelated IO,
527 * limit us to half the depth for background writeback.
528 */
529 limit = rwb->wb_background;
530 } else
531 limit = rwb->wb_normal;
532
533 return limit;
534}
535
536static inline bool may_queue(struct rq_wb *rwb, struct rq_wait *rqw,
Ingo Molnarac6424b2017-06-20 12:06:13 +0200537 wait_queue_entry_t *wait, unsigned long rw)
Jens Axboee34cbd32016-11-09 12:36:15 -0700538{
539 /*
540 * inc it here even if disabled, since we'll dec it at completion.
541 * this only happens if the task was sleeping in __wbt_wait(),
542 * and someone turned it off at the same time.
543 */
544 if (!rwb_enabled(rwb)) {
545 atomic_inc(&rqw->inflight);
546 return true;
547 }
548
549 /*
550 * If the waitqueue is already active and we are not the next
551 * in line to be woken up, wait for our turn.
552 */
553 if (waitqueue_active(&rqw->wait) &&
Ingo Molnar2055da92017-06-20 12:06:46 +0200554 rqw->wait.head.next != &wait->entry)
Jens Axboee34cbd32016-11-09 12:36:15 -0700555 return false;
556
557 return atomic_inc_below(&rqw->inflight, get_limit(rwb, rw));
558}
559
560/*
561 * Block if we will exceed our limit, or if we are currently waiting for
562 * the timer to kick off queuing again.
563 */
Jens Axboe8bea6092018-05-07 09:57:08 -0600564static void __wbt_wait(struct rq_wb *rwb, enum wbt_flags wb_acct,
565 unsigned long rw, spinlock_t *lock)
Bart Van Assche9eca5352017-01-02 09:48:47 -0700566 __releases(lock)
567 __acquires(lock)
Jens Axboee34cbd32016-11-09 12:36:15 -0700568{
Jens Axboe8bea6092018-05-07 09:57:08 -0600569 struct rq_wait *rqw = get_rq_wait(rwb, wb_acct);
Jens Axboee34cbd32016-11-09 12:36:15 -0700570 DEFINE_WAIT(wait);
571
572 if (may_queue(rwb, rqw, &wait, rw))
573 return;
574
575 do {
576 prepare_to_wait_exclusive(&rqw->wait, &wait,
577 TASK_UNINTERRUPTIBLE);
578
579 if (may_queue(rwb, rqw, &wait, rw))
580 break;
581
Bart Van Assche9eca5352017-01-02 09:48:47 -0700582 if (lock) {
Jens Axboee34cbd32016-11-09 12:36:15 -0700583 spin_unlock_irq(lock);
Bart Van Assche9eca5352017-01-02 09:48:47 -0700584 io_schedule();
Jens Axboee34cbd32016-11-09 12:36:15 -0700585 spin_lock_irq(lock);
Bart Van Assche9eca5352017-01-02 09:48:47 -0700586 } else
587 io_schedule();
Jens Axboee34cbd32016-11-09 12:36:15 -0700588 } while (1);
589
590 finish_wait(&rqw->wait, &wait);
591}
592
593static inline bool wbt_should_throttle(struct rq_wb *rwb, struct bio *bio)
594{
Jens Axboe782f5692018-05-07 10:03:23 -0600595 switch (bio_op(bio)) {
596 case REQ_OP_WRITE:
597 /*
598 * Don't throttle WRITE_ODIRECT
599 */
600 if ((bio->bi_opf & (REQ_SYNC | REQ_IDLE)) ==
601 (REQ_SYNC | REQ_IDLE))
602 return false;
603 /* fallthrough */
604 case REQ_OP_DISCARD:
605 return true;
606 default:
Jens Axboee34cbd32016-11-09 12:36:15 -0700607 return false;
Jens Axboe782f5692018-05-07 10:03:23 -0600608 }
Jens Axboee34cbd32016-11-09 12:36:15 -0700609}
610
611/*
612 * Returns true if the IO request should be accounted, false if not.
613 * May sleep, if we have exceeded the writeback limits. Caller can pass
614 * in an irq held spinlock, if it holds one when calling this function.
615 * If we do sleep, we'll release and re-grab it.
616 */
Bart Van Asschef2e0a0b2017-01-02 09:46:15 -0700617enum wbt_flags wbt_wait(struct rq_wb *rwb, struct bio *bio, spinlock_t *lock)
Jens Axboee34cbd32016-11-09 12:36:15 -0700618{
Jens Axboe8bea6092018-05-07 09:57:08 -0600619 enum wbt_flags ret = 0;
Jens Axboee34cbd32016-11-09 12:36:15 -0700620
621 if (!rwb_enabled(rwb))
622 return 0;
623
624 if (bio_op(bio) == REQ_OP_READ)
625 ret = WBT_READ;
626
627 if (!wbt_should_throttle(rwb, bio)) {
628 if (ret & WBT_READ)
629 wb_timestamp(rwb, &rwb->last_issue);
630 return ret;
631 }
632
Jens Axboe8bea6092018-05-07 09:57:08 -0600633 if (current_is_kswapd())
634 ret |= WBT_KSWAPD;
Jens Axboe782f5692018-05-07 10:03:23 -0600635 if (bio_op(bio) == REQ_OP_DISCARD)
636 ret |= WBT_DISCARD;
Jens Axboe8bea6092018-05-07 09:57:08 -0600637
638 __wbt_wait(rwb, ret, bio->bi_opf, lock);
Jens Axboee34cbd32016-11-09 12:36:15 -0700639
Omar Sandoval34dbad52017-03-21 08:56:08 -0700640 if (!blk_stat_is_active(rwb->cb))
Jens Axboee34cbd32016-11-09 12:36:15 -0700641 rwb_arm_timer(rwb);
642
Jens Axboee34cbd32016-11-09 12:36:15 -0700643 return ret | WBT_TRACKED;
644}
645
Omar Sandovala8a45942018-05-09 02:08:48 -0700646void wbt_issue(struct rq_wb *rwb, struct request *rq)
Jens Axboee34cbd32016-11-09 12:36:15 -0700647{
648 if (!rwb_enabled(rwb))
649 return;
650
651 /*
Omar Sandovala8a45942018-05-09 02:08:48 -0700652 * Track sync issue, in case it takes a long time to complete. Allows us
653 * to react quicker, if a sync IO takes a long time to complete. Note
654 * that this is just a hint. The request can go away when it completes,
655 * so it's important we never dereference it. We only use the address to
656 * compare with, which is why we store the sync_issue time locally.
Jens Axboee34cbd32016-11-09 12:36:15 -0700657 */
Omar Sandovala8a45942018-05-09 02:08:48 -0700658 if (wbt_is_read(rq) && !rwb->sync_issue) {
659 rwb->sync_cookie = rq;
Omar Sandoval544ccc8d2018-05-09 02:08:50 -0700660 rwb->sync_issue = rq->io_start_time_ns;
Jens Axboee34cbd32016-11-09 12:36:15 -0700661 }
662}
663
Omar Sandovala8a45942018-05-09 02:08:48 -0700664void wbt_requeue(struct rq_wb *rwb, struct request *rq)
Jens Axboee34cbd32016-11-09 12:36:15 -0700665{
666 if (!rwb_enabled(rwb))
667 return;
Omar Sandovala8a45942018-05-09 02:08:48 -0700668 if (rq == rwb->sync_cookie) {
Jens Axboee34cbd32016-11-09 12:36:15 -0700669 rwb->sync_issue = 0;
670 rwb->sync_cookie = NULL;
671 }
672}
673
674void wbt_set_queue_depth(struct rq_wb *rwb, unsigned int depth)
675{
676 if (rwb) {
677 rwb->queue_depth = depth;
678 wbt_update_limits(rwb);
679 }
680}
681
682void wbt_set_write_cache(struct rq_wb *rwb, bool write_cache_on)
683{
684 if (rwb)
685 rwb->wc = write_cache_on;
686}
687
Jan Kara3f19cd22017-04-11 11:29:01 +0200688/*
Luca Micciob5dc5d42017-10-09 16:27:21 +0200689 * Disable wbt, if enabled by default.
Jens Axboefa224ee2016-11-28 09:25:50 -0700690 */
691void wbt_disable_default(struct request_queue *q)
Jens Axboee34cbd32016-11-09 12:36:15 -0700692{
Jens Axboefa224ee2016-11-28 09:25:50 -0700693 struct rq_wb *rwb = q->rq_wb;
694
Jan Kara3f19cd22017-04-11 11:29:01 +0200695 if (rwb && rwb->enable_state == WBT_STATE_ON_DEFAULT)
696 wbt_exit(q);
Jens Axboee34cbd32016-11-09 12:36:15 -0700697}
Jens Axboefa224ee2016-11-28 09:25:50 -0700698EXPORT_SYMBOL_GPL(wbt_disable_default);
Jens Axboee34cbd32016-11-09 12:36:15 -0700699
Jan Kara8330cdb2017-04-19 11:33:27 +0200700/*
701 * Enable wbt if defaults are configured that way
702 */
703void wbt_enable_default(struct request_queue *q)
704{
705 /* Throttling already enabled? */
706 if (q->rq_wb)
707 return;
708
709 /* Queue not registered? Maybe shutting down... */
710 if (!test_bit(QUEUE_FLAG_REGISTERED, &q->queue_flags))
711 return;
712
713 if ((q->mq_ops && IS_ENABLED(CONFIG_BLK_WBT_MQ)) ||
714 (q->request_fn && IS_ENABLED(CONFIG_BLK_WBT_SQ)))
715 wbt_init(q);
716}
717EXPORT_SYMBOL_GPL(wbt_enable_default);
718
Jens Axboe80e091d2016-11-28 09:22:47 -0700719u64 wbt_default_latency_nsec(struct request_queue *q)
720{
721 /*
722 * We default to 2msec for non-rotational storage, and 75msec
723 * for rotational storage.
724 */
725 if (blk_queue_nonrot(q))
726 return 2000000ULL;
727 else
728 return 75000000ULL;
729}
730
Jens Axboe99c749a2017-04-21 07:55:42 -0600731static int wbt_data_dir(const struct request *rq)
732{
Jens Axboe5235553d2018-02-05 13:16:56 -0700733 const int op = req_op(rq);
734
735 if (op == REQ_OP_READ)
736 return READ;
Jens Axboe825843b2018-05-03 09:14:57 -0600737 else if (op_is_write(op))
Jens Axboe5235553d2018-02-05 13:16:56 -0700738 return WRITE;
739
740 /* don't account */
741 return -1;
Jens Axboe99c749a2017-04-21 07:55:42 -0600742}
743
Jens Axboe8054b892016-11-10 21:50:51 -0700744int wbt_init(struct request_queue *q)
Jens Axboee34cbd32016-11-09 12:36:15 -0700745{
746 struct rq_wb *rwb;
747 int i;
748
Jens Axboee34cbd32016-11-09 12:36:15 -0700749 rwb = kzalloc(sizeof(*rwb), GFP_KERNEL);
750 if (!rwb)
751 return -ENOMEM;
752
Jens Axboe99c749a2017-04-21 07:55:42 -0600753 rwb->cb = blk_stat_alloc_callback(wb_timer_fn, wbt_data_dir, 2, rwb);
Omar Sandoval34dbad52017-03-21 08:56:08 -0700754 if (!rwb->cb) {
755 kfree(rwb);
756 return -ENOMEM;
757 }
758
Jens Axboee34cbd32016-11-09 12:36:15 -0700759 for (i = 0; i < WBT_NUM_RWQ; i++) {
760 atomic_set(&rwb->rq_wait[i].inflight, 0);
761 init_waitqueue_head(&rwb->rq_wait[i].wait);
762 }
763
Jens Axboee34cbd32016-11-09 12:36:15 -0700764 rwb->last_comp = rwb->last_issue = jiffies;
Jens Axboed8a0cbf2016-11-10 21:52:53 -0700765 rwb->queue = q;
Jens Axboee34cbd32016-11-09 12:36:15 -0700766 rwb->win_nsec = RWB_WINDOW_NSEC;
Jens Axboed62118b2016-11-28 09:40:34 -0700767 rwb->enable_state = WBT_STATE_ON_DEFAULT;
Jens Axboee34cbd32016-11-09 12:36:15 -0700768 wbt_update_limits(rwb);
769
770 /*
Omar Sandoval34dbad52017-03-21 08:56:08 -0700771 * Assign rwb and add the stats callback.
Jens Axboee34cbd32016-11-09 12:36:15 -0700772 */
773 q->rq_wb = rwb;
Omar Sandoval34dbad52017-03-21 08:56:08 -0700774 blk_stat_add_callback(q, rwb->cb);
Jens Axboee34cbd32016-11-09 12:36:15 -0700775
Jens Axboe80e091d2016-11-28 09:22:47 -0700776 rwb->min_lat_nsec = wbt_default_latency_nsec(q);
Jens Axboee34cbd32016-11-09 12:36:15 -0700777
778 wbt_set_queue_depth(rwb, blk_queue_depth(q));
779 wbt_set_write_cache(rwb, test_bit(QUEUE_FLAG_WC, &q->queue_flags));
780
781 return 0;
782}
783
784void wbt_exit(struct request_queue *q)
785{
786 struct rq_wb *rwb = q->rq_wb;
787
788 if (rwb) {
Omar Sandoval34dbad52017-03-21 08:56:08 -0700789 blk_stat_remove_callback(q, rwb->cb);
790 blk_stat_free_callback(rwb->cb);
Jens Axboee34cbd32016-11-09 12:36:15 -0700791 q->rq_wb = NULL;
792 kfree(rwb);
793 }
794}