cgroup.c 143 KB

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