cgroup.c 162 KB

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