cgroup.c 82 KB

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