cgroup.c 156 KB

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