cfq-iosched.c 58 KB

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