blk-iolatency.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
  1. /*
  2. * Block rq-qos base io controller
  3. *
  4. * This works similar to wbt with a few exceptions
  5. *
  6. * - It's bio based, so the latency covers the whole block layer in addition to
  7. * the actual io.
  8. * - We will throttle all IO that comes in here if we need to.
  9. * - We use the mean latency over the 100ms window. This is because writes can
  10. * be particularly fast, which could give us a false sense of the impact of
  11. * other workloads on our protected workload.
  12. * - By default there's no throttling, we set the queue_depth to UINT_MAX so
  13. * that we can have as many outstanding bio's as we're allowed to. Only at
  14. * throttle time do we pay attention to the actual queue depth.
  15. *
  16. * The hierarchy works like the cpu controller does, we track the latency at
  17. * every configured node, and each configured node has it's own independent
  18. * queue depth. This means that we only care about our latency targets at the
  19. * peer level. Some group at the bottom of the hierarchy isn't going to affect
  20. * a group at the end of some other path if we're only configred at leaf level.
  21. *
  22. * Consider the following
  23. *
  24. * root blkg
  25. * / \
  26. * fast (target=5ms) slow (target=10ms)
  27. * / \ / \
  28. * a b normal(15ms) unloved
  29. *
  30. * "a" and "b" have no target, but their combined io under "fast" cannot exceed
  31. * an average latency of 5ms. If it does then we will throttle the "slow"
  32. * group. In the case of "normal", if it exceeds its 15ms target, we will
  33. * throttle "unloved", but nobody else.
  34. *
  35. * In this example "fast", "slow", and "normal" will be the only groups actually
  36. * accounting their io latencies. We have to walk up the heirarchy to the root
  37. * on every submit and complete so we can do the appropriate stat recording and
  38. * adjust the queue depth of ourselves if needed.
  39. *
  40. * There are 2 ways we throttle IO.
  41. *
  42. * 1) Queue depth throttling. As we throttle down we will adjust the maximum
  43. * number of IO's we're allowed to have in flight. This starts at (u64)-1 down
  44. * to 1. If the group is only ever submitting IO for itself then this is the
  45. * only way we throttle.
  46. *
  47. * 2) Induced delay throttling. This is for the case that a group is generating
  48. * IO that has to be issued by the root cg to avoid priority inversion. So think
  49. * REQ_META or REQ_SWAP. If we are already at qd == 1 and we're getting a lot
  50. * of work done for us on behalf of the root cg and are being asked to scale
  51. * down more then we induce a latency at userspace return. We accumulate the
  52. * total amount of time we need to be punished by doing
  53. *
  54. * total_time += min_lat_nsec - actual_io_completion
  55. *
  56. * and then at throttle time will do
  57. *
  58. * throttle_time = min(total_time, NSEC_PER_SEC)
  59. *
  60. * This induced delay will throttle back the activity that is generating the
  61. * root cg issued io's, wethere that's some metadata intensive operation or the
  62. * group is using so much memory that it is pushing us into swap.
  63. *
  64. * Copyright (C) 2018 Josef Bacik
  65. */
  66. #include <linux/kernel.h>
  67. #include <linux/blk_types.h>
  68. #include <linux/backing-dev.h>
  69. #include <linux/module.h>
  70. #include <linux/timer.h>
  71. #include <linux/memcontrol.h>
  72. #include <linux/sched/loadavg.h>
  73. #include <linux/sched/signal.h>
  74. #include <trace/events/block.h>
  75. #include <linux/blk-mq.h>
  76. #include "blk-rq-qos.h"
  77. #include "blk-stat.h"
  78. #include "blk.h"
  79. #define DEFAULT_SCALE_COOKIE 1000000U
  80. static struct blkcg_policy blkcg_policy_iolatency;
  81. struct iolatency_grp;
  82. struct blk_iolatency {
  83. struct rq_qos rqos;
  84. struct timer_list timer;
  85. atomic_t enabled;
  86. };
  87. static inline struct blk_iolatency *BLKIOLATENCY(struct rq_qos *rqos)
  88. {
  89. return container_of(rqos, struct blk_iolatency, rqos);
  90. }
  91. static inline bool blk_iolatency_enabled(struct blk_iolatency *blkiolat)
  92. {
  93. return atomic_read(&blkiolat->enabled) > 0;
  94. }
  95. struct child_latency_info {
  96. spinlock_t lock;
  97. /* Last time we adjusted the scale of everybody. */
  98. u64 last_scale_event;
  99. /* The latency that we missed. */
  100. u64 scale_lat;
  101. /* Total io's from all of our children for the last summation. */
  102. u64 nr_samples;
  103. /* The guy who actually changed the latency numbers. */
  104. struct iolatency_grp *scale_grp;
  105. /* Cookie to tell if we need to scale up or down. */
  106. atomic_t scale_cookie;
  107. };
  108. struct iolatency_grp {
  109. struct blkg_policy_data pd;
  110. struct blk_rq_stat __percpu *stats;
  111. struct blk_iolatency *blkiolat;
  112. struct rq_depth rq_depth;
  113. struct rq_wait rq_wait;
  114. atomic64_t window_start;
  115. atomic_t scale_cookie;
  116. u64 min_lat_nsec;
  117. u64 cur_win_nsec;
  118. /* total running average of our io latency. */
  119. u64 lat_avg;
  120. /* Our current number of IO's for the last summation. */
  121. u64 nr_samples;
  122. struct child_latency_info child_lat;
  123. };
  124. #define BLKIOLATENCY_MIN_WIN_SIZE (100 * NSEC_PER_MSEC)
  125. #define BLKIOLATENCY_MAX_WIN_SIZE NSEC_PER_SEC
  126. /*
  127. * These are the constants used to fake the fixed-point moving average
  128. * calculation just like load average. The call to CALC_LOAD folds
  129. * (FIXED_1 (2048) - exp_factor) * new_sample into lat_avg. The sampling
  130. * window size is bucketed to try to approximately calculate average
  131. * latency such that 1/exp (decay rate) is [1 min, 2.5 min) when windows
  132. * elapse immediately. Note, windows only elapse with IO activity. Idle
  133. * periods extend the most recent window.
  134. */
  135. #define BLKIOLATENCY_NR_EXP_FACTORS 5
  136. #define BLKIOLATENCY_EXP_BUCKET_SIZE (BLKIOLATENCY_MAX_WIN_SIZE / \
  137. (BLKIOLATENCY_NR_EXP_FACTORS - 1))
  138. static const u64 iolatency_exp_factors[BLKIOLATENCY_NR_EXP_FACTORS] = {
  139. 2045, // exp(1/600) - 600 samples
  140. 2039, // exp(1/240) - 240 samples
  141. 2031, // exp(1/120) - 120 samples
  142. 2023, // exp(1/80) - 80 samples
  143. 2014, // exp(1/60) - 60 samples
  144. };
  145. static inline struct iolatency_grp *pd_to_lat(struct blkg_policy_data *pd)
  146. {
  147. return pd ? container_of(pd, struct iolatency_grp, pd) : NULL;
  148. }
  149. static inline struct iolatency_grp *blkg_to_lat(struct blkcg_gq *blkg)
  150. {
  151. return pd_to_lat(blkg_to_pd(blkg, &blkcg_policy_iolatency));
  152. }
  153. static inline struct blkcg_gq *lat_to_blkg(struct iolatency_grp *iolat)
  154. {
  155. return pd_to_blkg(&iolat->pd);
  156. }
  157. static inline bool iolatency_may_queue(struct iolatency_grp *iolat,
  158. wait_queue_entry_t *wait,
  159. bool first_block)
  160. {
  161. struct rq_wait *rqw = &iolat->rq_wait;
  162. if (first_block && waitqueue_active(&rqw->wait) &&
  163. rqw->wait.head.next != &wait->entry)
  164. return false;
  165. return rq_wait_inc_below(rqw, iolat->rq_depth.max_depth);
  166. }
  167. static void __blkcg_iolatency_throttle(struct rq_qos *rqos,
  168. struct iolatency_grp *iolat,
  169. spinlock_t *lock, bool issue_as_root,
  170. bool use_memdelay)
  171. __releases(lock)
  172. __acquires(lock)
  173. {
  174. struct rq_wait *rqw = &iolat->rq_wait;
  175. unsigned use_delay = atomic_read(&lat_to_blkg(iolat)->use_delay);
  176. DEFINE_WAIT(wait);
  177. bool first_block = true;
  178. if (use_delay)
  179. blkcg_schedule_throttle(rqos->q, use_memdelay);
  180. /*
  181. * To avoid priority inversions we want to just take a slot if we are
  182. * issuing as root. If we're being killed off there's no point in
  183. * delaying things, we may have been killed by OOM so throttling may
  184. * make recovery take even longer, so just let the IO's through so the
  185. * task can go away.
  186. */
  187. if (issue_as_root || fatal_signal_pending(current)) {
  188. atomic_inc(&rqw->inflight);
  189. return;
  190. }
  191. if (iolatency_may_queue(iolat, &wait, first_block))
  192. return;
  193. do {
  194. prepare_to_wait_exclusive(&rqw->wait, &wait,
  195. TASK_UNINTERRUPTIBLE);
  196. if (iolatency_may_queue(iolat, &wait, first_block))
  197. break;
  198. first_block = false;
  199. if (lock) {
  200. spin_unlock_irq(lock);
  201. io_schedule();
  202. spin_lock_irq(lock);
  203. } else {
  204. io_schedule();
  205. }
  206. } while (1);
  207. finish_wait(&rqw->wait, &wait);
  208. }
  209. #define SCALE_DOWN_FACTOR 2
  210. #define SCALE_UP_FACTOR 4
  211. static inline unsigned long scale_amount(unsigned long qd, bool up)
  212. {
  213. return max(up ? qd >> SCALE_UP_FACTOR : qd >> SCALE_DOWN_FACTOR, 1UL);
  214. }
  215. /*
  216. * We scale the qd down faster than we scale up, so we need to use this helper
  217. * to adjust the scale_cookie accordingly so we don't prematurely get
  218. * scale_cookie at DEFAULT_SCALE_COOKIE and unthrottle too much.
  219. *
  220. * Each group has their own local copy of the last scale cookie they saw, so if
  221. * the global scale cookie goes up or down they know which way they need to go
  222. * based on their last knowledge of it.
  223. */
  224. static void scale_cookie_change(struct blk_iolatency *blkiolat,
  225. struct child_latency_info *lat_info,
  226. bool up)
  227. {
  228. unsigned long qd = blk_queue_depth(blkiolat->rqos.q);
  229. unsigned long scale = scale_amount(qd, up);
  230. unsigned long old = atomic_read(&lat_info->scale_cookie);
  231. unsigned long max_scale = qd << 1;
  232. unsigned long diff = 0;
  233. if (old < DEFAULT_SCALE_COOKIE)
  234. diff = DEFAULT_SCALE_COOKIE - old;
  235. if (up) {
  236. if (scale + old > DEFAULT_SCALE_COOKIE)
  237. atomic_set(&lat_info->scale_cookie,
  238. DEFAULT_SCALE_COOKIE);
  239. else if (diff > qd)
  240. atomic_inc(&lat_info->scale_cookie);
  241. else
  242. atomic_add(scale, &lat_info->scale_cookie);
  243. } else {
  244. /*
  245. * We don't want to dig a hole so deep that it takes us hours to
  246. * dig out of it. Just enough that we don't throttle/unthrottle
  247. * with jagged workloads but can still unthrottle once pressure
  248. * has sufficiently dissipated.
  249. */
  250. if (diff > qd) {
  251. if (diff < max_scale)
  252. atomic_dec(&lat_info->scale_cookie);
  253. } else {
  254. atomic_sub(scale, &lat_info->scale_cookie);
  255. }
  256. }
  257. }
  258. /*
  259. * Change the queue depth of the iolatency_grp. We add/subtract 1/16th of the
  260. * queue depth at a time so we don't get wild swings and hopefully dial in to
  261. * fairer distribution of the overall queue depth.
  262. */
  263. static void scale_change(struct iolatency_grp *iolat, bool up)
  264. {
  265. unsigned long qd = blk_queue_depth(iolat->blkiolat->rqos.q);
  266. unsigned long scale = scale_amount(qd, up);
  267. unsigned long old = iolat->rq_depth.max_depth;
  268. bool changed = false;
  269. if (old > qd)
  270. old = qd;
  271. if (up) {
  272. if (old == 1 && blkcg_unuse_delay(lat_to_blkg(iolat)))
  273. return;
  274. if (old < qd) {
  275. changed = true;
  276. old += scale;
  277. old = min(old, qd);
  278. iolat->rq_depth.max_depth = old;
  279. wake_up_all(&iolat->rq_wait.wait);
  280. }
  281. } else if (old > 1) {
  282. old >>= 1;
  283. changed = true;
  284. iolat->rq_depth.max_depth = max(old, 1UL);
  285. }
  286. }
  287. /* Check our parent and see if the scale cookie has changed. */
  288. static void check_scale_change(struct iolatency_grp *iolat)
  289. {
  290. struct iolatency_grp *parent;
  291. struct child_latency_info *lat_info;
  292. unsigned int cur_cookie;
  293. unsigned int our_cookie = atomic_read(&iolat->scale_cookie);
  294. u64 scale_lat;
  295. unsigned int old;
  296. int direction = 0;
  297. if (lat_to_blkg(iolat)->parent == NULL)
  298. return;
  299. parent = blkg_to_lat(lat_to_blkg(iolat)->parent);
  300. if (!parent)
  301. return;
  302. lat_info = &parent->child_lat;
  303. cur_cookie = atomic_read(&lat_info->scale_cookie);
  304. scale_lat = READ_ONCE(lat_info->scale_lat);
  305. if (cur_cookie < our_cookie)
  306. direction = -1;
  307. else if (cur_cookie > our_cookie)
  308. direction = 1;
  309. else
  310. return;
  311. old = atomic_cmpxchg(&iolat->scale_cookie, our_cookie, cur_cookie);
  312. /* Somebody beat us to the punch, just bail. */
  313. if (old != our_cookie)
  314. return;
  315. if (direction < 0 && iolat->min_lat_nsec) {
  316. u64 samples_thresh;
  317. if (!scale_lat || iolat->min_lat_nsec <= scale_lat)
  318. return;
  319. /*
  320. * Sometimes high priority groups are their own worst enemy, so
  321. * instead of taking it out on some poor other group that did 5%
  322. * or less of the IO's for the last summation just skip this
  323. * scale down event.
  324. */
  325. samples_thresh = lat_info->nr_samples * 5;
  326. samples_thresh = div64_u64(samples_thresh, 100);
  327. if (iolat->nr_samples <= samples_thresh)
  328. return;
  329. }
  330. /* We're as low as we can go. */
  331. if (iolat->rq_depth.max_depth == 1 && direction < 0) {
  332. blkcg_use_delay(lat_to_blkg(iolat));
  333. return;
  334. }
  335. /* We're back to the default cookie, unthrottle all the things. */
  336. if (cur_cookie == DEFAULT_SCALE_COOKIE) {
  337. blkcg_clear_delay(lat_to_blkg(iolat));
  338. iolat->rq_depth.max_depth = UINT_MAX;
  339. wake_up_all(&iolat->rq_wait.wait);
  340. return;
  341. }
  342. scale_change(iolat, direction > 0);
  343. }
  344. static void blkcg_iolatency_throttle(struct rq_qos *rqos, struct bio *bio,
  345. spinlock_t *lock)
  346. {
  347. struct blk_iolatency *blkiolat = BLKIOLATENCY(rqos);
  348. struct blkcg *blkcg;
  349. struct blkcg_gq *blkg;
  350. struct request_queue *q = rqos->q;
  351. bool issue_as_root = bio_issue_as_root_blkg(bio);
  352. if (!blk_iolatency_enabled(blkiolat))
  353. return;
  354. rcu_read_lock();
  355. blkcg = bio_blkcg(bio);
  356. bio_associate_blkcg(bio, &blkcg->css);
  357. blkg = blkg_lookup(blkcg, q);
  358. if (unlikely(!blkg)) {
  359. if (!lock)
  360. spin_lock_irq(q->queue_lock);
  361. blkg = blkg_lookup_create(blkcg, q);
  362. if (IS_ERR(blkg))
  363. blkg = NULL;
  364. if (!lock)
  365. spin_unlock_irq(q->queue_lock);
  366. }
  367. if (!blkg)
  368. goto out;
  369. bio_issue_init(&bio->bi_issue, bio_sectors(bio));
  370. bio_associate_blkg(bio, blkg);
  371. out:
  372. rcu_read_unlock();
  373. while (blkg && blkg->parent) {
  374. struct iolatency_grp *iolat = blkg_to_lat(blkg);
  375. if (!iolat) {
  376. blkg = blkg->parent;
  377. continue;
  378. }
  379. check_scale_change(iolat);
  380. __blkcg_iolatency_throttle(rqos, iolat, lock, issue_as_root,
  381. (bio->bi_opf & REQ_SWAP) == REQ_SWAP);
  382. blkg = blkg->parent;
  383. }
  384. if (!timer_pending(&blkiolat->timer))
  385. mod_timer(&blkiolat->timer, jiffies + HZ);
  386. }
  387. static void iolatency_record_time(struct iolatency_grp *iolat,
  388. struct bio_issue *issue, u64 now,
  389. bool issue_as_root)
  390. {
  391. struct blk_rq_stat *rq_stat;
  392. u64 start = bio_issue_time(issue);
  393. u64 req_time;
  394. /*
  395. * Have to do this so we are truncated to the correct time that our
  396. * issue is truncated to.
  397. */
  398. now = __bio_issue_time(now);
  399. if (now <= start)
  400. return;
  401. req_time = now - start;
  402. /*
  403. * We don't want to count issue_as_root bio's in the cgroups latency
  404. * statistics as it could skew the numbers downwards.
  405. */
  406. if (unlikely(issue_as_root && iolat->rq_depth.max_depth != UINT_MAX)) {
  407. u64 sub = iolat->min_lat_nsec;
  408. if (req_time < sub)
  409. blkcg_add_delay(lat_to_blkg(iolat), now, sub - req_time);
  410. return;
  411. }
  412. rq_stat = get_cpu_ptr(iolat->stats);
  413. blk_rq_stat_add(rq_stat, req_time);
  414. put_cpu_ptr(rq_stat);
  415. }
  416. #define BLKIOLATENCY_MIN_ADJUST_TIME (500 * NSEC_PER_MSEC)
  417. #define BLKIOLATENCY_MIN_GOOD_SAMPLES 5
  418. static void iolatency_check_latencies(struct iolatency_grp *iolat, u64 now)
  419. {
  420. struct blkcg_gq *blkg = lat_to_blkg(iolat);
  421. struct iolatency_grp *parent;
  422. struct child_latency_info *lat_info;
  423. struct blk_rq_stat stat;
  424. unsigned long flags;
  425. int cpu, exp_idx;
  426. blk_rq_stat_init(&stat);
  427. preempt_disable();
  428. for_each_online_cpu(cpu) {
  429. struct blk_rq_stat *s;
  430. s = per_cpu_ptr(iolat->stats, cpu);
  431. blk_rq_stat_sum(&stat, s);
  432. blk_rq_stat_init(s);
  433. }
  434. preempt_enable();
  435. parent = blkg_to_lat(blkg->parent);
  436. if (!parent)
  437. return;
  438. lat_info = &parent->child_lat;
  439. /*
  440. * CALC_LOAD takes in a number stored in fixed point representation.
  441. * Because we are using this for IO time in ns, the values stored
  442. * are significantly larger than the FIXED_1 denominator (2048).
  443. * Therefore, rounding errors in the calculation are negligible and
  444. * can be ignored.
  445. */
  446. exp_idx = min_t(int, BLKIOLATENCY_NR_EXP_FACTORS - 1,
  447. div64_u64(iolat->cur_win_nsec,
  448. BLKIOLATENCY_EXP_BUCKET_SIZE));
  449. CALC_LOAD(iolat->lat_avg, iolatency_exp_factors[exp_idx], stat.mean);
  450. /* Everything is ok and we don't need to adjust the scale. */
  451. if (stat.mean <= iolat->min_lat_nsec &&
  452. atomic_read(&lat_info->scale_cookie) == DEFAULT_SCALE_COOKIE)
  453. return;
  454. /* Somebody beat us to the punch, just bail. */
  455. spin_lock_irqsave(&lat_info->lock, flags);
  456. lat_info->nr_samples -= iolat->nr_samples;
  457. lat_info->nr_samples += stat.nr_samples;
  458. iolat->nr_samples = stat.nr_samples;
  459. if ((lat_info->last_scale_event >= now ||
  460. now - lat_info->last_scale_event < BLKIOLATENCY_MIN_ADJUST_TIME) &&
  461. lat_info->scale_lat <= iolat->min_lat_nsec)
  462. goto out;
  463. if (stat.mean <= iolat->min_lat_nsec &&
  464. stat.nr_samples >= BLKIOLATENCY_MIN_GOOD_SAMPLES) {
  465. if (lat_info->scale_grp == iolat) {
  466. lat_info->last_scale_event = now;
  467. scale_cookie_change(iolat->blkiolat, lat_info, true);
  468. }
  469. } else if (stat.mean > iolat->min_lat_nsec) {
  470. lat_info->last_scale_event = now;
  471. if (!lat_info->scale_grp ||
  472. lat_info->scale_lat > iolat->min_lat_nsec) {
  473. WRITE_ONCE(lat_info->scale_lat, iolat->min_lat_nsec);
  474. lat_info->scale_grp = iolat;
  475. }
  476. scale_cookie_change(iolat->blkiolat, lat_info, false);
  477. }
  478. out:
  479. spin_unlock_irqrestore(&lat_info->lock, flags);
  480. }
  481. static void blkcg_iolatency_done_bio(struct rq_qos *rqos, struct bio *bio)
  482. {
  483. struct blkcg_gq *blkg;
  484. struct rq_wait *rqw;
  485. struct iolatency_grp *iolat;
  486. u64 window_start;
  487. u64 now = ktime_to_ns(ktime_get());
  488. bool issue_as_root = bio_issue_as_root_blkg(bio);
  489. bool enabled = false;
  490. int inflight = 0;
  491. blkg = bio->bi_blkg;
  492. if (!blkg)
  493. return;
  494. iolat = blkg_to_lat(bio->bi_blkg);
  495. if (!iolat)
  496. return;
  497. enabled = blk_iolatency_enabled(iolat->blkiolat);
  498. if (!enabled)
  499. return;
  500. while (blkg && blkg->parent) {
  501. iolat = blkg_to_lat(blkg);
  502. if (!iolat) {
  503. blkg = blkg->parent;
  504. continue;
  505. }
  506. rqw = &iolat->rq_wait;
  507. inflight = atomic_dec_return(&rqw->inflight);
  508. WARN_ON_ONCE(inflight < 0);
  509. /*
  510. * If bi_status is BLK_STS_AGAIN, the bio wasn't actually
  511. * submitted, so do not account for it.
  512. */
  513. if (iolat->min_lat_nsec && bio->bi_status != BLK_STS_AGAIN) {
  514. iolatency_record_time(iolat, &bio->bi_issue, now,
  515. issue_as_root);
  516. window_start = atomic64_read(&iolat->window_start);
  517. if (now > window_start &&
  518. (now - window_start) >= iolat->cur_win_nsec) {
  519. if (atomic64_cmpxchg(&iolat->window_start,
  520. window_start, now) == window_start)
  521. iolatency_check_latencies(iolat, now);
  522. }
  523. }
  524. wake_up(&rqw->wait);
  525. blkg = blkg->parent;
  526. }
  527. }
  528. static void blkcg_iolatency_exit(struct rq_qos *rqos)
  529. {
  530. struct blk_iolatency *blkiolat = BLKIOLATENCY(rqos);
  531. del_timer_sync(&blkiolat->timer);
  532. blkcg_deactivate_policy(rqos->q, &blkcg_policy_iolatency);
  533. kfree(blkiolat);
  534. }
  535. static struct rq_qos_ops blkcg_iolatency_ops = {
  536. .throttle = blkcg_iolatency_throttle,
  537. .done_bio = blkcg_iolatency_done_bio,
  538. .exit = blkcg_iolatency_exit,
  539. };
  540. static void blkiolatency_timer_fn(struct timer_list *t)
  541. {
  542. struct blk_iolatency *blkiolat = from_timer(blkiolat, t, timer);
  543. struct blkcg_gq *blkg;
  544. struct cgroup_subsys_state *pos_css;
  545. u64 now = ktime_to_ns(ktime_get());
  546. rcu_read_lock();
  547. blkg_for_each_descendant_pre(blkg, pos_css,
  548. blkiolat->rqos.q->root_blkg) {
  549. struct iolatency_grp *iolat;
  550. struct child_latency_info *lat_info;
  551. unsigned long flags;
  552. u64 cookie;
  553. /*
  554. * We could be exiting, don't access the pd unless we have a
  555. * ref on the blkg.
  556. */
  557. if (!blkg_try_get(blkg))
  558. continue;
  559. iolat = blkg_to_lat(blkg);
  560. if (!iolat)
  561. goto next;
  562. lat_info = &iolat->child_lat;
  563. cookie = atomic_read(&lat_info->scale_cookie);
  564. if (cookie >= DEFAULT_SCALE_COOKIE)
  565. goto next;
  566. spin_lock_irqsave(&lat_info->lock, flags);
  567. if (lat_info->last_scale_event >= now)
  568. goto next_lock;
  569. /*
  570. * We scaled down but don't have a scale_grp, scale up and carry
  571. * on.
  572. */
  573. if (lat_info->scale_grp == NULL) {
  574. scale_cookie_change(iolat->blkiolat, lat_info, true);
  575. goto next_lock;
  576. }
  577. /*
  578. * It's been 5 seconds since our last scale event, clear the
  579. * scale grp in case the group that needed the scale down isn't
  580. * doing any IO currently.
  581. */
  582. if (now - lat_info->last_scale_event >=
  583. ((u64)NSEC_PER_SEC * 5))
  584. lat_info->scale_grp = NULL;
  585. next_lock:
  586. spin_unlock_irqrestore(&lat_info->lock, flags);
  587. next:
  588. blkg_put(blkg);
  589. }
  590. rcu_read_unlock();
  591. }
  592. int blk_iolatency_init(struct request_queue *q)
  593. {
  594. struct blk_iolatency *blkiolat;
  595. struct rq_qos *rqos;
  596. int ret;
  597. blkiolat = kzalloc(sizeof(*blkiolat), GFP_KERNEL);
  598. if (!blkiolat)
  599. return -ENOMEM;
  600. rqos = &blkiolat->rqos;
  601. rqos->id = RQ_QOS_CGROUP;
  602. rqos->ops = &blkcg_iolatency_ops;
  603. rqos->q = q;
  604. rq_qos_add(q, rqos);
  605. ret = blkcg_activate_policy(q, &blkcg_policy_iolatency);
  606. if (ret) {
  607. rq_qos_del(q, rqos);
  608. kfree(blkiolat);
  609. return ret;
  610. }
  611. timer_setup(&blkiolat->timer, blkiolatency_timer_fn, 0);
  612. return 0;
  613. }
  614. /*
  615. * return 1 for enabling iolatency, return -1 for disabling iolatency, otherwise
  616. * return 0.
  617. */
  618. static int iolatency_set_min_lat_nsec(struct blkcg_gq *blkg, u64 val)
  619. {
  620. struct iolatency_grp *iolat = blkg_to_lat(blkg);
  621. u64 oldval = iolat->min_lat_nsec;
  622. iolat->min_lat_nsec = val;
  623. iolat->cur_win_nsec = max_t(u64, val << 4, BLKIOLATENCY_MIN_WIN_SIZE);
  624. iolat->cur_win_nsec = min_t(u64, iolat->cur_win_nsec,
  625. BLKIOLATENCY_MAX_WIN_SIZE);
  626. if (!oldval && val)
  627. return 1;
  628. if (oldval && !val) {
  629. blkcg_clear_delay(blkg);
  630. return -1;
  631. }
  632. return 0;
  633. }
  634. static void iolatency_clear_scaling(struct blkcg_gq *blkg)
  635. {
  636. if (blkg->parent) {
  637. struct iolatency_grp *iolat = blkg_to_lat(blkg->parent);
  638. struct child_latency_info *lat_info;
  639. if (!iolat)
  640. return;
  641. lat_info = &iolat->child_lat;
  642. spin_lock(&lat_info->lock);
  643. atomic_set(&lat_info->scale_cookie, DEFAULT_SCALE_COOKIE);
  644. lat_info->last_scale_event = 0;
  645. lat_info->scale_grp = NULL;
  646. lat_info->scale_lat = 0;
  647. spin_unlock(&lat_info->lock);
  648. }
  649. }
  650. static ssize_t iolatency_set_limit(struct kernfs_open_file *of, char *buf,
  651. size_t nbytes, loff_t off)
  652. {
  653. struct blkcg *blkcg = css_to_blkcg(of_css(of));
  654. struct blkcg_gq *blkg;
  655. struct blk_iolatency *blkiolat;
  656. struct blkg_conf_ctx ctx;
  657. struct iolatency_grp *iolat;
  658. char *p, *tok;
  659. u64 lat_val = 0;
  660. u64 oldval;
  661. int ret;
  662. int enable = 0;
  663. ret = blkg_conf_prep(blkcg, &blkcg_policy_iolatency, buf, &ctx);
  664. if (ret)
  665. return ret;
  666. iolat = blkg_to_lat(ctx.blkg);
  667. blkiolat = iolat->blkiolat;
  668. p = ctx.body;
  669. ret = -EINVAL;
  670. while ((tok = strsep(&p, " "))) {
  671. char key[16];
  672. char val[21]; /* 18446744073709551616 */
  673. if (sscanf(tok, "%15[^=]=%20s", key, val) != 2)
  674. goto out;
  675. if (!strcmp(key, "target")) {
  676. u64 v;
  677. if (!strcmp(val, "max"))
  678. lat_val = 0;
  679. else if (sscanf(val, "%llu", &v) == 1)
  680. lat_val = v * NSEC_PER_USEC;
  681. else
  682. goto out;
  683. } else {
  684. goto out;
  685. }
  686. }
  687. /* Walk up the tree to see if our new val is lower than it should be. */
  688. blkg = ctx.blkg;
  689. oldval = iolat->min_lat_nsec;
  690. enable = iolatency_set_min_lat_nsec(blkg, lat_val);
  691. if (enable) {
  692. WARN_ON_ONCE(!blk_get_queue(blkg->q));
  693. blkg_get(blkg);
  694. }
  695. if (oldval != iolat->min_lat_nsec) {
  696. iolatency_clear_scaling(blkg);
  697. }
  698. ret = 0;
  699. out:
  700. blkg_conf_finish(&ctx);
  701. if (ret == 0 && enable) {
  702. struct iolatency_grp *tmp = blkg_to_lat(blkg);
  703. struct blk_iolatency *blkiolat = tmp->blkiolat;
  704. blk_mq_freeze_queue(blkg->q);
  705. if (enable == 1)
  706. atomic_inc(&blkiolat->enabled);
  707. else if (enable == -1)
  708. atomic_dec(&blkiolat->enabled);
  709. else
  710. WARN_ON_ONCE(1);
  711. blk_mq_unfreeze_queue(blkg->q);
  712. blkg_put(blkg);
  713. blk_put_queue(blkg->q);
  714. }
  715. return ret ?: nbytes;
  716. }
  717. static u64 iolatency_prfill_limit(struct seq_file *sf,
  718. struct blkg_policy_data *pd, int off)
  719. {
  720. struct iolatency_grp *iolat = pd_to_lat(pd);
  721. const char *dname = blkg_dev_name(pd->blkg);
  722. if (!dname || !iolat->min_lat_nsec)
  723. return 0;
  724. seq_printf(sf, "%s target=%llu\n",
  725. dname, div_u64(iolat->min_lat_nsec, NSEC_PER_USEC));
  726. return 0;
  727. }
  728. static int iolatency_print_limit(struct seq_file *sf, void *v)
  729. {
  730. blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
  731. iolatency_prfill_limit,
  732. &blkcg_policy_iolatency, seq_cft(sf)->private, false);
  733. return 0;
  734. }
  735. static size_t iolatency_pd_stat(struct blkg_policy_data *pd, char *buf,
  736. size_t size)
  737. {
  738. struct iolatency_grp *iolat = pd_to_lat(pd);
  739. unsigned long long avg_lat = div64_u64(iolat->lat_avg, NSEC_PER_USEC);
  740. unsigned long long cur_win = div64_u64(iolat->cur_win_nsec, NSEC_PER_MSEC);
  741. if (iolat->rq_depth.max_depth == UINT_MAX)
  742. return scnprintf(buf, size, " depth=max avg_lat=%llu win=%llu",
  743. avg_lat, cur_win);
  744. return scnprintf(buf, size, " depth=%u avg_lat=%llu win=%llu",
  745. iolat->rq_depth.max_depth, avg_lat, cur_win);
  746. }
  747. static struct blkg_policy_data *iolatency_pd_alloc(gfp_t gfp, int node)
  748. {
  749. struct iolatency_grp *iolat;
  750. iolat = kzalloc_node(sizeof(*iolat), gfp, node);
  751. if (!iolat)
  752. return NULL;
  753. iolat->stats = __alloc_percpu_gfp(sizeof(struct blk_rq_stat),
  754. __alignof__(struct blk_rq_stat), gfp);
  755. if (!iolat->stats) {
  756. kfree(iolat);
  757. return NULL;
  758. }
  759. return &iolat->pd;
  760. }
  761. static void iolatency_pd_init(struct blkg_policy_data *pd)
  762. {
  763. struct iolatency_grp *iolat = pd_to_lat(pd);
  764. struct blkcg_gq *blkg = lat_to_blkg(iolat);
  765. struct rq_qos *rqos = blkcg_rq_qos(blkg->q);
  766. struct blk_iolatency *blkiolat = BLKIOLATENCY(rqos);
  767. u64 now = ktime_to_ns(ktime_get());
  768. int cpu;
  769. for_each_possible_cpu(cpu) {
  770. struct blk_rq_stat *stat;
  771. stat = per_cpu_ptr(iolat->stats, cpu);
  772. blk_rq_stat_init(stat);
  773. }
  774. rq_wait_init(&iolat->rq_wait);
  775. spin_lock_init(&iolat->child_lat.lock);
  776. iolat->rq_depth.queue_depth = blk_queue_depth(blkg->q);
  777. iolat->rq_depth.max_depth = UINT_MAX;
  778. iolat->rq_depth.default_depth = iolat->rq_depth.queue_depth;
  779. iolat->blkiolat = blkiolat;
  780. iolat->cur_win_nsec = 100 * NSEC_PER_MSEC;
  781. atomic64_set(&iolat->window_start, now);
  782. /*
  783. * We init things in list order, so the pd for the parent may not be
  784. * init'ed yet for whatever reason.
  785. */
  786. if (blkg->parent && blkg_to_pd(blkg->parent, &blkcg_policy_iolatency)) {
  787. struct iolatency_grp *parent = blkg_to_lat(blkg->parent);
  788. atomic_set(&iolat->scale_cookie,
  789. atomic_read(&parent->child_lat.scale_cookie));
  790. } else {
  791. atomic_set(&iolat->scale_cookie, DEFAULT_SCALE_COOKIE);
  792. }
  793. atomic_set(&iolat->child_lat.scale_cookie, DEFAULT_SCALE_COOKIE);
  794. }
  795. static void iolatency_pd_offline(struct blkg_policy_data *pd)
  796. {
  797. struct iolatency_grp *iolat = pd_to_lat(pd);
  798. struct blkcg_gq *blkg = lat_to_blkg(iolat);
  799. struct blk_iolatency *blkiolat = iolat->blkiolat;
  800. int ret;
  801. ret = iolatency_set_min_lat_nsec(blkg, 0);
  802. if (ret == 1)
  803. atomic_inc(&blkiolat->enabled);
  804. if (ret == -1)
  805. atomic_dec(&blkiolat->enabled);
  806. iolatency_clear_scaling(blkg);
  807. }
  808. static void iolatency_pd_free(struct blkg_policy_data *pd)
  809. {
  810. struct iolatency_grp *iolat = pd_to_lat(pd);
  811. free_percpu(iolat->stats);
  812. kfree(iolat);
  813. }
  814. static struct cftype iolatency_files[] = {
  815. {
  816. .name = "latency",
  817. .flags = CFTYPE_NOT_ON_ROOT,
  818. .seq_show = iolatency_print_limit,
  819. .write = iolatency_set_limit,
  820. },
  821. {}
  822. };
  823. static struct blkcg_policy blkcg_policy_iolatency = {
  824. .dfl_cftypes = iolatency_files,
  825. .pd_alloc_fn = iolatency_pd_alloc,
  826. .pd_init_fn = iolatency_pd_init,
  827. .pd_offline_fn = iolatency_pd_offline,
  828. .pd_free_fn = iolatency_pd_free,
  829. .pd_stat_fn = iolatency_pd_stat,
  830. };
  831. static int __init iolatency_init(void)
  832. {
  833. return blkcg_policy_register(&blkcg_policy_iolatency);
  834. }
  835. static void __exit iolatency_exit(void)
  836. {
  837. return blkcg_policy_unregister(&blkcg_policy_iolatency);
  838. }
  839. module_init(iolatency_init);
  840. module_exit(iolatency_exit);