cgroup.c 158 KB

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