cfq-iosched.c 98 KB

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