cfq-iosched.c 100 KB

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