cfq-iosched.c 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464
  1. /*
  2. * linux/drivers/block/cfq-iosched.c
  3. *
  4. * CFQ, or complete fairness queueing, disk scheduler.
  5. *
  6. * Based on ideas from a previously unfinished io
  7. * scheduler (round robin per-process disk scheduling) and Andrea Arcangeli.
  8. *
  9. * Copyright (C) 2003 Jens Axboe <axboe@suse.de>
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/fs.h>
  13. #include <linux/blkdev.h>
  14. #include <linux/elevator.h>
  15. #include <linux/bio.h>
  16. #include <linux/config.h>
  17. #include <linux/module.h>
  18. #include <linux/slab.h>
  19. #include <linux/init.h>
  20. #include <linux/compiler.h>
  21. #include <linux/hash.h>
  22. #include <linux/rbtree.h>
  23. #include <linux/mempool.h>
  24. #include <linux/ioprio.h>
  25. #include <linux/writeback.h>
  26. /*
  27. * tunables
  28. */
  29. static int cfq_quantum = 4; /* max queue in one round of service */
  30. static int cfq_queued = 8; /* minimum rq allocate limit per-queue*/
  31. static int cfq_fifo_expire[2] = { HZ / 4, HZ / 8 };
  32. static int cfq_back_max = 16 * 1024; /* maximum backwards seek, in KiB */
  33. static int cfq_back_penalty = 2; /* penalty of a backwards seek */
  34. static int cfq_slice_sync = HZ / 10;
  35. static int cfq_slice_async = HZ / 25;
  36. static int cfq_slice_async_rq = 2;
  37. static int cfq_slice_idle = HZ / 100;
  38. #define CFQ_IDLE_GRACE (HZ / 10)
  39. #define CFQ_SLICE_SCALE (5)
  40. #define CFQ_KEY_ASYNC (0)
  41. #define CFQ_KEY_ANY (0xffff)
  42. /*
  43. * disable queueing at the driver/hardware level
  44. */
  45. static int cfq_max_depth = 2;
  46. /*
  47. * for the hash of cfqq inside the cfqd
  48. */
  49. #define CFQ_QHASH_SHIFT 6
  50. #define CFQ_QHASH_ENTRIES (1 << CFQ_QHASH_SHIFT)
  51. #define list_entry_qhash(entry) hlist_entry((entry), struct cfq_queue, cfq_hash)
  52. /*
  53. * for the hash of crq inside the cfqq
  54. */
  55. #define CFQ_MHASH_SHIFT 6
  56. #define CFQ_MHASH_BLOCK(sec) ((sec) >> 3)
  57. #define CFQ_MHASH_ENTRIES (1 << CFQ_MHASH_SHIFT)
  58. #define CFQ_MHASH_FN(sec) hash_long(CFQ_MHASH_BLOCK(sec), CFQ_MHASH_SHIFT)
  59. #define rq_hash_key(rq) ((rq)->sector + (rq)->nr_sectors)
  60. #define list_entry_hash(ptr) hlist_entry((ptr), struct cfq_rq, hash)
  61. #define list_entry_cfqq(ptr) list_entry((ptr), struct cfq_queue, cfq_list)
  62. #define list_entry_fifo(ptr) list_entry((ptr), struct request, queuelist)
  63. #define RQ_DATA(rq) (rq)->elevator_private
  64. /*
  65. * rb-tree defines
  66. */
  67. #define RB_NONE (2)
  68. #define RB_EMPTY(node) ((node)->rb_node == NULL)
  69. #define RB_CLEAR_COLOR(node) (node)->rb_color = RB_NONE
  70. #define RB_CLEAR(node) do { \
  71. (node)->rb_parent = NULL; \
  72. RB_CLEAR_COLOR((node)); \
  73. (node)->rb_right = NULL; \
  74. (node)->rb_left = NULL; \
  75. } while (0)
  76. #define RB_CLEAR_ROOT(root) ((root)->rb_node = NULL)
  77. #define rb_entry_crq(node) rb_entry((node), struct cfq_rq, rb_node)
  78. #define rq_rb_key(rq) (rq)->sector
  79. static kmem_cache_t *crq_pool;
  80. static kmem_cache_t *cfq_pool;
  81. static kmem_cache_t *cfq_ioc_pool;
  82. #define CFQ_PRIO_LISTS IOPRIO_BE_NR
  83. #define cfq_class_idle(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_IDLE)
  84. #define cfq_class_be(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_BE)
  85. #define cfq_class_rt(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_RT)
  86. #define ASYNC (0)
  87. #define SYNC (1)
  88. #define cfq_cfqq_dispatched(cfqq) \
  89. ((cfqq)->on_dispatch[ASYNC] + (cfqq)->on_dispatch[SYNC])
  90. #define cfq_cfqq_class_sync(cfqq) ((cfqq)->key != CFQ_KEY_ASYNC)
  91. #define cfq_cfqq_sync(cfqq) \
  92. (cfq_cfqq_class_sync(cfqq) || (cfqq)->on_dispatch[SYNC])
  93. /*
  94. * Per block device queue structure
  95. */
  96. struct cfq_data {
  97. atomic_t ref;
  98. request_queue_t *queue;
  99. /*
  100. * rr list of queues with requests and the count of them
  101. */
  102. struct list_head rr_list[CFQ_PRIO_LISTS];
  103. struct list_head busy_rr;
  104. struct list_head cur_rr;
  105. struct list_head idle_rr;
  106. unsigned int busy_queues;
  107. /*
  108. * non-ordered list of empty cfqq's
  109. */
  110. struct list_head empty_list;
  111. /*
  112. * cfqq lookup hash
  113. */
  114. struct hlist_head *cfq_hash;
  115. /*
  116. * global crq hash for all queues
  117. */
  118. struct hlist_head *crq_hash;
  119. unsigned int max_queued;
  120. mempool_t *crq_pool;
  121. int rq_in_driver;
  122. /*
  123. * schedule slice state info
  124. */
  125. /*
  126. * idle window management
  127. */
  128. struct timer_list idle_slice_timer;
  129. struct work_struct unplug_work;
  130. struct cfq_queue *active_queue;
  131. struct cfq_io_context *active_cic;
  132. int cur_prio, cur_end_prio;
  133. unsigned int dispatch_slice;
  134. struct timer_list idle_class_timer;
  135. sector_t last_sector;
  136. unsigned long last_end_request;
  137. unsigned int rq_starved;
  138. /*
  139. * tunables, see top of file
  140. */
  141. unsigned int cfq_quantum;
  142. unsigned int cfq_queued;
  143. unsigned int cfq_fifo_expire[2];
  144. unsigned int cfq_back_penalty;
  145. unsigned int cfq_back_max;
  146. unsigned int cfq_slice[2];
  147. unsigned int cfq_slice_async_rq;
  148. unsigned int cfq_slice_idle;
  149. unsigned int cfq_max_depth;
  150. };
  151. /*
  152. * Per process-grouping structure
  153. */
  154. struct cfq_queue {
  155. /* reference count */
  156. atomic_t ref;
  157. /* parent cfq_data */
  158. struct cfq_data *cfqd;
  159. /* cfqq lookup hash */
  160. struct hlist_node cfq_hash;
  161. /* hash key */
  162. unsigned int key;
  163. /* on either rr or empty list of cfqd */
  164. struct list_head cfq_list;
  165. /* sorted list of pending requests */
  166. struct rb_root sort_list;
  167. /* if fifo isn't expired, next request to serve */
  168. struct cfq_rq *next_crq;
  169. /* requests queued in sort_list */
  170. int queued[2];
  171. /* currently allocated requests */
  172. int allocated[2];
  173. /* fifo list of requests in sort_list */
  174. struct list_head fifo;
  175. unsigned long slice_start;
  176. unsigned long slice_end;
  177. unsigned long slice_left;
  178. unsigned long service_last;
  179. /* number of requests that are on the dispatch list */
  180. int on_dispatch[2];
  181. /* io prio of this group */
  182. unsigned short ioprio, org_ioprio;
  183. unsigned short ioprio_class, org_ioprio_class;
  184. /* various state flags, see below */
  185. unsigned int flags;
  186. };
  187. struct cfq_rq {
  188. struct rb_node rb_node;
  189. sector_t rb_key;
  190. struct request *request;
  191. struct hlist_node hash;
  192. struct cfq_queue *cfq_queue;
  193. struct cfq_io_context *io_context;
  194. unsigned int crq_flags;
  195. };
  196. enum cfqq_state_flags {
  197. CFQ_CFQQ_FLAG_on_rr = 0,
  198. CFQ_CFQQ_FLAG_wait_request,
  199. CFQ_CFQQ_FLAG_must_alloc,
  200. CFQ_CFQQ_FLAG_must_alloc_slice,
  201. CFQ_CFQQ_FLAG_must_dispatch,
  202. CFQ_CFQQ_FLAG_fifo_expire,
  203. CFQ_CFQQ_FLAG_idle_window,
  204. CFQ_CFQQ_FLAG_prio_changed,
  205. CFQ_CFQQ_FLAG_expired,
  206. };
  207. #define CFQ_CFQQ_FNS(name) \
  208. static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq) \
  209. { \
  210. cfqq->flags |= (1 << CFQ_CFQQ_FLAG_##name); \
  211. } \
  212. static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq) \
  213. { \
  214. cfqq->flags &= ~(1 << CFQ_CFQQ_FLAG_##name); \
  215. } \
  216. static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq) \
  217. { \
  218. return (cfqq->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0; \
  219. }
  220. CFQ_CFQQ_FNS(on_rr);
  221. CFQ_CFQQ_FNS(wait_request);
  222. CFQ_CFQQ_FNS(must_alloc);
  223. CFQ_CFQQ_FNS(must_alloc_slice);
  224. CFQ_CFQQ_FNS(must_dispatch);
  225. CFQ_CFQQ_FNS(fifo_expire);
  226. CFQ_CFQQ_FNS(idle_window);
  227. CFQ_CFQQ_FNS(prio_changed);
  228. CFQ_CFQQ_FNS(expired);
  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->rq_in_driver && 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. cfq_clear_cfqq_expired(cfqq);
  614. }
  615. cfqd->active_queue = cfqq;
  616. }
  617. /*
  618. * 0
  619. * 0,1
  620. * 0,1,2
  621. * 0,1,2,3
  622. * 0,1,2,3,4
  623. * 0,1,2,3,4,5
  624. * 0,1,2,3,4,5,6
  625. * 0,1,2,3,4,5,6,7
  626. */
  627. static int cfq_get_next_prio_level(struct cfq_data *cfqd)
  628. {
  629. int prio, wrap;
  630. prio = -1;
  631. wrap = 0;
  632. do {
  633. int p;
  634. for (p = cfqd->cur_prio; p <= cfqd->cur_end_prio; p++) {
  635. if (!list_empty(&cfqd->rr_list[p])) {
  636. prio = p;
  637. break;
  638. }
  639. }
  640. if (prio != -1)
  641. break;
  642. cfqd->cur_prio = 0;
  643. if (++cfqd->cur_end_prio == CFQ_PRIO_LISTS) {
  644. cfqd->cur_end_prio = 0;
  645. if (wrap)
  646. break;
  647. wrap = 1;
  648. }
  649. } while (1);
  650. if (unlikely(prio == -1))
  651. return -1;
  652. BUG_ON(prio >= CFQ_PRIO_LISTS);
  653. list_splice_init(&cfqd->rr_list[prio], &cfqd->cur_rr);
  654. cfqd->cur_prio = prio + 1;
  655. if (cfqd->cur_prio > cfqd->cur_end_prio) {
  656. cfqd->cur_end_prio = cfqd->cur_prio;
  657. cfqd->cur_prio = 0;
  658. }
  659. if (cfqd->cur_end_prio == CFQ_PRIO_LISTS) {
  660. cfqd->cur_prio = 0;
  661. cfqd->cur_end_prio = 0;
  662. }
  663. return prio;
  664. }
  665. static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd)
  666. {
  667. struct cfq_queue *cfqq;
  668. /*
  669. * if current queue is expired but not done with its requests yet,
  670. * wait for that to happen
  671. */
  672. if ((cfqq = cfqd->active_queue) != NULL) {
  673. if (cfq_cfqq_expired(cfqq) && cfq_cfqq_dispatched(cfqq))
  674. return NULL;
  675. }
  676. /*
  677. * if current list is non-empty, grab first entry. if it is empty,
  678. * get next prio level and grab first entry then if any are spliced
  679. */
  680. if (!list_empty(&cfqd->cur_rr) || cfq_get_next_prio_level(cfqd) != -1)
  681. cfqq = list_entry_cfqq(cfqd->cur_rr.next);
  682. /*
  683. * if we have idle queues and no rt or be queues had pending
  684. * requests, either allow immediate service if the grace period
  685. * has passed or arm the idle grace timer
  686. */
  687. if (!cfqq && !list_empty(&cfqd->idle_rr)) {
  688. unsigned long end = cfqd->last_end_request + CFQ_IDLE_GRACE;
  689. if (time_after_eq(jiffies, end))
  690. cfqq = list_entry_cfqq(cfqd->idle_rr.next);
  691. else
  692. mod_timer(&cfqd->idle_class_timer, end);
  693. }
  694. __cfq_set_active_queue(cfqd, cfqq);
  695. return cfqq;
  696. }
  697. /*
  698. * current cfqq expired its slice (or was too idle), select new one
  699. */
  700. static void
  701. __cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq,
  702. int preempted)
  703. {
  704. unsigned long now = jiffies;
  705. if (cfq_cfqq_wait_request(cfqq))
  706. del_timer(&cfqd->idle_slice_timer);
  707. if (!preempted && !cfq_cfqq_dispatched(cfqq))
  708. cfqq->service_last = now;
  709. cfq_clear_cfqq_must_dispatch(cfqq);
  710. cfq_clear_cfqq_wait_request(cfqq);
  711. /*
  712. * store what was left of this slice, if the queue idled out
  713. * or was preempted
  714. */
  715. if (time_after(cfqq->slice_end, now))
  716. cfqq->slice_left = cfqq->slice_end - now;
  717. else
  718. cfqq->slice_left = 0;
  719. if (cfq_cfqq_on_rr(cfqq))
  720. cfq_resort_rr_list(cfqq, preempted);
  721. if (cfqq == cfqd->active_queue)
  722. cfqd->active_queue = NULL;
  723. if (cfqd->active_cic) {
  724. put_io_context(cfqd->active_cic->ioc);
  725. cfqd->active_cic = NULL;
  726. }
  727. cfqd->dispatch_slice = 0;
  728. }
  729. static inline void cfq_slice_expired(struct cfq_data *cfqd, int preempted)
  730. {
  731. struct cfq_queue *cfqq = cfqd->active_queue;
  732. if (cfqq) {
  733. /*
  734. * use deferred expiry, if there are requests in progress as
  735. * not to disturb the slice of the next queue
  736. */
  737. if (cfq_cfqq_dispatched(cfqq))
  738. cfq_mark_cfqq_expired(cfqq);
  739. else
  740. __cfq_slice_expired(cfqd, cfqq, preempted);
  741. }
  742. }
  743. static int cfq_arm_slice_timer(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  744. {
  745. WARN_ON(!RB_EMPTY(&cfqq->sort_list));
  746. WARN_ON(cfqq != cfqd->active_queue);
  747. /*
  748. * idle is disabled, either manually or by past process history
  749. */
  750. if (!cfqd->cfq_slice_idle)
  751. return 0;
  752. if (!cfq_cfqq_idle_window(cfqq))
  753. return 0;
  754. /*
  755. * task has exited, don't wait
  756. */
  757. if (cfqd->active_cic && !cfqd->active_cic->ioc->task)
  758. return 0;
  759. cfq_mark_cfqq_must_dispatch(cfqq);
  760. cfq_mark_cfqq_wait_request(cfqq);
  761. if (!timer_pending(&cfqd->idle_slice_timer)) {
  762. unsigned long slice_left = min(cfqq->slice_end - 1, (unsigned long) cfqd->cfq_slice_idle);
  763. cfqd->idle_slice_timer.expires = jiffies + slice_left;
  764. add_timer(&cfqd->idle_slice_timer);
  765. }
  766. return 1;
  767. }
  768. static void cfq_dispatch_insert(request_queue_t *q, struct cfq_rq *crq)
  769. {
  770. struct cfq_data *cfqd = q->elevator->elevator_data;
  771. struct cfq_queue *cfqq = crq->cfq_queue;
  772. cfqq->next_crq = cfq_find_next_crq(cfqd, cfqq, crq);
  773. cfq_remove_request(crq->request);
  774. cfqq->on_dispatch[cfq_crq_is_sync(crq)]++;
  775. elv_dispatch_sort(q, crq->request);
  776. }
  777. /*
  778. * return expired entry, or NULL to just start from scratch in rbtree
  779. */
  780. static inline struct cfq_rq *cfq_check_fifo(struct cfq_queue *cfqq)
  781. {
  782. struct cfq_data *cfqd = cfqq->cfqd;
  783. struct request *rq;
  784. struct cfq_rq *crq;
  785. if (cfq_cfqq_fifo_expire(cfqq))
  786. return NULL;
  787. if (!list_empty(&cfqq->fifo)) {
  788. int fifo = cfq_cfqq_class_sync(cfqq);
  789. crq = RQ_DATA(list_entry_fifo(cfqq->fifo.next));
  790. rq = crq->request;
  791. if (time_after(jiffies, rq->start_time + cfqd->cfq_fifo_expire[fifo])) {
  792. cfq_mark_cfqq_fifo_expire(cfqq);
  793. return crq;
  794. }
  795. }
  796. return NULL;
  797. }
  798. /*
  799. * Scale schedule slice based on io priority. Use the sync time slice only
  800. * if a queue is marked sync and has sync io queued. A sync queue with async
  801. * io only, should not get full sync slice length.
  802. */
  803. static inline int
  804. cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  805. {
  806. const int base_slice = cfqd->cfq_slice[cfq_cfqq_sync(cfqq)];
  807. WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
  808. return base_slice + (base_slice/CFQ_SLICE_SCALE * (4 - cfqq->ioprio));
  809. }
  810. static inline void
  811. cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  812. {
  813. cfqq->slice_end = cfq_prio_to_slice(cfqd, cfqq) + jiffies;
  814. }
  815. static inline int
  816. cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  817. {
  818. const int base_rq = cfqd->cfq_slice_async_rq;
  819. WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
  820. return 2 * (base_rq + base_rq * (CFQ_PRIO_LISTS - 1 - cfqq->ioprio));
  821. }
  822. /*
  823. * get next queue for service
  824. */
  825. static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd)
  826. {
  827. unsigned long now = jiffies;
  828. struct cfq_queue *cfqq;
  829. cfqq = cfqd->active_queue;
  830. if (!cfqq)
  831. goto new_queue;
  832. if (cfq_cfqq_expired(cfqq))
  833. goto new_queue;
  834. /*
  835. * slice has expired
  836. */
  837. if (!cfq_cfqq_must_dispatch(cfqq) && time_after(now, cfqq->slice_end))
  838. goto expire;
  839. /*
  840. * if queue has requests, dispatch one. if not, check if
  841. * enough slice is left to wait for one
  842. */
  843. if (!RB_EMPTY(&cfqq->sort_list))
  844. goto keep_queue;
  845. else if (cfq_cfqq_class_sync(cfqq) &&
  846. time_before(now, cfqq->slice_end)) {
  847. if (cfq_arm_slice_timer(cfqd, cfqq))
  848. return NULL;
  849. }
  850. expire:
  851. cfq_slice_expired(cfqd, 0);
  852. new_queue:
  853. cfqq = cfq_set_active_queue(cfqd);
  854. keep_queue:
  855. return cfqq;
  856. }
  857. static int
  858. __cfq_dispatch_requests(struct cfq_data *cfqd, struct cfq_queue *cfqq,
  859. int max_dispatch)
  860. {
  861. int dispatched = 0;
  862. BUG_ON(RB_EMPTY(&cfqq->sort_list));
  863. do {
  864. struct cfq_rq *crq;
  865. /*
  866. * follow expired path, else get first next available
  867. */
  868. if ((crq = cfq_check_fifo(cfqq)) == NULL)
  869. crq = cfqq->next_crq;
  870. /*
  871. * finally, insert request into driver dispatch list
  872. */
  873. cfq_dispatch_insert(cfqd->queue, crq);
  874. cfqd->dispatch_slice++;
  875. dispatched++;
  876. if (!cfqd->active_cic) {
  877. atomic_inc(&crq->io_context->ioc->refcount);
  878. cfqd->active_cic = crq->io_context;
  879. }
  880. if (RB_EMPTY(&cfqq->sort_list))
  881. break;
  882. } while (dispatched < max_dispatch);
  883. /*
  884. * if slice end isn't set yet, set it. if at least one request was
  885. * sync, use the sync time slice value
  886. */
  887. if (!cfqq->slice_end)
  888. cfq_set_prio_slice(cfqd, cfqq);
  889. /*
  890. * expire an async queue immediately if it has used up its slice. idle
  891. * queue always expire after 1 dispatch round.
  892. */
  893. if ((!cfq_cfqq_sync(cfqq) &&
  894. cfqd->dispatch_slice >= cfq_prio_to_maxrq(cfqd, cfqq)) ||
  895. cfq_class_idle(cfqq))
  896. cfq_slice_expired(cfqd, 0);
  897. return dispatched;
  898. }
  899. static int
  900. cfq_forced_dispatch_cfqqs(struct list_head *list)
  901. {
  902. int dispatched = 0;
  903. struct cfq_queue *cfqq, *next;
  904. struct cfq_rq *crq;
  905. list_for_each_entry_safe(cfqq, next, list, cfq_list) {
  906. while ((crq = cfqq->next_crq)) {
  907. cfq_dispatch_insert(cfqq->cfqd->queue, crq);
  908. dispatched++;
  909. }
  910. BUG_ON(!list_empty(&cfqq->fifo));
  911. }
  912. return dispatched;
  913. }
  914. static int
  915. cfq_forced_dispatch(struct cfq_data *cfqd)
  916. {
  917. int i, dispatched = 0;
  918. for (i = 0; i < CFQ_PRIO_LISTS; i++)
  919. dispatched += cfq_forced_dispatch_cfqqs(&cfqd->rr_list[i]);
  920. dispatched += cfq_forced_dispatch_cfqqs(&cfqd->busy_rr);
  921. dispatched += cfq_forced_dispatch_cfqqs(&cfqd->cur_rr);
  922. dispatched += cfq_forced_dispatch_cfqqs(&cfqd->idle_rr);
  923. cfq_slice_expired(cfqd, 0);
  924. BUG_ON(cfqd->busy_queues);
  925. return dispatched;
  926. }
  927. static int
  928. cfq_dispatch_requests(request_queue_t *q, int force)
  929. {
  930. struct cfq_data *cfqd = q->elevator->elevator_data;
  931. struct cfq_queue *cfqq;
  932. if (!cfqd->busy_queues)
  933. return 0;
  934. if (unlikely(force))
  935. return cfq_forced_dispatch(cfqd);
  936. cfqq = cfq_select_queue(cfqd);
  937. if (cfqq) {
  938. int max_dispatch;
  939. /*
  940. * if idle window is disabled, allow queue buildup
  941. */
  942. if (!cfq_cfqq_idle_window(cfqq) &&
  943. cfqd->rq_in_driver >= cfqd->cfq_max_depth)
  944. return 0;
  945. cfq_clear_cfqq_must_dispatch(cfqq);
  946. cfq_clear_cfqq_wait_request(cfqq);
  947. del_timer(&cfqd->idle_slice_timer);
  948. max_dispatch = cfqd->cfq_quantum;
  949. if (cfq_class_idle(cfqq))
  950. max_dispatch = 1;
  951. return __cfq_dispatch_requests(cfqd, cfqq, max_dispatch);
  952. }
  953. return 0;
  954. }
  955. /*
  956. * task holds one reference to the queue, dropped when task exits. each crq
  957. * in-flight on this queue also holds a reference, dropped when crq is freed.
  958. *
  959. * queue lock must be held here.
  960. */
  961. static void cfq_put_queue(struct cfq_queue *cfqq)
  962. {
  963. struct cfq_data *cfqd = cfqq->cfqd;
  964. BUG_ON(atomic_read(&cfqq->ref) <= 0);
  965. if (!atomic_dec_and_test(&cfqq->ref))
  966. return;
  967. BUG_ON(rb_first(&cfqq->sort_list));
  968. BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
  969. BUG_ON(cfq_cfqq_on_rr(cfqq));
  970. if (unlikely(cfqd->active_queue == cfqq)) {
  971. __cfq_slice_expired(cfqd, cfqq, 0);
  972. cfq_schedule_dispatch(cfqd);
  973. }
  974. cfq_put_cfqd(cfqq->cfqd);
  975. /*
  976. * it's on the empty list and still hashed
  977. */
  978. list_del(&cfqq->cfq_list);
  979. hlist_del(&cfqq->cfq_hash);
  980. kmem_cache_free(cfq_pool, cfqq);
  981. }
  982. static inline struct cfq_queue *
  983. __cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, unsigned int prio,
  984. const int hashval)
  985. {
  986. struct hlist_head *hash_list = &cfqd->cfq_hash[hashval];
  987. struct hlist_node *entry, *next;
  988. hlist_for_each_safe(entry, next, hash_list) {
  989. struct cfq_queue *__cfqq = list_entry_qhash(entry);
  990. const unsigned short __p = IOPRIO_PRIO_VALUE(__cfqq->ioprio_class, __cfqq->ioprio);
  991. if (__cfqq->key == key && (__p == prio || prio == CFQ_KEY_ANY))
  992. return __cfqq;
  993. }
  994. return NULL;
  995. }
  996. static struct cfq_queue *
  997. cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, unsigned short prio)
  998. {
  999. return __cfq_find_cfq_hash(cfqd, key, prio, hash_long(key, CFQ_QHASH_SHIFT));
  1000. }
  1001. static void cfq_free_io_context(struct cfq_io_context *cic)
  1002. {
  1003. struct cfq_io_context *__cic;
  1004. struct list_head *entry, *next;
  1005. list_for_each_safe(entry, next, &cic->list) {
  1006. __cic = list_entry(entry, struct cfq_io_context, list);
  1007. kmem_cache_free(cfq_ioc_pool, __cic);
  1008. }
  1009. kmem_cache_free(cfq_ioc_pool, cic);
  1010. }
  1011. /*
  1012. * Called with interrupts disabled
  1013. */
  1014. static void cfq_exit_single_io_context(struct cfq_io_context *cic)
  1015. {
  1016. struct cfq_data *cfqd = cic->cfqq->cfqd;
  1017. request_queue_t *q = cfqd->queue;
  1018. WARN_ON(!irqs_disabled());
  1019. spin_lock(q->queue_lock);
  1020. if (unlikely(cic->cfqq == cfqd->active_queue)) {
  1021. __cfq_slice_expired(cfqd, cic->cfqq, 0);
  1022. cfq_schedule_dispatch(cfqd);
  1023. }
  1024. cfq_put_queue(cic->cfqq);
  1025. cic->cfqq = NULL;
  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. list_for_each(entry, &cic->list) {
  1042. __cic = list_entry(entry, struct cfq_io_context, list);
  1043. cfq_exit_single_io_context(__cic);
  1044. }
  1045. cfq_exit_single_io_context(cic);
  1046. local_irq_restore(flags);
  1047. }
  1048. static struct cfq_io_context *
  1049. cfq_alloc_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
  1050. {
  1051. struct cfq_io_context *cic = kmem_cache_alloc(cfq_ioc_pool, gfp_mask);
  1052. if (cic) {
  1053. INIT_LIST_HEAD(&cic->list);
  1054. cic->cfqq = NULL;
  1055. cic->key = NULL;
  1056. cic->last_end_request = jiffies;
  1057. cic->ttime_total = 0;
  1058. cic->ttime_samples = 0;
  1059. cic->ttime_mean = 0;
  1060. cic->dtor = cfq_free_io_context;
  1061. cic->exit = cfq_exit_io_context;
  1062. }
  1063. return cic;
  1064. }
  1065. static void cfq_init_prio_data(struct cfq_queue *cfqq)
  1066. {
  1067. struct task_struct *tsk = current;
  1068. int ioprio_class;
  1069. if (!cfq_cfqq_prio_changed(cfqq))
  1070. return;
  1071. ioprio_class = IOPRIO_PRIO_CLASS(tsk->ioprio);
  1072. switch (ioprio_class) {
  1073. default:
  1074. printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class);
  1075. case IOPRIO_CLASS_NONE:
  1076. /*
  1077. * no prio set, place us in the middle of the BE classes
  1078. */
  1079. cfqq->ioprio = task_nice_ioprio(tsk);
  1080. cfqq->ioprio_class = IOPRIO_CLASS_BE;
  1081. break;
  1082. case IOPRIO_CLASS_RT:
  1083. cfqq->ioprio = task_ioprio(tsk);
  1084. cfqq->ioprio_class = IOPRIO_CLASS_RT;
  1085. break;
  1086. case IOPRIO_CLASS_BE:
  1087. cfqq->ioprio = task_ioprio(tsk);
  1088. cfqq->ioprio_class = IOPRIO_CLASS_BE;
  1089. break;
  1090. case IOPRIO_CLASS_IDLE:
  1091. cfqq->ioprio_class = IOPRIO_CLASS_IDLE;
  1092. cfqq->ioprio = 7;
  1093. cfq_clear_cfqq_idle_window(cfqq);
  1094. break;
  1095. }
  1096. /*
  1097. * keep track of original prio settings in case we have to temporarily
  1098. * elevate the priority of this queue
  1099. */
  1100. cfqq->org_ioprio = cfqq->ioprio;
  1101. cfqq->org_ioprio_class = cfqq->ioprio_class;
  1102. if (cfq_cfqq_on_rr(cfqq))
  1103. cfq_resort_rr_list(cfqq, 0);
  1104. cfq_clear_cfqq_prio_changed(cfqq);
  1105. }
  1106. static inline void changed_ioprio(struct cfq_queue *cfqq)
  1107. {
  1108. if (cfqq) {
  1109. struct cfq_data *cfqd = cfqq->cfqd;
  1110. spin_lock(cfqd->queue->queue_lock);
  1111. cfq_mark_cfqq_prio_changed(cfqq);
  1112. cfq_init_prio_data(cfqq);
  1113. spin_unlock(cfqd->queue->queue_lock);
  1114. }
  1115. }
  1116. /*
  1117. * callback from sys_ioprio_set, irqs are disabled
  1118. */
  1119. static int cfq_ioc_set_ioprio(struct io_context *ioc, unsigned int ioprio)
  1120. {
  1121. struct cfq_io_context *cic = ioc->cic;
  1122. changed_ioprio(cic->cfqq);
  1123. list_for_each_entry(cic, &cic->list, list)
  1124. changed_ioprio(cic->cfqq);
  1125. return 0;
  1126. }
  1127. static struct cfq_queue *
  1128. cfq_get_queue(struct cfq_data *cfqd, unsigned int key, unsigned short ioprio,
  1129. gfp_t gfp_mask)
  1130. {
  1131. const int hashval = hash_long(key, CFQ_QHASH_SHIFT);
  1132. struct cfq_queue *cfqq, *new_cfqq = NULL;
  1133. retry:
  1134. cfqq = __cfq_find_cfq_hash(cfqd, key, ioprio, hashval);
  1135. if (!cfqq) {
  1136. if (new_cfqq) {
  1137. cfqq = new_cfqq;
  1138. new_cfqq = NULL;
  1139. } else if (gfp_mask & __GFP_WAIT) {
  1140. spin_unlock_irq(cfqd->queue->queue_lock);
  1141. new_cfqq = kmem_cache_alloc(cfq_pool, gfp_mask);
  1142. spin_lock_irq(cfqd->queue->queue_lock);
  1143. goto retry;
  1144. } else {
  1145. cfqq = kmem_cache_alloc(cfq_pool, gfp_mask);
  1146. if (!cfqq)
  1147. goto out;
  1148. }
  1149. memset(cfqq, 0, sizeof(*cfqq));
  1150. INIT_HLIST_NODE(&cfqq->cfq_hash);
  1151. INIT_LIST_HEAD(&cfqq->cfq_list);
  1152. RB_CLEAR_ROOT(&cfqq->sort_list);
  1153. INIT_LIST_HEAD(&cfqq->fifo);
  1154. cfqq->key = key;
  1155. hlist_add_head(&cfqq->cfq_hash, &cfqd->cfq_hash[hashval]);
  1156. atomic_set(&cfqq->ref, 0);
  1157. cfqq->cfqd = cfqd;
  1158. atomic_inc(&cfqd->ref);
  1159. cfqq->service_last = 0;
  1160. /*
  1161. * set ->slice_left to allow preemption for a new process
  1162. */
  1163. cfqq->slice_left = 2 * cfqd->cfq_slice_idle;
  1164. cfq_mark_cfqq_idle_window(cfqq);
  1165. cfq_mark_cfqq_prio_changed(cfqq);
  1166. cfq_init_prio_data(cfqq);
  1167. }
  1168. if (new_cfqq)
  1169. kmem_cache_free(cfq_pool, new_cfqq);
  1170. atomic_inc(&cfqq->ref);
  1171. out:
  1172. WARN_ON((gfp_mask & __GFP_WAIT) && !cfqq);
  1173. return cfqq;
  1174. }
  1175. /*
  1176. * Setup general io context and cfq io context. There can be several cfq
  1177. * io contexts per general io context, if this process is doing io to more
  1178. * than one device managed by cfq. Note that caller is holding a reference to
  1179. * cfqq, so we don't need to worry about it disappearing
  1180. */
  1181. static struct cfq_io_context *
  1182. cfq_get_io_context(struct cfq_data *cfqd, pid_t pid, gfp_t gfp_mask)
  1183. {
  1184. struct io_context *ioc = NULL;
  1185. struct cfq_io_context *cic;
  1186. might_sleep_if(gfp_mask & __GFP_WAIT);
  1187. ioc = get_io_context(gfp_mask);
  1188. if (!ioc)
  1189. return NULL;
  1190. if ((cic = ioc->cic) == NULL) {
  1191. cic = cfq_alloc_io_context(cfqd, gfp_mask);
  1192. if (cic == NULL)
  1193. goto err;
  1194. /*
  1195. * manually increment generic io_context usage count, it
  1196. * cannot go away since we are already holding one ref to it
  1197. */
  1198. ioc->cic = cic;
  1199. ioc->set_ioprio = cfq_ioc_set_ioprio;
  1200. cic->ioc = ioc;
  1201. cic->key = cfqd;
  1202. atomic_inc(&cfqd->ref);
  1203. } else {
  1204. struct cfq_io_context *__cic;
  1205. /*
  1206. * the first cic on the list is actually the head itself
  1207. */
  1208. if (cic->key == cfqd)
  1209. goto out;
  1210. /*
  1211. * cic exists, check if we already are there. linear search
  1212. * should be ok here, the list will usually not be more than
  1213. * 1 or a few entries long
  1214. */
  1215. list_for_each_entry(__cic, &cic->list, list) {
  1216. /*
  1217. * this process is already holding a reference to
  1218. * this queue, so no need to get one more
  1219. */
  1220. if (__cic->key == cfqd) {
  1221. cic = __cic;
  1222. goto out;
  1223. }
  1224. }
  1225. /*
  1226. * nope, process doesn't have a cic assoicated with this
  1227. * cfqq yet. get a new one and add to list
  1228. */
  1229. __cic = cfq_alloc_io_context(cfqd, gfp_mask);
  1230. if (__cic == NULL)
  1231. goto err;
  1232. __cic->ioc = ioc;
  1233. __cic->key = cfqd;
  1234. atomic_inc(&cfqd->ref);
  1235. list_add(&__cic->list, &cic->list);
  1236. cic = __cic;
  1237. }
  1238. out:
  1239. return cic;
  1240. err:
  1241. put_io_context(ioc);
  1242. return NULL;
  1243. }
  1244. static void
  1245. cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_io_context *cic)
  1246. {
  1247. unsigned long elapsed, ttime;
  1248. /*
  1249. * if this context already has stuff queued, thinktime is from
  1250. * last queue not last end
  1251. */
  1252. #if 0
  1253. if (time_after(cic->last_end_request, cic->last_queue))
  1254. elapsed = jiffies - cic->last_end_request;
  1255. else
  1256. elapsed = jiffies - cic->last_queue;
  1257. #else
  1258. elapsed = jiffies - cic->last_end_request;
  1259. #endif
  1260. ttime = min(elapsed, 2UL * cfqd->cfq_slice_idle);
  1261. cic->ttime_samples = (7*cic->ttime_samples + 256) / 8;
  1262. cic->ttime_total = (7*cic->ttime_total + 256*ttime) / 8;
  1263. cic->ttime_mean = (cic->ttime_total + 128) / cic->ttime_samples;
  1264. }
  1265. #define sample_valid(samples) ((samples) > 80)
  1266. /*
  1267. * Disable idle window if the process thinks too long or seeks so much that
  1268. * it doesn't matter
  1269. */
  1270. static void
  1271. cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq,
  1272. struct cfq_io_context *cic)
  1273. {
  1274. int enable_idle = cfq_cfqq_idle_window(cfqq);
  1275. if (!cic->ioc->task || !cfqd->cfq_slice_idle)
  1276. enable_idle = 0;
  1277. else if (sample_valid(cic->ttime_samples)) {
  1278. if (cic->ttime_mean > cfqd->cfq_slice_idle)
  1279. enable_idle = 0;
  1280. else
  1281. enable_idle = 1;
  1282. }
  1283. if (enable_idle)
  1284. cfq_mark_cfqq_idle_window(cfqq);
  1285. else
  1286. cfq_clear_cfqq_idle_window(cfqq);
  1287. }
  1288. /*
  1289. * Check if new_cfqq should preempt the currently active queue. Return 0 for
  1290. * no or if we aren't sure, a 1 will cause a preempt.
  1291. */
  1292. static int
  1293. cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
  1294. struct cfq_rq *crq)
  1295. {
  1296. struct cfq_queue *cfqq = cfqd->active_queue;
  1297. if (cfq_class_idle(new_cfqq))
  1298. return 0;
  1299. if (!cfqq)
  1300. return 1;
  1301. if (cfq_class_idle(cfqq))
  1302. return 1;
  1303. if (!cfq_cfqq_wait_request(new_cfqq))
  1304. return 0;
  1305. /*
  1306. * if it doesn't have slice left, forget it
  1307. */
  1308. if (new_cfqq->slice_left < cfqd->cfq_slice_idle)
  1309. return 0;
  1310. if (cfq_crq_is_sync(crq) && !cfq_cfqq_sync(cfqq))
  1311. return 1;
  1312. return 0;
  1313. }
  1314. /*
  1315. * cfqq preempts the active queue. if we allowed preempt with no slice left,
  1316. * let it have half of its nominal slice.
  1317. */
  1318. static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  1319. {
  1320. struct cfq_queue *__cfqq, *next;
  1321. list_for_each_entry_safe(__cfqq, next, &cfqd->cur_rr, cfq_list)
  1322. cfq_resort_rr_list(__cfqq, 1);
  1323. if (!cfqq->slice_left)
  1324. cfqq->slice_left = cfq_prio_to_slice(cfqd, cfqq) / 2;
  1325. cfqq->slice_end = cfqq->slice_left + jiffies;
  1326. __cfq_slice_expired(cfqd, cfqq, 1);
  1327. __cfq_set_active_queue(cfqd, cfqq);
  1328. }
  1329. /*
  1330. * should really be a ll_rw_blk.c helper
  1331. */
  1332. static void cfq_start_queueing(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  1333. {
  1334. request_queue_t *q = cfqd->queue;
  1335. if (!blk_queue_plugged(q))
  1336. q->request_fn(q);
  1337. else
  1338. __generic_unplug_device(q);
  1339. }
  1340. /*
  1341. * Called when a new fs request (crq) is added (to cfqq). Check if there's
  1342. * something we should do about it
  1343. */
  1344. static void
  1345. cfq_crq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
  1346. struct cfq_rq *crq)
  1347. {
  1348. struct cfq_io_context *cic;
  1349. cfqq->next_crq = cfq_choose_req(cfqd, cfqq->next_crq, crq);
  1350. /*
  1351. * we never wait for an async request and we don't allow preemption
  1352. * of an async request. so just return early
  1353. */
  1354. if (!cfq_crq_is_sync(crq))
  1355. return;
  1356. cic = crq->io_context;
  1357. cfq_update_io_thinktime(cfqd, cic);
  1358. cfq_update_idle_window(cfqd, cfqq, cic);
  1359. cic->last_queue = jiffies;
  1360. if (cfqq == cfqd->active_queue) {
  1361. /*
  1362. * if we are waiting for a request for this queue, let it rip
  1363. * immediately and flag that we must not expire this queue
  1364. * just now
  1365. */
  1366. if (cfq_cfqq_wait_request(cfqq)) {
  1367. cfq_mark_cfqq_must_dispatch(cfqq);
  1368. del_timer(&cfqd->idle_slice_timer);
  1369. cfq_start_queueing(cfqd, cfqq);
  1370. }
  1371. } else if (cfq_should_preempt(cfqd, cfqq, crq)) {
  1372. /*
  1373. * not the active queue - expire current slice if it is
  1374. * idle and has expired it's mean thinktime or this new queue
  1375. * has some old slice time left and is of higher priority
  1376. */
  1377. cfq_preempt_queue(cfqd, cfqq);
  1378. cfq_mark_cfqq_must_dispatch(cfqq);
  1379. cfq_start_queueing(cfqd, cfqq);
  1380. }
  1381. }
  1382. static void cfq_insert_request(request_queue_t *q, struct request *rq)
  1383. {
  1384. struct cfq_data *cfqd = q->elevator->elevator_data;
  1385. struct cfq_rq *crq = RQ_DATA(rq);
  1386. struct cfq_queue *cfqq = crq->cfq_queue;
  1387. cfq_init_prio_data(cfqq);
  1388. cfq_add_crq_rb(crq);
  1389. list_add_tail(&rq->queuelist, &cfqq->fifo);
  1390. if (rq_mergeable(rq))
  1391. cfq_add_crq_hash(cfqd, crq);
  1392. cfq_crq_enqueued(cfqd, cfqq, crq);
  1393. }
  1394. static void cfq_completed_request(request_queue_t *q, struct request *rq)
  1395. {
  1396. struct cfq_rq *crq = RQ_DATA(rq);
  1397. struct cfq_queue *cfqq = crq->cfq_queue;
  1398. struct cfq_data *cfqd = cfqq->cfqd;
  1399. const int sync = cfq_crq_is_sync(crq);
  1400. unsigned long now;
  1401. now = jiffies;
  1402. WARN_ON(!cfqd->rq_in_driver);
  1403. WARN_ON(!cfqq->on_dispatch[sync]);
  1404. cfqd->rq_in_driver--;
  1405. cfqq->on_dispatch[sync]--;
  1406. if (!cfq_class_idle(cfqq))
  1407. cfqd->last_end_request = now;
  1408. if (!cfq_cfqq_dispatched(cfqq)) {
  1409. if (cfq_cfqq_on_rr(cfqq)) {
  1410. cfqq->service_last = now;
  1411. cfq_resort_rr_list(cfqq, 0);
  1412. }
  1413. if (cfq_cfqq_expired(cfqq)) {
  1414. __cfq_slice_expired(cfqd, cfqq, 0);
  1415. cfq_schedule_dispatch(cfqd);
  1416. }
  1417. }
  1418. if (cfq_crq_is_sync(crq))
  1419. crq->io_context->last_end_request = now;
  1420. }
  1421. static struct request *
  1422. cfq_former_request(request_queue_t *q, struct request *rq)
  1423. {
  1424. struct cfq_rq *crq = RQ_DATA(rq);
  1425. struct rb_node *rbprev = rb_prev(&crq->rb_node);
  1426. if (rbprev)
  1427. return rb_entry_crq(rbprev)->request;
  1428. return NULL;
  1429. }
  1430. static struct request *
  1431. cfq_latter_request(request_queue_t *q, struct request *rq)
  1432. {
  1433. struct cfq_rq *crq = RQ_DATA(rq);
  1434. struct rb_node *rbnext = rb_next(&crq->rb_node);
  1435. if (rbnext)
  1436. return rb_entry_crq(rbnext)->request;
  1437. return NULL;
  1438. }
  1439. /*
  1440. * we temporarily boost lower priority queues if they are holding fs exclusive
  1441. * resources. they are boosted to normal prio (CLASS_BE/4)
  1442. */
  1443. static void cfq_prio_boost(struct cfq_queue *cfqq)
  1444. {
  1445. const int ioprio_class = cfqq->ioprio_class;
  1446. const int ioprio = cfqq->ioprio;
  1447. if (has_fs_excl()) {
  1448. /*
  1449. * boost idle prio on transactions that would lock out other
  1450. * users of the filesystem
  1451. */
  1452. if (cfq_class_idle(cfqq))
  1453. cfqq->ioprio_class = IOPRIO_CLASS_BE;
  1454. if (cfqq->ioprio > IOPRIO_NORM)
  1455. cfqq->ioprio = IOPRIO_NORM;
  1456. } else {
  1457. /*
  1458. * check if we need to unboost the queue
  1459. */
  1460. if (cfqq->ioprio_class != cfqq->org_ioprio_class)
  1461. cfqq->ioprio_class = cfqq->org_ioprio_class;
  1462. if (cfqq->ioprio != cfqq->org_ioprio)
  1463. cfqq->ioprio = cfqq->org_ioprio;
  1464. }
  1465. /*
  1466. * refile between round-robin lists if we moved the priority class
  1467. */
  1468. if ((ioprio_class != cfqq->ioprio_class || ioprio != cfqq->ioprio) &&
  1469. cfq_cfqq_on_rr(cfqq))
  1470. cfq_resort_rr_list(cfqq, 0);
  1471. }
  1472. static inline pid_t cfq_queue_pid(struct task_struct *task, int rw)
  1473. {
  1474. if (rw == READ || process_sync(task))
  1475. return task->pid;
  1476. return CFQ_KEY_ASYNC;
  1477. }
  1478. static inline int
  1479. __cfq_may_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq,
  1480. struct task_struct *task, int rw)
  1481. {
  1482. #if 1
  1483. if ((cfq_cfqq_wait_request(cfqq) || cfq_cfqq_must_alloc(cfqq)) &&
  1484. !cfq_cfqq_must_alloc_slice(cfqq)) {
  1485. cfq_mark_cfqq_must_alloc_slice(cfqq);
  1486. return ELV_MQUEUE_MUST;
  1487. }
  1488. return ELV_MQUEUE_MAY;
  1489. #else
  1490. if (!cfqq || task->flags & PF_MEMALLOC)
  1491. return ELV_MQUEUE_MAY;
  1492. if (!cfqq->allocated[rw] || cfq_cfqq_must_alloc(cfqq)) {
  1493. if (cfq_cfqq_wait_request(cfqq))
  1494. return ELV_MQUEUE_MUST;
  1495. /*
  1496. * only allow 1 ELV_MQUEUE_MUST per slice, otherwise we
  1497. * can quickly flood the queue with writes from a single task
  1498. */
  1499. if (rw == READ || !cfq_cfqq_must_alloc_slice(cfqq)) {
  1500. cfq_mark_cfqq_must_alloc_slice(cfqq);
  1501. return ELV_MQUEUE_MUST;
  1502. }
  1503. return ELV_MQUEUE_MAY;
  1504. }
  1505. if (cfq_class_idle(cfqq))
  1506. return ELV_MQUEUE_NO;
  1507. if (cfqq->allocated[rw] >= cfqd->max_queued) {
  1508. struct io_context *ioc = get_io_context(GFP_ATOMIC);
  1509. int ret = ELV_MQUEUE_NO;
  1510. if (ioc && ioc->nr_batch_requests)
  1511. ret = ELV_MQUEUE_MAY;
  1512. put_io_context(ioc);
  1513. return ret;
  1514. }
  1515. return ELV_MQUEUE_MAY;
  1516. #endif
  1517. }
  1518. static int cfq_may_queue(request_queue_t *q, int rw, struct bio *bio)
  1519. {
  1520. struct cfq_data *cfqd = q->elevator->elevator_data;
  1521. struct task_struct *tsk = current;
  1522. struct cfq_queue *cfqq;
  1523. /*
  1524. * don't force setup of a queue from here, as a call to may_queue
  1525. * does not necessarily imply that a request actually will be queued.
  1526. * so just lookup a possibly existing queue, or return 'may queue'
  1527. * if that fails
  1528. */
  1529. cfqq = cfq_find_cfq_hash(cfqd, cfq_queue_pid(tsk, rw), tsk->ioprio);
  1530. if (cfqq) {
  1531. cfq_init_prio_data(cfqq);
  1532. cfq_prio_boost(cfqq);
  1533. return __cfq_may_queue(cfqd, cfqq, tsk, rw);
  1534. }
  1535. return ELV_MQUEUE_MAY;
  1536. }
  1537. static void cfq_check_waiters(request_queue_t *q, struct cfq_queue *cfqq)
  1538. {
  1539. struct cfq_data *cfqd = q->elevator->elevator_data;
  1540. struct request_list *rl = &q->rq;
  1541. if (cfqq->allocated[READ] <= cfqd->max_queued || cfqd->rq_starved) {
  1542. smp_mb();
  1543. if (waitqueue_active(&rl->wait[READ]))
  1544. wake_up(&rl->wait[READ]);
  1545. }
  1546. if (cfqq->allocated[WRITE] <= cfqd->max_queued || cfqd->rq_starved) {
  1547. smp_mb();
  1548. if (waitqueue_active(&rl->wait[WRITE]))
  1549. wake_up(&rl->wait[WRITE]);
  1550. }
  1551. }
  1552. /*
  1553. * queue lock held here
  1554. */
  1555. static void cfq_put_request(request_queue_t *q, struct request *rq)
  1556. {
  1557. struct cfq_data *cfqd = q->elevator->elevator_data;
  1558. struct cfq_rq *crq = RQ_DATA(rq);
  1559. if (crq) {
  1560. struct cfq_queue *cfqq = crq->cfq_queue;
  1561. const int rw = rq_data_dir(rq);
  1562. BUG_ON(!cfqq->allocated[rw]);
  1563. cfqq->allocated[rw]--;
  1564. put_io_context(crq->io_context->ioc);
  1565. mempool_free(crq, cfqd->crq_pool);
  1566. rq->elevator_private = NULL;
  1567. cfq_check_waiters(q, cfqq);
  1568. cfq_put_queue(cfqq);
  1569. }
  1570. }
  1571. /*
  1572. * Allocate cfq data structures associated with this request.
  1573. */
  1574. static int
  1575. cfq_set_request(request_queue_t *q, struct request *rq, struct bio *bio,
  1576. gfp_t gfp_mask)
  1577. {
  1578. struct cfq_data *cfqd = q->elevator->elevator_data;
  1579. struct task_struct *tsk = current;
  1580. struct cfq_io_context *cic;
  1581. const int rw = rq_data_dir(rq);
  1582. pid_t key = cfq_queue_pid(tsk, rw);
  1583. struct cfq_queue *cfqq;
  1584. struct cfq_rq *crq;
  1585. unsigned long flags;
  1586. might_sleep_if(gfp_mask & __GFP_WAIT);
  1587. cic = cfq_get_io_context(cfqd, key, gfp_mask);
  1588. spin_lock_irqsave(q->queue_lock, flags);
  1589. if (!cic)
  1590. goto queue_fail;
  1591. if (!cic->cfqq) {
  1592. cfqq = cfq_get_queue(cfqd, key, tsk->ioprio, gfp_mask);
  1593. if (!cfqq)
  1594. goto queue_fail;
  1595. cic->cfqq = cfqq;
  1596. } else
  1597. cfqq = cic->cfqq;
  1598. cfqq->allocated[rw]++;
  1599. cfq_clear_cfqq_must_alloc(cfqq);
  1600. cfqd->rq_starved = 0;
  1601. atomic_inc(&cfqq->ref);
  1602. spin_unlock_irqrestore(q->queue_lock, flags);
  1603. crq = mempool_alloc(cfqd->crq_pool, gfp_mask);
  1604. if (crq) {
  1605. RB_CLEAR(&crq->rb_node);
  1606. crq->rb_key = 0;
  1607. crq->request = rq;
  1608. INIT_HLIST_NODE(&crq->hash);
  1609. crq->cfq_queue = cfqq;
  1610. crq->io_context = cic;
  1611. if (rw == READ || process_sync(tsk))
  1612. cfq_mark_crq_is_sync(crq);
  1613. else
  1614. cfq_clear_crq_is_sync(crq);
  1615. rq->elevator_private = crq;
  1616. return 0;
  1617. }
  1618. spin_lock_irqsave(q->queue_lock, flags);
  1619. cfqq->allocated[rw]--;
  1620. if (!(cfqq->allocated[0] + cfqq->allocated[1]))
  1621. cfq_mark_cfqq_must_alloc(cfqq);
  1622. cfq_put_queue(cfqq);
  1623. queue_fail:
  1624. if (cic)
  1625. put_io_context(cic->ioc);
  1626. /*
  1627. * mark us rq allocation starved. we need to kickstart the process
  1628. * ourselves if there are no pending requests that can do it for us.
  1629. * that would be an extremely rare OOM situation
  1630. */
  1631. cfqd->rq_starved = 1;
  1632. cfq_schedule_dispatch(cfqd);
  1633. spin_unlock_irqrestore(q->queue_lock, flags);
  1634. return 1;
  1635. }
  1636. static void cfq_kick_queue(void *data)
  1637. {
  1638. request_queue_t *q = data;
  1639. struct cfq_data *cfqd = q->elevator->elevator_data;
  1640. unsigned long flags;
  1641. spin_lock_irqsave(q->queue_lock, flags);
  1642. if (cfqd->rq_starved) {
  1643. struct request_list *rl = &q->rq;
  1644. /*
  1645. * we aren't guaranteed to get a request after this, but we
  1646. * have to be opportunistic
  1647. */
  1648. smp_mb();
  1649. if (waitqueue_active(&rl->wait[READ]))
  1650. wake_up(&rl->wait[READ]);
  1651. if (waitqueue_active(&rl->wait[WRITE]))
  1652. wake_up(&rl->wait[WRITE]);
  1653. }
  1654. blk_remove_plug(q);
  1655. q->request_fn(q);
  1656. spin_unlock_irqrestore(q->queue_lock, flags);
  1657. }
  1658. /*
  1659. * Timer running if the active_queue is currently idling inside its time slice
  1660. */
  1661. static void cfq_idle_slice_timer(unsigned long data)
  1662. {
  1663. struct cfq_data *cfqd = (struct cfq_data *) data;
  1664. struct cfq_queue *cfqq;
  1665. unsigned long flags;
  1666. spin_lock_irqsave(cfqd->queue->queue_lock, flags);
  1667. if ((cfqq = cfqd->active_queue) != NULL) {
  1668. unsigned long now = jiffies;
  1669. /*
  1670. * expired
  1671. */
  1672. if (time_after(now, cfqq->slice_end))
  1673. goto expire;
  1674. /*
  1675. * only expire and reinvoke request handler, if there are
  1676. * other queues with pending requests
  1677. */
  1678. if (!cfqd->busy_queues) {
  1679. cfqd->idle_slice_timer.expires = min(now + cfqd->cfq_slice_idle, cfqq->slice_end);
  1680. add_timer(&cfqd->idle_slice_timer);
  1681. goto out_cont;
  1682. }
  1683. /*
  1684. * not expired and it has a request pending, let it dispatch
  1685. */
  1686. if (!RB_EMPTY(&cfqq->sort_list)) {
  1687. cfq_mark_cfqq_must_dispatch(cfqq);
  1688. goto out_kick;
  1689. }
  1690. }
  1691. expire:
  1692. cfq_slice_expired(cfqd, 0);
  1693. out_kick:
  1694. cfq_schedule_dispatch(cfqd);
  1695. out_cont:
  1696. spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
  1697. }
  1698. /*
  1699. * Timer running if an idle class queue is waiting for service
  1700. */
  1701. static void cfq_idle_class_timer(unsigned long data)
  1702. {
  1703. struct cfq_data *cfqd = (struct cfq_data *) data;
  1704. unsigned long flags, end;
  1705. spin_lock_irqsave(cfqd->queue->queue_lock, flags);
  1706. /*
  1707. * race with a non-idle queue, reset timer
  1708. */
  1709. end = cfqd->last_end_request + CFQ_IDLE_GRACE;
  1710. if (!time_after_eq(jiffies, end)) {
  1711. cfqd->idle_class_timer.expires = end;
  1712. add_timer(&cfqd->idle_class_timer);
  1713. } else
  1714. cfq_schedule_dispatch(cfqd);
  1715. spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
  1716. }
  1717. static void cfq_shutdown_timer_wq(struct cfq_data *cfqd)
  1718. {
  1719. del_timer_sync(&cfqd->idle_slice_timer);
  1720. del_timer_sync(&cfqd->idle_class_timer);
  1721. blk_sync_queue(cfqd->queue);
  1722. }
  1723. static void cfq_put_cfqd(struct cfq_data *cfqd)
  1724. {
  1725. request_queue_t *q = cfqd->queue;
  1726. if (!atomic_dec_and_test(&cfqd->ref))
  1727. return;
  1728. cfq_shutdown_timer_wq(cfqd);
  1729. blk_put_queue(q);
  1730. mempool_destroy(cfqd->crq_pool);
  1731. kfree(cfqd->crq_hash);
  1732. kfree(cfqd->cfq_hash);
  1733. kfree(cfqd);
  1734. }
  1735. static void cfq_exit_queue(elevator_t *e)
  1736. {
  1737. struct cfq_data *cfqd = e->elevator_data;
  1738. cfq_shutdown_timer_wq(cfqd);
  1739. cfq_put_cfqd(cfqd);
  1740. }
  1741. static int cfq_init_queue(request_queue_t *q, elevator_t *e)
  1742. {
  1743. struct cfq_data *cfqd;
  1744. int i;
  1745. cfqd = kmalloc(sizeof(*cfqd), GFP_KERNEL);
  1746. if (!cfqd)
  1747. return -ENOMEM;
  1748. memset(cfqd, 0, sizeof(*cfqd));
  1749. for (i = 0; i < CFQ_PRIO_LISTS; i++)
  1750. INIT_LIST_HEAD(&cfqd->rr_list[i]);
  1751. INIT_LIST_HEAD(&cfqd->busy_rr);
  1752. INIT_LIST_HEAD(&cfqd->cur_rr);
  1753. INIT_LIST_HEAD(&cfqd->idle_rr);
  1754. INIT_LIST_HEAD(&cfqd->empty_list);
  1755. cfqd->crq_hash = kmalloc(sizeof(struct hlist_head) * CFQ_MHASH_ENTRIES, GFP_KERNEL);
  1756. if (!cfqd->crq_hash)
  1757. goto out_crqhash;
  1758. cfqd->cfq_hash = kmalloc(sizeof(struct hlist_head) * CFQ_QHASH_ENTRIES, GFP_KERNEL);
  1759. if (!cfqd->cfq_hash)
  1760. goto out_cfqhash;
  1761. cfqd->crq_pool = mempool_create(BLKDEV_MIN_RQ, mempool_alloc_slab, mempool_free_slab, crq_pool);
  1762. if (!cfqd->crq_pool)
  1763. goto out_crqpool;
  1764. for (i = 0; i < CFQ_MHASH_ENTRIES; i++)
  1765. INIT_HLIST_HEAD(&cfqd->crq_hash[i]);
  1766. for (i = 0; i < CFQ_QHASH_ENTRIES; i++)
  1767. INIT_HLIST_HEAD(&cfqd->cfq_hash[i]);
  1768. e->elevator_data = cfqd;
  1769. cfqd->queue = q;
  1770. atomic_inc(&q->refcnt);
  1771. cfqd->max_queued = q->nr_requests / 4;
  1772. q->nr_batching = cfq_queued;
  1773. init_timer(&cfqd->idle_slice_timer);
  1774. cfqd->idle_slice_timer.function = cfq_idle_slice_timer;
  1775. cfqd->idle_slice_timer.data = (unsigned long) cfqd;
  1776. init_timer(&cfqd->idle_class_timer);
  1777. cfqd->idle_class_timer.function = cfq_idle_class_timer;
  1778. cfqd->idle_class_timer.data = (unsigned long) cfqd;
  1779. INIT_WORK(&cfqd->unplug_work, cfq_kick_queue, q);
  1780. atomic_set(&cfqd->ref, 1);
  1781. cfqd->cfq_queued = cfq_queued;
  1782. cfqd->cfq_quantum = cfq_quantum;
  1783. cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0];
  1784. cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1];
  1785. cfqd->cfq_back_max = cfq_back_max;
  1786. cfqd->cfq_back_penalty = cfq_back_penalty;
  1787. cfqd->cfq_slice[0] = cfq_slice_async;
  1788. cfqd->cfq_slice[1] = cfq_slice_sync;
  1789. cfqd->cfq_slice_async_rq = cfq_slice_async_rq;
  1790. cfqd->cfq_slice_idle = cfq_slice_idle;
  1791. cfqd->cfq_max_depth = cfq_max_depth;
  1792. return 0;
  1793. out_crqpool:
  1794. kfree(cfqd->cfq_hash);
  1795. out_cfqhash:
  1796. kfree(cfqd->crq_hash);
  1797. out_crqhash:
  1798. kfree(cfqd);
  1799. return -ENOMEM;
  1800. }
  1801. static void cfq_slab_kill(void)
  1802. {
  1803. if (crq_pool)
  1804. kmem_cache_destroy(crq_pool);
  1805. if (cfq_pool)
  1806. kmem_cache_destroy(cfq_pool);
  1807. if (cfq_ioc_pool)
  1808. kmem_cache_destroy(cfq_ioc_pool);
  1809. }
  1810. static int __init cfq_slab_setup(void)
  1811. {
  1812. crq_pool = kmem_cache_create("crq_pool", sizeof(struct cfq_rq), 0, 0,
  1813. NULL, NULL);
  1814. if (!crq_pool)
  1815. goto fail;
  1816. cfq_pool = kmem_cache_create("cfq_pool", sizeof(struct cfq_queue), 0, 0,
  1817. NULL, NULL);
  1818. if (!cfq_pool)
  1819. goto fail;
  1820. cfq_ioc_pool = kmem_cache_create("cfq_ioc_pool",
  1821. sizeof(struct cfq_io_context), 0, 0, NULL, NULL);
  1822. if (!cfq_ioc_pool)
  1823. goto fail;
  1824. return 0;
  1825. fail:
  1826. cfq_slab_kill();
  1827. return -ENOMEM;
  1828. }
  1829. /*
  1830. * sysfs parts below -->
  1831. */
  1832. struct cfq_fs_entry {
  1833. struct attribute attr;
  1834. ssize_t (*show)(struct cfq_data *, char *);
  1835. ssize_t (*store)(struct cfq_data *, const char *, size_t);
  1836. };
  1837. static ssize_t
  1838. cfq_var_show(unsigned int var, char *page)
  1839. {
  1840. return sprintf(page, "%d\n", var);
  1841. }
  1842. static ssize_t
  1843. cfq_var_store(unsigned int *var, const char *page, size_t count)
  1844. {
  1845. char *p = (char *) page;
  1846. *var = simple_strtoul(p, &p, 10);
  1847. return count;
  1848. }
  1849. #define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
  1850. static ssize_t __FUNC(struct cfq_data *cfqd, char *page) \
  1851. { \
  1852. unsigned int __data = __VAR; \
  1853. if (__CONV) \
  1854. __data = jiffies_to_msecs(__data); \
  1855. return cfq_var_show(__data, (page)); \
  1856. }
  1857. SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0);
  1858. SHOW_FUNCTION(cfq_queued_show, cfqd->cfq_queued, 0);
  1859. SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1);
  1860. SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1);
  1861. SHOW_FUNCTION(cfq_back_max_show, cfqd->cfq_back_max, 0);
  1862. SHOW_FUNCTION(cfq_back_penalty_show, cfqd->cfq_back_penalty, 0);
  1863. SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1);
  1864. SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1);
  1865. SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1);
  1866. SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0);
  1867. SHOW_FUNCTION(cfq_max_depth_show, cfqd->cfq_max_depth, 0);
  1868. #undef SHOW_FUNCTION
  1869. #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
  1870. static ssize_t __FUNC(struct cfq_data *cfqd, const char *page, size_t count) \
  1871. { \
  1872. unsigned int __data; \
  1873. int ret = cfq_var_store(&__data, (page), count); \
  1874. if (__data < (MIN)) \
  1875. __data = (MIN); \
  1876. else if (__data > (MAX)) \
  1877. __data = (MAX); \
  1878. if (__CONV) \
  1879. *(__PTR) = msecs_to_jiffies(__data); \
  1880. else \
  1881. *(__PTR) = __data; \
  1882. return ret; \
  1883. }
  1884. STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0);
  1885. STORE_FUNCTION(cfq_queued_store, &cfqd->cfq_queued, 1, UINT_MAX, 0);
  1886. STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1, UINT_MAX, 1);
  1887. STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1, UINT_MAX, 1);
  1888. STORE_FUNCTION(cfq_back_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0);
  1889. STORE_FUNCTION(cfq_back_penalty_store, &cfqd->cfq_back_penalty, 1, UINT_MAX, 0);
  1890. STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1);
  1891. STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1);
  1892. STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1);
  1893. STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1, UINT_MAX, 0);
  1894. STORE_FUNCTION(cfq_max_depth_store, &cfqd->cfq_max_depth, 1, UINT_MAX, 0);
  1895. #undef STORE_FUNCTION
  1896. static struct cfq_fs_entry cfq_quantum_entry = {
  1897. .attr = {.name = "quantum", .mode = S_IRUGO | S_IWUSR },
  1898. .show = cfq_quantum_show,
  1899. .store = cfq_quantum_store,
  1900. };
  1901. static struct cfq_fs_entry cfq_queued_entry = {
  1902. .attr = {.name = "queued", .mode = S_IRUGO | S_IWUSR },
  1903. .show = cfq_queued_show,
  1904. .store = cfq_queued_store,
  1905. };
  1906. static struct cfq_fs_entry cfq_fifo_expire_sync_entry = {
  1907. .attr = {.name = "fifo_expire_sync", .mode = S_IRUGO | S_IWUSR },
  1908. .show = cfq_fifo_expire_sync_show,
  1909. .store = cfq_fifo_expire_sync_store,
  1910. };
  1911. static struct cfq_fs_entry cfq_fifo_expire_async_entry = {
  1912. .attr = {.name = "fifo_expire_async", .mode = S_IRUGO | S_IWUSR },
  1913. .show = cfq_fifo_expire_async_show,
  1914. .store = cfq_fifo_expire_async_store,
  1915. };
  1916. static struct cfq_fs_entry cfq_back_max_entry = {
  1917. .attr = {.name = "back_seek_max", .mode = S_IRUGO | S_IWUSR },
  1918. .show = cfq_back_max_show,
  1919. .store = cfq_back_max_store,
  1920. };
  1921. static struct cfq_fs_entry cfq_back_penalty_entry = {
  1922. .attr = {.name = "back_seek_penalty", .mode = S_IRUGO | S_IWUSR },
  1923. .show = cfq_back_penalty_show,
  1924. .store = cfq_back_penalty_store,
  1925. };
  1926. static struct cfq_fs_entry cfq_slice_sync_entry = {
  1927. .attr = {.name = "slice_sync", .mode = S_IRUGO | S_IWUSR },
  1928. .show = cfq_slice_sync_show,
  1929. .store = cfq_slice_sync_store,
  1930. };
  1931. static struct cfq_fs_entry cfq_slice_async_entry = {
  1932. .attr = {.name = "slice_async", .mode = S_IRUGO | S_IWUSR },
  1933. .show = cfq_slice_async_show,
  1934. .store = cfq_slice_async_store,
  1935. };
  1936. static struct cfq_fs_entry cfq_slice_async_rq_entry = {
  1937. .attr = {.name = "slice_async_rq", .mode = S_IRUGO | S_IWUSR },
  1938. .show = cfq_slice_async_rq_show,
  1939. .store = cfq_slice_async_rq_store,
  1940. };
  1941. static struct cfq_fs_entry cfq_slice_idle_entry = {
  1942. .attr = {.name = "slice_idle", .mode = S_IRUGO | S_IWUSR },
  1943. .show = cfq_slice_idle_show,
  1944. .store = cfq_slice_idle_store,
  1945. };
  1946. static struct cfq_fs_entry cfq_max_depth_entry = {
  1947. .attr = {.name = "max_depth", .mode = S_IRUGO | S_IWUSR },
  1948. .show = cfq_max_depth_show,
  1949. .store = cfq_max_depth_store,
  1950. };
  1951. static struct attribute *default_attrs[] = {
  1952. &cfq_quantum_entry.attr,
  1953. &cfq_queued_entry.attr,
  1954. &cfq_fifo_expire_sync_entry.attr,
  1955. &cfq_fifo_expire_async_entry.attr,
  1956. &cfq_back_max_entry.attr,
  1957. &cfq_back_penalty_entry.attr,
  1958. &cfq_slice_sync_entry.attr,
  1959. &cfq_slice_async_entry.attr,
  1960. &cfq_slice_async_rq_entry.attr,
  1961. &cfq_slice_idle_entry.attr,
  1962. &cfq_max_depth_entry.attr,
  1963. NULL,
  1964. };
  1965. #define to_cfq(atr) container_of((atr), struct cfq_fs_entry, attr)
  1966. static ssize_t
  1967. cfq_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
  1968. {
  1969. elevator_t *e = container_of(kobj, elevator_t, kobj);
  1970. struct cfq_fs_entry *entry = to_cfq(attr);
  1971. if (!entry->show)
  1972. return -EIO;
  1973. return entry->show(e->elevator_data, page);
  1974. }
  1975. static ssize_t
  1976. cfq_attr_store(struct kobject *kobj, struct attribute *attr,
  1977. const char *page, size_t length)
  1978. {
  1979. elevator_t *e = container_of(kobj, elevator_t, kobj);
  1980. struct cfq_fs_entry *entry = to_cfq(attr);
  1981. if (!entry->store)
  1982. return -EIO;
  1983. return entry->store(e->elevator_data, page, length);
  1984. }
  1985. static struct sysfs_ops cfq_sysfs_ops = {
  1986. .show = cfq_attr_show,
  1987. .store = cfq_attr_store,
  1988. };
  1989. static struct kobj_type cfq_ktype = {
  1990. .sysfs_ops = &cfq_sysfs_ops,
  1991. .default_attrs = default_attrs,
  1992. };
  1993. static struct elevator_type iosched_cfq = {
  1994. .ops = {
  1995. .elevator_merge_fn = cfq_merge,
  1996. .elevator_merged_fn = cfq_merged_request,
  1997. .elevator_merge_req_fn = cfq_merged_requests,
  1998. .elevator_dispatch_fn = cfq_dispatch_requests,
  1999. .elevator_add_req_fn = cfq_insert_request,
  2000. .elevator_activate_req_fn = cfq_activate_request,
  2001. .elevator_deactivate_req_fn = cfq_deactivate_request,
  2002. .elevator_queue_empty_fn = cfq_queue_empty,
  2003. .elevator_completed_req_fn = cfq_completed_request,
  2004. .elevator_former_req_fn = cfq_former_request,
  2005. .elevator_latter_req_fn = cfq_latter_request,
  2006. .elevator_set_req_fn = cfq_set_request,
  2007. .elevator_put_req_fn = cfq_put_request,
  2008. .elevator_may_queue_fn = cfq_may_queue,
  2009. .elevator_init_fn = cfq_init_queue,
  2010. .elevator_exit_fn = cfq_exit_queue,
  2011. },
  2012. .elevator_ktype = &cfq_ktype,
  2013. .elevator_name = "cfq",
  2014. .elevator_owner = THIS_MODULE,
  2015. };
  2016. static int __init cfq_init(void)
  2017. {
  2018. int ret;
  2019. /*
  2020. * could be 0 on HZ < 1000 setups
  2021. */
  2022. if (!cfq_slice_async)
  2023. cfq_slice_async = 1;
  2024. if (!cfq_slice_idle)
  2025. cfq_slice_idle = 1;
  2026. if (cfq_slab_setup())
  2027. return -ENOMEM;
  2028. ret = elv_register(&iosched_cfq);
  2029. if (ret)
  2030. cfq_slab_kill();
  2031. return ret;
  2032. }
  2033. static void __exit cfq_exit(void)
  2034. {
  2035. elv_unregister(&iosched_cfq);
  2036. cfq_slab_kill();
  2037. }
  2038. module_init(cfq_init);
  2039. module_exit(cfq_exit);
  2040. MODULE_AUTHOR("Jens Axboe");
  2041. MODULE_LICENSE("GPL");
  2042. MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");