cgroup.c 148 KB

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