cfq-iosched.c 98 KB

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