cgroup.c 145 KB

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