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