cfq-iosched.c 58 KB

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