cfq-iosched.c 59 KB

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