cfq-iosched.c 62 KB

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