cgroup.c 152 KB

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