blk-mq.c 33 KB

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