cfq-iosched.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499
  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@suse.de>
  8. */
  9. #include <linux/config.h>
  10. #include <linux/module.h>
  11. #include <linux/blkdev.h>
  12. #include <linux/elevator.h>
  13. #include <linux/hash.h>
  14. #include <linux/rbtree.h>
  15. #include <linux/ioprio.h>
  16. /*
  17. * tunables
  18. */
  19. static const int cfq_quantum = 4; /* max queue in one round of service */
  20. static const int cfq_queued = 8; /* minimum rq allocate limit per-queue*/
  21. static const int cfq_fifo_expire[2] = { HZ / 4, HZ / 8 };
  22. static const int cfq_back_max = 16 * 1024; /* maximum backwards seek, in KiB */
  23. static const int cfq_back_penalty = 2; /* penalty of a backwards seek */
  24. static const int cfq_slice_sync = HZ / 10;
  25. static int cfq_slice_async = HZ / 25;
  26. static const int cfq_slice_async_rq = 2;
  27. static int cfq_slice_idle = HZ / 70;
  28. #define CFQ_IDLE_GRACE (HZ / 10)
  29. #define CFQ_SLICE_SCALE (5)
  30. #define CFQ_KEY_ASYNC (0)
  31. static DEFINE_SPINLOCK(cfq_exit_lock);
  32. /*
  33. * for the hash of cfqq inside the cfqd
  34. */
  35. #define CFQ_QHASH_SHIFT 6
  36. #define CFQ_QHASH_ENTRIES (1 << CFQ_QHASH_SHIFT)
  37. #define list_entry_qhash(entry) hlist_entry((entry), struct cfq_queue, cfq_hash)
  38. /*
  39. * for the hash of crq inside the cfqq
  40. */
  41. #define CFQ_MHASH_SHIFT 6
  42. #define CFQ_MHASH_BLOCK(sec) ((sec) >> 3)
  43. #define CFQ_MHASH_ENTRIES (1 << CFQ_MHASH_SHIFT)
  44. #define CFQ_MHASH_FN(sec) hash_long(CFQ_MHASH_BLOCK(sec), CFQ_MHASH_SHIFT)
  45. #define rq_hash_key(rq) ((rq)->sector + (rq)->nr_sectors)
  46. #define list_entry_hash(ptr) hlist_entry((ptr), struct cfq_rq, hash)
  47. #define list_entry_cfqq(ptr) list_entry((ptr), struct cfq_queue, cfq_list)
  48. #define list_entry_fifo(ptr) list_entry((ptr), struct request, queuelist)
  49. #define RQ_DATA(rq) (rq)->elevator_private
  50. /*
  51. * rb-tree defines
  52. */
  53. #define RB_EMPTY(node) ((node)->rb_node == NULL)
  54. #define RB_CLEAR(node) do { \
  55. memset(node, 0, sizeof(*node)); \
  56. } while (0)
  57. #define RB_CLEAR_ROOT(root) ((root)->rb_node = NULL)
  58. #define rb_entry_crq(node) rb_entry((node), struct cfq_rq, rb_node)
  59. #define rq_rb_key(rq) (rq)->sector
  60. static kmem_cache_t *crq_pool;
  61. static kmem_cache_t *cfq_pool;
  62. static kmem_cache_t *cfq_ioc_pool;
  63. static atomic_t ioc_count = ATOMIC_INIT(0);
  64. static struct completion *ioc_gone;
  65. #define CFQ_PRIO_LISTS IOPRIO_BE_NR
  66. #define cfq_class_idle(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_IDLE)
  67. #define cfq_class_be(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_BE)
  68. #define cfq_class_rt(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_RT)
  69. #define ASYNC (0)
  70. #define SYNC (1)
  71. #define cfq_cfqq_dispatched(cfqq) \
  72. ((cfqq)->on_dispatch[ASYNC] + (cfqq)->on_dispatch[SYNC])
  73. #define cfq_cfqq_class_sync(cfqq) ((cfqq)->key != CFQ_KEY_ASYNC)
  74. #define cfq_cfqq_sync(cfqq) \
  75. (cfq_cfqq_class_sync(cfqq) || (cfqq)->on_dispatch[SYNC])
  76. #define sample_valid(samples) ((samples) > 80)
  77. /*
  78. * Per block device queue structure
  79. */
  80. struct cfq_data {
  81. request_queue_t *queue;
  82. /*
  83. * rr list of queues with requests and the count of them
  84. */
  85. struct list_head rr_list[CFQ_PRIO_LISTS];
  86. struct list_head busy_rr;
  87. struct list_head cur_rr;
  88. struct list_head idle_rr;
  89. unsigned int busy_queues;
  90. /*
  91. * non-ordered list of empty cfqq's
  92. */
  93. struct list_head empty_list;
  94. /*
  95. * cfqq lookup hash
  96. */
  97. struct hlist_head *cfq_hash;
  98. /*
  99. * global crq hash for all queues
  100. */
  101. struct hlist_head *crq_hash;
  102. unsigned int max_queued;
  103. mempool_t *crq_pool;
  104. int rq_in_driver;
  105. int hw_tag;
  106. /*
  107. * schedule slice state info
  108. */
  109. /*
  110. * idle window management
  111. */
  112. struct timer_list idle_slice_timer;
  113. struct work_struct unplug_work;
  114. struct cfq_queue *active_queue;
  115. struct cfq_io_context *active_cic;
  116. int cur_prio, cur_end_prio;
  117. unsigned int dispatch_slice;
  118. struct timer_list idle_class_timer;
  119. sector_t last_sector;
  120. unsigned long last_end_request;
  121. unsigned int rq_starved;
  122. /*
  123. * tunables, see top of file
  124. */
  125. unsigned int cfq_quantum;
  126. unsigned int cfq_queued;
  127. unsigned int cfq_fifo_expire[2];
  128. unsigned int cfq_back_penalty;
  129. unsigned int cfq_back_max;
  130. unsigned int cfq_slice[2];
  131. unsigned int cfq_slice_async_rq;
  132. unsigned int cfq_slice_idle;
  133. struct list_head cic_list;
  134. };
  135. /*
  136. * Per process-grouping structure
  137. */
  138. struct cfq_queue {
  139. /* reference count */
  140. atomic_t ref;
  141. /* parent cfq_data */
  142. struct cfq_data *cfqd;
  143. /* cfqq lookup hash */
  144. struct hlist_node cfq_hash;
  145. /* hash key */
  146. unsigned int key;
  147. /* on either rr or empty list of cfqd */
  148. struct list_head cfq_list;
  149. /* sorted list of pending requests */
  150. struct rb_root sort_list;
  151. /* if fifo isn't expired, next request to serve */
  152. struct cfq_rq *next_crq;
  153. /* requests queued in sort_list */
  154. int queued[2];
  155. /* currently allocated requests */
  156. int allocated[2];
  157. /* fifo list of requests in sort_list */
  158. struct list_head fifo;
  159. unsigned long slice_start;
  160. unsigned long slice_end;
  161. unsigned long slice_left;
  162. unsigned long service_last;
  163. /* number of requests that are on the dispatch list */
  164. int on_dispatch[2];
  165. /* io prio of this group */
  166. unsigned short ioprio, org_ioprio;
  167. unsigned short ioprio_class, org_ioprio_class;
  168. /* various state flags, see below */
  169. unsigned int flags;
  170. };
  171. struct cfq_rq {
  172. struct rb_node rb_node;
  173. sector_t rb_key;
  174. struct request *request;
  175. struct hlist_node hash;
  176. struct cfq_queue *cfq_queue;
  177. struct cfq_io_context *io_context;
  178. unsigned int crq_flags;
  179. };
  180. enum cfqq_state_flags {
  181. CFQ_CFQQ_FLAG_on_rr = 0,
  182. CFQ_CFQQ_FLAG_wait_request,
  183. CFQ_CFQQ_FLAG_must_alloc,
  184. CFQ_CFQQ_FLAG_must_alloc_slice,
  185. CFQ_CFQQ_FLAG_must_dispatch,
  186. CFQ_CFQQ_FLAG_fifo_expire,
  187. CFQ_CFQQ_FLAG_idle_window,
  188. CFQ_CFQQ_FLAG_prio_changed,
  189. };
  190. #define CFQ_CFQQ_FNS(name) \
  191. static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq) \
  192. { \
  193. cfqq->flags |= (1 << CFQ_CFQQ_FLAG_##name); \
  194. } \
  195. static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq) \
  196. { \
  197. cfqq->flags &= ~(1 << CFQ_CFQQ_FLAG_##name); \
  198. } \
  199. static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq) \
  200. { \
  201. return (cfqq->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0; \
  202. }
  203. CFQ_CFQQ_FNS(on_rr);
  204. CFQ_CFQQ_FNS(wait_request);
  205. CFQ_CFQQ_FNS(must_alloc);
  206. CFQ_CFQQ_FNS(must_alloc_slice);
  207. CFQ_CFQQ_FNS(must_dispatch);
  208. CFQ_CFQQ_FNS(fifo_expire);
  209. CFQ_CFQQ_FNS(idle_window);
  210. CFQ_CFQQ_FNS(prio_changed);
  211. #undef CFQ_CFQQ_FNS
  212. enum cfq_rq_state_flags {
  213. CFQ_CRQ_FLAG_is_sync = 0,
  214. };
  215. #define CFQ_CRQ_FNS(name) \
  216. static inline void cfq_mark_crq_##name(struct cfq_rq *crq) \
  217. { \
  218. crq->crq_flags |= (1 << CFQ_CRQ_FLAG_##name); \
  219. } \
  220. static inline void cfq_clear_crq_##name(struct cfq_rq *crq) \
  221. { \
  222. crq->crq_flags &= ~(1 << CFQ_CRQ_FLAG_##name); \
  223. } \
  224. static inline int cfq_crq_##name(const struct cfq_rq *crq) \
  225. { \
  226. return (crq->crq_flags & (1 << CFQ_CRQ_FLAG_##name)) != 0; \
  227. }
  228. CFQ_CRQ_FNS(is_sync);
  229. #undef CFQ_CRQ_FNS
  230. static struct cfq_queue *cfq_find_cfq_hash(struct cfq_data *, unsigned int, unsigned short);
  231. static void cfq_dispatch_insert(request_queue_t *, struct cfq_rq *);
  232. static struct cfq_queue *cfq_get_queue(struct cfq_data *cfqd, unsigned int key, struct task_struct *tsk, gfp_t gfp_mask);
  233. #define process_sync(tsk) ((tsk)->flags & PF_SYNCWRITE)
  234. /*
  235. * lots of deadline iosched dupes, can be abstracted later...
  236. */
  237. static inline void cfq_del_crq_hash(struct cfq_rq *crq)
  238. {
  239. hlist_del_init(&crq->hash);
  240. }
  241. static inline void cfq_add_crq_hash(struct cfq_data *cfqd, struct cfq_rq *crq)
  242. {
  243. const int hash_idx = CFQ_MHASH_FN(rq_hash_key(crq->request));
  244. hlist_add_head(&crq->hash, &cfqd->crq_hash[hash_idx]);
  245. }
  246. static struct request *cfq_find_rq_hash(struct cfq_data *cfqd, sector_t offset)
  247. {
  248. struct hlist_head *hash_list = &cfqd->crq_hash[CFQ_MHASH_FN(offset)];
  249. struct hlist_node *entry, *next;
  250. hlist_for_each_safe(entry, next, hash_list) {
  251. struct cfq_rq *crq = list_entry_hash(entry);
  252. struct request *__rq = crq->request;
  253. if (!rq_mergeable(__rq)) {
  254. cfq_del_crq_hash(crq);
  255. continue;
  256. }
  257. if (rq_hash_key(__rq) == offset)
  258. return __rq;
  259. }
  260. return NULL;
  261. }
  262. /*
  263. * scheduler run of queue, if there are requests pending and no one in the
  264. * driver that will restart queueing
  265. */
  266. static inline void cfq_schedule_dispatch(struct cfq_data *cfqd)
  267. {
  268. if (cfqd->busy_queues)
  269. kblockd_schedule_work(&cfqd->unplug_work);
  270. }
  271. static int cfq_queue_empty(request_queue_t *q)
  272. {
  273. struct cfq_data *cfqd = q->elevator->elevator_data;
  274. return !cfqd->busy_queues;
  275. }
  276. static inline pid_t cfq_queue_pid(struct task_struct *task, int rw)
  277. {
  278. if (rw == READ || process_sync(task))
  279. return task->pid;
  280. return CFQ_KEY_ASYNC;
  281. }
  282. /*
  283. * Lifted from AS - choose which of crq1 and crq2 that is best served now.
  284. * We choose the request that is closest to the head right now. Distance
  285. * behind the head is penalized and only allowed to a certain extent.
  286. */
  287. static struct cfq_rq *
  288. cfq_choose_req(struct cfq_data *cfqd, struct cfq_rq *crq1, struct cfq_rq *crq2)
  289. {
  290. sector_t last, s1, s2, d1 = 0, d2 = 0;
  291. unsigned long back_max;
  292. #define CFQ_RQ1_WRAP 0x01 /* request 1 wraps */
  293. #define CFQ_RQ2_WRAP 0x02 /* request 2 wraps */
  294. unsigned wrap = 0; /* bit mask: requests behind the disk head? */
  295. if (crq1 == NULL || crq1 == crq2)
  296. return crq2;
  297. if (crq2 == NULL)
  298. return crq1;
  299. if (cfq_crq_is_sync(crq1) && !cfq_crq_is_sync(crq2))
  300. return crq1;
  301. else if (cfq_crq_is_sync(crq2) && !cfq_crq_is_sync(crq1))
  302. return crq2;
  303. s1 = crq1->request->sector;
  304. s2 = crq2->request->sector;
  305. last = cfqd->last_sector;
  306. /*
  307. * by definition, 1KiB is 2 sectors
  308. */
  309. back_max = cfqd->cfq_back_max * 2;
  310. /*
  311. * Strict one way elevator _except_ in the case where we allow
  312. * short backward seeks which are biased as twice the cost of a
  313. * similar forward seek.
  314. */
  315. if (s1 >= last)
  316. d1 = s1 - last;
  317. else if (s1 + back_max >= last)
  318. d1 = (last - s1) * cfqd->cfq_back_penalty;
  319. else
  320. wrap |= CFQ_RQ1_WRAP;
  321. if (s2 >= last)
  322. d2 = s2 - last;
  323. else if (s2 + back_max >= last)
  324. d2 = (last - s2) * cfqd->cfq_back_penalty;
  325. else
  326. wrap |= CFQ_RQ2_WRAP;
  327. /* Found required data */
  328. /*
  329. * By doing switch() on the bit mask "wrap" we avoid having to
  330. * check two variables for all permutations: --> faster!
  331. */
  332. switch (wrap) {
  333. case 0: /* common case for CFQ: crq1 and crq2 not wrapped */
  334. if (d1 < d2)
  335. return crq1;
  336. else if (d2 < d1)
  337. return crq2;
  338. else {
  339. if (s1 >= s2)
  340. return crq1;
  341. else
  342. return crq2;
  343. }
  344. case CFQ_RQ2_WRAP:
  345. return crq1;
  346. case CFQ_RQ1_WRAP:
  347. return crq2;
  348. case (CFQ_RQ1_WRAP|CFQ_RQ2_WRAP): /* both crqs wrapped */
  349. default:
  350. /*
  351. * Since both rqs are wrapped,
  352. * start with the one that's further behind head
  353. * (--> only *one* back seek required),
  354. * since back seek takes more time than forward.
  355. */
  356. if (s1 <= s2)
  357. return crq1;
  358. else
  359. return crq2;
  360. }
  361. }
  362. /*
  363. * would be nice to take fifo expire time into account as well
  364. */
  365. static struct cfq_rq *
  366. cfq_find_next_crq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
  367. struct cfq_rq *last)
  368. {
  369. struct cfq_rq *crq_next = NULL, *crq_prev = NULL;
  370. struct rb_node *rbnext, *rbprev;
  371. if (!(rbnext = rb_next(&last->rb_node))) {
  372. rbnext = rb_first(&cfqq->sort_list);
  373. if (rbnext == &last->rb_node)
  374. rbnext = NULL;
  375. }
  376. rbprev = rb_prev(&last->rb_node);
  377. if (rbprev)
  378. crq_prev = rb_entry_crq(rbprev);
  379. if (rbnext)
  380. crq_next = rb_entry_crq(rbnext);
  381. return cfq_choose_req(cfqd, crq_next, crq_prev);
  382. }
  383. static void cfq_update_next_crq(struct cfq_rq *crq)
  384. {
  385. struct cfq_queue *cfqq = crq->cfq_queue;
  386. if (cfqq->next_crq == crq)
  387. cfqq->next_crq = cfq_find_next_crq(cfqq->cfqd, cfqq, crq);
  388. }
  389. static void cfq_resort_rr_list(struct cfq_queue *cfqq, int preempted)
  390. {
  391. struct cfq_data *cfqd = cfqq->cfqd;
  392. struct list_head *list, *entry;
  393. BUG_ON(!cfq_cfqq_on_rr(cfqq));
  394. list_del(&cfqq->cfq_list);
  395. if (cfq_class_rt(cfqq))
  396. list = &cfqd->cur_rr;
  397. else if (cfq_class_idle(cfqq))
  398. list = &cfqd->idle_rr;
  399. else {
  400. /*
  401. * if cfqq has requests in flight, don't allow it to be
  402. * found in cfq_set_active_queue before it has finished them.
  403. * this is done to increase fairness between a process that
  404. * has lots of io pending vs one that only generates one
  405. * sporadically or synchronously
  406. */
  407. if (cfq_cfqq_dispatched(cfqq))
  408. list = &cfqd->busy_rr;
  409. else
  410. list = &cfqd->rr_list[cfqq->ioprio];
  411. }
  412. /*
  413. * if queue was preempted, just add to front to be fair. busy_rr
  414. * isn't sorted, but insert at the back for fairness.
  415. */
  416. if (preempted || list == &cfqd->busy_rr) {
  417. if (preempted)
  418. list = list->prev;
  419. list_add_tail(&cfqq->cfq_list, list);
  420. return;
  421. }
  422. /*
  423. * sort by when queue was last serviced
  424. */
  425. entry = list;
  426. while ((entry = entry->prev) != list) {
  427. struct cfq_queue *__cfqq = list_entry_cfqq(entry);
  428. if (!__cfqq->service_last)
  429. break;
  430. if (time_before(__cfqq->service_last, cfqq->service_last))
  431. break;
  432. }
  433. list_add(&cfqq->cfq_list, entry);
  434. }
  435. /*
  436. * add to busy list of queues for service, trying to be fair in ordering
  437. * the pending list according to last request service
  438. */
  439. static inline void
  440. cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  441. {
  442. BUG_ON(cfq_cfqq_on_rr(cfqq));
  443. cfq_mark_cfqq_on_rr(cfqq);
  444. cfqd->busy_queues++;
  445. cfq_resort_rr_list(cfqq, 0);
  446. }
  447. static inline void
  448. cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  449. {
  450. BUG_ON(!cfq_cfqq_on_rr(cfqq));
  451. cfq_clear_cfqq_on_rr(cfqq);
  452. list_move(&cfqq->cfq_list, &cfqd->empty_list);
  453. BUG_ON(!cfqd->busy_queues);
  454. cfqd->busy_queues--;
  455. }
  456. /*
  457. * rb tree support functions
  458. */
  459. static inline void cfq_del_crq_rb(struct cfq_rq *crq)
  460. {
  461. struct cfq_queue *cfqq = crq->cfq_queue;
  462. struct cfq_data *cfqd = cfqq->cfqd;
  463. const int sync = cfq_crq_is_sync(crq);
  464. BUG_ON(!cfqq->queued[sync]);
  465. cfqq->queued[sync]--;
  466. cfq_update_next_crq(crq);
  467. rb_erase(&crq->rb_node, &cfqq->sort_list);
  468. if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY(&cfqq->sort_list))
  469. cfq_del_cfqq_rr(cfqd, cfqq);
  470. }
  471. static struct cfq_rq *
  472. __cfq_add_crq_rb(struct cfq_rq *crq)
  473. {
  474. struct rb_node **p = &crq->cfq_queue->sort_list.rb_node;
  475. struct rb_node *parent = NULL;
  476. struct cfq_rq *__crq;
  477. while (*p) {
  478. parent = *p;
  479. __crq = rb_entry_crq(parent);
  480. if (crq->rb_key < __crq->rb_key)
  481. p = &(*p)->rb_left;
  482. else if (crq->rb_key > __crq->rb_key)
  483. p = &(*p)->rb_right;
  484. else
  485. return __crq;
  486. }
  487. rb_link_node(&crq->rb_node, parent, p);
  488. return NULL;
  489. }
  490. static void cfq_add_crq_rb(struct cfq_rq *crq)
  491. {
  492. struct cfq_queue *cfqq = crq->cfq_queue;
  493. struct cfq_data *cfqd = cfqq->cfqd;
  494. struct request *rq = crq->request;
  495. struct cfq_rq *__alias;
  496. crq->rb_key = rq_rb_key(rq);
  497. cfqq->queued[cfq_crq_is_sync(crq)]++;
  498. /*
  499. * looks a little odd, but the first insert might return an alias.
  500. * if that happens, put the alias on the dispatch list
  501. */
  502. while ((__alias = __cfq_add_crq_rb(crq)) != NULL)
  503. cfq_dispatch_insert(cfqd->queue, __alias);
  504. rb_insert_color(&crq->rb_node, &cfqq->sort_list);
  505. if (!cfq_cfqq_on_rr(cfqq))
  506. cfq_add_cfqq_rr(cfqd, cfqq);
  507. /*
  508. * check if this request is a better next-serve candidate
  509. */
  510. cfqq->next_crq = cfq_choose_req(cfqd, cfqq->next_crq, crq);
  511. }
  512. static inline void
  513. cfq_reposition_crq_rb(struct cfq_queue *cfqq, struct cfq_rq *crq)
  514. {
  515. rb_erase(&crq->rb_node, &cfqq->sort_list);
  516. cfqq->queued[cfq_crq_is_sync(crq)]--;
  517. cfq_add_crq_rb(crq);
  518. }
  519. static struct request *
  520. cfq_find_rq_fmerge(struct cfq_data *cfqd, struct bio *bio)
  521. {
  522. struct task_struct *tsk = current;
  523. pid_t key = cfq_queue_pid(tsk, bio_data_dir(bio));
  524. struct cfq_queue *cfqq;
  525. struct rb_node *n;
  526. sector_t sector;
  527. cfqq = cfq_find_cfq_hash(cfqd, key, tsk->ioprio);
  528. if (!cfqq)
  529. goto out;
  530. sector = bio->bi_sector + bio_sectors(bio);
  531. n = cfqq->sort_list.rb_node;
  532. while (n) {
  533. struct cfq_rq *crq = rb_entry_crq(n);
  534. if (sector < crq->rb_key)
  535. n = n->rb_left;
  536. else if (sector > crq->rb_key)
  537. n = n->rb_right;
  538. else
  539. return crq->request;
  540. }
  541. out:
  542. return NULL;
  543. }
  544. static void cfq_activate_request(request_queue_t *q, struct request *rq)
  545. {
  546. struct cfq_data *cfqd = q->elevator->elevator_data;
  547. cfqd->rq_in_driver++;
  548. /*
  549. * If the depth is larger 1, it really could be queueing. But lets
  550. * make the mark a little higher - idling could still be good for
  551. * low queueing, and a low queueing number could also just indicate
  552. * a SCSI mid layer like behaviour where limit+1 is often seen.
  553. */
  554. if (!cfqd->hw_tag && cfqd->rq_in_driver > 4)
  555. cfqd->hw_tag = 1;
  556. }
  557. static void cfq_deactivate_request(request_queue_t *q, struct request *rq)
  558. {
  559. struct cfq_data *cfqd = q->elevator->elevator_data;
  560. WARN_ON(!cfqd->rq_in_driver);
  561. cfqd->rq_in_driver--;
  562. }
  563. static void cfq_remove_request(struct request *rq)
  564. {
  565. struct cfq_rq *crq = RQ_DATA(rq);
  566. list_del_init(&rq->queuelist);
  567. cfq_del_crq_rb(crq);
  568. cfq_del_crq_hash(crq);
  569. }
  570. static int
  571. cfq_merge(request_queue_t *q, struct request **req, struct bio *bio)
  572. {
  573. struct cfq_data *cfqd = q->elevator->elevator_data;
  574. struct request *__rq;
  575. int ret;
  576. __rq = cfq_find_rq_hash(cfqd, bio->bi_sector);
  577. if (__rq && elv_rq_merge_ok(__rq, bio)) {
  578. ret = ELEVATOR_BACK_MERGE;
  579. goto out;
  580. }
  581. __rq = cfq_find_rq_fmerge(cfqd, bio);
  582. if (__rq && elv_rq_merge_ok(__rq, bio)) {
  583. ret = ELEVATOR_FRONT_MERGE;
  584. goto out;
  585. }
  586. return ELEVATOR_NO_MERGE;
  587. out:
  588. *req = __rq;
  589. return ret;
  590. }
  591. static void cfq_merged_request(request_queue_t *q, struct request *req)
  592. {
  593. struct cfq_data *cfqd = q->elevator->elevator_data;
  594. struct cfq_rq *crq = RQ_DATA(req);
  595. cfq_del_crq_hash(crq);
  596. cfq_add_crq_hash(cfqd, crq);
  597. if (rq_rb_key(req) != crq->rb_key) {
  598. struct cfq_queue *cfqq = crq->cfq_queue;
  599. cfq_update_next_crq(crq);
  600. cfq_reposition_crq_rb(cfqq, crq);
  601. }
  602. }
  603. static void
  604. cfq_merged_requests(request_queue_t *q, struct request *rq,
  605. struct request *next)
  606. {
  607. cfq_merged_request(q, rq);
  608. /*
  609. * reposition in fifo if next is older than rq
  610. */
  611. if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
  612. time_before(next->start_time, rq->start_time))
  613. list_move(&rq->queuelist, &next->queuelist);
  614. cfq_remove_request(next);
  615. }
  616. static inline void
  617. __cfq_set_active_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  618. {
  619. if (cfqq) {
  620. /*
  621. * stop potential idle class queues waiting service
  622. */
  623. del_timer(&cfqd->idle_class_timer);
  624. cfqq->slice_start = jiffies;
  625. cfqq->slice_end = 0;
  626. cfqq->slice_left = 0;
  627. cfq_clear_cfqq_must_alloc_slice(cfqq);
  628. cfq_clear_cfqq_fifo_expire(cfqq);
  629. }
  630. cfqd->active_queue = cfqq;
  631. }
  632. /*
  633. * current cfqq expired its slice (or was too idle), select new one
  634. */
  635. static void
  636. __cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq,
  637. int preempted)
  638. {
  639. unsigned long now = jiffies;
  640. if (cfq_cfqq_wait_request(cfqq))
  641. del_timer(&cfqd->idle_slice_timer);
  642. if (!preempted && !cfq_cfqq_dispatched(cfqq)) {
  643. cfqq->service_last = now;
  644. cfq_schedule_dispatch(cfqd);
  645. }
  646. cfq_clear_cfqq_must_dispatch(cfqq);
  647. cfq_clear_cfqq_wait_request(cfqq);
  648. /*
  649. * store what was left of this slice, if the queue idled out
  650. * or was preempted
  651. */
  652. if (time_after(cfqq->slice_end, now))
  653. cfqq->slice_left = cfqq->slice_end - now;
  654. else
  655. cfqq->slice_left = 0;
  656. if (cfq_cfqq_on_rr(cfqq))
  657. cfq_resort_rr_list(cfqq, preempted);
  658. if (cfqq == cfqd->active_queue)
  659. cfqd->active_queue = NULL;
  660. if (cfqd->active_cic) {
  661. put_io_context(cfqd->active_cic->ioc);
  662. cfqd->active_cic = NULL;
  663. }
  664. cfqd->dispatch_slice = 0;
  665. }
  666. static inline void cfq_slice_expired(struct cfq_data *cfqd, int preempted)
  667. {
  668. struct cfq_queue *cfqq = cfqd->active_queue;
  669. if (cfqq)
  670. __cfq_slice_expired(cfqd, cfqq, preempted);
  671. }
  672. /*
  673. * 0
  674. * 0,1
  675. * 0,1,2
  676. * 0,1,2,3
  677. * 0,1,2,3,4
  678. * 0,1,2,3,4,5
  679. * 0,1,2,3,4,5,6
  680. * 0,1,2,3,4,5,6,7
  681. */
  682. static int cfq_get_next_prio_level(struct cfq_data *cfqd)
  683. {
  684. int prio, wrap;
  685. prio = -1;
  686. wrap = 0;
  687. do {
  688. int p;
  689. for (p = cfqd->cur_prio; p <= cfqd->cur_end_prio; p++) {
  690. if (!list_empty(&cfqd->rr_list[p])) {
  691. prio = p;
  692. break;
  693. }
  694. }
  695. if (prio != -1)
  696. break;
  697. cfqd->cur_prio = 0;
  698. if (++cfqd->cur_end_prio == CFQ_PRIO_LISTS) {
  699. cfqd->cur_end_prio = 0;
  700. if (wrap)
  701. break;
  702. wrap = 1;
  703. }
  704. } while (1);
  705. if (unlikely(prio == -1))
  706. return -1;
  707. BUG_ON(prio >= CFQ_PRIO_LISTS);
  708. list_splice_init(&cfqd->rr_list[prio], &cfqd->cur_rr);
  709. cfqd->cur_prio = prio + 1;
  710. if (cfqd->cur_prio > cfqd->cur_end_prio) {
  711. cfqd->cur_end_prio = cfqd->cur_prio;
  712. cfqd->cur_prio = 0;
  713. }
  714. if (cfqd->cur_end_prio == CFQ_PRIO_LISTS) {
  715. cfqd->cur_prio = 0;
  716. cfqd->cur_end_prio = 0;
  717. }
  718. return prio;
  719. }
  720. static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd)
  721. {
  722. struct cfq_queue *cfqq = NULL;
  723. /*
  724. * if current list is non-empty, grab first entry. if it is empty,
  725. * get next prio level and grab first entry then if any are spliced
  726. */
  727. if (!list_empty(&cfqd->cur_rr) || cfq_get_next_prio_level(cfqd) != -1)
  728. cfqq = list_entry_cfqq(cfqd->cur_rr.next);
  729. /*
  730. * If no new queues are available, check if the busy list has some
  731. * before falling back to idle io.
  732. */
  733. if (!cfqq && !list_empty(&cfqd->busy_rr))
  734. cfqq = list_entry_cfqq(cfqd->busy_rr.next);
  735. /*
  736. * if we have idle queues and no rt or be queues had pending
  737. * requests, either allow immediate service if the grace period
  738. * has passed or arm the idle grace timer
  739. */
  740. if (!cfqq && !list_empty(&cfqd->idle_rr)) {
  741. unsigned long end = cfqd->last_end_request + CFQ_IDLE_GRACE;
  742. if (time_after_eq(jiffies, end))
  743. cfqq = list_entry_cfqq(cfqd->idle_rr.next);
  744. else
  745. mod_timer(&cfqd->idle_class_timer, end);
  746. }
  747. __cfq_set_active_queue(cfqd, cfqq);
  748. return cfqq;
  749. }
  750. static int cfq_arm_slice_timer(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  751. {
  752. struct cfq_io_context *cic;
  753. unsigned long sl;
  754. WARN_ON(!RB_EMPTY(&cfqq->sort_list));
  755. WARN_ON(cfqq != cfqd->active_queue);
  756. /*
  757. * idle is disabled, either manually or by past process history
  758. */
  759. if (!cfqd->cfq_slice_idle)
  760. return 0;
  761. if (!cfq_cfqq_idle_window(cfqq))
  762. return 0;
  763. /*
  764. * task has exited, don't wait
  765. */
  766. cic = cfqd->active_cic;
  767. if (!cic || !cic->ioc->task)
  768. return 0;
  769. cfq_mark_cfqq_must_dispatch(cfqq);
  770. cfq_mark_cfqq_wait_request(cfqq);
  771. sl = min(cfqq->slice_end - 1, (unsigned long) cfqd->cfq_slice_idle);
  772. /*
  773. * we don't want to idle for seeks, but we do want to allow
  774. * fair distribution of slice time for a process doing back-to-back
  775. * seeks. so allow a little bit of time for him to submit a new rq
  776. */
  777. if (sample_valid(cic->seek_samples) && cic->seek_mean > 131072)
  778. sl = 2;
  779. mod_timer(&cfqd->idle_slice_timer, jiffies + sl);
  780. return 1;
  781. }
  782. static void cfq_dispatch_insert(request_queue_t *q, struct cfq_rq *crq)
  783. {
  784. struct cfq_data *cfqd = q->elevator->elevator_data;
  785. struct cfq_queue *cfqq = crq->cfq_queue;
  786. cfqq->next_crq = cfq_find_next_crq(cfqd, cfqq, crq);
  787. cfq_remove_request(crq->request);
  788. cfqq->on_dispatch[cfq_crq_is_sync(crq)]++;
  789. elv_dispatch_sort(q, crq->request);
  790. }
  791. /*
  792. * return expired entry, or NULL to just start from scratch in rbtree
  793. */
  794. static inline struct cfq_rq *cfq_check_fifo(struct cfq_queue *cfqq)
  795. {
  796. struct cfq_data *cfqd = cfqq->cfqd;
  797. struct request *rq;
  798. struct cfq_rq *crq;
  799. if (cfq_cfqq_fifo_expire(cfqq))
  800. return NULL;
  801. if (!list_empty(&cfqq->fifo)) {
  802. int fifo = cfq_cfqq_class_sync(cfqq);
  803. crq = RQ_DATA(list_entry_fifo(cfqq->fifo.next));
  804. rq = crq->request;
  805. if (time_after(jiffies, rq->start_time + cfqd->cfq_fifo_expire[fifo])) {
  806. cfq_mark_cfqq_fifo_expire(cfqq);
  807. return crq;
  808. }
  809. }
  810. return NULL;
  811. }
  812. /*
  813. * Scale schedule slice based on io priority. Use the sync time slice only
  814. * if a queue is marked sync and has sync io queued. A sync queue with async
  815. * io only, should not get full sync slice length.
  816. */
  817. static inline int
  818. cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  819. {
  820. const int base_slice = cfqd->cfq_slice[cfq_cfqq_sync(cfqq)];
  821. WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
  822. return base_slice + (base_slice/CFQ_SLICE_SCALE * (4 - cfqq->ioprio));
  823. }
  824. static inline void
  825. cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  826. {
  827. cfqq->slice_end = cfq_prio_to_slice(cfqd, cfqq) + jiffies;
  828. }
  829. static inline int
  830. cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  831. {
  832. const int base_rq = cfqd->cfq_slice_async_rq;
  833. WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
  834. return 2 * (base_rq + base_rq * (CFQ_PRIO_LISTS - 1 - cfqq->ioprio));
  835. }
  836. /*
  837. * get next queue for service
  838. */
  839. static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd)
  840. {
  841. unsigned long now = jiffies;
  842. struct cfq_queue *cfqq;
  843. cfqq = cfqd->active_queue;
  844. if (!cfqq)
  845. goto new_queue;
  846. /*
  847. * slice has expired
  848. */
  849. if (!cfq_cfqq_must_dispatch(cfqq) && time_after(now, cfqq->slice_end))
  850. goto expire;
  851. /*
  852. * if queue has requests, dispatch one. if not, check if
  853. * enough slice is left to wait for one
  854. */
  855. if (!RB_EMPTY(&cfqq->sort_list))
  856. goto keep_queue;
  857. else if (cfq_cfqq_class_sync(cfqq) &&
  858. time_before(now, cfqq->slice_end)) {
  859. if (cfq_arm_slice_timer(cfqd, cfqq))
  860. return NULL;
  861. }
  862. expire:
  863. cfq_slice_expired(cfqd, 0);
  864. new_queue:
  865. cfqq = cfq_set_active_queue(cfqd);
  866. keep_queue:
  867. return cfqq;
  868. }
  869. static int
  870. __cfq_dispatch_requests(struct cfq_data *cfqd, struct cfq_queue *cfqq,
  871. int max_dispatch)
  872. {
  873. int dispatched = 0;
  874. BUG_ON(RB_EMPTY(&cfqq->sort_list));
  875. do {
  876. struct cfq_rq *crq;
  877. /*
  878. * follow expired path, else get first next available
  879. */
  880. if ((crq = cfq_check_fifo(cfqq)) == NULL)
  881. crq = cfqq->next_crq;
  882. /*
  883. * finally, insert request into driver dispatch list
  884. */
  885. cfq_dispatch_insert(cfqd->queue, crq);
  886. cfqd->dispatch_slice++;
  887. dispatched++;
  888. if (!cfqd->active_cic) {
  889. atomic_inc(&crq->io_context->ioc->refcount);
  890. cfqd->active_cic = crq->io_context;
  891. }
  892. if (RB_EMPTY(&cfqq->sort_list))
  893. break;
  894. } while (dispatched < max_dispatch);
  895. /*
  896. * if slice end isn't set yet, set it. if at least one request was
  897. * sync, use the sync time slice value
  898. */
  899. if (!cfqq->slice_end)
  900. cfq_set_prio_slice(cfqd, cfqq);
  901. /*
  902. * expire an async queue immediately if it has used up its slice. idle
  903. * queue always expire after 1 dispatch round.
  904. */
  905. if ((!cfq_cfqq_sync(cfqq) &&
  906. cfqd->dispatch_slice >= cfq_prio_to_maxrq(cfqd, cfqq)) ||
  907. cfq_class_idle(cfqq))
  908. cfq_slice_expired(cfqd, 0);
  909. return dispatched;
  910. }
  911. static int
  912. cfq_forced_dispatch_cfqqs(struct list_head *list)
  913. {
  914. int dispatched = 0;
  915. struct cfq_queue *cfqq, *next;
  916. struct cfq_rq *crq;
  917. list_for_each_entry_safe(cfqq, next, list, cfq_list) {
  918. while ((crq = cfqq->next_crq)) {
  919. cfq_dispatch_insert(cfqq->cfqd->queue, crq);
  920. dispatched++;
  921. }
  922. BUG_ON(!list_empty(&cfqq->fifo));
  923. }
  924. return dispatched;
  925. }
  926. static int
  927. cfq_forced_dispatch(struct cfq_data *cfqd)
  928. {
  929. int i, dispatched = 0;
  930. for (i = 0; i < CFQ_PRIO_LISTS; i++)
  931. dispatched += cfq_forced_dispatch_cfqqs(&cfqd->rr_list[i]);
  932. dispatched += cfq_forced_dispatch_cfqqs(&cfqd->busy_rr);
  933. dispatched += cfq_forced_dispatch_cfqqs(&cfqd->cur_rr);
  934. dispatched += cfq_forced_dispatch_cfqqs(&cfqd->idle_rr);
  935. cfq_slice_expired(cfqd, 0);
  936. BUG_ON(cfqd->busy_queues);
  937. return dispatched;
  938. }
  939. static int
  940. cfq_dispatch_requests(request_queue_t *q, int force)
  941. {
  942. struct cfq_data *cfqd = q->elevator->elevator_data;
  943. struct cfq_queue *cfqq;
  944. if (!cfqd->busy_queues)
  945. return 0;
  946. if (unlikely(force))
  947. return cfq_forced_dispatch(cfqd);
  948. cfqq = cfq_select_queue(cfqd);
  949. if (cfqq) {
  950. int max_dispatch;
  951. cfq_clear_cfqq_must_dispatch(cfqq);
  952. cfq_clear_cfqq_wait_request(cfqq);
  953. del_timer(&cfqd->idle_slice_timer);
  954. max_dispatch = cfqd->cfq_quantum;
  955. if (cfq_class_idle(cfqq))
  956. max_dispatch = 1;
  957. return __cfq_dispatch_requests(cfqd, cfqq, max_dispatch);
  958. }
  959. return 0;
  960. }
  961. /*
  962. * task holds one reference to the queue, dropped when task exits. each crq
  963. * in-flight on this queue also holds a reference, dropped when crq is freed.
  964. *
  965. * queue lock must be held here.
  966. */
  967. static void cfq_put_queue(struct cfq_queue *cfqq)
  968. {
  969. struct cfq_data *cfqd = cfqq->cfqd;
  970. BUG_ON(atomic_read(&cfqq->ref) <= 0);
  971. if (!atomic_dec_and_test(&cfqq->ref))
  972. return;
  973. BUG_ON(rb_first(&cfqq->sort_list));
  974. BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
  975. BUG_ON(cfq_cfqq_on_rr(cfqq));
  976. if (unlikely(cfqd->active_queue == cfqq))
  977. __cfq_slice_expired(cfqd, cfqq, 0);
  978. /*
  979. * it's on the empty list and still hashed
  980. */
  981. list_del(&cfqq->cfq_list);
  982. hlist_del(&cfqq->cfq_hash);
  983. kmem_cache_free(cfq_pool, cfqq);
  984. }
  985. static inline struct cfq_queue *
  986. __cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, unsigned int prio,
  987. const int hashval)
  988. {
  989. struct hlist_head *hash_list = &cfqd->cfq_hash[hashval];
  990. struct hlist_node *entry;
  991. struct cfq_queue *__cfqq;
  992. hlist_for_each_entry(__cfqq, entry, hash_list, cfq_hash) {
  993. const unsigned short __p = IOPRIO_PRIO_VALUE(__cfqq->org_ioprio_class, __cfqq->org_ioprio);
  994. if (__cfqq->key == key && (__p == prio || !prio))
  995. return __cfqq;
  996. }
  997. return NULL;
  998. }
  999. static struct cfq_queue *
  1000. cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, unsigned short prio)
  1001. {
  1002. return __cfq_find_cfq_hash(cfqd, key, prio, hash_long(key, CFQ_QHASH_SHIFT));
  1003. }
  1004. static void cfq_free_io_context(struct io_context *ioc)
  1005. {
  1006. struct cfq_io_context *__cic;
  1007. struct rb_node *n;
  1008. int freed = 0;
  1009. while ((n = rb_first(&ioc->cic_root)) != NULL) {
  1010. __cic = rb_entry(n, struct cfq_io_context, rb_node);
  1011. rb_erase(&__cic->rb_node, &ioc->cic_root);
  1012. kmem_cache_free(cfq_ioc_pool, __cic);
  1013. freed++;
  1014. }
  1015. if (atomic_sub_and_test(freed, &ioc_count) && ioc_gone)
  1016. complete(ioc_gone);
  1017. }
  1018. static void cfq_trim(struct io_context *ioc)
  1019. {
  1020. ioc->set_ioprio = NULL;
  1021. cfq_free_io_context(ioc);
  1022. }
  1023. /*
  1024. * Called with interrupts disabled
  1025. */
  1026. static void cfq_exit_single_io_context(struct cfq_io_context *cic)
  1027. {
  1028. struct cfq_data *cfqd = cic->key;
  1029. request_queue_t *q;
  1030. if (!cfqd)
  1031. return;
  1032. q = cfqd->queue;
  1033. WARN_ON(!irqs_disabled());
  1034. spin_lock(q->queue_lock);
  1035. if (cic->cfqq[ASYNC]) {
  1036. if (unlikely(cic->cfqq[ASYNC] == cfqd->active_queue))
  1037. __cfq_slice_expired(cfqd, cic->cfqq[ASYNC], 0);
  1038. cfq_put_queue(cic->cfqq[ASYNC]);
  1039. cic->cfqq[ASYNC] = NULL;
  1040. }
  1041. if (cic->cfqq[SYNC]) {
  1042. if (unlikely(cic->cfqq[SYNC] == cfqd->active_queue))
  1043. __cfq_slice_expired(cfqd, cic->cfqq[SYNC], 0);
  1044. cfq_put_queue(cic->cfqq[SYNC]);
  1045. cic->cfqq[SYNC] = NULL;
  1046. }
  1047. cic->key = NULL;
  1048. list_del_init(&cic->queue_list);
  1049. spin_unlock(q->queue_lock);
  1050. }
  1051. static void cfq_exit_io_context(struct io_context *ioc)
  1052. {
  1053. struct cfq_io_context *__cic;
  1054. unsigned long flags;
  1055. struct rb_node *n;
  1056. /*
  1057. * put the reference this task is holding to the various queues
  1058. */
  1059. spin_lock_irqsave(&cfq_exit_lock, flags);
  1060. n = rb_first(&ioc->cic_root);
  1061. while (n != NULL) {
  1062. __cic = rb_entry(n, struct cfq_io_context, rb_node);
  1063. cfq_exit_single_io_context(__cic);
  1064. n = rb_next(n);
  1065. }
  1066. spin_unlock_irqrestore(&cfq_exit_lock, flags);
  1067. }
  1068. static struct cfq_io_context *
  1069. cfq_alloc_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
  1070. {
  1071. struct cfq_io_context *cic = kmem_cache_alloc(cfq_ioc_pool, gfp_mask);
  1072. if (cic) {
  1073. memset(cic, 0, sizeof(*cic));
  1074. RB_CLEAR_COLOR(&cic->rb_node);
  1075. cic->last_end_request = jiffies;
  1076. INIT_LIST_HEAD(&cic->queue_list);
  1077. cic->dtor = cfq_free_io_context;
  1078. cic->exit = cfq_exit_io_context;
  1079. atomic_inc(&ioc_count);
  1080. }
  1081. return cic;
  1082. }
  1083. static void cfq_init_prio_data(struct cfq_queue *cfqq)
  1084. {
  1085. struct task_struct *tsk = current;
  1086. int ioprio_class;
  1087. if (!cfq_cfqq_prio_changed(cfqq))
  1088. return;
  1089. ioprio_class = IOPRIO_PRIO_CLASS(tsk->ioprio);
  1090. switch (ioprio_class) {
  1091. default:
  1092. printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class);
  1093. case IOPRIO_CLASS_NONE:
  1094. /*
  1095. * no prio set, place us in the middle of the BE classes
  1096. */
  1097. cfqq->ioprio = task_nice_ioprio(tsk);
  1098. cfqq->ioprio_class = IOPRIO_CLASS_BE;
  1099. break;
  1100. case IOPRIO_CLASS_RT:
  1101. cfqq->ioprio = task_ioprio(tsk);
  1102. cfqq->ioprio_class = IOPRIO_CLASS_RT;
  1103. break;
  1104. case IOPRIO_CLASS_BE:
  1105. cfqq->ioprio = task_ioprio(tsk);
  1106. cfqq->ioprio_class = IOPRIO_CLASS_BE;
  1107. break;
  1108. case IOPRIO_CLASS_IDLE:
  1109. cfqq->ioprio_class = IOPRIO_CLASS_IDLE;
  1110. cfqq->ioprio = 7;
  1111. cfq_clear_cfqq_idle_window(cfqq);
  1112. break;
  1113. }
  1114. /*
  1115. * keep track of original prio settings in case we have to temporarily
  1116. * elevate the priority of this queue
  1117. */
  1118. cfqq->org_ioprio = cfqq->ioprio;
  1119. cfqq->org_ioprio_class = cfqq->ioprio_class;
  1120. if (cfq_cfqq_on_rr(cfqq))
  1121. cfq_resort_rr_list(cfqq, 0);
  1122. cfq_clear_cfqq_prio_changed(cfqq);
  1123. }
  1124. static inline void changed_ioprio(struct cfq_io_context *cic)
  1125. {
  1126. struct cfq_data *cfqd = cic->key;
  1127. struct cfq_queue *cfqq;
  1128. if (cfqd) {
  1129. spin_lock(cfqd->queue->queue_lock);
  1130. cfqq = cic->cfqq[ASYNC];
  1131. if (cfqq) {
  1132. struct cfq_queue *new_cfqq;
  1133. new_cfqq = cfq_get_queue(cfqd, CFQ_KEY_ASYNC,
  1134. cic->ioc->task, GFP_ATOMIC);
  1135. if (new_cfqq) {
  1136. cic->cfqq[ASYNC] = new_cfqq;
  1137. cfq_put_queue(cfqq);
  1138. }
  1139. }
  1140. cfqq = cic->cfqq[SYNC];
  1141. if (cfqq) {
  1142. cfq_mark_cfqq_prio_changed(cfqq);
  1143. cfq_init_prio_data(cfqq);
  1144. }
  1145. spin_unlock(cfqd->queue->queue_lock);
  1146. }
  1147. }
  1148. /*
  1149. * callback from sys_ioprio_set, irqs are disabled
  1150. */
  1151. static int cfq_ioc_set_ioprio(struct io_context *ioc, unsigned int ioprio)
  1152. {
  1153. struct cfq_io_context *cic;
  1154. struct rb_node *n;
  1155. spin_lock(&cfq_exit_lock);
  1156. n = rb_first(&ioc->cic_root);
  1157. while (n != NULL) {
  1158. cic = rb_entry(n, struct cfq_io_context, rb_node);
  1159. changed_ioprio(cic);
  1160. n = rb_next(n);
  1161. }
  1162. spin_unlock(&cfq_exit_lock);
  1163. return 0;
  1164. }
  1165. static struct cfq_queue *
  1166. cfq_get_queue(struct cfq_data *cfqd, unsigned int key, struct task_struct *tsk,
  1167. gfp_t gfp_mask)
  1168. {
  1169. const int hashval = hash_long(key, CFQ_QHASH_SHIFT);
  1170. struct cfq_queue *cfqq, *new_cfqq = NULL;
  1171. unsigned short ioprio;
  1172. retry:
  1173. ioprio = tsk->ioprio;
  1174. cfqq = __cfq_find_cfq_hash(cfqd, key, ioprio, hashval);
  1175. if (!cfqq) {
  1176. if (new_cfqq) {
  1177. cfqq = new_cfqq;
  1178. new_cfqq = NULL;
  1179. } else if (gfp_mask & __GFP_WAIT) {
  1180. spin_unlock_irq(cfqd->queue->queue_lock);
  1181. new_cfqq = kmem_cache_alloc(cfq_pool, gfp_mask);
  1182. spin_lock_irq(cfqd->queue->queue_lock);
  1183. goto retry;
  1184. } else {
  1185. cfqq = kmem_cache_alloc(cfq_pool, gfp_mask);
  1186. if (!cfqq)
  1187. goto out;
  1188. }
  1189. memset(cfqq, 0, sizeof(*cfqq));
  1190. INIT_HLIST_NODE(&cfqq->cfq_hash);
  1191. INIT_LIST_HEAD(&cfqq->cfq_list);
  1192. RB_CLEAR_ROOT(&cfqq->sort_list);
  1193. INIT_LIST_HEAD(&cfqq->fifo);
  1194. cfqq->key = key;
  1195. hlist_add_head(&cfqq->cfq_hash, &cfqd->cfq_hash[hashval]);
  1196. atomic_set(&cfqq->ref, 0);
  1197. cfqq->cfqd = cfqd;
  1198. cfqq->service_last = 0;
  1199. /*
  1200. * set ->slice_left to allow preemption for a new process
  1201. */
  1202. cfqq->slice_left = 2 * cfqd->cfq_slice_idle;
  1203. if (!cfqd->hw_tag)
  1204. cfq_mark_cfqq_idle_window(cfqq);
  1205. cfq_mark_cfqq_prio_changed(cfqq);
  1206. cfq_init_prio_data(cfqq);
  1207. }
  1208. if (new_cfqq)
  1209. kmem_cache_free(cfq_pool, new_cfqq);
  1210. atomic_inc(&cfqq->ref);
  1211. out:
  1212. WARN_ON((gfp_mask & __GFP_WAIT) && !cfqq);
  1213. return cfqq;
  1214. }
  1215. static void
  1216. cfq_drop_dead_cic(struct io_context *ioc, struct cfq_io_context *cic)
  1217. {
  1218. spin_lock(&cfq_exit_lock);
  1219. rb_erase(&cic->rb_node, &ioc->cic_root);
  1220. list_del_init(&cic->queue_list);
  1221. spin_unlock(&cfq_exit_lock);
  1222. kmem_cache_free(cfq_ioc_pool, cic);
  1223. atomic_dec(&ioc_count);
  1224. }
  1225. static struct cfq_io_context *
  1226. cfq_cic_rb_lookup(struct cfq_data *cfqd, struct io_context *ioc)
  1227. {
  1228. struct rb_node *n;
  1229. struct cfq_io_context *cic;
  1230. void *k, *key = cfqd;
  1231. restart:
  1232. n = ioc->cic_root.rb_node;
  1233. while (n) {
  1234. cic = rb_entry(n, struct cfq_io_context, rb_node);
  1235. /* ->key must be copied to avoid race with cfq_exit_queue() */
  1236. k = cic->key;
  1237. if (unlikely(!k)) {
  1238. cfq_drop_dead_cic(ioc, cic);
  1239. goto restart;
  1240. }
  1241. if (key < k)
  1242. n = n->rb_left;
  1243. else if (key > k)
  1244. n = n->rb_right;
  1245. else
  1246. return cic;
  1247. }
  1248. return NULL;
  1249. }
  1250. static inline void
  1251. cfq_cic_link(struct cfq_data *cfqd, struct io_context *ioc,
  1252. struct cfq_io_context *cic)
  1253. {
  1254. struct rb_node **p;
  1255. struct rb_node *parent;
  1256. struct cfq_io_context *__cic;
  1257. void *k;
  1258. cic->ioc = ioc;
  1259. cic->key = cfqd;
  1260. ioc->set_ioprio = cfq_ioc_set_ioprio;
  1261. restart:
  1262. parent = NULL;
  1263. p = &ioc->cic_root.rb_node;
  1264. while (*p) {
  1265. parent = *p;
  1266. __cic = rb_entry(parent, struct cfq_io_context, rb_node);
  1267. /* ->key must be copied to avoid race with cfq_exit_queue() */
  1268. k = __cic->key;
  1269. if (unlikely(!k)) {
  1270. cfq_drop_dead_cic(ioc, cic);
  1271. goto restart;
  1272. }
  1273. if (cic->key < k)
  1274. p = &(*p)->rb_left;
  1275. else if (cic->key > k)
  1276. p = &(*p)->rb_right;
  1277. else
  1278. BUG();
  1279. }
  1280. spin_lock(&cfq_exit_lock);
  1281. rb_link_node(&cic->rb_node, parent, p);
  1282. rb_insert_color(&cic->rb_node, &ioc->cic_root);
  1283. list_add(&cic->queue_list, &cfqd->cic_list);
  1284. spin_unlock(&cfq_exit_lock);
  1285. }
  1286. /*
  1287. * Setup general io context and cfq io context. There can be several cfq
  1288. * io contexts per general io context, if this process is doing io to more
  1289. * than one device managed by cfq.
  1290. */
  1291. static struct cfq_io_context *
  1292. cfq_get_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
  1293. {
  1294. struct io_context *ioc = NULL;
  1295. struct cfq_io_context *cic;
  1296. might_sleep_if(gfp_mask & __GFP_WAIT);
  1297. ioc = get_io_context(gfp_mask);
  1298. if (!ioc)
  1299. return NULL;
  1300. cic = cfq_cic_rb_lookup(cfqd, ioc);
  1301. if (cic)
  1302. goto out;
  1303. cic = cfq_alloc_io_context(cfqd, gfp_mask);
  1304. if (cic == NULL)
  1305. goto err;
  1306. cfq_cic_link(cfqd, ioc, cic);
  1307. out:
  1308. return cic;
  1309. err:
  1310. put_io_context(ioc);
  1311. return NULL;
  1312. }
  1313. static void
  1314. cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_io_context *cic)
  1315. {
  1316. unsigned long elapsed, ttime;
  1317. /*
  1318. * if this context already has stuff queued, thinktime is from
  1319. * last queue not last end
  1320. */
  1321. #if 0
  1322. if (time_after(cic->last_end_request, cic->last_queue))
  1323. elapsed = jiffies - cic->last_end_request;
  1324. else
  1325. elapsed = jiffies - cic->last_queue;
  1326. #else
  1327. elapsed = jiffies - cic->last_end_request;
  1328. #endif
  1329. ttime = min(elapsed, 2UL * cfqd->cfq_slice_idle);
  1330. cic->ttime_samples = (7*cic->ttime_samples + 256) / 8;
  1331. cic->ttime_total = (7*cic->ttime_total + 256*ttime) / 8;
  1332. cic->ttime_mean = (cic->ttime_total + 128) / cic->ttime_samples;
  1333. }
  1334. static void
  1335. cfq_update_io_seektime(struct cfq_data *cfqd, struct cfq_io_context *cic,
  1336. struct cfq_rq *crq)
  1337. {
  1338. sector_t sdist;
  1339. u64 total;
  1340. if (cic->last_request_pos < crq->request->sector)
  1341. sdist = crq->request->sector - cic->last_request_pos;
  1342. else
  1343. sdist = cic->last_request_pos - crq->request->sector;
  1344. /*
  1345. * Don't allow the seek distance to get too large from the
  1346. * odd fragment, pagein, etc
  1347. */
  1348. if (cic->seek_samples <= 60) /* second&third seek */
  1349. sdist = min(sdist, (cic->seek_mean * 4) + 2*1024*1024);
  1350. else
  1351. sdist = min(sdist, (cic->seek_mean * 4) + 2*1024*64);
  1352. cic->seek_samples = (7*cic->seek_samples + 256) / 8;
  1353. cic->seek_total = (7*cic->seek_total + (u64)256*sdist) / 8;
  1354. total = cic->seek_total + (cic->seek_samples/2);
  1355. do_div(total, cic->seek_samples);
  1356. cic->seek_mean = (sector_t)total;
  1357. }
  1358. /*
  1359. * Disable idle window if the process thinks too long or seeks so much that
  1360. * it doesn't matter
  1361. */
  1362. static void
  1363. cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq,
  1364. struct cfq_io_context *cic)
  1365. {
  1366. int enable_idle = cfq_cfqq_idle_window(cfqq);
  1367. if (!cic->ioc->task || !cfqd->cfq_slice_idle || cfqd->hw_tag)
  1368. enable_idle = 0;
  1369. else if (sample_valid(cic->ttime_samples)) {
  1370. if (cic->ttime_mean > cfqd->cfq_slice_idle)
  1371. enable_idle = 0;
  1372. else
  1373. enable_idle = 1;
  1374. }
  1375. if (enable_idle)
  1376. cfq_mark_cfqq_idle_window(cfqq);
  1377. else
  1378. cfq_clear_cfqq_idle_window(cfqq);
  1379. }
  1380. /*
  1381. * Check if new_cfqq should preempt the currently active queue. Return 0 for
  1382. * no or if we aren't sure, a 1 will cause a preempt.
  1383. */
  1384. static int
  1385. cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
  1386. struct cfq_rq *crq)
  1387. {
  1388. struct cfq_queue *cfqq = cfqd->active_queue;
  1389. if (cfq_class_idle(new_cfqq))
  1390. return 0;
  1391. if (!cfqq)
  1392. return 1;
  1393. if (cfq_class_idle(cfqq))
  1394. return 1;
  1395. if (!cfq_cfqq_wait_request(new_cfqq))
  1396. return 0;
  1397. /*
  1398. * if it doesn't have slice left, forget it
  1399. */
  1400. if (new_cfqq->slice_left < cfqd->cfq_slice_idle)
  1401. return 0;
  1402. if (cfq_crq_is_sync(crq) && !cfq_cfqq_sync(cfqq))
  1403. return 1;
  1404. return 0;
  1405. }
  1406. /*
  1407. * cfqq preempts the active queue. if we allowed preempt with no slice left,
  1408. * let it have half of its nominal slice.
  1409. */
  1410. static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  1411. {
  1412. struct cfq_queue *__cfqq, *next;
  1413. list_for_each_entry_safe(__cfqq, next, &cfqd->cur_rr, cfq_list)
  1414. cfq_resort_rr_list(__cfqq, 1);
  1415. if (!cfqq->slice_left)
  1416. cfqq->slice_left = cfq_prio_to_slice(cfqd, cfqq) / 2;
  1417. cfqq->slice_end = cfqq->slice_left + jiffies;
  1418. __cfq_slice_expired(cfqd, cfqq, 1);
  1419. __cfq_set_active_queue(cfqd, cfqq);
  1420. }
  1421. /*
  1422. * should really be a ll_rw_blk.c helper
  1423. */
  1424. static void cfq_start_queueing(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  1425. {
  1426. request_queue_t *q = cfqd->queue;
  1427. if (!blk_queue_plugged(q))
  1428. q->request_fn(q);
  1429. else
  1430. __generic_unplug_device(q);
  1431. }
  1432. /*
  1433. * Called when a new fs request (crq) is added (to cfqq). Check if there's
  1434. * something we should do about it
  1435. */
  1436. static void
  1437. cfq_crq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
  1438. struct cfq_rq *crq)
  1439. {
  1440. struct cfq_io_context *cic;
  1441. cfqq->next_crq = cfq_choose_req(cfqd, cfqq->next_crq, crq);
  1442. cic = crq->io_context;
  1443. /*
  1444. * we never wait for an async request and we don't allow preemption
  1445. * of an async request. so just return early
  1446. */
  1447. if (!cfq_crq_is_sync(crq)) {
  1448. /*
  1449. * sync process issued an async request, if it's waiting
  1450. * then expire it and kick rq handling.
  1451. */
  1452. if (cic == cfqd->active_cic &&
  1453. del_timer(&cfqd->idle_slice_timer)) {
  1454. cfq_slice_expired(cfqd, 0);
  1455. cfq_start_queueing(cfqd, cfqq);
  1456. }
  1457. return;
  1458. }
  1459. cfq_update_io_thinktime(cfqd, cic);
  1460. cfq_update_io_seektime(cfqd, cic, crq);
  1461. cfq_update_idle_window(cfqd, cfqq, cic);
  1462. cic->last_queue = jiffies;
  1463. cic->last_request_pos = crq->request->sector + crq->request->nr_sectors;
  1464. if (cfqq == cfqd->active_queue) {
  1465. /*
  1466. * if we are waiting for a request for this queue, let it rip
  1467. * immediately and flag that we must not expire this queue
  1468. * just now
  1469. */
  1470. if (cfq_cfqq_wait_request(cfqq)) {
  1471. cfq_mark_cfqq_must_dispatch(cfqq);
  1472. del_timer(&cfqd->idle_slice_timer);
  1473. cfq_start_queueing(cfqd, cfqq);
  1474. }
  1475. } else if (cfq_should_preempt(cfqd, cfqq, crq)) {
  1476. /*
  1477. * not the active queue - expire current slice if it is
  1478. * idle and has expired it's mean thinktime or this new queue
  1479. * has some old slice time left and is of higher priority
  1480. */
  1481. cfq_preempt_queue(cfqd, cfqq);
  1482. cfq_mark_cfqq_must_dispatch(cfqq);
  1483. cfq_start_queueing(cfqd, cfqq);
  1484. }
  1485. }
  1486. static void cfq_insert_request(request_queue_t *q, struct request *rq)
  1487. {
  1488. struct cfq_data *cfqd = q->elevator->elevator_data;
  1489. struct cfq_rq *crq = RQ_DATA(rq);
  1490. struct cfq_queue *cfqq = crq->cfq_queue;
  1491. cfq_init_prio_data(cfqq);
  1492. cfq_add_crq_rb(crq);
  1493. list_add_tail(&rq->queuelist, &cfqq->fifo);
  1494. if (rq_mergeable(rq))
  1495. cfq_add_crq_hash(cfqd, crq);
  1496. cfq_crq_enqueued(cfqd, cfqq, crq);
  1497. }
  1498. static void cfq_completed_request(request_queue_t *q, struct request *rq)
  1499. {
  1500. struct cfq_rq *crq = RQ_DATA(rq);
  1501. struct cfq_queue *cfqq = crq->cfq_queue;
  1502. struct cfq_data *cfqd = cfqq->cfqd;
  1503. const int sync = cfq_crq_is_sync(crq);
  1504. unsigned long now;
  1505. now = jiffies;
  1506. WARN_ON(!cfqd->rq_in_driver);
  1507. WARN_ON(!cfqq->on_dispatch[sync]);
  1508. cfqd->rq_in_driver--;
  1509. cfqq->on_dispatch[sync]--;
  1510. if (!cfq_class_idle(cfqq))
  1511. cfqd->last_end_request = now;
  1512. if (!cfq_cfqq_dispatched(cfqq)) {
  1513. if (cfq_cfqq_on_rr(cfqq)) {
  1514. cfqq->service_last = now;
  1515. cfq_resort_rr_list(cfqq, 0);
  1516. }
  1517. cfq_schedule_dispatch(cfqd);
  1518. }
  1519. if (cfq_crq_is_sync(crq))
  1520. crq->io_context->last_end_request = now;
  1521. }
  1522. static struct request *
  1523. cfq_former_request(request_queue_t *q, struct request *rq)
  1524. {
  1525. struct cfq_rq *crq = RQ_DATA(rq);
  1526. struct rb_node *rbprev = rb_prev(&crq->rb_node);
  1527. if (rbprev)
  1528. return rb_entry_crq(rbprev)->request;
  1529. return NULL;
  1530. }
  1531. static struct request *
  1532. cfq_latter_request(request_queue_t *q, struct request *rq)
  1533. {
  1534. struct cfq_rq *crq = RQ_DATA(rq);
  1535. struct rb_node *rbnext = rb_next(&crq->rb_node);
  1536. if (rbnext)
  1537. return rb_entry_crq(rbnext)->request;
  1538. return NULL;
  1539. }
  1540. /*
  1541. * we temporarily boost lower priority queues if they are holding fs exclusive
  1542. * resources. they are boosted to normal prio (CLASS_BE/4)
  1543. */
  1544. static void cfq_prio_boost(struct cfq_queue *cfqq)
  1545. {
  1546. const int ioprio_class = cfqq->ioprio_class;
  1547. const int ioprio = cfqq->ioprio;
  1548. if (has_fs_excl()) {
  1549. /*
  1550. * boost idle prio on transactions that would lock out other
  1551. * users of the filesystem
  1552. */
  1553. if (cfq_class_idle(cfqq))
  1554. cfqq->ioprio_class = IOPRIO_CLASS_BE;
  1555. if (cfqq->ioprio > IOPRIO_NORM)
  1556. cfqq->ioprio = IOPRIO_NORM;
  1557. } else {
  1558. /*
  1559. * check if we need to unboost the queue
  1560. */
  1561. if (cfqq->ioprio_class != cfqq->org_ioprio_class)
  1562. cfqq->ioprio_class = cfqq->org_ioprio_class;
  1563. if (cfqq->ioprio != cfqq->org_ioprio)
  1564. cfqq->ioprio = cfqq->org_ioprio;
  1565. }
  1566. /*
  1567. * refile between round-robin lists if we moved the priority class
  1568. */
  1569. if ((ioprio_class != cfqq->ioprio_class || ioprio != cfqq->ioprio) &&
  1570. cfq_cfqq_on_rr(cfqq))
  1571. cfq_resort_rr_list(cfqq, 0);
  1572. }
  1573. static inline int
  1574. __cfq_may_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq,
  1575. struct task_struct *task, int rw)
  1576. {
  1577. #if 1
  1578. if ((cfq_cfqq_wait_request(cfqq) || cfq_cfqq_must_alloc(cfqq)) &&
  1579. !cfq_cfqq_must_alloc_slice(cfqq)) {
  1580. cfq_mark_cfqq_must_alloc_slice(cfqq);
  1581. return ELV_MQUEUE_MUST;
  1582. }
  1583. return ELV_MQUEUE_MAY;
  1584. #else
  1585. if (!cfqq || task->flags & PF_MEMALLOC)
  1586. return ELV_MQUEUE_MAY;
  1587. if (!cfqq->allocated[rw] || cfq_cfqq_must_alloc(cfqq)) {
  1588. if (cfq_cfqq_wait_request(cfqq))
  1589. return ELV_MQUEUE_MUST;
  1590. /*
  1591. * only allow 1 ELV_MQUEUE_MUST per slice, otherwise we
  1592. * can quickly flood the queue with writes from a single task
  1593. */
  1594. if (rw == READ || !cfq_cfqq_must_alloc_slice(cfqq)) {
  1595. cfq_mark_cfqq_must_alloc_slice(cfqq);
  1596. return ELV_MQUEUE_MUST;
  1597. }
  1598. return ELV_MQUEUE_MAY;
  1599. }
  1600. if (cfq_class_idle(cfqq))
  1601. return ELV_MQUEUE_NO;
  1602. if (cfqq->allocated[rw] >= cfqd->max_queued) {
  1603. struct io_context *ioc = get_io_context(GFP_ATOMIC);
  1604. int ret = ELV_MQUEUE_NO;
  1605. if (ioc && ioc->nr_batch_requests)
  1606. ret = ELV_MQUEUE_MAY;
  1607. put_io_context(ioc);
  1608. return ret;
  1609. }
  1610. return ELV_MQUEUE_MAY;
  1611. #endif
  1612. }
  1613. static int cfq_may_queue(request_queue_t *q, int rw, struct bio *bio)
  1614. {
  1615. struct cfq_data *cfqd = q->elevator->elevator_data;
  1616. struct task_struct *tsk = current;
  1617. struct cfq_queue *cfqq;
  1618. /*
  1619. * don't force setup of a queue from here, as a call to may_queue
  1620. * does not necessarily imply that a request actually will be queued.
  1621. * so just lookup a possibly existing queue, or return 'may queue'
  1622. * if that fails
  1623. */
  1624. cfqq = cfq_find_cfq_hash(cfqd, cfq_queue_pid(tsk, rw), tsk->ioprio);
  1625. if (cfqq) {
  1626. cfq_init_prio_data(cfqq);
  1627. cfq_prio_boost(cfqq);
  1628. return __cfq_may_queue(cfqd, cfqq, tsk, rw);
  1629. }
  1630. return ELV_MQUEUE_MAY;
  1631. }
  1632. static void cfq_check_waiters(request_queue_t *q, struct cfq_queue *cfqq)
  1633. {
  1634. struct cfq_data *cfqd = q->elevator->elevator_data;
  1635. struct request_list *rl = &q->rq;
  1636. if (cfqq->allocated[READ] <= cfqd->max_queued || cfqd->rq_starved) {
  1637. smp_mb();
  1638. if (waitqueue_active(&rl->wait[READ]))
  1639. wake_up(&rl->wait[READ]);
  1640. }
  1641. if (cfqq->allocated[WRITE] <= cfqd->max_queued || cfqd->rq_starved) {
  1642. smp_mb();
  1643. if (waitqueue_active(&rl->wait[WRITE]))
  1644. wake_up(&rl->wait[WRITE]);
  1645. }
  1646. }
  1647. /*
  1648. * queue lock held here
  1649. */
  1650. static void cfq_put_request(request_queue_t *q, struct request *rq)
  1651. {
  1652. struct cfq_data *cfqd = q->elevator->elevator_data;
  1653. struct cfq_rq *crq = RQ_DATA(rq);
  1654. if (crq) {
  1655. struct cfq_queue *cfqq = crq->cfq_queue;
  1656. const int rw = rq_data_dir(rq);
  1657. BUG_ON(!cfqq->allocated[rw]);
  1658. cfqq->allocated[rw]--;
  1659. put_io_context(crq->io_context->ioc);
  1660. mempool_free(crq, cfqd->crq_pool);
  1661. rq->elevator_private = NULL;
  1662. cfq_check_waiters(q, cfqq);
  1663. cfq_put_queue(cfqq);
  1664. }
  1665. }
  1666. /*
  1667. * Allocate cfq data structures associated with this request.
  1668. */
  1669. static int
  1670. cfq_set_request(request_queue_t *q, struct request *rq, struct bio *bio,
  1671. gfp_t gfp_mask)
  1672. {
  1673. struct cfq_data *cfqd = q->elevator->elevator_data;
  1674. struct task_struct *tsk = current;
  1675. struct cfq_io_context *cic;
  1676. const int rw = rq_data_dir(rq);
  1677. pid_t key = cfq_queue_pid(tsk, rw);
  1678. struct cfq_queue *cfqq;
  1679. struct cfq_rq *crq;
  1680. unsigned long flags;
  1681. int is_sync = key != CFQ_KEY_ASYNC;
  1682. might_sleep_if(gfp_mask & __GFP_WAIT);
  1683. cic = cfq_get_io_context(cfqd, gfp_mask);
  1684. spin_lock_irqsave(q->queue_lock, flags);
  1685. if (!cic)
  1686. goto queue_fail;
  1687. if (!cic->cfqq[is_sync]) {
  1688. cfqq = cfq_get_queue(cfqd, key, tsk, gfp_mask);
  1689. if (!cfqq)
  1690. goto queue_fail;
  1691. cic->cfqq[is_sync] = cfqq;
  1692. } else
  1693. cfqq = cic->cfqq[is_sync];
  1694. cfqq->allocated[rw]++;
  1695. cfq_clear_cfqq_must_alloc(cfqq);
  1696. cfqd->rq_starved = 0;
  1697. atomic_inc(&cfqq->ref);
  1698. spin_unlock_irqrestore(q->queue_lock, flags);
  1699. crq = mempool_alloc(cfqd->crq_pool, gfp_mask);
  1700. if (crq) {
  1701. RB_CLEAR(&crq->rb_node);
  1702. crq->rb_key = 0;
  1703. crq->request = rq;
  1704. INIT_HLIST_NODE(&crq->hash);
  1705. crq->cfq_queue = cfqq;
  1706. crq->io_context = cic;
  1707. if (is_sync)
  1708. cfq_mark_crq_is_sync(crq);
  1709. else
  1710. cfq_clear_crq_is_sync(crq);
  1711. rq->elevator_private = crq;
  1712. return 0;
  1713. }
  1714. spin_lock_irqsave(q->queue_lock, flags);
  1715. cfqq->allocated[rw]--;
  1716. if (!(cfqq->allocated[0] + cfqq->allocated[1]))
  1717. cfq_mark_cfqq_must_alloc(cfqq);
  1718. cfq_put_queue(cfqq);
  1719. queue_fail:
  1720. if (cic)
  1721. put_io_context(cic->ioc);
  1722. /*
  1723. * mark us rq allocation starved. we need to kickstart the process
  1724. * ourselves if there are no pending requests that can do it for us.
  1725. * that would be an extremely rare OOM situation
  1726. */
  1727. cfqd->rq_starved = 1;
  1728. cfq_schedule_dispatch(cfqd);
  1729. spin_unlock_irqrestore(q->queue_lock, flags);
  1730. return 1;
  1731. }
  1732. static void cfq_kick_queue(void *data)
  1733. {
  1734. request_queue_t *q = data;
  1735. struct cfq_data *cfqd = q->elevator->elevator_data;
  1736. unsigned long flags;
  1737. spin_lock_irqsave(q->queue_lock, flags);
  1738. if (cfqd->rq_starved) {
  1739. struct request_list *rl = &q->rq;
  1740. /*
  1741. * we aren't guaranteed to get a request after this, but we
  1742. * have to be opportunistic
  1743. */
  1744. smp_mb();
  1745. if (waitqueue_active(&rl->wait[READ]))
  1746. wake_up(&rl->wait[READ]);
  1747. if (waitqueue_active(&rl->wait[WRITE]))
  1748. wake_up(&rl->wait[WRITE]);
  1749. }
  1750. blk_remove_plug(q);
  1751. q->request_fn(q);
  1752. spin_unlock_irqrestore(q->queue_lock, flags);
  1753. }
  1754. /*
  1755. * Timer running if the active_queue is currently idling inside its time slice
  1756. */
  1757. static void cfq_idle_slice_timer(unsigned long data)
  1758. {
  1759. struct cfq_data *cfqd = (struct cfq_data *) data;
  1760. struct cfq_queue *cfqq;
  1761. unsigned long flags;
  1762. spin_lock_irqsave(cfqd->queue->queue_lock, flags);
  1763. if ((cfqq = cfqd->active_queue) != NULL) {
  1764. unsigned long now = jiffies;
  1765. /*
  1766. * expired
  1767. */
  1768. if (time_after(now, cfqq->slice_end))
  1769. goto expire;
  1770. /*
  1771. * only expire and reinvoke request handler, if there are
  1772. * other queues with pending requests
  1773. */
  1774. if (!cfqd->busy_queues) {
  1775. cfqd->idle_slice_timer.expires = min(now + cfqd->cfq_slice_idle, cfqq->slice_end);
  1776. add_timer(&cfqd->idle_slice_timer);
  1777. goto out_cont;
  1778. }
  1779. /*
  1780. * not expired and it has a request pending, let it dispatch
  1781. */
  1782. if (!RB_EMPTY(&cfqq->sort_list)) {
  1783. cfq_mark_cfqq_must_dispatch(cfqq);
  1784. goto out_kick;
  1785. }
  1786. }
  1787. expire:
  1788. cfq_slice_expired(cfqd, 0);
  1789. out_kick:
  1790. cfq_schedule_dispatch(cfqd);
  1791. out_cont:
  1792. spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
  1793. }
  1794. /*
  1795. * Timer running if an idle class queue is waiting for service
  1796. */
  1797. static void cfq_idle_class_timer(unsigned long data)
  1798. {
  1799. struct cfq_data *cfqd = (struct cfq_data *) data;
  1800. unsigned long flags, end;
  1801. spin_lock_irqsave(cfqd->queue->queue_lock, flags);
  1802. /*
  1803. * race with a non-idle queue, reset timer
  1804. */
  1805. end = cfqd->last_end_request + CFQ_IDLE_GRACE;
  1806. if (!time_after_eq(jiffies, end))
  1807. mod_timer(&cfqd->idle_class_timer, end);
  1808. else
  1809. cfq_schedule_dispatch(cfqd);
  1810. spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
  1811. }
  1812. static void cfq_shutdown_timer_wq(struct cfq_data *cfqd)
  1813. {
  1814. del_timer_sync(&cfqd->idle_slice_timer);
  1815. del_timer_sync(&cfqd->idle_class_timer);
  1816. blk_sync_queue(cfqd->queue);
  1817. }
  1818. static void cfq_exit_queue(elevator_t *e)
  1819. {
  1820. struct cfq_data *cfqd = e->elevator_data;
  1821. request_queue_t *q = cfqd->queue;
  1822. cfq_shutdown_timer_wq(cfqd);
  1823. spin_lock(&cfq_exit_lock);
  1824. spin_lock_irq(q->queue_lock);
  1825. if (cfqd->active_queue)
  1826. __cfq_slice_expired(cfqd, cfqd->active_queue, 0);
  1827. while (!list_empty(&cfqd->cic_list)) {
  1828. struct cfq_io_context *cic = list_entry(cfqd->cic_list.next,
  1829. struct cfq_io_context,
  1830. queue_list);
  1831. if (cic->cfqq[ASYNC]) {
  1832. cfq_put_queue(cic->cfqq[ASYNC]);
  1833. cic->cfqq[ASYNC] = NULL;
  1834. }
  1835. if (cic->cfqq[SYNC]) {
  1836. cfq_put_queue(cic->cfqq[SYNC]);
  1837. cic->cfqq[SYNC] = NULL;
  1838. }
  1839. cic->key = NULL;
  1840. list_del_init(&cic->queue_list);
  1841. }
  1842. spin_unlock_irq(q->queue_lock);
  1843. spin_unlock(&cfq_exit_lock);
  1844. cfq_shutdown_timer_wq(cfqd);
  1845. mempool_destroy(cfqd->crq_pool);
  1846. kfree(cfqd->crq_hash);
  1847. kfree(cfqd->cfq_hash);
  1848. kfree(cfqd);
  1849. }
  1850. static void *cfq_init_queue(request_queue_t *q, elevator_t *e)
  1851. {
  1852. struct cfq_data *cfqd;
  1853. int i;
  1854. cfqd = kmalloc(sizeof(*cfqd), GFP_KERNEL);
  1855. if (!cfqd)
  1856. return NULL;
  1857. memset(cfqd, 0, sizeof(*cfqd));
  1858. for (i = 0; i < CFQ_PRIO_LISTS; i++)
  1859. INIT_LIST_HEAD(&cfqd->rr_list[i]);
  1860. INIT_LIST_HEAD(&cfqd->busy_rr);
  1861. INIT_LIST_HEAD(&cfqd->cur_rr);
  1862. INIT_LIST_HEAD(&cfqd->idle_rr);
  1863. INIT_LIST_HEAD(&cfqd->empty_list);
  1864. INIT_LIST_HEAD(&cfqd->cic_list);
  1865. cfqd->crq_hash = kmalloc(sizeof(struct hlist_head) * CFQ_MHASH_ENTRIES, GFP_KERNEL);
  1866. if (!cfqd->crq_hash)
  1867. goto out_crqhash;
  1868. cfqd->cfq_hash = kmalloc(sizeof(struct hlist_head) * CFQ_QHASH_ENTRIES, GFP_KERNEL);
  1869. if (!cfqd->cfq_hash)
  1870. goto out_cfqhash;
  1871. cfqd->crq_pool = mempool_create_slab_pool(BLKDEV_MIN_RQ, crq_pool);
  1872. if (!cfqd->crq_pool)
  1873. goto out_crqpool;
  1874. for (i = 0; i < CFQ_MHASH_ENTRIES; i++)
  1875. INIT_HLIST_HEAD(&cfqd->crq_hash[i]);
  1876. for (i = 0; i < CFQ_QHASH_ENTRIES; i++)
  1877. INIT_HLIST_HEAD(&cfqd->cfq_hash[i]);
  1878. cfqd->queue = q;
  1879. cfqd->max_queued = q->nr_requests / 4;
  1880. q->nr_batching = cfq_queued;
  1881. init_timer(&cfqd->idle_slice_timer);
  1882. cfqd->idle_slice_timer.function = cfq_idle_slice_timer;
  1883. cfqd->idle_slice_timer.data = (unsigned long) cfqd;
  1884. init_timer(&cfqd->idle_class_timer);
  1885. cfqd->idle_class_timer.function = cfq_idle_class_timer;
  1886. cfqd->idle_class_timer.data = (unsigned long) cfqd;
  1887. INIT_WORK(&cfqd->unplug_work, cfq_kick_queue, q);
  1888. cfqd->cfq_queued = cfq_queued;
  1889. cfqd->cfq_quantum = cfq_quantum;
  1890. cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0];
  1891. cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1];
  1892. cfqd->cfq_back_max = cfq_back_max;
  1893. cfqd->cfq_back_penalty = cfq_back_penalty;
  1894. cfqd->cfq_slice[0] = cfq_slice_async;
  1895. cfqd->cfq_slice[1] = cfq_slice_sync;
  1896. cfqd->cfq_slice_async_rq = cfq_slice_async_rq;
  1897. cfqd->cfq_slice_idle = cfq_slice_idle;
  1898. return cfqd;
  1899. out_crqpool:
  1900. kfree(cfqd->cfq_hash);
  1901. out_cfqhash:
  1902. kfree(cfqd->crq_hash);
  1903. out_crqhash:
  1904. kfree(cfqd);
  1905. return NULL;
  1906. }
  1907. static void cfq_slab_kill(void)
  1908. {
  1909. if (crq_pool)
  1910. kmem_cache_destroy(crq_pool);
  1911. if (cfq_pool)
  1912. kmem_cache_destroy(cfq_pool);
  1913. if (cfq_ioc_pool)
  1914. kmem_cache_destroy(cfq_ioc_pool);
  1915. }
  1916. static int __init cfq_slab_setup(void)
  1917. {
  1918. crq_pool = kmem_cache_create("crq_pool", sizeof(struct cfq_rq), 0, 0,
  1919. NULL, NULL);
  1920. if (!crq_pool)
  1921. goto fail;
  1922. cfq_pool = kmem_cache_create("cfq_pool", sizeof(struct cfq_queue), 0, 0,
  1923. NULL, NULL);
  1924. if (!cfq_pool)
  1925. goto fail;
  1926. cfq_ioc_pool = kmem_cache_create("cfq_ioc_pool",
  1927. sizeof(struct cfq_io_context), 0, 0, NULL, NULL);
  1928. if (!cfq_ioc_pool)
  1929. goto fail;
  1930. return 0;
  1931. fail:
  1932. cfq_slab_kill();
  1933. return -ENOMEM;
  1934. }
  1935. /*
  1936. * sysfs parts below -->
  1937. */
  1938. static ssize_t
  1939. cfq_var_show(unsigned int var, char *page)
  1940. {
  1941. return sprintf(page, "%d\n", var);
  1942. }
  1943. static ssize_t
  1944. cfq_var_store(unsigned int *var, const char *page, size_t count)
  1945. {
  1946. char *p = (char *) page;
  1947. *var = simple_strtoul(p, &p, 10);
  1948. return count;
  1949. }
  1950. #define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
  1951. static ssize_t __FUNC(elevator_t *e, char *page) \
  1952. { \
  1953. struct cfq_data *cfqd = e->elevator_data; \
  1954. unsigned int __data = __VAR; \
  1955. if (__CONV) \
  1956. __data = jiffies_to_msecs(__data); \
  1957. return cfq_var_show(__data, (page)); \
  1958. }
  1959. SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0);
  1960. SHOW_FUNCTION(cfq_queued_show, cfqd->cfq_queued, 0);
  1961. SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1);
  1962. SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1);
  1963. SHOW_FUNCTION(cfq_back_seek_max_show, cfqd->cfq_back_max, 0);
  1964. SHOW_FUNCTION(cfq_back_seek_penalty_show, cfqd->cfq_back_penalty, 0);
  1965. SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1);
  1966. SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1);
  1967. SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1);
  1968. SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0);
  1969. #undef SHOW_FUNCTION
  1970. #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
  1971. static ssize_t __FUNC(elevator_t *e, const char *page, size_t count) \
  1972. { \
  1973. struct cfq_data *cfqd = e->elevator_data; \
  1974. unsigned int __data; \
  1975. int ret = cfq_var_store(&__data, (page), count); \
  1976. if (__data < (MIN)) \
  1977. __data = (MIN); \
  1978. else if (__data > (MAX)) \
  1979. __data = (MAX); \
  1980. if (__CONV) \
  1981. *(__PTR) = msecs_to_jiffies(__data); \
  1982. else \
  1983. *(__PTR) = __data; \
  1984. return ret; \
  1985. }
  1986. STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0);
  1987. STORE_FUNCTION(cfq_queued_store, &cfqd->cfq_queued, 1, UINT_MAX, 0);
  1988. STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1, UINT_MAX, 1);
  1989. STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1, UINT_MAX, 1);
  1990. STORE_FUNCTION(cfq_back_seek_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0);
  1991. STORE_FUNCTION(cfq_back_seek_penalty_store, &cfqd->cfq_back_penalty, 1, UINT_MAX, 0);
  1992. STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1);
  1993. STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1);
  1994. STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1);
  1995. STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1, UINT_MAX, 0);
  1996. #undef STORE_FUNCTION
  1997. #define CFQ_ATTR(name) \
  1998. __ATTR(name, S_IRUGO|S_IWUSR, cfq_##name##_show, cfq_##name##_store)
  1999. static struct elv_fs_entry cfq_attrs[] = {
  2000. CFQ_ATTR(quantum),
  2001. CFQ_ATTR(queued),
  2002. CFQ_ATTR(fifo_expire_sync),
  2003. CFQ_ATTR(fifo_expire_async),
  2004. CFQ_ATTR(back_seek_max),
  2005. CFQ_ATTR(back_seek_penalty),
  2006. CFQ_ATTR(slice_sync),
  2007. CFQ_ATTR(slice_async),
  2008. CFQ_ATTR(slice_async_rq),
  2009. CFQ_ATTR(slice_idle),
  2010. __ATTR_NULL
  2011. };
  2012. static struct elevator_type iosched_cfq = {
  2013. .ops = {
  2014. .elevator_merge_fn = cfq_merge,
  2015. .elevator_merged_fn = cfq_merged_request,
  2016. .elevator_merge_req_fn = cfq_merged_requests,
  2017. .elevator_dispatch_fn = cfq_dispatch_requests,
  2018. .elevator_add_req_fn = cfq_insert_request,
  2019. .elevator_activate_req_fn = cfq_activate_request,
  2020. .elevator_deactivate_req_fn = cfq_deactivate_request,
  2021. .elevator_queue_empty_fn = cfq_queue_empty,
  2022. .elevator_completed_req_fn = cfq_completed_request,
  2023. .elevator_former_req_fn = cfq_former_request,
  2024. .elevator_latter_req_fn = cfq_latter_request,
  2025. .elevator_set_req_fn = cfq_set_request,
  2026. .elevator_put_req_fn = cfq_put_request,
  2027. .elevator_may_queue_fn = cfq_may_queue,
  2028. .elevator_init_fn = cfq_init_queue,
  2029. .elevator_exit_fn = cfq_exit_queue,
  2030. .trim = cfq_trim,
  2031. },
  2032. .elevator_attrs = cfq_attrs,
  2033. .elevator_name = "cfq",
  2034. .elevator_owner = THIS_MODULE,
  2035. };
  2036. static int __init cfq_init(void)
  2037. {
  2038. int ret;
  2039. /*
  2040. * could be 0 on HZ < 1000 setups
  2041. */
  2042. if (!cfq_slice_async)
  2043. cfq_slice_async = 1;
  2044. if (!cfq_slice_idle)
  2045. cfq_slice_idle = 1;
  2046. if (cfq_slab_setup())
  2047. return -ENOMEM;
  2048. ret = elv_register(&iosched_cfq);
  2049. if (ret)
  2050. cfq_slab_kill();
  2051. return ret;
  2052. }
  2053. static void __exit cfq_exit(void)
  2054. {
  2055. DECLARE_COMPLETION(all_gone);
  2056. elv_unregister(&iosched_cfq);
  2057. ioc_gone = &all_gone;
  2058. /* ioc_gone's update must be visible before reading ioc_count */
  2059. smp_wmb();
  2060. if (atomic_read(&ioc_count))
  2061. wait_for_completion(ioc_gone);
  2062. synchronize_rcu();
  2063. cfq_slab_kill();
  2064. }
  2065. module_init(cfq_init);
  2066. module_exit(cfq_exit);
  2067. MODULE_AUTHOR("Jens Axboe");
  2068. MODULE_LICENSE("GPL");
  2069. MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");