cfq-iosched.c 58 KB

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