cfq-iosched.c 60 KB

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