cfq-iosched.c 107 KB

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