cgroup.c 158 KB

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