cgroup.c 127 KB

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