cgroup.c 127 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876
  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 - 1, 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 - 1,
  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 struct kobject *cgroup_kobj;
  1417. static inline struct cgroup *__d_cgrp(struct dentry *dentry)
  1418. {
  1419. return dentry->d_fsdata;
  1420. }
  1421. static inline struct cftype *__d_cft(struct dentry *dentry)
  1422. {
  1423. return dentry->d_fsdata;
  1424. }
  1425. /**
  1426. * cgroup_path - generate the path of a cgroup
  1427. * @cgrp: the cgroup in question
  1428. * @buf: the buffer to write the path into
  1429. * @buflen: the length of the buffer
  1430. *
  1431. * Called with cgroup_mutex held or else with an RCU-protected cgroup
  1432. * reference. Writes path of cgroup into buf. Returns 0 on success,
  1433. * -errno on error.
  1434. */
  1435. int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
  1436. {
  1437. char *start;
  1438. struct dentry *dentry = rcu_dereference_check(cgrp->dentry,
  1439. rcu_read_lock_held() ||
  1440. cgroup_lock_is_held());
  1441. if (!dentry || cgrp == dummytop) {
  1442. /*
  1443. * Inactive subsystems have no dentry for their root
  1444. * cgroup
  1445. */
  1446. strcpy(buf, "/");
  1447. return 0;
  1448. }
  1449. start = buf + buflen;
  1450. *--start = '\0';
  1451. for (;;) {
  1452. int len = dentry->d_name.len;
  1453. if ((start -= len) < buf)
  1454. return -ENAMETOOLONG;
  1455. memcpy(start, dentry->d_name.name, len);
  1456. cgrp = cgrp->parent;
  1457. if (!cgrp)
  1458. break;
  1459. dentry = rcu_dereference_check(cgrp->dentry,
  1460. rcu_read_lock_held() ||
  1461. cgroup_lock_is_held());
  1462. if (!cgrp->parent)
  1463. continue;
  1464. if (--start < buf)
  1465. return -ENAMETOOLONG;
  1466. *start = '/';
  1467. }
  1468. memmove(buf, start, buf + buflen - start);
  1469. return 0;
  1470. }
  1471. EXPORT_SYMBOL_GPL(cgroup_path);
  1472. /**
  1473. * cgroup_attach_task - attach task 'tsk' to cgroup 'cgrp'
  1474. * @cgrp: the cgroup the task is attaching to
  1475. * @tsk: the task to be attached
  1476. *
  1477. * Call holding cgroup_mutex. May take task_lock of
  1478. * the task 'tsk' during call.
  1479. */
  1480. int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
  1481. {
  1482. int retval = 0;
  1483. struct cgroup_subsys *ss, *failed_ss = NULL;
  1484. struct cgroup *oldcgrp;
  1485. struct css_set *cg;
  1486. struct css_set *newcg;
  1487. struct cgroupfs_root *root = cgrp->root;
  1488. /* Nothing to do if the task is already in that cgroup */
  1489. oldcgrp = task_cgroup_from_root(tsk, root);
  1490. if (cgrp == oldcgrp)
  1491. return 0;
  1492. for_each_subsys(root, ss) {
  1493. if (ss->can_attach) {
  1494. retval = ss->can_attach(ss, cgrp, tsk, false);
  1495. if (retval) {
  1496. /*
  1497. * Remember on which subsystem the can_attach()
  1498. * failed, so that we only call cancel_attach()
  1499. * against the subsystems whose can_attach()
  1500. * succeeded. (See below)
  1501. */
  1502. failed_ss = ss;
  1503. goto out;
  1504. }
  1505. }
  1506. }
  1507. task_lock(tsk);
  1508. cg = tsk->cgroups;
  1509. get_css_set(cg);
  1510. task_unlock(tsk);
  1511. /*
  1512. * Locate or allocate a new css_set for this task,
  1513. * based on its final set of cgroups
  1514. */
  1515. newcg = find_css_set(cg, cgrp);
  1516. put_css_set(cg);
  1517. if (!newcg) {
  1518. retval = -ENOMEM;
  1519. goto out;
  1520. }
  1521. task_lock(tsk);
  1522. if (tsk->flags & PF_EXITING) {
  1523. task_unlock(tsk);
  1524. put_css_set(newcg);
  1525. retval = -ESRCH;
  1526. goto out;
  1527. }
  1528. rcu_assign_pointer(tsk->cgroups, newcg);
  1529. task_unlock(tsk);
  1530. /* Update the css_set linked lists if we're using them */
  1531. write_lock(&css_set_lock);
  1532. if (!list_empty(&tsk->cg_list)) {
  1533. list_del(&tsk->cg_list);
  1534. list_add(&tsk->cg_list, &newcg->tasks);
  1535. }
  1536. write_unlock(&css_set_lock);
  1537. for_each_subsys(root, ss) {
  1538. if (ss->attach)
  1539. ss->attach(ss, cgrp, oldcgrp, tsk, false);
  1540. }
  1541. set_bit(CGRP_RELEASABLE, &oldcgrp->flags);
  1542. synchronize_rcu();
  1543. put_css_set(cg);
  1544. /*
  1545. * wake up rmdir() waiter. the rmdir should fail since the cgroup
  1546. * is no longer empty.
  1547. */
  1548. cgroup_wakeup_rmdir_waiter(cgrp);
  1549. out:
  1550. if (retval) {
  1551. for_each_subsys(root, ss) {
  1552. if (ss == failed_ss)
  1553. /*
  1554. * This subsystem was the one that failed the
  1555. * can_attach() check earlier, so we don't need
  1556. * to call cancel_attach() against it or any
  1557. * remaining subsystems.
  1558. */
  1559. break;
  1560. if (ss->cancel_attach)
  1561. ss->cancel_attach(ss, cgrp, tsk, false);
  1562. }
  1563. }
  1564. return retval;
  1565. }
  1566. /**
  1567. * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
  1568. * @from: attach to all cgroups of a given task
  1569. * @tsk: the task to be attached
  1570. */
  1571. int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
  1572. {
  1573. struct cgroupfs_root *root;
  1574. struct cgroup *cur_cg;
  1575. int retval = 0;
  1576. cgroup_lock();
  1577. for_each_active_root(root) {
  1578. cur_cg = task_cgroup_from_root(from, root);
  1579. retval = cgroup_attach_task(cur_cg, tsk);
  1580. if (retval)
  1581. break;
  1582. }
  1583. cgroup_unlock();
  1584. return retval;
  1585. }
  1586. EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
  1587. /*
  1588. * Attach task with pid 'pid' to cgroup 'cgrp'. Call with cgroup_mutex
  1589. * held. May take task_lock of task
  1590. */
  1591. static int attach_task_by_pid(struct cgroup *cgrp, u64 pid)
  1592. {
  1593. struct task_struct *tsk;
  1594. const struct cred *cred = current_cred(), *tcred;
  1595. int ret;
  1596. if (pid) {
  1597. rcu_read_lock();
  1598. tsk = find_task_by_vpid(pid);
  1599. if (!tsk || tsk->flags & PF_EXITING) {
  1600. rcu_read_unlock();
  1601. return -ESRCH;
  1602. }
  1603. tcred = __task_cred(tsk);
  1604. if (cred->euid &&
  1605. cred->euid != tcred->uid &&
  1606. cred->euid != tcred->suid) {
  1607. rcu_read_unlock();
  1608. return -EACCES;
  1609. }
  1610. get_task_struct(tsk);
  1611. rcu_read_unlock();
  1612. } else {
  1613. tsk = current;
  1614. get_task_struct(tsk);
  1615. }
  1616. ret = cgroup_attach_task(cgrp, tsk);
  1617. put_task_struct(tsk);
  1618. return ret;
  1619. }
  1620. static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid)
  1621. {
  1622. int ret;
  1623. if (!cgroup_lock_live_group(cgrp))
  1624. return -ENODEV;
  1625. ret = attach_task_by_pid(cgrp, pid);
  1626. cgroup_unlock();
  1627. return ret;
  1628. }
  1629. /**
  1630. * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
  1631. * @cgrp: the cgroup to be checked for liveness
  1632. *
  1633. * On success, returns true; the lock should be later released with
  1634. * cgroup_unlock(). On failure returns false with no lock held.
  1635. */
  1636. bool cgroup_lock_live_group(struct cgroup *cgrp)
  1637. {
  1638. mutex_lock(&cgroup_mutex);
  1639. if (cgroup_is_removed(cgrp)) {
  1640. mutex_unlock(&cgroup_mutex);
  1641. return false;
  1642. }
  1643. return true;
  1644. }
  1645. EXPORT_SYMBOL_GPL(cgroup_lock_live_group);
  1646. static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft,
  1647. const char *buffer)
  1648. {
  1649. BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
  1650. if (!cgroup_lock_live_group(cgrp))
  1651. return -ENODEV;
  1652. strcpy(cgrp->root->release_agent_path, buffer);
  1653. cgroup_unlock();
  1654. return 0;
  1655. }
  1656. static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft,
  1657. struct seq_file *seq)
  1658. {
  1659. if (!cgroup_lock_live_group(cgrp))
  1660. return -ENODEV;
  1661. seq_puts(seq, cgrp->root->release_agent_path);
  1662. seq_putc(seq, '\n');
  1663. cgroup_unlock();
  1664. return 0;
  1665. }
  1666. /* A buffer size big enough for numbers or short strings */
  1667. #define CGROUP_LOCAL_BUFFER_SIZE 64
  1668. static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
  1669. struct file *file,
  1670. const char __user *userbuf,
  1671. size_t nbytes, loff_t *unused_ppos)
  1672. {
  1673. char buffer[CGROUP_LOCAL_BUFFER_SIZE];
  1674. int retval = 0;
  1675. char *end;
  1676. if (!nbytes)
  1677. return -EINVAL;
  1678. if (nbytes >= sizeof(buffer))
  1679. return -E2BIG;
  1680. if (copy_from_user(buffer, userbuf, nbytes))
  1681. return -EFAULT;
  1682. buffer[nbytes] = 0; /* nul-terminate */
  1683. if (cft->write_u64) {
  1684. u64 val = simple_strtoull(strstrip(buffer), &end, 0);
  1685. if (*end)
  1686. return -EINVAL;
  1687. retval = cft->write_u64(cgrp, cft, val);
  1688. } else {
  1689. s64 val = simple_strtoll(strstrip(buffer), &end, 0);
  1690. if (*end)
  1691. return -EINVAL;
  1692. retval = cft->write_s64(cgrp, cft, val);
  1693. }
  1694. if (!retval)
  1695. retval = nbytes;
  1696. return retval;
  1697. }
  1698. static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft,
  1699. struct file *file,
  1700. const char __user *userbuf,
  1701. size_t nbytes, loff_t *unused_ppos)
  1702. {
  1703. char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
  1704. int retval = 0;
  1705. size_t max_bytes = cft->max_write_len;
  1706. char *buffer = local_buffer;
  1707. if (!max_bytes)
  1708. max_bytes = sizeof(local_buffer) - 1;
  1709. if (nbytes >= max_bytes)
  1710. return -E2BIG;
  1711. /* Allocate a dynamic buffer if we need one */
  1712. if (nbytes >= sizeof(local_buffer)) {
  1713. buffer = kmalloc(nbytes + 1, GFP_KERNEL);
  1714. if (buffer == NULL)
  1715. return -ENOMEM;
  1716. }
  1717. if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
  1718. retval = -EFAULT;
  1719. goto out;
  1720. }
  1721. buffer[nbytes] = 0; /* nul-terminate */
  1722. retval = cft->write_string(cgrp, cft, strstrip(buffer));
  1723. if (!retval)
  1724. retval = nbytes;
  1725. out:
  1726. if (buffer != local_buffer)
  1727. kfree(buffer);
  1728. return retval;
  1729. }
  1730. static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
  1731. size_t nbytes, loff_t *ppos)
  1732. {
  1733. struct cftype *cft = __d_cft(file->f_dentry);
  1734. struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
  1735. if (cgroup_is_removed(cgrp))
  1736. return -ENODEV;
  1737. if (cft->write)
  1738. return cft->write(cgrp, cft, file, buf, nbytes, ppos);
  1739. if (cft->write_u64 || cft->write_s64)
  1740. return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
  1741. if (cft->write_string)
  1742. return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos);
  1743. if (cft->trigger) {
  1744. int ret = cft->trigger(cgrp, (unsigned int)cft->private);
  1745. return ret ? ret : nbytes;
  1746. }
  1747. return -EINVAL;
  1748. }
  1749. static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft,
  1750. struct file *file,
  1751. char __user *buf, size_t nbytes,
  1752. loff_t *ppos)
  1753. {
  1754. char tmp[CGROUP_LOCAL_BUFFER_SIZE];
  1755. u64 val = cft->read_u64(cgrp, cft);
  1756. int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
  1757. return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
  1758. }
  1759. static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft,
  1760. struct file *file,
  1761. char __user *buf, size_t nbytes,
  1762. loff_t *ppos)
  1763. {
  1764. char tmp[CGROUP_LOCAL_BUFFER_SIZE];
  1765. s64 val = cft->read_s64(cgrp, cft);
  1766. int len = sprintf(tmp, "%lld\n", (long long) val);
  1767. return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
  1768. }
  1769. static ssize_t cgroup_file_read(struct file *file, char __user *buf,
  1770. size_t nbytes, loff_t *ppos)
  1771. {
  1772. struct cftype *cft = __d_cft(file->f_dentry);
  1773. struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
  1774. if (cgroup_is_removed(cgrp))
  1775. return -ENODEV;
  1776. if (cft->read)
  1777. return cft->read(cgrp, cft, file, buf, nbytes, ppos);
  1778. if (cft->read_u64)
  1779. return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
  1780. if (cft->read_s64)
  1781. return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
  1782. return -EINVAL;
  1783. }
  1784. /*
  1785. * seqfile ops/methods for returning structured data. Currently just
  1786. * supports string->u64 maps, but can be extended in future.
  1787. */
  1788. struct cgroup_seqfile_state {
  1789. struct cftype *cft;
  1790. struct cgroup *cgroup;
  1791. };
  1792. static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
  1793. {
  1794. struct seq_file *sf = cb->state;
  1795. return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
  1796. }
  1797. static int cgroup_seqfile_show(struct seq_file *m, void *arg)
  1798. {
  1799. struct cgroup_seqfile_state *state = m->private;
  1800. struct cftype *cft = state->cft;
  1801. if (cft->read_map) {
  1802. struct cgroup_map_cb cb = {
  1803. .fill = cgroup_map_add,
  1804. .state = m,
  1805. };
  1806. return cft->read_map(state->cgroup, cft, &cb);
  1807. }
  1808. return cft->read_seq_string(state->cgroup, cft, m);
  1809. }
  1810. static int cgroup_seqfile_release(struct inode *inode, struct file *file)
  1811. {
  1812. struct seq_file *seq = file->private_data;
  1813. kfree(seq->private);
  1814. return single_release(inode, file);
  1815. }
  1816. static const struct file_operations cgroup_seqfile_operations = {
  1817. .read = seq_read,
  1818. .write = cgroup_file_write,
  1819. .llseek = seq_lseek,
  1820. .release = cgroup_seqfile_release,
  1821. };
  1822. static int cgroup_file_open(struct inode *inode, struct file *file)
  1823. {
  1824. int err;
  1825. struct cftype *cft;
  1826. err = generic_file_open(inode, file);
  1827. if (err)
  1828. return err;
  1829. cft = __d_cft(file->f_dentry);
  1830. if (cft->read_map || cft->read_seq_string) {
  1831. struct cgroup_seqfile_state *state =
  1832. kzalloc(sizeof(*state), GFP_USER);
  1833. if (!state)
  1834. return -ENOMEM;
  1835. state->cft = cft;
  1836. state->cgroup = __d_cgrp(file->f_dentry->d_parent);
  1837. file->f_op = &cgroup_seqfile_operations;
  1838. err = single_open(file, cgroup_seqfile_show, state);
  1839. if (err < 0)
  1840. kfree(state);
  1841. } else if (cft->open)
  1842. err = cft->open(inode, file);
  1843. else
  1844. err = 0;
  1845. return err;
  1846. }
  1847. static int cgroup_file_release(struct inode *inode, struct file *file)
  1848. {
  1849. struct cftype *cft = __d_cft(file->f_dentry);
  1850. if (cft->release)
  1851. return cft->release(inode, file);
  1852. return 0;
  1853. }
  1854. /*
  1855. * cgroup_rename - Only allow simple rename of directories in place.
  1856. */
  1857. static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
  1858. struct inode *new_dir, struct dentry *new_dentry)
  1859. {
  1860. if (!S_ISDIR(old_dentry->d_inode->i_mode))
  1861. return -ENOTDIR;
  1862. if (new_dentry->d_inode)
  1863. return -EEXIST;
  1864. if (old_dir != new_dir)
  1865. return -EIO;
  1866. return simple_rename(old_dir, old_dentry, new_dir, new_dentry);
  1867. }
  1868. static const struct file_operations cgroup_file_operations = {
  1869. .read = cgroup_file_read,
  1870. .write = cgroup_file_write,
  1871. .llseek = generic_file_llseek,
  1872. .open = cgroup_file_open,
  1873. .release = cgroup_file_release,
  1874. };
  1875. static const struct inode_operations cgroup_dir_inode_operations = {
  1876. .lookup = simple_lookup,
  1877. .mkdir = cgroup_mkdir,
  1878. .rmdir = cgroup_rmdir,
  1879. .rename = cgroup_rename,
  1880. };
  1881. /*
  1882. * Check if a file is a control file
  1883. */
  1884. static inline struct cftype *__file_cft(struct file *file)
  1885. {
  1886. if (file->f_dentry->d_inode->i_fop != &cgroup_file_operations)
  1887. return ERR_PTR(-EINVAL);
  1888. return __d_cft(file->f_dentry);
  1889. }
  1890. static int cgroup_create_file(struct dentry *dentry, mode_t mode,
  1891. struct super_block *sb)
  1892. {
  1893. static const struct dentry_operations cgroup_dops = {
  1894. .d_iput = cgroup_diput,
  1895. };
  1896. struct inode *inode;
  1897. if (!dentry)
  1898. return -ENOENT;
  1899. if (dentry->d_inode)
  1900. return -EEXIST;
  1901. inode = cgroup_new_inode(mode, sb);
  1902. if (!inode)
  1903. return -ENOMEM;
  1904. if (S_ISDIR(mode)) {
  1905. inode->i_op = &cgroup_dir_inode_operations;
  1906. inode->i_fop = &simple_dir_operations;
  1907. /* start off with i_nlink == 2 (for "." entry) */
  1908. inc_nlink(inode);
  1909. /* start with the directory inode held, so that we can
  1910. * populate it without racing with another mkdir */
  1911. mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
  1912. } else if (S_ISREG(mode)) {
  1913. inode->i_size = 0;
  1914. inode->i_fop = &cgroup_file_operations;
  1915. }
  1916. dentry->d_op = &cgroup_dops;
  1917. d_instantiate(dentry, inode);
  1918. dget(dentry); /* Extra count - pin the dentry in core */
  1919. return 0;
  1920. }
  1921. /*
  1922. * cgroup_create_dir - create a directory for an object.
  1923. * @cgrp: the cgroup we create the directory for. It must have a valid
  1924. * ->parent field. And we are going to fill its ->dentry field.
  1925. * @dentry: dentry of the new cgroup
  1926. * @mode: mode to set on new directory.
  1927. */
  1928. static int cgroup_create_dir(struct cgroup *cgrp, struct dentry *dentry,
  1929. mode_t mode)
  1930. {
  1931. struct dentry *parent;
  1932. int error = 0;
  1933. parent = cgrp->parent->dentry;
  1934. error = cgroup_create_file(dentry, S_IFDIR | mode, cgrp->root->sb);
  1935. if (!error) {
  1936. dentry->d_fsdata = cgrp;
  1937. inc_nlink(parent->d_inode);
  1938. rcu_assign_pointer(cgrp->dentry, dentry);
  1939. dget(dentry);
  1940. }
  1941. dput(dentry);
  1942. return error;
  1943. }
  1944. /**
  1945. * cgroup_file_mode - deduce file mode of a control file
  1946. * @cft: the control file in question
  1947. *
  1948. * returns cft->mode if ->mode is not 0
  1949. * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
  1950. * returns S_IRUGO if it has only a read handler
  1951. * returns S_IWUSR if it has only a write hander
  1952. */
  1953. static mode_t cgroup_file_mode(const struct cftype *cft)
  1954. {
  1955. mode_t mode = 0;
  1956. if (cft->mode)
  1957. return cft->mode;
  1958. if (cft->read || cft->read_u64 || cft->read_s64 ||
  1959. cft->read_map || cft->read_seq_string)
  1960. mode |= S_IRUGO;
  1961. if (cft->write || cft->write_u64 || cft->write_s64 ||
  1962. cft->write_string || cft->trigger)
  1963. mode |= S_IWUSR;
  1964. return mode;
  1965. }
  1966. int cgroup_add_file(struct cgroup *cgrp,
  1967. struct cgroup_subsys *subsys,
  1968. const struct cftype *cft)
  1969. {
  1970. struct dentry *dir = cgrp->dentry;
  1971. struct dentry *dentry;
  1972. int error;
  1973. mode_t mode;
  1974. char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
  1975. if (subsys && !test_bit(ROOT_NOPREFIX, &cgrp->root->flags)) {
  1976. strcpy(name, subsys->name);
  1977. strcat(name, ".");
  1978. }
  1979. strcat(name, cft->name);
  1980. BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
  1981. dentry = lookup_one_len(name, dir, strlen(name));
  1982. if (!IS_ERR(dentry)) {
  1983. mode = cgroup_file_mode(cft);
  1984. error = cgroup_create_file(dentry, mode | S_IFREG,
  1985. cgrp->root->sb);
  1986. if (!error)
  1987. dentry->d_fsdata = (void *)cft;
  1988. dput(dentry);
  1989. } else
  1990. error = PTR_ERR(dentry);
  1991. return error;
  1992. }
  1993. EXPORT_SYMBOL_GPL(cgroup_add_file);
  1994. int cgroup_add_files(struct cgroup *cgrp,
  1995. struct cgroup_subsys *subsys,
  1996. const struct cftype cft[],
  1997. int count)
  1998. {
  1999. int i, err;
  2000. for (i = 0; i < count; i++) {
  2001. err = cgroup_add_file(cgrp, subsys, &cft[i]);
  2002. if (err)
  2003. return err;
  2004. }
  2005. return 0;
  2006. }
  2007. EXPORT_SYMBOL_GPL(cgroup_add_files);
  2008. /**
  2009. * cgroup_task_count - count the number of tasks in a cgroup.
  2010. * @cgrp: the cgroup in question
  2011. *
  2012. * Return the number of tasks in the cgroup.
  2013. */
  2014. int cgroup_task_count(const struct cgroup *cgrp)
  2015. {
  2016. int count = 0;
  2017. struct cg_cgroup_link *link;
  2018. read_lock(&css_set_lock);
  2019. list_for_each_entry(link, &cgrp->css_sets, cgrp_link_list) {
  2020. count += atomic_read(&link->cg->refcount);
  2021. }
  2022. read_unlock(&css_set_lock);
  2023. return count;
  2024. }
  2025. /*
  2026. * Advance a list_head iterator. The iterator should be positioned at
  2027. * the start of a css_set
  2028. */
  2029. static void cgroup_advance_iter(struct cgroup *cgrp,
  2030. struct cgroup_iter *it)
  2031. {
  2032. struct list_head *l = it->cg_link;
  2033. struct cg_cgroup_link *link;
  2034. struct css_set *cg;
  2035. /* Advance to the next non-empty css_set */
  2036. do {
  2037. l = l->next;
  2038. if (l == &cgrp->css_sets) {
  2039. it->cg_link = NULL;
  2040. return;
  2041. }
  2042. link = list_entry(l, struct cg_cgroup_link, cgrp_link_list);
  2043. cg = link->cg;
  2044. } while (list_empty(&cg->tasks));
  2045. it->cg_link = l;
  2046. it->task = cg->tasks.next;
  2047. }
  2048. /*
  2049. * To reduce the fork() overhead for systems that are not actually
  2050. * using their cgroups capability, we don't maintain the lists running
  2051. * through each css_set to its tasks until we see the list actually
  2052. * used - in other words after the first call to cgroup_iter_start().
  2053. *
  2054. * The tasklist_lock is not held here, as do_each_thread() and
  2055. * while_each_thread() are protected by RCU.
  2056. */
  2057. static void cgroup_enable_task_cg_lists(void)
  2058. {
  2059. struct task_struct *p, *g;
  2060. write_lock(&css_set_lock);
  2061. use_task_css_set_links = 1;
  2062. do_each_thread(g, p) {
  2063. task_lock(p);
  2064. /*
  2065. * We should check if the process is exiting, otherwise
  2066. * it will race with cgroup_exit() in that the list
  2067. * entry won't be deleted though the process has exited.
  2068. */
  2069. if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
  2070. list_add(&p->cg_list, &p->cgroups->tasks);
  2071. task_unlock(p);
  2072. } while_each_thread(g, p);
  2073. write_unlock(&css_set_lock);
  2074. }
  2075. void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
  2076. {
  2077. /*
  2078. * The first time anyone tries to iterate across a cgroup,
  2079. * we need to enable the list linking each css_set to its
  2080. * tasks, and fix up all existing tasks.
  2081. */
  2082. if (!use_task_css_set_links)
  2083. cgroup_enable_task_cg_lists();
  2084. read_lock(&css_set_lock);
  2085. it->cg_link = &cgrp->css_sets;
  2086. cgroup_advance_iter(cgrp, it);
  2087. }
  2088. struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
  2089. struct cgroup_iter *it)
  2090. {
  2091. struct task_struct *res;
  2092. struct list_head *l = it->task;
  2093. struct cg_cgroup_link *link;
  2094. /* If the iterator cg is NULL, we have no tasks */
  2095. if (!it->cg_link)
  2096. return NULL;
  2097. res = list_entry(l, struct task_struct, cg_list);
  2098. /* Advance iterator to find next entry */
  2099. l = l->next;
  2100. link = list_entry(it->cg_link, struct cg_cgroup_link, cgrp_link_list);
  2101. if (l == &link->cg->tasks) {
  2102. /* We reached the end of this task list - move on to
  2103. * the next cg_cgroup_link */
  2104. cgroup_advance_iter(cgrp, it);
  2105. } else {
  2106. it->task = l;
  2107. }
  2108. return res;
  2109. }
  2110. void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
  2111. {
  2112. read_unlock(&css_set_lock);
  2113. }
  2114. static inline int started_after_time(struct task_struct *t1,
  2115. struct timespec *time,
  2116. struct task_struct *t2)
  2117. {
  2118. int start_diff = timespec_compare(&t1->start_time, time);
  2119. if (start_diff > 0) {
  2120. return 1;
  2121. } else if (start_diff < 0) {
  2122. return 0;
  2123. } else {
  2124. /*
  2125. * Arbitrarily, if two processes started at the same
  2126. * time, we'll say that the lower pointer value
  2127. * started first. Note that t2 may have exited by now
  2128. * so this may not be a valid pointer any longer, but
  2129. * that's fine - it still serves to distinguish
  2130. * between two tasks started (effectively) simultaneously.
  2131. */
  2132. return t1 > t2;
  2133. }
  2134. }
  2135. /*
  2136. * This function is a callback from heap_insert() and is used to order
  2137. * the heap.
  2138. * In this case we order the heap in descending task start time.
  2139. */
  2140. static inline int started_after(void *p1, void *p2)
  2141. {
  2142. struct task_struct *t1 = p1;
  2143. struct task_struct *t2 = p2;
  2144. return started_after_time(t1, &t2->start_time, t2);
  2145. }
  2146. /**
  2147. * cgroup_scan_tasks - iterate though all the tasks in a cgroup
  2148. * @scan: struct cgroup_scanner containing arguments for the scan
  2149. *
  2150. * Arguments include pointers to callback functions test_task() and
  2151. * process_task().
  2152. * Iterate through all the tasks in a cgroup, calling test_task() for each,
  2153. * and if it returns true, call process_task() for it also.
  2154. * The test_task pointer may be NULL, meaning always true (select all tasks).
  2155. * Effectively duplicates cgroup_iter_{start,next,end}()
  2156. * but does not lock css_set_lock for the call to process_task().
  2157. * The struct cgroup_scanner may be embedded in any structure of the caller's
  2158. * creation.
  2159. * It is guaranteed that process_task() will act on every task that
  2160. * is a member of the cgroup for the duration of this call. This
  2161. * function may or may not call process_task() for tasks that exit
  2162. * or move to a different cgroup during the call, or are forked or
  2163. * move into the cgroup during the call.
  2164. *
  2165. * Note that test_task() may be called with locks held, and may in some
  2166. * situations be called multiple times for the same task, so it should
  2167. * be cheap.
  2168. * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
  2169. * pre-allocated and will be used for heap operations (and its "gt" member will
  2170. * be overwritten), else a temporary heap will be used (allocation of which
  2171. * may cause this function to fail).
  2172. */
  2173. int cgroup_scan_tasks(struct cgroup_scanner *scan)
  2174. {
  2175. int retval, i;
  2176. struct cgroup_iter it;
  2177. struct task_struct *p, *dropped;
  2178. /* Never dereference latest_task, since it's not refcounted */
  2179. struct task_struct *latest_task = NULL;
  2180. struct ptr_heap tmp_heap;
  2181. struct ptr_heap *heap;
  2182. struct timespec latest_time = { 0, 0 };
  2183. if (scan->heap) {
  2184. /* The caller supplied our heap and pre-allocated its memory */
  2185. heap = scan->heap;
  2186. heap->gt = &started_after;
  2187. } else {
  2188. /* We need to allocate our own heap memory */
  2189. heap = &tmp_heap;
  2190. retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
  2191. if (retval)
  2192. /* cannot allocate the heap */
  2193. return retval;
  2194. }
  2195. again:
  2196. /*
  2197. * Scan tasks in the cgroup, using the scanner's "test_task" callback
  2198. * to determine which are of interest, and using the scanner's
  2199. * "process_task" callback to process any of them that need an update.
  2200. * Since we don't want to hold any locks during the task updates,
  2201. * gather tasks to be processed in a heap structure.
  2202. * The heap is sorted by descending task start time.
  2203. * If the statically-sized heap fills up, we overflow tasks that
  2204. * started later, and in future iterations only consider tasks that
  2205. * started after the latest task in the previous pass. This
  2206. * guarantees forward progress and that we don't miss any tasks.
  2207. */
  2208. heap->size = 0;
  2209. cgroup_iter_start(scan->cg, &it);
  2210. while ((p = cgroup_iter_next(scan->cg, &it))) {
  2211. /*
  2212. * Only affect tasks that qualify per the caller's callback,
  2213. * if he provided one
  2214. */
  2215. if (scan->test_task && !scan->test_task(p, scan))
  2216. continue;
  2217. /*
  2218. * Only process tasks that started after the last task
  2219. * we processed
  2220. */
  2221. if (!started_after_time(p, &latest_time, latest_task))
  2222. continue;
  2223. dropped = heap_insert(heap, p);
  2224. if (dropped == NULL) {
  2225. /*
  2226. * The new task was inserted; the heap wasn't
  2227. * previously full
  2228. */
  2229. get_task_struct(p);
  2230. } else if (dropped != p) {
  2231. /*
  2232. * The new task was inserted, and pushed out a
  2233. * different task
  2234. */
  2235. get_task_struct(p);
  2236. put_task_struct(dropped);
  2237. }
  2238. /*
  2239. * Else the new task was newer than anything already in
  2240. * the heap and wasn't inserted
  2241. */
  2242. }
  2243. cgroup_iter_end(scan->cg, &it);
  2244. if (heap->size) {
  2245. for (i = 0; i < heap->size; i++) {
  2246. struct task_struct *q = heap->ptrs[i];
  2247. if (i == 0) {
  2248. latest_time = q->start_time;
  2249. latest_task = q;
  2250. }
  2251. /* Process the task per the caller's callback */
  2252. scan->process_task(q, scan);
  2253. put_task_struct(q);
  2254. }
  2255. /*
  2256. * If we had to process any tasks at all, scan again
  2257. * in case some of them were in the middle of forking
  2258. * children that didn't get processed.
  2259. * Not the most efficient way to do it, but it avoids
  2260. * having to take callback_mutex in the fork path
  2261. */
  2262. goto again;
  2263. }
  2264. if (heap == &tmp_heap)
  2265. heap_free(&tmp_heap);
  2266. return 0;
  2267. }
  2268. /*
  2269. * Stuff for reading the 'tasks'/'procs' files.
  2270. *
  2271. * Reading this file can return large amounts of data if a cgroup has
  2272. * *lots* of attached tasks. So it may need several calls to read(),
  2273. * but we cannot guarantee that the information we produce is correct
  2274. * unless we produce it entirely atomically.
  2275. *
  2276. */
  2277. /*
  2278. * The following two functions "fix" the issue where there are more pids
  2279. * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
  2280. * TODO: replace with a kernel-wide solution to this problem
  2281. */
  2282. #define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
  2283. static void *pidlist_allocate(int count)
  2284. {
  2285. if (PIDLIST_TOO_LARGE(count))
  2286. return vmalloc(count * sizeof(pid_t));
  2287. else
  2288. return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
  2289. }
  2290. static void pidlist_free(void *p)
  2291. {
  2292. if (is_vmalloc_addr(p))
  2293. vfree(p);
  2294. else
  2295. kfree(p);
  2296. }
  2297. static void *pidlist_resize(void *p, int newcount)
  2298. {
  2299. void *newlist;
  2300. /* note: if new alloc fails, old p will still be valid either way */
  2301. if (is_vmalloc_addr(p)) {
  2302. newlist = vmalloc(newcount * sizeof(pid_t));
  2303. if (!newlist)
  2304. return NULL;
  2305. memcpy(newlist, p, newcount * sizeof(pid_t));
  2306. vfree(p);
  2307. } else {
  2308. newlist = krealloc(p, newcount * sizeof(pid_t), GFP_KERNEL);
  2309. }
  2310. return newlist;
  2311. }
  2312. /*
  2313. * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
  2314. * If the new stripped list is sufficiently smaller and there's enough memory
  2315. * to allocate a new buffer, will let go of the unneeded memory. Returns the
  2316. * number of unique elements.
  2317. */
  2318. /* is the size difference enough that we should re-allocate the array? */
  2319. #define PIDLIST_REALLOC_DIFFERENCE(old, new) ((old) - PAGE_SIZE >= (new))
  2320. static int pidlist_uniq(pid_t **p, int length)
  2321. {
  2322. int src, dest = 1;
  2323. pid_t *list = *p;
  2324. pid_t *newlist;
  2325. /*
  2326. * we presume the 0th element is unique, so i starts at 1. trivial
  2327. * edge cases first; no work needs to be done for either
  2328. */
  2329. if (length == 0 || length == 1)
  2330. return length;
  2331. /* src and dest walk down the list; dest counts unique elements */
  2332. for (src = 1; src < length; src++) {
  2333. /* find next unique element */
  2334. while (list[src] == list[src-1]) {
  2335. src++;
  2336. if (src == length)
  2337. goto after;
  2338. }
  2339. /* dest always points to where the next unique element goes */
  2340. list[dest] = list[src];
  2341. dest++;
  2342. }
  2343. after:
  2344. /*
  2345. * if the length difference is large enough, we want to allocate a
  2346. * smaller buffer to save memory. if this fails due to out of memory,
  2347. * we'll just stay with what we've got.
  2348. */
  2349. if (PIDLIST_REALLOC_DIFFERENCE(length, dest)) {
  2350. newlist = pidlist_resize(list, dest);
  2351. if (newlist)
  2352. *p = newlist;
  2353. }
  2354. return dest;
  2355. }
  2356. static int cmppid(const void *a, const void *b)
  2357. {
  2358. return *(pid_t *)a - *(pid_t *)b;
  2359. }
  2360. /*
  2361. * find the appropriate pidlist for our purpose (given procs vs tasks)
  2362. * returns with the lock on that pidlist already held, and takes care
  2363. * of the use count, or returns NULL with no locks held if we're out of
  2364. * memory.
  2365. */
  2366. static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
  2367. enum cgroup_filetype type)
  2368. {
  2369. struct cgroup_pidlist *l;
  2370. /* don't need task_nsproxy() if we're looking at ourself */
  2371. struct pid_namespace *ns = current->nsproxy->pid_ns;
  2372. /*
  2373. * We can't drop the pidlist_mutex before taking the l->mutex in case
  2374. * the last ref-holder is trying to remove l from the list at the same
  2375. * time. Holding the pidlist_mutex precludes somebody taking whichever
  2376. * list we find out from under us - compare release_pid_array().
  2377. */
  2378. mutex_lock(&cgrp->pidlist_mutex);
  2379. list_for_each_entry(l, &cgrp->pidlists, links) {
  2380. if (l->key.type == type && l->key.ns == ns) {
  2381. /* make sure l doesn't vanish out from under us */
  2382. down_write(&l->mutex);
  2383. mutex_unlock(&cgrp->pidlist_mutex);
  2384. return l;
  2385. }
  2386. }
  2387. /* entry not found; create a new one */
  2388. l = kmalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
  2389. if (!l) {
  2390. mutex_unlock(&cgrp->pidlist_mutex);
  2391. return l;
  2392. }
  2393. init_rwsem(&l->mutex);
  2394. down_write(&l->mutex);
  2395. l->key.type = type;
  2396. l->key.ns = get_pid_ns(ns);
  2397. l->use_count = 0; /* don't increment here */
  2398. l->list = NULL;
  2399. l->owner = cgrp;
  2400. list_add(&l->links, &cgrp->pidlists);
  2401. mutex_unlock(&cgrp->pidlist_mutex);
  2402. return l;
  2403. }
  2404. /*
  2405. * Load a cgroup's pidarray with either procs' tgids or tasks' pids
  2406. */
  2407. static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
  2408. struct cgroup_pidlist **lp)
  2409. {
  2410. pid_t *array;
  2411. int length;
  2412. int pid, n = 0; /* used for populating the array */
  2413. struct cgroup_iter it;
  2414. struct task_struct *tsk;
  2415. struct cgroup_pidlist *l;
  2416. /*
  2417. * If cgroup gets more users after we read count, we won't have
  2418. * enough space - tough. This race is indistinguishable to the
  2419. * caller from the case that the additional cgroup users didn't
  2420. * show up until sometime later on.
  2421. */
  2422. length = cgroup_task_count(cgrp);
  2423. array = pidlist_allocate(length);
  2424. if (!array)
  2425. return -ENOMEM;
  2426. /* now, populate the array */
  2427. cgroup_iter_start(cgrp, &it);
  2428. while ((tsk = cgroup_iter_next(cgrp, &it))) {
  2429. if (unlikely(n == length))
  2430. break;
  2431. /* get tgid or pid for procs or tasks file respectively */
  2432. if (type == CGROUP_FILE_PROCS)
  2433. pid = task_tgid_vnr(tsk);
  2434. else
  2435. pid = task_pid_vnr(tsk);
  2436. if (pid > 0) /* make sure to only use valid results */
  2437. array[n++] = pid;
  2438. }
  2439. cgroup_iter_end(cgrp, &it);
  2440. length = n;
  2441. /* now sort & (if procs) strip out duplicates */
  2442. sort(array, length, sizeof(pid_t), cmppid, NULL);
  2443. if (type == CGROUP_FILE_PROCS)
  2444. length = pidlist_uniq(&array, length);
  2445. l = cgroup_pidlist_find(cgrp, type);
  2446. if (!l) {
  2447. pidlist_free(array);
  2448. return -ENOMEM;
  2449. }
  2450. /* store array, freeing old if necessary - lock already held */
  2451. pidlist_free(l->list);
  2452. l->list = array;
  2453. l->length = length;
  2454. l->use_count++;
  2455. up_write(&l->mutex);
  2456. *lp = l;
  2457. return 0;
  2458. }
  2459. /**
  2460. * cgroupstats_build - build and fill cgroupstats
  2461. * @stats: cgroupstats to fill information into
  2462. * @dentry: A dentry entry belonging to the cgroup for which stats have
  2463. * been requested.
  2464. *
  2465. * Build and fill cgroupstats so that taskstats can export it to user
  2466. * space.
  2467. */
  2468. int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
  2469. {
  2470. int ret = -EINVAL;
  2471. struct cgroup *cgrp;
  2472. struct cgroup_iter it;
  2473. struct task_struct *tsk;
  2474. /*
  2475. * Validate dentry by checking the superblock operations,
  2476. * and make sure it's a directory.
  2477. */
  2478. if (dentry->d_sb->s_op != &cgroup_ops ||
  2479. !S_ISDIR(dentry->d_inode->i_mode))
  2480. goto err;
  2481. ret = 0;
  2482. cgrp = dentry->d_fsdata;
  2483. cgroup_iter_start(cgrp, &it);
  2484. while ((tsk = cgroup_iter_next(cgrp, &it))) {
  2485. switch (tsk->state) {
  2486. case TASK_RUNNING:
  2487. stats->nr_running++;
  2488. break;
  2489. case TASK_INTERRUPTIBLE:
  2490. stats->nr_sleeping++;
  2491. break;
  2492. case TASK_UNINTERRUPTIBLE:
  2493. stats->nr_uninterruptible++;
  2494. break;
  2495. case TASK_STOPPED:
  2496. stats->nr_stopped++;
  2497. break;
  2498. default:
  2499. if (delayacct_is_task_waiting_on_io(tsk))
  2500. stats->nr_io_wait++;
  2501. break;
  2502. }
  2503. }
  2504. cgroup_iter_end(cgrp, &it);
  2505. err:
  2506. return ret;
  2507. }
  2508. /*
  2509. * seq_file methods for the tasks/procs files. The seq_file position is the
  2510. * next pid to display; the seq_file iterator is a pointer to the pid
  2511. * in the cgroup->l->list array.
  2512. */
  2513. static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
  2514. {
  2515. /*
  2516. * Initially we receive a position value that corresponds to
  2517. * one more than the last pid shown (or 0 on the first call or
  2518. * after a seek to the start). Use a binary-search to find the
  2519. * next pid to display, if any
  2520. */
  2521. struct cgroup_pidlist *l = s->private;
  2522. int index = 0, pid = *pos;
  2523. int *iter;
  2524. down_read(&l->mutex);
  2525. if (pid) {
  2526. int end = l->length;
  2527. while (index < end) {
  2528. int mid = (index + end) / 2;
  2529. if (l->list[mid] == pid) {
  2530. index = mid;
  2531. break;
  2532. } else if (l->list[mid] <= pid)
  2533. index = mid + 1;
  2534. else
  2535. end = mid;
  2536. }
  2537. }
  2538. /* If we're off the end of the array, we're done */
  2539. if (index >= l->length)
  2540. return NULL;
  2541. /* Update the abstract position to be the actual pid that we found */
  2542. iter = l->list + index;
  2543. *pos = *iter;
  2544. return iter;
  2545. }
  2546. static void cgroup_pidlist_stop(struct seq_file *s, void *v)
  2547. {
  2548. struct cgroup_pidlist *l = s->private;
  2549. up_read(&l->mutex);
  2550. }
  2551. static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
  2552. {
  2553. struct cgroup_pidlist *l = s->private;
  2554. pid_t *p = v;
  2555. pid_t *end = l->list + l->length;
  2556. /*
  2557. * Advance to the next pid in the array. If this goes off the
  2558. * end, we're done
  2559. */
  2560. p++;
  2561. if (p >= end) {
  2562. return NULL;
  2563. } else {
  2564. *pos = *p;
  2565. return p;
  2566. }
  2567. }
  2568. static int cgroup_pidlist_show(struct seq_file *s, void *v)
  2569. {
  2570. return seq_printf(s, "%d\n", *(int *)v);
  2571. }
  2572. /*
  2573. * seq_operations functions for iterating on pidlists through seq_file -
  2574. * independent of whether it's tasks or procs
  2575. */
  2576. static const struct seq_operations cgroup_pidlist_seq_operations = {
  2577. .start = cgroup_pidlist_start,
  2578. .stop = cgroup_pidlist_stop,
  2579. .next = cgroup_pidlist_next,
  2580. .show = cgroup_pidlist_show,
  2581. };
  2582. static void cgroup_release_pid_array(struct cgroup_pidlist *l)
  2583. {
  2584. /*
  2585. * the case where we're the last user of this particular pidlist will
  2586. * have us remove it from the cgroup's list, which entails taking the
  2587. * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
  2588. * pidlist_mutex, we have to take pidlist_mutex first.
  2589. */
  2590. mutex_lock(&l->owner->pidlist_mutex);
  2591. down_write(&l->mutex);
  2592. BUG_ON(!l->use_count);
  2593. if (!--l->use_count) {
  2594. /* we're the last user if refcount is 0; remove and free */
  2595. list_del(&l->links);
  2596. mutex_unlock(&l->owner->pidlist_mutex);
  2597. pidlist_free(l->list);
  2598. put_pid_ns(l->key.ns);
  2599. up_write(&l->mutex);
  2600. kfree(l);
  2601. return;
  2602. }
  2603. mutex_unlock(&l->owner->pidlist_mutex);
  2604. up_write(&l->mutex);
  2605. }
  2606. static int cgroup_pidlist_release(struct inode *inode, struct file *file)
  2607. {
  2608. struct cgroup_pidlist *l;
  2609. if (!(file->f_mode & FMODE_READ))
  2610. return 0;
  2611. /*
  2612. * the seq_file will only be initialized if the file was opened for
  2613. * reading; hence we check if it's not null only in that case.
  2614. */
  2615. l = ((struct seq_file *)file->private_data)->private;
  2616. cgroup_release_pid_array(l);
  2617. return seq_release(inode, file);
  2618. }
  2619. static const struct file_operations cgroup_pidlist_operations = {
  2620. .read = seq_read,
  2621. .llseek = seq_lseek,
  2622. .write = cgroup_file_write,
  2623. .release = cgroup_pidlist_release,
  2624. };
  2625. /*
  2626. * The following functions handle opens on a file that displays a pidlist
  2627. * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
  2628. * in the cgroup.
  2629. */
  2630. /* helper function for the two below it */
  2631. static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
  2632. {
  2633. struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
  2634. struct cgroup_pidlist *l;
  2635. int retval;
  2636. /* Nothing to do for write-only files */
  2637. if (!(file->f_mode & FMODE_READ))
  2638. return 0;
  2639. /* have the array populated */
  2640. retval = pidlist_array_load(cgrp, type, &l);
  2641. if (retval)
  2642. return retval;
  2643. /* configure file information */
  2644. file->f_op = &cgroup_pidlist_operations;
  2645. retval = seq_open(file, &cgroup_pidlist_seq_operations);
  2646. if (retval) {
  2647. cgroup_release_pid_array(l);
  2648. return retval;
  2649. }
  2650. ((struct seq_file *)file->private_data)->private = l;
  2651. return 0;
  2652. }
  2653. static int cgroup_tasks_open(struct inode *unused, struct file *file)
  2654. {
  2655. return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
  2656. }
  2657. static int cgroup_procs_open(struct inode *unused, struct file *file)
  2658. {
  2659. return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
  2660. }
  2661. static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
  2662. struct cftype *cft)
  2663. {
  2664. return notify_on_release(cgrp);
  2665. }
  2666. static int cgroup_write_notify_on_release(struct cgroup *cgrp,
  2667. struct cftype *cft,
  2668. u64 val)
  2669. {
  2670. clear_bit(CGRP_RELEASABLE, &cgrp->flags);
  2671. if (val)
  2672. set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
  2673. else
  2674. clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
  2675. return 0;
  2676. }
  2677. /*
  2678. * Unregister event and free resources.
  2679. *
  2680. * Gets called from workqueue.
  2681. */
  2682. static void cgroup_event_remove(struct work_struct *work)
  2683. {
  2684. struct cgroup_event *event = container_of(work, struct cgroup_event,
  2685. remove);
  2686. struct cgroup *cgrp = event->cgrp;
  2687. event->cft->unregister_event(cgrp, event->cft, event->eventfd);
  2688. eventfd_ctx_put(event->eventfd);
  2689. kfree(event);
  2690. dput(cgrp->dentry);
  2691. }
  2692. /*
  2693. * Gets called on POLLHUP on eventfd when user closes it.
  2694. *
  2695. * Called with wqh->lock held and interrupts disabled.
  2696. */
  2697. static int cgroup_event_wake(wait_queue_t *wait, unsigned mode,
  2698. int sync, void *key)
  2699. {
  2700. struct cgroup_event *event = container_of(wait,
  2701. struct cgroup_event, wait);
  2702. struct cgroup *cgrp = event->cgrp;
  2703. unsigned long flags = (unsigned long)key;
  2704. if (flags & POLLHUP) {
  2705. __remove_wait_queue(event->wqh, &event->wait);
  2706. spin_lock(&cgrp->event_list_lock);
  2707. list_del(&event->list);
  2708. spin_unlock(&cgrp->event_list_lock);
  2709. /*
  2710. * We are in atomic context, but cgroup_event_remove() may
  2711. * sleep, so we have to call it in workqueue.
  2712. */
  2713. schedule_work(&event->remove);
  2714. }
  2715. return 0;
  2716. }
  2717. static void cgroup_event_ptable_queue_proc(struct file *file,
  2718. wait_queue_head_t *wqh, poll_table *pt)
  2719. {
  2720. struct cgroup_event *event = container_of(pt,
  2721. struct cgroup_event, pt);
  2722. event->wqh = wqh;
  2723. add_wait_queue(wqh, &event->wait);
  2724. }
  2725. /*
  2726. * Parse input and register new cgroup event handler.
  2727. *
  2728. * Input must be in format '<event_fd> <control_fd> <args>'.
  2729. * Interpretation of args is defined by control file implementation.
  2730. */
  2731. static int cgroup_write_event_control(struct cgroup *cgrp, struct cftype *cft,
  2732. const char *buffer)
  2733. {
  2734. struct cgroup_event *event = NULL;
  2735. unsigned int efd, cfd;
  2736. struct file *efile = NULL;
  2737. struct file *cfile = NULL;
  2738. char *endp;
  2739. int ret;
  2740. efd = simple_strtoul(buffer, &endp, 10);
  2741. if (*endp != ' ')
  2742. return -EINVAL;
  2743. buffer = endp + 1;
  2744. cfd = simple_strtoul(buffer, &endp, 10);
  2745. if ((*endp != ' ') && (*endp != '\0'))
  2746. return -EINVAL;
  2747. buffer = endp + 1;
  2748. event = kzalloc(sizeof(*event), GFP_KERNEL);
  2749. if (!event)
  2750. return -ENOMEM;
  2751. event->cgrp = cgrp;
  2752. INIT_LIST_HEAD(&event->list);
  2753. init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc);
  2754. init_waitqueue_func_entry(&event->wait, cgroup_event_wake);
  2755. INIT_WORK(&event->remove, cgroup_event_remove);
  2756. efile = eventfd_fget(efd);
  2757. if (IS_ERR(efile)) {
  2758. ret = PTR_ERR(efile);
  2759. goto fail;
  2760. }
  2761. event->eventfd = eventfd_ctx_fileget(efile);
  2762. if (IS_ERR(event->eventfd)) {
  2763. ret = PTR_ERR(event->eventfd);
  2764. goto fail;
  2765. }
  2766. cfile = fget(cfd);
  2767. if (!cfile) {
  2768. ret = -EBADF;
  2769. goto fail;
  2770. }
  2771. /* the process need read permission on control file */
  2772. ret = file_permission(cfile, MAY_READ);
  2773. if (ret < 0)
  2774. goto fail;
  2775. event->cft = __file_cft(cfile);
  2776. if (IS_ERR(event->cft)) {
  2777. ret = PTR_ERR(event->cft);
  2778. goto fail;
  2779. }
  2780. if (!event->cft->register_event || !event->cft->unregister_event) {
  2781. ret = -EINVAL;
  2782. goto fail;
  2783. }
  2784. ret = event->cft->register_event(cgrp, event->cft,
  2785. event->eventfd, buffer);
  2786. if (ret)
  2787. goto fail;
  2788. if (efile->f_op->poll(efile, &event->pt) & POLLHUP) {
  2789. event->cft->unregister_event(cgrp, event->cft, event->eventfd);
  2790. ret = 0;
  2791. goto fail;
  2792. }
  2793. /*
  2794. * Events should be removed after rmdir of cgroup directory, but before
  2795. * destroying subsystem state objects. Let's take reference to cgroup
  2796. * directory dentry to do that.
  2797. */
  2798. dget(cgrp->dentry);
  2799. spin_lock(&cgrp->event_list_lock);
  2800. list_add(&event->list, &cgrp->event_list);
  2801. spin_unlock(&cgrp->event_list_lock);
  2802. fput(cfile);
  2803. fput(efile);
  2804. return 0;
  2805. fail:
  2806. if (cfile)
  2807. fput(cfile);
  2808. if (event && event->eventfd && !IS_ERR(event->eventfd))
  2809. eventfd_ctx_put(event->eventfd);
  2810. if (!IS_ERR_OR_NULL(efile))
  2811. fput(efile);
  2812. kfree(event);
  2813. return ret;
  2814. }
  2815. /*
  2816. * for the common functions, 'private' gives the type of file
  2817. */
  2818. /* for hysterical raisins, we can't put this on the older files */
  2819. #define CGROUP_FILE_GENERIC_PREFIX "cgroup."
  2820. static struct cftype files[] = {
  2821. {
  2822. .name = "tasks",
  2823. .open = cgroup_tasks_open,
  2824. .write_u64 = cgroup_tasks_write,
  2825. .release = cgroup_pidlist_release,
  2826. .mode = S_IRUGO | S_IWUSR,
  2827. },
  2828. {
  2829. .name = CGROUP_FILE_GENERIC_PREFIX "procs",
  2830. .open = cgroup_procs_open,
  2831. /* .write_u64 = cgroup_procs_write, TODO */
  2832. .release = cgroup_pidlist_release,
  2833. .mode = S_IRUGO,
  2834. },
  2835. {
  2836. .name = "notify_on_release",
  2837. .read_u64 = cgroup_read_notify_on_release,
  2838. .write_u64 = cgroup_write_notify_on_release,
  2839. },
  2840. {
  2841. .name = CGROUP_FILE_GENERIC_PREFIX "event_control",
  2842. .write_string = cgroup_write_event_control,
  2843. .mode = S_IWUGO,
  2844. },
  2845. };
  2846. static struct cftype cft_release_agent = {
  2847. .name = "release_agent",
  2848. .read_seq_string = cgroup_release_agent_show,
  2849. .write_string = cgroup_release_agent_write,
  2850. .max_write_len = PATH_MAX,
  2851. };
  2852. static int cgroup_populate_dir(struct cgroup *cgrp)
  2853. {
  2854. int err;
  2855. struct cgroup_subsys *ss;
  2856. /* First clear out any existing files */
  2857. cgroup_clear_directory(cgrp->dentry);
  2858. err = cgroup_add_files(cgrp, NULL, files, ARRAY_SIZE(files));
  2859. if (err < 0)
  2860. return err;
  2861. if (cgrp == cgrp->top_cgroup) {
  2862. if ((err = cgroup_add_file(cgrp, NULL, &cft_release_agent)) < 0)
  2863. return err;
  2864. }
  2865. for_each_subsys(cgrp->root, ss) {
  2866. if (ss->populate && (err = ss->populate(ss, cgrp)) < 0)
  2867. return err;
  2868. }
  2869. /* This cgroup is ready now */
  2870. for_each_subsys(cgrp->root, ss) {
  2871. struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
  2872. /*
  2873. * Update id->css pointer and make this css visible from
  2874. * CSS ID functions. This pointer will be dereferened
  2875. * from RCU-read-side without locks.
  2876. */
  2877. if (css->id)
  2878. rcu_assign_pointer(css->id->css, css);
  2879. }
  2880. return 0;
  2881. }
  2882. static void init_cgroup_css(struct cgroup_subsys_state *css,
  2883. struct cgroup_subsys *ss,
  2884. struct cgroup *cgrp)
  2885. {
  2886. css->cgroup = cgrp;
  2887. atomic_set(&css->refcnt, 1);
  2888. css->flags = 0;
  2889. css->id = NULL;
  2890. if (cgrp == dummytop)
  2891. set_bit(CSS_ROOT, &css->flags);
  2892. BUG_ON(cgrp->subsys[ss->subsys_id]);
  2893. cgrp->subsys[ss->subsys_id] = css;
  2894. }
  2895. static void cgroup_lock_hierarchy(struct cgroupfs_root *root)
  2896. {
  2897. /* We need to take each hierarchy_mutex in a consistent order */
  2898. int i;
  2899. /*
  2900. * No worry about a race with rebind_subsystems that might mess up the
  2901. * locking order, since both parties are under cgroup_mutex.
  2902. */
  2903. for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
  2904. struct cgroup_subsys *ss = subsys[i];
  2905. if (ss == NULL)
  2906. continue;
  2907. if (ss->root == root)
  2908. mutex_lock(&ss->hierarchy_mutex);
  2909. }
  2910. }
  2911. static void cgroup_unlock_hierarchy(struct cgroupfs_root *root)
  2912. {
  2913. int i;
  2914. for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
  2915. struct cgroup_subsys *ss = subsys[i];
  2916. if (ss == NULL)
  2917. continue;
  2918. if (ss->root == root)
  2919. mutex_unlock(&ss->hierarchy_mutex);
  2920. }
  2921. }
  2922. /*
  2923. * cgroup_create - create a cgroup
  2924. * @parent: cgroup that will be parent of the new cgroup
  2925. * @dentry: dentry of the new cgroup
  2926. * @mode: mode to set on new inode
  2927. *
  2928. * Must be called with the mutex on the parent inode held
  2929. */
  2930. static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
  2931. mode_t mode)
  2932. {
  2933. struct cgroup *cgrp;
  2934. struct cgroupfs_root *root = parent->root;
  2935. int err = 0;
  2936. struct cgroup_subsys *ss;
  2937. struct super_block *sb = root->sb;
  2938. cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
  2939. if (!cgrp)
  2940. return -ENOMEM;
  2941. /* Grab a reference on the superblock so the hierarchy doesn't
  2942. * get deleted on unmount if there are child cgroups. This
  2943. * can be done outside cgroup_mutex, since the sb can't
  2944. * disappear while someone has an open control file on the
  2945. * fs */
  2946. atomic_inc(&sb->s_active);
  2947. mutex_lock(&cgroup_mutex);
  2948. init_cgroup_housekeeping(cgrp);
  2949. cgrp->parent = parent;
  2950. cgrp->root = parent->root;
  2951. cgrp->top_cgroup = parent->top_cgroup;
  2952. if (notify_on_release(parent))
  2953. set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
  2954. for_each_subsys(root, ss) {
  2955. struct cgroup_subsys_state *css = ss->create(ss, cgrp);
  2956. if (IS_ERR(css)) {
  2957. err = PTR_ERR(css);
  2958. goto err_destroy;
  2959. }
  2960. init_cgroup_css(css, ss, cgrp);
  2961. if (ss->use_id) {
  2962. err = alloc_css_id(ss, parent, cgrp);
  2963. if (err)
  2964. goto err_destroy;
  2965. }
  2966. /* At error, ->destroy() callback has to free assigned ID. */
  2967. }
  2968. cgroup_lock_hierarchy(root);
  2969. list_add(&cgrp->sibling, &cgrp->parent->children);
  2970. cgroup_unlock_hierarchy(root);
  2971. root->number_of_cgroups++;
  2972. err = cgroup_create_dir(cgrp, dentry, mode);
  2973. if (err < 0)
  2974. goto err_remove;
  2975. /* The cgroup directory was pre-locked for us */
  2976. BUG_ON(!mutex_is_locked(&cgrp->dentry->d_inode->i_mutex));
  2977. err = cgroup_populate_dir(cgrp);
  2978. /* If err < 0, we have a half-filled directory - oh well ;) */
  2979. mutex_unlock(&cgroup_mutex);
  2980. mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
  2981. return 0;
  2982. err_remove:
  2983. cgroup_lock_hierarchy(root);
  2984. list_del(&cgrp->sibling);
  2985. cgroup_unlock_hierarchy(root);
  2986. root->number_of_cgroups--;
  2987. err_destroy:
  2988. for_each_subsys(root, ss) {
  2989. if (cgrp->subsys[ss->subsys_id])
  2990. ss->destroy(ss, cgrp);
  2991. }
  2992. mutex_unlock(&cgroup_mutex);
  2993. /* Release the reference count that we took on the superblock */
  2994. deactivate_super(sb);
  2995. kfree(cgrp);
  2996. return err;
  2997. }
  2998. static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  2999. {
  3000. struct cgroup *c_parent = dentry->d_parent->d_fsdata;
  3001. /* the vfs holds inode->i_mutex already */
  3002. return cgroup_create(c_parent, dentry, mode | S_IFDIR);
  3003. }
  3004. static int cgroup_has_css_refs(struct cgroup *cgrp)
  3005. {
  3006. /* Check the reference count on each subsystem. Since we
  3007. * already established that there are no tasks in the
  3008. * cgroup, if the css refcount is also 1, then there should
  3009. * be no outstanding references, so the subsystem is safe to
  3010. * destroy. We scan across all subsystems rather than using
  3011. * the per-hierarchy linked list of mounted subsystems since
  3012. * we can be called via check_for_release() with no
  3013. * synchronization other than RCU, and the subsystem linked
  3014. * list isn't RCU-safe */
  3015. int i;
  3016. /*
  3017. * We won't need to lock the subsys array, because the subsystems
  3018. * we're concerned about aren't going anywhere since our cgroup root
  3019. * has a reference on them.
  3020. */
  3021. for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
  3022. struct cgroup_subsys *ss = subsys[i];
  3023. struct cgroup_subsys_state *css;
  3024. /* Skip subsystems not present or not in this hierarchy */
  3025. if (ss == NULL || ss->root != cgrp->root)
  3026. continue;
  3027. css = cgrp->subsys[ss->subsys_id];
  3028. /* When called from check_for_release() it's possible
  3029. * that by this point the cgroup has been removed
  3030. * and the css deleted. But a false-positive doesn't
  3031. * matter, since it can only happen if the cgroup
  3032. * has been deleted and hence no longer needs the
  3033. * release agent to be called anyway. */
  3034. if (css && (atomic_read(&css->refcnt) > 1))
  3035. return 1;
  3036. }
  3037. return 0;
  3038. }
  3039. /*
  3040. * Atomically mark all (or else none) of the cgroup's CSS objects as
  3041. * CSS_REMOVED. Return true on success, or false if the cgroup has
  3042. * busy subsystems. Call with cgroup_mutex held
  3043. */
  3044. static int cgroup_clear_css_refs(struct cgroup *cgrp)
  3045. {
  3046. struct cgroup_subsys *ss;
  3047. unsigned long flags;
  3048. bool failed = false;
  3049. local_irq_save(flags);
  3050. for_each_subsys(cgrp->root, ss) {
  3051. struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
  3052. int refcnt;
  3053. while (1) {
  3054. /* We can only remove a CSS with a refcnt==1 */
  3055. refcnt = atomic_read(&css->refcnt);
  3056. if (refcnt > 1) {
  3057. failed = true;
  3058. goto done;
  3059. }
  3060. BUG_ON(!refcnt);
  3061. /*
  3062. * Drop the refcnt to 0 while we check other
  3063. * subsystems. This will cause any racing
  3064. * css_tryget() to spin until we set the
  3065. * CSS_REMOVED bits or abort
  3066. */
  3067. if (atomic_cmpxchg(&css->refcnt, refcnt, 0) == refcnt)
  3068. break;
  3069. cpu_relax();
  3070. }
  3071. }
  3072. done:
  3073. for_each_subsys(cgrp->root, ss) {
  3074. struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
  3075. if (failed) {
  3076. /*
  3077. * Restore old refcnt if we previously managed
  3078. * to clear it from 1 to 0
  3079. */
  3080. if (!atomic_read(&css->refcnt))
  3081. atomic_set(&css->refcnt, 1);
  3082. } else {
  3083. /* Commit the fact that the CSS is removed */
  3084. set_bit(CSS_REMOVED, &css->flags);
  3085. }
  3086. }
  3087. local_irq_restore(flags);
  3088. return !failed;
  3089. }
  3090. static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
  3091. {
  3092. struct cgroup *cgrp = dentry->d_fsdata;
  3093. struct dentry *d;
  3094. struct cgroup *parent;
  3095. DEFINE_WAIT(wait);
  3096. struct cgroup_event *event, *tmp;
  3097. int ret;
  3098. /* the vfs holds both inode->i_mutex already */
  3099. again:
  3100. mutex_lock(&cgroup_mutex);
  3101. if (atomic_read(&cgrp->count) != 0) {
  3102. mutex_unlock(&cgroup_mutex);
  3103. return -EBUSY;
  3104. }
  3105. if (!list_empty(&cgrp->children)) {
  3106. mutex_unlock(&cgroup_mutex);
  3107. return -EBUSY;
  3108. }
  3109. mutex_unlock(&cgroup_mutex);
  3110. /*
  3111. * In general, subsystem has no css->refcnt after pre_destroy(). But
  3112. * in racy cases, subsystem may have to get css->refcnt after
  3113. * pre_destroy() and it makes rmdir return with -EBUSY. This sometimes
  3114. * make rmdir return -EBUSY too often. To avoid that, we use waitqueue
  3115. * for cgroup's rmdir. CGRP_WAIT_ON_RMDIR is for synchronizing rmdir
  3116. * and subsystem's reference count handling. Please see css_get/put
  3117. * and css_tryget() and cgroup_wakeup_rmdir_waiter() implementation.
  3118. */
  3119. set_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
  3120. /*
  3121. * Call pre_destroy handlers of subsys. Notify subsystems
  3122. * that rmdir() request comes.
  3123. */
  3124. ret = cgroup_call_pre_destroy(cgrp);
  3125. if (ret) {
  3126. clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
  3127. return ret;
  3128. }
  3129. mutex_lock(&cgroup_mutex);
  3130. parent = cgrp->parent;
  3131. if (atomic_read(&cgrp->count) || !list_empty(&cgrp->children)) {
  3132. clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
  3133. mutex_unlock(&cgroup_mutex);
  3134. return -EBUSY;
  3135. }
  3136. prepare_to_wait(&cgroup_rmdir_waitq, &wait, TASK_INTERRUPTIBLE);
  3137. if (!cgroup_clear_css_refs(cgrp)) {
  3138. mutex_unlock(&cgroup_mutex);
  3139. /*
  3140. * Because someone may call cgroup_wakeup_rmdir_waiter() before
  3141. * prepare_to_wait(), we need to check this flag.
  3142. */
  3143. if (test_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags))
  3144. schedule();
  3145. finish_wait(&cgroup_rmdir_waitq, &wait);
  3146. clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
  3147. if (signal_pending(current))
  3148. return -EINTR;
  3149. goto again;
  3150. }
  3151. /* NO css_tryget() can success after here. */
  3152. finish_wait(&cgroup_rmdir_waitq, &wait);
  3153. clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
  3154. spin_lock(&release_list_lock);
  3155. set_bit(CGRP_REMOVED, &cgrp->flags);
  3156. if (!list_empty(&cgrp->release_list))
  3157. list_del(&cgrp->release_list);
  3158. spin_unlock(&release_list_lock);
  3159. cgroup_lock_hierarchy(cgrp->root);
  3160. /* delete this cgroup from parent->children */
  3161. list_del(&cgrp->sibling);
  3162. cgroup_unlock_hierarchy(cgrp->root);
  3163. spin_lock(&cgrp->dentry->d_lock);
  3164. d = dget(cgrp->dentry);
  3165. spin_unlock(&d->d_lock);
  3166. cgroup_d_remove_dir(d);
  3167. dput(d);
  3168. set_bit(CGRP_RELEASABLE, &parent->flags);
  3169. check_for_release(parent);
  3170. /*
  3171. * Unregister events and notify userspace.
  3172. * Notify userspace about cgroup removing only after rmdir of cgroup
  3173. * directory to avoid race between userspace and kernelspace
  3174. */
  3175. spin_lock(&cgrp->event_list_lock);
  3176. list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) {
  3177. list_del(&event->list);
  3178. remove_wait_queue(event->wqh, &event->wait);
  3179. eventfd_signal(event->eventfd, 1);
  3180. schedule_work(&event->remove);
  3181. }
  3182. spin_unlock(&cgrp->event_list_lock);
  3183. mutex_unlock(&cgroup_mutex);
  3184. return 0;
  3185. }
  3186. static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
  3187. {
  3188. struct cgroup_subsys_state *css;
  3189. printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
  3190. /* Create the top cgroup state for this subsystem */
  3191. list_add(&ss->sibling, &rootnode.subsys_list);
  3192. ss->root = &rootnode;
  3193. css = ss->create(ss, dummytop);
  3194. /* We don't handle early failures gracefully */
  3195. BUG_ON(IS_ERR(css));
  3196. init_cgroup_css(css, ss, dummytop);
  3197. /* Update the init_css_set to contain a subsys
  3198. * pointer to this state - since the subsystem is
  3199. * newly registered, all tasks and hence the
  3200. * init_css_set is in the subsystem's top cgroup. */
  3201. init_css_set.subsys[ss->subsys_id] = dummytop->subsys[ss->subsys_id];
  3202. need_forkexit_callback |= ss->fork || ss->exit;
  3203. /* At system boot, before all subsystems have been
  3204. * registered, no tasks have been forked, so we don't
  3205. * need to invoke fork callbacks here. */
  3206. BUG_ON(!list_empty(&init_task.tasks));
  3207. mutex_init(&ss->hierarchy_mutex);
  3208. lockdep_set_class(&ss->hierarchy_mutex, &ss->subsys_key);
  3209. ss->active = 1;
  3210. /* this function shouldn't be used with modular subsystems, since they
  3211. * need to register a subsys_id, among other things */
  3212. BUG_ON(ss->module);
  3213. }
  3214. /**
  3215. * cgroup_load_subsys: load and register a modular subsystem at runtime
  3216. * @ss: the subsystem to load
  3217. *
  3218. * This function should be called in a modular subsystem's initcall. If the
  3219. * subsystem is built as a module, it will be assigned a new subsys_id and set
  3220. * up for use. If the subsystem is built-in anyway, work is delegated to the
  3221. * simpler cgroup_init_subsys.
  3222. */
  3223. int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
  3224. {
  3225. int i;
  3226. struct cgroup_subsys_state *css;
  3227. /* check name and function validity */
  3228. if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
  3229. ss->create == NULL || ss->destroy == NULL)
  3230. return -EINVAL;
  3231. /*
  3232. * we don't support callbacks in modular subsystems. this check is
  3233. * before the ss->module check for consistency; a subsystem that could
  3234. * be a module should still have no callbacks even if the user isn't
  3235. * compiling it as one.
  3236. */
  3237. if (ss->fork || ss->exit)
  3238. return -EINVAL;
  3239. /*
  3240. * an optionally modular subsystem is built-in: we want to do nothing,
  3241. * since cgroup_init_subsys will have already taken care of it.
  3242. */
  3243. if (ss->module == NULL) {
  3244. /* a few sanity checks */
  3245. BUG_ON(ss->subsys_id >= CGROUP_BUILTIN_SUBSYS_COUNT);
  3246. BUG_ON(subsys[ss->subsys_id] != ss);
  3247. return 0;
  3248. }
  3249. /*
  3250. * need to register a subsys id before anything else - for example,
  3251. * init_cgroup_css needs it.
  3252. */
  3253. mutex_lock(&cgroup_mutex);
  3254. /* find the first empty slot in the array */
  3255. for (i = CGROUP_BUILTIN_SUBSYS_COUNT; i < CGROUP_SUBSYS_COUNT; i++) {
  3256. if (subsys[i] == NULL)
  3257. break;
  3258. }
  3259. if (i == CGROUP_SUBSYS_COUNT) {
  3260. /* maximum number of subsystems already registered! */
  3261. mutex_unlock(&cgroup_mutex);
  3262. return -EBUSY;
  3263. }
  3264. /* assign ourselves the subsys_id */
  3265. ss->subsys_id = i;
  3266. subsys[i] = ss;
  3267. /*
  3268. * no ss->create seems to need anything important in the ss struct, so
  3269. * this can happen first (i.e. before the rootnode attachment).
  3270. */
  3271. css = ss->create(ss, dummytop);
  3272. if (IS_ERR(css)) {
  3273. /* failure case - need to deassign the subsys[] slot. */
  3274. subsys[i] = NULL;
  3275. mutex_unlock(&cgroup_mutex);
  3276. return PTR_ERR(css);
  3277. }
  3278. list_add(&ss->sibling, &rootnode.subsys_list);
  3279. ss->root = &rootnode;
  3280. /* our new subsystem will be attached to the dummy hierarchy. */
  3281. init_cgroup_css(css, ss, dummytop);
  3282. /* init_idr must be after init_cgroup_css because it sets css->id. */
  3283. if (ss->use_id) {
  3284. int ret = cgroup_init_idr(ss, css);
  3285. if (ret) {
  3286. dummytop->subsys[ss->subsys_id] = NULL;
  3287. ss->destroy(ss, dummytop);
  3288. subsys[i] = NULL;
  3289. mutex_unlock(&cgroup_mutex);
  3290. return ret;
  3291. }
  3292. }
  3293. /*
  3294. * Now we need to entangle the css into the existing css_sets. unlike
  3295. * in cgroup_init_subsys, there are now multiple css_sets, so each one
  3296. * will need a new pointer to it; done by iterating the css_set_table.
  3297. * furthermore, modifying the existing css_sets will corrupt the hash
  3298. * table state, so each changed css_set will need its hash recomputed.
  3299. * this is all done under the css_set_lock.
  3300. */
  3301. write_lock(&css_set_lock);
  3302. for (i = 0; i < CSS_SET_TABLE_SIZE; i++) {
  3303. struct css_set *cg;
  3304. struct hlist_node *node, *tmp;
  3305. struct hlist_head *bucket = &css_set_table[i], *new_bucket;
  3306. hlist_for_each_entry_safe(cg, node, tmp, bucket, hlist) {
  3307. /* skip entries that we already rehashed */
  3308. if (cg->subsys[ss->subsys_id])
  3309. continue;
  3310. /* remove existing entry */
  3311. hlist_del(&cg->hlist);
  3312. /* set new value */
  3313. cg->subsys[ss->subsys_id] = css;
  3314. /* recompute hash and restore entry */
  3315. new_bucket = css_set_hash(cg->subsys);
  3316. hlist_add_head(&cg->hlist, new_bucket);
  3317. }
  3318. }
  3319. write_unlock(&css_set_lock);
  3320. mutex_init(&ss->hierarchy_mutex);
  3321. lockdep_set_class(&ss->hierarchy_mutex, &ss->subsys_key);
  3322. ss->active = 1;
  3323. /* success! */
  3324. mutex_unlock(&cgroup_mutex);
  3325. return 0;
  3326. }
  3327. EXPORT_SYMBOL_GPL(cgroup_load_subsys);
  3328. /**
  3329. * cgroup_unload_subsys: unload a modular subsystem
  3330. * @ss: the subsystem to unload
  3331. *
  3332. * This function should be called in a modular subsystem's exitcall. When this
  3333. * function is invoked, the refcount on the subsystem's module will be 0, so
  3334. * the subsystem will not be attached to any hierarchy.
  3335. */
  3336. void cgroup_unload_subsys(struct cgroup_subsys *ss)
  3337. {
  3338. struct cg_cgroup_link *link;
  3339. struct hlist_head *hhead;
  3340. BUG_ON(ss->module == NULL);
  3341. /*
  3342. * we shouldn't be called if the subsystem is in use, and the use of
  3343. * try_module_get in parse_cgroupfs_options should ensure that it
  3344. * doesn't start being used while we're killing it off.
  3345. */
  3346. BUG_ON(ss->root != &rootnode);
  3347. mutex_lock(&cgroup_mutex);
  3348. /* deassign the subsys_id */
  3349. BUG_ON(ss->subsys_id < CGROUP_BUILTIN_SUBSYS_COUNT);
  3350. subsys[ss->subsys_id] = NULL;
  3351. /* remove subsystem from rootnode's list of subsystems */
  3352. list_del(&ss->sibling);
  3353. /*
  3354. * disentangle the css from all css_sets attached to the dummytop. as
  3355. * in loading, we need to pay our respects to the hashtable gods.
  3356. */
  3357. write_lock(&css_set_lock);
  3358. list_for_each_entry(link, &dummytop->css_sets, cgrp_link_list) {
  3359. struct css_set *cg = link->cg;
  3360. hlist_del(&cg->hlist);
  3361. BUG_ON(!cg->subsys[ss->subsys_id]);
  3362. cg->subsys[ss->subsys_id] = NULL;
  3363. hhead = css_set_hash(cg->subsys);
  3364. hlist_add_head(&cg->hlist, hhead);
  3365. }
  3366. write_unlock(&css_set_lock);
  3367. /*
  3368. * remove subsystem's css from the dummytop and free it - need to free
  3369. * before marking as null because ss->destroy needs the cgrp->subsys
  3370. * pointer to find their state. note that this also takes care of
  3371. * freeing the css_id.
  3372. */
  3373. ss->destroy(ss, dummytop);
  3374. dummytop->subsys[ss->subsys_id] = NULL;
  3375. mutex_unlock(&cgroup_mutex);
  3376. }
  3377. EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
  3378. /**
  3379. * cgroup_init_early - cgroup initialization at system boot
  3380. *
  3381. * Initialize cgroups at system boot, and initialize any
  3382. * subsystems that request early init.
  3383. */
  3384. int __init cgroup_init_early(void)
  3385. {
  3386. int i;
  3387. atomic_set(&init_css_set.refcount, 1);
  3388. INIT_LIST_HEAD(&init_css_set.cg_links);
  3389. INIT_LIST_HEAD(&init_css_set.tasks);
  3390. INIT_HLIST_NODE(&init_css_set.hlist);
  3391. css_set_count = 1;
  3392. init_cgroup_root(&rootnode);
  3393. root_count = 1;
  3394. init_task.cgroups = &init_css_set;
  3395. init_css_set_link.cg = &init_css_set;
  3396. init_css_set_link.cgrp = dummytop;
  3397. list_add(&init_css_set_link.cgrp_link_list,
  3398. &rootnode.top_cgroup.css_sets);
  3399. list_add(&init_css_set_link.cg_link_list,
  3400. &init_css_set.cg_links);
  3401. for (i = 0; i < CSS_SET_TABLE_SIZE; i++)
  3402. INIT_HLIST_HEAD(&css_set_table[i]);
  3403. /* at bootup time, we don't worry about modular subsystems */
  3404. for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
  3405. struct cgroup_subsys *ss = subsys[i];
  3406. BUG_ON(!ss->name);
  3407. BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
  3408. BUG_ON(!ss->create);
  3409. BUG_ON(!ss->destroy);
  3410. if (ss->subsys_id != i) {
  3411. printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
  3412. ss->name, ss->subsys_id);
  3413. BUG();
  3414. }
  3415. if (ss->early_init)
  3416. cgroup_init_subsys(ss);
  3417. }
  3418. return 0;
  3419. }
  3420. /**
  3421. * cgroup_init - cgroup initialization
  3422. *
  3423. * Register cgroup filesystem and /proc file, and initialize
  3424. * any subsystems that didn't request early init.
  3425. */
  3426. int __init cgroup_init(void)
  3427. {
  3428. int err;
  3429. int i;
  3430. struct hlist_head *hhead;
  3431. err = bdi_init(&cgroup_backing_dev_info);
  3432. if (err)
  3433. return err;
  3434. /* at bootup time, we don't worry about modular subsystems */
  3435. for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
  3436. struct cgroup_subsys *ss = subsys[i];
  3437. if (!ss->early_init)
  3438. cgroup_init_subsys(ss);
  3439. if (ss->use_id)
  3440. cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]);
  3441. }
  3442. /* Add init_css_set to the hash table */
  3443. hhead = css_set_hash(init_css_set.subsys);
  3444. hlist_add_head(&init_css_set.hlist, hhead);
  3445. BUG_ON(!init_root_id(&rootnode));
  3446. cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
  3447. if (!cgroup_kobj) {
  3448. err = -ENOMEM;
  3449. goto out;
  3450. }
  3451. err = register_filesystem(&cgroup_fs_type);
  3452. if (err < 0) {
  3453. kobject_put(cgroup_kobj);
  3454. goto out;
  3455. }
  3456. proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
  3457. out:
  3458. if (err)
  3459. bdi_destroy(&cgroup_backing_dev_info);
  3460. return err;
  3461. }
  3462. /*
  3463. * proc_cgroup_show()
  3464. * - Print task's cgroup paths into seq_file, one line for each hierarchy
  3465. * - Used for /proc/<pid>/cgroup.
  3466. * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
  3467. * doesn't really matter if tsk->cgroup changes after we read it,
  3468. * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
  3469. * anyway. No need to check that tsk->cgroup != NULL, thanks to
  3470. * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
  3471. * cgroup to top_cgroup.
  3472. */
  3473. /* TODO: Use a proper seq_file iterator */
  3474. static int proc_cgroup_show(struct seq_file *m, void *v)
  3475. {
  3476. struct pid *pid;
  3477. struct task_struct *tsk;
  3478. char *buf;
  3479. int retval;
  3480. struct cgroupfs_root *root;
  3481. retval = -ENOMEM;
  3482. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  3483. if (!buf)
  3484. goto out;
  3485. retval = -ESRCH;
  3486. pid = m->private;
  3487. tsk = get_pid_task(pid, PIDTYPE_PID);
  3488. if (!tsk)
  3489. goto out_free;
  3490. retval = 0;
  3491. mutex_lock(&cgroup_mutex);
  3492. for_each_active_root(root) {
  3493. struct cgroup_subsys *ss;
  3494. struct cgroup *cgrp;
  3495. int count = 0;
  3496. seq_printf(m, "%d:", root->hierarchy_id);
  3497. for_each_subsys(root, ss)
  3498. seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
  3499. if (strlen(root->name))
  3500. seq_printf(m, "%sname=%s", count ? "," : "",
  3501. root->name);
  3502. seq_putc(m, ':');
  3503. cgrp = task_cgroup_from_root(tsk, root);
  3504. retval = cgroup_path(cgrp, buf, PAGE_SIZE);
  3505. if (retval < 0)
  3506. goto out_unlock;
  3507. seq_puts(m, buf);
  3508. seq_putc(m, '\n');
  3509. }
  3510. out_unlock:
  3511. mutex_unlock(&cgroup_mutex);
  3512. put_task_struct(tsk);
  3513. out_free:
  3514. kfree(buf);
  3515. out:
  3516. return retval;
  3517. }
  3518. static int cgroup_open(struct inode *inode, struct file *file)
  3519. {
  3520. struct pid *pid = PROC_I(inode)->pid;
  3521. return single_open(file, proc_cgroup_show, pid);
  3522. }
  3523. const struct file_operations proc_cgroup_operations = {
  3524. .open = cgroup_open,
  3525. .read = seq_read,
  3526. .llseek = seq_lseek,
  3527. .release = single_release,
  3528. };
  3529. /* Display information about each subsystem and each hierarchy */
  3530. static int proc_cgroupstats_show(struct seq_file *m, void *v)
  3531. {
  3532. int i;
  3533. seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
  3534. /*
  3535. * ideally we don't want subsystems moving around while we do this.
  3536. * cgroup_mutex is also necessary to guarantee an atomic snapshot of
  3537. * subsys/hierarchy state.
  3538. */
  3539. mutex_lock(&cgroup_mutex);
  3540. for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
  3541. struct cgroup_subsys *ss = subsys[i];
  3542. if (ss == NULL)
  3543. continue;
  3544. seq_printf(m, "%s\t%d\t%d\t%d\n",
  3545. ss->name, ss->root->hierarchy_id,
  3546. ss->root->number_of_cgroups, !ss->disabled);
  3547. }
  3548. mutex_unlock(&cgroup_mutex);
  3549. return 0;
  3550. }
  3551. static int cgroupstats_open(struct inode *inode, struct file *file)
  3552. {
  3553. return single_open(file, proc_cgroupstats_show, NULL);
  3554. }
  3555. static const struct file_operations proc_cgroupstats_operations = {
  3556. .open = cgroupstats_open,
  3557. .read = seq_read,
  3558. .llseek = seq_lseek,
  3559. .release = single_release,
  3560. };
  3561. /**
  3562. * cgroup_fork - attach newly forked task to its parents cgroup.
  3563. * @child: pointer to task_struct of forking parent process.
  3564. *
  3565. * Description: A task inherits its parent's cgroup at fork().
  3566. *
  3567. * A pointer to the shared css_set was automatically copied in
  3568. * fork.c by dup_task_struct(). However, we ignore that copy, since
  3569. * it was not made under the protection of RCU or cgroup_mutex, so
  3570. * might no longer be a valid cgroup pointer. cgroup_attach_task() might
  3571. * have already changed current->cgroups, allowing the previously
  3572. * referenced cgroup group to be removed and freed.
  3573. *
  3574. * At the point that cgroup_fork() is called, 'current' is the parent
  3575. * task, and the passed argument 'child' points to the child task.
  3576. */
  3577. void cgroup_fork(struct task_struct *child)
  3578. {
  3579. task_lock(current);
  3580. child->cgroups = current->cgroups;
  3581. get_css_set(child->cgroups);
  3582. task_unlock(current);
  3583. INIT_LIST_HEAD(&child->cg_list);
  3584. }
  3585. /**
  3586. * cgroup_fork_callbacks - run fork callbacks
  3587. * @child: the new task
  3588. *
  3589. * Called on a new task very soon before adding it to the
  3590. * tasklist. No need to take any locks since no-one can
  3591. * be operating on this task.
  3592. */
  3593. void cgroup_fork_callbacks(struct task_struct *child)
  3594. {
  3595. if (need_forkexit_callback) {
  3596. int i;
  3597. /*
  3598. * forkexit callbacks are only supported for builtin
  3599. * subsystems, and the builtin section of the subsys array is
  3600. * immutable, so we don't need to lock the subsys array here.
  3601. */
  3602. for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
  3603. struct cgroup_subsys *ss = subsys[i];
  3604. if (ss->fork)
  3605. ss->fork(ss, child);
  3606. }
  3607. }
  3608. }
  3609. /**
  3610. * cgroup_post_fork - called on a new task after adding it to the task list
  3611. * @child: the task in question
  3612. *
  3613. * Adds the task to the list running through its css_set if necessary.
  3614. * Has to be after the task is visible on the task list in case we race
  3615. * with the first call to cgroup_iter_start() - to guarantee that the
  3616. * new task ends up on its list.
  3617. */
  3618. void cgroup_post_fork(struct task_struct *child)
  3619. {
  3620. if (use_task_css_set_links) {
  3621. write_lock(&css_set_lock);
  3622. task_lock(child);
  3623. if (list_empty(&child->cg_list))
  3624. list_add(&child->cg_list, &child->cgroups->tasks);
  3625. task_unlock(child);
  3626. write_unlock(&css_set_lock);
  3627. }
  3628. }
  3629. /**
  3630. * cgroup_exit - detach cgroup from exiting task
  3631. * @tsk: pointer to task_struct of exiting process
  3632. * @run_callback: run exit callbacks?
  3633. *
  3634. * Description: Detach cgroup from @tsk and release it.
  3635. *
  3636. * Note that cgroups marked notify_on_release force every task in
  3637. * them to take the global cgroup_mutex mutex when exiting.
  3638. * This could impact scaling on very large systems. Be reluctant to
  3639. * use notify_on_release cgroups where very high task exit scaling
  3640. * is required on large systems.
  3641. *
  3642. * the_top_cgroup_hack:
  3643. *
  3644. * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
  3645. *
  3646. * We call cgroup_exit() while the task is still competent to
  3647. * handle notify_on_release(), then leave the task attached to the
  3648. * root cgroup in each hierarchy for the remainder of its exit.
  3649. *
  3650. * To do this properly, we would increment the reference count on
  3651. * top_cgroup, and near the very end of the kernel/exit.c do_exit()
  3652. * code we would add a second cgroup function call, to drop that
  3653. * reference. This would just create an unnecessary hot spot on
  3654. * the top_cgroup reference count, to no avail.
  3655. *
  3656. * Normally, holding a reference to a cgroup without bumping its
  3657. * count is unsafe. The cgroup could go away, or someone could
  3658. * attach us to a different cgroup, decrementing the count on
  3659. * the first cgroup that we never incremented. But in this case,
  3660. * top_cgroup isn't going away, and either task has PF_EXITING set,
  3661. * which wards off any cgroup_attach_task() attempts, or task is a failed
  3662. * fork, never visible to cgroup_attach_task.
  3663. */
  3664. void cgroup_exit(struct task_struct *tsk, int run_callbacks)
  3665. {
  3666. int i;
  3667. struct css_set *cg;
  3668. if (run_callbacks && need_forkexit_callback) {
  3669. /*
  3670. * modular subsystems can't use callbacks, so no need to lock
  3671. * the subsys array
  3672. */
  3673. for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
  3674. struct cgroup_subsys *ss = subsys[i];
  3675. if (ss->exit)
  3676. ss->exit(ss, tsk);
  3677. }
  3678. }
  3679. /*
  3680. * Unlink from the css_set task list if necessary.
  3681. * Optimistically check cg_list before taking
  3682. * css_set_lock
  3683. */
  3684. if (!list_empty(&tsk->cg_list)) {
  3685. write_lock(&css_set_lock);
  3686. if (!list_empty(&tsk->cg_list))
  3687. list_del(&tsk->cg_list);
  3688. write_unlock(&css_set_lock);
  3689. }
  3690. /* Reassign the task to the init_css_set. */
  3691. task_lock(tsk);
  3692. cg = tsk->cgroups;
  3693. tsk->cgroups = &init_css_set;
  3694. task_unlock(tsk);
  3695. if (cg)
  3696. put_css_set_taskexit(cg);
  3697. }
  3698. /**
  3699. * cgroup_clone - clone the cgroup the given subsystem is attached to
  3700. * @tsk: the task to be moved
  3701. * @subsys: the given subsystem
  3702. * @nodename: the name for the new cgroup
  3703. *
  3704. * Duplicate the current cgroup in the hierarchy that the given
  3705. * subsystem is attached to, and move this task into the new
  3706. * child.
  3707. */
  3708. int cgroup_clone(struct task_struct *tsk, struct cgroup_subsys *subsys,
  3709. char *nodename)
  3710. {
  3711. struct dentry *dentry;
  3712. int ret = 0;
  3713. struct cgroup *parent, *child;
  3714. struct inode *inode;
  3715. struct css_set *cg;
  3716. struct cgroupfs_root *root;
  3717. struct cgroup_subsys *ss;
  3718. /* We shouldn't be called by an unregistered subsystem */
  3719. BUG_ON(!subsys->active);
  3720. /* First figure out what hierarchy and cgroup we're dealing
  3721. * with, and pin them so we can drop cgroup_mutex */
  3722. mutex_lock(&cgroup_mutex);
  3723. again:
  3724. root = subsys->root;
  3725. if (root == &rootnode) {
  3726. mutex_unlock(&cgroup_mutex);
  3727. return 0;
  3728. }
  3729. /* Pin the hierarchy */
  3730. if (!atomic_inc_not_zero(&root->sb->s_active)) {
  3731. /* We race with the final deactivate_super() */
  3732. mutex_unlock(&cgroup_mutex);
  3733. return 0;
  3734. }
  3735. /* Keep the cgroup alive */
  3736. task_lock(tsk);
  3737. parent = task_cgroup(tsk, subsys->subsys_id);
  3738. cg = tsk->cgroups;
  3739. get_css_set(cg);
  3740. task_unlock(tsk);
  3741. mutex_unlock(&cgroup_mutex);
  3742. /* Now do the VFS work to create a cgroup */
  3743. inode = parent->dentry->d_inode;
  3744. /* Hold the parent directory mutex across this operation to
  3745. * stop anyone else deleting the new cgroup */
  3746. mutex_lock(&inode->i_mutex);
  3747. dentry = lookup_one_len(nodename, parent->dentry, strlen(nodename));
  3748. if (IS_ERR(dentry)) {
  3749. printk(KERN_INFO
  3750. "cgroup: Couldn't allocate dentry for %s: %ld\n", nodename,
  3751. PTR_ERR(dentry));
  3752. ret = PTR_ERR(dentry);
  3753. goto out_release;
  3754. }
  3755. /* Create the cgroup directory, which also creates the cgroup */
  3756. ret = vfs_mkdir(inode, dentry, 0755);
  3757. child = __d_cgrp(dentry);
  3758. dput(dentry);
  3759. if (ret) {
  3760. printk(KERN_INFO
  3761. "Failed to create cgroup %s: %d\n", nodename,
  3762. ret);
  3763. goto out_release;
  3764. }
  3765. /* The cgroup now exists. Retake cgroup_mutex and check
  3766. * that we're still in the same state that we thought we
  3767. * were. */
  3768. mutex_lock(&cgroup_mutex);
  3769. if ((root != subsys->root) ||
  3770. (parent != task_cgroup(tsk, subsys->subsys_id))) {
  3771. /* Aargh, we raced ... */
  3772. mutex_unlock(&inode->i_mutex);
  3773. put_css_set(cg);
  3774. deactivate_super(root->sb);
  3775. /* The cgroup is still accessible in the VFS, but
  3776. * we're not going to try to rmdir() it at this
  3777. * point. */
  3778. printk(KERN_INFO
  3779. "Race in cgroup_clone() - leaking cgroup %s\n",
  3780. nodename);
  3781. goto again;
  3782. }
  3783. /* do any required auto-setup */
  3784. for_each_subsys(root, ss) {
  3785. if (ss->post_clone)
  3786. ss->post_clone(ss, child);
  3787. }
  3788. /* All seems fine. Finish by moving the task into the new cgroup */
  3789. ret = cgroup_attach_task(child, tsk);
  3790. mutex_unlock(&cgroup_mutex);
  3791. out_release:
  3792. mutex_unlock(&inode->i_mutex);
  3793. mutex_lock(&cgroup_mutex);
  3794. put_css_set(cg);
  3795. mutex_unlock(&cgroup_mutex);
  3796. deactivate_super(root->sb);
  3797. return ret;
  3798. }
  3799. /**
  3800. * cgroup_is_descendant - see if @cgrp is a descendant of @task's cgrp
  3801. * @cgrp: the cgroup in question
  3802. * @task: the task in question
  3803. *
  3804. * See if @cgrp is a descendant of @task's cgroup in the appropriate
  3805. * hierarchy.
  3806. *
  3807. * If we are sending in dummytop, then presumably we are creating
  3808. * the top cgroup in the subsystem.
  3809. *
  3810. * Called only by the ns (nsproxy) cgroup.
  3811. */
  3812. int cgroup_is_descendant(const struct cgroup *cgrp, struct task_struct *task)
  3813. {
  3814. int ret;
  3815. struct cgroup *target;
  3816. if (cgrp == dummytop)
  3817. return 1;
  3818. target = task_cgroup_from_root(task, cgrp->root);
  3819. while (cgrp != target && cgrp!= cgrp->top_cgroup)
  3820. cgrp = cgrp->parent;
  3821. ret = (cgrp == target);
  3822. return ret;
  3823. }
  3824. static void check_for_release(struct cgroup *cgrp)
  3825. {
  3826. /* All of these checks rely on RCU to keep the cgroup
  3827. * structure alive */
  3828. if (cgroup_is_releasable(cgrp) && !atomic_read(&cgrp->count)
  3829. && list_empty(&cgrp->children) && !cgroup_has_css_refs(cgrp)) {
  3830. /* Control Group is currently removeable. If it's not
  3831. * already queued for a userspace notification, queue
  3832. * it now */
  3833. int need_schedule_work = 0;
  3834. spin_lock(&release_list_lock);
  3835. if (!cgroup_is_removed(cgrp) &&
  3836. list_empty(&cgrp->release_list)) {
  3837. list_add(&cgrp->release_list, &release_list);
  3838. need_schedule_work = 1;
  3839. }
  3840. spin_unlock(&release_list_lock);
  3841. if (need_schedule_work)
  3842. schedule_work(&release_agent_work);
  3843. }
  3844. }
  3845. /* Caller must verify that the css is not for root cgroup */
  3846. void __css_put(struct cgroup_subsys_state *css, int count)
  3847. {
  3848. struct cgroup *cgrp = css->cgroup;
  3849. int val;
  3850. rcu_read_lock();
  3851. val = atomic_sub_return(count, &css->refcnt);
  3852. if (val == 1) {
  3853. if (notify_on_release(cgrp)) {
  3854. set_bit(CGRP_RELEASABLE, &cgrp->flags);
  3855. check_for_release(cgrp);
  3856. }
  3857. cgroup_wakeup_rmdir_waiter(cgrp);
  3858. }
  3859. rcu_read_unlock();
  3860. WARN_ON_ONCE(val < 1);
  3861. }
  3862. EXPORT_SYMBOL_GPL(__css_put);
  3863. /*
  3864. * Notify userspace when a cgroup is released, by running the
  3865. * configured release agent with the name of the cgroup (path
  3866. * relative to the root of cgroup file system) as the argument.
  3867. *
  3868. * Most likely, this user command will try to rmdir this cgroup.
  3869. *
  3870. * This races with the possibility that some other task will be
  3871. * attached to this cgroup before it is removed, or that some other
  3872. * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
  3873. * The presumed 'rmdir' will fail quietly if this cgroup is no longer
  3874. * unused, and this cgroup will be reprieved from its death sentence,
  3875. * to continue to serve a useful existence. Next time it's released,
  3876. * we will get notified again, if it still has 'notify_on_release' set.
  3877. *
  3878. * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
  3879. * means only wait until the task is successfully execve()'d. The
  3880. * separate release agent task is forked by call_usermodehelper(),
  3881. * then control in this thread returns here, without waiting for the
  3882. * release agent task. We don't bother to wait because the caller of
  3883. * this routine has no use for the exit status of the release agent
  3884. * task, so no sense holding our caller up for that.
  3885. */
  3886. static void cgroup_release_agent(struct work_struct *work)
  3887. {
  3888. BUG_ON(work != &release_agent_work);
  3889. mutex_lock(&cgroup_mutex);
  3890. spin_lock(&release_list_lock);
  3891. while (!list_empty(&release_list)) {
  3892. char *argv[3], *envp[3];
  3893. int i;
  3894. char *pathbuf = NULL, *agentbuf = NULL;
  3895. struct cgroup *cgrp = list_entry(release_list.next,
  3896. struct cgroup,
  3897. release_list);
  3898. list_del_init(&cgrp->release_list);
  3899. spin_unlock(&release_list_lock);
  3900. pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  3901. if (!pathbuf)
  3902. goto continue_free;
  3903. if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
  3904. goto continue_free;
  3905. agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
  3906. if (!agentbuf)
  3907. goto continue_free;
  3908. i = 0;
  3909. argv[i++] = agentbuf;
  3910. argv[i++] = pathbuf;
  3911. argv[i] = NULL;
  3912. i = 0;
  3913. /* minimal command environment */
  3914. envp[i++] = "HOME=/";
  3915. envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
  3916. envp[i] = NULL;
  3917. /* Drop the lock while we invoke the usermode helper,
  3918. * since the exec could involve hitting disk and hence
  3919. * be a slow process */
  3920. mutex_unlock(&cgroup_mutex);
  3921. call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
  3922. mutex_lock(&cgroup_mutex);
  3923. continue_free:
  3924. kfree(pathbuf);
  3925. kfree(agentbuf);
  3926. spin_lock(&release_list_lock);
  3927. }
  3928. spin_unlock(&release_list_lock);
  3929. mutex_unlock(&cgroup_mutex);
  3930. }
  3931. static int __init cgroup_disable(char *str)
  3932. {
  3933. int i;
  3934. char *token;
  3935. while ((token = strsep(&str, ",")) != NULL) {
  3936. if (!*token)
  3937. continue;
  3938. /*
  3939. * cgroup_disable, being at boot time, can't know about module
  3940. * subsystems, so we don't worry about them.
  3941. */
  3942. for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
  3943. struct cgroup_subsys *ss = subsys[i];
  3944. if (!strcmp(token, ss->name)) {
  3945. ss->disabled = 1;
  3946. printk(KERN_INFO "Disabling %s control group"
  3947. " subsystem\n", ss->name);
  3948. break;
  3949. }
  3950. }
  3951. }
  3952. return 1;
  3953. }
  3954. __setup("cgroup_disable=", cgroup_disable);
  3955. /*
  3956. * Functons for CSS ID.
  3957. */
  3958. /*
  3959. *To get ID other than 0, this should be called when !cgroup_is_removed().
  3960. */
  3961. unsigned short css_id(struct cgroup_subsys_state *css)
  3962. {
  3963. struct css_id *cssid;
  3964. /*
  3965. * This css_id() can return correct value when somone has refcnt
  3966. * on this or this is under rcu_read_lock(). Once css->id is allocated,
  3967. * it's unchanged until freed.
  3968. */
  3969. cssid = rcu_dereference_check(css->id,
  3970. rcu_read_lock_held() || atomic_read(&css->refcnt));
  3971. if (cssid)
  3972. return cssid->id;
  3973. return 0;
  3974. }
  3975. EXPORT_SYMBOL_GPL(css_id);
  3976. unsigned short css_depth(struct cgroup_subsys_state *css)
  3977. {
  3978. struct css_id *cssid;
  3979. cssid = rcu_dereference_check(css->id,
  3980. rcu_read_lock_held() || atomic_read(&css->refcnt));
  3981. if (cssid)
  3982. return cssid->depth;
  3983. return 0;
  3984. }
  3985. EXPORT_SYMBOL_GPL(css_depth);
  3986. /**
  3987. * css_is_ancestor - test "root" css is an ancestor of "child"
  3988. * @child: the css to be tested.
  3989. * @root: the css supporsed to be an ancestor of the child.
  3990. *
  3991. * Returns true if "root" is an ancestor of "child" in its hierarchy. Because
  3992. * this function reads css->id, this use rcu_dereference() and rcu_read_lock().
  3993. * But, considering usual usage, the csses should be valid objects after test.
  3994. * Assuming that the caller will do some action to the child if this returns
  3995. * returns true, the caller must take "child";s reference count.
  3996. * If "child" is valid object and this returns true, "root" is valid, too.
  3997. */
  3998. bool css_is_ancestor(struct cgroup_subsys_state *child,
  3999. const struct cgroup_subsys_state *root)
  4000. {
  4001. struct css_id *child_id;
  4002. struct css_id *root_id;
  4003. bool ret = true;
  4004. rcu_read_lock();
  4005. child_id = rcu_dereference(child->id);
  4006. root_id = rcu_dereference(root->id);
  4007. if (!child_id
  4008. || !root_id
  4009. || (child_id->depth < root_id->depth)
  4010. || (child_id->stack[root_id->depth] != root_id->id))
  4011. ret = false;
  4012. rcu_read_unlock();
  4013. return ret;
  4014. }
  4015. static void __free_css_id_cb(struct rcu_head *head)
  4016. {
  4017. struct css_id *id;
  4018. id = container_of(head, struct css_id, rcu_head);
  4019. kfree(id);
  4020. }
  4021. void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
  4022. {
  4023. struct css_id *id = css->id;
  4024. /* When this is called before css_id initialization, id can be NULL */
  4025. if (!id)
  4026. return;
  4027. BUG_ON(!ss->use_id);
  4028. rcu_assign_pointer(id->css, NULL);
  4029. rcu_assign_pointer(css->id, NULL);
  4030. spin_lock(&ss->id_lock);
  4031. idr_remove(&ss->idr, id->id);
  4032. spin_unlock(&ss->id_lock);
  4033. call_rcu(&id->rcu_head, __free_css_id_cb);
  4034. }
  4035. EXPORT_SYMBOL_GPL(free_css_id);
  4036. /*
  4037. * This is called by init or create(). Then, calls to this function are
  4038. * always serialized (By cgroup_mutex() at create()).
  4039. */
  4040. static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
  4041. {
  4042. struct css_id *newid;
  4043. int myid, error, size;
  4044. BUG_ON(!ss->use_id);
  4045. size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
  4046. newid = kzalloc(size, GFP_KERNEL);
  4047. if (!newid)
  4048. return ERR_PTR(-ENOMEM);
  4049. /* get id */
  4050. if (unlikely(!idr_pre_get(&ss->idr, GFP_KERNEL))) {
  4051. error = -ENOMEM;
  4052. goto err_out;
  4053. }
  4054. spin_lock(&ss->id_lock);
  4055. /* Don't use 0. allocates an ID of 1-65535 */
  4056. error = idr_get_new_above(&ss->idr, newid, 1, &myid);
  4057. spin_unlock(&ss->id_lock);
  4058. /* Returns error when there are no free spaces for new ID.*/
  4059. if (error) {
  4060. error = -ENOSPC;
  4061. goto err_out;
  4062. }
  4063. if (myid > CSS_ID_MAX)
  4064. goto remove_idr;
  4065. newid->id = myid;
  4066. newid->depth = depth;
  4067. return newid;
  4068. remove_idr:
  4069. error = -ENOSPC;
  4070. spin_lock(&ss->id_lock);
  4071. idr_remove(&ss->idr, myid);
  4072. spin_unlock(&ss->id_lock);
  4073. err_out:
  4074. kfree(newid);
  4075. return ERR_PTR(error);
  4076. }
  4077. static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss,
  4078. struct cgroup_subsys_state *rootcss)
  4079. {
  4080. struct css_id *newid;
  4081. spin_lock_init(&ss->id_lock);
  4082. idr_init(&ss->idr);
  4083. newid = get_new_cssid(ss, 0);
  4084. if (IS_ERR(newid))
  4085. return PTR_ERR(newid);
  4086. newid->stack[0] = newid->id;
  4087. newid->css = rootcss;
  4088. rootcss->id = newid;
  4089. return 0;
  4090. }
  4091. static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent,
  4092. struct cgroup *child)
  4093. {
  4094. int subsys_id, i, depth = 0;
  4095. struct cgroup_subsys_state *parent_css, *child_css;
  4096. struct css_id *child_id, *parent_id;
  4097. subsys_id = ss->subsys_id;
  4098. parent_css = parent->subsys[subsys_id];
  4099. child_css = child->subsys[subsys_id];
  4100. parent_id = parent_css->id;
  4101. depth = parent_id->depth + 1;
  4102. child_id = get_new_cssid(ss, depth);
  4103. if (IS_ERR(child_id))
  4104. return PTR_ERR(child_id);
  4105. for (i = 0; i < depth; i++)
  4106. child_id->stack[i] = parent_id->stack[i];
  4107. child_id->stack[depth] = child_id->id;
  4108. /*
  4109. * child_id->css pointer will be set after this cgroup is available
  4110. * see cgroup_populate_dir()
  4111. */
  4112. rcu_assign_pointer(child_css->id, child_id);
  4113. return 0;
  4114. }
  4115. /**
  4116. * css_lookup - lookup css by id
  4117. * @ss: cgroup subsys to be looked into.
  4118. * @id: the id
  4119. *
  4120. * Returns pointer to cgroup_subsys_state if there is valid one with id.
  4121. * NULL if not. Should be called under rcu_read_lock()
  4122. */
  4123. struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
  4124. {
  4125. struct css_id *cssid = NULL;
  4126. BUG_ON(!ss->use_id);
  4127. cssid = idr_find(&ss->idr, id);
  4128. if (unlikely(!cssid))
  4129. return NULL;
  4130. return rcu_dereference(cssid->css);
  4131. }
  4132. EXPORT_SYMBOL_GPL(css_lookup);
  4133. /**
  4134. * css_get_next - lookup next cgroup under specified hierarchy.
  4135. * @ss: pointer to subsystem
  4136. * @id: current position of iteration.
  4137. * @root: pointer to css. search tree under this.
  4138. * @foundid: position of found object.
  4139. *
  4140. * Search next css under the specified hierarchy of rootid. Calling under
  4141. * rcu_read_lock() is necessary. Returns NULL if it reaches the end.
  4142. */
  4143. struct cgroup_subsys_state *
  4144. css_get_next(struct cgroup_subsys *ss, int id,
  4145. struct cgroup_subsys_state *root, int *foundid)
  4146. {
  4147. struct cgroup_subsys_state *ret = NULL;
  4148. struct css_id *tmp;
  4149. int tmpid;
  4150. int rootid = css_id(root);
  4151. int depth = css_depth(root);
  4152. if (!rootid)
  4153. return NULL;
  4154. BUG_ON(!ss->use_id);
  4155. /* fill start point for scan */
  4156. tmpid = id;
  4157. while (1) {
  4158. /*
  4159. * scan next entry from bitmap(tree), tmpid is updated after
  4160. * idr_get_next().
  4161. */
  4162. spin_lock(&ss->id_lock);
  4163. tmp = idr_get_next(&ss->idr, &tmpid);
  4164. spin_unlock(&ss->id_lock);
  4165. if (!tmp)
  4166. break;
  4167. if (tmp->depth >= depth && tmp->stack[depth] == rootid) {
  4168. ret = rcu_dereference(tmp->css);
  4169. if (ret) {
  4170. *foundid = tmpid;
  4171. break;
  4172. }
  4173. }
  4174. /* continue to scan from next id */
  4175. tmpid = tmpid + 1;
  4176. }
  4177. return ret;
  4178. }
  4179. #ifdef CONFIG_CGROUP_DEBUG
  4180. static struct cgroup_subsys_state *debug_create(struct cgroup_subsys *ss,
  4181. struct cgroup *cont)
  4182. {
  4183. struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
  4184. if (!css)
  4185. return ERR_PTR(-ENOMEM);
  4186. return css;
  4187. }
  4188. static void debug_destroy(struct cgroup_subsys *ss, struct cgroup *cont)
  4189. {
  4190. kfree(cont->subsys[debug_subsys_id]);
  4191. }
  4192. static u64 cgroup_refcount_read(struct cgroup *cont, struct cftype *cft)
  4193. {
  4194. return atomic_read(&cont->count);
  4195. }
  4196. static u64 debug_taskcount_read(struct cgroup *cont, struct cftype *cft)
  4197. {
  4198. return cgroup_task_count(cont);
  4199. }
  4200. static u64 current_css_set_read(struct cgroup *cont, struct cftype *cft)
  4201. {
  4202. return (u64)(unsigned long)current->cgroups;
  4203. }
  4204. static u64 current_css_set_refcount_read(struct cgroup *cont,
  4205. struct cftype *cft)
  4206. {
  4207. u64 count;
  4208. rcu_read_lock();
  4209. count = atomic_read(&current->cgroups->refcount);
  4210. rcu_read_unlock();
  4211. return count;
  4212. }
  4213. static int current_css_set_cg_links_read(struct cgroup *cont,
  4214. struct cftype *cft,
  4215. struct seq_file *seq)
  4216. {
  4217. struct cg_cgroup_link *link;
  4218. struct css_set *cg;
  4219. read_lock(&css_set_lock);
  4220. rcu_read_lock();
  4221. cg = rcu_dereference(current->cgroups);
  4222. list_for_each_entry(link, &cg->cg_links, cg_link_list) {
  4223. struct cgroup *c = link->cgrp;
  4224. const char *name;
  4225. if (c->dentry)
  4226. name = c->dentry->d_name.name;
  4227. else
  4228. name = "?";
  4229. seq_printf(seq, "Root %d group %s\n",
  4230. c->root->hierarchy_id, name);
  4231. }
  4232. rcu_read_unlock();
  4233. read_unlock(&css_set_lock);
  4234. return 0;
  4235. }
  4236. #define MAX_TASKS_SHOWN_PER_CSS 25
  4237. static int cgroup_css_links_read(struct cgroup *cont,
  4238. struct cftype *cft,
  4239. struct seq_file *seq)
  4240. {
  4241. struct cg_cgroup_link *link;
  4242. read_lock(&css_set_lock);
  4243. list_for_each_entry(link, &cont->css_sets, cgrp_link_list) {
  4244. struct css_set *cg = link->cg;
  4245. struct task_struct *task;
  4246. int count = 0;
  4247. seq_printf(seq, "css_set %p\n", cg);
  4248. list_for_each_entry(task, &cg->tasks, cg_list) {
  4249. if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
  4250. seq_puts(seq, " ...\n");
  4251. break;
  4252. } else {
  4253. seq_printf(seq, " task %d\n",
  4254. task_pid_vnr(task));
  4255. }
  4256. }
  4257. }
  4258. read_unlock(&css_set_lock);
  4259. return 0;
  4260. }
  4261. static u64 releasable_read(struct cgroup *cgrp, struct cftype *cft)
  4262. {
  4263. return test_bit(CGRP_RELEASABLE, &cgrp->flags);
  4264. }
  4265. static struct cftype debug_files[] = {
  4266. {
  4267. .name = "cgroup_refcount",
  4268. .read_u64 = cgroup_refcount_read,
  4269. },
  4270. {
  4271. .name = "taskcount",
  4272. .read_u64 = debug_taskcount_read,
  4273. },
  4274. {
  4275. .name = "current_css_set",
  4276. .read_u64 = current_css_set_read,
  4277. },
  4278. {
  4279. .name = "current_css_set_refcount",
  4280. .read_u64 = current_css_set_refcount_read,
  4281. },
  4282. {
  4283. .name = "current_css_set_cg_links",
  4284. .read_seq_string = current_css_set_cg_links_read,
  4285. },
  4286. {
  4287. .name = "cgroup_css_links",
  4288. .read_seq_string = cgroup_css_links_read,
  4289. },
  4290. {
  4291. .name = "releasable",
  4292. .read_u64 = releasable_read,
  4293. },
  4294. };
  4295. static int debug_populate(struct cgroup_subsys *ss, struct cgroup *cont)
  4296. {
  4297. return cgroup_add_files(cont, ss, debug_files,
  4298. ARRAY_SIZE(debug_files));
  4299. }
  4300. struct cgroup_subsys debug_subsys = {
  4301. .name = "debug",
  4302. .create = debug_create,
  4303. .destroy = debug_destroy,
  4304. .populate = debug_populate,
  4305. .subsys_id = debug_subsys_id,
  4306. };
  4307. #endif /* CONFIG_CGROUP_DEBUG */