cgroup.c 138 KB

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