cgroup.c 138 KB

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