cgroup.c 167 KB

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