cgroup.c 153 KB

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