cgroup.c 144 KB

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