cgroup.c 83 KB

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