cfq-iosched.c 53 KB

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