blk-wbt.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * buffered writeback throttling. loosely based on CoDel. We can't drop
  4. * packets for IO scheduling, so the logic is something like this:
  5. *
  6. * - Monitor latencies in a defined window of time.
  7. * - If the minimum latency in the above window exceeds some target, increment
  8. * scaling step and scale down queue depth by a factor of 2x. The monitoring
  9. * window is then shrunk to 100 / sqrt(scaling step + 1).
  10. * - For any window where we don't have solid data on what the latencies
  11. * look like, retain status quo.
  12. * - If latencies look good, decrement scaling step.
  13. * - If we're only doing writes, allow the scaling step to go negative. This
  14. * will temporarily boost write performance, snapping back to a stable
  15. * scaling step of 0 if reads show up or the heavy writers finish. Unlike
  16. * positive scaling steps where we shrink the monitoring window, a negative
  17. * scaling step retains the default step==0 window size.
  18. *
  19. * Copyright (C) 2016 Jens Axboe
  20. *
  21. */
  22. #include <linux/kernel.h>
  23. #include <linux/blk_types.h>
  24. #include <linux/slab.h>
  25. #include <linux/backing-dev.h>
  26. #include <linux/swap.h>
  27. #include "blk-stat.h"
  28. #include "blk-wbt.h"
  29. #include "blk-rq-qos.h"
  30. #include "elevator.h"
  31. #include "blk.h"
  32. #define CREATE_TRACE_POINTS
  33. #include <trace/events/wbt.h>
  34. enum wbt_flags {
  35. WBT_TRACKED = 1, /* write, tracked for throttling */
  36. WBT_READ = 2, /* read */
  37. WBT_SWAP = 4, /* write, from swap_writeout() */
  38. WBT_DISCARD = 8, /* discard */
  39. WBT_NR_BITS = 4, /* number of bits */
  40. };
  41. enum {
  42. WBT_RWQ_BG = 0,
  43. WBT_RWQ_SWAP,
  44. WBT_RWQ_DISCARD,
  45. WBT_NUM_RWQ,
  46. };
  47. /*
  48. * If current state is WBT_STATE_ON/OFF_DEFAULT, it can be covered to any other
  49. * state, if current state is WBT_STATE_ON/OFF_MANUAL, it can only be covered
  50. * to WBT_STATE_OFF/ON_MANUAL.
  51. */
  52. enum {
  53. WBT_STATE_ON_DEFAULT = 1, /* on by default */
  54. WBT_STATE_ON_MANUAL = 2, /* on manually by sysfs */
  55. WBT_STATE_OFF_DEFAULT = 3, /* off by default */
  56. WBT_STATE_OFF_MANUAL = 4, /* off manually by sysfs */
  57. };
  58. struct rq_wb {
  59. /*
  60. * Settings that govern how we throttle
  61. */
  62. unsigned int wb_background; /* background writeback */
  63. unsigned int wb_normal; /* normal writeback */
  64. short enable_state; /* WBT_STATE_* */
  65. /*
  66. * Number of consecutive periods where we don't have enough
  67. * information to make a firm scale up/down decision.
  68. */
  69. unsigned int unknown_cnt;
  70. u64 win_nsec; /* default window size */
  71. u64 cur_win_nsec; /* current window size */
  72. struct blk_stat_callback *cb;
  73. u64 sync_issue;
  74. void *sync_cookie;
  75. unsigned long last_issue; /* issue time of last read rq */
  76. unsigned long last_comp; /* completion time of last read rq */
  77. unsigned long min_lat_nsec;
  78. struct rq_qos rqos;
  79. struct rq_wait rq_wait[WBT_NUM_RWQ];
  80. struct rq_depth rq_depth;
  81. };
  82. static int wbt_init(struct gendisk *disk, struct rq_wb *rwb);
  83. static inline struct rq_wb *RQWB(struct rq_qos *rqos)
  84. {
  85. return container_of(rqos, struct rq_wb, rqos);
  86. }
  87. static inline void wbt_clear_state(struct request *rq)
  88. {
  89. rq->wbt_flags = 0;
  90. }
  91. static inline enum wbt_flags wbt_flags(struct request *rq)
  92. {
  93. return rq->wbt_flags;
  94. }
  95. static inline bool wbt_is_tracked(struct request *rq)
  96. {
  97. return rq->wbt_flags & WBT_TRACKED;
  98. }
  99. static inline bool wbt_is_read(struct request *rq)
  100. {
  101. return rq->wbt_flags & WBT_READ;
  102. }
  103. enum {
  104. /*
  105. * Default setting, we'll scale up (to 75% of QD max) or down (min 1)
  106. * from here depending on device stats
  107. */
  108. RWB_DEF_DEPTH = 16,
  109. /*
  110. * 100msec window
  111. */
  112. RWB_WINDOW_NSEC = 100 * 1000 * 1000ULL,
  113. /*
  114. * Disregard stats, if we don't meet this minimum
  115. */
  116. RWB_MIN_WRITE_SAMPLES = 3,
  117. /*
  118. * If we have this number of consecutive windows without enough
  119. * information to scale up or down, slowly return to center state
  120. * (step == 0).
  121. */
  122. RWB_UNKNOWN_BUMP = 5,
  123. };
  124. static inline bool rwb_enabled(struct rq_wb *rwb)
  125. {
  126. return rwb && rwb->enable_state != WBT_STATE_OFF_DEFAULT &&
  127. rwb->enable_state != WBT_STATE_OFF_MANUAL;
  128. }
  129. static void wb_timestamp(struct rq_wb *rwb, unsigned long *var)
  130. {
  131. if (rwb_enabled(rwb)) {
  132. const unsigned long cur = jiffies;
  133. if (cur != *var)
  134. *var = cur;
  135. }
  136. }
  137. /*
  138. * If a task was rate throttled in balance_dirty_pages() within the last
  139. * second or so, use that to indicate a higher cleaning rate.
  140. */
  141. static bool wb_recent_wait(struct rq_wb *rwb)
  142. {
  143. struct backing_dev_info *bdi = rwb->rqos.disk->bdi;
  144. return time_before(jiffies, bdi->last_bdp_sleep + HZ);
  145. }
  146. static inline struct rq_wait *get_rq_wait(struct rq_wb *rwb,
  147. enum wbt_flags wb_acct)
  148. {
  149. if (wb_acct & WBT_SWAP)
  150. return &rwb->rq_wait[WBT_RWQ_SWAP];
  151. else if (wb_acct & WBT_DISCARD)
  152. return &rwb->rq_wait[WBT_RWQ_DISCARD];
  153. return &rwb->rq_wait[WBT_RWQ_BG];
  154. }
  155. static void rwb_wake_all(struct rq_wb *rwb)
  156. {
  157. int i;
  158. for (i = 0; i < WBT_NUM_RWQ; i++) {
  159. struct rq_wait *rqw = &rwb->rq_wait[i];
  160. if (wq_has_sleeper(&rqw->wait))
  161. wake_up_all(&rqw->wait);
  162. }
  163. }
  164. static void wbt_rqw_done(struct rq_wb *rwb, struct rq_wait *rqw,
  165. enum wbt_flags wb_acct)
  166. {
  167. int inflight, limit;
  168. inflight = atomic_dec_return(&rqw->inflight);
  169. /*
  170. * For discards, our limit is always the background. For writes, if
  171. * the device does write back caching, drop further down before we
  172. * wake people up.
  173. */
  174. if (wb_acct & WBT_DISCARD)
  175. limit = rwb->wb_background;
  176. else if (blk_queue_write_cache(rwb->rqos.disk->queue) &&
  177. !wb_recent_wait(rwb))
  178. limit = 0;
  179. else
  180. limit = rwb->wb_normal;
  181. /*
  182. * Don't wake anyone up if we are above the normal limit.
  183. */
  184. if (inflight && inflight >= limit)
  185. return;
  186. if (wq_has_sleeper(&rqw->wait)) {
  187. int diff = limit - inflight;
  188. if (!inflight || diff >= rwb->wb_background / 2)
  189. wake_up_all(&rqw->wait);
  190. }
  191. }
  192. static void __wbt_done(struct rq_qos *rqos, enum wbt_flags wb_acct)
  193. {
  194. struct rq_wb *rwb = RQWB(rqos);
  195. struct rq_wait *rqw;
  196. if (!(wb_acct & WBT_TRACKED))
  197. return;
  198. rqw = get_rq_wait(rwb, wb_acct);
  199. wbt_rqw_done(rwb, rqw, wb_acct);
  200. }
  201. /*
  202. * Called on completion of a request. Note that it's also called when
  203. * a request is merged, when the request gets freed.
  204. */
  205. static void wbt_done(struct rq_qos *rqos, struct request *rq)
  206. {
  207. struct rq_wb *rwb = RQWB(rqos);
  208. if (!wbt_is_tracked(rq)) {
  209. if (wbt_is_read(rq)) {
  210. if (rwb->sync_cookie == rq) {
  211. rwb->sync_issue = 0;
  212. rwb->sync_cookie = NULL;
  213. }
  214. wb_timestamp(rwb, &rwb->last_comp);
  215. }
  216. } else {
  217. WARN_ON_ONCE(rq == rwb->sync_cookie);
  218. __wbt_done(rqos, wbt_flags(rq));
  219. }
  220. wbt_clear_state(rq);
  221. }
  222. static inline bool stat_sample_valid(struct blk_rq_stat *stat)
  223. {
  224. /*
  225. * We need at least one read sample, and a minimum of
  226. * RWB_MIN_WRITE_SAMPLES. We require some write samples to know
  227. * that it's writes impacting us, and not just some sole read on
  228. * a device that is in a lower power state.
  229. */
  230. return (stat[READ].nr_samples >= 1 &&
  231. stat[WRITE].nr_samples >= RWB_MIN_WRITE_SAMPLES);
  232. }
  233. static u64 rwb_sync_issue_lat(struct rq_wb *rwb)
  234. {
  235. u64 issue = READ_ONCE(rwb->sync_issue);
  236. if (!issue || !rwb->sync_cookie)
  237. return 0;
  238. return blk_time_get_ns() - issue;
  239. }
  240. static inline unsigned int wbt_inflight(struct rq_wb *rwb)
  241. {
  242. unsigned int i, ret = 0;
  243. for (i = 0; i < WBT_NUM_RWQ; i++)
  244. ret += atomic_read(&rwb->rq_wait[i].inflight);
  245. return ret;
  246. }
  247. enum {
  248. LAT_OK = 1,
  249. LAT_UNKNOWN,
  250. LAT_UNKNOWN_WRITES,
  251. LAT_EXCEEDED,
  252. };
  253. static int latency_exceeded(struct rq_wb *rwb, struct blk_rq_stat *stat)
  254. {
  255. struct backing_dev_info *bdi = rwb->rqos.disk->bdi;
  256. struct rq_depth *rqd = &rwb->rq_depth;
  257. u64 thislat;
  258. /*
  259. * If our stored sync issue exceeds the window size, or it
  260. * exceeds our min target AND we haven't logged any entries,
  261. * flag the latency as exceeded. wbt works off completion latencies,
  262. * but for a flooded device, a single sync IO can take a long time
  263. * to complete after being issued. If this time exceeds our
  264. * monitoring window AND we didn't see any other completions in that
  265. * window, then count that sync IO as a violation of the latency.
  266. */
  267. thislat = rwb_sync_issue_lat(rwb);
  268. if (thislat > rwb->cur_win_nsec ||
  269. (thislat > rwb->min_lat_nsec && !stat[READ].nr_samples)) {
  270. trace_wbt_lat(bdi, thislat);
  271. return LAT_EXCEEDED;
  272. }
  273. /*
  274. * No read/write mix, if stat isn't valid
  275. */
  276. if (!stat_sample_valid(stat)) {
  277. /*
  278. * If we had writes in this stat window and the window is
  279. * current, we're only doing writes. If a task recently
  280. * waited or still has writes in flights, consider us doing
  281. * just writes as well.
  282. */
  283. if (stat[WRITE].nr_samples || wb_recent_wait(rwb) ||
  284. wbt_inflight(rwb))
  285. return LAT_UNKNOWN_WRITES;
  286. return LAT_UNKNOWN;
  287. }
  288. /*
  289. * If the 'min' latency exceeds our target, step down.
  290. */
  291. if (stat[READ].min > rwb->min_lat_nsec) {
  292. trace_wbt_lat(bdi, stat[READ].min);
  293. trace_wbt_stat(bdi, stat);
  294. return LAT_EXCEEDED;
  295. }
  296. if (rqd->scale_step)
  297. trace_wbt_stat(bdi, stat);
  298. return LAT_OK;
  299. }
  300. static void rwb_trace_step(struct rq_wb *rwb, const char *msg)
  301. {
  302. struct backing_dev_info *bdi = rwb->rqos.disk->bdi;
  303. struct rq_depth *rqd = &rwb->rq_depth;
  304. trace_wbt_step(bdi, msg, rqd->scale_step, rwb->cur_win_nsec,
  305. rwb->wb_background, rwb->wb_normal, rqd->max_depth);
  306. }
  307. static void calc_wb_limits(struct rq_wb *rwb)
  308. {
  309. if (rwb->min_lat_nsec == 0) {
  310. rwb->wb_normal = rwb->wb_background = 0;
  311. } else if (rwb->rq_depth.max_depth <= 2) {
  312. rwb->wb_normal = rwb->rq_depth.max_depth;
  313. rwb->wb_background = 1;
  314. } else {
  315. rwb->wb_normal = (rwb->rq_depth.max_depth + 1) / 2;
  316. rwb->wb_background = (rwb->rq_depth.max_depth + 3) / 4;
  317. }
  318. }
  319. static void scale_up(struct rq_wb *rwb)
  320. {
  321. if (!rq_depth_scale_up(&rwb->rq_depth))
  322. return;
  323. calc_wb_limits(rwb);
  324. rwb->unknown_cnt = 0;
  325. rwb_wake_all(rwb);
  326. rwb_trace_step(rwb, tracepoint_string("scale up"));
  327. }
  328. static void scale_down(struct rq_wb *rwb, bool hard_throttle)
  329. {
  330. if (!rq_depth_scale_down(&rwb->rq_depth, hard_throttle))
  331. return;
  332. calc_wb_limits(rwb);
  333. rwb->unknown_cnt = 0;
  334. rwb_trace_step(rwb, tracepoint_string("scale down"));
  335. }
  336. static void rwb_arm_timer(struct rq_wb *rwb)
  337. {
  338. struct rq_depth *rqd = &rwb->rq_depth;
  339. if (rqd->scale_step > 0) {
  340. /*
  341. * We should speed this up, using some variant of a fast
  342. * integer inverse square root calculation. Since we only do
  343. * this for every window expiration, it's not a huge deal,
  344. * though.
  345. */
  346. rwb->cur_win_nsec = div_u64(rwb->win_nsec << 4,
  347. int_sqrt((rqd->scale_step + 1) << 8));
  348. } else {
  349. /*
  350. * For step < 0, we don't want to increase/decrease the
  351. * window size.
  352. */
  353. rwb->cur_win_nsec = rwb->win_nsec;
  354. }
  355. blk_stat_activate_nsecs(rwb->cb, rwb->cur_win_nsec);
  356. }
  357. static void wb_timer_fn(struct blk_stat_callback *cb)
  358. {
  359. struct rq_wb *rwb = cb->data;
  360. struct rq_depth *rqd = &rwb->rq_depth;
  361. unsigned int inflight = wbt_inflight(rwb);
  362. int status;
  363. if (!rwb->rqos.disk)
  364. return;
  365. status = latency_exceeded(rwb, cb->stat);
  366. trace_wbt_timer(rwb->rqos.disk->bdi, status, rqd->scale_step, inflight);
  367. /*
  368. * If we exceeded the latency target, step down. If we did not,
  369. * step one level up. If we don't know enough to say either exceeded
  370. * or ok, then don't do anything.
  371. */
  372. switch (status) {
  373. case LAT_EXCEEDED:
  374. scale_down(rwb, true);
  375. break;
  376. case LAT_OK:
  377. scale_up(rwb);
  378. break;
  379. case LAT_UNKNOWN_WRITES:
  380. /*
  381. * We don't have a valid read/write sample, but we do have
  382. * writes going on. Allow step to go negative, to increase
  383. * write performance.
  384. */
  385. scale_up(rwb);
  386. break;
  387. case LAT_UNKNOWN:
  388. if (++rwb->unknown_cnt < RWB_UNKNOWN_BUMP)
  389. break;
  390. /*
  391. * We get here when previously scaled reduced depth, and we
  392. * currently don't have a valid read/write sample. For that
  393. * case, slowly return to center state (step == 0).
  394. */
  395. if (rqd->scale_step > 0)
  396. scale_up(rwb);
  397. else if (rqd->scale_step < 0)
  398. scale_down(rwb, false);
  399. break;
  400. default:
  401. break;
  402. }
  403. /*
  404. * Re-arm timer, if we have IO in flight
  405. */
  406. if (rqd->scale_step || inflight)
  407. rwb_arm_timer(rwb);
  408. }
  409. static void wbt_update_limits(struct rq_wb *rwb)
  410. {
  411. struct rq_depth *rqd = &rwb->rq_depth;
  412. rqd->scale_step = 0;
  413. rqd->scaled_max = false;
  414. rq_depth_calc_max_depth(rqd);
  415. calc_wb_limits(rwb);
  416. rwb_wake_all(rwb);
  417. }
  418. bool wbt_disabled(struct request_queue *q)
  419. {
  420. struct rq_qos *rqos = wbt_rq_qos(q);
  421. return !rqos || !rwb_enabled(RQWB(rqos));
  422. }
  423. u64 wbt_get_min_lat(struct request_queue *q)
  424. {
  425. struct rq_qos *rqos = wbt_rq_qos(q);
  426. if (!rqos)
  427. return 0;
  428. return RQWB(rqos)->min_lat_nsec;
  429. }
  430. static void wbt_set_min_lat(struct request_queue *q, u64 val)
  431. {
  432. struct rq_qos *rqos = wbt_rq_qos(q);
  433. if (!rqos)
  434. return;
  435. RQWB(rqos)->min_lat_nsec = val;
  436. if (val)
  437. RQWB(rqos)->enable_state = WBT_STATE_ON_MANUAL;
  438. else
  439. RQWB(rqos)->enable_state = WBT_STATE_OFF_MANUAL;
  440. wbt_update_limits(RQWB(rqos));
  441. }
  442. static bool close_io(struct rq_wb *rwb)
  443. {
  444. const unsigned long now = jiffies;
  445. return time_before(now, rwb->last_issue + HZ / 10) ||
  446. time_before(now, rwb->last_comp + HZ / 10);
  447. }
  448. #define REQ_HIPRIO (REQ_SYNC | REQ_META | REQ_PRIO | REQ_SWAP)
  449. static inline unsigned int get_limit(struct rq_wb *rwb, blk_opf_t opf)
  450. {
  451. unsigned int limit;
  452. if ((opf & REQ_OP_MASK) == REQ_OP_DISCARD)
  453. return rwb->wb_background;
  454. /*
  455. * At this point we know it's a buffered write. If this is
  456. * swap trying to free memory, or REQ_SYNC is set, then
  457. * it's WB_SYNC_ALL writeback, and we'll use the max limit for
  458. * that. If the write is marked as a background write, then use
  459. * the idle limit, or go to normal if we haven't had competing
  460. * IO for a bit.
  461. */
  462. if ((opf & REQ_HIPRIO) || wb_recent_wait(rwb))
  463. limit = rwb->rq_depth.max_depth;
  464. else if ((opf & REQ_BACKGROUND) || close_io(rwb)) {
  465. /*
  466. * If less than 100ms since we completed unrelated IO,
  467. * limit us to half the depth for background writeback.
  468. */
  469. limit = rwb->wb_background;
  470. } else
  471. limit = rwb->wb_normal;
  472. return limit;
  473. }
  474. struct wbt_wait_data {
  475. struct rq_wb *rwb;
  476. enum wbt_flags wb_acct;
  477. blk_opf_t opf;
  478. };
  479. static bool wbt_inflight_cb(struct rq_wait *rqw, void *private_data)
  480. {
  481. struct wbt_wait_data *data = private_data;
  482. return rq_wait_inc_below(rqw, get_limit(data->rwb, data->opf));
  483. }
  484. static void wbt_cleanup_cb(struct rq_wait *rqw, void *private_data)
  485. {
  486. struct wbt_wait_data *data = private_data;
  487. wbt_rqw_done(data->rwb, rqw, data->wb_acct);
  488. }
  489. /*
  490. * Block if we will exceed our limit, or if we are currently waiting for
  491. * the timer to kick off queuing again.
  492. */
  493. static void __wbt_wait(struct rq_wb *rwb, enum wbt_flags wb_acct,
  494. blk_opf_t opf)
  495. {
  496. struct rq_wait *rqw = get_rq_wait(rwb, wb_acct);
  497. struct wbt_wait_data data = {
  498. .rwb = rwb,
  499. .wb_acct = wb_acct,
  500. .opf = opf,
  501. };
  502. rq_qos_wait(rqw, &data, wbt_inflight_cb, wbt_cleanup_cb);
  503. }
  504. static inline bool wbt_should_throttle(struct bio *bio)
  505. {
  506. switch (bio_op(bio)) {
  507. case REQ_OP_WRITE:
  508. /*
  509. * Don't throttle WRITE_ODIRECT
  510. */
  511. if ((bio->bi_opf & (REQ_SYNC | REQ_IDLE)) ==
  512. (REQ_SYNC | REQ_IDLE))
  513. return false;
  514. fallthrough;
  515. case REQ_OP_DISCARD:
  516. return true;
  517. default:
  518. return false;
  519. }
  520. }
  521. static enum wbt_flags bio_to_wbt_flags(struct rq_wb *rwb, struct bio *bio)
  522. {
  523. enum wbt_flags flags = 0;
  524. if (!rwb_enabled(rwb))
  525. return 0;
  526. if (bio_op(bio) == REQ_OP_READ) {
  527. flags = WBT_READ;
  528. } else if (wbt_should_throttle(bio)) {
  529. if (bio->bi_opf & REQ_SWAP)
  530. flags |= WBT_SWAP;
  531. if (bio_op(bio) == REQ_OP_DISCARD)
  532. flags |= WBT_DISCARD;
  533. flags |= WBT_TRACKED;
  534. }
  535. return flags;
  536. }
  537. static void wbt_cleanup(struct rq_qos *rqos, struct bio *bio)
  538. {
  539. struct rq_wb *rwb = RQWB(rqos);
  540. enum wbt_flags flags = bio_to_wbt_flags(rwb, bio);
  541. __wbt_done(rqos, flags);
  542. }
  543. /* May sleep, if we have exceeded the writeback limits. */
  544. static void wbt_wait(struct rq_qos *rqos, struct bio *bio)
  545. {
  546. struct rq_wb *rwb = RQWB(rqos);
  547. enum wbt_flags flags;
  548. flags = bio_to_wbt_flags(rwb, bio);
  549. if (!(flags & WBT_TRACKED)) {
  550. if (flags & WBT_READ)
  551. wb_timestamp(rwb, &rwb->last_issue);
  552. return;
  553. }
  554. __wbt_wait(rwb, flags, bio->bi_opf);
  555. if (!blk_stat_is_active(rwb->cb))
  556. rwb_arm_timer(rwb);
  557. }
  558. static void wbt_track(struct rq_qos *rqos, struct request *rq, struct bio *bio)
  559. {
  560. struct rq_wb *rwb = RQWB(rqos);
  561. rq->wbt_flags |= bio_to_wbt_flags(rwb, bio);
  562. }
  563. static void wbt_issue(struct rq_qos *rqos, struct request *rq)
  564. {
  565. struct rq_wb *rwb = RQWB(rqos);
  566. if (!rwb_enabled(rwb))
  567. return;
  568. /*
  569. * Track sync issue, in case it takes a long time to complete. Allows us
  570. * to react quicker, if a sync IO takes a long time to complete. Note
  571. * that this is just a hint. The request can go away when it completes,
  572. * so it's important we never dereference it. We only use the address to
  573. * compare with, which is why we store the sync_issue time locally.
  574. */
  575. if (wbt_is_read(rq) && !rwb->sync_issue) {
  576. rwb->sync_cookie = rq;
  577. rwb->sync_issue = rq->io_start_time_ns;
  578. }
  579. }
  580. static void wbt_requeue(struct rq_qos *rqos, struct request *rq)
  581. {
  582. struct rq_wb *rwb = RQWB(rqos);
  583. if (!rwb_enabled(rwb))
  584. return;
  585. if (rq == rwb->sync_cookie) {
  586. rwb->sync_issue = 0;
  587. rwb->sync_cookie = NULL;
  588. }
  589. }
  590. static int wbt_data_dir(const struct request *rq)
  591. {
  592. const enum req_op op = req_op(rq);
  593. if (op == REQ_OP_READ)
  594. return READ;
  595. else if (op_is_write(op))
  596. return WRITE;
  597. /* don't account */
  598. return -1;
  599. }
  600. static struct rq_wb *wbt_alloc(void)
  601. {
  602. struct rq_wb *rwb = kzalloc_obj(*rwb);
  603. if (!rwb)
  604. return NULL;
  605. rwb->cb = blk_stat_alloc_callback(wb_timer_fn, wbt_data_dir, 2, rwb);
  606. if (!rwb->cb) {
  607. kfree(rwb);
  608. return NULL;
  609. }
  610. return rwb;
  611. }
  612. static void wbt_free(struct rq_wb *rwb)
  613. {
  614. blk_stat_free_callback(rwb->cb);
  615. kfree(rwb);
  616. }
  617. /*
  618. * Enable wbt if defaults are configured that way
  619. */
  620. static bool __wbt_enable_default(struct gendisk *disk)
  621. {
  622. struct request_queue *q = disk->queue;
  623. struct rq_qos *rqos;
  624. bool enable = IS_ENABLED(CONFIG_BLK_WBT_MQ);
  625. mutex_lock(&disk->rqos_state_mutex);
  626. if (blk_queue_disable_wbt(q))
  627. enable = false;
  628. /* Throttling already enabled? */
  629. rqos = wbt_rq_qos(q);
  630. if (rqos) {
  631. if (enable && RQWB(rqos)->enable_state == WBT_STATE_OFF_DEFAULT)
  632. RQWB(rqos)->enable_state = WBT_STATE_ON_DEFAULT;
  633. mutex_unlock(&disk->rqos_state_mutex);
  634. return false;
  635. }
  636. mutex_unlock(&disk->rqos_state_mutex);
  637. /* Queue not registered? Maybe shutting down... */
  638. if (!blk_queue_registered(q))
  639. return false;
  640. if (queue_is_mq(q) && enable)
  641. return true;
  642. return false;
  643. }
  644. void wbt_enable_default(struct gendisk *disk)
  645. {
  646. __wbt_enable_default(disk);
  647. }
  648. EXPORT_SYMBOL_GPL(wbt_enable_default);
  649. void wbt_init_enable_default(struct gendisk *disk)
  650. {
  651. struct request_queue *q = disk->queue;
  652. struct rq_wb *rwb;
  653. unsigned int memflags;
  654. if (!__wbt_enable_default(disk))
  655. return;
  656. rwb = wbt_alloc();
  657. if (WARN_ON_ONCE(!rwb))
  658. return;
  659. if (WARN_ON_ONCE(wbt_init(disk, rwb))) {
  660. wbt_free(rwb);
  661. return;
  662. }
  663. memflags = blk_debugfs_lock(q);
  664. blk_mq_debugfs_register_rq_qos(q);
  665. blk_debugfs_unlock(q, memflags);
  666. }
  667. static u64 wbt_default_latency_nsec(struct request_queue *q)
  668. {
  669. /*
  670. * We default to 2msec for non-rotational storage, and 75msec
  671. * for rotational storage.
  672. */
  673. if (blk_queue_rot(q))
  674. return 75000000ULL;
  675. return 2000000ULL;
  676. }
  677. static void wbt_queue_depth_changed(struct rq_qos *rqos)
  678. {
  679. RQWB(rqos)->rq_depth.queue_depth = blk_queue_depth(rqos->disk->queue);
  680. wbt_update_limits(RQWB(rqos));
  681. }
  682. static void wbt_exit(struct rq_qos *rqos)
  683. {
  684. struct rq_wb *rwb = RQWB(rqos);
  685. blk_stat_remove_callback(rqos->disk->queue, rwb->cb);
  686. wbt_free(rwb);
  687. }
  688. /*
  689. * Disable wbt, if enabled by default.
  690. */
  691. void wbt_disable_default(struct gendisk *disk)
  692. {
  693. struct rq_qos *rqos = wbt_rq_qos(disk->queue);
  694. struct rq_wb *rwb;
  695. if (!rqos)
  696. return;
  697. mutex_lock(&disk->rqos_state_mutex);
  698. rwb = RQWB(rqos);
  699. if (rwb->enable_state == WBT_STATE_ON_DEFAULT) {
  700. blk_stat_deactivate(rwb->cb);
  701. rwb->enable_state = WBT_STATE_OFF_DEFAULT;
  702. }
  703. mutex_unlock(&disk->rqos_state_mutex);
  704. }
  705. EXPORT_SYMBOL_GPL(wbt_disable_default);
  706. #ifdef CONFIG_BLK_DEBUG_FS
  707. static int wbt_curr_win_nsec_show(void *data, struct seq_file *m)
  708. {
  709. struct rq_qos *rqos = data;
  710. struct rq_wb *rwb = RQWB(rqos);
  711. seq_printf(m, "%llu\n", rwb->cur_win_nsec);
  712. return 0;
  713. }
  714. static int wbt_enabled_show(void *data, struct seq_file *m)
  715. {
  716. struct rq_qos *rqos = data;
  717. struct rq_wb *rwb = RQWB(rqos);
  718. seq_printf(m, "%d\n", rwb->enable_state);
  719. return 0;
  720. }
  721. static int wbt_id_show(void *data, struct seq_file *m)
  722. {
  723. struct rq_qos *rqos = data;
  724. seq_printf(m, "%u\n", rqos->id);
  725. return 0;
  726. }
  727. static int wbt_inflight_show(void *data, struct seq_file *m)
  728. {
  729. struct rq_qos *rqos = data;
  730. struct rq_wb *rwb = RQWB(rqos);
  731. int i;
  732. for (i = 0; i < WBT_NUM_RWQ; i++)
  733. seq_printf(m, "%d: inflight %d\n", i,
  734. atomic_read(&rwb->rq_wait[i].inflight));
  735. return 0;
  736. }
  737. static int wbt_min_lat_nsec_show(void *data, struct seq_file *m)
  738. {
  739. struct rq_qos *rqos = data;
  740. struct rq_wb *rwb = RQWB(rqos);
  741. seq_printf(m, "%lu\n", rwb->min_lat_nsec);
  742. return 0;
  743. }
  744. static int wbt_unknown_cnt_show(void *data, struct seq_file *m)
  745. {
  746. struct rq_qos *rqos = data;
  747. struct rq_wb *rwb = RQWB(rqos);
  748. seq_printf(m, "%u\n", rwb->unknown_cnt);
  749. return 0;
  750. }
  751. static int wbt_normal_show(void *data, struct seq_file *m)
  752. {
  753. struct rq_qos *rqos = data;
  754. struct rq_wb *rwb = RQWB(rqos);
  755. seq_printf(m, "%u\n", rwb->wb_normal);
  756. return 0;
  757. }
  758. static int wbt_background_show(void *data, struct seq_file *m)
  759. {
  760. struct rq_qos *rqos = data;
  761. struct rq_wb *rwb = RQWB(rqos);
  762. seq_printf(m, "%u\n", rwb->wb_background);
  763. return 0;
  764. }
  765. static const struct blk_mq_debugfs_attr wbt_debugfs_attrs[] = {
  766. {"curr_win_nsec", 0400, wbt_curr_win_nsec_show},
  767. {"enabled", 0400, wbt_enabled_show},
  768. {"id", 0400, wbt_id_show},
  769. {"inflight", 0400, wbt_inflight_show},
  770. {"min_lat_nsec", 0400, wbt_min_lat_nsec_show},
  771. {"unknown_cnt", 0400, wbt_unknown_cnt_show},
  772. {"wb_normal", 0400, wbt_normal_show},
  773. {"wb_background", 0400, wbt_background_show},
  774. {},
  775. };
  776. #endif
  777. static const struct rq_qos_ops wbt_rqos_ops = {
  778. .throttle = wbt_wait,
  779. .issue = wbt_issue,
  780. .track = wbt_track,
  781. .requeue = wbt_requeue,
  782. .done = wbt_done,
  783. .cleanup = wbt_cleanup,
  784. .queue_depth_changed = wbt_queue_depth_changed,
  785. .exit = wbt_exit,
  786. #ifdef CONFIG_BLK_DEBUG_FS
  787. .debugfs_attrs = wbt_debugfs_attrs,
  788. #endif
  789. };
  790. static int wbt_init(struct gendisk *disk, struct rq_wb *rwb)
  791. {
  792. struct request_queue *q = disk->queue;
  793. int ret;
  794. int i;
  795. for (i = 0; i < WBT_NUM_RWQ; i++)
  796. rq_wait_init(&rwb->rq_wait[i]);
  797. rwb->last_comp = rwb->last_issue = jiffies;
  798. rwb->win_nsec = RWB_WINDOW_NSEC;
  799. rwb->enable_state = WBT_STATE_ON_DEFAULT;
  800. rwb->rq_depth.default_depth = RWB_DEF_DEPTH;
  801. rwb->min_lat_nsec = wbt_default_latency_nsec(q);
  802. rwb->rq_depth.queue_depth = blk_queue_depth(q);
  803. wbt_update_limits(rwb);
  804. /*
  805. * Assign rwb and add the stats callback.
  806. */
  807. mutex_lock(&q->rq_qos_mutex);
  808. ret = rq_qos_add(&rwb->rqos, disk, RQ_QOS_WBT, &wbt_rqos_ops);
  809. mutex_unlock(&q->rq_qos_mutex);
  810. if (ret)
  811. return ret;
  812. blk_stat_add_callback(q, rwb->cb);
  813. return 0;
  814. }
  815. int wbt_set_lat(struct gendisk *disk, s64 val)
  816. {
  817. struct request_queue *q = disk->queue;
  818. struct rq_qos *rqos = wbt_rq_qos(q);
  819. struct rq_wb *rwb = NULL;
  820. unsigned int memflags;
  821. int ret = 0;
  822. if (!rqos) {
  823. rwb = wbt_alloc();
  824. if (!rwb)
  825. return -ENOMEM;
  826. }
  827. /*
  828. * Ensure that the queue is idled, in case the latency update
  829. * ends up either enabling or disabling wbt completely. We can't
  830. * have IO inflight if that happens.
  831. */
  832. memflags = blk_mq_freeze_queue(q);
  833. if (!rqos) {
  834. ret = wbt_init(disk, rwb);
  835. if (ret) {
  836. wbt_free(rwb);
  837. goto out;
  838. }
  839. }
  840. if (val == -1)
  841. val = wbt_default_latency_nsec(q);
  842. else if (val >= 0)
  843. val *= 1000ULL;
  844. if (wbt_get_min_lat(q) == val)
  845. goto out;
  846. blk_mq_quiesce_queue(q);
  847. mutex_lock(&disk->rqos_state_mutex);
  848. wbt_set_min_lat(q, val);
  849. mutex_unlock(&disk->rqos_state_mutex);
  850. blk_mq_unquiesce_queue(q);
  851. out:
  852. blk_mq_unfreeze_queue(q, memflags);
  853. memflags = blk_debugfs_lock(q);
  854. blk_mq_debugfs_register_rq_qos(q);
  855. blk_debugfs_unlock(q, memflags);
  856. return ret;
  857. }