cfq-iosched.c 107 KB

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