cgroup.c 71 KB

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