cfq-iosched.c 62 KB

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