cgroup.c 80 KB

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