kyber-iosched.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033
  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_DEFAULT_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. struct kyber_cpu_latency __percpu *cpu_latency;
  143. /* Timer for stats aggregation and adjusting domain tokens. */
  144. struct timer_list timer;
  145. unsigned int latency_buckets[KYBER_OTHER][2][KYBER_LATENCY_BUCKETS];
  146. unsigned long latency_timeout[KYBER_OTHER];
  147. int domain_p99[KYBER_OTHER];
  148. /* Target latencies in nanoseconds. */
  149. u64 latency_targets[KYBER_OTHER];
  150. };
  151. struct kyber_hctx_data {
  152. spinlock_t lock;
  153. struct list_head rqs[KYBER_NUM_DOMAINS];
  154. unsigned int cur_domain;
  155. unsigned int batching;
  156. struct kyber_ctx_queue *kcqs;
  157. struct sbitmap kcq_map[KYBER_NUM_DOMAINS];
  158. struct sbq_wait domain_wait[KYBER_NUM_DOMAINS];
  159. struct sbq_wait_state *domain_ws[KYBER_NUM_DOMAINS];
  160. atomic_t wait_index[KYBER_NUM_DOMAINS];
  161. };
  162. static int kyber_domain_wake(wait_queue_entry_t *wait, unsigned mode, int flags,
  163. void *key);
  164. static unsigned int kyber_sched_domain(blk_opf_t opf)
  165. {
  166. switch (opf & REQ_OP_MASK) {
  167. case REQ_OP_READ:
  168. return KYBER_READ;
  169. case REQ_OP_WRITE:
  170. return KYBER_WRITE;
  171. case REQ_OP_DISCARD:
  172. return KYBER_DISCARD;
  173. default:
  174. return KYBER_OTHER;
  175. }
  176. }
  177. static void flush_latency_buckets(struct kyber_queue_data *kqd,
  178. struct kyber_cpu_latency *cpu_latency,
  179. unsigned int sched_domain, unsigned int type)
  180. {
  181. unsigned int *buckets = kqd->latency_buckets[sched_domain][type];
  182. atomic_t *cpu_buckets = cpu_latency->buckets[sched_domain][type];
  183. unsigned int bucket;
  184. for (bucket = 0; bucket < KYBER_LATENCY_BUCKETS; bucket++)
  185. buckets[bucket] += atomic_xchg(&cpu_buckets[bucket], 0);
  186. }
  187. /*
  188. * Calculate the histogram bucket with the given percentile rank, or -1 if there
  189. * aren't enough samples yet.
  190. */
  191. static int calculate_percentile(struct kyber_queue_data *kqd,
  192. unsigned int sched_domain, unsigned int type,
  193. unsigned int percentile)
  194. {
  195. unsigned int *buckets = kqd->latency_buckets[sched_domain][type];
  196. unsigned int bucket, samples = 0, percentile_samples;
  197. for (bucket = 0; bucket < KYBER_LATENCY_BUCKETS; bucket++)
  198. samples += buckets[bucket];
  199. if (!samples)
  200. return -1;
  201. /*
  202. * We do the calculation once we have 500 samples or one second passes
  203. * since the first sample was recorded, whichever comes first.
  204. */
  205. if (!kqd->latency_timeout[sched_domain])
  206. kqd->latency_timeout[sched_domain] = max(jiffies + HZ, 1UL);
  207. if (samples < 500 &&
  208. time_is_after_jiffies(kqd->latency_timeout[sched_domain])) {
  209. return -1;
  210. }
  211. kqd->latency_timeout[sched_domain] = 0;
  212. percentile_samples = DIV_ROUND_UP(samples * percentile, 100);
  213. for (bucket = 0; bucket < KYBER_LATENCY_BUCKETS - 1; bucket++) {
  214. if (buckets[bucket] >= percentile_samples)
  215. break;
  216. percentile_samples -= buckets[bucket];
  217. }
  218. memset(buckets, 0, sizeof(kqd->latency_buckets[sched_domain][type]));
  219. trace_kyber_latency(kqd->dev, kyber_domain_names[sched_domain],
  220. kyber_latency_type_names[type], percentile,
  221. bucket + 1, 1 << KYBER_LATENCY_SHIFT, samples);
  222. return bucket;
  223. }
  224. static void kyber_resize_domain(struct kyber_queue_data *kqd,
  225. unsigned int sched_domain, unsigned int depth)
  226. {
  227. depth = clamp(depth, 1U, kyber_depth[sched_domain]);
  228. if (depth != kqd->domain_tokens[sched_domain].sb.depth) {
  229. sbitmap_queue_resize(&kqd->domain_tokens[sched_domain], depth);
  230. trace_kyber_adjust(kqd->dev, kyber_domain_names[sched_domain],
  231. depth);
  232. }
  233. }
  234. static void kyber_timer_fn(struct timer_list *t)
  235. {
  236. struct kyber_queue_data *kqd = timer_container_of(kqd, t, timer);
  237. unsigned int sched_domain;
  238. int cpu;
  239. bool bad = false;
  240. /* Sum all of the per-cpu latency histograms. */
  241. for_each_online_cpu(cpu) {
  242. struct kyber_cpu_latency *cpu_latency;
  243. cpu_latency = per_cpu_ptr(kqd->cpu_latency, cpu);
  244. for (sched_domain = 0; sched_domain < KYBER_OTHER; sched_domain++) {
  245. flush_latency_buckets(kqd, cpu_latency, sched_domain,
  246. KYBER_TOTAL_LATENCY);
  247. flush_latency_buckets(kqd, cpu_latency, sched_domain,
  248. KYBER_IO_LATENCY);
  249. }
  250. }
  251. /*
  252. * Check if any domains have a high I/O latency, which might indicate
  253. * congestion in the device. Note that we use the p90; we don't want to
  254. * be too sensitive to outliers here.
  255. */
  256. for (sched_domain = 0; sched_domain < KYBER_OTHER; sched_domain++) {
  257. int p90;
  258. p90 = calculate_percentile(kqd, sched_domain, KYBER_IO_LATENCY,
  259. 90);
  260. if (p90 >= KYBER_GOOD_BUCKETS)
  261. bad = true;
  262. }
  263. /*
  264. * Adjust the scheduling domain depths. If we determined that there was
  265. * congestion, we throttle all domains with good latencies. Either way,
  266. * we ease up on throttling domains with bad latencies.
  267. */
  268. for (sched_domain = 0; sched_domain < KYBER_OTHER; sched_domain++) {
  269. unsigned int orig_depth, depth;
  270. int p99;
  271. p99 = calculate_percentile(kqd, sched_domain,
  272. KYBER_TOTAL_LATENCY, 99);
  273. /*
  274. * This is kind of subtle: different domains will not
  275. * necessarily have enough samples to calculate the latency
  276. * percentiles during the same window, so we have to remember
  277. * the p99 for the next time we observe congestion; once we do,
  278. * we don't want to throttle again until we get more data, so we
  279. * reset it to -1.
  280. */
  281. if (bad) {
  282. if (p99 < 0)
  283. p99 = kqd->domain_p99[sched_domain];
  284. kqd->domain_p99[sched_domain] = -1;
  285. } else if (p99 >= 0) {
  286. kqd->domain_p99[sched_domain] = p99;
  287. }
  288. if (p99 < 0)
  289. continue;
  290. /*
  291. * If this domain has bad latency, throttle less. Otherwise,
  292. * throttle more iff we determined that there is congestion.
  293. *
  294. * The new depth is scaled linearly with the p99 latency vs the
  295. * latency target. E.g., if the p99 is 3/4 of the target, then
  296. * we throttle down to 3/4 of the current depth, and if the p99
  297. * is 2x the target, then we double the depth.
  298. */
  299. if (bad || p99 >= KYBER_GOOD_BUCKETS) {
  300. orig_depth = kqd->domain_tokens[sched_domain].sb.depth;
  301. depth = (orig_depth * (p99 + 1)) >> KYBER_LATENCY_SHIFT;
  302. kyber_resize_domain(kqd, sched_domain, depth);
  303. }
  304. }
  305. }
  306. static struct kyber_queue_data *kyber_queue_data_alloc(struct request_queue *q)
  307. {
  308. struct kyber_queue_data *kqd;
  309. int ret = -ENOMEM;
  310. int i;
  311. kqd = kzalloc_node(sizeof(*kqd), GFP_KERNEL, q->node);
  312. if (!kqd)
  313. goto err;
  314. kqd->q = q;
  315. kqd->dev = disk_devt(q->disk);
  316. kqd->cpu_latency = alloc_percpu_gfp(struct kyber_cpu_latency,
  317. GFP_KERNEL | __GFP_ZERO);
  318. if (!kqd->cpu_latency)
  319. goto err_kqd;
  320. timer_setup(&kqd->timer, kyber_timer_fn, 0);
  321. for (i = 0; i < KYBER_NUM_DOMAINS; i++) {
  322. WARN_ON(!kyber_depth[i]);
  323. WARN_ON(!kyber_batch_size[i]);
  324. ret = sbitmap_queue_init_node(&kqd->domain_tokens[i],
  325. kyber_depth[i], -1, false,
  326. GFP_KERNEL, q->node);
  327. if (ret) {
  328. while (--i >= 0)
  329. sbitmap_queue_free(&kqd->domain_tokens[i]);
  330. goto err_buckets;
  331. }
  332. }
  333. for (i = 0; i < KYBER_OTHER; i++) {
  334. kqd->domain_p99[i] = -1;
  335. kqd->latency_targets[i] = kyber_latency_targets[i];
  336. }
  337. return kqd;
  338. err_buckets:
  339. free_percpu(kqd->cpu_latency);
  340. err_kqd:
  341. kfree(kqd);
  342. err:
  343. return ERR_PTR(ret);
  344. }
  345. static void kyber_depth_updated(struct request_queue *q)
  346. {
  347. blk_mq_set_min_shallow_depth(q, q->async_depth);
  348. }
  349. static int kyber_init_sched(struct request_queue *q, struct elevator_queue *eq)
  350. {
  351. blk_stat_enable_accounting(q);
  352. blk_queue_flag_clear(QUEUE_FLAG_SQ_SCHED, q);
  353. q->elevator = eq;
  354. q->async_depth = q->nr_requests * KYBER_DEFAULT_ASYNC_PERCENT / 100;
  355. kyber_depth_updated(q);
  356. return 0;
  357. }
  358. static void *kyber_alloc_sched_data(struct request_queue *q)
  359. {
  360. struct kyber_queue_data *kqd;
  361. kqd = kyber_queue_data_alloc(q);
  362. if (IS_ERR(kqd))
  363. return NULL;
  364. return kqd;
  365. }
  366. static void kyber_exit_sched(struct elevator_queue *e)
  367. {
  368. struct kyber_queue_data *kqd = e->elevator_data;
  369. timer_shutdown_sync(&kqd->timer);
  370. blk_stat_disable_accounting(kqd->q);
  371. }
  372. static void kyber_free_sched_data(void *elv_data)
  373. {
  374. struct kyber_queue_data *kqd = elv_data;
  375. int i;
  376. if (!kqd)
  377. return;
  378. for (i = 0; i < KYBER_NUM_DOMAINS; i++)
  379. sbitmap_queue_free(&kqd->domain_tokens[i]);
  380. free_percpu(kqd->cpu_latency);
  381. kfree(kqd);
  382. }
  383. static void kyber_ctx_queue_init(struct kyber_ctx_queue *kcq)
  384. {
  385. unsigned int i;
  386. spin_lock_init(&kcq->lock);
  387. for (i = 0; i < KYBER_NUM_DOMAINS; i++)
  388. INIT_LIST_HEAD(&kcq->rq_list[i]);
  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. return 0;
  427. err_kcqs:
  428. kfree(khd->kcqs);
  429. err_khd:
  430. kfree(khd);
  431. return -ENOMEM;
  432. }
  433. static void kyber_exit_hctx(struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx)
  434. {
  435. struct kyber_hctx_data *khd = hctx->sched_data;
  436. int i;
  437. for (i = 0; i < KYBER_NUM_DOMAINS; i++)
  438. sbitmap_free(&khd->kcq_map[i]);
  439. kfree(khd->kcqs);
  440. kfree(hctx->sched_data);
  441. }
  442. static int rq_get_domain_token(struct request *rq)
  443. {
  444. return (long)rq->elv.priv[0];
  445. }
  446. static void rq_set_domain_token(struct request *rq, int token)
  447. {
  448. rq->elv.priv[0] = (void *)(long)token;
  449. }
  450. static void rq_clear_domain_token(struct kyber_queue_data *kqd,
  451. struct request *rq)
  452. {
  453. unsigned int sched_domain;
  454. int nr;
  455. nr = rq_get_domain_token(rq);
  456. if (nr != -1) {
  457. sched_domain = kyber_sched_domain(rq->cmd_flags);
  458. sbitmap_queue_clear(&kqd->domain_tokens[sched_domain], nr,
  459. rq->mq_ctx->cpu);
  460. }
  461. }
  462. static void kyber_limit_depth(blk_opf_t opf, struct blk_mq_alloc_data *data)
  463. {
  464. if (!blk_mq_is_sync_read(opf))
  465. data->shallow_depth = data->q->async_depth;
  466. }
  467. static bool kyber_bio_merge(struct request_queue *q, struct bio *bio,
  468. unsigned int nr_segs)
  469. {
  470. struct blk_mq_ctx *ctx = blk_mq_get_ctx(q);
  471. struct blk_mq_hw_ctx *hctx = blk_mq_map_queue(bio->bi_opf, ctx);
  472. struct kyber_hctx_data *khd = hctx->sched_data;
  473. struct kyber_ctx_queue *kcq = &khd->kcqs[ctx->index_hw[hctx->type]];
  474. unsigned int sched_domain = kyber_sched_domain(bio->bi_opf);
  475. struct list_head *rq_list = &kcq->rq_list[sched_domain];
  476. bool merged;
  477. spin_lock(&kcq->lock);
  478. merged = blk_bio_list_merge(hctx->queue, rq_list, bio, nr_segs);
  479. spin_unlock(&kcq->lock);
  480. return merged;
  481. }
  482. static void kyber_prepare_request(struct request *rq)
  483. {
  484. rq_set_domain_token(rq, -1);
  485. }
  486. static void kyber_insert_requests(struct blk_mq_hw_ctx *hctx,
  487. struct list_head *rq_list,
  488. blk_insert_t flags)
  489. {
  490. struct kyber_hctx_data *khd = hctx->sched_data;
  491. struct request *rq, *next;
  492. list_for_each_entry_safe(rq, next, rq_list, queuelist) {
  493. unsigned int sched_domain = kyber_sched_domain(rq->cmd_flags);
  494. struct kyber_ctx_queue *kcq = &khd->kcqs[rq->mq_ctx->index_hw[hctx->type]];
  495. struct list_head *head = &kcq->rq_list[sched_domain];
  496. spin_lock(&kcq->lock);
  497. trace_block_rq_insert(rq);
  498. if (flags & BLK_MQ_INSERT_AT_HEAD)
  499. list_move(&rq->queuelist, head);
  500. else
  501. list_move_tail(&rq->queuelist, head);
  502. sbitmap_set_bit(&khd->kcq_map[sched_domain],
  503. rq->mq_ctx->index_hw[hctx->type]);
  504. spin_unlock(&kcq->lock);
  505. }
  506. }
  507. static void kyber_finish_request(struct request *rq)
  508. {
  509. struct kyber_queue_data *kqd = rq->q->elevator->elevator_data;
  510. rq_clear_domain_token(kqd, rq);
  511. }
  512. static void add_latency_sample(struct kyber_cpu_latency *cpu_latency,
  513. unsigned int sched_domain, unsigned int type,
  514. u64 target, u64 latency)
  515. {
  516. unsigned int bucket;
  517. u64 divisor;
  518. if (latency > 0) {
  519. divisor = max_t(u64, target >> KYBER_LATENCY_SHIFT, 1);
  520. bucket = min_t(unsigned int, div64_u64(latency - 1, divisor),
  521. KYBER_LATENCY_BUCKETS - 1);
  522. } else {
  523. bucket = 0;
  524. }
  525. atomic_inc(&cpu_latency->buckets[sched_domain][type][bucket]);
  526. }
  527. static void kyber_completed_request(struct request *rq, u64 now)
  528. {
  529. struct kyber_queue_data *kqd = rq->q->elevator->elevator_data;
  530. struct kyber_cpu_latency *cpu_latency;
  531. unsigned int sched_domain;
  532. u64 target;
  533. sched_domain = kyber_sched_domain(rq->cmd_flags);
  534. if (sched_domain == KYBER_OTHER)
  535. return;
  536. cpu_latency = get_cpu_ptr(kqd->cpu_latency);
  537. target = kqd->latency_targets[sched_domain];
  538. add_latency_sample(cpu_latency, sched_domain, KYBER_TOTAL_LATENCY,
  539. target, now - rq->start_time_ns);
  540. add_latency_sample(cpu_latency, sched_domain, KYBER_IO_LATENCY, target,
  541. now - rq->io_start_time_ns);
  542. put_cpu_ptr(kqd->cpu_latency);
  543. timer_reduce(&kqd->timer, jiffies + HZ / 10);
  544. }
  545. struct flush_kcq_data {
  546. struct kyber_hctx_data *khd;
  547. unsigned int sched_domain;
  548. struct list_head *list;
  549. };
  550. static bool flush_busy_kcq(struct sbitmap *sb, unsigned int bitnr, void *data)
  551. {
  552. struct flush_kcq_data *flush_data = data;
  553. struct kyber_ctx_queue *kcq = &flush_data->khd->kcqs[bitnr];
  554. spin_lock(&kcq->lock);
  555. list_splice_tail_init(&kcq->rq_list[flush_data->sched_domain],
  556. flush_data->list);
  557. sbitmap_clear_bit(sb, bitnr);
  558. spin_unlock(&kcq->lock);
  559. return true;
  560. }
  561. static void kyber_flush_busy_kcqs(struct kyber_hctx_data *khd,
  562. unsigned int sched_domain,
  563. struct list_head *list)
  564. {
  565. struct flush_kcq_data data = {
  566. .khd = khd,
  567. .sched_domain = sched_domain,
  568. .list = list,
  569. };
  570. sbitmap_for_each_set(&khd->kcq_map[sched_domain],
  571. flush_busy_kcq, &data);
  572. }
  573. static int kyber_domain_wake(wait_queue_entry_t *wqe, unsigned mode, int flags,
  574. void *key)
  575. {
  576. struct blk_mq_hw_ctx *hctx = READ_ONCE(wqe->private);
  577. struct sbq_wait *wait = container_of(wqe, struct sbq_wait, wait);
  578. sbitmap_del_wait_queue(wait);
  579. blk_mq_run_hw_queue(hctx, true);
  580. return 1;
  581. }
  582. static int kyber_get_domain_token(struct kyber_queue_data *kqd,
  583. struct kyber_hctx_data *khd,
  584. struct blk_mq_hw_ctx *hctx)
  585. {
  586. unsigned int sched_domain = khd->cur_domain;
  587. struct sbitmap_queue *domain_tokens = &kqd->domain_tokens[sched_domain];
  588. struct sbq_wait *wait = &khd->domain_wait[sched_domain];
  589. struct sbq_wait_state *ws;
  590. int nr;
  591. nr = __sbitmap_queue_get(domain_tokens);
  592. /*
  593. * If we failed to get a domain token, make sure the hardware queue is
  594. * run when one becomes available. Note that this is serialized on
  595. * khd->lock, but we still need to be careful about the waker.
  596. */
  597. if (nr < 0 && list_empty_careful(&wait->wait.entry)) {
  598. ws = sbq_wait_ptr(domain_tokens,
  599. &khd->wait_index[sched_domain]);
  600. khd->domain_ws[sched_domain] = ws;
  601. sbitmap_add_wait_queue(domain_tokens, ws, wait);
  602. /*
  603. * Try again in case a token was freed before we got on the wait
  604. * queue.
  605. */
  606. nr = __sbitmap_queue_get(domain_tokens);
  607. }
  608. /*
  609. * If we got a token while we were on the wait queue, remove ourselves
  610. * from the wait queue to ensure that all wake ups make forward
  611. * progress. It's possible that the waker already deleted the entry
  612. * between the !list_empty_careful() check and us grabbing the lock, but
  613. * list_del_init() is okay with that.
  614. */
  615. if (nr >= 0 && !list_empty_careful(&wait->wait.entry)) {
  616. ws = khd->domain_ws[sched_domain];
  617. spin_lock_irq(&ws->wait.lock);
  618. sbitmap_del_wait_queue(wait);
  619. spin_unlock_irq(&ws->wait.lock);
  620. }
  621. return nr;
  622. }
  623. static struct request *
  624. kyber_dispatch_cur_domain(struct kyber_queue_data *kqd,
  625. struct kyber_hctx_data *khd,
  626. struct blk_mq_hw_ctx *hctx)
  627. {
  628. struct list_head *rqs;
  629. struct request *rq;
  630. int nr;
  631. rqs = &khd->rqs[khd->cur_domain];
  632. /*
  633. * If we already have a flushed request, then we just need to get a
  634. * token for it. Otherwise, if there are pending requests in the kcqs,
  635. * flush the kcqs, but only if we can get a token. If not, we should
  636. * leave the requests in the kcqs so that they can be merged. Note that
  637. * khd->lock serializes the flushes, so if we observed any bit set in
  638. * the kcq_map, we will always get a request.
  639. */
  640. rq = list_first_entry_or_null(rqs, struct request, queuelist);
  641. if (rq) {
  642. nr = kyber_get_domain_token(kqd, khd, hctx);
  643. if (nr >= 0) {
  644. khd->batching++;
  645. rq_set_domain_token(rq, nr);
  646. list_del_init(&rq->queuelist);
  647. return rq;
  648. } else {
  649. trace_kyber_throttled(kqd->dev,
  650. kyber_domain_names[khd->cur_domain]);
  651. }
  652. } else if (sbitmap_any_bit_set(&khd->kcq_map[khd->cur_domain])) {
  653. nr = kyber_get_domain_token(kqd, khd, hctx);
  654. if (nr >= 0) {
  655. kyber_flush_busy_kcqs(khd, khd->cur_domain, rqs);
  656. rq = list_first_entry(rqs, struct request, queuelist);
  657. khd->batching++;
  658. rq_set_domain_token(rq, nr);
  659. list_del_init(&rq->queuelist);
  660. return rq;
  661. } else {
  662. trace_kyber_throttled(kqd->dev,
  663. kyber_domain_names[khd->cur_domain]);
  664. }
  665. }
  666. /* There were either no pending requests or no tokens. */
  667. return NULL;
  668. }
  669. static struct request *kyber_dispatch_request(struct blk_mq_hw_ctx *hctx)
  670. {
  671. struct kyber_queue_data *kqd = hctx->queue->elevator->elevator_data;
  672. struct kyber_hctx_data *khd = hctx->sched_data;
  673. struct request *rq;
  674. int i;
  675. spin_lock(&khd->lock);
  676. /*
  677. * First, if we are still entitled to batch, try to dispatch a request
  678. * from the batch.
  679. */
  680. if (khd->batching < kyber_batch_size[khd->cur_domain]) {
  681. rq = kyber_dispatch_cur_domain(kqd, khd, hctx);
  682. if (rq)
  683. goto out;
  684. }
  685. /*
  686. * Either,
  687. * 1. We were no longer entitled to a batch.
  688. * 2. The domain we were batching didn't have any requests.
  689. * 3. The domain we were batching was out of tokens.
  690. *
  691. * Start another batch. Note that this wraps back around to the original
  692. * domain if no other domains have requests or tokens.
  693. */
  694. khd->batching = 0;
  695. for (i = 0; i < KYBER_NUM_DOMAINS; i++) {
  696. if (khd->cur_domain == KYBER_NUM_DOMAINS - 1)
  697. khd->cur_domain = 0;
  698. else
  699. khd->cur_domain++;
  700. rq = kyber_dispatch_cur_domain(kqd, khd, hctx);
  701. if (rq)
  702. goto out;
  703. }
  704. rq = NULL;
  705. out:
  706. spin_unlock(&khd->lock);
  707. return rq;
  708. }
  709. static bool kyber_has_work(struct blk_mq_hw_ctx *hctx)
  710. {
  711. struct kyber_hctx_data *khd = hctx->sched_data;
  712. int i;
  713. for (i = 0; i < KYBER_NUM_DOMAINS; i++) {
  714. if (!list_empty_careful(&khd->rqs[i]) ||
  715. sbitmap_any_bit_set(&khd->kcq_map[i]))
  716. return true;
  717. }
  718. return false;
  719. }
  720. #define KYBER_LAT_SHOW_STORE(domain, name) \
  721. static ssize_t kyber_##name##_lat_show(struct elevator_queue *e, \
  722. char *page) \
  723. { \
  724. struct kyber_queue_data *kqd = e->elevator_data; \
  725. \
  726. return sprintf(page, "%llu\n", kqd->latency_targets[domain]); \
  727. } \
  728. \
  729. static ssize_t kyber_##name##_lat_store(struct elevator_queue *e, \
  730. const char *page, size_t count) \
  731. { \
  732. struct kyber_queue_data *kqd = e->elevator_data; \
  733. unsigned long long nsec; \
  734. int ret; \
  735. \
  736. ret = kstrtoull(page, 10, &nsec); \
  737. if (ret) \
  738. return ret; \
  739. \
  740. kqd->latency_targets[domain] = nsec; \
  741. \
  742. return count; \
  743. }
  744. KYBER_LAT_SHOW_STORE(KYBER_READ, read);
  745. KYBER_LAT_SHOW_STORE(KYBER_WRITE, write);
  746. #undef KYBER_LAT_SHOW_STORE
  747. #define KYBER_LAT_ATTR(op) __ATTR(op##_lat_nsec, 0644, kyber_##op##_lat_show, kyber_##op##_lat_store)
  748. static const struct elv_fs_entry kyber_sched_attrs[] = {
  749. KYBER_LAT_ATTR(read),
  750. KYBER_LAT_ATTR(write),
  751. __ATTR_NULL
  752. };
  753. #undef KYBER_LAT_ATTR
  754. #ifdef CONFIG_BLK_DEBUG_FS
  755. #define KYBER_DEBUGFS_DOMAIN_ATTRS(domain, name) \
  756. static int kyber_##name##_tokens_show(void *data, struct seq_file *m) \
  757. { \
  758. struct request_queue *q = data; \
  759. struct kyber_queue_data *kqd = q->elevator->elevator_data; \
  760. \
  761. sbitmap_queue_show(&kqd->domain_tokens[domain], m); \
  762. return 0; \
  763. } \
  764. \
  765. static void *kyber_##name##_rqs_start(struct seq_file *m, loff_t *pos) \
  766. __acquires(&khd->lock) \
  767. { \
  768. struct blk_mq_hw_ctx *hctx = m->private; \
  769. struct kyber_hctx_data *khd = hctx->sched_data; \
  770. \
  771. spin_lock(&khd->lock); \
  772. return seq_list_start(&khd->rqs[domain], *pos); \
  773. } \
  774. \
  775. static void *kyber_##name##_rqs_next(struct seq_file *m, void *v, \
  776. loff_t *pos) \
  777. { \
  778. struct blk_mq_hw_ctx *hctx = m->private; \
  779. struct kyber_hctx_data *khd = hctx->sched_data; \
  780. \
  781. return seq_list_next(v, &khd->rqs[domain], pos); \
  782. } \
  783. \
  784. static void kyber_##name##_rqs_stop(struct seq_file *m, void *v) \
  785. __releases(&khd->lock) \
  786. { \
  787. struct blk_mq_hw_ctx *hctx = m->private; \
  788. struct kyber_hctx_data *khd = hctx->sched_data; \
  789. \
  790. spin_unlock(&khd->lock); \
  791. } \
  792. \
  793. static const struct seq_operations kyber_##name##_rqs_seq_ops = { \
  794. .start = kyber_##name##_rqs_start, \
  795. .next = kyber_##name##_rqs_next, \
  796. .stop = kyber_##name##_rqs_stop, \
  797. .show = blk_mq_debugfs_rq_show, \
  798. }; \
  799. \
  800. static int kyber_##name##_waiting_show(void *data, struct seq_file *m) \
  801. { \
  802. struct blk_mq_hw_ctx *hctx = data; \
  803. struct kyber_hctx_data *khd = hctx->sched_data; \
  804. wait_queue_entry_t *wait = &khd->domain_wait[domain].wait; \
  805. \
  806. seq_printf(m, "%d\n", !list_empty_careful(&wait->entry)); \
  807. return 0; \
  808. }
  809. KYBER_DEBUGFS_DOMAIN_ATTRS(KYBER_READ, read)
  810. KYBER_DEBUGFS_DOMAIN_ATTRS(KYBER_WRITE, write)
  811. KYBER_DEBUGFS_DOMAIN_ATTRS(KYBER_DISCARD, discard)
  812. KYBER_DEBUGFS_DOMAIN_ATTRS(KYBER_OTHER, other)
  813. #undef KYBER_DEBUGFS_DOMAIN_ATTRS
  814. static int kyber_cur_domain_show(void *data, struct seq_file *m)
  815. {
  816. struct blk_mq_hw_ctx *hctx = data;
  817. struct kyber_hctx_data *khd = hctx->sched_data;
  818. seq_printf(m, "%s\n", kyber_domain_names[khd->cur_domain]);
  819. return 0;
  820. }
  821. static int kyber_batching_show(void *data, struct seq_file *m)
  822. {
  823. struct blk_mq_hw_ctx *hctx = data;
  824. struct kyber_hctx_data *khd = hctx->sched_data;
  825. seq_printf(m, "%u\n", khd->batching);
  826. return 0;
  827. }
  828. #define KYBER_QUEUE_DOMAIN_ATTRS(name) \
  829. {#name "_tokens", 0400, kyber_##name##_tokens_show}
  830. static const struct blk_mq_debugfs_attr kyber_queue_debugfs_attrs[] = {
  831. KYBER_QUEUE_DOMAIN_ATTRS(read),
  832. KYBER_QUEUE_DOMAIN_ATTRS(write),
  833. KYBER_QUEUE_DOMAIN_ATTRS(discard),
  834. KYBER_QUEUE_DOMAIN_ATTRS(other),
  835. {},
  836. };
  837. #undef KYBER_QUEUE_DOMAIN_ATTRS
  838. #define KYBER_HCTX_DOMAIN_ATTRS(name) \
  839. {#name "_rqs", 0400, .seq_ops = &kyber_##name##_rqs_seq_ops}, \
  840. {#name "_waiting", 0400, kyber_##name##_waiting_show}
  841. static const struct blk_mq_debugfs_attr kyber_hctx_debugfs_attrs[] = {
  842. KYBER_HCTX_DOMAIN_ATTRS(read),
  843. KYBER_HCTX_DOMAIN_ATTRS(write),
  844. KYBER_HCTX_DOMAIN_ATTRS(discard),
  845. KYBER_HCTX_DOMAIN_ATTRS(other),
  846. {"cur_domain", 0400, kyber_cur_domain_show},
  847. {"batching", 0400, kyber_batching_show},
  848. {},
  849. };
  850. #undef KYBER_HCTX_DOMAIN_ATTRS
  851. #endif
  852. static struct elevator_type kyber_sched = {
  853. .ops = {
  854. .init_sched = kyber_init_sched,
  855. .exit_sched = kyber_exit_sched,
  856. .init_hctx = kyber_init_hctx,
  857. .exit_hctx = kyber_exit_hctx,
  858. .alloc_sched_data = kyber_alloc_sched_data,
  859. .free_sched_data = kyber_free_sched_data,
  860. .limit_depth = kyber_limit_depth,
  861. .bio_merge = kyber_bio_merge,
  862. .prepare_request = kyber_prepare_request,
  863. .insert_requests = kyber_insert_requests,
  864. .finish_request = kyber_finish_request,
  865. .requeue_request = kyber_finish_request,
  866. .completed_request = kyber_completed_request,
  867. .dispatch_request = kyber_dispatch_request,
  868. .has_work = kyber_has_work,
  869. .depth_updated = kyber_depth_updated,
  870. },
  871. #ifdef CONFIG_BLK_DEBUG_FS
  872. .queue_debugfs_attrs = kyber_queue_debugfs_attrs,
  873. .hctx_debugfs_attrs = kyber_hctx_debugfs_attrs,
  874. #endif
  875. .elevator_attrs = kyber_sched_attrs,
  876. .elevator_name = "kyber",
  877. .elevator_owner = THIS_MODULE,
  878. };
  879. static int __init kyber_init(void)
  880. {
  881. return elv_register(&kyber_sched);
  882. }
  883. static void __exit kyber_exit(void)
  884. {
  885. elv_unregister(&kyber_sched);
  886. }
  887. module_init(kyber_init);
  888. module_exit(kyber_exit);
  889. MODULE_AUTHOR("Omar Sandoval");
  890. MODULE_LICENSE("GPL");
  891. MODULE_DESCRIPTION("Kyber I/O scheduler");