cgroup.c 83 KB

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