cfq-iosched.c 58 KB

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