cgroup.c 153 KB

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