cfq-iosched.c 58 KB

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