kyber-iosched.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * The Kyber I/O scheduler. Controls latency by throttling queue depths using
  4. * scalable techniques.
  5. *
  6. * Copyright (C) 2017 Facebook
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/blkdev.h>
  10. #include <linux/module.h>
  11. #include <linux/sbitmap.h>
  12. #include <trace/events/block.h>
  13. #include "elevator.h"
  14. #include "blk.h"
  15. #include "blk-mq.h"
  16. #include "blk-mq-debugfs.h"
  17. #include "blk-mq-sched.h"
  18. #define CREATE_TRACE_POINTS
  19. #include <trace/events/kyber.h>
  20. /*
  21. * Scheduling domains: the device is divided into multiple domains based on the
  22. * request type.
  23. */
  24. enum {
  25. KYBER_READ,
  26. KYBER_WRITE,
  27. KYBER_DISCARD,
  28. KYBER_OTHER,
  29. KYBER_NUM_DOMAINS,
  30. };
  31. static const char *kyber_domain_names[] = {
  32. [KYBER_READ] = "READ",
  33. [KYBER_WRITE] = "WRITE",
  34. [KYBER_DISCARD] = "DISCARD",
  35. [KYBER_OTHER] = "OTHER",
  36. };
  37. enum {
  38. /*
  39. * In order to prevent starvation of synchronous requests by a flood of
  40. * asynchronous requests, we reserve 25% of requests for synchronous
  41. * operations.
  42. */
  43. KYBER_ASYNC_PERCENT = 75,
  44. };
  45. /*
  46. * Maximum device-wide depth for each scheduling domain.
  47. *
  48. * Even for fast devices with lots of tags like NVMe, you can saturate the
  49. * device with only a fraction of the maximum possible queue depth. So, we cap
  50. * these to a reasonable value.
  51. */
  52. static const unsigned int kyber_depth[] = {
  53. [KYBER_READ] = 256,
  54. [KYBER_WRITE] = 128,
  55. [KYBER_DISCARD] = 64,
  56. [KYBER_OTHER] = 16,
  57. };
  58. /*
  59. * Default latency targets for each scheduling domain.
  60. */
  61. static const u64 kyber_latency_targets[] = {
  62. [KYBER_READ] = 2ULL * NSEC_PER_MSEC,
  63. [KYBER_WRITE] = 10ULL * NSEC_PER_MSEC,
  64. [KYBER_DISCARD] = 5ULL * NSEC_PER_SEC,
  65. };
  66. /*
  67. * Batch size (number of requests we'll dispatch in a row) for each scheduling
  68. * domain.
  69. */
  70. static const unsigned int kyber_batch_size[] = {
  71. [KYBER_READ] = 16,
  72. [KYBER_WRITE] = 8,
  73. [KYBER_DISCARD] = 1,
  74. [KYBER_OTHER] = 1,
  75. };
  76. /*
  77. * Requests latencies are recorded in a histogram with buckets defined relative
  78. * to the target latency:
  79. *
  80. * <= 1/4 * target latency
  81. * <= 1/2 * target latency
  82. * <= 3/4 * target latency
  83. * <= target latency
  84. * <= 1 1/4 * target latency
  85. * <= 1 1/2 * target latency
  86. * <= 1 3/4 * target latency
  87. * > 1 3/4 * target latency
  88. */
  89. enum {
  90. /*
  91. * The width of the latency histogram buckets is
  92. * 1 / (1 << KYBER_LATENCY_SHIFT) * target latency.
  93. */
  94. KYBER_LATENCY_SHIFT = 2,
  95. /*
  96. * The first (1 << KYBER_LATENCY_SHIFT) buckets are <= target latency,
  97. * thus, "good".
  98. */
  99. KYBER_GOOD_BUCKETS = 1 << KYBER_LATENCY_SHIFT,
  100. /* There are also (1 << KYBER_LATENCY_SHIFT) "bad" buckets. */
  101. KYBER_LATENCY_BUCKETS = 2 << KYBER_LATENCY_SHIFT,
  102. };
  103. /*
  104. * We measure both the total latency and the I/O latency (i.e., latency after
  105. * submitting to the device).
  106. */
  107. enum {
  108. KYBER_TOTAL_LATENCY,
  109. KYBER_IO_LATENCY,
  110. };
  111. static const char *kyber_latency_type_names[] = {
  112. [KYBER_TOTAL_LATENCY] = "total",
  113. [KYBER_IO_LATENCY] = "I/O",
  114. };
  115. /*
  116. * Per-cpu latency histograms: total latency and I/O latency for each scheduling
  117. * domain except for KYBER_OTHER.
  118. */
  119. struct kyber_cpu_latency {
  120. atomic_t buckets[KYBER_OTHER][2][KYBER_LATENCY_BUCKETS];
  121. };
  122. /*
  123. * There is a same mapping between ctx & hctx and kcq & khd,
  124. * we use request->mq_ctx->index_hw to index the kcq in khd.
  125. */
  126. struct kyber_ctx_queue {
  127. /*
  128. * Used to ensure operations on rq_list and kcq_map to be an atmoic one.
  129. * Also protect the rqs on rq_list when merge.
  130. */
  131. spinlock_t lock;
  132. struct list_head rq_list[KYBER_NUM_DOMAINS];
  133. } ____cacheline_aligned_in_smp;
  134. struct kyber_queue_data {
  135. struct request_queue *q;
  136. dev_t dev;
  137. /*
  138. * Each scheduling domain has a limited number of in-flight requests
  139. * device-wide, limited by these tokens.
  140. */
  141. struct sbitmap_queue domain_tokens[KYBER_NUM_DOMAINS];
  142. /* Number of allowed async requests. */
  143. unsigned int async_depth;
  144. struct kyber_cpu_latency __percpu *cpu_latency;
  145. /* Timer for stats aggregation and adjusting domain tokens. */
  146. struct timer_list timer;
  147. unsigned int latency_buckets[KYBER_OTHER][2][KYBER_LATENCY_BUCKETS];
  148. unsigned long latency_timeout[KYBER_OTHER];
  149. int domain_p99[KYBER_OTHER];
  150. /* Target latencies in nanoseconds. */
  151. u64 latency_targets[KYBER_OTHER];
  152. };
  153. struct kyber_hctx_data {
  154. spinlock_t lock;
  155. struct list_head rqs[KYBER_NUM_DOMAINS];
  156. unsigned int cur_domain;
  157. unsigned int batching;
  158. struct kyber_ctx_queue *kcqs;
  159. struct sbitmap kcq_map[KYBER_NUM_DOMAINS];
  160. struct sbq_wait domain_wait[KYBER_NUM_DOMAINS];
  161. struct sbq_wait_state *domain_ws[KYBER_NUM_DOMAINS];
  162. atomic_t wait_index[KYBER_NUM_DOMAINS];
  163. };
  164. static int kyber_domain_wake(wait_queue_entry_t *wait, unsigned mode, int flags,
  165. void *key);
  166. static unsigned int kyber_sched_domain(blk_opf_t opf)
  167. {
  168. switch (opf & REQ_OP_MASK) {
  169. case REQ_OP_READ:
  170. return KYBER_READ;
  171. case REQ_OP_WRITE:
  172. return KYBER_WRITE;
  173. case REQ_OP_DISCARD:
  174. return KYBER_DISCARD;
  175. default:
  176. return KYBER_OTHER;
  177. }
  178. }
  179. static void flush_latency_buckets(struct kyber_queue_data *kqd,
  180. struct kyber_cpu_latency *cpu_latency,
  181. unsigned int sched_domain, unsigned int type)
  182. {
  183. unsigned int *buckets = kqd->latency_buckets[sched_domain][type];
  184. atomic_t *cpu_buckets = cpu_latency->buckets[sched_domain][type];
  185. unsigned int bucket;
  186. for (bucket = 0; bucket < KYBER_LATENCY_BUCKETS; bucket++)
  187. buckets[bucket] += atomic_xchg(&cpu_buckets[bucket], 0);
  188. }
  189. /*
  190. * Calculate the histogram bucket with the given percentile rank, or -1 if there
  191. * aren't enough samples yet.
  192. */
  193. static int calculate_percentile(struct kyber_queue_data *kqd,
  194. unsigned int sched_domain, unsigned int type,
  195. unsigned int percentile)
  196. {
  197. unsigned int *buckets = kqd->latency_buckets[sched_domain][type];
  198. unsigned int bucket, samples = 0, percentile_samples;
  199. for (bucket = 0; bucket < KYBER_LATENCY_BUCKETS; bucket++)
  200. samples += buckets[bucket];
  201. if (!samples)
  202. return -1;
  203. /*
  204. * We do the calculation once we have 500 samples or one second passes
  205. * since the first sample was recorded, whichever comes first.
  206. */
  207. if (!kqd->latency_timeout[sched_domain])
  208. kqd->latency_timeout[sched_domain] = max(jiffies + HZ, 1UL);
  209. if (samples < 500 &&
  210. time_is_after_jiffies(kqd->latency_timeout[sched_domain])) {
  211. return -1;
  212. }
  213. kqd->latency_timeout[sched_domain] = 0;
  214. percentile_samples = DIV_ROUND_UP(samples * percentile, 100);
  215. for (bucket = 0; bucket < KYBER_LATENCY_BUCKETS - 1; bucket++) {
  216. if (buckets[bucket] >= percentile_samples)
  217. break;
  218. percentile_samples -= buckets[bucket];
  219. }
  220. memset(buckets, 0, sizeof(kqd->latency_buckets[sched_domain][type]));
  221. trace_kyber_latency(kqd->dev, kyber_domain_names[sched_domain],
  222. kyber_latency_type_names[type], percentile,
  223. bucket + 1, 1 << KYBER_LATENCY_SHIFT, samples);
  224. return bucket;
  225. }
  226. static void kyber_resize_domain(struct kyber_queue_data *kqd,
  227. unsigned int sched_domain, unsigned int depth)
  228. {
  229. depth = clamp(depth, 1U, kyber_depth[sched_domain]);
  230. if (depth != kqd->domain_tokens[sched_domain].sb.depth) {
  231. sbitmap_queue_resize(&kqd->domain_tokens[sched_domain], depth);
  232. trace_kyber_adjust(kqd->dev, kyber_domain_names[sched_domain],
  233. depth);
  234. }
  235. }
  236. static void kyber_timer_fn(struct timer_list *t)
  237. {
  238. struct kyber_queue_data *kqd = from_timer(kqd, t, timer);
  239. unsigned int sched_domain;
  240. int cpu;
  241. bool bad = false;
  242. /* Sum all of the per-cpu latency histograms. */
  243. for_each_online_cpu(cpu) {
  244. struct kyber_cpu_latency *cpu_latency;
  245. cpu_latency = per_cpu_ptr(kqd->cpu_latency, cpu);
  246. for (sched_domain = 0; sched_domain < KYBER_OTHER; sched_domain++) {
  247. flush_latency_buckets(kqd, cpu_latency, sched_domain,
  248. KYBER_TOTAL_LATENCY);
  249. flush_latency_buckets(kqd, cpu_latency, sched_domain,
  250. KYBER_IO_LATENCY);
  251. }
  252. }
  253. /*
  254. * Check if any domains have a high I/O latency, which might indicate
  255. * congestion in the device. Note that we use the p90; we don't want to
  256. * be too sensitive to outliers here.
  257. */
  258. for (sched_domain = 0; sched_domain < KYBER_OTHER; sched_domain++) {
  259. int p90;
  260. p90 = calculate_percentile(kqd, sched_domain, KYBER_IO_LATENCY,
  261. 90);
  262. if (p90 >= KYBER_GOOD_BUCKETS)
  263. bad = true;
  264. }
  265. /*
  266. * Adjust the scheduling domain depths. If we determined that there was
  267. * congestion, we throttle all domains with good latencies. Either way,
  268. * we ease up on throttling domains with bad latencies.
  269. */
  270. for (sched_domain = 0; sched_domain < KYBER_OTHER; sched_domain++) {
  271. unsigned int orig_depth, depth;
  272. int p99;
  273. p99 = calculate_percentile(kqd, sched_domain,
  274. KYBER_TOTAL_LATENCY, 99);
  275. /*
  276. * This is kind of subtle: different domains will not
  277. * necessarily have enough samples to calculate the latency
  278. * percentiles during the same window, so we have to remember
  279. * the p99 for the next time we observe congestion; once we do,
  280. * we don't want to throttle again until we get more data, so we
  281. * reset it to -1.
  282. */
  283. if (bad) {
  284. if (p99 < 0)
  285. p99 = kqd->domain_p99[sched_domain];
  286. kqd->domain_p99[sched_domain] = -1;
  287. } else if (p99 >= 0) {
  288. kqd->domain_p99[sched_domain] = p99;
  289. }
  290. if (p99 < 0)
  291. continue;
  292. /*
  293. * If this domain has bad latency, throttle less. Otherwise,
  294. * throttle more iff we determined that there is congestion.
  295. *
  296. * The new depth is scaled linearly with the p99 latency vs the
  297. * latency target. E.g., if the p99 is 3/4 of the target, then
  298. * we throttle down to 3/4 of the current depth, and if the p99
  299. * is 2x the target, then we double the depth.
  300. */
  301. if (bad || p99 >= KYBER_GOOD_BUCKETS) {
  302. orig_depth = kqd->domain_tokens[sched_domain].sb.depth;
  303. depth = (orig_depth * (p99 + 1)) >> KYBER_LATENCY_SHIFT;
  304. kyber_resize_domain(kqd, sched_domain, depth);
  305. }
  306. }
  307. }
  308. static struct kyber_queue_data *kyber_queue_data_alloc(struct request_queue *q)
  309. {
  310. struct kyber_queue_data *kqd;
  311. int ret = -ENOMEM;
  312. int i;
  313. kqd = kzalloc_node(sizeof(*kqd), GFP_KERNEL, q->node);
  314. if (!kqd)
  315. goto err;
  316. kqd->q = q;
  317. kqd->dev = disk_devt(q->disk);
  318. kqd->cpu_latency = alloc_percpu_gfp(struct kyber_cpu_latency,
  319. GFP_KERNEL | __GFP_ZERO);
  320. if (!kqd->cpu_latency)
  321. goto err_kqd;
  322. timer_setup(&kqd->timer, kyber_timer_fn, 0);
  323. for (i = 0; i < KYBER_NUM_DOMAINS; i++) {
  324. WARN_ON(!kyber_depth[i]);
  325. WARN_ON(!kyber_batch_size[i]);
  326. ret = sbitmap_queue_init_node(&kqd->domain_tokens[i],
  327. kyber_depth[i], -1, false,
  328. GFP_KERNEL, q->node);
  329. if (ret) {
  330. while (--i >= 0)
  331. sbitmap_queue_free(&kqd->domain_tokens[i]);
  332. goto err_buckets;
  333. }
  334. }
  335. for (i = 0; i < KYBER_OTHER; i++) {
  336. kqd->domain_p99[i] = -1;
  337. kqd->latency_targets[i] = kyber_latency_targets[i];
  338. }
  339. return kqd;
  340. err_buckets:
  341. free_percpu(kqd->cpu_latency);
  342. err_kqd:
  343. kfree(kqd);
  344. err:
  345. return ERR_PTR(ret);
  346. }
  347. static int kyber_init_sched(struct request_queue *q, struct elevator_type *e)
  348. {
  349. struct kyber_queue_data *kqd;
  350. struct elevator_queue *eq;
  351. eq = elevator_alloc(q, e);
  352. if (!eq)
  353. return -ENOMEM;
  354. kqd = kyber_queue_data_alloc(q);
  355. if (IS_ERR(kqd)) {
  356. kobject_put(&eq->kobj);
  357. return PTR_ERR(kqd);
  358. }
  359. blk_stat_enable_accounting(q);
  360. blk_queue_flag_clear(QUEUE_FLAG_SQ_SCHED, q);
  361. eq->elevator_data = kqd;
  362. q->elevator = eq;
  363. return 0;
  364. }
  365. static void kyber_exit_sched(struct elevator_queue *e)
  366. {
  367. struct kyber_queue_data *kqd = e->elevator_data;
  368. int i;
  369. timer_shutdown_sync(&kqd->timer);
  370. blk_stat_disable_accounting(kqd->q);
  371. for (i = 0; i < KYBER_NUM_DOMAINS; i++)
  372. sbitmap_queue_free(&kqd->domain_tokens[i]);
  373. free_percpu(kqd->cpu_latency);
  374. kfree(kqd);
  375. }
  376. static void kyber_ctx_queue_init(struct kyber_ctx_queue *kcq)
  377. {
  378. unsigned int i;
  379. spin_lock_init(&kcq->lock);
  380. for (i = 0; i < KYBER_NUM_DOMAINS; i++)
  381. INIT_LIST_HEAD(&kcq->rq_list[i]);
  382. }
  383. static void kyber_depth_updated(struct blk_mq_hw_ctx *hctx)
  384. {
  385. struct kyber_queue_data *kqd = hctx->queue->elevator->elevator_data;
  386. struct blk_mq_tags *tags = hctx->sched_tags;
  387. kqd->async_depth = hctx->queue->nr_requests * KYBER_ASYNC_PERCENT / 100U;
  388. sbitmap_queue_min_shallow_depth(&tags->bitmap_tags, kqd->async_depth);
  389. }
  390. static int kyber_init_hctx(struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx)
  391. {
  392. struct kyber_hctx_data *khd;
  393. int i;
  394. khd = kmalloc_node(sizeof(*khd), GFP_KERNEL, hctx->numa_node);
  395. if (!khd)
  396. return -ENOMEM;
  397. khd->kcqs = kmalloc_array_node(hctx->nr_ctx,
  398. sizeof(struct kyber_ctx_queue),
  399. GFP_KERNEL, hctx->numa_node);
  400. if (!khd->kcqs)
  401. goto err_khd;
  402. for (i = 0; i < hctx->nr_ctx; i++)
  403. kyber_ctx_queue_init(&khd->kcqs[i]);
  404. for (i = 0; i < KYBER_NUM_DOMAINS; i++) {
  405. if (sbitmap_init_node(&khd->kcq_map[i], hctx->nr_ctx,
  406. ilog2(8), GFP_KERNEL, hctx->numa_node,
  407. false, false)) {
  408. while (--i >= 0)
  409. sbitmap_free(&khd->kcq_map[i]);
  410. goto err_kcqs;
  411. }
  412. }
  413. spin_lock_init(&khd->lock);
  414. for (i = 0; i < KYBER_NUM_DOMAINS; i++) {
  415. INIT_LIST_HEAD(&khd->rqs[i]);
  416. khd->domain_wait[i].sbq = NULL;
  417. init_waitqueue_func_entry(&khd->domain_wait[i].wait,
  418. kyber_domain_wake);
  419. khd->domain_wait[i].wait.private = hctx;
  420. INIT_LIST_HEAD(&khd->domain_wait[i].wait.entry);
  421. atomic_set(&khd->wait_index[i], 0);
  422. }
  423. khd->cur_domain = 0;
  424. khd->batching = 0;
  425. hctx->sched_data = khd;
  426. kyber_depth_updated(hctx);
  427. return 0;
  428. err_kcqs:
  429. kfree(khd->kcqs);
  430. err_khd:
  431. kfree(khd);
  432. return -ENOMEM;
  433. }
  434. static void kyber_exit_hctx(struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx)
  435. {
  436. struct kyber_hctx_data *khd = hctx->sched_data;
  437. int i;
  438. for (i = 0; i < KYBER_NUM_DOMAINS; i++)
  439. sbitmap_free(&khd->kcq_map[i]);
  440. kfree(khd->kcqs);
  441. kfree(hctx->sched_data);
  442. }
  443. static int rq_get_domain_token(struct request *rq)
  444. {
  445. return (long)rq->elv.priv[0];
  446. }
  447. static void rq_set_domain_token(struct request *rq, int token)
  448. {
  449. rq->elv.priv[0] = (void *)(long)token;
  450. }
  451. static void rq_clear_domain_token(struct kyber_queue_data *kqd,
  452. struct request *rq)
  453. {
  454. unsigned int sched_domain;
  455. int nr;
  456. nr = rq_get_domain_token(rq);
  457. if (nr != -1) {
  458. sched_domain = kyber_sched_domain(rq->cmd_flags);
  459. sbitmap_queue_clear(&kqd->domain_tokens[sched_domain], nr,
  460. rq->mq_ctx->cpu);
  461. }
  462. }
  463. static void kyber_limit_depth(blk_opf_t opf, struct blk_mq_alloc_data *data)
  464. {
  465. /*
  466. * We use the scheduler tags as per-hardware queue queueing tokens.
  467. * Async requests can be limited at this stage.
  468. */
  469. if (!op_is_sync(opf)) {
  470. struct kyber_queue_data *kqd = data->q->elevator->elevator_data;
  471. data->shallow_depth = kqd->async_depth;
  472. }
  473. }
  474. static bool kyber_bio_merge(struct request_queue *q, struct bio *bio,
  475. unsigned int nr_segs)
  476. {
  477. struct blk_mq_ctx *ctx = blk_mq_get_ctx(q);
  478. struct blk_mq_hw_ctx *hctx = blk_mq_map_queue(q, bio->bi_opf, ctx);
  479. struct kyber_hctx_data *khd = hctx->sched_data;
  480. struct kyber_ctx_queue *kcq = &khd->kcqs[ctx->index_hw[hctx->type]];
  481. unsigned int sched_domain = kyber_sched_domain(bio->bi_opf);
  482. struct list_head *rq_list = &kcq->rq_list[sched_domain];
  483. bool merged;
  484. spin_lock(&kcq->lock);
  485. merged = blk_bio_list_merge(hctx->queue, rq_list, bio, nr_segs);
  486. spin_unlock(&kcq->lock);
  487. return merged;
  488. }
  489. static void kyber_prepare_request(struct request *rq)
  490. {
  491. rq_set_domain_token(rq, -1);
  492. }
  493. static void kyber_insert_requests(struct blk_mq_hw_ctx *hctx,
  494. struct list_head *rq_list,
  495. blk_insert_t flags)
  496. {
  497. struct kyber_hctx_data *khd = hctx->sched_data;
  498. struct request *rq, *next;
  499. list_for_each_entry_safe(rq, next, rq_list, queuelist) {
  500. unsigned int sched_domain = kyber_sched_domain(rq->cmd_flags);
  501. struct kyber_ctx_queue *kcq = &khd->kcqs[rq->mq_ctx->index_hw[hctx->type]];
  502. struct list_head *head = &kcq->rq_list[sched_domain];
  503. spin_lock(&kcq->lock);
  504. trace_block_rq_insert(rq);
  505. if (flags & BLK_MQ_INSERT_AT_HEAD)
  506. list_move(&rq->queuelist, head);
  507. else
  508. list_move_tail(&rq->queuelist, head);
  509. sbitmap_set_bit(&khd->kcq_map[sched_domain],
  510. rq->mq_ctx->index_hw[hctx->type]);
  511. spin_unlock(&kcq->lock);
  512. }
  513. }
  514. static void kyber_finish_request(struct request *rq)
  515. {
  516. struct kyber_queue_data *kqd = rq->q->elevator->elevator_data;
  517. rq_clear_domain_token(kqd, rq);
  518. }
  519. static void add_latency_sample(struct kyber_cpu_latency *cpu_latency,
  520. unsigned int sched_domain, unsigned int type,
  521. u64 target, u64 latency)
  522. {
  523. unsigned int bucket;
  524. u64 divisor;
  525. if (latency > 0) {
  526. divisor = max_t(u64, target >> KYBER_LATENCY_SHIFT, 1);
  527. bucket = min_t(unsigned int, div64_u64(latency - 1, divisor),
  528. KYBER_LATENCY_BUCKETS - 1);
  529. } else {
  530. bucket = 0;
  531. }
  532. atomic_inc(&cpu_latency->buckets[sched_domain][type][bucket]);
  533. }
  534. static void kyber_completed_request(struct request *rq, u64 now)
  535. {
  536. struct kyber_queue_data *kqd = rq->q->elevator->elevator_data;
  537. struct kyber_cpu_latency *cpu_latency;
  538. unsigned int sched_domain;
  539. u64 target;
  540. sched_domain = kyber_sched_domain(rq->cmd_flags);
  541. if (sched_domain == KYBER_OTHER)
  542. return;
  543. cpu_latency = get_cpu_ptr(kqd->cpu_latency);
  544. target = kqd->latency_targets[sched_domain];
  545. add_latency_sample(cpu_latency, sched_domain, KYBER_TOTAL_LATENCY,
  546. target, now - rq->start_time_ns);
  547. add_latency_sample(cpu_latency, sched_domain, KYBER_IO_LATENCY, target,
  548. now - rq->io_start_time_ns);
  549. put_cpu_ptr(kqd->cpu_latency);
  550. timer_reduce(&kqd->timer, jiffies + HZ / 10);
  551. }
  552. struct flush_kcq_data {
  553. struct kyber_hctx_data *khd;
  554. unsigned int sched_domain;
  555. struct list_head *list;
  556. };
  557. static bool flush_busy_kcq(struct sbitmap *sb, unsigned int bitnr, void *data)
  558. {
  559. struct flush_kcq_data *flush_data = data;
  560. struct kyber_ctx_queue *kcq = &flush_data->khd->kcqs[bitnr];
  561. spin_lock(&kcq->lock);
  562. list_splice_tail_init(&kcq->rq_list[flush_data->sched_domain],
  563. flush_data->list);
  564. sbitmap_clear_bit(sb, bitnr);
  565. spin_unlock(&kcq->lock);
  566. return true;
  567. }
  568. static void kyber_flush_busy_kcqs(struct kyber_hctx_data *khd,
  569. unsigned int sched_domain,
  570. struct list_head *list)
  571. {
  572. struct flush_kcq_data data = {
  573. .khd = khd,
  574. .sched_domain = sched_domain,
  575. .list = list,
  576. };
  577. sbitmap_for_each_set(&khd->kcq_map[sched_domain],
  578. flush_busy_kcq, &data);
  579. }
  580. static int kyber_domain_wake(wait_queue_entry_t *wqe, unsigned mode, int flags,
  581. void *key)
  582. {
  583. struct blk_mq_hw_ctx *hctx = READ_ONCE(wqe->private);
  584. struct sbq_wait *wait = container_of(wqe, struct sbq_wait, wait);
  585. sbitmap_del_wait_queue(wait);
  586. blk_mq_run_hw_queue(hctx, true);
  587. return 1;
  588. }
  589. static int kyber_get_domain_token(struct kyber_queue_data *kqd,
  590. struct kyber_hctx_data *khd,
  591. struct blk_mq_hw_ctx *hctx)
  592. {
  593. unsigned int sched_domain = khd->cur_domain;
  594. struct sbitmap_queue *domain_tokens = &kqd->domain_tokens[sched_domain];
  595. struct sbq_wait *wait = &khd->domain_wait[sched_domain];
  596. struct sbq_wait_state *ws;
  597. int nr;
  598. nr = __sbitmap_queue_get(domain_tokens);
  599. /*
  600. * If we failed to get a domain token, make sure the hardware queue is
  601. * run when one becomes available. Note that this is serialized on
  602. * khd->lock, but we still need to be careful about the waker.
  603. */
  604. if (nr < 0 && list_empty_careful(&wait->wait.entry)) {
  605. ws = sbq_wait_ptr(domain_tokens,
  606. &khd->wait_index[sched_domain]);
  607. khd->domain_ws[sched_domain] = ws;
  608. sbitmap_add_wait_queue(domain_tokens, ws, wait);
  609. /*
  610. * Try again in case a token was freed before we got on the wait
  611. * queue.
  612. */
  613. nr = __sbitmap_queue_get(domain_tokens);
  614. }
  615. /*
  616. * If we got a token while we were on the wait queue, remove ourselves
  617. * from the wait queue to ensure that all wake ups make forward
  618. * progress. It's possible that the waker already deleted the entry
  619. * between the !list_empty_careful() check and us grabbing the lock, but
  620. * list_del_init() is okay with that.
  621. */
  622. if (nr >= 0 && !list_empty_careful(&wait->wait.entry)) {
  623. ws = khd->domain_ws[sched_domain];
  624. spin_lock_irq(&ws->wait.lock);
  625. sbitmap_del_wait_queue(wait);
  626. spin_unlock_irq(&ws->wait.lock);
  627. }
  628. return nr;
  629. }
  630. static struct request *
  631. kyber_dispatch_cur_domain(struct kyber_queue_data *kqd,
  632. struct kyber_hctx_data *khd,
  633. struct blk_mq_hw_ctx *hctx)
  634. {
  635. struct list_head *rqs;
  636. struct request *rq;
  637. int nr;
  638. rqs = &khd->rqs[khd->cur_domain];
  639. /*
  640. * If we already have a flushed request, then we just need to get a
  641. * token for it. Otherwise, if there are pending requests in the kcqs,
  642. * flush the kcqs, but only if we can get a token. If not, we should
  643. * leave the requests in the kcqs so that they can be merged. Note that
  644. * khd->lock serializes the flushes, so if we observed any bit set in
  645. * the kcq_map, we will always get a request.
  646. */
  647. rq = list_first_entry_or_null(rqs, struct request, queuelist);
  648. if (rq) {
  649. nr = kyber_get_domain_token(kqd, khd, hctx);
  650. if (nr >= 0) {
  651. khd->batching++;
  652. rq_set_domain_token(rq, nr);
  653. list_del_init(&rq->queuelist);
  654. return rq;
  655. } else {
  656. trace_kyber_throttled(kqd->dev,
  657. kyber_domain_names[khd->cur_domain]);
  658. }
  659. } else if (sbitmap_any_bit_set(&khd->kcq_map[khd->cur_domain])) {
  660. nr = kyber_get_domain_token(kqd, khd, hctx);
  661. if (nr >= 0) {
  662. kyber_flush_busy_kcqs(khd, khd->cur_domain, rqs);
  663. rq = list_first_entry(rqs, struct request, queuelist);
  664. khd->batching++;
  665. rq_set_domain_token(rq, nr);
  666. list_del_init(&rq->queuelist);
  667. return rq;
  668. } else {
  669. trace_kyber_throttled(kqd->dev,
  670. kyber_domain_names[khd->cur_domain]);
  671. }
  672. }
  673. /* There were either no pending requests or no tokens. */
  674. return NULL;
  675. }
  676. static struct request *kyber_dispatch_request(struct blk_mq_hw_ctx *hctx)
  677. {
  678. struct kyber_queue_data *kqd = hctx->queue->elevator->elevator_data;
  679. struct kyber_hctx_data *khd = hctx->sched_data;
  680. struct request *rq;
  681. int i;
  682. spin_lock(&khd->lock);
  683. /*
  684. * First, if we are still entitled to batch, try to dispatch a request
  685. * from the batch.
  686. */
  687. if (khd->batching < kyber_batch_size[khd->cur_domain]) {
  688. rq = kyber_dispatch_cur_domain(kqd, khd, hctx);
  689. if (rq)
  690. goto out;
  691. }
  692. /*
  693. * Either,
  694. * 1. We were no longer entitled to a batch.
  695. * 2. The domain we were batching didn't have any requests.
  696. * 3. The domain we were batching was out of tokens.
  697. *
  698. * Start another batch. Note that this wraps back around to the original
  699. * domain if no other domains have requests or tokens.
  700. */
  701. khd->batching = 0;
  702. for (i = 0; i < KYBER_NUM_DOMAINS; i++) {
  703. if (khd->cur_domain == KYBER_NUM_DOMAINS - 1)
  704. khd->cur_domain = 0;
  705. else
  706. khd->cur_domain++;
  707. rq = kyber_dispatch_cur_domain(kqd, khd, hctx);
  708. if (rq)
  709. goto out;
  710. }
  711. rq = NULL;
  712. out:
  713. spin_unlock(&khd->lock);
  714. return rq;
  715. }
  716. static bool kyber_has_work(struct blk_mq_hw_ctx *hctx)
  717. {
  718. struct kyber_hctx_data *khd = hctx->sched_data;
  719. int i;
  720. for (i = 0; i < KYBER_NUM_DOMAINS; i++) {
  721. if (!list_empty_careful(&khd->rqs[i]) ||
  722. sbitmap_any_bit_set(&khd->kcq_map[i]))
  723. return true;
  724. }
  725. return false;
  726. }
  727. #define KYBER_LAT_SHOW_STORE(domain, name) \
  728. static ssize_t kyber_##name##_lat_show(struct elevator_queue *e, \
  729. char *page) \
  730. { \
  731. struct kyber_queue_data *kqd = e->elevator_data; \
  732. \
  733. return sprintf(page, "%llu\n", kqd->latency_targets[domain]); \
  734. } \
  735. \
  736. static ssize_t kyber_##name##_lat_store(struct elevator_queue *e, \
  737. const char *page, size_t count) \
  738. { \
  739. struct kyber_queue_data *kqd = e->elevator_data; \
  740. unsigned long long nsec; \
  741. int ret; \
  742. \
  743. ret = kstrtoull(page, 10, &nsec); \
  744. if (ret) \
  745. return ret; \
  746. \
  747. kqd->latency_targets[domain] = nsec; \
  748. \
  749. return count; \
  750. }
  751. KYBER_LAT_SHOW_STORE(KYBER_READ, read);
  752. KYBER_LAT_SHOW_STORE(KYBER_WRITE, write);
  753. #undef KYBER_LAT_SHOW_STORE
  754. #define KYBER_LAT_ATTR(op) __ATTR(op##_lat_nsec, 0644, kyber_##op##_lat_show, kyber_##op##_lat_store)
  755. static struct elv_fs_entry kyber_sched_attrs[] = {
  756. KYBER_LAT_ATTR(read),
  757. KYBER_LAT_ATTR(write),
  758. __ATTR_NULL
  759. };
  760. #undef KYBER_LAT_ATTR
  761. #ifdef CONFIG_BLK_DEBUG_FS
  762. #define KYBER_DEBUGFS_DOMAIN_ATTRS(domain, name) \
  763. static int kyber_##name##_tokens_show(void *data, struct seq_file *m) \
  764. { \
  765. struct request_queue *q = data; \
  766. struct kyber_queue_data *kqd = q->elevator->elevator_data; \
  767. \
  768. sbitmap_queue_show(&kqd->domain_tokens[domain], m); \
  769. return 0; \
  770. } \
  771. \
  772. static void *kyber_##name##_rqs_start(struct seq_file *m, loff_t *pos) \
  773. __acquires(&khd->lock) \
  774. { \
  775. struct blk_mq_hw_ctx *hctx = m->private; \
  776. struct kyber_hctx_data *khd = hctx->sched_data; \
  777. \
  778. spin_lock(&khd->lock); \
  779. return seq_list_start(&khd->rqs[domain], *pos); \
  780. } \
  781. \
  782. static void *kyber_##name##_rqs_next(struct seq_file *m, void *v, \
  783. loff_t *pos) \
  784. { \
  785. struct blk_mq_hw_ctx *hctx = m->private; \
  786. struct kyber_hctx_data *khd = hctx->sched_data; \
  787. \
  788. return seq_list_next(v, &khd->rqs[domain], pos); \
  789. } \
  790. \
  791. static void kyber_##name##_rqs_stop(struct seq_file *m, void *v) \
  792. __releases(&khd->lock) \
  793. { \
  794. struct blk_mq_hw_ctx *hctx = m->private; \
  795. struct kyber_hctx_data *khd = hctx->sched_data; \
  796. \
  797. spin_unlock(&khd->lock); \
  798. } \
  799. \
  800. static const struct seq_operations kyber_##name##_rqs_seq_ops = { \
  801. .start = kyber_##name##_rqs_start, \
  802. .next = kyber_##name##_rqs_next, \
  803. .stop = kyber_##name##_rqs_stop, \
  804. .show = blk_mq_debugfs_rq_show, \
  805. }; \
  806. \
  807. static int kyber_##name##_waiting_show(void *data, struct seq_file *m) \
  808. { \
  809. struct blk_mq_hw_ctx *hctx = data; \
  810. struct kyber_hctx_data *khd = hctx->sched_data; \
  811. wait_queue_entry_t *wait = &khd->domain_wait[domain].wait; \
  812. \
  813. seq_printf(m, "%d\n", !list_empty_careful(&wait->entry)); \
  814. return 0; \
  815. }
  816. KYBER_DEBUGFS_DOMAIN_ATTRS(KYBER_READ, read)
  817. KYBER_DEBUGFS_DOMAIN_ATTRS(KYBER_WRITE, write)
  818. KYBER_DEBUGFS_DOMAIN_ATTRS(KYBER_DISCARD, discard)
  819. KYBER_DEBUGFS_DOMAIN_ATTRS(KYBER_OTHER, other)
  820. #undef KYBER_DEBUGFS_DOMAIN_ATTRS
  821. static int kyber_async_depth_show(void *data, struct seq_file *m)
  822. {
  823. struct request_queue *q = data;
  824. struct kyber_queue_data *kqd = q->elevator->elevator_data;
  825. seq_printf(m, "%u\n", kqd->async_depth);
  826. return 0;
  827. }
  828. static int kyber_cur_domain_show(void *data, struct seq_file *m)
  829. {
  830. struct blk_mq_hw_ctx *hctx = data;
  831. struct kyber_hctx_data *khd = hctx->sched_data;
  832. seq_printf(m, "%s\n", kyber_domain_names[khd->cur_domain]);
  833. return 0;
  834. }
  835. static int kyber_batching_show(void *data, struct seq_file *m)
  836. {
  837. struct blk_mq_hw_ctx *hctx = data;
  838. struct kyber_hctx_data *khd = hctx->sched_data;
  839. seq_printf(m, "%u\n", khd->batching);
  840. return 0;
  841. }
  842. #define KYBER_QUEUE_DOMAIN_ATTRS(name) \
  843. {#name "_tokens", 0400, kyber_##name##_tokens_show}
  844. static const struct blk_mq_debugfs_attr kyber_queue_debugfs_attrs[] = {
  845. KYBER_QUEUE_DOMAIN_ATTRS(read),
  846. KYBER_QUEUE_DOMAIN_ATTRS(write),
  847. KYBER_QUEUE_DOMAIN_ATTRS(discard),
  848. KYBER_QUEUE_DOMAIN_ATTRS(other),
  849. {"async_depth", 0400, kyber_async_depth_show},
  850. {},
  851. };
  852. #undef KYBER_QUEUE_DOMAIN_ATTRS
  853. #define KYBER_HCTX_DOMAIN_ATTRS(name) \
  854. {#name "_rqs", 0400, .seq_ops = &kyber_##name##_rqs_seq_ops}, \
  855. {#name "_waiting", 0400, kyber_##name##_waiting_show}
  856. static const struct blk_mq_debugfs_attr kyber_hctx_debugfs_attrs[] = {
  857. KYBER_HCTX_DOMAIN_ATTRS(read),
  858. KYBER_HCTX_DOMAIN_ATTRS(write),
  859. KYBER_HCTX_DOMAIN_ATTRS(discard),
  860. KYBER_HCTX_DOMAIN_ATTRS(other),
  861. {"cur_domain", 0400, kyber_cur_domain_show},
  862. {"batching", 0400, kyber_batching_show},
  863. {},
  864. };
  865. #undef KYBER_HCTX_DOMAIN_ATTRS
  866. #endif
  867. static struct elevator_type kyber_sched = {
  868. .ops = {
  869. .init_sched = kyber_init_sched,
  870. .exit_sched = kyber_exit_sched,
  871. .init_hctx = kyber_init_hctx,
  872. .exit_hctx = kyber_exit_hctx,
  873. .limit_depth = kyber_limit_depth,
  874. .bio_merge = kyber_bio_merge,
  875. .prepare_request = kyber_prepare_request,
  876. .insert_requests = kyber_insert_requests,
  877. .finish_request = kyber_finish_request,
  878. .requeue_request = kyber_finish_request,
  879. .completed_request = kyber_completed_request,
  880. .dispatch_request = kyber_dispatch_request,
  881. .has_work = kyber_has_work,
  882. .depth_updated = kyber_depth_updated,
  883. },
  884. #ifdef CONFIG_BLK_DEBUG_FS
  885. .queue_debugfs_attrs = kyber_queue_debugfs_attrs,
  886. .hctx_debugfs_attrs = kyber_hctx_debugfs_attrs,
  887. #endif
  888. .elevator_attrs = kyber_sched_attrs,
  889. .elevator_name = "kyber",
  890. .elevator_owner = THIS_MODULE,
  891. };
  892. static int __init kyber_init(void)
  893. {
  894. return elv_register(&kyber_sched);
  895. }
  896. static void __exit kyber_exit(void)
  897. {
  898. elv_unregister(&kyber_sched);
  899. }
  900. module_init(kyber_init);
  901. module_exit(kyber_exit);
  902. MODULE_AUTHOR("Omar Sandoval");
  903. MODULE_LICENSE("GPL");
  904. MODULE_DESCRIPTION("Kyber I/O scheduler");