cgroup.c 84 KB

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