cgroup.c 78 KB

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