cgroup.c 107 KB

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