cgroup.c 147 KB

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