cgroup.c 127 KB

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