cfq-iosched.c 53 KB

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