cgroup.c 148 KB

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