cfq-iosched.c 55 KB

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