cgroup.c 120 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562
  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. * Copyright notices from the original cpuset code:
  8. * --------------------------------------------------
  9. * Copyright (C) 2003 BULL SA.
  10. * Copyright (C) 2004-2006 Silicon Graphics, Inc.
  11. *
  12. * Portions derived from Patrick Mochel's sysfs code.
  13. * sysfs is Copyright (c) 2001-3 Patrick Mochel
  14. *
  15. * 2003-10-10 Written by Simon Derr.
  16. * 2003-10-22 Updates by Stephen Hemminger.
  17. * 2004 May-July Rework by Paul Jackson.
  18. * ---------------------------------------------------
  19. *
  20. * This file is subject to the terms and conditions of the GNU General Public
  21. * License. See the file COPYING in the main directory of the Linux
  22. * distribution for more details.
  23. */
  24. #include <linux/cgroup.h>
  25. #include <linux/module.h>
  26. #include <linux/ctype.h>
  27. #include <linux/errno.h>
  28. #include <linux/fs.h>
  29. #include <linux/kernel.h>
  30. #include <linux/list.h>
  31. #include <linux/mm.h>
  32. #include <linux/mutex.h>
  33. #include <linux/mount.h>
  34. #include <linux/pagemap.h>
  35. #include <linux/proc_fs.h>
  36. #include <linux/rcupdate.h>
  37. #include <linux/sched.h>
  38. #include <linux/backing-dev.h>
  39. #include <linux/seq_file.h>
  40. #include <linux/slab.h>
  41. #include <linux/magic.h>
  42. #include <linux/spinlock.h>
  43. #include <linux/string.h>
  44. #include <linux/sort.h>
  45. #include <linux/kmod.h>
  46. #include <linux/module.h>
  47. #include <linux/delayacct.h>
  48. #include <linux/cgroupstats.h>
  49. #include <linux/hash.h>
  50. #include <linux/namei.h>
  51. #include <linux/smp_lock.h>
  52. #include <linux/pid_namespace.h>
  53. #include <linux/idr.h>
  54. #include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
  55. #include <asm/atomic.h>
  56. static DEFINE_MUTEX(cgroup_mutex);
  57. /*
  58. * Generate an array of cgroup subsystem pointers. At boot time, this is
  59. * populated up to CGROUP_BUILTIN_SUBSYS_COUNT, and modular subsystems are
  60. * registered after that. The mutable section of this array is protected by
  61. * cgroup_mutex.
  62. */
  63. #define SUBSYS(_x) &_x ## _subsys,
  64. static struct cgroup_subsys *subsys[CGROUP_SUBSYS_COUNT] = {
  65. #include <linux/cgroup_subsys.h>
  66. };
  67. #define MAX_CGROUP_ROOT_NAMELEN 64
  68. /*
  69. * A cgroupfs_root represents the root of a cgroup hierarchy,
  70. * and may be associated with a superblock to form an active
  71. * hierarchy
  72. */
  73. struct cgroupfs_root {
  74. struct super_block *sb;
  75. /*
  76. * The bitmask of subsystems intended to be attached to this
  77. * hierarchy
  78. */
  79. unsigned long subsys_bits;
  80. /* Unique id for this hierarchy. */
  81. int hierarchy_id;
  82. /* The bitmask of subsystems currently attached to this hierarchy */
  83. unsigned long actual_subsys_bits;
  84. /* A list running through the attached subsystems */
  85. struct list_head subsys_list;
  86. /* The root cgroup for this hierarchy */
  87. struct cgroup top_cgroup;
  88. /* Tracks how many cgroups are currently defined in hierarchy.*/
  89. int number_of_cgroups;
  90. /* A list running through the active hierarchies */
  91. struct list_head root_list;
  92. /* Hierarchy-specific flags */
  93. unsigned long flags;
  94. /* The path to use for release notifications. */
  95. char release_agent_path[PATH_MAX];
  96. /* The name for this hierarchy - may be empty */
  97. char name[MAX_CGROUP_ROOT_NAMELEN];
  98. };
  99. /*
  100. * The "rootnode" hierarchy is the "dummy hierarchy", reserved for the
  101. * subsystems that are otherwise unattached - it never has more than a
  102. * single cgroup, and all tasks are part of that cgroup.
  103. */
  104. static struct cgroupfs_root rootnode;
  105. /*
  106. * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when
  107. * cgroup_subsys->use_id != 0.
  108. */
  109. #define CSS_ID_MAX (65535)
  110. struct css_id {
  111. /*
  112. * The css to which this ID points. This pointer is set to valid value
  113. * after cgroup is populated. If cgroup is removed, this will be NULL.
  114. * This pointer is expected to be RCU-safe because destroy()
  115. * is called after synchronize_rcu(). But for safe use, css_is_removed()
  116. * css_tryget() should be used for avoiding race.
  117. */
  118. struct cgroup_subsys_state *css;
  119. /*
  120. * ID of this css.
  121. */
  122. unsigned short id;
  123. /*
  124. * Depth in hierarchy which this ID belongs to.
  125. */
  126. unsigned short depth;
  127. /*
  128. * ID is freed by RCU. (and lookup routine is RCU safe.)
  129. */
  130. struct rcu_head rcu_head;
  131. /*
  132. * Hierarchy of CSS ID belongs to.
  133. */
  134. unsigned short stack[0]; /* Array of Length (depth+1) */
  135. };
  136. /* The list of hierarchy roots */
  137. static LIST_HEAD(roots);
  138. static int root_count;
  139. static DEFINE_IDA(hierarchy_ida);
  140. static int next_hierarchy_id;
  141. static DEFINE_SPINLOCK(hierarchy_id_lock);
  142. /* dummytop is a shorthand for the dummy hierarchy's top cgroup */
  143. #define dummytop (&rootnode.top_cgroup)
  144. /* This flag indicates whether tasks in the fork and exit paths should
  145. * check for fork/exit handlers to call. This avoids us having to do
  146. * extra work in the fork/exit path if none of the subsystems need to
  147. * be called.
  148. */
  149. static int need_forkexit_callback __read_mostly;
  150. #ifdef CONFIG_PROVE_LOCKING
  151. int cgroup_lock_is_held(void)
  152. {
  153. return lockdep_is_held(&cgroup_mutex);
  154. }
  155. #else /* #ifdef CONFIG_PROVE_LOCKING */
  156. int cgroup_lock_is_held(void)
  157. {
  158. return mutex_is_locked(&cgroup_mutex);
  159. }
  160. #endif /* #else #ifdef CONFIG_PROVE_LOCKING */
  161. EXPORT_SYMBOL_GPL(cgroup_lock_is_held);
  162. /* convenient tests for these bits */
  163. inline int cgroup_is_removed(const struct cgroup *cgrp)
  164. {
  165. return test_bit(CGRP_REMOVED, &cgrp->flags);
  166. }
  167. /* bits in struct cgroupfs_root flags field */
  168. enum {
  169. ROOT_NOPREFIX, /* mounted subsystems have no named prefix */
  170. };
  171. static int cgroup_is_releasable(const struct cgroup *cgrp)
  172. {
  173. const int bits =
  174. (1 << CGRP_RELEASABLE) |
  175. (1 << CGRP_NOTIFY_ON_RELEASE);
  176. return (cgrp->flags & bits) == bits;
  177. }
  178. static int notify_on_release(const struct cgroup *cgrp)
  179. {
  180. return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
  181. }
  182. /*
  183. * for_each_subsys() allows you to iterate on each subsystem attached to
  184. * an active hierarchy
  185. */
  186. #define for_each_subsys(_root, _ss) \
  187. list_for_each_entry(_ss, &_root->subsys_list, sibling)
  188. /* for_each_active_root() allows you to iterate across the active hierarchies */
  189. #define for_each_active_root(_root) \
  190. list_for_each_entry(_root, &roots, root_list)
  191. /* the list of cgroups eligible for automatic release. Protected by
  192. * release_list_lock */
  193. static LIST_HEAD(release_list);
  194. static DEFINE_SPINLOCK(release_list_lock);
  195. static void cgroup_release_agent(struct work_struct *work);
  196. static DECLARE_WORK(release_agent_work, cgroup_release_agent);
  197. static void check_for_release(struct cgroup *cgrp);
  198. /* Link structure for associating css_set objects with cgroups */
  199. struct cg_cgroup_link {
  200. /*
  201. * List running through cg_cgroup_links associated with a
  202. * cgroup, anchored on cgroup->css_sets
  203. */
  204. struct list_head cgrp_link_list;
  205. struct cgroup *cgrp;
  206. /*
  207. * List running through cg_cgroup_links pointing at a
  208. * single css_set object, anchored on css_set->cg_links
  209. */
  210. struct list_head cg_link_list;
  211. struct css_set *cg;
  212. };
  213. /* The default css_set - used by init and its children prior to any
  214. * hierarchies being mounted. It contains a pointer to the root state
  215. * for each subsystem. Also used to anchor the list of css_sets. Not
  216. * reference-counted, to improve performance when child cgroups
  217. * haven't been created.
  218. */
  219. static struct css_set init_css_set;
  220. static struct cg_cgroup_link init_css_set_link;
  221. static int cgroup_init_idr(struct cgroup_subsys *ss,
  222. struct cgroup_subsys_state *css);
  223. /* css_set_lock protects the list of css_set objects, and the
  224. * chain of tasks off each css_set. Nests outside task->alloc_lock
  225. * due to cgroup_iter_start() */
  226. static DEFINE_RWLOCK(css_set_lock);
  227. static int css_set_count;
  228. /*
  229. * hash table for cgroup groups. This improves the performance to find
  230. * an existing css_set. This hash doesn't (currently) take into
  231. * account cgroups in empty hierarchies.
  232. */
  233. #define CSS_SET_HASH_BITS 7
  234. #define CSS_SET_TABLE_SIZE (1 << CSS_SET_HASH_BITS)
  235. static struct hlist_head css_set_table[CSS_SET_TABLE_SIZE];
  236. static struct hlist_head *css_set_hash(struct cgroup_subsys_state *css[])
  237. {
  238. int i;
  239. int index;
  240. unsigned long tmp = 0UL;
  241. for (i = 0; i < CGROUP_SUBSYS_COUNT; i++)
  242. tmp += (unsigned long)css[i];
  243. tmp = (tmp >> 16) ^ tmp;
  244. index = hash_long(tmp, CSS_SET_HASH_BITS);
  245. return &css_set_table[index];
  246. }
  247. static void free_css_set_rcu(struct rcu_head *obj)
  248. {
  249. struct css_set *cg = container_of(obj, struct css_set, rcu_head);
  250. kfree(cg);
  251. }
  252. /* We don't maintain the lists running through each css_set to its
  253. * task until after the first call to cgroup_iter_start(). This
  254. * reduces the fork()/exit() overhead for people who have cgroups
  255. * compiled into their kernel but not actually in use */
  256. static int use_task_css_set_links __read_mostly;
  257. static void __put_css_set(struct css_set *cg, int taskexit)
  258. {
  259. struct cg_cgroup_link *link;
  260. struct cg_cgroup_link *saved_link;
  261. /*
  262. * Ensure that the refcount doesn't hit zero while any readers
  263. * can see it. Similar to atomic_dec_and_lock(), but for an
  264. * rwlock
  265. */
  266. if (atomic_add_unless(&cg->refcount, -1, 1))
  267. return;
  268. write_lock(&css_set_lock);
  269. if (!atomic_dec_and_test(&cg->refcount)) {
  270. write_unlock(&css_set_lock);
  271. return;
  272. }
  273. /* This css_set is dead. unlink it and release cgroup refcounts */
  274. hlist_del(&cg->hlist);
  275. css_set_count--;
  276. list_for_each_entry_safe(link, saved_link, &cg->cg_links,
  277. cg_link_list) {
  278. struct cgroup *cgrp = link->cgrp;
  279. list_del(&link->cg_link_list);
  280. list_del(&link->cgrp_link_list);
  281. if (atomic_dec_and_test(&cgrp->count) &&
  282. notify_on_release(cgrp)) {
  283. if (taskexit)
  284. set_bit(CGRP_RELEASABLE, &cgrp->flags);
  285. check_for_release(cgrp);
  286. }
  287. kfree(link);
  288. }
  289. write_unlock(&css_set_lock);
  290. call_rcu(&cg->rcu_head, free_css_set_rcu);
  291. }
  292. /*
  293. * refcounted get/put for css_set objects
  294. */
  295. static inline void get_css_set(struct css_set *cg)
  296. {
  297. atomic_inc(&cg->refcount);
  298. }
  299. static inline void put_css_set(struct css_set *cg)
  300. {
  301. __put_css_set(cg, 0);
  302. }
  303. static inline void put_css_set_taskexit(struct css_set *cg)
  304. {
  305. __put_css_set(cg, 1);
  306. }
  307. /*
  308. * compare_css_sets - helper function for find_existing_css_set().
  309. * @cg: candidate css_set being tested
  310. * @old_cg: existing css_set for a task
  311. * @new_cgrp: cgroup that's being entered by the task
  312. * @template: desired set of css pointers in css_set (pre-calculated)
  313. *
  314. * Returns true if "cg" matches "old_cg" except for the hierarchy
  315. * which "new_cgrp" belongs to, for which it should match "new_cgrp".
  316. */
  317. static bool compare_css_sets(struct css_set *cg,
  318. struct css_set *old_cg,
  319. struct cgroup *new_cgrp,
  320. struct cgroup_subsys_state *template[])
  321. {
  322. struct list_head *l1, *l2;
  323. if (memcmp(template, cg->subsys, sizeof(cg->subsys))) {
  324. /* Not all subsystems matched */
  325. return false;
  326. }
  327. /*
  328. * Compare cgroup pointers in order to distinguish between
  329. * different cgroups in heirarchies with no subsystems. We
  330. * could get by with just this check alone (and skip the
  331. * memcmp above) but on most setups the memcmp check will
  332. * avoid the need for this more expensive check on almost all
  333. * candidates.
  334. */
  335. l1 = &cg->cg_links;
  336. l2 = &old_cg->cg_links;
  337. while (1) {
  338. struct cg_cgroup_link *cgl1, *cgl2;
  339. struct cgroup *cg1, *cg2;
  340. l1 = l1->next;
  341. l2 = l2->next;
  342. /* See if we reached the end - both lists are equal length. */
  343. if (l1 == &cg->cg_links) {
  344. BUG_ON(l2 != &old_cg->cg_links);
  345. break;
  346. } else {
  347. BUG_ON(l2 == &old_cg->cg_links);
  348. }
  349. /* Locate the cgroups associated with these links. */
  350. cgl1 = list_entry(l1, struct cg_cgroup_link, cg_link_list);
  351. cgl2 = list_entry(l2, struct cg_cgroup_link, cg_link_list);
  352. cg1 = cgl1->cgrp;
  353. cg2 = cgl2->cgrp;
  354. /* Hierarchies should be linked in the same order. */
  355. BUG_ON(cg1->root != cg2->root);
  356. /*
  357. * If this hierarchy is the hierarchy of the cgroup
  358. * that's changing, then we need to check that this
  359. * css_set points to the new cgroup; if it's any other
  360. * hierarchy, then this css_set should point to the
  361. * same cgroup as the old css_set.
  362. */
  363. if (cg1->root == new_cgrp->root) {
  364. if (cg1 != new_cgrp)
  365. return false;
  366. } else {
  367. if (cg1 != cg2)
  368. return false;
  369. }
  370. }
  371. return true;
  372. }
  373. /*
  374. * find_existing_css_set() is a helper for
  375. * find_css_set(), and checks to see whether an existing
  376. * css_set is suitable.
  377. *
  378. * oldcg: the cgroup group that we're using before the cgroup
  379. * transition
  380. *
  381. * cgrp: the cgroup that we're moving into
  382. *
  383. * template: location in which to build the desired set of subsystem
  384. * state objects for the new cgroup group
  385. */
  386. static struct css_set *find_existing_css_set(
  387. struct css_set *oldcg,
  388. struct cgroup *cgrp,
  389. struct cgroup_subsys_state *template[])
  390. {
  391. int i;
  392. struct cgroupfs_root *root = cgrp->root;
  393. struct hlist_head *hhead;
  394. struct hlist_node *node;
  395. struct css_set *cg;
  396. /*
  397. * Build the set of subsystem state objects that we want to see in the
  398. * new css_set. while subsystems can change globally, the entries here
  399. * won't change, so no need for locking.
  400. */
  401. for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
  402. if (root->subsys_bits & (1UL << i)) {
  403. /* Subsystem is in this hierarchy. So we want
  404. * the subsystem state from the new
  405. * cgroup */
  406. template[i] = cgrp->subsys[i];
  407. } else {
  408. /* Subsystem is not in this hierarchy, so we
  409. * don't want to change the subsystem state */
  410. template[i] = oldcg->subsys[i];
  411. }
  412. }
  413. hhead = css_set_hash(template);
  414. hlist_for_each_entry(cg, node, hhead, hlist) {
  415. if (!compare_css_sets(cg, oldcg, cgrp, template))
  416. continue;
  417. /* This css_set matches what we need */
  418. return cg;
  419. }
  420. /* No existing cgroup group matched */
  421. return NULL;
  422. }
  423. static void free_cg_links(struct list_head *tmp)
  424. {
  425. struct cg_cgroup_link *link;
  426. struct cg_cgroup_link *saved_link;
  427. list_for_each_entry_safe(link, saved_link, tmp, cgrp_link_list) {
  428. list_del(&link->cgrp_link_list);
  429. kfree(link);
  430. }
  431. }
  432. /*
  433. * allocate_cg_links() allocates "count" cg_cgroup_link structures
  434. * and chains them on tmp through their cgrp_link_list fields. Returns 0 on
  435. * success or a negative error
  436. */
  437. static int allocate_cg_links(int count, struct list_head *tmp)
  438. {
  439. struct cg_cgroup_link *link;
  440. int i;
  441. INIT_LIST_HEAD(tmp);
  442. for (i = 0; i < count; i++) {
  443. link = kmalloc(sizeof(*link), GFP_KERNEL);
  444. if (!link) {
  445. free_cg_links(tmp);
  446. return -ENOMEM;
  447. }
  448. list_add(&link->cgrp_link_list, tmp);
  449. }
  450. return 0;
  451. }
  452. /**
  453. * link_css_set - a helper function to link a css_set to a cgroup
  454. * @tmp_cg_links: cg_cgroup_link objects allocated by allocate_cg_links()
  455. * @cg: the css_set to be linked
  456. * @cgrp: the destination cgroup
  457. */
  458. static void link_css_set(struct list_head *tmp_cg_links,
  459. struct css_set *cg, struct cgroup *cgrp)
  460. {
  461. struct cg_cgroup_link *link;
  462. BUG_ON(list_empty(tmp_cg_links));
  463. link = list_first_entry(tmp_cg_links, struct cg_cgroup_link,
  464. cgrp_link_list);
  465. link->cg = cg;
  466. link->cgrp = cgrp;
  467. atomic_inc(&cgrp->count);
  468. list_move(&link->cgrp_link_list, &cgrp->css_sets);
  469. /*
  470. * Always add links to the tail of the list so that the list
  471. * is sorted by order of hierarchy creation
  472. */
  473. list_add_tail(&link->cg_link_list, &cg->cg_links);
  474. }
  475. /*
  476. * find_css_set() takes an existing cgroup group and a
  477. * cgroup object, and returns a css_set object that's
  478. * equivalent to the old group, but with the given cgroup
  479. * substituted into the appropriate hierarchy. Must be called with
  480. * cgroup_mutex held
  481. */
  482. static struct css_set *find_css_set(
  483. struct css_set *oldcg, struct cgroup *cgrp)
  484. {
  485. struct css_set *res;
  486. struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT];
  487. struct list_head tmp_cg_links;
  488. struct hlist_head *hhead;
  489. struct cg_cgroup_link *link;
  490. /* First see if we already have a cgroup group that matches
  491. * the desired set */
  492. read_lock(&css_set_lock);
  493. res = find_existing_css_set(oldcg, cgrp, template);
  494. if (res)
  495. get_css_set(res);
  496. read_unlock(&css_set_lock);
  497. if (res)
  498. return res;
  499. res = kmalloc(sizeof(*res), GFP_KERNEL);
  500. if (!res)
  501. return NULL;
  502. /* Allocate all the cg_cgroup_link objects that we'll need */
  503. if (allocate_cg_links(root_count, &tmp_cg_links) < 0) {
  504. kfree(res);
  505. return NULL;
  506. }
  507. atomic_set(&res->refcount, 1);
  508. INIT_LIST_HEAD(&res->cg_links);
  509. INIT_LIST_HEAD(&res->tasks);
  510. INIT_HLIST_NODE(&res->hlist);
  511. /* Copy the set of subsystem state objects generated in
  512. * find_existing_css_set() */
  513. memcpy(res->subsys, template, sizeof(res->subsys));
  514. write_lock(&css_set_lock);
  515. /* Add reference counts and links from the new css_set. */
  516. list_for_each_entry(link, &oldcg->cg_links, cg_link_list) {
  517. struct cgroup *c = link->cgrp;
  518. if (c->root == cgrp->root)
  519. c = cgrp;
  520. link_css_set(&tmp_cg_links, res, c);
  521. }
  522. BUG_ON(!list_empty(&tmp_cg_links));
  523. css_set_count++;
  524. /* Add this cgroup group to the hash table */
  525. hhead = css_set_hash(res->subsys);
  526. hlist_add_head(&res->hlist, hhead);
  527. write_unlock(&css_set_lock);
  528. return res;
  529. }
  530. /*
  531. * Return the cgroup for "task" from the given hierarchy. Must be
  532. * called with cgroup_mutex held.
  533. */
  534. static struct cgroup *task_cgroup_from_root(struct task_struct *task,
  535. struct cgroupfs_root *root)
  536. {
  537. struct css_set *css;
  538. struct cgroup *res = NULL;
  539. BUG_ON(!mutex_is_locked(&cgroup_mutex));
  540. read_lock(&css_set_lock);
  541. /*
  542. * No need to lock the task - since we hold cgroup_mutex the
  543. * task can't change groups, so the only thing that can happen
  544. * is that it exits and its css is set back to init_css_set.
  545. */
  546. css = task->cgroups;
  547. if (css == &init_css_set) {
  548. res = &root->top_cgroup;
  549. } else {
  550. struct cg_cgroup_link *link;
  551. list_for_each_entry(link, &css->cg_links, cg_link_list) {
  552. struct cgroup *c = link->cgrp;
  553. if (c->root == root) {
  554. res = c;
  555. break;
  556. }
  557. }
  558. }
  559. read_unlock(&css_set_lock);
  560. BUG_ON(!res);
  561. return res;
  562. }
  563. /*
  564. * There is one global cgroup mutex. We also require taking
  565. * task_lock() when dereferencing a task's cgroup subsys pointers.
  566. * See "The task_lock() exception", at the end of this comment.
  567. *
  568. * A task must hold cgroup_mutex to modify cgroups.
  569. *
  570. * Any task can increment and decrement the count field without lock.
  571. * So in general, code holding cgroup_mutex can't rely on the count
  572. * field not changing. However, if the count goes to zero, then only
  573. * cgroup_attach_task() can increment it again. Because a count of zero
  574. * means that no tasks are currently attached, therefore there is no
  575. * way a task attached to that cgroup can fork (the other way to
  576. * increment the count). So code holding cgroup_mutex can safely
  577. * assume that if the count is zero, it will stay zero. Similarly, if
  578. * a task holds cgroup_mutex on a cgroup with zero count, it
  579. * knows that the cgroup won't be removed, as cgroup_rmdir()
  580. * needs that mutex.
  581. *
  582. * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
  583. * (usually) take cgroup_mutex. These are the two most performance
  584. * critical pieces of code here. The exception occurs on cgroup_exit(),
  585. * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
  586. * is taken, and if the cgroup count is zero, a usermode call made
  587. * to the release agent with the name of the cgroup (path relative to
  588. * the root of cgroup file system) as the argument.
  589. *
  590. * A cgroup can only be deleted if both its 'count' of using tasks
  591. * is zero, and its list of 'children' cgroups is empty. Since all
  592. * tasks in the system use _some_ cgroup, and since there is always at
  593. * least one task in the system (init, pid == 1), therefore, top_cgroup
  594. * always has either children cgroups and/or using tasks. So we don't
  595. * need a special hack to ensure that top_cgroup cannot be deleted.
  596. *
  597. * The task_lock() exception
  598. *
  599. * The need for this exception arises from the action of
  600. * cgroup_attach_task(), which overwrites one tasks cgroup pointer with
  601. * another. It does so using cgroup_mutex, however there are
  602. * several performance critical places that need to reference
  603. * task->cgroup without the expense of grabbing a system global
  604. * mutex. Therefore except as noted below, when dereferencing or, as
  605. * in cgroup_attach_task(), modifying a task'ss cgroup pointer we use
  606. * task_lock(), which acts on a spinlock (task->alloc_lock) already in
  607. * the task_struct routinely used for such matters.
  608. *
  609. * P.S. One more locking exception. RCU is used to guard the
  610. * update of a tasks cgroup pointer by cgroup_attach_task()
  611. */
  612. /**
  613. * cgroup_lock - lock out any changes to cgroup structures
  614. *
  615. */
  616. void cgroup_lock(void)
  617. {
  618. mutex_lock(&cgroup_mutex);
  619. }
  620. /**
  621. * cgroup_unlock - release lock on cgroup changes
  622. *
  623. * Undo the lock taken in a previous cgroup_lock() call.
  624. */
  625. void cgroup_unlock(void)
  626. {
  627. mutex_unlock(&cgroup_mutex);
  628. }
  629. /*
  630. * A couple of forward declarations required, due to cyclic reference loop:
  631. * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
  632. * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
  633. * -> cgroup_mkdir.
  634. */
  635. static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, int mode);
  636. static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
  637. static int cgroup_populate_dir(struct cgroup *cgrp);
  638. static const struct inode_operations cgroup_dir_inode_operations;
  639. static const struct file_operations proc_cgroupstats_operations;
  640. static struct backing_dev_info cgroup_backing_dev_info = {
  641. .name = "cgroup",
  642. .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
  643. };
  644. static int alloc_css_id(struct cgroup_subsys *ss,
  645. struct cgroup *parent, struct cgroup *child);
  646. static struct inode *cgroup_new_inode(mode_t mode, struct super_block *sb)
  647. {
  648. struct inode *inode = new_inode(sb);
  649. if (inode) {
  650. inode->i_mode = mode;
  651. inode->i_uid = current_fsuid();
  652. inode->i_gid = current_fsgid();
  653. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  654. inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
  655. }
  656. return inode;
  657. }
  658. /*
  659. * Call subsys's pre_destroy handler.
  660. * This is called before css refcnt check.
  661. */
  662. static int cgroup_call_pre_destroy(struct cgroup *cgrp)
  663. {
  664. struct cgroup_subsys *ss;
  665. int ret = 0;
  666. for_each_subsys(cgrp->root, ss)
  667. if (ss->pre_destroy) {
  668. ret = ss->pre_destroy(ss, cgrp);
  669. if (ret)
  670. break;
  671. }
  672. return ret;
  673. }
  674. static void free_cgroup_rcu(struct rcu_head *obj)
  675. {
  676. struct cgroup *cgrp = container_of(obj, struct cgroup, rcu_head);
  677. kfree(cgrp);
  678. }
  679. static void cgroup_diput(struct dentry *dentry, struct inode *inode)
  680. {
  681. /* is dentry a directory ? if so, kfree() associated cgroup */
  682. if (S_ISDIR(inode->i_mode)) {
  683. struct cgroup *cgrp = dentry->d_fsdata;
  684. struct cgroup_subsys *ss;
  685. BUG_ON(!(cgroup_is_removed(cgrp)));
  686. /* It's possible for external users to be holding css
  687. * reference counts on a cgroup; css_put() needs to
  688. * be able to access the cgroup after decrementing
  689. * the reference count in order to know if it needs to
  690. * queue the cgroup to be handled by the release
  691. * agent */
  692. synchronize_rcu();
  693. mutex_lock(&cgroup_mutex);
  694. /*
  695. * Release the subsystem state objects.
  696. */
  697. for_each_subsys(cgrp->root, ss)
  698. ss->destroy(ss, cgrp);
  699. cgrp->root->number_of_cgroups--;
  700. mutex_unlock(&cgroup_mutex);
  701. /*
  702. * Drop the active superblock reference that we took when we
  703. * created the cgroup
  704. */
  705. deactivate_super(cgrp->root->sb);
  706. /*
  707. * if we're getting rid of the cgroup, refcount should ensure
  708. * that there are no pidlists left.
  709. */
  710. BUG_ON(!list_empty(&cgrp->pidlists));
  711. call_rcu(&cgrp->rcu_head, free_cgroup_rcu);
  712. }
  713. iput(inode);
  714. }
  715. static void remove_dir(struct dentry *d)
  716. {
  717. struct dentry *parent = dget(d->d_parent);
  718. d_delete(d);
  719. simple_rmdir(parent->d_inode, d);
  720. dput(parent);
  721. }
  722. static void cgroup_clear_directory(struct dentry *dentry)
  723. {
  724. struct list_head *node;
  725. BUG_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
  726. spin_lock(&dcache_lock);
  727. node = dentry->d_subdirs.next;
  728. while (node != &dentry->d_subdirs) {
  729. struct dentry *d = list_entry(node, struct dentry, d_u.d_child);
  730. list_del_init(node);
  731. if (d->d_inode) {
  732. /* This should never be called on a cgroup
  733. * directory with child cgroups */
  734. BUG_ON(d->d_inode->i_mode & S_IFDIR);
  735. d = dget_locked(d);
  736. spin_unlock(&dcache_lock);
  737. d_delete(d);
  738. simple_unlink(dentry->d_inode, d);
  739. dput(d);
  740. spin_lock(&dcache_lock);
  741. }
  742. node = dentry->d_subdirs.next;
  743. }
  744. spin_unlock(&dcache_lock);
  745. }
  746. /*
  747. * NOTE : the dentry must have been dget()'ed
  748. */
  749. static void cgroup_d_remove_dir(struct dentry *dentry)
  750. {
  751. cgroup_clear_directory(dentry);
  752. spin_lock(&dcache_lock);
  753. list_del_init(&dentry->d_u.d_child);
  754. spin_unlock(&dcache_lock);
  755. remove_dir(dentry);
  756. }
  757. /*
  758. * A queue for waiters to do rmdir() cgroup. A tasks will sleep when
  759. * cgroup->count == 0 && list_empty(&cgroup->children) && subsys has some
  760. * reference to css->refcnt. In general, this refcnt is expected to goes down
  761. * to zero, soon.
  762. *
  763. * CGRP_WAIT_ON_RMDIR flag is set under cgroup's inode->i_mutex;
  764. */
  765. DECLARE_WAIT_QUEUE_HEAD(cgroup_rmdir_waitq);
  766. static void cgroup_wakeup_rmdir_waiter(struct cgroup *cgrp)
  767. {
  768. if (unlikely(test_and_clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags)))
  769. wake_up_all(&cgroup_rmdir_waitq);
  770. }
  771. void cgroup_exclude_rmdir(struct cgroup_subsys_state *css)
  772. {
  773. css_get(css);
  774. }
  775. void cgroup_release_and_wakeup_rmdir(struct cgroup_subsys_state *css)
  776. {
  777. cgroup_wakeup_rmdir_waiter(css->cgroup);
  778. css_put(css);
  779. }
  780. /*
  781. * Call with cgroup_mutex held. Drops reference counts on modules, including
  782. * any duplicate ones that parse_cgroupfs_options took. If this function
  783. * returns an error, no reference counts are touched.
  784. */
  785. static int rebind_subsystems(struct cgroupfs_root *root,
  786. unsigned long final_bits)
  787. {
  788. unsigned long added_bits, removed_bits;
  789. struct cgroup *cgrp = &root->top_cgroup;
  790. int i;
  791. BUG_ON(!mutex_is_locked(&cgroup_mutex));
  792. removed_bits = root->actual_subsys_bits & ~final_bits;
  793. added_bits = final_bits & ~root->actual_subsys_bits;
  794. /* Check that any added subsystems are currently free */
  795. for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
  796. unsigned long bit = 1UL << i;
  797. struct cgroup_subsys *ss = subsys[i];
  798. if (!(bit & added_bits))
  799. continue;
  800. /*
  801. * Nobody should tell us to do a subsys that doesn't exist:
  802. * parse_cgroupfs_options should catch that case and refcounts
  803. * ensure that subsystems won't disappear once selected.
  804. */
  805. BUG_ON(ss == NULL);
  806. if (ss->root != &rootnode) {
  807. /* Subsystem isn't free */
  808. return -EBUSY;
  809. }
  810. }
  811. /* Currently we don't handle adding/removing subsystems when
  812. * any child cgroups exist. This is theoretically supportable
  813. * but involves complex error handling, so it's being left until
  814. * later */
  815. if (root->number_of_cgroups > 1)
  816. return -EBUSY;
  817. /* Process each subsystem */
  818. for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
  819. struct cgroup_subsys *ss = subsys[i];
  820. unsigned long bit = 1UL << i;
  821. if (bit & added_bits) {
  822. /* We're binding this subsystem to this hierarchy */
  823. BUG_ON(ss == NULL);
  824. BUG_ON(cgrp->subsys[i]);
  825. BUG_ON(!dummytop->subsys[i]);
  826. BUG_ON(dummytop->subsys[i]->cgroup != dummytop);
  827. mutex_lock(&ss->hierarchy_mutex);
  828. cgrp->subsys[i] = dummytop->subsys[i];
  829. cgrp->subsys[i]->cgroup = cgrp;
  830. list_move(&ss->sibling, &root->subsys_list);
  831. ss->root = root;
  832. if (ss->bind)
  833. ss->bind(ss, cgrp);
  834. mutex_unlock(&ss->hierarchy_mutex);
  835. /* refcount was already taken, and we're keeping it */
  836. } else if (bit & removed_bits) {
  837. /* We're removing this subsystem */
  838. BUG_ON(ss == NULL);
  839. BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]);
  840. BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
  841. mutex_lock(&ss->hierarchy_mutex);
  842. if (ss->bind)
  843. ss->bind(ss, dummytop);
  844. dummytop->subsys[i]->cgroup = dummytop;
  845. cgrp->subsys[i] = NULL;
  846. subsys[i]->root = &rootnode;
  847. list_move(&ss->sibling, &rootnode.subsys_list);
  848. mutex_unlock(&ss->hierarchy_mutex);
  849. /* subsystem is now free - drop reference on module */
  850. module_put(ss->module);
  851. } else if (bit & final_bits) {
  852. /* Subsystem state should already exist */
  853. BUG_ON(ss == NULL);
  854. BUG_ON(!cgrp->subsys[i]);
  855. /*
  856. * a refcount was taken, but we already had one, so
  857. * drop the extra reference.
  858. */
  859. module_put(ss->module);
  860. #ifdef CONFIG_MODULE_UNLOAD
  861. BUG_ON(ss->module && !module_refcount(ss->module));
  862. #endif
  863. } else {
  864. /* Subsystem state shouldn't exist */
  865. BUG_ON(cgrp->subsys[i]);
  866. }
  867. }
  868. root->subsys_bits = root->actual_subsys_bits = final_bits;
  869. synchronize_rcu();
  870. return 0;
  871. }
  872. static int cgroup_show_options(struct seq_file *seq, struct vfsmount *vfs)
  873. {
  874. struct cgroupfs_root *root = vfs->mnt_sb->s_fs_info;
  875. struct cgroup_subsys *ss;
  876. mutex_lock(&cgroup_mutex);
  877. for_each_subsys(root, ss)
  878. seq_printf(seq, ",%s", ss->name);
  879. if (test_bit(ROOT_NOPREFIX, &root->flags))
  880. seq_puts(seq, ",noprefix");
  881. if (strlen(root->release_agent_path))
  882. seq_printf(seq, ",release_agent=%s", root->release_agent_path);
  883. if (strlen(root->name))
  884. seq_printf(seq, ",name=%s", root->name);
  885. mutex_unlock(&cgroup_mutex);
  886. return 0;
  887. }
  888. struct cgroup_sb_opts {
  889. unsigned long subsys_bits;
  890. unsigned long flags;
  891. char *release_agent;
  892. char *name;
  893. /* User explicitly requested empty subsystem */
  894. bool none;
  895. struct cgroupfs_root *new_root;
  896. };
  897. /*
  898. * Convert a hierarchy specifier into a bitmask of subsystems and flags. Call
  899. * with cgroup_mutex held to protect the subsys[] array. This function takes
  900. * refcounts on subsystems to be used, unless it returns error, in which case
  901. * no refcounts are taken.
  902. */
  903. static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
  904. {
  905. char *token, *o = data ?: "all";
  906. unsigned long mask = (unsigned long)-1;
  907. int i;
  908. bool module_pin_failed = false;
  909. BUG_ON(!mutex_is_locked(&cgroup_mutex));
  910. #ifdef CONFIG_CPUSETS
  911. mask = ~(1UL << cpuset_subsys_id);
  912. #endif
  913. memset(opts, 0, sizeof(*opts));
  914. while ((token = strsep(&o, ",")) != NULL) {
  915. if (!*token)
  916. return -EINVAL;
  917. if (!strcmp(token, "all")) {
  918. /* Add all non-disabled subsystems */
  919. opts->subsys_bits = 0;
  920. for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
  921. struct cgroup_subsys *ss = subsys[i];
  922. if (ss == NULL)
  923. continue;
  924. if (!ss->disabled)
  925. opts->subsys_bits |= 1ul << i;
  926. }
  927. } else if (!strcmp(token, "none")) {
  928. /* Explicitly have no subsystems */
  929. opts->none = true;
  930. } else if (!strcmp(token, "noprefix")) {
  931. set_bit(ROOT_NOPREFIX, &opts->flags);
  932. } else if (!strncmp(token, "release_agent=", 14)) {
  933. /* Specifying two release agents is forbidden */
  934. if (opts->release_agent)
  935. return -EINVAL;
  936. opts->release_agent =
  937. kstrndup(token + 14, PATH_MAX, GFP_KERNEL);
  938. if (!opts->release_agent)
  939. return -ENOMEM;
  940. } else if (!strncmp(token, "name=", 5)) {
  941. const char *name = token + 5;
  942. /* Can't specify an empty name */
  943. if (!strlen(name))
  944. return -EINVAL;
  945. /* Must match [\w.-]+ */
  946. for (i = 0; i < strlen(name); i++) {
  947. char c = name[i];
  948. if (isalnum(c))
  949. continue;
  950. if ((c == '.') || (c == '-') || (c == '_'))
  951. continue;
  952. return -EINVAL;
  953. }
  954. /* Specifying two names is forbidden */
  955. if (opts->name)
  956. return -EINVAL;
  957. opts->name = kstrndup(name,
  958. MAX_CGROUP_ROOT_NAMELEN,
  959. GFP_KERNEL);
  960. if (!opts->name)
  961. return -ENOMEM;
  962. } else {
  963. struct cgroup_subsys *ss;
  964. for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
  965. ss = subsys[i];
  966. if (ss == NULL)
  967. continue;
  968. if (!strcmp(token, ss->name)) {
  969. if (!ss->disabled)
  970. set_bit(i, &opts->subsys_bits);
  971. break;
  972. }
  973. }
  974. if (i == CGROUP_SUBSYS_COUNT)
  975. return -ENOENT;
  976. }
  977. }
  978. /* Consistency checks */
  979. /*
  980. * Option noprefix was introduced just for backward compatibility
  981. * with the old cpuset, so we allow noprefix only if mounting just
  982. * the cpuset subsystem.
  983. */
  984. if (test_bit(ROOT_NOPREFIX, &opts->flags) &&
  985. (opts->subsys_bits & mask))
  986. return -EINVAL;
  987. /* Can't specify "none" and some subsystems */
  988. if (opts->subsys_bits && opts->none)
  989. return -EINVAL;
  990. /*
  991. * We either have to specify by name or by subsystems. (So all
  992. * empty hierarchies must have a name).
  993. */
  994. if (!opts->subsys_bits && !opts->name)
  995. return -EINVAL;
  996. /*
  997. * Grab references on all the modules we'll need, so the subsystems
  998. * don't dance around before rebind_subsystems attaches them. This may
  999. * take duplicate reference counts on a subsystem that's already used,
  1000. * but rebind_subsystems handles this case.
  1001. */
  1002. for (i = CGROUP_BUILTIN_SUBSYS_COUNT; i < CGROUP_SUBSYS_COUNT; i++) {
  1003. unsigned long bit = 1UL << i;
  1004. if (!(bit & opts->subsys_bits))
  1005. continue;
  1006. if (!try_module_get(subsys[i]->module)) {
  1007. module_pin_failed = true;
  1008. break;
  1009. }
  1010. }
  1011. if (module_pin_failed) {
  1012. /*
  1013. * oops, one of the modules was going away. this means that we
  1014. * raced with a module_delete call, and to the user this is
  1015. * essentially a "subsystem doesn't exist" case.
  1016. */
  1017. for (i--; i >= CGROUP_BUILTIN_SUBSYS_COUNT; i--) {
  1018. /* drop refcounts only on the ones we took */
  1019. unsigned long bit = 1UL << i;
  1020. if (!(bit & opts->subsys_bits))
  1021. continue;
  1022. module_put(subsys[i]->module);
  1023. }
  1024. return -ENOENT;
  1025. }
  1026. return 0;
  1027. }
  1028. static void drop_parsed_module_refcounts(unsigned long subsys_bits)
  1029. {
  1030. int i;
  1031. for (i = CGROUP_BUILTIN_SUBSYS_COUNT; i < CGROUP_SUBSYS_COUNT; i++) {
  1032. unsigned long bit = 1UL << i;
  1033. if (!(bit & subsys_bits))
  1034. continue;
  1035. module_put(subsys[i]->module);
  1036. }
  1037. }
  1038. static int cgroup_remount(struct super_block *sb, int *flags, char *data)
  1039. {
  1040. int ret = 0;
  1041. struct cgroupfs_root *root = sb->s_fs_info;
  1042. struct cgroup *cgrp = &root->top_cgroup;
  1043. struct cgroup_sb_opts opts;
  1044. lock_kernel();
  1045. mutex_lock(&cgrp->dentry->d_inode->i_mutex);
  1046. mutex_lock(&cgroup_mutex);
  1047. /* See what subsystems are wanted */
  1048. ret = parse_cgroupfs_options(data, &opts);
  1049. if (ret)
  1050. goto out_unlock;
  1051. /* Don't allow flags or name to change at remount */
  1052. if (opts.flags != root->flags ||
  1053. (opts.name && strcmp(opts.name, root->name))) {
  1054. ret = -EINVAL;
  1055. drop_parsed_module_refcounts(opts.subsys_bits);
  1056. goto out_unlock;
  1057. }
  1058. ret = rebind_subsystems(root, opts.subsys_bits);
  1059. if (ret) {
  1060. drop_parsed_module_refcounts(opts.subsys_bits);
  1061. goto out_unlock;
  1062. }
  1063. /* (re)populate subsystem files */
  1064. cgroup_populate_dir(cgrp);
  1065. if (opts.release_agent)
  1066. strcpy(root->release_agent_path, opts.release_agent);
  1067. out_unlock:
  1068. kfree(opts.release_agent);
  1069. kfree(opts.name);
  1070. mutex_unlock(&cgroup_mutex);
  1071. mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
  1072. unlock_kernel();
  1073. return ret;
  1074. }
  1075. static const struct super_operations cgroup_ops = {
  1076. .statfs = simple_statfs,
  1077. .drop_inode = generic_delete_inode,
  1078. .show_options = cgroup_show_options,
  1079. .remount_fs = cgroup_remount,
  1080. };
  1081. static void init_cgroup_housekeeping(struct cgroup *cgrp)
  1082. {
  1083. INIT_LIST_HEAD(&cgrp->sibling);
  1084. INIT_LIST_HEAD(&cgrp->children);
  1085. INIT_LIST_HEAD(&cgrp->css_sets);
  1086. INIT_LIST_HEAD(&cgrp->release_list);
  1087. INIT_LIST_HEAD(&cgrp->pidlists);
  1088. mutex_init(&cgrp->pidlist_mutex);
  1089. }
  1090. static void init_cgroup_root(struct cgroupfs_root *root)
  1091. {
  1092. struct cgroup *cgrp = &root->top_cgroup;
  1093. INIT_LIST_HEAD(&root->subsys_list);
  1094. INIT_LIST_HEAD(&root->root_list);
  1095. root->number_of_cgroups = 1;
  1096. cgrp->root = root;
  1097. cgrp->top_cgroup = cgrp;
  1098. init_cgroup_housekeeping(cgrp);
  1099. }
  1100. static bool init_root_id(struct cgroupfs_root *root)
  1101. {
  1102. int ret = 0;
  1103. do {
  1104. if (!ida_pre_get(&hierarchy_ida, GFP_KERNEL))
  1105. return false;
  1106. spin_lock(&hierarchy_id_lock);
  1107. /* Try to allocate the next unused ID */
  1108. ret = ida_get_new_above(&hierarchy_ida, next_hierarchy_id,
  1109. &root->hierarchy_id);
  1110. if (ret == -ENOSPC)
  1111. /* Try again starting from 0 */
  1112. ret = ida_get_new(&hierarchy_ida, &root->hierarchy_id);
  1113. if (!ret) {
  1114. next_hierarchy_id = root->hierarchy_id + 1;
  1115. } else if (ret != -EAGAIN) {
  1116. /* Can only get here if the 31-bit IDR is full ... */
  1117. BUG_ON(ret);
  1118. }
  1119. spin_unlock(&hierarchy_id_lock);
  1120. } while (ret);
  1121. return true;
  1122. }
  1123. static int cgroup_test_super(struct super_block *sb, void *data)
  1124. {
  1125. struct cgroup_sb_opts *opts = data;
  1126. struct cgroupfs_root *root = sb->s_fs_info;
  1127. /* If we asked for a name then it must match */
  1128. if (opts->name && strcmp(opts->name, root->name))
  1129. return 0;
  1130. /*
  1131. * If we asked for subsystems (or explicitly for no
  1132. * subsystems) then they must match
  1133. */
  1134. if ((opts->subsys_bits || opts->none)
  1135. && (opts->subsys_bits != root->subsys_bits))
  1136. return 0;
  1137. return 1;
  1138. }
  1139. static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
  1140. {
  1141. struct cgroupfs_root *root;
  1142. if (!opts->subsys_bits && !opts->none)
  1143. return NULL;
  1144. root = kzalloc(sizeof(*root), GFP_KERNEL);
  1145. if (!root)
  1146. return ERR_PTR(-ENOMEM);
  1147. if (!init_root_id(root)) {
  1148. kfree(root);
  1149. return ERR_PTR(-ENOMEM);
  1150. }
  1151. init_cgroup_root(root);
  1152. root->subsys_bits = opts->subsys_bits;
  1153. root->flags = opts->flags;
  1154. if (opts->release_agent)
  1155. strcpy(root->release_agent_path, opts->release_agent);
  1156. if (opts->name)
  1157. strcpy(root->name, opts->name);
  1158. return root;
  1159. }
  1160. static void cgroup_drop_root(struct cgroupfs_root *root)
  1161. {
  1162. if (!root)
  1163. return;
  1164. BUG_ON(!root->hierarchy_id);
  1165. spin_lock(&hierarchy_id_lock);
  1166. ida_remove(&hierarchy_ida, root->hierarchy_id);
  1167. spin_unlock(&hierarchy_id_lock);
  1168. kfree(root);
  1169. }
  1170. static int cgroup_set_super(struct super_block *sb, void *data)
  1171. {
  1172. int ret;
  1173. struct cgroup_sb_opts *opts = data;
  1174. /* If we don't have a new root, we can't set up a new sb */
  1175. if (!opts->new_root)
  1176. return -EINVAL;
  1177. BUG_ON(!opts->subsys_bits && !opts->none);
  1178. ret = set_anon_super(sb, NULL);
  1179. if (ret)
  1180. return ret;
  1181. sb->s_fs_info = opts->new_root;
  1182. opts->new_root->sb = sb;
  1183. sb->s_blocksize = PAGE_CACHE_SIZE;
  1184. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  1185. sb->s_magic = CGROUP_SUPER_MAGIC;
  1186. sb->s_op = &cgroup_ops;
  1187. return 0;
  1188. }
  1189. static int cgroup_get_rootdir(struct super_block *sb)
  1190. {
  1191. struct inode *inode =
  1192. cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
  1193. struct dentry *dentry;
  1194. if (!inode)
  1195. return -ENOMEM;
  1196. inode->i_fop = &simple_dir_operations;
  1197. inode->i_op = &cgroup_dir_inode_operations;
  1198. /* directories start off with i_nlink == 2 (for "." entry) */
  1199. inc_nlink(inode);
  1200. dentry = d_alloc_root(inode);
  1201. if (!dentry) {
  1202. iput(inode);
  1203. return -ENOMEM;
  1204. }
  1205. sb->s_root = dentry;
  1206. return 0;
  1207. }
  1208. static int cgroup_get_sb(struct file_system_type *fs_type,
  1209. int flags, const char *unused_dev_name,
  1210. void *data, struct vfsmount *mnt)
  1211. {
  1212. struct cgroup_sb_opts opts;
  1213. struct cgroupfs_root *root;
  1214. int ret = 0;
  1215. struct super_block *sb;
  1216. struct cgroupfs_root *new_root;
  1217. /* First find the desired set of subsystems */
  1218. mutex_lock(&cgroup_mutex);
  1219. ret = parse_cgroupfs_options(data, &opts);
  1220. mutex_unlock(&cgroup_mutex);
  1221. if (ret)
  1222. goto out_err;
  1223. /*
  1224. * Allocate a new cgroup root. We may not need it if we're
  1225. * reusing an existing hierarchy.
  1226. */
  1227. new_root = cgroup_root_from_opts(&opts);
  1228. if (IS_ERR(new_root)) {
  1229. ret = PTR_ERR(new_root);
  1230. goto drop_modules;
  1231. }
  1232. opts.new_root = new_root;
  1233. /* Locate an existing or new sb for this hierarchy */
  1234. sb = sget(fs_type, cgroup_test_super, cgroup_set_super, &opts);
  1235. if (IS_ERR(sb)) {
  1236. ret = PTR_ERR(sb);
  1237. cgroup_drop_root(opts.new_root);
  1238. goto drop_modules;
  1239. }
  1240. root = sb->s_fs_info;
  1241. BUG_ON(!root);
  1242. if (root == opts.new_root) {
  1243. /* We used the new root structure, so this is a new hierarchy */
  1244. struct list_head tmp_cg_links;
  1245. struct cgroup *root_cgrp = &root->top_cgroup;
  1246. struct inode *inode;
  1247. struct cgroupfs_root *existing_root;
  1248. int i;
  1249. BUG_ON(sb->s_root != NULL);
  1250. ret = cgroup_get_rootdir(sb);
  1251. if (ret)
  1252. goto drop_new_super;
  1253. inode = sb->s_root->d_inode;
  1254. mutex_lock(&inode->i_mutex);
  1255. mutex_lock(&cgroup_mutex);
  1256. if (strlen(root->name)) {
  1257. /* Check for name clashes with existing mounts */
  1258. for_each_active_root(existing_root) {
  1259. if (!strcmp(existing_root->name, root->name)) {
  1260. ret = -EBUSY;
  1261. mutex_unlock(&cgroup_mutex);
  1262. mutex_unlock(&inode->i_mutex);
  1263. goto drop_new_super;
  1264. }
  1265. }
  1266. }
  1267. /*
  1268. * We're accessing css_set_count without locking
  1269. * css_set_lock here, but that's OK - it can only be
  1270. * increased by someone holding cgroup_lock, and
  1271. * that's us. The worst that can happen is that we
  1272. * have some link structures left over
  1273. */
  1274. ret = allocate_cg_links(css_set_count, &tmp_cg_links);
  1275. if (ret) {
  1276. mutex_unlock(&cgroup_mutex);
  1277. mutex_unlock(&inode->i_mutex);
  1278. goto drop_new_super;
  1279. }
  1280. ret = rebind_subsystems(root, root->subsys_bits);
  1281. if (ret == -EBUSY) {
  1282. mutex_unlock(&cgroup_mutex);
  1283. mutex_unlock(&inode->i_mutex);
  1284. free_cg_links(&tmp_cg_links);
  1285. goto drop_new_super;
  1286. }
  1287. /*
  1288. * There must be no failure case after here, since rebinding
  1289. * takes care of subsystems' refcounts, which are explicitly
  1290. * dropped in the failure exit path.
  1291. */
  1292. /* EBUSY should be the only error here */
  1293. BUG_ON(ret);
  1294. list_add(&root->root_list, &roots);
  1295. root_count++;
  1296. sb->s_root->d_fsdata = root_cgrp;
  1297. root->top_cgroup.dentry = sb->s_root;
  1298. /* Link the top cgroup in this hierarchy into all
  1299. * the css_set objects */
  1300. write_lock(&css_set_lock);
  1301. for (i = 0; i < CSS_SET_TABLE_SIZE; i++) {
  1302. struct hlist_head *hhead = &css_set_table[i];
  1303. struct hlist_node *node;
  1304. struct css_set *cg;
  1305. hlist_for_each_entry(cg, node, hhead, hlist)
  1306. link_css_set(&tmp_cg_links, cg, root_cgrp);
  1307. }
  1308. write_unlock(&css_set_lock);
  1309. free_cg_links(&tmp_cg_links);
  1310. BUG_ON(!list_empty(&root_cgrp->sibling));
  1311. BUG_ON(!list_empty(&root_cgrp->children));
  1312. BUG_ON(root->number_of_cgroups != 1);
  1313. cgroup_populate_dir(root_cgrp);
  1314. mutex_unlock(&cgroup_mutex);
  1315. mutex_unlock(&inode->i_mutex);
  1316. } else {
  1317. /*
  1318. * We re-used an existing hierarchy - the new root (if
  1319. * any) is not needed
  1320. */
  1321. cgroup_drop_root(opts.new_root);
  1322. /* no subsys rebinding, so refcounts don't change */
  1323. drop_parsed_module_refcounts(opts.subsys_bits);
  1324. }
  1325. simple_set_mnt(mnt, sb);
  1326. kfree(opts.release_agent);
  1327. kfree(opts.name);
  1328. return 0;
  1329. drop_new_super:
  1330. deactivate_locked_super(sb);
  1331. drop_modules:
  1332. drop_parsed_module_refcounts(opts.subsys_bits);
  1333. out_err:
  1334. kfree(opts.release_agent);
  1335. kfree(opts.name);
  1336. return ret;
  1337. }
  1338. static void cgroup_kill_sb(struct super_block *sb) {
  1339. struct cgroupfs_root *root = sb->s_fs_info;
  1340. struct cgroup *cgrp = &root->top_cgroup;
  1341. int ret;
  1342. struct cg_cgroup_link *link;
  1343. struct cg_cgroup_link *saved_link;
  1344. BUG_ON(!root);
  1345. BUG_ON(root->number_of_cgroups != 1);
  1346. BUG_ON(!list_empty(&cgrp->children));
  1347. BUG_ON(!list_empty(&cgrp->sibling));
  1348. mutex_lock(&cgroup_mutex);
  1349. /* Rebind all subsystems back to the default hierarchy */
  1350. ret = rebind_subsystems(root, 0);
  1351. /* Shouldn't be able to fail ... */
  1352. BUG_ON(ret);
  1353. /*
  1354. * Release all the links from css_sets to this hierarchy's
  1355. * root cgroup
  1356. */
  1357. write_lock(&css_set_lock);
  1358. list_for_each_entry_safe(link, saved_link, &cgrp->css_sets,
  1359. cgrp_link_list) {
  1360. list_del(&link->cg_link_list);
  1361. list_del(&link->cgrp_link_list);
  1362. kfree(link);
  1363. }
  1364. write_unlock(&css_set_lock);
  1365. if (!list_empty(&root->root_list)) {
  1366. list_del(&root->root_list);
  1367. root_count--;
  1368. }
  1369. mutex_unlock(&cgroup_mutex);
  1370. kill_litter_super(sb);
  1371. cgroup_drop_root(root);
  1372. }
  1373. static struct file_system_type cgroup_fs_type = {
  1374. .name = "cgroup",
  1375. .get_sb = cgroup_get_sb,
  1376. .kill_sb = cgroup_kill_sb,
  1377. };
  1378. static inline struct cgroup *__d_cgrp(struct dentry *dentry)
  1379. {
  1380. return dentry->d_fsdata;
  1381. }
  1382. static inline struct cftype *__d_cft(struct dentry *dentry)
  1383. {
  1384. return dentry->d_fsdata;
  1385. }
  1386. /**
  1387. * cgroup_path - generate the path of a cgroup
  1388. * @cgrp: the cgroup in question
  1389. * @buf: the buffer to write the path into
  1390. * @buflen: the length of the buffer
  1391. *
  1392. * Called with cgroup_mutex held or else with an RCU-protected cgroup
  1393. * reference. Writes path of cgroup into buf. Returns 0 on success,
  1394. * -errno on error.
  1395. */
  1396. int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
  1397. {
  1398. char *start;
  1399. struct dentry *dentry = rcu_dereference(cgrp->dentry);
  1400. if (!dentry || cgrp == dummytop) {
  1401. /*
  1402. * Inactive subsystems have no dentry for their root
  1403. * cgroup
  1404. */
  1405. strcpy(buf, "/");
  1406. return 0;
  1407. }
  1408. start = buf + buflen;
  1409. *--start = '\0';
  1410. for (;;) {
  1411. int len = dentry->d_name.len;
  1412. if ((start -= len) < buf)
  1413. return -ENAMETOOLONG;
  1414. memcpy(start, cgrp->dentry->d_name.name, len);
  1415. cgrp = cgrp->parent;
  1416. if (!cgrp)
  1417. break;
  1418. dentry = rcu_dereference(cgrp->dentry);
  1419. if (!cgrp->parent)
  1420. continue;
  1421. if (--start < buf)
  1422. return -ENAMETOOLONG;
  1423. *start = '/';
  1424. }
  1425. memmove(buf, start, buf + buflen - start);
  1426. return 0;
  1427. }
  1428. /**
  1429. * cgroup_attach_task - attach task 'tsk' to cgroup 'cgrp'
  1430. * @cgrp: the cgroup the task is attaching to
  1431. * @tsk: the task to be attached
  1432. *
  1433. * Call holding cgroup_mutex. May take task_lock of
  1434. * the task 'tsk' during call.
  1435. */
  1436. int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
  1437. {
  1438. int retval = 0;
  1439. struct cgroup_subsys *ss, *failed_ss = NULL;
  1440. struct cgroup *oldcgrp;
  1441. struct css_set *cg;
  1442. struct css_set *newcg;
  1443. struct cgroupfs_root *root = cgrp->root;
  1444. /* Nothing to do if the task is already in that cgroup */
  1445. oldcgrp = task_cgroup_from_root(tsk, root);
  1446. if (cgrp == oldcgrp)
  1447. return 0;
  1448. for_each_subsys(root, ss) {
  1449. if (ss->can_attach) {
  1450. retval = ss->can_attach(ss, cgrp, tsk, false);
  1451. if (retval) {
  1452. /*
  1453. * Remember on which subsystem the can_attach()
  1454. * failed, so that we only call cancel_attach()
  1455. * against the subsystems whose can_attach()
  1456. * succeeded. (See below)
  1457. */
  1458. failed_ss = ss;
  1459. goto out;
  1460. }
  1461. }
  1462. }
  1463. task_lock(tsk);
  1464. cg = tsk->cgroups;
  1465. get_css_set(cg);
  1466. task_unlock(tsk);
  1467. /*
  1468. * Locate or allocate a new css_set for this task,
  1469. * based on its final set of cgroups
  1470. */
  1471. newcg = find_css_set(cg, cgrp);
  1472. put_css_set(cg);
  1473. if (!newcg) {
  1474. retval = -ENOMEM;
  1475. goto out;
  1476. }
  1477. task_lock(tsk);
  1478. if (tsk->flags & PF_EXITING) {
  1479. task_unlock(tsk);
  1480. put_css_set(newcg);
  1481. retval = -ESRCH;
  1482. goto out;
  1483. }
  1484. rcu_assign_pointer(tsk->cgroups, newcg);
  1485. task_unlock(tsk);
  1486. /* Update the css_set linked lists if we're using them */
  1487. write_lock(&css_set_lock);
  1488. if (!list_empty(&tsk->cg_list)) {
  1489. list_del(&tsk->cg_list);
  1490. list_add(&tsk->cg_list, &newcg->tasks);
  1491. }
  1492. write_unlock(&css_set_lock);
  1493. for_each_subsys(root, ss) {
  1494. if (ss->attach)
  1495. ss->attach(ss, cgrp, oldcgrp, tsk, false);
  1496. }
  1497. set_bit(CGRP_RELEASABLE, &oldcgrp->flags);
  1498. synchronize_rcu();
  1499. put_css_set(cg);
  1500. /*
  1501. * wake up rmdir() waiter. the rmdir should fail since the cgroup
  1502. * is no longer empty.
  1503. */
  1504. cgroup_wakeup_rmdir_waiter(cgrp);
  1505. out:
  1506. if (retval) {
  1507. for_each_subsys(root, ss) {
  1508. if (ss == failed_ss)
  1509. /*
  1510. * This subsystem was the one that failed the
  1511. * can_attach() check earlier, so we don't need
  1512. * to call cancel_attach() against it or any
  1513. * remaining subsystems.
  1514. */
  1515. break;
  1516. if (ss->cancel_attach)
  1517. ss->cancel_attach(ss, cgrp, tsk, false);
  1518. }
  1519. }
  1520. return retval;
  1521. }
  1522. /*
  1523. * Attach task with pid 'pid' to cgroup 'cgrp'. Call with cgroup_mutex
  1524. * held. May take task_lock of task
  1525. */
  1526. static int attach_task_by_pid(struct cgroup *cgrp, u64 pid)
  1527. {
  1528. struct task_struct *tsk;
  1529. const struct cred *cred = current_cred(), *tcred;
  1530. int ret;
  1531. if (pid) {
  1532. rcu_read_lock();
  1533. tsk = find_task_by_vpid(pid);
  1534. if (!tsk || tsk->flags & PF_EXITING) {
  1535. rcu_read_unlock();
  1536. return -ESRCH;
  1537. }
  1538. tcred = __task_cred(tsk);
  1539. if (cred->euid &&
  1540. cred->euid != tcred->uid &&
  1541. cred->euid != tcred->suid) {
  1542. rcu_read_unlock();
  1543. return -EACCES;
  1544. }
  1545. get_task_struct(tsk);
  1546. rcu_read_unlock();
  1547. } else {
  1548. tsk = current;
  1549. get_task_struct(tsk);
  1550. }
  1551. ret = cgroup_attach_task(cgrp, tsk);
  1552. put_task_struct(tsk);
  1553. return ret;
  1554. }
  1555. static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid)
  1556. {
  1557. int ret;
  1558. if (!cgroup_lock_live_group(cgrp))
  1559. return -ENODEV;
  1560. ret = attach_task_by_pid(cgrp, pid);
  1561. cgroup_unlock();
  1562. return ret;
  1563. }
  1564. /**
  1565. * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
  1566. * @cgrp: the cgroup to be checked for liveness
  1567. *
  1568. * On success, returns true; the lock should be later released with
  1569. * cgroup_unlock(). On failure returns false with no lock held.
  1570. */
  1571. bool cgroup_lock_live_group(struct cgroup *cgrp)
  1572. {
  1573. mutex_lock(&cgroup_mutex);
  1574. if (cgroup_is_removed(cgrp)) {
  1575. mutex_unlock(&cgroup_mutex);
  1576. return false;
  1577. }
  1578. return true;
  1579. }
  1580. static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft,
  1581. const char *buffer)
  1582. {
  1583. BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
  1584. if (!cgroup_lock_live_group(cgrp))
  1585. return -ENODEV;
  1586. strcpy(cgrp->root->release_agent_path, buffer);
  1587. cgroup_unlock();
  1588. return 0;
  1589. }
  1590. static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft,
  1591. struct seq_file *seq)
  1592. {
  1593. if (!cgroup_lock_live_group(cgrp))
  1594. return -ENODEV;
  1595. seq_puts(seq, cgrp->root->release_agent_path);
  1596. seq_putc(seq, '\n');
  1597. cgroup_unlock();
  1598. return 0;
  1599. }
  1600. /* A buffer size big enough for numbers or short strings */
  1601. #define CGROUP_LOCAL_BUFFER_SIZE 64
  1602. static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
  1603. struct file *file,
  1604. const char __user *userbuf,
  1605. size_t nbytes, loff_t *unused_ppos)
  1606. {
  1607. char buffer[CGROUP_LOCAL_BUFFER_SIZE];
  1608. int retval = 0;
  1609. char *end;
  1610. if (!nbytes)
  1611. return -EINVAL;
  1612. if (nbytes >= sizeof(buffer))
  1613. return -E2BIG;
  1614. if (copy_from_user(buffer, userbuf, nbytes))
  1615. return -EFAULT;
  1616. buffer[nbytes] = 0; /* nul-terminate */
  1617. if (cft->write_u64) {
  1618. u64 val = simple_strtoull(strstrip(buffer), &end, 0);
  1619. if (*end)
  1620. return -EINVAL;
  1621. retval = cft->write_u64(cgrp, cft, val);
  1622. } else {
  1623. s64 val = simple_strtoll(strstrip(buffer), &end, 0);
  1624. if (*end)
  1625. return -EINVAL;
  1626. retval = cft->write_s64(cgrp, cft, val);
  1627. }
  1628. if (!retval)
  1629. retval = nbytes;
  1630. return retval;
  1631. }
  1632. static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft,
  1633. struct file *file,
  1634. const char __user *userbuf,
  1635. size_t nbytes, loff_t *unused_ppos)
  1636. {
  1637. char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
  1638. int retval = 0;
  1639. size_t max_bytes = cft->max_write_len;
  1640. char *buffer = local_buffer;
  1641. if (!max_bytes)
  1642. max_bytes = sizeof(local_buffer) - 1;
  1643. if (nbytes >= max_bytes)
  1644. return -E2BIG;
  1645. /* Allocate a dynamic buffer if we need one */
  1646. if (nbytes >= sizeof(local_buffer)) {
  1647. buffer = kmalloc(nbytes + 1, GFP_KERNEL);
  1648. if (buffer == NULL)
  1649. return -ENOMEM;
  1650. }
  1651. if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
  1652. retval = -EFAULT;
  1653. goto out;
  1654. }
  1655. buffer[nbytes] = 0; /* nul-terminate */
  1656. retval = cft->write_string(cgrp, cft, strstrip(buffer));
  1657. if (!retval)
  1658. retval = nbytes;
  1659. out:
  1660. if (buffer != local_buffer)
  1661. kfree(buffer);
  1662. return retval;
  1663. }
  1664. static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
  1665. size_t nbytes, loff_t *ppos)
  1666. {
  1667. struct cftype *cft = __d_cft(file->f_dentry);
  1668. struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
  1669. if (cgroup_is_removed(cgrp))
  1670. return -ENODEV;
  1671. if (cft->write)
  1672. return cft->write(cgrp, cft, file, buf, nbytes, ppos);
  1673. if (cft->write_u64 || cft->write_s64)
  1674. return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
  1675. if (cft->write_string)
  1676. return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos);
  1677. if (cft->trigger) {
  1678. int ret = cft->trigger(cgrp, (unsigned int)cft->private);
  1679. return ret ? ret : nbytes;
  1680. }
  1681. return -EINVAL;
  1682. }
  1683. static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft,
  1684. struct file *file,
  1685. char __user *buf, size_t nbytes,
  1686. loff_t *ppos)
  1687. {
  1688. char tmp[CGROUP_LOCAL_BUFFER_SIZE];
  1689. u64 val = cft->read_u64(cgrp, cft);
  1690. int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
  1691. return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
  1692. }
  1693. static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft,
  1694. struct file *file,
  1695. char __user *buf, size_t nbytes,
  1696. loff_t *ppos)
  1697. {
  1698. char tmp[CGROUP_LOCAL_BUFFER_SIZE];
  1699. s64 val = cft->read_s64(cgrp, cft);
  1700. int len = sprintf(tmp, "%lld\n", (long long) val);
  1701. return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
  1702. }
  1703. static ssize_t cgroup_file_read(struct file *file, char __user *buf,
  1704. size_t nbytes, loff_t *ppos)
  1705. {
  1706. struct cftype *cft = __d_cft(file->f_dentry);
  1707. struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
  1708. if (cgroup_is_removed(cgrp))
  1709. return -ENODEV;
  1710. if (cft->read)
  1711. return cft->read(cgrp, cft, file, buf, nbytes, ppos);
  1712. if (cft->read_u64)
  1713. return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
  1714. if (cft->read_s64)
  1715. return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
  1716. return -EINVAL;
  1717. }
  1718. /*
  1719. * seqfile ops/methods for returning structured data. Currently just
  1720. * supports string->u64 maps, but can be extended in future.
  1721. */
  1722. struct cgroup_seqfile_state {
  1723. struct cftype *cft;
  1724. struct cgroup *cgroup;
  1725. };
  1726. static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
  1727. {
  1728. struct seq_file *sf = cb->state;
  1729. return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
  1730. }
  1731. static int cgroup_seqfile_show(struct seq_file *m, void *arg)
  1732. {
  1733. struct cgroup_seqfile_state *state = m->private;
  1734. struct cftype *cft = state->cft;
  1735. if (cft->read_map) {
  1736. struct cgroup_map_cb cb = {
  1737. .fill = cgroup_map_add,
  1738. .state = m,
  1739. };
  1740. return cft->read_map(state->cgroup, cft, &cb);
  1741. }
  1742. return cft->read_seq_string(state->cgroup, cft, m);
  1743. }
  1744. static int cgroup_seqfile_release(struct inode *inode, struct file *file)
  1745. {
  1746. struct seq_file *seq = file->private_data;
  1747. kfree(seq->private);
  1748. return single_release(inode, file);
  1749. }
  1750. static const struct file_operations cgroup_seqfile_operations = {
  1751. .read = seq_read,
  1752. .write = cgroup_file_write,
  1753. .llseek = seq_lseek,
  1754. .release = cgroup_seqfile_release,
  1755. };
  1756. static int cgroup_file_open(struct inode *inode, struct file *file)
  1757. {
  1758. int err;
  1759. struct cftype *cft;
  1760. err = generic_file_open(inode, file);
  1761. if (err)
  1762. return err;
  1763. cft = __d_cft(file->f_dentry);
  1764. if (cft->read_map || cft->read_seq_string) {
  1765. struct cgroup_seqfile_state *state =
  1766. kzalloc(sizeof(*state), GFP_USER);
  1767. if (!state)
  1768. return -ENOMEM;
  1769. state->cft = cft;
  1770. state->cgroup = __d_cgrp(file->f_dentry->d_parent);
  1771. file->f_op = &cgroup_seqfile_operations;
  1772. err = single_open(file, cgroup_seqfile_show, state);
  1773. if (err < 0)
  1774. kfree(state);
  1775. } else if (cft->open)
  1776. err = cft->open(inode, file);
  1777. else
  1778. err = 0;
  1779. return err;
  1780. }
  1781. static int cgroup_file_release(struct inode *inode, struct file *file)
  1782. {
  1783. struct cftype *cft = __d_cft(file->f_dentry);
  1784. if (cft->release)
  1785. return cft->release(inode, file);
  1786. return 0;
  1787. }
  1788. /*
  1789. * cgroup_rename - Only allow simple rename of directories in place.
  1790. */
  1791. static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
  1792. struct inode *new_dir, struct dentry *new_dentry)
  1793. {
  1794. if (!S_ISDIR(old_dentry->d_inode->i_mode))
  1795. return -ENOTDIR;
  1796. if (new_dentry->d_inode)
  1797. return -EEXIST;
  1798. if (old_dir != new_dir)
  1799. return -EIO;
  1800. return simple_rename(old_dir, old_dentry, new_dir, new_dentry);
  1801. }
  1802. static const struct file_operations cgroup_file_operations = {
  1803. .read = cgroup_file_read,
  1804. .write = cgroup_file_write,
  1805. .llseek = generic_file_llseek,
  1806. .open = cgroup_file_open,
  1807. .release = cgroup_file_release,
  1808. };
  1809. static const struct inode_operations cgroup_dir_inode_operations = {
  1810. .lookup = simple_lookup,
  1811. .mkdir = cgroup_mkdir,
  1812. .rmdir = cgroup_rmdir,
  1813. .rename = cgroup_rename,
  1814. };
  1815. static int cgroup_create_file(struct dentry *dentry, mode_t mode,
  1816. struct super_block *sb)
  1817. {
  1818. static const struct dentry_operations cgroup_dops = {
  1819. .d_iput = cgroup_diput,
  1820. };
  1821. struct inode *inode;
  1822. if (!dentry)
  1823. return -ENOENT;
  1824. if (dentry->d_inode)
  1825. return -EEXIST;
  1826. inode = cgroup_new_inode(mode, sb);
  1827. if (!inode)
  1828. return -ENOMEM;
  1829. if (S_ISDIR(mode)) {
  1830. inode->i_op = &cgroup_dir_inode_operations;
  1831. inode->i_fop = &simple_dir_operations;
  1832. /* start off with i_nlink == 2 (for "." entry) */
  1833. inc_nlink(inode);
  1834. /* start with the directory inode held, so that we can
  1835. * populate it without racing with another mkdir */
  1836. mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
  1837. } else if (S_ISREG(mode)) {
  1838. inode->i_size = 0;
  1839. inode->i_fop = &cgroup_file_operations;
  1840. }
  1841. dentry->d_op = &cgroup_dops;
  1842. d_instantiate(dentry, inode);
  1843. dget(dentry); /* Extra count - pin the dentry in core */
  1844. return 0;
  1845. }
  1846. /*
  1847. * cgroup_create_dir - create a directory for an object.
  1848. * @cgrp: the cgroup we create the directory for. It must have a valid
  1849. * ->parent field. And we are going to fill its ->dentry field.
  1850. * @dentry: dentry of the new cgroup
  1851. * @mode: mode to set on new directory.
  1852. */
  1853. static int cgroup_create_dir(struct cgroup *cgrp, struct dentry *dentry,
  1854. mode_t mode)
  1855. {
  1856. struct dentry *parent;
  1857. int error = 0;
  1858. parent = cgrp->parent->dentry;
  1859. error = cgroup_create_file(dentry, S_IFDIR | mode, cgrp->root->sb);
  1860. if (!error) {
  1861. dentry->d_fsdata = cgrp;
  1862. inc_nlink(parent->d_inode);
  1863. rcu_assign_pointer(cgrp->dentry, dentry);
  1864. dget(dentry);
  1865. }
  1866. dput(dentry);
  1867. return error;
  1868. }
  1869. /**
  1870. * cgroup_file_mode - deduce file mode of a control file
  1871. * @cft: the control file in question
  1872. *
  1873. * returns cft->mode if ->mode is not 0
  1874. * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
  1875. * returns S_IRUGO if it has only a read handler
  1876. * returns S_IWUSR if it has only a write hander
  1877. */
  1878. static mode_t cgroup_file_mode(const struct cftype *cft)
  1879. {
  1880. mode_t mode = 0;
  1881. if (cft->mode)
  1882. return cft->mode;
  1883. if (cft->read || cft->read_u64 || cft->read_s64 ||
  1884. cft->read_map || cft->read_seq_string)
  1885. mode |= S_IRUGO;
  1886. if (cft->write || cft->write_u64 || cft->write_s64 ||
  1887. cft->write_string || cft->trigger)
  1888. mode |= S_IWUSR;
  1889. return mode;
  1890. }
  1891. int cgroup_add_file(struct cgroup *cgrp,
  1892. struct cgroup_subsys *subsys,
  1893. const struct cftype *cft)
  1894. {
  1895. struct dentry *dir = cgrp->dentry;
  1896. struct dentry *dentry;
  1897. int error;
  1898. mode_t mode;
  1899. char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
  1900. if (subsys && !test_bit(ROOT_NOPREFIX, &cgrp->root->flags)) {
  1901. strcpy(name, subsys->name);
  1902. strcat(name, ".");
  1903. }
  1904. strcat(name, cft->name);
  1905. BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
  1906. dentry = lookup_one_len(name, dir, strlen(name));
  1907. if (!IS_ERR(dentry)) {
  1908. mode = cgroup_file_mode(cft);
  1909. error = cgroup_create_file(dentry, mode | S_IFREG,
  1910. cgrp->root->sb);
  1911. if (!error)
  1912. dentry->d_fsdata = (void *)cft;
  1913. dput(dentry);
  1914. } else
  1915. error = PTR_ERR(dentry);
  1916. return error;
  1917. }
  1918. EXPORT_SYMBOL_GPL(cgroup_add_file);
  1919. int cgroup_add_files(struct cgroup *cgrp,
  1920. struct cgroup_subsys *subsys,
  1921. const struct cftype cft[],
  1922. int count)
  1923. {
  1924. int i, err;
  1925. for (i = 0; i < count; i++) {
  1926. err = cgroup_add_file(cgrp, subsys, &cft[i]);
  1927. if (err)
  1928. return err;
  1929. }
  1930. return 0;
  1931. }
  1932. EXPORT_SYMBOL_GPL(cgroup_add_files);
  1933. /**
  1934. * cgroup_task_count - count the number of tasks in a cgroup.
  1935. * @cgrp: the cgroup in question
  1936. *
  1937. * Return the number of tasks in the cgroup.
  1938. */
  1939. int cgroup_task_count(const struct cgroup *cgrp)
  1940. {
  1941. int count = 0;
  1942. struct cg_cgroup_link *link;
  1943. read_lock(&css_set_lock);
  1944. list_for_each_entry(link, &cgrp->css_sets, cgrp_link_list) {
  1945. count += atomic_read(&link->cg->refcount);
  1946. }
  1947. read_unlock(&css_set_lock);
  1948. return count;
  1949. }
  1950. /*
  1951. * Advance a list_head iterator. The iterator should be positioned at
  1952. * the start of a css_set
  1953. */
  1954. static void cgroup_advance_iter(struct cgroup *cgrp,
  1955. struct cgroup_iter *it)
  1956. {
  1957. struct list_head *l = it->cg_link;
  1958. struct cg_cgroup_link *link;
  1959. struct css_set *cg;
  1960. /* Advance to the next non-empty css_set */
  1961. do {
  1962. l = l->next;
  1963. if (l == &cgrp->css_sets) {
  1964. it->cg_link = NULL;
  1965. return;
  1966. }
  1967. link = list_entry(l, struct cg_cgroup_link, cgrp_link_list);
  1968. cg = link->cg;
  1969. } while (list_empty(&cg->tasks));
  1970. it->cg_link = l;
  1971. it->task = cg->tasks.next;
  1972. }
  1973. /*
  1974. * To reduce the fork() overhead for systems that are not actually
  1975. * using their cgroups capability, we don't maintain the lists running
  1976. * through each css_set to its tasks until we see the list actually
  1977. * used - in other words after the first call to cgroup_iter_start().
  1978. *
  1979. * The tasklist_lock is not held here, as do_each_thread() and
  1980. * while_each_thread() are protected by RCU.
  1981. */
  1982. static void cgroup_enable_task_cg_lists(void)
  1983. {
  1984. struct task_struct *p, *g;
  1985. write_lock(&css_set_lock);
  1986. use_task_css_set_links = 1;
  1987. do_each_thread(g, p) {
  1988. task_lock(p);
  1989. /*
  1990. * We should check if the process is exiting, otherwise
  1991. * it will race with cgroup_exit() in that the list
  1992. * entry won't be deleted though the process has exited.
  1993. */
  1994. if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
  1995. list_add(&p->cg_list, &p->cgroups->tasks);
  1996. task_unlock(p);
  1997. } while_each_thread(g, p);
  1998. write_unlock(&css_set_lock);
  1999. }
  2000. void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
  2001. {
  2002. /*
  2003. * The first time anyone tries to iterate across a cgroup,
  2004. * we need to enable the list linking each css_set to its
  2005. * tasks, and fix up all existing tasks.
  2006. */
  2007. if (!use_task_css_set_links)
  2008. cgroup_enable_task_cg_lists();
  2009. read_lock(&css_set_lock);
  2010. it->cg_link = &cgrp->css_sets;
  2011. cgroup_advance_iter(cgrp, it);
  2012. }
  2013. struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
  2014. struct cgroup_iter *it)
  2015. {
  2016. struct task_struct *res;
  2017. struct list_head *l = it->task;
  2018. struct cg_cgroup_link *link;
  2019. /* If the iterator cg is NULL, we have no tasks */
  2020. if (!it->cg_link)
  2021. return NULL;
  2022. res = list_entry(l, struct task_struct, cg_list);
  2023. /* Advance iterator to find next entry */
  2024. l = l->next;
  2025. link = list_entry(it->cg_link, struct cg_cgroup_link, cgrp_link_list);
  2026. if (l == &link->cg->tasks) {
  2027. /* We reached the end of this task list - move on to
  2028. * the next cg_cgroup_link */
  2029. cgroup_advance_iter(cgrp, it);
  2030. } else {
  2031. it->task = l;
  2032. }
  2033. return res;
  2034. }
  2035. void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
  2036. {
  2037. read_unlock(&css_set_lock);
  2038. }
  2039. static inline int started_after_time(struct task_struct *t1,
  2040. struct timespec *time,
  2041. struct task_struct *t2)
  2042. {
  2043. int start_diff = timespec_compare(&t1->start_time, time);
  2044. if (start_diff > 0) {
  2045. return 1;
  2046. } else if (start_diff < 0) {
  2047. return 0;
  2048. } else {
  2049. /*
  2050. * Arbitrarily, if two processes started at the same
  2051. * time, we'll say that the lower pointer value
  2052. * started first. Note that t2 may have exited by now
  2053. * so this may not be a valid pointer any longer, but
  2054. * that's fine - it still serves to distinguish
  2055. * between two tasks started (effectively) simultaneously.
  2056. */
  2057. return t1 > t2;
  2058. }
  2059. }
  2060. /*
  2061. * This function is a callback from heap_insert() and is used to order
  2062. * the heap.
  2063. * In this case we order the heap in descending task start time.
  2064. */
  2065. static inline int started_after(void *p1, void *p2)
  2066. {
  2067. struct task_struct *t1 = p1;
  2068. struct task_struct *t2 = p2;
  2069. return started_after_time(t1, &t2->start_time, t2);
  2070. }
  2071. /**
  2072. * cgroup_scan_tasks - iterate though all the tasks in a cgroup
  2073. * @scan: struct cgroup_scanner containing arguments for the scan
  2074. *
  2075. * Arguments include pointers to callback functions test_task() and
  2076. * process_task().
  2077. * Iterate through all the tasks in a cgroup, calling test_task() for each,
  2078. * and if it returns true, call process_task() for it also.
  2079. * The test_task pointer may be NULL, meaning always true (select all tasks).
  2080. * Effectively duplicates cgroup_iter_{start,next,end}()
  2081. * but does not lock css_set_lock for the call to process_task().
  2082. * The struct cgroup_scanner may be embedded in any structure of the caller's
  2083. * creation.
  2084. * It is guaranteed that process_task() will act on every task that
  2085. * is a member of the cgroup for the duration of this call. This
  2086. * function may or may not call process_task() for tasks that exit
  2087. * or move to a different cgroup during the call, or are forked or
  2088. * move into the cgroup during the call.
  2089. *
  2090. * Note that test_task() may be called with locks held, and may in some
  2091. * situations be called multiple times for the same task, so it should
  2092. * be cheap.
  2093. * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
  2094. * pre-allocated and will be used for heap operations (and its "gt" member will
  2095. * be overwritten), else a temporary heap will be used (allocation of which
  2096. * may cause this function to fail).
  2097. */
  2098. int cgroup_scan_tasks(struct cgroup_scanner *scan)
  2099. {
  2100. int retval, i;
  2101. struct cgroup_iter it;
  2102. struct task_struct *p, *dropped;
  2103. /* Never dereference latest_task, since it's not refcounted */
  2104. struct task_struct *latest_task = NULL;
  2105. struct ptr_heap tmp_heap;
  2106. struct ptr_heap *heap;
  2107. struct timespec latest_time = { 0, 0 };
  2108. if (scan->heap) {
  2109. /* The caller supplied our heap and pre-allocated its memory */
  2110. heap = scan->heap;
  2111. heap->gt = &started_after;
  2112. } else {
  2113. /* We need to allocate our own heap memory */
  2114. heap = &tmp_heap;
  2115. retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
  2116. if (retval)
  2117. /* cannot allocate the heap */
  2118. return retval;
  2119. }
  2120. again:
  2121. /*
  2122. * Scan tasks in the cgroup, using the scanner's "test_task" callback
  2123. * to determine which are of interest, and using the scanner's
  2124. * "process_task" callback to process any of them that need an update.
  2125. * Since we don't want to hold any locks during the task updates,
  2126. * gather tasks to be processed in a heap structure.
  2127. * The heap is sorted by descending task start time.
  2128. * If the statically-sized heap fills up, we overflow tasks that
  2129. * started later, and in future iterations only consider tasks that
  2130. * started after the latest task in the previous pass. This
  2131. * guarantees forward progress and that we don't miss any tasks.
  2132. */
  2133. heap->size = 0;
  2134. cgroup_iter_start(scan->cg, &it);
  2135. while ((p = cgroup_iter_next(scan->cg, &it))) {
  2136. /*
  2137. * Only affect tasks that qualify per the caller's callback,
  2138. * if he provided one
  2139. */
  2140. if (scan->test_task && !scan->test_task(p, scan))
  2141. continue;
  2142. /*
  2143. * Only process tasks that started after the last task
  2144. * we processed
  2145. */
  2146. if (!started_after_time(p, &latest_time, latest_task))
  2147. continue;
  2148. dropped = heap_insert(heap, p);
  2149. if (dropped == NULL) {
  2150. /*
  2151. * The new task was inserted; the heap wasn't
  2152. * previously full
  2153. */
  2154. get_task_struct(p);
  2155. } else if (dropped != p) {
  2156. /*
  2157. * The new task was inserted, and pushed out a
  2158. * different task
  2159. */
  2160. get_task_struct(p);
  2161. put_task_struct(dropped);
  2162. }
  2163. /*
  2164. * Else the new task was newer than anything already in
  2165. * the heap and wasn't inserted
  2166. */
  2167. }
  2168. cgroup_iter_end(scan->cg, &it);
  2169. if (heap->size) {
  2170. for (i = 0; i < heap->size; i++) {
  2171. struct task_struct *q = heap->ptrs[i];
  2172. if (i == 0) {
  2173. latest_time = q->start_time;
  2174. latest_task = q;
  2175. }
  2176. /* Process the task per the caller's callback */
  2177. scan->process_task(q, scan);
  2178. put_task_struct(q);
  2179. }
  2180. /*
  2181. * If we had to process any tasks at all, scan again
  2182. * in case some of them were in the middle of forking
  2183. * children that didn't get processed.
  2184. * Not the most efficient way to do it, but it avoids
  2185. * having to take callback_mutex in the fork path
  2186. */
  2187. goto again;
  2188. }
  2189. if (heap == &tmp_heap)
  2190. heap_free(&tmp_heap);
  2191. return 0;
  2192. }
  2193. /*
  2194. * Stuff for reading the 'tasks'/'procs' files.
  2195. *
  2196. * Reading this file can return large amounts of data if a cgroup has
  2197. * *lots* of attached tasks. So it may need several calls to read(),
  2198. * but we cannot guarantee that the information we produce is correct
  2199. * unless we produce it entirely atomically.
  2200. *
  2201. */
  2202. /*
  2203. * The following two functions "fix" the issue where there are more pids
  2204. * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
  2205. * TODO: replace with a kernel-wide solution to this problem
  2206. */
  2207. #define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
  2208. static void *pidlist_allocate(int count)
  2209. {
  2210. if (PIDLIST_TOO_LARGE(count))
  2211. return vmalloc(count * sizeof(pid_t));
  2212. else
  2213. return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
  2214. }
  2215. static void pidlist_free(void *p)
  2216. {
  2217. if (is_vmalloc_addr(p))
  2218. vfree(p);
  2219. else
  2220. kfree(p);
  2221. }
  2222. static void *pidlist_resize(void *p, int newcount)
  2223. {
  2224. void *newlist;
  2225. /* note: if new alloc fails, old p will still be valid either way */
  2226. if (is_vmalloc_addr(p)) {
  2227. newlist = vmalloc(newcount * sizeof(pid_t));
  2228. if (!newlist)
  2229. return NULL;
  2230. memcpy(newlist, p, newcount * sizeof(pid_t));
  2231. vfree(p);
  2232. } else {
  2233. newlist = krealloc(p, newcount * sizeof(pid_t), GFP_KERNEL);
  2234. }
  2235. return newlist;
  2236. }
  2237. /*
  2238. * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
  2239. * If the new stripped list is sufficiently smaller and there's enough memory
  2240. * to allocate a new buffer, will let go of the unneeded memory. Returns the
  2241. * number of unique elements.
  2242. */
  2243. /* is the size difference enough that we should re-allocate the array? */
  2244. #define PIDLIST_REALLOC_DIFFERENCE(old, new) ((old) - PAGE_SIZE >= (new))
  2245. static int pidlist_uniq(pid_t **p, int length)
  2246. {
  2247. int src, dest = 1;
  2248. pid_t *list = *p;
  2249. pid_t *newlist;
  2250. /*
  2251. * we presume the 0th element is unique, so i starts at 1. trivial
  2252. * edge cases first; no work needs to be done for either
  2253. */
  2254. if (length == 0 || length == 1)
  2255. return length;
  2256. /* src and dest walk down the list; dest counts unique elements */
  2257. for (src = 1; src < length; src++) {
  2258. /* find next unique element */
  2259. while (list[src] == list[src-1]) {
  2260. src++;
  2261. if (src == length)
  2262. goto after;
  2263. }
  2264. /* dest always points to where the next unique element goes */
  2265. list[dest] = list[src];
  2266. dest++;
  2267. }
  2268. after:
  2269. /*
  2270. * if the length difference is large enough, we want to allocate a
  2271. * smaller buffer to save memory. if this fails due to out of memory,
  2272. * we'll just stay with what we've got.
  2273. */
  2274. if (PIDLIST_REALLOC_DIFFERENCE(length, dest)) {
  2275. newlist = pidlist_resize(list, dest);
  2276. if (newlist)
  2277. *p = newlist;
  2278. }
  2279. return dest;
  2280. }
  2281. static int cmppid(const void *a, const void *b)
  2282. {
  2283. return *(pid_t *)a - *(pid_t *)b;
  2284. }
  2285. /*
  2286. * find the appropriate pidlist for our purpose (given procs vs tasks)
  2287. * returns with the lock on that pidlist already held, and takes care
  2288. * of the use count, or returns NULL with no locks held if we're out of
  2289. * memory.
  2290. */
  2291. static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
  2292. enum cgroup_filetype type)
  2293. {
  2294. struct cgroup_pidlist *l;
  2295. /* don't need task_nsproxy() if we're looking at ourself */
  2296. struct pid_namespace *ns = get_pid_ns(current->nsproxy->pid_ns);
  2297. /*
  2298. * We can't drop the pidlist_mutex before taking the l->mutex in case
  2299. * the last ref-holder is trying to remove l from the list at the same
  2300. * time. Holding the pidlist_mutex precludes somebody taking whichever
  2301. * list we find out from under us - compare release_pid_array().
  2302. */
  2303. mutex_lock(&cgrp->pidlist_mutex);
  2304. list_for_each_entry(l, &cgrp->pidlists, links) {
  2305. if (l->key.type == type && l->key.ns == ns) {
  2306. /* found a matching list - drop the extra refcount */
  2307. put_pid_ns(ns);
  2308. /* make sure l doesn't vanish out from under us */
  2309. down_write(&l->mutex);
  2310. mutex_unlock(&cgrp->pidlist_mutex);
  2311. return l;
  2312. }
  2313. }
  2314. /* entry not found; create a new one */
  2315. l = kmalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
  2316. if (!l) {
  2317. mutex_unlock(&cgrp->pidlist_mutex);
  2318. put_pid_ns(ns);
  2319. return l;
  2320. }
  2321. init_rwsem(&l->mutex);
  2322. down_write(&l->mutex);
  2323. l->key.type = type;
  2324. l->key.ns = ns;
  2325. l->use_count = 0; /* don't increment here */
  2326. l->list = NULL;
  2327. l->owner = cgrp;
  2328. list_add(&l->links, &cgrp->pidlists);
  2329. mutex_unlock(&cgrp->pidlist_mutex);
  2330. return l;
  2331. }
  2332. /*
  2333. * Load a cgroup's pidarray with either procs' tgids or tasks' pids
  2334. */
  2335. static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
  2336. struct cgroup_pidlist **lp)
  2337. {
  2338. pid_t *array;
  2339. int length;
  2340. int pid, n = 0; /* used for populating the array */
  2341. struct cgroup_iter it;
  2342. struct task_struct *tsk;
  2343. struct cgroup_pidlist *l;
  2344. /*
  2345. * If cgroup gets more users after we read count, we won't have
  2346. * enough space - tough. This race is indistinguishable to the
  2347. * caller from the case that the additional cgroup users didn't
  2348. * show up until sometime later on.
  2349. */
  2350. length = cgroup_task_count(cgrp);
  2351. array = pidlist_allocate(length);
  2352. if (!array)
  2353. return -ENOMEM;
  2354. /* now, populate the array */
  2355. cgroup_iter_start(cgrp, &it);
  2356. while ((tsk = cgroup_iter_next(cgrp, &it))) {
  2357. if (unlikely(n == length))
  2358. break;
  2359. /* get tgid or pid for procs or tasks file respectively */
  2360. if (type == CGROUP_FILE_PROCS)
  2361. pid = task_tgid_vnr(tsk);
  2362. else
  2363. pid = task_pid_vnr(tsk);
  2364. if (pid > 0) /* make sure to only use valid results */
  2365. array[n++] = pid;
  2366. }
  2367. cgroup_iter_end(cgrp, &it);
  2368. length = n;
  2369. /* now sort & (if procs) strip out duplicates */
  2370. sort(array, length, sizeof(pid_t), cmppid, NULL);
  2371. if (type == CGROUP_FILE_PROCS)
  2372. length = pidlist_uniq(&array, length);
  2373. l = cgroup_pidlist_find(cgrp, type);
  2374. if (!l) {
  2375. pidlist_free(array);
  2376. return -ENOMEM;
  2377. }
  2378. /* store array, freeing old if necessary - lock already held */
  2379. pidlist_free(l->list);
  2380. l->list = array;
  2381. l->length = length;
  2382. l->use_count++;
  2383. up_write(&l->mutex);
  2384. *lp = l;
  2385. return 0;
  2386. }
  2387. /**
  2388. * cgroupstats_build - build and fill cgroupstats
  2389. * @stats: cgroupstats to fill information into
  2390. * @dentry: A dentry entry belonging to the cgroup for which stats have
  2391. * been requested.
  2392. *
  2393. * Build and fill cgroupstats so that taskstats can export it to user
  2394. * space.
  2395. */
  2396. int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
  2397. {
  2398. int ret = -EINVAL;
  2399. struct cgroup *cgrp;
  2400. struct cgroup_iter it;
  2401. struct task_struct *tsk;
  2402. /*
  2403. * Validate dentry by checking the superblock operations,
  2404. * and make sure it's a directory.
  2405. */
  2406. if (dentry->d_sb->s_op != &cgroup_ops ||
  2407. !S_ISDIR(dentry->d_inode->i_mode))
  2408. goto err;
  2409. ret = 0;
  2410. cgrp = dentry->d_fsdata;
  2411. cgroup_iter_start(cgrp, &it);
  2412. while ((tsk = cgroup_iter_next(cgrp, &it))) {
  2413. switch (tsk->state) {
  2414. case TASK_RUNNING:
  2415. stats->nr_running++;
  2416. break;
  2417. case TASK_INTERRUPTIBLE:
  2418. stats->nr_sleeping++;
  2419. break;
  2420. case TASK_UNINTERRUPTIBLE:
  2421. stats->nr_uninterruptible++;
  2422. break;
  2423. case TASK_STOPPED:
  2424. stats->nr_stopped++;
  2425. break;
  2426. default:
  2427. if (delayacct_is_task_waiting_on_io(tsk))
  2428. stats->nr_io_wait++;
  2429. break;
  2430. }
  2431. }
  2432. cgroup_iter_end(cgrp, &it);
  2433. err:
  2434. return ret;
  2435. }
  2436. /*
  2437. * seq_file methods for the tasks/procs files. The seq_file position is the
  2438. * next pid to display; the seq_file iterator is a pointer to the pid
  2439. * in the cgroup->l->list array.
  2440. */
  2441. static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
  2442. {
  2443. /*
  2444. * Initially we receive a position value that corresponds to
  2445. * one more than the last pid shown (or 0 on the first call or
  2446. * after a seek to the start). Use a binary-search to find the
  2447. * next pid to display, if any
  2448. */
  2449. struct cgroup_pidlist *l = s->private;
  2450. int index = 0, pid = *pos;
  2451. int *iter;
  2452. down_read(&l->mutex);
  2453. if (pid) {
  2454. int end = l->length;
  2455. while (index < end) {
  2456. int mid = (index + end) / 2;
  2457. if (l->list[mid] == pid) {
  2458. index = mid;
  2459. break;
  2460. } else if (l->list[mid] <= pid)
  2461. index = mid + 1;
  2462. else
  2463. end = mid;
  2464. }
  2465. }
  2466. /* If we're off the end of the array, we're done */
  2467. if (index >= l->length)
  2468. return NULL;
  2469. /* Update the abstract position to be the actual pid that we found */
  2470. iter = l->list + index;
  2471. *pos = *iter;
  2472. return iter;
  2473. }
  2474. static void cgroup_pidlist_stop(struct seq_file *s, void *v)
  2475. {
  2476. struct cgroup_pidlist *l = s->private;
  2477. up_read(&l->mutex);
  2478. }
  2479. static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
  2480. {
  2481. struct cgroup_pidlist *l = s->private;
  2482. pid_t *p = v;
  2483. pid_t *end = l->list + l->length;
  2484. /*
  2485. * Advance to the next pid in the array. If this goes off the
  2486. * end, we're done
  2487. */
  2488. p++;
  2489. if (p >= end) {
  2490. return NULL;
  2491. } else {
  2492. *pos = *p;
  2493. return p;
  2494. }
  2495. }
  2496. static int cgroup_pidlist_show(struct seq_file *s, void *v)
  2497. {
  2498. return seq_printf(s, "%d\n", *(int *)v);
  2499. }
  2500. /*
  2501. * seq_operations functions for iterating on pidlists through seq_file -
  2502. * independent of whether it's tasks or procs
  2503. */
  2504. static const struct seq_operations cgroup_pidlist_seq_operations = {
  2505. .start = cgroup_pidlist_start,
  2506. .stop = cgroup_pidlist_stop,
  2507. .next = cgroup_pidlist_next,
  2508. .show = cgroup_pidlist_show,
  2509. };
  2510. static void cgroup_release_pid_array(struct cgroup_pidlist *l)
  2511. {
  2512. /*
  2513. * the case where we're the last user of this particular pidlist will
  2514. * have us remove it from the cgroup's list, which entails taking the
  2515. * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
  2516. * pidlist_mutex, we have to take pidlist_mutex first.
  2517. */
  2518. mutex_lock(&l->owner->pidlist_mutex);
  2519. down_write(&l->mutex);
  2520. BUG_ON(!l->use_count);
  2521. if (!--l->use_count) {
  2522. /* we're the last user if refcount is 0; remove and free */
  2523. list_del(&l->links);
  2524. mutex_unlock(&l->owner->pidlist_mutex);
  2525. pidlist_free(l->list);
  2526. put_pid_ns(l->key.ns);
  2527. up_write(&l->mutex);
  2528. kfree(l);
  2529. return;
  2530. }
  2531. mutex_unlock(&l->owner->pidlist_mutex);
  2532. up_write(&l->mutex);
  2533. }
  2534. static int cgroup_pidlist_release(struct inode *inode, struct file *file)
  2535. {
  2536. struct cgroup_pidlist *l;
  2537. if (!(file->f_mode & FMODE_READ))
  2538. return 0;
  2539. /*
  2540. * the seq_file will only be initialized if the file was opened for
  2541. * reading; hence we check if it's not null only in that case.
  2542. */
  2543. l = ((struct seq_file *)file->private_data)->private;
  2544. cgroup_release_pid_array(l);
  2545. return seq_release(inode, file);
  2546. }
  2547. static const struct file_operations cgroup_pidlist_operations = {
  2548. .read = seq_read,
  2549. .llseek = seq_lseek,
  2550. .write = cgroup_file_write,
  2551. .release = cgroup_pidlist_release,
  2552. };
  2553. /*
  2554. * The following functions handle opens on a file that displays a pidlist
  2555. * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
  2556. * in the cgroup.
  2557. */
  2558. /* helper function for the two below it */
  2559. static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
  2560. {
  2561. struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
  2562. struct cgroup_pidlist *l;
  2563. int retval;
  2564. /* Nothing to do for write-only files */
  2565. if (!(file->f_mode & FMODE_READ))
  2566. return 0;
  2567. /* have the array populated */
  2568. retval = pidlist_array_load(cgrp, type, &l);
  2569. if (retval)
  2570. return retval;
  2571. /* configure file information */
  2572. file->f_op = &cgroup_pidlist_operations;
  2573. retval = seq_open(file, &cgroup_pidlist_seq_operations);
  2574. if (retval) {
  2575. cgroup_release_pid_array(l);
  2576. return retval;
  2577. }
  2578. ((struct seq_file *)file->private_data)->private = l;
  2579. return 0;
  2580. }
  2581. static int cgroup_tasks_open(struct inode *unused, struct file *file)
  2582. {
  2583. return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
  2584. }
  2585. static int cgroup_procs_open(struct inode *unused, struct file *file)
  2586. {
  2587. return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
  2588. }
  2589. static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
  2590. struct cftype *cft)
  2591. {
  2592. return notify_on_release(cgrp);
  2593. }
  2594. static int cgroup_write_notify_on_release(struct cgroup *cgrp,
  2595. struct cftype *cft,
  2596. u64 val)
  2597. {
  2598. clear_bit(CGRP_RELEASABLE, &cgrp->flags);
  2599. if (val)
  2600. set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
  2601. else
  2602. clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
  2603. return 0;
  2604. }
  2605. /*
  2606. * for the common functions, 'private' gives the type of file
  2607. */
  2608. /* for hysterical raisins, we can't put this on the older files */
  2609. #define CGROUP_FILE_GENERIC_PREFIX "cgroup."
  2610. static struct cftype files[] = {
  2611. {
  2612. .name = "tasks",
  2613. .open = cgroup_tasks_open,
  2614. .write_u64 = cgroup_tasks_write,
  2615. .release = cgroup_pidlist_release,
  2616. .mode = S_IRUGO | S_IWUSR,
  2617. },
  2618. {
  2619. .name = CGROUP_FILE_GENERIC_PREFIX "procs",
  2620. .open = cgroup_procs_open,
  2621. /* .write_u64 = cgroup_procs_write, TODO */
  2622. .release = cgroup_pidlist_release,
  2623. .mode = S_IRUGO,
  2624. },
  2625. {
  2626. .name = "notify_on_release",
  2627. .read_u64 = cgroup_read_notify_on_release,
  2628. .write_u64 = cgroup_write_notify_on_release,
  2629. },
  2630. };
  2631. static struct cftype cft_release_agent = {
  2632. .name = "release_agent",
  2633. .read_seq_string = cgroup_release_agent_show,
  2634. .write_string = cgroup_release_agent_write,
  2635. .max_write_len = PATH_MAX,
  2636. };
  2637. static int cgroup_populate_dir(struct cgroup *cgrp)
  2638. {
  2639. int err;
  2640. struct cgroup_subsys *ss;
  2641. /* First clear out any existing files */
  2642. cgroup_clear_directory(cgrp->dentry);
  2643. err = cgroup_add_files(cgrp, NULL, files, ARRAY_SIZE(files));
  2644. if (err < 0)
  2645. return err;
  2646. if (cgrp == cgrp->top_cgroup) {
  2647. if ((err = cgroup_add_file(cgrp, NULL, &cft_release_agent)) < 0)
  2648. return err;
  2649. }
  2650. for_each_subsys(cgrp->root, ss) {
  2651. if (ss->populate && (err = ss->populate(ss, cgrp)) < 0)
  2652. return err;
  2653. }
  2654. /* This cgroup is ready now */
  2655. for_each_subsys(cgrp->root, ss) {
  2656. struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
  2657. /*
  2658. * Update id->css pointer and make this css visible from
  2659. * CSS ID functions. This pointer will be dereferened
  2660. * from RCU-read-side without locks.
  2661. */
  2662. if (css->id)
  2663. rcu_assign_pointer(css->id->css, css);
  2664. }
  2665. return 0;
  2666. }
  2667. static void init_cgroup_css(struct cgroup_subsys_state *css,
  2668. struct cgroup_subsys *ss,
  2669. struct cgroup *cgrp)
  2670. {
  2671. css->cgroup = cgrp;
  2672. atomic_set(&css->refcnt, 1);
  2673. css->flags = 0;
  2674. css->id = NULL;
  2675. if (cgrp == dummytop)
  2676. set_bit(CSS_ROOT, &css->flags);
  2677. BUG_ON(cgrp->subsys[ss->subsys_id]);
  2678. cgrp->subsys[ss->subsys_id] = css;
  2679. }
  2680. static void cgroup_lock_hierarchy(struct cgroupfs_root *root)
  2681. {
  2682. /* We need to take each hierarchy_mutex in a consistent order */
  2683. int i;
  2684. /*
  2685. * No worry about a race with rebind_subsystems that might mess up the
  2686. * locking order, since both parties are under cgroup_mutex.
  2687. */
  2688. for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
  2689. struct cgroup_subsys *ss = subsys[i];
  2690. if (ss == NULL)
  2691. continue;
  2692. if (ss->root == root)
  2693. mutex_lock(&ss->hierarchy_mutex);
  2694. }
  2695. }
  2696. static void cgroup_unlock_hierarchy(struct cgroupfs_root *root)
  2697. {
  2698. int i;
  2699. for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
  2700. struct cgroup_subsys *ss = subsys[i];
  2701. if (ss == NULL)
  2702. continue;
  2703. if (ss->root == root)
  2704. mutex_unlock(&ss->hierarchy_mutex);
  2705. }
  2706. }
  2707. /*
  2708. * cgroup_create - create a cgroup
  2709. * @parent: cgroup that will be parent of the new cgroup
  2710. * @dentry: dentry of the new cgroup
  2711. * @mode: mode to set on new inode
  2712. *
  2713. * Must be called with the mutex on the parent inode held
  2714. */
  2715. static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
  2716. mode_t mode)
  2717. {
  2718. struct cgroup *cgrp;
  2719. struct cgroupfs_root *root = parent->root;
  2720. int err = 0;
  2721. struct cgroup_subsys *ss;
  2722. struct super_block *sb = root->sb;
  2723. cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
  2724. if (!cgrp)
  2725. return -ENOMEM;
  2726. /* Grab a reference on the superblock so the hierarchy doesn't
  2727. * get deleted on unmount if there are child cgroups. This
  2728. * can be done outside cgroup_mutex, since the sb can't
  2729. * disappear while someone has an open control file on the
  2730. * fs */
  2731. atomic_inc(&sb->s_active);
  2732. mutex_lock(&cgroup_mutex);
  2733. init_cgroup_housekeeping(cgrp);
  2734. cgrp->parent = parent;
  2735. cgrp->root = parent->root;
  2736. cgrp->top_cgroup = parent->top_cgroup;
  2737. if (notify_on_release(parent))
  2738. set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
  2739. for_each_subsys(root, ss) {
  2740. struct cgroup_subsys_state *css = ss->create(ss, cgrp);
  2741. if (IS_ERR(css)) {
  2742. err = PTR_ERR(css);
  2743. goto err_destroy;
  2744. }
  2745. init_cgroup_css(css, ss, cgrp);
  2746. if (ss->use_id) {
  2747. err = alloc_css_id(ss, parent, cgrp);
  2748. if (err)
  2749. goto err_destroy;
  2750. }
  2751. /* At error, ->destroy() callback has to free assigned ID. */
  2752. }
  2753. cgroup_lock_hierarchy(root);
  2754. list_add(&cgrp->sibling, &cgrp->parent->children);
  2755. cgroup_unlock_hierarchy(root);
  2756. root->number_of_cgroups++;
  2757. err = cgroup_create_dir(cgrp, dentry, mode);
  2758. if (err < 0)
  2759. goto err_remove;
  2760. /* The cgroup directory was pre-locked for us */
  2761. BUG_ON(!mutex_is_locked(&cgrp->dentry->d_inode->i_mutex));
  2762. err = cgroup_populate_dir(cgrp);
  2763. /* If err < 0, we have a half-filled directory - oh well ;) */
  2764. mutex_unlock(&cgroup_mutex);
  2765. mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
  2766. return 0;
  2767. err_remove:
  2768. cgroup_lock_hierarchy(root);
  2769. list_del(&cgrp->sibling);
  2770. cgroup_unlock_hierarchy(root);
  2771. root->number_of_cgroups--;
  2772. err_destroy:
  2773. for_each_subsys(root, ss) {
  2774. if (cgrp->subsys[ss->subsys_id])
  2775. ss->destroy(ss, cgrp);
  2776. }
  2777. mutex_unlock(&cgroup_mutex);
  2778. /* Release the reference count that we took on the superblock */
  2779. deactivate_super(sb);
  2780. kfree(cgrp);
  2781. return err;
  2782. }
  2783. static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  2784. {
  2785. struct cgroup *c_parent = dentry->d_parent->d_fsdata;
  2786. /* the vfs holds inode->i_mutex already */
  2787. return cgroup_create(c_parent, dentry, mode | S_IFDIR);
  2788. }
  2789. static int cgroup_has_css_refs(struct cgroup *cgrp)
  2790. {
  2791. /* Check the reference count on each subsystem. Since we
  2792. * already established that there are no tasks in the
  2793. * cgroup, if the css refcount is also 1, then there should
  2794. * be no outstanding references, so the subsystem is safe to
  2795. * destroy. We scan across all subsystems rather than using
  2796. * the per-hierarchy linked list of mounted subsystems since
  2797. * we can be called via check_for_release() with no
  2798. * synchronization other than RCU, and the subsystem linked
  2799. * list isn't RCU-safe */
  2800. int i;
  2801. /*
  2802. * We won't need to lock the subsys array, because the subsystems
  2803. * we're concerned about aren't going anywhere since our cgroup root
  2804. * has a reference on them.
  2805. */
  2806. for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
  2807. struct cgroup_subsys *ss = subsys[i];
  2808. struct cgroup_subsys_state *css;
  2809. /* Skip subsystems not present or not in this hierarchy */
  2810. if (ss == NULL || ss->root != cgrp->root)
  2811. continue;
  2812. css = cgrp->subsys[ss->subsys_id];
  2813. /* When called from check_for_release() it's possible
  2814. * that by this point the cgroup has been removed
  2815. * and the css deleted. But a false-positive doesn't
  2816. * matter, since it can only happen if the cgroup
  2817. * has been deleted and hence no longer needs the
  2818. * release agent to be called anyway. */
  2819. if (css && (atomic_read(&css->refcnt) > 1))
  2820. return 1;
  2821. }
  2822. return 0;
  2823. }
  2824. /*
  2825. * Atomically mark all (or else none) of the cgroup's CSS objects as
  2826. * CSS_REMOVED. Return true on success, or false if the cgroup has
  2827. * busy subsystems. Call with cgroup_mutex held
  2828. */
  2829. static int cgroup_clear_css_refs(struct cgroup *cgrp)
  2830. {
  2831. struct cgroup_subsys *ss;
  2832. unsigned long flags;
  2833. bool failed = false;
  2834. local_irq_save(flags);
  2835. for_each_subsys(cgrp->root, ss) {
  2836. struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
  2837. int refcnt;
  2838. while (1) {
  2839. /* We can only remove a CSS with a refcnt==1 */
  2840. refcnt = atomic_read(&css->refcnt);
  2841. if (refcnt > 1) {
  2842. failed = true;
  2843. goto done;
  2844. }
  2845. BUG_ON(!refcnt);
  2846. /*
  2847. * Drop the refcnt to 0 while we check other
  2848. * subsystems. This will cause any racing
  2849. * css_tryget() to spin until we set the
  2850. * CSS_REMOVED bits or abort
  2851. */
  2852. if (atomic_cmpxchg(&css->refcnt, refcnt, 0) == refcnt)
  2853. break;
  2854. cpu_relax();
  2855. }
  2856. }
  2857. done:
  2858. for_each_subsys(cgrp->root, ss) {
  2859. struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
  2860. if (failed) {
  2861. /*
  2862. * Restore old refcnt if we previously managed
  2863. * to clear it from 1 to 0
  2864. */
  2865. if (!atomic_read(&css->refcnt))
  2866. atomic_set(&css->refcnt, 1);
  2867. } else {
  2868. /* Commit the fact that the CSS is removed */
  2869. set_bit(CSS_REMOVED, &css->flags);
  2870. }
  2871. }
  2872. local_irq_restore(flags);
  2873. return !failed;
  2874. }
  2875. static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
  2876. {
  2877. struct cgroup *cgrp = dentry->d_fsdata;
  2878. struct dentry *d;
  2879. struct cgroup *parent;
  2880. DEFINE_WAIT(wait);
  2881. int ret;
  2882. /* the vfs holds both inode->i_mutex already */
  2883. again:
  2884. mutex_lock(&cgroup_mutex);
  2885. if (atomic_read(&cgrp->count) != 0) {
  2886. mutex_unlock(&cgroup_mutex);
  2887. return -EBUSY;
  2888. }
  2889. if (!list_empty(&cgrp->children)) {
  2890. mutex_unlock(&cgroup_mutex);
  2891. return -EBUSY;
  2892. }
  2893. mutex_unlock(&cgroup_mutex);
  2894. /*
  2895. * In general, subsystem has no css->refcnt after pre_destroy(). But
  2896. * in racy cases, subsystem may have to get css->refcnt after
  2897. * pre_destroy() and it makes rmdir return with -EBUSY. This sometimes
  2898. * make rmdir return -EBUSY too often. To avoid that, we use waitqueue
  2899. * for cgroup's rmdir. CGRP_WAIT_ON_RMDIR is for synchronizing rmdir
  2900. * and subsystem's reference count handling. Please see css_get/put
  2901. * and css_tryget() and cgroup_wakeup_rmdir_waiter() implementation.
  2902. */
  2903. set_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
  2904. /*
  2905. * Call pre_destroy handlers of subsys. Notify subsystems
  2906. * that rmdir() request comes.
  2907. */
  2908. ret = cgroup_call_pre_destroy(cgrp);
  2909. if (ret) {
  2910. clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
  2911. return ret;
  2912. }
  2913. mutex_lock(&cgroup_mutex);
  2914. parent = cgrp->parent;
  2915. if (atomic_read(&cgrp->count) || !list_empty(&cgrp->children)) {
  2916. clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
  2917. mutex_unlock(&cgroup_mutex);
  2918. return -EBUSY;
  2919. }
  2920. prepare_to_wait(&cgroup_rmdir_waitq, &wait, TASK_INTERRUPTIBLE);
  2921. if (!cgroup_clear_css_refs(cgrp)) {
  2922. mutex_unlock(&cgroup_mutex);
  2923. /*
  2924. * Because someone may call cgroup_wakeup_rmdir_waiter() before
  2925. * prepare_to_wait(), we need to check this flag.
  2926. */
  2927. if (test_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags))
  2928. schedule();
  2929. finish_wait(&cgroup_rmdir_waitq, &wait);
  2930. clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
  2931. if (signal_pending(current))
  2932. return -EINTR;
  2933. goto again;
  2934. }
  2935. /* NO css_tryget() can success after here. */
  2936. finish_wait(&cgroup_rmdir_waitq, &wait);
  2937. clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
  2938. spin_lock(&release_list_lock);
  2939. set_bit(CGRP_REMOVED, &cgrp->flags);
  2940. if (!list_empty(&cgrp->release_list))
  2941. list_del(&cgrp->release_list);
  2942. spin_unlock(&release_list_lock);
  2943. cgroup_lock_hierarchy(cgrp->root);
  2944. /* delete this cgroup from parent->children */
  2945. list_del(&cgrp->sibling);
  2946. cgroup_unlock_hierarchy(cgrp->root);
  2947. spin_lock(&cgrp->dentry->d_lock);
  2948. d = dget(cgrp->dentry);
  2949. spin_unlock(&d->d_lock);
  2950. cgroup_d_remove_dir(d);
  2951. dput(d);
  2952. set_bit(CGRP_RELEASABLE, &parent->flags);
  2953. check_for_release(parent);
  2954. mutex_unlock(&cgroup_mutex);
  2955. return 0;
  2956. }
  2957. static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
  2958. {
  2959. struct cgroup_subsys_state *css;
  2960. printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
  2961. /* Create the top cgroup state for this subsystem */
  2962. list_add(&ss->sibling, &rootnode.subsys_list);
  2963. ss->root = &rootnode;
  2964. css = ss->create(ss, dummytop);
  2965. /* We don't handle early failures gracefully */
  2966. BUG_ON(IS_ERR(css));
  2967. init_cgroup_css(css, ss, dummytop);
  2968. /* Update the init_css_set to contain a subsys
  2969. * pointer to this state - since the subsystem is
  2970. * newly registered, all tasks and hence the
  2971. * init_css_set is in the subsystem's top cgroup. */
  2972. init_css_set.subsys[ss->subsys_id] = dummytop->subsys[ss->subsys_id];
  2973. need_forkexit_callback |= ss->fork || ss->exit;
  2974. /* At system boot, before all subsystems have been
  2975. * registered, no tasks have been forked, so we don't
  2976. * need to invoke fork callbacks here. */
  2977. BUG_ON(!list_empty(&init_task.tasks));
  2978. mutex_init(&ss->hierarchy_mutex);
  2979. lockdep_set_class(&ss->hierarchy_mutex, &ss->subsys_key);
  2980. ss->active = 1;
  2981. /* this function shouldn't be used with modular subsystems, since they
  2982. * need to register a subsys_id, among other things */
  2983. BUG_ON(ss->module);
  2984. }
  2985. /**
  2986. * cgroup_load_subsys: load and register a modular subsystem at runtime
  2987. * @ss: the subsystem to load
  2988. *
  2989. * This function should be called in a modular subsystem's initcall. If the
  2990. * subsytem is built as a module, it will be assigned a new subsys_id and set
  2991. * up for use. If the subsystem is built-in anyway, work is delegated to the
  2992. * simpler cgroup_init_subsys.
  2993. */
  2994. int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
  2995. {
  2996. int i;
  2997. struct cgroup_subsys_state *css;
  2998. /* check name and function validity */
  2999. if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
  3000. ss->create == NULL || ss->destroy == NULL)
  3001. return -EINVAL;
  3002. /*
  3003. * we don't support callbacks in modular subsystems. this check is
  3004. * before the ss->module check for consistency; a subsystem that could
  3005. * be a module should still have no callbacks even if the user isn't
  3006. * compiling it as one.
  3007. */
  3008. if (ss->fork || ss->exit)
  3009. return -EINVAL;
  3010. /*
  3011. * an optionally modular subsystem is built-in: we want to do nothing,
  3012. * since cgroup_init_subsys will have already taken care of it.
  3013. */
  3014. if (ss->module == NULL) {
  3015. /* a few sanity checks */
  3016. BUG_ON(ss->subsys_id >= CGROUP_BUILTIN_SUBSYS_COUNT);
  3017. BUG_ON(subsys[ss->subsys_id] != ss);
  3018. return 0;
  3019. }
  3020. /*
  3021. * need to register a subsys id before anything else - for example,
  3022. * init_cgroup_css needs it.
  3023. */
  3024. mutex_lock(&cgroup_mutex);
  3025. /* find the first empty slot in the array */
  3026. for (i = CGROUP_BUILTIN_SUBSYS_COUNT; i < CGROUP_SUBSYS_COUNT; i++) {
  3027. if (subsys[i] == NULL)
  3028. break;
  3029. }
  3030. if (i == CGROUP_SUBSYS_COUNT) {
  3031. /* maximum number of subsystems already registered! */
  3032. mutex_unlock(&cgroup_mutex);
  3033. return -EBUSY;
  3034. }
  3035. /* assign ourselves the subsys_id */
  3036. ss->subsys_id = i;
  3037. subsys[i] = ss;
  3038. /*
  3039. * no ss->create seems to need anything important in the ss struct, so
  3040. * this can happen first (i.e. before the rootnode attachment).
  3041. */
  3042. css = ss->create(ss, dummytop);
  3043. if (IS_ERR(css)) {
  3044. /* failure case - need to deassign the subsys[] slot. */
  3045. subsys[i] = NULL;
  3046. mutex_unlock(&cgroup_mutex);
  3047. return PTR_ERR(css);
  3048. }
  3049. list_add(&ss->sibling, &rootnode.subsys_list);
  3050. ss->root = &rootnode;
  3051. /* our new subsystem will be attached to the dummy hierarchy. */
  3052. init_cgroup_css(css, ss, dummytop);
  3053. /* init_idr must be after init_cgroup_css because it sets css->id. */
  3054. if (ss->use_id) {
  3055. int ret = cgroup_init_idr(ss, css);
  3056. if (ret) {
  3057. dummytop->subsys[ss->subsys_id] = NULL;
  3058. ss->destroy(ss, dummytop);
  3059. subsys[i] = NULL;
  3060. mutex_unlock(&cgroup_mutex);
  3061. return ret;
  3062. }
  3063. }
  3064. /*
  3065. * Now we need to entangle the css into the existing css_sets. unlike
  3066. * in cgroup_init_subsys, there are now multiple css_sets, so each one
  3067. * will need a new pointer to it; done by iterating the css_set_table.
  3068. * furthermore, modifying the existing css_sets will corrupt the hash
  3069. * table state, so each changed css_set will need its hash recomputed.
  3070. * this is all done under the css_set_lock.
  3071. */
  3072. write_lock(&css_set_lock);
  3073. for (i = 0; i < CSS_SET_TABLE_SIZE; i++) {
  3074. struct css_set *cg;
  3075. struct hlist_node *node, *tmp;
  3076. struct hlist_head *bucket = &css_set_table[i], *new_bucket;
  3077. hlist_for_each_entry_safe(cg, node, tmp, bucket, hlist) {
  3078. /* skip entries that we already rehashed */
  3079. if (cg->subsys[ss->subsys_id])
  3080. continue;
  3081. /* remove existing entry */
  3082. hlist_del(&cg->hlist);
  3083. /* set new value */
  3084. cg->subsys[ss->subsys_id] = css;
  3085. /* recompute hash and restore entry */
  3086. new_bucket = css_set_hash(cg->subsys);
  3087. hlist_add_head(&cg->hlist, new_bucket);
  3088. }
  3089. }
  3090. write_unlock(&css_set_lock);
  3091. mutex_init(&ss->hierarchy_mutex);
  3092. lockdep_set_class(&ss->hierarchy_mutex, &ss->subsys_key);
  3093. ss->active = 1;
  3094. /* success! */
  3095. mutex_unlock(&cgroup_mutex);
  3096. return 0;
  3097. }
  3098. EXPORT_SYMBOL_GPL(cgroup_load_subsys);
  3099. /**
  3100. * cgroup_unload_subsys: unload a modular subsystem
  3101. * @ss: the subsystem to unload
  3102. *
  3103. * This function should be called in a modular subsystem's exitcall. When this
  3104. * function is invoked, the refcount on the subsystem's module will be 0, so
  3105. * the subsystem will not be attached to any hierarchy.
  3106. */
  3107. void cgroup_unload_subsys(struct cgroup_subsys *ss)
  3108. {
  3109. struct cg_cgroup_link *link;
  3110. struct hlist_head *hhead;
  3111. BUG_ON(ss->module == NULL);
  3112. /*
  3113. * we shouldn't be called if the subsystem is in use, and the use of
  3114. * try_module_get in parse_cgroupfs_options should ensure that it
  3115. * doesn't start being used while we're killing it off.
  3116. */
  3117. BUG_ON(ss->root != &rootnode);
  3118. mutex_lock(&cgroup_mutex);
  3119. /* deassign the subsys_id */
  3120. BUG_ON(ss->subsys_id < CGROUP_BUILTIN_SUBSYS_COUNT);
  3121. subsys[ss->subsys_id] = NULL;
  3122. /* remove subsystem from rootnode's list of subsystems */
  3123. list_del(&ss->sibling);
  3124. /*
  3125. * disentangle the css from all css_sets attached to the dummytop. as
  3126. * in loading, we need to pay our respects to the hashtable gods.
  3127. */
  3128. write_lock(&css_set_lock);
  3129. list_for_each_entry(link, &dummytop->css_sets, cgrp_link_list) {
  3130. struct css_set *cg = link->cg;
  3131. hlist_del(&cg->hlist);
  3132. BUG_ON(!cg->subsys[ss->subsys_id]);
  3133. cg->subsys[ss->subsys_id] = NULL;
  3134. hhead = css_set_hash(cg->subsys);
  3135. hlist_add_head(&cg->hlist, hhead);
  3136. }
  3137. write_unlock(&css_set_lock);
  3138. /*
  3139. * remove subsystem's css from the dummytop and free it - need to free
  3140. * before marking as null because ss->destroy needs the cgrp->subsys
  3141. * pointer to find their state. note that this also takes care of
  3142. * freeing the css_id.
  3143. */
  3144. ss->destroy(ss, dummytop);
  3145. dummytop->subsys[ss->subsys_id] = NULL;
  3146. mutex_unlock(&cgroup_mutex);
  3147. }
  3148. EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
  3149. /**
  3150. * cgroup_init_early - cgroup initialization at system boot
  3151. *
  3152. * Initialize cgroups at system boot, and initialize any
  3153. * subsystems that request early init.
  3154. */
  3155. int __init cgroup_init_early(void)
  3156. {
  3157. int i;
  3158. atomic_set(&init_css_set.refcount, 1);
  3159. INIT_LIST_HEAD(&init_css_set.cg_links);
  3160. INIT_LIST_HEAD(&init_css_set.tasks);
  3161. INIT_HLIST_NODE(&init_css_set.hlist);
  3162. css_set_count = 1;
  3163. init_cgroup_root(&rootnode);
  3164. root_count = 1;
  3165. init_task.cgroups = &init_css_set;
  3166. init_css_set_link.cg = &init_css_set;
  3167. init_css_set_link.cgrp = dummytop;
  3168. list_add(&init_css_set_link.cgrp_link_list,
  3169. &rootnode.top_cgroup.css_sets);
  3170. list_add(&init_css_set_link.cg_link_list,
  3171. &init_css_set.cg_links);
  3172. for (i = 0; i < CSS_SET_TABLE_SIZE; i++)
  3173. INIT_HLIST_HEAD(&css_set_table[i]);
  3174. /* at bootup time, we don't worry about modular subsystems */
  3175. for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
  3176. struct cgroup_subsys *ss = subsys[i];
  3177. BUG_ON(!ss->name);
  3178. BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
  3179. BUG_ON(!ss->create);
  3180. BUG_ON(!ss->destroy);
  3181. if (ss->subsys_id != i) {
  3182. printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
  3183. ss->name, ss->subsys_id);
  3184. BUG();
  3185. }
  3186. if (ss->early_init)
  3187. cgroup_init_subsys(ss);
  3188. }
  3189. return 0;
  3190. }
  3191. /**
  3192. * cgroup_init - cgroup initialization
  3193. *
  3194. * Register cgroup filesystem and /proc file, and initialize
  3195. * any subsystems that didn't request early init.
  3196. */
  3197. int __init cgroup_init(void)
  3198. {
  3199. int err;
  3200. int i;
  3201. struct hlist_head *hhead;
  3202. err = bdi_init(&cgroup_backing_dev_info);
  3203. if (err)
  3204. return err;
  3205. /* at bootup time, we don't worry about modular subsystems */
  3206. for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
  3207. struct cgroup_subsys *ss = subsys[i];
  3208. if (!ss->early_init)
  3209. cgroup_init_subsys(ss);
  3210. if (ss->use_id)
  3211. cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]);
  3212. }
  3213. /* Add init_css_set to the hash table */
  3214. hhead = css_set_hash(init_css_set.subsys);
  3215. hlist_add_head(&init_css_set.hlist, hhead);
  3216. BUG_ON(!init_root_id(&rootnode));
  3217. err = register_filesystem(&cgroup_fs_type);
  3218. if (err < 0)
  3219. goto out;
  3220. proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
  3221. out:
  3222. if (err)
  3223. bdi_destroy(&cgroup_backing_dev_info);
  3224. return err;
  3225. }
  3226. /*
  3227. * proc_cgroup_show()
  3228. * - Print task's cgroup paths into seq_file, one line for each hierarchy
  3229. * - Used for /proc/<pid>/cgroup.
  3230. * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
  3231. * doesn't really matter if tsk->cgroup changes after we read it,
  3232. * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
  3233. * anyway. No need to check that tsk->cgroup != NULL, thanks to
  3234. * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
  3235. * cgroup to top_cgroup.
  3236. */
  3237. /* TODO: Use a proper seq_file iterator */
  3238. static int proc_cgroup_show(struct seq_file *m, void *v)
  3239. {
  3240. struct pid *pid;
  3241. struct task_struct *tsk;
  3242. char *buf;
  3243. int retval;
  3244. struct cgroupfs_root *root;
  3245. retval = -ENOMEM;
  3246. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  3247. if (!buf)
  3248. goto out;
  3249. retval = -ESRCH;
  3250. pid = m->private;
  3251. tsk = get_pid_task(pid, PIDTYPE_PID);
  3252. if (!tsk)
  3253. goto out_free;
  3254. retval = 0;
  3255. mutex_lock(&cgroup_mutex);
  3256. for_each_active_root(root) {
  3257. struct cgroup_subsys *ss;
  3258. struct cgroup *cgrp;
  3259. int count = 0;
  3260. seq_printf(m, "%d:", root->hierarchy_id);
  3261. for_each_subsys(root, ss)
  3262. seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
  3263. if (strlen(root->name))
  3264. seq_printf(m, "%sname=%s", count ? "," : "",
  3265. root->name);
  3266. seq_putc(m, ':');
  3267. cgrp = task_cgroup_from_root(tsk, root);
  3268. retval = cgroup_path(cgrp, buf, PAGE_SIZE);
  3269. if (retval < 0)
  3270. goto out_unlock;
  3271. seq_puts(m, buf);
  3272. seq_putc(m, '\n');
  3273. }
  3274. out_unlock:
  3275. mutex_unlock(&cgroup_mutex);
  3276. put_task_struct(tsk);
  3277. out_free:
  3278. kfree(buf);
  3279. out:
  3280. return retval;
  3281. }
  3282. static int cgroup_open(struct inode *inode, struct file *file)
  3283. {
  3284. struct pid *pid = PROC_I(inode)->pid;
  3285. return single_open(file, proc_cgroup_show, pid);
  3286. }
  3287. const struct file_operations proc_cgroup_operations = {
  3288. .open = cgroup_open,
  3289. .read = seq_read,
  3290. .llseek = seq_lseek,
  3291. .release = single_release,
  3292. };
  3293. /* Display information about each subsystem and each hierarchy */
  3294. static int proc_cgroupstats_show(struct seq_file *m, void *v)
  3295. {
  3296. int i;
  3297. seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
  3298. /*
  3299. * ideally we don't want subsystems moving around while we do this.
  3300. * cgroup_mutex is also necessary to guarantee an atomic snapshot of
  3301. * subsys/hierarchy state.
  3302. */
  3303. mutex_lock(&cgroup_mutex);
  3304. for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
  3305. struct cgroup_subsys *ss = subsys[i];
  3306. if (ss == NULL)
  3307. continue;
  3308. seq_printf(m, "%s\t%d\t%d\t%d\n",
  3309. ss->name, ss->root->hierarchy_id,
  3310. ss->root->number_of_cgroups, !ss->disabled);
  3311. }
  3312. mutex_unlock(&cgroup_mutex);
  3313. return 0;
  3314. }
  3315. static int cgroupstats_open(struct inode *inode, struct file *file)
  3316. {
  3317. return single_open(file, proc_cgroupstats_show, NULL);
  3318. }
  3319. static const struct file_operations proc_cgroupstats_operations = {
  3320. .open = cgroupstats_open,
  3321. .read = seq_read,
  3322. .llseek = seq_lseek,
  3323. .release = single_release,
  3324. };
  3325. /**
  3326. * cgroup_fork - attach newly forked task to its parents cgroup.
  3327. * @child: pointer to task_struct of forking parent process.
  3328. *
  3329. * Description: A task inherits its parent's cgroup at fork().
  3330. *
  3331. * A pointer to the shared css_set was automatically copied in
  3332. * fork.c by dup_task_struct(). However, we ignore that copy, since
  3333. * it was not made under the protection of RCU or cgroup_mutex, so
  3334. * might no longer be a valid cgroup pointer. cgroup_attach_task() might
  3335. * have already changed current->cgroups, allowing the previously
  3336. * referenced cgroup group to be removed and freed.
  3337. *
  3338. * At the point that cgroup_fork() is called, 'current' is the parent
  3339. * task, and the passed argument 'child' points to the child task.
  3340. */
  3341. void cgroup_fork(struct task_struct *child)
  3342. {
  3343. task_lock(current);
  3344. child->cgroups = current->cgroups;
  3345. get_css_set(child->cgroups);
  3346. task_unlock(current);
  3347. INIT_LIST_HEAD(&child->cg_list);
  3348. }
  3349. /**
  3350. * cgroup_fork_callbacks - run fork callbacks
  3351. * @child: the new task
  3352. *
  3353. * Called on a new task very soon before adding it to the
  3354. * tasklist. No need to take any locks since no-one can
  3355. * be operating on this task.
  3356. */
  3357. void cgroup_fork_callbacks(struct task_struct *child)
  3358. {
  3359. if (need_forkexit_callback) {
  3360. int i;
  3361. /*
  3362. * forkexit callbacks are only supported for builtin
  3363. * subsystems, and the builtin section of the subsys array is
  3364. * immutable, so we don't need to lock the subsys array here.
  3365. */
  3366. for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
  3367. struct cgroup_subsys *ss = subsys[i];
  3368. if (ss->fork)
  3369. ss->fork(ss, child);
  3370. }
  3371. }
  3372. }
  3373. /**
  3374. * cgroup_post_fork - called on a new task after adding it to the task list
  3375. * @child: the task in question
  3376. *
  3377. * Adds the task to the list running through its css_set if necessary.
  3378. * Has to be after the task is visible on the task list in case we race
  3379. * with the first call to cgroup_iter_start() - to guarantee that the
  3380. * new task ends up on its list.
  3381. */
  3382. void cgroup_post_fork(struct task_struct *child)
  3383. {
  3384. if (use_task_css_set_links) {
  3385. write_lock(&css_set_lock);
  3386. task_lock(child);
  3387. if (list_empty(&child->cg_list))
  3388. list_add(&child->cg_list, &child->cgroups->tasks);
  3389. task_unlock(child);
  3390. write_unlock(&css_set_lock);
  3391. }
  3392. }
  3393. /**
  3394. * cgroup_exit - detach cgroup from exiting task
  3395. * @tsk: pointer to task_struct of exiting process
  3396. * @run_callback: run exit callbacks?
  3397. *
  3398. * Description: Detach cgroup from @tsk and release it.
  3399. *
  3400. * Note that cgroups marked notify_on_release force every task in
  3401. * them to take the global cgroup_mutex mutex when exiting.
  3402. * This could impact scaling on very large systems. Be reluctant to
  3403. * use notify_on_release cgroups where very high task exit scaling
  3404. * is required on large systems.
  3405. *
  3406. * the_top_cgroup_hack:
  3407. *
  3408. * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
  3409. *
  3410. * We call cgroup_exit() while the task is still competent to
  3411. * handle notify_on_release(), then leave the task attached to the
  3412. * root cgroup in each hierarchy for the remainder of its exit.
  3413. *
  3414. * To do this properly, we would increment the reference count on
  3415. * top_cgroup, and near the very end of the kernel/exit.c do_exit()
  3416. * code we would add a second cgroup function call, to drop that
  3417. * reference. This would just create an unnecessary hot spot on
  3418. * the top_cgroup reference count, to no avail.
  3419. *
  3420. * Normally, holding a reference to a cgroup without bumping its
  3421. * count is unsafe. The cgroup could go away, or someone could
  3422. * attach us to a different cgroup, decrementing the count on
  3423. * the first cgroup that we never incremented. But in this case,
  3424. * top_cgroup isn't going away, and either task has PF_EXITING set,
  3425. * which wards off any cgroup_attach_task() attempts, or task is a failed
  3426. * fork, never visible to cgroup_attach_task.
  3427. */
  3428. void cgroup_exit(struct task_struct *tsk, int run_callbacks)
  3429. {
  3430. int i;
  3431. struct css_set *cg;
  3432. if (run_callbacks && need_forkexit_callback) {
  3433. /*
  3434. * modular subsystems can't use callbacks, so no need to lock
  3435. * the subsys array
  3436. */
  3437. for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
  3438. struct cgroup_subsys *ss = subsys[i];
  3439. if (ss->exit)
  3440. ss->exit(ss, tsk);
  3441. }
  3442. }
  3443. /*
  3444. * Unlink from the css_set task list if necessary.
  3445. * Optimistically check cg_list before taking
  3446. * css_set_lock
  3447. */
  3448. if (!list_empty(&tsk->cg_list)) {
  3449. write_lock(&css_set_lock);
  3450. if (!list_empty(&tsk->cg_list))
  3451. list_del(&tsk->cg_list);
  3452. write_unlock(&css_set_lock);
  3453. }
  3454. /* Reassign the task to the init_css_set. */
  3455. task_lock(tsk);
  3456. cg = tsk->cgroups;
  3457. tsk->cgroups = &init_css_set;
  3458. task_unlock(tsk);
  3459. if (cg)
  3460. put_css_set_taskexit(cg);
  3461. }
  3462. /**
  3463. * cgroup_clone - clone the cgroup the given subsystem is attached to
  3464. * @tsk: the task to be moved
  3465. * @subsys: the given subsystem
  3466. * @nodename: the name for the new cgroup
  3467. *
  3468. * Duplicate the current cgroup in the hierarchy that the given
  3469. * subsystem is attached to, and move this task into the new
  3470. * child.
  3471. */
  3472. int cgroup_clone(struct task_struct *tsk, struct cgroup_subsys *subsys,
  3473. char *nodename)
  3474. {
  3475. struct dentry *dentry;
  3476. int ret = 0;
  3477. struct cgroup *parent, *child;
  3478. struct inode *inode;
  3479. struct css_set *cg;
  3480. struct cgroupfs_root *root;
  3481. struct cgroup_subsys *ss;
  3482. /* We shouldn't be called by an unregistered subsystem */
  3483. BUG_ON(!subsys->active);
  3484. /* First figure out what hierarchy and cgroup we're dealing
  3485. * with, and pin them so we can drop cgroup_mutex */
  3486. mutex_lock(&cgroup_mutex);
  3487. again:
  3488. root = subsys->root;
  3489. if (root == &rootnode) {
  3490. mutex_unlock(&cgroup_mutex);
  3491. return 0;
  3492. }
  3493. /* Pin the hierarchy */
  3494. if (!atomic_inc_not_zero(&root->sb->s_active)) {
  3495. /* We race with the final deactivate_super() */
  3496. mutex_unlock(&cgroup_mutex);
  3497. return 0;
  3498. }
  3499. /* Keep the cgroup alive */
  3500. task_lock(tsk);
  3501. parent = task_cgroup(tsk, subsys->subsys_id);
  3502. cg = tsk->cgroups;
  3503. get_css_set(cg);
  3504. task_unlock(tsk);
  3505. mutex_unlock(&cgroup_mutex);
  3506. /* Now do the VFS work to create a cgroup */
  3507. inode = parent->dentry->d_inode;
  3508. /* Hold the parent directory mutex across this operation to
  3509. * stop anyone else deleting the new cgroup */
  3510. mutex_lock(&inode->i_mutex);
  3511. dentry = lookup_one_len(nodename, parent->dentry, strlen(nodename));
  3512. if (IS_ERR(dentry)) {
  3513. printk(KERN_INFO
  3514. "cgroup: Couldn't allocate dentry for %s: %ld\n", nodename,
  3515. PTR_ERR(dentry));
  3516. ret = PTR_ERR(dentry);
  3517. goto out_release;
  3518. }
  3519. /* Create the cgroup directory, which also creates the cgroup */
  3520. ret = vfs_mkdir(inode, dentry, 0755);
  3521. child = __d_cgrp(dentry);
  3522. dput(dentry);
  3523. if (ret) {
  3524. printk(KERN_INFO
  3525. "Failed to create cgroup %s: %d\n", nodename,
  3526. ret);
  3527. goto out_release;
  3528. }
  3529. /* The cgroup now exists. Retake cgroup_mutex and check
  3530. * that we're still in the same state that we thought we
  3531. * were. */
  3532. mutex_lock(&cgroup_mutex);
  3533. if ((root != subsys->root) ||
  3534. (parent != task_cgroup(tsk, subsys->subsys_id))) {
  3535. /* Aargh, we raced ... */
  3536. mutex_unlock(&inode->i_mutex);
  3537. put_css_set(cg);
  3538. deactivate_super(root->sb);
  3539. /* The cgroup is still accessible in the VFS, but
  3540. * we're not going to try to rmdir() it at this
  3541. * point. */
  3542. printk(KERN_INFO
  3543. "Race in cgroup_clone() - leaking cgroup %s\n",
  3544. nodename);
  3545. goto again;
  3546. }
  3547. /* do any required auto-setup */
  3548. for_each_subsys(root, ss) {
  3549. if (ss->post_clone)
  3550. ss->post_clone(ss, child);
  3551. }
  3552. /* All seems fine. Finish by moving the task into the new cgroup */
  3553. ret = cgroup_attach_task(child, tsk);
  3554. mutex_unlock(&cgroup_mutex);
  3555. out_release:
  3556. mutex_unlock(&inode->i_mutex);
  3557. mutex_lock(&cgroup_mutex);
  3558. put_css_set(cg);
  3559. mutex_unlock(&cgroup_mutex);
  3560. deactivate_super(root->sb);
  3561. return ret;
  3562. }
  3563. /**
  3564. * cgroup_is_descendant - see if @cgrp is a descendant of @task's cgrp
  3565. * @cgrp: the cgroup in question
  3566. * @task: the task in question
  3567. *
  3568. * See if @cgrp is a descendant of @task's cgroup in the appropriate
  3569. * hierarchy.
  3570. *
  3571. * If we are sending in dummytop, then presumably we are creating
  3572. * the top cgroup in the subsystem.
  3573. *
  3574. * Called only by the ns (nsproxy) cgroup.
  3575. */
  3576. int cgroup_is_descendant(const struct cgroup *cgrp, struct task_struct *task)
  3577. {
  3578. int ret;
  3579. struct cgroup *target;
  3580. if (cgrp == dummytop)
  3581. return 1;
  3582. target = task_cgroup_from_root(task, cgrp->root);
  3583. while (cgrp != target && cgrp!= cgrp->top_cgroup)
  3584. cgrp = cgrp->parent;
  3585. ret = (cgrp == target);
  3586. return ret;
  3587. }
  3588. static void check_for_release(struct cgroup *cgrp)
  3589. {
  3590. /* All of these checks rely on RCU to keep the cgroup
  3591. * structure alive */
  3592. if (cgroup_is_releasable(cgrp) && !atomic_read(&cgrp->count)
  3593. && list_empty(&cgrp->children) && !cgroup_has_css_refs(cgrp)) {
  3594. /* Control Group is currently removeable. If it's not
  3595. * already queued for a userspace notification, queue
  3596. * it now */
  3597. int need_schedule_work = 0;
  3598. spin_lock(&release_list_lock);
  3599. if (!cgroup_is_removed(cgrp) &&
  3600. list_empty(&cgrp->release_list)) {
  3601. list_add(&cgrp->release_list, &release_list);
  3602. need_schedule_work = 1;
  3603. }
  3604. spin_unlock(&release_list_lock);
  3605. if (need_schedule_work)
  3606. schedule_work(&release_agent_work);
  3607. }
  3608. }
  3609. /* Caller must verify that the css is not for root cgroup */
  3610. void __css_put(struct cgroup_subsys_state *css, int count)
  3611. {
  3612. struct cgroup *cgrp = css->cgroup;
  3613. int val;
  3614. rcu_read_lock();
  3615. val = atomic_sub_return(count, &css->refcnt);
  3616. if (val == 1) {
  3617. if (notify_on_release(cgrp)) {
  3618. set_bit(CGRP_RELEASABLE, &cgrp->flags);
  3619. check_for_release(cgrp);
  3620. }
  3621. cgroup_wakeup_rmdir_waiter(cgrp);
  3622. }
  3623. rcu_read_unlock();
  3624. WARN_ON_ONCE(val < 1);
  3625. }
  3626. /*
  3627. * Notify userspace when a cgroup is released, by running the
  3628. * configured release agent with the name of the cgroup (path
  3629. * relative to the root of cgroup file system) as the argument.
  3630. *
  3631. * Most likely, this user command will try to rmdir this cgroup.
  3632. *
  3633. * This races with the possibility that some other task will be
  3634. * attached to this cgroup before it is removed, or that some other
  3635. * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
  3636. * The presumed 'rmdir' will fail quietly if this cgroup is no longer
  3637. * unused, and this cgroup will be reprieved from its death sentence,
  3638. * to continue to serve a useful existence. Next time it's released,
  3639. * we will get notified again, if it still has 'notify_on_release' set.
  3640. *
  3641. * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
  3642. * means only wait until the task is successfully execve()'d. The
  3643. * separate release agent task is forked by call_usermodehelper(),
  3644. * then control in this thread returns here, without waiting for the
  3645. * release agent task. We don't bother to wait because the caller of
  3646. * this routine has no use for the exit status of the release agent
  3647. * task, so no sense holding our caller up for that.
  3648. */
  3649. static void cgroup_release_agent(struct work_struct *work)
  3650. {
  3651. BUG_ON(work != &release_agent_work);
  3652. mutex_lock(&cgroup_mutex);
  3653. spin_lock(&release_list_lock);
  3654. while (!list_empty(&release_list)) {
  3655. char *argv[3], *envp[3];
  3656. int i;
  3657. char *pathbuf = NULL, *agentbuf = NULL;
  3658. struct cgroup *cgrp = list_entry(release_list.next,
  3659. struct cgroup,
  3660. release_list);
  3661. list_del_init(&cgrp->release_list);
  3662. spin_unlock(&release_list_lock);
  3663. pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  3664. if (!pathbuf)
  3665. goto continue_free;
  3666. if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
  3667. goto continue_free;
  3668. agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
  3669. if (!agentbuf)
  3670. goto continue_free;
  3671. i = 0;
  3672. argv[i++] = agentbuf;
  3673. argv[i++] = pathbuf;
  3674. argv[i] = NULL;
  3675. i = 0;
  3676. /* minimal command environment */
  3677. envp[i++] = "HOME=/";
  3678. envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
  3679. envp[i] = NULL;
  3680. /* Drop the lock while we invoke the usermode helper,
  3681. * since the exec could involve hitting disk and hence
  3682. * be a slow process */
  3683. mutex_unlock(&cgroup_mutex);
  3684. call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
  3685. mutex_lock(&cgroup_mutex);
  3686. continue_free:
  3687. kfree(pathbuf);
  3688. kfree(agentbuf);
  3689. spin_lock(&release_list_lock);
  3690. }
  3691. spin_unlock(&release_list_lock);
  3692. mutex_unlock(&cgroup_mutex);
  3693. }
  3694. static int __init cgroup_disable(char *str)
  3695. {
  3696. int i;
  3697. char *token;
  3698. while ((token = strsep(&str, ",")) != NULL) {
  3699. if (!*token)
  3700. continue;
  3701. /*
  3702. * cgroup_disable, being at boot time, can't know about module
  3703. * subsystems, so we don't worry about them.
  3704. */
  3705. for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
  3706. struct cgroup_subsys *ss = subsys[i];
  3707. if (!strcmp(token, ss->name)) {
  3708. ss->disabled = 1;
  3709. printk(KERN_INFO "Disabling %s control group"
  3710. " subsystem\n", ss->name);
  3711. break;
  3712. }
  3713. }
  3714. }
  3715. return 1;
  3716. }
  3717. __setup("cgroup_disable=", cgroup_disable);
  3718. /*
  3719. * Functons for CSS ID.
  3720. */
  3721. /*
  3722. *To get ID other than 0, this should be called when !cgroup_is_removed().
  3723. */
  3724. unsigned short css_id(struct cgroup_subsys_state *css)
  3725. {
  3726. struct css_id *cssid = rcu_dereference(css->id);
  3727. if (cssid)
  3728. return cssid->id;
  3729. return 0;
  3730. }
  3731. unsigned short css_depth(struct cgroup_subsys_state *css)
  3732. {
  3733. struct css_id *cssid = rcu_dereference(css->id);
  3734. if (cssid)
  3735. return cssid->depth;
  3736. return 0;
  3737. }
  3738. bool css_is_ancestor(struct cgroup_subsys_state *child,
  3739. const struct cgroup_subsys_state *root)
  3740. {
  3741. struct css_id *child_id = rcu_dereference(child->id);
  3742. struct css_id *root_id = rcu_dereference(root->id);
  3743. if (!child_id || !root_id || (child_id->depth < root_id->depth))
  3744. return false;
  3745. return child_id->stack[root_id->depth] == root_id->id;
  3746. }
  3747. static void __free_css_id_cb(struct rcu_head *head)
  3748. {
  3749. struct css_id *id;
  3750. id = container_of(head, struct css_id, rcu_head);
  3751. kfree(id);
  3752. }
  3753. void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
  3754. {
  3755. struct css_id *id = css->id;
  3756. /* When this is called before css_id initialization, id can be NULL */
  3757. if (!id)
  3758. return;
  3759. BUG_ON(!ss->use_id);
  3760. rcu_assign_pointer(id->css, NULL);
  3761. rcu_assign_pointer(css->id, NULL);
  3762. spin_lock(&ss->id_lock);
  3763. idr_remove(&ss->idr, id->id);
  3764. spin_unlock(&ss->id_lock);
  3765. call_rcu(&id->rcu_head, __free_css_id_cb);
  3766. }
  3767. /*
  3768. * This is called by init or create(). Then, calls to this function are
  3769. * always serialized (By cgroup_mutex() at create()).
  3770. */
  3771. static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
  3772. {
  3773. struct css_id *newid;
  3774. int myid, error, size;
  3775. BUG_ON(!ss->use_id);
  3776. size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
  3777. newid = kzalloc(size, GFP_KERNEL);
  3778. if (!newid)
  3779. return ERR_PTR(-ENOMEM);
  3780. /* get id */
  3781. if (unlikely(!idr_pre_get(&ss->idr, GFP_KERNEL))) {
  3782. error = -ENOMEM;
  3783. goto err_out;
  3784. }
  3785. spin_lock(&ss->id_lock);
  3786. /* Don't use 0. allocates an ID of 1-65535 */
  3787. error = idr_get_new_above(&ss->idr, newid, 1, &myid);
  3788. spin_unlock(&ss->id_lock);
  3789. /* Returns error when there are no free spaces for new ID.*/
  3790. if (error) {
  3791. error = -ENOSPC;
  3792. goto err_out;
  3793. }
  3794. if (myid > CSS_ID_MAX)
  3795. goto remove_idr;
  3796. newid->id = myid;
  3797. newid->depth = depth;
  3798. return newid;
  3799. remove_idr:
  3800. error = -ENOSPC;
  3801. spin_lock(&ss->id_lock);
  3802. idr_remove(&ss->idr, myid);
  3803. spin_unlock(&ss->id_lock);
  3804. err_out:
  3805. kfree(newid);
  3806. return ERR_PTR(error);
  3807. }
  3808. static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss,
  3809. struct cgroup_subsys_state *rootcss)
  3810. {
  3811. struct css_id *newid;
  3812. spin_lock_init(&ss->id_lock);
  3813. idr_init(&ss->idr);
  3814. newid = get_new_cssid(ss, 0);
  3815. if (IS_ERR(newid))
  3816. return PTR_ERR(newid);
  3817. newid->stack[0] = newid->id;
  3818. newid->css = rootcss;
  3819. rootcss->id = newid;
  3820. return 0;
  3821. }
  3822. static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent,
  3823. struct cgroup *child)
  3824. {
  3825. int subsys_id, i, depth = 0;
  3826. struct cgroup_subsys_state *parent_css, *child_css;
  3827. struct css_id *child_id, *parent_id = NULL;
  3828. subsys_id = ss->subsys_id;
  3829. parent_css = parent->subsys[subsys_id];
  3830. child_css = child->subsys[subsys_id];
  3831. depth = css_depth(parent_css) + 1;
  3832. parent_id = parent_css->id;
  3833. child_id = get_new_cssid(ss, depth);
  3834. if (IS_ERR(child_id))
  3835. return PTR_ERR(child_id);
  3836. for (i = 0; i < depth; i++)
  3837. child_id->stack[i] = parent_id->stack[i];
  3838. child_id->stack[depth] = child_id->id;
  3839. /*
  3840. * child_id->css pointer will be set after this cgroup is available
  3841. * see cgroup_populate_dir()
  3842. */
  3843. rcu_assign_pointer(child_css->id, child_id);
  3844. return 0;
  3845. }
  3846. /**
  3847. * css_lookup - lookup css by id
  3848. * @ss: cgroup subsys to be looked into.
  3849. * @id: the id
  3850. *
  3851. * Returns pointer to cgroup_subsys_state if there is valid one with id.
  3852. * NULL if not. Should be called under rcu_read_lock()
  3853. */
  3854. struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
  3855. {
  3856. struct css_id *cssid = NULL;
  3857. BUG_ON(!ss->use_id);
  3858. cssid = idr_find(&ss->idr, id);
  3859. if (unlikely(!cssid))
  3860. return NULL;
  3861. return rcu_dereference(cssid->css);
  3862. }
  3863. /**
  3864. * css_get_next - lookup next cgroup under specified hierarchy.
  3865. * @ss: pointer to subsystem
  3866. * @id: current position of iteration.
  3867. * @root: pointer to css. search tree under this.
  3868. * @foundid: position of found object.
  3869. *
  3870. * Search next css under the specified hierarchy of rootid. Calling under
  3871. * rcu_read_lock() is necessary. Returns NULL if it reaches the end.
  3872. */
  3873. struct cgroup_subsys_state *
  3874. css_get_next(struct cgroup_subsys *ss, int id,
  3875. struct cgroup_subsys_state *root, int *foundid)
  3876. {
  3877. struct cgroup_subsys_state *ret = NULL;
  3878. struct css_id *tmp;
  3879. int tmpid;
  3880. int rootid = css_id(root);
  3881. int depth = css_depth(root);
  3882. if (!rootid)
  3883. return NULL;
  3884. BUG_ON(!ss->use_id);
  3885. /* fill start point for scan */
  3886. tmpid = id;
  3887. while (1) {
  3888. /*
  3889. * scan next entry from bitmap(tree), tmpid is updated after
  3890. * idr_get_next().
  3891. */
  3892. spin_lock(&ss->id_lock);
  3893. tmp = idr_get_next(&ss->idr, &tmpid);
  3894. spin_unlock(&ss->id_lock);
  3895. if (!tmp)
  3896. break;
  3897. if (tmp->depth >= depth && tmp->stack[depth] == rootid) {
  3898. ret = rcu_dereference(tmp->css);
  3899. if (ret) {
  3900. *foundid = tmpid;
  3901. break;
  3902. }
  3903. }
  3904. /* continue to scan from next id */
  3905. tmpid = tmpid + 1;
  3906. }
  3907. return ret;
  3908. }
  3909. #ifdef CONFIG_CGROUP_DEBUG
  3910. static struct cgroup_subsys_state *debug_create(struct cgroup_subsys *ss,
  3911. struct cgroup *cont)
  3912. {
  3913. struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
  3914. if (!css)
  3915. return ERR_PTR(-ENOMEM);
  3916. return css;
  3917. }
  3918. static void debug_destroy(struct cgroup_subsys *ss, struct cgroup *cont)
  3919. {
  3920. kfree(cont->subsys[debug_subsys_id]);
  3921. }
  3922. static u64 cgroup_refcount_read(struct cgroup *cont, struct cftype *cft)
  3923. {
  3924. return atomic_read(&cont->count);
  3925. }
  3926. static u64 debug_taskcount_read(struct cgroup *cont, struct cftype *cft)
  3927. {
  3928. return cgroup_task_count(cont);
  3929. }
  3930. static u64 current_css_set_read(struct cgroup *cont, struct cftype *cft)
  3931. {
  3932. return (u64)(unsigned long)current->cgroups;
  3933. }
  3934. static u64 current_css_set_refcount_read(struct cgroup *cont,
  3935. struct cftype *cft)
  3936. {
  3937. u64 count;
  3938. rcu_read_lock();
  3939. count = atomic_read(&current->cgroups->refcount);
  3940. rcu_read_unlock();
  3941. return count;
  3942. }
  3943. static int current_css_set_cg_links_read(struct cgroup *cont,
  3944. struct cftype *cft,
  3945. struct seq_file *seq)
  3946. {
  3947. struct cg_cgroup_link *link;
  3948. struct css_set *cg;
  3949. read_lock(&css_set_lock);
  3950. rcu_read_lock();
  3951. cg = rcu_dereference(current->cgroups);
  3952. list_for_each_entry(link, &cg->cg_links, cg_link_list) {
  3953. struct cgroup *c = link->cgrp;
  3954. const char *name;
  3955. if (c->dentry)
  3956. name = c->dentry->d_name.name;
  3957. else
  3958. name = "?";
  3959. seq_printf(seq, "Root %d group %s\n",
  3960. c->root->hierarchy_id, name);
  3961. }
  3962. rcu_read_unlock();
  3963. read_unlock(&css_set_lock);
  3964. return 0;
  3965. }
  3966. #define MAX_TASKS_SHOWN_PER_CSS 25
  3967. static int cgroup_css_links_read(struct cgroup *cont,
  3968. struct cftype *cft,
  3969. struct seq_file *seq)
  3970. {
  3971. struct cg_cgroup_link *link;
  3972. read_lock(&css_set_lock);
  3973. list_for_each_entry(link, &cont->css_sets, cgrp_link_list) {
  3974. struct css_set *cg = link->cg;
  3975. struct task_struct *task;
  3976. int count = 0;
  3977. seq_printf(seq, "css_set %p\n", cg);
  3978. list_for_each_entry(task, &cg->tasks, cg_list) {
  3979. if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
  3980. seq_puts(seq, " ...\n");
  3981. break;
  3982. } else {
  3983. seq_printf(seq, " task %d\n",
  3984. task_pid_vnr(task));
  3985. }
  3986. }
  3987. }
  3988. read_unlock(&css_set_lock);
  3989. return 0;
  3990. }
  3991. static u64 releasable_read(struct cgroup *cgrp, struct cftype *cft)
  3992. {
  3993. return test_bit(CGRP_RELEASABLE, &cgrp->flags);
  3994. }
  3995. static struct cftype debug_files[] = {
  3996. {
  3997. .name = "cgroup_refcount",
  3998. .read_u64 = cgroup_refcount_read,
  3999. },
  4000. {
  4001. .name = "taskcount",
  4002. .read_u64 = debug_taskcount_read,
  4003. },
  4004. {
  4005. .name = "current_css_set",
  4006. .read_u64 = current_css_set_read,
  4007. },
  4008. {
  4009. .name = "current_css_set_refcount",
  4010. .read_u64 = current_css_set_refcount_read,
  4011. },
  4012. {
  4013. .name = "current_css_set_cg_links",
  4014. .read_seq_string = current_css_set_cg_links_read,
  4015. },
  4016. {
  4017. .name = "cgroup_css_links",
  4018. .read_seq_string = cgroup_css_links_read,
  4019. },
  4020. {
  4021. .name = "releasable",
  4022. .read_u64 = releasable_read,
  4023. },
  4024. };
  4025. static int debug_populate(struct cgroup_subsys *ss, struct cgroup *cont)
  4026. {
  4027. return cgroup_add_files(cont, ss, debug_files,
  4028. ARRAY_SIZE(debug_files));
  4029. }
  4030. struct cgroup_subsys debug_subsys = {
  4031. .name = "debug",
  4032. .create = debug_create,
  4033. .destroy = debug_destroy,
  4034. .populate = debug_populate,
  4035. .subsys_id = debug_subsys_id,
  4036. };
  4037. #endif /* CONFIG_CGROUP_DEBUG */