cfq-iosched.c 101 KB

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