cgroup.c 152 KB

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