cfq-iosched.c 62 KB

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