cgroup.c 176 KB

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