cgroup.c 82 KB

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