cfq-iosched.c 127 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937
  1. /*
  2. * CFQ, or complete fairness queueing, disk scheduler.
  3. *
  4. * Based on ideas from a previously unfinished io
  5. * scheduler (round robin per-process disk scheduling) and Andrea Arcangeli.
  6. *
  7. * Copyright (C) 2003 Jens Axboe <axboe@kernel.dk>
  8. */
  9. #include <linux/module.h>
  10. #include <linux/slab.h>
  11. #include <linux/sched/clock.h>
  12. #include <linux/blkdev.h>
  13. #include <linux/elevator.h>
  14. #include <linux/ktime.h>
  15. #include <linux/rbtree.h>
  16. #include <linux/ioprio.h>
  17. #include <linux/blktrace_api.h>
  18. #include <linux/blk-cgroup.h>
  19. #include "blk.h"
  20. #include "blk-wbt.h"
  21. /*
  22. * tunables
  23. */
  24. /* max queue in one round of service */
  25. static const int cfq_quantum = 8;
  26. static const u64 cfq_fifo_expire[2] = { NSEC_PER_SEC / 4, NSEC_PER_SEC / 8 };
  27. /* maximum backwards seek, in KiB */
  28. static const int cfq_back_max = 16 * 1024;
  29. /* penalty of a backwards seek */
  30. static const int cfq_back_penalty = 2;
  31. static const u64 cfq_slice_sync = NSEC_PER_SEC / 10;
  32. static u64 cfq_slice_async = NSEC_PER_SEC / 25;
  33. static const int cfq_slice_async_rq = 2;
  34. static u64 cfq_slice_idle = NSEC_PER_SEC / 125;
  35. static u64 cfq_group_idle = NSEC_PER_SEC / 125;
  36. static const u64 cfq_target_latency = (u64)NSEC_PER_SEC * 3/10; /* 300 ms */
  37. static const int cfq_hist_divisor = 4;
  38. /*
  39. * offset from end of queue service tree for idle class
  40. */
  41. #define CFQ_IDLE_DELAY (NSEC_PER_SEC / 5)
  42. /* offset from end of group service tree under time slice mode */
  43. #define CFQ_SLICE_MODE_GROUP_DELAY (NSEC_PER_SEC / 5)
  44. /* offset from end of group service under IOPS mode */
  45. #define CFQ_IOPS_MODE_GROUP_DELAY (HZ / 5)
  46. /*
  47. * below this threshold, we consider thinktime immediate
  48. */
  49. #define CFQ_MIN_TT (2 * NSEC_PER_SEC / HZ)
  50. #define CFQ_SLICE_SCALE (5)
  51. #define CFQ_HW_QUEUE_MIN (5)
  52. #define CFQ_SERVICE_SHIFT 12
  53. #define CFQQ_SEEK_THR (sector_t)(8 * 100)
  54. #define CFQQ_CLOSE_THR (sector_t)(8 * 1024)
  55. #define CFQQ_SECT_THR_NONROT (sector_t)(2 * 32)
  56. #define CFQQ_SEEKY(cfqq) (hweight32(cfqq->seek_history) > 32/8)
  57. #define RQ_CIC(rq) icq_to_cic((rq)->elv.icq)
  58. #define RQ_CFQQ(rq) (struct cfq_queue *) ((rq)->elv.priv[0])
  59. #define RQ_CFQG(rq) (struct cfq_group *) ((rq)->elv.priv[1])
  60. static struct kmem_cache *cfq_pool;
  61. #define CFQ_PRIO_LISTS IOPRIO_BE_NR
  62. #define cfq_class_idle(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_IDLE)
  63. #define cfq_class_rt(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_RT)
  64. #define sample_valid(samples) ((samples) > 80)
  65. #define rb_entry_cfqg(node) rb_entry((node), struct cfq_group, rb_node)
  66. /* blkio-related constants */
  67. #define CFQ_WEIGHT_LEGACY_MIN 10
  68. #define CFQ_WEIGHT_LEGACY_DFL 500
  69. #define CFQ_WEIGHT_LEGACY_MAX 1000
  70. struct cfq_ttime {
  71. u64 last_end_request;
  72. u64 ttime_total;
  73. u64 ttime_mean;
  74. unsigned long ttime_samples;
  75. };
  76. /*
  77. * Most of our rbtree usage is for sorting with min extraction, so
  78. * if we cache the leftmost node we don't have to walk down the tree
  79. * to find it. Idea borrowed from Ingo Molnars CFS scheduler. We should
  80. * move this into the elevator for the rq sorting as well.
  81. */
  82. struct cfq_rb_root {
  83. struct rb_root rb;
  84. struct rb_node *left;
  85. unsigned count;
  86. u64 min_vdisktime;
  87. struct cfq_ttime ttime;
  88. };
  89. #define CFQ_RB_ROOT (struct cfq_rb_root) { .rb = RB_ROOT, \
  90. .ttime = {.last_end_request = ktime_get_ns(),},}
  91. /*
  92. * Per process-grouping structure
  93. */
  94. struct cfq_queue {
  95. /* reference count */
  96. int ref;
  97. /* various state flags, see below */
  98. unsigned int flags;
  99. /* parent cfq_data */
  100. struct cfq_data *cfqd;
  101. /* service_tree member */
  102. struct rb_node rb_node;
  103. /* service_tree key */
  104. u64 rb_key;
  105. /* prio tree member */
  106. struct rb_node p_node;
  107. /* prio tree root we belong to, if any */
  108. struct rb_root *p_root;
  109. /* sorted list of pending requests */
  110. struct rb_root sort_list;
  111. /* if fifo isn't expired, next request to serve */
  112. struct request *next_rq;
  113. /* requests queued in sort_list */
  114. int queued[2];
  115. /* currently allocated requests */
  116. int allocated[2];
  117. /* fifo list of requests in sort_list */
  118. struct list_head fifo;
  119. /* time when queue got scheduled in to dispatch first request. */
  120. u64 dispatch_start;
  121. u64 allocated_slice;
  122. u64 slice_dispatch;
  123. /* time when first request from queue completed and slice started. */
  124. u64 slice_start;
  125. u64 slice_end;
  126. s64 slice_resid;
  127. /* pending priority requests */
  128. int prio_pending;
  129. /* number of requests that are on the dispatch list or inside driver */
  130. int dispatched;
  131. /* io prio of this group */
  132. unsigned short ioprio, org_ioprio;
  133. unsigned short ioprio_class, org_ioprio_class;
  134. pid_t pid;
  135. u32 seek_history;
  136. sector_t last_request_pos;
  137. struct cfq_rb_root *service_tree;
  138. struct cfq_queue *new_cfqq;
  139. struct cfq_group *cfqg;
  140. /* Number of sectors dispatched from queue in single dispatch round */
  141. unsigned long nr_sectors;
  142. };
  143. /*
  144. * First index in the service_trees.
  145. * IDLE is handled separately, so it has negative index
  146. */
  147. enum wl_class_t {
  148. BE_WORKLOAD = 0,
  149. RT_WORKLOAD = 1,
  150. IDLE_WORKLOAD = 2,
  151. CFQ_PRIO_NR,
  152. };
  153. /*
  154. * Second index in the service_trees.
  155. */
  156. enum wl_type_t {
  157. ASYNC_WORKLOAD = 0,
  158. SYNC_NOIDLE_WORKLOAD = 1,
  159. SYNC_WORKLOAD = 2
  160. };
  161. struct cfqg_stats {
  162. #ifdef CONFIG_CFQ_GROUP_IOSCHED
  163. /* number of ios merged */
  164. struct blkg_rwstat merged;
  165. /* total time spent on device in ns, may not be accurate w/ queueing */
  166. struct blkg_rwstat service_time;
  167. /* total time spent waiting in scheduler queue in ns */
  168. struct blkg_rwstat wait_time;
  169. /* number of IOs queued up */
  170. struct blkg_rwstat queued;
  171. /* total disk time and nr sectors dispatched by this group */
  172. struct blkg_stat time;
  173. #ifdef CONFIG_DEBUG_BLK_CGROUP
  174. /* time not charged to this cgroup */
  175. struct blkg_stat unaccounted_time;
  176. /* sum of number of ios queued across all samples */
  177. struct blkg_stat avg_queue_size_sum;
  178. /* count of samples taken for average */
  179. struct blkg_stat avg_queue_size_samples;
  180. /* how many times this group has been removed from service tree */
  181. struct blkg_stat dequeue;
  182. /* total time spent waiting for it to be assigned a timeslice. */
  183. struct blkg_stat group_wait_time;
  184. /* time spent idling for this blkcg_gq */
  185. struct blkg_stat idle_time;
  186. /* total time with empty current active q with other requests queued */
  187. struct blkg_stat empty_time;
  188. /* fields after this shouldn't be cleared on stat reset */
  189. uint64_t start_group_wait_time;
  190. uint64_t start_idle_time;
  191. uint64_t start_empty_time;
  192. uint16_t flags;
  193. #endif /* CONFIG_DEBUG_BLK_CGROUP */
  194. #endif /* CONFIG_CFQ_GROUP_IOSCHED */
  195. };
  196. /* Per-cgroup data */
  197. struct cfq_group_data {
  198. /* must be the first member */
  199. struct blkcg_policy_data cpd;
  200. unsigned int weight;
  201. unsigned int leaf_weight;
  202. };
  203. /* This is per cgroup per device grouping structure */
  204. struct cfq_group {
  205. /* must be the first member */
  206. struct blkg_policy_data pd;
  207. /* group service_tree member */
  208. struct rb_node rb_node;
  209. /* group service_tree key */
  210. u64 vdisktime;
  211. /*
  212. * The number of active cfqgs and sum of their weights under this
  213. * cfqg. This covers this cfqg's leaf_weight and all children's
  214. * weights, but does not cover weights of further descendants.
  215. *
  216. * If a cfqg is on the service tree, it's active. An active cfqg
  217. * also activates its parent and contributes to the children_weight
  218. * of the parent.
  219. */
  220. int nr_active;
  221. unsigned int children_weight;
  222. /*
  223. * vfraction is the fraction of vdisktime that the tasks in this
  224. * cfqg are entitled to. This is determined by compounding the
  225. * ratios walking up from this cfqg to the root.
  226. *
  227. * It is in fixed point w/ CFQ_SERVICE_SHIFT and the sum of all
  228. * vfractions on a service tree is approximately 1. The sum may
  229. * deviate a bit due to rounding errors and fluctuations caused by
  230. * cfqgs entering and leaving the service tree.
  231. */
  232. unsigned int vfraction;
  233. /*
  234. * There are two weights - (internal) weight is the weight of this
  235. * cfqg against the sibling cfqgs. leaf_weight is the wight of
  236. * this cfqg against the child cfqgs. For the root cfqg, both
  237. * weights are kept in sync for backward compatibility.
  238. */
  239. unsigned int weight;
  240. unsigned int new_weight;
  241. unsigned int dev_weight;
  242. unsigned int leaf_weight;
  243. unsigned int new_leaf_weight;
  244. unsigned int dev_leaf_weight;
  245. /* number of cfqq currently on this group */
  246. int nr_cfqq;
  247. /*
  248. * Per group busy queues average. Useful for workload slice calc. We
  249. * create the array for each prio class but at run time it is used
  250. * only for RT and BE class and slot for IDLE class remains unused.
  251. * This is primarily done to avoid confusion and a gcc warning.
  252. */
  253. unsigned int busy_queues_avg[CFQ_PRIO_NR];
  254. /*
  255. * rr lists of queues with requests. We maintain service trees for
  256. * RT and BE classes. These trees are subdivided in subclasses
  257. * of SYNC, SYNC_NOIDLE and ASYNC based on workload type. For IDLE
  258. * class there is no subclassification and all the cfq queues go on
  259. * a single tree service_tree_idle.
  260. * Counts are embedded in the cfq_rb_root
  261. */
  262. struct cfq_rb_root service_trees[2][3];
  263. struct cfq_rb_root service_tree_idle;
  264. u64 saved_wl_slice;
  265. enum wl_type_t saved_wl_type;
  266. enum wl_class_t saved_wl_class;
  267. /* number of requests that are on the dispatch list or inside driver */
  268. int dispatched;
  269. struct cfq_ttime ttime;
  270. struct cfqg_stats stats; /* stats for this cfqg */
  271. /* async queue for each priority case */
  272. struct cfq_queue *async_cfqq[2][IOPRIO_BE_NR];
  273. struct cfq_queue *async_idle_cfqq;
  274. };
  275. struct cfq_io_cq {
  276. struct io_cq icq; /* must be the first member */
  277. struct cfq_queue *cfqq[2];
  278. struct cfq_ttime ttime;
  279. int ioprio; /* the current ioprio */
  280. #ifdef CONFIG_CFQ_GROUP_IOSCHED
  281. uint64_t blkcg_serial_nr; /* the current blkcg serial */
  282. #endif
  283. };
  284. /*
  285. * Per block device queue structure
  286. */
  287. struct cfq_data {
  288. struct request_queue *queue;
  289. /* Root service tree for cfq_groups */
  290. struct cfq_rb_root grp_service_tree;
  291. struct cfq_group *root_group;
  292. /*
  293. * The priority currently being served
  294. */
  295. enum wl_class_t serving_wl_class;
  296. enum wl_type_t serving_wl_type;
  297. u64 workload_expires;
  298. struct cfq_group *serving_group;
  299. /*
  300. * Each priority tree is sorted by next_request position. These
  301. * trees are used when determining if two or more queues are
  302. * interleaving requests (see cfq_close_cooperator).
  303. */
  304. struct rb_root prio_trees[CFQ_PRIO_LISTS];
  305. unsigned int busy_queues;
  306. unsigned int busy_sync_queues;
  307. int rq_in_driver;
  308. int rq_in_flight[2];
  309. /*
  310. * queue-depth detection
  311. */
  312. int rq_queued;
  313. int hw_tag;
  314. /*
  315. * hw_tag can be
  316. * -1 => indeterminate, (cfq will behave as if NCQ is present, to allow better detection)
  317. * 1 => NCQ is present (hw_tag_est_depth is the estimated max depth)
  318. * 0 => no NCQ
  319. */
  320. int hw_tag_est_depth;
  321. unsigned int hw_tag_samples;
  322. /*
  323. * idle window management
  324. */
  325. struct hrtimer idle_slice_timer;
  326. struct work_struct unplug_work;
  327. struct cfq_queue *active_queue;
  328. struct cfq_io_cq *active_cic;
  329. sector_t last_position;
  330. /*
  331. * tunables, see top of file
  332. */
  333. unsigned int cfq_quantum;
  334. unsigned int cfq_back_penalty;
  335. unsigned int cfq_back_max;
  336. unsigned int cfq_slice_async_rq;
  337. unsigned int cfq_latency;
  338. u64 cfq_fifo_expire[2];
  339. u64 cfq_slice[2];
  340. u64 cfq_slice_idle;
  341. u64 cfq_group_idle;
  342. u64 cfq_target_latency;
  343. /*
  344. * Fallback dummy cfqq for extreme OOM conditions
  345. */
  346. struct cfq_queue oom_cfqq;
  347. u64 last_delayed_sync;
  348. };
  349. static struct cfq_group *cfq_get_next_cfqg(struct cfq_data *cfqd);
  350. static void cfq_put_queue(struct cfq_queue *cfqq);
  351. static struct cfq_rb_root *st_for(struct cfq_group *cfqg,
  352. enum wl_class_t class,
  353. enum wl_type_t type)
  354. {
  355. if (!cfqg)
  356. return NULL;
  357. if (class == IDLE_WORKLOAD)
  358. return &cfqg->service_tree_idle;
  359. return &cfqg->service_trees[class][type];
  360. }
  361. enum cfqq_state_flags {
  362. CFQ_CFQQ_FLAG_on_rr = 0, /* on round-robin busy list */
  363. CFQ_CFQQ_FLAG_wait_request, /* waiting for a request */
  364. CFQ_CFQQ_FLAG_must_dispatch, /* must be allowed a dispatch */
  365. CFQ_CFQQ_FLAG_must_alloc_slice, /* per-slice must_alloc flag */
  366. CFQ_CFQQ_FLAG_fifo_expire, /* FIFO checked in this slice */
  367. CFQ_CFQQ_FLAG_idle_window, /* slice idling enabled */
  368. CFQ_CFQQ_FLAG_prio_changed, /* task priority has changed */
  369. CFQ_CFQQ_FLAG_slice_new, /* no requests dispatched in slice */
  370. CFQ_CFQQ_FLAG_sync, /* synchronous queue */
  371. CFQ_CFQQ_FLAG_coop, /* cfqq is shared */
  372. CFQ_CFQQ_FLAG_split_coop, /* shared cfqq will be splitted */
  373. CFQ_CFQQ_FLAG_deep, /* sync cfqq experienced large depth */
  374. CFQ_CFQQ_FLAG_wait_busy, /* Waiting for next request */
  375. };
  376. #define CFQ_CFQQ_FNS(name) \
  377. static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq) \
  378. { \
  379. (cfqq)->flags |= (1 << CFQ_CFQQ_FLAG_##name); \
  380. } \
  381. static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq) \
  382. { \
  383. (cfqq)->flags &= ~(1 << CFQ_CFQQ_FLAG_##name); \
  384. } \
  385. static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq) \
  386. { \
  387. return ((cfqq)->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0; \
  388. }
  389. CFQ_CFQQ_FNS(on_rr);
  390. CFQ_CFQQ_FNS(wait_request);
  391. CFQ_CFQQ_FNS(must_dispatch);
  392. CFQ_CFQQ_FNS(must_alloc_slice);
  393. CFQ_CFQQ_FNS(fifo_expire);
  394. CFQ_CFQQ_FNS(idle_window);
  395. CFQ_CFQQ_FNS(prio_changed);
  396. CFQ_CFQQ_FNS(slice_new);
  397. CFQ_CFQQ_FNS(sync);
  398. CFQ_CFQQ_FNS(coop);
  399. CFQ_CFQQ_FNS(split_coop);
  400. CFQ_CFQQ_FNS(deep);
  401. CFQ_CFQQ_FNS(wait_busy);
  402. #undef CFQ_CFQQ_FNS
  403. #if defined(CONFIG_CFQ_GROUP_IOSCHED) && defined(CONFIG_DEBUG_BLK_CGROUP)
  404. /* cfqg stats flags */
  405. enum cfqg_stats_flags {
  406. CFQG_stats_waiting = 0,
  407. CFQG_stats_idling,
  408. CFQG_stats_empty,
  409. };
  410. #define CFQG_FLAG_FNS(name) \
  411. static inline void cfqg_stats_mark_##name(struct cfqg_stats *stats) \
  412. { \
  413. stats->flags |= (1 << CFQG_stats_##name); \
  414. } \
  415. static inline void cfqg_stats_clear_##name(struct cfqg_stats *stats) \
  416. { \
  417. stats->flags &= ~(1 << CFQG_stats_##name); \
  418. } \
  419. static inline int cfqg_stats_##name(struct cfqg_stats *stats) \
  420. { \
  421. return (stats->flags & (1 << CFQG_stats_##name)) != 0; \
  422. } \
  423. CFQG_FLAG_FNS(waiting)
  424. CFQG_FLAG_FNS(idling)
  425. CFQG_FLAG_FNS(empty)
  426. #undef CFQG_FLAG_FNS
  427. /* This should be called with the queue_lock held. */
  428. static void cfqg_stats_update_group_wait_time(struct cfqg_stats *stats)
  429. {
  430. unsigned long long now;
  431. if (!cfqg_stats_waiting(stats))
  432. return;
  433. now = sched_clock();
  434. if (time_after64(now, stats->start_group_wait_time))
  435. blkg_stat_add(&stats->group_wait_time,
  436. now - stats->start_group_wait_time);
  437. cfqg_stats_clear_waiting(stats);
  438. }
  439. /* This should be called with the queue_lock held. */
  440. static void cfqg_stats_set_start_group_wait_time(struct cfq_group *cfqg,
  441. struct cfq_group *curr_cfqg)
  442. {
  443. struct cfqg_stats *stats = &cfqg->stats;
  444. if (cfqg_stats_waiting(stats))
  445. return;
  446. if (cfqg == curr_cfqg)
  447. return;
  448. stats->start_group_wait_time = sched_clock();
  449. cfqg_stats_mark_waiting(stats);
  450. }
  451. /* This should be called with the queue_lock held. */
  452. static void cfqg_stats_end_empty_time(struct cfqg_stats *stats)
  453. {
  454. unsigned long long now;
  455. if (!cfqg_stats_empty(stats))
  456. return;
  457. now = sched_clock();
  458. if (time_after64(now, stats->start_empty_time))
  459. blkg_stat_add(&stats->empty_time,
  460. now - stats->start_empty_time);
  461. cfqg_stats_clear_empty(stats);
  462. }
  463. static void cfqg_stats_update_dequeue(struct cfq_group *cfqg)
  464. {
  465. blkg_stat_add(&cfqg->stats.dequeue, 1);
  466. }
  467. static void cfqg_stats_set_start_empty_time(struct cfq_group *cfqg)
  468. {
  469. struct cfqg_stats *stats = &cfqg->stats;
  470. if (blkg_rwstat_total(&stats->queued))
  471. return;
  472. /*
  473. * group is already marked empty. This can happen if cfqq got new
  474. * request in parent group and moved to this group while being added
  475. * to service tree. Just ignore the event and move on.
  476. */
  477. if (cfqg_stats_empty(stats))
  478. return;
  479. stats->start_empty_time = sched_clock();
  480. cfqg_stats_mark_empty(stats);
  481. }
  482. static void cfqg_stats_update_idle_time(struct cfq_group *cfqg)
  483. {
  484. struct cfqg_stats *stats = &cfqg->stats;
  485. if (cfqg_stats_idling(stats)) {
  486. unsigned long long now = sched_clock();
  487. if (time_after64(now, stats->start_idle_time))
  488. blkg_stat_add(&stats->idle_time,
  489. now - stats->start_idle_time);
  490. cfqg_stats_clear_idling(stats);
  491. }
  492. }
  493. static void cfqg_stats_set_start_idle_time(struct cfq_group *cfqg)
  494. {
  495. struct cfqg_stats *stats = &cfqg->stats;
  496. BUG_ON(cfqg_stats_idling(stats));
  497. stats->start_idle_time = sched_clock();
  498. cfqg_stats_mark_idling(stats);
  499. }
  500. static void cfqg_stats_update_avg_queue_size(struct cfq_group *cfqg)
  501. {
  502. struct cfqg_stats *stats = &cfqg->stats;
  503. blkg_stat_add(&stats->avg_queue_size_sum,
  504. blkg_rwstat_total(&stats->queued));
  505. blkg_stat_add(&stats->avg_queue_size_samples, 1);
  506. cfqg_stats_update_group_wait_time(stats);
  507. }
  508. #else /* CONFIG_CFQ_GROUP_IOSCHED && CONFIG_DEBUG_BLK_CGROUP */
  509. static inline void cfqg_stats_set_start_group_wait_time(struct cfq_group *cfqg, struct cfq_group *curr_cfqg) { }
  510. static inline void cfqg_stats_end_empty_time(struct cfqg_stats *stats) { }
  511. static inline void cfqg_stats_update_dequeue(struct cfq_group *cfqg) { }
  512. static inline void cfqg_stats_set_start_empty_time(struct cfq_group *cfqg) { }
  513. static inline void cfqg_stats_update_idle_time(struct cfq_group *cfqg) { }
  514. static inline void cfqg_stats_set_start_idle_time(struct cfq_group *cfqg) { }
  515. static inline void cfqg_stats_update_avg_queue_size(struct cfq_group *cfqg) { }
  516. #endif /* CONFIG_CFQ_GROUP_IOSCHED && CONFIG_DEBUG_BLK_CGROUP */
  517. #ifdef CONFIG_CFQ_GROUP_IOSCHED
  518. static inline struct cfq_group *pd_to_cfqg(struct blkg_policy_data *pd)
  519. {
  520. return pd ? container_of(pd, struct cfq_group, pd) : NULL;
  521. }
  522. static struct cfq_group_data
  523. *cpd_to_cfqgd(struct blkcg_policy_data *cpd)
  524. {
  525. return cpd ? container_of(cpd, struct cfq_group_data, cpd) : NULL;
  526. }
  527. static inline struct blkcg_gq *cfqg_to_blkg(struct cfq_group *cfqg)
  528. {
  529. return pd_to_blkg(&cfqg->pd);
  530. }
  531. static struct blkcg_policy blkcg_policy_cfq;
  532. static inline struct cfq_group *blkg_to_cfqg(struct blkcg_gq *blkg)
  533. {
  534. return pd_to_cfqg(blkg_to_pd(blkg, &blkcg_policy_cfq));
  535. }
  536. static struct cfq_group_data *blkcg_to_cfqgd(struct blkcg *blkcg)
  537. {
  538. return cpd_to_cfqgd(blkcg_to_cpd(blkcg, &blkcg_policy_cfq));
  539. }
  540. static inline struct cfq_group *cfqg_parent(struct cfq_group *cfqg)
  541. {
  542. struct blkcg_gq *pblkg = cfqg_to_blkg(cfqg)->parent;
  543. return pblkg ? blkg_to_cfqg(pblkg) : NULL;
  544. }
  545. static inline bool cfqg_is_descendant(struct cfq_group *cfqg,
  546. struct cfq_group *ancestor)
  547. {
  548. return cgroup_is_descendant(cfqg_to_blkg(cfqg)->blkcg->css.cgroup,
  549. cfqg_to_blkg(ancestor)->blkcg->css.cgroup);
  550. }
  551. static inline void cfqg_get(struct cfq_group *cfqg)
  552. {
  553. return blkg_get(cfqg_to_blkg(cfqg));
  554. }
  555. static inline void cfqg_put(struct cfq_group *cfqg)
  556. {
  557. return blkg_put(cfqg_to_blkg(cfqg));
  558. }
  559. #define cfq_log_cfqq(cfqd, cfqq, fmt, args...) do { \
  560. char __pbuf[128]; \
  561. \
  562. blkg_path(cfqg_to_blkg((cfqq)->cfqg), __pbuf, sizeof(__pbuf)); \
  563. blk_add_trace_msg((cfqd)->queue, "cfq%d%c%c %s " fmt, (cfqq)->pid, \
  564. cfq_cfqq_sync((cfqq)) ? 'S' : 'A', \
  565. cfqq_type((cfqq)) == SYNC_NOIDLE_WORKLOAD ? 'N' : ' ',\
  566. __pbuf, ##args); \
  567. } while (0)
  568. #define cfq_log_cfqg(cfqd, cfqg, fmt, args...) do { \
  569. char __pbuf[128]; \
  570. \
  571. blkg_path(cfqg_to_blkg(cfqg), __pbuf, sizeof(__pbuf)); \
  572. blk_add_trace_msg((cfqd)->queue, "%s " fmt, __pbuf, ##args); \
  573. } while (0)
  574. static inline void cfqg_stats_update_io_add(struct cfq_group *cfqg,
  575. struct cfq_group *curr_cfqg,
  576. unsigned int op)
  577. {
  578. blkg_rwstat_add(&cfqg->stats.queued, op, 1);
  579. cfqg_stats_end_empty_time(&cfqg->stats);
  580. cfqg_stats_set_start_group_wait_time(cfqg, curr_cfqg);
  581. }
  582. static inline void cfqg_stats_update_timeslice_used(struct cfq_group *cfqg,
  583. uint64_t time, unsigned long unaccounted_time)
  584. {
  585. blkg_stat_add(&cfqg->stats.time, time);
  586. #ifdef CONFIG_DEBUG_BLK_CGROUP
  587. blkg_stat_add(&cfqg->stats.unaccounted_time, unaccounted_time);
  588. #endif
  589. }
  590. static inline void cfqg_stats_update_io_remove(struct cfq_group *cfqg,
  591. unsigned int op)
  592. {
  593. blkg_rwstat_add(&cfqg->stats.queued, op, -1);
  594. }
  595. static inline void cfqg_stats_update_io_merged(struct cfq_group *cfqg,
  596. unsigned int op)
  597. {
  598. blkg_rwstat_add(&cfqg->stats.merged, op, 1);
  599. }
  600. static inline void cfqg_stats_update_completion(struct cfq_group *cfqg,
  601. uint64_t start_time, uint64_t io_start_time,
  602. unsigned int op)
  603. {
  604. struct cfqg_stats *stats = &cfqg->stats;
  605. unsigned long long now = sched_clock();
  606. if (time_after64(now, io_start_time))
  607. blkg_rwstat_add(&stats->service_time, op, now - io_start_time);
  608. if (time_after64(io_start_time, start_time))
  609. blkg_rwstat_add(&stats->wait_time, op,
  610. io_start_time - start_time);
  611. }
  612. /* @stats = 0 */
  613. static void cfqg_stats_reset(struct cfqg_stats *stats)
  614. {
  615. /* queued stats shouldn't be cleared */
  616. blkg_rwstat_reset(&stats->merged);
  617. blkg_rwstat_reset(&stats->service_time);
  618. blkg_rwstat_reset(&stats->wait_time);
  619. blkg_stat_reset(&stats->time);
  620. #ifdef CONFIG_DEBUG_BLK_CGROUP
  621. blkg_stat_reset(&stats->unaccounted_time);
  622. blkg_stat_reset(&stats->avg_queue_size_sum);
  623. blkg_stat_reset(&stats->avg_queue_size_samples);
  624. blkg_stat_reset(&stats->dequeue);
  625. blkg_stat_reset(&stats->group_wait_time);
  626. blkg_stat_reset(&stats->idle_time);
  627. blkg_stat_reset(&stats->empty_time);
  628. #endif
  629. }
  630. /* @to += @from */
  631. static void cfqg_stats_add_aux(struct cfqg_stats *to, struct cfqg_stats *from)
  632. {
  633. /* queued stats shouldn't be cleared */
  634. blkg_rwstat_add_aux(&to->merged, &from->merged);
  635. blkg_rwstat_add_aux(&to->service_time, &from->service_time);
  636. blkg_rwstat_add_aux(&to->wait_time, &from->wait_time);
  637. blkg_stat_add_aux(&from->time, &from->time);
  638. #ifdef CONFIG_DEBUG_BLK_CGROUP
  639. blkg_stat_add_aux(&to->unaccounted_time, &from->unaccounted_time);
  640. blkg_stat_add_aux(&to->avg_queue_size_sum, &from->avg_queue_size_sum);
  641. blkg_stat_add_aux(&to->avg_queue_size_samples, &from->avg_queue_size_samples);
  642. blkg_stat_add_aux(&to->dequeue, &from->dequeue);
  643. blkg_stat_add_aux(&to->group_wait_time, &from->group_wait_time);
  644. blkg_stat_add_aux(&to->idle_time, &from->idle_time);
  645. blkg_stat_add_aux(&to->empty_time, &from->empty_time);
  646. #endif
  647. }
  648. /*
  649. * Transfer @cfqg's stats to its parent's aux counts so that the ancestors'
  650. * recursive stats can still account for the amount used by this cfqg after
  651. * it's gone.
  652. */
  653. static void cfqg_stats_xfer_dead(struct cfq_group *cfqg)
  654. {
  655. struct cfq_group *parent = cfqg_parent(cfqg);
  656. lockdep_assert_held(cfqg_to_blkg(cfqg)->q->queue_lock);
  657. if (unlikely(!parent))
  658. return;
  659. cfqg_stats_add_aux(&parent->stats, &cfqg->stats);
  660. cfqg_stats_reset(&cfqg->stats);
  661. }
  662. #else /* CONFIG_CFQ_GROUP_IOSCHED */
  663. static inline struct cfq_group *cfqg_parent(struct cfq_group *cfqg) { return NULL; }
  664. static inline bool cfqg_is_descendant(struct cfq_group *cfqg,
  665. struct cfq_group *ancestor)
  666. {
  667. return true;
  668. }
  669. static inline void cfqg_get(struct cfq_group *cfqg) { }
  670. static inline void cfqg_put(struct cfq_group *cfqg) { }
  671. #define cfq_log_cfqq(cfqd, cfqq, fmt, args...) \
  672. blk_add_trace_msg((cfqd)->queue, "cfq%d%c%c " fmt, (cfqq)->pid, \
  673. cfq_cfqq_sync((cfqq)) ? 'S' : 'A', \
  674. cfqq_type((cfqq)) == SYNC_NOIDLE_WORKLOAD ? 'N' : ' ',\
  675. ##args)
  676. #define cfq_log_cfqg(cfqd, cfqg, fmt, args...) do {} while (0)
  677. static inline void cfqg_stats_update_io_add(struct cfq_group *cfqg,
  678. struct cfq_group *curr_cfqg, unsigned int op) { }
  679. static inline void cfqg_stats_update_timeslice_used(struct cfq_group *cfqg,
  680. uint64_t time, unsigned long unaccounted_time) { }
  681. static inline void cfqg_stats_update_io_remove(struct cfq_group *cfqg,
  682. unsigned int op) { }
  683. static inline void cfqg_stats_update_io_merged(struct cfq_group *cfqg,
  684. unsigned int op) { }
  685. static inline void cfqg_stats_update_completion(struct cfq_group *cfqg,
  686. uint64_t start_time, uint64_t io_start_time,
  687. unsigned int op) { }
  688. #endif /* CONFIG_CFQ_GROUP_IOSCHED */
  689. #define cfq_log(cfqd, fmt, args...) \
  690. blk_add_trace_msg((cfqd)->queue, "cfq " fmt, ##args)
  691. /* Traverses through cfq group service trees */
  692. #define for_each_cfqg_st(cfqg, i, j, st) \
  693. for (i = 0; i <= IDLE_WORKLOAD; i++) \
  694. for (j = 0, st = i < IDLE_WORKLOAD ? &cfqg->service_trees[i][j]\
  695. : &cfqg->service_tree_idle; \
  696. (i < IDLE_WORKLOAD && j <= SYNC_WORKLOAD) || \
  697. (i == IDLE_WORKLOAD && j == 0); \
  698. j++, st = i < IDLE_WORKLOAD ? \
  699. &cfqg->service_trees[i][j]: NULL) \
  700. static inline bool cfq_io_thinktime_big(struct cfq_data *cfqd,
  701. struct cfq_ttime *ttime, bool group_idle)
  702. {
  703. u64 slice;
  704. if (!sample_valid(ttime->ttime_samples))
  705. return false;
  706. if (group_idle)
  707. slice = cfqd->cfq_group_idle;
  708. else
  709. slice = cfqd->cfq_slice_idle;
  710. return ttime->ttime_mean > slice;
  711. }
  712. static inline bool iops_mode(struct cfq_data *cfqd)
  713. {
  714. /*
  715. * If we are not idling on queues and it is a NCQ drive, parallel
  716. * execution of requests is on and measuring time is not possible
  717. * in most of the cases until and unless we drive shallower queue
  718. * depths and that becomes a performance bottleneck. In such cases
  719. * switch to start providing fairness in terms of number of IOs.
  720. */
  721. if (!cfqd->cfq_slice_idle && cfqd->hw_tag)
  722. return true;
  723. else
  724. return false;
  725. }
  726. static inline enum wl_class_t cfqq_class(struct cfq_queue *cfqq)
  727. {
  728. if (cfq_class_idle(cfqq))
  729. return IDLE_WORKLOAD;
  730. if (cfq_class_rt(cfqq))
  731. return RT_WORKLOAD;
  732. return BE_WORKLOAD;
  733. }
  734. static enum wl_type_t cfqq_type(struct cfq_queue *cfqq)
  735. {
  736. if (!cfq_cfqq_sync(cfqq))
  737. return ASYNC_WORKLOAD;
  738. if (!cfq_cfqq_idle_window(cfqq))
  739. return SYNC_NOIDLE_WORKLOAD;
  740. return SYNC_WORKLOAD;
  741. }
  742. static inline int cfq_group_busy_queues_wl(enum wl_class_t wl_class,
  743. struct cfq_data *cfqd,
  744. struct cfq_group *cfqg)
  745. {
  746. if (wl_class == IDLE_WORKLOAD)
  747. return cfqg->service_tree_idle.count;
  748. return cfqg->service_trees[wl_class][ASYNC_WORKLOAD].count +
  749. cfqg->service_trees[wl_class][SYNC_NOIDLE_WORKLOAD].count +
  750. cfqg->service_trees[wl_class][SYNC_WORKLOAD].count;
  751. }
  752. static inline int cfqg_busy_async_queues(struct cfq_data *cfqd,
  753. struct cfq_group *cfqg)
  754. {
  755. return cfqg->service_trees[RT_WORKLOAD][ASYNC_WORKLOAD].count +
  756. cfqg->service_trees[BE_WORKLOAD][ASYNC_WORKLOAD].count;
  757. }
  758. static void cfq_dispatch_insert(struct request_queue *, struct request *);
  759. static struct cfq_queue *cfq_get_queue(struct cfq_data *cfqd, bool is_sync,
  760. struct cfq_io_cq *cic, struct bio *bio);
  761. static inline struct cfq_io_cq *icq_to_cic(struct io_cq *icq)
  762. {
  763. /* cic->icq is the first member, %NULL will convert to %NULL */
  764. return container_of(icq, struct cfq_io_cq, icq);
  765. }
  766. static inline struct cfq_io_cq *cfq_cic_lookup(struct cfq_data *cfqd,
  767. struct io_context *ioc)
  768. {
  769. if (ioc)
  770. return icq_to_cic(ioc_lookup_icq(ioc, cfqd->queue));
  771. return NULL;
  772. }
  773. static inline struct cfq_queue *cic_to_cfqq(struct cfq_io_cq *cic, bool is_sync)
  774. {
  775. return cic->cfqq[is_sync];
  776. }
  777. static inline void cic_set_cfqq(struct cfq_io_cq *cic, struct cfq_queue *cfqq,
  778. bool is_sync)
  779. {
  780. cic->cfqq[is_sync] = cfqq;
  781. }
  782. static inline struct cfq_data *cic_to_cfqd(struct cfq_io_cq *cic)
  783. {
  784. return cic->icq.q->elevator->elevator_data;
  785. }
  786. /*
  787. * scheduler run of queue, if there are requests pending and no one in the
  788. * driver that will restart queueing
  789. */
  790. static inline void cfq_schedule_dispatch(struct cfq_data *cfqd)
  791. {
  792. if (cfqd->busy_queues) {
  793. cfq_log(cfqd, "schedule dispatch");
  794. kblockd_schedule_work(&cfqd->unplug_work);
  795. }
  796. }
  797. /*
  798. * Scale schedule slice based on io priority. Use the sync time slice only
  799. * if a queue is marked sync and has sync io queued. A sync queue with async
  800. * io only, should not get full sync slice length.
  801. */
  802. static inline u64 cfq_prio_slice(struct cfq_data *cfqd, bool sync,
  803. unsigned short prio)
  804. {
  805. u64 base_slice = cfqd->cfq_slice[sync];
  806. u64 slice = div_u64(base_slice, CFQ_SLICE_SCALE);
  807. WARN_ON(prio >= IOPRIO_BE_NR);
  808. return base_slice + (slice * (4 - prio));
  809. }
  810. static inline u64
  811. cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  812. {
  813. return cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio);
  814. }
  815. /**
  816. * cfqg_scale_charge - scale disk time charge according to cfqg weight
  817. * @charge: disk time being charged
  818. * @vfraction: vfraction of the cfqg, fixed point w/ CFQ_SERVICE_SHIFT
  819. *
  820. * Scale @charge according to @vfraction, which is in range (0, 1]. The
  821. * scaling is inversely proportional.
  822. *
  823. * scaled = charge / vfraction
  824. *
  825. * The result is also in fixed point w/ CFQ_SERVICE_SHIFT.
  826. */
  827. static inline u64 cfqg_scale_charge(u64 charge,
  828. unsigned int vfraction)
  829. {
  830. u64 c = charge << CFQ_SERVICE_SHIFT; /* make it fixed point */
  831. /* charge / vfraction */
  832. c <<= CFQ_SERVICE_SHIFT;
  833. return div_u64(c, vfraction);
  834. }
  835. static inline u64 max_vdisktime(u64 min_vdisktime, u64 vdisktime)
  836. {
  837. s64 delta = (s64)(vdisktime - min_vdisktime);
  838. if (delta > 0)
  839. min_vdisktime = vdisktime;
  840. return min_vdisktime;
  841. }
  842. static void update_min_vdisktime(struct cfq_rb_root *st)
  843. {
  844. struct cfq_group *cfqg;
  845. if (st->left) {
  846. cfqg = rb_entry_cfqg(st->left);
  847. st->min_vdisktime = max_vdisktime(st->min_vdisktime,
  848. cfqg->vdisktime);
  849. }
  850. }
  851. /*
  852. * get averaged number of queues of RT/BE priority.
  853. * average is updated, with a formula that gives more weight to higher numbers,
  854. * to quickly follows sudden increases and decrease slowly
  855. */
  856. static inline unsigned cfq_group_get_avg_queues(struct cfq_data *cfqd,
  857. struct cfq_group *cfqg, bool rt)
  858. {
  859. unsigned min_q, max_q;
  860. unsigned mult = cfq_hist_divisor - 1;
  861. unsigned round = cfq_hist_divisor / 2;
  862. unsigned busy = cfq_group_busy_queues_wl(rt, cfqd, cfqg);
  863. min_q = min(cfqg->busy_queues_avg[rt], busy);
  864. max_q = max(cfqg->busy_queues_avg[rt], busy);
  865. cfqg->busy_queues_avg[rt] = (mult * max_q + min_q + round) /
  866. cfq_hist_divisor;
  867. return cfqg->busy_queues_avg[rt];
  868. }
  869. static inline u64
  870. cfq_group_slice(struct cfq_data *cfqd, struct cfq_group *cfqg)
  871. {
  872. return cfqd->cfq_target_latency * cfqg->vfraction >> CFQ_SERVICE_SHIFT;
  873. }
  874. static inline u64
  875. cfq_scaled_cfqq_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  876. {
  877. u64 slice = cfq_prio_to_slice(cfqd, cfqq);
  878. if (cfqd->cfq_latency) {
  879. /*
  880. * interested queues (we consider only the ones with the same
  881. * priority class in the cfq group)
  882. */
  883. unsigned iq = cfq_group_get_avg_queues(cfqd, cfqq->cfqg,
  884. cfq_class_rt(cfqq));
  885. u64 sync_slice = cfqd->cfq_slice[1];
  886. u64 expect_latency = sync_slice * iq;
  887. u64 group_slice = cfq_group_slice(cfqd, cfqq->cfqg);
  888. if (expect_latency > group_slice) {
  889. u64 base_low_slice = 2 * cfqd->cfq_slice_idle;
  890. u64 low_slice;
  891. /* scale low_slice according to IO priority
  892. * and sync vs async */
  893. low_slice = div64_u64(base_low_slice*slice, sync_slice);
  894. low_slice = min(slice, low_slice);
  895. /* the adapted slice value is scaled to fit all iqs
  896. * into the target latency */
  897. slice = div64_u64(slice*group_slice, expect_latency);
  898. slice = max(slice, low_slice);
  899. }
  900. }
  901. return slice;
  902. }
  903. static inline void
  904. cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  905. {
  906. u64 slice = cfq_scaled_cfqq_slice(cfqd, cfqq);
  907. u64 now = ktime_get_ns();
  908. cfqq->slice_start = now;
  909. cfqq->slice_end = now + slice;
  910. cfqq->allocated_slice = slice;
  911. cfq_log_cfqq(cfqd, cfqq, "set_slice=%llu", cfqq->slice_end - now);
  912. }
  913. /*
  914. * We need to wrap this check in cfq_cfqq_slice_new(), since ->slice_end
  915. * isn't valid until the first request from the dispatch is activated
  916. * and the slice time set.
  917. */
  918. static inline bool cfq_slice_used(struct cfq_queue *cfqq)
  919. {
  920. if (cfq_cfqq_slice_new(cfqq))
  921. return false;
  922. if (ktime_get_ns() < cfqq->slice_end)
  923. return false;
  924. return true;
  925. }
  926. /*
  927. * Lifted from AS - choose which of rq1 and rq2 that is best served now.
  928. * We choose the request that is closest to the head right now. Distance
  929. * behind the head is penalized and only allowed to a certain extent.
  930. */
  931. static struct request *
  932. cfq_choose_req(struct cfq_data *cfqd, struct request *rq1, struct request *rq2, sector_t last)
  933. {
  934. sector_t s1, s2, d1 = 0, d2 = 0;
  935. unsigned long back_max;
  936. #define CFQ_RQ1_WRAP 0x01 /* request 1 wraps */
  937. #define CFQ_RQ2_WRAP 0x02 /* request 2 wraps */
  938. unsigned wrap = 0; /* bit mask: requests behind the disk head? */
  939. if (rq1 == NULL || rq1 == rq2)
  940. return rq2;
  941. if (rq2 == NULL)
  942. return rq1;
  943. if (rq_is_sync(rq1) != rq_is_sync(rq2))
  944. return rq_is_sync(rq1) ? rq1 : rq2;
  945. if ((rq1->cmd_flags ^ rq2->cmd_flags) & REQ_PRIO)
  946. return rq1->cmd_flags & REQ_PRIO ? rq1 : rq2;
  947. s1 = blk_rq_pos(rq1);
  948. s2 = blk_rq_pos(rq2);
  949. /*
  950. * by definition, 1KiB is 2 sectors
  951. */
  952. back_max = cfqd->cfq_back_max * 2;
  953. /*
  954. * Strict one way elevator _except_ in the case where we allow
  955. * short backward seeks which are biased as twice the cost of a
  956. * similar forward seek.
  957. */
  958. if (s1 >= last)
  959. d1 = s1 - last;
  960. else if (s1 + back_max >= last)
  961. d1 = (last - s1) * cfqd->cfq_back_penalty;
  962. else
  963. wrap |= CFQ_RQ1_WRAP;
  964. if (s2 >= last)
  965. d2 = s2 - last;
  966. else if (s2 + back_max >= last)
  967. d2 = (last - s2) * cfqd->cfq_back_penalty;
  968. else
  969. wrap |= CFQ_RQ2_WRAP;
  970. /* Found required data */
  971. /*
  972. * By doing switch() on the bit mask "wrap" we avoid having to
  973. * check two variables for all permutations: --> faster!
  974. */
  975. switch (wrap) {
  976. case 0: /* common case for CFQ: rq1 and rq2 not wrapped */
  977. if (d1 < d2)
  978. return rq1;
  979. else if (d2 < d1)
  980. return rq2;
  981. else {
  982. if (s1 >= s2)
  983. return rq1;
  984. else
  985. return rq2;
  986. }
  987. case CFQ_RQ2_WRAP:
  988. return rq1;
  989. case CFQ_RQ1_WRAP:
  990. return rq2;
  991. case (CFQ_RQ1_WRAP|CFQ_RQ2_WRAP): /* both rqs wrapped */
  992. default:
  993. /*
  994. * Since both rqs are wrapped,
  995. * start with the one that's further behind head
  996. * (--> only *one* back seek required),
  997. * since back seek takes more time than forward.
  998. */
  999. if (s1 <= s2)
  1000. return rq1;
  1001. else
  1002. return rq2;
  1003. }
  1004. }
  1005. /*
  1006. * The below is leftmost cache rbtree addon
  1007. */
  1008. static struct cfq_queue *cfq_rb_first(struct cfq_rb_root *root)
  1009. {
  1010. /* Service tree is empty */
  1011. if (!root->count)
  1012. return NULL;
  1013. if (!root->left)
  1014. root->left = rb_first(&root->rb);
  1015. if (root->left)
  1016. return rb_entry(root->left, struct cfq_queue, rb_node);
  1017. return NULL;
  1018. }
  1019. static struct cfq_group *cfq_rb_first_group(struct cfq_rb_root *root)
  1020. {
  1021. if (!root->left)
  1022. root->left = rb_first(&root->rb);
  1023. if (root->left)
  1024. return rb_entry_cfqg(root->left);
  1025. return NULL;
  1026. }
  1027. static void rb_erase_init(struct rb_node *n, struct rb_root *root)
  1028. {
  1029. rb_erase(n, root);
  1030. RB_CLEAR_NODE(n);
  1031. }
  1032. static void cfq_rb_erase(struct rb_node *n, struct cfq_rb_root *root)
  1033. {
  1034. if (root->left == n)
  1035. root->left = NULL;
  1036. rb_erase_init(n, &root->rb);
  1037. --root->count;
  1038. }
  1039. /*
  1040. * would be nice to take fifo expire time into account as well
  1041. */
  1042. static struct request *
  1043. cfq_find_next_rq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
  1044. struct request *last)
  1045. {
  1046. struct rb_node *rbnext = rb_next(&last->rb_node);
  1047. struct rb_node *rbprev = rb_prev(&last->rb_node);
  1048. struct request *next = NULL, *prev = NULL;
  1049. BUG_ON(RB_EMPTY_NODE(&last->rb_node));
  1050. if (rbprev)
  1051. prev = rb_entry_rq(rbprev);
  1052. if (rbnext)
  1053. next = rb_entry_rq(rbnext);
  1054. else {
  1055. rbnext = rb_first(&cfqq->sort_list);
  1056. if (rbnext && rbnext != &last->rb_node)
  1057. next = rb_entry_rq(rbnext);
  1058. }
  1059. return cfq_choose_req(cfqd, next, prev, blk_rq_pos(last));
  1060. }
  1061. static u64 cfq_slice_offset(struct cfq_data *cfqd,
  1062. struct cfq_queue *cfqq)
  1063. {
  1064. /*
  1065. * just an approximation, should be ok.
  1066. */
  1067. return (cfqq->cfqg->nr_cfqq - 1) * (cfq_prio_slice(cfqd, 1, 0) -
  1068. cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio));
  1069. }
  1070. static inline s64
  1071. cfqg_key(struct cfq_rb_root *st, struct cfq_group *cfqg)
  1072. {
  1073. return cfqg->vdisktime - st->min_vdisktime;
  1074. }
  1075. static void
  1076. __cfq_group_service_tree_add(struct cfq_rb_root *st, struct cfq_group *cfqg)
  1077. {
  1078. struct rb_node **node = &st->rb.rb_node;
  1079. struct rb_node *parent = NULL;
  1080. struct cfq_group *__cfqg;
  1081. s64 key = cfqg_key(st, cfqg);
  1082. int left = 1;
  1083. while (*node != NULL) {
  1084. parent = *node;
  1085. __cfqg = rb_entry_cfqg(parent);
  1086. if (key < cfqg_key(st, __cfqg))
  1087. node = &parent->rb_left;
  1088. else {
  1089. node = &parent->rb_right;
  1090. left = 0;
  1091. }
  1092. }
  1093. if (left)
  1094. st->left = &cfqg->rb_node;
  1095. rb_link_node(&cfqg->rb_node, parent, node);
  1096. rb_insert_color(&cfqg->rb_node, &st->rb);
  1097. }
  1098. /*
  1099. * This has to be called only on activation of cfqg
  1100. */
  1101. static void
  1102. cfq_update_group_weight(struct cfq_group *cfqg)
  1103. {
  1104. if (cfqg->new_weight) {
  1105. cfqg->weight = cfqg->new_weight;
  1106. cfqg->new_weight = 0;
  1107. }
  1108. }
  1109. static void
  1110. cfq_update_group_leaf_weight(struct cfq_group *cfqg)
  1111. {
  1112. BUG_ON(!RB_EMPTY_NODE(&cfqg->rb_node));
  1113. if (cfqg->new_leaf_weight) {
  1114. cfqg->leaf_weight = cfqg->new_leaf_weight;
  1115. cfqg->new_leaf_weight = 0;
  1116. }
  1117. }
  1118. static void
  1119. cfq_group_service_tree_add(struct cfq_rb_root *st, struct cfq_group *cfqg)
  1120. {
  1121. unsigned int vfr = 1 << CFQ_SERVICE_SHIFT; /* start with 1 */
  1122. struct cfq_group *pos = cfqg;
  1123. struct cfq_group *parent;
  1124. bool propagate;
  1125. /* add to the service tree */
  1126. BUG_ON(!RB_EMPTY_NODE(&cfqg->rb_node));
  1127. /*
  1128. * Update leaf_weight. We cannot update weight at this point
  1129. * because cfqg might already have been activated and is
  1130. * contributing its current weight to the parent's child_weight.
  1131. */
  1132. cfq_update_group_leaf_weight(cfqg);
  1133. __cfq_group_service_tree_add(st, cfqg);
  1134. /*
  1135. * Activate @cfqg and calculate the portion of vfraction @cfqg is
  1136. * entitled to. vfraction is calculated by walking the tree
  1137. * towards the root calculating the fraction it has at each level.
  1138. * The compounded ratio is how much vfraction @cfqg owns.
  1139. *
  1140. * Start with the proportion tasks in this cfqg has against active
  1141. * children cfqgs - its leaf_weight against children_weight.
  1142. */
  1143. propagate = !pos->nr_active++;
  1144. pos->children_weight += pos->leaf_weight;
  1145. vfr = vfr * pos->leaf_weight / pos->children_weight;
  1146. /*
  1147. * Compound ->weight walking up the tree. Both activation and
  1148. * vfraction calculation are done in the same loop. Propagation
  1149. * stops once an already activated node is met. vfraction
  1150. * calculation should always continue to the root.
  1151. */
  1152. while ((parent = cfqg_parent(pos))) {
  1153. if (propagate) {
  1154. cfq_update_group_weight(pos);
  1155. propagate = !parent->nr_active++;
  1156. parent->children_weight += pos->weight;
  1157. }
  1158. vfr = vfr * pos->weight / parent->children_weight;
  1159. pos = parent;
  1160. }
  1161. cfqg->vfraction = max_t(unsigned, vfr, 1);
  1162. }
  1163. static inline u64 cfq_get_cfqg_vdisktime_delay(struct cfq_data *cfqd)
  1164. {
  1165. if (!iops_mode(cfqd))
  1166. return CFQ_SLICE_MODE_GROUP_DELAY;
  1167. else
  1168. return CFQ_IOPS_MODE_GROUP_DELAY;
  1169. }
  1170. static void
  1171. cfq_group_notify_queue_add(struct cfq_data *cfqd, struct cfq_group *cfqg)
  1172. {
  1173. struct cfq_rb_root *st = &cfqd->grp_service_tree;
  1174. struct cfq_group *__cfqg;
  1175. struct rb_node *n;
  1176. cfqg->nr_cfqq++;
  1177. if (!RB_EMPTY_NODE(&cfqg->rb_node))
  1178. return;
  1179. /*
  1180. * Currently put the group at the end. Later implement something
  1181. * so that groups get lesser vtime based on their weights, so that
  1182. * if group does not loose all if it was not continuously backlogged.
  1183. */
  1184. n = rb_last(&st->rb);
  1185. if (n) {
  1186. __cfqg = rb_entry_cfqg(n);
  1187. cfqg->vdisktime = __cfqg->vdisktime +
  1188. cfq_get_cfqg_vdisktime_delay(cfqd);
  1189. } else
  1190. cfqg->vdisktime = st->min_vdisktime;
  1191. cfq_group_service_tree_add(st, cfqg);
  1192. }
  1193. static void
  1194. cfq_group_service_tree_del(struct cfq_rb_root *st, struct cfq_group *cfqg)
  1195. {
  1196. struct cfq_group *pos = cfqg;
  1197. bool propagate;
  1198. /*
  1199. * Undo activation from cfq_group_service_tree_add(). Deactivate
  1200. * @cfqg and propagate deactivation upwards.
  1201. */
  1202. propagate = !--pos->nr_active;
  1203. pos->children_weight -= pos->leaf_weight;
  1204. while (propagate) {
  1205. struct cfq_group *parent = cfqg_parent(pos);
  1206. /* @pos has 0 nr_active at this point */
  1207. WARN_ON_ONCE(pos->children_weight);
  1208. pos->vfraction = 0;
  1209. if (!parent)
  1210. break;
  1211. propagate = !--parent->nr_active;
  1212. parent->children_weight -= pos->weight;
  1213. pos = parent;
  1214. }
  1215. /* remove from the service tree */
  1216. if (!RB_EMPTY_NODE(&cfqg->rb_node))
  1217. cfq_rb_erase(&cfqg->rb_node, st);
  1218. }
  1219. static void
  1220. cfq_group_notify_queue_del(struct cfq_data *cfqd, struct cfq_group *cfqg)
  1221. {
  1222. struct cfq_rb_root *st = &cfqd->grp_service_tree;
  1223. BUG_ON(cfqg->nr_cfqq < 1);
  1224. cfqg->nr_cfqq--;
  1225. /* If there are other cfq queues under this group, don't delete it */
  1226. if (cfqg->nr_cfqq)
  1227. return;
  1228. cfq_log_cfqg(cfqd, cfqg, "del_from_rr group");
  1229. cfq_group_service_tree_del(st, cfqg);
  1230. cfqg->saved_wl_slice = 0;
  1231. cfqg_stats_update_dequeue(cfqg);
  1232. }
  1233. static inline u64 cfq_cfqq_slice_usage(struct cfq_queue *cfqq,
  1234. u64 *unaccounted_time)
  1235. {
  1236. u64 slice_used;
  1237. u64 now = ktime_get_ns();
  1238. /*
  1239. * Queue got expired before even a single request completed or
  1240. * got expired immediately after first request completion.
  1241. */
  1242. if (!cfqq->slice_start || cfqq->slice_start == now) {
  1243. /*
  1244. * Also charge the seek time incurred to the group, otherwise
  1245. * if there are mutiple queues in the group, each can dispatch
  1246. * a single request on seeky media and cause lots of seek time
  1247. * and group will never know it.
  1248. */
  1249. slice_used = max_t(u64, (now - cfqq->dispatch_start),
  1250. jiffies_to_nsecs(1));
  1251. } else {
  1252. slice_used = now - cfqq->slice_start;
  1253. if (slice_used > cfqq->allocated_slice) {
  1254. *unaccounted_time = slice_used - cfqq->allocated_slice;
  1255. slice_used = cfqq->allocated_slice;
  1256. }
  1257. if (cfqq->slice_start > cfqq->dispatch_start)
  1258. *unaccounted_time += cfqq->slice_start -
  1259. cfqq->dispatch_start;
  1260. }
  1261. return slice_used;
  1262. }
  1263. static void cfq_group_served(struct cfq_data *cfqd, struct cfq_group *cfqg,
  1264. struct cfq_queue *cfqq)
  1265. {
  1266. struct cfq_rb_root *st = &cfqd->grp_service_tree;
  1267. u64 used_sl, charge, unaccounted_sl = 0;
  1268. int nr_sync = cfqg->nr_cfqq - cfqg_busy_async_queues(cfqd, cfqg)
  1269. - cfqg->service_tree_idle.count;
  1270. unsigned int vfr;
  1271. u64 now = ktime_get_ns();
  1272. BUG_ON(nr_sync < 0);
  1273. used_sl = charge = cfq_cfqq_slice_usage(cfqq, &unaccounted_sl);
  1274. if (iops_mode(cfqd))
  1275. charge = cfqq->slice_dispatch;
  1276. else if (!cfq_cfqq_sync(cfqq) && !nr_sync)
  1277. charge = cfqq->allocated_slice;
  1278. /*
  1279. * Can't update vdisktime while on service tree and cfqg->vfraction
  1280. * is valid only while on it. Cache vfr, leave the service tree,
  1281. * update vdisktime and go back on. The re-addition to the tree
  1282. * will also update the weights as necessary.
  1283. */
  1284. vfr = cfqg->vfraction;
  1285. cfq_group_service_tree_del(st, cfqg);
  1286. cfqg->vdisktime += cfqg_scale_charge(charge, vfr);
  1287. cfq_group_service_tree_add(st, cfqg);
  1288. /* This group is being expired. Save the context */
  1289. if (cfqd->workload_expires > now) {
  1290. cfqg->saved_wl_slice = cfqd->workload_expires - now;
  1291. cfqg->saved_wl_type = cfqd->serving_wl_type;
  1292. cfqg->saved_wl_class = cfqd->serving_wl_class;
  1293. } else
  1294. cfqg->saved_wl_slice = 0;
  1295. cfq_log_cfqg(cfqd, cfqg, "served: vt=%llu min_vt=%llu", cfqg->vdisktime,
  1296. st->min_vdisktime);
  1297. cfq_log_cfqq(cfqq->cfqd, cfqq,
  1298. "sl_used=%llu disp=%llu charge=%llu iops=%u sect=%lu",
  1299. used_sl, cfqq->slice_dispatch, charge,
  1300. iops_mode(cfqd), cfqq->nr_sectors);
  1301. cfqg_stats_update_timeslice_used(cfqg, used_sl, unaccounted_sl);
  1302. cfqg_stats_set_start_empty_time(cfqg);
  1303. }
  1304. /**
  1305. * cfq_init_cfqg_base - initialize base part of a cfq_group
  1306. * @cfqg: cfq_group to initialize
  1307. *
  1308. * Initialize the base part which is used whether %CONFIG_CFQ_GROUP_IOSCHED
  1309. * is enabled or not.
  1310. */
  1311. static void cfq_init_cfqg_base(struct cfq_group *cfqg)
  1312. {
  1313. struct cfq_rb_root *st;
  1314. int i, j;
  1315. for_each_cfqg_st(cfqg, i, j, st)
  1316. *st = CFQ_RB_ROOT;
  1317. RB_CLEAR_NODE(&cfqg->rb_node);
  1318. cfqg->ttime.last_end_request = ktime_get_ns();
  1319. }
  1320. #ifdef CONFIG_CFQ_GROUP_IOSCHED
  1321. static int __cfq_set_weight(struct cgroup_subsys_state *css, u64 val,
  1322. bool on_dfl, bool reset_dev, bool is_leaf_weight);
  1323. static void cfqg_stats_exit(struct cfqg_stats *stats)
  1324. {
  1325. blkg_rwstat_exit(&stats->merged);
  1326. blkg_rwstat_exit(&stats->service_time);
  1327. blkg_rwstat_exit(&stats->wait_time);
  1328. blkg_rwstat_exit(&stats->queued);
  1329. blkg_stat_exit(&stats->time);
  1330. #ifdef CONFIG_DEBUG_BLK_CGROUP
  1331. blkg_stat_exit(&stats->unaccounted_time);
  1332. blkg_stat_exit(&stats->avg_queue_size_sum);
  1333. blkg_stat_exit(&stats->avg_queue_size_samples);
  1334. blkg_stat_exit(&stats->dequeue);
  1335. blkg_stat_exit(&stats->group_wait_time);
  1336. blkg_stat_exit(&stats->idle_time);
  1337. blkg_stat_exit(&stats->empty_time);
  1338. #endif
  1339. }
  1340. static int cfqg_stats_init(struct cfqg_stats *stats, gfp_t gfp)
  1341. {
  1342. if (blkg_rwstat_init(&stats->merged, gfp) ||
  1343. blkg_rwstat_init(&stats->service_time, gfp) ||
  1344. blkg_rwstat_init(&stats->wait_time, gfp) ||
  1345. blkg_rwstat_init(&stats->queued, gfp) ||
  1346. blkg_stat_init(&stats->time, gfp))
  1347. goto err;
  1348. #ifdef CONFIG_DEBUG_BLK_CGROUP
  1349. if (blkg_stat_init(&stats->unaccounted_time, gfp) ||
  1350. blkg_stat_init(&stats->avg_queue_size_sum, gfp) ||
  1351. blkg_stat_init(&stats->avg_queue_size_samples, gfp) ||
  1352. blkg_stat_init(&stats->dequeue, gfp) ||
  1353. blkg_stat_init(&stats->group_wait_time, gfp) ||
  1354. blkg_stat_init(&stats->idle_time, gfp) ||
  1355. blkg_stat_init(&stats->empty_time, gfp))
  1356. goto err;
  1357. #endif
  1358. return 0;
  1359. err:
  1360. cfqg_stats_exit(stats);
  1361. return -ENOMEM;
  1362. }
  1363. static struct blkcg_policy_data *cfq_cpd_alloc(gfp_t gfp)
  1364. {
  1365. struct cfq_group_data *cgd;
  1366. cgd = kzalloc(sizeof(*cgd), gfp);
  1367. if (!cgd)
  1368. return NULL;
  1369. return &cgd->cpd;
  1370. }
  1371. static void cfq_cpd_init(struct blkcg_policy_data *cpd)
  1372. {
  1373. struct cfq_group_data *cgd = cpd_to_cfqgd(cpd);
  1374. unsigned int weight = cgroup_subsys_on_dfl(io_cgrp_subsys) ?
  1375. CGROUP_WEIGHT_DFL : CFQ_WEIGHT_LEGACY_DFL;
  1376. if (cpd_to_blkcg(cpd) == &blkcg_root)
  1377. weight *= 2;
  1378. cgd->weight = weight;
  1379. cgd->leaf_weight = weight;
  1380. }
  1381. static void cfq_cpd_free(struct blkcg_policy_data *cpd)
  1382. {
  1383. kfree(cpd_to_cfqgd(cpd));
  1384. }
  1385. static void cfq_cpd_bind(struct blkcg_policy_data *cpd)
  1386. {
  1387. struct blkcg *blkcg = cpd_to_blkcg(cpd);
  1388. bool on_dfl = cgroup_subsys_on_dfl(io_cgrp_subsys);
  1389. unsigned int weight = on_dfl ? CGROUP_WEIGHT_DFL : CFQ_WEIGHT_LEGACY_DFL;
  1390. if (blkcg == &blkcg_root)
  1391. weight *= 2;
  1392. WARN_ON_ONCE(__cfq_set_weight(&blkcg->css, weight, on_dfl, true, false));
  1393. WARN_ON_ONCE(__cfq_set_weight(&blkcg->css, weight, on_dfl, true, true));
  1394. }
  1395. static struct blkg_policy_data *cfq_pd_alloc(gfp_t gfp, int node)
  1396. {
  1397. struct cfq_group *cfqg;
  1398. cfqg = kzalloc_node(sizeof(*cfqg), gfp, node);
  1399. if (!cfqg)
  1400. return NULL;
  1401. cfq_init_cfqg_base(cfqg);
  1402. if (cfqg_stats_init(&cfqg->stats, gfp)) {
  1403. kfree(cfqg);
  1404. return NULL;
  1405. }
  1406. return &cfqg->pd;
  1407. }
  1408. static void cfq_pd_init(struct blkg_policy_data *pd)
  1409. {
  1410. struct cfq_group *cfqg = pd_to_cfqg(pd);
  1411. struct cfq_group_data *cgd = blkcg_to_cfqgd(pd->blkg->blkcg);
  1412. cfqg->weight = cgd->weight;
  1413. cfqg->leaf_weight = cgd->leaf_weight;
  1414. }
  1415. static void cfq_pd_offline(struct blkg_policy_data *pd)
  1416. {
  1417. struct cfq_group *cfqg = pd_to_cfqg(pd);
  1418. int i;
  1419. for (i = 0; i < IOPRIO_BE_NR; i++) {
  1420. if (cfqg->async_cfqq[0][i])
  1421. cfq_put_queue(cfqg->async_cfqq[0][i]);
  1422. if (cfqg->async_cfqq[1][i])
  1423. cfq_put_queue(cfqg->async_cfqq[1][i]);
  1424. }
  1425. if (cfqg->async_idle_cfqq)
  1426. cfq_put_queue(cfqg->async_idle_cfqq);
  1427. /*
  1428. * @blkg is going offline and will be ignored by
  1429. * blkg_[rw]stat_recursive_sum(). Transfer stats to the parent so
  1430. * that they don't get lost. If IOs complete after this point, the
  1431. * stats for them will be lost. Oh well...
  1432. */
  1433. cfqg_stats_xfer_dead(cfqg);
  1434. }
  1435. static void cfq_pd_free(struct blkg_policy_data *pd)
  1436. {
  1437. struct cfq_group *cfqg = pd_to_cfqg(pd);
  1438. cfqg_stats_exit(&cfqg->stats);
  1439. return kfree(cfqg);
  1440. }
  1441. static void cfq_pd_reset_stats(struct blkg_policy_data *pd)
  1442. {
  1443. struct cfq_group *cfqg = pd_to_cfqg(pd);
  1444. cfqg_stats_reset(&cfqg->stats);
  1445. }
  1446. static struct cfq_group *cfq_lookup_cfqg(struct cfq_data *cfqd,
  1447. struct blkcg *blkcg)
  1448. {
  1449. struct blkcg_gq *blkg;
  1450. blkg = blkg_lookup(blkcg, cfqd->queue);
  1451. if (likely(blkg))
  1452. return blkg_to_cfqg(blkg);
  1453. return NULL;
  1454. }
  1455. static void cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg)
  1456. {
  1457. cfqq->cfqg = cfqg;
  1458. /* cfqq reference on cfqg */
  1459. cfqg_get(cfqg);
  1460. }
  1461. static u64 cfqg_prfill_weight_device(struct seq_file *sf,
  1462. struct blkg_policy_data *pd, int off)
  1463. {
  1464. struct cfq_group *cfqg = pd_to_cfqg(pd);
  1465. if (!cfqg->dev_weight)
  1466. return 0;
  1467. return __blkg_prfill_u64(sf, pd, cfqg->dev_weight);
  1468. }
  1469. static int cfqg_print_weight_device(struct seq_file *sf, void *v)
  1470. {
  1471. blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
  1472. cfqg_prfill_weight_device, &blkcg_policy_cfq,
  1473. 0, false);
  1474. return 0;
  1475. }
  1476. static u64 cfqg_prfill_leaf_weight_device(struct seq_file *sf,
  1477. struct blkg_policy_data *pd, int off)
  1478. {
  1479. struct cfq_group *cfqg = pd_to_cfqg(pd);
  1480. if (!cfqg->dev_leaf_weight)
  1481. return 0;
  1482. return __blkg_prfill_u64(sf, pd, cfqg->dev_leaf_weight);
  1483. }
  1484. static int cfqg_print_leaf_weight_device(struct seq_file *sf, void *v)
  1485. {
  1486. blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
  1487. cfqg_prfill_leaf_weight_device, &blkcg_policy_cfq,
  1488. 0, false);
  1489. return 0;
  1490. }
  1491. static int cfq_print_weight(struct seq_file *sf, void *v)
  1492. {
  1493. struct blkcg *blkcg = css_to_blkcg(seq_css(sf));
  1494. struct cfq_group_data *cgd = blkcg_to_cfqgd(blkcg);
  1495. unsigned int val = 0;
  1496. if (cgd)
  1497. val = cgd->weight;
  1498. seq_printf(sf, "%u\n", val);
  1499. return 0;
  1500. }
  1501. static int cfq_print_leaf_weight(struct seq_file *sf, void *v)
  1502. {
  1503. struct blkcg *blkcg = css_to_blkcg(seq_css(sf));
  1504. struct cfq_group_data *cgd = blkcg_to_cfqgd(blkcg);
  1505. unsigned int val = 0;
  1506. if (cgd)
  1507. val = cgd->leaf_weight;
  1508. seq_printf(sf, "%u\n", val);
  1509. return 0;
  1510. }
  1511. static ssize_t __cfqg_set_weight_device(struct kernfs_open_file *of,
  1512. char *buf, size_t nbytes, loff_t off,
  1513. bool on_dfl, bool is_leaf_weight)
  1514. {
  1515. unsigned int min = on_dfl ? CGROUP_WEIGHT_MIN : CFQ_WEIGHT_LEGACY_MIN;
  1516. unsigned int max = on_dfl ? CGROUP_WEIGHT_MAX : CFQ_WEIGHT_LEGACY_MAX;
  1517. struct blkcg *blkcg = css_to_blkcg(of_css(of));
  1518. struct blkg_conf_ctx ctx;
  1519. struct cfq_group *cfqg;
  1520. struct cfq_group_data *cfqgd;
  1521. int ret;
  1522. u64 v;
  1523. ret = blkg_conf_prep(blkcg, &blkcg_policy_cfq, buf, &ctx);
  1524. if (ret)
  1525. return ret;
  1526. if (sscanf(ctx.body, "%llu", &v) == 1) {
  1527. /* require "default" on dfl */
  1528. ret = -ERANGE;
  1529. if (!v && on_dfl)
  1530. goto out_finish;
  1531. } else if (!strcmp(strim(ctx.body), "default")) {
  1532. v = 0;
  1533. } else {
  1534. ret = -EINVAL;
  1535. goto out_finish;
  1536. }
  1537. cfqg = blkg_to_cfqg(ctx.blkg);
  1538. cfqgd = blkcg_to_cfqgd(blkcg);
  1539. ret = -ERANGE;
  1540. if (!v || (v >= min && v <= max)) {
  1541. if (!is_leaf_weight) {
  1542. cfqg->dev_weight = v;
  1543. cfqg->new_weight = v ?: cfqgd->weight;
  1544. } else {
  1545. cfqg->dev_leaf_weight = v;
  1546. cfqg->new_leaf_weight = v ?: cfqgd->leaf_weight;
  1547. }
  1548. ret = 0;
  1549. }
  1550. out_finish:
  1551. blkg_conf_finish(&ctx);
  1552. return ret ?: nbytes;
  1553. }
  1554. static ssize_t cfqg_set_weight_device(struct kernfs_open_file *of,
  1555. char *buf, size_t nbytes, loff_t off)
  1556. {
  1557. return __cfqg_set_weight_device(of, buf, nbytes, off, false, false);
  1558. }
  1559. static ssize_t cfqg_set_leaf_weight_device(struct kernfs_open_file *of,
  1560. char *buf, size_t nbytes, loff_t off)
  1561. {
  1562. return __cfqg_set_weight_device(of, buf, nbytes, off, false, true);
  1563. }
  1564. static int __cfq_set_weight(struct cgroup_subsys_state *css, u64 val,
  1565. bool on_dfl, bool reset_dev, bool is_leaf_weight)
  1566. {
  1567. unsigned int min = on_dfl ? CGROUP_WEIGHT_MIN : CFQ_WEIGHT_LEGACY_MIN;
  1568. unsigned int max = on_dfl ? CGROUP_WEIGHT_MAX : CFQ_WEIGHT_LEGACY_MAX;
  1569. struct blkcg *blkcg = css_to_blkcg(css);
  1570. struct blkcg_gq *blkg;
  1571. struct cfq_group_data *cfqgd;
  1572. int ret = 0;
  1573. if (val < min || val > max)
  1574. return -ERANGE;
  1575. spin_lock_irq(&blkcg->lock);
  1576. cfqgd = blkcg_to_cfqgd(blkcg);
  1577. if (!cfqgd) {
  1578. ret = -EINVAL;
  1579. goto out;
  1580. }
  1581. if (!is_leaf_weight)
  1582. cfqgd->weight = val;
  1583. else
  1584. cfqgd->leaf_weight = val;
  1585. hlist_for_each_entry(blkg, &blkcg->blkg_list, blkcg_node) {
  1586. struct cfq_group *cfqg = blkg_to_cfqg(blkg);
  1587. if (!cfqg)
  1588. continue;
  1589. if (!is_leaf_weight) {
  1590. if (reset_dev)
  1591. cfqg->dev_weight = 0;
  1592. if (!cfqg->dev_weight)
  1593. cfqg->new_weight = cfqgd->weight;
  1594. } else {
  1595. if (reset_dev)
  1596. cfqg->dev_leaf_weight = 0;
  1597. if (!cfqg->dev_leaf_weight)
  1598. cfqg->new_leaf_weight = cfqgd->leaf_weight;
  1599. }
  1600. }
  1601. out:
  1602. spin_unlock_irq(&blkcg->lock);
  1603. return ret;
  1604. }
  1605. static int cfq_set_weight(struct cgroup_subsys_state *css, struct cftype *cft,
  1606. u64 val)
  1607. {
  1608. return __cfq_set_weight(css, val, false, false, false);
  1609. }
  1610. static int cfq_set_leaf_weight(struct cgroup_subsys_state *css,
  1611. struct cftype *cft, u64 val)
  1612. {
  1613. return __cfq_set_weight(css, val, false, false, true);
  1614. }
  1615. static int cfqg_print_stat(struct seq_file *sf, void *v)
  1616. {
  1617. blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), blkg_prfill_stat,
  1618. &blkcg_policy_cfq, seq_cft(sf)->private, false);
  1619. return 0;
  1620. }
  1621. static int cfqg_print_rwstat(struct seq_file *sf, void *v)
  1622. {
  1623. blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), blkg_prfill_rwstat,
  1624. &blkcg_policy_cfq, seq_cft(sf)->private, true);
  1625. return 0;
  1626. }
  1627. static u64 cfqg_prfill_stat_recursive(struct seq_file *sf,
  1628. struct blkg_policy_data *pd, int off)
  1629. {
  1630. u64 sum = blkg_stat_recursive_sum(pd_to_blkg(pd),
  1631. &blkcg_policy_cfq, off);
  1632. return __blkg_prfill_u64(sf, pd, sum);
  1633. }
  1634. static u64 cfqg_prfill_rwstat_recursive(struct seq_file *sf,
  1635. struct blkg_policy_data *pd, int off)
  1636. {
  1637. struct blkg_rwstat sum = blkg_rwstat_recursive_sum(pd_to_blkg(pd),
  1638. &blkcg_policy_cfq, off);
  1639. return __blkg_prfill_rwstat(sf, pd, &sum);
  1640. }
  1641. static int cfqg_print_stat_recursive(struct seq_file *sf, void *v)
  1642. {
  1643. blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
  1644. cfqg_prfill_stat_recursive, &blkcg_policy_cfq,
  1645. seq_cft(sf)->private, false);
  1646. return 0;
  1647. }
  1648. static int cfqg_print_rwstat_recursive(struct seq_file *sf, void *v)
  1649. {
  1650. blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
  1651. cfqg_prfill_rwstat_recursive, &blkcg_policy_cfq,
  1652. seq_cft(sf)->private, true);
  1653. return 0;
  1654. }
  1655. static u64 cfqg_prfill_sectors(struct seq_file *sf, struct blkg_policy_data *pd,
  1656. int off)
  1657. {
  1658. u64 sum = blkg_rwstat_total(&pd->blkg->stat_bytes);
  1659. return __blkg_prfill_u64(sf, pd, sum >> 9);
  1660. }
  1661. static int cfqg_print_stat_sectors(struct seq_file *sf, void *v)
  1662. {
  1663. blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
  1664. cfqg_prfill_sectors, &blkcg_policy_cfq, 0, false);
  1665. return 0;
  1666. }
  1667. static u64 cfqg_prfill_sectors_recursive(struct seq_file *sf,
  1668. struct blkg_policy_data *pd, int off)
  1669. {
  1670. struct blkg_rwstat tmp = blkg_rwstat_recursive_sum(pd->blkg, NULL,
  1671. offsetof(struct blkcg_gq, stat_bytes));
  1672. u64 sum = atomic64_read(&tmp.aux_cnt[BLKG_RWSTAT_READ]) +
  1673. atomic64_read(&tmp.aux_cnt[BLKG_RWSTAT_WRITE]);
  1674. return __blkg_prfill_u64(sf, pd, sum >> 9);
  1675. }
  1676. static int cfqg_print_stat_sectors_recursive(struct seq_file *sf, void *v)
  1677. {
  1678. blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
  1679. cfqg_prfill_sectors_recursive, &blkcg_policy_cfq, 0,
  1680. false);
  1681. return 0;
  1682. }
  1683. #ifdef CONFIG_DEBUG_BLK_CGROUP
  1684. static u64 cfqg_prfill_avg_queue_size(struct seq_file *sf,
  1685. struct blkg_policy_data *pd, int off)
  1686. {
  1687. struct cfq_group *cfqg = pd_to_cfqg(pd);
  1688. u64 samples = blkg_stat_read(&cfqg->stats.avg_queue_size_samples);
  1689. u64 v = 0;
  1690. if (samples) {
  1691. v = blkg_stat_read(&cfqg->stats.avg_queue_size_sum);
  1692. v = div64_u64(v, samples);
  1693. }
  1694. __blkg_prfill_u64(sf, pd, v);
  1695. return 0;
  1696. }
  1697. /* print avg_queue_size */
  1698. static int cfqg_print_avg_queue_size(struct seq_file *sf, void *v)
  1699. {
  1700. blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
  1701. cfqg_prfill_avg_queue_size, &blkcg_policy_cfq,
  1702. 0, false);
  1703. return 0;
  1704. }
  1705. #endif /* CONFIG_DEBUG_BLK_CGROUP */
  1706. static struct cftype cfq_blkcg_legacy_files[] = {
  1707. /* on root, weight is mapped to leaf_weight */
  1708. {
  1709. .name = "weight_device",
  1710. .flags = CFTYPE_ONLY_ON_ROOT,
  1711. .seq_show = cfqg_print_leaf_weight_device,
  1712. .write = cfqg_set_leaf_weight_device,
  1713. },
  1714. {
  1715. .name = "weight",
  1716. .flags = CFTYPE_ONLY_ON_ROOT,
  1717. .seq_show = cfq_print_leaf_weight,
  1718. .write_u64 = cfq_set_leaf_weight,
  1719. },
  1720. /* no such mapping necessary for !roots */
  1721. {
  1722. .name = "weight_device",
  1723. .flags = CFTYPE_NOT_ON_ROOT,
  1724. .seq_show = cfqg_print_weight_device,
  1725. .write = cfqg_set_weight_device,
  1726. },
  1727. {
  1728. .name = "weight",
  1729. .flags = CFTYPE_NOT_ON_ROOT,
  1730. .seq_show = cfq_print_weight,
  1731. .write_u64 = cfq_set_weight,
  1732. },
  1733. {
  1734. .name = "leaf_weight_device",
  1735. .seq_show = cfqg_print_leaf_weight_device,
  1736. .write = cfqg_set_leaf_weight_device,
  1737. },
  1738. {
  1739. .name = "leaf_weight",
  1740. .seq_show = cfq_print_leaf_weight,
  1741. .write_u64 = cfq_set_leaf_weight,
  1742. },
  1743. /* statistics, covers only the tasks in the cfqg */
  1744. {
  1745. .name = "time",
  1746. .private = offsetof(struct cfq_group, stats.time),
  1747. .seq_show = cfqg_print_stat,
  1748. },
  1749. {
  1750. .name = "sectors",
  1751. .seq_show = cfqg_print_stat_sectors,
  1752. },
  1753. {
  1754. .name = "io_service_bytes",
  1755. .private = (unsigned long)&blkcg_policy_cfq,
  1756. .seq_show = blkg_print_stat_bytes,
  1757. },
  1758. {
  1759. .name = "io_serviced",
  1760. .private = (unsigned long)&blkcg_policy_cfq,
  1761. .seq_show = blkg_print_stat_ios,
  1762. },
  1763. {
  1764. .name = "io_service_time",
  1765. .private = offsetof(struct cfq_group, stats.service_time),
  1766. .seq_show = cfqg_print_rwstat,
  1767. },
  1768. {
  1769. .name = "io_wait_time",
  1770. .private = offsetof(struct cfq_group, stats.wait_time),
  1771. .seq_show = cfqg_print_rwstat,
  1772. },
  1773. {
  1774. .name = "io_merged",
  1775. .private = offsetof(struct cfq_group, stats.merged),
  1776. .seq_show = cfqg_print_rwstat,
  1777. },
  1778. {
  1779. .name = "io_queued",
  1780. .private = offsetof(struct cfq_group, stats.queued),
  1781. .seq_show = cfqg_print_rwstat,
  1782. },
  1783. /* the same statictics which cover the cfqg and its descendants */
  1784. {
  1785. .name = "time_recursive",
  1786. .private = offsetof(struct cfq_group, stats.time),
  1787. .seq_show = cfqg_print_stat_recursive,
  1788. },
  1789. {
  1790. .name = "sectors_recursive",
  1791. .seq_show = cfqg_print_stat_sectors_recursive,
  1792. },
  1793. {
  1794. .name = "io_service_bytes_recursive",
  1795. .private = (unsigned long)&blkcg_policy_cfq,
  1796. .seq_show = blkg_print_stat_bytes_recursive,
  1797. },
  1798. {
  1799. .name = "io_serviced_recursive",
  1800. .private = (unsigned long)&blkcg_policy_cfq,
  1801. .seq_show = blkg_print_stat_ios_recursive,
  1802. },
  1803. {
  1804. .name = "io_service_time_recursive",
  1805. .private = offsetof(struct cfq_group, stats.service_time),
  1806. .seq_show = cfqg_print_rwstat_recursive,
  1807. },
  1808. {
  1809. .name = "io_wait_time_recursive",
  1810. .private = offsetof(struct cfq_group, stats.wait_time),
  1811. .seq_show = cfqg_print_rwstat_recursive,
  1812. },
  1813. {
  1814. .name = "io_merged_recursive",
  1815. .private = offsetof(struct cfq_group, stats.merged),
  1816. .seq_show = cfqg_print_rwstat_recursive,
  1817. },
  1818. {
  1819. .name = "io_queued_recursive",
  1820. .private = offsetof(struct cfq_group, stats.queued),
  1821. .seq_show = cfqg_print_rwstat_recursive,
  1822. },
  1823. #ifdef CONFIG_DEBUG_BLK_CGROUP
  1824. {
  1825. .name = "avg_queue_size",
  1826. .seq_show = cfqg_print_avg_queue_size,
  1827. },
  1828. {
  1829. .name = "group_wait_time",
  1830. .private = offsetof(struct cfq_group, stats.group_wait_time),
  1831. .seq_show = cfqg_print_stat,
  1832. },
  1833. {
  1834. .name = "idle_time",
  1835. .private = offsetof(struct cfq_group, stats.idle_time),
  1836. .seq_show = cfqg_print_stat,
  1837. },
  1838. {
  1839. .name = "empty_time",
  1840. .private = offsetof(struct cfq_group, stats.empty_time),
  1841. .seq_show = cfqg_print_stat,
  1842. },
  1843. {
  1844. .name = "dequeue",
  1845. .private = offsetof(struct cfq_group, stats.dequeue),
  1846. .seq_show = cfqg_print_stat,
  1847. },
  1848. {
  1849. .name = "unaccounted_time",
  1850. .private = offsetof(struct cfq_group, stats.unaccounted_time),
  1851. .seq_show = cfqg_print_stat,
  1852. },
  1853. #endif /* CONFIG_DEBUG_BLK_CGROUP */
  1854. { } /* terminate */
  1855. };
  1856. static int cfq_print_weight_on_dfl(struct seq_file *sf, void *v)
  1857. {
  1858. struct blkcg *blkcg = css_to_blkcg(seq_css(sf));
  1859. struct cfq_group_data *cgd = blkcg_to_cfqgd(blkcg);
  1860. seq_printf(sf, "default %u\n", cgd->weight);
  1861. blkcg_print_blkgs(sf, blkcg, cfqg_prfill_weight_device,
  1862. &blkcg_policy_cfq, 0, false);
  1863. return 0;
  1864. }
  1865. static ssize_t cfq_set_weight_on_dfl(struct kernfs_open_file *of,
  1866. char *buf, size_t nbytes, loff_t off)
  1867. {
  1868. char *endp;
  1869. int ret;
  1870. u64 v;
  1871. buf = strim(buf);
  1872. /* "WEIGHT" or "default WEIGHT" sets the default weight */
  1873. v = simple_strtoull(buf, &endp, 0);
  1874. if (*endp == '\0' || sscanf(buf, "default %llu", &v) == 1) {
  1875. ret = __cfq_set_weight(of_css(of), v, true, false, false);
  1876. return ret ?: nbytes;
  1877. }
  1878. /* "MAJ:MIN WEIGHT" */
  1879. return __cfqg_set_weight_device(of, buf, nbytes, off, true, false);
  1880. }
  1881. static struct cftype cfq_blkcg_files[] = {
  1882. {
  1883. .name = "weight",
  1884. .flags = CFTYPE_NOT_ON_ROOT,
  1885. .seq_show = cfq_print_weight_on_dfl,
  1886. .write = cfq_set_weight_on_dfl,
  1887. },
  1888. { } /* terminate */
  1889. };
  1890. #else /* GROUP_IOSCHED */
  1891. static struct cfq_group *cfq_lookup_cfqg(struct cfq_data *cfqd,
  1892. struct blkcg *blkcg)
  1893. {
  1894. return cfqd->root_group;
  1895. }
  1896. static inline void
  1897. cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg) {
  1898. cfqq->cfqg = cfqg;
  1899. }
  1900. #endif /* GROUP_IOSCHED */
  1901. /*
  1902. * The cfqd->service_trees holds all pending cfq_queue's that have
  1903. * requests waiting to be processed. It is sorted in the order that
  1904. * we will service the queues.
  1905. */
  1906. static void cfq_service_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq,
  1907. bool add_front)
  1908. {
  1909. struct rb_node **p, *parent;
  1910. struct cfq_queue *__cfqq;
  1911. u64 rb_key;
  1912. struct cfq_rb_root *st;
  1913. int left;
  1914. int new_cfqq = 1;
  1915. u64 now = ktime_get_ns();
  1916. st = st_for(cfqq->cfqg, cfqq_class(cfqq), cfqq_type(cfqq));
  1917. if (cfq_class_idle(cfqq)) {
  1918. rb_key = CFQ_IDLE_DELAY;
  1919. parent = rb_last(&st->rb);
  1920. if (parent && parent != &cfqq->rb_node) {
  1921. __cfqq = rb_entry(parent, struct cfq_queue, rb_node);
  1922. rb_key += __cfqq->rb_key;
  1923. } else
  1924. rb_key += now;
  1925. } else if (!add_front) {
  1926. /*
  1927. * Get our rb key offset. Subtract any residual slice
  1928. * value carried from last service. A negative resid
  1929. * count indicates slice overrun, and this should position
  1930. * the next service time further away in the tree.
  1931. */
  1932. rb_key = cfq_slice_offset(cfqd, cfqq) + now;
  1933. rb_key -= cfqq->slice_resid;
  1934. cfqq->slice_resid = 0;
  1935. } else {
  1936. rb_key = -NSEC_PER_SEC;
  1937. __cfqq = cfq_rb_first(st);
  1938. rb_key += __cfqq ? __cfqq->rb_key : now;
  1939. }
  1940. if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
  1941. new_cfqq = 0;
  1942. /*
  1943. * same position, nothing more to do
  1944. */
  1945. if (rb_key == cfqq->rb_key && cfqq->service_tree == st)
  1946. return;
  1947. cfq_rb_erase(&cfqq->rb_node, cfqq->service_tree);
  1948. cfqq->service_tree = NULL;
  1949. }
  1950. left = 1;
  1951. parent = NULL;
  1952. cfqq->service_tree = st;
  1953. p = &st->rb.rb_node;
  1954. while (*p) {
  1955. parent = *p;
  1956. __cfqq = rb_entry(parent, struct cfq_queue, rb_node);
  1957. /*
  1958. * sort by key, that represents service time.
  1959. */
  1960. if (rb_key < __cfqq->rb_key)
  1961. p = &parent->rb_left;
  1962. else {
  1963. p = &parent->rb_right;
  1964. left = 0;
  1965. }
  1966. }
  1967. if (left)
  1968. st->left = &cfqq->rb_node;
  1969. cfqq->rb_key = rb_key;
  1970. rb_link_node(&cfqq->rb_node, parent, p);
  1971. rb_insert_color(&cfqq->rb_node, &st->rb);
  1972. st->count++;
  1973. if (add_front || !new_cfqq)
  1974. return;
  1975. cfq_group_notify_queue_add(cfqd, cfqq->cfqg);
  1976. }
  1977. static struct cfq_queue *
  1978. cfq_prio_tree_lookup(struct cfq_data *cfqd, struct rb_root *root,
  1979. sector_t sector, struct rb_node **ret_parent,
  1980. struct rb_node ***rb_link)
  1981. {
  1982. struct rb_node **p, *parent;
  1983. struct cfq_queue *cfqq = NULL;
  1984. parent = NULL;
  1985. p = &root->rb_node;
  1986. while (*p) {
  1987. struct rb_node **n;
  1988. parent = *p;
  1989. cfqq = rb_entry(parent, struct cfq_queue, p_node);
  1990. /*
  1991. * Sort strictly based on sector. Smallest to the left,
  1992. * largest to the right.
  1993. */
  1994. if (sector > blk_rq_pos(cfqq->next_rq))
  1995. n = &(*p)->rb_right;
  1996. else if (sector < blk_rq_pos(cfqq->next_rq))
  1997. n = &(*p)->rb_left;
  1998. else
  1999. break;
  2000. p = n;
  2001. cfqq = NULL;
  2002. }
  2003. *ret_parent = parent;
  2004. if (rb_link)
  2005. *rb_link = p;
  2006. return cfqq;
  2007. }
  2008. static void cfq_prio_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  2009. {
  2010. struct rb_node **p, *parent;
  2011. struct cfq_queue *__cfqq;
  2012. if (cfqq->p_root) {
  2013. rb_erase(&cfqq->p_node, cfqq->p_root);
  2014. cfqq->p_root = NULL;
  2015. }
  2016. if (cfq_class_idle(cfqq))
  2017. return;
  2018. if (!cfqq->next_rq)
  2019. return;
  2020. cfqq->p_root = &cfqd->prio_trees[cfqq->org_ioprio];
  2021. __cfqq = cfq_prio_tree_lookup(cfqd, cfqq->p_root,
  2022. blk_rq_pos(cfqq->next_rq), &parent, &p);
  2023. if (!__cfqq) {
  2024. rb_link_node(&cfqq->p_node, parent, p);
  2025. rb_insert_color(&cfqq->p_node, cfqq->p_root);
  2026. } else
  2027. cfqq->p_root = NULL;
  2028. }
  2029. /*
  2030. * Update cfqq's position in the service tree.
  2031. */
  2032. static void cfq_resort_rr_list(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  2033. {
  2034. /*
  2035. * Resorting requires the cfqq to be on the RR list already.
  2036. */
  2037. if (cfq_cfqq_on_rr(cfqq)) {
  2038. cfq_service_tree_add(cfqd, cfqq, 0);
  2039. cfq_prio_tree_add(cfqd, cfqq);
  2040. }
  2041. }
  2042. /*
  2043. * add to busy list of queues for service, trying to be fair in ordering
  2044. * the pending list according to last request service
  2045. */
  2046. static void cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  2047. {
  2048. cfq_log_cfqq(cfqd, cfqq, "add_to_rr");
  2049. BUG_ON(cfq_cfqq_on_rr(cfqq));
  2050. cfq_mark_cfqq_on_rr(cfqq);
  2051. cfqd->busy_queues++;
  2052. if (cfq_cfqq_sync(cfqq))
  2053. cfqd->busy_sync_queues++;
  2054. cfq_resort_rr_list(cfqd, cfqq);
  2055. }
  2056. /*
  2057. * Called when the cfqq no longer has requests pending, remove it from
  2058. * the service tree.
  2059. */
  2060. static void cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  2061. {
  2062. cfq_log_cfqq(cfqd, cfqq, "del_from_rr");
  2063. BUG_ON(!cfq_cfqq_on_rr(cfqq));
  2064. cfq_clear_cfqq_on_rr(cfqq);
  2065. if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
  2066. cfq_rb_erase(&cfqq->rb_node, cfqq->service_tree);
  2067. cfqq->service_tree = NULL;
  2068. }
  2069. if (cfqq->p_root) {
  2070. rb_erase(&cfqq->p_node, cfqq->p_root);
  2071. cfqq->p_root = NULL;
  2072. }
  2073. cfq_group_notify_queue_del(cfqd, cfqq->cfqg);
  2074. BUG_ON(!cfqd->busy_queues);
  2075. cfqd->busy_queues--;
  2076. if (cfq_cfqq_sync(cfqq))
  2077. cfqd->busy_sync_queues--;
  2078. }
  2079. /*
  2080. * rb tree support functions
  2081. */
  2082. static void cfq_del_rq_rb(struct request *rq)
  2083. {
  2084. struct cfq_queue *cfqq = RQ_CFQQ(rq);
  2085. const int sync = rq_is_sync(rq);
  2086. BUG_ON(!cfqq->queued[sync]);
  2087. cfqq->queued[sync]--;
  2088. elv_rb_del(&cfqq->sort_list, rq);
  2089. if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list)) {
  2090. /*
  2091. * Queue will be deleted from service tree when we actually
  2092. * expire it later. Right now just remove it from prio tree
  2093. * as it is empty.
  2094. */
  2095. if (cfqq->p_root) {
  2096. rb_erase(&cfqq->p_node, cfqq->p_root);
  2097. cfqq->p_root = NULL;
  2098. }
  2099. }
  2100. }
  2101. static void cfq_add_rq_rb(struct request *rq)
  2102. {
  2103. struct cfq_queue *cfqq = RQ_CFQQ(rq);
  2104. struct cfq_data *cfqd = cfqq->cfqd;
  2105. struct request *prev;
  2106. cfqq->queued[rq_is_sync(rq)]++;
  2107. elv_rb_add(&cfqq->sort_list, rq);
  2108. if (!cfq_cfqq_on_rr(cfqq))
  2109. cfq_add_cfqq_rr(cfqd, cfqq);
  2110. /*
  2111. * check if this request is a better next-serve candidate
  2112. */
  2113. prev = cfqq->next_rq;
  2114. cfqq->next_rq = cfq_choose_req(cfqd, cfqq->next_rq, rq, cfqd->last_position);
  2115. /*
  2116. * adjust priority tree position, if ->next_rq changes
  2117. */
  2118. if (prev != cfqq->next_rq)
  2119. cfq_prio_tree_add(cfqd, cfqq);
  2120. BUG_ON(!cfqq->next_rq);
  2121. }
  2122. static void cfq_reposition_rq_rb(struct cfq_queue *cfqq, struct request *rq)
  2123. {
  2124. elv_rb_del(&cfqq->sort_list, rq);
  2125. cfqq->queued[rq_is_sync(rq)]--;
  2126. cfqg_stats_update_io_remove(RQ_CFQG(rq), rq->cmd_flags);
  2127. cfq_add_rq_rb(rq);
  2128. cfqg_stats_update_io_add(RQ_CFQG(rq), cfqq->cfqd->serving_group,
  2129. rq->cmd_flags);
  2130. }
  2131. static struct request *
  2132. cfq_find_rq_fmerge(struct cfq_data *cfqd, struct bio *bio)
  2133. {
  2134. struct task_struct *tsk = current;
  2135. struct cfq_io_cq *cic;
  2136. struct cfq_queue *cfqq;
  2137. cic = cfq_cic_lookup(cfqd, tsk->io_context);
  2138. if (!cic)
  2139. return NULL;
  2140. cfqq = cic_to_cfqq(cic, op_is_sync(bio->bi_opf));
  2141. if (cfqq)
  2142. return elv_rb_find(&cfqq->sort_list, bio_end_sector(bio));
  2143. return NULL;
  2144. }
  2145. static void cfq_activate_request(struct request_queue *q, struct request *rq)
  2146. {
  2147. struct cfq_data *cfqd = q->elevator->elevator_data;
  2148. cfqd->rq_in_driver++;
  2149. cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "activate rq, drv=%d",
  2150. cfqd->rq_in_driver);
  2151. cfqd->last_position = blk_rq_pos(rq) + blk_rq_sectors(rq);
  2152. }
  2153. static void cfq_deactivate_request(struct request_queue *q, struct request *rq)
  2154. {
  2155. struct cfq_data *cfqd = q->elevator->elevator_data;
  2156. WARN_ON(!cfqd->rq_in_driver);
  2157. cfqd->rq_in_driver--;
  2158. cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "deactivate rq, drv=%d",
  2159. cfqd->rq_in_driver);
  2160. }
  2161. static void cfq_remove_request(struct request *rq)
  2162. {
  2163. struct cfq_queue *cfqq = RQ_CFQQ(rq);
  2164. if (cfqq->next_rq == rq)
  2165. cfqq->next_rq = cfq_find_next_rq(cfqq->cfqd, cfqq, rq);
  2166. list_del_init(&rq->queuelist);
  2167. cfq_del_rq_rb(rq);
  2168. cfqq->cfqd->rq_queued--;
  2169. cfqg_stats_update_io_remove(RQ_CFQG(rq), rq->cmd_flags);
  2170. if (rq->cmd_flags & REQ_PRIO) {
  2171. WARN_ON(!cfqq->prio_pending);
  2172. cfqq->prio_pending--;
  2173. }
  2174. }
  2175. static enum elv_merge cfq_merge(struct request_queue *q, struct request **req,
  2176. struct bio *bio)
  2177. {
  2178. struct cfq_data *cfqd = q->elevator->elevator_data;
  2179. struct request *__rq;
  2180. __rq = cfq_find_rq_fmerge(cfqd, bio);
  2181. if (__rq && elv_bio_merge_ok(__rq, bio)) {
  2182. *req = __rq;
  2183. return ELEVATOR_FRONT_MERGE;
  2184. }
  2185. return ELEVATOR_NO_MERGE;
  2186. }
  2187. static void cfq_merged_request(struct request_queue *q, struct request *req,
  2188. enum elv_merge type)
  2189. {
  2190. if (type == ELEVATOR_FRONT_MERGE) {
  2191. struct cfq_queue *cfqq = RQ_CFQQ(req);
  2192. cfq_reposition_rq_rb(cfqq, req);
  2193. }
  2194. }
  2195. static void cfq_bio_merged(struct request_queue *q, struct request *req,
  2196. struct bio *bio)
  2197. {
  2198. cfqg_stats_update_io_merged(RQ_CFQG(req), bio->bi_opf);
  2199. }
  2200. static void
  2201. cfq_merged_requests(struct request_queue *q, struct request *rq,
  2202. struct request *next)
  2203. {
  2204. struct cfq_queue *cfqq = RQ_CFQQ(rq);
  2205. struct cfq_data *cfqd = q->elevator->elevator_data;
  2206. /*
  2207. * reposition in fifo if next is older than rq
  2208. */
  2209. if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
  2210. next->fifo_time < rq->fifo_time &&
  2211. cfqq == RQ_CFQQ(next)) {
  2212. list_move(&rq->queuelist, &next->queuelist);
  2213. rq->fifo_time = next->fifo_time;
  2214. }
  2215. if (cfqq->next_rq == next)
  2216. cfqq->next_rq = rq;
  2217. cfq_remove_request(next);
  2218. cfqg_stats_update_io_merged(RQ_CFQG(rq), next->cmd_flags);
  2219. cfqq = RQ_CFQQ(next);
  2220. /*
  2221. * all requests of this queue are merged to other queues, delete it
  2222. * from the service tree. If it's the active_queue,
  2223. * cfq_dispatch_requests() will choose to expire it or do idle
  2224. */
  2225. if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list) &&
  2226. cfqq != cfqd->active_queue)
  2227. cfq_del_cfqq_rr(cfqd, cfqq);
  2228. }
  2229. static int cfq_allow_bio_merge(struct request_queue *q, struct request *rq,
  2230. struct bio *bio)
  2231. {
  2232. struct cfq_data *cfqd = q->elevator->elevator_data;
  2233. bool is_sync = op_is_sync(bio->bi_opf);
  2234. struct cfq_io_cq *cic;
  2235. struct cfq_queue *cfqq;
  2236. /*
  2237. * Disallow merge of a sync bio into an async request.
  2238. */
  2239. if (is_sync && !rq_is_sync(rq))
  2240. return false;
  2241. /*
  2242. * Lookup the cfqq that this bio will be queued with and allow
  2243. * merge only if rq is queued there.
  2244. */
  2245. cic = cfq_cic_lookup(cfqd, current->io_context);
  2246. if (!cic)
  2247. return false;
  2248. cfqq = cic_to_cfqq(cic, is_sync);
  2249. return cfqq == RQ_CFQQ(rq);
  2250. }
  2251. static int cfq_allow_rq_merge(struct request_queue *q, struct request *rq,
  2252. struct request *next)
  2253. {
  2254. return RQ_CFQQ(rq) == RQ_CFQQ(next);
  2255. }
  2256. static inline void cfq_del_timer(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  2257. {
  2258. hrtimer_try_to_cancel(&cfqd->idle_slice_timer);
  2259. cfqg_stats_update_idle_time(cfqq->cfqg);
  2260. }
  2261. static void __cfq_set_active_queue(struct cfq_data *cfqd,
  2262. struct cfq_queue *cfqq)
  2263. {
  2264. if (cfqq) {
  2265. cfq_log_cfqq(cfqd, cfqq, "set_active wl_class:%d wl_type:%d",
  2266. cfqd->serving_wl_class, cfqd->serving_wl_type);
  2267. cfqg_stats_update_avg_queue_size(cfqq->cfqg);
  2268. cfqq->slice_start = 0;
  2269. cfqq->dispatch_start = ktime_get_ns();
  2270. cfqq->allocated_slice = 0;
  2271. cfqq->slice_end = 0;
  2272. cfqq->slice_dispatch = 0;
  2273. cfqq->nr_sectors = 0;
  2274. cfq_clear_cfqq_wait_request(cfqq);
  2275. cfq_clear_cfqq_must_dispatch(cfqq);
  2276. cfq_clear_cfqq_must_alloc_slice(cfqq);
  2277. cfq_clear_cfqq_fifo_expire(cfqq);
  2278. cfq_mark_cfqq_slice_new(cfqq);
  2279. cfq_del_timer(cfqd, cfqq);
  2280. }
  2281. cfqd->active_queue = cfqq;
  2282. }
  2283. /*
  2284. * current cfqq expired its slice (or was too idle), select new one
  2285. */
  2286. static void
  2287. __cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq,
  2288. bool timed_out)
  2289. {
  2290. cfq_log_cfqq(cfqd, cfqq, "slice expired t=%d", timed_out);
  2291. if (cfq_cfqq_wait_request(cfqq))
  2292. cfq_del_timer(cfqd, cfqq);
  2293. cfq_clear_cfqq_wait_request(cfqq);
  2294. cfq_clear_cfqq_wait_busy(cfqq);
  2295. /*
  2296. * If this cfqq is shared between multiple processes, check to
  2297. * make sure that those processes are still issuing I/Os within
  2298. * the mean seek distance. If not, it may be time to break the
  2299. * queues apart again.
  2300. */
  2301. if (cfq_cfqq_coop(cfqq) && CFQQ_SEEKY(cfqq))
  2302. cfq_mark_cfqq_split_coop(cfqq);
  2303. /*
  2304. * store what was left of this slice, if the queue idled/timed out
  2305. */
  2306. if (timed_out) {
  2307. if (cfq_cfqq_slice_new(cfqq))
  2308. cfqq->slice_resid = cfq_scaled_cfqq_slice(cfqd, cfqq);
  2309. else
  2310. cfqq->slice_resid = cfqq->slice_end - ktime_get_ns();
  2311. cfq_log_cfqq(cfqd, cfqq, "resid=%lld", cfqq->slice_resid);
  2312. }
  2313. cfq_group_served(cfqd, cfqq->cfqg, cfqq);
  2314. if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list))
  2315. cfq_del_cfqq_rr(cfqd, cfqq);
  2316. cfq_resort_rr_list(cfqd, cfqq);
  2317. if (cfqq == cfqd->active_queue)
  2318. cfqd->active_queue = NULL;
  2319. if (cfqd->active_cic) {
  2320. put_io_context(cfqd->active_cic->icq.ioc);
  2321. cfqd->active_cic = NULL;
  2322. }
  2323. }
  2324. static inline void cfq_slice_expired(struct cfq_data *cfqd, bool timed_out)
  2325. {
  2326. struct cfq_queue *cfqq = cfqd->active_queue;
  2327. if (cfqq)
  2328. __cfq_slice_expired(cfqd, cfqq, timed_out);
  2329. }
  2330. /*
  2331. * Get next queue for service. Unless we have a queue preemption,
  2332. * we'll simply select the first cfqq in the service tree.
  2333. */
  2334. static struct cfq_queue *cfq_get_next_queue(struct cfq_data *cfqd)
  2335. {
  2336. struct cfq_rb_root *st = st_for(cfqd->serving_group,
  2337. cfqd->serving_wl_class, cfqd->serving_wl_type);
  2338. if (!cfqd->rq_queued)
  2339. return NULL;
  2340. /* There is nothing to dispatch */
  2341. if (!st)
  2342. return NULL;
  2343. if (RB_EMPTY_ROOT(&st->rb))
  2344. return NULL;
  2345. return cfq_rb_first(st);
  2346. }
  2347. static struct cfq_queue *cfq_get_next_queue_forced(struct cfq_data *cfqd)
  2348. {
  2349. struct cfq_group *cfqg;
  2350. struct cfq_queue *cfqq;
  2351. int i, j;
  2352. struct cfq_rb_root *st;
  2353. if (!cfqd->rq_queued)
  2354. return NULL;
  2355. cfqg = cfq_get_next_cfqg(cfqd);
  2356. if (!cfqg)
  2357. return NULL;
  2358. for_each_cfqg_st(cfqg, i, j, st) {
  2359. cfqq = cfq_rb_first(st);
  2360. if (cfqq)
  2361. return cfqq;
  2362. }
  2363. return NULL;
  2364. }
  2365. /*
  2366. * Get and set a new active queue for service.
  2367. */
  2368. static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd,
  2369. struct cfq_queue *cfqq)
  2370. {
  2371. if (!cfqq)
  2372. cfqq = cfq_get_next_queue(cfqd);
  2373. __cfq_set_active_queue(cfqd, cfqq);
  2374. return cfqq;
  2375. }
  2376. static inline sector_t cfq_dist_from_last(struct cfq_data *cfqd,
  2377. struct request *rq)
  2378. {
  2379. if (blk_rq_pos(rq) >= cfqd->last_position)
  2380. return blk_rq_pos(rq) - cfqd->last_position;
  2381. else
  2382. return cfqd->last_position - blk_rq_pos(rq);
  2383. }
  2384. static inline int cfq_rq_close(struct cfq_data *cfqd, struct cfq_queue *cfqq,
  2385. struct request *rq)
  2386. {
  2387. return cfq_dist_from_last(cfqd, rq) <= CFQQ_CLOSE_THR;
  2388. }
  2389. static struct cfq_queue *cfqq_close(struct cfq_data *cfqd,
  2390. struct cfq_queue *cur_cfqq)
  2391. {
  2392. struct rb_root *root = &cfqd->prio_trees[cur_cfqq->org_ioprio];
  2393. struct rb_node *parent, *node;
  2394. struct cfq_queue *__cfqq;
  2395. sector_t sector = cfqd->last_position;
  2396. if (RB_EMPTY_ROOT(root))
  2397. return NULL;
  2398. /*
  2399. * First, if we find a request starting at the end of the last
  2400. * request, choose it.
  2401. */
  2402. __cfqq = cfq_prio_tree_lookup(cfqd, root, sector, &parent, NULL);
  2403. if (__cfqq)
  2404. return __cfqq;
  2405. /*
  2406. * If the exact sector wasn't found, the parent of the NULL leaf
  2407. * will contain the closest sector.
  2408. */
  2409. __cfqq = rb_entry(parent, struct cfq_queue, p_node);
  2410. if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
  2411. return __cfqq;
  2412. if (blk_rq_pos(__cfqq->next_rq) < sector)
  2413. node = rb_next(&__cfqq->p_node);
  2414. else
  2415. node = rb_prev(&__cfqq->p_node);
  2416. if (!node)
  2417. return NULL;
  2418. __cfqq = rb_entry(node, struct cfq_queue, p_node);
  2419. if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
  2420. return __cfqq;
  2421. return NULL;
  2422. }
  2423. /*
  2424. * cfqd - obvious
  2425. * cur_cfqq - passed in so that we don't decide that the current queue is
  2426. * closely cooperating with itself.
  2427. *
  2428. * So, basically we're assuming that that cur_cfqq has dispatched at least
  2429. * one request, and that cfqd->last_position reflects a position on the disk
  2430. * associated with the I/O issued by cur_cfqq. I'm not sure this is a valid
  2431. * assumption.
  2432. */
  2433. static struct cfq_queue *cfq_close_cooperator(struct cfq_data *cfqd,
  2434. struct cfq_queue *cur_cfqq)
  2435. {
  2436. struct cfq_queue *cfqq;
  2437. if (cfq_class_idle(cur_cfqq))
  2438. return NULL;
  2439. if (!cfq_cfqq_sync(cur_cfqq))
  2440. return NULL;
  2441. if (CFQQ_SEEKY(cur_cfqq))
  2442. return NULL;
  2443. /*
  2444. * Don't search priority tree if it's the only queue in the group.
  2445. */
  2446. if (cur_cfqq->cfqg->nr_cfqq == 1)
  2447. return NULL;
  2448. /*
  2449. * We should notice if some of the queues are cooperating, eg
  2450. * working closely on the same area of the disk. In that case,
  2451. * we can group them together and don't waste time idling.
  2452. */
  2453. cfqq = cfqq_close(cfqd, cur_cfqq);
  2454. if (!cfqq)
  2455. return NULL;
  2456. /* If new queue belongs to different cfq_group, don't choose it */
  2457. if (cur_cfqq->cfqg != cfqq->cfqg)
  2458. return NULL;
  2459. /*
  2460. * It only makes sense to merge sync queues.
  2461. */
  2462. if (!cfq_cfqq_sync(cfqq))
  2463. return NULL;
  2464. if (CFQQ_SEEKY(cfqq))
  2465. return NULL;
  2466. /*
  2467. * Do not merge queues of different priority classes
  2468. */
  2469. if (cfq_class_rt(cfqq) != cfq_class_rt(cur_cfqq))
  2470. return NULL;
  2471. return cfqq;
  2472. }
  2473. /*
  2474. * Determine whether we should enforce idle window for this queue.
  2475. */
  2476. static bool cfq_should_idle(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  2477. {
  2478. enum wl_class_t wl_class = cfqq_class(cfqq);
  2479. struct cfq_rb_root *st = cfqq->service_tree;
  2480. BUG_ON(!st);
  2481. BUG_ON(!st->count);
  2482. if (!cfqd->cfq_slice_idle)
  2483. return false;
  2484. /* We never do for idle class queues. */
  2485. if (wl_class == IDLE_WORKLOAD)
  2486. return false;
  2487. /* We do for queues that were marked with idle window flag. */
  2488. if (cfq_cfqq_idle_window(cfqq) &&
  2489. !(blk_queue_nonrot(cfqd->queue) && cfqd->hw_tag))
  2490. return true;
  2491. /*
  2492. * Otherwise, we do only if they are the last ones
  2493. * in their service tree.
  2494. */
  2495. if (st->count == 1 && cfq_cfqq_sync(cfqq) &&
  2496. !cfq_io_thinktime_big(cfqd, &st->ttime, false))
  2497. return true;
  2498. cfq_log_cfqq(cfqd, cfqq, "Not idling. st->count:%d", st->count);
  2499. return false;
  2500. }
  2501. static void cfq_arm_slice_timer(struct cfq_data *cfqd)
  2502. {
  2503. struct cfq_queue *cfqq = cfqd->active_queue;
  2504. struct cfq_rb_root *st = cfqq->service_tree;
  2505. struct cfq_io_cq *cic;
  2506. u64 sl, group_idle = 0;
  2507. u64 now = ktime_get_ns();
  2508. /*
  2509. * SSD device without seek penalty, disable idling. But only do so
  2510. * for devices that support queuing, otherwise we still have a problem
  2511. * with sync vs async workloads.
  2512. */
  2513. if (blk_queue_nonrot(cfqd->queue) && cfqd->hw_tag)
  2514. return;
  2515. WARN_ON(!RB_EMPTY_ROOT(&cfqq->sort_list));
  2516. WARN_ON(cfq_cfqq_slice_new(cfqq));
  2517. /*
  2518. * idle is disabled, either manually or by past process history
  2519. */
  2520. if (!cfq_should_idle(cfqd, cfqq)) {
  2521. /* no queue idling. Check for group idling */
  2522. if (cfqd->cfq_group_idle)
  2523. group_idle = cfqd->cfq_group_idle;
  2524. else
  2525. return;
  2526. }
  2527. /*
  2528. * still active requests from this queue, don't idle
  2529. */
  2530. if (cfqq->dispatched)
  2531. return;
  2532. /*
  2533. * task has exited, don't wait
  2534. */
  2535. cic = cfqd->active_cic;
  2536. if (!cic || !atomic_read(&cic->icq.ioc->active_ref))
  2537. return;
  2538. /*
  2539. * If our average think time is larger than the remaining time
  2540. * slice, then don't idle. This avoids overrunning the allotted
  2541. * time slice.
  2542. */
  2543. if (sample_valid(cic->ttime.ttime_samples) &&
  2544. (cfqq->slice_end - now < cic->ttime.ttime_mean)) {
  2545. cfq_log_cfqq(cfqd, cfqq, "Not idling. think_time:%llu",
  2546. cic->ttime.ttime_mean);
  2547. return;
  2548. }
  2549. /*
  2550. * There are other queues in the group or this is the only group and
  2551. * it has too big thinktime, don't do group idle.
  2552. */
  2553. if (group_idle &&
  2554. (cfqq->cfqg->nr_cfqq > 1 ||
  2555. cfq_io_thinktime_big(cfqd, &st->ttime, true)))
  2556. return;
  2557. cfq_mark_cfqq_wait_request(cfqq);
  2558. if (group_idle)
  2559. sl = cfqd->cfq_group_idle;
  2560. else
  2561. sl = cfqd->cfq_slice_idle;
  2562. hrtimer_start(&cfqd->idle_slice_timer, ns_to_ktime(sl),
  2563. HRTIMER_MODE_REL);
  2564. cfqg_stats_set_start_idle_time(cfqq->cfqg);
  2565. cfq_log_cfqq(cfqd, cfqq, "arm_idle: %llu group_idle: %d", sl,
  2566. group_idle ? 1 : 0);
  2567. }
  2568. /*
  2569. * Move request from internal lists to the request queue dispatch list.
  2570. */
  2571. static void cfq_dispatch_insert(struct request_queue *q, struct request *rq)
  2572. {
  2573. struct cfq_data *cfqd = q->elevator->elevator_data;
  2574. struct cfq_queue *cfqq = RQ_CFQQ(rq);
  2575. cfq_log_cfqq(cfqd, cfqq, "dispatch_insert");
  2576. cfqq->next_rq = cfq_find_next_rq(cfqd, cfqq, rq);
  2577. cfq_remove_request(rq);
  2578. cfqq->dispatched++;
  2579. (RQ_CFQG(rq))->dispatched++;
  2580. elv_dispatch_sort(q, rq);
  2581. cfqd->rq_in_flight[cfq_cfqq_sync(cfqq)]++;
  2582. cfqq->nr_sectors += blk_rq_sectors(rq);
  2583. }
  2584. /*
  2585. * return expired entry, or NULL to just start from scratch in rbtree
  2586. */
  2587. static struct request *cfq_check_fifo(struct cfq_queue *cfqq)
  2588. {
  2589. struct request *rq = NULL;
  2590. if (cfq_cfqq_fifo_expire(cfqq))
  2591. return NULL;
  2592. cfq_mark_cfqq_fifo_expire(cfqq);
  2593. if (list_empty(&cfqq->fifo))
  2594. return NULL;
  2595. rq = rq_entry_fifo(cfqq->fifo.next);
  2596. if (ktime_get_ns() < rq->fifo_time)
  2597. rq = NULL;
  2598. return rq;
  2599. }
  2600. static inline int
  2601. cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  2602. {
  2603. const int base_rq = cfqd->cfq_slice_async_rq;
  2604. WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
  2605. return 2 * base_rq * (IOPRIO_BE_NR - cfqq->ioprio);
  2606. }
  2607. /*
  2608. * Must be called with the queue_lock held.
  2609. */
  2610. static int cfqq_process_refs(struct cfq_queue *cfqq)
  2611. {
  2612. int process_refs, io_refs;
  2613. io_refs = cfqq->allocated[READ] + cfqq->allocated[WRITE];
  2614. process_refs = cfqq->ref - io_refs;
  2615. BUG_ON(process_refs < 0);
  2616. return process_refs;
  2617. }
  2618. static void cfq_setup_merge(struct cfq_queue *cfqq, struct cfq_queue *new_cfqq)
  2619. {
  2620. int process_refs, new_process_refs;
  2621. struct cfq_queue *__cfqq;
  2622. /*
  2623. * If there are no process references on the new_cfqq, then it is
  2624. * unsafe to follow the ->new_cfqq chain as other cfqq's in the
  2625. * chain may have dropped their last reference (not just their
  2626. * last process reference).
  2627. */
  2628. if (!cfqq_process_refs(new_cfqq))
  2629. return;
  2630. /* Avoid a circular list and skip interim queue merges */
  2631. while ((__cfqq = new_cfqq->new_cfqq)) {
  2632. if (__cfqq == cfqq)
  2633. return;
  2634. new_cfqq = __cfqq;
  2635. }
  2636. process_refs = cfqq_process_refs(cfqq);
  2637. new_process_refs = cfqq_process_refs(new_cfqq);
  2638. /*
  2639. * If the process for the cfqq has gone away, there is no
  2640. * sense in merging the queues.
  2641. */
  2642. if (process_refs == 0 || new_process_refs == 0)
  2643. return;
  2644. /*
  2645. * Merge in the direction of the lesser amount of work.
  2646. */
  2647. if (new_process_refs >= process_refs) {
  2648. cfqq->new_cfqq = new_cfqq;
  2649. new_cfqq->ref += process_refs;
  2650. } else {
  2651. new_cfqq->new_cfqq = cfqq;
  2652. cfqq->ref += new_process_refs;
  2653. }
  2654. }
  2655. static enum wl_type_t cfq_choose_wl_type(struct cfq_data *cfqd,
  2656. struct cfq_group *cfqg, enum wl_class_t wl_class)
  2657. {
  2658. struct cfq_queue *queue;
  2659. int i;
  2660. bool key_valid = false;
  2661. u64 lowest_key = 0;
  2662. enum wl_type_t cur_best = SYNC_NOIDLE_WORKLOAD;
  2663. for (i = 0; i <= SYNC_WORKLOAD; ++i) {
  2664. /* select the one with lowest rb_key */
  2665. queue = cfq_rb_first(st_for(cfqg, wl_class, i));
  2666. if (queue &&
  2667. (!key_valid || queue->rb_key < lowest_key)) {
  2668. lowest_key = queue->rb_key;
  2669. cur_best = i;
  2670. key_valid = true;
  2671. }
  2672. }
  2673. return cur_best;
  2674. }
  2675. static void
  2676. choose_wl_class_and_type(struct cfq_data *cfqd, struct cfq_group *cfqg)
  2677. {
  2678. u64 slice;
  2679. unsigned count;
  2680. struct cfq_rb_root *st;
  2681. u64 group_slice;
  2682. enum wl_class_t original_class = cfqd->serving_wl_class;
  2683. u64 now = ktime_get_ns();
  2684. /* Choose next priority. RT > BE > IDLE */
  2685. if (cfq_group_busy_queues_wl(RT_WORKLOAD, cfqd, cfqg))
  2686. cfqd->serving_wl_class = RT_WORKLOAD;
  2687. else if (cfq_group_busy_queues_wl(BE_WORKLOAD, cfqd, cfqg))
  2688. cfqd->serving_wl_class = BE_WORKLOAD;
  2689. else {
  2690. cfqd->serving_wl_class = IDLE_WORKLOAD;
  2691. cfqd->workload_expires = now + jiffies_to_nsecs(1);
  2692. return;
  2693. }
  2694. if (original_class != cfqd->serving_wl_class)
  2695. goto new_workload;
  2696. /*
  2697. * For RT and BE, we have to choose also the type
  2698. * (SYNC, SYNC_NOIDLE, ASYNC), and to compute a workload
  2699. * expiration time
  2700. */
  2701. st = st_for(cfqg, cfqd->serving_wl_class, cfqd->serving_wl_type);
  2702. count = st->count;
  2703. /*
  2704. * check workload expiration, and that we still have other queues ready
  2705. */
  2706. if (count && !(now > cfqd->workload_expires))
  2707. return;
  2708. new_workload:
  2709. /* otherwise select new workload type */
  2710. cfqd->serving_wl_type = cfq_choose_wl_type(cfqd, cfqg,
  2711. cfqd->serving_wl_class);
  2712. st = st_for(cfqg, cfqd->serving_wl_class, cfqd->serving_wl_type);
  2713. count = st->count;
  2714. /*
  2715. * the workload slice is computed as a fraction of target latency
  2716. * proportional to the number of queues in that workload, over
  2717. * all the queues in the same priority class
  2718. */
  2719. group_slice = cfq_group_slice(cfqd, cfqg);
  2720. slice = div_u64(group_slice * count,
  2721. max_t(unsigned, cfqg->busy_queues_avg[cfqd->serving_wl_class],
  2722. cfq_group_busy_queues_wl(cfqd->serving_wl_class, cfqd,
  2723. cfqg)));
  2724. if (cfqd->serving_wl_type == ASYNC_WORKLOAD) {
  2725. u64 tmp;
  2726. /*
  2727. * Async queues are currently system wide. Just taking
  2728. * proportion of queues with-in same group will lead to higher
  2729. * async ratio system wide as generally root group is going
  2730. * to have higher weight. A more accurate thing would be to
  2731. * calculate system wide asnc/sync ratio.
  2732. */
  2733. tmp = cfqd->cfq_target_latency *
  2734. cfqg_busy_async_queues(cfqd, cfqg);
  2735. tmp = div_u64(tmp, cfqd->busy_queues);
  2736. slice = min_t(u64, slice, tmp);
  2737. /* async workload slice is scaled down according to
  2738. * the sync/async slice ratio. */
  2739. slice = div64_u64(slice*cfqd->cfq_slice[0], cfqd->cfq_slice[1]);
  2740. } else
  2741. /* sync workload slice is at least 2 * cfq_slice_idle */
  2742. slice = max(slice, 2 * cfqd->cfq_slice_idle);
  2743. slice = max_t(u64, slice, CFQ_MIN_TT);
  2744. cfq_log(cfqd, "workload slice:%llu", slice);
  2745. cfqd->workload_expires = now + slice;
  2746. }
  2747. static struct cfq_group *cfq_get_next_cfqg(struct cfq_data *cfqd)
  2748. {
  2749. struct cfq_rb_root *st = &cfqd->grp_service_tree;
  2750. struct cfq_group *cfqg;
  2751. if (RB_EMPTY_ROOT(&st->rb))
  2752. return NULL;
  2753. cfqg = cfq_rb_first_group(st);
  2754. update_min_vdisktime(st);
  2755. return cfqg;
  2756. }
  2757. static void cfq_choose_cfqg(struct cfq_data *cfqd)
  2758. {
  2759. struct cfq_group *cfqg = cfq_get_next_cfqg(cfqd);
  2760. u64 now = ktime_get_ns();
  2761. cfqd->serving_group = cfqg;
  2762. /* Restore the workload type data */
  2763. if (cfqg->saved_wl_slice) {
  2764. cfqd->workload_expires = now + cfqg->saved_wl_slice;
  2765. cfqd->serving_wl_type = cfqg->saved_wl_type;
  2766. cfqd->serving_wl_class = cfqg->saved_wl_class;
  2767. } else
  2768. cfqd->workload_expires = now - 1;
  2769. choose_wl_class_and_type(cfqd, cfqg);
  2770. }
  2771. /*
  2772. * Select a queue for service. If we have a current active queue,
  2773. * check whether to continue servicing it, or retrieve and set a new one.
  2774. */
  2775. static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd)
  2776. {
  2777. struct cfq_queue *cfqq, *new_cfqq = NULL;
  2778. u64 now = ktime_get_ns();
  2779. cfqq = cfqd->active_queue;
  2780. if (!cfqq)
  2781. goto new_queue;
  2782. if (!cfqd->rq_queued)
  2783. return NULL;
  2784. /*
  2785. * We were waiting for group to get backlogged. Expire the queue
  2786. */
  2787. if (cfq_cfqq_wait_busy(cfqq) && !RB_EMPTY_ROOT(&cfqq->sort_list))
  2788. goto expire;
  2789. /*
  2790. * The active queue has run out of time, expire it and select new.
  2791. */
  2792. if (cfq_slice_used(cfqq) && !cfq_cfqq_must_dispatch(cfqq)) {
  2793. /*
  2794. * If slice had not expired at the completion of last request
  2795. * we might not have turned on wait_busy flag. Don't expire
  2796. * the queue yet. Allow the group to get backlogged.
  2797. *
  2798. * The very fact that we have used the slice, that means we
  2799. * have been idling all along on this queue and it should be
  2800. * ok to wait for this request to complete.
  2801. */
  2802. if (cfqq->cfqg->nr_cfqq == 1 && RB_EMPTY_ROOT(&cfqq->sort_list)
  2803. && cfqq->dispatched && cfq_should_idle(cfqd, cfqq)) {
  2804. cfqq = NULL;
  2805. goto keep_queue;
  2806. } else
  2807. goto check_group_idle;
  2808. }
  2809. /*
  2810. * The active queue has requests and isn't expired, allow it to
  2811. * dispatch.
  2812. */
  2813. if (!RB_EMPTY_ROOT(&cfqq->sort_list))
  2814. goto keep_queue;
  2815. /*
  2816. * If another queue has a request waiting within our mean seek
  2817. * distance, let it run. The expire code will check for close
  2818. * cooperators and put the close queue at the front of the service
  2819. * tree. If possible, merge the expiring queue with the new cfqq.
  2820. */
  2821. new_cfqq = cfq_close_cooperator(cfqd, cfqq);
  2822. if (new_cfqq) {
  2823. if (!cfqq->new_cfqq)
  2824. cfq_setup_merge(cfqq, new_cfqq);
  2825. goto expire;
  2826. }
  2827. /*
  2828. * No requests pending. If the active queue still has requests in
  2829. * flight or is idling for a new request, allow either of these
  2830. * conditions to happen (or time out) before selecting a new queue.
  2831. */
  2832. if (hrtimer_active(&cfqd->idle_slice_timer)) {
  2833. cfqq = NULL;
  2834. goto keep_queue;
  2835. }
  2836. /*
  2837. * This is a deep seek queue, but the device is much faster than
  2838. * the queue can deliver, don't idle
  2839. **/
  2840. if (CFQQ_SEEKY(cfqq) && cfq_cfqq_idle_window(cfqq) &&
  2841. (cfq_cfqq_slice_new(cfqq) ||
  2842. (cfqq->slice_end - now > now - cfqq->slice_start))) {
  2843. cfq_clear_cfqq_deep(cfqq);
  2844. cfq_clear_cfqq_idle_window(cfqq);
  2845. }
  2846. if (cfqq->dispatched && cfq_should_idle(cfqd, cfqq)) {
  2847. cfqq = NULL;
  2848. goto keep_queue;
  2849. }
  2850. /*
  2851. * If group idle is enabled and there are requests dispatched from
  2852. * this group, wait for requests to complete.
  2853. */
  2854. check_group_idle:
  2855. if (cfqd->cfq_group_idle && cfqq->cfqg->nr_cfqq == 1 &&
  2856. cfqq->cfqg->dispatched &&
  2857. !cfq_io_thinktime_big(cfqd, &cfqq->cfqg->ttime, true)) {
  2858. cfqq = NULL;
  2859. goto keep_queue;
  2860. }
  2861. expire:
  2862. cfq_slice_expired(cfqd, 0);
  2863. new_queue:
  2864. /*
  2865. * Current queue expired. Check if we have to switch to a new
  2866. * service tree
  2867. */
  2868. if (!new_cfqq)
  2869. cfq_choose_cfqg(cfqd);
  2870. cfqq = cfq_set_active_queue(cfqd, new_cfqq);
  2871. keep_queue:
  2872. return cfqq;
  2873. }
  2874. static int __cfq_forced_dispatch_cfqq(struct cfq_queue *cfqq)
  2875. {
  2876. int dispatched = 0;
  2877. while (cfqq->next_rq) {
  2878. cfq_dispatch_insert(cfqq->cfqd->queue, cfqq->next_rq);
  2879. dispatched++;
  2880. }
  2881. BUG_ON(!list_empty(&cfqq->fifo));
  2882. /* By default cfqq is not expired if it is empty. Do it explicitly */
  2883. __cfq_slice_expired(cfqq->cfqd, cfqq, 0);
  2884. return dispatched;
  2885. }
  2886. /*
  2887. * Drain our current requests. Used for barriers and when switching
  2888. * io schedulers on-the-fly.
  2889. */
  2890. static int cfq_forced_dispatch(struct cfq_data *cfqd)
  2891. {
  2892. struct cfq_queue *cfqq;
  2893. int dispatched = 0;
  2894. /* Expire the timeslice of the current active queue first */
  2895. cfq_slice_expired(cfqd, 0);
  2896. while ((cfqq = cfq_get_next_queue_forced(cfqd)) != NULL) {
  2897. __cfq_set_active_queue(cfqd, cfqq);
  2898. dispatched += __cfq_forced_dispatch_cfqq(cfqq);
  2899. }
  2900. BUG_ON(cfqd->busy_queues);
  2901. cfq_log(cfqd, "forced_dispatch=%d", dispatched);
  2902. return dispatched;
  2903. }
  2904. static inline bool cfq_slice_used_soon(struct cfq_data *cfqd,
  2905. struct cfq_queue *cfqq)
  2906. {
  2907. u64 now = ktime_get_ns();
  2908. /* the queue hasn't finished any request, can't estimate */
  2909. if (cfq_cfqq_slice_new(cfqq))
  2910. return true;
  2911. if (now + cfqd->cfq_slice_idle * cfqq->dispatched > cfqq->slice_end)
  2912. return true;
  2913. return false;
  2914. }
  2915. static bool cfq_may_dispatch(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  2916. {
  2917. unsigned int max_dispatch;
  2918. if (cfq_cfqq_must_dispatch(cfqq))
  2919. return true;
  2920. /*
  2921. * Drain async requests before we start sync IO
  2922. */
  2923. if (cfq_should_idle(cfqd, cfqq) && cfqd->rq_in_flight[BLK_RW_ASYNC])
  2924. return false;
  2925. /*
  2926. * If this is an async queue and we have sync IO in flight, let it wait
  2927. */
  2928. if (cfqd->rq_in_flight[BLK_RW_SYNC] && !cfq_cfqq_sync(cfqq))
  2929. return false;
  2930. max_dispatch = max_t(unsigned int, cfqd->cfq_quantum / 2, 1);
  2931. if (cfq_class_idle(cfqq))
  2932. max_dispatch = 1;
  2933. /*
  2934. * Does this cfqq already have too much IO in flight?
  2935. */
  2936. if (cfqq->dispatched >= max_dispatch) {
  2937. bool promote_sync = false;
  2938. /*
  2939. * idle queue must always only have a single IO in flight
  2940. */
  2941. if (cfq_class_idle(cfqq))
  2942. return false;
  2943. /*
  2944. * If there is only one sync queue
  2945. * we can ignore async queue here and give the sync
  2946. * queue no dispatch limit. The reason is a sync queue can
  2947. * preempt async queue, limiting the sync queue doesn't make
  2948. * sense. This is useful for aiostress test.
  2949. */
  2950. if (cfq_cfqq_sync(cfqq) && cfqd->busy_sync_queues == 1)
  2951. promote_sync = true;
  2952. /*
  2953. * We have other queues, don't allow more IO from this one
  2954. */
  2955. if (cfqd->busy_queues > 1 && cfq_slice_used_soon(cfqd, cfqq) &&
  2956. !promote_sync)
  2957. return false;
  2958. /*
  2959. * Sole queue user, no limit
  2960. */
  2961. if (cfqd->busy_queues == 1 || promote_sync)
  2962. max_dispatch = -1;
  2963. else
  2964. /*
  2965. * Normally we start throttling cfqq when cfq_quantum/2
  2966. * requests have been dispatched. But we can drive
  2967. * deeper queue depths at the beginning of slice
  2968. * subjected to upper limit of cfq_quantum.
  2969. * */
  2970. max_dispatch = cfqd->cfq_quantum;
  2971. }
  2972. /*
  2973. * Async queues must wait a bit before being allowed dispatch.
  2974. * We also ramp up the dispatch depth gradually for async IO,
  2975. * based on the last sync IO we serviced
  2976. */
  2977. if (!cfq_cfqq_sync(cfqq) && cfqd->cfq_latency) {
  2978. u64 last_sync = ktime_get_ns() - cfqd->last_delayed_sync;
  2979. unsigned int depth;
  2980. depth = div64_u64(last_sync, cfqd->cfq_slice[1]);
  2981. if (!depth && !cfqq->dispatched)
  2982. depth = 1;
  2983. if (depth < max_dispatch)
  2984. max_dispatch = depth;
  2985. }
  2986. /*
  2987. * If we're below the current max, allow a dispatch
  2988. */
  2989. return cfqq->dispatched < max_dispatch;
  2990. }
  2991. /*
  2992. * Dispatch a request from cfqq, moving them to the request queue
  2993. * dispatch list.
  2994. */
  2995. static bool cfq_dispatch_request(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  2996. {
  2997. struct request *rq;
  2998. BUG_ON(RB_EMPTY_ROOT(&cfqq->sort_list));
  2999. rq = cfq_check_fifo(cfqq);
  3000. if (rq)
  3001. cfq_mark_cfqq_must_dispatch(cfqq);
  3002. if (!cfq_may_dispatch(cfqd, cfqq))
  3003. return false;
  3004. /*
  3005. * follow expired path, else get first next available
  3006. */
  3007. if (!rq)
  3008. rq = cfqq->next_rq;
  3009. else
  3010. cfq_log_cfqq(cfqq->cfqd, cfqq, "fifo=%p", rq);
  3011. /*
  3012. * insert request into driver dispatch list
  3013. */
  3014. cfq_dispatch_insert(cfqd->queue, rq);
  3015. if (!cfqd->active_cic) {
  3016. struct cfq_io_cq *cic = RQ_CIC(rq);
  3017. atomic_long_inc(&cic->icq.ioc->refcount);
  3018. cfqd->active_cic = cic;
  3019. }
  3020. return true;
  3021. }
  3022. /*
  3023. * Find the cfqq that we need to service and move a request from that to the
  3024. * dispatch list
  3025. */
  3026. static int cfq_dispatch_requests(struct request_queue *q, int force)
  3027. {
  3028. struct cfq_data *cfqd = q->elevator->elevator_data;
  3029. struct cfq_queue *cfqq;
  3030. if (!cfqd->busy_queues)
  3031. return 0;
  3032. if (unlikely(force))
  3033. return cfq_forced_dispatch(cfqd);
  3034. cfqq = cfq_select_queue(cfqd);
  3035. if (!cfqq)
  3036. return 0;
  3037. /*
  3038. * Dispatch a request from this cfqq, if it is allowed
  3039. */
  3040. if (!cfq_dispatch_request(cfqd, cfqq))
  3041. return 0;
  3042. cfqq->slice_dispatch++;
  3043. cfq_clear_cfqq_must_dispatch(cfqq);
  3044. /*
  3045. * expire an async queue immediately if it has used up its slice. idle
  3046. * queue always expire after 1 dispatch round.
  3047. */
  3048. if (cfqd->busy_queues > 1 && ((!cfq_cfqq_sync(cfqq) &&
  3049. cfqq->slice_dispatch >= cfq_prio_to_maxrq(cfqd, cfqq)) ||
  3050. cfq_class_idle(cfqq))) {
  3051. cfqq->slice_end = ktime_get_ns() + 1;
  3052. cfq_slice_expired(cfqd, 0);
  3053. }
  3054. cfq_log_cfqq(cfqd, cfqq, "dispatched a request");
  3055. return 1;
  3056. }
  3057. /*
  3058. * task holds one reference to the queue, dropped when task exits. each rq
  3059. * in-flight on this queue also holds a reference, dropped when rq is freed.
  3060. *
  3061. * Each cfq queue took a reference on the parent group. Drop it now.
  3062. * queue lock must be held here.
  3063. */
  3064. static void cfq_put_queue(struct cfq_queue *cfqq)
  3065. {
  3066. struct cfq_data *cfqd = cfqq->cfqd;
  3067. struct cfq_group *cfqg;
  3068. BUG_ON(cfqq->ref <= 0);
  3069. cfqq->ref--;
  3070. if (cfqq->ref)
  3071. return;
  3072. cfq_log_cfqq(cfqd, cfqq, "put_queue");
  3073. BUG_ON(rb_first(&cfqq->sort_list));
  3074. BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
  3075. cfqg = cfqq->cfqg;
  3076. if (unlikely(cfqd->active_queue == cfqq)) {
  3077. __cfq_slice_expired(cfqd, cfqq, 0);
  3078. cfq_schedule_dispatch(cfqd);
  3079. }
  3080. BUG_ON(cfq_cfqq_on_rr(cfqq));
  3081. kmem_cache_free(cfq_pool, cfqq);
  3082. cfqg_put(cfqg);
  3083. }
  3084. static void cfq_put_cooperator(struct cfq_queue *cfqq)
  3085. {
  3086. struct cfq_queue *__cfqq, *next;
  3087. /*
  3088. * If this queue was scheduled to merge with another queue, be
  3089. * sure to drop the reference taken on that queue (and others in
  3090. * the merge chain). See cfq_setup_merge and cfq_merge_cfqqs.
  3091. */
  3092. __cfqq = cfqq->new_cfqq;
  3093. while (__cfqq) {
  3094. if (__cfqq == cfqq) {
  3095. WARN(1, "cfqq->new_cfqq loop detected\n");
  3096. break;
  3097. }
  3098. next = __cfqq->new_cfqq;
  3099. cfq_put_queue(__cfqq);
  3100. __cfqq = next;
  3101. }
  3102. }
  3103. static void cfq_exit_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  3104. {
  3105. if (unlikely(cfqq == cfqd->active_queue)) {
  3106. __cfq_slice_expired(cfqd, cfqq, 0);
  3107. cfq_schedule_dispatch(cfqd);
  3108. }
  3109. cfq_put_cooperator(cfqq);
  3110. cfq_put_queue(cfqq);
  3111. }
  3112. static void cfq_init_icq(struct io_cq *icq)
  3113. {
  3114. struct cfq_io_cq *cic = icq_to_cic(icq);
  3115. cic->ttime.last_end_request = ktime_get_ns();
  3116. }
  3117. static void cfq_exit_icq(struct io_cq *icq)
  3118. {
  3119. struct cfq_io_cq *cic = icq_to_cic(icq);
  3120. struct cfq_data *cfqd = cic_to_cfqd(cic);
  3121. if (cic_to_cfqq(cic, false)) {
  3122. cfq_exit_cfqq(cfqd, cic_to_cfqq(cic, false));
  3123. cic_set_cfqq(cic, NULL, false);
  3124. }
  3125. if (cic_to_cfqq(cic, true)) {
  3126. cfq_exit_cfqq(cfqd, cic_to_cfqq(cic, true));
  3127. cic_set_cfqq(cic, NULL, true);
  3128. }
  3129. }
  3130. static void cfq_init_prio_data(struct cfq_queue *cfqq, struct cfq_io_cq *cic)
  3131. {
  3132. struct task_struct *tsk = current;
  3133. int ioprio_class;
  3134. if (!cfq_cfqq_prio_changed(cfqq))
  3135. return;
  3136. ioprio_class = IOPRIO_PRIO_CLASS(cic->ioprio);
  3137. switch (ioprio_class) {
  3138. default:
  3139. printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class);
  3140. case IOPRIO_CLASS_NONE:
  3141. /*
  3142. * no prio set, inherit CPU scheduling settings
  3143. */
  3144. cfqq->ioprio = task_nice_ioprio(tsk);
  3145. cfqq->ioprio_class = task_nice_ioclass(tsk);
  3146. break;
  3147. case IOPRIO_CLASS_RT:
  3148. cfqq->ioprio = IOPRIO_PRIO_DATA(cic->ioprio);
  3149. cfqq->ioprio_class = IOPRIO_CLASS_RT;
  3150. break;
  3151. case IOPRIO_CLASS_BE:
  3152. cfqq->ioprio = IOPRIO_PRIO_DATA(cic->ioprio);
  3153. cfqq->ioprio_class = IOPRIO_CLASS_BE;
  3154. break;
  3155. case IOPRIO_CLASS_IDLE:
  3156. cfqq->ioprio_class = IOPRIO_CLASS_IDLE;
  3157. cfqq->ioprio = 7;
  3158. cfq_clear_cfqq_idle_window(cfqq);
  3159. break;
  3160. }
  3161. /*
  3162. * keep track of original prio settings in case we have to temporarily
  3163. * elevate the priority of this queue
  3164. */
  3165. cfqq->org_ioprio = cfqq->ioprio;
  3166. cfqq->org_ioprio_class = cfqq->ioprio_class;
  3167. cfq_clear_cfqq_prio_changed(cfqq);
  3168. }
  3169. static void check_ioprio_changed(struct cfq_io_cq *cic, struct bio *bio)
  3170. {
  3171. int ioprio = cic->icq.ioc->ioprio;
  3172. struct cfq_data *cfqd = cic_to_cfqd(cic);
  3173. struct cfq_queue *cfqq;
  3174. /*
  3175. * Check whether ioprio has changed. The condition may trigger
  3176. * spuriously on a newly created cic but there's no harm.
  3177. */
  3178. if (unlikely(!cfqd) || likely(cic->ioprio == ioprio))
  3179. return;
  3180. cfqq = cic_to_cfqq(cic, false);
  3181. if (cfqq) {
  3182. cfq_put_queue(cfqq);
  3183. cfqq = cfq_get_queue(cfqd, BLK_RW_ASYNC, cic, bio);
  3184. cic_set_cfqq(cic, cfqq, false);
  3185. }
  3186. cfqq = cic_to_cfqq(cic, true);
  3187. if (cfqq)
  3188. cfq_mark_cfqq_prio_changed(cfqq);
  3189. cic->ioprio = ioprio;
  3190. }
  3191. static void cfq_init_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
  3192. pid_t pid, bool is_sync)
  3193. {
  3194. RB_CLEAR_NODE(&cfqq->rb_node);
  3195. RB_CLEAR_NODE(&cfqq->p_node);
  3196. INIT_LIST_HEAD(&cfqq->fifo);
  3197. cfqq->ref = 0;
  3198. cfqq->cfqd = cfqd;
  3199. cfq_mark_cfqq_prio_changed(cfqq);
  3200. if (is_sync) {
  3201. if (!cfq_class_idle(cfqq))
  3202. cfq_mark_cfqq_idle_window(cfqq);
  3203. cfq_mark_cfqq_sync(cfqq);
  3204. }
  3205. cfqq->pid = pid;
  3206. }
  3207. #ifdef CONFIG_CFQ_GROUP_IOSCHED
  3208. static void check_blkcg_changed(struct cfq_io_cq *cic, struct bio *bio)
  3209. {
  3210. struct cfq_data *cfqd = cic_to_cfqd(cic);
  3211. struct cfq_queue *cfqq;
  3212. uint64_t serial_nr;
  3213. rcu_read_lock();
  3214. serial_nr = bio_blkcg(bio)->css.serial_nr;
  3215. rcu_read_unlock();
  3216. /*
  3217. * Check whether blkcg has changed. The condition may trigger
  3218. * spuriously on a newly created cic but there's no harm.
  3219. */
  3220. if (unlikely(!cfqd) || likely(cic->blkcg_serial_nr == serial_nr))
  3221. return;
  3222. /*
  3223. * Drop reference to queues. New queues will be assigned in new
  3224. * group upon arrival of fresh requests.
  3225. */
  3226. cfqq = cic_to_cfqq(cic, false);
  3227. if (cfqq) {
  3228. cfq_log_cfqq(cfqd, cfqq, "changed cgroup");
  3229. cic_set_cfqq(cic, NULL, false);
  3230. cfq_put_queue(cfqq);
  3231. }
  3232. cfqq = cic_to_cfqq(cic, true);
  3233. if (cfqq) {
  3234. cfq_log_cfqq(cfqd, cfqq, "changed cgroup");
  3235. cic_set_cfqq(cic, NULL, true);
  3236. cfq_put_queue(cfqq);
  3237. }
  3238. cic->blkcg_serial_nr = serial_nr;
  3239. }
  3240. #else
  3241. static inline void check_blkcg_changed(struct cfq_io_cq *cic, struct bio *bio)
  3242. {
  3243. }
  3244. #endif /* CONFIG_CFQ_GROUP_IOSCHED */
  3245. static struct cfq_queue **
  3246. cfq_async_queue_prio(struct cfq_group *cfqg, int ioprio_class, int ioprio)
  3247. {
  3248. switch (ioprio_class) {
  3249. case IOPRIO_CLASS_RT:
  3250. return &cfqg->async_cfqq[0][ioprio];
  3251. case IOPRIO_CLASS_NONE:
  3252. ioprio = IOPRIO_NORM;
  3253. /* fall through */
  3254. case IOPRIO_CLASS_BE:
  3255. return &cfqg->async_cfqq[1][ioprio];
  3256. case IOPRIO_CLASS_IDLE:
  3257. return &cfqg->async_idle_cfqq;
  3258. default:
  3259. BUG();
  3260. }
  3261. }
  3262. static struct cfq_queue *
  3263. cfq_get_queue(struct cfq_data *cfqd, bool is_sync, struct cfq_io_cq *cic,
  3264. struct bio *bio)
  3265. {
  3266. int ioprio_class = IOPRIO_PRIO_CLASS(cic->ioprio);
  3267. int ioprio = IOPRIO_PRIO_DATA(cic->ioprio);
  3268. struct cfq_queue **async_cfqq = NULL;
  3269. struct cfq_queue *cfqq;
  3270. struct cfq_group *cfqg;
  3271. rcu_read_lock();
  3272. cfqg = cfq_lookup_cfqg(cfqd, bio_blkcg(bio));
  3273. if (!cfqg) {
  3274. cfqq = &cfqd->oom_cfqq;
  3275. goto out;
  3276. }
  3277. if (!is_sync) {
  3278. if (!ioprio_valid(cic->ioprio)) {
  3279. struct task_struct *tsk = current;
  3280. ioprio = task_nice_ioprio(tsk);
  3281. ioprio_class = task_nice_ioclass(tsk);
  3282. }
  3283. async_cfqq = cfq_async_queue_prio(cfqg, ioprio_class, ioprio);
  3284. cfqq = *async_cfqq;
  3285. if (cfqq)
  3286. goto out;
  3287. }
  3288. cfqq = kmem_cache_alloc_node(cfq_pool,
  3289. GFP_NOWAIT | __GFP_ZERO | __GFP_NOWARN,
  3290. cfqd->queue->node);
  3291. if (!cfqq) {
  3292. cfqq = &cfqd->oom_cfqq;
  3293. goto out;
  3294. }
  3295. /* cfq_init_cfqq() assumes cfqq->ioprio_class is initialized. */
  3296. cfqq->ioprio_class = IOPRIO_CLASS_NONE;
  3297. cfq_init_cfqq(cfqd, cfqq, current->pid, is_sync);
  3298. cfq_init_prio_data(cfqq, cic);
  3299. cfq_link_cfqq_cfqg(cfqq, cfqg);
  3300. cfq_log_cfqq(cfqd, cfqq, "alloced");
  3301. if (async_cfqq) {
  3302. /* a new async queue is created, pin and remember */
  3303. cfqq->ref++;
  3304. *async_cfqq = cfqq;
  3305. }
  3306. out:
  3307. cfqq->ref++;
  3308. rcu_read_unlock();
  3309. return cfqq;
  3310. }
  3311. static void
  3312. __cfq_update_io_thinktime(struct cfq_ttime *ttime, u64 slice_idle)
  3313. {
  3314. u64 elapsed = ktime_get_ns() - ttime->last_end_request;
  3315. elapsed = min(elapsed, 2UL * slice_idle);
  3316. ttime->ttime_samples = (7*ttime->ttime_samples + 256) / 8;
  3317. ttime->ttime_total = div_u64(7*ttime->ttime_total + 256*elapsed, 8);
  3318. ttime->ttime_mean = div64_ul(ttime->ttime_total + 128,
  3319. ttime->ttime_samples);
  3320. }
  3321. static void
  3322. cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_queue *cfqq,
  3323. struct cfq_io_cq *cic)
  3324. {
  3325. if (cfq_cfqq_sync(cfqq)) {
  3326. __cfq_update_io_thinktime(&cic->ttime, cfqd->cfq_slice_idle);
  3327. __cfq_update_io_thinktime(&cfqq->service_tree->ttime,
  3328. cfqd->cfq_slice_idle);
  3329. }
  3330. #ifdef CONFIG_CFQ_GROUP_IOSCHED
  3331. __cfq_update_io_thinktime(&cfqq->cfqg->ttime, cfqd->cfq_group_idle);
  3332. #endif
  3333. }
  3334. static void
  3335. cfq_update_io_seektime(struct cfq_data *cfqd, struct cfq_queue *cfqq,
  3336. struct request *rq)
  3337. {
  3338. sector_t sdist = 0;
  3339. sector_t n_sec = blk_rq_sectors(rq);
  3340. if (cfqq->last_request_pos) {
  3341. if (cfqq->last_request_pos < blk_rq_pos(rq))
  3342. sdist = blk_rq_pos(rq) - cfqq->last_request_pos;
  3343. else
  3344. sdist = cfqq->last_request_pos - blk_rq_pos(rq);
  3345. }
  3346. cfqq->seek_history <<= 1;
  3347. if (blk_queue_nonrot(cfqd->queue))
  3348. cfqq->seek_history |= (n_sec < CFQQ_SECT_THR_NONROT);
  3349. else
  3350. cfqq->seek_history |= (sdist > CFQQ_SEEK_THR);
  3351. }
  3352. static inline bool req_noidle(struct request *req)
  3353. {
  3354. return req_op(req) == REQ_OP_WRITE &&
  3355. (req->cmd_flags & (REQ_SYNC | REQ_IDLE)) == REQ_SYNC;
  3356. }
  3357. /*
  3358. * Disable idle window if the process thinks too long or seeks so much that
  3359. * it doesn't matter
  3360. */
  3361. static void
  3362. cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq,
  3363. struct cfq_io_cq *cic)
  3364. {
  3365. int old_idle, enable_idle;
  3366. /*
  3367. * Don't idle for async or idle io prio class
  3368. */
  3369. if (!cfq_cfqq_sync(cfqq) || cfq_class_idle(cfqq))
  3370. return;
  3371. enable_idle = old_idle = cfq_cfqq_idle_window(cfqq);
  3372. if (cfqq->queued[0] + cfqq->queued[1] >= 4)
  3373. cfq_mark_cfqq_deep(cfqq);
  3374. if (cfqq->next_rq && req_noidle(cfqq->next_rq))
  3375. enable_idle = 0;
  3376. else if (!atomic_read(&cic->icq.ioc->active_ref) ||
  3377. !cfqd->cfq_slice_idle ||
  3378. (!cfq_cfqq_deep(cfqq) && CFQQ_SEEKY(cfqq)))
  3379. enable_idle = 0;
  3380. else if (sample_valid(cic->ttime.ttime_samples)) {
  3381. if (cic->ttime.ttime_mean > cfqd->cfq_slice_idle)
  3382. enable_idle = 0;
  3383. else
  3384. enable_idle = 1;
  3385. }
  3386. if (old_idle != enable_idle) {
  3387. cfq_log_cfqq(cfqd, cfqq, "idle=%d", enable_idle);
  3388. if (enable_idle)
  3389. cfq_mark_cfqq_idle_window(cfqq);
  3390. else
  3391. cfq_clear_cfqq_idle_window(cfqq);
  3392. }
  3393. }
  3394. /*
  3395. * Check if new_cfqq should preempt the currently active queue. Return 0 for
  3396. * no or if we aren't sure, a 1 will cause a preempt.
  3397. */
  3398. static bool
  3399. cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
  3400. struct request *rq)
  3401. {
  3402. struct cfq_queue *cfqq;
  3403. cfqq = cfqd->active_queue;
  3404. if (!cfqq)
  3405. return false;
  3406. if (cfq_class_idle(new_cfqq))
  3407. return false;
  3408. if (cfq_class_idle(cfqq))
  3409. return true;
  3410. /*
  3411. * Don't allow a non-RT request to preempt an ongoing RT cfqq timeslice.
  3412. */
  3413. if (cfq_class_rt(cfqq) && !cfq_class_rt(new_cfqq))
  3414. return false;
  3415. /*
  3416. * if the new request is sync, but the currently running queue is
  3417. * not, let the sync request have priority.
  3418. */
  3419. if (rq_is_sync(rq) && !cfq_cfqq_sync(cfqq) && !cfq_cfqq_must_dispatch(cfqq))
  3420. return true;
  3421. /*
  3422. * Treat ancestors of current cgroup the same way as current cgroup.
  3423. * For anybody else we disallow preemption to guarantee service
  3424. * fairness among cgroups.
  3425. */
  3426. if (!cfqg_is_descendant(cfqq->cfqg, new_cfqq->cfqg))
  3427. return false;
  3428. if (cfq_slice_used(cfqq))
  3429. return true;
  3430. /*
  3431. * Allow an RT request to pre-empt an ongoing non-RT cfqq timeslice.
  3432. */
  3433. if (cfq_class_rt(new_cfqq) && !cfq_class_rt(cfqq))
  3434. return true;
  3435. WARN_ON_ONCE(cfqq->ioprio_class != new_cfqq->ioprio_class);
  3436. /* Allow preemption only if we are idling on sync-noidle tree */
  3437. if (cfqd->serving_wl_type == SYNC_NOIDLE_WORKLOAD &&
  3438. cfqq_type(new_cfqq) == SYNC_NOIDLE_WORKLOAD &&
  3439. RB_EMPTY_ROOT(&cfqq->sort_list))
  3440. return true;
  3441. /*
  3442. * So both queues are sync. Let the new request get disk time if
  3443. * it's a metadata request and the current queue is doing regular IO.
  3444. */
  3445. if ((rq->cmd_flags & REQ_PRIO) && !cfqq->prio_pending)
  3446. return true;
  3447. /* An idle queue should not be idle now for some reason */
  3448. if (RB_EMPTY_ROOT(&cfqq->sort_list) && !cfq_should_idle(cfqd, cfqq))
  3449. return true;
  3450. if (!cfqd->active_cic || !cfq_cfqq_wait_request(cfqq))
  3451. return false;
  3452. /*
  3453. * if this request is as-good as one we would expect from the
  3454. * current cfqq, let it preempt
  3455. */
  3456. if (cfq_rq_close(cfqd, cfqq, rq))
  3457. return true;
  3458. return false;
  3459. }
  3460. /*
  3461. * cfqq preempts the active queue. if we allowed preempt with no slice left,
  3462. * let it have half of its nominal slice.
  3463. */
  3464. static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  3465. {
  3466. enum wl_type_t old_type = cfqq_type(cfqd->active_queue);
  3467. cfq_log_cfqq(cfqd, cfqq, "preempt");
  3468. cfq_slice_expired(cfqd, 1);
  3469. /*
  3470. * workload type is changed, don't save slice, otherwise preempt
  3471. * doesn't happen
  3472. */
  3473. if (old_type != cfqq_type(cfqq))
  3474. cfqq->cfqg->saved_wl_slice = 0;
  3475. /*
  3476. * Put the new queue at the front of the of the current list,
  3477. * so we know that it will be selected next.
  3478. */
  3479. BUG_ON(!cfq_cfqq_on_rr(cfqq));
  3480. cfq_service_tree_add(cfqd, cfqq, 1);
  3481. cfqq->slice_end = 0;
  3482. cfq_mark_cfqq_slice_new(cfqq);
  3483. }
  3484. /*
  3485. * Called when a new fs request (rq) is added (to cfqq). Check if there's
  3486. * something we should do about it
  3487. */
  3488. static void
  3489. cfq_rq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
  3490. struct request *rq)
  3491. {
  3492. struct cfq_io_cq *cic = RQ_CIC(rq);
  3493. cfqd->rq_queued++;
  3494. if (rq->cmd_flags & REQ_PRIO)
  3495. cfqq->prio_pending++;
  3496. cfq_update_io_thinktime(cfqd, cfqq, cic);
  3497. cfq_update_io_seektime(cfqd, cfqq, rq);
  3498. cfq_update_idle_window(cfqd, cfqq, cic);
  3499. cfqq->last_request_pos = blk_rq_pos(rq) + blk_rq_sectors(rq);
  3500. if (cfqq == cfqd->active_queue) {
  3501. /*
  3502. * Remember that we saw a request from this process, but
  3503. * don't start queuing just yet. Otherwise we risk seeing lots
  3504. * of tiny requests, because we disrupt the normal plugging
  3505. * and merging. If the request is already larger than a single
  3506. * page, let it rip immediately. For that case we assume that
  3507. * merging is already done. Ditto for a busy system that
  3508. * has other work pending, don't risk delaying until the
  3509. * idle timer unplug to continue working.
  3510. */
  3511. if (cfq_cfqq_wait_request(cfqq)) {
  3512. if (blk_rq_bytes(rq) > PAGE_SIZE ||
  3513. cfqd->busy_queues > 1) {
  3514. cfq_del_timer(cfqd, cfqq);
  3515. cfq_clear_cfqq_wait_request(cfqq);
  3516. __blk_run_queue(cfqd->queue);
  3517. } else {
  3518. cfqg_stats_update_idle_time(cfqq->cfqg);
  3519. cfq_mark_cfqq_must_dispatch(cfqq);
  3520. }
  3521. }
  3522. } else if (cfq_should_preempt(cfqd, cfqq, rq)) {
  3523. /*
  3524. * not the active queue - expire current slice if it is
  3525. * idle and has expired it's mean thinktime or this new queue
  3526. * has some old slice time left and is of higher priority or
  3527. * this new queue is RT and the current one is BE
  3528. */
  3529. cfq_preempt_queue(cfqd, cfqq);
  3530. __blk_run_queue(cfqd->queue);
  3531. }
  3532. }
  3533. static void cfq_insert_request(struct request_queue *q, struct request *rq)
  3534. {
  3535. struct cfq_data *cfqd = q->elevator->elevator_data;
  3536. struct cfq_queue *cfqq = RQ_CFQQ(rq);
  3537. cfq_log_cfqq(cfqd, cfqq, "insert_request");
  3538. cfq_init_prio_data(cfqq, RQ_CIC(rq));
  3539. rq->fifo_time = ktime_get_ns() + cfqd->cfq_fifo_expire[rq_is_sync(rq)];
  3540. list_add_tail(&rq->queuelist, &cfqq->fifo);
  3541. cfq_add_rq_rb(rq);
  3542. cfqg_stats_update_io_add(RQ_CFQG(rq), cfqd->serving_group,
  3543. rq->cmd_flags);
  3544. cfq_rq_enqueued(cfqd, cfqq, rq);
  3545. }
  3546. /*
  3547. * Update hw_tag based on peak queue depth over 50 samples under
  3548. * sufficient load.
  3549. */
  3550. static void cfq_update_hw_tag(struct cfq_data *cfqd)
  3551. {
  3552. struct cfq_queue *cfqq = cfqd->active_queue;
  3553. if (cfqd->rq_in_driver > cfqd->hw_tag_est_depth)
  3554. cfqd->hw_tag_est_depth = cfqd->rq_in_driver;
  3555. if (cfqd->hw_tag == 1)
  3556. return;
  3557. if (cfqd->rq_queued <= CFQ_HW_QUEUE_MIN &&
  3558. cfqd->rq_in_driver <= CFQ_HW_QUEUE_MIN)
  3559. return;
  3560. /*
  3561. * If active queue hasn't enough requests and can idle, cfq might not
  3562. * dispatch sufficient requests to hardware. Don't zero hw_tag in this
  3563. * case
  3564. */
  3565. if (cfqq && cfq_cfqq_idle_window(cfqq) &&
  3566. cfqq->dispatched + cfqq->queued[0] + cfqq->queued[1] <
  3567. CFQ_HW_QUEUE_MIN && cfqd->rq_in_driver < CFQ_HW_QUEUE_MIN)
  3568. return;
  3569. if (cfqd->hw_tag_samples++ < 50)
  3570. return;
  3571. if (cfqd->hw_tag_est_depth >= CFQ_HW_QUEUE_MIN)
  3572. cfqd->hw_tag = 1;
  3573. else
  3574. cfqd->hw_tag = 0;
  3575. }
  3576. static bool cfq_should_wait_busy(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  3577. {
  3578. struct cfq_io_cq *cic = cfqd->active_cic;
  3579. u64 now = ktime_get_ns();
  3580. /* If the queue already has requests, don't wait */
  3581. if (!RB_EMPTY_ROOT(&cfqq->sort_list))
  3582. return false;
  3583. /* If there are other queues in the group, don't wait */
  3584. if (cfqq->cfqg->nr_cfqq > 1)
  3585. return false;
  3586. /* the only queue in the group, but think time is big */
  3587. if (cfq_io_thinktime_big(cfqd, &cfqq->cfqg->ttime, true))
  3588. return false;
  3589. if (cfq_slice_used(cfqq))
  3590. return true;
  3591. /* if slice left is less than think time, wait busy */
  3592. if (cic && sample_valid(cic->ttime.ttime_samples)
  3593. && (cfqq->slice_end - now < cic->ttime.ttime_mean))
  3594. return true;
  3595. /*
  3596. * If think times is less than a jiffy than ttime_mean=0 and above
  3597. * will not be true. It might happen that slice has not expired yet
  3598. * but will expire soon (4-5 ns) during select_queue(). To cover the
  3599. * case where think time is less than a jiffy, mark the queue wait
  3600. * busy if only 1 jiffy is left in the slice.
  3601. */
  3602. if (cfqq->slice_end - now <= jiffies_to_nsecs(1))
  3603. return true;
  3604. return false;
  3605. }
  3606. static void cfq_completed_request(struct request_queue *q, struct request *rq)
  3607. {
  3608. struct cfq_queue *cfqq = RQ_CFQQ(rq);
  3609. struct cfq_data *cfqd = cfqq->cfqd;
  3610. const int sync = rq_is_sync(rq);
  3611. u64 now = ktime_get_ns();
  3612. cfq_log_cfqq(cfqd, cfqq, "complete rqnoidle %d", req_noidle(rq));
  3613. cfq_update_hw_tag(cfqd);
  3614. WARN_ON(!cfqd->rq_in_driver);
  3615. WARN_ON(!cfqq->dispatched);
  3616. cfqd->rq_in_driver--;
  3617. cfqq->dispatched--;
  3618. (RQ_CFQG(rq))->dispatched--;
  3619. cfqg_stats_update_completion(cfqq->cfqg, rq_start_time_ns(rq),
  3620. rq_io_start_time_ns(rq), rq->cmd_flags);
  3621. cfqd->rq_in_flight[cfq_cfqq_sync(cfqq)]--;
  3622. if (sync) {
  3623. struct cfq_rb_root *st;
  3624. RQ_CIC(rq)->ttime.last_end_request = now;
  3625. if (cfq_cfqq_on_rr(cfqq))
  3626. st = cfqq->service_tree;
  3627. else
  3628. st = st_for(cfqq->cfqg, cfqq_class(cfqq),
  3629. cfqq_type(cfqq));
  3630. st->ttime.last_end_request = now;
  3631. /*
  3632. * We have to do this check in jiffies since start_time is in
  3633. * jiffies and it is not trivial to convert to ns. If
  3634. * cfq_fifo_expire[1] ever comes close to 1 jiffie, this test
  3635. * will become problematic but so far we are fine (the default
  3636. * is 128 ms).
  3637. */
  3638. if (!time_after(rq->start_time +
  3639. nsecs_to_jiffies(cfqd->cfq_fifo_expire[1]),
  3640. jiffies))
  3641. cfqd->last_delayed_sync = now;
  3642. }
  3643. #ifdef CONFIG_CFQ_GROUP_IOSCHED
  3644. cfqq->cfqg->ttime.last_end_request = now;
  3645. #endif
  3646. /*
  3647. * If this is the active queue, check if it needs to be expired,
  3648. * or if we want to idle in case it has no pending requests.
  3649. */
  3650. if (cfqd->active_queue == cfqq) {
  3651. const bool cfqq_empty = RB_EMPTY_ROOT(&cfqq->sort_list);
  3652. if (cfq_cfqq_slice_new(cfqq)) {
  3653. cfq_set_prio_slice(cfqd, cfqq);
  3654. cfq_clear_cfqq_slice_new(cfqq);
  3655. }
  3656. /*
  3657. * Should we wait for next request to come in before we expire
  3658. * the queue.
  3659. */
  3660. if (cfq_should_wait_busy(cfqd, cfqq)) {
  3661. u64 extend_sl = cfqd->cfq_slice_idle;
  3662. if (!cfqd->cfq_slice_idle)
  3663. extend_sl = cfqd->cfq_group_idle;
  3664. cfqq->slice_end = now + extend_sl;
  3665. cfq_mark_cfqq_wait_busy(cfqq);
  3666. cfq_log_cfqq(cfqd, cfqq, "will busy wait");
  3667. }
  3668. /*
  3669. * Idling is not enabled on:
  3670. * - expired queues
  3671. * - idle-priority queues
  3672. * - async queues
  3673. * - queues with still some requests queued
  3674. * - when there is a close cooperator
  3675. */
  3676. if (cfq_slice_used(cfqq) || cfq_class_idle(cfqq))
  3677. cfq_slice_expired(cfqd, 1);
  3678. else if (sync && cfqq_empty &&
  3679. !cfq_close_cooperator(cfqd, cfqq)) {
  3680. cfq_arm_slice_timer(cfqd);
  3681. }
  3682. }
  3683. if (!cfqd->rq_in_driver)
  3684. cfq_schedule_dispatch(cfqd);
  3685. }
  3686. static void cfqq_boost_on_prio(struct cfq_queue *cfqq, unsigned int op)
  3687. {
  3688. /*
  3689. * If REQ_PRIO is set, boost class and prio level, if it's below
  3690. * BE/NORM. If prio is not set, restore the potentially boosted
  3691. * class/prio level.
  3692. */
  3693. if (!(op & REQ_PRIO)) {
  3694. cfqq->ioprio_class = cfqq->org_ioprio_class;
  3695. cfqq->ioprio = cfqq->org_ioprio;
  3696. } else {
  3697. if (cfq_class_idle(cfqq))
  3698. cfqq->ioprio_class = IOPRIO_CLASS_BE;
  3699. if (cfqq->ioprio > IOPRIO_NORM)
  3700. cfqq->ioprio = IOPRIO_NORM;
  3701. }
  3702. }
  3703. static inline int __cfq_may_queue(struct cfq_queue *cfqq)
  3704. {
  3705. if (cfq_cfqq_wait_request(cfqq) && !cfq_cfqq_must_alloc_slice(cfqq)) {
  3706. cfq_mark_cfqq_must_alloc_slice(cfqq);
  3707. return ELV_MQUEUE_MUST;
  3708. }
  3709. return ELV_MQUEUE_MAY;
  3710. }
  3711. static int cfq_may_queue(struct request_queue *q, unsigned int op)
  3712. {
  3713. struct cfq_data *cfqd = q->elevator->elevator_data;
  3714. struct task_struct *tsk = current;
  3715. struct cfq_io_cq *cic;
  3716. struct cfq_queue *cfqq;
  3717. /*
  3718. * don't force setup of a queue from here, as a call to may_queue
  3719. * does not necessarily imply that a request actually will be queued.
  3720. * so just lookup a possibly existing queue, or return 'may queue'
  3721. * if that fails
  3722. */
  3723. cic = cfq_cic_lookup(cfqd, tsk->io_context);
  3724. if (!cic)
  3725. return ELV_MQUEUE_MAY;
  3726. cfqq = cic_to_cfqq(cic, op_is_sync(op));
  3727. if (cfqq) {
  3728. cfq_init_prio_data(cfqq, cic);
  3729. cfqq_boost_on_prio(cfqq, op);
  3730. return __cfq_may_queue(cfqq);
  3731. }
  3732. return ELV_MQUEUE_MAY;
  3733. }
  3734. /*
  3735. * queue lock held here
  3736. */
  3737. static void cfq_put_request(struct request *rq)
  3738. {
  3739. struct cfq_queue *cfqq = RQ_CFQQ(rq);
  3740. if (cfqq) {
  3741. const int rw = rq_data_dir(rq);
  3742. BUG_ON(!cfqq->allocated[rw]);
  3743. cfqq->allocated[rw]--;
  3744. /* Put down rq reference on cfqg */
  3745. cfqg_put(RQ_CFQG(rq));
  3746. rq->elv.priv[0] = NULL;
  3747. rq->elv.priv[1] = NULL;
  3748. cfq_put_queue(cfqq);
  3749. }
  3750. }
  3751. static struct cfq_queue *
  3752. cfq_merge_cfqqs(struct cfq_data *cfqd, struct cfq_io_cq *cic,
  3753. struct cfq_queue *cfqq)
  3754. {
  3755. cfq_log_cfqq(cfqd, cfqq, "merging with queue %p", cfqq->new_cfqq);
  3756. cic_set_cfqq(cic, cfqq->new_cfqq, 1);
  3757. cfq_mark_cfqq_coop(cfqq->new_cfqq);
  3758. cfq_put_queue(cfqq);
  3759. return cic_to_cfqq(cic, 1);
  3760. }
  3761. /*
  3762. * Returns NULL if a new cfqq should be allocated, or the old cfqq if this
  3763. * was the last process referring to said cfqq.
  3764. */
  3765. static struct cfq_queue *
  3766. split_cfqq(struct cfq_io_cq *cic, struct cfq_queue *cfqq)
  3767. {
  3768. if (cfqq_process_refs(cfqq) == 1) {
  3769. cfqq->pid = current->pid;
  3770. cfq_clear_cfqq_coop(cfqq);
  3771. cfq_clear_cfqq_split_coop(cfqq);
  3772. return cfqq;
  3773. }
  3774. cic_set_cfqq(cic, NULL, 1);
  3775. cfq_put_cooperator(cfqq);
  3776. cfq_put_queue(cfqq);
  3777. return NULL;
  3778. }
  3779. /*
  3780. * Allocate cfq data structures associated with this request.
  3781. */
  3782. static int
  3783. cfq_set_request(struct request_queue *q, struct request *rq, struct bio *bio,
  3784. gfp_t gfp_mask)
  3785. {
  3786. struct cfq_data *cfqd = q->elevator->elevator_data;
  3787. struct cfq_io_cq *cic = icq_to_cic(rq->elv.icq);
  3788. const int rw = rq_data_dir(rq);
  3789. const bool is_sync = rq_is_sync(rq);
  3790. struct cfq_queue *cfqq;
  3791. spin_lock_irq(q->queue_lock);
  3792. check_ioprio_changed(cic, bio);
  3793. check_blkcg_changed(cic, bio);
  3794. new_queue:
  3795. cfqq = cic_to_cfqq(cic, is_sync);
  3796. if (!cfqq || cfqq == &cfqd->oom_cfqq) {
  3797. if (cfqq)
  3798. cfq_put_queue(cfqq);
  3799. cfqq = cfq_get_queue(cfqd, is_sync, cic, bio);
  3800. cic_set_cfqq(cic, cfqq, is_sync);
  3801. } else {
  3802. /*
  3803. * If the queue was seeky for too long, break it apart.
  3804. */
  3805. if (cfq_cfqq_coop(cfqq) && cfq_cfqq_split_coop(cfqq)) {
  3806. cfq_log_cfqq(cfqd, cfqq, "breaking apart cfqq");
  3807. cfqq = split_cfqq(cic, cfqq);
  3808. if (!cfqq)
  3809. goto new_queue;
  3810. }
  3811. /*
  3812. * Check to see if this queue is scheduled to merge with
  3813. * another, closely cooperating queue. The merging of
  3814. * queues happens here as it must be done in process context.
  3815. * The reference on new_cfqq was taken in merge_cfqqs.
  3816. */
  3817. if (cfqq->new_cfqq)
  3818. cfqq = cfq_merge_cfqqs(cfqd, cic, cfqq);
  3819. }
  3820. cfqq->allocated[rw]++;
  3821. cfqq->ref++;
  3822. cfqg_get(cfqq->cfqg);
  3823. rq->elv.priv[0] = cfqq;
  3824. rq->elv.priv[1] = cfqq->cfqg;
  3825. spin_unlock_irq(q->queue_lock);
  3826. return 0;
  3827. }
  3828. static void cfq_kick_queue(struct work_struct *work)
  3829. {
  3830. struct cfq_data *cfqd =
  3831. container_of(work, struct cfq_data, unplug_work);
  3832. struct request_queue *q = cfqd->queue;
  3833. spin_lock_irq(q->queue_lock);
  3834. __blk_run_queue(cfqd->queue);
  3835. spin_unlock_irq(q->queue_lock);
  3836. }
  3837. /*
  3838. * Timer running if the active_queue is currently idling inside its time slice
  3839. */
  3840. static enum hrtimer_restart cfq_idle_slice_timer(struct hrtimer *timer)
  3841. {
  3842. struct cfq_data *cfqd = container_of(timer, struct cfq_data,
  3843. idle_slice_timer);
  3844. struct cfq_queue *cfqq;
  3845. unsigned long flags;
  3846. int timed_out = 1;
  3847. cfq_log(cfqd, "idle timer fired");
  3848. spin_lock_irqsave(cfqd->queue->queue_lock, flags);
  3849. cfqq = cfqd->active_queue;
  3850. if (cfqq) {
  3851. timed_out = 0;
  3852. /*
  3853. * We saw a request before the queue expired, let it through
  3854. */
  3855. if (cfq_cfqq_must_dispatch(cfqq))
  3856. goto out_kick;
  3857. /*
  3858. * expired
  3859. */
  3860. if (cfq_slice_used(cfqq))
  3861. goto expire;
  3862. /*
  3863. * only expire and reinvoke request handler, if there are
  3864. * other queues with pending requests
  3865. */
  3866. if (!cfqd->busy_queues)
  3867. goto out_cont;
  3868. /*
  3869. * not expired and it has a request pending, let it dispatch
  3870. */
  3871. if (!RB_EMPTY_ROOT(&cfqq->sort_list))
  3872. goto out_kick;
  3873. /*
  3874. * Queue depth flag is reset only when the idle didn't succeed
  3875. */
  3876. cfq_clear_cfqq_deep(cfqq);
  3877. }
  3878. expire:
  3879. cfq_slice_expired(cfqd, timed_out);
  3880. out_kick:
  3881. cfq_schedule_dispatch(cfqd);
  3882. out_cont:
  3883. spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
  3884. return HRTIMER_NORESTART;
  3885. }
  3886. static void cfq_shutdown_timer_wq(struct cfq_data *cfqd)
  3887. {
  3888. hrtimer_cancel(&cfqd->idle_slice_timer);
  3889. cancel_work_sync(&cfqd->unplug_work);
  3890. }
  3891. static void cfq_exit_queue(struct elevator_queue *e)
  3892. {
  3893. struct cfq_data *cfqd = e->elevator_data;
  3894. struct request_queue *q = cfqd->queue;
  3895. cfq_shutdown_timer_wq(cfqd);
  3896. spin_lock_irq(q->queue_lock);
  3897. if (cfqd->active_queue)
  3898. __cfq_slice_expired(cfqd, cfqd->active_queue, 0);
  3899. spin_unlock_irq(q->queue_lock);
  3900. cfq_shutdown_timer_wq(cfqd);
  3901. #ifdef CONFIG_CFQ_GROUP_IOSCHED
  3902. blkcg_deactivate_policy(q, &blkcg_policy_cfq);
  3903. #else
  3904. kfree(cfqd->root_group);
  3905. #endif
  3906. kfree(cfqd);
  3907. }
  3908. static int cfq_init_queue(struct request_queue *q, struct elevator_type *e)
  3909. {
  3910. struct cfq_data *cfqd;
  3911. struct blkcg_gq *blkg __maybe_unused;
  3912. int i, ret;
  3913. struct elevator_queue *eq;
  3914. eq = elevator_alloc(q, e);
  3915. if (!eq)
  3916. return -ENOMEM;
  3917. cfqd = kzalloc_node(sizeof(*cfqd), GFP_KERNEL, q->node);
  3918. if (!cfqd) {
  3919. kobject_put(&eq->kobj);
  3920. return -ENOMEM;
  3921. }
  3922. eq->elevator_data = cfqd;
  3923. cfqd->queue = q;
  3924. spin_lock_irq(q->queue_lock);
  3925. q->elevator = eq;
  3926. spin_unlock_irq(q->queue_lock);
  3927. /* Init root service tree */
  3928. cfqd->grp_service_tree = CFQ_RB_ROOT;
  3929. /* Init root group and prefer root group over other groups by default */
  3930. #ifdef CONFIG_CFQ_GROUP_IOSCHED
  3931. ret = blkcg_activate_policy(q, &blkcg_policy_cfq);
  3932. if (ret)
  3933. goto out_free;
  3934. cfqd->root_group = blkg_to_cfqg(q->root_blkg);
  3935. #else
  3936. ret = -ENOMEM;
  3937. cfqd->root_group = kzalloc_node(sizeof(*cfqd->root_group),
  3938. GFP_KERNEL, cfqd->queue->node);
  3939. if (!cfqd->root_group)
  3940. goto out_free;
  3941. cfq_init_cfqg_base(cfqd->root_group);
  3942. cfqd->root_group->weight = 2 * CFQ_WEIGHT_LEGACY_DFL;
  3943. cfqd->root_group->leaf_weight = 2 * CFQ_WEIGHT_LEGACY_DFL;
  3944. #endif
  3945. /*
  3946. * Not strictly needed (since RB_ROOT just clears the node and we
  3947. * zeroed cfqd on alloc), but better be safe in case someone decides
  3948. * to add magic to the rb code
  3949. */
  3950. for (i = 0; i < CFQ_PRIO_LISTS; i++)
  3951. cfqd->prio_trees[i] = RB_ROOT;
  3952. /*
  3953. * Our fallback cfqq if cfq_get_queue() runs into OOM issues.
  3954. * Grab a permanent reference to it, so that the normal code flow
  3955. * will not attempt to free it. oom_cfqq is linked to root_group
  3956. * but shouldn't hold a reference as it'll never be unlinked. Lose
  3957. * the reference from linking right away.
  3958. */
  3959. cfq_init_cfqq(cfqd, &cfqd->oom_cfqq, 1, 0);
  3960. cfqd->oom_cfqq.ref++;
  3961. spin_lock_irq(q->queue_lock);
  3962. cfq_link_cfqq_cfqg(&cfqd->oom_cfqq, cfqd->root_group);
  3963. cfqg_put(cfqd->root_group);
  3964. spin_unlock_irq(q->queue_lock);
  3965. hrtimer_init(&cfqd->idle_slice_timer, CLOCK_MONOTONIC,
  3966. HRTIMER_MODE_REL);
  3967. cfqd->idle_slice_timer.function = cfq_idle_slice_timer;
  3968. INIT_WORK(&cfqd->unplug_work, cfq_kick_queue);
  3969. cfqd->cfq_quantum = cfq_quantum;
  3970. cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0];
  3971. cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1];
  3972. cfqd->cfq_back_max = cfq_back_max;
  3973. cfqd->cfq_back_penalty = cfq_back_penalty;
  3974. cfqd->cfq_slice[0] = cfq_slice_async;
  3975. cfqd->cfq_slice[1] = cfq_slice_sync;
  3976. cfqd->cfq_target_latency = cfq_target_latency;
  3977. cfqd->cfq_slice_async_rq = cfq_slice_async_rq;
  3978. cfqd->cfq_slice_idle = cfq_slice_idle;
  3979. cfqd->cfq_group_idle = cfq_group_idle;
  3980. cfqd->cfq_latency = 1;
  3981. cfqd->hw_tag = -1;
  3982. /*
  3983. * we optimistically start assuming sync ops weren't delayed in last
  3984. * second, in order to have larger depth for async operations.
  3985. */
  3986. cfqd->last_delayed_sync = ktime_get_ns() - NSEC_PER_SEC;
  3987. return 0;
  3988. out_free:
  3989. kfree(cfqd);
  3990. kobject_put(&eq->kobj);
  3991. return ret;
  3992. }
  3993. static void cfq_registered_queue(struct request_queue *q)
  3994. {
  3995. struct elevator_queue *e = q->elevator;
  3996. struct cfq_data *cfqd = e->elevator_data;
  3997. /*
  3998. * Default to IOPS mode with no idling for SSDs
  3999. */
  4000. if (blk_queue_nonrot(q))
  4001. cfqd->cfq_slice_idle = 0;
  4002. wbt_disable_default(q);
  4003. }
  4004. /*
  4005. * sysfs parts below -->
  4006. */
  4007. static ssize_t
  4008. cfq_var_show(unsigned int var, char *page)
  4009. {
  4010. return sprintf(page, "%u\n", var);
  4011. }
  4012. static ssize_t
  4013. cfq_var_store(unsigned int *var, const char *page, size_t count)
  4014. {
  4015. char *p = (char *) page;
  4016. *var = simple_strtoul(p, &p, 10);
  4017. return count;
  4018. }
  4019. #define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
  4020. static ssize_t __FUNC(struct elevator_queue *e, char *page) \
  4021. { \
  4022. struct cfq_data *cfqd = e->elevator_data; \
  4023. u64 __data = __VAR; \
  4024. if (__CONV) \
  4025. __data = div_u64(__data, NSEC_PER_MSEC); \
  4026. return cfq_var_show(__data, (page)); \
  4027. }
  4028. SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0);
  4029. SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1);
  4030. SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1);
  4031. SHOW_FUNCTION(cfq_back_seek_max_show, cfqd->cfq_back_max, 0);
  4032. SHOW_FUNCTION(cfq_back_seek_penalty_show, cfqd->cfq_back_penalty, 0);
  4033. SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1);
  4034. SHOW_FUNCTION(cfq_group_idle_show, cfqd->cfq_group_idle, 1);
  4035. SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1);
  4036. SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1);
  4037. SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0);
  4038. SHOW_FUNCTION(cfq_low_latency_show, cfqd->cfq_latency, 0);
  4039. SHOW_FUNCTION(cfq_target_latency_show, cfqd->cfq_target_latency, 1);
  4040. #undef SHOW_FUNCTION
  4041. #define USEC_SHOW_FUNCTION(__FUNC, __VAR) \
  4042. static ssize_t __FUNC(struct elevator_queue *e, char *page) \
  4043. { \
  4044. struct cfq_data *cfqd = e->elevator_data; \
  4045. u64 __data = __VAR; \
  4046. __data = div_u64(__data, NSEC_PER_USEC); \
  4047. return cfq_var_show(__data, (page)); \
  4048. }
  4049. USEC_SHOW_FUNCTION(cfq_slice_idle_us_show, cfqd->cfq_slice_idle);
  4050. USEC_SHOW_FUNCTION(cfq_group_idle_us_show, cfqd->cfq_group_idle);
  4051. USEC_SHOW_FUNCTION(cfq_slice_sync_us_show, cfqd->cfq_slice[1]);
  4052. USEC_SHOW_FUNCTION(cfq_slice_async_us_show, cfqd->cfq_slice[0]);
  4053. USEC_SHOW_FUNCTION(cfq_target_latency_us_show, cfqd->cfq_target_latency);
  4054. #undef USEC_SHOW_FUNCTION
  4055. #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
  4056. static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count) \
  4057. { \
  4058. struct cfq_data *cfqd = e->elevator_data; \
  4059. unsigned int __data; \
  4060. int ret = cfq_var_store(&__data, (page), count); \
  4061. if (__data < (MIN)) \
  4062. __data = (MIN); \
  4063. else if (__data > (MAX)) \
  4064. __data = (MAX); \
  4065. if (__CONV) \
  4066. *(__PTR) = (u64)__data * NSEC_PER_MSEC; \
  4067. else \
  4068. *(__PTR) = __data; \
  4069. return ret; \
  4070. }
  4071. STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0);
  4072. STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1,
  4073. UINT_MAX, 1);
  4074. STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1,
  4075. UINT_MAX, 1);
  4076. STORE_FUNCTION(cfq_back_seek_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0);
  4077. STORE_FUNCTION(cfq_back_seek_penalty_store, &cfqd->cfq_back_penalty, 1,
  4078. UINT_MAX, 0);
  4079. STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1);
  4080. STORE_FUNCTION(cfq_group_idle_store, &cfqd->cfq_group_idle, 0, UINT_MAX, 1);
  4081. STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1);
  4082. STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1);
  4083. STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1,
  4084. UINT_MAX, 0);
  4085. STORE_FUNCTION(cfq_low_latency_store, &cfqd->cfq_latency, 0, 1, 0);
  4086. STORE_FUNCTION(cfq_target_latency_store, &cfqd->cfq_target_latency, 1, UINT_MAX, 1);
  4087. #undef STORE_FUNCTION
  4088. #define USEC_STORE_FUNCTION(__FUNC, __PTR, MIN, MAX) \
  4089. static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count) \
  4090. { \
  4091. struct cfq_data *cfqd = e->elevator_data; \
  4092. unsigned int __data; \
  4093. int ret = cfq_var_store(&__data, (page), count); \
  4094. if (__data < (MIN)) \
  4095. __data = (MIN); \
  4096. else if (__data > (MAX)) \
  4097. __data = (MAX); \
  4098. *(__PTR) = (u64)__data * NSEC_PER_USEC; \
  4099. return ret; \
  4100. }
  4101. USEC_STORE_FUNCTION(cfq_slice_idle_us_store, &cfqd->cfq_slice_idle, 0, UINT_MAX);
  4102. USEC_STORE_FUNCTION(cfq_group_idle_us_store, &cfqd->cfq_group_idle, 0, UINT_MAX);
  4103. USEC_STORE_FUNCTION(cfq_slice_sync_us_store, &cfqd->cfq_slice[1], 1, UINT_MAX);
  4104. USEC_STORE_FUNCTION(cfq_slice_async_us_store, &cfqd->cfq_slice[0], 1, UINT_MAX);
  4105. USEC_STORE_FUNCTION(cfq_target_latency_us_store, &cfqd->cfq_target_latency, 1, UINT_MAX);
  4106. #undef USEC_STORE_FUNCTION
  4107. #define CFQ_ATTR(name) \
  4108. __ATTR(name, S_IRUGO|S_IWUSR, cfq_##name##_show, cfq_##name##_store)
  4109. static struct elv_fs_entry cfq_attrs[] = {
  4110. CFQ_ATTR(quantum),
  4111. CFQ_ATTR(fifo_expire_sync),
  4112. CFQ_ATTR(fifo_expire_async),
  4113. CFQ_ATTR(back_seek_max),
  4114. CFQ_ATTR(back_seek_penalty),
  4115. CFQ_ATTR(slice_sync),
  4116. CFQ_ATTR(slice_sync_us),
  4117. CFQ_ATTR(slice_async),
  4118. CFQ_ATTR(slice_async_us),
  4119. CFQ_ATTR(slice_async_rq),
  4120. CFQ_ATTR(slice_idle),
  4121. CFQ_ATTR(slice_idle_us),
  4122. CFQ_ATTR(group_idle),
  4123. CFQ_ATTR(group_idle_us),
  4124. CFQ_ATTR(low_latency),
  4125. CFQ_ATTR(target_latency),
  4126. CFQ_ATTR(target_latency_us),
  4127. __ATTR_NULL
  4128. };
  4129. static struct elevator_type iosched_cfq = {
  4130. .ops.sq = {
  4131. .elevator_merge_fn = cfq_merge,
  4132. .elevator_merged_fn = cfq_merged_request,
  4133. .elevator_merge_req_fn = cfq_merged_requests,
  4134. .elevator_allow_bio_merge_fn = cfq_allow_bio_merge,
  4135. .elevator_allow_rq_merge_fn = cfq_allow_rq_merge,
  4136. .elevator_bio_merged_fn = cfq_bio_merged,
  4137. .elevator_dispatch_fn = cfq_dispatch_requests,
  4138. .elevator_add_req_fn = cfq_insert_request,
  4139. .elevator_activate_req_fn = cfq_activate_request,
  4140. .elevator_deactivate_req_fn = cfq_deactivate_request,
  4141. .elevator_completed_req_fn = cfq_completed_request,
  4142. .elevator_former_req_fn = elv_rb_former_request,
  4143. .elevator_latter_req_fn = elv_rb_latter_request,
  4144. .elevator_init_icq_fn = cfq_init_icq,
  4145. .elevator_exit_icq_fn = cfq_exit_icq,
  4146. .elevator_set_req_fn = cfq_set_request,
  4147. .elevator_put_req_fn = cfq_put_request,
  4148. .elevator_may_queue_fn = cfq_may_queue,
  4149. .elevator_init_fn = cfq_init_queue,
  4150. .elevator_exit_fn = cfq_exit_queue,
  4151. .elevator_registered_fn = cfq_registered_queue,
  4152. },
  4153. .icq_size = sizeof(struct cfq_io_cq),
  4154. .icq_align = __alignof__(struct cfq_io_cq),
  4155. .elevator_attrs = cfq_attrs,
  4156. .elevator_name = "cfq",
  4157. .elevator_owner = THIS_MODULE,
  4158. };
  4159. #ifdef CONFIG_CFQ_GROUP_IOSCHED
  4160. static struct blkcg_policy blkcg_policy_cfq = {
  4161. .dfl_cftypes = cfq_blkcg_files,
  4162. .legacy_cftypes = cfq_blkcg_legacy_files,
  4163. .cpd_alloc_fn = cfq_cpd_alloc,
  4164. .cpd_init_fn = cfq_cpd_init,
  4165. .cpd_free_fn = cfq_cpd_free,
  4166. .cpd_bind_fn = cfq_cpd_bind,
  4167. .pd_alloc_fn = cfq_pd_alloc,
  4168. .pd_init_fn = cfq_pd_init,
  4169. .pd_offline_fn = cfq_pd_offline,
  4170. .pd_free_fn = cfq_pd_free,
  4171. .pd_reset_stats_fn = cfq_pd_reset_stats,
  4172. };
  4173. #endif
  4174. static int __init cfq_init(void)
  4175. {
  4176. int ret;
  4177. #ifdef CONFIG_CFQ_GROUP_IOSCHED
  4178. ret = blkcg_policy_register(&blkcg_policy_cfq);
  4179. if (ret)
  4180. return ret;
  4181. #else
  4182. cfq_group_idle = 0;
  4183. #endif
  4184. ret = -ENOMEM;
  4185. cfq_pool = KMEM_CACHE(cfq_queue, 0);
  4186. if (!cfq_pool)
  4187. goto err_pol_unreg;
  4188. ret = elv_register(&iosched_cfq);
  4189. if (ret)
  4190. goto err_free_pool;
  4191. return 0;
  4192. err_free_pool:
  4193. kmem_cache_destroy(cfq_pool);
  4194. err_pol_unreg:
  4195. #ifdef CONFIG_CFQ_GROUP_IOSCHED
  4196. blkcg_policy_unregister(&blkcg_policy_cfq);
  4197. #endif
  4198. return ret;
  4199. }
  4200. static void __exit cfq_exit(void)
  4201. {
  4202. #ifdef CONFIG_CFQ_GROUP_IOSCHED
  4203. blkcg_policy_unregister(&blkcg_policy_cfq);
  4204. #endif
  4205. elv_unregister(&iosched_cfq);
  4206. kmem_cache_destroy(cfq_pool);
  4207. }
  4208. module_init(cfq_init);
  4209. module_exit(cfq_exit);
  4210. MODULE_AUTHOR("Jens Axboe");
  4211. MODULE_LICENSE("GPL");
  4212. MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");