Back to home page

LXR

 
 

    


0001 /*
0002  * buffered writeback throttling. loosely based on CoDel. We can't drop
0003  * packets for IO scheduling, so the logic is something like this:
0004  *
0005  * - Monitor latencies in a defined window of time.
0006  * - If the minimum latency in the above window exceeds some target, increment
0007  *   scaling step and scale down queue depth by a factor of 2x. The monitoring
0008  *   window is then shrunk to 100 / sqrt(scaling step + 1).
0009  * - For any window where we don't have solid data on what the latencies
0010  *   look like, retain status quo.
0011  * - If latencies look good, decrement scaling step.
0012  * - If we're only doing writes, allow the scaling step to go negative. This
0013  *   will temporarily boost write performance, snapping back to a stable
0014  *   scaling step of 0 if reads show up or the heavy writers finish. Unlike
0015  *   positive scaling steps where we shrink the monitoring window, a negative
0016  *   scaling step retains the default step==0 window size.
0017  *
0018  * Copyright (C) 2016 Jens Axboe
0019  *
0020  */
0021 #include <linux/kernel.h>
0022 #include <linux/blk_types.h>
0023 #include <linux/slab.h>
0024 #include <linux/backing-dev.h>
0025 #include <linux/swap.h>
0026 
0027 #include "blk-wbt.h"
0028 
0029 #define CREATE_TRACE_POINTS
0030 #include <trace/events/wbt.h>
0031 
0032 enum {
0033     /*
0034      * Default setting, we'll scale up (to 75% of QD max) or down (min 1)
0035      * from here depending on device stats
0036      */
0037     RWB_DEF_DEPTH   = 16,
0038 
0039     /*
0040      * 100msec window
0041      */
0042     RWB_WINDOW_NSEC     = 100 * 1000 * 1000ULL,
0043 
0044     /*
0045      * Disregard stats, if we don't meet this minimum
0046      */
0047     RWB_MIN_WRITE_SAMPLES   = 3,
0048 
0049     /*
0050      * If we have this number of consecutive windows with not enough
0051      * information to scale up or down, scale up.
0052      */
0053     RWB_UNKNOWN_BUMP    = 5,
0054 };
0055 
0056 static inline bool rwb_enabled(struct rq_wb *rwb)
0057 {
0058     return rwb && rwb->wb_normal != 0;
0059 }
0060 
0061 /*
0062  * Increment 'v', if 'v' is below 'below'. Returns true if we succeeded,
0063  * false if 'v' + 1 would be bigger than 'below'.
0064  */
0065 static bool atomic_inc_below(atomic_t *v, int below)
0066 {
0067     int cur = atomic_read(v);
0068 
0069     for (;;) {
0070         int old;
0071 
0072         if (cur >= below)
0073             return false;
0074         old = atomic_cmpxchg(v, cur, cur + 1);
0075         if (old == cur)
0076             break;
0077         cur = old;
0078     }
0079 
0080     return true;
0081 }
0082 
0083 static void wb_timestamp(struct rq_wb *rwb, unsigned long *var)
0084 {
0085     if (rwb_enabled(rwb)) {
0086         const unsigned long cur = jiffies;
0087 
0088         if (cur != *var)
0089             *var = cur;
0090     }
0091 }
0092 
0093 /*
0094  * If a task was rate throttled in balance_dirty_pages() within the last
0095  * second or so, use that to indicate a higher cleaning rate.
0096  */
0097 static bool wb_recent_wait(struct rq_wb *rwb)
0098 {
0099     struct bdi_writeback *wb = &rwb->queue->backing_dev_info.wb;
0100 
0101     return time_before(jiffies, wb->dirty_sleep + HZ);
0102 }
0103 
0104 static inline struct rq_wait *get_rq_wait(struct rq_wb *rwb, bool is_kswapd)
0105 {
0106     return &rwb->rq_wait[is_kswapd];
0107 }
0108 
0109 static void rwb_wake_all(struct rq_wb *rwb)
0110 {
0111     int i;
0112 
0113     for (i = 0; i < WBT_NUM_RWQ; i++) {
0114         struct rq_wait *rqw = &rwb->rq_wait[i];
0115 
0116         if (waitqueue_active(&rqw->wait))
0117             wake_up_all(&rqw->wait);
0118     }
0119 }
0120 
0121 void __wbt_done(struct rq_wb *rwb, enum wbt_flags wb_acct)
0122 {
0123     struct rq_wait *rqw;
0124     int inflight, limit;
0125 
0126     if (!(wb_acct & WBT_TRACKED))
0127         return;
0128 
0129     rqw = get_rq_wait(rwb, wb_acct & WBT_KSWAPD);
0130     inflight = atomic_dec_return(&rqw->inflight);
0131 
0132     /*
0133      * wbt got disabled with IO in flight. Wake up any potential
0134      * waiters, we don't have to do more than that.
0135      */
0136     if (unlikely(!rwb_enabled(rwb))) {
0137         rwb_wake_all(rwb);
0138         return;
0139     }
0140 
0141     /*
0142      * If the device does write back caching, drop further down
0143      * before we wake people up.
0144      */
0145     if (rwb->wc && !wb_recent_wait(rwb))
0146         limit = 0;
0147     else
0148         limit = rwb->wb_normal;
0149 
0150     /*
0151      * Don't wake anyone up if we are above the normal limit.
0152      */
0153     if (inflight && inflight >= limit)
0154         return;
0155 
0156     if (waitqueue_active(&rqw->wait)) {
0157         int diff = limit - inflight;
0158 
0159         if (!inflight || diff >= rwb->wb_background / 2)
0160             wake_up_all(&rqw->wait);
0161     }
0162 }
0163 
0164 /*
0165  * Called on completion of a request. Note that it's also called when
0166  * a request is merged, when the request gets freed.
0167  */
0168 void wbt_done(struct rq_wb *rwb, struct blk_issue_stat *stat)
0169 {
0170     if (!rwb)
0171         return;
0172 
0173     if (!wbt_is_tracked(stat)) {
0174         if (rwb->sync_cookie == stat) {
0175             rwb->sync_issue = 0;
0176             rwb->sync_cookie = NULL;
0177         }
0178 
0179         if (wbt_is_read(stat))
0180             wb_timestamp(rwb, &rwb->last_comp);
0181         wbt_clear_state(stat);
0182     } else {
0183         WARN_ON_ONCE(stat == rwb->sync_cookie);
0184         __wbt_done(rwb, wbt_stat_to_mask(stat));
0185         wbt_clear_state(stat);
0186     }
0187 }
0188 
0189 /*
0190  * Return true, if we can't increase the depth further by scaling
0191  */
0192 static bool calc_wb_limits(struct rq_wb *rwb)
0193 {
0194     unsigned int depth;
0195     bool ret = false;
0196 
0197     if (!rwb->min_lat_nsec) {
0198         rwb->wb_max = rwb->wb_normal = rwb->wb_background = 0;
0199         return false;
0200     }
0201 
0202     /*
0203      * For QD=1 devices, this is a special case. It's important for those
0204      * to have one request ready when one completes, so force a depth of
0205      * 2 for those devices. On the backend, it'll be a depth of 1 anyway,
0206      * since the device can't have more than that in flight. If we're
0207      * scaling down, then keep a setting of 1/1/1.
0208      */
0209     if (rwb->queue_depth == 1) {
0210         if (rwb->scale_step > 0)
0211             rwb->wb_max = rwb->wb_normal = 1;
0212         else {
0213             rwb->wb_max = rwb->wb_normal = 2;
0214             ret = true;
0215         }
0216         rwb->wb_background = 1;
0217     } else {
0218         /*
0219          * scale_step == 0 is our default state. If we have suffered
0220          * latency spikes, step will be > 0, and we shrink the
0221          * allowed write depths. If step is < 0, we're only doing
0222          * writes, and we allow a temporarily higher depth to
0223          * increase performance.
0224          */
0225         depth = min_t(unsigned int, RWB_DEF_DEPTH, rwb->queue_depth);
0226         if (rwb->scale_step > 0)
0227             depth = 1 + ((depth - 1) >> min(31, rwb->scale_step));
0228         else if (rwb->scale_step < 0) {
0229             unsigned int maxd = 3 * rwb->queue_depth / 4;
0230 
0231             depth = 1 + ((depth - 1) << -rwb->scale_step);
0232             if (depth > maxd) {
0233                 depth = maxd;
0234                 ret = true;
0235             }
0236         }
0237 
0238         /*
0239          * Set our max/normal/bg queue depths based on how far
0240          * we have scaled down (->scale_step).
0241          */
0242         rwb->wb_max = depth;
0243         rwb->wb_normal = (rwb->wb_max + 1) / 2;
0244         rwb->wb_background = (rwb->wb_max + 3) / 4;
0245     }
0246 
0247     return ret;
0248 }
0249 
0250 static inline bool stat_sample_valid(struct blk_rq_stat *stat)
0251 {
0252     /*
0253      * We need at least one read sample, and a minimum of
0254      * RWB_MIN_WRITE_SAMPLES. We require some write samples to know
0255      * that it's writes impacting us, and not just some sole read on
0256      * a device that is in a lower power state.
0257      */
0258     return stat[BLK_STAT_READ].nr_samples >= 1 &&
0259         stat[BLK_STAT_WRITE].nr_samples >= RWB_MIN_WRITE_SAMPLES;
0260 }
0261 
0262 static u64 rwb_sync_issue_lat(struct rq_wb *rwb)
0263 {
0264     u64 now, issue = ACCESS_ONCE(rwb->sync_issue);
0265 
0266     if (!issue || !rwb->sync_cookie)
0267         return 0;
0268 
0269     now = ktime_to_ns(ktime_get());
0270     return now - issue;
0271 }
0272 
0273 enum {
0274     LAT_OK = 1,
0275     LAT_UNKNOWN,
0276     LAT_UNKNOWN_WRITES,
0277     LAT_EXCEEDED,
0278 };
0279 
0280 static int __latency_exceeded(struct rq_wb *rwb, struct blk_rq_stat *stat)
0281 {
0282     struct backing_dev_info *bdi = &rwb->queue->backing_dev_info;
0283     u64 thislat;
0284 
0285     /*
0286      * If our stored sync issue exceeds the window size, or it
0287      * exceeds our min target AND we haven't logged any entries,
0288      * flag the latency as exceeded. wbt works off completion latencies,
0289      * but for a flooded device, a single sync IO can take a long time
0290      * to complete after being issued. If this time exceeds our
0291      * monitoring window AND we didn't see any other completions in that
0292      * window, then count that sync IO as a violation of the latency.
0293      */
0294     thislat = rwb_sync_issue_lat(rwb);
0295     if (thislat > rwb->cur_win_nsec ||
0296         (thislat > rwb->min_lat_nsec && !stat[BLK_STAT_READ].nr_samples)) {
0297         trace_wbt_lat(bdi, thislat);
0298         return LAT_EXCEEDED;
0299     }
0300 
0301     /*
0302      * No read/write mix, if stat isn't valid
0303      */
0304     if (!stat_sample_valid(stat)) {
0305         /*
0306          * If we had writes in this stat window and the window is
0307          * current, we're only doing writes. If a task recently
0308          * waited or still has writes in flights, consider us doing
0309          * just writes as well.
0310          */
0311         if ((stat[BLK_STAT_WRITE].nr_samples && blk_stat_is_current(stat)) ||
0312             wb_recent_wait(rwb) || wbt_inflight(rwb))
0313             return LAT_UNKNOWN_WRITES;
0314         return LAT_UNKNOWN;
0315     }
0316 
0317     /*
0318      * If the 'min' latency exceeds our target, step down.
0319      */
0320     if (stat[BLK_STAT_READ].min > rwb->min_lat_nsec) {
0321         trace_wbt_lat(bdi, stat[BLK_STAT_READ].min);
0322         trace_wbt_stat(bdi, stat);
0323         return LAT_EXCEEDED;
0324     }
0325 
0326     if (rwb->scale_step)
0327         trace_wbt_stat(bdi, stat);
0328 
0329     return LAT_OK;
0330 }
0331 
0332 static int latency_exceeded(struct rq_wb *rwb)
0333 {
0334     struct blk_rq_stat stat[2];
0335 
0336     blk_queue_stat_get(rwb->queue, stat);
0337     return __latency_exceeded(rwb, stat);
0338 }
0339 
0340 static void rwb_trace_step(struct rq_wb *rwb, const char *msg)
0341 {
0342     struct backing_dev_info *bdi = &rwb->queue->backing_dev_info;
0343 
0344     trace_wbt_step(bdi, msg, rwb->scale_step, rwb->cur_win_nsec,
0345             rwb->wb_background, rwb->wb_normal, rwb->wb_max);
0346 }
0347 
0348 static void scale_up(struct rq_wb *rwb)
0349 {
0350     /*
0351      * Hit max in previous round, stop here
0352      */
0353     if (rwb->scaled_max)
0354         return;
0355 
0356     rwb->scale_step--;
0357     rwb->unknown_cnt = 0;
0358     blk_stat_clear(rwb->queue);
0359 
0360     rwb->scaled_max = calc_wb_limits(rwb);
0361 
0362     rwb_wake_all(rwb);
0363 
0364     rwb_trace_step(rwb, "step up");
0365 }
0366 
0367 /*
0368  * Scale rwb down. If 'hard_throttle' is set, do it quicker, since we
0369  * had a latency violation.
0370  */
0371 static void scale_down(struct rq_wb *rwb, bool hard_throttle)
0372 {
0373     /*
0374      * Stop scaling down when we've hit the limit. This also prevents
0375      * ->scale_step from going to crazy values, if the device can't
0376      * keep up.
0377      */
0378     if (rwb->wb_max == 1)
0379         return;
0380 
0381     if (rwb->scale_step < 0 && hard_throttle)
0382         rwb->scale_step = 0;
0383     else
0384         rwb->scale_step++;
0385 
0386     rwb->scaled_max = false;
0387     rwb->unknown_cnt = 0;
0388     blk_stat_clear(rwb->queue);
0389     calc_wb_limits(rwb);
0390     rwb_trace_step(rwb, "step down");
0391 }
0392 
0393 static void rwb_arm_timer(struct rq_wb *rwb)
0394 {
0395     unsigned long expires;
0396 
0397     if (rwb->scale_step > 0) {
0398         /*
0399          * We should speed this up, using some variant of a fast
0400          * integer inverse square root calculation. Since we only do
0401          * this for every window expiration, it's not a huge deal,
0402          * though.
0403          */
0404         rwb->cur_win_nsec = div_u64(rwb->win_nsec << 4,
0405                     int_sqrt((rwb->scale_step + 1) << 8));
0406     } else {
0407         /*
0408          * For step < 0, we don't want to increase/decrease the
0409          * window size.
0410          */
0411         rwb->cur_win_nsec = rwb->win_nsec;
0412     }
0413 
0414     expires = jiffies + nsecs_to_jiffies(rwb->cur_win_nsec);
0415     mod_timer(&rwb->window_timer, expires);
0416 }
0417 
0418 static void wb_timer_fn(unsigned long data)
0419 {
0420     struct rq_wb *rwb = (struct rq_wb *) data;
0421     unsigned int inflight = wbt_inflight(rwb);
0422     int status;
0423 
0424     status = latency_exceeded(rwb);
0425 
0426     trace_wbt_timer(&rwb->queue->backing_dev_info, status, rwb->scale_step,
0427             inflight);
0428 
0429     /*
0430      * If we exceeded the latency target, step down. If we did not,
0431      * step one level up. If we don't know enough to say either exceeded
0432      * or ok, then don't do anything.
0433      */
0434     switch (status) {
0435     case LAT_EXCEEDED:
0436         scale_down(rwb, true);
0437         break;
0438     case LAT_OK:
0439         scale_up(rwb);
0440         break;
0441     case LAT_UNKNOWN_WRITES:
0442         /*
0443          * We started a the center step, but don't have a valid
0444          * read/write sample, but we do have writes going on.
0445          * Allow step to go negative, to increase write perf.
0446          */
0447         scale_up(rwb);
0448         break;
0449     case LAT_UNKNOWN:
0450         if (++rwb->unknown_cnt < RWB_UNKNOWN_BUMP)
0451             break;
0452         /*
0453          * We get here when previously scaled reduced depth, and we
0454          * currently don't have a valid read/write sample. For that
0455          * case, slowly return to center state (step == 0).
0456          */
0457         if (rwb->scale_step > 0)
0458             scale_up(rwb);
0459         else if (rwb->scale_step < 0)
0460             scale_down(rwb, false);
0461         break;
0462     default:
0463         break;
0464     }
0465 
0466     /*
0467      * Re-arm timer, if we have IO in flight
0468      */
0469     if (rwb->scale_step || inflight)
0470         rwb_arm_timer(rwb);
0471 }
0472 
0473 void wbt_update_limits(struct rq_wb *rwb)
0474 {
0475     rwb->scale_step = 0;
0476     rwb->scaled_max = false;
0477     calc_wb_limits(rwb);
0478 
0479     rwb_wake_all(rwb);
0480 }
0481 
0482 static bool close_io(struct rq_wb *rwb)
0483 {
0484     const unsigned long now = jiffies;
0485 
0486     return time_before(now, rwb->last_issue + HZ / 10) ||
0487         time_before(now, rwb->last_comp + HZ / 10);
0488 }
0489 
0490 #define REQ_HIPRIO  (REQ_SYNC | REQ_META | REQ_PRIO)
0491 
0492 static inline unsigned int get_limit(struct rq_wb *rwb, unsigned long rw)
0493 {
0494     unsigned int limit;
0495 
0496     /*
0497      * At this point we know it's a buffered write. If this is
0498      * kswapd trying to free memory, or REQ_SYNC is set, set, then
0499      * it's WB_SYNC_ALL writeback, and we'll use the max limit for
0500      * that. If the write is marked as a background write, then use
0501      * the idle limit, or go to normal if we haven't had competing
0502      * IO for a bit.
0503      */
0504     if ((rw & REQ_HIPRIO) || wb_recent_wait(rwb) || current_is_kswapd())
0505         limit = rwb->wb_max;
0506     else if ((rw & REQ_BACKGROUND) || close_io(rwb)) {
0507         /*
0508          * If less than 100ms since we completed unrelated IO,
0509          * limit us to half the depth for background writeback.
0510          */
0511         limit = rwb->wb_background;
0512     } else
0513         limit = rwb->wb_normal;
0514 
0515     return limit;
0516 }
0517 
0518 static inline bool may_queue(struct rq_wb *rwb, struct rq_wait *rqw,
0519                  wait_queue_t *wait, unsigned long rw)
0520 {
0521     /*
0522      * inc it here even if disabled, since we'll dec it at completion.
0523      * this only happens if the task was sleeping in __wbt_wait(),
0524      * and someone turned it off at the same time.
0525      */
0526     if (!rwb_enabled(rwb)) {
0527         atomic_inc(&rqw->inflight);
0528         return true;
0529     }
0530 
0531     /*
0532      * If the waitqueue is already active and we are not the next
0533      * in line to be woken up, wait for our turn.
0534      */
0535     if (waitqueue_active(&rqw->wait) &&
0536         rqw->wait.task_list.next != &wait->task_list)
0537         return false;
0538 
0539     return atomic_inc_below(&rqw->inflight, get_limit(rwb, rw));
0540 }
0541 
0542 /*
0543  * Block if we will exceed our limit, or if we are currently waiting for
0544  * the timer to kick off queuing again.
0545  */
0546 static void __wbt_wait(struct rq_wb *rwb, unsigned long rw, spinlock_t *lock)
0547     __releases(lock)
0548     __acquires(lock)
0549 {
0550     struct rq_wait *rqw = get_rq_wait(rwb, current_is_kswapd());
0551     DEFINE_WAIT(wait);
0552 
0553     if (may_queue(rwb, rqw, &wait, rw))
0554         return;
0555 
0556     do {
0557         prepare_to_wait_exclusive(&rqw->wait, &wait,
0558                         TASK_UNINTERRUPTIBLE);
0559 
0560         if (may_queue(rwb, rqw, &wait, rw))
0561             break;
0562 
0563         if (lock) {
0564             spin_unlock_irq(lock);
0565             io_schedule();
0566             spin_lock_irq(lock);
0567         } else
0568             io_schedule();
0569     } while (1);
0570 
0571     finish_wait(&rqw->wait, &wait);
0572 }
0573 
0574 static inline bool wbt_should_throttle(struct rq_wb *rwb, struct bio *bio)
0575 {
0576     const int op = bio_op(bio);
0577 
0578     /*
0579      * If not a WRITE, do nothing
0580      */
0581     if (op != REQ_OP_WRITE)
0582         return false;
0583 
0584     /*
0585      * Don't throttle WRITE_ODIRECT
0586      */
0587     if ((bio->bi_opf & (REQ_SYNC | REQ_IDLE)) == (REQ_SYNC | REQ_IDLE))
0588         return false;
0589 
0590     return true;
0591 }
0592 
0593 /*
0594  * Returns true if the IO request should be accounted, false if not.
0595  * May sleep, if we have exceeded the writeback limits. Caller can pass
0596  * in an irq held spinlock, if it holds one when calling this function.
0597  * If we do sleep, we'll release and re-grab it.
0598  */
0599 enum wbt_flags wbt_wait(struct rq_wb *rwb, struct bio *bio, spinlock_t *lock)
0600 {
0601     unsigned int ret = 0;
0602 
0603     if (!rwb_enabled(rwb))
0604         return 0;
0605 
0606     if (bio_op(bio) == REQ_OP_READ)
0607         ret = WBT_READ;
0608 
0609     if (!wbt_should_throttle(rwb, bio)) {
0610         if (ret & WBT_READ)
0611             wb_timestamp(rwb, &rwb->last_issue);
0612         return ret;
0613     }
0614 
0615     __wbt_wait(rwb, bio->bi_opf, lock);
0616 
0617     if (!timer_pending(&rwb->window_timer))
0618         rwb_arm_timer(rwb);
0619 
0620     if (current_is_kswapd())
0621         ret |= WBT_KSWAPD;
0622 
0623     return ret | WBT_TRACKED;
0624 }
0625 
0626 void wbt_issue(struct rq_wb *rwb, struct blk_issue_stat *stat)
0627 {
0628     if (!rwb_enabled(rwb))
0629         return;
0630 
0631     /*
0632      * Track sync issue, in case it takes a long time to complete. Allows
0633      * us to react quicker, if a sync IO takes a long time to complete.
0634      * Note that this is just a hint. 'stat' can go away when the
0635      * request completes, so it's important we never dereference it. We
0636      * only use the address to compare with, which is why we store the
0637      * sync_issue time locally.
0638      */
0639     if (wbt_is_read(stat) && !rwb->sync_issue) {
0640         rwb->sync_cookie = stat;
0641         rwb->sync_issue = blk_stat_time(stat);
0642     }
0643 }
0644 
0645 void wbt_requeue(struct rq_wb *rwb, struct blk_issue_stat *stat)
0646 {
0647     if (!rwb_enabled(rwb))
0648         return;
0649     if (stat == rwb->sync_cookie) {
0650         rwb->sync_issue = 0;
0651         rwb->sync_cookie = NULL;
0652     }
0653 }
0654 
0655 void wbt_set_queue_depth(struct rq_wb *rwb, unsigned int depth)
0656 {
0657     if (rwb) {
0658         rwb->queue_depth = depth;
0659         wbt_update_limits(rwb);
0660     }
0661 }
0662 
0663 void wbt_set_write_cache(struct rq_wb *rwb, bool write_cache_on)
0664 {
0665     if (rwb)
0666         rwb->wc = write_cache_on;
0667 }
0668 
0669  /*
0670  * Disable wbt, if enabled by default. Only called from CFQ, if we have
0671  * cgroups enabled
0672  */
0673 void wbt_disable_default(struct request_queue *q)
0674 {
0675     struct rq_wb *rwb = q->rq_wb;
0676 
0677     if (rwb && rwb->enable_state == WBT_STATE_ON_DEFAULT) {
0678         del_timer_sync(&rwb->window_timer);
0679         rwb->win_nsec = rwb->min_lat_nsec = 0;
0680         wbt_update_limits(rwb);
0681     }
0682 }
0683 EXPORT_SYMBOL_GPL(wbt_disable_default);
0684 
0685 u64 wbt_default_latency_nsec(struct request_queue *q)
0686 {
0687     /*
0688      * We default to 2msec for non-rotational storage, and 75msec
0689      * for rotational storage.
0690      */
0691     if (blk_queue_nonrot(q))
0692         return 2000000ULL;
0693     else
0694         return 75000000ULL;
0695 }
0696 
0697 int wbt_init(struct request_queue *q)
0698 {
0699     struct rq_wb *rwb;
0700     int i;
0701 
0702     /*
0703      * For now, we depend on the stats window being larger than
0704      * our monitoring window. Ensure that this isn't inadvertently
0705      * violated.
0706      */
0707     BUILD_BUG_ON(RWB_WINDOW_NSEC > BLK_STAT_NSEC);
0708     BUILD_BUG_ON(WBT_NR_BITS > BLK_STAT_RES_BITS);
0709 
0710     rwb = kzalloc(sizeof(*rwb), GFP_KERNEL);
0711     if (!rwb)
0712         return -ENOMEM;
0713 
0714     for (i = 0; i < WBT_NUM_RWQ; i++) {
0715         atomic_set(&rwb->rq_wait[i].inflight, 0);
0716         init_waitqueue_head(&rwb->rq_wait[i].wait);
0717     }
0718 
0719     setup_timer(&rwb->window_timer, wb_timer_fn, (unsigned long) rwb);
0720     rwb->wc = 1;
0721     rwb->queue_depth = RWB_DEF_DEPTH;
0722     rwb->last_comp = rwb->last_issue = jiffies;
0723     rwb->queue = q;
0724     rwb->win_nsec = RWB_WINDOW_NSEC;
0725     rwb->enable_state = WBT_STATE_ON_DEFAULT;
0726     wbt_update_limits(rwb);
0727 
0728     /*
0729      * Assign rwb, and turn on stats tracking for this queue
0730      */
0731     q->rq_wb = rwb;
0732     blk_stat_enable(q);
0733 
0734     rwb->min_lat_nsec = wbt_default_latency_nsec(q);
0735 
0736     wbt_set_queue_depth(rwb, blk_queue_depth(q));
0737     wbt_set_write_cache(rwb, test_bit(QUEUE_FLAG_WC, &q->queue_flags));
0738 
0739     return 0;
0740 }
0741 
0742 void wbt_exit(struct request_queue *q)
0743 {
0744     struct rq_wb *rwb = q->rq_wb;
0745 
0746     if (rwb) {
0747         del_timer_sync(&rwb->window_timer);
0748         q->rq_wb = NULL;
0749         kfree(rwb);
0750     }
0751 }