cgroup.c 130 KB

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