cfq-iosched.c 58 KB

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