cgroup.c 128 KB

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