cfq-iosched.c 60 KB

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