cgroup.c 82 KB

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