blk-mq.c 129 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Block multiqueue core code
  4. *
  5. * Copyright (C) 2013-2014 Jens Axboe
  6. * Copyright (C) 2013-2014 Christoph Hellwig
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/backing-dev.h>
  11. #include <linux/bio.h>
  12. #include <linux/blkdev.h>
  13. #include <linux/blk-integrity.h>
  14. #include <linux/kmemleak.h>
  15. #include <linux/mm.h>
  16. #include <linux/init.h>
  17. #include <linux/slab.h>
  18. #include <linux/workqueue.h>
  19. #include <linux/smp.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/llist.h>
  22. #include <linux/cpu.h>
  23. #include <linux/cache.h>
  24. #include <linux/sched/topology.h>
  25. #include <linux/sched/signal.h>
  26. #include <linux/delay.h>
  27. #include <linux/crash_dump.h>
  28. #include <linux/prefetch.h>
  29. #include <linux/blk-crypto.h>
  30. #include <linux/part_stat.h>
  31. #include <linux/sched/isolation.h>
  32. #include <trace/events/block.h>
  33. #include <linux/t10-pi.h>
  34. #include "blk.h"
  35. #include "blk-mq.h"
  36. #include "blk-mq-debugfs.h"
  37. #include "blk-pm.h"
  38. #include "blk-stat.h"
  39. #include "blk-mq-sched.h"
  40. #include "blk-rq-qos.h"
  41. static DEFINE_PER_CPU(struct llist_head, blk_cpu_done);
  42. static DEFINE_PER_CPU(call_single_data_t, blk_cpu_csd);
  43. static DEFINE_MUTEX(blk_mq_cpuhp_lock);
  44. static void blk_mq_insert_request(struct request *rq, blk_insert_t flags);
  45. static void blk_mq_request_bypass_insert(struct request *rq,
  46. blk_insert_t flags);
  47. static void blk_mq_try_issue_list_directly(struct blk_mq_hw_ctx *hctx,
  48. struct list_head *list);
  49. static int blk_hctx_poll(struct request_queue *q, struct blk_mq_hw_ctx *hctx,
  50. struct io_comp_batch *iob, unsigned int flags);
  51. /*
  52. * Check if any of the ctx, dispatch list or elevator
  53. * 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. const int bit = ctx->index_hw[hctx->type];
  68. if (!sbitmap_test_bit(&hctx->ctx_map, bit))
  69. sbitmap_set_bit(&hctx->ctx_map, bit);
  70. }
  71. static void blk_mq_hctx_clear_pending(struct blk_mq_hw_ctx *hctx,
  72. struct blk_mq_ctx *ctx)
  73. {
  74. const int bit = ctx->index_hw[hctx->type];
  75. sbitmap_clear_bit(&hctx->ctx_map, bit);
  76. }
  77. struct mq_inflight {
  78. struct block_device *part;
  79. unsigned int inflight[2];
  80. };
  81. static bool blk_mq_check_inflight(struct request *rq, void *priv)
  82. {
  83. struct mq_inflight *mi = priv;
  84. if (rq->part && blk_do_io_stat(rq) &&
  85. (!bdev_is_partition(mi->part) || rq->part == mi->part) &&
  86. blk_mq_rq_state(rq) == MQ_RQ_IN_FLIGHT)
  87. mi->inflight[rq_data_dir(rq)]++;
  88. return true;
  89. }
  90. unsigned int blk_mq_in_flight(struct request_queue *q,
  91. struct block_device *part)
  92. {
  93. struct mq_inflight mi = { .part = part };
  94. blk_mq_queue_tag_busy_iter(q, blk_mq_check_inflight, &mi);
  95. return mi.inflight[0] + mi.inflight[1];
  96. }
  97. void blk_mq_in_flight_rw(struct request_queue *q, struct block_device *part,
  98. unsigned int inflight[2])
  99. {
  100. struct mq_inflight mi = { .part = part };
  101. blk_mq_queue_tag_busy_iter(q, blk_mq_check_inflight, &mi);
  102. inflight[0] = mi.inflight[0];
  103. inflight[1] = mi.inflight[1];
  104. }
  105. #ifdef CONFIG_LOCKDEP
  106. static bool blk_freeze_set_owner(struct request_queue *q,
  107. struct task_struct *owner)
  108. {
  109. if (!owner)
  110. return false;
  111. if (!q->mq_freeze_depth) {
  112. q->mq_freeze_owner = owner;
  113. q->mq_freeze_owner_depth = 1;
  114. return true;
  115. }
  116. if (owner == q->mq_freeze_owner)
  117. q->mq_freeze_owner_depth += 1;
  118. return false;
  119. }
  120. /* verify the last unfreeze in owner context */
  121. static bool blk_unfreeze_check_owner(struct request_queue *q)
  122. {
  123. if (!q->mq_freeze_owner)
  124. return false;
  125. if (q->mq_freeze_owner != current)
  126. return false;
  127. if (--q->mq_freeze_owner_depth == 0) {
  128. q->mq_freeze_owner = NULL;
  129. return true;
  130. }
  131. return false;
  132. }
  133. #else
  134. static bool blk_freeze_set_owner(struct request_queue *q,
  135. struct task_struct *owner)
  136. {
  137. return false;
  138. }
  139. static bool blk_unfreeze_check_owner(struct request_queue *q)
  140. {
  141. return false;
  142. }
  143. #endif
  144. bool __blk_freeze_queue_start(struct request_queue *q,
  145. struct task_struct *owner)
  146. {
  147. bool freeze;
  148. mutex_lock(&q->mq_freeze_lock);
  149. freeze = blk_freeze_set_owner(q, owner);
  150. if (++q->mq_freeze_depth == 1) {
  151. percpu_ref_kill(&q->q_usage_counter);
  152. mutex_unlock(&q->mq_freeze_lock);
  153. if (queue_is_mq(q))
  154. blk_mq_run_hw_queues(q, false);
  155. } else {
  156. mutex_unlock(&q->mq_freeze_lock);
  157. }
  158. return freeze;
  159. }
  160. void blk_freeze_queue_start(struct request_queue *q)
  161. {
  162. if (__blk_freeze_queue_start(q, current))
  163. blk_freeze_acquire_lock(q, false, false);
  164. }
  165. EXPORT_SYMBOL_GPL(blk_freeze_queue_start);
  166. void blk_mq_freeze_queue_wait(struct request_queue *q)
  167. {
  168. wait_event(q->mq_freeze_wq, percpu_ref_is_zero(&q->q_usage_counter));
  169. }
  170. EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_wait);
  171. int blk_mq_freeze_queue_wait_timeout(struct request_queue *q,
  172. unsigned long timeout)
  173. {
  174. return wait_event_timeout(q->mq_freeze_wq,
  175. percpu_ref_is_zero(&q->q_usage_counter),
  176. timeout);
  177. }
  178. EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_wait_timeout);
  179. /*
  180. * Guarantee no request is in use, so we can change any data structure of
  181. * the queue afterward.
  182. */
  183. void blk_freeze_queue(struct request_queue *q)
  184. {
  185. /*
  186. * In the !blk_mq case we are only calling this to kill the
  187. * q_usage_counter, otherwise this increases the freeze depth
  188. * and waits for it to return to zero. For this reason there is
  189. * no blk_unfreeze_queue(), and blk_freeze_queue() is not
  190. * exported to drivers as the only user for unfreeze is blk_mq.
  191. */
  192. blk_freeze_queue_start(q);
  193. blk_mq_freeze_queue_wait(q);
  194. }
  195. void blk_mq_freeze_queue(struct request_queue *q)
  196. {
  197. /*
  198. * ...just an alias to keep freeze and unfreeze actions balanced
  199. * in the blk_mq_* namespace
  200. */
  201. blk_freeze_queue(q);
  202. }
  203. EXPORT_SYMBOL_GPL(blk_mq_freeze_queue);
  204. bool __blk_mq_unfreeze_queue(struct request_queue *q, bool force_atomic)
  205. {
  206. bool unfreeze;
  207. mutex_lock(&q->mq_freeze_lock);
  208. if (force_atomic)
  209. q->q_usage_counter.data->force_atomic = true;
  210. q->mq_freeze_depth--;
  211. WARN_ON_ONCE(q->mq_freeze_depth < 0);
  212. if (!q->mq_freeze_depth) {
  213. percpu_ref_resurrect(&q->q_usage_counter);
  214. wake_up_all(&q->mq_freeze_wq);
  215. }
  216. unfreeze = blk_unfreeze_check_owner(q);
  217. mutex_unlock(&q->mq_freeze_lock);
  218. return unfreeze;
  219. }
  220. void blk_mq_unfreeze_queue(struct request_queue *q)
  221. {
  222. if (__blk_mq_unfreeze_queue(q, false))
  223. blk_unfreeze_release_lock(q, false, false);
  224. }
  225. EXPORT_SYMBOL_GPL(blk_mq_unfreeze_queue);
  226. /*
  227. * non_owner variant of blk_freeze_queue_start
  228. *
  229. * Unlike blk_freeze_queue_start, the queue doesn't need to be unfrozen
  230. * by the same task. This is fragile and should not be used if at all
  231. * possible.
  232. */
  233. void blk_freeze_queue_start_non_owner(struct request_queue *q)
  234. {
  235. __blk_freeze_queue_start(q, NULL);
  236. }
  237. EXPORT_SYMBOL_GPL(blk_freeze_queue_start_non_owner);
  238. /* non_owner variant of blk_mq_unfreeze_queue */
  239. void blk_mq_unfreeze_queue_non_owner(struct request_queue *q)
  240. {
  241. __blk_mq_unfreeze_queue(q, false);
  242. }
  243. EXPORT_SYMBOL_GPL(blk_mq_unfreeze_queue_non_owner);
  244. /*
  245. * FIXME: replace the scsi_internal_device_*block_nowait() calls in the
  246. * mpt3sas driver such that this function can be removed.
  247. */
  248. void blk_mq_quiesce_queue_nowait(struct request_queue *q)
  249. {
  250. unsigned long flags;
  251. spin_lock_irqsave(&q->queue_lock, flags);
  252. if (!q->quiesce_depth++)
  253. blk_queue_flag_set(QUEUE_FLAG_QUIESCED, q);
  254. spin_unlock_irqrestore(&q->queue_lock, flags);
  255. }
  256. EXPORT_SYMBOL_GPL(blk_mq_quiesce_queue_nowait);
  257. /**
  258. * blk_mq_wait_quiesce_done() - wait until in-progress quiesce is done
  259. * @set: tag_set to wait on
  260. *
  261. * Note: it is driver's responsibility for making sure that quiesce has
  262. * been started on or more of the request_queues of the tag_set. This
  263. * function only waits for the quiesce on those request_queues that had
  264. * the quiesce flag set using blk_mq_quiesce_queue_nowait.
  265. */
  266. void blk_mq_wait_quiesce_done(struct blk_mq_tag_set *set)
  267. {
  268. if (set->flags & BLK_MQ_F_BLOCKING)
  269. synchronize_srcu(set->srcu);
  270. else
  271. synchronize_rcu();
  272. }
  273. EXPORT_SYMBOL_GPL(blk_mq_wait_quiesce_done);
  274. /**
  275. * blk_mq_quiesce_queue() - wait until all ongoing dispatches have finished
  276. * @q: request queue.
  277. *
  278. * Note: this function does not prevent that the struct request end_io()
  279. * callback function is invoked. Once this function is returned, we make
  280. * sure no dispatch can happen until the queue is unquiesced via
  281. * blk_mq_unquiesce_queue().
  282. */
  283. void blk_mq_quiesce_queue(struct request_queue *q)
  284. {
  285. blk_mq_quiesce_queue_nowait(q);
  286. /* nothing to wait for non-mq queues */
  287. if (queue_is_mq(q))
  288. blk_mq_wait_quiesce_done(q->tag_set);
  289. }
  290. EXPORT_SYMBOL_GPL(blk_mq_quiesce_queue);
  291. /*
  292. * blk_mq_unquiesce_queue() - counterpart of blk_mq_quiesce_queue()
  293. * @q: request queue.
  294. *
  295. * This function recovers queue into the state before quiescing
  296. * which is done by blk_mq_quiesce_queue.
  297. */
  298. void blk_mq_unquiesce_queue(struct request_queue *q)
  299. {
  300. unsigned long flags;
  301. bool run_queue = false;
  302. spin_lock_irqsave(&q->queue_lock, flags);
  303. if (WARN_ON_ONCE(q->quiesce_depth <= 0)) {
  304. ;
  305. } else if (!--q->quiesce_depth) {
  306. blk_queue_flag_clear(QUEUE_FLAG_QUIESCED, q);
  307. run_queue = true;
  308. }
  309. spin_unlock_irqrestore(&q->queue_lock, flags);
  310. /* dispatch requests which are inserted during quiescing */
  311. if (run_queue)
  312. blk_mq_run_hw_queues(q, true);
  313. }
  314. EXPORT_SYMBOL_GPL(blk_mq_unquiesce_queue);
  315. void blk_mq_quiesce_tagset(struct blk_mq_tag_set *set)
  316. {
  317. struct request_queue *q;
  318. mutex_lock(&set->tag_list_lock);
  319. list_for_each_entry(q, &set->tag_list, tag_set_list) {
  320. if (!blk_queue_skip_tagset_quiesce(q))
  321. blk_mq_quiesce_queue_nowait(q);
  322. }
  323. mutex_unlock(&set->tag_list_lock);
  324. blk_mq_wait_quiesce_done(set);
  325. }
  326. EXPORT_SYMBOL_GPL(blk_mq_quiesce_tagset);
  327. void blk_mq_unquiesce_tagset(struct blk_mq_tag_set *set)
  328. {
  329. struct request_queue *q;
  330. mutex_lock(&set->tag_list_lock);
  331. list_for_each_entry(q, &set->tag_list, tag_set_list) {
  332. if (!blk_queue_skip_tagset_quiesce(q))
  333. blk_mq_unquiesce_queue(q);
  334. }
  335. mutex_unlock(&set->tag_list_lock);
  336. }
  337. EXPORT_SYMBOL_GPL(blk_mq_unquiesce_tagset);
  338. void blk_mq_wake_waiters(struct request_queue *q)
  339. {
  340. struct blk_mq_hw_ctx *hctx;
  341. unsigned long i;
  342. queue_for_each_hw_ctx(q, hctx, i)
  343. if (blk_mq_hw_queue_mapped(hctx))
  344. blk_mq_tag_wakeup_all(hctx->tags, true);
  345. }
  346. void blk_rq_init(struct request_queue *q, struct request *rq)
  347. {
  348. memset(rq, 0, sizeof(*rq));
  349. INIT_LIST_HEAD(&rq->queuelist);
  350. rq->q = q;
  351. rq->__sector = (sector_t) -1;
  352. INIT_HLIST_NODE(&rq->hash);
  353. RB_CLEAR_NODE(&rq->rb_node);
  354. rq->tag = BLK_MQ_NO_TAG;
  355. rq->internal_tag = BLK_MQ_NO_TAG;
  356. rq->start_time_ns = blk_time_get_ns();
  357. rq->part = NULL;
  358. blk_crypto_rq_set_defaults(rq);
  359. }
  360. EXPORT_SYMBOL(blk_rq_init);
  361. /* Set start and alloc time when the allocated request is actually used */
  362. static inline void blk_mq_rq_time_init(struct request *rq, u64 alloc_time_ns)
  363. {
  364. if (blk_mq_need_time_stamp(rq))
  365. rq->start_time_ns = blk_time_get_ns();
  366. else
  367. rq->start_time_ns = 0;
  368. #ifdef CONFIG_BLK_RQ_ALLOC_TIME
  369. if (blk_queue_rq_alloc_time(rq->q))
  370. rq->alloc_time_ns = alloc_time_ns ?: rq->start_time_ns;
  371. else
  372. rq->alloc_time_ns = 0;
  373. #endif
  374. }
  375. static struct request *blk_mq_rq_ctx_init(struct blk_mq_alloc_data *data,
  376. struct blk_mq_tags *tags, unsigned int tag)
  377. {
  378. struct blk_mq_ctx *ctx = data->ctx;
  379. struct blk_mq_hw_ctx *hctx = data->hctx;
  380. struct request_queue *q = data->q;
  381. struct request *rq = tags->static_rqs[tag];
  382. rq->q = q;
  383. rq->mq_ctx = ctx;
  384. rq->mq_hctx = hctx;
  385. rq->cmd_flags = data->cmd_flags;
  386. if (data->flags & BLK_MQ_REQ_PM)
  387. data->rq_flags |= RQF_PM;
  388. if (blk_queue_io_stat(q))
  389. data->rq_flags |= RQF_IO_STAT;
  390. rq->rq_flags = data->rq_flags;
  391. if (data->rq_flags & RQF_SCHED_TAGS) {
  392. rq->tag = BLK_MQ_NO_TAG;
  393. rq->internal_tag = tag;
  394. } else {
  395. rq->tag = tag;
  396. rq->internal_tag = BLK_MQ_NO_TAG;
  397. }
  398. rq->timeout = 0;
  399. rq->part = NULL;
  400. rq->io_start_time_ns = 0;
  401. rq->stats_sectors = 0;
  402. rq->nr_phys_segments = 0;
  403. rq->nr_integrity_segments = 0;
  404. rq->end_io = NULL;
  405. rq->end_io_data = NULL;
  406. blk_crypto_rq_set_defaults(rq);
  407. INIT_LIST_HEAD(&rq->queuelist);
  408. /* tag was already set */
  409. WRITE_ONCE(rq->deadline, 0);
  410. req_ref_set(rq, 1);
  411. if (rq->rq_flags & RQF_USE_SCHED) {
  412. struct elevator_queue *e = data->q->elevator;
  413. INIT_HLIST_NODE(&rq->hash);
  414. RB_CLEAR_NODE(&rq->rb_node);
  415. if (e->type->ops.prepare_request)
  416. e->type->ops.prepare_request(rq);
  417. }
  418. return rq;
  419. }
  420. static inline struct request *
  421. __blk_mq_alloc_requests_batch(struct blk_mq_alloc_data *data)
  422. {
  423. unsigned int tag, tag_offset;
  424. struct blk_mq_tags *tags;
  425. struct request *rq;
  426. unsigned long tag_mask;
  427. int i, nr = 0;
  428. tag_mask = blk_mq_get_tags(data, data->nr_tags, &tag_offset);
  429. if (unlikely(!tag_mask))
  430. return NULL;
  431. tags = blk_mq_tags_from_data(data);
  432. for (i = 0; tag_mask; i++) {
  433. if (!(tag_mask & (1UL << i)))
  434. continue;
  435. tag = tag_offset + i;
  436. prefetch(tags->static_rqs[tag]);
  437. tag_mask &= ~(1UL << i);
  438. rq = blk_mq_rq_ctx_init(data, tags, tag);
  439. rq_list_add_head(data->cached_rqs, rq);
  440. nr++;
  441. }
  442. if (!(data->rq_flags & RQF_SCHED_TAGS))
  443. blk_mq_add_active_requests(data->hctx, nr);
  444. /* caller already holds a reference, add for remainder */
  445. percpu_ref_get_many(&data->q->q_usage_counter, nr - 1);
  446. data->nr_tags -= nr;
  447. return rq_list_pop(data->cached_rqs);
  448. }
  449. static struct request *__blk_mq_alloc_requests(struct blk_mq_alloc_data *data)
  450. {
  451. struct request_queue *q = data->q;
  452. u64 alloc_time_ns = 0;
  453. struct request *rq;
  454. unsigned int tag;
  455. /* alloc_time includes depth and tag waits */
  456. if (blk_queue_rq_alloc_time(q))
  457. alloc_time_ns = blk_time_get_ns();
  458. if (data->cmd_flags & REQ_NOWAIT)
  459. data->flags |= BLK_MQ_REQ_NOWAIT;
  460. retry:
  461. data->ctx = blk_mq_get_ctx(q);
  462. data->hctx = blk_mq_map_queue(q, data->cmd_flags, data->ctx);
  463. if (q->elevator) {
  464. /*
  465. * All requests use scheduler tags when an I/O scheduler is
  466. * enabled for the queue.
  467. */
  468. data->rq_flags |= RQF_SCHED_TAGS;
  469. /*
  470. * Flush/passthrough requests are special and go directly to the
  471. * dispatch list.
  472. */
  473. if ((data->cmd_flags & REQ_OP_MASK) != REQ_OP_FLUSH &&
  474. !blk_op_is_passthrough(data->cmd_flags)) {
  475. struct elevator_mq_ops *ops = &q->elevator->type->ops;
  476. WARN_ON_ONCE(data->flags & BLK_MQ_REQ_RESERVED);
  477. data->rq_flags |= RQF_USE_SCHED;
  478. if (ops->limit_depth)
  479. ops->limit_depth(data->cmd_flags, data);
  480. }
  481. } else {
  482. blk_mq_tag_busy(data->hctx);
  483. }
  484. if (data->flags & BLK_MQ_REQ_RESERVED)
  485. data->rq_flags |= RQF_RESV;
  486. /*
  487. * Try batched alloc if we want more than 1 tag.
  488. */
  489. if (data->nr_tags > 1) {
  490. rq = __blk_mq_alloc_requests_batch(data);
  491. if (rq) {
  492. blk_mq_rq_time_init(rq, alloc_time_ns);
  493. return rq;
  494. }
  495. data->nr_tags = 1;
  496. }
  497. /*
  498. * Waiting allocations only fail because of an inactive hctx. In that
  499. * case just retry the hctx assignment and tag allocation as CPU hotplug
  500. * should have migrated us to an online CPU by now.
  501. */
  502. tag = blk_mq_get_tag(data);
  503. if (tag == BLK_MQ_NO_TAG) {
  504. if (data->flags & BLK_MQ_REQ_NOWAIT)
  505. return NULL;
  506. /*
  507. * Give up the CPU and sleep for a random short time to
  508. * ensure that thread using a realtime scheduling class
  509. * are migrated off the CPU, and thus off the hctx that
  510. * is going away.
  511. */
  512. msleep(3);
  513. goto retry;
  514. }
  515. if (!(data->rq_flags & RQF_SCHED_TAGS))
  516. blk_mq_inc_active_requests(data->hctx);
  517. rq = blk_mq_rq_ctx_init(data, blk_mq_tags_from_data(data), tag);
  518. blk_mq_rq_time_init(rq, alloc_time_ns);
  519. return rq;
  520. }
  521. static struct request *blk_mq_rq_cache_fill(struct request_queue *q,
  522. struct blk_plug *plug,
  523. blk_opf_t opf,
  524. blk_mq_req_flags_t flags)
  525. {
  526. struct blk_mq_alloc_data data = {
  527. .q = q,
  528. .flags = flags,
  529. .cmd_flags = opf,
  530. .nr_tags = plug->nr_ios,
  531. .cached_rqs = &plug->cached_rqs,
  532. };
  533. struct request *rq;
  534. if (blk_queue_enter(q, flags))
  535. return NULL;
  536. plug->nr_ios = 1;
  537. rq = __blk_mq_alloc_requests(&data);
  538. if (unlikely(!rq))
  539. blk_queue_exit(q);
  540. return rq;
  541. }
  542. static struct request *blk_mq_alloc_cached_request(struct request_queue *q,
  543. blk_opf_t opf,
  544. blk_mq_req_flags_t flags)
  545. {
  546. struct blk_plug *plug = current->plug;
  547. struct request *rq;
  548. if (!plug)
  549. return NULL;
  550. if (rq_list_empty(&plug->cached_rqs)) {
  551. if (plug->nr_ios == 1)
  552. return NULL;
  553. rq = blk_mq_rq_cache_fill(q, plug, opf, flags);
  554. if (!rq)
  555. return NULL;
  556. } else {
  557. rq = rq_list_peek(&plug->cached_rqs);
  558. if (!rq || rq->q != q)
  559. return NULL;
  560. if (blk_mq_get_hctx_type(opf) != rq->mq_hctx->type)
  561. return NULL;
  562. if (op_is_flush(rq->cmd_flags) != op_is_flush(opf))
  563. return NULL;
  564. rq_list_pop(&plug->cached_rqs);
  565. blk_mq_rq_time_init(rq, 0);
  566. }
  567. rq->cmd_flags = opf;
  568. INIT_LIST_HEAD(&rq->queuelist);
  569. return rq;
  570. }
  571. struct request *blk_mq_alloc_request(struct request_queue *q, blk_opf_t opf,
  572. blk_mq_req_flags_t flags)
  573. {
  574. struct request *rq;
  575. rq = blk_mq_alloc_cached_request(q, opf, flags);
  576. if (!rq) {
  577. struct blk_mq_alloc_data data = {
  578. .q = q,
  579. .flags = flags,
  580. .cmd_flags = opf,
  581. .nr_tags = 1,
  582. };
  583. int ret;
  584. ret = blk_queue_enter(q, flags);
  585. if (ret)
  586. return ERR_PTR(ret);
  587. rq = __blk_mq_alloc_requests(&data);
  588. if (!rq)
  589. goto out_queue_exit;
  590. }
  591. rq->__data_len = 0;
  592. rq->__sector = (sector_t) -1;
  593. rq->bio = rq->biotail = NULL;
  594. return rq;
  595. out_queue_exit:
  596. blk_queue_exit(q);
  597. return ERR_PTR(-EWOULDBLOCK);
  598. }
  599. EXPORT_SYMBOL(blk_mq_alloc_request);
  600. struct request *blk_mq_alloc_request_hctx(struct request_queue *q,
  601. blk_opf_t opf, blk_mq_req_flags_t flags, unsigned int hctx_idx)
  602. {
  603. struct blk_mq_alloc_data data = {
  604. .q = q,
  605. .flags = flags,
  606. .cmd_flags = opf,
  607. .nr_tags = 1,
  608. };
  609. u64 alloc_time_ns = 0;
  610. struct request *rq;
  611. unsigned int cpu;
  612. unsigned int tag;
  613. int ret;
  614. /* alloc_time includes depth and tag waits */
  615. if (blk_queue_rq_alloc_time(q))
  616. alloc_time_ns = blk_time_get_ns();
  617. /*
  618. * If the tag allocator sleeps we could get an allocation for a
  619. * different hardware context. No need to complicate the low level
  620. * allocator for this for the rare use case of a command tied to
  621. * a specific queue.
  622. */
  623. if (WARN_ON_ONCE(!(flags & BLK_MQ_REQ_NOWAIT)) ||
  624. WARN_ON_ONCE(!(flags & BLK_MQ_REQ_RESERVED)))
  625. return ERR_PTR(-EINVAL);
  626. if (hctx_idx >= q->nr_hw_queues)
  627. return ERR_PTR(-EIO);
  628. ret = blk_queue_enter(q, flags);
  629. if (ret)
  630. return ERR_PTR(ret);
  631. /*
  632. * Check if the hardware context is actually mapped to anything.
  633. * If not tell the caller that it should skip this queue.
  634. */
  635. ret = -EXDEV;
  636. data.hctx = xa_load(&q->hctx_table, hctx_idx);
  637. if (!blk_mq_hw_queue_mapped(data.hctx))
  638. goto out_queue_exit;
  639. cpu = cpumask_first_and(data.hctx->cpumask, cpu_online_mask);
  640. if (cpu >= nr_cpu_ids)
  641. goto out_queue_exit;
  642. data.ctx = __blk_mq_get_ctx(q, cpu);
  643. if (q->elevator)
  644. data.rq_flags |= RQF_SCHED_TAGS;
  645. else
  646. blk_mq_tag_busy(data.hctx);
  647. if (flags & BLK_MQ_REQ_RESERVED)
  648. data.rq_flags |= RQF_RESV;
  649. ret = -EWOULDBLOCK;
  650. tag = blk_mq_get_tag(&data);
  651. if (tag == BLK_MQ_NO_TAG)
  652. goto out_queue_exit;
  653. if (!(data.rq_flags & RQF_SCHED_TAGS))
  654. blk_mq_inc_active_requests(data.hctx);
  655. rq = blk_mq_rq_ctx_init(&data, blk_mq_tags_from_data(&data), tag);
  656. blk_mq_rq_time_init(rq, alloc_time_ns);
  657. rq->__data_len = 0;
  658. rq->__sector = (sector_t) -1;
  659. rq->bio = rq->biotail = NULL;
  660. return rq;
  661. out_queue_exit:
  662. blk_queue_exit(q);
  663. return ERR_PTR(ret);
  664. }
  665. EXPORT_SYMBOL_GPL(blk_mq_alloc_request_hctx);
  666. static void blk_mq_finish_request(struct request *rq)
  667. {
  668. struct request_queue *q = rq->q;
  669. blk_zone_finish_request(rq);
  670. if (rq->rq_flags & RQF_USE_SCHED) {
  671. q->elevator->type->ops.finish_request(rq);
  672. /*
  673. * For postflush request that may need to be
  674. * completed twice, we should clear this flag
  675. * to avoid double finish_request() on the rq.
  676. */
  677. rq->rq_flags &= ~RQF_USE_SCHED;
  678. }
  679. }
  680. static void __blk_mq_free_request(struct request *rq)
  681. {
  682. struct request_queue *q = rq->q;
  683. struct blk_mq_ctx *ctx = rq->mq_ctx;
  684. struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
  685. const int sched_tag = rq->internal_tag;
  686. blk_crypto_free_request(rq);
  687. blk_pm_mark_last_busy(rq);
  688. rq->mq_hctx = NULL;
  689. if (rq->tag != BLK_MQ_NO_TAG) {
  690. blk_mq_dec_active_requests(hctx);
  691. blk_mq_put_tag(hctx->tags, ctx, rq->tag);
  692. }
  693. if (sched_tag != BLK_MQ_NO_TAG)
  694. blk_mq_put_tag(hctx->sched_tags, ctx, sched_tag);
  695. blk_mq_sched_restart(hctx);
  696. blk_queue_exit(q);
  697. }
  698. void blk_mq_free_request(struct request *rq)
  699. {
  700. struct request_queue *q = rq->q;
  701. blk_mq_finish_request(rq);
  702. if (unlikely(laptop_mode && !blk_rq_is_passthrough(rq)))
  703. laptop_io_completion(q->disk->bdi);
  704. rq_qos_done(q, rq);
  705. WRITE_ONCE(rq->state, MQ_RQ_IDLE);
  706. if (req_ref_put_and_test(rq))
  707. __blk_mq_free_request(rq);
  708. }
  709. EXPORT_SYMBOL_GPL(blk_mq_free_request);
  710. void blk_mq_free_plug_rqs(struct blk_plug *plug)
  711. {
  712. struct request *rq;
  713. while ((rq = rq_list_pop(&plug->cached_rqs)) != NULL)
  714. blk_mq_free_request(rq);
  715. }
  716. void blk_dump_rq_flags(struct request *rq, char *msg)
  717. {
  718. printk(KERN_INFO "%s: dev %s: flags=%llx\n", msg,
  719. rq->q->disk ? rq->q->disk->disk_name : "?",
  720. (__force unsigned long long) rq->cmd_flags);
  721. printk(KERN_INFO " sector %llu, nr/cnr %u/%u\n",
  722. (unsigned long long)blk_rq_pos(rq),
  723. blk_rq_sectors(rq), blk_rq_cur_sectors(rq));
  724. printk(KERN_INFO " bio %p, biotail %p, len %u\n",
  725. rq->bio, rq->biotail, blk_rq_bytes(rq));
  726. }
  727. EXPORT_SYMBOL(blk_dump_rq_flags);
  728. static void blk_account_io_completion(struct request *req, unsigned int bytes)
  729. {
  730. if (req->part && blk_do_io_stat(req)) {
  731. const int sgrp = op_stat_group(req_op(req));
  732. part_stat_lock();
  733. part_stat_add(req->part, sectors[sgrp], bytes >> 9);
  734. part_stat_unlock();
  735. }
  736. }
  737. static void blk_print_req_error(struct request *req, blk_status_t status)
  738. {
  739. printk_ratelimited(KERN_ERR
  740. "%s error, dev %s, sector %llu op 0x%x:(%s) flags 0x%x "
  741. "phys_seg %u prio class %u\n",
  742. blk_status_to_str(status),
  743. req->q->disk ? req->q->disk->disk_name : "?",
  744. blk_rq_pos(req), (__force u32)req_op(req),
  745. blk_op_str(req_op(req)),
  746. (__force u32)(req->cmd_flags & ~REQ_OP_MASK),
  747. req->nr_phys_segments,
  748. IOPRIO_PRIO_CLASS(req_get_ioprio(req)));
  749. }
  750. /*
  751. * Fully end IO on a request. Does not support partial completions, or
  752. * errors.
  753. */
  754. static void blk_complete_request(struct request *req)
  755. {
  756. const bool is_flush = (req->rq_flags & RQF_FLUSH_SEQ) != 0;
  757. int total_bytes = blk_rq_bytes(req);
  758. struct bio *bio = req->bio;
  759. trace_block_rq_complete(req, BLK_STS_OK, total_bytes);
  760. if (!bio)
  761. return;
  762. if (blk_integrity_rq(req) && req_op(req) == REQ_OP_READ)
  763. blk_integrity_complete(req, total_bytes);
  764. /*
  765. * Upper layers may call blk_crypto_evict_key() anytime after the last
  766. * bio_endio(). Therefore, the keyslot must be released before that.
  767. */
  768. blk_crypto_rq_put_keyslot(req);
  769. blk_account_io_completion(req, total_bytes);
  770. do {
  771. struct bio *next = bio->bi_next;
  772. /* Completion has already been traced */
  773. bio_clear_flag(bio, BIO_TRACE_COMPLETION);
  774. blk_zone_update_request_bio(req, bio);
  775. if (!is_flush)
  776. bio_endio(bio);
  777. bio = next;
  778. } while (bio);
  779. /*
  780. * Reset counters so that the request stacking driver
  781. * can find how many bytes remain in the request
  782. * later.
  783. */
  784. if (!req->end_io) {
  785. req->bio = NULL;
  786. req->__data_len = 0;
  787. }
  788. }
  789. /**
  790. * blk_update_request - Complete multiple bytes without completing the request
  791. * @req: the request being processed
  792. * @error: block status code
  793. * @nr_bytes: number of bytes to complete for @req
  794. *
  795. * Description:
  796. * Ends I/O on a number of bytes attached to @req, but doesn't complete
  797. * the request structure even if @req doesn't have leftover.
  798. * If @req has leftover, sets it up for the next range of segments.
  799. *
  800. * Passing the result of blk_rq_bytes() as @nr_bytes guarantees
  801. * %false return from this function.
  802. *
  803. * Note:
  804. * The RQF_SPECIAL_PAYLOAD flag is ignored on purpose in this function
  805. * except in the consistency check at the end of this function.
  806. *
  807. * Return:
  808. * %false - this request doesn't have any more data
  809. * %true - this request has more data
  810. **/
  811. bool blk_update_request(struct request *req, blk_status_t error,
  812. unsigned int nr_bytes)
  813. {
  814. bool is_flush = req->rq_flags & RQF_FLUSH_SEQ;
  815. bool quiet = req->rq_flags & RQF_QUIET;
  816. int total_bytes;
  817. trace_block_rq_complete(req, error, nr_bytes);
  818. if (!req->bio)
  819. return false;
  820. if (blk_integrity_rq(req) && req_op(req) == REQ_OP_READ &&
  821. error == BLK_STS_OK)
  822. blk_integrity_complete(req, nr_bytes);
  823. /*
  824. * Upper layers may call blk_crypto_evict_key() anytime after the last
  825. * bio_endio(). Therefore, the keyslot must be released before that.
  826. */
  827. if (blk_crypto_rq_has_keyslot(req) && nr_bytes >= blk_rq_bytes(req))
  828. __blk_crypto_rq_put_keyslot(req);
  829. if (unlikely(error && !blk_rq_is_passthrough(req) && !quiet) &&
  830. !test_bit(GD_DEAD, &req->q->disk->state)) {
  831. blk_print_req_error(req, error);
  832. trace_block_rq_error(req, error, nr_bytes);
  833. }
  834. blk_account_io_completion(req, nr_bytes);
  835. total_bytes = 0;
  836. while (req->bio) {
  837. struct bio *bio = req->bio;
  838. unsigned bio_bytes = min(bio->bi_iter.bi_size, nr_bytes);
  839. if (unlikely(error))
  840. bio->bi_status = error;
  841. if (bio_bytes == bio->bi_iter.bi_size) {
  842. req->bio = bio->bi_next;
  843. } else if (bio_is_zone_append(bio) && error == BLK_STS_OK) {
  844. /*
  845. * Partial zone append completions cannot be supported
  846. * as the BIO fragments may end up not being written
  847. * sequentially.
  848. */
  849. bio->bi_status = BLK_STS_IOERR;
  850. }
  851. /* Completion has already been traced */
  852. bio_clear_flag(bio, BIO_TRACE_COMPLETION);
  853. if (unlikely(quiet))
  854. bio_set_flag(bio, BIO_QUIET);
  855. bio_advance(bio, bio_bytes);
  856. /* Don't actually finish bio if it's part of flush sequence */
  857. if (!bio->bi_iter.bi_size) {
  858. blk_zone_update_request_bio(req, bio);
  859. if (!is_flush)
  860. bio_endio(bio);
  861. }
  862. total_bytes += bio_bytes;
  863. nr_bytes -= bio_bytes;
  864. if (!nr_bytes)
  865. break;
  866. }
  867. /*
  868. * completely done
  869. */
  870. if (!req->bio) {
  871. /*
  872. * Reset counters so that the request stacking driver
  873. * can find how many bytes remain in the request
  874. * later.
  875. */
  876. req->__data_len = 0;
  877. return false;
  878. }
  879. req->__data_len -= total_bytes;
  880. /* update sector only for requests with clear definition of sector */
  881. if (!blk_rq_is_passthrough(req))
  882. req->__sector += total_bytes >> 9;
  883. /* mixed attributes always follow the first bio */
  884. if (req->rq_flags & RQF_MIXED_MERGE) {
  885. req->cmd_flags &= ~REQ_FAILFAST_MASK;
  886. req->cmd_flags |= req->bio->bi_opf & REQ_FAILFAST_MASK;
  887. }
  888. if (!(req->rq_flags & RQF_SPECIAL_PAYLOAD)) {
  889. /*
  890. * If total number of sectors is less than the first segment
  891. * size, something has gone terribly wrong.
  892. */
  893. if (blk_rq_bytes(req) < blk_rq_cur_bytes(req)) {
  894. blk_dump_rq_flags(req, "request botched");
  895. req->__data_len = blk_rq_cur_bytes(req);
  896. }
  897. /* recalculate the number of segments */
  898. req->nr_phys_segments = blk_recalc_rq_segments(req);
  899. }
  900. return true;
  901. }
  902. EXPORT_SYMBOL_GPL(blk_update_request);
  903. static inline void blk_account_io_done(struct request *req, u64 now)
  904. {
  905. trace_block_io_done(req);
  906. /*
  907. * Account IO completion. flush_rq isn't accounted as a
  908. * normal IO on queueing nor completion. Accounting the
  909. * containing request is enough.
  910. */
  911. if (blk_do_io_stat(req) && req->part &&
  912. !(req->rq_flags & RQF_FLUSH_SEQ)) {
  913. const int sgrp = op_stat_group(req_op(req));
  914. part_stat_lock();
  915. update_io_ticks(req->part, jiffies, true);
  916. part_stat_inc(req->part, ios[sgrp]);
  917. part_stat_add(req->part, nsecs[sgrp], now - req->start_time_ns);
  918. part_stat_local_dec(req->part,
  919. in_flight[op_is_write(req_op(req))]);
  920. part_stat_unlock();
  921. }
  922. }
  923. static inline void blk_account_io_start(struct request *req)
  924. {
  925. trace_block_io_start(req);
  926. if (blk_do_io_stat(req)) {
  927. /*
  928. * All non-passthrough requests are created from a bio with one
  929. * exception: when a flush command that is part of a flush sequence
  930. * generated by the state machine in blk-flush.c is cloned onto the
  931. * lower device by dm-multipath we can get here without a bio.
  932. */
  933. if (req->bio)
  934. req->part = req->bio->bi_bdev;
  935. else
  936. req->part = req->q->disk->part0;
  937. part_stat_lock();
  938. update_io_ticks(req->part, jiffies, false);
  939. part_stat_local_inc(req->part,
  940. in_flight[op_is_write(req_op(req))]);
  941. part_stat_unlock();
  942. }
  943. }
  944. static inline void __blk_mq_end_request_acct(struct request *rq, u64 now)
  945. {
  946. if (rq->rq_flags & RQF_STATS)
  947. blk_stat_add(rq, now);
  948. blk_mq_sched_completed_request(rq, now);
  949. blk_account_io_done(rq, now);
  950. }
  951. inline void __blk_mq_end_request(struct request *rq, blk_status_t error)
  952. {
  953. if (blk_mq_need_time_stamp(rq))
  954. __blk_mq_end_request_acct(rq, blk_time_get_ns());
  955. blk_mq_finish_request(rq);
  956. if (rq->end_io) {
  957. rq_qos_done(rq->q, rq);
  958. if (rq->end_io(rq, error) == RQ_END_IO_FREE)
  959. blk_mq_free_request(rq);
  960. } else {
  961. blk_mq_free_request(rq);
  962. }
  963. }
  964. EXPORT_SYMBOL(__blk_mq_end_request);
  965. void blk_mq_end_request(struct request *rq, blk_status_t error)
  966. {
  967. if (blk_update_request(rq, error, blk_rq_bytes(rq)))
  968. BUG();
  969. __blk_mq_end_request(rq, error);
  970. }
  971. EXPORT_SYMBOL(blk_mq_end_request);
  972. #define TAG_COMP_BATCH 32
  973. static inline void blk_mq_flush_tag_batch(struct blk_mq_hw_ctx *hctx,
  974. int *tag_array, int nr_tags)
  975. {
  976. struct request_queue *q = hctx->queue;
  977. blk_mq_sub_active_requests(hctx, nr_tags);
  978. blk_mq_put_tags(hctx->tags, tag_array, nr_tags);
  979. percpu_ref_put_many(&q->q_usage_counter, nr_tags);
  980. }
  981. void blk_mq_end_request_batch(struct io_comp_batch *iob)
  982. {
  983. int tags[TAG_COMP_BATCH], nr_tags = 0;
  984. struct blk_mq_hw_ctx *cur_hctx = NULL;
  985. struct request *rq;
  986. u64 now = 0;
  987. if (iob->need_ts)
  988. now = blk_time_get_ns();
  989. while ((rq = rq_list_pop(&iob->req_list)) != NULL) {
  990. prefetch(rq->bio);
  991. prefetch(rq->rq_next);
  992. blk_complete_request(rq);
  993. if (iob->need_ts)
  994. __blk_mq_end_request_acct(rq, now);
  995. blk_mq_finish_request(rq);
  996. rq_qos_done(rq->q, rq);
  997. /*
  998. * If end_io handler returns NONE, then it still has
  999. * ownership of the request.
  1000. */
  1001. if (rq->end_io && rq->end_io(rq, 0) == RQ_END_IO_NONE)
  1002. continue;
  1003. WRITE_ONCE(rq->state, MQ_RQ_IDLE);
  1004. if (!req_ref_put_and_test(rq))
  1005. continue;
  1006. blk_crypto_free_request(rq);
  1007. blk_pm_mark_last_busy(rq);
  1008. if (nr_tags == TAG_COMP_BATCH || cur_hctx != rq->mq_hctx) {
  1009. if (cur_hctx)
  1010. blk_mq_flush_tag_batch(cur_hctx, tags, nr_tags);
  1011. nr_tags = 0;
  1012. cur_hctx = rq->mq_hctx;
  1013. }
  1014. tags[nr_tags++] = rq->tag;
  1015. }
  1016. if (nr_tags)
  1017. blk_mq_flush_tag_batch(cur_hctx, tags, nr_tags);
  1018. }
  1019. EXPORT_SYMBOL_GPL(blk_mq_end_request_batch);
  1020. static void blk_complete_reqs(struct llist_head *list)
  1021. {
  1022. struct llist_node *entry = llist_reverse_order(llist_del_all(list));
  1023. struct request *rq, *next;
  1024. llist_for_each_entry_safe(rq, next, entry, ipi_list)
  1025. rq->q->mq_ops->complete(rq);
  1026. }
  1027. static __latent_entropy void blk_done_softirq(void)
  1028. {
  1029. blk_complete_reqs(this_cpu_ptr(&blk_cpu_done));
  1030. }
  1031. static int blk_softirq_cpu_dead(unsigned int cpu)
  1032. {
  1033. blk_complete_reqs(&per_cpu(blk_cpu_done, cpu));
  1034. return 0;
  1035. }
  1036. static void __blk_mq_complete_request_remote(void *data)
  1037. {
  1038. __raise_softirq_irqoff(BLOCK_SOFTIRQ);
  1039. }
  1040. static inline bool blk_mq_complete_need_ipi(struct request *rq)
  1041. {
  1042. int cpu = raw_smp_processor_id();
  1043. if (!IS_ENABLED(CONFIG_SMP) ||
  1044. !test_bit(QUEUE_FLAG_SAME_COMP, &rq->q->queue_flags))
  1045. return false;
  1046. /*
  1047. * With force threaded interrupts enabled, raising softirq from an SMP
  1048. * function call will always result in waking the ksoftirqd thread.
  1049. * This is probably worse than completing the request on a different
  1050. * cache domain.
  1051. */
  1052. if (force_irqthreads())
  1053. return false;
  1054. /* same CPU or cache domain and capacity? Complete locally */
  1055. if (cpu == rq->mq_ctx->cpu ||
  1056. (!test_bit(QUEUE_FLAG_SAME_FORCE, &rq->q->queue_flags) &&
  1057. cpus_share_cache(cpu, rq->mq_ctx->cpu) &&
  1058. cpus_equal_capacity(cpu, rq->mq_ctx->cpu)))
  1059. return false;
  1060. /* don't try to IPI to an offline CPU */
  1061. return cpu_online(rq->mq_ctx->cpu);
  1062. }
  1063. static void blk_mq_complete_send_ipi(struct request *rq)
  1064. {
  1065. unsigned int cpu;
  1066. cpu = rq->mq_ctx->cpu;
  1067. if (llist_add(&rq->ipi_list, &per_cpu(blk_cpu_done, cpu)))
  1068. smp_call_function_single_async(cpu, &per_cpu(blk_cpu_csd, cpu));
  1069. }
  1070. static void blk_mq_raise_softirq(struct request *rq)
  1071. {
  1072. struct llist_head *list;
  1073. preempt_disable();
  1074. list = this_cpu_ptr(&blk_cpu_done);
  1075. if (llist_add(&rq->ipi_list, list))
  1076. raise_softirq(BLOCK_SOFTIRQ);
  1077. preempt_enable();
  1078. }
  1079. bool blk_mq_complete_request_remote(struct request *rq)
  1080. {
  1081. WRITE_ONCE(rq->state, MQ_RQ_COMPLETE);
  1082. /*
  1083. * For request which hctx has only one ctx mapping,
  1084. * or a polled request, always complete locally,
  1085. * it's pointless to redirect the completion.
  1086. */
  1087. if ((rq->mq_hctx->nr_ctx == 1 &&
  1088. rq->mq_ctx->cpu == raw_smp_processor_id()) ||
  1089. rq->cmd_flags & REQ_POLLED)
  1090. return false;
  1091. if (blk_mq_complete_need_ipi(rq)) {
  1092. blk_mq_complete_send_ipi(rq);
  1093. return true;
  1094. }
  1095. if (rq->q->nr_hw_queues == 1) {
  1096. blk_mq_raise_softirq(rq);
  1097. return true;
  1098. }
  1099. return false;
  1100. }
  1101. EXPORT_SYMBOL_GPL(blk_mq_complete_request_remote);
  1102. /**
  1103. * blk_mq_complete_request - end I/O on a request
  1104. * @rq: the request being processed
  1105. *
  1106. * Description:
  1107. * Complete a request by scheduling the ->complete_rq operation.
  1108. **/
  1109. void blk_mq_complete_request(struct request *rq)
  1110. {
  1111. if (!blk_mq_complete_request_remote(rq))
  1112. rq->q->mq_ops->complete(rq);
  1113. }
  1114. EXPORT_SYMBOL(blk_mq_complete_request);
  1115. /**
  1116. * blk_mq_start_request - Start processing a request
  1117. * @rq: Pointer to request to be started
  1118. *
  1119. * Function used by device drivers to notify the block layer that a request
  1120. * is going to be processed now, so blk layer can do proper initializations
  1121. * such as starting the timeout timer.
  1122. */
  1123. void blk_mq_start_request(struct request *rq)
  1124. {
  1125. struct request_queue *q = rq->q;
  1126. trace_block_rq_issue(rq);
  1127. if (test_bit(QUEUE_FLAG_STATS, &q->queue_flags) &&
  1128. !blk_rq_is_passthrough(rq)) {
  1129. rq->io_start_time_ns = blk_time_get_ns();
  1130. rq->stats_sectors = blk_rq_sectors(rq);
  1131. rq->rq_flags |= RQF_STATS;
  1132. rq_qos_issue(q, rq);
  1133. }
  1134. WARN_ON_ONCE(blk_mq_rq_state(rq) != MQ_RQ_IDLE);
  1135. blk_add_timer(rq);
  1136. WRITE_ONCE(rq->state, MQ_RQ_IN_FLIGHT);
  1137. rq->mq_hctx->tags->rqs[rq->tag] = rq;
  1138. if (blk_integrity_rq(rq) && req_op(rq) == REQ_OP_WRITE)
  1139. blk_integrity_prepare(rq);
  1140. if (rq->bio && rq->bio->bi_opf & REQ_POLLED)
  1141. WRITE_ONCE(rq->bio->bi_cookie, rq->mq_hctx->queue_num);
  1142. }
  1143. EXPORT_SYMBOL(blk_mq_start_request);
  1144. /*
  1145. * Allow 2x BLK_MAX_REQUEST_COUNT requests on plug queue for multiple
  1146. * queues. This is important for md arrays to benefit from merging
  1147. * requests.
  1148. */
  1149. static inline unsigned short blk_plug_max_rq_count(struct blk_plug *plug)
  1150. {
  1151. if (plug->multiple_queues)
  1152. return BLK_MAX_REQUEST_COUNT * 2;
  1153. return BLK_MAX_REQUEST_COUNT;
  1154. }
  1155. static void blk_add_rq_to_plug(struct blk_plug *plug, struct request *rq)
  1156. {
  1157. struct request *last = rq_list_peek(&plug->mq_list);
  1158. if (!plug->rq_count) {
  1159. trace_block_plug(rq->q);
  1160. } else if (plug->rq_count >= blk_plug_max_rq_count(plug) ||
  1161. (!blk_queue_nomerges(rq->q) &&
  1162. blk_rq_bytes(last) >= BLK_PLUG_FLUSH_SIZE)) {
  1163. blk_mq_flush_plug_list(plug, false);
  1164. last = NULL;
  1165. trace_block_plug(rq->q);
  1166. }
  1167. if (!plug->multiple_queues && last && last->q != rq->q)
  1168. plug->multiple_queues = true;
  1169. /*
  1170. * Any request allocated from sched tags can't be issued to
  1171. * ->queue_rqs() directly
  1172. */
  1173. if (!plug->has_elevator && (rq->rq_flags & RQF_SCHED_TAGS))
  1174. plug->has_elevator = true;
  1175. rq_list_add_tail(&plug->mq_list, rq);
  1176. plug->rq_count++;
  1177. }
  1178. /**
  1179. * blk_execute_rq_nowait - insert a request to I/O scheduler for execution
  1180. * @rq: request to insert
  1181. * @at_head: insert request at head or tail of queue
  1182. *
  1183. * Description:
  1184. * Insert a fully prepared request at the back of the I/O scheduler queue
  1185. * for execution. Don't wait for completion.
  1186. *
  1187. * Note:
  1188. * This function will invoke @done directly if the queue is dead.
  1189. */
  1190. void blk_execute_rq_nowait(struct request *rq, bool at_head)
  1191. {
  1192. struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
  1193. WARN_ON(irqs_disabled());
  1194. WARN_ON(!blk_rq_is_passthrough(rq));
  1195. blk_account_io_start(rq);
  1196. if (current->plug && !at_head) {
  1197. blk_add_rq_to_plug(current->plug, rq);
  1198. return;
  1199. }
  1200. blk_mq_insert_request(rq, at_head ? BLK_MQ_INSERT_AT_HEAD : 0);
  1201. blk_mq_run_hw_queue(hctx, hctx->flags & BLK_MQ_F_BLOCKING);
  1202. }
  1203. EXPORT_SYMBOL_GPL(blk_execute_rq_nowait);
  1204. struct blk_rq_wait {
  1205. struct completion done;
  1206. blk_status_t ret;
  1207. };
  1208. static enum rq_end_io_ret blk_end_sync_rq(struct request *rq, blk_status_t ret)
  1209. {
  1210. struct blk_rq_wait *wait = rq->end_io_data;
  1211. wait->ret = ret;
  1212. complete(&wait->done);
  1213. return RQ_END_IO_NONE;
  1214. }
  1215. bool blk_rq_is_poll(struct request *rq)
  1216. {
  1217. if (!rq->mq_hctx)
  1218. return false;
  1219. if (rq->mq_hctx->type != HCTX_TYPE_POLL)
  1220. return false;
  1221. return true;
  1222. }
  1223. EXPORT_SYMBOL_GPL(blk_rq_is_poll);
  1224. static void blk_rq_poll_completion(struct request *rq, struct completion *wait)
  1225. {
  1226. do {
  1227. blk_hctx_poll(rq->q, rq->mq_hctx, NULL, 0);
  1228. cond_resched();
  1229. } while (!completion_done(wait));
  1230. }
  1231. /**
  1232. * blk_execute_rq - insert a request into queue for execution
  1233. * @rq: request to insert
  1234. * @at_head: insert request at head or tail of queue
  1235. *
  1236. * Description:
  1237. * Insert a fully prepared request at the back of the I/O scheduler queue
  1238. * for execution and wait for completion.
  1239. * Return: The blk_status_t result provided to blk_mq_end_request().
  1240. */
  1241. blk_status_t blk_execute_rq(struct request *rq, bool at_head)
  1242. {
  1243. struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
  1244. struct blk_rq_wait wait = {
  1245. .done = COMPLETION_INITIALIZER_ONSTACK(wait.done),
  1246. };
  1247. WARN_ON(irqs_disabled());
  1248. WARN_ON(!blk_rq_is_passthrough(rq));
  1249. rq->end_io_data = &wait;
  1250. rq->end_io = blk_end_sync_rq;
  1251. blk_account_io_start(rq);
  1252. blk_mq_insert_request(rq, at_head ? BLK_MQ_INSERT_AT_HEAD : 0);
  1253. blk_mq_run_hw_queue(hctx, false);
  1254. if (blk_rq_is_poll(rq))
  1255. blk_rq_poll_completion(rq, &wait.done);
  1256. else
  1257. blk_wait_io(&wait.done);
  1258. return wait.ret;
  1259. }
  1260. EXPORT_SYMBOL(blk_execute_rq);
  1261. static void __blk_mq_requeue_request(struct request *rq)
  1262. {
  1263. struct request_queue *q = rq->q;
  1264. blk_mq_put_driver_tag(rq);
  1265. trace_block_rq_requeue(rq);
  1266. rq_qos_requeue(q, rq);
  1267. if (blk_mq_request_started(rq)) {
  1268. WRITE_ONCE(rq->state, MQ_RQ_IDLE);
  1269. rq->rq_flags &= ~RQF_TIMED_OUT;
  1270. }
  1271. }
  1272. void blk_mq_requeue_request(struct request *rq, bool kick_requeue_list)
  1273. {
  1274. struct request_queue *q = rq->q;
  1275. unsigned long flags;
  1276. __blk_mq_requeue_request(rq);
  1277. /* this request will be re-inserted to io scheduler queue */
  1278. blk_mq_sched_requeue_request(rq);
  1279. spin_lock_irqsave(&q->requeue_lock, flags);
  1280. list_add_tail(&rq->queuelist, &q->requeue_list);
  1281. spin_unlock_irqrestore(&q->requeue_lock, flags);
  1282. if (kick_requeue_list)
  1283. blk_mq_kick_requeue_list(q);
  1284. }
  1285. EXPORT_SYMBOL(blk_mq_requeue_request);
  1286. static void blk_mq_requeue_work(struct work_struct *work)
  1287. {
  1288. struct request_queue *q =
  1289. container_of(work, struct request_queue, requeue_work.work);
  1290. LIST_HEAD(rq_list);
  1291. LIST_HEAD(flush_list);
  1292. struct request *rq;
  1293. spin_lock_irq(&q->requeue_lock);
  1294. list_splice_init(&q->requeue_list, &rq_list);
  1295. list_splice_init(&q->flush_list, &flush_list);
  1296. spin_unlock_irq(&q->requeue_lock);
  1297. while (!list_empty(&rq_list)) {
  1298. rq = list_entry(rq_list.next, struct request, queuelist);
  1299. /*
  1300. * If RQF_DONTPREP ist set, the request has been started by the
  1301. * driver already and might have driver-specific data allocated
  1302. * already. Insert it into the hctx dispatch list to avoid
  1303. * block layer merges for the request.
  1304. */
  1305. if (rq->rq_flags & RQF_DONTPREP) {
  1306. list_del_init(&rq->queuelist);
  1307. blk_mq_request_bypass_insert(rq, 0);
  1308. } else {
  1309. list_del_init(&rq->queuelist);
  1310. blk_mq_insert_request(rq, BLK_MQ_INSERT_AT_HEAD);
  1311. }
  1312. }
  1313. while (!list_empty(&flush_list)) {
  1314. rq = list_entry(flush_list.next, struct request, queuelist);
  1315. list_del_init(&rq->queuelist);
  1316. blk_mq_insert_request(rq, 0);
  1317. }
  1318. blk_mq_run_hw_queues(q, false);
  1319. }
  1320. void blk_mq_kick_requeue_list(struct request_queue *q)
  1321. {
  1322. kblockd_mod_delayed_work_on(WORK_CPU_UNBOUND, &q->requeue_work, 0);
  1323. }
  1324. EXPORT_SYMBOL(blk_mq_kick_requeue_list);
  1325. void blk_mq_delay_kick_requeue_list(struct request_queue *q,
  1326. unsigned long msecs)
  1327. {
  1328. kblockd_mod_delayed_work_on(WORK_CPU_UNBOUND, &q->requeue_work,
  1329. msecs_to_jiffies(msecs));
  1330. }
  1331. EXPORT_SYMBOL(blk_mq_delay_kick_requeue_list);
  1332. static bool blk_is_flush_data_rq(struct request *rq)
  1333. {
  1334. return (rq->rq_flags & RQF_FLUSH_SEQ) && !is_flush_rq(rq);
  1335. }
  1336. static bool blk_mq_rq_inflight(struct request *rq, void *priv)
  1337. {
  1338. /*
  1339. * If we find a request that isn't idle we know the queue is busy
  1340. * as it's checked in the iter.
  1341. * Return false to stop the iteration.
  1342. *
  1343. * In case of queue quiesce, if one flush data request is completed,
  1344. * don't count it as inflight given the flush sequence is suspended,
  1345. * and the original flush data request is invisible to driver, just
  1346. * like other pending requests because of quiesce
  1347. */
  1348. if (blk_mq_request_started(rq) && !(blk_queue_quiesced(rq->q) &&
  1349. blk_is_flush_data_rq(rq) &&
  1350. blk_mq_request_completed(rq))) {
  1351. bool *busy = priv;
  1352. *busy = true;
  1353. return false;
  1354. }
  1355. return true;
  1356. }
  1357. bool blk_mq_queue_inflight(struct request_queue *q)
  1358. {
  1359. bool busy = false;
  1360. blk_mq_queue_tag_busy_iter(q, blk_mq_rq_inflight, &busy);
  1361. return busy;
  1362. }
  1363. EXPORT_SYMBOL_GPL(blk_mq_queue_inflight);
  1364. static void blk_mq_rq_timed_out(struct request *req)
  1365. {
  1366. req->rq_flags |= RQF_TIMED_OUT;
  1367. if (req->q->mq_ops->timeout) {
  1368. enum blk_eh_timer_return ret;
  1369. ret = req->q->mq_ops->timeout(req);
  1370. if (ret == BLK_EH_DONE)
  1371. return;
  1372. WARN_ON_ONCE(ret != BLK_EH_RESET_TIMER);
  1373. }
  1374. blk_add_timer(req);
  1375. }
  1376. struct blk_expired_data {
  1377. bool has_timedout_rq;
  1378. unsigned long next;
  1379. unsigned long timeout_start;
  1380. };
  1381. static bool blk_mq_req_expired(struct request *rq, struct blk_expired_data *expired)
  1382. {
  1383. unsigned long deadline;
  1384. if (blk_mq_rq_state(rq) != MQ_RQ_IN_FLIGHT)
  1385. return false;
  1386. if (rq->rq_flags & RQF_TIMED_OUT)
  1387. return false;
  1388. deadline = READ_ONCE(rq->deadline);
  1389. if (time_after_eq(expired->timeout_start, deadline))
  1390. return true;
  1391. if (expired->next == 0)
  1392. expired->next = deadline;
  1393. else if (time_after(expired->next, deadline))
  1394. expired->next = deadline;
  1395. return false;
  1396. }
  1397. void blk_mq_put_rq_ref(struct request *rq)
  1398. {
  1399. if (is_flush_rq(rq)) {
  1400. if (rq->end_io(rq, 0) == RQ_END_IO_FREE)
  1401. blk_mq_free_request(rq);
  1402. } else if (req_ref_put_and_test(rq)) {
  1403. __blk_mq_free_request(rq);
  1404. }
  1405. }
  1406. static bool blk_mq_check_expired(struct request *rq, void *priv)
  1407. {
  1408. struct blk_expired_data *expired = priv;
  1409. /*
  1410. * blk_mq_queue_tag_busy_iter() has locked the request, so it cannot
  1411. * be reallocated underneath the timeout handler's processing, then
  1412. * the expire check is reliable. If the request is not expired, then
  1413. * it was completed and reallocated as a new request after returning
  1414. * from blk_mq_check_expired().
  1415. */
  1416. if (blk_mq_req_expired(rq, expired)) {
  1417. expired->has_timedout_rq = true;
  1418. return false;
  1419. }
  1420. return true;
  1421. }
  1422. static bool blk_mq_handle_expired(struct request *rq, void *priv)
  1423. {
  1424. struct blk_expired_data *expired = priv;
  1425. if (blk_mq_req_expired(rq, expired))
  1426. blk_mq_rq_timed_out(rq);
  1427. return true;
  1428. }
  1429. static void blk_mq_timeout_work(struct work_struct *work)
  1430. {
  1431. struct request_queue *q =
  1432. container_of(work, struct request_queue, timeout_work);
  1433. struct blk_expired_data expired = {
  1434. .timeout_start = jiffies,
  1435. };
  1436. struct blk_mq_hw_ctx *hctx;
  1437. unsigned long i;
  1438. /* A deadlock might occur if a request is stuck requiring a
  1439. * timeout at the same time a queue freeze is waiting
  1440. * completion, since the timeout code would not be able to
  1441. * acquire the queue reference here.
  1442. *
  1443. * That's why we don't use blk_queue_enter here; instead, we use
  1444. * percpu_ref_tryget directly, because we need to be able to
  1445. * obtain a reference even in the short window between the queue
  1446. * starting to freeze, by dropping the first reference in
  1447. * blk_freeze_queue_start, and the moment the last request is
  1448. * consumed, marked by the instant q_usage_counter reaches
  1449. * zero.
  1450. */
  1451. if (!percpu_ref_tryget(&q->q_usage_counter))
  1452. return;
  1453. /* check if there is any timed-out request */
  1454. blk_mq_queue_tag_busy_iter(q, blk_mq_check_expired, &expired);
  1455. if (expired.has_timedout_rq) {
  1456. /*
  1457. * Before walking tags, we must ensure any submit started
  1458. * before the current time has finished. Since the submit
  1459. * uses srcu or rcu, wait for a synchronization point to
  1460. * ensure all running submits have finished
  1461. */
  1462. blk_mq_wait_quiesce_done(q->tag_set);
  1463. expired.next = 0;
  1464. blk_mq_queue_tag_busy_iter(q, blk_mq_handle_expired, &expired);
  1465. }
  1466. if (expired.next != 0) {
  1467. mod_timer(&q->timeout, expired.next);
  1468. } else {
  1469. /*
  1470. * Request timeouts are handled as a forward rolling timer. If
  1471. * we end up here it means that no requests are pending and
  1472. * also that no request has been pending for a while. Mark
  1473. * each hctx as idle.
  1474. */
  1475. queue_for_each_hw_ctx(q, hctx, i) {
  1476. /* the hctx may be unmapped, so check it here */
  1477. if (blk_mq_hw_queue_mapped(hctx))
  1478. blk_mq_tag_idle(hctx);
  1479. }
  1480. }
  1481. blk_queue_exit(q);
  1482. }
  1483. struct flush_busy_ctx_data {
  1484. struct blk_mq_hw_ctx *hctx;
  1485. struct list_head *list;
  1486. };
  1487. static bool flush_busy_ctx(struct sbitmap *sb, unsigned int bitnr, void *data)
  1488. {
  1489. struct flush_busy_ctx_data *flush_data = data;
  1490. struct blk_mq_hw_ctx *hctx = flush_data->hctx;
  1491. struct blk_mq_ctx *ctx = hctx->ctxs[bitnr];
  1492. enum hctx_type type = hctx->type;
  1493. spin_lock(&ctx->lock);
  1494. list_splice_tail_init(&ctx->rq_lists[type], flush_data->list);
  1495. sbitmap_clear_bit(sb, bitnr);
  1496. spin_unlock(&ctx->lock);
  1497. return true;
  1498. }
  1499. /*
  1500. * Process software queues that have been marked busy, splicing them
  1501. * to the for-dispatch
  1502. */
  1503. void blk_mq_flush_busy_ctxs(struct blk_mq_hw_ctx *hctx, struct list_head *list)
  1504. {
  1505. struct flush_busy_ctx_data data = {
  1506. .hctx = hctx,
  1507. .list = list,
  1508. };
  1509. sbitmap_for_each_set(&hctx->ctx_map, flush_busy_ctx, &data);
  1510. }
  1511. EXPORT_SYMBOL_GPL(blk_mq_flush_busy_ctxs);
  1512. struct dispatch_rq_data {
  1513. struct blk_mq_hw_ctx *hctx;
  1514. struct request *rq;
  1515. };
  1516. static bool dispatch_rq_from_ctx(struct sbitmap *sb, unsigned int bitnr,
  1517. void *data)
  1518. {
  1519. struct dispatch_rq_data *dispatch_data = data;
  1520. struct blk_mq_hw_ctx *hctx = dispatch_data->hctx;
  1521. struct blk_mq_ctx *ctx = hctx->ctxs[bitnr];
  1522. enum hctx_type type = hctx->type;
  1523. spin_lock(&ctx->lock);
  1524. if (!list_empty(&ctx->rq_lists[type])) {
  1525. dispatch_data->rq = list_entry_rq(ctx->rq_lists[type].next);
  1526. list_del_init(&dispatch_data->rq->queuelist);
  1527. if (list_empty(&ctx->rq_lists[type]))
  1528. sbitmap_clear_bit(sb, bitnr);
  1529. }
  1530. spin_unlock(&ctx->lock);
  1531. return !dispatch_data->rq;
  1532. }
  1533. struct request *blk_mq_dequeue_from_ctx(struct blk_mq_hw_ctx *hctx,
  1534. struct blk_mq_ctx *start)
  1535. {
  1536. unsigned off = start ? start->index_hw[hctx->type] : 0;
  1537. struct dispatch_rq_data data = {
  1538. .hctx = hctx,
  1539. .rq = NULL,
  1540. };
  1541. __sbitmap_for_each_set(&hctx->ctx_map, off,
  1542. dispatch_rq_from_ctx, &data);
  1543. return data.rq;
  1544. }
  1545. bool __blk_mq_alloc_driver_tag(struct request *rq)
  1546. {
  1547. struct sbitmap_queue *bt = &rq->mq_hctx->tags->bitmap_tags;
  1548. unsigned int tag_offset = rq->mq_hctx->tags->nr_reserved_tags;
  1549. int tag;
  1550. blk_mq_tag_busy(rq->mq_hctx);
  1551. if (blk_mq_tag_is_reserved(rq->mq_hctx->sched_tags, rq->internal_tag)) {
  1552. bt = &rq->mq_hctx->tags->breserved_tags;
  1553. tag_offset = 0;
  1554. } else {
  1555. if (!hctx_may_queue(rq->mq_hctx, bt))
  1556. return false;
  1557. }
  1558. tag = __sbitmap_queue_get(bt);
  1559. if (tag == BLK_MQ_NO_TAG)
  1560. return false;
  1561. rq->tag = tag + tag_offset;
  1562. blk_mq_inc_active_requests(rq->mq_hctx);
  1563. return true;
  1564. }
  1565. static int blk_mq_dispatch_wake(wait_queue_entry_t *wait, unsigned mode,
  1566. int flags, void *key)
  1567. {
  1568. struct blk_mq_hw_ctx *hctx;
  1569. hctx = container_of(wait, struct blk_mq_hw_ctx, dispatch_wait);
  1570. spin_lock(&hctx->dispatch_wait_lock);
  1571. if (!list_empty(&wait->entry)) {
  1572. struct sbitmap_queue *sbq;
  1573. list_del_init(&wait->entry);
  1574. sbq = &hctx->tags->bitmap_tags;
  1575. atomic_dec(&sbq->ws_active);
  1576. }
  1577. spin_unlock(&hctx->dispatch_wait_lock);
  1578. blk_mq_run_hw_queue(hctx, true);
  1579. return 1;
  1580. }
  1581. /*
  1582. * Mark us waiting for a tag. For shared tags, this involves hooking us into
  1583. * the tag wakeups. For non-shared tags, we can simply mark us needing a
  1584. * restart. For both cases, take care to check the condition again after
  1585. * marking us as waiting.
  1586. */
  1587. static bool blk_mq_mark_tag_wait(struct blk_mq_hw_ctx *hctx,
  1588. struct request *rq)
  1589. {
  1590. struct sbitmap_queue *sbq;
  1591. struct wait_queue_head *wq;
  1592. wait_queue_entry_t *wait;
  1593. bool ret;
  1594. if (!(hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED) &&
  1595. !(blk_mq_is_shared_tags(hctx->flags))) {
  1596. blk_mq_sched_mark_restart_hctx(hctx);
  1597. /*
  1598. * It's possible that a tag was freed in the window between the
  1599. * allocation failure and adding the hardware queue to the wait
  1600. * queue.
  1601. *
  1602. * Don't clear RESTART here, someone else could have set it.
  1603. * At most this will cost an extra queue run.
  1604. */
  1605. return blk_mq_get_driver_tag(rq);
  1606. }
  1607. wait = &hctx->dispatch_wait;
  1608. if (!list_empty_careful(&wait->entry))
  1609. return false;
  1610. if (blk_mq_tag_is_reserved(rq->mq_hctx->sched_tags, rq->internal_tag))
  1611. sbq = &hctx->tags->breserved_tags;
  1612. else
  1613. sbq = &hctx->tags->bitmap_tags;
  1614. wq = &bt_wait_ptr(sbq, hctx)->wait;
  1615. spin_lock_irq(&wq->lock);
  1616. spin_lock(&hctx->dispatch_wait_lock);
  1617. if (!list_empty(&wait->entry)) {
  1618. spin_unlock(&hctx->dispatch_wait_lock);
  1619. spin_unlock_irq(&wq->lock);
  1620. return false;
  1621. }
  1622. atomic_inc(&sbq->ws_active);
  1623. wait->flags &= ~WQ_FLAG_EXCLUSIVE;
  1624. __add_wait_queue(wq, wait);
  1625. /*
  1626. * Add one explicit barrier since blk_mq_get_driver_tag() may
  1627. * not imply barrier in case of failure.
  1628. *
  1629. * Order adding us to wait queue and allocating driver tag.
  1630. *
  1631. * The pair is the one implied in sbitmap_queue_wake_up() which
  1632. * orders clearing sbitmap tag bits and waitqueue_active() in
  1633. * __sbitmap_queue_wake_up(), since waitqueue_active() is lockless
  1634. *
  1635. * Otherwise, re-order of adding wait queue and getting driver tag
  1636. * may cause __sbitmap_queue_wake_up() to wake up nothing because
  1637. * the waitqueue_active() may not observe us in wait queue.
  1638. */
  1639. smp_mb();
  1640. /*
  1641. * It's possible that a tag was freed in the window between the
  1642. * allocation failure and adding the hardware queue to the wait
  1643. * queue.
  1644. */
  1645. ret = blk_mq_get_driver_tag(rq);
  1646. if (!ret) {
  1647. spin_unlock(&hctx->dispatch_wait_lock);
  1648. spin_unlock_irq(&wq->lock);
  1649. return false;
  1650. }
  1651. /*
  1652. * We got a tag, remove ourselves from the wait queue to ensure
  1653. * someone else gets the wakeup.
  1654. */
  1655. list_del_init(&wait->entry);
  1656. atomic_dec(&sbq->ws_active);
  1657. spin_unlock(&hctx->dispatch_wait_lock);
  1658. spin_unlock_irq(&wq->lock);
  1659. return true;
  1660. }
  1661. #define BLK_MQ_DISPATCH_BUSY_EWMA_WEIGHT 8
  1662. #define BLK_MQ_DISPATCH_BUSY_EWMA_FACTOR 4
  1663. /*
  1664. * Update dispatch busy with the Exponential Weighted Moving Average(EWMA):
  1665. * - EWMA is one simple way to compute running average value
  1666. * - weight(7/8 and 1/8) is applied so that it can decrease exponentially
  1667. * - take 4 as factor for avoiding to get too small(0) result, and this
  1668. * factor doesn't matter because EWMA decreases exponentially
  1669. */
  1670. static void blk_mq_update_dispatch_busy(struct blk_mq_hw_ctx *hctx, bool busy)
  1671. {
  1672. unsigned int ewma;
  1673. ewma = hctx->dispatch_busy;
  1674. if (!ewma && !busy)
  1675. return;
  1676. ewma *= BLK_MQ_DISPATCH_BUSY_EWMA_WEIGHT - 1;
  1677. if (busy)
  1678. ewma += 1 << BLK_MQ_DISPATCH_BUSY_EWMA_FACTOR;
  1679. ewma /= BLK_MQ_DISPATCH_BUSY_EWMA_WEIGHT;
  1680. hctx->dispatch_busy = ewma;
  1681. }
  1682. #define BLK_MQ_RESOURCE_DELAY 3 /* ms units */
  1683. static void blk_mq_handle_dev_resource(struct request *rq,
  1684. struct list_head *list)
  1685. {
  1686. list_add(&rq->queuelist, list);
  1687. __blk_mq_requeue_request(rq);
  1688. }
  1689. enum prep_dispatch {
  1690. PREP_DISPATCH_OK,
  1691. PREP_DISPATCH_NO_TAG,
  1692. PREP_DISPATCH_NO_BUDGET,
  1693. };
  1694. static enum prep_dispatch blk_mq_prep_dispatch_rq(struct request *rq,
  1695. bool need_budget)
  1696. {
  1697. struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
  1698. int budget_token = -1;
  1699. if (need_budget) {
  1700. budget_token = blk_mq_get_dispatch_budget(rq->q);
  1701. if (budget_token < 0) {
  1702. blk_mq_put_driver_tag(rq);
  1703. return PREP_DISPATCH_NO_BUDGET;
  1704. }
  1705. blk_mq_set_rq_budget_token(rq, budget_token);
  1706. }
  1707. if (!blk_mq_get_driver_tag(rq)) {
  1708. /*
  1709. * The initial allocation attempt failed, so we need to
  1710. * rerun the hardware queue when a tag is freed. The
  1711. * waitqueue takes care of that. If the queue is run
  1712. * before we add this entry back on the dispatch list,
  1713. * we'll re-run it below.
  1714. */
  1715. if (!blk_mq_mark_tag_wait(hctx, rq)) {
  1716. /*
  1717. * All budgets not got from this function will be put
  1718. * together during handling partial dispatch
  1719. */
  1720. if (need_budget)
  1721. blk_mq_put_dispatch_budget(rq->q, budget_token);
  1722. return PREP_DISPATCH_NO_TAG;
  1723. }
  1724. }
  1725. return PREP_DISPATCH_OK;
  1726. }
  1727. /* release all allocated budgets before calling to blk_mq_dispatch_rq_list */
  1728. static void blk_mq_release_budgets(struct request_queue *q,
  1729. struct list_head *list)
  1730. {
  1731. struct request *rq;
  1732. list_for_each_entry(rq, list, queuelist) {
  1733. int budget_token = blk_mq_get_rq_budget_token(rq);
  1734. if (budget_token >= 0)
  1735. blk_mq_put_dispatch_budget(q, budget_token);
  1736. }
  1737. }
  1738. /*
  1739. * blk_mq_commit_rqs will notify driver using bd->last that there is no
  1740. * more requests. (See comment in struct blk_mq_ops for commit_rqs for
  1741. * details)
  1742. * Attention, we should explicitly call this in unusual cases:
  1743. * 1) did not queue everything initially scheduled to queue
  1744. * 2) the last attempt to queue a request failed
  1745. */
  1746. static void blk_mq_commit_rqs(struct blk_mq_hw_ctx *hctx, int queued,
  1747. bool from_schedule)
  1748. {
  1749. if (hctx->queue->mq_ops->commit_rqs && queued) {
  1750. trace_block_unplug(hctx->queue, queued, !from_schedule);
  1751. hctx->queue->mq_ops->commit_rqs(hctx);
  1752. }
  1753. }
  1754. /*
  1755. * Returns true if we did some work AND can potentially do more.
  1756. */
  1757. bool blk_mq_dispatch_rq_list(struct blk_mq_hw_ctx *hctx, struct list_head *list,
  1758. unsigned int nr_budgets)
  1759. {
  1760. enum prep_dispatch prep;
  1761. struct request_queue *q = hctx->queue;
  1762. struct request *rq;
  1763. int queued;
  1764. blk_status_t ret = BLK_STS_OK;
  1765. bool needs_resource = false;
  1766. if (list_empty(list))
  1767. return false;
  1768. /*
  1769. * Now process all the entries, sending them to the driver.
  1770. */
  1771. queued = 0;
  1772. do {
  1773. struct blk_mq_queue_data bd;
  1774. rq = list_first_entry(list, struct request, queuelist);
  1775. WARN_ON_ONCE(hctx != rq->mq_hctx);
  1776. prep = blk_mq_prep_dispatch_rq(rq, !nr_budgets);
  1777. if (prep != PREP_DISPATCH_OK)
  1778. break;
  1779. list_del_init(&rq->queuelist);
  1780. bd.rq = rq;
  1781. bd.last = list_empty(list);
  1782. /*
  1783. * once the request is queued to lld, no need to cover the
  1784. * budget any more
  1785. */
  1786. if (nr_budgets)
  1787. nr_budgets--;
  1788. ret = q->mq_ops->queue_rq(hctx, &bd);
  1789. switch (ret) {
  1790. case BLK_STS_OK:
  1791. queued++;
  1792. break;
  1793. case BLK_STS_RESOURCE:
  1794. needs_resource = true;
  1795. fallthrough;
  1796. case BLK_STS_DEV_RESOURCE:
  1797. blk_mq_handle_dev_resource(rq, list);
  1798. goto out;
  1799. default:
  1800. blk_mq_end_request(rq, ret);
  1801. }
  1802. } while (!list_empty(list));
  1803. out:
  1804. /* If we didn't flush the entire list, we could have told the driver
  1805. * there was more coming, but that turned out to be a lie.
  1806. */
  1807. if (!list_empty(list) || ret != BLK_STS_OK)
  1808. blk_mq_commit_rqs(hctx, queued, false);
  1809. /*
  1810. * Any items that need requeuing? Stuff them into hctx->dispatch,
  1811. * that is where we will continue on next queue run.
  1812. */
  1813. if (!list_empty(list)) {
  1814. bool needs_restart;
  1815. /* For non-shared tags, the RESTART check will suffice */
  1816. bool no_tag = prep == PREP_DISPATCH_NO_TAG &&
  1817. ((hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED) ||
  1818. blk_mq_is_shared_tags(hctx->flags));
  1819. if (nr_budgets)
  1820. blk_mq_release_budgets(q, list);
  1821. spin_lock(&hctx->lock);
  1822. list_splice_tail_init(list, &hctx->dispatch);
  1823. spin_unlock(&hctx->lock);
  1824. /*
  1825. * Order adding requests to hctx->dispatch and checking
  1826. * SCHED_RESTART flag. The pair of this smp_mb() is the one
  1827. * in blk_mq_sched_restart(). Avoid restart code path to
  1828. * miss the new added requests to hctx->dispatch, meantime
  1829. * SCHED_RESTART is observed here.
  1830. */
  1831. smp_mb();
  1832. /*
  1833. * If SCHED_RESTART was set by the caller of this function and
  1834. * it is no longer set that means that it was cleared by another
  1835. * thread and hence that a queue rerun is needed.
  1836. *
  1837. * If 'no_tag' is set, that means that we failed getting
  1838. * a driver tag with an I/O scheduler attached. If our dispatch
  1839. * waitqueue is no longer active, ensure that we run the queue
  1840. * AFTER adding our entries back to the list.
  1841. *
  1842. * If no I/O scheduler has been configured it is possible that
  1843. * the hardware queue got stopped and restarted before requests
  1844. * were pushed back onto the dispatch list. Rerun the queue to
  1845. * avoid starvation. Notes:
  1846. * - blk_mq_run_hw_queue() checks whether or not a queue has
  1847. * been stopped before rerunning a queue.
  1848. * - Some but not all block drivers stop a queue before
  1849. * returning BLK_STS_RESOURCE. Two exceptions are scsi-mq
  1850. * and dm-rq.
  1851. *
  1852. * If driver returns BLK_STS_RESOURCE and SCHED_RESTART
  1853. * bit is set, run queue after a delay to avoid IO stalls
  1854. * that could otherwise occur if the queue is idle. We'll do
  1855. * similar if we couldn't get budget or couldn't lock a zone
  1856. * and SCHED_RESTART is set.
  1857. */
  1858. needs_restart = blk_mq_sched_needs_restart(hctx);
  1859. if (prep == PREP_DISPATCH_NO_BUDGET)
  1860. needs_resource = true;
  1861. if (!needs_restart ||
  1862. (no_tag && list_empty_careful(&hctx->dispatch_wait.entry)))
  1863. blk_mq_run_hw_queue(hctx, true);
  1864. else if (needs_resource)
  1865. blk_mq_delay_run_hw_queue(hctx, BLK_MQ_RESOURCE_DELAY);
  1866. blk_mq_update_dispatch_busy(hctx, true);
  1867. return false;
  1868. }
  1869. blk_mq_update_dispatch_busy(hctx, false);
  1870. return true;
  1871. }
  1872. static inline int blk_mq_first_mapped_cpu(struct blk_mq_hw_ctx *hctx)
  1873. {
  1874. int cpu = cpumask_first_and(hctx->cpumask, cpu_online_mask);
  1875. if (cpu >= nr_cpu_ids)
  1876. cpu = cpumask_first(hctx->cpumask);
  1877. return cpu;
  1878. }
  1879. /*
  1880. * ->next_cpu is always calculated from hctx->cpumask, so simply use
  1881. * it for speeding up the check
  1882. */
  1883. static bool blk_mq_hctx_empty_cpumask(struct blk_mq_hw_ctx *hctx)
  1884. {
  1885. return hctx->next_cpu >= nr_cpu_ids;
  1886. }
  1887. /*
  1888. * It'd be great if the workqueue API had a way to pass
  1889. * in a mask and had some smarts for more clever placement.
  1890. * For now we just round-robin here, switching for every
  1891. * BLK_MQ_CPU_WORK_BATCH queued items.
  1892. */
  1893. static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx)
  1894. {
  1895. bool tried = false;
  1896. int next_cpu = hctx->next_cpu;
  1897. /* Switch to unbound if no allowable CPUs in this hctx */
  1898. if (hctx->queue->nr_hw_queues == 1 || blk_mq_hctx_empty_cpumask(hctx))
  1899. return WORK_CPU_UNBOUND;
  1900. if (--hctx->next_cpu_batch <= 0) {
  1901. select_cpu:
  1902. next_cpu = cpumask_next_and(next_cpu, hctx->cpumask,
  1903. cpu_online_mask);
  1904. if (next_cpu >= nr_cpu_ids)
  1905. next_cpu = blk_mq_first_mapped_cpu(hctx);
  1906. hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
  1907. }
  1908. /*
  1909. * Do unbound schedule if we can't find a online CPU for this hctx,
  1910. * and it should only happen in the path of handling CPU DEAD.
  1911. */
  1912. if (!cpu_online(next_cpu)) {
  1913. if (!tried) {
  1914. tried = true;
  1915. goto select_cpu;
  1916. }
  1917. /*
  1918. * Make sure to re-select CPU next time once after CPUs
  1919. * in hctx->cpumask become online again.
  1920. */
  1921. hctx->next_cpu = next_cpu;
  1922. hctx->next_cpu_batch = 1;
  1923. return WORK_CPU_UNBOUND;
  1924. }
  1925. hctx->next_cpu = next_cpu;
  1926. return next_cpu;
  1927. }
  1928. /**
  1929. * blk_mq_delay_run_hw_queue - Run a hardware queue asynchronously.
  1930. * @hctx: Pointer to the hardware queue to run.
  1931. * @msecs: Milliseconds of delay to wait before running the queue.
  1932. *
  1933. * Run a hardware queue asynchronously with a delay of @msecs.
  1934. */
  1935. void blk_mq_delay_run_hw_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs)
  1936. {
  1937. if (unlikely(blk_mq_hctx_stopped(hctx)))
  1938. return;
  1939. kblockd_mod_delayed_work_on(blk_mq_hctx_next_cpu(hctx), &hctx->run_work,
  1940. msecs_to_jiffies(msecs));
  1941. }
  1942. EXPORT_SYMBOL(blk_mq_delay_run_hw_queue);
  1943. static inline bool blk_mq_hw_queue_need_run(struct blk_mq_hw_ctx *hctx)
  1944. {
  1945. bool need_run;
  1946. /*
  1947. * When queue is quiesced, we may be switching io scheduler, or
  1948. * updating nr_hw_queues, or other things, and we can't run queue
  1949. * any more, even blk_mq_hctx_has_pending() can't be called safely.
  1950. *
  1951. * And queue will be rerun in blk_mq_unquiesce_queue() if it is
  1952. * quiesced.
  1953. */
  1954. __blk_mq_run_dispatch_ops(hctx->queue, false,
  1955. need_run = !blk_queue_quiesced(hctx->queue) &&
  1956. blk_mq_hctx_has_pending(hctx));
  1957. return need_run;
  1958. }
  1959. /**
  1960. * blk_mq_run_hw_queue - Start to run a hardware queue.
  1961. * @hctx: Pointer to the hardware queue to run.
  1962. * @async: If we want to run the queue asynchronously.
  1963. *
  1964. * Check if the request queue is not in a quiesced state and if there are
  1965. * pending requests to be sent. If this is true, run the queue to send requests
  1966. * to hardware.
  1967. */
  1968. void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
  1969. {
  1970. bool need_run;
  1971. /*
  1972. * We can't run the queue inline with interrupts disabled.
  1973. */
  1974. WARN_ON_ONCE(!async && in_interrupt());
  1975. might_sleep_if(!async && hctx->flags & BLK_MQ_F_BLOCKING);
  1976. need_run = blk_mq_hw_queue_need_run(hctx);
  1977. if (!need_run) {
  1978. unsigned long flags;
  1979. /*
  1980. * Synchronize with blk_mq_unquiesce_queue(), because we check
  1981. * if hw queue is quiesced locklessly above, we need the use
  1982. * ->queue_lock to make sure we see the up-to-date status to
  1983. * not miss rerunning the hw queue.
  1984. */
  1985. spin_lock_irqsave(&hctx->queue->queue_lock, flags);
  1986. need_run = blk_mq_hw_queue_need_run(hctx);
  1987. spin_unlock_irqrestore(&hctx->queue->queue_lock, flags);
  1988. if (!need_run)
  1989. return;
  1990. }
  1991. if (async || !cpumask_test_cpu(raw_smp_processor_id(), hctx->cpumask)) {
  1992. blk_mq_delay_run_hw_queue(hctx, 0);
  1993. return;
  1994. }
  1995. blk_mq_run_dispatch_ops(hctx->queue,
  1996. blk_mq_sched_dispatch_requests(hctx));
  1997. }
  1998. EXPORT_SYMBOL(blk_mq_run_hw_queue);
  1999. /*
  2000. * Return prefered queue to dispatch from (if any) for non-mq aware IO
  2001. * scheduler.
  2002. */
  2003. static struct blk_mq_hw_ctx *blk_mq_get_sq_hctx(struct request_queue *q)
  2004. {
  2005. struct blk_mq_ctx *ctx = blk_mq_get_ctx(q);
  2006. /*
  2007. * If the IO scheduler does not respect hardware queues when
  2008. * dispatching, we just don't bother with multiple HW queues and
  2009. * dispatch from hctx for the current CPU since running multiple queues
  2010. * just causes lock contention inside the scheduler and pointless cache
  2011. * bouncing.
  2012. */
  2013. struct blk_mq_hw_ctx *hctx = ctx->hctxs[HCTX_TYPE_DEFAULT];
  2014. if (!blk_mq_hctx_stopped(hctx))
  2015. return hctx;
  2016. return NULL;
  2017. }
  2018. /**
  2019. * blk_mq_run_hw_queues - Run all hardware queues in a request queue.
  2020. * @q: Pointer to the request queue to run.
  2021. * @async: If we want to run the queue asynchronously.
  2022. */
  2023. void blk_mq_run_hw_queues(struct request_queue *q, bool async)
  2024. {
  2025. struct blk_mq_hw_ctx *hctx, *sq_hctx;
  2026. unsigned long i;
  2027. sq_hctx = NULL;
  2028. if (blk_queue_sq_sched(q))
  2029. sq_hctx = blk_mq_get_sq_hctx(q);
  2030. queue_for_each_hw_ctx(q, hctx, i) {
  2031. if (blk_mq_hctx_stopped(hctx))
  2032. continue;
  2033. /*
  2034. * Dispatch from this hctx either if there's no hctx preferred
  2035. * by IO scheduler or if it has requests that bypass the
  2036. * scheduler.
  2037. */
  2038. if (!sq_hctx || sq_hctx == hctx ||
  2039. !list_empty_careful(&hctx->dispatch))
  2040. blk_mq_run_hw_queue(hctx, async);
  2041. }
  2042. }
  2043. EXPORT_SYMBOL(blk_mq_run_hw_queues);
  2044. /**
  2045. * blk_mq_delay_run_hw_queues - Run all hardware queues asynchronously.
  2046. * @q: Pointer to the request queue to run.
  2047. * @msecs: Milliseconds of delay to wait before running the queues.
  2048. */
  2049. void blk_mq_delay_run_hw_queues(struct request_queue *q, unsigned long msecs)
  2050. {
  2051. struct blk_mq_hw_ctx *hctx, *sq_hctx;
  2052. unsigned long i;
  2053. sq_hctx = NULL;
  2054. if (blk_queue_sq_sched(q))
  2055. sq_hctx = blk_mq_get_sq_hctx(q);
  2056. queue_for_each_hw_ctx(q, hctx, i) {
  2057. if (blk_mq_hctx_stopped(hctx))
  2058. continue;
  2059. /*
  2060. * If there is already a run_work pending, leave the
  2061. * pending delay untouched. Otherwise, a hctx can stall
  2062. * if another hctx is re-delaying the other's work
  2063. * before the work executes.
  2064. */
  2065. if (delayed_work_pending(&hctx->run_work))
  2066. continue;
  2067. /*
  2068. * Dispatch from this hctx either if there's no hctx preferred
  2069. * by IO scheduler or if it has requests that bypass the
  2070. * scheduler.
  2071. */
  2072. if (!sq_hctx || sq_hctx == hctx ||
  2073. !list_empty_careful(&hctx->dispatch))
  2074. blk_mq_delay_run_hw_queue(hctx, msecs);
  2075. }
  2076. }
  2077. EXPORT_SYMBOL(blk_mq_delay_run_hw_queues);
  2078. /*
  2079. * This function is often used for pausing .queue_rq() by driver when
  2080. * there isn't enough resource or some conditions aren't satisfied, and
  2081. * BLK_STS_RESOURCE is usually returned.
  2082. *
  2083. * We do not guarantee that dispatch can be drained or blocked
  2084. * after blk_mq_stop_hw_queue() returns. Please use
  2085. * blk_mq_quiesce_queue() for that requirement.
  2086. */
  2087. void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx)
  2088. {
  2089. cancel_delayed_work(&hctx->run_work);
  2090. set_bit(BLK_MQ_S_STOPPED, &hctx->state);
  2091. }
  2092. EXPORT_SYMBOL(blk_mq_stop_hw_queue);
  2093. /*
  2094. * This function is often used for pausing .queue_rq() by driver when
  2095. * there isn't enough resource or some conditions aren't satisfied, and
  2096. * BLK_STS_RESOURCE is usually returned.
  2097. *
  2098. * We do not guarantee that dispatch can be drained or blocked
  2099. * after blk_mq_stop_hw_queues() returns. Please use
  2100. * blk_mq_quiesce_queue() for that requirement.
  2101. */
  2102. void blk_mq_stop_hw_queues(struct request_queue *q)
  2103. {
  2104. struct blk_mq_hw_ctx *hctx;
  2105. unsigned long i;
  2106. queue_for_each_hw_ctx(q, hctx, i)
  2107. blk_mq_stop_hw_queue(hctx);
  2108. }
  2109. EXPORT_SYMBOL(blk_mq_stop_hw_queues);
  2110. void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx)
  2111. {
  2112. clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
  2113. blk_mq_run_hw_queue(hctx, hctx->flags & BLK_MQ_F_BLOCKING);
  2114. }
  2115. EXPORT_SYMBOL(blk_mq_start_hw_queue);
  2116. void blk_mq_start_hw_queues(struct request_queue *q)
  2117. {
  2118. struct blk_mq_hw_ctx *hctx;
  2119. unsigned long i;
  2120. queue_for_each_hw_ctx(q, hctx, i)
  2121. blk_mq_start_hw_queue(hctx);
  2122. }
  2123. EXPORT_SYMBOL(blk_mq_start_hw_queues);
  2124. void blk_mq_start_stopped_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
  2125. {
  2126. if (!blk_mq_hctx_stopped(hctx))
  2127. return;
  2128. clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
  2129. /*
  2130. * Pairs with the smp_mb() in blk_mq_hctx_stopped() to order the
  2131. * clearing of BLK_MQ_S_STOPPED above and the checking of dispatch
  2132. * list in the subsequent routine.
  2133. */
  2134. smp_mb__after_atomic();
  2135. blk_mq_run_hw_queue(hctx, async);
  2136. }
  2137. EXPORT_SYMBOL_GPL(blk_mq_start_stopped_hw_queue);
  2138. void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async)
  2139. {
  2140. struct blk_mq_hw_ctx *hctx;
  2141. unsigned long i;
  2142. queue_for_each_hw_ctx(q, hctx, i)
  2143. blk_mq_start_stopped_hw_queue(hctx, async ||
  2144. (hctx->flags & BLK_MQ_F_BLOCKING));
  2145. }
  2146. EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues);
  2147. static void blk_mq_run_work_fn(struct work_struct *work)
  2148. {
  2149. struct blk_mq_hw_ctx *hctx =
  2150. container_of(work, struct blk_mq_hw_ctx, run_work.work);
  2151. blk_mq_run_dispatch_ops(hctx->queue,
  2152. blk_mq_sched_dispatch_requests(hctx));
  2153. }
  2154. /**
  2155. * blk_mq_request_bypass_insert - Insert a request at dispatch list.
  2156. * @rq: Pointer to request to be inserted.
  2157. * @flags: BLK_MQ_INSERT_*
  2158. *
  2159. * Should only be used carefully, when the caller knows we want to
  2160. * bypass a potential IO scheduler on the target device.
  2161. */
  2162. static void blk_mq_request_bypass_insert(struct request *rq, blk_insert_t flags)
  2163. {
  2164. struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
  2165. spin_lock(&hctx->lock);
  2166. if (flags & BLK_MQ_INSERT_AT_HEAD)
  2167. list_add(&rq->queuelist, &hctx->dispatch);
  2168. else
  2169. list_add_tail(&rq->queuelist, &hctx->dispatch);
  2170. spin_unlock(&hctx->lock);
  2171. }
  2172. static void blk_mq_insert_requests(struct blk_mq_hw_ctx *hctx,
  2173. struct blk_mq_ctx *ctx, struct list_head *list,
  2174. bool run_queue_async)
  2175. {
  2176. struct request *rq;
  2177. enum hctx_type type = hctx->type;
  2178. /*
  2179. * Try to issue requests directly if the hw queue isn't busy to save an
  2180. * extra enqueue & dequeue to the sw queue.
  2181. */
  2182. if (!hctx->dispatch_busy && !run_queue_async) {
  2183. blk_mq_run_dispatch_ops(hctx->queue,
  2184. blk_mq_try_issue_list_directly(hctx, list));
  2185. if (list_empty(list))
  2186. goto out;
  2187. }
  2188. /*
  2189. * preemption doesn't flush plug list, so it's possible ctx->cpu is
  2190. * offline now
  2191. */
  2192. list_for_each_entry(rq, list, queuelist) {
  2193. BUG_ON(rq->mq_ctx != ctx);
  2194. trace_block_rq_insert(rq);
  2195. if (rq->cmd_flags & REQ_NOWAIT)
  2196. run_queue_async = true;
  2197. }
  2198. spin_lock(&ctx->lock);
  2199. list_splice_tail_init(list, &ctx->rq_lists[type]);
  2200. blk_mq_hctx_mark_pending(hctx, ctx);
  2201. spin_unlock(&ctx->lock);
  2202. out:
  2203. blk_mq_run_hw_queue(hctx, run_queue_async);
  2204. }
  2205. static void blk_mq_insert_request(struct request *rq, blk_insert_t flags)
  2206. {
  2207. struct request_queue *q = rq->q;
  2208. struct blk_mq_ctx *ctx = rq->mq_ctx;
  2209. struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
  2210. if (blk_rq_is_passthrough(rq)) {
  2211. /*
  2212. * Passthrough request have to be added to hctx->dispatch
  2213. * directly. The device may be in a situation where it can't
  2214. * handle FS request, and always returns BLK_STS_RESOURCE for
  2215. * them, which gets them added to hctx->dispatch.
  2216. *
  2217. * If a passthrough request is required to unblock the queues,
  2218. * and it is added to the scheduler queue, there is no chance to
  2219. * dispatch it given we prioritize requests in hctx->dispatch.
  2220. */
  2221. blk_mq_request_bypass_insert(rq, flags);
  2222. } else if (req_op(rq) == REQ_OP_FLUSH) {
  2223. /*
  2224. * Firstly normal IO request is inserted to scheduler queue or
  2225. * sw queue, meantime we add flush request to dispatch queue(
  2226. * hctx->dispatch) directly and there is at most one in-flight
  2227. * flush request for each hw queue, so it doesn't matter to add
  2228. * flush request to tail or front of the dispatch queue.
  2229. *
  2230. * Secondly in case of NCQ, flush request belongs to non-NCQ
  2231. * command, and queueing it will fail when there is any
  2232. * in-flight normal IO request(NCQ command). When adding flush
  2233. * rq to the front of hctx->dispatch, it is easier to introduce
  2234. * extra time to flush rq's latency because of S_SCHED_RESTART
  2235. * compared with adding to the tail of dispatch queue, then
  2236. * chance of flush merge is increased, and less flush requests
  2237. * will be issued to controller. It is observed that ~10% time
  2238. * is saved in blktests block/004 on disk attached to AHCI/NCQ
  2239. * drive when adding flush rq to the front of hctx->dispatch.
  2240. *
  2241. * Simply queue flush rq to the front of hctx->dispatch so that
  2242. * intensive flush workloads can benefit in case of NCQ HW.
  2243. */
  2244. blk_mq_request_bypass_insert(rq, BLK_MQ_INSERT_AT_HEAD);
  2245. } else if (q->elevator) {
  2246. LIST_HEAD(list);
  2247. WARN_ON_ONCE(rq->tag != BLK_MQ_NO_TAG);
  2248. list_add(&rq->queuelist, &list);
  2249. q->elevator->type->ops.insert_requests(hctx, &list, flags);
  2250. } else {
  2251. trace_block_rq_insert(rq);
  2252. spin_lock(&ctx->lock);
  2253. if (flags & BLK_MQ_INSERT_AT_HEAD)
  2254. list_add(&rq->queuelist, &ctx->rq_lists[hctx->type]);
  2255. else
  2256. list_add_tail(&rq->queuelist,
  2257. &ctx->rq_lists[hctx->type]);
  2258. blk_mq_hctx_mark_pending(hctx, ctx);
  2259. spin_unlock(&ctx->lock);
  2260. }
  2261. }
  2262. static void blk_mq_bio_to_request(struct request *rq, struct bio *bio,
  2263. unsigned int nr_segs)
  2264. {
  2265. int err;
  2266. if (bio->bi_opf & REQ_RAHEAD)
  2267. rq->cmd_flags |= REQ_FAILFAST_MASK;
  2268. rq->__sector = bio->bi_iter.bi_sector;
  2269. blk_rq_bio_prep(rq, bio, nr_segs);
  2270. if (bio_integrity(bio))
  2271. rq->nr_integrity_segments = blk_rq_count_integrity_sg(rq->q,
  2272. bio);
  2273. /* This can't fail, since GFP_NOIO includes __GFP_DIRECT_RECLAIM. */
  2274. err = blk_crypto_rq_bio_prep(rq, bio, GFP_NOIO);
  2275. WARN_ON_ONCE(err);
  2276. blk_account_io_start(rq);
  2277. }
  2278. static blk_status_t __blk_mq_issue_directly(struct blk_mq_hw_ctx *hctx,
  2279. struct request *rq, bool last)
  2280. {
  2281. struct request_queue *q = rq->q;
  2282. struct blk_mq_queue_data bd = {
  2283. .rq = rq,
  2284. .last = last,
  2285. };
  2286. blk_status_t ret;
  2287. /*
  2288. * For OK queue, we are done. For error, caller may kill it.
  2289. * Any other error (busy), just add it to our list as we
  2290. * previously would have done.
  2291. */
  2292. ret = q->mq_ops->queue_rq(hctx, &bd);
  2293. switch (ret) {
  2294. case BLK_STS_OK:
  2295. blk_mq_update_dispatch_busy(hctx, false);
  2296. break;
  2297. case BLK_STS_RESOURCE:
  2298. case BLK_STS_DEV_RESOURCE:
  2299. blk_mq_update_dispatch_busy(hctx, true);
  2300. __blk_mq_requeue_request(rq);
  2301. break;
  2302. default:
  2303. blk_mq_update_dispatch_busy(hctx, false);
  2304. break;
  2305. }
  2306. return ret;
  2307. }
  2308. static bool blk_mq_get_budget_and_tag(struct request *rq)
  2309. {
  2310. int budget_token;
  2311. budget_token = blk_mq_get_dispatch_budget(rq->q);
  2312. if (budget_token < 0)
  2313. return false;
  2314. blk_mq_set_rq_budget_token(rq, budget_token);
  2315. if (!blk_mq_get_driver_tag(rq)) {
  2316. blk_mq_put_dispatch_budget(rq->q, budget_token);
  2317. return false;
  2318. }
  2319. return true;
  2320. }
  2321. /**
  2322. * blk_mq_try_issue_directly - Try to send a request directly to device driver.
  2323. * @hctx: Pointer of the associated hardware queue.
  2324. * @rq: Pointer to request to be sent.
  2325. *
  2326. * If the device has enough resources to accept a new request now, send the
  2327. * request directly to device driver. Else, insert at hctx->dispatch queue, so
  2328. * we can try send it another time in the future. Requests inserted at this
  2329. * queue have higher priority.
  2330. */
  2331. static void blk_mq_try_issue_directly(struct blk_mq_hw_ctx *hctx,
  2332. struct request *rq)
  2333. {
  2334. blk_status_t ret;
  2335. if (blk_mq_hctx_stopped(hctx) || blk_queue_quiesced(rq->q)) {
  2336. blk_mq_insert_request(rq, 0);
  2337. blk_mq_run_hw_queue(hctx, false);
  2338. return;
  2339. }
  2340. if ((rq->rq_flags & RQF_USE_SCHED) || !blk_mq_get_budget_and_tag(rq)) {
  2341. blk_mq_insert_request(rq, 0);
  2342. blk_mq_run_hw_queue(hctx, rq->cmd_flags & REQ_NOWAIT);
  2343. return;
  2344. }
  2345. ret = __blk_mq_issue_directly(hctx, rq, true);
  2346. switch (ret) {
  2347. case BLK_STS_OK:
  2348. break;
  2349. case BLK_STS_RESOURCE:
  2350. case BLK_STS_DEV_RESOURCE:
  2351. blk_mq_request_bypass_insert(rq, 0);
  2352. blk_mq_run_hw_queue(hctx, false);
  2353. break;
  2354. default:
  2355. blk_mq_end_request(rq, ret);
  2356. break;
  2357. }
  2358. }
  2359. static blk_status_t blk_mq_request_issue_directly(struct request *rq, bool last)
  2360. {
  2361. struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
  2362. if (blk_mq_hctx_stopped(hctx) || blk_queue_quiesced(rq->q)) {
  2363. blk_mq_insert_request(rq, 0);
  2364. blk_mq_run_hw_queue(hctx, false);
  2365. return BLK_STS_OK;
  2366. }
  2367. if (!blk_mq_get_budget_and_tag(rq))
  2368. return BLK_STS_RESOURCE;
  2369. return __blk_mq_issue_directly(hctx, rq, last);
  2370. }
  2371. static void blk_mq_plug_issue_direct(struct blk_plug *plug)
  2372. {
  2373. struct blk_mq_hw_ctx *hctx = NULL;
  2374. struct request *rq;
  2375. int queued = 0;
  2376. blk_status_t ret = BLK_STS_OK;
  2377. while ((rq = rq_list_pop(&plug->mq_list))) {
  2378. bool last = rq_list_empty(&plug->mq_list);
  2379. if (hctx != rq->mq_hctx) {
  2380. if (hctx) {
  2381. blk_mq_commit_rqs(hctx, queued, false);
  2382. queued = 0;
  2383. }
  2384. hctx = rq->mq_hctx;
  2385. }
  2386. ret = blk_mq_request_issue_directly(rq, last);
  2387. switch (ret) {
  2388. case BLK_STS_OK:
  2389. queued++;
  2390. break;
  2391. case BLK_STS_RESOURCE:
  2392. case BLK_STS_DEV_RESOURCE:
  2393. blk_mq_request_bypass_insert(rq, 0);
  2394. blk_mq_run_hw_queue(hctx, false);
  2395. goto out;
  2396. default:
  2397. blk_mq_end_request(rq, ret);
  2398. break;
  2399. }
  2400. }
  2401. out:
  2402. if (ret != BLK_STS_OK)
  2403. blk_mq_commit_rqs(hctx, queued, false);
  2404. }
  2405. static void __blk_mq_flush_plug_list(struct request_queue *q,
  2406. struct blk_plug *plug)
  2407. {
  2408. if (blk_queue_quiesced(q))
  2409. return;
  2410. q->mq_ops->queue_rqs(&plug->mq_list);
  2411. }
  2412. static void blk_mq_dispatch_plug_list(struct blk_plug *plug, bool from_sched)
  2413. {
  2414. struct blk_mq_hw_ctx *this_hctx = NULL;
  2415. struct blk_mq_ctx *this_ctx = NULL;
  2416. struct rq_list requeue_list = {};
  2417. unsigned int depth = 0;
  2418. bool is_passthrough = false;
  2419. LIST_HEAD(list);
  2420. do {
  2421. struct request *rq = rq_list_pop(&plug->mq_list);
  2422. if (!this_hctx) {
  2423. this_hctx = rq->mq_hctx;
  2424. this_ctx = rq->mq_ctx;
  2425. is_passthrough = blk_rq_is_passthrough(rq);
  2426. } else if (this_hctx != rq->mq_hctx || this_ctx != rq->mq_ctx ||
  2427. is_passthrough != blk_rq_is_passthrough(rq)) {
  2428. rq_list_add_tail(&requeue_list, rq);
  2429. continue;
  2430. }
  2431. list_add_tail(&rq->queuelist, &list);
  2432. depth++;
  2433. } while (!rq_list_empty(&plug->mq_list));
  2434. plug->mq_list = requeue_list;
  2435. trace_block_unplug(this_hctx->queue, depth, !from_sched);
  2436. percpu_ref_get(&this_hctx->queue->q_usage_counter);
  2437. /* passthrough requests should never be issued to the I/O scheduler */
  2438. if (is_passthrough) {
  2439. spin_lock(&this_hctx->lock);
  2440. list_splice_tail_init(&list, &this_hctx->dispatch);
  2441. spin_unlock(&this_hctx->lock);
  2442. blk_mq_run_hw_queue(this_hctx, from_sched);
  2443. } else if (this_hctx->queue->elevator) {
  2444. this_hctx->queue->elevator->type->ops.insert_requests(this_hctx,
  2445. &list, 0);
  2446. blk_mq_run_hw_queue(this_hctx, from_sched);
  2447. } else {
  2448. blk_mq_insert_requests(this_hctx, this_ctx, &list, from_sched);
  2449. }
  2450. percpu_ref_put(&this_hctx->queue->q_usage_counter);
  2451. }
  2452. void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule)
  2453. {
  2454. struct request *rq;
  2455. unsigned int depth;
  2456. /*
  2457. * We may have been called recursively midway through handling
  2458. * plug->mq_list via a schedule() in the driver's queue_rq() callback.
  2459. * To avoid mq_list changing under our feet, clear rq_count early and
  2460. * bail out specifically if rq_count is 0 rather than checking
  2461. * whether the mq_list is empty.
  2462. */
  2463. if (plug->rq_count == 0)
  2464. return;
  2465. depth = plug->rq_count;
  2466. plug->rq_count = 0;
  2467. if (!plug->multiple_queues && !plug->has_elevator && !from_schedule) {
  2468. struct request_queue *q;
  2469. rq = rq_list_peek(&plug->mq_list);
  2470. q = rq->q;
  2471. trace_block_unplug(q, depth, true);
  2472. /*
  2473. * Peek first request and see if we have a ->queue_rqs() hook.
  2474. * If we do, we can dispatch the whole plug list in one go. We
  2475. * already know at this point that all requests belong to the
  2476. * same queue, caller must ensure that's the case.
  2477. */
  2478. if (q->mq_ops->queue_rqs) {
  2479. blk_mq_run_dispatch_ops(q,
  2480. __blk_mq_flush_plug_list(q, plug));
  2481. if (rq_list_empty(&plug->mq_list))
  2482. return;
  2483. }
  2484. blk_mq_run_dispatch_ops(q,
  2485. blk_mq_plug_issue_direct(plug));
  2486. if (rq_list_empty(&plug->mq_list))
  2487. return;
  2488. }
  2489. do {
  2490. blk_mq_dispatch_plug_list(plug, from_schedule);
  2491. } while (!rq_list_empty(&plug->mq_list));
  2492. }
  2493. static void blk_mq_try_issue_list_directly(struct blk_mq_hw_ctx *hctx,
  2494. struct list_head *list)
  2495. {
  2496. int queued = 0;
  2497. blk_status_t ret = BLK_STS_OK;
  2498. while (!list_empty(list)) {
  2499. struct request *rq = list_first_entry(list, struct request,
  2500. queuelist);
  2501. list_del_init(&rq->queuelist);
  2502. ret = blk_mq_request_issue_directly(rq, list_empty(list));
  2503. switch (ret) {
  2504. case BLK_STS_OK:
  2505. queued++;
  2506. break;
  2507. case BLK_STS_RESOURCE:
  2508. case BLK_STS_DEV_RESOURCE:
  2509. blk_mq_request_bypass_insert(rq, 0);
  2510. if (list_empty(list))
  2511. blk_mq_run_hw_queue(hctx, false);
  2512. goto out;
  2513. default:
  2514. blk_mq_end_request(rq, ret);
  2515. break;
  2516. }
  2517. }
  2518. out:
  2519. if (ret != BLK_STS_OK)
  2520. blk_mq_commit_rqs(hctx, queued, false);
  2521. }
  2522. static bool blk_mq_attempt_bio_merge(struct request_queue *q,
  2523. struct bio *bio, unsigned int nr_segs)
  2524. {
  2525. if (!blk_queue_nomerges(q) && bio_mergeable(bio)) {
  2526. if (blk_attempt_plug_merge(q, bio, nr_segs))
  2527. return true;
  2528. if (blk_mq_sched_bio_merge(q, bio, nr_segs))
  2529. return true;
  2530. }
  2531. return false;
  2532. }
  2533. static struct request *blk_mq_get_new_requests(struct request_queue *q,
  2534. struct blk_plug *plug,
  2535. struct bio *bio,
  2536. unsigned int nsegs)
  2537. {
  2538. struct blk_mq_alloc_data data = {
  2539. .q = q,
  2540. .nr_tags = 1,
  2541. .cmd_flags = bio->bi_opf,
  2542. };
  2543. struct request *rq;
  2544. rq_qos_throttle(q, bio);
  2545. if (plug) {
  2546. data.nr_tags = plug->nr_ios;
  2547. plug->nr_ios = 1;
  2548. data.cached_rqs = &plug->cached_rqs;
  2549. }
  2550. rq = __blk_mq_alloc_requests(&data);
  2551. if (rq)
  2552. return rq;
  2553. rq_qos_cleanup(q, bio);
  2554. if (bio->bi_opf & REQ_NOWAIT)
  2555. bio_wouldblock_error(bio);
  2556. return NULL;
  2557. }
  2558. /*
  2559. * Check if there is a suitable cached request and return it.
  2560. */
  2561. static struct request *blk_mq_peek_cached_request(struct blk_plug *plug,
  2562. struct request_queue *q, blk_opf_t opf)
  2563. {
  2564. enum hctx_type type = blk_mq_get_hctx_type(opf);
  2565. struct request *rq;
  2566. if (!plug)
  2567. return NULL;
  2568. rq = rq_list_peek(&plug->cached_rqs);
  2569. if (!rq || rq->q != q)
  2570. return NULL;
  2571. if (type != rq->mq_hctx->type &&
  2572. (type != HCTX_TYPE_READ || rq->mq_hctx->type != HCTX_TYPE_DEFAULT))
  2573. return NULL;
  2574. if (op_is_flush(rq->cmd_flags) != op_is_flush(opf))
  2575. return NULL;
  2576. return rq;
  2577. }
  2578. static void blk_mq_use_cached_rq(struct request *rq, struct blk_plug *plug,
  2579. struct bio *bio)
  2580. {
  2581. if (rq_list_pop(&plug->cached_rqs) != rq)
  2582. WARN_ON_ONCE(1);
  2583. /*
  2584. * If any qos ->throttle() end up blocking, we will have flushed the
  2585. * plug and hence killed the cached_rq list as well. Pop this entry
  2586. * before we throttle.
  2587. */
  2588. rq_qos_throttle(rq->q, bio);
  2589. blk_mq_rq_time_init(rq, 0);
  2590. rq->cmd_flags = bio->bi_opf;
  2591. INIT_LIST_HEAD(&rq->queuelist);
  2592. }
  2593. static bool bio_unaligned(const struct bio *bio, struct request_queue *q)
  2594. {
  2595. unsigned int bs_mask = queue_logical_block_size(q) - 1;
  2596. /* .bi_sector of any zero sized bio need to be initialized */
  2597. if ((bio->bi_iter.bi_size & bs_mask) ||
  2598. ((bio->bi_iter.bi_sector << SECTOR_SHIFT) & bs_mask))
  2599. return true;
  2600. return false;
  2601. }
  2602. /**
  2603. * blk_mq_submit_bio - Create and send a request to block device.
  2604. * @bio: Bio pointer.
  2605. *
  2606. * Builds up a request structure from @q and @bio and send to the device. The
  2607. * request may not be queued directly to hardware if:
  2608. * * This request can be merged with another one
  2609. * * We want to place request at plug queue for possible future merging
  2610. * * There is an IO scheduler active at this queue
  2611. *
  2612. * It will not queue the request if there is an error with the bio, or at the
  2613. * request creation.
  2614. */
  2615. void blk_mq_submit_bio(struct bio *bio)
  2616. {
  2617. struct request_queue *q = bdev_get_queue(bio->bi_bdev);
  2618. struct blk_plug *plug = current->plug;
  2619. const int is_sync = op_is_sync(bio->bi_opf);
  2620. struct blk_mq_hw_ctx *hctx;
  2621. unsigned int nr_segs;
  2622. struct request *rq;
  2623. blk_status_t ret;
  2624. /*
  2625. * If the plug has a cached request for this queue, try to use it.
  2626. */
  2627. rq = blk_mq_peek_cached_request(plug, q, bio->bi_opf);
  2628. /*
  2629. * A BIO that was released from a zone write plug has already been
  2630. * through the preparation in this function, already holds a reference
  2631. * on the queue usage counter, and is the only write BIO in-flight for
  2632. * the target zone. Go straight to preparing a request for it.
  2633. */
  2634. if (bio_zone_write_plugging(bio)) {
  2635. nr_segs = bio->__bi_nr_segments;
  2636. if (rq)
  2637. blk_queue_exit(q);
  2638. goto new_request;
  2639. }
  2640. bio = blk_queue_bounce(bio, q);
  2641. /*
  2642. * The cached request already holds a q_usage_counter reference and we
  2643. * don't have to acquire a new one if we use it.
  2644. */
  2645. if (!rq) {
  2646. if (unlikely(bio_queue_enter(bio)))
  2647. return;
  2648. }
  2649. /*
  2650. * Device reconfiguration may change logical block size or reduce the
  2651. * number of poll queues, so the checks for alignment and poll support
  2652. * have to be done with queue usage counter held.
  2653. */
  2654. if (unlikely(bio_unaligned(bio, q))) {
  2655. bio_io_error(bio);
  2656. goto queue_exit;
  2657. }
  2658. if ((bio->bi_opf & REQ_POLLED) && !blk_mq_can_poll(q)) {
  2659. bio->bi_status = BLK_STS_NOTSUPP;
  2660. bio_endio(bio);
  2661. goto queue_exit;
  2662. }
  2663. bio = __bio_split_to_limits(bio, &q->limits, &nr_segs);
  2664. if (!bio)
  2665. goto queue_exit;
  2666. if (!bio_integrity_prep(bio))
  2667. goto queue_exit;
  2668. if (blk_mq_attempt_bio_merge(q, bio, nr_segs))
  2669. goto queue_exit;
  2670. if (bio_needs_zone_write_plugging(bio)) {
  2671. if (blk_zone_plug_bio(bio, nr_segs))
  2672. goto queue_exit;
  2673. }
  2674. new_request:
  2675. if (!rq) {
  2676. rq = blk_mq_get_new_requests(q, plug, bio, nr_segs);
  2677. if (unlikely(!rq))
  2678. goto queue_exit;
  2679. } else {
  2680. blk_mq_use_cached_rq(rq, plug, bio);
  2681. }
  2682. trace_block_getrq(bio);
  2683. rq_qos_track(q, rq, bio);
  2684. blk_mq_bio_to_request(rq, bio, nr_segs);
  2685. ret = blk_crypto_rq_get_keyslot(rq);
  2686. if (ret != BLK_STS_OK) {
  2687. bio->bi_status = ret;
  2688. bio_endio(bio);
  2689. blk_mq_free_request(rq);
  2690. return;
  2691. }
  2692. if (bio_zone_write_plugging(bio))
  2693. blk_zone_write_plug_init_request(rq);
  2694. if (op_is_flush(bio->bi_opf) && blk_insert_flush(rq))
  2695. return;
  2696. if (plug) {
  2697. blk_add_rq_to_plug(plug, rq);
  2698. return;
  2699. }
  2700. hctx = rq->mq_hctx;
  2701. if ((rq->rq_flags & RQF_USE_SCHED) ||
  2702. (hctx->dispatch_busy && (q->nr_hw_queues == 1 || !is_sync))) {
  2703. blk_mq_insert_request(rq, 0);
  2704. blk_mq_run_hw_queue(hctx, true);
  2705. } else {
  2706. blk_mq_run_dispatch_ops(q, blk_mq_try_issue_directly(hctx, rq));
  2707. }
  2708. return;
  2709. queue_exit:
  2710. /*
  2711. * Don't drop the queue reference if we were trying to use a cached
  2712. * request and thus didn't acquire one.
  2713. */
  2714. if (!rq)
  2715. blk_queue_exit(q);
  2716. }
  2717. #ifdef CONFIG_BLK_MQ_STACKING
  2718. /**
  2719. * blk_insert_cloned_request - Helper for stacking drivers to submit a request
  2720. * @rq: the request being queued
  2721. */
  2722. blk_status_t blk_insert_cloned_request(struct request *rq)
  2723. {
  2724. struct request_queue *q = rq->q;
  2725. unsigned int max_sectors = blk_queue_get_max_sectors(rq);
  2726. unsigned int max_segments = blk_rq_get_max_segments(rq);
  2727. blk_status_t ret;
  2728. if (blk_rq_sectors(rq) > max_sectors) {
  2729. /*
  2730. * SCSI device does not have a good way to return if
  2731. * Write Same/Zero is actually supported. If a device rejects
  2732. * a non-read/write command (discard, write same,etc.) the
  2733. * low-level device driver will set the relevant queue limit to
  2734. * 0 to prevent blk-lib from issuing more of the offending
  2735. * operations. Commands queued prior to the queue limit being
  2736. * reset need to be completed with BLK_STS_NOTSUPP to avoid I/O
  2737. * errors being propagated to upper layers.
  2738. */
  2739. if (max_sectors == 0)
  2740. return BLK_STS_NOTSUPP;
  2741. printk(KERN_ERR "%s: over max size limit. (%u > %u)\n",
  2742. __func__, blk_rq_sectors(rq), max_sectors);
  2743. return BLK_STS_IOERR;
  2744. }
  2745. /*
  2746. * The queue settings related to segment counting may differ from the
  2747. * original queue.
  2748. */
  2749. rq->nr_phys_segments = blk_recalc_rq_segments(rq);
  2750. if (rq->nr_phys_segments > max_segments) {
  2751. printk(KERN_ERR "%s: over max segments limit. (%u > %u)\n",
  2752. __func__, rq->nr_phys_segments, max_segments);
  2753. return BLK_STS_IOERR;
  2754. }
  2755. if (q->disk && should_fail_request(q->disk->part0, blk_rq_bytes(rq)))
  2756. return BLK_STS_IOERR;
  2757. ret = blk_crypto_rq_get_keyslot(rq);
  2758. if (ret != BLK_STS_OK)
  2759. return ret;
  2760. blk_account_io_start(rq);
  2761. /*
  2762. * Since we have a scheduler attached on the top device,
  2763. * bypass a potential scheduler on the bottom device for
  2764. * insert.
  2765. */
  2766. blk_mq_run_dispatch_ops(q,
  2767. ret = blk_mq_request_issue_directly(rq, true));
  2768. if (ret)
  2769. blk_account_io_done(rq, blk_time_get_ns());
  2770. return ret;
  2771. }
  2772. EXPORT_SYMBOL_GPL(blk_insert_cloned_request);
  2773. /**
  2774. * blk_rq_unprep_clone - Helper function to free all bios in a cloned request
  2775. * @rq: the clone request to be cleaned up
  2776. *
  2777. * Description:
  2778. * Free all bios in @rq for a cloned request.
  2779. */
  2780. void blk_rq_unprep_clone(struct request *rq)
  2781. {
  2782. struct bio *bio;
  2783. while ((bio = rq->bio) != NULL) {
  2784. rq->bio = bio->bi_next;
  2785. bio_put(bio);
  2786. }
  2787. }
  2788. EXPORT_SYMBOL_GPL(blk_rq_unprep_clone);
  2789. /**
  2790. * blk_rq_prep_clone - Helper function to setup clone request
  2791. * @rq: the request to be setup
  2792. * @rq_src: original request to be cloned
  2793. * @bs: bio_set that bios for clone are allocated from
  2794. * @gfp_mask: memory allocation mask for bio
  2795. * @bio_ctr: setup function to be called for each clone bio.
  2796. * Returns %0 for success, non %0 for failure.
  2797. * @data: private data to be passed to @bio_ctr
  2798. *
  2799. * Description:
  2800. * Clones bios in @rq_src to @rq, and copies attributes of @rq_src to @rq.
  2801. * Also, pages which the original bios are pointing to are not copied
  2802. * and the cloned bios just point same pages.
  2803. * So cloned bios must be completed before original bios, which means
  2804. * the caller must complete @rq before @rq_src.
  2805. */
  2806. int blk_rq_prep_clone(struct request *rq, struct request *rq_src,
  2807. struct bio_set *bs, gfp_t gfp_mask,
  2808. int (*bio_ctr)(struct bio *, struct bio *, void *),
  2809. void *data)
  2810. {
  2811. struct bio *bio, *bio_src;
  2812. if (!bs)
  2813. bs = &fs_bio_set;
  2814. __rq_for_each_bio(bio_src, rq_src) {
  2815. bio = bio_alloc_clone(rq->q->disk->part0, bio_src, gfp_mask,
  2816. bs);
  2817. if (!bio)
  2818. goto free_and_out;
  2819. if (bio_ctr && bio_ctr(bio, bio_src, data))
  2820. goto free_and_out;
  2821. if (rq->bio) {
  2822. rq->biotail->bi_next = bio;
  2823. rq->biotail = bio;
  2824. } else {
  2825. rq->bio = rq->biotail = bio;
  2826. }
  2827. bio = NULL;
  2828. }
  2829. /* Copy attributes of the original request to the clone request. */
  2830. rq->__sector = blk_rq_pos(rq_src);
  2831. rq->__data_len = blk_rq_bytes(rq_src);
  2832. if (rq_src->rq_flags & RQF_SPECIAL_PAYLOAD) {
  2833. rq->rq_flags |= RQF_SPECIAL_PAYLOAD;
  2834. rq->special_vec = rq_src->special_vec;
  2835. }
  2836. rq->nr_phys_segments = rq_src->nr_phys_segments;
  2837. rq->nr_integrity_segments = rq_src->nr_integrity_segments;
  2838. if (rq->bio && blk_crypto_rq_bio_prep(rq, rq->bio, gfp_mask) < 0)
  2839. goto free_and_out;
  2840. return 0;
  2841. free_and_out:
  2842. if (bio)
  2843. bio_put(bio);
  2844. blk_rq_unprep_clone(rq);
  2845. return -ENOMEM;
  2846. }
  2847. EXPORT_SYMBOL_GPL(blk_rq_prep_clone);
  2848. #endif /* CONFIG_BLK_MQ_STACKING */
  2849. /*
  2850. * Steal bios from a request and add them to a bio list.
  2851. * The request must not have been partially completed before.
  2852. */
  2853. void blk_steal_bios(struct bio_list *list, struct request *rq)
  2854. {
  2855. if (rq->bio) {
  2856. if (list->tail)
  2857. list->tail->bi_next = rq->bio;
  2858. else
  2859. list->head = rq->bio;
  2860. list->tail = rq->biotail;
  2861. rq->bio = NULL;
  2862. rq->biotail = NULL;
  2863. }
  2864. rq->__data_len = 0;
  2865. }
  2866. EXPORT_SYMBOL_GPL(blk_steal_bios);
  2867. static size_t order_to_size(unsigned int order)
  2868. {
  2869. return (size_t)PAGE_SIZE << order;
  2870. }
  2871. /* called before freeing request pool in @tags */
  2872. static void blk_mq_clear_rq_mapping(struct blk_mq_tags *drv_tags,
  2873. struct blk_mq_tags *tags)
  2874. {
  2875. struct page *page;
  2876. unsigned long flags;
  2877. /*
  2878. * There is no need to clear mapping if driver tags is not initialized
  2879. * or the mapping belongs to the driver tags.
  2880. */
  2881. if (!drv_tags || drv_tags == tags)
  2882. return;
  2883. list_for_each_entry(page, &tags->page_list, lru) {
  2884. unsigned long start = (unsigned long)page_address(page);
  2885. unsigned long end = start + order_to_size(page->private);
  2886. int i;
  2887. for (i = 0; i < drv_tags->nr_tags; i++) {
  2888. struct request *rq = drv_tags->rqs[i];
  2889. unsigned long rq_addr = (unsigned long)rq;
  2890. if (rq_addr >= start && rq_addr < end) {
  2891. WARN_ON_ONCE(req_ref_read(rq) != 0);
  2892. cmpxchg(&drv_tags->rqs[i], rq, NULL);
  2893. }
  2894. }
  2895. }
  2896. /*
  2897. * Wait until all pending iteration is done.
  2898. *
  2899. * Request reference is cleared and it is guaranteed to be observed
  2900. * after the ->lock is released.
  2901. */
  2902. spin_lock_irqsave(&drv_tags->lock, flags);
  2903. spin_unlock_irqrestore(&drv_tags->lock, flags);
  2904. }
  2905. void blk_mq_free_rqs(struct blk_mq_tag_set *set, struct blk_mq_tags *tags,
  2906. unsigned int hctx_idx)
  2907. {
  2908. struct blk_mq_tags *drv_tags;
  2909. struct page *page;
  2910. if (list_empty(&tags->page_list))
  2911. return;
  2912. if (blk_mq_is_shared_tags(set->flags))
  2913. drv_tags = set->shared_tags;
  2914. else
  2915. drv_tags = set->tags[hctx_idx];
  2916. if (tags->static_rqs && set->ops->exit_request) {
  2917. int i;
  2918. for (i = 0; i < tags->nr_tags; i++) {
  2919. struct request *rq = tags->static_rqs[i];
  2920. if (!rq)
  2921. continue;
  2922. set->ops->exit_request(set, rq, hctx_idx);
  2923. tags->static_rqs[i] = NULL;
  2924. }
  2925. }
  2926. blk_mq_clear_rq_mapping(drv_tags, tags);
  2927. while (!list_empty(&tags->page_list)) {
  2928. page = list_first_entry(&tags->page_list, struct page, lru);
  2929. list_del_init(&page->lru);
  2930. /*
  2931. * Remove kmemleak object previously allocated in
  2932. * blk_mq_alloc_rqs().
  2933. */
  2934. kmemleak_free(page_address(page));
  2935. __free_pages(page, page->private);
  2936. }
  2937. }
  2938. void blk_mq_free_rq_map(struct blk_mq_tags *tags)
  2939. {
  2940. kfree(tags->rqs);
  2941. tags->rqs = NULL;
  2942. kfree(tags->static_rqs);
  2943. tags->static_rqs = NULL;
  2944. blk_mq_free_tags(tags);
  2945. }
  2946. static enum hctx_type hctx_idx_to_type(struct blk_mq_tag_set *set,
  2947. unsigned int hctx_idx)
  2948. {
  2949. int i;
  2950. for (i = 0; i < set->nr_maps; i++) {
  2951. unsigned int start = set->map[i].queue_offset;
  2952. unsigned int end = start + set->map[i].nr_queues;
  2953. if (hctx_idx >= start && hctx_idx < end)
  2954. break;
  2955. }
  2956. if (i >= set->nr_maps)
  2957. i = HCTX_TYPE_DEFAULT;
  2958. return i;
  2959. }
  2960. static int blk_mq_get_hctx_node(struct blk_mq_tag_set *set,
  2961. unsigned int hctx_idx)
  2962. {
  2963. enum hctx_type type = hctx_idx_to_type(set, hctx_idx);
  2964. return blk_mq_hw_queue_to_node(&set->map[type], hctx_idx);
  2965. }
  2966. static struct blk_mq_tags *blk_mq_alloc_rq_map(struct blk_mq_tag_set *set,
  2967. unsigned int hctx_idx,
  2968. unsigned int nr_tags,
  2969. unsigned int reserved_tags)
  2970. {
  2971. int node = blk_mq_get_hctx_node(set, hctx_idx);
  2972. struct blk_mq_tags *tags;
  2973. if (node == NUMA_NO_NODE)
  2974. node = set->numa_node;
  2975. tags = blk_mq_init_tags(nr_tags, reserved_tags, node,
  2976. BLK_MQ_FLAG_TO_ALLOC_POLICY(set->flags));
  2977. if (!tags)
  2978. return NULL;
  2979. tags->rqs = kcalloc_node(nr_tags, sizeof(struct request *),
  2980. GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY,
  2981. node);
  2982. if (!tags->rqs)
  2983. goto err_free_tags;
  2984. tags->static_rqs = kcalloc_node(nr_tags, sizeof(struct request *),
  2985. GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY,
  2986. node);
  2987. if (!tags->static_rqs)
  2988. goto err_free_rqs;
  2989. return tags;
  2990. err_free_rqs:
  2991. kfree(tags->rqs);
  2992. err_free_tags:
  2993. blk_mq_free_tags(tags);
  2994. return NULL;
  2995. }
  2996. static int blk_mq_init_request(struct blk_mq_tag_set *set, struct request *rq,
  2997. unsigned int hctx_idx, int node)
  2998. {
  2999. int ret;
  3000. if (set->ops->init_request) {
  3001. ret = set->ops->init_request(set, rq, hctx_idx, node);
  3002. if (ret)
  3003. return ret;
  3004. }
  3005. WRITE_ONCE(rq->state, MQ_RQ_IDLE);
  3006. return 0;
  3007. }
  3008. static int blk_mq_alloc_rqs(struct blk_mq_tag_set *set,
  3009. struct blk_mq_tags *tags,
  3010. unsigned int hctx_idx, unsigned int depth)
  3011. {
  3012. unsigned int i, j, entries_per_page, max_order = 4;
  3013. int node = blk_mq_get_hctx_node(set, hctx_idx);
  3014. size_t rq_size, left;
  3015. if (node == NUMA_NO_NODE)
  3016. node = set->numa_node;
  3017. INIT_LIST_HEAD(&tags->page_list);
  3018. /*
  3019. * rq_size is the size of the request plus driver payload, rounded
  3020. * to the cacheline size
  3021. */
  3022. rq_size = round_up(sizeof(struct request) + set->cmd_size,
  3023. cache_line_size());
  3024. left = rq_size * depth;
  3025. for (i = 0; i < depth; ) {
  3026. int this_order = max_order;
  3027. struct page *page;
  3028. int to_do;
  3029. void *p;
  3030. while (this_order && left < order_to_size(this_order - 1))
  3031. this_order--;
  3032. do {
  3033. page = alloc_pages_node(node,
  3034. GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO,
  3035. this_order);
  3036. if (page)
  3037. break;
  3038. if (!this_order--)
  3039. break;
  3040. if (order_to_size(this_order) < rq_size)
  3041. break;
  3042. } while (1);
  3043. if (!page)
  3044. goto fail;
  3045. page->private = this_order;
  3046. list_add_tail(&page->lru, &tags->page_list);
  3047. p = page_address(page);
  3048. /*
  3049. * Allow kmemleak to scan these pages as they contain pointers
  3050. * to additional allocations like via ops->init_request().
  3051. */
  3052. kmemleak_alloc(p, order_to_size(this_order), 1, GFP_NOIO);
  3053. entries_per_page = order_to_size(this_order) / rq_size;
  3054. to_do = min(entries_per_page, depth - i);
  3055. left -= to_do * rq_size;
  3056. for (j = 0; j < to_do; j++) {
  3057. struct request *rq = p;
  3058. tags->static_rqs[i] = rq;
  3059. if (blk_mq_init_request(set, rq, hctx_idx, node)) {
  3060. tags->static_rqs[i] = NULL;
  3061. goto fail;
  3062. }
  3063. p += rq_size;
  3064. i++;
  3065. }
  3066. }
  3067. return 0;
  3068. fail:
  3069. blk_mq_free_rqs(set, tags, hctx_idx);
  3070. return -ENOMEM;
  3071. }
  3072. struct rq_iter_data {
  3073. struct blk_mq_hw_ctx *hctx;
  3074. bool has_rq;
  3075. };
  3076. static bool blk_mq_has_request(struct request *rq, void *data)
  3077. {
  3078. struct rq_iter_data *iter_data = data;
  3079. if (rq->mq_hctx != iter_data->hctx)
  3080. return true;
  3081. iter_data->has_rq = true;
  3082. return false;
  3083. }
  3084. static bool blk_mq_hctx_has_requests(struct blk_mq_hw_ctx *hctx)
  3085. {
  3086. struct blk_mq_tags *tags = hctx->sched_tags ?
  3087. hctx->sched_tags : hctx->tags;
  3088. struct rq_iter_data data = {
  3089. .hctx = hctx,
  3090. };
  3091. blk_mq_all_tag_iter(tags, blk_mq_has_request, &data);
  3092. return data.has_rq;
  3093. }
  3094. static bool blk_mq_hctx_has_online_cpu(struct blk_mq_hw_ctx *hctx,
  3095. unsigned int this_cpu)
  3096. {
  3097. enum hctx_type type = hctx->type;
  3098. int cpu;
  3099. /*
  3100. * hctx->cpumask has to rule out isolated CPUs, but userspace still
  3101. * might submit IOs on these isolated CPUs, so use the queue map to
  3102. * check if all CPUs mapped to this hctx are offline
  3103. */
  3104. for_each_online_cpu(cpu) {
  3105. struct blk_mq_hw_ctx *h = blk_mq_map_queue_type(hctx->queue,
  3106. type, cpu);
  3107. if (h != hctx)
  3108. continue;
  3109. /* this hctx has at least one online CPU */
  3110. if (this_cpu != cpu)
  3111. return true;
  3112. }
  3113. return false;
  3114. }
  3115. static int blk_mq_hctx_notify_offline(unsigned int cpu, struct hlist_node *node)
  3116. {
  3117. struct blk_mq_hw_ctx *hctx = hlist_entry_safe(node,
  3118. struct blk_mq_hw_ctx, cpuhp_online);
  3119. if (blk_mq_hctx_has_online_cpu(hctx, cpu))
  3120. return 0;
  3121. /*
  3122. * Prevent new request from being allocated on the current hctx.
  3123. *
  3124. * The smp_mb__after_atomic() Pairs with the implied barrier in
  3125. * test_and_set_bit_lock in sbitmap_get(). Ensures the inactive flag is
  3126. * seen once we return from the tag allocator.
  3127. */
  3128. set_bit(BLK_MQ_S_INACTIVE, &hctx->state);
  3129. smp_mb__after_atomic();
  3130. /*
  3131. * Try to grab a reference to the queue and wait for any outstanding
  3132. * requests. If we could not grab a reference the queue has been
  3133. * frozen and there are no requests.
  3134. */
  3135. if (percpu_ref_tryget(&hctx->queue->q_usage_counter)) {
  3136. while (blk_mq_hctx_has_requests(hctx))
  3137. msleep(5);
  3138. percpu_ref_put(&hctx->queue->q_usage_counter);
  3139. }
  3140. return 0;
  3141. }
  3142. /*
  3143. * Check if one CPU is mapped to the specified hctx
  3144. *
  3145. * Isolated CPUs have been ruled out from hctx->cpumask, which is supposed
  3146. * to be used for scheduling kworker only. For other usage, please call this
  3147. * helper for checking if one CPU belongs to the specified hctx
  3148. */
  3149. static bool blk_mq_cpu_mapped_to_hctx(unsigned int cpu,
  3150. const struct blk_mq_hw_ctx *hctx)
  3151. {
  3152. struct blk_mq_hw_ctx *mapped_hctx = blk_mq_map_queue_type(hctx->queue,
  3153. hctx->type, cpu);
  3154. return mapped_hctx == hctx;
  3155. }
  3156. static int blk_mq_hctx_notify_online(unsigned int cpu, struct hlist_node *node)
  3157. {
  3158. struct blk_mq_hw_ctx *hctx = hlist_entry_safe(node,
  3159. struct blk_mq_hw_ctx, cpuhp_online);
  3160. if (blk_mq_cpu_mapped_to_hctx(cpu, hctx))
  3161. clear_bit(BLK_MQ_S_INACTIVE, &hctx->state);
  3162. return 0;
  3163. }
  3164. /*
  3165. * 'cpu' is going away. splice any existing rq_list entries from this
  3166. * software queue to the hw queue dispatch list, and ensure that it
  3167. * gets run.
  3168. */
  3169. static int blk_mq_hctx_notify_dead(unsigned int cpu, struct hlist_node *node)
  3170. {
  3171. struct blk_mq_hw_ctx *hctx;
  3172. struct blk_mq_ctx *ctx;
  3173. LIST_HEAD(tmp);
  3174. enum hctx_type type;
  3175. hctx = hlist_entry_safe(node, struct blk_mq_hw_ctx, cpuhp_dead);
  3176. if (!blk_mq_cpu_mapped_to_hctx(cpu, hctx))
  3177. return 0;
  3178. ctx = __blk_mq_get_ctx(hctx->queue, cpu);
  3179. type = hctx->type;
  3180. spin_lock(&ctx->lock);
  3181. if (!list_empty(&ctx->rq_lists[type])) {
  3182. list_splice_init(&ctx->rq_lists[type], &tmp);
  3183. blk_mq_hctx_clear_pending(hctx, ctx);
  3184. }
  3185. spin_unlock(&ctx->lock);
  3186. if (list_empty(&tmp))
  3187. return 0;
  3188. spin_lock(&hctx->lock);
  3189. list_splice_tail_init(&tmp, &hctx->dispatch);
  3190. spin_unlock(&hctx->lock);
  3191. blk_mq_run_hw_queue(hctx, true);
  3192. return 0;
  3193. }
  3194. static void __blk_mq_remove_cpuhp(struct blk_mq_hw_ctx *hctx)
  3195. {
  3196. lockdep_assert_held(&blk_mq_cpuhp_lock);
  3197. if (!(hctx->flags & BLK_MQ_F_STACKING) &&
  3198. !hlist_unhashed(&hctx->cpuhp_online)) {
  3199. cpuhp_state_remove_instance_nocalls(CPUHP_AP_BLK_MQ_ONLINE,
  3200. &hctx->cpuhp_online);
  3201. INIT_HLIST_NODE(&hctx->cpuhp_online);
  3202. }
  3203. if (!hlist_unhashed(&hctx->cpuhp_dead)) {
  3204. cpuhp_state_remove_instance_nocalls(CPUHP_BLK_MQ_DEAD,
  3205. &hctx->cpuhp_dead);
  3206. INIT_HLIST_NODE(&hctx->cpuhp_dead);
  3207. }
  3208. }
  3209. static void blk_mq_remove_cpuhp(struct blk_mq_hw_ctx *hctx)
  3210. {
  3211. mutex_lock(&blk_mq_cpuhp_lock);
  3212. __blk_mq_remove_cpuhp(hctx);
  3213. mutex_unlock(&blk_mq_cpuhp_lock);
  3214. }
  3215. static void __blk_mq_add_cpuhp(struct blk_mq_hw_ctx *hctx)
  3216. {
  3217. lockdep_assert_held(&blk_mq_cpuhp_lock);
  3218. if (!(hctx->flags & BLK_MQ_F_STACKING) &&
  3219. hlist_unhashed(&hctx->cpuhp_online))
  3220. cpuhp_state_add_instance_nocalls(CPUHP_AP_BLK_MQ_ONLINE,
  3221. &hctx->cpuhp_online);
  3222. if (hlist_unhashed(&hctx->cpuhp_dead))
  3223. cpuhp_state_add_instance_nocalls(CPUHP_BLK_MQ_DEAD,
  3224. &hctx->cpuhp_dead);
  3225. }
  3226. static void __blk_mq_remove_cpuhp_list(struct list_head *head)
  3227. {
  3228. struct blk_mq_hw_ctx *hctx;
  3229. lockdep_assert_held(&blk_mq_cpuhp_lock);
  3230. list_for_each_entry(hctx, head, hctx_list)
  3231. __blk_mq_remove_cpuhp(hctx);
  3232. }
  3233. /*
  3234. * Unregister cpuhp callbacks from exited hw queues
  3235. *
  3236. * Safe to call if this `request_queue` is live
  3237. */
  3238. static void blk_mq_remove_hw_queues_cpuhp(struct request_queue *q)
  3239. {
  3240. LIST_HEAD(hctx_list);
  3241. spin_lock(&q->unused_hctx_lock);
  3242. list_splice_init(&q->unused_hctx_list, &hctx_list);
  3243. spin_unlock(&q->unused_hctx_lock);
  3244. mutex_lock(&blk_mq_cpuhp_lock);
  3245. __blk_mq_remove_cpuhp_list(&hctx_list);
  3246. mutex_unlock(&blk_mq_cpuhp_lock);
  3247. spin_lock(&q->unused_hctx_lock);
  3248. list_splice(&hctx_list, &q->unused_hctx_list);
  3249. spin_unlock(&q->unused_hctx_lock);
  3250. }
  3251. /*
  3252. * Register cpuhp callbacks from all hw queues
  3253. *
  3254. * Safe to call if this `request_queue` is live
  3255. */
  3256. static void blk_mq_add_hw_queues_cpuhp(struct request_queue *q)
  3257. {
  3258. struct blk_mq_hw_ctx *hctx;
  3259. unsigned long i;
  3260. mutex_lock(&blk_mq_cpuhp_lock);
  3261. queue_for_each_hw_ctx(q, hctx, i)
  3262. __blk_mq_add_cpuhp(hctx);
  3263. mutex_unlock(&blk_mq_cpuhp_lock);
  3264. }
  3265. /*
  3266. * Before freeing hw queue, clearing the flush request reference in
  3267. * tags->rqs[] for avoiding potential UAF.
  3268. */
  3269. static void blk_mq_clear_flush_rq_mapping(struct blk_mq_tags *tags,
  3270. unsigned int queue_depth, struct request *flush_rq)
  3271. {
  3272. int i;
  3273. unsigned long flags;
  3274. /* The hw queue may not be mapped yet */
  3275. if (!tags)
  3276. return;
  3277. WARN_ON_ONCE(req_ref_read(flush_rq) != 0);
  3278. for (i = 0; i < queue_depth; i++)
  3279. cmpxchg(&tags->rqs[i], flush_rq, NULL);
  3280. /*
  3281. * Wait until all pending iteration is done.
  3282. *
  3283. * Request reference is cleared and it is guaranteed to be observed
  3284. * after the ->lock is released.
  3285. */
  3286. spin_lock_irqsave(&tags->lock, flags);
  3287. spin_unlock_irqrestore(&tags->lock, flags);
  3288. }
  3289. /* hctx->ctxs will be freed in queue's release handler */
  3290. static void blk_mq_exit_hctx(struct request_queue *q,
  3291. struct blk_mq_tag_set *set,
  3292. struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx)
  3293. {
  3294. struct request *flush_rq = hctx->fq->flush_rq;
  3295. if (blk_mq_hw_queue_mapped(hctx))
  3296. blk_mq_tag_idle(hctx);
  3297. if (blk_queue_init_done(q))
  3298. blk_mq_clear_flush_rq_mapping(set->tags[hctx_idx],
  3299. set->queue_depth, flush_rq);
  3300. if (set->ops->exit_request)
  3301. set->ops->exit_request(set, flush_rq, hctx_idx);
  3302. if (set->ops->exit_hctx)
  3303. set->ops->exit_hctx(hctx, hctx_idx);
  3304. xa_erase(&q->hctx_table, hctx_idx);
  3305. spin_lock(&q->unused_hctx_lock);
  3306. list_add(&hctx->hctx_list, &q->unused_hctx_list);
  3307. spin_unlock(&q->unused_hctx_lock);
  3308. }
  3309. static void blk_mq_exit_hw_queues(struct request_queue *q,
  3310. struct blk_mq_tag_set *set, int nr_queue)
  3311. {
  3312. struct blk_mq_hw_ctx *hctx;
  3313. unsigned long i;
  3314. queue_for_each_hw_ctx(q, hctx, i) {
  3315. if (i == nr_queue)
  3316. break;
  3317. blk_mq_remove_cpuhp(hctx);
  3318. blk_mq_exit_hctx(q, set, hctx, i);
  3319. }
  3320. }
  3321. static int blk_mq_init_hctx(struct request_queue *q,
  3322. struct blk_mq_tag_set *set,
  3323. struct blk_mq_hw_ctx *hctx, unsigned hctx_idx)
  3324. {
  3325. hctx->queue_num = hctx_idx;
  3326. hctx->tags = set->tags[hctx_idx];
  3327. if (set->ops->init_hctx &&
  3328. set->ops->init_hctx(hctx, set->driver_data, hctx_idx))
  3329. goto fail;
  3330. if (blk_mq_init_request(set, hctx->fq->flush_rq, hctx_idx,
  3331. hctx->numa_node))
  3332. goto exit_hctx;
  3333. if (xa_insert(&q->hctx_table, hctx_idx, hctx, GFP_KERNEL))
  3334. goto exit_flush_rq;
  3335. if (!(hctx->flags & BLK_MQ_F_STACKING))
  3336. cpuhp_state_add_instance_nocalls(CPUHP_AP_BLK_MQ_ONLINE,
  3337. &hctx->cpuhp_online);
  3338. cpuhp_state_add_instance_nocalls(CPUHP_BLK_MQ_DEAD, &hctx->cpuhp_dead);
  3339. return 0;
  3340. exit_flush_rq:
  3341. if (set->ops->exit_request)
  3342. set->ops->exit_request(set, hctx->fq->flush_rq, hctx_idx);
  3343. exit_hctx:
  3344. if (set->ops->exit_hctx)
  3345. set->ops->exit_hctx(hctx, hctx_idx);
  3346. fail:
  3347. return -1;
  3348. }
  3349. static struct blk_mq_hw_ctx *
  3350. blk_mq_alloc_hctx(struct request_queue *q, struct blk_mq_tag_set *set,
  3351. int node)
  3352. {
  3353. struct blk_mq_hw_ctx *hctx;
  3354. gfp_t gfp = GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY;
  3355. hctx = kzalloc_node(sizeof(struct blk_mq_hw_ctx), gfp, node);
  3356. if (!hctx)
  3357. goto fail_alloc_hctx;
  3358. if (!zalloc_cpumask_var_node(&hctx->cpumask, gfp, node))
  3359. goto free_hctx;
  3360. atomic_set(&hctx->nr_active, 0);
  3361. if (node == NUMA_NO_NODE)
  3362. node = set->numa_node;
  3363. hctx->numa_node = node;
  3364. INIT_DELAYED_WORK(&hctx->run_work, blk_mq_run_work_fn);
  3365. spin_lock_init(&hctx->lock);
  3366. INIT_LIST_HEAD(&hctx->dispatch);
  3367. INIT_HLIST_NODE(&hctx->cpuhp_dead);
  3368. INIT_HLIST_NODE(&hctx->cpuhp_online);
  3369. hctx->queue = q;
  3370. hctx->flags = set->flags & ~BLK_MQ_F_TAG_QUEUE_SHARED;
  3371. INIT_LIST_HEAD(&hctx->hctx_list);
  3372. /*
  3373. * Allocate space for all possible cpus to avoid allocation at
  3374. * runtime
  3375. */
  3376. hctx->ctxs = kmalloc_array_node(nr_cpu_ids, sizeof(void *),
  3377. gfp, node);
  3378. if (!hctx->ctxs)
  3379. goto free_cpumask;
  3380. if (sbitmap_init_node(&hctx->ctx_map, nr_cpu_ids, ilog2(8),
  3381. gfp, node, false, false))
  3382. goto free_ctxs;
  3383. hctx->nr_ctx = 0;
  3384. spin_lock_init(&hctx->dispatch_wait_lock);
  3385. init_waitqueue_func_entry(&hctx->dispatch_wait, blk_mq_dispatch_wake);
  3386. INIT_LIST_HEAD(&hctx->dispatch_wait.entry);
  3387. hctx->fq = blk_alloc_flush_queue(hctx->numa_node, set->cmd_size, gfp);
  3388. if (!hctx->fq)
  3389. goto free_bitmap;
  3390. blk_mq_hctx_kobj_init(hctx);
  3391. return hctx;
  3392. free_bitmap:
  3393. sbitmap_free(&hctx->ctx_map);
  3394. free_ctxs:
  3395. kfree(hctx->ctxs);
  3396. free_cpumask:
  3397. free_cpumask_var(hctx->cpumask);
  3398. free_hctx:
  3399. kfree(hctx);
  3400. fail_alloc_hctx:
  3401. return NULL;
  3402. }
  3403. static void blk_mq_init_cpu_queues(struct request_queue *q,
  3404. unsigned int nr_hw_queues)
  3405. {
  3406. struct blk_mq_tag_set *set = q->tag_set;
  3407. unsigned int i, j;
  3408. for_each_possible_cpu(i) {
  3409. struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i);
  3410. struct blk_mq_hw_ctx *hctx;
  3411. int k;
  3412. __ctx->cpu = i;
  3413. spin_lock_init(&__ctx->lock);
  3414. for (k = HCTX_TYPE_DEFAULT; k < HCTX_MAX_TYPES; k++)
  3415. INIT_LIST_HEAD(&__ctx->rq_lists[k]);
  3416. __ctx->queue = q;
  3417. /*
  3418. * Set local node, IFF we have more than one hw queue. If
  3419. * not, we remain on the home node of the device
  3420. */
  3421. for (j = 0; j < set->nr_maps; j++) {
  3422. hctx = blk_mq_map_queue_type(q, j, i);
  3423. if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE)
  3424. hctx->numa_node = cpu_to_node(i);
  3425. }
  3426. }
  3427. }
  3428. struct blk_mq_tags *blk_mq_alloc_map_and_rqs(struct blk_mq_tag_set *set,
  3429. unsigned int hctx_idx,
  3430. unsigned int depth)
  3431. {
  3432. struct blk_mq_tags *tags;
  3433. int ret;
  3434. tags = blk_mq_alloc_rq_map(set, hctx_idx, depth, set->reserved_tags);
  3435. if (!tags)
  3436. return NULL;
  3437. ret = blk_mq_alloc_rqs(set, tags, hctx_idx, depth);
  3438. if (ret) {
  3439. blk_mq_free_rq_map(tags);
  3440. return NULL;
  3441. }
  3442. return tags;
  3443. }
  3444. static bool __blk_mq_alloc_map_and_rqs(struct blk_mq_tag_set *set,
  3445. int hctx_idx)
  3446. {
  3447. if (blk_mq_is_shared_tags(set->flags)) {
  3448. set->tags[hctx_idx] = set->shared_tags;
  3449. return true;
  3450. }
  3451. set->tags[hctx_idx] = blk_mq_alloc_map_and_rqs(set, hctx_idx,
  3452. set->queue_depth);
  3453. return set->tags[hctx_idx];
  3454. }
  3455. void blk_mq_free_map_and_rqs(struct blk_mq_tag_set *set,
  3456. struct blk_mq_tags *tags,
  3457. unsigned int hctx_idx)
  3458. {
  3459. if (tags) {
  3460. blk_mq_free_rqs(set, tags, hctx_idx);
  3461. blk_mq_free_rq_map(tags);
  3462. }
  3463. }
  3464. static void __blk_mq_free_map_and_rqs(struct blk_mq_tag_set *set,
  3465. unsigned int hctx_idx)
  3466. {
  3467. if (!blk_mq_is_shared_tags(set->flags))
  3468. blk_mq_free_map_and_rqs(set, set->tags[hctx_idx], hctx_idx);
  3469. set->tags[hctx_idx] = NULL;
  3470. }
  3471. static void blk_mq_map_swqueue(struct request_queue *q)
  3472. {
  3473. unsigned int j, hctx_idx;
  3474. unsigned long i;
  3475. struct blk_mq_hw_ctx *hctx;
  3476. struct blk_mq_ctx *ctx;
  3477. struct blk_mq_tag_set *set = q->tag_set;
  3478. queue_for_each_hw_ctx(q, hctx, i) {
  3479. cpumask_clear(hctx->cpumask);
  3480. hctx->nr_ctx = 0;
  3481. hctx->dispatch_from = NULL;
  3482. }
  3483. /*
  3484. * Map software to hardware queues.
  3485. *
  3486. * If the cpu isn't present, the cpu is mapped to first hctx.
  3487. */
  3488. for_each_possible_cpu(i) {
  3489. ctx = per_cpu_ptr(q->queue_ctx, i);
  3490. for (j = 0; j < set->nr_maps; j++) {
  3491. if (!set->map[j].nr_queues) {
  3492. ctx->hctxs[j] = blk_mq_map_queue_type(q,
  3493. HCTX_TYPE_DEFAULT, i);
  3494. continue;
  3495. }
  3496. hctx_idx = set->map[j].mq_map[i];
  3497. /* unmapped hw queue can be remapped after CPU topo changed */
  3498. if (!set->tags[hctx_idx] &&
  3499. !__blk_mq_alloc_map_and_rqs(set, hctx_idx)) {
  3500. /*
  3501. * If tags initialization fail for some hctx,
  3502. * that hctx won't be brought online. In this
  3503. * case, remap the current ctx to hctx[0] which
  3504. * is guaranteed to always have tags allocated
  3505. */
  3506. set->map[j].mq_map[i] = 0;
  3507. }
  3508. hctx = blk_mq_map_queue_type(q, j, i);
  3509. ctx->hctxs[j] = hctx;
  3510. /*
  3511. * If the CPU is already set in the mask, then we've
  3512. * mapped this one already. This can happen if
  3513. * devices share queues across queue maps.
  3514. */
  3515. if (cpumask_test_cpu(i, hctx->cpumask))
  3516. continue;
  3517. cpumask_set_cpu(i, hctx->cpumask);
  3518. hctx->type = j;
  3519. ctx->index_hw[hctx->type] = hctx->nr_ctx;
  3520. hctx->ctxs[hctx->nr_ctx++] = ctx;
  3521. /*
  3522. * If the nr_ctx type overflows, we have exceeded the
  3523. * amount of sw queues we can support.
  3524. */
  3525. BUG_ON(!hctx->nr_ctx);
  3526. }
  3527. for (; j < HCTX_MAX_TYPES; j++)
  3528. ctx->hctxs[j] = blk_mq_map_queue_type(q,
  3529. HCTX_TYPE_DEFAULT, i);
  3530. }
  3531. queue_for_each_hw_ctx(q, hctx, i) {
  3532. int cpu;
  3533. /*
  3534. * If no software queues are mapped to this hardware queue,
  3535. * disable it and free the request entries.
  3536. */
  3537. if (!hctx->nr_ctx) {
  3538. /* Never unmap queue 0. We need it as a
  3539. * fallback in case of a new remap fails
  3540. * allocation
  3541. */
  3542. if (i)
  3543. __blk_mq_free_map_and_rqs(set, i);
  3544. hctx->tags = NULL;
  3545. continue;
  3546. }
  3547. hctx->tags = set->tags[i];
  3548. WARN_ON(!hctx->tags);
  3549. /*
  3550. * Set the map size to the number of mapped software queues.
  3551. * This is more accurate and more efficient than looping
  3552. * over all possibly mapped software queues.
  3553. */
  3554. sbitmap_resize(&hctx->ctx_map, hctx->nr_ctx);
  3555. /*
  3556. * Rule out isolated CPUs from hctx->cpumask to avoid
  3557. * running block kworker on isolated CPUs
  3558. */
  3559. for_each_cpu(cpu, hctx->cpumask) {
  3560. if (cpu_is_isolated(cpu))
  3561. cpumask_clear_cpu(cpu, hctx->cpumask);
  3562. }
  3563. /*
  3564. * Initialize batch roundrobin counts
  3565. */
  3566. hctx->next_cpu = blk_mq_first_mapped_cpu(hctx);
  3567. hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
  3568. }
  3569. }
  3570. /*
  3571. * Caller needs to ensure that we're either frozen/quiesced, or that
  3572. * the queue isn't live yet.
  3573. */
  3574. static void queue_set_hctx_shared(struct request_queue *q, bool shared)
  3575. {
  3576. struct blk_mq_hw_ctx *hctx;
  3577. unsigned long i;
  3578. queue_for_each_hw_ctx(q, hctx, i) {
  3579. if (shared) {
  3580. hctx->flags |= BLK_MQ_F_TAG_QUEUE_SHARED;
  3581. } else {
  3582. blk_mq_tag_idle(hctx);
  3583. hctx->flags &= ~BLK_MQ_F_TAG_QUEUE_SHARED;
  3584. }
  3585. }
  3586. }
  3587. static void blk_mq_update_tag_set_shared(struct blk_mq_tag_set *set,
  3588. bool shared)
  3589. {
  3590. struct request_queue *q;
  3591. lockdep_assert_held(&set->tag_list_lock);
  3592. list_for_each_entry(q, &set->tag_list, tag_set_list) {
  3593. blk_mq_freeze_queue(q);
  3594. queue_set_hctx_shared(q, shared);
  3595. blk_mq_unfreeze_queue(q);
  3596. }
  3597. }
  3598. static void blk_mq_del_queue_tag_set(struct request_queue *q)
  3599. {
  3600. struct blk_mq_tag_set *set = q->tag_set;
  3601. mutex_lock(&set->tag_list_lock);
  3602. list_del(&q->tag_set_list);
  3603. if (list_is_singular(&set->tag_list)) {
  3604. /* just transitioned to unshared */
  3605. set->flags &= ~BLK_MQ_F_TAG_QUEUE_SHARED;
  3606. /* update existing queue */
  3607. blk_mq_update_tag_set_shared(set, false);
  3608. }
  3609. mutex_unlock(&set->tag_list_lock);
  3610. INIT_LIST_HEAD(&q->tag_set_list);
  3611. }
  3612. static void blk_mq_add_queue_tag_set(struct blk_mq_tag_set *set,
  3613. struct request_queue *q)
  3614. {
  3615. mutex_lock(&set->tag_list_lock);
  3616. /*
  3617. * Check to see if we're transitioning to shared (from 1 to 2 queues).
  3618. */
  3619. if (!list_empty(&set->tag_list) &&
  3620. !(set->flags & BLK_MQ_F_TAG_QUEUE_SHARED)) {
  3621. set->flags |= BLK_MQ_F_TAG_QUEUE_SHARED;
  3622. /* update existing queue */
  3623. blk_mq_update_tag_set_shared(set, true);
  3624. }
  3625. if (set->flags & BLK_MQ_F_TAG_QUEUE_SHARED)
  3626. queue_set_hctx_shared(q, true);
  3627. list_add_tail(&q->tag_set_list, &set->tag_list);
  3628. mutex_unlock(&set->tag_list_lock);
  3629. }
  3630. /* All allocations will be freed in release handler of q->mq_kobj */
  3631. static int blk_mq_alloc_ctxs(struct request_queue *q)
  3632. {
  3633. struct blk_mq_ctxs *ctxs;
  3634. int cpu;
  3635. ctxs = kzalloc(sizeof(*ctxs), GFP_KERNEL);
  3636. if (!ctxs)
  3637. return -ENOMEM;
  3638. ctxs->queue_ctx = alloc_percpu(struct blk_mq_ctx);
  3639. if (!ctxs->queue_ctx)
  3640. goto fail;
  3641. for_each_possible_cpu(cpu) {
  3642. struct blk_mq_ctx *ctx = per_cpu_ptr(ctxs->queue_ctx, cpu);
  3643. ctx->ctxs = ctxs;
  3644. }
  3645. q->mq_kobj = &ctxs->kobj;
  3646. q->queue_ctx = ctxs->queue_ctx;
  3647. return 0;
  3648. fail:
  3649. kfree(ctxs);
  3650. return -ENOMEM;
  3651. }
  3652. /*
  3653. * It is the actual release handler for mq, but we do it from
  3654. * request queue's release handler for avoiding use-after-free
  3655. * and headache because q->mq_kobj shouldn't have been introduced,
  3656. * but we can't group ctx/kctx kobj without it.
  3657. */
  3658. void blk_mq_release(struct request_queue *q)
  3659. {
  3660. struct blk_mq_hw_ctx *hctx, *next;
  3661. unsigned long i;
  3662. queue_for_each_hw_ctx(q, hctx, i)
  3663. WARN_ON_ONCE(hctx && list_empty(&hctx->hctx_list));
  3664. /* all hctx are in .unused_hctx_list now */
  3665. list_for_each_entry_safe(hctx, next, &q->unused_hctx_list, hctx_list) {
  3666. list_del_init(&hctx->hctx_list);
  3667. kobject_put(&hctx->kobj);
  3668. }
  3669. xa_destroy(&q->hctx_table);
  3670. /*
  3671. * release .mq_kobj and sw queue's kobject now because
  3672. * both share lifetime with request queue.
  3673. */
  3674. blk_mq_sysfs_deinit(q);
  3675. }
  3676. struct request_queue *blk_mq_alloc_queue(struct blk_mq_tag_set *set,
  3677. struct queue_limits *lim, void *queuedata)
  3678. {
  3679. struct queue_limits default_lim = { };
  3680. struct request_queue *q;
  3681. int ret;
  3682. if (!lim)
  3683. lim = &default_lim;
  3684. lim->features |= BLK_FEAT_IO_STAT | BLK_FEAT_NOWAIT;
  3685. if (set->nr_maps > HCTX_TYPE_POLL)
  3686. lim->features |= BLK_FEAT_POLL;
  3687. q = blk_alloc_queue(lim, set->numa_node);
  3688. if (IS_ERR(q))
  3689. return q;
  3690. q->queuedata = queuedata;
  3691. ret = blk_mq_init_allocated_queue(set, q);
  3692. if (ret) {
  3693. blk_put_queue(q);
  3694. return ERR_PTR(ret);
  3695. }
  3696. return q;
  3697. }
  3698. EXPORT_SYMBOL(blk_mq_alloc_queue);
  3699. /**
  3700. * blk_mq_destroy_queue - shutdown a request queue
  3701. * @q: request queue to shutdown
  3702. *
  3703. * This shuts down a request queue allocated by blk_mq_alloc_queue(). All future
  3704. * requests will be failed with -ENODEV. The caller is responsible for dropping
  3705. * the reference from blk_mq_alloc_queue() by calling blk_put_queue().
  3706. *
  3707. * Context: can sleep
  3708. */
  3709. void blk_mq_destroy_queue(struct request_queue *q)
  3710. {
  3711. WARN_ON_ONCE(!queue_is_mq(q));
  3712. WARN_ON_ONCE(blk_queue_registered(q));
  3713. might_sleep();
  3714. blk_queue_flag_set(QUEUE_FLAG_DYING, q);
  3715. blk_queue_start_drain(q);
  3716. blk_mq_freeze_queue_wait(q);
  3717. blk_sync_queue(q);
  3718. blk_mq_cancel_work_sync(q);
  3719. blk_mq_exit_queue(q);
  3720. }
  3721. EXPORT_SYMBOL(blk_mq_destroy_queue);
  3722. struct gendisk *__blk_mq_alloc_disk(struct blk_mq_tag_set *set,
  3723. struct queue_limits *lim, void *queuedata,
  3724. struct lock_class_key *lkclass)
  3725. {
  3726. struct request_queue *q;
  3727. struct gendisk *disk;
  3728. q = blk_mq_alloc_queue(set, lim, queuedata);
  3729. if (IS_ERR(q))
  3730. return ERR_CAST(q);
  3731. disk = __alloc_disk_node(q, set->numa_node, lkclass);
  3732. if (!disk) {
  3733. blk_mq_destroy_queue(q);
  3734. blk_put_queue(q);
  3735. return ERR_PTR(-ENOMEM);
  3736. }
  3737. set_bit(GD_OWNS_QUEUE, &disk->state);
  3738. return disk;
  3739. }
  3740. EXPORT_SYMBOL(__blk_mq_alloc_disk);
  3741. struct gendisk *blk_mq_alloc_disk_for_queue(struct request_queue *q,
  3742. struct lock_class_key *lkclass)
  3743. {
  3744. struct gendisk *disk;
  3745. if (!blk_get_queue(q))
  3746. return NULL;
  3747. disk = __alloc_disk_node(q, NUMA_NO_NODE, lkclass);
  3748. if (!disk)
  3749. blk_put_queue(q);
  3750. return disk;
  3751. }
  3752. EXPORT_SYMBOL(blk_mq_alloc_disk_for_queue);
  3753. /*
  3754. * Only hctx removed from cpuhp list can be reused
  3755. */
  3756. static bool blk_mq_hctx_is_reusable(struct blk_mq_hw_ctx *hctx)
  3757. {
  3758. return hlist_unhashed(&hctx->cpuhp_online) &&
  3759. hlist_unhashed(&hctx->cpuhp_dead);
  3760. }
  3761. static struct blk_mq_hw_ctx *blk_mq_alloc_and_init_hctx(
  3762. struct blk_mq_tag_set *set, struct request_queue *q,
  3763. int hctx_idx, int node)
  3764. {
  3765. struct blk_mq_hw_ctx *hctx = NULL, *tmp;
  3766. /* reuse dead hctx first */
  3767. spin_lock(&q->unused_hctx_lock);
  3768. list_for_each_entry(tmp, &q->unused_hctx_list, hctx_list) {
  3769. if (tmp->numa_node == node && blk_mq_hctx_is_reusable(tmp)) {
  3770. hctx = tmp;
  3771. break;
  3772. }
  3773. }
  3774. if (hctx)
  3775. list_del_init(&hctx->hctx_list);
  3776. spin_unlock(&q->unused_hctx_lock);
  3777. if (!hctx)
  3778. hctx = blk_mq_alloc_hctx(q, set, node);
  3779. if (!hctx)
  3780. goto fail;
  3781. if (blk_mq_init_hctx(q, set, hctx, hctx_idx))
  3782. goto free_hctx;
  3783. return hctx;
  3784. free_hctx:
  3785. kobject_put(&hctx->kobj);
  3786. fail:
  3787. return NULL;
  3788. }
  3789. static void blk_mq_realloc_hw_ctxs(struct blk_mq_tag_set *set,
  3790. struct request_queue *q)
  3791. {
  3792. struct blk_mq_hw_ctx *hctx;
  3793. unsigned long i, j;
  3794. /* protect against switching io scheduler */
  3795. mutex_lock(&q->sysfs_lock);
  3796. for (i = 0; i < set->nr_hw_queues; i++) {
  3797. int old_node;
  3798. int node = blk_mq_get_hctx_node(set, i);
  3799. struct blk_mq_hw_ctx *old_hctx = xa_load(&q->hctx_table, i);
  3800. if (old_hctx) {
  3801. old_node = old_hctx->numa_node;
  3802. blk_mq_exit_hctx(q, set, old_hctx, i);
  3803. }
  3804. if (!blk_mq_alloc_and_init_hctx(set, q, i, node)) {
  3805. if (!old_hctx)
  3806. break;
  3807. pr_warn("Allocate new hctx on node %d fails, fallback to previous one on node %d\n",
  3808. node, old_node);
  3809. hctx = blk_mq_alloc_and_init_hctx(set, q, i, old_node);
  3810. WARN_ON_ONCE(!hctx);
  3811. }
  3812. }
  3813. /*
  3814. * Increasing nr_hw_queues fails. Free the newly allocated
  3815. * hctxs and keep the previous q->nr_hw_queues.
  3816. */
  3817. if (i != set->nr_hw_queues) {
  3818. j = q->nr_hw_queues;
  3819. } else {
  3820. j = i;
  3821. q->nr_hw_queues = set->nr_hw_queues;
  3822. }
  3823. xa_for_each_start(&q->hctx_table, j, hctx, j)
  3824. blk_mq_exit_hctx(q, set, hctx, j);
  3825. mutex_unlock(&q->sysfs_lock);
  3826. /* unregister cpuhp callbacks for exited hctxs */
  3827. blk_mq_remove_hw_queues_cpuhp(q);
  3828. /* register cpuhp for new initialized hctxs */
  3829. blk_mq_add_hw_queues_cpuhp(q);
  3830. }
  3831. int blk_mq_init_allocated_queue(struct blk_mq_tag_set *set,
  3832. struct request_queue *q)
  3833. {
  3834. /* mark the queue as mq asap */
  3835. q->mq_ops = set->ops;
  3836. /*
  3837. * ->tag_set has to be setup before initialize hctx, which cpuphp
  3838. * handler needs it for checking queue mapping
  3839. */
  3840. q->tag_set = set;
  3841. if (blk_mq_alloc_ctxs(q))
  3842. goto err_exit;
  3843. /* init q->mq_kobj and sw queues' kobjects */
  3844. blk_mq_sysfs_init(q);
  3845. INIT_LIST_HEAD(&q->unused_hctx_list);
  3846. spin_lock_init(&q->unused_hctx_lock);
  3847. xa_init(&q->hctx_table);
  3848. blk_mq_realloc_hw_ctxs(set, q);
  3849. if (!q->nr_hw_queues)
  3850. goto err_hctxs;
  3851. INIT_WORK(&q->timeout_work, blk_mq_timeout_work);
  3852. blk_queue_rq_timeout(q, set->timeout ? set->timeout : 30 * HZ);
  3853. q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT;
  3854. INIT_DELAYED_WORK(&q->requeue_work, blk_mq_requeue_work);
  3855. INIT_LIST_HEAD(&q->flush_list);
  3856. INIT_LIST_HEAD(&q->requeue_list);
  3857. spin_lock_init(&q->requeue_lock);
  3858. q->nr_requests = set->queue_depth;
  3859. blk_mq_init_cpu_queues(q, set->nr_hw_queues);
  3860. blk_mq_add_queue_tag_set(set, q);
  3861. blk_mq_map_swqueue(q);
  3862. return 0;
  3863. err_hctxs:
  3864. blk_mq_release(q);
  3865. err_exit:
  3866. q->mq_ops = NULL;
  3867. return -ENOMEM;
  3868. }
  3869. EXPORT_SYMBOL(blk_mq_init_allocated_queue);
  3870. /* tags can _not_ be used after returning from blk_mq_exit_queue */
  3871. void blk_mq_exit_queue(struct request_queue *q)
  3872. {
  3873. struct blk_mq_tag_set *set = q->tag_set;
  3874. /* Checks hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED. */
  3875. blk_mq_exit_hw_queues(q, set, set->nr_hw_queues);
  3876. /* May clear BLK_MQ_F_TAG_QUEUE_SHARED in hctx->flags. */
  3877. blk_mq_del_queue_tag_set(q);
  3878. }
  3879. static int __blk_mq_alloc_rq_maps(struct blk_mq_tag_set *set)
  3880. {
  3881. int i;
  3882. if (blk_mq_is_shared_tags(set->flags)) {
  3883. set->shared_tags = blk_mq_alloc_map_and_rqs(set,
  3884. BLK_MQ_NO_HCTX_IDX,
  3885. set->queue_depth);
  3886. if (!set->shared_tags)
  3887. return -ENOMEM;
  3888. }
  3889. for (i = 0; i < set->nr_hw_queues; i++) {
  3890. if (!__blk_mq_alloc_map_and_rqs(set, i))
  3891. goto out_unwind;
  3892. cond_resched();
  3893. }
  3894. return 0;
  3895. out_unwind:
  3896. while (--i >= 0)
  3897. __blk_mq_free_map_and_rqs(set, i);
  3898. if (blk_mq_is_shared_tags(set->flags)) {
  3899. blk_mq_free_map_and_rqs(set, set->shared_tags,
  3900. BLK_MQ_NO_HCTX_IDX);
  3901. }
  3902. return -ENOMEM;
  3903. }
  3904. /*
  3905. * Allocate the request maps associated with this tag_set. Note that this
  3906. * may reduce the depth asked for, if memory is tight. set->queue_depth
  3907. * will be updated to reflect the allocated depth.
  3908. */
  3909. static int blk_mq_alloc_set_map_and_rqs(struct blk_mq_tag_set *set)
  3910. {
  3911. unsigned int depth;
  3912. int err;
  3913. depth = set->queue_depth;
  3914. do {
  3915. err = __blk_mq_alloc_rq_maps(set);
  3916. if (!err)
  3917. break;
  3918. set->queue_depth >>= 1;
  3919. if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN) {
  3920. err = -ENOMEM;
  3921. break;
  3922. }
  3923. } while (set->queue_depth);
  3924. if (!set->queue_depth || err) {
  3925. pr_err("blk-mq: failed to allocate request map\n");
  3926. return -ENOMEM;
  3927. }
  3928. if (depth != set->queue_depth)
  3929. pr_info("blk-mq: reduced tag depth (%u -> %u)\n",
  3930. depth, set->queue_depth);
  3931. return 0;
  3932. }
  3933. static void blk_mq_update_queue_map(struct blk_mq_tag_set *set)
  3934. {
  3935. /*
  3936. * blk_mq_map_queues() and multiple .map_queues() implementations
  3937. * expect that set->map[HCTX_TYPE_DEFAULT].nr_queues is set to the
  3938. * number of hardware queues.
  3939. */
  3940. if (set->nr_maps == 1)
  3941. set->map[HCTX_TYPE_DEFAULT].nr_queues = set->nr_hw_queues;
  3942. if (set->ops->map_queues) {
  3943. int i;
  3944. /*
  3945. * transport .map_queues is usually done in the following
  3946. * way:
  3947. *
  3948. * for (queue = 0; queue < set->nr_hw_queues; queue++) {
  3949. * mask = get_cpu_mask(queue)
  3950. * for_each_cpu(cpu, mask)
  3951. * set->map[x].mq_map[cpu] = queue;
  3952. * }
  3953. *
  3954. * When we need to remap, the table has to be cleared for
  3955. * killing stale mapping since one CPU may not be mapped
  3956. * to any hw queue.
  3957. */
  3958. for (i = 0; i < set->nr_maps; i++)
  3959. blk_mq_clear_mq_map(&set->map[i]);
  3960. set->ops->map_queues(set);
  3961. } else {
  3962. BUG_ON(set->nr_maps > 1);
  3963. blk_mq_map_queues(&set->map[HCTX_TYPE_DEFAULT]);
  3964. }
  3965. }
  3966. static int blk_mq_realloc_tag_set_tags(struct blk_mq_tag_set *set,
  3967. int new_nr_hw_queues)
  3968. {
  3969. struct blk_mq_tags **new_tags;
  3970. int i;
  3971. if (set->nr_hw_queues >= new_nr_hw_queues)
  3972. goto done;
  3973. new_tags = kcalloc_node(new_nr_hw_queues, sizeof(struct blk_mq_tags *),
  3974. GFP_KERNEL, set->numa_node);
  3975. if (!new_tags)
  3976. return -ENOMEM;
  3977. if (set->tags)
  3978. memcpy(new_tags, set->tags, set->nr_hw_queues *
  3979. sizeof(*set->tags));
  3980. kfree(set->tags);
  3981. set->tags = new_tags;
  3982. for (i = set->nr_hw_queues; i < new_nr_hw_queues; i++) {
  3983. if (!__blk_mq_alloc_map_and_rqs(set, i)) {
  3984. while (--i >= set->nr_hw_queues)
  3985. __blk_mq_free_map_and_rqs(set, i);
  3986. return -ENOMEM;
  3987. }
  3988. cond_resched();
  3989. }
  3990. done:
  3991. set->nr_hw_queues = new_nr_hw_queues;
  3992. return 0;
  3993. }
  3994. /*
  3995. * Alloc a tag set to be associated with one or more request queues.
  3996. * May fail with EINVAL for various error conditions. May adjust the
  3997. * requested depth down, if it's too large. In that case, the set
  3998. * value will be stored in set->queue_depth.
  3999. */
  4000. int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set)
  4001. {
  4002. int i, ret;
  4003. BUILD_BUG_ON(BLK_MQ_MAX_DEPTH > 1 << BLK_MQ_UNIQUE_TAG_BITS);
  4004. if (!set->nr_hw_queues)
  4005. return -EINVAL;
  4006. if (!set->queue_depth)
  4007. return -EINVAL;
  4008. if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN)
  4009. return -EINVAL;
  4010. if (!set->ops->queue_rq)
  4011. return -EINVAL;
  4012. if (!set->ops->get_budget ^ !set->ops->put_budget)
  4013. return -EINVAL;
  4014. if (set->queue_depth > BLK_MQ_MAX_DEPTH) {
  4015. pr_info("blk-mq: reduced tag depth to %u\n",
  4016. BLK_MQ_MAX_DEPTH);
  4017. set->queue_depth = BLK_MQ_MAX_DEPTH;
  4018. }
  4019. if (!set->nr_maps)
  4020. set->nr_maps = 1;
  4021. else if (set->nr_maps > HCTX_MAX_TYPES)
  4022. return -EINVAL;
  4023. /*
  4024. * If a crashdump is active, then we are potentially in a very
  4025. * memory constrained environment. Limit us to 64 tags to prevent
  4026. * using too much memory.
  4027. */
  4028. if (is_kdump_kernel())
  4029. set->queue_depth = min(64U, set->queue_depth);
  4030. /*
  4031. * There is no use for more h/w queues than cpus if we just have
  4032. * a single map
  4033. */
  4034. if (set->nr_maps == 1 && set->nr_hw_queues > nr_cpu_ids)
  4035. set->nr_hw_queues = nr_cpu_ids;
  4036. if (set->flags & BLK_MQ_F_BLOCKING) {
  4037. set->srcu = kmalloc(sizeof(*set->srcu), GFP_KERNEL);
  4038. if (!set->srcu)
  4039. return -ENOMEM;
  4040. ret = init_srcu_struct(set->srcu);
  4041. if (ret)
  4042. goto out_free_srcu;
  4043. }
  4044. ret = -ENOMEM;
  4045. set->tags = kcalloc_node(set->nr_hw_queues,
  4046. sizeof(struct blk_mq_tags *), GFP_KERNEL,
  4047. set->numa_node);
  4048. if (!set->tags)
  4049. goto out_cleanup_srcu;
  4050. for (i = 0; i < set->nr_maps; i++) {
  4051. set->map[i].mq_map = kcalloc_node(nr_cpu_ids,
  4052. sizeof(set->map[i].mq_map[0]),
  4053. GFP_KERNEL, set->numa_node);
  4054. if (!set->map[i].mq_map)
  4055. goto out_free_mq_map;
  4056. set->map[i].nr_queues = set->nr_hw_queues;
  4057. }
  4058. blk_mq_update_queue_map(set);
  4059. ret = blk_mq_alloc_set_map_and_rqs(set);
  4060. if (ret)
  4061. goto out_free_mq_map;
  4062. mutex_init(&set->tag_list_lock);
  4063. INIT_LIST_HEAD(&set->tag_list);
  4064. return 0;
  4065. out_free_mq_map:
  4066. for (i = 0; i < set->nr_maps; i++) {
  4067. kfree(set->map[i].mq_map);
  4068. set->map[i].mq_map = NULL;
  4069. }
  4070. kfree(set->tags);
  4071. set->tags = NULL;
  4072. out_cleanup_srcu:
  4073. if (set->flags & BLK_MQ_F_BLOCKING)
  4074. cleanup_srcu_struct(set->srcu);
  4075. out_free_srcu:
  4076. if (set->flags & BLK_MQ_F_BLOCKING)
  4077. kfree(set->srcu);
  4078. return ret;
  4079. }
  4080. EXPORT_SYMBOL(blk_mq_alloc_tag_set);
  4081. /* allocate and initialize a tagset for a simple single-queue device */
  4082. int blk_mq_alloc_sq_tag_set(struct blk_mq_tag_set *set,
  4083. const struct blk_mq_ops *ops, unsigned int queue_depth,
  4084. unsigned int set_flags)
  4085. {
  4086. memset(set, 0, sizeof(*set));
  4087. set->ops = ops;
  4088. set->nr_hw_queues = 1;
  4089. set->nr_maps = 1;
  4090. set->queue_depth = queue_depth;
  4091. set->numa_node = NUMA_NO_NODE;
  4092. set->flags = set_flags;
  4093. return blk_mq_alloc_tag_set(set);
  4094. }
  4095. EXPORT_SYMBOL_GPL(blk_mq_alloc_sq_tag_set);
  4096. void blk_mq_free_tag_set(struct blk_mq_tag_set *set)
  4097. {
  4098. int i, j;
  4099. for (i = 0; i < set->nr_hw_queues; i++)
  4100. __blk_mq_free_map_and_rqs(set, i);
  4101. if (blk_mq_is_shared_tags(set->flags)) {
  4102. blk_mq_free_map_and_rqs(set, set->shared_tags,
  4103. BLK_MQ_NO_HCTX_IDX);
  4104. }
  4105. for (j = 0; j < set->nr_maps; j++) {
  4106. kfree(set->map[j].mq_map);
  4107. set->map[j].mq_map = NULL;
  4108. }
  4109. kfree(set->tags);
  4110. set->tags = NULL;
  4111. if (set->flags & BLK_MQ_F_BLOCKING) {
  4112. cleanup_srcu_struct(set->srcu);
  4113. kfree(set->srcu);
  4114. }
  4115. }
  4116. EXPORT_SYMBOL(blk_mq_free_tag_set);
  4117. int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr)
  4118. {
  4119. struct blk_mq_tag_set *set = q->tag_set;
  4120. struct blk_mq_hw_ctx *hctx;
  4121. int ret;
  4122. unsigned long i;
  4123. if (WARN_ON_ONCE(!q->mq_freeze_depth))
  4124. return -EINVAL;
  4125. if (!set)
  4126. return -EINVAL;
  4127. if (q->nr_requests == nr)
  4128. return 0;
  4129. blk_mq_quiesce_queue(q);
  4130. ret = 0;
  4131. queue_for_each_hw_ctx(q, hctx, i) {
  4132. if (!hctx->tags)
  4133. continue;
  4134. /*
  4135. * If we're using an MQ scheduler, just update the scheduler
  4136. * queue depth. This is similar to what the old code would do.
  4137. */
  4138. if (hctx->sched_tags) {
  4139. ret = blk_mq_tag_update_depth(hctx, &hctx->sched_tags,
  4140. nr, true);
  4141. } else {
  4142. ret = blk_mq_tag_update_depth(hctx, &hctx->tags, nr,
  4143. false);
  4144. }
  4145. if (ret)
  4146. break;
  4147. if (q->elevator && q->elevator->type->ops.depth_updated)
  4148. q->elevator->type->ops.depth_updated(hctx);
  4149. }
  4150. if (!ret) {
  4151. q->nr_requests = nr;
  4152. if (blk_mq_is_shared_tags(set->flags)) {
  4153. if (q->elevator)
  4154. blk_mq_tag_update_sched_shared_tags(q);
  4155. else
  4156. blk_mq_tag_resize_shared_tags(set, nr);
  4157. }
  4158. }
  4159. blk_mq_unquiesce_queue(q);
  4160. return ret;
  4161. }
  4162. /*
  4163. * request_queue and elevator_type pair.
  4164. * It is just used by __blk_mq_update_nr_hw_queues to cache
  4165. * the elevator_type associated with a request_queue.
  4166. */
  4167. struct blk_mq_qe_pair {
  4168. struct list_head node;
  4169. struct request_queue *q;
  4170. struct elevator_type *type;
  4171. };
  4172. /*
  4173. * Cache the elevator_type in qe pair list and switch the
  4174. * io scheduler to 'none'
  4175. */
  4176. static bool blk_mq_elv_switch_none(struct list_head *head,
  4177. struct request_queue *q)
  4178. {
  4179. struct blk_mq_qe_pair *qe;
  4180. qe = kmalloc(sizeof(*qe), GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY);
  4181. if (!qe)
  4182. return false;
  4183. /* q->elevator needs protection from ->sysfs_lock */
  4184. mutex_lock(&q->sysfs_lock);
  4185. /* the check has to be done with holding sysfs_lock */
  4186. if (!q->elevator) {
  4187. kfree(qe);
  4188. goto unlock;
  4189. }
  4190. INIT_LIST_HEAD(&qe->node);
  4191. qe->q = q;
  4192. qe->type = q->elevator->type;
  4193. /* keep a reference to the elevator module as we'll switch back */
  4194. __elevator_get(qe->type);
  4195. list_add(&qe->node, head);
  4196. elevator_disable(q);
  4197. unlock:
  4198. mutex_unlock(&q->sysfs_lock);
  4199. return true;
  4200. }
  4201. static struct blk_mq_qe_pair *blk_lookup_qe_pair(struct list_head *head,
  4202. struct request_queue *q)
  4203. {
  4204. struct blk_mq_qe_pair *qe;
  4205. list_for_each_entry(qe, head, node)
  4206. if (qe->q == q)
  4207. return qe;
  4208. return NULL;
  4209. }
  4210. static void blk_mq_elv_switch_back(struct list_head *head,
  4211. struct request_queue *q)
  4212. {
  4213. struct blk_mq_qe_pair *qe;
  4214. struct elevator_type *t;
  4215. qe = blk_lookup_qe_pair(head, q);
  4216. if (!qe)
  4217. return;
  4218. t = qe->type;
  4219. list_del(&qe->node);
  4220. kfree(qe);
  4221. mutex_lock(&q->sysfs_lock);
  4222. elevator_switch(q, t);
  4223. /* drop the reference acquired in blk_mq_elv_switch_none */
  4224. elevator_put(t);
  4225. mutex_unlock(&q->sysfs_lock);
  4226. }
  4227. static void __blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set,
  4228. int nr_hw_queues)
  4229. {
  4230. struct request_queue *q;
  4231. LIST_HEAD(head);
  4232. int prev_nr_hw_queues = set->nr_hw_queues;
  4233. int i;
  4234. lockdep_assert_held(&set->tag_list_lock);
  4235. if (set->nr_maps == 1 && nr_hw_queues > nr_cpu_ids)
  4236. nr_hw_queues = nr_cpu_ids;
  4237. if (nr_hw_queues < 1)
  4238. return;
  4239. if (set->nr_maps == 1 && nr_hw_queues == set->nr_hw_queues)
  4240. return;
  4241. list_for_each_entry(q, &set->tag_list, tag_set_list)
  4242. blk_mq_freeze_queue(q);
  4243. /*
  4244. * Switch IO scheduler to 'none', cleaning up the data associated
  4245. * with the previous scheduler. We will switch back once we are done
  4246. * updating the new sw to hw queue mappings.
  4247. */
  4248. list_for_each_entry(q, &set->tag_list, tag_set_list)
  4249. if (!blk_mq_elv_switch_none(&head, q))
  4250. goto switch_back;
  4251. list_for_each_entry(q, &set->tag_list, tag_set_list) {
  4252. blk_mq_debugfs_unregister_hctxs(q);
  4253. blk_mq_sysfs_unregister_hctxs(q);
  4254. }
  4255. if (blk_mq_realloc_tag_set_tags(set, nr_hw_queues) < 0)
  4256. goto reregister;
  4257. fallback:
  4258. blk_mq_update_queue_map(set);
  4259. list_for_each_entry(q, &set->tag_list, tag_set_list) {
  4260. blk_mq_realloc_hw_ctxs(set, q);
  4261. if (q->nr_hw_queues != set->nr_hw_queues) {
  4262. int i = prev_nr_hw_queues;
  4263. pr_warn("Increasing nr_hw_queues to %d fails, fallback to %d\n",
  4264. nr_hw_queues, prev_nr_hw_queues);
  4265. for (; i < set->nr_hw_queues; i++)
  4266. __blk_mq_free_map_and_rqs(set, i);
  4267. set->nr_hw_queues = prev_nr_hw_queues;
  4268. goto fallback;
  4269. }
  4270. blk_mq_map_swqueue(q);
  4271. }
  4272. reregister:
  4273. list_for_each_entry(q, &set->tag_list, tag_set_list) {
  4274. blk_mq_sysfs_register_hctxs(q);
  4275. blk_mq_debugfs_register_hctxs(q);
  4276. }
  4277. switch_back:
  4278. list_for_each_entry(q, &set->tag_list, tag_set_list)
  4279. blk_mq_elv_switch_back(&head, q);
  4280. list_for_each_entry(q, &set->tag_list, tag_set_list)
  4281. blk_mq_unfreeze_queue(q);
  4282. /* Free the excess tags when nr_hw_queues shrink. */
  4283. for (i = set->nr_hw_queues; i < prev_nr_hw_queues; i++)
  4284. __blk_mq_free_map_and_rqs(set, i);
  4285. }
  4286. void blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set, int nr_hw_queues)
  4287. {
  4288. mutex_lock(&set->tag_list_lock);
  4289. __blk_mq_update_nr_hw_queues(set, nr_hw_queues);
  4290. mutex_unlock(&set->tag_list_lock);
  4291. }
  4292. EXPORT_SYMBOL_GPL(blk_mq_update_nr_hw_queues);
  4293. static int blk_hctx_poll(struct request_queue *q, struct blk_mq_hw_ctx *hctx,
  4294. struct io_comp_batch *iob, unsigned int flags)
  4295. {
  4296. long state = get_current_state();
  4297. int ret;
  4298. do {
  4299. ret = q->mq_ops->poll(hctx, iob);
  4300. if (ret > 0) {
  4301. __set_current_state(TASK_RUNNING);
  4302. return ret;
  4303. }
  4304. if (signal_pending_state(state, current))
  4305. __set_current_state(TASK_RUNNING);
  4306. if (task_is_running(current))
  4307. return 1;
  4308. if (ret < 0 || (flags & BLK_POLL_ONESHOT))
  4309. break;
  4310. cpu_relax();
  4311. } while (!need_resched());
  4312. __set_current_state(TASK_RUNNING);
  4313. return 0;
  4314. }
  4315. int blk_mq_poll(struct request_queue *q, blk_qc_t cookie,
  4316. struct io_comp_batch *iob, unsigned int flags)
  4317. {
  4318. if (!blk_mq_can_poll(q))
  4319. return 0;
  4320. return blk_hctx_poll(q, xa_load(&q->hctx_table, cookie), iob, flags);
  4321. }
  4322. int blk_rq_poll(struct request *rq, struct io_comp_batch *iob,
  4323. unsigned int poll_flags)
  4324. {
  4325. struct request_queue *q = rq->q;
  4326. int ret;
  4327. if (!blk_rq_is_poll(rq))
  4328. return 0;
  4329. if (!percpu_ref_tryget(&q->q_usage_counter))
  4330. return 0;
  4331. ret = blk_hctx_poll(q, rq->mq_hctx, iob, poll_flags);
  4332. blk_queue_exit(q);
  4333. return ret;
  4334. }
  4335. EXPORT_SYMBOL_GPL(blk_rq_poll);
  4336. unsigned int blk_mq_rq_cpu(struct request *rq)
  4337. {
  4338. return rq->mq_ctx->cpu;
  4339. }
  4340. EXPORT_SYMBOL(blk_mq_rq_cpu);
  4341. void blk_mq_cancel_work_sync(struct request_queue *q)
  4342. {
  4343. struct blk_mq_hw_ctx *hctx;
  4344. unsigned long i;
  4345. cancel_delayed_work_sync(&q->requeue_work);
  4346. queue_for_each_hw_ctx(q, hctx, i)
  4347. cancel_delayed_work_sync(&hctx->run_work);
  4348. }
  4349. static int __init blk_mq_init(void)
  4350. {
  4351. int i;
  4352. for_each_possible_cpu(i)
  4353. init_llist_head(&per_cpu(blk_cpu_done, i));
  4354. for_each_possible_cpu(i)
  4355. INIT_CSD(&per_cpu(blk_cpu_csd, i),
  4356. __blk_mq_complete_request_remote, NULL);
  4357. open_softirq(BLOCK_SOFTIRQ, blk_done_softirq);
  4358. cpuhp_setup_state_nocalls(CPUHP_BLOCK_SOFTIRQ_DEAD,
  4359. "block/softirq:dead", NULL,
  4360. blk_softirq_cpu_dead);
  4361. cpuhp_setup_state_multi(CPUHP_BLK_MQ_DEAD, "block/mq:dead", NULL,
  4362. blk_mq_hctx_notify_dead);
  4363. cpuhp_setup_state_multi(CPUHP_AP_BLK_MQ_ONLINE, "block/mq:online",
  4364. blk_mq_hctx_notify_online,
  4365. blk_mq_hctx_notify_offline);
  4366. return 0;
  4367. }
  4368. subsys_initcall(blk_mq_init);