cgroup.c 160 KB

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