cfq-iosched.c 58 KB

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