cgroup.c 142 KB

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