cfq-iosched.c 53 KB

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