cfq-iosched.c 128 KB

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