blk-mq.c 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899
  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/kmemleak.h>
  13. #include <linux/mm.h>
  14. #include <linux/init.h>
  15. #include <linux/slab.h>
  16. #include <linux/workqueue.h>
  17. #include <linux/smp.h>
  18. #include <linux/llist.h>
  19. #include <linux/list_sort.h>
  20. #include <linux/cpu.h>
  21. #include <linux/cache.h>
  22. #include <linux/sched/sysctl.h>
  23. #include <linux/sched/topology.h>
  24. #include <linux/sched/signal.h>
  25. #include <linux/delay.h>
  26. #include <linux/crash_dump.h>
  27. #include <linux/prefetch.h>
  28. #include <trace/events/block.h>
  29. #include <linux/blk-mq.h>
  30. #include "blk.h"
  31. #include "blk-mq.h"
  32. #include "blk-mq-debugfs.h"
  33. #include "blk-mq-tag.h"
  34. #include "blk-stat.h"
  35. #include "blk-wbt.h"
  36. #include "blk-mq-sched.h"
  37. static DEFINE_MUTEX(all_q_mutex);
  38. static LIST_HEAD(all_q_list);
  39. static void blk_mq_poll_stats_start(struct request_queue *q);
  40. static void blk_mq_poll_stats_fn(struct blk_stat_callback *cb);
  41. static void __blk_mq_stop_hw_queues(struct request_queue *q, bool sync);
  42. static int blk_mq_poll_stats_bkt(const struct request *rq)
  43. {
  44. int ddir, bytes, bucket;
  45. ddir = rq_data_dir(rq);
  46. bytes = blk_rq_bytes(rq);
  47. bucket = ddir + 2*(ilog2(bytes) - 9);
  48. if (bucket < 0)
  49. return -1;
  50. else if (bucket >= BLK_MQ_POLL_STATS_BKTS)
  51. return ddir + BLK_MQ_POLL_STATS_BKTS - 2;
  52. return bucket;
  53. }
  54. /*
  55. * Check if any of the ctx's have pending work in this hardware queue
  56. */
  57. bool blk_mq_hctx_has_pending(struct blk_mq_hw_ctx *hctx)
  58. {
  59. return sbitmap_any_bit_set(&hctx->ctx_map) ||
  60. !list_empty_careful(&hctx->dispatch) ||
  61. blk_mq_sched_has_work(hctx);
  62. }
  63. /*
  64. * Mark this ctx as having pending work in this hardware queue
  65. */
  66. static void blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx *hctx,
  67. struct blk_mq_ctx *ctx)
  68. {
  69. if (!sbitmap_test_bit(&hctx->ctx_map, ctx->index_hw))
  70. sbitmap_set_bit(&hctx->ctx_map, ctx->index_hw);
  71. }
  72. static void blk_mq_hctx_clear_pending(struct blk_mq_hw_ctx *hctx,
  73. struct blk_mq_ctx *ctx)
  74. {
  75. sbitmap_clear_bit(&hctx->ctx_map, ctx->index_hw);
  76. }
  77. void blk_freeze_queue_start(struct request_queue *q)
  78. {
  79. int freeze_depth;
  80. freeze_depth = atomic_inc_return(&q->mq_freeze_depth);
  81. if (freeze_depth == 1) {
  82. percpu_ref_kill(&q->q_usage_counter);
  83. blk_mq_run_hw_queues(q, false);
  84. }
  85. }
  86. EXPORT_SYMBOL_GPL(blk_freeze_queue_start);
  87. void blk_mq_freeze_queue_wait(struct request_queue *q)
  88. {
  89. wait_event(q->mq_freeze_wq, percpu_ref_is_zero(&q->q_usage_counter));
  90. }
  91. EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_wait);
  92. int blk_mq_freeze_queue_wait_timeout(struct request_queue *q,
  93. unsigned long timeout)
  94. {
  95. return wait_event_timeout(q->mq_freeze_wq,
  96. percpu_ref_is_zero(&q->q_usage_counter),
  97. timeout);
  98. }
  99. EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_wait_timeout);
  100. /*
  101. * Guarantee no request is in use, so we can change any data structure of
  102. * the queue afterward.
  103. */
  104. void blk_freeze_queue(struct request_queue *q)
  105. {
  106. /*
  107. * In the !blk_mq case we are only calling this to kill the
  108. * q_usage_counter, otherwise this increases the freeze depth
  109. * and waits for it to return to zero. For this reason there is
  110. * no blk_unfreeze_queue(), and blk_freeze_queue() is not
  111. * exported to drivers as the only user for unfreeze is blk_mq.
  112. */
  113. blk_freeze_queue_start(q);
  114. blk_mq_freeze_queue_wait(q);
  115. }
  116. void blk_mq_freeze_queue(struct request_queue *q)
  117. {
  118. /*
  119. * ...just an alias to keep freeze and unfreeze actions balanced
  120. * in the blk_mq_* namespace
  121. */
  122. blk_freeze_queue(q);
  123. }
  124. EXPORT_SYMBOL_GPL(blk_mq_freeze_queue);
  125. void blk_mq_unfreeze_queue(struct request_queue *q)
  126. {
  127. int freeze_depth;
  128. freeze_depth = atomic_dec_return(&q->mq_freeze_depth);
  129. WARN_ON_ONCE(freeze_depth < 0);
  130. if (!freeze_depth) {
  131. percpu_ref_reinit(&q->q_usage_counter);
  132. wake_up_all(&q->mq_freeze_wq);
  133. }
  134. }
  135. EXPORT_SYMBOL_GPL(blk_mq_unfreeze_queue);
  136. /**
  137. * blk_mq_quiesce_queue() - wait until all ongoing queue_rq calls have finished
  138. * @q: request queue.
  139. *
  140. * Note: this function does not prevent that the struct request end_io()
  141. * callback function is invoked. Additionally, it is not prevented that
  142. * new queue_rq() calls occur unless the queue has been stopped first.
  143. */
  144. void blk_mq_quiesce_queue(struct request_queue *q)
  145. {
  146. struct blk_mq_hw_ctx *hctx;
  147. unsigned int i;
  148. bool rcu = false;
  149. __blk_mq_stop_hw_queues(q, true);
  150. queue_for_each_hw_ctx(q, hctx, i) {
  151. if (hctx->flags & BLK_MQ_F_BLOCKING)
  152. synchronize_srcu(&hctx->queue_rq_srcu);
  153. else
  154. rcu = true;
  155. }
  156. if (rcu)
  157. synchronize_rcu();
  158. }
  159. EXPORT_SYMBOL_GPL(blk_mq_quiesce_queue);
  160. void blk_mq_wake_waiters(struct request_queue *q)
  161. {
  162. struct blk_mq_hw_ctx *hctx;
  163. unsigned int i;
  164. queue_for_each_hw_ctx(q, hctx, i)
  165. if (blk_mq_hw_queue_mapped(hctx))
  166. blk_mq_tag_wakeup_all(hctx->tags, true);
  167. /*
  168. * If we are called because the queue has now been marked as
  169. * dying, we need to ensure that processes currently waiting on
  170. * the queue are notified as well.
  171. */
  172. wake_up_all(&q->mq_freeze_wq);
  173. }
  174. bool blk_mq_can_queue(struct blk_mq_hw_ctx *hctx)
  175. {
  176. return blk_mq_has_free_tags(hctx->tags);
  177. }
  178. EXPORT_SYMBOL(blk_mq_can_queue);
  179. void blk_mq_rq_ctx_init(struct request_queue *q, struct blk_mq_ctx *ctx,
  180. struct request *rq, unsigned int op)
  181. {
  182. INIT_LIST_HEAD(&rq->queuelist);
  183. /* csd/requeue_work/fifo_time is initialized before use */
  184. rq->q = q;
  185. rq->mq_ctx = ctx;
  186. rq->cmd_flags = op;
  187. if (blk_queue_io_stat(q))
  188. rq->rq_flags |= RQF_IO_STAT;
  189. /* do not touch atomic flags, it needs atomic ops against the timer */
  190. rq->cpu = -1;
  191. INIT_HLIST_NODE(&rq->hash);
  192. RB_CLEAR_NODE(&rq->rb_node);
  193. rq->rq_disk = NULL;
  194. rq->part = NULL;
  195. rq->start_time = jiffies;
  196. #ifdef CONFIG_BLK_CGROUP
  197. rq->rl = NULL;
  198. set_start_time_ns(rq);
  199. rq->io_start_time_ns = 0;
  200. #endif
  201. rq->nr_phys_segments = 0;
  202. #if defined(CONFIG_BLK_DEV_INTEGRITY)
  203. rq->nr_integrity_segments = 0;
  204. #endif
  205. rq->special = NULL;
  206. /* tag was already set */
  207. rq->extra_len = 0;
  208. INIT_LIST_HEAD(&rq->timeout_list);
  209. rq->timeout = 0;
  210. rq->end_io = NULL;
  211. rq->end_io_data = NULL;
  212. rq->next_rq = NULL;
  213. ctx->rq_dispatched[op_is_sync(op)]++;
  214. }
  215. EXPORT_SYMBOL_GPL(blk_mq_rq_ctx_init);
  216. struct request *__blk_mq_alloc_request(struct blk_mq_alloc_data *data,
  217. unsigned int op)
  218. {
  219. struct request *rq;
  220. unsigned int tag;
  221. tag = blk_mq_get_tag(data);
  222. if (tag != BLK_MQ_TAG_FAIL) {
  223. struct blk_mq_tags *tags = blk_mq_tags_from_data(data);
  224. rq = tags->static_rqs[tag];
  225. if (data->flags & BLK_MQ_REQ_INTERNAL) {
  226. rq->tag = -1;
  227. rq->internal_tag = tag;
  228. } else {
  229. if (blk_mq_tag_busy(data->hctx)) {
  230. rq->rq_flags = RQF_MQ_INFLIGHT;
  231. atomic_inc(&data->hctx->nr_active);
  232. }
  233. rq->tag = tag;
  234. rq->internal_tag = -1;
  235. data->hctx->tags->rqs[rq->tag] = rq;
  236. }
  237. blk_mq_rq_ctx_init(data->q, data->ctx, rq, op);
  238. return rq;
  239. }
  240. return NULL;
  241. }
  242. EXPORT_SYMBOL_GPL(__blk_mq_alloc_request);
  243. struct request *blk_mq_alloc_request(struct request_queue *q, int rw,
  244. unsigned int flags)
  245. {
  246. struct blk_mq_alloc_data alloc_data = { .flags = flags };
  247. struct request *rq;
  248. int ret;
  249. ret = blk_queue_enter(q, flags & BLK_MQ_REQ_NOWAIT);
  250. if (ret)
  251. return ERR_PTR(ret);
  252. rq = blk_mq_sched_get_request(q, NULL, rw, &alloc_data);
  253. blk_mq_put_ctx(alloc_data.ctx);
  254. blk_queue_exit(q);
  255. if (!rq)
  256. return ERR_PTR(-EWOULDBLOCK);
  257. rq->__data_len = 0;
  258. rq->__sector = (sector_t) -1;
  259. rq->bio = rq->biotail = NULL;
  260. return rq;
  261. }
  262. EXPORT_SYMBOL(blk_mq_alloc_request);
  263. struct request *blk_mq_alloc_request_hctx(struct request_queue *q, int rw,
  264. unsigned int flags, unsigned int hctx_idx)
  265. {
  266. struct blk_mq_alloc_data alloc_data = { .flags = flags };
  267. struct request *rq;
  268. unsigned int cpu;
  269. int ret;
  270. /*
  271. * If the tag allocator sleeps we could get an allocation for a
  272. * different hardware context. No need to complicate the low level
  273. * allocator for this for the rare use case of a command tied to
  274. * a specific queue.
  275. */
  276. if (WARN_ON_ONCE(!(flags & BLK_MQ_REQ_NOWAIT)))
  277. return ERR_PTR(-EINVAL);
  278. if (hctx_idx >= q->nr_hw_queues)
  279. return ERR_PTR(-EIO);
  280. ret = blk_queue_enter(q, true);
  281. if (ret)
  282. return ERR_PTR(ret);
  283. /*
  284. * Check if the hardware context is actually mapped to anything.
  285. * If not tell the caller that it should skip this queue.
  286. */
  287. alloc_data.hctx = q->queue_hw_ctx[hctx_idx];
  288. if (!blk_mq_hw_queue_mapped(alloc_data.hctx)) {
  289. blk_queue_exit(q);
  290. return ERR_PTR(-EXDEV);
  291. }
  292. cpu = cpumask_first(alloc_data.hctx->cpumask);
  293. alloc_data.ctx = __blk_mq_get_ctx(q, cpu);
  294. rq = blk_mq_sched_get_request(q, NULL, rw, &alloc_data);
  295. blk_queue_exit(q);
  296. if (!rq)
  297. return ERR_PTR(-EWOULDBLOCK);
  298. return rq;
  299. }
  300. EXPORT_SYMBOL_GPL(blk_mq_alloc_request_hctx);
  301. void __blk_mq_finish_request(struct blk_mq_hw_ctx *hctx, struct blk_mq_ctx *ctx,
  302. struct request *rq)
  303. {
  304. const int sched_tag = rq->internal_tag;
  305. struct request_queue *q = rq->q;
  306. if (rq->rq_flags & RQF_MQ_INFLIGHT)
  307. atomic_dec(&hctx->nr_active);
  308. wbt_done(q->rq_wb, &rq->issue_stat);
  309. rq->rq_flags = 0;
  310. clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
  311. clear_bit(REQ_ATOM_POLL_SLEPT, &rq->atomic_flags);
  312. if (rq->tag != -1)
  313. blk_mq_put_tag(hctx, hctx->tags, ctx, rq->tag);
  314. if (sched_tag != -1)
  315. blk_mq_put_tag(hctx, hctx->sched_tags, ctx, sched_tag);
  316. blk_mq_sched_restart(hctx);
  317. blk_queue_exit(q);
  318. }
  319. static void blk_mq_finish_hctx_request(struct blk_mq_hw_ctx *hctx,
  320. struct request *rq)
  321. {
  322. struct blk_mq_ctx *ctx = rq->mq_ctx;
  323. ctx->rq_completed[rq_is_sync(rq)]++;
  324. __blk_mq_finish_request(hctx, ctx, rq);
  325. }
  326. void blk_mq_finish_request(struct request *rq)
  327. {
  328. blk_mq_finish_hctx_request(blk_mq_map_queue(rq->q, rq->mq_ctx->cpu), rq);
  329. }
  330. EXPORT_SYMBOL_GPL(blk_mq_finish_request);
  331. void blk_mq_free_request(struct request *rq)
  332. {
  333. blk_mq_sched_put_request(rq);
  334. }
  335. EXPORT_SYMBOL_GPL(blk_mq_free_request);
  336. inline void __blk_mq_end_request(struct request *rq, int error)
  337. {
  338. blk_account_io_done(rq);
  339. if (rq->end_io) {
  340. wbt_done(rq->q->rq_wb, &rq->issue_stat);
  341. rq->end_io(rq, error);
  342. } else {
  343. if (unlikely(blk_bidi_rq(rq)))
  344. blk_mq_free_request(rq->next_rq);
  345. blk_mq_free_request(rq);
  346. }
  347. }
  348. EXPORT_SYMBOL(__blk_mq_end_request);
  349. void blk_mq_end_request(struct request *rq, int error)
  350. {
  351. if (blk_update_request(rq, error, blk_rq_bytes(rq)))
  352. BUG();
  353. __blk_mq_end_request(rq, error);
  354. }
  355. EXPORT_SYMBOL(blk_mq_end_request);
  356. static void __blk_mq_complete_request_remote(void *data)
  357. {
  358. struct request *rq = data;
  359. rq->q->softirq_done_fn(rq);
  360. }
  361. static void __blk_mq_complete_request(struct request *rq)
  362. {
  363. struct blk_mq_ctx *ctx = rq->mq_ctx;
  364. bool shared = false;
  365. int cpu;
  366. if (rq->internal_tag != -1)
  367. blk_mq_sched_completed_request(rq);
  368. if (rq->rq_flags & RQF_STATS) {
  369. blk_mq_poll_stats_start(rq->q);
  370. blk_stat_add(rq);
  371. }
  372. if (!test_bit(QUEUE_FLAG_SAME_COMP, &rq->q->queue_flags)) {
  373. rq->q->softirq_done_fn(rq);
  374. return;
  375. }
  376. cpu = get_cpu();
  377. if (!test_bit(QUEUE_FLAG_SAME_FORCE, &rq->q->queue_flags))
  378. shared = cpus_share_cache(cpu, ctx->cpu);
  379. if (cpu != ctx->cpu && !shared && cpu_online(ctx->cpu)) {
  380. rq->csd.func = __blk_mq_complete_request_remote;
  381. rq->csd.info = rq;
  382. rq->csd.flags = 0;
  383. smp_call_function_single_async(ctx->cpu, &rq->csd);
  384. } else {
  385. rq->q->softirq_done_fn(rq);
  386. }
  387. put_cpu();
  388. }
  389. /**
  390. * blk_mq_complete_request - end I/O on a request
  391. * @rq: the request being processed
  392. *
  393. * Description:
  394. * Ends all I/O on a request. It does not handle partial completions.
  395. * The actual completion happens out-of-order, through a IPI handler.
  396. **/
  397. void blk_mq_complete_request(struct request *rq)
  398. {
  399. struct request_queue *q = rq->q;
  400. if (unlikely(blk_should_fake_timeout(q)))
  401. return;
  402. if (!blk_mark_rq_complete(rq))
  403. __blk_mq_complete_request(rq);
  404. }
  405. EXPORT_SYMBOL(blk_mq_complete_request);
  406. int blk_mq_request_started(struct request *rq)
  407. {
  408. return test_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
  409. }
  410. EXPORT_SYMBOL_GPL(blk_mq_request_started);
  411. void blk_mq_start_request(struct request *rq)
  412. {
  413. struct request_queue *q = rq->q;
  414. blk_mq_sched_started_request(rq);
  415. trace_block_rq_issue(q, rq);
  416. if (test_bit(QUEUE_FLAG_STATS, &q->queue_flags)) {
  417. blk_stat_set_issue(&rq->issue_stat, blk_rq_sectors(rq));
  418. rq->rq_flags |= RQF_STATS;
  419. wbt_issue(q->rq_wb, &rq->issue_stat);
  420. }
  421. blk_add_timer(rq);
  422. /*
  423. * Ensure that ->deadline is visible before set the started
  424. * flag and clear the completed flag.
  425. */
  426. smp_mb__before_atomic();
  427. /*
  428. * Mark us as started and clear complete. Complete might have been
  429. * set if requeue raced with timeout, which then marked it as
  430. * complete. So be sure to clear complete again when we start
  431. * the request, otherwise we'll ignore the completion event.
  432. */
  433. if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
  434. set_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
  435. if (test_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags))
  436. clear_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags);
  437. if (q->dma_drain_size && blk_rq_bytes(rq)) {
  438. /*
  439. * Make sure space for the drain appears. We know we can do
  440. * this because max_hw_segments has been adjusted to be one
  441. * fewer than the device can handle.
  442. */
  443. rq->nr_phys_segments++;
  444. }
  445. }
  446. EXPORT_SYMBOL(blk_mq_start_request);
  447. /*
  448. * When we reach here because queue is busy, REQ_ATOM_COMPLETE
  449. * flag isn't set yet, so there may be race with timeout handler,
  450. * but given rq->deadline is just set in .queue_rq() under
  451. * this situation, the race won't be possible in reality because
  452. * rq->timeout should be set as big enough to cover the window
  453. * between blk_mq_start_request() called from .queue_rq() and
  454. * clearing REQ_ATOM_STARTED here.
  455. */
  456. static void __blk_mq_requeue_request(struct request *rq)
  457. {
  458. struct request_queue *q = rq->q;
  459. trace_block_rq_requeue(q, rq);
  460. wbt_requeue(q->rq_wb, &rq->issue_stat);
  461. blk_mq_sched_requeue_request(rq);
  462. if (test_and_clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags)) {
  463. if (q->dma_drain_size && blk_rq_bytes(rq))
  464. rq->nr_phys_segments--;
  465. }
  466. }
  467. void blk_mq_requeue_request(struct request *rq, bool kick_requeue_list)
  468. {
  469. __blk_mq_requeue_request(rq);
  470. BUG_ON(blk_queued_rq(rq));
  471. blk_mq_add_to_requeue_list(rq, true, kick_requeue_list);
  472. }
  473. EXPORT_SYMBOL(blk_mq_requeue_request);
  474. static void blk_mq_requeue_work(struct work_struct *work)
  475. {
  476. struct request_queue *q =
  477. container_of(work, struct request_queue, requeue_work.work);
  478. LIST_HEAD(rq_list);
  479. struct request *rq, *next;
  480. unsigned long flags;
  481. spin_lock_irqsave(&q->requeue_lock, flags);
  482. list_splice_init(&q->requeue_list, &rq_list);
  483. spin_unlock_irqrestore(&q->requeue_lock, flags);
  484. list_for_each_entry_safe(rq, next, &rq_list, queuelist) {
  485. if (!(rq->rq_flags & RQF_SOFTBARRIER))
  486. continue;
  487. rq->rq_flags &= ~RQF_SOFTBARRIER;
  488. list_del_init(&rq->queuelist);
  489. blk_mq_sched_insert_request(rq, true, false, false, true);
  490. }
  491. while (!list_empty(&rq_list)) {
  492. rq = list_entry(rq_list.next, struct request, queuelist);
  493. list_del_init(&rq->queuelist);
  494. blk_mq_sched_insert_request(rq, false, false, false, true);
  495. }
  496. blk_mq_run_hw_queues(q, false);
  497. }
  498. void blk_mq_add_to_requeue_list(struct request *rq, bool at_head,
  499. bool kick_requeue_list)
  500. {
  501. struct request_queue *q = rq->q;
  502. unsigned long flags;
  503. /*
  504. * We abuse this flag that is otherwise used by the I/O scheduler to
  505. * request head insertation from the workqueue.
  506. */
  507. BUG_ON(rq->rq_flags & RQF_SOFTBARRIER);
  508. spin_lock_irqsave(&q->requeue_lock, flags);
  509. if (at_head) {
  510. rq->rq_flags |= RQF_SOFTBARRIER;
  511. list_add(&rq->queuelist, &q->requeue_list);
  512. } else {
  513. list_add_tail(&rq->queuelist, &q->requeue_list);
  514. }
  515. spin_unlock_irqrestore(&q->requeue_lock, flags);
  516. if (kick_requeue_list)
  517. blk_mq_kick_requeue_list(q);
  518. }
  519. EXPORT_SYMBOL(blk_mq_add_to_requeue_list);
  520. void blk_mq_kick_requeue_list(struct request_queue *q)
  521. {
  522. kblockd_schedule_delayed_work(&q->requeue_work, 0);
  523. }
  524. EXPORT_SYMBOL(blk_mq_kick_requeue_list);
  525. void blk_mq_delay_kick_requeue_list(struct request_queue *q,
  526. unsigned long msecs)
  527. {
  528. kblockd_schedule_delayed_work(&q->requeue_work,
  529. msecs_to_jiffies(msecs));
  530. }
  531. EXPORT_SYMBOL(blk_mq_delay_kick_requeue_list);
  532. struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags, unsigned int tag)
  533. {
  534. if (tag < tags->nr_tags) {
  535. prefetch(tags->rqs[tag]);
  536. return tags->rqs[tag];
  537. }
  538. return NULL;
  539. }
  540. EXPORT_SYMBOL(blk_mq_tag_to_rq);
  541. struct blk_mq_timeout_data {
  542. unsigned long next;
  543. unsigned int next_set;
  544. };
  545. void blk_mq_rq_timed_out(struct request *req, bool reserved)
  546. {
  547. const struct blk_mq_ops *ops = req->q->mq_ops;
  548. enum blk_eh_timer_return ret = BLK_EH_RESET_TIMER;
  549. /*
  550. * We know that complete is set at this point. If STARTED isn't set
  551. * anymore, then the request isn't active and the "timeout" should
  552. * just be ignored. This can happen due to the bitflag ordering.
  553. * Timeout first checks if STARTED is set, and if it is, assumes
  554. * the request is active. But if we race with completion, then
  555. * both flags will get cleared. So check here again, and ignore
  556. * a timeout event with a request that isn't active.
  557. */
  558. if (!test_bit(REQ_ATOM_STARTED, &req->atomic_flags))
  559. return;
  560. if (ops->timeout)
  561. ret = ops->timeout(req, reserved);
  562. switch (ret) {
  563. case BLK_EH_HANDLED:
  564. __blk_mq_complete_request(req);
  565. break;
  566. case BLK_EH_RESET_TIMER:
  567. blk_add_timer(req);
  568. blk_clear_rq_complete(req);
  569. break;
  570. case BLK_EH_NOT_HANDLED:
  571. break;
  572. default:
  573. printk(KERN_ERR "block: bad eh return: %d\n", ret);
  574. break;
  575. }
  576. }
  577. static void blk_mq_check_expired(struct blk_mq_hw_ctx *hctx,
  578. struct request *rq, void *priv, bool reserved)
  579. {
  580. struct blk_mq_timeout_data *data = priv;
  581. if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
  582. return;
  583. /*
  584. * The rq being checked may have been freed and reallocated
  585. * out already here, we avoid this race by checking rq->deadline
  586. * and REQ_ATOM_COMPLETE flag together:
  587. *
  588. * - if rq->deadline is observed as new value because of
  589. * reusing, the rq won't be timed out because of timing.
  590. * - if rq->deadline is observed as previous value,
  591. * REQ_ATOM_COMPLETE flag won't be cleared in reuse path
  592. * because we put a barrier between setting rq->deadline
  593. * and clearing the flag in blk_mq_start_request(), so
  594. * this rq won't be timed out too.
  595. */
  596. if (time_after_eq(jiffies, rq->deadline)) {
  597. if (!blk_mark_rq_complete(rq))
  598. blk_mq_rq_timed_out(rq, reserved);
  599. } else if (!data->next_set || time_after(data->next, rq->deadline)) {
  600. data->next = rq->deadline;
  601. data->next_set = 1;
  602. }
  603. }
  604. static void blk_mq_timeout_work(struct work_struct *work)
  605. {
  606. struct request_queue *q =
  607. container_of(work, struct request_queue, timeout_work);
  608. struct blk_mq_timeout_data data = {
  609. .next = 0,
  610. .next_set = 0,
  611. };
  612. int i;
  613. /* A deadlock might occur if a request is stuck requiring a
  614. * timeout at the same time a queue freeze is waiting
  615. * completion, since the timeout code would not be able to
  616. * acquire the queue reference here.
  617. *
  618. * That's why we don't use blk_queue_enter here; instead, we use
  619. * percpu_ref_tryget directly, because we need to be able to
  620. * obtain a reference even in the short window between the queue
  621. * starting to freeze, by dropping the first reference in
  622. * blk_freeze_queue_start, and the moment the last request is
  623. * consumed, marked by the instant q_usage_counter reaches
  624. * zero.
  625. */
  626. if (!percpu_ref_tryget(&q->q_usage_counter))
  627. return;
  628. blk_mq_queue_tag_busy_iter(q, blk_mq_check_expired, &data);
  629. if (data.next_set) {
  630. data.next = blk_rq_timeout(round_jiffies_up(data.next));
  631. mod_timer(&q->timeout, data.next);
  632. } else {
  633. struct blk_mq_hw_ctx *hctx;
  634. queue_for_each_hw_ctx(q, hctx, i) {
  635. /* the hctx may be unmapped, so check it here */
  636. if (blk_mq_hw_queue_mapped(hctx))
  637. blk_mq_tag_idle(hctx);
  638. }
  639. }
  640. blk_queue_exit(q);
  641. }
  642. /*
  643. * Reverse check our software queue for entries that we could potentially
  644. * merge with. Currently includes a hand-wavy stop count of 8, to not spend
  645. * too much time checking for merges.
  646. */
  647. static bool blk_mq_attempt_merge(struct request_queue *q,
  648. struct blk_mq_ctx *ctx, struct bio *bio)
  649. {
  650. struct request *rq;
  651. int checked = 8;
  652. list_for_each_entry_reverse(rq, &ctx->rq_list, queuelist) {
  653. bool merged = false;
  654. if (!checked--)
  655. break;
  656. if (!blk_rq_merge_ok(rq, bio))
  657. continue;
  658. switch (blk_try_merge(rq, bio)) {
  659. case ELEVATOR_BACK_MERGE:
  660. if (blk_mq_sched_allow_merge(q, rq, bio))
  661. merged = bio_attempt_back_merge(q, rq, bio);
  662. break;
  663. case ELEVATOR_FRONT_MERGE:
  664. if (blk_mq_sched_allow_merge(q, rq, bio))
  665. merged = bio_attempt_front_merge(q, rq, bio);
  666. break;
  667. case ELEVATOR_DISCARD_MERGE:
  668. merged = bio_attempt_discard_merge(q, rq, bio);
  669. break;
  670. default:
  671. continue;
  672. }
  673. if (merged)
  674. ctx->rq_merged++;
  675. return merged;
  676. }
  677. return false;
  678. }
  679. struct flush_busy_ctx_data {
  680. struct blk_mq_hw_ctx *hctx;
  681. struct list_head *list;
  682. };
  683. static bool flush_busy_ctx(struct sbitmap *sb, unsigned int bitnr, void *data)
  684. {
  685. struct flush_busy_ctx_data *flush_data = data;
  686. struct blk_mq_hw_ctx *hctx = flush_data->hctx;
  687. struct blk_mq_ctx *ctx = hctx->ctxs[bitnr];
  688. sbitmap_clear_bit(sb, bitnr);
  689. spin_lock(&ctx->lock);
  690. list_splice_tail_init(&ctx->rq_list, flush_data->list);
  691. spin_unlock(&ctx->lock);
  692. return true;
  693. }
  694. /*
  695. * Process software queues that have been marked busy, splicing them
  696. * to the for-dispatch
  697. */
  698. void blk_mq_flush_busy_ctxs(struct blk_mq_hw_ctx *hctx, struct list_head *list)
  699. {
  700. struct flush_busy_ctx_data data = {
  701. .hctx = hctx,
  702. .list = list,
  703. };
  704. sbitmap_for_each_set(&hctx->ctx_map, flush_busy_ctx, &data);
  705. }
  706. EXPORT_SYMBOL_GPL(blk_mq_flush_busy_ctxs);
  707. static inline unsigned int queued_to_index(unsigned int queued)
  708. {
  709. if (!queued)
  710. return 0;
  711. return min(BLK_MQ_MAX_DISPATCH_ORDER - 1, ilog2(queued) + 1);
  712. }
  713. bool blk_mq_get_driver_tag(struct request *rq, struct blk_mq_hw_ctx **hctx,
  714. bool wait)
  715. {
  716. struct blk_mq_alloc_data data = {
  717. .q = rq->q,
  718. .hctx = blk_mq_map_queue(rq->q, rq->mq_ctx->cpu),
  719. .flags = wait ? 0 : BLK_MQ_REQ_NOWAIT,
  720. };
  721. might_sleep_if(wait);
  722. if (rq->tag != -1)
  723. goto done;
  724. if (blk_mq_tag_is_reserved(data.hctx->sched_tags, rq->internal_tag))
  725. data.flags |= BLK_MQ_REQ_RESERVED;
  726. rq->tag = blk_mq_get_tag(&data);
  727. if (rq->tag >= 0) {
  728. if (blk_mq_tag_busy(data.hctx)) {
  729. rq->rq_flags |= RQF_MQ_INFLIGHT;
  730. atomic_inc(&data.hctx->nr_active);
  731. }
  732. data.hctx->tags->rqs[rq->tag] = rq;
  733. }
  734. done:
  735. if (hctx)
  736. *hctx = data.hctx;
  737. return rq->tag != -1;
  738. }
  739. static void __blk_mq_put_driver_tag(struct blk_mq_hw_ctx *hctx,
  740. struct request *rq)
  741. {
  742. blk_mq_put_tag(hctx, hctx->tags, rq->mq_ctx, rq->tag);
  743. rq->tag = -1;
  744. if (rq->rq_flags & RQF_MQ_INFLIGHT) {
  745. rq->rq_flags &= ~RQF_MQ_INFLIGHT;
  746. atomic_dec(&hctx->nr_active);
  747. }
  748. }
  749. static void blk_mq_put_driver_tag_hctx(struct blk_mq_hw_ctx *hctx,
  750. struct request *rq)
  751. {
  752. if (rq->tag == -1 || rq->internal_tag == -1)
  753. return;
  754. __blk_mq_put_driver_tag(hctx, rq);
  755. }
  756. static void blk_mq_put_driver_tag(struct request *rq)
  757. {
  758. struct blk_mq_hw_ctx *hctx;
  759. if (rq->tag == -1 || rq->internal_tag == -1)
  760. return;
  761. hctx = blk_mq_map_queue(rq->q, rq->mq_ctx->cpu);
  762. __blk_mq_put_driver_tag(hctx, rq);
  763. }
  764. /*
  765. * If we fail getting a driver tag because all the driver tags are already
  766. * assigned and on the dispatch list, BUT the first entry does not have a
  767. * tag, then we could deadlock. For that case, move entries with assigned
  768. * driver tags to the front, leaving the set of tagged requests in the
  769. * same order, and the untagged set in the same order.
  770. */
  771. static bool reorder_tags_to_front(struct list_head *list)
  772. {
  773. struct request *rq, *tmp, *first = NULL;
  774. list_for_each_entry_safe_reverse(rq, tmp, list, queuelist) {
  775. if (rq == first)
  776. break;
  777. if (rq->tag != -1) {
  778. list_move(&rq->queuelist, list);
  779. if (!first)
  780. first = rq;
  781. }
  782. }
  783. return first != NULL;
  784. }
  785. static int blk_mq_dispatch_wake(wait_queue_t *wait, unsigned mode, int flags,
  786. void *key)
  787. {
  788. struct blk_mq_hw_ctx *hctx;
  789. hctx = container_of(wait, struct blk_mq_hw_ctx, dispatch_wait);
  790. list_del(&wait->task_list);
  791. clear_bit_unlock(BLK_MQ_S_TAG_WAITING, &hctx->state);
  792. blk_mq_run_hw_queue(hctx, true);
  793. return 1;
  794. }
  795. static bool blk_mq_dispatch_wait_add(struct blk_mq_hw_ctx *hctx)
  796. {
  797. struct sbq_wait_state *ws;
  798. /*
  799. * The TAG_WAITING bit serves as a lock protecting hctx->dispatch_wait.
  800. * The thread which wins the race to grab this bit adds the hardware
  801. * queue to the wait queue.
  802. */
  803. if (test_bit(BLK_MQ_S_TAG_WAITING, &hctx->state) ||
  804. test_and_set_bit_lock(BLK_MQ_S_TAG_WAITING, &hctx->state))
  805. return false;
  806. init_waitqueue_func_entry(&hctx->dispatch_wait, blk_mq_dispatch_wake);
  807. ws = bt_wait_ptr(&hctx->tags->bitmap_tags, hctx);
  808. /*
  809. * As soon as this returns, it's no longer safe to fiddle with
  810. * hctx->dispatch_wait, since a completion can wake up the wait queue
  811. * and unlock the bit.
  812. */
  813. add_wait_queue(&ws->wait, &hctx->dispatch_wait);
  814. return true;
  815. }
  816. bool blk_mq_dispatch_rq_list(struct request_queue *q, struct list_head *list)
  817. {
  818. struct blk_mq_hw_ctx *hctx;
  819. struct request *rq;
  820. int errors, queued, ret = BLK_MQ_RQ_QUEUE_OK;
  821. if (list_empty(list))
  822. return false;
  823. /*
  824. * Now process all the entries, sending them to the driver.
  825. */
  826. errors = queued = 0;
  827. do {
  828. struct blk_mq_queue_data bd;
  829. rq = list_first_entry(list, struct request, queuelist);
  830. if (!blk_mq_get_driver_tag(rq, &hctx, false)) {
  831. if (!queued && reorder_tags_to_front(list))
  832. continue;
  833. /*
  834. * The initial allocation attempt failed, so we need to
  835. * rerun the hardware queue when a tag is freed.
  836. */
  837. if (!blk_mq_dispatch_wait_add(hctx))
  838. break;
  839. /*
  840. * It's possible that a tag was freed in the window
  841. * between the allocation failure and adding the
  842. * hardware queue to the wait queue.
  843. */
  844. if (!blk_mq_get_driver_tag(rq, &hctx, false))
  845. break;
  846. }
  847. list_del_init(&rq->queuelist);
  848. bd.rq = rq;
  849. /*
  850. * Flag last if we have no more requests, or if we have more
  851. * but can't assign a driver tag to it.
  852. */
  853. if (list_empty(list))
  854. bd.last = true;
  855. else {
  856. struct request *nxt;
  857. nxt = list_first_entry(list, struct request, queuelist);
  858. bd.last = !blk_mq_get_driver_tag(nxt, NULL, false);
  859. }
  860. ret = q->mq_ops->queue_rq(hctx, &bd);
  861. switch (ret) {
  862. case BLK_MQ_RQ_QUEUE_OK:
  863. queued++;
  864. break;
  865. case BLK_MQ_RQ_QUEUE_BUSY:
  866. blk_mq_put_driver_tag_hctx(hctx, rq);
  867. list_add(&rq->queuelist, list);
  868. __blk_mq_requeue_request(rq);
  869. break;
  870. default:
  871. pr_err("blk-mq: bad return on queue: %d\n", ret);
  872. case BLK_MQ_RQ_QUEUE_ERROR:
  873. errors++;
  874. blk_mq_end_request(rq, -EIO);
  875. break;
  876. }
  877. if (ret == BLK_MQ_RQ_QUEUE_BUSY)
  878. break;
  879. } while (!list_empty(list));
  880. hctx->dispatched[queued_to_index(queued)]++;
  881. /*
  882. * Any items that need requeuing? Stuff them into hctx->dispatch,
  883. * that is where we will continue on next queue run.
  884. */
  885. if (!list_empty(list)) {
  886. /*
  887. * If an I/O scheduler has been configured and we got a driver
  888. * tag for the next request already, free it again.
  889. */
  890. rq = list_first_entry(list, struct request, queuelist);
  891. blk_mq_put_driver_tag(rq);
  892. spin_lock(&hctx->lock);
  893. list_splice_init(list, &hctx->dispatch);
  894. spin_unlock(&hctx->lock);
  895. /*
  896. * If SCHED_RESTART was set by the caller of this function and
  897. * it is no longer set that means that it was cleared by another
  898. * thread and hence that a queue rerun is needed.
  899. *
  900. * If TAG_WAITING is set that means that an I/O scheduler has
  901. * been configured and another thread is waiting for a driver
  902. * tag. To guarantee fairness, do not rerun this hardware queue
  903. * but let the other thread grab the driver tag.
  904. *
  905. * If no I/O scheduler has been configured it is possible that
  906. * the hardware queue got stopped and restarted before requests
  907. * were pushed back onto the dispatch list. Rerun the queue to
  908. * avoid starvation. Notes:
  909. * - blk_mq_run_hw_queue() checks whether or not a queue has
  910. * been stopped before rerunning a queue.
  911. * - Some but not all block drivers stop a queue before
  912. * returning BLK_MQ_RQ_QUEUE_BUSY. Two exceptions are scsi-mq
  913. * and dm-rq.
  914. */
  915. if (!blk_mq_sched_needs_restart(hctx) &&
  916. !test_bit(BLK_MQ_S_TAG_WAITING, &hctx->state))
  917. blk_mq_run_hw_queue(hctx, true);
  918. }
  919. return (queued + errors) != 0;
  920. }
  921. static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx)
  922. {
  923. int srcu_idx;
  924. WARN_ON(!cpumask_test_cpu(raw_smp_processor_id(), hctx->cpumask) &&
  925. cpu_online(hctx->next_cpu));
  926. if (!(hctx->flags & BLK_MQ_F_BLOCKING)) {
  927. rcu_read_lock();
  928. blk_mq_sched_dispatch_requests(hctx);
  929. rcu_read_unlock();
  930. } else {
  931. might_sleep();
  932. srcu_idx = srcu_read_lock(&hctx->queue_rq_srcu);
  933. blk_mq_sched_dispatch_requests(hctx);
  934. srcu_read_unlock(&hctx->queue_rq_srcu, srcu_idx);
  935. }
  936. }
  937. /*
  938. * It'd be great if the workqueue API had a way to pass
  939. * in a mask and had some smarts for more clever placement.
  940. * For now we just round-robin here, switching for every
  941. * BLK_MQ_CPU_WORK_BATCH queued items.
  942. */
  943. static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx)
  944. {
  945. if (hctx->queue->nr_hw_queues == 1)
  946. return WORK_CPU_UNBOUND;
  947. if (--hctx->next_cpu_batch <= 0) {
  948. int next_cpu;
  949. next_cpu = cpumask_next(hctx->next_cpu, hctx->cpumask);
  950. if (next_cpu >= nr_cpu_ids)
  951. next_cpu = cpumask_first(hctx->cpumask);
  952. hctx->next_cpu = next_cpu;
  953. hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
  954. }
  955. return hctx->next_cpu;
  956. }
  957. static void __blk_mq_delay_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async,
  958. unsigned long msecs)
  959. {
  960. if (unlikely(blk_mq_hctx_stopped(hctx) ||
  961. !blk_mq_hw_queue_mapped(hctx)))
  962. return;
  963. if (!async && !(hctx->flags & BLK_MQ_F_BLOCKING)) {
  964. int cpu = get_cpu();
  965. if (cpumask_test_cpu(cpu, hctx->cpumask)) {
  966. __blk_mq_run_hw_queue(hctx);
  967. put_cpu();
  968. return;
  969. }
  970. put_cpu();
  971. }
  972. kblockd_schedule_delayed_work_on(blk_mq_hctx_next_cpu(hctx),
  973. &hctx->run_work,
  974. msecs_to_jiffies(msecs));
  975. }
  976. void blk_mq_delay_run_hw_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs)
  977. {
  978. __blk_mq_delay_run_hw_queue(hctx, true, msecs);
  979. }
  980. EXPORT_SYMBOL(blk_mq_delay_run_hw_queue);
  981. void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
  982. {
  983. __blk_mq_delay_run_hw_queue(hctx, async, 0);
  984. }
  985. EXPORT_SYMBOL(blk_mq_run_hw_queue);
  986. void blk_mq_run_hw_queues(struct request_queue *q, bool async)
  987. {
  988. struct blk_mq_hw_ctx *hctx;
  989. int i;
  990. queue_for_each_hw_ctx(q, hctx, i) {
  991. if (!blk_mq_hctx_has_pending(hctx) ||
  992. blk_mq_hctx_stopped(hctx))
  993. continue;
  994. blk_mq_run_hw_queue(hctx, async);
  995. }
  996. }
  997. EXPORT_SYMBOL(blk_mq_run_hw_queues);
  998. /**
  999. * blk_mq_queue_stopped() - check whether one or more hctxs have been stopped
  1000. * @q: request queue.
  1001. *
  1002. * The caller is responsible for serializing this function against
  1003. * blk_mq_{start,stop}_hw_queue().
  1004. */
  1005. bool blk_mq_queue_stopped(struct request_queue *q)
  1006. {
  1007. struct blk_mq_hw_ctx *hctx;
  1008. int i;
  1009. queue_for_each_hw_ctx(q, hctx, i)
  1010. if (blk_mq_hctx_stopped(hctx))
  1011. return true;
  1012. return false;
  1013. }
  1014. EXPORT_SYMBOL(blk_mq_queue_stopped);
  1015. static void __blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx, bool sync)
  1016. {
  1017. if (sync)
  1018. cancel_delayed_work_sync(&hctx->run_work);
  1019. else
  1020. cancel_delayed_work(&hctx->run_work);
  1021. set_bit(BLK_MQ_S_STOPPED, &hctx->state);
  1022. }
  1023. void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx)
  1024. {
  1025. __blk_mq_stop_hw_queue(hctx, false);
  1026. }
  1027. EXPORT_SYMBOL(blk_mq_stop_hw_queue);
  1028. static void __blk_mq_stop_hw_queues(struct request_queue *q, bool sync)
  1029. {
  1030. struct blk_mq_hw_ctx *hctx;
  1031. int i;
  1032. queue_for_each_hw_ctx(q, hctx, i)
  1033. __blk_mq_stop_hw_queue(hctx, sync);
  1034. }
  1035. void blk_mq_stop_hw_queues(struct request_queue *q)
  1036. {
  1037. __blk_mq_stop_hw_queues(q, false);
  1038. }
  1039. EXPORT_SYMBOL(blk_mq_stop_hw_queues);
  1040. void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx)
  1041. {
  1042. clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
  1043. blk_mq_run_hw_queue(hctx, false);
  1044. }
  1045. EXPORT_SYMBOL(blk_mq_start_hw_queue);
  1046. void blk_mq_start_hw_queues(struct request_queue *q)
  1047. {
  1048. struct blk_mq_hw_ctx *hctx;
  1049. int i;
  1050. queue_for_each_hw_ctx(q, hctx, i)
  1051. blk_mq_start_hw_queue(hctx);
  1052. }
  1053. EXPORT_SYMBOL(blk_mq_start_hw_queues);
  1054. void blk_mq_start_stopped_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
  1055. {
  1056. if (!blk_mq_hctx_stopped(hctx))
  1057. return;
  1058. clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
  1059. blk_mq_run_hw_queue(hctx, async);
  1060. }
  1061. EXPORT_SYMBOL_GPL(blk_mq_start_stopped_hw_queue);
  1062. void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async)
  1063. {
  1064. struct blk_mq_hw_ctx *hctx;
  1065. int i;
  1066. queue_for_each_hw_ctx(q, hctx, i)
  1067. blk_mq_start_stopped_hw_queue(hctx, async);
  1068. }
  1069. EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues);
  1070. static void blk_mq_run_work_fn(struct work_struct *work)
  1071. {
  1072. struct blk_mq_hw_ctx *hctx;
  1073. hctx = container_of(work, struct blk_mq_hw_ctx, run_work.work);
  1074. /*
  1075. * If we are stopped, don't run the queue. The exception is if
  1076. * BLK_MQ_S_START_ON_RUN is set. For that case, we auto-clear
  1077. * the STOPPED bit and run it.
  1078. */
  1079. if (test_bit(BLK_MQ_S_STOPPED, &hctx->state)) {
  1080. if (!test_bit(BLK_MQ_S_START_ON_RUN, &hctx->state))
  1081. return;
  1082. clear_bit(BLK_MQ_S_START_ON_RUN, &hctx->state);
  1083. clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
  1084. }
  1085. __blk_mq_run_hw_queue(hctx);
  1086. }
  1087. void blk_mq_delay_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs)
  1088. {
  1089. if (unlikely(!blk_mq_hw_queue_mapped(hctx)))
  1090. return;
  1091. /*
  1092. * Stop the hw queue, then modify currently delayed work.
  1093. * This should prevent us from running the queue prematurely.
  1094. * Mark the queue as auto-clearing STOPPED when it runs.
  1095. */
  1096. blk_mq_stop_hw_queue(hctx);
  1097. set_bit(BLK_MQ_S_START_ON_RUN, &hctx->state);
  1098. kblockd_mod_delayed_work_on(blk_mq_hctx_next_cpu(hctx),
  1099. &hctx->run_work,
  1100. msecs_to_jiffies(msecs));
  1101. }
  1102. EXPORT_SYMBOL(blk_mq_delay_queue);
  1103. static inline void __blk_mq_insert_req_list(struct blk_mq_hw_ctx *hctx,
  1104. struct request *rq,
  1105. bool at_head)
  1106. {
  1107. struct blk_mq_ctx *ctx = rq->mq_ctx;
  1108. trace_block_rq_insert(hctx->queue, rq);
  1109. if (at_head)
  1110. list_add(&rq->queuelist, &ctx->rq_list);
  1111. else
  1112. list_add_tail(&rq->queuelist, &ctx->rq_list);
  1113. }
  1114. void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx, struct request *rq,
  1115. bool at_head)
  1116. {
  1117. struct blk_mq_ctx *ctx = rq->mq_ctx;
  1118. __blk_mq_insert_req_list(hctx, rq, at_head);
  1119. blk_mq_hctx_mark_pending(hctx, ctx);
  1120. }
  1121. void blk_mq_insert_requests(struct blk_mq_hw_ctx *hctx, struct blk_mq_ctx *ctx,
  1122. struct list_head *list)
  1123. {
  1124. /*
  1125. * preemption doesn't flush plug list, so it's possible ctx->cpu is
  1126. * offline now
  1127. */
  1128. spin_lock(&ctx->lock);
  1129. while (!list_empty(list)) {
  1130. struct request *rq;
  1131. rq = list_first_entry(list, struct request, queuelist);
  1132. BUG_ON(rq->mq_ctx != ctx);
  1133. list_del_init(&rq->queuelist);
  1134. __blk_mq_insert_req_list(hctx, rq, false);
  1135. }
  1136. blk_mq_hctx_mark_pending(hctx, ctx);
  1137. spin_unlock(&ctx->lock);
  1138. }
  1139. static int plug_ctx_cmp(void *priv, struct list_head *a, struct list_head *b)
  1140. {
  1141. struct request *rqa = container_of(a, struct request, queuelist);
  1142. struct request *rqb = container_of(b, struct request, queuelist);
  1143. return !(rqa->mq_ctx < rqb->mq_ctx ||
  1144. (rqa->mq_ctx == rqb->mq_ctx &&
  1145. blk_rq_pos(rqa) < blk_rq_pos(rqb)));
  1146. }
  1147. void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule)
  1148. {
  1149. struct blk_mq_ctx *this_ctx;
  1150. struct request_queue *this_q;
  1151. struct request *rq;
  1152. LIST_HEAD(list);
  1153. LIST_HEAD(ctx_list);
  1154. unsigned int depth;
  1155. list_splice_init(&plug->mq_list, &list);
  1156. list_sort(NULL, &list, plug_ctx_cmp);
  1157. this_q = NULL;
  1158. this_ctx = NULL;
  1159. depth = 0;
  1160. while (!list_empty(&list)) {
  1161. rq = list_entry_rq(list.next);
  1162. list_del_init(&rq->queuelist);
  1163. BUG_ON(!rq->q);
  1164. if (rq->mq_ctx != this_ctx) {
  1165. if (this_ctx) {
  1166. trace_block_unplug(this_q, depth, from_schedule);
  1167. blk_mq_sched_insert_requests(this_q, this_ctx,
  1168. &ctx_list,
  1169. from_schedule);
  1170. }
  1171. this_ctx = rq->mq_ctx;
  1172. this_q = rq->q;
  1173. depth = 0;
  1174. }
  1175. depth++;
  1176. list_add_tail(&rq->queuelist, &ctx_list);
  1177. }
  1178. /*
  1179. * If 'this_ctx' is set, we know we have entries to complete
  1180. * on 'ctx_list'. Do those.
  1181. */
  1182. if (this_ctx) {
  1183. trace_block_unplug(this_q, depth, from_schedule);
  1184. blk_mq_sched_insert_requests(this_q, this_ctx, &ctx_list,
  1185. from_schedule);
  1186. }
  1187. }
  1188. static void blk_mq_bio_to_request(struct request *rq, struct bio *bio)
  1189. {
  1190. blk_init_request_from_bio(rq, bio);
  1191. blk_account_io_start(rq, true);
  1192. }
  1193. static inline bool hctx_allow_merges(struct blk_mq_hw_ctx *hctx)
  1194. {
  1195. return (hctx->flags & BLK_MQ_F_SHOULD_MERGE) &&
  1196. !blk_queue_nomerges(hctx->queue);
  1197. }
  1198. static inline bool blk_mq_merge_queue_io(struct blk_mq_hw_ctx *hctx,
  1199. struct blk_mq_ctx *ctx,
  1200. struct request *rq, struct bio *bio)
  1201. {
  1202. if (!hctx_allow_merges(hctx) || !bio_mergeable(bio)) {
  1203. blk_mq_bio_to_request(rq, bio);
  1204. spin_lock(&ctx->lock);
  1205. insert_rq:
  1206. __blk_mq_insert_request(hctx, rq, false);
  1207. spin_unlock(&ctx->lock);
  1208. return false;
  1209. } else {
  1210. struct request_queue *q = hctx->queue;
  1211. spin_lock(&ctx->lock);
  1212. if (!blk_mq_attempt_merge(q, ctx, bio)) {
  1213. blk_mq_bio_to_request(rq, bio);
  1214. goto insert_rq;
  1215. }
  1216. spin_unlock(&ctx->lock);
  1217. __blk_mq_finish_request(hctx, ctx, rq);
  1218. return true;
  1219. }
  1220. }
  1221. static blk_qc_t request_to_qc_t(struct blk_mq_hw_ctx *hctx, struct request *rq)
  1222. {
  1223. if (rq->tag != -1)
  1224. return blk_tag_to_qc_t(rq->tag, hctx->queue_num, false);
  1225. return blk_tag_to_qc_t(rq->internal_tag, hctx->queue_num, true);
  1226. }
  1227. static void __blk_mq_try_issue_directly(struct request *rq, blk_qc_t *cookie,
  1228. bool may_sleep)
  1229. {
  1230. struct request_queue *q = rq->q;
  1231. struct blk_mq_queue_data bd = {
  1232. .rq = rq,
  1233. .last = true,
  1234. };
  1235. struct blk_mq_hw_ctx *hctx;
  1236. blk_qc_t new_cookie;
  1237. int ret;
  1238. if (q->elevator)
  1239. goto insert;
  1240. if (!blk_mq_get_driver_tag(rq, &hctx, false))
  1241. goto insert;
  1242. new_cookie = request_to_qc_t(hctx, rq);
  1243. /*
  1244. * For OK queue, we are done. For error, kill it. Any other
  1245. * error (busy), just add it to our list as we previously
  1246. * would have done
  1247. */
  1248. ret = q->mq_ops->queue_rq(hctx, &bd);
  1249. if (ret == BLK_MQ_RQ_QUEUE_OK) {
  1250. *cookie = new_cookie;
  1251. return;
  1252. }
  1253. if (ret == BLK_MQ_RQ_QUEUE_ERROR) {
  1254. *cookie = BLK_QC_T_NONE;
  1255. blk_mq_end_request(rq, -EIO);
  1256. return;
  1257. }
  1258. __blk_mq_requeue_request(rq);
  1259. insert:
  1260. blk_mq_sched_insert_request(rq, false, true, false, may_sleep);
  1261. }
  1262. static void blk_mq_try_issue_directly(struct blk_mq_hw_ctx *hctx,
  1263. struct request *rq, blk_qc_t *cookie)
  1264. {
  1265. if (!(hctx->flags & BLK_MQ_F_BLOCKING)) {
  1266. rcu_read_lock();
  1267. __blk_mq_try_issue_directly(rq, cookie, false);
  1268. rcu_read_unlock();
  1269. } else {
  1270. unsigned int srcu_idx;
  1271. might_sleep();
  1272. srcu_idx = srcu_read_lock(&hctx->queue_rq_srcu);
  1273. __blk_mq_try_issue_directly(rq, cookie, true);
  1274. srcu_read_unlock(&hctx->queue_rq_srcu, srcu_idx);
  1275. }
  1276. }
  1277. static blk_qc_t blk_mq_make_request(struct request_queue *q, struct bio *bio)
  1278. {
  1279. const int is_sync = op_is_sync(bio->bi_opf);
  1280. const int is_flush_fua = op_is_flush(bio->bi_opf);
  1281. struct blk_mq_alloc_data data = { .flags = 0 };
  1282. struct request *rq;
  1283. unsigned int request_count = 0;
  1284. struct blk_plug *plug;
  1285. struct request *same_queue_rq = NULL;
  1286. blk_qc_t cookie;
  1287. unsigned int wb_acct;
  1288. blk_queue_bounce(q, &bio);
  1289. blk_queue_split(q, &bio, q->bio_split);
  1290. if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
  1291. bio_io_error(bio);
  1292. return BLK_QC_T_NONE;
  1293. }
  1294. if (!is_flush_fua && !blk_queue_nomerges(q) &&
  1295. blk_attempt_plug_merge(q, bio, &request_count, &same_queue_rq))
  1296. return BLK_QC_T_NONE;
  1297. if (blk_mq_sched_bio_merge(q, bio))
  1298. return BLK_QC_T_NONE;
  1299. wb_acct = wbt_wait(q->rq_wb, bio, NULL);
  1300. trace_block_getrq(q, bio, bio->bi_opf);
  1301. rq = blk_mq_sched_get_request(q, bio, bio->bi_opf, &data);
  1302. if (unlikely(!rq)) {
  1303. __wbt_done(q->rq_wb, wb_acct);
  1304. return BLK_QC_T_NONE;
  1305. }
  1306. wbt_track(&rq->issue_stat, wb_acct);
  1307. cookie = request_to_qc_t(data.hctx, rq);
  1308. plug = current->plug;
  1309. if (unlikely(is_flush_fua)) {
  1310. blk_mq_put_ctx(data.ctx);
  1311. blk_mq_bio_to_request(rq, bio);
  1312. if (q->elevator) {
  1313. blk_mq_sched_insert_request(rq, false, true, true,
  1314. true);
  1315. } else {
  1316. blk_insert_flush(rq);
  1317. blk_mq_run_hw_queue(data.hctx, true);
  1318. }
  1319. } else if (plug && q->nr_hw_queues == 1) {
  1320. struct request *last = NULL;
  1321. blk_mq_put_ctx(data.ctx);
  1322. blk_mq_bio_to_request(rq, bio);
  1323. /*
  1324. * @request_count may become stale because of schedule
  1325. * out, so check the list again.
  1326. */
  1327. if (list_empty(&plug->mq_list))
  1328. request_count = 0;
  1329. else if (blk_queue_nomerges(q))
  1330. request_count = blk_plug_queued_count(q);
  1331. if (!request_count)
  1332. trace_block_plug(q);
  1333. else
  1334. last = list_entry_rq(plug->mq_list.prev);
  1335. if (request_count >= BLK_MAX_REQUEST_COUNT || (last &&
  1336. blk_rq_bytes(last) >= BLK_PLUG_FLUSH_SIZE)) {
  1337. blk_flush_plug_list(plug, false);
  1338. trace_block_plug(q);
  1339. }
  1340. list_add_tail(&rq->queuelist, &plug->mq_list);
  1341. } else if (plug && !blk_queue_nomerges(q)) {
  1342. blk_mq_bio_to_request(rq, bio);
  1343. /*
  1344. * We do limited plugging. If the bio can be merged, do that.
  1345. * Otherwise the existing request in the plug list will be
  1346. * issued. So the plug list will have one request at most
  1347. * The plug list might get flushed before this. If that happens,
  1348. * the plug list is empty, and same_queue_rq is invalid.
  1349. */
  1350. if (list_empty(&plug->mq_list))
  1351. same_queue_rq = NULL;
  1352. if (same_queue_rq)
  1353. list_del_init(&same_queue_rq->queuelist);
  1354. list_add_tail(&rq->queuelist, &plug->mq_list);
  1355. blk_mq_put_ctx(data.ctx);
  1356. if (same_queue_rq)
  1357. blk_mq_try_issue_directly(data.hctx, same_queue_rq,
  1358. &cookie);
  1359. } else if (q->nr_hw_queues > 1 && is_sync) {
  1360. blk_mq_put_ctx(data.ctx);
  1361. blk_mq_bio_to_request(rq, bio);
  1362. blk_mq_try_issue_directly(data.hctx, rq, &cookie);
  1363. } else if (q->elevator) {
  1364. blk_mq_put_ctx(data.ctx);
  1365. blk_mq_bio_to_request(rq, bio);
  1366. blk_mq_sched_insert_request(rq, false, true, true, true);
  1367. } else if (!blk_mq_merge_queue_io(data.hctx, data.ctx, rq, bio)) {
  1368. blk_mq_put_ctx(data.ctx);
  1369. blk_mq_run_hw_queue(data.hctx, true);
  1370. } else
  1371. blk_mq_put_ctx(data.ctx);
  1372. return cookie;
  1373. }
  1374. void blk_mq_free_rqs(struct blk_mq_tag_set *set, struct blk_mq_tags *tags,
  1375. unsigned int hctx_idx)
  1376. {
  1377. struct page *page;
  1378. if (tags->rqs && set->ops->exit_request) {
  1379. int i;
  1380. for (i = 0; i < tags->nr_tags; i++) {
  1381. struct request *rq = tags->static_rqs[i];
  1382. if (!rq)
  1383. continue;
  1384. set->ops->exit_request(set, rq, hctx_idx);
  1385. tags->static_rqs[i] = NULL;
  1386. }
  1387. }
  1388. while (!list_empty(&tags->page_list)) {
  1389. page = list_first_entry(&tags->page_list, struct page, lru);
  1390. list_del_init(&page->lru);
  1391. /*
  1392. * Remove kmemleak object previously allocated in
  1393. * blk_mq_init_rq_map().
  1394. */
  1395. kmemleak_free(page_address(page));
  1396. __free_pages(page, page->private);
  1397. }
  1398. }
  1399. void blk_mq_free_rq_map(struct blk_mq_tags *tags)
  1400. {
  1401. kfree(tags->rqs);
  1402. tags->rqs = NULL;
  1403. kfree(tags->static_rqs);
  1404. tags->static_rqs = NULL;
  1405. blk_mq_free_tags(tags);
  1406. }
  1407. struct blk_mq_tags *blk_mq_alloc_rq_map(struct blk_mq_tag_set *set,
  1408. unsigned int hctx_idx,
  1409. unsigned int nr_tags,
  1410. unsigned int reserved_tags)
  1411. {
  1412. struct blk_mq_tags *tags;
  1413. int node;
  1414. node = blk_mq_hw_queue_to_node(set->mq_map, hctx_idx);
  1415. if (node == NUMA_NO_NODE)
  1416. node = set->numa_node;
  1417. tags = blk_mq_init_tags(nr_tags, reserved_tags, node,
  1418. BLK_MQ_FLAG_TO_ALLOC_POLICY(set->flags));
  1419. if (!tags)
  1420. return NULL;
  1421. tags->rqs = kzalloc_node(nr_tags * sizeof(struct request *),
  1422. GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY,
  1423. node);
  1424. if (!tags->rqs) {
  1425. blk_mq_free_tags(tags);
  1426. return NULL;
  1427. }
  1428. tags->static_rqs = kzalloc_node(nr_tags * sizeof(struct request *),
  1429. GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY,
  1430. node);
  1431. if (!tags->static_rqs) {
  1432. kfree(tags->rqs);
  1433. blk_mq_free_tags(tags);
  1434. return NULL;
  1435. }
  1436. return tags;
  1437. }
  1438. static size_t order_to_size(unsigned int order)
  1439. {
  1440. return (size_t)PAGE_SIZE << order;
  1441. }
  1442. int blk_mq_alloc_rqs(struct blk_mq_tag_set *set, struct blk_mq_tags *tags,
  1443. unsigned int hctx_idx, unsigned int depth)
  1444. {
  1445. unsigned int i, j, entries_per_page, max_order = 4;
  1446. size_t rq_size, left;
  1447. int node;
  1448. node = blk_mq_hw_queue_to_node(set->mq_map, hctx_idx);
  1449. if (node == NUMA_NO_NODE)
  1450. node = set->numa_node;
  1451. INIT_LIST_HEAD(&tags->page_list);
  1452. /*
  1453. * rq_size is the size of the request plus driver payload, rounded
  1454. * to the cacheline size
  1455. */
  1456. rq_size = round_up(sizeof(struct request) + set->cmd_size,
  1457. cache_line_size());
  1458. left = rq_size * depth;
  1459. for (i = 0; i < depth; ) {
  1460. int this_order = max_order;
  1461. struct page *page;
  1462. int to_do;
  1463. void *p;
  1464. while (this_order && left < order_to_size(this_order - 1))
  1465. this_order--;
  1466. do {
  1467. page = alloc_pages_node(node,
  1468. GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO,
  1469. this_order);
  1470. if (page)
  1471. break;
  1472. if (!this_order--)
  1473. break;
  1474. if (order_to_size(this_order) < rq_size)
  1475. break;
  1476. } while (1);
  1477. if (!page)
  1478. goto fail;
  1479. page->private = this_order;
  1480. list_add_tail(&page->lru, &tags->page_list);
  1481. p = page_address(page);
  1482. /*
  1483. * Allow kmemleak to scan these pages as they contain pointers
  1484. * to additional allocations like via ops->init_request().
  1485. */
  1486. kmemleak_alloc(p, order_to_size(this_order), 1, GFP_NOIO);
  1487. entries_per_page = order_to_size(this_order) / rq_size;
  1488. to_do = min(entries_per_page, depth - i);
  1489. left -= to_do * rq_size;
  1490. for (j = 0; j < to_do; j++) {
  1491. struct request *rq = p;
  1492. tags->static_rqs[i] = rq;
  1493. if (set->ops->init_request) {
  1494. if (set->ops->init_request(set, rq, hctx_idx,
  1495. node)) {
  1496. tags->static_rqs[i] = NULL;
  1497. goto fail;
  1498. }
  1499. }
  1500. p += rq_size;
  1501. i++;
  1502. }
  1503. }
  1504. return 0;
  1505. fail:
  1506. blk_mq_free_rqs(set, tags, hctx_idx);
  1507. return -ENOMEM;
  1508. }
  1509. /*
  1510. * 'cpu' is going away. splice any existing rq_list entries from this
  1511. * software queue to the hw queue dispatch list, and ensure that it
  1512. * gets run.
  1513. */
  1514. static int blk_mq_hctx_notify_dead(unsigned int cpu, struct hlist_node *node)
  1515. {
  1516. struct blk_mq_hw_ctx *hctx;
  1517. struct blk_mq_ctx *ctx;
  1518. LIST_HEAD(tmp);
  1519. hctx = hlist_entry_safe(node, struct blk_mq_hw_ctx, cpuhp_dead);
  1520. ctx = __blk_mq_get_ctx(hctx->queue, cpu);
  1521. spin_lock(&ctx->lock);
  1522. if (!list_empty(&ctx->rq_list)) {
  1523. list_splice_init(&ctx->rq_list, &tmp);
  1524. blk_mq_hctx_clear_pending(hctx, ctx);
  1525. }
  1526. spin_unlock(&ctx->lock);
  1527. if (list_empty(&tmp))
  1528. return 0;
  1529. spin_lock(&hctx->lock);
  1530. list_splice_tail_init(&tmp, &hctx->dispatch);
  1531. spin_unlock(&hctx->lock);
  1532. blk_mq_run_hw_queue(hctx, true);
  1533. return 0;
  1534. }
  1535. static void blk_mq_remove_cpuhp(struct blk_mq_hw_ctx *hctx)
  1536. {
  1537. cpuhp_state_remove_instance_nocalls(CPUHP_BLK_MQ_DEAD,
  1538. &hctx->cpuhp_dead);
  1539. }
  1540. /* hctx->ctxs will be freed in queue's release handler */
  1541. static void blk_mq_exit_hctx(struct request_queue *q,
  1542. struct blk_mq_tag_set *set,
  1543. struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx)
  1544. {
  1545. blk_mq_debugfs_unregister_hctx(hctx);
  1546. blk_mq_tag_idle(hctx);
  1547. if (set->ops->exit_request)
  1548. set->ops->exit_request(set, hctx->fq->flush_rq, hctx_idx);
  1549. blk_mq_sched_exit_hctx(q, hctx, hctx_idx);
  1550. if (set->ops->exit_hctx)
  1551. set->ops->exit_hctx(hctx, hctx_idx);
  1552. if (hctx->flags & BLK_MQ_F_BLOCKING)
  1553. cleanup_srcu_struct(&hctx->queue_rq_srcu);
  1554. blk_mq_remove_cpuhp(hctx);
  1555. blk_free_flush_queue(hctx->fq);
  1556. sbitmap_free(&hctx->ctx_map);
  1557. }
  1558. static void blk_mq_exit_hw_queues(struct request_queue *q,
  1559. struct blk_mq_tag_set *set, int nr_queue)
  1560. {
  1561. struct blk_mq_hw_ctx *hctx;
  1562. unsigned int i;
  1563. queue_for_each_hw_ctx(q, hctx, i) {
  1564. if (i == nr_queue)
  1565. break;
  1566. blk_mq_exit_hctx(q, set, hctx, i);
  1567. }
  1568. }
  1569. static int blk_mq_init_hctx(struct request_queue *q,
  1570. struct blk_mq_tag_set *set,
  1571. struct blk_mq_hw_ctx *hctx, unsigned hctx_idx)
  1572. {
  1573. int node;
  1574. node = hctx->numa_node;
  1575. if (node == NUMA_NO_NODE)
  1576. node = hctx->numa_node = set->numa_node;
  1577. INIT_DELAYED_WORK(&hctx->run_work, blk_mq_run_work_fn);
  1578. spin_lock_init(&hctx->lock);
  1579. INIT_LIST_HEAD(&hctx->dispatch);
  1580. hctx->queue = q;
  1581. hctx->queue_num = hctx_idx;
  1582. hctx->flags = set->flags & ~BLK_MQ_F_TAG_SHARED;
  1583. cpuhp_state_add_instance_nocalls(CPUHP_BLK_MQ_DEAD, &hctx->cpuhp_dead);
  1584. hctx->tags = set->tags[hctx_idx];
  1585. /*
  1586. * Allocate space for all possible cpus to avoid allocation at
  1587. * runtime
  1588. */
  1589. hctx->ctxs = kmalloc_node(nr_cpu_ids * sizeof(void *),
  1590. GFP_KERNEL, node);
  1591. if (!hctx->ctxs)
  1592. goto unregister_cpu_notifier;
  1593. if (sbitmap_init_node(&hctx->ctx_map, nr_cpu_ids, ilog2(8), GFP_KERNEL,
  1594. node))
  1595. goto free_ctxs;
  1596. hctx->nr_ctx = 0;
  1597. if (set->ops->init_hctx &&
  1598. set->ops->init_hctx(hctx, set->driver_data, hctx_idx))
  1599. goto free_bitmap;
  1600. if (blk_mq_sched_init_hctx(q, hctx, hctx_idx))
  1601. goto exit_hctx;
  1602. hctx->fq = blk_alloc_flush_queue(q, hctx->numa_node, set->cmd_size);
  1603. if (!hctx->fq)
  1604. goto sched_exit_hctx;
  1605. if (set->ops->init_request &&
  1606. set->ops->init_request(set, hctx->fq->flush_rq, hctx_idx,
  1607. node))
  1608. goto free_fq;
  1609. if (hctx->flags & BLK_MQ_F_BLOCKING)
  1610. init_srcu_struct(&hctx->queue_rq_srcu);
  1611. blk_mq_debugfs_register_hctx(q, hctx);
  1612. return 0;
  1613. free_fq:
  1614. kfree(hctx->fq);
  1615. sched_exit_hctx:
  1616. blk_mq_sched_exit_hctx(q, hctx, hctx_idx);
  1617. exit_hctx:
  1618. if (set->ops->exit_hctx)
  1619. set->ops->exit_hctx(hctx, hctx_idx);
  1620. free_bitmap:
  1621. sbitmap_free(&hctx->ctx_map);
  1622. free_ctxs:
  1623. kfree(hctx->ctxs);
  1624. unregister_cpu_notifier:
  1625. blk_mq_remove_cpuhp(hctx);
  1626. return -1;
  1627. }
  1628. static void blk_mq_init_cpu_queues(struct request_queue *q,
  1629. unsigned int nr_hw_queues)
  1630. {
  1631. unsigned int i;
  1632. for_each_possible_cpu(i) {
  1633. struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i);
  1634. struct blk_mq_hw_ctx *hctx;
  1635. __ctx->cpu = i;
  1636. spin_lock_init(&__ctx->lock);
  1637. INIT_LIST_HEAD(&__ctx->rq_list);
  1638. __ctx->queue = q;
  1639. /* If the cpu isn't online, the cpu is mapped to first hctx */
  1640. if (!cpu_online(i))
  1641. continue;
  1642. hctx = blk_mq_map_queue(q, i);
  1643. /*
  1644. * Set local node, IFF we have more than one hw queue. If
  1645. * not, we remain on the home node of the device
  1646. */
  1647. if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE)
  1648. hctx->numa_node = local_memory_node(cpu_to_node(i));
  1649. }
  1650. }
  1651. static bool __blk_mq_alloc_rq_map(struct blk_mq_tag_set *set, int hctx_idx)
  1652. {
  1653. int ret = 0;
  1654. set->tags[hctx_idx] = blk_mq_alloc_rq_map(set, hctx_idx,
  1655. set->queue_depth, set->reserved_tags);
  1656. if (!set->tags[hctx_idx])
  1657. return false;
  1658. ret = blk_mq_alloc_rqs(set, set->tags[hctx_idx], hctx_idx,
  1659. set->queue_depth);
  1660. if (!ret)
  1661. return true;
  1662. blk_mq_free_rq_map(set->tags[hctx_idx]);
  1663. set->tags[hctx_idx] = NULL;
  1664. return false;
  1665. }
  1666. static void blk_mq_free_map_and_requests(struct blk_mq_tag_set *set,
  1667. unsigned int hctx_idx)
  1668. {
  1669. if (set->tags[hctx_idx]) {
  1670. blk_mq_free_rqs(set, set->tags[hctx_idx], hctx_idx);
  1671. blk_mq_free_rq_map(set->tags[hctx_idx]);
  1672. set->tags[hctx_idx] = NULL;
  1673. }
  1674. }
  1675. static void blk_mq_map_swqueue(struct request_queue *q,
  1676. const struct cpumask *online_mask)
  1677. {
  1678. unsigned int i, hctx_idx;
  1679. struct blk_mq_hw_ctx *hctx;
  1680. struct blk_mq_ctx *ctx;
  1681. struct blk_mq_tag_set *set = q->tag_set;
  1682. /*
  1683. * Avoid others reading imcomplete hctx->cpumask through sysfs
  1684. */
  1685. mutex_lock(&q->sysfs_lock);
  1686. queue_for_each_hw_ctx(q, hctx, i) {
  1687. cpumask_clear(hctx->cpumask);
  1688. hctx->nr_ctx = 0;
  1689. }
  1690. /*
  1691. * Map software to hardware queues
  1692. */
  1693. for_each_possible_cpu(i) {
  1694. /* If the cpu isn't online, the cpu is mapped to first hctx */
  1695. if (!cpumask_test_cpu(i, online_mask))
  1696. continue;
  1697. hctx_idx = q->mq_map[i];
  1698. /* unmapped hw queue can be remapped after CPU topo changed */
  1699. if (!set->tags[hctx_idx] &&
  1700. !__blk_mq_alloc_rq_map(set, hctx_idx)) {
  1701. /*
  1702. * If tags initialization fail for some hctx,
  1703. * that hctx won't be brought online. In this
  1704. * case, remap the current ctx to hctx[0] which
  1705. * is guaranteed to always have tags allocated
  1706. */
  1707. q->mq_map[i] = 0;
  1708. }
  1709. ctx = per_cpu_ptr(q->queue_ctx, i);
  1710. hctx = blk_mq_map_queue(q, i);
  1711. cpumask_set_cpu(i, hctx->cpumask);
  1712. ctx->index_hw = hctx->nr_ctx;
  1713. hctx->ctxs[hctx->nr_ctx++] = ctx;
  1714. }
  1715. mutex_unlock(&q->sysfs_lock);
  1716. queue_for_each_hw_ctx(q, hctx, i) {
  1717. /*
  1718. * If no software queues are mapped to this hardware queue,
  1719. * disable it and free the request entries.
  1720. */
  1721. if (!hctx->nr_ctx) {
  1722. /* Never unmap queue 0. We need it as a
  1723. * fallback in case of a new remap fails
  1724. * allocation
  1725. */
  1726. if (i && set->tags[i])
  1727. blk_mq_free_map_and_requests(set, i);
  1728. hctx->tags = NULL;
  1729. continue;
  1730. }
  1731. hctx->tags = set->tags[i];
  1732. WARN_ON(!hctx->tags);
  1733. /*
  1734. * Set the map size to the number of mapped software queues.
  1735. * This is more accurate and more efficient than looping
  1736. * over all possibly mapped software queues.
  1737. */
  1738. sbitmap_resize(&hctx->ctx_map, hctx->nr_ctx);
  1739. /*
  1740. * Initialize batch roundrobin counts
  1741. */
  1742. hctx->next_cpu = cpumask_first(hctx->cpumask);
  1743. hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
  1744. }
  1745. }
  1746. static void queue_set_hctx_shared(struct request_queue *q, bool shared)
  1747. {
  1748. struct blk_mq_hw_ctx *hctx;
  1749. int i;
  1750. queue_for_each_hw_ctx(q, hctx, i) {
  1751. if (shared)
  1752. hctx->flags |= BLK_MQ_F_TAG_SHARED;
  1753. else
  1754. hctx->flags &= ~BLK_MQ_F_TAG_SHARED;
  1755. }
  1756. }
  1757. static void blk_mq_update_tag_set_depth(struct blk_mq_tag_set *set, bool shared)
  1758. {
  1759. struct request_queue *q;
  1760. lockdep_assert_held(&set->tag_list_lock);
  1761. list_for_each_entry(q, &set->tag_list, tag_set_list) {
  1762. blk_mq_freeze_queue(q);
  1763. queue_set_hctx_shared(q, shared);
  1764. blk_mq_unfreeze_queue(q);
  1765. }
  1766. }
  1767. static void blk_mq_del_queue_tag_set(struct request_queue *q)
  1768. {
  1769. struct blk_mq_tag_set *set = q->tag_set;
  1770. mutex_lock(&set->tag_list_lock);
  1771. list_del_rcu(&q->tag_set_list);
  1772. INIT_LIST_HEAD(&q->tag_set_list);
  1773. if (list_is_singular(&set->tag_list)) {
  1774. /* just transitioned to unshared */
  1775. set->flags &= ~BLK_MQ_F_TAG_SHARED;
  1776. /* update existing queue */
  1777. blk_mq_update_tag_set_depth(set, false);
  1778. }
  1779. mutex_unlock(&set->tag_list_lock);
  1780. synchronize_rcu();
  1781. }
  1782. static void blk_mq_add_queue_tag_set(struct blk_mq_tag_set *set,
  1783. struct request_queue *q)
  1784. {
  1785. q->tag_set = set;
  1786. mutex_lock(&set->tag_list_lock);
  1787. /* Check to see if we're transitioning to shared (from 1 to 2 queues). */
  1788. if (!list_empty(&set->tag_list) && !(set->flags & BLK_MQ_F_TAG_SHARED)) {
  1789. set->flags |= BLK_MQ_F_TAG_SHARED;
  1790. /* update existing queue */
  1791. blk_mq_update_tag_set_depth(set, true);
  1792. }
  1793. if (set->flags & BLK_MQ_F_TAG_SHARED)
  1794. queue_set_hctx_shared(q, true);
  1795. list_add_tail_rcu(&q->tag_set_list, &set->tag_list);
  1796. mutex_unlock(&set->tag_list_lock);
  1797. }
  1798. /*
  1799. * It is the actual release handler for mq, but we do it from
  1800. * request queue's release handler for avoiding use-after-free
  1801. * and headache because q->mq_kobj shouldn't have been introduced,
  1802. * but we can't group ctx/kctx kobj without it.
  1803. */
  1804. void blk_mq_release(struct request_queue *q)
  1805. {
  1806. struct blk_mq_hw_ctx *hctx;
  1807. unsigned int i;
  1808. /* hctx kobj stays in hctx */
  1809. queue_for_each_hw_ctx(q, hctx, i) {
  1810. if (!hctx)
  1811. continue;
  1812. kobject_put(&hctx->kobj);
  1813. }
  1814. q->mq_map = NULL;
  1815. kfree(q->queue_hw_ctx);
  1816. /*
  1817. * release .mq_kobj and sw queue's kobject now because
  1818. * both share lifetime with request queue.
  1819. */
  1820. blk_mq_sysfs_deinit(q);
  1821. free_percpu(q->queue_ctx);
  1822. }
  1823. struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set)
  1824. {
  1825. struct request_queue *uninit_q, *q;
  1826. uninit_q = blk_alloc_queue_node(GFP_KERNEL, set->numa_node);
  1827. if (!uninit_q)
  1828. return ERR_PTR(-ENOMEM);
  1829. q = blk_mq_init_allocated_queue(set, uninit_q);
  1830. if (IS_ERR(q))
  1831. blk_cleanup_queue(uninit_q);
  1832. return q;
  1833. }
  1834. EXPORT_SYMBOL(blk_mq_init_queue);
  1835. static void blk_mq_realloc_hw_ctxs(struct blk_mq_tag_set *set,
  1836. struct request_queue *q)
  1837. {
  1838. int i, j;
  1839. struct blk_mq_hw_ctx **hctxs = q->queue_hw_ctx;
  1840. blk_mq_sysfs_unregister(q);
  1841. for (i = 0; i < set->nr_hw_queues; i++) {
  1842. int node;
  1843. if (hctxs[i])
  1844. continue;
  1845. node = blk_mq_hw_queue_to_node(q->mq_map, i);
  1846. hctxs[i] = kzalloc_node(sizeof(struct blk_mq_hw_ctx),
  1847. GFP_KERNEL, node);
  1848. if (!hctxs[i])
  1849. break;
  1850. if (!zalloc_cpumask_var_node(&hctxs[i]->cpumask, GFP_KERNEL,
  1851. node)) {
  1852. kfree(hctxs[i]);
  1853. hctxs[i] = NULL;
  1854. break;
  1855. }
  1856. atomic_set(&hctxs[i]->nr_active, 0);
  1857. hctxs[i]->numa_node = node;
  1858. hctxs[i]->queue_num = i;
  1859. if (blk_mq_init_hctx(q, set, hctxs[i], i)) {
  1860. free_cpumask_var(hctxs[i]->cpumask);
  1861. kfree(hctxs[i]);
  1862. hctxs[i] = NULL;
  1863. break;
  1864. }
  1865. blk_mq_hctx_kobj_init(hctxs[i]);
  1866. }
  1867. for (j = i; j < q->nr_hw_queues; j++) {
  1868. struct blk_mq_hw_ctx *hctx = hctxs[j];
  1869. if (hctx) {
  1870. if (hctx->tags)
  1871. blk_mq_free_map_and_requests(set, j);
  1872. blk_mq_exit_hctx(q, set, hctx, j);
  1873. kobject_put(&hctx->kobj);
  1874. hctxs[j] = NULL;
  1875. }
  1876. }
  1877. q->nr_hw_queues = i;
  1878. blk_mq_sysfs_register(q);
  1879. }
  1880. struct request_queue *blk_mq_init_allocated_queue(struct blk_mq_tag_set *set,
  1881. struct request_queue *q)
  1882. {
  1883. /* mark the queue as mq asap */
  1884. q->mq_ops = set->ops;
  1885. q->poll_cb = blk_stat_alloc_callback(blk_mq_poll_stats_fn,
  1886. blk_mq_poll_stats_bkt,
  1887. BLK_MQ_POLL_STATS_BKTS, q);
  1888. if (!q->poll_cb)
  1889. goto err_exit;
  1890. q->queue_ctx = alloc_percpu(struct blk_mq_ctx);
  1891. if (!q->queue_ctx)
  1892. goto err_exit;
  1893. /* init q->mq_kobj and sw queues' kobjects */
  1894. blk_mq_sysfs_init(q);
  1895. q->queue_hw_ctx = kzalloc_node(nr_cpu_ids * sizeof(*(q->queue_hw_ctx)),
  1896. GFP_KERNEL, set->numa_node);
  1897. if (!q->queue_hw_ctx)
  1898. goto err_percpu;
  1899. q->mq_map = set->mq_map;
  1900. blk_mq_realloc_hw_ctxs(set, q);
  1901. if (!q->nr_hw_queues)
  1902. goto err_hctxs;
  1903. INIT_WORK(&q->timeout_work, blk_mq_timeout_work);
  1904. blk_queue_rq_timeout(q, set->timeout ? set->timeout : 30 * HZ);
  1905. q->nr_queues = nr_cpu_ids;
  1906. q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT;
  1907. if (!(set->flags & BLK_MQ_F_SG_MERGE))
  1908. q->queue_flags |= 1 << QUEUE_FLAG_NO_SG_MERGE;
  1909. q->sg_reserved_size = INT_MAX;
  1910. INIT_DELAYED_WORK(&q->requeue_work, blk_mq_requeue_work);
  1911. INIT_LIST_HEAD(&q->requeue_list);
  1912. spin_lock_init(&q->requeue_lock);
  1913. blk_queue_make_request(q, blk_mq_make_request);
  1914. /*
  1915. * Do this after blk_queue_make_request() overrides it...
  1916. */
  1917. q->nr_requests = set->queue_depth;
  1918. /*
  1919. * Default to classic polling
  1920. */
  1921. q->poll_nsec = -1;
  1922. if (set->ops->complete)
  1923. blk_queue_softirq_done(q, set->ops->complete);
  1924. blk_mq_init_cpu_queues(q, set->nr_hw_queues);
  1925. get_online_cpus();
  1926. mutex_lock(&all_q_mutex);
  1927. list_add_tail(&q->all_q_node, &all_q_list);
  1928. blk_mq_add_queue_tag_set(set, q);
  1929. blk_mq_map_swqueue(q, cpu_online_mask);
  1930. mutex_unlock(&all_q_mutex);
  1931. put_online_cpus();
  1932. if (!(set->flags & BLK_MQ_F_NO_SCHED)) {
  1933. int ret;
  1934. ret = blk_mq_sched_init(q);
  1935. if (ret)
  1936. return ERR_PTR(ret);
  1937. }
  1938. return q;
  1939. err_hctxs:
  1940. kfree(q->queue_hw_ctx);
  1941. err_percpu:
  1942. free_percpu(q->queue_ctx);
  1943. err_exit:
  1944. q->mq_ops = NULL;
  1945. return ERR_PTR(-ENOMEM);
  1946. }
  1947. EXPORT_SYMBOL(blk_mq_init_allocated_queue);
  1948. void blk_mq_free_queue(struct request_queue *q)
  1949. {
  1950. struct blk_mq_tag_set *set = q->tag_set;
  1951. mutex_lock(&all_q_mutex);
  1952. list_del_init(&q->all_q_node);
  1953. mutex_unlock(&all_q_mutex);
  1954. blk_mq_del_queue_tag_set(q);
  1955. blk_mq_exit_hw_queues(q, set, set->nr_hw_queues);
  1956. }
  1957. /* Basically redo blk_mq_init_queue with queue frozen */
  1958. static void blk_mq_queue_reinit(struct request_queue *q,
  1959. const struct cpumask *online_mask)
  1960. {
  1961. WARN_ON_ONCE(!atomic_read(&q->mq_freeze_depth));
  1962. blk_mq_debugfs_unregister_hctxs(q);
  1963. blk_mq_sysfs_unregister(q);
  1964. /*
  1965. * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe
  1966. * we should change hctx numa_node according to new topology (this
  1967. * involves free and re-allocate memory, worthy doing?)
  1968. */
  1969. blk_mq_map_swqueue(q, online_mask);
  1970. blk_mq_sysfs_register(q);
  1971. blk_mq_debugfs_register_hctxs(q);
  1972. }
  1973. /*
  1974. * New online cpumask which is going to be set in this hotplug event.
  1975. * Declare this cpumasks as global as cpu-hotplug operation is invoked
  1976. * one-by-one and dynamically allocating this could result in a failure.
  1977. */
  1978. static struct cpumask cpuhp_online_new;
  1979. static void blk_mq_queue_reinit_work(void)
  1980. {
  1981. struct request_queue *q;
  1982. mutex_lock(&all_q_mutex);
  1983. /*
  1984. * We need to freeze and reinit all existing queues. Freezing
  1985. * involves synchronous wait for an RCU grace period and doing it
  1986. * one by one may take a long time. Start freezing all queues in
  1987. * one swoop and then wait for the completions so that freezing can
  1988. * take place in parallel.
  1989. */
  1990. list_for_each_entry(q, &all_q_list, all_q_node)
  1991. blk_freeze_queue_start(q);
  1992. list_for_each_entry(q, &all_q_list, all_q_node)
  1993. blk_mq_freeze_queue_wait(q);
  1994. list_for_each_entry(q, &all_q_list, all_q_node)
  1995. blk_mq_queue_reinit(q, &cpuhp_online_new);
  1996. list_for_each_entry(q, &all_q_list, all_q_node)
  1997. blk_mq_unfreeze_queue(q);
  1998. mutex_unlock(&all_q_mutex);
  1999. }
  2000. static int blk_mq_queue_reinit_dead(unsigned int cpu)
  2001. {
  2002. cpumask_copy(&cpuhp_online_new, cpu_online_mask);
  2003. blk_mq_queue_reinit_work();
  2004. return 0;
  2005. }
  2006. /*
  2007. * Before hotadded cpu starts handling requests, new mappings must be
  2008. * established. Otherwise, these requests in hw queue might never be
  2009. * dispatched.
  2010. *
  2011. * For example, there is a single hw queue (hctx) and two CPU queues (ctx0
  2012. * for CPU0, and ctx1 for CPU1).
  2013. *
  2014. * Now CPU1 is just onlined and a request is inserted into ctx1->rq_list
  2015. * and set bit0 in pending bitmap as ctx1->index_hw is still zero.
  2016. *
  2017. * And then while running hw queue, blk_mq_flush_busy_ctxs() finds bit0 is set
  2018. * in pending bitmap and tries to retrieve requests in hctx->ctxs[0]->rq_list.
  2019. * But htx->ctxs[0] is a pointer to ctx0, so the request in ctx1->rq_list is
  2020. * ignored.
  2021. */
  2022. static int blk_mq_queue_reinit_prepare(unsigned int cpu)
  2023. {
  2024. cpumask_copy(&cpuhp_online_new, cpu_online_mask);
  2025. cpumask_set_cpu(cpu, &cpuhp_online_new);
  2026. blk_mq_queue_reinit_work();
  2027. return 0;
  2028. }
  2029. static int __blk_mq_alloc_rq_maps(struct blk_mq_tag_set *set)
  2030. {
  2031. int i;
  2032. for (i = 0; i < set->nr_hw_queues; i++)
  2033. if (!__blk_mq_alloc_rq_map(set, i))
  2034. goto out_unwind;
  2035. return 0;
  2036. out_unwind:
  2037. while (--i >= 0)
  2038. blk_mq_free_rq_map(set->tags[i]);
  2039. return -ENOMEM;
  2040. }
  2041. /*
  2042. * Allocate the request maps associated with this tag_set. Note that this
  2043. * may reduce the depth asked for, if memory is tight. set->queue_depth
  2044. * will be updated to reflect the allocated depth.
  2045. */
  2046. static int blk_mq_alloc_rq_maps(struct blk_mq_tag_set *set)
  2047. {
  2048. unsigned int depth;
  2049. int err;
  2050. depth = set->queue_depth;
  2051. do {
  2052. err = __blk_mq_alloc_rq_maps(set);
  2053. if (!err)
  2054. break;
  2055. set->queue_depth >>= 1;
  2056. if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN) {
  2057. err = -ENOMEM;
  2058. break;
  2059. }
  2060. } while (set->queue_depth);
  2061. if (!set->queue_depth || err) {
  2062. pr_err("blk-mq: failed to allocate request map\n");
  2063. return -ENOMEM;
  2064. }
  2065. if (depth != set->queue_depth)
  2066. pr_info("blk-mq: reduced tag depth (%u -> %u)\n",
  2067. depth, set->queue_depth);
  2068. return 0;
  2069. }
  2070. static int blk_mq_update_queue_map(struct blk_mq_tag_set *set)
  2071. {
  2072. if (set->ops->map_queues)
  2073. return set->ops->map_queues(set);
  2074. else
  2075. return blk_mq_map_queues(set);
  2076. }
  2077. /*
  2078. * Alloc a tag set to be associated with one or more request queues.
  2079. * May fail with EINVAL for various error conditions. May adjust the
  2080. * requested depth down, if if it too large. In that case, the set
  2081. * value will be stored in set->queue_depth.
  2082. */
  2083. int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set)
  2084. {
  2085. int ret;
  2086. BUILD_BUG_ON(BLK_MQ_MAX_DEPTH > 1 << BLK_MQ_UNIQUE_TAG_BITS);
  2087. if (!set->nr_hw_queues)
  2088. return -EINVAL;
  2089. if (!set->queue_depth)
  2090. return -EINVAL;
  2091. if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN)
  2092. return -EINVAL;
  2093. if (!set->ops->queue_rq)
  2094. return -EINVAL;
  2095. if (set->queue_depth > BLK_MQ_MAX_DEPTH) {
  2096. pr_info("blk-mq: reduced tag depth to %u\n",
  2097. BLK_MQ_MAX_DEPTH);
  2098. set->queue_depth = BLK_MQ_MAX_DEPTH;
  2099. }
  2100. /*
  2101. * If a crashdump is active, then we are potentially in a very
  2102. * memory constrained environment. Limit us to 1 queue and
  2103. * 64 tags to prevent using too much memory.
  2104. */
  2105. if (is_kdump_kernel()) {
  2106. set->nr_hw_queues = 1;
  2107. set->queue_depth = min(64U, set->queue_depth);
  2108. }
  2109. /*
  2110. * There is no use for more h/w queues than cpus.
  2111. */
  2112. if (set->nr_hw_queues > nr_cpu_ids)
  2113. set->nr_hw_queues = nr_cpu_ids;
  2114. set->tags = kzalloc_node(nr_cpu_ids * sizeof(struct blk_mq_tags *),
  2115. GFP_KERNEL, set->numa_node);
  2116. if (!set->tags)
  2117. return -ENOMEM;
  2118. ret = -ENOMEM;
  2119. set->mq_map = kzalloc_node(sizeof(*set->mq_map) * nr_cpu_ids,
  2120. GFP_KERNEL, set->numa_node);
  2121. if (!set->mq_map)
  2122. goto out_free_tags;
  2123. ret = blk_mq_update_queue_map(set);
  2124. if (ret)
  2125. goto out_free_mq_map;
  2126. ret = blk_mq_alloc_rq_maps(set);
  2127. if (ret)
  2128. goto out_free_mq_map;
  2129. mutex_init(&set->tag_list_lock);
  2130. INIT_LIST_HEAD(&set->tag_list);
  2131. return 0;
  2132. out_free_mq_map:
  2133. kfree(set->mq_map);
  2134. set->mq_map = NULL;
  2135. out_free_tags:
  2136. kfree(set->tags);
  2137. set->tags = NULL;
  2138. return ret;
  2139. }
  2140. EXPORT_SYMBOL(blk_mq_alloc_tag_set);
  2141. void blk_mq_free_tag_set(struct blk_mq_tag_set *set)
  2142. {
  2143. int i;
  2144. for (i = 0; i < nr_cpu_ids; i++)
  2145. blk_mq_free_map_and_requests(set, i);
  2146. kfree(set->mq_map);
  2147. set->mq_map = NULL;
  2148. kfree(set->tags);
  2149. set->tags = NULL;
  2150. }
  2151. EXPORT_SYMBOL(blk_mq_free_tag_set);
  2152. int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr)
  2153. {
  2154. struct blk_mq_tag_set *set = q->tag_set;
  2155. struct blk_mq_hw_ctx *hctx;
  2156. int i, ret;
  2157. if (!set)
  2158. return -EINVAL;
  2159. blk_mq_freeze_queue(q);
  2160. ret = 0;
  2161. queue_for_each_hw_ctx(q, hctx, i) {
  2162. if (!hctx->tags)
  2163. continue;
  2164. /*
  2165. * If we're using an MQ scheduler, just update the scheduler
  2166. * queue depth. This is similar to what the old code would do.
  2167. */
  2168. if (!hctx->sched_tags) {
  2169. ret = blk_mq_tag_update_depth(hctx, &hctx->tags,
  2170. min(nr, set->queue_depth),
  2171. false);
  2172. } else {
  2173. ret = blk_mq_tag_update_depth(hctx, &hctx->sched_tags,
  2174. nr, true);
  2175. }
  2176. if (ret)
  2177. break;
  2178. }
  2179. if (!ret)
  2180. q->nr_requests = nr;
  2181. blk_mq_unfreeze_queue(q);
  2182. return ret;
  2183. }
  2184. static void __blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set,
  2185. int nr_hw_queues)
  2186. {
  2187. struct request_queue *q;
  2188. lockdep_assert_held(&set->tag_list_lock);
  2189. if (nr_hw_queues > nr_cpu_ids)
  2190. nr_hw_queues = nr_cpu_ids;
  2191. if (nr_hw_queues < 1 || nr_hw_queues == set->nr_hw_queues)
  2192. return;
  2193. list_for_each_entry(q, &set->tag_list, tag_set_list)
  2194. blk_mq_freeze_queue(q);
  2195. set->nr_hw_queues = nr_hw_queues;
  2196. blk_mq_update_queue_map(set);
  2197. list_for_each_entry(q, &set->tag_list, tag_set_list) {
  2198. blk_mq_realloc_hw_ctxs(set, q);
  2199. blk_mq_queue_reinit(q, cpu_online_mask);
  2200. }
  2201. list_for_each_entry(q, &set->tag_list, tag_set_list)
  2202. blk_mq_unfreeze_queue(q);
  2203. }
  2204. void blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set, int nr_hw_queues)
  2205. {
  2206. mutex_lock(&set->tag_list_lock);
  2207. __blk_mq_update_nr_hw_queues(set, nr_hw_queues);
  2208. mutex_unlock(&set->tag_list_lock);
  2209. }
  2210. EXPORT_SYMBOL_GPL(blk_mq_update_nr_hw_queues);
  2211. /* Enable polling stats and return whether they were already enabled. */
  2212. static bool blk_poll_stats_enable(struct request_queue *q)
  2213. {
  2214. if (test_bit(QUEUE_FLAG_POLL_STATS, &q->queue_flags) ||
  2215. test_and_set_bit(QUEUE_FLAG_POLL_STATS, &q->queue_flags))
  2216. return true;
  2217. blk_stat_add_callback(q, q->poll_cb);
  2218. return false;
  2219. }
  2220. static void blk_mq_poll_stats_start(struct request_queue *q)
  2221. {
  2222. /*
  2223. * We don't arm the callback if polling stats are not enabled or the
  2224. * callback is already active.
  2225. */
  2226. if (!test_bit(QUEUE_FLAG_POLL_STATS, &q->queue_flags) ||
  2227. blk_stat_is_active(q->poll_cb))
  2228. return;
  2229. blk_stat_activate_msecs(q->poll_cb, 100);
  2230. }
  2231. static void blk_mq_poll_stats_fn(struct blk_stat_callback *cb)
  2232. {
  2233. struct request_queue *q = cb->data;
  2234. int bucket;
  2235. for (bucket = 0; bucket < BLK_MQ_POLL_STATS_BKTS; bucket++) {
  2236. if (cb->stat[bucket].nr_samples)
  2237. q->poll_stat[bucket] = cb->stat[bucket];
  2238. }
  2239. }
  2240. static unsigned long blk_mq_poll_nsecs(struct request_queue *q,
  2241. struct blk_mq_hw_ctx *hctx,
  2242. struct request *rq)
  2243. {
  2244. unsigned long ret = 0;
  2245. int bucket;
  2246. /*
  2247. * If stats collection isn't on, don't sleep but turn it on for
  2248. * future users
  2249. */
  2250. if (!blk_poll_stats_enable(q))
  2251. return 0;
  2252. /*
  2253. * As an optimistic guess, use half of the mean service time
  2254. * for this type of request. We can (and should) make this smarter.
  2255. * For instance, if the completion latencies are tight, we can
  2256. * get closer than just half the mean. This is especially
  2257. * important on devices where the completion latencies are longer
  2258. * than ~10 usec. We do use the stats for the relevant IO size
  2259. * if available which does lead to better estimates.
  2260. */
  2261. bucket = blk_mq_poll_stats_bkt(rq);
  2262. if (bucket < 0)
  2263. return ret;
  2264. if (q->poll_stat[bucket].nr_samples)
  2265. ret = (q->poll_stat[bucket].mean + 1) / 2;
  2266. return ret;
  2267. }
  2268. static bool blk_mq_poll_hybrid_sleep(struct request_queue *q,
  2269. struct blk_mq_hw_ctx *hctx,
  2270. struct request *rq)
  2271. {
  2272. struct hrtimer_sleeper hs;
  2273. enum hrtimer_mode mode;
  2274. unsigned int nsecs;
  2275. ktime_t kt;
  2276. if (test_bit(REQ_ATOM_POLL_SLEPT, &rq->atomic_flags))
  2277. return false;
  2278. /*
  2279. * poll_nsec can be:
  2280. *
  2281. * -1: don't ever hybrid sleep
  2282. * 0: use half of prev avg
  2283. * >0: use this specific value
  2284. */
  2285. if (q->poll_nsec == -1)
  2286. return false;
  2287. else if (q->poll_nsec > 0)
  2288. nsecs = q->poll_nsec;
  2289. else
  2290. nsecs = blk_mq_poll_nsecs(q, hctx, rq);
  2291. if (!nsecs)
  2292. return false;
  2293. set_bit(REQ_ATOM_POLL_SLEPT, &rq->atomic_flags);
  2294. /*
  2295. * This will be replaced with the stats tracking code, using
  2296. * 'avg_completion_time / 2' as the pre-sleep target.
  2297. */
  2298. kt = nsecs;
  2299. mode = HRTIMER_MODE_REL;
  2300. hrtimer_init_on_stack(&hs.timer, CLOCK_MONOTONIC, mode);
  2301. hrtimer_set_expires(&hs.timer, kt);
  2302. hrtimer_init_sleeper(&hs, current);
  2303. do {
  2304. if (test_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags))
  2305. break;
  2306. set_current_state(TASK_UNINTERRUPTIBLE);
  2307. hrtimer_start_expires(&hs.timer, mode);
  2308. if (hs.task)
  2309. io_schedule();
  2310. hrtimer_cancel(&hs.timer);
  2311. mode = HRTIMER_MODE_ABS;
  2312. } while (hs.task && !signal_pending(current));
  2313. __set_current_state(TASK_RUNNING);
  2314. destroy_hrtimer_on_stack(&hs.timer);
  2315. return true;
  2316. }
  2317. static bool __blk_mq_poll(struct blk_mq_hw_ctx *hctx, struct request *rq)
  2318. {
  2319. struct request_queue *q = hctx->queue;
  2320. long state;
  2321. /*
  2322. * If we sleep, have the caller restart the poll loop to reset
  2323. * the state. Like for the other success return cases, the
  2324. * caller is responsible for checking if the IO completed. If
  2325. * the IO isn't complete, we'll get called again and will go
  2326. * straight to the busy poll loop.
  2327. */
  2328. if (blk_mq_poll_hybrid_sleep(q, hctx, rq))
  2329. return true;
  2330. hctx->poll_considered++;
  2331. state = current->state;
  2332. while (!need_resched()) {
  2333. int ret;
  2334. hctx->poll_invoked++;
  2335. ret = q->mq_ops->poll(hctx, rq->tag);
  2336. if (ret > 0) {
  2337. hctx->poll_success++;
  2338. set_current_state(TASK_RUNNING);
  2339. return true;
  2340. }
  2341. if (signal_pending_state(state, current))
  2342. set_current_state(TASK_RUNNING);
  2343. if (current->state == TASK_RUNNING)
  2344. return true;
  2345. if (ret < 0)
  2346. break;
  2347. cpu_relax();
  2348. }
  2349. return false;
  2350. }
  2351. bool blk_mq_poll(struct request_queue *q, blk_qc_t cookie)
  2352. {
  2353. struct blk_mq_hw_ctx *hctx;
  2354. struct blk_plug *plug;
  2355. struct request *rq;
  2356. if (!q->mq_ops || !q->mq_ops->poll || !blk_qc_t_valid(cookie) ||
  2357. !test_bit(QUEUE_FLAG_POLL, &q->queue_flags))
  2358. return false;
  2359. plug = current->plug;
  2360. if (plug)
  2361. blk_flush_plug_list(plug, false);
  2362. hctx = q->queue_hw_ctx[blk_qc_t_to_queue_num(cookie)];
  2363. if (!blk_qc_t_is_internal(cookie))
  2364. rq = blk_mq_tag_to_rq(hctx->tags, blk_qc_t_to_tag(cookie));
  2365. else {
  2366. rq = blk_mq_tag_to_rq(hctx->sched_tags, blk_qc_t_to_tag(cookie));
  2367. /*
  2368. * With scheduling, if the request has completed, we'll
  2369. * get a NULL return here, as we clear the sched tag when
  2370. * that happens. The request still remains valid, like always,
  2371. * so we should be safe with just the NULL check.
  2372. */
  2373. if (!rq)
  2374. return false;
  2375. }
  2376. return __blk_mq_poll(hctx, rq);
  2377. }
  2378. EXPORT_SYMBOL_GPL(blk_mq_poll);
  2379. void blk_mq_disable_hotplug(void)
  2380. {
  2381. mutex_lock(&all_q_mutex);
  2382. }
  2383. void blk_mq_enable_hotplug(void)
  2384. {
  2385. mutex_unlock(&all_q_mutex);
  2386. }
  2387. static int __init blk_mq_init(void)
  2388. {
  2389. cpuhp_setup_state_multi(CPUHP_BLK_MQ_DEAD, "block/mq:dead", NULL,
  2390. blk_mq_hctx_notify_dead);
  2391. cpuhp_setup_state_nocalls(CPUHP_BLK_MQ_PREPARE, "block/mq:prepare",
  2392. blk_mq_queue_reinit_prepare,
  2393. blk_mq_queue_reinit_dead);
  2394. return 0;
  2395. }
  2396. subsys_initcall(blk_mq_init);