blk-mq.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506
  1. #include <linux/kernel.h>
  2. #include <linux/module.h>
  3. #include <linux/backing-dev.h>
  4. #include <linux/bio.h>
  5. #include <linux/blkdev.h>
  6. #include <linux/mm.h>
  7. #include <linux/init.h>
  8. #include <linux/slab.h>
  9. #include <linux/workqueue.h>
  10. #include <linux/smp.h>
  11. #include <linux/llist.h>
  12. #include <linux/list_sort.h>
  13. #include <linux/cpu.h>
  14. #include <linux/cache.h>
  15. #include <linux/sched/sysctl.h>
  16. #include <linux/delay.h>
  17. #include <trace/events/block.h>
  18. #include <linux/blk-mq.h>
  19. #include "blk.h"
  20. #include "blk-mq.h"
  21. #include "blk-mq-tag.h"
  22. static DEFINE_MUTEX(all_q_mutex);
  23. static LIST_HEAD(all_q_list);
  24. static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx);
  25. DEFINE_PER_CPU(struct llist_head, ipi_lists);
  26. static struct blk_mq_ctx *__blk_mq_get_ctx(struct request_queue *q,
  27. unsigned int cpu)
  28. {
  29. return per_cpu_ptr(q->queue_ctx, cpu);
  30. }
  31. /*
  32. * This assumes per-cpu software queueing queues. They could be per-node
  33. * as well, for instance. For now this is hardcoded as-is. Note that we don't
  34. * care about preemption, since we know the ctx's are persistent. This does
  35. * mean that we can't rely on ctx always matching the currently running CPU.
  36. */
  37. static struct blk_mq_ctx *blk_mq_get_ctx(struct request_queue *q)
  38. {
  39. return __blk_mq_get_ctx(q, get_cpu());
  40. }
  41. static void blk_mq_put_ctx(struct blk_mq_ctx *ctx)
  42. {
  43. put_cpu();
  44. }
  45. /*
  46. * Check if any of the ctx's have pending work in this hardware queue
  47. */
  48. static bool blk_mq_hctx_has_pending(struct blk_mq_hw_ctx *hctx)
  49. {
  50. unsigned int i;
  51. for (i = 0; i < hctx->nr_ctx_map; i++)
  52. if (hctx->ctx_map[i])
  53. return true;
  54. return false;
  55. }
  56. /*
  57. * Mark this ctx as having pending work in this hardware queue
  58. */
  59. static void blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx *hctx,
  60. struct blk_mq_ctx *ctx)
  61. {
  62. if (!test_bit(ctx->index_hw, hctx->ctx_map))
  63. set_bit(ctx->index_hw, hctx->ctx_map);
  64. }
  65. static struct request *blk_mq_alloc_rq(struct blk_mq_hw_ctx *hctx, gfp_t gfp,
  66. bool reserved)
  67. {
  68. struct request *rq;
  69. unsigned int tag;
  70. tag = blk_mq_get_tag(hctx->tags, gfp, reserved);
  71. if (tag != BLK_MQ_TAG_FAIL) {
  72. rq = hctx->rqs[tag];
  73. rq->tag = tag;
  74. return rq;
  75. }
  76. return NULL;
  77. }
  78. static int blk_mq_queue_enter(struct request_queue *q)
  79. {
  80. int ret;
  81. __percpu_counter_add(&q->mq_usage_counter, 1, 1000000);
  82. smp_wmb();
  83. /* we have problems to freeze the queue if it's initializing */
  84. if (!blk_queue_bypass(q) || !blk_queue_init_done(q))
  85. return 0;
  86. __percpu_counter_add(&q->mq_usage_counter, -1, 1000000);
  87. spin_lock_irq(q->queue_lock);
  88. ret = wait_event_interruptible_lock_irq(q->mq_freeze_wq,
  89. !blk_queue_bypass(q), *q->queue_lock);
  90. /* inc usage with lock hold to avoid freeze_queue runs here */
  91. if (!ret)
  92. __percpu_counter_add(&q->mq_usage_counter, 1, 1000000);
  93. spin_unlock_irq(q->queue_lock);
  94. return ret;
  95. }
  96. static void blk_mq_queue_exit(struct request_queue *q)
  97. {
  98. __percpu_counter_add(&q->mq_usage_counter, -1, 1000000);
  99. }
  100. /*
  101. * Guarantee no request is in use, so we can change any data structure of
  102. * the queue afterward.
  103. */
  104. static void blk_mq_freeze_queue(struct request_queue *q)
  105. {
  106. bool drain;
  107. spin_lock_irq(q->queue_lock);
  108. drain = !q->bypass_depth++;
  109. queue_flag_set(QUEUE_FLAG_BYPASS, q);
  110. spin_unlock_irq(q->queue_lock);
  111. if (!drain)
  112. return;
  113. while (true) {
  114. s64 count;
  115. spin_lock_irq(q->queue_lock);
  116. count = percpu_counter_sum(&q->mq_usage_counter);
  117. spin_unlock_irq(q->queue_lock);
  118. if (count == 0)
  119. break;
  120. blk_mq_run_queues(q, false);
  121. msleep(10);
  122. }
  123. }
  124. static void blk_mq_unfreeze_queue(struct request_queue *q)
  125. {
  126. bool wake = false;
  127. spin_lock_irq(q->queue_lock);
  128. if (!--q->bypass_depth) {
  129. queue_flag_clear(QUEUE_FLAG_BYPASS, q);
  130. wake = true;
  131. }
  132. WARN_ON_ONCE(q->bypass_depth < 0);
  133. spin_unlock_irq(q->queue_lock);
  134. if (wake)
  135. wake_up_all(&q->mq_freeze_wq);
  136. }
  137. bool blk_mq_can_queue(struct blk_mq_hw_ctx *hctx)
  138. {
  139. return blk_mq_has_free_tags(hctx->tags);
  140. }
  141. EXPORT_SYMBOL(blk_mq_can_queue);
  142. static void blk_mq_rq_ctx_init(struct request_queue *q, struct blk_mq_ctx *ctx,
  143. struct request *rq, unsigned int rw_flags)
  144. {
  145. if (blk_queue_io_stat(q))
  146. rw_flags |= REQ_IO_STAT;
  147. rq->mq_ctx = ctx;
  148. rq->cmd_flags = rw_flags;
  149. ctx->rq_dispatched[rw_is_sync(rw_flags)]++;
  150. }
  151. static struct request *__blk_mq_alloc_request(struct blk_mq_hw_ctx *hctx,
  152. gfp_t gfp, bool reserved)
  153. {
  154. return blk_mq_alloc_rq(hctx, gfp, reserved);
  155. }
  156. static struct request *blk_mq_alloc_request_pinned(struct request_queue *q,
  157. int rw, gfp_t gfp,
  158. bool reserved)
  159. {
  160. struct request *rq;
  161. do {
  162. struct blk_mq_ctx *ctx = blk_mq_get_ctx(q);
  163. struct blk_mq_hw_ctx *hctx = q->mq_ops->map_queue(q, ctx->cpu);
  164. rq = __blk_mq_alloc_request(hctx, gfp & ~__GFP_WAIT, reserved);
  165. if (rq) {
  166. blk_mq_rq_ctx_init(q, ctx, rq, rw);
  167. break;
  168. } else if (!(gfp & __GFP_WAIT))
  169. break;
  170. blk_mq_put_ctx(ctx);
  171. __blk_mq_run_hw_queue(hctx);
  172. blk_mq_wait_for_tags(hctx->tags);
  173. } while (1);
  174. return rq;
  175. }
  176. struct request *blk_mq_alloc_request(struct request_queue *q, int rw,
  177. gfp_t gfp, bool reserved)
  178. {
  179. struct request *rq;
  180. if (blk_mq_queue_enter(q))
  181. return NULL;
  182. rq = blk_mq_alloc_request_pinned(q, rw, gfp, reserved);
  183. blk_mq_put_ctx(rq->mq_ctx);
  184. return rq;
  185. }
  186. struct request *blk_mq_alloc_reserved_request(struct request_queue *q, int rw,
  187. gfp_t gfp)
  188. {
  189. struct request *rq;
  190. if (blk_mq_queue_enter(q))
  191. return NULL;
  192. rq = blk_mq_alloc_request_pinned(q, rw, gfp, true);
  193. blk_mq_put_ctx(rq->mq_ctx);
  194. return rq;
  195. }
  196. EXPORT_SYMBOL(blk_mq_alloc_reserved_request);
  197. /*
  198. * Re-init and set pdu, if we have it
  199. */
  200. static void blk_mq_rq_init(struct blk_mq_hw_ctx *hctx, struct request *rq)
  201. {
  202. blk_rq_init(hctx->queue, rq);
  203. if (hctx->cmd_size)
  204. rq->special = blk_mq_rq_to_pdu(rq);
  205. }
  206. static void __blk_mq_free_request(struct blk_mq_hw_ctx *hctx,
  207. struct blk_mq_ctx *ctx, struct request *rq)
  208. {
  209. const int tag = rq->tag;
  210. struct request_queue *q = rq->q;
  211. blk_mq_rq_init(hctx, rq);
  212. blk_mq_put_tag(hctx->tags, tag);
  213. blk_mq_queue_exit(q);
  214. }
  215. void blk_mq_free_request(struct request *rq)
  216. {
  217. struct blk_mq_ctx *ctx = rq->mq_ctx;
  218. struct blk_mq_hw_ctx *hctx;
  219. struct request_queue *q = rq->q;
  220. ctx->rq_completed[rq_is_sync(rq)]++;
  221. hctx = q->mq_ops->map_queue(q, ctx->cpu);
  222. __blk_mq_free_request(hctx, ctx, rq);
  223. }
  224. static void blk_mq_bio_endio(struct request *rq, struct bio *bio, int error)
  225. {
  226. if (error)
  227. clear_bit(BIO_UPTODATE, &bio->bi_flags);
  228. else if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
  229. error = -EIO;
  230. if (unlikely(rq->cmd_flags & REQ_QUIET))
  231. set_bit(BIO_QUIET, &bio->bi_flags);
  232. /* don't actually finish bio if it's part of flush sequence */
  233. if (!(rq->cmd_flags & REQ_FLUSH_SEQ))
  234. bio_endio(bio, error);
  235. }
  236. void blk_mq_complete_request(struct request *rq, int error)
  237. {
  238. struct bio *bio = rq->bio;
  239. unsigned int bytes = 0;
  240. trace_block_rq_complete(rq->q, rq);
  241. while (bio) {
  242. struct bio *next = bio->bi_next;
  243. bio->bi_next = NULL;
  244. bytes += bio->bi_size;
  245. blk_mq_bio_endio(rq, bio, error);
  246. bio = next;
  247. }
  248. blk_account_io_completion(rq, bytes);
  249. if (rq->end_io)
  250. rq->end_io(rq, error);
  251. else
  252. blk_mq_free_request(rq);
  253. blk_account_io_done(rq);
  254. }
  255. void __blk_mq_end_io(struct request *rq, int error)
  256. {
  257. if (!blk_mark_rq_complete(rq))
  258. blk_mq_complete_request(rq, error);
  259. }
  260. #if defined(CONFIG_SMP)
  261. /*
  262. * Called with interrupts disabled.
  263. */
  264. static void ipi_end_io(void *data)
  265. {
  266. struct llist_head *list = &per_cpu(ipi_lists, smp_processor_id());
  267. struct llist_node *entry, *next;
  268. struct request *rq;
  269. entry = llist_del_all(list);
  270. while (entry) {
  271. next = entry->next;
  272. rq = llist_entry(entry, struct request, ll_list);
  273. __blk_mq_end_io(rq, rq->errors);
  274. entry = next;
  275. }
  276. }
  277. static int ipi_remote_cpu(struct blk_mq_ctx *ctx, const int cpu,
  278. struct request *rq, const int error)
  279. {
  280. struct call_single_data *data = &rq->csd;
  281. rq->errors = error;
  282. rq->ll_list.next = NULL;
  283. /*
  284. * If the list is non-empty, an existing IPI must already
  285. * be "in flight". If that is the case, we need not schedule
  286. * a new one.
  287. */
  288. if (llist_add(&rq->ll_list, &per_cpu(ipi_lists, ctx->cpu))) {
  289. data->func = ipi_end_io;
  290. data->flags = 0;
  291. __smp_call_function_single(ctx->cpu, data, 0);
  292. }
  293. return true;
  294. }
  295. #else /* CONFIG_SMP */
  296. static int ipi_remote_cpu(struct blk_mq_ctx *ctx, const int cpu,
  297. struct request *rq, const int error)
  298. {
  299. return false;
  300. }
  301. #endif
  302. /*
  303. * End IO on this request on a multiqueue enabled driver. We'll either do
  304. * it directly inline, or punt to a local IPI handler on the matching
  305. * remote CPU.
  306. */
  307. void blk_mq_end_io(struct request *rq, int error)
  308. {
  309. struct blk_mq_ctx *ctx = rq->mq_ctx;
  310. int cpu;
  311. if (!ctx->ipi_redirect)
  312. return __blk_mq_end_io(rq, error);
  313. cpu = get_cpu();
  314. if (cpu == ctx->cpu || !cpu_online(ctx->cpu) ||
  315. !ipi_remote_cpu(ctx, cpu, rq, error))
  316. __blk_mq_end_io(rq, error);
  317. put_cpu();
  318. }
  319. EXPORT_SYMBOL(blk_mq_end_io);
  320. static void blk_mq_start_request(struct request *rq)
  321. {
  322. struct request_queue *q = rq->q;
  323. trace_block_rq_issue(q, rq);
  324. /*
  325. * Just mark start time and set the started bit. Due to memory
  326. * ordering, we know we'll see the correct deadline as long as
  327. * REQ_ATOMIC_STARTED is seen.
  328. */
  329. rq->deadline = jiffies + q->rq_timeout;
  330. set_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
  331. }
  332. static void blk_mq_requeue_request(struct request *rq)
  333. {
  334. struct request_queue *q = rq->q;
  335. trace_block_rq_requeue(q, rq);
  336. clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
  337. }
  338. struct blk_mq_timeout_data {
  339. struct blk_mq_hw_ctx *hctx;
  340. unsigned long *next;
  341. unsigned int *next_set;
  342. };
  343. static void blk_mq_timeout_check(void *__data, unsigned long *free_tags)
  344. {
  345. struct blk_mq_timeout_data *data = __data;
  346. struct blk_mq_hw_ctx *hctx = data->hctx;
  347. unsigned int tag;
  348. /* It may not be in flight yet (this is where
  349. * the REQ_ATOMIC_STARTED flag comes in). The requests are
  350. * statically allocated, so we know it's always safe to access the
  351. * memory associated with a bit offset into ->rqs[].
  352. */
  353. tag = 0;
  354. do {
  355. struct request *rq;
  356. tag = find_next_zero_bit(free_tags, hctx->queue_depth, tag);
  357. if (tag >= hctx->queue_depth)
  358. break;
  359. rq = hctx->rqs[tag++];
  360. if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
  361. continue;
  362. blk_rq_check_expired(rq, data->next, data->next_set);
  363. } while (1);
  364. }
  365. static void blk_mq_hw_ctx_check_timeout(struct blk_mq_hw_ctx *hctx,
  366. unsigned long *next,
  367. unsigned int *next_set)
  368. {
  369. struct blk_mq_timeout_data data = {
  370. .hctx = hctx,
  371. .next = next,
  372. .next_set = next_set,
  373. };
  374. /*
  375. * Ask the tagging code to iterate busy requests, so we can
  376. * check them for timeout.
  377. */
  378. blk_mq_tag_busy_iter(hctx->tags, blk_mq_timeout_check, &data);
  379. }
  380. static void blk_mq_rq_timer(unsigned long data)
  381. {
  382. struct request_queue *q = (struct request_queue *) data;
  383. struct blk_mq_hw_ctx *hctx;
  384. unsigned long next = 0;
  385. int i, next_set = 0;
  386. queue_for_each_hw_ctx(q, hctx, i)
  387. blk_mq_hw_ctx_check_timeout(hctx, &next, &next_set);
  388. if (next_set)
  389. mod_timer(&q->timeout, round_jiffies_up(next));
  390. }
  391. /*
  392. * Reverse check our software queue for entries that we could potentially
  393. * merge with. Currently includes a hand-wavy stop count of 8, to not spend
  394. * too much time checking for merges.
  395. */
  396. static bool blk_mq_attempt_merge(struct request_queue *q,
  397. struct blk_mq_ctx *ctx, struct bio *bio)
  398. {
  399. struct request *rq;
  400. int checked = 8;
  401. list_for_each_entry_reverse(rq, &ctx->rq_list, queuelist) {
  402. int el_ret;
  403. if (!checked--)
  404. break;
  405. if (!blk_rq_merge_ok(rq, bio))
  406. continue;
  407. el_ret = blk_try_merge(rq, bio);
  408. if (el_ret == ELEVATOR_BACK_MERGE) {
  409. if (bio_attempt_back_merge(q, rq, bio)) {
  410. ctx->rq_merged++;
  411. return true;
  412. }
  413. break;
  414. } else if (el_ret == ELEVATOR_FRONT_MERGE) {
  415. if (bio_attempt_front_merge(q, rq, bio)) {
  416. ctx->rq_merged++;
  417. return true;
  418. }
  419. break;
  420. }
  421. }
  422. return false;
  423. }
  424. void blk_mq_add_timer(struct request *rq)
  425. {
  426. __blk_add_timer(rq, NULL);
  427. }
  428. /*
  429. * Run this hardware queue, pulling any software queues mapped to it in.
  430. * Note that this function currently has various problems around ordering
  431. * of IO. In particular, we'd like FIFO behaviour on handling existing
  432. * items on the hctx->dispatch list. Ignore that for now.
  433. */
  434. static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx)
  435. {
  436. struct request_queue *q = hctx->queue;
  437. struct blk_mq_ctx *ctx;
  438. struct request *rq;
  439. LIST_HEAD(rq_list);
  440. int bit, queued;
  441. if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->flags)))
  442. return;
  443. hctx->run++;
  444. /*
  445. * Touch any software queue that has pending entries.
  446. */
  447. for_each_set_bit(bit, hctx->ctx_map, hctx->nr_ctx) {
  448. clear_bit(bit, hctx->ctx_map);
  449. ctx = hctx->ctxs[bit];
  450. BUG_ON(bit != ctx->index_hw);
  451. spin_lock(&ctx->lock);
  452. list_splice_tail_init(&ctx->rq_list, &rq_list);
  453. spin_unlock(&ctx->lock);
  454. }
  455. /*
  456. * If we have previous entries on our dispatch list, grab them
  457. * and stuff them at the front for more fair dispatch.
  458. */
  459. if (!list_empty_careful(&hctx->dispatch)) {
  460. spin_lock(&hctx->lock);
  461. if (!list_empty(&hctx->dispatch))
  462. list_splice_init(&hctx->dispatch, &rq_list);
  463. spin_unlock(&hctx->lock);
  464. }
  465. /*
  466. * Delete and return all entries from our dispatch list
  467. */
  468. queued = 0;
  469. /*
  470. * Now process all the entries, sending them to the driver.
  471. */
  472. while (!list_empty(&rq_list)) {
  473. int ret;
  474. rq = list_first_entry(&rq_list, struct request, queuelist);
  475. list_del_init(&rq->queuelist);
  476. blk_mq_start_request(rq);
  477. /*
  478. * Last request in the series. Flag it as such, this
  479. * enables drivers to know when IO should be kicked off,
  480. * if they don't do it on a per-request basis.
  481. *
  482. * Note: the flag isn't the only condition drivers
  483. * should do kick off. If drive is busy, the last
  484. * request might not have the bit set.
  485. */
  486. if (list_empty(&rq_list))
  487. rq->cmd_flags |= REQ_END;
  488. ret = q->mq_ops->queue_rq(hctx, rq);
  489. switch (ret) {
  490. case BLK_MQ_RQ_QUEUE_OK:
  491. queued++;
  492. continue;
  493. case BLK_MQ_RQ_QUEUE_BUSY:
  494. /*
  495. * FIXME: we should have a mechanism to stop the queue
  496. * like blk_stop_queue, otherwise we will waste cpu
  497. * time
  498. */
  499. list_add(&rq->queuelist, &rq_list);
  500. blk_mq_requeue_request(rq);
  501. break;
  502. default:
  503. pr_err("blk-mq: bad return on queue: %d\n", ret);
  504. rq->errors = -EIO;
  505. case BLK_MQ_RQ_QUEUE_ERROR:
  506. blk_mq_end_io(rq, rq->errors);
  507. break;
  508. }
  509. if (ret == BLK_MQ_RQ_QUEUE_BUSY)
  510. break;
  511. }
  512. if (!queued)
  513. hctx->dispatched[0]++;
  514. else if (queued < (1 << (BLK_MQ_MAX_DISPATCH_ORDER - 1)))
  515. hctx->dispatched[ilog2(queued) + 1]++;
  516. /*
  517. * Any items that need requeuing? Stuff them into hctx->dispatch,
  518. * that is where we will continue on next queue run.
  519. */
  520. if (!list_empty(&rq_list)) {
  521. spin_lock(&hctx->lock);
  522. list_splice(&rq_list, &hctx->dispatch);
  523. spin_unlock(&hctx->lock);
  524. }
  525. }
  526. void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
  527. {
  528. if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->flags)))
  529. return;
  530. if (!async)
  531. __blk_mq_run_hw_queue(hctx);
  532. else {
  533. struct request_queue *q = hctx->queue;
  534. kblockd_schedule_delayed_work(q, &hctx->delayed_work, 0);
  535. }
  536. }
  537. void blk_mq_run_queues(struct request_queue *q, bool async)
  538. {
  539. struct blk_mq_hw_ctx *hctx;
  540. int i;
  541. queue_for_each_hw_ctx(q, hctx, i) {
  542. if ((!blk_mq_hctx_has_pending(hctx) &&
  543. list_empty_careful(&hctx->dispatch)) ||
  544. test_bit(BLK_MQ_S_STOPPED, &hctx->flags))
  545. continue;
  546. blk_mq_run_hw_queue(hctx, async);
  547. }
  548. }
  549. EXPORT_SYMBOL(blk_mq_run_queues);
  550. void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx)
  551. {
  552. cancel_delayed_work(&hctx->delayed_work);
  553. set_bit(BLK_MQ_S_STOPPED, &hctx->state);
  554. }
  555. EXPORT_SYMBOL(blk_mq_stop_hw_queue);
  556. void blk_mq_stop_hw_queues(struct request_queue *q)
  557. {
  558. struct blk_mq_hw_ctx *hctx;
  559. int i;
  560. queue_for_each_hw_ctx(q, hctx, i)
  561. blk_mq_stop_hw_queue(hctx);
  562. }
  563. EXPORT_SYMBOL(blk_mq_stop_hw_queues);
  564. void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx)
  565. {
  566. clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
  567. __blk_mq_run_hw_queue(hctx);
  568. }
  569. EXPORT_SYMBOL(blk_mq_start_hw_queue);
  570. void blk_mq_start_stopped_hw_queues(struct request_queue *q)
  571. {
  572. struct blk_mq_hw_ctx *hctx;
  573. int i;
  574. queue_for_each_hw_ctx(q, hctx, i) {
  575. if (!test_bit(BLK_MQ_S_STOPPED, &hctx->state))
  576. continue;
  577. clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
  578. blk_mq_run_hw_queue(hctx, true);
  579. }
  580. }
  581. EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues);
  582. static void blk_mq_work_fn(struct work_struct *work)
  583. {
  584. struct blk_mq_hw_ctx *hctx;
  585. hctx = container_of(work, struct blk_mq_hw_ctx, delayed_work.work);
  586. __blk_mq_run_hw_queue(hctx);
  587. }
  588. static void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx,
  589. struct request *rq)
  590. {
  591. struct blk_mq_ctx *ctx = rq->mq_ctx;
  592. trace_block_rq_insert(hctx->queue, rq);
  593. list_add_tail(&rq->queuelist, &ctx->rq_list);
  594. blk_mq_hctx_mark_pending(hctx, ctx);
  595. /*
  596. * We do this early, to ensure we are on the right CPU.
  597. */
  598. blk_mq_add_timer(rq);
  599. }
  600. void blk_mq_insert_request(struct request_queue *q, struct request *rq,
  601. bool run_queue)
  602. {
  603. struct blk_mq_hw_ctx *hctx;
  604. struct blk_mq_ctx *ctx, *current_ctx;
  605. ctx = rq->mq_ctx;
  606. hctx = q->mq_ops->map_queue(q, ctx->cpu);
  607. if (rq->cmd_flags & (REQ_FLUSH | REQ_FUA)) {
  608. blk_insert_flush(rq);
  609. } else {
  610. current_ctx = blk_mq_get_ctx(q);
  611. if (!cpu_online(ctx->cpu)) {
  612. ctx = current_ctx;
  613. hctx = q->mq_ops->map_queue(q, ctx->cpu);
  614. rq->mq_ctx = ctx;
  615. }
  616. spin_lock(&ctx->lock);
  617. __blk_mq_insert_request(hctx, rq);
  618. spin_unlock(&ctx->lock);
  619. blk_mq_put_ctx(current_ctx);
  620. }
  621. if (run_queue)
  622. __blk_mq_run_hw_queue(hctx);
  623. }
  624. EXPORT_SYMBOL(blk_mq_insert_request);
  625. /*
  626. * This is a special version of blk_mq_insert_request to bypass FLUSH request
  627. * check. Should only be used internally.
  628. */
  629. void blk_mq_run_request(struct request *rq, bool run_queue, bool async)
  630. {
  631. struct request_queue *q = rq->q;
  632. struct blk_mq_hw_ctx *hctx;
  633. struct blk_mq_ctx *ctx, *current_ctx;
  634. current_ctx = blk_mq_get_ctx(q);
  635. ctx = rq->mq_ctx;
  636. if (!cpu_online(ctx->cpu)) {
  637. ctx = current_ctx;
  638. rq->mq_ctx = ctx;
  639. }
  640. hctx = q->mq_ops->map_queue(q, ctx->cpu);
  641. /* ctx->cpu might be offline */
  642. spin_lock(&ctx->lock);
  643. __blk_mq_insert_request(hctx, rq);
  644. spin_unlock(&ctx->lock);
  645. blk_mq_put_ctx(current_ctx);
  646. if (run_queue)
  647. blk_mq_run_hw_queue(hctx, async);
  648. }
  649. static void blk_mq_insert_requests(struct request_queue *q,
  650. struct blk_mq_ctx *ctx,
  651. struct list_head *list,
  652. int depth,
  653. bool from_schedule)
  654. {
  655. struct blk_mq_hw_ctx *hctx;
  656. struct blk_mq_ctx *current_ctx;
  657. trace_block_unplug(q, depth, !from_schedule);
  658. current_ctx = blk_mq_get_ctx(q);
  659. if (!cpu_online(ctx->cpu))
  660. ctx = current_ctx;
  661. hctx = q->mq_ops->map_queue(q, ctx->cpu);
  662. /*
  663. * preemption doesn't flush plug list, so it's possible ctx->cpu is
  664. * offline now
  665. */
  666. spin_lock(&ctx->lock);
  667. while (!list_empty(list)) {
  668. struct request *rq;
  669. rq = list_first_entry(list, struct request, queuelist);
  670. list_del_init(&rq->queuelist);
  671. rq->mq_ctx = ctx;
  672. __blk_mq_insert_request(hctx, rq);
  673. }
  674. spin_unlock(&ctx->lock);
  675. blk_mq_put_ctx(current_ctx);
  676. blk_mq_run_hw_queue(hctx, from_schedule);
  677. }
  678. static int plug_ctx_cmp(void *priv, struct list_head *a, struct list_head *b)
  679. {
  680. struct request *rqa = container_of(a, struct request, queuelist);
  681. struct request *rqb = container_of(b, struct request, queuelist);
  682. return !(rqa->mq_ctx < rqb->mq_ctx ||
  683. (rqa->mq_ctx == rqb->mq_ctx &&
  684. blk_rq_pos(rqa) < blk_rq_pos(rqb)));
  685. }
  686. void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule)
  687. {
  688. struct blk_mq_ctx *this_ctx;
  689. struct request_queue *this_q;
  690. struct request *rq;
  691. LIST_HEAD(list);
  692. LIST_HEAD(ctx_list);
  693. unsigned int depth;
  694. list_splice_init(&plug->mq_list, &list);
  695. list_sort(NULL, &list, plug_ctx_cmp);
  696. this_q = NULL;
  697. this_ctx = NULL;
  698. depth = 0;
  699. while (!list_empty(&list)) {
  700. rq = list_entry_rq(list.next);
  701. list_del_init(&rq->queuelist);
  702. BUG_ON(!rq->q);
  703. if (rq->mq_ctx != this_ctx) {
  704. if (this_ctx) {
  705. blk_mq_insert_requests(this_q, this_ctx,
  706. &ctx_list, depth,
  707. from_schedule);
  708. }
  709. this_ctx = rq->mq_ctx;
  710. this_q = rq->q;
  711. depth = 0;
  712. }
  713. depth++;
  714. list_add_tail(&rq->queuelist, &ctx_list);
  715. }
  716. /*
  717. * If 'this_ctx' is set, we know we have entries to complete
  718. * on 'ctx_list'. Do those.
  719. */
  720. if (this_ctx) {
  721. blk_mq_insert_requests(this_q, this_ctx, &ctx_list, depth,
  722. from_schedule);
  723. }
  724. }
  725. static void blk_mq_bio_to_request(struct request *rq, struct bio *bio)
  726. {
  727. init_request_from_bio(rq, bio);
  728. blk_account_io_start(rq, 1);
  729. }
  730. static void blk_mq_make_request(struct request_queue *q, struct bio *bio)
  731. {
  732. struct blk_mq_hw_ctx *hctx;
  733. struct blk_mq_ctx *ctx;
  734. const int is_sync = rw_is_sync(bio->bi_rw);
  735. const int is_flush_fua = bio->bi_rw & (REQ_FLUSH | REQ_FUA);
  736. int rw = bio_data_dir(bio);
  737. struct request *rq;
  738. unsigned int use_plug, request_count = 0;
  739. /*
  740. * If we have multiple hardware queues, just go directly to
  741. * one of those for sync IO.
  742. */
  743. use_plug = !is_flush_fua && ((q->nr_hw_queues == 1) || !is_sync);
  744. blk_queue_bounce(q, &bio);
  745. if (use_plug && blk_attempt_plug_merge(q, bio, &request_count))
  746. return;
  747. if (blk_mq_queue_enter(q)) {
  748. bio_endio(bio, -EIO);
  749. return;
  750. }
  751. ctx = blk_mq_get_ctx(q);
  752. hctx = q->mq_ops->map_queue(q, ctx->cpu);
  753. trace_block_getrq(q, bio, rw);
  754. rq = __blk_mq_alloc_request(hctx, GFP_ATOMIC, false);
  755. if (likely(rq))
  756. blk_mq_rq_ctx_init(q, ctx, rq, rw);
  757. else {
  758. blk_mq_put_ctx(ctx);
  759. trace_block_sleeprq(q, bio, rw);
  760. rq = blk_mq_alloc_request_pinned(q, rw, __GFP_WAIT|GFP_ATOMIC,
  761. false);
  762. ctx = rq->mq_ctx;
  763. hctx = q->mq_ops->map_queue(q, ctx->cpu);
  764. }
  765. hctx->queued++;
  766. if (unlikely(is_flush_fua)) {
  767. blk_mq_bio_to_request(rq, bio);
  768. blk_mq_put_ctx(ctx);
  769. blk_insert_flush(rq);
  770. goto run_queue;
  771. }
  772. /*
  773. * A task plug currently exists. Since this is completely lockless,
  774. * utilize that to temporarily store requests until the task is
  775. * either done or scheduled away.
  776. */
  777. if (use_plug) {
  778. struct blk_plug *plug = current->plug;
  779. if (plug) {
  780. blk_mq_bio_to_request(rq, bio);
  781. if (list_empty(&plug->mq_list))
  782. trace_block_plug(q);
  783. else if (request_count >= BLK_MAX_REQUEST_COUNT) {
  784. blk_flush_plug_list(plug, false);
  785. trace_block_plug(q);
  786. }
  787. list_add_tail(&rq->queuelist, &plug->mq_list);
  788. blk_mq_put_ctx(ctx);
  789. return;
  790. }
  791. }
  792. spin_lock(&ctx->lock);
  793. if ((hctx->flags & BLK_MQ_F_SHOULD_MERGE) &&
  794. blk_mq_attempt_merge(q, ctx, bio))
  795. __blk_mq_free_request(hctx, ctx, rq);
  796. else {
  797. blk_mq_bio_to_request(rq, bio);
  798. __blk_mq_insert_request(hctx, rq);
  799. }
  800. spin_unlock(&ctx->lock);
  801. blk_mq_put_ctx(ctx);
  802. /*
  803. * For a SYNC request, send it to the hardware immediately. For an
  804. * ASYNC request, just ensure that we run it later on. The latter
  805. * allows for merging opportunities and more efficient dispatching.
  806. */
  807. run_queue:
  808. blk_mq_run_hw_queue(hctx, !is_sync || is_flush_fua);
  809. }
  810. /*
  811. * Default mapping to a software queue, since we use one per CPU.
  812. */
  813. struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *q, const int cpu)
  814. {
  815. return q->queue_hw_ctx[q->mq_map[cpu]];
  816. }
  817. EXPORT_SYMBOL(blk_mq_map_queue);
  818. struct blk_mq_hw_ctx *blk_mq_alloc_single_hw_queue(struct blk_mq_reg *reg,
  819. unsigned int hctx_index)
  820. {
  821. return kmalloc_node(sizeof(struct blk_mq_hw_ctx),
  822. GFP_KERNEL | __GFP_ZERO, reg->numa_node);
  823. }
  824. EXPORT_SYMBOL(blk_mq_alloc_single_hw_queue);
  825. void blk_mq_free_single_hw_queue(struct blk_mq_hw_ctx *hctx,
  826. unsigned int hctx_index)
  827. {
  828. kfree(hctx);
  829. }
  830. EXPORT_SYMBOL(blk_mq_free_single_hw_queue);
  831. static void blk_mq_hctx_notify(void *data, unsigned long action,
  832. unsigned int cpu)
  833. {
  834. struct blk_mq_hw_ctx *hctx = data;
  835. struct blk_mq_ctx *ctx;
  836. LIST_HEAD(tmp);
  837. if (action != CPU_DEAD && action != CPU_DEAD_FROZEN)
  838. return;
  839. /*
  840. * Move ctx entries to new CPU, if this one is going away.
  841. */
  842. ctx = __blk_mq_get_ctx(hctx->queue, cpu);
  843. spin_lock(&ctx->lock);
  844. if (!list_empty(&ctx->rq_list)) {
  845. list_splice_init(&ctx->rq_list, &tmp);
  846. clear_bit(ctx->index_hw, hctx->ctx_map);
  847. }
  848. spin_unlock(&ctx->lock);
  849. if (list_empty(&tmp))
  850. return;
  851. ctx = blk_mq_get_ctx(hctx->queue);
  852. spin_lock(&ctx->lock);
  853. while (!list_empty(&tmp)) {
  854. struct request *rq;
  855. rq = list_first_entry(&tmp, struct request, queuelist);
  856. rq->mq_ctx = ctx;
  857. list_move_tail(&rq->queuelist, &ctx->rq_list);
  858. }
  859. blk_mq_hctx_mark_pending(hctx, ctx);
  860. spin_unlock(&ctx->lock);
  861. blk_mq_put_ctx(ctx);
  862. }
  863. static void blk_mq_init_hw_commands(struct blk_mq_hw_ctx *hctx,
  864. void (*init)(void *, struct blk_mq_hw_ctx *,
  865. struct request *, unsigned int),
  866. void *data)
  867. {
  868. unsigned int i;
  869. for (i = 0; i < hctx->queue_depth; i++) {
  870. struct request *rq = hctx->rqs[i];
  871. init(data, hctx, rq, i);
  872. }
  873. }
  874. void blk_mq_init_commands(struct request_queue *q,
  875. void (*init)(void *, struct blk_mq_hw_ctx *,
  876. struct request *, unsigned int),
  877. void *data)
  878. {
  879. struct blk_mq_hw_ctx *hctx;
  880. unsigned int i;
  881. queue_for_each_hw_ctx(q, hctx, i)
  882. blk_mq_init_hw_commands(hctx, init, data);
  883. }
  884. EXPORT_SYMBOL(blk_mq_init_commands);
  885. static void blk_mq_free_rq_map(struct blk_mq_hw_ctx *hctx)
  886. {
  887. struct page *page;
  888. while (!list_empty(&hctx->page_list)) {
  889. page = list_first_entry(&hctx->page_list, struct page, list);
  890. list_del_init(&page->list);
  891. __free_pages(page, page->private);
  892. }
  893. kfree(hctx->rqs);
  894. if (hctx->tags)
  895. blk_mq_free_tags(hctx->tags);
  896. }
  897. static size_t order_to_size(unsigned int order)
  898. {
  899. size_t ret = PAGE_SIZE;
  900. while (order--)
  901. ret *= 2;
  902. return ret;
  903. }
  904. static int blk_mq_init_rq_map(struct blk_mq_hw_ctx *hctx,
  905. unsigned int reserved_tags, int node)
  906. {
  907. unsigned int i, j, entries_per_page, max_order = 4;
  908. size_t rq_size, left;
  909. INIT_LIST_HEAD(&hctx->page_list);
  910. hctx->rqs = kmalloc_node(hctx->queue_depth * sizeof(struct request *),
  911. GFP_KERNEL, node);
  912. if (!hctx->rqs)
  913. return -ENOMEM;
  914. /*
  915. * rq_size is the size of the request plus driver payload, rounded
  916. * to the cacheline size
  917. */
  918. rq_size = round_up(sizeof(struct request) + hctx->cmd_size,
  919. cache_line_size());
  920. left = rq_size * hctx->queue_depth;
  921. for (i = 0; i < hctx->queue_depth;) {
  922. int this_order = max_order;
  923. struct page *page;
  924. int to_do;
  925. void *p;
  926. while (left < order_to_size(this_order - 1) && this_order)
  927. this_order--;
  928. do {
  929. page = alloc_pages_node(node, GFP_KERNEL, this_order);
  930. if (page)
  931. break;
  932. if (!this_order--)
  933. break;
  934. if (order_to_size(this_order) < rq_size)
  935. break;
  936. } while (1);
  937. if (!page)
  938. break;
  939. page->private = this_order;
  940. list_add_tail(&page->list, &hctx->page_list);
  941. p = page_address(page);
  942. entries_per_page = order_to_size(this_order) / rq_size;
  943. to_do = min(entries_per_page, hctx->queue_depth - i);
  944. left -= to_do * rq_size;
  945. for (j = 0; j < to_do; j++) {
  946. hctx->rqs[i] = p;
  947. blk_mq_rq_init(hctx, hctx->rqs[i]);
  948. p += rq_size;
  949. i++;
  950. }
  951. }
  952. if (i < (reserved_tags + BLK_MQ_TAG_MIN))
  953. goto err_rq_map;
  954. else if (i != hctx->queue_depth) {
  955. hctx->queue_depth = i;
  956. pr_warn("%s: queue depth set to %u because of low memory\n",
  957. __func__, i);
  958. }
  959. hctx->tags = blk_mq_init_tags(hctx->queue_depth, reserved_tags, node);
  960. if (!hctx->tags) {
  961. err_rq_map:
  962. blk_mq_free_rq_map(hctx);
  963. return -ENOMEM;
  964. }
  965. return 0;
  966. }
  967. static int blk_mq_init_hw_queues(struct request_queue *q,
  968. struct blk_mq_reg *reg, void *driver_data)
  969. {
  970. struct blk_mq_hw_ctx *hctx;
  971. unsigned int i, j;
  972. /*
  973. * Initialize hardware queues
  974. */
  975. queue_for_each_hw_ctx(q, hctx, i) {
  976. unsigned int num_maps;
  977. int node;
  978. node = hctx->numa_node;
  979. if (node == NUMA_NO_NODE)
  980. node = hctx->numa_node = reg->numa_node;
  981. INIT_DELAYED_WORK(&hctx->delayed_work, blk_mq_work_fn);
  982. spin_lock_init(&hctx->lock);
  983. INIT_LIST_HEAD(&hctx->dispatch);
  984. hctx->queue = q;
  985. hctx->queue_num = i;
  986. hctx->flags = reg->flags;
  987. hctx->queue_depth = reg->queue_depth;
  988. hctx->cmd_size = reg->cmd_size;
  989. blk_mq_init_cpu_notifier(&hctx->cpu_notifier,
  990. blk_mq_hctx_notify, hctx);
  991. blk_mq_register_cpu_notifier(&hctx->cpu_notifier);
  992. if (blk_mq_init_rq_map(hctx, reg->reserved_tags, node))
  993. break;
  994. /*
  995. * Allocate space for all possible cpus to avoid allocation in
  996. * runtime
  997. */
  998. hctx->ctxs = kmalloc_node(nr_cpu_ids * sizeof(void *),
  999. GFP_KERNEL, node);
  1000. if (!hctx->ctxs)
  1001. break;
  1002. num_maps = ALIGN(nr_cpu_ids, BITS_PER_LONG) / BITS_PER_LONG;
  1003. hctx->ctx_map = kzalloc_node(num_maps * sizeof(unsigned long),
  1004. GFP_KERNEL, node);
  1005. if (!hctx->ctx_map)
  1006. break;
  1007. hctx->nr_ctx_map = num_maps;
  1008. hctx->nr_ctx = 0;
  1009. if (reg->ops->init_hctx &&
  1010. reg->ops->init_hctx(hctx, driver_data, i))
  1011. break;
  1012. }
  1013. if (i == q->nr_hw_queues)
  1014. return 0;
  1015. /*
  1016. * Init failed
  1017. */
  1018. queue_for_each_hw_ctx(q, hctx, j) {
  1019. if (i == j)
  1020. break;
  1021. if (reg->ops->exit_hctx)
  1022. reg->ops->exit_hctx(hctx, j);
  1023. blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
  1024. blk_mq_free_rq_map(hctx);
  1025. kfree(hctx->ctxs);
  1026. }
  1027. return 1;
  1028. }
  1029. static void blk_mq_init_cpu_queues(struct request_queue *q,
  1030. unsigned int nr_hw_queues)
  1031. {
  1032. unsigned int i;
  1033. for_each_possible_cpu(i) {
  1034. struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i);
  1035. struct blk_mq_hw_ctx *hctx;
  1036. memset(__ctx, 0, sizeof(*__ctx));
  1037. __ctx->cpu = i;
  1038. spin_lock_init(&__ctx->lock);
  1039. INIT_LIST_HEAD(&__ctx->rq_list);
  1040. __ctx->queue = q;
  1041. /* If the cpu isn't online, the cpu is mapped to first hctx */
  1042. hctx = q->mq_ops->map_queue(q, i);
  1043. hctx->nr_ctx++;
  1044. if (!cpu_online(i))
  1045. continue;
  1046. /*
  1047. * Set local node, IFF we have more than one hw queue. If
  1048. * not, we remain on the home node of the device
  1049. */
  1050. if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE)
  1051. hctx->numa_node = cpu_to_node(i);
  1052. }
  1053. }
  1054. static void blk_mq_map_swqueue(struct request_queue *q)
  1055. {
  1056. unsigned int i;
  1057. struct blk_mq_hw_ctx *hctx;
  1058. struct blk_mq_ctx *ctx;
  1059. queue_for_each_hw_ctx(q, hctx, i) {
  1060. hctx->nr_ctx = 0;
  1061. }
  1062. /*
  1063. * Map software to hardware queues
  1064. */
  1065. queue_for_each_ctx(q, ctx, i) {
  1066. /* If the cpu isn't online, the cpu is mapped to first hctx */
  1067. hctx = q->mq_ops->map_queue(q, i);
  1068. ctx->index_hw = hctx->nr_ctx;
  1069. hctx->ctxs[hctx->nr_ctx++] = ctx;
  1070. }
  1071. }
  1072. struct request_queue *blk_mq_init_queue(struct blk_mq_reg *reg,
  1073. void *driver_data)
  1074. {
  1075. struct blk_mq_hw_ctx **hctxs;
  1076. struct blk_mq_ctx *ctx;
  1077. struct request_queue *q;
  1078. int i;
  1079. if (!reg->nr_hw_queues ||
  1080. !reg->ops->queue_rq || !reg->ops->map_queue ||
  1081. !reg->ops->alloc_hctx || !reg->ops->free_hctx)
  1082. return ERR_PTR(-EINVAL);
  1083. if (!reg->queue_depth)
  1084. reg->queue_depth = BLK_MQ_MAX_DEPTH;
  1085. else if (reg->queue_depth > BLK_MQ_MAX_DEPTH) {
  1086. pr_err("blk-mq: queuedepth too large (%u)\n", reg->queue_depth);
  1087. reg->queue_depth = BLK_MQ_MAX_DEPTH;
  1088. }
  1089. /*
  1090. * Set aside a tag for flush requests. It will only be used while
  1091. * another flush request is in progress but outside the driver.
  1092. *
  1093. * TODO: only allocate if flushes are supported
  1094. */
  1095. reg->queue_depth++;
  1096. reg->reserved_tags++;
  1097. if (reg->queue_depth < (reg->reserved_tags + BLK_MQ_TAG_MIN))
  1098. return ERR_PTR(-EINVAL);
  1099. ctx = alloc_percpu(struct blk_mq_ctx);
  1100. if (!ctx)
  1101. return ERR_PTR(-ENOMEM);
  1102. hctxs = kmalloc_node(reg->nr_hw_queues * sizeof(*hctxs), GFP_KERNEL,
  1103. reg->numa_node);
  1104. if (!hctxs)
  1105. goto err_percpu;
  1106. for (i = 0; i < reg->nr_hw_queues; i++) {
  1107. hctxs[i] = reg->ops->alloc_hctx(reg, i);
  1108. if (!hctxs[i])
  1109. goto err_hctxs;
  1110. hctxs[i]->numa_node = NUMA_NO_NODE;
  1111. hctxs[i]->queue_num = i;
  1112. }
  1113. q = blk_alloc_queue_node(GFP_KERNEL, reg->numa_node);
  1114. if (!q)
  1115. goto err_hctxs;
  1116. q->mq_map = blk_mq_make_queue_map(reg);
  1117. if (!q->mq_map)
  1118. goto err_map;
  1119. setup_timer(&q->timeout, blk_mq_rq_timer, (unsigned long) q);
  1120. blk_queue_rq_timeout(q, 30000);
  1121. q->nr_queues = nr_cpu_ids;
  1122. q->nr_hw_queues = reg->nr_hw_queues;
  1123. q->queue_ctx = ctx;
  1124. q->queue_hw_ctx = hctxs;
  1125. q->mq_ops = reg->ops;
  1126. q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT;
  1127. blk_queue_make_request(q, blk_mq_make_request);
  1128. blk_queue_rq_timed_out(q, reg->ops->timeout);
  1129. if (reg->timeout)
  1130. blk_queue_rq_timeout(q, reg->timeout);
  1131. blk_mq_init_flush(q);
  1132. blk_mq_init_cpu_queues(q, reg->nr_hw_queues);
  1133. if (blk_mq_init_hw_queues(q, reg, driver_data))
  1134. goto err_hw;
  1135. blk_mq_map_swqueue(q);
  1136. mutex_lock(&all_q_mutex);
  1137. list_add_tail(&q->all_q_node, &all_q_list);
  1138. mutex_unlock(&all_q_mutex);
  1139. return q;
  1140. err_hw:
  1141. kfree(q->mq_map);
  1142. err_map:
  1143. blk_cleanup_queue(q);
  1144. err_hctxs:
  1145. for (i = 0; i < reg->nr_hw_queues; i++) {
  1146. if (!hctxs[i])
  1147. break;
  1148. reg->ops->free_hctx(hctxs[i], i);
  1149. }
  1150. kfree(hctxs);
  1151. err_percpu:
  1152. free_percpu(ctx);
  1153. return ERR_PTR(-ENOMEM);
  1154. }
  1155. EXPORT_SYMBOL(blk_mq_init_queue);
  1156. void blk_mq_free_queue(struct request_queue *q)
  1157. {
  1158. struct blk_mq_hw_ctx *hctx;
  1159. int i;
  1160. queue_for_each_hw_ctx(q, hctx, i) {
  1161. cancel_delayed_work_sync(&hctx->delayed_work);
  1162. kfree(hctx->ctx_map);
  1163. kfree(hctx->ctxs);
  1164. blk_mq_free_rq_map(hctx);
  1165. blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
  1166. if (q->mq_ops->exit_hctx)
  1167. q->mq_ops->exit_hctx(hctx, i);
  1168. q->mq_ops->free_hctx(hctx, i);
  1169. }
  1170. free_percpu(q->queue_ctx);
  1171. kfree(q->queue_hw_ctx);
  1172. kfree(q->mq_map);
  1173. q->queue_ctx = NULL;
  1174. q->queue_hw_ctx = NULL;
  1175. q->mq_map = NULL;
  1176. mutex_lock(&all_q_mutex);
  1177. list_del_init(&q->all_q_node);
  1178. mutex_unlock(&all_q_mutex);
  1179. }
  1180. EXPORT_SYMBOL(blk_mq_free_queue);
  1181. /* Basically redo blk_mq_init_queue with queue frozen */
  1182. static void blk_mq_queue_reinit(struct request_queue *q)
  1183. {
  1184. blk_mq_freeze_queue(q);
  1185. blk_mq_update_queue_map(q->mq_map, q->nr_hw_queues);
  1186. /*
  1187. * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe
  1188. * we should change hctx numa_node according to new topology (this
  1189. * involves free and re-allocate memory, worthy doing?)
  1190. */
  1191. blk_mq_map_swqueue(q);
  1192. blk_mq_unfreeze_queue(q);
  1193. }
  1194. static int blk_mq_queue_reinit_notify(struct notifier_block *nb,
  1195. unsigned long action, void *hcpu)
  1196. {
  1197. struct request_queue *q;
  1198. /*
  1199. * Before new mapping is established, hotadded cpu might already start
  1200. * handling requests. This doesn't break anything as we map offline
  1201. * CPUs to first hardware queue. We will re-init queue below to get
  1202. * optimal settings.
  1203. */
  1204. if (action != CPU_DEAD && action != CPU_DEAD_FROZEN &&
  1205. action != CPU_ONLINE && action != CPU_ONLINE_FROZEN)
  1206. return NOTIFY_OK;
  1207. mutex_lock(&all_q_mutex);
  1208. list_for_each_entry(q, &all_q_list, all_q_node)
  1209. blk_mq_queue_reinit(q);
  1210. mutex_unlock(&all_q_mutex);
  1211. return NOTIFY_OK;
  1212. }
  1213. static int __init blk_mq_init(void)
  1214. {
  1215. unsigned int i;
  1216. for_each_possible_cpu(i)
  1217. init_llist_head(&per_cpu(ipi_lists, i));
  1218. blk_mq_cpu_init();
  1219. /* Must be called after percpu_counter_hotcpu_callback() */
  1220. hotcpu_notifier(blk_mq_queue_reinit_notify, -10);
  1221. return 0;
  1222. }
  1223. subsys_initcall(blk_mq_init);