cfq-iosched.c 54 KB

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