cgroup.c 91 KB

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