cgroup.c 146 KB

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