cfq-iosched.c 51 KB

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