cgroup.c 144 KB

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