cfq-iosched.c 58 KB

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