cgroup.c 129 KB

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