cfq-iosched.c 99 KB

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