cgroup.c 126 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842
  1. /*
  2. * Generic process-grouping system.
  3. *
  4. * Based originally on the cpuset system, extracted by Paul Menage
  5. * Copyright (C) 2006 Google, Inc
  6. *
  7. * Notifications support
  8. * Copyright (C) 2009 Nokia Corporation
  9. * Author: Kirill A. Shutemov
  10. *
  11. * Copyright notices from the original cpuset code:
  12. * --------------------------------------------------
  13. * Copyright (C) 2003 BULL SA.
  14. * Copyright (C) 2004-2006 Silicon Graphics, Inc.
  15. *
  16. * Portions derived from Patrick Mochel's sysfs code.
  17. * sysfs is Copyright (c) 2001-3 Patrick Mochel
  18. *
  19. * 2003-10-10 Written by Simon Derr.
  20. * 2003-10-22 Updates by Stephen Hemminger.
  21. * 2004 May-July Rework by Paul Jackson.
  22. * ---------------------------------------------------
  23. *
  24. * This file is subject to the terms and conditions of the GNU General Public
  25. * License. See the file COPYING in the main directory of the Linux
  26. * distribution for more details.
  27. */
  28. #include <linux/cgroup.h>
  29. #include <linux/ctype.h>
  30. #include <linux/errno.h>
  31. #include <linux/fs.h>
  32. #include <linux/kernel.h>
  33. #include <linux/list.h>
  34. #include <linux/mm.h>
  35. #include <linux/mutex.h>
  36. #include <linux/mount.h>
  37. #include <linux/pagemap.h>
  38. #include <linux/proc_fs.h>
  39. #include <linux/rcupdate.h>
  40. #include <linux/sched.h>
  41. #include <linux/backing-dev.h>
  42. #include <linux/seq_file.h>
  43. #include <linux/slab.h>
  44. #include <linux/magic.h>
  45. #include <linux/spinlock.h>
  46. #include <linux/string.h>
  47. #include <linux/sort.h>
  48. #include <linux/kmod.h>
  49. #include <linux/module.h>
  50. #include <linux/delayacct.h>
  51. #include <linux/cgroupstats.h>
  52. #include <linux/hash.h>
  53. #include <linux/namei.h>
  54. #include <linux/smp_lock.h>
  55. #include <linux/pid_namespace.h>
  56. #include <linux/idr.h>
  57. #include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
  58. #include <linux/eventfd.h>
  59. #include <linux/poll.h>
  60. #include <asm/atomic.h>
  61. static DEFINE_MUTEX(cgroup_mutex);
  62. /*
  63. * Generate an array of cgroup subsystem pointers. At boot time, this is
  64. * populated up to CGROUP_BUILTIN_SUBSYS_COUNT, and modular subsystems are
  65. * registered after that. The mutable section of this array is protected by
  66. * cgroup_mutex.
  67. */
  68. #define SUBSYS(_x) &_x ## _subsys,
  69. static struct cgroup_subsys *subsys[CGROUP_SUBSYS_COUNT] = {
  70. #include <linux/cgroup_subsys.h>
  71. };
  72. #define MAX_CGROUP_ROOT_NAMELEN 64
  73. /*
  74. * A cgroupfs_root represents the root of a cgroup hierarchy,
  75. * and may be associated with a superblock to form an active
  76. * hierarchy
  77. */
  78. struct cgroupfs_root {
  79. struct super_block *sb;
  80. /*
  81. * The bitmask of subsystems intended to be attached to this
  82. * hierarchy
  83. */
  84. unsigned long subsys_bits;
  85. /* Unique id for this hierarchy. */
  86. int hierarchy_id;
  87. /* The bitmask of subsystems currently attached to this hierarchy */
  88. unsigned long actual_subsys_bits;
  89. /* A list running through the attached subsystems */
  90. struct list_head subsys_list;
  91. /* The root cgroup for this hierarchy */
  92. struct cgroup top_cgroup;
  93. /* Tracks how many cgroups are currently defined in hierarchy.*/
  94. int number_of_cgroups;
  95. /* A list running through the active hierarchies */
  96. struct list_head root_list;
  97. /* Hierarchy-specific flags */
  98. unsigned long flags;
  99. /* The path to use for release notifications. */
  100. char release_agent_path[PATH_MAX];
  101. /* The name for this hierarchy - may be empty */
  102. char name[MAX_CGROUP_ROOT_NAMELEN];
  103. };
  104. /*
  105. * The "rootnode" hierarchy is the "dummy hierarchy", reserved for the
  106. * subsystems that are otherwise unattached - it never has more than a
  107. * single cgroup, and all tasks are part of that cgroup.
  108. */
  109. static struct cgroupfs_root rootnode;
  110. /*
  111. * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when
  112. * cgroup_subsys->use_id != 0.
  113. */
  114. #define CSS_ID_MAX (65535)
  115. struct css_id {
  116. /*
  117. * The css to which this ID points. This pointer is set to valid value
  118. * after cgroup is populated. If cgroup is removed, this will be NULL.
  119. * This pointer is expected to be RCU-safe because destroy()
  120. * is called after synchronize_rcu(). But for safe use, css_is_removed()
  121. * css_tryget() should be used for avoiding race.
  122. */
  123. struct cgroup_subsys_state *css;
  124. /*
  125. * ID of this css.
  126. */
  127. unsigned short id;
  128. /*
  129. * Depth in hierarchy which this ID belongs to.
  130. */
  131. unsigned short depth;
  132. /*
  133. * ID is freed by RCU. (and lookup routine is RCU safe.)
  134. */
  135. struct rcu_head rcu_head;
  136. /*
  137. * Hierarchy of CSS ID belongs to.
  138. */
  139. unsigned short stack[0]; /* Array of Length (depth+1) */
  140. };
  141. /*
  142. * cgroup_event represents events which userspace want to recieve.
  143. */
  144. struct cgroup_event {
  145. /*
  146. * Cgroup which the event belongs to.
  147. */
  148. struct cgroup *cgrp;
  149. /*
  150. * Control file which the event associated.
  151. */
  152. struct cftype *cft;
  153. /*
  154. * eventfd to signal userspace about the event.
  155. */
  156. struct eventfd_ctx *eventfd;
  157. /*
  158. * Each of these stored in a list by the cgroup.
  159. */
  160. struct list_head list;
  161. /*
  162. * All fields below needed to unregister event when
  163. * userspace closes eventfd.
  164. */
  165. poll_table pt;
  166. wait_queue_head_t *wqh;
  167. wait_queue_t wait;
  168. struct work_struct remove;
  169. };
  170. /* The list of hierarchy roots */
  171. static LIST_HEAD(roots);
  172. static int root_count;
  173. static DEFINE_IDA(hierarchy_ida);
  174. static int next_hierarchy_id;
  175. static DEFINE_SPINLOCK(hierarchy_id_lock);
  176. /* dummytop is a shorthand for the dummy hierarchy's top cgroup */
  177. #define dummytop (&rootnode.top_cgroup)
  178. /* This flag indicates whether tasks in the fork and exit paths should
  179. * check for fork/exit handlers to call. This avoids us having to do
  180. * extra work in the fork/exit path if none of the subsystems need to
  181. * be called.
  182. */
  183. static int need_forkexit_callback __read_mostly;
  184. #ifdef CONFIG_PROVE_LOCKING
  185. int cgroup_lock_is_held(void)
  186. {
  187. return lockdep_is_held(&cgroup_mutex);
  188. }
  189. #else /* #ifdef CONFIG_PROVE_LOCKING */
  190. int cgroup_lock_is_held(void)
  191. {
  192. return mutex_is_locked(&cgroup_mutex);
  193. }
  194. #endif /* #else #ifdef CONFIG_PROVE_LOCKING */
  195. EXPORT_SYMBOL_GPL(cgroup_lock_is_held);
  196. /* convenient tests for these bits */
  197. inline int cgroup_is_removed(const struct cgroup *cgrp)
  198. {
  199. return test_bit(CGRP_REMOVED, &cgrp->flags);
  200. }
  201. /* bits in struct cgroupfs_root flags field */
  202. enum {
  203. ROOT_NOPREFIX, /* mounted subsystems have no named prefix */
  204. };
  205. static int cgroup_is_releasable(const struct cgroup *cgrp)
  206. {
  207. const int bits =
  208. (1 << CGRP_RELEASABLE) |
  209. (1 << CGRP_NOTIFY_ON_RELEASE);
  210. return (cgrp->flags & bits) == bits;
  211. }
  212. static int notify_on_release(const struct cgroup *cgrp)
  213. {
  214. return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
  215. }
  216. /*
  217. * for_each_subsys() allows you to iterate on each subsystem attached to
  218. * an active hierarchy
  219. */
  220. #define for_each_subsys(_root, _ss) \
  221. list_for_each_entry(_ss, &_root->subsys_list, sibling)
  222. /* for_each_active_root() allows you to iterate across the active hierarchies */
  223. #define for_each_active_root(_root) \
  224. list_for_each_entry(_root, &roots, root_list)
  225. /* the list of cgroups eligible for automatic release. Protected by
  226. * release_list_lock */
  227. static LIST_HEAD(release_list);
  228. static DEFINE_SPINLOCK(release_list_lock);
  229. static void cgroup_release_agent(struct work_struct *work);
  230. static DECLARE_WORK(release_agent_work, cgroup_release_agent);
  231. static void check_for_release(struct cgroup *cgrp);
  232. /* Link structure for associating css_set objects with cgroups */
  233. struct cg_cgroup_link {
  234. /*
  235. * List running through cg_cgroup_links associated with a
  236. * cgroup, anchored on cgroup->css_sets
  237. */
  238. struct list_head cgrp_link_list;
  239. struct cgroup *cgrp;
  240. /*
  241. * List running through cg_cgroup_links pointing at a
  242. * single css_set object, anchored on css_set->cg_links
  243. */
  244. struct list_head cg_link_list;
  245. struct css_set *cg;
  246. };
  247. /* The default css_set - used by init and its children prior to any
  248. * hierarchies being mounted. It contains a pointer to the root state
  249. * for each subsystem. Also used to anchor the list of css_sets. Not
  250. * reference-counted, to improve performance when child cgroups
  251. * haven't been created.
  252. */
  253. static struct css_set init_css_set;
  254. static struct cg_cgroup_link init_css_set_link;
  255. static int cgroup_init_idr(struct cgroup_subsys *ss,
  256. struct cgroup_subsys_state *css);
  257. /* css_set_lock protects the list of css_set objects, and the
  258. * chain of tasks off each css_set. Nests outside task->alloc_lock
  259. * due to cgroup_iter_start() */
  260. static DEFINE_RWLOCK(css_set_lock);
  261. static int css_set_count;
  262. /*
  263. * hash table for cgroup groups. This improves the performance to find
  264. * an existing css_set. This hash doesn't (currently) take into
  265. * account cgroups in empty hierarchies.
  266. */
  267. #define CSS_SET_HASH_BITS 7
  268. #define CSS_SET_TABLE_SIZE (1 << CSS_SET_HASH_BITS)
  269. static struct hlist_head css_set_table[CSS_SET_TABLE_SIZE];
  270. static struct hlist_head *css_set_hash(struct cgroup_subsys_state *css[])
  271. {
  272. int i;
  273. int index;
  274. unsigned long tmp = 0UL;
  275. for (i = 0; i < CGROUP_SUBSYS_COUNT; i++)
  276. tmp += (unsigned long)css[i];
  277. tmp = (tmp >> 16) ^ tmp;
  278. index = hash_long(tmp, CSS_SET_HASH_BITS);
  279. return &css_set_table[index];
  280. }
  281. static void free_css_set_rcu(struct rcu_head *obj)
  282. {
  283. struct css_set *cg = container_of(obj, struct css_set, rcu_head);
  284. kfree(cg);
  285. }
  286. /* We don't maintain the lists running through each css_set to its
  287. * task until after the first call to cgroup_iter_start(). This
  288. * reduces the fork()/exit() overhead for people who have cgroups
  289. * compiled into their kernel but not actually in use */
  290. static int use_task_css_set_links __read_mostly;
  291. static void __put_css_set(struct css_set *cg, int taskexit)
  292. {
  293. struct cg_cgroup_link *link;
  294. struct cg_cgroup_link *saved_link;
  295. /*
  296. * Ensure that the refcount doesn't hit zero while any readers
  297. * can see it. Similar to atomic_dec_and_lock(), but for an
  298. * rwlock
  299. */
  300. if (atomic_add_unless(&cg->refcount, -1, 1))
  301. return;
  302. write_lock(&css_set_lock);
  303. if (!atomic_dec_and_test(&cg->refcount)) {
  304. write_unlock(&css_set_lock);
  305. return;
  306. }
  307. /* This css_set is dead. unlink it and release cgroup refcounts */
  308. hlist_del(&cg->hlist);
  309. css_set_count--;
  310. list_for_each_entry_safe(link, saved_link, &cg->cg_links,
  311. cg_link_list) {
  312. struct cgroup *cgrp = link->cgrp;
  313. list_del(&link->cg_link_list);
  314. list_del(&link->cgrp_link_list);
  315. if (atomic_dec_and_test(&cgrp->count) &&
  316. notify_on_release(cgrp)) {
  317. if (taskexit)
  318. set_bit(CGRP_RELEASABLE, &cgrp->flags);
  319. check_for_release(cgrp);
  320. }
  321. kfree(link);
  322. }
  323. write_unlock(&css_set_lock);
  324. call_rcu(&cg->rcu_head, free_css_set_rcu);
  325. }
  326. /*
  327. * refcounted get/put for css_set objects
  328. */
  329. static inline void get_css_set(struct css_set *cg)
  330. {
  331. atomic_inc(&cg->refcount);
  332. }
  333. static inline void put_css_set(struct css_set *cg)
  334. {
  335. __put_css_set(cg, 0);
  336. }
  337. static inline void put_css_set_taskexit(struct css_set *cg)
  338. {
  339. __put_css_set(cg, 1);
  340. }
  341. /*
  342. * compare_css_sets - helper function for find_existing_css_set().
  343. * @cg: candidate css_set being tested
  344. * @old_cg: existing css_set for a task
  345. * @new_cgrp: cgroup that's being entered by the task
  346. * @template: desired set of css pointers in css_set (pre-calculated)
  347. *
  348. * Returns true if "cg" matches "old_cg" except for the hierarchy
  349. * which "new_cgrp" belongs to, for which it should match "new_cgrp".
  350. */
  351. static bool compare_css_sets(struct css_set *cg,
  352. struct css_set *old_cg,
  353. struct cgroup *new_cgrp,
  354. struct cgroup_subsys_state *template[])
  355. {
  356. struct list_head *l1, *l2;
  357. if (memcmp(template, cg->subsys, sizeof(cg->subsys))) {
  358. /* Not all subsystems matched */
  359. return false;
  360. }
  361. /*
  362. * Compare cgroup pointers in order to distinguish between
  363. * different cgroups in heirarchies with no subsystems. We
  364. * could get by with just this check alone (and skip the
  365. * memcmp above) but on most setups the memcmp check will
  366. * avoid the need for this more expensive check on almost all
  367. * candidates.
  368. */
  369. l1 = &cg->cg_links;
  370. l2 = &old_cg->cg_links;
  371. while (1) {
  372. struct cg_cgroup_link *cgl1, *cgl2;
  373. struct cgroup *cg1, *cg2;
  374. l1 = l1->next;
  375. l2 = l2->next;
  376. /* See if we reached the end - both lists are equal length. */
  377. if (l1 == &cg->cg_links) {
  378. BUG_ON(l2 != &old_cg->cg_links);
  379. break;
  380. } else {
  381. BUG_ON(l2 == &old_cg->cg_links);
  382. }
  383. /* Locate the cgroups associated with these links. */
  384. cgl1 = list_entry(l1, struct cg_cgroup_link, cg_link_list);
  385. cgl2 = list_entry(l2, struct cg_cgroup_link, cg_link_list);
  386. cg1 = cgl1->cgrp;
  387. cg2 = cgl2->cgrp;
  388. /* Hierarchies should be linked in the same order. */
  389. BUG_ON(cg1->root != cg2->root);
  390. /*
  391. * If this hierarchy is the hierarchy of the cgroup
  392. * that's changing, then we need to check that this
  393. * css_set points to the new cgroup; if it's any other
  394. * hierarchy, then this css_set should point to the
  395. * same cgroup as the old css_set.
  396. */
  397. if (cg1->root == new_cgrp->root) {
  398. if (cg1 != new_cgrp)
  399. return false;
  400. } else {
  401. if (cg1 != cg2)
  402. return false;
  403. }
  404. }
  405. return true;
  406. }
  407. /*
  408. * find_existing_css_set() is a helper for
  409. * find_css_set(), and checks to see whether an existing
  410. * css_set is suitable.
  411. *
  412. * oldcg: the cgroup group that we're using before the cgroup
  413. * transition
  414. *
  415. * cgrp: the cgroup that we're moving into
  416. *
  417. * template: location in which to build the desired set of subsystem
  418. * state objects for the new cgroup group
  419. */
  420. static struct css_set *find_existing_css_set(
  421. struct css_set *oldcg,
  422. struct cgroup *cgrp,
  423. struct cgroup_subsys_state *template[])
  424. {
  425. int i;
  426. struct cgroupfs_root *root = cgrp->root;
  427. struct hlist_head *hhead;
  428. struct hlist_node *node;
  429. struct css_set *cg;
  430. /*
  431. * Build the set of subsystem state objects that we want to see in the
  432. * new css_set. while subsystems can change globally, the entries here
  433. * won't change, so no need for locking.
  434. */
  435. for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
  436. if (root->subsys_bits & (1UL << i)) {
  437. /* Subsystem is in this hierarchy. So we want
  438. * the subsystem state from the new
  439. * cgroup */
  440. template[i] = cgrp->subsys[i];
  441. } else {
  442. /* Subsystem is not in this hierarchy, so we
  443. * don't want to change the subsystem state */
  444. template[i] = oldcg->subsys[i];
  445. }
  446. }
  447. hhead = css_set_hash(template);
  448. hlist_for_each_entry(cg, node, hhead, hlist) {
  449. if (!compare_css_sets(cg, oldcg, cgrp, template))
  450. continue;
  451. /* This css_set matches what we need */
  452. return cg;
  453. }
  454. /* No existing cgroup group matched */
  455. return NULL;
  456. }
  457. static void free_cg_links(struct list_head *tmp)
  458. {
  459. struct cg_cgroup_link *link;
  460. struct cg_cgroup_link *saved_link;
  461. list_for_each_entry_safe(link, saved_link, tmp, cgrp_link_list) {
  462. list_del(&link->cgrp_link_list);
  463. kfree(link);
  464. }
  465. }
  466. /*
  467. * allocate_cg_links() allocates "count" cg_cgroup_link structures
  468. * and chains them on tmp through their cgrp_link_list fields. Returns 0 on
  469. * success or a negative error
  470. */
  471. static int allocate_cg_links(int count, struct list_head *tmp)
  472. {
  473. struct cg_cgroup_link *link;
  474. int i;
  475. INIT_LIST_HEAD(tmp);
  476. for (i = 0; i < count; i++) {
  477. link = kmalloc(sizeof(*link), GFP_KERNEL);
  478. if (!link) {
  479. free_cg_links(tmp);
  480. return -ENOMEM;
  481. }
  482. list_add(&link->cgrp_link_list, tmp);
  483. }
  484. return 0;
  485. }
  486. /**
  487. * link_css_set - a helper function to link a css_set to a cgroup
  488. * @tmp_cg_links: cg_cgroup_link objects allocated by allocate_cg_links()
  489. * @cg: the css_set to be linked
  490. * @cgrp: the destination cgroup
  491. */
  492. static void link_css_set(struct list_head *tmp_cg_links,
  493. struct css_set *cg, struct cgroup *cgrp)
  494. {
  495. struct cg_cgroup_link *link;
  496. BUG_ON(list_empty(tmp_cg_links));
  497. link = list_first_entry(tmp_cg_links, struct cg_cgroup_link,
  498. cgrp_link_list);
  499. link->cg = cg;
  500. link->cgrp = cgrp;
  501. atomic_inc(&cgrp->count);
  502. list_move(&link->cgrp_link_list, &cgrp->css_sets);
  503. /*
  504. * Always add links to the tail of the list so that the list
  505. * is sorted by order of hierarchy creation
  506. */
  507. list_add_tail(&link->cg_link_list, &cg->cg_links);
  508. }
  509. /*
  510. * find_css_set() takes an existing cgroup group and a
  511. * cgroup object, and returns a css_set object that's
  512. * equivalent to the old group, but with the given cgroup
  513. * substituted into the appropriate hierarchy. Must be called with
  514. * cgroup_mutex held
  515. */
  516. static struct css_set *find_css_set(
  517. struct css_set *oldcg, struct cgroup *cgrp)
  518. {
  519. struct css_set *res;
  520. struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT];
  521. struct list_head tmp_cg_links;
  522. struct hlist_head *hhead;
  523. struct cg_cgroup_link *link;
  524. /* First see if we already have a cgroup group that matches
  525. * the desired set */
  526. read_lock(&css_set_lock);
  527. res = find_existing_css_set(oldcg, cgrp, template);
  528. if (res)
  529. get_css_set(res);
  530. read_unlock(&css_set_lock);
  531. if (res)
  532. return res;
  533. res = kmalloc(sizeof(*res), GFP_KERNEL);
  534. if (!res)
  535. return NULL;
  536. /* Allocate all the cg_cgroup_link objects that we'll need */
  537. if (allocate_cg_links(root_count, &tmp_cg_links) < 0) {
  538. kfree(res);
  539. return NULL;
  540. }
  541. atomic_set(&res->refcount, 1);
  542. INIT_LIST_HEAD(&res->cg_links);
  543. INIT_LIST_HEAD(&res->tasks);
  544. INIT_HLIST_NODE(&res->hlist);
  545. /* Copy the set of subsystem state objects generated in
  546. * find_existing_css_set() */
  547. memcpy(res->subsys, template, sizeof(res->subsys));
  548. write_lock(&css_set_lock);
  549. /* Add reference counts and links from the new css_set. */
  550. list_for_each_entry(link, &oldcg->cg_links, cg_link_list) {
  551. struct cgroup *c = link->cgrp;
  552. if (c->root == cgrp->root)
  553. c = cgrp;
  554. link_css_set(&tmp_cg_links, res, c);
  555. }
  556. BUG_ON(!list_empty(&tmp_cg_links));
  557. css_set_count++;
  558. /* Add this cgroup group to the hash table */
  559. hhead = css_set_hash(res->subsys);
  560. hlist_add_head(&res->hlist, hhead);
  561. write_unlock(&css_set_lock);
  562. return res;
  563. }
  564. /*
  565. * Return the cgroup for "task" from the given hierarchy. Must be
  566. * called with cgroup_mutex held.
  567. */
  568. static struct cgroup *task_cgroup_from_root(struct task_struct *task,
  569. struct cgroupfs_root *root)
  570. {
  571. struct css_set *css;
  572. struct cgroup *res = NULL;
  573. BUG_ON(!mutex_is_locked(&cgroup_mutex));
  574. read_lock(&css_set_lock);
  575. /*
  576. * No need to lock the task - since we hold cgroup_mutex the
  577. * task can't change groups, so the only thing that can happen
  578. * is that it exits and its css is set back to init_css_set.
  579. */
  580. css = task->cgroups;
  581. if (css == &init_css_set) {
  582. res = &root->top_cgroup;
  583. } else {
  584. struct cg_cgroup_link *link;
  585. list_for_each_entry(link, &css->cg_links, cg_link_list) {
  586. struct cgroup *c = link->cgrp;
  587. if (c->root == root) {
  588. res = c;
  589. break;
  590. }
  591. }
  592. }
  593. read_unlock(&css_set_lock);
  594. BUG_ON(!res);
  595. return res;
  596. }
  597. /*
  598. * There is one global cgroup mutex. We also require taking
  599. * task_lock() when dereferencing a task's cgroup subsys pointers.
  600. * See "The task_lock() exception", at the end of this comment.
  601. *
  602. * A task must hold cgroup_mutex to modify cgroups.
  603. *
  604. * Any task can increment and decrement the count field without lock.
  605. * So in general, code holding cgroup_mutex can't rely on the count
  606. * field not changing. However, if the count goes to zero, then only
  607. * cgroup_attach_task() can increment it again. Because a count of zero
  608. * means that no tasks are currently attached, therefore there is no
  609. * way a task attached to that cgroup can fork (the other way to
  610. * increment the count). So code holding cgroup_mutex can safely
  611. * assume that if the count is zero, it will stay zero. Similarly, if
  612. * a task holds cgroup_mutex on a cgroup with zero count, it
  613. * knows that the cgroup won't be removed, as cgroup_rmdir()
  614. * needs that mutex.
  615. *
  616. * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
  617. * (usually) take cgroup_mutex. These are the two most performance
  618. * critical pieces of code here. The exception occurs on cgroup_exit(),
  619. * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
  620. * is taken, and if the cgroup count is zero, a usermode call made
  621. * to the release agent with the name of the cgroup (path relative to
  622. * the root of cgroup file system) as the argument.
  623. *
  624. * A cgroup can only be deleted if both its 'count' of using tasks
  625. * is zero, and its list of 'children' cgroups is empty. Since all
  626. * tasks in the system use _some_ cgroup, and since there is always at
  627. * least one task in the system (init, pid == 1), therefore, top_cgroup
  628. * always has either children cgroups and/or using tasks. So we don't
  629. * need a special hack to ensure that top_cgroup cannot be deleted.
  630. *
  631. * The task_lock() exception
  632. *
  633. * The need for this exception arises from the action of
  634. * cgroup_attach_task(), which overwrites one tasks cgroup pointer with
  635. * another. It does so using cgroup_mutex, however there are
  636. * several performance critical places that need to reference
  637. * task->cgroup without the expense of grabbing a system global
  638. * mutex. Therefore except as noted below, when dereferencing or, as
  639. * in cgroup_attach_task(), modifying a task'ss cgroup pointer we use
  640. * task_lock(), which acts on a spinlock (task->alloc_lock) already in
  641. * the task_struct routinely used for such matters.
  642. *
  643. * P.S. One more locking exception. RCU is used to guard the
  644. * update of a tasks cgroup pointer by cgroup_attach_task()
  645. */
  646. /**
  647. * cgroup_lock - lock out any changes to cgroup structures
  648. *
  649. */
  650. void cgroup_lock(void)
  651. {
  652. mutex_lock(&cgroup_mutex);
  653. }
  654. EXPORT_SYMBOL_GPL(cgroup_lock);
  655. /**
  656. * cgroup_unlock - release lock on cgroup changes
  657. *
  658. * Undo the lock taken in a previous cgroup_lock() call.
  659. */
  660. void cgroup_unlock(void)
  661. {
  662. mutex_unlock(&cgroup_mutex);
  663. }
  664. EXPORT_SYMBOL_GPL(cgroup_unlock);
  665. /*
  666. * A couple of forward declarations required, due to cyclic reference loop:
  667. * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
  668. * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
  669. * -> cgroup_mkdir.
  670. */
  671. static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, int mode);
  672. static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
  673. static int cgroup_populate_dir(struct cgroup *cgrp);
  674. static const struct inode_operations cgroup_dir_inode_operations;
  675. static const struct file_operations proc_cgroupstats_operations;
  676. static struct backing_dev_info cgroup_backing_dev_info = {
  677. .name = "cgroup",
  678. .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
  679. };
  680. static int alloc_css_id(struct cgroup_subsys *ss,
  681. struct cgroup *parent, struct cgroup *child);
  682. static struct inode *cgroup_new_inode(mode_t mode, struct super_block *sb)
  683. {
  684. struct inode *inode = new_inode(sb);
  685. if (inode) {
  686. inode->i_mode = mode;
  687. inode->i_uid = current_fsuid();
  688. inode->i_gid = current_fsgid();
  689. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  690. inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
  691. }
  692. return inode;
  693. }
  694. /*
  695. * Call subsys's pre_destroy handler.
  696. * This is called before css refcnt check.
  697. */
  698. static int cgroup_call_pre_destroy(struct cgroup *cgrp)
  699. {
  700. struct cgroup_subsys *ss;
  701. int ret = 0;
  702. for_each_subsys(cgrp->root, ss)
  703. if (ss->pre_destroy) {
  704. ret = ss->pre_destroy(ss, cgrp);
  705. if (ret)
  706. break;
  707. }
  708. return ret;
  709. }
  710. static void free_cgroup_rcu(struct rcu_head *obj)
  711. {
  712. struct cgroup *cgrp = container_of(obj, struct cgroup, rcu_head);
  713. kfree(cgrp);
  714. }
  715. static void cgroup_diput(struct dentry *dentry, struct inode *inode)
  716. {
  717. /* is dentry a directory ? if so, kfree() associated cgroup */
  718. if (S_ISDIR(inode->i_mode)) {
  719. struct cgroup *cgrp = dentry->d_fsdata;
  720. struct cgroup_subsys *ss;
  721. BUG_ON(!(cgroup_is_removed(cgrp)));
  722. /* It's possible for external users to be holding css
  723. * reference counts on a cgroup; css_put() needs to
  724. * be able to access the cgroup after decrementing
  725. * the reference count in order to know if it needs to
  726. * queue the cgroup to be handled by the release
  727. * agent */
  728. synchronize_rcu();
  729. mutex_lock(&cgroup_mutex);
  730. /*
  731. * Release the subsystem state objects.
  732. */
  733. for_each_subsys(cgrp->root, ss)
  734. ss->destroy(ss, cgrp);
  735. cgrp->root->number_of_cgroups--;
  736. mutex_unlock(&cgroup_mutex);
  737. /*
  738. * Drop the active superblock reference that we took when we
  739. * created the cgroup
  740. */
  741. deactivate_super(cgrp->root->sb);
  742. /*
  743. * if we're getting rid of the cgroup, refcount should ensure
  744. * that there are no pidlists left.
  745. */
  746. BUG_ON(!list_empty(&cgrp->pidlists));
  747. call_rcu(&cgrp->rcu_head, free_cgroup_rcu);
  748. }
  749. iput(inode);
  750. }
  751. static void remove_dir(struct dentry *d)
  752. {
  753. struct dentry *parent = dget(d->d_parent);
  754. d_delete(d);
  755. simple_rmdir(parent->d_inode, d);
  756. dput(parent);
  757. }
  758. static void cgroup_clear_directory(struct dentry *dentry)
  759. {
  760. struct list_head *node;
  761. BUG_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
  762. spin_lock(&dcache_lock);
  763. node = dentry->d_subdirs.next;
  764. while (node != &dentry->d_subdirs) {
  765. struct dentry *d = list_entry(node, struct dentry, d_u.d_child);
  766. list_del_init(node);
  767. if (d->d_inode) {
  768. /* This should never be called on a cgroup
  769. * directory with child cgroups */
  770. BUG_ON(d->d_inode->i_mode & S_IFDIR);
  771. d = dget_locked(d);
  772. spin_unlock(&dcache_lock);
  773. d_delete(d);
  774. simple_unlink(dentry->d_inode, d);
  775. dput(d);
  776. spin_lock(&dcache_lock);
  777. }
  778. node = dentry->d_subdirs.next;
  779. }
  780. spin_unlock(&dcache_lock);
  781. }
  782. /*
  783. * NOTE : the dentry must have been dget()'ed
  784. */
  785. static void cgroup_d_remove_dir(struct dentry *dentry)
  786. {
  787. cgroup_clear_directory(dentry);
  788. spin_lock(&dcache_lock);
  789. list_del_init(&dentry->d_u.d_child);
  790. spin_unlock(&dcache_lock);
  791. remove_dir(dentry);
  792. }
  793. /*
  794. * A queue for waiters to do rmdir() cgroup. A tasks will sleep when
  795. * cgroup->count == 0 && list_empty(&cgroup->children) && subsys has some
  796. * reference to css->refcnt. In general, this refcnt is expected to goes down
  797. * to zero, soon.
  798. *
  799. * CGRP_WAIT_ON_RMDIR flag is set under cgroup's inode->i_mutex;
  800. */
  801. DECLARE_WAIT_QUEUE_HEAD(cgroup_rmdir_waitq);
  802. static void cgroup_wakeup_rmdir_waiter(struct cgroup *cgrp)
  803. {
  804. if (unlikely(test_and_clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags)))
  805. wake_up_all(&cgroup_rmdir_waitq);
  806. }
  807. void cgroup_exclude_rmdir(struct cgroup_subsys_state *css)
  808. {
  809. css_get(css);
  810. }
  811. void cgroup_release_and_wakeup_rmdir(struct cgroup_subsys_state *css)
  812. {
  813. cgroup_wakeup_rmdir_waiter(css->cgroup);
  814. css_put(css);
  815. }
  816. /*
  817. * Call with cgroup_mutex held. Drops reference counts on modules, including
  818. * any duplicate ones that parse_cgroupfs_options took. If this function
  819. * returns an error, no reference counts are touched.
  820. */
  821. static int rebind_subsystems(struct cgroupfs_root *root,
  822. unsigned long final_bits)
  823. {
  824. unsigned long added_bits, removed_bits;
  825. struct cgroup *cgrp = &root->top_cgroup;
  826. int i;
  827. BUG_ON(!mutex_is_locked(&cgroup_mutex));
  828. removed_bits = root->actual_subsys_bits & ~final_bits;
  829. added_bits = final_bits & ~root->actual_subsys_bits;
  830. /* Check that any added subsystems are currently free */
  831. for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
  832. unsigned long bit = 1UL << i;
  833. struct cgroup_subsys *ss = subsys[i];
  834. if (!(bit & added_bits))
  835. continue;
  836. /*
  837. * Nobody should tell us to do a subsys that doesn't exist:
  838. * parse_cgroupfs_options should catch that case and refcounts
  839. * ensure that subsystems won't disappear once selected.
  840. */
  841. BUG_ON(ss == NULL);
  842. if (ss->root != &rootnode) {
  843. /* Subsystem isn't free */
  844. return -EBUSY;
  845. }
  846. }
  847. /* Currently we don't handle adding/removing subsystems when
  848. * any child cgroups exist. This is theoretically supportable
  849. * but involves complex error handling, so it's being left until
  850. * later */
  851. if (root->number_of_cgroups > 1)
  852. return -EBUSY;
  853. /* Process each subsystem */
  854. for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
  855. struct cgroup_subsys *ss = subsys[i];
  856. unsigned long bit = 1UL << i;
  857. if (bit & added_bits) {
  858. /* We're binding this subsystem to this hierarchy */
  859. BUG_ON(ss == NULL);
  860. BUG_ON(cgrp->subsys[i]);
  861. BUG_ON(!dummytop->subsys[i]);
  862. BUG_ON(dummytop->subsys[i]->cgroup != dummytop);
  863. mutex_lock(&ss->hierarchy_mutex);
  864. cgrp->subsys[i] = dummytop->subsys[i];
  865. cgrp->subsys[i]->cgroup = cgrp;
  866. list_move(&ss->sibling, &root->subsys_list);
  867. ss->root = root;
  868. if (ss->bind)
  869. ss->bind(ss, cgrp);
  870. mutex_unlock(&ss->hierarchy_mutex);
  871. /* refcount was already taken, and we're keeping it */
  872. } else if (bit & removed_bits) {
  873. /* We're removing this subsystem */
  874. BUG_ON(ss == NULL);
  875. BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]);
  876. BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
  877. mutex_lock(&ss->hierarchy_mutex);
  878. if (ss->bind)
  879. ss->bind(ss, dummytop);
  880. dummytop->subsys[i]->cgroup = dummytop;
  881. cgrp->subsys[i] = NULL;
  882. subsys[i]->root = &rootnode;
  883. list_move(&ss->sibling, &rootnode.subsys_list);
  884. mutex_unlock(&ss->hierarchy_mutex);
  885. /* subsystem is now free - drop reference on module */
  886. module_put(ss->module);
  887. } else if (bit & final_bits) {
  888. /* Subsystem state should already exist */
  889. BUG_ON(ss == NULL);
  890. BUG_ON(!cgrp->subsys[i]);
  891. /*
  892. * a refcount was taken, but we already had one, so
  893. * drop the extra reference.
  894. */
  895. module_put(ss->module);
  896. #ifdef CONFIG_MODULE_UNLOAD
  897. BUG_ON(ss->module && !module_refcount(ss->module));
  898. #endif
  899. } else {
  900. /* Subsystem state shouldn't exist */
  901. BUG_ON(cgrp->subsys[i]);
  902. }
  903. }
  904. root->subsys_bits = root->actual_subsys_bits = final_bits;
  905. synchronize_rcu();
  906. return 0;
  907. }
  908. static int cgroup_show_options(struct seq_file *seq, struct vfsmount *vfs)
  909. {
  910. struct cgroupfs_root *root = vfs->mnt_sb->s_fs_info;
  911. struct cgroup_subsys *ss;
  912. mutex_lock(&cgroup_mutex);
  913. for_each_subsys(root, ss)
  914. seq_printf(seq, ",%s", ss->name);
  915. if (test_bit(ROOT_NOPREFIX, &root->flags))
  916. seq_puts(seq, ",noprefix");
  917. if (strlen(root->release_agent_path))
  918. seq_printf(seq, ",release_agent=%s", root->release_agent_path);
  919. if (strlen(root->name))
  920. seq_printf(seq, ",name=%s", root->name);
  921. mutex_unlock(&cgroup_mutex);
  922. return 0;
  923. }
  924. struct cgroup_sb_opts {
  925. unsigned long subsys_bits;
  926. unsigned long flags;
  927. char *release_agent;
  928. char *name;
  929. /* User explicitly requested empty subsystem */
  930. bool none;
  931. struct cgroupfs_root *new_root;
  932. };
  933. /*
  934. * Convert a hierarchy specifier into a bitmask of subsystems and flags. Call
  935. * with cgroup_mutex held to protect the subsys[] array. This function takes
  936. * refcounts on subsystems to be used, unless it returns error, in which case
  937. * no refcounts are taken.
  938. */
  939. static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
  940. {
  941. char *token, *o = data ?: "all";
  942. unsigned long mask = (unsigned long)-1;
  943. int i;
  944. bool module_pin_failed = false;
  945. BUG_ON(!mutex_is_locked(&cgroup_mutex));
  946. #ifdef CONFIG_CPUSETS
  947. mask = ~(1UL << cpuset_subsys_id);
  948. #endif
  949. memset(opts, 0, sizeof(*opts));
  950. while ((token = strsep(&o, ",")) != NULL) {
  951. if (!*token)
  952. return -EINVAL;
  953. if (!strcmp(token, "all")) {
  954. /* Add all non-disabled subsystems */
  955. opts->subsys_bits = 0;
  956. for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
  957. struct cgroup_subsys *ss = subsys[i];
  958. if (ss == NULL)
  959. continue;
  960. if (!ss->disabled)
  961. opts->subsys_bits |= 1ul << i;
  962. }
  963. } else if (!strcmp(token, "none")) {
  964. /* Explicitly have no subsystems */
  965. opts->none = true;
  966. } else if (!strcmp(token, "noprefix")) {
  967. set_bit(ROOT_NOPREFIX, &opts->flags);
  968. } else if (!strncmp(token, "release_agent=", 14)) {
  969. /* Specifying two release agents is forbidden */
  970. if (opts->release_agent)
  971. return -EINVAL;
  972. opts->release_agent =
  973. kstrndup(token + 14, PATH_MAX, GFP_KERNEL);
  974. if (!opts->release_agent)
  975. return -ENOMEM;
  976. } else if (!strncmp(token, "name=", 5)) {
  977. const char *name = token + 5;
  978. /* Can't specify an empty name */
  979. if (!strlen(name))
  980. return -EINVAL;
  981. /* Must match [\w.-]+ */
  982. for (i = 0; i < strlen(name); i++) {
  983. char c = name[i];
  984. if (isalnum(c))
  985. continue;
  986. if ((c == '.') || (c == '-') || (c == '_'))
  987. continue;
  988. return -EINVAL;
  989. }
  990. /* Specifying two names is forbidden */
  991. if (opts->name)
  992. return -EINVAL;
  993. opts->name = kstrndup(name,
  994. MAX_CGROUP_ROOT_NAMELEN,
  995. GFP_KERNEL);
  996. if (!opts->name)
  997. return -ENOMEM;
  998. } else {
  999. struct cgroup_subsys *ss;
  1000. for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
  1001. ss = subsys[i];
  1002. if (ss == NULL)
  1003. continue;
  1004. if (!strcmp(token, ss->name)) {
  1005. if (!ss->disabled)
  1006. set_bit(i, &opts->subsys_bits);
  1007. break;
  1008. }
  1009. }
  1010. if (i == CGROUP_SUBSYS_COUNT)
  1011. return -ENOENT;
  1012. }
  1013. }
  1014. /* Consistency checks */
  1015. /*
  1016. * Option noprefix was introduced just for backward compatibility
  1017. * with the old cpuset, so we allow noprefix only if mounting just
  1018. * the cpuset subsystem.
  1019. */
  1020. if (test_bit(ROOT_NOPREFIX, &opts->flags) &&
  1021. (opts->subsys_bits & mask))
  1022. return -EINVAL;
  1023. /* Can't specify "none" and some subsystems */
  1024. if (opts->subsys_bits && opts->none)
  1025. return -EINVAL;
  1026. /*
  1027. * We either have to specify by name or by subsystems. (So all
  1028. * empty hierarchies must have a name).
  1029. */
  1030. if (!opts->subsys_bits && !opts->name)
  1031. return -EINVAL;
  1032. /*
  1033. * Grab references on all the modules we'll need, so the subsystems
  1034. * don't dance around before rebind_subsystems attaches them. This may
  1035. * take duplicate reference counts on a subsystem that's already used,
  1036. * but rebind_subsystems handles this case.
  1037. */
  1038. for (i = CGROUP_BUILTIN_SUBSYS_COUNT; i < CGROUP_SUBSYS_COUNT; i++) {
  1039. unsigned long bit = 1UL << i;
  1040. if (!(bit & opts->subsys_bits))
  1041. continue;
  1042. if (!try_module_get(subsys[i]->module)) {
  1043. module_pin_failed = true;
  1044. break;
  1045. }
  1046. }
  1047. if (module_pin_failed) {
  1048. /*
  1049. * oops, one of the modules was going away. this means that we
  1050. * raced with a module_delete call, and to the user this is
  1051. * essentially a "subsystem doesn't exist" case.
  1052. */
  1053. for (i--; i >= CGROUP_BUILTIN_SUBSYS_COUNT; i--) {
  1054. /* drop refcounts only on the ones we took */
  1055. unsigned long bit = 1UL << i;
  1056. if (!(bit & opts->subsys_bits))
  1057. continue;
  1058. module_put(subsys[i]->module);
  1059. }
  1060. return -ENOENT;
  1061. }
  1062. return 0;
  1063. }
  1064. static void drop_parsed_module_refcounts(unsigned long subsys_bits)
  1065. {
  1066. int i;
  1067. for (i = CGROUP_BUILTIN_SUBSYS_COUNT; i < CGROUP_SUBSYS_COUNT; i++) {
  1068. unsigned long bit = 1UL << i;
  1069. if (!(bit & subsys_bits))
  1070. continue;
  1071. module_put(subsys[i]->module);
  1072. }
  1073. }
  1074. static int cgroup_remount(struct super_block *sb, int *flags, char *data)
  1075. {
  1076. int ret = 0;
  1077. struct cgroupfs_root *root = sb->s_fs_info;
  1078. struct cgroup *cgrp = &root->top_cgroup;
  1079. struct cgroup_sb_opts opts;
  1080. lock_kernel();
  1081. mutex_lock(&cgrp->dentry->d_inode->i_mutex);
  1082. mutex_lock(&cgroup_mutex);
  1083. /* See what subsystems are wanted */
  1084. ret = parse_cgroupfs_options(data, &opts);
  1085. if (ret)
  1086. goto out_unlock;
  1087. /* Don't allow flags or name to change at remount */
  1088. if (opts.flags != root->flags ||
  1089. (opts.name && strcmp(opts.name, root->name))) {
  1090. ret = -EINVAL;
  1091. drop_parsed_module_refcounts(opts.subsys_bits);
  1092. goto out_unlock;
  1093. }
  1094. ret = rebind_subsystems(root, opts.subsys_bits);
  1095. if (ret) {
  1096. drop_parsed_module_refcounts(opts.subsys_bits);
  1097. goto out_unlock;
  1098. }
  1099. /* (re)populate subsystem files */
  1100. cgroup_populate_dir(cgrp);
  1101. if (opts.release_agent)
  1102. strcpy(root->release_agent_path, opts.release_agent);
  1103. out_unlock:
  1104. kfree(opts.release_agent);
  1105. kfree(opts.name);
  1106. mutex_unlock(&cgroup_mutex);
  1107. mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
  1108. unlock_kernel();
  1109. return ret;
  1110. }
  1111. static const struct super_operations cgroup_ops = {
  1112. .statfs = simple_statfs,
  1113. .drop_inode = generic_delete_inode,
  1114. .show_options = cgroup_show_options,
  1115. .remount_fs = cgroup_remount,
  1116. };
  1117. static void init_cgroup_housekeeping(struct cgroup *cgrp)
  1118. {
  1119. INIT_LIST_HEAD(&cgrp->sibling);
  1120. INIT_LIST_HEAD(&cgrp->children);
  1121. INIT_LIST_HEAD(&cgrp->css_sets);
  1122. INIT_LIST_HEAD(&cgrp->release_list);
  1123. INIT_LIST_HEAD(&cgrp->pidlists);
  1124. mutex_init(&cgrp->pidlist_mutex);
  1125. INIT_LIST_HEAD(&cgrp->event_list);
  1126. spin_lock_init(&cgrp->event_list_lock);
  1127. }
  1128. static void init_cgroup_root(struct cgroupfs_root *root)
  1129. {
  1130. struct cgroup *cgrp = &root->top_cgroup;
  1131. INIT_LIST_HEAD(&root->subsys_list);
  1132. INIT_LIST_HEAD(&root->root_list);
  1133. root->number_of_cgroups = 1;
  1134. cgrp->root = root;
  1135. cgrp->top_cgroup = cgrp;
  1136. init_cgroup_housekeeping(cgrp);
  1137. }
  1138. static bool init_root_id(struct cgroupfs_root *root)
  1139. {
  1140. int ret = 0;
  1141. do {
  1142. if (!ida_pre_get(&hierarchy_ida, GFP_KERNEL))
  1143. return false;
  1144. spin_lock(&hierarchy_id_lock);
  1145. /* Try to allocate the next unused ID */
  1146. ret = ida_get_new_above(&hierarchy_ida, next_hierarchy_id,
  1147. &root->hierarchy_id);
  1148. if (ret == -ENOSPC)
  1149. /* Try again starting from 0 */
  1150. ret = ida_get_new(&hierarchy_ida, &root->hierarchy_id);
  1151. if (!ret) {
  1152. next_hierarchy_id = root->hierarchy_id + 1;
  1153. } else if (ret != -EAGAIN) {
  1154. /* Can only get here if the 31-bit IDR is full ... */
  1155. BUG_ON(ret);
  1156. }
  1157. spin_unlock(&hierarchy_id_lock);
  1158. } while (ret);
  1159. return true;
  1160. }
  1161. static int cgroup_test_super(struct super_block *sb, void *data)
  1162. {
  1163. struct cgroup_sb_opts *opts = data;
  1164. struct cgroupfs_root *root = sb->s_fs_info;
  1165. /* If we asked for a name then it must match */
  1166. if (opts->name && strcmp(opts->name, root->name))
  1167. return 0;
  1168. /*
  1169. * If we asked for subsystems (or explicitly for no
  1170. * subsystems) then they must match
  1171. */
  1172. if ((opts->subsys_bits || opts->none)
  1173. && (opts->subsys_bits != root->subsys_bits))
  1174. return 0;
  1175. return 1;
  1176. }
  1177. static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
  1178. {
  1179. struct cgroupfs_root *root;
  1180. if (!opts->subsys_bits && !opts->none)
  1181. return NULL;
  1182. root = kzalloc(sizeof(*root), GFP_KERNEL);
  1183. if (!root)
  1184. return ERR_PTR(-ENOMEM);
  1185. if (!init_root_id(root)) {
  1186. kfree(root);
  1187. return ERR_PTR(-ENOMEM);
  1188. }
  1189. init_cgroup_root(root);
  1190. root->subsys_bits = opts->subsys_bits;
  1191. root->flags = opts->flags;
  1192. if (opts->release_agent)
  1193. strcpy(root->release_agent_path, opts->release_agent);
  1194. if (opts->name)
  1195. strcpy(root->name, opts->name);
  1196. return root;
  1197. }
  1198. static void cgroup_drop_root(struct cgroupfs_root *root)
  1199. {
  1200. if (!root)
  1201. return;
  1202. BUG_ON(!root->hierarchy_id);
  1203. spin_lock(&hierarchy_id_lock);
  1204. ida_remove(&hierarchy_ida, root->hierarchy_id);
  1205. spin_unlock(&hierarchy_id_lock);
  1206. kfree(root);
  1207. }
  1208. static int cgroup_set_super(struct super_block *sb, void *data)
  1209. {
  1210. int ret;
  1211. struct cgroup_sb_opts *opts = data;
  1212. /* If we don't have a new root, we can't set up a new sb */
  1213. if (!opts->new_root)
  1214. return -EINVAL;
  1215. BUG_ON(!opts->subsys_bits && !opts->none);
  1216. ret = set_anon_super(sb, NULL);
  1217. if (ret)
  1218. return ret;
  1219. sb->s_fs_info = opts->new_root;
  1220. opts->new_root->sb = sb;
  1221. sb->s_blocksize = PAGE_CACHE_SIZE;
  1222. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  1223. sb->s_magic = CGROUP_SUPER_MAGIC;
  1224. sb->s_op = &cgroup_ops;
  1225. return 0;
  1226. }
  1227. static int cgroup_get_rootdir(struct super_block *sb)
  1228. {
  1229. struct inode *inode =
  1230. cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
  1231. struct dentry *dentry;
  1232. if (!inode)
  1233. return -ENOMEM;
  1234. inode->i_fop = &simple_dir_operations;
  1235. inode->i_op = &cgroup_dir_inode_operations;
  1236. /* directories start off with i_nlink == 2 (for "." entry) */
  1237. inc_nlink(inode);
  1238. dentry = d_alloc_root(inode);
  1239. if (!dentry) {
  1240. iput(inode);
  1241. return -ENOMEM;
  1242. }
  1243. sb->s_root = dentry;
  1244. return 0;
  1245. }
  1246. static int cgroup_get_sb(struct file_system_type *fs_type,
  1247. int flags, const char *unused_dev_name,
  1248. void *data, struct vfsmount *mnt)
  1249. {
  1250. struct cgroup_sb_opts opts;
  1251. struct cgroupfs_root *root;
  1252. int ret = 0;
  1253. struct super_block *sb;
  1254. struct cgroupfs_root *new_root;
  1255. /* First find the desired set of subsystems */
  1256. mutex_lock(&cgroup_mutex);
  1257. ret = parse_cgroupfs_options(data, &opts);
  1258. mutex_unlock(&cgroup_mutex);
  1259. if (ret)
  1260. goto out_err;
  1261. /*
  1262. * Allocate a new cgroup root. We may not need it if we're
  1263. * reusing an existing hierarchy.
  1264. */
  1265. new_root = cgroup_root_from_opts(&opts);
  1266. if (IS_ERR(new_root)) {
  1267. ret = PTR_ERR(new_root);
  1268. goto drop_modules;
  1269. }
  1270. opts.new_root = new_root;
  1271. /* Locate an existing or new sb for this hierarchy */
  1272. sb = sget(fs_type, cgroup_test_super, cgroup_set_super, &opts);
  1273. if (IS_ERR(sb)) {
  1274. ret = PTR_ERR(sb);
  1275. cgroup_drop_root(opts.new_root);
  1276. goto drop_modules;
  1277. }
  1278. root = sb->s_fs_info;
  1279. BUG_ON(!root);
  1280. if (root == opts.new_root) {
  1281. /* We used the new root structure, so this is a new hierarchy */
  1282. struct list_head tmp_cg_links;
  1283. struct cgroup *root_cgrp = &root->top_cgroup;
  1284. struct inode *inode;
  1285. struct cgroupfs_root *existing_root;
  1286. int i;
  1287. BUG_ON(sb->s_root != NULL);
  1288. ret = cgroup_get_rootdir(sb);
  1289. if (ret)
  1290. goto drop_new_super;
  1291. inode = sb->s_root->d_inode;
  1292. mutex_lock(&inode->i_mutex);
  1293. mutex_lock(&cgroup_mutex);
  1294. if (strlen(root->name)) {
  1295. /* Check for name clashes with existing mounts */
  1296. for_each_active_root(existing_root) {
  1297. if (!strcmp(existing_root->name, root->name)) {
  1298. ret = -EBUSY;
  1299. mutex_unlock(&cgroup_mutex);
  1300. mutex_unlock(&inode->i_mutex);
  1301. goto drop_new_super;
  1302. }
  1303. }
  1304. }
  1305. /*
  1306. * We're accessing css_set_count without locking
  1307. * css_set_lock here, but that's OK - it can only be
  1308. * increased by someone holding cgroup_lock, and
  1309. * that's us. The worst that can happen is that we
  1310. * have some link structures left over
  1311. */
  1312. ret = allocate_cg_links(css_set_count, &tmp_cg_links);
  1313. if (ret) {
  1314. mutex_unlock(&cgroup_mutex);
  1315. mutex_unlock(&inode->i_mutex);
  1316. goto drop_new_super;
  1317. }
  1318. ret = rebind_subsystems(root, root->subsys_bits);
  1319. if (ret == -EBUSY) {
  1320. mutex_unlock(&cgroup_mutex);
  1321. mutex_unlock(&inode->i_mutex);
  1322. free_cg_links(&tmp_cg_links);
  1323. goto drop_new_super;
  1324. }
  1325. /*
  1326. * There must be no failure case after here, since rebinding
  1327. * takes care of subsystems' refcounts, which are explicitly
  1328. * dropped in the failure exit path.
  1329. */
  1330. /* EBUSY should be the only error here */
  1331. BUG_ON(ret);
  1332. list_add(&root->root_list, &roots);
  1333. root_count++;
  1334. sb->s_root->d_fsdata = root_cgrp;
  1335. root->top_cgroup.dentry = sb->s_root;
  1336. /* Link the top cgroup in this hierarchy into all
  1337. * the css_set objects */
  1338. write_lock(&css_set_lock);
  1339. for (i = 0; i < CSS_SET_TABLE_SIZE; i++) {
  1340. struct hlist_head *hhead = &css_set_table[i];
  1341. struct hlist_node *node;
  1342. struct css_set *cg;
  1343. hlist_for_each_entry(cg, node, hhead, hlist)
  1344. link_css_set(&tmp_cg_links, cg, root_cgrp);
  1345. }
  1346. write_unlock(&css_set_lock);
  1347. free_cg_links(&tmp_cg_links);
  1348. BUG_ON(!list_empty(&root_cgrp->sibling));
  1349. BUG_ON(!list_empty(&root_cgrp->children));
  1350. BUG_ON(root->number_of_cgroups != 1);
  1351. cgroup_populate_dir(root_cgrp);
  1352. mutex_unlock(&cgroup_mutex);
  1353. mutex_unlock(&inode->i_mutex);
  1354. } else {
  1355. /*
  1356. * We re-used an existing hierarchy - the new root (if
  1357. * any) is not needed
  1358. */
  1359. cgroup_drop_root(opts.new_root);
  1360. /* no subsys rebinding, so refcounts don't change */
  1361. drop_parsed_module_refcounts(opts.subsys_bits);
  1362. }
  1363. simple_set_mnt(mnt, sb);
  1364. kfree(opts.release_agent);
  1365. kfree(opts.name);
  1366. return 0;
  1367. drop_new_super:
  1368. deactivate_locked_super(sb);
  1369. drop_modules:
  1370. drop_parsed_module_refcounts(opts.subsys_bits);
  1371. out_err:
  1372. kfree(opts.release_agent);
  1373. kfree(opts.name);
  1374. return ret;
  1375. }
  1376. static void cgroup_kill_sb(struct super_block *sb) {
  1377. struct cgroupfs_root *root = sb->s_fs_info;
  1378. struct cgroup *cgrp = &root->top_cgroup;
  1379. int ret;
  1380. struct cg_cgroup_link *link;
  1381. struct cg_cgroup_link *saved_link;
  1382. BUG_ON(!root);
  1383. BUG_ON(root->number_of_cgroups != 1);
  1384. BUG_ON(!list_empty(&cgrp->children));
  1385. BUG_ON(!list_empty(&cgrp->sibling));
  1386. mutex_lock(&cgroup_mutex);
  1387. /* Rebind all subsystems back to the default hierarchy */
  1388. ret = rebind_subsystems(root, 0);
  1389. /* Shouldn't be able to fail ... */
  1390. BUG_ON(ret);
  1391. /*
  1392. * Release all the links from css_sets to this hierarchy's
  1393. * root cgroup
  1394. */
  1395. write_lock(&css_set_lock);
  1396. list_for_each_entry_safe(link, saved_link, &cgrp->css_sets,
  1397. cgrp_link_list) {
  1398. list_del(&link->cg_link_list);
  1399. list_del(&link->cgrp_link_list);
  1400. kfree(link);
  1401. }
  1402. write_unlock(&css_set_lock);
  1403. if (!list_empty(&root->root_list)) {
  1404. list_del(&root->root_list);
  1405. root_count--;
  1406. }
  1407. mutex_unlock(&cgroup_mutex);
  1408. kill_litter_super(sb);
  1409. cgroup_drop_root(root);
  1410. }
  1411. static struct file_system_type cgroup_fs_type = {
  1412. .name = "cgroup",
  1413. .get_sb = cgroup_get_sb,
  1414. .kill_sb = cgroup_kill_sb,
  1415. };
  1416. static inline struct cgroup *__d_cgrp(struct dentry *dentry)
  1417. {
  1418. return dentry->d_fsdata;
  1419. }
  1420. static inline struct cftype *__d_cft(struct dentry *dentry)
  1421. {
  1422. return dentry->d_fsdata;
  1423. }
  1424. /**
  1425. * cgroup_path - generate the path of a cgroup
  1426. * @cgrp: the cgroup in question
  1427. * @buf: the buffer to write the path into
  1428. * @buflen: the length of the buffer
  1429. *
  1430. * Called with cgroup_mutex held or else with an RCU-protected cgroup
  1431. * reference. Writes path of cgroup into buf. Returns 0 on success,
  1432. * -errno on error.
  1433. */
  1434. int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
  1435. {
  1436. char *start;
  1437. struct dentry *dentry = rcu_dereference_check(cgrp->dentry,
  1438. rcu_read_lock_held() ||
  1439. cgroup_lock_is_held());
  1440. if (!dentry || cgrp == dummytop) {
  1441. /*
  1442. * Inactive subsystems have no dentry for their root
  1443. * cgroup
  1444. */
  1445. strcpy(buf, "/");
  1446. return 0;
  1447. }
  1448. start = buf + buflen;
  1449. *--start = '\0';
  1450. for (;;) {
  1451. int len = dentry->d_name.len;
  1452. if ((start -= len) < buf)
  1453. return -ENAMETOOLONG;
  1454. memcpy(start, dentry->d_name.name, len);
  1455. cgrp = cgrp->parent;
  1456. if (!cgrp)
  1457. break;
  1458. dentry = rcu_dereference_check(cgrp->dentry,
  1459. rcu_read_lock_held() ||
  1460. cgroup_lock_is_held());
  1461. if (!cgrp->parent)
  1462. continue;
  1463. if (--start < buf)
  1464. return -ENAMETOOLONG;
  1465. *start = '/';
  1466. }
  1467. memmove(buf, start, buf + buflen - start);
  1468. return 0;
  1469. }
  1470. EXPORT_SYMBOL_GPL(cgroup_path);
  1471. /**
  1472. * cgroup_attach_task - attach task 'tsk' to cgroup 'cgrp'
  1473. * @cgrp: the cgroup the task is attaching to
  1474. * @tsk: the task to be attached
  1475. *
  1476. * Call holding cgroup_mutex. May take task_lock of
  1477. * the task 'tsk' during call.
  1478. */
  1479. int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
  1480. {
  1481. int retval = 0;
  1482. struct cgroup_subsys *ss, *failed_ss = NULL;
  1483. struct cgroup *oldcgrp;
  1484. struct css_set *cg;
  1485. struct css_set *newcg;
  1486. struct cgroupfs_root *root = cgrp->root;
  1487. /* Nothing to do if the task is already in that cgroup */
  1488. oldcgrp = task_cgroup_from_root(tsk, root);
  1489. if (cgrp == oldcgrp)
  1490. return 0;
  1491. for_each_subsys(root, ss) {
  1492. if (ss->can_attach) {
  1493. retval = ss->can_attach(ss, cgrp, tsk, false);
  1494. if (retval) {
  1495. /*
  1496. * Remember on which subsystem the can_attach()
  1497. * failed, so that we only call cancel_attach()
  1498. * against the subsystems whose can_attach()
  1499. * succeeded. (See below)
  1500. */
  1501. failed_ss = ss;
  1502. goto out;
  1503. }
  1504. }
  1505. }
  1506. task_lock(tsk);
  1507. cg = tsk->cgroups;
  1508. get_css_set(cg);
  1509. task_unlock(tsk);
  1510. /*
  1511. * Locate or allocate a new css_set for this task,
  1512. * based on its final set of cgroups
  1513. */
  1514. newcg = find_css_set(cg, cgrp);
  1515. put_css_set(cg);
  1516. if (!newcg) {
  1517. retval = -ENOMEM;
  1518. goto out;
  1519. }
  1520. task_lock(tsk);
  1521. if (tsk->flags & PF_EXITING) {
  1522. task_unlock(tsk);
  1523. put_css_set(newcg);
  1524. retval = -ESRCH;
  1525. goto out;
  1526. }
  1527. rcu_assign_pointer(tsk->cgroups, newcg);
  1528. task_unlock(tsk);
  1529. /* Update the css_set linked lists if we're using them */
  1530. write_lock(&css_set_lock);
  1531. if (!list_empty(&tsk->cg_list)) {
  1532. list_del(&tsk->cg_list);
  1533. list_add(&tsk->cg_list, &newcg->tasks);
  1534. }
  1535. write_unlock(&css_set_lock);
  1536. for_each_subsys(root, ss) {
  1537. if (ss->attach)
  1538. ss->attach(ss, cgrp, oldcgrp, tsk, false);
  1539. }
  1540. set_bit(CGRP_RELEASABLE, &oldcgrp->flags);
  1541. synchronize_rcu();
  1542. put_css_set(cg);
  1543. /*
  1544. * wake up rmdir() waiter. the rmdir should fail since the cgroup
  1545. * is no longer empty.
  1546. */
  1547. cgroup_wakeup_rmdir_waiter(cgrp);
  1548. out:
  1549. if (retval) {
  1550. for_each_subsys(root, ss) {
  1551. if (ss == failed_ss)
  1552. /*
  1553. * This subsystem was the one that failed the
  1554. * can_attach() check earlier, so we don't need
  1555. * to call cancel_attach() against it or any
  1556. * remaining subsystems.
  1557. */
  1558. break;
  1559. if (ss->cancel_attach)
  1560. ss->cancel_attach(ss, cgrp, tsk, false);
  1561. }
  1562. }
  1563. return retval;
  1564. }
  1565. /*
  1566. * Attach task with pid 'pid' to cgroup 'cgrp'. Call with cgroup_mutex
  1567. * held. May take task_lock of task
  1568. */
  1569. static int attach_task_by_pid(struct cgroup *cgrp, u64 pid)
  1570. {
  1571. struct task_struct *tsk;
  1572. const struct cred *cred = current_cred(), *tcred;
  1573. int ret;
  1574. if (pid) {
  1575. rcu_read_lock();
  1576. tsk = find_task_by_vpid(pid);
  1577. if (!tsk || tsk->flags & PF_EXITING) {
  1578. rcu_read_unlock();
  1579. return -ESRCH;
  1580. }
  1581. tcred = __task_cred(tsk);
  1582. if (cred->euid &&
  1583. cred->euid != tcred->uid &&
  1584. cred->euid != tcred->suid) {
  1585. rcu_read_unlock();
  1586. return -EACCES;
  1587. }
  1588. get_task_struct(tsk);
  1589. rcu_read_unlock();
  1590. } else {
  1591. tsk = current;
  1592. get_task_struct(tsk);
  1593. }
  1594. ret = cgroup_attach_task(cgrp, tsk);
  1595. put_task_struct(tsk);
  1596. return ret;
  1597. }
  1598. static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid)
  1599. {
  1600. int ret;
  1601. if (!cgroup_lock_live_group(cgrp))
  1602. return -ENODEV;
  1603. ret = attach_task_by_pid(cgrp, pid);
  1604. cgroup_unlock();
  1605. return ret;
  1606. }
  1607. /**
  1608. * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
  1609. * @cgrp: the cgroup to be checked for liveness
  1610. *
  1611. * On success, returns true; the lock should be later released with
  1612. * cgroup_unlock(). On failure returns false with no lock held.
  1613. */
  1614. bool cgroup_lock_live_group(struct cgroup *cgrp)
  1615. {
  1616. mutex_lock(&cgroup_mutex);
  1617. if (cgroup_is_removed(cgrp)) {
  1618. mutex_unlock(&cgroup_mutex);
  1619. return false;
  1620. }
  1621. return true;
  1622. }
  1623. EXPORT_SYMBOL_GPL(cgroup_lock_live_group);
  1624. static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft,
  1625. const char *buffer)
  1626. {
  1627. BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
  1628. if (!cgroup_lock_live_group(cgrp))
  1629. return -ENODEV;
  1630. strcpy(cgrp->root->release_agent_path, buffer);
  1631. cgroup_unlock();
  1632. return 0;
  1633. }
  1634. static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft,
  1635. struct seq_file *seq)
  1636. {
  1637. if (!cgroup_lock_live_group(cgrp))
  1638. return -ENODEV;
  1639. seq_puts(seq, cgrp->root->release_agent_path);
  1640. seq_putc(seq, '\n');
  1641. cgroup_unlock();
  1642. return 0;
  1643. }
  1644. /* A buffer size big enough for numbers or short strings */
  1645. #define CGROUP_LOCAL_BUFFER_SIZE 64
  1646. static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
  1647. struct file *file,
  1648. const char __user *userbuf,
  1649. size_t nbytes, loff_t *unused_ppos)
  1650. {
  1651. char buffer[CGROUP_LOCAL_BUFFER_SIZE];
  1652. int retval = 0;
  1653. char *end;
  1654. if (!nbytes)
  1655. return -EINVAL;
  1656. if (nbytes >= sizeof(buffer))
  1657. return -E2BIG;
  1658. if (copy_from_user(buffer, userbuf, nbytes))
  1659. return -EFAULT;
  1660. buffer[nbytes] = 0; /* nul-terminate */
  1661. if (cft->write_u64) {
  1662. u64 val = simple_strtoull(strstrip(buffer), &end, 0);
  1663. if (*end)
  1664. return -EINVAL;
  1665. retval = cft->write_u64(cgrp, cft, val);
  1666. } else {
  1667. s64 val = simple_strtoll(strstrip(buffer), &end, 0);
  1668. if (*end)
  1669. return -EINVAL;
  1670. retval = cft->write_s64(cgrp, cft, val);
  1671. }
  1672. if (!retval)
  1673. retval = nbytes;
  1674. return retval;
  1675. }
  1676. static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft,
  1677. struct file *file,
  1678. const char __user *userbuf,
  1679. size_t nbytes, loff_t *unused_ppos)
  1680. {
  1681. char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
  1682. int retval = 0;
  1683. size_t max_bytes = cft->max_write_len;
  1684. char *buffer = local_buffer;
  1685. if (!max_bytes)
  1686. max_bytes = sizeof(local_buffer) - 1;
  1687. if (nbytes >= max_bytes)
  1688. return -E2BIG;
  1689. /* Allocate a dynamic buffer if we need one */
  1690. if (nbytes >= sizeof(local_buffer)) {
  1691. buffer = kmalloc(nbytes + 1, GFP_KERNEL);
  1692. if (buffer == NULL)
  1693. return -ENOMEM;
  1694. }
  1695. if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
  1696. retval = -EFAULT;
  1697. goto out;
  1698. }
  1699. buffer[nbytes] = 0; /* nul-terminate */
  1700. retval = cft->write_string(cgrp, cft, strstrip(buffer));
  1701. if (!retval)
  1702. retval = nbytes;
  1703. out:
  1704. if (buffer != local_buffer)
  1705. kfree(buffer);
  1706. return retval;
  1707. }
  1708. static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
  1709. size_t nbytes, loff_t *ppos)
  1710. {
  1711. struct cftype *cft = __d_cft(file->f_dentry);
  1712. struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
  1713. if (cgroup_is_removed(cgrp))
  1714. return -ENODEV;
  1715. if (cft->write)
  1716. return cft->write(cgrp, cft, file, buf, nbytes, ppos);
  1717. if (cft->write_u64 || cft->write_s64)
  1718. return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
  1719. if (cft->write_string)
  1720. return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos);
  1721. if (cft->trigger) {
  1722. int ret = cft->trigger(cgrp, (unsigned int)cft->private);
  1723. return ret ? ret : nbytes;
  1724. }
  1725. return -EINVAL;
  1726. }
  1727. static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft,
  1728. struct file *file,
  1729. char __user *buf, size_t nbytes,
  1730. loff_t *ppos)
  1731. {
  1732. char tmp[CGROUP_LOCAL_BUFFER_SIZE];
  1733. u64 val = cft->read_u64(cgrp, cft);
  1734. int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
  1735. return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
  1736. }
  1737. static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft,
  1738. struct file *file,
  1739. char __user *buf, size_t nbytes,
  1740. loff_t *ppos)
  1741. {
  1742. char tmp[CGROUP_LOCAL_BUFFER_SIZE];
  1743. s64 val = cft->read_s64(cgrp, cft);
  1744. int len = sprintf(tmp, "%lld\n", (long long) val);
  1745. return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
  1746. }
  1747. static ssize_t cgroup_file_read(struct file *file, char __user *buf,
  1748. size_t nbytes, loff_t *ppos)
  1749. {
  1750. struct cftype *cft = __d_cft(file->f_dentry);
  1751. struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
  1752. if (cgroup_is_removed(cgrp))
  1753. return -ENODEV;
  1754. if (cft->read)
  1755. return cft->read(cgrp, cft, file, buf, nbytes, ppos);
  1756. if (cft->read_u64)
  1757. return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
  1758. if (cft->read_s64)
  1759. return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
  1760. return -EINVAL;
  1761. }
  1762. /*
  1763. * seqfile ops/methods for returning structured data. Currently just
  1764. * supports string->u64 maps, but can be extended in future.
  1765. */
  1766. struct cgroup_seqfile_state {
  1767. struct cftype *cft;
  1768. struct cgroup *cgroup;
  1769. };
  1770. static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
  1771. {
  1772. struct seq_file *sf = cb->state;
  1773. return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
  1774. }
  1775. static int cgroup_seqfile_show(struct seq_file *m, void *arg)
  1776. {
  1777. struct cgroup_seqfile_state *state = m->private;
  1778. struct cftype *cft = state->cft;
  1779. if (cft->read_map) {
  1780. struct cgroup_map_cb cb = {
  1781. .fill = cgroup_map_add,
  1782. .state = m,
  1783. };
  1784. return cft->read_map(state->cgroup, cft, &cb);
  1785. }
  1786. return cft->read_seq_string(state->cgroup, cft, m);
  1787. }
  1788. static int cgroup_seqfile_release(struct inode *inode, struct file *file)
  1789. {
  1790. struct seq_file *seq = file->private_data;
  1791. kfree(seq->private);
  1792. return single_release(inode, file);
  1793. }
  1794. static const struct file_operations cgroup_seqfile_operations = {
  1795. .read = seq_read,
  1796. .write = cgroup_file_write,
  1797. .llseek = seq_lseek,
  1798. .release = cgroup_seqfile_release,
  1799. };
  1800. static int cgroup_file_open(struct inode *inode, struct file *file)
  1801. {
  1802. int err;
  1803. struct cftype *cft;
  1804. err = generic_file_open(inode, file);
  1805. if (err)
  1806. return err;
  1807. cft = __d_cft(file->f_dentry);
  1808. if (cft->read_map || cft->read_seq_string) {
  1809. struct cgroup_seqfile_state *state =
  1810. kzalloc(sizeof(*state), GFP_USER);
  1811. if (!state)
  1812. return -ENOMEM;
  1813. state->cft = cft;
  1814. state->cgroup = __d_cgrp(file->f_dentry->d_parent);
  1815. file->f_op = &cgroup_seqfile_operations;
  1816. err = single_open(file, cgroup_seqfile_show, state);
  1817. if (err < 0)
  1818. kfree(state);
  1819. } else if (cft->open)
  1820. err = cft->open(inode, file);
  1821. else
  1822. err = 0;
  1823. return err;
  1824. }
  1825. static int cgroup_file_release(struct inode *inode, struct file *file)
  1826. {
  1827. struct cftype *cft = __d_cft(file->f_dentry);
  1828. if (cft->release)
  1829. return cft->release(inode, file);
  1830. return 0;
  1831. }
  1832. /*
  1833. * cgroup_rename - Only allow simple rename of directories in place.
  1834. */
  1835. static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
  1836. struct inode *new_dir, struct dentry *new_dentry)
  1837. {
  1838. if (!S_ISDIR(old_dentry->d_inode->i_mode))
  1839. return -ENOTDIR;
  1840. if (new_dentry->d_inode)
  1841. return -EEXIST;
  1842. if (old_dir != new_dir)
  1843. return -EIO;
  1844. return simple_rename(old_dir, old_dentry, new_dir, new_dentry);
  1845. }
  1846. static const struct file_operations cgroup_file_operations = {
  1847. .read = cgroup_file_read,
  1848. .write = cgroup_file_write,
  1849. .llseek = generic_file_llseek,
  1850. .open = cgroup_file_open,
  1851. .release = cgroup_file_release,
  1852. };
  1853. static const struct inode_operations cgroup_dir_inode_operations = {
  1854. .lookup = simple_lookup,
  1855. .mkdir = cgroup_mkdir,
  1856. .rmdir = cgroup_rmdir,
  1857. .rename = cgroup_rename,
  1858. };
  1859. /*
  1860. * Check if a file is a control file
  1861. */
  1862. static inline struct cftype *__file_cft(struct file *file)
  1863. {
  1864. if (file->f_dentry->d_inode->i_fop != &cgroup_file_operations)
  1865. return ERR_PTR(-EINVAL);
  1866. return __d_cft(file->f_dentry);
  1867. }
  1868. static int cgroup_create_file(struct dentry *dentry, mode_t mode,
  1869. struct super_block *sb)
  1870. {
  1871. static const struct dentry_operations cgroup_dops = {
  1872. .d_iput = cgroup_diput,
  1873. };
  1874. struct inode *inode;
  1875. if (!dentry)
  1876. return -ENOENT;
  1877. if (dentry->d_inode)
  1878. return -EEXIST;
  1879. inode = cgroup_new_inode(mode, sb);
  1880. if (!inode)
  1881. return -ENOMEM;
  1882. if (S_ISDIR(mode)) {
  1883. inode->i_op = &cgroup_dir_inode_operations;
  1884. inode->i_fop = &simple_dir_operations;
  1885. /* start off with i_nlink == 2 (for "." entry) */
  1886. inc_nlink(inode);
  1887. /* start with the directory inode held, so that we can
  1888. * populate it without racing with another mkdir */
  1889. mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
  1890. } else if (S_ISREG(mode)) {
  1891. inode->i_size = 0;
  1892. inode->i_fop = &cgroup_file_operations;
  1893. }
  1894. dentry->d_op = &cgroup_dops;
  1895. d_instantiate(dentry, inode);
  1896. dget(dentry); /* Extra count - pin the dentry in core */
  1897. return 0;
  1898. }
  1899. /*
  1900. * cgroup_create_dir - create a directory for an object.
  1901. * @cgrp: the cgroup we create the directory for. It must have a valid
  1902. * ->parent field. And we are going to fill its ->dentry field.
  1903. * @dentry: dentry of the new cgroup
  1904. * @mode: mode to set on new directory.
  1905. */
  1906. static int cgroup_create_dir(struct cgroup *cgrp, struct dentry *dentry,
  1907. mode_t mode)
  1908. {
  1909. struct dentry *parent;
  1910. int error = 0;
  1911. parent = cgrp->parent->dentry;
  1912. error = cgroup_create_file(dentry, S_IFDIR | mode, cgrp->root->sb);
  1913. if (!error) {
  1914. dentry->d_fsdata = cgrp;
  1915. inc_nlink(parent->d_inode);
  1916. rcu_assign_pointer(cgrp->dentry, dentry);
  1917. dget(dentry);
  1918. }
  1919. dput(dentry);
  1920. return error;
  1921. }
  1922. /**
  1923. * cgroup_file_mode - deduce file mode of a control file
  1924. * @cft: the control file in question
  1925. *
  1926. * returns cft->mode if ->mode is not 0
  1927. * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
  1928. * returns S_IRUGO if it has only a read handler
  1929. * returns S_IWUSR if it has only a write hander
  1930. */
  1931. static mode_t cgroup_file_mode(const struct cftype *cft)
  1932. {
  1933. mode_t mode = 0;
  1934. if (cft->mode)
  1935. return cft->mode;
  1936. if (cft->read || cft->read_u64 || cft->read_s64 ||
  1937. cft->read_map || cft->read_seq_string)
  1938. mode |= S_IRUGO;
  1939. if (cft->write || cft->write_u64 || cft->write_s64 ||
  1940. cft->write_string || cft->trigger)
  1941. mode |= S_IWUSR;
  1942. return mode;
  1943. }
  1944. int cgroup_add_file(struct cgroup *cgrp,
  1945. struct cgroup_subsys *subsys,
  1946. const struct cftype *cft)
  1947. {
  1948. struct dentry *dir = cgrp->dentry;
  1949. struct dentry *dentry;
  1950. int error;
  1951. mode_t mode;
  1952. char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
  1953. if (subsys && !test_bit(ROOT_NOPREFIX, &cgrp->root->flags)) {
  1954. strcpy(name, subsys->name);
  1955. strcat(name, ".");
  1956. }
  1957. strcat(name, cft->name);
  1958. BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
  1959. dentry = lookup_one_len(name, dir, strlen(name));
  1960. if (!IS_ERR(dentry)) {
  1961. mode = cgroup_file_mode(cft);
  1962. error = cgroup_create_file(dentry, mode | S_IFREG,
  1963. cgrp->root->sb);
  1964. if (!error)
  1965. dentry->d_fsdata = (void *)cft;
  1966. dput(dentry);
  1967. } else
  1968. error = PTR_ERR(dentry);
  1969. return error;
  1970. }
  1971. EXPORT_SYMBOL_GPL(cgroup_add_file);
  1972. int cgroup_add_files(struct cgroup *cgrp,
  1973. struct cgroup_subsys *subsys,
  1974. const struct cftype cft[],
  1975. int count)
  1976. {
  1977. int i, err;
  1978. for (i = 0; i < count; i++) {
  1979. err = cgroup_add_file(cgrp, subsys, &cft[i]);
  1980. if (err)
  1981. return err;
  1982. }
  1983. return 0;
  1984. }
  1985. EXPORT_SYMBOL_GPL(cgroup_add_files);
  1986. /**
  1987. * cgroup_task_count - count the number of tasks in a cgroup.
  1988. * @cgrp: the cgroup in question
  1989. *
  1990. * Return the number of tasks in the cgroup.
  1991. */
  1992. int cgroup_task_count(const struct cgroup *cgrp)
  1993. {
  1994. int count = 0;
  1995. struct cg_cgroup_link *link;
  1996. read_lock(&css_set_lock);
  1997. list_for_each_entry(link, &cgrp->css_sets, cgrp_link_list) {
  1998. count += atomic_read(&link->cg->refcount);
  1999. }
  2000. read_unlock(&css_set_lock);
  2001. return count;
  2002. }
  2003. /*
  2004. * Advance a list_head iterator. The iterator should be positioned at
  2005. * the start of a css_set
  2006. */
  2007. static void cgroup_advance_iter(struct cgroup *cgrp,
  2008. struct cgroup_iter *it)
  2009. {
  2010. struct list_head *l = it->cg_link;
  2011. struct cg_cgroup_link *link;
  2012. struct css_set *cg;
  2013. /* Advance to the next non-empty css_set */
  2014. do {
  2015. l = l->next;
  2016. if (l == &cgrp->css_sets) {
  2017. it->cg_link = NULL;
  2018. return;
  2019. }
  2020. link = list_entry(l, struct cg_cgroup_link, cgrp_link_list);
  2021. cg = link->cg;
  2022. } while (list_empty(&cg->tasks));
  2023. it->cg_link = l;
  2024. it->task = cg->tasks.next;
  2025. }
  2026. /*
  2027. * To reduce the fork() overhead for systems that are not actually
  2028. * using their cgroups capability, we don't maintain the lists running
  2029. * through each css_set to its tasks until we see the list actually
  2030. * used - in other words after the first call to cgroup_iter_start().
  2031. *
  2032. * The tasklist_lock is not held here, as do_each_thread() and
  2033. * while_each_thread() are protected by RCU.
  2034. */
  2035. static void cgroup_enable_task_cg_lists(void)
  2036. {
  2037. struct task_struct *p, *g;
  2038. write_lock(&css_set_lock);
  2039. use_task_css_set_links = 1;
  2040. do_each_thread(g, p) {
  2041. task_lock(p);
  2042. /*
  2043. * We should check if the process is exiting, otherwise
  2044. * it will race with cgroup_exit() in that the list
  2045. * entry won't be deleted though the process has exited.
  2046. */
  2047. if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
  2048. list_add(&p->cg_list, &p->cgroups->tasks);
  2049. task_unlock(p);
  2050. } while_each_thread(g, p);
  2051. write_unlock(&css_set_lock);
  2052. }
  2053. void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
  2054. {
  2055. /*
  2056. * The first time anyone tries to iterate across a cgroup,
  2057. * we need to enable the list linking each css_set to its
  2058. * tasks, and fix up all existing tasks.
  2059. */
  2060. if (!use_task_css_set_links)
  2061. cgroup_enable_task_cg_lists();
  2062. read_lock(&css_set_lock);
  2063. it->cg_link = &cgrp->css_sets;
  2064. cgroup_advance_iter(cgrp, it);
  2065. }
  2066. struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
  2067. struct cgroup_iter *it)
  2068. {
  2069. struct task_struct *res;
  2070. struct list_head *l = it->task;
  2071. struct cg_cgroup_link *link;
  2072. /* If the iterator cg is NULL, we have no tasks */
  2073. if (!it->cg_link)
  2074. return NULL;
  2075. res = list_entry(l, struct task_struct, cg_list);
  2076. /* Advance iterator to find next entry */
  2077. l = l->next;
  2078. link = list_entry(it->cg_link, struct cg_cgroup_link, cgrp_link_list);
  2079. if (l == &link->cg->tasks) {
  2080. /* We reached the end of this task list - move on to
  2081. * the next cg_cgroup_link */
  2082. cgroup_advance_iter(cgrp, it);
  2083. } else {
  2084. it->task = l;
  2085. }
  2086. return res;
  2087. }
  2088. void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
  2089. {
  2090. read_unlock(&css_set_lock);
  2091. }
  2092. static inline int started_after_time(struct task_struct *t1,
  2093. struct timespec *time,
  2094. struct task_struct *t2)
  2095. {
  2096. int start_diff = timespec_compare(&t1->start_time, time);
  2097. if (start_diff > 0) {
  2098. return 1;
  2099. } else if (start_diff < 0) {
  2100. return 0;
  2101. } else {
  2102. /*
  2103. * Arbitrarily, if two processes started at the same
  2104. * time, we'll say that the lower pointer value
  2105. * started first. Note that t2 may have exited by now
  2106. * so this may not be a valid pointer any longer, but
  2107. * that's fine - it still serves to distinguish
  2108. * between two tasks started (effectively) simultaneously.
  2109. */
  2110. return t1 > t2;
  2111. }
  2112. }
  2113. /*
  2114. * This function is a callback from heap_insert() and is used to order
  2115. * the heap.
  2116. * In this case we order the heap in descending task start time.
  2117. */
  2118. static inline int started_after(void *p1, void *p2)
  2119. {
  2120. struct task_struct *t1 = p1;
  2121. struct task_struct *t2 = p2;
  2122. return started_after_time(t1, &t2->start_time, t2);
  2123. }
  2124. /**
  2125. * cgroup_scan_tasks - iterate though all the tasks in a cgroup
  2126. * @scan: struct cgroup_scanner containing arguments for the scan
  2127. *
  2128. * Arguments include pointers to callback functions test_task() and
  2129. * process_task().
  2130. * Iterate through all the tasks in a cgroup, calling test_task() for each,
  2131. * and if it returns true, call process_task() for it also.
  2132. * The test_task pointer may be NULL, meaning always true (select all tasks).
  2133. * Effectively duplicates cgroup_iter_{start,next,end}()
  2134. * but does not lock css_set_lock for the call to process_task().
  2135. * The struct cgroup_scanner may be embedded in any structure of the caller's
  2136. * creation.
  2137. * It is guaranteed that process_task() will act on every task that
  2138. * is a member of the cgroup for the duration of this call. This
  2139. * function may or may not call process_task() for tasks that exit
  2140. * or move to a different cgroup during the call, or are forked or
  2141. * move into the cgroup during the call.
  2142. *
  2143. * Note that test_task() may be called with locks held, and may in some
  2144. * situations be called multiple times for the same task, so it should
  2145. * be cheap.
  2146. * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
  2147. * pre-allocated and will be used for heap operations (and its "gt" member will
  2148. * be overwritten), else a temporary heap will be used (allocation of which
  2149. * may cause this function to fail).
  2150. */
  2151. int cgroup_scan_tasks(struct cgroup_scanner *scan)
  2152. {
  2153. int retval, i;
  2154. struct cgroup_iter it;
  2155. struct task_struct *p, *dropped;
  2156. /* Never dereference latest_task, since it's not refcounted */
  2157. struct task_struct *latest_task = NULL;
  2158. struct ptr_heap tmp_heap;
  2159. struct ptr_heap *heap;
  2160. struct timespec latest_time = { 0, 0 };
  2161. if (scan->heap) {
  2162. /* The caller supplied our heap and pre-allocated its memory */
  2163. heap = scan->heap;
  2164. heap->gt = &started_after;
  2165. } else {
  2166. /* We need to allocate our own heap memory */
  2167. heap = &tmp_heap;
  2168. retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
  2169. if (retval)
  2170. /* cannot allocate the heap */
  2171. return retval;
  2172. }
  2173. again:
  2174. /*
  2175. * Scan tasks in the cgroup, using the scanner's "test_task" callback
  2176. * to determine which are of interest, and using the scanner's
  2177. * "process_task" callback to process any of them that need an update.
  2178. * Since we don't want to hold any locks during the task updates,
  2179. * gather tasks to be processed in a heap structure.
  2180. * The heap is sorted by descending task start time.
  2181. * If the statically-sized heap fills up, we overflow tasks that
  2182. * started later, and in future iterations only consider tasks that
  2183. * started after the latest task in the previous pass. This
  2184. * guarantees forward progress and that we don't miss any tasks.
  2185. */
  2186. heap->size = 0;
  2187. cgroup_iter_start(scan->cg, &it);
  2188. while ((p = cgroup_iter_next(scan->cg, &it))) {
  2189. /*
  2190. * Only affect tasks that qualify per the caller's callback,
  2191. * if he provided one
  2192. */
  2193. if (scan->test_task && !scan->test_task(p, scan))
  2194. continue;
  2195. /*
  2196. * Only process tasks that started after the last task
  2197. * we processed
  2198. */
  2199. if (!started_after_time(p, &latest_time, latest_task))
  2200. continue;
  2201. dropped = heap_insert(heap, p);
  2202. if (dropped == NULL) {
  2203. /*
  2204. * The new task was inserted; the heap wasn't
  2205. * previously full
  2206. */
  2207. get_task_struct(p);
  2208. } else if (dropped != p) {
  2209. /*
  2210. * The new task was inserted, and pushed out a
  2211. * different task
  2212. */
  2213. get_task_struct(p);
  2214. put_task_struct(dropped);
  2215. }
  2216. /*
  2217. * Else the new task was newer than anything already in
  2218. * the heap and wasn't inserted
  2219. */
  2220. }
  2221. cgroup_iter_end(scan->cg, &it);
  2222. if (heap->size) {
  2223. for (i = 0; i < heap->size; i++) {
  2224. struct task_struct *q = heap->ptrs[i];
  2225. if (i == 0) {
  2226. latest_time = q->start_time;
  2227. latest_task = q;
  2228. }
  2229. /* Process the task per the caller's callback */
  2230. scan->process_task(q, scan);
  2231. put_task_struct(q);
  2232. }
  2233. /*
  2234. * If we had to process any tasks at all, scan again
  2235. * in case some of them were in the middle of forking
  2236. * children that didn't get processed.
  2237. * Not the most efficient way to do it, but it avoids
  2238. * having to take callback_mutex in the fork path
  2239. */
  2240. goto again;
  2241. }
  2242. if (heap == &tmp_heap)
  2243. heap_free(&tmp_heap);
  2244. return 0;
  2245. }
  2246. /*
  2247. * Stuff for reading the 'tasks'/'procs' files.
  2248. *
  2249. * Reading this file can return large amounts of data if a cgroup has
  2250. * *lots* of attached tasks. So it may need several calls to read(),
  2251. * but we cannot guarantee that the information we produce is correct
  2252. * unless we produce it entirely atomically.
  2253. *
  2254. */
  2255. /*
  2256. * The following two functions "fix" the issue where there are more pids
  2257. * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
  2258. * TODO: replace with a kernel-wide solution to this problem
  2259. */
  2260. #define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
  2261. static void *pidlist_allocate(int count)
  2262. {
  2263. if (PIDLIST_TOO_LARGE(count))
  2264. return vmalloc(count * sizeof(pid_t));
  2265. else
  2266. return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
  2267. }
  2268. static void pidlist_free(void *p)
  2269. {
  2270. if (is_vmalloc_addr(p))
  2271. vfree(p);
  2272. else
  2273. kfree(p);
  2274. }
  2275. static void *pidlist_resize(void *p, int newcount)
  2276. {
  2277. void *newlist;
  2278. /* note: if new alloc fails, old p will still be valid either way */
  2279. if (is_vmalloc_addr(p)) {
  2280. newlist = vmalloc(newcount * sizeof(pid_t));
  2281. if (!newlist)
  2282. return NULL;
  2283. memcpy(newlist, p, newcount * sizeof(pid_t));
  2284. vfree(p);
  2285. } else {
  2286. newlist = krealloc(p, newcount * sizeof(pid_t), GFP_KERNEL);
  2287. }
  2288. return newlist;
  2289. }
  2290. /*
  2291. * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
  2292. * If the new stripped list is sufficiently smaller and there's enough memory
  2293. * to allocate a new buffer, will let go of the unneeded memory. Returns the
  2294. * number of unique elements.
  2295. */
  2296. /* is the size difference enough that we should re-allocate the array? */
  2297. #define PIDLIST_REALLOC_DIFFERENCE(old, new) ((old) - PAGE_SIZE >= (new))
  2298. static int pidlist_uniq(pid_t **p, int length)
  2299. {
  2300. int src, dest = 1;
  2301. pid_t *list = *p;
  2302. pid_t *newlist;
  2303. /*
  2304. * we presume the 0th element is unique, so i starts at 1. trivial
  2305. * edge cases first; no work needs to be done for either
  2306. */
  2307. if (length == 0 || length == 1)
  2308. return length;
  2309. /* src and dest walk down the list; dest counts unique elements */
  2310. for (src = 1; src < length; src++) {
  2311. /* find next unique element */
  2312. while (list[src] == list[src-1]) {
  2313. src++;
  2314. if (src == length)
  2315. goto after;
  2316. }
  2317. /* dest always points to where the next unique element goes */
  2318. list[dest] = list[src];
  2319. dest++;
  2320. }
  2321. after:
  2322. /*
  2323. * if the length difference is large enough, we want to allocate a
  2324. * smaller buffer to save memory. if this fails due to out of memory,
  2325. * we'll just stay with what we've got.
  2326. */
  2327. if (PIDLIST_REALLOC_DIFFERENCE(length, dest)) {
  2328. newlist = pidlist_resize(list, dest);
  2329. if (newlist)
  2330. *p = newlist;
  2331. }
  2332. return dest;
  2333. }
  2334. static int cmppid(const void *a, const void *b)
  2335. {
  2336. return *(pid_t *)a - *(pid_t *)b;
  2337. }
  2338. /*
  2339. * find the appropriate pidlist for our purpose (given procs vs tasks)
  2340. * returns with the lock on that pidlist already held, and takes care
  2341. * of the use count, or returns NULL with no locks held if we're out of
  2342. * memory.
  2343. */
  2344. static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
  2345. enum cgroup_filetype type)
  2346. {
  2347. struct cgroup_pidlist *l;
  2348. /* don't need task_nsproxy() if we're looking at ourself */
  2349. struct pid_namespace *ns = current->nsproxy->pid_ns;
  2350. /*
  2351. * We can't drop the pidlist_mutex before taking the l->mutex in case
  2352. * the last ref-holder is trying to remove l from the list at the same
  2353. * time. Holding the pidlist_mutex precludes somebody taking whichever
  2354. * list we find out from under us - compare release_pid_array().
  2355. */
  2356. mutex_lock(&cgrp->pidlist_mutex);
  2357. list_for_each_entry(l, &cgrp->pidlists, links) {
  2358. if (l->key.type == type && l->key.ns == ns) {
  2359. /* make sure l doesn't vanish out from under us */
  2360. down_write(&l->mutex);
  2361. mutex_unlock(&cgrp->pidlist_mutex);
  2362. return l;
  2363. }
  2364. }
  2365. /* entry not found; create a new one */
  2366. l = kmalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
  2367. if (!l) {
  2368. mutex_unlock(&cgrp->pidlist_mutex);
  2369. return l;
  2370. }
  2371. init_rwsem(&l->mutex);
  2372. down_write(&l->mutex);
  2373. l->key.type = type;
  2374. l->key.ns = get_pid_ns(ns);
  2375. l->use_count = 0; /* don't increment here */
  2376. l->list = NULL;
  2377. l->owner = cgrp;
  2378. list_add(&l->links, &cgrp->pidlists);
  2379. mutex_unlock(&cgrp->pidlist_mutex);
  2380. return l;
  2381. }
  2382. /*
  2383. * Load a cgroup's pidarray with either procs' tgids or tasks' pids
  2384. */
  2385. static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
  2386. struct cgroup_pidlist **lp)
  2387. {
  2388. pid_t *array;
  2389. int length;
  2390. int pid, n = 0; /* used for populating the array */
  2391. struct cgroup_iter it;
  2392. struct task_struct *tsk;
  2393. struct cgroup_pidlist *l;
  2394. /*
  2395. * If cgroup gets more users after we read count, we won't have
  2396. * enough space - tough. This race is indistinguishable to the
  2397. * caller from the case that the additional cgroup users didn't
  2398. * show up until sometime later on.
  2399. */
  2400. length = cgroup_task_count(cgrp);
  2401. array = pidlist_allocate(length);
  2402. if (!array)
  2403. return -ENOMEM;
  2404. /* now, populate the array */
  2405. cgroup_iter_start(cgrp, &it);
  2406. while ((tsk = cgroup_iter_next(cgrp, &it))) {
  2407. if (unlikely(n == length))
  2408. break;
  2409. /* get tgid or pid for procs or tasks file respectively */
  2410. if (type == CGROUP_FILE_PROCS)
  2411. pid = task_tgid_vnr(tsk);
  2412. else
  2413. pid = task_pid_vnr(tsk);
  2414. if (pid > 0) /* make sure to only use valid results */
  2415. array[n++] = pid;
  2416. }
  2417. cgroup_iter_end(cgrp, &it);
  2418. length = n;
  2419. /* now sort & (if procs) strip out duplicates */
  2420. sort(array, length, sizeof(pid_t), cmppid, NULL);
  2421. if (type == CGROUP_FILE_PROCS)
  2422. length = pidlist_uniq(&array, length);
  2423. l = cgroup_pidlist_find(cgrp, type);
  2424. if (!l) {
  2425. pidlist_free(array);
  2426. return -ENOMEM;
  2427. }
  2428. /* store array, freeing old if necessary - lock already held */
  2429. pidlist_free(l->list);
  2430. l->list = array;
  2431. l->length = length;
  2432. l->use_count++;
  2433. up_write(&l->mutex);
  2434. *lp = l;
  2435. return 0;
  2436. }
  2437. /**
  2438. * cgroupstats_build - build and fill cgroupstats
  2439. * @stats: cgroupstats to fill information into
  2440. * @dentry: A dentry entry belonging to the cgroup for which stats have
  2441. * been requested.
  2442. *
  2443. * Build and fill cgroupstats so that taskstats can export it to user
  2444. * space.
  2445. */
  2446. int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
  2447. {
  2448. int ret = -EINVAL;
  2449. struct cgroup *cgrp;
  2450. struct cgroup_iter it;
  2451. struct task_struct *tsk;
  2452. /*
  2453. * Validate dentry by checking the superblock operations,
  2454. * and make sure it's a directory.
  2455. */
  2456. if (dentry->d_sb->s_op != &cgroup_ops ||
  2457. !S_ISDIR(dentry->d_inode->i_mode))
  2458. goto err;
  2459. ret = 0;
  2460. cgrp = dentry->d_fsdata;
  2461. cgroup_iter_start(cgrp, &it);
  2462. while ((tsk = cgroup_iter_next(cgrp, &it))) {
  2463. switch (tsk->state) {
  2464. case TASK_RUNNING:
  2465. stats->nr_running++;
  2466. break;
  2467. case TASK_INTERRUPTIBLE:
  2468. stats->nr_sleeping++;
  2469. break;
  2470. case TASK_UNINTERRUPTIBLE:
  2471. stats->nr_uninterruptible++;
  2472. break;
  2473. case TASK_STOPPED:
  2474. stats->nr_stopped++;
  2475. break;
  2476. default:
  2477. if (delayacct_is_task_waiting_on_io(tsk))
  2478. stats->nr_io_wait++;
  2479. break;
  2480. }
  2481. }
  2482. cgroup_iter_end(cgrp, &it);
  2483. err:
  2484. return ret;
  2485. }
  2486. /*
  2487. * seq_file methods for the tasks/procs files. The seq_file position is the
  2488. * next pid to display; the seq_file iterator is a pointer to the pid
  2489. * in the cgroup->l->list array.
  2490. */
  2491. static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
  2492. {
  2493. /*
  2494. * Initially we receive a position value that corresponds to
  2495. * one more than the last pid shown (or 0 on the first call or
  2496. * after a seek to the start). Use a binary-search to find the
  2497. * next pid to display, if any
  2498. */
  2499. struct cgroup_pidlist *l = s->private;
  2500. int index = 0, pid = *pos;
  2501. int *iter;
  2502. down_read(&l->mutex);
  2503. if (pid) {
  2504. int end = l->length;
  2505. while (index < end) {
  2506. int mid = (index + end) / 2;
  2507. if (l->list[mid] == pid) {
  2508. index = mid;
  2509. break;
  2510. } else if (l->list[mid] <= pid)
  2511. index = mid + 1;
  2512. else
  2513. end = mid;
  2514. }
  2515. }
  2516. /* If we're off the end of the array, we're done */
  2517. if (index >= l->length)
  2518. return NULL;
  2519. /* Update the abstract position to be the actual pid that we found */
  2520. iter = l->list + index;
  2521. *pos = *iter;
  2522. return iter;
  2523. }
  2524. static void cgroup_pidlist_stop(struct seq_file *s, void *v)
  2525. {
  2526. struct cgroup_pidlist *l = s->private;
  2527. up_read(&l->mutex);
  2528. }
  2529. static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
  2530. {
  2531. struct cgroup_pidlist *l = s->private;
  2532. pid_t *p = v;
  2533. pid_t *end = l->list + l->length;
  2534. /*
  2535. * Advance to the next pid in the array. If this goes off the
  2536. * end, we're done
  2537. */
  2538. p++;
  2539. if (p >= end) {
  2540. return NULL;
  2541. } else {
  2542. *pos = *p;
  2543. return p;
  2544. }
  2545. }
  2546. static int cgroup_pidlist_show(struct seq_file *s, void *v)
  2547. {
  2548. return seq_printf(s, "%d\n", *(int *)v);
  2549. }
  2550. /*
  2551. * seq_operations functions for iterating on pidlists through seq_file -
  2552. * independent of whether it's tasks or procs
  2553. */
  2554. static const struct seq_operations cgroup_pidlist_seq_operations = {
  2555. .start = cgroup_pidlist_start,
  2556. .stop = cgroup_pidlist_stop,
  2557. .next = cgroup_pidlist_next,
  2558. .show = cgroup_pidlist_show,
  2559. };
  2560. static void cgroup_release_pid_array(struct cgroup_pidlist *l)
  2561. {
  2562. /*
  2563. * the case where we're the last user of this particular pidlist will
  2564. * have us remove it from the cgroup's list, which entails taking the
  2565. * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
  2566. * pidlist_mutex, we have to take pidlist_mutex first.
  2567. */
  2568. mutex_lock(&l->owner->pidlist_mutex);
  2569. down_write(&l->mutex);
  2570. BUG_ON(!l->use_count);
  2571. if (!--l->use_count) {
  2572. /* we're the last user if refcount is 0; remove and free */
  2573. list_del(&l->links);
  2574. mutex_unlock(&l->owner->pidlist_mutex);
  2575. pidlist_free(l->list);
  2576. put_pid_ns(l->key.ns);
  2577. up_write(&l->mutex);
  2578. kfree(l);
  2579. return;
  2580. }
  2581. mutex_unlock(&l->owner->pidlist_mutex);
  2582. up_write(&l->mutex);
  2583. }
  2584. static int cgroup_pidlist_release(struct inode *inode, struct file *file)
  2585. {
  2586. struct cgroup_pidlist *l;
  2587. if (!(file->f_mode & FMODE_READ))
  2588. return 0;
  2589. /*
  2590. * the seq_file will only be initialized if the file was opened for
  2591. * reading; hence we check if it's not null only in that case.
  2592. */
  2593. l = ((struct seq_file *)file->private_data)->private;
  2594. cgroup_release_pid_array(l);
  2595. return seq_release(inode, file);
  2596. }
  2597. static const struct file_operations cgroup_pidlist_operations = {
  2598. .read = seq_read,
  2599. .llseek = seq_lseek,
  2600. .write = cgroup_file_write,
  2601. .release = cgroup_pidlist_release,
  2602. };
  2603. /*
  2604. * The following functions handle opens on a file that displays a pidlist
  2605. * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
  2606. * in the cgroup.
  2607. */
  2608. /* helper function for the two below it */
  2609. static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
  2610. {
  2611. struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
  2612. struct cgroup_pidlist *l;
  2613. int retval;
  2614. /* Nothing to do for write-only files */
  2615. if (!(file->f_mode & FMODE_READ))
  2616. return 0;
  2617. /* have the array populated */
  2618. retval = pidlist_array_load(cgrp, type, &l);
  2619. if (retval)
  2620. return retval;
  2621. /* configure file information */
  2622. file->f_op = &cgroup_pidlist_operations;
  2623. retval = seq_open(file, &cgroup_pidlist_seq_operations);
  2624. if (retval) {
  2625. cgroup_release_pid_array(l);
  2626. return retval;
  2627. }
  2628. ((struct seq_file *)file->private_data)->private = l;
  2629. return 0;
  2630. }
  2631. static int cgroup_tasks_open(struct inode *unused, struct file *file)
  2632. {
  2633. return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
  2634. }
  2635. static int cgroup_procs_open(struct inode *unused, struct file *file)
  2636. {
  2637. return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
  2638. }
  2639. static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
  2640. struct cftype *cft)
  2641. {
  2642. return notify_on_release(cgrp);
  2643. }
  2644. static int cgroup_write_notify_on_release(struct cgroup *cgrp,
  2645. struct cftype *cft,
  2646. u64 val)
  2647. {
  2648. clear_bit(CGRP_RELEASABLE, &cgrp->flags);
  2649. if (val)
  2650. set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
  2651. else
  2652. clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
  2653. return 0;
  2654. }
  2655. /*
  2656. * Unregister event and free resources.
  2657. *
  2658. * Gets called from workqueue.
  2659. */
  2660. static void cgroup_event_remove(struct work_struct *work)
  2661. {
  2662. struct cgroup_event *event = container_of(work, struct cgroup_event,
  2663. remove);
  2664. struct cgroup *cgrp = event->cgrp;
  2665. /* TODO: check return code */
  2666. event->cft->unregister_event(cgrp, event->cft, event->eventfd);
  2667. eventfd_ctx_put(event->eventfd);
  2668. kfree(event);
  2669. dput(cgrp->dentry);
  2670. }
  2671. /*
  2672. * Gets called on POLLHUP on eventfd when user closes it.
  2673. *
  2674. * Called with wqh->lock held and interrupts disabled.
  2675. */
  2676. static int cgroup_event_wake(wait_queue_t *wait, unsigned mode,
  2677. int sync, void *key)
  2678. {
  2679. struct cgroup_event *event = container_of(wait,
  2680. struct cgroup_event, wait);
  2681. struct cgroup *cgrp = event->cgrp;
  2682. unsigned long flags = (unsigned long)key;
  2683. if (flags & POLLHUP) {
  2684. remove_wait_queue_locked(event->wqh, &event->wait);
  2685. spin_lock(&cgrp->event_list_lock);
  2686. list_del(&event->list);
  2687. spin_unlock(&cgrp->event_list_lock);
  2688. /*
  2689. * We are in atomic context, but cgroup_event_remove() may
  2690. * sleep, so we have to call it in workqueue.
  2691. */
  2692. schedule_work(&event->remove);
  2693. }
  2694. return 0;
  2695. }
  2696. static void cgroup_event_ptable_queue_proc(struct file *file,
  2697. wait_queue_head_t *wqh, poll_table *pt)
  2698. {
  2699. struct cgroup_event *event = container_of(pt,
  2700. struct cgroup_event, pt);
  2701. event->wqh = wqh;
  2702. add_wait_queue(wqh, &event->wait);
  2703. }
  2704. /*
  2705. * Parse input and register new cgroup event handler.
  2706. *
  2707. * Input must be in format '<event_fd> <control_fd> <args>'.
  2708. * Interpretation of args is defined by control file implementation.
  2709. */
  2710. static int cgroup_write_event_control(struct cgroup *cgrp, struct cftype *cft,
  2711. const char *buffer)
  2712. {
  2713. struct cgroup_event *event = NULL;
  2714. unsigned int efd, cfd;
  2715. struct file *efile = NULL;
  2716. struct file *cfile = NULL;
  2717. char *endp;
  2718. int ret;
  2719. efd = simple_strtoul(buffer, &endp, 10);
  2720. if (*endp != ' ')
  2721. return -EINVAL;
  2722. buffer = endp + 1;
  2723. cfd = simple_strtoul(buffer, &endp, 10);
  2724. if ((*endp != ' ') && (*endp != '\0'))
  2725. return -EINVAL;
  2726. buffer = endp + 1;
  2727. event = kzalloc(sizeof(*event), GFP_KERNEL);
  2728. if (!event)
  2729. return -ENOMEM;
  2730. event->cgrp = cgrp;
  2731. INIT_LIST_HEAD(&event->list);
  2732. init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc);
  2733. init_waitqueue_func_entry(&event->wait, cgroup_event_wake);
  2734. INIT_WORK(&event->remove, cgroup_event_remove);
  2735. efile = eventfd_fget(efd);
  2736. if (IS_ERR(efile)) {
  2737. ret = PTR_ERR(efile);
  2738. goto fail;
  2739. }
  2740. event->eventfd = eventfd_ctx_fileget(efile);
  2741. if (IS_ERR(event->eventfd)) {
  2742. ret = PTR_ERR(event->eventfd);
  2743. goto fail;
  2744. }
  2745. cfile = fget(cfd);
  2746. if (!cfile) {
  2747. ret = -EBADF;
  2748. goto fail;
  2749. }
  2750. /* the process need read permission on control file */
  2751. ret = file_permission(cfile, MAY_READ);
  2752. if (ret < 0)
  2753. goto fail;
  2754. event->cft = __file_cft(cfile);
  2755. if (IS_ERR(event->cft)) {
  2756. ret = PTR_ERR(event->cft);
  2757. goto fail;
  2758. }
  2759. if (!event->cft->register_event || !event->cft->unregister_event) {
  2760. ret = -EINVAL;
  2761. goto fail;
  2762. }
  2763. ret = event->cft->register_event(cgrp, event->cft,
  2764. event->eventfd, buffer);
  2765. if (ret)
  2766. goto fail;
  2767. if (efile->f_op->poll(efile, &event->pt) & POLLHUP) {
  2768. event->cft->unregister_event(cgrp, event->cft, event->eventfd);
  2769. ret = 0;
  2770. goto fail;
  2771. }
  2772. /*
  2773. * Events should be removed after rmdir of cgroup directory, but before
  2774. * destroying subsystem state objects. Let's take reference to cgroup
  2775. * directory dentry to do that.
  2776. */
  2777. dget(cgrp->dentry);
  2778. spin_lock(&cgrp->event_list_lock);
  2779. list_add(&event->list, &cgrp->event_list);
  2780. spin_unlock(&cgrp->event_list_lock);
  2781. fput(cfile);
  2782. fput(efile);
  2783. return 0;
  2784. fail:
  2785. if (cfile)
  2786. fput(cfile);
  2787. if (event && event->eventfd && !IS_ERR(event->eventfd))
  2788. eventfd_ctx_put(event->eventfd);
  2789. if (!IS_ERR_OR_NULL(efile))
  2790. fput(efile);
  2791. kfree(event);
  2792. return ret;
  2793. }
  2794. /*
  2795. * for the common functions, 'private' gives the type of file
  2796. */
  2797. /* for hysterical raisins, we can't put this on the older files */
  2798. #define CGROUP_FILE_GENERIC_PREFIX "cgroup."
  2799. static struct cftype files[] = {
  2800. {
  2801. .name = "tasks",
  2802. .open = cgroup_tasks_open,
  2803. .write_u64 = cgroup_tasks_write,
  2804. .release = cgroup_pidlist_release,
  2805. .mode = S_IRUGO | S_IWUSR,
  2806. },
  2807. {
  2808. .name = CGROUP_FILE_GENERIC_PREFIX "procs",
  2809. .open = cgroup_procs_open,
  2810. /* .write_u64 = cgroup_procs_write, TODO */
  2811. .release = cgroup_pidlist_release,
  2812. .mode = S_IRUGO,
  2813. },
  2814. {
  2815. .name = "notify_on_release",
  2816. .read_u64 = cgroup_read_notify_on_release,
  2817. .write_u64 = cgroup_write_notify_on_release,
  2818. },
  2819. {
  2820. .name = CGROUP_FILE_GENERIC_PREFIX "event_control",
  2821. .write_string = cgroup_write_event_control,
  2822. .mode = S_IWUGO,
  2823. },
  2824. };
  2825. static struct cftype cft_release_agent = {
  2826. .name = "release_agent",
  2827. .read_seq_string = cgroup_release_agent_show,
  2828. .write_string = cgroup_release_agent_write,
  2829. .max_write_len = PATH_MAX,
  2830. };
  2831. static int cgroup_populate_dir(struct cgroup *cgrp)
  2832. {
  2833. int err;
  2834. struct cgroup_subsys *ss;
  2835. /* First clear out any existing files */
  2836. cgroup_clear_directory(cgrp->dentry);
  2837. err = cgroup_add_files(cgrp, NULL, files, ARRAY_SIZE(files));
  2838. if (err < 0)
  2839. return err;
  2840. if (cgrp == cgrp->top_cgroup) {
  2841. if ((err = cgroup_add_file(cgrp, NULL, &cft_release_agent)) < 0)
  2842. return err;
  2843. }
  2844. for_each_subsys(cgrp->root, ss) {
  2845. if (ss->populate && (err = ss->populate(ss, cgrp)) < 0)
  2846. return err;
  2847. }
  2848. /* This cgroup is ready now */
  2849. for_each_subsys(cgrp->root, ss) {
  2850. struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
  2851. /*
  2852. * Update id->css pointer and make this css visible from
  2853. * CSS ID functions. This pointer will be dereferened
  2854. * from RCU-read-side without locks.
  2855. */
  2856. if (css->id)
  2857. rcu_assign_pointer(css->id->css, css);
  2858. }
  2859. return 0;
  2860. }
  2861. static void init_cgroup_css(struct cgroup_subsys_state *css,
  2862. struct cgroup_subsys *ss,
  2863. struct cgroup *cgrp)
  2864. {
  2865. css->cgroup = cgrp;
  2866. atomic_set(&css->refcnt, 1);
  2867. css->flags = 0;
  2868. css->id = NULL;
  2869. if (cgrp == dummytop)
  2870. set_bit(CSS_ROOT, &css->flags);
  2871. BUG_ON(cgrp->subsys[ss->subsys_id]);
  2872. cgrp->subsys[ss->subsys_id] = css;
  2873. }
  2874. static void cgroup_lock_hierarchy(struct cgroupfs_root *root)
  2875. {
  2876. /* We need to take each hierarchy_mutex in a consistent order */
  2877. int i;
  2878. /*
  2879. * No worry about a race with rebind_subsystems that might mess up the
  2880. * locking order, since both parties are under cgroup_mutex.
  2881. */
  2882. for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
  2883. struct cgroup_subsys *ss = subsys[i];
  2884. if (ss == NULL)
  2885. continue;
  2886. if (ss->root == root)
  2887. mutex_lock(&ss->hierarchy_mutex);
  2888. }
  2889. }
  2890. static void cgroup_unlock_hierarchy(struct cgroupfs_root *root)
  2891. {
  2892. int i;
  2893. for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
  2894. struct cgroup_subsys *ss = subsys[i];
  2895. if (ss == NULL)
  2896. continue;
  2897. if (ss->root == root)
  2898. mutex_unlock(&ss->hierarchy_mutex);
  2899. }
  2900. }
  2901. /*
  2902. * cgroup_create - create a cgroup
  2903. * @parent: cgroup that will be parent of the new cgroup
  2904. * @dentry: dentry of the new cgroup
  2905. * @mode: mode to set on new inode
  2906. *
  2907. * Must be called with the mutex on the parent inode held
  2908. */
  2909. static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
  2910. mode_t mode)
  2911. {
  2912. struct cgroup *cgrp;
  2913. struct cgroupfs_root *root = parent->root;
  2914. int err = 0;
  2915. struct cgroup_subsys *ss;
  2916. struct super_block *sb = root->sb;
  2917. cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
  2918. if (!cgrp)
  2919. return -ENOMEM;
  2920. /* Grab a reference on the superblock so the hierarchy doesn't
  2921. * get deleted on unmount if there are child cgroups. This
  2922. * can be done outside cgroup_mutex, since the sb can't
  2923. * disappear while someone has an open control file on the
  2924. * fs */
  2925. atomic_inc(&sb->s_active);
  2926. mutex_lock(&cgroup_mutex);
  2927. init_cgroup_housekeeping(cgrp);
  2928. cgrp->parent = parent;
  2929. cgrp->root = parent->root;
  2930. cgrp->top_cgroup = parent->top_cgroup;
  2931. if (notify_on_release(parent))
  2932. set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
  2933. for_each_subsys(root, ss) {
  2934. struct cgroup_subsys_state *css = ss->create(ss, cgrp);
  2935. if (IS_ERR(css)) {
  2936. err = PTR_ERR(css);
  2937. goto err_destroy;
  2938. }
  2939. init_cgroup_css(css, ss, cgrp);
  2940. if (ss->use_id) {
  2941. err = alloc_css_id(ss, parent, cgrp);
  2942. if (err)
  2943. goto err_destroy;
  2944. }
  2945. /* At error, ->destroy() callback has to free assigned ID. */
  2946. }
  2947. cgroup_lock_hierarchy(root);
  2948. list_add(&cgrp->sibling, &cgrp->parent->children);
  2949. cgroup_unlock_hierarchy(root);
  2950. root->number_of_cgroups++;
  2951. err = cgroup_create_dir(cgrp, dentry, mode);
  2952. if (err < 0)
  2953. goto err_remove;
  2954. /* The cgroup directory was pre-locked for us */
  2955. BUG_ON(!mutex_is_locked(&cgrp->dentry->d_inode->i_mutex));
  2956. err = cgroup_populate_dir(cgrp);
  2957. /* If err < 0, we have a half-filled directory - oh well ;) */
  2958. mutex_unlock(&cgroup_mutex);
  2959. mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
  2960. return 0;
  2961. err_remove:
  2962. cgroup_lock_hierarchy(root);
  2963. list_del(&cgrp->sibling);
  2964. cgroup_unlock_hierarchy(root);
  2965. root->number_of_cgroups--;
  2966. err_destroy:
  2967. for_each_subsys(root, ss) {
  2968. if (cgrp->subsys[ss->subsys_id])
  2969. ss->destroy(ss, cgrp);
  2970. }
  2971. mutex_unlock(&cgroup_mutex);
  2972. /* Release the reference count that we took on the superblock */
  2973. deactivate_super(sb);
  2974. kfree(cgrp);
  2975. return err;
  2976. }
  2977. static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  2978. {
  2979. struct cgroup *c_parent = dentry->d_parent->d_fsdata;
  2980. /* the vfs holds inode->i_mutex already */
  2981. return cgroup_create(c_parent, dentry, mode | S_IFDIR);
  2982. }
  2983. static int cgroup_has_css_refs(struct cgroup *cgrp)
  2984. {
  2985. /* Check the reference count on each subsystem. Since we
  2986. * already established that there are no tasks in the
  2987. * cgroup, if the css refcount is also 1, then there should
  2988. * be no outstanding references, so the subsystem is safe to
  2989. * destroy. We scan across all subsystems rather than using
  2990. * the per-hierarchy linked list of mounted subsystems since
  2991. * we can be called via check_for_release() with no
  2992. * synchronization other than RCU, and the subsystem linked
  2993. * list isn't RCU-safe */
  2994. int i;
  2995. /*
  2996. * We won't need to lock the subsys array, because the subsystems
  2997. * we're concerned about aren't going anywhere since our cgroup root
  2998. * has a reference on them.
  2999. */
  3000. for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
  3001. struct cgroup_subsys *ss = subsys[i];
  3002. struct cgroup_subsys_state *css;
  3003. /* Skip subsystems not present or not in this hierarchy */
  3004. if (ss == NULL || ss->root != cgrp->root)
  3005. continue;
  3006. css = cgrp->subsys[ss->subsys_id];
  3007. /* When called from check_for_release() it's possible
  3008. * that by this point the cgroup has been removed
  3009. * and the css deleted. But a false-positive doesn't
  3010. * matter, since it can only happen if the cgroup
  3011. * has been deleted and hence no longer needs the
  3012. * release agent to be called anyway. */
  3013. if (css && (atomic_read(&css->refcnt) > 1))
  3014. return 1;
  3015. }
  3016. return 0;
  3017. }
  3018. /*
  3019. * Atomically mark all (or else none) of the cgroup's CSS objects as
  3020. * CSS_REMOVED. Return true on success, or false if the cgroup has
  3021. * busy subsystems. Call with cgroup_mutex held
  3022. */
  3023. static int cgroup_clear_css_refs(struct cgroup *cgrp)
  3024. {
  3025. struct cgroup_subsys *ss;
  3026. unsigned long flags;
  3027. bool failed = false;
  3028. local_irq_save(flags);
  3029. for_each_subsys(cgrp->root, ss) {
  3030. struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
  3031. int refcnt;
  3032. while (1) {
  3033. /* We can only remove a CSS with a refcnt==1 */
  3034. refcnt = atomic_read(&css->refcnt);
  3035. if (refcnt > 1) {
  3036. failed = true;
  3037. goto done;
  3038. }
  3039. BUG_ON(!refcnt);
  3040. /*
  3041. * Drop the refcnt to 0 while we check other
  3042. * subsystems. This will cause any racing
  3043. * css_tryget() to spin until we set the
  3044. * CSS_REMOVED bits or abort
  3045. */
  3046. if (atomic_cmpxchg(&css->refcnt, refcnt, 0) == refcnt)
  3047. break;
  3048. cpu_relax();
  3049. }
  3050. }
  3051. done:
  3052. for_each_subsys(cgrp->root, ss) {
  3053. struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
  3054. if (failed) {
  3055. /*
  3056. * Restore old refcnt if we previously managed
  3057. * to clear it from 1 to 0
  3058. */
  3059. if (!atomic_read(&css->refcnt))
  3060. atomic_set(&css->refcnt, 1);
  3061. } else {
  3062. /* Commit the fact that the CSS is removed */
  3063. set_bit(CSS_REMOVED, &css->flags);
  3064. }
  3065. }
  3066. local_irq_restore(flags);
  3067. return !failed;
  3068. }
  3069. static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
  3070. {
  3071. struct cgroup *cgrp = dentry->d_fsdata;
  3072. struct dentry *d;
  3073. struct cgroup *parent;
  3074. DEFINE_WAIT(wait);
  3075. struct cgroup_event *event, *tmp;
  3076. int ret;
  3077. /* the vfs holds both inode->i_mutex already */
  3078. again:
  3079. mutex_lock(&cgroup_mutex);
  3080. if (atomic_read(&cgrp->count) != 0) {
  3081. mutex_unlock(&cgroup_mutex);
  3082. return -EBUSY;
  3083. }
  3084. if (!list_empty(&cgrp->children)) {
  3085. mutex_unlock(&cgroup_mutex);
  3086. return -EBUSY;
  3087. }
  3088. mutex_unlock(&cgroup_mutex);
  3089. /*
  3090. * In general, subsystem has no css->refcnt after pre_destroy(). But
  3091. * in racy cases, subsystem may have to get css->refcnt after
  3092. * pre_destroy() and it makes rmdir return with -EBUSY. This sometimes
  3093. * make rmdir return -EBUSY too often. To avoid that, we use waitqueue
  3094. * for cgroup's rmdir. CGRP_WAIT_ON_RMDIR is for synchronizing rmdir
  3095. * and subsystem's reference count handling. Please see css_get/put
  3096. * and css_tryget() and cgroup_wakeup_rmdir_waiter() implementation.
  3097. */
  3098. set_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
  3099. /*
  3100. * Call pre_destroy handlers of subsys. Notify subsystems
  3101. * that rmdir() request comes.
  3102. */
  3103. ret = cgroup_call_pre_destroy(cgrp);
  3104. if (ret) {
  3105. clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
  3106. return ret;
  3107. }
  3108. mutex_lock(&cgroup_mutex);
  3109. parent = cgrp->parent;
  3110. if (atomic_read(&cgrp->count) || !list_empty(&cgrp->children)) {
  3111. clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
  3112. mutex_unlock(&cgroup_mutex);
  3113. return -EBUSY;
  3114. }
  3115. prepare_to_wait(&cgroup_rmdir_waitq, &wait, TASK_INTERRUPTIBLE);
  3116. if (!cgroup_clear_css_refs(cgrp)) {
  3117. mutex_unlock(&cgroup_mutex);
  3118. /*
  3119. * Because someone may call cgroup_wakeup_rmdir_waiter() before
  3120. * prepare_to_wait(), we need to check this flag.
  3121. */
  3122. if (test_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags))
  3123. schedule();
  3124. finish_wait(&cgroup_rmdir_waitq, &wait);
  3125. clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
  3126. if (signal_pending(current))
  3127. return -EINTR;
  3128. goto again;
  3129. }
  3130. /* NO css_tryget() can success after here. */
  3131. finish_wait(&cgroup_rmdir_waitq, &wait);
  3132. clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
  3133. spin_lock(&release_list_lock);
  3134. set_bit(CGRP_REMOVED, &cgrp->flags);
  3135. if (!list_empty(&cgrp->release_list))
  3136. list_del(&cgrp->release_list);
  3137. spin_unlock(&release_list_lock);
  3138. cgroup_lock_hierarchy(cgrp->root);
  3139. /* delete this cgroup from parent->children */
  3140. list_del(&cgrp->sibling);
  3141. cgroup_unlock_hierarchy(cgrp->root);
  3142. spin_lock(&cgrp->dentry->d_lock);
  3143. d = dget(cgrp->dentry);
  3144. spin_unlock(&d->d_lock);
  3145. cgroup_d_remove_dir(d);
  3146. dput(d);
  3147. set_bit(CGRP_RELEASABLE, &parent->flags);
  3148. check_for_release(parent);
  3149. /*
  3150. * Unregister events and notify userspace.
  3151. * Notify userspace about cgroup removing only after rmdir of cgroup
  3152. * directory to avoid race between userspace and kernelspace
  3153. */
  3154. spin_lock(&cgrp->event_list_lock);
  3155. list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) {
  3156. list_del(&event->list);
  3157. remove_wait_queue(event->wqh, &event->wait);
  3158. eventfd_signal(event->eventfd, 1);
  3159. schedule_work(&event->remove);
  3160. }
  3161. spin_unlock(&cgrp->event_list_lock);
  3162. mutex_unlock(&cgroup_mutex);
  3163. return 0;
  3164. }
  3165. static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
  3166. {
  3167. struct cgroup_subsys_state *css;
  3168. printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
  3169. /* Create the top cgroup state for this subsystem */
  3170. list_add(&ss->sibling, &rootnode.subsys_list);
  3171. ss->root = &rootnode;
  3172. css = ss->create(ss, dummytop);
  3173. /* We don't handle early failures gracefully */
  3174. BUG_ON(IS_ERR(css));
  3175. init_cgroup_css(css, ss, dummytop);
  3176. /* Update the init_css_set to contain a subsys
  3177. * pointer to this state - since the subsystem is
  3178. * newly registered, all tasks and hence the
  3179. * init_css_set is in the subsystem's top cgroup. */
  3180. init_css_set.subsys[ss->subsys_id] = dummytop->subsys[ss->subsys_id];
  3181. need_forkexit_callback |= ss->fork || ss->exit;
  3182. /* At system boot, before all subsystems have been
  3183. * registered, no tasks have been forked, so we don't
  3184. * need to invoke fork callbacks here. */
  3185. BUG_ON(!list_empty(&init_task.tasks));
  3186. mutex_init(&ss->hierarchy_mutex);
  3187. lockdep_set_class(&ss->hierarchy_mutex, &ss->subsys_key);
  3188. ss->active = 1;
  3189. /* this function shouldn't be used with modular subsystems, since they
  3190. * need to register a subsys_id, among other things */
  3191. BUG_ON(ss->module);
  3192. }
  3193. /**
  3194. * cgroup_load_subsys: load and register a modular subsystem at runtime
  3195. * @ss: the subsystem to load
  3196. *
  3197. * This function should be called in a modular subsystem's initcall. If the
  3198. * subsytem is built as a module, it will be assigned a new subsys_id and set
  3199. * up for use. If the subsystem is built-in anyway, work is delegated to the
  3200. * simpler cgroup_init_subsys.
  3201. */
  3202. int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
  3203. {
  3204. int i;
  3205. struct cgroup_subsys_state *css;
  3206. /* check name and function validity */
  3207. if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
  3208. ss->create == NULL || ss->destroy == NULL)
  3209. return -EINVAL;
  3210. /*
  3211. * we don't support callbacks in modular subsystems. this check is
  3212. * before the ss->module check for consistency; a subsystem that could
  3213. * be a module should still have no callbacks even if the user isn't
  3214. * compiling it as one.
  3215. */
  3216. if (ss->fork || ss->exit)
  3217. return -EINVAL;
  3218. /*
  3219. * an optionally modular subsystem is built-in: we want to do nothing,
  3220. * since cgroup_init_subsys will have already taken care of it.
  3221. */
  3222. if (ss->module == NULL) {
  3223. /* a few sanity checks */
  3224. BUG_ON(ss->subsys_id >= CGROUP_BUILTIN_SUBSYS_COUNT);
  3225. BUG_ON(subsys[ss->subsys_id] != ss);
  3226. return 0;
  3227. }
  3228. /*
  3229. * need to register a subsys id before anything else - for example,
  3230. * init_cgroup_css needs it.
  3231. */
  3232. mutex_lock(&cgroup_mutex);
  3233. /* find the first empty slot in the array */
  3234. for (i = CGROUP_BUILTIN_SUBSYS_COUNT; i < CGROUP_SUBSYS_COUNT; i++) {
  3235. if (subsys[i] == NULL)
  3236. break;
  3237. }
  3238. if (i == CGROUP_SUBSYS_COUNT) {
  3239. /* maximum number of subsystems already registered! */
  3240. mutex_unlock(&cgroup_mutex);
  3241. return -EBUSY;
  3242. }
  3243. /* assign ourselves the subsys_id */
  3244. ss->subsys_id = i;
  3245. subsys[i] = ss;
  3246. /*
  3247. * no ss->create seems to need anything important in the ss struct, so
  3248. * this can happen first (i.e. before the rootnode attachment).
  3249. */
  3250. css = ss->create(ss, dummytop);
  3251. if (IS_ERR(css)) {
  3252. /* failure case - need to deassign the subsys[] slot. */
  3253. subsys[i] = NULL;
  3254. mutex_unlock(&cgroup_mutex);
  3255. return PTR_ERR(css);
  3256. }
  3257. list_add(&ss->sibling, &rootnode.subsys_list);
  3258. ss->root = &rootnode;
  3259. /* our new subsystem will be attached to the dummy hierarchy. */
  3260. init_cgroup_css(css, ss, dummytop);
  3261. /* init_idr must be after init_cgroup_css because it sets css->id. */
  3262. if (ss->use_id) {
  3263. int ret = cgroup_init_idr(ss, css);
  3264. if (ret) {
  3265. dummytop->subsys[ss->subsys_id] = NULL;
  3266. ss->destroy(ss, dummytop);
  3267. subsys[i] = NULL;
  3268. mutex_unlock(&cgroup_mutex);
  3269. return ret;
  3270. }
  3271. }
  3272. /*
  3273. * Now we need to entangle the css into the existing css_sets. unlike
  3274. * in cgroup_init_subsys, there are now multiple css_sets, so each one
  3275. * will need a new pointer to it; done by iterating the css_set_table.
  3276. * furthermore, modifying the existing css_sets will corrupt the hash
  3277. * table state, so each changed css_set will need its hash recomputed.
  3278. * this is all done under the css_set_lock.
  3279. */
  3280. write_lock(&css_set_lock);
  3281. for (i = 0; i < CSS_SET_TABLE_SIZE; i++) {
  3282. struct css_set *cg;
  3283. struct hlist_node *node, *tmp;
  3284. struct hlist_head *bucket = &css_set_table[i], *new_bucket;
  3285. hlist_for_each_entry_safe(cg, node, tmp, bucket, hlist) {
  3286. /* skip entries that we already rehashed */
  3287. if (cg->subsys[ss->subsys_id])
  3288. continue;
  3289. /* remove existing entry */
  3290. hlist_del(&cg->hlist);
  3291. /* set new value */
  3292. cg->subsys[ss->subsys_id] = css;
  3293. /* recompute hash and restore entry */
  3294. new_bucket = css_set_hash(cg->subsys);
  3295. hlist_add_head(&cg->hlist, new_bucket);
  3296. }
  3297. }
  3298. write_unlock(&css_set_lock);
  3299. mutex_init(&ss->hierarchy_mutex);
  3300. lockdep_set_class(&ss->hierarchy_mutex, &ss->subsys_key);
  3301. ss->active = 1;
  3302. /* success! */
  3303. mutex_unlock(&cgroup_mutex);
  3304. return 0;
  3305. }
  3306. EXPORT_SYMBOL_GPL(cgroup_load_subsys);
  3307. /**
  3308. * cgroup_unload_subsys: unload a modular subsystem
  3309. * @ss: the subsystem to unload
  3310. *
  3311. * This function should be called in a modular subsystem's exitcall. When this
  3312. * function is invoked, the refcount on the subsystem's module will be 0, so
  3313. * the subsystem will not be attached to any hierarchy.
  3314. */
  3315. void cgroup_unload_subsys(struct cgroup_subsys *ss)
  3316. {
  3317. struct cg_cgroup_link *link;
  3318. struct hlist_head *hhead;
  3319. BUG_ON(ss->module == NULL);
  3320. /*
  3321. * we shouldn't be called if the subsystem is in use, and the use of
  3322. * try_module_get in parse_cgroupfs_options should ensure that it
  3323. * doesn't start being used while we're killing it off.
  3324. */
  3325. BUG_ON(ss->root != &rootnode);
  3326. mutex_lock(&cgroup_mutex);
  3327. /* deassign the subsys_id */
  3328. BUG_ON(ss->subsys_id < CGROUP_BUILTIN_SUBSYS_COUNT);
  3329. subsys[ss->subsys_id] = NULL;
  3330. /* remove subsystem from rootnode's list of subsystems */
  3331. list_del(&ss->sibling);
  3332. /*
  3333. * disentangle the css from all css_sets attached to the dummytop. as
  3334. * in loading, we need to pay our respects to the hashtable gods.
  3335. */
  3336. write_lock(&css_set_lock);
  3337. list_for_each_entry(link, &dummytop->css_sets, cgrp_link_list) {
  3338. struct css_set *cg = link->cg;
  3339. hlist_del(&cg->hlist);
  3340. BUG_ON(!cg->subsys[ss->subsys_id]);
  3341. cg->subsys[ss->subsys_id] = NULL;
  3342. hhead = css_set_hash(cg->subsys);
  3343. hlist_add_head(&cg->hlist, hhead);
  3344. }
  3345. write_unlock(&css_set_lock);
  3346. /*
  3347. * remove subsystem's css from the dummytop and free it - need to free
  3348. * before marking as null because ss->destroy needs the cgrp->subsys
  3349. * pointer to find their state. note that this also takes care of
  3350. * freeing the css_id.
  3351. */
  3352. ss->destroy(ss, dummytop);
  3353. dummytop->subsys[ss->subsys_id] = NULL;
  3354. mutex_unlock(&cgroup_mutex);
  3355. }
  3356. EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
  3357. /**
  3358. * cgroup_init_early - cgroup initialization at system boot
  3359. *
  3360. * Initialize cgroups at system boot, and initialize any
  3361. * subsystems that request early init.
  3362. */
  3363. int __init cgroup_init_early(void)
  3364. {
  3365. int i;
  3366. atomic_set(&init_css_set.refcount, 1);
  3367. INIT_LIST_HEAD(&init_css_set.cg_links);
  3368. INIT_LIST_HEAD(&init_css_set.tasks);
  3369. INIT_HLIST_NODE(&init_css_set.hlist);
  3370. css_set_count = 1;
  3371. init_cgroup_root(&rootnode);
  3372. root_count = 1;
  3373. init_task.cgroups = &init_css_set;
  3374. init_css_set_link.cg = &init_css_set;
  3375. init_css_set_link.cgrp = dummytop;
  3376. list_add(&init_css_set_link.cgrp_link_list,
  3377. &rootnode.top_cgroup.css_sets);
  3378. list_add(&init_css_set_link.cg_link_list,
  3379. &init_css_set.cg_links);
  3380. for (i = 0; i < CSS_SET_TABLE_SIZE; i++)
  3381. INIT_HLIST_HEAD(&css_set_table[i]);
  3382. /* at bootup time, we don't worry about modular subsystems */
  3383. for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
  3384. struct cgroup_subsys *ss = subsys[i];
  3385. BUG_ON(!ss->name);
  3386. BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
  3387. BUG_ON(!ss->create);
  3388. BUG_ON(!ss->destroy);
  3389. if (ss->subsys_id != i) {
  3390. printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
  3391. ss->name, ss->subsys_id);
  3392. BUG();
  3393. }
  3394. if (ss->early_init)
  3395. cgroup_init_subsys(ss);
  3396. }
  3397. return 0;
  3398. }
  3399. /**
  3400. * cgroup_init - cgroup initialization
  3401. *
  3402. * Register cgroup filesystem and /proc file, and initialize
  3403. * any subsystems that didn't request early init.
  3404. */
  3405. int __init cgroup_init(void)
  3406. {
  3407. int err;
  3408. int i;
  3409. struct hlist_head *hhead;
  3410. err = bdi_init(&cgroup_backing_dev_info);
  3411. if (err)
  3412. return err;
  3413. /* at bootup time, we don't worry about modular subsystems */
  3414. for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
  3415. struct cgroup_subsys *ss = subsys[i];
  3416. if (!ss->early_init)
  3417. cgroup_init_subsys(ss);
  3418. if (ss->use_id)
  3419. cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]);
  3420. }
  3421. /* Add init_css_set to the hash table */
  3422. hhead = css_set_hash(init_css_set.subsys);
  3423. hlist_add_head(&init_css_set.hlist, hhead);
  3424. BUG_ON(!init_root_id(&rootnode));
  3425. err = register_filesystem(&cgroup_fs_type);
  3426. if (err < 0)
  3427. goto out;
  3428. proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
  3429. out:
  3430. if (err)
  3431. bdi_destroy(&cgroup_backing_dev_info);
  3432. return err;
  3433. }
  3434. /*
  3435. * proc_cgroup_show()
  3436. * - Print task's cgroup paths into seq_file, one line for each hierarchy
  3437. * - Used for /proc/<pid>/cgroup.
  3438. * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
  3439. * doesn't really matter if tsk->cgroup changes after we read it,
  3440. * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
  3441. * anyway. No need to check that tsk->cgroup != NULL, thanks to
  3442. * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
  3443. * cgroup to top_cgroup.
  3444. */
  3445. /* TODO: Use a proper seq_file iterator */
  3446. static int proc_cgroup_show(struct seq_file *m, void *v)
  3447. {
  3448. struct pid *pid;
  3449. struct task_struct *tsk;
  3450. char *buf;
  3451. int retval;
  3452. struct cgroupfs_root *root;
  3453. retval = -ENOMEM;
  3454. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  3455. if (!buf)
  3456. goto out;
  3457. retval = -ESRCH;
  3458. pid = m->private;
  3459. tsk = get_pid_task(pid, PIDTYPE_PID);
  3460. if (!tsk)
  3461. goto out_free;
  3462. retval = 0;
  3463. mutex_lock(&cgroup_mutex);
  3464. for_each_active_root(root) {
  3465. struct cgroup_subsys *ss;
  3466. struct cgroup *cgrp;
  3467. int count = 0;
  3468. seq_printf(m, "%d:", root->hierarchy_id);
  3469. for_each_subsys(root, ss)
  3470. seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
  3471. if (strlen(root->name))
  3472. seq_printf(m, "%sname=%s", count ? "," : "",
  3473. root->name);
  3474. seq_putc(m, ':');
  3475. cgrp = task_cgroup_from_root(tsk, root);
  3476. retval = cgroup_path(cgrp, buf, PAGE_SIZE);
  3477. if (retval < 0)
  3478. goto out_unlock;
  3479. seq_puts(m, buf);
  3480. seq_putc(m, '\n');
  3481. }
  3482. out_unlock:
  3483. mutex_unlock(&cgroup_mutex);
  3484. put_task_struct(tsk);
  3485. out_free:
  3486. kfree(buf);
  3487. out:
  3488. return retval;
  3489. }
  3490. static int cgroup_open(struct inode *inode, struct file *file)
  3491. {
  3492. struct pid *pid = PROC_I(inode)->pid;
  3493. return single_open(file, proc_cgroup_show, pid);
  3494. }
  3495. const struct file_operations proc_cgroup_operations = {
  3496. .open = cgroup_open,
  3497. .read = seq_read,
  3498. .llseek = seq_lseek,
  3499. .release = single_release,
  3500. };
  3501. /* Display information about each subsystem and each hierarchy */
  3502. static int proc_cgroupstats_show(struct seq_file *m, void *v)
  3503. {
  3504. int i;
  3505. seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
  3506. /*
  3507. * ideally we don't want subsystems moving around while we do this.
  3508. * cgroup_mutex is also necessary to guarantee an atomic snapshot of
  3509. * subsys/hierarchy state.
  3510. */
  3511. mutex_lock(&cgroup_mutex);
  3512. for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
  3513. struct cgroup_subsys *ss = subsys[i];
  3514. if (ss == NULL)
  3515. continue;
  3516. seq_printf(m, "%s\t%d\t%d\t%d\n",
  3517. ss->name, ss->root->hierarchy_id,
  3518. ss->root->number_of_cgroups, !ss->disabled);
  3519. }
  3520. mutex_unlock(&cgroup_mutex);
  3521. return 0;
  3522. }
  3523. static int cgroupstats_open(struct inode *inode, struct file *file)
  3524. {
  3525. return single_open(file, proc_cgroupstats_show, NULL);
  3526. }
  3527. static const struct file_operations proc_cgroupstats_operations = {
  3528. .open = cgroupstats_open,
  3529. .read = seq_read,
  3530. .llseek = seq_lseek,
  3531. .release = single_release,
  3532. };
  3533. /**
  3534. * cgroup_fork - attach newly forked task to its parents cgroup.
  3535. * @child: pointer to task_struct of forking parent process.
  3536. *
  3537. * Description: A task inherits its parent's cgroup at fork().
  3538. *
  3539. * A pointer to the shared css_set was automatically copied in
  3540. * fork.c by dup_task_struct(). However, we ignore that copy, since
  3541. * it was not made under the protection of RCU or cgroup_mutex, so
  3542. * might no longer be a valid cgroup pointer. cgroup_attach_task() might
  3543. * have already changed current->cgroups, allowing the previously
  3544. * referenced cgroup group to be removed and freed.
  3545. *
  3546. * At the point that cgroup_fork() is called, 'current' is the parent
  3547. * task, and the passed argument 'child' points to the child task.
  3548. */
  3549. void cgroup_fork(struct task_struct *child)
  3550. {
  3551. task_lock(current);
  3552. child->cgroups = current->cgroups;
  3553. get_css_set(child->cgroups);
  3554. task_unlock(current);
  3555. INIT_LIST_HEAD(&child->cg_list);
  3556. }
  3557. /**
  3558. * cgroup_fork_callbacks - run fork callbacks
  3559. * @child: the new task
  3560. *
  3561. * Called on a new task very soon before adding it to the
  3562. * tasklist. No need to take any locks since no-one can
  3563. * be operating on this task.
  3564. */
  3565. void cgroup_fork_callbacks(struct task_struct *child)
  3566. {
  3567. if (need_forkexit_callback) {
  3568. int i;
  3569. /*
  3570. * forkexit callbacks are only supported for builtin
  3571. * subsystems, and the builtin section of the subsys array is
  3572. * immutable, so we don't need to lock the subsys array here.
  3573. */
  3574. for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
  3575. struct cgroup_subsys *ss = subsys[i];
  3576. if (ss->fork)
  3577. ss->fork(ss, child);
  3578. }
  3579. }
  3580. }
  3581. /**
  3582. * cgroup_post_fork - called on a new task after adding it to the task list
  3583. * @child: the task in question
  3584. *
  3585. * Adds the task to the list running through its css_set if necessary.
  3586. * Has to be after the task is visible on the task list in case we race
  3587. * with the first call to cgroup_iter_start() - to guarantee that the
  3588. * new task ends up on its list.
  3589. */
  3590. void cgroup_post_fork(struct task_struct *child)
  3591. {
  3592. if (use_task_css_set_links) {
  3593. write_lock(&css_set_lock);
  3594. task_lock(child);
  3595. if (list_empty(&child->cg_list))
  3596. list_add(&child->cg_list, &child->cgroups->tasks);
  3597. task_unlock(child);
  3598. write_unlock(&css_set_lock);
  3599. }
  3600. }
  3601. /**
  3602. * cgroup_exit - detach cgroup from exiting task
  3603. * @tsk: pointer to task_struct of exiting process
  3604. * @run_callback: run exit callbacks?
  3605. *
  3606. * Description: Detach cgroup from @tsk and release it.
  3607. *
  3608. * Note that cgroups marked notify_on_release force every task in
  3609. * them to take the global cgroup_mutex mutex when exiting.
  3610. * This could impact scaling on very large systems. Be reluctant to
  3611. * use notify_on_release cgroups where very high task exit scaling
  3612. * is required on large systems.
  3613. *
  3614. * the_top_cgroup_hack:
  3615. *
  3616. * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
  3617. *
  3618. * We call cgroup_exit() while the task is still competent to
  3619. * handle notify_on_release(), then leave the task attached to the
  3620. * root cgroup in each hierarchy for the remainder of its exit.
  3621. *
  3622. * To do this properly, we would increment the reference count on
  3623. * top_cgroup, and near the very end of the kernel/exit.c do_exit()
  3624. * code we would add a second cgroup function call, to drop that
  3625. * reference. This would just create an unnecessary hot spot on
  3626. * the top_cgroup reference count, to no avail.
  3627. *
  3628. * Normally, holding a reference to a cgroup without bumping its
  3629. * count is unsafe. The cgroup could go away, or someone could
  3630. * attach us to a different cgroup, decrementing the count on
  3631. * the first cgroup that we never incremented. But in this case,
  3632. * top_cgroup isn't going away, and either task has PF_EXITING set,
  3633. * which wards off any cgroup_attach_task() attempts, or task is a failed
  3634. * fork, never visible to cgroup_attach_task.
  3635. */
  3636. void cgroup_exit(struct task_struct *tsk, int run_callbacks)
  3637. {
  3638. int i;
  3639. struct css_set *cg;
  3640. if (run_callbacks && need_forkexit_callback) {
  3641. /*
  3642. * modular subsystems can't use callbacks, so no need to lock
  3643. * the subsys array
  3644. */
  3645. for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
  3646. struct cgroup_subsys *ss = subsys[i];
  3647. if (ss->exit)
  3648. ss->exit(ss, tsk);
  3649. }
  3650. }
  3651. /*
  3652. * Unlink from the css_set task list if necessary.
  3653. * Optimistically check cg_list before taking
  3654. * css_set_lock
  3655. */
  3656. if (!list_empty(&tsk->cg_list)) {
  3657. write_lock(&css_set_lock);
  3658. if (!list_empty(&tsk->cg_list))
  3659. list_del(&tsk->cg_list);
  3660. write_unlock(&css_set_lock);
  3661. }
  3662. /* Reassign the task to the init_css_set. */
  3663. task_lock(tsk);
  3664. cg = tsk->cgroups;
  3665. tsk->cgroups = &init_css_set;
  3666. task_unlock(tsk);
  3667. if (cg)
  3668. put_css_set_taskexit(cg);
  3669. }
  3670. /**
  3671. * cgroup_clone - clone the cgroup the given subsystem is attached to
  3672. * @tsk: the task to be moved
  3673. * @subsys: the given subsystem
  3674. * @nodename: the name for the new cgroup
  3675. *
  3676. * Duplicate the current cgroup in the hierarchy that the given
  3677. * subsystem is attached to, and move this task into the new
  3678. * child.
  3679. */
  3680. int cgroup_clone(struct task_struct *tsk, struct cgroup_subsys *subsys,
  3681. char *nodename)
  3682. {
  3683. struct dentry *dentry;
  3684. int ret = 0;
  3685. struct cgroup *parent, *child;
  3686. struct inode *inode;
  3687. struct css_set *cg;
  3688. struct cgroupfs_root *root;
  3689. struct cgroup_subsys *ss;
  3690. /* We shouldn't be called by an unregistered subsystem */
  3691. BUG_ON(!subsys->active);
  3692. /* First figure out what hierarchy and cgroup we're dealing
  3693. * with, and pin them so we can drop cgroup_mutex */
  3694. mutex_lock(&cgroup_mutex);
  3695. again:
  3696. root = subsys->root;
  3697. if (root == &rootnode) {
  3698. mutex_unlock(&cgroup_mutex);
  3699. return 0;
  3700. }
  3701. /* Pin the hierarchy */
  3702. if (!atomic_inc_not_zero(&root->sb->s_active)) {
  3703. /* We race with the final deactivate_super() */
  3704. mutex_unlock(&cgroup_mutex);
  3705. return 0;
  3706. }
  3707. /* Keep the cgroup alive */
  3708. task_lock(tsk);
  3709. parent = task_cgroup(tsk, subsys->subsys_id);
  3710. cg = tsk->cgroups;
  3711. get_css_set(cg);
  3712. task_unlock(tsk);
  3713. mutex_unlock(&cgroup_mutex);
  3714. /* Now do the VFS work to create a cgroup */
  3715. inode = parent->dentry->d_inode;
  3716. /* Hold the parent directory mutex across this operation to
  3717. * stop anyone else deleting the new cgroup */
  3718. mutex_lock(&inode->i_mutex);
  3719. dentry = lookup_one_len(nodename, parent->dentry, strlen(nodename));
  3720. if (IS_ERR(dentry)) {
  3721. printk(KERN_INFO
  3722. "cgroup: Couldn't allocate dentry for %s: %ld\n", nodename,
  3723. PTR_ERR(dentry));
  3724. ret = PTR_ERR(dentry);
  3725. goto out_release;
  3726. }
  3727. /* Create the cgroup directory, which also creates the cgroup */
  3728. ret = vfs_mkdir(inode, dentry, 0755);
  3729. child = __d_cgrp(dentry);
  3730. dput(dentry);
  3731. if (ret) {
  3732. printk(KERN_INFO
  3733. "Failed to create cgroup %s: %d\n", nodename,
  3734. ret);
  3735. goto out_release;
  3736. }
  3737. /* The cgroup now exists. Retake cgroup_mutex and check
  3738. * that we're still in the same state that we thought we
  3739. * were. */
  3740. mutex_lock(&cgroup_mutex);
  3741. if ((root != subsys->root) ||
  3742. (parent != task_cgroup(tsk, subsys->subsys_id))) {
  3743. /* Aargh, we raced ... */
  3744. mutex_unlock(&inode->i_mutex);
  3745. put_css_set(cg);
  3746. deactivate_super(root->sb);
  3747. /* The cgroup is still accessible in the VFS, but
  3748. * we're not going to try to rmdir() it at this
  3749. * point. */
  3750. printk(KERN_INFO
  3751. "Race in cgroup_clone() - leaking cgroup %s\n",
  3752. nodename);
  3753. goto again;
  3754. }
  3755. /* do any required auto-setup */
  3756. for_each_subsys(root, ss) {
  3757. if (ss->post_clone)
  3758. ss->post_clone(ss, child);
  3759. }
  3760. /* All seems fine. Finish by moving the task into the new cgroup */
  3761. ret = cgroup_attach_task(child, tsk);
  3762. mutex_unlock(&cgroup_mutex);
  3763. out_release:
  3764. mutex_unlock(&inode->i_mutex);
  3765. mutex_lock(&cgroup_mutex);
  3766. put_css_set(cg);
  3767. mutex_unlock(&cgroup_mutex);
  3768. deactivate_super(root->sb);
  3769. return ret;
  3770. }
  3771. /**
  3772. * cgroup_is_descendant - see if @cgrp is a descendant of @task's cgrp
  3773. * @cgrp: the cgroup in question
  3774. * @task: the task in question
  3775. *
  3776. * See if @cgrp is a descendant of @task's cgroup in the appropriate
  3777. * hierarchy.
  3778. *
  3779. * If we are sending in dummytop, then presumably we are creating
  3780. * the top cgroup in the subsystem.
  3781. *
  3782. * Called only by the ns (nsproxy) cgroup.
  3783. */
  3784. int cgroup_is_descendant(const struct cgroup *cgrp, struct task_struct *task)
  3785. {
  3786. int ret;
  3787. struct cgroup *target;
  3788. if (cgrp == dummytop)
  3789. return 1;
  3790. target = task_cgroup_from_root(task, cgrp->root);
  3791. while (cgrp != target && cgrp!= cgrp->top_cgroup)
  3792. cgrp = cgrp->parent;
  3793. ret = (cgrp == target);
  3794. return ret;
  3795. }
  3796. static void check_for_release(struct cgroup *cgrp)
  3797. {
  3798. /* All of these checks rely on RCU to keep the cgroup
  3799. * structure alive */
  3800. if (cgroup_is_releasable(cgrp) && !atomic_read(&cgrp->count)
  3801. && list_empty(&cgrp->children) && !cgroup_has_css_refs(cgrp)) {
  3802. /* Control Group is currently removeable. If it's not
  3803. * already queued for a userspace notification, queue
  3804. * it now */
  3805. int need_schedule_work = 0;
  3806. spin_lock(&release_list_lock);
  3807. if (!cgroup_is_removed(cgrp) &&
  3808. list_empty(&cgrp->release_list)) {
  3809. list_add(&cgrp->release_list, &release_list);
  3810. need_schedule_work = 1;
  3811. }
  3812. spin_unlock(&release_list_lock);
  3813. if (need_schedule_work)
  3814. schedule_work(&release_agent_work);
  3815. }
  3816. }
  3817. /* Caller must verify that the css is not for root cgroup */
  3818. void __css_put(struct cgroup_subsys_state *css, int count)
  3819. {
  3820. struct cgroup *cgrp = css->cgroup;
  3821. int val;
  3822. rcu_read_lock();
  3823. val = atomic_sub_return(count, &css->refcnt);
  3824. if (val == 1) {
  3825. if (notify_on_release(cgrp)) {
  3826. set_bit(CGRP_RELEASABLE, &cgrp->flags);
  3827. check_for_release(cgrp);
  3828. }
  3829. cgroup_wakeup_rmdir_waiter(cgrp);
  3830. }
  3831. rcu_read_unlock();
  3832. WARN_ON_ONCE(val < 1);
  3833. }
  3834. EXPORT_SYMBOL_GPL(__css_put);
  3835. /*
  3836. * Notify userspace when a cgroup is released, by running the
  3837. * configured release agent with the name of the cgroup (path
  3838. * relative to the root of cgroup file system) as the argument.
  3839. *
  3840. * Most likely, this user command will try to rmdir this cgroup.
  3841. *
  3842. * This races with the possibility that some other task will be
  3843. * attached to this cgroup before it is removed, or that some other
  3844. * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
  3845. * The presumed 'rmdir' will fail quietly if this cgroup is no longer
  3846. * unused, and this cgroup will be reprieved from its death sentence,
  3847. * to continue to serve a useful existence. Next time it's released,
  3848. * we will get notified again, if it still has 'notify_on_release' set.
  3849. *
  3850. * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
  3851. * means only wait until the task is successfully execve()'d. The
  3852. * separate release agent task is forked by call_usermodehelper(),
  3853. * then control in this thread returns here, without waiting for the
  3854. * release agent task. We don't bother to wait because the caller of
  3855. * this routine has no use for the exit status of the release agent
  3856. * task, so no sense holding our caller up for that.
  3857. */
  3858. static void cgroup_release_agent(struct work_struct *work)
  3859. {
  3860. BUG_ON(work != &release_agent_work);
  3861. mutex_lock(&cgroup_mutex);
  3862. spin_lock(&release_list_lock);
  3863. while (!list_empty(&release_list)) {
  3864. char *argv[3], *envp[3];
  3865. int i;
  3866. char *pathbuf = NULL, *agentbuf = NULL;
  3867. struct cgroup *cgrp = list_entry(release_list.next,
  3868. struct cgroup,
  3869. release_list);
  3870. list_del_init(&cgrp->release_list);
  3871. spin_unlock(&release_list_lock);
  3872. pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  3873. if (!pathbuf)
  3874. goto continue_free;
  3875. if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
  3876. goto continue_free;
  3877. agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
  3878. if (!agentbuf)
  3879. goto continue_free;
  3880. i = 0;
  3881. argv[i++] = agentbuf;
  3882. argv[i++] = pathbuf;
  3883. argv[i] = NULL;
  3884. i = 0;
  3885. /* minimal command environment */
  3886. envp[i++] = "HOME=/";
  3887. envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
  3888. envp[i] = NULL;
  3889. /* Drop the lock while we invoke the usermode helper,
  3890. * since the exec could involve hitting disk and hence
  3891. * be a slow process */
  3892. mutex_unlock(&cgroup_mutex);
  3893. call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
  3894. mutex_lock(&cgroup_mutex);
  3895. continue_free:
  3896. kfree(pathbuf);
  3897. kfree(agentbuf);
  3898. spin_lock(&release_list_lock);
  3899. }
  3900. spin_unlock(&release_list_lock);
  3901. mutex_unlock(&cgroup_mutex);
  3902. }
  3903. static int __init cgroup_disable(char *str)
  3904. {
  3905. int i;
  3906. char *token;
  3907. while ((token = strsep(&str, ",")) != NULL) {
  3908. if (!*token)
  3909. continue;
  3910. /*
  3911. * cgroup_disable, being at boot time, can't know about module
  3912. * subsystems, so we don't worry about them.
  3913. */
  3914. for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
  3915. struct cgroup_subsys *ss = subsys[i];
  3916. if (!strcmp(token, ss->name)) {
  3917. ss->disabled = 1;
  3918. printk(KERN_INFO "Disabling %s control group"
  3919. " subsystem\n", ss->name);
  3920. break;
  3921. }
  3922. }
  3923. }
  3924. return 1;
  3925. }
  3926. __setup("cgroup_disable=", cgroup_disable);
  3927. /*
  3928. * Functons for CSS ID.
  3929. */
  3930. /*
  3931. *To get ID other than 0, this should be called when !cgroup_is_removed().
  3932. */
  3933. unsigned short css_id(struct cgroup_subsys_state *css)
  3934. {
  3935. struct css_id *cssid;
  3936. /*
  3937. * This css_id() can return correct value when somone has refcnt
  3938. * on this or this is under rcu_read_lock(). Once css->id is allocated,
  3939. * it's unchanged until freed.
  3940. */
  3941. cssid = rcu_dereference_check(css->id,
  3942. rcu_read_lock_held() || atomic_read(&css->refcnt));
  3943. if (cssid)
  3944. return cssid->id;
  3945. return 0;
  3946. }
  3947. EXPORT_SYMBOL_GPL(css_id);
  3948. unsigned short css_depth(struct cgroup_subsys_state *css)
  3949. {
  3950. struct css_id *cssid;
  3951. cssid = rcu_dereference_check(css->id,
  3952. rcu_read_lock_held() || atomic_read(&css->refcnt));
  3953. if (cssid)
  3954. return cssid->depth;
  3955. return 0;
  3956. }
  3957. EXPORT_SYMBOL_GPL(css_depth);
  3958. /**
  3959. * css_is_ancestor - test "root" css is an ancestor of "child"
  3960. * @child: the css to be tested.
  3961. * @root: the css supporsed to be an ancestor of the child.
  3962. *
  3963. * Returns true if "root" is an ancestor of "child" in its hierarchy. Because
  3964. * this function reads css->id, this use rcu_dereference() and rcu_read_lock().
  3965. * But, considering usual usage, the csses should be valid objects after test.
  3966. * Assuming that the caller will do some action to the child if this returns
  3967. * returns true, the caller must take "child";s reference count.
  3968. * If "child" is valid object and this returns true, "root" is valid, too.
  3969. */
  3970. bool css_is_ancestor(struct cgroup_subsys_state *child,
  3971. const struct cgroup_subsys_state *root)
  3972. {
  3973. struct css_id *child_id;
  3974. struct css_id *root_id;
  3975. bool ret = true;
  3976. rcu_read_lock();
  3977. child_id = rcu_dereference(child->id);
  3978. root_id = rcu_dereference(root->id);
  3979. if (!child_id
  3980. || !root_id
  3981. || (child_id->depth < root_id->depth)
  3982. || (child_id->stack[root_id->depth] != root_id->id))
  3983. ret = false;
  3984. rcu_read_unlock();
  3985. return ret;
  3986. }
  3987. static void __free_css_id_cb(struct rcu_head *head)
  3988. {
  3989. struct css_id *id;
  3990. id = container_of(head, struct css_id, rcu_head);
  3991. kfree(id);
  3992. }
  3993. void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
  3994. {
  3995. struct css_id *id = css->id;
  3996. /* When this is called before css_id initialization, id can be NULL */
  3997. if (!id)
  3998. return;
  3999. BUG_ON(!ss->use_id);
  4000. rcu_assign_pointer(id->css, NULL);
  4001. rcu_assign_pointer(css->id, NULL);
  4002. spin_lock(&ss->id_lock);
  4003. idr_remove(&ss->idr, id->id);
  4004. spin_unlock(&ss->id_lock);
  4005. call_rcu(&id->rcu_head, __free_css_id_cb);
  4006. }
  4007. EXPORT_SYMBOL_GPL(free_css_id);
  4008. /*
  4009. * This is called by init or create(). Then, calls to this function are
  4010. * always serialized (By cgroup_mutex() at create()).
  4011. */
  4012. static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
  4013. {
  4014. struct css_id *newid;
  4015. int myid, error, size;
  4016. BUG_ON(!ss->use_id);
  4017. size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
  4018. newid = kzalloc(size, GFP_KERNEL);
  4019. if (!newid)
  4020. return ERR_PTR(-ENOMEM);
  4021. /* get id */
  4022. if (unlikely(!idr_pre_get(&ss->idr, GFP_KERNEL))) {
  4023. error = -ENOMEM;
  4024. goto err_out;
  4025. }
  4026. spin_lock(&ss->id_lock);
  4027. /* Don't use 0. allocates an ID of 1-65535 */
  4028. error = idr_get_new_above(&ss->idr, newid, 1, &myid);
  4029. spin_unlock(&ss->id_lock);
  4030. /* Returns error when there are no free spaces for new ID.*/
  4031. if (error) {
  4032. error = -ENOSPC;
  4033. goto err_out;
  4034. }
  4035. if (myid > CSS_ID_MAX)
  4036. goto remove_idr;
  4037. newid->id = myid;
  4038. newid->depth = depth;
  4039. return newid;
  4040. remove_idr:
  4041. error = -ENOSPC;
  4042. spin_lock(&ss->id_lock);
  4043. idr_remove(&ss->idr, myid);
  4044. spin_unlock(&ss->id_lock);
  4045. err_out:
  4046. kfree(newid);
  4047. return ERR_PTR(error);
  4048. }
  4049. static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss,
  4050. struct cgroup_subsys_state *rootcss)
  4051. {
  4052. struct css_id *newid;
  4053. spin_lock_init(&ss->id_lock);
  4054. idr_init(&ss->idr);
  4055. newid = get_new_cssid(ss, 0);
  4056. if (IS_ERR(newid))
  4057. return PTR_ERR(newid);
  4058. newid->stack[0] = newid->id;
  4059. newid->css = rootcss;
  4060. rootcss->id = newid;
  4061. return 0;
  4062. }
  4063. static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent,
  4064. struct cgroup *child)
  4065. {
  4066. int subsys_id, i, depth = 0;
  4067. struct cgroup_subsys_state *parent_css, *child_css;
  4068. struct css_id *child_id, *parent_id;
  4069. subsys_id = ss->subsys_id;
  4070. parent_css = parent->subsys[subsys_id];
  4071. child_css = child->subsys[subsys_id];
  4072. parent_id = parent_css->id;
  4073. depth = parent_id->depth;
  4074. child_id = get_new_cssid(ss, depth);
  4075. if (IS_ERR(child_id))
  4076. return PTR_ERR(child_id);
  4077. for (i = 0; i < depth; i++)
  4078. child_id->stack[i] = parent_id->stack[i];
  4079. child_id->stack[depth] = child_id->id;
  4080. /*
  4081. * child_id->css pointer will be set after this cgroup is available
  4082. * see cgroup_populate_dir()
  4083. */
  4084. rcu_assign_pointer(child_css->id, child_id);
  4085. return 0;
  4086. }
  4087. /**
  4088. * css_lookup - lookup css by id
  4089. * @ss: cgroup subsys to be looked into.
  4090. * @id: the id
  4091. *
  4092. * Returns pointer to cgroup_subsys_state if there is valid one with id.
  4093. * NULL if not. Should be called under rcu_read_lock()
  4094. */
  4095. struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
  4096. {
  4097. struct css_id *cssid = NULL;
  4098. BUG_ON(!ss->use_id);
  4099. cssid = idr_find(&ss->idr, id);
  4100. if (unlikely(!cssid))
  4101. return NULL;
  4102. return rcu_dereference(cssid->css);
  4103. }
  4104. EXPORT_SYMBOL_GPL(css_lookup);
  4105. /**
  4106. * css_get_next - lookup next cgroup under specified hierarchy.
  4107. * @ss: pointer to subsystem
  4108. * @id: current position of iteration.
  4109. * @root: pointer to css. search tree under this.
  4110. * @foundid: position of found object.
  4111. *
  4112. * Search next css under the specified hierarchy of rootid. Calling under
  4113. * rcu_read_lock() is necessary. Returns NULL if it reaches the end.
  4114. */
  4115. struct cgroup_subsys_state *
  4116. css_get_next(struct cgroup_subsys *ss, int id,
  4117. struct cgroup_subsys_state *root, int *foundid)
  4118. {
  4119. struct cgroup_subsys_state *ret = NULL;
  4120. struct css_id *tmp;
  4121. int tmpid;
  4122. int rootid = css_id(root);
  4123. int depth = css_depth(root);
  4124. if (!rootid)
  4125. return NULL;
  4126. BUG_ON(!ss->use_id);
  4127. /* fill start point for scan */
  4128. tmpid = id;
  4129. while (1) {
  4130. /*
  4131. * scan next entry from bitmap(tree), tmpid is updated after
  4132. * idr_get_next().
  4133. */
  4134. spin_lock(&ss->id_lock);
  4135. tmp = idr_get_next(&ss->idr, &tmpid);
  4136. spin_unlock(&ss->id_lock);
  4137. if (!tmp)
  4138. break;
  4139. if (tmp->depth >= depth && tmp->stack[depth] == rootid) {
  4140. ret = rcu_dereference(tmp->css);
  4141. if (ret) {
  4142. *foundid = tmpid;
  4143. break;
  4144. }
  4145. }
  4146. /* continue to scan from next id */
  4147. tmpid = tmpid + 1;
  4148. }
  4149. return ret;
  4150. }
  4151. #ifdef CONFIG_CGROUP_DEBUG
  4152. static struct cgroup_subsys_state *debug_create(struct cgroup_subsys *ss,
  4153. struct cgroup *cont)
  4154. {
  4155. struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
  4156. if (!css)
  4157. return ERR_PTR(-ENOMEM);
  4158. return css;
  4159. }
  4160. static void debug_destroy(struct cgroup_subsys *ss, struct cgroup *cont)
  4161. {
  4162. kfree(cont->subsys[debug_subsys_id]);
  4163. }
  4164. static u64 cgroup_refcount_read(struct cgroup *cont, struct cftype *cft)
  4165. {
  4166. return atomic_read(&cont->count);
  4167. }
  4168. static u64 debug_taskcount_read(struct cgroup *cont, struct cftype *cft)
  4169. {
  4170. return cgroup_task_count(cont);
  4171. }
  4172. static u64 current_css_set_read(struct cgroup *cont, struct cftype *cft)
  4173. {
  4174. return (u64)(unsigned long)current->cgroups;
  4175. }
  4176. static u64 current_css_set_refcount_read(struct cgroup *cont,
  4177. struct cftype *cft)
  4178. {
  4179. u64 count;
  4180. rcu_read_lock();
  4181. count = atomic_read(&current->cgroups->refcount);
  4182. rcu_read_unlock();
  4183. return count;
  4184. }
  4185. static int current_css_set_cg_links_read(struct cgroup *cont,
  4186. struct cftype *cft,
  4187. struct seq_file *seq)
  4188. {
  4189. struct cg_cgroup_link *link;
  4190. struct css_set *cg;
  4191. read_lock(&css_set_lock);
  4192. rcu_read_lock();
  4193. cg = rcu_dereference(current->cgroups);
  4194. list_for_each_entry(link, &cg->cg_links, cg_link_list) {
  4195. struct cgroup *c = link->cgrp;
  4196. const char *name;
  4197. if (c->dentry)
  4198. name = c->dentry->d_name.name;
  4199. else
  4200. name = "?";
  4201. seq_printf(seq, "Root %d group %s\n",
  4202. c->root->hierarchy_id, name);
  4203. }
  4204. rcu_read_unlock();
  4205. read_unlock(&css_set_lock);
  4206. return 0;
  4207. }
  4208. #define MAX_TASKS_SHOWN_PER_CSS 25
  4209. static int cgroup_css_links_read(struct cgroup *cont,
  4210. struct cftype *cft,
  4211. struct seq_file *seq)
  4212. {
  4213. struct cg_cgroup_link *link;
  4214. read_lock(&css_set_lock);
  4215. list_for_each_entry(link, &cont->css_sets, cgrp_link_list) {
  4216. struct css_set *cg = link->cg;
  4217. struct task_struct *task;
  4218. int count = 0;
  4219. seq_printf(seq, "css_set %p\n", cg);
  4220. list_for_each_entry(task, &cg->tasks, cg_list) {
  4221. if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
  4222. seq_puts(seq, " ...\n");
  4223. break;
  4224. } else {
  4225. seq_printf(seq, " task %d\n",
  4226. task_pid_vnr(task));
  4227. }
  4228. }
  4229. }
  4230. read_unlock(&css_set_lock);
  4231. return 0;
  4232. }
  4233. static u64 releasable_read(struct cgroup *cgrp, struct cftype *cft)
  4234. {
  4235. return test_bit(CGRP_RELEASABLE, &cgrp->flags);
  4236. }
  4237. static struct cftype debug_files[] = {
  4238. {
  4239. .name = "cgroup_refcount",
  4240. .read_u64 = cgroup_refcount_read,
  4241. },
  4242. {
  4243. .name = "taskcount",
  4244. .read_u64 = debug_taskcount_read,
  4245. },
  4246. {
  4247. .name = "current_css_set",
  4248. .read_u64 = current_css_set_read,
  4249. },
  4250. {
  4251. .name = "current_css_set_refcount",
  4252. .read_u64 = current_css_set_refcount_read,
  4253. },
  4254. {
  4255. .name = "current_css_set_cg_links",
  4256. .read_seq_string = current_css_set_cg_links_read,
  4257. },
  4258. {
  4259. .name = "cgroup_css_links",
  4260. .read_seq_string = cgroup_css_links_read,
  4261. },
  4262. {
  4263. .name = "releasable",
  4264. .read_u64 = releasable_read,
  4265. },
  4266. };
  4267. static int debug_populate(struct cgroup_subsys *ss, struct cgroup *cont)
  4268. {
  4269. return cgroup_add_files(cont, ss, debug_files,
  4270. ARRAY_SIZE(debug_files));
  4271. }
  4272. struct cgroup_subsys debug_subsys = {
  4273. .name = "debug",
  4274. .create = debug_create,
  4275. .destroy = debug_destroy,
  4276. .populate = debug_populate,
  4277. .subsys_id = debug_subsys_id,
  4278. };
  4279. #endif /* CONFIG_CGROUP_DEBUG */