cfq-iosched.c 65 KB

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