blk-wbt: throttle discards like background writes
[linux-block.git] / block / blk-wbt.c
CommitLineData
e34cbd30
JA
1/*
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
32enum {
33 /*
34 * Default setting, we'll scale up (to 75% of QD max) or down (min 1)
35 * from here depending on device stats
36 */
37 RWB_DEF_DEPTH = 16,
38
39 /*
40 * 100msec window
41 */
42 RWB_WINDOW_NSEC = 100 * 1000 * 1000ULL,
43
44 /*
45 * Disregard stats, if we don't meet this minimum
46 */
47 RWB_MIN_WRITE_SAMPLES = 3,
48
49 /*
50 * If we have this number of consecutive windows with not enough
51 * information to scale up or down, scale up.
52 */
53 RWB_UNKNOWN_BUMP = 5,
54};
55
56static inline bool rwb_enabled(struct rq_wb *rwb)
57{
58 return rwb && rwb->wb_normal != 0;
59}
60
61/*
62 * Increment 'v', if 'v' is below 'below'. Returns true if we succeeded,
63 * false if 'v' + 1 would be bigger than 'below'.
64 */
65static bool atomic_inc_below(atomic_t *v, int below)
66{
67 int cur = atomic_read(v);
68
69 for (;;) {
70 int old;
71
72 if (cur >= below)
73 return false;
74 old = atomic_cmpxchg(v, cur, cur + 1);
75 if (old == cur)
76 break;
77 cur = old;
78 }
79
80 return true;
81}
82
83static void wb_timestamp(struct rq_wb *rwb, unsigned long *var)
84{
85 if (rwb_enabled(rwb)) {
86 const unsigned long cur = jiffies;
87
88 if (cur != *var)
89 *var = cur;
90 }
91}
92
93/*
94 * If a task was rate throttled in balance_dirty_pages() within the last
95 * second or so, use that to indicate a higher cleaning rate.
96 */
97static bool wb_recent_wait(struct rq_wb *rwb)
98{
dc3b17cc 99 struct bdi_writeback *wb = &rwb->queue->backing_dev_info->wb;
e34cbd30
JA
100
101 return time_before(jiffies, wb->dirty_sleep + HZ);
102}
103
8bea6090
JA
104static inline struct rq_wait *get_rq_wait(struct rq_wb *rwb,
105 enum wbt_flags wb_acct)
e34cbd30 106{
8bea6090
JA
107 if (wb_acct & WBT_KSWAPD)
108 return &rwb->rq_wait[WBT_RWQ_KSWAPD];
782f5697
JA
109 else if (wb_acct & WBT_DISCARD)
110 return &rwb->rq_wait[WBT_RWQ_DISCARD];
8bea6090
JA
111
112 return &rwb->rq_wait[WBT_RWQ_BG];
e34cbd30
JA
113}
114
115static void rwb_wake_all(struct rq_wb *rwb)
116{
117 int i;
118
119 for (i = 0; i < WBT_NUM_RWQ; i++) {
120 struct rq_wait *rqw = &rwb->rq_wait[i];
121
122 if (waitqueue_active(&rqw->wait))
123 wake_up_all(&rqw->wait);
124 }
125}
126
127void __wbt_done(struct rq_wb *rwb, enum wbt_flags wb_acct)
128{
129 struct rq_wait *rqw;
130 int inflight, limit;
131
132 if (!(wb_acct & WBT_TRACKED))
133 return;
134
8bea6090 135 rqw = get_rq_wait(rwb, wb_acct);
e34cbd30
JA
136 inflight = atomic_dec_return(&rqw->inflight);
137
138 /*
139 * wbt got disabled with IO in flight. Wake up any potential
140 * waiters, we don't have to do more than that.
141 */
142 if (unlikely(!rwb_enabled(rwb))) {
143 rwb_wake_all(rwb);
144 return;
145 }
146
147 /*
782f5697
JA
148 * For discards, our limit is always the background. For writes, if
149 * the device does write back caching, drop further down before we
150 * wake people up.
e34cbd30 151 */
782f5697
JA
152 if (wb_acct & WBT_DISCARD)
153 limit = rwb->wb_background;
154 else if (rwb->wc && !wb_recent_wait(rwb))
e34cbd30
JA
155 limit = 0;
156 else
157 limit = rwb->wb_normal;
158
159 /*
160 * Don't wake anyone up if we are above the normal limit.
161 */
162 if (inflight && inflight >= limit)
163 return;
164
165 if (waitqueue_active(&rqw->wait)) {
166 int diff = limit - inflight;
167
168 if (!inflight || diff >= rwb->wb_background / 2)
169 wake_up_all(&rqw->wait);
170 }
171}
172
173/*
174 * Called on completion of a request. Note that it's also called when
175 * a request is merged, when the request gets freed.
176 */
177void wbt_done(struct rq_wb *rwb, struct blk_issue_stat *stat)
178{
179 if (!rwb)
180 return;
181
182 if (!wbt_is_tracked(stat)) {
183 if (rwb->sync_cookie == stat) {
184 rwb->sync_issue = 0;
185 rwb->sync_cookie = NULL;
186 }
187
188 if (wbt_is_read(stat))
189 wb_timestamp(rwb, &rwb->last_comp);
e34cbd30
JA
190 } else {
191 WARN_ON_ONCE(stat == rwb->sync_cookie);
192 __wbt_done(rwb, wbt_stat_to_mask(stat));
e34cbd30 193 }
62d772fa 194 wbt_clear_state(stat);
e34cbd30
JA
195}
196
197/*
198 * Return true, if we can't increase the depth further by scaling
199 */
200static bool calc_wb_limits(struct rq_wb *rwb)
201{
202 unsigned int depth;
203 bool ret = false;
204
205 if (!rwb->min_lat_nsec) {
206 rwb->wb_max = rwb->wb_normal = rwb->wb_background = 0;
207 return false;
208 }
209
210 /*
211 * For QD=1 devices, this is a special case. It's important for those
212 * to have one request ready when one completes, so force a depth of
213 * 2 for those devices. On the backend, it'll be a depth of 1 anyway,
214 * since the device can't have more than that in flight. If we're
215 * scaling down, then keep a setting of 1/1/1.
216 */
217 if (rwb->queue_depth == 1) {
218 if (rwb->scale_step > 0)
219 rwb->wb_max = rwb->wb_normal = 1;
220 else {
221 rwb->wb_max = rwb->wb_normal = 2;
222 ret = true;
223 }
224 rwb->wb_background = 1;
225 } else {
226 /*
227 * scale_step == 0 is our default state. If we have suffered
228 * latency spikes, step will be > 0, and we shrink the
229 * allowed write depths. If step is < 0, we're only doing
230 * writes, and we allow a temporarily higher depth to
231 * increase performance.
232 */
233 depth = min_t(unsigned int, RWB_DEF_DEPTH, rwb->queue_depth);
234 if (rwb->scale_step > 0)
235 depth = 1 + ((depth - 1) >> min(31, rwb->scale_step));
236 else if (rwb->scale_step < 0) {
237 unsigned int maxd = 3 * rwb->queue_depth / 4;
238
239 depth = 1 + ((depth - 1) << -rwb->scale_step);
240 if (depth > maxd) {
241 depth = maxd;
242 ret = true;
243 }
244 }
245
246 /*
247 * Set our max/normal/bg queue depths based on how far
248 * we have scaled down (->scale_step).
249 */
250 rwb->wb_max = depth;
251 rwb->wb_normal = (rwb->wb_max + 1) / 2;
252 rwb->wb_background = (rwb->wb_max + 3) / 4;
253 }
254
255 return ret;
256}
257
4121d385 258static inline bool stat_sample_valid(struct blk_rq_stat *stat)
e34cbd30
JA
259{
260 /*
261 * We need at least one read sample, and a minimum of
262 * RWB_MIN_WRITE_SAMPLES. We require some write samples to know
263 * that it's writes impacting us, and not just some sole read on
264 * a device that is in a lower power state.
265 */
fa2e39cb
OS
266 return (stat[READ].nr_samples >= 1 &&
267 stat[WRITE].nr_samples >= RWB_MIN_WRITE_SAMPLES);
e34cbd30
JA
268}
269
270static u64 rwb_sync_issue_lat(struct rq_wb *rwb)
271{
6aa7de05 272 u64 now, issue = READ_ONCE(rwb->sync_issue);
e34cbd30
JA
273
274 if (!issue || !rwb->sync_cookie)
275 return 0;
276
277 now = ktime_to_ns(ktime_get());
278 return now - issue;
279}
280
281enum {
282 LAT_OK = 1,
283 LAT_UNKNOWN,
284 LAT_UNKNOWN_WRITES,
285 LAT_EXCEEDED,
286};
287
34dbad5d 288static int latency_exceeded(struct rq_wb *rwb, struct blk_rq_stat *stat)
e34cbd30 289{
dc3b17cc 290 struct backing_dev_info *bdi = rwb->queue->backing_dev_info;
e34cbd30
JA
291 u64 thislat;
292
293 /*
294 * If our stored sync issue exceeds the window size, or it
295 * exceeds our min target AND we haven't logged any entries,
296 * flag the latency as exceeded. wbt works off completion latencies,
297 * but for a flooded device, a single sync IO can take a long time
298 * to complete after being issued. If this time exceeds our
299 * monitoring window AND we didn't see any other completions in that
300 * window, then count that sync IO as a violation of the latency.
301 */
302 thislat = rwb_sync_issue_lat(rwb);
303 if (thislat > rwb->cur_win_nsec ||
fa2e39cb 304 (thislat > rwb->min_lat_nsec && !stat[READ].nr_samples)) {
d8a0cbfd 305 trace_wbt_lat(bdi, thislat);
e34cbd30
JA
306 return LAT_EXCEEDED;
307 }
308
309 /*
310 * No read/write mix, if stat isn't valid
311 */
312 if (!stat_sample_valid(stat)) {
313 /*
314 * If we had writes in this stat window and the window is
315 * current, we're only doing writes. If a task recently
316 * waited or still has writes in flights, consider us doing
317 * just writes as well.
318 */
34dbad5d
OS
319 if (stat[WRITE].nr_samples || wb_recent_wait(rwb) ||
320 wbt_inflight(rwb))
e34cbd30
JA
321 return LAT_UNKNOWN_WRITES;
322 return LAT_UNKNOWN;
323 }
324
325 /*
326 * If the 'min' latency exceeds our target, step down.
327 */
fa2e39cb
OS
328 if (stat[READ].min > rwb->min_lat_nsec) {
329 trace_wbt_lat(bdi, stat[READ].min);
d8a0cbfd 330 trace_wbt_stat(bdi, stat);
e34cbd30
JA
331 return LAT_EXCEEDED;
332 }
333
334 if (rwb->scale_step)
d8a0cbfd 335 trace_wbt_stat(bdi, stat);
e34cbd30
JA
336
337 return LAT_OK;
338}
339
e34cbd30
JA
340static void rwb_trace_step(struct rq_wb *rwb, const char *msg)
341{
dc3b17cc 342 struct backing_dev_info *bdi = rwb->queue->backing_dev_info;
d8a0cbfd
JA
343
344 trace_wbt_step(bdi, msg, rwb->scale_step, rwb->cur_win_nsec,
e34cbd30
JA
345 rwb->wb_background, rwb->wb_normal, rwb->wb_max);
346}
347
348static void scale_up(struct rq_wb *rwb)
349{
350 /*
351 * Hit max in previous round, stop here
352 */
353 if (rwb->scaled_max)
354 return;
355
356 rwb->scale_step--;
357 rwb->unknown_cnt = 0;
e34cbd30
JA
358
359 rwb->scaled_max = calc_wb_limits(rwb);
360
361 rwb_wake_all(rwb);
362
363 rwb_trace_step(rwb, "step up");
364}
365
366/*
367 * Scale rwb down. If 'hard_throttle' is set, do it quicker, since we
368 * had a latency violation.
369 */
370static void scale_down(struct rq_wb *rwb, bool hard_throttle)
371{
372 /*
373 * Stop scaling down when we've hit the limit. This also prevents
374 * ->scale_step from going to crazy values, if the device can't
375 * keep up.
376 */
377 if (rwb->wb_max == 1)
378 return;
379
380 if (rwb->scale_step < 0 && hard_throttle)
381 rwb->scale_step = 0;
382 else
383 rwb->scale_step++;
384
385 rwb->scaled_max = false;
386 rwb->unknown_cnt = 0;
e34cbd30
JA
387 calc_wb_limits(rwb);
388 rwb_trace_step(rwb, "step down");
389}
390
391static void rwb_arm_timer(struct rq_wb *rwb)
392{
e34cbd30
JA
393 if (rwb->scale_step > 0) {
394 /*
395 * We should speed this up, using some variant of a fast
396 * integer inverse square root calculation. Since we only do
397 * this for every window expiration, it's not a huge deal,
398 * though.
399 */
400 rwb->cur_win_nsec = div_u64(rwb->win_nsec << 4,
401 int_sqrt((rwb->scale_step + 1) << 8));
402 } else {
403 /*
404 * For step < 0, we don't want to increase/decrease the
405 * window size.
406 */
407 rwb->cur_win_nsec = rwb->win_nsec;
408 }
409
34dbad5d 410 blk_stat_activate_nsecs(rwb->cb, rwb->cur_win_nsec);
e34cbd30
JA
411}
412
34dbad5d 413static void wb_timer_fn(struct blk_stat_callback *cb)
e34cbd30 414{
34dbad5d 415 struct rq_wb *rwb = cb->data;
e34cbd30
JA
416 unsigned int inflight = wbt_inflight(rwb);
417 int status;
418
34dbad5d 419 status = latency_exceeded(rwb, cb->stat);
e34cbd30 420
dc3b17cc 421 trace_wbt_timer(rwb->queue->backing_dev_info, status, rwb->scale_step,
d8a0cbfd 422 inflight);
e34cbd30
JA
423
424 /*
425 * If we exceeded the latency target, step down. If we did not,
426 * step one level up. If we don't know enough to say either exceeded
427 * or ok, then don't do anything.
428 */
429 switch (status) {
430 case LAT_EXCEEDED:
431 scale_down(rwb, true);
432 break;
433 case LAT_OK:
434 scale_up(rwb);
435 break;
436 case LAT_UNKNOWN_WRITES:
437 /*
438 * We started a the center step, but don't have a valid
439 * read/write sample, but we do have writes going on.
440 * Allow step to go negative, to increase write perf.
441 */
442 scale_up(rwb);
443 break;
444 case LAT_UNKNOWN:
445 if (++rwb->unknown_cnt < RWB_UNKNOWN_BUMP)
446 break;
447 /*
448 * We get here when previously scaled reduced depth, and we
449 * currently don't have a valid read/write sample. For that
450 * case, slowly return to center state (step == 0).
451 */
452 if (rwb->scale_step > 0)
453 scale_up(rwb);
454 else if (rwb->scale_step < 0)
455 scale_down(rwb, false);
456 break;
457 default:
458 break;
459 }
460
461 /*
462 * Re-arm timer, if we have IO in flight
463 */
464 if (rwb->scale_step || inflight)
465 rwb_arm_timer(rwb);
466}
467
468void wbt_update_limits(struct rq_wb *rwb)
469{
470 rwb->scale_step = 0;
471 rwb->scaled_max = false;
472 calc_wb_limits(rwb);
473
474 rwb_wake_all(rwb);
475}
476
477static bool close_io(struct rq_wb *rwb)
478{
479 const unsigned long now = jiffies;
480
481 return time_before(now, rwb->last_issue + HZ / 10) ||
482 time_before(now, rwb->last_comp + HZ / 10);
483}
484
485#define REQ_HIPRIO (REQ_SYNC | REQ_META | REQ_PRIO)
486
487static inline unsigned int get_limit(struct rq_wb *rwb, unsigned long rw)
488{
489 unsigned int limit;
490
782f5697
JA
491 if ((rw & REQ_OP_MASK) == REQ_OP_DISCARD)
492 return rwb->wb_background;
493
e34cbd30
JA
494 /*
495 * At this point we know it's a buffered write. If this is
3dfbdc44 496 * kswapd trying to free memory, or REQ_SYNC is set, then
e34cbd30
JA
497 * it's WB_SYNC_ALL writeback, and we'll use the max limit for
498 * that. If the write is marked as a background write, then use
499 * the idle limit, or go to normal if we haven't had competing
500 * IO for a bit.
501 */
502 if ((rw & REQ_HIPRIO) || wb_recent_wait(rwb) || current_is_kswapd())
503 limit = rwb->wb_max;
504 else if ((rw & REQ_BACKGROUND) || close_io(rwb)) {
505 /*
506 * If less than 100ms since we completed unrelated IO,
507 * limit us to half the depth for background writeback.
508 */
509 limit = rwb->wb_background;
510 } else
511 limit = rwb->wb_normal;
512
513 return limit;
514}
515
516static inline bool may_queue(struct rq_wb *rwb, struct rq_wait *rqw,
ac6424b9 517 wait_queue_entry_t *wait, unsigned long rw)
e34cbd30
JA
518{
519 /*
520 * inc it here even if disabled, since we'll dec it at completion.
521 * this only happens if the task was sleeping in __wbt_wait(),
522 * and someone turned it off at the same time.
523 */
524 if (!rwb_enabled(rwb)) {
525 atomic_inc(&rqw->inflight);
526 return true;
527 }
528
529 /*
530 * If the waitqueue is already active and we are not the next
531 * in line to be woken up, wait for our turn.
532 */
533 if (waitqueue_active(&rqw->wait) &&
2055da97 534 rqw->wait.head.next != &wait->entry)
e34cbd30
JA
535 return false;
536
537 return atomic_inc_below(&rqw->inflight, get_limit(rwb, rw));
538}
539
540/*
541 * Block if we will exceed our limit, or if we are currently waiting for
542 * the timer to kick off queuing again.
543 */
8bea6090
JA
544static void __wbt_wait(struct rq_wb *rwb, enum wbt_flags wb_acct,
545 unsigned long rw, spinlock_t *lock)
9eca5350
BVA
546 __releases(lock)
547 __acquires(lock)
e34cbd30 548{
8bea6090 549 struct rq_wait *rqw = get_rq_wait(rwb, wb_acct);
e34cbd30
JA
550 DEFINE_WAIT(wait);
551
552 if (may_queue(rwb, rqw, &wait, rw))
553 return;
554
555 do {
556 prepare_to_wait_exclusive(&rqw->wait, &wait,
557 TASK_UNINTERRUPTIBLE);
558
559 if (may_queue(rwb, rqw, &wait, rw))
560 break;
561
9eca5350 562 if (lock) {
e34cbd30 563 spin_unlock_irq(lock);
9eca5350 564 io_schedule();
e34cbd30 565 spin_lock_irq(lock);
9eca5350
BVA
566 } else
567 io_schedule();
e34cbd30
JA
568 } while (1);
569
570 finish_wait(&rqw->wait, &wait);
571}
572
573static inline bool wbt_should_throttle(struct rq_wb *rwb, struct bio *bio)
574{
782f5697
JA
575 switch (bio_op(bio)) {
576 case REQ_OP_WRITE:
577 /*
578 * Don't throttle WRITE_ODIRECT
579 */
580 if ((bio->bi_opf & (REQ_SYNC | REQ_IDLE)) ==
581 (REQ_SYNC | REQ_IDLE))
582 return false;
583 /* fallthrough */
584 case REQ_OP_DISCARD:
585 return true;
586 default:
e34cbd30 587 return false;
782f5697 588 }
e34cbd30
JA
589}
590
591/*
592 * Returns true if the IO request should be accounted, false if not.
593 * May sleep, if we have exceeded the writeback limits. Caller can pass
594 * in an irq held spinlock, if it holds one when calling this function.
595 * If we do sleep, we'll release and re-grab it.
596 */
f2e0a0b2 597enum wbt_flags wbt_wait(struct rq_wb *rwb, struct bio *bio, spinlock_t *lock)
e34cbd30 598{
8bea6090 599 enum wbt_flags ret = 0;
e34cbd30
JA
600
601 if (!rwb_enabled(rwb))
602 return 0;
603
604 if (bio_op(bio) == REQ_OP_READ)
605 ret = WBT_READ;
606
607 if (!wbt_should_throttle(rwb, bio)) {
608 if (ret & WBT_READ)
609 wb_timestamp(rwb, &rwb->last_issue);
610 return ret;
611 }
612
8bea6090
JA
613 if (current_is_kswapd())
614 ret |= WBT_KSWAPD;
782f5697
JA
615 if (bio_op(bio) == REQ_OP_DISCARD)
616 ret |= WBT_DISCARD;
8bea6090
JA
617
618 __wbt_wait(rwb, ret, bio->bi_opf, lock);
e34cbd30 619
34dbad5d 620 if (!blk_stat_is_active(rwb->cb))
e34cbd30
JA
621 rwb_arm_timer(rwb);
622
e34cbd30
JA
623 return ret | WBT_TRACKED;
624}
625
626void wbt_issue(struct rq_wb *rwb, struct blk_issue_stat *stat)
627{
628 if (!rwb_enabled(rwb))
629 return;
630
631 /*
632 * Track sync issue, in case it takes a long time to complete. Allows
633 * us to react quicker, if a sync IO takes a long time to complete.
634 * Note that this is just a hint. 'stat' can go away when the
635 * request completes, so it's important we never dereference it. We
636 * only use the address to compare with, which is why we store the
637 * sync_issue time locally.
638 */
639 if (wbt_is_read(stat) && !rwb->sync_issue) {
640 rwb->sync_cookie = stat;
641 rwb->sync_issue = blk_stat_time(stat);
642 }
643}
644
645void wbt_requeue(struct rq_wb *rwb, struct blk_issue_stat *stat)
646{
647 if (!rwb_enabled(rwb))
648 return;
649 if (stat == rwb->sync_cookie) {
650 rwb->sync_issue = 0;
651 rwb->sync_cookie = NULL;
652 }
653}
654
655void wbt_set_queue_depth(struct rq_wb *rwb, unsigned int depth)
656{
657 if (rwb) {
658 rwb->queue_depth = depth;
659 wbt_update_limits(rwb);
660 }
661}
662
663void wbt_set_write_cache(struct rq_wb *rwb, bool write_cache_on)
664{
665 if (rwb)
666 rwb->wc = write_cache_on;
667}
668
3f19cd23 669/*
b5dc5d4d 670 * Disable wbt, if enabled by default.
fa224eed
JA
671 */
672void wbt_disable_default(struct request_queue *q)
e34cbd30 673{
fa224eed
JA
674 struct rq_wb *rwb = q->rq_wb;
675
3f19cd23
JK
676 if (rwb && rwb->enable_state == WBT_STATE_ON_DEFAULT)
677 wbt_exit(q);
e34cbd30 678}
fa224eed 679EXPORT_SYMBOL_GPL(wbt_disable_default);
e34cbd30 680
8330cdb0
JK
681/*
682 * Enable wbt if defaults are configured that way
683 */
684void wbt_enable_default(struct request_queue *q)
685{
686 /* Throttling already enabled? */
687 if (q->rq_wb)
688 return;
689
690 /* Queue not registered? Maybe shutting down... */
691 if (!test_bit(QUEUE_FLAG_REGISTERED, &q->queue_flags))
692 return;
693
694 if ((q->mq_ops && IS_ENABLED(CONFIG_BLK_WBT_MQ)) ||
695 (q->request_fn && IS_ENABLED(CONFIG_BLK_WBT_SQ)))
696 wbt_init(q);
697}
698EXPORT_SYMBOL_GPL(wbt_enable_default);
699
80e091d1
JA
700u64 wbt_default_latency_nsec(struct request_queue *q)
701{
702 /*
703 * We default to 2msec for non-rotational storage, and 75msec
704 * for rotational storage.
705 */
706 if (blk_queue_nonrot(q))
707 return 2000000ULL;
708 else
709 return 75000000ULL;
710}
711
99c749a4
JA
712static int wbt_data_dir(const struct request *rq)
713{
5235553d
JA
714 const int op = req_op(rq);
715
716 if (op == REQ_OP_READ)
717 return READ;
825843b0 718 else if (op_is_write(op))
5235553d
JA
719 return WRITE;
720
721 /* don't account */
722 return -1;
99c749a4
JA
723}
724
8054b89f 725int wbt_init(struct request_queue *q)
e34cbd30
JA
726{
727 struct rq_wb *rwb;
728 int i;
729
e34cbd30
JA
730 BUILD_BUG_ON(WBT_NR_BITS > BLK_STAT_RES_BITS);
731
e34cbd30
JA
732 rwb = kzalloc(sizeof(*rwb), GFP_KERNEL);
733 if (!rwb)
734 return -ENOMEM;
735
99c749a4 736 rwb->cb = blk_stat_alloc_callback(wb_timer_fn, wbt_data_dir, 2, rwb);
34dbad5d
OS
737 if (!rwb->cb) {
738 kfree(rwb);
739 return -ENOMEM;
740 }
741
e34cbd30
JA
742 for (i = 0; i < WBT_NUM_RWQ; i++) {
743 atomic_set(&rwb->rq_wait[i].inflight, 0);
744 init_waitqueue_head(&rwb->rq_wait[i].wait);
745 }
746
e34cbd30 747 rwb->last_comp = rwb->last_issue = jiffies;
d8a0cbfd 748 rwb->queue = q;
e34cbd30 749 rwb->win_nsec = RWB_WINDOW_NSEC;
d62118b6 750 rwb->enable_state = WBT_STATE_ON_DEFAULT;
e34cbd30
JA
751 wbt_update_limits(rwb);
752
753 /*
34dbad5d 754 * Assign rwb and add the stats callback.
e34cbd30
JA
755 */
756 q->rq_wb = rwb;
34dbad5d 757 blk_stat_add_callback(q, rwb->cb);
e34cbd30 758
80e091d1 759 rwb->min_lat_nsec = wbt_default_latency_nsec(q);
e34cbd30
JA
760
761 wbt_set_queue_depth(rwb, blk_queue_depth(q));
762 wbt_set_write_cache(rwb, test_bit(QUEUE_FLAG_WC, &q->queue_flags));
763
764 return 0;
765}
766
767void wbt_exit(struct request_queue *q)
768{
769 struct rq_wb *rwb = q->rq_wb;
770
771 if (rwb) {
34dbad5d
OS
772 blk_stat_remove_callback(q, rwb->cb);
773 blk_stat_free_callback(rwb->cb);
e34cbd30
JA
774 q->rq_wb = NULL;
775 kfree(rwb);
776 }
777}