cgroup.c 93 KB

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