cgroup.c 158 KB

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