cgroup.c 162 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  29. #include <linux/cgroup.h>
  30. #include <linux/cred.h>
  31. #include <linux/ctype.h>
  32. #include <linux/errno.h>
  33. #include <linux/init_task.h>
  34. #include <linux/kernel.h>
  35. #include <linux/list.h>
  36. #include <linux/magic.h>
  37. #include <linux/mm.h>
  38. #include <linux/mutex.h>
  39. #include <linux/mount.h>
  40. #include <linux/pagemap.h>
  41. #include <linux/proc_fs.h>
  42. #include <linux/rcupdate.h>
  43. #include <linux/sched.h>
  44. #include <linux/slab.h>
  45. #include <linux/spinlock.h>
  46. #include <linux/percpu-rwsem.h>
  47. #include <linux/string.h>
  48. #include <linux/sort.h>
  49. #include <linux/kmod.h>
  50. #include <linux/delayacct.h>
  51. #include <linux/cgroupstats.h>
  52. #include <linux/hashtable.h>
  53. #include <linux/pid_namespace.h>
  54. #include <linux/idr.h>
  55. #include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
  56. #include <linux/kthread.h>
  57. #include <linux/delay.h>
  58. #include <linux/atomic.h>
  59. #include <linux/cpuset.h>
  60. #include <net/sock.h>
  61. /*
  62. * pidlists linger the following amount before being destroyed. The goal
  63. * is avoiding frequent destruction in the middle of consecutive read calls
  64. * Expiring in the middle is a performance problem not a correctness one.
  65. * 1 sec should be enough.
  66. */
  67. #define CGROUP_PIDLIST_DESTROY_DELAY HZ
  68. #define CGROUP_FILE_NAME_MAX (MAX_CGROUP_TYPE_NAMELEN + \
  69. MAX_CFTYPE_NAME + 2)
  70. /*
  71. * cgroup_mutex is the master lock. Any modification to cgroup or its
  72. * hierarchy must be performed while holding it.
  73. *
  74. * css_set_lock protects task->cgroups pointer, the list of css_set
  75. * objects, and the chain of tasks off each css_set.
  76. *
  77. * These locks are exported if CONFIG_PROVE_RCU so that accessors in
  78. * cgroup.h can use them for lockdep annotations.
  79. */
  80. #ifdef CONFIG_PROVE_RCU
  81. DEFINE_MUTEX(cgroup_mutex);
  82. DEFINE_SPINLOCK(css_set_lock);
  83. EXPORT_SYMBOL_GPL(cgroup_mutex);
  84. EXPORT_SYMBOL_GPL(css_set_lock);
  85. #else
  86. static DEFINE_MUTEX(cgroup_mutex);
  87. static DEFINE_SPINLOCK(css_set_lock);
  88. #endif
  89. /*
  90. * Protects cgroup_idr and css_idr so that IDs can be released without
  91. * grabbing cgroup_mutex.
  92. */
  93. static DEFINE_SPINLOCK(cgroup_idr_lock);
  94. /*
  95. * Protects cgroup_file->kn for !self csses. It synchronizes notifications
  96. * against file removal/re-creation across css hiding.
  97. */
  98. static DEFINE_SPINLOCK(cgroup_file_kn_lock);
  99. /*
  100. * Protects cgroup_subsys->release_agent_path. Modifying it also requires
  101. * cgroup_mutex. Reading requires either cgroup_mutex or this spinlock.
  102. */
  103. static DEFINE_SPINLOCK(release_agent_path_lock);
  104. struct percpu_rw_semaphore cgroup_threadgroup_rwsem;
  105. #define cgroup_assert_mutex_or_rcu_locked() \
  106. RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \
  107. !lockdep_is_held(&cgroup_mutex), \
  108. "cgroup_mutex or RCU read lock required");
  109. /*
  110. * cgroup destruction makes heavy use of work items and there can be a lot
  111. * of concurrent destructions. Use a separate workqueue so that cgroup
  112. * destruction work items don't end up filling up max_active of system_wq
  113. * which may lead to deadlock.
  114. */
  115. static struct workqueue_struct *cgroup_destroy_wq;
  116. /*
  117. * pidlist destructions need to be flushed on cgroup destruction. Use a
  118. * separate workqueue as flush domain.
  119. */
  120. static struct workqueue_struct *cgroup_pidlist_destroy_wq;
  121. /* generate an array of cgroup subsystem pointers */
  122. #define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys,
  123. static struct cgroup_subsys *cgroup_subsys[] = {
  124. #include <linux/cgroup_subsys.h>
  125. };
  126. #undef SUBSYS
  127. /* array of cgroup subsystem names */
  128. #define SUBSYS(_x) [_x ## _cgrp_id] = #_x,
  129. static const char *cgroup_subsys_name[] = {
  130. #include <linux/cgroup_subsys.h>
  131. };
  132. #undef SUBSYS
  133. /* array of static_keys for cgroup_subsys_enabled() and cgroup_subsys_on_dfl() */
  134. #define SUBSYS(_x) \
  135. DEFINE_STATIC_KEY_TRUE(_x ## _cgrp_subsys_enabled_key); \
  136. DEFINE_STATIC_KEY_TRUE(_x ## _cgrp_subsys_on_dfl_key); \
  137. EXPORT_SYMBOL_GPL(_x ## _cgrp_subsys_enabled_key); \
  138. EXPORT_SYMBOL_GPL(_x ## _cgrp_subsys_on_dfl_key);
  139. #include <linux/cgroup_subsys.h>
  140. #undef SUBSYS
  141. #define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys_enabled_key,
  142. static struct static_key_true *cgroup_subsys_enabled_key[] = {
  143. #include <linux/cgroup_subsys.h>
  144. };
  145. #undef SUBSYS
  146. #define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys_on_dfl_key,
  147. static struct static_key_true *cgroup_subsys_on_dfl_key[] = {
  148. #include <linux/cgroup_subsys.h>
  149. };
  150. #undef SUBSYS
  151. /*
  152. * The default hierarchy, reserved for the subsystems that are otherwise
  153. * unattached - it never has more than a single cgroup, and all tasks are
  154. * part of that cgroup.
  155. */
  156. struct cgroup_root cgrp_dfl_root;
  157. EXPORT_SYMBOL_GPL(cgrp_dfl_root);
  158. /*
  159. * The default hierarchy always exists but is hidden until mounted for the
  160. * first time. This is for backward compatibility.
  161. */
  162. static bool cgrp_dfl_root_visible;
  163. /* some controllers are not supported in the default hierarchy */
  164. static unsigned long cgrp_dfl_root_inhibit_ss_mask;
  165. /* The list of hierarchy roots */
  166. static LIST_HEAD(cgroup_roots);
  167. static int cgroup_root_count;
  168. /* hierarchy ID allocation and mapping, protected by cgroup_mutex */
  169. static DEFINE_IDR(cgroup_hierarchy_idr);
  170. /*
  171. * Assign a monotonically increasing serial number to csses. It guarantees
  172. * cgroups with bigger numbers are newer than those with smaller numbers.
  173. * Also, as csses are always appended to the parent's ->children list, it
  174. * guarantees that sibling csses are always sorted in the ascending serial
  175. * number order on the list. Protected by cgroup_mutex.
  176. */
  177. static u64 css_serial_nr_next = 1;
  178. /*
  179. * These bitmask flags indicate whether tasks in the fork and exit paths have
  180. * fork/exit handlers to call. This avoids us having to do extra work in the
  181. * fork/exit path to check which subsystems have fork/exit callbacks.
  182. */
  183. static unsigned long have_fork_callback __read_mostly;
  184. static unsigned long have_exit_callback __read_mostly;
  185. static unsigned long have_free_callback __read_mostly;
  186. /* Ditto for the can_fork callback. */
  187. static unsigned long have_canfork_callback __read_mostly;
  188. static struct file_system_type cgroup2_fs_type;
  189. static struct cftype cgroup_dfl_base_files[];
  190. static struct cftype cgroup_legacy_base_files[];
  191. static int rebind_subsystems(struct cgroup_root *dst_root,
  192. unsigned long ss_mask);
  193. static void css_task_iter_advance(struct css_task_iter *it);
  194. static int cgroup_destroy_locked(struct cgroup *cgrp);
  195. static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss,
  196. bool visible);
  197. static void css_release(struct percpu_ref *ref);
  198. static void kill_css(struct cgroup_subsys_state *css);
  199. static int cgroup_addrm_files(struct cgroup_subsys_state *css,
  200. struct cgroup *cgrp, struct cftype cfts[],
  201. bool is_add);
  202. /**
  203. * cgroup_ssid_enabled - cgroup subsys enabled test by subsys ID
  204. * @ssid: subsys ID of interest
  205. *
  206. * cgroup_subsys_enabled() can only be used with literal subsys names which
  207. * is fine for individual subsystems but unsuitable for cgroup core. This
  208. * is slower static_key_enabled() based test indexed by @ssid.
  209. */
  210. static bool cgroup_ssid_enabled(int ssid)
  211. {
  212. return static_key_enabled(cgroup_subsys_enabled_key[ssid]);
  213. }
  214. /**
  215. * cgroup_on_dfl - test whether a cgroup is on the default hierarchy
  216. * @cgrp: the cgroup of interest
  217. *
  218. * The default hierarchy is the v2 interface of cgroup and this function
  219. * can be used to test whether a cgroup is on the default hierarchy for
  220. * cases where a subsystem should behave differnetly depending on the
  221. * interface version.
  222. *
  223. * The set of behaviors which change on the default hierarchy are still
  224. * being determined and the mount option is prefixed with __DEVEL__.
  225. *
  226. * List of changed behaviors:
  227. *
  228. * - Mount options "noprefix", "xattr", "clone_children", "release_agent"
  229. * and "name" are disallowed.
  230. *
  231. * - When mounting an existing superblock, mount options should match.
  232. *
  233. * - Remount is disallowed.
  234. *
  235. * - rename(2) is disallowed.
  236. *
  237. * - "tasks" is removed. Everything should be at process granularity. Use
  238. * "cgroup.procs" instead.
  239. *
  240. * - "cgroup.procs" is not sorted. pids will be unique unless they got
  241. * recycled inbetween reads.
  242. *
  243. * - "release_agent" and "notify_on_release" are removed. Replacement
  244. * notification mechanism will be implemented.
  245. *
  246. * - "cgroup.clone_children" is removed.
  247. *
  248. * - "cgroup.subtree_populated" is available. Its value is 0 if the cgroup
  249. * and its descendants contain no task; otherwise, 1. The file also
  250. * generates kernfs notification which can be monitored through poll and
  251. * [di]notify when the value of the file changes.
  252. *
  253. * - cpuset: tasks will be kept in empty cpusets when hotplug happens and
  254. * take masks of ancestors with non-empty cpus/mems, instead of being
  255. * moved to an ancestor.
  256. *
  257. * - cpuset: a task can be moved into an empty cpuset, and again it takes
  258. * masks of ancestors.
  259. *
  260. * - memcg: use_hierarchy is on by default and the cgroup file for the flag
  261. * is not created.
  262. *
  263. * - blkcg: blk-throttle becomes properly hierarchical.
  264. *
  265. * - debug: disallowed on the default hierarchy.
  266. */
  267. static bool cgroup_on_dfl(const struct cgroup *cgrp)
  268. {
  269. return cgrp->root == &cgrp_dfl_root;
  270. }
  271. /* IDR wrappers which synchronize using cgroup_idr_lock */
  272. static int cgroup_idr_alloc(struct idr *idr, void *ptr, int start, int end,
  273. gfp_t gfp_mask)
  274. {
  275. int ret;
  276. idr_preload(gfp_mask);
  277. spin_lock_bh(&cgroup_idr_lock);
  278. ret = idr_alloc(idr, ptr, start, end, gfp_mask & ~__GFP_DIRECT_RECLAIM);
  279. spin_unlock_bh(&cgroup_idr_lock);
  280. idr_preload_end();
  281. return ret;
  282. }
  283. static void *cgroup_idr_replace(struct idr *idr, void *ptr, int id)
  284. {
  285. void *ret;
  286. spin_lock_bh(&cgroup_idr_lock);
  287. ret = idr_replace(idr, ptr, id);
  288. spin_unlock_bh(&cgroup_idr_lock);
  289. return ret;
  290. }
  291. static void cgroup_idr_remove(struct idr *idr, int id)
  292. {
  293. spin_lock_bh(&cgroup_idr_lock);
  294. idr_remove(idr, id);
  295. spin_unlock_bh(&cgroup_idr_lock);
  296. }
  297. static struct cgroup *cgroup_parent(struct cgroup *cgrp)
  298. {
  299. struct cgroup_subsys_state *parent_css = cgrp->self.parent;
  300. if (parent_css)
  301. return container_of(parent_css, struct cgroup, self);
  302. return NULL;
  303. }
  304. /**
  305. * cgroup_css - obtain a cgroup's css for the specified subsystem
  306. * @cgrp: the cgroup of interest
  307. * @ss: the subsystem of interest (%NULL returns @cgrp->self)
  308. *
  309. * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This
  310. * function must be called either under cgroup_mutex or rcu_read_lock() and
  311. * the caller is responsible for pinning the returned css if it wants to
  312. * keep accessing it outside the said locks. This function may return
  313. * %NULL if @cgrp doesn't have @subsys_id enabled.
  314. */
  315. static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp,
  316. struct cgroup_subsys *ss)
  317. {
  318. if (ss)
  319. return rcu_dereference_check(cgrp->subsys[ss->id],
  320. lockdep_is_held(&cgroup_mutex));
  321. else
  322. return &cgrp->self;
  323. }
  324. /**
  325. * cgroup_e_css - obtain a cgroup's effective css for the specified subsystem
  326. * @cgrp: the cgroup of interest
  327. * @ss: the subsystem of interest (%NULL returns @cgrp->self)
  328. *
  329. * Similar to cgroup_css() but returns the effective css, which is defined
  330. * as the matching css of the nearest ancestor including self which has @ss
  331. * enabled. If @ss is associated with the hierarchy @cgrp is on, this
  332. * function is guaranteed to return non-NULL css.
  333. */
  334. static struct cgroup_subsys_state *cgroup_e_css(struct cgroup *cgrp,
  335. struct cgroup_subsys *ss)
  336. {
  337. lockdep_assert_held(&cgroup_mutex);
  338. if (!ss)
  339. return &cgrp->self;
  340. if (!(cgrp->root->subsys_mask & (1 << ss->id)))
  341. return NULL;
  342. /*
  343. * This function is used while updating css associations and thus
  344. * can't test the csses directly. Use ->child_subsys_mask.
  345. */
  346. while (cgroup_parent(cgrp) &&
  347. !(cgroup_parent(cgrp)->child_subsys_mask & (1 << ss->id)))
  348. cgrp = cgroup_parent(cgrp);
  349. return cgroup_css(cgrp, ss);
  350. }
  351. /**
  352. * cgroup_get_e_css - get a cgroup's effective css for the specified subsystem
  353. * @cgrp: the cgroup of interest
  354. * @ss: the subsystem of interest
  355. *
  356. * Find and get the effective css of @cgrp for @ss. The effective css is
  357. * defined as the matching css of the nearest ancestor including self which
  358. * has @ss enabled. If @ss is not mounted on the hierarchy @cgrp is on,
  359. * the root css is returned, so this function always returns a valid css.
  360. * The returned css must be put using css_put().
  361. */
  362. struct cgroup_subsys_state *cgroup_get_e_css(struct cgroup *cgrp,
  363. struct cgroup_subsys *ss)
  364. {
  365. struct cgroup_subsys_state *css;
  366. rcu_read_lock();
  367. do {
  368. css = cgroup_css(cgrp, ss);
  369. if (css && css_tryget_online(css))
  370. goto out_unlock;
  371. cgrp = cgroup_parent(cgrp);
  372. } while (cgrp);
  373. css = init_css_set.subsys[ss->id];
  374. css_get(css);
  375. out_unlock:
  376. rcu_read_unlock();
  377. return css;
  378. }
  379. /* convenient tests for these bits */
  380. static inline bool cgroup_is_dead(const struct cgroup *cgrp)
  381. {
  382. return !(cgrp->self.flags & CSS_ONLINE);
  383. }
  384. static void cgroup_get(struct cgroup *cgrp)
  385. {
  386. WARN_ON_ONCE(cgroup_is_dead(cgrp));
  387. css_get(&cgrp->self);
  388. }
  389. static bool cgroup_tryget(struct cgroup *cgrp)
  390. {
  391. return css_tryget(&cgrp->self);
  392. }
  393. struct cgroup_subsys_state *of_css(struct kernfs_open_file *of)
  394. {
  395. struct cgroup *cgrp = of->kn->parent->priv;
  396. struct cftype *cft = of_cft(of);
  397. /*
  398. * This is open and unprotected implementation of cgroup_css().
  399. * seq_css() is only called from a kernfs file operation which has
  400. * an active reference on the file. Because all the subsystem
  401. * files are drained before a css is disassociated with a cgroup,
  402. * the matching css from the cgroup's subsys table is guaranteed to
  403. * be and stay valid until the enclosing operation is complete.
  404. */
  405. if (cft->ss)
  406. return rcu_dereference_raw(cgrp->subsys[cft->ss->id]);
  407. else
  408. return &cgrp->self;
  409. }
  410. EXPORT_SYMBOL_GPL(of_css);
  411. static int notify_on_release(const struct cgroup *cgrp)
  412. {
  413. return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
  414. }
  415. /**
  416. * for_each_css - iterate all css's of a cgroup
  417. * @css: the iteration cursor
  418. * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
  419. * @cgrp: the target cgroup to iterate css's of
  420. *
  421. * Should be called under cgroup_[tree_]mutex.
  422. */
  423. #define for_each_css(css, ssid, cgrp) \
  424. for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
  425. if (!((css) = rcu_dereference_check( \
  426. (cgrp)->subsys[(ssid)], \
  427. lockdep_is_held(&cgroup_mutex)))) { } \
  428. else
  429. /**
  430. * for_each_e_css - iterate all effective css's of a cgroup
  431. * @css: the iteration cursor
  432. * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
  433. * @cgrp: the target cgroup to iterate css's of
  434. *
  435. * Should be called under cgroup_[tree_]mutex.
  436. */
  437. #define for_each_e_css(css, ssid, cgrp) \
  438. for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
  439. if (!((css) = cgroup_e_css(cgrp, cgroup_subsys[(ssid)]))) \
  440. ; \
  441. else
  442. /**
  443. * for_each_subsys - iterate all enabled cgroup subsystems
  444. * @ss: the iteration cursor
  445. * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
  446. */
  447. #define for_each_subsys(ss, ssid) \
  448. for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT && \
  449. (((ss) = cgroup_subsys[ssid]) || true); (ssid)++)
  450. /**
  451. * for_each_subsys_which - filter for_each_subsys with a bitmask
  452. * @ss: the iteration cursor
  453. * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
  454. * @ss_maskp: a pointer to the bitmask
  455. *
  456. * The block will only run for cases where the ssid-th bit (1 << ssid) of
  457. * mask is set to 1.
  458. */
  459. #define for_each_subsys_which(ss, ssid, ss_maskp) \
  460. if (!CGROUP_SUBSYS_COUNT) /* to avoid spurious gcc warning */ \
  461. (ssid) = 0; \
  462. else \
  463. for_each_set_bit(ssid, ss_maskp, CGROUP_SUBSYS_COUNT) \
  464. if (((ss) = cgroup_subsys[ssid]) && false) \
  465. break; \
  466. else
  467. /* iterate across the hierarchies */
  468. #define for_each_root(root) \
  469. list_for_each_entry((root), &cgroup_roots, root_list)
  470. /* iterate over child cgrps, lock should be held throughout iteration */
  471. #define cgroup_for_each_live_child(child, cgrp) \
  472. list_for_each_entry((child), &(cgrp)->self.children, self.sibling) \
  473. if (({ lockdep_assert_held(&cgroup_mutex); \
  474. cgroup_is_dead(child); })) \
  475. ; \
  476. else
  477. static void cgroup_release_agent(struct work_struct *work);
  478. static void check_for_release(struct cgroup *cgrp);
  479. /*
  480. * A cgroup can be associated with multiple css_sets as different tasks may
  481. * belong to different cgroups on different hierarchies. In the other
  482. * direction, a css_set is naturally associated with multiple cgroups.
  483. * This M:N relationship is represented by the following link structure
  484. * which exists for each association and allows traversing the associations
  485. * from both sides.
  486. */
  487. struct cgrp_cset_link {
  488. /* the cgroup and css_set this link associates */
  489. struct cgroup *cgrp;
  490. struct css_set *cset;
  491. /* list of cgrp_cset_links anchored at cgrp->cset_links */
  492. struct list_head cset_link;
  493. /* list of cgrp_cset_links anchored at css_set->cgrp_links */
  494. struct list_head cgrp_link;
  495. };
  496. /*
  497. * The default css_set - used by init and its children prior to any
  498. * hierarchies being mounted. It contains a pointer to the root state
  499. * for each subsystem. Also used to anchor the list of css_sets. Not
  500. * reference-counted, to improve performance when child cgroups
  501. * haven't been created.
  502. */
  503. struct css_set init_css_set = {
  504. .refcount = ATOMIC_INIT(1),
  505. .cgrp_links = LIST_HEAD_INIT(init_css_set.cgrp_links),
  506. .tasks = LIST_HEAD_INIT(init_css_set.tasks),
  507. .mg_tasks = LIST_HEAD_INIT(init_css_set.mg_tasks),
  508. .mg_preload_node = LIST_HEAD_INIT(init_css_set.mg_preload_node),
  509. .mg_node = LIST_HEAD_INIT(init_css_set.mg_node),
  510. .task_iters = LIST_HEAD_INIT(init_css_set.task_iters),
  511. };
  512. static int css_set_count = 1; /* 1 for init_css_set */
  513. /**
  514. * css_set_populated - does a css_set contain any tasks?
  515. * @cset: target css_set
  516. */
  517. static bool css_set_populated(struct css_set *cset)
  518. {
  519. lockdep_assert_held(&css_set_lock);
  520. return !list_empty(&cset->tasks) || !list_empty(&cset->mg_tasks);
  521. }
  522. /**
  523. * cgroup_update_populated - updated populated count of a cgroup
  524. * @cgrp: the target cgroup
  525. * @populated: inc or dec populated count
  526. *
  527. * One of the css_sets associated with @cgrp is either getting its first
  528. * task or losing the last. Update @cgrp->populated_cnt accordingly. The
  529. * count is propagated towards root so that a given cgroup's populated_cnt
  530. * is zero iff the cgroup and all its descendants don't contain any tasks.
  531. *
  532. * @cgrp's interface file "cgroup.populated" is zero if
  533. * @cgrp->populated_cnt is zero and 1 otherwise. When @cgrp->populated_cnt
  534. * changes from or to zero, userland is notified that the content of the
  535. * interface file has changed. This can be used to detect when @cgrp and
  536. * its descendants become populated or empty.
  537. */
  538. static void cgroup_update_populated(struct cgroup *cgrp, bool populated)
  539. {
  540. lockdep_assert_held(&css_set_lock);
  541. do {
  542. bool trigger;
  543. if (populated)
  544. trigger = !cgrp->populated_cnt++;
  545. else
  546. trigger = !--cgrp->populated_cnt;
  547. if (!trigger)
  548. break;
  549. check_for_release(cgrp);
  550. cgroup_file_notify(&cgrp->events_file);
  551. cgrp = cgroup_parent(cgrp);
  552. } while (cgrp);
  553. }
  554. /**
  555. * css_set_update_populated - update populated state of a css_set
  556. * @cset: target css_set
  557. * @populated: whether @cset is populated or depopulated
  558. *
  559. * @cset is either getting the first task or losing the last. Update the
  560. * ->populated_cnt of all associated cgroups accordingly.
  561. */
  562. static void css_set_update_populated(struct css_set *cset, bool populated)
  563. {
  564. struct cgrp_cset_link *link;
  565. lockdep_assert_held(&css_set_lock);
  566. list_for_each_entry(link, &cset->cgrp_links, cgrp_link)
  567. cgroup_update_populated(link->cgrp, populated);
  568. }
  569. /**
  570. * css_set_move_task - move a task from one css_set to another
  571. * @task: task being moved
  572. * @from_cset: css_set @task currently belongs to (may be NULL)
  573. * @to_cset: new css_set @task is being moved to (may be NULL)
  574. * @use_mg_tasks: move to @to_cset->mg_tasks instead of ->tasks
  575. *
  576. * Move @task from @from_cset to @to_cset. If @task didn't belong to any
  577. * css_set, @from_cset can be NULL. If @task is being disassociated
  578. * instead of moved, @to_cset can be NULL.
  579. *
  580. * This function automatically handles populated_cnt updates and
  581. * css_task_iter adjustments but the caller is responsible for managing
  582. * @from_cset and @to_cset's reference counts.
  583. */
  584. static void css_set_move_task(struct task_struct *task,
  585. struct css_set *from_cset, struct css_set *to_cset,
  586. bool use_mg_tasks)
  587. {
  588. lockdep_assert_held(&css_set_lock);
  589. if (from_cset) {
  590. struct css_task_iter *it, *pos;
  591. WARN_ON_ONCE(list_empty(&task->cg_list));
  592. /*
  593. * @task is leaving, advance task iterators which are
  594. * pointing to it so that they can resume at the next
  595. * position. Advancing an iterator might remove it from
  596. * the list, use safe walk. See css_task_iter_advance*()
  597. * for details.
  598. */
  599. list_for_each_entry_safe(it, pos, &from_cset->task_iters,
  600. iters_node)
  601. if (it->task_pos == &task->cg_list)
  602. css_task_iter_advance(it);
  603. list_del_init(&task->cg_list);
  604. if (!css_set_populated(from_cset))
  605. css_set_update_populated(from_cset, false);
  606. } else {
  607. WARN_ON_ONCE(!list_empty(&task->cg_list));
  608. }
  609. if (to_cset) {
  610. /*
  611. * We are synchronized through cgroup_threadgroup_rwsem
  612. * against PF_EXITING setting such that we can't race
  613. * against cgroup_exit() changing the css_set to
  614. * init_css_set and dropping the old one.
  615. */
  616. WARN_ON_ONCE(task->flags & PF_EXITING);
  617. if (!css_set_populated(to_cset))
  618. css_set_update_populated(to_cset, true);
  619. rcu_assign_pointer(task->cgroups, to_cset);
  620. list_add_tail(&task->cg_list, use_mg_tasks ? &to_cset->mg_tasks :
  621. &to_cset->tasks);
  622. }
  623. }
  624. /*
  625. * hash table for cgroup groups. This improves the performance to find
  626. * an existing css_set. This hash doesn't (currently) take into
  627. * account cgroups in empty hierarchies.
  628. */
  629. #define CSS_SET_HASH_BITS 7
  630. static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
  631. static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
  632. {
  633. unsigned long key = 0UL;
  634. struct cgroup_subsys *ss;
  635. int i;
  636. for_each_subsys(ss, i)
  637. key += (unsigned long)css[i];
  638. key = (key >> 16) ^ key;
  639. return key;
  640. }
  641. static void put_css_set_locked(struct css_set *cset)
  642. {
  643. struct cgrp_cset_link *link, *tmp_link;
  644. struct cgroup_subsys *ss;
  645. int ssid;
  646. lockdep_assert_held(&css_set_lock);
  647. if (!atomic_dec_and_test(&cset->refcount))
  648. return;
  649. /* This css_set is dead. unlink it and release cgroup and css refs */
  650. for_each_subsys(ss, ssid) {
  651. list_del(&cset->e_cset_node[ssid]);
  652. css_put(cset->subsys[ssid]);
  653. }
  654. hash_del(&cset->hlist);
  655. css_set_count--;
  656. list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
  657. list_del(&link->cset_link);
  658. list_del(&link->cgrp_link);
  659. if (cgroup_parent(link->cgrp))
  660. cgroup_put(link->cgrp);
  661. kfree(link);
  662. }
  663. kfree_rcu(cset, rcu_head);
  664. }
  665. static void put_css_set(struct css_set *cset)
  666. {
  667. /*
  668. * Ensure that the refcount doesn't hit zero while any readers
  669. * can see it. Similar to atomic_dec_and_lock(), but for an
  670. * rwlock
  671. */
  672. if (atomic_add_unless(&cset->refcount, -1, 1))
  673. return;
  674. spin_lock_bh(&css_set_lock);
  675. put_css_set_locked(cset);
  676. spin_unlock_bh(&css_set_lock);
  677. }
  678. /*
  679. * refcounted get/put for css_set objects
  680. */
  681. static inline void get_css_set(struct css_set *cset)
  682. {
  683. atomic_inc(&cset->refcount);
  684. }
  685. /**
  686. * compare_css_sets - helper function for find_existing_css_set().
  687. * @cset: candidate css_set being tested
  688. * @old_cset: existing css_set for a task
  689. * @new_cgrp: cgroup that's being entered by the task
  690. * @template: desired set of css pointers in css_set (pre-calculated)
  691. *
  692. * Returns true if "cset" matches "old_cset" except for the hierarchy
  693. * which "new_cgrp" belongs to, for which it should match "new_cgrp".
  694. */
  695. static bool compare_css_sets(struct css_set *cset,
  696. struct css_set *old_cset,
  697. struct cgroup *new_cgrp,
  698. struct cgroup_subsys_state *template[])
  699. {
  700. struct list_head *l1, *l2;
  701. /*
  702. * On the default hierarchy, there can be csets which are
  703. * associated with the same set of cgroups but different csses.
  704. * Let's first ensure that csses match.
  705. */
  706. if (memcmp(template, cset->subsys, sizeof(cset->subsys)))
  707. return false;
  708. /*
  709. * Compare cgroup pointers in order to distinguish between
  710. * different cgroups in hierarchies. As different cgroups may
  711. * share the same effective css, this comparison is always
  712. * necessary.
  713. */
  714. l1 = &cset->cgrp_links;
  715. l2 = &old_cset->cgrp_links;
  716. while (1) {
  717. struct cgrp_cset_link *link1, *link2;
  718. struct cgroup *cgrp1, *cgrp2;
  719. l1 = l1->next;
  720. l2 = l2->next;
  721. /* See if we reached the end - both lists are equal length. */
  722. if (l1 == &cset->cgrp_links) {
  723. BUG_ON(l2 != &old_cset->cgrp_links);
  724. break;
  725. } else {
  726. BUG_ON(l2 == &old_cset->cgrp_links);
  727. }
  728. /* Locate the cgroups associated with these links. */
  729. link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
  730. link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
  731. cgrp1 = link1->cgrp;
  732. cgrp2 = link2->cgrp;
  733. /* Hierarchies should be linked in the same order. */
  734. BUG_ON(cgrp1->root != cgrp2->root);
  735. /*
  736. * If this hierarchy is the hierarchy of the cgroup
  737. * that's changing, then we need to check that this
  738. * css_set points to the new cgroup; if it's any other
  739. * hierarchy, then this css_set should point to the
  740. * same cgroup as the old css_set.
  741. */
  742. if (cgrp1->root == new_cgrp->root) {
  743. if (cgrp1 != new_cgrp)
  744. return false;
  745. } else {
  746. if (cgrp1 != cgrp2)
  747. return false;
  748. }
  749. }
  750. return true;
  751. }
  752. /**
  753. * find_existing_css_set - init css array and find the matching css_set
  754. * @old_cset: the css_set that we're using before the cgroup transition
  755. * @cgrp: the cgroup that we're moving into
  756. * @template: out param for the new set of csses, should be clear on entry
  757. */
  758. static struct css_set *find_existing_css_set(struct css_set *old_cset,
  759. struct cgroup *cgrp,
  760. struct cgroup_subsys_state *template[])
  761. {
  762. struct cgroup_root *root = cgrp->root;
  763. struct cgroup_subsys *ss;
  764. struct css_set *cset;
  765. unsigned long key;
  766. int i;
  767. /*
  768. * Build the set of subsystem state objects that we want to see in the
  769. * new css_set. while subsystems can change globally, the entries here
  770. * won't change, so no need for locking.
  771. */
  772. for_each_subsys(ss, i) {
  773. if (root->subsys_mask & (1UL << i)) {
  774. /*
  775. * @ss is in this hierarchy, so we want the
  776. * effective css from @cgrp.
  777. */
  778. template[i] = cgroup_e_css(cgrp, ss);
  779. } else {
  780. /*
  781. * @ss is not in this hierarchy, so we don't want
  782. * to change the css.
  783. */
  784. template[i] = old_cset->subsys[i];
  785. }
  786. }
  787. key = css_set_hash(template);
  788. hash_for_each_possible(css_set_table, cset, hlist, key) {
  789. if (!compare_css_sets(cset, old_cset, cgrp, template))
  790. continue;
  791. /* This css_set matches what we need */
  792. return cset;
  793. }
  794. /* No existing cgroup group matched */
  795. return NULL;
  796. }
  797. static void free_cgrp_cset_links(struct list_head *links_to_free)
  798. {
  799. struct cgrp_cset_link *link, *tmp_link;
  800. list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
  801. list_del(&link->cset_link);
  802. kfree(link);
  803. }
  804. }
  805. /**
  806. * allocate_cgrp_cset_links - allocate cgrp_cset_links
  807. * @count: the number of links to allocate
  808. * @tmp_links: list_head the allocated links are put on
  809. *
  810. * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
  811. * through ->cset_link. Returns 0 on success or -errno.
  812. */
  813. static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
  814. {
  815. struct cgrp_cset_link *link;
  816. int i;
  817. INIT_LIST_HEAD(tmp_links);
  818. for (i = 0; i < count; i++) {
  819. link = kzalloc(sizeof(*link), GFP_KERNEL);
  820. if (!link) {
  821. free_cgrp_cset_links(tmp_links);
  822. return -ENOMEM;
  823. }
  824. list_add(&link->cset_link, tmp_links);
  825. }
  826. return 0;
  827. }
  828. /**
  829. * link_css_set - a helper function to link a css_set to a cgroup
  830. * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
  831. * @cset: the css_set to be linked
  832. * @cgrp: the destination cgroup
  833. */
  834. static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
  835. struct cgroup *cgrp)
  836. {
  837. struct cgrp_cset_link *link;
  838. BUG_ON(list_empty(tmp_links));
  839. if (cgroup_on_dfl(cgrp))
  840. cset->dfl_cgrp = cgrp;
  841. link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
  842. link->cset = cset;
  843. link->cgrp = cgrp;
  844. /*
  845. * Always add links to the tail of the lists so that the lists are
  846. * in choronological order.
  847. */
  848. list_move_tail(&link->cset_link, &cgrp->cset_links);
  849. list_add_tail(&link->cgrp_link, &cset->cgrp_links);
  850. if (cgroup_parent(cgrp))
  851. cgroup_get(cgrp);
  852. }
  853. /**
  854. * find_css_set - return a new css_set with one cgroup updated
  855. * @old_cset: the baseline css_set
  856. * @cgrp: the cgroup to be updated
  857. *
  858. * Return a new css_set that's equivalent to @old_cset, but with @cgrp
  859. * substituted into the appropriate hierarchy.
  860. */
  861. static struct css_set *find_css_set(struct css_set *old_cset,
  862. struct cgroup *cgrp)
  863. {
  864. struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { };
  865. struct css_set *cset;
  866. struct list_head tmp_links;
  867. struct cgrp_cset_link *link;
  868. struct cgroup_subsys *ss;
  869. unsigned long key;
  870. int ssid;
  871. lockdep_assert_held(&cgroup_mutex);
  872. /* First see if we already have a cgroup group that matches
  873. * the desired set */
  874. spin_lock_bh(&css_set_lock);
  875. cset = find_existing_css_set(old_cset, cgrp, template);
  876. if (cset)
  877. get_css_set(cset);
  878. spin_unlock_bh(&css_set_lock);
  879. if (cset)
  880. return cset;
  881. cset = kzalloc(sizeof(*cset), GFP_KERNEL);
  882. if (!cset)
  883. return NULL;
  884. /* Allocate all the cgrp_cset_link objects that we'll need */
  885. if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) {
  886. kfree(cset);
  887. return NULL;
  888. }
  889. atomic_set(&cset->refcount, 1);
  890. INIT_LIST_HEAD(&cset->cgrp_links);
  891. INIT_LIST_HEAD(&cset->tasks);
  892. INIT_LIST_HEAD(&cset->mg_tasks);
  893. INIT_LIST_HEAD(&cset->mg_preload_node);
  894. INIT_LIST_HEAD(&cset->mg_node);
  895. INIT_LIST_HEAD(&cset->task_iters);
  896. INIT_HLIST_NODE(&cset->hlist);
  897. /* Copy the set of subsystem state objects generated in
  898. * find_existing_css_set() */
  899. memcpy(cset->subsys, template, sizeof(cset->subsys));
  900. spin_lock_bh(&css_set_lock);
  901. /* Add reference counts and links from the new css_set. */
  902. list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
  903. struct cgroup *c = link->cgrp;
  904. if (c->root == cgrp->root)
  905. c = cgrp;
  906. link_css_set(&tmp_links, cset, c);
  907. }
  908. BUG_ON(!list_empty(&tmp_links));
  909. css_set_count++;
  910. /* Add @cset to the hash table */
  911. key = css_set_hash(cset->subsys);
  912. hash_add(css_set_table, &cset->hlist, key);
  913. for_each_subsys(ss, ssid) {
  914. struct cgroup_subsys_state *css = cset->subsys[ssid];
  915. list_add_tail(&cset->e_cset_node[ssid],
  916. &css->cgroup->e_csets[ssid]);
  917. css_get(css);
  918. }
  919. spin_unlock_bh(&css_set_lock);
  920. return cset;
  921. }
  922. static struct cgroup_root *cgroup_root_from_kf(struct kernfs_root *kf_root)
  923. {
  924. struct cgroup *root_cgrp = kf_root->kn->priv;
  925. return root_cgrp->root;
  926. }
  927. static int cgroup_init_root_id(struct cgroup_root *root)
  928. {
  929. int id;
  930. lockdep_assert_held(&cgroup_mutex);
  931. id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, 0, 0, GFP_KERNEL);
  932. if (id < 0)
  933. return id;
  934. root->hierarchy_id = id;
  935. return 0;
  936. }
  937. static void cgroup_exit_root_id(struct cgroup_root *root)
  938. {
  939. lockdep_assert_held(&cgroup_mutex);
  940. if (root->hierarchy_id) {
  941. idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
  942. root->hierarchy_id = 0;
  943. }
  944. }
  945. static void cgroup_free_root(struct cgroup_root *root)
  946. {
  947. if (root) {
  948. /* hierarchy ID should already have been released */
  949. WARN_ON_ONCE(root->hierarchy_id);
  950. idr_destroy(&root->cgroup_idr);
  951. kfree(root);
  952. }
  953. }
  954. static void cgroup_destroy_root(struct cgroup_root *root)
  955. {
  956. struct cgroup *cgrp = &root->cgrp;
  957. struct cgrp_cset_link *link, *tmp_link;
  958. mutex_lock(&cgroup_mutex);
  959. BUG_ON(atomic_read(&root->nr_cgrps));
  960. BUG_ON(!list_empty(&cgrp->self.children));
  961. /* Rebind all subsystems back to the default hierarchy */
  962. rebind_subsystems(&cgrp_dfl_root, root->subsys_mask);
  963. /*
  964. * Release all the links from cset_links to this hierarchy's
  965. * root cgroup
  966. */
  967. spin_lock_bh(&css_set_lock);
  968. list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
  969. list_del(&link->cset_link);
  970. list_del(&link->cgrp_link);
  971. kfree(link);
  972. }
  973. spin_unlock_bh(&css_set_lock);
  974. if (!list_empty(&root->root_list)) {
  975. list_del(&root->root_list);
  976. cgroup_root_count--;
  977. }
  978. cgroup_exit_root_id(root);
  979. mutex_unlock(&cgroup_mutex);
  980. kernfs_destroy_root(root->kf_root);
  981. cgroup_free_root(root);
  982. }
  983. /* look up cgroup associated with given css_set on the specified hierarchy */
  984. static struct cgroup *cset_cgroup_from_root(struct css_set *cset,
  985. struct cgroup_root *root)
  986. {
  987. struct cgroup *res = NULL;
  988. lockdep_assert_held(&cgroup_mutex);
  989. lockdep_assert_held(&css_set_lock);
  990. if (cset == &init_css_set) {
  991. res = &root->cgrp;
  992. } else {
  993. struct cgrp_cset_link *link;
  994. list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
  995. struct cgroup *c = link->cgrp;
  996. if (c->root == root) {
  997. res = c;
  998. break;
  999. }
  1000. }
  1001. }
  1002. BUG_ON(!res);
  1003. return res;
  1004. }
  1005. /*
  1006. * Return the cgroup for "task" from the given hierarchy. Must be
  1007. * called with cgroup_mutex and css_set_lock held.
  1008. */
  1009. static struct cgroup *task_cgroup_from_root(struct task_struct *task,
  1010. struct cgroup_root *root)
  1011. {
  1012. /*
  1013. * No need to lock the task - since we hold cgroup_mutex the
  1014. * task can't change groups, so the only thing that can happen
  1015. * is that it exits and its css is set back to init_css_set.
  1016. */
  1017. return cset_cgroup_from_root(task_css_set(task), root);
  1018. }
  1019. /*
  1020. * A task must hold cgroup_mutex to modify cgroups.
  1021. *
  1022. * Any task can increment and decrement the count field without lock.
  1023. * So in general, code holding cgroup_mutex can't rely on the count
  1024. * field not changing. However, if the count goes to zero, then only
  1025. * cgroup_attach_task() can increment it again. Because a count of zero
  1026. * means that no tasks are currently attached, therefore there is no
  1027. * way a task attached to that cgroup can fork (the other way to
  1028. * increment the count). So code holding cgroup_mutex can safely
  1029. * assume that if the count is zero, it will stay zero. Similarly, if
  1030. * a task holds cgroup_mutex on a cgroup with zero count, it
  1031. * knows that the cgroup won't be removed, as cgroup_rmdir()
  1032. * needs that mutex.
  1033. *
  1034. * A cgroup can only be deleted if both its 'count' of using tasks
  1035. * is zero, and its list of 'children' cgroups is empty. Since all
  1036. * tasks in the system use _some_ cgroup, and since there is always at
  1037. * least one task in the system (init, pid == 1), therefore, root cgroup
  1038. * always has either children cgroups and/or using tasks. So we don't
  1039. * need a special hack to ensure that root cgroup cannot be deleted.
  1040. *
  1041. * P.S. One more locking exception. RCU is used to guard the
  1042. * update of a tasks cgroup pointer by cgroup_attach_task()
  1043. */
  1044. static struct kernfs_syscall_ops cgroup_kf_syscall_ops;
  1045. static const struct file_operations proc_cgroupstats_operations;
  1046. static char *cgroup_file_name(struct cgroup *cgrp, const struct cftype *cft,
  1047. char *buf)
  1048. {
  1049. struct cgroup_subsys *ss = cft->ss;
  1050. if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) &&
  1051. !(cgrp->root->flags & CGRP_ROOT_NOPREFIX))
  1052. snprintf(buf, CGROUP_FILE_NAME_MAX, "%s.%s",
  1053. cgroup_on_dfl(cgrp) ? ss->name : ss->legacy_name,
  1054. cft->name);
  1055. else
  1056. strncpy(buf, cft->name, CGROUP_FILE_NAME_MAX);
  1057. return buf;
  1058. }
  1059. /**
  1060. * cgroup_file_mode - deduce file mode of a control file
  1061. * @cft: the control file in question
  1062. *
  1063. * S_IRUGO for read, S_IWUSR for write.
  1064. */
  1065. static umode_t cgroup_file_mode(const struct cftype *cft)
  1066. {
  1067. umode_t mode = 0;
  1068. if (cft->read_u64 || cft->read_s64 || cft->seq_show)
  1069. mode |= S_IRUGO;
  1070. if (cft->write_u64 || cft->write_s64 || cft->write) {
  1071. if (cft->flags & CFTYPE_WORLD_WRITABLE)
  1072. mode |= S_IWUGO;
  1073. else
  1074. mode |= S_IWUSR;
  1075. }
  1076. return mode;
  1077. }
  1078. /**
  1079. * cgroup_calc_child_subsys_mask - calculate child_subsys_mask
  1080. * @cgrp: the target cgroup
  1081. * @subtree_control: the new subtree_control mask to consider
  1082. *
  1083. * On the default hierarchy, a subsystem may request other subsystems to be
  1084. * enabled together through its ->depends_on mask. In such cases, more
  1085. * subsystems than specified in "cgroup.subtree_control" may be enabled.
  1086. *
  1087. * This function calculates which subsystems need to be enabled if
  1088. * @subtree_control is to be applied to @cgrp. The returned mask is always
  1089. * a superset of @subtree_control and follows the usual hierarchy rules.
  1090. */
  1091. static unsigned long cgroup_calc_child_subsys_mask(struct cgroup *cgrp,
  1092. unsigned long subtree_control)
  1093. {
  1094. struct cgroup *parent = cgroup_parent(cgrp);
  1095. unsigned long cur_ss_mask = subtree_control;
  1096. struct cgroup_subsys *ss;
  1097. int ssid;
  1098. lockdep_assert_held(&cgroup_mutex);
  1099. if (!cgroup_on_dfl(cgrp))
  1100. return cur_ss_mask;
  1101. while (true) {
  1102. unsigned long new_ss_mask = cur_ss_mask;
  1103. for_each_subsys_which(ss, ssid, &cur_ss_mask)
  1104. new_ss_mask |= ss->depends_on;
  1105. /*
  1106. * Mask out subsystems which aren't available. This can
  1107. * happen only if some depended-upon subsystems were bound
  1108. * to non-default hierarchies.
  1109. */
  1110. if (parent)
  1111. new_ss_mask &= parent->child_subsys_mask;
  1112. else
  1113. new_ss_mask &= cgrp->root->subsys_mask;
  1114. if (new_ss_mask == cur_ss_mask)
  1115. break;
  1116. cur_ss_mask = new_ss_mask;
  1117. }
  1118. return cur_ss_mask;
  1119. }
  1120. /**
  1121. * cgroup_refresh_child_subsys_mask - update child_subsys_mask
  1122. * @cgrp: the target cgroup
  1123. *
  1124. * Update @cgrp->child_subsys_mask according to the current
  1125. * @cgrp->subtree_control using cgroup_calc_child_subsys_mask().
  1126. */
  1127. static void cgroup_refresh_child_subsys_mask(struct cgroup *cgrp)
  1128. {
  1129. cgrp->child_subsys_mask =
  1130. cgroup_calc_child_subsys_mask(cgrp, cgrp->subtree_control);
  1131. }
  1132. /**
  1133. * cgroup_kn_unlock - unlocking helper for cgroup kernfs methods
  1134. * @kn: the kernfs_node being serviced
  1135. *
  1136. * This helper undoes cgroup_kn_lock_live() and should be invoked before
  1137. * the method finishes if locking succeeded. Note that once this function
  1138. * returns the cgroup returned by cgroup_kn_lock_live() may become
  1139. * inaccessible any time. If the caller intends to continue to access the
  1140. * cgroup, it should pin it before invoking this function.
  1141. */
  1142. static void cgroup_kn_unlock(struct kernfs_node *kn)
  1143. {
  1144. struct cgroup *cgrp;
  1145. if (kernfs_type(kn) == KERNFS_DIR)
  1146. cgrp = kn->priv;
  1147. else
  1148. cgrp = kn->parent->priv;
  1149. mutex_unlock(&cgroup_mutex);
  1150. kernfs_unbreak_active_protection(kn);
  1151. cgroup_put(cgrp);
  1152. }
  1153. /**
  1154. * cgroup_kn_lock_live - locking helper for cgroup kernfs methods
  1155. * @kn: the kernfs_node being serviced
  1156. *
  1157. * This helper is to be used by a cgroup kernfs method currently servicing
  1158. * @kn. It breaks the active protection, performs cgroup locking and
  1159. * verifies that the associated cgroup is alive. Returns the cgroup if
  1160. * alive; otherwise, %NULL. A successful return should be undone by a
  1161. * matching cgroup_kn_unlock() invocation.
  1162. *
  1163. * Any cgroup kernfs method implementation which requires locking the
  1164. * associated cgroup should use this helper. It avoids nesting cgroup
  1165. * locking under kernfs active protection and allows all kernfs operations
  1166. * including self-removal.
  1167. */
  1168. static struct cgroup *cgroup_kn_lock_live(struct kernfs_node *kn)
  1169. {
  1170. struct cgroup *cgrp;
  1171. if (kernfs_type(kn) == KERNFS_DIR)
  1172. cgrp = kn->priv;
  1173. else
  1174. cgrp = kn->parent->priv;
  1175. /*
  1176. * We're gonna grab cgroup_mutex which nests outside kernfs
  1177. * active_ref. cgroup liveliness check alone provides enough
  1178. * protection against removal. Ensure @cgrp stays accessible and
  1179. * break the active_ref protection.
  1180. */
  1181. if (!cgroup_tryget(cgrp))
  1182. return NULL;
  1183. kernfs_break_active_protection(kn);
  1184. mutex_lock(&cgroup_mutex);
  1185. if (!cgroup_is_dead(cgrp))
  1186. return cgrp;
  1187. cgroup_kn_unlock(kn);
  1188. return NULL;
  1189. }
  1190. static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
  1191. {
  1192. char name[CGROUP_FILE_NAME_MAX];
  1193. lockdep_assert_held(&cgroup_mutex);
  1194. if (cft->file_offset) {
  1195. struct cgroup_subsys_state *css = cgroup_css(cgrp, cft->ss);
  1196. struct cgroup_file *cfile = (void *)css + cft->file_offset;
  1197. spin_lock_irq(&cgroup_file_kn_lock);
  1198. cfile->kn = NULL;
  1199. spin_unlock_irq(&cgroup_file_kn_lock);
  1200. }
  1201. kernfs_remove_by_name(cgrp->kn, cgroup_file_name(cgrp, cft, name));
  1202. }
  1203. /**
  1204. * css_clear_dir - remove subsys files in a cgroup directory
  1205. * @css: taget css
  1206. * @cgrp_override: specify if target cgroup is different from css->cgroup
  1207. */
  1208. static void css_clear_dir(struct cgroup_subsys_state *css,
  1209. struct cgroup *cgrp_override)
  1210. {
  1211. struct cgroup *cgrp = cgrp_override ?: css->cgroup;
  1212. struct cftype *cfts;
  1213. list_for_each_entry(cfts, &css->ss->cfts, node)
  1214. cgroup_addrm_files(css, cgrp, cfts, false);
  1215. }
  1216. /**
  1217. * css_populate_dir - create subsys files in a cgroup directory
  1218. * @css: target css
  1219. * @cgrp_overried: specify if target cgroup is different from css->cgroup
  1220. *
  1221. * On failure, no file is added.
  1222. */
  1223. static int css_populate_dir(struct cgroup_subsys_state *css,
  1224. struct cgroup *cgrp_override)
  1225. {
  1226. struct cgroup *cgrp = cgrp_override ?: css->cgroup;
  1227. struct cftype *cfts, *failed_cfts;
  1228. int ret;
  1229. if (!css->ss) {
  1230. if (cgroup_on_dfl(cgrp))
  1231. cfts = cgroup_dfl_base_files;
  1232. else
  1233. cfts = cgroup_legacy_base_files;
  1234. return cgroup_addrm_files(&cgrp->self, cgrp, cfts, true);
  1235. }
  1236. list_for_each_entry(cfts, &css->ss->cfts, node) {
  1237. ret = cgroup_addrm_files(css, cgrp, cfts, true);
  1238. if (ret < 0) {
  1239. failed_cfts = cfts;
  1240. goto err;
  1241. }
  1242. }
  1243. return 0;
  1244. err:
  1245. list_for_each_entry(cfts, &css->ss->cfts, node) {
  1246. if (cfts == failed_cfts)
  1247. break;
  1248. cgroup_addrm_files(css, cgrp, cfts, false);
  1249. }
  1250. return ret;
  1251. }
  1252. static int rebind_subsystems(struct cgroup_root *dst_root,
  1253. unsigned long ss_mask)
  1254. {
  1255. struct cgroup *dcgrp = &dst_root->cgrp;
  1256. struct cgroup_subsys *ss;
  1257. unsigned long tmp_ss_mask;
  1258. int ssid, i, ret;
  1259. lockdep_assert_held(&cgroup_mutex);
  1260. for_each_subsys_which(ss, ssid, &ss_mask) {
  1261. /* if @ss has non-root csses attached to it, can't move */
  1262. if (css_next_child(NULL, cgroup_css(&ss->root->cgrp, ss)))
  1263. return -EBUSY;
  1264. /* can't move between two non-dummy roots either */
  1265. if (ss->root != &cgrp_dfl_root && dst_root != &cgrp_dfl_root)
  1266. return -EBUSY;
  1267. }
  1268. /* skip creating root files on dfl_root for inhibited subsystems */
  1269. tmp_ss_mask = ss_mask;
  1270. if (dst_root == &cgrp_dfl_root)
  1271. tmp_ss_mask &= ~cgrp_dfl_root_inhibit_ss_mask;
  1272. for_each_subsys_which(ss, ssid, &tmp_ss_mask) {
  1273. struct cgroup *scgrp = &ss->root->cgrp;
  1274. int tssid;
  1275. ret = css_populate_dir(cgroup_css(scgrp, ss), dcgrp);
  1276. if (!ret)
  1277. continue;
  1278. /*
  1279. * Rebinding back to the default root is not allowed to
  1280. * fail. Using both default and non-default roots should
  1281. * be rare. Moving subsystems back and forth even more so.
  1282. * Just warn about it and continue.
  1283. */
  1284. if (dst_root == &cgrp_dfl_root) {
  1285. if (cgrp_dfl_root_visible) {
  1286. pr_warn("failed to create files (%d) while rebinding 0x%lx to default root\n",
  1287. ret, ss_mask);
  1288. pr_warn("you may retry by moving them to a different hierarchy and unbinding\n");
  1289. }
  1290. continue;
  1291. }
  1292. for_each_subsys_which(ss, tssid, &tmp_ss_mask) {
  1293. if (tssid == ssid)
  1294. break;
  1295. css_clear_dir(cgroup_css(scgrp, ss), dcgrp);
  1296. }
  1297. return ret;
  1298. }
  1299. /*
  1300. * Nothing can fail from this point on. Remove files for the
  1301. * removed subsystems and rebind each subsystem.
  1302. */
  1303. for_each_subsys_which(ss, ssid, &ss_mask) {
  1304. struct cgroup_root *src_root = ss->root;
  1305. struct cgroup *scgrp = &src_root->cgrp;
  1306. struct cgroup_subsys_state *css = cgroup_css(scgrp, ss);
  1307. struct css_set *cset;
  1308. WARN_ON(!css || cgroup_css(dcgrp, ss));
  1309. css_clear_dir(css, NULL);
  1310. RCU_INIT_POINTER(scgrp->subsys[ssid], NULL);
  1311. rcu_assign_pointer(dcgrp->subsys[ssid], css);
  1312. ss->root = dst_root;
  1313. css->cgroup = dcgrp;
  1314. spin_lock_bh(&css_set_lock);
  1315. hash_for_each(css_set_table, i, cset, hlist)
  1316. list_move_tail(&cset->e_cset_node[ss->id],
  1317. &dcgrp->e_csets[ss->id]);
  1318. spin_unlock_bh(&css_set_lock);
  1319. src_root->subsys_mask &= ~(1 << ssid);
  1320. scgrp->subtree_control &= ~(1 << ssid);
  1321. cgroup_refresh_child_subsys_mask(scgrp);
  1322. /* default hierarchy doesn't enable controllers by default */
  1323. dst_root->subsys_mask |= 1 << ssid;
  1324. if (dst_root == &cgrp_dfl_root) {
  1325. static_branch_enable(cgroup_subsys_on_dfl_key[ssid]);
  1326. } else {
  1327. dcgrp->subtree_control |= 1 << ssid;
  1328. cgroup_refresh_child_subsys_mask(dcgrp);
  1329. static_branch_disable(cgroup_subsys_on_dfl_key[ssid]);
  1330. }
  1331. if (ss->bind)
  1332. ss->bind(css);
  1333. }
  1334. kernfs_activate(dcgrp->kn);
  1335. return 0;
  1336. }
  1337. static int cgroup_show_options(struct seq_file *seq,
  1338. struct kernfs_root *kf_root)
  1339. {
  1340. struct cgroup_root *root = cgroup_root_from_kf(kf_root);
  1341. struct cgroup_subsys *ss;
  1342. int ssid;
  1343. if (root != &cgrp_dfl_root)
  1344. for_each_subsys(ss, ssid)
  1345. if (root->subsys_mask & (1 << ssid))
  1346. seq_show_option(seq, ss->legacy_name, NULL);
  1347. if (root->flags & CGRP_ROOT_NOPREFIX)
  1348. seq_puts(seq, ",noprefix");
  1349. if (root->flags & CGRP_ROOT_XATTR)
  1350. seq_puts(seq, ",xattr");
  1351. spin_lock(&release_agent_path_lock);
  1352. if (strlen(root->release_agent_path))
  1353. seq_show_option(seq, "release_agent",
  1354. root->release_agent_path);
  1355. spin_unlock(&release_agent_path_lock);
  1356. if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags))
  1357. seq_puts(seq, ",clone_children");
  1358. if (strlen(root->name))
  1359. seq_show_option(seq, "name", root->name);
  1360. return 0;
  1361. }
  1362. struct cgroup_sb_opts {
  1363. unsigned long subsys_mask;
  1364. unsigned int flags;
  1365. char *release_agent;
  1366. bool cpuset_clone_children;
  1367. char *name;
  1368. /* User explicitly requested empty subsystem */
  1369. bool none;
  1370. };
  1371. static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
  1372. {
  1373. char *token, *o = data;
  1374. bool all_ss = false, one_ss = false;
  1375. unsigned long mask = -1UL;
  1376. struct cgroup_subsys *ss;
  1377. int nr_opts = 0;
  1378. int i;
  1379. #ifdef CONFIG_CPUSETS
  1380. mask = ~(1U << cpuset_cgrp_id);
  1381. #endif
  1382. memset(opts, 0, sizeof(*opts));
  1383. while ((token = strsep(&o, ",")) != NULL) {
  1384. nr_opts++;
  1385. if (!*token)
  1386. return -EINVAL;
  1387. if (!strcmp(token, "none")) {
  1388. /* Explicitly have no subsystems */
  1389. opts->none = true;
  1390. continue;
  1391. }
  1392. if (!strcmp(token, "all")) {
  1393. /* Mutually exclusive option 'all' + subsystem name */
  1394. if (one_ss)
  1395. return -EINVAL;
  1396. all_ss = true;
  1397. continue;
  1398. }
  1399. if (!strcmp(token, "noprefix")) {
  1400. opts->flags |= CGRP_ROOT_NOPREFIX;
  1401. continue;
  1402. }
  1403. if (!strcmp(token, "clone_children")) {
  1404. opts->cpuset_clone_children = true;
  1405. continue;
  1406. }
  1407. if (!strcmp(token, "xattr")) {
  1408. opts->flags |= CGRP_ROOT_XATTR;
  1409. continue;
  1410. }
  1411. if (!strncmp(token, "release_agent=", 14)) {
  1412. /* Specifying two release agents is forbidden */
  1413. if (opts->release_agent)
  1414. return -EINVAL;
  1415. opts->release_agent =
  1416. kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
  1417. if (!opts->release_agent)
  1418. return -ENOMEM;
  1419. continue;
  1420. }
  1421. if (!strncmp(token, "name=", 5)) {
  1422. const char *name = token + 5;
  1423. /* Can't specify an empty name */
  1424. if (!strlen(name))
  1425. return -EINVAL;
  1426. /* Must match [\w.-]+ */
  1427. for (i = 0; i < strlen(name); i++) {
  1428. char c = name[i];
  1429. if (isalnum(c))
  1430. continue;
  1431. if ((c == '.') || (c == '-') || (c == '_'))
  1432. continue;
  1433. return -EINVAL;
  1434. }
  1435. /* Specifying two names is forbidden */
  1436. if (opts->name)
  1437. return -EINVAL;
  1438. opts->name = kstrndup(name,
  1439. MAX_CGROUP_ROOT_NAMELEN - 1,
  1440. GFP_KERNEL);
  1441. if (!opts->name)
  1442. return -ENOMEM;
  1443. continue;
  1444. }
  1445. for_each_subsys(ss, i) {
  1446. if (strcmp(token, ss->legacy_name))
  1447. continue;
  1448. if (!cgroup_ssid_enabled(i))
  1449. continue;
  1450. /* Mutually exclusive option 'all' + subsystem name */
  1451. if (all_ss)
  1452. return -EINVAL;
  1453. opts->subsys_mask |= (1 << i);
  1454. one_ss = true;
  1455. break;
  1456. }
  1457. if (i == CGROUP_SUBSYS_COUNT)
  1458. return -ENOENT;
  1459. }
  1460. /*
  1461. * If the 'all' option was specified select all the subsystems,
  1462. * otherwise if 'none', 'name=' and a subsystem name options were
  1463. * not specified, let's default to 'all'
  1464. */
  1465. if (all_ss || (!one_ss && !opts->none && !opts->name))
  1466. for_each_subsys(ss, i)
  1467. if (cgroup_ssid_enabled(i))
  1468. opts->subsys_mask |= (1 << i);
  1469. /*
  1470. * We either have to specify by name or by subsystems. (So all
  1471. * empty hierarchies must have a name).
  1472. */
  1473. if (!opts->subsys_mask && !opts->name)
  1474. return -EINVAL;
  1475. /*
  1476. * Option noprefix was introduced just for backward compatibility
  1477. * with the old cpuset, so we allow noprefix only if mounting just
  1478. * the cpuset subsystem.
  1479. */
  1480. if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
  1481. return -EINVAL;
  1482. /* Can't specify "none" and some subsystems */
  1483. if (opts->subsys_mask && opts->none)
  1484. return -EINVAL;
  1485. return 0;
  1486. }
  1487. static int cgroup_remount(struct kernfs_root *kf_root, int *flags, char *data)
  1488. {
  1489. int ret = 0;
  1490. struct cgroup_root *root = cgroup_root_from_kf(kf_root);
  1491. struct cgroup_sb_opts opts;
  1492. unsigned long added_mask, removed_mask;
  1493. if (root == &cgrp_dfl_root) {
  1494. pr_err("remount is not allowed\n");
  1495. return -EINVAL;
  1496. }
  1497. mutex_lock(&cgroup_mutex);
  1498. /* See what subsystems are wanted */
  1499. ret = parse_cgroupfs_options(data, &opts);
  1500. if (ret)
  1501. goto out_unlock;
  1502. if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
  1503. pr_warn("option changes via remount are deprecated (pid=%d comm=%s)\n",
  1504. task_tgid_nr(current), current->comm);
  1505. added_mask = opts.subsys_mask & ~root->subsys_mask;
  1506. removed_mask = root->subsys_mask & ~opts.subsys_mask;
  1507. /* Don't allow flags or name to change at remount */
  1508. if ((opts.flags ^ root->flags) ||
  1509. (opts.name && strcmp(opts.name, root->name))) {
  1510. pr_err("option or name mismatch, new: 0x%x \"%s\", old: 0x%x \"%s\"\n",
  1511. opts.flags, opts.name ?: "", root->flags, root->name);
  1512. ret = -EINVAL;
  1513. goto out_unlock;
  1514. }
  1515. /* remounting is not allowed for populated hierarchies */
  1516. if (!list_empty(&root->cgrp.self.children)) {
  1517. ret = -EBUSY;
  1518. goto out_unlock;
  1519. }
  1520. ret = rebind_subsystems(root, added_mask);
  1521. if (ret)
  1522. goto out_unlock;
  1523. rebind_subsystems(&cgrp_dfl_root, removed_mask);
  1524. if (opts.release_agent) {
  1525. spin_lock(&release_agent_path_lock);
  1526. strcpy(root->release_agent_path, opts.release_agent);
  1527. spin_unlock(&release_agent_path_lock);
  1528. }
  1529. out_unlock:
  1530. kfree(opts.release_agent);
  1531. kfree(opts.name);
  1532. mutex_unlock(&cgroup_mutex);
  1533. return ret;
  1534. }
  1535. /*
  1536. * To reduce the fork() overhead for systems that are not actually using
  1537. * their cgroups capability, we don't maintain the lists running through
  1538. * each css_set to its tasks until we see the list actually used - in other
  1539. * words after the first mount.
  1540. */
  1541. static bool use_task_css_set_links __read_mostly;
  1542. static void cgroup_enable_task_cg_lists(void)
  1543. {
  1544. struct task_struct *p, *g;
  1545. spin_lock_bh(&css_set_lock);
  1546. if (use_task_css_set_links)
  1547. goto out_unlock;
  1548. use_task_css_set_links = true;
  1549. /*
  1550. * We need tasklist_lock because RCU is not safe against
  1551. * while_each_thread(). Besides, a forking task that has passed
  1552. * cgroup_post_fork() without seeing use_task_css_set_links = 1
  1553. * is not guaranteed to have its child immediately visible in the
  1554. * tasklist if we walk through it with RCU.
  1555. */
  1556. read_lock(&tasklist_lock);
  1557. do_each_thread(g, p) {
  1558. WARN_ON_ONCE(!list_empty(&p->cg_list) ||
  1559. task_css_set(p) != &init_css_set);
  1560. /*
  1561. * We should check if the process is exiting, otherwise
  1562. * it will race with cgroup_exit() in that the list
  1563. * entry won't be deleted though the process has exited.
  1564. * Do it while holding siglock so that we don't end up
  1565. * racing against cgroup_exit().
  1566. */
  1567. spin_lock_irq(&p->sighand->siglock);
  1568. if (!(p->flags & PF_EXITING)) {
  1569. struct css_set *cset = task_css_set(p);
  1570. if (!css_set_populated(cset))
  1571. css_set_update_populated(cset, true);
  1572. list_add_tail(&p->cg_list, &cset->tasks);
  1573. get_css_set(cset);
  1574. }
  1575. spin_unlock_irq(&p->sighand->siglock);
  1576. } while_each_thread(g, p);
  1577. read_unlock(&tasklist_lock);
  1578. out_unlock:
  1579. spin_unlock_bh(&css_set_lock);
  1580. }
  1581. static void init_cgroup_housekeeping(struct cgroup *cgrp)
  1582. {
  1583. struct cgroup_subsys *ss;
  1584. int ssid;
  1585. INIT_LIST_HEAD(&cgrp->self.sibling);
  1586. INIT_LIST_HEAD(&cgrp->self.children);
  1587. INIT_LIST_HEAD(&cgrp->cset_links);
  1588. INIT_LIST_HEAD(&cgrp->pidlists);
  1589. mutex_init(&cgrp->pidlist_mutex);
  1590. cgrp->self.cgroup = cgrp;
  1591. cgrp->self.flags |= CSS_ONLINE;
  1592. for_each_subsys(ss, ssid)
  1593. INIT_LIST_HEAD(&cgrp->e_csets[ssid]);
  1594. init_waitqueue_head(&cgrp->offline_waitq);
  1595. INIT_WORK(&cgrp->release_agent_work, cgroup_release_agent);
  1596. }
  1597. static void init_cgroup_root(struct cgroup_root *root,
  1598. struct cgroup_sb_opts *opts)
  1599. {
  1600. struct cgroup *cgrp = &root->cgrp;
  1601. INIT_LIST_HEAD(&root->root_list);
  1602. atomic_set(&root->nr_cgrps, 1);
  1603. cgrp->root = root;
  1604. init_cgroup_housekeeping(cgrp);
  1605. idr_init(&root->cgroup_idr);
  1606. root->flags = opts->flags;
  1607. if (opts->release_agent)
  1608. strcpy(root->release_agent_path, opts->release_agent);
  1609. if (opts->name)
  1610. strcpy(root->name, opts->name);
  1611. if (opts->cpuset_clone_children)
  1612. set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags);
  1613. }
  1614. static int cgroup_setup_root(struct cgroup_root *root, unsigned long ss_mask)
  1615. {
  1616. LIST_HEAD(tmp_links);
  1617. struct cgroup *root_cgrp = &root->cgrp;
  1618. struct css_set *cset;
  1619. int i, ret;
  1620. lockdep_assert_held(&cgroup_mutex);
  1621. ret = cgroup_idr_alloc(&root->cgroup_idr, root_cgrp, 1, 2, GFP_KERNEL);
  1622. if (ret < 0)
  1623. goto out;
  1624. root_cgrp->id = ret;
  1625. root_cgrp->ancestor_ids[0] = ret;
  1626. ret = percpu_ref_init(&root_cgrp->self.refcnt, css_release, 0,
  1627. GFP_KERNEL);
  1628. if (ret)
  1629. goto out;
  1630. /*
  1631. * We're accessing css_set_count without locking css_set_lock here,
  1632. * but that's OK - it can only be increased by someone holding
  1633. * cgroup_lock, and that's us. The worst that can happen is that we
  1634. * have some link structures left over
  1635. */
  1636. ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
  1637. if (ret)
  1638. goto cancel_ref;
  1639. ret = cgroup_init_root_id(root);
  1640. if (ret)
  1641. goto cancel_ref;
  1642. root->kf_root = kernfs_create_root(&cgroup_kf_syscall_ops,
  1643. KERNFS_ROOT_CREATE_DEACTIVATED,
  1644. root_cgrp);
  1645. if (IS_ERR(root->kf_root)) {
  1646. ret = PTR_ERR(root->kf_root);
  1647. goto exit_root_id;
  1648. }
  1649. root_cgrp->kn = root->kf_root->kn;
  1650. ret = css_populate_dir(&root_cgrp->self, NULL);
  1651. if (ret)
  1652. goto destroy_root;
  1653. ret = rebind_subsystems(root, ss_mask);
  1654. if (ret)
  1655. goto destroy_root;
  1656. /*
  1657. * There must be no failure case after here, since rebinding takes
  1658. * care of subsystems' refcounts, which are explicitly dropped in
  1659. * the failure exit path.
  1660. */
  1661. list_add(&root->root_list, &cgroup_roots);
  1662. cgroup_root_count++;
  1663. /*
  1664. * Link the root cgroup in this hierarchy into all the css_set
  1665. * objects.
  1666. */
  1667. spin_lock_bh(&css_set_lock);
  1668. hash_for_each(css_set_table, i, cset, hlist) {
  1669. link_css_set(&tmp_links, cset, root_cgrp);
  1670. if (css_set_populated(cset))
  1671. cgroup_update_populated(root_cgrp, true);
  1672. }
  1673. spin_unlock_bh(&css_set_lock);
  1674. BUG_ON(!list_empty(&root_cgrp->self.children));
  1675. BUG_ON(atomic_read(&root->nr_cgrps) != 1);
  1676. kernfs_activate(root_cgrp->kn);
  1677. ret = 0;
  1678. goto out;
  1679. destroy_root:
  1680. kernfs_destroy_root(root->kf_root);
  1681. root->kf_root = NULL;
  1682. exit_root_id:
  1683. cgroup_exit_root_id(root);
  1684. cancel_ref:
  1685. percpu_ref_exit(&root_cgrp->self.refcnt);
  1686. out:
  1687. free_cgrp_cset_links(&tmp_links);
  1688. return ret;
  1689. }
  1690. static struct dentry *cgroup_mount(struct file_system_type *fs_type,
  1691. int flags, const char *unused_dev_name,
  1692. void *data)
  1693. {
  1694. bool is_v2 = fs_type == &cgroup2_fs_type;
  1695. struct super_block *pinned_sb = NULL;
  1696. struct cgroup_subsys *ss;
  1697. struct cgroup_root *root;
  1698. struct cgroup_sb_opts opts;
  1699. struct dentry *dentry;
  1700. int ret;
  1701. int i;
  1702. bool new_sb;
  1703. /*
  1704. * The first time anyone tries to mount a cgroup, enable the list
  1705. * linking each css_set to its tasks and fix up all existing tasks.
  1706. */
  1707. if (!use_task_css_set_links)
  1708. cgroup_enable_task_cg_lists();
  1709. if (is_v2) {
  1710. if (data) {
  1711. pr_err("cgroup2: unknown option \"%s\"\n", (char *)data);
  1712. return ERR_PTR(-EINVAL);
  1713. }
  1714. cgrp_dfl_root_visible = true;
  1715. root = &cgrp_dfl_root;
  1716. cgroup_get(&root->cgrp);
  1717. goto out_mount;
  1718. }
  1719. mutex_lock(&cgroup_mutex);
  1720. /* First find the desired set of subsystems */
  1721. ret = parse_cgroupfs_options(data, &opts);
  1722. if (ret)
  1723. goto out_unlock;
  1724. /*
  1725. * Destruction of cgroup root is asynchronous, so subsystems may
  1726. * still be dying after the previous unmount. Let's drain the
  1727. * dying subsystems. We just need to ensure that the ones
  1728. * unmounted previously finish dying and don't care about new ones
  1729. * starting. Testing ref liveliness is good enough.
  1730. */
  1731. for_each_subsys(ss, i) {
  1732. if (!(opts.subsys_mask & (1 << i)) ||
  1733. ss->root == &cgrp_dfl_root)
  1734. continue;
  1735. if (!percpu_ref_tryget_live(&ss->root->cgrp.self.refcnt)) {
  1736. mutex_unlock(&cgroup_mutex);
  1737. msleep(10);
  1738. ret = restart_syscall();
  1739. goto out_free;
  1740. }
  1741. cgroup_put(&ss->root->cgrp);
  1742. }
  1743. for_each_root(root) {
  1744. bool name_match = false;
  1745. if (root == &cgrp_dfl_root)
  1746. continue;
  1747. /*
  1748. * If we asked for a name then it must match. Also, if
  1749. * name matches but sybsys_mask doesn't, we should fail.
  1750. * Remember whether name matched.
  1751. */
  1752. if (opts.name) {
  1753. if (strcmp(opts.name, root->name))
  1754. continue;
  1755. name_match = true;
  1756. }
  1757. /*
  1758. * If we asked for subsystems (or explicitly for no
  1759. * subsystems) then they must match.
  1760. */
  1761. if ((opts.subsys_mask || opts.none) &&
  1762. (opts.subsys_mask != root->subsys_mask)) {
  1763. if (!name_match)
  1764. continue;
  1765. ret = -EBUSY;
  1766. goto out_unlock;
  1767. }
  1768. if (root->flags ^ opts.flags)
  1769. pr_warn("new mount options do not match the existing superblock, will be ignored\n");
  1770. /*
  1771. * We want to reuse @root whose lifetime is governed by its
  1772. * ->cgrp. Let's check whether @root is alive and keep it
  1773. * that way. As cgroup_kill_sb() can happen anytime, we
  1774. * want to block it by pinning the sb so that @root doesn't
  1775. * get killed before mount is complete.
  1776. *
  1777. * With the sb pinned, tryget_live can reliably indicate
  1778. * whether @root can be reused. If it's being killed,
  1779. * drain it. We can use wait_queue for the wait but this
  1780. * path is super cold. Let's just sleep a bit and retry.
  1781. */
  1782. pinned_sb = kernfs_pin_sb(root->kf_root, NULL);
  1783. if (IS_ERR(pinned_sb) ||
  1784. !percpu_ref_tryget_live(&root->cgrp.self.refcnt)) {
  1785. mutex_unlock(&cgroup_mutex);
  1786. if (!IS_ERR_OR_NULL(pinned_sb))
  1787. deactivate_super(pinned_sb);
  1788. msleep(10);
  1789. ret = restart_syscall();
  1790. goto out_free;
  1791. }
  1792. ret = 0;
  1793. goto out_unlock;
  1794. }
  1795. /*
  1796. * No such thing, create a new one. name= matching without subsys
  1797. * specification is allowed for already existing hierarchies but we
  1798. * can't create new one without subsys specification.
  1799. */
  1800. if (!opts.subsys_mask && !opts.none) {
  1801. ret = -EINVAL;
  1802. goto out_unlock;
  1803. }
  1804. root = kzalloc(sizeof(*root), GFP_KERNEL);
  1805. if (!root) {
  1806. ret = -ENOMEM;
  1807. goto out_unlock;
  1808. }
  1809. init_cgroup_root(root, &opts);
  1810. ret = cgroup_setup_root(root, opts.subsys_mask);
  1811. if (ret)
  1812. cgroup_free_root(root);
  1813. out_unlock:
  1814. mutex_unlock(&cgroup_mutex);
  1815. out_free:
  1816. kfree(opts.release_agent);
  1817. kfree(opts.name);
  1818. if (ret)
  1819. return ERR_PTR(ret);
  1820. out_mount:
  1821. dentry = kernfs_mount(fs_type, flags, root->kf_root,
  1822. is_v2 ? CGROUP2_SUPER_MAGIC : CGROUP_SUPER_MAGIC,
  1823. &new_sb);
  1824. if (IS_ERR(dentry) || !new_sb)
  1825. cgroup_put(&root->cgrp);
  1826. /*
  1827. * If @pinned_sb, we're reusing an existing root and holding an
  1828. * extra ref on its sb. Mount is complete. Put the extra ref.
  1829. */
  1830. if (pinned_sb) {
  1831. WARN_ON(new_sb);
  1832. deactivate_super(pinned_sb);
  1833. }
  1834. return dentry;
  1835. }
  1836. static void cgroup_kill_sb(struct super_block *sb)
  1837. {
  1838. struct kernfs_root *kf_root = kernfs_root_from_sb(sb);
  1839. struct cgroup_root *root = cgroup_root_from_kf(kf_root);
  1840. /*
  1841. * If @root doesn't have any mounts or children, start killing it.
  1842. * This prevents new mounts by disabling percpu_ref_tryget_live().
  1843. * cgroup_mount() may wait for @root's release.
  1844. *
  1845. * And don't kill the default root.
  1846. */
  1847. if (!list_empty(&root->cgrp.self.children) ||
  1848. root == &cgrp_dfl_root)
  1849. cgroup_put(&root->cgrp);
  1850. else
  1851. percpu_ref_kill(&root->cgrp.self.refcnt);
  1852. kernfs_kill_sb(sb);
  1853. }
  1854. static struct file_system_type cgroup_fs_type = {
  1855. .name = "cgroup",
  1856. .mount = cgroup_mount,
  1857. .kill_sb = cgroup_kill_sb,
  1858. };
  1859. static struct file_system_type cgroup2_fs_type = {
  1860. .name = "cgroup2",
  1861. .mount = cgroup_mount,
  1862. .kill_sb = cgroup_kill_sb,
  1863. };
  1864. /**
  1865. * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
  1866. * @task: target task
  1867. * @buf: the buffer to write the path into
  1868. * @buflen: the length of the buffer
  1869. *
  1870. * Determine @task's cgroup on the first (the one with the lowest non-zero
  1871. * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
  1872. * function grabs cgroup_mutex and shouldn't be used inside locks used by
  1873. * cgroup controller callbacks.
  1874. *
  1875. * Return value is the same as kernfs_path().
  1876. */
  1877. char *task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
  1878. {
  1879. struct cgroup_root *root;
  1880. struct cgroup *cgrp;
  1881. int hierarchy_id = 1;
  1882. char *path = NULL;
  1883. mutex_lock(&cgroup_mutex);
  1884. spin_lock_bh(&css_set_lock);
  1885. root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
  1886. if (root) {
  1887. cgrp = task_cgroup_from_root(task, root);
  1888. path = cgroup_path(cgrp, buf, buflen);
  1889. } else {
  1890. /* if no hierarchy exists, everyone is in "/" */
  1891. if (strlcpy(buf, "/", buflen) < buflen)
  1892. path = buf;
  1893. }
  1894. spin_unlock_bh(&css_set_lock);
  1895. mutex_unlock(&cgroup_mutex);
  1896. return path;
  1897. }
  1898. EXPORT_SYMBOL_GPL(task_cgroup_path);
  1899. /* used to track tasks and other necessary states during migration */
  1900. struct cgroup_taskset {
  1901. /* the src and dst cset list running through cset->mg_node */
  1902. struct list_head src_csets;
  1903. struct list_head dst_csets;
  1904. /* the subsys currently being processed */
  1905. int ssid;
  1906. /*
  1907. * Fields for cgroup_taskset_*() iteration.
  1908. *
  1909. * Before migration is committed, the target migration tasks are on
  1910. * ->mg_tasks of the csets on ->src_csets. After, on ->mg_tasks of
  1911. * the csets on ->dst_csets. ->csets point to either ->src_csets
  1912. * or ->dst_csets depending on whether migration is committed.
  1913. *
  1914. * ->cur_csets and ->cur_task point to the current task position
  1915. * during iteration.
  1916. */
  1917. struct list_head *csets;
  1918. struct css_set *cur_cset;
  1919. struct task_struct *cur_task;
  1920. };
  1921. #define CGROUP_TASKSET_INIT(tset) (struct cgroup_taskset){ \
  1922. .src_csets = LIST_HEAD_INIT(tset.src_csets), \
  1923. .dst_csets = LIST_HEAD_INIT(tset.dst_csets), \
  1924. .csets = &tset.src_csets, \
  1925. }
  1926. /**
  1927. * cgroup_taskset_add - try to add a migration target task to a taskset
  1928. * @task: target task
  1929. * @tset: target taskset
  1930. *
  1931. * Add @task, which is a migration target, to @tset. This function becomes
  1932. * noop if @task doesn't need to be migrated. @task's css_set should have
  1933. * been added as a migration source and @task->cg_list will be moved from
  1934. * the css_set's tasks list to mg_tasks one.
  1935. */
  1936. static void cgroup_taskset_add(struct task_struct *task,
  1937. struct cgroup_taskset *tset)
  1938. {
  1939. struct css_set *cset;
  1940. lockdep_assert_held(&css_set_lock);
  1941. /* @task either already exited or can't exit until the end */
  1942. if (task->flags & PF_EXITING)
  1943. return;
  1944. /* leave @task alone if post_fork() hasn't linked it yet */
  1945. if (list_empty(&task->cg_list))
  1946. return;
  1947. cset = task_css_set(task);
  1948. if (!cset->mg_src_cgrp)
  1949. return;
  1950. list_move_tail(&task->cg_list, &cset->mg_tasks);
  1951. if (list_empty(&cset->mg_node))
  1952. list_add_tail(&cset->mg_node, &tset->src_csets);
  1953. if (list_empty(&cset->mg_dst_cset->mg_node))
  1954. list_move_tail(&cset->mg_dst_cset->mg_node,
  1955. &tset->dst_csets);
  1956. }
  1957. /**
  1958. * cgroup_taskset_first - reset taskset and return the first task
  1959. * @tset: taskset of interest
  1960. * @dst_cssp: output variable for the destination css
  1961. *
  1962. * @tset iteration is initialized and the first task is returned.
  1963. */
  1964. struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset,
  1965. struct cgroup_subsys_state **dst_cssp)
  1966. {
  1967. tset->cur_cset = list_first_entry(tset->csets, struct css_set, mg_node);
  1968. tset->cur_task = NULL;
  1969. return cgroup_taskset_next(tset, dst_cssp);
  1970. }
  1971. /**
  1972. * cgroup_taskset_next - iterate to the next task in taskset
  1973. * @tset: taskset of interest
  1974. * @dst_cssp: output variable for the destination css
  1975. *
  1976. * Return the next task in @tset. Iteration must have been initialized
  1977. * with cgroup_taskset_first().
  1978. */
  1979. struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset,
  1980. struct cgroup_subsys_state **dst_cssp)
  1981. {
  1982. struct css_set *cset = tset->cur_cset;
  1983. struct task_struct *task = tset->cur_task;
  1984. while (&cset->mg_node != tset->csets) {
  1985. if (!task)
  1986. task = list_first_entry(&cset->mg_tasks,
  1987. struct task_struct, cg_list);
  1988. else
  1989. task = list_next_entry(task, cg_list);
  1990. if (&task->cg_list != &cset->mg_tasks) {
  1991. tset->cur_cset = cset;
  1992. tset->cur_task = task;
  1993. /*
  1994. * This function may be called both before and
  1995. * after cgroup_taskset_migrate(). The two cases
  1996. * can be distinguished by looking at whether @cset
  1997. * has its ->mg_dst_cset set.
  1998. */
  1999. if (cset->mg_dst_cset)
  2000. *dst_cssp = cset->mg_dst_cset->subsys[tset->ssid];
  2001. else
  2002. *dst_cssp = cset->subsys[tset->ssid];
  2003. return task;
  2004. }
  2005. cset = list_next_entry(cset, mg_node);
  2006. task = NULL;
  2007. }
  2008. return NULL;
  2009. }
  2010. /**
  2011. * cgroup_taskset_migrate - migrate a taskset to a cgroup
  2012. * @tset: taget taskset
  2013. * @dst_cgrp: destination cgroup
  2014. *
  2015. * Migrate tasks in @tset to @dst_cgrp. This function fails iff one of the
  2016. * ->can_attach callbacks fails and guarantees that either all or none of
  2017. * the tasks in @tset are migrated. @tset is consumed regardless of
  2018. * success.
  2019. */
  2020. static int cgroup_taskset_migrate(struct cgroup_taskset *tset,
  2021. struct cgroup *dst_cgrp)
  2022. {
  2023. struct cgroup_subsys_state *css, *failed_css = NULL;
  2024. struct task_struct *task, *tmp_task;
  2025. struct css_set *cset, *tmp_cset;
  2026. int i, ret;
  2027. /* methods shouldn't be called if no task is actually migrating */
  2028. if (list_empty(&tset->src_csets))
  2029. return 0;
  2030. /* check that we can legitimately attach to the cgroup */
  2031. for_each_e_css(css, i, dst_cgrp) {
  2032. if (css->ss->can_attach) {
  2033. tset->ssid = i;
  2034. ret = css->ss->can_attach(tset);
  2035. if (ret) {
  2036. failed_css = css;
  2037. goto out_cancel_attach;
  2038. }
  2039. }
  2040. }
  2041. /*
  2042. * Now that we're guaranteed success, proceed to move all tasks to
  2043. * the new cgroup. There are no failure cases after here, so this
  2044. * is the commit point.
  2045. */
  2046. spin_lock_bh(&css_set_lock);
  2047. list_for_each_entry(cset, &tset->src_csets, mg_node) {
  2048. list_for_each_entry_safe(task, tmp_task, &cset->mg_tasks, cg_list) {
  2049. struct css_set *from_cset = task_css_set(task);
  2050. struct css_set *to_cset = cset->mg_dst_cset;
  2051. get_css_set(to_cset);
  2052. css_set_move_task(task, from_cset, to_cset, true);
  2053. put_css_set_locked(from_cset);
  2054. }
  2055. }
  2056. spin_unlock_bh(&css_set_lock);
  2057. /*
  2058. * Migration is committed, all target tasks are now on dst_csets.
  2059. * Nothing is sensitive to fork() after this point. Notify
  2060. * controllers that migration is complete.
  2061. */
  2062. tset->csets = &tset->dst_csets;
  2063. for_each_e_css(css, i, dst_cgrp) {
  2064. if (css->ss->attach) {
  2065. tset->ssid = i;
  2066. css->ss->attach(tset);
  2067. }
  2068. }
  2069. ret = 0;
  2070. goto out_release_tset;
  2071. out_cancel_attach:
  2072. for_each_e_css(css, i, dst_cgrp) {
  2073. if (css == failed_css)
  2074. break;
  2075. if (css->ss->cancel_attach) {
  2076. tset->ssid = i;
  2077. css->ss->cancel_attach(tset);
  2078. }
  2079. }
  2080. out_release_tset:
  2081. spin_lock_bh(&css_set_lock);
  2082. list_splice_init(&tset->dst_csets, &tset->src_csets);
  2083. list_for_each_entry_safe(cset, tmp_cset, &tset->src_csets, mg_node) {
  2084. list_splice_tail_init(&cset->mg_tasks, &cset->tasks);
  2085. list_del_init(&cset->mg_node);
  2086. }
  2087. spin_unlock_bh(&css_set_lock);
  2088. return ret;
  2089. }
  2090. /**
  2091. * cgroup_migrate_finish - cleanup after attach
  2092. * @preloaded_csets: list of preloaded css_sets
  2093. *
  2094. * Undo cgroup_migrate_add_src() and cgroup_migrate_prepare_dst(). See
  2095. * those functions for details.
  2096. */
  2097. static void cgroup_migrate_finish(struct list_head *preloaded_csets)
  2098. {
  2099. struct css_set *cset, *tmp_cset;
  2100. lockdep_assert_held(&cgroup_mutex);
  2101. spin_lock_bh(&css_set_lock);
  2102. list_for_each_entry_safe(cset, tmp_cset, preloaded_csets, mg_preload_node) {
  2103. cset->mg_src_cgrp = NULL;
  2104. cset->mg_dst_cset = NULL;
  2105. list_del_init(&cset->mg_preload_node);
  2106. put_css_set_locked(cset);
  2107. }
  2108. spin_unlock_bh(&css_set_lock);
  2109. }
  2110. /**
  2111. * cgroup_migrate_add_src - add a migration source css_set
  2112. * @src_cset: the source css_set to add
  2113. * @dst_cgrp: the destination cgroup
  2114. * @preloaded_csets: list of preloaded css_sets
  2115. *
  2116. * Tasks belonging to @src_cset are about to be migrated to @dst_cgrp. Pin
  2117. * @src_cset and add it to @preloaded_csets, which should later be cleaned
  2118. * up by cgroup_migrate_finish().
  2119. *
  2120. * This function may be called without holding cgroup_threadgroup_rwsem
  2121. * even if the target is a process. Threads may be created and destroyed
  2122. * but as long as cgroup_mutex is not dropped, no new css_set can be put
  2123. * into play and the preloaded css_sets are guaranteed to cover all
  2124. * migrations.
  2125. */
  2126. static void cgroup_migrate_add_src(struct css_set *src_cset,
  2127. struct cgroup *dst_cgrp,
  2128. struct list_head *preloaded_csets)
  2129. {
  2130. struct cgroup *src_cgrp;
  2131. lockdep_assert_held(&cgroup_mutex);
  2132. lockdep_assert_held(&css_set_lock);
  2133. src_cgrp = cset_cgroup_from_root(src_cset, dst_cgrp->root);
  2134. if (!list_empty(&src_cset->mg_preload_node))
  2135. return;
  2136. WARN_ON(src_cset->mg_src_cgrp);
  2137. WARN_ON(!list_empty(&src_cset->mg_tasks));
  2138. WARN_ON(!list_empty(&src_cset->mg_node));
  2139. src_cset->mg_src_cgrp = src_cgrp;
  2140. get_css_set(src_cset);
  2141. list_add(&src_cset->mg_preload_node, preloaded_csets);
  2142. }
  2143. /**
  2144. * cgroup_migrate_prepare_dst - prepare destination css_sets for migration
  2145. * @dst_cgrp: the destination cgroup (may be %NULL)
  2146. * @preloaded_csets: list of preloaded source css_sets
  2147. *
  2148. * Tasks are about to be moved to @dst_cgrp and all the source css_sets
  2149. * have been preloaded to @preloaded_csets. This function looks up and
  2150. * pins all destination css_sets, links each to its source, and append them
  2151. * to @preloaded_csets. If @dst_cgrp is %NULL, the destination of each
  2152. * source css_set is assumed to be its cgroup on the default hierarchy.
  2153. *
  2154. * This function must be called after cgroup_migrate_add_src() has been
  2155. * called on each migration source css_set. After migration is performed
  2156. * using cgroup_migrate(), cgroup_migrate_finish() must be called on
  2157. * @preloaded_csets.
  2158. */
  2159. static int cgroup_migrate_prepare_dst(struct cgroup *dst_cgrp,
  2160. struct list_head *preloaded_csets)
  2161. {
  2162. LIST_HEAD(csets);
  2163. struct css_set *src_cset, *tmp_cset;
  2164. lockdep_assert_held(&cgroup_mutex);
  2165. /*
  2166. * Except for the root, child_subsys_mask must be zero for a cgroup
  2167. * with tasks so that child cgroups don't compete against tasks.
  2168. */
  2169. if (dst_cgrp && cgroup_on_dfl(dst_cgrp) && cgroup_parent(dst_cgrp) &&
  2170. dst_cgrp->child_subsys_mask)
  2171. return -EBUSY;
  2172. /* look up the dst cset for each src cset and link it to src */
  2173. list_for_each_entry_safe(src_cset, tmp_cset, preloaded_csets, mg_preload_node) {
  2174. struct css_set *dst_cset;
  2175. dst_cset = find_css_set(src_cset,
  2176. dst_cgrp ?: src_cset->dfl_cgrp);
  2177. if (!dst_cset)
  2178. goto err;
  2179. WARN_ON_ONCE(src_cset->mg_dst_cset || dst_cset->mg_dst_cset);
  2180. /*
  2181. * If src cset equals dst, it's noop. Drop the src.
  2182. * cgroup_migrate() will skip the cset too. Note that we
  2183. * can't handle src == dst as some nodes are used by both.
  2184. */
  2185. if (src_cset == dst_cset) {
  2186. src_cset->mg_src_cgrp = NULL;
  2187. list_del_init(&src_cset->mg_preload_node);
  2188. put_css_set(src_cset);
  2189. put_css_set(dst_cset);
  2190. continue;
  2191. }
  2192. src_cset->mg_dst_cset = dst_cset;
  2193. if (list_empty(&dst_cset->mg_preload_node))
  2194. list_add(&dst_cset->mg_preload_node, &csets);
  2195. else
  2196. put_css_set(dst_cset);
  2197. }
  2198. list_splice_tail(&csets, preloaded_csets);
  2199. return 0;
  2200. err:
  2201. cgroup_migrate_finish(&csets);
  2202. return -ENOMEM;
  2203. }
  2204. /**
  2205. * cgroup_migrate - migrate a process or task to a cgroup
  2206. * @leader: the leader of the process or the task to migrate
  2207. * @threadgroup: whether @leader points to the whole process or a single task
  2208. * @cgrp: the destination cgroup
  2209. *
  2210. * Migrate a process or task denoted by @leader to @cgrp. If migrating a
  2211. * process, the caller must be holding cgroup_threadgroup_rwsem. The
  2212. * caller is also responsible for invoking cgroup_migrate_add_src() and
  2213. * cgroup_migrate_prepare_dst() on the targets before invoking this
  2214. * function and following up with cgroup_migrate_finish().
  2215. *
  2216. * As long as a controller's ->can_attach() doesn't fail, this function is
  2217. * guaranteed to succeed. This means that, excluding ->can_attach()
  2218. * failure, when migrating multiple targets, the success or failure can be
  2219. * decided for all targets by invoking group_migrate_prepare_dst() before
  2220. * actually starting migrating.
  2221. */
  2222. static int cgroup_migrate(struct task_struct *leader, bool threadgroup,
  2223. struct cgroup *cgrp)
  2224. {
  2225. struct cgroup_taskset tset = CGROUP_TASKSET_INIT(tset);
  2226. struct task_struct *task;
  2227. /*
  2228. * Prevent freeing of tasks while we take a snapshot. Tasks that are
  2229. * already PF_EXITING could be freed from underneath us unless we
  2230. * take an rcu_read_lock.
  2231. */
  2232. spin_lock_bh(&css_set_lock);
  2233. rcu_read_lock();
  2234. task = leader;
  2235. do {
  2236. cgroup_taskset_add(task, &tset);
  2237. if (!threadgroup)
  2238. break;
  2239. } while_each_thread(leader, task);
  2240. rcu_read_unlock();
  2241. spin_unlock_bh(&css_set_lock);
  2242. return cgroup_taskset_migrate(&tset, cgrp);
  2243. }
  2244. /**
  2245. * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
  2246. * @dst_cgrp: the cgroup to attach to
  2247. * @leader: the task or the leader of the threadgroup to be attached
  2248. * @threadgroup: attach the whole threadgroup?
  2249. *
  2250. * Call holding cgroup_mutex and cgroup_threadgroup_rwsem.
  2251. */
  2252. static int cgroup_attach_task(struct cgroup *dst_cgrp,
  2253. struct task_struct *leader, bool threadgroup)
  2254. {
  2255. LIST_HEAD(preloaded_csets);
  2256. struct task_struct *task;
  2257. int ret;
  2258. /* look up all src csets */
  2259. spin_lock_bh(&css_set_lock);
  2260. rcu_read_lock();
  2261. task = leader;
  2262. do {
  2263. cgroup_migrate_add_src(task_css_set(task), dst_cgrp,
  2264. &preloaded_csets);
  2265. if (!threadgroup)
  2266. break;
  2267. } while_each_thread(leader, task);
  2268. rcu_read_unlock();
  2269. spin_unlock_bh(&css_set_lock);
  2270. /* prepare dst csets and commit */
  2271. ret = cgroup_migrate_prepare_dst(dst_cgrp, &preloaded_csets);
  2272. if (!ret)
  2273. ret = cgroup_migrate(leader, threadgroup, dst_cgrp);
  2274. cgroup_migrate_finish(&preloaded_csets);
  2275. return ret;
  2276. }
  2277. static int cgroup_procs_write_permission(struct task_struct *task,
  2278. struct cgroup *dst_cgrp,
  2279. struct kernfs_open_file *of)
  2280. {
  2281. const struct cred *cred = current_cred();
  2282. const struct cred *tcred = get_task_cred(task);
  2283. int ret = 0;
  2284. /*
  2285. * even if we're attaching all tasks in the thread group, we only
  2286. * need to check permissions on one of them.
  2287. */
  2288. if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
  2289. !uid_eq(cred->euid, tcred->uid) &&
  2290. !uid_eq(cred->euid, tcred->suid))
  2291. ret = -EACCES;
  2292. if (!ret && cgroup_on_dfl(dst_cgrp)) {
  2293. struct super_block *sb = of->file->f_path.dentry->d_sb;
  2294. struct cgroup *cgrp;
  2295. struct inode *inode;
  2296. spin_lock_bh(&css_set_lock);
  2297. cgrp = task_cgroup_from_root(task, &cgrp_dfl_root);
  2298. spin_unlock_bh(&css_set_lock);
  2299. while (!cgroup_is_descendant(dst_cgrp, cgrp))
  2300. cgrp = cgroup_parent(cgrp);
  2301. ret = -ENOMEM;
  2302. inode = kernfs_get_inode(sb, cgrp->procs_file.kn);
  2303. if (inode) {
  2304. ret = inode_permission(inode, MAY_WRITE);
  2305. iput(inode);
  2306. }
  2307. }
  2308. put_cred(tcred);
  2309. return ret;
  2310. }
  2311. /*
  2312. * Find the task_struct of the task to attach by vpid and pass it along to the
  2313. * function to attach either it or all tasks in its threadgroup. Will lock
  2314. * cgroup_mutex and threadgroup.
  2315. */
  2316. static ssize_t __cgroup_procs_write(struct kernfs_open_file *of, char *buf,
  2317. size_t nbytes, loff_t off, bool threadgroup)
  2318. {
  2319. struct task_struct *tsk;
  2320. struct cgroup *cgrp;
  2321. pid_t pid;
  2322. int ret;
  2323. if (kstrtoint(strstrip(buf), 0, &pid) || pid < 0)
  2324. return -EINVAL;
  2325. cgrp = cgroup_kn_lock_live(of->kn);
  2326. if (!cgrp)
  2327. return -ENODEV;
  2328. percpu_down_write(&cgroup_threadgroup_rwsem);
  2329. rcu_read_lock();
  2330. if (pid) {
  2331. tsk = find_task_by_vpid(pid);
  2332. if (!tsk) {
  2333. ret = -ESRCH;
  2334. goto out_unlock_rcu;
  2335. }
  2336. } else {
  2337. tsk = current;
  2338. }
  2339. if (threadgroup)
  2340. tsk = tsk->group_leader;
  2341. /*
  2342. * Workqueue threads may acquire PF_NO_SETAFFINITY and become
  2343. * trapped in a cpuset, or RT worker may be born in a cgroup
  2344. * with no rt_runtime allocated. Just say no.
  2345. */
  2346. if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
  2347. ret = -EINVAL;
  2348. goto out_unlock_rcu;
  2349. }
  2350. get_task_struct(tsk);
  2351. rcu_read_unlock();
  2352. ret = cgroup_procs_write_permission(tsk, cgrp, of);
  2353. if (!ret)
  2354. ret = cgroup_attach_task(cgrp, tsk, threadgroup);
  2355. put_task_struct(tsk);
  2356. goto out_unlock_threadgroup;
  2357. out_unlock_rcu:
  2358. rcu_read_unlock();
  2359. out_unlock_threadgroup:
  2360. percpu_up_write(&cgroup_threadgroup_rwsem);
  2361. cgroup_kn_unlock(of->kn);
  2362. cpuset_post_attach_flush();
  2363. return ret ?: nbytes;
  2364. }
  2365. /**
  2366. * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
  2367. * @from: attach to all cgroups of a given task
  2368. * @tsk: the task to be attached
  2369. */
  2370. int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
  2371. {
  2372. struct cgroup_root *root;
  2373. int retval = 0;
  2374. mutex_lock(&cgroup_mutex);
  2375. for_each_root(root) {
  2376. struct cgroup *from_cgrp;
  2377. if (root == &cgrp_dfl_root)
  2378. continue;
  2379. spin_lock_bh(&css_set_lock);
  2380. from_cgrp = task_cgroup_from_root(from, root);
  2381. spin_unlock_bh(&css_set_lock);
  2382. retval = cgroup_attach_task(from_cgrp, tsk, false);
  2383. if (retval)
  2384. break;
  2385. }
  2386. mutex_unlock(&cgroup_mutex);
  2387. return retval;
  2388. }
  2389. EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
  2390. static ssize_t cgroup_tasks_write(struct kernfs_open_file *of,
  2391. char *buf, size_t nbytes, loff_t off)
  2392. {
  2393. return __cgroup_procs_write(of, buf, nbytes, off, false);
  2394. }
  2395. static ssize_t cgroup_procs_write(struct kernfs_open_file *of,
  2396. char *buf, size_t nbytes, loff_t off)
  2397. {
  2398. return __cgroup_procs_write(of, buf, nbytes, off, true);
  2399. }
  2400. static ssize_t cgroup_release_agent_write(struct kernfs_open_file *of,
  2401. char *buf, size_t nbytes, loff_t off)
  2402. {
  2403. struct cgroup *cgrp;
  2404. BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
  2405. cgrp = cgroup_kn_lock_live(of->kn);
  2406. if (!cgrp)
  2407. return -ENODEV;
  2408. spin_lock(&release_agent_path_lock);
  2409. strlcpy(cgrp->root->release_agent_path, strstrip(buf),
  2410. sizeof(cgrp->root->release_agent_path));
  2411. spin_unlock(&release_agent_path_lock);
  2412. cgroup_kn_unlock(of->kn);
  2413. return nbytes;
  2414. }
  2415. static int cgroup_release_agent_show(struct seq_file *seq, void *v)
  2416. {
  2417. struct cgroup *cgrp = seq_css(seq)->cgroup;
  2418. spin_lock(&release_agent_path_lock);
  2419. seq_puts(seq, cgrp->root->release_agent_path);
  2420. spin_unlock(&release_agent_path_lock);
  2421. seq_putc(seq, '\n');
  2422. return 0;
  2423. }
  2424. static int cgroup_sane_behavior_show(struct seq_file *seq, void *v)
  2425. {
  2426. seq_puts(seq, "0\n");
  2427. return 0;
  2428. }
  2429. static void cgroup_print_ss_mask(struct seq_file *seq, unsigned long ss_mask)
  2430. {
  2431. struct cgroup_subsys *ss;
  2432. bool printed = false;
  2433. int ssid;
  2434. for_each_subsys_which(ss, ssid, &ss_mask) {
  2435. if (printed)
  2436. seq_putc(seq, ' ');
  2437. seq_printf(seq, "%s", ss->name);
  2438. printed = true;
  2439. }
  2440. if (printed)
  2441. seq_putc(seq, '\n');
  2442. }
  2443. /* show controllers which are currently attached to the default hierarchy */
  2444. static int cgroup_root_controllers_show(struct seq_file *seq, void *v)
  2445. {
  2446. struct cgroup *cgrp = seq_css(seq)->cgroup;
  2447. cgroup_print_ss_mask(seq, cgrp->root->subsys_mask &
  2448. ~cgrp_dfl_root_inhibit_ss_mask);
  2449. return 0;
  2450. }
  2451. /* show controllers which are enabled from the parent */
  2452. static int cgroup_controllers_show(struct seq_file *seq, void *v)
  2453. {
  2454. struct cgroup *cgrp = seq_css(seq)->cgroup;
  2455. cgroup_print_ss_mask(seq, cgroup_parent(cgrp)->subtree_control);
  2456. return 0;
  2457. }
  2458. /* show controllers which are enabled for a given cgroup's children */
  2459. static int cgroup_subtree_control_show(struct seq_file *seq, void *v)
  2460. {
  2461. struct cgroup *cgrp = seq_css(seq)->cgroup;
  2462. cgroup_print_ss_mask(seq, cgrp->subtree_control);
  2463. return 0;
  2464. }
  2465. /**
  2466. * cgroup_update_dfl_csses - update css assoc of a subtree in default hierarchy
  2467. * @cgrp: root of the subtree to update csses for
  2468. *
  2469. * @cgrp's child_subsys_mask has changed and its subtree's (self excluded)
  2470. * css associations need to be updated accordingly. This function looks up
  2471. * all css_sets which are attached to the subtree, creates the matching
  2472. * updated css_sets and migrates the tasks to the new ones.
  2473. */
  2474. static int cgroup_update_dfl_csses(struct cgroup *cgrp)
  2475. {
  2476. LIST_HEAD(preloaded_csets);
  2477. struct cgroup_taskset tset = CGROUP_TASKSET_INIT(tset);
  2478. struct cgroup_subsys_state *css;
  2479. struct css_set *src_cset;
  2480. int ret;
  2481. lockdep_assert_held(&cgroup_mutex);
  2482. percpu_down_write(&cgroup_threadgroup_rwsem);
  2483. /* look up all csses currently attached to @cgrp's subtree */
  2484. spin_lock_bh(&css_set_lock);
  2485. css_for_each_descendant_pre(css, cgroup_css(cgrp, NULL)) {
  2486. struct cgrp_cset_link *link;
  2487. /* self is not affected by child_subsys_mask change */
  2488. if (css->cgroup == cgrp)
  2489. continue;
  2490. list_for_each_entry(link, &css->cgroup->cset_links, cset_link)
  2491. cgroup_migrate_add_src(link->cset, cgrp,
  2492. &preloaded_csets);
  2493. }
  2494. spin_unlock_bh(&css_set_lock);
  2495. /* NULL dst indicates self on default hierarchy */
  2496. ret = cgroup_migrate_prepare_dst(NULL, &preloaded_csets);
  2497. if (ret)
  2498. goto out_finish;
  2499. spin_lock_bh(&css_set_lock);
  2500. list_for_each_entry(src_cset, &preloaded_csets, mg_preload_node) {
  2501. struct task_struct *task, *ntask;
  2502. /* src_csets precede dst_csets, break on the first dst_cset */
  2503. if (!src_cset->mg_src_cgrp)
  2504. break;
  2505. /* all tasks in src_csets need to be migrated */
  2506. list_for_each_entry_safe(task, ntask, &src_cset->tasks, cg_list)
  2507. cgroup_taskset_add(task, &tset);
  2508. }
  2509. spin_unlock_bh(&css_set_lock);
  2510. ret = cgroup_taskset_migrate(&tset, cgrp);
  2511. out_finish:
  2512. cgroup_migrate_finish(&preloaded_csets);
  2513. percpu_up_write(&cgroup_threadgroup_rwsem);
  2514. return ret;
  2515. }
  2516. /* change the enabled child controllers for a cgroup in the default hierarchy */
  2517. static ssize_t cgroup_subtree_control_write(struct kernfs_open_file *of,
  2518. char *buf, size_t nbytes,
  2519. loff_t off)
  2520. {
  2521. unsigned long enable = 0, disable = 0;
  2522. unsigned long css_enable, css_disable, old_sc, new_sc, old_ss, new_ss;
  2523. struct cgroup *cgrp, *child;
  2524. struct cgroup_subsys *ss;
  2525. char *tok;
  2526. int ssid, ret;
  2527. /*
  2528. * Parse input - space separated list of subsystem names prefixed
  2529. * with either + or -.
  2530. */
  2531. buf = strstrip(buf);
  2532. while ((tok = strsep(&buf, " "))) {
  2533. unsigned long tmp_ss_mask = ~cgrp_dfl_root_inhibit_ss_mask;
  2534. if (tok[0] == '\0')
  2535. continue;
  2536. for_each_subsys_which(ss, ssid, &tmp_ss_mask) {
  2537. if (!cgroup_ssid_enabled(ssid) ||
  2538. strcmp(tok + 1, ss->name))
  2539. continue;
  2540. if (*tok == '+') {
  2541. enable |= 1 << ssid;
  2542. disable &= ~(1 << ssid);
  2543. } else if (*tok == '-') {
  2544. disable |= 1 << ssid;
  2545. enable &= ~(1 << ssid);
  2546. } else {
  2547. return -EINVAL;
  2548. }
  2549. break;
  2550. }
  2551. if (ssid == CGROUP_SUBSYS_COUNT)
  2552. return -EINVAL;
  2553. }
  2554. cgrp = cgroup_kn_lock_live(of->kn);
  2555. if (!cgrp)
  2556. return -ENODEV;
  2557. for_each_subsys(ss, ssid) {
  2558. if (enable & (1 << ssid)) {
  2559. if (cgrp->subtree_control & (1 << ssid)) {
  2560. enable &= ~(1 << ssid);
  2561. continue;
  2562. }
  2563. /* unavailable or not enabled on the parent? */
  2564. if (!(cgrp_dfl_root.subsys_mask & (1 << ssid)) ||
  2565. (cgroup_parent(cgrp) &&
  2566. !(cgroup_parent(cgrp)->subtree_control & (1 << ssid)))) {
  2567. ret = -ENOENT;
  2568. goto out_unlock;
  2569. }
  2570. } else if (disable & (1 << ssid)) {
  2571. if (!(cgrp->subtree_control & (1 << ssid))) {
  2572. disable &= ~(1 << ssid);
  2573. continue;
  2574. }
  2575. /* a child has it enabled? */
  2576. cgroup_for_each_live_child(child, cgrp) {
  2577. if (child->subtree_control & (1 << ssid)) {
  2578. ret = -EBUSY;
  2579. goto out_unlock;
  2580. }
  2581. }
  2582. }
  2583. }
  2584. if (!enable && !disable) {
  2585. ret = 0;
  2586. goto out_unlock;
  2587. }
  2588. /*
  2589. * Except for the root, subtree_control must be zero for a cgroup
  2590. * with tasks so that child cgroups don't compete against tasks.
  2591. */
  2592. if (enable && cgroup_parent(cgrp) && !list_empty(&cgrp->cset_links)) {
  2593. ret = -EBUSY;
  2594. goto out_unlock;
  2595. }
  2596. /*
  2597. * Update subsys masks and calculate what needs to be done. More
  2598. * subsystems than specified may need to be enabled or disabled
  2599. * depending on subsystem dependencies.
  2600. */
  2601. old_sc = cgrp->subtree_control;
  2602. old_ss = cgrp->child_subsys_mask;
  2603. new_sc = (old_sc | enable) & ~disable;
  2604. new_ss = cgroup_calc_child_subsys_mask(cgrp, new_sc);
  2605. css_enable = ~old_ss & new_ss;
  2606. css_disable = old_ss & ~new_ss;
  2607. enable |= css_enable;
  2608. disable |= css_disable;
  2609. /*
  2610. * Because css offlining is asynchronous, userland might try to
  2611. * re-enable the same controller while the previous instance is
  2612. * still around. In such cases, wait till it's gone using
  2613. * offline_waitq.
  2614. */
  2615. for_each_subsys_which(ss, ssid, &css_enable) {
  2616. cgroup_for_each_live_child(child, cgrp) {
  2617. DEFINE_WAIT(wait);
  2618. if (!cgroup_css(child, ss))
  2619. continue;
  2620. cgroup_get(child);
  2621. prepare_to_wait(&child->offline_waitq, &wait,
  2622. TASK_UNINTERRUPTIBLE);
  2623. cgroup_kn_unlock(of->kn);
  2624. schedule();
  2625. finish_wait(&child->offline_waitq, &wait);
  2626. cgroup_put(child);
  2627. return restart_syscall();
  2628. }
  2629. }
  2630. cgrp->subtree_control = new_sc;
  2631. cgrp->child_subsys_mask = new_ss;
  2632. /*
  2633. * Create new csses or make the existing ones visible. A css is
  2634. * created invisible if it's being implicitly enabled through
  2635. * dependency. An invisible css is made visible when the userland
  2636. * explicitly enables it.
  2637. */
  2638. for_each_subsys(ss, ssid) {
  2639. if (!(enable & (1 << ssid)))
  2640. continue;
  2641. cgroup_for_each_live_child(child, cgrp) {
  2642. if (css_enable & (1 << ssid))
  2643. ret = create_css(child, ss,
  2644. cgrp->subtree_control & (1 << ssid));
  2645. else
  2646. ret = css_populate_dir(cgroup_css(child, ss),
  2647. NULL);
  2648. if (ret)
  2649. goto err_undo_css;
  2650. }
  2651. }
  2652. /*
  2653. * At this point, cgroup_e_css() results reflect the new csses
  2654. * making the following cgroup_update_dfl_csses() properly update
  2655. * css associations of all tasks in the subtree.
  2656. */
  2657. ret = cgroup_update_dfl_csses(cgrp);
  2658. if (ret)
  2659. goto err_undo_css;
  2660. /*
  2661. * All tasks are migrated out of disabled csses. Kill or hide
  2662. * them. A css is hidden when the userland requests it to be
  2663. * disabled while other subsystems are still depending on it. The
  2664. * css must not actively control resources and be in the vanilla
  2665. * state if it's made visible again later. Controllers which may
  2666. * be depended upon should provide ->css_reset() for this purpose.
  2667. */
  2668. for_each_subsys(ss, ssid) {
  2669. if (!(disable & (1 << ssid)))
  2670. continue;
  2671. cgroup_for_each_live_child(child, cgrp) {
  2672. struct cgroup_subsys_state *css = cgroup_css(child, ss);
  2673. if (css_disable & (1 << ssid)) {
  2674. kill_css(css);
  2675. } else {
  2676. css_clear_dir(css, NULL);
  2677. if (ss->css_reset)
  2678. ss->css_reset(css);
  2679. }
  2680. }
  2681. }
  2682. /*
  2683. * The effective csses of all the descendants (excluding @cgrp) may
  2684. * have changed. Subsystems can optionally subscribe to this event
  2685. * by implementing ->css_e_css_changed() which is invoked if any of
  2686. * the effective csses seen from the css's cgroup may have changed.
  2687. */
  2688. for_each_subsys(ss, ssid) {
  2689. struct cgroup_subsys_state *this_css = cgroup_css(cgrp, ss);
  2690. struct cgroup_subsys_state *css;
  2691. if (!ss->css_e_css_changed || !this_css)
  2692. continue;
  2693. css_for_each_descendant_pre(css, this_css)
  2694. if (css != this_css)
  2695. ss->css_e_css_changed(css);
  2696. }
  2697. kernfs_activate(cgrp->kn);
  2698. ret = 0;
  2699. out_unlock:
  2700. cgroup_kn_unlock(of->kn);
  2701. return ret ?: nbytes;
  2702. err_undo_css:
  2703. cgrp->subtree_control = old_sc;
  2704. cgrp->child_subsys_mask = old_ss;
  2705. for_each_subsys(ss, ssid) {
  2706. if (!(enable & (1 << ssid)))
  2707. continue;
  2708. cgroup_for_each_live_child(child, cgrp) {
  2709. struct cgroup_subsys_state *css = cgroup_css(child, ss);
  2710. if (!css)
  2711. continue;
  2712. if (css_enable & (1 << ssid))
  2713. kill_css(css);
  2714. else
  2715. css_clear_dir(css, NULL);
  2716. }
  2717. }
  2718. goto out_unlock;
  2719. }
  2720. static int cgroup_events_show(struct seq_file *seq, void *v)
  2721. {
  2722. seq_printf(seq, "populated %d\n",
  2723. cgroup_is_populated(seq_css(seq)->cgroup));
  2724. return 0;
  2725. }
  2726. static ssize_t cgroup_file_write(struct kernfs_open_file *of, char *buf,
  2727. size_t nbytes, loff_t off)
  2728. {
  2729. struct cgroup *cgrp = of->kn->parent->priv;
  2730. struct cftype *cft = of->kn->priv;
  2731. struct cgroup_subsys_state *css;
  2732. int ret;
  2733. if (cft->write)
  2734. return cft->write(of, buf, nbytes, off);
  2735. /*
  2736. * kernfs guarantees that a file isn't deleted with operations in
  2737. * flight, which means that the matching css is and stays alive and
  2738. * doesn't need to be pinned. The RCU locking is not necessary
  2739. * either. It's just for the convenience of using cgroup_css().
  2740. */
  2741. rcu_read_lock();
  2742. css = cgroup_css(cgrp, cft->ss);
  2743. rcu_read_unlock();
  2744. if (cft->write_u64) {
  2745. unsigned long long v;
  2746. ret = kstrtoull(buf, 0, &v);
  2747. if (!ret)
  2748. ret = cft->write_u64(css, cft, v);
  2749. } else if (cft->write_s64) {
  2750. long long v;
  2751. ret = kstrtoll(buf, 0, &v);
  2752. if (!ret)
  2753. ret = cft->write_s64(css, cft, v);
  2754. } else {
  2755. ret = -EINVAL;
  2756. }
  2757. return ret ?: nbytes;
  2758. }
  2759. static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos)
  2760. {
  2761. return seq_cft(seq)->seq_start(seq, ppos);
  2762. }
  2763. static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos)
  2764. {
  2765. return seq_cft(seq)->seq_next(seq, v, ppos);
  2766. }
  2767. static void cgroup_seqfile_stop(struct seq_file *seq, void *v)
  2768. {
  2769. seq_cft(seq)->seq_stop(seq, v);
  2770. }
  2771. static int cgroup_seqfile_show(struct seq_file *m, void *arg)
  2772. {
  2773. struct cftype *cft = seq_cft(m);
  2774. struct cgroup_subsys_state *css = seq_css(m);
  2775. if (cft->seq_show)
  2776. return cft->seq_show(m, arg);
  2777. if (cft->read_u64)
  2778. seq_printf(m, "%llu\n", cft->read_u64(css, cft));
  2779. else if (cft->read_s64)
  2780. seq_printf(m, "%lld\n", cft->read_s64(css, cft));
  2781. else
  2782. return -EINVAL;
  2783. return 0;
  2784. }
  2785. static struct kernfs_ops cgroup_kf_single_ops = {
  2786. .atomic_write_len = PAGE_SIZE,
  2787. .write = cgroup_file_write,
  2788. .seq_show = cgroup_seqfile_show,
  2789. };
  2790. static struct kernfs_ops cgroup_kf_ops = {
  2791. .atomic_write_len = PAGE_SIZE,
  2792. .write = cgroup_file_write,
  2793. .seq_start = cgroup_seqfile_start,
  2794. .seq_next = cgroup_seqfile_next,
  2795. .seq_stop = cgroup_seqfile_stop,
  2796. .seq_show = cgroup_seqfile_show,
  2797. };
  2798. /*
  2799. * cgroup_rename - Only allow simple rename of directories in place.
  2800. */
  2801. static int cgroup_rename(struct kernfs_node *kn, struct kernfs_node *new_parent,
  2802. const char *new_name_str)
  2803. {
  2804. struct cgroup *cgrp = kn->priv;
  2805. int ret;
  2806. if (kernfs_type(kn) != KERNFS_DIR)
  2807. return -ENOTDIR;
  2808. if (kn->parent != new_parent)
  2809. return -EIO;
  2810. /*
  2811. * This isn't a proper migration and its usefulness is very
  2812. * limited. Disallow on the default hierarchy.
  2813. */
  2814. if (cgroup_on_dfl(cgrp))
  2815. return -EPERM;
  2816. /*
  2817. * We're gonna grab cgroup_mutex which nests outside kernfs
  2818. * active_ref. kernfs_rename() doesn't require active_ref
  2819. * protection. Break them before grabbing cgroup_mutex.
  2820. */
  2821. kernfs_break_active_protection(new_parent);
  2822. kernfs_break_active_protection(kn);
  2823. mutex_lock(&cgroup_mutex);
  2824. ret = kernfs_rename(kn, new_parent, new_name_str);
  2825. mutex_unlock(&cgroup_mutex);
  2826. kernfs_unbreak_active_protection(kn);
  2827. kernfs_unbreak_active_protection(new_parent);
  2828. return ret;
  2829. }
  2830. /* set uid and gid of cgroup dirs and files to that of the creator */
  2831. static int cgroup_kn_set_ugid(struct kernfs_node *kn)
  2832. {
  2833. struct iattr iattr = { .ia_valid = ATTR_UID | ATTR_GID,
  2834. .ia_uid = current_fsuid(),
  2835. .ia_gid = current_fsgid(), };
  2836. if (uid_eq(iattr.ia_uid, GLOBAL_ROOT_UID) &&
  2837. gid_eq(iattr.ia_gid, GLOBAL_ROOT_GID))
  2838. return 0;
  2839. return kernfs_setattr(kn, &iattr);
  2840. }
  2841. static int cgroup_add_file(struct cgroup_subsys_state *css, struct cgroup *cgrp,
  2842. struct cftype *cft)
  2843. {
  2844. char name[CGROUP_FILE_NAME_MAX];
  2845. struct kernfs_node *kn;
  2846. struct lock_class_key *key = NULL;
  2847. int ret;
  2848. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  2849. key = &cft->lockdep_key;
  2850. #endif
  2851. kn = __kernfs_create_file(cgrp->kn, cgroup_file_name(cgrp, cft, name),
  2852. cgroup_file_mode(cft), 0, cft->kf_ops, cft,
  2853. NULL, key);
  2854. if (IS_ERR(kn))
  2855. return PTR_ERR(kn);
  2856. ret = cgroup_kn_set_ugid(kn);
  2857. if (ret) {
  2858. kernfs_remove(kn);
  2859. return ret;
  2860. }
  2861. if (cft->file_offset) {
  2862. struct cgroup_file *cfile = (void *)css + cft->file_offset;
  2863. spin_lock_irq(&cgroup_file_kn_lock);
  2864. cfile->kn = kn;
  2865. spin_unlock_irq(&cgroup_file_kn_lock);
  2866. }
  2867. return 0;
  2868. }
  2869. /**
  2870. * cgroup_addrm_files - add or remove files to a cgroup directory
  2871. * @css: the target css
  2872. * @cgrp: the target cgroup (usually css->cgroup)
  2873. * @cfts: array of cftypes to be added
  2874. * @is_add: whether to add or remove
  2875. *
  2876. * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
  2877. * For removals, this function never fails.
  2878. */
  2879. static int cgroup_addrm_files(struct cgroup_subsys_state *css,
  2880. struct cgroup *cgrp, struct cftype cfts[],
  2881. bool is_add)
  2882. {
  2883. struct cftype *cft, *cft_end = NULL;
  2884. int ret;
  2885. lockdep_assert_held(&cgroup_mutex);
  2886. restart:
  2887. for (cft = cfts; cft != cft_end && cft->name[0] != '\0'; cft++) {
  2888. /* does cft->flags tell us to skip this file on @cgrp? */
  2889. if ((cft->flags & __CFTYPE_ONLY_ON_DFL) && !cgroup_on_dfl(cgrp))
  2890. continue;
  2891. if ((cft->flags & __CFTYPE_NOT_ON_DFL) && cgroup_on_dfl(cgrp))
  2892. continue;
  2893. if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgroup_parent(cgrp))
  2894. continue;
  2895. if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgroup_parent(cgrp))
  2896. continue;
  2897. if (is_add) {
  2898. ret = cgroup_add_file(css, cgrp, cft);
  2899. if (ret) {
  2900. pr_warn("%s: failed to add %s, err=%d\n",
  2901. __func__, cft->name, ret);
  2902. cft_end = cft;
  2903. is_add = false;
  2904. goto restart;
  2905. }
  2906. } else {
  2907. cgroup_rm_file(cgrp, cft);
  2908. }
  2909. }
  2910. return 0;
  2911. }
  2912. static int cgroup_apply_cftypes(struct cftype *cfts, bool is_add)
  2913. {
  2914. LIST_HEAD(pending);
  2915. struct cgroup_subsys *ss = cfts[0].ss;
  2916. struct cgroup *root = &ss->root->cgrp;
  2917. struct cgroup_subsys_state *css;
  2918. int ret = 0;
  2919. lockdep_assert_held(&cgroup_mutex);
  2920. /* add/rm files for all cgroups created before */
  2921. css_for_each_descendant_pre(css, cgroup_css(root, ss)) {
  2922. struct cgroup *cgrp = css->cgroup;
  2923. if (cgroup_is_dead(cgrp))
  2924. continue;
  2925. ret = cgroup_addrm_files(css, cgrp, cfts, is_add);
  2926. if (ret)
  2927. break;
  2928. }
  2929. if (is_add && !ret)
  2930. kernfs_activate(root->kn);
  2931. return ret;
  2932. }
  2933. static void cgroup_exit_cftypes(struct cftype *cfts)
  2934. {
  2935. struct cftype *cft;
  2936. for (cft = cfts; cft->name[0] != '\0'; cft++) {
  2937. /* free copy for custom atomic_write_len, see init_cftypes() */
  2938. if (cft->max_write_len && cft->max_write_len != PAGE_SIZE)
  2939. kfree(cft->kf_ops);
  2940. cft->kf_ops = NULL;
  2941. cft->ss = NULL;
  2942. /* revert flags set by cgroup core while adding @cfts */
  2943. cft->flags &= ~(__CFTYPE_ONLY_ON_DFL | __CFTYPE_NOT_ON_DFL);
  2944. }
  2945. }
  2946. static int cgroup_init_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
  2947. {
  2948. struct cftype *cft;
  2949. for (cft = cfts; cft->name[0] != '\0'; cft++) {
  2950. struct kernfs_ops *kf_ops;
  2951. WARN_ON(cft->ss || cft->kf_ops);
  2952. if (cft->seq_start)
  2953. kf_ops = &cgroup_kf_ops;
  2954. else
  2955. kf_ops = &cgroup_kf_single_ops;
  2956. /*
  2957. * Ugh... if @cft wants a custom max_write_len, we need to
  2958. * make a copy of kf_ops to set its atomic_write_len.
  2959. */
  2960. if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) {
  2961. kf_ops = kmemdup(kf_ops, sizeof(*kf_ops), GFP_KERNEL);
  2962. if (!kf_ops) {
  2963. cgroup_exit_cftypes(cfts);
  2964. return -ENOMEM;
  2965. }
  2966. kf_ops->atomic_write_len = cft->max_write_len;
  2967. }
  2968. cft->kf_ops = kf_ops;
  2969. cft->ss = ss;
  2970. }
  2971. return 0;
  2972. }
  2973. static int cgroup_rm_cftypes_locked(struct cftype *cfts)
  2974. {
  2975. lockdep_assert_held(&cgroup_mutex);
  2976. if (!cfts || !cfts[0].ss)
  2977. return -ENOENT;
  2978. list_del(&cfts->node);
  2979. cgroup_apply_cftypes(cfts, false);
  2980. cgroup_exit_cftypes(cfts);
  2981. return 0;
  2982. }
  2983. /**
  2984. * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
  2985. * @cfts: zero-length name terminated array of cftypes
  2986. *
  2987. * Unregister @cfts. Files described by @cfts are removed from all
  2988. * existing cgroups and all future cgroups won't have them either. This
  2989. * function can be called anytime whether @cfts' subsys is attached or not.
  2990. *
  2991. * Returns 0 on successful unregistration, -ENOENT if @cfts is not
  2992. * registered.
  2993. */
  2994. int cgroup_rm_cftypes(struct cftype *cfts)
  2995. {
  2996. int ret;
  2997. mutex_lock(&cgroup_mutex);
  2998. ret = cgroup_rm_cftypes_locked(cfts);
  2999. mutex_unlock(&cgroup_mutex);
  3000. return ret;
  3001. }
  3002. /**
  3003. * cgroup_add_cftypes - add an array of cftypes to a subsystem
  3004. * @ss: target cgroup subsystem
  3005. * @cfts: zero-length name terminated array of cftypes
  3006. *
  3007. * Register @cfts to @ss. Files described by @cfts are created for all
  3008. * existing cgroups to which @ss is attached and all future cgroups will
  3009. * have them too. This function can be called anytime whether @ss is
  3010. * attached or not.
  3011. *
  3012. * Returns 0 on successful registration, -errno on failure. Note that this
  3013. * function currently returns 0 as long as @cfts registration is successful
  3014. * even if some file creation attempts on existing cgroups fail.
  3015. */
  3016. static int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
  3017. {
  3018. int ret;
  3019. if (!cgroup_ssid_enabled(ss->id))
  3020. return 0;
  3021. if (!cfts || cfts[0].name[0] == '\0')
  3022. return 0;
  3023. ret = cgroup_init_cftypes(ss, cfts);
  3024. if (ret)
  3025. return ret;
  3026. mutex_lock(&cgroup_mutex);
  3027. list_add_tail(&cfts->node, &ss->cfts);
  3028. ret = cgroup_apply_cftypes(cfts, true);
  3029. if (ret)
  3030. cgroup_rm_cftypes_locked(cfts);
  3031. mutex_unlock(&cgroup_mutex);
  3032. return ret;
  3033. }
  3034. /**
  3035. * cgroup_add_dfl_cftypes - add an array of cftypes for default hierarchy
  3036. * @ss: target cgroup subsystem
  3037. * @cfts: zero-length name terminated array of cftypes
  3038. *
  3039. * Similar to cgroup_add_cftypes() but the added files are only used for
  3040. * the default hierarchy.
  3041. */
  3042. int cgroup_add_dfl_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
  3043. {
  3044. struct cftype *cft;
  3045. for (cft = cfts; cft && cft->name[0] != '\0'; cft++)
  3046. cft->flags |= __CFTYPE_ONLY_ON_DFL;
  3047. return cgroup_add_cftypes(ss, cfts);
  3048. }
  3049. /**
  3050. * cgroup_add_legacy_cftypes - add an array of cftypes for legacy hierarchies
  3051. * @ss: target cgroup subsystem
  3052. * @cfts: zero-length name terminated array of cftypes
  3053. *
  3054. * Similar to cgroup_add_cftypes() but the added files are only used for
  3055. * the legacy hierarchies.
  3056. */
  3057. int cgroup_add_legacy_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
  3058. {
  3059. struct cftype *cft;
  3060. for (cft = cfts; cft && cft->name[0] != '\0'; cft++)
  3061. cft->flags |= __CFTYPE_NOT_ON_DFL;
  3062. return cgroup_add_cftypes(ss, cfts);
  3063. }
  3064. /**
  3065. * cgroup_file_notify - generate a file modified event for a cgroup_file
  3066. * @cfile: target cgroup_file
  3067. *
  3068. * @cfile must have been obtained by setting cftype->file_offset.
  3069. */
  3070. void cgroup_file_notify(struct cgroup_file *cfile)
  3071. {
  3072. unsigned long flags;
  3073. spin_lock_irqsave(&cgroup_file_kn_lock, flags);
  3074. if (cfile->kn)
  3075. kernfs_notify(cfile->kn);
  3076. spin_unlock_irqrestore(&cgroup_file_kn_lock, flags);
  3077. }
  3078. /**
  3079. * cgroup_task_count - count the number of tasks in a cgroup.
  3080. * @cgrp: the cgroup in question
  3081. *
  3082. * Return the number of tasks in the cgroup.
  3083. */
  3084. static int cgroup_task_count(const struct cgroup *cgrp)
  3085. {
  3086. int count = 0;
  3087. struct cgrp_cset_link *link;
  3088. spin_lock_bh(&css_set_lock);
  3089. list_for_each_entry(link, &cgrp->cset_links, cset_link)
  3090. count += atomic_read(&link->cset->refcount);
  3091. spin_unlock_bh(&css_set_lock);
  3092. return count;
  3093. }
  3094. /**
  3095. * css_next_child - find the next child of a given css
  3096. * @pos: the current position (%NULL to initiate traversal)
  3097. * @parent: css whose children to walk
  3098. *
  3099. * This function returns the next child of @parent and should be called
  3100. * under either cgroup_mutex or RCU read lock. The only requirement is
  3101. * that @parent and @pos are accessible. The next sibling is guaranteed to
  3102. * be returned regardless of their states.
  3103. *
  3104. * If a subsystem synchronizes ->css_online() and the start of iteration, a
  3105. * css which finished ->css_online() is guaranteed to be visible in the
  3106. * future iterations and will stay visible until the last reference is put.
  3107. * A css which hasn't finished ->css_online() or already finished
  3108. * ->css_offline() may show up during traversal. It's each subsystem's
  3109. * responsibility to synchronize against on/offlining.
  3110. */
  3111. struct cgroup_subsys_state *css_next_child(struct cgroup_subsys_state *pos,
  3112. struct cgroup_subsys_state *parent)
  3113. {
  3114. struct cgroup_subsys_state *next;
  3115. cgroup_assert_mutex_or_rcu_locked();
  3116. /*
  3117. * @pos could already have been unlinked from the sibling list.
  3118. * Once a cgroup is removed, its ->sibling.next is no longer
  3119. * updated when its next sibling changes. CSS_RELEASED is set when
  3120. * @pos is taken off list, at which time its next pointer is valid,
  3121. * and, as releases are serialized, the one pointed to by the next
  3122. * pointer is guaranteed to not have started release yet. This
  3123. * implies that if we observe !CSS_RELEASED on @pos in this RCU
  3124. * critical section, the one pointed to by its next pointer is
  3125. * guaranteed to not have finished its RCU grace period even if we
  3126. * have dropped rcu_read_lock() inbetween iterations.
  3127. *
  3128. * If @pos has CSS_RELEASED set, its next pointer can't be
  3129. * dereferenced; however, as each css is given a monotonically
  3130. * increasing unique serial number and always appended to the
  3131. * sibling list, the next one can be found by walking the parent's
  3132. * children until the first css with higher serial number than
  3133. * @pos's. While this path can be slower, it happens iff iteration
  3134. * races against release and the race window is very small.
  3135. */
  3136. if (!pos) {
  3137. next = list_entry_rcu(parent->children.next, struct cgroup_subsys_state, sibling);
  3138. } else if (likely(!(pos->flags & CSS_RELEASED))) {
  3139. next = list_entry_rcu(pos->sibling.next, struct cgroup_subsys_state, sibling);
  3140. } else {
  3141. list_for_each_entry_rcu(next, &parent->children, sibling)
  3142. if (next->serial_nr > pos->serial_nr)
  3143. break;
  3144. }
  3145. /*
  3146. * @next, if not pointing to the head, can be dereferenced and is
  3147. * the next sibling.
  3148. */
  3149. if (&next->sibling != &parent->children)
  3150. return next;
  3151. return NULL;
  3152. }
  3153. /**
  3154. * css_next_descendant_pre - find the next descendant for pre-order walk
  3155. * @pos: the current position (%NULL to initiate traversal)
  3156. * @root: css whose descendants to walk
  3157. *
  3158. * To be used by css_for_each_descendant_pre(). Find the next descendant
  3159. * to visit for pre-order traversal of @root's descendants. @root is
  3160. * included in the iteration and the first node to be visited.
  3161. *
  3162. * While this function requires cgroup_mutex or RCU read locking, it
  3163. * doesn't require the whole traversal to be contained in a single critical
  3164. * section. This function will return the correct next descendant as long
  3165. * as both @pos and @root are accessible and @pos is a descendant of @root.
  3166. *
  3167. * If a subsystem synchronizes ->css_online() and the start of iteration, a
  3168. * css which finished ->css_online() is guaranteed to be visible in the
  3169. * future iterations and will stay visible until the last reference is put.
  3170. * A css which hasn't finished ->css_online() or already finished
  3171. * ->css_offline() may show up during traversal. It's each subsystem's
  3172. * responsibility to synchronize against on/offlining.
  3173. */
  3174. struct cgroup_subsys_state *
  3175. css_next_descendant_pre(struct cgroup_subsys_state *pos,
  3176. struct cgroup_subsys_state *root)
  3177. {
  3178. struct cgroup_subsys_state *next;
  3179. cgroup_assert_mutex_or_rcu_locked();
  3180. /* if first iteration, visit @root */
  3181. if (!pos)
  3182. return root;
  3183. /* visit the first child if exists */
  3184. next = css_next_child(NULL, pos);
  3185. if (next)
  3186. return next;
  3187. /* no child, visit my or the closest ancestor's next sibling */
  3188. while (pos != root) {
  3189. next = css_next_child(pos, pos->parent);
  3190. if (next)
  3191. return next;
  3192. pos = pos->parent;
  3193. }
  3194. return NULL;
  3195. }
  3196. /**
  3197. * css_rightmost_descendant - return the rightmost descendant of a css
  3198. * @pos: css of interest
  3199. *
  3200. * Return the rightmost descendant of @pos. If there's no descendant, @pos
  3201. * is returned. This can be used during pre-order traversal to skip
  3202. * subtree of @pos.
  3203. *
  3204. * While this function requires cgroup_mutex or RCU read locking, it
  3205. * doesn't require the whole traversal to be contained in a single critical
  3206. * section. This function will return the correct rightmost descendant as
  3207. * long as @pos is accessible.
  3208. */
  3209. struct cgroup_subsys_state *
  3210. css_rightmost_descendant(struct cgroup_subsys_state *pos)
  3211. {
  3212. struct cgroup_subsys_state *last, *tmp;
  3213. cgroup_assert_mutex_or_rcu_locked();
  3214. do {
  3215. last = pos;
  3216. /* ->prev isn't RCU safe, walk ->next till the end */
  3217. pos = NULL;
  3218. css_for_each_child(tmp, last)
  3219. pos = tmp;
  3220. } while (pos);
  3221. return last;
  3222. }
  3223. static struct cgroup_subsys_state *
  3224. css_leftmost_descendant(struct cgroup_subsys_state *pos)
  3225. {
  3226. struct cgroup_subsys_state *last;
  3227. do {
  3228. last = pos;
  3229. pos = css_next_child(NULL, pos);
  3230. } while (pos);
  3231. return last;
  3232. }
  3233. /**
  3234. * css_next_descendant_post - find the next descendant for post-order walk
  3235. * @pos: the current position (%NULL to initiate traversal)
  3236. * @root: css whose descendants to walk
  3237. *
  3238. * To be used by css_for_each_descendant_post(). Find the next descendant
  3239. * to visit for post-order traversal of @root's descendants. @root is
  3240. * included in the iteration and the last node to be visited.
  3241. *
  3242. * While this function requires cgroup_mutex or RCU read locking, it
  3243. * doesn't require the whole traversal to be contained in a single critical
  3244. * section. This function will return the correct next descendant as long
  3245. * as both @pos and @cgroup are accessible and @pos is a descendant of
  3246. * @cgroup.
  3247. *
  3248. * If a subsystem synchronizes ->css_online() and the start of iteration, a
  3249. * css which finished ->css_online() is guaranteed to be visible in the
  3250. * future iterations and will stay visible until the last reference is put.
  3251. * A css which hasn't finished ->css_online() or already finished
  3252. * ->css_offline() may show up during traversal. It's each subsystem's
  3253. * responsibility to synchronize against on/offlining.
  3254. */
  3255. struct cgroup_subsys_state *
  3256. css_next_descendant_post(struct cgroup_subsys_state *pos,
  3257. struct cgroup_subsys_state *root)
  3258. {
  3259. struct cgroup_subsys_state *next;
  3260. cgroup_assert_mutex_or_rcu_locked();
  3261. /* if first iteration, visit leftmost descendant which may be @root */
  3262. if (!pos)
  3263. return css_leftmost_descendant(root);
  3264. /* if we visited @root, we're done */
  3265. if (pos == root)
  3266. return NULL;
  3267. /* if there's an unvisited sibling, visit its leftmost descendant */
  3268. next = css_next_child(pos, pos->parent);
  3269. if (next)
  3270. return css_leftmost_descendant(next);
  3271. /* no sibling left, visit parent */
  3272. return pos->parent;
  3273. }
  3274. /**
  3275. * css_has_online_children - does a css have online children
  3276. * @css: the target css
  3277. *
  3278. * Returns %true if @css has any online children; otherwise, %false. This
  3279. * function can be called from any context but the caller is responsible
  3280. * for synchronizing against on/offlining as necessary.
  3281. */
  3282. bool css_has_online_children(struct cgroup_subsys_state *css)
  3283. {
  3284. struct cgroup_subsys_state *child;
  3285. bool ret = false;
  3286. rcu_read_lock();
  3287. css_for_each_child(child, css) {
  3288. if (child->flags & CSS_ONLINE) {
  3289. ret = true;
  3290. break;
  3291. }
  3292. }
  3293. rcu_read_unlock();
  3294. return ret;
  3295. }
  3296. /**
  3297. * css_task_iter_advance_css_set - advance a task itererator to the next css_set
  3298. * @it: the iterator to advance
  3299. *
  3300. * Advance @it to the next css_set to walk.
  3301. */
  3302. static void css_task_iter_advance_css_set(struct css_task_iter *it)
  3303. {
  3304. struct list_head *l = it->cset_pos;
  3305. struct cgrp_cset_link *link;
  3306. struct css_set *cset;
  3307. lockdep_assert_held(&css_set_lock);
  3308. /* Advance to the next non-empty css_set */
  3309. do {
  3310. l = l->next;
  3311. if (l == it->cset_head) {
  3312. it->cset_pos = NULL;
  3313. it->task_pos = NULL;
  3314. return;
  3315. }
  3316. if (it->ss) {
  3317. cset = container_of(l, struct css_set,
  3318. e_cset_node[it->ss->id]);
  3319. } else {
  3320. link = list_entry(l, struct cgrp_cset_link, cset_link);
  3321. cset = link->cset;
  3322. }
  3323. } while (!css_set_populated(cset));
  3324. it->cset_pos = l;
  3325. if (!list_empty(&cset->tasks))
  3326. it->task_pos = cset->tasks.next;
  3327. else
  3328. it->task_pos = cset->mg_tasks.next;
  3329. it->tasks_head = &cset->tasks;
  3330. it->mg_tasks_head = &cset->mg_tasks;
  3331. /*
  3332. * We don't keep css_sets locked across iteration steps and thus
  3333. * need to take steps to ensure that iteration can be resumed after
  3334. * the lock is re-acquired. Iteration is performed at two levels -
  3335. * css_sets and tasks in them.
  3336. *
  3337. * Once created, a css_set never leaves its cgroup lists, so a
  3338. * pinned css_set is guaranteed to stay put and we can resume
  3339. * iteration afterwards.
  3340. *
  3341. * Tasks may leave @cset across iteration steps. This is resolved
  3342. * by registering each iterator with the css_set currently being
  3343. * walked and making css_set_move_task() advance iterators whose
  3344. * next task is leaving.
  3345. */
  3346. if (it->cur_cset) {
  3347. list_del(&it->iters_node);
  3348. put_css_set_locked(it->cur_cset);
  3349. }
  3350. get_css_set(cset);
  3351. it->cur_cset = cset;
  3352. list_add(&it->iters_node, &cset->task_iters);
  3353. }
  3354. static void css_task_iter_advance(struct css_task_iter *it)
  3355. {
  3356. struct list_head *l = it->task_pos;
  3357. lockdep_assert_held(&css_set_lock);
  3358. WARN_ON_ONCE(!l);
  3359. /*
  3360. * Advance iterator to find next entry. cset->tasks is consumed
  3361. * first and then ->mg_tasks. After ->mg_tasks, we move onto the
  3362. * next cset.
  3363. */
  3364. l = l->next;
  3365. if (l == it->tasks_head)
  3366. l = it->mg_tasks_head->next;
  3367. if (l == it->mg_tasks_head)
  3368. css_task_iter_advance_css_set(it);
  3369. else
  3370. it->task_pos = l;
  3371. }
  3372. /**
  3373. * css_task_iter_start - initiate task iteration
  3374. * @css: the css to walk tasks of
  3375. * @it: the task iterator to use
  3376. *
  3377. * Initiate iteration through the tasks of @css. The caller can call
  3378. * css_task_iter_next() to walk through the tasks until the function
  3379. * returns NULL. On completion of iteration, css_task_iter_end() must be
  3380. * called.
  3381. */
  3382. void css_task_iter_start(struct cgroup_subsys_state *css,
  3383. struct css_task_iter *it)
  3384. {
  3385. /* no one should try to iterate before mounting cgroups */
  3386. WARN_ON_ONCE(!use_task_css_set_links);
  3387. memset(it, 0, sizeof(*it));
  3388. spin_lock_bh(&css_set_lock);
  3389. it->ss = css->ss;
  3390. if (it->ss)
  3391. it->cset_pos = &css->cgroup->e_csets[css->ss->id];
  3392. else
  3393. it->cset_pos = &css->cgroup->cset_links;
  3394. it->cset_head = it->cset_pos;
  3395. css_task_iter_advance_css_set(it);
  3396. spin_unlock_bh(&css_set_lock);
  3397. }
  3398. /**
  3399. * css_task_iter_next - return the next task for the iterator
  3400. * @it: the task iterator being iterated
  3401. *
  3402. * The "next" function for task iteration. @it should have been
  3403. * initialized via css_task_iter_start(). Returns NULL when the iteration
  3404. * reaches the end.
  3405. */
  3406. struct task_struct *css_task_iter_next(struct css_task_iter *it)
  3407. {
  3408. if (it->cur_task) {
  3409. put_task_struct(it->cur_task);
  3410. it->cur_task = NULL;
  3411. }
  3412. spin_lock_bh(&css_set_lock);
  3413. if (it->task_pos) {
  3414. it->cur_task = list_entry(it->task_pos, struct task_struct,
  3415. cg_list);
  3416. get_task_struct(it->cur_task);
  3417. css_task_iter_advance(it);
  3418. }
  3419. spin_unlock_bh(&css_set_lock);
  3420. return it->cur_task;
  3421. }
  3422. /**
  3423. * css_task_iter_end - finish task iteration
  3424. * @it: the task iterator to finish
  3425. *
  3426. * Finish task iteration started by css_task_iter_start().
  3427. */
  3428. void css_task_iter_end(struct css_task_iter *it)
  3429. {
  3430. if (it->cur_cset) {
  3431. spin_lock_bh(&css_set_lock);
  3432. list_del(&it->iters_node);
  3433. put_css_set_locked(it->cur_cset);
  3434. spin_unlock_bh(&css_set_lock);
  3435. }
  3436. if (it->cur_task)
  3437. put_task_struct(it->cur_task);
  3438. }
  3439. /**
  3440. * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
  3441. * @to: cgroup to which the tasks will be moved
  3442. * @from: cgroup in which the tasks currently reside
  3443. *
  3444. * Locking rules between cgroup_post_fork() and the migration path
  3445. * guarantee that, if a task is forking while being migrated, the new child
  3446. * is guaranteed to be either visible in the source cgroup after the
  3447. * parent's migration is complete or put into the target cgroup. No task
  3448. * can slip out of migration through forking.
  3449. */
  3450. int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
  3451. {
  3452. LIST_HEAD(preloaded_csets);
  3453. struct cgrp_cset_link *link;
  3454. struct css_task_iter it;
  3455. struct task_struct *task;
  3456. int ret;
  3457. mutex_lock(&cgroup_mutex);
  3458. /* all tasks in @from are being moved, all csets are source */
  3459. spin_lock_bh(&css_set_lock);
  3460. list_for_each_entry(link, &from->cset_links, cset_link)
  3461. cgroup_migrate_add_src(link->cset, to, &preloaded_csets);
  3462. spin_unlock_bh(&css_set_lock);
  3463. ret = cgroup_migrate_prepare_dst(to, &preloaded_csets);
  3464. if (ret)
  3465. goto out_err;
  3466. /*
  3467. * Migrate tasks one-by-one until @from is empty. This fails iff
  3468. * ->can_attach() fails.
  3469. */
  3470. do {
  3471. css_task_iter_start(&from->self, &it);
  3472. task = css_task_iter_next(&it);
  3473. if (task)
  3474. get_task_struct(task);
  3475. css_task_iter_end(&it);
  3476. if (task) {
  3477. ret = cgroup_migrate(task, false, to);
  3478. put_task_struct(task);
  3479. }
  3480. } while (task && !ret);
  3481. out_err:
  3482. cgroup_migrate_finish(&preloaded_csets);
  3483. mutex_unlock(&cgroup_mutex);
  3484. return ret;
  3485. }
  3486. /*
  3487. * Stuff for reading the 'tasks'/'procs' files.
  3488. *
  3489. * Reading this file can return large amounts of data if a cgroup has
  3490. * *lots* of attached tasks. So it may need several calls to read(),
  3491. * but we cannot guarantee that the information we produce is correct
  3492. * unless we produce it entirely atomically.
  3493. *
  3494. */
  3495. /* which pidlist file are we talking about? */
  3496. enum cgroup_filetype {
  3497. CGROUP_FILE_PROCS,
  3498. CGROUP_FILE_TASKS,
  3499. };
  3500. /*
  3501. * A pidlist is a list of pids that virtually represents the contents of one
  3502. * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
  3503. * a pair (one each for procs, tasks) for each pid namespace that's relevant
  3504. * to the cgroup.
  3505. */
  3506. struct cgroup_pidlist {
  3507. /*
  3508. * used to find which pidlist is wanted. doesn't change as long as
  3509. * this particular list stays in the list.
  3510. */
  3511. struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
  3512. /* array of xids */
  3513. pid_t *list;
  3514. /* how many elements the above list has */
  3515. int length;
  3516. /* each of these stored in a list by its cgroup */
  3517. struct list_head links;
  3518. /* pointer to the cgroup we belong to, for list removal purposes */
  3519. struct cgroup *owner;
  3520. /* for delayed destruction */
  3521. struct delayed_work destroy_dwork;
  3522. };
  3523. /*
  3524. * The following two functions "fix" the issue where there are more pids
  3525. * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
  3526. * TODO: replace with a kernel-wide solution to this problem
  3527. */
  3528. #define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
  3529. static void *pidlist_allocate(int count)
  3530. {
  3531. if (PIDLIST_TOO_LARGE(count))
  3532. return vmalloc(count * sizeof(pid_t));
  3533. else
  3534. return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
  3535. }
  3536. static void pidlist_free(void *p)
  3537. {
  3538. kvfree(p);
  3539. }
  3540. /*
  3541. * Used to destroy all pidlists lingering waiting for destroy timer. None
  3542. * should be left afterwards.
  3543. */
  3544. static void cgroup_pidlist_destroy_all(struct cgroup *cgrp)
  3545. {
  3546. struct cgroup_pidlist *l, *tmp_l;
  3547. mutex_lock(&cgrp->pidlist_mutex);
  3548. list_for_each_entry_safe(l, tmp_l, &cgrp->pidlists, links)
  3549. mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, 0);
  3550. mutex_unlock(&cgrp->pidlist_mutex);
  3551. flush_workqueue(cgroup_pidlist_destroy_wq);
  3552. BUG_ON(!list_empty(&cgrp->pidlists));
  3553. }
  3554. static void cgroup_pidlist_destroy_work_fn(struct work_struct *work)
  3555. {
  3556. struct delayed_work *dwork = to_delayed_work(work);
  3557. struct cgroup_pidlist *l = container_of(dwork, struct cgroup_pidlist,
  3558. destroy_dwork);
  3559. struct cgroup_pidlist *tofree = NULL;
  3560. mutex_lock(&l->owner->pidlist_mutex);
  3561. /*
  3562. * Destroy iff we didn't get queued again. The state won't change
  3563. * as destroy_dwork can only be queued while locked.
  3564. */
  3565. if (!delayed_work_pending(dwork)) {
  3566. list_del(&l->links);
  3567. pidlist_free(l->list);
  3568. put_pid_ns(l->key.ns);
  3569. tofree = l;
  3570. }
  3571. mutex_unlock(&l->owner->pidlist_mutex);
  3572. kfree(tofree);
  3573. }
  3574. /*
  3575. * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
  3576. * Returns the number of unique elements.
  3577. */
  3578. static int pidlist_uniq(pid_t *list, int length)
  3579. {
  3580. int src, dest = 1;
  3581. /*
  3582. * we presume the 0th element is unique, so i starts at 1. trivial
  3583. * edge cases first; no work needs to be done for either
  3584. */
  3585. if (length == 0 || length == 1)
  3586. return length;
  3587. /* src and dest walk down the list; dest counts unique elements */
  3588. for (src = 1; src < length; src++) {
  3589. /* find next unique element */
  3590. while (list[src] == list[src-1]) {
  3591. src++;
  3592. if (src == length)
  3593. goto after;
  3594. }
  3595. /* dest always points to where the next unique element goes */
  3596. list[dest] = list[src];
  3597. dest++;
  3598. }
  3599. after:
  3600. return dest;
  3601. }
  3602. /*
  3603. * The two pid files - task and cgroup.procs - guaranteed that the result
  3604. * is sorted, which forced this whole pidlist fiasco. As pid order is
  3605. * different per namespace, each namespace needs differently sorted list,
  3606. * making it impossible to use, for example, single rbtree of member tasks
  3607. * sorted by task pointer. As pidlists can be fairly large, allocating one
  3608. * per open file is dangerous, so cgroup had to implement shared pool of
  3609. * pidlists keyed by cgroup and namespace.
  3610. *
  3611. * All this extra complexity was caused by the original implementation
  3612. * committing to an entirely unnecessary property. In the long term, we
  3613. * want to do away with it. Explicitly scramble sort order if on the
  3614. * default hierarchy so that no such expectation exists in the new
  3615. * interface.
  3616. *
  3617. * Scrambling is done by swapping every two consecutive bits, which is
  3618. * non-identity one-to-one mapping which disturbs sort order sufficiently.
  3619. */
  3620. static pid_t pid_fry(pid_t pid)
  3621. {
  3622. unsigned a = pid & 0x55555555;
  3623. unsigned b = pid & 0xAAAAAAAA;
  3624. return (a << 1) | (b >> 1);
  3625. }
  3626. static pid_t cgroup_pid_fry(struct cgroup *cgrp, pid_t pid)
  3627. {
  3628. if (cgroup_on_dfl(cgrp))
  3629. return pid_fry(pid);
  3630. else
  3631. return pid;
  3632. }
  3633. static int cmppid(const void *a, const void *b)
  3634. {
  3635. return *(pid_t *)a - *(pid_t *)b;
  3636. }
  3637. static int fried_cmppid(const void *a, const void *b)
  3638. {
  3639. return pid_fry(*(pid_t *)a) - pid_fry(*(pid_t *)b);
  3640. }
  3641. static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
  3642. enum cgroup_filetype type)
  3643. {
  3644. struct cgroup_pidlist *l;
  3645. /* don't need task_nsproxy() if we're looking at ourself */
  3646. struct pid_namespace *ns = task_active_pid_ns(current);
  3647. lockdep_assert_held(&cgrp->pidlist_mutex);
  3648. list_for_each_entry(l, &cgrp->pidlists, links)
  3649. if (l->key.type == type && l->key.ns == ns)
  3650. return l;
  3651. return NULL;
  3652. }
  3653. /*
  3654. * find the appropriate pidlist for our purpose (given procs vs tasks)
  3655. * returns with the lock on that pidlist already held, and takes care
  3656. * of the use count, or returns NULL with no locks held if we're out of
  3657. * memory.
  3658. */
  3659. static struct cgroup_pidlist *cgroup_pidlist_find_create(struct cgroup *cgrp,
  3660. enum cgroup_filetype type)
  3661. {
  3662. struct cgroup_pidlist *l;
  3663. lockdep_assert_held(&cgrp->pidlist_mutex);
  3664. l = cgroup_pidlist_find(cgrp, type);
  3665. if (l)
  3666. return l;
  3667. /* entry not found; create a new one */
  3668. l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
  3669. if (!l)
  3670. return l;
  3671. INIT_DELAYED_WORK(&l->destroy_dwork, cgroup_pidlist_destroy_work_fn);
  3672. l->key.type = type;
  3673. /* don't need task_nsproxy() if we're looking at ourself */
  3674. l->key.ns = get_pid_ns(task_active_pid_ns(current));
  3675. l->owner = cgrp;
  3676. list_add(&l->links, &cgrp->pidlists);
  3677. return l;
  3678. }
  3679. /*
  3680. * Load a cgroup's pidarray with either procs' tgids or tasks' pids
  3681. */
  3682. static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
  3683. struct cgroup_pidlist **lp)
  3684. {
  3685. pid_t *array;
  3686. int length;
  3687. int pid, n = 0; /* used for populating the array */
  3688. struct css_task_iter it;
  3689. struct task_struct *tsk;
  3690. struct cgroup_pidlist *l;
  3691. lockdep_assert_held(&cgrp->pidlist_mutex);
  3692. /*
  3693. * If cgroup gets more users after we read count, we won't have
  3694. * enough space - tough. This race is indistinguishable to the
  3695. * caller from the case that the additional cgroup users didn't
  3696. * show up until sometime later on.
  3697. */
  3698. length = cgroup_task_count(cgrp);
  3699. array = pidlist_allocate(length);
  3700. if (!array)
  3701. return -ENOMEM;
  3702. /* now, populate the array */
  3703. css_task_iter_start(&cgrp->self, &it);
  3704. while ((tsk = css_task_iter_next(&it))) {
  3705. if (unlikely(n == length))
  3706. break;
  3707. /* get tgid or pid for procs or tasks file respectively */
  3708. if (type == CGROUP_FILE_PROCS)
  3709. pid = task_tgid_vnr(tsk);
  3710. else
  3711. pid = task_pid_vnr(tsk);
  3712. if (pid > 0) /* make sure to only use valid results */
  3713. array[n++] = pid;
  3714. }
  3715. css_task_iter_end(&it);
  3716. length = n;
  3717. /* now sort & (if procs) strip out duplicates */
  3718. if (cgroup_on_dfl(cgrp))
  3719. sort(array, length, sizeof(pid_t), fried_cmppid, NULL);
  3720. else
  3721. sort(array, length, sizeof(pid_t), cmppid, NULL);
  3722. if (type == CGROUP_FILE_PROCS)
  3723. length = pidlist_uniq(array, length);
  3724. l = cgroup_pidlist_find_create(cgrp, type);
  3725. if (!l) {
  3726. pidlist_free(array);
  3727. return -ENOMEM;
  3728. }
  3729. /* store array, freeing old if necessary */
  3730. pidlist_free(l->list);
  3731. l->list = array;
  3732. l->length = length;
  3733. *lp = l;
  3734. return 0;
  3735. }
  3736. /**
  3737. * cgroupstats_build - build and fill cgroupstats
  3738. * @stats: cgroupstats to fill information into
  3739. * @dentry: A dentry entry belonging to the cgroup for which stats have
  3740. * been requested.
  3741. *
  3742. * Build and fill cgroupstats so that taskstats can export it to user
  3743. * space.
  3744. */
  3745. int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
  3746. {
  3747. struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
  3748. struct cgroup *cgrp;
  3749. struct css_task_iter it;
  3750. struct task_struct *tsk;
  3751. /* it should be kernfs_node belonging to cgroupfs and is a directory */
  3752. if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
  3753. kernfs_type(kn) != KERNFS_DIR)
  3754. return -EINVAL;
  3755. mutex_lock(&cgroup_mutex);
  3756. /*
  3757. * We aren't being called from kernfs and there's no guarantee on
  3758. * @kn->priv's validity. For this and css_tryget_online_from_dir(),
  3759. * @kn->priv is RCU safe. Let's do the RCU dancing.
  3760. */
  3761. rcu_read_lock();
  3762. cgrp = rcu_dereference(kn->priv);
  3763. if (!cgrp || cgroup_is_dead(cgrp)) {
  3764. rcu_read_unlock();
  3765. mutex_unlock(&cgroup_mutex);
  3766. return -ENOENT;
  3767. }
  3768. rcu_read_unlock();
  3769. css_task_iter_start(&cgrp->self, &it);
  3770. while ((tsk = css_task_iter_next(&it))) {
  3771. switch (tsk->state) {
  3772. case TASK_RUNNING:
  3773. stats->nr_running++;
  3774. break;
  3775. case TASK_INTERRUPTIBLE:
  3776. stats->nr_sleeping++;
  3777. break;
  3778. case TASK_UNINTERRUPTIBLE:
  3779. stats->nr_uninterruptible++;
  3780. break;
  3781. case TASK_STOPPED:
  3782. stats->nr_stopped++;
  3783. break;
  3784. default:
  3785. if (delayacct_is_task_waiting_on_io(tsk))
  3786. stats->nr_io_wait++;
  3787. break;
  3788. }
  3789. }
  3790. css_task_iter_end(&it);
  3791. mutex_unlock(&cgroup_mutex);
  3792. return 0;
  3793. }
  3794. /*
  3795. * seq_file methods for the tasks/procs files. The seq_file position is the
  3796. * next pid to display; the seq_file iterator is a pointer to the pid
  3797. * in the cgroup->l->list array.
  3798. */
  3799. static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
  3800. {
  3801. /*
  3802. * Initially we receive a position value that corresponds to
  3803. * one more than the last pid shown (or 0 on the first call or
  3804. * after a seek to the start). Use a binary-search to find the
  3805. * next pid to display, if any
  3806. */
  3807. struct kernfs_open_file *of = s->private;
  3808. struct cgroup *cgrp = seq_css(s)->cgroup;
  3809. struct cgroup_pidlist *l;
  3810. enum cgroup_filetype type = seq_cft(s)->private;
  3811. int index = 0, pid = *pos;
  3812. int *iter, ret;
  3813. mutex_lock(&cgrp->pidlist_mutex);
  3814. /*
  3815. * !NULL @of->priv indicates that this isn't the first start()
  3816. * after open. If the matching pidlist is around, we can use that.
  3817. * Look for it. Note that @of->priv can't be used directly. It
  3818. * could already have been destroyed.
  3819. */
  3820. if (of->priv)
  3821. of->priv = cgroup_pidlist_find(cgrp, type);
  3822. /*
  3823. * Either this is the first start() after open or the matching
  3824. * pidlist has been destroyed inbetween. Create a new one.
  3825. */
  3826. if (!of->priv) {
  3827. ret = pidlist_array_load(cgrp, type,
  3828. (struct cgroup_pidlist **)&of->priv);
  3829. if (ret)
  3830. return ERR_PTR(ret);
  3831. }
  3832. l = of->priv;
  3833. if (pid) {
  3834. int end = l->length;
  3835. while (index < end) {
  3836. int mid = (index + end) / 2;
  3837. if (cgroup_pid_fry(cgrp, l->list[mid]) == pid) {
  3838. index = mid;
  3839. break;
  3840. } else if (cgroup_pid_fry(cgrp, l->list[mid]) <= pid)
  3841. index = mid + 1;
  3842. else
  3843. end = mid;
  3844. }
  3845. }
  3846. /* If we're off the end of the array, we're done */
  3847. if (index >= l->length)
  3848. return NULL;
  3849. /* Update the abstract position to be the actual pid that we found */
  3850. iter = l->list + index;
  3851. *pos = cgroup_pid_fry(cgrp, *iter);
  3852. return iter;
  3853. }
  3854. static void cgroup_pidlist_stop(struct seq_file *s, void *v)
  3855. {
  3856. struct kernfs_open_file *of = s->private;
  3857. struct cgroup_pidlist *l = of->priv;
  3858. if (l)
  3859. mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork,
  3860. CGROUP_PIDLIST_DESTROY_DELAY);
  3861. mutex_unlock(&seq_css(s)->cgroup->pidlist_mutex);
  3862. }
  3863. static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
  3864. {
  3865. struct kernfs_open_file *of = s->private;
  3866. struct cgroup_pidlist *l = of->priv;
  3867. pid_t *p = v;
  3868. pid_t *end = l->list + l->length;
  3869. /*
  3870. * Advance to the next pid in the array. If this goes off the
  3871. * end, we're done
  3872. */
  3873. p++;
  3874. if (p >= end) {
  3875. return NULL;
  3876. } else {
  3877. *pos = cgroup_pid_fry(seq_css(s)->cgroup, *p);
  3878. return p;
  3879. }
  3880. }
  3881. static int cgroup_pidlist_show(struct seq_file *s, void *v)
  3882. {
  3883. seq_printf(s, "%d\n", *(int *)v);
  3884. return 0;
  3885. }
  3886. static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
  3887. struct cftype *cft)
  3888. {
  3889. return notify_on_release(css->cgroup);
  3890. }
  3891. static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
  3892. struct cftype *cft, u64 val)
  3893. {
  3894. if (val)
  3895. set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
  3896. else
  3897. clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
  3898. return 0;
  3899. }
  3900. static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
  3901. struct cftype *cft)
  3902. {
  3903. return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
  3904. }
  3905. static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
  3906. struct cftype *cft, u64 val)
  3907. {
  3908. if (val)
  3909. set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
  3910. else
  3911. clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
  3912. return 0;
  3913. }
  3914. /* cgroup core interface files for the default hierarchy */
  3915. static struct cftype cgroup_dfl_base_files[] = {
  3916. {
  3917. .name = "cgroup.procs",
  3918. .file_offset = offsetof(struct cgroup, procs_file),
  3919. .seq_start = cgroup_pidlist_start,
  3920. .seq_next = cgroup_pidlist_next,
  3921. .seq_stop = cgroup_pidlist_stop,
  3922. .seq_show = cgroup_pidlist_show,
  3923. .private = CGROUP_FILE_PROCS,
  3924. .write = cgroup_procs_write,
  3925. },
  3926. {
  3927. .name = "cgroup.controllers",
  3928. .flags = CFTYPE_ONLY_ON_ROOT,
  3929. .seq_show = cgroup_root_controllers_show,
  3930. },
  3931. {
  3932. .name = "cgroup.controllers",
  3933. .flags = CFTYPE_NOT_ON_ROOT,
  3934. .seq_show = cgroup_controllers_show,
  3935. },
  3936. {
  3937. .name = "cgroup.subtree_control",
  3938. .seq_show = cgroup_subtree_control_show,
  3939. .write = cgroup_subtree_control_write,
  3940. },
  3941. {
  3942. .name = "cgroup.events",
  3943. .flags = CFTYPE_NOT_ON_ROOT,
  3944. .file_offset = offsetof(struct cgroup, events_file),
  3945. .seq_show = cgroup_events_show,
  3946. },
  3947. { } /* terminate */
  3948. };
  3949. /* cgroup core interface files for the legacy hierarchies */
  3950. static struct cftype cgroup_legacy_base_files[] = {
  3951. {
  3952. .name = "cgroup.procs",
  3953. .seq_start = cgroup_pidlist_start,
  3954. .seq_next = cgroup_pidlist_next,
  3955. .seq_stop = cgroup_pidlist_stop,
  3956. .seq_show = cgroup_pidlist_show,
  3957. .private = CGROUP_FILE_PROCS,
  3958. .write = cgroup_procs_write,
  3959. },
  3960. {
  3961. .name = "cgroup.clone_children",
  3962. .read_u64 = cgroup_clone_children_read,
  3963. .write_u64 = cgroup_clone_children_write,
  3964. },
  3965. {
  3966. .name = "cgroup.sane_behavior",
  3967. .flags = CFTYPE_ONLY_ON_ROOT,
  3968. .seq_show = cgroup_sane_behavior_show,
  3969. },
  3970. {
  3971. .name = "tasks",
  3972. .seq_start = cgroup_pidlist_start,
  3973. .seq_next = cgroup_pidlist_next,
  3974. .seq_stop = cgroup_pidlist_stop,
  3975. .seq_show = cgroup_pidlist_show,
  3976. .private = CGROUP_FILE_TASKS,
  3977. .write = cgroup_tasks_write,
  3978. },
  3979. {
  3980. .name = "notify_on_release",
  3981. .read_u64 = cgroup_read_notify_on_release,
  3982. .write_u64 = cgroup_write_notify_on_release,
  3983. },
  3984. {
  3985. .name = "release_agent",
  3986. .flags = CFTYPE_ONLY_ON_ROOT,
  3987. .seq_show = cgroup_release_agent_show,
  3988. .write = cgroup_release_agent_write,
  3989. .max_write_len = PATH_MAX - 1,
  3990. },
  3991. { } /* terminate */
  3992. };
  3993. /*
  3994. * css destruction is four-stage process.
  3995. *
  3996. * 1. Destruction starts. Killing of the percpu_ref is initiated.
  3997. * Implemented in kill_css().
  3998. *
  3999. * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
  4000. * and thus css_tryget_online() is guaranteed to fail, the css can be
  4001. * offlined by invoking offline_css(). After offlining, the base ref is
  4002. * put. Implemented in css_killed_work_fn().
  4003. *
  4004. * 3. When the percpu_ref reaches zero, the only possible remaining
  4005. * accessors are inside RCU read sections. css_release() schedules the
  4006. * RCU callback.
  4007. *
  4008. * 4. After the grace period, the css can be freed. Implemented in
  4009. * css_free_work_fn().
  4010. *
  4011. * It is actually hairier because both step 2 and 4 require process context
  4012. * and thus involve punting to css->destroy_work adding two additional
  4013. * steps to the already complex sequence.
  4014. */
  4015. static void css_free_work_fn(struct work_struct *work)
  4016. {
  4017. struct cgroup_subsys_state *css =
  4018. container_of(work, struct cgroup_subsys_state, destroy_work);
  4019. struct cgroup_subsys *ss = css->ss;
  4020. struct cgroup *cgrp = css->cgroup;
  4021. percpu_ref_exit(&css->refcnt);
  4022. if (ss) {
  4023. /* css free path */
  4024. struct cgroup_subsys_state *parent = css->parent;
  4025. int id = css->id;
  4026. ss->css_free(css);
  4027. cgroup_idr_remove(&ss->css_idr, id);
  4028. cgroup_put(cgrp);
  4029. if (parent)
  4030. css_put(parent);
  4031. } else {
  4032. /* cgroup free path */
  4033. atomic_dec(&cgrp->root->nr_cgrps);
  4034. cgroup_pidlist_destroy_all(cgrp);
  4035. cancel_work_sync(&cgrp->release_agent_work);
  4036. if (cgroup_parent(cgrp)) {
  4037. /*
  4038. * We get a ref to the parent, and put the ref when
  4039. * this cgroup is being freed, so it's guaranteed
  4040. * that the parent won't be destroyed before its
  4041. * children.
  4042. */
  4043. cgroup_put(cgroup_parent(cgrp));
  4044. kernfs_put(cgrp->kn);
  4045. kfree(cgrp);
  4046. } else {
  4047. /*
  4048. * This is root cgroup's refcnt reaching zero,
  4049. * which indicates that the root should be
  4050. * released.
  4051. */
  4052. cgroup_destroy_root(cgrp->root);
  4053. }
  4054. }
  4055. }
  4056. static void css_free_rcu_fn(struct rcu_head *rcu_head)
  4057. {
  4058. struct cgroup_subsys_state *css =
  4059. container_of(rcu_head, struct cgroup_subsys_state, rcu_head);
  4060. INIT_WORK(&css->destroy_work, css_free_work_fn);
  4061. queue_work(cgroup_destroy_wq, &css->destroy_work);
  4062. }
  4063. static void css_release_work_fn(struct work_struct *work)
  4064. {
  4065. struct cgroup_subsys_state *css =
  4066. container_of(work, struct cgroup_subsys_state, destroy_work);
  4067. struct cgroup_subsys *ss = css->ss;
  4068. struct cgroup *cgrp = css->cgroup;
  4069. mutex_lock(&cgroup_mutex);
  4070. css->flags |= CSS_RELEASED;
  4071. list_del_rcu(&css->sibling);
  4072. if (ss) {
  4073. /* css release path */
  4074. cgroup_idr_replace(&ss->css_idr, NULL, css->id);
  4075. if (ss->css_released)
  4076. ss->css_released(css);
  4077. } else {
  4078. /* cgroup release path */
  4079. cgroup_idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
  4080. cgrp->id = -1;
  4081. /*
  4082. * There are two control paths which try to determine
  4083. * cgroup from dentry without going through kernfs -
  4084. * cgroupstats_build() and css_tryget_online_from_dir().
  4085. * Those are supported by RCU protecting clearing of
  4086. * cgrp->kn->priv backpointer.
  4087. */
  4088. RCU_INIT_POINTER(*(void __rcu __force **)&cgrp->kn->priv, NULL);
  4089. }
  4090. mutex_unlock(&cgroup_mutex);
  4091. call_rcu(&css->rcu_head, css_free_rcu_fn);
  4092. }
  4093. static void css_release(struct percpu_ref *ref)
  4094. {
  4095. struct cgroup_subsys_state *css =
  4096. container_of(ref, struct cgroup_subsys_state, refcnt);
  4097. INIT_WORK(&css->destroy_work, css_release_work_fn);
  4098. queue_work(cgroup_destroy_wq, &css->destroy_work);
  4099. }
  4100. static void init_and_link_css(struct cgroup_subsys_state *css,
  4101. struct cgroup_subsys *ss, struct cgroup *cgrp)
  4102. {
  4103. lockdep_assert_held(&cgroup_mutex);
  4104. cgroup_get(cgrp);
  4105. memset(css, 0, sizeof(*css));
  4106. css->cgroup = cgrp;
  4107. css->ss = ss;
  4108. INIT_LIST_HEAD(&css->sibling);
  4109. INIT_LIST_HEAD(&css->children);
  4110. css->serial_nr = css_serial_nr_next++;
  4111. atomic_set(&css->online_cnt, 0);
  4112. if (cgroup_parent(cgrp)) {
  4113. css->parent = cgroup_css(cgroup_parent(cgrp), ss);
  4114. css_get(css->parent);
  4115. }
  4116. BUG_ON(cgroup_css(cgrp, ss));
  4117. }
  4118. /* invoke ->css_online() on a new CSS and mark it online if successful */
  4119. static int online_css(struct cgroup_subsys_state *css)
  4120. {
  4121. struct cgroup_subsys *ss = css->ss;
  4122. int ret = 0;
  4123. lockdep_assert_held(&cgroup_mutex);
  4124. if (ss->css_online)
  4125. ret = ss->css_online(css);
  4126. if (!ret) {
  4127. css->flags |= CSS_ONLINE;
  4128. rcu_assign_pointer(css->cgroup->subsys[ss->id], css);
  4129. atomic_inc(&css->online_cnt);
  4130. if (css->parent)
  4131. atomic_inc(&css->parent->online_cnt);
  4132. }
  4133. return ret;
  4134. }
  4135. /* if the CSS is online, invoke ->css_offline() on it and mark it offline */
  4136. static void offline_css(struct cgroup_subsys_state *css)
  4137. {
  4138. struct cgroup_subsys *ss = css->ss;
  4139. lockdep_assert_held(&cgroup_mutex);
  4140. if (!(css->flags & CSS_ONLINE))
  4141. return;
  4142. if (ss->css_offline)
  4143. ss->css_offline(css);
  4144. css->flags &= ~CSS_ONLINE;
  4145. RCU_INIT_POINTER(css->cgroup->subsys[ss->id], NULL);
  4146. wake_up_all(&css->cgroup->offline_waitq);
  4147. }
  4148. /**
  4149. * create_css - create a cgroup_subsys_state
  4150. * @cgrp: the cgroup new css will be associated with
  4151. * @ss: the subsys of new css
  4152. * @visible: whether to create control knobs for the new css or not
  4153. *
  4154. * Create a new css associated with @cgrp - @ss pair. On success, the new
  4155. * css is online and installed in @cgrp with all interface files created if
  4156. * @visible. Returns 0 on success, -errno on failure.
  4157. */
  4158. static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss,
  4159. bool visible)
  4160. {
  4161. struct cgroup *parent = cgroup_parent(cgrp);
  4162. struct cgroup_subsys_state *parent_css = cgroup_css(parent, ss);
  4163. struct cgroup_subsys_state *css;
  4164. int err;
  4165. lockdep_assert_held(&cgroup_mutex);
  4166. css = ss->css_alloc(parent_css);
  4167. if (IS_ERR(css))
  4168. return PTR_ERR(css);
  4169. init_and_link_css(css, ss, cgrp);
  4170. err = percpu_ref_init(&css->refcnt, css_release, 0, GFP_KERNEL);
  4171. if (err)
  4172. goto err_free_css;
  4173. err = cgroup_idr_alloc(&ss->css_idr, NULL, 2, 0, GFP_KERNEL);
  4174. if (err < 0)
  4175. goto err_free_percpu_ref;
  4176. css->id = err;
  4177. if (visible) {
  4178. err = css_populate_dir(css, NULL);
  4179. if (err)
  4180. goto err_free_id;
  4181. }
  4182. /* @css is ready to be brought online now, make it visible */
  4183. list_add_tail_rcu(&css->sibling, &parent_css->children);
  4184. cgroup_idr_replace(&ss->css_idr, css, css->id);
  4185. err = online_css(css);
  4186. if (err)
  4187. goto err_list_del;
  4188. if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
  4189. cgroup_parent(parent)) {
  4190. pr_warn("%s (%d) created nested cgroup for controller \"%s\" which has incomplete hierarchy support. Nested cgroups may change behavior in the future.\n",
  4191. current->comm, current->pid, ss->name);
  4192. if (!strcmp(ss->name, "memory"))
  4193. pr_warn("\"memory\" requires setting use_hierarchy to 1 on the root\n");
  4194. ss->warned_broken_hierarchy = true;
  4195. }
  4196. return 0;
  4197. err_list_del:
  4198. list_del_rcu(&css->sibling);
  4199. css_clear_dir(css, NULL);
  4200. err_free_id:
  4201. cgroup_idr_remove(&ss->css_idr, css->id);
  4202. err_free_percpu_ref:
  4203. percpu_ref_exit(&css->refcnt);
  4204. err_free_css:
  4205. call_rcu(&css->rcu_head, css_free_rcu_fn);
  4206. return err;
  4207. }
  4208. static int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
  4209. umode_t mode)
  4210. {
  4211. struct cgroup *parent, *cgrp, *tcgrp;
  4212. struct cgroup_root *root;
  4213. struct cgroup_subsys *ss;
  4214. struct kernfs_node *kn;
  4215. int level, ssid, ret;
  4216. /* Do not accept '\n' to prevent making /proc/<pid>/cgroup unparsable.
  4217. */
  4218. if (strchr(name, '\n'))
  4219. return -EINVAL;
  4220. parent = cgroup_kn_lock_live(parent_kn);
  4221. if (!parent)
  4222. return -ENODEV;
  4223. root = parent->root;
  4224. level = parent->level + 1;
  4225. /* allocate the cgroup and its ID, 0 is reserved for the root */
  4226. cgrp = kzalloc(sizeof(*cgrp) +
  4227. sizeof(cgrp->ancestor_ids[0]) * (level + 1), GFP_KERNEL);
  4228. if (!cgrp) {
  4229. ret = -ENOMEM;
  4230. goto out_unlock;
  4231. }
  4232. ret = percpu_ref_init(&cgrp->self.refcnt, css_release, 0, GFP_KERNEL);
  4233. if (ret)
  4234. goto out_free_cgrp;
  4235. /*
  4236. * Temporarily set the pointer to NULL, so idr_find() won't return
  4237. * a half-baked cgroup.
  4238. */
  4239. cgrp->id = cgroup_idr_alloc(&root->cgroup_idr, NULL, 2, 0, GFP_KERNEL);
  4240. if (cgrp->id < 0) {
  4241. ret = -ENOMEM;
  4242. goto out_cancel_ref;
  4243. }
  4244. init_cgroup_housekeeping(cgrp);
  4245. cgrp->self.parent = &parent->self;
  4246. cgrp->root = root;
  4247. cgrp->level = level;
  4248. for (tcgrp = cgrp; tcgrp; tcgrp = cgroup_parent(tcgrp))
  4249. cgrp->ancestor_ids[tcgrp->level] = tcgrp->id;
  4250. if (notify_on_release(parent))
  4251. set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
  4252. if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
  4253. set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
  4254. /* create the directory */
  4255. kn = kernfs_create_dir(parent->kn, name, mode, cgrp);
  4256. if (IS_ERR(kn)) {
  4257. ret = PTR_ERR(kn);
  4258. goto out_free_id;
  4259. }
  4260. cgrp->kn = kn;
  4261. /*
  4262. * This extra ref will be put in cgroup_free_fn() and guarantees
  4263. * that @cgrp->kn is always accessible.
  4264. */
  4265. kernfs_get(kn);
  4266. cgrp->self.serial_nr = css_serial_nr_next++;
  4267. /* allocation complete, commit to creation */
  4268. list_add_tail_rcu(&cgrp->self.sibling, &cgroup_parent(cgrp)->self.children);
  4269. atomic_inc(&root->nr_cgrps);
  4270. cgroup_get(parent);
  4271. /*
  4272. * @cgrp is now fully operational. If something fails after this
  4273. * point, it'll be released via the normal destruction path.
  4274. */
  4275. cgroup_idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
  4276. ret = cgroup_kn_set_ugid(kn);
  4277. if (ret)
  4278. goto out_destroy;
  4279. ret = css_populate_dir(&cgrp->self, NULL);
  4280. if (ret)
  4281. goto out_destroy;
  4282. /* let's create and online css's */
  4283. for_each_subsys(ss, ssid) {
  4284. if (parent->child_subsys_mask & (1 << ssid)) {
  4285. ret = create_css(cgrp, ss,
  4286. parent->subtree_control & (1 << ssid));
  4287. if (ret)
  4288. goto out_destroy;
  4289. }
  4290. }
  4291. /*
  4292. * On the default hierarchy, a child doesn't automatically inherit
  4293. * subtree_control from the parent. Each is configured manually.
  4294. */
  4295. if (!cgroup_on_dfl(cgrp)) {
  4296. cgrp->subtree_control = parent->subtree_control;
  4297. cgroup_refresh_child_subsys_mask(cgrp);
  4298. }
  4299. kernfs_activate(kn);
  4300. ret = 0;
  4301. goto out_unlock;
  4302. out_free_id:
  4303. cgroup_idr_remove(&root->cgroup_idr, cgrp->id);
  4304. out_cancel_ref:
  4305. percpu_ref_exit(&cgrp->self.refcnt);
  4306. out_free_cgrp:
  4307. kfree(cgrp);
  4308. out_unlock:
  4309. cgroup_kn_unlock(parent_kn);
  4310. return ret;
  4311. out_destroy:
  4312. cgroup_destroy_locked(cgrp);
  4313. goto out_unlock;
  4314. }
  4315. /*
  4316. * This is called when the refcnt of a css is confirmed to be killed.
  4317. * css_tryget_online() is now guaranteed to fail. Tell the subsystem to
  4318. * initate destruction and put the css ref from kill_css().
  4319. */
  4320. static void css_killed_work_fn(struct work_struct *work)
  4321. {
  4322. struct cgroup_subsys_state *css =
  4323. container_of(work, struct cgroup_subsys_state, destroy_work);
  4324. mutex_lock(&cgroup_mutex);
  4325. do {
  4326. offline_css(css);
  4327. css_put(css);
  4328. /* @css can't go away while we're holding cgroup_mutex */
  4329. css = css->parent;
  4330. } while (css && atomic_dec_and_test(&css->online_cnt));
  4331. mutex_unlock(&cgroup_mutex);
  4332. }
  4333. /* css kill confirmation processing requires process context, bounce */
  4334. static void css_killed_ref_fn(struct percpu_ref *ref)
  4335. {
  4336. struct cgroup_subsys_state *css =
  4337. container_of(ref, struct cgroup_subsys_state, refcnt);
  4338. if (atomic_dec_and_test(&css->online_cnt)) {
  4339. INIT_WORK(&css->destroy_work, css_killed_work_fn);
  4340. queue_work(cgroup_destroy_wq, &css->destroy_work);
  4341. }
  4342. }
  4343. /**
  4344. * kill_css - destroy a css
  4345. * @css: css to destroy
  4346. *
  4347. * This function initiates destruction of @css by removing cgroup interface
  4348. * files and putting its base reference. ->css_offline() will be invoked
  4349. * asynchronously once css_tryget_online() is guaranteed to fail and when
  4350. * the reference count reaches zero, @css will be released.
  4351. */
  4352. static void kill_css(struct cgroup_subsys_state *css)
  4353. {
  4354. lockdep_assert_held(&cgroup_mutex);
  4355. /*
  4356. * This must happen before css is disassociated with its cgroup.
  4357. * See seq_css() for details.
  4358. */
  4359. css_clear_dir(css, NULL);
  4360. /*
  4361. * Killing would put the base ref, but we need to keep it alive
  4362. * until after ->css_offline().
  4363. */
  4364. css_get(css);
  4365. /*
  4366. * cgroup core guarantees that, by the time ->css_offline() is
  4367. * invoked, no new css reference will be given out via
  4368. * css_tryget_online(). We can't simply call percpu_ref_kill() and
  4369. * proceed to offlining css's because percpu_ref_kill() doesn't
  4370. * guarantee that the ref is seen as killed on all CPUs on return.
  4371. *
  4372. * Use percpu_ref_kill_and_confirm() to get notifications as each
  4373. * css is confirmed to be seen as killed on all CPUs.
  4374. */
  4375. percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
  4376. }
  4377. /**
  4378. * cgroup_destroy_locked - the first stage of cgroup destruction
  4379. * @cgrp: cgroup to be destroyed
  4380. *
  4381. * css's make use of percpu refcnts whose killing latency shouldn't be
  4382. * exposed to userland and are RCU protected. Also, cgroup core needs to
  4383. * guarantee that css_tryget_online() won't succeed by the time
  4384. * ->css_offline() is invoked. To satisfy all the requirements,
  4385. * destruction is implemented in the following two steps.
  4386. *
  4387. * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
  4388. * userland visible parts and start killing the percpu refcnts of
  4389. * css's. Set up so that the next stage will be kicked off once all
  4390. * the percpu refcnts are confirmed to be killed.
  4391. *
  4392. * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
  4393. * rest of destruction. Once all cgroup references are gone, the
  4394. * cgroup is RCU-freed.
  4395. *
  4396. * This function implements s1. After this step, @cgrp is gone as far as
  4397. * the userland is concerned and a new cgroup with the same name may be
  4398. * created. As cgroup doesn't care about the names internally, this
  4399. * doesn't cause any problem.
  4400. */
  4401. static int cgroup_destroy_locked(struct cgroup *cgrp)
  4402. __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
  4403. {
  4404. struct cgroup_subsys_state *css;
  4405. int ssid;
  4406. lockdep_assert_held(&cgroup_mutex);
  4407. /*
  4408. * Only migration can raise populated from zero and we're already
  4409. * holding cgroup_mutex.
  4410. */
  4411. if (cgroup_is_populated(cgrp))
  4412. return -EBUSY;
  4413. /*
  4414. * Make sure there's no live children. We can't test emptiness of
  4415. * ->self.children as dead children linger on it while being
  4416. * drained; otherwise, "rmdir parent/child parent" may fail.
  4417. */
  4418. if (css_has_online_children(&cgrp->self))
  4419. return -EBUSY;
  4420. /*
  4421. * Mark @cgrp dead. This prevents further task migration and child
  4422. * creation by disabling cgroup_lock_live_group().
  4423. */
  4424. cgrp->self.flags &= ~CSS_ONLINE;
  4425. /* initiate massacre of all css's */
  4426. for_each_css(css, ssid, cgrp)
  4427. kill_css(css);
  4428. /*
  4429. * Remove @cgrp directory along with the base files. @cgrp has an
  4430. * extra ref on its kn.
  4431. */
  4432. kernfs_remove(cgrp->kn);
  4433. check_for_release(cgroup_parent(cgrp));
  4434. /* put the base reference */
  4435. percpu_ref_kill(&cgrp->self.refcnt);
  4436. return 0;
  4437. };
  4438. static int cgroup_rmdir(struct kernfs_node *kn)
  4439. {
  4440. struct cgroup *cgrp;
  4441. int ret = 0;
  4442. cgrp = cgroup_kn_lock_live(kn);
  4443. if (!cgrp)
  4444. return 0;
  4445. ret = cgroup_destroy_locked(cgrp);
  4446. cgroup_kn_unlock(kn);
  4447. return ret;
  4448. }
  4449. static struct kernfs_syscall_ops cgroup_kf_syscall_ops = {
  4450. .remount_fs = cgroup_remount,
  4451. .show_options = cgroup_show_options,
  4452. .mkdir = cgroup_mkdir,
  4453. .rmdir = cgroup_rmdir,
  4454. .rename = cgroup_rename,
  4455. };
  4456. static void __init cgroup_init_subsys(struct cgroup_subsys *ss, bool early)
  4457. {
  4458. struct cgroup_subsys_state *css;
  4459. pr_debug("Initializing cgroup subsys %s\n", ss->name);
  4460. mutex_lock(&cgroup_mutex);
  4461. idr_init(&ss->css_idr);
  4462. INIT_LIST_HEAD(&ss->cfts);
  4463. /* Create the root cgroup state for this subsystem */
  4464. ss->root = &cgrp_dfl_root;
  4465. css = ss->css_alloc(cgroup_css(&cgrp_dfl_root.cgrp, ss));
  4466. /* We don't handle early failures gracefully */
  4467. BUG_ON(IS_ERR(css));
  4468. init_and_link_css(css, ss, &cgrp_dfl_root.cgrp);
  4469. /*
  4470. * Root csses are never destroyed and we can't initialize
  4471. * percpu_ref during early init. Disable refcnting.
  4472. */
  4473. css->flags |= CSS_NO_REF;
  4474. if (early) {
  4475. /* allocation can't be done safely during early init */
  4476. css->id = 1;
  4477. } else {
  4478. css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2, GFP_KERNEL);
  4479. BUG_ON(css->id < 0);
  4480. }
  4481. /* Update the init_css_set to contain a subsys
  4482. * pointer to this state - since the subsystem is
  4483. * newly registered, all tasks and hence the
  4484. * init_css_set is in the subsystem's root cgroup. */
  4485. init_css_set.subsys[ss->id] = css;
  4486. have_fork_callback |= (bool)ss->fork << ss->id;
  4487. have_exit_callback |= (bool)ss->exit << ss->id;
  4488. have_free_callback |= (bool)ss->free << ss->id;
  4489. have_canfork_callback |= (bool)ss->can_fork << ss->id;
  4490. /* At system boot, before all subsystems have been
  4491. * registered, no tasks have been forked, so we don't
  4492. * need to invoke fork callbacks here. */
  4493. BUG_ON(!list_empty(&init_task.tasks));
  4494. BUG_ON(online_css(css));
  4495. mutex_unlock(&cgroup_mutex);
  4496. }
  4497. /**
  4498. * cgroup_init_early - cgroup initialization at system boot
  4499. *
  4500. * Initialize cgroups at system boot, and initialize any
  4501. * subsystems that request early init.
  4502. */
  4503. int __init cgroup_init_early(void)
  4504. {
  4505. static struct cgroup_sb_opts __initdata opts;
  4506. struct cgroup_subsys *ss;
  4507. int i;
  4508. init_cgroup_root(&cgrp_dfl_root, &opts);
  4509. cgrp_dfl_root.cgrp.self.flags |= CSS_NO_REF;
  4510. RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
  4511. for_each_subsys(ss, i) {
  4512. WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id,
  4513. "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p name:id=%d:%s\n",
  4514. i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free,
  4515. ss->id, ss->name);
  4516. WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN,
  4517. "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]);
  4518. ss->id = i;
  4519. ss->name = cgroup_subsys_name[i];
  4520. if (!ss->legacy_name)
  4521. ss->legacy_name = cgroup_subsys_name[i];
  4522. if (ss->early_init)
  4523. cgroup_init_subsys(ss, true);
  4524. }
  4525. return 0;
  4526. }
  4527. static unsigned long cgroup_disable_mask __initdata;
  4528. /**
  4529. * cgroup_init - cgroup initialization
  4530. *
  4531. * Register cgroup filesystem and /proc file, and initialize
  4532. * any subsystems that didn't request early init.
  4533. */
  4534. int __init cgroup_init(void)
  4535. {
  4536. struct cgroup_subsys *ss;
  4537. unsigned long key;
  4538. int ssid;
  4539. BUG_ON(percpu_init_rwsem(&cgroup_threadgroup_rwsem));
  4540. BUG_ON(cgroup_init_cftypes(NULL, cgroup_dfl_base_files));
  4541. BUG_ON(cgroup_init_cftypes(NULL, cgroup_legacy_base_files));
  4542. mutex_lock(&cgroup_mutex);
  4543. /* Add init_css_set to the hash table */
  4544. key = css_set_hash(init_css_set.subsys);
  4545. hash_add(css_set_table, &init_css_set.hlist, key);
  4546. BUG_ON(cgroup_setup_root(&cgrp_dfl_root, 0));
  4547. mutex_unlock(&cgroup_mutex);
  4548. for_each_subsys(ss, ssid) {
  4549. if (ss->early_init) {
  4550. struct cgroup_subsys_state *css =
  4551. init_css_set.subsys[ss->id];
  4552. css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2,
  4553. GFP_KERNEL);
  4554. BUG_ON(css->id < 0);
  4555. } else {
  4556. cgroup_init_subsys(ss, false);
  4557. }
  4558. list_add_tail(&init_css_set.e_cset_node[ssid],
  4559. &cgrp_dfl_root.cgrp.e_csets[ssid]);
  4560. /*
  4561. * Setting dfl_root subsys_mask needs to consider the
  4562. * disabled flag and cftype registration needs kmalloc,
  4563. * both of which aren't available during early_init.
  4564. */
  4565. if (cgroup_disable_mask & (1 << ssid)) {
  4566. static_branch_disable(cgroup_subsys_enabled_key[ssid]);
  4567. printk(KERN_INFO "Disabling %s control group subsystem\n",
  4568. ss->name);
  4569. continue;
  4570. }
  4571. cgrp_dfl_root.subsys_mask |= 1 << ss->id;
  4572. if (!ss->dfl_cftypes)
  4573. cgrp_dfl_root_inhibit_ss_mask |= 1 << ss->id;
  4574. if (ss->dfl_cftypes == ss->legacy_cftypes) {
  4575. WARN_ON(cgroup_add_cftypes(ss, ss->dfl_cftypes));
  4576. } else {
  4577. WARN_ON(cgroup_add_dfl_cftypes(ss, ss->dfl_cftypes));
  4578. WARN_ON(cgroup_add_legacy_cftypes(ss, ss->legacy_cftypes));
  4579. }
  4580. if (ss->bind)
  4581. ss->bind(init_css_set.subsys[ssid]);
  4582. }
  4583. WARN_ON(sysfs_create_mount_point(fs_kobj, "cgroup"));
  4584. WARN_ON(register_filesystem(&cgroup_fs_type));
  4585. WARN_ON(register_filesystem(&cgroup2_fs_type));
  4586. WARN_ON(!proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations));
  4587. return 0;
  4588. }
  4589. static int __init cgroup_wq_init(void)
  4590. {
  4591. /*
  4592. * There isn't much point in executing destruction path in
  4593. * parallel. Good chunk is serialized with cgroup_mutex anyway.
  4594. * Use 1 for @max_active.
  4595. *
  4596. * We would prefer to do this in cgroup_init() above, but that
  4597. * is called before init_workqueues(): so leave this until after.
  4598. */
  4599. cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1);
  4600. BUG_ON(!cgroup_destroy_wq);
  4601. /*
  4602. * Used to destroy pidlists and separate to serve as flush domain.
  4603. * Cap @max_active to 1 too.
  4604. */
  4605. cgroup_pidlist_destroy_wq = alloc_workqueue("cgroup_pidlist_destroy",
  4606. 0, 1);
  4607. BUG_ON(!cgroup_pidlist_destroy_wq);
  4608. return 0;
  4609. }
  4610. core_initcall(cgroup_wq_init);
  4611. /*
  4612. * proc_cgroup_show()
  4613. * - Print task's cgroup paths into seq_file, one line for each hierarchy
  4614. * - Used for /proc/<pid>/cgroup.
  4615. */
  4616. int proc_cgroup_show(struct seq_file *m, struct pid_namespace *ns,
  4617. struct pid *pid, struct task_struct *tsk)
  4618. {
  4619. char *buf, *path;
  4620. int retval;
  4621. struct cgroup_root *root;
  4622. retval = -ENOMEM;
  4623. buf = kmalloc(PATH_MAX, GFP_KERNEL);
  4624. if (!buf)
  4625. goto out;
  4626. mutex_lock(&cgroup_mutex);
  4627. spin_lock_bh(&css_set_lock);
  4628. for_each_root(root) {
  4629. struct cgroup_subsys *ss;
  4630. struct cgroup *cgrp;
  4631. int ssid, count = 0;
  4632. if (root == &cgrp_dfl_root && !cgrp_dfl_root_visible)
  4633. continue;
  4634. seq_printf(m, "%d:", root->hierarchy_id);
  4635. if (root != &cgrp_dfl_root)
  4636. for_each_subsys(ss, ssid)
  4637. if (root->subsys_mask & (1 << ssid))
  4638. seq_printf(m, "%s%s", count++ ? "," : "",
  4639. ss->legacy_name);
  4640. if (strlen(root->name))
  4641. seq_printf(m, "%sname=%s", count ? "," : "",
  4642. root->name);
  4643. seq_putc(m, ':');
  4644. cgrp = task_cgroup_from_root(tsk, root);
  4645. /*
  4646. * On traditional hierarchies, all zombie tasks show up as
  4647. * belonging to the root cgroup. On the default hierarchy,
  4648. * while a zombie doesn't show up in "cgroup.procs" and
  4649. * thus can't be migrated, its /proc/PID/cgroup keeps
  4650. * reporting the cgroup it belonged to before exiting. If
  4651. * the cgroup is removed before the zombie is reaped,
  4652. * " (deleted)" is appended to the cgroup path.
  4653. */
  4654. if (cgroup_on_dfl(cgrp) || !(tsk->flags & PF_EXITING)) {
  4655. path = cgroup_path(cgrp, buf, PATH_MAX);
  4656. if (!path) {
  4657. retval = -ENAMETOOLONG;
  4658. goto out_unlock;
  4659. }
  4660. } else {
  4661. path = "/";
  4662. }
  4663. seq_puts(m, path);
  4664. if (cgroup_on_dfl(cgrp) && cgroup_is_dead(cgrp))
  4665. seq_puts(m, " (deleted)\n");
  4666. else
  4667. seq_putc(m, '\n');
  4668. }
  4669. retval = 0;
  4670. out_unlock:
  4671. spin_unlock_bh(&css_set_lock);
  4672. mutex_unlock(&cgroup_mutex);
  4673. kfree(buf);
  4674. out:
  4675. return retval;
  4676. }
  4677. /* Display information about each subsystem and each hierarchy */
  4678. static int proc_cgroupstats_show(struct seq_file *m, void *v)
  4679. {
  4680. struct cgroup_subsys *ss;
  4681. int i;
  4682. seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
  4683. /*
  4684. * ideally we don't want subsystems moving around while we do this.
  4685. * cgroup_mutex is also necessary to guarantee an atomic snapshot of
  4686. * subsys/hierarchy state.
  4687. */
  4688. mutex_lock(&cgroup_mutex);
  4689. for_each_subsys(ss, i)
  4690. seq_printf(m, "%s\t%d\t%d\t%d\n",
  4691. ss->legacy_name, ss->root->hierarchy_id,
  4692. atomic_read(&ss->root->nr_cgrps),
  4693. cgroup_ssid_enabled(i));
  4694. mutex_unlock(&cgroup_mutex);
  4695. return 0;
  4696. }
  4697. static int cgroupstats_open(struct inode *inode, struct file *file)
  4698. {
  4699. return single_open(file, proc_cgroupstats_show, NULL);
  4700. }
  4701. static const struct file_operations proc_cgroupstats_operations = {
  4702. .open = cgroupstats_open,
  4703. .read = seq_read,
  4704. .llseek = seq_lseek,
  4705. .release = single_release,
  4706. };
  4707. /**
  4708. * cgroup_fork - initialize cgroup related fields during copy_process()
  4709. * @child: pointer to task_struct of forking parent process.
  4710. *
  4711. * A task is associated with the init_css_set until cgroup_post_fork()
  4712. * attaches it to the parent's css_set. Empty cg_list indicates that
  4713. * @child isn't holding reference to its css_set.
  4714. */
  4715. void cgroup_fork(struct task_struct *child)
  4716. {
  4717. RCU_INIT_POINTER(child->cgroups, &init_css_set);
  4718. INIT_LIST_HEAD(&child->cg_list);
  4719. }
  4720. /**
  4721. * cgroup_can_fork - called on a new task before the process is exposed
  4722. * @child: the task in question.
  4723. *
  4724. * This calls the subsystem can_fork() callbacks. If the can_fork() callback
  4725. * returns an error, the fork aborts with that error code. This allows for
  4726. * a cgroup subsystem to conditionally allow or deny new forks.
  4727. */
  4728. int cgroup_can_fork(struct task_struct *child)
  4729. {
  4730. struct cgroup_subsys *ss;
  4731. int i, j, ret;
  4732. for_each_subsys_which(ss, i, &have_canfork_callback) {
  4733. ret = ss->can_fork(child);
  4734. if (ret)
  4735. goto out_revert;
  4736. }
  4737. return 0;
  4738. out_revert:
  4739. for_each_subsys(ss, j) {
  4740. if (j >= i)
  4741. break;
  4742. if (ss->cancel_fork)
  4743. ss->cancel_fork(child);
  4744. }
  4745. return ret;
  4746. }
  4747. /**
  4748. * cgroup_cancel_fork - called if a fork failed after cgroup_can_fork()
  4749. * @child: the task in question
  4750. *
  4751. * This calls the cancel_fork() callbacks if a fork failed *after*
  4752. * cgroup_can_fork() succeded.
  4753. */
  4754. void cgroup_cancel_fork(struct task_struct *child)
  4755. {
  4756. struct cgroup_subsys *ss;
  4757. int i;
  4758. for_each_subsys(ss, i)
  4759. if (ss->cancel_fork)
  4760. ss->cancel_fork(child);
  4761. }
  4762. /**
  4763. * cgroup_post_fork - called on a new task after adding it to the task list
  4764. * @child: the task in question
  4765. *
  4766. * Adds the task to the list running through its css_set if necessary and
  4767. * call the subsystem fork() callbacks. Has to be after the task is
  4768. * visible on the task list in case we race with the first call to
  4769. * cgroup_task_iter_start() - to guarantee that the new task ends up on its
  4770. * list.
  4771. */
  4772. void cgroup_post_fork(struct task_struct *child)
  4773. {
  4774. struct cgroup_subsys *ss;
  4775. int i;
  4776. /*
  4777. * This may race against cgroup_enable_task_cg_lists(). As that
  4778. * function sets use_task_css_set_links before grabbing
  4779. * tasklist_lock and we just went through tasklist_lock to add
  4780. * @child, it's guaranteed that either we see the set
  4781. * use_task_css_set_links or cgroup_enable_task_cg_lists() sees
  4782. * @child during its iteration.
  4783. *
  4784. * If we won the race, @child is associated with %current's
  4785. * css_set. Grabbing css_set_lock guarantees both that the
  4786. * association is stable, and, on completion of the parent's
  4787. * migration, @child is visible in the source of migration or
  4788. * already in the destination cgroup. This guarantee is necessary
  4789. * when implementing operations which need to migrate all tasks of
  4790. * a cgroup to another.
  4791. *
  4792. * Note that if we lose to cgroup_enable_task_cg_lists(), @child
  4793. * will remain in init_css_set. This is safe because all tasks are
  4794. * in the init_css_set before cg_links is enabled and there's no
  4795. * operation which transfers all tasks out of init_css_set.
  4796. */
  4797. if (use_task_css_set_links) {
  4798. struct css_set *cset;
  4799. spin_lock_bh(&css_set_lock);
  4800. cset = task_css_set(current);
  4801. if (list_empty(&child->cg_list)) {
  4802. get_css_set(cset);
  4803. css_set_move_task(child, NULL, cset, false);
  4804. }
  4805. spin_unlock_bh(&css_set_lock);
  4806. }
  4807. /*
  4808. * Call ss->fork(). This must happen after @child is linked on
  4809. * css_set; otherwise, @child might change state between ->fork()
  4810. * and addition to css_set.
  4811. */
  4812. for_each_subsys_which(ss, i, &have_fork_callback)
  4813. ss->fork(child);
  4814. }
  4815. /**
  4816. * cgroup_exit - detach cgroup from exiting task
  4817. * @tsk: pointer to task_struct of exiting process
  4818. *
  4819. * Description: Detach cgroup from @tsk and release it.
  4820. *
  4821. * Note that cgroups marked notify_on_release force every task in
  4822. * them to take the global cgroup_mutex mutex when exiting.
  4823. * This could impact scaling on very large systems. Be reluctant to
  4824. * use notify_on_release cgroups where very high task exit scaling
  4825. * is required on large systems.
  4826. *
  4827. * We set the exiting tasks cgroup to the root cgroup (top_cgroup). We
  4828. * call cgroup_exit() while the task is still competent to handle
  4829. * notify_on_release(), then leave the task attached to the root cgroup in
  4830. * each hierarchy for the remainder of its exit. No need to bother with
  4831. * init_css_set refcnting. init_css_set never goes away and we can't race
  4832. * with migration path - PF_EXITING is visible to migration path.
  4833. */
  4834. void cgroup_exit(struct task_struct *tsk)
  4835. {
  4836. struct cgroup_subsys *ss;
  4837. struct css_set *cset;
  4838. int i;
  4839. /*
  4840. * Unlink from @tsk from its css_set. As migration path can't race
  4841. * with us, we can check css_set and cg_list without synchronization.
  4842. */
  4843. cset = task_css_set(tsk);
  4844. if (!list_empty(&tsk->cg_list)) {
  4845. spin_lock_bh(&css_set_lock);
  4846. css_set_move_task(tsk, cset, NULL, false);
  4847. spin_unlock_bh(&css_set_lock);
  4848. } else {
  4849. get_css_set(cset);
  4850. }
  4851. /* see cgroup_post_fork() for details */
  4852. for_each_subsys_which(ss, i, &have_exit_callback)
  4853. ss->exit(tsk);
  4854. }
  4855. void cgroup_free(struct task_struct *task)
  4856. {
  4857. struct css_set *cset = task_css_set(task);
  4858. struct cgroup_subsys *ss;
  4859. int ssid;
  4860. for_each_subsys_which(ss, ssid, &have_free_callback)
  4861. ss->free(task);
  4862. put_css_set(cset);
  4863. }
  4864. static void check_for_release(struct cgroup *cgrp)
  4865. {
  4866. if (notify_on_release(cgrp) && !cgroup_is_populated(cgrp) &&
  4867. !css_has_online_children(&cgrp->self) && !cgroup_is_dead(cgrp))
  4868. schedule_work(&cgrp->release_agent_work);
  4869. }
  4870. /*
  4871. * Notify userspace when a cgroup is released, by running the
  4872. * configured release agent with the name of the cgroup (path
  4873. * relative to the root of cgroup file system) as the argument.
  4874. *
  4875. * Most likely, this user command will try to rmdir this cgroup.
  4876. *
  4877. * This races with the possibility that some other task will be
  4878. * attached to this cgroup before it is removed, or that some other
  4879. * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
  4880. * The presumed 'rmdir' will fail quietly if this cgroup is no longer
  4881. * unused, and this cgroup will be reprieved from its death sentence,
  4882. * to continue to serve a useful existence. Next time it's released,
  4883. * we will get notified again, if it still has 'notify_on_release' set.
  4884. *
  4885. * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
  4886. * means only wait until the task is successfully execve()'d. The
  4887. * separate release agent task is forked by call_usermodehelper(),
  4888. * then control in this thread returns here, without waiting for the
  4889. * release agent task. We don't bother to wait because the caller of
  4890. * this routine has no use for the exit status of the release agent
  4891. * task, so no sense holding our caller up for that.
  4892. */
  4893. static void cgroup_release_agent(struct work_struct *work)
  4894. {
  4895. struct cgroup *cgrp =
  4896. container_of(work, struct cgroup, release_agent_work);
  4897. char *pathbuf = NULL, *agentbuf = NULL, *path;
  4898. char *argv[3], *envp[3];
  4899. mutex_lock(&cgroup_mutex);
  4900. pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
  4901. agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
  4902. if (!pathbuf || !agentbuf)
  4903. goto out;
  4904. path = cgroup_path(cgrp, pathbuf, PATH_MAX);
  4905. if (!path)
  4906. goto out;
  4907. argv[0] = agentbuf;
  4908. argv[1] = path;
  4909. argv[2] = NULL;
  4910. /* minimal command environment */
  4911. envp[0] = "HOME=/";
  4912. envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
  4913. envp[2] = NULL;
  4914. mutex_unlock(&cgroup_mutex);
  4915. call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
  4916. goto out_free;
  4917. out:
  4918. mutex_unlock(&cgroup_mutex);
  4919. out_free:
  4920. kfree(agentbuf);
  4921. kfree(pathbuf);
  4922. }
  4923. static int __init cgroup_disable(char *str)
  4924. {
  4925. struct cgroup_subsys *ss;
  4926. char *token;
  4927. int i;
  4928. while ((token = strsep(&str, ",")) != NULL) {
  4929. if (!*token)
  4930. continue;
  4931. for_each_subsys(ss, i) {
  4932. if (strcmp(token, ss->name) &&
  4933. strcmp(token, ss->legacy_name))
  4934. continue;
  4935. cgroup_disable_mask |= 1 << i;
  4936. }
  4937. }
  4938. return 1;
  4939. }
  4940. __setup("cgroup_disable=", cgroup_disable);
  4941. /**
  4942. * css_tryget_online_from_dir - get corresponding css from a cgroup dentry
  4943. * @dentry: directory dentry of interest
  4944. * @ss: subsystem of interest
  4945. *
  4946. * If @dentry is a directory for a cgroup which has @ss enabled on it, try
  4947. * to get the corresponding css and return it. If such css doesn't exist
  4948. * or can't be pinned, an ERR_PTR value is returned.
  4949. */
  4950. struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry,
  4951. struct cgroup_subsys *ss)
  4952. {
  4953. struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
  4954. struct cgroup_subsys_state *css = NULL;
  4955. struct cgroup *cgrp;
  4956. /* is @dentry a cgroup dir? */
  4957. if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
  4958. kernfs_type(kn) != KERNFS_DIR)
  4959. return ERR_PTR(-EBADF);
  4960. rcu_read_lock();
  4961. /*
  4962. * This path doesn't originate from kernfs and @kn could already
  4963. * have been or be removed at any point. @kn->priv is RCU
  4964. * protected for this access. See css_release_work_fn() for details.
  4965. */
  4966. cgrp = rcu_dereference(kn->priv);
  4967. if (cgrp)
  4968. css = cgroup_css(cgrp, ss);
  4969. if (!css || !css_tryget_online(css))
  4970. css = ERR_PTR(-ENOENT);
  4971. rcu_read_unlock();
  4972. return css;
  4973. }
  4974. /**
  4975. * css_from_id - lookup css by id
  4976. * @id: the cgroup id
  4977. * @ss: cgroup subsys to be looked into
  4978. *
  4979. * Returns the css if there's valid one with @id, otherwise returns NULL.
  4980. * Should be called under rcu_read_lock().
  4981. */
  4982. struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
  4983. {
  4984. WARN_ON_ONCE(!rcu_read_lock_held());
  4985. return id > 0 ? idr_find(&ss->css_idr, id) : NULL;
  4986. }
  4987. /**
  4988. * cgroup_get_from_path - lookup and get a cgroup from its default hierarchy path
  4989. * @path: path on the default hierarchy
  4990. *
  4991. * Find the cgroup at @path on the default hierarchy, increment its
  4992. * reference count and return it. Returns pointer to the found cgroup on
  4993. * success, ERR_PTR(-ENOENT) if @path doens't exist and ERR_PTR(-ENOTDIR)
  4994. * if @path points to a non-directory.
  4995. */
  4996. struct cgroup *cgroup_get_from_path(const char *path)
  4997. {
  4998. struct kernfs_node *kn;
  4999. struct cgroup *cgrp;
  5000. mutex_lock(&cgroup_mutex);
  5001. kn = kernfs_walk_and_get(cgrp_dfl_root.cgrp.kn, path);
  5002. if (kn) {
  5003. if (kernfs_type(kn) == KERNFS_DIR) {
  5004. cgrp = kn->priv;
  5005. cgroup_get(cgrp);
  5006. } else {
  5007. cgrp = ERR_PTR(-ENOTDIR);
  5008. }
  5009. kernfs_put(kn);
  5010. } else {
  5011. cgrp = ERR_PTR(-ENOENT);
  5012. }
  5013. mutex_unlock(&cgroup_mutex);
  5014. return cgrp;
  5015. }
  5016. EXPORT_SYMBOL_GPL(cgroup_get_from_path);
  5017. /*
  5018. * sock->sk_cgrp_data handling. For more info, see sock_cgroup_data
  5019. * definition in cgroup-defs.h.
  5020. */
  5021. #ifdef CONFIG_SOCK_CGROUP_DATA
  5022. #if defined(CONFIG_CGROUP_NET_PRIO) || defined(CONFIG_CGROUP_NET_CLASSID)
  5023. DEFINE_SPINLOCK(cgroup_sk_update_lock);
  5024. static bool cgroup_sk_alloc_disabled __read_mostly;
  5025. void cgroup_sk_alloc_disable(void)
  5026. {
  5027. if (cgroup_sk_alloc_disabled)
  5028. return;
  5029. pr_info("cgroup: disabling cgroup2 socket matching due to net_prio or net_cls activation\n");
  5030. cgroup_sk_alloc_disabled = true;
  5031. }
  5032. #else
  5033. #define cgroup_sk_alloc_disabled false
  5034. #endif
  5035. void cgroup_sk_alloc(struct sock_cgroup_data *skcd)
  5036. {
  5037. if (cgroup_sk_alloc_disabled)
  5038. return;
  5039. rcu_read_lock();
  5040. while (true) {
  5041. struct css_set *cset;
  5042. cset = task_css_set(current);
  5043. if (likely(cgroup_tryget(cset->dfl_cgrp))) {
  5044. skcd->val = (unsigned long)cset->dfl_cgrp;
  5045. break;
  5046. }
  5047. cpu_relax();
  5048. }
  5049. rcu_read_unlock();
  5050. }
  5051. void cgroup_sk_free(struct sock_cgroup_data *skcd)
  5052. {
  5053. cgroup_put(sock_cgroup_ptr(skcd));
  5054. }
  5055. #endif /* CONFIG_SOCK_CGROUP_DATA */
  5056. #ifdef CONFIG_CGROUP_DEBUG
  5057. static struct cgroup_subsys_state *
  5058. debug_css_alloc(struct cgroup_subsys_state *parent_css)
  5059. {
  5060. struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
  5061. if (!css)
  5062. return ERR_PTR(-ENOMEM);
  5063. return css;
  5064. }
  5065. static void debug_css_free(struct cgroup_subsys_state *css)
  5066. {
  5067. kfree(css);
  5068. }
  5069. static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
  5070. struct cftype *cft)
  5071. {
  5072. return cgroup_task_count(css->cgroup);
  5073. }
  5074. static u64 current_css_set_read(struct cgroup_subsys_state *css,
  5075. struct cftype *cft)
  5076. {
  5077. return (u64)(unsigned long)current->cgroups;
  5078. }
  5079. static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css,
  5080. struct cftype *cft)
  5081. {
  5082. u64 count;
  5083. rcu_read_lock();
  5084. count = atomic_read(&task_css_set(current)->refcount);
  5085. rcu_read_unlock();
  5086. return count;
  5087. }
  5088. static int current_css_set_cg_links_read(struct seq_file *seq, void *v)
  5089. {
  5090. struct cgrp_cset_link *link;
  5091. struct css_set *cset;
  5092. char *name_buf;
  5093. name_buf = kmalloc(NAME_MAX + 1, GFP_KERNEL);
  5094. if (!name_buf)
  5095. return -ENOMEM;
  5096. spin_lock_bh(&css_set_lock);
  5097. rcu_read_lock();
  5098. cset = rcu_dereference(current->cgroups);
  5099. list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
  5100. struct cgroup *c = link->cgrp;
  5101. cgroup_name(c, name_buf, NAME_MAX + 1);
  5102. seq_printf(seq, "Root %d group %s\n",
  5103. c->root->hierarchy_id, name_buf);
  5104. }
  5105. rcu_read_unlock();
  5106. spin_unlock_bh(&css_set_lock);
  5107. kfree(name_buf);
  5108. return 0;
  5109. }
  5110. #define MAX_TASKS_SHOWN_PER_CSS 25
  5111. static int cgroup_css_links_read(struct seq_file *seq, void *v)
  5112. {
  5113. struct cgroup_subsys_state *css = seq_css(seq);
  5114. struct cgrp_cset_link *link;
  5115. spin_lock_bh(&css_set_lock);
  5116. list_for_each_entry(link, &css->cgroup->cset_links, cset_link) {
  5117. struct css_set *cset = link->cset;
  5118. struct task_struct *task;
  5119. int count = 0;
  5120. seq_printf(seq, "css_set %p\n", cset);
  5121. list_for_each_entry(task, &cset->tasks, cg_list) {
  5122. if (count++ > MAX_TASKS_SHOWN_PER_CSS)
  5123. goto overflow;
  5124. seq_printf(seq, " task %d\n", task_pid_vnr(task));
  5125. }
  5126. list_for_each_entry(task, &cset->mg_tasks, cg_list) {
  5127. if (count++ > MAX_TASKS_SHOWN_PER_CSS)
  5128. goto overflow;
  5129. seq_printf(seq, " task %d\n", task_pid_vnr(task));
  5130. }
  5131. continue;
  5132. overflow:
  5133. seq_puts(seq, " ...\n");
  5134. }
  5135. spin_unlock_bh(&css_set_lock);
  5136. return 0;
  5137. }
  5138. static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft)
  5139. {
  5140. return (!cgroup_is_populated(css->cgroup) &&
  5141. !css_has_online_children(&css->cgroup->self));
  5142. }
  5143. static struct cftype debug_files[] = {
  5144. {
  5145. .name = "taskcount",
  5146. .read_u64 = debug_taskcount_read,
  5147. },
  5148. {
  5149. .name = "current_css_set",
  5150. .read_u64 = current_css_set_read,
  5151. },
  5152. {
  5153. .name = "current_css_set_refcount",
  5154. .read_u64 = current_css_set_refcount_read,
  5155. },
  5156. {
  5157. .name = "current_css_set_cg_links",
  5158. .seq_show = current_css_set_cg_links_read,
  5159. },
  5160. {
  5161. .name = "cgroup_css_links",
  5162. .seq_show = cgroup_css_links_read,
  5163. },
  5164. {
  5165. .name = "releasable",
  5166. .read_u64 = releasable_read,
  5167. },
  5168. { } /* terminate */
  5169. };
  5170. struct cgroup_subsys debug_cgrp_subsys = {
  5171. .css_alloc = debug_css_alloc,
  5172. .css_free = debug_css_free,
  5173. .legacy_cftypes = debug_files,
  5174. };
  5175. #endif /* CONFIG_CGROUP_DEBUG */