cgroup.c 108 KB

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