cgroup.c 145 KB

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