cgroup.c 151 KB

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