cgroup.c 139 KB

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