cgroup.c 158 KB

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