blk-wbt.c 18 KB

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