cfq-iosched.c 124 KB

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