cgroup.c 145 KB

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