cfq-iosched.c 92 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742
  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/jiffies.h>
  13. #include <linux/rbtree.h>
  14. #include <linux/ioprio.h>
  15. #include <linux/blktrace_api.h>
  16. #include "blk-cgroup.h"
  17. /*
  18. * tunables
  19. */
  20. /* max queue in one round of service */
  21. static const int cfq_quantum = 4;
  22. static const int cfq_fifo_expire[2] = { HZ / 4, HZ / 8 };
  23. /* maximum backwards seek, in KiB */
  24. static const int cfq_back_max = 16 * 1024;
  25. /* penalty of a backwards seek */
  26. static const int cfq_back_penalty = 2;
  27. static const int cfq_slice_sync = HZ / 10;
  28. static int cfq_slice_async = HZ / 25;
  29. static const int cfq_slice_async_rq = 2;
  30. static int cfq_slice_idle = HZ / 125;
  31. static const int cfq_target_latency = HZ * 3/10; /* 300 ms */
  32. static const int cfq_hist_divisor = 4;
  33. /*
  34. * offset from end of service tree
  35. */
  36. #define CFQ_IDLE_DELAY (HZ / 5)
  37. /*
  38. * below this threshold, we consider thinktime immediate
  39. */
  40. #define CFQ_MIN_TT (2)
  41. /*
  42. * Allow merged cfqqs to perform this amount of seeky I/O before
  43. * deciding to break the queues up again.
  44. */
  45. #define CFQQ_COOP_TOUT (HZ)
  46. #define CFQ_SLICE_SCALE (5)
  47. #define CFQ_HW_QUEUE_MIN (5)
  48. #define CFQ_SERVICE_SHIFT 12
  49. #define RQ_CIC(rq) \
  50. ((struct cfq_io_context *) (rq)->elevator_private)
  51. #define RQ_CFQQ(rq) (struct cfq_queue *) ((rq)->elevator_private2)
  52. static struct kmem_cache *cfq_pool;
  53. static struct kmem_cache *cfq_ioc_pool;
  54. static DEFINE_PER_CPU(unsigned long, cfq_ioc_count);
  55. static struct completion *ioc_gone;
  56. static DEFINE_SPINLOCK(ioc_gone_lock);
  57. #define CFQ_PRIO_LISTS IOPRIO_BE_NR
  58. #define cfq_class_idle(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_IDLE)
  59. #define cfq_class_rt(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_RT)
  60. #define sample_valid(samples) ((samples) > 80)
  61. #define rb_entry_cfqg(node) rb_entry((node), struct cfq_group, rb_node)
  62. /*
  63. * Most of our rbtree usage is for sorting with min extraction, so
  64. * if we cache the leftmost node we don't have to walk down the tree
  65. * to find it. Idea borrowed from Ingo Molnars CFS scheduler. We should
  66. * move this into the elevator for the rq sorting as well.
  67. */
  68. struct cfq_rb_root {
  69. struct rb_root rb;
  70. struct rb_node *left;
  71. unsigned count;
  72. u64 min_vdisktime;
  73. struct rb_node *active;
  74. unsigned total_weight;
  75. };
  76. #define CFQ_RB_ROOT (struct cfq_rb_root) { RB_ROOT, NULL, 0, 0, }
  77. /*
  78. * Per process-grouping structure
  79. */
  80. struct cfq_queue {
  81. /* reference count */
  82. atomic_t ref;
  83. /* various state flags, see below */
  84. unsigned int flags;
  85. /* parent cfq_data */
  86. struct cfq_data *cfqd;
  87. /* service_tree member */
  88. struct rb_node rb_node;
  89. /* service_tree key */
  90. unsigned long rb_key;
  91. /* prio tree member */
  92. struct rb_node p_node;
  93. /* prio tree root we belong to, if any */
  94. struct rb_root *p_root;
  95. /* sorted list of pending requests */
  96. struct rb_root sort_list;
  97. /* if fifo isn't expired, next request to serve */
  98. struct request *next_rq;
  99. /* requests queued in sort_list */
  100. int queued[2];
  101. /* currently allocated requests */
  102. int allocated[2];
  103. /* fifo list of requests in sort_list */
  104. struct list_head fifo;
  105. /* time when queue got scheduled in to dispatch first request. */
  106. unsigned long dispatch_start;
  107. /* time when first request from queue completed and slice started. */
  108. unsigned long slice_start;
  109. unsigned long slice_end;
  110. long slice_resid;
  111. unsigned int slice_dispatch;
  112. /* pending metadata requests */
  113. int meta_pending;
  114. /* number of requests that are on the dispatch list or inside driver */
  115. int dispatched;
  116. /* io prio of this group */
  117. unsigned short ioprio, org_ioprio;
  118. unsigned short ioprio_class, org_ioprio_class;
  119. unsigned int seek_samples;
  120. u64 seek_total;
  121. sector_t seek_mean;
  122. sector_t last_request_pos;
  123. unsigned long seeky_start;
  124. pid_t pid;
  125. struct cfq_rb_root *service_tree;
  126. struct cfq_queue *new_cfqq;
  127. struct cfq_group *cfqg;
  128. };
  129. /*
  130. * First index in the service_trees.
  131. * IDLE is handled separately, so it has negative index
  132. */
  133. enum wl_prio_t {
  134. BE_WORKLOAD = 0,
  135. RT_WORKLOAD = 1,
  136. IDLE_WORKLOAD = 2,
  137. };
  138. /*
  139. * Second index in the service_trees.
  140. */
  141. enum wl_type_t {
  142. ASYNC_WORKLOAD = 0,
  143. SYNC_NOIDLE_WORKLOAD = 1,
  144. SYNC_WORKLOAD = 2
  145. };
  146. /* This is per cgroup per device grouping structure */
  147. struct cfq_group {
  148. /* group service_tree member */
  149. struct rb_node rb_node;
  150. /* group service_tree key */
  151. u64 vdisktime;
  152. unsigned int weight;
  153. bool on_st;
  154. /* number of cfqq currently on this group */
  155. int nr_cfqq;
  156. /* Per group busy queus average. Useful for workload slice calc. */
  157. unsigned int busy_queues_avg[2];
  158. /*
  159. * rr lists of queues with requests, onle rr for each priority class.
  160. * Counts are embedded in the cfq_rb_root
  161. */
  162. struct cfq_rb_root service_trees[2][3];
  163. struct cfq_rb_root service_tree_idle;
  164. unsigned long saved_workload_slice;
  165. enum wl_type_t saved_workload;
  166. enum wl_prio_t saved_serving_prio;
  167. struct blkio_group blkg;
  168. #ifdef CONFIG_CFQ_GROUP_IOSCHED
  169. struct hlist_node cfqd_node;
  170. atomic_t ref;
  171. #endif
  172. };
  173. /*
  174. * Per block device queue structure
  175. */
  176. struct cfq_data {
  177. struct request_queue *queue;
  178. /* Root service tree for cfq_groups */
  179. struct cfq_rb_root grp_service_tree;
  180. struct cfq_group root_group;
  181. /* Number of active cfq groups on group service tree */
  182. int nr_groups;
  183. /*
  184. * The priority currently being served
  185. */
  186. enum wl_prio_t serving_prio;
  187. enum wl_type_t serving_type;
  188. unsigned long workload_expires;
  189. struct cfq_group *serving_group;
  190. bool noidle_tree_requires_idle;
  191. /*
  192. * Each priority tree is sorted by next_request position. These
  193. * trees are used when determining if two or more queues are
  194. * interleaving requests (see cfq_close_cooperator).
  195. */
  196. struct rb_root prio_trees[CFQ_PRIO_LISTS];
  197. unsigned int busy_queues;
  198. int rq_in_driver[2];
  199. int sync_flight;
  200. /*
  201. * queue-depth detection
  202. */
  203. int rq_queued;
  204. int hw_tag;
  205. /*
  206. * hw_tag can be
  207. * -1 => indeterminate, (cfq will behave as if NCQ is present, to allow better detection)
  208. * 1 => NCQ is present (hw_tag_est_depth is the estimated max depth)
  209. * 0 => no NCQ
  210. */
  211. int hw_tag_est_depth;
  212. unsigned int hw_tag_samples;
  213. /*
  214. * idle window management
  215. */
  216. struct timer_list idle_slice_timer;
  217. struct work_struct unplug_work;
  218. struct cfq_queue *active_queue;
  219. struct cfq_io_context *active_cic;
  220. /*
  221. * async queue for each priority case
  222. */
  223. struct cfq_queue *async_cfqq[2][IOPRIO_BE_NR];
  224. struct cfq_queue *async_idle_cfqq;
  225. sector_t last_position;
  226. /*
  227. * tunables, see top of file
  228. */
  229. unsigned int cfq_quantum;
  230. unsigned int cfq_fifo_expire[2];
  231. unsigned int cfq_back_penalty;
  232. unsigned int cfq_back_max;
  233. unsigned int cfq_slice[2];
  234. unsigned int cfq_slice_async_rq;
  235. unsigned int cfq_slice_idle;
  236. unsigned int cfq_latency;
  237. struct list_head cic_list;
  238. /*
  239. * Fallback dummy cfqq for extreme OOM conditions
  240. */
  241. struct cfq_queue oom_cfqq;
  242. unsigned long last_end_sync_rq;
  243. /* List of cfq groups being managed on this device*/
  244. struct hlist_head cfqg_list;
  245. };
  246. static struct cfq_group *cfq_get_next_cfqg(struct cfq_data *cfqd);
  247. static struct cfq_rb_root *service_tree_for(struct cfq_group *cfqg,
  248. enum wl_prio_t prio,
  249. enum wl_type_t type,
  250. struct cfq_data *cfqd)
  251. {
  252. if (!cfqg)
  253. return NULL;
  254. if (prio == IDLE_WORKLOAD)
  255. return &cfqg->service_tree_idle;
  256. return &cfqg->service_trees[prio][type];
  257. }
  258. enum cfqq_state_flags {
  259. CFQ_CFQQ_FLAG_on_rr = 0, /* on round-robin busy list */
  260. CFQ_CFQQ_FLAG_wait_request, /* waiting for a request */
  261. CFQ_CFQQ_FLAG_must_dispatch, /* must be allowed a dispatch */
  262. CFQ_CFQQ_FLAG_must_alloc_slice, /* per-slice must_alloc flag */
  263. CFQ_CFQQ_FLAG_fifo_expire, /* FIFO checked in this slice */
  264. CFQ_CFQQ_FLAG_idle_window, /* slice idling enabled */
  265. CFQ_CFQQ_FLAG_prio_changed, /* task priority has changed */
  266. CFQ_CFQQ_FLAG_slice_new, /* no requests dispatched in slice */
  267. CFQ_CFQQ_FLAG_sync, /* synchronous queue */
  268. CFQ_CFQQ_FLAG_coop, /* cfqq is shared */
  269. CFQ_CFQQ_FLAG_deep, /* sync cfqq experienced large depth */
  270. };
  271. #define CFQ_CFQQ_FNS(name) \
  272. static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq) \
  273. { \
  274. (cfqq)->flags |= (1 << CFQ_CFQQ_FLAG_##name); \
  275. } \
  276. static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq) \
  277. { \
  278. (cfqq)->flags &= ~(1 << CFQ_CFQQ_FLAG_##name); \
  279. } \
  280. static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq) \
  281. { \
  282. return ((cfqq)->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0; \
  283. }
  284. CFQ_CFQQ_FNS(on_rr);
  285. CFQ_CFQQ_FNS(wait_request);
  286. CFQ_CFQQ_FNS(must_dispatch);
  287. CFQ_CFQQ_FNS(must_alloc_slice);
  288. CFQ_CFQQ_FNS(fifo_expire);
  289. CFQ_CFQQ_FNS(idle_window);
  290. CFQ_CFQQ_FNS(prio_changed);
  291. CFQ_CFQQ_FNS(slice_new);
  292. CFQ_CFQQ_FNS(sync);
  293. CFQ_CFQQ_FNS(coop);
  294. CFQ_CFQQ_FNS(deep);
  295. #undef CFQ_CFQQ_FNS
  296. #ifdef CONFIG_DEBUG_CFQ_IOSCHED
  297. #define cfq_log_cfqq(cfqd, cfqq, fmt, args...) \
  298. blk_add_trace_msg((cfqd)->queue, "cfq%d%c %s " fmt, (cfqq)->pid, \
  299. cfq_cfqq_sync((cfqq)) ? 'S' : 'A', \
  300. blkg_path(&(cfqq)->cfqg->blkg), ##args);
  301. #define cfq_log_cfqg(cfqd, cfqg, fmt, args...) \
  302. blk_add_trace_msg((cfqd)->queue, "%s " fmt, \
  303. blkg_path(&(cfqg)->blkg), ##args); \
  304. #else
  305. #define cfq_log_cfqq(cfqd, cfqq, fmt, args...) \
  306. blk_add_trace_msg((cfqd)->queue, "cfq%d " fmt, (cfqq)->pid, ##args)
  307. #define cfq_log_cfqg(cfqd, cfqg, fmt, args...) do {} while (0);
  308. #endif
  309. #define cfq_log(cfqd, fmt, args...) \
  310. blk_add_trace_msg((cfqd)->queue, "cfq " fmt, ##args)
  311. /* Traverses through cfq group service trees */
  312. #define for_each_cfqg_st(cfqg, i, j, st) \
  313. for (i = 0; i <= IDLE_WORKLOAD; i++) \
  314. for (j = 0, st = i < IDLE_WORKLOAD ? &cfqg->service_trees[i][j]\
  315. : &cfqg->service_tree_idle; \
  316. (i < IDLE_WORKLOAD && j <= SYNC_WORKLOAD) || \
  317. (i == IDLE_WORKLOAD && j == 0); \
  318. j++, st = i < IDLE_WORKLOAD ? \
  319. &cfqg->service_trees[i][j]: NULL) \
  320. static inline enum wl_prio_t cfqq_prio(struct cfq_queue *cfqq)
  321. {
  322. if (cfq_class_idle(cfqq))
  323. return IDLE_WORKLOAD;
  324. if (cfq_class_rt(cfqq))
  325. return RT_WORKLOAD;
  326. return BE_WORKLOAD;
  327. }
  328. static enum wl_type_t cfqq_type(struct cfq_queue *cfqq)
  329. {
  330. if (!cfq_cfqq_sync(cfqq))
  331. return ASYNC_WORKLOAD;
  332. if (!cfq_cfqq_idle_window(cfqq))
  333. return SYNC_NOIDLE_WORKLOAD;
  334. return SYNC_WORKLOAD;
  335. }
  336. static inline int cfq_group_busy_queues_wl(enum wl_prio_t wl,
  337. struct cfq_data *cfqd,
  338. struct cfq_group *cfqg)
  339. {
  340. if (wl == IDLE_WORKLOAD)
  341. return cfqg->service_tree_idle.count;
  342. return cfqg->service_trees[wl][ASYNC_WORKLOAD].count
  343. + cfqg->service_trees[wl][SYNC_NOIDLE_WORKLOAD].count
  344. + cfqg->service_trees[wl][SYNC_WORKLOAD].count;
  345. }
  346. static void cfq_dispatch_insert(struct request_queue *, struct request *);
  347. static struct cfq_queue *cfq_get_queue(struct cfq_data *, bool,
  348. struct io_context *, gfp_t);
  349. static struct cfq_io_context *cfq_cic_lookup(struct cfq_data *,
  350. struct io_context *);
  351. static inline int rq_in_driver(struct cfq_data *cfqd)
  352. {
  353. return cfqd->rq_in_driver[0] + cfqd->rq_in_driver[1];
  354. }
  355. static inline struct cfq_queue *cic_to_cfqq(struct cfq_io_context *cic,
  356. bool is_sync)
  357. {
  358. return cic->cfqq[is_sync];
  359. }
  360. static inline void cic_set_cfqq(struct cfq_io_context *cic,
  361. struct cfq_queue *cfqq, bool is_sync)
  362. {
  363. cic->cfqq[is_sync] = cfqq;
  364. }
  365. /*
  366. * We regard a request as SYNC, if it's either a read or has the SYNC bit
  367. * set (in which case it could also be direct WRITE).
  368. */
  369. static inline bool cfq_bio_sync(struct bio *bio)
  370. {
  371. return bio_data_dir(bio) == READ || bio_rw_flagged(bio, BIO_RW_SYNCIO);
  372. }
  373. /*
  374. * scheduler run of queue, if there are requests pending and no one in the
  375. * driver that will restart queueing
  376. */
  377. static inline void cfq_schedule_dispatch(struct cfq_data *cfqd)
  378. {
  379. if (cfqd->busy_queues) {
  380. cfq_log(cfqd, "schedule dispatch");
  381. kblockd_schedule_work(cfqd->queue, &cfqd->unplug_work);
  382. }
  383. }
  384. static int cfq_queue_empty(struct request_queue *q)
  385. {
  386. struct cfq_data *cfqd = q->elevator->elevator_data;
  387. return !cfqd->rq_queued;
  388. }
  389. /*
  390. * Scale schedule slice based on io priority. Use the sync time slice only
  391. * if a queue is marked sync and has sync io queued. A sync queue with async
  392. * io only, should not get full sync slice length.
  393. */
  394. static inline int cfq_prio_slice(struct cfq_data *cfqd, bool sync,
  395. unsigned short prio)
  396. {
  397. const int base_slice = cfqd->cfq_slice[sync];
  398. WARN_ON(prio >= IOPRIO_BE_NR);
  399. return base_slice + (base_slice/CFQ_SLICE_SCALE * (4 - prio));
  400. }
  401. static inline int
  402. cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  403. {
  404. return cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio);
  405. }
  406. static inline u64 cfq_scale_slice(unsigned long delta, struct cfq_group *cfqg)
  407. {
  408. u64 d = delta << CFQ_SERVICE_SHIFT;
  409. d = d * BLKIO_WEIGHT_DEFAULT;
  410. do_div(d, cfqg->weight);
  411. return d;
  412. }
  413. static inline u64 max_vdisktime(u64 min_vdisktime, u64 vdisktime)
  414. {
  415. s64 delta = (s64)(vdisktime - min_vdisktime);
  416. if (delta > 0)
  417. min_vdisktime = vdisktime;
  418. return min_vdisktime;
  419. }
  420. static inline u64 min_vdisktime(u64 min_vdisktime, u64 vdisktime)
  421. {
  422. s64 delta = (s64)(vdisktime - min_vdisktime);
  423. if (delta < 0)
  424. min_vdisktime = vdisktime;
  425. return min_vdisktime;
  426. }
  427. static void update_min_vdisktime(struct cfq_rb_root *st)
  428. {
  429. u64 vdisktime = st->min_vdisktime;
  430. struct cfq_group *cfqg;
  431. if (st->active) {
  432. cfqg = rb_entry_cfqg(st->active);
  433. vdisktime = cfqg->vdisktime;
  434. }
  435. if (st->left) {
  436. cfqg = rb_entry_cfqg(st->left);
  437. vdisktime = min_vdisktime(vdisktime, cfqg->vdisktime);
  438. }
  439. st->min_vdisktime = max_vdisktime(st->min_vdisktime, vdisktime);
  440. }
  441. /*
  442. * get averaged number of queues of RT/BE priority.
  443. * average is updated, with a formula that gives more weight to higher numbers,
  444. * to quickly follows sudden increases and decrease slowly
  445. */
  446. static inline unsigned cfq_group_get_avg_queues(struct cfq_data *cfqd,
  447. struct cfq_group *cfqg, bool rt)
  448. {
  449. unsigned min_q, max_q;
  450. unsigned mult = cfq_hist_divisor - 1;
  451. unsigned round = cfq_hist_divisor / 2;
  452. unsigned busy = cfq_group_busy_queues_wl(rt, cfqd, cfqg);
  453. min_q = min(cfqg->busy_queues_avg[rt], busy);
  454. max_q = max(cfqg->busy_queues_avg[rt], busy);
  455. cfqg->busy_queues_avg[rt] = (mult * max_q + min_q + round) /
  456. cfq_hist_divisor;
  457. return cfqg->busy_queues_avg[rt];
  458. }
  459. static inline unsigned
  460. cfq_group_slice(struct cfq_data *cfqd, struct cfq_group *cfqg)
  461. {
  462. struct cfq_rb_root *st = &cfqd->grp_service_tree;
  463. return cfq_target_latency * cfqg->weight / st->total_weight;
  464. }
  465. static inline void
  466. cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  467. {
  468. unsigned slice = cfq_prio_to_slice(cfqd, cfqq);
  469. if (cfqd->cfq_latency) {
  470. /*
  471. * interested queues (we consider only the ones with the same
  472. * priority class in the cfq group)
  473. */
  474. unsigned iq = cfq_group_get_avg_queues(cfqd, cfqq->cfqg,
  475. cfq_class_rt(cfqq));
  476. unsigned sync_slice = cfqd->cfq_slice[1];
  477. unsigned expect_latency = sync_slice * iq;
  478. unsigned group_slice = cfq_group_slice(cfqd, cfqq->cfqg);
  479. if (expect_latency > group_slice) {
  480. unsigned base_low_slice = 2 * cfqd->cfq_slice_idle;
  481. /* scale low_slice according to IO priority
  482. * and sync vs async */
  483. unsigned low_slice =
  484. min(slice, base_low_slice * slice / sync_slice);
  485. /* the adapted slice value is scaled to fit all iqs
  486. * into the target latency */
  487. slice = max(slice * group_slice / expect_latency,
  488. low_slice);
  489. }
  490. }
  491. cfqq->slice_start = jiffies;
  492. cfqq->slice_end = jiffies + slice;
  493. cfq_log_cfqq(cfqd, cfqq, "set_slice=%lu", cfqq->slice_end - jiffies);
  494. }
  495. /*
  496. * We need to wrap this check in cfq_cfqq_slice_new(), since ->slice_end
  497. * isn't valid until the first request from the dispatch is activated
  498. * and the slice time set.
  499. */
  500. static inline bool cfq_slice_used(struct cfq_queue *cfqq)
  501. {
  502. if (cfq_cfqq_slice_new(cfqq))
  503. return 0;
  504. if (time_before(jiffies, cfqq->slice_end))
  505. return 0;
  506. return 1;
  507. }
  508. /*
  509. * Lifted from AS - choose which of rq1 and rq2 that is best served now.
  510. * We choose the request that is closest to the head right now. Distance
  511. * behind the head is penalized and only allowed to a certain extent.
  512. */
  513. static struct request *
  514. cfq_choose_req(struct cfq_data *cfqd, struct request *rq1, struct request *rq2, sector_t last)
  515. {
  516. sector_t s1, s2, d1 = 0, d2 = 0;
  517. unsigned long back_max;
  518. #define CFQ_RQ1_WRAP 0x01 /* request 1 wraps */
  519. #define CFQ_RQ2_WRAP 0x02 /* request 2 wraps */
  520. unsigned wrap = 0; /* bit mask: requests behind the disk head? */
  521. if (rq1 == NULL || rq1 == rq2)
  522. return rq2;
  523. if (rq2 == NULL)
  524. return rq1;
  525. if (rq_is_sync(rq1) && !rq_is_sync(rq2))
  526. return rq1;
  527. else if (rq_is_sync(rq2) && !rq_is_sync(rq1))
  528. return rq2;
  529. if (rq_is_meta(rq1) && !rq_is_meta(rq2))
  530. return rq1;
  531. else if (rq_is_meta(rq2) && !rq_is_meta(rq1))
  532. return rq2;
  533. s1 = blk_rq_pos(rq1);
  534. s2 = blk_rq_pos(rq2);
  535. /*
  536. * by definition, 1KiB is 2 sectors
  537. */
  538. back_max = cfqd->cfq_back_max * 2;
  539. /*
  540. * Strict one way elevator _except_ in the case where we allow
  541. * short backward seeks which are biased as twice the cost of a
  542. * similar forward seek.
  543. */
  544. if (s1 >= last)
  545. d1 = s1 - last;
  546. else if (s1 + back_max >= last)
  547. d1 = (last - s1) * cfqd->cfq_back_penalty;
  548. else
  549. wrap |= CFQ_RQ1_WRAP;
  550. if (s2 >= last)
  551. d2 = s2 - last;
  552. else if (s2 + back_max >= last)
  553. d2 = (last - s2) * cfqd->cfq_back_penalty;
  554. else
  555. wrap |= CFQ_RQ2_WRAP;
  556. /* Found required data */
  557. /*
  558. * By doing switch() on the bit mask "wrap" we avoid having to
  559. * check two variables for all permutations: --> faster!
  560. */
  561. switch (wrap) {
  562. case 0: /* common case for CFQ: rq1 and rq2 not wrapped */
  563. if (d1 < d2)
  564. return rq1;
  565. else if (d2 < d1)
  566. return rq2;
  567. else {
  568. if (s1 >= s2)
  569. return rq1;
  570. else
  571. return rq2;
  572. }
  573. case CFQ_RQ2_WRAP:
  574. return rq1;
  575. case CFQ_RQ1_WRAP:
  576. return rq2;
  577. case (CFQ_RQ1_WRAP|CFQ_RQ2_WRAP): /* both rqs wrapped */
  578. default:
  579. /*
  580. * Since both rqs are wrapped,
  581. * start with the one that's further behind head
  582. * (--> only *one* back seek required),
  583. * since back seek takes more time than forward.
  584. */
  585. if (s1 <= s2)
  586. return rq1;
  587. else
  588. return rq2;
  589. }
  590. }
  591. /*
  592. * The below is leftmost cache rbtree addon
  593. */
  594. static struct cfq_queue *cfq_rb_first(struct cfq_rb_root *root)
  595. {
  596. /* Service tree is empty */
  597. if (!root->count)
  598. return NULL;
  599. if (!root->left)
  600. root->left = rb_first(&root->rb);
  601. if (root->left)
  602. return rb_entry(root->left, struct cfq_queue, rb_node);
  603. return NULL;
  604. }
  605. static struct cfq_group *cfq_rb_first_group(struct cfq_rb_root *root)
  606. {
  607. if (!root->left)
  608. root->left = rb_first(&root->rb);
  609. if (root->left)
  610. return rb_entry_cfqg(root->left);
  611. return NULL;
  612. }
  613. static void rb_erase_init(struct rb_node *n, struct rb_root *root)
  614. {
  615. rb_erase(n, root);
  616. RB_CLEAR_NODE(n);
  617. }
  618. static void cfq_rb_erase(struct rb_node *n, struct cfq_rb_root *root)
  619. {
  620. if (root->left == n)
  621. root->left = NULL;
  622. rb_erase_init(n, &root->rb);
  623. --root->count;
  624. }
  625. /*
  626. * would be nice to take fifo expire time into account as well
  627. */
  628. static struct request *
  629. cfq_find_next_rq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
  630. struct request *last)
  631. {
  632. struct rb_node *rbnext = rb_next(&last->rb_node);
  633. struct rb_node *rbprev = rb_prev(&last->rb_node);
  634. struct request *next = NULL, *prev = NULL;
  635. BUG_ON(RB_EMPTY_NODE(&last->rb_node));
  636. if (rbprev)
  637. prev = rb_entry_rq(rbprev);
  638. if (rbnext)
  639. next = rb_entry_rq(rbnext);
  640. else {
  641. rbnext = rb_first(&cfqq->sort_list);
  642. if (rbnext && rbnext != &last->rb_node)
  643. next = rb_entry_rq(rbnext);
  644. }
  645. return cfq_choose_req(cfqd, next, prev, blk_rq_pos(last));
  646. }
  647. static unsigned long cfq_slice_offset(struct cfq_data *cfqd,
  648. struct cfq_queue *cfqq)
  649. {
  650. /*
  651. * just an approximation, should be ok.
  652. */
  653. return (cfqq->cfqg->nr_cfqq - 1) * (cfq_prio_slice(cfqd, 1, 0) -
  654. cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio));
  655. }
  656. static inline s64
  657. cfqg_key(struct cfq_rb_root *st, struct cfq_group *cfqg)
  658. {
  659. return cfqg->vdisktime - st->min_vdisktime;
  660. }
  661. static void
  662. __cfq_group_service_tree_add(struct cfq_rb_root *st, struct cfq_group *cfqg)
  663. {
  664. struct rb_node **node = &st->rb.rb_node;
  665. struct rb_node *parent = NULL;
  666. struct cfq_group *__cfqg;
  667. s64 key = cfqg_key(st, cfqg);
  668. int left = 1;
  669. while (*node != NULL) {
  670. parent = *node;
  671. __cfqg = rb_entry_cfqg(parent);
  672. if (key < cfqg_key(st, __cfqg))
  673. node = &parent->rb_left;
  674. else {
  675. node = &parent->rb_right;
  676. left = 0;
  677. }
  678. }
  679. if (left)
  680. st->left = &cfqg->rb_node;
  681. rb_link_node(&cfqg->rb_node, parent, node);
  682. rb_insert_color(&cfqg->rb_node, &st->rb);
  683. }
  684. static void
  685. cfq_group_service_tree_add(struct cfq_data *cfqd, struct cfq_group *cfqg)
  686. {
  687. struct cfq_rb_root *st = &cfqd->grp_service_tree;
  688. struct cfq_group *__cfqg;
  689. struct rb_node *n;
  690. cfqg->nr_cfqq++;
  691. if (cfqg->on_st)
  692. return;
  693. /*
  694. * Currently put the group at the end. Later implement something
  695. * so that groups get lesser vtime based on their weights, so that
  696. * if group does not loose all if it was not continously backlogged.
  697. */
  698. n = rb_last(&st->rb);
  699. if (n) {
  700. __cfqg = rb_entry_cfqg(n);
  701. cfqg->vdisktime = __cfqg->vdisktime + CFQ_IDLE_DELAY;
  702. } else
  703. cfqg->vdisktime = st->min_vdisktime;
  704. __cfq_group_service_tree_add(st, cfqg);
  705. cfqg->on_st = true;
  706. cfqd->nr_groups++;
  707. st->total_weight += cfqg->weight;
  708. }
  709. static void
  710. cfq_group_service_tree_del(struct cfq_data *cfqd, struct cfq_group *cfqg)
  711. {
  712. struct cfq_rb_root *st = &cfqd->grp_service_tree;
  713. if (st->active == &cfqg->rb_node)
  714. st->active = NULL;
  715. BUG_ON(cfqg->nr_cfqq < 1);
  716. cfqg->nr_cfqq--;
  717. /* If there are other cfq queues under this group, don't delete it */
  718. if (cfqg->nr_cfqq)
  719. return;
  720. cfq_log_cfqg(cfqd, cfqg, "del_from_rr group");
  721. cfqg->on_st = false;
  722. cfqd->nr_groups--;
  723. st->total_weight -= cfqg->weight;
  724. if (!RB_EMPTY_NODE(&cfqg->rb_node))
  725. cfq_rb_erase(&cfqg->rb_node, st);
  726. cfqg->saved_workload_slice = 0;
  727. }
  728. static inline unsigned int cfq_cfqq_slice_usage(struct cfq_queue *cfqq)
  729. {
  730. unsigned int slice_used, allocated_slice;
  731. /*
  732. * Queue got expired before even a single request completed or
  733. * got expired immediately after first request completion.
  734. */
  735. if (!cfqq->slice_start || cfqq->slice_start == jiffies) {
  736. /*
  737. * Also charge the seek time incurred to the group, otherwise
  738. * if there are mutiple queues in the group, each can dispatch
  739. * a single request on seeky media and cause lots of seek time
  740. * and group will never know it.
  741. */
  742. slice_used = max_t(unsigned, (jiffies - cfqq->dispatch_start),
  743. 1);
  744. } else {
  745. slice_used = jiffies - cfqq->slice_start;
  746. allocated_slice = cfqq->slice_end - cfqq->slice_start;
  747. if (slice_used > allocated_slice)
  748. slice_used = allocated_slice;
  749. }
  750. cfq_log_cfqq(cfqq->cfqd, cfqq, "sl_used=%u", slice_used);
  751. return slice_used;
  752. }
  753. static void cfq_group_served(struct cfq_data *cfqd, struct cfq_group *cfqg,
  754. struct cfq_queue *cfqq)
  755. {
  756. struct cfq_rb_root *st = &cfqd->grp_service_tree;
  757. unsigned int used_sl;
  758. used_sl = cfq_cfqq_slice_usage(cfqq);
  759. /* Can't update vdisktime while group is on service tree */
  760. cfq_rb_erase(&cfqg->rb_node, st);
  761. cfqg->vdisktime += cfq_scale_slice(used_sl, cfqg);
  762. __cfq_group_service_tree_add(st, cfqg);
  763. /* This group is being expired. Save the context */
  764. if (time_after(cfqd->workload_expires, jiffies)) {
  765. cfqg->saved_workload_slice = cfqd->workload_expires
  766. - jiffies;
  767. cfqg->saved_workload = cfqd->serving_type;
  768. cfqg->saved_serving_prio = cfqd->serving_prio;
  769. } else
  770. cfqg->saved_workload_slice = 0;
  771. cfq_log_cfqg(cfqd, cfqg, "served: vt=%llu min_vt=%llu", cfqg->vdisktime,
  772. st->min_vdisktime);
  773. }
  774. #ifdef CONFIG_CFQ_GROUP_IOSCHED
  775. static inline struct cfq_group *cfqg_of_blkg(struct blkio_group *blkg)
  776. {
  777. if (blkg)
  778. return container_of(blkg, struct cfq_group, blkg);
  779. return NULL;
  780. }
  781. static struct cfq_group *
  782. cfq_find_alloc_cfqg(struct cfq_data *cfqd, struct cgroup *cgroup, int create)
  783. {
  784. struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
  785. struct cfq_group *cfqg = NULL;
  786. void *key = cfqd;
  787. int i, j;
  788. struct cfq_rb_root *st;
  789. /* Do we need to take this reference */
  790. if (!css_tryget(&blkcg->css))
  791. return NULL;;
  792. cfqg = cfqg_of_blkg(blkiocg_lookup_group(blkcg, key));
  793. if (cfqg || !create)
  794. goto done;
  795. cfqg = kzalloc_node(sizeof(*cfqg), GFP_ATOMIC, cfqd->queue->node);
  796. if (!cfqg)
  797. goto done;
  798. cfqg->weight = blkcg->weight;
  799. for_each_cfqg_st(cfqg, i, j, st)
  800. *st = CFQ_RB_ROOT;
  801. RB_CLEAR_NODE(&cfqg->rb_node);
  802. /*
  803. * Take the initial reference that will be released on destroy
  804. * This can be thought of a joint reference by cgroup and
  805. * elevator which will be dropped by either elevator exit
  806. * or cgroup deletion path depending on who is exiting first.
  807. */
  808. atomic_set(&cfqg->ref, 1);
  809. /* Add group onto cgroup list */
  810. blkiocg_add_blkio_group(blkcg, &cfqg->blkg, (void *)cfqd);
  811. /* Add group on cfqd list */
  812. hlist_add_head(&cfqg->cfqd_node, &cfqd->cfqg_list);
  813. done:
  814. css_put(&blkcg->css);
  815. return cfqg;
  816. }
  817. /*
  818. * Search for the cfq group current task belongs to. If create = 1, then also
  819. * create the cfq group if it does not exist. request_queue lock must be held.
  820. */
  821. static struct cfq_group *cfq_get_cfqg(struct cfq_data *cfqd, int create)
  822. {
  823. struct cgroup *cgroup;
  824. struct cfq_group *cfqg = NULL;
  825. rcu_read_lock();
  826. cgroup = task_cgroup(current, blkio_subsys_id);
  827. cfqg = cfq_find_alloc_cfqg(cfqd, cgroup, create);
  828. if (!cfqg && create)
  829. cfqg = &cfqd->root_group;
  830. rcu_read_unlock();
  831. return cfqg;
  832. }
  833. static void cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg)
  834. {
  835. /* Currently, all async queues are mapped to root group */
  836. if (!cfq_cfqq_sync(cfqq))
  837. cfqg = &cfqq->cfqd->root_group;
  838. cfqq->cfqg = cfqg;
  839. /* cfqq reference on cfqg */
  840. atomic_inc(&cfqq->cfqg->ref);
  841. }
  842. static void cfq_put_cfqg(struct cfq_group *cfqg)
  843. {
  844. struct cfq_rb_root *st;
  845. int i, j;
  846. BUG_ON(atomic_read(&cfqg->ref) <= 0);
  847. if (!atomic_dec_and_test(&cfqg->ref))
  848. return;
  849. for_each_cfqg_st(cfqg, i, j, st)
  850. BUG_ON(!RB_EMPTY_ROOT(&st->rb) || st->active != NULL);
  851. kfree(cfqg);
  852. }
  853. static void cfq_destroy_cfqg(struct cfq_data *cfqd, struct cfq_group *cfqg)
  854. {
  855. /* Something wrong if we are trying to remove same group twice */
  856. BUG_ON(hlist_unhashed(&cfqg->cfqd_node));
  857. hlist_del_init(&cfqg->cfqd_node);
  858. /*
  859. * Put the reference taken at the time of creation so that when all
  860. * queues are gone, group can be destroyed.
  861. */
  862. cfq_put_cfqg(cfqg);
  863. }
  864. static void cfq_release_cfq_groups(struct cfq_data *cfqd)
  865. {
  866. struct hlist_node *pos, *n;
  867. struct cfq_group *cfqg;
  868. hlist_for_each_entry_safe(cfqg, pos, n, &cfqd->cfqg_list, cfqd_node) {
  869. /*
  870. * If cgroup removal path got to blk_group first and removed
  871. * it from cgroup list, then it will take care of destroying
  872. * cfqg also.
  873. */
  874. if (!blkiocg_del_blkio_group(&cfqg->blkg))
  875. cfq_destroy_cfqg(cfqd, cfqg);
  876. }
  877. }
  878. /*
  879. * Blk cgroup controller notification saying that blkio_group object is being
  880. * delinked as associated cgroup object is going away. That also means that
  881. * no new IO will come in this group. So get rid of this group as soon as
  882. * any pending IO in the group is finished.
  883. *
  884. * This function is called under rcu_read_lock(). key is the rcu protected
  885. * pointer. That means "key" is a valid cfq_data pointer as long as we are rcu
  886. * read lock.
  887. *
  888. * "key" was fetched from blkio_group under blkio_cgroup->lock. That means
  889. * it should not be NULL as even if elevator was exiting, cgroup deltion
  890. * path got to it first.
  891. */
  892. void cfq_unlink_blkio_group(void *key, struct blkio_group *blkg)
  893. {
  894. unsigned long flags;
  895. struct cfq_data *cfqd = key;
  896. spin_lock_irqsave(cfqd->queue->queue_lock, flags);
  897. cfq_destroy_cfqg(cfqd, cfqg_of_blkg(blkg));
  898. spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
  899. }
  900. #else /* GROUP_IOSCHED */
  901. static struct cfq_group *cfq_get_cfqg(struct cfq_data *cfqd, int create)
  902. {
  903. return &cfqd->root_group;
  904. }
  905. static inline void
  906. cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg) {
  907. cfqq->cfqg = cfqg;
  908. }
  909. static void cfq_release_cfq_groups(struct cfq_data *cfqd) {}
  910. static inline void cfq_put_cfqg(struct cfq_group *cfqg) {}
  911. #endif /* GROUP_IOSCHED */
  912. /*
  913. * The cfqd->service_trees holds all pending cfq_queue's that have
  914. * requests waiting to be processed. It is sorted in the order that
  915. * we will service the queues.
  916. */
  917. static void cfq_service_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq,
  918. bool add_front)
  919. {
  920. struct rb_node **p, *parent;
  921. struct cfq_queue *__cfqq;
  922. unsigned long rb_key;
  923. struct cfq_rb_root *service_tree;
  924. int left;
  925. int new_cfqq = 1;
  926. service_tree = service_tree_for(cfqq->cfqg, cfqq_prio(cfqq),
  927. cfqq_type(cfqq), cfqd);
  928. if (cfq_class_idle(cfqq)) {
  929. rb_key = CFQ_IDLE_DELAY;
  930. parent = rb_last(&service_tree->rb);
  931. if (parent && parent != &cfqq->rb_node) {
  932. __cfqq = rb_entry(parent, struct cfq_queue, rb_node);
  933. rb_key += __cfqq->rb_key;
  934. } else
  935. rb_key += jiffies;
  936. } else if (!add_front) {
  937. /*
  938. * Get our rb key offset. Subtract any residual slice
  939. * value carried from last service. A negative resid
  940. * count indicates slice overrun, and this should position
  941. * the next service time further away in the tree.
  942. */
  943. rb_key = cfq_slice_offset(cfqd, cfqq) + jiffies;
  944. rb_key -= cfqq->slice_resid;
  945. cfqq->slice_resid = 0;
  946. } else {
  947. rb_key = -HZ;
  948. __cfqq = cfq_rb_first(service_tree);
  949. rb_key += __cfqq ? __cfqq->rb_key : jiffies;
  950. }
  951. if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
  952. new_cfqq = 0;
  953. /*
  954. * same position, nothing more to do
  955. */
  956. if (rb_key == cfqq->rb_key &&
  957. cfqq->service_tree == service_tree)
  958. return;
  959. cfq_rb_erase(&cfqq->rb_node, cfqq->service_tree);
  960. cfqq->service_tree = NULL;
  961. }
  962. left = 1;
  963. parent = NULL;
  964. cfqq->service_tree = service_tree;
  965. p = &service_tree->rb.rb_node;
  966. while (*p) {
  967. struct rb_node **n;
  968. parent = *p;
  969. __cfqq = rb_entry(parent, struct cfq_queue, rb_node);
  970. /*
  971. * sort by key, that represents service time.
  972. */
  973. if (time_before(rb_key, __cfqq->rb_key))
  974. n = &(*p)->rb_left;
  975. else {
  976. n = &(*p)->rb_right;
  977. left = 0;
  978. }
  979. p = n;
  980. }
  981. if (left)
  982. service_tree->left = &cfqq->rb_node;
  983. cfqq->rb_key = rb_key;
  984. rb_link_node(&cfqq->rb_node, parent, p);
  985. rb_insert_color(&cfqq->rb_node, &service_tree->rb);
  986. service_tree->count++;
  987. if (add_front || !new_cfqq)
  988. return;
  989. cfq_group_service_tree_add(cfqd, cfqq->cfqg);
  990. }
  991. static struct cfq_queue *
  992. cfq_prio_tree_lookup(struct cfq_data *cfqd, struct rb_root *root,
  993. sector_t sector, struct rb_node **ret_parent,
  994. struct rb_node ***rb_link)
  995. {
  996. struct rb_node **p, *parent;
  997. struct cfq_queue *cfqq = NULL;
  998. parent = NULL;
  999. p = &root->rb_node;
  1000. while (*p) {
  1001. struct rb_node **n;
  1002. parent = *p;
  1003. cfqq = rb_entry(parent, struct cfq_queue, p_node);
  1004. /*
  1005. * Sort strictly based on sector. Smallest to the left,
  1006. * largest to the right.
  1007. */
  1008. if (sector > blk_rq_pos(cfqq->next_rq))
  1009. n = &(*p)->rb_right;
  1010. else if (sector < blk_rq_pos(cfqq->next_rq))
  1011. n = &(*p)->rb_left;
  1012. else
  1013. break;
  1014. p = n;
  1015. cfqq = NULL;
  1016. }
  1017. *ret_parent = parent;
  1018. if (rb_link)
  1019. *rb_link = p;
  1020. return cfqq;
  1021. }
  1022. static void cfq_prio_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  1023. {
  1024. struct rb_node **p, *parent;
  1025. struct cfq_queue *__cfqq;
  1026. if (cfqq->p_root) {
  1027. rb_erase(&cfqq->p_node, cfqq->p_root);
  1028. cfqq->p_root = NULL;
  1029. }
  1030. if (cfq_class_idle(cfqq))
  1031. return;
  1032. if (!cfqq->next_rq)
  1033. return;
  1034. cfqq->p_root = &cfqd->prio_trees[cfqq->org_ioprio];
  1035. __cfqq = cfq_prio_tree_lookup(cfqd, cfqq->p_root,
  1036. blk_rq_pos(cfqq->next_rq), &parent, &p);
  1037. if (!__cfqq) {
  1038. rb_link_node(&cfqq->p_node, parent, p);
  1039. rb_insert_color(&cfqq->p_node, cfqq->p_root);
  1040. } else
  1041. cfqq->p_root = NULL;
  1042. }
  1043. /*
  1044. * Update cfqq's position in the service tree.
  1045. */
  1046. static void cfq_resort_rr_list(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  1047. {
  1048. /*
  1049. * Resorting requires the cfqq to be on the RR list already.
  1050. */
  1051. if (cfq_cfqq_on_rr(cfqq)) {
  1052. cfq_service_tree_add(cfqd, cfqq, 0);
  1053. cfq_prio_tree_add(cfqd, cfqq);
  1054. }
  1055. }
  1056. /*
  1057. * add to busy list of queues for service, trying to be fair in ordering
  1058. * the pending list according to last request service
  1059. */
  1060. static void cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  1061. {
  1062. cfq_log_cfqq(cfqd, cfqq, "add_to_rr");
  1063. BUG_ON(cfq_cfqq_on_rr(cfqq));
  1064. cfq_mark_cfqq_on_rr(cfqq);
  1065. cfqd->busy_queues++;
  1066. cfq_resort_rr_list(cfqd, cfqq);
  1067. }
  1068. /*
  1069. * Called when the cfqq no longer has requests pending, remove it from
  1070. * the service tree.
  1071. */
  1072. static void cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  1073. {
  1074. cfq_log_cfqq(cfqd, cfqq, "del_from_rr");
  1075. BUG_ON(!cfq_cfqq_on_rr(cfqq));
  1076. cfq_clear_cfqq_on_rr(cfqq);
  1077. if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
  1078. cfq_rb_erase(&cfqq->rb_node, cfqq->service_tree);
  1079. cfqq->service_tree = NULL;
  1080. }
  1081. if (cfqq->p_root) {
  1082. rb_erase(&cfqq->p_node, cfqq->p_root);
  1083. cfqq->p_root = NULL;
  1084. }
  1085. cfq_group_service_tree_del(cfqd, cfqq->cfqg);
  1086. BUG_ON(!cfqd->busy_queues);
  1087. cfqd->busy_queues--;
  1088. }
  1089. /*
  1090. * rb tree support functions
  1091. */
  1092. static void cfq_del_rq_rb(struct request *rq)
  1093. {
  1094. struct cfq_queue *cfqq = RQ_CFQQ(rq);
  1095. const int sync = rq_is_sync(rq);
  1096. BUG_ON(!cfqq->queued[sync]);
  1097. cfqq->queued[sync]--;
  1098. elv_rb_del(&cfqq->sort_list, rq);
  1099. if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list)) {
  1100. /*
  1101. * Queue will be deleted from service tree when we actually
  1102. * expire it later. Right now just remove it from prio tree
  1103. * as it is empty.
  1104. */
  1105. if (cfqq->p_root) {
  1106. rb_erase(&cfqq->p_node, cfqq->p_root);
  1107. cfqq->p_root = NULL;
  1108. }
  1109. }
  1110. }
  1111. static void cfq_add_rq_rb(struct request *rq)
  1112. {
  1113. struct cfq_queue *cfqq = RQ_CFQQ(rq);
  1114. struct cfq_data *cfqd = cfqq->cfqd;
  1115. struct request *__alias, *prev;
  1116. cfqq->queued[rq_is_sync(rq)]++;
  1117. /*
  1118. * looks a little odd, but the first insert might return an alias.
  1119. * if that happens, put the alias on the dispatch list
  1120. */
  1121. while ((__alias = elv_rb_add(&cfqq->sort_list, rq)) != NULL)
  1122. cfq_dispatch_insert(cfqd->queue, __alias);
  1123. if (!cfq_cfqq_on_rr(cfqq))
  1124. cfq_add_cfqq_rr(cfqd, cfqq);
  1125. /*
  1126. * check if this request is a better next-serve candidate
  1127. */
  1128. prev = cfqq->next_rq;
  1129. cfqq->next_rq = cfq_choose_req(cfqd, cfqq->next_rq, rq, cfqd->last_position);
  1130. /*
  1131. * adjust priority tree position, if ->next_rq changes
  1132. */
  1133. if (prev != cfqq->next_rq)
  1134. cfq_prio_tree_add(cfqd, cfqq);
  1135. BUG_ON(!cfqq->next_rq);
  1136. }
  1137. static void cfq_reposition_rq_rb(struct cfq_queue *cfqq, struct request *rq)
  1138. {
  1139. elv_rb_del(&cfqq->sort_list, rq);
  1140. cfqq->queued[rq_is_sync(rq)]--;
  1141. cfq_add_rq_rb(rq);
  1142. }
  1143. static struct request *
  1144. cfq_find_rq_fmerge(struct cfq_data *cfqd, struct bio *bio)
  1145. {
  1146. struct task_struct *tsk = current;
  1147. struct cfq_io_context *cic;
  1148. struct cfq_queue *cfqq;
  1149. cic = cfq_cic_lookup(cfqd, tsk->io_context);
  1150. if (!cic)
  1151. return NULL;
  1152. cfqq = cic_to_cfqq(cic, cfq_bio_sync(bio));
  1153. if (cfqq) {
  1154. sector_t sector = bio->bi_sector + bio_sectors(bio);
  1155. return elv_rb_find(&cfqq->sort_list, sector);
  1156. }
  1157. return NULL;
  1158. }
  1159. static void cfq_activate_request(struct request_queue *q, struct request *rq)
  1160. {
  1161. struct cfq_data *cfqd = q->elevator->elevator_data;
  1162. cfqd->rq_in_driver[rq_is_sync(rq)]++;
  1163. cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "activate rq, drv=%d",
  1164. rq_in_driver(cfqd));
  1165. cfqd->last_position = blk_rq_pos(rq) + blk_rq_sectors(rq);
  1166. }
  1167. static void cfq_deactivate_request(struct request_queue *q, struct request *rq)
  1168. {
  1169. struct cfq_data *cfqd = q->elevator->elevator_data;
  1170. const int sync = rq_is_sync(rq);
  1171. WARN_ON(!cfqd->rq_in_driver[sync]);
  1172. cfqd->rq_in_driver[sync]--;
  1173. cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "deactivate rq, drv=%d",
  1174. rq_in_driver(cfqd));
  1175. }
  1176. static void cfq_remove_request(struct request *rq)
  1177. {
  1178. struct cfq_queue *cfqq = RQ_CFQQ(rq);
  1179. if (cfqq->next_rq == rq)
  1180. cfqq->next_rq = cfq_find_next_rq(cfqq->cfqd, cfqq, rq);
  1181. list_del_init(&rq->queuelist);
  1182. cfq_del_rq_rb(rq);
  1183. cfqq->cfqd->rq_queued--;
  1184. if (rq_is_meta(rq)) {
  1185. WARN_ON(!cfqq->meta_pending);
  1186. cfqq->meta_pending--;
  1187. }
  1188. }
  1189. static int cfq_merge(struct request_queue *q, struct request **req,
  1190. struct bio *bio)
  1191. {
  1192. struct cfq_data *cfqd = q->elevator->elevator_data;
  1193. struct request *__rq;
  1194. __rq = cfq_find_rq_fmerge(cfqd, bio);
  1195. if (__rq && elv_rq_merge_ok(__rq, bio)) {
  1196. *req = __rq;
  1197. return ELEVATOR_FRONT_MERGE;
  1198. }
  1199. return ELEVATOR_NO_MERGE;
  1200. }
  1201. static void cfq_merged_request(struct request_queue *q, struct request *req,
  1202. int type)
  1203. {
  1204. if (type == ELEVATOR_FRONT_MERGE) {
  1205. struct cfq_queue *cfqq = RQ_CFQQ(req);
  1206. cfq_reposition_rq_rb(cfqq, req);
  1207. }
  1208. }
  1209. static void
  1210. cfq_merged_requests(struct request_queue *q, struct request *rq,
  1211. struct request *next)
  1212. {
  1213. struct cfq_queue *cfqq = RQ_CFQQ(rq);
  1214. /*
  1215. * reposition in fifo if next is older than rq
  1216. */
  1217. if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
  1218. time_before(rq_fifo_time(next), rq_fifo_time(rq))) {
  1219. list_move(&rq->queuelist, &next->queuelist);
  1220. rq_set_fifo_time(rq, rq_fifo_time(next));
  1221. }
  1222. if (cfqq->next_rq == next)
  1223. cfqq->next_rq = rq;
  1224. cfq_remove_request(next);
  1225. }
  1226. static int cfq_allow_merge(struct request_queue *q, struct request *rq,
  1227. struct bio *bio)
  1228. {
  1229. struct cfq_data *cfqd = q->elevator->elevator_data;
  1230. struct cfq_io_context *cic;
  1231. struct cfq_queue *cfqq;
  1232. /*
  1233. * Disallow merge of a sync bio into an async request.
  1234. */
  1235. if (cfq_bio_sync(bio) && !rq_is_sync(rq))
  1236. return false;
  1237. /*
  1238. * Lookup the cfqq that this bio will be queued with. Allow
  1239. * merge only if rq is queued there.
  1240. */
  1241. cic = cfq_cic_lookup(cfqd, current->io_context);
  1242. if (!cic)
  1243. return false;
  1244. cfqq = cic_to_cfqq(cic, cfq_bio_sync(bio));
  1245. return cfqq == RQ_CFQQ(rq);
  1246. }
  1247. static void __cfq_set_active_queue(struct cfq_data *cfqd,
  1248. struct cfq_queue *cfqq)
  1249. {
  1250. if (cfqq) {
  1251. cfq_log_cfqq(cfqd, cfqq, "set_active");
  1252. cfqq->slice_start = 0;
  1253. cfqq->dispatch_start = jiffies;
  1254. cfqq->slice_end = 0;
  1255. cfqq->slice_dispatch = 0;
  1256. cfq_clear_cfqq_wait_request(cfqq);
  1257. cfq_clear_cfqq_must_dispatch(cfqq);
  1258. cfq_clear_cfqq_must_alloc_slice(cfqq);
  1259. cfq_clear_cfqq_fifo_expire(cfqq);
  1260. cfq_mark_cfqq_slice_new(cfqq);
  1261. del_timer(&cfqd->idle_slice_timer);
  1262. }
  1263. cfqd->active_queue = cfqq;
  1264. }
  1265. /*
  1266. * current cfqq expired its slice (or was too idle), select new one
  1267. */
  1268. static void
  1269. __cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq,
  1270. bool timed_out)
  1271. {
  1272. cfq_log_cfqq(cfqd, cfqq, "slice expired t=%d", timed_out);
  1273. if (cfq_cfqq_wait_request(cfqq))
  1274. del_timer(&cfqd->idle_slice_timer);
  1275. cfq_clear_cfqq_wait_request(cfqq);
  1276. /*
  1277. * store what was left of this slice, if the queue idled/timed out
  1278. */
  1279. if (timed_out && !cfq_cfqq_slice_new(cfqq)) {
  1280. cfqq->slice_resid = cfqq->slice_end - jiffies;
  1281. cfq_log_cfqq(cfqd, cfqq, "resid=%ld", cfqq->slice_resid);
  1282. }
  1283. cfq_group_served(cfqd, cfqq->cfqg, cfqq);
  1284. if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list))
  1285. cfq_del_cfqq_rr(cfqd, cfqq);
  1286. cfq_resort_rr_list(cfqd, cfqq);
  1287. if (cfqq == cfqd->active_queue)
  1288. cfqd->active_queue = NULL;
  1289. if (&cfqq->cfqg->rb_node == cfqd->grp_service_tree.active)
  1290. cfqd->grp_service_tree.active = NULL;
  1291. if (cfqd->active_cic) {
  1292. put_io_context(cfqd->active_cic->ioc);
  1293. cfqd->active_cic = NULL;
  1294. }
  1295. }
  1296. static inline void cfq_slice_expired(struct cfq_data *cfqd, bool timed_out)
  1297. {
  1298. struct cfq_queue *cfqq = cfqd->active_queue;
  1299. if (cfqq)
  1300. __cfq_slice_expired(cfqd, cfqq, timed_out);
  1301. }
  1302. /*
  1303. * Get next queue for service. Unless we have a queue preemption,
  1304. * we'll simply select the first cfqq in the service tree.
  1305. */
  1306. static struct cfq_queue *cfq_get_next_queue(struct cfq_data *cfqd)
  1307. {
  1308. struct cfq_rb_root *service_tree =
  1309. service_tree_for(cfqd->serving_group, cfqd->serving_prio,
  1310. cfqd->serving_type, cfqd);
  1311. if (!cfqd->rq_queued)
  1312. return NULL;
  1313. /* There is nothing to dispatch */
  1314. if (!service_tree)
  1315. return NULL;
  1316. if (RB_EMPTY_ROOT(&service_tree->rb))
  1317. return NULL;
  1318. return cfq_rb_first(service_tree);
  1319. }
  1320. static struct cfq_queue *cfq_get_next_queue_forced(struct cfq_data *cfqd)
  1321. {
  1322. struct cfq_group *cfqg;
  1323. struct cfq_queue *cfqq;
  1324. int i, j;
  1325. struct cfq_rb_root *st;
  1326. if (!cfqd->rq_queued)
  1327. return NULL;
  1328. cfqg = cfq_get_next_cfqg(cfqd);
  1329. if (!cfqg)
  1330. return NULL;
  1331. for_each_cfqg_st(cfqg, i, j, st)
  1332. if ((cfqq = cfq_rb_first(st)) != NULL)
  1333. return cfqq;
  1334. return NULL;
  1335. }
  1336. /*
  1337. * Get and set a new active queue for service.
  1338. */
  1339. static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd,
  1340. struct cfq_queue *cfqq)
  1341. {
  1342. if (!cfqq)
  1343. cfqq = cfq_get_next_queue(cfqd);
  1344. __cfq_set_active_queue(cfqd, cfqq);
  1345. return cfqq;
  1346. }
  1347. static inline sector_t cfq_dist_from_last(struct cfq_data *cfqd,
  1348. struct request *rq)
  1349. {
  1350. if (blk_rq_pos(rq) >= cfqd->last_position)
  1351. return blk_rq_pos(rq) - cfqd->last_position;
  1352. else
  1353. return cfqd->last_position - blk_rq_pos(rq);
  1354. }
  1355. #define CFQQ_SEEK_THR 8 * 1024
  1356. #define CFQQ_SEEKY(cfqq) ((cfqq)->seek_mean > CFQQ_SEEK_THR)
  1357. static inline int cfq_rq_close(struct cfq_data *cfqd, struct cfq_queue *cfqq,
  1358. struct request *rq)
  1359. {
  1360. sector_t sdist = cfqq->seek_mean;
  1361. if (!sample_valid(cfqq->seek_samples))
  1362. sdist = CFQQ_SEEK_THR;
  1363. return cfq_dist_from_last(cfqd, rq) <= sdist;
  1364. }
  1365. static struct cfq_queue *cfqq_close(struct cfq_data *cfqd,
  1366. struct cfq_queue *cur_cfqq)
  1367. {
  1368. struct rb_root *root = &cfqd->prio_trees[cur_cfqq->org_ioprio];
  1369. struct rb_node *parent, *node;
  1370. struct cfq_queue *__cfqq;
  1371. sector_t sector = cfqd->last_position;
  1372. if (RB_EMPTY_ROOT(root))
  1373. return NULL;
  1374. /*
  1375. * First, if we find a request starting at the end of the last
  1376. * request, choose it.
  1377. */
  1378. __cfqq = cfq_prio_tree_lookup(cfqd, root, sector, &parent, NULL);
  1379. if (__cfqq)
  1380. return __cfqq;
  1381. /*
  1382. * If the exact sector wasn't found, the parent of the NULL leaf
  1383. * will contain the closest sector.
  1384. */
  1385. __cfqq = rb_entry(parent, struct cfq_queue, p_node);
  1386. if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
  1387. return __cfqq;
  1388. if (blk_rq_pos(__cfqq->next_rq) < sector)
  1389. node = rb_next(&__cfqq->p_node);
  1390. else
  1391. node = rb_prev(&__cfqq->p_node);
  1392. if (!node)
  1393. return NULL;
  1394. __cfqq = rb_entry(node, struct cfq_queue, p_node);
  1395. if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
  1396. return __cfqq;
  1397. return NULL;
  1398. }
  1399. /*
  1400. * cfqd - obvious
  1401. * cur_cfqq - passed in so that we don't decide that the current queue is
  1402. * closely cooperating with itself.
  1403. *
  1404. * So, basically we're assuming that that cur_cfqq has dispatched at least
  1405. * one request, and that cfqd->last_position reflects a position on the disk
  1406. * associated with the I/O issued by cur_cfqq. I'm not sure this is a valid
  1407. * assumption.
  1408. */
  1409. static struct cfq_queue *cfq_close_cooperator(struct cfq_data *cfqd,
  1410. struct cfq_queue *cur_cfqq)
  1411. {
  1412. struct cfq_queue *cfqq;
  1413. if (!cfq_cfqq_sync(cur_cfqq))
  1414. return NULL;
  1415. if (CFQQ_SEEKY(cur_cfqq))
  1416. return NULL;
  1417. /*
  1418. * We should notice if some of the queues are cooperating, eg
  1419. * working closely on the same area of the disk. In that case,
  1420. * we can group them together and don't waste time idling.
  1421. */
  1422. cfqq = cfqq_close(cfqd, cur_cfqq);
  1423. if (!cfqq)
  1424. return NULL;
  1425. /*
  1426. * It only makes sense to merge sync queues.
  1427. */
  1428. if (!cfq_cfqq_sync(cfqq))
  1429. return NULL;
  1430. if (CFQQ_SEEKY(cfqq))
  1431. return NULL;
  1432. /*
  1433. * Do not merge queues of different priority classes
  1434. */
  1435. if (cfq_class_rt(cfqq) != cfq_class_rt(cur_cfqq))
  1436. return NULL;
  1437. return cfqq;
  1438. }
  1439. /*
  1440. * Determine whether we should enforce idle window for this queue.
  1441. */
  1442. static bool cfq_should_idle(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  1443. {
  1444. enum wl_prio_t prio = cfqq_prio(cfqq);
  1445. struct cfq_rb_root *service_tree = cfqq->service_tree;
  1446. BUG_ON(!service_tree);
  1447. BUG_ON(!service_tree->count);
  1448. /* We never do for idle class queues. */
  1449. if (prio == IDLE_WORKLOAD)
  1450. return false;
  1451. /* We do for queues that were marked with idle window flag. */
  1452. if (cfq_cfqq_idle_window(cfqq))
  1453. return true;
  1454. /*
  1455. * Otherwise, we do only if they are the last ones
  1456. * in their service tree.
  1457. */
  1458. return service_tree->count == 1;
  1459. }
  1460. static void cfq_arm_slice_timer(struct cfq_data *cfqd)
  1461. {
  1462. struct cfq_queue *cfqq = cfqd->active_queue;
  1463. struct cfq_io_context *cic;
  1464. unsigned long sl;
  1465. /*
  1466. * SSD device without seek penalty, disable idling. But only do so
  1467. * for devices that support queuing, otherwise we still have a problem
  1468. * with sync vs async workloads.
  1469. */
  1470. if (blk_queue_nonrot(cfqd->queue) && cfqd->hw_tag)
  1471. return;
  1472. WARN_ON(!RB_EMPTY_ROOT(&cfqq->sort_list));
  1473. WARN_ON(cfq_cfqq_slice_new(cfqq));
  1474. /*
  1475. * idle is disabled, either manually or by past process history
  1476. */
  1477. if (!cfqd->cfq_slice_idle || !cfq_should_idle(cfqd, cfqq))
  1478. return;
  1479. /*
  1480. * still active requests from this queue, don't idle
  1481. */
  1482. if (cfqq->dispatched)
  1483. return;
  1484. /*
  1485. * task has exited, don't wait
  1486. */
  1487. cic = cfqd->active_cic;
  1488. if (!cic || !atomic_read(&cic->ioc->nr_tasks))
  1489. return;
  1490. /*
  1491. * If our average think time is larger than the remaining time
  1492. * slice, then don't idle. This avoids overrunning the allotted
  1493. * time slice.
  1494. */
  1495. if (sample_valid(cic->ttime_samples) &&
  1496. (cfqq->slice_end - jiffies < cic->ttime_mean))
  1497. return;
  1498. cfq_mark_cfqq_wait_request(cfqq);
  1499. sl = cfqd->cfq_slice_idle;
  1500. mod_timer(&cfqd->idle_slice_timer, jiffies + sl);
  1501. cfq_log_cfqq(cfqd, cfqq, "arm_idle: %lu", sl);
  1502. }
  1503. /*
  1504. * Move request from internal lists to the request queue dispatch list.
  1505. */
  1506. static void cfq_dispatch_insert(struct request_queue *q, struct request *rq)
  1507. {
  1508. struct cfq_data *cfqd = q->elevator->elevator_data;
  1509. struct cfq_queue *cfqq = RQ_CFQQ(rq);
  1510. cfq_log_cfqq(cfqd, cfqq, "dispatch_insert");
  1511. cfqq->next_rq = cfq_find_next_rq(cfqd, cfqq, rq);
  1512. cfq_remove_request(rq);
  1513. cfqq->dispatched++;
  1514. elv_dispatch_sort(q, rq);
  1515. if (cfq_cfqq_sync(cfqq))
  1516. cfqd->sync_flight++;
  1517. }
  1518. /*
  1519. * return expired entry, or NULL to just start from scratch in rbtree
  1520. */
  1521. static struct request *cfq_check_fifo(struct cfq_queue *cfqq)
  1522. {
  1523. struct request *rq = NULL;
  1524. if (cfq_cfqq_fifo_expire(cfqq))
  1525. return NULL;
  1526. cfq_mark_cfqq_fifo_expire(cfqq);
  1527. if (list_empty(&cfqq->fifo))
  1528. return NULL;
  1529. rq = rq_entry_fifo(cfqq->fifo.next);
  1530. if (time_before(jiffies, rq_fifo_time(rq)))
  1531. rq = NULL;
  1532. cfq_log_cfqq(cfqq->cfqd, cfqq, "fifo=%p", rq);
  1533. return rq;
  1534. }
  1535. static inline int
  1536. cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  1537. {
  1538. const int base_rq = cfqd->cfq_slice_async_rq;
  1539. WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
  1540. return 2 * (base_rq + base_rq * (CFQ_PRIO_LISTS - 1 - cfqq->ioprio));
  1541. }
  1542. /*
  1543. * Must be called with the queue_lock held.
  1544. */
  1545. static int cfqq_process_refs(struct cfq_queue *cfqq)
  1546. {
  1547. int process_refs, io_refs;
  1548. io_refs = cfqq->allocated[READ] + cfqq->allocated[WRITE];
  1549. process_refs = atomic_read(&cfqq->ref) - io_refs;
  1550. BUG_ON(process_refs < 0);
  1551. return process_refs;
  1552. }
  1553. static void cfq_setup_merge(struct cfq_queue *cfqq, struct cfq_queue *new_cfqq)
  1554. {
  1555. int process_refs, new_process_refs;
  1556. struct cfq_queue *__cfqq;
  1557. /* Avoid a circular list and skip interim queue merges */
  1558. while ((__cfqq = new_cfqq->new_cfqq)) {
  1559. if (__cfqq == cfqq)
  1560. return;
  1561. new_cfqq = __cfqq;
  1562. }
  1563. process_refs = cfqq_process_refs(cfqq);
  1564. /*
  1565. * If the process for the cfqq has gone away, there is no
  1566. * sense in merging the queues.
  1567. */
  1568. if (process_refs == 0)
  1569. return;
  1570. /*
  1571. * Merge in the direction of the lesser amount of work.
  1572. */
  1573. new_process_refs = cfqq_process_refs(new_cfqq);
  1574. if (new_process_refs >= process_refs) {
  1575. cfqq->new_cfqq = new_cfqq;
  1576. atomic_add(process_refs, &new_cfqq->ref);
  1577. } else {
  1578. new_cfqq->new_cfqq = cfqq;
  1579. atomic_add(new_process_refs, &cfqq->ref);
  1580. }
  1581. }
  1582. static enum wl_type_t cfq_choose_wl(struct cfq_data *cfqd,
  1583. struct cfq_group *cfqg, enum wl_prio_t prio,
  1584. bool prio_changed)
  1585. {
  1586. struct cfq_queue *queue;
  1587. int i;
  1588. bool key_valid = false;
  1589. unsigned long lowest_key = 0;
  1590. enum wl_type_t cur_best = SYNC_NOIDLE_WORKLOAD;
  1591. if (prio_changed) {
  1592. /*
  1593. * When priorities switched, we prefer starting
  1594. * from SYNC_NOIDLE (first choice), or just SYNC
  1595. * over ASYNC
  1596. */
  1597. if (service_tree_for(cfqg, prio, cur_best, cfqd)->count)
  1598. return cur_best;
  1599. cur_best = SYNC_WORKLOAD;
  1600. if (service_tree_for(cfqg, prio, cur_best, cfqd)->count)
  1601. return cur_best;
  1602. return ASYNC_WORKLOAD;
  1603. }
  1604. for (i = 0; i < 3; ++i) {
  1605. /* otherwise, select the one with lowest rb_key */
  1606. queue = cfq_rb_first(service_tree_for(cfqg, prio, i, cfqd));
  1607. if (queue &&
  1608. (!key_valid || time_before(queue->rb_key, lowest_key))) {
  1609. lowest_key = queue->rb_key;
  1610. cur_best = i;
  1611. key_valid = true;
  1612. }
  1613. }
  1614. return cur_best;
  1615. }
  1616. static void choose_service_tree(struct cfq_data *cfqd, struct cfq_group *cfqg)
  1617. {
  1618. enum wl_prio_t previous_prio = cfqd->serving_prio;
  1619. bool prio_changed;
  1620. unsigned slice;
  1621. unsigned count;
  1622. struct cfq_rb_root *st;
  1623. unsigned group_slice;
  1624. if (!cfqg) {
  1625. cfqd->serving_prio = IDLE_WORKLOAD;
  1626. cfqd->workload_expires = jiffies + 1;
  1627. return;
  1628. }
  1629. /* Choose next priority. RT > BE > IDLE */
  1630. if (cfq_group_busy_queues_wl(RT_WORKLOAD, cfqd, cfqg))
  1631. cfqd->serving_prio = RT_WORKLOAD;
  1632. else if (cfq_group_busy_queues_wl(BE_WORKLOAD, cfqd, cfqg))
  1633. cfqd->serving_prio = BE_WORKLOAD;
  1634. else {
  1635. cfqd->serving_prio = IDLE_WORKLOAD;
  1636. cfqd->workload_expires = jiffies + 1;
  1637. return;
  1638. }
  1639. /*
  1640. * For RT and BE, we have to choose also the type
  1641. * (SYNC, SYNC_NOIDLE, ASYNC), and to compute a workload
  1642. * expiration time
  1643. */
  1644. prio_changed = (cfqd->serving_prio != previous_prio);
  1645. st = service_tree_for(cfqg, cfqd->serving_prio, cfqd->serving_type,
  1646. cfqd);
  1647. count = st->count;
  1648. /*
  1649. * If priority didn't change, check workload expiration,
  1650. * and that we still have other queues ready
  1651. */
  1652. if (!prio_changed && count &&
  1653. !time_after(jiffies, cfqd->workload_expires))
  1654. return;
  1655. /* otherwise select new workload type */
  1656. cfqd->serving_type =
  1657. cfq_choose_wl(cfqd, cfqg, cfqd->serving_prio, prio_changed);
  1658. st = service_tree_for(cfqg, cfqd->serving_prio, cfqd->serving_type,
  1659. cfqd);
  1660. count = st->count;
  1661. /*
  1662. * the workload slice is computed as a fraction of target latency
  1663. * proportional to the number of queues in that workload, over
  1664. * all the queues in the same priority class
  1665. */
  1666. group_slice = cfq_group_slice(cfqd, cfqg);
  1667. slice = group_slice * count /
  1668. max_t(unsigned, cfqg->busy_queues_avg[cfqd->serving_prio],
  1669. cfq_group_busy_queues_wl(cfqd->serving_prio, cfqd, cfqg));
  1670. if (cfqd->serving_type == ASYNC_WORKLOAD)
  1671. /* async workload slice is scaled down according to
  1672. * the sync/async slice ratio. */
  1673. slice = slice * cfqd->cfq_slice[0] / cfqd->cfq_slice[1];
  1674. else
  1675. /* sync workload slice is at least 2 * cfq_slice_idle */
  1676. slice = max(slice, 2 * cfqd->cfq_slice_idle);
  1677. slice = max_t(unsigned, slice, CFQ_MIN_TT);
  1678. cfqd->workload_expires = jiffies + slice;
  1679. cfqd->noidle_tree_requires_idle = false;
  1680. }
  1681. static struct cfq_group *cfq_get_next_cfqg(struct cfq_data *cfqd)
  1682. {
  1683. struct cfq_rb_root *st = &cfqd->grp_service_tree;
  1684. struct cfq_group *cfqg;
  1685. if (RB_EMPTY_ROOT(&st->rb))
  1686. return NULL;
  1687. cfqg = cfq_rb_first_group(st);
  1688. st->active = &cfqg->rb_node;
  1689. update_min_vdisktime(st);
  1690. return cfqg;
  1691. }
  1692. static void cfq_choose_cfqg(struct cfq_data *cfqd)
  1693. {
  1694. struct cfq_group *cfqg = cfq_get_next_cfqg(cfqd);
  1695. cfqd->serving_group = cfqg;
  1696. /* Restore the workload type data */
  1697. if (cfqg->saved_workload_slice) {
  1698. cfqd->workload_expires = jiffies + cfqg->saved_workload_slice;
  1699. cfqd->serving_type = cfqg->saved_workload;
  1700. cfqd->serving_prio = cfqg->saved_serving_prio;
  1701. }
  1702. choose_service_tree(cfqd, cfqg);
  1703. }
  1704. /*
  1705. * Select a queue for service. If we have a current active queue,
  1706. * check whether to continue servicing it, or retrieve and set a new one.
  1707. */
  1708. static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd)
  1709. {
  1710. struct cfq_queue *cfqq, *new_cfqq = NULL;
  1711. cfqq = cfqd->active_queue;
  1712. if (!cfqq)
  1713. goto new_queue;
  1714. if (!cfqd->rq_queued)
  1715. return NULL;
  1716. /*
  1717. * The active queue has run out of time, expire it and select new.
  1718. */
  1719. if (cfq_slice_used(cfqq) && !cfq_cfqq_must_dispatch(cfqq))
  1720. goto expire;
  1721. /*
  1722. * The active queue has requests and isn't expired, allow it to
  1723. * dispatch.
  1724. */
  1725. if (!RB_EMPTY_ROOT(&cfqq->sort_list))
  1726. goto keep_queue;
  1727. /*
  1728. * If another queue has a request waiting within our mean seek
  1729. * distance, let it run. The expire code will check for close
  1730. * cooperators and put the close queue at the front of the service
  1731. * tree. If possible, merge the expiring queue with the new cfqq.
  1732. */
  1733. new_cfqq = cfq_close_cooperator(cfqd, cfqq);
  1734. if (new_cfqq) {
  1735. if (!cfqq->new_cfqq)
  1736. cfq_setup_merge(cfqq, new_cfqq);
  1737. goto expire;
  1738. }
  1739. /*
  1740. * No requests pending. If the active queue still has requests in
  1741. * flight or is idling for a new request, allow either of these
  1742. * conditions to happen (or time out) before selecting a new queue.
  1743. */
  1744. if (timer_pending(&cfqd->idle_slice_timer) ||
  1745. (cfqq->dispatched && cfq_should_idle(cfqd, cfqq))) {
  1746. cfqq = NULL;
  1747. goto keep_queue;
  1748. }
  1749. expire:
  1750. cfq_slice_expired(cfqd, 0);
  1751. new_queue:
  1752. /*
  1753. * Current queue expired. Check if we have to switch to a new
  1754. * service tree
  1755. */
  1756. if (!new_cfqq)
  1757. cfq_choose_cfqg(cfqd);
  1758. cfqq = cfq_set_active_queue(cfqd, new_cfqq);
  1759. keep_queue:
  1760. return cfqq;
  1761. }
  1762. static int __cfq_forced_dispatch_cfqq(struct cfq_queue *cfqq)
  1763. {
  1764. int dispatched = 0;
  1765. while (cfqq->next_rq) {
  1766. cfq_dispatch_insert(cfqq->cfqd->queue, cfqq->next_rq);
  1767. dispatched++;
  1768. }
  1769. BUG_ON(!list_empty(&cfqq->fifo));
  1770. /* By default cfqq is not expired if it is empty. Do it explicitly */
  1771. __cfq_slice_expired(cfqq->cfqd, cfqq, 0);
  1772. return dispatched;
  1773. }
  1774. /*
  1775. * Drain our current requests. Used for barriers and when switching
  1776. * io schedulers on-the-fly.
  1777. */
  1778. static int cfq_forced_dispatch(struct cfq_data *cfqd)
  1779. {
  1780. struct cfq_queue *cfqq;
  1781. int dispatched = 0;
  1782. while ((cfqq = cfq_get_next_queue_forced(cfqd)) != NULL)
  1783. dispatched += __cfq_forced_dispatch_cfqq(cfqq);
  1784. cfq_slice_expired(cfqd, 0);
  1785. BUG_ON(cfqd->busy_queues);
  1786. cfq_log(cfqd, "forced_dispatch=%d", dispatched);
  1787. return dispatched;
  1788. }
  1789. static bool cfq_may_dispatch(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  1790. {
  1791. unsigned int max_dispatch;
  1792. /*
  1793. * Drain async requests before we start sync IO
  1794. */
  1795. if (cfq_should_idle(cfqd, cfqq) && cfqd->rq_in_driver[BLK_RW_ASYNC])
  1796. return false;
  1797. /*
  1798. * If this is an async queue and we have sync IO in flight, let it wait
  1799. */
  1800. if (cfqd->sync_flight && !cfq_cfqq_sync(cfqq))
  1801. return false;
  1802. max_dispatch = cfqd->cfq_quantum;
  1803. if (cfq_class_idle(cfqq))
  1804. max_dispatch = 1;
  1805. /*
  1806. * Does this cfqq already have too much IO in flight?
  1807. */
  1808. if (cfqq->dispatched >= max_dispatch) {
  1809. /*
  1810. * idle queue must always only have a single IO in flight
  1811. */
  1812. if (cfq_class_idle(cfqq))
  1813. return false;
  1814. /*
  1815. * We have other queues, don't allow more IO from this one
  1816. */
  1817. if (cfqd->busy_queues > 1)
  1818. return false;
  1819. /*
  1820. * Sole queue user, no limit
  1821. */
  1822. max_dispatch = -1;
  1823. }
  1824. /*
  1825. * Async queues must wait a bit before being allowed dispatch.
  1826. * We also ramp up the dispatch depth gradually for async IO,
  1827. * based on the last sync IO we serviced
  1828. */
  1829. if (!cfq_cfqq_sync(cfqq) && cfqd->cfq_latency) {
  1830. unsigned long last_sync = jiffies - cfqd->last_end_sync_rq;
  1831. unsigned int depth;
  1832. depth = last_sync / cfqd->cfq_slice[1];
  1833. if (!depth && !cfqq->dispatched)
  1834. depth = 1;
  1835. if (depth < max_dispatch)
  1836. max_dispatch = depth;
  1837. }
  1838. /*
  1839. * If we're below the current max, allow a dispatch
  1840. */
  1841. return cfqq->dispatched < max_dispatch;
  1842. }
  1843. /*
  1844. * Dispatch a request from cfqq, moving them to the request queue
  1845. * dispatch list.
  1846. */
  1847. static bool cfq_dispatch_request(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  1848. {
  1849. struct request *rq;
  1850. BUG_ON(RB_EMPTY_ROOT(&cfqq->sort_list));
  1851. if (!cfq_may_dispatch(cfqd, cfqq))
  1852. return false;
  1853. /*
  1854. * follow expired path, else get first next available
  1855. */
  1856. rq = cfq_check_fifo(cfqq);
  1857. if (!rq)
  1858. rq = cfqq->next_rq;
  1859. /*
  1860. * insert request into driver dispatch list
  1861. */
  1862. cfq_dispatch_insert(cfqd->queue, rq);
  1863. if (!cfqd->active_cic) {
  1864. struct cfq_io_context *cic = RQ_CIC(rq);
  1865. atomic_long_inc(&cic->ioc->refcount);
  1866. cfqd->active_cic = cic;
  1867. }
  1868. return true;
  1869. }
  1870. /*
  1871. * Find the cfqq that we need to service and move a request from that to the
  1872. * dispatch list
  1873. */
  1874. static int cfq_dispatch_requests(struct request_queue *q, int force)
  1875. {
  1876. struct cfq_data *cfqd = q->elevator->elevator_data;
  1877. struct cfq_queue *cfqq;
  1878. if (!cfqd->busy_queues)
  1879. return 0;
  1880. if (unlikely(force))
  1881. return cfq_forced_dispatch(cfqd);
  1882. cfqq = cfq_select_queue(cfqd);
  1883. if (!cfqq)
  1884. return 0;
  1885. /*
  1886. * Dispatch a request from this cfqq, if it is allowed
  1887. */
  1888. if (!cfq_dispatch_request(cfqd, cfqq))
  1889. return 0;
  1890. cfqq->slice_dispatch++;
  1891. cfq_clear_cfqq_must_dispatch(cfqq);
  1892. /*
  1893. * expire an async queue immediately if it has used up its slice. idle
  1894. * queue always expire after 1 dispatch round.
  1895. */
  1896. if (cfqd->busy_queues > 1 && ((!cfq_cfqq_sync(cfqq) &&
  1897. cfqq->slice_dispatch >= cfq_prio_to_maxrq(cfqd, cfqq)) ||
  1898. cfq_class_idle(cfqq))) {
  1899. cfqq->slice_end = jiffies + 1;
  1900. cfq_slice_expired(cfqd, 0);
  1901. }
  1902. cfq_log_cfqq(cfqd, cfqq, "dispatched a request");
  1903. return 1;
  1904. }
  1905. /*
  1906. * task holds one reference to the queue, dropped when task exits. each rq
  1907. * in-flight on this queue also holds a reference, dropped when rq is freed.
  1908. *
  1909. * Each cfq queue took a reference on the parent group. Drop it now.
  1910. * queue lock must be held here.
  1911. */
  1912. static void cfq_put_queue(struct cfq_queue *cfqq)
  1913. {
  1914. struct cfq_data *cfqd = cfqq->cfqd;
  1915. struct cfq_group *cfqg;
  1916. BUG_ON(atomic_read(&cfqq->ref) <= 0);
  1917. if (!atomic_dec_and_test(&cfqq->ref))
  1918. return;
  1919. cfq_log_cfqq(cfqd, cfqq, "put_queue");
  1920. BUG_ON(rb_first(&cfqq->sort_list));
  1921. BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
  1922. cfqg = cfqq->cfqg;
  1923. if (unlikely(cfqd->active_queue == cfqq)) {
  1924. __cfq_slice_expired(cfqd, cfqq, 0);
  1925. cfq_schedule_dispatch(cfqd);
  1926. }
  1927. BUG_ON(cfq_cfqq_on_rr(cfqq));
  1928. kmem_cache_free(cfq_pool, cfqq);
  1929. cfq_put_cfqg(cfqg);
  1930. }
  1931. /*
  1932. * Must always be called with the rcu_read_lock() held
  1933. */
  1934. static void
  1935. __call_for_each_cic(struct io_context *ioc,
  1936. void (*func)(struct io_context *, struct cfq_io_context *))
  1937. {
  1938. struct cfq_io_context *cic;
  1939. struct hlist_node *n;
  1940. hlist_for_each_entry_rcu(cic, n, &ioc->cic_list, cic_list)
  1941. func(ioc, cic);
  1942. }
  1943. /*
  1944. * Call func for each cic attached to this ioc.
  1945. */
  1946. static void
  1947. call_for_each_cic(struct io_context *ioc,
  1948. void (*func)(struct io_context *, struct cfq_io_context *))
  1949. {
  1950. rcu_read_lock();
  1951. __call_for_each_cic(ioc, func);
  1952. rcu_read_unlock();
  1953. }
  1954. static void cfq_cic_free_rcu(struct rcu_head *head)
  1955. {
  1956. struct cfq_io_context *cic;
  1957. cic = container_of(head, struct cfq_io_context, rcu_head);
  1958. kmem_cache_free(cfq_ioc_pool, cic);
  1959. elv_ioc_count_dec(cfq_ioc_count);
  1960. if (ioc_gone) {
  1961. /*
  1962. * CFQ scheduler is exiting, grab exit lock and check
  1963. * the pending io context count. If it hits zero,
  1964. * complete ioc_gone and set it back to NULL
  1965. */
  1966. spin_lock(&ioc_gone_lock);
  1967. if (ioc_gone && !elv_ioc_count_read(cfq_ioc_count)) {
  1968. complete(ioc_gone);
  1969. ioc_gone = NULL;
  1970. }
  1971. spin_unlock(&ioc_gone_lock);
  1972. }
  1973. }
  1974. static void cfq_cic_free(struct cfq_io_context *cic)
  1975. {
  1976. call_rcu(&cic->rcu_head, cfq_cic_free_rcu);
  1977. }
  1978. static void cic_free_func(struct io_context *ioc, struct cfq_io_context *cic)
  1979. {
  1980. unsigned long flags;
  1981. BUG_ON(!cic->dead_key);
  1982. spin_lock_irqsave(&ioc->lock, flags);
  1983. radix_tree_delete(&ioc->radix_root, cic->dead_key);
  1984. hlist_del_rcu(&cic->cic_list);
  1985. spin_unlock_irqrestore(&ioc->lock, flags);
  1986. cfq_cic_free(cic);
  1987. }
  1988. /*
  1989. * Must be called with rcu_read_lock() held or preemption otherwise disabled.
  1990. * Only two callers of this - ->dtor() which is called with the rcu_read_lock(),
  1991. * and ->trim() which is called with the task lock held
  1992. */
  1993. static void cfq_free_io_context(struct io_context *ioc)
  1994. {
  1995. /*
  1996. * ioc->refcount is zero here, or we are called from elv_unregister(),
  1997. * so no more cic's are allowed to be linked into this ioc. So it
  1998. * should be ok to iterate over the known list, we will see all cic's
  1999. * since no new ones are added.
  2000. */
  2001. __call_for_each_cic(ioc, cic_free_func);
  2002. }
  2003. static void cfq_exit_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  2004. {
  2005. struct cfq_queue *__cfqq, *next;
  2006. if (unlikely(cfqq == cfqd->active_queue)) {
  2007. __cfq_slice_expired(cfqd, cfqq, 0);
  2008. cfq_schedule_dispatch(cfqd);
  2009. }
  2010. /*
  2011. * If this queue was scheduled to merge with another queue, be
  2012. * sure to drop the reference taken on that queue (and others in
  2013. * the merge chain). See cfq_setup_merge and cfq_merge_cfqqs.
  2014. */
  2015. __cfqq = cfqq->new_cfqq;
  2016. while (__cfqq) {
  2017. if (__cfqq == cfqq) {
  2018. WARN(1, "cfqq->new_cfqq loop detected\n");
  2019. break;
  2020. }
  2021. next = __cfqq->new_cfqq;
  2022. cfq_put_queue(__cfqq);
  2023. __cfqq = next;
  2024. }
  2025. cfq_put_queue(cfqq);
  2026. }
  2027. static void __cfq_exit_single_io_context(struct cfq_data *cfqd,
  2028. struct cfq_io_context *cic)
  2029. {
  2030. struct io_context *ioc = cic->ioc;
  2031. list_del_init(&cic->queue_list);
  2032. /*
  2033. * Make sure key == NULL is seen for dead queues
  2034. */
  2035. smp_wmb();
  2036. cic->dead_key = (unsigned long) cic->key;
  2037. cic->key = NULL;
  2038. if (ioc->ioc_data == cic)
  2039. rcu_assign_pointer(ioc->ioc_data, NULL);
  2040. if (cic->cfqq[BLK_RW_ASYNC]) {
  2041. cfq_exit_cfqq(cfqd, cic->cfqq[BLK_RW_ASYNC]);
  2042. cic->cfqq[BLK_RW_ASYNC] = NULL;
  2043. }
  2044. if (cic->cfqq[BLK_RW_SYNC]) {
  2045. cfq_exit_cfqq(cfqd, cic->cfqq[BLK_RW_SYNC]);
  2046. cic->cfqq[BLK_RW_SYNC] = NULL;
  2047. }
  2048. }
  2049. static void cfq_exit_single_io_context(struct io_context *ioc,
  2050. struct cfq_io_context *cic)
  2051. {
  2052. struct cfq_data *cfqd = cic->key;
  2053. if (cfqd) {
  2054. struct request_queue *q = cfqd->queue;
  2055. unsigned long flags;
  2056. spin_lock_irqsave(q->queue_lock, flags);
  2057. /*
  2058. * Ensure we get a fresh copy of the ->key to prevent
  2059. * race between exiting task and queue
  2060. */
  2061. smp_read_barrier_depends();
  2062. if (cic->key)
  2063. __cfq_exit_single_io_context(cfqd, cic);
  2064. spin_unlock_irqrestore(q->queue_lock, flags);
  2065. }
  2066. }
  2067. /*
  2068. * The process that ioc belongs to has exited, we need to clean up
  2069. * and put the internal structures we have that belongs to that process.
  2070. */
  2071. static void cfq_exit_io_context(struct io_context *ioc)
  2072. {
  2073. call_for_each_cic(ioc, cfq_exit_single_io_context);
  2074. }
  2075. static struct cfq_io_context *
  2076. cfq_alloc_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
  2077. {
  2078. struct cfq_io_context *cic;
  2079. cic = kmem_cache_alloc_node(cfq_ioc_pool, gfp_mask | __GFP_ZERO,
  2080. cfqd->queue->node);
  2081. if (cic) {
  2082. cic->last_end_request = jiffies;
  2083. INIT_LIST_HEAD(&cic->queue_list);
  2084. INIT_HLIST_NODE(&cic->cic_list);
  2085. cic->dtor = cfq_free_io_context;
  2086. cic->exit = cfq_exit_io_context;
  2087. elv_ioc_count_inc(cfq_ioc_count);
  2088. }
  2089. return cic;
  2090. }
  2091. static void cfq_init_prio_data(struct cfq_queue *cfqq, struct io_context *ioc)
  2092. {
  2093. struct task_struct *tsk = current;
  2094. int ioprio_class;
  2095. if (!cfq_cfqq_prio_changed(cfqq))
  2096. return;
  2097. ioprio_class = IOPRIO_PRIO_CLASS(ioc->ioprio);
  2098. switch (ioprio_class) {
  2099. default:
  2100. printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class);
  2101. case IOPRIO_CLASS_NONE:
  2102. /*
  2103. * no prio set, inherit CPU scheduling settings
  2104. */
  2105. cfqq->ioprio = task_nice_ioprio(tsk);
  2106. cfqq->ioprio_class = task_nice_ioclass(tsk);
  2107. break;
  2108. case IOPRIO_CLASS_RT:
  2109. cfqq->ioprio = task_ioprio(ioc);
  2110. cfqq->ioprio_class = IOPRIO_CLASS_RT;
  2111. break;
  2112. case IOPRIO_CLASS_BE:
  2113. cfqq->ioprio = task_ioprio(ioc);
  2114. cfqq->ioprio_class = IOPRIO_CLASS_BE;
  2115. break;
  2116. case IOPRIO_CLASS_IDLE:
  2117. cfqq->ioprio_class = IOPRIO_CLASS_IDLE;
  2118. cfqq->ioprio = 7;
  2119. cfq_clear_cfqq_idle_window(cfqq);
  2120. break;
  2121. }
  2122. /*
  2123. * keep track of original prio settings in case we have to temporarily
  2124. * elevate the priority of this queue
  2125. */
  2126. cfqq->org_ioprio = cfqq->ioprio;
  2127. cfqq->org_ioprio_class = cfqq->ioprio_class;
  2128. cfq_clear_cfqq_prio_changed(cfqq);
  2129. }
  2130. static void changed_ioprio(struct io_context *ioc, struct cfq_io_context *cic)
  2131. {
  2132. struct cfq_data *cfqd = cic->key;
  2133. struct cfq_queue *cfqq;
  2134. unsigned long flags;
  2135. if (unlikely(!cfqd))
  2136. return;
  2137. spin_lock_irqsave(cfqd->queue->queue_lock, flags);
  2138. cfqq = cic->cfqq[BLK_RW_ASYNC];
  2139. if (cfqq) {
  2140. struct cfq_queue *new_cfqq;
  2141. new_cfqq = cfq_get_queue(cfqd, BLK_RW_ASYNC, cic->ioc,
  2142. GFP_ATOMIC);
  2143. if (new_cfqq) {
  2144. cic->cfqq[BLK_RW_ASYNC] = new_cfqq;
  2145. cfq_put_queue(cfqq);
  2146. }
  2147. }
  2148. cfqq = cic->cfqq[BLK_RW_SYNC];
  2149. if (cfqq)
  2150. cfq_mark_cfqq_prio_changed(cfqq);
  2151. spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
  2152. }
  2153. static void cfq_ioc_set_ioprio(struct io_context *ioc)
  2154. {
  2155. call_for_each_cic(ioc, changed_ioprio);
  2156. ioc->ioprio_changed = 0;
  2157. }
  2158. static void cfq_init_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
  2159. pid_t pid, bool is_sync)
  2160. {
  2161. RB_CLEAR_NODE(&cfqq->rb_node);
  2162. RB_CLEAR_NODE(&cfqq->p_node);
  2163. INIT_LIST_HEAD(&cfqq->fifo);
  2164. atomic_set(&cfqq->ref, 0);
  2165. cfqq->cfqd = cfqd;
  2166. cfq_mark_cfqq_prio_changed(cfqq);
  2167. if (is_sync) {
  2168. if (!cfq_class_idle(cfqq))
  2169. cfq_mark_cfqq_idle_window(cfqq);
  2170. cfq_mark_cfqq_sync(cfqq);
  2171. }
  2172. cfqq->pid = pid;
  2173. }
  2174. static struct cfq_queue *
  2175. cfq_find_alloc_queue(struct cfq_data *cfqd, bool is_sync,
  2176. struct io_context *ioc, gfp_t gfp_mask)
  2177. {
  2178. struct cfq_queue *cfqq, *new_cfqq = NULL;
  2179. struct cfq_io_context *cic;
  2180. struct cfq_group *cfqg;
  2181. retry:
  2182. cfqg = cfq_get_cfqg(cfqd, 1);
  2183. cic = cfq_cic_lookup(cfqd, ioc);
  2184. /* cic always exists here */
  2185. cfqq = cic_to_cfqq(cic, is_sync);
  2186. /*
  2187. * Always try a new alloc if we fell back to the OOM cfqq
  2188. * originally, since it should just be a temporary situation.
  2189. */
  2190. if (!cfqq || cfqq == &cfqd->oom_cfqq) {
  2191. cfqq = NULL;
  2192. if (new_cfqq) {
  2193. cfqq = new_cfqq;
  2194. new_cfqq = NULL;
  2195. } else if (gfp_mask & __GFP_WAIT) {
  2196. spin_unlock_irq(cfqd->queue->queue_lock);
  2197. new_cfqq = kmem_cache_alloc_node(cfq_pool,
  2198. gfp_mask | __GFP_ZERO,
  2199. cfqd->queue->node);
  2200. spin_lock_irq(cfqd->queue->queue_lock);
  2201. if (new_cfqq)
  2202. goto retry;
  2203. } else {
  2204. cfqq = kmem_cache_alloc_node(cfq_pool,
  2205. gfp_mask | __GFP_ZERO,
  2206. cfqd->queue->node);
  2207. }
  2208. if (cfqq) {
  2209. cfq_init_cfqq(cfqd, cfqq, current->pid, is_sync);
  2210. cfq_init_prio_data(cfqq, ioc);
  2211. cfq_link_cfqq_cfqg(cfqq, cfqg);
  2212. cfq_log_cfqq(cfqd, cfqq, "alloced");
  2213. } else
  2214. cfqq = &cfqd->oom_cfqq;
  2215. }
  2216. if (new_cfqq)
  2217. kmem_cache_free(cfq_pool, new_cfqq);
  2218. return cfqq;
  2219. }
  2220. static struct cfq_queue **
  2221. cfq_async_queue_prio(struct cfq_data *cfqd, int ioprio_class, int ioprio)
  2222. {
  2223. switch (ioprio_class) {
  2224. case IOPRIO_CLASS_RT:
  2225. return &cfqd->async_cfqq[0][ioprio];
  2226. case IOPRIO_CLASS_BE:
  2227. return &cfqd->async_cfqq[1][ioprio];
  2228. case IOPRIO_CLASS_IDLE:
  2229. return &cfqd->async_idle_cfqq;
  2230. default:
  2231. BUG();
  2232. }
  2233. }
  2234. static struct cfq_queue *
  2235. cfq_get_queue(struct cfq_data *cfqd, bool is_sync, struct io_context *ioc,
  2236. gfp_t gfp_mask)
  2237. {
  2238. const int ioprio = task_ioprio(ioc);
  2239. const int ioprio_class = task_ioprio_class(ioc);
  2240. struct cfq_queue **async_cfqq = NULL;
  2241. struct cfq_queue *cfqq = NULL;
  2242. if (!is_sync) {
  2243. async_cfqq = cfq_async_queue_prio(cfqd, ioprio_class, ioprio);
  2244. cfqq = *async_cfqq;
  2245. }
  2246. if (!cfqq)
  2247. cfqq = cfq_find_alloc_queue(cfqd, is_sync, ioc, gfp_mask);
  2248. /*
  2249. * pin the queue now that it's allocated, scheduler exit will prune it
  2250. */
  2251. if (!is_sync && !(*async_cfqq)) {
  2252. atomic_inc(&cfqq->ref);
  2253. *async_cfqq = cfqq;
  2254. }
  2255. atomic_inc(&cfqq->ref);
  2256. return cfqq;
  2257. }
  2258. /*
  2259. * We drop cfq io contexts lazily, so we may find a dead one.
  2260. */
  2261. static void
  2262. cfq_drop_dead_cic(struct cfq_data *cfqd, struct io_context *ioc,
  2263. struct cfq_io_context *cic)
  2264. {
  2265. unsigned long flags;
  2266. WARN_ON(!list_empty(&cic->queue_list));
  2267. spin_lock_irqsave(&ioc->lock, flags);
  2268. BUG_ON(ioc->ioc_data == cic);
  2269. radix_tree_delete(&ioc->radix_root, (unsigned long) cfqd);
  2270. hlist_del_rcu(&cic->cic_list);
  2271. spin_unlock_irqrestore(&ioc->lock, flags);
  2272. cfq_cic_free(cic);
  2273. }
  2274. static struct cfq_io_context *
  2275. cfq_cic_lookup(struct cfq_data *cfqd, struct io_context *ioc)
  2276. {
  2277. struct cfq_io_context *cic;
  2278. unsigned long flags;
  2279. void *k;
  2280. if (unlikely(!ioc))
  2281. return NULL;
  2282. rcu_read_lock();
  2283. /*
  2284. * we maintain a last-hit cache, to avoid browsing over the tree
  2285. */
  2286. cic = rcu_dereference(ioc->ioc_data);
  2287. if (cic && cic->key == cfqd) {
  2288. rcu_read_unlock();
  2289. return cic;
  2290. }
  2291. do {
  2292. cic = radix_tree_lookup(&ioc->radix_root, (unsigned long) cfqd);
  2293. rcu_read_unlock();
  2294. if (!cic)
  2295. break;
  2296. /* ->key must be copied to avoid race with cfq_exit_queue() */
  2297. k = cic->key;
  2298. if (unlikely(!k)) {
  2299. cfq_drop_dead_cic(cfqd, ioc, cic);
  2300. rcu_read_lock();
  2301. continue;
  2302. }
  2303. spin_lock_irqsave(&ioc->lock, flags);
  2304. rcu_assign_pointer(ioc->ioc_data, cic);
  2305. spin_unlock_irqrestore(&ioc->lock, flags);
  2306. break;
  2307. } while (1);
  2308. return cic;
  2309. }
  2310. /*
  2311. * Add cic into ioc, using cfqd as the search key. This enables us to lookup
  2312. * the process specific cfq io context when entered from the block layer.
  2313. * Also adds the cic to a per-cfqd list, used when this queue is removed.
  2314. */
  2315. static int cfq_cic_link(struct cfq_data *cfqd, struct io_context *ioc,
  2316. struct cfq_io_context *cic, gfp_t gfp_mask)
  2317. {
  2318. unsigned long flags;
  2319. int ret;
  2320. ret = radix_tree_preload(gfp_mask);
  2321. if (!ret) {
  2322. cic->ioc = ioc;
  2323. cic->key = cfqd;
  2324. spin_lock_irqsave(&ioc->lock, flags);
  2325. ret = radix_tree_insert(&ioc->radix_root,
  2326. (unsigned long) cfqd, cic);
  2327. if (!ret)
  2328. hlist_add_head_rcu(&cic->cic_list, &ioc->cic_list);
  2329. spin_unlock_irqrestore(&ioc->lock, flags);
  2330. radix_tree_preload_end();
  2331. if (!ret) {
  2332. spin_lock_irqsave(cfqd->queue->queue_lock, flags);
  2333. list_add(&cic->queue_list, &cfqd->cic_list);
  2334. spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
  2335. }
  2336. }
  2337. if (ret)
  2338. printk(KERN_ERR "cfq: cic link failed!\n");
  2339. return ret;
  2340. }
  2341. /*
  2342. * Setup general io context and cfq io context. There can be several cfq
  2343. * io contexts per general io context, if this process is doing io to more
  2344. * than one device managed by cfq.
  2345. */
  2346. static struct cfq_io_context *
  2347. cfq_get_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
  2348. {
  2349. struct io_context *ioc = NULL;
  2350. struct cfq_io_context *cic;
  2351. might_sleep_if(gfp_mask & __GFP_WAIT);
  2352. ioc = get_io_context(gfp_mask, cfqd->queue->node);
  2353. if (!ioc)
  2354. return NULL;
  2355. cic = cfq_cic_lookup(cfqd, ioc);
  2356. if (cic)
  2357. goto out;
  2358. cic = cfq_alloc_io_context(cfqd, gfp_mask);
  2359. if (cic == NULL)
  2360. goto err;
  2361. if (cfq_cic_link(cfqd, ioc, cic, gfp_mask))
  2362. goto err_free;
  2363. out:
  2364. smp_read_barrier_depends();
  2365. if (unlikely(ioc->ioprio_changed))
  2366. cfq_ioc_set_ioprio(ioc);
  2367. return cic;
  2368. err_free:
  2369. cfq_cic_free(cic);
  2370. err:
  2371. put_io_context(ioc);
  2372. return NULL;
  2373. }
  2374. static void
  2375. cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_io_context *cic)
  2376. {
  2377. unsigned long elapsed = jiffies - cic->last_end_request;
  2378. unsigned long ttime = min(elapsed, 2UL * cfqd->cfq_slice_idle);
  2379. cic->ttime_samples = (7*cic->ttime_samples + 256) / 8;
  2380. cic->ttime_total = (7*cic->ttime_total + 256*ttime) / 8;
  2381. cic->ttime_mean = (cic->ttime_total + 128) / cic->ttime_samples;
  2382. }
  2383. static void
  2384. cfq_update_io_seektime(struct cfq_data *cfqd, struct cfq_queue *cfqq,
  2385. struct request *rq)
  2386. {
  2387. sector_t sdist;
  2388. u64 total;
  2389. if (!cfqq->last_request_pos)
  2390. sdist = 0;
  2391. else if (cfqq->last_request_pos < blk_rq_pos(rq))
  2392. sdist = blk_rq_pos(rq) - cfqq->last_request_pos;
  2393. else
  2394. sdist = cfqq->last_request_pos - blk_rq_pos(rq);
  2395. /*
  2396. * Don't allow the seek distance to get too large from the
  2397. * odd fragment, pagein, etc
  2398. */
  2399. if (cfqq->seek_samples <= 60) /* second&third seek */
  2400. sdist = min(sdist, (cfqq->seek_mean * 4) + 2*1024*1024);
  2401. else
  2402. sdist = min(sdist, (cfqq->seek_mean * 4) + 2*1024*64);
  2403. cfqq->seek_samples = (7*cfqq->seek_samples + 256) / 8;
  2404. cfqq->seek_total = (7*cfqq->seek_total + (u64)256*sdist) / 8;
  2405. total = cfqq->seek_total + (cfqq->seek_samples/2);
  2406. do_div(total, cfqq->seek_samples);
  2407. cfqq->seek_mean = (sector_t)total;
  2408. /*
  2409. * If this cfqq is shared between multiple processes, check to
  2410. * make sure that those processes are still issuing I/Os within
  2411. * the mean seek distance. If not, it may be time to break the
  2412. * queues apart again.
  2413. */
  2414. if (cfq_cfqq_coop(cfqq)) {
  2415. if (CFQQ_SEEKY(cfqq) && !cfqq->seeky_start)
  2416. cfqq->seeky_start = jiffies;
  2417. else if (!CFQQ_SEEKY(cfqq))
  2418. cfqq->seeky_start = 0;
  2419. }
  2420. }
  2421. /*
  2422. * Disable idle window if the process thinks too long or seeks so much that
  2423. * it doesn't matter
  2424. */
  2425. static void
  2426. cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq,
  2427. struct cfq_io_context *cic)
  2428. {
  2429. int old_idle, enable_idle;
  2430. /*
  2431. * Don't idle for async or idle io prio class
  2432. */
  2433. if (!cfq_cfqq_sync(cfqq) || cfq_class_idle(cfqq))
  2434. return;
  2435. enable_idle = old_idle = cfq_cfqq_idle_window(cfqq);
  2436. if (cfqq->queued[0] + cfqq->queued[1] >= 4)
  2437. cfq_mark_cfqq_deep(cfqq);
  2438. if (!atomic_read(&cic->ioc->nr_tasks) || !cfqd->cfq_slice_idle ||
  2439. (!cfq_cfqq_deep(cfqq) && sample_valid(cfqq->seek_samples)
  2440. && CFQQ_SEEKY(cfqq)))
  2441. enable_idle = 0;
  2442. else if (sample_valid(cic->ttime_samples)) {
  2443. if (cic->ttime_mean > cfqd->cfq_slice_idle)
  2444. enable_idle = 0;
  2445. else
  2446. enable_idle = 1;
  2447. }
  2448. if (old_idle != enable_idle) {
  2449. cfq_log_cfqq(cfqd, cfqq, "idle=%d", enable_idle);
  2450. if (enable_idle)
  2451. cfq_mark_cfqq_idle_window(cfqq);
  2452. else
  2453. cfq_clear_cfqq_idle_window(cfqq);
  2454. }
  2455. }
  2456. /*
  2457. * Check if new_cfqq should preempt the currently active queue. Return 0 for
  2458. * no or if we aren't sure, a 1 will cause a preempt.
  2459. */
  2460. static bool
  2461. cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
  2462. struct request *rq)
  2463. {
  2464. struct cfq_queue *cfqq;
  2465. cfqq = cfqd->active_queue;
  2466. if (!cfqq)
  2467. return false;
  2468. if (cfq_slice_used(cfqq))
  2469. return true;
  2470. if (cfq_class_idle(new_cfqq))
  2471. return false;
  2472. if (cfq_class_idle(cfqq))
  2473. return true;
  2474. /* Allow preemption only if we are idling on sync-noidle tree */
  2475. if (cfqd->serving_type == SYNC_NOIDLE_WORKLOAD &&
  2476. cfqq_type(new_cfqq) == SYNC_NOIDLE_WORKLOAD &&
  2477. new_cfqq->service_tree->count == 2 &&
  2478. RB_EMPTY_ROOT(&cfqq->sort_list))
  2479. return true;
  2480. /*
  2481. * if the new request is sync, but the currently running queue is
  2482. * not, let the sync request have priority.
  2483. */
  2484. if (rq_is_sync(rq) && !cfq_cfqq_sync(cfqq))
  2485. return true;
  2486. /*
  2487. * So both queues are sync. Let the new request get disk time if
  2488. * it's a metadata request and the current queue is doing regular IO.
  2489. */
  2490. if (rq_is_meta(rq) && !cfqq->meta_pending)
  2491. return true;
  2492. /*
  2493. * Allow an RT request to pre-empt an ongoing non-RT cfqq timeslice.
  2494. */
  2495. if (cfq_class_rt(new_cfqq) && !cfq_class_rt(cfqq))
  2496. return true;
  2497. if (!cfqd->active_cic || !cfq_cfqq_wait_request(cfqq))
  2498. return false;
  2499. /*
  2500. * if this request is as-good as one we would expect from the
  2501. * current cfqq, let it preempt
  2502. */
  2503. if (cfq_rq_close(cfqd, cfqq, rq))
  2504. return true;
  2505. return false;
  2506. }
  2507. /*
  2508. * cfqq preempts the active queue. if we allowed preempt with no slice left,
  2509. * let it have half of its nominal slice.
  2510. */
  2511. static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  2512. {
  2513. cfq_log_cfqq(cfqd, cfqq, "preempt");
  2514. cfq_slice_expired(cfqd, 1);
  2515. /*
  2516. * Put the new queue at the front of the of the current list,
  2517. * so we know that it will be selected next.
  2518. */
  2519. BUG_ON(!cfq_cfqq_on_rr(cfqq));
  2520. cfq_service_tree_add(cfqd, cfqq, 1);
  2521. cfqq->slice_end = 0;
  2522. cfq_mark_cfqq_slice_new(cfqq);
  2523. }
  2524. /*
  2525. * Called when a new fs request (rq) is added (to cfqq). Check if there's
  2526. * something we should do about it
  2527. */
  2528. static void
  2529. cfq_rq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
  2530. struct request *rq)
  2531. {
  2532. struct cfq_io_context *cic = RQ_CIC(rq);
  2533. cfqd->rq_queued++;
  2534. if (rq_is_meta(rq))
  2535. cfqq->meta_pending++;
  2536. cfq_update_io_thinktime(cfqd, cic);
  2537. cfq_update_io_seektime(cfqd, cfqq, rq);
  2538. cfq_update_idle_window(cfqd, cfqq, cic);
  2539. cfqq->last_request_pos = blk_rq_pos(rq) + blk_rq_sectors(rq);
  2540. if (cfqq == cfqd->active_queue) {
  2541. /*
  2542. * Remember that we saw a request from this process, but
  2543. * don't start queuing just yet. Otherwise we risk seeing lots
  2544. * of tiny requests, because we disrupt the normal plugging
  2545. * and merging. If the request is already larger than a single
  2546. * page, let it rip immediately. For that case we assume that
  2547. * merging is already done. Ditto for a busy system that
  2548. * has other work pending, don't risk delaying until the
  2549. * idle timer unplug to continue working.
  2550. */
  2551. if (cfq_cfqq_wait_request(cfqq)) {
  2552. if (blk_rq_bytes(rq) > PAGE_CACHE_SIZE ||
  2553. cfqd->busy_queues > 1) {
  2554. del_timer(&cfqd->idle_slice_timer);
  2555. __blk_run_queue(cfqd->queue);
  2556. } else
  2557. cfq_mark_cfqq_must_dispatch(cfqq);
  2558. }
  2559. } else if (cfq_should_preempt(cfqd, cfqq, rq)) {
  2560. /*
  2561. * not the active queue - expire current slice if it is
  2562. * idle and has expired it's mean thinktime or this new queue
  2563. * has some old slice time left and is of higher priority or
  2564. * this new queue is RT and the current one is BE
  2565. */
  2566. cfq_preempt_queue(cfqd, cfqq);
  2567. __blk_run_queue(cfqd->queue);
  2568. }
  2569. }
  2570. static void cfq_insert_request(struct request_queue *q, struct request *rq)
  2571. {
  2572. struct cfq_data *cfqd = q->elevator->elevator_data;
  2573. struct cfq_queue *cfqq = RQ_CFQQ(rq);
  2574. cfq_log_cfqq(cfqd, cfqq, "insert_request");
  2575. cfq_init_prio_data(cfqq, RQ_CIC(rq)->ioc);
  2576. rq_set_fifo_time(rq, jiffies + cfqd->cfq_fifo_expire[rq_is_sync(rq)]);
  2577. list_add_tail(&rq->queuelist, &cfqq->fifo);
  2578. cfq_add_rq_rb(rq);
  2579. cfq_rq_enqueued(cfqd, cfqq, rq);
  2580. }
  2581. /*
  2582. * Update hw_tag based on peak queue depth over 50 samples under
  2583. * sufficient load.
  2584. */
  2585. static void cfq_update_hw_tag(struct cfq_data *cfqd)
  2586. {
  2587. struct cfq_queue *cfqq = cfqd->active_queue;
  2588. if (rq_in_driver(cfqd) > cfqd->hw_tag_est_depth)
  2589. cfqd->hw_tag_est_depth = rq_in_driver(cfqd);
  2590. if (cfqd->hw_tag == 1)
  2591. return;
  2592. if (cfqd->rq_queued <= CFQ_HW_QUEUE_MIN &&
  2593. rq_in_driver(cfqd) <= CFQ_HW_QUEUE_MIN)
  2594. return;
  2595. /*
  2596. * If active queue hasn't enough requests and can idle, cfq might not
  2597. * dispatch sufficient requests to hardware. Don't zero hw_tag in this
  2598. * case
  2599. */
  2600. if (cfqq && cfq_cfqq_idle_window(cfqq) &&
  2601. cfqq->dispatched + cfqq->queued[0] + cfqq->queued[1] <
  2602. CFQ_HW_QUEUE_MIN && rq_in_driver(cfqd) < CFQ_HW_QUEUE_MIN)
  2603. return;
  2604. if (cfqd->hw_tag_samples++ < 50)
  2605. return;
  2606. if (cfqd->hw_tag_est_depth >= CFQ_HW_QUEUE_MIN)
  2607. cfqd->hw_tag = 1;
  2608. else
  2609. cfqd->hw_tag = 0;
  2610. }
  2611. static void cfq_completed_request(struct request_queue *q, struct request *rq)
  2612. {
  2613. struct cfq_queue *cfqq = RQ_CFQQ(rq);
  2614. struct cfq_data *cfqd = cfqq->cfqd;
  2615. const int sync = rq_is_sync(rq);
  2616. unsigned long now;
  2617. now = jiffies;
  2618. cfq_log_cfqq(cfqd, cfqq, "complete rqnoidle %d", !!rq_noidle(rq));
  2619. cfq_update_hw_tag(cfqd);
  2620. WARN_ON(!cfqd->rq_in_driver[sync]);
  2621. WARN_ON(!cfqq->dispatched);
  2622. cfqd->rq_in_driver[sync]--;
  2623. cfqq->dispatched--;
  2624. if (cfq_cfqq_sync(cfqq))
  2625. cfqd->sync_flight--;
  2626. if (sync) {
  2627. RQ_CIC(rq)->last_end_request = now;
  2628. cfqd->last_end_sync_rq = now;
  2629. }
  2630. /*
  2631. * If this is the active queue, check if it needs to be expired,
  2632. * or if we want to idle in case it has no pending requests.
  2633. */
  2634. if (cfqd->active_queue == cfqq) {
  2635. const bool cfqq_empty = RB_EMPTY_ROOT(&cfqq->sort_list);
  2636. if (cfq_cfqq_slice_new(cfqq)) {
  2637. cfq_set_prio_slice(cfqd, cfqq);
  2638. cfq_clear_cfqq_slice_new(cfqq);
  2639. }
  2640. /*
  2641. * Idling is not enabled on:
  2642. * - expired queues
  2643. * - idle-priority queues
  2644. * - async queues
  2645. * - queues with still some requests queued
  2646. * - when there is a close cooperator
  2647. */
  2648. if (cfq_slice_used(cfqq) || cfq_class_idle(cfqq))
  2649. cfq_slice_expired(cfqd, 1);
  2650. else if (sync && cfqq_empty &&
  2651. !cfq_close_cooperator(cfqd, cfqq)) {
  2652. cfqd->noidle_tree_requires_idle |= !rq_noidle(rq);
  2653. /*
  2654. * Idling is enabled for SYNC_WORKLOAD.
  2655. * SYNC_NOIDLE_WORKLOAD idles at the end of the tree
  2656. * only if we processed at least one !rq_noidle request
  2657. */
  2658. if (cfqd->serving_type == SYNC_WORKLOAD
  2659. || cfqd->noidle_tree_requires_idle)
  2660. cfq_arm_slice_timer(cfqd);
  2661. }
  2662. }
  2663. if (!rq_in_driver(cfqd))
  2664. cfq_schedule_dispatch(cfqd);
  2665. }
  2666. /*
  2667. * we temporarily boost lower priority queues if they are holding fs exclusive
  2668. * resources. they are boosted to normal prio (CLASS_BE/4)
  2669. */
  2670. static void cfq_prio_boost(struct cfq_queue *cfqq)
  2671. {
  2672. if (has_fs_excl()) {
  2673. /*
  2674. * boost idle prio on transactions that would lock out other
  2675. * users of the filesystem
  2676. */
  2677. if (cfq_class_idle(cfqq))
  2678. cfqq->ioprio_class = IOPRIO_CLASS_BE;
  2679. if (cfqq->ioprio > IOPRIO_NORM)
  2680. cfqq->ioprio = IOPRIO_NORM;
  2681. } else {
  2682. /*
  2683. * unboost the queue (if needed)
  2684. */
  2685. cfqq->ioprio_class = cfqq->org_ioprio_class;
  2686. cfqq->ioprio = cfqq->org_ioprio;
  2687. }
  2688. }
  2689. static inline int __cfq_may_queue(struct cfq_queue *cfqq)
  2690. {
  2691. if (cfq_cfqq_wait_request(cfqq) && !cfq_cfqq_must_alloc_slice(cfqq)) {
  2692. cfq_mark_cfqq_must_alloc_slice(cfqq);
  2693. return ELV_MQUEUE_MUST;
  2694. }
  2695. return ELV_MQUEUE_MAY;
  2696. }
  2697. static int cfq_may_queue(struct request_queue *q, int rw)
  2698. {
  2699. struct cfq_data *cfqd = q->elevator->elevator_data;
  2700. struct task_struct *tsk = current;
  2701. struct cfq_io_context *cic;
  2702. struct cfq_queue *cfqq;
  2703. /*
  2704. * don't force setup of a queue from here, as a call to may_queue
  2705. * does not necessarily imply that a request actually will be queued.
  2706. * so just lookup a possibly existing queue, or return 'may queue'
  2707. * if that fails
  2708. */
  2709. cic = cfq_cic_lookup(cfqd, tsk->io_context);
  2710. if (!cic)
  2711. return ELV_MQUEUE_MAY;
  2712. cfqq = cic_to_cfqq(cic, rw_is_sync(rw));
  2713. if (cfqq) {
  2714. cfq_init_prio_data(cfqq, cic->ioc);
  2715. cfq_prio_boost(cfqq);
  2716. return __cfq_may_queue(cfqq);
  2717. }
  2718. return ELV_MQUEUE_MAY;
  2719. }
  2720. /*
  2721. * queue lock held here
  2722. */
  2723. static void cfq_put_request(struct request *rq)
  2724. {
  2725. struct cfq_queue *cfqq = RQ_CFQQ(rq);
  2726. if (cfqq) {
  2727. const int rw = rq_data_dir(rq);
  2728. BUG_ON(!cfqq->allocated[rw]);
  2729. cfqq->allocated[rw]--;
  2730. put_io_context(RQ_CIC(rq)->ioc);
  2731. rq->elevator_private = NULL;
  2732. rq->elevator_private2 = NULL;
  2733. cfq_put_queue(cfqq);
  2734. }
  2735. }
  2736. static struct cfq_queue *
  2737. cfq_merge_cfqqs(struct cfq_data *cfqd, struct cfq_io_context *cic,
  2738. struct cfq_queue *cfqq)
  2739. {
  2740. cfq_log_cfqq(cfqd, cfqq, "merging with queue %p", cfqq->new_cfqq);
  2741. cic_set_cfqq(cic, cfqq->new_cfqq, 1);
  2742. cfq_mark_cfqq_coop(cfqq->new_cfqq);
  2743. cfq_put_queue(cfqq);
  2744. return cic_to_cfqq(cic, 1);
  2745. }
  2746. static int should_split_cfqq(struct cfq_queue *cfqq)
  2747. {
  2748. if (cfqq->seeky_start &&
  2749. time_after(jiffies, cfqq->seeky_start + CFQQ_COOP_TOUT))
  2750. return 1;
  2751. return 0;
  2752. }
  2753. /*
  2754. * Returns NULL if a new cfqq should be allocated, or the old cfqq if this
  2755. * was the last process referring to said cfqq.
  2756. */
  2757. static struct cfq_queue *
  2758. split_cfqq(struct cfq_io_context *cic, struct cfq_queue *cfqq)
  2759. {
  2760. if (cfqq_process_refs(cfqq) == 1) {
  2761. cfqq->seeky_start = 0;
  2762. cfqq->pid = current->pid;
  2763. cfq_clear_cfqq_coop(cfqq);
  2764. return cfqq;
  2765. }
  2766. cic_set_cfqq(cic, NULL, 1);
  2767. cfq_put_queue(cfqq);
  2768. return NULL;
  2769. }
  2770. /*
  2771. * Allocate cfq data structures associated with this request.
  2772. */
  2773. static int
  2774. cfq_set_request(struct request_queue *q, struct request *rq, gfp_t gfp_mask)
  2775. {
  2776. struct cfq_data *cfqd = q->elevator->elevator_data;
  2777. struct cfq_io_context *cic;
  2778. const int rw = rq_data_dir(rq);
  2779. const bool is_sync = rq_is_sync(rq);
  2780. struct cfq_queue *cfqq;
  2781. unsigned long flags;
  2782. might_sleep_if(gfp_mask & __GFP_WAIT);
  2783. cic = cfq_get_io_context(cfqd, gfp_mask);
  2784. spin_lock_irqsave(q->queue_lock, flags);
  2785. if (!cic)
  2786. goto queue_fail;
  2787. new_queue:
  2788. cfqq = cic_to_cfqq(cic, is_sync);
  2789. if (!cfqq || cfqq == &cfqd->oom_cfqq) {
  2790. cfqq = cfq_get_queue(cfqd, is_sync, cic->ioc, gfp_mask);
  2791. cic_set_cfqq(cic, cfqq, is_sync);
  2792. } else {
  2793. /*
  2794. * If the queue was seeky for too long, break it apart.
  2795. */
  2796. if (cfq_cfqq_coop(cfqq) && should_split_cfqq(cfqq)) {
  2797. cfq_log_cfqq(cfqd, cfqq, "breaking apart cfqq");
  2798. cfqq = split_cfqq(cic, cfqq);
  2799. if (!cfqq)
  2800. goto new_queue;
  2801. }
  2802. /*
  2803. * Check to see if this queue is scheduled to merge with
  2804. * another, closely cooperating queue. The merging of
  2805. * queues happens here as it must be done in process context.
  2806. * The reference on new_cfqq was taken in merge_cfqqs.
  2807. */
  2808. if (cfqq->new_cfqq)
  2809. cfqq = cfq_merge_cfqqs(cfqd, cic, cfqq);
  2810. }
  2811. cfqq->allocated[rw]++;
  2812. atomic_inc(&cfqq->ref);
  2813. spin_unlock_irqrestore(q->queue_lock, flags);
  2814. rq->elevator_private = cic;
  2815. rq->elevator_private2 = cfqq;
  2816. return 0;
  2817. queue_fail:
  2818. if (cic)
  2819. put_io_context(cic->ioc);
  2820. cfq_schedule_dispatch(cfqd);
  2821. spin_unlock_irqrestore(q->queue_lock, flags);
  2822. cfq_log(cfqd, "set_request fail");
  2823. return 1;
  2824. }
  2825. static void cfq_kick_queue(struct work_struct *work)
  2826. {
  2827. struct cfq_data *cfqd =
  2828. container_of(work, struct cfq_data, unplug_work);
  2829. struct request_queue *q = cfqd->queue;
  2830. spin_lock_irq(q->queue_lock);
  2831. __blk_run_queue(cfqd->queue);
  2832. spin_unlock_irq(q->queue_lock);
  2833. }
  2834. /*
  2835. * Timer running if the active_queue is currently idling inside its time slice
  2836. */
  2837. static void cfq_idle_slice_timer(unsigned long data)
  2838. {
  2839. struct cfq_data *cfqd = (struct cfq_data *) data;
  2840. struct cfq_queue *cfqq;
  2841. unsigned long flags;
  2842. int timed_out = 1;
  2843. cfq_log(cfqd, "idle timer fired");
  2844. spin_lock_irqsave(cfqd->queue->queue_lock, flags);
  2845. cfqq = cfqd->active_queue;
  2846. if (cfqq) {
  2847. timed_out = 0;
  2848. /*
  2849. * We saw a request before the queue expired, let it through
  2850. */
  2851. if (cfq_cfqq_must_dispatch(cfqq))
  2852. goto out_kick;
  2853. /*
  2854. * expired
  2855. */
  2856. if (cfq_slice_used(cfqq))
  2857. goto expire;
  2858. /*
  2859. * only expire and reinvoke request handler, if there are
  2860. * other queues with pending requests
  2861. */
  2862. if (!cfqd->busy_queues)
  2863. goto out_cont;
  2864. /*
  2865. * not expired and it has a request pending, let it dispatch
  2866. */
  2867. if (!RB_EMPTY_ROOT(&cfqq->sort_list))
  2868. goto out_kick;
  2869. /*
  2870. * Queue depth flag is reset only when the idle didn't succeed
  2871. */
  2872. cfq_clear_cfqq_deep(cfqq);
  2873. }
  2874. expire:
  2875. cfq_slice_expired(cfqd, timed_out);
  2876. out_kick:
  2877. cfq_schedule_dispatch(cfqd);
  2878. out_cont:
  2879. spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
  2880. }
  2881. static void cfq_shutdown_timer_wq(struct cfq_data *cfqd)
  2882. {
  2883. del_timer_sync(&cfqd->idle_slice_timer);
  2884. cancel_work_sync(&cfqd->unplug_work);
  2885. }
  2886. static void cfq_put_async_queues(struct cfq_data *cfqd)
  2887. {
  2888. int i;
  2889. for (i = 0; i < IOPRIO_BE_NR; i++) {
  2890. if (cfqd->async_cfqq[0][i])
  2891. cfq_put_queue(cfqd->async_cfqq[0][i]);
  2892. if (cfqd->async_cfqq[1][i])
  2893. cfq_put_queue(cfqd->async_cfqq[1][i]);
  2894. }
  2895. if (cfqd->async_idle_cfqq)
  2896. cfq_put_queue(cfqd->async_idle_cfqq);
  2897. }
  2898. static void cfq_exit_queue(struct elevator_queue *e)
  2899. {
  2900. struct cfq_data *cfqd = e->elevator_data;
  2901. struct request_queue *q = cfqd->queue;
  2902. cfq_shutdown_timer_wq(cfqd);
  2903. spin_lock_irq(q->queue_lock);
  2904. if (cfqd->active_queue)
  2905. __cfq_slice_expired(cfqd, cfqd->active_queue, 0);
  2906. while (!list_empty(&cfqd->cic_list)) {
  2907. struct cfq_io_context *cic = list_entry(cfqd->cic_list.next,
  2908. struct cfq_io_context,
  2909. queue_list);
  2910. __cfq_exit_single_io_context(cfqd, cic);
  2911. }
  2912. cfq_put_async_queues(cfqd);
  2913. cfq_release_cfq_groups(cfqd);
  2914. blkiocg_del_blkio_group(&cfqd->root_group.blkg);
  2915. spin_unlock_irq(q->queue_lock);
  2916. cfq_shutdown_timer_wq(cfqd);
  2917. /* Wait for cfqg->blkg->key accessors to exit their grace periods. */
  2918. synchronize_rcu();
  2919. kfree(cfqd);
  2920. }
  2921. static void *cfq_init_queue(struct request_queue *q)
  2922. {
  2923. struct cfq_data *cfqd;
  2924. int i, j;
  2925. struct cfq_group *cfqg;
  2926. struct cfq_rb_root *st;
  2927. cfqd = kmalloc_node(sizeof(*cfqd), GFP_KERNEL | __GFP_ZERO, q->node);
  2928. if (!cfqd)
  2929. return NULL;
  2930. /* Init root service tree */
  2931. cfqd->grp_service_tree = CFQ_RB_ROOT;
  2932. /* Init root group */
  2933. cfqg = &cfqd->root_group;
  2934. for_each_cfqg_st(cfqg, i, j, st)
  2935. *st = CFQ_RB_ROOT;
  2936. RB_CLEAR_NODE(&cfqg->rb_node);
  2937. /* Give preference to root group over other groups */
  2938. cfqg->weight = 2*BLKIO_WEIGHT_DEFAULT;
  2939. #ifdef CONFIG_CFQ_GROUP_IOSCHED
  2940. /*
  2941. * Take a reference to root group which we never drop. This is just
  2942. * to make sure that cfq_put_cfqg() does not try to kfree root group
  2943. */
  2944. atomic_set(&cfqg->ref, 1);
  2945. blkiocg_add_blkio_group(&blkio_root_cgroup, &cfqg->blkg, (void *)cfqd);
  2946. #endif
  2947. /*
  2948. * Not strictly needed (since RB_ROOT just clears the node and we
  2949. * zeroed cfqd on alloc), but better be safe in case someone decides
  2950. * to add magic to the rb code
  2951. */
  2952. for (i = 0; i < CFQ_PRIO_LISTS; i++)
  2953. cfqd->prio_trees[i] = RB_ROOT;
  2954. /*
  2955. * Our fallback cfqq if cfq_find_alloc_queue() runs into OOM issues.
  2956. * Grab a permanent reference to it, so that the normal code flow
  2957. * will not attempt to free it.
  2958. */
  2959. cfq_init_cfqq(cfqd, &cfqd->oom_cfqq, 1, 0);
  2960. atomic_inc(&cfqd->oom_cfqq.ref);
  2961. cfq_link_cfqq_cfqg(&cfqd->oom_cfqq, &cfqd->root_group);
  2962. INIT_LIST_HEAD(&cfqd->cic_list);
  2963. cfqd->queue = q;
  2964. init_timer(&cfqd->idle_slice_timer);
  2965. cfqd->idle_slice_timer.function = cfq_idle_slice_timer;
  2966. cfqd->idle_slice_timer.data = (unsigned long) cfqd;
  2967. INIT_WORK(&cfqd->unplug_work, cfq_kick_queue);
  2968. cfqd->cfq_quantum = cfq_quantum;
  2969. cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0];
  2970. cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1];
  2971. cfqd->cfq_back_max = cfq_back_max;
  2972. cfqd->cfq_back_penalty = cfq_back_penalty;
  2973. cfqd->cfq_slice[0] = cfq_slice_async;
  2974. cfqd->cfq_slice[1] = cfq_slice_sync;
  2975. cfqd->cfq_slice_async_rq = cfq_slice_async_rq;
  2976. cfqd->cfq_slice_idle = cfq_slice_idle;
  2977. cfqd->cfq_latency = 1;
  2978. cfqd->hw_tag = -1;
  2979. cfqd->last_end_sync_rq = jiffies;
  2980. return cfqd;
  2981. }
  2982. static void cfq_slab_kill(void)
  2983. {
  2984. /*
  2985. * Caller already ensured that pending RCU callbacks are completed,
  2986. * so we should have no busy allocations at this point.
  2987. */
  2988. if (cfq_pool)
  2989. kmem_cache_destroy(cfq_pool);
  2990. if (cfq_ioc_pool)
  2991. kmem_cache_destroy(cfq_ioc_pool);
  2992. }
  2993. static int __init cfq_slab_setup(void)
  2994. {
  2995. cfq_pool = KMEM_CACHE(cfq_queue, 0);
  2996. if (!cfq_pool)
  2997. goto fail;
  2998. cfq_ioc_pool = KMEM_CACHE(cfq_io_context, 0);
  2999. if (!cfq_ioc_pool)
  3000. goto fail;
  3001. return 0;
  3002. fail:
  3003. cfq_slab_kill();
  3004. return -ENOMEM;
  3005. }
  3006. /*
  3007. * sysfs parts below -->
  3008. */
  3009. static ssize_t
  3010. cfq_var_show(unsigned int var, char *page)
  3011. {
  3012. return sprintf(page, "%d\n", var);
  3013. }
  3014. static ssize_t
  3015. cfq_var_store(unsigned int *var, const char *page, size_t count)
  3016. {
  3017. char *p = (char *) page;
  3018. *var = simple_strtoul(p, &p, 10);
  3019. return count;
  3020. }
  3021. #define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
  3022. static ssize_t __FUNC(struct elevator_queue *e, char *page) \
  3023. { \
  3024. struct cfq_data *cfqd = e->elevator_data; \
  3025. unsigned int __data = __VAR; \
  3026. if (__CONV) \
  3027. __data = jiffies_to_msecs(__data); \
  3028. return cfq_var_show(__data, (page)); \
  3029. }
  3030. SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0);
  3031. SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1);
  3032. SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1);
  3033. SHOW_FUNCTION(cfq_back_seek_max_show, cfqd->cfq_back_max, 0);
  3034. SHOW_FUNCTION(cfq_back_seek_penalty_show, cfqd->cfq_back_penalty, 0);
  3035. SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1);
  3036. SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1);
  3037. SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1);
  3038. SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0);
  3039. SHOW_FUNCTION(cfq_low_latency_show, cfqd->cfq_latency, 0);
  3040. #undef SHOW_FUNCTION
  3041. #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
  3042. static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count) \
  3043. { \
  3044. struct cfq_data *cfqd = e->elevator_data; \
  3045. unsigned int __data; \
  3046. int ret = cfq_var_store(&__data, (page), count); \
  3047. if (__data < (MIN)) \
  3048. __data = (MIN); \
  3049. else if (__data > (MAX)) \
  3050. __data = (MAX); \
  3051. if (__CONV) \
  3052. *(__PTR) = msecs_to_jiffies(__data); \
  3053. else \
  3054. *(__PTR) = __data; \
  3055. return ret; \
  3056. }
  3057. STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0);
  3058. STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1,
  3059. UINT_MAX, 1);
  3060. STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1,
  3061. UINT_MAX, 1);
  3062. STORE_FUNCTION(cfq_back_seek_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0);
  3063. STORE_FUNCTION(cfq_back_seek_penalty_store, &cfqd->cfq_back_penalty, 1,
  3064. UINT_MAX, 0);
  3065. STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1);
  3066. STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1);
  3067. STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1);
  3068. STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1,
  3069. UINT_MAX, 0);
  3070. STORE_FUNCTION(cfq_low_latency_store, &cfqd->cfq_latency, 0, 1, 0);
  3071. #undef STORE_FUNCTION
  3072. #define CFQ_ATTR(name) \
  3073. __ATTR(name, S_IRUGO|S_IWUSR, cfq_##name##_show, cfq_##name##_store)
  3074. static struct elv_fs_entry cfq_attrs[] = {
  3075. CFQ_ATTR(quantum),
  3076. CFQ_ATTR(fifo_expire_sync),
  3077. CFQ_ATTR(fifo_expire_async),
  3078. CFQ_ATTR(back_seek_max),
  3079. CFQ_ATTR(back_seek_penalty),
  3080. CFQ_ATTR(slice_sync),
  3081. CFQ_ATTR(slice_async),
  3082. CFQ_ATTR(slice_async_rq),
  3083. CFQ_ATTR(slice_idle),
  3084. CFQ_ATTR(low_latency),
  3085. __ATTR_NULL
  3086. };
  3087. static struct elevator_type iosched_cfq = {
  3088. .ops = {
  3089. .elevator_merge_fn = cfq_merge,
  3090. .elevator_merged_fn = cfq_merged_request,
  3091. .elevator_merge_req_fn = cfq_merged_requests,
  3092. .elevator_allow_merge_fn = cfq_allow_merge,
  3093. .elevator_dispatch_fn = cfq_dispatch_requests,
  3094. .elevator_add_req_fn = cfq_insert_request,
  3095. .elevator_activate_req_fn = cfq_activate_request,
  3096. .elevator_deactivate_req_fn = cfq_deactivate_request,
  3097. .elevator_queue_empty_fn = cfq_queue_empty,
  3098. .elevator_completed_req_fn = cfq_completed_request,
  3099. .elevator_former_req_fn = elv_rb_former_request,
  3100. .elevator_latter_req_fn = elv_rb_latter_request,
  3101. .elevator_set_req_fn = cfq_set_request,
  3102. .elevator_put_req_fn = cfq_put_request,
  3103. .elevator_may_queue_fn = cfq_may_queue,
  3104. .elevator_init_fn = cfq_init_queue,
  3105. .elevator_exit_fn = cfq_exit_queue,
  3106. .trim = cfq_free_io_context,
  3107. },
  3108. .elevator_attrs = cfq_attrs,
  3109. .elevator_name = "cfq",
  3110. .elevator_owner = THIS_MODULE,
  3111. };
  3112. static int __init cfq_init(void)
  3113. {
  3114. /*
  3115. * could be 0 on HZ < 1000 setups
  3116. */
  3117. if (!cfq_slice_async)
  3118. cfq_slice_async = 1;
  3119. if (!cfq_slice_idle)
  3120. cfq_slice_idle = 1;
  3121. if (cfq_slab_setup())
  3122. return -ENOMEM;
  3123. elv_register(&iosched_cfq);
  3124. return 0;
  3125. }
  3126. static void __exit cfq_exit(void)
  3127. {
  3128. DECLARE_COMPLETION_ONSTACK(all_gone);
  3129. elv_unregister(&iosched_cfq);
  3130. ioc_gone = &all_gone;
  3131. /* ioc_gone's update must be visible before reading ioc_count */
  3132. smp_wmb();
  3133. /*
  3134. * this also protects us from entering cfq_slab_kill() with
  3135. * pending RCU callbacks
  3136. */
  3137. if (elv_ioc_count_read(cfq_ioc_count))
  3138. wait_for_completion(&all_gone);
  3139. cfq_slab_kill();
  3140. }
  3141. module_init(cfq_init);
  3142. module_exit(cfq_exit);
  3143. MODULE_AUTHOR("Jens Axboe");
  3144. MODULE_LICENSE("GPL");
  3145. MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");