cgroup.c 130 KB

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