blk-wbt.c 19 KB

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