cfq-iosched.c 108 KB

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