cgroup.c 110 KB

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