cgroup.c 158 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883
  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/file.h>
  63. #include <linux/atomic.h>
  64. /*
  65. * cgroup_mutex is the master lock. Any modification to cgroup or its
  66. * hierarchy must be performed while holding it.
  67. *
  68. * cgroup_root_mutex nests inside cgroup_mutex and should be held to modify
  69. * cgroupfs_root of any cgroup hierarchy - subsys list, flags,
  70. * release_agent_path and so on. Modifying requires both cgroup_mutex and
  71. * cgroup_root_mutex. Readers can acquire either of the two. This is to
  72. * break the following locking order cycle.
  73. *
  74. * A. cgroup_mutex -> cred_guard_mutex -> s_type->i_mutex_key -> namespace_sem
  75. * B. namespace_sem -> cgroup_mutex
  76. *
  77. * B happens only through cgroup_show_options() and using cgroup_root_mutex
  78. * breaks it.
  79. */
  80. #ifdef CONFIG_PROVE_RCU
  81. DEFINE_MUTEX(cgroup_mutex);
  82. EXPORT_SYMBOL_GPL(cgroup_mutex); /* only for lockdep */
  83. #else
  84. static DEFINE_MUTEX(cgroup_mutex);
  85. #endif
  86. static DEFINE_MUTEX(cgroup_root_mutex);
  87. /*
  88. * Generate an array of cgroup subsystem pointers. At boot time, this is
  89. * populated with the built in subsystems, and modular subsystems are
  90. * registered after that. The mutable section of this array is protected by
  91. * cgroup_mutex.
  92. */
  93. #define SUBSYS(_x) [_x ## _subsys_id] = &_x ## _subsys,
  94. #define IS_SUBSYS_ENABLED(option) IS_BUILTIN(option)
  95. static struct cgroup_subsys *cgroup_subsys[CGROUP_SUBSYS_COUNT] = {
  96. #include <linux/cgroup_subsys.h>
  97. };
  98. /*
  99. * The dummy hierarchy, reserved for the subsystems that are otherwise
  100. * unattached - it never has more than a single cgroup, and all tasks are
  101. * part of that cgroup.
  102. */
  103. static struct cgroupfs_root cgroup_dummy_root;
  104. /* dummy_top is a shorthand for the dummy hierarchy's top cgroup */
  105. static struct cgroup * const cgroup_dummy_top = &cgroup_dummy_root.top_cgroup;
  106. /*
  107. * cgroupfs file entry, pointed to from leaf dentry->d_fsdata.
  108. */
  109. struct cfent {
  110. struct list_head node;
  111. struct dentry *dentry;
  112. struct cftype *type;
  113. struct cgroup_subsys_state *css;
  114. /* file xattrs */
  115. struct simple_xattrs xattrs;
  116. };
  117. /*
  118. * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when
  119. * cgroup_subsys->use_id != 0.
  120. */
  121. #define CSS_ID_MAX (65535)
  122. struct css_id {
  123. /*
  124. * The css to which this ID points. This pointer is set to valid value
  125. * after cgroup is populated. If cgroup is removed, this will be NULL.
  126. * This pointer is expected to be RCU-safe because destroy()
  127. * is called after synchronize_rcu(). But for safe use, css_tryget()
  128. * should be used for avoiding race.
  129. */
  130. struct cgroup_subsys_state __rcu *css;
  131. /*
  132. * ID of this css.
  133. */
  134. unsigned short id;
  135. /*
  136. * Depth in hierarchy which this ID belongs to.
  137. */
  138. unsigned short depth;
  139. /*
  140. * ID is freed by RCU. (and lookup routine is RCU safe.)
  141. */
  142. struct rcu_head rcu_head;
  143. /*
  144. * Hierarchy of CSS ID belongs to.
  145. */
  146. unsigned short stack[0]; /* Array of Length (depth+1) */
  147. };
  148. /*
  149. * cgroup_event represents events which userspace want to receive.
  150. */
  151. struct cgroup_event {
  152. /*
  153. * css which the event belongs to.
  154. */
  155. struct cgroup_subsys_state *css;
  156. /*
  157. * Control file which the event associated.
  158. */
  159. struct cftype *cft;
  160. /*
  161. * eventfd to signal userspace about the event.
  162. */
  163. struct eventfd_ctx *eventfd;
  164. /*
  165. * Each of these stored in a list by the cgroup.
  166. */
  167. struct list_head list;
  168. /*
  169. * All fields below needed to unregister event when
  170. * userspace closes eventfd.
  171. */
  172. poll_table pt;
  173. wait_queue_head_t *wqh;
  174. wait_queue_t wait;
  175. struct work_struct remove;
  176. };
  177. /* The list of hierarchy roots */
  178. static LIST_HEAD(cgroup_roots);
  179. static int cgroup_root_count;
  180. /*
  181. * Hierarchy ID allocation and mapping. It follows the same exclusion
  182. * rules as other root ops - both cgroup_mutex and cgroup_root_mutex for
  183. * writes, either for reads.
  184. */
  185. static DEFINE_IDR(cgroup_hierarchy_idr);
  186. static struct cgroup_name root_cgroup_name = { .name = "/" };
  187. /*
  188. * Assign a monotonically increasing serial number to cgroups. It
  189. * guarantees cgroups with bigger numbers are newer than those with smaller
  190. * numbers. Also, as cgroups are always appended to the parent's
  191. * ->children list, it guarantees that sibling cgroups are always sorted in
  192. * the ascending serial number order on the list. Protected by
  193. * cgroup_mutex.
  194. */
  195. static u64 cgroup_serial_nr_next = 1;
  196. /* This flag indicates whether tasks in the fork and exit paths should
  197. * check for fork/exit handlers to call. This avoids us having to do
  198. * extra work in the fork/exit path if none of the subsystems need to
  199. * be called.
  200. */
  201. static int need_forkexit_callback __read_mostly;
  202. static struct cftype cgroup_base_files[];
  203. static void cgroup_destroy_css_killed(struct cgroup *cgrp);
  204. static int cgroup_destroy_locked(struct cgroup *cgrp);
  205. static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
  206. bool is_add);
  207. /**
  208. * cgroup_css - obtain a cgroup's css for the specified subsystem
  209. * @cgrp: the cgroup of interest
  210. * @ss: the subsystem of interest (%NULL returns the dummy_css)
  211. *
  212. * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This
  213. * function must be called either under cgroup_mutex or rcu_read_lock() and
  214. * the caller is responsible for pinning the returned css if it wants to
  215. * keep accessing it outside the said locks. This function may return
  216. * %NULL if @cgrp doesn't have @subsys_id enabled.
  217. */
  218. static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp,
  219. struct cgroup_subsys *ss)
  220. {
  221. if (ss)
  222. return rcu_dereference_check(cgrp->subsys[ss->subsys_id],
  223. lockdep_is_held(&cgroup_mutex));
  224. else
  225. return &cgrp->dummy_css;
  226. }
  227. /* convenient tests for these bits */
  228. static inline bool cgroup_is_dead(const struct cgroup *cgrp)
  229. {
  230. return test_bit(CGRP_DEAD, &cgrp->flags);
  231. }
  232. /**
  233. * cgroup_is_descendant - test ancestry
  234. * @cgrp: the cgroup to be tested
  235. * @ancestor: possible ancestor of @cgrp
  236. *
  237. * Test whether @cgrp is a descendant of @ancestor. It also returns %true
  238. * if @cgrp == @ancestor. This function is safe to call as long as @cgrp
  239. * and @ancestor are accessible.
  240. */
  241. bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor)
  242. {
  243. while (cgrp) {
  244. if (cgrp == ancestor)
  245. return true;
  246. cgrp = cgrp->parent;
  247. }
  248. return false;
  249. }
  250. EXPORT_SYMBOL_GPL(cgroup_is_descendant);
  251. static int cgroup_is_releasable(const struct cgroup *cgrp)
  252. {
  253. const int bits =
  254. (1 << CGRP_RELEASABLE) |
  255. (1 << CGRP_NOTIFY_ON_RELEASE);
  256. return (cgrp->flags & bits) == bits;
  257. }
  258. static int notify_on_release(const struct cgroup *cgrp)
  259. {
  260. return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
  261. }
  262. /**
  263. * for_each_subsys - iterate all loaded cgroup subsystems
  264. * @ss: the iteration cursor
  265. * @i: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
  266. *
  267. * Should be called under cgroup_mutex.
  268. */
  269. #define for_each_subsys(ss, i) \
  270. for ((i) = 0; (i) < CGROUP_SUBSYS_COUNT; (i)++) \
  271. if (({ lockdep_assert_held(&cgroup_mutex); \
  272. !((ss) = cgroup_subsys[i]); })) { } \
  273. else
  274. /**
  275. * for_each_builtin_subsys - iterate all built-in cgroup subsystems
  276. * @ss: the iteration cursor
  277. * @i: the index of @ss, CGROUP_BUILTIN_SUBSYS_COUNT after reaching the end
  278. *
  279. * Bulit-in subsystems are always present and iteration itself doesn't
  280. * require any synchronization.
  281. */
  282. #define for_each_builtin_subsys(ss, i) \
  283. for ((i) = 0; (i) < CGROUP_BUILTIN_SUBSYS_COUNT && \
  284. (((ss) = cgroup_subsys[i]) || true); (i)++)
  285. /* iterate each subsystem attached to a hierarchy */
  286. #define for_each_root_subsys(root, ss) \
  287. list_for_each_entry((ss), &(root)->subsys_list, sibling)
  288. /* iterate across the active hierarchies */
  289. #define for_each_active_root(root) \
  290. list_for_each_entry((root), &cgroup_roots, root_list)
  291. static inline struct cgroup *__d_cgrp(struct dentry *dentry)
  292. {
  293. return dentry->d_fsdata;
  294. }
  295. static inline struct cfent *__d_cfe(struct dentry *dentry)
  296. {
  297. return dentry->d_fsdata;
  298. }
  299. static inline struct cftype *__d_cft(struct dentry *dentry)
  300. {
  301. return __d_cfe(dentry)->type;
  302. }
  303. /**
  304. * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
  305. * @cgrp: the cgroup to be checked for liveness
  306. *
  307. * On success, returns true; the mutex should be later unlocked. On
  308. * failure returns false with no lock held.
  309. */
  310. static bool cgroup_lock_live_group(struct cgroup *cgrp)
  311. {
  312. mutex_lock(&cgroup_mutex);
  313. if (cgroup_is_dead(cgrp)) {
  314. mutex_unlock(&cgroup_mutex);
  315. return false;
  316. }
  317. return true;
  318. }
  319. /* the list of cgroups eligible for automatic release. Protected by
  320. * release_list_lock */
  321. static LIST_HEAD(release_list);
  322. static DEFINE_RAW_SPINLOCK(release_list_lock);
  323. static void cgroup_release_agent(struct work_struct *work);
  324. static DECLARE_WORK(release_agent_work, cgroup_release_agent);
  325. static void check_for_release(struct cgroup *cgrp);
  326. /*
  327. * A cgroup can be associated with multiple css_sets as different tasks may
  328. * belong to different cgroups on different hierarchies. In the other
  329. * direction, a css_set is naturally associated with multiple cgroups.
  330. * This M:N relationship is represented by the following link structure
  331. * which exists for each association and allows traversing the associations
  332. * from both sides.
  333. */
  334. struct cgrp_cset_link {
  335. /* the cgroup and css_set this link associates */
  336. struct cgroup *cgrp;
  337. struct css_set *cset;
  338. /* list of cgrp_cset_links anchored at cgrp->cset_links */
  339. struct list_head cset_link;
  340. /* list of cgrp_cset_links anchored at css_set->cgrp_links */
  341. struct list_head cgrp_link;
  342. };
  343. /* The default css_set - used by init and its children prior to any
  344. * hierarchies being mounted. It contains a pointer to the root state
  345. * for each subsystem. Also used to anchor the list of css_sets. Not
  346. * reference-counted, to improve performance when child cgroups
  347. * haven't been created.
  348. */
  349. static struct css_set init_css_set;
  350. static struct cgrp_cset_link init_cgrp_cset_link;
  351. static int cgroup_init_idr(struct cgroup_subsys *ss,
  352. struct cgroup_subsys_state *css);
  353. /*
  354. * css_set_lock protects the list of css_set objects, and the chain of
  355. * tasks off each css_set. Nests outside task->alloc_lock due to
  356. * css_task_iter_start().
  357. */
  358. static DEFINE_RWLOCK(css_set_lock);
  359. static int css_set_count;
  360. /*
  361. * hash table for cgroup groups. This improves the performance to find
  362. * an existing css_set. This hash doesn't (currently) take into
  363. * account cgroups in empty hierarchies.
  364. */
  365. #define CSS_SET_HASH_BITS 7
  366. static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
  367. static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
  368. {
  369. unsigned long key = 0UL;
  370. struct cgroup_subsys *ss;
  371. int i;
  372. for_each_subsys(ss, i)
  373. key += (unsigned long)css[i];
  374. key = (key >> 16) ^ key;
  375. return key;
  376. }
  377. /*
  378. * We don't maintain the lists running through each css_set to its task
  379. * until after the first call to css_task_iter_start(). This reduces the
  380. * fork()/exit() overhead for people who have cgroups compiled into their
  381. * kernel but not actually in use.
  382. */
  383. static int use_task_css_set_links __read_mostly;
  384. static void __put_css_set(struct css_set *cset, int taskexit)
  385. {
  386. struct cgrp_cset_link *link, *tmp_link;
  387. /*
  388. * Ensure that the refcount doesn't hit zero while any readers
  389. * can see it. Similar to atomic_dec_and_lock(), but for an
  390. * rwlock
  391. */
  392. if (atomic_add_unless(&cset->refcount, -1, 1))
  393. return;
  394. write_lock(&css_set_lock);
  395. if (!atomic_dec_and_test(&cset->refcount)) {
  396. write_unlock(&css_set_lock);
  397. return;
  398. }
  399. /* This css_set is dead. unlink it and release cgroup refcounts */
  400. hash_del(&cset->hlist);
  401. css_set_count--;
  402. list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
  403. struct cgroup *cgrp = link->cgrp;
  404. list_del(&link->cset_link);
  405. list_del(&link->cgrp_link);
  406. /* @cgrp can't go away while we're holding css_set_lock */
  407. if (list_empty(&cgrp->cset_links) && notify_on_release(cgrp)) {
  408. if (taskexit)
  409. set_bit(CGRP_RELEASABLE, &cgrp->flags);
  410. check_for_release(cgrp);
  411. }
  412. kfree(link);
  413. }
  414. write_unlock(&css_set_lock);
  415. kfree_rcu(cset, rcu_head);
  416. }
  417. /*
  418. * refcounted get/put for css_set objects
  419. */
  420. static inline void get_css_set(struct css_set *cset)
  421. {
  422. atomic_inc(&cset->refcount);
  423. }
  424. static inline void put_css_set(struct css_set *cset)
  425. {
  426. __put_css_set(cset, 0);
  427. }
  428. static inline void put_css_set_taskexit(struct css_set *cset)
  429. {
  430. __put_css_set(cset, 1);
  431. }
  432. /**
  433. * compare_css_sets - helper function for find_existing_css_set().
  434. * @cset: candidate css_set being tested
  435. * @old_cset: existing css_set for a task
  436. * @new_cgrp: cgroup that's being entered by the task
  437. * @template: desired set of css pointers in css_set (pre-calculated)
  438. *
  439. * Returns true if "cset" matches "old_cset" except for the hierarchy
  440. * which "new_cgrp" belongs to, for which it should match "new_cgrp".
  441. */
  442. static bool compare_css_sets(struct css_set *cset,
  443. struct css_set *old_cset,
  444. struct cgroup *new_cgrp,
  445. struct cgroup_subsys_state *template[])
  446. {
  447. struct list_head *l1, *l2;
  448. if (memcmp(template, cset->subsys, sizeof(cset->subsys))) {
  449. /* Not all subsystems matched */
  450. return false;
  451. }
  452. /*
  453. * Compare cgroup pointers in order to distinguish between
  454. * different cgroups in heirarchies with no subsystems. We
  455. * could get by with just this check alone (and skip the
  456. * memcmp above) but on most setups the memcmp check will
  457. * avoid the need for this more expensive check on almost all
  458. * candidates.
  459. */
  460. l1 = &cset->cgrp_links;
  461. l2 = &old_cset->cgrp_links;
  462. while (1) {
  463. struct cgrp_cset_link *link1, *link2;
  464. struct cgroup *cgrp1, *cgrp2;
  465. l1 = l1->next;
  466. l2 = l2->next;
  467. /* See if we reached the end - both lists are equal length. */
  468. if (l1 == &cset->cgrp_links) {
  469. BUG_ON(l2 != &old_cset->cgrp_links);
  470. break;
  471. } else {
  472. BUG_ON(l2 == &old_cset->cgrp_links);
  473. }
  474. /* Locate the cgroups associated with these links. */
  475. link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
  476. link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
  477. cgrp1 = link1->cgrp;
  478. cgrp2 = link2->cgrp;
  479. /* Hierarchies should be linked in the same order. */
  480. BUG_ON(cgrp1->root != cgrp2->root);
  481. /*
  482. * If this hierarchy is the hierarchy of the cgroup
  483. * that's changing, then we need to check that this
  484. * css_set points to the new cgroup; if it's any other
  485. * hierarchy, then this css_set should point to the
  486. * same cgroup as the old css_set.
  487. */
  488. if (cgrp1->root == new_cgrp->root) {
  489. if (cgrp1 != new_cgrp)
  490. return false;
  491. } else {
  492. if (cgrp1 != cgrp2)
  493. return false;
  494. }
  495. }
  496. return true;
  497. }
  498. /**
  499. * find_existing_css_set - init css array and find the matching css_set
  500. * @old_cset: the css_set that we're using before the cgroup transition
  501. * @cgrp: the cgroup that we're moving into
  502. * @template: out param for the new set of csses, should be clear on entry
  503. */
  504. static struct css_set *find_existing_css_set(struct css_set *old_cset,
  505. struct cgroup *cgrp,
  506. struct cgroup_subsys_state *template[])
  507. {
  508. struct cgroupfs_root *root = cgrp->root;
  509. struct cgroup_subsys *ss;
  510. struct css_set *cset;
  511. unsigned long key;
  512. int i;
  513. /*
  514. * Build the set of subsystem state objects that we want to see in the
  515. * new css_set. while subsystems can change globally, the entries here
  516. * won't change, so no need for locking.
  517. */
  518. for_each_subsys(ss, i) {
  519. if (root->subsys_mask & (1UL << i)) {
  520. /* Subsystem is in this hierarchy. So we want
  521. * the subsystem state from the new
  522. * cgroup */
  523. template[i] = cgroup_css(cgrp, ss);
  524. } else {
  525. /* Subsystem is not in this hierarchy, so we
  526. * don't want to change the subsystem state */
  527. template[i] = old_cset->subsys[i];
  528. }
  529. }
  530. key = css_set_hash(template);
  531. hash_for_each_possible(css_set_table, cset, hlist, key) {
  532. if (!compare_css_sets(cset, old_cset, cgrp, template))
  533. continue;
  534. /* This css_set matches what we need */
  535. return cset;
  536. }
  537. /* No existing cgroup group matched */
  538. return NULL;
  539. }
  540. static void free_cgrp_cset_links(struct list_head *links_to_free)
  541. {
  542. struct cgrp_cset_link *link, *tmp_link;
  543. list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
  544. list_del(&link->cset_link);
  545. kfree(link);
  546. }
  547. }
  548. /**
  549. * allocate_cgrp_cset_links - allocate cgrp_cset_links
  550. * @count: the number of links to allocate
  551. * @tmp_links: list_head the allocated links are put on
  552. *
  553. * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
  554. * through ->cset_link. Returns 0 on success or -errno.
  555. */
  556. static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
  557. {
  558. struct cgrp_cset_link *link;
  559. int i;
  560. INIT_LIST_HEAD(tmp_links);
  561. for (i = 0; i < count; i++) {
  562. link = kzalloc(sizeof(*link), GFP_KERNEL);
  563. if (!link) {
  564. free_cgrp_cset_links(tmp_links);
  565. return -ENOMEM;
  566. }
  567. list_add(&link->cset_link, tmp_links);
  568. }
  569. return 0;
  570. }
  571. /**
  572. * link_css_set - a helper function to link a css_set to a cgroup
  573. * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
  574. * @cset: the css_set to be linked
  575. * @cgrp: the destination cgroup
  576. */
  577. static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
  578. struct cgroup *cgrp)
  579. {
  580. struct cgrp_cset_link *link;
  581. BUG_ON(list_empty(tmp_links));
  582. link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
  583. link->cset = cset;
  584. link->cgrp = cgrp;
  585. list_move(&link->cset_link, &cgrp->cset_links);
  586. /*
  587. * Always add links to the tail of the list so that the list
  588. * is sorted by order of hierarchy creation
  589. */
  590. list_add_tail(&link->cgrp_link, &cset->cgrp_links);
  591. }
  592. /**
  593. * find_css_set - return a new css_set with one cgroup updated
  594. * @old_cset: the baseline css_set
  595. * @cgrp: the cgroup to be updated
  596. *
  597. * Return a new css_set that's equivalent to @old_cset, but with @cgrp
  598. * substituted into the appropriate hierarchy.
  599. */
  600. static struct css_set *find_css_set(struct css_set *old_cset,
  601. struct cgroup *cgrp)
  602. {
  603. struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { };
  604. struct css_set *cset;
  605. struct list_head tmp_links;
  606. struct cgrp_cset_link *link;
  607. unsigned long key;
  608. lockdep_assert_held(&cgroup_mutex);
  609. /* First see if we already have a cgroup group that matches
  610. * the desired set */
  611. read_lock(&css_set_lock);
  612. cset = find_existing_css_set(old_cset, cgrp, template);
  613. if (cset)
  614. get_css_set(cset);
  615. read_unlock(&css_set_lock);
  616. if (cset)
  617. return cset;
  618. cset = kzalloc(sizeof(*cset), GFP_KERNEL);
  619. if (!cset)
  620. return NULL;
  621. /* Allocate all the cgrp_cset_link objects that we'll need */
  622. if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) {
  623. kfree(cset);
  624. return NULL;
  625. }
  626. atomic_set(&cset->refcount, 1);
  627. INIT_LIST_HEAD(&cset->cgrp_links);
  628. INIT_LIST_HEAD(&cset->tasks);
  629. INIT_HLIST_NODE(&cset->hlist);
  630. /* Copy the set of subsystem state objects generated in
  631. * find_existing_css_set() */
  632. memcpy(cset->subsys, template, sizeof(cset->subsys));
  633. write_lock(&css_set_lock);
  634. /* Add reference counts and links from the new css_set. */
  635. list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
  636. struct cgroup *c = link->cgrp;
  637. if (c->root == cgrp->root)
  638. c = cgrp;
  639. link_css_set(&tmp_links, cset, c);
  640. }
  641. BUG_ON(!list_empty(&tmp_links));
  642. css_set_count++;
  643. /* Add this cgroup group to the hash table */
  644. key = css_set_hash(cset->subsys);
  645. hash_add(css_set_table, &cset->hlist, key);
  646. write_unlock(&css_set_lock);
  647. return cset;
  648. }
  649. /*
  650. * Return the cgroup for "task" from the given hierarchy. Must be
  651. * called with cgroup_mutex held.
  652. */
  653. static struct cgroup *task_cgroup_from_root(struct task_struct *task,
  654. struct cgroupfs_root *root)
  655. {
  656. struct css_set *cset;
  657. struct cgroup *res = NULL;
  658. BUG_ON(!mutex_is_locked(&cgroup_mutex));
  659. read_lock(&css_set_lock);
  660. /*
  661. * No need to lock the task - since we hold cgroup_mutex the
  662. * task can't change groups, so the only thing that can happen
  663. * is that it exits and its css is set back to init_css_set.
  664. */
  665. cset = task_css_set(task);
  666. if (cset == &init_css_set) {
  667. res = &root->top_cgroup;
  668. } else {
  669. struct cgrp_cset_link *link;
  670. list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
  671. struct cgroup *c = link->cgrp;
  672. if (c->root == root) {
  673. res = c;
  674. break;
  675. }
  676. }
  677. }
  678. read_unlock(&css_set_lock);
  679. BUG_ON(!res);
  680. return res;
  681. }
  682. /*
  683. * There is one global cgroup mutex. We also require taking
  684. * task_lock() when dereferencing a task's cgroup subsys pointers.
  685. * See "The task_lock() exception", at the end of this comment.
  686. *
  687. * A task must hold cgroup_mutex to modify cgroups.
  688. *
  689. * Any task can increment and decrement the count field without lock.
  690. * So in general, code holding cgroup_mutex can't rely on the count
  691. * field not changing. However, if the count goes to zero, then only
  692. * cgroup_attach_task() can increment it again. Because a count of zero
  693. * means that no tasks are currently attached, therefore there is no
  694. * way a task attached to that cgroup can fork (the other way to
  695. * increment the count). So code holding cgroup_mutex can safely
  696. * assume that if the count is zero, it will stay zero. Similarly, if
  697. * a task holds cgroup_mutex on a cgroup with zero count, it
  698. * knows that the cgroup won't be removed, as cgroup_rmdir()
  699. * needs that mutex.
  700. *
  701. * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
  702. * (usually) take cgroup_mutex. These are the two most performance
  703. * critical pieces of code here. The exception occurs on cgroup_exit(),
  704. * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
  705. * is taken, and if the cgroup count is zero, a usermode call made
  706. * to the release agent with the name of the cgroup (path relative to
  707. * the root of cgroup file system) as the argument.
  708. *
  709. * A cgroup can only be deleted if both its 'count' of using tasks
  710. * is zero, and its list of 'children' cgroups is empty. Since all
  711. * tasks in the system use _some_ cgroup, and since there is always at
  712. * least one task in the system (init, pid == 1), therefore, top_cgroup
  713. * always has either children cgroups and/or using tasks. So we don't
  714. * need a special hack to ensure that top_cgroup cannot be deleted.
  715. *
  716. * The task_lock() exception
  717. *
  718. * The need for this exception arises from the action of
  719. * cgroup_attach_task(), which overwrites one task's cgroup pointer with
  720. * another. It does so using cgroup_mutex, however there are
  721. * several performance critical places that need to reference
  722. * task->cgroup without the expense of grabbing a system global
  723. * mutex. Therefore except as noted below, when dereferencing or, as
  724. * in cgroup_attach_task(), modifying a task's cgroup pointer we use
  725. * task_lock(), which acts on a spinlock (task->alloc_lock) already in
  726. * the task_struct routinely used for such matters.
  727. *
  728. * P.S. One more locking exception. RCU is used to guard the
  729. * update of a tasks cgroup pointer by cgroup_attach_task()
  730. */
  731. /*
  732. * A couple of forward declarations required, due to cyclic reference loop:
  733. * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
  734. * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
  735. * -> cgroup_mkdir.
  736. */
  737. static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
  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 = simple_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. /*
  2298. * Check if a file is a control file
  2299. */
  2300. static inline struct cftype *__file_cft(struct file *file)
  2301. {
  2302. if (file_inode(file)->i_fop != &cgroup_file_operations)
  2303. return ERR_PTR(-EINVAL);
  2304. return __d_cft(file->f_dentry);
  2305. }
  2306. static int cgroup_create_file(struct dentry *dentry, umode_t mode,
  2307. struct super_block *sb)
  2308. {
  2309. struct inode *inode;
  2310. if (!dentry)
  2311. return -ENOENT;
  2312. if (dentry->d_inode)
  2313. return -EEXIST;
  2314. inode = cgroup_new_inode(mode, sb);
  2315. if (!inode)
  2316. return -ENOMEM;
  2317. if (S_ISDIR(mode)) {
  2318. inode->i_op = &cgroup_dir_inode_operations;
  2319. inode->i_fop = &simple_dir_operations;
  2320. /* start off with i_nlink == 2 (for "." entry) */
  2321. inc_nlink(inode);
  2322. inc_nlink(dentry->d_parent->d_inode);
  2323. /*
  2324. * Control reaches here with cgroup_mutex held.
  2325. * @inode->i_mutex should nest outside cgroup_mutex but we
  2326. * want to populate it immediately without releasing
  2327. * cgroup_mutex. As @inode isn't visible to anyone else
  2328. * yet, trylock will always succeed without affecting
  2329. * lockdep checks.
  2330. */
  2331. WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex));
  2332. } else if (S_ISREG(mode)) {
  2333. inode->i_size = 0;
  2334. inode->i_fop = &cgroup_file_operations;
  2335. inode->i_op = &cgroup_file_inode_operations;
  2336. }
  2337. d_instantiate(dentry, inode);
  2338. dget(dentry); /* Extra count - pin the dentry in core */
  2339. return 0;
  2340. }
  2341. /**
  2342. * cgroup_file_mode - deduce file mode of a control file
  2343. * @cft: the control file in question
  2344. *
  2345. * returns cft->mode if ->mode is not 0
  2346. * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
  2347. * returns S_IRUGO if it has only a read handler
  2348. * returns S_IWUSR if it has only a write hander
  2349. */
  2350. static umode_t cgroup_file_mode(const struct cftype *cft)
  2351. {
  2352. umode_t mode = 0;
  2353. if (cft->mode)
  2354. return cft->mode;
  2355. if (cft->read || cft->read_u64 || cft->read_s64 ||
  2356. cft->read_map || cft->read_seq_string)
  2357. mode |= S_IRUGO;
  2358. if (cft->write || cft->write_u64 || cft->write_s64 ||
  2359. cft->write_string || cft->trigger)
  2360. mode |= S_IWUSR;
  2361. return mode;
  2362. }
  2363. static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft)
  2364. {
  2365. struct dentry *dir = cgrp->dentry;
  2366. struct cgroup *parent = __d_cgrp(dir);
  2367. struct dentry *dentry;
  2368. struct cfent *cfe;
  2369. int error;
  2370. umode_t mode;
  2371. char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
  2372. if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) &&
  2373. !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) {
  2374. strcpy(name, cft->ss->name);
  2375. strcat(name, ".");
  2376. }
  2377. strcat(name, cft->name);
  2378. BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
  2379. cfe = kzalloc(sizeof(*cfe), GFP_KERNEL);
  2380. if (!cfe)
  2381. return -ENOMEM;
  2382. dentry = lookup_one_len(name, dir, strlen(name));
  2383. if (IS_ERR(dentry)) {
  2384. error = PTR_ERR(dentry);
  2385. goto out;
  2386. }
  2387. cfe->type = (void *)cft;
  2388. cfe->dentry = dentry;
  2389. dentry->d_fsdata = cfe;
  2390. simple_xattrs_init(&cfe->xattrs);
  2391. mode = cgroup_file_mode(cft);
  2392. error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb);
  2393. if (!error) {
  2394. list_add_tail(&cfe->node, &parent->files);
  2395. cfe = NULL;
  2396. }
  2397. dput(dentry);
  2398. out:
  2399. kfree(cfe);
  2400. return error;
  2401. }
  2402. /**
  2403. * cgroup_addrm_files - add or remove files to a cgroup directory
  2404. * @cgrp: the target cgroup
  2405. * @cfts: array of cftypes to be added
  2406. * @is_add: whether to add or remove
  2407. *
  2408. * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
  2409. * For removals, this function never fails. If addition fails, this
  2410. * function doesn't remove files already added. The caller is responsible
  2411. * for cleaning up.
  2412. */
  2413. static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
  2414. bool is_add)
  2415. {
  2416. struct cftype *cft;
  2417. int ret;
  2418. lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
  2419. lockdep_assert_held(&cgroup_mutex);
  2420. for (cft = cfts; cft->name[0] != '\0'; cft++) {
  2421. /* does cft->flags tell us to skip this file on @cgrp? */
  2422. if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
  2423. continue;
  2424. if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
  2425. continue;
  2426. if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
  2427. continue;
  2428. if (is_add) {
  2429. ret = cgroup_add_file(cgrp, cft);
  2430. if (ret) {
  2431. pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
  2432. cft->name, ret);
  2433. return ret;
  2434. }
  2435. } else {
  2436. cgroup_rm_file(cgrp, cft);
  2437. }
  2438. }
  2439. return 0;
  2440. }
  2441. static void cgroup_cfts_prepare(void)
  2442. __acquires(&cgroup_mutex)
  2443. {
  2444. /*
  2445. * Thanks to the entanglement with vfs inode locking, we can't walk
  2446. * the existing cgroups under cgroup_mutex and create files.
  2447. * Instead, we use css_for_each_descendant_pre() and drop RCU read
  2448. * lock before calling cgroup_addrm_files().
  2449. */
  2450. mutex_lock(&cgroup_mutex);
  2451. }
  2452. static int cgroup_cfts_commit(struct cftype *cfts, bool is_add)
  2453. __releases(&cgroup_mutex)
  2454. {
  2455. LIST_HEAD(pending);
  2456. struct cgroup_subsys *ss = cfts[0].ss;
  2457. struct cgroup *root = &ss->root->top_cgroup;
  2458. struct super_block *sb = ss->root->sb;
  2459. struct dentry *prev = NULL;
  2460. struct inode *inode;
  2461. struct cgroup_subsys_state *css;
  2462. u64 update_before;
  2463. int ret = 0;
  2464. /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
  2465. if (!cfts || ss->root == &cgroup_dummy_root ||
  2466. !atomic_inc_not_zero(&sb->s_active)) {
  2467. mutex_unlock(&cgroup_mutex);
  2468. return 0;
  2469. }
  2470. /*
  2471. * All cgroups which are created after we drop cgroup_mutex will
  2472. * have the updated set of files, so we only need to update the
  2473. * cgroups created before the current @cgroup_serial_nr_next.
  2474. */
  2475. update_before = cgroup_serial_nr_next;
  2476. mutex_unlock(&cgroup_mutex);
  2477. /* add/rm files for all cgroups created before */
  2478. rcu_read_lock();
  2479. css_for_each_descendant_pre(css, cgroup_css(root, ss)) {
  2480. struct cgroup *cgrp = css->cgroup;
  2481. if (cgroup_is_dead(cgrp))
  2482. continue;
  2483. inode = cgrp->dentry->d_inode;
  2484. dget(cgrp->dentry);
  2485. rcu_read_unlock();
  2486. dput(prev);
  2487. prev = cgrp->dentry;
  2488. mutex_lock(&inode->i_mutex);
  2489. mutex_lock(&cgroup_mutex);
  2490. if (cgrp->serial_nr < update_before && !cgroup_is_dead(cgrp))
  2491. ret = cgroup_addrm_files(cgrp, cfts, is_add);
  2492. mutex_unlock(&cgroup_mutex);
  2493. mutex_unlock(&inode->i_mutex);
  2494. rcu_read_lock();
  2495. if (ret)
  2496. break;
  2497. }
  2498. rcu_read_unlock();
  2499. dput(prev);
  2500. deactivate_super(sb);
  2501. return ret;
  2502. }
  2503. /**
  2504. * cgroup_add_cftypes - add an array of cftypes to a subsystem
  2505. * @ss: target cgroup subsystem
  2506. * @cfts: zero-length name terminated array of cftypes
  2507. *
  2508. * Register @cfts to @ss. Files described by @cfts are created for all
  2509. * existing cgroups to which @ss is attached and all future cgroups will
  2510. * have them too. This function can be called anytime whether @ss is
  2511. * attached or not.
  2512. *
  2513. * Returns 0 on successful registration, -errno on failure. Note that this
  2514. * function currently returns 0 as long as @cfts registration is successful
  2515. * even if some file creation attempts on existing cgroups fail.
  2516. */
  2517. int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
  2518. {
  2519. struct cftype_set *set;
  2520. struct cftype *cft;
  2521. int ret;
  2522. set = kzalloc(sizeof(*set), GFP_KERNEL);
  2523. if (!set)
  2524. return -ENOMEM;
  2525. for (cft = cfts; cft->name[0] != '\0'; cft++)
  2526. cft->ss = ss;
  2527. cgroup_cfts_prepare();
  2528. set->cfts = cfts;
  2529. list_add_tail(&set->node, &ss->cftsets);
  2530. ret = cgroup_cfts_commit(cfts, true);
  2531. if (ret)
  2532. cgroup_rm_cftypes(cfts);
  2533. return ret;
  2534. }
  2535. EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
  2536. /**
  2537. * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
  2538. * @cfts: zero-length name terminated array of cftypes
  2539. *
  2540. * Unregister @cfts. Files described by @cfts are removed from all
  2541. * existing cgroups and all future cgroups won't have them either. This
  2542. * function can be called anytime whether @cfts' subsys is attached or not.
  2543. *
  2544. * Returns 0 on successful unregistration, -ENOENT if @cfts is not
  2545. * registered.
  2546. */
  2547. int cgroup_rm_cftypes(struct cftype *cfts)
  2548. {
  2549. struct cftype_set *set;
  2550. if (!cfts || !cfts[0].ss)
  2551. return -ENOENT;
  2552. cgroup_cfts_prepare();
  2553. list_for_each_entry(set, &cfts[0].ss->cftsets, node) {
  2554. if (set->cfts == cfts) {
  2555. list_del(&set->node);
  2556. kfree(set);
  2557. cgroup_cfts_commit(cfts, false);
  2558. return 0;
  2559. }
  2560. }
  2561. cgroup_cfts_commit(NULL, false);
  2562. return -ENOENT;
  2563. }
  2564. /**
  2565. * cgroup_task_count - count the number of tasks in a cgroup.
  2566. * @cgrp: the cgroup in question
  2567. *
  2568. * Return the number of tasks in the cgroup.
  2569. */
  2570. int cgroup_task_count(const struct cgroup *cgrp)
  2571. {
  2572. int count = 0;
  2573. struct cgrp_cset_link *link;
  2574. read_lock(&css_set_lock);
  2575. list_for_each_entry(link, &cgrp->cset_links, cset_link)
  2576. count += atomic_read(&link->cset->refcount);
  2577. read_unlock(&css_set_lock);
  2578. return count;
  2579. }
  2580. /*
  2581. * To reduce the fork() overhead for systems that are not actually using
  2582. * their cgroups capability, we don't maintain the lists running through
  2583. * each css_set to its tasks until we see the list actually used - in other
  2584. * words after the first call to css_task_iter_start().
  2585. */
  2586. static void cgroup_enable_task_cg_lists(void)
  2587. {
  2588. struct task_struct *p, *g;
  2589. write_lock(&css_set_lock);
  2590. use_task_css_set_links = 1;
  2591. /*
  2592. * We need tasklist_lock because RCU is not safe against
  2593. * while_each_thread(). Besides, a forking task that has passed
  2594. * cgroup_post_fork() without seeing use_task_css_set_links = 1
  2595. * is not guaranteed to have its child immediately visible in the
  2596. * tasklist if we walk through it with RCU.
  2597. */
  2598. read_lock(&tasklist_lock);
  2599. do_each_thread(g, p) {
  2600. task_lock(p);
  2601. /*
  2602. * We should check if the process is exiting, otherwise
  2603. * it will race with cgroup_exit() in that the list
  2604. * entry won't be deleted though the process has exited.
  2605. */
  2606. if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
  2607. list_add(&p->cg_list, &task_css_set(p)->tasks);
  2608. task_unlock(p);
  2609. } while_each_thread(g, p);
  2610. read_unlock(&tasklist_lock);
  2611. write_unlock(&css_set_lock);
  2612. }
  2613. /**
  2614. * css_next_child - find the next child of a given css
  2615. * @pos_css: the current position (%NULL to initiate traversal)
  2616. * @parent_css: css whose children to walk
  2617. *
  2618. * This function returns the next child of @parent_css and should be called
  2619. * under RCU read lock. The only requirement is that @parent_css and
  2620. * @pos_css are accessible. The next sibling is guaranteed to be returned
  2621. * regardless of their states.
  2622. */
  2623. struct cgroup_subsys_state *
  2624. css_next_child(struct cgroup_subsys_state *pos_css,
  2625. struct cgroup_subsys_state *parent_css)
  2626. {
  2627. struct cgroup *pos = pos_css ? pos_css->cgroup : NULL;
  2628. struct cgroup *cgrp = parent_css->cgroup;
  2629. struct cgroup *next;
  2630. WARN_ON_ONCE(!rcu_read_lock_held());
  2631. /*
  2632. * @pos could already have been removed. Once a cgroup is removed,
  2633. * its ->sibling.next is no longer updated when its next sibling
  2634. * changes. As CGRP_DEAD assertion is serialized and happens
  2635. * before the cgroup is taken off the ->sibling list, if we see it
  2636. * unasserted, it's guaranteed that the next sibling hasn't
  2637. * finished its grace period even if it's already removed, and thus
  2638. * safe to dereference from this RCU critical section. If
  2639. * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
  2640. * to be visible as %true here.
  2641. *
  2642. * If @pos is dead, its next pointer can't be dereferenced;
  2643. * however, as each cgroup is given a monotonically increasing
  2644. * unique serial number and always appended to the sibling list,
  2645. * the next one can be found by walking the parent's children until
  2646. * we see a cgroup with higher serial number than @pos's. While
  2647. * this path can be slower, it's taken only when either the current
  2648. * cgroup is removed or iteration and removal race.
  2649. */
  2650. if (!pos) {
  2651. next = list_entry_rcu(cgrp->children.next, struct cgroup, sibling);
  2652. } else if (likely(!cgroup_is_dead(pos))) {
  2653. next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
  2654. } else {
  2655. list_for_each_entry_rcu(next, &cgrp->children, sibling)
  2656. if (next->serial_nr > pos->serial_nr)
  2657. break;
  2658. }
  2659. if (&next->sibling == &cgrp->children)
  2660. return NULL;
  2661. return cgroup_css(next, parent_css->ss);
  2662. }
  2663. EXPORT_SYMBOL_GPL(css_next_child);
  2664. /**
  2665. * css_next_descendant_pre - find the next descendant for pre-order walk
  2666. * @pos: the current position (%NULL to initiate traversal)
  2667. * @root: css whose descendants to walk
  2668. *
  2669. * To be used by css_for_each_descendant_pre(). Find the next descendant
  2670. * to visit for pre-order traversal of @root's descendants. @root is
  2671. * included in the iteration and the first node to be visited.
  2672. *
  2673. * While this function requires RCU read locking, it doesn't require the
  2674. * whole traversal to be contained in a single RCU critical section. This
  2675. * function will return the correct next descendant as long as both @pos
  2676. * and @root are accessible and @pos is a descendant of @root.
  2677. */
  2678. struct cgroup_subsys_state *
  2679. css_next_descendant_pre(struct cgroup_subsys_state *pos,
  2680. struct cgroup_subsys_state *root)
  2681. {
  2682. struct cgroup_subsys_state *next;
  2683. WARN_ON_ONCE(!rcu_read_lock_held());
  2684. /* if first iteration, visit @root */
  2685. if (!pos)
  2686. return root;
  2687. /* visit the first child if exists */
  2688. next = css_next_child(NULL, pos);
  2689. if (next)
  2690. return next;
  2691. /* no child, visit my or the closest ancestor's next sibling */
  2692. while (pos != root) {
  2693. next = css_next_child(pos, css_parent(pos));
  2694. if (next)
  2695. return next;
  2696. pos = css_parent(pos);
  2697. }
  2698. return NULL;
  2699. }
  2700. EXPORT_SYMBOL_GPL(css_next_descendant_pre);
  2701. /**
  2702. * css_rightmost_descendant - return the rightmost descendant of a css
  2703. * @pos: css of interest
  2704. *
  2705. * Return the rightmost descendant of @pos. If there's no descendant, @pos
  2706. * is returned. This can be used during pre-order traversal to skip
  2707. * subtree of @pos.
  2708. *
  2709. * While this function requires RCU read locking, it doesn't require the
  2710. * whole traversal to be contained in a single RCU critical section. This
  2711. * function will return the correct rightmost descendant as long as @pos is
  2712. * accessible.
  2713. */
  2714. struct cgroup_subsys_state *
  2715. css_rightmost_descendant(struct cgroup_subsys_state *pos)
  2716. {
  2717. struct cgroup_subsys_state *last, *tmp;
  2718. WARN_ON_ONCE(!rcu_read_lock_held());
  2719. do {
  2720. last = pos;
  2721. /* ->prev isn't RCU safe, walk ->next till the end */
  2722. pos = NULL;
  2723. css_for_each_child(tmp, last)
  2724. pos = tmp;
  2725. } while (pos);
  2726. return last;
  2727. }
  2728. EXPORT_SYMBOL_GPL(css_rightmost_descendant);
  2729. static struct cgroup_subsys_state *
  2730. css_leftmost_descendant(struct cgroup_subsys_state *pos)
  2731. {
  2732. struct cgroup_subsys_state *last;
  2733. do {
  2734. last = pos;
  2735. pos = css_next_child(NULL, pos);
  2736. } while (pos);
  2737. return last;
  2738. }
  2739. /**
  2740. * css_next_descendant_post - find the next descendant for post-order walk
  2741. * @pos: the current position (%NULL to initiate traversal)
  2742. * @root: css whose descendants to walk
  2743. *
  2744. * To be used by css_for_each_descendant_post(). Find the next descendant
  2745. * to visit for post-order traversal of @root's descendants. @root is
  2746. * included in the iteration and the last node to be visited.
  2747. *
  2748. * While this function requires RCU read locking, it doesn't require the
  2749. * whole traversal to be contained in a single RCU critical section. This
  2750. * function will return the correct next descendant as long as both @pos
  2751. * and @cgroup are accessible and @pos is a descendant of @cgroup.
  2752. */
  2753. struct cgroup_subsys_state *
  2754. css_next_descendant_post(struct cgroup_subsys_state *pos,
  2755. struct cgroup_subsys_state *root)
  2756. {
  2757. struct cgroup_subsys_state *next;
  2758. WARN_ON_ONCE(!rcu_read_lock_held());
  2759. /* if first iteration, visit the leftmost descendant */
  2760. if (!pos) {
  2761. next = css_leftmost_descendant(root);
  2762. return next != root ? next : NULL;
  2763. }
  2764. /* if we visited @root, we're done */
  2765. if (pos == root)
  2766. return NULL;
  2767. /* if there's an unvisited sibling, visit its leftmost descendant */
  2768. next = css_next_child(pos, css_parent(pos));
  2769. if (next)
  2770. return css_leftmost_descendant(next);
  2771. /* no sibling left, visit parent */
  2772. return css_parent(pos);
  2773. }
  2774. EXPORT_SYMBOL_GPL(css_next_descendant_post);
  2775. /**
  2776. * css_advance_task_iter - advance a task itererator to the next css_set
  2777. * @it: the iterator to advance
  2778. *
  2779. * Advance @it to the next css_set to walk.
  2780. */
  2781. static void css_advance_task_iter(struct css_task_iter *it)
  2782. {
  2783. struct list_head *l = it->cset_link;
  2784. struct cgrp_cset_link *link;
  2785. struct css_set *cset;
  2786. /* Advance to the next non-empty css_set */
  2787. do {
  2788. l = l->next;
  2789. if (l == &it->origin_css->cgroup->cset_links) {
  2790. it->cset_link = NULL;
  2791. return;
  2792. }
  2793. link = list_entry(l, struct cgrp_cset_link, cset_link);
  2794. cset = link->cset;
  2795. } while (list_empty(&cset->tasks));
  2796. it->cset_link = l;
  2797. it->task = cset->tasks.next;
  2798. }
  2799. /**
  2800. * css_task_iter_start - initiate task iteration
  2801. * @css: the css to walk tasks of
  2802. * @it: the task iterator to use
  2803. *
  2804. * Initiate iteration through the tasks of @css. The caller can call
  2805. * css_task_iter_next() to walk through the tasks until the function
  2806. * returns NULL. On completion of iteration, css_task_iter_end() must be
  2807. * called.
  2808. *
  2809. * Note that this function acquires a lock which is released when the
  2810. * iteration finishes. The caller can't sleep while iteration is in
  2811. * progress.
  2812. */
  2813. void css_task_iter_start(struct cgroup_subsys_state *css,
  2814. struct css_task_iter *it)
  2815. __acquires(css_set_lock)
  2816. {
  2817. /*
  2818. * The first time anyone tries to iterate across a css, we need to
  2819. * enable the list linking each css_set to its tasks, and fix up
  2820. * all existing tasks.
  2821. */
  2822. if (!use_task_css_set_links)
  2823. cgroup_enable_task_cg_lists();
  2824. read_lock(&css_set_lock);
  2825. it->origin_css = css;
  2826. it->cset_link = &css->cgroup->cset_links;
  2827. css_advance_task_iter(it);
  2828. }
  2829. /**
  2830. * css_task_iter_next - return the next task for the iterator
  2831. * @it: the task iterator being iterated
  2832. *
  2833. * The "next" function for task iteration. @it should have been
  2834. * initialized via css_task_iter_start(). Returns NULL when the iteration
  2835. * reaches the end.
  2836. */
  2837. struct task_struct *css_task_iter_next(struct css_task_iter *it)
  2838. {
  2839. struct task_struct *res;
  2840. struct list_head *l = it->task;
  2841. struct cgrp_cset_link *link;
  2842. /* If the iterator cg is NULL, we have no tasks */
  2843. if (!it->cset_link)
  2844. return NULL;
  2845. res = list_entry(l, struct task_struct, cg_list);
  2846. /* Advance iterator to find next entry */
  2847. l = l->next;
  2848. link = list_entry(it->cset_link, struct cgrp_cset_link, cset_link);
  2849. if (l == &link->cset->tasks) {
  2850. /*
  2851. * We reached the end of this task list - move on to the
  2852. * next cgrp_cset_link.
  2853. */
  2854. css_advance_task_iter(it);
  2855. } else {
  2856. it->task = l;
  2857. }
  2858. return res;
  2859. }
  2860. /**
  2861. * css_task_iter_end - finish task iteration
  2862. * @it: the task iterator to finish
  2863. *
  2864. * Finish task iteration started by css_task_iter_start().
  2865. */
  2866. void css_task_iter_end(struct css_task_iter *it)
  2867. __releases(css_set_lock)
  2868. {
  2869. read_unlock(&css_set_lock);
  2870. }
  2871. static inline int started_after_time(struct task_struct *t1,
  2872. struct timespec *time,
  2873. struct task_struct *t2)
  2874. {
  2875. int start_diff = timespec_compare(&t1->start_time, time);
  2876. if (start_diff > 0) {
  2877. return 1;
  2878. } else if (start_diff < 0) {
  2879. return 0;
  2880. } else {
  2881. /*
  2882. * Arbitrarily, if two processes started at the same
  2883. * time, we'll say that the lower pointer value
  2884. * started first. Note that t2 may have exited by now
  2885. * so this may not be a valid pointer any longer, but
  2886. * that's fine - it still serves to distinguish
  2887. * between two tasks started (effectively) simultaneously.
  2888. */
  2889. return t1 > t2;
  2890. }
  2891. }
  2892. /*
  2893. * This function is a callback from heap_insert() and is used to order
  2894. * the heap.
  2895. * In this case we order the heap in descending task start time.
  2896. */
  2897. static inline int started_after(void *p1, void *p2)
  2898. {
  2899. struct task_struct *t1 = p1;
  2900. struct task_struct *t2 = p2;
  2901. return started_after_time(t1, &t2->start_time, t2);
  2902. }
  2903. /**
  2904. * css_scan_tasks - iterate though all the tasks in a css
  2905. * @css: the css to iterate tasks of
  2906. * @test: optional test callback
  2907. * @process: process callback
  2908. * @data: data passed to @test and @process
  2909. * @heap: optional pre-allocated heap used for task iteration
  2910. *
  2911. * Iterate through all the tasks in @css, calling @test for each, and if it
  2912. * returns %true, call @process for it also.
  2913. *
  2914. * @test may be NULL, meaning always true (select all tasks), which
  2915. * effectively duplicates css_task_iter_{start,next,end}() but does not
  2916. * lock css_set_lock for the call to @process.
  2917. *
  2918. * It is guaranteed that @process will act on every task that is a member
  2919. * of @css for the duration of this call. This function may or may not
  2920. * call @process for tasks that exit or move to a different css during the
  2921. * call, or are forked or move into the css during the call.
  2922. *
  2923. * Note that @test may be called with locks held, and may in some
  2924. * situations be called multiple times for the same task, so it should be
  2925. * cheap.
  2926. *
  2927. * If @heap is non-NULL, a heap has been pre-allocated and will be used for
  2928. * heap operations (and its "gt" member will be overwritten), else a
  2929. * temporary heap will be used (allocation of which may cause this function
  2930. * to fail).
  2931. */
  2932. int css_scan_tasks(struct cgroup_subsys_state *css,
  2933. bool (*test)(struct task_struct *, void *),
  2934. void (*process)(struct task_struct *, void *),
  2935. void *data, struct ptr_heap *heap)
  2936. {
  2937. int retval, i;
  2938. struct css_task_iter it;
  2939. struct task_struct *p, *dropped;
  2940. /* Never dereference latest_task, since it's not refcounted */
  2941. struct task_struct *latest_task = NULL;
  2942. struct ptr_heap tmp_heap;
  2943. struct timespec latest_time = { 0, 0 };
  2944. if (heap) {
  2945. /* The caller supplied our heap and pre-allocated its memory */
  2946. heap->gt = &started_after;
  2947. } else {
  2948. /* We need to allocate our own heap memory */
  2949. heap = &tmp_heap;
  2950. retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
  2951. if (retval)
  2952. /* cannot allocate the heap */
  2953. return retval;
  2954. }
  2955. again:
  2956. /*
  2957. * Scan tasks in the css, using the @test callback to determine
  2958. * which are of interest, and invoking @process callback on the
  2959. * ones which need an update. Since we don't want to hold any
  2960. * locks during the task updates, gather tasks to be processed in a
  2961. * heap structure. The heap is sorted by descending task start
  2962. * time. If the statically-sized heap fills up, we overflow tasks
  2963. * that started later, and in future iterations only consider tasks
  2964. * that started after the latest task in the previous pass. This
  2965. * guarantees forward progress and that we don't miss any tasks.
  2966. */
  2967. heap->size = 0;
  2968. css_task_iter_start(css, &it);
  2969. while ((p = css_task_iter_next(&it))) {
  2970. /*
  2971. * Only affect tasks that qualify per the caller's callback,
  2972. * if he provided one
  2973. */
  2974. if (test && !test(p, data))
  2975. continue;
  2976. /*
  2977. * Only process tasks that started after the last task
  2978. * we processed
  2979. */
  2980. if (!started_after_time(p, &latest_time, latest_task))
  2981. continue;
  2982. dropped = heap_insert(heap, p);
  2983. if (dropped == NULL) {
  2984. /*
  2985. * The new task was inserted; the heap wasn't
  2986. * previously full
  2987. */
  2988. get_task_struct(p);
  2989. } else if (dropped != p) {
  2990. /*
  2991. * The new task was inserted, and pushed out a
  2992. * different task
  2993. */
  2994. get_task_struct(p);
  2995. put_task_struct(dropped);
  2996. }
  2997. /*
  2998. * Else the new task was newer than anything already in
  2999. * the heap and wasn't inserted
  3000. */
  3001. }
  3002. css_task_iter_end(&it);
  3003. if (heap->size) {
  3004. for (i = 0; i < heap->size; i++) {
  3005. struct task_struct *q = heap->ptrs[i];
  3006. if (i == 0) {
  3007. latest_time = q->start_time;
  3008. latest_task = q;
  3009. }
  3010. /* Process the task per the caller's callback */
  3011. process(q, data);
  3012. put_task_struct(q);
  3013. }
  3014. /*
  3015. * If we had to process any tasks at all, scan again
  3016. * in case some of them were in the middle of forking
  3017. * children that didn't get processed.
  3018. * Not the most efficient way to do it, but it avoids
  3019. * having to take callback_mutex in the fork path
  3020. */
  3021. goto again;
  3022. }
  3023. if (heap == &tmp_heap)
  3024. heap_free(&tmp_heap);
  3025. return 0;
  3026. }
  3027. static void cgroup_transfer_one_task(struct task_struct *task, void *data)
  3028. {
  3029. struct cgroup *new_cgroup = data;
  3030. mutex_lock(&cgroup_mutex);
  3031. cgroup_attach_task(new_cgroup, task, false);
  3032. mutex_unlock(&cgroup_mutex);
  3033. }
  3034. /**
  3035. * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
  3036. * @to: cgroup to which the tasks will be moved
  3037. * @from: cgroup in which the tasks currently reside
  3038. */
  3039. int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
  3040. {
  3041. return css_scan_tasks(&from->dummy_css, NULL, cgroup_transfer_one_task,
  3042. to, NULL);
  3043. }
  3044. /*
  3045. * Stuff for reading the 'tasks'/'procs' files.
  3046. *
  3047. * Reading this file can return large amounts of data if a cgroup has
  3048. * *lots* of attached tasks. So it may need several calls to read(),
  3049. * but we cannot guarantee that the information we produce is correct
  3050. * unless we produce it entirely atomically.
  3051. *
  3052. */
  3053. /* which pidlist file are we talking about? */
  3054. enum cgroup_filetype {
  3055. CGROUP_FILE_PROCS,
  3056. CGROUP_FILE_TASKS,
  3057. };
  3058. /*
  3059. * A pidlist is a list of pids that virtually represents the contents of one
  3060. * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
  3061. * a pair (one each for procs, tasks) for each pid namespace that's relevant
  3062. * to the cgroup.
  3063. */
  3064. struct cgroup_pidlist {
  3065. /*
  3066. * used to find which pidlist is wanted. doesn't change as long as
  3067. * this particular list stays in the list.
  3068. */
  3069. struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
  3070. /* array of xids */
  3071. pid_t *list;
  3072. /* how many elements the above list has */
  3073. int length;
  3074. /* how many files are using the current array */
  3075. int use_count;
  3076. /* each of these stored in a list by its cgroup */
  3077. struct list_head links;
  3078. /* pointer to the cgroup we belong to, for list removal purposes */
  3079. struct cgroup *owner;
  3080. /* protects the other fields */
  3081. struct rw_semaphore rwsem;
  3082. };
  3083. /*
  3084. * The following two functions "fix" the issue where there are more pids
  3085. * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
  3086. * TODO: replace with a kernel-wide solution to this problem
  3087. */
  3088. #define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
  3089. static void *pidlist_allocate(int count)
  3090. {
  3091. if (PIDLIST_TOO_LARGE(count))
  3092. return vmalloc(count * sizeof(pid_t));
  3093. else
  3094. return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
  3095. }
  3096. static void pidlist_free(void *p)
  3097. {
  3098. if (is_vmalloc_addr(p))
  3099. vfree(p);
  3100. else
  3101. kfree(p);
  3102. }
  3103. /*
  3104. * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
  3105. * Returns the number of unique elements.
  3106. */
  3107. static int pidlist_uniq(pid_t *list, int length)
  3108. {
  3109. int src, dest = 1;
  3110. /*
  3111. * we presume the 0th element is unique, so i starts at 1. trivial
  3112. * edge cases first; no work needs to be done for either
  3113. */
  3114. if (length == 0 || length == 1)
  3115. return length;
  3116. /* src and dest walk down the list; dest counts unique elements */
  3117. for (src = 1; src < length; src++) {
  3118. /* find next unique element */
  3119. while (list[src] == list[src-1]) {
  3120. src++;
  3121. if (src == length)
  3122. goto after;
  3123. }
  3124. /* dest always points to where the next unique element goes */
  3125. list[dest] = list[src];
  3126. dest++;
  3127. }
  3128. after:
  3129. return dest;
  3130. }
  3131. static int cmppid(const void *a, const void *b)
  3132. {
  3133. return *(pid_t *)a - *(pid_t *)b;
  3134. }
  3135. /*
  3136. * find the appropriate pidlist for our purpose (given procs vs tasks)
  3137. * returns with the lock on that pidlist already held, and takes care
  3138. * of the use count, or returns NULL with no locks held if we're out of
  3139. * memory.
  3140. */
  3141. static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
  3142. enum cgroup_filetype type)
  3143. {
  3144. struct cgroup_pidlist *l;
  3145. /* don't need task_nsproxy() if we're looking at ourself */
  3146. struct pid_namespace *ns = task_active_pid_ns(current);
  3147. /*
  3148. * We can't drop the pidlist_mutex before taking the l->rwsem in case
  3149. * the last ref-holder is trying to remove l from the list at the same
  3150. * time. Holding the pidlist_mutex precludes somebody taking whichever
  3151. * list we find out from under us - compare release_pid_array().
  3152. */
  3153. mutex_lock(&cgrp->pidlist_mutex);
  3154. list_for_each_entry(l, &cgrp->pidlists, links) {
  3155. if (l->key.type == type && l->key.ns == ns) {
  3156. /* make sure l doesn't vanish out from under us */
  3157. down_write(&l->rwsem);
  3158. mutex_unlock(&cgrp->pidlist_mutex);
  3159. return l;
  3160. }
  3161. }
  3162. /* entry not found; create a new one */
  3163. l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
  3164. if (!l) {
  3165. mutex_unlock(&cgrp->pidlist_mutex);
  3166. return l;
  3167. }
  3168. init_rwsem(&l->rwsem);
  3169. down_write(&l->rwsem);
  3170. l->key.type = type;
  3171. l->key.ns = get_pid_ns(ns);
  3172. l->owner = cgrp;
  3173. list_add(&l->links, &cgrp->pidlists);
  3174. mutex_unlock(&cgrp->pidlist_mutex);
  3175. return l;
  3176. }
  3177. /*
  3178. * Load a cgroup's pidarray with either procs' tgids or tasks' pids
  3179. */
  3180. static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
  3181. struct cgroup_pidlist **lp)
  3182. {
  3183. pid_t *array;
  3184. int length;
  3185. int pid, n = 0; /* used for populating the array */
  3186. struct css_task_iter it;
  3187. struct task_struct *tsk;
  3188. struct cgroup_pidlist *l;
  3189. /*
  3190. * If cgroup gets more users after we read count, we won't have
  3191. * enough space - tough. This race is indistinguishable to the
  3192. * caller from the case that the additional cgroup users didn't
  3193. * show up until sometime later on.
  3194. */
  3195. length = cgroup_task_count(cgrp);
  3196. array = pidlist_allocate(length);
  3197. if (!array)
  3198. return -ENOMEM;
  3199. /* now, populate the array */
  3200. css_task_iter_start(&cgrp->dummy_css, &it);
  3201. while ((tsk = css_task_iter_next(&it))) {
  3202. if (unlikely(n == length))
  3203. break;
  3204. /* get tgid or pid for procs or tasks file respectively */
  3205. if (type == CGROUP_FILE_PROCS)
  3206. pid = task_tgid_vnr(tsk);
  3207. else
  3208. pid = task_pid_vnr(tsk);
  3209. if (pid > 0) /* make sure to only use valid results */
  3210. array[n++] = pid;
  3211. }
  3212. css_task_iter_end(&it);
  3213. length = n;
  3214. /* now sort & (if procs) strip out duplicates */
  3215. sort(array, length, sizeof(pid_t), cmppid, NULL);
  3216. if (type == CGROUP_FILE_PROCS)
  3217. length = pidlist_uniq(array, length);
  3218. l = cgroup_pidlist_find(cgrp, type);
  3219. if (!l) {
  3220. pidlist_free(array);
  3221. return -ENOMEM;
  3222. }
  3223. /* store array, freeing old if necessary - lock already held */
  3224. pidlist_free(l->list);
  3225. l->list = array;
  3226. l->length = length;
  3227. l->use_count++;
  3228. up_write(&l->rwsem);
  3229. *lp = l;
  3230. return 0;
  3231. }
  3232. /**
  3233. * cgroupstats_build - build and fill cgroupstats
  3234. * @stats: cgroupstats to fill information into
  3235. * @dentry: A dentry entry belonging to the cgroup for which stats have
  3236. * been requested.
  3237. *
  3238. * Build and fill cgroupstats so that taskstats can export it to user
  3239. * space.
  3240. */
  3241. int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
  3242. {
  3243. int ret = -EINVAL;
  3244. struct cgroup *cgrp;
  3245. struct css_task_iter it;
  3246. struct task_struct *tsk;
  3247. /*
  3248. * Validate dentry by checking the superblock operations,
  3249. * and make sure it's a directory.
  3250. */
  3251. if (dentry->d_sb->s_op != &cgroup_ops ||
  3252. !S_ISDIR(dentry->d_inode->i_mode))
  3253. goto err;
  3254. ret = 0;
  3255. cgrp = dentry->d_fsdata;
  3256. css_task_iter_start(&cgrp->dummy_css, &it);
  3257. while ((tsk = css_task_iter_next(&it))) {
  3258. switch (tsk->state) {
  3259. case TASK_RUNNING:
  3260. stats->nr_running++;
  3261. break;
  3262. case TASK_INTERRUPTIBLE:
  3263. stats->nr_sleeping++;
  3264. break;
  3265. case TASK_UNINTERRUPTIBLE:
  3266. stats->nr_uninterruptible++;
  3267. break;
  3268. case TASK_STOPPED:
  3269. stats->nr_stopped++;
  3270. break;
  3271. default:
  3272. if (delayacct_is_task_waiting_on_io(tsk))
  3273. stats->nr_io_wait++;
  3274. break;
  3275. }
  3276. }
  3277. css_task_iter_end(&it);
  3278. err:
  3279. return ret;
  3280. }
  3281. /*
  3282. * seq_file methods for the tasks/procs files. The seq_file position is the
  3283. * next pid to display; the seq_file iterator is a pointer to the pid
  3284. * in the cgroup->l->list array.
  3285. */
  3286. static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
  3287. {
  3288. /*
  3289. * Initially we receive a position value that corresponds to
  3290. * one more than the last pid shown (or 0 on the first call or
  3291. * after a seek to the start). Use a binary-search to find the
  3292. * next pid to display, if any
  3293. */
  3294. struct cgroup_pidlist *l = s->private;
  3295. int index = 0, pid = *pos;
  3296. int *iter;
  3297. down_read(&l->rwsem);
  3298. if (pid) {
  3299. int end = l->length;
  3300. while (index < end) {
  3301. int mid = (index + end) / 2;
  3302. if (l->list[mid] == pid) {
  3303. index = mid;
  3304. break;
  3305. } else if (l->list[mid] <= pid)
  3306. index = mid + 1;
  3307. else
  3308. end = mid;
  3309. }
  3310. }
  3311. /* If we're off the end of the array, we're done */
  3312. if (index >= l->length)
  3313. return NULL;
  3314. /* Update the abstract position to be the actual pid that we found */
  3315. iter = l->list + index;
  3316. *pos = *iter;
  3317. return iter;
  3318. }
  3319. static void cgroup_pidlist_stop(struct seq_file *s, void *v)
  3320. {
  3321. struct cgroup_pidlist *l = s->private;
  3322. up_read(&l->rwsem);
  3323. }
  3324. static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
  3325. {
  3326. struct cgroup_pidlist *l = s->private;
  3327. pid_t *p = v;
  3328. pid_t *end = l->list + l->length;
  3329. /*
  3330. * Advance to the next pid in the array. If this goes off the
  3331. * end, we're done
  3332. */
  3333. p++;
  3334. if (p >= end) {
  3335. return NULL;
  3336. } else {
  3337. *pos = *p;
  3338. return p;
  3339. }
  3340. }
  3341. static int cgroup_pidlist_show(struct seq_file *s, void *v)
  3342. {
  3343. return seq_printf(s, "%d\n", *(int *)v);
  3344. }
  3345. /*
  3346. * seq_operations functions for iterating on pidlists through seq_file -
  3347. * independent of whether it's tasks or procs
  3348. */
  3349. static const struct seq_operations cgroup_pidlist_seq_operations = {
  3350. .start = cgroup_pidlist_start,
  3351. .stop = cgroup_pidlist_stop,
  3352. .next = cgroup_pidlist_next,
  3353. .show = cgroup_pidlist_show,
  3354. };
  3355. static void cgroup_release_pid_array(struct cgroup_pidlist *l)
  3356. {
  3357. /*
  3358. * the case where we're the last user of this particular pidlist will
  3359. * have us remove it from the cgroup's list, which entails taking the
  3360. * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
  3361. * pidlist_mutex, we have to take pidlist_mutex first.
  3362. */
  3363. mutex_lock(&l->owner->pidlist_mutex);
  3364. down_write(&l->rwsem);
  3365. BUG_ON(!l->use_count);
  3366. if (!--l->use_count) {
  3367. /* we're the last user if refcount is 0; remove and free */
  3368. list_del(&l->links);
  3369. mutex_unlock(&l->owner->pidlist_mutex);
  3370. pidlist_free(l->list);
  3371. put_pid_ns(l->key.ns);
  3372. up_write(&l->rwsem);
  3373. kfree(l);
  3374. return;
  3375. }
  3376. mutex_unlock(&l->owner->pidlist_mutex);
  3377. up_write(&l->rwsem);
  3378. }
  3379. static int cgroup_pidlist_release(struct inode *inode, struct file *file)
  3380. {
  3381. struct cgroup_pidlist *l;
  3382. if (!(file->f_mode & FMODE_READ))
  3383. return 0;
  3384. /*
  3385. * the seq_file will only be initialized if the file was opened for
  3386. * reading; hence we check if it's not null only in that case.
  3387. */
  3388. l = ((struct seq_file *)file->private_data)->private;
  3389. cgroup_release_pid_array(l);
  3390. return seq_release(inode, file);
  3391. }
  3392. static const struct file_operations cgroup_pidlist_operations = {
  3393. .read = seq_read,
  3394. .llseek = seq_lseek,
  3395. .write = cgroup_file_write,
  3396. .release = cgroup_pidlist_release,
  3397. };
  3398. /*
  3399. * The following functions handle opens on a file that displays a pidlist
  3400. * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
  3401. * in the cgroup.
  3402. */
  3403. /* helper function for the two below it */
  3404. static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
  3405. {
  3406. struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
  3407. struct cgroup_pidlist *l;
  3408. int retval;
  3409. /* Nothing to do for write-only files */
  3410. if (!(file->f_mode & FMODE_READ))
  3411. return 0;
  3412. /* have the array populated */
  3413. retval = pidlist_array_load(cgrp, type, &l);
  3414. if (retval)
  3415. return retval;
  3416. /* configure file information */
  3417. file->f_op = &cgroup_pidlist_operations;
  3418. retval = seq_open(file, &cgroup_pidlist_seq_operations);
  3419. if (retval) {
  3420. cgroup_release_pid_array(l);
  3421. return retval;
  3422. }
  3423. ((struct seq_file *)file->private_data)->private = l;
  3424. return 0;
  3425. }
  3426. static int cgroup_tasks_open(struct inode *unused, struct file *file)
  3427. {
  3428. return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
  3429. }
  3430. static int cgroup_procs_open(struct inode *unused, struct file *file)
  3431. {
  3432. return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
  3433. }
  3434. static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
  3435. struct cftype *cft)
  3436. {
  3437. return notify_on_release(css->cgroup);
  3438. }
  3439. static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
  3440. struct cftype *cft, u64 val)
  3441. {
  3442. clear_bit(CGRP_RELEASABLE, &css->cgroup->flags);
  3443. if (val)
  3444. set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
  3445. else
  3446. clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
  3447. return 0;
  3448. }
  3449. /*
  3450. * When dput() is called asynchronously, if umount has been done and
  3451. * then deactivate_super() in cgroup_free_fn() kills the superblock,
  3452. * there's a small window that vfs will see the root dentry with non-zero
  3453. * refcnt and trigger BUG().
  3454. *
  3455. * That's why we hold a reference before dput() and drop it right after.
  3456. */
  3457. static void cgroup_dput(struct cgroup *cgrp)
  3458. {
  3459. struct super_block *sb = cgrp->root->sb;
  3460. atomic_inc(&sb->s_active);
  3461. dput(cgrp->dentry);
  3462. deactivate_super(sb);
  3463. }
  3464. /*
  3465. * Unregister event and free resources.
  3466. *
  3467. * Gets called from workqueue.
  3468. */
  3469. static void cgroup_event_remove(struct work_struct *work)
  3470. {
  3471. struct cgroup_event *event = container_of(work, struct cgroup_event,
  3472. remove);
  3473. struct cgroup_subsys_state *css = event->css;
  3474. remove_wait_queue(event->wqh, &event->wait);
  3475. event->cft->unregister_event(css, event->cft, event->eventfd);
  3476. /* Notify userspace the event is going away. */
  3477. eventfd_signal(event->eventfd, 1);
  3478. eventfd_ctx_put(event->eventfd);
  3479. kfree(event);
  3480. css_put(css);
  3481. }
  3482. /*
  3483. * Gets called on POLLHUP on eventfd when user closes it.
  3484. *
  3485. * Called with wqh->lock held and interrupts disabled.
  3486. */
  3487. static int cgroup_event_wake(wait_queue_t *wait, unsigned mode,
  3488. int sync, void *key)
  3489. {
  3490. struct cgroup_event *event = container_of(wait,
  3491. struct cgroup_event, wait);
  3492. struct cgroup *cgrp = event->css->cgroup;
  3493. unsigned long flags = (unsigned long)key;
  3494. if (flags & POLLHUP) {
  3495. /*
  3496. * If the event has been detached at cgroup removal, we
  3497. * can simply return knowing the other side will cleanup
  3498. * for us.
  3499. *
  3500. * We can't race against event freeing since the other
  3501. * side will require wqh->lock via remove_wait_queue(),
  3502. * which we hold.
  3503. */
  3504. spin_lock(&cgrp->event_list_lock);
  3505. if (!list_empty(&event->list)) {
  3506. list_del_init(&event->list);
  3507. /*
  3508. * We are in atomic context, but cgroup_event_remove()
  3509. * may sleep, so we have to call it in workqueue.
  3510. */
  3511. schedule_work(&event->remove);
  3512. }
  3513. spin_unlock(&cgrp->event_list_lock);
  3514. }
  3515. return 0;
  3516. }
  3517. static void cgroup_event_ptable_queue_proc(struct file *file,
  3518. wait_queue_head_t *wqh, poll_table *pt)
  3519. {
  3520. struct cgroup_event *event = container_of(pt,
  3521. struct cgroup_event, pt);
  3522. event->wqh = wqh;
  3523. add_wait_queue(wqh, &event->wait);
  3524. }
  3525. /*
  3526. * Parse input and register new cgroup event handler.
  3527. *
  3528. * Input must be in format '<event_fd> <control_fd> <args>'.
  3529. * Interpretation of args is defined by control file implementation.
  3530. */
  3531. static int cgroup_write_event_control(struct cgroup_subsys_state *dummy_css,
  3532. struct cftype *cft, const char *buffer)
  3533. {
  3534. struct cgroup *cgrp = dummy_css->cgroup;
  3535. struct cgroup_event *event;
  3536. struct cgroup_subsys_state *cfile_css;
  3537. unsigned int efd, cfd;
  3538. struct fd efile;
  3539. struct fd cfile;
  3540. char *endp;
  3541. int ret;
  3542. efd = simple_strtoul(buffer, &endp, 10);
  3543. if (*endp != ' ')
  3544. return -EINVAL;
  3545. buffer = endp + 1;
  3546. cfd = simple_strtoul(buffer, &endp, 10);
  3547. if ((*endp != ' ') && (*endp != '\0'))
  3548. return -EINVAL;
  3549. buffer = endp + 1;
  3550. event = kzalloc(sizeof(*event), GFP_KERNEL);
  3551. if (!event)
  3552. return -ENOMEM;
  3553. INIT_LIST_HEAD(&event->list);
  3554. init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc);
  3555. init_waitqueue_func_entry(&event->wait, cgroup_event_wake);
  3556. INIT_WORK(&event->remove, cgroup_event_remove);
  3557. efile = fdget(efd);
  3558. if (!efile.file) {
  3559. ret = -EBADF;
  3560. goto out_kfree;
  3561. }
  3562. event->eventfd = eventfd_ctx_fileget(efile.file);
  3563. if (IS_ERR(event->eventfd)) {
  3564. ret = PTR_ERR(event->eventfd);
  3565. goto out_put_efile;
  3566. }
  3567. cfile = fdget(cfd);
  3568. if (!cfile.file) {
  3569. ret = -EBADF;
  3570. goto out_put_eventfd;
  3571. }
  3572. /* the process need read permission on control file */
  3573. /* AV: shouldn't we check that it's been opened for read instead? */
  3574. ret = inode_permission(file_inode(cfile.file), MAY_READ);
  3575. if (ret < 0)
  3576. goto out_put_cfile;
  3577. event->cft = __file_cft(cfile.file);
  3578. if (IS_ERR(event->cft)) {
  3579. ret = PTR_ERR(event->cft);
  3580. goto out_put_cfile;
  3581. }
  3582. if (!event->cft->ss) {
  3583. ret = -EBADF;
  3584. goto out_put_cfile;
  3585. }
  3586. /*
  3587. * Determine the css of @cfile, verify it belongs to the same
  3588. * cgroup as cgroup.event_control, and associate @event with it.
  3589. * Remaining events are automatically removed on cgroup destruction
  3590. * but the removal is asynchronous, so take an extra ref.
  3591. */
  3592. rcu_read_lock();
  3593. ret = -EINVAL;
  3594. event->css = cgroup_css(cgrp, event->cft->ss);
  3595. cfile_css = css_from_dir(cfile.file->f_dentry->d_parent, event->cft->ss);
  3596. if (event->css && event->css == cfile_css && css_tryget(event->css))
  3597. ret = 0;
  3598. rcu_read_unlock();
  3599. if (ret)
  3600. goto out_put_cfile;
  3601. if (!event->cft->register_event || !event->cft->unregister_event) {
  3602. ret = -EINVAL;
  3603. goto out_put_css;
  3604. }
  3605. ret = event->cft->register_event(event->css, event->cft,
  3606. event->eventfd, buffer);
  3607. if (ret)
  3608. goto out_put_css;
  3609. efile.file->f_op->poll(efile.file, &event->pt);
  3610. spin_lock(&cgrp->event_list_lock);
  3611. list_add(&event->list, &cgrp->event_list);
  3612. spin_unlock(&cgrp->event_list_lock);
  3613. fdput(cfile);
  3614. fdput(efile);
  3615. return 0;
  3616. out_put_css:
  3617. css_put(event->css);
  3618. out_put_cfile:
  3619. fdput(cfile);
  3620. out_put_eventfd:
  3621. eventfd_ctx_put(event->eventfd);
  3622. out_put_efile:
  3623. fdput(efile);
  3624. out_kfree:
  3625. kfree(event);
  3626. return ret;
  3627. }
  3628. static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
  3629. struct cftype *cft)
  3630. {
  3631. return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
  3632. }
  3633. static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
  3634. struct cftype *cft, u64 val)
  3635. {
  3636. if (val)
  3637. set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
  3638. else
  3639. clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
  3640. return 0;
  3641. }
  3642. static struct cftype cgroup_base_files[] = {
  3643. {
  3644. .name = "cgroup.procs",
  3645. .open = cgroup_procs_open,
  3646. .write_u64 = cgroup_procs_write,
  3647. .release = cgroup_pidlist_release,
  3648. .mode = S_IRUGO | S_IWUSR,
  3649. },
  3650. {
  3651. .name = "cgroup.event_control",
  3652. .write_string = cgroup_write_event_control,
  3653. .mode = S_IWUGO,
  3654. },
  3655. {
  3656. .name = "cgroup.clone_children",
  3657. .flags = CFTYPE_INSANE,
  3658. .read_u64 = cgroup_clone_children_read,
  3659. .write_u64 = cgroup_clone_children_write,
  3660. },
  3661. {
  3662. .name = "cgroup.sane_behavior",
  3663. .flags = CFTYPE_ONLY_ON_ROOT,
  3664. .read_seq_string = cgroup_sane_behavior_show,
  3665. },
  3666. /*
  3667. * Historical crazy stuff. These don't have "cgroup." prefix and
  3668. * don't exist if sane_behavior. If you're depending on these, be
  3669. * prepared to be burned.
  3670. */
  3671. {
  3672. .name = "tasks",
  3673. .flags = CFTYPE_INSANE, /* use "procs" instead */
  3674. .open = cgroup_tasks_open,
  3675. .write_u64 = cgroup_tasks_write,
  3676. .release = cgroup_pidlist_release,
  3677. .mode = S_IRUGO | S_IWUSR,
  3678. },
  3679. {
  3680. .name = "notify_on_release",
  3681. .flags = CFTYPE_INSANE,
  3682. .read_u64 = cgroup_read_notify_on_release,
  3683. .write_u64 = cgroup_write_notify_on_release,
  3684. },
  3685. {
  3686. .name = "release_agent",
  3687. .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT,
  3688. .read_seq_string = cgroup_release_agent_show,
  3689. .write_string = cgroup_release_agent_write,
  3690. .max_write_len = PATH_MAX,
  3691. },
  3692. { } /* terminate */
  3693. };
  3694. /**
  3695. * cgroup_populate_dir - create subsys files in a cgroup directory
  3696. * @cgrp: target cgroup
  3697. * @subsys_mask: mask of the subsystem ids whose files should be added
  3698. *
  3699. * On failure, no file is added.
  3700. */
  3701. static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask)
  3702. {
  3703. struct cgroup_subsys *ss;
  3704. int i, ret = 0;
  3705. /* process cftsets of each subsystem */
  3706. for_each_subsys(ss, i) {
  3707. struct cftype_set *set;
  3708. if (!test_bit(i, &subsys_mask))
  3709. continue;
  3710. list_for_each_entry(set, &ss->cftsets, node) {
  3711. ret = cgroup_addrm_files(cgrp, set->cfts, true);
  3712. if (ret < 0)
  3713. goto err;
  3714. }
  3715. }
  3716. /* This cgroup is ready now */
  3717. for_each_root_subsys(cgrp->root, ss) {
  3718. struct cgroup_subsys_state *css = cgroup_css(cgrp, ss);
  3719. struct css_id *id = rcu_dereference_protected(css->id, true);
  3720. /*
  3721. * Update id->css pointer and make this css visible from
  3722. * CSS ID functions. This pointer will be dereferened
  3723. * from RCU-read-side without locks.
  3724. */
  3725. if (id)
  3726. rcu_assign_pointer(id->css, css);
  3727. }
  3728. return 0;
  3729. err:
  3730. cgroup_clear_dir(cgrp, subsys_mask);
  3731. return ret;
  3732. }
  3733. /*
  3734. * css destruction is four-stage process.
  3735. *
  3736. * 1. Destruction starts. Killing of the percpu_ref is initiated.
  3737. * Implemented in kill_css().
  3738. *
  3739. * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
  3740. * and thus css_tryget() is guaranteed to fail, the css can be offlined
  3741. * by invoking offline_css(). After offlining, the base ref is put.
  3742. * Implemented in css_killed_work_fn().
  3743. *
  3744. * 3. When the percpu_ref reaches zero, the only possible remaining
  3745. * accessors are inside RCU read sections. css_release() schedules the
  3746. * RCU callback.
  3747. *
  3748. * 4. After the grace period, the css can be freed. Implemented in
  3749. * css_free_work_fn().
  3750. *
  3751. * It is actually hairier because both step 2 and 4 require process context
  3752. * and thus involve punting to css->destroy_work adding two additional
  3753. * steps to the already complex sequence.
  3754. */
  3755. static void css_free_work_fn(struct work_struct *work)
  3756. {
  3757. struct cgroup_subsys_state *css =
  3758. container_of(work, struct cgroup_subsys_state, destroy_work);
  3759. struct cgroup *cgrp = css->cgroup;
  3760. if (css->parent)
  3761. css_put(css->parent);
  3762. css->ss->css_free(css);
  3763. cgroup_dput(cgrp);
  3764. }
  3765. static void css_free_rcu_fn(struct rcu_head *rcu_head)
  3766. {
  3767. struct cgroup_subsys_state *css =
  3768. container_of(rcu_head, struct cgroup_subsys_state, rcu_head);
  3769. /*
  3770. * css holds an extra ref to @cgrp->dentry which is put on the last
  3771. * css_put(). dput() requires process context which we don't have.
  3772. */
  3773. INIT_WORK(&css->destroy_work, css_free_work_fn);
  3774. schedule_work(&css->destroy_work);
  3775. }
  3776. static void css_release(struct percpu_ref *ref)
  3777. {
  3778. struct cgroup_subsys_state *css =
  3779. container_of(ref, struct cgroup_subsys_state, refcnt);
  3780. call_rcu(&css->rcu_head, css_free_rcu_fn);
  3781. }
  3782. static void init_css(struct cgroup_subsys_state *css, struct cgroup_subsys *ss,
  3783. struct cgroup *cgrp)
  3784. {
  3785. css->cgroup = cgrp;
  3786. css->ss = ss;
  3787. css->flags = 0;
  3788. css->id = NULL;
  3789. if (cgrp->parent)
  3790. css->parent = cgroup_css(cgrp->parent, ss);
  3791. else
  3792. css->flags |= CSS_ROOT;
  3793. BUG_ON(cgroup_css(cgrp, ss));
  3794. }
  3795. /* invoke ->css_online() on a new CSS and mark it online if successful */
  3796. static int online_css(struct cgroup_subsys_state *css)
  3797. {
  3798. struct cgroup_subsys *ss = css->ss;
  3799. int ret = 0;
  3800. lockdep_assert_held(&cgroup_mutex);
  3801. if (ss->css_online)
  3802. ret = ss->css_online(css);
  3803. if (!ret) {
  3804. css->flags |= CSS_ONLINE;
  3805. css->cgroup->nr_css++;
  3806. rcu_assign_pointer(css->cgroup->subsys[ss->subsys_id], css);
  3807. }
  3808. return ret;
  3809. }
  3810. /* if the CSS is online, invoke ->css_offline() on it and mark it offline */
  3811. static void offline_css(struct cgroup_subsys_state *css)
  3812. {
  3813. struct cgroup_subsys *ss = css->ss;
  3814. lockdep_assert_held(&cgroup_mutex);
  3815. if (!(css->flags & CSS_ONLINE))
  3816. return;
  3817. if (ss->css_offline)
  3818. ss->css_offline(css);
  3819. css->flags &= ~CSS_ONLINE;
  3820. css->cgroup->nr_css--;
  3821. RCU_INIT_POINTER(css->cgroup->subsys[ss->subsys_id], css);
  3822. }
  3823. /*
  3824. * cgroup_create - create a cgroup
  3825. * @parent: cgroup that will be parent of the new cgroup
  3826. * @dentry: dentry of the new cgroup
  3827. * @mode: mode to set on new inode
  3828. *
  3829. * Must be called with the mutex on the parent inode held
  3830. */
  3831. static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
  3832. umode_t mode)
  3833. {
  3834. struct cgroup_subsys_state *css_ar[CGROUP_SUBSYS_COUNT] = { };
  3835. struct cgroup *cgrp;
  3836. struct cgroup_name *name;
  3837. struct cgroupfs_root *root = parent->root;
  3838. int err = 0;
  3839. struct cgroup_subsys *ss;
  3840. struct super_block *sb = root->sb;
  3841. /* allocate the cgroup and its ID, 0 is reserved for the root */
  3842. cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
  3843. if (!cgrp)
  3844. return -ENOMEM;
  3845. name = cgroup_alloc_name(dentry);
  3846. if (!name)
  3847. goto err_free_cgrp;
  3848. rcu_assign_pointer(cgrp->name, name);
  3849. /*
  3850. * Temporarily set the pointer to NULL, so idr_find() won't return
  3851. * a half-baked cgroup.
  3852. */
  3853. cgrp->id = idr_alloc(&root->cgroup_idr, NULL, 1, 0, GFP_KERNEL);
  3854. if (cgrp->id < 0)
  3855. goto err_free_name;
  3856. /*
  3857. * Only live parents can have children. Note that the liveliness
  3858. * check isn't strictly necessary because cgroup_mkdir() and
  3859. * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
  3860. * anyway so that locking is contained inside cgroup proper and we
  3861. * don't get nasty surprises if we ever grow another caller.
  3862. */
  3863. if (!cgroup_lock_live_group(parent)) {
  3864. err = -ENODEV;
  3865. goto err_free_id;
  3866. }
  3867. /* Grab a reference on the superblock so the hierarchy doesn't
  3868. * get deleted on unmount if there are child cgroups. This
  3869. * can be done outside cgroup_mutex, since the sb can't
  3870. * disappear while someone has an open control file on the
  3871. * fs */
  3872. atomic_inc(&sb->s_active);
  3873. init_cgroup_housekeeping(cgrp);
  3874. dentry->d_fsdata = cgrp;
  3875. cgrp->dentry = dentry;
  3876. cgrp->parent = parent;
  3877. cgrp->dummy_css.parent = &parent->dummy_css;
  3878. cgrp->root = parent->root;
  3879. if (notify_on_release(parent))
  3880. set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
  3881. if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
  3882. set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
  3883. for_each_root_subsys(root, ss) {
  3884. struct cgroup_subsys_state *css;
  3885. css = ss->css_alloc(cgroup_css(parent, ss));
  3886. if (IS_ERR(css)) {
  3887. err = PTR_ERR(css);
  3888. goto err_free_all;
  3889. }
  3890. css_ar[ss->subsys_id] = css;
  3891. err = percpu_ref_init(&css->refcnt, css_release);
  3892. if (err)
  3893. goto err_free_all;
  3894. init_css(css, ss, cgrp);
  3895. if (ss->use_id) {
  3896. err = alloc_css_id(css);
  3897. if (err)
  3898. goto err_free_all;
  3899. }
  3900. }
  3901. /*
  3902. * Create directory. cgroup_create_file() returns with the new
  3903. * directory locked on success so that it can be populated without
  3904. * dropping cgroup_mutex.
  3905. */
  3906. err = cgroup_create_file(dentry, S_IFDIR | mode, sb);
  3907. if (err < 0)
  3908. goto err_free_all;
  3909. lockdep_assert_held(&dentry->d_inode->i_mutex);
  3910. cgrp->serial_nr = cgroup_serial_nr_next++;
  3911. /* allocation complete, commit to creation */
  3912. list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
  3913. root->number_of_cgroups++;
  3914. /* each css holds a ref to the cgroup's dentry and the parent css */
  3915. for_each_root_subsys(root, ss) {
  3916. struct cgroup_subsys_state *css = css_ar[ss->subsys_id];
  3917. dget(dentry);
  3918. css_get(css->parent);
  3919. }
  3920. /* hold a ref to the parent's dentry */
  3921. dget(parent->dentry);
  3922. /* creation succeeded, notify subsystems */
  3923. for_each_root_subsys(root, ss) {
  3924. struct cgroup_subsys_state *css = css_ar[ss->subsys_id];
  3925. err = online_css(css);
  3926. if (err)
  3927. goto err_destroy;
  3928. if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
  3929. parent->parent) {
  3930. 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",
  3931. current->comm, current->pid, ss->name);
  3932. if (!strcmp(ss->name, "memory"))
  3933. pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
  3934. ss->warned_broken_hierarchy = true;
  3935. }
  3936. }
  3937. idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
  3938. err = cgroup_addrm_files(cgrp, cgroup_base_files, true);
  3939. if (err)
  3940. goto err_destroy;
  3941. err = cgroup_populate_dir(cgrp, root->subsys_mask);
  3942. if (err)
  3943. goto err_destroy;
  3944. mutex_unlock(&cgroup_mutex);
  3945. mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
  3946. return 0;
  3947. err_free_all:
  3948. for_each_root_subsys(root, ss) {
  3949. struct cgroup_subsys_state *css = css_ar[ss->subsys_id];
  3950. if (css) {
  3951. percpu_ref_cancel_init(&css->refcnt);
  3952. ss->css_free(css);
  3953. }
  3954. }
  3955. mutex_unlock(&cgroup_mutex);
  3956. /* Release the reference count that we took on the superblock */
  3957. deactivate_super(sb);
  3958. err_free_id:
  3959. idr_remove(&root->cgroup_idr, cgrp->id);
  3960. err_free_name:
  3961. kfree(rcu_dereference_raw(cgrp->name));
  3962. err_free_cgrp:
  3963. kfree(cgrp);
  3964. return err;
  3965. err_destroy:
  3966. cgroup_destroy_locked(cgrp);
  3967. mutex_unlock(&cgroup_mutex);
  3968. mutex_unlock(&dentry->d_inode->i_mutex);
  3969. return err;
  3970. }
  3971. static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  3972. {
  3973. struct cgroup *c_parent = dentry->d_parent->d_fsdata;
  3974. /* the vfs holds inode->i_mutex already */
  3975. return cgroup_create(c_parent, dentry, mode | S_IFDIR);
  3976. }
  3977. /*
  3978. * This is called when the refcnt of a css is confirmed to be killed.
  3979. * css_tryget() is now guaranteed to fail.
  3980. */
  3981. static void css_killed_work_fn(struct work_struct *work)
  3982. {
  3983. struct cgroup_subsys_state *css =
  3984. container_of(work, struct cgroup_subsys_state, destroy_work);
  3985. struct cgroup *cgrp = css->cgroup;
  3986. mutex_lock(&cgroup_mutex);
  3987. /*
  3988. * css_tryget() is guaranteed to fail now. Tell subsystems to
  3989. * initate destruction.
  3990. */
  3991. offline_css(css);
  3992. /*
  3993. * If @cgrp is marked dead, it's waiting for refs of all css's to
  3994. * be disabled before proceeding to the second phase of cgroup
  3995. * destruction. If we are the last one, kick it off.
  3996. */
  3997. if (!cgrp->nr_css && cgroup_is_dead(cgrp))
  3998. cgroup_destroy_css_killed(cgrp);
  3999. mutex_unlock(&cgroup_mutex);
  4000. /*
  4001. * Put the css refs from kill_css(). Each css holds an extra
  4002. * reference to the cgroup's dentry and cgroup removal proceeds
  4003. * regardless of css refs. On the last put of each css, whenever
  4004. * that may be, the extra dentry ref is put so that dentry
  4005. * destruction happens only after all css's are released.
  4006. */
  4007. css_put(css);
  4008. }
  4009. /* css kill confirmation processing requires process context, bounce */
  4010. static void css_killed_ref_fn(struct percpu_ref *ref)
  4011. {
  4012. struct cgroup_subsys_state *css =
  4013. container_of(ref, struct cgroup_subsys_state, refcnt);
  4014. INIT_WORK(&css->destroy_work, css_killed_work_fn);
  4015. schedule_work(&css->destroy_work);
  4016. }
  4017. /**
  4018. * kill_css - destroy a css
  4019. * @css: css to destroy
  4020. *
  4021. * This function initiates destruction of @css by removing cgroup interface
  4022. * files and putting its base reference. ->css_offline() will be invoked
  4023. * asynchronously once css_tryget() is guaranteed to fail and when the
  4024. * reference count reaches zero, @css will be released.
  4025. */
  4026. static void kill_css(struct cgroup_subsys_state *css)
  4027. {
  4028. cgroup_clear_dir(css->cgroup, 1 << css->ss->subsys_id);
  4029. /*
  4030. * Killing would put the base ref, but we need to keep it alive
  4031. * until after ->css_offline().
  4032. */
  4033. css_get(css);
  4034. /*
  4035. * cgroup core guarantees that, by the time ->css_offline() is
  4036. * invoked, no new css reference will be given out via
  4037. * css_tryget(). We can't simply call percpu_ref_kill() and
  4038. * proceed to offlining css's because percpu_ref_kill() doesn't
  4039. * guarantee that the ref is seen as killed on all CPUs on return.
  4040. *
  4041. * Use percpu_ref_kill_and_confirm() to get notifications as each
  4042. * css is confirmed to be seen as killed on all CPUs.
  4043. */
  4044. percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
  4045. }
  4046. /**
  4047. * cgroup_destroy_locked - the first stage of cgroup destruction
  4048. * @cgrp: cgroup to be destroyed
  4049. *
  4050. * css's make use of percpu refcnts whose killing latency shouldn't be
  4051. * exposed to userland and are RCU protected. Also, cgroup core needs to
  4052. * guarantee that css_tryget() won't succeed by the time ->css_offline() is
  4053. * invoked. To satisfy all the requirements, destruction is implemented in
  4054. * the following two steps.
  4055. *
  4056. * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
  4057. * userland visible parts and start killing the percpu refcnts of
  4058. * css's. Set up so that the next stage will be kicked off once all
  4059. * the percpu refcnts are confirmed to be killed.
  4060. *
  4061. * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
  4062. * rest of destruction. Once all cgroup references are gone, the
  4063. * cgroup is RCU-freed.
  4064. *
  4065. * This function implements s1. After this step, @cgrp is gone as far as
  4066. * the userland is concerned and a new cgroup with the same name may be
  4067. * created. As cgroup doesn't care about the names internally, this
  4068. * doesn't cause any problem.
  4069. */
  4070. static int cgroup_destroy_locked(struct cgroup *cgrp)
  4071. __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
  4072. {
  4073. struct dentry *d = cgrp->dentry;
  4074. struct cgroup_event *event, *tmp;
  4075. struct cgroup_subsys *ss;
  4076. struct cgroup *child;
  4077. bool empty;
  4078. lockdep_assert_held(&d->d_inode->i_mutex);
  4079. lockdep_assert_held(&cgroup_mutex);
  4080. /*
  4081. * css_set_lock synchronizes access to ->cset_links and prevents
  4082. * @cgrp from being removed while __put_css_set() is in progress.
  4083. */
  4084. read_lock(&css_set_lock);
  4085. empty = list_empty(&cgrp->cset_links);
  4086. read_unlock(&css_set_lock);
  4087. if (!empty)
  4088. return -EBUSY;
  4089. /*
  4090. * Make sure there's no live children. We can't test ->children
  4091. * emptiness as dead children linger on it while being destroyed;
  4092. * otherwise, "rmdir parent/child parent" may fail with -EBUSY.
  4093. */
  4094. empty = true;
  4095. rcu_read_lock();
  4096. list_for_each_entry_rcu(child, &cgrp->children, sibling) {
  4097. empty = cgroup_is_dead(child);
  4098. if (!empty)
  4099. break;
  4100. }
  4101. rcu_read_unlock();
  4102. if (!empty)
  4103. return -EBUSY;
  4104. /*
  4105. * Initiate massacre of all css's. cgroup_destroy_css_killed()
  4106. * will be invoked to perform the rest of destruction once the
  4107. * percpu refs of all css's are confirmed to be killed.
  4108. */
  4109. for_each_root_subsys(cgrp->root, ss)
  4110. kill_css(cgroup_css(cgrp, ss));
  4111. /*
  4112. * Mark @cgrp dead. This prevents further task migration and child
  4113. * creation by disabling cgroup_lock_live_group(). Note that
  4114. * CGRP_DEAD assertion is depended upon by css_next_child() to
  4115. * resume iteration after dropping RCU read lock. See
  4116. * css_next_child() for details.
  4117. */
  4118. set_bit(CGRP_DEAD, &cgrp->flags);
  4119. /* CGRP_DEAD is set, remove from ->release_list for the last time */
  4120. raw_spin_lock(&release_list_lock);
  4121. if (!list_empty(&cgrp->release_list))
  4122. list_del_init(&cgrp->release_list);
  4123. raw_spin_unlock(&release_list_lock);
  4124. /*
  4125. * If @cgrp has css's attached, the second stage of cgroup
  4126. * destruction is kicked off from css_killed_work_fn() after the
  4127. * refs of all attached css's are killed. If @cgrp doesn't have
  4128. * any css, we kick it off here.
  4129. */
  4130. if (!cgrp->nr_css)
  4131. cgroup_destroy_css_killed(cgrp);
  4132. /*
  4133. * Clear the base files and remove @cgrp directory. The removal
  4134. * puts the base ref but we aren't quite done with @cgrp yet, so
  4135. * hold onto it.
  4136. */
  4137. cgroup_addrm_files(cgrp, cgroup_base_files, false);
  4138. dget(d);
  4139. cgroup_d_remove_dir(d);
  4140. /*
  4141. * Unregister events and notify userspace.
  4142. * Notify userspace about cgroup removing only after rmdir of cgroup
  4143. * directory to avoid race between userspace and kernelspace.
  4144. */
  4145. spin_lock(&cgrp->event_list_lock);
  4146. list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) {
  4147. list_del_init(&event->list);
  4148. schedule_work(&event->remove);
  4149. }
  4150. spin_unlock(&cgrp->event_list_lock);
  4151. return 0;
  4152. };
  4153. /**
  4154. * cgroup_destroy_css_killed - the second step of cgroup destruction
  4155. * @work: cgroup->destroy_free_work
  4156. *
  4157. * This function is invoked from a work item for a cgroup which is being
  4158. * destroyed after all css's are offlined and performs the rest of
  4159. * destruction. This is the second step of destruction described in the
  4160. * comment above cgroup_destroy_locked().
  4161. */
  4162. static void cgroup_destroy_css_killed(struct cgroup *cgrp)
  4163. {
  4164. struct cgroup *parent = cgrp->parent;
  4165. struct dentry *d = cgrp->dentry;
  4166. lockdep_assert_held(&cgroup_mutex);
  4167. /* delete this cgroup from parent->children */
  4168. list_del_rcu(&cgrp->sibling);
  4169. /*
  4170. * We should remove the cgroup object from idr before its grace
  4171. * period starts, so we won't be looking up a cgroup while the
  4172. * cgroup is being freed.
  4173. */
  4174. idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
  4175. cgrp->id = -1;
  4176. dput(d);
  4177. set_bit(CGRP_RELEASABLE, &parent->flags);
  4178. check_for_release(parent);
  4179. }
  4180. static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
  4181. {
  4182. int ret;
  4183. mutex_lock(&cgroup_mutex);
  4184. ret = cgroup_destroy_locked(dentry->d_fsdata);
  4185. mutex_unlock(&cgroup_mutex);
  4186. return ret;
  4187. }
  4188. static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss)
  4189. {
  4190. INIT_LIST_HEAD(&ss->cftsets);
  4191. /*
  4192. * base_cftset is embedded in subsys itself, no need to worry about
  4193. * deregistration.
  4194. */
  4195. if (ss->base_cftypes) {
  4196. struct cftype *cft;
  4197. for (cft = ss->base_cftypes; cft->name[0] != '\0'; cft++)
  4198. cft->ss = ss;
  4199. ss->base_cftset.cfts = ss->base_cftypes;
  4200. list_add_tail(&ss->base_cftset.node, &ss->cftsets);
  4201. }
  4202. }
  4203. static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
  4204. {
  4205. struct cgroup_subsys_state *css;
  4206. printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
  4207. mutex_lock(&cgroup_mutex);
  4208. /* init base cftset */
  4209. cgroup_init_cftsets(ss);
  4210. /* Create the top cgroup state for this subsystem */
  4211. list_add(&ss->sibling, &cgroup_dummy_root.subsys_list);
  4212. ss->root = &cgroup_dummy_root;
  4213. css = ss->css_alloc(cgroup_css(cgroup_dummy_top, ss));
  4214. /* We don't handle early failures gracefully */
  4215. BUG_ON(IS_ERR(css));
  4216. init_css(css, ss, cgroup_dummy_top);
  4217. /* Update the init_css_set to contain a subsys
  4218. * pointer to this state - since the subsystem is
  4219. * newly registered, all tasks and hence the
  4220. * init_css_set is in the subsystem's top cgroup. */
  4221. init_css_set.subsys[ss->subsys_id] = css;
  4222. need_forkexit_callback |= ss->fork || ss->exit;
  4223. /* At system boot, before all subsystems have been
  4224. * registered, no tasks have been forked, so we don't
  4225. * need to invoke fork callbacks here. */
  4226. BUG_ON(!list_empty(&init_task.tasks));
  4227. BUG_ON(online_css(css));
  4228. mutex_unlock(&cgroup_mutex);
  4229. /* this function shouldn't be used with modular subsystems, since they
  4230. * need to register a subsys_id, among other things */
  4231. BUG_ON(ss->module);
  4232. }
  4233. /**
  4234. * cgroup_load_subsys: load and register a modular subsystem at runtime
  4235. * @ss: the subsystem to load
  4236. *
  4237. * This function should be called in a modular subsystem's initcall. If the
  4238. * subsystem is built as a module, it will be assigned a new subsys_id and set
  4239. * up for use. If the subsystem is built-in anyway, work is delegated to the
  4240. * simpler cgroup_init_subsys.
  4241. */
  4242. int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
  4243. {
  4244. struct cgroup_subsys_state *css;
  4245. int i, ret;
  4246. struct hlist_node *tmp;
  4247. struct css_set *cset;
  4248. unsigned long key;
  4249. /* check name and function validity */
  4250. if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
  4251. ss->css_alloc == NULL || ss->css_free == NULL)
  4252. return -EINVAL;
  4253. /*
  4254. * we don't support callbacks in modular subsystems. this check is
  4255. * before the ss->module check for consistency; a subsystem that could
  4256. * be a module should still have no callbacks even if the user isn't
  4257. * compiling it as one.
  4258. */
  4259. if (ss->fork || ss->exit)
  4260. return -EINVAL;
  4261. /*
  4262. * an optionally modular subsystem is built-in: we want to do nothing,
  4263. * since cgroup_init_subsys will have already taken care of it.
  4264. */
  4265. if (ss->module == NULL) {
  4266. /* a sanity check */
  4267. BUG_ON(cgroup_subsys[ss->subsys_id] != ss);
  4268. return 0;
  4269. }
  4270. /* init base cftset */
  4271. cgroup_init_cftsets(ss);
  4272. mutex_lock(&cgroup_mutex);
  4273. cgroup_subsys[ss->subsys_id] = ss;
  4274. /*
  4275. * no ss->css_alloc seems to need anything important in the ss
  4276. * struct, so this can happen first (i.e. before the dummy root
  4277. * attachment).
  4278. */
  4279. css = ss->css_alloc(cgroup_css(cgroup_dummy_top, ss));
  4280. if (IS_ERR(css)) {
  4281. /* failure case - need to deassign the cgroup_subsys[] slot. */
  4282. cgroup_subsys[ss->subsys_id] = NULL;
  4283. mutex_unlock(&cgroup_mutex);
  4284. return PTR_ERR(css);
  4285. }
  4286. list_add(&ss->sibling, &cgroup_dummy_root.subsys_list);
  4287. ss->root = &cgroup_dummy_root;
  4288. /* our new subsystem will be attached to the dummy hierarchy. */
  4289. init_css(css, ss, cgroup_dummy_top);
  4290. /* init_idr must be after init_css() because it sets css->id. */
  4291. if (ss->use_id) {
  4292. ret = cgroup_init_idr(ss, css);
  4293. if (ret)
  4294. goto err_unload;
  4295. }
  4296. /*
  4297. * Now we need to entangle the css into the existing css_sets. unlike
  4298. * in cgroup_init_subsys, there are now multiple css_sets, so each one
  4299. * will need a new pointer to it; done by iterating the css_set_table.
  4300. * furthermore, modifying the existing css_sets will corrupt the hash
  4301. * table state, so each changed css_set will need its hash recomputed.
  4302. * this is all done under the css_set_lock.
  4303. */
  4304. write_lock(&css_set_lock);
  4305. hash_for_each_safe(css_set_table, i, tmp, cset, hlist) {
  4306. /* skip entries that we already rehashed */
  4307. if (cset->subsys[ss->subsys_id])
  4308. continue;
  4309. /* remove existing entry */
  4310. hash_del(&cset->hlist);
  4311. /* set new value */
  4312. cset->subsys[ss->subsys_id] = css;
  4313. /* recompute hash and restore entry */
  4314. key = css_set_hash(cset->subsys);
  4315. hash_add(css_set_table, &cset->hlist, key);
  4316. }
  4317. write_unlock(&css_set_lock);
  4318. ret = online_css(css);
  4319. if (ret)
  4320. goto err_unload;
  4321. /* success! */
  4322. mutex_unlock(&cgroup_mutex);
  4323. return 0;
  4324. err_unload:
  4325. mutex_unlock(&cgroup_mutex);
  4326. /* @ss can't be mounted here as try_module_get() would fail */
  4327. cgroup_unload_subsys(ss);
  4328. return ret;
  4329. }
  4330. EXPORT_SYMBOL_GPL(cgroup_load_subsys);
  4331. /**
  4332. * cgroup_unload_subsys: unload a modular subsystem
  4333. * @ss: the subsystem to unload
  4334. *
  4335. * This function should be called in a modular subsystem's exitcall. When this
  4336. * function is invoked, the refcount on the subsystem's module will be 0, so
  4337. * the subsystem will not be attached to any hierarchy.
  4338. */
  4339. void cgroup_unload_subsys(struct cgroup_subsys *ss)
  4340. {
  4341. struct cgrp_cset_link *link;
  4342. BUG_ON(ss->module == NULL);
  4343. /*
  4344. * we shouldn't be called if the subsystem is in use, and the use of
  4345. * try_module_get() in rebind_subsystems() should ensure that it
  4346. * doesn't start being used while we're killing it off.
  4347. */
  4348. BUG_ON(ss->root != &cgroup_dummy_root);
  4349. mutex_lock(&cgroup_mutex);
  4350. offline_css(cgroup_css(cgroup_dummy_top, ss));
  4351. if (ss->use_id)
  4352. idr_destroy(&ss->idr);
  4353. /* deassign the subsys_id */
  4354. cgroup_subsys[ss->subsys_id] = NULL;
  4355. /* remove subsystem from the dummy root's list of subsystems */
  4356. list_del_init(&ss->sibling);
  4357. /*
  4358. * disentangle the css from all css_sets attached to the dummy
  4359. * top. as in loading, we need to pay our respects to the hashtable
  4360. * gods.
  4361. */
  4362. write_lock(&css_set_lock);
  4363. list_for_each_entry(link, &cgroup_dummy_top->cset_links, cset_link) {
  4364. struct css_set *cset = link->cset;
  4365. unsigned long key;
  4366. hash_del(&cset->hlist);
  4367. cset->subsys[ss->subsys_id] = NULL;
  4368. key = css_set_hash(cset->subsys);
  4369. hash_add(css_set_table, &cset->hlist, key);
  4370. }
  4371. write_unlock(&css_set_lock);
  4372. /*
  4373. * remove subsystem's css from the cgroup_dummy_top and free it -
  4374. * need to free before marking as null because ss->css_free needs
  4375. * the cgrp->subsys pointer to find their state. note that this
  4376. * also takes care of freeing the css_id.
  4377. */
  4378. ss->css_free(cgroup_css(cgroup_dummy_top, ss));
  4379. RCU_INIT_POINTER(cgroup_dummy_top->subsys[ss->subsys_id], NULL);
  4380. mutex_unlock(&cgroup_mutex);
  4381. }
  4382. EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
  4383. /**
  4384. * cgroup_init_early - cgroup initialization at system boot
  4385. *
  4386. * Initialize cgroups at system boot, and initialize any
  4387. * subsystems that request early init.
  4388. */
  4389. int __init cgroup_init_early(void)
  4390. {
  4391. struct cgroup_subsys *ss;
  4392. int i;
  4393. atomic_set(&init_css_set.refcount, 1);
  4394. INIT_LIST_HEAD(&init_css_set.cgrp_links);
  4395. INIT_LIST_HEAD(&init_css_set.tasks);
  4396. INIT_HLIST_NODE(&init_css_set.hlist);
  4397. css_set_count = 1;
  4398. init_cgroup_root(&cgroup_dummy_root);
  4399. cgroup_root_count = 1;
  4400. RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
  4401. init_cgrp_cset_link.cset = &init_css_set;
  4402. init_cgrp_cset_link.cgrp = cgroup_dummy_top;
  4403. list_add(&init_cgrp_cset_link.cset_link, &cgroup_dummy_top->cset_links);
  4404. list_add(&init_cgrp_cset_link.cgrp_link, &init_css_set.cgrp_links);
  4405. /* at bootup time, we don't worry about modular subsystems */
  4406. for_each_builtin_subsys(ss, i) {
  4407. BUG_ON(!ss->name);
  4408. BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
  4409. BUG_ON(!ss->css_alloc);
  4410. BUG_ON(!ss->css_free);
  4411. if (ss->subsys_id != i) {
  4412. printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
  4413. ss->name, ss->subsys_id);
  4414. BUG();
  4415. }
  4416. if (ss->early_init)
  4417. cgroup_init_subsys(ss);
  4418. }
  4419. return 0;
  4420. }
  4421. /**
  4422. * cgroup_init - cgroup initialization
  4423. *
  4424. * Register cgroup filesystem and /proc file, and initialize
  4425. * any subsystems that didn't request early init.
  4426. */
  4427. int __init cgroup_init(void)
  4428. {
  4429. struct cgroup_subsys *ss;
  4430. unsigned long key;
  4431. int i, err;
  4432. err = bdi_init(&cgroup_backing_dev_info);
  4433. if (err)
  4434. return err;
  4435. for_each_builtin_subsys(ss, i) {
  4436. if (!ss->early_init)
  4437. cgroup_init_subsys(ss);
  4438. if (ss->use_id)
  4439. cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]);
  4440. }
  4441. /* allocate id for the dummy hierarchy */
  4442. mutex_lock(&cgroup_mutex);
  4443. mutex_lock(&cgroup_root_mutex);
  4444. /* Add init_css_set to the hash table */
  4445. key = css_set_hash(init_css_set.subsys);
  4446. hash_add(css_set_table, &init_css_set.hlist, key);
  4447. BUG_ON(cgroup_init_root_id(&cgroup_dummy_root, 0, 1));
  4448. err = idr_alloc(&cgroup_dummy_root.cgroup_idr, cgroup_dummy_top,
  4449. 0, 1, GFP_KERNEL);
  4450. BUG_ON(err < 0);
  4451. mutex_unlock(&cgroup_root_mutex);
  4452. mutex_unlock(&cgroup_mutex);
  4453. cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
  4454. if (!cgroup_kobj) {
  4455. err = -ENOMEM;
  4456. goto out;
  4457. }
  4458. err = register_filesystem(&cgroup_fs_type);
  4459. if (err < 0) {
  4460. kobject_put(cgroup_kobj);
  4461. goto out;
  4462. }
  4463. proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
  4464. out:
  4465. if (err)
  4466. bdi_destroy(&cgroup_backing_dev_info);
  4467. return err;
  4468. }
  4469. /*
  4470. * proc_cgroup_show()
  4471. * - Print task's cgroup paths into seq_file, one line for each hierarchy
  4472. * - Used for /proc/<pid>/cgroup.
  4473. * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
  4474. * doesn't really matter if tsk->cgroup changes after we read it,
  4475. * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
  4476. * anyway. No need to check that tsk->cgroup != NULL, thanks to
  4477. * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
  4478. * cgroup to top_cgroup.
  4479. */
  4480. /* TODO: Use a proper seq_file iterator */
  4481. int proc_cgroup_show(struct seq_file *m, void *v)
  4482. {
  4483. struct pid *pid;
  4484. struct task_struct *tsk;
  4485. char *buf;
  4486. int retval;
  4487. struct cgroupfs_root *root;
  4488. retval = -ENOMEM;
  4489. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  4490. if (!buf)
  4491. goto out;
  4492. retval = -ESRCH;
  4493. pid = m->private;
  4494. tsk = get_pid_task(pid, PIDTYPE_PID);
  4495. if (!tsk)
  4496. goto out_free;
  4497. retval = 0;
  4498. mutex_lock(&cgroup_mutex);
  4499. for_each_active_root(root) {
  4500. struct cgroup_subsys *ss;
  4501. struct cgroup *cgrp;
  4502. int count = 0;
  4503. seq_printf(m, "%d:", root->hierarchy_id);
  4504. for_each_root_subsys(root, ss)
  4505. seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
  4506. if (strlen(root->name))
  4507. seq_printf(m, "%sname=%s", count ? "," : "",
  4508. root->name);
  4509. seq_putc(m, ':');
  4510. cgrp = task_cgroup_from_root(tsk, root);
  4511. retval = cgroup_path(cgrp, buf, PAGE_SIZE);
  4512. if (retval < 0)
  4513. goto out_unlock;
  4514. seq_puts(m, buf);
  4515. seq_putc(m, '\n');
  4516. }
  4517. out_unlock:
  4518. mutex_unlock(&cgroup_mutex);
  4519. put_task_struct(tsk);
  4520. out_free:
  4521. kfree(buf);
  4522. out:
  4523. return retval;
  4524. }
  4525. /* Display information about each subsystem and each hierarchy */
  4526. static int proc_cgroupstats_show(struct seq_file *m, void *v)
  4527. {
  4528. struct cgroup_subsys *ss;
  4529. int i;
  4530. seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
  4531. /*
  4532. * ideally we don't want subsystems moving around while we do this.
  4533. * cgroup_mutex is also necessary to guarantee an atomic snapshot of
  4534. * subsys/hierarchy state.
  4535. */
  4536. mutex_lock(&cgroup_mutex);
  4537. for_each_subsys(ss, i)
  4538. seq_printf(m, "%s\t%d\t%d\t%d\n",
  4539. ss->name, ss->root->hierarchy_id,
  4540. ss->root->number_of_cgroups, !ss->disabled);
  4541. mutex_unlock(&cgroup_mutex);
  4542. return 0;
  4543. }
  4544. static int cgroupstats_open(struct inode *inode, struct file *file)
  4545. {
  4546. return single_open(file, proc_cgroupstats_show, NULL);
  4547. }
  4548. static const struct file_operations proc_cgroupstats_operations = {
  4549. .open = cgroupstats_open,
  4550. .read = seq_read,
  4551. .llseek = seq_lseek,
  4552. .release = single_release,
  4553. };
  4554. /**
  4555. * cgroup_fork - attach newly forked task to its parents cgroup.
  4556. * @child: pointer to task_struct of forking parent process.
  4557. *
  4558. * Description: A task inherits its parent's cgroup at fork().
  4559. *
  4560. * A pointer to the shared css_set was automatically copied in
  4561. * fork.c by dup_task_struct(). However, we ignore that copy, since
  4562. * it was not made under the protection of RCU or cgroup_mutex, so
  4563. * might no longer be a valid cgroup pointer. cgroup_attach_task() might
  4564. * have already changed current->cgroups, allowing the previously
  4565. * referenced cgroup group to be removed and freed.
  4566. *
  4567. * At the point that cgroup_fork() is called, 'current' is the parent
  4568. * task, and the passed argument 'child' points to the child task.
  4569. */
  4570. void cgroup_fork(struct task_struct *child)
  4571. {
  4572. task_lock(current);
  4573. get_css_set(task_css_set(current));
  4574. child->cgroups = current->cgroups;
  4575. task_unlock(current);
  4576. INIT_LIST_HEAD(&child->cg_list);
  4577. }
  4578. /**
  4579. * cgroup_post_fork - called on a new task after adding it to the task list
  4580. * @child: the task in question
  4581. *
  4582. * Adds the task to the list running through its css_set if necessary and
  4583. * call the subsystem fork() callbacks. Has to be after the task is
  4584. * visible on the task list in case we race with the first call to
  4585. * cgroup_task_iter_start() - to guarantee that the new task ends up on its
  4586. * list.
  4587. */
  4588. void cgroup_post_fork(struct task_struct *child)
  4589. {
  4590. struct cgroup_subsys *ss;
  4591. int i;
  4592. /*
  4593. * use_task_css_set_links is set to 1 before we walk the tasklist
  4594. * under the tasklist_lock and we read it here after we added the child
  4595. * to the tasklist under the tasklist_lock as well. If the child wasn't
  4596. * yet in the tasklist when we walked through it from
  4597. * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
  4598. * should be visible now due to the paired locking and barriers implied
  4599. * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
  4600. * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
  4601. * lock on fork.
  4602. */
  4603. if (use_task_css_set_links) {
  4604. write_lock(&css_set_lock);
  4605. task_lock(child);
  4606. if (list_empty(&child->cg_list))
  4607. list_add(&child->cg_list, &task_css_set(child)->tasks);
  4608. task_unlock(child);
  4609. write_unlock(&css_set_lock);
  4610. }
  4611. /*
  4612. * Call ss->fork(). This must happen after @child is linked on
  4613. * css_set; otherwise, @child might change state between ->fork()
  4614. * and addition to css_set.
  4615. */
  4616. if (need_forkexit_callback) {
  4617. /*
  4618. * fork/exit callbacks are supported only for builtin
  4619. * subsystems, and the builtin section of the subsys
  4620. * array is immutable, so we don't need to lock the
  4621. * subsys array here. On the other hand, modular section
  4622. * of the array can be freed at module unload, so we
  4623. * can't touch that.
  4624. */
  4625. for_each_builtin_subsys(ss, i)
  4626. if (ss->fork)
  4627. ss->fork(child);
  4628. }
  4629. }
  4630. /**
  4631. * cgroup_exit - detach cgroup from exiting task
  4632. * @tsk: pointer to task_struct of exiting process
  4633. * @run_callback: run exit callbacks?
  4634. *
  4635. * Description: Detach cgroup from @tsk and release it.
  4636. *
  4637. * Note that cgroups marked notify_on_release force every task in
  4638. * them to take the global cgroup_mutex mutex when exiting.
  4639. * This could impact scaling on very large systems. Be reluctant to
  4640. * use notify_on_release cgroups where very high task exit scaling
  4641. * is required on large systems.
  4642. *
  4643. * the_top_cgroup_hack:
  4644. *
  4645. * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
  4646. *
  4647. * We call cgroup_exit() while the task is still competent to
  4648. * handle notify_on_release(), then leave the task attached to the
  4649. * root cgroup in each hierarchy for the remainder of its exit.
  4650. *
  4651. * To do this properly, we would increment the reference count on
  4652. * top_cgroup, and near the very end of the kernel/exit.c do_exit()
  4653. * code we would add a second cgroup function call, to drop that
  4654. * reference. This would just create an unnecessary hot spot on
  4655. * the top_cgroup reference count, to no avail.
  4656. *
  4657. * Normally, holding a reference to a cgroup without bumping its
  4658. * count is unsafe. The cgroup could go away, or someone could
  4659. * attach us to a different cgroup, decrementing the count on
  4660. * the first cgroup that we never incremented. But in this case,
  4661. * top_cgroup isn't going away, and either task has PF_EXITING set,
  4662. * which wards off any cgroup_attach_task() attempts, or task is a failed
  4663. * fork, never visible to cgroup_attach_task.
  4664. */
  4665. void cgroup_exit(struct task_struct *tsk, int run_callbacks)
  4666. {
  4667. struct cgroup_subsys *ss;
  4668. struct css_set *cset;
  4669. int i;
  4670. /*
  4671. * Unlink from the css_set task list if necessary.
  4672. * Optimistically check cg_list before taking
  4673. * css_set_lock
  4674. */
  4675. if (!list_empty(&tsk->cg_list)) {
  4676. write_lock(&css_set_lock);
  4677. if (!list_empty(&tsk->cg_list))
  4678. list_del_init(&tsk->cg_list);
  4679. write_unlock(&css_set_lock);
  4680. }
  4681. /* Reassign the task to the init_css_set. */
  4682. task_lock(tsk);
  4683. cset = task_css_set(tsk);
  4684. RCU_INIT_POINTER(tsk->cgroups, &init_css_set);
  4685. if (run_callbacks && need_forkexit_callback) {
  4686. /*
  4687. * fork/exit callbacks are supported only for builtin
  4688. * subsystems, see cgroup_post_fork() for details.
  4689. */
  4690. for_each_builtin_subsys(ss, i) {
  4691. if (ss->exit) {
  4692. struct cgroup_subsys_state *old_css = cset->subsys[i];
  4693. struct cgroup_subsys_state *css = task_css(tsk, i);
  4694. ss->exit(css, old_css, tsk);
  4695. }
  4696. }
  4697. }
  4698. task_unlock(tsk);
  4699. put_css_set_taskexit(cset);
  4700. }
  4701. static void check_for_release(struct cgroup *cgrp)
  4702. {
  4703. if (cgroup_is_releasable(cgrp) &&
  4704. list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) {
  4705. /*
  4706. * Control Group is currently removeable. If it's not
  4707. * already queued for a userspace notification, queue
  4708. * it now
  4709. */
  4710. int need_schedule_work = 0;
  4711. raw_spin_lock(&release_list_lock);
  4712. if (!cgroup_is_dead(cgrp) &&
  4713. list_empty(&cgrp->release_list)) {
  4714. list_add(&cgrp->release_list, &release_list);
  4715. need_schedule_work = 1;
  4716. }
  4717. raw_spin_unlock(&release_list_lock);
  4718. if (need_schedule_work)
  4719. schedule_work(&release_agent_work);
  4720. }
  4721. }
  4722. /*
  4723. * Notify userspace when a cgroup is released, by running the
  4724. * configured release agent with the name of the cgroup (path
  4725. * relative to the root of cgroup file system) as the argument.
  4726. *
  4727. * Most likely, this user command will try to rmdir this cgroup.
  4728. *
  4729. * This races with the possibility that some other task will be
  4730. * attached to this cgroup before it is removed, or that some other
  4731. * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
  4732. * The presumed 'rmdir' will fail quietly if this cgroup is no longer
  4733. * unused, and this cgroup will be reprieved from its death sentence,
  4734. * to continue to serve a useful existence. Next time it's released,
  4735. * we will get notified again, if it still has 'notify_on_release' set.
  4736. *
  4737. * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
  4738. * means only wait until the task is successfully execve()'d. The
  4739. * separate release agent task is forked by call_usermodehelper(),
  4740. * then control in this thread returns here, without waiting for the
  4741. * release agent task. We don't bother to wait because the caller of
  4742. * this routine has no use for the exit status of the release agent
  4743. * task, so no sense holding our caller up for that.
  4744. */
  4745. static void cgroup_release_agent(struct work_struct *work)
  4746. {
  4747. BUG_ON(work != &release_agent_work);
  4748. mutex_lock(&cgroup_mutex);
  4749. raw_spin_lock(&release_list_lock);
  4750. while (!list_empty(&release_list)) {
  4751. char *argv[3], *envp[3];
  4752. int i;
  4753. char *pathbuf = NULL, *agentbuf = NULL;
  4754. struct cgroup *cgrp = list_entry(release_list.next,
  4755. struct cgroup,
  4756. release_list);
  4757. list_del_init(&cgrp->release_list);
  4758. raw_spin_unlock(&release_list_lock);
  4759. pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  4760. if (!pathbuf)
  4761. goto continue_free;
  4762. if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
  4763. goto continue_free;
  4764. agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
  4765. if (!agentbuf)
  4766. goto continue_free;
  4767. i = 0;
  4768. argv[i++] = agentbuf;
  4769. argv[i++] = pathbuf;
  4770. argv[i] = NULL;
  4771. i = 0;
  4772. /* minimal command environment */
  4773. envp[i++] = "HOME=/";
  4774. envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
  4775. envp[i] = NULL;
  4776. /* Drop the lock while we invoke the usermode helper,
  4777. * since the exec could involve hitting disk and hence
  4778. * be a slow process */
  4779. mutex_unlock(&cgroup_mutex);
  4780. call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
  4781. mutex_lock(&cgroup_mutex);
  4782. continue_free:
  4783. kfree(pathbuf);
  4784. kfree(agentbuf);
  4785. raw_spin_lock(&release_list_lock);
  4786. }
  4787. raw_spin_unlock(&release_list_lock);
  4788. mutex_unlock(&cgroup_mutex);
  4789. }
  4790. static int __init cgroup_disable(char *str)
  4791. {
  4792. struct cgroup_subsys *ss;
  4793. char *token;
  4794. int i;
  4795. while ((token = strsep(&str, ",")) != NULL) {
  4796. if (!*token)
  4797. continue;
  4798. /*
  4799. * cgroup_disable, being at boot time, can't know about
  4800. * module subsystems, so we don't worry about them.
  4801. */
  4802. for_each_builtin_subsys(ss, i) {
  4803. if (!strcmp(token, ss->name)) {
  4804. ss->disabled = 1;
  4805. printk(KERN_INFO "Disabling %s control group"
  4806. " subsystem\n", ss->name);
  4807. break;
  4808. }
  4809. }
  4810. }
  4811. return 1;
  4812. }
  4813. __setup("cgroup_disable=", cgroup_disable);
  4814. /*
  4815. * Functons for CSS ID.
  4816. */
  4817. /* to get ID other than 0, this should be called when !cgroup_is_dead() */
  4818. unsigned short css_id(struct cgroup_subsys_state *css)
  4819. {
  4820. struct css_id *cssid;
  4821. /*
  4822. * This css_id() can return correct value when somone has refcnt
  4823. * on this or this is under rcu_read_lock(). Once css->id is allocated,
  4824. * it's unchanged until freed.
  4825. */
  4826. cssid = rcu_dereference_raw(css->id);
  4827. if (cssid)
  4828. return cssid->id;
  4829. return 0;
  4830. }
  4831. EXPORT_SYMBOL_GPL(css_id);
  4832. /**
  4833. * css_is_ancestor - test "root" css is an ancestor of "child"
  4834. * @child: the css to be tested.
  4835. * @root: the css supporsed to be an ancestor of the child.
  4836. *
  4837. * Returns true if "root" is an ancestor of "child" in its hierarchy. Because
  4838. * this function reads css->id, the caller must hold rcu_read_lock().
  4839. * But, considering usual usage, the csses should be valid objects after test.
  4840. * Assuming that the caller will do some action to the child if this returns
  4841. * returns true, the caller must take "child";s reference count.
  4842. * If "child" is valid object and this returns true, "root" is valid, too.
  4843. */
  4844. bool css_is_ancestor(struct cgroup_subsys_state *child,
  4845. const struct cgroup_subsys_state *root)
  4846. {
  4847. struct css_id *child_id;
  4848. struct css_id *root_id;
  4849. child_id = rcu_dereference(child->id);
  4850. if (!child_id)
  4851. return false;
  4852. root_id = rcu_dereference(root->id);
  4853. if (!root_id)
  4854. return false;
  4855. if (child_id->depth < root_id->depth)
  4856. return false;
  4857. if (child_id->stack[root_id->depth] != root_id->id)
  4858. return false;
  4859. return true;
  4860. }
  4861. void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
  4862. {
  4863. struct css_id *id = rcu_dereference_protected(css->id, true);
  4864. /* When this is called before css_id initialization, id can be NULL */
  4865. if (!id)
  4866. return;
  4867. BUG_ON(!ss->use_id);
  4868. rcu_assign_pointer(id->css, NULL);
  4869. rcu_assign_pointer(css->id, NULL);
  4870. spin_lock(&ss->id_lock);
  4871. idr_remove(&ss->idr, id->id);
  4872. spin_unlock(&ss->id_lock);
  4873. kfree_rcu(id, rcu_head);
  4874. }
  4875. EXPORT_SYMBOL_GPL(free_css_id);
  4876. /*
  4877. * This is called by init or create(). Then, calls to this function are
  4878. * always serialized (By cgroup_mutex() at create()).
  4879. */
  4880. static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
  4881. {
  4882. struct css_id *newid;
  4883. int ret, size;
  4884. BUG_ON(!ss->use_id);
  4885. size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
  4886. newid = kzalloc(size, GFP_KERNEL);
  4887. if (!newid)
  4888. return ERR_PTR(-ENOMEM);
  4889. idr_preload(GFP_KERNEL);
  4890. spin_lock(&ss->id_lock);
  4891. /* Don't use 0. allocates an ID of 1-65535 */
  4892. ret = idr_alloc(&ss->idr, newid, 1, CSS_ID_MAX + 1, GFP_NOWAIT);
  4893. spin_unlock(&ss->id_lock);
  4894. idr_preload_end();
  4895. /* Returns error when there are no free spaces for new ID.*/
  4896. if (ret < 0)
  4897. goto err_out;
  4898. newid->id = ret;
  4899. newid->depth = depth;
  4900. return newid;
  4901. err_out:
  4902. kfree(newid);
  4903. return ERR_PTR(ret);
  4904. }
  4905. static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss,
  4906. struct cgroup_subsys_state *rootcss)
  4907. {
  4908. struct css_id *newid;
  4909. spin_lock_init(&ss->id_lock);
  4910. idr_init(&ss->idr);
  4911. newid = get_new_cssid(ss, 0);
  4912. if (IS_ERR(newid))
  4913. return PTR_ERR(newid);
  4914. newid->stack[0] = newid->id;
  4915. RCU_INIT_POINTER(newid->css, rootcss);
  4916. RCU_INIT_POINTER(rootcss->id, newid);
  4917. return 0;
  4918. }
  4919. static int alloc_css_id(struct cgroup_subsys_state *child_css)
  4920. {
  4921. struct cgroup_subsys_state *parent_css = css_parent(child_css);
  4922. struct css_id *child_id, *parent_id;
  4923. int i, depth;
  4924. parent_id = rcu_dereference_protected(parent_css->id, true);
  4925. depth = parent_id->depth + 1;
  4926. child_id = get_new_cssid(child_css->ss, depth);
  4927. if (IS_ERR(child_id))
  4928. return PTR_ERR(child_id);
  4929. for (i = 0; i < depth; i++)
  4930. child_id->stack[i] = parent_id->stack[i];
  4931. child_id->stack[depth] = child_id->id;
  4932. /*
  4933. * child_id->css pointer will be set after this cgroup is available
  4934. * see cgroup_populate_dir()
  4935. */
  4936. rcu_assign_pointer(child_css->id, child_id);
  4937. return 0;
  4938. }
  4939. /**
  4940. * css_lookup - lookup css by id
  4941. * @ss: cgroup subsys to be looked into.
  4942. * @id: the id
  4943. *
  4944. * Returns pointer to cgroup_subsys_state if there is valid one with id.
  4945. * NULL if not. Should be called under rcu_read_lock()
  4946. */
  4947. struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
  4948. {
  4949. struct css_id *cssid = NULL;
  4950. BUG_ON(!ss->use_id);
  4951. cssid = idr_find(&ss->idr, id);
  4952. if (unlikely(!cssid))
  4953. return NULL;
  4954. return rcu_dereference(cssid->css);
  4955. }
  4956. EXPORT_SYMBOL_GPL(css_lookup);
  4957. /**
  4958. * css_from_dir - get corresponding css from the dentry of a cgroup dir
  4959. * @dentry: directory dentry of interest
  4960. * @ss: subsystem of interest
  4961. *
  4962. * Must be called under RCU read lock. The caller is responsible for
  4963. * pinning the returned css if it needs to be accessed outside the RCU
  4964. * critical section.
  4965. */
  4966. struct cgroup_subsys_state *css_from_dir(struct dentry *dentry,
  4967. struct cgroup_subsys *ss)
  4968. {
  4969. struct cgroup *cgrp;
  4970. WARN_ON_ONCE(!rcu_read_lock_held());
  4971. /* is @dentry a cgroup dir? */
  4972. if (!dentry->d_inode ||
  4973. dentry->d_inode->i_op != &cgroup_dir_inode_operations)
  4974. return ERR_PTR(-EBADF);
  4975. cgrp = __d_cgrp(dentry);
  4976. return cgroup_css(cgrp, ss) ?: ERR_PTR(-ENOENT);
  4977. }
  4978. /**
  4979. * css_from_id - lookup css by id
  4980. * @id: the cgroup id
  4981. * @ss: cgroup subsys to be looked into
  4982. *
  4983. * Returns the css if there's valid one with @id, otherwise returns NULL.
  4984. * Should be called under rcu_read_lock().
  4985. */
  4986. struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
  4987. {
  4988. struct cgroup *cgrp;
  4989. rcu_lockdep_assert(rcu_read_lock_held() ||
  4990. lockdep_is_held(&cgroup_mutex),
  4991. "css_from_id() needs proper protection");
  4992. cgrp = idr_find(&ss->root->cgroup_idr, id);
  4993. if (cgrp)
  4994. return cgroup_css(cgrp, ss);
  4995. return NULL;
  4996. }
  4997. #ifdef CONFIG_CGROUP_DEBUG
  4998. static struct cgroup_subsys_state *
  4999. debug_css_alloc(struct cgroup_subsys_state *parent_css)
  5000. {
  5001. struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
  5002. if (!css)
  5003. return ERR_PTR(-ENOMEM);
  5004. return css;
  5005. }
  5006. static void debug_css_free(struct cgroup_subsys_state *css)
  5007. {
  5008. kfree(css);
  5009. }
  5010. static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
  5011. struct cftype *cft)
  5012. {
  5013. return cgroup_task_count(css->cgroup);
  5014. }
  5015. static u64 current_css_set_read(struct cgroup_subsys_state *css,
  5016. struct cftype *cft)
  5017. {
  5018. return (u64)(unsigned long)current->cgroups;
  5019. }
  5020. static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css,
  5021. struct cftype *cft)
  5022. {
  5023. u64 count;
  5024. rcu_read_lock();
  5025. count = atomic_read(&task_css_set(current)->refcount);
  5026. rcu_read_unlock();
  5027. return count;
  5028. }
  5029. static int current_css_set_cg_links_read(struct cgroup_subsys_state *css,
  5030. struct cftype *cft,
  5031. struct seq_file *seq)
  5032. {
  5033. struct cgrp_cset_link *link;
  5034. struct css_set *cset;
  5035. read_lock(&css_set_lock);
  5036. rcu_read_lock();
  5037. cset = rcu_dereference(current->cgroups);
  5038. list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
  5039. struct cgroup *c = link->cgrp;
  5040. const char *name;
  5041. if (c->dentry)
  5042. name = c->dentry->d_name.name;
  5043. else
  5044. name = "?";
  5045. seq_printf(seq, "Root %d group %s\n",
  5046. c->root->hierarchy_id, name);
  5047. }
  5048. rcu_read_unlock();
  5049. read_unlock(&css_set_lock);
  5050. return 0;
  5051. }
  5052. #define MAX_TASKS_SHOWN_PER_CSS 25
  5053. static int cgroup_css_links_read(struct cgroup_subsys_state *css,
  5054. struct cftype *cft, struct seq_file *seq)
  5055. {
  5056. struct cgrp_cset_link *link;
  5057. read_lock(&css_set_lock);
  5058. list_for_each_entry(link, &css->cgroup->cset_links, cset_link) {
  5059. struct css_set *cset = link->cset;
  5060. struct task_struct *task;
  5061. int count = 0;
  5062. seq_printf(seq, "css_set %p\n", cset);
  5063. list_for_each_entry(task, &cset->tasks, cg_list) {
  5064. if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
  5065. seq_puts(seq, " ...\n");
  5066. break;
  5067. } else {
  5068. seq_printf(seq, " task %d\n",
  5069. task_pid_vnr(task));
  5070. }
  5071. }
  5072. }
  5073. read_unlock(&css_set_lock);
  5074. return 0;
  5075. }
  5076. static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft)
  5077. {
  5078. return test_bit(CGRP_RELEASABLE, &css->cgroup->flags);
  5079. }
  5080. static struct cftype debug_files[] = {
  5081. {
  5082. .name = "taskcount",
  5083. .read_u64 = debug_taskcount_read,
  5084. },
  5085. {
  5086. .name = "current_css_set",
  5087. .read_u64 = current_css_set_read,
  5088. },
  5089. {
  5090. .name = "current_css_set_refcount",
  5091. .read_u64 = current_css_set_refcount_read,
  5092. },
  5093. {
  5094. .name = "current_css_set_cg_links",
  5095. .read_seq_string = current_css_set_cg_links_read,
  5096. },
  5097. {
  5098. .name = "cgroup_css_links",
  5099. .read_seq_string = cgroup_css_links_read,
  5100. },
  5101. {
  5102. .name = "releasable",
  5103. .read_u64 = releasable_read,
  5104. },
  5105. { } /* terminate */
  5106. };
  5107. struct cgroup_subsys debug_subsys = {
  5108. .name = "debug",
  5109. .css_alloc = debug_css_alloc,
  5110. .css_free = debug_css_free,
  5111. .subsys_id = debug_subsys_id,
  5112. .base_cftypes = debug_files,
  5113. };
  5114. #endif /* CONFIG_CGROUP_DEBUG */