cgroup.c 149 KB

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