cgroup.c 153 KB

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