cgroup.c 125 KB

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