cfq-iosched.c 54 KB

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