cgroup.c 79 KB

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