cgroup.c 137 KB

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