cfq-iosched.c 120 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651
  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/slab.h>
  11. #include <linux/blkdev.h>
  12. #include <linux/elevator.h>
  13. #include <linux/jiffies.h>
  14. #include <linux/rbtree.h>
  15. #include <linux/ioprio.h>
  16. #include <linux/blktrace_api.h>
  17. #include "blk.h"
  18. #include "blk-cgroup.h"
  19. /*
  20. * tunables
  21. */
  22. /* max queue in one round of service */
  23. static const int cfq_quantum = 8;
  24. static const int cfq_fifo_expire[2] = { HZ / 4, HZ / 8 };
  25. /* maximum backwards seek, in KiB */
  26. static const int cfq_back_max = 16 * 1024;
  27. /* penalty of a backwards seek */
  28. static const int cfq_back_penalty = 2;
  29. static const int cfq_slice_sync = HZ / 10;
  30. static int cfq_slice_async = HZ / 25;
  31. static const int cfq_slice_async_rq = 2;
  32. static int cfq_slice_idle = HZ / 125;
  33. static int cfq_group_idle = HZ / 125;
  34. static const int cfq_target_latency = HZ * 3/10; /* 300 ms */
  35. static const int cfq_hist_divisor = 4;
  36. /*
  37. * offset from end of service tree
  38. */
  39. #define CFQ_IDLE_DELAY (HZ / 5)
  40. /*
  41. * below this threshold, we consider thinktime immediate
  42. */
  43. #define CFQ_MIN_TT (2)
  44. #define CFQ_SLICE_SCALE (5)
  45. #define CFQ_HW_QUEUE_MIN (5)
  46. #define CFQ_SERVICE_SHIFT 12
  47. #define CFQQ_SEEK_THR (sector_t)(8 * 100)
  48. #define CFQQ_CLOSE_THR (sector_t)(8 * 1024)
  49. #define CFQQ_SECT_THR_NONROT (sector_t)(2 * 32)
  50. #define CFQQ_SEEKY(cfqq) (hweight32(cfqq->seek_history) > 32/8)
  51. #define RQ_CIC(rq) icq_to_cic((rq)->elv.icq)
  52. #define RQ_CFQQ(rq) (struct cfq_queue *) ((rq)->elv.priv[0])
  53. #define RQ_CFQG(rq) (struct cfq_group *) ((rq)->elv.priv[1])
  54. static struct kmem_cache *cfq_pool;
  55. #define CFQ_PRIO_LISTS IOPRIO_BE_NR
  56. #define cfq_class_idle(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_IDLE)
  57. #define cfq_class_rt(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_RT)
  58. #define sample_valid(samples) ((samples) > 80)
  59. #define rb_entry_cfqg(node) rb_entry((node), struct cfq_group, rb_node)
  60. struct cfq_ttime {
  61. unsigned long last_end_request;
  62. unsigned long ttime_total;
  63. unsigned long ttime_samples;
  64. unsigned long ttime_mean;
  65. };
  66. /*
  67. * Most of our rbtree usage is for sorting with min extraction, so
  68. * if we cache the leftmost node we don't have to walk down the tree
  69. * to find it. Idea borrowed from Ingo Molnars CFS scheduler. We should
  70. * move this into the elevator for the rq sorting as well.
  71. */
  72. struct cfq_rb_root {
  73. struct rb_root rb;
  74. struct rb_node *left;
  75. unsigned count;
  76. u64 min_vdisktime;
  77. struct cfq_ttime ttime;
  78. };
  79. #define CFQ_RB_ROOT (struct cfq_rb_root) { .rb = RB_ROOT, \
  80. .ttime = {.last_end_request = jiffies,},}
  81. /*
  82. * Per process-grouping structure
  83. */
  84. struct cfq_queue {
  85. /* reference count */
  86. int ref;
  87. /* various state flags, see below */
  88. unsigned int flags;
  89. /* parent cfq_data */
  90. struct cfq_data *cfqd;
  91. /* service_tree member */
  92. struct rb_node rb_node;
  93. /* service_tree key */
  94. unsigned long rb_key;
  95. /* prio tree member */
  96. struct rb_node p_node;
  97. /* prio tree root we belong to, if any */
  98. struct rb_root *p_root;
  99. /* sorted list of pending requests */
  100. struct rb_root sort_list;
  101. /* if fifo isn't expired, next request to serve */
  102. struct request *next_rq;
  103. /* requests queued in sort_list */
  104. int queued[2];
  105. /* currently allocated requests */
  106. int allocated[2];
  107. /* fifo list of requests in sort_list */
  108. struct list_head fifo;
  109. /* time when queue got scheduled in to dispatch first request. */
  110. unsigned long dispatch_start;
  111. unsigned int allocated_slice;
  112. unsigned int slice_dispatch;
  113. /* time when first request from queue completed and slice started. */
  114. unsigned long slice_start;
  115. unsigned long slice_end;
  116. long slice_resid;
  117. /* pending priority requests */
  118. int prio_pending;
  119. /* number of requests that are on the dispatch list or inside driver */
  120. int dispatched;
  121. /* io prio of this group */
  122. unsigned short ioprio, org_ioprio;
  123. unsigned short ioprio_class;
  124. pid_t pid;
  125. u32 seek_history;
  126. sector_t last_request_pos;
  127. struct cfq_rb_root *service_tree;
  128. struct cfq_queue *new_cfqq;
  129. struct cfq_group *cfqg;
  130. /* Number of sectors dispatched from queue in single dispatch round */
  131. unsigned long nr_sectors;
  132. };
  133. /*
  134. * First index in the service_trees.
  135. * IDLE is handled separately, so it has negative index
  136. */
  137. enum wl_class_t {
  138. BE_WORKLOAD = 0,
  139. RT_WORKLOAD = 1,
  140. IDLE_WORKLOAD = 2,
  141. CFQ_PRIO_NR,
  142. };
  143. /*
  144. * Second index in the service_trees.
  145. */
  146. enum wl_type_t {
  147. ASYNC_WORKLOAD = 0,
  148. SYNC_NOIDLE_WORKLOAD = 1,
  149. SYNC_WORKLOAD = 2
  150. };
  151. struct cfqg_stats {
  152. #ifdef CONFIG_CFQ_GROUP_IOSCHED
  153. /* total bytes transferred */
  154. struct blkg_rwstat service_bytes;
  155. /* total IOs serviced, post merge */
  156. struct blkg_rwstat serviced;
  157. /* number of ios merged */
  158. struct blkg_rwstat merged;
  159. /* total time spent on device in ns, may not be accurate w/ queueing */
  160. struct blkg_rwstat service_time;
  161. /* total time spent waiting in scheduler queue in ns */
  162. struct blkg_rwstat wait_time;
  163. /* number of IOs queued up */
  164. struct blkg_rwstat queued;
  165. /* total sectors transferred */
  166. struct blkg_stat sectors;
  167. /* total disk time and nr sectors dispatched by this group */
  168. struct blkg_stat time;
  169. #ifdef CONFIG_DEBUG_BLK_CGROUP
  170. /* time not charged to this cgroup */
  171. struct blkg_stat unaccounted_time;
  172. /* sum of number of ios queued across all samples */
  173. struct blkg_stat avg_queue_size_sum;
  174. /* count of samples taken for average */
  175. struct blkg_stat avg_queue_size_samples;
  176. /* how many times this group has been removed from service tree */
  177. struct blkg_stat dequeue;
  178. /* total time spent waiting for it to be assigned a timeslice. */
  179. struct blkg_stat group_wait_time;
  180. /* time spent idling for this blkcg_gq */
  181. struct blkg_stat idle_time;
  182. /* total time with empty current active q with other requests queued */
  183. struct blkg_stat empty_time;
  184. /* fields after this shouldn't be cleared on stat reset */
  185. uint64_t start_group_wait_time;
  186. uint64_t start_idle_time;
  187. uint64_t start_empty_time;
  188. uint16_t flags;
  189. #endif /* CONFIG_DEBUG_BLK_CGROUP */
  190. #endif /* CONFIG_CFQ_GROUP_IOSCHED */
  191. };
  192. /* This is per cgroup per device grouping structure */
  193. struct cfq_group {
  194. /* must be the first member */
  195. struct blkg_policy_data pd;
  196. /* group service_tree member */
  197. struct rb_node rb_node;
  198. /* group service_tree key */
  199. u64 vdisktime;
  200. /*
  201. * The number of active cfqgs and sum of their weights under this
  202. * cfqg. This covers this cfqg's leaf_weight and all children's
  203. * weights, but does not cover weights of further descendants.
  204. *
  205. * If a cfqg is on the service tree, it's active. An active cfqg
  206. * also activates its parent and contributes to the children_weight
  207. * of the parent.
  208. */
  209. int nr_active;
  210. unsigned int children_weight;
  211. /*
  212. * vfraction is the fraction of vdisktime that the tasks in this
  213. * cfqg are entitled to. This is determined by compounding the
  214. * ratios walking up from this cfqg to the root.
  215. *
  216. * It is in fixed point w/ CFQ_SERVICE_SHIFT and the sum of all
  217. * vfractions on a service tree is approximately 1. The sum may
  218. * deviate a bit due to rounding errors and fluctuations caused by
  219. * cfqgs entering and leaving the service tree.
  220. */
  221. unsigned int vfraction;
  222. /*
  223. * There are two weights - (internal) weight is the weight of this
  224. * cfqg against the sibling cfqgs. leaf_weight is the wight of
  225. * this cfqg against the child cfqgs. For the root cfqg, both
  226. * weights are kept in sync for backward compatibility.
  227. */
  228. unsigned int weight;
  229. unsigned int new_weight;
  230. unsigned int dev_weight;
  231. unsigned int leaf_weight;
  232. unsigned int new_leaf_weight;
  233. unsigned int dev_leaf_weight;
  234. /* number of cfqq currently on this group */
  235. int nr_cfqq;
  236. /*
  237. * Per group busy queues average. Useful for workload slice calc. We
  238. * create the array for each prio class but at run time it is used
  239. * only for RT and BE class and slot for IDLE class remains unused.
  240. * This is primarily done to avoid confusion and a gcc warning.
  241. */
  242. unsigned int busy_queues_avg[CFQ_PRIO_NR];
  243. /*
  244. * rr lists of queues with requests. We maintain service trees for
  245. * RT and BE classes. These trees are subdivided in subclasses
  246. * of SYNC, SYNC_NOIDLE and ASYNC based on workload type. For IDLE
  247. * class there is no subclassification and all the cfq queues go on
  248. * a single tree service_tree_idle.
  249. * Counts are embedded in the cfq_rb_root
  250. */
  251. struct cfq_rb_root service_trees[2][3];
  252. struct cfq_rb_root service_tree_idle;
  253. unsigned long saved_wl_slice;
  254. enum wl_type_t saved_wl_type;
  255. enum wl_class_t saved_wl_class;
  256. /* number of requests that are on the dispatch list or inside driver */
  257. int dispatched;
  258. struct cfq_ttime ttime;
  259. struct cfqg_stats stats; /* stats for this cfqg */
  260. struct cfqg_stats dead_stats; /* stats pushed from dead children */
  261. };
  262. struct cfq_io_cq {
  263. struct io_cq icq; /* must be the first member */
  264. struct cfq_queue *cfqq[2];
  265. struct cfq_ttime ttime;
  266. int ioprio; /* the current ioprio */
  267. #ifdef CONFIG_CFQ_GROUP_IOSCHED
  268. uint64_t blkcg_id; /* the current blkcg ID */
  269. #endif
  270. };
  271. /*
  272. * Per block device queue structure
  273. */
  274. struct cfq_data {
  275. struct request_queue *queue;
  276. /* Root service tree for cfq_groups */
  277. struct cfq_rb_root grp_service_tree;
  278. struct cfq_group *root_group;
  279. /*
  280. * The priority currently being served
  281. */
  282. enum wl_class_t serving_wl_class;
  283. enum wl_type_t serving_wl_type;
  284. unsigned long workload_expires;
  285. struct cfq_group *serving_group;
  286. /*
  287. * Each priority tree is sorted by next_request position. These
  288. * trees are used when determining if two or more queues are
  289. * interleaving requests (see cfq_close_cooperator).
  290. */
  291. struct rb_root prio_trees[CFQ_PRIO_LISTS];
  292. unsigned int busy_queues;
  293. unsigned int busy_sync_queues;
  294. int rq_in_driver;
  295. int rq_in_flight[2];
  296. /*
  297. * queue-depth detection
  298. */
  299. int rq_queued;
  300. int hw_tag;
  301. /*
  302. * hw_tag can be
  303. * -1 => indeterminate, (cfq will behave as if NCQ is present, to allow better detection)
  304. * 1 => NCQ is present (hw_tag_est_depth is the estimated max depth)
  305. * 0 => no NCQ
  306. */
  307. int hw_tag_est_depth;
  308. unsigned int hw_tag_samples;
  309. /*
  310. * idle window management
  311. */
  312. struct timer_list idle_slice_timer;
  313. struct work_struct unplug_work;
  314. struct cfq_queue *active_queue;
  315. struct cfq_io_cq *active_cic;
  316. /*
  317. * async queue for each priority case
  318. */
  319. struct cfq_queue *async_cfqq[2][IOPRIO_BE_NR];
  320. struct cfq_queue *async_idle_cfqq;
  321. sector_t last_position;
  322. /*
  323. * tunables, see top of file
  324. */
  325. unsigned int cfq_quantum;
  326. unsigned int cfq_fifo_expire[2];
  327. unsigned int cfq_back_penalty;
  328. unsigned int cfq_back_max;
  329. unsigned int cfq_slice[2];
  330. unsigned int cfq_slice_async_rq;
  331. unsigned int cfq_slice_idle;
  332. unsigned int cfq_group_idle;
  333. unsigned int cfq_latency;
  334. unsigned int cfq_target_latency;
  335. /*
  336. * Fallback dummy cfqq for extreme OOM conditions
  337. */
  338. struct cfq_queue oom_cfqq;
  339. unsigned long last_delayed_sync;
  340. };
  341. static struct cfq_group *cfq_get_next_cfqg(struct cfq_data *cfqd);
  342. static struct cfq_rb_root *st_for(struct cfq_group *cfqg,
  343. enum wl_class_t class,
  344. enum wl_type_t type)
  345. {
  346. if (!cfqg)
  347. return NULL;
  348. if (class == IDLE_WORKLOAD)
  349. return &cfqg->service_tree_idle;
  350. return &cfqg->service_trees[class][type];
  351. }
  352. enum cfqq_state_flags {
  353. CFQ_CFQQ_FLAG_on_rr = 0, /* on round-robin busy list */
  354. CFQ_CFQQ_FLAG_wait_request, /* waiting for a request */
  355. CFQ_CFQQ_FLAG_must_dispatch, /* must be allowed a dispatch */
  356. CFQ_CFQQ_FLAG_must_alloc_slice, /* per-slice must_alloc flag */
  357. CFQ_CFQQ_FLAG_fifo_expire, /* FIFO checked in this slice */
  358. CFQ_CFQQ_FLAG_idle_window, /* slice idling enabled */
  359. CFQ_CFQQ_FLAG_prio_changed, /* task priority has changed */
  360. CFQ_CFQQ_FLAG_slice_new, /* no requests dispatched in slice */
  361. CFQ_CFQQ_FLAG_sync, /* synchronous queue */
  362. CFQ_CFQQ_FLAG_coop, /* cfqq is shared */
  363. CFQ_CFQQ_FLAG_split_coop, /* shared cfqq will be splitted */
  364. CFQ_CFQQ_FLAG_deep, /* sync cfqq experienced large depth */
  365. CFQ_CFQQ_FLAG_wait_busy, /* Waiting for next request */
  366. };
  367. #define CFQ_CFQQ_FNS(name) \
  368. static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq) \
  369. { \
  370. (cfqq)->flags |= (1 << CFQ_CFQQ_FLAG_##name); \
  371. } \
  372. static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq) \
  373. { \
  374. (cfqq)->flags &= ~(1 << CFQ_CFQQ_FLAG_##name); \
  375. } \
  376. static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq) \
  377. { \
  378. return ((cfqq)->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0; \
  379. }
  380. CFQ_CFQQ_FNS(on_rr);
  381. CFQ_CFQQ_FNS(wait_request);
  382. CFQ_CFQQ_FNS(must_dispatch);
  383. CFQ_CFQQ_FNS(must_alloc_slice);
  384. CFQ_CFQQ_FNS(fifo_expire);
  385. CFQ_CFQQ_FNS(idle_window);
  386. CFQ_CFQQ_FNS(prio_changed);
  387. CFQ_CFQQ_FNS(slice_new);
  388. CFQ_CFQQ_FNS(sync);
  389. CFQ_CFQQ_FNS(coop);
  390. CFQ_CFQQ_FNS(split_coop);
  391. CFQ_CFQQ_FNS(deep);
  392. CFQ_CFQQ_FNS(wait_busy);
  393. #undef CFQ_CFQQ_FNS
  394. static inline struct cfq_group *pd_to_cfqg(struct blkg_policy_data *pd)
  395. {
  396. return pd ? container_of(pd, struct cfq_group, pd) : NULL;
  397. }
  398. static inline struct blkcg_gq *cfqg_to_blkg(struct cfq_group *cfqg)
  399. {
  400. return pd_to_blkg(&cfqg->pd);
  401. }
  402. #if defined(CONFIG_CFQ_GROUP_IOSCHED) && defined(CONFIG_DEBUG_BLK_CGROUP)
  403. /* cfqg stats flags */
  404. enum cfqg_stats_flags {
  405. CFQG_stats_waiting = 0,
  406. CFQG_stats_idling,
  407. CFQG_stats_empty,
  408. };
  409. #define CFQG_FLAG_FNS(name) \
  410. static inline void cfqg_stats_mark_##name(struct cfqg_stats *stats) \
  411. { \
  412. stats->flags |= (1 << CFQG_stats_##name); \
  413. } \
  414. static inline void cfqg_stats_clear_##name(struct cfqg_stats *stats) \
  415. { \
  416. stats->flags &= ~(1 << CFQG_stats_##name); \
  417. } \
  418. static inline int cfqg_stats_##name(struct cfqg_stats *stats) \
  419. { \
  420. return (stats->flags & (1 << CFQG_stats_##name)) != 0; \
  421. } \
  422. CFQG_FLAG_FNS(waiting)
  423. CFQG_FLAG_FNS(idling)
  424. CFQG_FLAG_FNS(empty)
  425. #undef CFQG_FLAG_FNS
  426. /* This should be called with the queue_lock held. */
  427. static void cfqg_stats_update_group_wait_time(struct cfqg_stats *stats)
  428. {
  429. unsigned long long now;
  430. if (!cfqg_stats_waiting(stats))
  431. return;
  432. now = sched_clock();
  433. if (time_after64(now, stats->start_group_wait_time))
  434. blkg_stat_add(&stats->group_wait_time,
  435. now - stats->start_group_wait_time);
  436. cfqg_stats_clear_waiting(stats);
  437. }
  438. /* This should be called with the queue_lock held. */
  439. static void cfqg_stats_set_start_group_wait_time(struct cfq_group *cfqg,
  440. struct cfq_group *curr_cfqg)
  441. {
  442. struct cfqg_stats *stats = &cfqg->stats;
  443. if (cfqg_stats_waiting(stats))
  444. return;
  445. if (cfqg == curr_cfqg)
  446. return;
  447. stats->start_group_wait_time = sched_clock();
  448. cfqg_stats_mark_waiting(stats);
  449. }
  450. /* This should be called with the queue_lock held. */
  451. static void cfqg_stats_end_empty_time(struct cfqg_stats *stats)
  452. {
  453. unsigned long long now;
  454. if (!cfqg_stats_empty(stats))
  455. return;
  456. now = sched_clock();
  457. if (time_after64(now, stats->start_empty_time))
  458. blkg_stat_add(&stats->empty_time,
  459. now - stats->start_empty_time);
  460. cfqg_stats_clear_empty(stats);
  461. }
  462. static void cfqg_stats_update_dequeue(struct cfq_group *cfqg)
  463. {
  464. blkg_stat_add(&cfqg->stats.dequeue, 1);
  465. }
  466. static void cfqg_stats_set_start_empty_time(struct cfq_group *cfqg)
  467. {
  468. struct cfqg_stats *stats = &cfqg->stats;
  469. if (blkg_rwstat_total(&stats->queued))
  470. return;
  471. /*
  472. * group is already marked empty. This can happen if cfqq got new
  473. * request in parent group and moved to this group while being added
  474. * to service tree. Just ignore the event and move on.
  475. */
  476. if (cfqg_stats_empty(stats))
  477. return;
  478. stats->start_empty_time = sched_clock();
  479. cfqg_stats_mark_empty(stats);
  480. }
  481. static void cfqg_stats_update_idle_time(struct cfq_group *cfqg)
  482. {
  483. struct cfqg_stats *stats = &cfqg->stats;
  484. if (cfqg_stats_idling(stats)) {
  485. unsigned long long now = sched_clock();
  486. if (time_after64(now, stats->start_idle_time))
  487. blkg_stat_add(&stats->idle_time,
  488. now - stats->start_idle_time);
  489. cfqg_stats_clear_idling(stats);
  490. }
  491. }
  492. static void cfqg_stats_set_start_idle_time(struct cfq_group *cfqg)
  493. {
  494. struct cfqg_stats *stats = &cfqg->stats;
  495. BUG_ON(cfqg_stats_idling(stats));
  496. stats->start_idle_time = sched_clock();
  497. cfqg_stats_mark_idling(stats);
  498. }
  499. static void cfqg_stats_update_avg_queue_size(struct cfq_group *cfqg)
  500. {
  501. struct cfqg_stats *stats = &cfqg->stats;
  502. blkg_stat_add(&stats->avg_queue_size_sum,
  503. blkg_rwstat_total(&stats->queued));
  504. blkg_stat_add(&stats->avg_queue_size_samples, 1);
  505. cfqg_stats_update_group_wait_time(stats);
  506. }
  507. #else /* CONFIG_CFQ_GROUP_IOSCHED && CONFIG_DEBUG_BLK_CGROUP */
  508. static inline void cfqg_stats_set_start_group_wait_time(struct cfq_group *cfqg, struct cfq_group *curr_cfqg) { }
  509. static inline void cfqg_stats_end_empty_time(struct cfqg_stats *stats) { }
  510. static inline void cfqg_stats_update_dequeue(struct cfq_group *cfqg) { }
  511. static inline void cfqg_stats_set_start_empty_time(struct cfq_group *cfqg) { }
  512. static inline void cfqg_stats_update_idle_time(struct cfq_group *cfqg) { }
  513. static inline void cfqg_stats_set_start_idle_time(struct cfq_group *cfqg) { }
  514. static inline void cfqg_stats_update_avg_queue_size(struct cfq_group *cfqg) { }
  515. #endif /* CONFIG_CFQ_GROUP_IOSCHED && CONFIG_DEBUG_BLK_CGROUP */
  516. #ifdef CONFIG_CFQ_GROUP_IOSCHED
  517. static struct blkcg_policy blkcg_policy_cfq;
  518. static inline struct cfq_group *blkg_to_cfqg(struct blkcg_gq *blkg)
  519. {
  520. return pd_to_cfqg(blkg_to_pd(blkg, &blkcg_policy_cfq));
  521. }
  522. static inline struct cfq_group *cfqg_parent(struct cfq_group *cfqg)
  523. {
  524. struct blkcg_gq *pblkg = cfqg_to_blkg(cfqg)->parent;
  525. return pblkg ? blkg_to_cfqg(pblkg) : NULL;
  526. }
  527. static inline void cfqg_get(struct cfq_group *cfqg)
  528. {
  529. return blkg_get(cfqg_to_blkg(cfqg));
  530. }
  531. static inline void cfqg_put(struct cfq_group *cfqg)
  532. {
  533. return blkg_put(cfqg_to_blkg(cfqg));
  534. }
  535. #define cfq_log_cfqq(cfqd, cfqq, fmt, args...) do { \
  536. char __pbuf[128]; \
  537. \
  538. blkg_path(cfqg_to_blkg((cfqq)->cfqg), __pbuf, sizeof(__pbuf)); \
  539. blk_add_trace_msg((cfqd)->queue, "cfq%d%c%c %s " fmt, (cfqq)->pid, \
  540. cfq_cfqq_sync((cfqq)) ? 'S' : 'A', \
  541. cfqq_type((cfqq)) == SYNC_NOIDLE_WORKLOAD ? 'N' : ' ',\
  542. __pbuf, ##args); \
  543. } while (0)
  544. #define cfq_log_cfqg(cfqd, cfqg, fmt, args...) do { \
  545. char __pbuf[128]; \
  546. \
  547. blkg_path(cfqg_to_blkg(cfqg), __pbuf, sizeof(__pbuf)); \
  548. blk_add_trace_msg((cfqd)->queue, "%s " fmt, __pbuf, ##args); \
  549. } while (0)
  550. static inline void cfqg_stats_update_io_add(struct cfq_group *cfqg,
  551. struct cfq_group *curr_cfqg, int rw)
  552. {
  553. blkg_rwstat_add(&cfqg->stats.queued, rw, 1);
  554. cfqg_stats_end_empty_time(&cfqg->stats);
  555. cfqg_stats_set_start_group_wait_time(cfqg, curr_cfqg);
  556. }
  557. static inline void cfqg_stats_update_timeslice_used(struct cfq_group *cfqg,
  558. unsigned long time, unsigned long unaccounted_time)
  559. {
  560. blkg_stat_add(&cfqg->stats.time, time);
  561. #ifdef CONFIG_DEBUG_BLK_CGROUP
  562. blkg_stat_add(&cfqg->stats.unaccounted_time, unaccounted_time);
  563. #endif
  564. }
  565. static inline void cfqg_stats_update_io_remove(struct cfq_group *cfqg, int rw)
  566. {
  567. blkg_rwstat_add(&cfqg->stats.queued, rw, -1);
  568. }
  569. static inline void cfqg_stats_update_io_merged(struct cfq_group *cfqg, int rw)
  570. {
  571. blkg_rwstat_add(&cfqg->stats.merged, rw, 1);
  572. }
  573. static inline void cfqg_stats_update_dispatch(struct cfq_group *cfqg,
  574. uint64_t bytes, int rw)
  575. {
  576. blkg_stat_add(&cfqg->stats.sectors, bytes >> 9);
  577. blkg_rwstat_add(&cfqg->stats.serviced, rw, 1);
  578. blkg_rwstat_add(&cfqg->stats.service_bytes, rw, bytes);
  579. }
  580. static inline void cfqg_stats_update_completion(struct cfq_group *cfqg,
  581. uint64_t start_time, uint64_t io_start_time, int rw)
  582. {
  583. struct cfqg_stats *stats = &cfqg->stats;
  584. unsigned long long now = sched_clock();
  585. if (time_after64(now, io_start_time))
  586. blkg_rwstat_add(&stats->service_time, rw, now - io_start_time);
  587. if (time_after64(io_start_time, start_time))
  588. blkg_rwstat_add(&stats->wait_time, rw,
  589. io_start_time - start_time);
  590. }
  591. /* @stats = 0 */
  592. static void cfqg_stats_reset(struct cfqg_stats *stats)
  593. {
  594. /* queued stats shouldn't be cleared */
  595. blkg_rwstat_reset(&stats->service_bytes);
  596. blkg_rwstat_reset(&stats->serviced);
  597. blkg_rwstat_reset(&stats->merged);
  598. blkg_rwstat_reset(&stats->service_time);
  599. blkg_rwstat_reset(&stats->wait_time);
  600. blkg_stat_reset(&stats->time);
  601. #ifdef CONFIG_DEBUG_BLK_CGROUP
  602. blkg_stat_reset(&stats->unaccounted_time);
  603. blkg_stat_reset(&stats->avg_queue_size_sum);
  604. blkg_stat_reset(&stats->avg_queue_size_samples);
  605. blkg_stat_reset(&stats->dequeue);
  606. blkg_stat_reset(&stats->group_wait_time);
  607. blkg_stat_reset(&stats->idle_time);
  608. blkg_stat_reset(&stats->empty_time);
  609. #endif
  610. }
  611. /* @to += @from */
  612. static void cfqg_stats_merge(struct cfqg_stats *to, struct cfqg_stats *from)
  613. {
  614. /* queued stats shouldn't be cleared */
  615. blkg_rwstat_merge(&to->service_bytes, &from->service_bytes);
  616. blkg_rwstat_merge(&to->serviced, &from->serviced);
  617. blkg_rwstat_merge(&to->merged, &from->merged);
  618. blkg_rwstat_merge(&to->service_time, &from->service_time);
  619. blkg_rwstat_merge(&to->wait_time, &from->wait_time);
  620. blkg_stat_merge(&from->time, &from->time);
  621. #ifdef CONFIG_DEBUG_BLK_CGROUP
  622. blkg_stat_merge(&to->unaccounted_time, &from->unaccounted_time);
  623. blkg_stat_merge(&to->avg_queue_size_sum, &from->avg_queue_size_sum);
  624. blkg_stat_merge(&to->avg_queue_size_samples, &from->avg_queue_size_samples);
  625. blkg_stat_merge(&to->dequeue, &from->dequeue);
  626. blkg_stat_merge(&to->group_wait_time, &from->group_wait_time);
  627. blkg_stat_merge(&to->idle_time, &from->idle_time);
  628. blkg_stat_merge(&to->empty_time, &from->empty_time);
  629. #endif
  630. }
  631. /*
  632. * Transfer @cfqg's stats to its parent's dead_stats so that the ancestors'
  633. * recursive stats can still account for the amount used by this cfqg after
  634. * it's gone.
  635. */
  636. static void cfqg_stats_xfer_dead(struct cfq_group *cfqg)
  637. {
  638. struct cfq_group *parent = cfqg_parent(cfqg);
  639. lockdep_assert_held(cfqg_to_blkg(cfqg)->q->queue_lock);
  640. if (unlikely(!parent))
  641. return;
  642. cfqg_stats_merge(&parent->dead_stats, &cfqg->stats);
  643. cfqg_stats_merge(&parent->dead_stats, &cfqg->dead_stats);
  644. cfqg_stats_reset(&cfqg->stats);
  645. cfqg_stats_reset(&cfqg->dead_stats);
  646. }
  647. #else /* CONFIG_CFQ_GROUP_IOSCHED */
  648. static inline struct cfq_group *cfqg_parent(struct cfq_group *cfqg) { return NULL; }
  649. static inline void cfqg_get(struct cfq_group *cfqg) { }
  650. static inline void cfqg_put(struct cfq_group *cfqg) { }
  651. #define cfq_log_cfqq(cfqd, cfqq, fmt, args...) \
  652. blk_add_trace_msg((cfqd)->queue, "cfq%d%c%c " fmt, (cfqq)->pid, \
  653. cfq_cfqq_sync((cfqq)) ? 'S' : 'A', \
  654. cfqq_type((cfqq)) == SYNC_NOIDLE_WORKLOAD ? 'N' : ' ',\
  655. ##args)
  656. #define cfq_log_cfqg(cfqd, cfqg, fmt, args...) do {} while (0)
  657. static inline void cfqg_stats_update_io_add(struct cfq_group *cfqg,
  658. struct cfq_group *curr_cfqg, int rw) { }
  659. static inline void cfqg_stats_update_timeslice_used(struct cfq_group *cfqg,
  660. unsigned long time, unsigned long unaccounted_time) { }
  661. static inline void cfqg_stats_update_io_remove(struct cfq_group *cfqg, int rw) { }
  662. static inline void cfqg_stats_update_io_merged(struct cfq_group *cfqg, int rw) { }
  663. static inline void cfqg_stats_update_dispatch(struct cfq_group *cfqg,
  664. uint64_t bytes, int rw) { }
  665. static inline void cfqg_stats_update_completion(struct cfq_group *cfqg,
  666. uint64_t start_time, uint64_t io_start_time, int rw) { }
  667. #endif /* CONFIG_CFQ_GROUP_IOSCHED */
  668. #define cfq_log(cfqd, fmt, args...) \
  669. blk_add_trace_msg((cfqd)->queue, "cfq " fmt, ##args)
  670. /* Traverses through cfq group service trees */
  671. #define for_each_cfqg_st(cfqg, i, j, st) \
  672. for (i = 0; i <= IDLE_WORKLOAD; i++) \
  673. for (j = 0, st = i < IDLE_WORKLOAD ? &cfqg->service_trees[i][j]\
  674. : &cfqg->service_tree_idle; \
  675. (i < IDLE_WORKLOAD && j <= SYNC_WORKLOAD) || \
  676. (i == IDLE_WORKLOAD && j == 0); \
  677. j++, st = i < IDLE_WORKLOAD ? \
  678. &cfqg->service_trees[i][j]: NULL) \
  679. static inline bool cfq_io_thinktime_big(struct cfq_data *cfqd,
  680. struct cfq_ttime *ttime, bool group_idle)
  681. {
  682. unsigned long slice;
  683. if (!sample_valid(ttime->ttime_samples))
  684. return false;
  685. if (group_idle)
  686. slice = cfqd->cfq_group_idle;
  687. else
  688. slice = cfqd->cfq_slice_idle;
  689. return ttime->ttime_mean > slice;
  690. }
  691. static inline bool iops_mode(struct cfq_data *cfqd)
  692. {
  693. /*
  694. * If we are not idling on queues and it is a NCQ drive, parallel
  695. * execution of requests is on and measuring time is not possible
  696. * in most of the cases until and unless we drive shallower queue
  697. * depths and that becomes a performance bottleneck. In such cases
  698. * switch to start providing fairness in terms of number of IOs.
  699. */
  700. if (!cfqd->cfq_slice_idle && cfqd->hw_tag)
  701. return true;
  702. else
  703. return false;
  704. }
  705. static inline enum wl_class_t cfqq_class(struct cfq_queue *cfqq)
  706. {
  707. if (cfq_class_idle(cfqq))
  708. return IDLE_WORKLOAD;
  709. if (cfq_class_rt(cfqq))
  710. return RT_WORKLOAD;
  711. return BE_WORKLOAD;
  712. }
  713. static enum wl_type_t cfqq_type(struct cfq_queue *cfqq)
  714. {
  715. if (!cfq_cfqq_sync(cfqq))
  716. return ASYNC_WORKLOAD;
  717. if (!cfq_cfqq_idle_window(cfqq))
  718. return SYNC_NOIDLE_WORKLOAD;
  719. return SYNC_WORKLOAD;
  720. }
  721. static inline int cfq_group_busy_queues_wl(enum wl_class_t wl_class,
  722. struct cfq_data *cfqd,
  723. struct cfq_group *cfqg)
  724. {
  725. if (wl_class == IDLE_WORKLOAD)
  726. return cfqg->service_tree_idle.count;
  727. return cfqg->service_trees[wl_class][ASYNC_WORKLOAD].count +
  728. cfqg->service_trees[wl_class][SYNC_NOIDLE_WORKLOAD].count +
  729. cfqg->service_trees[wl_class][SYNC_WORKLOAD].count;
  730. }
  731. static inline int cfqg_busy_async_queues(struct cfq_data *cfqd,
  732. struct cfq_group *cfqg)
  733. {
  734. return cfqg->service_trees[RT_WORKLOAD][ASYNC_WORKLOAD].count +
  735. cfqg->service_trees[BE_WORKLOAD][ASYNC_WORKLOAD].count;
  736. }
  737. static void cfq_dispatch_insert(struct request_queue *, struct request *);
  738. static struct cfq_queue *cfq_get_queue(struct cfq_data *cfqd, bool is_sync,
  739. struct cfq_io_cq *cic, struct bio *bio,
  740. gfp_t gfp_mask);
  741. static inline struct cfq_io_cq *icq_to_cic(struct io_cq *icq)
  742. {
  743. /* cic->icq is the first member, %NULL will convert to %NULL */
  744. return container_of(icq, struct cfq_io_cq, icq);
  745. }
  746. static inline struct cfq_io_cq *cfq_cic_lookup(struct cfq_data *cfqd,
  747. struct io_context *ioc)
  748. {
  749. if (ioc)
  750. return icq_to_cic(ioc_lookup_icq(ioc, cfqd->queue));
  751. return NULL;
  752. }
  753. static inline struct cfq_queue *cic_to_cfqq(struct cfq_io_cq *cic, bool is_sync)
  754. {
  755. return cic->cfqq[is_sync];
  756. }
  757. static inline void cic_set_cfqq(struct cfq_io_cq *cic, struct cfq_queue *cfqq,
  758. bool is_sync)
  759. {
  760. cic->cfqq[is_sync] = cfqq;
  761. }
  762. static inline struct cfq_data *cic_to_cfqd(struct cfq_io_cq *cic)
  763. {
  764. return cic->icq.q->elevator->elevator_data;
  765. }
  766. /*
  767. * We regard a request as SYNC, if it's either a read or has the SYNC bit
  768. * set (in which case it could also be direct WRITE).
  769. */
  770. static inline bool cfq_bio_sync(struct bio *bio)
  771. {
  772. return bio_data_dir(bio) == READ || (bio->bi_rw & REQ_SYNC);
  773. }
  774. /*
  775. * scheduler run of queue, if there are requests pending and no one in the
  776. * driver that will restart queueing
  777. */
  778. static inline void cfq_schedule_dispatch(struct cfq_data *cfqd)
  779. {
  780. if (cfqd->busy_queues) {
  781. cfq_log(cfqd, "schedule dispatch");
  782. kblockd_schedule_work(cfqd->queue, &cfqd->unplug_work);
  783. }
  784. }
  785. /*
  786. * Scale schedule slice based on io priority. Use the sync time slice only
  787. * if a queue is marked sync and has sync io queued. A sync queue with async
  788. * io only, should not get full sync slice length.
  789. */
  790. static inline int cfq_prio_slice(struct cfq_data *cfqd, bool sync,
  791. unsigned short prio)
  792. {
  793. const int base_slice = cfqd->cfq_slice[sync];
  794. WARN_ON(prio >= IOPRIO_BE_NR);
  795. return base_slice + (base_slice/CFQ_SLICE_SCALE * (4 - prio));
  796. }
  797. static inline int
  798. cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  799. {
  800. return cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio);
  801. }
  802. /**
  803. * cfqg_scale_charge - scale disk time charge according to cfqg weight
  804. * @charge: disk time being charged
  805. * @vfraction: vfraction of the cfqg, fixed point w/ CFQ_SERVICE_SHIFT
  806. *
  807. * Scale @charge according to @vfraction, which is in range (0, 1]. The
  808. * scaling is inversely proportional.
  809. *
  810. * scaled = charge / vfraction
  811. *
  812. * The result is also in fixed point w/ CFQ_SERVICE_SHIFT.
  813. */
  814. static inline u64 cfqg_scale_charge(unsigned long charge,
  815. unsigned int vfraction)
  816. {
  817. u64 c = charge << CFQ_SERVICE_SHIFT; /* make it fixed point */
  818. /* charge / vfraction */
  819. c <<= CFQ_SERVICE_SHIFT;
  820. do_div(c, vfraction);
  821. return c;
  822. }
  823. static inline u64 max_vdisktime(u64 min_vdisktime, u64 vdisktime)
  824. {
  825. s64 delta = (s64)(vdisktime - min_vdisktime);
  826. if (delta > 0)
  827. min_vdisktime = vdisktime;
  828. return min_vdisktime;
  829. }
  830. static inline u64 min_vdisktime(u64 min_vdisktime, u64 vdisktime)
  831. {
  832. s64 delta = (s64)(vdisktime - min_vdisktime);
  833. if (delta < 0)
  834. min_vdisktime = vdisktime;
  835. return min_vdisktime;
  836. }
  837. static void update_min_vdisktime(struct cfq_rb_root *st)
  838. {
  839. struct cfq_group *cfqg;
  840. if (st->left) {
  841. cfqg = rb_entry_cfqg(st->left);
  842. st->min_vdisktime = max_vdisktime(st->min_vdisktime,
  843. cfqg->vdisktime);
  844. }
  845. }
  846. /*
  847. * get averaged number of queues of RT/BE priority.
  848. * average is updated, with a formula that gives more weight to higher numbers,
  849. * to quickly follows sudden increases and decrease slowly
  850. */
  851. static inline unsigned cfq_group_get_avg_queues(struct cfq_data *cfqd,
  852. struct cfq_group *cfqg, bool rt)
  853. {
  854. unsigned min_q, max_q;
  855. unsigned mult = cfq_hist_divisor - 1;
  856. unsigned round = cfq_hist_divisor / 2;
  857. unsigned busy = cfq_group_busy_queues_wl(rt, cfqd, cfqg);
  858. min_q = min(cfqg->busy_queues_avg[rt], busy);
  859. max_q = max(cfqg->busy_queues_avg[rt], busy);
  860. cfqg->busy_queues_avg[rt] = (mult * max_q + min_q + round) /
  861. cfq_hist_divisor;
  862. return cfqg->busy_queues_avg[rt];
  863. }
  864. static inline unsigned
  865. cfq_group_slice(struct cfq_data *cfqd, struct cfq_group *cfqg)
  866. {
  867. return cfqd->cfq_target_latency * cfqg->vfraction >> CFQ_SERVICE_SHIFT;
  868. }
  869. static inline unsigned
  870. cfq_scaled_cfqq_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  871. {
  872. unsigned slice = cfq_prio_to_slice(cfqd, cfqq);
  873. if (cfqd->cfq_latency) {
  874. /*
  875. * interested queues (we consider only the ones with the same
  876. * priority class in the cfq group)
  877. */
  878. unsigned iq = cfq_group_get_avg_queues(cfqd, cfqq->cfqg,
  879. cfq_class_rt(cfqq));
  880. unsigned sync_slice = cfqd->cfq_slice[1];
  881. unsigned expect_latency = sync_slice * iq;
  882. unsigned group_slice = cfq_group_slice(cfqd, cfqq->cfqg);
  883. if (expect_latency > group_slice) {
  884. unsigned base_low_slice = 2 * cfqd->cfq_slice_idle;
  885. /* scale low_slice according to IO priority
  886. * and sync vs async */
  887. unsigned low_slice =
  888. min(slice, base_low_slice * slice / sync_slice);
  889. /* the adapted slice value is scaled to fit all iqs
  890. * into the target latency */
  891. slice = max(slice * group_slice / expect_latency,
  892. low_slice);
  893. }
  894. }
  895. return slice;
  896. }
  897. static inline void
  898. cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  899. {
  900. unsigned slice = cfq_scaled_cfqq_slice(cfqd, cfqq);
  901. cfqq->slice_start = jiffies;
  902. cfqq->slice_end = jiffies + slice;
  903. cfqq->allocated_slice = slice;
  904. cfq_log_cfqq(cfqd, cfqq, "set_slice=%lu", cfqq->slice_end - jiffies);
  905. }
  906. /*
  907. * We need to wrap this check in cfq_cfqq_slice_new(), since ->slice_end
  908. * isn't valid until the first request from the dispatch is activated
  909. * and the slice time set.
  910. */
  911. static inline bool cfq_slice_used(struct cfq_queue *cfqq)
  912. {
  913. if (cfq_cfqq_slice_new(cfqq))
  914. return false;
  915. if (time_before(jiffies, cfqq->slice_end))
  916. return false;
  917. return true;
  918. }
  919. /*
  920. * Lifted from AS - choose which of rq1 and rq2 that is best served now.
  921. * We choose the request that is closest to the head right now. Distance
  922. * behind the head is penalized and only allowed to a certain extent.
  923. */
  924. static struct request *
  925. cfq_choose_req(struct cfq_data *cfqd, struct request *rq1, struct request *rq2, sector_t last)
  926. {
  927. sector_t s1, s2, d1 = 0, d2 = 0;
  928. unsigned long back_max;
  929. #define CFQ_RQ1_WRAP 0x01 /* request 1 wraps */
  930. #define CFQ_RQ2_WRAP 0x02 /* request 2 wraps */
  931. unsigned wrap = 0; /* bit mask: requests behind the disk head? */
  932. if (rq1 == NULL || rq1 == rq2)
  933. return rq2;
  934. if (rq2 == NULL)
  935. return rq1;
  936. if (rq_is_sync(rq1) != rq_is_sync(rq2))
  937. return rq_is_sync(rq1) ? rq1 : rq2;
  938. if ((rq1->cmd_flags ^ rq2->cmd_flags) & REQ_PRIO)
  939. return rq1->cmd_flags & REQ_PRIO ? rq1 : rq2;
  940. s1 = blk_rq_pos(rq1);
  941. s2 = blk_rq_pos(rq2);
  942. /*
  943. * by definition, 1KiB is 2 sectors
  944. */
  945. back_max = cfqd->cfq_back_max * 2;
  946. /*
  947. * Strict one way elevator _except_ in the case where we allow
  948. * short backward seeks which are biased as twice the cost of a
  949. * similar forward seek.
  950. */
  951. if (s1 >= last)
  952. d1 = s1 - last;
  953. else if (s1 + back_max >= last)
  954. d1 = (last - s1) * cfqd->cfq_back_penalty;
  955. else
  956. wrap |= CFQ_RQ1_WRAP;
  957. if (s2 >= last)
  958. d2 = s2 - last;
  959. else if (s2 + back_max >= last)
  960. d2 = (last - s2) * cfqd->cfq_back_penalty;
  961. else
  962. wrap |= CFQ_RQ2_WRAP;
  963. /* Found required data */
  964. /*
  965. * By doing switch() on the bit mask "wrap" we avoid having to
  966. * check two variables for all permutations: --> faster!
  967. */
  968. switch (wrap) {
  969. case 0: /* common case for CFQ: rq1 and rq2 not wrapped */
  970. if (d1 < d2)
  971. return rq1;
  972. else if (d2 < d1)
  973. return rq2;
  974. else {
  975. if (s1 >= s2)
  976. return rq1;
  977. else
  978. return rq2;
  979. }
  980. case CFQ_RQ2_WRAP:
  981. return rq1;
  982. case CFQ_RQ1_WRAP:
  983. return rq2;
  984. case (CFQ_RQ1_WRAP|CFQ_RQ2_WRAP): /* both rqs wrapped */
  985. default:
  986. /*
  987. * Since both rqs are wrapped,
  988. * start with the one that's further behind head
  989. * (--> only *one* back seek required),
  990. * since back seek takes more time than forward.
  991. */
  992. if (s1 <= s2)
  993. return rq1;
  994. else
  995. return rq2;
  996. }
  997. }
  998. /*
  999. * The below is leftmost cache rbtree addon
  1000. */
  1001. static struct cfq_queue *cfq_rb_first(struct cfq_rb_root *root)
  1002. {
  1003. /* Service tree is empty */
  1004. if (!root->count)
  1005. return NULL;
  1006. if (!root->left)
  1007. root->left = rb_first(&root->rb);
  1008. if (root->left)
  1009. return rb_entry(root->left, struct cfq_queue, rb_node);
  1010. return NULL;
  1011. }
  1012. static struct cfq_group *cfq_rb_first_group(struct cfq_rb_root *root)
  1013. {
  1014. if (!root->left)
  1015. root->left = rb_first(&root->rb);
  1016. if (root->left)
  1017. return rb_entry_cfqg(root->left);
  1018. return NULL;
  1019. }
  1020. static void rb_erase_init(struct rb_node *n, struct rb_root *root)
  1021. {
  1022. rb_erase(n, root);
  1023. RB_CLEAR_NODE(n);
  1024. }
  1025. static void cfq_rb_erase(struct rb_node *n, struct cfq_rb_root *root)
  1026. {
  1027. if (root->left == n)
  1028. root->left = NULL;
  1029. rb_erase_init(n, &root->rb);
  1030. --root->count;
  1031. }
  1032. /*
  1033. * would be nice to take fifo expire time into account as well
  1034. */
  1035. static struct request *
  1036. cfq_find_next_rq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
  1037. struct request *last)
  1038. {
  1039. struct rb_node *rbnext = rb_next(&last->rb_node);
  1040. struct rb_node *rbprev = rb_prev(&last->rb_node);
  1041. struct request *next = NULL, *prev = NULL;
  1042. BUG_ON(RB_EMPTY_NODE(&last->rb_node));
  1043. if (rbprev)
  1044. prev = rb_entry_rq(rbprev);
  1045. if (rbnext)
  1046. next = rb_entry_rq(rbnext);
  1047. else {
  1048. rbnext = rb_first(&cfqq->sort_list);
  1049. if (rbnext && rbnext != &last->rb_node)
  1050. next = rb_entry_rq(rbnext);
  1051. }
  1052. return cfq_choose_req(cfqd, next, prev, blk_rq_pos(last));
  1053. }
  1054. static unsigned long cfq_slice_offset(struct cfq_data *cfqd,
  1055. struct cfq_queue *cfqq)
  1056. {
  1057. /*
  1058. * just an approximation, should be ok.
  1059. */
  1060. return (cfqq->cfqg->nr_cfqq - 1) * (cfq_prio_slice(cfqd, 1, 0) -
  1061. cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio));
  1062. }
  1063. static inline s64
  1064. cfqg_key(struct cfq_rb_root *st, struct cfq_group *cfqg)
  1065. {
  1066. return cfqg->vdisktime - st->min_vdisktime;
  1067. }
  1068. static void
  1069. __cfq_group_service_tree_add(struct cfq_rb_root *st, struct cfq_group *cfqg)
  1070. {
  1071. struct rb_node **node = &st->rb.rb_node;
  1072. struct rb_node *parent = NULL;
  1073. struct cfq_group *__cfqg;
  1074. s64 key = cfqg_key(st, cfqg);
  1075. int left = 1;
  1076. while (*node != NULL) {
  1077. parent = *node;
  1078. __cfqg = rb_entry_cfqg(parent);
  1079. if (key < cfqg_key(st, __cfqg))
  1080. node = &parent->rb_left;
  1081. else {
  1082. node = &parent->rb_right;
  1083. left = 0;
  1084. }
  1085. }
  1086. if (left)
  1087. st->left = &cfqg->rb_node;
  1088. rb_link_node(&cfqg->rb_node, parent, node);
  1089. rb_insert_color(&cfqg->rb_node, &st->rb);
  1090. }
  1091. static void
  1092. cfq_update_group_weight(struct cfq_group *cfqg)
  1093. {
  1094. BUG_ON(!RB_EMPTY_NODE(&cfqg->rb_node));
  1095. if (cfqg->new_weight) {
  1096. cfqg->weight = cfqg->new_weight;
  1097. cfqg->new_weight = 0;
  1098. }
  1099. if (cfqg->new_leaf_weight) {
  1100. cfqg->leaf_weight = cfqg->new_leaf_weight;
  1101. cfqg->new_leaf_weight = 0;
  1102. }
  1103. }
  1104. static void
  1105. cfq_group_service_tree_add(struct cfq_rb_root *st, struct cfq_group *cfqg)
  1106. {
  1107. unsigned int vfr = 1 << CFQ_SERVICE_SHIFT; /* start with 1 */
  1108. struct cfq_group *pos = cfqg;
  1109. struct cfq_group *parent;
  1110. bool propagate;
  1111. /* add to the service tree */
  1112. BUG_ON(!RB_EMPTY_NODE(&cfqg->rb_node));
  1113. cfq_update_group_weight(cfqg);
  1114. __cfq_group_service_tree_add(st, cfqg);
  1115. /*
  1116. * Activate @cfqg and calculate the portion of vfraction @cfqg is
  1117. * entitled to. vfraction is calculated by walking the tree
  1118. * towards the root calculating the fraction it has at each level.
  1119. * The compounded ratio is how much vfraction @cfqg owns.
  1120. *
  1121. * Start with the proportion tasks in this cfqg has against active
  1122. * children cfqgs - its leaf_weight against children_weight.
  1123. */
  1124. propagate = !pos->nr_active++;
  1125. pos->children_weight += pos->leaf_weight;
  1126. vfr = vfr * pos->leaf_weight / pos->children_weight;
  1127. /*
  1128. * Compound ->weight walking up the tree. Both activation and
  1129. * vfraction calculation are done in the same loop. Propagation
  1130. * stops once an already activated node is met. vfraction
  1131. * calculation should always continue to the root.
  1132. */
  1133. while ((parent = cfqg_parent(pos))) {
  1134. if (propagate) {
  1135. propagate = !parent->nr_active++;
  1136. parent->children_weight += pos->weight;
  1137. }
  1138. vfr = vfr * pos->weight / parent->children_weight;
  1139. pos = parent;
  1140. }
  1141. cfqg->vfraction = max_t(unsigned, vfr, 1);
  1142. }
  1143. static void
  1144. cfq_group_notify_queue_add(struct cfq_data *cfqd, struct cfq_group *cfqg)
  1145. {
  1146. struct cfq_rb_root *st = &cfqd->grp_service_tree;
  1147. struct cfq_group *__cfqg;
  1148. struct rb_node *n;
  1149. cfqg->nr_cfqq++;
  1150. if (!RB_EMPTY_NODE(&cfqg->rb_node))
  1151. return;
  1152. /*
  1153. * Currently put the group at the end. Later implement something
  1154. * so that groups get lesser vtime based on their weights, so that
  1155. * if group does not loose all if it was not continuously backlogged.
  1156. */
  1157. n = rb_last(&st->rb);
  1158. if (n) {
  1159. __cfqg = rb_entry_cfqg(n);
  1160. cfqg->vdisktime = __cfqg->vdisktime + CFQ_IDLE_DELAY;
  1161. } else
  1162. cfqg->vdisktime = st->min_vdisktime;
  1163. cfq_group_service_tree_add(st, cfqg);
  1164. }
  1165. static void
  1166. cfq_group_service_tree_del(struct cfq_rb_root *st, struct cfq_group *cfqg)
  1167. {
  1168. struct cfq_group *pos = cfqg;
  1169. bool propagate;
  1170. /*
  1171. * Undo activation from cfq_group_service_tree_add(). Deactivate
  1172. * @cfqg and propagate deactivation upwards.
  1173. */
  1174. propagate = !--pos->nr_active;
  1175. pos->children_weight -= pos->leaf_weight;
  1176. while (propagate) {
  1177. struct cfq_group *parent = cfqg_parent(pos);
  1178. /* @pos has 0 nr_active at this point */
  1179. WARN_ON_ONCE(pos->children_weight);
  1180. pos->vfraction = 0;
  1181. if (!parent)
  1182. break;
  1183. propagate = !--parent->nr_active;
  1184. parent->children_weight -= pos->weight;
  1185. pos = parent;
  1186. }
  1187. /* remove from the service tree */
  1188. if (!RB_EMPTY_NODE(&cfqg->rb_node))
  1189. cfq_rb_erase(&cfqg->rb_node, st);
  1190. }
  1191. static void
  1192. cfq_group_notify_queue_del(struct cfq_data *cfqd, struct cfq_group *cfqg)
  1193. {
  1194. struct cfq_rb_root *st = &cfqd->grp_service_tree;
  1195. BUG_ON(cfqg->nr_cfqq < 1);
  1196. cfqg->nr_cfqq--;
  1197. /* If there are other cfq queues under this group, don't delete it */
  1198. if (cfqg->nr_cfqq)
  1199. return;
  1200. cfq_log_cfqg(cfqd, cfqg, "del_from_rr group");
  1201. cfq_group_service_tree_del(st, cfqg);
  1202. cfqg->saved_wl_slice = 0;
  1203. cfqg_stats_update_dequeue(cfqg);
  1204. }
  1205. static inline unsigned int cfq_cfqq_slice_usage(struct cfq_queue *cfqq,
  1206. unsigned int *unaccounted_time)
  1207. {
  1208. unsigned int slice_used;
  1209. /*
  1210. * Queue got expired before even a single request completed or
  1211. * got expired immediately after first request completion.
  1212. */
  1213. if (!cfqq->slice_start || cfqq->slice_start == jiffies) {
  1214. /*
  1215. * Also charge the seek time incurred to the group, otherwise
  1216. * if there are mutiple queues in the group, each can dispatch
  1217. * a single request on seeky media and cause lots of seek time
  1218. * and group will never know it.
  1219. */
  1220. slice_used = max_t(unsigned, (jiffies - cfqq->dispatch_start),
  1221. 1);
  1222. } else {
  1223. slice_used = jiffies - cfqq->slice_start;
  1224. if (slice_used > cfqq->allocated_slice) {
  1225. *unaccounted_time = slice_used - cfqq->allocated_slice;
  1226. slice_used = cfqq->allocated_slice;
  1227. }
  1228. if (time_after(cfqq->slice_start, cfqq->dispatch_start))
  1229. *unaccounted_time += cfqq->slice_start -
  1230. cfqq->dispatch_start;
  1231. }
  1232. return slice_used;
  1233. }
  1234. static void cfq_group_served(struct cfq_data *cfqd, struct cfq_group *cfqg,
  1235. struct cfq_queue *cfqq)
  1236. {
  1237. struct cfq_rb_root *st = &cfqd->grp_service_tree;
  1238. unsigned int used_sl, charge, unaccounted_sl = 0;
  1239. int nr_sync = cfqg->nr_cfqq - cfqg_busy_async_queues(cfqd, cfqg)
  1240. - cfqg->service_tree_idle.count;
  1241. unsigned int vfr;
  1242. BUG_ON(nr_sync < 0);
  1243. used_sl = charge = cfq_cfqq_slice_usage(cfqq, &unaccounted_sl);
  1244. if (iops_mode(cfqd))
  1245. charge = cfqq->slice_dispatch;
  1246. else if (!cfq_cfqq_sync(cfqq) && !nr_sync)
  1247. charge = cfqq->allocated_slice;
  1248. /*
  1249. * Can't update vdisktime while on service tree and cfqg->vfraction
  1250. * is valid only while on it. Cache vfr, leave the service tree,
  1251. * update vdisktime and go back on. The re-addition to the tree
  1252. * will also update the weights as necessary.
  1253. */
  1254. vfr = cfqg->vfraction;
  1255. cfq_group_service_tree_del(st, cfqg);
  1256. cfqg->vdisktime += cfqg_scale_charge(charge, vfr);
  1257. cfq_group_service_tree_add(st, cfqg);
  1258. /* This group is being expired. Save the context */
  1259. if (time_after(cfqd->workload_expires, jiffies)) {
  1260. cfqg->saved_wl_slice = cfqd->workload_expires
  1261. - jiffies;
  1262. cfqg->saved_wl_type = cfqd->serving_wl_type;
  1263. cfqg->saved_wl_class = cfqd->serving_wl_class;
  1264. } else
  1265. cfqg->saved_wl_slice = 0;
  1266. cfq_log_cfqg(cfqd, cfqg, "served: vt=%llu min_vt=%llu", cfqg->vdisktime,
  1267. st->min_vdisktime);
  1268. cfq_log_cfqq(cfqq->cfqd, cfqq,
  1269. "sl_used=%u disp=%u charge=%u iops=%u sect=%lu",
  1270. used_sl, cfqq->slice_dispatch, charge,
  1271. iops_mode(cfqd), cfqq->nr_sectors);
  1272. cfqg_stats_update_timeslice_used(cfqg, used_sl, unaccounted_sl);
  1273. cfqg_stats_set_start_empty_time(cfqg);
  1274. }
  1275. /**
  1276. * cfq_init_cfqg_base - initialize base part of a cfq_group
  1277. * @cfqg: cfq_group to initialize
  1278. *
  1279. * Initialize the base part which is used whether %CONFIG_CFQ_GROUP_IOSCHED
  1280. * is enabled or not.
  1281. */
  1282. static void cfq_init_cfqg_base(struct cfq_group *cfqg)
  1283. {
  1284. struct cfq_rb_root *st;
  1285. int i, j;
  1286. for_each_cfqg_st(cfqg, i, j, st)
  1287. *st = CFQ_RB_ROOT;
  1288. RB_CLEAR_NODE(&cfqg->rb_node);
  1289. cfqg->ttime.last_end_request = jiffies;
  1290. }
  1291. #ifdef CONFIG_CFQ_GROUP_IOSCHED
  1292. static void cfqg_stats_init(struct cfqg_stats *stats)
  1293. {
  1294. blkg_rwstat_init(&stats->service_bytes);
  1295. blkg_rwstat_init(&stats->serviced);
  1296. blkg_rwstat_init(&stats->merged);
  1297. blkg_rwstat_init(&stats->service_time);
  1298. blkg_rwstat_init(&stats->wait_time);
  1299. blkg_rwstat_init(&stats->queued);
  1300. blkg_stat_init(&stats->sectors);
  1301. blkg_stat_init(&stats->time);
  1302. #ifdef CONFIG_DEBUG_BLK_CGROUP
  1303. blkg_stat_init(&stats->unaccounted_time);
  1304. blkg_stat_init(&stats->avg_queue_size_sum);
  1305. blkg_stat_init(&stats->avg_queue_size_samples);
  1306. blkg_stat_init(&stats->dequeue);
  1307. blkg_stat_init(&stats->group_wait_time);
  1308. blkg_stat_init(&stats->idle_time);
  1309. blkg_stat_init(&stats->empty_time);
  1310. #endif
  1311. }
  1312. static void cfq_pd_init(struct blkcg_gq *blkg)
  1313. {
  1314. struct cfq_group *cfqg = blkg_to_cfqg(blkg);
  1315. cfq_init_cfqg_base(cfqg);
  1316. cfqg->weight = blkg->blkcg->cfq_weight;
  1317. cfqg->leaf_weight = blkg->blkcg->cfq_leaf_weight;
  1318. cfqg_stats_init(&cfqg->stats);
  1319. cfqg_stats_init(&cfqg->dead_stats);
  1320. }
  1321. static void cfq_pd_offline(struct blkcg_gq *blkg)
  1322. {
  1323. /*
  1324. * @blkg is going offline and will be ignored by
  1325. * blkg_[rw]stat_recursive_sum(). Transfer stats to the parent so
  1326. * that they don't get lost. If IOs complete after this point, the
  1327. * stats for them will be lost. Oh well...
  1328. */
  1329. cfqg_stats_xfer_dead(blkg_to_cfqg(blkg));
  1330. }
  1331. /* offset delta from cfqg->stats to cfqg->dead_stats */
  1332. static const int dead_stats_off_delta = offsetof(struct cfq_group, dead_stats) -
  1333. offsetof(struct cfq_group, stats);
  1334. /* to be used by recursive prfill, sums live and dead stats recursively */
  1335. static u64 cfqg_stat_pd_recursive_sum(struct blkg_policy_data *pd, int off)
  1336. {
  1337. u64 sum = 0;
  1338. sum += blkg_stat_recursive_sum(pd, off);
  1339. sum += blkg_stat_recursive_sum(pd, off + dead_stats_off_delta);
  1340. return sum;
  1341. }
  1342. /* to be used by recursive prfill, sums live and dead rwstats recursively */
  1343. static struct blkg_rwstat cfqg_rwstat_pd_recursive_sum(struct blkg_policy_data *pd,
  1344. int off)
  1345. {
  1346. struct blkg_rwstat a, b;
  1347. a = blkg_rwstat_recursive_sum(pd, off);
  1348. b = blkg_rwstat_recursive_sum(pd, off + dead_stats_off_delta);
  1349. blkg_rwstat_merge(&a, &b);
  1350. return a;
  1351. }
  1352. static void cfq_pd_reset_stats(struct blkcg_gq *blkg)
  1353. {
  1354. struct cfq_group *cfqg = blkg_to_cfqg(blkg);
  1355. cfqg_stats_reset(&cfqg->stats);
  1356. cfqg_stats_reset(&cfqg->dead_stats);
  1357. }
  1358. /*
  1359. * Search for the cfq group current task belongs to. request_queue lock must
  1360. * be held.
  1361. */
  1362. static struct cfq_group *cfq_lookup_create_cfqg(struct cfq_data *cfqd,
  1363. struct blkcg *blkcg)
  1364. {
  1365. struct request_queue *q = cfqd->queue;
  1366. struct cfq_group *cfqg = NULL;
  1367. /* avoid lookup for the common case where there's no blkcg */
  1368. if (blkcg == &blkcg_root) {
  1369. cfqg = cfqd->root_group;
  1370. } else {
  1371. struct blkcg_gq *blkg;
  1372. blkg = blkg_lookup_create(blkcg, q);
  1373. if (!IS_ERR(blkg))
  1374. cfqg = blkg_to_cfqg(blkg);
  1375. }
  1376. return cfqg;
  1377. }
  1378. static void cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg)
  1379. {
  1380. /* Currently, all async queues are mapped to root group */
  1381. if (!cfq_cfqq_sync(cfqq))
  1382. cfqg = cfqq->cfqd->root_group;
  1383. cfqq->cfqg = cfqg;
  1384. /* cfqq reference on cfqg */
  1385. cfqg_get(cfqg);
  1386. }
  1387. static u64 cfqg_prfill_weight_device(struct seq_file *sf,
  1388. struct blkg_policy_data *pd, int off)
  1389. {
  1390. struct cfq_group *cfqg = pd_to_cfqg(pd);
  1391. if (!cfqg->dev_weight)
  1392. return 0;
  1393. return __blkg_prfill_u64(sf, pd, cfqg->dev_weight);
  1394. }
  1395. static int cfqg_print_weight_device(struct seq_file *sf, void *v)
  1396. {
  1397. blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
  1398. cfqg_prfill_weight_device, &blkcg_policy_cfq,
  1399. 0, false);
  1400. return 0;
  1401. }
  1402. static u64 cfqg_prfill_leaf_weight_device(struct seq_file *sf,
  1403. struct blkg_policy_data *pd, int off)
  1404. {
  1405. struct cfq_group *cfqg = pd_to_cfqg(pd);
  1406. if (!cfqg->dev_leaf_weight)
  1407. return 0;
  1408. return __blkg_prfill_u64(sf, pd, cfqg->dev_leaf_weight);
  1409. }
  1410. static int cfqg_print_leaf_weight_device(struct seq_file *sf, void *v)
  1411. {
  1412. blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
  1413. cfqg_prfill_leaf_weight_device, &blkcg_policy_cfq,
  1414. 0, false);
  1415. return 0;
  1416. }
  1417. static int cfq_print_weight(struct seq_file *sf, void *v)
  1418. {
  1419. seq_printf(sf, "%u\n", css_to_blkcg(seq_css(sf))->cfq_weight);
  1420. return 0;
  1421. }
  1422. static int cfq_print_leaf_weight(struct seq_file *sf, void *v)
  1423. {
  1424. seq_printf(sf, "%u\n", css_to_blkcg(seq_css(sf))->cfq_leaf_weight);
  1425. return 0;
  1426. }
  1427. static int __cfqg_set_weight_device(struct cgroup_subsys_state *css,
  1428. struct cftype *cft, const char *buf,
  1429. bool is_leaf_weight)
  1430. {
  1431. struct blkcg *blkcg = css_to_blkcg(css);
  1432. struct blkg_conf_ctx ctx;
  1433. struct cfq_group *cfqg;
  1434. int ret;
  1435. ret = blkg_conf_prep(blkcg, &blkcg_policy_cfq, buf, &ctx);
  1436. if (ret)
  1437. return ret;
  1438. ret = -EINVAL;
  1439. cfqg = blkg_to_cfqg(ctx.blkg);
  1440. if (!ctx.v || (ctx.v >= CFQ_WEIGHT_MIN && ctx.v <= CFQ_WEIGHT_MAX)) {
  1441. if (!is_leaf_weight) {
  1442. cfqg->dev_weight = ctx.v;
  1443. cfqg->new_weight = ctx.v ?: blkcg->cfq_weight;
  1444. } else {
  1445. cfqg->dev_leaf_weight = ctx.v;
  1446. cfqg->new_leaf_weight = ctx.v ?: blkcg->cfq_leaf_weight;
  1447. }
  1448. ret = 0;
  1449. }
  1450. blkg_conf_finish(&ctx);
  1451. return ret;
  1452. }
  1453. static int cfqg_set_weight_device(struct cgroup_subsys_state *css,
  1454. struct cftype *cft, const char *buf)
  1455. {
  1456. return __cfqg_set_weight_device(css, cft, buf, false);
  1457. }
  1458. static int cfqg_set_leaf_weight_device(struct cgroup_subsys_state *css,
  1459. struct cftype *cft, const char *buf)
  1460. {
  1461. return __cfqg_set_weight_device(css, cft, buf, true);
  1462. }
  1463. static int __cfq_set_weight(struct cgroup_subsys_state *css, struct cftype *cft,
  1464. u64 val, bool is_leaf_weight)
  1465. {
  1466. struct blkcg *blkcg = css_to_blkcg(css);
  1467. struct blkcg_gq *blkg;
  1468. if (val < CFQ_WEIGHT_MIN || val > CFQ_WEIGHT_MAX)
  1469. return -EINVAL;
  1470. spin_lock_irq(&blkcg->lock);
  1471. if (!is_leaf_weight)
  1472. blkcg->cfq_weight = val;
  1473. else
  1474. blkcg->cfq_leaf_weight = val;
  1475. hlist_for_each_entry(blkg, &blkcg->blkg_list, blkcg_node) {
  1476. struct cfq_group *cfqg = blkg_to_cfqg(blkg);
  1477. if (!cfqg)
  1478. continue;
  1479. if (!is_leaf_weight) {
  1480. if (!cfqg->dev_weight)
  1481. cfqg->new_weight = blkcg->cfq_weight;
  1482. } else {
  1483. if (!cfqg->dev_leaf_weight)
  1484. cfqg->new_leaf_weight = blkcg->cfq_leaf_weight;
  1485. }
  1486. }
  1487. spin_unlock_irq(&blkcg->lock);
  1488. return 0;
  1489. }
  1490. static int cfq_set_weight(struct cgroup_subsys_state *css, struct cftype *cft,
  1491. u64 val)
  1492. {
  1493. return __cfq_set_weight(css, cft, val, false);
  1494. }
  1495. static int cfq_set_leaf_weight(struct cgroup_subsys_state *css,
  1496. struct cftype *cft, u64 val)
  1497. {
  1498. return __cfq_set_weight(css, cft, val, true);
  1499. }
  1500. static int cfqg_print_stat(struct seq_file *sf, void *v)
  1501. {
  1502. blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), blkg_prfill_stat,
  1503. &blkcg_policy_cfq, seq_cft(sf)->private, false);
  1504. return 0;
  1505. }
  1506. static int cfqg_print_rwstat(struct seq_file *sf, void *v)
  1507. {
  1508. blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), blkg_prfill_rwstat,
  1509. &blkcg_policy_cfq, seq_cft(sf)->private, true);
  1510. return 0;
  1511. }
  1512. static u64 cfqg_prfill_stat_recursive(struct seq_file *sf,
  1513. struct blkg_policy_data *pd, int off)
  1514. {
  1515. u64 sum = cfqg_stat_pd_recursive_sum(pd, off);
  1516. return __blkg_prfill_u64(sf, pd, sum);
  1517. }
  1518. static u64 cfqg_prfill_rwstat_recursive(struct seq_file *sf,
  1519. struct blkg_policy_data *pd, int off)
  1520. {
  1521. struct blkg_rwstat sum = cfqg_rwstat_pd_recursive_sum(pd, off);
  1522. return __blkg_prfill_rwstat(sf, pd, &sum);
  1523. }
  1524. static int cfqg_print_stat_recursive(struct seq_file *sf, void *v)
  1525. {
  1526. blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
  1527. cfqg_prfill_stat_recursive, &blkcg_policy_cfq,
  1528. seq_cft(sf)->private, false);
  1529. return 0;
  1530. }
  1531. static int cfqg_print_rwstat_recursive(struct seq_file *sf, void *v)
  1532. {
  1533. blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
  1534. cfqg_prfill_rwstat_recursive, &blkcg_policy_cfq,
  1535. seq_cft(sf)->private, true);
  1536. return 0;
  1537. }
  1538. #ifdef CONFIG_DEBUG_BLK_CGROUP
  1539. static u64 cfqg_prfill_avg_queue_size(struct seq_file *sf,
  1540. struct blkg_policy_data *pd, int off)
  1541. {
  1542. struct cfq_group *cfqg = pd_to_cfqg(pd);
  1543. u64 samples = blkg_stat_read(&cfqg->stats.avg_queue_size_samples);
  1544. u64 v = 0;
  1545. if (samples) {
  1546. v = blkg_stat_read(&cfqg->stats.avg_queue_size_sum);
  1547. v = div64_u64(v, samples);
  1548. }
  1549. __blkg_prfill_u64(sf, pd, v);
  1550. return 0;
  1551. }
  1552. /* print avg_queue_size */
  1553. static int cfqg_print_avg_queue_size(struct seq_file *sf, void *v)
  1554. {
  1555. blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
  1556. cfqg_prfill_avg_queue_size, &blkcg_policy_cfq,
  1557. 0, false);
  1558. return 0;
  1559. }
  1560. #endif /* CONFIG_DEBUG_BLK_CGROUP */
  1561. static struct cftype cfq_blkcg_files[] = {
  1562. /* on root, weight is mapped to leaf_weight */
  1563. {
  1564. .name = "weight_device",
  1565. .flags = CFTYPE_ONLY_ON_ROOT,
  1566. .seq_show = cfqg_print_leaf_weight_device,
  1567. .write_string = cfqg_set_leaf_weight_device,
  1568. .max_write_len = 256,
  1569. },
  1570. {
  1571. .name = "weight",
  1572. .flags = CFTYPE_ONLY_ON_ROOT,
  1573. .seq_show = cfq_print_leaf_weight,
  1574. .write_u64 = cfq_set_leaf_weight,
  1575. },
  1576. /* no such mapping necessary for !roots */
  1577. {
  1578. .name = "weight_device",
  1579. .flags = CFTYPE_NOT_ON_ROOT,
  1580. .seq_show = cfqg_print_weight_device,
  1581. .write_string = cfqg_set_weight_device,
  1582. .max_write_len = 256,
  1583. },
  1584. {
  1585. .name = "weight",
  1586. .flags = CFTYPE_NOT_ON_ROOT,
  1587. .seq_show = cfq_print_weight,
  1588. .write_u64 = cfq_set_weight,
  1589. },
  1590. {
  1591. .name = "leaf_weight_device",
  1592. .seq_show = cfqg_print_leaf_weight_device,
  1593. .write_string = cfqg_set_leaf_weight_device,
  1594. .max_write_len = 256,
  1595. },
  1596. {
  1597. .name = "leaf_weight",
  1598. .seq_show = cfq_print_leaf_weight,
  1599. .write_u64 = cfq_set_leaf_weight,
  1600. },
  1601. /* statistics, covers only the tasks in the cfqg */
  1602. {
  1603. .name = "time",
  1604. .private = offsetof(struct cfq_group, stats.time),
  1605. .seq_show = cfqg_print_stat,
  1606. },
  1607. {
  1608. .name = "sectors",
  1609. .private = offsetof(struct cfq_group, stats.sectors),
  1610. .seq_show = cfqg_print_stat,
  1611. },
  1612. {
  1613. .name = "io_service_bytes",
  1614. .private = offsetof(struct cfq_group, stats.service_bytes),
  1615. .seq_show = cfqg_print_rwstat,
  1616. },
  1617. {
  1618. .name = "io_serviced",
  1619. .private = offsetof(struct cfq_group, stats.serviced),
  1620. .seq_show = cfqg_print_rwstat,
  1621. },
  1622. {
  1623. .name = "io_service_time",
  1624. .private = offsetof(struct cfq_group, stats.service_time),
  1625. .seq_show = cfqg_print_rwstat,
  1626. },
  1627. {
  1628. .name = "io_wait_time",
  1629. .private = offsetof(struct cfq_group, stats.wait_time),
  1630. .seq_show = cfqg_print_rwstat,
  1631. },
  1632. {
  1633. .name = "io_merged",
  1634. .private = offsetof(struct cfq_group, stats.merged),
  1635. .seq_show = cfqg_print_rwstat,
  1636. },
  1637. {
  1638. .name = "io_queued",
  1639. .private = offsetof(struct cfq_group, stats.queued),
  1640. .seq_show = cfqg_print_rwstat,
  1641. },
  1642. /* the same statictics which cover the cfqg and its descendants */
  1643. {
  1644. .name = "time_recursive",
  1645. .private = offsetof(struct cfq_group, stats.time),
  1646. .seq_show = cfqg_print_stat_recursive,
  1647. },
  1648. {
  1649. .name = "sectors_recursive",
  1650. .private = offsetof(struct cfq_group, stats.sectors),
  1651. .seq_show = cfqg_print_stat_recursive,
  1652. },
  1653. {
  1654. .name = "io_service_bytes_recursive",
  1655. .private = offsetof(struct cfq_group, stats.service_bytes),
  1656. .seq_show = cfqg_print_rwstat_recursive,
  1657. },
  1658. {
  1659. .name = "io_serviced_recursive",
  1660. .private = offsetof(struct cfq_group, stats.serviced),
  1661. .seq_show = cfqg_print_rwstat_recursive,
  1662. },
  1663. {
  1664. .name = "io_service_time_recursive",
  1665. .private = offsetof(struct cfq_group, stats.service_time),
  1666. .seq_show = cfqg_print_rwstat_recursive,
  1667. },
  1668. {
  1669. .name = "io_wait_time_recursive",
  1670. .private = offsetof(struct cfq_group, stats.wait_time),
  1671. .seq_show = cfqg_print_rwstat_recursive,
  1672. },
  1673. {
  1674. .name = "io_merged_recursive",
  1675. .private = offsetof(struct cfq_group, stats.merged),
  1676. .seq_show = cfqg_print_rwstat_recursive,
  1677. },
  1678. {
  1679. .name = "io_queued_recursive",
  1680. .private = offsetof(struct cfq_group, stats.queued),
  1681. .seq_show = cfqg_print_rwstat_recursive,
  1682. },
  1683. #ifdef CONFIG_DEBUG_BLK_CGROUP
  1684. {
  1685. .name = "avg_queue_size",
  1686. .seq_show = cfqg_print_avg_queue_size,
  1687. },
  1688. {
  1689. .name = "group_wait_time",
  1690. .private = offsetof(struct cfq_group, stats.group_wait_time),
  1691. .seq_show = cfqg_print_stat,
  1692. },
  1693. {
  1694. .name = "idle_time",
  1695. .private = offsetof(struct cfq_group, stats.idle_time),
  1696. .seq_show = cfqg_print_stat,
  1697. },
  1698. {
  1699. .name = "empty_time",
  1700. .private = offsetof(struct cfq_group, stats.empty_time),
  1701. .seq_show = cfqg_print_stat,
  1702. },
  1703. {
  1704. .name = "dequeue",
  1705. .private = offsetof(struct cfq_group, stats.dequeue),
  1706. .seq_show = cfqg_print_stat,
  1707. },
  1708. {
  1709. .name = "unaccounted_time",
  1710. .private = offsetof(struct cfq_group, stats.unaccounted_time),
  1711. .seq_show = cfqg_print_stat,
  1712. },
  1713. #endif /* CONFIG_DEBUG_BLK_CGROUP */
  1714. { } /* terminate */
  1715. };
  1716. #else /* GROUP_IOSCHED */
  1717. static struct cfq_group *cfq_lookup_create_cfqg(struct cfq_data *cfqd,
  1718. struct blkcg *blkcg)
  1719. {
  1720. return cfqd->root_group;
  1721. }
  1722. static inline void
  1723. cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg) {
  1724. cfqq->cfqg = cfqg;
  1725. }
  1726. #endif /* GROUP_IOSCHED */
  1727. /*
  1728. * The cfqd->service_trees holds all pending cfq_queue's that have
  1729. * requests waiting to be processed. It is sorted in the order that
  1730. * we will service the queues.
  1731. */
  1732. static void cfq_service_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq,
  1733. bool add_front)
  1734. {
  1735. struct rb_node **p, *parent;
  1736. struct cfq_queue *__cfqq;
  1737. unsigned long rb_key;
  1738. struct cfq_rb_root *st;
  1739. int left;
  1740. int new_cfqq = 1;
  1741. st = st_for(cfqq->cfqg, cfqq_class(cfqq), cfqq_type(cfqq));
  1742. if (cfq_class_idle(cfqq)) {
  1743. rb_key = CFQ_IDLE_DELAY;
  1744. parent = rb_last(&st->rb);
  1745. if (parent && parent != &cfqq->rb_node) {
  1746. __cfqq = rb_entry(parent, struct cfq_queue, rb_node);
  1747. rb_key += __cfqq->rb_key;
  1748. } else
  1749. rb_key += jiffies;
  1750. } else if (!add_front) {
  1751. /*
  1752. * Get our rb key offset. Subtract any residual slice
  1753. * value carried from last service. A negative resid
  1754. * count indicates slice overrun, and this should position
  1755. * the next service time further away in the tree.
  1756. */
  1757. rb_key = cfq_slice_offset(cfqd, cfqq) + jiffies;
  1758. rb_key -= cfqq->slice_resid;
  1759. cfqq->slice_resid = 0;
  1760. } else {
  1761. rb_key = -HZ;
  1762. __cfqq = cfq_rb_first(st);
  1763. rb_key += __cfqq ? __cfqq->rb_key : jiffies;
  1764. }
  1765. if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
  1766. new_cfqq = 0;
  1767. /*
  1768. * same position, nothing more to do
  1769. */
  1770. if (rb_key == cfqq->rb_key && cfqq->service_tree == st)
  1771. return;
  1772. cfq_rb_erase(&cfqq->rb_node, cfqq->service_tree);
  1773. cfqq->service_tree = NULL;
  1774. }
  1775. left = 1;
  1776. parent = NULL;
  1777. cfqq->service_tree = st;
  1778. p = &st->rb.rb_node;
  1779. while (*p) {
  1780. parent = *p;
  1781. __cfqq = rb_entry(parent, struct cfq_queue, rb_node);
  1782. /*
  1783. * sort by key, that represents service time.
  1784. */
  1785. if (time_before(rb_key, __cfqq->rb_key))
  1786. p = &parent->rb_left;
  1787. else {
  1788. p = &parent->rb_right;
  1789. left = 0;
  1790. }
  1791. }
  1792. if (left)
  1793. st->left = &cfqq->rb_node;
  1794. cfqq->rb_key = rb_key;
  1795. rb_link_node(&cfqq->rb_node, parent, p);
  1796. rb_insert_color(&cfqq->rb_node, &st->rb);
  1797. st->count++;
  1798. if (add_front || !new_cfqq)
  1799. return;
  1800. cfq_group_notify_queue_add(cfqd, cfqq->cfqg);
  1801. }
  1802. static struct cfq_queue *
  1803. cfq_prio_tree_lookup(struct cfq_data *cfqd, struct rb_root *root,
  1804. sector_t sector, struct rb_node **ret_parent,
  1805. struct rb_node ***rb_link)
  1806. {
  1807. struct rb_node **p, *parent;
  1808. struct cfq_queue *cfqq = NULL;
  1809. parent = NULL;
  1810. p = &root->rb_node;
  1811. while (*p) {
  1812. struct rb_node **n;
  1813. parent = *p;
  1814. cfqq = rb_entry(parent, struct cfq_queue, p_node);
  1815. /*
  1816. * Sort strictly based on sector. Smallest to the left,
  1817. * largest to the right.
  1818. */
  1819. if (sector > blk_rq_pos(cfqq->next_rq))
  1820. n = &(*p)->rb_right;
  1821. else if (sector < blk_rq_pos(cfqq->next_rq))
  1822. n = &(*p)->rb_left;
  1823. else
  1824. break;
  1825. p = n;
  1826. cfqq = NULL;
  1827. }
  1828. *ret_parent = parent;
  1829. if (rb_link)
  1830. *rb_link = p;
  1831. return cfqq;
  1832. }
  1833. static void cfq_prio_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  1834. {
  1835. struct rb_node **p, *parent;
  1836. struct cfq_queue *__cfqq;
  1837. if (cfqq->p_root) {
  1838. rb_erase(&cfqq->p_node, cfqq->p_root);
  1839. cfqq->p_root = NULL;
  1840. }
  1841. if (cfq_class_idle(cfqq))
  1842. return;
  1843. if (!cfqq->next_rq)
  1844. return;
  1845. cfqq->p_root = &cfqd->prio_trees[cfqq->org_ioprio];
  1846. __cfqq = cfq_prio_tree_lookup(cfqd, cfqq->p_root,
  1847. blk_rq_pos(cfqq->next_rq), &parent, &p);
  1848. if (!__cfqq) {
  1849. rb_link_node(&cfqq->p_node, parent, p);
  1850. rb_insert_color(&cfqq->p_node, cfqq->p_root);
  1851. } else
  1852. cfqq->p_root = NULL;
  1853. }
  1854. /*
  1855. * Update cfqq's position in the service tree.
  1856. */
  1857. static void cfq_resort_rr_list(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  1858. {
  1859. /*
  1860. * Resorting requires the cfqq to be on the RR list already.
  1861. */
  1862. if (cfq_cfqq_on_rr(cfqq)) {
  1863. cfq_service_tree_add(cfqd, cfqq, 0);
  1864. cfq_prio_tree_add(cfqd, cfqq);
  1865. }
  1866. }
  1867. /*
  1868. * add to busy list of queues for service, trying to be fair in ordering
  1869. * the pending list according to last request service
  1870. */
  1871. static void cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  1872. {
  1873. cfq_log_cfqq(cfqd, cfqq, "add_to_rr");
  1874. BUG_ON(cfq_cfqq_on_rr(cfqq));
  1875. cfq_mark_cfqq_on_rr(cfqq);
  1876. cfqd->busy_queues++;
  1877. if (cfq_cfqq_sync(cfqq))
  1878. cfqd->busy_sync_queues++;
  1879. cfq_resort_rr_list(cfqd, cfqq);
  1880. }
  1881. /*
  1882. * Called when the cfqq no longer has requests pending, remove it from
  1883. * the service tree.
  1884. */
  1885. static void cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  1886. {
  1887. cfq_log_cfqq(cfqd, cfqq, "del_from_rr");
  1888. BUG_ON(!cfq_cfqq_on_rr(cfqq));
  1889. cfq_clear_cfqq_on_rr(cfqq);
  1890. if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
  1891. cfq_rb_erase(&cfqq->rb_node, cfqq->service_tree);
  1892. cfqq->service_tree = NULL;
  1893. }
  1894. if (cfqq->p_root) {
  1895. rb_erase(&cfqq->p_node, cfqq->p_root);
  1896. cfqq->p_root = NULL;
  1897. }
  1898. cfq_group_notify_queue_del(cfqd, cfqq->cfqg);
  1899. BUG_ON(!cfqd->busy_queues);
  1900. cfqd->busy_queues--;
  1901. if (cfq_cfqq_sync(cfqq))
  1902. cfqd->busy_sync_queues--;
  1903. }
  1904. /*
  1905. * rb tree support functions
  1906. */
  1907. static void cfq_del_rq_rb(struct request *rq)
  1908. {
  1909. struct cfq_queue *cfqq = RQ_CFQQ(rq);
  1910. const int sync = rq_is_sync(rq);
  1911. BUG_ON(!cfqq->queued[sync]);
  1912. cfqq->queued[sync]--;
  1913. elv_rb_del(&cfqq->sort_list, rq);
  1914. if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list)) {
  1915. /*
  1916. * Queue will be deleted from service tree when we actually
  1917. * expire it later. Right now just remove it from prio tree
  1918. * as it is empty.
  1919. */
  1920. if (cfqq->p_root) {
  1921. rb_erase(&cfqq->p_node, cfqq->p_root);
  1922. cfqq->p_root = NULL;
  1923. }
  1924. }
  1925. }
  1926. static void cfq_add_rq_rb(struct request *rq)
  1927. {
  1928. struct cfq_queue *cfqq = RQ_CFQQ(rq);
  1929. struct cfq_data *cfqd = cfqq->cfqd;
  1930. struct request *prev;
  1931. cfqq->queued[rq_is_sync(rq)]++;
  1932. elv_rb_add(&cfqq->sort_list, rq);
  1933. if (!cfq_cfqq_on_rr(cfqq))
  1934. cfq_add_cfqq_rr(cfqd, cfqq);
  1935. /*
  1936. * check if this request is a better next-serve candidate
  1937. */
  1938. prev = cfqq->next_rq;
  1939. cfqq->next_rq = cfq_choose_req(cfqd, cfqq->next_rq, rq, cfqd->last_position);
  1940. /*
  1941. * adjust priority tree position, if ->next_rq changes
  1942. */
  1943. if (prev != cfqq->next_rq)
  1944. cfq_prio_tree_add(cfqd, cfqq);
  1945. BUG_ON(!cfqq->next_rq);
  1946. }
  1947. static void cfq_reposition_rq_rb(struct cfq_queue *cfqq, struct request *rq)
  1948. {
  1949. elv_rb_del(&cfqq->sort_list, rq);
  1950. cfqq->queued[rq_is_sync(rq)]--;
  1951. cfqg_stats_update_io_remove(RQ_CFQG(rq), rq->cmd_flags);
  1952. cfq_add_rq_rb(rq);
  1953. cfqg_stats_update_io_add(RQ_CFQG(rq), cfqq->cfqd->serving_group,
  1954. rq->cmd_flags);
  1955. }
  1956. static struct request *
  1957. cfq_find_rq_fmerge(struct cfq_data *cfqd, struct bio *bio)
  1958. {
  1959. struct task_struct *tsk = current;
  1960. struct cfq_io_cq *cic;
  1961. struct cfq_queue *cfqq;
  1962. cic = cfq_cic_lookup(cfqd, tsk->io_context);
  1963. if (!cic)
  1964. return NULL;
  1965. cfqq = cic_to_cfqq(cic, cfq_bio_sync(bio));
  1966. if (cfqq)
  1967. return elv_rb_find(&cfqq->sort_list, bio_end_sector(bio));
  1968. return NULL;
  1969. }
  1970. static void cfq_activate_request(struct request_queue *q, struct request *rq)
  1971. {
  1972. struct cfq_data *cfqd = q->elevator->elevator_data;
  1973. cfqd->rq_in_driver++;
  1974. cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "activate rq, drv=%d",
  1975. cfqd->rq_in_driver);
  1976. cfqd->last_position = blk_rq_pos(rq) + blk_rq_sectors(rq);
  1977. }
  1978. static void cfq_deactivate_request(struct request_queue *q, struct request *rq)
  1979. {
  1980. struct cfq_data *cfqd = q->elevator->elevator_data;
  1981. WARN_ON(!cfqd->rq_in_driver);
  1982. cfqd->rq_in_driver--;
  1983. cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "deactivate rq, drv=%d",
  1984. cfqd->rq_in_driver);
  1985. }
  1986. static void cfq_remove_request(struct request *rq)
  1987. {
  1988. struct cfq_queue *cfqq = RQ_CFQQ(rq);
  1989. if (cfqq->next_rq == rq)
  1990. cfqq->next_rq = cfq_find_next_rq(cfqq->cfqd, cfqq, rq);
  1991. list_del_init(&rq->queuelist);
  1992. cfq_del_rq_rb(rq);
  1993. cfqq->cfqd->rq_queued--;
  1994. cfqg_stats_update_io_remove(RQ_CFQG(rq), rq->cmd_flags);
  1995. if (rq->cmd_flags & REQ_PRIO) {
  1996. WARN_ON(!cfqq->prio_pending);
  1997. cfqq->prio_pending--;
  1998. }
  1999. }
  2000. static int cfq_merge(struct request_queue *q, struct request **req,
  2001. struct bio *bio)
  2002. {
  2003. struct cfq_data *cfqd = q->elevator->elevator_data;
  2004. struct request *__rq;
  2005. __rq = cfq_find_rq_fmerge(cfqd, bio);
  2006. if (__rq && elv_rq_merge_ok(__rq, bio)) {
  2007. *req = __rq;
  2008. return ELEVATOR_FRONT_MERGE;
  2009. }
  2010. return ELEVATOR_NO_MERGE;
  2011. }
  2012. static void cfq_merged_request(struct request_queue *q, struct request *req,
  2013. int type)
  2014. {
  2015. if (type == ELEVATOR_FRONT_MERGE) {
  2016. struct cfq_queue *cfqq = RQ_CFQQ(req);
  2017. cfq_reposition_rq_rb(cfqq, req);
  2018. }
  2019. }
  2020. static void cfq_bio_merged(struct request_queue *q, struct request *req,
  2021. struct bio *bio)
  2022. {
  2023. cfqg_stats_update_io_merged(RQ_CFQG(req), bio->bi_rw);
  2024. }
  2025. static void
  2026. cfq_merged_requests(struct request_queue *q, struct request *rq,
  2027. struct request *next)
  2028. {
  2029. struct cfq_queue *cfqq = RQ_CFQQ(rq);
  2030. struct cfq_data *cfqd = q->elevator->elevator_data;
  2031. /*
  2032. * reposition in fifo if next is older than rq
  2033. */
  2034. if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
  2035. time_before(rq_fifo_time(next), rq_fifo_time(rq)) &&
  2036. cfqq == RQ_CFQQ(next)) {
  2037. list_move(&rq->queuelist, &next->queuelist);
  2038. rq_set_fifo_time(rq, rq_fifo_time(next));
  2039. }
  2040. if (cfqq->next_rq == next)
  2041. cfqq->next_rq = rq;
  2042. cfq_remove_request(next);
  2043. cfqg_stats_update_io_merged(RQ_CFQG(rq), next->cmd_flags);
  2044. cfqq = RQ_CFQQ(next);
  2045. /*
  2046. * all requests of this queue are merged to other queues, delete it
  2047. * from the service tree. If it's the active_queue,
  2048. * cfq_dispatch_requests() will choose to expire it or do idle
  2049. */
  2050. if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list) &&
  2051. cfqq != cfqd->active_queue)
  2052. cfq_del_cfqq_rr(cfqd, cfqq);
  2053. }
  2054. static int cfq_allow_merge(struct request_queue *q, struct request *rq,
  2055. struct bio *bio)
  2056. {
  2057. struct cfq_data *cfqd = q->elevator->elevator_data;
  2058. struct cfq_io_cq *cic;
  2059. struct cfq_queue *cfqq;
  2060. /*
  2061. * Disallow merge of a sync bio into an async request.
  2062. */
  2063. if (cfq_bio_sync(bio) && !rq_is_sync(rq))
  2064. return false;
  2065. /*
  2066. * Lookup the cfqq that this bio will be queued with and allow
  2067. * merge only if rq is queued there.
  2068. */
  2069. cic = cfq_cic_lookup(cfqd, current->io_context);
  2070. if (!cic)
  2071. return false;
  2072. cfqq = cic_to_cfqq(cic, cfq_bio_sync(bio));
  2073. return cfqq == RQ_CFQQ(rq);
  2074. }
  2075. static inline void cfq_del_timer(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  2076. {
  2077. del_timer(&cfqd->idle_slice_timer);
  2078. cfqg_stats_update_idle_time(cfqq->cfqg);
  2079. }
  2080. static void __cfq_set_active_queue(struct cfq_data *cfqd,
  2081. struct cfq_queue *cfqq)
  2082. {
  2083. if (cfqq) {
  2084. cfq_log_cfqq(cfqd, cfqq, "set_active wl_class:%d wl_type:%d",
  2085. cfqd->serving_wl_class, cfqd->serving_wl_type);
  2086. cfqg_stats_update_avg_queue_size(cfqq->cfqg);
  2087. cfqq->slice_start = 0;
  2088. cfqq->dispatch_start = jiffies;
  2089. cfqq->allocated_slice = 0;
  2090. cfqq->slice_end = 0;
  2091. cfqq->slice_dispatch = 0;
  2092. cfqq->nr_sectors = 0;
  2093. cfq_clear_cfqq_wait_request(cfqq);
  2094. cfq_clear_cfqq_must_dispatch(cfqq);
  2095. cfq_clear_cfqq_must_alloc_slice(cfqq);
  2096. cfq_clear_cfqq_fifo_expire(cfqq);
  2097. cfq_mark_cfqq_slice_new(cfqq);
  2098. cfq_del_timer(cfqd, cfqq);
  2099. }
  2100. cfqd->active_queue = cfqq;
  2101. }
  2102. /*
  2103. * current cfqq expired its slice (or was too idle), select new one
  2104. */
  2105. static void
  2106. __cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq,
  2107. bool timed_out)
  2108. {
  2109. cfq_log_cfqq(cfqd, cfqq, "slice expired t=%d", timed_out);
  2110. if (cfq_cfqq_wait_request(cfqq))
  2111. cfq_del_timer(cfqd, cfqq);
  2112. cfq_clear_cfqq_wait_request(cfqq);
  2113. cfq_clear_cfqq_wait_busy(cfqq);
  2114. /*
  2115. * If this cfqq is shared between multiple processes, check to
  2116. * make sure that those processes are still issuing I/Os within
  2117. * the mean seek distance. If not, it may be time to break the
  2118. * queues apart again.
  2119. */
  2120. if (cfq_cfqq_coop(cfqq) && CFQQ_SEEKY(cfqq))
  2121. cfq_mark_cfqq_split_coop(cfqq);
  2122. /*
  2123. * store what was left of this slice, if the queue idled/timed out
  2124. */
  2125. if (timed_out) {
  2126. if (cfq_cfqq_slice_new(cfqq))
  2127. cfqq->slice_resid = cfq_scaled_cfqq_slice(cfqd, cfqq);
  2128. else
  2129. cfqq->slice_resid = cfqq->slice_end - jiffies;
  2130. cfq_log_cfqq(cfqd, cfqq, "resid=%ld", cfqq->slice_resid);
  2131. }
  2132. cfq_group_served(cfqd, cfqq->cfqg, cfqq);
  2133. if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list))
  2134. cfq_del_cfqq_rr(cfqd, cfqq);
  2135. cfq_resort_rr_list(cfqd, cfqq);
  2136. if (cfqq == cfqd->active_queue)
  2137. cfqd->active_queue = NULL;
  2138. if (cfqd->active_cic) {
  2139. put_io_context(cfqd->active_cic->icq.ioc);
  2140. cfqd->active_cic = NULL;
  2141. }
  2142. }
  2143. static inline void cfq_slice_expired(struct cfq_data *cfqd, bool timed_out)
  2144. {
  2145. struct cfq_queue *cfqq = cfqd->active_queue;
  2146. if (cfqq)
  2147. __cfq_slice_expired(cfqd, cfqq, timed_out);
  2148. }
  2149. /*
  2150. * Get next queue for service. Unless we have a queue preemption,
  2151. * we'll simply select the first cfqq in the service tree.
  2152. */
  2153. static struct cfq_queue *cfq_get_next_queue(struct cfq_data *cfqd)
  2154. {
  2155. struct cfq_rb_root *st = st_for(cfqd->serving_group,
  2156. cfqd->serving_wl_class, cfqd->serving_wl_type);
  2157. if (!cfqd->rq_queued)
  2158. return NULL;
  2159. /* There is nothing to dispatch */
  2160. if (!st)
  2161. return NULL;
  2162. if (RB_EMPTY_ROOT(&st->rb))
  2163. return NULL;
  2164. return cfq_rb_first(st);
  2165. }
  2166. static struct cfq_queue *cfq_get_next_queue_forced(struct cfq_data *cfqd)
  2167. {
  2168. struct cfq_group *cfqg;
  2169. struct cfq_queue *cfqq;
  2170. int i, j;
  2171. struct cfq_rb_root *st;
  2172. if (!cfqd->rq_queued)
  2173. return NULL;
  2174. cfqg = cfq_get_next_cfqg(cfqd);
  2175. if (!cfqg)
  2176. return NULL;
  2177. for_each_cfqg_st(cfqg, i, j, st)
  2178. if ((cfqq = cfq_rb_first(st)) != NULL)
  2179. return cfqq;
  2180. return NULL;
  2181. }
  2182. /*
  2183. * Get and set a new active queue for service.
  2184. */
  2185. static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd,
  2186. struct cfq_queue *cfqq)
  2187. {
  2188. if (!cfqq)
  2189. cfqq = cfq_get_next_queue(cfqd);
  2190. __cfq_set_active_queue(cfqd, cfqq);
  2191. return cfqq;
  2192. }
  2193. static inline sector_t cfq_dist_from_last(struct cfq_data *cfqd,
  2194. struct request *rq)
  2195. {
  2196. if (blk_rq_pos(rq) >= cfqd->last_position)
  2197. return blk_rq_pos(rq) - cfqd->last_position;
  2198. else
  2199. return cfqd->last_position - blk_rq_pos(rq);
  2200. }
  2201. static inline int cfq_rq_close(struct cfq_data *cfqd, struct cfq_queue *cfqq,
  2202. struct request *rq)
  2203. {
  2204. return cfq_dist_from_last(cfqd, rq) <= CFQQ_CLOSE_THR;
  2205. }
  2206. static struct cfq_queue *cfqq_close(struct cfq_data *cfqd,
  2207. struct cfq_queue *cur_cfqq)
  2208. {
  2209. struct rb_root *root = &cfqd->prio_trees[cur_cfqq->org_ioprio];
  2210. struct rb_node *parent, *node;
  2211. struct cfq_queue *__cfqq;
  2212. sector_t sector = cfqd->last_position;
  2213. if (RB_EMPTY_ROOT(root))
  2214. return NULL;
  2215. /*
  2216. * First, if we find a request starting at the end of the last
  2217. * request, choose it.
  2218. */
  2219. __cfqq = cfq_prio_tree_lookup(cfqd, root, sector, &parent, NULL);
  2220. if (__cfqq)
  2221. return __cfqq;
  2222. /*
  2223. * If the exact sector wasn't found, the parent of the NULL leaf
  2224. * will contain the closest sector.
  2225. */
  2226. __cfqq = rb_entry(parent, struct cfq_queue, p_node);
  2227. if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
  2228. return __cfqq;
  2229. if (blk_rq_pos(__cfqq->next_rq) < sector)
  2230. node = rb_next(&__cfqq->p_node);
  2231. else
  2232. node = rb_prev(&__cfqq->p_node);
  2233. if (!node)
  2234. return NULL;
  2235. __cfqq = rb_entry(node, struct cfq_queue, p_node);
  2236. if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
  2237. return __cfqq;
  2238. return NULL;
  2239. }
  2240. /*
  2241. * cfqd - obvious
  2242. * cur_cfqq - passed in so that we don't decide that the current queue is
  2243. * closely cooperating with itself.
  2244. *
  2245. * So, basically we're assuming that that cur_cfqq has dispatched at least
  2246. * one request, and that cfqd->last_position reflects a position on the disk
  2247. * associated with the I/O issued by cur_cfqq. I'm not sure this is a valid
  2248. * assumption.
  2249. */
  2250. static struct cfq_queue *cfq_close_cooperator(struct cfq_data *cfqd,
  2251. struct cfq_queue *cur_cfqq)
  2252. {
  2253. struct cfq_queue *cfqq;
  2254. if (cfq_class_idle(cur_cfqq))
  2255. return NULL;
  2256. if (!cfq_cfqq_sync(cur_cfqq))
  2257. return NULL;
  2258. if (CFQQ_SEEKY(cur_cfqq))
  2259. return NULL;
  2260. /*
  2261. * Don't search priority tree if it's the only queue in the group.
  2262. */
  2263. if (cur_cfqq->cfqg->nr_cfqq == 1)
  2264. return NULL;
  2265. /*
  2266. * We should notice if some of the queues are cooperating, eg
  2267. * working closely on the same area of the disk. In that case,
  2268. * we can group them together and don't waste time idling.
  2269. */
  2270. cfqq = cfqq_close(cfqd, cur_cfqq);
  2271. if (!cfqq)
  2272. return NULL;
  2273. /* If new queue belongs to different cfq_group, don't choose it */
  2274. if (cur_cfqq->cfqg != cfqq->cfqg)
  2275. return NULL;
  2276. /*
  2277. * It only makes sense to merge sync queues.
  2278. */
  2279. if (!cfq_cfqq_sync(cfqq))
  2280. return NULL;
  2281. if (CFQQ_SEEKY(cfqq))
  2282. return NULL;
  2283. /*
  2284. * Do not merge queues of different priority classes
  2285. */
  2286. if (cfq_class_rt(cfqq) != cfq_class_rt(cur_cfqq))
  2287. return NULL;
  2288. return cfqq;
  2289. }
  2290. /*
  2291. * Determine whether we should enforce idle window for this queue.
  2292. */
  2293. static bool cfq_should_idle(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  2294. {
  2295. enum wl_class_t wl_class = cfqq_class(cfqq);
  2296. struct cfq_rb_root *st = cfqq->service_tree;
  2297. BUG_ON(!st);
  2298. BUG_ON(!st->count);
  2299. if (!cfqd->cfq_slice_idle)
  2300. return false;
  2301. /* We never do for idle class queues. */
  2302. if (wl_class == IDLE_WORKLOAD)
  2303. return false;
  2304. /* We do for queues that were marked with idle window flag. */
  2305. if (cfq_cfqq_idle_window(cfqq) &&
  2306. !(blk_queue_nonrot(cfqd->queue) && cfqd->hw_tag))
  2307. return true;
  2308. /*
  2309. * Otherwise, we do only if they are the last ones
  2310. * in their service tree.
  2311. */
  2312. if (st->count == 1 && cfq_cfqq_sync(cfqq) &&
  2313. !cfq_io_thinktime_big(cfqd, &st->ttime, false))
  2314. return true;
  2315. cfq_log_cfqq(cfqd, cfqq, "Not idling. st->count:%d", st->count);
  2316. return false;
  2317. }
  2318. static void cfq_arm_slice_timer(struct cfq_data *cfqd)
  2319. {
  2320. struct cfq_queue *cfqq = cfqd->active_queue;
  2321. struct cfq_io_cq *cic;
  2322. unsigned long sl, group_idle = 0;
  2323. /*
  2324. * SSD device without seek penalty, disable idling. But only do so
  2325. * for devices that support queuing, otherwise we still have a problem
  2326. * with sync vs async workloads.
  2327. */
  2328. if (blk_queue_nonrot(cfqd->queue) && cfqd->hw_tag)
  2329. return;
  2330. WARN_ON(!RB_EMPTY_ROOT(&cfqq->sort_list));
  2331. WARN_ON(cfq_cfqq_slice_new(cfqq));
  2332. /*
  2333. * idle is disabled, either manually or by past process history
  2334. */
  2335. if (!cfq_should_idle(cfqd, cfqq)) {
  2336. /* no queue idling. Check for group idling */
  2337. if (cfqd->cfq_group_idle)
  2338. group_idle = cfqd->cfq_group_idle;
  2339. else
  2340. return;
  2341. }
  2342. /*
  2343. * still active requests from this queue, don't idle
  2344. */
  2345. if (cfqq->dispatched)
  2346. return;
  2347. /*
  2348. * task has exited, don't wait
  2349. */
  2350. cic = cfqd->active_cic;
  2351. if (!cic || !atomic_read(&cic->icq.ioc->active_ref))
  2352. return;
  2353. /*
  2354. * If our average think time is larger than the remaining time
  2355. * slice, then don't idle. This avoids overrunning the allotted
  2356. * time slice.
  2357. */
  2358. if (sample_valid(cic->ttime.ttime_samples) &&
  2359. (cfqq->slice_end - jiffies < cic->ttime.ttime_mean)) {
  2360. cfq_log_cfqq(cfqd, cfqq, "Not idling. think_time:%lu",
  2361. cic->ttime.ttime_mean);
  2362. return;
  2363. }
  2364. /* There are other queues in the group, don't do group idle */
  2365. if (group_idle && cfqq->cfqg->nr_cfqq > 1)
  2366. return;
  2367. cfq_mark_cfqq_wait_request(cfqq);
  2368. if (group_idle)
  2369. sl = cfqd->cfq_group_idle;
  2370. else
  2371. sl = cfqd->cfq_slice_idle;
  2372. mod_timer(&cfqd->idle_slice_timer, jiffies + sl);
  2373. cfqg_stats_set_start_idle_time(cfqq->cfqg);
  2374. cfq_log_cfqq(cfqd, cfqq, "arm_idle: %lu group_idle: %d", sl,
  2375. group_idle ? 1 : 0);
  2376. }
  2377. /*
  2378. * Move request from internal lists to the request queue dispatch list.
  2379. */
  2380. static void cfq_dispatch_insert(struct request_queue *q, struct request *rq)
  2381. {
  2382. struct cfq_data *cfqd = q->elevator->elevator_data;
  2383. struct cfq_queue *cfqq = RQ_CFQQ(rq);
  2384. cfq_log_cfqq(cfqd, cfqq, "dispatch_insert");
  2385. cfqq->next_rq = cfq_find_next_rq(cfqd, cfqq, rq);
  2386. cfq_remove_request(rq);
  2387. cfqq->dispatched++;
  2388. (RQ_CFQG(rq))->dispatched++;
  2389. elv_dispatch_sort(q, rq);
  2390. cfqd->rq_in_flight[cfq_cfqq_sync(cfqq)]++;
  2391. cfqq->nr_sectors += blk_rq_sectors(rq);
  2392. cfqg_stats_update_dispatch(cfqq->cfqg, blk_rq_bytes(rq), rq->cmd_flags);
  2393. }
  2394. /*
  2395. * return expired entry, or NULL to just start from scratch in rbtree
  2396. */
  2397. static struct request *cfq_check_fifo(struct cfq_queue *cfqq)
  2398. {
  2399. struct request *rq = NULL;
  2400. if (cfq_cfqq_fifo_expire(cfqq))
  2401. return NULL;
  2402. cfq_mark_cfqq_fifo_expire(cfqq);
  2403. if (list_empty(&cfqq->fifo))
  2404. return NULL;
  2405. rq = rq_entry_fifo(cfqq->fifo.next);
  2406. if (time_before(jiffies, rq_fifo_time(rq)))
  2407. rq = NULL;
  2408. cfq_log_cfqq(cfqq->cfqd, cfqq, "fifo=%p", rq);
  2409. return rq;
  2410. }
  2411. static inline int
  2412. cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  2413. {
  2414. const int base_rq = cfqd->cfq_slice_async_rq;
  2415. WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
  2416. return 2 * base_rq * (IOPRIO_BE_NR - cfqq->ioprio);
  2417. }
  2418. /*
  2419. * Must be called with the queue_lock held.
  2420. */
  2421. static int cfqq_process_refs(struct cfq_queue *cfqq)
  2422. {
  2423. int process_refs, io_refs;
  2424. io_refs = cfqq->allocated[READ] + cfqq->allocated[WRITE];
  2425. process_refs = cfqq->ref - io_refs;
  2426. BUG_ON(process_refs < 0);
  2427. return process_refs;
  2428. }
  2429. static void cfq_setup_merge(struct cfq_queue *cfqq, struct cfq_queue *new_cfqq)
  2430. {
  2431. int process_refs, new_process_refs;
  2432. struct cfq_queue *__cfqq;
  2433. /*
  2434. * If there are no process references on the new_cfqq, then it is
  2435. * unsafe to follow the ->new_cfqq chain as other cfqq's in the
  2436. * chain may have dropped their last reference (not just their
  2437. * last process reference).
  2438. */
  2439. if (!cfqq_process_refs(new_cfqq))
  2440. return;
  2441. /* Avoid a circular list and skip interim queue merges */
  2442. while ((__cfqq = new_cfqq->new_cfqq)) {
  2443. if (__cfqq == cfqq)
  2444. return;
  2445. new_cfqq = __cfqq;
  2446. }
  2447. process_refs = cfqq_process_refs(cfqq);
  2448. new_process_refs = cfqq_process_refs(new_cfqq);
  2449. /*
  2450. * If the process for the cfqq has gone away, there is no
  2451. * sense in merging the queues.
  2452. */
  2453. if (process_refs == 0 || new_process_refs == 0)
  2454. return;
  2455. /*
  2456. * Merge in the direction of the lesser amount of work.
  2457. */
  2458. if (new_process_refs >= process_refs) {
  2459. cfqq->new_cfqq = new_cfqq;
  2460. new_cfqq->ref += process_refs;
  2461. } else {
  2462. new_cfqq->new_cfqq = cfqq;
  2463. cfqq->ref += new_process_refs;
  2464. }
  2465. }
  2466. static enum wl_type_t cfq_choose_wl_type(struct cfq_data *cfqd,
  2467. struct cfq_group *cfqg, enum wl_class_t wl_class)
  2468. {
  2469. struct cfq_queue *queue;
  2470. int i;
  2471. bool key_valid = false;
  2472. unsigned long lowest_key = 0;
  2473. enum wl_type_t cur_best = SYNC_NOIDLE_WORKLOAD;
  2474. for (i = 0; i <= SYNC_WORKLOAD; ++i) {
  2475. /* select the one with lowest rb_key */
  2476. queue = cfq_rb_first(st_for(cfqg, wl_class, i));
  2477. if (queue &&
  2478. (!key_valid || time_before(queue->rb_key, lowest_key))) {
  2479. lowest_key = queue->rb_key;
  2480. cur_best = i;
  2481. key_valid = true;
  2482. }
  2483. }
  2484. return cur_best;
  2485. }
  2486. static void
  2487. choose_wl_class_and_type(struct cfq_data *cfqd, struct cfq_group *cfqg)
  2488. {
  2489. unsigned slice;
  2490. unsigned count;
  2491. struct cfq_rb_root *st;
  2492. unsigned group_slice;
  2493. enum wl_class_t original_class = cfqd->serving_wl_class;
  2494. /* Choose next priority. RT > BE > IDLE */
  2495. if (cfq_group_busy_queues_wl(RT_WORKLOAD, cfqd, cfqg))
  2496. cfqd->serving_wl_class = RT_WORKLOAD;
  2497. else if (cfq_group_busy_queues_wl(BE_WORKLOAD, cfqd, cfqg))
  2498. cfqd->serving_wl_class = BE_WORKLOAD;
  2499. else {
  2500. cfqd->serving_wl_class = IDLE_WORKLOAD;
  2501. cfqd->workload_expires = jiffies + 1;
  2502. return;
  2503. }
  2504. if (original_class != cfqd->serving_wl_class)
  2505. goto new_workload;
  2506. /*
  2507. * For RT and BE, we have to choose also the type
  2508. * (SYNC, SYNC_NOIDLE, ASYNC), and to compute a workload
  2509. * expiration time
  2510. */
  2511. st = st_for(cfqg, cfqd->serving_wl_class, cfqd->serving_wl_type);
  2512. count = st->count;
  2513. /*
  2514. * check workload expiration, and that we still have other queues ready
  2515. */
  2516. if (count && !time_after(jiffies, cfqd->workload_expires))
  2517. return;
  2518. new_workload:
  2519. /* otherwise select new workload type */
  2520. cfqd->serving_wl_type = cfq_choose_wl_type(cfqd, cfqg,
  2521. cfqd->serving_wl_class);
  2522. st = st_for(cfqg, cfqd->serving_wl_class, cfqd->serving_wl_type);
  2523. count = st->count;
  2524. /*
  2525. * the workload slice is computed as a fraction of target latency
  2526. * proportional to the number of queues in that workload, over
  2527. * all the queues in the same priority class
  2528. */
  2529. group_slice = cfq_group_slice(cfqd, cfqg);
  2530. slice = group_slice * count /
  2531. max_t(unsigned, cfqg->busy_queues_avg[cfqd->serving_wl_class],
  2532. cfq_group_busy_queues_wl(cfqd->serving_wl_class, cfqd,
  2533. cfqg));
  2534. if (cfqd->serving_wl_type == ASYNC_WORKLOAD) {
  2535. unsigned int tmp;
  2536. /*
  2537. * Async queues are currently system wide. Just taking
  2538. * proportion of queues with-in same group will lead to higher
  2539. * async ratio system wide as generally root group is going
  2540. * to have higher weight. A more accurate thing would be to
  2541. * calculate system wide asnc/sync ratio.
  2542. */
  2543. tmp = cfqd->cfq_target_latency *
  2544. cfqg_busy_async_queues(cfqd, cfqg);
  2545. tmp = tmp/cfqd->busy_queues;
  2546. slice = min_t(unsigned, slice, tmp);
  2547. /* async workload slice is scaled down according to
  2548. * the sync/async slice ratio. */
  2549. slice = slice * cfqd->cfq_slice[0] / cfqd->cfq_slice[1];
  2550. } else
  2551. /* sync workload slice is at least 2 * cfq_slice_idle */
  2552. slice = max(slice, 2 * cfqd->cfq_slice_idle);
  2553. slice = max_t(unsigned, slice, CFQ_MIN_TT);
  2554. cfq_log(cfqd, "workload slice:%d", slice);
  2555. cfqd->workload_expires = jiffies + slice;
  2556. }
  2557. static struct cfq_group *cfq_get_next_cfqg(struct cfq_data *cfqd)
  2558. {
  2559. struct cfq_rb_root *st = &cfqd->grp_service_tree;
  2560. struct cfq_group *cfqg;
  2561. if (RB_EMPTY_ROOT(&st->rb))
  2562. return NULL;
  2563. cfqg = cfq_rb_first_group(st);
  2564. update_min_vdisktime(st);
  2565. return cfqg;
  2566. }
  2567. static void cfq_choose_cfqg(struct cfq_data *cfqd)
  2568. {
  2569. struct cfq_group *cfqg = cfq_get_next_cfqg(cfqd);
  2570. cfqd->serving_group = cfqg;
  2571. /* Restore the workload type data */
  2572. if (cfqg->saved_wl_slice) {
  2573. cfqd->workload_expires = jiffies + cfqg->saved_wl_slice;
  2574. cfqd->serving_wl_type = cfqg->saved_wl_type;
  2575. cfqd->serving_wl_class = cfqg->saved_wl_class;
  2576. } else
  2577. cfqd->workload_expires = jiffies - 1;
  2578. choose_wl_class_and_type(cfqd, cfqg);
  2579. }
  2580. /*
  2581. * Select a queue for service. If we have a current active queue,
  2582. * check whether to continue servicing it, or retrieve and set a new one.
  2583. */
  2584. static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd)
  2585. {
  2586. struct cfq_queue *cfqq, *new_cfqq = NULL;
  2587. cfqq = cfqd->active_queue;
  2588. if (!cfqq)
  2589. goto new_queue;
  2590. if (!cfqd->rq_queued)
  2591. return NULL;
  2592. /*
  2593. * We were waiting for group to get backlogged. Expire the queue
  2594. */
  2595. if (cfq_cfqq_wait_busy(cfqq) && !RB_EMPTY_ROOT(&cfqq->sort_list))
  2596. goto expire;
  2597. /*
  2598. * The active queue has run out of time, expire it and select new.
  2599. */
  2600. if (cfq_slice_used(cfqq) && !cfq_cfqq_must_dispatch(cfqq)) {
  2601. /*
  2602. * If slice had not expired at the completion of last request
  2603. * we might not have turned on wait_busy flag. Don't expire
  2604. * the queue yet. Allow the group to get backlogged.
  2605. *
  2606. * The very fact that we have used the slice, that means we
  2607. * have been idling all along on this queue and it should be
  2608. * ok to wait for this request to complete.
  2609. */
  2610. if (cfqq->cfqg->nr_cfqq == 1 && RB_EMPTY_ROOT(&cfqq->sort_list)
  2611. && cfqq->dispatched && cfq_should_idle(cfqd, cfqq)) {
  2612. cfqq = NULL;
  2613. goto keep_queue;
  2614. } else
  2615. goto check_group_idle;
  2616. }
  2617. /*
  2618. * The active queue has requests and isn't expired, allow it to
  2619. * dispatch.
  2620. */
  2621. if (!RB_EMPTY_ROOT(&cfqq->sort_list))
  2622. goto keep_queue;
  2623. /*
  2624. * If another queue has a request waiting within our mean seek
  2625. * distance, let it run. The expire code will check for close
  2626. * cooperators and put the close queue at the front of the service
  2627. * tree. If possible, merge the expiring queue with the new cfqq.
  2628. */
  2629. new_cfqq = cfq_close_cooperator(cfqd, cfqq);
  2630. if (new_cfqq) {
  2631. if (!cfqq->new_cfqq)
  2632. cfq_setup_merge(cfqq, new_cfqq);
  2633. goto expire;
  2634. }
  2635. /*
  2636. * No requests pending. If the active queue still has requests in
  2637. * flight or is idling for a new request, allow either of these
  2638. * conditions to happen (or time out) before selecting a new queue.
  2639. */
  2640. if (timer_pending(&cfqd->idle_slice_timer)) {
  2641. cfqq = NULL;
  2642. goto keep_queue;
  2643. }
  2644. /*
  2645. * This is a deep seek queue, but the device is much faster than
  2646. * the queue can deliver, don't idle
  2647. **/
  2648. if (CFQQ_SEEKY(cfqq) && cfq_cfqq_idle_window(cfqq) &&
  2649. (cfq_cfqq_slice_new(cfqq) ||
  2650. (cfqq->slice_end - jiffies > jiffies - cfqq->slice_start))) {
  2651. cfq_clear_cfqq_deep(cfqq);
  2652. cfq_clear_cfqq_idle_window(cfqq);
  2653. }
  2654. if (cfqq->dispatched && cfq_should_idle(cfqd, cfqq)) {
  2655. cfqq = NULL;
  2656. goto keep_queue;
  2657. }
  2658. /*
  2659. * If group idle is enabled and there are requests dispatched from
  2660. * this group, wait for requests to complete.
  2661. */
  2662. check_group_idle:
  2663. if (cfqd->cfq_group_idle && cfqq->cfqg->nr_cfqq == 1 &&
  2664. cfqq->cfqg->dispatched &&
  2665. !cfq_io_thinktime_big(cfqd, &cfqq->cfqg->ttime, true)) {
  2666. cfqq = NULL;
  2667. goto keep_queue;
  2668. }
  2669. expire:
  2670. cfq_slice_expired(cfqd, 0);
  2671. new_queue:
  2672. /*
  2673. * Current queue expired. Check if we have to switch to a new
  2674. * service tree
  2675. */
  2676. if (!new_cfqq)
  2677. cfq_choose_cfqg(cfqd);
  2678. cfqq = cfq_set_active_queue(cfqd, new_cfqq);
  2679. keep_queue:
  2680. return cfqq;
  2681. }
  2682. static int __cfq_forced_dispatch_cfqq(struct cfq_queue *cfqq)
  2683. {
  2684. int dispatched = 0;
  2685. while (cfqq->next_rq) {
  2686. cfq_dispatch_insert(cfqq->cfqd->queue, cfqq->next_rq);
  2687. dispatched++;
  2688. }
  2689. BUG_ON(!list_empty(&cfqq->fifo));
  2690. /* By default cfqq is not expired if it is empty. Do it explicitly */
  2691. __cfq_slice_expired(cfqq->cfqd, cfqq, 0);
  2692. return dispatched;
  2693. }
  2694. /*
  2695. * Drain our current requests. Used for barriers and when switching
  2696. * io schedulers on-the-fly.
  2697. */
  2698. static int cfq_forced_dispatch(struct cfq_data *cfqd)
  2699. {
  2700. struct cfq_queue *cfqq;
  2701. int dispatched = 0;
  2702. /* Expire the timeslice of the current active queue first */
  2703. cfq_slice_expired(cfqd, 0);
  2704. while ((cfqq = cfq_get_next_queue_forced(cfqd)) != NULL) {
  2705. __cfq_set_active_queue(cfqd, cfqq);
  2706. dispatched += __cfq_forced_dispatch_cfqq(cfqq);
  2707. }
  2708. BUG_ON(cfqd->busy_queues);
  2709. cfq_log(cfqd, "forced_dispatch=%d", dispatched);
  2710. return dispatched;
  2711. }
  2712. static inline bool cfq_slice_used_soon(struct cfq_data *cfqd,
  2713. struct cfq_queue *cfqq)
  2714. {
  2715. /* the queue hasn't finished any request, can't estimate */
  2716. if (cfq_cfqq_slice_new(cfqq))
  2717. return true;
  2718. if (time_after(jiffies + cfqd->cfq_slice_idle * cfqq->dispatched,
  2719. cfqq->slice_end))
  2720. return true;
  2721. return false;
  2722. }
  2723. static bool cfq_may_dispatch(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  2724. {
  2725. unsigned int max_dispatch;
  2726. /*
  2727. * Drain async requests before we start sync IO
  2728. */
  2729. if (cfq_should_idle(cfqd, cfqq) && cfqd->rq_in_flight[BLK_RW_ASYNC])
  2730. return false;
  2731. /*
  2732. * If this is an async queue and we have sync IO in flight, let it wait
  2733. */
  2734. if (cfqd->rq_in_flight[BLK_RW_SYNC] && !cfq_cfqq_sync(cfqq))
  2735. return false;
  2736. max_dispatch = max_t(unsigned int, cfqd->cfq_quantum / 2, 1);
  2737. if (cfq_class_idle(cfqq))
  2738. max_dispatch = 1;
  2739. /*
  2740. * Does this cfqq already have too much IO in flight?
  2741. */
  2742. if (cfqq->dispatched >= max_dispatch) {
  2743. bool promote_sync = false;
  2744. /*
  2745. * idle queue must always only have a single IO in flight
  2746. */
  2747. if (cfq_class_idle(cfqq))
  2748. return false;
  2749. /*
  2750. * If there is only one sync queue
  2751. * we can ignore async queue here and give the sync
  2752. * queue no dispatch limit. The reason is a sync queue can
  2753. * preempt async queue, limiting the sync queue doesn't make
  2754. * sense. This is useful for aiostress test.
  2755. */
  2756. if (cfq_cfqq_sync(cfqq) && cfqd->busy_sync_queues == 1)
  2757. promote_sync = true;
  2758. /*
  2759. * We have other queues, don't allow more IO from this one
  2760. */
  2761. if (cfqd->busy_queues > 1 && cfq_slice_used_soon(cfqd, cfqq) &&
  2762. !promote_sync)
  2763. return false;
  2764. /*
  2765. * Sole queue user, no limit
  2766. */
  2767. if (cfqd->busy_queues == 1 || promote_sync)
  2768. max_dispatch = -1;
  2769. else
  2770. /*
  2771. * Normally we start throttling cfqq when cfq_quantum/2
  2772. * requests have been dispatched. But we can drive
  2773. * deeper queue depths at the beginning of slice
  2774. * subjected to upper limit of cfq_quantum.
  2775. * */
  2776. max_dispatch = cfqd->cfq_quantum;
  2777. }
  2778. /*
  2779. * Async queues must wait a bit before being allowed dispatch.
  2780. * We also ramp up the dispatch depth gradually for async IO,
  2781. * based on the last sync IO we serviced
  2782. */
  2783. if (!cfq_cfqq_sync(cfqq) && cfqd->cfq_latency) {
  2784. unsigned long last_sync = jiffies - cfqd->last_delayed_sync;
  2785. unsigned int depth;
  2786. depth = last_sync / cfqd->cfq_slice[1];
  2787. if (!depth && !cfqq->dispatched)
  2788. depth = 1;
  2789. if (depth < max_dispatch)
  2790. max_dispatch = depth;
  2791. }
  2792. /*
  2793. * If we're below the current max, allow a dispatch
  2794. */
  2795. return cfqq->dispatched < max_dispatch;
  2796. }
  2797. /*
  2798. * Dispatch a request from cfqq, moving them to the request queue
  2799. * dispatch list.
  2800. */
  2801. static bool cfq_dispatch_request(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  2802. {
  2803. struct request *rq;
  2804. BUG_ON(RB_EMPTY_ROOT(&cfqq->sort_list));
  2805. if (!cfq_may_dispatch(cfqd, cfqq))
  2806. return false;
  2807. /*
  2808. * follow expired path, else get first next available
  2809. */
  2810. rq = cfq_check_fifo(cfqq);
  2811. if (!rq)
  2812. rq = cfqq->next_rq;
  2813. /*
  2814. * insert request into driver dispatch list
  2815. */
  2816. cfq_dispatch_insert(cfqd->queue, rq);
  2817. if (!cfqd->active_cic) {
  2818. struct cfq_io_cq *cic = RQ_CIC(rq);
  2819. atomic_long_inc(&cic->icq.ioc->refcount);
  2820. cfqd->active_cic = cic;
  2821. }
  2822. return true;
  2823. }
  2824. /*
  2825. * Find the cfqq that we need to service and move a request from that to the
  2826. * dispatch list
  2827. */
  2828. static int cfq_dispatch_requests(struct request_queue *q, int force)
  2829. {
  2830. struct cfq_data *cfqd = q->elevator->elevator_data;
  2831. struct cfq_queue *cfqq;
  2832. if (!cfqd->busy_queues)
  2833. return 0;
  2834. if (unlikely(force))
  2835. return cfq_forced_dispatch(cfqd);
  2836. cfqq = cfq_select_queue(cfqd);
  2837. if (!cfqq)
  2838. return 0;
  2839. /*
  2840. * Dispatch a request from this cfqq, if it is allowed
  2841. */
  2842. if (!cfq_dispatch_request(cfqd, cfqq))
  2843. return 0;
  2844. cfqq->slice_dispatch++;
  2845. cfq_clear_cfqq_must_dispatch(cfqq);
  2846. /*
  2847. * expire an async queue immediately if it has used up its slice. idle
  2848. * queue always expire after 1 dispatch round.
  2849. */
  2850. if (cfqd->busy_queues > 1 && ((!cfq_cfqq_sync(cfqq) &&
  2851. cfqq->slice_dispatch >= cfq_prio_to_maxrq(cfqd, cfqq)) ||
  2852. cfq_class_idle(cfqq))) {
  2853. cfqq->slice_end = jiffies + 1;
  2854. cfq_slice_expired(cfqd, 0);
  2855. }
  2856. cfq_log_cfqq(cfqd, cfqq, "dispatched a request");
  2857. return 1;
  2858. }
  2859. /*
  2860. * task holds one reference to the queue, dropped when task exits. each rq
  2861. * in-flight on this queue also holds a reference, dropped when rq is freed.
  2862. *
  2863. * Each cfq queue took a reference on the parent group. Drop it now.
  2864. * queue lock must be held here.
  2865. */
  2866. static void cfq_put_queue(struct cfq_queue *cfqq)
  2867. {
  2868. struct cfq_data *cfqd = cfqq->cfqd;
  2869. struct cfq_group *cfqg;
  2870. BUG_ON(cfqq->ref <= 0);
  2871. cfqq->ref--;
  2872. if (cfqq->ref)
  2873. return;
  2874. cfq_log_cfqq(cfqd, cfqq, "put_queue");
  2875. BUG_ON(rb_first(&cfqq->sort_list));
  2876. BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
  2877. cfqg = cfqq->cfqg;
  2878. if (unlikely(cfqd->active_queue == cfqq)) {
  2879. __cfq_slice_expired(cfqd, cfqq, 0);
  2880. cfq_schedule_dispatch(cfqd);
  2881. }
  2882. BUG_ON(cfq_cfqq_on_rr(cfqq));
  2883. kmem_cache_free(cfq_pool, cfqq);
  2884. cfqg_put(cfqg);
  2885. }
  2886. static void cfq_put_cooperator(struct cfq_queue *cfqq)
  2887. {
  2888. struct cfq_queue *__cfqq, *next;
  2889. /*
  2890. * If this queue was scheduled to merge with another queue, be
  2891. * sure to drop the reference taken on that queue (and others in
  2892. * the merge chain). See cfq_setup_merge and cfq_merge_cfqqs.
  2893. */
  2894. __cfqq = cfqq->new_cfqq;
  2895. while (__cfqq) {
  2896. if (__cfqq == cfqq) {
  2897. WARN(1, "cfqq->new_cfqq loop detected\n");
  2898. break;
  2899. }
  2900. next = __cfqq->new_cfqq;
  2901. cfq_put_queue(__cfqq);
  2902. __cfqq = next;
  2903. }
  2904. }
  2905. static void cfq_exit_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  2906. {
  2907. if (unlikely(cfqq == cfqd->active_queue)) {
  2908. __cfq_slice_expired(cfqd, cfqq, 0);
  2909. cfq_schedule_dispatch(cfqd);
  2910. }
  2911. cfq_put_cooperator(cfqq);
  2912. cfq_put_queue(cfqq);
  2913. }
  2914. static void cfq_init_icq(struct io_cq *icq)
  2915. {
  2916. struct cfq_io_cq *cic = icq_to_cic(icq);
  2917. cic->ttime.last_end_request = jiffies;
  2918. }
  2919. static void cfq_exit_icq(struct io_cq *icq)
  2920. {
  2921. struct cfq_io_cq *cic = icq_to_cic(icq);
  2922. struct cfq_data *cfqd = cic_to_cfqd(cic);
  2923. if (cic->cfqq[BLK_RW_ASYNC]) {
  2924. cfq_exit_cfqq(cfqd, cic->cfqq[BLK_RW_ASYNC]);
  2925. cic->cfqq[BLK_RW_ASYNC] = NULL;
  2926. }
  2927. if (cic->cfqq[BLK_RW_SYNC]) {
  2928. cfq_exit_cfqq(cfqd, cic->cfqq[BLK_RW_SYNC]);
  2929. cic->cfqq[BLK_RW_SYNC] = NULL;
  2930. }
  2931. }
  2932. static void cfq_init_prio_data(struct cfq_queue *cfqq, struct cfq_io_cq *cic)
  2933. {
  2934. struct task_struct *tsk = current;
  2935. int ioprio_class;
  2936. if (!cfq_cfqq_prio_changed(cfqq))
  2937. return;
  2938. ioprio_class = IOPRIO_PRIO_CLASS(cic->ioprio);
  2939. switch (ioprio_class) {
  2940. default:
  2941. printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class);
  2942. case IOPRIO_CLASS_NONE:
  2943. /*
  2944. * no prio set, inherit CPU scheduling settings
  2945. */
  2946. cfqq->ioprio = task_nice_ioprio(tsk);
  2947. cfqq->ioprio_class = task_nice_ioclass(tsk);
  2948. break;
  2949. case IOPRIO_CLASS_RT:
  2950. cfqq->ioprio = IOPRIO_PRIO_DATA(cic->ioprio);
  2951. cfqq->ioprio_class = IOPRIO_CLASS_RT;
  2952. break;
  2953. case IOPRIO_CLASS_BE:
  2954. cfqq->ioprio = IOPRIO_PRIO_DATA(cic->ioprio);
  2955. cfqq->ioprio_class = IOPRIO_CLASS_BE;
  2956. break;
  2957. case IOPRIO_CLASS_IDLE:
  2958. cfqq->ioprio_class = IOPRIO_CLASS_IDLE;
  2959. cfqq->ioprio = 7;
  2960. cfq_clear_cfqq_idle_window(cfqq);
  2961. break;
  2962. }
  2963. /*
  2964. * keep track of original prio settings in case we have to temporarily
  2965. * elevate the priority of this queue
  2966. */
  2967. cfqq->org_ioprio = cfqq->ioprio;
  2968. cfq_clear_cfqq_prio_changed(cfqq);
  2969. }
  2970. static void check_ioprio_changed(struct cfq_io_cq *cic, struct bio *bio)
  2971. {
  2972. int ioprio = cic->icq.ioc->ioprio;
  2973. struct cfq_data *cfqd = cic_to_cfqd(cic);
  2974. struct cfq_queue *cfqq;
  2975. /*
  2976. * Check whether ioprio has changed. The condition may trigger
  2977. * spuriously on a newly created cic but there's no harm.
  2978. */
  2979. if (unlikely(!cfqd) || likely(cic->ioprio == ioprio))
  2980. return;
  2981. cfqq = cic->cfqq[BLK_RW_ASYNC];
  2982. if (cfqq) {
  2983. struct cfq_queue *new_cfqq;
  2984. new_cfqq = cfq_get_queue(cfqd, BLK_RW_ASYNC, cic, bio,
  2985. GFP_ATOMIC);
  2986. if (new_cfqq) {
  2987. cic->cfqq[BLK_RW_ASYNC] = new_cfqq;
  2988. cfq_put_queue(cfqq);
  2989. }
  2990. }
  2991. cfqq = cic->cfqq[BLK_RW_SYNC];
  2992. if (cfqq)
  2993. cfq_mark_cfqq_prio_changed(cfqq);
  2994. cic->ioprio = ioprio;
  2995. }
  2996. static void cfq_init_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
  2997. pid_t pid, bool is_sync)
  2998. {
  2999. RB_CLEAR_NODE(&cfqq->rb_node);
  3000. RB_CLEAR_NODE(&cfqq->p_node);
  3001. INIT_LIST_HEAD(&cfqq->fifo);
  3002. cfqq->ref = 0;
  3003. cfqq->cfqd = cfqd;
  3004. cfq_mark_cfqq_prio_changed(cfqq);
  3005. if (is_sync) {
  3006. if (!cfq_class_idle(cfqq))
  3007. cfq_mark_cfqq_idle_window(cfqq);
  3008. cfq_mark_cfqq_sync(cfqq);
  3009. }
  3010. cfqq->pid = pid;
  3011. }
  3012. #ifdef CONFIG_CFQ_GROUP_IOSCHED
  3013. static void check_blkcg_changed(struct cfq_io_cq *cic, struct bio *bio)
  3014. {
  3015. struct cfq_data *cfqd = cic_to_cfqd(cic);
  3016. struct cfq_queue *sync_cfqq;
  3017. uint64_t id;
  3018. rcu_read_lock();
  3019. id = bio_blkcg(bio)->id;
  3020. rcu_read_unlock();
  3021. /*
  3022. * Check whether blkcg has changed. The condition may trigger
  3023. * spuriously on a newly created cic but there's no harm.
  3024. */
  3025. if (unlikely(!cfqd) || likely(cic->blkcg_id == id))
  3026. return;
  3027. sync_cfqq = cic_to_cfqq(cic, 1);
  3028. if (sync_cfqq) {
  3029. /*
  3030. * Drop reference to sync queue. A new sync queue will be
  3031. * assigned in new group upon arrival of a fresh request.
  3032. */
  3033. cfq_log_cfqq(cfqd, sync_cfqq, "changed cgroup");
  3034. cic_set_cfqq(cic, NULL, 1);
  3035. cfq_put_queue(sync_cfqq);
  3036. }
  3037. cic->blkcg_id = id;
  3038. }
  3039. #else
  3040. static inline void check_blkcg_changed(struct cfq_io_cq *cic, struct bio *bio) { }
  3041. #endif /* CONFIG_CFQ_GROUP_IOSCHED */
  3042. static struct cfq_queue *
  3043. cfq_find_alloc_queue(struct cfq_data *cfqd, bool is_sync, struct cfq_io_cq *cic,
  3044. struct bio *bio, gfp_t gfp_mask)
  3045. {
  3046. struct blkcg *blkcg;
  3047. struct cfq_queue *cfqq, *new_cfqq = NULL;
  3048. struct cfq_group *cfqg;
  3049. retry:
  3050. rcu_read_lock();
  3051. blkcg = bio_blkcg(bio);
  3052. cfqg = cfq_lookup_create_cfqg(cfqd, blkcg);
  3053. cfqq = cic_to_cfqq(cic, is_sync);
  3054. /*
  3055. * Always try a new alloc if we fell back to the OOM cfqq
  3056. * originally, since it should just be a temporary situation.
  3057. */
  3058. if (!cfqq || cfqq == &cfqd->oom_cfqq) {
  3059. cfqq = NULL;
  3060. if (new_cfqq) {
  3061. cfqq = new_cfqq;
  3062. new_cfqq = NULL;
  3063. } else if (gfp_mask & __GFP_WAIT) {
  3064. rcu_read_unlock();
  3065. spin_unlock_irq(cfqd->queue->queue_lock);
  3066. new_cfqq = kmem_cache_alloc_node(cfq_pool,
  3067. gfp_mask | __GFP_ZERO,
  3068. cfqd->queue->node);
  3069. spin_lock_irq(cfqd->queue->queue_lock);
  3070. if (new_cfqq)
  3071. goto retry;
  3072. else
  3073. return &cfqd->oom_cfqq;
  3074. } else {
  3075. cfqq = kmem_cache_alloc_node(cfq_pool,
  3076. gfp_mask | __GFP_ZERO,
  3077. cfqd->queue->node);
  3078. }
  3079. if (cfqq) {
  3080. cfq_init_cfqq(cfqd, cfqq, current->pid, is_sync);
  3081. cfq_init_prio_data(cfqq, cic);
  3082. cfq_link_cfqq_cfqg(cfqq, cfqg);
  3083. cfq_log_cfqq(cfqd, cfqq, "alloced");
  3084. } else
  3085. cfqq = &cfqd->oom_cfqq;
  3086. }
  3087. if (new_cfqq)
  3088. kmem_cache_free(cfq_pool, new_cfqq);
  3089. rcu_read_unlock();
  3090. return cfqq;
  3091. }
  3092. static struct cfq_queue **
  3093. cfq_async_queue_prio(struct cfq_data *cfqd, int ioprio_class, int ioprio)
  3094. {
  3095. switch (ioprio_class) {
  3096. case IOPRIO_CLASS_RT:
  3097. return &cfqd->async_cfqq[0][ioprio];
  3098. case IOPRIO_CLASS_NONE:
  3099. ioprio = IOPRIO_NORM;
  3100. /* fall through */
  3101. case IOPRIO_CLASS_BE:
  3102. return &cfqd->async_cfqq[1][ioprio];
  3103. case IOPRIO_CLASS_IDLE:
  3104. return &cfqd->async_idle_cfqq;
  3105. default:
  3106. BUG();
  3107. }
  3108. }
  3109. static struct cfq_queue *
  3110. cfq_get_queue(struct cfq_data *cfqd, bool is_sync, struct cfq_io_cq *cic,
  3111. struct bio *bio, gfp_t gfp_mask)
  3112. {
  3113. const int ioprio_class = IOPRIO_PRIO_CLASS(cic->ioprio);
  3114. const int ioprio = IOPRIO_PRIO_DATA(cic->ioprio);
  3115. struct cfq_queue **async_cfqq = NULL;
  3116. struct cfq_queue *cfqq = NULL;
  3117. if (!is_sync) {
  3118. async_cfqq = cfq_async_queue_prio(cfqd, ioprio_class, ioprio);
  3119. cfqq = *async_cfqq;
  3120. }
  3121. if (!cfqq)
  3122. cfqq = cfq_find_alloc_queue(cfqd, is_sync, cic, bio, gfp_mask);
  3123. /*
  3124. * pin the queue now that it's allocated, scheduler exit will prune it
  3125. */
  3126. if (!is_sync && !(*async_cfqq)) {
  3127. cfqq->ref++;
  3128. *async_cfqq = cfqq;
  3129. }
  3130. cfqq->ref++;
  3131. return cfqq;
  3132. }
  3133. static void
  3134. __cfq_update_io_thinktime(struct cfq_ttime *ttime, unsigned long slice_idle)
  3135. {
  3136. unsigned long elapsed = jiffies - ttime->last_end_request;
  3137. elapsed = min(elapsed, 2UL * slice_idle);
  3138. ttime->ttime_samples = (7*ttime->ttime_samples + 256) / 8;
  3139. ttime->ttime_total = (7*ttime->ttime_total + 256*elapsed) / 8;
  3140. ttime->ttime_mean = (ttime->ttime_total + 128) / ttime->ttime_samples;
  3141. }
  3142. static void
  3143. cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_queue *cfqq,
  3144. struct cfq_io_cq *cic)
  3145. {
  3146. if (cfq_cfqq_sync(cfqq)) {
  3147. __cfq_update_io_thinktime(&cic->ttime, cfqd->cfq_slice_idle);
  3148. __cfq_update_io_thinktime(&cfqq->service_tree->ttime,
  3149. cfqd->cfq_slice_idle);
  3150. }
  3151. #ifdef CONFIG_CFQ_GROUP_IOSCHED
  3152. __cfq_update_io_thinktime(&cfqq->cfqg->ttime, cfqd->cfq_group_idle);
  3153. #endif
  3154. }
  3155. static void
  3156. cfq_update_io_seektime(struct cfq_data *cfqd, struct cfq_queue *cfqq,
  3157. struct request *rq)
  3158. {
  3159. sector_t sdist = 0;
  3160. sector_t n_sec = blk_rq_sectors(rq);
  3161. if (cfqq->last_request_pos) {
  3162. if (cfqq->last_request_pos < blk_rq_pos(rq))
  3163. sdist = blk_rq_pos(rq) - cfqq->last_request_pos;
  3164. else
  3165. sdist = cfqq->last_request_pos - blk_rq_pos(rq);
  3166. }
  3167. cfqq->seek_history <<= 1;
  3168. if (blk_queue_nonrot(cfqd->queue))
  3169. cfqq->seek_history |= (n_sec < CFQQ_SECT_THR_NONROT);
  3170. else
  3171. cfqq->seek_history |= (sdist > CFQQ_SEEK_THR);
  3172. }
  3173. /*
  3174. * Disable idle window if the process thinks too long or seeks so much that
  3175. * it doesn't matter
  3176. */
  3177. static void
  3178. cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq,
  3179. struct cfq_io_cq *cic)
  3180. {
  3181. int old_idle, enable_idle;
  3182. /*
  3183. * Don't idle for async or idle io prio class
  3184. */
  3185. if (!cfq_cfqq_sync(cfqq) || cfq_class_idle(cfqq))
  3186. return;
  3187. enable_idle = old_idle = cfq_cfqq_idle_window(cfqq);
  3188. if (cfqq->queued[0] + cfqq->queued[1] >= 4)
  3189. cfq_mark_cfqq_deep(cfqq);
  3190. if (cfqq->next_rq && (cfqq->next_rq->cmd_flags & REQ_NOIDLE))
  3191. enable_idle = 0;
  3192. else if (!atomic_read(&cic->icq.ioc->active_ref) ||
  3193. !cfqd->cfq_slice_idle ||
  3194. (!cfq_cfqq_deep(cfqq) && CFQQ_SEEKY(cfqq)))
  3195. enable_idle = 0;
  3196. else if (sample_valid(cic->ttime.ttime_samples)) {
  3197. if (cic->ttime.ttime_mean > cfqd->cfq_slice_idle)
  3198. enable_idle = 0;
  3199. else
  3200. enable_idle = 1;
  3201. }
  3202. if (old_idle != enable_idle) {
  3203. cfq_log_cfqq(cfqd, cfqq, "idle=%d", enable_idle);
  3204. if (enable_idle)
  3205. cfq_mark_cfqq_idle_window(cfqq);
  3206. else
  3207. cfq_clear_cfqq_idle_window(cfqq);
  3208. }
  3209. }
  3210. /*
  3211. * Check if new_cfqq should preempt the currently active queue. Return 0 for
  3212. * no or if we aren't sure, a 1 will cause a preempt.
  3213. */
  3214. static bool
  3215. cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
  3216. struct request *rq)
  3217. {
  3218. struct cfq_queue *cfqq;
  3219. cfqq = cfqd->active_queue;
  3220. if (!cfqq)
  3221. return false;
  3222. if (cfq_class_idle(new_cfqq))
  3223. return false;
  3224. if (cfq_class_idle(cfqq))
  3225. return true;
  3226. /*
  3227. * Don't allow a non-RT request to preempt an ongoing RT cfqq timeslice.
  3228. */
  3229. if (cfq_class_rt(cfqq) && !cfq_class_rt(new_cfqq))
  3230. return false;
  3231. /*
  3232. * if the new request is sync, but the currently running queue is
  3233. * not, let the sync request have priority.
  3234. */
  3235. if (rq_is_sync(rq) && !cfq_cfqq_sync(cfqq))
  3236. return true;
  3237. if (new_cfqq->cfqg != cfqq->cfqg)
  3238. return false;
  3239. if (cfq_slice_used(cfqq))
  3240. return true;
  3241. /* Allow preemption only if we are idling on sync-noidle tree */
  3242. if (cfqd->serving_wl_type == SYNC_NOIDLE_WORKLOAD &&
  3243. cfqq_type(new_cfqq) == SYNC_NOIDLE_WORKLOAD &&
  3244. new_cfqq->service_tree->count == 2 &&
  3245. RB_EMPTY_ROOT(&cfqq->sort_list))
  3246. return true;
  3247. /*
  3248. * So both queues are sync. Let the new request get disk time if
  3249. * it's a metadata request and the current queue is doing regular IO.
  3250. */
  3251. if ((rq->cmd_flags & REQ_PRIO) && !cfqq->prio_pending)
  3252. return true;
  3253. /*
  3254. * Allow an RT request to pre-empt an ongoing non-RT cfqq timeslice.
  3255. */
  3256. if (cfq_class_rt(new_cfqq) && !cfq_class_rt(cfqq))
  3257. return true;
  3258. /* An idle queue should not be idle now for some reason */
  3259. if (RB_EMPTY_ROOT(&cfqq->sort_list) && !cfq_should_idle(cfqd, cfqq))
  3260. return true;
  3261. if (!cfqd->active_cic || !cfq_cfqq_wait_request(cfqq))
  3262. return false;
  3263. /*
  3264. * if this request is as-good as one we would expect from the
  3265. * current cfqq, let it preempt
  3266. */
  3267. if (cfq_rq_close(cfqd, cfqq, rq))
  3268. return true;
  3269. return false;
  3270. }
  3271. /*
  3272. * cfqq preempts the active queue. if we allowed preempt with no slice left,
  3273. * let it have half of its nominal slice.
  3274. */
  3275. static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  3276. {
  3277. enum wl_type_t old_type = cfqq_type(cfqd->active_queue);
  3278. cfq_log_cfqq(cfqd, cfqq, "preempt");
  3279. cfq_slice_expired(cfqd, 1);
  3280. /*
  3281. * workload type is changed, don't save slice, otherwise preempt
  3282. * doesn't happen
  3283. */
  3284. if (old_type != cfqq_type(cfqq))
  3285. cfqq->cfqg->saved_wl_slice = 0;
  3286. /*
  3287. * Put the new queue at the front of the of the current list,
  3288. * so we know that it will be selected next.
  3289. */
  3290. BUG_ON(!cfq_cfqq_on_rr(cfqq));
  3291. cfq_service_tree_add(cfqd, cfqq, 1);
  3292. cfqq->slice_end = 0;
  3293. cfq_mark_cfqq_slice_new(cfqq);
  3294. }
  3295. /*
  3296. * Called when a new fs request (rq) is added (to cfqq). Check if there's
  3297. * something we should do about it
  3298. */
  3299. static void
  3300. cfq_rq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
  3301. struct request *rq)
  3302. {
  3303. struct cfq_io_cq *cic = RQ_CIC(rq);
  3304. cfqd->rq_queued++;
  3305. if (rq->cmd_flags & REQ_PRIO)
  3306. cfqq->prio_pending++;
  3307. cfq_update_io_thinktime(cfqd, cfqq, cic);
  3308. cfq_update_io_seektime(cfqd, cfqq, rq);
  3309. cfq_update_idle_window(cfqd, cfqq, cic);
  3310. cfqq->last_request_pos = blk_rq_pos(rq) + blk_rq_sectors(rq);
  3311. if (cfqq == cfqd->active_queue) {
  3312. /*
  3313. * Remember that we saw a request from this process, but
  3314. * don't start queuing just yet. Otherwise we risk seeing lots
  3315. * of tiny requests, because we disrupt the normal plugging
  3316. * and merging. If the request is already larger than a single
  3317. * page, let it rip immediately. For that case we assume that
  3318. * merging is already done. Ditto for a busy system that
  3319. * has other work pending, don't risk delaying until the
  3320. * idle timer unplug to continue working.
  3321. */
  3322. if (cfq_cfqq_wait_request(cfqq)) {
  3323. if (blk_rq_bytes(rq) > PAGE_CACHE_SIZE ||
  3324. cfqd->busy_queues > 1) {
  3325. cfq_del_timer(cfqd, cfqq);
  3326. cfq_clear_cfqq_wait_request(cfqq);
  3327. __blk_run_queue(cfqd->queue);
  3328. } else {
  3329. cfqg_stats_update_idle_time(cfqq->cfqg);
  3330. cfq_mark_cfqq_must_dispatch(cfqq);
  3331. }
  3332. }
  3333. } else if (cfq_should_preempt(cfqd, cfqq, rq)) {
  3334. /*
  3335. * not the active queue - expire current slice if it is
  3336. * idle and has expired it's mean thinktime or this new queue
  3337. * has some old slice time left and is of higher priority or
  3338. * this new queue is RT and the current one is BE
  3339. */
  3340. cfq_preempt_queue(cfqd, cfqq);
  3341. __blk_run_queue(cfqd->queue);
  3342. }
  3343. }
  3344. static void cfq_insert_request(struct request_queue *q, struct request *rq)
  3345. {
  3346. struct cfq_data *cfqd = q->elevator->elevator_data;
  3347. struct cfq_queue *cfqq = RQ_CFQQ(rq);
  3348. cfq_log_cfqq(cfqd, cfqq, "insert_request");
  3349. cfq_init_prio_data(cfqq, RQ_CIC(rq));
  3350. rq_set_fifo_time(rq, jiffies + cfqd->cfq_fifo_expire[rq_is_sync(rq)]);
  3351. list_add_tail(&rq->queuelist, &cfqq->fifo);
  3352. cfq_add_rq_rb(rq);
  3353. cfqg_stats_update_io_add(RQ_CFQG(rq), cfqd->serving_group,
  3354. rq->cmd_flags);
  3355. cfq_rq_enqueued(cfqd, cfqq, rq);
  3356. }
  3357. /*
  3358. * Update hw_tag based on peak queue depth over 50 samples under
  3359. * sufficient load.
  3360. */
  3361. static void cfq_update_hw_tag(struct cfq_data *cfqd)
  3362. {
  3363. struct cfq_queue *cfqq = cfqd->active_queue;
  3364. if (cfqd->rq_in_driver > cfqd->hw_tag_est_depth)
  3365. cfqd->hw_tag_est_depth = cfqd->rq_in_driver;
  3366. if (cfqd->hw_tag == 1)
  3367. return;
  3368. if (cfqd->rq_queued <= CFQ_HW_QUEUE_MIN &&
  3369. cfqd->rq_in_driver <= CFQ_HW_QUEUE_MIN)
  3370. return;
  3371. /*
  3372. * If active queue hasn't enough requests and can idle, cfq might not
  3373. * dispatch sufficient requests to hardware. Don't zero hw_tag in this
  3374. * case
  3375. */
  3376. if (cfqq && cfq_cfqq_idle_window(cfqq) &&
  3377. cfqq->dispatched + cfqq->queued[0] + cfqq->queued[1] <
  3378. CFQ_HW_QUEUE_MIN && cfqd->rq_in_driver < CFQ_HW_QUEUE_MIN)
  3379. return;
  3380. if (cfqd->hw_tag_samples++ < 50)
  3381. return;
  3382. if (cfqd->hw_tag_est_depth >= CFQ_HW_QUEUE_MIN)
  3383. cfqd->hw_tag = 1;
  3384. else
  3385. cfqd->hw_tag = 0;
  3386. }
  3387. static bool cfq_should_wait_busy(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  3388. {
  3389. struct cfq_io_cq *cic = cfqd->active_cic;
  3390. /* If the queue already has requests, don't wait */
  3391. if (!RB_EMPTY_ROOT(&cfqq->sort_list))
  3392. return false;
  3393. /* If there are other queues in the group, don't wait */
  3394. if (cfqq->cfqg->nr_cfqq > 1)
  3395. return false;
  3396. /* the only queue in the group, but think time is big */
  3397. if (cfq_io_thinktime_big(cfqd, &cfqq->cfqg->ttime, true))
  3398. return false;
  3399. if (cfq_slice_used(cfqq))
  3400. return true;
  3401. /* if slice left is less than think time, wait busy */
  3402. if (cic && sample_valid(cic->ttime.ttime_samples)
  3403. && (cfqq->slice_end - jiffies < cic->ttime.ttime_mean))
  3404. return true;
  3405. /*
  3406. * If think times is less than a jiffy than ttime_mean=0 and above
  3407. * will not be true. It might happen that slice has not expired yet
  3408. * but will expire soon (4-5 ns) during select_queue(). To cover the
  3409. * case where think time is less than a jiffy, mark the queue wait
  3410. * busy if only 1 jiffy is left in the slice.
  3411. */
  3412. if (cfqq->slice_end - jiffies == 1)
  3413. return true;
  3414. return false;
  3415. }
  3416. static void cfq_completed_request(struct request_queue *q, struct request *rq)
  3417. {
  3418. struct cfq_queue *cfqq = RQ_CFQQ(rq);
  3419. struct cfq_data *cfqd = cfqq->cfqd;
  3420. const int sync = rq_is_sync(rq);
  3421. unsigned long now;
  3422. now = jiffies;
  3423. cfq_log_cfqq(cfqd, cfqq, "complete rqnoidle %d",
  3424. !!(rq->cmd_flags & REQ_NOIDLE));
  3425. cfq_update_hw_tag(cfqd);
  3426. WARN_ON(!cfqd->rq_in_driver);
  3427. WARN_ON(!cfqq->dispatched);
  3428. cfqd->rq_in_driver--;
  3429. cfqq->dispatched--;
  3430. (RQ_CFQG(rq))->dispatched--;
  3431. cfqg_stats_update_completion(cfqq->cfqg, rq_start_time_ns(rq),
  3432. rq_io_start_time_ns(rq), rq->cmd_flags);
  3433. cfqd->rq_in_flight[cfq_cfqq_sync(cfqq)]--;
  3434. if (sync) {
  3435. struct cfq_rb_root *st;
  3436. RQ_CIC(rq)->ttime.last_end_request = now;
  3437. if (cfq_cfqq_on_rr(cfqq))
  3438. st = cfqq->service_tree;
  3439. else
  3440. st = st_for(cfqq->cfqg, cfqq_class(cfqq),
  3441. cfqq_type(cfqq));
  3442. st->ttime.last_end_request = now;
  3443. if (!time_after(rq->start_time + cfqd->cfq_fifo_expire[1], now))
  3444. cfqd->last_delayed_sync = now;
  3445. }
  3446. #ifdef CONFIG_CFQ_GROUP_IOSCHED
  3447. cfqq->cfqg->ttime.last_end_request = now;
  3448. #endif
  3449. /*
  3450. * If this is the active queue, check if it needs to be expired,
  3451. * or if we want to idle in case it has no pending requests.
  3452. */
  3453. if (cfqd->active_queue == cfqq) {
  3454. const bool cfqq_empty = RB_EMPTY_ROOT(&cfqq->sort_list);
  3455. if (cfq_cfqq_slice_new(cfqq)) {
  3456. cfq_set_prio_slice(cfqd, cfqq);
  3457. cfq_clear_cfqq_slice_new(cfqq);
  3458. }
  3459. /*
  3460. * Should we wait for next request to come in before we expire
  3461. * the queue.
  3462. */
  3463. if (cfq_should_wait_busy(cfqd, cfqq)) {
  3464. unsigned long extend_sl = cfqd->cfq_slice_idle;
  3465. if (!cfqd->cfq_slice_idle)
  3466. extend_sl = cfqd->cfq_group_idle;
  3467. cfqq->slice_end = jiffies + extend_sl;
  3468. cfq_mark_cfqq_wait_busy(cfqq);
  3469. cfq_log_cfqq(cfqd, cfqq, "will busy wait");
  3470. }
  3471. /*
  3472. * Idling is not enabled on:
  3473. * - expired queues
  3474. * - idle-priority queues
  3475. * - async queues
  3476. * - queues with still some requests queued
  3477. * - when there is a close cooperator
  3478. */
  3479. if (cfq_slice_used(cfqq) || cfq_class_idle(cfqq))
  3480. cfq_slice_expired(cfqd, 1);
  3481. else if (sync && cfqq_empty &&
  3482. !cfq_close_cooperator(cfqd, cfqq)) {
  3483. cfq_arm_slice_timer(cfqd);
  3484. }
  3485. }
  3486. if (!cfqd->rq_in_driver)
  3487. cfq_schedule_dispatch(cfqd);
  3488. }
  3489. static inline int __cfq_may_queue(struct cfq_queue *cfqq)
  3490. {
  3491. if (cfq_cfqq_wait_request(cfqq) && !cfq_cfqq_must_alloc_slice(cfqq)) {
  3492. cfq_mark_cfqq_must_alloc_slice(cfqq);
  3493. return ELV_MQUEUE_MUST;
  3494. }
  3495. return ELV_MQUEUE_MAY;
  3496. }
  3497. static int cfq_may_queue(struct request_queue *q, int rw)
  3498. {
  3499. struct cfq_data *cfqd = q->elevator->elevator_data;
  3500. struct task_struct *tsk = current;
  3501. struct cfq_io_cq *cic;
  3502. struct cfq_queue *cfqq;
  3503. /*
  3504. * don't force setup of a queue from here, as a call to may_queue
  3505. * does not necessarily imply that a request actually will be queued.
  3506. * so just lookup a possibly existing queue, or return 'may queue'
  3507. * if that fails
  3508. */
  3509. cic = cfq_cic_lookup(cfqd, tsk->io_context);
  3510. if (!cic)
  3511. return ELV_MQUEUE_MAY;
  3512. cfqq = cic_to_cfqq(cic, rw_is_sync(rw));
  3513. if (cfqq) {
  3514. cfq_init_prio_data(cfqq, cic);
  3515. return __cfq_may_queue(cfqq);
  3516. }
  3517. return ELV_MQUEUE_MAY;
  3518. }
  3519. /*
  3520. * queue lock held here
  3521. */
  3522. static void cfq_put_request(struct request *rq)
  3523. {
  3524. struct cfq_queue *cfqq = RQ_CFQQ(rq);
  3525. if (cfqq) {
  3526. const int rw = rq_data_dir(rq);
  3527. BUG_ON(!cfqq->allocated[rw]);
  3528. cfqq->allocated[rw]--;
  3529. /* Put down rq reference on cfqg */
  3530. cfqg_put(RQ_CFQG(rq));
  3531. rq->elv.priv[0] = NULL;
  3532. rq->elv.priv[1] = NULL;
  3533. cfq_put_queue(cfqq);
  3534. }
  3535. }
  3536. static struct cfq_queue *
  3537. cfq_merge_cfqqs(struct cfq_data *cfqd, struct cfq_io_cq *cic,
  3538. struct cfq_queue *cfqq)
  3539. {
  3540. cfq_log_cfqq(cfqd, cfqq, "merging with queue %p", cfqq->new_cfqq);
  3541. cic_set_cfqq(cic, cfqq->new_cfqq, 1);
  3542. cfq_mark_cfqq_coop(cfqq->new_cfqq);
  3543. cfq_put_queue(cfqq);
  3544. return cic_to_cfqq(cic, 1);
  3545. }
  3546. /*
  3547. * Returns NULL if a new cfqq should be allocated, or the old cfqq if this
  3548. * was the last process referring to said cfqq.
  3549. */
  3550. static struct cfq_queue *
  3551. split_cfqq(struct cfq_io_cq *cic, struct cfq_queue *cfqq)
  3552. {
  3553. if (cfqq_process_refs(cfqq) == 1) {
  3554. cfqq->pid = current->pid;
  3555. cfq_clear_cfqq_coop(cfqq);
  3556. cfq_clear_cfqq_split_coop(cfqq);
  3557. return cfqq;
  3558. }
  3559. cic_set_cfqq(cic, NULL, 1);
  3560. cfq_put_cooperator(cfqq);
  3561. cfq_put_queue(cfqq);
  3562. return NULL;
  3563. }
  3564. /*
  3565. * Allocate cfq data structures associated with this request.
  3566. */
  3567. static int
  3568. cfq_set_request(struct request_queue *q, struct request *rq, struct bio *bio,
  3569. gfp_t gfp_mask)
  3570. {
  3571. struct cfq_data *cfqd = q->elevator->elevator_data;
  3572. struct cfq_io_cq *cic = icq_to_cic(rq->elv.icq);
  3573. const int rw = rq_data_dir(rq);
  3574. const bool is_sync = rq_is_sync(rq);
  3575. struct cfq_queue *cfqq;
  3576. might_sleep_if(gfp_mask & __GFP_WAIT);
  3577. spin_lock_irq(q->queue_lock);
  3578. check_ioprio_changed(cic, bio);
  3579. check_blkcg_changed(cic, bio);
  3580. new_queue:
  3581. cfqq = cic_to_cfqq(cic, is_sync);
  3582. if (!cfqq || cfqq == &cfqd->oom_cfqq) {
  3583. cfqq = cfq_get_queue(cfqd, is_sync, cic, bio, gfp_mask);
  3584. cic_set_cfqq(cic, cfqq, is_sync);
  3585. } else {
  3586. /*
  3587. * If the queue was seeky for too long, break it apart.
  3588. */
  3589. if (cfq_cfqq_coop(cfqq) && cfq_cfqq_split_coop(cfqq)) {
  3590. cfq_log_cfqq(cfqd, cfqq, "breaking apart cfqq");
  3591. cfqq = split_cfqq(cic, cfqq);
  3592. if (!cfqq)
  3593. goto new_queue;
  3594. }
  3595. /*
  3596. * Check to see if this queue is scheduled to merge with
  3597. * another, closely cooperating queue. The merging of
  3598. * queues happens here as it must be done in process context.
  3599. * The reference on new_cfqq was taken in merge_cfqqs.
  3600. */
  3601. if (cfqq->new_cfqq)
  3602. cfqq = cfq_merge_cfqqs(cfqd, cic, cfqq);
  3603. }
  3604. cfqq->allocated[rw]++;
  3605. cfqq->ref++;
  3606. cfqg_get(cfqq->cfqg);
  3607. rq->elv.priv[0] = cfqq;
  3608. rq->elv.priv[1] = cfqq->cfqg;
  3609. spin_unlock_irq(q->queue_lock);
  3610. return 0;
  3611. }
  3612. static void cfq_kick_queue(struct work_struct *work)
  3613. {
  3614. struct cfq_data *cfqd =
  3615. container_of(work, struct cfq_data, unplug_work);
  3616. struct request_queue *q = cfqd->queue;
  3617. spin_lock_irq(q->queue_lock);
  3618. __blk_run_queue(cfqd->queue);
  3619. spin_unlock_irq(q->queue_lock);
  3620. }
  3621. /*
  3622. * Timer running if the active_queue is currently idling inside its time slice
  3623. */
  3624. static void cfq_idle_slice_timer(unsigned long data)
  3625. {
  3626. struct cfq_data *cfqd = (struct cfq_data *) data;
  3627. struct cfq_queue *cfqq;
  3628. unsigned long flags;
  3629. int timed_out = 1;
  3630. cfq_log(cfqd, "idle timer fired");
  3631. spin_lock_irqsave(cfqd->queue->queue_lock, flags);
  3632. cfqq = cfqd->active_queue;
  3633. if (cfqq) {
  3634. timed_out = 0;
  3635. /*
  3636. * We saw a request before the queue expired, let it through
  3637. */
  3638. if (cfq_cfqq_must_dispatch(cfqq))
  3639. goto out_kick;
  3640. /*
  3641. * expired
  3642. */
  3643. if (cfq_slice_used(cfqq))
  3644. goto expire;
  3645. /*
  3646. * only expire and reinvoke request handler, if there are
  3647. * other queues with pending requests
  3648. */
  3649. if (!cfqd->busy_queues)
  3650. goto out_cont;
  3651. /*
  3652. * not expired and it has a request pending, let it dispatch
  3653. */
  3654. if (!RB_EMPTY_ROOT(&cfqq->sort_list))
  3655. goto out_kick;
  3656. /*
  3657. * Queue depth flag is reset only when the idle didn't succeed
  3658. */
  3659. cfq_clear_cfqq_deep(cfqq);
  3660. }
  3661. expire:
  3662. cfq_slice_expired(cfqd, timed_out);
  3663. out_kick:
  3664. cfq_schedule_dispatch(cfqd);
  3665. out_cont:
  3666. spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
  3667. }
  3668. static void cfq_shutdown_timer_wq(struct cfq_data *cfqd)
  3669. {
  3670. del_timer_sync(&cfqd->idle_slice_timer);
  3671. cancel_work_sync(&cfqd->unplug_work);
  3672. }
  3673. static void cfq_put_async_queues(struct cfq_data *cfqd)
  3674. {
  3675. int i;
  3676. for (i = 0; i < IOPRIO_BE_NR; i++) {
  3677. if (cfqd->async_cfqq[0][i])
  3678. cfq_put_queue(cfqd->async_cfqq[0][i]);
  3679. if (cfqd->async_cfqq[1][i])
  3680. cfq_put_queue(cfqd->async_cfqq[1][i]);
  3681. }
  3682. if (cfqd->async_idle_cfqq)
  3683. cfq_put_queue(cfqd->async_idle_cfqq);
  3684. }
  3685. static void cfq_exit_queue(struct elevator_queue *e)
  3686. {
  3687. struct cfq_data *cfqd = e->elevator_data;
  3688. struct request_queue *q = cfqd->queue;
  3689. cfq_shutdown_timer_wq(cfqd);
  3690. spin_lock_irq(q->queue_lock);
  3691. if (cfqd->active_queue)
  3692. __cfq_slice_expired(cfqd, cfqd->active_queue, 0);
  3693. cfq_put_async_queues(cfqd);
  3694. spin_unlock_irq(q->queue_lock);
  3695. cfq_shutdown_timer_wq(cfqd);
  3696. #ifdef CONFIG_CFQ_GROUP_IOSCHED
  3697. blkcg_deactivate_policy(q, &blkcg_policy_cfq);
  3698. #else
  3699. kfree(cfqd->root_group);
  3700. #endif
  3701. kfree(cfqd);
  3702. }
  3703. static int cfq_init_queue(struct request_queue *q, struct elevator_type *e)
  3704. {
  3705. struct cfq_data *cfqd;
  3706. struct blkcg_gq *blkg __maybe_unused;
  3707. int i, ret;
  3708. struct elevator_queue *eq;
  3709. eq = elevator_alloc(q, e);
  3710. if (!eq)
  3711. return -ENOMEM;
  3712. cfqd = kzalloc_node(sizeof(*cfqd), GFP_KERNEL, q->node);
  3713. if (!cfqd) {
  3714. kobject_put(&eq->kobj);
  3715. return -ENOMEM;
  3716. }
  3717. eq->elevator_data = cfqd;
  3718. cfqd->queue = q;
  3719. spin_lock_irq(q->queue_lock);
  3720. q->elevator = eq;
  3721. spin_unlock_irq(q->queue_lock);
  3722. /* Init root service tree */
  3723. cfqd->grp_service_tree = CFQ_RB_ROOT;
  3724. /* Init root group and prefer root group over other groups by default */
  3725. #ifdef CONFIG_CFQ_GROUP_IOSCHED
  3726. ret = blkcg_activate_policy(q, &blkcg_policy_cfq);
  3727. if (ret)
  3728. goto out_free;
  3729. cfqd->root_group = blkg_to_cfqg(q->root_blkg);
  3730. #else
  3731. ret = -ENOMEM;
  3732. cfqd->root_group = kzalloc_node(sizeof(*cfqd->root_group),
  3733. GFP_KERNEL, cfqd->queue->node);
  3734. if (!cfqd->root_group)
  3735. goto out_free;
  3736. cfq_init_cfqg_base(cfqd->root_group);
  3737. #endif
  3738. cfqd->root_group->weight = 2 * CFQ_WEIGHT_DEFAULT;
  3739. cfqd->root_group->leaf_weight = 2 * CFQ_WEIGHT_DEFAULT;
  3740. /*
  3741. * Not strictly needed (since RB_ROOT just clears the node and we
  3742. * zeroed cfqd on alloc), but better be safe in case someone decides
  3743. * to add magic to the rb code
  3744. */
  3745. for (i = 0; i < CFQ_PRIO_LISTS; i++)
  3746. cfqd->prio_trees[i] = RB_ROOT;
  3747. /*
  3748. * Our fallback cfqq if cfq_find_alloc_queue() runs into OOM issues.
  3749. * Grab a permanent reference to it, so that the normal code flow
  3750. * will not attempt to free it. oom_cfqq is linked to root_group
  3751. * but shouldn't hold a reference as it'll never be unlinked. Lose
  3752. * the reference from linking right away.
  3753. */
  3754. cfq_init_cfqq(cfqd, &cfqd->oom_cfqq, 1, 0);
  3755. cfqd->oom_cfqq.ref++;
  3756. spin_lock_irq(q->queue_lock);
  3757. cfq_link_cfqq_cfqg(&cfqd->oom_cfqq, cfqd->root_group);
  3758. cfqg_put(cfqd->root_group);
  3759. spin_unlock_irq(q->queue_lock);
  3760. init_timer(&cfqd->idle_slice_timer);
  3761. cfqd->idle_slice_timer.function = cfq_idle_slice_timer;
  3762. cfqd->idle_slice_timer.data = (unsigned long) cfqd;
  3763. INIT_WORK(&cfqd->unplug_work, cfq_kick_queue);
  3764. cfqd->cfq_quantum = cfq_quantum;
  3765. cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0];
  3766. cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1];
  3767. cfqd->cfq_back_max = cfq_back_max;
  3768. cfqd->cfq_back_penalty = cfq_back_penalty;
  3769. cfqd->cfq_slice[0] = cfq_slice_async;
  3770. cfqd->cfq_slice[1] = cfq_slice_sync;
  3771. cfqd->cfq_target_latency = cfq_target_latency;
  3772. cfqd->cfq_slice_async_rq = cfq_slice_async_rq;
  3773. cfqd->cfq_slice_idle = cfq_slice_idle;
  3774. cfqd->cfq_group_idle = cfq_group_idle;
  3775. cfqd->cfq_latency = 1;
  3776. cfqd->hw_tag = -1;
  3777. /*
  3778. * we optimistically start assuming sync ops weren't delayed in last
  3779. * second, in order to have larger depth for async operations.
  3780. */
  3781. cfqd->last_delayed_sync = jiffies - HZ;
  3782. return 0;
  3783. out_free:
  3784. kfree(cfqd);
  3785. kobject_put(&eq->kobj);
  3786. return ret;
  3787. }
  3788. /*
  3789. * sysfs parts below -->
  3790. */
  3791. static ssize_t
  3792. cfq_var_show(unsigned int var, char *page)
  3793. {
  3794. return sprintf(page, "%d\n", var);
  3795. }
  3796. static ssize_t
  3797. cfq_var_store(unsigned int *var, const char *page, size_t count)
  3798. {
  3799. char *p = (char *) page;
  3800. *var = simple_strtoul(p, &p, 10);
  3801. return count;
  3802. }
  3803. #define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
  3804. static ssize_t __FUNC(struct elevator_queue *e, char *page) \
  3805. { \
  3806. struct cfq_data *cfqd = e->elevator_data; \
  3807. unsigned int __data = __VAR; \
  3808. if (__CONV) \
  3809. __data = jiffies_to_msecs(__data); \
  3810. return cfq_var_show(__data, (page)); \
  3811. }
  3812. SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0);
  3813. SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1);
  3814. SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1);
  3815. SHOW_FUNCTION(cfq_back_seek_max_show, cfqd->cfq_back_max, 0);
  3816. SHOW_FUNCTION(cfq_back_seek_penalty_show, cfqd->cfq_back_penalty, 0);
  3817. SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1);
  3818. SHOW_FUNCTION(cfq_group_idle_show, cfqd->cfq_group_idle, 1);
  3819. SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1);
  3820. SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1);
  3821. SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0);
  3822. SHOW_FUNCTION(cfq_low_latency_show, cfqd->cfq_latency, 0);
  3823. SHOW_FUNCTION(cfq_target_latency_show, cfqd->cfq_target_latency, 1);
  3824. #undef SHOW_FUNCTION
  3825. #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
  3826. static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count) \
  3827. { \
  3828. struct cfq_data *cfqd = e->elevator_data; \
  3829. unsigned int __data; \
  3830. int ret = cfq_var_store(&__data, (page), count); \
  3831. if (__data < (MIN)) \
  3832. __data = (MIN); \
  3833. else if (__data > (MAX)) \
  3834. __data = (MAX); \
  3835. if (__CONV) \
  3836. *(__PTR) = msecs_to_jiffies(__data); \
  3837. else \
  3838. *(__PTR) = __data; \
  3839. return ret; \
  3840. }
  3841. STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0);
  3842. STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1,
  3843. UINT_MAX, 1);
  3844. STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1,
  3845. UINT_MAX, 1);
  3846. STORE_FUNCTION(cfq_back_seek_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0);
  3847. STORE_FUNCTION(cfq_back_seek_penalty_store, &cfqd->cfq_back_penalty, 1,
  3848. UINT_MAX, 0);
  3849. STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1);
  3850. STORE_FUNCTION(cfq_group_idle_store, &cfqd->cfq_group_idle, 0, UINT_MAX, 1);
  3851. STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1);
  3852. STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1);
  3853. STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1,
  3854. UINT_MAX, 0);
  3855. STORE_FUNCTION(cfq_low_latency_store, &cfqd->cfq_latency, 0, 1, 0);
  3856. STORE_FUNCTION(cfq_target_latency_store, &cfqd->cfq_target_latency, 1, UINT_MAX, 1);
  3857. #undef STORE_FUNCTION
  3858. #define CFQ_ATTR(name) \
  3859. __ATTR(name, S_IRUGO|S_IWUSR, cfq_##name##_show, cfq_##name##_store)
  3860. static struct elv_fs_entry cfq_attrs[] = {
  3861. CFQ_ATTR(quantum),
  3862. CFQ_ATTR(fifo_expire_sync),
  3863. CFQ_ATTR(fifo_expire_async),
  3864. CFQ_ATTR(back_seek_max),
  3865. CFQ_ATTR(back_seek_penalty),
  3866. CFQ_ATTR(slice_sync),
  3867. CFQ_ATTR(slice_async),
  3868. CFQ_ATTR(slice_async_rq),
  3869. CFQ_ATTR(slice_idle),
  3870. CFQ_ATTR(group_idle),
  3871. CFQ_ATTR(low_latency),
  3872. CFQ_ATTR(target_latency),
  3873. __ATTR_NULL
  3874. };
  3875. static struct elevator_type iosched_cfq = {
  3876. .ops = {
  3877. .elevator_merge_fn = cfq_merge,
  3878. .elevator_merged_fn = cfq_merged_request,
  3879. .elevator_merge_req_fn = cfq_merged_requests,
  3880. .elevator_allow_merge_fn = cfq_allow_merge,
  3881. .elevator_bio_merged_fn = cfq_bio_merged,
  3882. .elevator_dispatch_fn = cfq_dispatch_requests,
  3883. .elevator_add_req_fn = cfq_insert_request,
  3884. .elevator_activate_req_fn = cfq_activate_request,
  3885. .elevator_deactivate_req_fn = cfq_deactivate_request,
  3886. .elevator_completed_req_fn = cfq_completed_request,
  3887. .elevator_former_req_fn = elv_rb_former_request,
  3888. .elevator_latter_req_fn = elv_rb_latter_request,
  3889. .elevator_init_icq_fn = cfq_init_icq,
  3890. .elevator_exit_icq_fn = cfq_exit_icq,
  3891. .elevator_set_req_fn = cfq_set_request,
  3892. .elevator_put_req_fn = cfq_put_request,
  3893. .elevator_may_queue_fn = cfq_may_queue,
  3894. .elevator_init_fn = cfq_init_queue,
  3895. .elevator_exit_fn = cfq_exit_queue,
  3896. },
  3897. .icq_size = sizeof(struct cfq_io_cq),
  3898. .icq_align = __alignof__(struct cfq_io_cq),
  3899. .elevator_attrs = cfq_attrs,
  3900. .elevator_name = "cfq",
  3901. .elevator_owner = THIS_MODULE,
  3902. };
  3903. #ifdef CONFIG_CFQ_GROUP_IOSCHED
  3904. static struct blkcg_policy blkcg_policy_cfq = {
  3905. .pd_size = sizeof(struct cfq_group),
  3906. .cftypes = cfq_blkcg_files,
  3907. .pd_init_fn = cfq_pd_init,
  3908. .pd_offline_fn = cfq_pd_offline,
  3909. .pd_reset_stats_fn = cfq_pd_reset_stats,
  3910. };
  3911. #endif
  3912. static int __init cfq_init(void)
  3913. {
  3914. int ret;
  3915. /*
  3916. * could be 0 on HZ < 1000 setups
  3917. */
  3918. if (!cfq_slice_async)
  3919. cfq_slice_async = 1;
  3920. if (!cfq_slice_idle)
  3921. cfq_slice_idle = 1;
  3922. #ifdef CONFIG_CFQ_GROUP_IOSCHED
  3923. if (!cfq_group_idle)
  3924. cfq_group_idle = 1;
  3925. ret = blkcg_policy_register(&blkcg_policy_cfq);
  3926. if (ret)
  3927. return ret;
  3928. #else
  3929. cfq_group_idle = 0;
  3930. #endif
  3931. ret = -ENOMEM;
  3932. cfq_pool = KMEM_CACHE(cfq_queue, 0);
  3933. if (!cfq_pool)
  3934. goto err_pol_unreg;
  3935. ret = elv_register(&iosched_cfq);
  3936. if (ret)
  3937. goto err_free_pool;
  3938. return 0;
  3939. err_free_pool:
  3940. kmem_cache_destroy(cfq_pool);
  3941. err_pol_unreg:
  3942. #ifdef CONFIG_CFQ_GROUP_IOSCHED
  3943. blkcg_policy_unregister(&blkcg_policy_cfq);
  3944. #endif
  3945. return ret;
  3946. }
  3947. static void __exit cfq_exit(void)
  3948. {
  3949. #ifdef CONFIG_CFQ_GROUP_IOSCHED
  3950. blkcg_policy_unregister(&blkcg_policy_cfq);
  3951. #endif
  3952. elv_unregister(&iosched_cfq);
  3953. kmem_cache_destroy(cfq_pool);
  3954. }
  3955. module_init(cfq_init);
  3956. module_exit(cfq_exit);
  3957. MODULE_AUTHOR("Jens Axboe");
  3958. MODULE_LICENSE("GPL");
  3959. MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");