cgroup.c 152 KB

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