blk-mq.c 78 KB

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