blk-mq.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490
  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_stop_hw_queues(struct request_queue *q)
  554. {
  555. struct blk_mq_hw_ctx *hctx;
  556. int i;
  557. queue_for_each_hw_ctx(q, hctx, i)
  558. blk_mq_stop_hw_queue(hctx);
  559. }
  560. EXPORT_SYMBOL(blk_mq_stop_hw_queues);
  561. void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx)
  562. {
  563. clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
  564. __blk_mq_run_hw_queue(hctx);
  565. }
  566. EXPORT_SYMBOL(blk_mq_start_hw_queue);
  567. void blk_mq_start_stopped_hw_queues(struct request_queue *q)
  568. {
  569. struct blk_mq_hw_ctx *hctx;
  570. int i;
  571. queue_for_each_hw_ctx(q, hctx, i) {
  572. if (!test_bit(BLK_MQ_S_STOPPED, &hctx->state))
  573. continue;
  574. clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
  575. blk_mq_run_hw_queue(hctx, true);
  576. }
  577. }
  578. EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues);
  579. static void blk_mq_work_fn(struct work_struct *work)
  580. {
  581. struct blk_mq_hw_ctx *hctx;
  582. hctx = container_of(work, struct blk_mq_hw_ctx, delayed_work.work);
  583. __blk_mq_run_hw_queue(hctx);
  584. }
  585. static void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx,
  586. struct request *rq)
  587. {
  588. struct blk_mq_ctx *ctx = rq->mq_ctx;
  589. list_add_tail(&rq->queuelist, &ctx->rq_list);
  590. blk_mq_hctx_mark_pending(hctx, ctx);
  591. /*
  592. * We do this early, to ensure we are on the right CPU.
  593. */
  594. blk_mq_add_timer(rq);
  595. }
  596. void blk_mq_insert_request(struct request_queue *q, struct request *rq,
  597. bool run_queue)
  598. {
  599. struct blk_mq_hw_ctx *hctx;
  600. struct blk_mq_ctx *ctx, *current_ctx;
  601. ctx = rq->mq_ctx;
  602. hctx = q->mq_ops->map_queue(q, ctx->cpu);
  603. if (rq->cmd_flags & (REQ_FLUSH | REQ_FUA)) {
  604. blk_insert_flush(rq);
  605. } else {
  606. current_ctx = blk_mq_get_ctx(q);
  607. if (!cpu_online(ctx->cpu)) {
  608. ctx = current_ctx;
  609. hctx = q->mq_ops->map_queue(q, ctx->cpu);
  610. rq->mq_ctx = ctx;
  611. }
  612. spin_lock(&ctx->lock);
  613. __blk_mq_insert_request(hctx, rq);
  614. spin_unlock(&ctx->lock);
  615. blk_mq_put_ctx(current_ctx);
  616. }
  617. if (run_queue)
  618. __blk_mq_run_hw_queue(hctx);
  619. }
  620. EXPORT_SYMBOL(blk_mq_insert_request);
  621. /*
  622. * This is a special version of blk_mq_insert_request to bypass FLUSH request
  623. * check. Should only be used internally.
  624. */
  625. void blk_mq_run_request(struct request *rq, bool run_queue, bool async)
  626. {
  627. struct request_queue *q = rq->q;
  628. struct blk_mq_hw_ctx *hctx;
  629. struct blk_mq_ctx *ctx, *current_ctx;
  630. current_ctx = blk_mq_get_ctx(q);
  631. ctx = rq->mq_ctx;
  632. if (!cpu_online(ctx->cpu)) {
  633. ctx = current_ctx;
  634. rq->mq_ctx = ctx;
  635. }
  636. hctx = q->mq_ops->map_queue(q, ctx->cpu);
  637. /* ctx->cpu might be offline */
  638. spin_lock(&ctx->lock);
  639. __blk_mq_insert_request(hctx, rq);
  640. spin_unlock(&ctx->lock);
  641. blk_mq_put_ctx(current_ctx);
  642. if (run_queue)
  643. blk_mq_run_hw_queue(hctx, async);
  644. }
  645. static void blk_mq_insert_requests(struct request_queue *q,
  646. struct blk_mq_ctx *ctx,
  647. struct list_head *list,
  648. int depth,
  649. bool from_schedule)
  650. {
  651. struct blk_mq_hw_ctx *hctx;
  652. struct blk_mq_ctx *current_ctx;
  653. trace_block_unplug(q, depth, !from_schedule);
  654. current_ctx = blk_mq_get_ctx(q);
  655. if (!cpu_online(ctx->cpu))
  656. ctx = current_ctx;
  657. hctx = q->mq_ops->map_queue(q, ctx->cpu);
  658. /*
  659. * preemption doesn't flush plug list, so it's possible ctx->cpu is
  660. * offline now
  661. */
  662. spin_lock(&ctx->lock);
  663. while (!list_empty(list)) {
  664. struct request *rq;
  665. rq = list_first_entry(list, struct request, queuelist);
  666. list_del_init(&rq->queuelist);
  667. rq->mq_ctx = ctx;
  668. __blk_mq_insert_request(hctx, rq);
  669. }
  670. spin_unlock(&ctx->lock);
  671. blk_mq_put_ctx(current_ctx);
  672. blk_mq_run_hw_queue(hctx, from_schedule);
  673. }
  674. static int plug_ctx_cmp(void *priv, struct list_head *a, struct list_head *b)
  675. {
  676. struct request *rqa = container_of(a, struct request, queuelist);
  677. struct request *rqb = container_of(b, struct request, queuelist);
  678. return !(rqa->mq_ctx < rqb->mq_ctx ||
  679. (rqa->mq_ctx == rqb->mq_ctx &&
  680. blk_rq_pos(rqa) < blk_rq_pos(rqb)));
  681. }
  682. void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule)
  683. {
  684. struct blk_mq_ctx *this_ctx;
  685. struct request_queue *this_q;
  686. struct request *rq;
  687. LIST_HEAD(list);
  688. LIST_HEAD(ctx_list);
  689. unsigned int depth;
  690. list_splice_init(&plug->mq_list, &list);
  691. list_sort(NULL, &list, plug_ctx_cmp);
  692. this_q = NULL;
  693. this_ctx = NULL;
  694. depth = 0;
  695. while (!list_empty(&list)) {
  696. rq = list_entry_rq(list.next);
  697. list_del_init(&rq->queuelist);
  698. BUG_ON(!rq->q);
  699. if (rq->mq_ctx != this_ctx) {
  700. if (this_ctx) {
  701. blk_mq_insert_requests(this_q, this_ctx,
  702. &ctx_list, depth,
  703. from_schedule);
  704. }
  705. this_ctx = rq->mq_ctx;
  706. this_q = rq->q;
  707. depth = 0;
  708. }
  709. depth++;
  710. list_add_tail(&rq->queuelist, &ctx_list);
  711. }
  712. /*
  713. * If 'this_ctx' is set, we know we have entries to complete
  714. * on 'ctx_list'. Do those.
  715. */
  716. if (this_ctx) {
  717. blk_mq_insert_requests(this_q, this_ctx, &ctx_list, depth,
  718. from_schedule);
  719. }
  720. }
  721. static void blk_mq_bio_to_request(struct request *rq, struct bio *bio)
  722. {
  723. init_request_from_bio(rq, bio);
  724. blk_account_io_start(rq, 1);
  725. }
  726. static void blk_mq_make_request(struct request_queue *q, struct bio *bio)
  727. {
  728. struct blk_mq_hw_ctx *hctx;
  729. struct blk_mq_ctx *ctx;
  730. const int is_sync = rw_is_sync(bio->bi_rw);
  731. const int is_flush_fua = bio->bi_rw & (REQ_FLUSH | REQ_FUA);
  732. int rw = bio_data_dir(bio);
  733. struct request *rq;
  734. unsigned int use_plug, request_count = 0;
  735. /*
  736. * If we have multiple hardware queues, just go directly to
  737. * one of those for sync IO.
  738. */
  739. use_plug = !is_flush_fua && ((q->nr_hw_queues == 1) || !is_sync);
  740. blk_queue_bounce(q, &bio);
  741. if (use_plug && blk_attempt_plug_merge(q, bio, &request_count))
  742. return;
  743. if (blk_mq_queue_enter(q)) {
  744. bio_endio(bio, -EIO);
  745. return;
  746. }
  747. ctx = blk_mq_get_ctx(q);
  748. hctx = q->mq_ops->map_queue(q, ctx->cpu);
  749. trace_block_getrq(q, bio, rw);
  750. rq = __blk_mq_alloc_request(hctx, GFP_ATOMIC, false);
  751. if (likely(rq))
  752. blk_mq_rq_ctx_init(ctx, rq, rw);
  753. else {
  754. blk_mq_put_ctx(ctx);
  755. trace_block_sleeprq(q, bio, rw);
  756. rq = blk_mq_alloc_request_pinned(q, rw, __GFP_WAIT|GFP_ATOMIC,
  757. false);
  758. ctx = rq->mq_ctx;
  759. hctx = q->mq_ops->map_queue(q, ctx->cpu);
  760. }
  761. hctx->queued++;
  762. if (unlikely(is_flush_fua)) {
  763. blk_mq_bio_to_request(rq, bio);
  764. blk_mq_put_ctx(ctx);
  765. blk_insert_flush(rq);
  766. goto run_queue;
  767. }
  768. /*
  769. * A task plug currently exists. Since this is completely lockless,
  770. * utilize that to temporarily store requests until the task is
  771. * either done or scheduled away.
  772. */
  773. if (use_plug) {
  774. struct blk_plug *plug = current->plug;
  775. if (plug) {
  776. blk_mq_bio_to_request(rq, bio);
  777. if (list_empty(&plug->list))
  778. trace_block_plug(q);
  779. else if (request_count >= BLK_MAX_REQUEST_COUNT) {
  780. blk_flush_plug_list(plug, false);
  781. trace_block_plug(q);
  782. }
  783. list_add_tail(&rq->queuelist, &plug->mq_list);
  784. blk_mq_put_ctx(ctx);
  785. return;
  786. }
  787. }
  788. spin_lock(&ctx->lock);
  789. if ((hctx->flags & BLK_MQ_F_SHOULD_MERGE) &&
  790. blk_mq_attempt_merge(q, ctx, bio))
  791. __blk_mq_free_request(hctx, ctx, rq);
  792. else {
  793. blk_mq_bio_to_request(rq, bio);
  794. __blk_mq_insert_request(hctx, rq);
  795. }
  796. spin_unlock(&ctx->lock);
  797. blk_mq_put_ctx(ctx);
  798. /*
  799. * For a SYNC request, send it to the hardware immediately. For an
  800. * ASYNC request, just ensure that we run it later on. The latter
  801. * allows for merging opportunities and more efficient dispatching.
  802. */
  803. run_queue:
  804. blk_mq_run_hw_queue(hctx, !is_sync || is_flush_fua);
  805. }
  806. /*
  807. * Default mapping to a software queue, since we use one per CPU.
  808. */
  809. struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *q, const int cpu)
  810. {
  811. return q->queue_hw_ctx[q->mq_map[cpu]];
  812. }
  813. EXPORT_SYMBOL(blk_mq_map_queue);
  814. struct blk_mq_hw_ctx *blk_mq_alloc_single_hw_queue(struct blk_mq_reg *reg,
  815. unsigned int hctx_index)
  816. {
  817. return kmalloc_node(sizeof(struct blk_mq_hw_ctx),
  818. GFP_KERNEL | __GFP_ZERO, reg->numa_node);
  819. }
  820. EXPORT_SYMBOL(blk_mq_alloc_single_hw_queue);
  821. void blk_mq_free_single_hw_queue(struct blk_mq_hw_ctx *hctx,
  822. unsigned int hctx_index)
  823. {
  824. kfree(hctx);
  825. }
  826. EXPORT_SYMBOL(blk_mq_free_single_hw_queue);
  827. static void blk_mq_hctx_notify(void *data, unsigned long action,
  828. unsigned int cpu)
  829. {
  830. struct blk_mq_hw_ctx *hctx = data;
  831. struct blk_mq_ctx *ctx;
  832. LIST_HEAD(tmp);
  833. if (action != CPU_DEAD && action != CPU_DEAD_FROZEN)
  834. return;
  835. /*
  836. * Move ctx entries to new CPU, if this one is going away.
  837. */
  838. ctx = __blk_mq_get_ctx(hctx->queue, cpu);
  839. spin_lock(&ctx->lock);
  840. if (!list_empty(&ctx->rq_list)) {
  841. list_splice_init(&ctx->rq_list, &tmp);
  842. clear_bit(ctx->index_hw, hctx->ctx_map);
  843. }
  844. spin_unlock(&ctx->lock);
  845. if (list_empty(&tmp))
  846. return;
  847. ctx = blk_mq_get_ctx(hctx->queue);
  848. spin_lock(&ctx->lock);
  849. while (!list_empty(&tmp)) {
  850. struct request *rq;
  851. rq = list_first_entry(&tmp, struct request, queuelist);
  852. rq->mq_ctx = ctx;
  853. list_move_tail(&rq->queuelist, &ctx->rq_list);
  854. }
  855. blk_mq_hctx_mark_pending(hctx, ctx);
  856. spin_unlock(&ctx->lock);
  857. blk_mq_put_ctx(ctx);
  858. }
  859. static void blk_mq_init_hw_commands(struct blk_mq_hw_ctx *hctx,
  860. void (*init)(void *, struct blk_mq_hw_ctx *,
  861. struct request *, unsigned int),
  862. void *data)
  863. {
  864. unsigned int i;
  865. for (i = 0; i < hctx->queue_depth; i++) {
  866. struct request *rq = hctx->rqs[i];
  867. init(data, hctx, rq, i);
  868. }
  869. }
  870. void blk_mq_init_commands(struct request_queue *q,
  871. void (*init)(void *, struct blk_mq_hw_ctx *,
  872. struct request *, unsigned int),
  873. void *data)
  874. {
  875. struct blk_mq_hw_ctx *hctx;
  876. unsigned int i;
  877. queue_for_each_hw_ctx(q, hctx, i)
  878. blk_mq_init_hw_commands(hctx, init, data);
  879. }
  880. EXPORT_SYMBOL(blk_mq_init_commands);
  881. static void blk_mq_free_rq_map(struct blk_mq_hw_ctx *hctx)
  882. {
  883. struct page *page;
  884. while (!list_empty(&hctx->page_list)) {
  885. page = list_first_entry(&hctx->page_list, struct page, list);
  886. list_del_init(&page->list);
  887. __free_pages(page, page->private);
  888. }
  889. kfree(hctx->rqs);
  890. if (hctx->tags)
  891. blk_mq_free_tags(hctx->tags);
  892. }
  893. static size_t order_to_size(unsigned int order)
  894. {
  895. size_t ret = PAGE_SIZE;
  896. while (order--)
  897. ret *= 2;
  898. return ret;
  899. }
  900. static int blk_mq_init_rq_map(struct blk_mq_hw_ctx *hctx,
  901. unsigned int reserved_tags, int node)
  902. {
  903. unsigned int i, j, entries_per_page, max_order = 4;
  904. size_t rq_size, left;
  905. INIT_LIST_HEAD(&hctx->page_list);
  906. hctx->rqs = kmalloc_node(hctx->queue_depth * sizeof(struct request *),
  907. GFP_KERNEL, node);
  908. if (!hctx->rqs)
  909. return -ENOMEM;
  910. /*
  911. * rq_size is the size of the request plus driver payload, rounded
  912. * to the cacheline size
  913. */
  914. rq_size = round_up(sizeof(struct request) + hctx->cmd_size,
  915. cache_line_size());
  916. left = rq_size * hctx->queue_depth;
  917. for (i = 0; i < hctx->queue_depth;) {
  918. int this_order = max_order;
  919. struct page *page;
  920. int to_do;
  921. void *p;
  922. while (left < order_to_size(this_order - 1) && this_order)
  923. this_order--;
  924. do {
  925. page = alloc_pages_node(node, GFP_KERNEL, this_order);
  926. if (page)
  927. break;
  928. if (!this_order--)
  929. break;
  930. if (order_to_size(this_order) < rq_size)
  931. break;
  932. } while (1);
  933. if (!page)
  934. break;
  935. page->private = this_order;
  936. list_add_tail(&page->list, &hctx->page_list);
  937. p = page_address(page);
  938. entries_per_page = order_to_size(this_order) / rq_size;
  939. to_do = min(entries_per_page, hctx->queue_depth - i);
  940. left -= to_do * rq_size;
  941. for (j = 0; j < to_do; j++) {
  942. hctx->rqs[i] = p;
  943. blk_mq_rq_init(hctx, hctx->rqs[i]);
  944. p += rq_size;
  945. i++;
  946. }
  947. }
  948. if (i < (reserved_tags + BLK_MQ_TAG_MIN))
  949. goto err_rq_map;
  950. else if (i != hctx->queue_depth) {
  951. hctx->queue_depth = i;
  952. pr_warn("%s: queue depth set to %u because of low memory\n",
  953. __func__, i);
  954. }
  955. hctx->tags = blk_mq_init_tags(hctx->queue_depth, reserved_tags, node);
  956. if (!hctx->tags) {
  957. err_rq_map:
  958. blk_mq_free_rq_map(hctx);
  959. return -ENOMEM;
  960. }
  961. return 0;
  962. }
  963. static int blk_mq_init_hw_queues(struct request_queue *q,
  964. struct blk_mq_reg *reg, void *driver_data)
  965. {
  966. struct blk_mq_hw_ctx *hctx;
  967. unsigned int i, j;
  968. /*
  969. * Initialize hardware queues
  970. */
  971. queue_for_each_hw_ctx(q, hctx, i) {
  972. unsigned int num_maps;
  973. int node;
  974. node = hctx->numa_node;
  975. if (node == NUMA_NO_NODE)
  976. node = hctx->numa_node = reg->numa_node;
  977. INIT_DELAYED_WORK(&hctx->delayed_work, blk_mq_work_fn);
  978. spin_lock_init(&hctx->lock);
  979. INIT_LIST_HEAD(&hctx->dispatch);
  980. hctx->queue = q;
  981. hctx->queue_num = i;
  982. hctx->flags = reg->flags;
  983. hctx->queue_depth = reg->queue_depth;
  984. hctx->cmd_size = reg->cmd_size;
  985. blk_mq_init_cpu_notifier(&hctx->cpu_notifier,
  986. blk_mq_hctx_notify, hctx);
  987. blk_mq_register_cpu_notifier(&hctx->cpu_notifier);
  988. if (blk_mq_init_rq_map(hctx, reg->reserved_tags, node))
  989. break;
  990. /*
  991. * Allocate space for all possible cpus to avoid allocation in
  992. * runtime
  993. */
  994. hctx->ctxs = kmalloc_node(nr_cpu_ids * sizeof(void *),
  995. GFP_KERNEL, node);
  996. if (!hctx->ctxs)
  997. break;
  998. num_maps = ALIGN(nr_cpu_ids, BITS_PER_LONG) / BITS_PER_LONG;
  999. hctx->ctx_map = kzalloc_node(num_maps * sizeof(unsigned long),
  1000. GFP_KERNEL, node);
  1001. if (!hctx->ctx_map)
  1002. break;
  1003. hctx->nr_ctx_map = num_maps;
  1004. hctx->nr_ctx = 0;
  1005. if (reg->ops->init_hctx &&
  1006. reg->ops->init_hctx(hctx, driver_data, i))
  1007. break;
  1008. }
  1009. if (i == q->nr_hw_queues)
  1010. return 0;
  1011. /*
  1012. * Init failed
  1013. */
  1014. queue_for_each_hw_ctx(q, hctx, j) {
  1015. if (i == j)
  1016. break;
  1017. if (reg->ops->exit_hctx)
  1018. reg->ops->exit_hctx(hctx, j);
  1019. blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
  1020. blk_mq_free_rq_map(hctx);
  1021. kfree(hctx->ctxs);
  1022. }
  1023. return 1;
  1024. }
  1025. static void blk_mq_init_cpu_queues(struct request_queue *q,
  1026. unsigned int nr_hw_queues)
  1027. {
  1028. unsigned int i;
  1029. for_each_possible_cpu(i) {
  1030. struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i);
  1031. struct blk_mq_hw_ctx *hctx;
  1032. memset(__ctx, 0, sizeof(*__ctx));
  1033. __ctx->cpu = i;
  1034. spin_lock_init(&__ctx->lock);
  1035. INIT_LIST_HEAD(&__ctx->rq_list);
  1036. __ctx->queue = q;
  1037. /* If the cpu isn't online, the cpu is mapped to first hctx */
  1038. hctx = q->mq_ops->map_queue(q, i);
  1039. hctx->nr_ctx++;
  1040. if (!cpu_online(i))
  1041. continue;
  1042. /*
  1043. * Set local node, IFF we have more than one hw queue. If
  1044. * not, we remain on the home node of the device
  1045. */
  1046. if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE)
  1047. hctx->numa_node = cpu_to_node(i);
  1048. }
  1049. }
  1050. static void blk_mq_map_swqueue(struct request_queue *q)
  1051. {
  1052. unsigned int i;
  1053. struct blk_mq_hw_ctx *hctx;
  1054. struct blk_mq_ctx *ctx;
  1055. queue_for_each_hw_ctx(q, hctx, i) {
  1056. hctx->nr_ctx = 0;
  1057. }
  1058. /*
  1059. * Map software to hardware queues
  1060. */
  1061. queue_for_each_ctx(q, ctx, i) {
  1062. /* If the cpu isn't online, the cpu is mapped to first hctx */
  1063. hctx = q->mq_ops->map_queue(q, i);
  1064. ctx->index_hw = hctx->nr_ctx;
  1065. hctx->ctxs[hctx->nr_ctx++] = ctx;
  1066. }
  1067. }
  1068. struct request_queue *blk_mq_init_queue(struct blk_mq_reg *reg,
  1069. void *driver_data)
  1070. {
  1071. struct blk_mq_hw_ctx **hctxs;
  1072. struct blk_mq_ctx *ctx;
  1073. struct request_queue *q;
  1074. int i;
  1075. if (!reg->nr_hw_queues ||
  1076. !reg->ops->queue_rq || !reg->ops->map_queue ||
  1077. !reg->ops->alloc_hctx || !reg->ops->free_hctx)
  1078. return ERR_PTR(-EINVAL);
  1079. if (!reg->queue_depth)
  1080. reg->queue_depth = BLK_MQ_MAX_DEPTH;
  1081. else if (reg->queue_depth > BLK_MQ_MAX_DEPTH) {
  1082. pr_err("blk-mq: queuedepth too large (%u)\n", reg->queue_depth);
  1083. reg->queue_depth = BLK_MQ_MAX_DEPTH;
  1084. }
  1085. if (reg->queue_depth < (reg->reserved_tags + BLK_MQ_TAG_MIN))
  1086. return ERR_PTR(-EINVAL);
  1087. ctx = alloc_percpu(struct blk_mq_ctx);
  1088. if (!ctx)
  1089. return ERR_PTR(-ENOMEM);
  1090. hctxs = kmalloc_node(reg->nr_hw_queues * sizeof(*hctxs), GFP_KERNEL,
  1091. reg->numa_node);
  1092. if (!hctxs)
  1093. goto err_percpu;
  1094. for (i = 0; i < reg->nr_hw_queues; i++) {
  1095. hctxs[i] = reg->ops->alloc_hctx(reg, i);
  1096. if (!hctxs[i])
  1097. goto err_hctxs;
  1098. hctxs[i]->numa_node = NUMA_NO_NODE;
  1099. hctxs[i]->queue_num = i;
  1100. }
  1101. q = blk_alloc_queue_node(GFP_KERNEL, reg->numa_node);
  1102. if (!q)
  1103. goto err_hctxs;
  1104. q->mq_map = blk_mq_make_queue_map(reg);
  1105. if (!q->mq_map)
  1106. goto err_map;
  1107. setup_timer(&q->timeout, blk_mq_rq_timer, (unsigned long) q);
  1108. blk_queue_rq_timeout(q, 30000);
  1109. q->nr_queues = nr_cpu_ids;
  1110. q->nr_hw_queues = reg->nr_hw_queues;
  1111. q->queue_ctx = ctx;
  1112. q->queue_hw_ctx = hctxs;
  1113. q->mq_ops = reg->ops;
  1114. blk_queue_make_request(q, blk_mq_make_request);
  1115. blk_queue_rq_timed_out(q, reg->ops->timeout);
  1116. if (reg->timeout)
  1117. blk_queue_rq_timeout(q, reg->timeout);
  1118. blk_mq_init_flush(q);
  1119. blk_mq_init_cpu_queues(q, reg->nr_hw_queues);
  1120. if (blk_mq_init_hw_queues(q, reg, driver_data))
  1121. goto err_hw;
  1122. blk_mq_map_swqueue(q);
  1123. mutex_lock(&all_q_mutex);
  1124. list_add_tail(&q->all_q_node, &all_q_list);
  1125. mutex_unlock(&all_q_mutex);
  1126. return q;
  1127. err_hw:
  1128. kfree(q->mq_map);
  1129. err_map:
  1130. blk_cleanup_queue(q);
  1131. err_hctxs:
  1132. for (i = 0; i < reg->nr_hw_queues; i++) {
  1133. if (!hctxs[i])
  1134. break;
  1135. reg->ops->free_hctx(hctxs[i], i);
  1136. }
  1137. kfree(hctxs);
  1138. err_percpu:
  1139. free_percpu(ctx);
  1140. return ERR_PTR(-ENOMEM);
  1141. }
  1142. EXPORT_SYMBOL(blk_mq_init_queue);
  1143. void blk_mq_free_queue(struct request_queue *q)
  1144. {
  1145. struct blk_mq_hw_ctx *hctx;
  1146. int i;
  1147. queue_for_each_hw_ctx(q, hctx, i) {
  1148. cancel_delayed_work_sync(&hctx->delayed_work);
  1149. kfree(hctx->ctx_map);
  1150. kfree(hctx->ctxs);
  1151. blk_mq_free_rq_map(hctx);
  1152. blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
  1153. if (q->mq_ops->exit_hctx)
  1154. q->mq_ops->exit_hctx(hctx, i);
  1155. q->mq_ops->free_hctx(hctx, i);
  1156. }
  1157. free_percpu(q->queue_ctx);
  1158. kfree(q->queue_hw_ctx);
  1159. kfree(q->mq_map);
  1160. q->queue_ctx = NULL;
  1161. q->queue_hw_ctx = NULL;
  1162. q->mq_map = NULL;
  1163. mutex_lock(&all_q_mutex);
  1164. list_del_init(&q->all_q_node);
  1165. mutex_unlock(&all_q_mutex);
  1166. }
  1167. EXPORT_SYMBOL(blk_mq_free_queue);
  1168. /* Basically redo blk_mq_init_queue with queue frozen */
  1169. static void __cpuinit blk_mq_queue_reinit(struct request_queue *q)
  1170. {
  1171. blk_mq_freeze_queue(q);
  1172. blk_mq_update_queue_map(q->mq_map, q->nr_hw_queues);
  1173. /*
  1174. * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe
  1175. * we should change hctx numa_node according to new topology (this
  1176. * involves free and re-allocate memory, worthy doing?)
  1177. */
  1178. blk_mq_map_swqueue(q);
  1179. blk_mq_unfreeze_queue(q);
  1180. }
  1181. static int __cpuinit blk_mq_queue_reinit_notify(struct notifier_block *nb,
  1182. unsigned long action, void *hcpu)
  1183. {
  1184. struct request_queue *q;
  1185. /*
  1186. * Before new mapping is established, hotadded cpu might already start
  1187. * handling requests. This doesn't break anything as we map offline
  1188. * CPUs to first hardware queue. We will re-init queue below to get
  1189. * optimal settings.
  1190. */
  1191. if (action != CPU_DEAD && action != CPU_DEAD_FROZEN &&
  1192. action != CPU_ONLINE && action != CPU_ONLINE_FROZEN)
  1193. return NOTIFY_OK;
  1194. mutex_lock(&all_q_mutex);
  1195. list_for_each_entry(q, &all_q_list, all_q_node)
  1196. blk_mq_queue_reinit(q);
  1197. mutex_unlock(&all_q_mutex);
  1198. return NOTIFY_OK;
  1199. }
  1200. static int __init blk_mq_init(void)
  1201. {
  1202. unsigned int i;
  1203. for_each_possible_cpu(i)
  1204. init_llist_head(&per_cpu(ipi_lists, i));
  1205. blk_mq_cpu_init();
  1206. /* Must be called after percpu_counter_hotcpu_callback() */
  1207. hotcpu_notifier(blk_mq_queue_reinit_notify, -10);
  1208. return 0;
  1209. }
  1210. subsys_initcall(blk_mq_init);