cgroup.c 138 KB

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