cfq-iosched.c 54 KB

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