cgroup.c 78 KB

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