mm/memblock.c: do not complain about top-down allocations for !MEMORY_HOTREMOVE
[linux-2.6-block.git] / block / blk-wbt.c
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
32 static inline void wbt_clear_state(struct request *rq)
33 {
34         rq->wbt_flags = 0;
35 }
36
37 static inline enum wbt_flags wbt_flags(struct request *rq)
38 {
39         return rq->wbt_flags;
40 }
41
42 static inline bool wbt_is_tracked(struct request *rq)
43 {
44         return rq->wbt_flags & WBT_TRACKED;
45 }
46
47 static inline bool wbt_is_read(struct request *rq)
48 {
49         return rq->wbt_flags & WBT_READ;
50 }
51
52 enum {
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
76 static 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  */
85 static 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
103 static 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  */
117 static bool wb_recent_wait(struct rq_wb *rwb)
118 {
119         struct bdi_writeback *wb = &rwb->queue->backing_dev_info->wb;
120
121         return time_before(jiffies, wb->dirty_sleep + HZ);
122 }
123
124 static inline struct rq_wait *get_rq_wait(struct rq_wb *rwb,
125                                           enum wbt_flags wb_acct)
126 {
127         if (wb_acct & WBT_KSWAPD)
128                 return &rwb->rq_wait[WBT_RWQ_KSWAPD];
129         else if (wb_acct & WBT_DISCARD)
130                 return &rwb->rq_wait[WBT_RWQ_DISCARD];
131
132         return &rwb->rq_wait[WBT_RWQ_BG];
133 }
134
135 static 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
147 void __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
155         rqw = get_rq_wait(rwb, wb_acct);
156         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         /*
168          * 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.
171          */
172         if (wb_acct & WBT_DISCARD)
173                 limit = rwb->wb_background;
174         else if (rwb->wc && !wb_recent_wait(rwb))
175                 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  */
197 void wbt_done(struct rq_wb *rwb, struct request *rq)
198 {
199         if (!rwb)
200                 return;
201
202         if (!wbt_is_tracked(rq)) {
203                 if (rwb->sync_cookie == rq) {
204                         rwb->sync_issue = 0;
205                         rwb->sync_cookie = NULL;
206                 }
207
208                 if (wbt_is_read(rq))
209                         wb_timestamp(rwb, &rwb->last_comp);
210         } else {
211                 WARN_ON_ONCE(rq == rwb->sync_cookie);
212                 __wbt_done(rwb, wbt_flags(rq));
213         }
214         wbt_clear_state(rq);
215 }
216
217 /*
218  * Return true, if we can't increase the depth further by scaling
219  */
220 static 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
278 static inline bool stat_sample_valid(struct blk_rq_stat *stat)
279 {
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          */
286         return (stat[READ].nr_samples >= 1 &&
287                 stat[WRITE].nr_samples >= RWB_MIN_WRITE_SAMPLES);
288 }
289
290 static u64 rwb_sync_issue_lat(struct rq_wb *rwb)
291 {
292         u64 now, issue = READ_ONCE(rwb->sync_issue);
293
294         if (!issue || !rwb->sync_cookie)
295                 return 0;
296
297         now = ktime_to_ns(ktime_get());
298         return now - issue;
299 }
300
301 enum {
302         LAT_OK = 1,
303         LAT_UNKNOWN,
304         LAT_UNKNOWN_WRITES,
305         LAT_EXCEEDED,
306 };
307
308 static int latency_exceeded(struct rq_wb *rwb, struct blk_rq_stat *stat)
309 {
310         struct backing_dev_info *bdi = rwb->queue->backing_dev_info;
311         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 ||
324             (thislat > rwb->min_lat_nsec && !stat[READ].nr_samples)) {
325                 trace_wbt_lat(bdi, thislat);
326                 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                  */
339                 if (stat[WRITE].nr_samples || wb_recent_wait(rwb) ||
340                     wbt_inflight(rwb))
341                         return LAT_UNKNOWN_WRITES;
342                 return LAT_UNKNOWN;
343         }
344
345         /*
346          * If the 'min' latency exceeds our target, step down.
347          */
348         if (stat[READ].min > rwb->min_lat_nsec) {
349                 trace_wbt_lat(bdi, stat[READ].min);
350                 trace_wbt_stat(bdi, stat);
351                 return LAT_EXCEEDED;
352         }
353
354         if (rwb->scale_step)
355                 trace_wbt_stat(bdi, stat);
356
357         return LAT_OK;
358 }
359
360 static void rwb_trace_step(struct rq_wb *rwb, const char *msg)
361 {
362         struct backing_dev_info *bdi = rwb->queue->backing_dev_info;
363
364         trace_wbt_step(bdi, msg, rwb->scale_step, rwb->cur_win_nsec,
365                         rwb->wb_background, rwb->wb_normal, rwb->wb_max);
366 }
367
368 static 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;
378
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  */
390 static 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;
407         calc_wb_limits(rwb);
408         rwb_trace_step(rwb, "step down");
409 }
410
411 static void rwb_arm_timer(struct rq_wb *rwb)
412 {
413         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
430         blk_stat_activate_nsecs(rwb->cb, rwb->cur_win_nsec);
431 }
432
433 static void wb_timer_fn(struct blk_stat_callback *cb)
434 {
435         struct rq_wb *rwb = cb->data;
436         unsigned int inflight = wbt_inflight(rwb);
437         int status;
438
439         status = latency_exceeded(rwb, cb->stat);
440
441         trace_wbt_timer(rwb->queue->backing_dev_info, status, rwb->scale_step,
442                         inflight);
443
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
488 void 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
497 static 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
507 static inline unsigned int get_limit(struct rq_wb *rwb, unsigned long rw)
508 {
509         unsigned int limit;
510
511         if ((rw & REQ_OP_MASK) == REQ_OP_DISCARD)
512                 return rwb->wb_background;
513
514         /*
515          * At this point we know it's a buffered write. If this is
516          * kswapd trying to free memory, or REQ_SYNC is set, then
517          * 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
536 static inline bool may_queue(struct rq_wb *rwb, struct rq_wait *rqw,
537                              wait_queue_entry_t *wait, unsigned long rw)
538 {
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) &&
554             rqw->wait.head.next != &wait->entry)
555                 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  */
564 static void __wbt_wait(struct rq_wb *rwb, enum wbt_flags wb_acct,
565                        unsigned long rw, spinlock_t *lock)
566         __releases(lock)
567         __acquires(lock)
568 {
569         struct rq_wait *rqw = get_rq_wait(rwb, wb_acct);
570         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
582                 if (lock) {
583                         spin_unlock_irq(lock);
584                         io_schedule();
585                         spin_lock_irq(lock);
586                 } else
587                         io_schedule();
588         } while (1);
589
590         finish_wait(&rqw->wait, &wait);
591 }
592
593 static inline bool wbt_should_throttle(struct rq_wb *rwb, struct bio *bio)
594 {
595         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:
607                 return false;
608         }
609 }
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  */
617 enum wbt_flags wbt_wait(struct rq_wb *rwb, struct bio *bio, spinlock_t *lock)
618 {
619         enum wbt_flags ret = 0;
620
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
633         if (current_is_kswapd())
634                 ret |= WBT_KSWAPD;
635         if (bio_op(bio) == REQ_OP_DISCARD)
636                 ret |= WBT_DISCARD;
637
638         __wbt_wait(rwb, ret, bio->bi_opf, lock);
639
640         if (!blk_stat_is_active(rwb->cb))
641                 rwb_arm_timer(rwb);
642
643         return ret | WBT_TRACKED;
644 }
645
646 void wbt_issue(struct rq_wb *rwb, struct request *rq)
647 {
648         if (!rwb_enabled(rwb))
649                 return;
650
651         /*
652          * 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.
657          */
658         if (wbt_is_read(rq) && !rwb->sync_issue) {
659                 rwb->sync_cookie = rq;
660                 rwb->sync_issue = rq->io_start_time_ns;
661         }
662 }
663
664 void wbt_requeue(struct rq_wb *rwb, struct request *rq)
665 {
666         if (!rwb_enabled(rwb))
667                 return;
668         if (rq == rwb->sync_cookie) {
669                 rwb->sync_issue = 0;
670                 rwb->sync_cookie = NULL;
671         }
672 }
673
674 void 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
682 void wbt_set_write_cache(struct rq_wb *rwb, bool write_cache_on)
683 {
684         if (rwb)
685                 rwb->wc = write_cache_on;
686 }
687
688 /*
689  * Disable wbt, if enabled by default.
690  */
691 void wbt_disable_default(struct request_queue *q)
692 {
693         struct rq_wb *rwb = q->rq_wb;
694
695         if (rwb && rwb->enable_state == WBT_STATE_ON_DEFAULT)
696                 wbt_exit(q);
697 }
698 EXPORT_SYMBOL_GPL(wbt_disable_default);
699
700 /*
701  * Enable wbt if defaults are configured that way
702  */
703 void 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 }
717 EXPORT_SYMBOL_GPL(wbt_enable_default);
718
719 u64 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
731 static int wbt_data_dir(const struct request *rq)
732 {
733         const int op = req_op(rq);
734
735         if (op == REQ_OP_READ)
736                 return READ;
737         else if (op_is_write(op))
738                 return WRITE;
739
740         /* don't account */
741         return -1;
742 }
743
744 int wbt_init(struct request_queue *q)
745 {
746         struct rq_wb *rwb;
747         int i;
748
749         rwb = kzalloc(sizeof(*rwb), GFP_KERNEL);
750         if (!rwb)
751                 return -ENOMEM;
752
753         rwb->cb = blk_stat_alloc_callback(wb_timer_fn, wbt_data_dir, 2, rwb);
754         if (!rwb->cb) {
755                 kfree(rwb);
756                 return -ENOMEM;
757         }
758
759         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
764         rwb->last_comp = rwb->last_issue = jiffies;
765         rwb->queue = q;
766         rwb->win_nsec = RWB_WINDOW_NSEC;
767         rwb->enable_state = WBT_STATE_ON_DEFAULT;
768         wbt_update_limits(rwb);
769
770         /*
771          * Assign rwb and add the stats callback.
772          */
773         q->rq_wb = rwb;
774         blk_stat_add_callback(q, rwb->cb);
775
776         rwb->min_lat_nsec = wbt_default_latency_nsec(q);
777
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
784 void wbt_exit(struct request_queue *q)
785 {
786         struct rq_wb *rwb = q->rq_wb;
787
788         if (rwb) {
789                 blk_stat_remove_callback(q, rwb->cb);
790                 blk_stat_free_callback(rwb->cb);
791                 q->rq_wb = NULL;
792                 kfree(rwb);
793         }
794 }