cfq-iosched.c 127 KB

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