cfq-iosched.c 58 KB

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