cgroup.c 93 KB

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