cgroup.c 140 KB

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