cgroup.c 185 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099
  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 "cgroup-internal.h"
  30. #include <linux/bpf-cgroup.h>
  31. #include <linux/cred.h>
  32. #include <linux/errno.h>
  33. #include <linux/init_task.h>
  34. #include <linux/kernel.h>
  35. #include <linux/magic.h>
  36. #include <linux/mutex.h>
  37. #include <linux/mount.h>
  38. #include <linux/pagemap.h>
  39. #include <linux/proc_fs.h>
  40. #include <linux/rcupdate.h>
  41. #include <linux/sched.h>
  42. #include <linux/sched/task.h>
  43. #include <linux/slab.h>
  44. #include <linux/spinlock.h>
  45. #include <linux/percpu-rwsem.h>
  46. #include <linux/string.h>
  47. #include <linux/hashtable.h>
  48. #include <linux/idr.h>
  49. #include <linux/kthread.h>
  50. #include <linux/atomic.h>
  51. #include <linux/cpuset.h>
  52. #include <linux/proc_ns.h>
  53. #include <linux/nsproxy.h>
  54. #include <linux/file.h>
  55. #include <linux/fs_parser.h>
  56. #include <linux/sched/cputime.h>
  57. #include <linux/sched/deadline.h>
  58. #include <linux/psi.h>
  59. #include <net/sock.h>
  60. #define CREATE_TRACE_POINTS
  61. #include <trace/events/cgroup.h>
  62. #undef CREATE_TRACE_POINTS
  63. #include <trace/hooks/cgroup.h>
  64. #define CGROUP_FILE_NAME_MAX (MAX_CGROUP_TYPE_NAMELEN + \
  65. MAX_CFTYPE_NAME + 2)
  66. /* let's not notify more than 100 times per second */
  67. #define CGROUP_FILE_NOTIFY_MIN_INTV DIV_ROUND_UP(HZ, 100)
  68. /*
  69. * To avoid confusing the compiler (and generating warnings) with code
  70. * that attempts to access what would be a 0-element array (i.e. sized
  71. * to a potentially empty array when CGROUP_SUBSYS_COUNT == 0), this
  72. * constant expression can be added.
  73. */
  74. #define CGROUP_HAS_SUBSYS_CONFIG (CGROUP_SUBSYS_COUNT > 0)
  75. /*
  76. * cgroup_mutex is the master lock. Any modification to cgroup or its
  77. * hierarchy must be performed while holding it.
  78. *
  79. * css_set_lock protects task->cgroups pointer, the list of css_set
  80. * objects, and the chain of tasks off each css_set.
  81. *
  82. * These locks are exported if CONFIG_PROVE_RCU so that accessors in
  83. * cgroup.h can use them for lockdep annotations.
  84. */
  85. DEFINE_MUTEX(cgroup_mutex);
  86. DEFINE_SPINLOCK(css_set_lock);
  87. #ifdef CONFIG_PROVE_RCU
  88. EXPORT_SYMBOL_GPL(cgroup_mutex);
  89. EXPORT_SYMBOL_GPL(css_set_lock);
  90. #endif
  91. DEFINE_SPINLOCK(trace_cgroup_path_lock);
  92. char trace_cgroup_path[TRACE_CGROUP_PATH_LEN];
  93. static bool cgroup_debug __read_mostly;
  94. /*
  95. * Protects cgroup_idr and css_idr so that IDs can be released without
  96. * grabbing cgroup_mutex.
  97. */
  98. static DEFINE_SPINLOCK(cgroup_idr_lock);
  99. /*
  100. * Protects cgroup_file->kn for !self csses. It synchronizes notifications
  101. * against file removal/re-creation across css hiding.
  102. */
  103. static DEFINE_SPINLOCK(cgroup_file_kn_lock);
  104. DEFINE_PERCPU_RWSEM(cgroup_threadgroup_rwsem);
  105. EXPORT_SYMBOL_GPL(cgroup_threadgroup_rwsem);
  106. #define cgroup_assert_mutex_or_rcu_locked() \
  107. RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \
  108. !lockdep_is_held(&cgroup_mutex), \
  109. "cgroup_mutex or RCU read lock required");
  110. /*
  111. * cgroup destruction makes heavy use of work items and there can be a lot
  112. * of concurrent destructions. Use a separate workqueue so that cgroup
  113. * destruction work items don't end up filling up max_active of system_wq
  114. * which may lead to deadlock.
  115. */
  116. static struct workqueue_struct *cgroup_destroy_wq;
  117. /* generate an array of cgroup subsystem pointers */
  118. #define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys,
  119. struct cgroup_subsys *cgroup_subsys[] = {
  120. #include <linux/cgroup_subsys.h>
  121. };
  122. #undef SUBSYS
  123. /* array of cgroup subsystem names */
  124. #define SUBSYS(_x) [_x ## _cgrp_id] = #_x,
  125. static const char *cgroup_subsys_name[] = {
  126. #include <linux/cgroup_subsys.h>
  127. };
  128. #undef SUBSYS
  129. /* array of static_keys for cgroup_subsys_enabled() and cgroup_subsys_on_dfl() */
  130. #define SUBSYS(_x) \
  131. DEFINE_STATIC_KEY_TRUE(_x ## _cgrp_subsys_enabled_key); \
  132. DEFINE_STATIC_KEY_TRUE(_x ## _cgrp_subsys_on_dfl_key); \
  133. EXPORT_SYMBOL_GPL(_x ## _cgrp_subsys_enabled_key); \
  134. EXPORT_SYMBOL_GPL(_x ## _cgrp_subsys_on_dfl_key);
  135. #include <linux/cgroup_subsys.h>
  136. #undef SUBSYS
  137. #define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys_enabled_key,
  138. static struct static_key_true *cgroup_subsys_enabled_key[] = {
  139. #include <linux/cgroup_subsys.h>
  140. };
  141. #undef SUBSYS
  142. #define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys_on_dfl_key,
  143. static struct static_key_true *cgroup_subsys_on_dfl_key[] = {
  144. #include <linux/cgroup_subsys.h>
  145. };
  146. #undef SUBSYS
  147. static DEFINE_PER_CPU(struct cgroup_rstat_cpu, cgrp_dfl_root_rstat_cpu);
  148. /* the default hierarchy */
  149. struct cgroup_root cgrp_dfl_root = { .cgrp.rstat_cpu = &cgrp_dfl_root_rstat_cpu };
  150. EXPORT_SYMBOL_GPL(cgrp_dfl_root);
  151. /*
  152. * The default hierarchy always exists but is hidden until mounted for the
  153. * first time. This is for backward compatibility.
  154. */
  155. static bool cgrp_dfl_visible;
  156. /* some controllers are not supported in the default hierarchy */
  157. static u16 cgrp_dfl_inhibit_ss_mask;
  158. /* some controllers are implicitly enabled on the default hierarchy */
  159. static u16 cgrp_dfl_implicit_ss_mask;
  160. /* some controllers can be threaded on the default hierarchy */
  161. static u16 cgrp_dfl_threaded_ss_mask;
  162. /* The list of hierarchy roots */
  163. LIST_HEAD(cgroup_roots);
  164. static int cgroup_root_count;
  165. /* hierarchy ID allocation and mapping, protected by cgroup_mutex */
  166. static DEFINE_IDR(cgroup_hierarchy_idr);
  167. /*
  168. * Assign a monotonically increasing serial number to csses. It guarantees
  169. * cgroups with bigger numbers are newer than those with smaller numbers.
  170. * Also, as csses are always appended to the parent's ->children list, it
  171. * guarantees that sibling csses are always sorted in the ascending serial
  172. * number order on the list. Protected by cgroup_mutex.
  173. */
  174. static u64 css_serial_nr_next = 1;
  175. /*
  176. * These bitmasks identify subsystems with specific features to avoid
  177. * having to do iterative checks repeatedly.
  178. */
  179. static u16 have_fork_callback __read_mostly;
  180. static u16 have_exit_callback __read_mostly;
  181. static u16 have_release_callback __read_mostly;
  182. static u16 have_canfork_callback __read_mostly;
  183. /* cgroup namespace for init task */
  184. struct cgroup_namespace init_cgroup_ns = {
  185. .ns.count = REFCOUNT_INIT(2),
  186. .user_ns = &init_user_ns,
  187. .ns.ops = &cgroupns_operations,
  188. .ns.inum = PROC_CGROUP_INIT_INO,
  189. .root_cset = &init_css_set,
  190. };
  191. static struct file_system_type cgroup2_fs_type;
  192. static struct cftype cgroup_base_files[];
  193. static struct cftype cgroup_psi_files[];
  194. /* cgroup optional features */
  195. enum cgroup_opt_features {
  196. #ifdef CONFIG_PSI
  197. OPT_FEATURE_PRESSURE,
  198. #endif
  199. OPT_FEATURE_COUNT
  200. };
  201. static const char *cgroup_opt_feature_names[OPT_FEATURE_COUNT] = {
  202. #ifdef CONFIG_PSI
  203. "pressure",
  204. #endif
  205. };
  206. static u16 cgroup_feature_disable_mask __read_mostly;
  207. static int cgroup_apply_control(struct cgroup *cgrp);
  208. static void cgroup_finalize_control(struct cgroup *cgrp, int ret);
  209. static void css_task_iter_skip(struct css_task_iter *it,
  210. struct task_struct *task);
  211. static int cgroup_destroy_locked(struct cgroup *cgrp);
  212. static struct cgroup_subsys_state *css_create(struct cgroup *cgrp,
  213. struct cgroup_subsys *ss);
  214. static void css_release(struct percpu_ref *ref);
  215. static void kill_css(struct cgroup_subsys_state *css);
  216. static int cgroup_addrm_files(struct cgroup_subsys_state *css,
  217. struct cgroup *cgrp, struct cftype cfts[],
  218. bool is_add);
  219. /**
  220. * cgroup_ssid_enabled - cgroup subsys enabled test by subsys ID
  221. * @ssid: subsys ID of interest
  222. *
  223. * cgroup_subsys_enabled() can only be used with literal subsys names which
  224. * is fine for individual subsystems but unsuitable for cgroup core. This
  225. * is slower static_key_enabled() based test indexed by @ssid.
  226. */
  227. bool cgroup_ssid_enabled(int ssid)
  228. {
  229. if (!CGROUP_HAS_SUBSYS_CONFIG)
  230. return false;
  231. return static_key_enabled(cgroup_subsys_enabled_key[ssid]);
  232. }
  233. /**
  234. * cgroup_on_dfl - test whether a cgroup is on the default hierarchy
  235. * @cgrp: the cgroup of interest
  236. *
  237. * The default hierarchy is the v2 interface of cgroup and this function
  238. * can be used to test whether a cgroup is on the default hierarchy for
  239. * cases where a subsystem should behave differently depending on the
  240. * interface version.
  241. *
  242. * List of changed behaviors:
  243. *
  244. * - Mount options "noprefix", "xattr", "clone_children", "release_agent"
  245. * and "name" are disallowed.
  246. *
  247. * - When mounting an existing superblock, mount options should match.
  248. *
  249. * - rename(2) is disallowed.
  250. *
  251. * - "tasks" is removed. Everything should be at process granularity. Use
  252. * "cgroup.procs" instead.
  253. *
  254. * - "cgroup.procs" is not sorted. pids will be unique unless they got
  255. * recycled in-between reads.
  256. *
  257. * - "release_agent" and "notify_on_release" are removed. Replacement
  258. * notification mechanism will be implemented.
  259. *
  260. * - "cgroup.clone_children" is removed.
  261. *
  262. * - "cgroup.subtree_populated" is available. Its value is 0 if the cgroup
  263. * and its descendants contain no task; otherwise, 1. The file also
  264. * generates kernfs notification which can be monitored through poll and
  265. * [di]notify when the value of the file changes.
  266. *
  267. * - cpuset: tasks will be kept in empty cpusets when hotplug happens and
  268. * take masks of ancestors with non-empty cpus/mems, instead of being
  269. * moved to an ancestor.
  270. *
  271. * - cpuset: a task can be moved into an empty cpuset, and again it takes
  272. * masks of ancestors.
  273. *
  274. * - blkcg: blk-throttle becomes properly hierarchical.
  275. *
  276. * - debug: disallowed on the default hierarchy.
  277. */
  278. bool cgroup_on_dfl(const struct cgroup *cgrp)
  279. {
  280. return cgrp->root == &cgrp_dfl_root;
  281. }
  282. /* IDR wrappers which synchronize using cgroup_idr_lock */
  283. static int cgroup_idr_alloc(struct idr *idr, void *ptr, int start, int end,
  284. gfp_t gfp_mask)
  285. {
  286. int ret;
  287. idr_preload(gfp_mask);
  288. spin_lock_bh(&cgroup_idr_lock);
  289. ret = idr_alloc(idr, ptr, start, end, gfp_mask & ~__GFP_DIRECT_RECLAIM);
  290. spin_unlock_bh(&cgroup_idr_lock);
  291. idr_preload_end();
  292. return ret;
  293. }
  294. static void *cgroup_idr_replace(struct idr *idr, void *ptr, int id)
  295. {
  296. void *ret;
  297. spin_lock_bh(&cgroup_idr_lock);
  298. ret = idr_replace(idr, ptr, id);
  299. spin_unlock_bh(&cgroup_idr_lock);
  300. return ret;
  301. }
  302. static void cgroup_idr_remove(struct idr *idr, int id)
  303. {
  304. spin_lock_bh(&cgroup_idr_lock);
  305. idr_remove(idr, id);
  306. spin_unlock_bh(&cgroup_idr_lock);
  307. }
  308. static bool cgroup_has_tasks(struct cgroup *cgrp)
  309. {
  310. return cgrp->nr_populated_csets;
  311. }
  312. bool cgroup_is_threaded(struct cgroup *cgrp)
  313. {
  314. return cgrp->dom_cgrp != cgrp;
  315. }
  316. /* can @cgrp host both domain and threaded children? */
  317. static bool cgroup_is_mixable(struct cgroup *cgrp)
  318. {
  319. /*
  320. * Root isn't under domain level resource control exempting it from
  321. * the no-internal-process constraint, so it can serve as a thread
  322. * root and a parent of resource domains at the same time.
  323. */
  324. return !cgroup_parent(cgrp);
  325. }
  326. /* can @cgrp become a thread root? Should always be true for a thread root */
  327. static bool cgroup_can_be_thread_root(struct cgroup *cgrp)
  328. {
  329. /* mixables don't care */
  330. if (cgroup_is_mixable(cgrp))
  331. return true;
  332. /* domain roots can't be nested under threaded */
  333. if (cgroup_is_threaded(cgrp))
  334. return false;
  335. /* can only have either domain or threaded children */
  336. if (cgrp->nr_populated_domain_children)
  337. return false;
  338. /* and no domain controllers can be enabled */
  339. if (cgrp->subtree_control & ~cgrp_dfl_threaded_ss_mask)
  340. return false;
  341. return true;
  342. }
  343. /* is @cgrp root of a threaded subtree? */
  344. bool cgroup_is_thread_root(struct cgroup *cgrp)
  345. {
  346. /* thread root should be a domain */
  347. if (cgroup_is_threaded(cgrp))
  348. return false;
  349. /* a domain w/ threaded children is a thread root */
  350. if (cgrp->nr_threaded_children)
  351. return true;
  352. /*
  353. * A domain which has tasks and explicit threaded controllers
  354. * enabled is a thread root.
  355. */
  356. if (cgroup_has_tasks(cgrp) &&
  357. (cgrp->subtree_control & cgrp_dfl_threaded_ss_mask))
  358. return true;
  359. return false;
  360. }
  361. /* a domain which isn't connected to the root w/o brekage can't be used */
  362. static bool cgroup_is_valid_domain(struct cgroup *cgrp)
  363. {
  364. /* the cgroup itself can be a thread root */
  365. if (cgroup_is_threaded(cgrp))
  366. return false;
  367. /* but the ancestors can't be unless mixable */
  368. while ((cgrp = cgroup_parent(cgrp))) {
  369. if (!cgroup_is_mixable(cgrp) && cgroup_is_thread_root(cgrp))
  370. return false;
  371. if (cgroup_is_threaded(cgrp))
  372. return false;
  373. }
  374. return true;
  375. }
  376. /* subsystems visibly enabled on a cgroup */
  377. static u16 cgroup_control(struct cgroup *cgrp)
  378. {
  379. struct cgroup *parent = cgroup_parent(cgrp);
  380. u16 root_ss_mask = cgrp->root->subsys_mask;
  381. if (parent) {
  382. u16 ss_mask = parent->subtree_control;
  383. /* threaded cgroups can only have threaded controllers */
  384. if (cgroup_is_threaded(cgrp))
  385. ss_mask &= cgrp_dfl_threaded_ss_mask;
  386. return ss_mask;
  387. }
  388. if (cgroup_on_dfl(cgrp))
  389. root_ss_mask &= ~(cgrp_dfl_inhibit_ss_mask |
  390. cgrp_dfl_implicit_ss_mask);
  391. return root_ss_mask;
  392. }
  393. /* subsystems enabled on a cgroup */
  394. static u16 cgroup_ss_mask(struct cgroup *cgrp)
  395. {
  396. struct cgroup *parent = cgroup_parent(cgrp);
  397. if (parent) {
  398. u16 ss_mask = parent->subtree_ss_mask;
  399. /* threaded cgroups can only have threaded controllers */
  400. if (cgroup_is_threaded(cgrp))
  401. ss_mask &= cgrp_dfl_threaded_ss_mask;
  402. return ss_mask;
  403. }
  404. return cgrp->root->subsys_mask;
  405. }
  406. /**
  407. * cgroup_css - obtain a cgroup's css for the specified subsystem
  408. * @cgrp: the cgroup of interest
  409. * @ss: the subsystem of interest (%NULL returns @cgrp->self)
  410. *
  411. * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This
  412. * function must be called either under cgroup_mutex or rcu_read_lock() and
  413. * the caller is responsible for pinning the returned css if it wants to
  414. * keep accessing it outside the said locks. This function may return
  415. * %NULL if @cgrp doesn't have @subsys_id enabled.
  416. */
  417. static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp,
  418. struct cgroup_subsys *ss)
  419. {
  420. if (CGROUP_HAS_SUBSYS_CONFIG && ss)
  421. return rcu_dereference_check(cgrp->subsys[ss->id],
  422. lockdep_is_held(&cgroup_mutex));
  423. else
  424. return &cgrp->self;
  425. }
  426. /**
  427. * cgroup_tryget_css - try to get a cgroup's css for the specified subsystem
  428. * @cgrp: the cgroup of interest
  429. * @ss: the subsystem of interest
  430. *
  431. * Find and get @cgrp's css associated with @ss. If the css doesn't exist
  432. * or is offline, %NULL is returned.
  433. */
  434. static struct cgroup_subsys_state *cgroup_tryget_css(struct cgroup *cgrp,
  435. struct cgroup_subsys *ss)
  436. {
  437. struct cgroup_subsys_state *css;
  438. rcu_read_lock();
  439. css = cgroup_css(cgrp, ss);
  440. if (css && !css_tryget_online(css))
  441. css = NULL;
  442. rcu_read_unlock();
  443. return css;
  444. }
  445. /**
  446. * cgroup_e_css_by_mask - obtain a cgroup's effective css for the specified ss
  447. * @cgrp: the cgroup of interest
  448. * @ss: the subsystem of interest (%NULL returns @cgrp->self)
  449. *
  450. * Similar to cgroup_css() but returns the effective css, which is defined
  451. * as the matching css of the nearest ancestor including self which has @ss
  452. * enabled. If @ss is associated with the hierarchy @cgrp is on, this
  453. * function is guaranteed to return non-NULL css.
  454. */
  455. static struct cgroup_subsys_state *cgroup_e_css_by_mask(struct cgroup *cgrp,
  456. struct cgroup_subsys *ss)
  457. {
  458. lockdep_assert_held(&cgroup_mutex);
  459. if (!ss)
  460. return &cgrp->self;
  461. /*
  462. * This function is used while updating css associations and thus
  463. * can't test the csses directly. Test ss_mask.
  464. */
  465. while (!(cgroup_ss_mask(cgrp) & (1 << ss->id))) {
  466. cgrp = cgroup_parent(cgrp);
  467. if (!cgrp)
  468. return NULL;
  469. }
  470. return cgroup_css(cgrp, ss);
  471. }
  472. /**
  473. * cgroup_e_css - obtain a cgroup's effective css for the specified subsystem
  474. * @cgrp: the cgroup of interest
  475. * @ss: the subsystem of interest
  476. *
  477. * Find and get the effective css of @cgrp for @ss. The effective css is
  478. * defined as the matching css of the nearest ancestor including self which
  479. * has @ss enabled. If @ss is not mounted on the hierarchy @cgrp is on,
  480. * the root css is returned, so this function always returns a valid css.
  481. *
  482. * The returned css is not guaranteed to be online, and therefore it is the
  483. * callers responsibility to try get a reference for it.
  484. */
  485. struct cgroup_subsys_state *cgroup_e_css(struct cgroup *cgrp,
  486. struct cgroup_subsys *ss)
  487. {
  488. struct cgroup_subsys_state *css;
  489. if (!CGROUP_HAS_SUBSYS_CONFIG)
  490. return NULL;
  491. do {
  492. css = cgroup_css(cgrp, ss);
  493. if (css)
  494. return css;
  495. cgrp = cgroup_parent(cgrp);
  496. } while (cgrp);
  497. return init_css_set.subsys[ss->id];
  498. }
  499. /**
  500. * cgroup_get_e_css - get a cgroup's effective css for the specified subsystem
  501. * @cgrp: the cgroup of interest
  502. * @ss: the subsystem of interest
  503. *
  504. * Find and get the effective css of @cgrp for @ss. The effective css is
  505. * defined as the matching css of the nearest ancestor including self which
  506. * has @ss enabled. If @ss is not mounted on the hierarchy @cgrp is on,
  507. * the root css is returned, so this function always returns a valid css.
  508. * The returned css must be put using css_put().
  509. */
  510. struct cgroup_subsys_state *cgroup_get_e_css(struct cgroup *cgrp,
  511. struct cgroup_subsys *ss)
  512. {
  513. struct cgroup_subsys_state *css;
  514. if (!CGROUP_HAS_SUBSYS_CONFIG)
  515. return NULL;
  516. rcu_read_lock();
  517. do {
  518. css = cgroup_css(cgrp, ss);
  519. if (css && css_tryget_online(css))
  520. goto out_unlock;
  521. cgrp = cgroup_parent(cgrp);
  522. } while (cgrp);
  523. css = init_css_set.subsys[ss->id];
  524. css_get(css);
  525. out_unlock:
  526. rcu_read_unlock();
  527. return css;
  528. }
  529. EXPORT_SYMBOL_GPL(cgroup_get_e_css);
  530. static void cgroup_get_live(struct cgroup *cgrp)
  531. {
  532. WARN_ON_ONCE(cgroup_is_dead(cgrp));
  533. css_get(&cgrp->self);
  534. }
  535. /**
  536. * __cgroup_task_count - count the number of tasks in a cgroup. The caller
  537. * is responsible for taking the css_set_lock.
  538. * @cgrp: the cgroup in question
  539. */
  540. int __cgroup_task_count(const struct cgroup *cgrp)
  541. {
  542. int count = 0;
  543. struct cgrp_cset_link *link;
  544. lockdep_assert_held(&css_set_lock);
  545. list_for_each_entry(link, &cgrp->cset_links, cset_link)
  546. count += link->cset->nr_tasks;
  547. return count;
  548. }
  549. /**
  550. * cgroup_task_count - count the number of tasks in a cgroup.
  551. * @cgrp: the cgroup in question
  552. */
  553. int cgroup_task_count(const struct cgroup *cgrp)
  554. {
  555. int count;
  556. spin_lock_irq(&css_set_lock);
  557. count = __cgroup_task_count(cgrp);
  558. spin_unlock_irq(&css_set_lock);
  559. return count;
  560. }
  561. struct cgroup_subsys_state *of_css(struct kernfs_open_file *of)
  562. {
  563. struct cgroup *cgrp = of->kn->parent->priv;
  564. struct cftype *cft = of_cft(of);
  565. /*
  566. * This is open and unprotected implementation of cgroup_css().
  567. * seq_css() is only called from a kernfs file operation which has
  568. * an active reference on the file. Because all the subsystem
  569. * files are drained before a css is disassociated with a cgroup,
  570. * the matching css from the cgroup's subsys table is guaranteed to
  571. * be and stay valid until the enclosing operation is complete.
  572. */
  573. if (CGROUP_HAS_SUBSYS_CONFIG && cft->ss)
  574. return rcu_dereference_raw(cgrp->subsys[cft->ss->id]);
  575. else
  576. return &cgrp->self;
  577. }
  578. EXPORT_SYMBOL_GPL(of_css);
  579. /**
  580. * for_each_css - iterate all css's of a cgroup
  581. * @css: the iteration cursor
  582. * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
  583. * @cgrp: the target cgroup to iterate css's of
  584. *
  585. * Should be called under cgroup_[tree_]mutex.
  586. */
  587. #define for_each_css(css, ssid, cgrp) \
  588. for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
  589. if (!((css) = rcu_dereference_check( \
  590. (cgrp)->subsys[(ssid)], \
  591. lockdep_is_held(&cgroup_mutex)))) { } \
  592. else
  593. /**
  594. * for_each_e_css - iterate all effective css's of a cgroup
  595. * @css: the iteration cursor
  596. * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
  597. * @cgrp: the target cgroup to iterate css's of
  598. *
  599. * Should be called under cgroup_[tree_]mutex.
  600. */
  601. #define for_each_e_css(css, ssid, cgrp) \
  602. for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
  603. if (!((css) = cgroup_e_css_by_mask(cgrp, \
  604. cgroup_subsys[(ssid)]))) \
  605. ; \
  606. else
  607. /**
  608. * do_each_subsys_mask - filter for_each_subsys with a bitmask
  609. * @ss: the iteration cursor
  610. * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
  611. * @ss_mask: the bitmask
  612. *
  613. * The block will only run for cases where the ssid-th bit (1 << ssid) of
  614. * @ss_mask is set.
  615. */
  616. #define do_each_subsys_mask(ss, ssid, ss_mask) do { \
  617. unsigned long __ss_mask = (ss_mask); \
  618. if (!CGROUP_HAS_SUBSYS_CONFIG) { \
  619. (ssid) = 0; \
  620. break; \
  621. } \
  622. for_each_set_bit(ssid, &__ss_mask, CGROUP_SUBSYS_COUNT) { \
  623. (ss) = cgroup_subsys[ssid]; \
  624. {
  625. #define while_each_subsys_mask() \
  626. } \
  627. } \
  628. } while (false)
  629. /* iterate over child cgrps, lock should be held throughout iteration */
  630. #define cgroup_for_each_live_child(child, cgrp) \
  631. list_for_each_entry((child), &(cgrp)->self.children, self.sibling) \
  632. if (({ lockdep_assert_held(&cgroup_mutex); \
  633. cgroup_is_dead(child); })) \
  634. ; \
  635. else
  636. /* walk live descendants in pre order */
  637. #define cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) \
  638. css_for_each_descendant_pre((d_css), cgroup_css((cgrp), NULL)) \
  639. if (({ lockdep_assert_held(&cgroup_mutex); \
  640. (dsct) = (d_css)->cgroup; \
  641. cgroup_is_dead(dsct); })) \
  642. ; \
  643. else
  644. /* walk live descendants in postorder */
  645. #define cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) \
  646. css_for_each_descendant_post((d_css), cgroup_css((cgrp), NULL)) \
  647. if (({ lockdep_assert_held(&cgroup_mutex); \
  648. (dsct) = (d_css)->cgroup; \
  649. cgroup_is_dead(dsct); })) \
  650. ; \
  651. else
  652. /*
  653. * The default css_set - used by init and its children prior to any
  654. * hierarchies being mounted. It contains a pointer to the root state
  655. * for each subsystem. Also used to anchor the list of css_sets. Not
  656. * reference-counted, to improve performance when child cgroups
  657. * haven't been created.
  658. */
  659. struct css_set init_css_set = {
  660. .refcount = REFCOUNT_INIT(1),
  661. .dom_cset = &init_css_set,
  662. .tasks = LIST_HEAD_INIT(init_css_set.tasks),
  663. .mg_tasks = LIST_HEAD_INIT(init_css_set.mg_tasks),
  664. .dying_tasks = LIST_HEAD_INIT(init_css_set.dying_tasks),
  665. .task_iters = LIST_HEAD_INIT(init_css_set.task_iters),
  666. .threaded_csets = LIST_HEAD_INIT(init_css_set.threaded_csets),
  667. .cgrp_links = LIST_HEAD_INIT(init_css_set.cgrp_links),
  668. .mg_src_preload_node = LIST_HEAD_INIT(init_css_set.mg_src_preload_node),
  669. .mg_dst_preload_node = LIST_HEAD_INIT(init_css_set.mg_dst_preload_node),
  670. .mg_node = LIST_HEAD_INIT(init_css_set.mg_node),
  671. /*
  672. * The following field is re-initialized when this cset gets linked
  673. * in cgroup_init(). However, let's initialize the field
  674. * statically too so that the default cgroup can be accessed safely
  675. * early during boot.
  676. */
  677. .dfl_cgrp = &cgrp_dfl_root.cgrp,
  678. };
  679. static int css_set_count = 1; /* 1 for init_css_set */
  680. static bool css_set_threaded(struct css_set *cset)
  681. {
  682. return cset->dom_cset != cset;
  683. }
  684. /**
  685. * css_set_populated - does a css_set contain any tasks?
  686. * @cset: target css_set
  687. *
  688. * css_set_populated() should be the same as !!cset->nr_tasks at steady
  689. * state. However, css_set_populated() can be called while a task is being
  690. * added to or removed from the linked list before the nr_tasks is
  691. * properly updated. Hence, we can't just look at ->nr_tasks here.
  692. */
  693. static bool css_set_populated(struct css_set *cset)
  694. {
  695. lockdep_assert_held(&css_set_lock);
  696. return !list_empty(&cset->tasks) || !list_empty(&cset->mg_tasks);
  697. }
  698. /**
  699. * cgroup_update_populated - update the populated count of a cgroup
  700. * @cgrp: the target cgroup
  701. * @populated: inc or dec populated count
  702. *
  703. * One of the css_sets associated with @cgrp is either getting its first
  704. * task or losing the last. Update @cgrp->nr_populated_* accordingly. The
  705. * count is propagated towards root so that a given cgroup's
  706. * nr_populated_children is zero iff none of its descendants contain any
  707. * tasks.
  708. *
  709. * @cgrp's interface file "cgroup.populated" is zero if both
  710. * @cgrp->nr_populated_csets and @cgrp->nr_populated_children are zero and
  711. * 1 otherwise. When the sum changes from or to zero, userland is notified
  712. * that the content of the interface file has changed. This can be used to
  713. * detect when @cgrp and its descendants become populated or empty.
  714. */
  715. static void cgroup_update_populated(struct cgroup *cgrp, bool populated)
  716. {
  717. struct cgroup *child = NULL;
  718. int adj = populated ? 1 : -1;
  719. lockdep_assert_held(&css_set_lock);
  720. do {
  721. bool was_populated = cgroup_is_populated(cgrp);
  722. if (!child) {
  723. cgrp->nr_populated_csets += adj;
  724. } else {
  725. if (cgroup_is_threaded(child))
  726. cgrp->nr_populated_threaded_children += adj;
  727. else
  728. cgrp->nr_populated_domain_children += adj;
  729. }
  730. if (was_populated == cgroup_is_populated(cgrp))
  731. break;
  732. cgroup1_check_for_release(cgrp);
  733. TRACE_CGROUP_PATH(notify_populated, cgrp,
  734. cgroup_is_populated(cgrp));
  735. cgroup_file_notify(&cgrp->events_file);
  736. child = cgrp;
  737. cgrp = cgroup_parent(cgrp);
  738. } while (cgrp);
  739. }
  740. /**
  741. * css_set_update_populated - update populated state of a css_set
  742. * @cset: target css_set
  743. * @populated: whether @cset is populated or depopulated
  744. *
  745. * @cset is either getting the first task or losing the last. Update the
  746. * populated counters of all associated cgroups accordingly.
  747. */
  748. static void css_set_update_populated(struct css_set *cset, bool populated)
  749. {
  750. struct cgrp_cset_link *link;
  751. lockdep_assert_held(&css_set_lock);
  752. list_for_each_entry(link, &cset->cgrp_links, cgrp_link)
  753. cgroup_update_populated(link->cgrp, populated);
  754. }
  755. /*
  756. * @task is leaving, advance task iterators which are pointing to it so
  757. * that they can resume at the next position. Advancing an iterator might
  758. * remove it from the list, use safe walk. See css_task_iter_skip() for
  759. * details.
  760. */
  761. static void css_set_skip_task_iters(struct css_set *cset,
  762. struct task_struct *task)
  763. {
  764. struct css_task_iter *it, *pos;
  765. list_for_each_entry_safe(it, pos, &cset->task_iters, iters_node)
  766. css_task_iter_skip(it, task);
  767. }
  768. /**
  769. * css_set_move_task - move a task from one css_set to another
  770. * @task: task being moved
  771. * @from_cset: css_set @task currently belongs to (may be NULL)
  772. * @to_cset: new css_set @task is being moved to (may be NULL)
  773. * @use_mg_tasks: move to @to_cset->mg_tasks instead of ->tasks
  774. *
  775. * Move @task from @from_cset to @to_cset. If @task didn't belong to any
  776. * css_set, @from_cset can be NULL. If @task is being disassociated
  777. * instead of moved, @to_cset can be NULL.
  778. *
  779. * This function automatically handles populated counter updates and
  780. * css_task_iter adjustments but the caller is responsible for managing
  781. * @from_cset and @to_cset's reference counts.
  782. */
  783. static void css_set_move_task(struct task_struct *task,
  784. struct css_set *from_cset, struct css_set *to_cset,
  785. bool use_mg_tasks)
  786. {
  787. lockdep_assert_held(&css_set_lock);
  788. if (to_cset && !css_set_populated(to_cset))
  789. css_set_update_populated(to_cset, true);
  790. if (from_cset) {
  791. WARN_ON_ONCE(list_empty(&task->cg_list));
  792. css_set_skip_task_iters(from_cset, task);
  793. list_del_init(&task->cg_list);
  794. if (!css_set_populated(from_cset))
  795. css_set_update_populated(from_cset, false);
  796. } else {
  797. WARN_ON_ONCE(!list_empty(&task->cg_list));
  798. }
  799. if (to_cset) {
  800. /*
  801. * We are synchronized through cgroup_threadgroup_rwsem
  802. * against PF_EXITING setting such that we can't race
  803. * against cgroup_exit()/cgroup_free() dropping the css_set.
  804. */
  805. WARN_ON_ONCE(task->flags & PF_EXITING);
  806. cgroup_move_task(task, to_cset);
  807. list_add_tail(&task->cg_list, use_mg_tasks ? &to_cset->mg_tasks :
  808. &to_cset->tasks);
  809. }
  810. }
  811. /*
  812. * hash table for cgroup groups. This improves the performance to find
  813. * an existing css_set. This hash doesn't (currently) take into
  814. * account cgroups in empty hierarchies.
  815. */
  816. #define CSS_SET_HASH_BITS 7
  817. static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
  818. static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
  819. {
  820. unsigned long key = 0UL;
  821. struct cgroup_subsys *ss;
  822. int i;
  823. for_each_subsys(ss, i)
  824. key += (unsigned long)css[i];
  825. key = (key >> 16) ^ key;
  826. return key;
  827. }
  828. void put_css_set_locked(struct css_set *cset)
  829. {
  830. struct cgrp_cset_link *link, *tmp_link;
  831. struct cgroup_subsys *ss;
  832. int ssid;
  833. lockdep_assert_held(&css_set_lock);
  834. if (!refcount_dec_and_test(&cset->refcount))
  835. return;
  836. WARN_ON_ONCE(!list_empty(&cset->threaded_csets));
  837. /* This css_set is dead. Unlink it and release cgroup and css refs */
  838. for_each_subsys(ss, ssid) {
  839. list_del(&cset->e_cset_node[ssid]);
  840. css_put(cset->subsys[ssid]);
  841. }
  842. hash_del(&cset->hlist);
  843. css_set_count--;
  844. list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
  845. list_del(&link->cset_link);
  846. list_del(&link->cgrp_link);
  847. if (cgroup_parent(link->cgrp))
  848. cgroup_put(link->cgrp);
  849. kfree(link);
  850. }
  851. if (css_set_threaded(cset)) {
  852. list_del(&cset->threaded_csets_node);
  853. put_css_set_locked(cset->dom_cset);
  854. }
  855. kfree_rcu(cset, rcu_head);
  856. }
  857. /**
  858. * compare_css_sets - helper function for find_existing_css_set().
  859. * @cset: candidate css_set being tested
  860. * @old_cset: existing css_set for a task
  861. * @new_cgrp: cgroup that's being entered by the task
  862. * @template: desired set of css pointers in css_set (pre-calculated)
  863. *
  864. * Returns true if "cset" matches "old_cset" except for the hierarchy
  865. * which "new_cgrp" belongs to, for which it should match "new_cgrp".
  866. */
  867. static bool compare_css_sets(struct css_set *cset,
  868. struct css_set *old_cset,
  869. struct cgroup *new_cgrp,
  870. struct cgroup_subsys_state *template[])
  871. {
  872. struct cgroup *new_dfl_cgrp;
  873. struct list_head *l1, *l2;
  874. /*
  875. * On the default hierarchy, there can be csets which are
  876. * associated with the same set of cgroups but different csses.
  877. * Let's first ensure that csses match.
  878. */
  879. if (memcmp(template, cset->subsys, sizeof(cset->subsys)))
  880. return false;
  881. /* @cset's domain should match the default cgroup's */
  882. if (cgroup_on_dfl(new_cgrp))
  883. new_dfl_cgrp = new_cgrp;
  884. else
  885. new_dfl_cgrp = old_cset->dfl_cgrp;
  886. if (new_dfl_cgrp->dom_cgrp != cset->dom_cset->dfl_cgrp)
  887. return false;
  888. /*
  889. * Compare cgroup pointers in order to distinguish between
  890. * different cgroups in hierarchies. As different cgroups may
  891. * share the same effective css, this comparison is always
  892. * necessary.
  893. */
  894. l1 = &cset->cgrp_links;
  895. l2 = &old_cset->cgrp_links;
  896. while (1) {
  897. struct cgrp_cset_link *link1, *link2;
  898. struct cgroup *cgrp1, *cgrp2;
  899. l1 = l1->next;
  900. l2 = l2->next;
  901. /* See if we reached the end - both lists are equal length. */
  902. if (l1 == &cset->cgrp_links) {
  903. BUG_ON(l2 != &old_cset->cgrp_links);
  904. break;
  905. } else {
  906. BUG_ON(l2 == &old_cset->cgrp_links);
  907. }
  908. /* Locate the cgroups associated with these links. */
  909. link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
  910. link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
  911. cgrp1 = link1->cgrp;
  912. cgrp2 = link2->cgrp;
  913. /* Hierarchies should be linked in the same order. */
  914. BUG_ON(cgrp1->root != cgrp2->root);
  915. /*
  916. * If this hierarchy is the hierarchy of the cgroup
  917. * that's changing, then we need to check that this
  918. * css_set points to the new cgroup; if it's any other
  919. * hierarchy, then this css_set should point to the
  920. * same cgroup as the old css_set.
  921. */
  922. if (cgrp1->root == new_cgrp->root) {
  923. if (cgrp1 != new_cgrp)
  924. return false;
  925. } else {
  926. if (cgrp1 != cgrp2)
  927. return false;
  928. }
  929. }
  930. return true;
  931. }
  932. /**
  933. * find_existing_css_set - init css array and find the matching css_set
  934. * @old_cset: the css_set that we're using before the cgroup transition
  935. * @cgrp: the cgroup that we're moving into
  936. * @template: out param for the new set of csses, should be clear on entry
  937. */
  938. static struct css_set *find_existing_css_set(struct css_set *old_cset,
  939. struct cgroup *cgrp,
  940. struct cgroup_subsys_state *template[])
  941. {
  942. struct cgroup_root *root = cgrp->root;
  943. struct cgroup_subsys *ss;
  944. struct css_set *cset;
  945. unsigned long key;
  946. int i;
  947. /*
  948. * Build the set of subsystem state objects that we want to see in the
  949. * new css_set. While subsystems can change globally, the entries here
  950. * won't change, so no need for locking.
  951. */
  952. for_each_subsys(ss, i) {
  953. if (root->subsys_mask & (1UL << i)) {
  954. /*
  955. * @ss is in this hierarchy, so we want the
  956. * effective css from @cgrp.
  957. */
  958. template[i] = cgroup_e_css_by_mask(cgrp, ss);
  959. } else {
  960. /*
  961. * @ss is not in this hierarchy, so we don't want
  962. * to change the css.
  963. */
  964. template[i] = old_cset->subsys[i];
  965. }
  966. }
  967. key = css_set_hash(template);
  968. hash_for_each_possible(css_set_table, cset, hlist, key) {
  969. if (!compare_css_sets(cset, old_cset, cgrp, template))
  970. continue;
  971. /* This css_set matches what we need */
  972. return cset;
  973. }
  974. /* No existing cgroup group matched */
  975. return NULL;
  976. }
  977. static void free_cgrp_cset_links(struct list_head *links_to_free)
  978. {
  979. struct cgrp_cset_link *link, *tmp_link;
  980. list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
  981. list_del(&link->cset_link);
  982. kfree(link);
  983. }
  984. }
  985. /**
  986. * allocate_cgrp_cset_links - allocate cgrp_cset_links
  987. * @count: the number of links to allocate
  988. * @tmp_links: list_head the allocated links are put on
  989. *
  990. * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
  991. * through ->cset_link. Returns 0 on success or -errno.
  992. */
  993. static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
  994. {
  995. struct cgrp_cset_link *link;
  996. int i;
  997. INIT_LIST_HEAD(tmp_links);
  998. for (i = 0; i < count; i++) {
  999. link = kzalloc(sizeof(*link), GFP_KERNEL);
  1000. if (!link) {
  1001. free_cgrp_cset_links(tmp_links);
  1002. return -ENOMEM;
  1003. }
  1004. list_add(&link->cset_link, tmp_links);
  1005. }
  1006. return 0;
  1007. }
  1008. /**
  1009. * link_css_set - a helper function to link a css_set to a cgroup
  1010. * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
  1011. * @cset: the css_set to be linked
  1012. * @cgrp: the destination cgroup
  1013. */
  1014. static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
  1015. struct cgroup *cgrp)
  1016. {
  1017. struct cgrp_cset_link *link;
  1018. BUG_ON(list_empty(tmp_links));
  1019. if (cgroup_on_dfl(cgrp))
  1020. cset->dfl_cgrp = cgrp;
  1021. link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
  1022. link->cset = cset;
  1023. link->cgrp = cgrp;
  1024. /*
  1025. * Always add links to the tail of the lists so that the lists are
  1026. * in chronological order.
  1027. */
  1028. list_move_tail(&link->cset_link, &cgrp->cset_links);
  1029. list_add_tail(&link->cgrp_link, &cset->cgrp_links);
  1030. if (cgroup_parent(cgrp))
  1031. cgroup_get_live(cgrp);
  1032. }
  1033. /**
  1034. * find_css_set - return a new css_set with one cgroup updated
  1035. * @old_cset: the baseline css_set
  1036. * @cgrp: the cgroup to be updated
  1037. *
  1038. * Return a new css_set that's equivalent to @old_cset, but with @cgrp
  1039. * substituted into the appropriate hierarchy.
  1040. */
  1041. static struct css_set *find_css_set(struct css_set *old_cset,
  1042. struct cgroup *cgrp)
  1043. {
  1044. struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { };
  1045. struct css_set *cset;
  1046. struct list_head tmp_links;
  1047. struct cgrp_cset_link *link;
  1048. struct cgroup_subsys *ss;
  1049. unsigned long key;
  1050. int ssid;
  1051. lockdep_assert_held(&cgroup_mutex);
  1052. /* First see if we already have a cgroup group that matches
  1053. * the desired set */
  1054. spin_lock_irq(&css_set_lock);
  1055. cset = find_existing_css_set(old_cset, cgrp, template);
  1056. if (cset)
  1057. get_css_set(cset);
  1058. spin_unlock_irq(&css_set_lock);
  1059. if (cset)
  1060. return cset;
  1061. cset = kzalloc(sizeof(*cset), GFP_KERNEL);
  1062. if (!cset)
  1063. return NULL;
  1064. /* Allocate all the cgrp_cset_link objects that we'll need */
  1065. if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) {
  1066. kfree(cset);
  1067. return NULL;
  1068. }
  1069. refcount_set(&cset->refcount, 1);
  1070. cset->dom_cset = cset;
  1071. INIT_LIST_HEAD(&cset->tasks);
  1072. INIT_LIST_HEAD(&cset->mg_tasks);
  1073. INIT_LIST_HEAD(&cset->dying_tasks);
  1074. INIT_LIST_HEAD(&cset->task_iters);
  1075. INIT_LIST_HEAD(&cset->threaded_csets);
  1076. INIT_HLIST_NODE(&cset->hlist);
  1077. INIT_LIST_HEAD(&cset->cgrp_links);
  1078. INIT_LIST_HEAD(&cset->mg_src_preload_node);
  1079. INIT_LIST_HEAD(&cset->mg_dst_preload_node);
  1080. INIT_LIST_HEAD(&cset->mg_node);
  1081. /* Copy the set of subsystem state objects generated in
  1082. * find_existing_css_set() */
  1083. memcpy(cset->subsys, template, sizeof(cset->subsys));
  1084. spin_lock_irq(&css_set_lock);
  1085. /* Add reference counts and links from the new css_set. */
  1086. list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
  1087. struct cgroup *c = link->cgrp;
  1088. if (c->root == cgrp->root)
  1089. c = cgrp;
  1090. link_css_set(&tmp_links, cset, c);
  1091. }
  1092. BUG_ON(!list_empty(&tmp_links));
  1093. css_set_count++;
  1094. /* Add @cset to the hash table */
  1095. key = css_set_hash(cset->subsys);
  1096. hash_add(css_set_table, &cset->hlist, key);
  1097. for_each_subsys(ss, ssid) {
  1098. struct cgroup_subsys_state *css = cset->subsys[ssid];
  1099. list_add_tail(&cset->e_cset_node[ssid],
  1100. &css->cgroup->e_csets[ssid]);
  1101. css_get(css);
  1102. }
  1103. spin_unlock_irq(&css_set_lock);
  1104. /*
  1105. * If @cset should be threaded, look up the matching dom_cset and
  1106. * link them up. We first fully initialize @cset then look for the
  1107. * dom_cset. It's simpler this way and safe as @cset is guaranteed
  1108. * to stay empty until we return.
  1109. */
  1110. if (cgroup_is_threaded(cset->dfl_cgrp)) {
  1111. struct css_set *dcset;
  1112. dcset = find_css_set(cset, cset->dfl_cgrp->dom_cgrp);
  1113. if (!dcset) {
  1114. put_css_set(cset);
  1115. return NULL;
  1116. }
  1117. spin_lock_irq(&css_set_lock);
  1118. cset->dom_cset = dcset;
  1119. list_add_tail(&cset->threaded_csets_node,
  1120. &dcset->threaded_csets);
  1121. spin_unlock_irq(&css_set_lock);
  1122. }
  1123. return cset;
  1124. }
  1125. struct cgroup_root *cgroup_root_from_kf(struct kernfs_root *kf_root)
  1126. {
  1127. struct cgroup *root_cgrp = kernfs_root_to_node(kf_root)->priv;
  1128. return root_cgrp->root;
  1129. }
  1130. void cgroup_favor_dynmods(struct cgroup_root *root, bool favor)
  1131. {
  1132. bool favoring = root->flags & CGRP_ROOT_FAVOR_DYNMODS;
  1133. /* see the comment above CGRP_ROOT_FAVOR_DYNMODS definition */
  1134. if (favor && !favoring) {
  1135. rcu_sync_enter(&cgroup_threadgroup_rwsem.rss);
  1136. root->flags |= CGRP_ROOT_FAVOR_DYNMODS;
  1137. } else if (!favor && favoring) {
  1138. rcu_sync_exit(&cgroup_threadgroup_rwsem.rss);
  1139. root->flags &= ~CGRP_ROOT_FAVOR_DYNMODS;
  1140. }
  1141. }
  1142. static int cgroup_init_root_id(struct cgroup_root *root)
  1143. {
  1144. int id;
  1145. lockdep_assert_held(&cgroup_mutex);
  1146. id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, 0, 0, GFP_KERNEL);
  1147. if (id < 0)
  1148. return id;
  1149. root->hierarchy_id = id;
  1150. return 0;
  1151. }
  1152. static void cgroup_exit_root_id(struct cgroup_root *root)
  1153. {
  1154. lockdep_assert_held(&cgroup_mutex);
  1155. idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
  1156. }
  1157. void cgroup_free_root(struct cgroup_root *root)
  1158. {
  1159. kfree(root);
  1160. }
  1161. static void cgroup_destroy_root(struct cgroup_root *root)
  1162. {
  1163. struct cgroup *cgrp = &root->cgrp;
  1164. struct cgrp_cset_link *link, *tmp_link;
  1165. trace_cgroup_destroy_root(root);
  1166. cgroup_lock_and_drain_offline(&cgrp_dfl_root.cgrp);
  1167. BUG_ON(atomic_read(&root->nr_cgrps));
  1168. BUG_ON(!list_empty(&cgrp->self.children));
  1169. /* Rebind all subsystems back to the default hierarchy */
  1170. WARN_ON(rebind_subsystems(&cgrp_dfl_root, root->subsys_mask));
  1171. /*
  1172. * Release all the links from cset_links to this hierarchy's
  1173. * root cgroup
  1174. */
  1175. spin_lock_irq(&css_set_lock);
  1176. list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
  1177. list_del(&link->cset_link);
  1178. list_del(&link->cgrp_link);
  1179. kfree(link);
  1180. }
  1181. spin_unlock_irq(&css_set_lock);
  1182. if (!list_empty(&root->root_list)) {
  1183. list_del(&root->root_list);
  1184. cgroup_root_count--;
  1185. }
  1186. cgroup_favor_dynmods(root, false);
  1187. cgroup_exit_root_id(root);
  1188. cgroup_unlock();
  1189. cgroup_rstat_exit(cgrp);
  1190. kernfs_destroy_root(root->kf_root);
  1191. cgroup_free_root(root);
  1192. }
  1193. /*
  1194. * Returned cgroup is without refcount but it's valid as long as cset pins it.
  1195. */
  1196. static inline struct cgroup *__cset_cgroup_from_root(struct css_set *cset,
  1197. struct cgroup_root *root)
  1198. {
  1199. struct cgroup *res_cgroup = NULL;
  1200. if (cset == &init_css_set) {
  1201. res_cgroup = &root->cgrp;
  1202. } else if (root == &cgrp_dfl_root) {
  1203. res_cgroup = cset->dfl_cgrp;
  1204. } else {
  1205. struct cgrp_cset_link *link;
  1206. lockdep_assert_held(&css_set_lock);
  1207. list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
  1208. struct cgroup *c = link->cgrp;
  1209. if (c->root == root) {
  1210. res_cgroup = c;
  1211. break;
  1212. }
  1213. }
  1214. }
  1215. BUG_ON(!res_cgroup);
  1216. return res_cgroup;
  1217. }
  1218. /*
  1219. * look up cgroup associated with current task's cgroup namespace on the
  1220. * specified hierarchy
  1221. */
  1222. static struct cgroup *
  1223. current_cgns_cgroup_from_root(struct cgroup_root *root)
  1224. {
  1225. struct cgroup *res = NULL;
  1226. struct css_set *cset;
  1227. lockdep_assert_held(&css_set_lock);
  1228. rcu_read_lock();
  1229. cset = current->nsproxy->cgroup_ns->root_cset;
  1230. res = __cset_cgroup_from_root(cset, root);
  1231. rcu_read_unlock();
  1232. return res;
  1233. }
  1234. /*
  1235. * Look up cgroup associated with current task's cgroup namespace on the default
  1236. * hierarchy.
  1237. *
  1238. * Unlike current_cgns_cgroup_from_root(), this doesn't need locks:
  1239. * - Internal rcu_read_lock is unnecessary because we don't dereference any rcu
  1240. * pointers.
  1241. * - css_set_lock is not needed because we just read cset->dfl_cgrp.
  1242. * - As a bonus returned cgrp is pinned with the current because it cannot
  1243. * switch cgroup_ns asynchronously.
  1244. */
  1245. static struct cgroup *current_cgns_cgroup_dfl(void)
  1246. {
  1247. struct css_set *cset;
  1248. cset = current->nsproxy->cgroup_ns->root_cset;
  1249. return __cset_cgroup_from_root(cset, &cgrp_dfl_root);
  1250. }
  1251. /* look up cgroup associated with given css_set on the specified hierarchy */
  1252. static struct cgroup *cset_cgroup_from_root(struct css_set *cset,
  1253. struct cgroup_root *root)
  1254. {
  1255. lockdep_assert_held(&cgroup_mutex);
  1256. lockdep_assert_held(&css_set_lock);
  1257. return __cset_cgroup_from_root(cset, root);
  1258. }
  1259. /*
  1260. * Return the cgroup for "task" from the given hierarchy. Must be
  1261. * called with cgroup_mutex and css_set_lock held.
  1262. */
  1263. struct cgroup *task_cgroup_from_root(struct task_struct *task,
  1264. struct cgroup_root *root)
  1265. {
  1266. /*
  1267. * No need to lock the task - since we hold css_set_lock the
  1268. * task can't change groups.
  1269. */
  1270. return cset_cgroup_from_root(task_css_set(task), root);
  1271. }
  1272. /*
  1273. * A task must hold cgroup_mutex to modify cgroups.
  1274. *
  1275. * Any task can increment and decrement the count field without lock.
  1276. * So in general, code holding cgroup_mutex can't rely on the count
  1277. * field not changing. However, if the count goes to zero, then only
  1278. * cgroup_attach_task() can increment it again. Because a count of zero
  1279. * means that no tasks are currently attached, therefore there is no
  1280. * way a task attached to that cgroup can fork (the other way to
  1281. * increment the count). So code holding cgroup_mutex can safely
  1282. * assume that if the count is zero, it will stay zero. Similarly, if
  1283. * a task holds cgroup_mutex on a cgroup with zero count, it
  1284. * knows that the cgroup won't be removed, as cgroup_rmdir()
  1285. * needs that mutex.
  1286. *
  1287. * A cgroup can only be deleted if both its 'count' of using tasks
  1288. * is zero, and its list of 'children' cgroups is empty. Since all
  1289. * tasks in the system use _some_ cgroup, and since there is always at
  1290. * least one task in the system (init, pid == 1), therefore, root cgroup
  1291. * always has either children cgroups and/or using tasks. So we don't
  1292. * need a special hack to ensure that root cgroup cannot be deleted.
  1293. *
  1294. * P.S. One more locking exception. RCU is used to guard the
  1295. * update of a tasks cgroup pointer by cgroup_attach_task()
  1296. */
  1297. static struct kernfs_syscall_ops cgroup_kf_syscall_ops;
  1298. static char *cgroup_file_name(struct cgroup *cgrp, const struct cftype *cft,
  1299. char *buf)
  1300. {
  1301. struct cgroup_subsys *ss = cft->ss;
  1302. if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) &&
  1303. !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) {
  1304. const char *dbg = (cft->flags & CFTYPE_DEBUG) ? ".__DEBUG__." : "";
  1305. snprintf(buf, CGROUP_FILE_NAME_MAX, "%s%s.%s",
  1306. dbg, cgroup_on_dfl(cgrp) ? ss->name : ss->legacy_name,
  1307. cft->name);
  1308. } else {
  1309. strscpy(buf, cft->name, CGROUP_FILE_NAME_MAX);
  1310. }
  1311. return buf;
  1312. }
  1313. /**
  1314. * cgroup_file_mode - deduce file mode of a control file
  1315. * @cft: the control file in question
  1316. *
  1317. * S_IRUGO for read, S_IWUSR for write.
  1318. */
  1319. static umode_t cgroup_file_mode(const struct cftype *cft)
  1320. {
  1321. umode_t mode = 0;
  1322. if (cft->read_u64 || cft->read_s64 || cft->seq_show)
  1323. mode |= S_IRUGO;
  1324. if (cft->write_u64 || cft->write_s64 || cft->write) {
  1325. if (cft->flags & CFTYPE_WORLD_WRITABLE)
  1326. mode |= S_IWUGO;
  1327. else
  1328. mode |= S_IWUSR;
  1329. }
  1330. return mode;
  1331. }
  1332. /**
  1333. * cgroup_calc_subtree_ss_mask - calculate subtree_ss_mask
  1334. * @subtree_control: the new subtree_control mask to consider
  1335. * @this_ss_mask: available subsystems
  1336. *
  1337. * On the default hierarchy, a subsystem may request other subsystems to be
  1338. * enabled together through its ->depends_on mask. In such cases, more
  1339. * subsystems than specified in "cgroup.subtree_control" may be enabled.
  1340. *
  1341. * This function calculates which subsystems need to be enabled if
  1342. * @subtree_control is to be applied while restricted to @this_ss_mask.
  1343. */
  1344. static u16 cgroup_calc_subtree_ss_mask(u16 subtree_control, u16 this_ss_mask)
  1345. {
  1346. u16 cur_ss_mask = subtree_control;
  1347. struct cgroup_subsys *ss;
  1348. int ssid;
  1349. lockdep_assert_held(&cgroup_mutex);
  1350. cur_ss_mask |= cgrp_dfl_implicit_ss_mask;
  1351. while (true) {
  1352. u16 new_ss_mask = cur_ss_mask;
  1353. do_each_subsys_mask(ss, ssid, cur_ss_mask) {
  1354. new_ss_mask |= ss->depends_on;
  1355. } while_each_subsys_mask();
  1356. /*
  1357. * Mask out subsystems which aren't available. This can
  1358. * happen only if some depended-upon subsystems were bound
  1359. * to non-default hierarchies.
  1360. */
  1361. new_ss_mask &= this_ss_mask;
  1362. if (new_ss_mask == cur_ss_mask)
  1363. break;
  1364. cur_ss_mask = new_ss_mask;
  1365. }
  1366. return cur_ss_mask;
  1367. }
  1368. /**
  1369. * cgroup_kn_unlock - unlocking helper for cgroup kernfs methods
  1370. * @kn: the kernfs_node being serviced
  1371. *
  1372. * This helper undoes cgroup_kn_lock_live() and should be invoked before
  1373. * the method finishes if locking succeeded. Note that once this function
  1374. * returns the cgroup returned by cgroup_kn_lock_live() may become
  1375. * inaccessible any time. If the caller intends to continue to access the
  1376. * cgroup, it should pin it before invoking this function.
  1377. */
  1378. void cgroup_kn_unlock(struct kernfs_node *kn)
  1379. {
  1380. struct cgroup *cgrp;
  1381. if (kernfs_type(kn) == KERNFS_DIR)
  1382. cgrp = kn->priv;
  1383. else
  1384. cgrp = kn->parent->priv;
  1385. cgroup_unlock();
  1386. kernfs_unbreak_active_protection(kn);
  1387. cgroup_put(cgrp);
  1388. }
  1389. /**
  1390. * cgroup_kn_lock_live - locking helper for cgroup kernfs methods
  1391. * @kn: the kernfs_node being serviced
  1392. * @drain_offline: perform offline draining on the cgroup
  1393. *
  1394. * This helper is to be used by a cgroup kernfs method currently servicing
  1395. * @kn. It breaks the active protection, performs cgroup locking and
  1396. * verifies that the associated cgroup is alive. Returns the cgroup if
  1397. * alive; otherwise, %NULL. A successful return should be undone by a
  1398. * matching cgroup_kn_unlock() invocation. If @drain_offline is %true, the
  1399. * cgroup is drained of offlining csses before return.
  1400. *
  1401. * Any cgroup kernfs method implementation which requires locking the
  1402. * associated cgroup should use this helper. It avoids nesting cgroup
  1403. * locking under kernfs active protection and allows all kernfs operations
  1404. * including self-removal.
  1405. */
  1406. struct cgroup *cgroup_kn_lock_live(struct kernfs_node *kn, bool drain_offline)
  1407. {
  1408. struct cgroup *cgrp;
  1409. if (kernfs_type(kn) == KERNFS_DIR)
  1410. cgrp = kn->priv;
  1411. else
  1412. cgrp = kn->parent->priv;
  1413. /*
  1414. * We're gonna grab cgroup_mutex which nests outside kernfs
  1415. * active_ref. cgroup liveliness check alone provides enough
  1416. * protection against removal. Ensure @cgrp stays accessible and
  1417. * break the active_ref protection.
  1418. */
  1419. if (!cgroup_tryget(cgrp))
  1420. return NULL;
  1421. kernfs_break_active_protection(kn);
  1422. if (drain_offline)
  1423. cgroup_lock_and_drain_offline(cgrp);
  1424. else
  1425. cgroup_lock();
  1426. if (!cgroup_is_dead(cgrp))
  1427. return cgrp;
  1428. cgroup_kn_unlock(kn);
  1429. return NULL;
  1430. }
  1431. static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
  1432. {
  1433. char name[CGROUP_FILE_NAME_MAX];
  1434. lockdep_assert_held(&cgroup_mutex);
  1435. if (cft->file_offset) {
  1436. struct cgroup_subsys_state *css = cgroup_css(cgrp, cft->ss);
  1437. struct cgroup_file *cfile = (void *)css + cft->file_offset;
  1438. spin_lock_irq(&cgroup_file_kn_lock);
  1439. cfile->kn = NULL;
  1440. spin_unlock_irq(&cgroup_file_kn_lock);
  1441. del_timer_sync(&cfile->notify_timer);
  1442. }
  1443. kernfs_remove_by_name(cgrp->kn, cgroup_file_name(cgrp, cft, name));
  1444. }
  1445. /**
  1446. * css_clear_dir - remove subsys files in a cgroup directory
  1447. * @css: target css
  1448. */
  1449. static void css_clear_dir(struct cgroup_subsys_state *css)
  1450. {
  1451. struct cgroup *cgrp = css->cgroup;
  1452. struct cftype *cfts;
  1453. if (!(css->flags & CSS_VISIBLE))
  1454. return;
  1455. css->flags &= ~CSS_VISIBLE;
  1456. if (!css->ss) {
  1457. if (cgroup_on_dfl(cgrp)) {
  1458. cgroup_addrm_files(css, cgrp,
  1459. cgroup_base_files, false);
  1460. if (cgroup_psi_enabled())
  1461. cgroup_addrm_files(css, cgrp,
  1462. cgroup_psi_files, false);
  1463. } else {
  1464. cgroup_addrm_files(css, cgrp,
  1465. cgroup1_base_files, false);
  1466. }
  1467. } else {
  1468. list_for_each_entry(cfts, &css->ss->cfts, node)
  1469. cgroup_addrm_files(css, cgrp, cfts, false);
  1470. }
  1471. }
  1472. /**
  1473. * css_populate_dir - create subsys files in a cgroup directory
  1474. * @css: target css
  1475. *
  1476. * On failure, no file is added.
  1477. */
  1478. static int css_populate_dir(struct cgroup_subsys_state *css)
  1479. {
  1480. struct cgroup *cgrp = css->cgroup;
  1481. struct cftype *cfts, *failed_cfts;
  1482. int ret;
  1483. if ((css->flags & CSS_VISIBLE) || !cgrp->kn)
  1484. return 0;
  1485. if (!css->ss) {
  1486. if (cgroup_on_dfl(cgrp)) {
  1487. ret = cgroup_addrm_files(&cgrp->self, cgrp,
  1488. cgroup_base_files, true);
  1489. if (ret < 0)
  1490. return ret;
  1491. if (cgroup_psi_enabled()) {
  1492. ret = cgroup_addrm_files(&cgrp->self, cgrp,
  1493. cgroup_psi_files, true);
  1494. if (ret < 0)
  1495. return ret;
  1496. }
  1497. } else {
  1498. cgroup_addrm_files(css, cgrp,
  1499. cgroup1_base_files, true);
  1500. }
  1501. } else {
  1502. list_for_each_entry(cfts, &css->ss->cfts, node) {
  1503. ret = cgroup_addrm_files(css, cgrp, cfts, true);
  1504. if (ret < 0) {
  1505. failed_cfts = cfts;
  1506. goto err;
  1507. }
  1508. }
  1509. }
  1510. css->flags |= CSS_VISIBLE;
  1511. return 0;
  1512. err:
  1513. list_for_each_entry(cfts, &css->ss->cfts, node) {
  1514. if (cfts == failed_cfts)
  1515. break;
  1516. cgroup_addrm_files(css, cgrp, cfts, false);
  1517. }
  1518. return ret;
  1519. }
  1520. int rebind_subsystems(struct cgroup_root *dst_root, u16 ss_mask)
  1521. {
  1522. struct cgroup *dcgrp = &dst_root->cgrp;
  1523. struct cgroup_subsys *ss;
  1524. int ssid, ret;
  1525. u16 dfl_disable_ss_mask = 0;
  1526. lockdep_assert_held(&cgroup_mutex);
  1527. do_each_subsys_mask(ss, ssid, ss_mask) {
  1528. /*
  1529. * If @ss has non-root csses attached to it, can't move.
  1530. * If @ss is an implicit controller, it is exempt from this
  1531. * rule and can be stolen.
  1532. */
  1533. if (css_next_child(NULL, cgroup_css(&ss->root->cgrp, ss)) &&
  1534. !ss->implicit_on_dfl)
  1535. return -EBUSY;
  1536. /* can't move between two non-dummy roots either */
  1537. if (ss->root != &cgrp_dfl_root && dst_root != &cgrp_dfl_root)
  1538. return -EBUSY;
  1539. /*
  1540. * Collect ssid's that need to be disabled from default
  1541. * hierarchy.
  1542. */
  1543. if (ss->root == &cgrp_dfl_root)
  1544. dfl_disable_ss_mask |= 1 << ssid;
  1545. } while_each_subsys_mask();
  1546. if (dfl_disable_ss_mask) {
  1547. struct cgroup *scgrp = &cgrp_dfl_root.cgrp;
  1548. /*
  1549. * Controllers from default hierarchy that need to be rebound
  1550. * are all disabled together in one go.
  1551. */
  1552. cgrp_dfl_root.subsys_mask &= ~dfl_disable_ss_mask;
  1553. WARN_ON(cgroup_apply_control(scgrp));
  1554. cgroup_finalize_control(scgrp, 0);
  1555. }
  1556. do_each_subsys_mask(ss, ssid, ss_mask) {
  1557. struct cgroup_root *src_root = ss->root;
  1558. struct cgroup *scgrp = &src_root->cgrp;
  1559. struct cgroup_subsys_state *css = cgroup_css(scgrp, ss);
  1560. struct css_set *cset, *cset_pos;
  1561. struct css_task_iter *it;
  1562. WARN_ON(!css || cgroup_css(dcgrp, ss));
  1563. if (src_root != &cgrp_dfl_root) {
  1564. /* disable from the source */
  1565. src_root->subsys_mask &= ~(1 << ssid);
  1566. WARN_ON(cgroup_apply_control(scgrp));
  1567. cgroup_finalize_control(scgrp, 0);
  1568. }
  1569. /* rebind */
  1570. RCU_INIT_POINTER(scgrp->subsys[ssid], NULL);
  1571. rcu_assign_pointer(dcgrp->subsys[ssid], css);
  1572. ss->root = dst_root;
  1573. css->cgroup = dcgrp;
  1574. spin_lock_irq(&css_set_lock);
  1575. WARN_ON(!list_empty(&dcgrp->e_csets[ss->id]));
  1576. list_for_each_entry_safe(cset, cset_pos, &scgrp->e_csets[ss->id],
  1577. e_cset_node[ss->id]) {
  1578. list_move_tail(&cset->e_cset_node[ss->id],
  1579. &dcgrp->e_csets[ss->id]);
  1580. /*
  1581. * all css_sets of scgrp together in same order to dcgrp,
  1582. * patch in-flight iterators to preserve correct iteration.
  1583. * since the iterator is always advanced right away and
  1584. * finished when it->cset_pos meets it->cset_head, so only
  1585. * update it->cset_head is enough here.
  1586. */
  1587. list_for_each_entry(it, &cset->task_iters, iters_node)
  1588. if (it->cset_head == &scgrp->e_csets[ss->id])
  1589. it->cset_head = &dcgrp->e_csets[ss->id];
  1590. }
  1591. spin_unlock_irq(&css_set_lock);
  1592. if (ss->css_rstat_flush) {
  1593. list_del_rcu(&css->rstat_css_node);
  1594. synchronize_rcu();
  1595. list_add_rcu(&css->rstat_css_node,
  1596. &dcgrp->rstat_css_list);
  1597. }
  1598. /* default hierarchy doesn't enable controllers by default */
  1599. dst_root->subsys_mask |= 1 << ssid;
  1600. if (dst_root == &cgrp_dfl_root) {
  1601. static_branch_enable(cgroup_subsys_on_dfl_key[ssid]);
  1602. } else {
  1603. dcgrp->subtree_control |= 1 << ssid;
  1604. static_branch_disable(cgroup_subsys_on_dfl_key[ssid]);
  1605. }
  1606. ret = cgroup_apply_control(dcgrp);
  1607. if (ret)
  1608. pr_warn("partial failure to rebind %s controller (err=%d)\n",
  1609. ss->name, ret);
  1610. if (ss->bind)
  1611. ss->bind(css);
  1612. } while_each_subsys_mask();
  1613. kernfs_activate(dcgrp->kn);
  1614. return 0;
  1615. }
  1616. int cgroup_show_path(struct seq_file *sf, struct kernfs_node *kf_node,
  1617. struct kernfs_root *kf_root)
  1618. {
  1619. int len = 0;
  1620. char *buf = NULL;
  1621. struct cgroup_root *kf_cgroot = cgroup_root_from_kf(kf_root);
  1622. struct cgroup *ns_cgroup;
  1623. buf = kmalloc(PATH_MAX, GFP_KERNEL);
  1624. if (!buf)
  1625. return -ENOMEM;
  1626. spin_lock_irq(&css_set_lock);
  1627. ns_cgroup = current_cgns_cgroup_from_root(kf_cgroot);
  1628. len = kernfs_path_from_node(kf_node, ns_cgroup->kn, buf, PATH_MAX);
  1629. spin_unlock_irq(&css_set_lock);
  1630. if (len >= PATH_MAX)
  1631. len = -ERANGE;
  1632. else if (len > 0) {
  1633. seq_escape(sf, buf, " \t\n\\");
  1634. len = 0;
  1635. }
  1636. kfree(buf);
  1637. return len;
  1638. }
  1639. enum cgroup2_param {
  1640. Opt_nsdelegate,
  1641. Opt_favordynmods,
  1642. Opt_memory_localevents,
  1643. Opt_memory_recursiveprot,
  1644. nr__cgroup2_params
  1645. };
  1646. static const struct fs_parameter_spec cgroup2_fs_parameters[] = {
  1647. fsparam_flag("nsdelegate", Opt_nsdelegate),
  1648. fsparam_flag("favordynmods", Opt_favordynmods),
  1649. fsparam_flag("memory_localevents", Opt_memory_localevents),
  1650. fsparam_flag("memory_recursiveprot", Opt_memory_recursiveprot),
  1651. {}
  1652. };
  1653. static int cgroup2_parse_param(struct fs_context *fc, struct fs_parameter *param)
  1654. {
  1655. struct cgroup_fs_context *ctx = cgroup_fc2context(fc);
  1656. struct fs_parse_result result;
  1657. int opt;
  1658. opt = fs_parse(fc, cgroup2_fs_parameters, param, &result);
  1659. if (opt < 0)
  1660. return opt;
  1661. switch (opt) {
  1662. case Opt_nsdelegate:
  1663. ctx->flags |= CGRP_ROOT_NS_DELEGATE;
  1664. return 0;
  1665. case Opt_favordynmods:
  1666. ctx->flags |= CGRP_ROOT_FAVOR_DYNMODS;
  1667. return 0;
  1668. case Opt_memory_localevents:
  1669. ctx->flags |= CGRP_ROOT_MEMORY_LOCAL_EVENTS;
  1670. return 0;
  1671. case Opt_memory_recursiveprot:
  1672. ctx->flags |= CGRP_ROOT_MEMORY_RECURSIVE_PROT;
  1673. return 0;
  1674. }
  1675. return -EINVAL;
  1676. }
  1677. static void apply_cgroup_root_flags(unsigned int root_flags)
  1678. {
  1679. if (current->nsproxy->cgroup_ns == &init_cgroup_ns) {
  1680. if (root_flags & CGRP_ROOT_NS_DELEGATE)
  1681. cgrp_dfl_root.flags |= CGRP_ROOT_NS_DELEGATE;
  1682. else
  1683. cgrp_dfl_root.flags &= ~CGRP_ROOT_NS_DELEGATE;
  1684. cgroup_favor_dynmods(&cgrp_dfl_root,
  1685. root_flags & CGRP_ROOT_FAVOR_DYNMODS);
  1686. if (root_flags & CGRP_ROOT_MEMORY_LOCAL_EVENTS)
  1687. cgrp_dfl_root.flags |= CGRP_ROOT_MEMORY_LOCAL_EVENTS;
  1688. else
  1689. cgrp_dfl_root.flags &= ~CGRP_ROOT_MEMORY_LOCAL_EVENTS;
  1690. if (root_flags & CGRP_ROOT_MEMORY_RECURSIVE_PROT)
  1691. cgrp_dfl_root.flags |= CGRP_ROOT_MEMORY_RECURSIVE_PROT;
  1692. else
  1693. cgrp_dfl_root.flags &= ~CGRP_ROOT_MEMORY_RECURSIVE_PROT;
  1694. }
  1695. }
  1696. static int cgroup_show_options(struct seq_file *seq, struct kernfs_root *kf_root)
  1697. {
  1698. if (cgrp_dfl_root.flags & CGRP_ROOT_NS_DELEGATE)
  1699. seq_puts(seq, ",nsdelegate");
  1700. if (cgrp_dfl_root.flags & CGRP_ROOT_FAVOR_DYNMODS)
  1701. seq_puts(seq, ",favordynmods");
  1702. if (cgrp_dfl_root.flags & CGRP_ROOT_MEMORY_LOCAL_EVENTS)
  1703. seq_puts(seq, ",memory_localevents");
  1704. if (cgrp_dfl_root.flags & CGRP_ROOT_MEMORY_RECURSIVE_PROT)
  1705. seq_puts(seq, ",memory_recursiveprot");
  1706. return 0;
  1707. }
  1708. static int cgroup_reconfigure(struct fs_context *fc)
  1709. {
  1710. struct cgroup_fs_context *ctx = cgroup_fc2context(fc);
  1711. apply_cgroup_root_flags(ctx->flags);
  1712. return 0;
  1713. }
  1714. static void init_cgroup_housekeeping(struct cgroup *cgrp)
  1715. {
  1716. struct cgroup_subsys *ss;
  1717. int ssid;
  1718. INIT_LIST_HEAD(&cgrp->self.sibling);
  1719. INIT_LIST_HEAD(&cgrp->self.children);
  1720. INIT_LIST_HEAD(&cgrp->cset_links);
  1721. INIT_LIST_HEAD(&cgrp->pidlists);
  1722. mutex_init(&cgrp->pidlist_mutex);
  1723. cgrp->self.cgroup = cgrp;
  1724. cgrp->self.flags |= CSS_ONLINE;
  1725. cgrp->dom_cgrp = cgrp;
  1726. cgrp->max_descendants = INT_MAX;
  1727. cgrp->max_depth = INT_MAX;
  1728. INIT_LIST_HEAD(&cgrp->rstat_css_list);
  1729. prev_cputime_init(&cgrp->prev_cputime);
  1730. for_each_subsys(ss, ssid)
  1731. INIT_LIST_HEAD(&cgrp->e_csets[ssid]);
  1732. init_waitqueue_head(&cgrp->offline_waitq);
  1733. INIT_WORK(&cgrp->release_agent_work, cgroup1_release_agent);
  1734. }
  1735. void init_cgroup_root(struct cgroup_fs_context *ctx)
  1736. {
  1737. struct cgroup_root *root = ctx->root;
  1738. struct cgroup *cgrp = &root->cgrp;
  1739. INIT_LIST_HEAD(&root->root_list);
  1740. atomic_set(&root->nr_cgrps, 1);
  1741. cgrp->root = root;
  1742. init_cgroup_housekeeping(cgrp);
  1743. /* DYNMODS must be modified through cgroup_favor_dynmods() */
  1744. root->flags = ctx->flags & ~CGRP_ROOT_FAVOR_DYNMODS;
  1745. if (ctx->release_agent)
  1746. strscpy(root->release_agent_path, ctx->release_agent, PATH_MAX);
  1747. if (ctx->name)
  1748. strscpy(root->name, ctx->name, MAX_CGROUP_ROOT_NAMELEN);
  1749. if (ctx->cpuset_clone_children)
  1750. set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags);
  1751. }
  1752. int cgroup_setup_root(struct cgroup_root *root, u16 ss_mask)
  1753. {
  1754. LIST_HEAD(tmp_links);
  1755. struct cgroup *root_cgrp = &root->cgrp;
  1756. struct kernfs_syscall_ops *kf_sops;
  1757. struct css_set *cset;
  1758. int i, ret;
  1759. lockdep_assert_held(&cgroup_mutex);
  1760. ret = percpu_ref_init(&root_cgrp->self.refcnt, css_release,
  1761. 0, GFP_KERNEL);
  1762. if (ret)
  1763. goto out;
  1764. /*
  1765. * We're accessing css_set_count without locking css_set_lock here,
  1766. * but that's OK - it can only be increased by someone holding
  1767. * cgroup_lock, and that's us. Later rebinding may disable
  1768. * controllers on the default hierarchy and thus create new csets,
  1769. * which can't be more than the existing ones. Allocate 2x.
  1770. */
  1771. ret = allocate_cgrp_cset_links(2 * css_set_count, &tmp_links);
  1772. if (ret)
  1773. goto cancel_ref;
  1774. ret = cgroup_init_root_id(root);
  1775. if (ret)
  1776. goto cancel_ref;
  1777. kf_sops = root == &cgrp_dfl_root ?
  1778. &cgroup_kf_syscall_ops : &cgroup1_kf_syscall_ops;
  1779. root->kf_root = kernfs_create_root(kf_sops,
  1780. KERNFS_ROOT_CREATE_DEACTIVATED |
  1781. KERNFS_ROOT_SUPPORT_EXPORTOP |
  1782. KERNFS_ROOT_SUPPORT_USER_XATTR,
  1783. root_cgrp);
  1784. if (IS_ERR(root->kf_root)) {
  1785. ret = PTR_ERR(root->kf_root);
  1786. goto exit_root_id;
  1787. }
  1788. root_cgrp->kn = kernfs_root_to_node(root->kf_root);
  1789. WARN_ON_ONCE(cgroup_ino(root_cgrp) != 1);
  1790. root_cgrp->ancestors[0] = root_cgrp;
  1791. ret = css_populate_dir(&root_cgrp->self);
  1792. if (ret)
  1793. goto destroy_root;
  1794. ret = cgroup_rstat_init(root_cgrp);
  1795. if (ret)
  1796. goto destroy_root;
  1797. ret = rebind_subsystems(root, ss_mask);
  1798. if (ret)
  1799. goto exit_stats;
  1800. ret = cgroup_bpf_inherit(root_cgrp);
  1801. WARN_ON_ONCE(ret);
  1802. trace_cgroup_setup_root(root);
  1803. /*
  1804. * There must be no failure case after here, since rebinding takes
  1805. * care of subsystems' refcounts, which are explicitly dropped in
  1806. * the failure exit path.
  1807. */
  1808. list_add(&root->root_list, &cgroup_roots);
  1809. cgroup_root_count++;
  1810. /*
  1811. * Link the root cgroup in this hierarchy into all the css_set
  1812. * objects.
  1813. */
  1814. spin_lock_irq(&css_set_lock);
  1815. hash_for_each(css_set_table, i, cset, hlist) {
  1816. link_css_set(&tmp_links, cset, root_cgrp);
  1817. if (css_set_populated(cset))
  1818. cgroup_update_populated(root_cgrp, true);
  1819. }
  1820. spin_unlock_irq(&css_set_lock);
  1821. BUG_ON(!list_empty(&root_cgrp->self.children));
  1822. BUG_ON(atomic_read(&root->nr_cgrps) != 1);
  1823. ret = 0;
  1824. goto out;
  1825. exit_stats:
  1826. cgroup_rstat_exit(root_cgrp);
  1827. destroy_root:
  1828. kernfs_destroy_root(root->kf_root);
  1829. root->kf_root = NULL;
  1830. exit_root_id:
  1831. cgroup_exit_root_id(root);
  1832. cancel_ref:
  1833. percpu_ref_exit(&root_cgrp->self.refcnt);
  1834. out:
  1835. free_cgrp_cset_links(&tmp_links);
  1836. return ret;
  1837. }
  1838. int cgroup_do_get_tree(struct fs_context *fc)
  1839. {
  1840. struct cgroup_fs_context *ctx = cgroup_fc2context(fc);
  1841. int ret;
  1842. ctx->kfc.root = ctx->root->kf_root;
  1843. if (fc->fs_type == &cgroup2_fs_type)
  1844. ctx->kfc.magic = CGROUP2_SUPER_MAGIC;
  1845. else
  1846. ctx->kfc.magic = CGROUP_SUPER_MAGIC;
  1847. ret = kernfs_get_tree(fc);
  1848. /*
  1849. * In non-init cgroup namespace, instead of root cgroup's dentry,
  1850. * we return the dentry corresponding to the cgroupns->root_cgrp.
  1851. */
  1852. if (!ret && ctx->ns != &init_cgroup_ns) {
  1853. struct dentry *nsdentry;
  1854. struct super_block *sb = fc->root->d_sb;
  1855. struct cgroup *cgrp;
  1856. cgroup_lock();
  1857. spin_lock_irq(&css_set_lock);
  1858. cgrp = cset_cgroup_from_root(ctx->ns->root_cset, ctx->root);
  1859. spin_unlock_irq(&css_set_lock);
  1860. cgroup_unlock();
  1861. nsdentry = kernfs_node_dentry(cgrp->kn, sb);
  1862. dput(fc->root);
  1863. if (IS_ERR(nsdentry)) {
  1864. deactivate_locked_super(sb);
  1865. ret = PTR_ERR(nsdentry);
  1866. nsdentry = NULL;
  1867. }
  1868. fc->root = nsdentry;
  1869. }
  1870. if (!ctx->kfc.new_sb_created)
  1871. cgroup_put(&ctx->root->cgrp);
  1872. return ret;
  1873. }
  1874. /*
  1875. * Destroy a cgroup filesystem context.
  1876. */
  1877. static void cgroup_fs_context_free(struct fs_context *fc)
  1878. {
  1879. struct cgroup_fs_context *ctx = cgroup_fc2context(fc);
  1880. kfree(ctx->name);
  1881. kfree(ctx->release_agent);
  1882. put_cgroup_ns(ctx->ns);
  1883. kernfs_free_fs_context(fc);
  1884. kfree(ctx);
  1885. }
  1886. static int cgroup_get_tree(struct fs_context *fc)
  1887. {
  1888. struct cgroup_fs_context *ctx = cgroup_fc2context(fc);
  1889. int ret;
  1890. WRITE_ONCE(cgrp_dfl_visible, true);
  1891. cgroup_get_live(&cgrp_dfl_root.cgrp);
  1892. ctx->root = &cgrp_dfl_root;
  1893. ret = cgroup_do_get_tree(fc);
  1894. if (!ret)
  1895. apply_cgroup_root_flags(ctx->flags);
  1896. return ret;
  1897. }
  1898. static const struct fs_context_operations cgroup_fs_context_ops = {
  1899. .free = cgroup_fs_context_free,
  1900. .parse_param = cgroup2_parse_param,
  1901. .get_tree = cgroup_get_tree,
  1902. .reconfigure = cgroup_reconfigure,
  1903. };
  1904. static const struct fs_context_operations cgroup1_fs_context_ops = {
  1905. .free = cgroup_fs_context_free,
  1906. .parse_param = cgroup1_parse_param,
  1907. .get_tree = cgroup1_get_tree,
  1908. .reconfigure = cgroup1_reconfigure,
  1909. };
  1910. /*
  1911. * Initialise the cgroup filesystem creation/reconfiguration context. Notably,
  1912. * we select the namespace we're going to use.
  1913. */
  1914. static int cgroup_init_fs_context(struct fs_context *fc)
  1915. {
  1916. struct cgroup_fs_context *ctx;
  1917. ctx = kzalloc(sizeof(struct cgroup_fs_context), GFP_KERNEL);
  1918. if (!ctx)
  1919. return -ENOMEM;
  1920. ctx->ns = current->nsproxy->cgroup_ns;
  1921. get_cgroup_ns(ctx->ns);
  1922. fc->fs_private = &ctx->kfc;
  1923. if (fc->fs_type == &cgroup2_fs_type)
  1924. fc->ops = &cgroup_fs_context_ops;
  1925. else
  1926. fc->ops = &cgroup1_fs_context_ops;
  1927. put_user_ns(fc->user_ns);
  1928. fc->user_ns = get_user_ns(ctx->ns->user_ns);
  1929. fc->global = true;
  1930. #ifdef CONFIG_CGROUP_FAVOR_DYNMODS
  1931. ctx->flags |= CGRP_ROOT_FAVOR_DYNMODS;
  1932. #endif
  1933. return 0;
  1934. }
  1935. static void cgroup_kill_sb(struct super_block *sb)
  1936. {
  1937. struct kernfs_root *kf_root = kernfs_root_from_sb(sb);
  1938. struct cgroup_root *root = cgroup_root_from_kf(kf_root);
  1939. /*
  1940. * If @root doesn't have any children, start killing it.
  1941. * This prevents new mounts by disabling percpu_ref_tryget_live().
  1942. *
  1943. * And don't kill the default root.
  1944. */
  1945. if (list_empty(&root->cgrp.self.children) && root != &cgrp_dfl_root &&
  1946. !percpu_ref_is_dying(&root->cgrp.self.refcnt)) {
  1947. cgroup_bpf_offline(&root->cgrp);
  1948. percpu_ref_kill(&root->cgrp.self.refcnt);
  1949. }
  1950. cgroup_put(&root->cgrp);
  1951. kernfs_kill_sb(sb);
  1952. }
  1953. struct file_system_type cgroup_fs_type = {
  1954. .name = "cgroup",
  1955. .init_fs_context = cgroup_init_fs_context,
  1956. .parameters = cgroup1_fs_parameters,
  1957. .kill_sb = cgroup_kill_sb,
  1958. .fs_flags = FS_USERNS_MOUNT,
  1959. };
  1960. static struct file_system_type cgroup2_fs_type = {
  1961. .name = "cgroup2",
  1962. .init_fs_context = cgroup_init_fs_context,
  1963. .parameters = cgroup2_fs_parameters,
  1964. .kill_sb = cgroup_kill_sb,
  1965. .fs_flags = FS_USERNS_MOUNT,
  1966. };
  1967. #ifdef CONFIG_CPUSETS
  1968. static const struct fs_context_operations cpuset_fs_context_ops = {
  1969. .get_tree = cgroup1_get_tree,
  1970. .free = cgroup_fs_context_free,
  1971. };
  1972. /*
  1973. * This is ugly, but preserves the userspace API for existing cpuset
  1974. * users. If someone tries to mount the "cpuset" filesystem, we
  1975. * silently switch it to mount "cgroup" instead
  1976. */
  1977. static int cpuset_init_fs_context(struct fs_context *fc)
  1978. {
  1979. char *agent = kstrdup("/sbin/cpuset_release_agent", GFP_USER);
  1980. struct cgroup_fs_context *ctx;
  1981. int err;
  1982. err = cgroup_init_fs_context(fc);
  1983. if (err) {
  1984. kfree(agent);
  1985. return err;
  1986. }
  1987. fc->ops = &cpuset_fs_context_ops;
  1988. ctx = cgroup_fc2context(fc);
  1989. ctx->subsys_mask = 1 << cpuset_cgrp_id;
  1990. ctx->flags |= CGRP_ROOT_NOPREFIX;
  1991. ctx->release_agent = agent;
  1992. get_filesystem(&cgroup_fs_type);
  1993. put_filesystem(fc->fs_type);
  1994. fc->fs_type = &cgroup_fs_type;
  1995. return 0;
  1996. }
  1997. static struct file_system_type cpuset_fs_type = {
  1998. .name = "cpuset",
  1999. .init_fs_context = cpuset_init_fs_context,
  2000. .fs_flags = FS_USERNS_MOUNT,
  2001. };
  2002. #endif
  2003. int cgroup_path_ns_locked(struct cgroup *cgrp, char *buf, size_t buflen,
  2004. struct cgroup_namespace *ns)
  2005. {
  2006. struct cgroup *root = cset_cgroup_from_root(ns->root_cset, cgrp->root);
  2007. return kernfs_path_from_node(cgrp->kn, root->kn, buf, buflen);
  2008. }
  2009. int cgroup_path_ns(struct cgroup *cgrp, char *buf, size_t buflen,
  2010. struct cgroup_namespace *ns)
  2011. {
  2012. int ret;
  2013. cgroup_lock();
  2014. spin_lock_irq(&css_set_lock);
  2015. ret = cgroup_path_ns_locked(cgrp, buf, buflen, ns);
  2016. spin_unlock_irq(&css_set_lock);
  2017. cgroup_unlock();
  2018. return ret;
  2019. }
  2020. EXPORT_SYMBOL_GPL(cgroup_path_ns);
  2021. /**
  2022. * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
  2023. * @task: target task
  2024. * @buf: the buffer to write the path into
  2025. * @buflen: the length of the buffer
  2026. *
  2027. * Determine @task's cgroup on the first (the one with the lowest non-zero
  2028. * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
  2029. * function grabs cgroup_mutex and shouldn't be used inside locks used by
  2030. * cgroup controller callbacks.
  2031. *
  2032. * Return value is the same as kernfs_path().
  2033. */
  2034. int task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
  2035. {
  2036. struct cgroup_root *root;
  2037. struct cgroup *cgrp;
  2038. int hierarchy_id = 1;
  2039. int ret;
  2040. cgroup_lock();
  2041. spin_lock_irq(&css_set_lock);
  2042. root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
  2043. if (root) {
  2044. cgrp = task_cgroup_from_root(task, root);
  2045. ret = cgroup_path_ns_locked(cgrp, buf, buflen, &init_cgroup_ns);
  2046. } else {
  2047. /* if no hierarchy exists, everyone is in "/" */
  2048. ret = strscpy(buf, "/", buflen);
  2049. }
  2050. spin_unlock_irq(&css_set_lock);
  2051. cgroup_unlock();
  2052. return ret;
  2053. }
  2054. EXPORT_SYMBOL_GPL(task_cgroup_path);
  2055. /**
  2056. * cgroup_attach_lock - Lock for ->attach()
  2057. * @lock_threadgroup: whether to down_write cgroup_threadgroup_rwsem
  2058. *
  2059. * cgroup migration sometimes needs to stabilize threadgroups against forks and
  2060. * exits by write-locking cgroup_threadgroup_rwsem. However, some ->attach()
  2061. * implementations (e.g. cpuset), also need to disable CPU hotplug.
  2062. * Unfortunately, letting ->attach() operations acquire cpus_read_lock() can
  2063. * lead to deadlocks.
  2064. *
  2065. * Bringing up a CPU may involve creating and destroying tasks which requires
  2066. * read-locking threadgroup_rwsem, so threadgroup_rwsem nests inside
  2067. * cpus_read_lock(). If we call an ->attach() which acquires the cpus lock while
  2068. * write-locking threadgroup_rwsem, the locking order is reversed and we end up
  2069. * waiting for an on-going CPU hotplug operation which in turn is waiting for
  2070. * the threadgroup_rwsem to be released to create new tasks. For more details:
  2071. *
  2072. * http://lkml.kernel.org/r/20220711174629.uehfmqegcwn2lqzu@wubuntu
  2073. *
  2074. * Resolve the situation by always acquiring cpus_read_lock() before optionally
  2075. * write-locking cgroup_threadgroup_rwsem. This allows ->attach() to assume that
  2076. * CPU hotplug is disabled on entry.
  2077. */
  2078. void cgroup_attach_lock(bool lock_threadgroup)
  2079. {
  2080. cpus_read_lock();
  2081. if (lock_threadgroup)
  2082. percpu_down_write(&cgroup_threadgroup_rwsem);
  2083. }
  2084. /**
  2085. * cgroup_attach_unlock - Undo cgroup_attach_lock()
  2086. * @lock_threadgroup: whether to up_write cgroup_threadgroup_rwsem
  2087. */
  2088. void cgroup_attach_unlock(bool lock_threadgroup)
  2089. {
  2090. if (lock_threadgroup)
  2091. percpu_up_write(&cgroup_threadgroup_rwsem);
  2092. cpus_read_unlock();
  2093. }
  2094. /**
  2095. * cgroup_migrate_add_task - add a migration target task to a migration context
  2096. * @task: target task
  2097. * @mgctx: target migration context
  2098. *
  2099. * Add @task, which is a migration target, to @mgctx->tset. This function
  2100. * becomes noop if @task doesn't need to be migrated. @task's css_set
  2101. * should have been added as a migration source and @task->cg_list will be
  2102. * moved from the css_set's tasks list to mg_tasks one.
  2103. */
  2104. static void cgroup_migrate_add_task(struct task_struct *task,
  2105. struct cgroup_mgctx *mgctx)
  2106. {
  2107. struct css_set *cset;
  2108. lockdep_assert_held(&css_set_lock);
  2109. /* @task either already exited or can't exit until the end */
  2110. if (task->flags & PF_EXITING)
  2111. return;
  2112. /* cgroup_threadgroup_rwsem protects racing against forks */
  2113. WARN_ON_ONCE(list_empty(&task->cg_list));
  2114. cset = task_css_set(task);
  2115. if (!cset->mg_src_cgrp)
  2116. return;
  2117. mgctx->tset.nr_tasks++;
  2118. list_move_tail(&task->cg_list, &cset->mg_tasks);
  2119. if (list_empty(&cset->mg_node))
  2120. list_add_tail(&cset->mg_node,
  2121. &mgctx->tset.src_csets);
  2122. if (list_empty(&cset->mg_dst_cset->mg_node))
  2123. list_add_tail(&cset->mg_dst_cset->mg_node,
  2124. &mgctx->tset.dst_csets);
  2125. }
  2126. /**
  2127. * cgroup_taskset_first - reset taskset and return the first task
  2128. * @tset: taskset of interest
  2129. * @dst_cssp: output variable for the destination css
  2130. *
  2131. * @tset iteration is initialized and the first task is returned.
  2132. */
  2133. struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset,
  2134. struct cgroup_subsys_state **dst_cssp)
  2135. {
  2136. tset->cur_cset = list_first_entry(tset->csets, struct css_set, mg_node);
  2137. tset->cur_task = NULL;
  2138. return cgroup_taskset_next(tset, dst_cssp);
  2139. }
  2140. EXPORT_SYMBOL_GPL(cgroup_taskset_first);
  2141. /**
  2142. * cgroup_taskset_next - iterate to the next task in taskset
  2143. * @tset: taskset of interest
  2144. * @dst_cssp: output variable for the destination css
  2145. *
  2146. * Return the next task in @tset. Iteration must have been initialized
  2147. * with cgroup_taskset_first().
  2148. */
  2149. struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset,
  2150. struct cgroup_subsys_state **dst_cssp)
  2151. {
  2152. struct css_set *cset = tset->cur_cset;
  2153. struct task_struct *task = tset->cur_task;
  2154. while (CGROUP_HAS_SUBSYS_CONFIG && &cset->mg_node != tset->csets) {
  2155. if (!task)
  2156. task = list_first_entry(&cset->mg_tasks,
  2157. struct task_struct, cg_list);
  2158. else
  2159. task = list_next_entry(task, cg_list);
  2160. if (&task->cg_list != &cset->mg_tasks) {
  2161. tset->cur_cset = cset;
  2162. tset->cur_task = task;
  2163. /*
  2164. * This function may be called both before and
  2165. * after cgroup_taskset_migrate(). The two cases
  2166. * can be distinguished by looking at whether @cset
  2167. * has its ->mg_dst_cset set.
  2168. */
  2169. if (cset->mg_dst_cset)
  2170. *dst_cssp = cset->mg_dst_cset->subsys[tset->ssid];
  2171. else
  2172. *dst_cssp = cset->subsys[tset->ssid];
  2173. return task;
  2174. }
  2175. cset = list_next_entry(cset, mg_node);
  2176. task = NULL;
  2177. }
  2178. return NULL;
  2179. }
  2180. EXPORT_SYMBOL_GPL(cgroup_taskset_next);
  2181. /**
  2182. * cgroup_migrate_execute - migrate a taskset
  2183. * @mgctx: migration context
  2184. *
  2185. * Migrate tasks in @mgctx as setup by migration preparation functions.
  2186. * This function fails iff one of the ->can_attach callbacks fails and
  2187. * guarantees that either all or none of the tasks in @mgctx are migrated.
  2188. * @mgctx is consumed regardless of success.
  2189. */
  2190. static int cgroup_migrate_execute(struct cgroup_mgctx *mgctx)
  2191. {
  2192. struct cgroup_taskset *tset = &mgctx->tset;
  2193. struct cgroup_subsys *ss;
  2194. struct task_struct *task, *tmp_task;
  2195. struct css_set *cset, *tmp_cset;
  2196. int ssid, failed_ssid, ret;
  2197. /* check that we can legitimately attach to the cgroup */
  2198. if (tset->nr_tasks) {
  2199. do_each_subsys_mask(ss, ssid, mgctx->ss_mask) {
  2200. if (ss->can_attach) {
  2201. tset->ssid = ssid;
  2202. ret = ss->can_attach(tset);
  2203. if (ret) {
  2204. failed_ssid = ssid;
  2205. goto out_cancel_attach;
  2206. }
  2207. }
  2208. } while_each_subsys_mask();
  2209. }
  2210. /*
  2211. * Now that we're guaranteed success, proceed to move all tasks to
  2212. * the new cgroup. There are no failure cases after here, so this
  2213. * is the commit point.
  2214. */
  2215. spin_lock_irq(&css_set_lock);
  2216. list_for_each_entry(cset, &tset->src_csets, mg_node) {
  2217. list_for_each_entry_safe(task, tmp_task, &cset->mg_tasks, cg_list) {
  2218. struct css_set *from_cset = task_css_set(task);
  2219. struct css_set *to_cset = cset->mg_dst_cset;
  2220. get_css_set(to_cset);
  2221. to_cset->nr_tasks++;
  2222. css_set_move_task(task, from_cset, to_cset, true);
  2223. from_cset->nr_tasks--;
  2224. /*
  2225. * If the source or destination cgroup is frozen,
  2226. * the task might require to change its state.
  2227. */
  2228. cgroup_freezer_migrate_task(task, from_cset->dfl_cgrp,
  2229. to_cset->dfl_cgrp);
  2230. put_css_set_locked(from_cset);
  2231. }
  2232. }
  2233. spin_unlock_irq(&css_set_lock);
  2234. /*
  2235. * Migration is committed, all target tasks are now on dst_csets.
  2236. * Nothing is sensitive to fork() after this point. Notify
  2237. * controllers that migration is complete.
  2238. */
  2239. tset->csets = &tset->dst_csets;
  2240. if (tset->nr_tasks) {
  2241. do_each_subsys_mask(ss, ssid, mgctx->ss_mask) {
  2242. if (ss->attach) {
  2243. tset->ssid = ssid;
  2244. trace_android_vh_cgroup_attach(ss, tset);
  2245. ss->attach(tset);
  2246. }
  2247. } while_each_subsys_mask();
  2248. }
  2249. ret = 0;
  2250. goto out_release_tset;
  2251. out_cancel_attach:
  2252. if (tset->nr_tasks) {
  2253. do_each_subsys_mask(ss, ssid, mgctx->ss_mask) {
  2254. if (ssid == failed_ssid)
  2255. break;
  2256. if (ss->cancel_attach) {
  2257. tset->ssid = ssid;
  2258. ss->cancel_attach(tset);
  2259. }
  2260. } while_each_subsys_mask();
  2261. }
  2262. out_release_tset:
  2263. spin_lock_irq(&css_set_lock);
  2264. list_splice_init(&tset->dst_csets, &tset->src_csets);
  2265. list_for_each_entry_safe(cset, tmp_cset, &tset->src_csets, mg_node) {
  2266. list_splice_tail_init(&cset->mg_tasks, &cset->tasks);
  2267. list_del_init(&cset->mg_node);
  2268. }
  2269. spin_unlock_irq(&css_set_lock);
  2270. /*
  2271. * Re-initialize the cgroup_taskset structure in case it is reused
  2272. * again in another cgroup_migrate_add_task()/cgroup_migrate_execute()
  2273. * iteration.
  2274. */
  2275. tset->nr_tasks = 0;
  2276. tset->csets = &tset->src_csets;
  2277. return ret;
  2278. }
  2279. /**
  2280. * cgroup_migrate_vet_dst - verify whether a cgroup can be migration destination
  2281. * @dst_cgrp: destination cgroup to test
  2282. *
  2283. * On the default hierarchy, except for the mixable, (possible) thread root
  2284. * and threaded cgroups, subtree_control must be zero for migration
  2285. * destination cgroups with tasks so that child cgroups don't compete
  2286. * against tasks.
  2287. */
  2288. int cgroup_migrate_vet_dst(struct cgroup *dst_cgrp)
  2289. {
  2290. /* v1 doesn't have any restriction */
  2291. if (!cgroup_on_dfl(dst_cgrp))
  2292. return 0;
  2293. /* verify @dst_cgrp can host resources */
  2294. if (!cgroup_is_valid_domain(dst_cgrp->dom_cgrp))
  2295. return -EOPNOTSUPP;
  2296. /*
  2297. * If @dst_cgrp is already or can become a thread root or is
  2298. * threaded, it doesn't matter.
  2299. */
  2300. if (cgroup_can_be_thread_root(dst_cgrp) || cgroup_is_threaded(dst_cgrp))
  2301. return 0;
  2302. /* apply no-internal-process constraint */
  2303. if (dst_cgrp->subtree_control)
  2304. return -EBUSY;
  2305. return 0;
  2306. }
  2307. /**
  2308. * cgroup_migrate_finish - cleanup after attach
  2309. * @mgctx: migration context
  2310. *
  2311. * Undo cgroup_migrate_add_src() and cgroup_migrate_prepare_dst(). See
  2312. * those functions for details.
  2313. */
  2314. void cgroup_migrate_finish(struct cgroup_mgctx *mgctx)
  2315. {
  2316. struct css_set *cset, *tmp_cset;
  2317. lockdep_assert_held(&cgroup_mutex);
  2318. spin_lock_irq(&css_set_lock);
  2319. list_for_each_entry_safe(cset, tmp_cset, &mgctx->preloaded_src_csets,
  2320. mg_src_preload_node) {
  2321. cset->mg_src_cgrp = NULL;
  2322. cset->mg_dst_cgrp = NULL;
  2323. cset->mg_dst_cset = NULL;
  2324. list_del_init(&cset->mg_src_preload_node);
  2325. put_css_set_locked(cset);
  2326. }
  2327. list_for_each_entry_safe(cset, tmp_cset, &mgctx->preloaded_dst_csets,
  2328. mg_dst_preload_node) {
  2329. cset->mg_src_cgrp = NULL;
  2330. cset->mg_dst_cgrp = NULL;
  2331. cset->mg_dst_cset = NULL;
  2332. list_del_init(&cset->mg_dst_preload_node);
  2333. put_css_set_locked(cset);
  2334. }
  2335. spin_unlock_irq(&css_set_lock);
  2336. }
  2337. /**
  2338. * cgroup_migrate_add_src - add a migration source css_set
  2339. * @src_cset: the source css_set to add
  2340. * @dst_cgrp: the destination cgroup
  2341. * @mgctx: migration context
  2342. *
  2343. * Tasks belonging to @src_cset are about to be migrated to @dst_cgrp. Pin
  2344. * @src_cset and add it to @mgctx->src_csets, which should later be cleaned
  2345. * up by cgroup_migrate_finish().
  2346. *
  2347. * This function may be called without holding cgroup_threadgroup_rwsem
  2348. * even if the target is a process. Threads may be created and destroyed
  2349. * but as long as cgroup_mutex is not dropped, no new css_set can be put
  2350. * into play and the preloaded css_sets are guaranteed to cover all
  2351. * migrations.
  2352. */
  2353. void cgroup_migrate_add_src(struct css_set *src_cset,
  2354. struct cgroup *dst_cgrp,
  2355. struct cgroup_mgctx *mgctx)
  2356. {
  2357. struct cgroup *src_cgrp;
  2358. lockdep_assert_held(&cgroup_mutex);
  2359. lockdep_assert_held(&css_set_lock);
  2360. /*
  2361. * If ->dead, @src_set is associated with one or more dead cgroups
  2362. * and doesn't contain any migratable tasks. Ignore it early so
  2363. * that the rest of migration path doesn't get confused by it.
  2364. */
  2365. if (src_cset->dead)
  2366. return;
  2367. if (!list_empty(&src_cset->mg_src_preload_node))
  2368. return;
  2369. src_cgrp = cset_cgroup_from_root(src_cset, dst_cgrp->root);
  2370. WARN_ON(src_cset->mg_src_cgrp);
  2371. WARN_ON(src_cset->mg_dst_cgrp);
  2372. WARN_ON(!list_empty(&src_cset->mg_tasks));
  2373. WARN_ON(!list_empty(&src_cset->mg_node));
  2374. src_cset->mg_src_cgrp = src_cgrp;
  2375. src_cset->mg_dst_cgrp = dst_cgrp;
  2376. get_css_set(src_cset);
  2377. list_add_tail(&src_cset->mg_src_preload_node, &mgctx->preloaded_src_csets);
  2378. }
  2379. /**
  2380. * cgroup_migrate_prepare_dst - prepare destination css_sets for migration
  2381. * @mgctx: migration context
  2382. *
  2383. * Tasks are about to be moved and all the source css_sets have been
  2384. * preloaded to @mgctx->preloaded_src_csets. This function looks up and
  2385. * pins all destination css_sets, links each to its source, and append them
  2386. * to @mgctx->preloaded_dst_csets.
  2387. *
  2388. * This function must be called after cgroup_migrate_add_src() has been
  2389. * called on each migration source css_set. After migration is performed
  2390. * using cgroup_migrate(), cgroup_migrate_finish() must be called on
  2391. * @mgctx.
  2392. */
  2393. int cgroup_migrate_prepare_dst(struct cgroup_mgctx *mgctx)
  2394. {
  2395. struct css_set *src_cset, *tmp_cset;
  2396. lockdep_assert_held(&cgroup_mutex);
  2397. /* look up the dst cset for each src cset and link it to src */
  2398. list_for_each_entry_safe(src_cset, tmp_cset, &mgctx->preloaded_src_csets,
  2399. mg_src_preload_node) {
  2400. struct css_set *dst_cset;
  2401. struct cgroup_subsys *ss;
  2402. int ssid;
  2403. dst_cset = find_css_set(src_cset, src_cset->mg_dst_cgrp);
  2404. if (!dst_cset)
  2405. return -ENOMEM;
  2406. WARN_ON_ONCE(src_cset->mg_dst_cset || dst_cset->mg_dst_cset);
  2407. /*
  2408. * If src cset equals dst, it's noop. Drop the src.
  2409. * cgroup_migrate() will skip the cset too. Note that we
  2410. * can't handle src == dst as some nodes are used by both.
  2411. */
  2412. if (src_cset == dst_cset) {
  2413. src_cset->mg_src_cgrp = NULL;
  2414. src_cset->mg_dst_cgrp = NULL;
  2415. list_del_init(&src_cset->mg_src_preload_node);
  2416. put_css_set(src_cset);
  2417. put_css_set(dst_cset);
  2418. continue;
  2419. }
  2420. src_cset->mg_dst_cset = dst_cset;
  2421. if (list_empty(&dst_cset->mg_dst_preload_node))
  2422. list_add_tail(&dst_cset->mg_dst_preload_node,
  2423. &mgctx->preloaded_dst_csets);
  2424. else
  2425. put_css_set(dst_cset);
  2426. for_each_subsys(ss, ssid)
  2427. if (src_cset->subsys[ssid] != dst_cset->subsys[ssid])
  2428. mgctx->ss_mask |= 1 << ssid;
  2429. }
  2430. return 0;
  2431. }
  2432. /**
  2433. * cgroup_migrate - migrate a process or task to a cgroup
  2434. * @leader: the leader of the process or the task to migrate
  2435. * @threadgroup: whether @leader points to the whole process or a single task
  2436. * @mgctx: migration context
  2437. *
  2438. * Migrate a process or task denoted by @leader. If migrating a process,
  2439. * the caller must be holding cgroup_threadgroup_rwsem. The caller is also
  2440. * responsible for invoking cgroup_migrate_add_src() and
  2441. * cgroup_migrate_prepare_dst() on the targets before invoking this
  2442. * function and following up with cgroup_migrate_finish().
  2443. *
  2444. * As long as a controller's ->can_attach() doesn't fail, this function is
  2445. * guaranteed to succeed. This means that, excluding ->can_attach()
  2446. * failure, when migrating multiple targets, the success or failure can be
  2447. * decided for all targets by invoking group_migrate_prepare_dst() before
  2448. * actually starting migrating.
  2449. */
  2450. int cgroup_migrate(struct task_struct *leader, bool threadgroup,
  2451. struct cgroup_mgctx *mgctx)
  2452. {
  2453. struct task_struct *task;
  2454. /*
  2455. * Prevent freeing of tasks while we take a snapshot. Tasks that are
  2456. * already PF_EXITING could be freed from underneath us unless we
  2457. * take an rcu_read_lock.
  2458. */
  2459. spin_lock_irq(&css_set_lock);
  2460. rcu_read_lock();
  2461. task = leader;
  2462. do {
  2463. cgroup_migrate_add_task(task, mgctx);
  2464. if (!threadgroup)
  2465. break;
  2466. } while_each_thread(leader, task);
  2467. rcu_read_unlock();
  2468. spin_unlock_irq(&css_set_lock);
  2469. return cgroup_migrate_execute(mgctx);
  2470. }
  2471. /**
  2472. * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
  2473. * @dst_cgrp: the cgroup to attach to
  2474. * @leader: the task or the leader of the threadgroup to be attached
  2475. * @threadgroup: attach the whole threadgroup?
  2476. *
  2477. * Call holding cgroup_mutex and cgroup_threadgroup_rwsem.
  2478. */
  2479. int cgroup_attach_task(struct cgroup *dst_cgrp, struct task_struct *leader,
  2480. bool threadgroup)
  2481. {
  2482. DEFINE_CGROUP_MGCTX(mgctx);
  2483. struct task_struct *task;
  2484. int ret = 0;
  2485. /* look up all src csets */
  2486. spin_lock_irq(&css_set_lock);
  2487. rcu_read_lock();
  2488. task = leader;
  2489. do {
  2490. cgroup_migrate_add_src(task_css_set(task), dst_cgrp, &mgctx);
  2491. if (!threadgroup)
  2492. break;
  2493. } while_each_thread(leader, task);
  2494. rcu_read_unlock();
  2495. spin_unlock_irq(&css_set_lock);
  2496. /* prepare dst csets and commit */
  2497. ret = cgroup_migrate_prepare_dst(&mgctx);
  2498. if (!ret)
  2499. ret = cgroup_migrate(leader, threadgroup, &mgctx);
  2500. cgroup_migrate_finish(&mgctx);
  2501. if (!ret)
  2502. TRACE_CGROUP_PATH(attach_task, dst_cgrp, leader, threadgroup);
  2503. return ret;
  2504. }
  2505. struct task_struct *cgroup_procs_write_start(char *buf, bool threadgroup,
  2506. bool *threadgroup_locked,
  2507. struct cgroup *dst_cgrp)
  2508. {
  2509. struct task_struct *tsk;
  2510. pid_t pid;
  2511. bool force_migration = false;
  2512. if (kstrtoint(strstrip(buf), 0, &pid) || pid < 0)
  2513. return ERR_PTR(-EINVAL);
  2514. /*
  2515. * If we migrate a single thread, we don't care about threadgroup
  2516. * stability. If the thread is `current`, it won't exit(2) under our
  2517. * hands or change PID through exec(2). We exclude
  2518. * cgroup_update_dfl_csses and other cgroup_{proc,thread}s_write
  2519. * callers by cgroup_mutex.
  2520. * Therefore, we can skip the global lock.
  2521. */
  2522. lockdep_assert_held(&cgroup_mutex);
  2523. *threadgroup_locked = pid || threadgroup;
  2524. cgroup_attach_lock(*threadgroup_locked);
  2525. rcu_read_lock();
  2526. if (pid) {
  2527. tsk = find_task_by_vpid(pid);
  2528. if (!tsk) {
  2529. tsk = ERR_PTR(-ESRCH);
  2530. goto out_unlock_threadgroup;
  2531. }
  2532. } else {
  2533. tsk = current;
  2534. }
  2535. if (threadgroup)
  2536. tsk = tsk->group_leader;
  2537. if (tsk->flags & PF_KTHREAD)
  2538. trace_android_rvh_cgroup_force_kthread_migration(tsk, dst_cgrp, &force_migration);
  2539. /*
  2540. * kthreads may acquire PF_NO_SETAFFINITY during initialization.
  2541. * If userland migrates such a kthread to a non-root cgroup, it can
  2542. * become trapped in a cpuset, or RT kthread may be born in a
  2543. * cgroup with no rt_runtime allocated. Just say no.
  2544. */
  2545. if (!force_migration && (tsk->no_cgroup_migration || (tsk->flags & PF_NO_SETAFFINITY))) {
  2546. tsk = ERR_PTR(-EINVAL);
  2547. goto out_unlock_threadgroup;
  2548. }
  2549. get_task_struct(tsk);
  2550. goto out_unlock_rcu;
  2551. out_unlock_threadgroup:
  2552. cgroup_attach_unlock(*threadgroup_locked);
  2553. *threadgroup_locked = false;
  2554. out_unlock_rcu:
  2555. rcu_read_unlock();
  2556. return tsk;
  2557. }
  2558. void cgroup_procs_write_finish(struct task_struct *task, bool threadgroup_locked)
  2559. {
  2560. struct cgroup_subsys *ss;
  2561. int ssid;
  2562. /* release reference from cgroup_procs_write_start() */
  2563. put_task_struct(task);
  2564. cgroup_attach_unlock(threadgroup_locked);
  2565. for_each_subsys(ss, ssid)
  2566. if (ss->post_attach)
  2567. ss->post_attach();
  2568. }
  2569. static void cgroup_print_ss_mask(struct seq_file *seq, u16 ss_mask)
  2570. {
  2571. struct cgroup_subsys *ss;
  2572. bool printed = false;
  2573. int ssid;
  2574. do_each_subsys_mask(ss, ssid, ss_mask) {
  2575. if (printed)
  2576. seq_putc(seq, ' ');
  2577. seq_puts(seq, ss->name);
  2578. printed = true;
  2579. } while_each_subsys_mask();
  2580. if (printed)
  2581. seq_putc(seq, '\n');
  2582. }
  2583. /* show controllers which are enabled from the parent */
  2584. static int cgroup_controllers_show(struct seq_file *seq, void *v)
  2585. {
  2586. struct cgroup *cgrp = seq_css(seq)->cgroup;
  2587. cgroup_print_ss_mask(seq, cgroup_control(cgrp));
  2588. return 0;
  2589. }
  2590. /* show controllers which are enabled for a given cgroup's children */
  2591. static int cgroup_subtree_control_show(struct seq_file *seq, void *v)
  2592. {
  2593. struct cgroup *cgrp = seq_css(seq)->cgroup;
  2594. cgroup_print_ss_mask(seq, cgrp->subtree_control);
  2595. return 0;
  2596. }
  2597. /**
  2598. * cgroup_update_dfl_csses - update css assoc of a subtree in default hierarchy
  2599. * @cgrp: root of the subtree to update csses for
  2600. *
  2601. * @cgrp's control masks have changed and its subtree's css associations
  2602. * need to be updated accordingly. This function looks up all css_sets
  2603. * which are attached to the subtree, creates the matching updated css_sets
  2604. * and migrates the tasks to the new ones.
  2605. */
  2606. static int cgroup_update_dfl_csses(struct cgroup *cgrp)
  2607. {
  2608. DEFINE_CGROUP_MGCTX(mgctx);
  2609. struct cgroup_subsys_state *d_css;
  2610. struct cgroup *dsct;
  2611. struct css_set *src_cset;
  2612. bool has_tasks;
  2613. int ret;
  2614. lockdep_assert_held(&cgroup_mutex);
  2615. /* look up all csses currently attached to @cgrp's subtree */
  2616. spin_lock_irq(&css_set_lock);
  2617. cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) {
  2618. struct cgrp_cset_link *link;
  2619. /*
  2620. * As cgroup_update_dfl_csses() is only called by
  2621. * cgroup_apply_control(). The csses associated with the
  2622. * given cgrp will not be affected by changes made to
  2623. * its subtree_control file. We can skip them.
  2624. */
  2625. if (dsct == cgrp)
  2626. continue;
  2627. list_for_each_entry(link, &dsct->cset_links, cset_link)
  2628. cgroup_migrate_add_src(link->cset, dsct, &mgctx);
  2629. }
  2630. spin_unlock_irq(&css_set_lock);
  2631. /*
  2632. * We need to write-lock threadgroup_rwsem while migrating tasks.
  2633. * However, if there are no source csets for @cgrp, changing its
  2634. * controllers isn't gonna produce any task migrations and the
  2635. * write-locking can be skipped safely.
  2636. */
  2637. has_tasks = !list_empty(&mgctx.preloaded_src_csets);
  2638. cgroup_attach_lock(has_tasks);
  2639. /* NULL dst indicates self on default hierarchy */
  2640. ret = cgroup_migrate_prepare_dst(&mgctx);
  2641. if (ret)
  2642. goto out_finish;
  2643. spin_lock_irq(&css_set_lock);
  2644. list_for_each_entry(src_cset, &mgctx.preloaded_src_csets,
  2645. mg_src_preload_node) {
  2646. struct task_struct *task, *ntask;
  2647. /* all tasks in src_csets need to be migrated */
  2648. list_for_each_entry_safe(task, ntask, &src_cset->tasks, cg_list)
  2649. cgroup_migrate_add_task(task, &mgctx);
  2650. }
  2651. spin_unlock_irq(&css_set_lock);
  2652. ret = cgroup_migrate_execute(&mgctx);
  2653. out_finish:
  2654. cgroup_migrate_finish(&mgctx);
  2655. cgroup_attach_unlock(has_tasks);
  2656. return ret;
  2657. }
  2658. /**
  2659. * cgroup_lock_and_drain_offline - lock cgroup_mutex and drain offlined csses
  2660. * @cgrp: root of the target subtree
  2661. *
  2662. * Because css offlining is asynchronous, userland may try to re-enable a
  2663. * controller while the previous css is still around. This function grabs
  2664. * cgroup_mutex and drains the previous css instances of @cgrp's subtree.
  2665. */
  2666. void cgroup_lock_and_drain_offline(struct cgroup *cgrp)
  2667. __acquires(&cgroup_mutex)
  2668. {
  2669. struct cgroup *dsct;
  2670. struct cgroup_subsys_state *d_css;
  2671. struct cgroup_subsys *ss;
  2672. int ssid;
  2673. restart:
  2674. cgroup_lock();
  2675. cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) {
  2676. for_each_subsys(ss, ssid) {
  2677. struct cgroup_subsys_state *css = cgroup_css(dsct, ss);
  2678. DEFINE_WAIT(wait);
  2679. if (!css || !percpu_ref_is_dying(&css->refcnt))
  2680. continue;
  2681. cgroup_get_live(dsct);
  2682. prepare_to_wait(&dsct->offline_waitq, &wait,
  2683. TASK_UNINTERRUPTIBLE);
  2684. cgroup_unlock();
  2685. schedule();
  2686. finish_wait(&dsct->offline_waitq, &wait);
  2687. cgroup_put(dsct);
  2688. goto restart;
  2689. }
  2690. }
  2691. }
  2692. /**
  2693. * cgroup_save_control - save control masks and dom_cgrp of a subtree
  2694. * @cgrp: root of the target subtree
  2695. *
  2696. * Save ->subtree_control, ->subtree_ss_mask and ->dom_cgrp to the
  2697. * respective old_ prefixed fields for @cgrp's subtree including @cgrp
  2698. * itself.
  2699. */
  2700. static void cgroup_save_control(struct cgroup *cgrp)
  2701. {
  2702. struct cgroup *dsct;
  2703. struct cgroup_subsys_state *d_css;
  2704. cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) {
  2705. dsct->old_subtree_control = dsct->subtree_control;
  2706. dsct->old_subtree_ss_mask = dsct->subtree_ss_mask;
  2707. dsct->old_dom_cgrp = dsct->dom_cgrp;
  2708. }
  2709. }
  2710. /**
  2711. * cgroup_propagate_control - refresh control masks of a subtree
  2712. * @cgrp: root of the target subtree
  2713. *
  2714. * For @cgrp and its subtree, ensure ->subtree_ss_mask matches
  2715. * ->subtree_control and propagate controller availability through the
  2716. * subtree so that descendants don't have unavailable controllers enabled.
  2717. */
  2718. static void cgroup_propagate_control(struct cgroup *cgrp)
  2719. {
  2720. struct cgroup *dsct;
  2721. struct cgroup_subsys_state *d_css;
  2722. cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) {
  2723. dsct->subtree_control &= cgroup_control(dsct);
  2724. dsct->subtree_ss_mask =
  2725. cgroup_calc_subtree_ss_mask(dsct->subtree_control,
  2726. cgroup_ss_mask(dsct));
  2727. }
  2728. }
  2729. /**
  2730. * cgroup_restore_control - restore control masks and dom_cgrp of a subtree
  2731. * @cgrp: root of the target subtree
  2732. *
  2733. * Restore ->subtree_control, ->subtree_ss_mask and ->dom_cgrp from the
  2734. * respective old_ prefixed fields for @cgrp's subtree including @cgrp
  2735. * itself.
  2736. */
  2737. static void cgroup_restore_control(struct cgroup *cgrp)
  2738. {
  2739. struct cgroup *dsct;
  2740. struct cgroup_subsys_state *d_css;
  2741. cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) {
  2742. dsct->subtree_control = dsct->old_subtree_control;
  2743. dsct->subtree_ss_mask = dsct->old_subtree_ss_mask;
  2744. dsct->dom_cgrp = dsct->old_dom_cgrp;
  2745. }
  2746. }
  2747. static bool css_visible(struct cgroup_subsys_state *css)
  2748. {
  2749. struct cgroup_subsys *ss = css->ss;
  2750. struct cgroup *cgrp = css->cgroup;
  2751. if (cgroup_control(cgrp) & (1 << ss->id))
  2752. return true;
  2753. if (!(cgroup_ss_mask(cgrp) & (1 << ss->id)))
  2754. return false;
  2755. return cgroup_on_dfl(cgrp) && ss->implicit_on_dfl;
  2756. }
  2757. /**
  2758. * cgroup_apply_control_enable - enable or show csses according to control
  2759. * @cgrp: root of the target subtree
  2760. *
  2761. * Walk @cgrp's subtree and create new csses or make the existing ones
  2762. * visible. A css is created invisible if it's being implicitly enabled
  2763. * through dependency. An invisible css is made visible when the userland
  2764. * explicitly enables it.
  2765. *
  2766. * Returns 0 on success, -errno on failure. On failure, csses which have
  2767. * been processed already aren't cleaned up. The caller is responsible for
  2768. * cleaning up with cgroup_apply_control_disable().
  2769. */
  2770. static int cgroup_apply_control_enable(struct cgroup *cgrp)
  2771. {
  2772. struct cgroup *dsct;
  2773. struct cgroup_subsys_state *d_css;
  2774. struct cgroup_subsys *ss;
  2775. int ssid, ret;
  2776. cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) {
  2777. for_each_subsys(ss, ssid) {
  2778. struct cgroup_subsys_state *css = cgroup_css(dsct, ss);
  2779. if (!(cgroup_ss_mask(dsct) & (1 << ss->id)))
  2780. continue;
  2781. if (!css) {
  2782. css = css_create(dsct, ss);
  2783. if (IS_ERR(css))
  2784. return PTR_ERR(css);
  2785. }
  2786. WARN_ON_ONCE(percpu_ref_is_dying(&css->refcnt));
  2787. if (css_visible(css)) {
  2788. ret = css_populate_dir(css);
  2789. if (ret)
  2790. return ret;
  2791. }
  2792. }
  2793. }
  2794. return 0;
  2795. }
  2796. /**
  2797. * cgroup_apply_control_disable - kill or hide csses according to control
  2798. * @cgrp: root of the target subtree
  2799. *
  2800. * Walk @cgrp's subtree and kill and hide csses so that they match
  2801. * cgroup_ss_mask() and cgroup_visible_mask().
  2802. *
  2803. * A css is hidden when the userland requests it to be disabled while other
  2804. * subsystems are still depending on it. The css must not actively control
  2805. * resources and be in the vanilla state if it's made visible again later.
  2806. * Controllers which may be depended upon should provide ->css_reset() for
  2807. * this purpose.
  2808. */
  2809. static void cgroup_apply_control_disable(struct cgroup *cgrp)
  2810. {
  2811. struct cgroup *dsct;
  2812. struct cgroup_subsys_state *d_css;
  2813. struct cgroup_subsys *ss;
  2814. int ssid;
  2815. cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) {
  2816. for_each_subsys(ss, ssid) {
  2817. struct cgroup_subsys_state *css = cgroup_css(dsct, ss);
  2818. if (!css)
  2819. continue;
  2820. WARN_ON_ONCE(percpu_ref_is_dying(&css->refcnt));
  2821. if (css->parent &&
  2822. !(cgroup_ss_mask(dsct) & (1 << ss->id))) {
  2823. kill_css(css);
  2824. } else if (!css_visible(css)) {
  2825. css_clear_dir(css);
  2826. if (ss->css_reset)
  2827. ss->css_reset(css);
  2828. }
  2829. }
  2830. }
  2831. }
  2832. /**
  2833. * cgroup_apply_control - apply control mask updates to the subtree
  2834. * @cgrp: root of the target subtree
  2835. *
  2836. * subsystems can be enabled and disabled in a subtree using the following
  2837. * steps.
  2838. *
  2839. * 1. Call cgroup_save_control() to stash the current state.
  2840. * 2. Update ->subtree_control masks in the subtree as desired.
  2841. * 3. Call cgroup_apply_control() to apply the changes.
  2842. * 4. Optionally perform other related operations.
  2843. * 5. Call cgroup_finalize_control() to finish up.
  2844. *
  2845. * This function implements step 3 and propagates the mask changes
  2846. * throughout @cgrp's subtree, updates csses accordingly and perform
  2847. * process migrations.
  2848. */
  2849. static int cgroup_apply_control(struct cgroup *cgrp)
  2850. {
  2851. int ret;
  2852. cgroup_propagate_control(cgrp);
  2853. ret = cgroup_apply_control_enable(cgrp);
  2854. if (ret)
  2855. return ret;
  2856. /*
  2857. * At this point, cgroup_e_css_by_mask() results reflect the new csses
  2858. * making the following cgroup_update_dfl_csses() properly update
  2859. * css associations of all tasks in the subtree.
  2860. */
  2861. return cgroup_update_dfl_csses(cgrp);
  2862. }
  2863. /**
  2864. * cgroup_finalize_control - finalize control mask update
  2865. * @cgrp: root of the target subtree
  2866. * @ret: the result of the update
  2867. *
  2868. * Finalize control mask update. See cgroup_apply_control() for more info.
  2869. */
  2870. static void cgroup_finalize_control(struct cgroup *cgrp, int ret)
  2871. {
  2872. if (ret) {
  2873. cgroup_restore_control(cgrp);
  2874. cgroup_propagate_control(cgrp);
  2875. }
  2876. cgroup_apply_control_disable(cgrp);
  2877. }
  2878. static int cgroup_vet_subtree_control_enable(struct cgroup *cgrp, u16 enable)
  2879. {
  2880. u16 domain_enable = enable & ~cgrp_dfl_threaded_ss_mask;
  2881. /* if nothing is getting enabled, nothing to worry about */
  2882. if (!enable)
  2883. return 0;
  2884. /* can @cgrp host any resources? */
  2885. if (!cgroup_is_valid_domain(cgrp->dom_cgrp))
  2886. return -EOPNOTSUPP;
  2887. /* mixables don't care */
  2888. if (cgroup_is_mixable(cgrp))
  2889. return 0;
  2890. if (domain_enable) {
  2891. /* can't enable domain controllers inside a thread subtree */
  2892. if (cgroup_is_thread_root(cgrp) || cgroup_is_threaded(cgrp))
  2893. return -EOPNOTSUPP;
  2894. } else {
  2895. /*
  2896. * Threaded controllers can handle internal competitions
  2897. * and are always allowed inside a (prospective) thread
  2898. * subtree.
  2899. */
  2900. if (cgroup_can_be_thread_root(cgrp) || cgroup_is_threaded(cgrp))
  2901. return 0;
  2902. }
  2903. /*
  2904. * Controllers can't be enabled for a cgroup with tasks to avoid
  2905. * child cgroups competing against tasks.
  2906. */
  2907. if (cgroup_has_tasks(cgrp))
  2908. return -EBUSY;
  2909. return 0;
  2910. }
  2911. /* change the enabled child controllers for a cgroup in the default hierarchy */
  2912. static ssize_t cgroup_subtree_control_write(struct kernfs_open_file *of,
  2913. char *buf, size_t nbytes,
  2914. loff_t off)
  2915. {
  2916. u16 enable = 0, disable = 0;
  2917. struct cgroup *cgrp, *child;
  2918. struct cgroup_subsys *ss;
  2919. char *tok;
  2920. int ssid, ret;
  2921. /*
  2922. * Parse input - space separated list of subsystem names prefixed
  2923. * with either + or -.
  2924. */
  2925. buf = strstrip(buf);
  2926. while ((tok = strsep(&buf, " "))) {
  2927. if (tok[0] == '\0')
  2928. continue;
  2929. do_each_subsys_mask(ss, ssid, ~cgrp_dfl_inhibit_ss_mask) {
  2930. if (!cgroup_ssid_enabled(ssid) ||
  2931. strcmp(tok + 1, ss->name))
  2932. continue;
  2933. if (*tok == '+') {
  2934. enable |= 1 << ssid;
  2935. disable &= ~(1 << ssid);
  2936. } else if (*tok == '-') {
  2937. disable |= 1 << ssid;
  2938. enable &= ~(1 << ssid);
  2939. } else {
  2940. return -EINVAL;
  2941. }
  2942. break;
  2943. } while_each_subsys_mask();
  2944. if (ssid == CGROUP_SUBSYS_COUNT)
  2945. return -EINVAL;
  2946. }
  2947. cgrp = cgroup_kn_lock_live(of->kn, true);
  2948. if (!cgrp)
  2949. return -ENODEV;
  2950. for_each_subsys(ss, ssid) {
  2951. if (enable & (1 << ssid)) {
  2952. if (cgrp->subtree_control & (1 << ssid)) {
  2953. enable &= ~(1 << ssid);
  2954. continue;
  2955. }
  2956. if (!(cgroup_control(cgrp) & (1 << ssid))) {
  2957. ret = -ENOENT;
  2958. goto out_unlock;
  2959. }
  2960. } else if (disable & (1 << ssid)) {
  2961. if (!(cgrp->subtree_control & (1 << ssid))) {
  2962. disable &= ~(1 << ssid);
  2963. continue;
  2964. }
  2965. /* a child has it enabled? */
  2966. cgroup_for_each_live_child(child, cgrp) {
  2967. if (child->subtree_control & (1 << ssid)) {
  2968. ret = -EBUSY;
  2969. goto out_unlock;
  2970. }
  2971. }
  2972. }
  2973. }
  2974. if (!enable && !disable) {
  2975. ret = 0;
  2976. goto out_unlock;
  2977. }
  2978. ret = cgroup_vet_subtree_control_enable(cgrp, enable);
  2979. if (ret)
  2980. goto out_unlock;
  2981. /* save and update control masks and prepare csses */
  2982. cgroup_save_control(cgrp);
  2983. cgrp->subtree_control |= enable;
  2984. cgrp->subtree_control &= ~disable;
  2985. ret = cgroup_apply_control(cgrp);
  2986. cgroup_finalize_control(cgrp, ret);
  2987. if (ret)
  2988. goto out_unlock;
  2989. kernfs_activate(cgrp->kn);
  2990. out_unlock:
  2991. cgroup_kn_unlock(of->kn);
  2992. return ret ?: nbytes;
  2993. }
  2994. /**
  2995. * cgroup_enable_threaded - make @cgrp threaded
  2996. * @cgrp: the target cgroup
  2997. *
  2998. * Called when "threaded" is written to the cgroup.type interface file and
  2999. * tries to make @cgrp threaded and join the parent's resource domain.
  3000. * This function is never called on the root cgroup as cgroup.type doesn't
  3001. * exist on it.
  3002. */
  3003. static int cgroup_enable_threaded(struct cgroup *cgrp)
  3004. {
  3005. struct cgroup *parent = cgroup_parent(cgrp);
  3006. struct cgroup *dom_cgrp = parent->dom_cgrp;
  3007. struct cgroup *dsct;
  3008. struct cgroup_subsys_state *d_css;
  3009. int ret;
  3010. lockdep_assert_held(&cgroup_mutex);
  3011. /* noop if already threaded */
  3012. if (cgroup_is_threaded(cgrp))
  3013. return 0;
  3014. /*
  3015. * If @cgroup is populated or has domain controllers enabled, it
  3016. * can't be switched. While the below cgroup_can_be_thread_root()
  3017. * test can catch the same conditions, that's only when @parent is
  3018. * not mixable, so let's check it explicitly.
  3019. */
  3020. if (cgroup_is_populated(cgrp) ||
  3021. cgrp->subtree_control & ~cgrp_dfl_threaded_ss_mask)
  3022. return -EOPNOTSUPP;
  3023. /* we're joining the parent's domain, ensure its validity */
  3024. if (!cgroup_is_valid_domain(dom_cgrp) ||
  3025. !cgroup_can_be_thread_root(dom_cgrp))
  3026. return -EOPNOTSUPP;
  3027. /*
  3028. * The following shouldn't cause actual migrations and should
  3029. * always succeed.
  3030. */
  3031. cgroup_save_control(cgrp);
  3032. cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp)
  3033. if (dsct == cgrp || cgroup_is_threaded(dsct))
  3034. dsct->dom_cgrp = dom_cgrp;
  3035. ret = cgroup_apply_control(cgrp);
  3036. if (!ret)
  3037. parent->nr_threaded_children++;
  3038. cgroup_finalize_control(cgrp, ret);
  3039. return ret;
  3040. }
  3041. static int cgroup_type_show(struct seq_file *seq, void *v)
  3042. {
  3043. struct cgroup *cgrp = seq_css(seq)->cgroup;
  3044. if (cgroup_is_threaded(cgrp))
  3045. seq_puts(seq, "threaded\n");
  3046. else if (!cgroup_is_valid_domain(cgrp))
  3047. seq_puts(seq, "domain invalid\n");
  3048. else if (cgroup_is_thread_root(cgrp))
  3049. seq_puts(seq, "domain threaded\n");
  3050. else
  3051. seq_puts(seq, "domain\n");
  3052. return 0;
  3053. }
  3054. static ssize_t cgroup_type_write(struct kernfs_open_file *of, char *buf,
  3055. size_t nbytes, loff_t off)
  3056. {
  3057. struct cgroup *cgrp;
  3058. int ret;
  3059. /* only switching to threaded mode is supported */
  3060. if (strcmp(strstrip(buf), "threaded"))
  3061. return -EINVAL;
  3062. /* drain dying csses before we re-apply (threaded) subtree control */
  3063. cgrp = cgroup_kn_lock_live(of->kn, true);
  3064. if (!cgrp)
  3065. return -ENOENT;
  3066. /* threaded can only be enabled */
  3067. ret = cgroup_enable_threaded(cgrp);
  3068. cgroup_kn_unlock(of->kn);
  3069. return ret ?: nbytes;
  3070. }
  3071. static int cgroup_max_descendants_show(struct seq_file *seq, void *v)
  3072. {
  3073. struct cgroup *cgrp = seq_css(seq)->cgroup;
  3074. int descendants = READ_ONCE(cgrp->max_descendants);
  3075. if (descendants == INT_MAX)
  3076. seq_puts(seq, "max\n");
  3077. else
  3078. seq_printf(seq, "%d\n", descendants);
  3079. return 0;
  3080. }
  3081. static ssize_t cgroup_max_descendants_write(struct kernfs_open_file *of,
  3082. char *buf, size_t nbytes, loff_t off)
  3083. {
  3084. struct cgroup *cgrp;
  3085. int descendants;
  3086. ssize_t ret;
  3087. buf = strstrip(buf);
  3088. if (!strcmp(buf, "max")) {
  3089. descendants = INT_MAX;
  3090. } else {
  3091. ret = kstrtoint(buf, 0, &descendants);
  3092. if (ret)
  3093. return ret;
  3094. }
  3095. if (descendants < 0)
  3096. return -ERANGE;
  3097. cgrp = cgroup_kn_lock_live(of->kn, false);
  3098. if (!cgrp)
  3099. return -ENOENT;
  3100. cgrp->max_descendants = descendants;
  3101. cgroup_kn_unlock(of->kn);
  3102. return nbytes;
  3103. }
  3104. static int cgroup_max_depth_show(struct seq_file *seq, void *v)
  3105. {
  3106. struct cgroup *cgrp = seq_css(seq)->cgroup;
  3107. int depth = READ_ONCE(cgrp->max_depth);
  3108. if (depth == INT_MAX)
  3109. seq_puts(seq, "max\n");
  3110. else
  3111. seq_printf(seq, "%d\n", depth);
  3112. return 0;
  3113. }
  3114. static ssize_t cgroup_max_depth_write(struct kernfs_open_file *of,
  3115. char *buf, size_t nbytes, loff_t off)
  3116. {
  3117. struct cgroup *cgrp;
  3118. ssize_t ret;
  3119. int depth;
  3120. buf = strstrip(buf);
  3121. if (!strcmp(buf, "max")) {
  3122. depth = INT_MAX;
  3123. } else {
  3124. ret = kstrtoint(buf, 0, &depth);
  3125. if (ret)
  3126. return ret;
  3127. }
  3128. if (depth < 0)
  3129. return -ERANGE;
  3130. cgrp = cgroup_kn_lock_live(of->kn, false);
  3131. if (!cgrp)
  3132. return -ENOENT;
  3133. cgrp->max_depth = depth;
  3134. cgroup_kn_unlock(of->kn);
  3135. return nbytes;
  3136. }
  3137. static int cgroup_events_show(struct seq_file *seq, void *v)
  3138. {
  3139. struct cgroup *cgrp = seq_css(seq)->cgroup;
  3140. seq_printf(seq, "populated %d\n", cgroup_is_populated(cgrp));
  3141. seq_printf(seq, "frozen %d\n", test_bit(CGRP_FROZEN, &cgrp->flags));
  3142. return 0;
  3143. }
  3144. static int cgroup_stat_show(struct seq_file *seq, void *v)
  3145. {
  3146. struct cgroup *cgroup = seq_css(seq)->cgroup;
  3147. seq_printf(seq, "nr_descendants %d\n",
  3148. cgroup->nr_descendants);
  3149. seq_printf(seq, "nr_dying_descendants %d\n",
  3150. cgroup->nr_dying_descendants);
  3151. return 0;
  3152. }
  3153. static int __maybe_unused cgroup_extra_stat_show(struct seq_file *seq,
  3154. struct cgroup *cgrp, int ssid)
  3155. {
  3156. struct cgroup_subsys *ss = cgroup_subsys[ssid];
  3157. struct cgroup_subsys_state *css;
  3158. int ret;
  3159. if (!ss->css_extra_stat_show)
  3160. return 0;
  3161. css = cgroup_tryget_css(cgrp, ss);
  3162. if (!css)
  3163. return 0;
  3164. ret = ss->css_extra_stat_show(seq, css);
  3165. css_put(css);
  3166. return ret;
  3167. }
  3168. static int cpu_stat_show(struct seq_file *seq, void *v)
  3169. {
  3170. struct cgroup __maybe_unused *cgrp = seq_css(seq)->cgroup;
  3171. int ret = 0;
  3172. cgroup_base_stat_cputime_show(seq);
  3173. #ifdef CONFIG_CGROUP_SCHED
  3174. ret = cgroup_extra_stat_show(seq, cgrp, cpu_cgrp_id);
  3175. #endif
  3176. return ret;
  3177. }
  3178. #ifdef CONFIG_PSI
  3179. static int cgroup_io_pressure_show(struct seq_file *seq, void *v)
  3180. {
  3181. struct cgroup *cgrp = seq_css(seq)->cgroup;
  3182. struct psi_group *psi = cgroup_psi(cgrp);
  3183. return psi_show(seq, psi, PSI_IO);
  3184. }
  3185. static int cgroup_memory_pressure_show(struct seq_file *seq, void *v)
  3186. {
  3187. struct cgroup *cgrp = seq_css(seq)->cgroup;
  3188. struct psi_group *psi = cgroup_psi(cgrp);
  3189. return psi_show(seq, psi, PSI_MEM);
  3190. }
  3191. static int cgroup_cpu_pressure_show(struct seq_file *seq, void *v)
  3192. {
  3193. struct cgroup *cgrp = seq_css(seq)->cgroup;
  3194. struct psi_group *psi = cgroup_psi(cgrp);
  3195. return psi_show(seq, psi, PSI_CPU);
  3196. }
  3197. static ssize_t pressure_write(struct kernfs_open_file *of, char *buf,
  3198. size_t nbytes, enum psi_res res)
  3199. {
  3200. struct cgroup_file_ctx *ctx = of->priv;
  3201. struct psi_trigger *new;
  3202. struct cgroup *cgrp;
  3203. struct psi_group *psi;
  3204. cgrp = cgroup_kn_lock_live(of->kn, false);
  3205. if (!cgrp)
  3206. return -ENODEV;
  3207. cgroup_get(cgrp);
  3208. cgroup_kn_unlock(of->kn);
  3209. /* Allow only one trigger per file descriptor */
  3210. if (ctx->psi.trigger) {
  3211. cgroup_put(cgrp);
  3212. return -EBUSY;
  3213. }
  3214. psi = cgroup_psi(cgrp);
  3215. new = psi_trigger_create(psi, buf, res);
  3216. if (IS_ERR(new)) {
  3217. cgroup_put(cgrp);
  3218. return PTR_ERR(new);
  3219. }
  3220. smp_store_release(&ctx->psi.trigger, new);
  3221. cgroup_put(cgrp);
  3222. return nbytes;
  3223. }
  3224. static ssize_t cgroup_io_pressure_write(struct kernfs_open_file *of,
  3225. char *buf, size_t nbytes,
  3226. loff_t off)
  3227. {
  3228. return pressure_write(of, buf, nbytes, PSI_IO);
  3229. }
  3230. static ssize_t cgroup_memory_pressure_write(struct kernfs_open_file *of,
  3231. char *buf, size_t nbytes,
  3232. loff_t off)
  3233. {
  3234. return pressure_write(of, buf, nbytes, PSI_MEM);
  3235. }
  3236. static ssize_t cgroup_cpu_pressure_write(struct kernfs_open_file *of,
  3237. char *buf, size_t nbytes,
  3238. loff_t off)
  3239. {
  3240. return pressure_write(of, buf, nbytes, PSI_CPU);
  3241. }
  3242. #ifdef CONFIG_IRQ_TIME_ACCOUNTING
  3243. static int cgroup_irq_pressure_show(struct seq_file *seq, void *v)
  3244. {
  3245. struct cgroup *cgrp = seq_css(seq)->cgroup;
  3246. struct psi_group *psi = cgroup_psi(cgrp);
  3247. return psi_show(seq, psi, PSI_IRQ);
  3248. }
  3249. static ssize_t cgroup_irq_pressure_write(struct kernfs_open_file *of,
  3250. char *buf, size_t nbytes,
  3251. loff_t off)
  3252. {
  3253. return pressure_write(of, buf, nbytes, PSI_IRQ);
  3254. }
  3255. #endif
  3256. static int cgroup_pressure_show(struct seq_file *seq, void *v)
  3257. {
  3258. struct cgroup *cgrp = seq_css(seq)->cgroup;
  3259. struct psi_group *psi = cgroup_psi(cgrp);
  3260. seq_printf(seq, "%d\n", psi->enabled);
  3261. return 0;
  3262. }
  3263. static ssize_t cgroup_pressure_write(struct kernfs_open_file *of,
  3264. char *buf, size_t nbytes,
  3265. loff_t off)
  3266. {
  3267. ssize_t ret;
  3268. int enable;
  3269. struct cgroup *cgrp;
  3270. struct psi_group *psi;
  3271. ret = kstrtoint(strstrip(buf), 0, &enable);
  3272. if (ret)
  3273. return ret;
  3274. if (enable < 0 || enable > 1)
  3275. return -ERANGE;
  3276. cgrp = cgroup_kn_lock_live(of->kn, false);
  3277. if (!cgrp)
  3278. return -ENOENT;
  3279. psi = cgroup_psi(cgrp);
  3280. if (psi->enabled != enable) {
  3281. int i;
  3282. /* show or hide {cpu,memory,io,irq}.pressure files */
  3283. for (i = 0; i < NR_PSI_RESOURCES; i++)
  3284. cgroup_file_show(&cgrp->psi_files[i], enable);
  3285. psi->enabled = enable;
  3286. if (enable)
  3287. psi_cgroup_restart(psi);
  3288. }
  3289. cgroup_kn_unlock(of->kn);
  3290. return nbytes;
  3291. }
  3292. static __poll_t cgroup_pressure_poll(struct kernfs_open_file *of,
  3293. poll_table *pt)
  3294. {
  3295. struct cgroup_file_ctx *ctx = of->priv;
  3296. return psi_trigger_poll(&ctx->psi.trigger, of->file, pt);
  3297. }
  3298. static int cgroup_pressure_open(struct kernfs_open_file *of)
  3299. {
  3300. return (of->file->f_mode & FMODE_WRITE && !capable(CAP_SYS_RESOURCE)) ?
  3301. -EPERM : 0;
  3302. }
  3303. static void cgroup_pressure_release(struct kernfs_open_file *of)
  3304. {
  3305. struct cgroup_file_ctx *ctx = of->priv;
  3306. psi_trigger_destroy(ctx->psi.trigger);
  3307. }
  3308. bool cgroup_psi_enabled(void)
  3309. {
  3310. if (static_branch_likely(&psi_disabled))
  3311. return false;
  3312. return (cgroup_feature_disable_mask & (1 << OPT_FEATURE_PRESSURE)) == 0;
  3313. }
  3314. #else /* CONFIG_PSI */
  3315. bool cgroup_psi_enabled(void)
  3316. {
  3317. return false;
  3318. }
  3319. #endif /* CONFIG_PSI */
  3320. static int cgroup_freeze_show(struct seq_file *seq, void *v)
  3321. {
  3322. struct cgroup *cgrp = seq_css(seq)->cgroup;
  3323. seq_printf(seq, "%d\n", cgrp->freezer.freeze);
  3324. return 0;
  3325. }
  3326. static ssize_t cgroup_freeze_write(struct kernfs_open_file *of,
  3327. char *buf, size_t nbytes, loff_t off)
  3328. {
  3329. struct cgroup *cgrp;
  3330. ssize_t ret;
  3331. int freeze;
  3332. ret = kstrtoint(strstrip(buf), 0, &freeze);
  3333. if (ret)
  3334. return ret;
  3335. if (freeze < 0 || freeze > 1)
  3336. return -ERANGE;
  3337. cgrp = cgroup_kn_lock_live(of->kn, false);
  3338. if (!cgrp)
  3339. return -ENOENT;
  3340. cgroup_freeze(cgrp, freeze);
  3341. cgroup_kn_unlock(of->kn);
  3342. return nbytes;
  3343. }
  3344. static void __cgroup_kill(struct cgroup *cgrp)
  3345. {
  3346. struct css_task_iter it;
  3347. struct task_struct *task;
  3348. lockdep_assert_held(&cgroup_mutex);
  3349. spin_lock_irq(&css_set_lock);
  3350. set_bit(CGRP_KILL, &cgrp->flags);
  3351. spin_unlock_irq(&css_set_lock);
  3352. css_task_iter_start(&cgrp->self, CSS_TASK_ITER_PROCS | CSS_TASK_ITER_THREADED, &it);
  3353. while ((task = css_task_iter_next(&it))) {
  3354. /* Ignore kernel threads here. */
  3355. if (task->flags & PF_KTHREAD)
  3356. continue;
  3357. /* Skip tasks that are already dying. */
  3358. if (__fatal_signal_pending(task))
  3359. continue;
  3360. send_sig(SIGKILL, task, 0);
  3361. }
  3362. css_task_iter_end(&it);
  3363. spin_lock_irq(&css_set_lock);
  3364. clear_bit(CGRP_KILL, &cgrp->flags);
  3365. spin_unlock_irq(&css_set_lock);
  3366. }
  3367. static void cgroup_kill(struct cgroup *cgrp)
  3368. {
  3369. struct cgroup_subsys_state *css;
  3370. struct cgroup *dsct;
  3371. lockdep_assert_held(&cgroup_mutex);
  3372. cgroup_for_each_live_descendant_pre(dsct, css, cgrp)
  3373. __cgroup_kill(dsct);
  3374. }
  3375. static ssize_t cgroup_kill_write(struct kernfs_open_file *of, char *buf,
  3376. size_t nbytes, loff_t off)
  3377. {
  3378. ssize_t ret = 0;
  3379. int kill;
  3380. struct cgroup *cgrp;
  3381. ret = kstrtoint(strstrip(buf), 0, &kill);
  3382. if (ret)
  3383. return ret;
  3384. if (kill != 1)
  3385. return -ERANGE;
  3386. cgrp = cgroup_kn_lock_live(of->kn, false);
  3387. if (!cgrp)
  3388. return -ENOENT;
  3389. /*
  3390. * Killing is a process directed operation, i.e. the whole thread-group
  3391. * is taken down so act like we do for cgroup.procs and only make this
  3392. * writable in non-threaded cgroups.
  3393. */
  3394. if (cgroup_is_threaded(cgrp))
  3395. ret = -EOPNOTSUPP;
  3396. else
  3397. cgroup_kill(cgrp);
  3398. cgroup_kn_unlock(of->kn);
  3399. return ret ?: nbytes;
  3400. }
  3401. static int cgroup_file_open(struct kernfs_open_file *of)
  3402. {
  3403. struct cftype *cft = of_cft(of);
  3404. struct cgroup_file_ctx *ctx;
  3405. int ret;
  3406. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  3407. if (!ctx)
  3408. return -ENOMEM;
  3409. ctx->ns = current->nsproxy->cgroup_ns;
  3410. get_cgroup_ns(ctx->ns);
  3411. of->priv = ctx;
  3412. if (!cft->open)
  3413. return 0;
  3414. ret = cft->open(of);
  3415. if (ret) {
  3416. put_cgroup_ns(ctx->ns);
  3417. kfree(ctx);
  3418. }
  3419. return ret;
  3420. }
  3421. static void cgroup_file_release(struct kernfs_open_file *of)
  3422. {
  3423. struct cftype *cft = of_cft(of);
  3424. struct cgroup_file_ctx *ctx = of->priv;
  3425. if (cft->release)
  3426. cft->release(of);
  3427. put_cgroup_ns(ctx->ns);
  3428. kfree(ctx);
  3429. }
  3430. static ssize_t cgroup_file_write(struct kernfs_open_file *of, char *buf,
  3431. size_t nbytes, loff_t off)
  3432. {
  3433. struct cgroup_file_ctx *ctx = of->priv;
  3434. struct cgroup *cgrp = of->kn->parent->priv;
  3435. struct cftype *cft = of_cft(of);
  3436. struct cgroup_subsys_state *css;
  3437. int ret;
  3438. if (!nbytes)
  3439. return 0;
  3440. /*
  3441. * If namespaces are delegation boundaries, disallow writes to
  3442. * files in an non-init namespace root from inside the namespace
  3443. * except for the files explicitly marked delegatable -
  3444. * cgroup.procs and cgroup.subtree_control.
  3445. */
  3446. if ((cgrp->root->flags & CGRP_ROOT_NS_DELEGATE) &&
  3447. !(cft->flags & CFTYPE_NS_DELEGATABLE) &&
  3448. ctx->ns != &init_cgroup_ns && ctx->ns->root_cset->dfl_cgrp == cgrp)
  3449. return -EPERM;
  3450. if (cft->write)
  3451. return cft->write(of, buf, nbytes, off);
  3452. /*
  3453. * kernfs guarantees that a file isn't deleted with operations in
  3454. * flight, which means that the matching css is and stays alive and
  3455. * doesn't need to be pinned. The RCU locking is not necessary
  3456. * either. It's just for the convenience of using cgroup_css().
  3457. */
  3458. rcu_read_lock();
  3459. css = cgroup_css(cgrp, cft->ss);
  3460. rcu_read_unlock();
  3461. if (cft->write_u64) {
  3462. unsigned long long v;
  3463. ret = kstrtoull(buf, 0, &v);
  3464. if (!ret)
  3465. ret = cft->write_u64(css, cft, v);
  3466. } else if (cft->write_s64) {
  3467. long long v;
  3468. ret = kstrtoll(buf, 0, &v);
  3469. if (!ret)
  3470. ret = cft->write_s64(css, cft, v);
  3471. } else {
  3472. ret = -EINVAL;
  3473. }
  3474. return ret ?: nbytes;
  3475. }
  3476. static __poll_t cgroup_file_poll(struct kernfs_open_file *of, poll_table *pt)
  3477. {
  3478. struct cftype *cft = of_cft(of);
  3479. if (cft->poll)
  3480. return cft->poll(of, pt);
  3481. return kernfs_generic_poll(of, pt);
  3482. }
  3483. static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos)
  3484. {
  3485. return seq_cft(seq)->seq_start(seq, ppos);
  3486. }
  3487. static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos)
  3488. {
  3489. return seq_cft(seq)->seq_next(seq, v, ppos);
  3490. }
  3491. static void cgroup_seqfile_stop(struct seq_file *seq, void *v)
  3492. {
  3493. if (seq_cft(seq)->seq_stop)
  3494. seq_cft(seq)->seq_stop(seq, v);
  3495. }
  3496. static int cgroup_seqfile_show(struct seq_file *m, void *arg)
  3497. {
  3498. struct cftype *cft = seq_cft(m);
  3499. struct cgroup_subsys_state *css = seq_css(m);
  3500. if (cft->seq_show)
  3501. return cft->seq_show(m, arg);
  3502. if (cft->read_u64)
  3503. seq_printf(m, "%llu\n", cft->read_u64(css, cft));
  3504. else if (cft->read_s64)
  3505. seq_printf(m, "%lld\n", cft->read_s64(css, cft));
  3506. else
  3507. return -EINVAL;
  3508. return 0;
  3509. }
  3510. static struct kernfs_ops cgroup_kf_single_ops = {
  3511. .atomic_write_len = PAGE_SIZE,
  3512. .open = cgroup_file_open,
  3513. .release = cgroup_file_release,
  3514. .write = cgroup_file_write,
  3515. .poll = cgroup_file_poll,
  3516. .seq_show = cgroup_seqfile_show,
  3517. };
  3518. static struct kernfs_ops cgroup_kf_ops = {
  3519. .atomic_write_len = PAGE_SIZE,
  3520. .open = cgroup_file_open,
  3521. .release = cgroup_file_release,
  3522. .write = cgroup_file_write,
  3523. .poll = cgroup_file_poll,
  3524. .seq_start = cgroup_seqfile_start,
  3525. .seq_next = cgroup_seqfile_next,
  3526. .seq_stop = cgroup_seqfile_stop,
  3527. .seq_show = cgroup_seqfile_show,
  3528. };
  3529. /* set uid and gid of cgroup dirs and files to that of the creator */
  3530. static int cgroup_kn_set_ugid(struct kernfs_node *kn)
  3531. {
  3532. struct iattr iattr = { .ia_valid = ATTR_UID | ATTR_GID,
  3533. .ia_uid = current_fsuid(),
  3534. .ia_gid = current_fsgid(), };
  3535. if (uid_eq(iattr.ia_uid, GLOBAL_ROOT_UID) &&
  3536. gid_eq(iattr.ia_gid, GLOBAL_ROOT_GID))
  3537. return 0;
  3538. return kernfs_setattr(kn, &iattr);
  3539. }
  3540. static void cgroup_file_notify_timer(struct timer_list *timer)
  3541. {
  3542. cgroup_file_notify(container_of(timer, struct cgroup_file,
  3543. notify_timer));
  3544. }
  3545. static int cgroup_add_file(struct cgroup_subsys_state *css, struct cgroup *cgrp,
  3546. struct cftype *cft)
  3547. {
  3548. char name[CGROUP_FILE_NAME_MAX];
  3549. struct kernfs_node *kn;
  3550. struct lock_class_key *key = NULL;
  3551. int ret;
  3552. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  3553. key = &cft->lockdep_key;
  3554. #endif
  3555. kn = __kernfs_create_file(cgrp->kn, cgroup_file_name(cgrp, cft, name),
  3556. cgroup_file_mode(cft),
  3557. GLOBAL_ROOT_UID, GLOBAL_ROOT_GID,
  3558. 0, cft->kf_ops, cft,
  3559. NULL, key);
  3560. if (IS_ERR(kn))
  3561. return PTR_ERR(kn);
  3562. ret = cgroup_kn_set_ugid(kn);
  3563. if (ret) {
  3564. kernfs_remove(kn);
  3565. return ret;
  3566. }
  3567. if (cft->file_offset) {
  3568. struct cgroup_file *cfile = (void *)css + cft->file_offset;
  3569. timer_setup(&cfile->notify_timer, cgroup_file_notify_timer, 0);
  3570. spin_lock_irq(&cgroup_file_kn_lock);
  3571. cfile->kn = kn;
  3572. spin_unlock_irq(&cgroup_file_kn_lock);
  3573. }
  3574. return 0;
  3575. }
  3576. /**
  3577. * cgroup_addrm_files - add or remove files to a cgroup directory
  3578. * @css: the target css
  3579. * @cgrp: the target cgroup (usually css->cgroup)
  3580. * @cfts: array of cftypes to be added
  3581. * @is_add: whether to add or remove
  3582. *
  3583. * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
  3584. * For removals, this function never fails.
  3585. */
  3586. static int cgroup_addrm_files(struct cgroup_subsys_state *css,
  3587. struct cgroup *cgrp, struct cftype cfts[],
  3588. bool is_add)
  3589. {
  3590. struct cftype *cft, *cft_end = NULL;
  3591. int ret = 0;
  3592. lockdep_assert_held(&cgroup_mutex);
  3593. restart:
  3594. for (cft = cfts; cft != cft_end && cft->name[0] != '\0'; cft++) {
  3595. /* does cft->flags tell us to skip this file on @cgrp? */
  3596. if ((cft->flags & __CFTYPE_ONLY_ON_DFL) && !cgroup_on_dfl(cgrp))
  3597. continue;
  3598. if ((cft->flags & __CFTYPE_NOT_ON_DFL) && cgroup_on_dfl(cgrp))
  3599. continue;
  3600. if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgroup_parent(cgrp))
  3601. continue;
  3602. if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgroup_parent(cgrp))
  3603. continue;
  3604. if ((cft->flags & CFTYPE_DEBUG) && !cgroup_debug)
  3605. continue;
  3606. if (is_add) {
  3607. ret = cgroup_add_file(css, cgrp, cft);
  3608. if (ret) {
  3609. pr_warn("%s: failed to add %s, err=%d\n",
  3610. __func__, cft->name, ret);
  3611. cft_end = cft;
  3612. is_add = false;
  3613. goto restart;
  3614. }
  3615. } else {
  3616. cgroup_rm_file(cgrp, cft);
  3617. }
  3618. }
  3619. return ret;
  3620. }
  3621. static int cgroup_apply_cftypes(struct cftype *cfts, bool is_add)
  3622. {
  3623. struct cgroup_subsys *ss = cfts[0].ss;
  3624. struct cgroup *root = &ss->root->cgrp;
  3625. struct cgroup_subsys_state *css;
  3626. int ret = 0;
  3627. lockdep_assert_held(&cgroup_mutex);
  3628. /* add/rm files for all cgroups created before */
  3629. css_for_each_descendant_pre(css, cgroup_css(root, ss)) {
  3630. struct cgroup *cgrp = css->cgroup;
  3631. if (!(css->flags & CSS_VISIBLE))
  3632. continue;
  3633. ret = cgroup_addrm_files(css, cgrp, cfts, is_add);
  3634. if (ret)
  3635. break;
  3636. }
  3637. if (is_add && !ret)
  3638. kernfs_activate(root->kn);
  3639. return ret;
  3640. }
  3641. static void cgroup_exit_cftypes(struct cftype *cfts)
  3642. {
  3643. struct cftype *cft;
  3644. for (cft = cfts; cft->name[0] != '\0'; cft++) {
  3645. /* free copy for custom atomic_write_len, see init_cftypes() */
  3646. if (cft->max_write_len && cft->max_write_len != PAGE_SIZE)
  3647. kfree(cft->kf_ops);
  3648. cft->kf_ops = NULL;
  3649. cft->ss = NULL;
  3650. /* revert flags set by cgroup core while adding @cfts */
  3651. cft->flags &= ~(__CFTYPE_ONLY_ON_DFL | __CFTYPE_NOT_ON_DFL |
  3652. __CFTYPE_ADDED);
  3653. }
  3654. }
  3655. static int cgroup_init_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
  3656. {
  3657. struct cftype *cft;
  3658. int ret = 0;
  3659. for (cft = cfts; cft->name[0] != '\0'; cft++) {
  3660. struct kernfs_ops *kf_ops;
  3661. WARN_ON(cft->ss || cft->kf_ops);
  3662. if (cft->flags & __CFTYPE_ADDED) {
  3663. ret = -EBUSY;
  3664. break;
  3665. }
  3666. if (cft->seq_start)
  3667. kf_ops = &cgroup_kf_ops;
  3668. else
  3669. kf_ops = &cgroup_kf_single_ops;
  3670. /*
  3671. * Ugh... if @cft wants a custom max_write_len, we need to
  3672. * make a copy of kf_ops to set its atomic_write_len.
  3673. */
  3674. if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) {
  3675. kf_ops = kmemdup(kf_ops, sizeof(*kf_ops), GFP_KERNEL);
  3676. if (!kf_ops) {
  3677. ret = -ENOMEM;
  3678. break;
  3679. }
  3680. kf_ops->atomic_write_len = cft->max_write_len;
  3681. }
  3682. cft->kf_ops = kf_ops;
  3683. cft->ss = ss;
  3684. cft->flags |= __CFTYPE_ADDED;
  3685. }
  3686. if (ret)
  3687. cgroup_exit_cftypes(cfts);
  3688. return ret;
  3689. }
  3690. static int cgroup_rm_cftypes_locked(struct cftype *cfts)
  3691. {
  3692. lockdep_assert_held(&cgroup_mutex);
  3693. list_del(&cfts->node);
  3694. cgroup_apply_cftypes(cfts, false);
  3695. cgroup_exit_cftypes(cfts);
  3696. return 0;
  3697. }
  3698. /**
  3699. * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
  3700. * @cfts: zero-length name terminated array of cftypes
  3701. *
  3702. * Unregister @cfts. Files described by @cfts are removed from all
  3703. * existing cgroups and all future cgroups won't have them either. This
  3704. * function can be called anytime whether @cfts' subsys is attached or not.
  3705. *
  3706. * Returns 0 on successful unregistration, -ENOENT if @cfts is not
  3707. * registered.
  3708. */
  3709. int cgroup_rm_cftypes(struct cftype *cfts)
  3710. {
  3711. int ret;
  3712. if (!cfts || cfts[0].name[0] == '\0')
  3713. return 0;
  3714. if (!(cfts[0].flags & __CFTYPE_ADDED))
  3715. return -ENOENT;
  3716. cgroup_lock();
  3717. ret = cgroup_rm_cftypes_locked(cfts);
  3718. cgroup_unlock();
  3719. return ret;
  3720. }
  3721. /**
  3722. * cgroup_add_cftypes - add an array of cftypes to a subsystem
  3723. * @ss: target cgroup subsystem
  3724. * @cfts: zero-length name terminated array of cftypes
  3725. *
  3726. * Register @cfts to @ss. Files described by @cfts are created for all
  3727. * existing cgroups to which @ss is attached and all future cgroups will
  3728. * have them too. This function can be called anytime whether @ss is
  3729. * attached or not.
  3730. *
  3731. * Returns 0 on successful registration, -errno on failure. Note that this
  3732. * function currently returns 0 as long as @cfts registration is successful
  3733. * even if some file creation attempts on existing cgroups fail.
  3734. */
  3735. static int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
  3736. {
  3737. int ret;
  3738. if (!cgroup_ssid_enabled(ss->id))
  3739. return 0;
  3740. if (!cfts || cfts[0].name[0] == '\0')
  3741. return 0;
  3742. ret = cgroup_init_cftypes(ss, cfts);
  3743. if (ret)
  3744. return ret;
  3745. cgroup_lock();
  3746. list_add_tail(&cfts->node, &ss->cfts);
  3747. ret = cgroup_apply_cftypes(cfts, true);
  3748. if (ret)
  3749. cgroup_rm_cftypes_locked(cfts);
  3750. cgroup_unlock();
  3751. return ret;
  3752. }
  3753. /**
  3754. * cgroup_add_dfl_cftypes - add an array of cftypes for default hierarchy
  3755. * @ss: target cgroup subsystem
  3756. * @cfts: zero-length name terminated array of cftypes
  3757. *
  3758. * Similar to cgroup_add_cftypes() but the added files are only used for
  3759. * the default hierarchy.
  3760. */
  3761. int cgroup_add_dfl_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
  3762. {
  3763. struct cftype *cft;
  3764. for (cft = cfts; cft && cft->name[0] != '\0'; cft++)
  3765. cft->flags |= __CFTYPE_ONLY_ON_DFL;
  3766. return cgroup_add_cftypes(ss, cfts);
  3767. }
  3768. EXPORT_SYMBOL_GPL(cgroup_add_dfl_cftypes);
  3769. /**
  3770. * cgroup_add_legacy_cftypes - add an array of cftypes for legacy hierarchies
  3771. * @ss: target cgroup subsystem
  3772. * @cfts: zero-length name terminated array of cftypes
  3773. *
  3774. * Similar to cgroup_add_cftypes() but the added files are only used for
  3775. * the legacy hierarchies.
  3776. */
  3777. int cgroup_add_legacy_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
  3778. {
  3779. struct cftype *cft;
  3780. for (cft = cfts; cft && cft->name[0] != '\0'; cft++)
  3781. cft->flags |= __CFTYPE_NOT_ON_DFL;
  3782. return cgroup_add_cftypes(ss, cfts);
  3783. }
  3784. EXPORT_SYMBOL_GPL(cgroup_add_legacy_cftypes);
  3785. /**
  3786. * cgroup_file_notify - generate a file modified event for a cgroup_file
  3787. * @cfile: target cgroup_file
  3788. *
  3789. * @cfile must have been obtained by setting cftype->file_offset.
  3790. */
  3791. void cgroup_file_notify(struct cgroup_file *cfile)
  3792. {
  3793. unsigned long flags;
  3794. spin_lock_irqsave(&cgroup_file_kn_lock, flags);
  3795. if (cfile->kn) {
  3796. unsigned long last = cfile->notified_at;
  3797. unsigned long next = last + CGROUP_FILE_NOTIFY_MIN_INTV;
  3798. if (time_in_range(jiffies, last, next)) {
  3799. timer_reduce(&cfile->notify_timer, next);
  3800. } else {
  3801. kernfs_notify(cfile->kn);
  3802. cfile->notified_at = jiffies;
  3803. }
  3804. }
  3805. spin_unlock_irqrestore(&cgroup_file_kn_lock, flags);
  3806. }
  3807. /**
  3808. * cgroup_file_show - show or hide a hidden cgroup file
  3809. * @cfile: target cgroup_file obtained by setting cftype->file_offset
  3810. * @show: whether to show or hide
  3811. */
  3812. void cgroup_file_show(struct cgroup_file *cfile, bool show)
  3813. {
  3814. struct kernfs_node *kn;
  3815. spin_lock_irq(&cgroup_file_kn_lock);
  3816. kn = cfile->kn;
  3817. kernfs_get(kn);
  3818. spin_unlock_irq(&cgroup_file_kn_lock);
  3819. if (kn)
  3820. kernfs_show(kn, show);
  3821. kernfs_put(kn);
  3822. }
  3823. /**
  3824. * css_next_child - find the next child of a given css
  3825. * @pos: the current position (%NULL to initiate traversal)
  3826. * @parent: css whose children to walk
  3827. *
  3828. * This function returns the next child of @parent and should be called
  3829. * under either cgroup_mutex or RCU read lock. The only requirement is
  3830. * that @parent and @pos are accessible. The next sibling is guaranteed to
  3831. * be returned regardless of their states.
  3832. *
  3833. * If a subsystem synchronizes ->css_online() and the start of iteration, a
  3834. * css which finished ->css_online() is guaranteed to be visible in the
  3835. * future iterations and will stay visible until the last reference is put.
  3836. * A css which hasn't finished ->css_online() or already finished
  3837. * ->css_offline() may show up during traversal. It's each subsystem's
  3838. * responsibility to synchronize against on/offlining.
  3839. */
  3840. struct cgroup_subsys_state *css_next_child(struct cgroup_subsys_state *pos,
  3841. struct cgroup_subsys_state *parent)
  3842. {
  3843. struct cgroup_subsys_state *next;
  3844. cgroup_assert_mutex_or_rcu_locked();
  3845. /*
  3846. * @pos could already have been unlinked from the sibling list.
  3847. * Once a cgroup is removed, its ->sibling.next is no longer
  3848. * updated when its next sibling changes. CSS_RELEASED is set when
  3849. * @pos is taken off list, at which time its next pointer is valid,
  3850. * and, as releases are serialized, the one pointed to by the next
  3851. * pointer is guaranteed to not have started release yet. This
  3852. * implies that if we observe !CSS_RELEASED on @pos in this RCU
  3853. * critical section, the one pointed to by its next pointer is
  3854. * guaranteed to not have finished its RCU grace period even if we
  3855. * have dropped rcu_read_lock() in-between iterations.
  3856. *
  3857. * If @pos has CSS_RELEASED set, its next pointer can't be
  3858. * dereferenced; however, as each css is given a monotonically
  3859. * increasing unique serial number and always appended to the
  3860. * sibling list, the next one can be found by walking the parent's
  3861. * children until the first css with higher serial number than
  3862. * @pos's. While this path can be slower, it happens iff iteration
  3863. * races against release and the race window is very small.
  3864. */
  3865. if (!pos) {
  3866. next = list_entry_rcu(parent->children.next, struct cgroup_subsys_state, sibling);
  3867. } else if (likely(!(pos->flags & CSS_RELEASED))) {
  3868. next = list_entry_rcu(pos->sibling.next, struct cgroup_subsys_state, sibling);
  3869. } else {
  3870. list_for_each_entry_rcu(next, &parent->children, sibling,
  3871. lockdep_is_held(&cgroup_mutex))
  3872. if (next->serial_nr > pos->serial_nr)
  3873. break;
  3874. }
  3875. /*
  3876. * @next, if not pointing to the head, can be dereferenced and is
  3877. * the next sibling.
  3878. */
  3879. if (&next->sibling != &parent->children)
  3880. return next;
  3881. return NULL;
  3882. }
  3883. EXPORT_SYMBOL_GPL(css_next_child);
  3884. /**
  3885. * css_next_descendant_pre - find the next descendant for pre-order walk
  3886. * @pos: the current position (%NULL to initiate traversal)
  3887. * @root: css whose descendants to walk
  3888. *
  3889. * To be used by css_for_each_descendant_pre(). Find the next descendant
  3890. * to visit for pre-order traversal of @root's descendants. @root is
  3891. * included in the iteration and the first node to be visited.
  3892. *
  3893. * While this function requires cgroup_mutex or RCU read locking, it
  3894. * doesn't require the whole traversal to be contained in a single critical
  3895. * section. This function will return the correct next descendant as long
  3896. * as both @pos and @root are accessible and @pos is a descendant of @root.
  3897. *
  3898. * If a subsystem synchronizes ->css_online() and the start of iteration, a
  3899. * css which finished ->css_online() is guaranteed to be visible in the
  3900. * future iterations and will stay visible until the last reference is put.
  3901. * A css which hasn't finished ->css_online() or already finished
  3902. * ->css_offline() may show up during traversal. It's each subsystem's
  3903. * responsibility to synchronize against on/offlining.
  3904. */
  3905. struct cgroup_subsys_state *
  3906. css_next_descendant_pre(struct cgroup_subsys_state *pos,
  3907. struct cgroup_subsys_state *root)
  3908. {
  3909. struct cgroup_subsys_state *next;
  3910. cgroup_assert_mutex_or_rcu_locked();
  3911. /* if first iteration, visit @root */
  3912. if (!pos)
  3913. return root;
  3914. /* visit the first child if exists */
  3915. next = css_next_child(NULL, pos);
  3916. if (next)
  3917. return next;
  3918. /* no child, visit my or the closest ancestor's next sibling */
  3919. while (pos != root) {
  3920. next = css_next_child(pos, pos->parent);
  3921. if (next)
  3922. return next;
  3923. pos = pos->parent;
  3924. }
  3925. return NULL;
  3926. }
  3927. EXPORT_SYMBOL_GPL(css_next_descendant_pre);
  3928. /**
  3929. * css_rightmost_descendant - return the rightmost descendant of a css
  3930. * @pos: css of interest
  3931. *
  3932. * Return the rightmost descendant of @pos. If there's no descendant, @pos
  3933. * is returned. This can be used during pre-order traversal to skip
  3934. * subtree of @pos.
  3935. *
  3936. * While this function requires cgroup_mutex or RCU read locking, it
  3937. * doesn't require the whole traversal to be contained in a single critical
  3938. * section. This function will return the correct rightmost descendant as
  3939. * long as @pos is accessible.
  3940. */
  3941. struct cgroup_subsys_state *
  3942. css_rightmost_descendant(struct cgroup_subsys_state *pos)
  3943. {
  3944. struct cgroup_subsys_state *last, *tmp;
  3945. cgroup_assert_mutex_or_rcu_locked();
  3946. do {
  3947. last = pos;
  3948. /* ->prev isn't RCU safe, walk ->next till the end */
  3949. pos = NULL;
  3950. css_for_each_child(tmp, last)
  3951. pos = tmp;
  3952. } while (pos);
  3953. return last;
  3954. }
  3955. static struct cgroup_subsys_state *
  3956. css_leftmost_descendant(struct cgroup_subsys_state *pos)
  3957. {
  3958. struct cgroup_subsys_state *last;
  3959. do {
  3960. last = pos;
  3961. pos = css_next_child(NULL, pos);
  3962. } while (pos);
  3963. return last;
  3964. }
  3965. /**
  3966. * css_next_descendant_post - find the next descendant for post-order walk
  3967. * @pos: the current position (%NULL to initiate traversal)
  3968. * @root: css whose descendants to walk
  3969. *
  3970. * To be used by css_for_each_descendant_post(). Find the next descendant
  3971. * to visit for post-order traversal of @root's descendants. @root is
  3972. * included in the iteration and the last node to be visited.
  3973. *
  3974. * While this function requires cgroup_mutex or RCU read locking, it
  3975. * doesn't require the whole traversal to be contained in a single critical
  3976. * section. This function will return the correct next descendant as long
  3977. * as both @pos and @cgroup are accessible and @pos is a descendant of
  3978. * @cgroup.
  3979. *
  3980. * If a subsystem synchronizes ->css_online() and the start of iteration, a
  3981. * css which finished ->css_online() is guaranteed to be visible in the
  3982. * future iterations and will stay visible until the last reference is put.
  3983. * A css which hasn't finished ->css_online() or already finished
  3984. * ->css_offline() may show up during traversal. It's each subsystem's
  3985. * responsibility to synchronize against on/offlining.
  3986. */
  3987. struct cgroup_subsys_state *
  3988. css_next_descendant_post(struct cgroup_subsys_state *pos,
  3989. struct cgroup_subsys_state *root)
  3990. {
  3991. struct cgroup_subsys_state *next;
  3992. cgroup_assert_mutex_or_rcu_locked();
  3993. /* if first iteration, visit leftmost descendant which may be @root */
  3994. if (!pos)
  3995. return css_leftmost_descendant(root);
  3996. /* if we visited @root, we're done */
  3997. if (pos == root)
  3998. return NULL;
  3999. /* if there's an unvisited sibling, visit its leftmost descendant */
  4000. next = css_next_child(pos, pos->parent);
  4001. if (next)
  4002. return css_leftmost_descendant(next);
  4003. /* no sibling left, visit parent */
  4004. return pos->parent;
  4005. }
  4006. /**
  4007. * css_has_online_children - does a css have online children
  4008. * @css: the target css
  4009. *
  4010. * Returns %true if @css has any online children; otherwise, %false. This
  4011. * function can be called from any context but the caller is responsible
  4012. * for synchronizing against on/offlining as necessary.
  4013. */
  4014. bool css_has_online_children(struct cgroup_subsys_state *css)
  4015. {
  4016. struct cgroup_subsys_state *child;
  4017. bool ret = false;
  4018. rcu_read_lock();
  4019. css_for_each_child(child, css) {
  4020. if (child->flags & CSS_ONLINE) {
  4021. ret = true;
  4022. break;
  4023. }
  4024. }
  4025. rcu_read_unlock();
  4026. return ret;
  4027. }
  4028. static struct css_set *css_task_iter_next_css_set(struct css_task_iter *it)
  4029. {
  4030. struct list_head *l;
  4031. struct cgrp_cset_link *link;
  4032. struct css_set *cset;
  4033. lockdep_assert_held(&css_set_lock);
  4034. /* find the next threaded cset */
  4035. if (it->tcset_pos) {
  4036. l = it->tcset_pos->next;
  4037. if (l != it->tcset_head) {
  4038. it->tcset_pos = l;
  4039. return container_of(l, struct css_set,
  4040. threaded_csets_node);
  4041. }
  4042. it->tcset_pos = NULL;
  4043. }
  4044. /* find the next cset */
  4045. l = it->cset_pos;
  4046. l = l->next;
  4047. if (l == it->cset_head) {
  4048. it->cset_pos = NULL;
  4049. return NULL;
  4050. }
  4051. if (it->ss) {
  4052. cset = container_of(l, struct css_set, e_cset_node[it->ss->id]);
  4053. } else {
  4054. link = list_entry(l, struct cgrp_cset_link, cset_link);
  4055. cset = link->cset;
  4056. }
  4057. it->cset_pos = l;
  4058. /* initialize threaded css_set walking */
  4059. if (it->flags & CSS_TASK_ITER_THREADED) {
  4060. if (it->cur_dcset)
  4061. put_css_set_locked(it->cur_dcset);
  4062. it->cur_dcset = cset;
  4063. get_css_set(cset);
  4064. it->tcset_head = &cset->threaded_csets;
  4065. it->tcset_pos = &cset->threaded_csets;
  4066. }
  4067. return cset;
  4068. }
  4069. /**
  4070. * css_task_iter_advance_css_set - advance a task iterator to the next css_set
  4071. * @it: the iterator to advance
  4072. *
  4073. * Advance @it to the next css_set to walk.
  4074. */
  4075. static void css_task_iter_advance_css_set(struct css_task_iter *it)
  4076. {
  4077. struct css_set *cset;
  4078. lockdep_assert_held(&css_set_lock);
  4079. /* Advance to the next non-empty css_set and find first non-empty tasks list*/
  4080. while ((cset = css_task_iter_next_css_set(it))) {
  4081. if (!list_empty(&cset->tasks)) {
  4082. it->cur_tasks_head = &cset->tasks;
  4083. break;
  4084. } else if (!list_empty(&cset->mg_tasks)) {
  4085. it->cur_tasks_head = &cset->mg_tasks;
  4086. break;
  4087. } else if (!list_empty(&cset->dying_tasks)) {
  4088. it->cur_tasks_head = &cset->dying_tasks;
  4089. break;
  4090. }
  4091. }
  4092. if (!cset) {
  4093. it->task_pos = NULL;
  4094. return;
  4095. }
  4096. it->task_pos = it->cur_tasks_head->next;
  4097. /*
  4098. * We don't keep css_sets locked across iteration steps and thus
  4099. * need to take steps to ensure that iteration can be resumed after
  4100. * the lock is re-acquired. Iteration is performed at two levels -
  4101. * css_sets and tasks in them.
  4102. *
  4103. * Once created, a css_set never leaves its cgroup lists, so a
  4104. * pinned css_set is guaranteed to stay put and we can resume
  4105. * iteration afterwards.
  4106. *
  4107. * Tasks may leave @cset across iteration steps. This is resolved
  4108. * by registering each iterator with the css_set currently being
  4109. * walked and making css_set_move_task() advance iterators whose
  4110. * next task is leaving.
  4111. */
  4112. if (it->cur_cset) {
  4113. list_del(&it->iters_node);
  4114. put_css_set_locked(it->cur_cset);
  4115. }
  4116. get_css_set(cset);
  4117. it->cur_cset = cset;
  4118. list_add(&it->iters_node, &cset->task_iters);
  4119. }
  4120. static void css_task_iter_skip(struct css_task_iter *it,
  4121. struct task_struct *task)
  4122. {
  4123. lockdep_assert_held(&css_set_lock);
  4124. if (it->task_pos == &task->cg_list) {
  4125. it->task_pos = it->task_pos->next;
  4126. it->flags |= CSS_TASK_ITER_SKIPPED;
  4127. }
  4128. }
  4129. static void css_task_iter_advance(struct css_task_iter *it)
  4130. {
  4131. struct task_struct *task;
  4132. lockdep_assert_held(&css_set_lock);
  4133. repeat:
  4134. if (it->task_pos) {
  4135. /*
  4136. * Advance iterator to find next entry. We go through cset
  4137. * tasks, mg_tasks and dying_tasks, when consumed we move onto
  4138. * the next cset.
  4139. */
  4140. if (it->flags & CSS_TASK_ITER_SKIPPED)
  4141. it->flags &= ~CSS_TASK_ITER_SKIPPED;
  4142. else
  4143. it->task_pos = it->task_pos->next;
  4144. if (it->task_pos == &it->cur_cset->tasks) {
  4145. it->cur_tasks_head = &it->cur_cset->mg_tasks;
  4146. it->task_pos = it->cur_tasks_head->next;
  4147. }
  4148. if (it->task_pos == &it->cur_cset->mg_tasks) {
  4149. it->cur_tasks_head = &it->cur_cset->dying_tasks;
  4150. it->task_pos = it->cur_tasks_head->next;
  4151. }
  4152. if (it->task_pos == &it->cur_cset->dying_tasks)
  4153. css_task_iter_advance_css_set(it);
  4154. } else {
  4155. /* called from start, proceed to the first cset */
  4156. css_task_iter_advance_css_set(it);
  4157. }
  4158. if (!it->task_pos)
  4159. return;
  4160. task = list_entry(it->task_pos, struct task_struct, cg_list);
  4161. if (it->flags & CSS_TASK_ITER_PROCS) {
  4162. /* if PROCS, skip over tasks which aren't group leaders */
  4163. if (!thread_group_leader(task))
  4164. goto repeat;
  4165. /* and dying leaders w/o live member threads */
  4166. if (it->cur_tasks_head == &it->cur_cset->dying_tasks &&
  4167. !atomic_read(&task->signal->live))
  4168. goto repeat;
  4169. } else {
  4170. /* skip all dying ones */
  4171. if (it->cur_tasks_head == &it->cur_cset->dying_tasks)
  4172. goto repeat;
  4173. }
  4174. }
  4175. /**
  4176. * css_task_iter_start - initiate task iteration
  4177. * @css: the css to walk tasks of
  4178. * @flags: CSS_TASK_ITER_* flags
  4179. * @it: the task iterator to use
  4180. *
  4181. * Initiate iteration through the tasks of @css. The caller can call
  4182. * css_task_iter_next() to walk through the tasks until the function
  4183. * returns NULL. On completion of iteration, css_task_iter_end() must be
  4184. * called.
  4185. */
  4186. void css_task_iter_start(struct cgroup_subsys_state *css, unsigned int flags,
  4187. struct css_task_iter *it)
  4188. {
  4189. memset(it, 0, sizeof(*it));
  4190. spin_lock_irq(&css_set_lock);
  4191. it->ss = css->ss;
  4192. it->flags = flags;
  4193. if (CGROUP_HAS_SUBSYS_CONFIG && it->ss)
  4194. it->cset_pos = &css->cgroup->e_csets[css->ss->id];
  4195. else
  4196. it->cset_pos = &css->cgroup->cset_links;
  4197. it->cset_head = it->cset_pos;
  4198. css_task_iter_advance(it);
  4199. spin_unlock_irq(&css_set_lock);
  4200. }
  4201. /**
  4202. * css_task_iter_next - return the next task for the iterator
  4203. * @it: the task iterator being iterated
  4204. *
  4205. * The "next" function for task iteration. @it should have been
  4206. * initialized via css_task_iter_start(). Returns NULL when the iteration
  4207. * reaches the end.
  4208. */
  4209. struct task_struct *css_task_iter_next(struct css_task_iter *it)
  4210. {
  4211. if (it->cur_task) {
  4212. put_task_struct(it->cur_task);
  4213. it->cur_task = NULL;
  4214. }
  4215. spin_lock_irq(&css_set_lock);
  4216. /* @it may be half-advanced by skips, finish advancing */
  4217. if (it->flags & CSS_TASK_ITER_SKIPPED)
  4218. css_task_iter_advance(it);
  4219. if (it->task_pos) {
  4220. it->cur_task = list_entry(it->task_pos, struct task_struct,
  4221. cg_list);
  4222. get_task_struct(it->cur_task);
  4223. css_task_iter_advance(it);
  4224. }
  4225. spin_unlock_irq(&css_set_lock);
  4226. return it->cur_task;
  4227. }
  4228. /**
  4229. * css_task_iter_end - finish task iteration
  4230. * @it: the task iterator to finish
  4231. *
  4232. * Finish task iteration started by css_task_iter_start().
  4233. */
  4234. void css_task_iter_end(struct css_task_iter *it)
  4235. {
  4236. if (it->cur_cset) {
  4237. spin_lock_irq(&css_set_lock);
  4238. list_del(&it->iters_node);
  4239. put_css_set_locked(it->cur_cset);
  4240. spin_unlock_irq(&css_set_lock);
  4241. }
  4242. if (it->cur_dcset)
  4243. put_css_set(it->cur_dcset);
  4244. if (it->cur_task)
  4245. put_task_struct(it->cur_task);
  4246. }
  4247. static void cgroup_procs_release(struct kernfs_open_file *of)
  4248. {
  4249. struct cgroup_file_ctx *ctx = of->priv;
  4250. if (ctx->procs.started)
  4251. css_task_iter_end(&ctx->procs.iter);
  4252. }
  4253. static void *cgroup_procs_next(struct seq_file *s, void *v, loff_t *pos)
  4254. {
  4255. struct kernfs_open_file *of = s->private;
  4256. struct cgroup_file_ctx *ctx = of->priv;
  4257. if (pos)
  4258. (*pos)++;
  4259. return css_task_iter_next(&ctx->procs.iter);
  4260. }
  4261. static void *__cgroup_procs_start(struct seq_file *s, loff_t *pos,
  4262. unsigned int iter_flags)
  4263. {
  4264. struct kernfs_open_file *of = s->private;
  4265. struct cgroup *cgrp = seq_css(s)->cgroup;
  4266. struct cgroup_file_ctx *ctx = of->priv;
  4267. struct css_task_iter *it = &ctx->procs.iter;
  4268. /*
  4269. * When a seq_file is seeked, it's always traversed sequentially
  4270. * from position 0, so we can simply keep iterating on !0 *pos.
  4271. */
  4272. if (!ctx->procs.started) {
  4273. if (WARN_ON_ONCE((*pos)))
  4274. return ERR_PTR(-EINVAL);
  4275. css_task_iter_start(&cgrp->self, iter_flags, it);
  4276. ctx->procs.started = true;
  4277. } else if (!(*pos)) {
  4278. css_task_iter_end(it);
  4279. css_task_iter_start(&cgrp->self, iter_flags, it);
  4280. } else
  4281. return it->cur_task;
  4282. return cgroup_procs_next(s, NULL, NULL);
  4283. }
  4284. static void *cgroup_procs_start(struct seq_file *s, loff_t *pos)
  4285. {
  4286. struct cgroup *cgrp = seq_css(s)->cgroup;
  4287. /*
  4288. * All processes of a threaded subtree belong to the domain cgroup
  4289. * of the subtree. Only threads can be distributed across the
  4290. * subtree. Reject reads on cgroup.procs in the subtree proper.
  4291. * They're always empty anyway.
  4292. */
  4293. if (cgroup_is_threaded(cgrp))
  4294. return ERR_PTR(-EOPNOTSUPP);
  4295. return __cgroup_procs_start(s, pos, CSS_TASK_ITER_PROCS |
  4296. CSS_TASK_ITER_THREADED);
  4297. }
  4298. static int cgroup_procs_show(struct seq_file *s, void *v)
  4299. {
  4300. seq_printf(s, "%d\n", task_pid_vnr(v));
  4301. return 0;
  4302. }
  4303. static int cgroup_may_write(const struct cgroup *cgrp, struct super_block *sb)
  4304. {
  4305. int ret;
  4306. struct inode *inode;
  4307. lockdep_assert_held(&cgroup_mutex);
  4308. inode = kernfs_get_inode(sb, cgrp->procs_file.kn);
  4309. if (!inode)
  4310. return -ENOMEM;
  4311. ret = inode_permission(&init_user_ns, inode, MAY_WRITE);
  4312. iput(inode);
  4313. return ret;
  4314. }
  4315. static int cgroup_procs_write_permission(struct cgroup *src_cgrp,
  4316. struct cgroup *dst_cgrp,
  4317. struct super_block *sb,
  4318. struct cgroup_namespace *ns)
  4319. {
  4320. struct cgroup *com_cgrp = src_cgrp;
  4321. int ret;
  4322. lockdep_assert_held(&cgroup_mutex);
  4323. /* find the common ancestor */
  4324. while (!cgroup_is_descendant(dst_cgrp, com_cgrp))
  4325. com_cgrp = cgroup_parent(com_cgrp);
  4326. /* %current should be authorized to migrate to the common ancestor */
  4327. ret = cgroup_may_write(com_cgrp, sb);
  4328. if (ret)
  4329. return ret;
  4330. /*
  4331. * If namespaces are delegation boundaries, %current must be able
  4332. * to see both source and destination cgroups from its namespace.
  4333. */
  4334. if ((cgrp_dfl_root.flags & CGRP_ROOT_NS_DELEGATE) &&
  4335. (!cgroup_is_descendant(src_cgrp, ns->root_cset->dfl_cgrp) ||
  4336. !cgroup_is_descendant(dst_cgrp, ns->root_cset->dfl_cgrp)))
  4337. return -ENOENT;
  4338. return 0;
  4339. }
  4340. static int cgroup_attach_permissions(struct cgroup *src_cgrp,
  4341. struct cgroup *dst_cgrp,
  4342. struct super_block *sb, bool threadgroup,
  4343. struct cgroup_namespace *ns)
  4344. {
  4345. int ret = 0;
  4346. ret = cgroup_procs_write_permission(src_cgrp, dst_cgrp, sb, ns);
  4347. if (ret)
  4348. return ret;
  4349. ret = cgroup_migrate_vet_dst(dst_cgrp);
  4350. if (ret)
  4351. return ret;
  4352. if (!threadgroup && (src_cgrp->dom_cgrp != dst_cgrp->dom_cgrp))
  4353. ret = -EOPNOTSUPP;
  4354. return ret;
  4355. }
  4356. static ssize_t __cgroup_procs_write(struct kernfs_open_file *of, char *buf,
  4357. bool threadgroup)
  4358. {
  4359. struct cgroup_file_ctx *ctx = of->priv;
  4360. struct cgroup *src_cgrp, *dst_cgrp;
  4361. struct task_struct *task;
  4362. const struct cred *saved_cred;
  4363. ssize_t ret;
  4364. bool threadgroup_locked;
  4365. dst_cgrp = cgroup_kn_lock_live(of->kn, false);
  4366. if (!dst_cgrp)
  4367. return -ENODEV;
  4368. task = cgroup_procs_write_start(buf, threadgroup, &threadgroup_locked, dst_cgrp);
  4369. ret = PTR_ERR_OR_ZERO(task);
  4370. if (ret)
  4371. goto out_unlock;
  4372. /* find the source cgroup */
  4373. spin_lock_irq(&css_set_lock);
  4374. src_cgrp = task_cgroup_from_root(task, &cgrp_dfl_root);
  4375. spin_unlock_irq(&css_set_lock);
  4376. /*
  4377. * Process and thread migrations follow same delegation rule. Check
  4378. * permissions using the credentials from file open to protect against
  4379. * inherited fd attacks.
  4380. */
  4381. saved_cred = override_creds(of->file->f_cred);
  4382. ret = cgroup_attach_permissions(src_cgrp, dst_cgrp,
  4383. of->file->f_path.dentry->d_sb,
  4384. threadgroup, ctx->ns);
  4385. revert_creds(saved_cred);
  4386. if (ret)
  4387. goto out_finish;
  4388. ret = cgroup_attach_task(dst_cgrp, task, threadgroup);
  4389. out_finish:
  4390. cgroup_procs_write_finish(task, threadgroup_locked);
  4391. out_unlock:
  4392. cgroup_kn_unlock(of->kn);
  4393. return ret;
  4394. }
  4395. static ssize_t cgroup_procs_write(struct kernfs_open_file *of,
  4396. char *buf, size_t nbytes, loff_t off)
  4397. {
  4398. return __cgroup_procs_write(of, buf, true) ?: nbytes;
  4399. }
  4400. static void *cgroup_threads_start(struct seq_file *s, loff_t *pos)
  4401. {
  4402. return __cgroup_procs_start(s, pos, 0);
  4403. }
  4404. static ssize_t cgroup_threads_write(struct kernfs_open_file *of,
  4405. char *buf, size_t nbytes, loff_t off)
  4406. {
  4407. return __cgroup_procs_write(of, buf, false) ?: nbytes;
  4408. }
  4409. /* cgroup core interface files for the default hierarchy */
  4410. static struct cftype cgroup_base_files[] = {
  4411. {
  4412. .name = "cgroup.type",
  4413. .flags = CFTYPE_NOT_ON_ROOT,
  4414. .seq_show = cgroup_type_show,
  4415. .write = cgroup_type_write,
  4416. },
  4417. {
  4418. .name = "cgroup.procs",
  4419. .flags = CFTYPE_NS_DELEGATABLE,
  4420. .file_offset = offsetof(struct cgroup, procs_file),
  4421. .release = cgroup_procs_release,
  4422. .seq_start = cgroup_procs_start,
  4423. .seq_next = cgroup_procs_next,
  4424. .seq_show = cgroup_procs_show,
  4425. .write = cgroup_procs_write,
  4426. },
  4427. {
  4428. .name = "cgroup.threads",
  4429. .flags = CFTYPE_NS_DELEGATABLE,
  4430. .release = cgroup_procs_release,
  4431. .seq_start = cgroup_threads_start,
  4432. .seq_next = cgroup_procs_next,
  4433. .seq_show = cgroup_procs_show,
  4434. .write = cgroup_threads_write,
  4435. },
  4436. {
  4437. .name = "cgroup.controllers",
  4438. .seq_show = cgroup_controllers_show,
  4439. },
  4440. {
  4441. .name = "cgroup.subtree_control",
  4442. .flags = CFTYPE_NS_DELEGATABLE,
  4443. .seq_show = cgroup_subtree_control_show,
  4444. .write = cgroup_subtree_control_write,
  4445. },
  4446. {
  4447. .name = "cgroup.events",
  4448. .flags = CFTYPE_NOT_ON_ROOT,
  4449. .file_offset = offsetof(struct cgroup, events_file),
  4450. .seq_show = cgroup_events_show,
  4451. },
  4452. {
  4453. .name = "cgroup.max.descendants",
  4454. .seq_show = cgroup_max_descendants_show,
  4455. .write = cgroup_max_descendants_write,
  4456. },
  4457. {
  4458. .name = "cgroup.max.depth",
  4459. .seq_show = cgroup_max_depth_show,
  4460. .write = cgroup_max_depth_write,
  4461. },
  4462. {
  4463. .name = "cgroup.stat",
  4464. .seq_show = cgroup_stat_show,
  4465. },
  4466. {
  4467. .name = "cgroup.freeze",
  4468. .flags = CFTYPE_NOT_ON_ROOT,
  4469. .seq_show = cgroup_freeze_show,
  4470. .write = cgroup_freeze_write,
  4471. },
  4472. {
  4473. .name = "cgroup.kill",
  4474. .flags = CFTYPE_NOT_ON_ROOT,
  4475. .write = cgroup_kill_write,
  4476. },
  4477. {
  4478. .name = "cpu.stat",
  4479. .seq_show = cpu_stat_show,
  4480. },
  4481. { } /* terminate */
  4482. };
  4483. static struct cftype cgroup_psi_files[] = {
  4484. #ifdef CONFIG_PSI
  4485. {
  4486. .name = "io.pressure",
  4487. .file_offset = offsetof(struct cgroup, psi_files[PSI_IO]),
  4488. .open = cgroup_pressure_open,
  4489. .seq_show = cgroup_io_pressure_show,
  4490. .write = cgroup_io_pressure_write,
  4491. .poll = cgroup_pressure_poll,
  4492. .release = cgroup_pressure_release,
  4493. },
  4494. {
  4495. .name = "memory.pressure",
  4496. .file_offset = offsetof(struct cgroup, psi_files[PSI_MEM]),
  4497. .open = cgroup_pressure_open,
  4498. .seq_show = cgroup_memory_pressure_show,
  4499. .write = cgroup_memory_pressure_write,
  4500. .poll = cgroup_pressure_poll,
  4501. .release = cgroup_pressure_release,
  4502. },
  4503. {
  4504. .name = "cpu.pressure",
  4505. .file_offset = offsetof(struct cgroup, psi_files[PSI_CPU]),
  4506. .open = cgroup_pressure_open,
  4507. .seq_show = cgroup_cpu_pressure_show,
  4508. .write = cgroup_cpu_pressure_write,
  4509. .poll = cgroup_pressure_poll,
  4510. .release = cgroup_pressure_release,
  4511. },
  4512. #ifdef CONFIG_IRQ_TIME_ACCOUNTING
  4513. {
  4514. .name = "irq.pressure",
  4515. .file_offset = offsetof(struct cgroup, psi_files[PSI_IRQ]),
  4516. .open = cgroup_pressure_open,
  4517. .seq_show = cgroup_irq_pressure_show,
  4518. .write = cgroup_irq_pressure_write,
  4519. .poll = cgroup_pressure_poll,
  4520. .release = cgroup_pressure_release,
  4521. },
  4522. #endif
  4523. {
  4524. .name = "cgroup.pressure",
  4525. .seq_show = cgroup_pressure_show,
  4526. .write = cgroup_pressure_write,
  4527. },
  4528. #endif /* CONFIG_PSI */
  4529. { } /* terminate */
  4530. };
  4531. /*
  4532. * css destruction is four-stage process.
  4533. *
  4534. * 1. Destruction starts. Killing of the percpu_ref is initiated.
  4535. * Implemented in kill_css().
  4536. *
  4537. * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
  4538. * and thus css_tryget_online() is guaranteed to fail, the css can be
  4539. * offlined by invoking offline_css(). After offlining, the base ref is
  4540. * put. Implemented in css_killed_work_fn().
  4541. *
  4542. * 3. When the percpu_ref reaches zero, the only possible remaining
  4543. * accessors are inside RCU read sections. css_release() schedules the
  4544. * RCU callback.
  4545. *
  4546. * 4. After the grace period, the css can be freed. Implemented in
  4547. * css_free_work_fn().
  4548. *
  4549. * It is actually hairier because both step 2 and 4 require process context
  4550. * and thus involve punting to css->destroy_work adding two additional
  4551. * steps to the already complex sequence.
  4552. */
  4553. static void css_free_rwork_fn(struct work_struct *work)
  4554. {
  4555. struct cgroup_subsys_state *css = container_of(to_rcu_work(work),
  4556. struct cgroup_subsys_state, destroy_rwork);
  4557. struct cgroup_subsys *ss = css->ss;
  4558. struct cgroup *cgrp = css->cgroup;
  4559. percpu_ref_exit(&css->refcnt);
  4560. if (ss) {
  4561. /* css free path */
  4562. struct cgroup_subsys_state *parent = css->parent;
  4563. int id = css->id;
  4564. ss->css_free(css);
  4565. cgroup_idr_remove(&ss->css_idr, id);
  4566. cgroup_put(cgrp);
  4567. if (parent)
  4568. css_put(parent);
  4569. } else {
  4570. /* cgroup free path */
  4571. atomic_dec(&cgrp->root->nr_cgrps);
  4572. cgroup1_pidlist_destroy_all(cgrp);
  4573. cancel_work_sync(&cgrp->release_agent_work);
  4574. if (cgroup_parent(cgrp)) {
  4575. /*
  4576. * We get a ref to the parent, and put the ref when
  4577. * this cgroup is being freed, so it's guaranteed
  4578. * that the parent won't be destroyed before its
  4579. * children.
  4580. */
  4581. cgroup_put(cgroup_parent(cgrp));
  4582. kernfs_put(cgrp->kn);
  4583. psi_cgroup_free(cgrp);
  4584. cgroup_rstat_exit(cgrp);
  4585. kfree(cgrp);
  4586. } else {
  4587. /*
  4588. * This is root cgroup's refcnt reaching zero,
  4589. * which indicates that the root should be
  4590. * released.
  4591. */
  4592. cgroup_destroy_root(cgrp->root);
  4593. }
  4594. }
  4595. }
  4596. static void css_release_work_fn(struct work_struct *work)
  4597. {
  4598. struct cgroup_subsys_state *css =
  4599. container_of(work, struct cgroup_subsys_state, destroy_work);
  4600. struct cgroup_subsys *ss = css->ss;
  4601. struct cgroup *cgrp = css->cgroup;
  4602. cgroup_lock();
  4603. css->flags |= CSS_RELEASED;
  4604. list_del_rcu(&css->sibling);
  4605. if (ss) {
  4606. /* css release path */
  4607. if (!list_empty(&css->rstat_css_node)) {
  4608. cgroup_rstat_flush(cgrp);
  4609. list_del_rcu(&css->rstat_css_node);
  4610. }
  4611. cgroup_idr_replace(&ss->css_idr, NULL, css->id);
  4612. if (ss->css_released)
  4613. ss->css_released(css);
  4614. } else {
  4615. struct cgroup *tcgrp;
  4616. /* cgroup release path */
  4617. TRACE_CGROUP_PATH(release, cgrp);
  4618. cgroup_rstat_flush(cgrp);
  4619. spin_lock_irq(&css_set_lock);
  4620. for (tcgrp = cgroup_parent(cgrp); tcgrp;
  4621. tcgrp = cgroup_parent(tcgrp))
  4622. tcgrp->nr_dying_descendants--;
  4623. spin_unlock_irq(&css_set_lock);
  4624. /*
  4625. * There are two control paths which try to determine
  4626. * cgroup from dentry without going through kernfs -
  4627. * cgroupstats_build() and css_tryget_online_from_dir().
  4628. * Those are supported by RCU protecting clearing of
  4629. * cgrp->kn->priv backpointer.
  4630. */
  4631. if (cgrp->kn)
  4632. RCU_INIT_POINTER(*(void __rcu __force **)&cgrp->kn->priv,
  4633. NULL);
  4634. }
  4635. cgroup_unlock();
  4636. INIT_RCU_WORK(&css->destroy_rwork, css_free_rwork_fn);
  4637. queue_rcu_work(cgroup_destroy_wq, &css->destroy_rwork);
  4638. }
  4639. static void css_release(struct percpu_ref *ref)
  4640. {
  4641. struct cgroup_subsys_state *css =
  4642. container_of(ref, struct cgroup_subsys_state, refcnt);
  4643. INIT_WORK(&css->destroy_work, css_release_work_fn);
  4644. queue_work(cgroup_destroy_wq, &css->destroy_work);
  4645. }
  4646. static void init_and_link_css(struct cgroup_subsys_state *css,
  4647. struct cgroup_subsys *ss, struct cgroup *cgrp)
  4648. {
  4649. lockdep_assert_held(&cgroup_mutex);
  4650. cgroup_get_live(cgrp);
  4651. memset(css, 0, sizeof(*css));
  4652. css->cgroup = cgrp;
  4653. css->ss = ss;
  4654. css->id = -1;
  4655. INIT_LIST_HEAD(&css->sibling);
  4656. INIT_LIST_HEAD(&css->children);
  4657. INIT_LIST_HEAD(&css->rstat_css_node);
  4658. css->serial_nr = css_serial_nr_next++;
  4659. atomic_set(&css->online_cnt, 0);
  4660. if (cgroup_parent(cgrp)) {
  4661. css->parent = cgroup_css(cgroup_parent(cgrp), ss);
  4662. css_get(css->parent);
  4663. }
  4664. if (ss->css_rstat_flush)
  4665. list_add_rcu(&css->rstat_css_node, &cgrp->rstat_css_list);
  4666. BUG_ON(cgroup_css(cgrp, ss));
  4667. }
  4668. /* invoke ->css_online() on a new CSS and mark it online if successful */
  4669. static int online_css(struct cgroup_subsys_state *css)
  4670. {
  4671. struct cgroup_subsys *ss = css->ss;
  4672. int ret = 0;
  4673. lockdep_assert_held(&cgroup_mutex);
  4674. if (ss->css_online)
  4675. ret = ss->css_online(css);
  4676. if (!ret) {
  4677. css->flags |= CSS_ONLINE;
  4678. rcu_assign_pointer(css->cgroup->subsys[ss->id], css);
  4679. atomic_inc(&css->online_cnt);
  4680. if (css->parent)
  4681. atomic_inc(&css->parent->online_cnt);
  4682. }
  4683. return ret;
  4684. }
  4685. /* if the CSS is online, invoke ->css_offline() on it and mark it offline */
  4686. static void offline_css(struct cgroup_subsys_state *css)
  4687. {
  4688. struct cgroup_subsys *ss = css->ss;
  4689. lockdep_assert_held(&cgroup_mutex);
  4690. if (!(css->flags & CSS_ONLINE))
  4691. return;
  4692. if (ss->css_offline)
  4693. ss->css_offline(css);
  4694. css->flags &= ~CSS_ONLINE;
  4695. RCU_INIT_POINTER(css->cgroup->subsys[ss->id], NULL);
  4696. wake_up_all(&css->cgroup->offline_waitq);
  4697. }
  4698. /**
  4699. * css_create - create a cgroup_subsys_state
  4700. * @cgrp: the cgroup new css will be associated with
  4701. * @ss: the subsys of new css
  4702. *
  4703. * Create a new css associated with @cgrp - @ss pair. On success, the new
  4704. * css is online and installed in @cgrp. This function doesn't create the
  4705. * interface files. Returns 0 on success, -errno on failure.
  4706. */
  4707. static struct cgroup_subsys_state *css_create(struct cgroup *cgrp,
  4708. struct cgroup_subsys *ss)
  4709. {
  4710. struct cgroup *parent = cgroup_parent(cgrp);
  4711. struct cgroup_subsys_state *parent_css = cgroup_css(parent, ss);
  4712. struct cgroup_subsys_state *css;
  4713. int err;
  4714. lockdep_assert_held(&cgroup_mutex);
  4715. css = ss->css_alloc(parent_css);
  4716. if (!css)
  4717. css = ERR_PTR(-ENOMEM);
  4718. if (IS_ERR(css))
  4719. return css;
  4720. init_and_link_css(css, ss, cgrp);
  4721. err = percpu_ref_init(&css->refcnt, css_release, 0, GFP_KERNEL);
  4722. if (err)
  4723. goto err_free_css;
  4724. err = cgroup_idr_alloc(&ss->css_idr, NULL, 2, 0, GFP_KERNEL);
  4725. if (err < 0)
  4726. goto err_free_css;
  4727. css->id = err;
  4728. /* @css is ready to be brought online now, make it visible */
  4729. list_add_tail_rcu(&css->sibling, &parent_css->children);
  4730. cgroup_idr_replace(&ss->css_idr, css, css->id);
  4731. err = online_css(css);
  4732. if (err)
  4733. goto err_list_del;
  4734. return css;
  4735. err_list_del:
  4736. list_del_rcu(&css->sibling);
  4737. err_free_css:
  4738. list_del_rcu(&css->rstat_css_node);
  4739. INIT_RCU_WORK(&css->destroy_rwork, css_free_rwork_fn);
  4740. queue_rcu_work(cgroup_destroy_wq, &css->destroy_rwork);
  4741. return ERR_PTR(err);
  4742. }
  4743. /*
  4744. * The returned cgroup is fully initialized including its control mask, but
  4745. * it isn't associated with its kernfs_node and doesn't have the control
  4746. * mask applied.
  4747. */
  4748. static struct cgroup *cgroup_create(struct cgroup *parent, const char *name,
  4749. umode_t mode)
  4750. {
  4751. struct cgroup_root *root = parent->root;
  4752. struct cgroup *cgrp, *tcgrp;
  4753. struct kernfs_node *kn;
  4754. int level = parent->level + 1;
  4755. int ret;
  4756. /* allocate the cgroup and its ID, 0 is reserved for the root */
  4757. cgrp = kzalloc(struct_size(cgrp, ancestors, (level + 1)), GFP_KERNEL);
  4758. if (!cgrp)
  4759. return ERR_PTR(-ENOMEM);
  4760. ret = percpu_ref_init(&cgrp->self.refcnt, css_release, 0, GFP_KERNEL);
  4761. if (ret)
  4762. goto out_free_cgrp;
  4763. ret = cgroup_rstat_init(cgrp);
  4764. if (ret)
  4765. goto out_cancel_ref;
  4766. /* create the directory */
  4767. kn = kernfs_create_dir(parent->kn, name, mode, cgrp);
  4768. if (IS_ERR(kn)) {
  4769. ret = PTR_ERR(kn);
  4770. goto out_stat_exit;
  4771. }
  4772. cgrp->kn = kn;
  4773. init_cgroup_housekeeping(cgrp);
  4774. cgrp->self.parent = &parent->self;
  4775. cgrp->root = root;
  4776. cgrp->level = level;
  4777. ret = psi_cgroup_alloc(cgrp);
  4778. if (ret)
  4779. goto out_kernfs_remove;
  4780. ret = cgroup_bpf_inherit(cgrp);
  4781. if (ret)
  4782. goto out_psi_free;
  4783. /*
  4784. * New cgroup inherits effective freeze counter, and
  4785. * if the parent has to be frozen, the child has too.
  4786. */
  4787. cgrp->freezer.e_freeze = parent->freezer.e_freeze;
  4788. if (cgrp->freezer.e_freeze) {
  4789. /*
  4790. * Set the CGRP_FREEZE flag, so when a process will be
  4791. * attached to the child cgroup, it will become frozen.
  4792. * At this point the new cgroup is unpopulated, so we can
  4793. * consider it frozen immediately.
  4794. */
  4795. set_bit(CGRP_FREEZE, &cgrp->flags);
  4796. set_bit(CGRP_FROZEN, &cgrp->flags);
  4797. }
  4798. spin_lock_irq(&css_set_lock);
  4799. for (tcgrp = cgrp; tcgrp; tcgrp = cgroup_parent(tcgrp)) {
  4800. cgrp->ancestors[tcgrp->level] = tcgrp;
  4801. if (tcgrp != cgrp) {
  4802. tcgrp->nr_descendants++;
  4803. /*
  4804. * If the new cgroup is frozen, all ancestor cgroups
  4805. * get a new frozen descendant, but their state can't
  4806. * change because of this.
  4807. */
  4808. if (cgrp->freezer.e_freeze)
  4809. tcgrp->freezer.nr_frozen_descendants++;
  4810. }
  4811. }
  4812. spin_unlock_irq(&css_set_lock);
  4813. if (notify_on_release(parent))
  4814. set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
  4815. if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
  4816. set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
  4817. cgrp->self.serial_nr = css_serial_nr_next++;
  4818. /* allocation complete, commit to creation */
  4819. list_add_tail_rcu(&cgrp->self.sibling, &cgroup_parent(cgrp)->self.children);
  4820. atomic_inc(&root->nr_cgrps);
  4821. cgroup_get_live(parent);
  4822. /*
  4823. * On the default hierarchy, a child doesn't automatically inherit
  4824. * subtree_control from the parent. Each is configured manually.
  4825. */
  4826. if (!cgroup_on_dfl(cgrp))
  4827. cgrp->subtree_control = cgroup_control(cgrp);
  4828. cgroup_propagate_control(cgrp);
  4829. return cgrp;
  4830. out_psi_free:
  4831. psi_cgroup_free(cgrp);
  4832. out_kernfs_remove:
  4833. kernfs_remove(cgrp->kn);
  4834. out_stat_exit:
  4835. cgroup_rstat_exit(cgrp);
  4836. out_cancel_ref:
  4837. percpu_ref_exit(&cgrp->self.refcnt);
  4838. out_free_cgrp:
  4839. kfree(cgrp);
  4840. return ERR_PTR(ret);
  4841. }
  4842. static bool cgroup_check_hierarchy_limits(struct cgroup *parent)
  4843. {
  4844. struct cgroup *cgroup;
  4845. int ret = false;
  4846. int level = 1;
  4847. lockdep_assert_held(&cgroup_mutex);
  4848. for (cgroup = parent; cgroup; cgroup = cgroup_parent(cgroup)) {
  4849. if (cgroup->nr_descendants >= cgroup->max_descendants)
  4850. goto fail;
  4851. if (level > cgroup->max_depth)
  4852. goto fail;
  4853. level++;
  4854. }
  4855. ret = true;
  4856. fail:
  4857. return ret;
  4858. }
  4859. int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name, umode_t mode)
  4860. {
  4861. struct cgroup *parent, *cgrp;
  4862. int ret;
  4863. /* do not accept '\n' to prevent making /proc/<pid>/cgroup unparsable */
  4864. if (strchr(name, '\n'))
  4865. return -EINVAL;
  4866. parent = cgroup_kn_lock_live(parent_kn, false);
  4867. if (!parent)
  4868. return -ENODEV;
  4869. if (!cgroup_check_hierarchy_limits(parent)) {
  4870. ret = -EAGAIN;
  4871. goto out_unlock;
  4872. }
  4873. cgrp = cgroup_create(parent, name, mode);
  4874. if (IS_ERR(cgrp)) {
  4875. ret = PTR_ERR(cgrp);
  4876. goto out_unlock;
  4877. }
  4878. /*
  4879. * This extra ref will be put in cgroup_free_fn() and guarantees
  4880. * that @cgrp->kn is always accessible.
  4881. */
  4882. kernfs_get(cgrp->kn);
  4883. ret = cgroup_kn_set_ugid(cgrp->kn);
  4884. if (ret)
  4885. goto out_destroy;
  4886. ret = css_populate_dir(&cgrp->self);
  4887. if (ret)
  4888. goto out_destroy;
  4889. ret = cgroup_apply_control_enable(cgrp);
  4890. if (ret)
  4891. goto out_destroy;
  4892. TRACE_CGROUP_PATH(mkdir, cgrp);
  4893. /* let's create and online css's */
  4894. kernfs_activate(cgrp->kn);
  4895. ret = 0;
  4896. goto out_unlock;
  4897. out_destroy:
  4898. cgroup_destroy_locked(cgrp);
  4899. out_unlock:
  4900. cgroup_kn_unlock(parent_kn);
  4901. return ret;
  4902. }
  4903. /*
  4904. * This is called when the refcnt of a css is confirmed to be killed.
  4905. * css_tryget_online() is now guaranteed to fail. Tell the subsystem to
  4906. * initiate destruction and put the css ref from kill_css().
  4907. */
  4908. static void css_killed_work_fn(struct work_struct *work)
  4909. {
  4910. struct cgroup_subsys_state *css =
  4911. container_of(work, struct cgroup_subsys_state, destroy_work);
  4912. cgroup_lock();
  4913. do {
  4914. offline_css(css);
  4915. css_put(css);
  4916. /* @css can't go away while we're holding cgroup_mutex */
  4917. css = css->parent;
  4918. } while (css && atomic_dec_and_test(&css->online_cnt));
  4919. cgroup_unlock();
  4920. }
  4921. /* css kill confirmation processing requires process context, bounce */
  4922. static void css_killed_ref_fn(struct percpu_ref *ref)
  4923. {
  4924. struct cgroup_subsys_state *css =
  4925. container_of(ref, struct cgroup_subsys_state, refcnt);
  4926. if (atomic_dec_and_test(&css->online_cnt)) {
  4927. INIT_WORK(&css->destroy_work, css_killed_work_fn);
  4928. queue_work(cgroup_destroy_wq, &css->destroy_work);
  4929. }
  4930. }
  4931. /**
  4932. * kill_css - destroy a css
  4933. * @css: css to destroy
  4934. *
  4935. * This function initiates destruction of @css by removing cgroup interface
  4936. * files and putting its base reference. ->css_offline() will be invoked
  4937. * asynchronously once css_tryget_online() is guaranteed to fail and when
  4938. * the reference count reaches zero, @css will be released.
  4939. */
  4940. static void kill_css(struct cgroup_subsys_state *css)
  4941. {
  4942. lockdep_assert_held(&cgroup_mutex);
  4943. if (css->flags & CSS_DYING)
  4944. return;
  4945. css->flags |= CSS_DYING;
  4946. /*
  4947. * This must happen before css is disassociated with its cgroup.
  4948. * See seq_css() for details.
  4949. */
  4950. css_clear_dir(css);
  4951. /*
  4952. * Killing would put the base ref, but we need to keep it alive
  4953. * until after ->css_offline().
  4954. */
  4955. css_get(css);
  4956. /*
  4957. * cgroup core guarantees that, by the time ->css_offline() is
  4958. * invoked, no new css reference will be given out via
  4959. * css_tryget_online(). We can't simply call percpu_ref_kill() and
  4960. * proceed to offlining css's because percpu_ref_kill() doesn't
  4961. * guarantee that the ref is seen as killed on all CPUs on return.
  4962. *
  4963. * Use percpu_ref_kill_and_confirm() to get notifications as each
  4964. * css is confirmed to be seen as killed on all CPUs.
  4965. */
  4966. percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
  4967. }
  4968. /**
  4969. * cgroup_destroy_locked - the first stage of cgroup destruction
  4970. * @cgrp: cgroup to be destroyed
  4971. *
  4972. * css's make use of percpu refcnts whose killing latency shouldn't be
  4973. * exposed to userland and are RCU protected. Also, cgroup core needs to
  4974. * guarantee that css_tryget_online() won't succeed by the time
  4975. * ->css_offline() is invoked. To satisfy all the requirements,
  4976. * destruction is implemented in the following two steps.
  4977. *
  4978. * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
  4979. * userland visible parts and start killing the percpu refcnts of
  4980. * css's. Set up so that the next stage will be kicked off once all
  4981. * the percpu refcnts are confirmed to be killed.
  4982. *
  4983. * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
  4984. * rest of destruction. Once all cgroup references are gone, the
  4985. * cgroup is RCU-freed.
  4986. *
  4987. * This function implements s1. After this step, @cgrp is gone as far as
  4988. * the userland is concerned and a new cgroup with the same name may be
  4989. * created. As cgroup doesn't care about the names internally, this
  4990. * doesn't cause any problem.
  4991. */
  4992. static int cgroup_destroy_locked(struct cgroup *cgrp)
  4993. __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
  4994. {
  4995. struct cgroup *tcgrp, *parent = cgroup_parent(cgrp);
  4996. struct cgroup_subsys_state *css;
  4997. struct cgrp_cset_link *link;
  4998. int ssid;
  4999. lockdep_assert_held(&cgroup_mutex);
  5000. /*
  5001. * Only migration can raise populated from zero and we're already
  5002. * holding cgroup_mutex.
  5003. */
  5004. if (cgroup_is_populated(cgrp))
  5005. return -EBUSY;
  5006. /*
  5007. * Make sure there's no live children. We can't test emptiness of
  5008. * ->self.children as dead children linger on it while being
  5009. * drained; otherwise, "rmdir parent/child parent" may fail.
  5010. */
  5011. if (css_has_online_children(&cgrp->self))
  5012. return -EBUSY;
  5013. /*
  5014. * Mark @cgrp and the associated csets dead. The former prevents
  5015. * further task migration and child creation by disabling
  5016. * cgroup_lock_live_group(). The latter makes the csets ignored by
  5017. * the migration path.
  5018. */
  5019. cgrp->self.flags &= ~CSS_ONLINE;
  5020. spin_lock_irq(&css_set_lock);
  5021. list_for_each_entry(link, &cgrp->cset_links, cset_link)
  5022. link->cset->dead = true;
  5023. spin_unlock_irq(&css_set_lock);
  5024. /* initiate massacre of all css's */
  5025. for_each_css(css, ssid, cgrp)
  5026. kill_css(css);
  5027. /* clear and remove @cgrp dir, @cgrp has an extra ref on its kn */
  5028. css_clear_dir(&cgrp->self);
  5029. kernfs_remove(cgrp->kn);
  5030. if (cgroup_is_threaded(cgrp))
  5031. parent->nr_threaded_children--;
  5032. spin_lock_irq(&css_set_lock);
  5033. for (tcgrp = cgroup_parent(cgrp); tcgrp; tcgrp = cgroup_parent(tcgrp)) {
  5034. tcgrp->nr_descendants--;
  5035. tcgrp->nr_dying_descendants++;
  5036. /*
  5037. * If the dying cgroup is frozen, decrease frozen descendants
  5038. * counters of ancestor cgroups.
  5039. */
  5040. if (test_bit(CGRP_FROZEN, &cgrp->flags))
  5041. tcgrp->freezer.nr_frozen_descendants--;
  5042. }
  5043. spin_unlock_irq(&css_set_lock);
  5044. cgroup1_check_for_release(parent);
  5045. cgroup_bpf_offline(cgrp);
  5046. /* put the base reference */
  5047. percpu_ref_kill(&cgrp->self.refcnt);
  5048. return 0;
  5049. };
  5050. int cgroup_rmdir(struct kernfs_node *kn)
  5051. {
  5052. struct cgroup *cgrp;
  5053. int ret = 0;
  5054. cgrp = cgroup_kn_lock_live(kn, false);
  5055. if (!cgrp)
  5056. return 0;
  5057. ret = cgroup_destroy_locked(cgrp);
  5058. if (!ret)
  5059. TRACE_CGROUP_PATH(rmdir, cgrp);
  5060. cgroup_kn_unlock(kn);
  5061. return ret;
  5062. }
  5063. static struct kernfs_syscall_ops cgroup_kf_syscall_ops = {
  5064. .show_options = cgroup_show_options,
  5065. .mkdir = cgroup_mkdir,
  5066. .rmdir = cgroup_rmdir,
  5067. .show_path = cgroup_show_path,
  5068. };
  5069. static void __init cgroup_init_subsys(struct cgroup_subsys *ss, bool early)
  5070. {
  5071. struct cgroup_subsys_state *css;
  5072. pr_debug("Initializing cgroup subsys %s\n", ss->name);
  5073. cgroup_lock();
  5074. idr_init(&ss->css_idr);
  5075. INIT_LIST_HEAD(&ss->cfts);
  5076. /* Create the root cgroup state for this subsystem */
  5077. ss->root = &cgrp_dfl_root;
  5078. css = ss->css_alloc(NULL);
  5079. /* We don't handle early failures gracefully */
  5080. BUG_ON(IS_ERR(css));
  5081. init_and_link_css(css, ss, &cgrp_dfl_root.cgrp);
  5082. /*
  5083. * Root csses are never destroyed and we can't initialize
  5084. * percpu_ref during early init. Disable refcnting.
  5085. */
  5086. css->flags |= CSS_NO_REF;
  5087. if (early) {
  5088. /* allocation can't be done safely during early init */
  5089. css->id = 1;
  5090. } else {
  5091. css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2, GFP_KERNEL);
  5092. BUG_ON(css->id < 0);
  5093. }
  5094. /* Update the init_css_set to contain a subsys
  5095. * pointer to this state - since the subsystem is
  5096. * newly registered, all tasks and hence the
  5097. * init_css_set is in the subsystem's root cgroup. */
  5098. init_css_set.subsys[ss->id] = css;
  5099. have_fork_callback |= (bool)ss->fork << ss->id;
  5100. have_exit_callback |= (bool)ss->exit << ss->id;
  5101. have_release_callback |= (bool)ss->release << ss->id;
  5102. have_canfork_callback |= (bool)ss->can_fork << ss->id;
  5103. /* At system boot, before all subsystems have been
  5104. * registered, no tasks have been forked, so we don't
  5105. * need to invoke fork callbacks here. */
  5106. BUG_ON(!list_empty(&init_task.tasks));
  5107. BUG_ON(online_css(css));
  5108. cgroup_unlock();
  5109. }
  5110. /**
  5111. * cgroup_init_early - cgroup initialization at system boot
  5112. *
  5113. * Initialize cgroups at system boot, and initialize any
  5114. * subsystems that request early init.
  5115. */
  5116. int __init cgroup_init_early(void)
  5117. {
  5118. static struct cgroup_fs_context __initdata ctx;
  5119. struct cgroup_subsys *ss;
  5120. int i;
  5121. ctx.root = &cgrp_dfl_root;
  5122. init_cgroup_root(&ctx);
  5123. cgrp_dfl_root.cgrp.self.flags |= CSS_NO_REF;
  5124. RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
  5125. for_each_subsys(ss, i) {
  5126. WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id,
  5127. "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p id:name=%d:%s\n",
  5128. i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free,
  5129. ss->id, ss->name);
  5130. WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN,
  5131. "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]);
  5132. ss->id = i;
  5133. ss->name = cgroup_subsys_name[i];
  5134. if (!ss->legacy_name)
  5135. ss->legacy_name = cgroup_subsys_name[i];
  5136. if (ss->early_init)
  5137. cgroup_init_subsys(ss, true);
  5138. }
  5139. return 0;
  5140. }
  5141. /**
  5142. * cgroup_init - cgroup initialization
  5143. *
  5144. * Register cgroup filesystem and /proc file, and initialize
  5145. * any subsystems that didn't request early init.
  5146. */
  5147. int __init cgroup_init(void)
  5148. {
  5149. struct cgroup_subsys *ss;
  5150. int ssid;
  5151. BUILD_BUG_ON(CGROUP_SUBSYS_COUNT > 16);
  5152. BUG_ON(cgroup_init_cftypes(NULL, cgroup_base_files));
  5153. BUG_ON(cgroup_init_cftypes(NULL, cgroup_psi_files));
  5154. BUG_ON(cgroup_init_cftypes(NULL, cgroup1_base_files));
  5155. cgroup_rstat_boot();
  5156. get_user_ns(init_cgroup_ns.user_ns);
  5157. cgroup_lock();
  5158. /*
  5159. * Add init_css_set to the hash table so that dfl_root can link to
  5160. * it during init.
  5161. */
  5162. hash_add(css_set_table, &init_css_set.hlist,
  5163. css_set_hash(init_css_set.subsys));
  5164. BUG_ON(cgroup_setup_root(&cgrp_dfl_root, 0));
  5165. cgroup_unlock();
  5166. for_each_subsys(ss, ssid) {
  5167. if (ss->early_init) {
  5168. struct cgroup_subsys_state *css =
  5169. init_css_set.subsys[ss->id];
  5170. css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2,
  5171. GFP_KERNEL);
  5172. BUG_ON(css->id < 0);
  5173. } else {
  5174. cgroup_init_subsys(ss, false);
  5175. }
  5176. list_add_tail(&init_css_set.e_cset_node[ssid],
  5177. &cgrp_dfl_root.cgrp.e_csets[ssid]);
  5178. /*
  5179. * Setting dfl_root subsys_mask needs to consider the
  5180. * disabled flag and cftype registration needs kmalloc,
  5181. * both of which aren't available during early_init.
  5182. */
  5183. if (!cgroup_ssid_enabled(ssid))
  5184. continue;
  5185. if (cgroup1_ssid_disabled(ssid))
  5186. printk(KERN_INFO "Disabling %s control group subsystem in v1 mounts\n",
  5187. ss->name);
  5188. cgrp_dfl_root.subsys_mask |= 1 << ss->id;
  5189. /* implicit controllers must be threaded too */
  5190. WARN_ON(ss->implicit_on_dfl && !ss->threaded);
  5191. if (ss->implicit_on_dfl)
  5192. cgrp_dfl_implicit_ss_mask |= 1 << ss->id;
  5193. else if (!ss->dfl_cftypes)
  5194. cgrp_dfl_inhibit_ss_mask |= 1 << ss->id;
  5195. if (ss->threaded)
  5196. cgrp_dfl_threaded_ss_mask |= 1 << ss->id;
  5197. if (ss->dfl_cftypes == ss->legacy_cftypes) {
  5198. WARN_ON(cgroup_add_cftypes(ss, ss->dfl_cftypes));
  5199. } else {
  5200. WARN_ON(cgroup_add_dfl_cftypes(ss, ss->dfl_cftypes));
  5201. WARN_ON(cgroup_add_legacy_cftypes(ss, ss->legacy_cftypes));
  5202. }
  5203. if (ss->bind)
  5204. ss->bind(init_css_set.subsys[ssid]);
  5205. cgroup_lock();
  5206. css_populate_dir(init_css_set.subsys[ssid]);
  5207. cgroup_unlock();
  5208. }
  5209. /* init_css_set.subsys[] has been updated, re-hash */
  5210. hash_del(&init_css_set.hlist);
  5211. hash_add(css_set_table, &init_css_set.hlist,
  5212. css_set_hash(init_css_set.subsys));
  5213. WARN_ON(sysfs_create_mount_point(fs_kobj, "cgroup"));
  5214. WARN_ON(register_filesystem(&cgroup_fs_type));
  5215. WARN_ON(register_filesystem(&cgroup2_fs_type));
  5216. WARN_ON(!proc_create_single("cgroups", 0, NULL, proc_cgroupstats_show));
  5217. #ifdef CONFIG_CPUSETS
  5218. WARN_ON(register_filesystem(&cpuset_fs_type));
  5219. #endif
  5220. return 0;
  5221. }
  5222. static int __init cgroup_wq_init(void)
  5223. {
  5224. /*
  5225. * There isn't much point in executing destruction path in
  5226. * parallel. Good chunk is serialized with cgroup_mutex anyway.
  5227. * Use 1 for @max_active.
  5228. *
  5229. * We would prefer to do this in cgroup_init() above, but that
  5230. * is called before init_workqueues(): so leave this until after.
  5231. */
  5232. cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1);
  5233. BUG_ON(!cgroup_destroy_wq);
  5234. return 0;
  5235. }
  5236. core_initcall(cgroup_wq_init);
  5237. void cgroup_path_from_kernfs_id(u64 id, char *buf, size_t buflen)
  5238. {
  5239. struct kernfs_node *kn;
  5240. kn = kernfs_find_and_get_node_by_id(cgrp_dfl_root.kf_root, id);
  5241. if (!kn)
  5242. return;
  5243. kernfs_path(kn, buf, buflen);
  5244. kernfs_put(kn);
  5245. }
  5246. /*
  5247. * cgroup_get_from_id : get the cgroup associated with cgroup id
  5248. * @id: cgroup id
  5249. * On success return the cgrp or ERR_PTR on failure
  5250. * Only cgroups within current task's cgroup NS are valid.
  5251. */
  5252. struct cgroup *cgroup_get_from_id(u64 id)
  5253. {
  5254. struct kernfs_node *kn;
  5255. struct cgroup *cgrp, *root_cgrp;
  5256. kn = kernfs_find_and_get_node_by_id(cgrp_dfl_root.kf_root, id);
  5257. if (!kn)
  5258. return ERR_PTR(-ENOENT);
  5259. if (kernfs_type(kn) != KERNFS_DIR) {
  5260. kernfs_put(kn);
  5261. return ERR_PTR(-ENOENT);
  5262. }
  5263. rcu_read_lock();
  5264. cgrp = rcu_dereference(*(void __rcu __force **)&kn->priv);
  5265. if (cgrp && !cgroup_tryget(cgrp))
  5266. cgrp = NULL;
  5267. rcu_read_unlock();
  5268. kernfs_put(kn);
  5269. if (!cgrp)
  5270. return ERR_PTR(-ENOENT);
  5271. root_cgrp = current_cgns_cgroup_dfl();
  5272. if (!cgroup_is_descendant(cgrp, root_cgrp)) {
  5273. cgroup_put(cgrp);
  5274. return ERR_PTR(-ENOENT);
  5275. }
  5276. return cgrp;
  5277. }
  5278. EXPORT_SYMBOL_GPL(cgroup_get_from_id);
  5279. /*
  5280. * proc_cgroup_show()
  5281. * - Print task's cgroup paths into seq_file, one line for each hierarchy
  5282. * - Used for /proc/<pid>/cgroup.
  5283. */
  5284. int proc_cgroup_show(struct seq_file *m, struct pid_namespace *ns,
  5285. struct pid *pid, struct task_struct *tsk)
  5286. {
  5287. char *buf;
  5288. int retval;
  5289. struct cgroup_root *root;
  5290. retval = -ENOMEM;
  5291. buf = kmalloc(PATH_MAX, GFP_KERNEL);
  5292. if (!buf)
  5293. goto out;
  5294. cgroup_lock();
  5295. spin_lock_irq(&css_set_lock);
  5296. for_each_root(root) {
  5297. struct cgroup_subsys *ss;
  5298. struct cgroup *cgrp;
  5299. int ssid, count = 0;
  5300. if (root == &cgrp_dfl_root && !READ_ONCE(cgrp_dfl_visible))
  5301. continue;
  5302. seq_printf(m, "%d:", root->hierarchy_id);
  5303. if (root != &cgrp_dfl_root)
  5304. for_each_subsys(ss, ssid)
  5305. if (root->subsys_mask & (1 << ssid))
  5306. seq_printf(m, "%s%s", count++ ? "," : "",
  5307. ss->legacy_name);
  5308. if (strlen(root->name))
  5309. seq_printf(m, "%sname=%s", count ? "," : "",
  5310. root->name);
  5311. seq_putc(m, ':');
  5312. cgrp = task_cgroup_from_root(tsk, root);
  5313. /*
  5314. * On traditional hierarchies, all zombie tasks show up as
  5315. * belonging to the root cgroup. On the default hierarchy,
  5316. * while a zombie doesn't show up in "cgroup.procs" and
  5317. * thus can't be migrated, its /proc/PID/cgroup keeps
  5318. * reporting the cgroup it belonged to before exiting. If
  5319. * the cgroup is removed before the zombie is reaped,
  5320. * " (deleted)" is appended to the cgroup path.
  5321. */
  5322. if (cgroup_on_dfl(cgrp) || !(tsk->flags & PF_EXITING)) {
  5323. retval = cgroup_path_ns_locked(cgrp, buf, PATH_MAX,
  5324. current->nsproxy->cgroup_ns);
  5325. if (retval >= PATH_MAX)
  5326. retval = -ENAMETOOLONG;
  5327. if (retval < 0)
  5328. goto out_unlock;
  5329. seq_puts(m, buf);
  5330. } else {
  5331. seq_puts(m, "/");
  5332. }
  5333. if (cgroup_on_dfl(cgrp) && cgroup_is_dead(cgrp))
  5334. seq_puts(m, " (deleted)\n");
  5335. else
  5336. seq_putc(m, '\n');
  5337. }
  5338. retval = 0;
  5339. out_unlock:
  5340. spin_unlock_irq(&css_set_lock);
  5341. cgroup_unlock();
  5342. kfree(buf);
  5343. out:
  5344. return retval;
  5345. }
  5346. /**
  5347. * cgroup_fork - initialize cgroup related fields during copy_process()
  5348. * @child: pointer to task_struct of forking parent process.
  5349. *
  5350. * A task is associated with the init_css_set until cgroup_post_fork()
  5351. * attaches it to the target css_set.
  5352. */
  5353. void cgroup_fork(struct task_struct *child)
  5354. {
  5355. RCU_INIT_POINTER(child->cgroups, &init_css_set);
  5356. INIT_LIST_HEAD(&child->cg_list);
  5357. }
  5358. /**
  5359. * cgroup_v1v2_get_from_file - get a cgroup pointer from a file pointer
  5360. * @f: file corresponding to cgroup_dir
  5361. *
  5362. * Find the cgroup from a file pointer associated with a cgroup directory.
  5363. * Returns a pointer to the cgroup on success. ERR_PTR is returned if the
  5364. * cgroup cannot be found.
  5365. */
  5366. static struct cgroup *cgroup_v1v2_get_from_file(struct file *f)
  5367. {
  5368. struct cgroup_subsys_state *css;
  5369. css = css_tryget_online_from_dir(f->f_path.dentry, NULL);
  5370. if (IS_ERR(css))
  5371. return ERR_CAST(css);
  5372. return css->cgroup;
  5373. }
  5374. /**
  5375. * cgroup_get_from_file - same as cgroup_v1v2_get_from_file, but only supports
  5376. * cgroup2.
  5377. * @f: file corresponding to cgroup2_dir
  5378. */
  5379. static struct cgroup *cgroup_get_from_file(struct file *f)
  5380. {
  5381. struct cgroup *cgrp = cgroup_v1v2_get_from_file(f);
  5382. if (IS_ERR(cgrp))
  5383. return ERR_CAST(cgrp);
  5384. if (!cgroup_on_dfl(cgrp)) {
  5385. cgroup_put(cgrp);
  5386. return ERR_PTR(-EBADF);
  5387. }
  5388. return cgrp;
  5389. }
  5390. /**
  5391. * cgroup_css_set_fork - find or create a css_set for a child process
  5392. * @kargs: the arguments passed to create the child process
  5393. *
  5394. * This functions finds or creates a new css_set which the child
  5395. * process will be attached to in cgroup_post_fork(). By default,
  5396. * the child process will be given the same css_set as its parent.
  5397. *
  5398. * If CLONE_INTO_CGROUP is specified this function will try to find an
  5399. * existing css_set which includes the requested cgroup and if not create
  5400. * a new css_set that the child will be attached to later. If this function
  5401. * succeeds it will hold cgroup_threadgroup_rwsem on return. If
  5402. * CLONE_INTO_CGROUP is requested this function will grab cgroup mutex
  5403. * before grabbing cgroup_threadgroup_rwsem and will hold a reference
  5404. * to the target cgroup.
  5405. */
  5406. static int cgroup_css_set_fork(struct kernel_clone_args *kargs)
  5407. __acquires(&cgroup_mutex) __acquires(&cgroup_threadgroup_rwsem)
  5408. {
  5409. int ret;
  5410. struct cgroup *dst_cgrp = NULL;
  5411. struct css_set *cset;
  5412. struct super_block *sb;
  5413. struct file *f;
  5414. if (kargs->flags & CLONE_INTO_CGROUP)
  5415. cgroup_lock();
  5416. cgroup_threadgroup_change_begin(current);
  5417. spin_lock_irq(&css_set_lock);
  5418. cset = task_css_set(current);
  5419. get_css_set(cset);
  5420. spin_unlock_irq(&css_set_lock);
  5421. if (!(kargs->flags & CLONE_INTO_CGROUP)) {
  5422. kargs->cset = cset;
  5423. return 0;
  5424. }
  5425. f = fget_raw(kargs->cgroup);
  5426. if (!f) {
  5427. ret = -EBADF;
  5428. goto err;
  5429. }
  5430. sb = f->f_path.dentry->d_sb;
  5431. dst_cgrp = cgroup_get_from_file(f);
  5432. if (IS_ERR(dst_cgrp)) {
  5433. ret = PTR_ERR(dst_cgrp);
  5434. dst_cgrp = NULL;
  5435. goto err;
  5436. }
  5437. if (cgroup_is_dead(dst_cgrp)) {
  5438. ret = -ENODEV;
  5439. goto err;
  5440. }
  5441. /*
  5442. * Verify that we the target cgroup is writable for us. This is
  5443. * usually done by the vfs layer but since we're not going through
  5444. * the vfs layer here we need to do it "manually".
  5445. */
  5446. ret = cgroup_may_write(dst_cgrp, sb);
  5447. if (ret)
  5448. goto err;
  5449. /*
  5450. * Spawning a task directly into a cgroup works by passing a file
  5451. * descriptor to the target cgroup directory. This can even be an O_PATH
  5452. * file descriptor. But it can never be a cgroup.procs file descriptor.
  5453. * This was done on purpose so spawning into a cgroup could be
  5454. * conceptualized as an atomic
  5455. *
  5456. * fd = openat(dfd_cgroup, "cgroup.procs", ...);
  5457. * write(fd, <child-pid>, ...);
  5458. *
  5459. * sequence, i.e. it's a shorthand for the caller opening and writing
  5460. * cgroup.procs of the cgroup indicated by @dfd_cgroup. This allows us
  5461. * to always use the caller's credentials.
  5462. */
  5463. ret = cgroup_attach_permissions(cset->dfl_cgrp, dst_cgrp, sb,
  5464. !(kargs->flags & CLONE_THREAD),
  5465. current->nsproxy->cgroup_ns);
  5466. if (ret)
  5467. goto err;
  5468. kargs->cset = find_css_set(cset, dst_cgrp);
  5469. if (!kargs->cset) {
  5470. ret = -ENOMEM;
  5471. goto err;
  5472. }
  5473. put_css_set(cset);
  5474. fput(f);
  5475. kargs->cgrp = dst_cgrp;
  5476. return ret;
  5477. err:
  5478. cgroup_threadgroup_change_end(current);
  5479. cgroup_unlock();
  5480. if (f)
  5481. fput(f);
  5482. if (dst_cgrp)
  5483. cgroup_put(dst_cgrp);
  5484. put_css_set(cset);
  5485. if (kargs->cset)
  5486. put_css_set(kargs->cset);
  5487. return ret;
  5488. }
  5489. /**
  5490. * cgroup_css_set_put_fork - drop references we took during fork
  5491. * @kargs: the arguments passed to create the child process
  5492. *
  5493. * Drop references to the prepared css_set and target cgroup if
  5494. * CLONE_INTO_CGROUP was requested.
  5495. */
  5496. static void cgroup_css_set_put_fork(struct kernel_clone_args *kargs)
  5497. __releases(&cgroup_threadgroup_rwsem) __releases(&cgroup_mutex)
  5498. {
  5499. struct cgroup *cgrp = kargs->cgrp;
  5500. struct css_set *cset = kargs->cset;
  5501. cgroup_threadgroup_change_end(current);
  5502. if (cset) {
  5503. put_css_set(cset);
  5504. kargs->cset = NULL;
  5505. }
  5506. if (kargs->flags & CLONE_INTO_CGROUP) {
  5507. cgroup_unlock();
  5508. if (cgrp) {
  5509. cgroup_put(cgrp);
  5510. kargs->cgrp = NULL;
  5511. }
  5512. }
  5513. }
  5514. /**
  5515. * cgroup_can_fork - called on a new task before the process is exposed
  5516. * @child: the child process
  5517. * @kargs: the arguments passed to create the child process
  5518. *
  5519. * This prepares a new css_set for the child process which the child will
  5520. * be attached to in cgroup_post_fork().
  5521. * This calls the subsystem can_fork() callbacks. If the cgroup_can_fork()
  5522. * callback returns an error, the fork aborts with that error code. This
  5523. * allows for a cgroup subsystem to conditionally allow or deny new forks.
  5524. */
  5525. int cgroup_can_fork(struct task_struct *child, struct kernel_clone_args *kargs)
  5526. {
  5527. struct cgroup_subsys *ss;
  5528. int i, j, ret;
  5529. ret = cgroup_css_set_fork(kargs);
  5530. if (ret)
  5531. return ret;
  5532. do_each_subsys_mask(ss, i, have_canfork_callback) {
  5533. ret = ss->can_fork(child, kargs->cset);
  5534. if (ret)
  5535. goto out_revert;
  5536. } while_each_subsys_mask();
  5537. return 0;
  5538. out_revert:
  5539. for_each_subsys(ss, j) {
  5540. if (j >= i)
  5541. break;
  5542. if (ss->cancel_fork)
  5543. ss->cancel_fork(child, kargs->cset);
  5544. }
  5545. cgroup_css_set_put_fork(kargs);
  5546. return ret;
  5547. }
  5548. /**
  5549. * cgroup_cancel_fork - called if a fork failed after cgroup_can_fork()
  5550. * @child: the child process
  5551. * @kargs: the arguments passed to create the child process
  5552. *
  5553. * This calls the cancel_fork() callbacks if a fork failed *after*
  5554. * cgroup_can_fork() succeeded and cleans up references we took to
  5555. * prepare a new css_set for the child process in cgroup_can_fork().
  5556. */
  5557. void cgroup_cancel_fork(struct task_struct *child,
  5558. struct kernel_clone_args *kargs)
  5559. {
  5560. struct cgroup_subsys *ss;
  5561. int i;
  5562. for_each_subsys(ss, i)
  5563. if (ss->cancel_fork)
  5564. ss->cancel_fork(child, kargs->cset);
  5565. cgroup_css_set_put_fork(kargs);
  5566. }
  5567. /**
  5568. * cgroup_post_fork - finalize cgroup setup for the child process
  5569. * @child: the child process
  5570. * @kargs: the arguments passed to create the child process
  5571. *
  5572. * Attach the child process to its css_set calling the subsystem fork()
  5573. * callbacks.
  5574. */
  5575. void cgroup_post_fork(struct task_struct *child,
  5576. struct kernel_clone_args *kargs)
  5577. __releases(&cgroup_threadgroup_rwsem) __releases(&cgroup_mutex)
  5578. {
  5579. unsigned long cgrp_flags = 0;
  5580. bool kill = false;
  5581. struct cgroup_subsys *ss;
  5582. struct css_set *cset;
  5583. int i;
  5584. cset = kargs->cset;
  5585. kargs->cset = NULL;
  5586. spin_lock_irq(&css_set_lock);
  5587. /* init tasks are special, only link regular threads */
  5588. if (likely(child->pid)) {
  5589. if (kargs->cgrp)
  5590. cgrp_flags = kargs->cgrp->flags;
  5591. else
  5592. cgrp_flags = cset->dfl_cgrp->flags;
  5593. WARN_ON_ONCE(!list_empty(&child->cg_list));
  5594. cset->nr_tasks++;
  5595. css_set_move_task(child, NULL, cset, false);
  5596. } else {
  5597. put_css_set(cset);
  5598. cset = NULL;
  5599. }
  5600. if (!(child->flags & PF_KTHREAD)) {
  5601. if (unlikely(test_bit(CGRP_FREEZE, &cgrp_flags))) {
  5602. /*
  5603. * If the cgroup has to be frozen, the new task has
  5604. * too. Let's set the JOBCTL_TRAP_FREEZE jobctl bit to
  5605. * get the task into the frozen state.
  5606. */
  5607. spin_lock(&child->sighand->siglock);
  5608. WARN_ON_ONCE(child->frozen);
  5609. child->jobctl |= JOBCTL_TRAP_FREEZE;
  5610. spin_unlock(&child->sighand->siglock);
  5611. /*
  5612. * Calling cgroup_update_frozen() isn't required here,
  5613. * because it will be called anyway a bit later from
  5614. * do_freezer_trap(). So we avoid cgroup's transient
  5615. * switch from the frozen state and back.
  5616. */
  5617. }
  5618. /*
  5619. * If the cgroup is to be killed notice it now and take the
  5620. * child down right after we finished preparing it for
  5621. * userspace.
  5622. */
  5623. kill = test_bit(CGRP_KILL, &cgrp_flags);
  5624. }
  5625. spin_unlock_irq(&css_set_lock);
  5626. /*
  5627. * Call ss->fork(). This must happen after @child is linked on
  5628. * css_set; otherwise, @child might change state between ->fork()
  5629. * and addition to css_set.
  5630. */
  5631. do_each_subsys_mask(ss, i, have_fork_callback) {
  5632. ss->fork(child);
  5633. } while_each_subsys_mask();
  5634. /* Make the new cset the root_cset of the new cgroup namespace. */
  5635. if (kargs->flags & CLONE_NEWCGROUP) {
  5636. struct css_set *rcset = child->nsproxy->cgroup_ns->root_cset;
  5637. get_css_set(cset);
  5638. child->nsproxy->cgroup_ns->root_cset = cset;
  5639. put_css_set(rcset);
  5640. }
  5641. /* Cgroup has to be killed so take down child immediately. */
  5642. if (unlikely(kill))
  5643. do_send_sig_info(SIGKILL, SEND_SIG_NOINFO, child, PIDTYPE_TGID);
  5644. cgroup_css_set_put_fork(kargs);
  5645. }
  5646. /**
  5647. * cgroup_exit - detach cgroup from exiting task
  5648. * @tsk: pointer to task_struct of exiting process
  5649. *
  5650. * Description: Detach cgroup from @tsk.
  5651. *
  5652. */
  5653. void cgroup_exit(struct task_struct *tsk)
  5654. {
  5655. struct cgroup_subsys *ss;
  5656. struct css_set *cset;
  5657. int i;
  5658. spin_lock_irq(&css_set_lock);
  5659. WARN_ON_ONCE(list_empty(&tsk->cg_list));
  5660. cset = task_css_set(tsk);
  5661. css_set_move_task(tsk, cset, NULL, false);
  5662. list_add_tail(&tsk->cg_list, &cset->dying_tasks);
  5663. cset->nr_tasks--;
  5664. if (dl_task(tsk))
  5665. dec_dl_tasks_cs(tsk);
  5666. WARN_ON_ONCE(cgroup_task_frozen(tsk));
  5667. if (unlikely(!(tsk->flags & PF_KTHREAD) &&
  5668. test_bit(CGRP_FREEZE, &task_dfl_cgroup(tsk)->flags)))
  5669. cgroup_update_frozen(task_dfl_cgroup(tsk));
  5670. spin_unlock_irq(&css_set_lock);
  5671. /* see cgroup_post_fork() for details */
  5672. do_each_subsys_mask(ss, i, have_exit_callback) {
  5673. ss->exit(tsk);
  5674. } while_each_subsys_mask();
  5675. }
  5676. void cgroup_release(struct task_struct *task)
  5677. {
  5678. struct cgroup_subsys *ss;
  5679. int ssid;
  5680. do_each_subsys_mask(ss, ssid, have_release_callback) {
  5681. ss->release(task);
  5682. } while_each_subsys_mask();
  5683. spin_lock_irq(&css_set_lock);
  5684. css_set_skip_task_iters(task_css_set(task), task);
  5685. list_del_init(&task->cg_list);
  5686. spin_unlock_irq(&css_set_lock);
  5687. }
  5688. void cgroup_free(struct task_struct *task)
  5689. {
  5690. struct css_set *cset = task_css_set(task);
  5691. put_css_set(cset);
  5692. }
  5693. static int __init cgroup_disable(char *str)
  5694. {
  5695. struct cgroup_subsys *ss;
  5696. char *token;
  5697. int i;
  5698. while ((token = strsep(&str, ",")) != NULL) {
  5699. if (!*token)
  5700. continue;
  5701. for_each_subsys(ss, i) {
  5702. if (strcmp(token, ss->name) &&
  5703. strcmp(token, ss->legacy_name))
  5704. continue;
  5705. static_branch_disable(cgroup_subsys_enabled_key[i]);
  5706. pr_info("Disabling %s control group subsystem\n",
  5707. ss->name);
  5708. }
  5709. for (i = 0; i < OPT_FEATURE_COUNT; i++) {
  5710. if (strcmp(token, cgroup_opt_feature_names[i]))
  5711. continue;
  5712. cgroup_feature_disable_mask |= 1 << i;
  5713. pr_info("Disabling %s control group feature\n",
  5714. cgroup_opt_feature_names[i]);
  5715. break;
  5716. }
  5717. }
  5718. return 1;
  5719. }
  5720. __setup("cgroup_disable=", cgroup_disable);
  5721. void __init __weak enable_debug_cgroup(void) { }
  5722. static int __init enable_cgroup_debug(char *str)
  5723. {
  5724. cgroup_debug = true;
  5725. enable_debug_cgroup();
  5726. return 1;
  5727. }
  5728. __setup("cgroup_debug", enable_cgroup_debug);
  5729. /**
  5730. * css_tryget_online_from_dir - get corresponding css from a cgroup dentry
  5731. * @dentry: directory dentry of interest
  5732. * @ss: subsystem of interest
  5733. *
  5734. * If @dentry is a directory for a cgroup which has @ss enabled on it, try
  5735. * to get the corresponding css and return it. If such css doesn't exist
  5736. * or can't be pinned, an ERR_PTR value is returned.
  5737. */
  5738. struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry,
  5739. struct cgroup_subsys *ss)
  5740. {
  5741. struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
  5742. struct file_system_type *s_type = dentry->d_sb->s_type;
  5743. struct cgroup_subsys_state *css = NULL;
  5744. struct cgroup *cgrp;
  5745. /* is @dentry a cgroup dir? */
  5746. if ((s_type != &cgroup_fs_type && s_type != &cgroup2_fs_type) ||
  5747. !kn || kernfs_type(kn) != KERNFS_DIR)
  5748. return ERR_PTR(-EBADF);
  5749. rcu_read_lock();
  5750. /*
  5751. * This path doesn't originate from kernfs and @kn could already
  5752. * have been or be removed at any point. @kn->priv is RCU
  5753. * protected for this access. See css_release_work_fn() for details.
  5754. */
  5755. cgrp = rcu_dereference(*(void __rcu __force **)&kn->priv);
  5756. if (cgrp)
  5757. css = cgroup_css(cgrp, ss);
  5758. if (!css || !css_tryget_online(css))
  5759. css = ERR_PTR(-ENOENT);
  5760. rcu_read_unlock();
  5761. return css;
  5762. }
  5763. /**
  5764. * css_from_id - lookup css by id
  5765. * @id: the cgroup id
  5766. * @ss: cgroup subsys to be looked into
  5767. *
  5768. * Returns the css if there's valid one with @id, otherwise returns NULL.
  5769. * Should be called under rcu_read_lock().
  5770. */
  5771. struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
  5772. {
  5773. WARN_ON_ONCE(!rcu_read_lock_held());
  5774. return idr_find(&ss->css_idr, id);
  5775. }
  5776. /**
  5777. * cgroup_get_from_path - lookup and get a cgroup from its default hierarchy path
  5778. * @path: path on the default hierarchy
  5779. *
  5780. * Find the cgroup at @path on the default hierarchy, increment its
  5781. * reference count and return it. Returns pointer to the found cgroup on
  5782. * success, ERR_PTR(-ENOENT) if @path doesn't exist or if the cgroup has already
  5783. * been released and ERR_PTR(-ENOTDIR) if @path points to a non-directory.
  5784. */
  5785. struct cgroup *cgroup_get_from_path(const char *path)
  5786. {
  5787. struct kernfs_node *kn;
  5788. struct cgroup *cgrp = ERR_PTR(-ENOENT);
  5789. struct cgroup *root_cgrp;
  5790. root_cgrp = current_cgns_cgroup_dfl();
  5791. kn = kernfs_walk_and_get(root_cgrp->kn, path);
  5792. if (!kn)
  5793. goto out;
  5794. if (kernfs_type(kn) != KERNFS_DIR) {
  5795. cgrp = ERR_PTR(-ENOTDIR);
  5796. goto out_kernfs;
  5797. }
  5798. rcu_read_lock();
  5799. cgrp = rcu_dereference(*(void __rcu __force **)&kn->priv);
  5800. if (!cgrp || !cgroup_tryget(cgrp))
  5801. cgrp = ERR_PTR(-ENOENT);
  5802. rcu_read_unlock();
  5803. out_kernfs:
  5804. kernfs_put(kn);
  5805. out:
  5806. return cgrp;
  5807. }
  5808. EXPORT_SYMBOL_GPL(cgroup_get_from_path);
  5809. /**
  5810. * cgroup_v1v2_get_from_fd - get a cgroup pointer from a fd
  5811. * @fd: fd obtained by open(cgroup_dir)
  5812. *
  5813. * Find the cgroup from a fd which should be obtained
  5814. * by opening a cgroup directory. Returns a pointer to the
  5815. * cgroup on success. ERR_PTR is returned if the cgroup
  5816. * cannot be found.
  5817. */
  5818. struct cgroup *cgroup_v1v2_get_from_fd(int fd)
  5819. {
  5820. struct cgroup *cgrp;
  5821. struct file *f;
  5822. f = fget_raw(fd);
  5823. if (!f)
  5824. return ERR_PTR(-EBADF);
  5825. cgrp = cgroup_v1v2_get_from_file(f);
  5826. fput(f);
  5827. return cgrp;
  5828. }
  5829. /**
  5830. * cgroup_get_from_fd - same as cgroup_v1v2_get_from_fd, but only supports
  5831. * cgroup2.
  5832. * @fd: fd obtained by open(cgroup2_dir)
  5833. */
  5834. struct cgroup *cgroup_get_from_fd(int fd)
  5835. {
  5836. struct cgroup *cgrp = cgroup_v1v2_get_from_fd(fd);
  5837. if (IS_ERR(cgrp))
  5838. return ERR_CAST(cgrp);
  5839. if (!cgroup_on_dfl(cgrp)) {
  5840. cgroup_put(cgrp);
  5841. return ERR_PTR(-EBADF);
  5842. }
  5843. return cgrp;
  5844. }
  5845. EXPORT_SYMBOL_GPL(cgroup_get_from_fd);
  5846. static u64 power_of_ten(int power)
  5847. {
  5848. u64 v = 1;
  5849. while (power--)
  5850. v *= 10;
  5851. return v;
  5852. }
  5853. /**
  5854. * cgroup_parse_float - parse a floating number
  5855. * @input: input string
  5856. * @dec_shift: number of decimal digits to shift
  5857. * @v: output
  5858. *
  5859. * Parse a decimal floating point number in @input and store the result in
  5860. * @v with decimal point right shifted @dec_shift times. For example, if
  5861. * @input is "12.3456" and @dec_shift is 3, *@v will be set to 12345.
  5862. * Returns 0 on success, -errno otherwise.
  5863. *
  5864. * There's nothing cgroup specific about this function except that it's
  5865. * currently the only user.
  5866. */
  5867. int cgroup_parse_float(const char *input, unsigned dec_shift, s64 *v)
  5868. {
  5869. s64 whole, frac = 0;
  5870. int fstart = 0, fend = 0, flen;
  5871. if (!sscanf(input, "%lld.%n%lld%n", &whole, &fstart, &frac, &fend))
  5872. return -EINVAL;
  5873. if (frac < 0)
  5874. return -EINVAL;
  5875. flen = fend > fstart ? fend - fstart : 0;
  5876. if (flen < dec_shift)
  5877. frac *= power_of_ten(dec_shift - flen);
  5878. else
  5879. frac = DIV_ROUND_CLOSEST_ULL(frac, power_of_ten(flen - dec_shift));
  5880. *v = whole * power_of_ten(dec_shift) + frac;
  5881. return 0;
  5882. }
  5883. /*
  5884. * sock->sk_cgrp_data handling. For more info, see sock_cgroup_data
  5885. * definition in cgroup-defs.h.
  5886. */
  5887. #ifdef CONFIG_SOCK_CGROUP_DATA
  5888. void cgroup_sk_alloc(struct sock_cgroup_data *skcd)
  5889. {
  5890. struct cgroup *cgroup;
  5891. rcu_read_lock();
  5892. /* Don't associate the sock with unrelated interrupted task's cgroup. */
  5893. if (in_interrupt()) {
  5894. cgroup = &cgrp_dfl_root.cgrp;
  5895. cgroup_get(cgroup);
  5896. goto out;
  5897. }
  5898. while (true) {
  5899. struct css_set *cset;
  5900. cset = task_css_set(current);
  5901. if (likely(cgroup_tryget(cset->dfl_cgrp))) {
  5902. cgroup = cset->dfl_cgrp;
  5903. break;
  5904. }
  5905. cpu_relax();
  5906. }
  5907. out:
  5908. skcd->cgroup = cgroup;
  5909. cgroup_bpf_get(cgroup);
  5910. rcu_read_unlock();
  5911. }
  5912. void cgroup_sk_clone(struct sock_cgroup_data *skcd)
  5913. {
  5914. struct cgroup *cgrp = sock_cgroup_ptr(skcd);
  5915. /*
  5916. * We might be cloning a socket which is left in an empty
  5917. * cgroup and the cgroup might have already been rmdir'd.
  5918. * Don't use cgroup_get_live().
  5919. */
  5920. cgroup_get(cgrp);
  5921. cgroup_bpf_get(cgrp);
  5922. }
  5923. void cgroup_sk_free(struct sock_cgroup_data *skcd)
  5924. {
  5925. struct cgroup *cgrp = sock_cgroup_ptr(skcd);
  5926. cgroup_bpf_put(cgrp);
  5927. cgroup_put(cgrp);
  5928. }
  5929. #endif /* CONFIG_SOCK_CGROUP_DATA */
  5930. #ifdef CONFIG_SYSFS
  5931. static ssize_t show_delegatable_files(struct cftype *files, char *buf,
  5932. ssize_t size, const char *prefix)
  5933. {
  5934. struct cftype *cft;
  5935. ssize_t ret = 0;
  5936. for (cft = files; cft && cft->name[0] != '\0'; cft++) {
  5937. if (!(cft->flags & CFTYPE_NS_DELEGATABLE))
  5938. continue;
  5939. if (prefix)
  5940. ret += snprintf(buf + ret, size - ret, "%s.", prefix);
  5941. ret += snprintf(buf + ret, size - ret, "%s\n", cft->name);
  5942. if (WARN_ON(ret >= size))
  5943. break;
  5944. }
  5945. return ret;
  5946. }
  5947. static ssize_t delegate_show(struct kobject *kobj, struct kobj_attribute *attr,
  5948. char *buf)
  5949. {
  5950. struct cgroup_subsys *ss;
  5951. int ssid;
  5952. ssize_t ret = 0;
  5953. ret = show_delegatable_files(cgroup_base_files, buf + ret,
  5954. PAGE_SIZE - ret, NULL);
  5955. if (cgroup_psi_enabled())
  5956. ret += show_delegatable_files(cgroup_psi_files, buf + ret,
  5957. PAGE_SIZE - ret, NULL);
  5958. for_each_subsys(ss, ssid)
  5959. ret += show_delegatable_files(ss->dfl_cftypes, buf + ret,
  5960. PAGE_SIZE - ret,
  5961. cgroup_subsys_name[ssid]);
  5962. return ret;
  5963. }
  5964. static struct kobj_attribute cgroup_delegate_attr = __ATTR_RO(delegate);
  5965. static ssize_t features_show(struct kobject *kobj, struct kobj_attribute *attr,
  5966. char *buf)
  5967. {
  5968. return snprintf(buf, PAGE_SIZE,
  5969. "nsdelegate\n"
  5970. "favordynmods\n"
  5971. "memory_localevents\n"
  5972. "memory_recursiveprot\n");
  5973. }
  5974. static struct kobj_attribute cgroup_features_attr = __ATTR_RO(features);
  5975. static struct attribute *cgroup_sysfs_attrs[] = {
  5976. &cgroup_delegate_attr.attr,
  5977. &cgroup_features_attr.attr,
  5978. NULL,
  5979. };
  5980. static const struct attribute_group cgroup_sysfs_attr_group = {
  5981. .attrs = cgroup_sysfs_attrs,
  5982. .name = "cgroup",
  5983. };
  5984. static int __init cgroup_sysfs_init(void)
  5985. {
  5986. return sysfs_create_group(kernel_kobj, &cgroup_sysfs_attr_group);
  5987. }
  5988. subsys_initcall(cgroup_sysfs_init);
  5989. #endif /* CONFIG_SYSFS */