cgroup.c 137 KB

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