cgroup.c 130 KB

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