cgroup.c 145 KB

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