cfq-iosched.c 97 KB

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