cgroup.c 154 KB

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