cfq-iosched.c 65 KB

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