cfq-iosched.c 57 KB

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