cfq-iosched.c 64 KB

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