cfq-iosched.c 58 KB

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