cgroup.c 147 KB

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