cgroup.c 109 KB

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