cgroup.c 95 KB

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