blk-mq.c 34 KB

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