cgroup.c 148 KB

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