cgroup.c 155 KB

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