blk-mq.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035
  1. /*
  2. * Block multiqueue core code
  3. *
  4. * Copyright (C) 2013-2014 Jens Axboe
  5. * Copyright (C) 2013-2014 Christoph Hellwig
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/backing-dev.h>
  10. #include <linux/bio.h>
  11. #include <linux/blkdev.h>
  12. #include <linux/mm.h>
  13. #include <linux/init.h>
  14. #include <linux/slab.h>
  15. #include <linux/workqueue.h>
  16. #include <linux/smp.h>
  17. #include <linux/llist.h>
  18. #include <linux/list_sort.h>
  19. #include <linux/cpu.h>
  20. #include <linux/cache.h>
  21. #include <linux/sched/sysctl.h>
  22. #include <linux/delay.h>
  23. #include <trace/events/block.h>
  24. #include <linux/blk-mq.h>
  25. #include "blk.h"
  26. #include "blk-mq.h"
  27. #include "blk-mq-tag.h"
  28. static DEFINE_MUTEX(all_q_mutex);
  29. static LIST_HEAD(all_q_list);
  30. static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx);
  31. /*
  32. * Check if any of the ctx's have pending work in this hardware queue
  33. */
  34. static bool blk_mq_hctx_has_pending(struct blk_mq_hw_ctx *hctx)
  35. {
  36. unsigned int i;
  37. for (i = 0; i < hctx->ctx_map.map_size; i++)
  38. if (hctx->ctx_map.map[i].word)
  39. return true;
  40. return false;
  41. }
  42. static inline struct blk_align_bitmap *get_bm(struct blk_mq_hw_ctx *hctx,
  43. struct blk_mq_ctx *ctx)
  44. {
  45. return &hctx->ctx_map.map[ctx->index_hw / hctx->ctx_map.bits_per_word];
  46. }
  47. #define CTX_TO_BIT(hctx, ctx) \
  48. ((ctx)->index_hw & ((hctx)->ctx_map.bits_per_word - 1))
  49. /*
  50. * Mark this ctx as having pending work in this hardware queue
  51. */
  52. static void blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx *hctx,
  53. struct blk_mq_ctx *ctx)
  54. {
  55. struct blk_align_bitmap *bm = get_bm(hctx, ctx);
  56. if (!test_bit(CTX_TO_BIT(hctx, ctx), &bm->word))
  57. set_bit(CTX_TO_BIT(hctx, ctx), &bm->word);
  58. }
  59. static void blk_mq_hctx_clear_pending(struct blk_mq_hw_ctx *hctx,
  60. struct blk_mq_ctx *ctx)
  61. {
  62. struct blk_align_bitmap *bm = get_bm(hctx, ctx);
  63. clear_bit(CTX_TO_BIT(hctx, ctx), &bm->word);
  64. }
  65. static int blk_mq_queue_enter(struct request_queue *q)
  66. {
  67. while (true) {
  68. int ret;
  69. if (percpu_ref_tryget_live(&q->mq_usage_counter))
  70. return 0;
  71. ret = wait_event_interruptible(q->mq_freeze_wq,
  72. !q->mq_freeze_depth || blk_queue_dying(q));
  73. if (blk_queue_dying(q))
  74. return -ENODEV;
  75. if (ret)
  76. return ret;
  77. }
  78. }
  79. static void blk_mq_queue_exit(struct request_queue *q)
  80. {
  81. percpu_ref_put(&q->mq_usage_counter);
  82. }
  83. static void blk_mq_usage_counter_release(struct percpu_ref *ref)
  84. {
  85. struct request_queue *q =
  86. container_of(ref, struct request_queue, mq_usage_counter);
  87. wake_up_all(&q->mq_freeze_wq);
  88. }
  89. /*
  90. * Guarantee no request is in use, so we can change any data structure of
  91. * the queue afterward.
  92. */
  93. void blk_mq_freeze_queue(struct request_queue *q)
  94. {
  95. spin_lock_irq(q->queue_lock);
  96. q->mq_freeze_depth++;
  97. spin_unlock_irq(q->queue_lock);
  98. percpu_ref_kill(&q->mq_usage_counter);
  99. blk_mq_run_queues(q, false);
  100. wait_event(q->mq_freeze_wq, percpu_ref_is_zero(&q->mq_usage_counter));
  101. }
  102. static void blk_mq_unfreeze_queue(struct request_queue *q)
  103. {
  104. bool wake = false;
  105. spin_lock_irq(q->queue_lock);
  106. wake = !--q->mq_freeze_depth;
  107. WARN_ON_ONCE(q->mq_freeze_depth < 0);
  108. spin_unlock_irq(q->queue_lock);
  109. if (wake) {
  110. percpu_ref_reinit(&q->mq_usage_counter);
  111. wake_up_all(&q->mq_freeze_wq);
  112. }
  113. }
  114. bool blk_mq_can_queue(struct blk_mq_hw_ctx *hctx)
  115. {
  116. return blk_mq_has_free_tags(hctx->tags);
  117. }
  118. EXPORT_SYMBOL(blk_mq_can_queue);
  119. static void blk_mq_rq_ctx_init(struct request_queue *q, struct blk_mq_ctx *ctx,
  120. struct request *rq, unsigned int rw_flags)
  121. {
  122. if (blk_queue_io_stat(q))
  123. rw_flags |= REQ_IO_STAT;
  124. INIT_LIST_HEAD(&rq->queuelist);
  125. /* csd/requeue_work/fifo_time is initialized before use */
  126. rq->q = q;
  127. rq->mq_ctx = ctx;
  128. rq->cmd_flags |= rw_flags;
  129. /* do not touch atomic flags, it needs atomic ops against the timer */
  130. rq->cpu = -1;
  131. INIT_HLIST_NODE(&rq->hash);
  132. RB_CLEAR_NODE(&rq->rb_node);
  133. rq->rq_disk = NULL;
  134. rq->part = NULL;
  135. rq->start_time = jiffies;
  136. #ifdef CONFIG_BLK_CGROUP
  137. rq->rl = NULL;
  138. set_start_time_ns(rq);
  139. rq->io_start_time_ns = 0;
  140. #endif
  141. rq->nr_phys_segments = 0;
  142. #if defined(CONFIG_BLK_DEV_INTEGRITY)
  143. rq->nr_integrity_segments = 0;
  144. #endif
  145. rq->special = NULL;
  146. /* tag was already set */
  147. rq->errors = 0;
  148. rq->extra_len = 0;
  149. rq->sense_len = 0;
  150. rq->resid_len = 0;
  151. rq->sense = NULL;
  152. INIT_LIST_HEAD(&rq->timeout_list);
  153. rq->timeout = 0;
  154. rq->end_io = NULL;
  155. rq->end_io_data = NULL;
  156. rq->next_rq = NULL;
  157. ctx->rq_dispatched[rw_is_sync(rw_flags)]++;
  158. }
  159. static struct request *
  160. __blk_mq_alloc_request(struct blk_mq_alloc_data *data, int rw)
  161. {
  162. struct request *rq;
  163. unsigned int tag;
  164. tag = blk_mq_get_tag(data);
  165. if (tag != BLK_MQ_TAG_FAIL) {
  166. rq = data->hctx->tags->rqs[tag];
  167. rq->cmd_flags = 0;
  168. if (blk_mq_tag_busy(data->hctx)) {
  169. rq->cmd_flags = REQ_MQ_INFLIGHT;
  170. atomic_inc(&data->hctx->nr_active);
  171. }
  172. rq->tag = tag;
  173. blk_mq_rq_ctx_init(data->q, data->ctx, rq, rw);
  174. return rq;
  175. }
  176. return NULL;
  177. }
  178. struct request *blk_mq_alloc_request(struct request_queue *q, int rw, gfp_t gfp,
  179. bool reserved)
  180. {
  181. struct blk_mq_ctx *ctx;
  182. struct blk_mq_hw_ctx *hctx;
  183. struct request *rq;
  184. struct blk_mq_alloc_data alloc_data;
  185. if (blk_mq_queue_enter(q))
  186. return NULL;
  187. ctx = blk_mq_get_ctx(q);
  188. hctx = q->mq_ops->map_queue(q, ctx->cpu);
  189. blk_mq_set_alloc_data(&alloc_data, q, gfp & ~__GFP_WAIT,
  190. reserved, ctx, hctx);
  191. rq = __blk_mq_alloc_request(&alloc_data, rw);
  192. if (!rq && (gfp & __GFP_WAIT)) {
  193. __blk_mq_run_hw_queue(hctx);
  194. blk_mq_put_ctx(ctx);
  195. ctx = blk_mq_get_ctx(q);
  196. hctx = q->mq_ops->map_queue(q, ctx->cpu);
  197. blk_mq_set_alloc_data(&alloc_data, q, gfp, reserved, ctx,
  198. hctx);
  199. rq = __blk_mq_alloc_request(&alloc_data, rw);
  200. ctx = alloc_data.ctx;
  201. }
  202. blk_mq_put_ctx(ctx);
  203. return rq;
  204. }
  205. EXPORT_SYMBOL(blk_mq_alloc_request);
  206. static void __blk_mq_free_request(struct blk_mq_hw_ctx *hctx,
  207. struct blk_mq_ctx *ctx, struct request *rq)
  208. {
  209. const int tag = rq->tag;
  210. struct request_queue *q = rq->q;
  211. if (rq->cmd_flags & REQ_MQ_INFLIGHT)
  212. atomic_dec(&hctx->nr_active);
  213. clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
  214. blk_mq_put_tag(hctx, tag, &ctx->last_tag);
  215. blk_mq_queue_exit(q);
  216. }
  217. void blk_mq_free_request(struct request *rq)
  218. {
  219. struct blk_mq_ctx *ctx = rq->mq_ctx;
  220. struct blk_mq_hw_ctx *hctx;
  221. struct request_queue *q = rq->q;
  222. ctx->rq_completed[rq_is_sync(rq)]++;
  223. hctx = q->mq_ops->map_queue(q, ctx->cpu);
  224. __blk_mq_free_request(hctx, ctx, rq);
  225. }
  226. /*
  227. * Clone all relevant state from a request that has been put on hold in
  228. * the flush state machine into the preallocated flush request that hangs
  229. * off the request queue.
  230. *
  231. * For a driver the flush request should be invisible, that's why we are
  232. * impersonating the original request here.
  233. */
  234. void blk_mq_clone_flush_request(struct request *flush_rq,
  235. struct request *orig_rq)
  236. {
  237. struct blk_mq_hw_ctx *hctx =
  238. orig_rq->q->mq_ops->map_queue(orig_rq->q, orig_rq->mq_ctx->cpu);
  239. flush_rq->mq_ctx = orig_rq->mq_ctx;
  240. flush_rq->tag = orig_rq->tag;
  241. memcpy(blk_mq_rq_to_pdu(flush_rq), blk_mq_rq_to_pdu(orig_rq),
  242. hctx->cmd_size);
  243. }
  244. inline void __blk_mq_end_io(struct request *rq, int error)
  245. {
  246. blk_account_io_done(rq);
  247. if (rq->end_io) {
  248. rq->end_io(rq, error);
  249. } else {
  250. if (unlikely(blk_bidi_rq(rq)))
  251. blk_mq_free_request(rq->next_rq);
  252. blk_mq_free_request(rq);
  253. }
  254. }
  255. EXPORT_SYMBOL(__blk_mq_end_io);
  256. void blk_mq_end_io(struct request *rq, int error)
  257. {
  258. if (blk_update_request(rq, error, blk_rq_bytes(rq)))
  259. BUG();
  260. __blk_mq_end_io(rq, error);
  261. }
  262. EXPORT_SYMBOL(blk_mq_end_io);
  263. static void __blk_mq_complete_request_remote(void *data)
  264. {
  265. struct request *rq = data;
  266. rq->q->softirq_done_fn(rq);
  267. }
  268. static void blk_mq_ipi_complete_request(struct request *rq)
  269. {
  270. struct blk_mq_ctx *ctx = rq->mq_ctx;
  271. bool shared = false;
  272. int cpu;
  273. if (!test_bit(QUEUE_FLAG_SAME_COMP, &rq->q->queue_flags)) {
  274. rq->q->softirq_done_fn(rq);
  275. return;
  276. }
  277. cpu = get_cpu();
  278. if (!test_bit(QUEUE_FLAG_SAME_FORCE, &rq->q->queue_flags))
  279. shared = cpus_share_cache(cpu, ctx->cpu);
  280. if (cpu != ctx->cpu && !shared && cpu_online(ctx->cpu)) {
  281. rq->csd.func = __blk_mq_complete_request_remote;
  282. rq->csd.info = rq;
  283. rq->csd.flags = 0;
  284. smp_call_function_single_async(ctx->cpu, &rq->csd);
  285. } else {
  286. rq->q->softirq_done_fn(rq);
  287. }
  288. put_cpu();
  289. }
  290. void __blk_mq_complete_request(struct request *rq)
  291. {
  292. struct request_queue *q = rq->q;
  293. if (!q->softirq_done_fn)
  294. blk_mq_end_io(rq, rq->errors);
  295. else
  296. blk_mq_ipi_complete_request(rq);
  297. }
  298. /**
  299. * blk_mq_complete_request - end I/O on a request
  300. * @rq: the request being processed
  301. *
  302. * Description:
  303. * Ends all I/O on a request. It does not handle partial completions.
  304. * The actual completion happens out-of-order, through a IPI handler.
  305. **/
  306. void blk_mq_complete_request(struct request *rq)
  307. {
  308. struct request_queue *q = rq->q;
  309. if (unlikely(blk_should_fake_timeout(q)))
  310. return;
  311. if (!blk_mark_rq_complete(rq))
  312. __blk_mq_complete_request(rq);
  313. }
  314. EXPORT_SYMBOL(blk_mq_complete_request);
  315. static void blk_mq_start_request(struct request *rq, bool last)
  316. {
  317. struct request_queue *q = rq->q;
  318. trace_block_rq_issue(q, rq);
  319. rq->resid_len = blk_rq_bytes(rq);
  320. if (unlikely(blk_bidi_rq(rq)))
  321. rq->next_rq->resid_len = blk_rq_bytes(rq->next_rq);
  322. blk_add_timer(rq);
  323. /*
  324. * Mark us as started and clear complete. Complete might have been
  325. * set if requeue raced with timeout, which then marked it as
  326. * complete. So be sure to clear complete again when we start
  327. * the request, otherwise we'll ignore the completion event.
  328. */
  329. if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
  330. set_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
  331. if (test_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags))
  332. clear_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags);
  333. if (q->dma_drain_size && blk_rq_bytes(rq)) {
  334. /*
  335. * Make sure space for the drain appears. We know we can do
  336. * this because max_hw_segments has been adjusted to be one
  337. * fewer than the device can handle.
  338. */
  339. rq->nr_phys_segments++;
  340. }
  341. /*
  342. * Flag the last request in the series so that drivers know when IO
  343. * should be kicked off, if they don't do it on a per-request basis.
  344. *
  345. * Note: the flag isn't the only condition drivers should do kick off.
  346. * If drive is busy, the last request might not have the bit set.
  347. */
  348. if (last)
  349. rq->cmd_flags |= REQ_END;
  350. }
  351. static void __blk_mq_requeue_request(struct request *rq)
  352. {
  353. struct request_queue *q = rq->q;
  354. trace_block_rq_requeue(q, rq);
  355. clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
  356. rq->cmd_flags &= ~REQ_END;
  357. if (q->dma_drain_size && blk_rq_bytes(rq))
  358. rq->nr_phys_segments--;
  359. }
  360. void blk_mq_requeue_request(struct request *rq)
  361. {
  362. __blk_mq_requeue_request(rq);
  363. blk_clear_rq_complete(rq);
  364. BUG_ON(blk_queued_rq(rq));
  365. blk_mq_add_to_requeue_list(rq, true);
  366. }
  367. EXPORT_SYMBOL(blk_mq_requeue_request);
  368. static void blk_mq_requeue_work(struct work_struct *work)
  369. {
  370. struct request_queue *q =
  371. container_of(work, struct request_queue, requeue_work);
  372. LIST_HEAD(rq_list);
  373. struct request *rq, *next;
  374. unsigned long flags;
  375. spin_lock_irqsave(&q->requeue_lock, flags);
  376. list_splice_init(&q->requeue_list, &rq_list);
  377. spin_unlock_irqrestore(&q->requeue_lock, flags);
  378. list_for_each_entry_safe(rq, next, &rq_list, queuelist) {
  379. if (!(rq->cmd_flags & REQ_SOFTBARRIER))
  380. continue;
  381. rq->cmd_flags &= ~REQ_SOFTBARRIER;
  382. list_del_init(&rq->queuelist);
  383. blk_mq_insert_request(rq, true, false, false);
  384. }
  385. while (!list_empty(&rq_list)) {
  386. rq = list_entry(rq_list.next, struct request, queuelist);
  387. list_del_init(&rq->queuelist);
  388. blk_mq_insert_request(rq, false, false, false);
  389. }
  390. blk_mq_run_queues(q, false);
  391. }
  392. void blk_mq_add_to_requeue_list(struct request *rq, bool at_head)
  393. {
  394. struct request_queue *q = rq->q;
  395. unsigned long flags;
  396. /*
  397. * We abuse this flag that is otherwise used by the I/O scheduler to
  398. * request head insertation from the workqueue.
  399. */
  400. BUG_ON(rq->cmd_flags & REQ_SOFTBARRIER);
  401. spin_lock_irqsave(&q->requeue_lock, flags);
  402. if (at_head) {
  403. rq->cmd_flags |= REQ_SOFTBARRIER;
  404. list_add(&rq->queuelist, &q->requeue_list);
  405. } else {
  406. list_add_tail(&rq->queuelist, &q->requeue_list);
  407. }
  408. spin_unlock_irqrestore(&q->requeue_lock, flags);
  409. }
  410. EXPORT_SYMBOL(blk_mq_add_to_requeue_list);
  411. void blk_mq_kick_requeue_list(struct request_queue *q)
  412. {
  413. kblockd_schedule_work(&q->requeue_work);
  414. }
  415. EXPORT_SYMBOL(blk_mq_kick_requeue_list);
  416. static inline bool is_flush_request(struct request *rq, unsigned int tag)
  417. {
  418. return ((rq->cmd_flags & REQ_FLUSH_SEQ) &&
  419. rq->q->flush_rq->tag == tag);
  420. }
  421. struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags, unsigned int tag)
  422. {
  423. struct request *rq = tags->rqs[tag];
  424. if (!is_flush_request(rq, tag))
  425. return rq;
  426. return rq->q->flush_rq;
  427. }
  428. EXPORT_SYMBOL(blk_mq_tag_to_rq);
  429. struct blk_mq_timeout_data {
  430. struct blk_mq_hw_ctx *hctx;
  431. unsigned long *next;
  432. unsigned int *next_set;
  433. };
  434. static void blk_mq_timeout_check(void *__data, unsigned long *free_tags)
  435. {
  436. struct blk_mq_timeout_data *data = __data;
  437. struct blk_mq_hw_ctx *hctx = data->hctx;
  438. unsigned int tag;
  439. /* It may not be in flight yet (this is where
  440. * the REQ_ATOMIC_STARTED flag comes in). The requests are
  441. * statically allocated, so we know it's always safe to access the
  442. * memory associated with a bit offset into ->rqs[].
  443. */
  444. tag = 0;
  445. do {
  446. struct request *rq;
  447. tag = find_next_zero_bit(free_tags, hctx->tags->nr_tags, tag);
  448. if (tag >= hctx->tags->nr_tags)
  449. break;
  450. rq = blk_mq_tag_to_rq(hctx->tags, tag++);
  451. if (rq->q != hctx->queue)
  452. continue;
  453. if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
  454. continue;
  455. blk_rq_check_expired(rq, data->next, data->next_set);
  456. } while (1);
  457. }
  458. static void blk_mq_hw_ctx_check_timeout(struct blk_mq_hw_ctx *hctx,
  459. unsigned long *next,
  460. unsigned int *next_set)
  461. {
  462. struct blk_mq_timeout_data data = {
  463. .hctx = hctx,
  464. .next = next,
  465. .next_set = next_set,
  466. };
  467. /*
  468. * Ask the tagging code to iterate busy requests, so we can
  469. * check them for timeout.
  470. */
  471. blk_mq_tag_busy_iter(hctx->tags, blk_mq_timeout_check, &data);
  472. }
  473. static enum blk_eh_timer_return blk_mq_rq_timed_out(struct request *rq)
  474. {
  475. struct request_queue *q = rq->q;
  476. /*
  477. * We know that complete is set at this point. If STARTED isn't set
  478. * anymore, then the request isn't active and the "timeout" should
  479. * just be ignored. This can happen due to the bitflag ordering.
  480. * Timeout first checks if STARTED is set, and if it is, assumes
  481. * the request is active. But if we race with completion, then
  482. * we both flags will get cleared. So check here again, and ignore
  483. * a timeout event with a request that isn't active.
  484. */
  485. if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
  486. return BLK_EH_NOT_HANDLED;
  487. if (!q->mq_ops->timeout)
  488. return BLK_EH_RESET_TIMER;
  489. return q->mq_ops->timeout(rq);
  490. }
  491. static void blk_mq_rq_timer(unsigned long data)
  492. {
  493. struct request_queue *q = (struct request_queue *) data;
  494. struct blk_mq_hw_ctx *hctx;
  495. unsigned long next = 0;
  496. int i, next_set = 0;
  497. queue_for_each_hw_ctx(q, hctx, i) {
  498. /*
  499. * If not software queues are currently mapped to this
  500. * hardware queue, there's nothing to check
  501. */
  502. if (!hctx->nr_ctx || !hctx->tags)
  503. continue;
  504. blk_mq_hw_ctx_check_timeout(hctx, &next, &next_set);
  505. }
  506. if (next_set) {
  507. next = blk_rq_timeout(round_jiffies_up(next));
  508. mod_timer(&q->timeout, next);
  509. } else {
  510. queue_for_each_hw_ctx(q, hctx, i)
  511. blk_mq_tag_idle(hctx);
  512. }
  513. }
  514. /*
  515. * Reverse check our software queue for entries that we could potentially
  516. * merge with. Currently includes a hand-wavy stop count of 8, to not spend
  517. * too much time checking for merges.
  518. */
  519. static bool blk_mq_attempt_merge(struct request_queue *q,
  520. struct blk_mq_ctx *ctx, struct bio *bio)
  521. {
  522. struct request *rq;
  523. int checked = 8;
  524. list_for_each_entry_reverse(rq, &ctx->rq_list, queuelist) {
  525. int el_ret;
  526. if (!checked--)
  527. break;
  528. if (!blk_rq_merge_ok(rq, bio))
  529. continue;
  530. el_ret = blk_try_merge(rq, bio);
  531. if (el_ret == ELEVATOR_BACK_MERGE) {
  532. if (bio_attempt_back_merge(q, rq, bio)) {
  533. ctx->rq_merged++;
  534. return true;
  535. }
  536. break;
  537. } else if (el_ret == ELEVATOR_FRONT_MERGE) {
  538. if (bio_attempt_front_merge(q, rq, bio)) {
  539. ctx->rq_merged++;
  540. return true;
  541. }
  542. break;
  543. }
  544. }
  545. return false;
  546. }
  547. /*
  548. * Process software queues that have been marked busy, splicing them
  549. * to the for-dispatch
  550. */
  551. static void flush_busy_ctxs(struct blk_mq_hw_ctx *hctx, struct list_head *list)
  552. {
  553. struct blk_mq_ctx *ctx;
  554. int i;
  555. for (i = 0; i < hctx->ctx_map.map_size; i++) {
  556. struct blk_align_bitmap *bm = &hctx->ctx_map.map[i];
  557. unsigned int off, bit;
  558. if (!bm->word)
  559. continue;
  560. bit = 0;
  561. off = i * hctx->ctx_map.bits_per_word;
  562. do {
  563. bit = find_next_bit(&bm->word, bm->depth, bit);
  564. if (bit >= bm->depth)
  565. break;
  566. ctx = hctx->ctxs[bit + off];
  567. clear_bit(bit, &bm->word);
  568. spin_lock(&ctx->lock);
  569. list_splice_tail_init(&ctx->rq_list, list);
  570. spin_unlock(&ctx->lock);
  571. bit++;
  572. } while (1);
  573. }
  574. }
  575. /*
  576. * Run this hardware queue, pulling any software queues mapped to it in.
  577. * Note that this function currently has various problems around ordering
  578. * of IO. In particular, we'd like FIFO behaviour on handling existing
  579. * items on the hctx->dispatch list. Ignore that for now.
  580. */
  581. static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx)
  582. {
  583. struct request_queue *q = hctx->queue;
  584. struct request *rq;
  585. LIST_HEAD(rq_list);
  586. int queued;
  587. WARN_ON(!cpumask_test_cpu(raw_smp_processor_id(), hctx->cpumask));
  588. if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state)))
  589. return;
  590. hctx->run++;
  591. /*
  592. * Touch any software queue that has pending entries.
  593. */
  594. flush_busy_ctxs(hctx, &rq_list);
  595. /*
  596. * If we have previous entries on our dispatch list, grab them
  597. * and stuff them at the front for more fair dispatch.
  598. */
  599. if (!list_empty_careful(&hctx->dispatch)) {
  600. spin_lock(&hctx->lock);
  601. if (!list_empty(&hctx->dispatch))
  602. list_splice_init(&hctx->dispatch, &rq_list);
  603. spin_unlock(&hctx->lock);
  604. }
  605. /*
  606. * Now process all the entries, sending them to the driver.
  607. */
  608. queued = 0;
  609. while (!list_empty(&rq_list)) {
  610. int ret;
  611. rq = list_first_entry(&rq_list, struct request, queuelist);
  612. list_del_init(&rq->queuelist);
  613. blk_mq_start_request(rq, list_empty(&rq_list));
  614. ret = q->mq_ops->queue_rq(hctx, rq);
  615. switch (ret) {
  616. case BLK_MQ_RQ_QUEUE_OK:
  617. queued++;
  618. continue;
  619. case BLK_MQ_RQ_QUEUE_BUSY:
  620. list_add(&rq->queuelist, &rq_list);
  621. __blk_mq_requeue_request(rq);
  622. break;
  623. default:
  624. pr_err("blk-mq: bad return on queue: %d\n", ret);
  625. case BLK_MQ_RQ_QUEUE_ERROR:
  626. rq->errors = -EIO;
  627. blk_mq_end_io(rq, rq->errors);
  628. break;
  629. }
  630. if (ret == BLK_MQ_RQ_QUEUE_BUSY)
  631. break;
  632. }
  633. if (!queued)
  634. hctx->dispatched[0]++;
  635. else if (queued < (1 << (BLK_MQ_MAX_DISPATCH_ORDER - 1)))
  636. hctx->dispatched[ilog2(queued) + 1]++;
  637. /*
  638. * Any items that need requeuing? Stuff them into hctx->dispatch,
  639. * that is where we will continue on next queue run.
  640. */
  641. if (!list_empty(&rq_list)) {
  642. spin_lock(&hctx->lock);
  643. list_splice(&rq_list, &hctx->dispatch);
  644. spin_unlock(&hctx->lock);
  645. }
  646. }
  647. /*
  648. * It'd be great if the workqueue API had a way to pass
  649. * in a mask and had some smarts for more clever placement.
  650. * For now we just round-robin here, switching for every
  651. * BLK_MQ_CPU_WORK_BATCH queued items.
  652. */
  653. static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx)
  654. {
  655. int cpu = hctx->next_cpu;
  656. if (--hctx->next_cpu_batch <= 0) {
  657. int next_cpu;
  658. next_cpu = cpumask_next(hctx->next_cpu, hctx->cpumask);
  659. if (next_cpu >= nr_cpu_ids)
  660. next_cpu = cpumask_first(hctx->cpumask);
  661. hctx->next_cpu = next_cpu;
  662. hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
  663. }
  664. return cpu;
  665. }
  666. void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
  667. {
  668. if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state)))
  669. return;
  670. if (!async && cpumask_test_cpu(smp_processor_id(), hctx->cpumask))
  671. __blk_mq_run_hw_queue(hctx);
  672. else if (hctx->queue->nr_hw_queues == 1)
  673. kblockd_schedule_delayed_work(&hctx->run_work, 0);
  674. else {
  675. unsigned int cpu;
  676. cpu = blk_mq_hctx_next_cpu(hctx);
  677. kblockd_schedule_delayed_work_on(cpu, &hctx->run_work, 0);
  678. }
  679. }
  680. void blk_mq_run_queues(struct request_queue *q, bool async)
  681. {
  682. struct blk_mq_hw_ctx *hctx;
  683. int i;
  684. queue_for_each_hw_ctx(q, hctx, i) {
  685. if ((!blk_mq_hctx_has_pending(hctx) &&
  686. list_empty_careful(&hctx->dispatch)) ||
  687. test_bit(BLK_MQ_S_STOPPED, &hctx->state))
  688. continue;
  689. preempt_disable();
  690. blk_mq_run_hw_queue(hctx, async);
  691. preempt_enable();
  692. }
  693. }
  694. EXPORT_SYMBOL(blk_mq_run_queues);
  695. void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx)
  696. {
  697. cancel_delayed_work(&hctx->run_work);
  698. cancel_delayed_work(&hctx->delay_work);
  699. set_bit(BLK_MQ_S_STOPPED, &hctx->state);
  700. }
  701. EXPORT_SYMBOL(blk_mq_stop_hw_queue);
  702. void blk_mq_stop_hw_queues(struct request_queue *q)
  703. {
  704. struct blk_mq_hw_ctx *hctx;
  705. int i;
  706. queue_for_each_hw_ctx(q, hctx, i)
  707. blk_mq_stop_hw_queue(hctx);
  708. }
  709. EXPORT_SYMBOL(blk_mq_stop_hw_queues);
  710. void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx)
  711. {
  712. clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
  713. preempt_disable();
  714. blk_mq_run_hw_queue(hctx, false);
  715. preempt_enable();
  716. }
  717. EXPORT_SYMBOL(blk_mq_start_hw_queue);
  718. void blk_mq_start_hw_queues(struct request_queue *q)
  719. {
  720. struct blk_mq_hw_ctx *hctx;
  721. int i;
  722. queue_for_each_hw_ctx(q, hctx, i)
  723. blk_mq_start_hw_queue(hctx);
  724. }
  725. EXPORT_SYMBOL(blk_mq_start_hw_queues);
  726. void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async)
  727. {
  728. struct blk_mq_hw_ctx *hctx;
  729. int i;
  730. queue_for_each_hw_ctx(q, hctx, i) {
  731. if (!test_bit(BLK_MQ_S_STOPPED, &hctx->state))
  732. continue;
  733. clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
  734. preempt_disable();
  735. blk_mq_run_hw_queue(hctx, async);
  736. preempt_enable();
  737. }
  738. }
  739. EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues);
  740. static void blk_mq_run_work_fn(struct work_struct *work)
  741. {
  742. struct blk_mq_hw_ctx *hctx;
  743. hctx = container_of(work, struct blk_mq_hw_ctx, run_work.work);
  744. __blk_mq_run_hw_queue(hctx);
  745. }
  746. static void blk_mq_delay_work_fn(struct work_struct *work)
  747. {
  748. struct blk_mq_hw_ctx *hctx;
  749. hctx = container_of(work, struct blk_mq_hw_ctx, delay_work.work);
  750. if (test_and_clear_bit(BLK_MQ_S_STOPPED, &hctx->state))
  751. __blk_mq_run_hw_queue(hctx);
  752. }
  753. void blk_mq_delay_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs)
  754. {
  755. unsigned long tmo = msecs_to_jiffies(msecs);
  756. if (hctx->queue->nr_hw_queues == 1)
  757. kblockd_schedule_delayed_work(&hctx->delay_work, tmo);
  758. else {
  759. unsigned int cpu;
  760. cpu = blk_mq_hctx_next_cpu(hctx);
  761. kblockd_schedule_delayed_work_on(cpu, &hctx->delay_work, tmo);
  762. }
  763. }
  764. EXPORT_SYMBOL(blk_mq_delay_queue);
  765. static void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx,
  766. struct request *rq, bool at_head)
  767. {
  768. struct blk_mq_ctx *ctx = rq->mq_ctx;
  769. trace_block_rq_insert(hctx->queue, rq);
  770. if (at_head)
  771. list_add(&rq->queuelist, &ctx->rq_list);
  772. else
  773. list_add_tail(&rq->queuelist, &ctx->rq_list);
  774. blk_mq_hctx_mark_pending(hctx, ctx);
  775. }
  776. void blk_mq_insert_request(struct request *rq, bool at_head, bool run_queue,
  777. bool async)
  778. {
  779. struct request_queue *q = rq->q;
  780. struct blk_mq_hw_ctx *hctx;
  781. struct blk_mq_ctx *ctx = rq->mq_ctx, *current_ctx;
  782. current_ctx = blk_mq_get_ctx(q);
  783. if (!cpu_online(ctx->cpu))
  784. rq->mq_ctx = ctx = current_ctx;
  785. hctx = q->mq_ops->map_queue(q, ctx->cpu);
  786. if (rq->cmd_flags & (REQ_FLUSH | REQ_FUA) &&
  787. !(rq->cmd_flags & (REQ_FLUSH_SEQ))) {
  788. blk_insert_flush(rq);
  789. } else {
  790. spin_lock(&ctx->lock);
  791. __blk_mq_insert_request(hctx, rq, at_head);
  792. spin_unlock(&ctx->lock);
  793. }
  794. if (run_queue)
  795. blk_mq_run_hw_queue(hctx, async);
  796. blk_mq_put_ctx(current_ctx);
  797. }
  798. static void blk_mq_insert_requests(struct request_queue *q,
  799. struct blk_mq_ctx *ctx,
  800. struct list_head *list,
  801. int depth,
  802. bool from_schedule)
  803. {
  804. struct blk_mq_hw_ctx *hctx;
  805. struct blk_mq_ctx *current_ctx;
  806. trace_block_unplug(q, depth, !from_schedule);
  807. current_ctx = blk_mq_get_ctx(q);
  808. if (!cpu_online(ctx->cpu))
  809. ctx = current_ctx;
  810. hctx = q->mq_ops->map_queue(q, ctx->cpu);
  811. /*
  812. * preemption doesn't flush plug list, so it's possible ctx->cpu is
  813. * offline now
  814. */
  815. spin_lock(&ctx->lock);
  816. while (!list_empty(list)) {
  817. struct request *rq;
  818. rq = list_first_entry(list, struct request, queuelist);
  819. list_del_init(&rq->queuelist);
  820. rq->mq_ctx = ctx;
  821. __blk_mq_insert_request(hctx, rq, false);
  822. }
  823. spin_unlock(&ctx->lock);
  824. blk_mq_run_hw_queue(hctx, from_schedule);
  825. blk_mq_put_ctx(current_ctx);
  826. }
  827. static int plug_ctx_cmp(void *priv, struct list_head *a, struct list_head *b)
  828. {
  829. struct request *rqa = container_of(a, struct request, queuelist);
  830. struct request *rqb = container_of(b, struct request, queuelist);
  831. return !(rqa->mq_ctx < rqb->mq_ctx ||
  832. (rqa->mq_ctx == rqb->mq_ctx &&
  833. blk_rq_pos(rqa) < blk_rq_pos(rqb)));
  834. }
  835. void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule)
  836. {
  837. struct blk_mq_ctx *this_ctx;
  838. struct request_queue *this_q;
  839. struct request *rq;
  840. LIST_HEAD(list);
  841. LIST_HEAD(ctx_list);
  842. unsigned int depth;
  843. list_splice_init(&plug->mq_list, &list);
  844. list_sort(NULL, &list, plug_ctx_cmp);
  845. this_q = NULL;
  846. this_ctx = NULL;
  847. depth = 0;
  848. while (!list_empty(&list)) {
  849. rq = list_entry_rq(list.next);
  850. list_del_init(&rq->queuelist);
  851. BUG_ON(!rq->q);
  852. if (rq->mq_ctx != this_ctx) {
  853. if (this_ctx) {
  854. blk_mq_insert_requests(this_q, this_ctx,
  855. &ctx_list, depth,
  856. from_schedule);
  857. }
  858. this_ctx = rq->mq_ctx;
  859. this_q = rq->q;
  860. depth = 0;
  861. }
  862. depth++;
  863. list_add_tail(&rq->queuelist, &ctx_list);
  864. }
  865. /*
  866. * If 'this_ctx' is set, we know we have entries to complete
  867. * on 'ctx_list'. Do those.
  868. */
  869. if (this_ctx) {
  870. blk_mq_insert_requests(this_q, this_ctx, &ctx_list, depth,
  871. from_schedule);
  872. }
  873. }
  874. static void blk_mq_bio_to_request(struct request *rq, struct bio *bio)
  875. {
  876. init_request_from_bio(rq, bio);
  877. if (blk_do_io_stat(rq))
  878. blk_account_io_start(rq, 1);
  879. }
  880. static inline bool blk_mq_merge_queue_io(struct blk_mq_hw_ctx *hctx,
  881. struct blk_mq_ctx *ctx,
  882. struct request *rq, struct bio *bio)
  883. {
  884. struct request_queue *q = hctx->queue;
  885. if (!(hctx->flags & BLK_MQ_F_SHOULD_MERGE)) {
  886. blk_mq_bio_to_request(rq, bio);
  887. spin_lock(&ctx->lock);
  888. insert_rq:
  889. __blk_mq_insert_request(hctx, rq, false);
  890. spin_unlock(&ctx->lock);
  891. return false;
  892. } else {
  893. spin_lock(&ctx->lock);
  894. if (!blk_mq_attempt_merge(q, ctx, bio)) {
  895. blk_mq_bio_to_request(rq, bio);
  896. goto insert_rq;
  897. }
  898. spin_unlock(&ctx->lock);
  899. __blk_mq_free_request(hctx, ctx, rq);
  900. return true;
  901. }
  902. }
  903. struct blk_map_ctx {
  904. struct blk_mq_hw_ctx *hctx;
  905. struct blk_mq_ctx *ctx;
  906. };
  907. static struct request *blk_mq_map_request(struct request_queue *q,
  908. struct bio *bio,
  909. struct blk_map_ctx *data)
  910. {
  911. struct blk_mq_hw_ctx *hctx;
  912. struct blk_mq_ctx *ctx;
  913. struct request *rq;
  914. int rw = bio_data_dir(bio);
  915. struct blk_mq_alloc_data alloc_data;
  916. if (unlikely(blk_mq_queue_enter(q))) {
  917. bio_endio(bio, -EIO);
  918. return NULL;
  919. }
  920. ctx = blk_mq_get_ctx(q);
  921. hctx = q->mq_ops->map_queue(q, ctx->cpu);
  922. if (rw_is_sync(bio->bi_rw))
  923. rw |= REQ_SYNC;
  924. trace_block_getrq(q, bio, rw);
  925. blk_mq_set_alloc_data(&alloc_data, q, GFP_ATOMIC, false, ctx,
  926. hctx);
  927. rq = __blk_mq_alloc_request(&alloc_data, rw);
  928. if (unlikely(!rq)) {
  929. __blk_mq_run_hw_queue(hctx);
  930. blk_mq_put_ctx(ctx);
  931. trace_block_sleeprq(q, bio, rw);
  932. ctx = blk_mq_get_ctx(q);
  933. hctx = q->mq_ops->map_queue(q, ctx->cpu);
  934. blk_mq_set_alloc_data(&alloc_data, q,
  935. __GFP_WAIT|GFP_ATOMIC, false, ctx, hctx);
  936. rq = __blk_mq_alloc_request(&alloc_data, rw);
  937. ctx = alloc_data.ctx;
  938. hctx = alloc_data.hctx;
  939. }
  940. hctx->queued++;
  941. data->hctx = hctx;
  942. data->ctx = ctx;
  943. return rq;
  944. }
  945. /*
  946. * Multiple hardware queue variant. This will not use per-process plugs,
  947. * but will attempt to bypass the hctx queueing if we can go straight to
  948. * hardware for SYNC IO.
  949. */
  950. static void blk_mq_make_request(struct request_queue *q, struct bio *bio)
  951. {
  952. const int is_sync = rw_is_sync(bio->bi_rw);
  953. const int is_flush_fua = bio->bi_rw & (REQ_FLUSH | REQ_FUA);
  954. struct blk_map_ctx data;
  955. struct request *rq;
  956. blk_queue_bounce(q, &bio);
  957. if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
  958. bio_endio(bio, -EIO);
  959. return;
  960. }
  961. rq = blk_mq_map_request(q, bio, &data);
  962. if (unlikely(!rq))
  963. return;
  964. if (unlikely(is_flush_fua)) {
  965. blk_mq_bio_to_request(rq, bio);
  966. blk_insert_flush(rq);
  967. goto run_queue;
  968. }
  969. if (is_sync) {
  970. int ret;
  971. blk_mq_bio_to_request(rq, bio);
  972. blk_mq_start_request(rq, true);
  973. /*
  974. * For OK queue, we are done. For error, kill it. Any other
  975. * error (busy), just add it to our list as we previously
  976. * would have done
  977. */
  978. ret = q->mq_ops->queue_rq(data.hctx, rq);
  979. if (ret == BLK_MQ_RQ_QUEUE_OK)
  980. goto done;
  981. else {
  982. __blk_mq_requeue_request(rq);
  983. if (ret == BLK_MQ_RQ_QUEUE_ERROR) {
  984. rq->errors = -EIO;
  985. blk_mq_end_io(rq, rq->errors);
  986. goto done;
  987. }
  988. }
  989. }
  990. if (!blk_mq_merge_queue_io(data.hctx, data.ctx, rq, bio)) {
  991. /*
  992. * For a SYNC request, send it to the hardware immediately. For
  993. * an ASYNC request, just ensure that we run it later on. The
  994. * latter allows for merging opportunities and more efficient
  995. * dispatching.
  996. */
  997. run_queue:
  998. blk_mq_run_hw_queue(data.hctx, !is_sync || is_flush_fua);
  999. }
  1000. done:
  1001. blk_mq_put_ctx(data.ctx);
  1002. }
  1003. /*
  1004. * Single hardware queue variant. This will attempt to use any per-process
  1005. * plug for merging and IO deferral.
  1006. */
  1007. static void blk_sq_make_request(struct request_queue *q, struct bio *bio)
  1008. {
  1009. const int is_sync = rw_is_sync(bio->bi_rw);
  1010. const int is_flush_fua = bio->bi_rw & (REQ_FLUSH | REQ_FUA);
  1011. unsigned int use_plug, request_count = 0;
  1012. struct blk_map_ctx data;
  1013. struct request *rq;
  1014. /*
  1015. * If we have multiple hardware queues, just go directly to
  1016. * one of those for sync IO.
  1017. */
  1018. use_plug = !is_flush_fua && !is_sync;
  1019. blk_queue_bounce(q, &bio);
  1020. if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
  1021. bio_endio(bio, -EIO);
  1022. return;
  1023. }
  1024. if (use_plug && !blk_queue_nomerges(q) &&
  1025. blk_attempt_plug_merge(q, bio, &request_count))
  1026. return;
  1027. rq = blk_mq_map_request(q, bio, &data);
  1028. if (unlikely(!rq))
  1029. return;
  1030. if (unlikely(is_flush_fua)) {
  1031. blk_mq_bio_to_request(rq, bio);
  1032. blk_insert_flush(rq);
  1033. goto run_queue;
  1034. }
  1035. /*
  1036. * A task plug currently exists. Since this is completely lockless,
  1037. * utilize that to temporarily store requests until the task is
  1038. * either done or scheduled away.
  1039. */
  1040. if (use_plug) {
  1041. struct blk_plug *plug = current->plug;
  1042. if (plug) {
  1043. blk_mq_bio_to_request(rq, bio);
  1044. if (list_empty(&plug->mq_list))
  1045. trace_block_plug(q);
  1046. else if (request_count >= BLK_MAX_REQUEST_COUNT) {
  1047. blk_flush_plug_list(plug, false);
  1048. trace_block_plug(q);
  1049. }
  1050. list_add_tail(&rq->queuelist, &plug->mq_list);
  1051. blk_mq_put_ctx(data.ctx);
  1052. return;
  1053. }
  1054. }
  1055. if (!blk_mq_merge_queue_io(data.hctx, data.ctx, rq, bio)) {
  1056. /*
  1057. * For a SYNC request, send it to the hardware immediately. For
  1058. * an ASYNC request, just ensure that we run it later on. The
  1059. * latter allows for merging opportunities and more efficient
  1060. * dispatching.
  1061. */
  1062. run_queue:
  1063. blk_mq_run_hw_queue(data.hctx, !is_sync || is_flush_fua);
  1064. }
  1065. blk_mq_put_ctx(data.ctx);
  1066. }
  1067. /*
  1068. * Default mapping to a software queue, since we use one per CPU.
  1069. */
  1070. struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *q, const int cpu)
  1071. {
  1072. return q->queue_hw_ctx[q->mq_map[cpu]];
  1073. }
  1074. EXPORT_SYMBOL(blk_mq_map_queue);
  1075. static void blk_mq_free_rq_map(struct blk_mq_tag_set *set,
  1076. struct blk_mq_tags *tags, unsigned int hctx_idx)
  1077. {
  1078. struct page *page;
  1079. if (tags->rqs && set->ops->exit_request) {
  1080. int i;
  1081. for (i = 0; i < tags->nr_tags; i++) {
  1082. if (!tags->rqs[i])
  1083. continue;
  1084. set->ops->exit_request(set->driver_data, tags->rqs[i],
  1085. hctx_idx, i);
  1086. }
  1087. }
  1088. while (!list_empty(&tags->page_list)) {
  1089. page = list_first_entry(&tags->page_list, struct page, lru);
  1090. list_del_init(&page->lru);
  1091. __free_pages(page, page->private);
  1092. }
  1093. kfree(tags->rqs);
  1094. blk_mq_free_tags(tags);
  1095. }
  1096. static size_t order_to_size(unsigned int order)
  1097. {
  1098. return (size_t)PAGE_SIZE << order;
  1099. }
  1100. static struct blk_mq_tags *blk_mq_init_rq_map(struct blk_mq_tag_set *set,
  1101. unsigned int hctx_idx)
  1102. {
  1103. struct blk_mq_tags *tags;
  1104. unsigned int i, j, entries_per_page, max_order = 4;
  1105. size_t rq_size, left;
  1106. tags = blk_mq_init_tags(set->queue_depth, set->reserved_tags,
  1107. set->numa_node);
  1108. if (!tags)
  1109. return NULL;
  1110. INIT_LIST_HEAD(&tags->page_list);
  1111. tags->rqs = kmalloc_node(set->queue_depth * sizeof(struct request *),
  1112. GFP_KERNEL, set->numa_node);
  1113. if (!tags->rqs) {
  1114. blk_mq_free_tags(tags);
  1115. return NULL;
  1116. }
  1117. /*
  1118. * rq_size is the size of the request plus driver payload, rounded
  1119. * to the cacheline size
  1120. */
  1121. rq_size = round_up(sizeof(struct request) + set->cmd_size,
  1122. cache_line_size());
  1123. left = rq_size * set->queue_depth;
  1124. for (i = 0; i < set->queue_depth; ) {
  1125. int this_order = max_order;
  1126. struct page *page;
  1127. int to_do;
  1128. void *p;
  1129. while (left < order_to_size(this_order - 1) && this_order)
  1130. this_order--;
  1131. do {
  1132. page = alloc_pages_node(set->numa_node, GFP_KERNEL,
  1133. this_order);
  1134. if (page)
  1135. break;
  1136. if (!this_order--)
  1137. break;
  1138. if (order_to_size(this_order) < rq_size)
  1139. break;
  1140. } while (1);
  1141. if (!page)
  1142. goto fail;
  1143. page->private = this_order;
  1144. list_add_tail(&page->lru, &tags->page_list);
  1145. p = page_address(page);
  1146. entries_per_page = order_to_size(this_order) / rq_size;
  1147. to_do = min(entries_per_page, set->queue_depth - i);
  1148. left -= to_do * rq_size;
  1149. for (j = 0; j < to_do; j++) {
  1150. tags->rqs[i] = p;
  1151. if (set->ops->init_request) {
  1152. if (set->ops->init_request(set->driver_data,
  1153. tags->rqs[i], hctx_idx, i,
  1154. set->numa_node))
  1155. goto fail;
  1156. }
  1157. p += rq_size;
  1158. i++;
  1159. }
  1160. }
  1161. return tags;
  1162. fail:
  1163. pr_warn("%s: failed to allocate requests\n", __func__);
  1164. blk_mq_free_rq_map(set, tags, hctx_idx);
  1165. return NULL;
  1166. }
  1167. static void blk_mq_free_bitmap(struct blk_mq_ctxmap *bitmap)
  1168. {
  1169. kfree(bitmap->map);
  1170. }
  1171. static int blk_mq_alloc_bitmap(struct blk_mq_ctxmap *bitmap, int node)
  1172. {
  1173. unsigned int bpw = 8, total, num_maps, i;
  1174. bitmap->bits_per_word = bpw;
  1175. num_maps = ALIGN(nr_cpu_ids, bpw) / bpw;
  1176. bitmap->map = kzalloc_node(num_maps * sizeof(struct blk_align_bitmap),
  1177. GFP_KERNEL, node);
  1178. if (!bitmap->map)
  1179. return -ENOMEM;
  1180. bitmap->map_size = num_maps;
  1181. total = nr_cpu_ids;
  1182. for (i = 0; i < num_maps; i++) {
  1183. bitmap->map[i].depth = min(total, bitmap->bits_per_word);
  1184. total -= bitmap->map[i].depth;
  1185. }
  1186. return 0;
  1187. }
  1188. static int blk_mq_hctx_cpu_offline(struct blk_mq_hw_ctx *hctx, int cpu)
  1189. {
  1190. struct request_queue *q = hctx->queue;
  1191. struct blk_mq_ctx *ctx;
  1192. LIST_HEAD(tmp);
  1193. /*
  1194. * Move ctx entries to new CPU, if this one is going away.
  1195. */
  1196. ctx = __blk_mq_get_ctx(q, cpu);
  1197. spin_lock(&ctx->lock);
  1198. if (!list_empty(&ctx->rq_list)) {
  1199. list_splice_init(&ctx->rq_list, &tmp);
  1200. blk_mq_hctx_clear_pending(hctx, ctx);
  1201. }
  1202. spin_unlock(&ctx->lock);
  1203. if (list_empty(&tmp))
  1204. return NOTIFY_OK;
  1205. ctx = blk_mq_get_ctx(q);
  1206. spin_lock(&ctx->lock);
  1207. while (!list_empty(&tmp)) {
  1208. struct request *rq;
  1209. rq = list_first_entry(&tmp, struct request, queuelist);
  1210. rq->mq_ctx = ctx;
  1211. list_move_tail(&rq->queuelist, &ctx->rq_list);
  1212. }
  1213. hctx = q->mq_ops->map_queue(q, ctx->cpu);
  1214. blk_mq_hctx_mark_pending(hctx, ctx);
  1215. spin_unlock(&ctx->lock);
  1216. blk_mq_run_hw_queue(hctx, true);
  1217. blk_mq_put_ctx(ctx);
  1218. return NOTIFY_OK;
  1219. }
  1220. static int blk_mq_hctx_cpu_online(struct blk_mq_hw_ctx *hctx, int cpu)
  1221. {
  1222. struct request_queue *q = hctx->queue;
  1223. struct blk_mq_tag_set *set = q->tag_set;
  1224. if (set->tags[hctx->queue_num])
  1225. return NOTIFY_OK;
  1226. set->tags[hctx->queue_num] = blk_mq_init_rq_map(set, hctx->queue_num);
  1227. if (!set->tags[hctx->queue_num])
  1228. return NOTIFY_STOP;
  1229. hctx->tags = set->tags[hctx->queue_num];
  1230. return NOTIFY_OK;
  1231. }
  1232. static int blk_mq_hctx_notify(void *data, unsigned long action,
  1233. unsigned int cpu)
  1234. {
  1235. struct blk_mq_hw_ctx *hctx = data;
  1236. if (action == CPU_DEAD || action == CPU_DEAD_FROZEN)
  1237. return blk_mq_hctx_cpu_offline(hctx, cpu);
  1238. else if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN)
  1239. return blk_mq_hctx_cpu_online(hctx, cpu);
  1240. return NOTIFY_OK;
  1241. }
  1242. static void blk_mq_exit_hw_queues(struct request_queue *q,
  1243. struct blk_mq_tag_set *set, int nr_queue)
  1244. {
  1245. struct blk_mq_hw_ctx *hctx;
  1246. unsigned int i;
  1247. queue_for_each_hw_ctx(q, hctx, i) {
  1248. if (i == nr_queue)
  1249. break;
  1250. blk_mq_tag_idle(hctx);
  1251. if (set->ops->exit_hctx)
  1252. set->ops->exit_hctx(hctx, i);
  1253. blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
  1254. kfree(hctx->ctxs);
  1255. blk_mq_free_bitmap(&hctx->ctx_map);
  1256. }
  1257. }
  1258. static void blk_mq_free_hw_queues(struct request_queue *q,
  1259. struct blk_mq_tag_set *set)
  1260. {
  1261. struct blk_mq_hw_ctx *hctx;
  1262. unsigned int i;
  1263. queue_for_each_hw_ctx(q, hctx, i) {
  1264. free_cpumask_var(hctx->cpumask);
  1265. kfree(hctx);
  1266. }
  1267. }
  1268. static int blk_mq_init_hw_queues(struct request_queue *q,
  1269. struct blk_mq_tag_set *set)
  1270. {
  1271. struct blk_mq_hw_ctx *hctx;
  1272. unsigned int i;
  1273. /*
  1274. * Initialize hardware queues
  1275. */
  1276. queue_for_each_hw_ctx(q, hctx, i) {
  1277. int node;
  1278. node = hctx->numa_node;
  1279. if (node == NUMA_NO_NODE)
  1280. node = hctx->numa_node = set->numa_node;
  1281. INIT_DELAYED_WORK(&hctx->run_work, blk_mq_run_work_fn);
  1282. INIT_DELAYED_WORK(&hctx->delay_work, blk_mq_delay_work_fn);
  1283. spin_lock_init(&hctx->lock);
  1284. INIT_LIST_HEAD(&hctx->dispatch);
  1285. hctx->queue = q;
  1286. hctx->queue_num = i;
  1287. hctx->flags = set->flags;
  1288. hctx->cmd_size = set->cmd_size;
  1289. blk_mq_init_cpu_notifier(&hctx->cpu_notifier,
  1290. blk_mq_hctx_notify, hctx);
  1291. blk_mq_register_cpu_notifier(&hctx->cpu_notifier);
  1292. hctx->tags = set->tags[i];
  1293. /*
  1294. * Allocate space for all possible cpus to avoid allocation in
  1295. * runtime
  1296. */
  1297. hctx->ctxs = kmalloc_node(nr_cpu_ids * sizeof(void *),
  1298. GFP_KERNEL, node);
  1299. if (!hctx->ctxs)
  1300. break;
  1301. if (blk_mq_alloc_bitmap(&hctx->ctx_map, node))
  1302. break;
  1303. hctx->nr_ctx = 0;
  1304. if (set->ops->init_hctx &&
  1305. set->ops->init_hctx(hctx, set->driver_data, i))
  1306. break;
  1307. }
  1308. if (i == q->nr_hw_queues)
  1309. return 0;
  1310. /*
  1311. * Init failed
  1312. */
  1313. blk_mq_exit_hw_queues(q, set, i);
  1314. return 1;
  1315. }
  1316. static void blk_mq_init_cpu_queues(struct request_queue *q,
  1317. unsigned int nr_hw_queues)
  1318. {
  1319. unsigned int i;
  1320. for_each_possible_cpu(i) {
  1321. struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i);
  1322. struct blk_mq_hw_ctx *hctx;
  1323. memset(__ctx, 0, sizeof(*__ctx));
  1324. __ctx->cpu = i;
  1325. spin_lock_init(&__ctx->lock);
  1326. INIT_LIST_HEAD(&__ctx->rq_list);
  1327. __ctx->queue = q;
  1328. /* If the cpu isn't online, the cpu is mapped to first hctx */
  1329. if (!cpu_online(i))
  1330. continue;
  1331. hctx = q->mq_ops->map_queue(q, i);
  1332. cpumask_set_cpu(i, hctx->cpumask);
  1333. hctx->nr_ctx++;
  1334. /*
  1335. * Set local node, IFF we have more than one hw queue. If
  1336. * not, we remain on the home node of the device
  1337. */
  1338. if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE)
  1339. hctx->numa_node = cpu_to_node(i);
  1340. }
  1341. }
  1342. static void blk_mq_map_swqueue(struct request_queue *q)
  1343. {
  1344. unsigned int i;
  1345. struct blk_mq_hw_ctx *hctx;
  1346. struct blk_mq_ctx *ctx;
  1347. queue_for_each_hw_ctx(q, hctx, i) {
  1348. cpumask_clear(hctx->cpumask);
  1349. hctx->nr_ctx = 0;
  1350. }
  1351. /*
  1352. * Map software to hardware queues
  1353. */
  1354. queue_for_each_ctx(q, ctx, i) {
  1355. /* If the cpu isn't online, the cpu is mapped to first hctx */
  1356. if (!cpu_online(i))
  1357. continue;
  1358. hctx = q->mq_ops->map_queue(q, i);
  1359. cpumask_set_cpu(i, hctx->cpumask);
  1360. ctx->index_hw = hctx->nr_ctx;
  1361. hctx->ctxs[hctx->nr_ctx++] = ctx;
  1362. }
  1363. queue_for_each_hw_ctx(q, hctx, i) {
  1364. /*
  1365. * If not software queues are mapped to this hardware queue,
  1366. * disable it and free the request entries
  1367. */
  1368. if (!hctx->nr_ctx) {
  1369. struct blk_mq_tag_set *set = q->tag_set;
  1370. if (set->tags[i]) {
  1371. blk_mq_free_rq_map(set, set->tags[i], i);
  1372. set->tags[i] = NULL;
  1373. hctx->tags = NULL;
  1374. }
  1375. continue;
  1376. }
  1377. /*
  1378. * Initialize batch roundrobin counts
  1379. */
  1380. hctx->next_cpu = cpumask_first(hctx->cpumask);
  1381. hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
  1382. }
  1383. }
  1384. static void blk_mq_update_tag_set_depth(struct blk_mq_tag_set *set)
  1385. {
  1386. struct blk_mq_hw_ctx *hctx;
  1387. struct request_queue *q;
  1388. bool shared;
  1389. int i;
  1390. if (set->tag_list.next == set->tag_list.prev)
  1391. shared = false;
  1392. else
  1393. shared = true;
  1394. list_for_each_entry(q, &set->tag_list, tag_set_list) {
  1395. blk_mq_freeze_queue(q);
  1396. queue_for_each_hw_ctx(q, hctx, i) {
  1397. if (shared)
  1398. hctx->flags |= BLK_MQ_F_TAG_SHARED;
  1399. else
  1400. hctx->flags &= ~BLK_MQ_F_TAG_SHARED;
  1401. }
  1402. blk_mq_unfreeze_queue(q);
  1403. }
  1404. }
  1405. static void blk_mq_del_queue_tag_set(struct request_queue *q)
  1406. {
  1407. struct blk_mq_tag_set *set = q->tag_set;
  1408. blk_mq_freeze_queue(q);
  1409. mutex_lock(&set->tag_list_lock);
  1410. list_del_init(&q->tag_set_list);
  1411. blk_mq_update_tag_set_depth(set);
  1412. mutex_unlock(&set->tag_list_lock);
  1413. blk_mq_unfreeze_queue(q);
  1414. }
  1415. static void blk_mq_add_queue_tag_set(struct blk_mq_tag_set *set,
  1416. struct request_queue *q)
  1417. {
  1418. q->tag_set = set;
  1419. mutex_lock(&set->tag_list_lock);
  1420. list_add_tail(&q->tag_set_list, &set->tag_list);
  1421. blk_mq_update_tag_set_depth(set);
  1422. mutex_unlock(&set->tag_list_lock);
  1423. }
  1424. struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set)
  1425. {
  1426. struct blk_mq_hw_ctx **hctxs;
  1427. struct blk_mq_ctx __percpu *ctx;
  1428. struct request_queue *q;
  1429. unsigned int *map;
  1430. int i;
  1431. ctx = alloc_percpu(struct blk_mq_ctx);
  1432. if (!ctx)
  1433. return ERR_PTR(-ENOMEM);
  1434. hctxs = kmalloc_node(set->nr_hw_queues * sizeof(*hctxs), GFP_KERNEL,
  1435. set->numa_node);
  1436. if (!hctxs)
  1437. goto err_percpu;
  1438. map = blk_mq_make_queue_map(set);
  1439. if (!map)
  1440. goto err_map;
  1441. for (i = 0; i < set->nr_hw_queues; i++) {
  1442. int node = blk_mq_hw_queue_to_node(map, i);
  1443. hctxs[i] = kzalloc_node(sizeof(struct blk_mq_hw_ctx),
  1444. GFP_KERNEL, node);
  1445. if (!hctxs[i])
  1446. goto err_hctxs;
  1447. if (!zalloc_cpumask_var(&hctxs[i]->cpumask, GFP_KERNEL))
  1448. goto err_hctxs;
  1449. atomic_set(&hctxs[i]->nr_active, 0);
  1450. hctxs[i]->numa_node = node;
  1451. hctxs[i]->queue_num = i;
  1452. }
  1453. q = blk_alloc_queue_node(GFP_KERNEL, set->numa_node);
  1454. if (!q)
  1455. goto err_hctxs;
  1456. if (percpu_ref_init(&q->mq_usage_counter, blk_mq_usage_counter_release))
  1457. goto err_map;
  1458. setup_timer(&q->timeout, blk_mq_rq_timer, (unsigned long) q);
  1459. blk_queue_rq_timeout(q, 30000);
  1460. q->nr_queues = nr_cpu_ids;
  1461. q->nr_hw_queues = set->nr_hw_queues;
  1462. q->mq_map = map;
  1463. q->queue_ctx = ctx;
  1464. q->queue_hw_ctx = hctxs;
  1465. q->mq_ops = set->ops;
  1466. q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT;
  1467. if (!(set->flags & BLK_MQ_F_SG_MERGE))
  1468. q->queue_flags |= 1 << QUEUE_FLAG_NO_SG_MERGE;
  1469. q->sg_reserved_size = INT_MAX;
  1470. INIT_WORK(&q->requeue_work, blk_mq_requeue_work);
  1471. INIT_LIST_HEAD(&q->requeue_list);
  1472. spin_lock_init(&q->requeue_lock);
  1473. if (q->nr_hw_queues > 1)
  1474. blk_queue_make_request(q, blk_mq_make_request);
  1475. else
  1476. blk_queue_make_request(q, blk_sq_make_request);
  1477. blk_queue_rq_timed_out(q, blk_mq_rq_timed_out);
  1478. if (set->timeout)
  1479. blk_queue_rq_timeout(q, set->timeout);
  1480. /*
  1481. * Do this after blk_queue_make_request() overrides it...
  1482. */
  1483. q->nr_requests = set->queue_depth;
  1484. if (set->ops->complete)
  1485. blk_queue_softirq_done(q, set->ops->complete);
  1486. blk_mq_init_flush(q);
  1487. blk_mq_init_cpu_queues(q, set->nr_hw_queues);
  1488. q->flush_rq = kzalloc(round_up(sizeof(struct request) +
  1489. set->cmd_size, cache_line_size()),
  1490. GFP_KERNEL);
  1491. if (!q->flush_rq)
  1492. goto err_hw;
  1493. if (blk_mq_init_hw_queues(q, set))
  1494. goto err_flush_rq;
  1495. mutex_lock(&all_q_mutex);
  1496. list_add_tail(&q->all_q_node, &all_q_list);
  1497. mutex_unlock(&all_q_mutex);
  1498. blk_mq_add_queue_tag_set(set, q);
  1499. blk_mq_map_swqueue(q);
  1500. return q;
  1501. err_flush_rq:
  1502. kfree(q->flush_rq);
  1503. err_hw:
  1504. blk_cleanup_queue(q);
  1505. err_hctxs:
  1506. kfree(map);
  1507. for (i = 0; i < set->nr_hw_queues; i++) {
  1508. if (!hctxs[i])
  1509. break;
  1510. free_cpumask_var(hctxs[i]->cpumask);
  1511. kfree(hctxs[i]);
  1512. }
  1513. err_map:
  1514. kfree(hctxs);
  1515. err_percpu:
  1516. free_percpu(ctx);
  1517. return ERR_PTR(-ENOMEM);
  1518. }
  1519. EXPORT_SYMBOL(blk_mq_init_queue);
  1520. void blk_mq_free_queue(struct request_queue *q)
  1521. {
  1522. struct blk_mq_tag_set *set = q->tag_set;
  1523. blk_mq_del_queue_tag_set(q);
  1524. blk_mq_exit_hw_queues(q, set, set->nr_hw_queues);
  1525. blk_mq_free_hw_queues(q, set);
  1526. percpu_ref_exit(&q->mq_usage_counter);
  1527. free_percpu(q->queue_ctx);
  1528. kfree(q->queue_hw_ctx);
  1529. kfree(q->mq_map);
  1530. q->queue_ctx = NULL;
  1531. q->queue_hw_ctx = NULL;
  1532. q->mq_map = NULL;
  1533. mutex_lock(&all_q_mutex);
  1534. list_del_init(&q->all_q_node);
  1535. mutex_unlock(&all_q_mutex);
  1536. }
  1537. /* Basically redo blk_mq_init_queue with queue frozen */
  1538. static void blk_mq_queue_reinit(struct request_queue *q)
  1539. {
  1540. blk_mq_freeze_queue(q);
  1541. blk_mq_sysfs_unregister(q);
  1542. blk_mq_update_queue_map(q->mq_map, q->nr_hw_queues);
  1543. /*
  1544. * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe
  1545. * we should change hctx numa_node according to new topology (this
  1546. * involves free and re-allocate memory, worthy doing?)
  1547. */
  1548. blk_mq_map_swqueue(q);
  1549. blk_mq_sysfs_register(q);
  1550. blk_mq_unfreeze_queue(q);
  1551. }
  1552. static int blk_mq_queue_reinit_notify(struct notifier_block *nb,
  1553. unsigned long action, void *hcpu)
  1554. {
  1555. struct request_queue *q;
  1556. /*
  1557. * Before new mappings are established, hotadded cpu might already
  1558. * start handling requests. This doesn't break anything as we map
  1559. * offline CPUs to first hardware queue. We will re-init the queue
  1560. * below to get optimal settings.
  1561. */
  1562. if (action != CPU_DEAD && action != CPU_DEAD_FROZEN &&
  1563. action != CPU_ONLINE && action != CPU_ONLINE_FROZEN)
  1564. return NOTIFY_OK;
  1565. mutex_lock(&all_q_mutex);
  1566. list_for_each_entry(q, &all_q_list, all_q_node)
  1567. blk_mq_queue_reinit(q);
  1568. mutex_unlock(&all_q_mutex);
  1569. return NOTIFY_OK;
  1570. }
  1571. /*
  1572. * Alloc a tag set to be associated with one or more request queues.
  1573. * May fail with EINVAL for various error conditions. May adjust the
  1574. * requested depth down, if if it too large. In that case, the set
  1575. * value will be stored in set->queue_depth.
  1576. */
  1577. int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set)
  1578. {
  1579. int i;
  1580. if (!set->nr_hw_queues)
  1581. return -EINVAL;
  1582. if (!set->queue_depth)
  1583. return -EINVAL;
  1584. if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN)
  1585. return -EINVAL;
  1586. if (!set->nr_hw_queues || !set->ops->queue_rq || !set->ops->map_queue)
  1587. return -EINVAL;
  1588. if (set->queue_depth > BLK_MQ_MAX_DEPTH) {
  1589. pr_info("blk-mq: reduced tag depth to %u\n",
  1590. BLK_MQ_MAX_DEPTH);
  1591. set->queue_depth = BLK_MQ_MAX_DEPTH;
  1592. }
  1593. set->tags = kmalloc_node(set->nr_hw_queues *
  1594. sizeof(struct blk_mq_tags *),
  1595. GFP_KERNEL, set->numa_node);
  1596. if (!set->tags)
  1597. goto out;
  1598. for (i = 0; i < set->nr_hw_queues; i++) {
  1599. set->tags[i] = blk_mq_init_rq_map(set, i);
  1600. if (!set->tags[i])
  1601. goto out_unwind;
  1602. }
  1603. mutex_init(&set->tag_list_lock);
  1604. INIT_LIST_HEAD(&set->tag_list);
  1605. return 0;
  1606. out_unwind:
  1607. while (--i >= 0)
  1608. blk_mq_free_rq_map(set, set->tags[i], i);
  1609. out:
  1610. return -ENOMEM;
  1611. }
  1612. EXPORT_SYMBOL(blk_mq_alloc_tag_set);
  1613. void blk_mq_free_tag_set(struct blk_mq_tag_set *set)
  1614. {
  1615. int i;
  1616. for (i = 0; i < set->nr_hw_queues; i++) {
  1617. if (set->tags[i])
  1618. blk_mq_free_rq_map(set, set->tags[i], i);
  1619. }
  1620. kfree(set->tags);
  1621. }
  1622. EXPORT_SYMBOL(blk_mq_free_tag_set);
  1623. int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr)
  1624. {
  1625. struct blk_mq_tag_set *set = q->tag_set;
  1626. struct blk_mq_hw_ctx *hctx;
  1627. int i, ret;
  1628. if (!set || nr > set->queue_depth)
  1629. return -EINVAL;
  1630. ret = 0;
  1631. queue_for_each_hw_ctx(q, hctx, i) {
  1632. ret = blk_mq_tag_update_depth(hctx->tags, nr);
  1633. if (ret)
  1634. break;
  1635. }
  1636. if (!ret)
  1637. q->nr_requests = nr;
  1638. return ret;
  1639. }
  1640. void blk_mq_disable_hotplug(void)
  1641. {
  1642. mutex_lock(&all_q_mutex);
  1643. }
  1644. void blk_mq_enable_hotplug(void)
  1645. {
  1646. mutex_unlock(&all_q_mutex);
  1647. }
  1648. static int __init blk_mq_init(void)
  1649. {
  1650. blk_mq_cpu_init();
  1651. hotcpu_notifier(blk_mq_queue_reinit_notify, 0);
  1652. return 0;
  1653. }
  1654. subsys_initcall(blk_mq_init);