cgroup.c 157 KB

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