cfq-iosched.c 69 KB

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