cfq-iosched.c 108 KB

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