blk-mq.c 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304
  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-pm.h"
  35. #include "blk-stat.h"
  36. #include "blk-mq-sched.h"
  37. #include "blk-rq-qos.h"
  38. static bool blk_mq_poll(struct request_queue *q, blk_qc_t cookie);
  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 int blk_mq_poll_stats_bkt(const struct request *rq)
  42. {
  43. int ddir, bytes, bucket;
  44. ddir = rq_data_dir(rq);
  45. bytes = blk_rq_bytes(rq);
  46. bucket = ddir + 2*(ilog2(bytes) - 9);
  47. if (bucket < 0)
  48. return -1;
  49. else if (bucket >= BLK_MQ_POLL_STATS_BKTS)
  50. return ddir + BLK_MQ_POLL_STATS_BKTS - 2;
  51. return bucket;
  52. }
  53. /*
  54. * Check if any of the ctx's have pending work in this hardware queue
  55. */
  56. static bool blk_mq_hctx_has_pending(struct blk_mq_hw_ctx *hctx)
  57. {
  58. return !list_empty_careful(&hctx->dispatch) ||
  59. sbitmap_any_bit_set(&hctx->ctx_map) ||
  60. blk_mq_sched_has_work(hctx);
  61. }
  62. /*
  63. * Mark this ctx as having pending work in this hardware queue
  64. */
  65. static void blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx *hctx,
  66. struct blk_mq_ctx *ctx)
  67. {
  68. if (!sbitmap_test_bit(&hctx->ctx_map, ctx->index_hw))
  69. sbitmap_set_bit(&hctx->ctx_map, ctx->index_hw);
  70. }
  71. static void blk_mq_hctx_clear_pending(struct blk_mq_hw_ctx *hctx,
  72. struct blk_mq_ctx *ctx)
  73. {
  74. sbitmap_clear_bit(&hctx->ctx_map, ctx->index_hw);
  75. }
  76. struct mq_inflight {
  77. struct hd_struct *part;
  78. unsigned int *inflight;
  79. };
  80. static void blk_mq_check_inflight(struct blk_mq_hw_ctx *hctx,
  81. struct request *rq, void *priv,
  82. bool reserved)
  83. {
  84. struct mq_inflight *mi = priv;
  85. /*
  86. * index[0] counts the specific partition that was asked for. index[1]
  87. * counts the ones that are active on the whole device, so increment
  88. * that if mi->part is indeed a partition, and not a whole device.
  89. */
  90. if (rq->part == mi->part)
  91. mi->inflight[0]++;
  92. if (mi->part->partno)
  93. mi->inflight[1]++;
  94. }
  95. void blk_mq_in_flight(struct request_queue *q, struct hd_struct *part,
  96. unsigned int inflight[2])
  97. {
  98. struct mq_inflight mi = { .part = part, .inflight = inflight, };
  99. inflight[0] = inflight[1] = 0;
  100. blk_mq_queue_tag_busy_iter(q, blk_mq_check_inflight, &mi);
  101. }
  102. static void blk_mq_check_inflight_rw(struct blk_mq_hw_ctx *hctx,
  103. struct request *rq, void *priv,
  104. bool reserved)
  105. {
  106. struct mq_inflight *mi = priv;
  107. if (rq->part == mi->part)
  108. mi->inflight[rq_data_dir(rq)]++;
  109. }
  110. void blk_mq_in_flight_rw(struct request_queue *q, struct hd_struct *part,
  111. unsigned int inflight[2])
  112. {
  113. struct mq_inflight mi = { .part = part, .inflight = inflight, };
  114. inflight[0] = inflight[1] = 0;
  115. blk_mq_queue_tag_busy_iter(q, blk_mq_check_inflight_rw, &mi);
  116. }
  117. void blk_freeze_queue_start(struct request_queue *q)
  118. {
  119. int freeze_depth;
  120. freeze_depth = atomic_inc_return(&q->mq_freeze_depth);
  121. if (freeze_depth == 1) {
  122. percpu_ref_kill(&q->q_usage_counter);
  123. if (q->mq_ops)
  124. blk_mq_run_hw_queues(q, false);
  125. }
  126. }
  127. EXPORT_SYMBOL_GPL(blk_freeze_queue_start);
  128. void blk_mq_freeze_queue_wait(struct request_queue *q)
  129. {
  130. wait_event(q->mq_freeze_wq, percpu_ref_is_zero(&q->q_usage_counter));
  131. }
  132. EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_wait);
  133. int blk_mq_freeze_queue_wait_timeout(struct request_queue *q,
  134. unsigned long timeout)
  135. {
  136. return wait_event_timeout(q->mq_freeze_wq,
  137. percpu_ref_is_zero(&q->q_usage_counter),
  138. timeout);
  139. }
  140. EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_wait_timeout);
  141. /*
  142. * Guarantee no request is in use, so we can change any data structure of
  143. * the queue afterward.
  144. */
  145. void blk_freeze_queue(struct request_queue *q)
  146. {
  147. /*
  148. * In the !blk_mq case we are only calling this to kill the
  149. * q_usage_counter, otherwise this increases the freeze depth
  150. * and waits for it to return to zero. For this reason there is
  151. * no blk_unfreeze_queue(), and blk_freeze_queue() is not
  152. * exported to drivers as the only user for unfreeze is blk_mq.
  153. */
  154. blk_freeze_queue_start(q);
  155. if (!q->mq_ops)
  156. blk_drain_queue(q);
  157. blk_mq_freeze_queue_wait(q);
  158. }
  159. void blk_mq_freeze_queue(struct request_queue *q)
  160. {
  161. /*
  162. * ...just an alias to keep freeze and unfreeze actions balanced
  163. * in the blk_mq_* namespace
  164. */
  165. blk_freeze_queue(q);
  166. }
  167. EXPORT_SYMBOL_GPL(blk_mq_freeze_queue);
  168. void blk_mq_unfreeze_queue(struct request_queue *q)
  169. {
  170. int freeze_depth;
  171. freeze_depth = atomic_dec_return(&q->mq_freeze_depth);
  172. WARN_ON_ONCE(freeze_depth < 0);
  173. if (!freeze_depth) {
  174. percpu_ref_resurrect(&q->q_usage_counter);
  175. wake_up_all(&q->mq_freeze_wq);
  176. }
  177. }
  178. EXPORT_SYMBOL_GPL(blk_mq_unfreeze_queue);
  179. /*
  180. * FIXME: replace the scsi_internal_device_*block_nowait() calls in the
  181. * mpt3sas driver such that this function can be removed.
  182. */
  183. void blk_mq_quiesce_queue_nowait(struct request_queue *q)
  184. {
  185. blk_queue_flag_set(QUEUE_FLAG_QUIESCED, q);
  186. }
  187. EXPORT_SYMBOL_GPL(blk_mq_quiesce_queue_nowait);
  188. /**
  189. * blk_mq_quiesce_queue() - wait until all ongoing dispatches have finished
  190. * @q: request queue.
  191. *
  192. * Note: this function does not prevent that the struct request end_io()
  193. * callback function is invoked. Once this function is returned, we make
  194. * sure no dispatch can happen until the queue is unquiesced via
  195. * blk_mq_unquiesce_queue().
  196. */
  197. void blk_mq_quiesce_queue(struct request_queue *q)
  198. {
  199. struct blk_mq_hw_ctx *hctx;
  200. unsigned int i;
  201. bool rcu = false;
  202. blk_mq_quiesce_queue_nowait(q);
  203. queue_for_each_hw_ctx(q, hctx, i) {
  204. if (hctx->flags & BLK_MQ_F_BLOCKING)
  205. synchronize_srcu(hctx->srcu);
  206. else
  207. rcu = true;
  208. }
  209. if (rcu)
  210. synchronize_rcu();
  211. }
  212. EXPORT_SYMBOL_GPL(blk_mq_quiesce_queue);
  213. /*
  214. * blk_mq_unquiesce_queue() - counterpart of blk_mq_quiesce_queue()
  215. * @q: request queue.
  216. *
  217. * This function recovers queue into the state before quiescing
  218. * which is done by blk_mq_quiesce_queue.
  219. */
  220. void blk_mq_unquiesce_queue(struct request_queue *q)
  221. {
  222. blk_queue_flag_clear(QUEUE_FLAG_QUIESCED, q);
  223. /* dispatch requests which are inserted during quiescing */
  224. blk_mq_run_hw_queues(q, true);
  225. }
  226. EXPORT_SYMBOL_GPL(blk_mq_unquiesce_queue);
  227. void blk_mq_wake_waiters(struct request_queue *q)
  228. {
  229. struct blk_mq_hw_ctx *hctx;
  230. unsigned int i;
  231. queue_for_each_hw_ctx(q, hctx, i)
  232. if (blk_mq_hw_queue_mapped(hctx))
  233. blk_mq_tag_wakeup_all(hctx->tags, true);
  234. }
  235. bool blk_mq_can_queue(struct blk_mq_hw_ctx *hctx)
  236. {
  237. return blk_mq_has_free_tags(hctx->tags);
  238. }
  239. EXPORT_SYMBOL(blk_mq_can_queue);
  240. static struct request *blk_mq_rq_ctx_init(struct blk_mq_alloc_data *data,
  241. unsigned int tag, unsigned int op)
  242. {
  243. struct blk_mq_tags *tags = blk_mq_tags_from_data(data);
  244. struct request *rq = tags->static_rqs[tag];
  245. req_flags_t rq_flags = 0;
  246. if (data->flags & BLK_MQ_REQ_INTERNAL) {
  247. rq->tag = -1;
  248. rq->internal_tag = tag;
  249. } else {
  250. if (data->hctx->flags & BLK_MQ_F_TAG_SHARED) {
  251. rq_flags = RQF_MQ_INFLIGHT;
  252. atomic_inc(&data->hctx->nr_active);
  253. }
  254. rq->tag = tag;
  255. rq->internal_tag = -1;
  256. data->hctx->tags->rqs[rq->tag] = rq;
  257. }
  258. /* csd/requeue_work/fifo_time is initialized before use */
  259. rq->q = data->q;
  260. rq->mq_ctx = data->ctx;
  261. rq->rq_flags = rq_flags;
  262. rq->cpu = -1;
  263. rq->cmd_flags = op;
  264. if (data->flags & BLK_MQ_REQ_PREEMPT)
  265. rq->rq_flags |= RQF_PREEMPT;
  266. if (blk_queue_io_stat(data->q))
  267. rq->rq_flags |= RQF_IO_STAT;
  268. INIT_LIST_HEAD(&rq->queuelist);
  269. INIT_HLIST_NODE(&rq->hash);
  270. RB_CLEAR_NODE(&rq->rb_node);
  271. rq->rq_disk = NULL;
  272. rq->part = NULL;
  273. rq->start_time_ns = ktime_get_ns();
  274. rq->io_start_time_ns = 0;
  275. rq->nr_phys_segments = 0;
  276. #if defined(CONFIG_BLK_DEV_INTEGRITY)
  277. rq->nr_integrity_segments = 0;
  278. #endif
  279. rq->special = NULL;
  280. /* tag was already set */
  281. rq->extra_len = 0;
  282. rq->__deadline = 0;
  283. INIT_LIST_HEAD(&rq->timeout_list);
  284. rq->timeout = 0;
  285. rq->end_io = NULL;
  286. rq->end_io_data = NULL;
  287. rq->next_rq = NULL;
  288. #ifdef CONFIG_BLK_CGROUP
  289. rq->rl = NULL;
  290. #endif
  291. data->ctx->rq_dispatched[op_is_sync(op)]++;
  292. refcount_set(&rq->ref, 1);
  293. return rq;
  294. }
  295. static struct request *blk_mq_get_request(struct request_queue *q,
  296. struct bio *bio, unsigned int op,
  297. struct blk_mq_alloc_data *data)
  298. {
  299. struct elevator_queue *e = q->elevator;
  300. struct request *rq;
  301. unsigned int tag;
  302. bool put_ctx_on_error = false;
  303. blk_queue_enter_live(q);
  304. data->q = q;
  305. if (likely(!data->ctx)) {
  306. data->ctx = blk_mq_get_ctx(q);
  307. put_ctx_on_error = true;
  308. }
  309. if (likely(!data->hctx))
  310. data->hctx = blk_mq_map_queue(q, data->ctx->cpu);
  311. if (op & REQ_NOWAIT)
  312. data->flags |= BLK_MQ_REQ_NOWAIT;
  313. if (e) {
  314. data->flags |= BLK_MQ_REQ_INTERNAL;
  315. /*
  316. * Flush requests are special and go directly to the
  317. * dispatch list. Don't include reserved tags in the
  318. * limiting, as it isn't useful.
  319. */
  320. if (!op_is_flush(op) && e->type->ops.mq.limit_depth &&
  321. !(data->flags & BLK_MQ_REQ_RESERVED))
  322. e->type->ops.mq.limit_depth(op, data);
  323. } else {
  324. blk_mq_tag_busy(data->hctx);
  325. }
  326. tag = blk_mq_get_tag(data);
  327. if (tag == BLK_MQ_TAG_FAIL) {
  328. if (put_ctx_on_error) {
  329. blk_mq_put_ctx(data->ctx);
  330. data->ctx = NULL;
  331. }
  332. blk_queue_exit(q);
  333. return NULL;
  334. }
  335. rq = blk_mq_rq_ctx_init(data, tag, op);
  336. if (!op_is_flush(op)) {
  337. rq->elv.icq = NULL;
  338. if (e && e->type->ops.mq.prepare_request) {
  339. if (e->type->icq_cache && rq_ioc(bio))
  340. blk_mq_sched_assign_ioc(rq, bio);
  341. e->type->ops.mq.prepare_request(rq, bio);
  342. rq->rq_flags |= RQF_ELVPRIV;
  343. }
  344. }
  345. data->hctx->queued++;
  346. return rq;
  347. }
  348. struct request *blk_mq_alloc_request(struct request_queue *q, unsigned int op,
  349. blk_mq_req_flags_t flags)
  350. {
  351. struct blk_mq_alloc_data alloc_data = { .flags = flags };
  352. struct request *rq;
  353. int ret;
  354. ret = blk_queue_enter(q, flags);
  355. if (ret)
  356. return ERR_PTR(ret);
  357. rq = blk_mq_get_request(q, NULL, op, &alloc_data);
  358. blk_queue_exit(q);
  359. if (!rq)
  360. return ERR_PTR(-EWOULDBLOCK);
  361. blk_mq_put_ctx(alloc_data.ctx);
  362. rq->__data_len = 0;
  363. rq->__sector = (sector_t) -1;
  364. rq->bio = rq->biotail = NULL;
  365. return rq;
  366. }
  367. EXPORT_SYMBOL(blk_mq_alloc_request);
  368. struct request *blk_mq_alloc_request_hctx(struct request_queue *q,
  369. unsigned int op, blk_mq_req_flags_t flags, unsigned int hctx_idx)
  370. {
  371. struct blk_mq_alloc_data alloc_data = { .flags = flags };
  372. struct request *rq;
  373. unsigned int cpu;
  374. int ret;
  375. /*
  376. * If the tag allocator sleeps we could get an allocation for a
  377. * different hardware context. No need to complicate the low level
  378. * allocator for this for the rare use case of a command tied to
  379. * a specific queue.
  380. */
  381. if (WARN_ON_ONCE(!(flags & BLK_MQ_REQ_NOWAIT)))
  382. return ERR_PTR(-EINVAL);
  383. if (hctx_idx >= q->nr_hw_queues)
  384. return ERR_PTR(-EIO);
  385. ret = blk_queue_enter(q, flags);
  386. if (ret)
  387. return ERR_PTR(ret);
  388. /*
  389. * Check if the hardware context is actually mapped to anything.
  390. * If not tell the caller that it should skip this queue.
  391. */
  392. alloc_data.hctx = q->queue_hw_ctx[hctx_idx];
  393. if (!blk_mq_hw_queue_mapped(alloc_data.hctx)) {
  394. blk_queue_exit(q);
  395. return ERR_PTR(-EXDEV);
  396. }
  397. cpu = cpumask_first_and(alloc_data.hctx->cpumask, cpu_online_mask);
  398. alloc_data.ctx = __blk_mq_get_ctx(q, cpu);
  399. rq = blk_mq_get_request(q, NULL, op, &alloc_data);
  400. blk_queue_exit(q);
  401. if (!rq)
  402. return ERR_PTR(-EWOULDBLOCK);
  403. return rq;
  404. }
  405. EXPORT_SYMBOL_GPL(blk_mq_alloc_request_hctx);
  406. static void __blk_mq_free_request(struct request *rq)
  407. {
  408. struct request_queue *q = rq->q;
  409. struct blk_mq_ctx *ctx = rq->mq_ctx;
  410. struct blk_mq_hw_ctx *hctx = blk_mq_map_queue(q, ctx->cpu);
  411. const int sched_tag = rq->internal_tag;
  412. blk_pm_mark_last_busy(rq);
  413. if (rq->tag != -1)
  414. blk_mq_put_tag(hctx, hctx->tags, ctx, rq->tag);
  415. if (sched_tag != -1)
  416. blk_mq_put_tag(hctx, hctx->sched_tags, ctx, sched_tag);
  417. blk_mq_sched_restart(hctx);
  418. blk_queue_exit(q);
  419. }
  420. void blk_mq_free_request(struct request *rq)
  421. {
  422. struct request_queue *q = rq->q;
  423. struct elevator_queue *e = q->elevator;
  424. struct blk_mq_ctx *ctx = rq->mq_ctx;
  425. struct blk_mq_hw_ctx *hctx = blk_mq_map_queue(q, ctx->cpu);
  426. if (rq->rq_flags & RQF_ELVPRIV) {
  427. if (e && e->type->ops.mq.finish_request)
  428. e->type->ops.mq.finish_request(rq);
  429. if (rq->elv.icq) {
  430. put_io_context(rq->elv.icq->ioc);
  431. rq->elv.icq = NULL;
  432. }
  433. }
  434. ctx->rq_completed[rq_is_sync(rq)]++;
  435. if (rq->rq_flags & RQF_MQ_INFLIGHT)
  436. atomic_dec(&hctx->nr_active);
  437. if (unlikely(laptop_mode && !blk_rq_is_passthrough(rq)))
  438. laptop_io_completion(q->backing_dev_info);
  439. rq_qos_done(q, rq);
  440. if (blk_rq_rl(rq))
  441. blk_put_rl(blk_rq_rl(rq));
  442. WRITE_ONCE(rq->state, MQ_RQ_IDLE);
  443. if (refcount_dec_and_test(&rq->ref))
  444. __blk_mq_free_request(rq);
  445. }
  446. EXPORT_SYMBOL_GPL(blk_mq_free_request);
  447. inline void __blk_mq_end_request(struct request *rq, blk_status_t error)
  448. {
  449. u64 now = ktime_get_ns();
  450. if (rq->rq_flags & RQF_STATS) {
  451. blk_mq_poll_stats_start(rq->q);
  452. blk_stat_add(rq, now);
  453. }
  454. if (rq->internal_tag != -1)
  455. blk_mq_sched_completed_request(rq, now);
  456. blk_account_io_done(rq, now);
  457. if (rq->end_io) {
  458. rq_qos_done(rq->q, rq);
  459. rq->end_io(rq, error);
  460. } else {
  461. if (unlikely(blk_bidi_rq(rq)))
  462. blk_mq_free_request(rq->next_rq);
  463. blk_mq_free_request(rq);
  464. }
  465. }
  466. EXPORT_SYMBOL(__blk_mq_end_request);
  467. void blk_mq_end_request(struct request *rq, blk_status_t error)
  468. {
  469. if (blk_update_request(rq, error, blk_rq_bytes(rq)))
  470. BUG();
  471. __blk_mq_end_request(rq, error);
  472. }
  473. EXPORT_SYMBOL(blk_mq_end_request);
  474. static void __blk_mq_complete_request_remote(void *data)
  475. {
  476. struct request *rq = data;
  477. rq->q->softirq_done_fn(rq);
  478. }
  479. static void __blk_mq_complete_request(struct request *rq)
  480. {
  481. struct blk_mq_ctx *ctx = rq->mq_ctx;
  482. bool shared = false;
  483. int cpu;
  484. if (!blk_mq_mark_complete(rq))
  485. return;
  486. /*
  487. * Most of single queue controllers, there is only one irq vector
  488. * for handling IO completion, and the only irq's affinity is set
  489. * as all possible CPUs. On most of ARCHs, this affinity means the
  490. * irq is handled on one specific CPU.
  491. *
  492. * So complete IO reqeust in softirq context in case of single queue
  493. * for not degrading IO performance by irqsoff latency.
  494. */
  495. if (rq->q->nr_hw_queues == 1) {
  496. __blk_complete_request(rq);
  497. return;
  498. }
  499. if (!test_bit(QUEUE_FLAG_SAME_COMP, &rq->q->queue_flags)) {
  500. rq->q->softirq_done_fn(rq);
  501. return;
  502. }
  503. cpu = get_cpu();
  504. if (!test_bit(QUEUE_FLAG_SAME_FORCE, &rq->q->queue_flags))
  505. shared = cpus_share_cache(cpu, ctx->cpu);
  506. if (cpu != ctx->cpu && !shared && cpu_online(ctx->cpu)) {
  507. rq->csd.func = __blk_mq_complete_request_remote;
  508. rq->csd.info = rq;
  509. rq->csd.flags = 0;
  510. smp_call_function_single_async(ctx->cpu, &rq->csd);
  511. } else {
  512. rq->q->softirq_done_fn(rq);
  513. }
  514. put_cpu();
  515. }
  516. static void hctx_unlock(struct blk_mq_hw_ctx *hctx, int srcu_idx)
  517. __releases(hctx->srcu)
  518. {
  519. if (!(hctx->flags & BLK_MQ_F_BLOCKING))
  520. rcu_read_unlock();
  521. else
  522. srcu_read_unlock(hctx->srcu, srcu_idx);
  523. }
  524. static void hctx_lock(struct blk_mq_hw_ctx *hctx, int *srcu_idx)
  525. __acquires(hctx->srcu)
  526. {
  527. if (!(hctx->flags & BLK_MQ_F_BLOCKING)) {
  528. /* shut up gcc false positive */
  529. *srcu_idx = 0;
  530. rcu_read_lock();
  531. } else
  532. *srcu_idx = srcu_read_lock(hctx->srcu);
  533. }
  534. /**
  535. * blk_mq_complete_request - end I/O on a request
  536. * @rq: the request being processed
  537. *
  538. * Description:
  539. * Ends all I/O on a request. It does not handle partial completions.
  540. * The actual completion happens out-of-order, through a IPI handler.
  541. **/
  542. void blk_mq_complete_request(struct request *rq)
  543. {
  544. if (unlikely(blk_should_fake_timeout(rq->q)))
  545. return;
  546. __blk_mq_complete_request(rq);
  547. }
  548. EXPORT_SYMBOL(blk_mq_complete_request);
  549. int blk_mq_request_started(struct request *rq)
  550. {
  551. return blk_mq_rq_state(rq) != MQ_RQ_IDLE;
  552. }
  553. EXPORT_SYMBOL_GPL(blk_mq_request_started);
  554. void blk_mq_start_request(struct request *rq)
  555. {
  556. struct request_queue *q = rq->q;
  557. blk_mq_sched_started_request(rq);
  558. trace_block_rq_issue(q, rq);
  559. if (test_bit(QUEUE_FLAG_STATS, &q->queue_flags)) {
  560. rq->io_start_time_ns = ktime_get_ns();
  561. #ifdef CONFIG_BLK_DEV_THROTTLING_LOW
  562. rq->throtl_size = blk_rq_sectors(rq);
  563. #endif
  564. rq->rq_flags |= RQF_STATS;
  565. rq_qos_issue(q, rq);
  566. }
  567. WARN_ON_ONCE(blk_mq_rq_state(rq) != MQ_RQ_IDLE);
  568. blk_add_timer(rq);
  569. WRITE_ONCE(rq->state, MQ_RQ_IN_FLIGHT);
  570. if (q->dma_drain_size && blk_rq_bytes(rq)) {
  571. /*
  572. * Make sure space for the drain appears. We know we can do
  573. * this because max_hw_segments has been adjusted to be one
  574. * fewer than the device can handle.
  575. */
  576. rq->nr_phys_segments++;
  577. }
  578. }
  579. EXPORT_SYMBOL(blk_mq_start_request);
  580. static void __blk_mq_requeue_request(struct request *rq)
  581. {
  582. struct request_queue *q = rq->q;
  583. blk_mq_put_driver_tag(rq);
  584. trace_block_rq_requeue(q, rq);
  585. rq_qos_requeue(q, rq);
  586. if (blk_mq_request_started(rq)) {
  587. WRITE_ONCE(rq->state, MQ_RQ_IDLE);
  588. rq->rq_flags &= ~RQF_TIMED_OUT;
  589. if (q->dma_drain_size && blk_rq_bytes(rq))
  590. rq->nr_phys_segments--;
  591. }
  592. }
  593. void blk_mq_requeue_request(struct request *rq, bool kick_requeue_list)
  594. {
  595. __blk_mq_requeue_request(rq);
  596. /* this request will be re-inserted to io scheduler queue */
  597. blk_mq_sched_requeue_request(rq);
  598. BUG_ON(blk_queued_rq(rq));
  599. blk_mq_add_to_requeue_list(rq, true, kick_requeue_list);
  600. }
  601. EXPORT_SYMBOL(blk_mq_requeue_request);
  602. static void blk_mq_requeue_work(struct work_struct *work)
  603. {
  604. struct request_queue *q =
  605. container_of(work, struct request_queue, requeue_work.work);
  606. LIST_HEAD(rq_list);
  607. struct request *rq, *next;
  608. spin_lock_irq(&q->requeue_lock);
  609. list_splice_init(&q->requeue_list, &rq_list);
  610. spin_unlock_irq(&q->requeue_lock);
  611. list_for_each_entry_safe(rq, next, &rq_list, queuelist) {
  612. if (!(rq->rq_flags & RQF_SOFTBARRIER))
  613. continue;
  614. rq->rq_flags &= ~RQF_SOFTBARRIER;
  615. list_del_init(&rq->queuelist);
  616. blk_mq_sched_insert_request(rq, true, false, false);
  617. }
  618. while (!list_empty(&rq_list)) {
  619. rq = list_entry(rq_list.next, struct request, queuelist);
  620. list_del_init(&rq->queuelist);
  621. blk_mq_sched_insert_request(rq, false, false, false);
  622. }
  623. blk_mq_run_hw_queues(q, false);
  624. }
  625. void blk_mq_add_to_requeue_list(struct request *rq, bool at_head,
  626. bool kick_requeue_list)
  627. {
  628. struct request_queue *q = rq->q;
  629. unsigned long flags;
  630. /*
  631. * We abuse this flag that is otherwise used by the I/O scheduler to
  632. * request head insertion from the workqueue.
  633. */
  634. BUG_ON(rq->rq_flags & RQF_SOFTBARRIER);
  635. spin_lock_irqsave(&q->requeue_lock, flags);
  636. if (at_head) {
  637. rq->rq_flags |= RQF_SOFTBARRIER;
  638. list_add(&rq->queuelist, &q->requeue_list);
  639. } else {
  640. list_add_tail(&rq->queuelist, &q->requeue_list);
  641. }
  642. spin_unlock_irqrestore(&q->requeue_lock, flags);
  643. if (kick_requeue_list)
  644. blk_mq_kick_requeue_list(q);
  645. }
  646. EXPORT_SYMBOL(blk_mq_add_to_requeue_list);
  647. void blk_mq_kick_requeue_list(struct request_queue *q)
  648. {
  649. kblockd_mod_delayed_work_on(WORK_CPU_UNBOUND, &q->requeue_work, 0);
  650. }
  651. EXPORT_SYMBOL(blk_mq_kick_requeue_list);
  652. void blk_mq_delay_kick_requeue_list(struct request_queue *q,
  653. unsigned long msecs)
  654. {
  655. kblockd_mod_delayed_work_on(WORK_CPU_UNBOUND, &q->requeue_work,
  656. msecs_to_jiffies(msecs));
  657. }
  658. EXPORT_SYMBOL(blk_mq_delay_kick_requeue_list);
  659. struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags, unsigned int tag)
  660. {
  661. if (tag < tags->nr_tags) {
  662. prefetch(tags->rqs[tag]);
  663. return tags->rqs[tag];
  664. }
  665. return NULL;
  666. }
  667. EXPORT_SYMBOL(blk_mq_tag_to_rq);
  668. static void blk_mq_rq_timed_out(struct request *req, bool reserved)
  669. {
  670. req->rq_flags |= RQF_TIMED_OUT;
  671. if (req->q->mq_ops->timeout) {
  672. enum blk_eh_timer_return ret;
  673. ret = req->q->mq_ops->timeout(req, reserved);
  674. if (ret == BLK_EH_DONE)
  675. return;
  676. WARN_ON_ONCE(ret != BLK_EH_RESET_TIMER);
  677. }
  678. blk_add_timer(req);
  679. }
  680. static bool blk_mq_req_expired(struct request *rq, unsigned long *next)
  681. {
  682. unsigned long deadline;
  683. if (blk_mq_rq_state(rq) != MQ_RQ_IN_FLIGHT)
  684. return false;
  685. if (rq->rq_flags & RQF_TIMED_OUT)
  686. return false;
  687. deadline = blk_rq_deadline(rq);
  688. if (time_after_eq(jiffies, deadline))
  689. return true;
  690. if (*next == 0)
  691. *next = deadline;
  692. else if (time_after(*next, deadline))
  693. *next = deadline;
  694. return false;
  695. }
  696. static void blk_mq_check_expired(struct blk_mq_hw_ctx *hctx,
  697. struct request *rq, void *priv, bool reserved)
  698. {
  699. unsigned long *next = priv;
  700. /*
  701. * Just do a quick check if it is expired before locking the request in
  702. * so we're not unnecessarilly synchronizing across CPUs.
  703. */
  704. if (!blk_mq_req_expired(rq, next))
  705. return;
  706. /*
  707. * We have reason to believe the request may be expired. Take a
  708. * reference on the request to lock this request lifetime into its
  709. * currently allocated context to prevent it from being reallocated in
  710. * the event the completion by-passes this timeout handler.
  711. *
  712. * If the reference was already released, then the driver beat the
  713. * timeout handler to posting a natural completion.
  714. */
  715. if (!refcount_inc_not_zero(&rq->ref))
  716. return;
  717. /*
  718. * The request is now locked and cannot be reallocated underneath the
  719. * timeout handler's processing. Re-verify this exact request is truly
  720. * expired; if it is not expired, then the request was completed and
  721. * reallocated as a new request.
  722. */
  723. if (blk_mq_req_expired(rq, next))
  724. blk_mq_rq_timed_out(rq, reserved);
  725. if (refcount_dec_and_test(&rq->ref))
  726. __blk_mq_free_request(rq);
  727. }
  728. static void blk_mq_timeout_work(struct work_struct *work)
  729. {
  730. struct request_queue *q =
  731. container_of(work, struct request_queue, timeout_work);
  732. unsigned long next = 0;
  733. struct blk_mq_hw_ctx *hctx;
  734. int i;
  735. /* A deadlock might occur if a request is stuck requiring a
  736. * timeout at the same time a queue freeze is waiting
  737. * completion, since the timeout code would not be able to
  738. * acquire the queue reference here.
  739. *
  740. * That's why we don't use blk_queue_enter here; instead, we use
  741. * percpu_ref_tryget directly, because we need to be able to
  742. * obtain a reference even in the short window between the queue
  743. * starting to freeze, by dropping the first reference in
  744. * blk_freeze_queue_start, and the moment the last request is
  745. * consumed, marked by the instant q_usage_counter reaches
  746. * zero.
  747. */
  748. if (!percpu_ref_tryget(&q->q_usage_counter))
  749. return;
  750. blk_mq_queue_tag_busy_iter(q, blk_mq_check_expired, &next);
  751. if (next != 0) {
  752. mod_timer(&q->timeout, next);
  753. } else {
  754. /*
  755. * Request timeouts are handled as a forward rolling timer. If
  756. * we end up here it means that no requests are pending and
  757. * also that no request has been pending for a while. Mark
  758. * each hctx as idle.
  759. */
  760. queue_for_each_hw_ctx(q, hctx, i) {
  761. /* the hctx may be unmapped, so check it here */
  762. if (blk_mq_hw_queue_mapped(hctx))
  763. blk_mq_tag_idle(hctx);
  764. }
  765. }
  766. blk_queue_exit(q);
  767. }
  768. struct flush_busy_ctx_data {
  769. struct blk_mq_hw_ctx *hctx;
  770. struct list_head *list;
  771. };
  772. static bool flush_busy_ctx(struct sbitmap *sb, unsigned int bitnr, void *data)
  773. {
  774. struct flush_busy_ctx_data *flush_data = data;
  775. struct blk_mq_hw_ctx *hctx = flush_data->hctx;
  776. struct blk_mq_ctx *ctx = hctx->ctxs[bitnr];
  777. spin_lock(&ctx->lock);
  778. list_splice_tail_init(&ctx->rq_list, flush_data->list);
  779. sbitmap_clear_bit(sb, bitnr);
  780. spin_unlock(&ctx->lock);
  781. return true;
  782. }
  783. /*
  784. * Process software queues that have been marked busy, splicing them
  785. * to the for-dispatch
  786. */
  787. void blk_mq_flush_busy_ctxs(struct blk_mq_hw_ctx *hctx, struct list_head *list)
  788. {
  789. struct flush_busy_ctx_data data = {
  790. .hctx = hctx,
  791. .list = list,
  792. };
  793. sbitmap_for_each_set(&hctx->ctx_map, flush_busy_ctx, &data);
  794. }
  795. EXPORT_SYMBOL_GPL(blk_mq_flush_busy_ctxs);
  796. struct dispatch_rq_data {
  797. struct blk_mq_hw_ctx *hctx;
  798. struct request *rq;
  799. };
  800. static bool dispatch_rq_from_ctx(struct sbitmap *sb, unsigned int bitnr,
  801. void *data)
  802. {
  803. struct dispatch_rq_data *dispatch_data = data;
  804. struct blk_mq_hw_ctx *hctx = dispatch_data->hctx;
  805. struct blk_mq_ctx *ctx = hctx->ctxs[bitnr];
  806. spin_lock(&ctx->lock);
  807. if (!list_empty(&ctx->rq_list)) {
  808. dispatch_data->rq = list_entry_rq(ctx->rq_list.next);
  809. list_del_init(&dispatch_data->rq->queuelist);
  810. if (list_empty(&ctx->rq_list))
  811. sbitmap_clear_bit(sb, bitnr);
  812. }
  813. spin_unlock(&ctx->lock);
  814. return !dispatch_data->rq;
  815. }
  816. struct request *blk_mq_dequeue_from_ctx(struct blk_mq_hw_ctx *hctx,
  817. struct blk_mq_ctx *start)
  818. {
  819. unsigned off = start ? start->index_hw : 0;
  820. struct dispatch_rq_data data = {
  821. .hctx = hctx,
  822. .rq = NULL,
  823. };
  824. __sbitmap_for_each_set(&hctx->ctx_map, off,
  825. dispatch_rq_from_ctx, &data);
  826. return data.rq;
  827. }
  828. static inline unsigned int queued_to_index(unsigned int queued)
  829. {
  830. if (!queued)
  831. return 0;
  832. return min(BLK_MQ_MAX_DISPATCH_ORDER - 1, ilog2(queued) + 1);
  833. }
  834. bool blk_mq_get_driver_tag(struct request *rq)
  835. {
  836. struct blk_mq_alloc_data data = {
  837. .q = rq->q,
  838. .hctx = blk_mq_map_queue(rq->q, rq->mq_ctx->cpu),
  839. .flags = BLK_MQ_REQ_NOWAIT,
  840. };
  841. bool shared;
  842. if (rq->tag != -1)
  843. goto done;
  844. if (blk_mq_tag_is_reserved(data.hctx->sched_tags, rq->internal_tag))
  845. data.flags |= BLK_MQ_REQ_RESERVED;
  846. shared = blk_mq_tag_busy(data.hctx);
  847. rq->tag = blk_mq_get_tag(&data);
  848. if (rq->tag >= 0) {
  849. if (shared) {
  850. rq->rq_flags |= RQF_MQ_INFLIGHT;
  851. atomic_inc(&data.hctx->nr_active);
  852. }
  853. data.hctx->tags->rqs[rq->tag] = rq;
  854. }
  855. done:
  856. return rq->tag != -1;
  857. }
  858. static int blk_mq_dispatch_wake(wait_queue_entry_t *wait, unsigned mode,
  859. int flags, void *key)
  860. {
  861. struct blk_mq_hw_ctx *hctx;
  862. hctx = container_of(wait, struct blk_mq_hw_ctx, dispatch_wait);
  863. spin_lock(&hctx->dispatch_wait_lock);
  864. list_del_init(&wait->entry);
  865. spin_unlock(&hctx->dispatch_wait_lock);
  866. blk_mq_run_hw_queue(hctx, true);
  867. return 1;
  868. }
  869. /*
  870. * Mark us waiting for a tag. For shared tags, this involves hooking us into
  871. * the tag wakeups. For non-shared tags, we can simply mark us needing a
  872. * restart. For both cases, take care to check the condition again after
  873. * marking us as waiting.
  874. */
  875. static bool blk_mq_mark_tag_wait(struct blk_mq_hw_ctx *hctx,
  876. struct request *rq)
  877. {
  878. struct wait_queue_head *wq;
  879. wait_queue_entry_t *wait;
  880. bool ret;
  881. if (!(hctx->flags & BLK_MQ_F_TAG_SHARED)) {
  882. if (!test_bit(BLK_MQ_S_SCHED_RESTART, &hctx->state))
  883. set_bit(BLK_MQ_S_SCHED_RESTART, &hctx->state);
  884. /*
  885. * It's possible that a tag was freed in the window between the
  886. * allocation failure and adding the hardware queue to the wait
  887. * queue.
  888. *
  889. * Don't clear RESTART here, someone else could have set it.
  890. * At most this will cost an extra queue run.
  891. */
  892. return blk_mq_get_driver_tag(rq);
  893. }
  894. wait = &hctx->dispatch_wait;
  895. if (!list_empty_careful(&wait->entry))
  896. return false;
  897. wq = &bt_wait_ptr(&hctx->tags->bitmap_tags, hctx)->wait;
  898. spin_lock_irq(&wq->lock);
  899. spin_lock(&hctx->dispatch_wait_lock);
  900. if (!list_empty(&wait->entry)) {
  901. spin_unlock(&hctx->dispatch_wait_lock);
  902. spin_unlock_irq(&wq->lock);
  903. return false;
  904. }
  905. wait->flags &= ~WQ_FLAG_EXCLUSIVE;
  906. __add_wait_queue(wq, wait);
  907. /*
  908. * It's possible that a tag was freed in the window between the
  909. * allocation failure and adding the hardware queue to the wait
  910. * queue.
  911. */
  912. ret = blk_mq_get_driver_tag(rq);
  913. if (!ret) {
  914. spin_unlock(&hctx->dispatch_wait_lock);
  915. spin_unlock_irq(&wq->lock);
  916. return false;
  917. }
  918. /*
  919. * We got a tag, remove ourselves from the wait queue to ensure
  920. * someone else gets the wakeup.
  921. */
  922. list_del_init(&wait->entry);
  923. spin_unlock(&hctx->dispatch_wait_lock);
  924. spin_unlock_irq(&wq->lock);
  925. return true;
  926. }
  927. #define BLK_MQ_DISPATCH_BUSY_EWMA_WEIGHT 8
  928. #define BLK_MQ_DISPATCH_BUSY_EWMA_FACTOR 4
  929. /*
  930. * Update dispatch busy with the Exponential Weighted Moving Average(EWMA):
  931. * - EWMA is one simple way to compute running average value
  932. * - weight(7/8 and 1/8) is applied so that it can decrease exponentially
  933. * - take 4 as factor for avoiding to get too small(0) result, and this
  934. * factor doesn't matter because EWMA decreases exponentially
  935. */
  936. static void blk_mq_update_dispatch_busy(struct blk_mq_hw_ctx *hctx, bool busy)
  937. {
  938. unsigned int ewma;
  939. if (hctx->queue->elevator)
  940. return;
  941. ewma = hctx->dispatch_busy;
  942. if (!ewma && !busy)
  943. return;
  944. ewma *= BLK_MQ_DISPATCH_BUSY_EWMA_WEIGHT - 1;
  945. if (busy)
  946. ewma += 1 << BLK_MQ_DISPATCH_BUSY_EWMA_FACTOR;
  947. ewma /= BLK_MQ_DISPATCH_BUSY_EWMA_WEIGHT;
  948. hctx->dispatch_busy = ewma;
  949. }
  950. #define BLK_MQ_RESOURCE_DELAY 3 /* ms units */
  951. /*
  952. * Returns true if we did some work AND can potentially do more.
  953. */
  954. bool blk_mq_dispatch_rq_list(struct request_queue *q, struct list_head *list,
  955. bool got_budget)
  956. {
  957. struct blk_mq_hw_ctx *hctx;
  958. struct request *rq, *nxt;
  959. bool no_tag = false;
  960. int errors, queued;
  961. blk_status_t ret = BLK_STS_OK;
  962. if (list_empty(list))
  963. return false;
  964. WARN_ON(!list_is_singular(list) && got_budget);
  965. /*
  966. * Now process all the entries, sending them to the driver.
  967. */
  968. errors = queued = 0;
  969. do {
  970. struct blk_mq_queue_data bd;
  971. rq = list_first_entry(list, struct request, queuelist);
  972. hctx = blk_mq_map_queue(rq->q, rq->mq_ctx->cpu);
  973. if (!got_budget && !blk_mq_get_dispatch_budget(hctx))
  974. break;
  975. if (!blk_mq_get_driver_tag(rq)) {
  976. /*
  977. * The initial allocation attempt failed, so we need to
  978. * rerun the hardware queue when a tag is freed. The
  979. * waitqueue takes care of that. If the queue is run
  980. * before we add this entry back on the dispatch list,
  981. * we'll re-run it below.
  982. */
  983. if (!blk_mq_mark_tag_wait(hctx, rq)) {
  984. blk_mq_put_dispatch_budget(hctx);
  985. /*
  986. * For non-shared tags, the RESTART check
  987. * will suffice.
  988. */
  989. if (hctx->flags & BLK_MQ_F_TAG_SHARED)
  990. no_tag = true;
  991. break;
  992. }
  993. }
  994. list_del_init(&rq->queuelist);
  995. bd.rq = rq;
  996. /*
  997. * Flag last if we have no more requests, or if we have more
  998. * but can't assign a driver tag to it.
  999. */
  1000. if (list_empty(list))
  1001. bd.last = true;
  1002. else {
  1003. nxt = list_first_entry(list, struct request, queuelist);
  1004. bd.last = !blk_mq_get_driver_tag(nxt);
  1005. }
  1006. ret = q->mq_ops->queue_rq(hctx, &bd);
  1007. if (ret == BLK_STS_RESOURCE || ret == BLK_STS_DEV_RESOURCE) {
  1008. /*
  1009. * If an I/O scheduler has been configured and we got a
  1010. * driver tag for the next request already, free it
  1011. * again.
  1012. */
  1013. if (!list_empty(list)) {
  1014. nxt = list_first_entry(list, struct request, queuelist);
  1015. blk_mq_put_driver_tag(nxt);
  1016. }
  1017. list_add(&rq->queuelist, list);
  1018. __blk_mq_requeue_request(rq);
  1019. break;
  1020. }
  1021. if (unlikely(ret != BLK_STS_OK)) {
  1022. errors++;
  1023. blk_mq_end_request(rq, BLK_STS_IOERR);
  1024. continue;
  1025. }
  1026. queued++;
  1027. } while (!list_empty(list));
  1028. hctx->dispatched[queued_to_index(queued)]++;
  1029. /*
  1030. * Any items that need requeuing? Stuff them into hctx->dispatch,
  1031. * that is where we will continue on next queue run.
  1032. */
  1033. if (!list_empty(list)) {
  1034. bool needs_restart;
  1035. spin_lock(&hctx->lock);
  1036. list_splice_init(list, &hctx->dispatch);
  1037. spin_unlock(&hctx->lock);
  1038. /*
  1039. * If SCHED_RESTART was set by the caller of this function and
  1040. * it is no longer set that means that it was cleared by another
  1041. * thread and hence that a queue rerun is needed.
  1042. *
  1043. * If 'no_tag' is set, that means that we failed getting
  1044. * a driver tag with an I/O scheduler attached. If our dispatch
  1045. * waitqueue is no longer active, ensure that we run the queue
  1046. * AFTER adding our entries back to the list.
  1047. *
  1048. * If no I/O scheduler has been configured it is possible that
  1049. * the hardware queue got stopped and restarted before requests
  1050. * were pushed back onto the dispatch list. Rerun the queue to
  1051. * avoid starvation. Notes:
  1052. * - blk_mq_run_hw_queue() checks whether or not a queue has
  1053. * been stopped before rerunning a queue.
  1054. * - Some but not all block drivers stop a queue before
  1055. * returning BLK_STS_RESOURCE. Two exceptions are scsi-mq
  1056. * and dm-rq.
  1057. *
  1058. * If driver returns BLK_STS_RESOURCE and SCHED_RESTART
  1059. * bit is set, run queue after a delay to avoid IO stalls
  1060. * that could otherwise occur if the queue is idle.
  1061. */
  1062. needs_restart = blk_mq_sched_needs_restart(hctx);
  1063. if (!needs_restart ||
  1064. (no_tag && list_empty_careful(&hctx->dispatch_wait.entry)))
  1065. blk_mq_run_hw_queue(hctx, true);
  1066. else if (needs_restart && (ret == BLK_STS_RESOURCE))
  1067. blk_mq_delay_run_hw_queue(hctx, BLK_MQ_RESOURCE_DELAY);
  1068. blk_mq_update_dispatch_busy(hctx, true);
  1069. return false;
  1070. } else
  1071. blk_mq_update_dispatch_busy(hctx, false);
  1072. /*
  1073. * If the host/device is unable to accept more work, inform the
  1074. * caller of that.
  1075. */
  1076. if (ret == BLK_STS_RESOURCE || ret == BLK_STS_DEV_RESOURCE)
  1077. return false;
  1078. return (queued + errors) != 0;
  1079. }
  1080. static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx)
  1081. {
  1082. int srcu_idx;
  1083. /*
  1084. * We should be running this queue from one of the CPUs that
  1085. * are mapped to it.
  1086. *
  1087. * There are at least two related races now between setting
  1088. * hctx->next_cpu from blk_mq_hctx_next_cpu() and running
  1089. * __blk_mq_run_hw_queue():
  1090. *
  1091. * - hctx->next_cpu is found offline in blk_mq_hctx_next_cpu(),
  1092. * but later it becomes online, then this warning is harmless
  1093. * at all
  1094. *
  1095. * - hctx->next_cpu is found online in blk_mq_hctx_next_cpu(),
  1096. * but later it becomes offline, then the warning can't be
  1097. * triggered, and we depend on blk-mq timeout handler to
  1098. * handle dispatched requests to this hctx
  1099. */
  1100. if (!cpumask_test_cpu(raw_smp_processor_id(), hctx->cpumask) &&
  1101. cpu_online(hctx->next_cpu)) {
  1102. printk(KERN_WARNING "run queue from wrong CPU %d, hctx %s\n",
  1103. raw_smp_processor_id(),
  1104. cpumask_empty(hctx->cpumask) ? "inactive": "active");
  1105. dump_stack();
  1106. }
  1107. /*
  1108. * We can't run the queue inline with ints disabled. Ensure that
  1109. * we catch bad users of this early.
  1110. */
  1111. WARN_ON_ONCE(in_interrupt());
  1112. might_sleep_if(hctx->flags & BLK_MQ_F_BLOCKING);
  1113. hctx_lock(hctx, &srcu_idx);
  1114. blk_mq_sched_dispatch_requests(hctx);
  1115. hctx_unlock(hctx, srcu_idx);
  1116. }
  1117. static inline int blk_mq_first_mapped_cpu(struct blk_mq_hw_ctx *hctx)
  1118. {
  1119. int cpu = cpumask_first_and(hctx->cpumask, cpu_online_mask);
  1120. if (cpu >= nr_cpu_ids)
  1121. cpu = cpumask_first(hctx->cpumask);
  1122. return cpu;
  1123. }
  1124. /*
  1125. * It'd be great if the workqueue API had a way to pass
  1126. * in a mask and had some smarts for more clever placement.
  1127. * For now we just round-robin here, switching for every
  1128. * BLK_MQ_CPU_WORK_BATCH queued items.
  1129. */
  1130. static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx)
  1131. {
  1132. bool tried = false;
  1133. int next_cpu = hctx->next_cpu;
  1134. if (hctx->queue->nr_hw_queues == 1)
  1135. return WORK_CPU_UNBOUND;
  1136. if (--hctx->next_cpu_batch <= 0) {
  1137. select_cpu:
  1138. next_cpu = cpumask_next_and(next_cpu, hctx->cpumask,
  1139. cpu_online_mask);
  1140. if (next_cpu >= nr_cpu_ids)
  1141. next_cpu = blk_mq_first_mapped_cpu(hctx);
  1142. hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
  1143. }
  1144. /*
  1145. * Do unbound schedule if we can't find a online CPU for this hctx,
  1146. * and it should only happen in the path of handling CPU DEAD.
  1147. */
  1148. if (!cpu_online(next_cpu)) {
  1149. if (!tried) {
  1150. tried = true;
  1151. goto select_cpu;
  1152. }
  1153. /*
  1154. * Make sure to re-select CPU next time once after CPUs
  1155. * in hctx->cpumask become online again.
  1156. */
  1157. hctx->next_cpu = next_cpu;
  1158. hctx->next_cpu_batch = 1;
  1159. return WORK_CPU_UNBOUND;
  1160. }
  1161. hctx->next_cpu = next_cpu;
  1162. return next_cpu;
  1163. }
  1164. static void __blk_mq_delay_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async,
  1165. unsigned long msecs)
  1166. {
  1167. if (unlikely(blk_mq_hctx_stopped(hctx)))
  1168. return;
  1169. if (!async && !(hctx->flags & BLK_MQ_F_BLOCKING)) {
  1170. int cpu = get_cpu();
  1171. if (cpumask_test_cpu(cpu, hctx->cpumask)) {
  1172. __blk_mq_run_hw_queue(hctx);
  1173. put_cpu();
  1174. return;
  1175. }
  1176. put_cpu();
  1177. }
  1178. kblockd_mod_delayed_work_on(blk_mq_hctx_next_cpu(hctx), &hctx->run_work,
  1179. msecs_to_jiffies(msecs));
  1180. }
  1181. void blk_mq_delay_run_hw_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs)
  1182. {
  1183. __blk_mq_delay_run_hw_queue(hctx, true, msecs);
  1184. }
  1185. EXPORT_SYMBOL(blk_mq_delay_run_hw_queue);
  1186. bool blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
  1187. {
  1188. int srcu_idx;
  1189. bool need_run;
  1190. /*
  1191. * When queue is quiesced, we may be switching io scheduler, or
  1192. * updating nr_hw_queues, or other things, and we can't run queue
  1193. * any more, even __blk_mq_hctx_has_pending() can't be called safely.
  1194. *
  1195. * And queue will be rerun in blk_mq_unquiesce_queue() if it is
  1196. * quiesced.
  1197. */
  1198. hctx_lock(hctx, &srcu_idx);
  1199. need_run = !blk_queue_quiesced(hctx->queue) &&
  1200. blk_mq_hctx_has_pending(hctx);
  1201. hctx_unlock(hctx, srcu_idx);
  1202. if (need_run) {
  1203. __blk_mq_delay_run_hw_queue(hctx, async, 0);
  1204. return true;
  1205. }
  1206. return false;
  1207. }
  1208. EXPORT_SYMBOL(blk_mq_run_hw_queue);
  1209. void blk_mq_run_hw_queues(struct request_queue *q, bool async)
  1210. {
  1211. struct blk_mq_hw_ctx *hctx;
  1212. int i;
  1213. queue_for_each_hw_ctx(q, hctx, i) {
  1214. if (blk_mq_hctx_stopped(hctx))
  1215. continue;
  1216. blk_mq_run_hw_queue(hctx, async);
  1217. }
  1218. }
  1219. EXPORT_SYMBOL(blk_mq_run_hw_queues);
  1220. /**
  1221. * blk_mq_queue_stopped() - check whether one or more hctxs have been stopped
  1222. * @q: request queue.
  1223. *
  1224. * The caller is responsible for serializing this function against
  1225. * blk_mq_{start,stop}_hw_queue().
  1226. */
  1227. bool blk_mq_queue_stopped(struct request_queue *q)
  1228. {
  1229. struct blk_mq_hw_ctx *hctx;
  1230. int i;
  1231. queue_for_each_hw_ctx(q, hctx, i)
  1232. if (blk_mq_hctx_stopped(hctx))
  1233. return true;
  1234. return false;
  1235. }
  1236. EXPORT_SYMBOL(blk_mq_queue_stopped);
  1237. /*
  1238. * This function is often used for pausing .queue_rq() by driver when
  1239. * there isn't enough resource or some conditions aren't satisfied, and
  1240. * BLK_STS_RESOURCE is usually returned.
  1241. *
  1242. * We do not guarantee that dispatch can be drained or blocked
  1243. * after blk_mq_stop_hw_queue() returns. Please use
  1244. * blk_mq_quiesce_queue() for that requirement.
  1245. */
  1246. void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx)
  1247. {
  1248. cancel_delayed_work(&hctx->run_work);
  1249. set_bit(BLK_MQ_S_STOPPED, &hctx->state);
  1250. }
  1251. EXPORT_SYMBOL(blk_mq_stop_hw_queue);
  1252. /*
  1253. * This function is often used for pausing .queue_rq() by driver when
  1254. * there isn't enough resource or some conditions aren't satisfied, and
  1255. * BLK_STS_RESOURCE is usually returned.
  1256. *
  1257. * We do not guarantee that dispatch can be drained or blocked
  1258. * after blk_mq_stop_hw_queues() returns. Please use
  1259. * blk_mq_quiesce_queue() for that requirement.
  1260. */
  1261. void blk_mq_stop_hw_queues(struct request_queue *q)
  1262. {
  1263. struct blk_mq_hw_ctx *hctx;
  1264. int i;
  1265. queue_for_each_hw_ctx(q, hctx, i)
  1266. blk_mq_stop_hw_queue(hctx);
  1267. }
  1268. EXPORT_SYMBOL(blk_mq_stop_hw_queues);
  1269. void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx)
  1270. {
  1271. clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
  1272. blk_mq_run_hw_queue(hctx, false);
  1273. }
  1274. EXPORT_SYMBOL(blk_mq_start_hw_queue);
  1275. void blk_mq_start_hw_queues(struct request_queue *q)
  1276. {
  1277. struct blk_mq_hw_ctx *hctx;
  1278. int i;
  1279. queue_for_each_hw_ctx(q, hctx, i)
  1280. blk_mq_start_hw_queue(hctx);
  1281. }
  1282. EXPORT_SYMBOL(blk_mq_start_hw_queues);
  1283. void blk_mq_start_stopped_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
  1284. {
  1285. if (!blk_mq_hctx_stopped(hctx))
  1286. return;
  1287. clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
  1288. blk_mq_run_hw_queue(hctx, async);
  1289. }
  1290. EXPORT_SYMBOL_GPL(blk_mq_start_stopped_hw_queue);
  1291. void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async)
  1292. {
  1293. struct blk_mq_hw_ctx *hctx;
  1294. int i;
  1295. queue_for_each_hw_ctx(q, hctx, i)
  1296. blk_mq_start_stopped_hw_queue(hctx, async);
  1297. }
  1298. EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues);
  1299. static void blk_mq_run_work_fn(struct work_struct *work)
  1300. {
  1301. struct blk_mq_hw_ctx *hctx;
  1302. hctx = container_of(work, struct blk_mq_hw_ctx, run_work.work);
  1303. /*
  1304. * If we are stopped, don't run the queue.
  1305. */
  1306. if (test_bit(BLK_MQ_S_STOPPED, &hctx->state))
  1307. return;
  1308. __blk_mq_run_hw_queue(hctx);
  1309. }
  1310. static inline void __blk_mq_insert_req_list(struct blk_mq_hw_ctx *hctx,
  1311. struct request *rq,
  1312. bool at_head)
  1313. {
  1314. struct blk_mq_ctx *ctx = rq->mq_ctx;
  1315. lockdep_assert_held(&ctx->lock);
  1316. trace_block_rq_insert(hctx->queue, rq);
  1317. if (at_head)
  1318. list_add(&rq->queuelist, &ctx->rq_list);
  1319. else
  1320. list_add_tail(&rq->queuelist, &ctx->rq_list);
  1321. }
  1322. void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx, struct request *rq,
  1323. bool at_head)
  1324. {
  1325. struct blk_mq_ctx *ctx = rq->mq_ctx;
  1326. lockdep_assert_held(&ctx->lock);
  1327. __blk_mq_insert_req_list(hctx, rq, at_head);
  1328. blk_mq_hctx_mark_pending(hctx, ctx);
  1329. }
  1330. /*
  1331. * Should only be used carefully, when the caller knows we want to
  1332. * bypass a potential IO scheduler on the target device.
  1333. */
  1334. void blk_mq_request_bypass_insert(struct request *rq, bool run_queue)
  1335. {
  1336. struct blk_mq_ctx *ctx = rq->mq_ctx;
  1337. struct blk_mq_hw_ctx *hctx = blk_mq_map_queue(rq->q, ctx->cpu);
  1338. spin_lock(&hctx->lock);
  1339. list_add_tail(&rq->queuelist, &hctx->dispatch);
  1340. spin_unlock(&hctx->lock);
  1341. if (run_queue)
  1342. blk_mq_run_hw_queue(hctx, false);
  1343. }
  1344. void blk_mq_insert_requests(struct blk_mq_hw_ctx *hctx, struct blk_mq_ctx *ctx,
  1345. struct list_head *list)
  1346. {
  1347. struct request *rq;
  1348. /*
  1349. * preemption doesn't flush plug list, so it's possible ctx->cpu is
  1350. * offline now
  1351. */
  1352. list_for_each_entry(rq, list, queuelist) {
  1353. BUG_ON(rq->mq_ctx != ctx);
  1354. trace_block_rq_insert(hctx->queue, rq);
  1355. }
  1356. spin_lock(&ctx->lock);
  1357. list_splice_tail_init(list, &ctx->rq_list);
  1358. blk_mq_hctx_mark_pending(hctx, ctx);
  1359. spin_unlock(&ctx->lock);
  1360. }
  1361. static int plug_ctx_cmp(void *priv, struct list_head *a, struct list_head *b)
  1362. {
  1363. struct request *rqa = container_of(a, struct request, queuelist);
  1364. struct request *rqb = container_of(b, struct request, queuelist);
  1365. return !(rqa->mq_ctx < rqb->mq_ctx ||
  1366. (rqa->mq_ctx == rqb->mq_ctx &&
  1367. blk_rq_pos(rqa) < blk_rq_pos(rqb)));
  1368. }
  1369. void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule)
  1370. {
  1371. struct blk_mq_ctx *this_ctx;
  1372. struct request_queue *this_q;
  1373. struct request *rq;
  1374. LIST_HEAD(list);
  1375. LIST_HEAD(ctx_list);
  1376. unsigned int depth;
  1377. list_splice_init(&plug->mq_list, &list);
  1378. list_sort(NULL, &list, plug_ctx_cmp);
  1379. this_q = NULL;
  1380. this_ctx = NULL;
  1381. depth = 0;
  1382. while (!list_empty(&list)) {
  1383. rq = list_entry_rq(list.next);
  1384. list_del_init(&rq->queuelist);
  1385. BUG_ON(!rq->q);
  1386. if (rq->mq_ctx != this_ctx) {
  1387. if (this_ctx) {
  1388. trace_block_unplug(this_q, depth, !from_schedule);
  1389. blk_mq_sched_insert_requests(this_q, this_ctx,
  1390. &ctx_list,
  1391. from_schedule);
  1392. }
  1393. this_ctx = rq->mq_ctx;
  1394. this_q = rq->q;
  1395. depth = 0;
  1396. }
  1397. depth++;
  1398. list_add_tail(&rq->queuelist, &ctx_list);
  1399. }
  1400. /*
  1401. * If 'this_ctx' is set, we know we have entries to complete
  1402. * on 'ctx_list'. Do those.
  1403. */
  1404. if (this_ctx) {
  1405. trace_block_unplug(this_q, depth, !from_schedule);
  1406. blk_mq_sched_insert_requests(this_q, this_ctx, &ctx_list,
  1407. from_schedule);
  1408. }
  1409. }
  1410. static void blk_mq_bio_to_request(struct request *rq, struct bio *bio)
  1411. {
  1412. blk_init_request_from_bio(rq, bio);
  1413. blk_rq_set_rl(rq, blk_get_rl(rq->q, bio));
  1414. blk_account_io_start(rq, true);
  1415. }
  1416. static blk_qc_t request_to_qc_t(struct blk_mq_hw_ctx *hctx, struct request *rq)
  1417. {
  1418. if (rq->tag != -1)
  1419. return blk_tag_to_qc_t(rq->tag, hctx->queue_num, false);
  1420. return blk_tag_to_qc_t(rq->internal_tag, hctx->queue_num, true);
  1421. }
  1422. static blk_status_t __blk_mq_issue_directly(struct blk_mq_hw_ctx *hctx,
  1423. struct request *rq,
  1424. blk_qc_t *cookie)
  1425. {
  1426. struct request_queue *q = rq->q;
  1427. struct blk_mq_queue_data bd = {
  1428. .rq = rq,
  1429. .last = true,
  1430. };
  1431. blk_qc_t new_cookie;
  1432. blk_status_t ret;
  1433. new_cookie = request_to_qc_t(hctx, rq);
  1434. /*
  1435. * For OK queue, we are done. For error, caller may kill it.
  1436. * Any other error (busy), just add it to our list as we
  1437. * previously would have done.
  1438. */
  1439. ret = q->mq_ops->queue_rq(hctx, &bd);
  1440. switch (ret) {
  1441. case BLK_STS_OK:
  1442. blk_mq_update_dispatch_busy(hctx, false);
  1443. *cookie = new_cookie;
  1444. break;
  1445. case BLK_STS_RESOURCE:
  1446. case BLK_STS_DEV_RESOURCE:
  1447. blk_mq_update_dispatch_busy(hctx, true);
  1448. __blk_mq_requeue_request(rq);
  1449. break;
  1450. default:
  1451. blk_mq_update_dispatch_busy(hctx, false);
  1452. *cookie = BLK_QC_T_NONE;
  1453. break;
  1454. }
  1455. return ret;
  1456. }
  1457. static blk_status_t __blk_mq_try_issue_directly(struct blk_mq_hw_ctx *hctx,
  1458. struct request *rq,
  1459. blk_qc_t *cookie,
  1460. bool bypass_insert)
  1461. {
  1462. struct request_queue *q = rq->q;
  1463. bool run_queue = true;
  1464. /*
  1465. * RCU or SRCU read lock is needed before checking quiesced flag.
  1466. *
  1467. * When queue is stopped or quiesced, ignore 'bypass_insert' from
  1468. * blk_mq_request_issue_directly(), and return BLK_STS_OK to caller,
  1469. * and avoid driver to try to dispatch again.
  1470. */
  1471. if (blk_mq_hctx_stopped(hctx) || blk_queue_quiesced(q)) {
  1472. run_queue = false;
  1473. bypass_insert = false;
  1474. goto insert;
  1475. }
  1476. if (q->elevator && !bypass_insert)
  1477. goto insert;
  1478. if (!blk_mq_get_dispatch_budget(hctx))
  1479. goto insert;
  1480. if (!blk_mq_get_driver_tag(rq)) {
  1481. blk_mq_put_dispatch_budget(hctx);
  1482. goto insert;
  1483. }
  1484. return __blk_mq_issue_directly(hctx, rq, cookie);
  1485. insert:
  1486. if (bypass_insert)
  1487. return BLK_STS_RESOURCE;
  1488. blk_mq_request_bypass_insert(rq, run_queue);
  1489. return BLK_STS_OK;
  1490. }
  1491. static void blk_mq_try_issue_directly(struct blk_mq_hw_ctx *hctx,
  1492. struct request *rq, blk_qc_t *cookie)
  1493. {
  1494. blk_status_t ret;
  1495. int srcu_idx;
  1496. might_sleep_if(hctx->flags & BLK_MQ_F_BLOCKING);
  1497. hctx_lock(hctx, &srcu_idx);
  1498. ret = __blk_mq_try_issue_directly(hctx, rq, cookie, false);
  1499. if (ret == BLK_STS_RESOURCE || ret == BLK_STS_DEV_RESOURCE)
  1500. blk_mq_request_bypass_insert(rq, true);
  1501. else if (ret != BLK_STS_OK)
  1502. blk_mq_end_request(rq, ret);
  1503. hctx_unlock(hctx, srcu_idx);
  1504. }
  1505. blk_status_t blk_mq_request_issue_directly(struct request *rq)
  1506. {
  1507. blk_status_t ret;
  1508. int srcu_idx;
  1509. blk_qc_t unused_cookie;
  1510. struct blk_mq_ctx *ctx = rq->mq_ctx;
  1511. struct blk_mq_hw_ctx *hctx = blk_mq_map_queue(rq->q, ctx->cpu);
  1512. hctx_lock(hctx, &srcu_idx);
  1513. ret = __blk_mq_try_issue_directly(hctx, rq, &unused_cookie, true);
  1514. hctx_unlock(hctx, srcu_idx);
  1515. return ret;
  1516. }
  1517. void blk_mq_try_issue_list_directly(struct blk_mq_hw_ctx *hctx,
  1518. struct list_head *list)
  1519. {
  1520. while (!list_empty(list)) {
  1521. blk_status_t ret;
  1522. struct request *rq = list_first_entry(list, struct request,
  1523. queuelist);
  1524. list_del_init(&rq->queuelist);
  1525. ret = blk_mq_request_issue_directly(rq);
  1526. if (ret != BLK_STS_OK) {
  1527. if (ret == BLK_STS_RESOURCE ||
  1528. ret == BLK_STS_DEV_RESOURCE) {
  1529. blk_mq_request_bypass_insert(rq,
  1530. list_empty(list));
  1531. break;
  1532. }
  1533. blk_mq_end_request(rq, ret);
  1534. }
  1535. }
  1536. }
  1537. static blk_qc_t blk_mq_make_request(struct request_queue *q, struct bio *bio)
  1538. {
  1539. const int is_sync = op_is_sync(bio->bi_opf);
  1540. const int is_flush_fua = op_is_flush(bio->bi_opf);
  1541. struct blk_mq_alloc_data data = { .flags = 0 };
  1542. struct request *rq;
  1543. unsigned int request_count = 0;
  1544. struct blk_plug *plug;
  1545. struct request *same_queue_rq = NULL;
  1546. blk_qc_t cookie;
  1547. blk_queue_bounce(q, &bio);
  1548. blk_queue_split(q, &bio);
  1549. if (!bio_integrity_prep(bio))
  1550. return BLK_QC_T_NONE;
  1551. if (!is_flush_fua && !blk_queue_nomerges(q) &&
  1552. blk_attempt_plug_merge(q, bio, &request_count, &same_queue_rq))
  1553. return BLK_QC_T_NONE;
  1554. if (blk_mq_sched_bio_merge(q, bio))
  1555. return BLK_QC_T_NONE;
  1556. rq_qos_throttle(q, bio, NULL);
  1557. rq = blk_mq_get_request(q, bio, bio->bi_opf, &data);
  1558. if (unlikely(!rq)) {
  1559. rq_qos_cleanup(q, bio);
  1560. if (bio->bi_opf & REQ_NOWAIT)
  1561. bio_wouldblock_error(bio);
  1562. return BLK_QC_T_NONE;
  1563. }
  1564. trace_block_getrq(q, bio, bio->bi_opf);
  1565. rq_qos_track(q, rq, bio);
  1566. cookie = request_to_qc_t(data.hctx, rq);
  1567. plug = current->plug;
  1568. if (unlikely(is_flush_fua)) {
  1569. blk_mq_put_ctx(data.ctx);
  1570. blk_mq_bio_to_request(rq, bio);
  1571. /* bypass scheduler for flush rq */
  1572. blk_insert_flush(rq);
  1573. blk_mq_run_hw_queue(data.hctx, true);
  1574. } else if (plug && q->nr_hw_queues == 1) {
  1575. struct request *last = NULL;
  1576. blk_mq_put_ctx(data.ctx);
  1577. blk_mq_bio_to_request(rq, bio);
  1578. /*
  1579. * @request_count may become stale because of schedule
  1580. * out, so check the list again.
  1581. */
  1582. if (list_empty(&plug->mq_list))
  1583. request_count = 0;
  1584. else if (blk_queue_nomerges(q))
  1585. request_count = blk_plug_queued_count(q);
  1586. if (!request_count)
  1587. trace_block_plug(q);
  1588. else
  1589. last = list_entry_rq(plug->mq_list.prev);
  1590. if (request_count >= BLK_MAX_REQUEST_COUNT || (last &&
  1591. blk_rq_bytes(last) >= BLK_PLUG_FLUSH_SIZE)) {
  1592. blk_flush_plug_list(plug, false);
  1593. trace_block_plug(q);
  1594. }
  1595. list_add_tail(&rq->queuelist, &plug->mq_list);
  1596. } else if (plug && !blk_queue_nomerges(q)) {
  1597. blk_mq_bio_to_request(rq, bio);
  1598. /*
  1599. * We do limited plugging. If the bio can be merged, do that.
  1600. * Otherwise the existing request in the plug list will be
  1601. * issued. So the plug list will have one request at most
  1602. * The plug list might get flushed before this. If that happens,
  1603. * the plug list is empty, and same_queue_rq is invalid.
  1604. */
  1605. if (list_empty(&plug->mq_list))
  1606. same_queue_rq = NULL;
  1607. if (same_queue_rq)
  1608. list_del_init(&same_queue_rq->queuelist);
  1609. list_add_tail(&rq->queuelist, &plug->mq_list);
  1610. blk_mq_put_ctx(data.ctx);
  1611. if (same_queue_rq) {
  1612. data.hctx = blk_mq_map_queue(q,
  1613. same_queue_rq->mq_ctx->cpu);
  1614. blk_mq_try_issue_directly(data.hctx, same_queue_rq,
  1615. &cookie);
  1616. }
  1617. } else if ((q->nr_hw_queues > 1 && is_sync) || (!q->elevator &&
  1618. !data.hctx->dispatch_busy)) {
  1619. blk_mq_put_ctx(data.ctx);
  1620. blk_mq_bio_to_request(rq, bio);
  1621. blk_mq_try_issue_directly(data.hctx, rq, &cookie);
  1622. } else {
  1623. blk_mq_put_ctx(data.ctx);
  1624. blk_mq_bio_to_request(rq, bio);
  1625. blk_mq_sched_insert_request(rq, false, true, true);
  1626. }
  1627. return cookie;
  1628. }
  1629. void blk_mq_free_rqs(struct blk_mq_tag_set *set, struct blk_mq_tags *tags,
  1630. unsigned int hctx_idx)
  1631. {
  1632. struct page *page;
  1633. if (tags->rqs && set->ops->exit_request) {
  1634. int i;
  1635. for (i = 0; i < tags->nr_tags; i++) {
  1636. struct request *rq = tags->static_rqs[i];
  1637. if (!rq)
  1638. continue;
  1639. set->ops->exit_request(set, rq, hctx_idx);
  1640. tags->static_rqs[i] = NULL;
  1641. }
  1642. }
  1643. while (!list_empty(&tags->page_list)) {
  1644. page = list_first_entry(&tags->page_list, struct page, lru);
  1645. list_del_init(&page->lru);
  1646. /*
  1647. * Remove kmemleak object previously allocated in
  1648. * blk_mq_init_rq_map().
  1649. */
  1650. kmemleak_free(page_address(page));
  1651. __free_pages(page, page->private);
  1652. }
  1653. }
  1654. void blk_mq_free_rq_map(struct blk_mq_tags *tags)
  1655. {
  1656. kfree(tags->rqs);
  1657. tags->rqs = NULL;
  1658. kfree(tags->static_rqs);
  1659. tags->static_rqs = NULL;
  1660. blk_mq_free_tags(tags);
  1661. }
  1662. struct blk_mq_tags *blk_mq_alloc_rq_map(struct blk_mq_tag_set *set,
  1663. unsigned int hctx_idx,
  1664. unsigned int nr_tags,
  1665. unsigned int reserved_tags)
  1666. {
  1667. struct blk_mq_tags *tags;
  1668. int node;
  1669. node = blk_mq_hw_queue_to_node(set->mq_map, hctx_idx);
  1670. if (node == NUMA_NO_NODE)
  1671. node = set->numa_node;
  1672. tags = blk_mq_init_tags(nr_tags, reserved_tags, node,
  1673. BLK_MQ_FLAG_TO_ALLOC_POLICY(set->flags));
  1674. if (!tags)
  1675. return NULL;
  1676. tags->rqs = kcalloc_node(nr_tags, sizeof(struct request *),
  1677. GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY,
  1678. node);
  1679. if (!tags->rqs) {
  1680. blk_mq_free_tags(tags);
  1681. return NULL;
  1682. }
  1683. tags->static_rqs = kcalloc_node(nr_tags, sizeof(struct request *),
  1684. GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY,
  1685. node);
  1686. if (!tags->static_rqs) {
  1687. kfree(tags->rqs);
  1688. blk_mq_free_tags(tags);
  1689. return NULL;
  1690. }
  1691. return tags;
  1692. }
  1693. static size_t order_to_size(unsigned int order)
  1694. {
  1695. return (size_t)PAGE_SIZE << order;
  1696. }
  1697. static int blk_mq_init_request(struct blk_mq_tag_set *set, struct request *rq,
  1698. unsigned int hctx_idx, int node)
  1699. {
  1700. int ret;
  1701. if (set->ops->init_request) {
  1702. ret = set->ops->init_request(set, rq, hctx_idx, node);
  1703. if (ret)
  1704. return ret;
  1705. }
  1706. WRITE_ONCE(rq->state, MQ_RQ_IDLE);
  1707. return 0;
  1708. }
  1709. int blk_mq_alloc_rqs(struct blk_mq_tag_set *set, struct blk_mq_tags *tags,
  1710. unsigned int hctx_idx, unsigned int depth)
  1711. {
  1712. unsigned int i, j, entries_per_page, max_order = 4;
  1713. size_t rq_size, left;
  1714. int node;
  1715. node = blk_mq_hw_queue_to_node(set->mq_map, hctx_idx);
  1716. if (node == NUMA_NO_NODE)
  1717. node = set->numa_node;
  1718. INIT_LIST_HEAD(&tags->page_list);
  1719. /*
  1720. * rq_size is the size of the request plus driver payload, rounded
  1721. * to the cacheline size
  1722. */
  1723. rq_size = round_up(sizeof(struct request) + set->cmd_size,
  1724. cache_line_size());
  1725. left = rq_size * depth;
  1726. for (i = 0; i < depth; ) {
  1727. int this_order = max_order;
  1728. struct page *page;
  1729. int to_do;
  1730. void *p;
  1731. while (this_order && left < order_to_size(this_order - 1))
  1732. this_order--;
  1733. do {
  1734. page = alloc_pages_node(node,
  1735. GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO,
  1736. this_order);
  1737. if (page)
  1738. break;
  1739. if (!this_order--)
  1740. break;
  1741. if (order_to_size(this_order) < rq_size)
  1742. break;
  1743. } while (1);
  1744. if (!page)
  1745. goto fail;
  1746. page->private = this_order;
  1747. list_add_tail(&page->lru, &tags->page_list);
  1748. p = page_address(page);
  1749. /*
  1750. * Allow kmemleak to scan these pages as they contain pointers
  1751. * to additional allocations like via ops->init_request().
  1752. */
  1753. kmemleak_alloc(p, order_to_size(this_order), 1, GFP_NOIO);
  1754. entries_per_page = order_to_size(this_order) / rq_size;
  1755. to_do = min(entries_per_page, depth - i);
  1756. left -= to_do * rq_size;
  1757. for (j = 0; j < to_do; j++) {
  1758. struct request *rq = p;
  1759. tags->static_rqs[i] = rq;
  1760. if (blk_mq_init_request(set, rq, hctx_idx, node)) {
  1761. tags->static_rqs[i] = NULL;
  1762. goto fail;
  1763. }
  1764. p += rq_size;
  1765. i++;
  1766. }
  1767. }
  1768. return 0;
  1769. fail:
  1770. blk_mq_free_rqs(set, tags, hctx_idx);
  1771. return -ENOMEM;
  1772. }
  1773. /*
  1774. * 'cpu' is going away. splice any existing rq_list entries from this
  1775. * software queue to the hw queue dispatch list, and ensure that it
  1776. * gets run.
  1777. */
  1778. static int blk_mq_hctx_notify_dead(unsigned int cpu, struct hlist_node *node)
  1779. {
  1780. struct blk_mq_hw_ctx *hctx;
  1781. struct blk_mq_ctx *ctx;
  1782. LIST_HEAD(tmp);
  1783. hctx = hlist_entry_safe(node, struct blk_mq_hw_ctx, cpuhp_dead);
  1784. ctx = __blk_mq_get_ctx(hctx->queue, cpu);
  1785. spin_lock(&ctx->lock);
  1786. if (!list_empty(&ctx->rq_list)) {
  1787. list_splice_init(&ctx->rq_list, &tmp);
  1788. blk_mq_hctx_clear_pending(hctx, ctx);
  1789. }
  1790. spin_unlock(&ctx->lock);
  1791. if (list_empty(&tmp))
  1792. return 0;
  1793. spin_lock(&hctx->lock);
  1794. list_splice_tail_init(&tmp, &hctx->dispatch);
  1795. spin_unlock(&hctx->lock);
  1796. blk_mq_run_hw_queue(hctx, true);
  1797. return 0;
  1798. }
  1799. static void blk_mq_remove_cpuhp(struct blk_mq_hw_ctx *hctx)
  1800. {
  1801. cpuhp_state_remove_instance_nocalls(CPUHP_BLK_MQ_DEAD,
  1802. &hctx->cpuhp_dead);
  1803. }
  1804. /* hctx->ctxs will be freed in queue's release handler */
  1805. static void blk_mq_exit_hctx(struct request_queue *q,
  1806. struct blk_mq_tag_set *set,
  1807. struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx)
  1808. {
  1809. if (blk_mq_hw_queue_mapped(hctx))
  1810. blk_mq_tag_idle(hctx);
  1811. if (set->ops->exit_request)
  1812. set->ops->exit_request(set, hctx->fq->flush_rq, hctx_idx);
  1813. if (set->ops->exit_hctx)
  1814. set->ops->exit_hctx(hctx, hctx_idx);
  1815. if (hctx->flags & BLK_MQ_F_BLOCKING)
  1816. cleanup_srcu_struct(hctx->srcu);
  1817. blk_mq_remove_cpuhp(hctx);
  1818. blk_free_flush_queue(hctx->fq);
  1819. sbitmap_free(&hctx->ctx_map);
  1820. }
  1821. static void blk_mq_exit_hw_queues(struct request_queue *q,
  1822. struct blk_mq_tag_set *set, int nr_queue)
  1823. {
  1824. struct blk_mq_hw_ctx *hctx;
  1825. unsigned int i;
  1826. queue_for_each_hw_ctx(q, hctx, i) {
  1827. if (i == nr_queue)
  1828. break;
  1829. blk_mq_debugfs_unregister_hctx(hctx);
  1830. blk_mq_exit_hctx(q, set, hctx, i);
  1831. }
  1832. }
  1833. static int blk_mq_init_hctx(struct request_queue *q,
  1834. struct blk_mq_tag_set *set,
  1835. struct blk_mq_hw_ctx *hctx, unsigned hctx_idx)
  1836. {
  1837. int node;
  1838. node = hctx->numa_node;
  1839. if (node == NUMA_NO_NODE)
  1840. node = hctx->numa_node = set->numa_node;
  1841. INIT_DELAYED_WORK(&hctx->run_work, blk_mq_run_work_fn);
  1842. spin_lock_init(&hctx->lock);
  1843. INIT_LIST_HEAD(&hctx->dispatch);
  1844. hctx->queue = q;
  1845. hctx->flags = set->flags & ~BLK_MQ_F_TAG_SHARED;
  1846. cpuhp_state_add_instance_nocalls(CPUHP_BLK_MQ_DEAD, &hctx->cpuhp_dead);
  1847. hctx->tags = set->tags[hctx_idx];
  1848. /*
  1849. * Allocate space for all possible cpus to avoid allocation at
  1850. * runtime
  1851. */
  1852. hctx->ctxs = kmalloc_array_node(nr_cpu_ids, sizeof(void *),
  1853. GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY, node);
  1854. if (!hctx->ctxs)
  1855. goto unregister_cpu_notifier;
  1856. if (sbitmap_init_node(&hctx->ctx_map, nr_cpu_ids, ilog2(8),
  1857. GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY, node))
  1858. goto free_ctxs;
  1859. hctx->nr_ctx = 0;
  1860. spin_lock_init(&hctx->dispatch_wait_lock);
  1861. init_waitqueue_func_entry(&hctx->dispatch_wait, blk_mq_dispatch_wake);
  1862. INIT_LIST_HEAD(&hctx->dispatch_wait.entry);
  1863. if (set->ops->init_hctx &&
  1864. set->ops->init_hctx(hctx, set->driver_data, hctx_idx))
  1865. goto free_bitmap;
  1866. hctx->fq = blk_alloc_flush_queue(q, hctx->numa_node, set->cmd_size,
  1867. GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY);
  1868. if (!hctx->fq)
  1869. goto exit_hctx;
  1870. if (blk_mq_init_request(set, hctx->fq->flush_rq, hctx_idx, node))
  1871. goto free_fq;
  1872. if (hctx->flags & BLK_MQ_F_BLOCKING)
  1873. init_srcu_struct(hctx->srcu);
  1874. return 0;
  1875. free_fq:
  1876. kfree(hctx->fq);
  1877. exit_hctx:
  1878. if (set->ops->exit_hctx)
  1879. set->ops->exit_hctx(hctx, hctx_idx);
  1880. free_bitmap:
  1881. sbitmap_free(&hctx->ctx_map);
  1882. free_ctxs:
  1883. kfree(hctx->ctxs);
  1884. unregister_cpu_notifier:
  1885. blk_mq_remove_cpuhp(hctx);
  1886. return -1;
  1887. }
  1888. static void blk_mq_init_cpu_queues(struct request_queue *q,
  1889. unsigned int nr_hw_queues)
  1890. {
  1891. unsigned int i;
  1892. for_each_possible_cpu(i) {
  1893. struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i);
  1894. struct blk_mq_hw_ctx *hctx;
  1895. __ctx->cpu = i;
  1896. spin_lock_init(&__ctx->lock);
  1897. INIT_LIST_HEAD(&__ctx->rq_list);
  1898. __ctx->queue = q;
  1899. /*
  1900. * Set local node, IFF we have more than one hw queue. If
  1901. * not, we remain on the home node of the device
  1902. */
  1903. hctx = blk_mq_map_queue(q, i);
  1904. if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE)
  1905. hctx->numa_node = local_memory_node(cpu_to_node(i));
  1906. }
  1907. }
  1908. static bool __blk_mq_alloc_rq_map(struct blk_mq_tag_set *set, int hctx_idx)
  1909. {
  1910. int ret = 0;
  1911. set->tags[hctx_idx] = blk_mq_alloc_rq_map(set, hctx_idx,
  1912. set->queue_depth, set->reserved_tags);
  1913. if (!set->tags[hctx_idx])
  1914. return false;
  1915. ret = blk_mq_alloc_rqs(set, set->tags[hctx_idx], hctx_idx,
  1916. set->queue_depth);
  1917. if (!ret)
  1918. return true;
  1919. blk_mq_free_rq_map(set->tags[hctx_idx]);
  1920. set->tags[hctx_idx] = NULL;
  1921. return false;
  1922. }
  1923. static void blk_mq_free_map_and_requests(struct blk_mq_tag_set *set,
  1924. unsigned int hctx_idx)
  1925. {
  1926. if (set->tags[hctx_idx]) {
  1927. blk_mq_free_rqs(set, set->tags[hctx_idx], hctx_idx);
  1928. blk_mq_free_rq_map(set->tags[hctx_idx]);
  1929. set->tags[hctx_idx] = NULL;
  1930. }
  1931. }
  1932. static void blk_mq_map_swqueue(struct request_queue *q)
  1933. {
  1934. unsigned int i, hctx_idx;
  1935. struct blk_mq_hw_ctx *hctx;
  1936. struct blk_mq_ctx *ctx;
  1937. struct blk_mq_tag_set *set = q->tag_set;
  1938. /*
  1939. * Avoid others reading imcomplete hctx->cpumask through sysfs
  1940. */
  1941. mutex_lock(&q->sysfs_lock);
  1942. queue_for_each_hw_ctx(q, hctx, i) {
  1943. cpumask_clear(hctx->cpumask);
  1944. hctx->nr_ctx = 0;
  1945. hctx->dispatch_from = NULL;
  1946. }
  1947. /*
  1948. * Map software to hardware queues.
  1949. *
  1950. * If the cpu isn't present, the cpu is mapped to first hctx.
  1951. */
  1952. for_each_possible_cpu(i) {
  1953. hctx_idx = q->mq_map[i];
  1954. /* unmapped hw queue can be remapped after CPU topo changed */
  1955. if (!set->tags[hctx_idx] &&
  1956. !__blk_mq_alloc_rq_map(set, hctx_idx)) {
  1957. /*
  1958. * If tags initialization fail for some hctx,
  1959. * that hctx won't be brought online. In this
  1960. * case, remap the current ctx to hctx[0] which
  1961. * is guaranteed to always have tags allocated
  1962. */
  1963. q->mq_map[i] = 0;
  1964. }
  1965. ctx = per_cpu_ptr(q->queue_ctx, i);
  1966. hctx = blk_mq_map_queue(q, i);
  1967. cpumask_set_cpu(i, hctx->cpumask);
  1968. ctx->index_hw = hctx->nr_ctx;
  1969. hctx->ctxs[hctx->nr_ctx++] = ctx;
  1970. }
  1971. mutex_unlock(&q->sysfs_lock);
  1972. queue_for_each_hw_ctx(q, hctx, i) {
  1973. /*
  1974. * If no software queues are mapped to this hardware queue,
  1975. * disable it and free the request entries.
  1976. */
  1977. if (!hctx->nr_ctx) {
  1978. /* Never unmap queue 0. We need it as a
  1979. * fallback in case of a new remap fails
  1980. * allocation
  1981. */
  1982. if (i && set->tags[i])
  1983. blk_mq_free_map_and_requests(set, i);
  1984. hctx->tags = NULL;
  1985. continue;
  1986. }
  1987. hctx->tags = set->tags[i];
  1988. WARN_ON(!hctx->tags);
  1989. /*
  1990. * Set the map size to the number of mapped software queues.
  1991. * This is more accurate and more efficient than looping
  1992. * over all possibly mapped software queues.
  1993. */
  1994. sbitmap_resize(&hctx->ctx_map, hctx->nr_ctx);
  1995. /*
  1996. * Initialize batch roundrobin counts
  1997. */
  1998. hctx->next_cpu = blk_mq_first_mapped_cpu(hctx);
  1999. hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
  2000. }
  2001. }
  2002. /*
  2003. * Caller needs to ensure that we're either frozen/quiesced, or that
  2004. * the queue isn't live yet.
  2005. */
  2006. static void queue_set_hctx_shared(struct request_queue *q, bool shared)
  2007. {
  2008. struct blk_mq_hw_ctx *hctx;
  2009. int i;
  2010. queue_for_each_hw_ctx(q, hctx, i) {
  2011. if (shared)
  2012. hctx->flags |= BLK_MQ_F_TAG_SHARED;
  2013. else
  2014. hctx->flags &= ~BLK_MQ_F_TAG_SHARED;
  2015. }
  2016. }
  2017. static void blk_mq_update_tag_set_depth(struct blk_mq_tag_set *set,
  2018. bool shared)
  2019. {
  2020. struct request_queue *q;
  2021. lockdep_assert_held(&set->tag_list_lock);
  2022. list_for_each_entry(q, &set->tag_list, tag_set_list) {
  2023. blk_mq_freeze_queue(q);
  2024. queue_set_hctx_shared(q, shared);
  2025. blk_mq_unfreeze_queue(q);
  2026. }
  2027. }
  2028. static void blk_mq_del_queue_tag_set(struct request_queue *q)
  2029. {
  2030. struct blk_mq_tag_set *set = q->tag_set;
  2031. mutex_lock(&set->tag_list_lock);
  2032. list_del_rcu(&q->tag_set_list);
  2033. if (list_is_singular(&set->tag_list)) {
  2034. /* just transitioned to unshared */
  2035. set->flags &= ~BLK_MQ_F_TAG_SHARED;
  2036. /* update existing queue */
  2037. blk_mq_update_tag_set_depth(set, false);
  2038. }
  2039. mutex_unlock(&set->tag_list_lock);
  2040. INIT_LIST_HEAD(&q->tag_set_list);
  2041. }
  2042. static void blk_mq_add_queue_tag_set(struct blk_mq_tag_set *set,
  2043. struct request_queue *q)
  2044. {
  2045. q->tag_set = set;
  2046. mutex_lock(&set->tag_list_lock);
  2047. /*
  2048. * Check to see if we're transitioning to shared (from 1 to 2 queues).
  2049. */
  2050. if (!list_empty(&set->tag_list) &&
  2051. !(set->flags & BLK_MQ_F_TAG_SHARED)) {
  2052. set->flags |= BLK_MQ_F_TAG_SHARED;
  2053. /* update existing queue */
  2054. blk_mq_update_tag_set_depth(set, true);
  2055. }
  2056. if (set->flags & BLK_MQ_F_TAG_SHARED)
  2057. queue_set_hctx_shared(q, true);
  2058. list_add_tail_rcu(&q->tag_set_list, &set->tag_list);
  2059. mutex_unlock(&set->tag_list_lock);
  2060. }
  2061. /*
  2062. * It is the actual release handler for mq, but we do it from
  2063. * request queue's release handler for avoiding use-after-free
  2064. * and headache because q->mq_kobj shouldn't have been introduced,
  2065. * but we can't group ctx/kctx kobj without it.
  2066. */
  2067. void blk_mq_release(struct request_queue *q)
  2068. {
  2069. struct blk_mq_hw_ctx *hctx;
  2070. unsigned int i;
  2071. /* hctx kobj stays in hctx */
  2072. queue_for_each_hw_ctx(q, hctx, i) {
  2073. if (!hctx)
  2074. continue;
  2075. kobject_put(&hctx->kobj);
  2076. }
  2077. q->mq_map = NULL;
  2078. kfree(q->queue_hw_ctx);
  2079. /*
  2080. * release .mq_kobj and sw queue's kobject now because
  2081. * both share lifetime with request queue.
  2082. */
  2083. blk_mq_sysfs_deinit(q);
  2084. free_percpu(q->queue_ctx);
  2085. }
  2086. struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set)
  2087. {
  2088. struct request_queue *uninit_q, *q;
  2089. uninit_q = blk_alloc_queue_node(GFP_KERNEL, set->numa_node, NULL);
  2090. if (!uninit_q)
  2091. return ERR_PTR(-ENOMEM);
  2092. q = blk_mq_init_allocated_queue(set, uninit_q);
  2093. if (IS_ERR(q))
  2094. blk_cleanup_queue(uninit_q);
  2095. return q;
  2096. }
  2097. EXPORT_SYMBOL(blk_mq_init_queue);
  2098. /*
  2099. * Helper for setting up a queue with mq ops, given queue depth, and
  2100. * the passed in mq ops flags.
  2101. */
  2102. struct request_queue *blk_mq_init_sq_queue(struct blk_mq_tag_set *set,
  2103. const struct blk_mq_ops *ops,
  2104. unsigned int queue_depth,
  2105. unsigned int set_flags)
  2106. {
  2107. struct request_queue *q;
  2108. int ret;
  2109. memset(set, 0, sizeof(*set));
  2110. set->ops = ops;
  2111. set->nr_hw_queues = 1;
  2112. set->queue_depth = queue_depth;
  2113. set->numa_node = NUMA_NO_NODE;
  2114. set->flags = set_flags;
  2115. ret = blk_mq_alloc_tag_set(set);
  2116. if (ret)
  2117. return ERR_PTR(ret);
  2118. q = blk_mq_init_queue(set);
  2119. if (IS_ERR(q)) {
  2120. blk_mq_free_tag_set(set);
  2121. return q;
  2122. }
  2123. return q;
  2124. }
  2125. EXPORT_SYMBOL(blk_mq_init_sq_queue);
  2126. static int blk_mq_hw_ctx_size(struct blk_mq_tag_set *tag_set)
  2127. {
  2128. int hw_ctx_size = sizeof(struct blk_mq_hw_ctx);
  2129. BUILD_BUG_ON(ALIGN(offsetof(struct blk_mq_hw_ctx, srcu),
  2130. __alignof__(struct blk_mq_hw_ctx)) !=
  2131. sizeof(struct blk_mq_hw_ctx));
  2132. if (tag_set->flags & BLK_MQ_F_BLOCKING)
  2133. hw_ctx_size += sizeof(struct srcu_struct);
  2134. return hw_ctx_size;
  2135. }
  2136. static struct blk_mq_hw_ctx *blk_mq_alloc_and_init_hctx(
  2137. struct blk_mq_tag_set *set, struct request_queue *q,
  2138. int hctx_idx, int node)
  2139. {
  2140. struct blk_mq_hw_ctx *hctx;
  2141. hctx = kzalloc_node(blk_mq_hw_ctx_size(set),
  2142. GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY,
  2143. node);
  2144. if (!hctx)
  2145. return NULL;
  2146. if (!zalloc_cpumask_var_node(&hctx->cpumask,
  2147. GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY,
  2148. node)) {
  2149. kfree(hctx);
  2150. return NULL;
  2151. }
  2152. atomic_set(&hctx->nr_active, 0);
  2153. hctx->numa_node = node;
  2154. hctx->queue_num = hctx_idx;
  2155. if (blk_mq_init_hctx(q, set, hctx, hctx_idx)) {
  2156. free_cpumask_var(hctx->cpumask);
  2157. kfree(hctx);
  2158. return NULL;
  2159. }
  2160. blk_mq_hctx_kobj_init(hctx);
  2161. return hctx;
  2162. }
  2163. static void blk_mq_realloc_hw_ctxs(struct blk_mq_tag_set *set,
  2164. struct request_queue *q)
  2165. {
  2166. int i, j, end;
  2167. struct blk_mq_hw_ctx **hctxs = q->queue_hw_ctx;
  2168. /* protect against switching io scheduler */
  2169. mutex_lock(&q->sysfs_lock);
  2170. for (i = 0; i < set->nr_hw_queues; i++) {
  2171. int node;
  2172. struct blk_mq_hw_ctx *hctx;
  2173. node = blk_mq_hw_queue_to_node(q->mq_map, i);
  2174. /*
  2175. * If the hw queue has been mapped to another numa node,
  2176. * we need to realloc the hctx. If allocation fails, fallback
  2177. * to use the previous one.
  2178. */
  2179. if (hctxs[i] && (hctxs[i]->numa_node == node))
  2180. continue;
  2181. hctx = blk_mq_alloc_and_init_hctx(set, q, i, node);
  2182. if (hctx) {
  2183. if (hctxs[i]) {
  2184. blk_mq_exit_hctx(q, set, hctxs[i], i);
  2185. kobject_put(&hctxs[i]->kobj);
  2186. }
  2187. hctxs[i] = hctx;
  2188. } else {
  2189. if (hctxs[i])
  2190. pr_warn("Allocate new hctx on node %d fails,\
  2191. fallback to previous one on node %d\n",
  2192. node, hctxs[i]->numa_node);
  2193. else
  2194. break;
  2195. }
  2196. }
  2197. /*
  2198. * Increasing nr_hw_queues fails. Free the newly allocated
  2199. * hctxs and keep the previous q->nr_hw_queues.
  2200. */
  2201. if (i != set->nr_hw_queues) {
  2202. j = q->nr_hw_queues;
  2203. end = i;
  2204. } else {
  2205. j = i;
  2206. end = q->nr_hw_queues;
  2207. q->nr_hw_queues = set->nr_hw_queues;
  2208. }
  2209. for (; j < end; j++) {
  2210. struct blk_mq_hw_ctx *hctx = hctxs[j];
  2211. if (hctx) {
  2212. if (hctx->tags)
  2213. blk_mq_free_map_and_requests(set, j);
  2214. blk_mq_exit_hctx(q, set, hctx, j);
  2215. kobject_put(&hctx->kobj);
  2216. hctxs[j] = NULL;
  2217. }
  2218. }
  2219. mutex_unlock(&q->sysfs_lock);
  2220. }
  2221. struct request_queue *blk_mq_init_allocated_queue(struct blk_mq_tag_set *set,
  2222. struct request_queue *q)
  2223. {
  2224. /* mark the queue as mq asap */
  2225. q->mq_ops = set->ops;
  2226. q->poll_cb = blk_stat_alloc_callback(blk_mq_poll_stats_fn,
  2227. blk_mq_poll_stats_bkt,
  2228. BLK_MQ_POLL_STATS_BKTS, q);
  2229. if (!q->poll_cb)
  2230. goto err_exit;
  2231. q->queue_ctx = alloc_percpu(struct blk_mq_ctx);
  2232. if (!q->queue_ctx)
  2233. goto err_exit;
  2234. /* init q->mq_kobj and sw queues' kobjects */
  2235. blk_mq_sysfs_init(q);
  2236. q->queue_hw_ctx = kcalloc_node(nr_cpu_ids, sizeof(*(q->queue_hw_ctx)),
  2237. GFP_KERNEL, set->numa_node);
  2238. if (!q->queue_hw_ctx)
  2239. goto err_percpu;
  2240. q->mq_map = set->mq_map;
  2241. blk_mq_realloc_hw_ctxs(set, q);
  2242. if (!q->nr_hw_queues)
  2243. goto err_hctxs;
  2244. INIT_WORK(&q->timeout_work, blk_mq_timeout_work);
  2245. blk_queue_rq_timeout(q, set->timeout ? set->timeout : 30 * HZ);
  2246. q->nr_queues = nr_cpu_ids;
  2247. q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT;
  2248. if (!(set->flags & BLK_MQ_F_SG_MERGE))
  2249. queue_flag_set_unlocked(QUEUE_FLAG_NO_SG_MERGE, q);
  2250. q->sg_reserved_size = INT_MAX;
  2251. INIT_DELAYED_WORK(&q->requeue_work, blk_mq_requeue_work);
  2252. INIT_LIST_HEAD(&q->requeue_list);
  2253. spin_lock_init(&q->requeue_lock);
  2254. blk_queue_make_request(q, blk_mq_make_request);
  2255. if (q->mq_ops->poll)
  2256. q->poll_fn = blk_mq_poll;
  2257. /*
  2258. * Do this after blk_queue_make_request() overrides it...
  2259. */
  2260. q->nr_requests = set->queue_depth;
  2261. /*
  2262. * Default to classic polling
  2263. */
  2264. q->poll_nsec = -1;
  2265. if (set->ops->complete)
  2266. blk_queue_softirq_done(q, set->ops->complete);
  2267. blk_mq_init_cpu_queues(q, set->nr_hw_queues);
  2268. blk_mq_add_queue_tag_set(set, q);
  2269. blk_mq_map_swqueue(q);
  2270. if (!(set->flags & BLK_MQ_F_NO_SCHED)) {
  2271. int ret;
  2272. ret = elevator_init_mq(q);
  2273. if (ret)
  2274. return ERR_PTR(ret);
  2275. }
  2276. return q;
  2277. err_hctxs:
  2278. kfree(q->queue_hw_ctx);
  2279. err_percpu:
  2280. free_percpu(q->queue_ctx);
  2281. err_exit:
  2282. q->mq_ops = NULL;
  2283. return ERR_PTR(-ENOMEM);
  2284. }
  2285. EXPORT_SYMBOL(blk_mq_init_allocated_queue);
  2286. void blk_mq_free_queue(struct request_queue *q)
  2287. {
  2288. struct blk_mq_tag_set *set = q->tag_set;
  2289. blk_mq_del_queue_tag_set(q);
  2290. blk_mq_exit_hw_queues(q, set, set->nr_hw_queues);
  2291. }
  2292. static int __blk_mq_alloc_rq_maps(struct blk_mq_tag_set *set)
  2293. {
  2294. int i;
  2295. for (i = 0; i < set->nr_hw_queues; i++)
  2296. if (!__blk_mq_alloc_rq_map(set, i))
  2297. goto out_unwind;
  2298. return 0;
  2299. out_unwind:
  2300. while (--i >= 0)
  2301. blk_mq_free_rq_map(set->tags[i]);
  2302. return -ENOMEM;
  2303. }
  2304. /*
  2305. * Allocate the request maps associated with this tag_set. Note that this
  2306. * may reduce the depth asked for, if memory is tight. set->queue_depth
  2307. * will be updated to reflect the allocated depth.
  2308. */
  2309. static int blk_mq_alloc_rq_maps(struct blk_mq_tag_set *set)
  2310. {
  2311. unsigned int depth;
  2312. int err;
  2313. depth = set->queue_depth;
  2314. do {
  2315. err = __blk_mq_alloc_rq_maps(set);
  2316. if (!err)
  2317. break;
  2318. set->queue_depth >>= 1;
  2319. if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN) {
  2320. err = -ENOMEM;
  2321. break;
  2322. }
  2323. } while (set->queue_depth);
  2324. if (!set->queue_depth || err) {
  2325. pr_err("blk-mq: failed to allocate request map\n");
  2326. return -ENOMEM;
  2327. }
  2328. if (depth != set->queue_depth)
  2329. pr_info("blk-mq: reduced tag depth (%u -> %u)\n",
  2330. depth, set->queue_depth);
  2331. return 0;
  2332. }
  2333. static int blk_mq_update_queue_map(struct blk_mq_tag_set *set)
  2334. {
  2335. if (set->ops->map_queues) {
  2336. /*
  2337. * transport .map_queues is usually done in the following
  2338. * way:
  2339. *
  2340. * for (queue = 0; queue < set->nr_hw_queues; queue++) {
  2341. * mask = get_cpu_mask(queue)
  2342. * for_each_cpu(cpu, mask)
  2343. * set->mq_map[cpu] = queue;
  2344. * }
  2345. *
  2346. * When we need to remap, the table has to be cleared for
  2347. * killing stale mapping since one CPU may not be mapped
  2348. * to any hw queue.
  2349. */
  2350. blk_mq_clear_mq_map(set);
  2351. return set->ops->map_queues(set);
  2352. } else
  2353. return blk_mq_map_queues(set);
  2354. }
  2355. /*
  2356. * Alloc a tag set to be associated with one or more request queues.
  2357. * May fail with EINVAL for various error conditions. May adjust the
  2358. * requested depth down, if it's too large. In that case, the set
  2359. * value will be stored in set->queue_depth.
  2360. */
  2361. int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set)
  2362. {
  2363. int ret;
  2364. BUILD_BUG_ON(BLK_MQ_MAX_DEPTH > 1 << BLK_MQ_UNIQUE_TAG_BITS);
  2365. if (!set->nr_hw_queues)
  2366. return -EINVAL;
  2367. if (!set->queue_depth)
  2368. return -EINVAL;
  2369. if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN)
  2370. return -EINVAL;
  2371. if (!set->ops->queue_rq)
  2372. return -EINVAL;
  2373. if (!set->ops->get_budget ^ !set->ops->put_budget)
  2374. return -EINVAL;
  2375. if (set->queue_depth > BLK_MQ_MAX_DEPTH) {
  2376. pr_info("blk-mq: reduced tag depth to %u\n",
  2377. BLK_MQ_MAX_DEPTH);
  2378. set->queue_depth = BLK_MQ_MAX_DEPTH;
  2379. }
  2380. /*
  2381. * If a crashdump is active, then we are potentially in a very
  2382. * memory constrained environment. Limit us to 1 queue and
  2383. * 64 tags to prevent using too much memory.
  2384. */
  2385. if (is_kdump_kernel()) {
  2386. set->nr_hw_queues = 1;
  2387. set->queue_depth = min(64U, set->queue_depth);
  2388. }
  2389. /*
  2390. * There is no use for more h/w queues than cpus.
  2391. */
  2392. if (set->nr_hw_queues > nr_cpu_ids)
  2393. set->nr_hw_queues = nr_cpu_ids;
  2394. set->tags = kcalloc_node(nr_cpu_ids, sizeof(struct blk_mq_tags *),
  2395. GFP_KERNEL, set->numa_node);
  2396. if (!set->tags)
  2397. return -ENOMEM;
  2398. ret = -ENOMEM;
  2399. set->mq_map = kcalloc_node(nr_cpu_ids, sizeof(*set->mq_map),
  2400. GFP_KERNEL, set->numa_node);
  2401. if (!set->mq_map)
  2402. goto out_free_tags;
  2403. ret = blk_mq_update_queue_map(set);
  2404. if (ret)
  2405. goto out_free_mq_map;
  2406. ret = blk_mq_alloc_rq_maps(set);
  2407. if (ret)
  2408. goto out_free_mq_map;
  2409. mutex_init(&set->tag_list_lock);
  2410. INIT_LIST_HEAD(&set->tag_list);
  2411. return 0;
  2412. out_free_mq_map:
  2413. kfree(set->mq_map);
  2414. set->mq_map = NULL;
  2415. out_free_tags:
  2416. kfree(set->tags);
  2417. set->tags = NULL;
  2418. return ret;
  2419. }
  2420. EXPORT_SYMBOL(blk_mq_alloc_tag_set);
  2421. void blk_mq_free_tag_set(struct blk_mq_tag_set *set)
  2422. {
  2423. int i;
  2424. for (i = 0; i < nr_cpu_ids; i++)
  2425. blk_mq_free_map_and_requests(set, i);
  2426. kfree(set->mq_map);
  2427. set->mq_map = NULL;
  2428. kfree(set->tags);
  2429. set->tags = NULL;
  2430. }
  2431. EXPORT_SYMBOL(blk_mq_free_tag_set);
  2432. int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr)
  2433. {
  2434. struct blk_mq_tag_set *set = q->tag_set;
  2435. struct blk_mq_hw_ctx *hctx;
  2436. int i, ret;
  2437. if (!set)
  2438. return -EINVAL;
  2439. blk_mq_freeze_queue(q);
  2440. blk_mq_quiesce_queue(q);
  2441. ret = 0;
  2442. queue_for_each_hw_ctx(q, hctx, i) {
  2443. if (!hctx->tags)
  2444. continue;
  2445. /*
  2446. * If we're using an MQ scheduler, just update the scheduler
  2447. * queue depth. This is similar to what the old code would do.
  2448. */
  2449. if (!hctx->sched_tags) {
  2450. ret = blk_mq_tag_update_depth(hctx, &hctx->tags, nr,
  2451. false);
  2452. } else {
  2453. ret = blk_mq_tag_update_depth(hctx, &hctx->sched_tags,
  2454. nr, true);
  2455. }
  2456. if (ret)
  2457. break;
  2458. }
  2459. if (!ret)
  2460. q->nr_requests = nr;
  2461. blk_mq_unquiesce_queue(q);
  2462. blk_mq_unfreeze_queue(q);
  2463. return ret;
  2464. }
  2465. /*
  2466. * request_queue and elevator_type pair.
  2467. * It is just used by __blk_mq_update_nr_hw_queues to cache
  2468. * the elevator_type associated with a request_queue.
  2469. */
  2470. struct blk_mq_qe_pair {
  2471. struct list_head node;
  2472. struct request_queue *q;
  2473. struct elevator_type *type;
  2474. };
  2475. /*
  2476. * Cache the elevator_type in qe pair list and switch the
  2477. * io scheduler to 'none'
  2478. */
  2479. static bool blk_mq_elv_switch_none(struct list_head *head,
  2480. struct request_queue *q)
  2481. {
  2482. struct blk_mq_qe_pair *qe;
  2483. if (!q->elevator)
  2484. return true;
  2485. qe = kmalloc(sizeof(*qe), GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY);
  2486. if (!qe)
  2487. return false;
  2488. INIT_LIST_HEAD(&qe->node);
  2489. qe->q = q;
  2490. qe->type = q->elevator->type;
  2491. list_add(&qe->node, head);
  2492. mutex_lock(&q->sysfs_lock);
  2493. /*
  2494. * After elevator_switch_mq, the previous elevator_queue will be
  2495. * released by elevator_release. The reference of the io scheduler
  2496. * module get by elevator_get will also be put. So we need to get
  2497. * a reference of the io scheduler module here to prevent it to be
  2498. * removed.
  2499. */
  2500. __module_get(qe->type->elevator_owner);
  2501. elevator_switch_mq(q, NULL);
  2502. mutex_unlock(&q->sysfs_lock);
  2503. return true;
  2504. }
  2505. static void blk_mq_elv_switch_back(struct list_head *head,
  2506. struct request_queue *q)
  2507. {
  2508. struct blk_mq_qe_pair *qe;
  2509. struct elevator_type *t = NULL;
  2510. list_for_each_entry(qe, head, node)
  2511. if (qe->q == q) {
  2512. t = qe->type;
  2513. break;
  2514. }
  2515. if (!t)
  2516. return;
  2517. list_del(&qe->node);
  2518. kfree(qe);
  2519. mutex_lock(&q->sysfs_lock);
  2520. elevator_switch_mq(q, t);
  2521. mutex_unlock(&q->sysfs_lock);
  2522. }
  2523. static void __blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set,
  2524. int nr_hw_queues)
  2525. {
  2526. struct request_queue *q;
  2527. LIST_HEAD(head);
  2528. int prev_nr_hw_queues;
  2529. lockdep_assert_held(&set->tag_list_lock);
  2530. if (nr_hw_queues > nr_cpu_ids)
  2531. nr_hw_queues = nr_cpu_ids;
  2532. if (nr_hw_queues < 1 || nr_hw_queues == set->nr_hw_queues)
  2533. return;
  2534. list_for_each_entry(q, &set->tag_list, tag_set_list)
  2535. blk_mq_freeze_queue(q);
  2536. /*
  2537. * Sync with blk_mq_queue_tag_busy_iter.
  2538. */
  2539. synchronize_rcu();
  2540. /*
  2541. * Switch IO scheduler to 'none', cleaning up the data associated
  2542. * with the previous scheduler. We will switch back once we are done
  2543. * updating the new sw to hw queue mappings.
  2544. */
  2545. list_for_each_entry(q, &set->tag_list, tag_set_list)
  2546. if (!blk_mq_elv_switch_none(&head, q))
  2547. goto switch_back;
  2548. list_for_each_entry(q, &set->tag_list, tag_set_list) {
  2549. blk_mq_debugfs_unregister_hctxs(q);
  2550. blk_mq_sysfs_unregister(q);
  2551. }
  2552. prev_nr_hw_queues = set->nr_hw_queues;
  2553. set->nr_hw_queues = nr_hw_queues;
  2554. blk_mq_update_queue_map(set);
  2555. fallback:
  2556. list_for_each_entry(q, &set->tag_list, tag_set_list) {
  2557. blk_mq_realloc_hw_ctxs(set, q);
  2558. if (q->nr_hw_queues != set->nr_hw_queues) {
  2559. pr_warn("Increasing nr_hw_queues to %d fails, fallback to %d\n",
  2560. nr_hw_queues, prev_nr_hw_queues);
  2561. set->nr_hw_queues = prev_nr_hw_queues;
  2562. blk_mq_map_queues(set);
  2563. goto fallback;
  2564. }
  2565. blk_mq_map_swqueue(q);
  2566. }
  2567. list_for_each_entry(q, &set->tag_list, tag_set_list) {
  2568. blk_mq_sysfs_register(q);
  2569. blk_mq_debugfs_register_hctxs(q);
  2570. }
  2571. switch_back:
  2572. list_for_each_entry(q, &set->tag_list, tag_set_list)
  2573. blk_mq_elv_switch_back(&head, q);
  2574. list_for_each_entry(q, &set->tag_list, tag_set_list)
  2575. blk_mq_unfreeze_queue(q);
  2576. }
  2577. void blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set, int nr_hw_queues)
  2578. {
  2579. mutex_lock(&set->tag_list_lock);
  2580. __blk_mq_update_nr_hw_queues(set, nr_hw_queues);
  2581. mutex_unlock(&set->tag_list_lock);
  2582. }
  2583. EXPORT_SYMBOL_GPL(blk_mq_update_nr_hw_queues);
  2584. /* Enable polling stats and return whether they were already enabled. */
  2585. static bool blk_poll_stats_enable(struct request_queue *q)
  2586. {
  2587. if (test_bit(QUEUE_FLAG_POLL_STATS, &q->queue_flags) ||
  2588. blk_queue_flag_test_and_set(QUEUE_FLAG_POLL_STATS, q))
  2589. return true;
  2590. blk_stat_add_callback(q, q->poll_cb);
  2591. return false;
  2592. }
  2593. static void blk_mq_poll_stats_start(struct request_queue *q)
  2594. {
  2595. /*
  2596. * We don't arm the callback if polling stats are not enabled or the
  2597. * callback is already active.
  2598. */
  2599. if (!test_bit(QUEUE_FLAG_POLL_STATS, &q->queue_flags) ||
  2600. blk_stat_is_active(q->poll_cb))
  2601. return;
  2602. blk_stat_activate_msecs(q->poll_cb, 100);
  2603. }
  2604. static void blk_mq_poll_stats_fn(struct blk_stat_callback *cb)
  2605. {
  2606. struct request_queue *q = cb->data;
  2607. int bucket;
  2608. for (bucket = 0; bucket < BLK_MQ_POLL_STATS_BKTS; bucket++) {
  2609. if (cb->stat[bucket].nr_samples)
  2610. q->poll_stat[bucket] = cb->stat[bucket];
  2611. }
  2612. }
  2613. static unsigned long blk_mq_poll_nsecs(struct request_queue *q,
  2614. struct blk_mq_hw_ctx *hctx,
  2615. struct request *rq)
  2616. {
  2617. unsigned long ret = 0;
  2618. int bucket;
  2619. /*
  2620. * If stats collection isn't on, don't sleep but turn it on for
  2621. * future users
  2622. */
  2623. if (!blk_poll_stats_enable(q))
  2624. return 0;
  2625. /*
  2626. * As an optimistic guess, use half of the mean service time
  2627. * for this type of request. We can (and should) make this smarter.
  2628. * For instance, if the completion latencies are tight, we can
  2629. * get closer than just half the mean. This is especially
  2630. * important on devices where the completion latencies are longer
  2631. * than ~10 usec. We do use the stats for the relevant IO size
  2632. * if available which does lead to better estimates.
  2633. */
  2634. bucket = blk_mq_poll_stats_bkt(rq);
  2635. if (bucket < 0)
  2636. return ret;
  2637. if (q->poll_stat[bucket].nr_samples)
  2638. ret = (q->poll_stat[bucket].mean + 1) / 2;
  2639. return ret;
  2640. }
  2641. static bool blk_mq_poll_hybrid_sleep(struct request_queue *q,
  2642. struct blk_mq_hw_ctx *hctx,
  2643. struct request *rq)
  2644. {
  2645. struct hrtimer_sleeper hs;
  2646. enum hrtimer_mode mode;
  2647. unsigned int nsecs;
  2648. ktime_t kt;
  2649. if (rq->rq_flags & RQF_MQ_POLL_SLEPT)
  2650. return false;
  2651. /*
  2652. * poll_nsec can be:
  2653. *
  2654. * -1: don't ever hybrid sleep
  2655. * 0: use half of prev avg
  2656. * >0: use this specific value
  2657. */
  2658. if (q->poll_nsec == -1)
  2659. return false;
  2660. else if (q->poll_nsec > 0)
  2661. nsecs = q->poll_nsec;
  2662. else
  2663. nsecs = blk_mq_poll_nsecs(q, hctx, rq);
  2664. if (!nsecs)
  2665. return false;
  2666. rq->rq_flags |= RQF_MQ_POLL_SLEPT;
  2667. /*
  2668. * This will be replaced with the stats tracking code, using
  2669. * 'avg_completion_time / 2' as the pre-sleep target.
  2670. */
  2671. kt = nsecs;
  2672. mode = HRTIMER_MODE_REL;
  2673. hrtimer_init_on_stack(&hs.timer, CLOCK_MONOTONIC, mode);
  2674. hrtimer_set_expires(&hs.timer, kt);
  2675. hrtimer_init_sleeper(&hs, current);
  2676. do {
  2677. if (blk_mq_rq_state(rq) == MQ_RQ_COMPLETE)
  2678. break;
  2679. set_current_state(TASK_UNINTERRUPTIBLE);
  2680. hrtimer_start_expires(&hs.timer, mode);
  2681. if (hs.task)
  2682. io_schedule();
  2683. hrtimer_cancel(&hs.timer);
  2684. mode = HRTIMER_MODE_ABS;
  2685. } while (hs.task && !signal_pending(current));
  2686. __set_current_state(TASK_RUNNING);
  2687. destroy_hrtimer_on_stack(&hs.timer);
  2688. return true;
  2689. }
  2690. static bool __blk_mq_poll(struct blk_mq_hw_ctx *hctx, struct request *rq)
  2691. {
  2692. struct request_queue *q = hctx->queue;
  2693. long state;
  2694. /*
  2695. * If we sleep, have the caller restart the poll loop to reset
  2696. * the state. Like for the other success return cases, the
  2697. * caller is responsible for checking if the IO completed. If
  2698. * the IO isn't complete, we'll get called again and will go
  2699. * straight to the busy poll loop.
  2700. */
  2701. if (blk_mq_poll_hybrid_sleep(q, hctx, rq))
  2702. return true;
  2703. hctx->poll_considered++;
  2704. state = current->state;
  2705. while (!need_resched()) {
  2706. int ret;
  2707. hctx->poll_invoked++;
  2708. ret = q->mq_ops->poll(hctx, rq->tag);
  2709. if (ret > 0) {
  2710. hctx->poll_success++;
  2711. set_current_state(TASK_RUNNING);
  2712. return true;
  2713. }
  2714. if (signal_pending_state(state, current))
  2715. set_current_state(TASK_RUNNING);
  2716. if (current->state == TASK_RUNNING)
  2717. return true;
  2718. if (ret < 0)
  2719. break;
  2720. cpu_relax();
  2721. }
  2722. __set_current_state(TASK_RUNNING);
  2723. return false;
  2724. }
  2725. static bool blk_mq_poll(struct request_queue *q, blk_qc_t cookie)
  2726. {
  2727. struct blk_mq_hw_ctx *hctx;
  2728. struct request *rq;
  2729. if (!test_bit(QUEUE_FLAG_POLL, &q->queue_flags))
  2730. return false;
  2731. hctx = q->queue_hw_ctx[blk_qc_t_to_queue_num(cookie)];
  2732. if (!blk_qc_t_is_internal(cookie))
  2733. rq = blk_mq_tag_to_rq(hctx->tags, blk_qc_t_to_tag(cookie));
  2734. else {
  2735. rq = blk_mq_tag_to_rq(hctx->sched_tags, blk_qc_t_to_tag(cookie));
  2736. /*
  2737. * With scheduling, if the request has completed, we'll
  2738. * get a NULL return here, as we clear the sched tag when
  2739. * that happens. The request still remains valid, like always,
  2740. * so we should be safe with just the NULL check.
  2741. */
  2742. if (!rq)
  2743. return false;
  2744. }
  2745. return __blk_mq_poll(hctx, rq);
  2746. }
  2747. static int __init blk_mq_init(void)
  2748. {
  2749. cpuhp_setup_state_multi(CPUHP_BLK_MQ_DEAD, "block/mq:dead", NULL,
  2750. blk_mq_hctx_notify_dead);
  2751. return 0;
  2752. }
  2753. subsys_initcall(blk_mq_init);