blk-core.c 99 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756
  1. /*
  2. * Copyright (C) 1991, 1992 Linus Torvalds
  3. * Copyright (C) 1994, Karl Keyte: Added support for disk statistics
  4. * Elevator latency, (C) 2000 Andrea Arcangeli <andrea@suse.de> SuSE
  5. * Queue request tables / lock, selectable elevator, Jens Axboe <axboe@suse.de>
  6. * kernel-doc documentation started by NeilBrown <neilb@cse.unsw.edu.au>
  7. * - July2000
  8. * bio rewrite, highmem i/o, etc, Jens Axboe <axboe@suse.de> - may 2001
  9. */
  10. /*
  11. * This handles all read/write requests to block devices
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/backing-dev.h>
  16. #include <linux/bio.h>
  17. #include <linux/blkdev.h>
  18. #include <linux/blk-mq.h>
  19. #include <linux/highmem.h>
  20. #include <linux/mm.h>
  21. #include <linux/kernel_stat.h>
  22. #include <linux/string.h>
  23. #include <linux/init.h>
  24. #include <linux/completion.h>
  25. #include <linux/slab.h>
  26. #include <linux/swap.h>
  27. #include <linux/writeback.h>
  28. #include <linux/task_io_accounting_ops.h>
  29. #include <linux/fault-inject.h>
  30. #include <linux/list_sort.h>
  31. #include <linux/delay.h>
  32. #include <linux/ratelimit.h>
  33. #include <linux/pm_runtime.h>
  34. #include <linux/blk-cgroup.h>
  35. #include <linux/debugfs.h>
  36. #include <linux/bpf.h>
  37. #define CREATE_TRACE_POINTS
  38. #include <trace/events/block.h>
  39. #include "blk.h"
  40. #include "blk-mq.h"
  41. #include "blk-mq-sched.h"
  42. #include "blk-pm.h"
  43. #include "blk-rq-qos.h"
  44. #ifdef CONFIG_DEBUG_FS
  45. struct dentry *blk_debugfs_root;
  46. #endif
  47. EXPORT_TRACEPOINT_SYMBOL_GPL(block_bio_remap);
  48. EXPORT_TRACEPOINT_SYMBOL_GPL(block_rq_remap);
  49. EXPORT_TRACEPOINT_SYMBOL_GPL(block_bio_complete);
  50. EXPORT_TRACEPOINT_SYMBOL_GPL(block_split);
  51. EXPORT_TRACEPOINT_SYMBOL_GPL(block_unplug);
  52. DEFINE_IDA(blk_queue_ida);
  53. /*
  54. * For the allocated request tables
  55. */
  56. struct kmem_cache *request_cachep;
  57. /*
  58. * For queue allocation
  59. */
  60. struct kmem_cache *blk_requestq_cachep;
  61. /*
  62. * Controlling structure to kblockd
  63. */
  64. static struct workqueue_struct *kblockd_workqueue;
  65. /**
  66. * blk_queue_flag_set - atomically set a queue flag
  67. * @flag: flag to be set
  68. * @q: request queue
  69. */
  70. void blk_queue_flag_set(unsigned int flag, struct request_queue *q)
  71. {
  72. unsigned long flags;
  73. spin_lock_irqsave(q->queue_lock, flags);
  74. queue_flag_set(flag, q);
  75. spin_unlock_irqrestore(q->queue_lock, flags);
  76. }
  77. EXPORT_SYMBOL(blk_queue_flag_set);
  78. /**
  79. * blk_queue_flag_clear - atomically clear a queue flag
  80. * @flag: flag to be cleared
  81. * @q: request queue
  82. */
  83. void blk_queue_flag_clear(unsigned int flag, struct request_queue *q)
  84. {
  85. unsigned long flags;
  86. spin_lock_irqsave(q->queue_lock, flags);
  87. queue_flag_clear(flag, q);
  88. spin_unlock_irqrestore(q->queue_lock, flags);
  89. }
  90. EXPORT_SYMBOL(blk_queue_flag_clear);
  91. /**
  92. * blk_queue_flag_test_and_set - atomically test and set a queue flag
  93. * @flag: flag to be set
  94. * @q: request queue
  95. *
  96. * Returns the previous value of @flag - 0 if the flag was not set and 1 if
  97. * the flag was already set.
  98. */
  99. bool blk_queue_flag_test_and_set(unsigned int flag, struct request_queue *q)
  100. {
  101. unsigned long flags;
  102. bool res;
  103. spin_lock_irqsave(q->queue_lock, flags);
  104. res = queue_flag_test_and_set(flag, q);
  105. spin_unlock_irqrestore(q->queue_lock, flags);
  106. return res;
  107. }
  108. EXPORT_SYMBOL_GPL(blk_queue_flag_test_and_set);
  109. /**
  110. * blk_queue_flag_test_and_clear - atomically test and clear a queue flag
  111. * @flag: flag to be cleared
  112. * @q: request queue
  113. *
  114. * Returns the previous value of @flag - 0 if the flag was not set and 1 if
  115. * the flag was set.
  116. */
  117. bool blk_queue_flag_test_and_clear(unsigned int flag, struct request_queue *q)
  118. {
  119. unsigned long flags;
  120. bool res;
  121. spin_lock_irqsave(q->queue_lock, flags);
  122. res = queue_flag_test_and_clear(flag, q);
  123. spin_unlock_irqrestore(q->queue_lock, flags);
  124. return res;
  125. }
  126. EXPORT_SYMBOL_GPL(blk_queue_flag_test_and_clear);
  127. static void blk_clear_congested(struct request_list *rl, int sync)
  128. {
  129. #ifdef CONFIG_CGROUP_WRITEBACK
  130. clear_wb_congested(rl->blkg->wb_congested, sync);
  131. #else
  132. /*
  133. * If !CGROUP_WRITEBACK, all blkg's map to bdi->wb and we shouldn't
  134. * flip its congestion state for events on other blkcgs.
  135. */
  136. if (rl == &rl->q->root_rl)
  137. clear_wb_congested(rl->q->backing_dev_info->wb.congested, sync);
  138. #endif
  139. }
  140. static void blk_set_congested(struct request_list *rl, int sync)
  141. {
  142. #ifdef CONFIG_CGROUP_WRITEBACK
  143. set_wb_congested(rl->blkg->wb_congested, sync);
  144. #else
  145. /* see blk_clear_congested() */
  146. if (rl == &rl->q->root_rl)
  147. set_wb_congested(rl->q->backing_dev_info->wb.congested, sync);
  148. #endif
  149. }
  150. void blk_queue_congestion_threshold(struct request_queue *q)
  151. {
  152. int nr;
  153. nr = q->nr_requests - (q->nr_requests / 8) + 1;
  154. if (nr > q->nr_requests)
  155. nr = q->nr_requests;
  156. q->nr_congestion_on = nr;
  157. nr = q->nr_requests - (q->nr_requests / 8) - (q->nr_requests / 16) - 1;
  158. if (nr < 1)
  159. nr = 1;
  160. q->nr_congestion_off = nr;
  161. }
  162. void blk_rq_init(struct request_queue *q, struct request *rq)
  163. {
  164. memset(rq, 0, sizeof(*rq));
  165. INIT_LIST_HEAD(&rq->queuelist);
  166. INIT_LIST_HEAD(&rq->timeout_list);
  167. rq->cpu = -1;
  168. rq->q = q;
  169. rq->__sector = (sector_t) -1;
  170. INIT_HLIST_NODE(&rq->hash);
  171. RB_CLEAR_NODE(&rq->rb_node);
  172. rq->tag = -1;
  173. rq->internal_tag = -1;
  174. rq->start_time_ns = ktime_get_ns();
  175. rq->part = NULL;
  176. }
  177. EXPORT_SYMBOL(blk_rq_init);
  178. static const struct {
  179. int errno;
  180. const char *name;
  181. } blk_errors[] = {
  182. [BLK_STS_OK] = { 0, "" },
  183. [BLK_STS_NOTSUPP] = { -EOPNOTSUPP, "operation not supported" },
  184. [BLK_STS_TIMEOUT] = { -ETIMEDOUT, "timeout" },
  185. [BLK_STS_NOSPC] = { -ENOSPC, "critical space allocation" },
  186. [BLK_STS_TRANSPORT] = { -ENOLINK, "recoverable transport" },
  187. [BLK_STS_TARGET] = { -EREMOTEIO, "critical target" },
  188. [BLK_STS_NEXUS] = { -EBADE, "critical nexus" },
  189. [BLK_STS_MEDIUM] = { -ENODATA, "critical medium" },
  190. [BLK_STS_PROTECTION] = { -EILSEQ, "protection" },
  191. [BLK_STS_RESOURCE] = { -ENOMEM, "kernel resource" },
  192. [BLK_STS_DEV_RESOURCE] = { -EBUSY, "device resource" },
  193. [BLK_STS_AGAIN] = { -EAGAIN, "nonblocking retry" },
  194. /* device mapper special case, should not leak out: */
  195. [BLK_STS_DM_REQUEUE] = { -EREMCHG, "dm internal retry" },
  196. /* everything else not covered above: */
  197. [BLK_STS_IOERR] = { -EIO, "I/O" },
  198. };
  199. blk_status_t errno_to_blk_status(int errno)
  200. {
  201. int i;
  202. for (i = 0; i < ARRAY_SIZE(blk_errors); i++) {
  203. if (blk_errors[i].errno == errno)
  204. return (__force blk_status_t)i;
  205. }
  206. return BLK_STS_IOERR;
  207. }
  208. EXPORT_SYMBOL_GPL(errno_to_blk_status);
  209. int blk_status_to_errno(blk_status_t status)
  210. {
  211. int idx = (__force int)status;
  212. if (WARN_ON_ONCE(idx >= ARRAY_SIZE(blk_errors)))
  213. return -EIO;
  214. return blk_errors[idx].errno;
  215. }
  216. EXPORT_SYMBOL_GPL(blk_status_to_errno);
  217. static void print_req_error(struct request *req, blk_status_t status)
  218. {
  219. int idx = (__force int)status;
  220. if (WARN_ON_ONCE(idx >= ARRAY_SIZE(blk_errors)))
  221. return;
  222. printk_ratelimited(KERN_ERR "%s: %s error, dev %s, sector %llu\n",
  223. __func__, blk_errors[idx].name, req->rq_disk ?
  224. req->rq_disk->disk_name : "?",
  225. (unsigned long long)blk_rq_pos(req));
  226. }
  227. static void req_bio_endio(struct request *rq, struct bio *bio,
  228. unsigned int nbytes, blk_status_t error)
  229. {
  230. if (error)
  231. bio->bi_status = error;
  232. if (unlikely(rq->rq_flags & RQF_QUIET))
  233. bio_set_flag(bio, BIO_QUIET);
  234. bio_advance(bio, nbytes);
  235. /* don't actually finish bio if it's part of flush sequence */
  236. if (bio->bi_iter.bi_size == 0 && !(rq->rq_flags & RQF_FLUSH_SEQ))
  237. bio_endio(bio);
  238. }
  239. void blk_dump_rq_flags(struct request *rq, char *msg)
  240. {
  241. printk(KERN_INFO "%s: dev %s: flags=%llx\n", msg,
  242. rq->rq_disk ? rq->rq_disk->disk_name : "?",
  243. (unsigned long long) rq->cmd_flags);
  244. printk(KERN_INFO " sector %llu, nr/cnr %u/%u\n",
  245. (unsigned long long)blk_rq_pos(rq),
  246. blk_rq_sectors(rq), blk_rq_cur_sectors(rq));
  247. printk(KERN_INFO " bio %p, biotail %p, len %u\n",
  248. rq->bio, rq->biotail, blk_rq_bytes(rq));
  249. }
  250. EXPORT_SYMBOL(blk_dump_rq_flags);
  251. static void blk_delay_work(struct work_struct *work)
  252. {
  253. struct request_queue *q;
  254. q = container_of(work, struct request_queue, delay_work.work);
  255. spin_lock_irq(q->queue_lock);
  256. __blk_run_queue(q);
  257. spin_unlock_irq(q->queue_lock);
  258. }
  259. /**
  260. * blk_delay_queue - restart queueing after defined interval
  261. * @q: The &struct request_queue in question
  262. * @msecs: Delay in msecs
  263. *
  264. * Description:
  265. * Sometimes queueing needs to be postponed for a little while, to allow
  266. * resources to come back. This function will make sure that queueing is
  267. * restarted around the specified time.
  268. */
  269. void blk_delay_queue(struct request_queue *q, unsigned long msecs)
  270. {
  271. lockdep_assert_held(q->queue_lock);
  272. WARN_ON_ONCE(q->mq_ops);
  273. if (likely(!blk_queue_dead(q)))
  274. queue_delayed_work(kblockd_workqueue, &q->delay_work,
  275. msecs_to_jiffies(msecs));
  276. }
  277. EXPORT_SYMBOL(blk_delay_queue);
  278. /**
  279. * blk_start_queue_async - asynchronously restart a previously stopped queue
  280. * @q: The &struct request_queue in question
  281. *
  282. * Description:
  283. * blk_start_queue_async() will clear the stop flag on the queue, and
  284. * ensure that the request_fn for the queue is run from an async
  285. * context.
  286. **/
  287. void blk_start_queue_async(struct request_queue *q)
  288. {
  289. lockdep_assert_held(q->queue_lock);
  290. WARN_ON_ONCE(q->mq_ops);
  291. queue_flag_clear(QUEUE_FLAG_STOPPED, q);
  292. blk_run_queue_async(q);
  293. }
  294. EXPORT_SYMBOL(blk_start_queue_async);
  295. /**
  296. * blk_start_queue - restart a previously stopped queue
  297. * @q: The &struct request_queue in question
  298. *
  299. * Description:
  300. * blk_start_queue() will clear the stop flag on the queue, and call
  301. * the request_fn for the queue if it was in a stopped state when
  302. * entered. Also see blk_stop_queue().
  303. **/
  304. void blk_start_queue(struct request_queue *q)
  305. {
  306. lockdep_assert_held(q->queue_lock);
  307. WARN_ON_ONCE(q->mq_ops);
  308. queue_flag_clear(QUEUE_FLAG_STOPPED, q);
  309. __blk_run_queue(q);
  310. }
  311. EXPORT_SYMBOL(blk_start_queue);
  312. /**
  313. * blk_stop_queue - stop a queue
  314. * @q: The &struct request_queue in question
  315. *
  316. * Description:
  317. * The Linux block layer assumes that a block driver will consume all
  318. * entries on the request queue when the request_fn strategy is called.
  319. * Often this will not happen, because of hardware limitations (queue
  320. * depth settings). If a device driver gets a 'queue full' response,
  321. * or if it simply chooses not to queue more I/O at one point, it can
  322. * call this function to prevent the request_fn from being called until
  323. * the driver has signalled it's ready to go again. This happens by calling
  324. * blk_start_queue() to restart queue operations.
  325. **/
  326. void blk_stop_queue(struct request_queue *q)
  327. {
  328. lockdep_assert_held(q->queue_lock);
  329. WARN_ON_ONCE(q->mq_ops);
  330. cancel_delayed_work(&q->delay_work);
  331. queue_flag_set(QUEUE_FLAG_STOPPED, q);
  332. }
  333. EXPORT_SYMBOL(blk_stop_queue);
  334. /**
  335. * blk_sync_queue - cancel any pending callbacks on a queue
  336. * @q: the queue
  337. *
  338. * Description:
  339. * The block layer may perform asynchronous callback activity
  340. * on a queue, such as calling the unplug function after a timeout.
  341. * A block device may call blk_sync_queue to ensure that any
  342. * such activity is cancelled, thus allowing it to release resources
  343. * that the callbacks might use. The caller must already have made sure
  344. * that its ->make_request_fn will not re-add plugging prior to calling
  345. * this function.
  346. *
  347. * This function does not cancel any asynchronous activity arising
  348. * out of elevator or throttling code. That would require elevator_exit()
  349. * and blkcg_exit_queue() to be called with queue lock initialized.
  350. *
  351. */
  352. void blk_sync_queue(struct request_queue *q)
  353. {
  354. del_timer_sync(&q->timeout);
  355. cancel_work_sync(&q->timeout_work);
  356. if (q->mq_ops) {
  357. struct blk_mq_hw_ctx *hctx;
  358. int i;
  359. cancel_delayed_work_sync(&q->requeue_work);
  360. queue_for_each_hw_ctx(q, hctx, i)
  361. cancel_delayed_work_sync(&hctx->run_work);
  362. } else {
  363. cancel_delayed_work_sync(&q->delay_work);
  364. }
  365. }
  366. EXPORT_SYMBOL(blk_sync_queue);
  367. /**
  368. * blk_set_pm_only - increment pm_only counter
  369. * @q: request queue pointer
  370. */
  371. void blk_set_pm_only(struct request_queue *q)
  372. {
  373. atomic_inc(&q->pm_only);
  374. }
  375. EXPORT_SYMBOL_GPL(blk_set_pm_only);
  376. void blk_clear_pm_only(struct request_queue *q)
  377. {
  378. int pm_only;
  379. pm_only = atomic_dec_return(&q->pm_only);
  380. WARN_ON_ONCE(pm_only < 0);
  381. if (pm_only == 0)
  382. wake_up_all(&q->mq_freeze_wq);
  383. }
  384. EXPORT_SYMBOL_GPL(blk_clear_pm_only);
  385. /**
  386. * __blk_run_queue_uncond - run a queue whether or not it has been stopped
  387. * @q: The queue to run
  388. *
  389. * Description:
  390. * Invoke request handling on a queue if there are any pending requests.
  391. * May be used to restart request handling after a request has completed.
  392. * This variant runs the queue whether or not the queue has been
  393. * stopped. Must be called with the queue lock held and interrupts
  394. * disabled. See also @blk_run_queue.
  395. */
  396. inline void __blk_run_queue_uncond(struct request_queue *q)
  397. {
  398. lockdep_assert_held(q->queue_lock);
  399. WARN_ON_ONCE(q->mq_ops);
  400. if (unlikely(blk_queue_dead(q)))
  401. return;
  402. /*
  403. * Some request_fn implementations, e.g. scsi_request_fn(), unlock
  404. * the queue lock internally. As a result multiple threads may be
  405. * running such a request function concurrently. Keep track of the
  406. * number of active request_fn invocations such that blk_drain_queue()
  407. * can wait until all these request_fn calls have finished.
  408. */
  409. q->request_fn_active++;
  410. q->request_fn(q);
  411. q->request_fn_active--;
  412. }
  413. EXPORT_SYMBOL_GPL(__blk_run_queue_uncond);
  414. /**
  415. * __blk_run_queue - run a single device queue
  416. * @q: The queue to run
  417. *
  418. * Description:
  419. * See @blk_run_queue.
  420. */
  421. void __blk_run_queue(struct request_queue *q)
  422. {
  423. lockdep_assert_held(q->queue_lock);
  424. WARN_ON_ONCE(q->mq_ops);
  425. if (unlikely(blk_queue_stopped(q)))
  426. return;
  427. __blk_run_queue_uncond(q);
  428. }
  429. EXPORT_SYMBOL(__blk_run_queue);
  430. /**
  431. * blk_run_queue_async - run a single device queue in workqueue context
  432. * @q: The queue to run
  433. *
  434. * Description:
  435. * Tells kblockd to perform the equivalent of @blk_run_queue on behalf
  436. * of us.
  437. *
  438. * Note:
  439. * Since it is not allowed to run q->delay_work after blk_cleanup_queue()
  440. * has canceled q->delay_work, callers must hold the queue lock to avoid
  441. * race conditions between blk_cleanup_queue() and blk_run_queue_async().
  442. */
  443. void blk_run_queue_async(struct request_queue *q)
  444. {
  445. lockdep_assert_held(q->queue_lock);
  446. WARN_ON_ONCE(q->mq_ops);
  447. if (likely(!blk_queue_stopped(q) && !blk_queue_dead(q)))
  448. mod_delayed_work(kblockd_workqueue, &q->delay_work, 0);
  449. }
  450. EXPORT_SYMBOL(blk_run_queue_async);
  451. /**
  452. * blk_run_queue - run a single device queue
  453. * @q: The queue to run
  454. *
  455. * Description:
  456. * Invoke request handling on this queue, if it has pending work to do.
  457. * May be used to restart queueing when a request has completed.
  458. */
  459. void blk_run_queue(struct request_queue *q)
  460. {
  461. unsigned long flags;
  462. WARN_ON_ONCE(q->mq_ops);
  463. spin_lock_irqsave(q->queue_lock, flags);
  464. __blk_run_queue(q);
  465. spin_unlock_irqrestore(q->queue_lock, flags);
  466. }
  467. EXPORT_SYMBOL(blk_run_queue);
  468. void blk_put_queue(struct request_queue *q)
  469. {
  470. kobject_put(&q->kobj);
  471. }
  472. EXPORT_SYMBOL(blk_put_queue);
  473. /**
  474. * __blk_drain_queue - drain requests from request_queue
  475. * @q: queue to drain
  476. * @drain_all: whether to drain all requests or only the ones w/ ELVPRIV
  477. *
  478. * Drain requests from @q. If @drain_all is set, all requests are drained.
  479. * If not, only ELVPRIV requests are drained. The caller is responsible
  480. * for ensuring that no new requests which need to be drained are queued.
  481. */
  482. static void __blk_drain_queue(struct request_queue *q, bool drain_all)
  483. __releases(q->queue_lock)
  484. __acquires(q->queue_lock)
  485. {
  486. int i;
  487. lockdep_assert_held(q->queue_lock);
  488. WARN_ON_ONCE(q->mq_ops);
  489. while (true) {
  490. bool drain = false;
  491. /*
  492. * The caller might be trying to drain @q before its
  493. * elevator is initialized.
  494. */
  495. if (q->elevator)
  496. elv_drain_elevator(q);
  497. blkcg_drain_queue(q);
  498. /*
  499. * This function might be called on a queue which failed
  500. * driver init after queue creation or is not yet fully
  501. * active yet. Some drivers (e.g. fd and loop) get unhappy
  502. * in such cases. Kick queue iff dispatch queue has
  503. * something on it and @q has request_fn set.
  504. */
  505. if (!list_empty(&q->queue_head) && q->request_fn)
  506. __blk_run_queue(q);
  507. drain |= q->nr_rqs_elvpriv;
  508. drain |= q->request_fn_active;
  509. /*
  510. * Unfortunately, requests are queued at and tracked from
  511. * multiple places and there's no single counter which can
  512. * be drained. Check all the queues and counters.
  513. */
  514. if (drain_all) {
  515. struct blk_flush_queue *fq = blk_get_flush_queue(q, NULL);
  516. drain |= !list_empty(&q->queue_head);
  517. for (i = 0; i < 2; i++) {
  518. drain |= q->nr_rqs[i];
  519. drain |= q->in_flight[i];
  520. if (fq)
  521. drain |= !list_empty(&fq->flush_queue[i]);
  522. }
  523. }
  524. if (!drain)
  525. break;
  526. spin_unlock_irq(q->queue_lock);
  527. msleep(10);
  528. spin_lock_irq(q->queue_lock);
  529. }
  530. /*
  531. * With queue marked dead, any woken up waiter will fail the
  532. * allocation path, so the wakeup chaining is lost and we're
  533. * left with hung waiters. We need to wake up those waiters.
  534. */
  535. if (q->request_fn) {
  536. struct request_list *rl;
  537. blk_queue_for_each_rl(rl, q)
  538. for (i = 0; i < ARRAY_SIZE(rl->wait); i++)
  539. wake_up_all(&rl->wait[i]);
  540. }
  541. }
  542. void blk_drain_queue(struct request_queue *q)
  543. {
  544. spin_lock_irq(q->queue_lock);
  545. __blk_drain_queue(q, true);
  546. spin_unlock_irq(q->queue_lock);
  547. }
  548. /**
  549. * blk_queue_bypass_start - enter queue bypass mode
  550. * @q: queue of interest
  551. *
  552. * In bypass mode, only the dispatch FIFO queue of @q is used. This
  553. * function makes @q enter bypass mode and drains all requests which were
  554. * throttled or issued before. On return, it's guaranteed that no request
  555. * is being throttled or has ELVPRIV set and blk_queue_bypass() %true
  556. * inside queue or RCU read lock.
  557. */
  558. void blk_queue_bypass_start(struct request_queue *q)
  559. {
  560. WARN_ON_ONCE(q->mq_ops);
  561. spin_lock_irq(q->queue_lock);
  562. q->bypass_depth++;
  563. queue_flag_set(QUEUE_FLAG_BYPASS, q);
  564. spin_unlock_irq(q->queue_lock);
  565. /*
  566. * Queues start drained. Skip actual draining till init is
  567. * complete. This avoids lenghty delays during queue init which
  568. * can happen many times during boot.
  569. */
  570. if (blk_queue_init_done(q)) {
  571. spin_lock_irq(q->queue_lock);
  572. __blk_drain_queue(q, false);
  573. spin_unlock_irq(q->queue_lock);
  574. /* ensure blk_queue_bypass() is %true inside RCU read lock */
  575. synchronize_rcu();
  576. }
  577. }
  578. EXPORT_SYMBOL_GPL(blk_queue_bypass_start);
  579. /**
  580. * blk_queue_bypass_end - leave queue bypass mode
  581. * @q: queue of interest
  582. *
  583. * Leave bypass mode and restore the normal queueing behavior.
  584. *
  585. * Note: although blk_queue_bypass_start() is only called for blk-sq queues,
  586. * this function is called for both blk-sq and blk-mq queues.
  587. */
  588. void blk_queue_bypass_end(struct request_queue *q)
  589. {
  590. spin_lock_irq(q->queue_lock);
  591. if (!--q->bypass_depth)
  592. queue_flag_clear(QUEUE_FLAG_BYPASS, q);
  593. WARN_ON_ONCE(q->bypass_depth < 0);
  594. spin_unlock_irq(q->queue_lock);
  595. }
  596. EXPORT_SYMBOL_GPL(blk_queue_bypass_end);
  597. void blk_set_queue_dying(struct request_queue *q)
  598. {
  599. blk_queue_flag_set(QUEUE_FLAG_DYING, q);
  600. /*
  601. * When queue DYING flag is set, we need to block new req
  602. * entering queue, so we call blk_freeze_queue_start() to
  603. * prevent I/O from crossing blk_queue_enter().
  604. */
  605. blk_freeze_queue_start(q);
  606. if (q->mq_ops)
  607. blk_mq_wake_waiters(q);
  608. else {
  609. struct request_list *rl;
  610. spin_lock_irq(q->queue_lock);
  611. blk_queue_for_each_rl(rl, q) {
  612. if (rl->rq_pool) {
  613. wake_up_all(&rl->wait[BLK_RW_SYNC]);
  614. wake_up_all(&rl->wait[BLK_RW_ASYNC]);
  615. }
  616. }
  617. spin_unlock_irq(q->queue_lock);
  618. }
  619. /* Make blk_queue_enter() reexamine the DYING flag. */
  620. wake_up_all(&q->mq_freeze_wq);
  621. }
  622. EXPORT_SYMBOL_GPL(blk_set_queue_dying);
  623. /* Unconfigure the I/O scheduler and dissociate from the cgroup controller. */
  624. void blk_exit_queue(struct request_queue *q)
  625. {
  626. /*
  627. * Since the I/O scheduler exit code may access cgroup information,
  628. * perform I/O scheduler exit before disassociating from the block
  629. * cgroup controller.
  630. */
  631. if (q->elevator) {
  632. ioc_clear_queue(q);
  633. elevator_exit(q, q->elevator);
  634. q->elevator = NULL;
  635. }
  636. /*
  637. * Remove all references to @q from the block cgroup controller before
  638. * restoring @q->queue_lock to avoid that restoring this pointer causes
  639. * e.g. blkcg_print_blkgs() to crash.
  640. */
  641. blkcg_exit_queue(q);
  642. /*
  643. * Since the cgroup code may dereference the @q->backing_dev_info
  644. * pointer, only decrease its reference count after having removed the
  645. * association with the block cgroup controller.
  646. */
  647. bdi_put(q->backing_dev_info);
  648. }
  649. /**
  650. * blk_cleanup_queue - shutdown a request queue
  651. * @q: request queue to shutdown
  652. *
  653. * Mark @q DYING, drain all pending requests, mark @q DEAD, destroy and
  654. * put it. All future requests will be failed immediately with -ENODEV.
  655. */
  656. void blk_cleanup_queue(struct request_queue *q)
  657. {
  658. spinlock_t *lock = q->queue_lock;
  659. /* mark @q DYING, no new request or merges will be allowed afterwards */
  660. mutex_lock(&q->sysfs_lock);
  661. blk_set_queue_dying(q);
  662. spin_lock_irq(lock);
  663. /*
  664. * A dying queue is permanently in bypass mode till released. Note
  665. * that, unlike blk_queue_bypass_start(), we aren't performing
  666. * synchronize_rcu() after entering bypass mode to avoid the delay
  667. * as some drivers create and destroy a lot of queues while
  668. * probing. This is still safe because blk_release_queue() will be
  669. * called only after the queue refcnt drops to zero and nothing,
  670. * RCU or not, would be traversing the queue by then.
  671. */
  672. q->bypass_depth++;
  673. queue_flag_set(QUEUE_FLAG_BYPASS, q);
  674. queue_flag_set(QUEUE_FLAG_NOMERGES, q);
  675. queue_flag_set(QUEUE_FLAG_NOXMERGES, q);
  676. queue_flag_set(QUEUE_FLAG_DYING, q);
  677. spin_unlock_irq(lock);
  678. mutex_unlock(&q->sysfs_lock);
  679. /*
  680. * Drain all requests queued before DYING marking. Set DEAD flag to
  681. * prevent that q->request_fn() gets invoked after draining finished.
  682. */
  683. blk_freeze_queue(q);
  684. rq_qos_exit(q);
  685. spin_lock_irq(lock);
  686. queue_flag_set(QUEUE_FLAG_DEAD, q);
  687. spin_unlock_irq(lock);
  688. /*
  689. * make sure all in-progress dispatch are completed because
  690. * blk_freeze_queue() can only complete all requests, and
  691. * dispatch may still be in-progress since we dispatch requests
  692. * from more than one contexts.
  693. *
  694. * We rely on driver to deal with the race in case that queue
  695. * initialization isn't done.
  696. */
  697. if (q->mq_ops && blk_queue_init_done(q))
  698. blk_mq_quiesce_queue(q);
  699. /* for synchronous bio-based driver finish in-flight integrity i/o */
  700. blk_flush_integrity();
  701. /* @q won't process any more request, flush async actions */
  702. del_timer_sync(&q->backing_dev_info->laptop_mode_wb_timer);
  703. blk_sync_queue(q);
  704. /*
  705. * I/O scheduler exit is only safe after the sysfs scheduler attribute
  706. * has been removed.
  707. */
  708. WARN_ON_ONCE(q->kobj.state_in_sysfs);
  709. blk_exit_queue(q);
  710. if (q->mq_ops)
  711. blk_mq_free_queue(q);
  712. percpu_ref_exit(&q->q_usage_counter);
  713. spin_lock_irq(lock);
  714. if (q->queue_lock != &q->__queue_lock)
  715. q->queue_lock = &q->__queue_lock;
  716. spin_unlock_irq(lock);
  717. /* @q is and will stay empty, shutdown and put */
  718. blk_put_queue(q);
  719. }
  720. EXPORT_SYMBOL(blk_cleanup_queue);
  721. /* Allocate memory local to the request queue */
  722. static void *alloc_request_simple(gfp_t gfp_mask, void *data)
  723. {
  724. struct request_queue *q = data;
  725. return kmem_cache_alloc_node(request_cachep, gfp_mask, q->node);
  726. }
  727. static void free_request_simple(void *element, void *data)
  728. {
  729. kmem_cache_free(request_cachep, element);
  730. }
  731. static void *alloc_request_size(gfp_t gfp_mask, void *data)
  732. {
  733. struct request_queue *q = data;
  734. struct request *rq;
  735. rq = kmalloc_node(sizeof(struct request) + q->cmd_size, gfp_mask,
  736. q->node);
  737. if (rq && q->init_rq_fn && q->init_rq_fn(q, rq, gfp_mask) < 0) {
  738. kfree(rq);
  739. rq = NULL;
  740. }
  741. return rq;
  742. }
  743. static void free_request_size(void *element, void *data)
  744. {
  745. struct request_queue *q = data;
  746. if (q->exit_rq_fn)
  747. q->exit_rq_fn(q, element);
  748. kfree(element);
  749. }
  750. int blk_init_rl(struct request_list *rl, struct request_queue *q,
  751. gfp_t gfp_mask)
  752. {
  753. if (unlikely(rl->rq_pool) || q->mq_ops)
  754. return 0;
  755. rl->q = q;
  756. rl->count[BLK_RW_SYNC] = rl->count[BLK_RW_ASYNC] = 0;
  757. rl->starved[BLK_RW_SYNC] = rl->starved[BLK_RW_ASYNC] = 0;
  758. init_waitqueue_head(&rl->wait[BLK_RW_SYNC]);
  759. init_waitqueue_head(&rl->wait[BLK_RW_ASYNC]);
  760. if (q->cmd_size) {
  761. rl->rq_pool = mempool_create_node(BLKDEV_MIN_RQ,
  762. alloc_request_size, free_request_size,
  763. q, gfp_mask, q->node);
  764. } else {
  765. rl->rq_pool = mempool_create_node(BLKDEV_MIN_RQ,
  766. alloc_request_simple, free_request_simple,
  767. q, gfp_mask, q->node);
  768. }
  769. if (!rl->rq_pool)
  770. return -ENOMEM;
  771. if (rl != &q->root_rl)
  772. WARN_ON_ONCE(!blk_get_queue(q));
  773. return 0;
  774. }
  775. void blk_exit_rl(struct request_queue *q, struct request_list *rl)
  776. {
  777. if (rl->rq_pool) {
  778. mempool_destroy(rl->rq_pool);
  779. if (rl != &q->root_rl)
  780. blk_put_queue(q);
  781. }
  782. }
  783. struct request_queue *blk_alloc_queue(gfp_t gfp_mask)
  784. {
  785. return blk_alloc_queue_node(gfp_mask, NUMA_NO_NODE, NULL);
  786. }
  787. EXPORT_SYMBOL(blk_alloc_queue);
  788. /**
  789. * blk_queue_enter() - try to increase q->q_usage_counter
  790. * @q: request queue pointer
  791. * @flags: BLK_MQ_REQ_NOWAIT and/or BLK_MQ_REQ_PREEMPT
  792. */
  793. int blk_queue_enter(struct request_queue *q, blk_mq_req_flags_t flags)
  794. {
  795. const bool pm = flags & BLK_MQ_REQ_PREEMPT;
  796. while (true) {
  797. bool success = false;
  798. rcu_read_lock();
  799. if (percpu_ref_tryget_live(&q->q_usage_counter)) {
  800. /*
  801. * The code that increments the pm_only counter is
  802. * responsible for ensuring that that counter is
  803. * globally visible before the queue is unfrozen.
  804. */
  805. if (pm || !blk_queue_pm_only(q)) {
  806. success = true;
  807. } else {
  808. percpu_ref_put(&q->q_usage_counter);
  809. }
  810. }
  811. rcu_read_unlock();
  812. if (success)
  813. return 0;
  814. if (flags & BLK_MQ_REQ_NOWAIT)
  815. return -EBUSY;
  816. /*
  817. * read pair of barrier in blk_freeze_queue_start(),
  818. * we need to order reading __PERCPU_REF_DEAD flag of
  819. * .q_usage_counter and reading .mq_freeze_depth or
  820. * queue dying flag, otherwise the following wait may
  821. * never return if the two reads are reordered.
  822. */
  823. smp_rmb();
  824. wait_event(q->mq_freeze_wq,
  825. (atomic_read(&q->mq_freeze_depth) == 0 &&
  826. (pm || (blk_pm_request_resume(q),
  827. !blk_queue_pm_only(q)))) ||
  828. blk_queue_dying(q));
  829. if (blk_queue_dying(q))
  830. return -ENODEV;
  831. }
  832. }
  833. void blk_queue_exit(struct request_queue *q)
  834. {
  835. percpu_ref_put(&q->q_usage_counter);
  836. }
  837. static void blk_queue_usage_counter_release(struct percpu_ref *ref)
  838. {
  839. struct request_queue *q =
  840. container_of(ref, struct request_queue, q_usage_counter);
  841. wake_up_all(&q->mq_freeze_wq);
  842. }
  843. static void blk_rq_timed_out_timer(struct timer_list *t)
  844. {
  845. struct request_queue *q = from_timer(q, t, timeout);
  846. kblockd_schedule_work(&q->timeout_work);
  847. }
  848. /**
  849. * blk_alloc_queue_node - allocate a request queue
  850. * @gfp_mask: memory allocation flags
  851. * @node_id: NUMA node to allocate memory from
  852. * @lock: For legacy queues, pointer to a spinlock that will be used to e.g.
  853. * serialize calls to the legacy .request_fn() callback. Ignored for
  854. * blk-mq request queues.
  855. *
  856. * Note: pass the queue lock as the third argument to this function instead of
  857. * setting the queue lock pointer explicitly to avoid triggering a sporadic
  858. * crash in the blkcg code. This function namely calls blkcg_init_queue() and
  859. * the queue lock pointer must be set before blkcg_init_queue() is called.
  860. */
  861. struct request_queue *blk_alloc_queue_node(gfp_t gfp_mask, int node_id,
  862. spinlock_t *lock)
  863. {
  864. struct request_queue *q;
  865. int ret;
  866. q = kmem_cache_alloc_node(blk_requestq_cachep,
  867. gfp_mask | __GFP_ZERO, node_id);
  868. if (!q)
  869. return NULL;
  870. INIT_LIST_HEAD(&q->queue_head);
  871. q->last_merge = NULL;
  872. q->end_sector = 0;
  873. q->boundary_rq = NULL;
  874. q->id = ida_simple_get(&blk_queue_ida, 0, 0, gfp_mask);
  875. if (q->id < 0)
  876. goto fail_q;
  877. ret = bioset_init(&q->bio_split, BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS);
  878. if (ret)
  879. goto fail_id;
  880. q->backing_dev_info = bdi_alloc_node(gfp_mask, node_id);
  881. if (!q->backing_dev_info)
  882. goto fail_split;
  883. q->stats = blk_alloc_queue_stats();
  884. if (!q->stats)
  885. goto fail_stats;
  886. q->backing_dev_info->ra_pages =
  887. (VM_MAX_READAHEAD * 1024) / PAGE_SIZE;
  888. q->backing_dev_info->capabilities = BDI_CAP_CGROUP_WRITEBACK;
  889. q->backing_dev_info->name = "block";
  890. q->node = node_id;
  891. timer_setup(&q->backing_dev_info->laptop_mode_wb_timer,
  892. laptop_mode_timer_fn, 0);
  893. timer_setup(&q->timeout, blk_rq_timed_out_timer, 0);
  894. INIT_WORK(&q->timeout_work, NULL);
  895. INIT_LIST_HEAD(&q->timeout_list);
  896. INIT_LIST_HEAD(&q->icq_list);
  897. #ifdef CONFIG_BLK_CGROUP
  898. INIT_LIST_HEAD(&q->blkg_list);
  899. #endif
  900. INIT_DELAYED_WORK(&q->delay_work, blk_delay_work);
  901. kobject_init(&q->kobj, &blk_queue_ktype);
  902. #ifdef CONFIG_BLK_DEV_IO_TRACE
  903. mutex_init(&q->blk_trace_mutex);
  904. #endif
  905. mutex_init(&q->sysfs_lock);
  906. spin_lock_init(&q->__queue_lock);
  907. q->queue_lock = lock ? : &q->__queue_lock;
  908. /*
  909. * A queue starts its life with bypass turned on to avoid
  910. * unnecessary bypass on/off overhead and nasty surprises during
  911. * init. The initial bypass will be finished when the queue is
  912. * registered by blk_register_queue().
  913. */
  914. q->bypass_depth = 1;
  915. queue_flag_set_unlocked(QUEUE_FLAG_BYPASS, q);
  916. init_waitqueue_head(&q->mq_freeze_wq);
  917. /*
  918. * Init percpu_ref in atomic mode so that it's faster to shutdown.
  919. * See blk_register_queue() for details.
  920. */
  921. if (percpu_ref_init(&q->q_usage_counter,
  922. blk_queue_usage_counter_release,
  923. PERCPU_REF_INIT_ATOMIC, GFP_KERNEL))
  924. goto fail_bdi;
  925. if (blkcg_init_queue(q))
  926. goto fail_ref;
  927. return q;
  928. fail_ref:
  929. percpu_ref_exit(&q->q_usage_counter);
  930. fail_bdi:
  931. blk_free_queue_stats(q->stats);
  932. fail_stats:
  933. bdi_put(q->backing_dev_info);
  934. fail_split:
  935. bioset_exit(&q->bio_split);
  936. fail_id:
  937. ida_simple_remove(&blk_queue_ida, q->id);
  938. fail_q:
  939. kmem_cache_free(blk_requestq_cachep, q);
  940. return NULL;
  941. }
  942. EXPORT_SYMBOL(blk_alloc_queue_node);
  943. /**
  944. * blk_init_queue - prepare a request queue for use with a block device
  945. * @rfn: The function to be called to process requests that have been
  946. * placed on the queue.
  947. * @lock: Request queue spin lock
  948. *
  949. * Description:
  950. * If a block device wishes to use the standard request handling procedures,
  951. * which sorts requests and coalesces adjacent requests, then it must
  952. * call blk_init_queue(). The function @rfn will be called when there
  953. * are requests on the queue that need to be processed. If the device
  954. * supports plugging, then @rfn may not be called immediately when requests
  955. * are available on the queue, but may be called at some time later instead.
  956. * Plugged queues are generally unplugged when a buffer belonging to one
  957. * of the requests on the queue is needed, or due to memory pressure.
  958. *
  959. * @rfn is not required, or even expected, to remove all requests off the
  960. * queue, but only as many as it can handle at a time. If it does leave
  961. * requests on the queue, it is responsible for arranging that the requests
  962. * get dealt with eventually.
  963. *
  964. * The queue spin lock must be held while manipulating the requests on the
  965. * request queue; this lock will be taken also from interrupt context, so irq
  966. * disabling is needed for it.
  967. *
  968. * Function returns a pointer to the initialized request queue, or %NULL if
  969. * it didn't succeed.
  970. *
  971. * Note:
  972. * blk_init_queue() must be paired with a blk_cleanup_queue() call
  973. * when the block device is deactivated (such as at module unload).
  974. **/
  975. struct request_queue *blk_init_queue(request_fn_proc *rfn, spinlock_t *lock)
  976. {
  977. return blk_init_queue_node(rfn, lock, NUMA_NO_NODE);
  978. }
  979. EXPORT_SYMBOL(blk_init_queue);
  980. struct request_queue *
  981. blk_init_queue_node(request_fn_proc *rfn, spinlock_t *lock, int node_id)
  982. {
  983. struct request_queue *q;
  984. q = blk_alloc_queue_node(GFP_KERNEL, node_id, lock);
  985. if (!q)
  986. return NULL;
  987. q->request_fn = rfn;
  988. if (blk_init_allocated_queue(q) < 0) {
  989. blk_cleanup_queue(q);
  990. return NULL;
  991. }
  992. return q;
  993. }
  994. EXPORT_SYMBOL(blk_init_queue_node);
  995. static blk_qc_t blk_queue_bio(struct request_queue *q, struct bio *bio);
  996. int blk_init_allocated_queue(struct request_queue *q)
  997. {
  998. WARN_ON_ONCE(q->mq_ops);
  999. q->fq = blk_alloc_flush_queue(q, NUMA_NO_NODE, q->cmd_size, GFP_KERNEL);
  1000. if (!q->fq)
  1001. return -ENOMEM;
  1002. if (q->init_rq_fn && q->init_rq_fn(q, q->fq->flush_rq, GFP_KERNEL))
  1003. goto out_free_flush_queue;
  1004. if (blk_init_rl(&q->root_rl, q, GFP_KERNEL))
  1005. goto out_exit_flush_rq;
  1006. INIT_WORK(&q->timeout_work, blk_timeout_work);
  1007. q->queue_flags |= QUEUE_FLAG_DEFAULT;
  1008. /*
  1009. * This also sets hw/phys segments, boundary and size
  1010. */
  1011. blk_queue_make_request(q, blk_queue_bio);
  1012. q->sg_reserved_size = INT_MAX;
  1013. if (elevator_init(q))
  1014. goto out_exit_flush_rq;
  1015. return 0;
  1016. out_exit_flush_rq:
  1017. if (q->exit_rq_fn)
  1018. q->exit_rq_fn(q, q->fq->flush_rq);
  1019. out_free_flush_queue:
  1020. blk_free_flush_queue(q->fq);
  1021. q->fq = NULL;
  1022. return -ENOMEM;
  1023. }
  1024. EXPORT_SYMBOL(blk_init_allocated_queue);
  1025. bool blk_get_queue(struct request_queue *q)
  1026. {
  1027. if (likely(!blk_queue_dying(q))) {
  1028. __blk_get_queue(q);
  1029. return true;
  1030. }
  1031. return false;
  1032. }
  1033. EXPORT_SYMBOL(blk_get_queue);
  1034. static inline void blk_free_request(struct request_list *rl, struct request *rq)
  1035. {
  1036. if (rq->rq_flags & RQF_ELVPRIV) {
  1037. elv_put_request(rl->q, rq);
  1038. if (rq->elv.icq)
  1039. put_io_context(rq->elv.icq->ioc);
  1040. }
  1041. mempool_free(rq, rl->rq_pool);
  1042. }
  1043. /*
  1044. * ioc_batching returns true if the ioc is a valid batching request and
  1045. * should be given priority access to a request.
  1046. */
  1047. static inline int ioc_batching(struct request_queue *q, struct io_context *ioc)
  1048. {
  1049. if (!ioc)
  1050. return 0;
  1051. /*
  1052. * Make sure the process is able to allocate at least 1 request
  1053. * even if the batch times out, otherwise we could theoretically
  1054. * lose wakeups.
  1055. */
  1056. return ioc->nr_batch_requests == q->nr_batching ||
  1057. (ioc->nr_batch_requests > 0
  1058. && time_before(jiffies, ioc->last_waited + BLK_BATCH_TIME));
  1059. }
  1060. /*
  1061. * ioc_set_batching sets ioc to be a new "batcher" if it is not one. This
  1062. * will cause the process to be a "batcher" on all queues in the system. This
  1063. * is the behaviour we want though - once it gets a wakeup it should be given
  1064. * a nice run.
  1065. */
  1066. static void ioc_set_batching(struct request_queue *q, struct io_context *ioc)
  1067. {
  1068. if (!ioc || ioc_batching(q, ioc))
  1069. return;
  1070. ioc->nr_batch_requests = q->nr_batching;
  1071. ioc->last_waited = jiffies;
  1072. }
  1073. static void __freed_request(struct request_list *rl, int sync)
  1074. {
  1075. struct request_queue *q = rl->q;
  1076. if (rl->count[sync] < queue_congestion_off_threshold(q))
  1077. blk_clear_congested(rl, sync);
  1078. if (rl->count[sync] + 1 <= q->nr_requests) {
  1079. if (waitqueue_active(&rl->wait[sync]))
  1080. wake_up(&rl->wait[sync]);
  1081. blk_clear_rl_full(rl, sync);
  1082. }
  1083. }
  1084. /*
  1085. * A request has just been released. Account for it, update the full and
  1086. * congestion status, wake up any waiters. Called under q->queue_lock.
  1087. */
  1088. static void freed_request(struct request_list *rl, bool sync,
  1089. req_flags_t rq_flags)
  1090. {
  1091. struct request_queue *q = rl->q;
  1092. q->nr_rqs[sync]--;
  1093. rl->count[sync]--;
  1094. if (rq_flags & RQF_ELVPRIV)
  1095. q->nr_rqs_elvpriv--;
  1096. __freed_request(rl, sync);
  1097. if (unlikely(rl->starved[sync ^ 1]))
  1098. __freed_request(rl, sync ^ 1);
  1099. }
  1100. int blk_update_nr_requests(struct request_queue *q, unsigned int nr)
  1101. {
  1102. struct request_list *rl;
  1103. int on_thresh, off_thresh;
  1104. WARN_ON_ONCE(q->mq_ops);
  1105. spin_lock_irq(q->queue_lock);
  1106. q->nr_requests = nr;
  1107. blk_queue_congestion_threshold(q);
  1108. on_thresh = queue_congestion_on_threshold(q);
  1109. off_thresh = queue_congestion_off_threshold(q);
  1110. blk_queue_for_each_rl(rl, q) {
  1111. if (rl->count[BLK_RW_SYNC] >= on_thresh)
  1112. blk_set_congested(rl, BLK_RW_SYNC);
  1113. else if (rl->count[BLK_RW_SYNC] < off_thresh)
  1114. blk_clear_congested(rl, BLK_RW_SYNC);
  1115. if (rl->count[BLK_RW_ASYNC] >= on_thresh)
  1116. blk_set_congested(rl, BLK_RW_ASYNC);
  1117. else if (rl->count[BLK_RW_ASYNC] < off_thresh)
  1118. blk_clear_congested(rl, BLK_RW_ASYNC);
  1119. if (rl->count[BLK_RW_SYNC] >= q->nr_requests) {
  1120. blk_set_rl_full(rl, BLK_RW_SYNC);
  1121. } else {
  1122. blk_clear_rl_full(rl, BLK_RW_SYNC);
  1123. wake_up(&rl->wait[BLK_RW_SYNC]);
  1124. }
  1125. if (rl->count[BLK_RW_ASYNC] >= q->nr_requests) {
  1126. blk_set_rl_full(rl, BLK_RW_ASYNC);
  1127. } else {
  1128. blk_clear_rl_full(rl, BLK_RW_ASYNC);
  1129. wake_up(&rl->wait[BLK_RW_ASYNC]);
  1130. }
  1131. }
  1132. spin_unlock_irq(q->queue_lock);
  1133. return 0;
  1134. }
  1135. /**
  1136. * __get_request - get a free request
  1137. * @rl: request list to allocate from
  1138. * @op: operation and flags
  1139. * @bio: bio to allocate request for (can be %NULL)
  1140. * @flags: BLQ_MQ_REQ_* flags
  1141. * @gfp_mask: allocator flags
  1142. *
  1143. * Get a free request from @q. This function may fail under memory
  1144. * pressure or if @q is dead.
  1145. *
  1146. * Must be called with @q->queue_lock held and,
  1147. * Returns ERR_PTR on failure, with @q->queue_lock held.
  1148. * Returns request pointer on success, with @q->queue_lock *not held*.
  1149. */
  1150. static struct request *__get_request(struct request_list *rl, unsigned int op,
  1151. struct bio *bio, blk_mq_req_flags_t flags, gfp_t gfp_mask)
  1152. {
  1153. struct request_queue *q = rl->q;
  1154. struct request *rq;
  1155. struct elevator_type *et = q->elevator->type;
  1156. struct io_context *ioc = rq_ioc(bio);
  1157. struct io_cq *icq = NULL;
  1158. const bool is_sync = op_is_sync(op);
  1159. int may_queue;
  1160. req_flags_t rq_flags = RQF_ALLOCED;
  1161. lockdep_assert_held(q->queue_lock);
  1162. if (unlikely(blk_queue_dying(q)))
  1163. return ERR_PTR(-ENODEV);
  1164. may_queue = elv_may_queue(q, op);
  1165. if (may_queue == ELV_MQUEUE_NO)
  1166. goto rq_starved;
  1167. if (rl->count[is_sync]+1 >= queue_congestion_on_threshold(q)) {
  1168. if (rl->count[is_sync]+1 >= q->nr_requests) {
  1169. /*
  1170. * The queue will fill after this allocation, so set
  1171. * it as full, and mark this process as "batching".
  1172. * This process will be allowed to complete a batch of
  1173. * requests, others will be blocked.
  1174. */
  1175. if (!blk_rl_full(rl, is_sync)) {
  1176. ioc_set_batching(q, ioc);
  1177. blk_set_rl_full(rl, is_sync);
  1178. } else {
  1179. if (may_queue != ELV_MQUEUE_MUST
  1180. && !ioc_batching(q, ioc)) {
  1181. /*
  1182. * The queue is full and the allocating
  1183. * process is not a "batcher", and not
  1184. * exempted by the IO scheduler
  1185. */
  1186. return ERR_PTR(-ENOMEM);
  1187. }
  1188. }
  1189. }
  1190. blk_set_congested(rl, is_sync);
  1191. }
  1192. /*
  1193. * Only allow batching queuers to allocate up to 50% over the defined
  1194. * limit of requests, otherwise we could have thousands of requests
  1195. * allocated with any setting of ->nr_requests
  1196. */
  1197. if (rl->count[is_sync] >= (3 * q->nr_requests / 2))
  1198. return ERR_PTR(-ENOMEM);
  1199. q->nr_rqs[is_sync]++;
  1200. rl->count[is_sync]++;
  1201. rl->starved[is_sync] = 0;
  1202. /*
  1203. * Decide whether the new request will be managed by elevator. If
  1204. * so, mark @rq_flags and increment elvpriv. Non-zero elvpriv will
  1205. * prevent the current elevator from being destroyed until the new
  1206. * request is freed. This guarantees icq's won't be destroyed and
  1207. * makes creating new ones safe.
  1208. *
  1209. * Flush requests do not use the elevator so skip initialization.
  1210. * This allows a request to share the flush and elevator data.
  1211. *
  1212. * Also, lookup icq while holding queue_lock. If it doesn't exist,
  1213. * it will be created after releasing queue_lock.
  1214. */
  1215. if (!op_is_flush(op) && !blk_queue_bypass(q)) {
  1216. rq_flags |= RQF_ELVPRIV;
  1217. q->nr_rqs_elvpriv++;
  1218. if (et->icq_cache && ioc)
  1219. icq = ioc_lookup_icq(ioc, q);
  1220. }
  1221. if (blk_queue_io_stat(q))
  1222. rq_flags |= RQF_IO_STAT;
  1223. spin_unlock_irq(q->queue_lock);
  1224. /* allocate and init request */
  1225. rq = mempool_alloc(rl->rq_pool, gfp_mask);
  1226. if (!rq)
  1227. goto fail_alloc;
  1228. blk_rq_init(q, rq);
  1229. blk_rq_set_rl(rq, rl);
  1230. rq->cmd_flags = op;
  1231. rq->rq_flags = rq_flags;
  1232. if (flags & BLK_MQ_REQ_PREEMPT)
  1233. rq->rq_flags |= RQF_PREEMPT;
  1234. /* init elvpriv */
  1235. if (rq_flags & RQF_ELVPRIV) {
  1236. if (unlikely(et->icq_cache && !icq)) {
  1237. if (ioc)
  1238. icq = ioc_create_icq(ioc, q, gfp_mask);
  1239. if (!icq)
  1240. goto fail_elvpriv;
  1241. }
  1242. rq->elv.icq = icq;
  1243. if (unlikely(elv_set_request(q, rq, bio, gfp_mask)))
  1244. goto fail_elvpriv;
  1245. /* @rq->elv.icq holds io_context until @rq is freed */
  1246. if (icq)
  1247. get_io_context(icq->ioc);
  1248. }
  1249. out:
  1250. /*
  1251. * ioc may be NULL here, and ioc_batching will be false. That's
  1252. * OK, if the queue is under the request limit then requests need
  1253. * not count toward the nr_batch_requests limit. There will always
  1254. * be some limit enforced by BLK_BATCH_TIME.
  1255. */
  1256. if (ioc_batching(q, ioc))
  1257. ioc->nr_batch_requests--;
  1258. trace_block_getrq(q, bio, op);
  1259. return rq;
  1260. fail_elvpriv:
  1261. /*
  1262. * elvpriv init failed. ioc, icq and elvpriv aren't mempool backed
  1263. * and may fail indefinitely under memory pressure and thus
  1264. * shouldn't stall IO. Treat this request as !elvpriv. This will
  1265. * disturb iosched and blkcg but weird is bettern than dead.
  1266. */
  1267. printk_ratelimited(KERN_WARNING "%s: dev %s: request aux data allocation failed, iosched may be disturbed\n",
  1268. __func__, dev_name(q->backing_dev_info->dev));
  1269. rq->rq_flags &= ~RQF_ELVPRIV;
  1270. rq->elv.icq = NULL;
  1271. spin_lock_irq(q->queue_lock);
  1272. q->nr_rqs_elvpriv--;
  1273. spin_unlock_irq(q->queue_lock);
  1274. goto out;
  1275. fail_alloc:
  1276. /*
  1277. * Allocation failed presumably due to memory. Undo anything we
  1278. * might have messed up.
  1279. *
  1280. * Allocating task should really be put onto the front of the wait
  1281. * queue, but this is pretty rare.
  1282. */
  1283. spin_lock_irq(q->queue_lock);
  1284. freed_request(rl, is_sync, rq_flags);
  1285. /*
  1286. * in the very unlikely event that allocation failed and no
  1287. * requests for this direction was pending, mark us starved so that
  1288. * freeing of a request in the other direction will notice
  1289. * us. another possible fix would be to split the rq mempool into
  1290. * READ and WRITE
  1291. */
  1292. rq_starved:
  1293. if (unlikely(rl->count[is_sync] == 0))
  1294. rl->starved[is_sync] = 1;
  1295. return ERR_PTR(-ENOMEM);
  1296. }
  1297. /**
  1298. * get_request - get a free request
  1299. * @q: request_queue to allocate request from
  1300. * @op: operation and flags
  1301. * @bio: bio to allocate request for (can be %NULL)
  1302. * @flags: BLK_MQ_REQ_* flags.
  1303. * @gfp: allocator flags
  1304. *
  1305. * Get a free request from @q. If %BLK_MQ_REQ_NOWAIT is set in @flags,
  1306. * this function keeps retrying under memory pressure and fails iff @q is dead.
  1307. *
  1308. * Must be called with @q->queue_lock held and,
  1309. * Returns ERR_PTR on failure, with @q->queue_lock held.
  1310. * Returns request pointer on success, with @q->queue_lock *not held*.
  1311. */
  1312. static struct request *get_request(struct request_queue *q, unsigned int op,
  1313. struct bio *bio, blk_mq_req_flags_t flags, gfp_t gfp)
  1314. {
  1315. const bool is_sync = op_is_sync(op);
  1316. DEFINE_WAIT(wait);
  1317. struct request_list *rl;
  1318. struct request *rq;
  1319. lockdep_assert_held(q->queue_lock);
  1320. WARN_ON_ONCE(q->mq_ops);
  1321. rl = blk_get_rl(q, bio); /* transferred to @rq on success */
  1322. retry:
  1323. rq = __get_request(rl, op, bio, flags, gfp);
  1324. if (!IS_ERR(rq))
  1325. return rq;
  1326. if (op & REQ_NOWAIT) {
  1327. blk_put_rl(rl);
  1328. return ERR_PTR(-EAGAIN);
  1329. }
  1330. if ((flags & BLK_MQ_REQ_NOWAIT) || unlikely(blk_queue_dying(q))) {
  1331. blk_put_rl(rl);
  1332. return rq;
  1333. }
  1334. /* wait on @rl and retry */
  1335. prepare_to_wait_exclusive(&rl->wait[is_sync], &wait,
  1336. TASK_UNINTERRUPTIBLE);
  1337. trace_block_sleeprq(q, bio, op);
  1338. spin_unlock_irq(q->queue_lock);
  1339. io_schedule();
  1340. /*
  1341. * After sleeping, we become a "batching" process and will be able
  1342. * to allocate at least one request, and up to a big batch of them
  1343. * for a small period time. See ioc_batching, ioc_set_batching
  1344. */
  1345. ioc_set_batching(q, current->io_context);
  1346. spin_lock_irq(q->queue_lock);
  1347. finish_wait(&rl->wait[is_sync], &wait);
  1348. goto retry;
  1349. }
  1350. /* flags: BLK_MQ_REQ_PREEMPT and/or BLK_MQ_REQ_NOWAIT. */
  1351. static struct request *blk_old_get_request(struct request_queue *q,
  1352. unsigned int op, blk_mq_req_flags_t flags)
  1353. {
  1354. struct request *rq;
  1355. gfp_t gfp_mask = flags & BLK_MQ_REQ_NOWAIT ? GFP_ATOMIC : GFP_NOIO;
  1356. int ret = 0;
  1357. WARN_ON_ONCE(q->mq_ops);
  1358. /* create ioc upfront */
  1359. create_io_context(gfp_mask, q->node);
  1360. ret = blk_queue_enter(q, flags);
  1361. if (ret)
  1362. return ERR_PTR(ret);
  1363. spin_lock_irq(q->queue_lock);
  1364. rq = get_request(q, op, NULL, flags, gfp_mask);
  1365. if (IS_ERR(rq)) {
  1366. spin_unlock_irq(q->queue_lock);
  1367. blk_queue_exit(q);
  1368. return rq;
  1369. }
  1370. /* q->queue_lock is unlocked at this point */
  1371. rq->__data_len = 0;
  1372. rq->__sector = (sector_t) -1;
  1373. rq->bio = rq->biotail = NULL;
  1374. return rq;
  1375. }
  1376. /**
  1377. * blk_get_request - allocate a request
  1378. * @q: request queue to allocate a request for
  1379. * @op: operation (REQ_OP_*) and REQ_* flags, e.g. REQ_SYNC.
  1380. * @flags: BLK_MQ_REQ_* flags, e.g. BLK_MQ_REQ_NOWAIT.
  1381. */
  1382. struct request *blk_get_request(struct request_queue *q, unsigned int op,
  1383. blk_mq_req_flags_t flags)
  1384. {
  1385. struct request *req;
  1386. WARN_ON_ONCE(op & REQ_NOWAIT);
  1387. WARN_ON_ONCE(flags & ~(BLK_MQ_REQ_NOWAIT | BLK_MQ_REQ_PREEMPT));
  1388. if (q->mq_ops) {
  1389. req = blk_mq_alloc_request(q, op, flags);
  1390. if (!IS_ERR(req) && q->mq_ops->initialize_rq_fn)
  1391. q->mq_ops->initialize_rq_fn(req);
  1392. } else {
  1393. req = blk_old_get_request(q, op, flags);
  1394. if (!IS_ERR(req) && q->initialize_rq_fn)
  1395. q->initialize_rq_fn(req);
  1396. }
  1397. return req;
  1398. }
  1399. EXPORT_SYMBOL(blk_get_request);
  1400. /**
  1401. * blk_requeue_request - put a request back on queue
  1402. * @q: request queue where request should be inserted
  1403. * @rq: request to be inserted
  1404. *
  1405. * Description:
  1406. * Drivers often keep queueing requests until the hardware cannot accept
  1407. * more, when that condition happens we need to put the request back
  1408. * on the queue. Must be called with queue lock held.
  1409. */
  1410. void blk_requeue_request(struct request_queue *q, struct request *rq)
  1411. {
  1412. lockdep_assert_held(q->queue_lock);
  1413. WARN_ON_ONCE(q->mq_ops);
  1414. blk_delete_timer(rq);
  1415. blk_clear_rq_complete(rq);
  1416. trace_block_rq_requeue(q, rq);
  1417. rq_qos_requeue(q, rq);
  1418. if (rq->rq_flags & RQF_QUEUED)
  1419. blk_queue_end_tag(q, rq);
  1420. BUG_ON(blk_queued_rq(rq));
  1421. elv_requeue_request(q, rq);
  1422. }
  1423. EXPORT_SYMBOL(blk_requeue_request);
  1424. static void add_acct_request(struct request_queue *q, struct request *rq,
  1425. int where)
  1426. {
  1427. blk_account_io_start(rq, true);
  1428. __elv_add_request(q, rq, where);
  1429. }
  1430. static void part_round_stats_single(struct request_queue *q, int cpu,
  1431. struct hd_struct *part, unsigned long now,
  1432. unsigned int inflight)
  1433. {
  1434. if (inflight) {
  1435. __part_stat_add(cpu, part, time_in_queue,
  1436. inflight * (now - part->stamp));
  1437. __part_stat_add(cpu, part, io_ticks, (now - part->stamp));
  1438. }
  1439. part->stamp = now;
  1440. }
  1441. /**
  1442. * part_round_stats() - Round off the performance stats on a struct disk_stats.
  1443. * @q: target block queue
  1444. * @cpu: cpu number for stats access
  1445. * @part: target partition
  1446. *
  1447. * The average IO queue length and utilisation statistics are maintained
  1448. * by observing the current state of the queue length and the amount of
  1449. * time it has been in this state for.
  1450. *
  1451. * Normally, that accounting is done on IO completion, but that can result
  1452. * in more than a second's worth of IO being accounted for within any one
  1453. * second, leading to >100% utilisation. To deal with that, we call this
  1454. * function to do a round-off before returning the results when reading
  1455. * /proc/diskstats. This accounts immediately for all queue usage up to
  1456. * the current jiffies and restarts the counters again.
  1457. */
  1458. void part_round_stats(struct request_queue *q, int cpu, struct hd_struct *part)
  1459. {
  1460. struct hd_struct *part2 = NULL;
  1461. unsigned long now = jiffies;
  1462. unsigned int inflight[2];
  1463. int stats = 0;
  1464. if (part->stamp != now)
  1465. stats |= 1;
  1466. if (part->partno) {
  1467. part2 = &part_to_disk(part)->part0;
  1468. if (part2->stamp != now)
  1469. stats |= 2;
  1470. }
  1471. if (!stats)
  1472. return;
  1473. part_in_flight(q, part, inflight);
  1474. if (stats & 2)
  1475. part_round_stats_single(q, cpu, part2, now, inflight[1]);
  1476. if (stats & 1)
  1477. part_round_stats_single(q, cpu, part, now, inflight[0]);
  1478. }
  1479. EXPORT_SYMBOL_GPL(part_round_stats);
  1480. void __blk_put_request(struct request_queue *q, struct request *req)
  1481. {
  1482. req_flags_t rq_flags = req->rq_flags;
  1483. if (unlikely(!q))
  1484. return;
  1485. if (q->mq_ops) {
  1486. blk_mq_free_request(req);
  1487. return;
  1488. }
  1489. lockdep_assert_held(q->queue_lock);
  1490. blk_req_zone_write_unlock(req);
  1491. blk_pm_put_request(req);
  1492. blk_pm_mark_last_busy(req);
  1493. elv_completed_request(q, req);
  1494. /* this is a bio leak */
  1495. WARN_ON(req->bio != NULL);
  1496. rq_qos_done(q, req);
  1497. /*
  1498. * Request may not have originated from ll_rw_blk. if not,
  1499. * it didn't come out of our reserved rq pools
  1500. */
  1501. if (rq_flags & RQF_ALLOCED) {
  1502. struct request_list *rl = blk_rq_rl(req);
  1503. bool sync = op_is_sync(req->cmd_flags);
  1504. BUG_ON(!list_empty(&req->queuelist));
  1505. BUG_ON(ELV_ON_HASH(req));
  1506. blk_free_request(rl, req);
  1507. freed_request(rl, sync, rq_flags);
  1508. blk_put_rl(rl);
  1509. blk_queue_exit(q);
  1510. }
  1511. }
  1512. EXPORT_SYMBOL_GPL(__blk_put_request);
  1513. void blk_put_request(struct request *req)
  1514. {
  1515. struct request_queue *q = req->q;
  1516. if (q->mq_ops)
  1517. blk_mq_free_request(req);
  1518. else {
  1519. unsigned long flags;
  1520. spin_lock_irqsave(q->queue_lock, flags);
  1521. __blk_put_request(q, req);
  1522. spin_unlock_irqrestore(q->queue_lock, flags);
  1523. }
  1524. }
  1525. EXPORT_SYMBOL(blk_put_request);
  1526. bool bio_attempt_back_merge(struct request_queue *q, struct request *req,
  1527. struct bio *bio)
  1528. {
  1529. const int ff = bio->bi_opf & REQ_FAILFAST_MASK;
  1530. if (!ll_back_merge_fn(q, req, bio))
  1531. return false;
  1532. trace_block_bio_backmerge(q, req, bio);
  1533. if ((req->cmd_flags & REQ_FAILFAST_MASK) != ff)
  1534. blk_rq_set_mixed_merge(req);
  1535. req->biotail->bi_next = bio;
  1536. req->biotail = bio;
  1537. req->__data_len += bio->bi_iter.bi_size;
  1538. req->ioprio = ioprio_best(req->ioprio, bio_prio(bio));
  1539. blk_account_io_start(req, false);
  1540. return true;
  1541. }
  1542. bool bio_attempt_front_merge(struct request_queue *q, struct request *req,
  1543. struct bio *bio)
  1544. {
  1545. const int ff = bio->bi_opf & REQ_FAILFAST_MASK;
  1546. if (!ll_front_merge_fn(q, req, bio))
  1547. return false;
  1548. trace_block_bio_frontmerge(q, req, bio);
  1549. if ((req->cmd_flags & REQ_FAILFAST_MASK) != ff)
  1550. blk_rq_set_mixed_merge(req);
  1551. bio->bi_next = req->bio;
  1552. req->bio = bio;
  1553. req->__sector = bio->bi_iter.bi_sector;
  1554. req->__data_len += bio->bi_iter.bi_size;
  1555. req->ioprio = ioprio_best(req->ioprio, bio_prio(bio));
  1556. blk_account_io_start(req, false);
  1557. return true;
  1558. }
  1559. bool bio_attempt_discard_merge(struct request_queue *q, struct request *req,
  1560. struct bio *bio)
  1561. {
  1562. unsigned short segments = blk_rq_nr_discard_segments(req);
  1563. if (segments >= queue_max_discard_segments(q))
  1564. goto no_merge;
  1565. if (blk_rq_sectors(req) + bio_sectors(bio) >
  1566. blk_rq_get_max_sectors(req, blk_rq_pos(req)))
  1567. goto no_merge;
  1568. req->biotail->bi_next = bio;
  1569. req->biotail = bio;
  1570. req->__data_len += bio->bi_iter.bi_size;
  1571. req->ioprio = ioprio_best(req->ioprio, bio_prio(bio));
  1572. req->nr_phys_segments = segments + 1;
  1573. blk_account_io_start(req, false);
  1574. return true;
  1575. no_merge:
  1576. req_set_nomerge(q, req);
  1577. return false;
  1578. }
  1579. /**
  1580. * blk_attempt_plug_merge - try to merge with %current's plugged list
  1581. * @q: request_queue new bio is being queued at
  1582. * @bio: new bio being queued
  1583. * @request_count: out parameter for number of traversed plugged requests
  1584. * @same_queue_rq: pointer to &struct request that gets filled in when
  1585. * another request associated with @q is found on the plug list
  1586. * (optional, may be %NULL)
  1587. *
  1588. * Determine whether @bio being queued on @q can be merged with a request
  1589. * on %current's plugged list. Returns %true if merge was successful,
  1590. * otherwise %false.
  1591. *
  1592. * Plugging coalesces IOs from the same issuer for the same purpose without
  1593. * going through @q->queue_lock. As such it's more of an issuing mechanism
  1594. * than scheduling, and the request, while may have elvpriv data, is not
  1595. * added on the elevator at this point. In addition, we don't have
  1596. * reliable access to the elevator outside queue lock. Only check basic
  1597. * merging parameters without querying the elevator.
  1598. *
  1599. * Caller must ensure !blk_queue_nomerges(q) beforehand.
  1600. */
  1601. bool blk_attempt_plug_merge(struct request_queue *q, struct bio *bio,
  1602. unsigned int *request_count,
  1603. struct request **same_queue_rq)
  1604. {
  1605. struct blk_plug *plug;
  1606. struct request *rq;
  1607. struct list_head *plug_list;
  1608. plug = current->plug;
  1609. if (!plug)
  1610. return false;
  1611. *request_count = 0;
  1612. if (q->mq_ops)
  1613. plug_list = &plug->mq_list;
  1614. else
  1615. plug_list = &plug->list;
  1616. list_for_each_entry_reverse(rq, plug_list, queuelist) {
  1617. bool merged = false;
  1618. if (rq->q == q) {
  1619. (*request_count)++;
  1620. /*
  1621. * Only blk-mq multiple hardware queues case checks the
  1622. * rq in the same queue, there should be only one such
  1623. * rq in a queue
  1624. **/
  1625. if (same_queue_rq)
  1626. *same_queue_rq = rq;
  1627. }
  1628. if (rq->q != q || !blk_rq_merge_ok(rq, bio))
  1629. continue;
  1630. switch (blk_try_merge(rq, bio)) {
  1631. case ELEVATOR_BACK_MERGE:
  1632. merged = bio_attempt_back_merge(q, rq, bio);
  1633. break;
  1634. case ELEVATOR_FRONT_MERGE:
  1635. merged = bio_attempt_front_merge(q, rq, bio);
  1636. break;
  1637. case ELEVATOR_DISCARD_MERGE:
  1638. merged = bio_attempt_discard_merge(q, rq, bio);
  1639. break;
  1640. default:
  1641. break;
  1642. }
  1643. if (merged)
  1644. return true;
  1645. }
  1646. return false;
  1647. }
  1648. unsigned int blk_plug_queued_count(struct request_queue *q)
  1649. {
  1650. struct blk_plug *plug;
  1651. struct request *rq;
  1652. struct list_head *plug_list;
  1653. unsigned int ret = 0;
  1654. plug = current->plug;
  1655. if (!plug)
  1656. goto out;
  1657. if (q->mq_ops)
  1658. plug_list = &plug->mq_list;
  1659. else
  1660. plug_list = &plug->list;
  1661. list_for_each_entry(rq, plug_list, queuelist) {
  1662. if (rq->q == q)
  1663. ret++;
  1664. }
  1665. out:
  1666. return ret;
  1667. }
  1668. void blk_init_request_from_bio(struct request *req, struct bio *bio)
  1669. {
  1670. struct io_context *ioc = rq_ioc(bio);
  1671. if (bio->bi_opf & REQ_RAHEAD)
  1672. req->cmd_flags |= REQ_FAILFAST_MASK;
  1673. req->__sector = bio->bi_iter.bi_sector;
  1674. if (ioprio_valid(bio_prio(bio)))
  1675. req->ioprio = bio_prio(bio);
  1676. else if (ioc)
  1677. req->ioprio = ioc->ioprio;
  1678. else
  1679. req->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE, 0);
  1680. req->write_hint = bio->bi_write_hint;
  1681. blk_rq_bio_prep(req->q, req, bio);
  1682. }
  1683. EXPORT_SYMBOL_GPL(blk_init_request_from_bio);
  1684. static blk_qc_t blk_queue_bio(struct request_queue *q, struct bio *bio)
  1685. {
  1686. struct blk_plug *plug;
  1687. int where = ELEVATOR_INSERT_SORT;
  1688. struct request *req, *free;
  1689. unsigned int request_count = 0;
  1690. /*
  1691. * low level driver can indicate that it wants pages above a
  1692. * certain limit bounced to low memory (ie for highmem, or even
  1693. * ISA dma in theory)
  1694. */
  1695. blk_queue_bounce(q, &bio);
  1696. blk_queue_split(q, &bio);
  1697. if (!bio_integrity_prep(bio))
  1698. return BLK_QC_T_NONE;
  1699. if (op_is_flush(bio->bi_opf)) {
  1700. spin_lock_irq(q->queue_lock);
  1701. where = ELEVATOR_INSERT_FLUSH;
  1702. goto get_rq;
  1703. }
  1704. /*
  1705. * Check if we can merge with the plugged list before grabbing
  1706. * any locks.
  1707. */
  1708. if (!blk_queue_nomerges(q)) {
  1709. if (blk_attempt_plug_merge(q, bio, &request_count, NULL))
  1710. return BLK_QC_T_NONE;
  1711. } else
  1712. request_count = blk_plug_queued_count(q);
  1713. spin_lock_irq(q->queue_lock);
  1714. switch (elv_merge(q, &req, bio)) {
  1715. case ELEVATOR_BACK_MERGE:
  1716. if (!bio_attempt_back_merge(q, req, bio))
  1717. break;
  1718. elv_bio_merged(q, req, bio);
  1719. free = attempt_back_merge(q, req);
  1720. if (free)
  1721. __blk_put_request(q, free);
  1722. else
  1723. elv_merged_request(q, req, ELEVATOR_BACK_MERGE);
  1724. goto out_unlock;
  1725. case ELEVATOR_FRONT_MERGE:
  1726. if (!bio_attempt_front_merge(q, req, bio))
  1727. break;
  1728. elv_bio_merged(q, req, bio);
  1729. free = attempt_front_merge(q, req);
  1730. if (free)
  1731. __blk_put_request(q, free);
  1732. else
  1733. elv_merged_request(q, req, ELEVATOR_FRONT_MERGE);
  1734. goto out_unlock;
  1735. default:
  1736. break;
  1737. }
  1738. get_rq:
  1739. rq_qos_throttle(q, bio, q->queue_lock);
  1740. /*
  1741. * Grab a free request. This is might sleep but can not fail.
  1742. * Returns with the queue unlocked.
  1743. */
  1744. blk_queue_enter_live(q);
  1745. req = get_request(q, bio->bi_opf, bio, 0, GFP_NOIO);
  1746. if (IS_ERR(req)) {
  1747. blk_queue_exit(q);
  1748. rq_qos_cleanup(q, bio);
  1749. if (PTR_ERR(req) == -ENOMEM)
  1750. bio->bi_status = BLK_STS_RESOURCE;
  1751. else
  1752. bio->bi_status = BLK_STS_IOERR;
  1753. bio_endio(bio);
  1754. goto out_unlock;
  1755. }
  1756. rq_qos_track(q, req, bio);
  1757. /*
  1758. * After dropping the lock and possibly sleeping here, our request
  1759. * may now be mergeable after it had proven unmergeable (above).
  1760. * We don't worry about that case for efficiency. It won't happen
  1761. * often, and the elevators are able to handle it.
  1762. */
  1763. blk_init_request_from_bio(req, bio);
  1764. if (test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags))
  1765. req->cpu = raw_smp_processor_id();
  1766. plug = current->plug;
  1767. if (plug) {
  1768. /*
  1769. * If this is the first request added after a plug, fire
  1770. * of a plug trace.
  1771. *
  1772. * @request_count may become stale because of schedule
  1773. * out, so check plug list again.
  1774. */
  1775. if (!request_count || list_empty(&plug->list))
  1776. trace_block_plug(q);
  1777. else {
  1778. struct request *last = list_entry_rq(plug->list.prev);
  1779. if (request_count >= BLK_MAX_REQUEST_COUNT ||
  1780. blk_rq_bytes(last) >= BLK_PLUG_FLUSH_SIZE) {
  1781. blk_flush_plug_list(plug, false);
  1782. trace_block_plug(q);
  1783. }
  1784. }
  1785. list_add_tail(&req->queuelist, &plug->list);
  1786. blk_account_io_start(req, true);
  1787. } else {
  1788. spin_lock_irq(q->queue_lock);
  1789. add_acct_request(q, req, where);
  1790. __blk_run_queue(q);
  1791. out_unlock:
  1792. spin_unlock_irq(q->queue_lock);
  1793. }
  1794. return BLK_QC_T_NONE;
  1795. }
  1796. static void handle_bad_sector(struct bio *bio, sector_t maxsector)
  1797. {
  1798. char b[BDEVNAME_SIZE];
  1799. printk(KERN_INFO "attempt to access beyond end of device\n");
  1800. printk(KERN_INFO "%s: rw=%d, want=%Lu, limit=%Lu\n",
  1801. bio_devname(bio, b), bio->bi_opf,
  1802. (unsigned long long)bio_end_sector(bio),
  1803. (long long)maxsector);
  1804. }
  1805. #ifdef CONFIG_FAIL_MAKE_REQUEST
  1806. static DECLARE_FAULT_ATTR(fail_make_request);
  1807. static int __init setup_fail_make_request(char *str)
  1808. {
  1809. return setup_fault_attr(&fail_make_request, str);
  1810. }
  1811. __setup("fail_make_request=", setup_fail_make_request);
  1812. static bool should_fail_request(struct hd_struct *part, unsigned int bytes)
  1813. {
  1814. return part->make_it_fail && should_fail(&fail_make_request, bytes);
  1815. }
  1816. static int __init fail_make_request_debugfs(void)
  1817. {
  1818. struct dentry *dir = fault_create_debugfs_attr("fail_make_request",
  1819. NULL, &fail_make_request);
  1820. return PTR_ERR_OR_ZERO(dir);
  1821. }
  1822. late_initcall(fail_make_request_debugfs);
  1823. #else /* CONFIG_FAIL_MAKE_REQUEST */
  1824. static inline bool should_fail_request(struct hd_struct *part,
  1825. unsigned int bytes)
  1826. {
  1827. return false;
  1828. }
  1829. #endif /* CONFIG_FAIL_MAKE_REQUEST */
  1830. static inline bool bio_check_ro(struct bio *bio, struct hd_struct *part)
  1831. {
  1832. const int op = bio_op(bio);
  1833. if (part->policy && op_is_write(op)) {
  1834. char b[BDEVNAME_SIZE];
  1835. if (op_is_flush(bio->bi_opf) && !bio_sectors(bio))
  1836. return false;
  1837. WARN_ONCE(1,
  1838. "generic_make_request: Trying to write "
  1839. "to read-only block-device %s (partno %d)\n",
  1840. bio_devname(bio, b), part->partno);
  1841. /* Older lvm-tools actually trigger this */
  1842. return false;
  1843. }
  1844. return false;
  1845. }
  1846. static noinline int should_fail_bio(struct bio *bio)
  1847. {
  1848. if (should_fail_request(&bio->bi_disk->part0, bio->bi_iter.bi_size))
  1849. return -EIO;
  1850. return 0;
  1851. }
  1852. ALLOW_ERROR_INJECTION(should_fail_bio, ERRNO);
  1853. /*
  1854. * Check whether this bio extends beyond the end of the device or partition.
  1855. * This may well happen - the kernel calls bread() without checking the size of
  1856. * the device, e.g., when mounting a file system.
  1857. */
  1858. static inline int bio_check_eod(struct bio *bio, sector_t maxsector)
  1859. {
  1860. unsigned int nr_sectors = bio_sectors(bio);
  1861. if (nr_sectors && maxsector &&
  1862. (nr_sectors > maxsector ||
  1863. bio->bi_iter.bi_sector > maxsector - nr_sectors)) {
  1864. handle_bad_sector(bio, maxsector);
  1865. return -EIO;
  1866. }
  1867. return 0;
  1868. }
  1869. /*
  1870. * Remap block n of partition p to block n+start(p) of the disk.
  1871. */
  1872. static inline int blk_partition_remap(struct bio *bio)
  1873. {
  1874. struct hd_struct *p;
  1875. int ret = -EIO;
  1876. rcu_read_lock();
  1877. p = __disk_get_part(bio->bi_disk, bio->bi_partno);
  1878. if (unlikely(!p))
  1879. goto out;
  1880. if (unlikely(should_fail_request(p, bio->bi_iter.bi_size)))
  1881. goto out;
  1882. if (unlikely(bio_check_ro(bio, p)))
  1883. goto out;
  1884. /*
  1885. * Zone reset does not include bi_size so bio_sectors() is always 0.
  1886. * Include a test for the reset op code and perform the remap if needed.
  1887. */
  1888. if (bio_sectors(bio) || bio_op(bio) == REQ_OP_ZONE_RESET) {
  1889. if (bio_check_eod(bio, part_nr_sects_read(p)))
  1890. goto out;
  1891. bio->bi_iter.bi_sector += p->start_sect;
  1892. trace_block_bio_remap(bio->bi_disk->queue, bio, part_devt(p),
  1893. bio->bi_iter.bi_sector - p->start_sect);
  1894. }
  1895. bio->bi_partno = 0;
  1896. ret = 0;
  1897. out:
  1898. rcu_read_unlock();
  1899. return ret;
  1900. }
  1901. static noinline_for_stack bool
  1902. generic_make_request_checks(struct bio *bio)
  1903. {
  1904. struct request_queue *q;
  1905. int nr_sectors = bio_sectors(bio);
  1906. blk_status_t status = BLK_STS_IOERR;
  1907. char b[BDEVNAME_SIZE];
  1908. might_sleep();
  1909. q = bio->bi_disk->queue;
  1910. if (unlikely(!q)) {
  1911. printk(KERN_ERR
  1912. "generic_make_request: Trying to access "
  1913. "nonexistent block-device %s (%Lu)\n",
  1914. bio_devname(bio, b), (long long)bio->bi_iter.bi_sector);
  1915. goto end_io;
  1916. }
  1917. /*
  1918. * For a REQ_NOWAIT based request, return -EOPNOTSUPP
  1919. * if queue is not a request based queue.
  1920. */
  1921. if ((bio->bi_opf & REQ_NOWAIT) && !queue_is_rq_based(q))
  1922. goto not_supported;
  1923. if (should_fail_bio(bio))
  1924. goto end_io;
  1925. if (bio->bi_partno) {
  1926. if (unlikely(blk_partition_remap(bio)))
  1927. goto end_io;
  1928. } else {
  1929. if (unlikely(bio_check_ro(bio, &bio->bi_disk->part0)))
  1930. goto end_io;
  1931. if (unlikely(bio_check_eod(bio, get_capacity(bio->bi_disk))))
  1932. goto end_io;
  1933. }
  1934. /*
  1935. * Filter flush bio's early so that make_request based
  1936. * drivers without flush support don't have to worry
  1937. * about them.
  1938. */
  1939. if (op_is_flush(bio->bi_opf) &&
  1940. !test_bit(QUEUE_FLAG_WC, &q->queue_flags)) {
  1941. bio->bi_opf &= ~(REQ_PREFLUSH | REQ_FUA);
  1942. if (!nr_sectors) {
  1943. status = BLK_STS_OK;
  1944. goto end_io;
  1945. }
  1946. }
  1947. switch (bio_op(bio)) {
  1948. case REQ_OP_DISCARD:
  1949. if (!blk_queue_discard(q))
  1950. goto not_supported;
  1951. break;
  1952. case REQ_OP_SECURE_ERASE:
  1953. if (!blk_queue_secure_erase(q))
  1954. goto not_supported;
  1955. break;
  1956. case REQ_OP_WRITE_SAME:
  1957. if (!q->limits.max_write_same_sectors)
  1958. goto not_supported;
  1959. break;
  1960. case REQ_OP_ZONE_RESET:
  1961. if (!blk_queue_is_zoned(q))
  1962. goto not_supported;
  1963. break;
  1964. case REQ_OP_WRITE_ZEROES:
  1965. if (!q->limits.max_write_zeroes_sectors)
  1966. goto not_supported;
  1967. break;
  1968. default:
  1969. break;
  1970. }
  1971. /*
  1972. * Various block parts want %current->io_context and lazy ioc
  1973. * allocation ends up trading a lot of pain for a small amount of
  1974. * memory. Just allocate it upfront. This may fail and block
  1975. * layer knows how to live with it.
  1976. */
  1977. create_io_context(GFP_ATOMIC, q->node);
  1978. if (!blkcg_bio_issue_check(q, bio))
  1979. return false;
  1980. if (!bio_flagged(bio, BIO_TRACE_COMPLETION)) {
  1981. trace_block_bio_queue(q, bio);
  1982. /* Now that enqueuing has been traced, we need to trace
  1983. * completion as well.
  1984. */
  1985. bio_set_flag(bio, BIO_TRACE_COMPLETION);
  1986. }
  1987. return true;
  1988. not_supported:
  1989. status = BLK_STS_NOTSUPP;
  1990. end_io:
  1991. bio->bi_status = status;
  1992. bio_endio(bio);
  1993. return false;
  1994. }
  1995. /**
  1996. * generic_make_request - hand a buffer to its device driver for I/O
  1997. * @bio: The bio describing the location in memory and on the device.
  1998. *
  1999. * generic_make_request() is used to make I/O requests of block
  2000. * devices. It is passed a &struct bio, which describes the I/O that needs
  2001. * to be done.
  2002. *
  2003. * generic_make_request() does not return any status. The
  2004. * success/failure status of the request, along with notification of
  2005. * completion, is delivered asynchronously through the bio->bi_end_io
  2006. * function described (one day) else where.
  2007. *
  2008. * The caller of generic_make_request must make sure that bi_io_vec
  2009. * are set to describe the memory buffer, and that bi_dev and bi_sector are
  2010. * set to describe the device address, and the
  2011. * bi_end_io and optionally bi_private are set to describe how
  2012. * completion notification should be signaled.
  2013. *
  2014. * generic_make_request and the drivers it calls may use bi_next if this
  2015. * bio happens to be merged with someone else, and may resubmit the bio to
  2016. * a lower device by calling into generic_make_request recursively, which
  2017. * means the bio should NOT be touched after the call to ->make_request_fn.
  2018. */
  2019. blk_qc_t generic_make_request(struct bio *bio)
  2020. {
  2021. /*
  2022. * bio_list_on_stack[0] contains bios submitted by the current
  2023. * make_request_fn.
  2024. * bio_list_on_stack[1] contains bios that were submitted before
  2025. * the current make_request_fn, but that haven't been processed
  2026. * yet.
  2027. */
  2028. struct bio_list bio_list_on_stack[2];
  2029. blk_mq_req_flags_t flags = 0;
  2030. struct request_queue *q = bio->bi_disk->queue;
  2031. blk_qc_t ret = BLK_QC_T_NONE;
  2032. if (bio->bi_opf & REQ_NOWAIT)
  2033. flags = BLK_MQ_REQ_NOWAIT;
  2034. if (bio_flagged(bio, BIO_QUEUE_ENTERED))
  2035. blk_queue_enter_live(q);
  2036. else if (blk_queue_enter(q, flags) < 0) {
  2037. if (!blk_queue_dying(q) && (bio->bi_opf & REQ_NOWAIT))
  2038. bio_wouldblock_error(bio);
  2039. else
  2040. bio_io_error(bio);
  2041. return ret;
  2042. }
  2043. if (!generic_make_request_checks(bio))
  2044. goto out;
  2045. /*
  2046. * We only want one ->make_request_fn to be active at a time, else
  2047. * stack usage with stacked devices could be a problem. So use
  2048. * current->bio_list to keep a list of requests submited by a
  2049. * make_request_fn function. current->bio_list is also used as a
  2050. * flag to say if generic_make_request is currently active in this
  2051. * task or not. If it is NULL, then no make_request is active. If
  2052. * it is non-NULL, then a make_request is active, and new requests
  2053. * should be added at the tail
  2054. */
  2055. if (current->bio_list) {
  2056. bio_list_add(&current->bio_list[0], bio);
  2057. goto out;
  2058. }
  2059. /* following loop may be a bit non-obvious, and so deserves some
  2060. * explanation.
  2061. * Before entering the loop, bio->bi_next is NULL (as all callers
  2062. * ensure that) so we have a list with a single bio.
  2063. * We pretend that we have just taken it off a longer list, so
  2064. * we assign bio_list to a pointer to the bio_list_on_stack,
  2065. * thus initialising the bio_list of new bios to be
  2066. * added. ->make_request() may indeed add some more bios
  2067. * through a recursive call to generic_make_request. If it
  2068. * did, we find a non-NULL value in bio_list and re-enter the loop
  2069. * from the top. In this case we really did just take the bio
  2070. * of the top of the list (no pretending) and so remove it from
  2071. * bio_list, and call into ->make_request() again.
  2072. */
  2073. BUG_ON(bio->bi_next);
  2074. bio_list_init(&bio_list_on_stack[0]);
  2075. current->bio_list = bio_list_on_stack;
  2076. do {
  2077. bool enter_succeeded = true;
  2078. if (unlikely(q != bio->bi_disk->queue)) {
  2079. if (q)
  2080. blk_queue_exit(q);
  2081. q = bio->bi_disk->queue;
  2082. flags = 0;
  2083. if (bio->bi_opf & REQ_NOWAIT)
  2084. flags = BLK_MQ_REQ_NOWAIT;
  2085. if (blk_queue_enter(q, flags) < 0) {
  2086. enter_succeeded = false;
  2087. q = NULL;
  2088. }
  2089. }
  2090. if (enter_succeeded) {
  2091. struct bio_list lower, same;
  2092. /* Create a fresh bio_list for all subordinate requests */
  2093. bio_list_on_stack[1] = bio_list_on_stack[0];
  2094. bio_list_init(&bio_list_on_stack[0]);
  2095. ret = q->make_request_fn(q, bio);
  2096. /* sort new bios into those for a lower level
  2097. * and those for the same level
  2098. */
  2099. bio_list_init(&lower);
  2100. bio_list_init(&same);
  2101. while ((bio = bio_list_pop(&bio_list_on_stack[0])) != NULL)
  2102. if (q == bio->bi_disk->queue)
  2103. bio_list_add(&same, bio);
  2104. else
  2105. bio_list_add(&lower, bio);
  2106. /* now assemble so we handle the lowest level first */
  2107. bio_list_merge(&bio_list_on_stack[0], &lower);
  2108. bio_list_merge(&bio_list_on_stack[0], &same);
  2109. bio_list_merge(&bio_list_on_stack[0], &bio_list_on_stack[1]);
  2110. } else {
  2111. if (unlikely(!blk_queue_dying(q) &&
  2112. (bio->bi_opf & REQ_NOWAIT)))
  2113. bio_wouldblock_error(bio);
  2114. else
  2115. bio_io_error(bio);
  2116. }
  2117. bio = bio_list_pop(&bio_list_on_stack[0]);
  2118. } while (bio);
  2119. current->bio_list = NULL; /* deactivate */
  2120. out:
  2121. if (q)
  2122. blk_queue_exit(q);
  2123. return ret;
  2124. }
  2125. EXPORT_SYMBOL(generic_make_request);
  2126. /**
  2127. * direct_make_request - hand a buffer directly to its device driver for I/O
  2128. * @bio: The bio describing the location in memory and on the device.
  2129. *
  2130. * This function behaves like generic_make_request(), but does not protect
  2131. * against recursion. Must only be used if the called driver is known
  2132. * to not call generic_make_request (or direct_make_request) again from
  2133. * its make_request function. (Calling direct_make_request again from
  2134. * a workqueue is perfectly fine as that doesn't recurse).
  2135. */
  2136. blk_qc_t direct_make_request(struct bio *bio)
  2137. {
  2138. struct request_queue *q = bio->bi_disk->queue;
  2139. bool nowait = bio->bi_opf & REQ_NOWAIT;
  2140. blk_qc_t ret;
  2141. if (!generic_make_request_checks(bio))
  2142. return BLK_QC_T_NONE;
  2143. if (unlikely(blk_queue_enter(q, nowait ? BLK_MQ_REQ_NOWAIT : 0))) {
  2144. if (nowait && !blk_queue_dying(q))
  2145. bio->bi_status = BLK_STS_AGAIN;
  2146. else
  2147. bio->bi_status = BLK_STS_IOERR;
  2148. bio_endio(bio);
  2149. return BLK_QC_T_NONE;
  2150. }
  2151. ret = q->make_request_fn(q, bio);
  2152. blk_queue_exit(q);
  2153. return ret;
  2154. }
  2155. EXPORT_SYMBOL_GPL(direct_make_request);
  2156. /**
  2157. * submit_bio - submit a bio to the block device layer for I/O
  2158. * @bio: The &struct bio which describes the I/O
  2159. *
  2160. * submit_bio() is very similar in purpose to generic_make_request(), and
  2161. * uses that function to do most of the work. Both are fairly rough
  2162. * interfaces; @bio must be presetup and ready for I/O.
  2163. *
  2164. */
  2165. blk_qc_t submit_bio(struct bio *bio)
  2166. {
  2167. /*
  2168. * If it's a regular read/write or a barrier with data attached,
  2169. * go through the normal accounting stuff before submission.
  2170. */
  2171. if (bio_has_data(bio)) {
  2172. unsigned int count;
  2173. if (unlikely(bio_op(bio) == REQ_OP_WRITE_SAME))
  2174. count = queue_logical_block_size(bio->bi_disk->queue) >> 9;
  2175. else
  2176. count = bio_sectors(bio);
  2177. if (op_is_write(bio_op(bio))) {
  2178. count_vm_events(PGPGOUT, count);
  2179. } else {
  2180. task_io_account_read(bio->bi_iter.bi_size);
  2181. count_vm_events(PGPGIN, count);
  2182. }
  2183. if (unlikely(block_dump)) {
  2184. char b[BDEVNAME_SIZE];
  2185. printk(KERN_DEBUG "%s(%d): %s block %Lu on %s (%u sectors)\n",
  2186. current->comm, task_pid_nr(current),
  2187. op_is_write(bio_op(bio)) ? "WRITE" : "READ",
  2188. (unsigned long long)bio->bi_iter.bi_sector,
  2189. bio_devname(bio, b), count);
  2190. }
  2191. }
  2192. return generic_make_request(bio);
  2193. }
  2194. EXPORT_SYMBOL(submit_bio);
  2195. bool blk_poll(struct request_queue *q, blk_qc_t cookie)
  2196. {
  2197. if (!q->poll_fn || !blk_qc_t_valid(cookie))
  2198. return false;
  2199. if (current->plug)
  2200. blk_flush_plug_list(current->plug, false);
  2201. return q->poll_fn(q, cookie);
  2202. }
  2203. EXPORT_SYMBOL_GPL(blk_poll);
  2204. /**
  2205. * blk_cloned_rq_check_limits - Helper function to check a cloned request
  2206. * for new the queue limits
  2207. * @q: the queue
  2208. * @rq: the request being checked
  2209. *
  2210. * Description:
  2211. * @rq may have been made based on weaker limitations of upper-level queues
  2212. * in request stacking drivers, and it may violate the limitation of @q.
  2213. * Since the block layer and the underlying device driver trust @rq
  2214. * after it is inserted to @q, it should be checked against @q before
  2215. * the insertion using this generic function.
  2216. *
  2217. * Request stacking drivers like request-based dm may change the queue
  2218. * limits when retrying requests on other queues. Those requests need
  2219. * to be checked against the new queue limits again during dispatch.
  2220. */
  2221. static int blk_cloned_rq_check_limits(struct request_queue *q,
  2222. struct request *rq)
  2223. {
  2224. if (blk_rq_sectors(rq) > blk_queue_get_max_sectors(q, req_op(rq))) {
  2225. printk(KERN_ERR "%s: over max size limit.\n", __func__);
  2226. return -EIO;
  2227. }
  2228. /*
  2229. * queue's settings related to segment counting like q->bounce_pfn
  2230. * may differ from that of other stacking queues.
  2231. * Recalculate it to check the request correctly on this queue's
  2232. * limitation.
  2233. */
  2234. blk_recalc_rq_segments(rq);
  2235. if (rq->nr_phys_segments > queue_max_segments(q)) {
  2236. printk(KERN_ERR "%s: over max segments limit.\n", __func__);
  2237. return -EIO;
  2238. }
  2239. return 0;
  2240. }
  2241. /**
  2242. * blk_insert_cloned_request - Helper for stacking drivers to submit a request
  2243. * @q: the queue to submit the request
  2244. * @rq: the request being queued
  2245. */
  2246. blk_status_t blk_insert_cloned_request(struct request_queue *q, struct request *rq)
  2247. {
  2248. unsigned long flags;
  2249. int where = ELEVATOR_INSERT_BACK;
  2250. if (blk_cloned_rq_check_limits(q, rq))
  2251. return BLK_STS_IOERR;
  2252. if (rq->rq_disk &&
  2253. should_fail_request(&rq->rq_disk->part0, blk_rq_bytes(rq)))
  2254. return BLK_STS_IOERR;
  2255. if (q->mq_ops) {
  2256. if (blk_queue_io_stat(q))
  2257. blk_account_io_start(rq, true);
  2258. /*
  2259. * Since we have a scheduler attached on the top device,
  2260. * bypass a potential scheduler on the bottom device for
  2261. * insert.
  2262. */
  2263. return blk_mq_request_issue_directly(rq);
  2264. }
  2265. spin_lock_irqsave(q->queue_lock, flags);
  2266. if (unlikely(blk_queue_dying(q))) {
  2267. spin_unlock_irqrestore(q->queue_lock, flags);
  2268. return BLK_STS_IOERR;
  2269. }
  2270. /*
  2271. * Submitting request must be dequeued before calling this function
  2272. * because it will be linked to another request_queue
  2273. */
  2274. BUG_ON(blk_queued_rq(rq));
  2275. if (op_is_flush(rq->cmd_flags))
  2276. where = ELEVATOR_INSERT_FLUSH;
  2277. add_acct_request(q, rq, where);
  2278. if (where == ELEVATOR_INSERT_FLUSH)
  2279. __blk_run_queue(q);
  2280. spin_unlock_irqrestore(q->queue_lock, flags);
  2281. return BLK_STS_OK;
  2282. }
  2283. EXPORT_SYMBOL_GPL(blk_insert_cloned_request);
  2284. /**
  2285. * blk_rq_err_bytes - determine number of bytes till the next failure boundary
  2286. * @rq: request to examine
  2287. *
  2288. * Description:
  2289. * A request could be merge of IOs which require different failure
  2290. * handling. This function determines the number of bytes which
  2291. * can be failed from the beginning of the request without
  2292. * crossing into area which need to be retried further.
  2293. *
  2294. * Return:
  2295. * The number of bytes to fail.
  2296. */
  2297. unsigned int blk_rq_err_bytes(const struct request *rq)
  2298. {
  2299. unsigned int ff = rq->cmd_flags & REQ_FAILFAST_MASK;
  2300. unsigned int bytes = 0;
  2301. struct bio *bio;
  2302. if (!(rq->rq_flags & RQF_MIXED_MERGE))
  2303. return blk_rq_bytes(rq);
  2304. /*
  2305. * Currently the only 'mixing' which can happen is between
  2306. * different fastfail types. We can safely fail portions
  2307. * which have all the failfast bits that the first one has -
  2308. * the ones which are at least as eager to fail as the first
  2309. * one.
  2310. */
  2311. for (bio = rq->bio; bio; bio = bio->bi_next) {
  2312. if ((bio->bi_opf & ff) != ff)
  2313. break;
  2314. bytes += bio->bi_iter.bi_size;
  2315. }
  2316. /* this could lead to infinite loop */
  2317. BUG_ON(blk_rq_bytes(rq) && !bytes);
  2318. return bytes;
  2319. }
  2320. EXPORT_SYMBOL_GPL(blk_rq_err_bytes);
  2321. void blk_account_io_completion(struct request *req, unsigned int bytes)
  2322. {
  2323. if (blk_do_io_stat(req)) {
  2324. const int sgrp = op_stat_group(req_op(req));
  2325. struct hd_struct *part;
  2326. int cpu;
  2327. cpu = part_stat_lock();
  2328. part = req->part;
  2329. part_stat_add(cpu, part, sectors[sgrp], bytes >> 9);
  2330. part_stat_unlock();
  2331. }
  2332. }
  2333. void blk_account_io_done(struct request *req, u64 now)
  2334. {
  2335. /*
  2336. * Account IO completion. flush_rq isn't accounted as a
  2337. * normal IO on queueing nor completion. Accounting the
  2338. * containing request is enough.
  2339. */
  2340. if (blk_do_io_stat(req) && !(req->rq_flags & RQF_FLUSH_SEQ)) {
  2341. const int sgrp = op_stat_group(req_op(req));
  2342. struct hd_struct *part;
  2343. int cpu;
  2344. cpu = part_stat_lock();
  2345. part = req->part;
  2346. part_stat_inc(cpu, part, ios[sgrp]);
  2347. part_stat_add(cpu, part, nsecs[sgrp], now - req->start_time_ns);
  2348. part_round_stats(req->q, cpu, part);
  2349. part_dec_in_flight(req->q, part, rq_data_dir(req));
  2350. hd_struct_put(part);
  2351. part_stat_unlock();
  2352. }
  2353. }
  2354. void blk_account_io_start(struct request *rq, bool new_io)
  2355. {
  2356. struct hd_struct *part;
  2357. int rw = rq_data_dir(rq);
  2358. int cpu;
  2359. if (!blk_do_io_stat(rq))
  2360. return;
  2361. cpu = part_stat_lock();
  2362. if (!new_io) {
  2363. part = rq->part;
  2364. part_stat_inc(cpu, part, merges[rw]);
  2365. } else {
  2366. part = disk_map_sector_rcu(rq->rq_disk, blk_rq_pos(rq));
  2367. if (!hd_struct_try_get(part)) {
  2368. /*
  2369. * The partition is already being removed,
  2370. * the request will be accounted on the disk only
  2371. *
  2372. * We take a reference on disk->part0 although that
  2373. * partition will never be deleted, so we can treat
  2374. * it as any other partition.
  2375. */
  2376. part = &rq->rq_disk->part0;
  2377. hd_struct_get(part);
  2378. }
  2379. part_round_stats(rq->q, cpu, part);
  2380. part_inc_in_flight(rq->q, part, rw);
  2381. rq->part = part;
  2382. }
  2383. part_stat_unlock();
  2384. }
  2385. static struct request *elv_next_request(struct request_queue *q)
  2386. {
  2387. struct request *rq;
  2388. struct blk_flush_queue *fq = blk_get_flush_queue(q, NULL);
  2389. WARN_ON_ONCE(q->mq_ops);
  2390. while (1) {
  2391. list_for_each_entry(rq, &q->queue_head, queuelist) {
  2392. #ifdef CONFIG_PM
  2393. /*
  2394. * If a request gets queued in state RPM_SUSPENDED
  2395. * then that's a kernel bug.
  2396. */
  2397. WARN_ON_ONCE(q->rpm_status == RPM_SUSPENDED);
  2398. #endif
  2399. return rq;
  2400. }
  2401. /*
  2402. * Flush request is running and flush request isn't queueable
  2403. * in the drive, we can hold the queue till flush request is
  2404. * finished. Even we don't do this, driver can't dispatch next
  2405. * requests and will requeue them. And this can improve
  2406. * throughput too. For example, we have request flush1, write1,
  2407. * flush 2. flush1 is dispatched, then queue is hold, write1
  2408. * isn't inserted to queue. After flush1 is finished, flush2
  2409. * will be dispatched. Since disk cache is already clean,
  2410. * flush2 will be finished very soon, so looks like flush2 is
  2411. * folded to flush1.
  2412. * Since the queue is hold, a flag is set to indicate the queue
  2413. * should be restarted later. Please see flush_end_io() for
  2414. * details.
  2415. */
  2416. if (fq->flush_pending_idx != fq->flush_running_idx &&
  2417. !queue_flush_queueable(q)) {
  2418. fq->flush_queue_delayed = 1;
  2419. return NULL;
  2420. }
  2421. if (unlikely(blk_queue_bypass(q)) ||
  2422. !q->elevator->type->ops.sq.elevator_dispatch_fn(q, 0))
  2423. return NULL;
  2424. }
  2425. }
  2426. /**
  2427. * blk_peek_request - peek at the top of a request queue
  2428. * @q: request queue to peek at
  2429. *
  2430. * Description:
  2431. * Return the request at the top of @q. The returned request
  2432. * should be started using blk_start_request() before LLD starts
  2433. * processing it.
  2434. *
  2435. * Return:
  2436. * Pointer to the request at the top of @q if available. Null
  2437. * otherwise.
  2438. */
  2439. struct request *blk_peek_request(struct request_queue *q)
  2440. {
  2441. struct request *rq;
  2442. int ret;
  2443. lockdep_assert_held(q->queue_lock);
  2444. WARN_ON_ONCE(q->mq_ops);
  2445. while ((rq = elv_next_request(q)) != NULL) {
  2446. if (!(rq->rq_flags & RQF_STARTED)) {
  2447. /*
  2448. * This is the first time the device driver
  2449. * sees this request (possibly after
  2450. * requeueing). Notify IO scheduler.
  2451. */
  2452. if (rq->rq_flags & RQF_SORTED)
  2453. elv_activate_rq(q, rq);
  2454. /*
  2455. * just mark as started even if we don't start
  2456. * it, a request that has been delayed should
  2457. * not be passed by new incoming requests
  2458. */
  2459. rq->rq_flags |= RQF_STARTED;
  2460. trace_block_rq_issue(q, rq);
  2461. }
  2462. if (!q->boundary_rq || q->boundary_rq == rq) {
  2463. q->end_sector = rq_end_sector(rq);
  2464. q->boundary_rq = NULL;
  2465. }
  2466. if (rq->rq_flags & RQF_DONTPREP)
  2467. break;
  2468. if (q->dma_drain_size && blk_rq_bytes(rq)) {
  2469. /*
  2470. * make sure space for the drain appears we
  2471. * know we can do this because max_hw_segments
  2472. * has been adjusted to be one fewer than the
  2473. * device can handle
  2474. */
  2475. rq->nr_phys_segments++;
  2476. }
  2477. if (!q->prep_rq_fn)
  2478. break;
  2479. ret = q->prep_rq_fn(q, rq);
  2480. if (ret == BLKPREP_OK) {
  2481. break;
  2482. } else if (ret == BLKPREP_DEFER) {
  2483. /*
  2484. * the request may have been (partially) prepped.
  2485. * we need to keep this request in the front to
  2486. * avoid resource deadlock. RQF_STARTED will
  2487. * prevent other fs requests from passing this one.
  2488. */
  2489. if (q->dma_drain_size && blk_rq_bytes(rq) &&
  2490. !(rq->rq_flags & RQF_DONTPREP)) {
  2491. /*
  2492. * remove the space for the drain we added
  2493. * so that we don't add it again
  2494. */
  2495. --rq->nr_phys_segments;
  2496. }
  2497. rq = NULL;
  2498. break;
  2499. } else if (ret == BLKPREP_KILL || ret == BLKPREP_INVALID) {
  2500. rq->rq_flags |= RQF_QUIET;
  2501. /*
  2502. * Mark this request as started so we don't trigger
  2503. * any debug logic in the end I/O path.
  2504. */
  2505. blk_start_request(rq);
  2506. __blk_end_request_all(rq, ret == BLKPREP_INVALID ?
  2507. BLK_STS_TARGET : BLK_STS_IOERR);
  2508. } else {
  2509. printk(KERN_ERR "%s: bad return=%d\n", __func__, ret);
  2510. break;
  2511. }
  2512. }
  2513. return rq;
  2514. }
  2515. EXPORT_SYMBOL(blk_peek_request);
  2516. static void blk_dequeue_request(struct request *rq)
  2517. {
  2518. struct request_queue *q = rq->q;
  2519. BUG_ON(list_empty(&rq->queuelist));
  2520. BUG_ON(ELV_ON_HASH(rq));
  2521. list_del_init(&rq->queuelist);
  2522. /*
  2523. * the time frame between a request being removed from the lists
  2524. * and to it is freed is accounted as io that is in progress at
  2525. * the driver side.
  2526. */
  2527. if (blk_account_rq(rq))
  2528. q->in_flight[rq_is_sync(rq)]++;
  2529. }
  2530. /**
  2531. * blk_start_request - start request processing on the driver
  2532. * @req: request to dequeue
  2533. *
  2534. * Description:
  2535. * Dequeue @req and start timeout timer on it. This hands off the
  2536. * request to the driver.
  2537. */
  2538. void blk_start_request(struct request *req)
  2539. {
  2540. lockdep_assert_held(req->q->queue_lock);
  2541. WARN_ON_ONCE(req->q->mq_ops);
  2542. blk_dequeue_request(req);
  2543. if (test_bit(QUEUE_FLAG_STATS, &req->q->queue_flags)) {
  2544. req->io_start_time_ns = ktime_get_ns();
  2545. #ifdef CONFIG_BLK_DEV_THROTTLING_LOW
  2546. req->throtl_size = blk_rq_sectors(req);
  2547. #endif
  2548. req->rq_flags |= RQF_STATS;
  2549. rq_qos_issue(req->q, req);
  2550. }
  2551. BUG_ON(blk_rq_is_complete(req));
  2552. blk_add_timer(req);
  2553. }
  2554. EXPORT_SYMBOL(blk_start_request);
  2555. /**
  2556. * blk_fetch_request - fetch a request from a request queue
  2557. * @q: request queue to fetch a request from
  2558. *
  2559. * Description:
  2560. * Return the request at the top of @q. The request is started on
  2561. * return and LLD can start processing it immediately.
  2562. *
  2563. * Return:
  2564. * Pointer to the request at the top of @q if available. Null
  2565. * otherwise.
  2566. */
  2567. struct request *blk_fetch_request(struct request_queue *q)
  2568. {
  2569. struct request *rq;
  2570. lockdep_assert_held(q->queue_lock);
  2571. WARN_ON_ONCE(q->mq_ops);
  2572. rq = blk_peek_request(q);
  2573. if (rq)
  2574. blk_start_request(rq);
  2575. return rq;
  2576. }
  2577. EXPORT_SYMBOL(blk_fetch_request);
  2578. /*
  2579. * Steal bios from a request and add them to a bio list.
  2580. * The request must not have been partially completed before.
  2581. */
  2582. void blk_steal_bios(struct bio_list *list, struct request *rq)
  2583. {
  2584. if (rq->bio) {
  2585. if (list->tail)
  2586. list->tail->bi_next = rq->bio;
  2587. else
  2588. list->head = rq->bio;
  2589. list->tail = rq->biotail;
  2590. rq->bio = NULL;
  2591. rq->biotail = NULL;
  2592. }
  2593. rq->__data_len = 0;
  2594. }
  2595. EXPORT_SYMBOL_GPL(blk_steal_bios);
  2596. /**
  2597. * blk_update_request - Special helper function for request stacking drivers
  2598. * @req: the request being processed
  2599. * @error: block status code
  2600. * @nr_bytes: number of bytes to complete @req
  2601. *
  2602. * Description:
  2603. * Ends I/O on a number of bytes attached to @req, but doesn't complete
  2604. * the request structure even if @req doesn't have leftover.
  2605. * If @req has leftover, sets it up for the next range of segments.
  2606. *
  2607. * This special helper function is only for request stacking drivers
  2608. * (e.g. request-based dm) so that they can handle partial completion.
  2609. * Actual device drivers should use blk_end_request instead.
  2610. *
  2611. * Passing the result of blk_rq_bytes() as @nr_bytes guarantees
  2612. * %false return from this function.
  2613. *
  2614. * Note:
  2615. * The RQF_SPECIAL_PAYLOAD flag is ignored on purpose in both
  2616. * blk_rq_bytes() and in blk_update_request().
  2617. *
  2618. * Return:
  2619. * %false - this request doesn't have any more data
  2620. * %true - this request has more data
  2621. **/
  2622. bool blk_update_request(struct request *req, blk_status_t error,
  2623. unsigned int nr_bytes)
  2624. {
  2625. int total_bytes;
  2626. trace_block_rq_complete(req, blk_status_to_errno(error), nr_bytes);
  2627. if (!req->bio)
  2628. return false;
  2629. if (unlikely(error && !blk_rq_is_passthrough(req) &&
  2630. !(req->rq_flags & RQF_QUIET)))
  2631. print_req_error(req, error);
  2632. blk_account_io_completion(req, nr_bytes);
  2633. total_bytes = 0;
  2634. while (req->bio) {
  2635. struct bio *bio = req->bio;
  2636. unsigned bio_bytes = min(bio->bi_iter.bi_size, nr_bytes);
  2637. if (bio_bytes == bio->bi_iter.bi_size)
  2638. req->bio = bio->bi_next;
  2639. /* Completion has already been traced */
  2640. bio_clear_flag(bio, BIO_TRACE_COMPLETION);
  2641. req_bio_endio(req, bio, bio_bytes, error);
  2642. total_bytes += bio_bytes;
  2643. nr_bytes -= bio_bytes;
  2644. if (!nr_bytes)
  2645. break;
  2646. }
  2647. /*
  2648. * completely done
  2649. */
  2650. if (!req->bio) {
  2651. /*
  2652. * Reset counters so that the request stacking driver
  2653. * can find how many bytes remain in the request
  2654. * later.
  2655. */
  2656. req->__data_len = 0;
  2657. return false;
  2658. }
  2659. req->__data_len -= total_bytes;
  2660. /* update sector only for requests with clear definition of sector */
  2661. if (!blk_rq_is_passthrough(req))
  2662. req->__sector += total_bytes >> 9;
  2663. /* mixed attributes always follow the first bio */
  2664. if (req->rq_flags & RQF_MIXED_MERGE) {
  2665. req->cmd_flags &= ~REQ_FAILFAST_MASK;
  2666. req->cmd_flags |= req->bio->bi_opf & REQ_FAILFAST_MASK;
  2667. }
  2668. if (!(req->rq_flags & RQF_SPECIAL_PAYLOAD)) {
  2669. /*
  2670. * If total number of sectors is less than the first segment
  2671. * size, something has gone terribly wrong.
  2672. */
  2673. if (blk_rq_bytes(req) < blk_rq_cur_bytes(req)) {
  2674. blk_dump_rq_flags(req, "request botched");
  2675. req->__data_len = blk_rq_cur_bytes(req);
  2676. }
  2677. /* recalculate the number of segments */
  2678. blk_recalc_rq_segments(req);
  2679. }
  2680. return true;
  2681. }
  2682. EXPORT_SYMBOL_GPL(blk_update_request);
  2683. static bool blk_update_bidi_request(struct request *rq, blk_status_t error,
  2684. unsigned int nr_bytes,
  2685. unsigned int bidi_bytes)
  2686. {
  2687. if (blk_update_request(rq, error, nr_bytes))
  2688. return true;
  2689. /* Bidi request must be completed as a whole */
  2690. if (unlikely(blk_bidi_rq(rq)) &&
  2691. blk_update_request(rq->next_rq, error, bidi_bytes))
  2692. return true;
  2693. if (blk_queue_add_random(rq->q))
  2694. add_disk_randomness(rq->rq_disk);
  2695. return false;
  2696. }
  2697. /**
  2698. * blk_unprep_request - unprepare a request
  2699. * @req: the request
  2700. *
  2701. * This function makes a request ready for complete resubmission (or
  2702. * completion). It happens only after all error handling is complete,
  2703. * so represents the appropriate moment to deallocate any resources
  2704. * that were allocated to the request in the prep_rq_fn. The queue
  2705. * lock is held when calling this.
  2706. */
  2707. void blk_unprep_request(struct request *req)
  2708. {
  2709. struct request_queue *q = req->q;
  2710. req->rq_flags &= ~RQF_DONTPREP;
  2711. if (q->unprep_rq_fn)
  2712. q->unprep_rq_fn(q, req);
  2713. }
  2714. EXPORT_SYMBOL_GPL(blk_unprep_request);
  2715. void blk_finish_request(struct request *req, blk_status_t error)
  2716. {
  2717. struct request_queue *q = req->q;
  2718. u64 now = ktime_get_ns();
  2719. lockdep_assert_held(req->q->queue_lock);
  2720. WARN_ON_ONCE(q->mq_ops);
  2721. if (req->rq_flags & RQF_STATS)
  2722. blk_stat_add(req, now);
  2723. if (req->rq_flags & RQF_QUEUED)
  2724. blk_queue_end_tag(q, req);
  2725. BUG_ON(blk_queued_rq(req));
  2726. if (unlikely(laptop_mode) && !blk_rq_is_passthrough(req))
  2727. laptop_io_completion(req->q->backing_dev_info);
  2728. blk_delete_timer(req);
  2729. if (req->rq_flags & RQF_DONTPREP)
  2730. blk_unprep_request(req);
  2731. blk_account_io_done(req, now);
  2732. if (req->end_io) {
  2733. rq_qos_done(q, req);
  2734. req->end_io(req, error);
  2735. } else {
  2736. if (blk_bidi_rq(req))
  2737. __blk_put_request(req->next_rq->q, req->next_rq);
  2738. __blk_put_request(q, req);
  2739. }
  2740. }
  2741. EXPORT_SYMBOL(blk_finish_request);
  2742. /**
  2743. * blk_end_bidi_request - Complete a bidi request
  2744. * @rq: the request to complete
  2745. * @error: block status code
  2746. * @nr_bytes: number of bytes to complete @rq
  2747. * @bidi_bytes: number of bytes to complete @rq->next_rq
  2748. *
  2749. * Description:
  2750. * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
  2751. * Drivers that supports bidi can safely call this member for any
  2752. * type of request, bidi or uni. In the later case @bidi_bytes is
  2753. * just ignored.
  2754. *
  2755. * Return:
  2756. * %false - we are done with this request
  2757. * %true - still buffers pending for this request
  2758. **/
  2759. static bool blk_end_bidi_request(struct request *rq, blk_status_t error,
  2760. unsigned int nr_bytes, unsigned int bidi_bytes)
  2761. {
  2762. struct request_queue *q = rq->q;
  2763. unsigned long flags;
  2764. WARN_ON_ONCE(q->mq_ops);
  2765. if (blk_update_bidi_request(rq, error, nr_bytes, bidi_bytes))
  2766. return true;
  2767. spin_lock_irqsave(q->queue_lock, flags);
  2768. blk_finish_request(rq, error);
  2769. spin_unlock_irqrestore(q->queue_lock, flags);
  2770. return false;
  2771. }
  2772. /**
  2773. * __blk_end_bidi_request - Complete a bidi request with queue lock held
  2774. * @rq: the request to complete
  2775. * @error: block status code
  2776. * @nr_bytes: number of bytes to complete @rq
  2777. * @bidi_bytes: number of bytes to complete @rq->next_rq
  2778. *
  2779. * Description:
  2780. * Identical to blk_end_bidi_request() except that queue lock is
  2781. * assumed to be locked on entry and remains so on return.
  2782. *
  2783. * Return:
  2784. * %false - we are done with this request
  2785. * %true - still buffers pending for this request
  2786. **/
  2787. static bool __blk_end_bidi_request(struct request *rq, blk_status_t error,
  2788. unsigned int nr_bytes, unsigned int bidi_bytes)
  2789. {
  2790. lockdep_assert_held(rq->q->queue_lock);
  2791. WARN_ON_ONCE(rq->q->mq_ops);
  2792. if (blk_update_bidi_request(rq, error, nr_bytes, bidi_bytes))
  2793. return true;
  2794. blk_finish_request(rq, error);
  2795. return false;
  2796. }
  2797. /**
  2798. * blk_end_request - Helper function for drivers to complete the request.
  2799. * @rq: the request being processed
  2800. * @error: block status code
  2801. * @nr_bytes: number of bytes to complete
  2802. *
  2803. * Description:
  2804. * Ends I/O on a number of bytes attached to @rq.
  2805. * If @rq has leftover, sets it up for the next range of segments.
  2806. *
  2807. * Return:
  2808. * %false - we are done with this request
  2809. * %true - still buffers pending for this request
  2810. **/
  2811. bool blk_end_request(struct request *rq, blk_status_t error,
  2812. unsigned int nr_bytes)
  2813. {
  2814. WARN_ON_ONCE(rq->q->mq_ops);
  2815. return blk_end_bidi_request(rq, error, nr_bytes, 0);
  2816. }
  2817. EXPORT_SYMBOL(blk_end_request);
  2818. /**
  2819. * blk_end_request_all - Helper function for drives to finish the request.
  2820. * @rq: the request to finish
  2821. * @error: block status code
  2822. *
  2823. * Description:
  2824. * Completely finish @rq.
  2825. */
  2826. void blk_end_request_all(struct request *rq, blk_status_t error)
  2827. {
  2828. bool pending;
  2829. unsigned int bidi_bytes = 0;
  2830. if (unlikely(blk_bidi_rq(rq)))
  2831. bidi_bytes = blk_rq_bytes(rq->next_rq);
  2832. pending = blk_end_bidi_request(rq, error, blk_rq_bytes(rq), bidi_bytes);
  2833. BUG_ON(pending);
  2834. }
  2835. EXPORT_SYMBOL(blk_end_request_all);
  2836. /**
  2837. * __blk_end_request - Helper function for drivers to complete the request.
  2838. * @rq: the request being processed
  2839. * @error: block status code
  2840. * @nr_bytes: number of bytes to complete
  2841. *
  2842. * Description:
  2843. * Must be called with queue lock held unlike blk_end_request().
  2844. *
  2845. * Return:
  2846. * %false - we are done with this request
  2847. * %true - still buffers pending for this request
  2848. **/
  2849. bool __blk_end_request(struct request *rq, blk_status_t error,
  2850. unsigned int nr_bytes)
  2851. {
  2852. lockdep_assert_held(rq->q->queue_lock);
  2853. WARN_ON_ONCE(rq->q->mq_ops);
  2854. return __blk_end_bidi_request(rq, error, nr_bytes, 0);
  2855. }
  2856. EXPORT_SYMBOL(__blk_end_request);
  2857. /**
  2858. * __blk_end_request_all - Helper function for drives to finish the request.
  2859. * @rq: the request to finish
  2860. * @error: block status code
  2861. *
  2862. * Description:
  2863. * Completely finish @rq. Must be called with queue lock held.
  2864. */
  2865. void __blk_end_request_all(struct request *rq, blk_status_t error)
  2866. {
  2867. bool pending;
  2868. unsigned int bidi_bytes = 0;
  2869. lockdep_assert_held(rq->q->queue_lock);
  2870. WARN_ON_ONCE(rq->q->mq_ops);
  2871. if (unlikely(blk_bidi_rq(rq)))
  2872. bidi_bytes = blk_rq_bytes(rq->next_rq);
  2873. pending = __blk_end_bidi_request(rq, error, blk_rq_bytes(rq), bidi_bytes);
  2874. BUG_ON(pending);
  2875. }
  2876. EXPORT_SYMBOL(__blk_end_request_all);
  2877. /**
  2878. * __blk_end_request_cur - Helper function to finish the current request chunk.
  2879. * @rq: the request to finish the current chunk for
  2880. * @error: block status code
  2881. *
  2882. * Description:
  2883. * Complete the current consecutively mapped chunk from @rq. Must
  2884. * be called with queue lock held.
  2885. *
  2886. * Return:
  2887. * %false - we are done with this request
  2888. * %true - still buffers pending for this request
  2889. */
  2890. bool __blk_end_request_cur(struct request *rq, blk_status_t error)
  2891. {
  2892. return __blk_end_request(rq, error, blk_rq_cur_bytes(rq));
  2893. }
  2894. EXPORT_SYMBOL(__blk_end_request_cur);
  2895. void blk_rq_bio_prep(struct request_queue *q, struct request *rq,
  2896. struct bio *bio)
  2897. {
  2898. if (bio_has_data(bio))
  2899. rq->nr_phys_segments = bio_phys_segments(q, bio);
  2900. else if (bio_op(bio) == REQ_OP_DISCARD)
  2901. rq->nr_phys_segments = 1;
  2902. rq->__data_len = bio->bi_iter.bi_size;
  2903. rq->bio = rq->biotail = bio;
  2904. if (bio->bi_disk)
  2905. rq->rq_disk = bio->bi_disk;
  2906. }
  2907. #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
  2908. /**
  2909. * rq_flush_dcache_pages - Helper function to flush all pages in a request
  2910. * @rq: the request to be flushed
  2911. *
  2912. * Description:
  2913. * Flush all pages in @rq.
  2914. */
  2915. void rq_flush_dcache_pages(struct request *rq)
  2916. {
  2917. struct req_iterator iter;
  2918. struct bio_vec bvec;
  2919. rq_for_each_segment(bvec, rq, iter)
  2920. flush_dcache_page(bvec.bv_page);
  2921. }
  2922. EXPORT_SYMBOL_GPL(rq_flush_dcache_pages);
  2923. #endif
  2924. /**
  2925. * blk_lld_busy - Check if underlying low-level drivers of a device are busy
  2926. * @q : the queue of the device being checked
  2927. *
  2928. * Description:
  2929. * Check if underlying low-level drivers of a device are busy.
  2930. * If the drivers want to export their busy state, they must set own
  2931. * exporting function using blk_queue_lld_busy() first.
  2932. *
  2933. * Basically, this function is used only by request stacking drivers
  2934. * to stop dispatching requests to underlying devices when underlying
  2935. * devices are busy. This behavior helps more I/O merging on the queue
  2936. * of the request stacking driver and prevents I/O throughput regression
  2937. * on burst I/O load.
  2938. *
  2939. * Return:
  2940. * 0 - Not busy (The request stacking driver should dispatch request)
  2941. * 1 - Busy (The request stacking driver should stop dispatching request)
  2942. */
  2943. int blk_lld_busy(struct request_queue *q)
  2944. {
  2945. if (q->lld_busy_fn)
  2946. return q->lld_busy_fn(q);
  2947. return 0;
  2948. }
  2949. EXPORT_SYMBOL_GPL(blk_lld_busy);
  2950. /**
  2951. * blk_rq_unprep_clone - Helper function to free all bios in a cloned request
  2952. * @rq: the clone request to be cleaned up
  2953. *
  2954. * Description:
  2955. * Free all bios in @rq for a cloned request.
  2956. */
  2957. void blk_rq_unprep_clone(struct request *rq)
  2958. {
  2959. struct bio *bio;
  2960. while ((bio = rq->bio) != NULL) {
  2961. rq->bio = bio->bi_next;
  2962. bio_put(bio);
  2963. }
  2964. }
  2965. EXPORT_SYMBOL_GPL(blk_rq_unprep_clone);
  2966. /*
  2967. * Copy attributes of the original request to the clone request.
  2968. * The actual data parts (e.g. ->cmd, ->sense) are not copied.
  2969. */
  2970. static void __blk_rq_prep_clone(struct request *dst, struct request *src)
  2971. {
  2972. dst->cpu = src->cpu;
  2973. dst->__sector = blk_rq_pos(src);
  2974. dst->__data_len = blk_rq_bytes(src);
  2975. if (src->rq_flags & RQF_SPECIAL_PAYLOAD) {
  2976. dst->rq_flags |= RQF_SPECIAL_PAYLOAD;
  2977. dst->special_vec = src->special_vec;
  2978. }
  2979. dst->nr_phys_segments = src->nr_phys_segments;
  2980. dst->ioprio = src->ioprio;
  2981. dst->extra_len = src->extra_len;
  2982. }
  2983. /**
  2984. * blk_rq_prep_clone - Helper function to setup clone request
  2985. * @rq: the request to be setup
  2986. * @rq_src: original request to be cloned
  2987. * @bs: bio_set that bios for clone are allocated from
  2988. * @gfp_mask: memory allocation mask for bio
  2989. * @bio_ctr: setup function to be called for each clone bio.
  2990. * Returns %0 for success, non %0 for failure.
  2991. * @data: private data to be passed to @bio_ctr
  2992. *
  2993. * Description:
  2994. * Clones bios in @rq_src to @rq, and copies attributes of @rq_src to @rq.
  2995. * The actual data parts of @rq_src (e.g. ->cmd, ->sense)
  2996. * are not copied, and copying such parts is the caller's responsibility.
  2997. * Also, pages which the original bios are pointing to are not copied
  2998. * and the cloned bios just point same pages.
  2999. * So cloned bios must be completed before original bios, which means
  3000. * the caller must complete @rq before @rq_src.
  3001. */
  3002. int blk_rq_prep_clone(struct request *rq, struct request *rq_src,
  3003. struct bio_set *bs, gfp_t gfp_mask,
  3004. int (*bio_ctr)(struct bio *, struct bio *, void *),
  3005. void *data)
  3006. {
  3007. struct bio *bio, *bio_src;
  3008. if (!bs)
  3009. bs = &fs_bio_set;
  3010. __rq_for_each_bio(bio_src, rq_src) {
  3011. bio = bio_clone_fast(bio_src, gfp_mask, bs);
  3012. if (!bio)
  3013. goto free_and_out;
  3014. if (bio_ctr && bio_ctr(bio, bio_src, data))
  3015. goto free_and_out;
  3016. if (rq->bio) {
  3017. rq->biotail->bi_next = bio;
  3018. rq->biotail = bio;
  3019. } else
  3020. rq->bio = rq->biotail = bio;
  3021. }
  3022. __blk_rq_prep_clone(rq, rq_src);
  3023. return 0;
  3024. free_and_out:
  3025. if (bio)
  3026. bio_put(bio);
  3027. blk_rq_unprep_clone(rq);
  3028. return -ENOMEM;
  3029. }
  3030. EXPORT_SYMBOL_GPL(blk_rq_prep_clone);
  3031. int kblockd_schedule_work(struct work_struct *work)
  3032. {
  3033. return queue_work(kblockd_workqueue, work);
  3034. }
  3035. EXPORT_SYMBOL(kblockd_schedule_work);
  3036. int kblockd_schedule_work_on(int cpu, struct work_struct *work)
  3037. {
  3038. return queue_work_on(cpu, kblockd_workqueue, work);
  3039. }
  3040. EXPORT_SYMBOL(kblockd_schedule_work_on);
  3041. int kblockd_mod_delayed_work_on(int cpu, struct delayed_work *dwork,
  3042. unsigned long delay)
  3043. {
  3044. return mod_delayed_work_on(cpu, kblockd_workqueue, dwork, delay);
  3045. }
  3046. EXPORT_SYMBOL(kblockd_mod_delayed_work_on);
  3047. /**
  3048. * blk_start_plug - initialize blk_plug and track it inside the task_struct
  3049. * @plug: The &struct blk_plug that needs to be initialized
  3050. *
  3051. * Description:
  3052. * Tracking blk_plug inside the task_struct will help with auto-flushing the
  3053. * pending I/O should the task end up blocking between blk_start_plug() and
  3054. * blk_finish_plug(). This is important from a performance perspective, but
  3055. * also ensures that we don't deadlock. For instance, if the task is blocking
  3056. * for a memory allocation, memory reclaim could end up wanting to free a
  3057. * page belonging to that request that is currently residing in our private
  3058. * plug. By flushing the pending I/O when the process goes to sleep, we avoid
  3059. * this kind of deadlock.
  3060. */
  3061. void blk_start_plug(struct blk_plug *plug)
  3062. {
  3063. struct task_struct *tsk = current;
  3064. /*
  3065. * If this is a nested plug, don't actually assign it.
  3066. */
  3067. if (tsk->plug)
  3068. return;
  3069. INIT_LIST_HEAD(&plug->list);
  3070. INIT_LIST_HEAD(&plug->mq_list);
  3071. INIT_LIST_HEAD(&plug->cb_list);
  3072. /*
  3073. * Store ordering should not be needed here, since a potential
  3074. * preempt will imply a full memory barrier
  3075. */
  3076. tsk->plug = plug;
  3077. }
  3078. EXPORT_SYMBOL(blk_start_plug);
  3079. static int plug_rq_cmp(void *priv, struct list_head *a, struct list_head *b)
  3080. {
  3081. struct request *rqa = container_of(a, struct request, queuelist);
  3082. struct request *rqb = container_of(b, struct request, queuelist);
  3083. return !(rqa->q < rqb->q ||
  3084. (rqa->q == rqb->q && blk_rq_pos(rqa) < blk_rq_pos(rqb)));
  3085. }
  3086. /*
  3087. * If 'from_schedule' is true, then postpone the dispatch of requests
  3088. * until a safe kblockd context. We due this to avoid accidental big
  3089. * additional stack usage in driver dispatch, in places where the originally
  3090. * plugger did not intend it.
  3091. */
  3092. static void queue_unplugged(struct request_queue *q, unsigned int depth,
  3093. bool from_schedule)
  3094. __releases(q->queue_lock)
  3095. {
  3096. lockdep_assert_held(q->queue_lock);
  3097. trace_block_unplug(q, depth, !from_schedule);
  3098. if (from_schedule)
  3099. blk_run_queue_async(q);
  3100. else
  3101. __blk_run_queue(q);
  3102. spin_unlock_irq(q->queue_lock);
  3103. }
  3104. static void flush_plug_callbacks(struct blk_plug *plug, bool from_schedule)
  3105. {
  3106. LIST_HEAD(callbacks);
  3107. while (!list_empty(&plug->cb_list)) {
  3108. list_splice_init(&plug->cb_list, &callbacks);
  3109. while (!list_empty(&callbacks)) {
  3110. struct blk_plug_cb *cb = list_first_entry(&callbacks,
  3111. struct blk_plug_cb,
  3112. list);
  3113. list_del(&cb->list);
  3114. cb->callback(cb, from_schedule);
  3115. }
  3116. }
  3117. }
  3118. struct blk_plug_cb *blk_check_plugged(blk_plug_cb_fn unplug, void *data,
  3119. int size)
  3120. {
  3121. struct blk_plug *plug = current->plug;
  3122. struct blk_plug_cb *cb;
  3123. if (!plug)
  3124. return NULL;
  3125. list_for_each_entry(cb, &plug->cb_list, list)
  3126. if (cb->callback == unplug && cb->data == data)
  3127. return cb;
  3128. /* Not currently on the callback list */
  3129. BUG_ON(size < sizeof(*cb));
  3130. cb = kzalloc(size, GFP_ATOMIC);
  3131. if (cb) {
  3132. cb->data = data;
  3133. cb->callback = unplug;
  3134. list_add(&cb->list, &plug->cb_list);
  3135. }
  3136. return cb;
  3137. }
  3138. EXPORT_SYMBOL(blk_check_plugged);
  3139. void blk_flush_plug_list(struct blk_plug *plug, bool from_schedule)
  3140. {
  3141. struct request_queue *q;
  3142. struct request *rq;
  3143. LIST_HEAD(list);
  3144. unsigned int depth;
  3145. flush_plug_callbacks(plug, from_schedule);
  3146. if (!list_empty(&plug->mq_list))
  3147. blk_mq_flush_plug_list(plug, from_schedule);
  3148. if (list_empty(&plug->list))
  3149. return;
  3150. list_splice_init(&plug->list, &list);
  3151. list_sort(NULL, &list, plug_rq_cmp);
  3152. q = NULL;
  3153. depth = 0;
  3154. while (!list_empty(&list)) {
  3155. rq = list_entry_rq(list.next);
  3156. list_del_init(&rq->queuelist);
  3157. BUG_ON(!rq->q);
  3158. if (rq->q != q) {
  3159. /*
  3160. * This drops the queue lock
  3161. */
  3162. if (q)
  3163. queue_unplugged(q, depth, from_schedule);
  3164. q = rq->q;
  3165. depth = 0;
  3166. spin_lock_irq(q->queue_lock);
  3167. }
  3168. /*
  3169. * Short-circuit if @q is dead
  3170. */
  3171. if (unlikely(blk_queue_dying(q))) {
  3172. __blk_end_request_all(rq, BLK_STS_IOERR);
  3173. continue;
  3174. }
  3175. /*
  3176. * rq is already accounted, so use raw insert
  3177. */
  3178. if (op_is_flush(rq->cmd_flags))
  3179. __elv_add_request(q, rq, ELEVATOR_INSERT_FLUSH);
  3180. else
  3181. __elv_add_request(q, rq, ELEVATOR_INSERT_SORT_MERGE);
  3182. depth++;
  3183. }
  3184. /*
  3185. * This drops the queue lock
  3186. */
  3187. if (q)
  3188. queue_unplugged(q, depth, from_schedule);
  3189. }
  3190. void blk_finish_plug(struct blk_plug *plug)
  3191. {
  3192. if (plug != current->plug)
  3193. return;
  3194. blk_flush_plug_list(plug, false);
  3195. current->plug = NULL;
  3196. }
  3197. EXPORT_SYMBOL(blk_finish_plug);
  3198. int __init blk_dev_init(void)
  3199. {
  3200. BUILD_BUG_ON(REQ_OP_LAST >= (1 << REQ_OP_BITS));
  3201. BUILD_BUG_ON(REQ_OP_BITS + REQ_FLAG_BITS > 8 *
  3202. FIELD_SIZEOF(struct request, cmd_flags));
  3203. BUILD_BUG_ON(REQ_OP_BITS + REQ_FLAG_BITS > 8 *
  3204. FIELD_SIZEOF(struct bio, bi_opf));
  3205. /* used for unplugging and affects IO latency/throughput - HIGHPRI */
  3206. kblockd_workqueue = alloc_workqueue("kblockd",
  3207. WQ_MEM_RECLAIM | WQ_HIGHPRI, 0);
  3208. if (!kblockd_workqueue)
  3209. panic("Failed to create kblockd\n");
  3210. request_cachep = kmem_cache_create("blkdev_requests",
  3211. sizeof(struct request), 0, SLAB_PANIC, NULL);
  3212. blk_requestq_cachep = kmem_cache_create("request_queue",
  3213. sizeof(struct request_queue), 0, SLAB_PANIC, NULL);
  3214. #ifdef CONFIG_DEBUG_FS
  3215. blk_debugfs_root = debugfs_create_dir("block", NULL);
  3216. #endif
  3217. return 0;
  3218. }