cgroup.c 151 KB

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