cfq-iosched.c 56 KB

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