cfq-iosched.c 108 KB

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