cgroup.c 128 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707
  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/slab.h>
  43. #include <linux/spinlock.h>
  44. #include <linux/rwsem.h>
  45. #include <linux/string.h>
  46. #include <linux/sort.h>
  47. #include <linux/kmod.h>
  48. #include <linux/delayacct.h>
  49. #include <linux/cgroupstats.h>
  50. #include <linux/hashtable.h>
  51. #include <linux/pid_namespace.h>
  52. #include <linux/idr.h>
  53. #include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
  54. #include <linux/kthread.h>
  55. #include <linux/delay.h>
  56. #include <linux/atomic.h>
  57. /*
  58. * pidlists linger the following amount before being destroyed. The goal
  59. * is avoiding frequent destruction in the middle of consecutive read calls
  60. * Expiring in the middle is a performance problem not a correctness one.
  61. * 1 sec should be enough.
  62. */
  63. #define CGROUP_PIDLIST_DESTROY_DELAY HZ
  64. #define CGROUP_FILE_NAME_MAX (MAX_CGROUP_TYPE_NAMELEN + \
  65. MAX_CFTYPE_NAME + 2)
  66. /*
  67. * cgroup_tree_mutex nests above cgroup_mutex and protects cftypes, file
  68. * creation/removal and hierarchy changing operations including cgroup
  69. * creation, removal, css association and controller rebinding. This outer
  70. * lock is needed mainly to resolve the circular dependency between kernfs
  71. * active ref and cgroup_mutex. cgroup_tree_mutex nests above both.
  72. */
  73. static DEFINE_MUTEX(cgroup_tree_mutex);
  74. /*
  75. * cgroup_mutex is the master lock. Any modification to cgroup or its
  76. * hierarchy must be performed while holding it.
  77. *
  78. * css_set_rwsem protects task->cgroups pointer, the list of css_set
  79. * objects, and the chain of tasks off each css_set.
  80. *
  81. * These locks are exported if CONFIG_PROVE_RCU so that accessors in
  82. * cgroup.h can use them for lockdep annotations.
  83. */
  84. #ifdef CONFIG_PROVE_RCU
  85. DEFINE_MUTEX(cgroup_mutex);
  86. DECLARE_RWSEM(css_set_rwsem);
  87. EXPORT_SYMBOL_GPL(cgroup_mutex);
  88. EXPORT_SYMBOL_GPL(css_set_rwsem);
  89. #else
  90. static DEFINE_MUTEX(cgroup_mutex);
  91. static DECLARE_RWSEM(css_set_rwsem);
  92. #endif
  93. /*
  94. * Protects cgroup_subsys->release_agent_path. Modifying it also requires
  95. * cgroup_mutex. Reading requires either cgroup_mutex or this spinlock.
  96. */
  97. static DEFINE_SPINLOCK(release_agent_path_lock);
  98. #define cgroup_assert_mutexes_or_rcu_locked() \
  99. rcu_lockdep_assert(rcu_read_lock_held() || \
  100. lockdep_is_held(&cgroup_tree_mutex) || \
  101. lockdep_is_held(&cgroup_mutex), \
  102. "cgroup_[tree_]mutex or RCU read lock required");
  103. /*
  104. * cgroup destruction makes heavy use of work items and there can be a lot
  105. * of concurrent destructions. Use a separate workqueue so that cgroup
  106. * destruction work items don't end up filling up max_active of system_wq
  107. * which may lead to deadlock.
  108. */
  109. static struct workqueue_struct *cgroup_destroy_wq;
  110. /*
  111. * pidlist destructions need to be flushed on cgroup destruction. Use a
  112. * separate workqueue as flush domain.
  113. */
  114. static struct workqueue_struct *cgroup_pidlist_destroy_wq;
  115. /* generate an array of cgroup subsystem pointers */
  116. #define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys,
  117. static struct cgroup_subsys *cgroup_subsys[] = {
  118. #include <linux/cgroup_subsys.h>
  119. };
  120. #undef SUBSYS
  121. /* array of cgroup subsystem names */
  122. #define SUBSYS(_x) [_x ## _cgrp_id] = #_x,
  123. static const char *cgroup_subsys_name[] = {
  124. #include <linux/cgroup_subsys.h>
  125. };
  126. #undef SUBSYS
  127. /*
  128. * The dummy hierarchy, reserved for the subsystems that are otherwise
  129. * unattached - it never has more than a single cgroup, and all tasks are
  130. * part of that cgroup.
  131. */
  132. static struct cgroupfs_root cgroup_dummy_root;
  133. /* dummy_top is a shorthand for the dummy hierarchy's top cgroup */
  134. static struct cgroup * const cgroup_dummy_top = &cgroup_dummy_root.top_cgroup;
  135. /* The list of hierarchy roots */
  136. static LIST_HEAD(cgroup_roots);
  137. static int cgroup_root_count;
  138. /* hierarchy ID allocation and mapping, protected by cgroup_mutex */
  139. static DEFINE_IDR(cgroup_hierarchy_idr);
  140. /*
  141. * Assign a monotonically increasing serial number to cgroups. It
  142. * guarantees cgroups with bigger numbers are newer than those with smaller
  143. * numbers. Also, as cgroups are always appended to the parent's
  144. * ->children list, it guarantees that sibling cgroups are always sorted in
  145. * the ascending serial number order on the list. Protected by
  146. * cgroup_mutex.
  147. */
  148. static u64 cgroup_serial_nr_next = 1;
  149. /* This flag indicates whether tasks in the fork and exit paths should
  150. * check for fork/exit handlers to call. This avoids us having to do
  151. * extra work in the fork/exit path if none of the subsystems need to
  152. * be called.
  153. */
  154. static int need_forkexit_callback __read_mostly;
  155. static struct cftype cgroup_base_files[];
  156. static void cgroup_put(struct cgroup *cgrp);
  157. static int rebind_subsystems(struct cgroupfs_root *root,
  158. unsigned long added_mask, unsigned removed_mask);
  159. static void cgroup_destroy_css_killed(struct cgroup *cgrp);
  160. static int cgroup_destroy_locked(struct cgroup *cgrp);
  161. static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
  162. bool is_add);
  163. static void cgroup_pidlist_destroy_all(struct cgroup *cgrp);
  164. /**
  165. * cgroup_css - obtain a cgroup's css for the specified subsystem
  166. * @cgrp: the cgroup of interest
  167. * @ss: the subsystem of interest (%NULL returns the dummy_css)
  168. *
  169. * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This
  170. * function must be called either under cgroup_mutex or rcu_read_lock() and
  171. * the caller is responsible for pinning the returned css if it wants to
  172. * keep accessing it outside the said locks. This function may return
  173. * %NULL if @cgrp doesn't have @subsys_id enabled.
  174. */
  175. static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp,
  176. struct cgroup_subsys *ss)
  177. {
  178. if (ss)
  179. return rcu_dereference_check(cgrp->subsys[ss->id],
  180. lockdep_is_held(&cgroup_tree_mutex) ||
  181. lockdep_is_held(&cgroup_mutex));
  182. else
  183. return &cgrp->dummy_css;
  184. }
  185. /* convenient tests for these bits */
  186. static inline bool cgroup_is_dead(const struct cgroup *cgrp)
  187. {
  188. return test_bit(CGRP_DEAD, &cgrp->flags);
  189. }
  190. struct cgroup_subsys_state *seq_css(struct seq_file *seq)
  191. {
  192. struct kernfs_open_file *of = seq->private;
  193. struct cgroup *cgrp = of->kn->parent->priv;
  194. struct cftype *cft = seq_cft(seq);
  195. /*
  196. * This is open and unprotected implementation of cgroup_css().
  197. * seq_css() is only called from a kernfs file operation which has
  198. * an active reference on the file. Because all the subsystem
  199. * files are drained before a css is disassociated with a cgroup,
  200. * the matching css from the cgroup's subsys table is guaranteed to
  201. * be and stay valid until the enclosing operation is complete.
  202. */
  203. if (cft->ss)
  204. return rcu_dereference_raw(cgrp->subsys[cft->ss->id]);
  205. else
  206. return &cgrp->dummy_css;
  207. }
  208. EXPORT_SYMBOL_GPL(seq_css);
  209. /**
  210. * cgroup_is_descendant - test ancestry
  211. * @cgrp: the cgroup to be tested
  212. * @ancestor: possible ancestor of @cgrp
  213. *
  214. * Test whether @cgrp is a descendant of @ancestor. It also returns %true
  215. * if @cgrp == @ancestor. This function is safe to call as long as @cgrp
  216. * and @ancestor are accessible.
  217. */
  218. bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor)
  219. {
  220. while (cgrp) {
  221. if (cgrp == ancestor)
  222. return true;
  223. cgrp = cgrp->parent;
  224. }
  225. return false;
  226. }
  227. static int cgroup_is_releasable(const struct cgroup *cgrp)
  228. {
  229. const int bits =
  230. (1 << CGRP_RELEASABLE) |
  231. (1 << CGRP_NOTIFY_ON_RELEASE);
  232. return (cgrp->flags & bits) == bits;
  233. }
  234. static int notify_on_release(const struct cgroup *cgrp)
  235. {
  236. return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
  237. }
  238. /**
  239. * for_each_css - iterate all css's of a cgroup
  240. * @css: the iteration cursor
  241. * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
  242. * @cgrp: the target cgroup to iterate css's of
  243. *
  244. * Should be called under cgroup_mutex.
  245. */
  246. #define for_each_css(css, ssid, cgrp) \
  247. for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
  248. if (!((css) = rcu_dereference_check( \
  249. (cgrp)->subsys[(ssid)], \
  250. lockdep_is_held(&cgroup_tree_mutex) || \
  251. lockdep_is_held(&cgroup_mutex)))) { } \
  252. else
  253. /**
  254. * for_each_subsys - iterate all enabled cgroup subsystems
  255. * @ss: the iteration cursor
  256. * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
  257. */
  258. #define for_each_subsys(ss, ssid) \
  259. for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT && \
  260. (((ss) = cgroup_subsys[ssid]) || true); (ssid)++)
  261. /* iterate across the active hierarchies */
  262. #define for_each_active_root(root) \
  263. list_for_each_entry((root), &cgroup_roots, root_list)
  264. /**
  265. * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
  266. * @cgrp: the cgroup to be checked for liveness
  267. *
  268. * On success, returns true; the mutex should be later unlocked. On
  269. * failure returns false with no lock held.
  270. */
  271. static bool cgroup_lock_live_group(struct cgroup *cgrp)
  272. {
  273. mutex_lock(&cgroup_mutex);
  274. if (cgroup_is_dead(cgrp)) {
  275. mutex_unlock(&cgroup_mutex);
  276. return false;
  277. }
  278. return true;
  279. }
  280. /* the list of cgroups eligible for automatic release. Protected by
  281. * release_list_lock */
  282. static LIST_HEAD(release_list);
  283. static DEFINE_RAW_SPINLOCK(release_list_lock);
  284. static void cgroup_release_agent(struct work_struct *work);
  285. static DECLARE_WORK(release_agent_work, cgroup_release_agent);
  286. static void check_for_release(struct cgroup *cgrp);
  287. /*
  288. * A cgroup can be associated with multiple css_sets as different tasks may
  289. * belong to different cgroups on different hierarchies. In the other
  290. * direction, a css_set is naturally associated with multiple cgroups.
  291. * This M:N relationship is represented by the following link structure
  292. * which exists for each association and allows traversing the associations
  293. * from both sides.
  294. */
  295. struct cgrp_cset_link {
  296. /* the cgroup and css_set this link associates */
  297. struct cgroup *cgrp;
  298. struct css_set *cset;
  299. /* list of cgrp_cset_links anchored at cgrp->cset_links */
  300. struct list_head cset_link;
  301. /* list of cgrp_cset_links anchored at css_set->cgrp_links */
  302. struct list_head cgrp_link;
  303. };
  304. /*
  305. * The default css_set - used by init and its children prior to any
  306. * hierarchies being mounted. It contains a pointer to the root state
  307. * for each subsystem. Also used to anchor the list of css_sets. Not
  308. * reference-counted, to improve performance when child cgroups
  309. * haven't been created.
  310. */
  311. static struct css_set init_css_set = {
  312. .refcount = ATOMIC_INIT(1),
  313. .cgrp_links = LIST_HEAD_INIT(init_css_set.cgrp_links),
  314. .tasks = LIST_HEAD_INIT(init_css_set.tasks),
  315. .mg_tasks = LIST_HEAD_INIT(init_css_set.mg_tasks),
  316. .mg_preload_node = LIST_HEAD_INIT(init_css_set.mg_preload_node),
  317. .mg_node = LIST_HEAD_INIT(init_css_set.mg_node),
  318. };
  319. static struct cgrp_cset_link init_cgrp_cset_link;
  320. static int css_set_count = 1; /* 1 for init_css_set */
  321. /*
  322. * hash table for cgroup groups. This improves the performance to find
  323. * an existing css_set. This hash doesn't (currently) take into
  324. * account cgroups in empty hierarchies.
  325. */
  326. #define CSS_SET_HASH_BITS 7
  327. static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
  328. static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
  329. {
  330. unsigned long key = 0UL;
  331. struct cgroup_subsys *ss;
  332. int i;
  333. for_each_subsys(ss, i)
  334. key += (unsigned long)css[i];
  335. key = (key >> 16) ^ key;
  336. return key;
  337. }
  338. static void put_css_set_locked(struct css_set *cset, bool taskexit)
  339. {
  340. struct cgrp_cset_link *link, *tmp_link;
  341. lockdep_assert_held(&css_set_rwsem);
  342. if (!atomic_dec_and_test(&cset->refcount))
  343. return;
  344. /* This css_set is dead. unlink it and release cgroup refcounts */
  345. hash_del(&cset->hlist);
  346. css_set_count--;
  347. list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
  348. struct cgroup *cgrp = link->cgrp;
  349. list_del(&link->cset_link);
  350. list_del(&link->cgrp_link);
  351. /* @cgrp can't go away while we're holding css_set_rwsem */
  352. if (list_empty(&cgrp->cset_links) && notify_on_release(cgrp)) {
  353. if (taskexit)
  354. set_bit(CGRP_RELEASABLE, &cgrp->flags);
  355. check_for_release(cgrp);
  356. }
  357. kfree(link);
  358. }
  359. kfree_rcu(cset, rcu_head);
  360. }
  361. static void put_css_set(struct css_set *cset, bool taskexit)
  362. {
  363. /*
  364. * Ensure that the refcount doesn't hit zero while any readers
  365. * can see it. Similar to atomic_dec_and_lock(), but for an
  366. * rwlock
  367. */
  368. if (atomic_add_unless(&cset->refcount, -1, 1))
  369. return;
  370. down_write(&css_set_rwsem);
  371. put_css_set_locked(cset, taskexit);
  372. up_write(&css_set_rwsem);
  373. }
  374. /*
  375. * refcounted get/put for css_set objects
  376. */
  377. static inline void get_css_set(struct css_set *cset)
  378. {
  379. atomic_inc(&cset->refcount);
  380. }
  381. /**
  382. * compare_css_sets - helper function for find_existing_css_set().
  383. * @cset: candidate css_set being tested
  384. * @old_cset: existing css_set for a task
  385. * @new_cgrp: cgroup that's being entered by the task
  386. * @template: desired set of css pointers in css_set (pre-calculated)
  387. *
  388. * Returns true if "cset" matches "old_cset" except for the hierarchy
  389. * which "new_cgrp" belongs to, for which it should match "new_cgrp".
  390. */
  391. static bool compare_css_sets(struct css_set *cset,
  392. struct css_set *old_cset,
  393. struct cgroup *new_cgrp,
  394. struct cgroup_subsys_state *template[])
  395. {
  396. struct list_head *l1, *l2;
  397. if (memcmp(template, cset->subsys, sizeof(cset->subsys))) {
  398. /* Not all subsystems matched */
  399. return false;
  400. }
  401. /*
  402. * Compare cgroup pointers in order to distinguish between
  403. * different cgroups in heirarchies with no subsystems. We
  404. * could get by with just this check alone (and skip the
  405. * memcmp above) but on most setups the memcmp check will
  406. * avoid the need for this more expensive check on almost all
  407. * candidates.
  408. */
  409. l1 = &cset->cgrp_links;
  410. l2 = &old_cset->cgrp_links;
  411. while (1) {
  412. struct cgrp_cset_link *link1, *link2;
  413. struct cgroup *cgrp1, *cgrp2;
  414. l1 = l1->next;
  415. l2 = l2->next;
  416. /* See if we reached the end - both lists are equal length. */
  417. if (l1 == &cset->cgrp_links) {
  418. BUG_ON(l2 != &old_cset->cgrp_links);
  419. break;
  420. } else {
  421. BUG_ON(l2 == &old_cset->cgrp_links);
  422. }
  423. /* Locate the cgroups associated with these links. */
  424. link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
  425. link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
  426. cgrp1 = link1->cgrp;
  427. cgrp2 = link2->cgrp;
  428. /* Hierarchies should be linked in the same order. */
  429. BUG_ON(cgrp1->root != cgrp2->root);
  430. /*
  431. * If this hierarchy is the hierarchy of the cgroup
  432. * that's changing, then we need to check that this
  433. * css_set points to the new cgroup; if it's any other
  434. * hierarchy, then this css_set should point to the
  435. * same cgroup as the old css_set.
  436. */
  437. if (cgrp1->root == new_cgrp->root) {
  438. if (cgrp1 != new_cgrp)
  439. return false;
  440. } else {
  441. if (cgrp1 != cgrp2)
  442. return false;
  443. }
  444. }
  445. return true;
  446. }
  447. /**
  448. * find_existing_css_set - init css array and find the matching css_set
  449. * @old_cset: the css_set that we're using before the cgroup transition
  450. * @cgrp: the cgroup that we're moving into
  451. * @template: out param for the new set of csses, should be clear on entry
  452. */
  453. static struct css_set *find_existing_css_set(struct css_set *old_cset,
  454. struct cgroup *cgrp,
  455. struct cgroup_subsys_state *template[])
  456. {
  457. struct cgroupfs_root *root = cgrp->root;
  458. struct cgroup_subsys *ss;
  459. struct css_set *cset;
  460. unsigned long key;
  461. int i;
  462. /*
  463. * Build the set of subsystem state objects that we want to see in the
  464. * new css_set. while subsystems can change globally, the entries here
  465. * won't change, so no need for locking.
  466. */
  467. for_each_subsys(ss, i) {
  468. if (root->subsys_mask & (1UL << i)) {
  469. /* Subsystem is in this hierarchy. So we want
  470. * the subsystem state from the new
  471. * cgroup */
  472. template[i] = cgroup_css(cgrp, ss);
  473. } else {
  474. /* Subsystem is not in this hierarchy, so we
  475. * don't want to change the subsystem state */
  476. template[i] = old_cset->subsys[i];
  477. }
  478. }
  479. key = css_set_hash(template);
  480. hash_for_each_possible(css_set_table, cset, hlist, key) {
  481. if (!compare_css_sets(cset, old_cset, cgrp, template))
  482. continue;
  483. /* This css_set matches what we need */
  484. return cset;
  485. }
  486. /* No existing cgroup group matched */
  487. return NULL;
  488. }
  489. static void free_cgrp_cset_links(struct list_head *links_to_free)
  490. {
  491. struct cgrp_cset_link *link, *tmp_link;
  492. list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
  493. list_del(&link->cset_link);
  494. kfree(link);
  495. }
  496. }
  497. /**
  498. * allocate_cgrp_cset_links - allocate cgrp_cset_links
  499. * @count: the number of links to allocate
  500. * @tmp_links: list_head the allocated links are put on
  501. *
  502. * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
  503. * through ->cset_link. Returns 0 on success or -errno.
  504. */
  505. static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
  506. {
  507. struct cgrp_cset_link *link;
  508. int i;
  509. INIT_LIST_HEAD(tmp_links);
  510. for (i = 0; i < count; i++) {
  511. link = kzalloc(sizeof(*link), GFP_KERNEL);
  512. if (!link) {
  513. free_cgrp_cset_links(tmp_links);
  514. return -ENOMEM;
  515. }
  516. list_add(&link->cset_link, tmp_links);
  517. }
  518. return 0;
  519. }
  520. /**
  521. * link_css_set - a helper function to link a css_set to a cgroup
  522. * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
  523. * @cset: the css_set to be linked
  524. * @cgrp: the destination cgroup
  525. */
  526. static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
  527. struct cgroup *cgrp)
  528. {
  529. struct cgrp_cset_link *link;
  530. BUG_ON(list_empty(tmp_links));
  531. link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
  532. link->cset = cset;
  533. link->cgrp = cgrp;
  534. list_move(&link->cset_link, &cgrp->cset_links);
  535. /*
  536. * Always add links to the tail of the list so that the list
  537. * is sorted by order of hierarchy creation
  538. */
  539. list_add_tail(&link->cgrp_link, &cset->cgrp_links);
  540. }
  541. /**
  542. * find_css_set - return a new css_set with one cgroup updated
  543. * @old_cset: the baseline css_set
  544. * @cgrp: the cgroup to be updated
  545. *
  546. * Return a new css_set that's equivalent to @old_cset, but with @cgrp
  547. * substituted into the appropriate hierarchy.
  548. */
  549. static struct css_set *find_css_set(struct css_set *old_cset,
  550. struct cgroup *cgrp)
  551. {
  552. struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { };
  553. struct css_set *cset;
  554. struct list_head tmp_links;
  555. struct cgrp_cset_link *link;
  556. unsigned long key;
  557. lockdep_assert_held(&cgroup_mutex);
  558. /* First see if we already have a cgroup group that matches
  559. * the desired set */
  560. down_read(&css_set_rwsem);
  561. cset = find_existing_css_set(old_cset, cgrp, template);
  562. if (cset)
  563. get_css_set(cset);
  564. up_read(&css_set_rwsem);
  565. if (cset)
  566. return cset;
  567. cset = kzalloc(sizeof(*cset), GFP_KERNEL);
  568. if (!cset)
  569. return NULL;
  570. /* Allocate all the cgrp_cset_link objects that we'll need */
  571. if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) {
  572. kfree(cset);
  573. return NULL;
  574. }
  575. atomic_set(&cset->refcount, 1);
  576. INIT_LIST_HEAD(&cset->cgrp_links);
  577. INIT_LIST_HEAD(&cset->tasks);
  578. INIT_LIST_HEAD(&cset->mg_tasks);
  579. INIT_LIST_HEAD(&cset->mg_preload_node);
  580. INIT_LIST_HEAD(&cset->mg_node);
  581. INIT_HLIST_NODE(&cset->hlist);
  582. /* Copy the set of subsystem state objects generated in
  583. * find_existing_css_set() */
  584. memcpy(cset->subsys, template, sizeof(cset->subsys));
  585. down_write(&css_set_rwsem);
  586. /* Add reference counts and links from the new css_set. */
  587. list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
  588. struct cgroup *c = link->cgrp;
  589. if (c->root == cgrp->root)
  590. c = cgrp;
  591. link_css_set(&tmp_links, cset, c);
  592. }
  593. BUG_ON(!list_empty(&tmp_links));
  594. css_set_count++;
  595. /* Add this cgroup group to the hash table */
  596. key = css_set_hash(cset->subsys);
  597. hash_add(css_set_table, &cset->hlist, key);
  598. up_write(&css_set_rwsem);
  599. return cset;
  600. }
  601. static struct cgroupfs_root *cgroup_root_from_kf(struct kernfs_root *kf_root)
  602. {
  603. struct cgroup *top_cgrp = kf_root->kn->priv;
  604. return top_cgrp->root;
  605. }
  606. static int cgroup_init_root_id(struct cgroupfs_root *root, int start, int end)
  607. {
  608. int id;
  609. lockdep_assert_held(&cgroup_mutex);
  610. id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, start, end,
  611. GFP_KERNEL);
  612. if (id < 0)
  613. return id;
  614. root->hierarchy_id = id;
  615. return 0;
  616. }
  617. static void cgroup_exit_root_id(struct cgroupfs_root *root)
  618. {
  619. lockdep_assert_held(&cgroup_mutex);
  620. if (root->hierarchy_id) {
  621. idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
  622. root->hierarchy_id = 0;
  623. }
  624. }
  625. static void cgroup_free_root(struct cgroupfs_root *root)
  626. {
  627. if (root) {
  628. /* hierarhcy ID shoulid already have been released */
  629. WARN_ON_ONCE(root->hierarchy_id);
  630. idr_destroy(&root->cgroup_idr);
  631. kfree(root);
  632. }
  633. }
  634. static void cgroup_destroy_root(struct cgroupfs_root *root)
  635. {
  636. struct cgroup *cgrp = &root->top_cgroup;
  637. struct cgrp_cset_link *link, *tmp_link;
  638. mutex_lock(&cgroup_tree_mutex);
  639. mutex_lock(&cgroup_mutex);
  640. BUG_ON(atomic_read(&root->nr_cgrps));
  641. BUG_ON(!list_empty(&cgrp->children));
  642. /* Rebind all subsystems back to the default hierarchy */
  643. WARN_ON(rebind_subsystems(root, 0, root->subsys_mask));
  644. /*
  645. * Release all the links from cset_links to this hierarchy's
  646. * root cgroup
  647. */
  648. down_write(&css_set_rwsem);
  649. list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
  650. list_del(&link->cset_link);
  651. list_del(&link->cgrp_link);
  652. kfree(link);
  653. }
  654. up_write(&css_set_rwsem);
  655. if (!list_empty(&root->root_list)) {
  656. list_del(&root->root_list);
  657. cgroup_root_count--;
  658. }
  659. cgroup_exit_root_id(root);
  660. mutex_unlock(&cgroup_mutex);
  661. mutex_unlock(&cgroup_tree_mutex);
  662. kernfs_destroy_root(root->kf_root);
  663. cgroup_free_root(root);
  664. }
  665. /* look up cgroup associated with given css_set on the specified hierarchy */
  666. static struct cgroup *cset_cgroup_from_root(struct css_set *cset,
  667. struct cgroupfs_root *root)
  668. {
  669. struct cgroup *res = NULL;
  670. lockdep_assert_held(&cgroup_mutex);
  671. lockdep_assert_held(&css_set_rwsem);
  672. if (cset == &init_css_set) {
  673. res = &root->top_cgroup;
  674. } else {
  675. struct cgrp_cset_link *link;
  676. list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
  677. struct cgroup *c = link->cgrp;
  678. if (c->root == root) {
  679. res = c;
  680. break;
  681. }
  682. }
  683. }
  684. BUG_ON(!res);
  685. return res;
  686. }
  687. /*
  688. * Return the cgroup for "task" from the given hierarchy. Must be
  689. * called with cgroup_mutex and css_set_rwsem held.
  690. */
  691. static struct cgroup *task_cgroup_from_root(struct task_struct *task,
  692. struct cgroupfs_root *root)
  693. {
  694. /*
  695. * No need to lock the task - since we hold cgroup_mutex the
  696. * task can't change groups, so the only thing that can happen
  697. * is that it exits and its css is set back to init_css_set.
  698. */
  699. return cset_cgroup_from_root(task_css_set(task), root);
  700. }
  701. /*
  702. * A task must hold cgroup_mutex to modify cgroups.
  703. *
  704. * Any task can increment and decrement the count field without lock.
  705. * So in general, code holding cgroup_mutex can't rely on the count
  706. * field not changing. However, if the count goes to zero, then only
  707. * cgroup_attach_task() can increment it again. Because a count of zero
  708. * means that no tasks are currently attached, therefore there is no
  709. * way a task attached to that cgroup can fork (the other way to
  710. * increment the count). So code holding cgroup_mutex can safely
  711. * assume that if the count is zero, it will stay zero. Similarly, if
  712. * a task holds cgroup_mutex on a cgroup with zero count, it
  713. * knows that the cgroup won't be removed, as cgroup_rmdir()
  714. * needs that mutex.
  715. *
  716. * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
  717. * (usually) take cgroup_mutex. These are the two most performance
  718. * critical pieces of code here. The exception occurs on cgroup_exit(),
  719. * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
  720. * is taken, and if the cgroup count is zero, a usermode call made
  721. * to the release agent with the name of the cgroup (path relative to
  722. * the root of cgroup file system) as the argument.
  723. *
  724. * A cgroup can only be deleted if both its 'count' of using tasks
  725. * is zero, and its list of 'children' cgroups is empty. Since all
  726. * tasks in the system use _some_ cgroup, and since there is always at
  727. * least one task in the system (init, pid == 1), therefore, top_cgroup
  728. * always has either children cgroups and/or using tasks. So we don't
  729. * need a special hack to ensure that top_cgroup cannot be deleted.
  730. *
  731. * P.S. One more locking exception. RCU is used to guard the
  732. * update of a tasks cgroup pointer by cgroup_attach_task()
  733. */
  734. static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask);
  735. static struct kernfs_syscall_ops cgroup_kf_syscall_ops;
  736. static const struct file_operations proc_cgroupstats_operations;
  737. static char *cgroup_file_name(struct cgroup *cgrp, const struct cftype *cft,
  738. char *buf)
  739. {
  740. if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) &&
  741. !(cgrp->root->flags & CGRP_ROOT_NOPREFIX))
  742. snprintf(buf, CGROUP_FILE_NAME_MAX, "%s.%s",
  743. cft->ss->name, cft->name);
  744. else
  745. strncpy(buf, cft->name, CGROUP_FILE_NAME_MAX);
  746. return buf;
  747. }
  748. /**
  749. * cgroup_file_mode - deduce file mode of a control file
  750. * @cft: the control file in question
  751. *
  752. * returns cft->mode if ->mode is not 0
  753. * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
  754. * returns S_IRUGO if it has only a read handler
  755. * returns S_IWUSR if it has only a write hander
  756. */
  757. static umode_t cgroup_file_mode(const struct cftype *cft)
  758. {
  759. umode_t mode = 0;
  760. if (cft->mode)
  761. return cft->mode;
  762. if (cft->read_u64 || cft->read_s64 || cft->seq_show)
  763. mode |= S_IRUGO;
  764. if (cft->write_u64 || cft->write_s64 || cft->write_string ||
  765. cft->trigger)
  766. mode |= S_IWUSR;
  767. return mode;
  768. }
  769. static void cgroup_free_fn(struct work_struct *work)
  770. {
  771. struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
  772. atomic_dec(&cgrp->root->nr_cgrps);
  773. cgroup_pidlist_destroy_all(cgrp);
  774. if (cgrp->parent) {
  775. /*
  776. * We get a ref to the parent, and put the ref when this
  777. * cgroup is being freed, so it's guaranteed that the
  778. * parent won't be destroyed before its children.
  779. */
  780. cgroup_put(cgrp->parent);
  781. kernfs_put(cgrp->kn);
  782. kfree(cgrp);
  783. } else {
  784. /*
  785. * This is top cgroup's refcnt reaching zero, which
  786. * indicates that the root should be released.
  787. */
  788. cgroup_destroy_root(cgrp->root);
  789. }
  790. }
  791. static void cgroup_free_rcu(struct rcu_head *head)
  792. {
  793. struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head);
  794. INIT_WORK(&cgrp->destroy_work, cgroup_free_fn);
  795. queue_work(cgroup_destroy_wq, &cgrp->destroy_work);
  796. }
  797. static void cgroup_get(struct cgroup *cgrp)
  798. {
  799. WARN_ON_ONCE(cgroup_is_dead(cgrp));
  800. WARN_ON_ONCE(atomic_read(&cgrp->refcnt) <= 0);
  801. atomic_inc(&cgrp->refcnt);
  802. }
  803. static void cgroup_put(struct cgroup *cgrp)
  804. {
  805. if (!atomic_dec_and_test(&cgrp->refcnt))
  806. return;
  807. if (WARN_ON_ONCE(cgrp->parent && !cgroup_is_dead(cgrp)))
  808. return;
  809. /*
  810. * XXX: cgrp->id is only used to look up css's. As cgroup and
  811. * css's lifetimes will be decoupled, it should be made
  812. * per-subsystem and moved to css->id so that lookups are
  813. * successful until the target css is released.
  814. */
  815. mutex_lock(&cgroup_mutex);
  816. idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
  817. mutex_unlock(&cgroup_mutex);
  818. cgrp->id = -1;
  819. call_rcu(&cgrp->rcu_head, cgroup_free_rcu);
  820. }
  821. static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
  822. {
  823. char name[CGROUP_FILE_NAME_MAX];
  824. lockdep_assert_held(&cgroup_tree_mutex);
  825. kernfs_remove_by_name(cgrp->kn, cgroup_file_name(cgrp, cft, name));
  826. }
  827. /**
  828. * cgroup_clear_dir - remove subsys files in a cgroup directory
  829. * @cgrp: target cgroup
  830. * @subsys_mask: mask of the subsystem ids whose files should be removed
  831. */
  832. static void cgroup_clear_dir(struct cgroup *cgrp, unsigned long subsys_mask)
  833. {
  834. struct cgroup_subsys *ss;
  835. int i;
  836. for_each_subsys(ss, i) {
  837. struct cftype *cfts;
  838. if (!test_bit(i, &subsys_mask))
  839. continue;
  840. list_for_each_entry(cfts, &ss->cfts, node)
  841. cgroup_addrm_files(cgrp, cfts, false);
  842. }
  843. }
  844. static int rebind_subsystems(struct cgroupfs_root *root,
  845. unsigned long added_mask, unsigned removed_mask)
  846. {
  847. struct cgroup *cgrp = &root->top_cgroup;
  848. struct cgroup_subsys *ss;
  849. int i, ret;
  850. lockdep_assert_held(&cgroup_tree_mutex);
  851. lockdep_assert_held(&cgroup_mutex);
  852. /* Check that any added subsystems are currently free */
  853. for_each_subsys(ss, i)
  854. if ((added_mask & (1 << i)) && ss->root != &cgroup_dummy_root)
  855. return -EBUSY;
  856. ret = cgroup_populate_dir(cgrp, added_mask);
  857. if (ret)
  858. return ret;
  859. /*
  860. * Nothing can fail from this point on. Remove files for the
  861. * removed subsystems and rebind each subsystem.
  862. */
  863. mutex_unlock(&cgroup_mutex);
  864. cgroup_clear_dir(cgrp, removed_mask);
  865. mutex_lock(&cgroup_mutex);
  866. for_each_subsys(ss, i) {
  867. unsigned long bit = 1UL << i;
  868. if (bit & added_mask) {
  869. /* We're binding this subsystem to this hierarchy */
  870. BUG_ON(cgroup_css(cgrp, ss));
  871. BUG_ON(!cgroup_css(cgroup_dummy_top, ss));
  872. BUG_ON(cgroup_css(cgroup_dummy_top, ss)->cgroup != cgroup_dummy_top);
  873. rcu_assign_pointer(cgrp->subsys[i],
  874. cgroup_css(cgroup_dummy_top, ss));
  875. cgroup_css(cgrp, ss)->cgroup = cgrp;
  876. ss->root = root;
  877. if (ss->bind)
  878. ss->bind(cgroup_css(cgrp, ss));
  879. /* refcount was already taken, and we're keeping it */
  880. root->subsys_mask |= bit;
  881. } else if (bit & removed_mask) {
  882. /* We're removing this subsystem */
  883. BUG_ON(cgroup_css(cgrp, ss) != cgroup_css(cgroup_dummy_top, ss));
  884. BUG_ON(cgroup_css(cgrp, ss)->cgroup != cgrp);
  885. if (ss->bind)
  886. ss->bind(cgroup_css(cgroup_dummy_top, ss));
  887. cgroup_css(cgroup_dummy_top, ss)->cgroup = cgroup_dummy_top;
  888. RCU_INIT_POINTER(cgrp->subsys[i], NULL);
  889. cgroup_subsys[i]->root = &cgroup_dummy_root;
  890. root->subsys_mask &= ~bit;
  891. }
  892. }
  893. kernfs_activate(cgrp->kn);
  894. return 0;
  895. }
  896. static int cgroup_show_options(struct seq_file *seq,
  897. struct kernfs_root *kf_root)
  898. {
  899. struct cgroupfs_root *root = cgroup_root_from_kf(kf_root);
  900. struct cgroup_subsys *ss;
  901. int ssid;
  902. for_each_subsys(ss, ssid)
  903. if (root->subsys_mask & (1 << ssid))
  904. seq_printf(seq, ",%s", ss->name);
  905. if (root->flags & CGRP_ROOT_SANE_BEHAVIOR)
  906. seq_puts(seq, ",sane_behavior");
  907. if (root->flags & CGRP_ROOT_NOPREFIX)
  908. seq_puts(seq, ",noprefix");
  909. if (root->flags & CGRP_ROOT_XATTR)
  910. seq_puts(seq, ",xattr");
  911. spin_lock(&release_agent_path_lock);
  912. if (strlen(root->release_agent_path))
  913. seq_printf(seq, ",release_agent=%s", root->release_agent_path);
  914. spin_unlock(&release_agent_path_lock);
  915. if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags))
  916. seq_puts(seq, ",clone_children");
  917. if (strlen(root->name))
  918. seq_printf(seq, ",name=%s", root->name);
  919. return 0;
  920. }
  921. struct cgroup_sb_opts {
  922. unsigned long subsys_mask;
  923. unsigned long flags;
  924. char *release_agent;
  925. bool cpuset_clone_children;
  926. char *name;
  927. /* User explicitly requested empty subsystem */
  928. bool none;
  929. };
  930. /*
  931. * Convert a hierarchy specifier into a bitmask of subsystems and
  932. * flags. Call with cgroup_mutex held to protect the cgroup_subsys[]
  933. * array. This function takes refcounts on subsystems to be used, unless it
  934. * returns error, in which case no refcounts are taken.
  935. */
  936. static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
  937. {
  938. char *token, *o = data;
  939. bool all_ss = false, one_ss = false;
  940. unsigned long mask = (unsigned long)-1;
  941. struct cgroup_subsys *ss;
  942. int i;
  943. BUG_ON(!mutex_is_locked(&cgroup_mutex));
  944. #ifdef CONFIG_CPUSETS
  945. mask = ~(1UL << cpuset_cgrp_id);
  946. #endif
  947. memset(opts, 0, sizeof(*opts));
  948. while ((token = strsep(&o, ",")) != NULL) {
  949. if (!*token)
  950. return -EINVAL;
  951. if (!strcmp(token, "none")) {
  952. /* Explicitly have no subsystems */
  953. opts->none = true;
  954. continue;
  955. }
  956. if (!strcmp(token, "all")) {
  957. /* Mutually exclusive option 'all' + subsystem name */
  958. if (one_ss)
  959. return -EINVAL;
  960. all_ss = true;
  961. continue;
  962. }
  963. if (!strcmp(token, "__DEVEL__sane_behavior")) {
  964. opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
  965. continue;
  966. }
  967. if (!strcmp(token, "noprefix")) {
  968. opts->flags |= CGRP_ROOT_NOPREFIX;
  969. continue;
  970. }
  971. if (!strcmp(token, "clone_children")) {
  972. opts->cpuset_clone_children = true;
  973. continue;
  974. }
  975. if (!strcmp(token, "xattr")) {
  976. opts->flags |= CGRP_ROOT_XATTR;
  977. continue;
  978. }
  979. if (!strncmp(token, "release_agent=", 14)) {
  980. /* Specifying two release agents is forbidden */
  981. if (opts->release_agent)
  982. return -EINVAL;
  983. opts->release_agent =
  984. kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
  985. if (!opts->release_agent)
  986. return -ENOMEM;
  987. continue;
  988. }
  989. if (!strncmp(token, "name=", 5)) {
  990. const char *name = token + 5;
  991. /* Can't specify an empty name */
  992. if (!strlen(name))
  993. return -EINVAL;
  994. /* Must match [\w.-]+ */
  995. for (i = 0; i < strlen(name); i++) {
  996. char c = name[i];
  997. if (isalnum(c))
  998. continue;
  999. if ((c == '.') || (c == '-') || (c == '_'))
  1000. continue;
  1001. return -EINVAL;
  1002. }
  1003. /* Specifying two names is forbidden */
  1004. if (opts->name)
  1005. return -EINVAL;
  1006. opts->name = kstrndup(name,
  1007. MAX_CGROUP_ROOT_NAMELEN - 1,
  1008. GFP_KERNEL);
  1009. if (!opts->name)
  1010. return -ENOMEM;
  1011. continue;
  1012. }
  1013. for_each_subsys(ss, i) {
  1014. if (strcmp(token, ss->name))
  1015. continue;
  1016. if (ss->disabled)
  1017. continue;
  1018. /* Mutually exclusive option 'all' + subsystem name */
  1019. if (all_ss)
  1020. return -EINVAL;
  1021. set_bit(i, &opts->subsys_mask);
  1022. one_ss = true;
  1023. break;
  1024. }
  1025. if (i == CGROUP_SUBSYS_COUNT)
  1026. return -ENOENT;
  1027. }
  1028. /*
  1029. * If the 'all' option was specified select all the subsystems,
  1030. * otherwise if 'none', 'name=' and a subsystem name options
  1031. * were not specified, let's default to 'all'
  1032. */
  1033. if (all_ss || (!one_ss && !opts->none && !opts->name))
  1034. for_each_subsys(ss, i)
  1035. if (!ss->disabled)
  1036. set_bit(i, &opts->subsys_mask);
  1037. /* Consistency checks */
  1038. if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
  1039. pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
  1040. if ((opts->flags & (CGRP_ROOT_NOPREFIX | CGRP_ROOT_XATTR)) ||
  1041. opts->cpuset_clone_children || opts->release_agent ||
  1042. opts->name) {
  1043. pr_err("cgroup: sane_behavior: noprefix, xattr, clone_children, release_agent and name are not allowed\n");
  1044. return -EINVAL;
  1045. }
  1046. }
  1047. /*
  1048. * Option noprefix was introduced just for backward compatibility
  1049. * with the old cpuset, so we allow noprefix only if mounting just
  1050. * the cpuset subsystem.
  1051. */
  1052. if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
  1053. return -EINVAL;
  1054. /* Can't specify "none" and some subsystems */
  1055. if (opts->subsys_mask && opts->none)
  1056. return -EINVAL;
  1057. /*
  1058. * We either have to specify by name or by subsystems. (So all
  1059. * empty hierarchies must have a name).
  1060. */
  1061. if (!opts->subsys_mask && !opts->name)
  1062. return -EINVAL;
  1063. return 0;
  1064. }
  1065. static int cgroup_remount(struct kernfs_root *kf_root, int *flags, char *data)
  1066. {
  1067. int ret = 0;
  1068. struct cgroupfs_root *root = cgroup_root_from_kf(kf_root);
  1069. struct cgroup_sb_opts opts;
  1070. unsigned long added_mask, removed_mask;
  1071. if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) {
  1072. pr_err("cgroup: sane_behavior: remount is not allowed\n");
  1073. return -EINVAL;
  1074. }
  1075. mutex_lock(&cgroup_tree_mutex);
  1076. mutex_lock(&cgroup_mutex);
  1077. /* See what subsystems are wanted */
  1078. ret = parse_cgroupfs_options(data, &opts);
  1079. if (ret)
  1080. goto out_unlock;
  1081. if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
  1082. pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
  1083. task_tgid_nr(current), current->comm);
  1084. added_mask = opts.subsys_mask & ~root->subsys_mask;
  1085. removed_mask = root->subsys_mask & ~opts.subsys_mask;
  1086. /* Don't allow flags or name to change at remount */
  1087. if (((opts.flags ^ root->flags) & CGRP_ROOT_OPTION_MASK) ||
  1088. (opts.name && strcmp(opts.name, root->name))) {
  1089. pr_err("cgroup: option or name mismatch, new: 0x%lx \"%s\", old: 0x%lx \"%s\"\n",
  1090. opts.flags & CGRP_ROOT_OPTION_MASK, opts.name ?: "",
  1091. root->flags & CGRP_ROOT_OPTION_MASK, root->name);
  1092. ret = -EINVAL;
  1093. goto out_unlock;
  1094. }
  1095. /* remounting is not allowed for populated hierarchies */
  1096. if (!list_empty(&root->top_cgroup.children)) {
  1097. ret = -EBUSY;
  1098. goto out_unlock;
  1099. }
  1100. ret = rebind_subsystems(root, added_mask, removed_mask);
  1101. if (ret)
  1102. goto out_unlock;
  1103. if (opts.release_agent) {
  1104. spin_lock(&release_agent_path_lock);
  1105. strcpy(root->release_agent_path, opts.release_agent);
  1106. spin_unlock(&release_agent_path_lock);
  1107. }
  1108. out_unlock:
  1109. kfree(opts.release_agent);
  1110. kfree(opts.name);
  1111. mutex_unlock(&cgroup_mutex);
  1112. mutex_unlock(&cgroup_tree_mutex);
  1113. return ret;
  1114. }
  1115. /*
  1116. * To reduce the fork() overhead for systems that are not actually using
  1117. * their cgroups capability, we don't maintain the lists running through
  1118. * each css_set to its tasks until we see the list actually used - in other
  1119. * words after the first mount.
  1120. */
  1121. static bool use_task_css_set_links __read_mostly;
  1122. static void cgroup_enable_task_cg_lists(void)
  1123. {
  1124. struct task_struct *p, *g;
  1125. down_write(&css_set_rwsem);
  1126. if (use_task_css_set_links)
  1127. goto out_unlock;
  1128. use_task_css_set_links = true;
  1129. /*
  1130. * We need tasklist_lock because RCU is not safe against
  1131. * while_each_thread(). Besides, a forking task that has passed
  1132. * cgroup_post_fork() without seeing use_task_css_set_links = 1
  1133. * is not guaranteed to have its child immediately visible in the
  1134. * tasklist if we walk through it with RCU.
  1135. */
  1136. read_lock(&tasklist_lock);
  1137. do_each_thread(g, p) {
  1138. WARN_ON_ONCE(!list_empty(&p->cg_list) ||
  1139. task_css_set(p) != &init_css_set);
  1140. /*
  1141. * We should check if the process is exiting, otherwise
  1142. * it will race with cgroup_exit() in that the list
  1143. * entry won't be deleted though the process has exited.
  1144. * Do it while holding siglock so that we don't end up
  1145. * racing against cgroup_exit().
  1146. */
  1147. spin_lock_irq(&p->sighand->siglock);
  1148. if (!(p->flags & PF_EXITING)) {
  1149. struct css_set *cset = task_css_set(p);
  1150. list_add(&p->cg_list, &cset->tasks);
  1151. get_css_set(cset);
  1152. }
  1153. spin_unlock_irq(&p->sighand->siglock);
  1154. } while_each_thread(g, p);
  1155. read_unlock(&tasklist_lock);
  1156. out_unlock:
  1157. up_write(&css_set_rwsem);
  1158. }
  1159. static void init_cgroup_housekeeping(struct cgroup *cgrp)
  1160. {
  1161. atomic_set(&cgrp->refcnt, 1);
  1162. INIT_LIST_HEAD(&cgrp->sibling);
  1163. INIT_LIST_HEAD(&cgrp->children);
  1164. INIT_LIST_HEAD(&cgrp->cset_links);
  1165. INIT_LIST_HEAD(&cgrp->release_list);
  1166. INIT_LIST_HEAD(&cgrp->pidlists);
  1167. mutex_init(&cgrp->pidlist_mutex);
  1168. cgrp->dummy_css.cgroup = cgrp;
  1169. }
  1170. static void init_cgroup_root(struct cgroupfs_root *root,
  1171. struct cgroup_sb_opts *opts)
  1172. {
  1173. struct cgroup *cgrp = &root->top_cgroup;
  1174. INIT_LIST_HEAD(&root->root_list);
  1175. atomic_set(&root->nr_cgrps, 1);
  1176. cgrp->root = root;
  1177. init_cgroup_housekeeping(cgrp);
  1178. idr_init(&root->cgroup_idr);
  1179. root->flags = opts->flags;
  1180. if (opts->release_agent)
  1181. strcpy(root->release_agent_path, opts->release_agent);
  1182. if (opts->name)
  1183. strcpy(root->name, opts->name);
  1184. if (opts->cpuset_clone_children)
  1185. set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags);
  1186. }
  1187. static int cgroup_setup_root(struct cgroupfs_root *root, unsigned long ss_mask)
  1188. {
  1189. LIST_HEAD(tmp_links);
  1190. struct cgroup *root_cgrp = &root->top_cgroup;
  1191. struct css_set *cset;
  1192. int i, ret;
  1193. lockdep_assert_held(&cgroup_tree_mutex);
  1194. lockdep_assert_held(&cgroup_mutex);
  1195. ret = idr_alloc(&root->cgroup_idr, root_cgrp, 0, 1, GFP_KERNEL);
  1196. if (ret < 0)
  1197. goto out;
  1198. root_cgrp->id = ret;
  1199. /*
  1200. * We're accessing css_set_count without locking css_set_rwsem here,
  1201. * but that's OK - it can only be increased by someone holding
  1202. * cgroup_lock, and that's us. The worst that can happen is that we
  1203. * have some link structures left over
  1204. */
  1205. ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
  1206. if (ret)
  1207. goto out;
  1208. /* ID 0 is reserved for dummy root, 1 for unified hierarchy */
  1209. ret = cgroup_init_root_id(root, 2, 0);
  1210. if (ret)
  1211. goto out;
  1212. root->kf_root = kernfs_create_root(&cgroup_kf_syscall_ops,
  1213. KERNFS_ROOT_CREATE_DEACTIVATED,
  1214. root_cgrp);
  1215. if (IS_ERR(root->kf_root)) {
  1216. ret = PTR_ERR(root->kf_root);
  1217. goto exit_root_id;
  1218. }
  1219. root_cgrp->kn = root->kf_root->kn;
  1220. ret = cgroup_addrm_files(root_cgrp, cgroup_base_files, true);
  1221. if (ret)
  1222. goto destroy_root;
  1223. ret = rebind_subsystems(root, ss_mask, 0);
  1224. if (ret)
  1225. goto destroy_root;
  1226. /*
  1227. * There must be no failure case after here, since rebinding takes
  1228. * care of subsystems' refcounts, which are explicitly dropped in
  1229. * the failure exit path.
  1230. */
  1231. list_add(&root->root_list, &cgroup_roots);
  1232. cgroup_root_count++;
  1233. /*
  1234. * Link the top cgroup in this hierarchy into all the css_set
  1235. * objects.
  1236. */
  1237. down_write(&css_set_rwsem);
  1238. hash_for_each(css_set_table, i, cset, hlist)
  1239. link_css_set(&tmp_links, cset, root_cgrp);
  1240. up_write(&css_set_rwsem);
  1241. BUG_ON(!list_empty(&root_cgrp->children));
  1242. BUG_ON(atomic_read(&root->nr_cgrps) != 1);
  1243. kernfs_activate(root_cgrp->kn);
  1244. ret = 0;
  1245. goto out;
  1246. destroy_root:
  1247. kernfs_destroy_root(root->kf_root);
  1248. root->kf_root = NULL;
  1249. exit_root_id:
  1250. cgroup_exit_root_id(root);
  1251. out:
  1252. free_cgrp_cset_links(&tmp_links);
  1253. return ret;
  1254. }
  1255. static struct dentry *cgroup_mount(struct file_system_type *fs_type,
  1256. int flags, const char *unused_dev_name,
  1257. void *data)
  1258. {
  1259. struct cgroupfs_root *root;
  1260. struct cgroup_sb_opts opts;
  1261. struct dentry *dentry;
  1262. int ret;
  1263. /*
  1264. * The first time anyone tries to mount a cgroup, enable the list
  1265. * linking each css_set to its tasks and fix up all existing tasks.
  1266. */
  1267. if (!use_task_css_set_links)
  1268. cgroup_enable_task_cg_lists();
  1269. retry:
  1270. mutex_lock(&cgroup_tree_mutex);
  1271. mutex_lock(&cgroup_mutex);
  1272. /* First find the desired set of subsystems */
  1273. ret = parse_cgroupfs_options(data, &opts);
  1274. if (ret)
  1275. goto out_unlock;
  1276. /* look for a matching existing root */
  1277. for_each_active_root(root) {
  1278. bool name_match = false;
  1279. /*
  1280. * If we asked for a name then it must match. Also, if
  1281. * name matches but sybsys_mask doesn't, we should fail.
  1282. * Remember whether name matched.
  1283. */
  1284. if (opts.name) {
  1285. if (strcmp(opts.name, root->name))
  1286. continue;
  1287. name_match = true;
  1288. }
  1289. /*
  1290. * If we asked for subsystems (or explicitly for no
  1291. * subsystems) then they must match.
  1292. */
  1293. if ((opts.subsys_mask || opts.none) &&
  1294. (opts.subsys_mask != root->subsys_mask)) {
  1295. if (!name_match)
  1296. continue;
  1297. ret = -EBUSY;
  1298. goto out_unlock;
  1299. }
  1300. if ((root->flags ^ opts.flags) & CGRP_ROOT_OPTION_MASK) {
  1301. if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) {
  1302. pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n");
  1303. ret = -EINVAL;
  1304. goto out_unlock;
  1305. } else {
  1306. pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n");
  1307. }
  1308. }
  1309. /*
  1310. * A root's lifetime is governed by its top cgroup. Zero
  1311. * ref indicate that the root is being destroyed. Wait for
  1312. * destruction to complete so that the subsystems are free.
  1313. * We can use wait_queue for the wait but this path is
  1314. * super cold. Let's just sleep for a bit and retry.
  1315. */
  1316. if (!atomic_inc_not_zero(&root->top_cgroup.refcnt)) {
  1317. mutex_unlock(&cgroup_mutex);
  1318. mutex_unlock(&cgroup_tree_mutex);
  1319. kfree(opts.release_agent);
  1320. kfree(opts.name);
  1321. msleep(10);
  1322. goto retry;
  1323. }
  1324. ret = 0;
  1325. goto out_unlock;
  1326. }
  1327. /*
  1328. * No such thing, create a new one. name= matching without subsys
  1329. * specification is allowed for already existing hierarchies but we
  1330. * can't create new one without subsys specification.
  1331. */
  1332. if (!opts.subsys_mask && !opts.none) {
  1333. ret = -EINVAL;
  1334. goto out_unlock;
  1335. }
  1336. root = kzalloc(sizeof(*root), GFP_KERNEL);
  1337. if (!root) {
  1338. ret = -ENOMEM;
  1339. goto out_unlock;
  1340. }
  1341. init_cgroup_root(root, &opts);
  1342. ret = cgroup_setup_root(root, opts.subsys_mask);
  1343. if (ret)
  1344. cgroup_free_root(root);
  1345. out_unlock:
  1346. mutex_unlock(&cgroup_mutex);
  1347. mutex_unlock(&cgroup_tree_mutex);
  1348. kfree(opts.release_agent);
  1349. kfree(opts.name);
  1350. if (ret)
  1351. return ERR_PTR(ret);
  1352. dentry = kernfs_mount(fs_type, flags, root->kf_root);
  1353. if (IS_ERR(dentry))
  1354. cgroup_put(&root->top_cgroup);
  1355. return dentry;
  1356. }
  1357. static void cgroup_kill_sb(struct super_block *sb)
  1358. {
  1359. struct kernfs_root *kf_root = kernfs_root_from_sb(sb);
  1360. struct cgroupfs_root *root = cgroup_root_from_kf(kf_root);
  1361. cgroup_put(&root->top_cgroup);
  1362. kernfs_kill_sb(sb);
  1363. }
  1364. static struct file_system_type cgroup_fs_type = {
  1365. .name = "cgroup",
  1366. .mount = cgroup_mount,
  1367. .kill_sb = cgroup_kill_sb,
  1368. };
  1369. static struct kobject *cgroup_kobj;
  1370. /**
  1371. * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
  1372. * @task: target task
  1373. * @buf: the buffer to write the path into
  1374. * @buflen: the length of the buffer
  1375. *
  1376. * Determine @task's cgroup on the first (the one with the lowest non-zero
  1377. * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
  1378. * function grabs cgroup_mutex and shouldn't be used inside locks used by
  1379. * cgroup controller callbacks.
  1380. *
  1381. * Return value is the same as kernfs_path().
  1382. */
  1383. char *task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
  1384. {
  1385. struct cgroupfs_root *root;
  1386. struct cgroup *cgrp;
  1387. int hierarchy_id = 1;
  1388. char *path = NULL;
  1389. mutex_lock(&cgroup_mutex);
  1390. down_read(&css_set_rwsem);
  1391. root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
  1392. if (root) {
  1393. cgrp = task_cgroup_from_root(task, root);
  1394. path = cgroup_path(cgrp, buf, buflen);
  1395. } else {
  1396. /* if no hierarchy exists, everyone is in "/" */
  1397. if (strlcpy(buf, "/", buflen) < buflen)
  1398. path = buf;
  1399. }
  1400. up_read(&css_set_rwsem);
  1401. mutex_unlock(&cgroup_mutex);
  1402. return path;
  1403. }
  1404. EXPORT_SYMBOL_GPL(task_cgroup_path);
  1405. /* used to track tasks and other necessary states during migration */
  1406. struct cgroup_taskset {
  1407. /* the src and dst cset list running through cset->mg_node */
  1408. struct list_head src_csets;
  1409. struct list_head dst_csets;
  1410. /*
  1411. * Fields for cgroup_taskset_*() iteration.
  1412. *
  1413. * Before migration is committed, the target migration tasks are on
  1414. * ->mg_tasks of the csets on ->src_csets. After, on ->mg_tasks of
  1415. * the csets on ->dst_csets. ->csets point to either ->src_csets
  1416. * or ->dst_csets depending on whether migration is committed.
  1417. *
  1418. * ->cur_csets and ->cur_task point to the current task position
  1419. * during iteration.
  1420. */
  1421. struct list_head *csets;
  1422. struct css_set *cur_cset;
  1423. struct task_struct *cur_task;
  1424. };
  1425. /**
  1426. * cgroup_taskset_first - reset taskset and return the first task
  1427. * @tset: taskset of interest
  1428. *
  1429. * @tset iteration is initialized and the first task is returned.
  1430. */
  1431. struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
  1432. {
  1433. tset->cur_cset = list_first_entry(tset->csets, struct css_set, mg_node);
  1434. tset->cur_task = NULL;
  1435. return cgroup_taskset_next(tset);
  1436. }
  1437. /**
  1438. * cgroup_taskset_next - iterate to the next task in taskset
  1439. * @tset: taskset of interest
  1440. *
  1441. * Return the next task in @tset. Iteration must have been initialized
  1442. * with cgroup_taskset_first().
  1443. */
  1444. struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
  1445. {
  1446. struct css_set *cset = tset->cur_cset;
  1447. struct task_struct *task = tset->cur_task;
  1448. while (&cset->mg_node != tset->csets) {
  1449. if (!task)
  1450. task = list_first_entry(&cset->mg_tasks,
  1451. struct task_struct, cg_list);
  1452. else
  1453. task = list_next_entry(task, cg_list);
  1454. if (&task->cg_list != &cset->mg_tasks) {
  1455. tset->cur_cset = cset;
  1456. tset->cur_task = task;
  1457. return task;
  1458. }
  1459. cset = list_next_entry(cset, mg_node);
  1460. task = NULL;
  1461. }
  1462. return NULL;
  1463. }
  1464. /**
  1465. * cgroup_task_migrate - move a task from one cgroup to another.
  1466. * @old_cgrp; the cgroup @tsk is being migrated from
  1467. * @tsk: the task being migrated
  1468. * @new_cset: the new css_set @tsk is being attached to
  1469. *
  1470. * Must be called with cgroup_mutex, threadgroup and css_set_rwsem locked.
  1471. */
  1472. static void cgroup_task_migrate(struct cgroup *old_cgrp,
  1473. struct task_struct *tsk,
  1474. struct css_set *new_cset)
  1475. {
  1476. struct css_set *old_cset;
  1477. lockdep_assert_held(&cgroup_mutex);
  1478. lockdep_assert_held(&css_set_rwsem);
  1479. /*
  1480. * We are synchronized through threadgroup_lock() against PF_EXITING
  1481. * setting such that we can't race against cgroup_exit() changing the
  1482. * css_set to init_css_set and dropping the old one.
  1483. */
  1484. WARN_ON_ONCE(tsk->flags & PF_EXITING);
  1485. old_cset = task_css_set(tsk);
  1486. get_css_set(new_cset);
  1487. rcu_assign_pointer(tsk->cgroups, new_cset);
  1488. list_move(&tsk->cg_list, &new_cset->mg_tasks);
  1489. /*
  1490. * We just gained a reference on old_cset by taking it from the
  1491. * task. As trading it for new_cset is protected by cgroup_mutex,
  1492. * we're safe to drop it here; it will be freed under RCU.
  1493. */
  1494. set_bit(CGRP_RELEASABLE, &old_cgrp->flags);
  1495. put_css_set_locked(old_cset, false);
  1496. }
  1497. /**
  1498. * cgroup_migrate_finish - cleanup after attach
  1499. * @preloaded_csets: list of preloaded css_sets
  1500. *
  1501. * Undo cgroup_migrate_add_src() and cgroup_migrate_prepare_dst(). See
  1502. * those functions for details.
  1503. */
  1504. static void cgroup_migrate_finish(struct list_head *preloaded_csets)
  1505. {
  1506. struct css_set *cset, *tmp_cset;
  1507. lockdep_assert_held(&cgroup_mutex);
  1508. down_write(&css_set_rwsem);
  1509. list_for_each_entry_safe(cset, tmp_cset, preloaded_csets, mg_preload_node) {
  1510. cset->mg_src_cgrp = NULL;
  1511. cset->mg_dst_cset = NULL;
  1512. list_del_init(&cset->mg_preload_node);
  1513. put_css_set_locked(cset, false);
  1514. }
  1515. up_write(&css_set_rwsem);
  1516. }
  1517. /**
  1518. * cgroup_migrate_add_src - add a migration source css_set
  1519. * @src_cset: the source css_set to add
  1520. * @dst_cgrp: the destination cgroup
  1521. * @preloaded_csets: list of preloaded css_sets
  1522. *
  1523. * Tasks belonging to @src_cset are about to be migrated to @dst_cgrp. Pin
  1524. * @src_cset and add it to @preloaded_csets, which should later be cleaned
  1525. * up by cgroup_migrate_finish().
  1526. *
  1527. * This function may be called without holding threadgroup_lock even if the
  1528. * target is a process. Threads may be created and destroyed but as long
  1529. * as cgroup_mutex is not dropped, no new css_set can be put into play and
  1530. * the preloaded css_sets are guaranteed to cover all migrations.
  1531. */
  1532. static void cgroup_migrate_add_src(struct css_set *src_cset,
  1533. struct cgroup *dst_cgrp,
  1534. struct list_head *preloaded_csets)
  1535. {
  1536. struct cgroup *src_cgrp;
  1537. lockdep_assert_held(&cgroup_mutex);
  1538. lockdep_assert_held(&css_set_rwsem);
  1539. src_cgrp = cset_cgroup_from_root(src_cset, dst_cgrp->root);
  1540. /* nothing to do if this cset already belongs to the cgroup */
  1541. if (src_cgrp == dst_cgrp)
  1542. return;
  1543. if (!list_empty(&src_cset->mg_preload_node))
  1544. return;
  1545. WARN_ON(src_cset->mg_src_cgrp);
  1546. WARN_ON(!list_empty(&src_cset->mg_tasks));
  1547. WARN_ON(!list_empty(&src_cset->mg_node));
  1548. src_cset->mg_src_cgrp = src_cgrp;
  1549. get_css_set(src_cset);
  1550. list_add(&src_cset->mg_preload_node, preloaded_csets);
  1551. }
  1552. /**
  1553. * cgroup_migrate_prepare_dst - prepare destination css_sets for migration
  1554. * @dst_cgrp: the destination cgroup
  1555. * @preloaded_csets: list of preloaded source css_sets
  1556. *
  1557. * Tasks are about to be moved to @dst_cgrp and all the source css_sets
  1558. * have been preloaded to @preloaded_csets. This function looks up and
  1559. * pins all destination css_sets, links each to its source, and put them on
  1560. * @preloaded_csets.
  1561. *
  1562. * This function must be called after cgroup_migrate_add_src() has been
  1563. * called on each migration source css_set. After migration is performed
  1564. * using cgroup_migrate(), cgroup_migrate_finish() must be called on
  1565. * @preloaded_csets.
  1566. */
  1567. static int cgroup_migrate_prepare_dst(struct cgroup *dst_cgrp,
  1568. struct list_head *preloaded_csets)
  1569. {
  1570. LIST_HEAD(csets);
  1571. struct css_set *src_cset;
  1572. lockdep_assert_held(&cgroup_mutex);
  1573. /* look up the dst cset for each src cset and link it to src */
  1574. list_for_each_entry(src_cset, preloaded_csets, mg_preload_node) {
  1575. struct css_set *dst_cset;
  1576. dst_cset = find_css_set(src_cset, dst_cgrp);
  1577. if (!dst_cset)
  1578. goto err;
  1579. WARN_ON_ONCE(src_cset->mg_dst_cset || dst_cset->mg_dst_cset);
  1580. src_cset->mg_dst_cset = dst_cset;
  1581. if (list_empty(&dst_cset->mg_preload_node))
  1582. list_add(&dst_cset->mg_preload_node, &csets);
  1583. else
  1584. put_css_set(dst_cset, false);
  1585. }
  1586. list_splice(&csets, preloaded_csets);
  1587. return 0;
  1588. err:
  1589. cgroup_migrate_finish(&csets);
  1590. return -ENOMEM;
  1591. }
  1592. /**
  1593. * cgroup_migrate - migrate a process or task to a cgroup
  1594. * @cgrp: the destination cgroup
  1595. * @leader: the leader of the process or the task to migrate
  1596. * @threadgroup: whether @leader points to the whole process or a single task
  1597. *
  1598. * Migrate a process or task denoted by @leader to @cgrp. If migrating a
  1599. * process, the caller must be holding threadgroup_lock of @leader. The
  1600. * caller is also responsible for invoking cgroup_migrate_add_src() and
  1601. * cgroup_migrate_prepare_dst() on the targets before invoking this
  1602. * function and following up with cgroup_migrate_finish().
  1603. *
  1604. * As long as a controller's ->can_attach() doesn't fail, this function is
  1605. * guaranteed to succeed. This means that, excluding ->can_attach()
  1606. * failure, when migrating multiple targets, the success or failure can be
  1607. * decided for all targets by invoking group_migrate_prepare_dst() before
  1608. * actually starting migrating.
  1609. */
  1610. static int cgroup_migrate(struct cgroup *cgrp, struct task_struct *leader,
  1611. bool threadgroup)
  1612. {
  1613. struct cgroup_taskset tset = {
  1614. .src_csets = LIST_HEAD_INIT(tset.src_csets),
  1615. .dst_csets = LIST_HEAD_INIT(tset.dst_csets),
  1616. .csets = &tset.src_csets,
  1617. };
  1618. struct cgroup_subsys_state *css, *failed_css = NULL;
  1619. struct css_set *cset, *tmp_cset;
  1620. struct task_struct *task, *tmp_task;
  1621. int i, ret;
  1622. /*
  1623. * Prevent freeing of tasks while we take a snapshot. Tasks that are
  1624. * already PF_EXITING could be freed from underneath us unless we
  1625. * take an rcu_read_lock.
  1626. */
  1627. down_write(&css_set_rwsem);
  1628. rcu_read_lock();
  1629. task = leader;
  1630. do {
  1631. /* @task either already exited or can't exit until the end */
  1632. if (task->flags & PF_EXITING)
  1633. goto next;
  1634. /* leave @task alone if post_fork() hasn't linked it yet */
  1635. if (list_empty(&task->cg_list))
  1636. goto next;
  1637. cset = task_css_set(task);
  1638. if (!cset->mg_src_cgrp)
  1639. goto next;
  1640. list_move(&task->cg_list, &cset->mg_tasks);
  1641. list_move(&cset->mg_node, &tset.src_csets);
  1642. list_move(&cset->mg_dst_cset->mg_node, &tset.dst_csets);
  1643. next:
  1644. if (!threadgroup)
  1645. break;
  1646. } while_each_thread(leader, task);
  1647. rcu_read_unlock();
  1648. up_write(&css_set_rwsem);
  1649. /* methods shouldn't be called if no task is actually migrating */
  1650. if (list_empty(&tset.src_csets))
  1651. return 0;
  1652. /* check that we can legitimately attach to the cgroup */
  1653. for_each_css(css, i, cgrp) {
  1654. if (css->ss->can_attach) {
  1655. ret = css->ss->can_attach(css, &tset);
  1656. if (ret) {
  1657. failed_css = css;
  1658. goto out_cancel_attach;
  1659. }
  1660. }
  1661. }
  1662. /*
  1663. * Now that we're guaranteed success, proceed to move all tasks to
  1664. * the new cgroup. There are no failure cases after here, so this
  1665. * is the commit point.
  1666. */
  1667. down_write(&css_set_rwsem);
  1668. list_for_each_entry(cset, &tset.src_csets, mg_node) {
  1669. list_for_each_entry_safe(task, tmp_task, &cset->mg_tasks, cg_list)
  1670. cgroup_task_migrate(cset->mg_src_cgrp, task,
  1671. cset->mg_dst_cset);
  1672. }
  1673. up_write(&css_set_rwsem);
  1674. /*
  1675. * Migration is committed, all target tasks are now on dst_csets.
  1676. * Nothing is sensitive to fork() after this point. Notify
  1677. * controllers that migration is complete.
  1678. */
  1679. tset.csets = &tset.dst_csets;
  1680. for_each_css(css, i, cgrp)
  1681. if (css->ss->attach)
  1682. css->ss->attach(css, &tset);
  1683. ret = 0;
  1684. goto out_release_tset;
  1685. out_cancel_attach:
  1686. for_each_css(css, i, cgrp) {
  1687. if (css == failed_css)
  1688. break;
  1689. if (css->ss->cancel_attach)
  1690. css->ss->cancel_attach(css, &tset);
  1691. }
  1692. out_release_tset:
  1693. down_write(&css_set_rwsem);
  1694. list_splice_init(&tset.dst_csets, &tset.src_csets);
  1695. list_for_each_entry_safe(cset, tmp_cset, &tset.src_csets, mg_node) {
  1696. list_splice_init(&cset->mg_tasks, &cset->tasks);
  1697. list_del_init(&cset->mg_node);
  1698. }
  1699. up_write(&css_set_rwsem);
  1700. return ret;
  1701. }
  1702. /**
  1703. * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
  1704. * @dst_cgrp: the cgroup to attach to
  1705. * @leader: the task or the leader of the threadgroup to be attached
  1706. * @threadgroup: attach the whole threadgroup?
  1707. *
  1708. * Call holding cgroup_mutex and threadgroup_lock of @leader.
  1709. */
  1710. static int cgroup_attach_task(struct cgroup *dst_cgrp,
  1711. struct task_struct *leader, bool threadgroup)
  1712. {
  1713. LIST_HEAD(preloaded_csets);
  1714. struct task_struct *task;
  1715. int ret;
  1716. /* look up all src csets */
  1717. down_read(&css_set_rwsem);
  1718. rcu_read_lock();
  1719. task = leader;
  1720. do {
  1721. cgroup_migrate_add_src(task_css_set(task), dst_cgrp,
  1722. &preloaded_csets);
  1723. if (!threadgroup)
  1724. break;
  1725. } while_each_thread(leader, task);
  1726. rcu_read_unlock();
  1727. up_read(&css_set_rwsem);
  1728. /* prepare dst csets and commit */
  1729. ret = cgroup_migrate_prepare_dst(dst_cgrp, &preloaded_csets);
  1730. if (!ret)
  1731. ret = cgroup_migrate(dst_cgrp, leader, threadgroup);
  1732. cgroup_migrate_finish(&preloaded_csets);
  1733. return ret;
  1734. }
  1735. /*
  1736. * Find the task_struct of the task to attach by vpid and pass it along to the
  1737. * function to attach either it or all tasks in its threadgroup. Will lock
  1738. * cgroup_mutex and threadgroup.
  1739. */
  1740. static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
  1741. {
  1742. struct task_struct *tsk;
  1743. const struct cred *cred = current_cred(), *tcred;
  1744. int ret;
  1745. if (!cgroup_lock_live_group(cgrp))
  1746. return -ENODEV;
  1747. retry_find_task:
  1748. rcu_read_lock();
  1749. if (pid) {
  1750. tsk = find_task_by_vpid(pid);
  1751. if (!tsk) {
  1752. rcu_read_unlock();
  1753. ret = -ESRCH;
  1754. goto out_unlock_cgroup;
  1755. }
  1756. /*
  1757. * even if we're attaching all tasks in the thread group, we
  1758. * only need to check permissions on one of them.
  1759. */
  1760. tcred = __task_cred(tsk);
  1761. if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
  1762. !uid_eq(cred->euid, tcred->uid) &&
  1763. !uid_eq(cred->euid, tcred->suid)) {
  1764. rcu_read_unlock();
  1765. ret = -EACCES;
  1766. goto out_unlock_cgroup;
  1767. }
  1768. } else
  1769. tsk = current;
  1770. if (threadgroup)
  1771. tsk = tsk->group_leader;
  1772. /*
  1773. * Workqueue threads may acquire PF_NO_SETAFFINITY and become
  1774. * trapped in a cpuset, or RT worker may be born in a cgroup
  1775. * with no rt_runtime allocated. Just say no.
  1776. */
  1777. if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
  1778. ret = -EINVAL;
  1779. rcu_read_unlock();
  1780. goto out_unlock_cgroup;
  1781. }
  1782. get_task_struct(tsk);
  1783. rcu_read_unlock();
  1784. threadgroup_lock(tsk);
  1785. if (threadgroup) {
  1786. if (!thread_group_leader(tsk)) {
  1787. /*
  1788. * a race with de_thread from another thread's exec()
  1789. * may strip us of our leadership, if this happens,
  1790. * there is no choice but to throw this task away and
  1791. * try again; this is
  1792. * "double-double-toil-and-trouble-check locking".
  1793. */
  1794. threadgroup_unlock(tsk);
  1795. put_task_struct(tsk);
  1796. goto retry_find_task;
  1797. }
  1798. }
  1799. ret = cgroup_attach_task(cgrp, tsk, threadgroup);
  1800. threadgroup_unlock(tsk);
  1801. put_task_struct(tsk);
  1802. out_unlock_cgroup:
  1803. mutex_unlock(&cgroup_mutex);
  1804. return ret;
  1805. }
  1806. /**
  1807. * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
  1808. * @from: attach to all cgroups of a given task
  1809. * @tsk: the task to be attached
  1810. */
  1811. int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
  1812. {
  1813. struct cgroupfs_root *root;
  1814. int retval = 0;
  1815. mutex_lock(&cgroup_mutex);
  1816. for_each_active_root(root) {
  1817. struct cgroup *from_cgrp;
  1818. down_read(&css_set_rwsem);
  1819. from_cgrp = task_cgroup_from_root(from, root);
  1820. up_read(&css_set_rwsem);
  1821. retval = cgroup_attach_task(from_cgrp, tsk, false);
  1822. if (retval)
  1823. break;
  1824. }
  1825. mutex_unlock(&cgroup_mutex);
  1826. return retval;
  1827. }
  1828. EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
  1829. static int cgroup_tasks_write(struct cgroup_subsys_state *css,
  1830. struct cftype *cft, u64 pid)
  1831. {
  1832. return attach_task_by_pid(css->cgroup, pid, false);
  1833. }
  1834. static int cgroup_procs_write(struct cgroup_subsys_state *css,
  1835. struct cftype *cft, u64 tgid)
  1836. {
  1837. return attach_task_by_pid(css->cgroup, tgid, true);
  1838. }
  1839. static int cgroup_release_agent_write(struct cgroup_subsys_state *css,
  1840. struct cftype *cft, const char *buffer)
  1841. {
  1842. struct cgroupfs_root *root = css->cgroup->root;
  1843. BUILD_BUG_ON(sizeof(root->release_agent_path) < PATH_MAX);
  1844. if (!cgroup_lock_live_group(css->cgroup))
  1845. return -ENODEV;
  1846. spin_lock(&release_agent_path_lock);
  1847. strlcpy(root->release_agent_path, buffer,
  1848. sizeof(root->release_agent_path));
  1849. spin_unlock(&release_agent_path_lock);
  1850. mutex_unlock(&cgroup_mutex);
  1851. return 0;
  1852. }
  1853. static int cgroup_release_agent_show(struct seq_file *seq, void *v)
  1854. {
  1855. struct cgroup *cgrp = seq_css(seq)->cgroup;
  1856. if (!cgroup_lock_live_group(cgrp))
  1857. return -ENODEV;
  1858. seq_puts(seq, cgrp->root->release_agent_path);
  1859. seq_putc(seq, '\n');
  1860. mutex_unlock(&cgroup_mutex);
  1861. return 0;
  1862. }
  1863. static int cgroup_sane_behavior_show(struct seq_file *seq, void *v)
  1864. {
  1865. struct cgroup *cgrp = seq_css(seq)->cgroup;
  1866. seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp));
  1867. return 0;
  1868. }
  1869. static ssize_t cgroup_file_write(struct kernfs_open_file *of, char *buf,
  1870. size_t nbytes, loff_t off)
  1871. {
  1872. struct cgroup *cgrp = of->kn->parent->priv;
  1873. struct cftype *cft = of->kn->priv;
  1874. struct cgroup_subsys_state *css;
  1875. int ret;
  1876. /*
  1877. * kernfs guarantees that a file isn't deleted with operations in
  1878. * flight, which means that the matching css is and stays alive and
  1879. * doesn't need to be pinned. The RCU locking is not necessary
  1880. * either. It's just for the convenience of using cgroup_css().
  1881. */
  1882. rcu_read_lock();
  1883. css = cgroup_css(cgrp, cft->ss);
  1884. rcu_read_unlock();
  1885. if (cft->write_string) {
  1886. ret = cft->write_string(css, cft, strstrip(buf));
  1887. } else if (cft->write_u64) {
  1888. unsigned long long v;
  1889. ret = kstrtoull(buf, 0, &v);
  1890. if (!ret)
  1891. ret = cft->write_u64(css, cft, v);
  1892. } else if (cft->write_s64) {
  1893. long long v;
  1894. ret = kstrtoll(buf, 0, &v);
  1895. if (!ret)
  1896. ret = cft->write_s64(css, cft, v);
  1897. } else if (cft->trigger) {
  1898. ret = cft->trigger(css, (unsigned int)cft->private);
  1899. } else {
  1900. ret = -EINVAL;
  1901. }
  1902. return ret ?: nbytes;
  1903. }
  1904. static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos)
  1905. {
  1906. return seq_cft(seq)->seq_start(seq, ppos);
  1907. }
  1908. static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos)
  1909. {
  1910. return seq_cft(seq)->seq_next(seq, v, ppos);
  1911. }
  1912. static void cgroup_seqfile_stop(struct seq_file *seq, void *v)
  1913. {
  1914. seq_cft(seq)->seq_stop(seq, v);
  1915. }
  1916. static int cgroup_seqfile_show(struct seq_file *m, void *arg)
  1917. {
  1918. struct cftype *cft = seq_cft(m);
  1919. struct cgroup_subsys_state *css = seq_css(m);
  1920. if (cft->seq_show)
  1921. return cft->seq_show(m, arg);
  1922. if (cft->read_u64)
  1923. seq_printf(m, "%llu\n", cft->read_u64(css, cft));
  1924. else if (cft->read_s64)
  1925. seq_printf(m, "%lld\n", cft->read_s64(css, cft));
  1926. else
  1927. return -EINVAL;
  1928. return 0;
  1929. }
  1930. static struct kernfs_ops cgroup_kf_single_ops = {
  1931. .atomic_write_len = PAGE_SIZE,
  1932. .write = cgroup_file_write,
  1933. .seq_show = cgroup_seqfile_show,
  1934. };
  1935. static struct kernfs_ops cgroup_kf_ops = {
  1936. .atomic_write_len = PAGE_SIZE,
  1937. .write = cgroup_file_write,
  1938. .seq_start = cgroup_seqfile_start,
  1939. .seq_next = cgroup_seqfile_next,
  1940. .seq_stop = cgroup_seqfile_stop,
  1941. .seq_show = cgroup_seqfile_show,
  1942. };
  1943. /*
  1944. * cgroup_rename - Only allow simple rename of directories in place.
  1945. */
  1946. static int cgroup_rename(struct kernfs_node *kn, struct kernfs_node *new_parent,
  1947. const char *new_name_str)
  1948. {
  1949. struct cgroup *cgrp = kn->priv;
  1950. int ret;
  1951. if (kernfs_type(kn) != KERNFS_DIR)
  1952. return -ENOTDIR;
  1953. if (kn->parent != new_parent)
  1954. return -EIO;
  1955. /*
  1956. * This isn't a proper migration and its usefulness is very
  1957. * limited. Disallow if sane_behavior.
  1958. */
  1959. if (cgroup_sane_behavior(cgrp))
  1960. return -EPERM;
  1961. mutex_lock(&cgroup_tree_mutex);
  1962. mutex_lock(&cgroup_mutex);
  1963. ret = kernfs_rename(kn, new_parent, new_name_str);
  1964. mutex_unlock(&cgroup_mutex);
  1965. mutex_unlock(&cgroup_tree_mutex);
  1966. return ret;
  1967. }
  1968. static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft)
  1969. {
  1970. char name[CGROUP_FILE_NAME_MAX];
  1971. struct kernfs_node *kn;
  1972. struct lock_class_key *key = NULL;
  1973. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  1974. key = &cft->lockdep_key;
  1975. #endif
  1976. kn = __kernfs_create_file(cgrp->kn, cgroup_file_name(cgrp, cft, name),
  1977. cgroup_file_mode(cft), 0, cft->kf_ops, cft,
  1978. NULL, false, key);
  1979. return PTR_ERR_OR_ZERO(kn);
  1980. }
  1981. /**
  1982. * cgroup_addrm_files - add or remove files to a cgroup directory
  1983. * @cgrp: the target cgroup
  1984. * @cfts: array of cftypes to be added
  1985. * @is_add: whether to add or remove
  1986. *
  1987. * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
  1988. * For removals, this function never fails. If addition fails, this
  1989. * function doesn't remove files already added. The caller is responsible
  1990. * for cleaning up.
  1991. */
  1992. static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
  1993. bool is_add)
  1994. {
  1995. struct cftype *cft;
  1996. int ret;
  1997. lockdep_assert_held(&cgroup_tree_mutex);
  1998. for (cft = cfts; cft->name[0] != '\0'; cft++) {
  1999. /* does cft->flags tell us to skip this file on @cgrp? */
  2000. if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
  2001. continue;
  2002. if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
  2003. continue;
  2004. if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
  2005. continue;
  2006. if (is_add) {
  2007. ret = cgroup_add_file(cgrp, cft);
  2008. if (ret) {
  2009. pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
  2010. cft->name, ret);
  2011. return ret;
  2012. }
  2013. } else {
  2014. cgroup_rm_file(cgrp, cft);
  2015. }
  2016. }
  2017. return 0;
  2018. }
  2019. static int cgroup_apply_cftypes(struct cftype *cfts, bool is_add)
  2020. {
  2021. LIST_HEAD(pending);
  2022. struct cgroup_subsys *ss = cfts[0].ss;
  2023. struct cgroup *root = &ss->root->top_cgroup;
  2024. struct cgroup_subsys_state *css;
  2025. int ret = 0;
  2026. lockdep_assert_held(&cgroup_tree_mutex);
  2027. /* don't bother if @ss isn't attached */
  2028. if (ss->root == &cgroup_dummy_root)
  2029. return 0;
  2030. /* add/rm files for all cgroups created before */
  2031. css_for_each_descendant_pre(css, cgroup_css(root, ss)) {
  2032. struct cgroup *cgrp = css->cgroup;
  2033. if (cgroup_is_dead(cgrp))
  2034. continue;
  2035. ret = cgroup_addrm_files(cgrp, cfts, is_add);
  2036. if (ret)
  2037. break;
  2038. }
  2039. if (is_add && !ret)
  2040. kernfs_activate(root->kn);
  2041. return ret;
  2042. }
  2043. static void cgroup_exit_cftypes(struct cftype *cfts)
  2044. {
  2045. struct cftype *cft;
  2046. for (cft = cfts; cft->name[0] != '\0'; cft++) {
  2047. /* free copy for custom atomic_write_len, see init_cftypes() */
  2048. if (cft->max_write_len && cft->max_write_len != PAGE_SIZE)
  2049. kfree(cft->kf_ops);
  2050. cft->kf_ops = NULL;
  2051. cft->ss = NULL;
  2052. }
  2053. }
  2054. static int cgroup_init_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
  2055. {
  2056. struct cftype *cft;
  2057. for (cft = cfts; cft->name[0] != '\0'; cft++) {
  2058. struct kernfs_ops *kf_ops;
  2059. WARN_ON(cft->ss || cft->kf_ops);
  2060. if (cft->seq_start)
  2061. kf_ops = &cgroup_kf_ops;
  2062. else
  2063. kf_ops = &cgroup_kf_single_ops;
  2064. /*
  2065. * Ugh... if @cft wants a custom max_write_len, we need to
  2066. * make a copy of kf_ops to set its atomic_write_len.
  2067. */
  2068. if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) {
  2069. kf_ops = kmemdup(kf_ops, sizeof(*kf_ops), GFP_KERNEL);
  2070. if (!kf_ops) {
  2071. cgroup_exit_cftypes(cfts);
  2072. return -ENOMEM;
  2073. }
  2074. kf_ops->atomic_write_len = cft->max_write_len;
  2075. }
  2076. cft->kf_ops = kf_ops;
  2077. cft->ss = ss;
  2078. }
  2079. return 0;
  2080. }
  2081. static int cgroup_rm_cftypes_locked(struct cftype *cfts)
  2082. {
  2083. lockdep_assert_held(&cgroup_tree_mutex);
  2084. if (!cfts || !cfts[0].ss)
  2085. return -ENOENT;
  2086. list_del(&cfts->node);
  2087. cgroup_apply_cftypes(cfts, false);
  2088. cgroup_exit_cftypes(cfts);
  2089. return 0;
  2090. }
  2091. /**
  2092. * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
  2093. * @cfts: zero-length name terminated array of cftypes
  2094. *
  2095. * Unregister @cfts. Files described by @cfts are removed from all
  2096. * existing cgroups and all future cgroups won't have them either. This
  2097. * function can be called anytime whether @cfts' subsys is attached or not.
  2098. *
  2099. * Returns 0 on successful unregistration, -ENOENT if @cfts is not
  2100. * registered.
  2101. */
  2102. int cgroup_rm_cftypes(struct cftype *cfts)
  2103. {
  2104. int ret;
  2105. mutex_lock(&cgroup_tree_mutex);
  2106. ret = cgroup_rm_cftypes_locked(cfts);
  2107. mutex_unlock(&cgroup_tree_mutex);
  2108. return ret;
  2109. }
  2110. /**
  2111. * cgroup_add_cftypes - add an array of cftypes to a subsystem
  2112. * @ss: target cgroup subsystem
  2113. * @cfts: zero-length name terminated array of cftypes
  2114. *
  2115. * Register @cfts to @ss. Files described by @cfts are created for all
  2116. * existing cgroups to which @ss is attached and all future cgroups will
  2117. * have them too. This function can be called anytime whether @ss is
  2118. * attached or not.
  2119. *
  2120. * Returns 0 on successful registration, -errno on failure. Note that this
  2121. * function currently returns 0 as long as @cfts registration is successful
  2122. * even if some file creation attempts on existing cgroups fail.
  2123. */
  2124. int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
  2125. {
  2126. int ret;
  2127. if (!cfts || cfts[0].name[0] == '\0')
  2128. return 0;
  2129. ret = cgroup_init_cftypes(ss, cfts);
  2130. if (ret)
  2131. return ret;
  2132. mutex_lock(&cgroup_tree_mutex);
  2133. list_add_tail(&cfts->node, &ss->cfts);
  2134. ret = cgroup_apply_cftypes(cfts, true);
  2135. if (ret)
  2136. cgroup_rm_cftypes_locked(cfts);
  2137. mutex_unlock(&cgroup_tree_mutex);
  2138. return ret;
  2139. }
  2140. /**
  2141. * cgroup_task_count - count the number of tasks in a cgroup.
  2142. * @cgrp: the cgroup in question
  2143. *
  2144. * Return the number of tasks in the cgroup.
  2145. */
  2146. static int cgroup_task_count(const struct cgroup *cgrp)
  2147. {
  2148. int count = 0;
  2149. struct cgrp_cset_link *link;
  2150. down_read(&css_set_rwsem);
  2151. list_for_each_entry(link, &cgrp->cset_links, cset_link)
  2152. count += atomic_read(&link->cset->refcount);
  2153. up_read(&css_set_rwsem);
  2154. return count;
  2155. }
  2156. /**
  2157. * css_next_child - find the next child of a given css
  2158. * @pos_css: the current position (%NULL to initiate traversal)
  2159. * @parent_css: css whose children to walk
  2160. *
  2161. * This function returns the next child of @parent_css and should be called
  2162. * under either cgroup_mutex or RCU read lock. The only requirement is
  2163. * that @parent_css and @pos_css are accessible. The next sibling is
  2164. * guaranteed to be returned regardless of their states.
  2165. */
  2166. struct cgroup_subsys_state *
  2167. css_next_child(struct cgroup_subsys_state *pos_css,
  2168. struct cgroup_subsys_state *parent_css)
  2169. {
  2170. struct cgroup *pos = pos_css ? pos_css->cgroup : NULL;
  2171. struct cgroup *cgrp = parent_css->cgroup;
  2172. struct cgroup *next;
  2173. cgroup_assert_mutexes_or_rcu_locked();
  2174. /*
  2175. * @pos could already have been removed. Once a cgroup is removed,
  2176. * its ->sibling.next is no longer updated when its next sibling
  2177. * changes. As CGRP_DEAD assertion is serialized and happens
  2178. * before the cgroup is taken off the ->sibling list, if we see it
  2179. * unasserted, it's guaranteed that the next sibling hasn't
  2180. * finished its grace period even if it's already removed, and thus
  2181. * safe to dereference from this RCU critical section. If
  2182. * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
  2183. * to be visible as %true here.
  2184. *
  2185. * If @pos is dead, its next pointer can't be dereferenced;
  2186. * however, as each cgroup is given a monotonically increasing
  2187. * unique serial number and always appended to the sibling list,
  2188. * the next one can be found by walking the parent's children until
  2189. * we see a cgroup with higher serial number than @pos's. While
  2190. * this path can be slower, it's taken only when either the current
  2191. * cgroup is removed or iteration and removal race.
  2192. */
  2193. if (!pos) {
  2194. next = list_entry_rcu(cgrp->children.next, struct cgroup, sibling);
  2195. } else if (likely(!cgroup_is_dead(pos))) {
  2196. next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
  2197. } else {
  2198. list_for_each_entry_rcu(next, &cgrp->children, sibling)
  2199. if (next->serial_nr > pos->serial_nr)
  2200. break;
  2201. }
  2202. if (&next->sibling == &cgrp->children)
  2203. return NULL;
  2204. return cgroup_css(next, parent_css->ss);
  2205. }
  2206. /**
  2207. * css_next_descendant_pre - find the next descendant for pre-order walk
  2208. * @pos: the current position (%NULL to initiate traversal)
  2209. * @root: css whose descendants to walk
  2210. *
  2211. * To be used by css_for_each_descendant_pre(). Find the next descendant
  2212. * to visit for pre-order traversal of @root's descendants. @root is
  2213. * included in the iteration and the first node to be visited.
  2214. *
  2215. * While this function requires cgroup_mutex or RCU read locking, it
  2216. * doesn't require the whole traversal to be contained in a single critical
  2217. * section. This function will return the correct next descendant as long
  2218. * as both @pos and @root are accessible and @pos is a descendant of @root.
  2219. */
  2220. struct cgroup_subsys_state *
  2221. css_next_descendant_pre(struct cgroup_subsys_state *pos,
  2222. struct cgroup_subsys_state *root)
  2223. {
  2224. struct cgroup_subsys_state *next;
  2225. cgroup_assert_mutexes_or_rcu_locked();
  2226. /* if first iteration, visit @root */
  2227. if (!pos)
  2228. return root;
  2229. /* visit the first child if exists */
  2230. next = css_next_child(NULL, pos);
  2231. if (next)
  2232. return next;
  2233. /* no child, visit my or the closest ancestor's next sibling */
  2234. while (pos != root) {
  2235. next = css_next_child(pos, css_parent(pos));
  2236. if (next)
  2237. return next;
  2238. pos = css_parent(pos);
  2239. }
  2240. return NULL;
  2241. }
  2242. /**
  2243. * css_rightmost_descendant - return the rightmost descendant of a css
  2244. * @pos: css of interest
  2245. *
  2246. * Return the rightmost descendant of @pos. If there's no descendant, @pos
  2247. * is returned. This can be used during pre-order traversal to skip
  2248. * subtree of @pos.
  2249. *
  2250. * While this function requires cgroup_mutex or RCU read locking, it
  2251. * doesn't require the whole traversal to be contained in a single critical
  2252. * section. This function will return the correct rightmost descendant as
  2253. * long as @pos is accessible.
  2254. */
  2255. struct cgroup_subsys_state *
  2256. css_rightmost_descendant(struct cgroup_subsys_state *pos)
  2257. {
  2258. struct cgroup_subsys_state *last, *tmp;
  2259. cgroup_assert_mutexes_or_rcu_locked();
  2260. do {
  2261. last = pos;
  2262. /* ->prev isn't RCU safe, walk ->next till the end */
  2263. pos = NULL;
  2264. css_for_each_child(tmp, last)
  2265. pos = tmp;
  2266. } while (pos);
  2267. return last;
  2268. }
  2269. static struct cgroup_subsys_state *
  2270. css_leftmost_descendant(struct cgroup_subsys_state *pos)
  2271. {
  2272. struct cgroup_subsys_state *last;
  2273. do {
  2274. last = pos;
  2275. pos = css_next_child(NULL, pos);
  2276. } while (pos);
  2277. return last;
  2278. }
  2279. /**
  2280. * css_next_descendant_post - find the next descendant for post-order walk
  2281. * @pos: the current position (%NULL to initiate traversal)
  2282. * @root: css whose descendants to walk
  2283. *
  2284. * To be used by css_for_each_descendant_post(). Find the next descendant
  2285. * to visit for post-order traversal of @root's descendants. @root is
  2286. * included in the iteration and the last node to be visited.
  2287. *
  2288. * While this function requires cgroup_mutex or RCU read locking, it
  2289. * doesn't require the whole traversal to be contained in a single critical
  2290. * section. This function will return the correct next descendant as long
  2291. * as both @pos and @cgroup are accessible and @pos is a descendant of
  2292. * @cgroup.
  2293. */
  2294. struct cgroup_subsys_state *
  2295. css_next_descendant_post(struct cgroup_subsys_state *pos,
  2296. struct cgroup_subsys_state *root)
  2297. {
  2298. struct cgroup_subsys_state *next;
  2299. cgroup_assert_mutexes_or_rcu_locked();
  2300. /* if first iteration, visit leftmost descendant which may be @root */
  2301. if (!pos)
  2302. return css_leftmost_descendant(root);
  2303. /* if we visited @root, we're done */
  2304. if (pos == root)
  2305. return NULL;
  2306. /* if there's an unvisited sibling, visit its leftmost descendant */
  2307. next = css_next_child(pos, css_parent(pos));
  2308. if (next)
  2309. return css_leftmost_descendant(next);
  2310. /* no sibling left, visit parent */
  2311. return css_parent(pos);
  2312. }
  2313. /**
  2314. * css_advance_task_iter - advance a task itererator to the next css_set
  2315. * @it: the iterator to advance
  2316. *
  2317. * Advance @it to the next css_set to walk.
  2318. */
  2319. static void css_advance_task_iter(struct css_task_iter *it)
  2320. {
  2321. struct list_head *l = it->cset_link;
  2322. struct cgrp_cset_link *link;
  2323. struct css_set *cset;
  2324. /* Advance to the next non-empty css_set */
  2325. do {
  2326. l = l->next;
  2327. if (l == &it->origin_css->cgroup->cset_links) {
  2328. it->cset_link = NULL;
  2329. return;
  2330. }
  2331. link = list_entry(l, struct cgrp_cset_link, cset_link);
  2332. cset = link->cset;
  2333. } while (list_empty(&cset->tasks) && list_empty(&cset->mg_tasks));
  2334. it->cset_link = l;
  2335. if (!list_empty(&cset->tasks))
  2336. it->task = cset->tasks.next;
  2337. else
  2338. it->task = cset->mg_tasks.next;
  2339. }
  2340. /**
  2341. * css_task_iter_start - initiate task iteration
  2342. * @css: the css to walk tasks of
  2343. * @it: the task iterator to use
  2344. *
  2345. * Initiate iteration through the tasks of @css. The caller can call
  2346. * css_task_iter_next() to walk through the tasks until the function
  2347. * returns NULL. On completion of iteration, css_task_iter_end() must be
  2348. * called.
  2349. *
  2350. * Note that this function acquires a lock which is released when the
  2351. * iteration finishes. The caller can't sleep while iteration is in
  2352. * progress.
  2353. */
  2354. void css_task_iter_start(struct cgroup_subsys_state *css,
  2355. struct css_task_iter *it)
  2356. __acquires(css_set_rwsem)
  2357. {
  2358. /* no one should try to iterate before mounting cgroups */
  2359. WARN_ON_ONCE(!use_task_css_set_links);
  2360. down_read(&css_set_rwsem);
  2361. it->origin_css = css;
  2362. it->cset_link = &css->cgroup->cset_links;
  2363. css_advance_task_iter(it);
  2364. }
  2365. /**
  2366. * css_task_iter_next - return the next task for the iterator
  2367. * @it: the task iterator being iterated
  2368. *
  2369. * The "next" function for task iteration. @it should have been
  2370. * initialized via css_task_iter_start(). Returns NULL when the iteration
  2371. * reaches the end.
  2372. */
  2373. struct task_struct *css_task_iter_next(struct css_task_iter *it)
  2374. {
  2375. struct task_struct *res;
  2376. struct list_head *l = it->task;
  2377. struct cgrp_cset_link *link = list_entry(it->cset_link,
  2378. struct cgrp_cset_link, cset_link);
  2379. /* If the iterator cg is NULL, we have no tasks */
  2380. if (!it->cset_link)
  2381. return NULL;
  2382. res = list_entry(l, struct task_struct, cg_list);
  2383. /*
  2384. * Advance iterator to find next entry. cset->tasks is consumed
  2385. * first and then ->mg_tasks. After ->mg_tasks, we move onto the
  2386. * next cset.
  2387. */
  2388. l = l->next;
  2389. if (l == &link->cset->tasks)
  2390. l = link->cset->mg_tasks.next;
  2391. if (l == &link->cset->mg_tasks)
  2392. css_advance_task_iter(it);
  2393. else
  2394. it->task = l;
  2395. return res;
  2396. }
  2397. /**
  2398. * css_task_iter_end - finish task iteration
  2399. * @it: the task iterator to finish
  2400. *
  2401. * Finish task iteration started by css_task_iter_start().
  2402. */
  2403. void css_task_iter_end(struct css_task_iter *it)
  2404. __releases(css_set_rwsem)
  2405. {
  2406. up_read(&css_set_rwsem);
  2407. }
  2408. /**
  2409. * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
  2410. * @to: cgroup to which the tasks will be moved
  2411. * @from: cgroup in which the tasks currently reside
  2412. *
  2413. * Locking rules between cgroup_post_fork() and the migration path
  2414. * guarantee that, if a task is forking while being migrated, the new child
  2415. * is guaranteed to be either visible in the source cgroup after the
  2416. * parent's migration is complete or put into the target cgroup. No task
  2417. * can slip out of migration through forking.
  2418. */
  2419. int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
  2420. {
  2421. LIST_HEAD(preloaded_csets);
  2422. struct cgrp_cset_link *link;
  2423. struct css_task_iter it;
  2424. struct task_struct *task;
  2425. int ret;
  2426. mutex_lock(&cgroup_mutex);
  2427. /* all tasks in @from are being moved, all csets are source */
  2428. down_read(&css_set_rwsem);
  2429. list_for_each_entry(link, &from->cset_links, cset_link)
  2430. cgroup_migrate_add_src(link->cset, to, &preloaded_csets);
  2431. up_read(&css_set_rwsem);
  2432. ret = cgroup_migrate_prepare_dst(to, &preloaded_csets);
  2433. if (ret)
  2434. goto out_err;
  2435. /*
  2436. * Migrate tasks one-by-one until @form is empty. This fails iff
  2437. * ->can_attach() fails.
  2438. */
  2439. do {
  2440. css_task_iter_start(&from->dummy_css, &it);
  2441. task = css_task_iter_next(&it);
  2442. if (task)
  2443. get_task_struct(task);
  2444. css_task_iter_end(&it);
  2445. if (task) {
  2446. ret = cgroup_migrate(to, task, false);
  2447. put_task_struct(task);
  2448. }
  2449. } while (task && !ret);
  2450. out_err:
  2451. cgroup_migrate_finish(&preloaded_csets);
  2452. mutex_unlock(&cgroup_mutex);
  2453. return ret;
  2454. }
  2455. /*
  2456. * Stuff for reading the 'tasks'/'procs' files.
  2457. *
  2458. * Reading this file can return large amounts of data if a cgroup has
  2459. * *lots* of attached tasks. So it may need several calls to read(),
  2460. * but we cannot guarantee that the information we produce is correct
  2461. * unless we produce it entirely atomically.
  2462. *
  2463. */
  2464. /* which pidlist file are we talking about? */
  2465. enum cgroup_filetype {
  2466. CGROUP_FILE_PROCS,
  2467. CGROUP_FILE_TASKS,
  2468. };
  2469. /*
  2470. * A pidlist is a list of pids that virtually represents the contents of one
  2471. * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
  2472. * a pair (one each for procs, tasks) for each pid namespace that's relevant
  2473. * to the cgroup.
  2474. */
  2475. struct cgroup_pidlist {
  2476. /*
  2477. * used to find which pidlist is wanted. doesn't change as long as
  2478. * this particular list stays in the list.
  2479. */
  2480. struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
  2481. /* array of xids */
  2482. pid_t *list;
  2483. /* how many elements the above list has */
  2484. int length;
  2485. /* each of these stored in a list by its cgroup */
  2486. struct list_head links;
  2487. /* pointer to the cgroup we belong to, for list removal purposes */
  2488. struct cgroup *owner;
  2489. /* for delayed destruction */
  2490. struct delayed_work destroy_dwork;
  2491. };
  2492. /*
  2493. * The following two functions "fix" the issue where there are more pids
  2494. * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
  2495. * TODO: replace with a kernel-wide solution to this problem
  2496. */
  2497. #define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
  2498. static void *pidlist_allocate(int count)
  2499. {
  2500. if (PIDLIST_TOO_LARGE(count))
  2501. return vmalloc(count * sizeof(pid_t));
  2502. else
  2503. return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
  2504. }
  2505. static void pidlist_free(void *p)
  2506. {
  2507. if (is_vmalloc_addr(p))
  2508. vfree(p);
  2509. else
  2510. kfree(p);
  2511. }
  2512. /*
  2513. * Used to destroy all pidlists lingering waiting for destroy timer. None
  2514. * should be left afterwards.
  2515. */
  2516. static void cgroup_pidlist_destroy_all(struct cgroup *cgrp)
  2517. {
  2518. struct cgroup_pidlist *l, *tmp_l;
  2519. mutex_lock(&cgrp->pidlist_mutex);
  2520. list_for_each_entry_safe(l, tmp_l, &cgrp->pidlists, links)
  2521. mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, 0);
  2522. mutex_unlock(&cgrp->pidlist_mutex);
  2523. flush_workqueue(cgroup_pidlist_destroy_wq);
  2524. BUG_ON(!list_empty(&cgrp->pidlists));
  2525. }
  2526. static void cgroup_pidlist_destroy_work_fn(struct work_struct *work)
  2527. {
  2528. struct delayed_work *dwork = to_delayed_work(work);
  2529. struct cgroup_pidlist *l = container_of(dwork, struct cgroup_pidlist,
  2530. destroy_dwork);
  2531. struct cgroup_pidlist *tofree = NULL;
  2532. mutex_lock(&l->owner->pidlist_mutex);
  2533. /*
  2534. * Destroy iff we didn't get queued again. The state won't change
  2535. * as destroy_dwork can only be queued while locked.
  2536. */
  2537. if (!delayed_work_pending(dwork)) {
  2538. list_del(&l->links);
  2539. pidlist_free(l->list);
  2540. put_pid_ns(l->key.ns);
  2541. tofree = l;
  2542. }
  2543. mutex_unlock(&l->owner->pidlist_mutex);
  2544. kfree(tofree);
  2545. }
  2546. /*
  2547. * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
  2548. * Returns the number of unique elements.
  2549. */
  2550. static int pidlist_uniq(pid_t *list, int length)
  2551. {
  2552. int src, dest = 1;
  2553. /*
  2554. * we presume the 0th element is unique, so i starts at 1. trivial
  2555. * edge cases first; no work needs to be done for either
  2556. */
  2557. if (length == 0 || length == 1)
  2558. return length;
  2559. /* src and dest walk down the list; dest counts unique elements */
  2560. for (src = 1; src < length; src++) {
  2561. /* find next unique element */
  2562. while (list[src] == list[src-1]) {
  2563. src++;
  2564. if (src == length)
  2565. goto after;
  2566. }
  2567. /* dest always points to where the next unique element goes */
  2568. list[dest] = list[src];
  2569. dest++;
  2570. }
  2571. after:
  2572. return dest;
  2573. }
  2574. /*
  2575. * The two pid files - task and cgroup.procs - guaranteed that the result
  2576. * is sorted, which forced this whole pidlist fiasco. As pid order is
  2577. * different per namespace, each namespace needs differently sorted list,
  2578. * making it impossible to use, for example, single rbtree of member tasks
  2579. * sorted by task pointer. As pidlists can be fairly large, allocating one
  2580. * per open file is dangerous, so cgroup had to implement shared pool of
  2581. * pidlists keyed by cgroup and namespace.
  2582. *
  2583. * All this extra complexity was caused by the original implementation
  2584. * committing to an entirely unnecessary property. In the long term, we
  2585. * want to do away with it. Explicitly scramble sort order if
  2586. * sane_behavior so that no such expectation exists in the new interface.
  2587. *
  2588. * Scrambling is done by swapping every two consecutive bits, which is
  2589. * non-identity one-to-one mapping which disturbs sort order sufficiently.
  2590. */
  2591. static pid_t pid_fry(pid_t pid)
  2592. {
  2593. unsigned a = pid & 0x55555555;
  2594. unsigned b = pid & 0xAAAAAAAA;
  2595. return (a << 1) | (b >> 1);
  2596. }
  2597. static pid_t cgroup_pid_fry(struct cgroup *cgrp, pid_t pid)
  2598. {
  2599. if (cgroup_sane_behavior(cgrp))
  2600. return pid_fry(pid);
  2601. else
  2602. return pid;
  2603. }
  2604. static int cmppid(const void *a, const void *b)
  2605. {
  2606. return *(pid_t *)a - *(pid_t *)b;
  2607. }
  2608. static int fried_cmppid(const void *a, const void *b)
  2609. {
  2610. return pid_fry(*(pid_t *)a) - pid_fry(*(pid_t *)b);
  2611. }
  2612. static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
  2613. enum cgroup_filetype type)
  2614. {
  2615. struct cgroup_pidlist *l;
  2616. /* don't need task_nsproxy() if we're looking at ourself */
  2617. struct pid_namespace *ns = task_active_pid_ns(current);
  2618. lockdep_assert_held(&cgrp->pidlist_mutex);
  2619. list_for_each_entry(l, &cgrp->pidlists, links)
  2620. if (l->key.type == type && l->key.ns == ns)
  2621. return l;
  2622. return NULL;
  2623. }
  2624. /*
  2625. * find the appropriate pidlist for our purpose (given procs vs tasks)
  2626. * returns with the lock on that pidlist already held, and takes care
  2627. * of the use count, or returns NULL with no locks held if we're out of
  2628. * memory.
  2629. */
  2630. static struct cgroup_pidlist *cgroup_pidlist_find_create(struct cgroup *cgrp,
  2631. enum cgroup_filetype type)
  2632. {
  2633. struct cgroup_pidlist *l;
  2634. lockdep_assert_held(&cgrp->pidlist_mutex);
  2635. l = cgroup_pidlist_find(cgrp, type);
  2636. if (l)
  2637. return l;
  2638. /* entry not found; create a new one */
  2639. l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
  2640. if (!l)
  2641. return l;
  2642. INIT_DELAYED_WORK(&l->destroy_dwork, cgroup_pidlist_destroy_work_fn);
  2643. l->key.type = type;
  2644. /* don't need task_nsproxy() if we're looking at ourself */
  2645. l->key.ns = get_pid_ns(task_active_pid_ns(current));
  2646. l->owner = cgrp;
  2647. list_add(&l->links, &cgrp->pidlists);
  2648. return l;
  2649. }
  2650. /*
  2651. * Load a cgroup's pidarray with either procs' tgids or tasks' pids
  2652. */
  2653. static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
  2654. struct cgroup_pidlist **lp)
  2655. {
  2656. pid_t *array;
  2657. int length;
  2658. int pid, n = 0; /* used for populating the array */
  2659. struct css_task_iter it;
  2660. struct task_struct *tsk;
  2661. struct cgroup_pidlist *l;
  2662. lockdep_assert_held(&cgrp->pidlist_mutex);
  2663. /*
  2664. * If cgroup gets more users after we read count, we won't have
  2665. * enough space - tough. This race is indistinguishable to the
  2666. * caller from the case that the additional cgroup users didn't
  2667. * show up until sometime later on.
  2668. */
  2669. length = cgroup_task_count(cgrp);
  2670. array = pidlist_allocate(length);
  2671. if (!array)
  2672. return -ENOMEM;
  2673. /* now, populate the array */
  2674. css_task_iter_start(&cgrp->dummy_css, &it);
  2675. while ((tsk = css_task_iter_next(&it))) {
  2676. if (unlikely(n == length))
  2677. break;
  2678. /* get tgid or pid for procs or tasks file respectively */
  2679. if (type == CGROUP_FILE_PROCS)
  2680. pid = task_tgid_vnr(tsk);
  2681. else
  2682. pid = task_pid_vnr(tsk);
  2683. if (pid > 0) /* make sure to only use valid results */
  2684. array[n++] = pid;
  2685. }
  2686. css_task_iter_end(&it);
  2687. length = n;
  2688. /* now sort & (if procs) strip out duplicates */
  2689. if (cgroup_sane_behavior(cgrp))
  2690. sort(array, length, sizeof(pid_t), fried_cmppid, NULL);
  2691. else
  2692. sort(array, length, sizeof(pid_t), cmppid, NULL);
  2693. if (type == CGROUP_FILE_PROCS)
  2694. length = pidlist_uniq(array, length);
  2695. l = cgroup_pidlist_find_create(cgrp, type);
  2696. if (!l) {
  2697. mutex_unlock(&cgrp->pidlist_mutex);
  2698. pidlist_free(array);
  2699. return -ENOMEM;
  2700. }
  2701. /* store array, freeing old if necessary */
  2702. pidlist_free(l->list);
  2703. l->list = array;
  2704. l->length = length;
  2705. *lp = l;
  2706. return 0;
  2707. }
  2708. /**
  2709. * cgroupstats_build - build and fill cgroupstats
  2710. * @stats: cgroupstats to fill information into
  2711. * @dentry: A dentry entry belonging to the cgroup for which stats have
  2712. * been requested.
  2713. *
  2714. * Build and fill cgroupstats so that taskstats can export it to user
  2715. * space.
  2716. */
  2717. int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
  2718. {
  2719. struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
  2720. struct cgroup *cgrp;
  2721. struct css_task_iter it;
  2722. struct task_struct *tsk;
  2723. /* it should be kernfs_node belonging to cgroupfs and is a directory */
  2724. if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
  2725. kernfs_type(kn) != KERNFS_DIR)
  2726. return -EINVAL;
  2727. mutex_lock(&cgroup_mutex);
  2728. /*
  2729. * We aren't being called from kernfs and there's no guarantee on
  2730. * @kn->priv's validity. For this and css_tryget_from_dir(),
  2731. * @kn->priv is RCU safe. Let's do the RCU dancing.
  2732. */
  2733. rcu_read_lock();
  2734. cgrp = rcu_dereference(kn->priv);
  2735. if (!cgrp || cgroup_is_dead(cgrp)) {
  2736. rcu_read_unlock();
  2737. mutex_unlock(&cgroup_mutex);
  2738. return -ENOENT;
  2739. }
  2740. rcu_read_unlock();
  2741. css_task_iter_start(&cgrp->dummy_css, &it);
  2742. while ((tsk = css_task_iter_next(&it))) {
  2743. switch (tsk->state) {
  2744. case TASK_RUNNING:
  2745. stats->nr_running++;
  2746. break;
  2747. case TASK_INTERRUPTIBLE:
  2748. stats->nr_sleeping++;
  2749. break;
  2750. case TASK_UNINTERRUPTIBLE:
  2751. stats->nr_uninterruptible++;
  2752. break;
  2753. case TASK_STOPPED:
  2754. stats->nr_stopped++;
  2755. break;
  2756. default:
  2757. if (delayacct_is_task_waiting_on_io(tsk))
  2758. stats->nr_io_wait++;
  2759. break;
  2760. }
  2761. }
  2762. css_task_iter_end(&it);
  2763. mutex_unlock(&cgroup_mutex);
  2764. return 0;
  2765. }
  2766. /*
  2767. * seq_file methods for the tasks/procs files. The seq_file position is the
  2768. * next pid to display; the seq_file iterator is a pointer to the pid
  2769. * in the cgroup->l->list array.
  2770. */
  2771. static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
  2772. {
  2773. /*
  2774. * Initially we receive a position value that corresponds to
  2775. * one more than the last pid shown (or 0 on the first call or
  2776. * after a seek to the start). Use a binary-search to find the
  2777. * next pid to display, if any
  2778. */
  2779. struct kernfs_open_file *of = s->private;
  2780. struct cgroup *cgrp = seq_css(s)->cgroup;
  2781. struct cgroup_pidlist *l;
  2782. enum cgroup_filetype type = seq_cft(s)->private;
  2783. int index = 0, pid = *pos;
  2784. int *iter, ret;
  2785. mutex_lock(&cgrp->pidlist_mutex);
  2786. /*
  2787. * !NULL @of->priv indicates that this isn't the first start()
  2788. * after open. If the matching pidlist is around, we can use that.
  2789. * Look for it. Note that @of->priv can't be used directly. It
  2790. * could already have been destroyed.
  2791. */
  2792. if (of->priv)
  2793. of->priv = cgroup_pidlist_find(cgrp, type);
  2794. /*
  2795. * Either this is the first start() after open or the matching
  2796. * pidlist has been destroyed inbetween. Create a new one.
  2797. */
  2798. if (!of->priv) {
  2799. ret = pidlist_array_load(cgrp, type,
  2800. (struct cgroup_pidlist **)&of->priv);
  2801. if (ret)
  2802. return ERR_PTR(ret);
  2803. }
  2804. l = of->priv;
  2805. if (pid) {
  2806. int end = l->length;
  2807. while (index < end) {
  2808. int mid = (index + end) / 2;
  2809. if (cgroup_pid_fry(cgrp, l->list[mid]) == pid) {
  2810. index = mid;
  2811. break;
  2812. } else if (cgroup_pid_fry(cgrp, l->list[mid]) <= pid)
  2813. index = mid + 1;
  2814. else
  2815. end = mid;
  2816. }
  2817. }
  2818. /* If we're off the end of the array, we're done */
  2819. if (index >= l->length)
  2820. return NULL;
  2821. /* Update the abstract position to be the actual pid that we found */
  2822. iter = l->list + index;
  2823. *pos = cgroup_pid_fry(cgrp, *iter);
  2824. return iter;
  2825. }
  2826. static void cgroup_pidlist_stop(struct seq_file *s, void *v)
  2827. {
  2828. struct kernfs_open_file *of = s->private;
  2829. struct cgroup_pidlist *l = of->priv;
  2830. if (l)
  2831. mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork,
  2832. CGROUP_PIDLIST_DESTROY_DELAY);
  2833. mutex_unlock(&seq_css(s)->cgroup->pidlist_mutex);
  2834. }
  2835. static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
  2836. {
  2837. struct kernfs_open_file *of = s->private;
  2838. struct cgroup_pidlist *l = of->priv;
  2839. pid_t *p = v;
  2840. pid_t *end = l->list + l->length;
  2841. /*
  2842. * Advance to the next pid in the array. If this goes off the
  2843. * end, we're done
  2844. */
  2845. p++;
  2846. if (p >= end) {
  2847. return NULL;
  2848. } else {
  2849. *pos = cgroup_pid_fry(seq_css(s)->cgroup, *p);
  2850. return p;
  2851. }
  2852. }
  2853. static int cgroup_pidlist_show(struct seq_file *s, void *v)
  2854. {
  2855. return seq_printf(s, "%d\n", *(int *)v);
  2856. }
  2857. /*
  2858. * seq_operations functions for iterating on pidlists through seq_file -
  2859. * independent of whether it's tasks or procs
  2860. */
  2861. static const struct seq_operations cgroup_pidlist_seq_operations = {
  2862. .start = cgroup_pidlist_start,
  2863. .stop = cgroup_pidlist_stop,
  2864. .next = cgroup_pidlist_next,
  2865. .show = cgroup_pidlist_show,
  2866. };
  2867. static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
  2868. struct cftype *cft)
  2869. {
  2870. return notify_on_release(css->cgroup);
  2871. }
  2872. static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
  2873. struct cftype *cft, u64 val)
  2874. {
  2875. clear_bit(CGRP_RELEASABLE, &css->cgroup->flags);
  2876. if (val)
  2877. set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
  2878. else
  2879. clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
  2880. return 0;
  2881. }
  2882. static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
  2883. struct cftype *cft)
  2884. {
  2885. return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
  2886. }
  2887. static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
  2888. struct cftype *cft, u64 val)
  2889. {
  2890. if (val)
  2891. set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
  2892. else
  2893. clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
  2894. return 0;
  2895. }
  2896. static struct cftype cgroup_base_files[] = {
  2897. {
  2898. .name = "cgroup.procs",
  2899. .seq_start = cgroup_pidlist_start,
  2900. .seq_next = cgroup_pidlist_next,
  2901. .seq_stop = cgroup_pidlist_stop,
  2902. .seq_show = cgroup_pidlist_show,
  2903. .private = CGROUP_FILE_PROCS,
  2904. .write_u64 = cgroup_procs_write,
  2905. .mode = S_IRUGO | S_IWUSR,
  2906. },
  2907. {
  2908. .name = "cgroup.clone_children",
  2909. .flags = CFTYPE_INSANE,
  2910. .read_u64 = cgroup_clone_children_read,
  2911. .write_u64 = cgroup_clone_children_write,
  2912. },
  2913. {
  2914. .name = "cgroup.sane_behavior",
  2915. .flags = CFTYPE_ONLY_ON_ROOT,
  2916. .seq_show = cgroup_sane_behavior_show,
  2917. },
  2918. /*
  2919. * Historical crazy stuff. These don't have "cgroup." prefix and
  2920. * don't exist if sane_behavior. If you're depending on these, be
  2921. * prepared to be burned.
  2922. */
  2923. {
  2924. .name = "tasks",
  2925. .flags = CFTYPE_INSANE, /* use "procs" instead */
  2926. .seq_start = cgroup_pidlist_start,
  2927. .seq_next = cgroup_pidlist_next,
  2928. .seq_stop = cgroup_pidlist_stop,
  2929. .seq_show = cgroup_pidlist_show,
  2930. .private = CGROUP_FILE_TASKS,
  2931. .write_u64 = cgroup_tasks_write,
  2932. .mode = S_IRUGO | S_IWUSR,
  2933. },
  2934. {
  2935. .name = "notify_on_release",
  2936. .flags = CFTYPE_INSANE,
  2937. .read_u64 = cgroup_read_notify_on_release,
  2938. .write_u64 = cgroup_write_notify_on_release,
  2939. },
  2940. {
  2941. .name = "release_agent",
  2942. .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT,
  2943. .seq_show = cgroup_release_agent_show,
  2944. .write_string = cgroup_release_agent_write,
  2945. .max_write_len = PATH_MAX - 1,
  2946. },
  2947. { } /* terminate */
  2948. };
  2949. /**
  2950. * cgroup_populate_dir - create subsys files in a cgroup directory
  2951. * @cgrp: target cgroup
  2952. * @subsys_mask: mask of the subsystem ids whose files should be added
  2953. *
  2954. * On failure, no file is added.
  2955. */
  2956. static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask)
  2957. {
  2958. struct cgroup_subsys *ss;
  2959. int i, ret = 0;
  2960. /* process cftsets of each subsystem */
  2961. for_each_subsys(ss, i) {
  2962. struct cftype *cfts;
  2963. if (!test_bit(i, &subsys_mask))
  2964. continue;
  2965. list_for_each_entry(cfts, &ss->cfts, node) {
  2966. ret = cgroup_addrm_files(cgrp, cfts, true);
  2967. if (ret < 0)
  2968. goto err;
  2969. }
  2970. }
  2971. return 0;
  2972. err:
  2973. cgroup_clear_dir(cgrp, subsys_mask);
  2974. return ret;
  2975. }
  2976. /*
  2977. * css destruction is four-stage process.
  2978. *
  2979. * 1. Destruction starts. Killing of the percpu_ref is initiated.
  2980. * Implemented in kill_css().
  2981. *
  2982. * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
  2983. * and thus css_tryget() is guaranteed to fail, the css can be offlined
  2984. * by invoking offline_css(). After offlining, the base ref is put.
  2985. * Implemented in css_killed_work_fn().
  2986. *
  2987. * 3. When the percpu_ref reaches zero, the only possible remaining
  2988. * accessors are inside RCU read sections. css_release() schedules the
  2989. * RCU callback.
  2990. *
  2991. * 4. After the grace period, the css can be freed. Implemented in
  2992. * css_free_work_fn().
  2993. *
  2994. * It is actually hairier because both step 2 and 4 require process context
  2995. * and thus involve punting to css->destroy_work adding two additional
  2996. * steps to the already complex sequence.
  2997. */
  2998. static void css_free_work_fn(struct work_struct *work)
  2999. {
  3000. struct cgroup_subsys_state *css =
  3001. container_of(work, struct cgroup_subsys_state, destroy_work);
  3002. struct cgroup *cgrp = css->cgroup;
  3003. if (css->parent)
  3004. css_put(css->parent);
  3005. css->ss->css_free(css);
  3006. cgroup_put(cgrp);
  3007. }
  3008. static void css_free_rcu_fn(struct rcu_head *rcu_head)
  3009. {
  3010. struct cgroup_subsys_state *css =
  3011. container_of(rcu_head, struct cgroup_subsys_state, rcu_head);
  3012. INIT_WORK(&css->destroy_work, css_free_work_fn);
  3013. queue_work(cgroup_destroy_wq, &css->destroy_work);
  3014. }
  3015. static void css_release(struct percpu_ref *ref)
  3016. {
  3017. struct cgroup_subsys_state *css =
  3018. container_of(ref, struct cgroup_subsys_state, refcnt);
  3019. rcu_assign_pointer(css->cgroup->subsys[css->ss->id], NULL);
  3020. call_rcu(&css->rcu_head, css_free_rcu_fn);
  3021. }
  3022. static void init_css(struct cgroup_subsys_state *css, struct cgroup_subsys *ss,
  3023. struct cgroup *cgrp)
  3024. {
  3025. css->cgroup = cgrp;
  3026. css->ss = ss;
  3027. css->flags = 0;
  3028. if (cgrp->parent)
  3029. css->parent = cgroup_css(cgrp->parent, ss);
  3030. else
  3031. css->flags |= CSS_ROOT;
  3032. BUG_ON(cgroup_css(cgrp, ss));
  3033. }
  3034. /* invoke ->css_online() on a new CSS and mark it online if successful */
  3035. static int online_css(struct cgroup_subsys_state *css)
  3036. {
  3037. struct cgroup_subsys *ss = css->ss;
  3038. int ret = 0;
  3039. lockdep_assert_held(&cgroup_tree_mutex);
  3040. lockdep_assert_held(&cgroup_mutex);
  3041. if (ss->css_online)
  3042. ret = ss->css_online(css);
  3043. if (!ret) {
  3044. css->flags |= CSS_ONLINE;
  3045. css->cgroup->nr_css++;
  3046. rcu_assign_pointer(css->cgroup->subsys[ss->id], css);
  3047. }
  3048. return ret;
  3049. }
  3050. /* if the CSS is online, invoke ->css_offline() on it and mark it offline */
  3051. static void offline_css(struct cgroup_subsys_state *css)
  3052. {
  3053. struct cgroup_subsys *ss = css->ss;
  3054. lockdep_assert_held(&cgroup_tree_mutex);
  3055. lockdep_assert_held(&cgroup_mutex);
  3056. if (!(css->flags & CSS_ONLINE))
  3057. return;
  3058. if (ss->css_offline)
  3059. ss->css_offline(css);
  3060. css->flags &= ~CSS_ONLINE;
  3061. css->cgroup->nr_css--;
  3062. RCU_INIT_POINTER(css->cgroup->subsys[ss->id], css);
  3063. }
  3064. /**
  3065. * create_css - create a cgroup_subsys_state
  3066. * @cgrp: the cgroup new css will be associated with
  3067. * @ss: the subsys of new css
  3068. *
  3069. * Create a new css associated with @cgrp - @ss pair. On success, the new
  3070. * css is online and installed in @cgrp with all interface files created.
  3071. * Returns 0 on success, -errno on failure.
  3072. */
  3073. static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss)
  3074. {
  3075. struct cgroup *parent = cgrp->parent;
  3076. struct cgroup_subsys_state *css;
  3077. int err;
  3078. lockdep_assert_held(&cgroup_mutex);
  3079. css = ss->css_alloc(cgroup_css(parent, ss));
  3080. if (IS_ERR(css))
  3081. return PTR_ERR(css);
  3082. err = percpu_ref_init(&css->refcnt, css_release);
  3083. if (err)
  3084. goto err_free;
  3085. init_css(css, ss, cgrp);
  3086. err = cgroup_populate_dir(cgrp, 1 << ss->id);
  3087. if (err)
  3088. goto err_free;
  3089. err = online_css(css);
  3090. if (err)
  3091. goto err_free;
  3092. cgroup_get(cgrp);
  3093. css_get(css->parent);
  3094. if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
  3095. parent->parent) {
  3096. 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",
  3097. current->comm, current->pid, ss->name);
  3098. if (!strcmp(ss->name, "memory"))
  3099. pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
  3100. ss->warned_broken_hierarchy = true;
  3101. }
  3102. return 0;
  3103. err_free:
  3104. percpu_ref_cancel_init(&css->refcnt);
  3105. ss->css_free(css);
  3106. return err;
  3107. }
  3108. /**
  3109. * cgroup_create - create a cgroup
  3110. * @parent: cgroup that will be parent of the new cgroup
  3111. * @name: name of the new cgroup
  3112. * @mode: mode to set on new cgroup
  3113. */
  3114. static long cgroup_create(struct cgroup *parent, const char *name,
  3115. umode_t mode)
  3116. {
  3117. struct cgroup *cgrp;
  3118. struct cgroupfs_root *root = parent->root;
  3119. int ssid, err;
  3120. struct cgroup_subsys *ss;
  3121. struct kernfs_node *kn;
  3122. /* allocate the cgroup and its ID, 0 is reserved for the root */
  3123. cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
  3124. if (!cgrp)
  3125. return -ENOMEM;
  3126. mutex_lock(&cgroup_tree_mutex);
  3127. /*
  3128. * Only live parents can have children. Note that the liveliness
  3129. * check isn't strictly necessary because cgroup_mkdir() and
  3130. * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
  3131. * anyway so that locking is contained inside cgroup proper and we
  3132. * don't get nasty surprises if we ever grow another caller.
  3133. */
  3134. if (!cgroup_lock_live_group(parent)) {
  3135. err = -ENODEV;
  3136. goto err_unlock_tree;
  3137. }
  3138. /*
  3139. * Temporarily set the pointer to NULL, so idr_find() won't return
  3140. * a half-baked cgroup.
  3141. */
  3142. cgrp->id = idr_alloc(&root->cgroup_idr, NULL, 1, 0, GFP_KERNEL);
  3143. if (cgrp->id < 0) {
  3144. err = -ENOMEM;
  3145. goto err_unlock;
  3146. }
  3147. init_cgroup_housekeeping(cgrp);
  3148. cgrp->parent = parent;
  3149. cgrp->dummy_css.parent = &parent->dummy_css;
  3150. cgrp->root = parent->root;
  3151. if (notify_on_release(parent))
  3152. set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
  3153. if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
  3154. set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
  3155. /* create the directory */
  3156. kn = kernfs_create_dir(parent->kn, name, mode, cgrp);
  3157. if (IS_ERR(kn)) {
  3158. err = PTR_ERR(kn);
  3159. goto err_free_id;
  3160. }
  3161. cgrp->kn = kn;
  3162. /*
  3163. * This extra ref will be put in cgroup_free_fn() and guarantees
  3164. * that @cgrp->kn is always accessible.
  3165. */
  3166. kernfs_get(kn);
  3167. cgrp->serial_nr = cgroup_serial_nr_next++;
  3168. /* allocation complete, commit to creation */
  3169. list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
  3170. atomic_inc(&root->nr_cgrps);
  3171. cgroup_get(parent);
  3172. /*
  3173. * @cgrp is now fully operational. If something fails after this
  3174. * point, it'll be released via the normal destruction path.
  3175. */
  3176. idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
  3177. err = cgroup_addrm_files(cgrp, cgroup_base_files, true);
  3178. if (err)
  3179. goto err_destroy;
  3180. /* let's create and online css's */
  3181. for_each_subsys(ss, ssid) {
  3182. if (root->subsys_mask & (1 << ssid)) {
  3183. err = create_css(cgrp, ss);
  3184. if (err)
  3185. goto err_destroy;
  3186. }
  3187. }
  3188. kernfs_activate(kn);
  3189. mutex_unlock(&cgroup_mutex);
  3190. mutex_unlock(&cgroup_tree_mutex);
  3191. return 0;
  3192. err_free_id:
  3193. idr_remove(&root->cgroup_idr, cgrp->id);
  3194. err_unlock:
  3195. mutex_unlock(&cgroup_mutex);
  3196. err_unlock_tree:
  3197. mutex_unlock(&cgroup_tree_mutex);
  3198. kfree(cgrp);
  3199. return err;
  3200. err_destroy:
  3201. cgroup_destroy_locked(cgrp);
  3202. mutex_unlock(&cgroup_mutex);
  3203. mutex_unlock(&cgroup_tree_mutex);
  3204. return err;
  3205. }
  3206. static int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
  3207. umode_t mode)
  3208. {
  3209. struct cgroup *parent = parent_kn->priv;
  3210. return cgroup_create(parent, name, mode);
  3211. }
  3212. /*
  3213. * This is called when the refcnt of a css is confirmed to be killed.
  3214. * css_tryget() is now guaranteed to fail.
  3215. */
  3216. static void css_killed_work_fn(struct work_struct *work)
  3217. {
  3218. struct cgroup_subsys_state *css =
  3219. container_of(work, struct cgroup_subsys_state, destroy_work);
  3220. struct cgroup *cgrp = css->cgroup;
  3221. mutex_lock(&cgroup_tree_mutex);
  3222. mutex_lock(&cgroup_mutex);
  3223. /*
  3224. * css_tryget() is guaranteed to fail now. Tell subsystems to
  3225. * initate destruction.
  3226. */
  3227. offline_css(css);
  3228. /*
  3229. * If @cgrp is marked dead, it's waiting for refs of all css's to
  3230. * be disabled before proceeding to the second phase of cgroup
  3231. * destruction. If we are the last one, kick it off.
  3232. */
  3233. if (!cgrp->nr_css && cgroup_is_dead(cgrp))
  3234. cgroup_destroy_css_killed(cgrp);
  3235. mutex_unlock(&cgroup_mutex);
  3236. mutex_unlock(&cgroup_tree_mutex);
  3237. /*
  3238. * Put the css refs from kill_css(). Each css holds an extra
  3239. * reference to the cgroup's dentry and cgroup removal proceeds
  3240. * regardless of css refs. On the last put of each css, whenever
  3241. * that may be, the extra dentry ref is put so that dentry
  3242. * destruction happens only after all css's are released.
  3243. */
  3244. css_put(css);
  3245. }
  3246. /* css kill confirmation processing requires process context, bounce */
  3247. static void css_killed_ref_fn(struct percpu_ref *ref)
  3248. {
  3249. struct cgroup_subsys_state *css =
  3250. container_of(ref, struct cgroup_subsys_state, refcnt);
  3251. INIT_WORK(&css->destroy_work, css_killed_work_fn);
  3252. queue_work(cgroup_destroy_wq, &css->destroy_work);
  3253. }
  3254. /**
  3255. * kill_css - destroy a css
  3256. * @css: css to destroy
  3257. *
  3258. * This function initiates destruction of @css by removing cgroup interface
  3259. * files and putting its base reference. ->css_offline() will be invoked
  3260. * asynchronously once css_tryget() is guaranteed to fail and when the
  3261. * reference count reaches zero, @css will be released.
  3262. */
  3263. static void kill_css(struct cgroup_subsys_state *css)
  3264. {
  3265. /*
  3266. * This must happen before css is disassociated with its cgroup.
  3267. * See seq_css() for details.
  3268. */
  3269. cgroup_clear_dir(css->cgroup, 1 << css->ss->id);
  3270. /*
  3271. * Killing would put the base ref, but we need to keep it alive
  3272. * until after ->css_offline().
  3273. */
  3274. css_get(css);
  3275. /*
  3276. * cgroup core guarantees that, by the time ->css_offline() is
  3277. * invoked, no new css reference will be given out via
  3278. * css_tryget(). We can't simply call percpu_ref_kill() and
  3279. * proceed to offlining css's because percpu_ref_kill() doesn't
  3280. * guarantee that the ref is seen as killed on all CPUs on return.
  3281. *
  3282. * Use percpu_ref_kill_and_confirm() to get notifications as each
  3283. * css is confirmed to be seen as killed on all CPUs.
  3284. */
  3285. percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
  3286. }
  3287. /**
  3288. * cgroup_destroy_locked - the first stage of cgroup destruction
  3289. * @cgrp: cgroup to be destroyed
  3290. *
  3291. * css's make use of percpu refcnts whose killing latency shouldn't be
  3292. * exposed to userland and are RCU protected. Also, cgroup core needs to
  3293. * guarantee that css_tryget() won't succeed by the time ->css_offline() is
  3294. * invoked. To satisfy all the requirements, destruction is implemented in
  3295. * the following two steps.
  3296. *
  3297. * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
  3298. * userland visible parts and start killing the percpu refcnts of
  3299. * css's. Set up so that the next stage will be kicked off once all
  3300. * the percpu refcnts are confirmed to be killed.
  3301. *
  3302. * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
  3303. * rest of destruction. Once all cgroup references are gone, the
  3304. * cgroup is RCU-freed.
  3305. *
  3306. * This function implements s1. After this step, @cgrp is gone as far as
  3307. * the userland is concerned and a new cgroup with the same name may be
  3308. * created. As cgroup doesn't care about the names internally, this
  3309. * doesn't cause any problem.
  3310. */
  3311. static int cgroup_destroy_locked(struct cgroup *cgrp)
  3312. __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
  3313. {
  3314. struct cgroup *child;
  3315. struct cgroup_subsys_state *css;
  3316. bool empty;
  3317. int ssid;
  3318. lockdep_assert_held(&cgroup_tree_mutex);
  3319. lockdep_assert_held(&cgroup_mutex);
  3320. /*
  3321. * css_set_rwsem synchronizes access to ->cset_links and prevents
  3322. * @cgrp from being removed while put_css_set() is in progress.
  3323. */
  3324. down_read(&css_set_rwsem);
  3325. empty = list_empty(&cgrp->cset_links);
  3326. up_read(&css_set_rwsem);
  3327. if (!empty)
  3328. return -EBUSY;
  3329. /*
  3330. * Make sure there's no live children. We can't test ->children
  3331. * emptiness as dead children linger on it while being destroyed;
  3332. * otherwise, "rmdir parent/child parent" may fail with -EBUSY.
  3333. */
  3334. empty = true;
  3335. rcu_read_lock();
  3336. list_for_each_entry_rcu(child, &cgrp->children, sibling) {
  3337. empty = cgroup_is_dead(child);
  3338. if (!empty)
  3339. break;
  3340. }
  3341. rcu_read_unlock();
  3342. if (!empty)
  3343. return -EBUSY;
  3344. /*
  3345. * Mark @cgrp dead. This prevents further task migration and child
  3346. * creation by disabling cgroup_lock_live_group(). Note that
  3347. * CGRP_DEAD assertion is depended upon by css_next_child() to
  3348. * resume iteration after dropping RCU read lock. See
  3349. * css_next_child() for details.
  3350. */
  3351. set_bit(CGRP_DEAD, &cgrp->flags);
  3352. /*
  3353. * Initiate massacre of all css's. cgroup_destroy_css_killed()
  3354. * will be invoked to perform the rest of destruction once the
  3355. * percpu refs of all css's are confirmed to be killed. This
  3356. * involves removing the subsystem's files, drop cgroup_mutex.
  3357. */
  3358. mutex_unlock(&cgroup_mutex);
  3359. for_each_css(css, ssid, cgrp)
  3360. kill_css(css);
  3361. mutex_lock(&cgroup_mutex);
  3362. /* CGRP_DEAD is set, remove from ->release_list for the last time */
  3363. raw_spin_lock(&release_list_lock);
  3364. if (!list_empty(&cgrp->release_list))
  3365. list_del_init(&cgrp->release_list);
  3366. raw_spin_unlock(&release_list_lock);
  3367. /*
  3368. * If @cgrp has css's attached, the second stage of cgroup
  3369. * destruction is kicked off from css_killed_work_fn() after the
  3370. * refs of all attached css's are killed. If @cgrp doesn't have
  3371. * any css, we kick it off here.
  3372. */
  3373. if (!cgrp->nr_css)
  3374. cgroup_destroy_css_killed(cgrp);
  3375. /* remove @cgrp directory along with the base files */
  3376. mutex_unlock(&cgroup_mutex);
  3377. /*
  3378. * There are two control paths which try to determine cgroup from
  3379. * dentry without going through kernfs - cgroupstats_build() and
  3380. * css_tryget_from_dir(). Those are supported by RCU protecting
  3381. * clearing of cgrp->kn->priv backpointer, which should happen
  3382. * after all files under it have been removed.
  3383. */
  3384. kernfs_remove(cgrp->kn); /* @cgrp has an extra ref on its kn */
  3385. RCU_INIT_POINTER(*(void __rcu __force **)&cgrp->kn->priv, NULL);
  3386. mutex_lock(&cgroup_mutex);
  3387. return 0;
  3388. };
  3389. /**
  3390. * cgroup_destroy_css_killed - the second step of cgroup destruction
  3391. * @work: cgroup->destroy_free_work
  3392. *
  3393. * This function is invoked from a work item for a cgroup which is being
  3394. * destroyed after all css's are offlined and performs the rest of
  3395. * destruction. This is the second step of destruction described in the
  3396. * comment above cgroup_destroy_locked().
  3397. */
  3398. static void cgroup_destroy_css_killed(struct cgroup *cgrp)
  3399. {
  3400. struct cgroup *parent = cgrp->parent;
  3401. lockdep_assert_held(&cgroup_tree_mutex);
  3402. lockdep_assert_held(&cgroup_mutex);
  3403. /* delete this cgroup from parent->children */
  3404. list_del_rcu(&cgrp->sibling);
  3405. cgroup_put(cgrp);
  3406. set_bit(CGRP_RELEASABLE, &parent->flags);
  3407. check_for_release(parent);
  3408. }
  3409. static int cgroup_rmdir(struct kernfs_node *kn)
  3410. {
  3411. struct cgroup *cgrp = kn->priv;
  3412. int ret = 0;
  3413. /*
  3414. * This is self-destruction but @kn can't be removed while this
  3415. * callback is in progress. Let's break active protection. Once
  3416. * the protection is broken, @cgrp can be destroyed at any point.
  3417. * Pin it so that it stays accessible.
  3418. */
  3419. cgroup_get(cgrp);
  3420. kernfs_break_active_protection(kn);
  3421. mutex_lock(&cgroup_tree_mutex);
  3422. mutex_lock(&cgroup_mutex);
  3423. /*
  3424. * @cgrp might already have been destroyed while we're trying to
  3425. * grab the mutexes.
  3426. */
  3427. if (!cgroup_is_dead(cgrp))
  3428. ret = cgroup_destroy_locked(cgrp);
  3429. mutex_unlock(&cgroup_mutex);
  3430. mutex_unlock(&cgroup_tree_mutex);
  3431. kernfs_unbreak_active_protection(kn);
  3432. cgroup_put(cgrp);
  3433. return ret;
  3434. }
  3435. static struct kernfs_syscall_ops cgroup_kf_syscall_ops = {
  3436. .remount_fs = cgroup_remount,
  3437. .show_options = cgroup_show_options,
  3438. .mkdir = cgroup_mkdir,
  3439. .rmdir = cgroup_rmdir,
  3440. .rename = cgroup_rename,
  3441. };
  3442. static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
  3443. {
  3444. struct cgroup_subsys_state *css;
  3445. printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
  3446. mutex_lock(&cgroup_tree_mutex);
  3447. mutex_lock(&cgroup_mutex);
  3448. INIT_LIST_HEAD(&ss->cfts);
  3449. /* Create the top cgroup state for this subsystem */
  3450. ss->root = &cgroup_dummy_root;
  3451. css = ss->css_alloc(cgroup_css(cgroup_dummy_top, ss));
  3452. /* We don't handle early failures gracefully */
  3453. BUG_ON(IS_ERR(css));
  3454. init_css(css, ss, cgroup_dummy_top);
  3455. /* Update the init_css_set to contain a subsys
  3456. * pointer to this state - since the subsystem is
  3457. * newly registered, all tasks and hence the
  3458. * init_css_set is in the subsystem's top cgroup. */
  3459. init_css_set.subsys[ss->id] = css;
  3460. need_forkexit_callback |= ss->fork || ss->exit;
  3461. /* At system boot, before all subsystems have been
  3462. * registered, no tasks have been forked, so we don't
  3463. * need to invoke fork callbacks here. */
  3464. BUG_ON(!list_empty(&init_task.tasks));
  3465. BUG_ON(online_css(css));
  3466. mutex_unlock(&cgroup_mutex);
  3467. mutex_unlock(&cgroup_tree_mutex);
  3468. }
  3469. /**
  3470. * cgroup_init_early - cgroup initialization at system boot
  3471. *
  3472. * Initialize cgroups at system boot, and initialize any
  3473. * subsystems that request early init.
  3474. */
  3475. int __init cgroup_init_early(void)
  3476. {
  3477. static struct cgroup_sb_opts __initdata opts = { };
  3478. struct cgroup_subsys *ss;
  3479. int i;
  3480. init_cgroup_root(&cgroup_dummy_root, &opts);
  3481. RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
  3482. for_each_subsys(ss, i) {
  3483. WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id,
  3484. "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p name:id=%d:%s\n",
  3485. i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free,
  3486. ss->id, ss->name);
  3487. WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN,
  3488. "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]);
  3489. ss->id = i;
  3490. ss->name = cgroup_subsys_name[i];
  3491. if (ss->early_init)
  3492. cgroup_init_subsys(ss);
  3493. }
  3494. return 0;
  3495. }
  3496. /**
  3497. * cgroup_init - cgroup initialization
  3498. *
  3499. * Register cgroup filesystem and /proc file, and initialize
  3500. * any subsystems that didn't request early init.
  3501. */
  3502. int __init cgroup_init(void)
  3503. {
  3504. struct cgroup_subsys *ss;
  3505. unsigned long key;
  3506. int ssid, err;
  3507. BUG_ON(cgroup_init_cftypes(NULL, cgroup_base_files));
  3508. /* allocate id for the dummy hierarchy */
  3509. mutex_lock(&cgroup_mutex);
  3510. /* Add init_css_set to the hash table */
  3511. key = css_set_hash(init_css_set.subsys);
  3512. hash_add(css_set_table, &init_css_set.hlist, key);
  3513. BUG_ON(cgroup_init_root_id(&cgroup_dummy_root, 0, 1));
  3514. err = idr_alloc(&cgroup_dummy_root.cgroup_idr, cgroup_dummy_top,
  3515. 0, 1, GFP_KERNEL);
  3516. BUG_ON(err < 0);
  3517. cgroup_root_count = 1;
  3518. init_cgrp_cset_link.cset = &init_css_set;
  3519. init_cgrp_cset_link.cgrp = cgroup_dummy_top;
  3520. list_add(&init_cgrp_cset_link.cset_link, &cgroup_dummy_top->cset_links);
  3521. list_add(&init_cgrp_cset_link.cgrp_link, &init_css_set.cgrp_links);
  3522. mutex_unlock(&cgroup_mutex);
  3523. for_each_subsys(ss, ssid) {
  3524. if (!ss->early_init)
  3525. cgroup_init_subsys(ss);
  3526. /*
  3527. * cftype registration needs kmalloc and can't be done
  3528. * during early_init. Register base cftypes separately.
  3529. */
  3530. if (ss->base_cftypes)
  3531. WARN_ON(cgroup_add_cftypes(ss, ss->base_cftypes));
  3532. }
  3533. cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
  3534. if (!cgroup_kobj)
  3535. return -ENOMEM;
  3536. err = register_filesystem(&cgroup_fs_type);
  3537. if (err < 0) {
  3538. kobject_put(cgroup_kobj);
  3539. return err;
  3540. }
  3541. proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
  3542. return 0;
  3543. }
  3544. static int __init cgroup_wq_init(void)
  3545. {
  3546. /*
  3547. * There isn't much point in executing destruction path in
  3548. * parallel. Good chunk is serialized with cgroup_mutex anyway.
  3549. * Use 1 for @max_active.
  3550. *
  3551. * We would prefer to do this in cgroup_init() above, but that
  3552. * is called before init_workqueues(): so leave this until after.
  3553. */
  3554. cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1);
  3555. BUG_ON(!cgroup_destroy_wq);
  3556. /*
  3557. * Used to destroy pidlists and separate to serve as flush domain.
  3558. * Cap @max_active to 1 too.
  3559. */
  3560. cgroup_pidlist_destroy_wq = alloc_workqueue("cgroup_pidlist_destroy",
  3561. 0, 1);
  3562. BUG_ON(!cgroup_pidlist_destroy_wq);
  3563. return 0;
  3564. }
  3565. core_initcall(cgroup_wq_init);
  3566. /*
  3567. * proc_cgroup_show()
  3568. * - Print task's cgroup paths into seq_file, one line for each hierarchy
  3569. * - Used for /proc/<pid>/cgroup.
  3570. */
  3571. /* TODO: Use a proper seq_file iterator */
  3572. int proc_cgroup_show(struct seq_file *m, void *v)
  3573. {
  3574. struct pid *pid;
  3575. struct task_struct *tsk;
  3576. char *buf, *path;
  3577. int retval;
  3578. struct cgroupfs_root *root;
  3579. retval = -ENOMEM;
  3580. buf = kmalloc(PATH_MAX, GFP_KERNEL);
  3581. if (!buf)
  3582. goto out;
  3583. retval = -ESRCH;
  3584. pid = m->private;
  3585. tsk = get_pid_task(pid, PIDTYPE_PID);
  3586. if (!tsk)
  3587. goto out_free;
  3588. retval = 0;
  3589. mutex_lock(&cgroup_mutex);
  3590. down_read(&css_set_rwsem);
  3591. for_each_active_root(root) {
  3592. struct cgroup_subsys *ss;
  3593. struct cgroup *cgrp;
  3594. int ssid, count = 0;
  3595. seq_printf(m, "%d:", root->hierarchy_id);
  3596. for_each_subsys(ss, ssid)
  3597. if (root->subsys_mask & (1 << ssid))
  3598. seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
  3599. if (strlen(root->name))
  3600. seq_printf(m, "%sname=%s", count ? "," : "",
  3601. root->name);
  3602. seq_putc(m, ':');
  3603. cgrp = task_cgroup_from_root(tsk, root);
  3604. path = cgroup_path(cgrp, buf, PATH_MAX);
  3605. if (!path) {
  3606. retval = -ENAMETOOLONG;
  3607. goto out_unlock;
  3608. }
  3609. seq_puts(m, path);
  3610. seq_putc(m, '\n');
  3611. }
  3612. out_unlock:
  3613. up_read(&css_set_rwsem);
  3614. mutex_unlock(&cgroup_mutex);
  3615. put_task_struct(tsk);
  3616. out_free:
  3617. kfree(buf);
  3618. out:
  3619. return retval;
  3620. }
  3621. /* Display information about each subsystem and each hierarchy */
  3622. static int proc_cgroupstats_show(struct seq_file *m, void *v)
  3623. {
  3624. struct cgroup_subsys *ss;
  3625. int i;
  3626. seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
  3627. /*
  3628. * ideally we don't want subsystems moving around while we do this.
  3629. * cgroup_mutex is also necessary to guarantee an atomic snapshot of
  3630. * subsys/hierarchy state.
  3631. */
  3632. mutex_lock(&cgroup_mutex);
  3633. for_each_subsys(ss, i)
  3634. seq_printf(m, "%s\t%d\t%d\t%d\n",
  3635. ss->name, ss->root->hierarchy_id,
  3636. atomic_read(&ss->root->nr_cgrps), !ss->disabled);
  3637. mutex_unlock(&cgroup_mutex);
  3638. return 0;
  3639. }
  3640. static int cgroupstats_open(struct inode *inode, struct file *file)
  3641. {
  3642. return single_open(file, proc_cgroupstats_show, NULL);
  3643. }
  3644. static const struct file_operations proc_cgroupstats_operations = {
  3645. .open = cgroupstats_open,
  3646. .read = seq_read,
  3647. .llseek = seq_lseek,
  3648. .release = single_release,
  3649. };
  3650. /**
  3651. * cgroup_fork - initialize cgroup related fields during copy_process()
  3652. * @child: pointer to task_struct of forking parent process.
  3653. *
  3654. * A task is associated with the init_css_set until cgroup_post_fork()
  3655. * attaches it to the parent's css_set. Empty cg_list indicates that
  3656. * @child isn't holding reference to its css_set.
  3657. */
  3658. void cgroup_fork(struct task_struct *child)
  3659. {
  3660. RCU_INIT_POINTER(child->cgroups, &init_css_set);
  3661. INIT_LIST_HEAD(&child->cg_list);
  3662. }
  3663. /**
  3664. * cgroup_post_fork - called on a new task after adding it to the task list
  3665. * @child: the task in question
  3666. *
  3667. * Adds the task to the list running through its css_set if necessary and
  3668. * call the subsystem fork() callbacks. Has to be after the task is
  3669. * visible on the task list in case we race with the first call to
  3670. * cgroup_task_iter_start() - to guarantee that the new task ends up on its
  3671. * list.
  3672. */
  3673. void cgroup_post_fork(struct task_struct *child)
  3674. {
  3675. struct cgroup_subsys *ss;
  3676. int i;
  3677. /*
  3678. * This may race against cgroup_enable_task_cg_links(). As that
  3679. * function sets use_task_css_set_links before grabbing
  3680. * tasklist_lock and we just went through tasklist_lock to add
  3681. * @child, it's guaranteed that either we see the set
  3682. * use_task_css_set_links or cgroup_enable_task_cg_lists() sees
  3683. * @child during its iteration.
  3684. *
  3685. * If we won the race, @child is associated with %current's
  3686. * css_set. Grabbing css_set_rwsem guarantees both that the
  3687. * association is stable, and, on completion of the parent's
  3688. * migration, @child is visible in the source of migration or
  3689. * already in the destination cgroup. This guarantee is necessary
  3690. * when implementing operations which need to migrate all tasks of
  3691. * a cgroup to another.
  3692. *
  3693. * Note that if we lose to cgroup_enable_task_cg_links(), @child
  3694. * will remain in init_css_set. This is safe because all tasks are
  3695. * in the init_css_set before cg_links is enabled and there's no
  3696. * operation which transfers all tasks out of init_css_set.
  3697. */
  3698. if (use_task_css_set_links) {
  3699. struct css_set *cset;
  3700. down_write(&css_set_rwsem);
  3701. cset = task_css_set(current);
  3702. if (list_empty(&child->cg_list)) {
  3703. rcu_assign_pointer(child->cgroups, cset);
  3704. list_add(&child->cg_list, &cset->tasks);
  3705. get_css_set(cset);
  3706. }
  3707. up_write(&css_set_rwsem);
  3708. }
  3709. /*
  3710. * Call ss->fork(). This must happen after @child is linked on
  3711. * css_set; otherwise, @child might change state between ->fork()
  3712. * and addition to css_set.
  3713. */
  3714. if (need_forkexit_callback) {
  3715. for_each_subsys(ss, i)
  3716. if (ss->fork)
  3717. ss->fork(child);
  3718. }
  3719. }
  3720. /**
  3721. * cgroup_exit - detach cgroup from exiting task
  3722. * @tsk: pointer to task_struct of exiting process
  3723. * @run_callback: run exit callbacks?
  3724. *
  3725. * Description: Detach cgroup from @tsk and release it.
  3726. *
  3727. * Note that cgroups marked notify_on_release force every task in
  3728. * them to take the global cgroup_mutex mutex when exiting.
  3729. * This could impact scaling on very large systems. Be reluctant to
  3730. * use notify_on_release cgroups where very high task exit scaling
  3731. * is required on large systems.
  3732. *
  3733. * We set the exiting tasks cgroup to the root cgroup (top_cgroup). We
  3734. * call cgroup_exit() while the task is still competent to handle
  3735. * notify_on_release(), then leave the task attached to the root cgroup in
  3736. * each hierarchy for the remainder of its exit. No need to bother with
  3737. * init_css_set refcnting. init_css_set never goes away and we can't race
  3738. * with migration path - either PF_EXITING is visible to migration path or
  3739. * @tsk never got on the tasklist.
  3740. */
  3741. void cgroup_exit(struct task_struct *tsk, int run_callbacks)
  3742. {
  3743. struct cgroup_subsys *ss;
  3744. struct css_set *cset;
  3745. bool put_cset = false;
  3746. int i;
  3747. /*
  3748. * Unlink from @tsk from its css_set. As migration path can't race
  3749. * with us, we can check cg_list without grabbing css_set_rwsem.
  3750. */
  3751. if (!list_empty(&tsk->cg_list)) {
  3752. down_write(&css_set_rwsem);
  3753. list_del_init(&tsk->cg_list);
  3754. up_write(&css_set_rwsem);
  3755. put_cset = true;
  3756. }
  3757. /* Reassign the task to the init_css_set. */
  3758. cset = task_css_set(tsk);
  3759. RCU_INIT_POINTER(tsk->cgroups, &init_css_set);
  3760. if (run_callbacks && need_forkexit_callback) {
  3761. /* see cgroup_post_fork() for details */
  3762. for_each_subsys(ss, i) {
  3763. if (ss->exit) {
  3764. struct cgroup_subsys_state *old_css = cset->subsys[i];
  3765. struct cgroup_subsys_state *css = task_css(tsk, i);
  3766. ss->exit(css, old_css, tsk);
  3767. }
  3768. }
  3769. }
  3770. if (put_cset)
  3771. put_css_set(cset, true);
  3772. }
  3773. static void check_for_release(struct cgroup *cgrp)
  3774. {
  3775. if (cgroup_is_releasable(cgrp) &&
  3776. list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) {
  3777. /*
  3778. * Control Group is currently removeable. If it's not
  3779. * already queued for a userspace notification, queue
  3780. * it now
  3781. */
  3782. int need_schedule_work = 0;
  3783. raw_spin_lock(&release_list_lock);
  3784. if (!cgroup_is_dead(cgrp) &&
  3785. list_empty(&cgrp->release_list)) {
  3786. list_add(&cgrp->release_list, &release_list);
  3787. need_schedule_work = 1;
  3788. }
  3789. raw_spin_unlock(&release_list_lock);
  3790. if (need_schedule_work)
  3791. schedule_work(&release_agent_work);
  3792. }
  3793. }
  3794. /*
  3795. * Notify userspace when a cgroup is released, by running the
  3796. * configured release agent with the name of the cgroup (path
  3797. * relative to the root of cgroup file system) as the argument.
  3798. *
  3799. * Most likely, this user command will try to rmdir this cgroup.
  3800. *
  3801. * This races with the possibility that some other task will be
  3802. * attached to this cgroup before it is removed, or that some other
  3803. * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
  3804. * The presumed 'rmdir' will fail quietly if this cgroup is no longer
  3805. * unused, and this cgroup will be reprieved from its death sentence,
  3806. * to continue to serve a useful existence. Next time it's released,
  3807. * we will get notified again, if it still has 'notify_on_release' set.
  3808. *
  3809. * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
  3810. * means only wait until the task is successfully execve()'d. The
  3811. * separate release agent task is forked by call_usermodehelper(),
  3812. * then control in this thread returns here, without waiting for the
  3813. * release agent task. We don't bother to wait because the caller of
  3814. * this routine has no use for the exit status of the release agent
  3815. * task, so no sense holding our caller up for that.
  3816. */
  3817. static void cgroup_release_agent(struct work_struct *work)
  3818. {
  3819. BUG_ON(work != &release_agent_work);
  3820. mutex_lock(&cgroup_mutex);
  3821. raw_spin_lock(&release_list_lock);
  3822. while (!list_empty(&release_list)) {
  3823. char *argv[3], *envp[3];
  3824. int i;
  3825. char *pathbuf = NULL, *agentbuf = NULL, *path;
  3826. struct cgroup *cgrp = list_entry(release_list.next,
  3827. struct cgroup,
  3828. release_list);
  3829. list_del_init(&cgrp->release_list);
  3830. raw_spin_unlock(&release_list_lock);
  3831. pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
  3832. if (!pathbuf)
  3833. goto continue_free;
  3834. path = cgroup_path(cgrp, pathbuf, PATH_MAX);
  3835. if (!path)
  3836. goto continue_free;
  3837. agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
  3838. if (!agentbuf)
  3839. goto continue_free;
  3840. i = 0;
  3841. argv[i++] = agentbuf;
  3842. argv[i++] = path;
  3843. argv[i] = NULL;
  3844. i = 0;
  3845. /* minimal command environment */
  3846. envp[i++] = "HOME=/";
  3847. envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
  3848. envp[i] = NULL;
  3849. /* Drop the lock while we invoke the usermode helper,
  3850. * since the exec could involve hitting disk and hence
  3851. * be a slow process */
  3852. mutex_unlock(&cgroup_mutex);
  3853. call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
  3854. mutex_lock(&cgroup_mutex);
  3855. continue_free:
  3856. kfree(pathbuf);
  3857. kfree(agentbuf);
  3858. raw_spin_lock(&release_list_lock);
  3859. }
  3860. raw_spin_unlock(&release_list_lock);
  3861. mutex_unlock(&cgroup_mutex);
  3862. }
  3863. static int __init cgroup_disable(char *str)
  3864. {
  3865. struct cgroup_subsys *ss;
  3866. char *token;
  3867. int i;
  3868. while ((token = strsep(&str, ",")) != NULL) {
  3869. if (!*token)
  3870. continue;
  3871. for_each_subsys(ss, i) {
  3872. if (!strcmp(token, ss->name)) {
  3873. ss->disabled = 1;
  3874. printk(KERN_INFO "Disabling %s control group"
  3875. " subsystem\n", ss->name);
  3876. break;
  3877. }
  3878. }
  3879. }
  3880. return 1;
  3881. }
  3882. __setup("cgroup_disable=", cgroup_disable);
  3883. /**
  3884. * css_tryget_from_dir - get corresponding css from the dentry of a cgroup dir
  3885. * @dentry: directory dentry of interest
  3886. * @ss: subsystem of interest
  3887. *
  3888. * If @dentry is a directory for a cgroup which has @ss enabled on it, try
  3889. * to get the corresponding css and return it. If such css doesn't exist
  3890. * or can't be pinned, an ERR_PTR value is returned.
  3891. */
  3892. struct cgroup_subsys_state *css_tryget_from_dir(struct dentry *dentry,
  3893. struct cgroup_subsys *ss)
  3894. {
  3895. struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
  3896. struct cgroup_subsys_state *css = NULL;
  3897. struct cgroup *cgrp;
  3898. /* is @dentry a cgroup dir? */
  3899. if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
  3900. kernfs_type(kn) != KERNFS_DIR)
  3901. return ERR_PTR(-EBADF);
  3902. rcu_read_lock();
  3903. /*
  3904. * This path doesn't originate from kernfs and @kn could already
  3905. * have been or be removed at any point. @kn->priv is RCU
  3906. * protected for this access. See destroy_locked() for details.
  3907. */
  3908. cgrp = rcu_dereference(kn->priv);
  3909. if (cgrp)
  3910. css = cgroup_css(cgrp, ss);
  3911. if (!css || !css_tryget(css))
  3912. css = ERR_PTR(-ENOENT);
  3913. rcu_read_unlock();
  3914. return css;
  3915. }
  3916. /**
  3917. * css_from_id - lookup css by id
  3918. * @id: the cgroup id
  3919. * @ss: cgroup subsys to be looked into
  3920. *
  3921. * Returns the css if there's valid one with @id, otherwise returns NULL.
  3922. * Should be called under rcu_read_lock().
  3923. */
  3924. struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
  3925. {
  3926. struct cgroup *cgrp;
  3927. cgroup_assert_mutexes_or_rcu_locked();
  3928. cgrp = idr_find(&ss->root->cgroup_idr, id);
  3929. if (cgrp)
  3930. return cgroup_css(cgrp, ss);
  3931. return NULL;
  3932. }
  3933. #ifdef CONFIG_CGROUP_DEBUG
  3934. static struct cgroup_subsys_state *
  3935. debug_css_alloc(struct cgroup_subsys_state *parent_css)
  3936. {
  3937. struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
  3938. if (!css)
  3939. return ERR_PTR(-ENOMEM);
  3940. return css;
  3941. }
  3942. static void debug_css_free(struct cgroup_subsys_state *css)
  3943. {
  3944. kfree(css);
  3945. }
  3946. static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
  3947. struct cftype *cft)
  3948. {
  3949. return cgroup_task_count(css->cgroup);
  3950. }
  3951. static u64 current_css_set_read(struct cgroup_subsys_state *css,
  3952. struct cftype *cft)
  3953. {
  3954. return (u64)(unsigned long)current->cgroups;
  3955. }
  3956. static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css,
  3957. struct cftype *cft)
  3958. {
  3959. u64 count;
  3960. rcu_read_lock();
  3961. count = atomic_read(&task_css_set(current)->refcount);
  3962. rcu_read_unlock();
  3963. return count;
  3964. }
  3965. static int current_css_set_cg_links_read(struct seq_file *seq, void *v)
  3966. {
  3967. struct cgrp_cset_link *link;
  3968. struct css_set *cset;
  3969. char *name_buf;
  3970. name_buf = kmalloc(NAME_MAX + 1, GFP_KERNEL);
  3971. if (!name_buf)
  3972. return -ENOMEM;
  3973. down_read(&css_set_rwsem);
  3974. rcu_read_lock();
  3975. cset = rcu_dereference(current->cgroups);
  3976. list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
  3977. struct cgroup *c = link->cgrp;
  3978. const char *name = "?";
  3979. if (c != cgroup_dummy_top) {
  3980. cgroup_name(c, name_buf, NAME_MAX + 1);
  3981. name = name_buf;
  3982. }
  3983. seq_printf(seq, "Root %d group %s\n",
  3984. c->root->hierarchy_id, name);
  3985. }
  3986. rcu_read_unlock();
  3987. up_read(&css_set_rwsem);
  3988. kfree(name_buf);
  3989. return 0;
  3990. }
  3991. #define MAX_TASKS_SHOWN_PER_CSS 25
  3992. static int cgroup_css_links_read(struct seq_file *seq, void *v)
  3993. {
  3994. struct cgroup_subsys_state *css = seq_css(seq);
  3995. struct cgrp_cset_link *link;
  3996. down_read(&css_set_rwsem);
  3997. list_for_each_entry(link, &css->cgroup->cset_links, cset_link) {
  3998. struct css_set *cset = link->cset;
  3999. struct task_struct *task;
  4000. int count = 0;
  4001. seq_printf(seq, "css_set %p\n", cset);
  4002. list_for_each_entry(task, &cset->tasks, cg_list) {
  4003. if (count++ > MAX_TASKS_SHOWN_PER_CSS)
  4004. goto overflow;
  4005. seq_printf(seq, " task %d\n", task_pid_vnr(task));
  4006. }
  4007. list_for_each_entry(task, &cset->mg_tasks, cg_list) {
  4008. if (count++ > MAX_TASKS_SHOWN_PER_CSS)
  4009. goto overflow;
  4010. seq_printf(seq, " task %d\n", task_pid_vnr(task));
  4011. }
  4012. continue;
  4013. overflow:
  4014. seq_puts(seq, " ...\n");
  4015. }
  4016. up_read(&css_set_rwsem);
  4017. return 0;
  4018. }
  4019. static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft)
  4020. {
  4021. return test_bit(CGRP_RELEASABLE, &css->cgroup->flags);
  4022. }
  4023. static struct cftype debug_files[] = {
  4024. {
  4025. .name = "taskcount",
  4026. .read_u64 = debug_taskcount_read,
  4027. },
  4028. {
  4029. .name = "current_css_set",
  4030. .read_u64 = current_css_set_read,
  4031. },
  4032. {
  4033. .name = "current_css_set_refcount",
  4034. .read_u64 = current_css_set_refcount_read,
  4035. },
  4036. {
  4037. .name = "current_css_set_cg_links",
  4038. .seq_show = current_css_set_cg_links_read,
  4039. },
  4040. {
  4041. .name = "cgroup_css_links",
  4042. .seq_show = cgroup_css_links_read,
  4043. },
  4044. {
  4045. .name = "releasable",
  4046. .read_u64 = releasable_read,
  4047. },
  4048. { } /* terminate */
  4049. };
  4050. struct cgroup_subsys debug_cgrp_subsys = {
  4051. .css_alloc = debug_css_alloc,
  4052. .css_free = debug_css_free,
  4053. .base_cftypes = debug_files,
  4054. };
  4055. #endif /* CONFIG_CGROUP_DEBUG */