cgroup.c 152 KB

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