cfq-iosched.c 92 KB

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