cgroup.c 150 KB

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