cfq-iosched.c 63 KB

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