cgroup.c 138 KB

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