blk-wbt.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  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[READ].nr_samples >= 1 &&
  222. 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[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[WRITE].nr_samples || wb_recent_wait(rwb) ||
  268. 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[READ].min > rwb->min_lat_nsec) {
  276. trace_wbt_lat(bdi, 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 void rwb_trace_step(struct rq_wb *rwb, const char *msg)
  285. {
  286. struct backing_dev_info *bdi = rwb->queue->backing_dev_info;
  287. trace_wbt_step(bdi, msg, rwb->scale_step, rwb->cur_win_nsec,
  288. rwb->wb_background, rwb->wb_normal, rwb->wb_max);
  289. }
  290. static void scale_up(struct rq_wb *rwb)
  291. {
  292. /*
  293. * Hit max in previous round, stop here
  294. */
  295. if (rwb->scaled_max)
  296. return;
  297. rwb->scale_step--;
  298. rwb->unknown_cnt = 0;
  299. rwb->scaled_max = calc_wb_limits(rwb);
  300. rwb_wake_all(rwb);
  301. rwb_trace_step(rwb, "step up");
  302. }
  303. /*
  304. * Scale rwb down. If 'hard_throttle' is set, do it quicker, since we
  305. * had a latency violation.
  306. */
  307. static void scale_down(struct rq_wb *rwb, bool hard_throttle)
  308. {
  309. /*
  310. * Stop scaling down when we've hit the limit. This also prevents
  311. * ->scale_step from going to crazy values, if the device can't
  312. * keep up.
  313. */
  314. if (rwb->wb_max == 1)
  315. return;
  316. if (rwb->scale_step < 0 && hard_throttle)
  317. rwb->scale_step = 0;
  318. else
  319. rwb->scale_step++;
  320. rwb->scaled_max = false;
  321. rwb->unknown_cnt = 0;
  322. calc_wb_limits(rwb);
  323. rwb_trace_step(rwb, "step down");
  324. }
  325. static void rwb_arm_timer(struct rq_wb *rwb)
  326. {
  327. if (rwb->scale_step > 0) {
  328. /*
  329. * We should speed this up, using some variant of a fast
  330. * integer inverse square root calculation. Since we only do
  331. * this for every window expiration, it's not a huge deal,
  332. * though.
  333. */
  334. rwb->cur_win_nsec = div_u64(rwb->win_nsec << 4,
  335. int_sqrt((rwb->scale_step + 1) << 8));
  336. } else {
  337. /*
  338. * For step < 0, we don't want to increase/decrease the
  339. * window size.
  340. */
  341. rwb->cur_win_nsec = rwb->win_nsec;
  342. }
  343. blk_stat_activate_nsecs(rwb->cb, rwb->cur_win_nsec);
  344. }
  345. static void wb_timer_fn(struct blk_stat_callback *cb)
  346. {
  347. struct rq_wb *rwb = cb->data;
  348. unsigned int inflight = wbt_inflight(rwb);
  349. int status;
  350. status = latency_exceeded(rwb, cb->stat);
  351. trace_wbt_timer(rwb->queue->backing_dev_info, status, rwb->scale_step,
  352. inflight);
  353. /*
  354. * If we exceeded the latency target, step down. If we did not,
  355. * step one level up. If we don't know enough to say either exceeded
  356. * or ok, then don't do anything.
  357. */
  358. switch (status) {
  359. case LAT_EXCEEDED:
  360. scale_down(rwb, true);
  361. break;
  362. case LAT_OK:
  363. scale_up(rwb);
  364. break;
  365. case LAT_UNKNOWN_WRITES:
  366. /*
  367. * We started a the center step, but don't have a valid
  368. * read/write sample, but we do have writes going on.
  369. * Allow step to go negative, to increase write perf.
  370. */
  371. scale_up(rwb);
  372. break;
  373. case LAT_UNKNOWN:
  374. if (++rwb->unknown_cnt < RWB_UNKNOWN_BUMP)
  375. break;
  376. /*
  377. * We get here when previously scaled reduced depth, and we
  378. * currently don't have a valid read/write sample. For that
  379. * case, slowly return to center state (step == 0).
  380. */
  381. if (rwb->scale_step > 0)
  382. scale_up(rwb);
  383. else if (rwb->scale_step < 0)
  384. scale_down(rwb, false);
  385. break;
  386. default:
  387. break;
  388. }
  389. /*
  390. * Re-arm timer, if we have IO in flight
  391. */
  392. if (rwb->scale_step || inflight)
  393. rwb_arm_timer(rwb);
  394. }
  395. void wbt_update_limits(struct rq_wb *rwb)
  396. {
  397. rwb->scale_step = 0;
  398. rwb->scaled_max = false;
  399. calc_wb_limits(rwb);
  400. rwb_wake_all(rwb);
  401. }
  402. static bool close_io(struct rq_wb *rwb)
  403. {
  404. const unsigned long now = jiffies;
  405. return time_before(now, rwb->last_issue + HZ / 10) ||
  406. time_before(now, rwb->last_comp + HZ / 10);
  407. }
  408. #define REQ_HIPRIO (REQ_SYNC | REQ_META | REQ_PRIO)
  409. static inline unsigned int get_limit(struct rq_wb *rwb, unsigned long rw)
  410. {
  411. unsigned int limit;
  412. /*
  413. * At this point we know it's a buffered write. If this is
  414. * kswapd trying to free memory, or REQ_SYNC is set, set, then
  415. * it's WB_SYNC_ALL writeback, and we'll use the max limit for
  416. * that. If the write is marked as a background write, then use
  417. * the idle limit, or go to normal if we haven't had competing
  418. * IO for a bit.
  419. */
  420. if ((rw & REQ_HIPRIO) || wb_recent_wait(rwb) || current_is_kswapd())
  421. limit = rwb->wb_max;
  422. else if ((rw & REQ_BACKGROUND) || close_io(rwb)) {
  423. /*
  424. * If less than 100ms since we completed unrelated IO,
  425. * limit us to half the depth for background writeback.
  426. */
  427. limit = rwb->wb_background;
  428. } else
  429. limit = rwb->wb_normal;
  430. return limit;
  431. }
  432. static inline bool may_queue(struct rq_wb *rwb, struct rq_wait *rqw,
  433. wait_queue_entry_t *wait, unsigned long rw)
  434. {
  435. /*
  436. * inc it here even if disabled, since we'll dec it at completion.
  437. * this only happens if the task was sleeping in __wbt_wait(),
  438. * and someone turned it off at the same time.
  439. */
  440. if (!rwb_enabled(rwb)) {
  441. atomic_inc(&rqw->inflight);
  442. return true;
  443. }
  444. /*
  445. * If the waitqueue is already active and we are not the next
  446. * in line to be woken up, wait for our turn.
  447. */
  448. if (waitqueue_active(&rqw->wait) &&
  449. rqw->wait.head.next != &wait->entry)
  450. return false;
  451. return atomic_inc_below(&rqw->inflight, get_limit(rwb, rw));
  452. }
  453. /*
  454. * Block if we will exceed our limit, or if we are currently waiting for
  455. * the timer to kick off queuing again.
  456. */
  457. static void __wbt_wait(struct rq_wb *rwb, unsigned long rw, spinlock_t *lock)
  458. __releases(lock)
  459. __acquires(lock)
  460. {
  461. struct rq_wait *rqw = get_rq_wait(rwb, current_is_kswapd());
  462. DEFINE_WAIT(wait);
  463. if (may_queue(rwb, rqw, &wait, rw))
  464. return;
  465. do {
  466. prepare_to_wait_exclusive(&rqw->wait, &wait,
  467. TASK_UNINTERRUPTIBLE);
  468. if (may_queue(rwb, rqw, &wait, rw))
  469. break;
  470. if (lock) {
  471. spin_unlock_irq(lock);
  472. io_schedule();
  473. spin_lock_irq(lock);
  474. } else
  475. io_schedule();
  476. } while (1);
  477. finish_wait(&rqw->wait, &wait);
  478. }
  479. static inline bool wbt_should_throttle(struct rq_wb *rwb, struct bio *bio)
  480. {
  481. const int op = bio_op(bio);
  482. /*
  483. * If not a WRITE, do nothing
  484. */
  485. if (op != REQ_OP_WRITE)
  486. return false;
  487. /*
  488. * Don't throttle WRITE_ODIRECT
  489. */
  490. if ((bio->bi_opf & (REQ_SYNC | REQ_IDLE)) == (REQ_SYNC | REQ_IDLE))
  491. return false;
  492. return true;
  493. }
  494. /*
  495. * Returns true if the IO request should be accounted, false if not.
  496. * May sleep, if we have exceeded the writeback limits. Caller can pass
  497. * in an irq held spinlock, if it holds one when calling this function.
  498. * If we do sleep, we'll release and re-grab it.
  499. */
  500. enum wbt_flags wbt_wait(struct rq_wb *rwb, struct bio *bio, spinlock_t *lock)
  501. {
  502. unsigned int ret = 0;
  503. if (!rwb_enabled(rwb))
  504. return 0;
  505. if (bio_op(bio) == REQ_OP_READ)
  506. ret = WBT_READ;
  507. if (!wbt_should_throttle(rwb, bio)) {
  508. if (ret & WBT_READ)
  509. wb_timestamp(rwb, &rwb->last_issue);
  510. return ret;
  511. }
  512. __wbt_wait(rwb, bio->bi_opf, lock);
  513. if (!blk_stat_is_active(rwb->cb))
  514. rwb_arm_timer(rwb);
  515. if (current_is_kswapd())
  516. ret |= WBT_KSWAPD;
  517. return ret | WBT_TRACKED;
  518. }
  519. void wbt_issue(struct rq_wb *rwb, struct blk_issue_stat *stat)
  520. {
  521. if (!rwb_enabled(rwb))
  522. return;
  523. /*
  524. * Track sync issue, in case it takes a long time to complete. Allows
  525. * us to react quicker, if a sync IO takes a long time to complete.
  526. * Note that this is just a hint. 'stat' can go away when the
  527. * request completes, so it's important we never dereference it. We
  528. * only use the address to compare with, which is why we store the
  529. * sync_issue time locally.
  530. */
  531. if (wbt_is_read(stat) && !rwb->sync_issue) {
  532. rwb->sync_cookie = stat;
  533. rwb->sync_issue = blk_stat_time(stat);
  534. }
  535. }
  536. void wbt_requeue(struct rq_wb *rwb, struct blk_issue_stat *stat)
  537. {
  538. if (!rwb_enabled(rwb))
  539. return;
  540. if (stat == rwb->sync_cookie) {
  541. rwb->sync_issue = 0;
  542. rwb->sync_cookie = NULL;
  543. }
  544. }
  545. void wbt_set_queue_depth(struct rq_wb *rwb, unsigned int depth)
  546. {
  547. if (rwb) {
  548. rwb->queue_depth = depth;
  549. wbt_update_limits(rwb);
  550. }
  551. }
  552. void wbt_set_write_cache(struct rq_wb *rwb, bool write_cache_on)
  553. {
  554. if (rwb)
  555. rwb->wc = write_cache_on;
  556. }
  557. /*
  558. * Disable wbt, if enabled by default.
  559. */
  560. void wbt_disable_default(struct request_queue *q)
  561. {
  562. struct rq_wb *rwb = q->rq_wb;
  563. if (rwb && rwb->enable_state == WBT_STATE_ON_DEFAULT)
  564. wbt_exit(q);
  565. }
  566. EXPORT_SYMBOL_GPL(wbt_disable_default);
  567. /*
  568. * Enable wbt if defaults are configured that way
  569. */
  570. void wbt_enable_default(struct request_queue *q)
  571. {
  572. /* Throttling already enabled? */
  573. if (q->rq_wb)
  574. return;
  575. /* Queue not registered? Maybe shutting down... */
  576. if (!test_bit(QUEUE_FLAG_REGISTERED, &q->queue_flags))
  577. return;
  578. if ((q->mq_ops && IS_ENABLED(CONFIG_BLK_WBT_MQ)) ||
  579. (q->request_fn && IS_ENABLED(CONFIG_BLK_WBT_SQ)))
  580. wbt_init(q);
  581. }
  582. EXPORT_SYMBOL_GPL(wbt_enable_default);
  583. u64 wbt_default_latency_nsec(struct request_queue *q)
  584. {
  585. /*
  586. * We default to 2msec for non-rotational storage, and 75msec
  587. * for rotational storage.
  588. */
  589. if (blk_queue_nonrot(q))
  590. return 2000000ULL;
  591. else
  592. return 75000000ULL;
  593. }
  594. static int wbt_data_dir(const struct request *rq)
  595. {
  596. const int op = req_op(rq);
  597. if (op == REQ_OP_READ)
  598. return READ;
  599. else if (op == REQ_OP_WRITE || op == REQ_OP_FLUSH)
  600. return WRITE;
  601. /* don't account */
  602. return -1;
  603. }
  604. int wbt_init(struct request_queue *q)
  605. {
  606. struct rq_wb *rwb;
  607. int i;
  608. BUILD_BUG_ON(WBT_NR_BITS > BLK_STAT_RES_BITS);
  609. rwb = kzalloc(sizeof(*rwb), GFP_KERNEL);
  610. if (!rwb)
  611. return -ENOMEM;
  612. rwb->cb = blk_stat_alloc_callback(wb_timer_fn, wbt_data_dir, 2, rwb);
  613. if (!rwb->cb) {
  614. kfree(rwb);
  615. return -ENOMEM;
  616. }
  617. for (i = 0; i < WBT_NUM_RWQ; i++) {
  618. atomic_set(&rwb->rq_wait[i].inflight, 0);
  619. init_waitqueue_head(&rwb->rq_wait[i].wait);
  620. }
  621. rwb->wc = 1;
  622. rwb->queue_depth = RWB_DEF_DEPTH;
  623. rwb->last_comp = rwb->last_issue = jiffies;
  624. rwb->queue = q;
  625. rwb->win_nsec = RWB_WINDOW_NSEC;
  626. rwb->enable_state = WBT_STATE_ON_DEFAULT;
  627. wbt_update_limits(rwb);
  628. /*
  629. * Assign rwb and add the stats callback.
  630. */
  631. q->rq_wb = rwb;
  632. blk_stat_add_callback(q, rwb->cb);
  633. rwb->min_lat_nsec = wbt_default_latency_nsec(q);
  634. wbt_set_queue_depth(rwb, blk_queue_depth(q));
  635. wbt_set_write_cache(rwb, test_bit(QUEUE_FLAG_WC, &q->queue_flags));
  636. return 0;
  637. }
  638. void wbt_exit(struct request_queue *q)
  639. {
  640. struct rq_wb *rwb = q->rq_wb;
  641. if (rwb) {
  642. blk_stat_remove_callback(q, rwb->cb);
  643. blk_stat_free_callback(rwb->cb);
  644. q->rq_wb = NULL;
  645. kfree(rwb);
  646. }
  647. }