memcontrol.c 141 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508
  1. /* memcontrol.c - Memory Controller
  2. *
  3. * Copyright IBM Corporation, 2007
  4. * Author Balbir Singh <balbir@linux.vnet.ibm.com>
  5. *
  6. * Copyright 2007 OpenVZ SWsoft Inc
  7. * Author: Pavel Emelianov <xemul@openvz.org>
  8. *
  9. * Memory thresholds
  10. * Copyright (C) 2009 Nokia Corporation
  11. * Author: Kirill A. Shutemov
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. */
  23. #include <linux/res_counter.h>
  24. #include <linux/memcontrol.h>
  25. #include <linux/cgroup.h>
  26. #include <linux/mm.h>
  27. #include <linux/hugetlb.h>
  28. #include <linux/pagemap.h>
  29. #include <linux/smp.h>
  30. #include <linux/page-flags.h>
  31. #include <linux/backing-dev.h>
  32. #include <linux/bit_spinlock.h>
  33. #include <linux/rcupdate.h>
  34. #include <linux/limits.h>
  35. #include <linux/export.h>
  36. #include <linux/mutex.h>
  37. #include <linux/rbtree.h>
  38. #include <linux/slab.h>
  39. #include <linux/swap.h>
  40. #include <linux/swapops.h>
  41. #include <linux/spinlock.h>
  42. #include <linux/eventfd.h>
  43. #include <linux/sort.h>
  44. #include <linux/fs.h>
  45. #include <linux/seq_file.h>
  46. #include <linux/vmalloc.h>
  47. #include <linux/mm_inline.h>
  48. #include <linux/page_cgroup.h>
  49. #include <linux/cpu.h>
  50. #include <linux/oom.h>
  51. #include "internal.h"
  52. #include <asm/uaccess.h>
  53. #include <trace/events/vmscan.h>
  54. struct cgroup_subsys mem_cgroup_subsys __read_mostly;
  55. #define MEM_CGROUP_RECLAIM_RETRIES 5
  56. struct mem_cgroup *root_mem_cgroup __read_mostly;
  57. #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
  58. /* Turned on only when memory cgroup is enabled && really_do_swap_account = 1 */
  59. int do_swap_account __read_mostly;
  60. /* for remember boot option*/
  61. #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP_ENABLED
  62. static int really_do_swap_account __initdata = 1;
  63. #else
  64. static int really_do_swap_account __initdata = 0;
  65. #endif
  66. #else
  67. #define do_swap_account (0)
  68. #endif
  69. /*
  70. * Statistics for memory cgroup.
  71. */
  72. enum mem_cgroup_stat_index {
  73. /*
  74. * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss.
  75. */
  76. MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */
  77. MEM_CGROUP_STAT_RSS, /* # of pages charged as anon rss */
  78. MEM_CGROUP_STAT_FILE_MAPPED, /* # of pages charged as file rss */
  79. MEM_CGROUP_STAT_SWAPOUT, /* # of pages, swapped out */
  80. MEM_CGROUP_STAT_DATA, /* end of data requires synchronization */
  81. MEM_CGROUP_ON_MOVE, /* someone is moving account between groups */
  82. MEM_CGROUP_STAT_NSTATS,
  83. };
  84. enum mem_cgroup_events_index {
  85. MEM_CGROUP_EVENTS_PGPGIN, /* # of pages paged in */
  86. MEM_CGROUP_EVENTS_PGPGOUT, /* # of pages paged out */
  87. MEM_CGROUP_EVENTS_COUNT, /* # of pages paged in/out */
  88. MEM_CGROUP_EVENTS_PGFAULT, /* # of page-faults */
  89. MEM_CGROUP_EVENTS_PGMAJFAULT, /* # of major page-faults */
  90. MEM_CGROUP_EVENTS_NSTATS,
  91. };
  92. /*
  93. * Per memcg event counter is incremented at every pagein/pageout. With THP,
  94. * it will be incremated by the number of pages. This counter is used for
  95. * for trigger some periodic events. This is straightforward and better
  96. * than using jiffies etc. to handle periodic memcg event.
  97. */
  98. enum mem_cgroup_events_target {
  99. MEM_CGROUP_TARGET_THRESH,
  100. MEM_CGROUP_TARGET_SOFTLIMIT,
  101. MEM_CGROUP_TARGET_NUMAINFO,
  102. MEM_CGROUP_NTARGETS,
  103. };
  104. #define THRESHOLDS_EVENTS_TARGET (128)
  105. #define SOFTLIMIT_EVENTS_TARGET (1024)
  106. #define NUMAINFO_EVENTS_TARGET (1024)
  107. struct mem_cgroup_stat_cpu {
  108. long count[MEM_CGROUP_STAT_NSTATS];
  109. unsigned long events[MEM_CGROUP_EVENTS_NSTATS];
  110. unsigned long targets[MEM_CGROUP_NTARGETS];
  111. };
  112. /*
  113. * per-zone information in memory controller.
  114. */
  115. struct mem_cgroup_per_zone {
  116. /*
  117. * spin_lock to protect the per cgroup LRU
  118. */
  119. struct list_head lists[NR_LRU_LISTS];
  120. unsigned long count[NR_LRU_LISTS];
  121. struct zone_reclaim_stat reclaim_stat;
  122. struct rb_node tree_node; /* RB tree node */
  123. unsigned long long usage_in_excess;/* Set to the value by which */
  124. /* the soft limit is exceeded*/
  125. bool on_tree;
  126. struct mem_cgroup *mem; /* Back pointer, we cannot */
  127. /* use container_of */
  128. };
  129. /* Macro for accessing counter */
  130. #define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)])
  131. struct mem_cgroup_per_node {
  132. struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
  133. };
  134. struct mem_cgroup_lru_info {
  135. struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES];
  136. };
  137. /*
  138. * Cgroups above their limits are maintained in a RB-Tree, independent of
  139. * their hierarchy representation
  140. */
  141. struct mem_cgroup_tree_per_zone {
  142. struct rb_root rb_root;
  143. spinlock_t lock;
  144. };
  145. struct mem_cgroup_tree_per_node {
  146. struct mem_cgroup_tree_per_zone rb_tree_per_zone[MAX_NR_ZONES];
  147. };
  148. struct mem_cgroup_tree {
  149. struct mem_cgroup_tree_per_node *rb_tree_per_node[MAX_NUMNODES];
  150. };
  151. static struct mem_cgroup_tree soft_limit_tree __read_mostly;
  152. struct mem_cgroup_threshold {
  153. struct eventfd_ctx *eventfd;
  154. u64 threshold;
  155. };
  156. /* For threshold */
  157. struct mem_cgroup_threshold_ary {
  158. /* An array index points to threshold just below usage. */
  159. int current_threshold;
  160. /* Size of entries[] */
  161. unsigned int size;
  162. /* Array of thresholds */
  163. struct mem_cgroup_threshold entries[0];
  164. };
  165. struct mem_cgroup_thresholds {
  166. /* Primary thresholds array */
  167. struct mem_cgroup_threshold_ary *primary;
  168. /*
  169. * Spare threshold array.
  170. * This is needed to make mem_cgroup_unregister_event() "never fail".
  171. * It must be able to store at least primary->size - 1 entries.
  172. */
  173. struct mem_cgroup_threshold_ary *spare;
  174. };
  175. /* for OOM */
  176. struct mem_cgroup_eventfd_list {
  177. struct list_head list;
  178. struct eventfd_ctx *eventfd;
  179. };
  180. static void mem_cgroup_threshold(struct mem_cgroup *mem);
  181. static void mem_cgroup_oom_notify(struct mem_cgroup *mem);
  182. /*
  183. * The memory controller data structure. The memory controller controls both
  184. * page cache and RSS per cgroup. We would eventually like to provide
  185. * statistics based on the statistics developed by Rik Van Riel for clock-pro,
  186. * to help the administrator determine what knobs to tune.
  187. *
  188. * TODO: Add a water mark for the memory controller. Reclaim will begin when
  189. * we hit the water mark. May be even add a low water mark, such that
  190. * no reclaim occurs from a cgroup at it's low water mark, this is
  191. * a feature that will be implemented much later in the future.
  192. */
  193. struct mem_cgroup {
  194. struct cgroup_subsys_state css;
  195. /*
  196. * the counter to account for memory usage
  197. */
  198. struct res_counter res;
  199. /*
  200. * the counter to account for mem+swap usage.
  201. */
  202. struct res_counter memsw;
  203. /*
  204. * Per cgroup active and inactive list, similar to the
  205. * per zone LRU lists.
  206. */
  207. struct mem_cgroup_lru_info info;
  208. /*
  209. * While reclaiming in a hierarchy, we cache the last child we
  210. * reclaimed from.
  211. */
  212. int last_scanned_child;
  213. int last_scanned_node;
  214. #if MAX_NUMNODES > 1
  215. nodemask_t scan_nodes;
  216. atomic_t numainfo_events;
  217. atomic_t numainfo_updating;
  218. #endif
  219. /*
  220. * Should the accounting and control be hierarchical, per subtree?
  221. */
  222. bool use_hierarchy;
  223. bool oom_lock;
  224. atomic_t under_oom;
  225. atomic_t refcnt;
  226. int swappiness;
  227. /* OOM-Killer disable */
  228. int oom_kill_disable;
  229. /* set when res.limit == memsw.limit */
  230. bool memsw_is_minimum;
  231. /* protect arrays of thresholds */
  232. struct mutex thresholds_lock;
  233. /* thresholds for memory usage. RCU-protected */
  234. struct mem_cgroup_thresholds thresholds;
  235. /* thresholds for mem+swap usage. RCU-protected */
  236. struct mem_cgroup_thresholds memsw_thresholds;
  237. /* For oom notifier event fd */
  238. struct list_head oom_notify;
  239. /*
  240. * Should we move charges of a task when a task is moved into this
  241. * mem_cgroup ? And what type of charges should we move ?
  242. */
  243. unsigned long move_charge_at_immigrate;
  244. /*
  245. * percpu counter.
  246. */
  247. struct mem_cgroup_stat_cpu *stat;
  248. /*
  249. * used when a cpu is offlined or other synchronizations
  250. * See mem_cgroup_read_stat().
  251. */
  252. struct mem_cgroup_stat_cpu nocpu_base;
  253. spinlock_t pcp_counter_lock;
  254. };
  255. /* Stuffs for move charges at task migration. */
  256. /*
  257. * Types of charges to be moved. "move_charge_at_immitgrate" is treated as a
  258. * left-shifted bitmap of these types.
  259. */
  260. enum move_type {
  261. MOVE_CHARGE_TYPE_ANON, /* private anonymous page and swap of it */
  262. MOVE_CHARGE_TYPE_FILE, /* file page(including tmpfs) and swap of it */
  263. NR_MOVE_TYPE,
  264. };
  265. /* "mc" and its members are protected by cgroup_mutex */
  266. static struct move_charge_struct {
  267. spinlock_t lock; /* for from, to */
  268. struct mem_cgroup *from;
  269. struct mem_cgroup *to;
  270. unsigned long precharge;
  271. unsigned long moved_charge;
  272. unsigned long moved_swap;
  273. struct task_struct *moving_task; /* a task moving charges */
  274. wait_queue_head_t waitq; /* a waitq for other context */
  275. } mc = {
  276. .lock = __SPIN_LOCK_UNLOCKED(mc.lock),
  277. .waitq = __WAIT_QUEUE_HEAD_INITIALIZER(mc.waitq),
  278. };
  279. static bool move_anon(void)
  280. {
  281. return test_bit(MOVE_CHARGE_TYPE_ANON,
  282. &mc.to->move_charge_at_immigrate);
  283. }
  284. static bool move_file(void)
  285. {
  286. return test_bit(MOVE_CHARGE_TYPE_FILE,
  287. &mc.to->move_charge_at_immigrate);
  288. }
  289. /*
  290. * Maximum loops in mem_cgroup_hierarchical_reclaim(), used for soft
  291. * limit reclaim to prevent infinite loops, if they ever occur.
  292. */
  293. #define MEM_CGROUP_MAX_RECLAIM_LOOPS (100)
  294. #define MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS (2)
  295. enum charge_type {
  296. MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
  297. MEM_CGROUP_CHARGE_TYPE_MAPPED,
  298. MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */
  299. MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */
  300. MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
  301. MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */
  302. NR_CHARGE_TYPE,
  303. };
  304. /* for encoding cft->private value on file */
  305. #define _MEM (0)
  306. #define _MEMSWAP (1)
  307. #define _OOM_TYPE (2)
  308. #define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val))
  309. #define MEMFILE_TYPE(val) (((val) >> 16) & 0xffff)
  310. #define MEMFILE_ATTR(val) ((val) & 0xffff)
  311. /* Used for OOM nofiier */
  312. #define OOM_CONTROL (0)
  313. /*
  314. * Reclaim flags for mem_cgroup_hierarchical_reclaim
  315. */
  316. #define MEM_CGROUP_RECLAIM_NOSWAP_BIT 0x0
  317. #define MEM_CGROUP_RECLAIM_NOSWAP (1 << MEM_CGROUP_RECLAIM_NOSWAP_BIT)
  318. #define MEM_CGROUP_RECLAIM_SHRINK_BIT 0x1
  319. #define MEM_CGROUP_RECLAIM_SHRINK (1 << MEM_CGROUP_RECLAIM_SHRINK_BIT)
  320. #define MEM_CGROUP_RECLAIM_SOFT_BIT 0x2
  321. #define MEM_CGROUP_RECLAIM_SOFT (1 << MEM_CGROUP_RECLAIM_SOFT_BIT)
  322. static void mem_cgroup_get(struct mem_cgroup *mem);
  323. static void mem_cgroup_put(struct mem_cgroup *mem);
  324. static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem);
  325. static void drain_all_stock_async(struct mem_cgroup *mem);
  326. static struct mem_cgroup_per_zone *
  327. mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid)
  328. {
  329. return &mem->info.nodeinfo[nid]->zoneinfo[zid];
  330. }
  331. struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *mem)
  332. {
  333. return &mem->css;
  334. }
  335. static struct mem_cgroup_per_zone *
  336. page_cgroup_zoneinfo(struct mem_cgroup *mem, struct page *page)
  337. {
  338. int nid = page_to_nid(page);
  339. int zid = page_zonenum(page);
  340. return mem_cgroup_zoneinfo(mem, nid, zid);
  341. }
  342. static struct mem_cgroup_tree_per_zone *
  343. soft_limit_tree_node_zone(int nid, int zid)
  344. {
  345. return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
  346. }
  347. static struct mem_cgroup_tree_per_zone *
  348. soft_limit_tree_from_page(struct page *page)
  349. {
  350. int nid = page_to_nid(page);
  351. int zid = page_zonenum(page);
  352. return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
  353. }
  354. static void
  355. __mem_cgroup_insert_exceeded(struct mem_cgroup *mem,
  356. struct mem_cgroup_per_zone *mz,
  357. struct mem_cgroup_tree_per_zone *mctz,
  358. unsigned long long new_usage_in_excess)
  359. {
  360. struct rb_node **p = &mctz->rb_root.rb_node;
  361. struct rb_node *parent = NULL;
  362. struct mem_cgroup_per_zone *mz_node;
  363. if (mz->on_tree)
  364. return;
  365. mz->usage_in_excess = new_usage_in_excess;
  366. if (!mz->usage_in_excess)
  367. return;
  368. while (*p) {
  369. parent = *p;
  370. mz_node = rb_entry(parent, struct mem_cgroup_per_zone,
  371. tree_node);
  372. if (mz->usage_in_excess < mz_node->usage_in_excess)
  373. p = &(*p)->rb_left;
  374. /*
  375. * We can't avoid mem cgroups that are over their soft
  376. * limit by the same amount
  377. */
  378. else if (mz->usage_in_excess >= mz_node->usage_in_excess)
  379. p = &(*p)->rb_right;
  380. }
  381. rb_link_node(&mz->tree_node, parent, p);
  382. rb_insert_color(&mz->tree_node, &mctz->rb_root);
  383. mz->on_tree = true;
  384. }
  385. static void
  386. __mem_cgroup_remove_exceeded(struct mem_cgroup *mem,
  387. struct mem_cgroup_per_zone *mz,
  388. struct mem_cgroup_tree_per_zone *mctz)
  389. {
  390. if (!mz->on_tree)
  391. return;
  392. rb_erase(&mz->tree_node, &mctz->rb_root);
  393. mz->on_tree = false;
  394. }
  395. static void
  396. mem_cgroup_remove_exceeded(struct mem_cgroup *mem,
  397. struct mem_cgroup_per_zone *mz,
  398. struct mem_cgroup_tree_per_zone *mctz)
  399. {
  400. spin_lock(&mctz->lock);
  401. __mem_cgroup_remove_exceeded(mem, mz, mctz);
  402. spin_unlock(&mctz->lock);
  403. }
  404. static void mem_cgroup_update_tree(struct mem_cgroup *mem, struct page *page)
  405. {
  406. unsigned long long excess;
  407. struct mem_cgroup_per_zone *mz;
  408. struct mem_cgroup_tree_per_zone *mctz;
  409. int nid = page_to_nid(page);
  410. int zid = page_zonenum(page);
  411. mctz = soft_limit_tree_from_page(page);
  412. /*
  413. * Necessary to update all ancestors when hierarchy is used.
  414. * because their event counter is not touched.
  415. */
  416. for (; mem; mem = parent_mem_cgroup(mem)) {
  417. mz = mem_cgroup_zoneinfo(mem, nid, zid);
  418. excess = res_counter_soft_limit_excess(&mem->res);
  419. /*
  420. * We have to update the tree if mz is on RB-tree or
  421. * mem is over its softlimit.
  422. */
  423. if (excess || mz->on_tree) {
  424. spin_lock(&mctz->lock);
  425. /* if on-tree, remove it */
  426. if (mz->on_tree)
  427. __mem_cgroup_remove_exceeded(mem, mz, mctz);
  428. /*
  429. * Insert again. mz->usage_in_excess will be updated.
  430. * If excess is 0, no tree ops.
  431. */
  432. __mem_cgroup_insert_exceeded(mem, mz, mctz, excess);
  433. spin_unlock(&mctz->lock);
  434. }
  435. }
  436. }
  437. static void mem_cgroup_remove_from_trees(struct mem_cgroup *mem)
  438. {
  439. int node, zone;
  440. struct mem_cgroup_per_zone *mz;
  441. struct mem_cgroup_tree_per_zone *mctz;
  442. for_each_node_state(node, N_POSSIBLE) {
  443. for (zone = 0; zone < MAX_NR_ZONES; zone++) {
  444. mz = mem_cgroup_zoneinfo(mem, node, zone);
  445. mctz = soft_limit_tree_node_zone(node, zone);
  446. mem_cgroup_remove_exceeded(mem, mz, mctz);
  447. }
  448. }
  449. }
  450. static struct mem_cgroup_per_zone *
  451. __mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
  452. {
  453. struct rb_node *rightmost = NULL;
  454. struct mem_cgroup_per_zone *mz;
  455. retry:
  456. mz = NULL;
  457. rightmost = rb_last(&mctz->rb_root);
  458. if (!rightmost)
  459. goto done; /* Nothing to reclaim from */
  460. mz = rb_entry(rightmost, struct mem_cgroup_per_zone, tree_node);
  461. /*
  462. * Remove the node now but someone else can add it back,
  463. * we will to add it back at the end of reclaim to its correct
  464. * position in the tree.
  465. */
  466. __mem_cgroup_remove_exceeded(mz->mem, mz, mctz);
  467. if (!res_counter_soft_limit_excess(&mz->mem->res) ||
  468. !css_tryget(&mz->mem->css))
  469. goto retry;
  470. done:
  471. return mz;
  472. }
  473. static struct mem_cgroup_per_zone *
  474. mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
  475. {
  476. struct mem_cgroup_per_zone *mz;
  477. spin_lock(&mctz->lock);
  478. mz = __mem_cgroup_largest_soft_limit_node(mctz);
  479. spin_unlock(&mctz->lock);
  480. return mz;
  481. }
  482. /*
  483. * Implementation Note: reading percpu statistics for memcg.
  484. *
  485. * Both of vmstat[] and percpu_counter has threshold and do periodic
  486. * synchronization to implement "quick" read. There are trade-off between
  487. * reading cost and precision of value. Then, we may have a chance to implement
  488. * a periodic synchronizion of counter in memcg's counter.
  489. *
  490. * But this _read() function is used for user interface now. The user accounts
  491. * memory usage by memory cgroup and he _always_ requires exact value because
  492. * he accounts memory. Even if we provide quick-and-fuzzy read, we always
  493. * have to visit all online cpus and make sum. So, for now, unnecessary
  494. * synchronization is not implemented. (just implemented for cpu hotplug)
  495. *
  496. * If there are kernel internal actions which can make use of some not-exact
  497. * value, and reading all cpu value can be performance bottleneck in some
  498. * common workload, threashold and synchonization as vmstat[] should be
  499. * implemented.
  500. */
  501. static long mem_cgroup_read_stat(struct mem_cgroup *mem,
  502. enum mem_cgroup_stat_index idx)
  503. {
  504. long val = 0;
  505. int cpu;
  506. get_online_cpus();
  507. for_each_online_cpu(cpu)
  508. val += per_cpu(mem->stat->count[idx], cpu);
  509. #ifdef CONFIG_HOTPLUG_CPU
  510. spin_lock(&mem->pcp_counter_lock);
  511. val += mem->nocpu_base.count[idx];
  512. spin_unlock(&mem->pcp_counter_lock);
  513. #endif
  514. put_online_cpus();
  515. return val;
  516. }
  517. static void mem_cgroup_swap_statistics(struct mem_cgroup *mem,
  518. bool charge)
  519. {
  520. int val = (charge) ? 1 : -1;
  521. this_cpu_add(mem->stat->count[MEM_CGROUP_STAT_SWAPOUT], val);
  522. }
  523. void mem_cgroup_pgfault(struct mem_cgroup *mem, int val)
  524. {
  525. this_cpu_add(mem->stat->events[MEM_CGROUP_EVENTS_PGFAULT], val);
  526. }
  527. void mem_cgroup_pgmajfault(struct mem_cgroup *mem, int val)
  528. {
  529. this_cpu_add(mem->stat->events[MEM_CGROUP_EVENTS_PGMAJFAULT], val);
  530. }
  531. static unsigned long mem_cgroup_read_events(struct mem_cgroup *mem,
  532. enum mem_cgroup_events_index idx)
  533. {
  534. unsigned long val = 0;
  535. int cpu;
  536. for_each_online_cpu(cpu)
  537. val += per_cpu(mem->stat->events[idx], cpu);
  538. #ifdef CONFIG_HOTPLUG_CPU
  539. spin_lock(&mem->pcp_counter_lock);
  540. val += mem->nocpu_base.events[idx];
  541. spin_unlock(&mem->pcp_counter_lock);
  542. #endif
  543. return val;
  544. }
  545. static void mem_cgroup_charge_statistics(struct mem_cgroup *mem,
  546. bool file, int nr_pages)
  547. {
  548. preempt_disable();
  549. if (file)
  550. __this_cpu_add(mem->stat->count[MEM_CGROUP_STAT_CACHE], nr_pages);
  551. else
  552. __this_cpu_add(mem->stat->count[MEM_CGROUP_STAT_RSS], nr_pages);
  553. /* pagein of a big page is an event. So, ignore page size */
  554. if (nr_pages > 0)
  555. __this_cpu_inc(mem->stat->events[MEM_CGROUP_EVENTS_PGPGIN]);
  556. else {
  557. __this_cpu_inc(mem->stat->events[MEM_CGROUP_EVENTS_PGPGOUT]);
  558. nr_pages = -nr_pages; /* for event */
  559. }
  560. __this_cpu_add(mem->stat->events[MEM_CGROUP_EVENTS_COUNT], nr_pages);
  561. preempt_enable();
  562. }
  563. unsigned long
  564. mem_cgroup_zone_nr_lru_pages(struct mem_cgroup *mem, int nid, int zid,
  565. unsigned int lru_mask)
  566. {
  567. struct mem_cgroup_per_zone *mz;
  568. enum lru_list l;
  569. unsigned long ret = 0;
  570. mz = mem_cgroup_zoneinfo(mem, nid, zid);
  571. for_each_lru(l) {
  572. if (BIT(l) & lru_mask)
  573. ret += MEM_CGROUP_ZSTAT(mz, l);
  574. }
  575. return ret;
  576. }
  577. static unsigned long
  578. mem_cgroup_node_nr_lru_pages(struct mem_cgroup *mem,
  579. int nid, unsigned int lru_mask)
  580. {
  581. u64 total = 0;
  582. int zid;
  583. for (zid = 0; zid < MAX_NR_ZONES; zid++)
  584. total += mem_cgroup_zone_nr_lru_pages(mem, nid, zid, lru_mask);
  585. return total;
  586. }
  587. static unsigned long mem_cgroup_nr_lru_pages(struct mem_cgroup *mem,
  588. unsigned int lru_mask)
  589. {
  590. int nid;
  591. u64 total = 0;
  592. for_each_node_state(nid, N_HIGH_MEMORY)
  593. total += mem_cgroup_node_nr_lru_pages(mem, nid, lru_mask);
  594. return total;
  595. }
  596. static bool __memcg_event_check(struct mem_cgroup *mem, int target)
  597. {
  598. unsigned long val, next;
  599. val = this_cpu_read(mem->stat->events[MEM_CGROUP_EVENTS_COUNT]);
  600. next = this_cpu_read(mem->stat->targets[target]);
  601. /* from time_after() in jiffies.h */
  602. return ((long)next - (long)val < 0);
  603. }
  604. static void __mem_cgroup_target_update(struct mem_cgroup *mem, int target)
  605. {
  606. unsigned long val, next;
  607. val = this_cpu_read(mem->stat->events[MEM_CGROUP_EVENTS_COUNT]);
  608. switch (target) {
  609. case MEM_CGROUP_TARGET_THRESH:
  610. next = val + THRESHOLDS_EVENTS_TARGET;
  611. break;
  612. case MEM_CGROUP_TARGET_SOFTLIMIT:
  613. next = val + SOFTLIMIT_EVENTS_TARGET;
  614. break;
  615. case MEM_CGROUP_TARGET_NUMAINFO:
  616. next = val + NUMAINFO_EVENTS_TARGET;
  617. break;
  618. default:
  619. return;
  620. }
  621. this_cpu_write(mem->stat->targets[target], next);
  622. }
  623. /*
  624. * Check events in order.
  625. *
  626. */
  627. static void memcg_check_events(struct mem_cgroup *mem, struct page *page)
  628. {
  629. /* threshold event is triggered in finer grain than soft limit */
  630. if (unlikely(__memcg_event_check(mem, MEM_CGROUP_TARGET_THRESH))) {
  631. mem_cgroup_threshold(mem);
  632. __mem_cgroup_target_update(mem, MEM_CGROUP_TARGET_THRESH);
  633. if (unlikely(__memcg_event_check(mem,
  634. MEM_CGROUP_TARGET_SOFTLIMIT))) {
  635. mem_cgroup_update_tree(mem, page);
  636. __mem_cgroup_target_update(mem,
  637. MEM_CGROUP_TARGET_SOFTLIMIT);
  638. }
  639. #if MAX_NUMNODES > 1
  640. if (unlikely(__memcg_event_check(mem,
  641. MEM_CGROUP_TARGET_NUMAINFO))) {
  642. atomic_inc(&mem->numainfo_events);
  643. __mem_cgroup_target_update(mem,
  644. MEM_CGROUP_TARGET_NUMAINFO);
  645. }
  646. #endif
  647. }
  648. }
  649. static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
  650. {
  651. return container_of(cgroup_subsys_state(cont,
  652. mem_cgroup_subsys_id), struct mem_cgroup,
  653. css);
  654. }
  655. struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
  656. {
  657. /*
  658. * mm_update_next_owner() may clear mm->owner to NULL
  659. * if it races with swapoff, page migration, etc.
  660. * So this can be called with p == NULL.
  661. */
  662. if (unlikely(!p))
  663. return NULL;
  664. return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
  665. struct mem_cgroup, css);
  666. }
  667. struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
  668. {
  669. struct mem_cgroup *mem = NULL;
  670. if (!mm)
  671. return NULL;
  672. /*
  673. * Because we have no locks, mm->owner's may be being moved to other
  674. * cgroup. We use css_tryget() here even if this looks
  675. * pessimistic (rather than adding locks here).
  676. */
  677. rcu_read_lock();
  678. do {
  679. mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
  680. if (unlikely(!mem))
  681. break;
  682. } while (!css_tryget(&mem->css));
  683. rcu_read_unlock();
  684. return mem;
  685. }
  686. /* The caller has to guarantee "mem" exists before calling this */
  687. static struct mem_cgroup *mem_cgroup_start_loop(struct mem_cgroup *mem)
  688. {
  689. struct cgroup_subsys_state *css;
  690. int found;
  691. if (!mem) /* ROOT cgroup has the smallest ID */
  692. return root_mem_cgroup; /*css_put/get against root is ignored*/
  693. if (!mem->use_hierarchy) {
  694. if (css_tryget(&mem->css))
  695. return mem;
  696. return NULL;
  697. }
  698. rcu_read_lock();
  699. /*
  700. * searching a memory cgroup which has the smallest ID under given
  701. * ROOT cgroup. (ID >= 1)
  702. */
  703. css = css_get_next(&mem_cgroup_subsys, 1, &mem->css, &found);
  704. if (css && css_tryget(css))
  705. mem = container_of(css, struct mem_cgroup, css);
  706. else
  707. mem = NULL;
  708. rcu_read_unlock();
  709. return mem;
  710. }
  711. static struct mem_cgroup *mem_cgroup_get_next(struct mem_cgroup *iter,
  712. struct mem_cgroup *root,
  713. bool cond)
  714. {
  715. int nextid = css_id(&iter->css) + 1;
  716. int found;
  717. int hierarchy_used;
  718. struct cgroup_subsys_state *css;
  719. hierarchy_used = iter->use_hierarchy;
  720. css_put(&iter->css);
  721. /* If no ROOT, walk all, ignore hierarchy */
  722. if (!cond || (root && !hierarchy_used))
  723. return NULL;
  724. if (!root)
  725. root = root_mem_cgroup;
  726. do {
  727. iter = NULL;
  728. rcu_read_lock();
  729. css = css_get_next(&mem_cgroup_subsys, nextid,
  730. &root->css, &found);
  731. if (css && css_tryget(css))
  732. iter = container_of(css, struct mem_cgroup, css);
  733. rcu_read_unlock();
  734. /* If css is NULL, no more cgroups will be found */
  735. nextid = found + 1;
  736. } while (css && !iter);
  737. return iter;
  738. }
  739. /*
  740. * for_eacn_mem_cgroup_tree() for visiting all cgroup under tree. Please
  741. * be careful that "break" loop is not allowed. We have reference count.
  742. * Instead of that modify "cond" to be false and "continue" to exit the loop.
  743. */
  744. #define for_each_mem_cgroup_tree_cond(iter, root, cond) \
  745. for (iter = mem_cgroup_start_loop(root);\
  746. iter != NULL;\
  747. iter = mem_cgroup_get_next(iter, root, cond))
  748. #define for_each_mem_cgroup_tree(iter, root) \
  749. for_each_mem_cgroup_tree_cond(iter, root, true)
  750. #define for_each_mem_cgroup_all(iter) \
  751. for_each_mem_cgroup_tree_cond(iter, NULL, true)
  752. static inline bool mem_cgroup_is_root(struct mem_cgroup *mem)
  753. {
  754. return (mem == root_mem_cgroup);
  755. }
  756. void mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx)
  757. {
  758. struct mem_cgroup *mem;
  759. if (!mm)
  760. return;
  761. rcu_read_lock();
  762. mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
  763. if (unlikely(!mem))
  764. goto out;
  765. switch (idx) {
  766. case PGMAJFAULT:
  767. mem_cgroup_pgmajfault(mem, 1);
  768. break;
  769. case PGFAULT:
  770. mem_cgroup_pgfault(mem, 1);
  771. break;
  772. default:
  773. BUG();
  774. }
  775. out:
  776. rcu_read_unlock();
  777. }
  778. EXPORT_SYMBOL(mem_cgroup_count_vm_event);
  779. /*
  780. * Following LRU functions are allowed to be used without PCG_LOCK.
  781. * Operations are called by routine of global LRU independently from memcg.
  782. * What we have to take care of here is validness of pc->mem_cgroup.
  783. *
  784. * Changes to pc->mem_cgroup happens when
  785. * 1. charge
  786. * 2. moving account
  787. * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
  788. * It is added to LRU before charge.
  789. * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
  790. * When moving account, the page is not on LRU. It's isolated.
  791. */
  792. void mem_cgroup_del_lru_list(struct page *page, enum lru_list lru)
  793. {
  794. struct page_cgroup *pc;
  795. struct mem_cgroup_per_zone *mz;
  796. if (mem_cgroup_disabled())
  797. return;
  798. pc = lookup_page_cgroup(page);
  799. /* can happen while we handle swapcache. */
  800. if (!TestClearPageCgroupAcctLRU(pc))
  801. return;
  802. VM_BUG_ON(!pc->mem_cgroup);
  803. /*
  804. * We don't check PCG_USED bit. It's cleared when the "page" is finally
  805. * removed from global LRU.
  806. */
  807. mz = page_cgroup_zoneinfo(pc->mem_cgroup, page);
  808. /* huge page split is done under lru_lock. so, we have no races. */
  809. MEM_CGROUP_ZSTAT(mz, lru) -= 1 << compound_order(page);
  810. if (mem_cgroup_is_root(pc->mem_cgroup))
  811. return;
  812. VM_BUG_ON(list_empty(&pc->lru));
  813. list_del_init(&pc->lru);
  814. }
  815. void mem_cgroup_del_lru(struct page *page)
  816. {
  817. mem_cgroup_del_lru_list(page, page_lru(page));
  818. }
  819. /*
  820. * Writeback is about to end against a page which has been marked for immediate
  821. * reclaim. If it still appears to be reclaimable, move it to the tail of the
  822. * inactive list.
  823. */
  824. void mem_cgroup_rotate_reclaimable_page(struct page *page)
  825. {
  826. struct mem_cgroup_per_zone *mz;
  827. struct page_cgroup *pc;
  828. enum lru_list lru = page_lru(page);
  829. if (mem_cgroup_disabled())
  830. return;
  831. pc = lookup_page_cgroup(page);
  832. /* unused or root page is not rotated. */
  833. if (!PageCgroupUsed(pc))
  834. return;
  835. /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */
  836. smp_rmb();
  837. if (mem_cgroup_is_root(pc->mem_cgroup))
  838. return;
  839. mz = page_cgroup_zoneinfo(pc->mem_cgroup, page);
  840. list_move_tail(&pc->lru, &mz->lists[lru]);
  841. }
  842. void mem_cgroup_rotate_lru_list(struct page *page, enum lru_list lru)
  843. {
  844. struct mem_cgroup_per_zone *mz;
  845. struct page_cgroup *pc;
  846. if (mem_cgroup_disabled())
  847. return;
  848. pc = lookup_page_cgroup(page);
  849. /* unused or root page is not rotated. */
  850. if (!PageCgroupUsed(pc))
  851. return;
  852. /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */
  853. smp_rmb();
  854. if (mem_cgroup_is_root(pc->mem_cgroup))
  855. return;
  856. mz = page_cgroup_zoneinfo(pc->mem_cgroup, page);
  857. list_move(&pc->lru, &mz->lists[lru]);
  858. }
  859. void mem_cgroup_add_lru_list(struct page *page, enum lru_list lru)
  860. {
  861. struct page_cgroup *pc;
  862. struct mem_cgroup_per_zone *mz;
  863. if (mem_cgroup_disabled())
  864. return;
  865. pc = lookup_page_cgroup(page);
  866. VM_BUG_ON(PageCgroupAcctLRU(pc));
  867. if (!PageCgroupUsed(pc))
  868. return;
  869. /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */
  870. smp_rmb();
  871. mz = page_cgroup_zoneinfo(pc->mem_cgroup, page);
  872. /* huge page split is done under lru_lock. so, we have no races. */
  873. MEM_CGROUP_ZSTAT(mz, lru) += 1 << compound_order(page);
  874. SetPageCgroupAcctLRU(pc);
  875. if (mem_cgroup_is_root(pc->mem_cgroup))
  876. return;
  877. list_add(&pc->lru, &mz->lists[lru]);
  878. }
  879. /*
  880. * At handling SwapCache and other FUSE stuff, pc->mem_cgroup may be changed
  881. * while it's linked to lru because the page may be reused after it's fully
  882. * uncharged. To handle that, unlink page_cgroup from LRU when charge it again.
  883. * It's done under lock_page and expected that zone->lru_lock isnever held.
  884. */
  885. static void mem_cgroup_lru_del_before_commit(struct page *page)
  886. {
  887. unsigned long flags;
  888. struct zone *zone = page_zone(page);
  889. struct page_cgroup *pc = lookup_page_cgroup(page);
  890. /*
  891. * Doing this check without taking ->lru_lock seems wrong but this
  892. * is safe. Because if page_cgroup's USED bit is unset, the page
  893. * will not be added to any memcg's LRU. If page_cgroup's USED bit is
  894. * set, the commit after this will fail, anyway.
  895. * This all charge/uncharge is done under some mutual execustion.
  896. * So, we don't need to taking care of changes in USED bit.
  897. */
  898. if (likely(!PageLRU(page)))
  899. return;
  900. spin_lock_irqsave(&zone->lru_lock, flags);
  901. /*
  902. * Forget old LRU when this page_cgroup is *not* used. This Used bit
  903. * is guarded by lock_page() because the page is SwapCache.
  904. */
  905. if (!PageCgroupUsed(pc))
  906. mem_cgroup_del_lru_list(page, page_lru(page));
  907. spin_unlock_irqrestore(&zone->lru_lock, flags);
  908. }
  909. static void mem_cgroup_lru_add_after_commit(struct page *page)
  910. {
  911. unsigned long flags;
  912. struct zone *zone = page_zone(page);
  913. struct page_cgroup *pc = lookup_page_cgroup(page);
  914. /* taking care of that the page is added to LRU while we commit it */
  915. if (likely(!PageLRU(page)))
  916. return;
  917. spin_lock_irqsave(&zone->lru_lock, flags);
  918. /* link when the page is linked to LRU but page_cgroup isn't */
  919. if (PageLRU(page) && !PageCgroupAcctLRU(pc))
  920. mem_cgroup_add_lru_list(page, page_lru(page));
  921. spin_unlock_irqrestore(&zone->lru_lock, flags);
  922. }
  923. void mem_cgroup_move_lists(struct page *page,
  924. enum lru_list from, enum lru_list to)
  925. {
  926. if (mem_cgroup_disabled())
  927. return;
  928. mem_cgroup_del_lru_list(page, from);
  929. mem_cgroup_add_lru_list(page, to);
  930. }
  931. /*
  932. * Checks whether given mem is same or in the root_mem's
  933. * hierarchy subtree
  934. */
  935. static bool mem_cgroup_same_or_subtree(const struct mem_cgroup *root_mem,
  936. struct mem_cgroup *mem)
  937. {
  938. if (root_mem != mem) {
  939. return (root_mem->use_hierarchy &&
  940. css_is_ancestor(&mem->css, &root_mem->css));
  941. }
  942. return true;
  943. }
  944. int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
  945. {
  946. int ret;
  947. struct mem_cgroup *curr = NULL;
  948. struct task_struct *p;
  949. p = find_lock_task_mm(task);
  950. if (!p)
  951. return 0;
  952. curr = try_get_mem_cgroup_from_mm(p->mm);
  953. task_unlock(p);
  954. if (!curr)
  955. return 0;
  956. /*
  957. * We should check use_hierarchy of "mem" not "curr". Because checking
  958. * use_hierarchy of "curr" here make this function true if hierarchy is
  959. * enabled in "curr" and "curr" is a child of "mem" in *cgroup*
  960. * hierarchy(even if use_hierarchy is disabled in "mem").
  961. */
  962. ret = mem_cgroup_same_or_subtree(mem, curr);
  963. css_put(&curr->css);
  964. return ret;
  965. }
  966. static int calc_inactive_ratio(struct mem_cgroup *memcg, unsigned long *present_pages)
  967. {
  968. unsigned long active;
  969. unsigned long inactive;
  970. unsigned long gb;
  971. unsigned long inactive_ratio;
  972. inactive = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_INACTIVE_ANON));
  973. active = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_ACTIVE_ANON));
  974. gb = (inactive + active) >> (30 - PAGE_SHIFT);
  975. if (gb)
  976. inactive_ratio = int_sqrt(10 * gb);
  977. else
  978. inactive_ratio = 1;
  979. if (present_pages) {
  980. present_pages[0] = inactive;
  981. present_pages[1] = active;
  982. }
  983. return inactive_ratio;
  984. }
  985. int mem_cgroup_inactive_anon_is_low(struct mem_cgroup *memcg)
  986. {
  987. unsigned long active;
  988. unsigned long inactive;
  989. unsigned long present_pages[2];
  990. unsigned long inactive_ratio;
  991. inactive_ratio = calc_inactive_ratio(memcg, present_pages);
  992. inactive = present_pages[0];
  993. active = present_pages[1];
  994. if (inactive * inactive_ratio < active)
  995. return 1;
  996. return 0;
  997. }
  998. int mem_cgroup_inactive_file_is_low(struct mem_cgroup *memcg)
  999. {
  1000. unsigned long active;
  1001. unsigned long inactive;
  1002. inactive = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_INACTIVE_FILE));
  1003. active = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_ACTIVE_FILE));
  1004. return (active > inactive);
  1005. }
  1006. struct zone_reclaim_stat *mem_cgroup_get_reclaim_stat(struct mem_cgroup *memcg,
  1007. struct zone *zone)
  1008. {
  1009. int nid = zone_to_nid(zone);
  1010. int zid = zone_idx(zone);
  1011. struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
  1012. return &mz->reclaim_stat;
  1013. }
  1014. struct zone_reclaim_stat *
  1015. mem_cgroup_get_reclaim_stat_from_page(struct page *page)
  1016. {
  1017. struct page_cgroup *pc;
  1018. struct mem_cgroup_per_zone *mz;
  1019. if (mem_cgroup_disabled())
  1020. return NULL;
  1021. pc = lookup_page_cgroup(page);
  1022. if (!PageCgroupUsed(pc))
  1023. return NULL;
  1024. /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */
  1025. smp_rmb();
  1026. mz = page_cgroup_zoneinfo(pc->mem_cgroup, page);
  1027. return &mz->reclaim_stat;
  1028. }
  1029. unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
  1030. struct list_head *dst,
  1031. unsigned long *scanned, int order,
  1032. int mode, struct zone *z,
  1033. struct mem_cgroup *mem_cont,
  1034. int active, int file)
  1035. {
  1036. unsigned long nr_taken = 0;
  1037. struct page *page;
  1038. unsigned long scan;
  1039. LIST_HEAD(pc_list);
  1040. struct list_head *src;
  1041. struct page_cgroup *pc, *tmp;
  1042. int nid = zone_to_nid(z);
  1043. int zid = zone_idx(z);
  1044. struct mem_cgroup_per_zone *mz;
  1045. int lru = LRU_FILE * file + active;
  1046. int ret;
  1047. BUG_ON(!mem_cont);
  1048. mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
  1049. src = &mz->lists[lru];
  1050. scan = 0;
  1051. list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
  1052. if (scan >= nr_to_scan)
  1053. break;
  1054. if (unlikely(!PageCgroupUsed(pc)))
  1055. continue;
  1056. page = lookup_cgroup_page(pc);
  1057. if (unlikely(!PageLRU(page)))
  1058. continue;
  1059. scan++;
  1060. ret = __isolate_lru_page(page, mode, file);
  1061. switch (ret) {
  1062. case 0:
  1063. list_move(&page->lru, dst);
  1064. mem_cgroup_del_lru(page);
  1065. nr_taken += hpage_nr_pages(page);
  1066. break;
  1067. case -EBUSY:
  1068. /* we don't affect global LRU but rotate in our LRU */
  1069. mem_cgroup_rotate_lru_list(page, page_lru(page));
  1070. break;
  1071. default:
  1072. break;
  1073. }
  1074. }
  1075. *scanned = scan;
  1076. trace_mm_vmscan_memcg_isolate(0, nr_to_scan, scan, nr_taken,
  1077. 0, 0, 0, mode);
  1078. return nr_taken;
  1079. }
  1080. #define mem_cgroup_from_res_counter(counter, member) \
  1081. container_of(counter, struct mem_cgroup, member)
  1082. /**
  1083. * mem_cgroup_margin - calculate chargeable space of a memory cgroup
  1084. * @mem: the memory cgroup
  1085. *
  1086. * Returns the maximum amount of memory @mem can be charged with, in
  1087. * pages.
  1088. */
  1089. static unsigned long mem_cgroup_margin(struct mem_cgroup *mem)
  1090. {
  1091. unsigned long long margin;
  1092. margin = res_counter_margin(&mem->res);
  1093. if (do_swap_account)
  1094. margin = min(margin, res_counter_margin(&mem->memsw));
  1095. return margin >> PAGE_SHIFT;
  1096. }
  1097. int mem_cgroup_swappiness(struct mem_cgroup *memcg)
  1098. {
  1099. struct cgroup *cgrp = memcg->css.cgroup;
  1100. /* root ? */
  1101. if (cgrp->parent == NULL)
  1102. return vm_swappiness;
  1103. return memcg->swappiness;
  1104. }
  1105. static void mem_cgroup_start_move(struct mem_cgroup *mem)
  1106. {
  1107. int cpu;
  1108. get_online_cpus();
  1109. spin_lock(&mem->pcp_counter_lock);
  1110. for_each_online_cpu(cpu)
  1111. per_cpu(mem->stat->count[MEM_CGROUP_ON_MOVE], cpu) += 1;
  1112. mem->nocpu_base.count[MEM_CGROUP_ON_MOVE] += 1;
  1113. spin_unlock(&mem->pcp_counter_lock);
  1114. put_online_cpus();
  1115. synchronize_rcu();
  1116. }
  1117. static void mem_cgroup_end_move(struct mem_cgroup *mem)
  1118. {
  1119. int cpu;
  1120. if (!mem)
  1121. return;
  1122. get_online_cpus();
  1123. spin_lock(&mem->pcp_counter_lock);
  1124. for_each_online_cpu(cpu)
  1125. per_cpu(mem->stat->count[MEM_CGROUP_ON_MOVE], cpu) -= 1;
  1126. mem->nocpu_base.count[MEM_CGROUP_ON_MOVE] -= 1;
  1127. spin_unlock(&mem->pcp_counter_lock);
  1128. put_online_cpus();
  1129. }
  1130. /*
  1131. * 2 routines for checking "mem" is under move_account() or not.
  1132. *
  1133. * mem_cgroup_stealed() - checking a cgroup is mc.from or not. This is used
  1134. * for avoiding race in accounting. If true,
  1135. * pc->mem_cgroup may be overwritten.
  1136. *
  1137. * mem_cgroup_under_move() - checking a cgroup is mc.from or mc.to or
  1138. * under hierarchy of moving cgroups. This is for
  1139. * waiting at hith-memory prressure caused by "move".
  1140. */
  1141. static bool mem_cgroup_stealed(struct mem_cgroup *mem)
  1142. {
  1143. VM_BUG_ON(!rcu_read_lock_held());
  1144. return this_cpu_read(mem->stat->count[MEM_CGROUP_ON_MOVE]) > 0;
  1145. }
  1146. static bool mem_cgroup_under_move(struct mem_cgroup *mem)
  1147. {
  1148. struct mem_cgroup *from;
  1149. struct mem_cgroup *to;
  1150. bool ret = false;
  1151. /*
  1152. * Unlike task_move routines, we access mc.to, mc.from not under
  1153. * mutual exclusion by cgroup_mutex. Here, we take spinlock instead.
  1154. */
  1155. spin_lock(&mc.lock);
  1156. from = mc.from;
  1157. to = mc.to;
  1158. if (!from)
  1159. goto unlock;
  1160. ret = mem_cgroup_same_or_subtree(mem, from)
  1161. || mem_cgroup_same_or_subtree(mem, to);
  1162. unlock:
  1163. spin_unlock(&mc.lock);
  1164. return ret;
  1165. }
  1166. static bool mem_cgroup_wait_acct_move(struct mem_cgroup *mem)
  1167. {
  1168. if (mc.moving_task && current != mc.moving_task) {
  1169. if (mem_cgroup_under_move(mem)) {
  1170. DEFINE_WAIT(wait);
  1171. prepare_to_wait(&mc.waitq, &wait, TASK_INTERRUPTIBLE);
  1172. /* moving charge context might have finished. */
  1173. if (mc.moving_task)
  1174. schedule();
  1175. finish_wait(&mc.waitq, &wait);
  1176. return true;
  1177. }
  1178. }
  1179. return false;
  1180. }
  1181. /**
  1182. * mem_cgroup_print_oom_info: Called from OOM with tasklist_lock held in read mode.
  1183. * @memcg: The memory cgroup that went over limit
  1184. * @p: Task that is going to be killed
  1185. *
  1186. * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
  1187. * enabled
  1188. */
  1189. void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
  1190. {
  1191. struct cgroup *task_cgrp;
  1192. struct cgroup *mem_cgrp;
  1193. /*
  1194. * Need a buffer in BSS, can't rely on allocations. The code relies
  1195. * on the assumption that OOM is serialized for memory controller.
  1196. * If this assumption is broken, revisit this code.
  1197. */
  1198. static char memcg_name[PATH_MAX];
  1199. int ret;
  1200. if (!memcg || !p)
  1201. return;
  1202. rcu_read_lock();
  1203. mem_cgrp = memcg->css.cgroup;
  1204. task_cgrp = task_cgroup(p, mem_cgroup_subsys_id);
  1205. ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX);
  1206. if (ret < 0) {
  1207. /*
  1208. * Unfortunately, we are unable to convert to a useful name
  1209. * But we'll still print out the usage information
  1210. */
  1211. rcu_read_unlock();
  1212. goto done;
  1213. }
  1214. rcu_read_unlock();
  1215. printk(KERN_INFO "Task in %s killed", memcg_name);
  1216. rcu_read_lock();
  1217. ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX);
  1218. if (ret < 0) {
  1219. rcu_read_unlock();
  1220. goto done;
  1221. }
  1222. rcu_read_unlock();
  1223. /*
  1224. * Continues from above, so we don't need an KERN_ level
  1225. */
  1226. printk(KERN_CONT " as a result of limit of %s\n", memcg_name);
  1227. done:
  1228. printk(KERN_INFO "memory: usage %llukB, limit %llukB, failcnt %llu\n",
  1229. res_counter_read_u64(&memcg->res, RES_USAGE) >> 10,
  1230. res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10,
  1231. res_counter_read_u64(&memcg->res, RES_FAILCNT));
  1232. printk(KERN_INFO "memory+swap: usage %llukB, limit %llukB, "
  1233. "failcnt %llu\n",
  1234. res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10,
  1235. res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10,
  1236. res_counter_read_u64(&memcg->memsw, RES_FAILCNT));
  1237. }
  1238. /*
  1239. * This function returns the number of memcg under hierarchy tree. Returns
  1240. * 1(self count) if no children.
  1241. */
  1242. static int mem_cgroup_count_children(struct mem_cgroup *mem)
  1243. {
  1244. int num = 0;
  1245. struct mem_cgroup *iter;
  1246. for_each_mem_cgroup_tree(iter, mem)
  1247. num++;
  1248. return num;
  1249. }
  1250. /*
  1251. * Return the memory (and swap, if configured) limit for a memcg.
  1252. */
  1253. u64 mem_cgroup_get_limit(struct mem_cgroup *memcg)
  1254. {
  1255. u64 limit;
  1256. u64 memsw;
  1257. limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
  1258. limit += total_swap_pages << PAGE_SHIFT;
  1259. memsw = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
  1260. /*
  1261. * If memsw is finite and limits the amount of swap space available
  1262. * to this memcg, return that limit.
  1263. */
  1264. return min(limit, memsw);
  1265. }
  1266. /*
  1267. * Visit the first child (need not be the first child as per the ordering
  1268. * of the cgroup list, since we track last_scanned_child) of @mem and use
  1269. * that to reclaim free pages from.
  1270. */
  1271. static struct mem_cgroup *
  1272. mem_cgroup_select_victim(struct mem_cgroup *root_mem)
  1273. {
  1274. struct mem_cgroup *ret = NULL;
  1275. struct cgroup_subsys_state *css;
  1276. int nextid, found;
  1277. if (!root_mem->use_hierarchy) {
  1278. css_get(&root_mem->css);
  1279. ret = root_mem;
  1280. }
  1281. while (!ret) {
  1282. rcu_read_lock();
  1283. nextid = root_mem->last_scanned_child + 1;
  1284. css = css_get_next(&mem_cgroup_subsys, nextid, &root_mem->css,
  1285. &found);
  1286. if (css && css_tryget(css))
  1287. ret = container_of(css, struct mem_cgroup, css);
  1288. rcu_read_unlock();
  1289. /* Updates scanning parameter */
  1290. if (!css) {
  1291. /* this means start scan from ID:1 */
  1292. root_mem->last_scanned_child = 0;
  1293. } else
  1294. root_mem->last_scanned_child = found;
  1295. }
  1296. return ret;
  1297. }
  1298. /**
  1299. * test_mem_cgroup_node_reclaimable
  1300. * @mem: the target memcg
  1301. * @nid: the node ID to be checked.
  1302. * @noswap : specify true here if the user wants flle only information.
  1303. *
  1304. * This function returns whether the specified memcg contains any
  1305. * reclaimable pages on a node. Returns true if there are any reclaimable
  1306. * pages in the node.
  1307. */
  1308. static bool test_mem_cgroup_node_reclaimable(struct mem_cgroup *mem,
  1309. int nid, bool noswap)
  1310. {
  1311. if (mem_cgroup_node_nr_lru_pages(mem, nid, LRU_ALL_FILE))
  1312. return true;
  1313. if (noswap || !total_swap_pages)
  1314. return false;
  1315. if (mem_cgroup_node_nr_lru_pages(mem, nid, LRU_ALL_ANON))
  1316. return true;
  1317. return false;
  1318. }
  1319. #if MAX_NUMNODES > 1
  1320. /*
  1321. * Always updating the nodemask is not very good - even if we have an empty
  1322. * list or the wrong list here, we can start from some node and traverse all
  1323. * nodes based on the zonelist. So update the list loosely once per 10 secs.
  1324. *
  1325. */
  1326. static void mem_cgroup_may_update_nodemask(struct mem_cgroup *mem)
  1327. {
  1328. int nid;
  1329. /*
  1330. * numainfo_events > 0 means there was at least NUMAINFO_EVENTS_TARGET
  1331. * pagein/pageout changes since the last update.
  1332. */
  1333. if (!atomic_read(&mem->numainfo_events))
  1334. return;
  1335. if (atomic_inc_return(&mem->numainfo_updating) > 1)
  1336. return;
  1337. /* make a nodemask where this memcg uses memory from */
  1338. mem->scan_nodes = node_states[N_HIGH_MEMORY];
  1339. for_each_node_mask(nid, node_states[N_HIGH_MEMORY]) {
  1340. if (!test_mem_cgroup_node_reclaimable(mem, nid, false))
  1341. node_clear(nid, mem->scan_nodes);
  1342. }
  1343. atomic_set(&mem->numainfo_events, 0);
  1344. atomic_set(&mem->numainfo_updating, 0);
  1345. }
  1346. /*
  1347. * Selecting a node where we start reclaim from. Because what we need is just
  1348. * reducing usage counter, start from anywhere is O,K. Considering
  1349. * memory reclaim from current node, there are pros. and cons.
  1350. *
  1351. * Freeing memory from current node means freeing memory from a node which
  1352. * we'll use or we've used. So, it may make LRU bad. And if several threads
  1353. * hit limits, it will see a contention on a node. But freeing from remote
  1354. * node means more costs for memory reclaim because of memory latency.
  1355. *
  1356. * Now, we use round-robin. Better algorithm is welcomed.
  1357. */
  1358. int mem_cgroup_select_victim_node(struct mem_cgroup *mem)
  1359. {
  1360. int node;
  1361. mem_cgroup_may_update_nodemask(mem);
  1362. node = mem->last_scanned_node;
  1363. node = next_node(node, mem->scan_nodes);
  1364. if (node == MAX_NUMNODES)
  1365. node = first_node(mem->scan_nodes);
  1366. /*
  1367. * We call this when we hit limit, not when pages are added to LRU.
  1368. * No LRU may hold pages because all pages are UNEVICTABLE or
  1369. * memcg is too small and all pages are not on LRU. In that case,
  1370. * we use curret node.
  1371. */
  1372. if (unlikely(node == MAX_NUMNODES))
  1373. node = numa_node_id();
  1374. mem->last_scanned_node = node;
  1375. return node;
  1376. }
  1377. /*
  1378. * Check all nodes whether it contains reclaimable pages or not.
  1379. * For quick scan, we make use of scan_nodes. This will allow us to skip
  1380. * unused nodes. But scan_nodes is lazily updated and may not cotain
  1381. * enough new information. We need to do double check.
  1382. */
  1383. bool mem_cgroup_reclaimable(struct mem_cgroup *mem, bool noswap)
  1384. {
  1385. int nid;
  1386. /*
  1387. * quick check...making use of scan_node.
  1388. * We can skip unused nodes.
  1389. */
  1390. if (!nodes_empty(mem->scan_nodes)) {
  1391. for (nid = first_node(mem->scan_nodes);
  1392. nid < MAX_NUMNODES;
  1393. nid = next_node(nid, mem->scan_nodes)) {
  1394. if (test_mem_cgroup_node_reclaimable(mem, nid, noswap))
  1395. return true;
  1396. }
  1397. }
  1398. /*
  1399. * Check rest of nodes.
  1400. */
  1401. for_each_node_state(nid, N_HIGH_MEMORY) {
  1402. if (node_isset(nid, mem->scan_nodes))
  1403. continue;
  1404. if (test_mem_cgroup_node_reclaimable(mem, nid, noswap))
  1405. return true;
  1406. }
  1407. return false;
  1408. }
  1409. #else
  1410. int mem_cgroup_select_victim_node(struct mem_cgroup *mem)
  1411. {
  1412. return 0;
  1413. }
  1414. bool mem_cgroup_reclaimable(struct mem_cgroup *mem, bool noswap)
  1415. {
  1416. return test_mem_cgroup_node_reclaimable(mem, 0, noswap);
  1417. }
  1418. #endif
  1419. /*
  1420. * Scan the hierarchy if needed to reclaim memory. We remember the last child
  1421. * we reclaimed from, so that we don't end up penalizing one child extensively
  1422. * based on its position in the children list.
  1423. *
  1424. * root_mem is the original ancestor that we've been reclaim from.
  1425. *
  1426. * We give up and return to the caller when we visit root_mem twice.
  1427. * (other groups can be removed while we're walking....)
  1428. *
  1429. * If shrink==true, for avoiding to free too much, this returns immedieately.
  1430. */
  1431. static int mem_cgroup_hierarchical_reclaim(struct mem_cgroup *root_mem,
  1432. struct zone *zone,
  1433. gfp_t gfp_mask,
  1434. unsigned long reclaim_options,
  1435. unsigned long *total_scanned)
  1436. {
  1437. struct mem_cgroup *victim;
  1438. int ret, total = 0;
  1439. int loop = 0;
  1440. bool noswap = reclaim_options & MEM_CGROUP_RECLAIM_NOSWAP;
  1441. bool shrink = reclaim_options & MEM_CGROUP_RECLAIM_SHRINK;
  1442. bool check_soft = reclaim_options & MEM_CGROUP_RECLAIM_SOFT;
  1443. unsigned long excess;
  1444. unsigned long nr_scanned;
  1445. excess = res_counter_soft_limit_excess(&root_mem->res) >> PAGE_SHIFT;
  1446. /* If memsw_is_minimum==1, swap-out is of-no-use. */
  1447. if (!check_soft && !shrink && root_mem->memsw_is_minimum)
  1448. noswap = true;
  1449. while (1) {
  1450. victim = mem_cgroup_select_victim(root_mem);
  1451. if (victim == root_mem) {
  1452. loop++;
  1453. /*
  1454. * We are not draining per cpu cached charges during
  1455. * soft limit reclaim because global reclaim doesn't
  1456. * care about charges. It tries to free some memory and
  1457. * charges will not give any.
  1458. */
  1459. if (!check_soft && loop >= 1)
  1460. drain_all_stock_async(root_mem);
  1461. if (loop >= 2) {
  1462. /*
  1463. * If we have not been able to reclaim
  1464. * anything, it might because there are
  1465. * no reclaimable pages under this hierarchy
  1466. */
  1467. if (!check_soft || !total) {
  1468. css_put(&victim->css);
  1469. break;
  1470. }
  1471. /*
  1472. * We want to do more targeted reclaim.
  1473. * excess >> 2 is not to excessive so as to
  1474. * reclaim too much, nor too less that we keep
  1475. * coming back to reclaim from this cgroup
  1476. */
  1477. if (total >= (excess >> 2) ||
  1478. (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS)) {
  1479. css_put(&victim->css);
  1480. break;
  1481. }
  1482. }
  1483. }
  1484. if (!mem_cgroup_reclaimable(victim, noswap)) {
  1485. /* this cgroup's local usage == 0 */
  1486. css_put(&victim->css);
  1487. continue;
  1488. }
  1489. /* we use swappiness of local cgroup */
  1490. if (check_soft) {
  1491. ret = mem_cgroup_shrink_node_zone(victim, gfp_mask,
  1492. noswap, zone, &nr_scanned);
  1493. *total_scanned += nr_scanned;
  1494. } else
  1495. ret = try_to_free_mem_cgroup_pages(victim, gfp_mask,
  1496. noswap);
  1497. css_put(&victim->css);
  1498. /*
  1499. * At shrinking usage, we can't check we should stop here or
  1500. * reclaim more. It's depends on callers. last_scanned_child
  1501. * will work enough for keeping fairness under tree.
  1502. */
  1503. if (shrink)
  1504. return ret;
  1505. total += ret;
  1506. if (check_soft) {
  1507. if (!res_counter_soft_limit_excess(&root_mem->res))
  1508. return total;
  1509. } else if (mem_cgroup_margin(root_mem))
  1510. return total;
  1511. }
  1512. return total;
  1513. }
  1514. /*
  1515. * Check OOM-Killer is already running under our hierarchy.
  1516. * If someone is running, return false.
  1517. * Has to be called with memcg_oom_lock
  1518. */
  1519. static bool mem_cgroup_oom_lock(struct mem_cgroup *mem)
  1520. {
  1521. struct mem_cgroup *iter, *failed = NULL;
  1522. bool cond = true;
  1523. for_each_mem_cgroup_tree_cond(iter, mem, cond) {
  1524. if (iter->oom_lock) {
  1525. /*
  1526. * this subtree of our hierarchy is already locked
  1527. * so we cannot give a lock.
  1528. */
  1529. failed = iter;
  1530. cond = false;
  1531. } else
  1532. iter->oom_lock = true;
  1533. }
  1534. if (!failed)
  1535. return true;
  1536. /*
  1537. * OK, we failed to lock the whole subtree so we have to clean up
  1538. * what we set up to the failing subtree
  1539. */
  1540. cond = true;
  1541. for_each_mem_cgroup_tree_cond(iter, mem, cond) {
  1542. if (iter == failed) {
  1543. cond = false;
  1544. continue;
  1545. }
  1546. iter->oom_lock = false;
  1547. }
  1548. return false;
  1549. }
  1550. /*
  1551. * Has to be called with memcg_oom_lock
  1552. */
  1553. static int mem_cgroup_oom_unlock(struct mem_cgroup *mem)
  1554. {
  1555. struct mem_cgroup *iter;
  1556. for_each_mem_cgroup_tree(iter, mem)
  1557. iter->oom_lock = false;
  1558. return 0;
  1559. }
  1560. static void mem_cgroup_mark_under_oom(struct mem_cgroup *mem)
  1561. {
  1562. struct mem_cgroup *iter;
  1563. for_each_mem_cgroup_tree(iter, mem)
  1564. atomic_inc(&iter->under_oom);
  1565. }
  1566. static void mem_cgroup_unmark_under_oom(struct mem_cgroup *mem)
  1567. {
  1568. struct mem_cgroup *iter;
  1569. /*
  1570. * When a new child is created while the hierarchy is under oom,
  1571. * mem_cgroup_oom_lock() may not be called. We have to use
  1572. * atomic_add_unless() here.
  1573. */
  1574. for_each_mem_cgroup_tree(iter, mem)
  1575. atomic_add_unless(&iter->under_oom, -1, 0);
  1576. }
  1577. static DEFINE_SPINLOCK(memcg_oom_lock);
  1578. static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq);
  1579. struct oom_wait_info {
  1580. struct mem_cgroup *mem;
  1581. wait_queue_t wait;
  1582. };
  1583. static int memcg_oom_wake_function(wait_queue_t *wait,
  1584. unsigned mode, int sync, void *arg)
  1585. {
  1586. struct mem_cgroup *wake_mem = (struct mem_cgroup *)arg,
  1587. *oom_wait_mem;
  1588. struct oom_wait_info *oom_wait_info;
  1589. oom_wait_info = container_of(wait, struct oom_wait_info, wait);
  1590. oom_wait_mem = oom_wait_info->mem;
  1591. /*
  1592. * Both of oom_wait_info->mem and wake_mem are stable under us.
  1593. * Then we can use css_is_ancestor without taking care of RCU.
  1594. */
  1595. if (!mem_cgroup_same_or_subtree(oom_wait_mem, wake_mem)
  1596. && !mem_cgroup_same_or_subtree(wake_mem, oom_wait_mem))
  1597. return 0;
  1598. return autoremove_wake_function(wait, mode, sync, arg);
  1599. }
  1600. static void memcg_wakeup_oom(struct mem_cgroup *mem)
  1601. {
  1602. /* for filtering, pass "mem" as argument. */
  1603. __wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, mem);
  1604. }
  1605. static void memcg_oom_recover(struct mem_cgroup *mem)
  1606. {
  1607. if (mem && atomic_read(&mem->under_oom))
  1608. memcg_wakeup_oom(mem);
  1609. }
  1610. /*
  1611. * try to call OOM killer. returns false if we should exit memory-reclaim loop.
  1612. */
  1613. bool mem_cgroup_handle_oom(struct mem_cgroup *mem, gfp_t mask)
  1614. {
  1615. struct oom_wait_info owait;
  1616. bool locked, need_to_kill;
  1617. owait.mem = mem;
  1618. owait.wait.flags = 0;
  1619. owait.wait.func = memcg_oom_wake_function;
  1620. owait.wait.private = current;
  1621. INIT_LIST_HEAD(&owait.wait.task_list);
  1622. need_to_kill = true;
  1623. mem_cgroup_mark_under_oom(mem);
  1624. /* At first, try to OOM lock hierarchy under mem.*/
  1625. spin_lock(&memcg_oom_lock);
  1626. locked = mem_cgroup_oom_lock(mem);
  1627. /*
  1628. * Even if signal_pending(), we can't quit charge() loop without
  1629. * accounting. So, UNINTERRUPTIBLE is appropriate. But SIGKILL
  1630. * under OOM is always welcomed, use TASK_KILLABLE here.
  1631. */
  1632. prepare_to_wait(&memcg_oom_waitq, &owait.wait, TASK_KILLABLE);
  1633. if (!locked || mem->oom_kill_disable)
  1634. need_to_kill = false;
  1635. if (locked)
  1636. mem_cgroup_oom_notify(mem);
  1637. spin_unlock(&memcg_oom_lock);
  1638. if (need_to_kill) {
  1639. finish_wait(&memcg_oom_waitq, &owait.wait);
  1640. mem_cgroup_out_of_memory(mem, mask);
  1641. } else {
  1642. schedule();
  1643. finish_wait(&memcg_oom_waitq, &owait.wait);
  1644. }
  1645. spin_lock(&memcg_oom_lock);
  1646. if (locked)
  1647. mem_cgroup_oom_unlock(mem);
  1648. memcg_wakeup_oom(mem);
  1649. spin_unlock(&memcg_oom_lock);
  1650. mem_cgroup_unmark_under_oom(mem);
  1651. if (test_thread_flag(TIF_MEMDIE) || fatal_signal_pending(current))
  1652. return false;
  1653. /* Give chance to dying process */
  1654. schedule_timeout(1);
  1655. return true;
  1656. }
  1657. /*
  1658. * Currently used to update mapped file statistics, but the routine can be
  1659. * generalized to update other statistics as well.
  1660. *
  1661. * Notes: Race condition
  1662. *
  1663. * We usually use page_cgroup_lock() for accessing page_cgroup member but
  1664. * it tends to be costly. But considering some conditions, we doesn't need
  1665. * to do so _always_.
  1666. *
  1667. * Considering "charge", lock_page_cgroup() is not required because all
  1668. * file-stat operations happen after a page is attached to radix-tree. There
  1669. * are no race with "charge".
  1670. *
  1671. * Considering "uncharge", we know that memcg doesn't clear pc->mem_cgroup
  1672. * at "uncharge" intentionally. So, we always see valid pc->mem_cgroup even
  1673. * if there are race with "uncharge". Statistics itself is properly handled
  1674. * by flags.
  1675. *
  1676. * Considering "move", this is an only case we see a race. To make the race
  1677. * small, we check MEM_CGROUP_ON_MOVE percpu value and detect there are
  1678. * possibility of race condition. If there is, we take a lock.
  1679. */
  1680. void mem_cgroup_update_page_stat(struct page *page,
  1681. enum mem_cgroup_page_stat_item idx, int val)
  1682. {
  1683. struct mem_cgroup *mem;
  1684. struct page_cgroup *pc = lookup_page_cgroup(page);
  1685. bool need_unlock = false;
  1686. unsigned long uninitialized_var(flags);
  1687. if (unlikely(!pc))
  1688. return;
  1689. rcu_read_lock();
  1690. mem = pc->mem_cgroup;
  1691. if (unlikely(!mem || !PageCgroupUsed(pc)))
  1692. goto out;
  1693. /* pc->mem_cgroup is unstable ? */
  1694. if (unlikely(mem_cgroup_stealed(mem)) || PageTransHuge(page)) {
  1695. /* take a lock against to access pc->mem_cgroup */
  1696. move_lock_page_cgroup(pc, &flags);
  1697. need_unlock = true;
  1698. mem = pc->mem_cgroup;
  1699. if (!mem || !PageCgroupUsed(pc))
  1700. goto out;
  1701. }
  1702. switch (idx) {
  1703. case MEMCG_NR_FILE_MAPPED:
  1704. if (val > 0)
  1705. SetPageCgroupFileMapped(pc);
  1706. else if (!page_mapped(page))
  1707. ClearPageCgroupFileMapped(pc);
  1708. idx = MEM_CGROUP_STAT_FILE_MAPPED;
  1709. break;
  1710. default:
  1711. BUG();
  1712. }
  1713. this_cpu_add(mem->stat->count[idx], val);
  1714. out:
  1715. if (unlikely(need_unlock))
  1716. move_unlock_page_cgroup(pc, &flags);
  1717. rcu_read_unlock();
  1718. return;
  1719. }
  1720. EXPORT_SYMBOL(mem_cgroup_update_page_stat);
  1721. /*
  1722. * size of first charge trial. "32" comes from vmscan.c's magic value.
  1723. * TODO: maybe necessary to use big numbers in big irons.
  1724. */
  1725. #define CHARGE_BATCH 32U
  1726. struct memcg_stock_pcp {
  1727. struct mem_cgroup *cached; /* this never be root cgroup */
  1728. unsigned int nr_pages;
  1729. struct work_struct work;
  1730. unsigned long flags;
  1731. #define FLUSHING_CACHED_CHARGE (0)
  1732. };
  1733. static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock);
  1734. static DEFINE_MUTEX(percpu_charge_mutex);
  1735. /*
  1736. * Try to consume stocked charge on this cpu. If success, one page is consumed
  1737. * from local stock and true is returned. If the stock is 0 or charges from a
  1738. * cgroup which is not current target, returns false. This stock will be
  1739. * refilled.
  1740. */
  1741. static bool consume_stock(struct mem_cgroup *mem)
  1742. {
  1743. struct memcg_stock_pcp *stock;
  1744. bool ret = true;
  1745. stock = &get_cpu_var(memcg_stock);
  1746. if (mem == stock->cached && stock->nr_pages)
  1747. stock->nr_pages--;
  1748. else /* need to call res_counter_charge */
  1749. ret = false;
  1750. put_cpu_var(memcg_stock);
  1751. return ret;
  1752. }
  1753. /*
  1754. * Returns stocks cached in percpu to res_counter and reset cached information.
  1755. */
  1756. static void drain_stock(struct memcg_stock_pcp *stock)
  1757. {
  1758. struct mem_cgroup *old = stock->cached;
  1759. if (stock->nr_pages) {
  1760. unsigned long bytes = stock->nr_pages * PAGE_SIZE;
  1761. res_counter_uncharge(&old->res, bytes);
  1762. if (do_swap_account)
  1763. res_counter_uncharge(&old->memsw, bytes);
  1764. stock->nr_pages = 0;
  1765. }
  1766. stock->cached = NULL;
  1767. }
  1768. /*
  1769. * This must be called under preempt disabled or must be called by
  1770. * a thread which is pinned to local cpu.
  1771. */
  1772. static void drain_local_stock(struct work_struct *dummy)
  1773. {
  1774. struct memcg_stock_pcp *stock = &__get_cpu_var(memcg_stock);
  1775. drain_stock(stock);
  1776. clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags);
  1777. }
  1778. /*
  1779. * Cache charges(val) which is from res_counter, to local per_cpu area.
  1780. * This will be consumed by consume_stock() function, later.
  1781. */
  1782. static void refill_stock(struct mem_cgroup *mem, unsigned int nr_pages)
  1783. {
  1784. struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
  1785. if (stock->cached != mem) { /* reset if necessary */
  1786. drain_stock(stock);
  1787. stock->cached = mem;
  1788. }
  1789. stock->nr_pages += nr_pages;
  1790. put_cpu_var(memcg_stock);
  1791. }
  1792. /*
  1793. * Drains all per-CPU charge caches for given root_mem resp. subtree
  1794. * of the hierarchy under it. sync flag says whether we should block
  1795. * until the work is done.
  1796. */
  1797. static void drain_all_stock(struct mem_cgroup *root_mem, bool sync)
  1798. {
  1799. int cpu, curcpu;
  1800. /* Notify other cpus that system-wide "drain" is running */
  1801. get_online_cpus();
  1802. curcpu = get_cpu();
  1803. for_each_online_cpu(cpu) {
  1804. struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
  1805. struct mem_cgroup *mem;
  1806. mem = stock->cached;
  1807. if (!mem || !stock->nr_pages)
  1808. continue;
  1809. if (!mem_cgroup_same_or_subtree(root_mem, mem))
  1810. continue;
  1811. if (!test_and_set_bit(FLUSHING_CACHED_CHARGE, &stock->flags)) {
  1812. if (cpu == curcpu)
  1813. drain_local_stock(&stock->work);
  1814. else
  1815. schedule_work_on(cpu, &stock->work);
  1816. }
  1817. }
  1818. put_cpu();
  1819. if (!sync)
  1820. goto out;
  1821. for_each_online_cpu(cpu) {
  1822. struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
  1823. if (test_bit(FLUSHING_CACHED_CHARGE, &stock->flags))
  1824. flush_work(&stock->work);
  1825. }
  1826. out:
  1827. put_online_cpus();
  1828. }
  1829. /*
  1830. * Tries to drain stocked charges in other cpus. This function is asynchronous
  1831. * and just put a work per cpu for draining localy on each cpu. Caller can
  1832. * expects some charges will be back to res_counter later but cannot wait for
  1833. * it.
  1834. */
  1835. static void drain_all_stock_async(struct mem_cgroup *root_mem)
  1836. {
  1837. /*
  1838. * If someone calls draining, avoid adding more kworker runs.
  1839. */
  1840. if (!mutex_trylock(&percpu_charge_mutex))
  1841. return;
  1842. drain_all_stock(root_mem, false);
  1843. mutex_unlock(&percpu_charge_mutex);
  1844. }
  1845. /* This is a synchronous drain interface. */
  1846. static void drain_all_stock_sync(struct mem_cgroup *root_mem)
  1847. {
  1848. /* called when force_empty is called */
  1849. mutex_lock(&percpu_charge_mutex);
  1850. drain_all_stock(root_mem, true);
  1851. mutex_unlock(&percpu_charge_mutex);
  1852. }
  1853. /*
  1854. * This function drains percpu counter value from DEAD cpu and
  1855. * move it to local cpu. Note that this function can be preempted.
  1856. */
  1857. static void mem_cgroup_drain_pcp_counter(struct mem_cgroup *mem, int cpu)
  1858. {
  1859. int i;
  1860. spin_lock(&mem->pcp_counter_lock);
  1861. for (i = 0; i < MEM_CGROUP_STAT_DATA; i++) {
  1862. long x = per_cpu(mem->stat->count[i], cpu);
  1863. per_cpu(mem->stat->count[i], cpu) = 0;
  1864. mem->nocpu_base.count[i] += x;
  1865. }
  1866. for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) {
  1867. unsigned long x = per_cpu(mem->stat->events[i], cpu);
  1868. per_cpu(mem->stat->events[i], cpu) = 0;
  1869. mem->nocpu_base.events[i] += x;
  1870. }
  1871. /* need to clear ON_MOVE value, works as a kind of lock. */
  1872. per_cpu(mem->stat->count[MEM_CGROUP_ON_MOVE], cpu) = 0;
  1873. spin_unlock(&mem->pcp_counter_lock);
  1874. }
  1875. static void synchronize_mem_cgroup_on_move(struct mem_cgroup *mem, int cpu)
  1876. {
  1877. int idx = MEM_CGROUP_ON_MOVE;
  1878. spin_lock(&mem->pcp_counter_lock);
  1879. per_cpu(mem->stat->count[idx], cpu) = mem->nocpu_base.count[idx];
  1880. spin_unlock(&mem->pcp_counter_lock);
  1881. }
  1882. static int __cpuinit memcg_cpu_hotplug_callback(struct notifier_block *nb,
  1883. unsigned long action,
  1884. void *hcpu)
  1885. {
  1886. int cpu = (unsigned long)hcpu;
  1887. struct memcg_stock_pcp *stock;
  1888. struct mem_cgroup *iter;
  1889. if ((action == CPU_ONLINE)) {
  1890. for_each_mem_cgroup_all(iter)
  1891. synchronize_mem_cgroup_on_move(iter, cpu);
  1892. return NOTIFY_OK;
  1893. }
  1894. if ((action != CPU_DEAD) || action != CPU_DEAD_FROZEN)
  1895. return NOTIFY_OK;
  1896. for_each_mem_cgroup_all(iter)
  1897. mem_cgroup_drain_pcp_counter(iter, cpu);
  1898. stock = &per_cpu(memcg_stock, cpu);
  1899. drain_stock(stock);
  1900. return NOTIFY_OK;
  1901. }
  1902. /* See __mem_cgroup_try_charge() for details */
  1903. enum {
  1904. CHARGE_OK, /* success */
  1905. CHARGE_RETRY, /* need to retry but retry is not bad */
  1906. CHARGE_NOMEM, /* we can't do more. return -ENOMEM */
  1907. CHARGE_WOULDBLOCK, /* GFP_WAIT wasn't set and no enough res. */
  1908. CHARGE_OOM_DIE, /* the current is killed because of OOM */
  1909. };
  1910. static int mem_cgroup_do_charge(struct mem_cgroup *mem, gfp_t gfp_mask,
  1911. unsigned int nr_pages, bool oom_check)
  1912. {
  1913. unsigned long csize = nr_pages * PAGE_SIZE;
  1914. struct mem_cgroup *mem_over_limit;
  1915. struct res_counter *fail_res;
  1916. unsigned long flags = 0;
  1917. int ret;
  1918. ret = res_counter_charge(&mem->res, csize, &fail_res);
  1919. if (likely(!ret)) {
  1920. if (!do_swap_account)
  1921. return CHARGE_OK;
  1922. ret = res_counter_charge(&mem->memsw, csize, &fail_res);
  1923. if (likely(!ret))
  1924. return CHARGE_OK;
  1925. res_counter_uncharge(&mem->res, csize);
  1926. mem_over_limit = mem_cgroup_from_res_counter(fail_res, memsw);
  1927. flags |= MEM_CGROUP_RECLAIM_NOSWAP;
  1928. } else
  1929. mem_over_limit = mem_cgroup_from_res_counter(fail_res, res);
  1930. /*
  1931. * nr_pages can be either a huge page (HPAGE_PMD_NR), a batch
  1932. * of regular pages (CHARGE_BATCH), or a single regular page (1).
  1933. *
  1934. * Never reclaim on behalf of optional batching, retry with a
  1935. * single page instead.
  1936. */
  1937. if (nr_pages == CHARGE_BATCH)
  1938. return CHARGE_RETRY;
  1939. if (!(gfp_mask & __GFP_WAIT))
  1940. return CHARGE_WOULDBLOCK;
  1941. ret = mem_cgroup_hierarchical_reclaim(mem_over_limit, NULL,
  1942. gfp_mask, flags, NULL);
  1943. if (mem_cgroup_margin(mem_over_limit) >= nr_pages)
  1944. return CHARGE_RETRY;
  1945. /*
  1946. * Even though the limit is exceeded at this point, reclaim
  1947. * may have been able to free some pages. Retry the charge
  1948. * before killing the task.
  1949. *
  1950. * Only for regular pages, though: huge pages are rather
  1951. * unlikely to succeed so close to the limit, and we fall back
  1952. * to regular pages anyway in case of failure.
  1953. */
  1954. if (nr_pages == 1 && ret)
  1955. return CHARGE_RETRY;
  1956. /*
  1957. * At task move, charge accounts can be doubly counted. So, it's
  1958. * better to wait until the end of task_move if something is going on.
  1959. */
  1960. if (mem_cgroup_wait_acct_move(mem_over_limit))
  1961. return CHARGE_RETRY;
  1962. /* If we don't need to call oom-killer at el, return immediately */
  1963. if (!oom_check)
  1964. return CHARGE_NOMEM;
  1965. /* check OOM */
  1966. if (!mem_cgroup_handle_oom(mem_over_limit, gfp_mask))
  1967. return CHARGE_OOM_DIE;
  1968. return CHARGE_RETRY;
  1969. }
  1970. /*
  1971. * Unlike exported interface, "oom" parameter is added. if oom==true,
  1972. * oom-killer can be invoked.
  1973. */
  1974. static int __mem_cgroup_try_charge(struct mm_struct *mm,
  1975. gfp_t gfp_mask,
  1976. unsigned int nr_pages,
  1977. struct mem_cgroup **memcg,
  1978. bool oom)
  1979. {
  1980. unsigned int batch = max(CHARGE_BATCH, nr_pages);
  1981. int nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
  1982. struct mem_cgroup *mem = NULL;
  1983. int ret;
  1984. /*
  1985. * Unlike gloval-vm's OOM-kill, we're not in memory shortage
  1986. * in system level. So, allow to go ahead dying process in addition to
  1987. * MEMDIE process.
  1988. */
  1989. if (unlikely(test_thread_flag(TIF_MEMDIE)
  1990. || fatal_signal_pending(current)))
  1991. goto bypass;
  1992. /*
  1993. * We always charge the cgroup the mm_struct belongs to.
  1994. * The mm_struct's mem_cgroup changes on task migration if the
  1995. * thread group leader migrates. It's possible that mm is not
  1996. * set, if so charge the init_mm (happens for pagecache usage).
  1997. */
  1998. if (!*memcg && !mm)
  1999. goto bypass;
  2000. again:
  2001. if (*memcg) { /* css should be a valid one */
  2002. mem = *memcg;
  2003. VM_BUG_ON(css_is_removed(&mem->css));
  2004. if (mem_cgroup_is_root(mem))
  2005. goto done;
  2006. if (nr_pages == 1 && consume_stock(mem))
  2007. goto done;
  2008. css_get(&mem->css);
  2009. } else {
  2010. struct task_struct *p;
  2011. rcu_read_lock();
  2012. p = rcu_dereference(mm->owner);
  2013. /*
  2014. * Because we don't have task_lock(), "p" can exit.
  2015. * In that case, "mem" can point to root or p can be NULL with
  2016. * race with swapoff. Then, we have small risk of mis-accouning.
  2017. * But such kind of mis-account by race always happens because
  2018. * we don't have cgroup_mutex(). It's overkill and we allo that
  2019. * small race, here.
  2020. * (*) swapoff at el will charge against mm-struct not against
  2021. * task-struct. So, mm->owner can be NULL.
  2022. */
  2023. mem = mem_cgroup_from_task(p);
  2024. if (!mem || mem_cgroup_is_root(mem)) {
  2025. rcu_read_unlock();
  2026. goto done;
  2027. }
  2028. if (nr_pages == 1 && consume_stock(mem)) {
  2029. /*
  2030. * It seems dagerous to access memcg without css_get().
  2031. * But considering how consume_stok works, it's not
  2032. * necessary. If consume_stock success, some charges
  2033. * from this memcg are cached on this cpu. So, we
  2034. * don't need to call css_get()/css_tryget() before
  2035. * calling consume_stock().
  2036. */
  2037. rcu_read_unlock();
  2038. goto done;
  2039. }
  2040. /* after here, we may be blocked. we need to get refcnt */
  2041. if (!css_tryget(&mem->css)) {
  2042. rcu_read_unlock();
  2043. goto again;
  2044. }
  2045. rcu_read_unlock();
  2046. }
  2047. do {
  2048. bool oom_check;
  2049. /* If killed, bypass charge */
  2050. if (fatal_signal_pending(current)) {
  2051. css_put(&mem->css);
  2052. goto bypass;
  2053. }
  2054. oom_check = false;
  2055. if (oom && !nr_oom_retries) {
  2056. oom_check = true;
  2057. nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
  2058. }
  2059. ret = mem_cgroup_do_charge(mem, gfp_mask, batch, oom_check);
  2060. switch (ret) {
  2061. case CHARGE_OK:
  2062. break;
  2063. case CHARGE_RETRY: /* not in OOM situation but retry */
  2064. batch = nr_pages;
  2065. css_put(&mem->css);
  2066. mem = NULL;
  2067. goto again;
  2068. case CHARGE_WOULDBLOCK: /* !__GFP_WAIT */
  2069. css_put(&mem->css);
  2070. goto nomem;
  2071. case CHARGE_NOMEM: /* OOM routine works */
  2072. if (!oom) {
  2073. css_put(&mem->css);
  2074. goto nomem;
  2075. }
  2076. /* If oom, we never return -ENOMEM */
  2077. nr_oom_retries--;
  2078. break;
  2079. case CHARGE_OOM_DIE: /* Killed by OOM Killer */
  2080. css_put(&mem->css);
  2081. goto bypass;
  2082. }
  2083. } while (ret != CHARGE_OK);
  2084. if (batch > nr_pages)
  2085. refill_stock(mem, batch - nr_pages);
  2086. css_put(&mem->css);
  2087. done:
  2088. *memcg = mem;
  2089. return 0;
  2090. nomem:
  2091. *memcg = NULL;
  2092. return -ENOMEM;
  2093. bypass:
  2094. *memcg = NULL;
  2095. return 0;
  2096. }
  2097. /*
  2098. * Somemtimes we have to undo a charge we got by try_charge().
  2099. * This function is for that and do uncharge, put css's refcnt.
  2100. * gotten by try_charge().
  2101. */
  2102. static void __mem_cgroup_cancel_charge(struct mem_cgroup *mem,
  2103. unsigned int nr_pages)
  2104. {
  2105. if (!mem_cgroup_is_root(mem)) {
  2106. unsigned long bytes = nr_pages * PAGE_SIZE;
  2107. res_counter_uncharge(&mem->res, bytes);
  2108. if (do_swap_account)
  2109. res_counter_uncharge(&mem->memsw, bytes);
  2110. }
  2111. }
  2112. /*
  2113. * A helper function to get mem_cgroup from ID. must be called under
  2114. * rcu_read_lock(). The caller must check css_is_removed() or some if
  2115. * it's concern. (dropping refcnt from swap can be called against removed
  2116. * memcg.)
  2117. */
  2118. static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
  2119. {
  2120. struct cgroup_subsys_state *css;
  2121. /* ID 0 is unused ID */
  2122. if (!id)
  2123. return NULL;
  2124. css = css_lookup(&mem_cgroup_subsys, id);
  2125. if (!css)
  2126. return NULL;
  2127. return container_of(css, struct mem_cgroup, css);
  2128. }
  2129. struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
  2130. {
  2131. struct mem_cgroup *mem = NULL;
  2132. struct page_cgroup *pc;
  2133. unsigned short id;
  2134. swp_entry_t ent;
  2135. VM_BUG_ON(!PageLocked(page));
  2136. pc = lookup_page_cgroup(page);
  2137. lock_page_cgroup(pc);
  2138. if (PageCgroupUsed(pc)) {
  2139. mem = pc->mem_cgroup;
  2140. if (mem && !css_tryget(&mem->css))
  2141. mem = NULL;
  2142. } else if (PageSwapCache(page)) {
  2143. ent.val = page_private(page);
  2144. id = lookup_swap_cgroup(ent);
  2145. rcu_read_lock();
  2146. mem = mem_cgroup_lookup(id);
  2147. if (mem && !css_tryget(&mem->css))
  2148. mem = NULL;
  2149. rcu_read_unlock();
  2150. }
  2151. unlock_page_cgroup(pc);
  2152. return mem;
  2153. }
  2154. static void __mem_cgroup_commit_charge(struct mem_cgroup *mem,
  2155. struct page *page,
  2156. unsigned int nr_pages,
  2157. struct page_cgroup *pc,
  2158. enum charge_type ctype)
  2159. {
  2160. lock_page_cgroup(pc);
  2161. if (unlikely(PageCgroupUsed(pc))) {
  2162. unlock_page_cgroup(pc);
  2163. __mem_cgroup_cancel_charge(mem, nr_pages);
  2164. return;
  2165. }
  2166. /*
  2167. * we don't need page_cgroup_lock about tail pages, becase they are not
  2168. * accessed by any other context at this point.
  2169. */
  2170. pc->mem_cgroup = mem;
  2171. /*
  2172. * We access a page_cgroup asynchronously without lock_page_cgroup().
  2173. * Especially when a page_cgroup is taken from a page, pc->mem_cgroup
  2174. * is accessed after testing USED bit. To make pc->mem_cgroup visible
  2175. * before USED bit, we need memory barrier here.
  2176. * See mem_cgroup_add_lru_list(), etc.
  2177. */
  2178. smp_wmb();
  2179. switch (ctype) {
  2180. case MEM_CGROUP_CHARGE_TYPE_CACHE:
  2181. case MEM_CGROUP_CHARGE_TYPE_SHMEM:
  2182. SetPageCgroupCache(pc);
  2183. SetPageCgroupUsed(pc);
  2184. break;
  2185. case MEM_CGROUP_CHARGE_TYPE_MAPPED:
  2186. ClearPageCgroupCache(pc);
  2187. SetPageCgroupUsed(pc);
  2188. break;
  2189. default:
  2190. break;
  2191. }
  2192. mem_cgroup_charge_statistics(mem, PageCgroupCache(pc), nr_pages);
  2193. unlock_page_cgroup(pc);
  2194. /*
  2195. * "charge_statistics" updated event counter. Then, check it.
  2196. * Insert ancestor (and ancestor's ancestors), to softlimit RB-tree.
  2197. * if they exceeds softlimit.
  2198. */
  2199. memcg_check_events(mem, page);
  2200. }
  2201. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  2202. #define PCGF_NOCOPY_AT_SPLIT ((1 << PCG_LOCK) | (1 << PCG_MOVE_LOCK) |\
  2203. (1 << PCG_ACCT_LRU) | (1 << PCG_MIGRATION))
  2204. /*
  2205. * Because tail pages are not marked as "used", set it. We're under
  2206. * zone->lru_lock, 'splitting on pmd' and compund_lock.
  2207. */
  2208. void mem_cgroup_split_huge_fixup(struct page *head, struct page *tail)
  2209. {
  2210. struct page_cgroup *head_pc = lookup_page_cgroup(head);
  2211. struct page_cgroup *tail_pc = lookup_page_cgroup(tail);
  2212. unsigned long flags;
  2213. if (mem_cgroup_disabled())
  2214. return;
  2215. /*
  2216. * We have no races with charge/uncharge but will have races with
  2217. * page state accounting.
  2218. */
  2219. move_lock_page_cgroup(head_pc, &flags);
  2220. tail_pc->mem_cgroup = head_pc->mem_cgroup;
  2221. smp_wmb(); /* see __commit_charge() */
  2222. if (PageCgroupAcctLRU(head_pc)) {
  2223. enum lru_list lru;
  2224. struct mem_cgroup_per_zone *mz;
  2225. /*
  2226. * LRU flags cannot be copied because we need to add tail
  2227. *.page to LRU by generic call and our hook will be called.
  2228. * We hold lru_lock, then, reduce counter directly.
  2229. */
  2230. lru = page_lru(head);
  2231. mz = page_cgroup_zoneinfo(head_pc->mem_cgroup, head);
  2232. MEM_CGROUP_ZSTAT(mz, lru) -= 1;
  2233. }
  2234. tail_pc->flags = head_pc->flags & ~PCGF_NOCOPY_AT_SPLIT;
  2235. move_unlock_page_cgroup(head_pc, &flags);
  2236. }
  2237. #endif
  2238. /**
  2239. * mem_cgroup_move_account - move account of the page
  2240. * @page: the page
  2241. * @nr_pages: number of regular pages (>1 for huge pages)
  2242. * @pc: page_cgroup of the page.
  2243. * @from: mem_cgroup which the page is moved from.
  2244. * @to: mem_cgroup which the page is moved to. @from != @to.
  2245. * @uncharge: whether we should call uncharge and css_put against @from.
  2246. *
  2247. * The caller must confirm following.
  2248. * - page is not on LRU (isolate_page() is useful.)
  2249. * - compound_lock is held when nr_pages > 1
  2250. *
  2251. * This function doesn't do "charge" nor css_get to new cgroup. It should be
  2252. * done by a caller(__mem_cgroup_try_charge would be useful). If @uncharge is
  2253. * true, this function does "uncharge" from old cgroup, but it doesn't if
  2254. * @uncharge is false, so a caller should do "uncharge".
  2255. */
  2256. static int mem_cgroup_move_account(struct page *page,
  2257. unsigned int nr_pages,
  2258. struct page_cgroup *pc,
  2259. struct mem_cgroup *from,
  2260. struct mem_cgroup *to,
  2261. bool uncharge)
  2262. {
  2263. unsigned long flags;
  2264. int ret;
  2265. VM_BUG_ON(from == to);
  2266. VM_BUG_ON(PageLRU(page));
  2267. /*
  2268. * The page is isolated from LRU. So, collapse function
  2269. * will not handle this page. But page splitting can happen.
  2270. * Do this check under compound_page_lock(). The caller should
  2271. * hold it.
  2272. */
  2273. ret = -EBUSY;
  2274. if (nr_pages > 1 && !PageTransHuge(page))
  2275. goto out;
  2276. lock_page_cgroup(pc);
  2277. ret = -EINVAL;
  2278. if (!PageCgroupUsed(pc) || pc->mem_cgroup != from)
  2279. goto unlock;
  2280. move_lock_page_cgroup(pc, &flags);
  2281. if (PageCgroupFileMapped(pc)) {
  2282. /* Update mapped_file data for mem_cgroup */
  2283. preempt_disable();
  2284. __this_cpu_dec(from->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
  2285. __this_cpu_inc(to->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
  2286. preempt_enable();
  2287. }
  2288. mem_cgroup_charge_statistics(from, PageCgroupCache(pc), -nr_pages);
  2289. if (uncharge)
  2290. /* This is not "cancel", but cancel_charge does all we need. */
  2291. __mem_cgroup_cancel_charge(from, nr_pages);
  2292. /* caller should have done css_get */
  2293. pc->mem_cgroup = to;
  2294. mem_cgroup_charge_statistics(to, PageCgroupCache(pc), nr_pages);
  2295. /*
  2296. * We charges against "to" which may not have any tasks. Then, "to"
  2297. * can be under rmdir(). But in current implementation, caller of
  2298. * this function is just force_empty() and move charge, so it's
  2299. * guaranteed that "to" is never removed. So, we don't check rmdir
  2300. * status here.
  2301. */
  2302. move_unlock_page_cgroup(pc, &flags);
  2303. ret = 0;
  2304. unlock:
  2305. unlock_page_cgroup(pc);
  2306. /*
  2307. * check events
  2308. */
  2309. memcg_check_events(to, page);
  2310. memcg_check_events(from, page);
  2311. out:
  2312. return ret;
  2313. }
  2314. /*
  2315. * move charges to its parent.
  2316. */
  2317. static int mem_cgroup_move_parent(struct page *page,
  2318. struct page_cgroup *pc,
  2319. struct mem_cgroup *child,
  2320. gfp_t gfp_mask)
  2321. {
  2322. struct cgroup *cg = child->css.cgroup;
  2323. struct cgroup *pcg = cg->parent;
  2324. struct mem_cgroup *parent;
  2325. unsigned int nr_pages;
  2326. unsigned long uninitialized_var(flags);
  2327. int ret;
  2328. /* Is ROOT ? */
  2329. if (!pcg)
  2330. return -EINVAL;
  2331. ret = -EBUSY;
  2332. if (!get_page_unless_zero(page))
  2333. goto out;
  2334. if (isolate_lru_page(page))
  2335. goto put;
  2336. nr_pages = hpage_nr_pages(page);
  2337. parent = mem_cgroup_from_cont(pcg);
  2338. ret = __mem_cgroup_try_charge(NULL, gfp_mask, nr_pages, &parent, false);
  2339. if (ret || !parent)
  2340. goto put_back;
  2341. if (nr_pages > 1)
  2342. flags = compound_lock_irqsave(page);
  2343. ret = mem_cgroup_move_account(page, nr_pages, pc, child, parent, true);
  2344. if (ret)
  2345. __mem_cgroup_cancel_charge(parent, nr_pages);
  2346. if (nr_pages > 1)
  2347. compound_unlock_irqrestore(page, flags);
  2348. put_back:
  2349. putback_lru_page(page);
  2350. put:
  2351. put_page(page);
  2352. out:
  2353. return ret;
  2354. }
  2355. /*
  2356. * Charge the memory controller for page usage.
  2357. * Return
  2358. * 0 if the charge was successful
  2359. * < 0 if the cgroup is over its limit
  2360. */
  2361. static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
  2362. gfp_t gfp_mask, enum charge_type ctype)
  2363. {
  2364. struct mem_cgroup *mem = NULL;
  2365. unsigned int nr_pages = 1;
  2366. struct page_cgroup *pc;
  2367. bool oom = true;
  2368. int ret;
  2369. if (PageTransHuge(page)) {
  2370. nr_pages <<= compound_order(page);
  2371. VM_BUG_ON(!PageTransHuge(page));
  2372. /*
  2373. * Never OOM-kill a process for a huge page. The
  2374. * fault handler will fall back to regular pages.
  2375. */
  2376. oom = false;
  2377. }
  2378. pc = lookup_page_cgroup(page);
  2379. BUG_ON(!pc); /* XXX: remove this and move pc lookup into commit */
  2380. ret = __mem_cgroup_try_charge(mm, gfp_mask, nr_pages, &mem, oom);
  2381. if (ret || !mem)
  2382. return ret;
  2383. __mem_cgroup_commit_charge(mem, page, nr_pages, pc, ctype);
  2384. return 0;
  2385. }
  2386. int mem_cgroup_newpage_charge(struct page *page,
  2387. struct mm_struct *mm, gfp_t gfp_mask)
  2388. {
  2389. if (mem_cgroup_disabled())
  2390. return 0;
  2391. /*
  2392. * If already mapped, we don't have to account.
  2393. * If page cache, page->mapping has address_space.
  2394. * But page->mapping may have out-of-use anon_vma pointer,
  2395. * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
  2396. * is NULL.
  2397. */
  2398. if (page_mapped(page) || (page->mapping && !PageAnon(page)))
  2399. return 0;
  2400. if (unlikely(!mm))
  2401. mm = &init_mm;
  2402. return mem_cgroup_charge_common(page, mm, gfp_mask,
  2403. MEM_CGROUP_CHARGE_TYPE_MAPPED);
  2404. }
  2405. static void
  2406. __mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
  2407. enum charge_type ctype);
  2408. static void
  2409. __mem_cgroup_commit_charge_lrucare(struct page *page, struct mem_cgroup *mem,
  2410. enum charge_type ctype)
  2411. {
  2412. struct page_cgroup *pc = lookup_page_cgroup(page);
  2413. /*
  2414. * In some case, SwapCache, FUSE(splice_buf->radixtree), the page
  2415. * is already on LRU. It means the page may on some other page_cgroup's
  2416. * LRU. Take care of it.
  2417. */
  2418. mem_cgroup_lru_del_before_commit(page);
  2419. __mem_cgroup_commit_charge(mem, page, 1, pc, ctype);
  2420. mem_cgroup_lru_add_after_commit(page);
  2421. return;
  2422. }
  2423. int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
  2424. gfp_t gfp_mask)
  2425. {
  2426. struct mem_cgroup *mem = NULL;
  2427. int ret;
  2428. if (mem_cgroup_disabled())
  2429. return 0;
  2430. if (PageCompound(page))
  2431. return 0;
  2432. if (unlikely(!mm))
  2433. mm = &init_mm;
  2434. if (page_is_file_cache(page)) {
  2435. ret = __mem_cgroup_try_charge(mm, gfp_mask, 1, &mem, true);
  2436. if (ret || !mem)
  2437. return ret;
  2438. /*
  2439. * FUSE reuses pages without going through the final
  2440. * put that would remove them from the LRU list, make
  2441. * sure that they get relinked properly.
  2442. */
  2443. __mem_cgroup_commit_charge_lrucare(page, mem,
  2444. MEM_CGROUP_CHARGE_TYPE_CACHE);
  2445. return ret;
  2446. }
  2447. /* shmem */
  2448. if (PageSwapCache(page)) {
  2449. ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
  2450. if (!ret)
  2451. __mem_cgroup_commit_charge_swapin(page, mem,
  2452. MEM_CGROUP_CHARGE_TYPE_SHMEM);
  2453. } else
  2454. ret = mem_cgroup_charge_common(page, mm, gfp_mask,
  2455. MEM_CGROUP_CHARGE_TYPE_SHMEM);
  2456. return ret;
  2457. }
  2458. /*
  2459. * While swap-in, try_charge -> commit or cancel, the page is locked.
  2460. * And when try_charge() successfully returns, one refcnt to memcg without
  2461. * struct page_cgroup is acquired. This refcnt will be consumed by
  2462. * "commit()" or removed by "cancel()"
  2463. */
  2464. int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
  2465. struct page *page,
  2466. gfp_t mask, struct mem_cgroup **ptr)
  2467. {
  2468. struct mem_cgroup *mem;
  2469. int ret;
  2470. *ptr = NULL;
  2471. if (mem_cgroup_disabled())
  2472. return 0;
  2473. if (!do_swap_account)
  2474. goto charge_cur_mm;
  2475. /*
  2476. * A racing thread's fault, or swapoff, may have already updated
  2477. * the pte, and even removed page from swap cache: in those cases
  2478. * do_swap_page()'s pte_same() test will fail; but there's also a
  2479. * KSM case which does need to charge the page.
  2480. */
  2481. if (!PageSwapCache(page))
  2482. goto charge_cur_mm;
  2483. mem = try_get_mem_cgroup_from_page(page);
  2484. if (!mem)
  2485. goto charge_cur_mm;
  2486. *ptr = mem;
  2487. ret = __mem_cgroup_try_charge(NULL, mask, 1, ptr, true);
  2488. css_put(&mem->css);
  2489. return ret;
  2490. charge_cur_mm:
  2491. if (unlikely(!mm))
  2492. mm = &init_mm;
  2493. return __mem_cgroup_try_charge(mm, mask, 1, ptr, true);
  2494. }
  2495. static void
  2496. __mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
  2497. enum charge_type ctype)
  2498. {
  2499. if (mem_cgroup_disabled())
  2500. return;
  2501. if (!ptr)
  2502. return;
  2503. cgroup_exclude_rmdir(&ptr->css);
  2504. __mem_cgroup_commit_charge_lrucare(page, ptr, ctype);
  2505. /*
  2506. * Now swap is on-memory. This means this page may be
  2507. * counted both as mem and swap....double count.
  2508. * Fix it by uncharging from memsw. Basically, this SwapCache is stable
  2509. * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
  2510. * may call delete_from_swap_cache() before reach here.
  2511. */
  2512. if (do_swap_account && PageSwapCache(page)) {
  2513. swp_entry_t ent = {.val = page_private(page)};
  2514. unsigned short id;
  2515. struct mem_cgroup *memcg;
  2516. id = swap_cgroup_record(ent, 0);
  2517. rcu_read_lock();
  2518. memcg = mem_cgroup_lookup(id);
  2519. if (memcg) {
  2520. /*
  2521. * This recorded memcg can be obsolete one. So, avoid
  2522. * calling css_tryget
  2523. */
  2524. if (!mem_cgroup_is_root(memcg))
  2525. res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
  2526. mem_cgroup_swap_statistics(memcg, false);
  2527. mem_cgroup_put(memcg);
  2528. }
  2529. rcu_read_unlock();
  2530. }
  2531. /*
  2532. * At swapin, we may charge account against cgroup which has no tasks.
  2533. * So, rmdir()->pre_destroy() can be called while we do this charge.
  2534. * In that case, we need to call pre_destroy() again. check it here.
  2535. */
  2536. cgroup_release_and_wakeup_rmdir(&ptr->css);
  2537. }
  2538. void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr)
  2539. {
  2540. __mem_cgroup_commit_charge_swapin(page, ptr,
  2541. MEM_CGROUP_CHARGE_TYPE_MAPPED);
  2542. }
  2543. void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem)
  2544. {
  2545. if (mem_cgroup_disabled())
  2546. return;
  2547. if (!mem)
  2548. return;
  2549. __mem_cgroup_cancel_charge(mem, 1);
  2550. }
  2551. static void mem_cgroup_do_uncharge(struct mem_cgroup *mem,
  2552. unsigned int nr_pages,
  2553. const enum charge_type ctype)
  2554. {
  2555. struct memcg_batch_info *batch = NULL;
  2556. bool uncharge_memsw = true;
  2557. /* If swapout, usage of swap doesn't decrease */
  2558. if (!do_swap_account || ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
  2559. uncharge_memsw = false;
  2560. batch = &current->memcg_batch;
  2561. /*
  2562. * In usual, we do css_get() when we remember memcg pointer.
  2563. * But in this case, we keep res->usage until end of a series of
  2564. * uncharges. Then, it's ok to ignore memcg's refcnt.
  2565. */
  2566. if (!batch->memcg)
  2567. batch->memcg = mem;
  2568. /*
  2569. * do_batch > 0 when unmapping pages or inode invalidate/truncate.
  2570. * In those cases, all pages freed continuously can be expected to be in
  2571. * the same cgroup and we have chance to coalesce uncharges.
  2572. * But we do uncharge one by one if this is killed by OOM(TIF_MEMDIE)
  2573. * because we want to do uncharge as soon as possible.
  2574. */
  2575. if (!batch->do_batch || test_thread_flag(TIF_MEMDIE))
  2576. goto direct_uncharge;
  2577. if (nr_pages > 1)
  2578. goto direct_uncharge;
  2579. /*
  2580. * In typical case, batch->memcg == mem. This means we can
  2581. * merge a series of uncharges to an uncharge of res_counter.
  2582. * If not, we uncharge res_counter ony by one.
  2583. */
  2584. if (batch->memcg != mem)
  2585. goto direct_uncharge;
  2586. /* remember freed charge and uncharge it later */
  2587. batch->nr_pages++;
  2588. if (uncharge_memsw)
  2589. batch->memsw_nr_pages++;
  2590. return;
  2591. direct_uncharge:
  2592. res_counter_uncharge(&mem->res, nr_pages * PAGE_SIZE);
  2593. if (uncharge_memsw)
  2594. res_counter_uncharge(&mem->memsw, nr_pages * PAGE_SIZE);
  2595. if (unlikely(batch->memcg != mem))
  2596. memcg_oom_recover(mem);
  2597. return;
  2598. }
  2599. /*
  2600. * uncharge if !page_mapped(page)
  2601. */
  2602. static struct mem_cgroup *
  2603. __mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
  2604. {
  2605. struct mem_cgroup *mem = NULL;
  2606. unsigned int nr_pages = 1;
  2607. struct page_cgroup *pc;
  2608. if (mem_cgroup_disabled())
  2609. return NULL;
  2610. if (PageSwapCache(page))
  2611. return NULL;
  2612. if (PageTransHuge(page)) {
  2613. nr_pages <<= compound_order(page);
  2614. VM_BUG_ON(!PageTransHuge(page));
  2615. }
  2616. /*
  2617. * Check if our page_cgroup is valid
  2618. */
  2619. pc = lookup_page_cgroup(page);
  2620. if (unlikely(!pc || !PageCgroupUsed(pc)))
  2621. return NULL;
  2622. lock_page_cgroup(pc);
  2623. mem = pc->mem_cgroup;
  2624. if (!PageCgroupUsed(pc))
  2625. goto unlock_out;
  2626. switch (ctype) {
  2627. case MEM_CGROUP_CHARGE_TYPE_MAPPED:
  2628. case MEM_CGROUP_CHARGE_TYPE_DROP:
  2629. /* See mem_cgroup_prepare_migration() */
  2630. if (page_mapped(page) || PageCgroupMigration(pc))
  2631. goto unlock_out;
  2632. break;
  2633. case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
  2634. if (!PageAnon(page)) { /* Shared memory */
  2635. if (page->mapping && !page_is_file_cache(page))
  2636. goto unlock_out;
  2637. } else if (page_mapped(page)) /* Anon */
  2638. goto unlock_out;
  2639. break;
  2640. default:
  2641. break;
  2642. }
  2643. mem_cgroup_charge_statistics(mem, PageCgroupCache(pc), -nr_pages);
  2644. ClearPageCgroupUsed(pc);
  2645. /*
  2646. * pc->mem_cgroup is not cleared here. It will be accessed when it's
  2647. * freed from LRU. This is safe because uncharged page is expected not
  2648. * to be reused (freed soon). Exception is SwapCache, it's handled by
  2649. * special functions.
  2650. */
  2651. unlock_page_cgroup(pc);
  2652. /*
  2653. * even after unlock, we have mem->res.usage here and this memcg
  2654. * will never be freed.
  2655. */
  2656. memcg_check_events(mem, page);
  2657. if (do_swap_account && ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT) {
  2658. mem_cgroup_swap_statistics(mem, true);
  2659. mem_cgroup_get(mem);
  2660. }
  2661. if (!mem_cgroup_is_root(mem))
  2662. mem_cgroup_do_uncharge(mem, nr_pages, ctype);
  2663. return mem;
  2664. unlock_out:
  2665. unlock_page_cgroup(pc);
  2666. return NULL;
  2667. }
  2668. void mem_cgroup_uncharge_page(struct page *page)
  2669. {
  2670. /* early check. */
  2671. if (page_mapped(page))
  2672. return;
  2673. if (page->mapping && !PageAnon(page))
  2674. return;
  2675. __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
  2676. }
  2677. void mem_cgroup_uncharge_cache_page(struct page *page)
  2678. {
  2679. VM_BUG_ON(page_mapped(page));
  2680. VM_BUG_ON(page->mapping);
  2681. __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
  2682. }
  2683. /*
  2684. * Batch_start/batch_end is called in unmap_page_range/invlidate/trucate.
  2685. * In that cases, pages are freed continuously and we can expect pages
  2686. * are in the same memcg. All these calls itself limits the number of
  2687. * pages freed at once, then uncharge_start/end() is called properly.
  2688. * This may be called prural(2) times in a context,
  2689. */
  2690. void mem_cgroup_uncharge_start(void)
  2691. {
  2692. current->memcg_batch.do_batch++;
  2693. /* We can do nest. */
  2694. if (current->memcg_batch.do_batch == 1) {
  2695. current->memcg_batch.memcg = NULL;
  2696. current->memcg_batch.nr_pages = 0;
  2697. current->memcg_batch.memsw_nr_pages = 0;
  2698. }
  2699. }
  2700. void mem_cgroup_uncharge_end(void)
  2701. {
  2702. struct memcg_batch_info *batch = &current->memcg_batch;
  2703. if (!batch->do_batch)
  2704. return;
  2705. batch->do_batch--;
  2706. if (batch->do_batch) /* If stacked, do nothing. */
  2707. return;
  2708. if (!batch->memcg)
  2709. return;
  2710. /*
  2711. * This "batch->memcg" is valid without any css_get/put etc...
  2712. * bacause we hide charges behind us.
  2713. */
  2714. if (batch->nr_pages)
  2715. res_counter_uncharge(&batch->memcg->res,
  2716. batch->nr_pages * PAGE_SIZE);
  2717. if (batch->memsw_nr_pages)
  2718. res_counter_uncharge(&batch->memcg->memsw,
  2719. batch->memsw_nr_pages * PAGE_SIZE);
  2720. memcg_oom_recover(batch->memcg);
  2721. /* forget this pointer (for sanity check) */
  2722. batch->memcg = NULL;
  2723. }
  2724. #ifdef CONFIG_SWAP
  2725. /*
  2726. * called after __delete_from_swap_cache() and drop "page" account.
  2727. * memcg information is recorded to swap_cgroup of "ent"
  2728. */
  2729. void
  2730. mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
  2731. {
  2732. struct mem_cgroup *memcg;
  2733. int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT;
  2734. if (!swapout) /* this was a swap cache but the swap is unused ! */
  2735. ctype = MEM_CGROUP_CHARGE_TYPE_DROP;
  2736. memcg = __mem_cgroup_uncharge_common(page, ctype);
  2737. /*
  2738. * record memcg information, if swapout && memcg != NULL,
  2739. * mem_cgroup_get() was called in uncharge().
  2740. */
  2741. if (do_swap_account && swapout && memcg)
  2742. swap_cgroup_record(ent, css_id(&memcg->css));
  2743. }
  2744. #endif
  2745. #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
  2746. /*
  2747. * called from swap_entry_free(). remove record in swap_cgroup and
  2748. * uncharge "memsw" account.
  2749. */
  2750. void mem_cgroup_uncharge_swap(swp_entry_t ent)
  2751. {
  2752. struct mem_cgroup *memcg;
  2753. unsigned short id;
  2754. if (!do_swap_account)
  2755. return;
  2756. id = swap_cgroup_record(ent, 0);
  2757. rcu_read_lock();
  2758. memcg = mem_cgroup_lookup(id);
  2759. if (memcg) {
  2760. /*
  2761. * We uncharge this because swap is freed.
  2762. * This memcg can be obsolete one. We avoid calling css_tryget
  2763. */
  2764. if (!mem_cgroup_is_root(memcg))
  2765. res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
  2766. mem_cgroup_swap_statistics(memcg, false);
  2767. mem_cgroup_put(memcg);
  2768. }
  2769. rcu_read_unlock();
  2770. }
  2771. /**
  2772. * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record.
  2773. * @entry: swap entry to be moved
  2774. * @from: mem_cgroup which the entry is moved from
  2775. * @to: mem_cgroup which the entry is moved to
  2776. * @need_fixup: whether we should fixup res_counters and refcounts.
  2777. *
  2778. * It succeeds only when the swap_cgroup's record for this entry is the same
  2779. * as the mem_cgroup's id of @from.
  2780. *
  2781. * Returns 0 on success, -EINVAL on failure.
  2782. *
  2783. * The caller must have charged to @to, IOW, called res_counter_charge() about
  2784. * both res and memsw, and called css_get().
  2785. */
  2786. static int mem_cgroup_move_swap_account(swp_entry_t entry,
  2787. struct mem_cgroup *from, struct mem_cgroup *to, bool need_fixup)
  2788. {
  2789. unsigned short old_id, new_id;
  2790. old_id = css_id(&from->css);
  2791. new_id = css_id(&to->css);
  2792. if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) {
  2793. mem_cgroup_swap_statistics(from, false);
  2794. mem_cgroup_swap_statistics(to, true);
  2795. /*
  2796. * This function is only called from task migration context now.
  2797. * It postpones res_counter and refcount handling till the end
  2798. * of task migration(mem_cgroup_clear_mc()) for performance
  2799. * improvement. But we cannot postpone mem_cgroup_get(to)
  2800. * because if the process that has been moved to @to does
  2801. * swap-in, the refcount of @to might be decreased to 0.
  2802. */
  2803. mem_cgroup_get(to);
  2804. if (need_fixup) {
  2805. if (!mem_cgroup_is_root(from))
  2806. res_counter_uncharge(&from->memsw, PAGE_SIZE);
  2807. mem_cgroup_put(from);
  2808. /*
  2809. * we charged both to->res and to->memsw, so we should
  2810. * uncharge to->res.
  2811. */
  2812. if (!mem_cgroup_is_root(to))
  2813. res_counter_uncharge(&to->res, PAGE_SIZE);
  2814. }
  2815. return 0;
  2816. }
  2817. return -EINVAL;
  2818. }
  2819. #else
  2820. static inline int mem_cgroup_move_swap_account(swp_entry_t entry,
  2821. struct mem_cgroup *from, struct mem_cgroup *to, bool need_fixup)
  2822. {
  2823. return -EINVAL;
  2824. }
  2825. #endif
  2826. /*
  2827. * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
  2828. * page belongs to.
  2829. */
  2830. int mem_cgroup_prepare_migration(struct page *page,
  2831. struct page *newpage, struct mem_cgroup **ptr, gfp_t gfp_mask)
  2832. {
  2833. struct mem_cgroup *mem = NULL;
  2834. struct page_cgroup *pc;
  2835. enum charge_type ctype;
  2836. int ret = 0;
  2837. *ptr = NULL;
  2838. VM_BUG_ON(PageTransHuge(page));
  2839. if (mem_cgroup_disabled())
  2840. return 0;
  2841. pc = lookup_page_cgroup(page);
  2842. lock_page_cgroup(pc);
  2843. if (PageCgroupUsed(pc)) {
  2844. mem = pc->mem_cgroup;
  2845. css_get(&mem->css);
  2846. /*
  2847. * At migrating an anonymous page, its mapcount goes down
  2848. * to 0 and uncharge() will be called. But, even if it's fully
  2849. * unmapped, migration may fail and this page has to be
  2850. * charged again. We set MIGRATION flag here and delay uncharge
  2851. * until end_migration() is called
  2852. *
  2853. * Corner Case Thinking
  2854. * A)
  2855. * When the old page was mapped as Anon and it's unmap-and-freed
  2856. * while migration was ongoing.
  2857. * If unmap finds the old page, uncharge() of it will be delayed
  2858. * until end_migration(). If unmap finds a new page, it's
  2859. * uncharged when it make mapcount to be 1->0. If unmap code
  2860. * finds swap_migration_entry, the new page will not be mapped
  2861. * and end_migration() will find it(mapcount==0).
  2862. *
  2863. * B)
  2864. * When the old page was mapped but migraion fails, the kernel
  2865. * remaps it. A charge for it is kept by MIGRATION flag even
  2866. * if mapcount goes down to 0. We can do remap successfully
  2867. * without charging it again.
  2868. *
  2869. * C)
  2870. * The "old" page is under lock_page() until the end of
  2871. * migration, so, the old page itself will not be swapped-out.
  2872. * If the new page is swapped out before end_migraton, our
  2873. * hook to usual swap-out path will catch the event.
  2874. */
  2875. if (PageAnon(page))
  2876. SetPageCgroupMigration(pc);
  2877. }
  2878. unlock_page_cgroup(pc);
  2879. /*
  2880. * If the page is not charged at this point,
  2881. * we return here.
  2882. */
  2883. if (!mem)
  2884. return 0;
  2885. *ptr = mem;
  2886. ret = __mem_cgroup_try_charge(NULL, gfp_mask, 1, ptr, false);
  2887. css_put(&mem->css);/* drop extra refcnt */
  2888. if (ret || *ptr == NULL) {
  2889. if (PageAnon(page)) {
  2890. lock_page_cgroup(pc);
  2891. ClearPageCgroupMigration(pc);
  2892. unlock_page_cgroup(pc);
  2893. /*
  2894. * The old page may be fully unmapped while we kept it.
  2895. */
  2896. mem_cgroup_uncharge_page(page);
  2897. }
  2898. return -ENOMEM;
  2899. }
  2900. /*
  2901. * We charge new page before it's used/mapped. So, even if unlock_page()
  2902. * is called before end_migration, we can catch all events on this new
  2903. * page. In the case new page is migrated but not remapped, new page's
  2904. * mapcount will be finally 0 and we call uncharge in end_migration().
  2905. */
  2906. pc = lookup_page_cgroup(newpage);
  2907. if (PageAnon(page))
  2908. ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
  2909. else if (page_is_file_cache(page))
  2910. ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
  2911. else
  2912. ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
  2913. __mem_cgroup_commit_charge(mem, page, 1, pc, ctype);
  2914. return ret;
  2915. }
  2916. /* remove redundant charge if migration failed*/
  2917. void mem_cgroup_end_migration(struct mem_cgroup *mem,
  2918. struct page *oldpage, struct page *newpage, bool migration_ok)
  2919. {
  2920. struct page *used, *unused;
  2921. struct page_cgroup *pc;
  2922. if (!mem)
  2923. return;
  2924. /* blocks rmdir() */
  2925. cgroup_exclude_rmdir(&mem->css);
  2926. if (!migration_ok) {
  2927. used = oldpage;
  2928. unused = newpage;
  2929. } else {
  2930. used = newpage;
  2931. unused = oldpage;
  2932. }
  2933. /*
  2934. * We disallowed uncharge of pages under migration because mapcount
  2935. * of the page goes down to zero, temporarly.
  2936. * Clear the flag and check the page should be charged.
  2937. */
  2938. pc = lookup_page_cgroup(oldpage);
  2939. lock_page_cgroup(pc);
  2940. ClearPageCgroupMigration(pc);
  2941. unlock_page_cgroup(pc);
  2942. __mem_cgroup_uncharge_common(unused, MEM_CGROUP_CHARGE_TYPE_FORCE);
  2943. /*
  2944. * If a page is a file cache, radix-tree replacement is very atomic
  2945. * and we can skip this check. When it was an Anon page, its mapcount
  2946. * goes down to 0. But because we added MIGRATION flage, it's not
  2947. * uncharged yet. There are several case but page->mapcount check
  2948. * and USED bit check in mem_cgroup_uncharge_page() will do enough
  2949. * check. (see prepare_charge() also)
  2950. */
  2951. if (PageAnon(used))
  2952. mem_cgroup_uncharge_page(used);
  2953. /*
  2954. * At migration, we may charge account against cgroup which has no
  2955. * tasks.
  2956. * So, rmdir()->pre_destroy() can be called while we do this charge.
  2957. * In that case, we need to call pre_destroy() again. check it here.
  2958. */
  2959. cgroup_release_and_wakeup_rmdir(&mem->css);
  2960. }
  2961. #ifdef CONFIG_DEBUG_VM
  2962. static struct page_cgroup *lookup_page_cgroup_used(struct page *page)
  2963. {
  2964. struct page_cgroup *pc;
  2965. pc = lookup_page_cgroup(page);
  2966. if (likely(pc) && PageCgroupUsed(pc))
  2967. return pc;
  2968. return NULL;
  2969. }
  2970. bool mem_cgroup_bad_page_check(struct page *page)
  2971. {
  2972. if (mem_cgroup_disabled())
  2973. return false;
  2974. return lookup_page_cgroup_used(page) != NULL;
  2975. }
  2976. void mem_cgroup_print_bad_page(struct page *page)
  2977. {
  2978. struct page_cgroup *pc;
  2979. pc = lookup_page_cgroup_used(page);
  2980. if (pc) {
  2981. int ret = -1;
  2982. char *path;
  2983. printk(KERN_ALERT "pc:%p pc->flags:%lx pc->mem_cgroup:%p",
  2984. pc, pc->flags, pc->mem_cgroup);
  2985. path = kmalloc(PATH_MAX, GFP_KERNEL);
  2986. if (path) {
  2987. rcu_read_lock();
  2988. ret = cgroup_path(pc->mem_cgroup->css.cgroup,
  2989. path, PATH_MAX);
  2990. rcu_read_unlock();
  2991. }
  2992. printk(KERN_CONT "(%s)\n",
  2993. (ret < 0) ? "cannot get the path" : path);
  2994. kfree(path);
  2995. }
  2996. }
  2997. #endif
  2998. static DEFINE_MUTEX(set_limit_mutex);
  2999. static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
  3000. unsigned long long val)
  3001. {
  3002. int retry_count;
  3003. u64 memswlimit, memlimit;
  3004. int ret = 0;
  3005. int children = mem_cgroup_count_children(memcg);
  3006. u64 curusage, oldusage;
  3007. int enlarge;
  3008. /*
  3009. * For keeping hierarchical_reclaim simple, how long we should retry
  3010. * is depends on callers. We set our retry-count to be function
  3011. * of # of children which we should visit in this loop.
  3012. */
  3013. retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
  3014. oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
  3015. enlarge = 0;
  3016. while (retry_count) {
  3017. if (signal_pending(current)) {
  3018. ret = -EINTR;
  3019. break;
  3020. }
  3021. /*
  3022. * Rather than hide all in some function, I do this in
  3023. * open coded manner. You see what this really does.
  3024. * We have to guarantee mem->res.limit < mem->memsw.limit.
  3025. */
  3026. mutex_lock(&set_limit_mutex);
  3027. memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
  3028. if (memswlimit < val) {
  3029. ret = -EINVAL;
  3030. mutex_unlock(&set_limit_mutex);
  3031. break;
  3032. }
  3033. memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
  3034. if (memlimit < val)
  3035. enlarge = 1;
  3036. ret = res_counter_set_limit(&memcg->res, val);
  3037. if (!ret) {
  3038. if (memswlimit == val)
  3039. memcg->memsw_is_minimum = true;
  3040. else
  3041. memcg->memsw_is_minimum = false;
  3042. }
  3043. mutex_unlock(&set_limit_mutex);
  3044. if (!ret)
  3045. break;
  3046. mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL,
  3047. MEM_CGROUP_RECLAIM_SHRINK,
  3048. NULL);
  3049. curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
  3050. /* Usage is reduced ? */
  3051. if (curusage >= oldusage)
  3052. retry_count--;
  3053. else
  3054. oldusage = curusage;
  3055. }
  3056. if (!ret && enlarge)
  3057. memcg_oom_recover(memcg);
  3058. return ret;
  3059. }
  3060. static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
  3061. unsigned long long val)
  3062. {
  3063. int retry_count;
  3064. u64 memlimit, memswlimit, oldusage, curusage;
  3065. int children = mem_cgroup_count_children(memcg);
  3066. int ret = -EBUSY;
  3067. int enlarge = 0;
  3068. /* see mem_cgroup_resize_res_limit */
  3069. retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
  3070. oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
  3071. while (retry_count) {
  3072. if (signal_pending(current)) {
  3073. ret = -EINTR;
  3074. break;
  3075. }
  3076. /*
  3077. * Rather than hide all in some function, I do this in
  3078. * open coded manner. You see what this really does.
  3079. * We have to guarantee mem->res.limit < mem->memsw.limit.
  3080. */
  3081. mutex_lock(&set_limit_mutex);
  3082. memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
  3083. if (memlimit > val) {
  3084. ret = -EINVAL;
  3085. mutex_unlock(&set_limit_mutex);
  3086. break;
  3087. }
  3088. memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
  3089. if (memswlimit < val)
  3090. enlarge = 1;
  3091. ret = res_counter_set_limit(&memcg->memsw, val);
  3092. if (!ret) {
  3093. if (memlimit == val)
  3094. memcg->memsw_is_minimum = true;
  3095. else
  3096. memcg->memsw_is_minimum = false;
  3097. }
  3098. mutex_unlock(&set_limit_mutex);
  3099. if (!ret)
  3100. break;
  3101. mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL,
  3102. MEM_CGROUP_RECLAIM_NOSWAP |
  3103. MEM_CGROUP_RECLAIM_SHRINK,
  3104. NULL);
  3105. curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
  3106. /* Usage is reduced ? */
  3107. if (curusage >= oldusage)
  3108. retry_count--;
  3109. else
  3110. oldusage = curusage;
  3111. }
  3112. if (!ret && enlarge)
  3113. memcg_oom_recover(memcg);
  3114. return ret;
  3115. }
  3116. unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
  3117. gfp_t gfp_mask,
  3118. unsigned long *total_scanned)
  3119. {
  3120. unsigned long nr_reclaimed = 0;
  3121. struct mem_cgroup_per_zone *mz, *next_mz = NULL;
  3122. unsigned long reclaimed;
  3123. int loop = 0;
  3124. struct mem_cgroup_tree_per_zone *mctz;
  3125. unsigned long long excess;
  3126. unsigned long nr_scanned;
  3127. if (order > 0)
  3128. return 0;
  3129. mctz = soft_limit_tree_node_zone(zone_to_nid(zone), zone_idx(zone));
  3130. /*
  3131. * This loop can run a while, specially if mem_cgroup's continuously
  3132. * keep exceeding their soft limit and putting the system under
  3133. * pressure
  3134. */
  3135. do {
  3136. if (next_mz)
  3137. mz = next_mz;
  3138. else
  3139. mz = mem_cgroup_largest_soft_limit_node(mctz);
  3140. if (!mz)
  3141. break;
  3142. nr_scanned = 0;
  3143. reclaimed = mem_cgroup_hierarchical_reclaim(mz->mem, zone,
  3144. gfp_mask,
  3145. MEM_CGROUP_RECLAIM_SOFT,
  3146. &nr_scanned);
  3147. nr_reclaimed += reclaimed;
  3148. *total_scanned += nr_scanned;
  3149. spin_lock(&mctz->lock);
  3150. /*
  3151. * If we failed to reclaim anything from this memory cgroup
  3152. * it is time to move on to the next cgroup
  3153. */
  3154. next_mz = NULL;
  3155. if (!reclaimed) {
  3156. do {
  3157. /*
  3158. * Loop until we find yet another one.
  3159. *
  3160. * By the time we get the soft_limit lock
  3161. * again, someone might have aded the
  3162. * group back on the RB tree. Iterate to
  3163. * make sure we get a different mem.
  3164. * mem_cgroup_largest_soft_limit_node returns
  3165. * NULL if no other cgroup is present on
  3166. * the tree
  3167. */
  3168. next_mz =
  3169. __mem_cgroup_largest_soft_limit_node(mctz);
  3170. if (next_mz == mz)
  3171. css_put(&next_mz->mem->css);
  3172. else /* next_mz == NULL or other memcg */
  3173. break;
  3174. } while (1);
  3175. }
  3176. __mem_cgroup_remove_exceeded(mz->mem, mz, mctz);
  3177. excess = res_counter_soft_limit_excess(&mz->mem->res);
  3178. /*
  3179. * One school of thought says that we should not add
  3180. * back the node to the tree if reclaim returns 0.
  3181. * But our reclaim could return 0, simply because due
  3182. * to priority we are exposing a smaller subset of
  3183. * memory to reclaim from. Consider this as a longer
  3184. * term TODO.
  3185. */
  3186. /* If excess == 0, no tree ops */
  3187. __mem_cgroup_insert_exceeded(mz->mem, mz, mctz, excess);
  3188. spin_unlock(&mctz->lock);
  3189. css_put(&mz->mem->css);
  3190. loop++;
  3191. /*
  3192. * Could not reclaim anything and there are no more
  3193. * mem cgroups to try or we seem to be looping without
  3194. * reclaiming anything.
  3195. */
  3196. if (!nr_reclaimed &&
  3197. (next_mz == NULL ||
  3198. loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS))
  3199. break;
  3200. } while (!nr_reclaimed);
  3201. if (next_mz)
  3202. css_put(&next_mz->mem->css);
  3203. return nr_reclaimed;
  3204. }
  3205. /*
  3206. * This routine traverse page_cgroup in given list and drop them all.
  3207. * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
  3208. */
  3209. static int mem_cgroup_force_empty_list(struct mem_cgroup *mem,
  3210. int node, int zid, enum lru_list lru)
  3211. {
  3212. struct zone *zone;
  3213. struct mem_cgroup_per_zone *mz;
  3214. struct page_cgroup *pc, *busy;
  3215. unsigned long flags, loop;
  3216. struct list_head *list;
  3217. int ret = 0;
  3218. zone = &NODE_DATA(node)->node_zones[zid];
  3219. mz = mem_cgroup_zoneinfo(mem, node, zid);
  3220. list = &mz->lists[lru];
  3221. loop = MEM_CGROUP_ZSTAT(mz, lru);
  3222. /* give some margin against EBUSY etc...*/
  3223. loop += 256;
  3224. busy = NULL;
  3225. while (loop--) {
  3226. struct page *page;
  3227. ret = 0;
  3228. spin_lock_irqsave(&zone->lru_lock, flags);
  3229. if (list_empty(list)) {
  3230. spin_unlock_irqrestore(&zone->lru_lock, flags);
  3231. break;
  3232. }
  3233. pc = list_entry(list->prev, struct page_cgroup, lru);
  3234. if (busy == pc) {
  3235. list_move(&pc->lru, list);
  3236. busy = NULL;
  3237. spin_unlock_irqrestore(&zone->lru_lock, flags);
  3238. continue;
  3239. }
  3240. spin_unlock_irqrestore(&zone->lru_lock, flags);
  3241. page = lookup_cgroup_page(pc);
  3242. ret = mem_cgroup_move_parent(page, pc, mem, GFP_KERNEL);
  3243. if (ret == -ENOMEM)
  3244. break;
  3245. if (ret == -EBUSY || ret == -EINVAL) {
  3246. /* found lock contention or "pc" is obsolete. */
  3247. busy = pc;
  3248. cond_resched();
  3249. } else
  3250. busy = NULL;
  3251. }
  3252. if (!ret && !list_empty(list))
  3253. return -EBUSY;
  3254. return ret;
  3255. }
  3256. /*
  3257. * make mem_cgroup's charge to be 0 if there is no task.
  3258. * This enables deleting this mem_cgroup.
  3259. */
  3260. static int mem_cgroup_force_empty(struct mem_cgroup *mem, bool free_all)
  3261. {
  3262. int ret;
  3263. int node, zid, shrink;
  3264. int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
  3265. struct cgroup *cgrp = mem->css.cgroup;
  3266. css_get(&mem->css);
  3267. shrink = 0;
  3268. /* should free all ? */
  3269. if (free_all)
  3270. goto try_to_free;
  3271. move_account:
  3272. do {
  3273. ret = -EBUSY;
  3274. if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
  3275. goto out;
  3276. ret = -EINTR;
  3277. if (signal_pending(current))
  3278. goto out;
  3279. /* This is for making all *used* pages to be on LRU. */
  3280. lru_add_drain_all();
  3281. drain_all_stock_sync(mem);
  3282. ret = 0;
  3283. mem_cgroup_start_move(mem);
  3284. for_each_node_state(node, N_HIGH_MEMORY) {
  3285. for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) {
  3286. enum lru_list l;
  3287. for_each_lru(l) {
  3288. ret = mem_cgroup_force_empty_list(mem,
  3289. node, zid, l);
  3290. if (ret)
  3291. break;
  3292. }
  3293. }
  3294. if (ret)
  3295. break;
  3296. }
  3297. mem_cgroup_end_move(mem);
  3298. memcg_oom_recover(mem);
  3299. /* it seems parent cgroup doesn't have enough mem */
  3300. if (ret == -ENOMEM)
  3301. goto try_to_free;
  3302. cond_resched();
  3303. /* "ret" should also be checked to ensure all lists are empty. */
  3304. } while (mem->res.usage > 0 || ret);
  3305. out:
  3306. css_put(&mem->css);
  3307. return ret;
  3308. try_to_free:
  3309. /* returns EBUSY if there is a task or if we come here twice. */
  3310. if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) {
  3311. ret = -EBUSY;
  3312. goto out;
  3313. }
  3314. /* we call try-to-free pages for make this cgroup empty */
  3315. lru_add_drain_all();
  3316. /* try to free all pages in this cgroup */
  3317. shrink = 1;
  3318. while (nr_retries && mem->res.usage > 0) {
  3319. int progress;
  3320. if (signal_pending(current)) {
  3321. ret = -EINTR;
  3322. goto out;
  3323. }
  3324. progress = try_to_free_mem_cgroup_pages(mem, GFP_KERNEL,
  3325. false);
  3326. if (!progress) {
  3327. nr_retries--;
  3328. /* maybe some writeback is necessary */
  3329. congestion_wait(BLK_RW_ASYNC, HZ/10);
  3330. }
  3331. }
  3332. lru_add_drain();
  3333. /* try move_account...there may be some *locked* pages. */
  3334. goto move_account;
  3335. }
  3336. int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
  3337. {
  3338. return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true);
  3339. }
  3340. static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft)
  3341. {
  3342. return mem_cgroup_from_cont(cont)->use_hierarchy;
  3343. }
  3344. static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft,
  3345. u64 val)
  3346. {
  3347. int retval = 0;
  3348. struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
  3349. struct cgroup *parent = cont->parent;
  3350. struct mem_cgroup *parent_mem = NULL;
  3351. if (parent)
  3352. parent_mem = mem_cgroup_from_cont(parent);
  3353. cgroup_lock();
  3354. /*
  3355. * If parent's use_hierarchy is set, we can't make any modifications
  3356. * in the child subtrees. If it is unset, then the change can
  3357. * occur, provided the current cgroup has no children.
  3358. *
  3359. * For the root cgroup, parent_mem is NULL, we allow value to be
  3360. * set if there are no children.
  3361. */
  3362. if ((!parent_mem || !parent_mem->use_hierarchy) &&
  3363. (val == 1 || val == 0)) {
  3364. if (list_empty(&cont->children))
  3365. mem->use_hierarchy = val;
  3366. else
  3367. retval = -EBUSY;
  3368. } else
  3369. retval = -EINVAL;
  3370. cgroup_unlock();
  3371. return retval;
  3372. }
  3373. static unsigned long mem_cgroup_recursive_stat(struct mem_cgroup *mem,
  3374. enum mem_cgroup_stat_index idx)
  3375. {
  3376. struct mem_cgroup *iter;
  3377. long val = 0;
  3378. /* Per-cpu values can be negative, use a signed accumulator */
  3379. for_each_mem_cgroup_tree(iter, mem)
  3380. val += mem_cgroup_read_stat(iter, idx);
  3381. if (val < 0) /* race ? */
  3382. val = 0;
  3383. return val;
  3384. }
  3385. static inline u64 mem_cgroup_usage(struct mem_cgroup *mem, bool swap)
  3386. {
  3387. u64 val;
  3388. if (!mem_cgroup_is_root(mem)) {
  3389. if (!swap)
  3390. return res_counter_read_u64(&mem->res, RES_USAGE);
  3391. else
  3392. return res_counter_read_u64(&mem->memsw, RES_USAGE);
  3393. }
  3394. val = mem_cgroup_recursive_stat(mem, MEM_CGROUP_STAT_CACHE);
  3395. val += mem_cgroup_recursive_stat(mem, MEM_CGROUP_STAT_RSS);
  3396. if (swap)
  3397. val += mem_cgroup_recursive_stat(mem, MEM_CGROUP_STAT_SWAPOUT);
  3398. return val << PAGE_SHIFT;
  3399. }
  3400. static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
  3401. {
  3402. struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
  3403. u64 val;
  3404. int type, name;
  3405. type = MEMFILE_TYPE(cft->private);
  3406. name = MEMFILE_ATTR(cft->private);
  3407. switch (type) {
  3408. case _MEM:
  3409. if (name == RES_USAGE)
  3410. val = mem_cgroup_usage(mem, false);
  3411. else
  3412. val = res_counter_read_u64(&mem->res, name);
  3413. break;
  3414. case _MEMSWAP:
  3415. if (name == RES_USAGE)
  3416. val = mem_cgroup_usage(mem, true);
  3417. else
  3418. val = res_counter_read_u64(&mem->memsw, name);
  3419. break;
  3420. default:
  3421. BUG();
  3422. break;
  3423. }
  3424. return val;
  3425. }
  3426. /*
  3427. * The user of this function is...
  3428. * RES_LIMIT.
  3429. */
  3430. static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
  3431. const char *buffer)
  3432. {
  3433. struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
  3434. int type, name;
  3435. unsigned long long val;
  3436. int ret;
  3437. type = MEMFILE_TYPE(cft->private);
  3438. name = MEMFILE_ATTR(cft->private);
  3439. switch (name) {
  3440. case RES_LIMIT:
  3441. if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
  3442. ret = -EINVAL;
  3443. break;
  3444. }
  3445. /* This function does all necessary parse...reuse it */
  3446. ret = res_counter_memparse_write_strategy(buffer, &val);
  3447. if (ret)
  3448. break;
  3449. if (type == _MEM)
  3450. ret = mem_cgroup_resize_limit(memcg, val);
  3451. else
  3452. ret = mem_cgroup_resize_memsw_limit(memcg, val);
  3453. break;
  3454. case RES_SOFT_LIMIT:
  3455. ret = res_counter_memparse_write_strategy(buffer, &val);
  3456. if (ret)
  3457. break;
  3458. /*
  3459. * For memsw, soft limits are hard to implement in terms
  3460. * of semantics, for now, we support soft limits for
  3461. * control without swap
  3462. */
  3463. if (type == _MEM)
  3464. ret = res_counter_set_soft_limit(&memcg->res, val);
  3465. else
  3466. ret = -EINVAL;
  3467. break;
  3468. default:
  3469. ret = -EINVAL; /* should be BUG() ? */
  3470. break;
  3471. }
  3472. return ret;
  3473. }
  3474. static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
  3475. unsigned long long *mem_limit, unsigned long long *memsw_limit)
  3476. {
  3477. struct cgroup *cgroup;
  3478. unsigned long long min_limit, min_memsw_limit, tmp;
  3479. min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
  3480. min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
  3481. cgroup = memcg->css.cgroup;
  3482. if (!memcg->use_hierarchy)
  3483. goto out;
  3484. while (cgroup->parent) {
  3485. cgroup = cgroup->parent;
  3486. memcg = mem_cgroup_from_cont(cgroup);
  3487. if (!memcg->use_hierarchy)
  3488. break;
  3489. tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
  3490. min_limit = min(min_limit, tmp);
  3491. tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
  3492. min_memsw_limit = min(min_memsw_limit, tmp);
  3493. }
  3494. out:
  3495. *mem_limit = min_limit;
  3496. *memsw_limit = min_memsw_limit;
  3497. return;
  3498. }
  3499. static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
  3500. {
  3501. struct mem_cgroup *mem;
  3502. int type, name;
  3503. mem = mem_cgroup_from_cont(cont);
  3504. type = MEMFILE_TYPE(event);
  3505. name = MEMFILE_ATTR(event);
  3506. switch (name) {
  3507. case RES_MAX_USAGE:
  3508. if (type == _MEM)
  3509. res_counter_reset_max(&mem->res);
  3510. else
  3511. res_counter_reset_max(&mem->memsw);
  3512. break;
  3513. case RES_FAILCNT:
  3514. if (type == _MEM)
  3515. res_counter_reset_failcnt(&mem->res);
  3516. else
  3517. res_counter_reset_failcnt(&mem->memsw);
  3518. break;
  3519. }
  3520. return 0;
  3521. }
  3522. static u64 mem_cgroup_move_charge_read(struct cgroup *cgrp,
  3523. struct cftype *cft)
  3524. {
  3525. return mem_cgroup_from_cont(cgrp)->move_charge_at_immigrate;
  3526. }
  3527. #ifdef CONFIG_MMU
  3528. static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
  3529. struct cftype *cft, u64 val)
  3530. {
  3531. struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp);
  3532. if (val >= (1 << NR_MOVE_TYPE))
  3533. return -EINVAL;
  3534. /*
  3535. * We check this value several times in both in can_attach() and
  3536. * attach(), so we need cgroup lock to prevent this value from being
  3537. * inconsistent.
  3538. */
  3539. cgroup_lock();
  3540. mem->move_charge_at_immigrate = val;
  3541. cgroup_unlock();
  3542. return 0;
  3543. }
  3544. #else
  3545. static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
  3546. struct cftype *cft, u64 val)
  3547. {
  3548. return -ENOSYS;
  3549. }
  3550. #endif
  3551. /* For read statistics */
  3552. enum {
  3553. MCS_CACHE,
  3554. MCS_RSS,
  3555. MCS_FILE_MAPPED,
  3556. MCS_PGPGIN,
  3557. MCS_PGPGOUT,
  3558. MCS_SWAP,
  3559. MCS_PGFAULT,
  3560. MCS_PGMAJFAULT,
  3561. MCS_INACTIVE_ANON,
  3562. MCS_ACTIVE_ANON,
  3563. MCS_INACTIVE_FILE,
  3564. MCS_ACTIVE_FILE,
  3565. MCS_UNEVICTABLE,
  3566. NR_MCS_STAT,
  3567. };
  3568. struct mcs_total_stat {
  3569. s64 stat[NR_MCS_STAT];
  3570. };
  3571. struct {
  3572. char *local_name;
  3573. char *total_name;
  3574. } memcg_stat_strings[NR_MCS_STAT] = {
  3575. {"cache", "total_cache"},
  3576. {"rss", "total_rss"},
  3577. {"mapped_file", "total_mapped_file"},
  3578. {"pgpgin", "total_pgpgin"},
  3579. {"pgpgout", "total_pgpgout"},
  3580. {"swap", "total_swap"},
  3581. {"pgfault", "total_pgfault"},
  3582. {"pgmajfault", "total_pgmajfault"},
  3583. {"inactive_anon", "total_inactive_anon"},
  3584. {"active_anon", "total_active_anon"},
  3585. {"inactive_file", "total_inactive_file"},
  3586. {"active_file", "total_active_file"},
  3587. {"unevictable", "total_unevictable"}
  3588. };
  3589. static void
  3590. mem_cgroup_get_local_stat(struct mem_cgroup *mem, struct mcs_total_stat *s)
  3591. {
  3592. s64 val;
  3593. /* per cpu stat */
  3594. val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_CACHE);
  3595. s->stat[MCS_CACHE] += val * PAGE_SIZE;
  3596. val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_RSS);
  3597. s->stat[MCS_RSS] += val * PAGE_SIZE;
  3598. val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_FILE_MAPPED);
  3599. s->stat[MCS_FILE_MAPPED] += val * PAGE_SIZE;
  3600. val = mem_cgroup_read_events(mem, MEM_CGROUP_EVENTS_PGPGIN);
  3601. s->stat[MCS_PGPGIN] += val;
  3602. val = mem_cgroup_read_events(mem, MEM_CGROUP_EVENTS_PGPGOUT);
  3603. s->stat[MCS_PGPGOUT] += val;
  3604. if (do_swap_account) {
  3605. val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_SWAPOUT);
  3606. s->stat[MCS_SWAP] += val * PAGE_SIZE;
  3607. }
  3608. val = mem_cgroup_read_events(mem, MEM_CGROUP_EVENTS_PGFAULT);
  3609. s->stat[MCS_PGFAULT] += val;
  3610. val = mem_cgroup_read_events(mem, MEM_CGROUP_EVENTS_PGMAJFAULT);
  3611. s->stat[MCS_PGMAJFAULT] += val;
  3612. /* per zone stat */
  3613. val = mem_cgroup_nr_lru_pages(mem, BIT(LRU_INACTIVE_ANON));
  3614. s->stat[MCS_INACTIVE_ANON] += val * PAGE_SIZE;
  3615. val = mem_cgroup_nr_lru_pages(mem, BIT(LRU_ACTIVE_ANON));
  3616. s->stat[MCS_ACTIVE_ANON] += val * PAGE_SIZE;
  3617. val = mem_cgroup_nr_lru_pages(mem, BIT(LRU_INACTIVE_FILE));
  3618. s->stat[MCS_INACTIVE_FILE] += val * PAGE_SIZE;
  3619. val = mem_cgroup_nr_lru_pages(mem, BIT(LRU_ACTIVE_FILE));
  3620. s->stat[MCS_ACTIVE_FILE] += val * PAGE_SIZE;
  3621. val = mem_cgroup_nr_lru_pages(mem, BIT(LRU_UNEVICTABLE));
  3622. s->stat[MCS_UNEVICTABLE] += val * PAGE_SIZE;
  3623. }
  3624. static void
  3625. mem_cgroup_get_total_stat(struct mem_cgroup *mem, struct mcs_total_stat *s)
  3626. {
  3627. struct mem_cgroup *iter;
  3628. for_each_mem_cgroup_tree(iter, mem)
  3629. mem_cgroup_get_local_stat(iter, s);
  3630. }
  3631. #ifdef CONFIG_NUMA
  3632. static int mem_control_numa_stat_show(struct seq_file *m, void *arg)
  3633. {
  3634. int nid;
  3635. unsigned long total_nr, file_nr, anon_nr, unevictable_nr;
  3636. unsigned long node_nr;
  3637. struct cgroup *cont = m->private;
  3638. struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
  3639. total_nr = mem_cgroup_nr_lru_pages(mem_cont, LRU_ALL);
  3640. seq_printf(m, "total=%lu", total_nr);
  3641. for_each_node_state(nid, N_HIGH_MEMORY) {
  3642. node_nr = mem_cgroup_node_nr_lru_pages(mem_cont, nid, LRU_ALL);
  3643. seq_printf(m, " N%d=%lu", nid, node_nr);
  3644. }
  3645. seq_putc(m, '\n');
  3646. file_nr = mem_cgroup_nr_lru_pages(mem_cont, LRU_ALL_FILE);
  3647. seq_printf(m, "file=%lu", file_nr);
  3648. for_each_node_state(nid, N_HIGH_MEMORY) {
  3649. node_nr = mem_cgroup_node_nr_lru_pages(mem_cont, nid,
  3650. LRU_ALL_FILE);
  3651. seq_printf(m, " N%d=%lu", nid, node_nr);
  3652. }
  3653. seq_putc(m, '\n');
  3654. anon_nr = mem_cgroup_nr_lru_pages(mem_cont, LRU_ALL_ANON);
  3655. seq_printf(m, "anon=%lu", anon_nr);
  3656. for_each_node_state(nid, N_HIGH_MEMORY) {
  3657. node_nr = mem_cgroup_node_nr_lru_pages(mem_cont, nid,
  3658. LRU_ALL_ANON);
  3659. seq_printf(m, " N%d=%lu", nid, node_nr);
  3660. }
  3661. seq_putc(m, '\n');
  3662. unevictable_nr = mem_cgroup_nr_lru_pages(mem_cont, BIT(LRU_UNEVICTABLE));
  3663. seq_printf(m, "unevictable=%lu", unevictable_nr);
  3664. for_each_node_state(nid, N_HIGH_MEMORY) {
  3665. node_nr = mem_cgroup_node_nr_lru_pages(mem_cont, nid,
  3666. BIT(LRU_UNEVICTABLE));
  3667. seq_printf(m, " N%d=%lu", nid, node_nr);
  3668. }
  3669. seq_putc(m, '\n');
  3670. return 0;
  3671. }
  3672. #endif /* CONFIG_NUMA */
  3673. static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
  3674. struct cgroup_map_cb *cb)
  3675. {
  3676. struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
  3677. struct mcs_total_stat mystat;
  3678. int i;
  3679. memset(&mystat, 0, sizeof(mystat));
  3680. mem_cgroup_get_local_stat(mem_cont, &mystat);
  3681. for (i = 0; i < NR_MCS_STAT; i++) {
  3682. if (i == MCS_SWAP && !do_swap_account)
  3683. continue;
  3684. cb->fill(cb, memcg_stat_strings[i].local_name, mystat.stat[i]);
  3685. }
  3686. /* Hierarchical information */
  3687. {
  3688. unsigned long long limit, memsw_limit;
  3689. memcg_get_hierarchical_limit(mem_cont, &limit, &memsw_limit);
  3690. cb->fill(cb, "hierarchical_memory_limit", limit);
  3691. if (do_swap_account)
  3692. cb->fill(cb, "hierarchical_memsw_limit", memsw_limit);
  3693. }
  3694. memset(&mystat, 0, sizeof(mystat));
  3695. mem_cgroup_get_total_stat(mem_cont, &mystat);
  3696. for (i = 0; i < NR_MCS_STAT; i++) {
  3697. if (i == MCS_SWAP && !do_swap_account)
  3698. continue;
  3699. cb->fill(cb, memcg_stat_strings[i].total_name, mystat.stat[i]);
  3700. }
  3701. #ifdef CONFIG_DEBUG_VM
  3702. cb->fill(cb, "inactive_ratio", calc_inactive_ratio(mem_cont, NULL));
  3703. {
  3704. int nid, zid;
  3705. struct mem_cgroup_per_zone *mz;
  3706. unsigned long recent_rotated[2] = {0, 0};
  3707. unsigned long recent_scanned[2] = {0, 0};
  3708. for_each_online_node(nid)
  3709. for (zid = 0; zid < MAX_NR_ZONES; zid++) {
  3710. mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
  3711. recent_rotated[0] +=
  3712. mz->reclaim_stat.recent_rotated[0];
  3713. recent_rotated[1] +=
  3714. mz->reclaim_stat.recent_rotated[1];
  3715. recent_scanned[0] +=
  3716. mz->reclaim_stat.recent_scanned[0];
  3717. recent_scanned[1] +=
  3718. mz->reclaim_stat.recent_scanned[1];
  3719. }
  3720. cb->fill(cb, "recent_rotated_anon", recent_rotated[0]);
  3721. cb->fill(cb, "recent_rotated_file", recent_rotated[1]);
  3722. cb->fill(cb, "recent_scanned_anon", recent_scanned[0]);
  3723. cb->fill(cb, "recent_scanned_file", recent_scanned[1]);
  3724. }
  3725. #endif
  3726. return 0;
  3727. }
  3728. static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft)
  3729. {
  3730. struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
  3731. return mem_cgroup_swappiness(memcg);
  3732. }
  3733. static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
  3734. u64 val)
  3735. {
  3736. struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
  3737. struct mem_cgroup *parent;
  3738. if (val > 100)
  3739. return -EINVAL;
  3740. if (cgrp->parent == NULL)
  3741. return -EINVAL;
  3742. parent = mem_cgroup_from_cont(cgrp->parent);
  3743. cgroup_lock();
  3744. /* If under hierarchy, only empty-root can set this value */
  3745. if ((parent->use_hierarchy) ||
  3746. (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
  3747. cgroup_unlock();
  3748. return -EINVAL;
  3749. }
  3750. memcg->swappiness = val;
  3751. cgroup_unlock();
  3752. return 0;
  3753. }
  3754. static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap)
  3755. {
  3756. struct mem_cgroup_threshold_ary *t;
  3757. u64 usage;
  3758. int i;
  3759. rcu_read_lock();
  3760. if (!swap)
  3761. t = rcu_dereference(memcg->thresholds.primary);
  3762. else
  3763. t = rcu_dereference(memcg->memsw_thresholds.primary);
  3764. if (!t)
  3765. goto unlock;
  3766. usage = mem_cgroup_usage(memcg, swap);
  3767. /*
  3768. * current_threshold points to threshold just below usage.
  3769. * If it's not true, a threshold was crossed after last
  3770. * call of __mem_cgroup_threshold().
  3771. */
  3772. i = t->current_threshold;
  3773. /*
  3774. * Iterate backward over array of thresholds starting from
  3775. * current_threshold and check if a threshold is crossed.
  3776. * If none of thresholds below usage is crossed, we read
  3777. * only one element of the array here.
  3778. */
  3779. for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--)
  3780. eventfd_signal(t->entries[i].eventfd, 1);
  3781. /* i = current_threshold + 1 */
  3782. i++;
  3783. /*
  3784. * Iterate forward over array of thresholds starting from
  3785. * current_threshold+1 and check if a threshold is crossed.
  3786. * If none of thresholds above usage is crossed, we read
  3787. * only one element of the array here.
  3788. */
  3789. for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++)
  3790. eventfd_signal(t->entries[i].eventfd, 1);
  3791. /* Update current_threshold */
  3792. t->current_threshold = i - 1;
  3793. unlock:
  3794. rcu_read_unlock();
  3795. }
  3796. static void mem_cgroup_threshold(struct mem_cgroup *memcg)
  3797. {
  3798. while (memcg) {
  3799. __mem_cgroup_threshold(memcg, false);
  3800. if (do_swap_account)
  3801. __mem_cgroup_threshold(memcg, true);
  3802. memcg = parent_mem_cgroup(memcg);
  3803. }
  3804. }
  3805. static int compare_thresholds(const void *a, const void *b)
  3806. {
  3807. const struct mem_cgroup_threshold *_a = a;
  3808. const struct mem_cgroup_threshold *_b = b;
  3809. return _a->threshold - _b->threshold;
  3810. }
  3811. static int mem_cgroup_oom_notify_cb(struct mem_cgroup *mem)
  3812. {
  3813. struct mem_cgroup_eventfd_list *ev;
  3814. list_for_each_entry(ev, &mem->oom_notify, list)
  3815. eventfd_signal(ev->eventfd, 1);
  3816. return 0;
  3817. }
  3818. static void mem_cgroup_oom_notify(struct mem_cgroup *mem)
  3819. {
  3820. struct mem_cgroup *iter;
  3821. for_each_mem_cgroup_tree(iter, mem)
  3822. mem_cgroup_oom_notify_cb(iter);
  3823. }
  3824. static int mem_cgroup_usage_register_event(struct cgroup *cgrp,
  3825. struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
  3826. {
  3827. struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
  3828. struct mem_cgroup_thresholds *thresholds;
  3829. struct mem_cgroup_threshold_ary *new;
  3830. int type = MEMFILE_TYPE(cft->private);
  3831. u64 threshold, usage;
  3832. int i, size, ret;
  3833. ret = res_counter_memparse_write_strategy(args, &threshold);
  3834. if (ret)
  3835. return ret;
  3836. mutex_lock(&memcg->thresholds_lock);
  3837. if (type == _MEM)
  3838. thresholds = &memcg->thresholds;
  3839. else if (type == _MEMSWAP)
  3840. thresholds = &memcg->memsw_thresholds;
  3841. else
  3842. BUG();
  3843. usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
  3844. /* Check if a threshold crossed before adding a new one */
  3845. if (thresholds->primary)
  3846. __mem_cgroup_threshold(memcg, type == _MEMSWAP);
  3847. size = thresholds->primary ? thresholds->primary->size + 1 : 1;
  3848. /* Allocate memory for new array of thresholds */
  3849. new = kmalloc(sizeof(*new) + size * sizeof(struct mem_cgroup_threshold),
  3850. GFP_KERNEL);
  3851. if (!new) {
  3852. ret = -ENOMEM;
  3853. goto unlock;
  3854. }
  3855. new->size = size;
  3856. /* Copy thresholds (if any) to new array */
  3857. if (thresholds->primary) {
  3858. memcpy(new->entries, thresholds->primary->entries, (size - 1) *
  3859. sizeof(struct mem_cgroup_threshold));
  3860. }
  3861. /* Add new threshold */
  3862. new->entries[size - 1].eventfd = eventfd;
  3863. new->entries[size - 1].threshold = threshold;
  3864. /* Sort thresholds. Registering of new threshold isn't time-critical */
  3865. sort(new->entries, size, sizeof(struct mem_cgroup_threshold),
  3866. compare_thresholds, NULL);
  3867. /* Find current threshold */
  3868. new->current_threshold = -1;
  3869. for (i = 0; i < size; i++) {
  3870. if (new->entries[i].threshold < usage) {
  3871. /*
  3872. * new->current_threshold will not be used until
  3873. * rcu_assign_pointer(), so it's safe to increment
  3874. * it here.
  3875. */
  3876. ++new->current_threshold;
  3877. }
  3878. }
  3879. /* Free old spare buffer and save old primary buffer as spare */
  3880. kfree(thresholds->spare);
  3881. thresholds->spare = thresholds->primary;
  3882. rcu_assign_pointer(thresholds->primary, new);
  3883. /* To be sure that nobody uses thresholds */
  3884. synchronize_rcu();
  3885. unlock:
  3886. mutex_unlock(&memcg->thresholds_lock);
  3887. return ret;
  3888. }
  3889. static void mem_cgroup_usage_unregister_event(struct cgroup *cgrp,
  3890. struct cftype *cft, struct eventfd_ctx *eventfd)
  3891. {
  3892. struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
  3893. struct mem_cgroup_thresholds *thresholds;
  3894. struct mem_cgroup_threshold_ary *new;
  3895. int type = MEMFILE_TYPE(cft->private);
  3896. u64 usage;
  3897. int i, j, size;
  3898. mutex_lock(&memcg->thresholds_lock);
  3899. if (type == _MEM)
  3900. thresholds = &memcg->thresholds;
  3901. else if (type == _MEMSWAP)
  3902. thresholds = &memcg->memsw_thresholds;
  3903. else
  3904. BUG();
  3905. /*
  3906. * Something went wrong if we trying to unregister a threshold
  3907. * if we don't have thresholds
  3908. */
  3909. BUG_ON(!thresholds);
  3910. usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
  3911. /* Check if a threshold crossed before removing */
  3912. __mem_cgroup_threshold(memcg, type == _MEMSWAP);
  3913. /* Calculate new number of threshold */
  3914. size = 0;
  3915. for (i = 0; i < thresholds->primary->size; i++) {
  3916. if (thresholds->primary->entries[i].eventfd != eventfd)
  3917. size++;
  3918. }
  3919. new = thresholds->spare;
  3920. /* Set thresholds array to NULL if we don't have thresholds */
  3921. if (!size) {
  3922. kfree(new);
  3923. new = NULL;
  3924. goto swap_buffers;
  3925. }
  3926. new->size = size;
  3927. /* Copy thresholds and find current threshold */
  3928. new->current_threshold = -1;
  3929. for (i = 0, j = 0; i < thresholds->primary->size; i++) {
  3930. if (thresholds->primary->entries[i].eventfd == eventfd)
  3931. continue;
  3932. new->entries[j] = thresholds->primary->entries[i];
  3933. if (new->entries[j].threshold < usage) {
  3934. /*
  3935. * new->current_threshold will not be used
  3936. * until rcu_assign_pointer(), so it's safe to increment
  3937. * it here.
  3938. */
  3939. ++new->current_threshold;
  3940. }
  3941. j++;
  3942. }
  3943. swap_buffers:
  3944. /* Swap primary and spare array */
  3945. thresholds->spare = thresholds->primary;
  3946. rcu_assign_pointer(thresholds->primary, new);
  3947. /* To be sure that nobody uses thresholds */
  3948. synchronize_rcu();
  3949. mutex_unlock(&memcg->thresholds_lock);
  3950. }
  3951. static int mem_cgroup_oom_register_event(struct cgroup *cgrp,
  3952. struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
  3953. {
  3954. struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
  3955. struct mem_cgroup_eventfd_list *event;
  3956. int type = MEMFILE_TYPE(cft->private);
  3957. BUG_ON(type != _OOM_TYPE);
  3958. event = kmalloc(sizeof(*event), GFP_KERNEL);
  3959. if (!event)
  3960. return -ENOMEM;
  3961. spin_lock(&memcg_oom_lock);
  3962. event->eventfd = eventfd;
  3963. list_add(&event->list, &memcg->oom_notify);
  3964. /* already in OOM ? */
  3965. if (atomic_read(&memcg->under_oom))
  3966. eventfd_signal(eventfd, 1);
  3967. spin_unlock(&memcg_oom_lock);
  3968. return 0;
  3969. }
  3970. static void mem_cgroup_oom_unregister_event(struct cgroup *cgrp,
  3971. struct cftype *cft, struct eventfd_ctx *eventfd)
  3972. {
  3973. struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp);
  3974. struct mem_cgroup_eventfd_list *ev, *tmp;
  3975. int type = MEMFILE_TYPE(cft->private);
  3976. BUG_ON(type != _OOM_TYPE);
  3977. spin_lock(&memcg_oom_lock);
  3978. list_for_each_entry_safe(ev, tmp, &mem->oom_notify, list) {
  3979. if (ev->eventfd == eventfd) {
  3980. list_del(&ev->list);
  3981. kfree(ev);
  3982. }
  3983. }
  3984. spin_unlock(&memcg_oom_lock);
  3985. }
  3986. static int mem_cgroup_oom_control_read(struct cgroup *cgrp,
  3987. struct cftype *cft, struct cgroup_map_cb *cb)
  3988. {
  3989. struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp);
  3990. cb->fill(cb, "oom_kill_disable", mem->oom_kill_disable);
  3991. if (atomic_read(&mem->under_oom))
  3992. cb->fill(cb, "under_oom", 1);
  3993. else
  3994. cb->fill(cb, "under_oom", 0);
  3995. return 0;
  3996. }
  3997. static int mem_cgroup_oom_control_write(struct cgroup *cgrp,
  3998. struct cftype *cft, u64 val)
  3999. {
  4000. struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp);
  4001. struct mem_cgroup *parent;
  4002. /* cannot set to root cgroup and only 0 and 1 are allowed */
  4003. if (!cgrp->parent || !((val == 0) || (val == 1)))
  4004. return -EINVAL;
  4005. parent = mem_cgroup_from_cont(cgrp->parent);
  4006. cgroup_lock();
  4007. /* oom-kill-disable is a flag for subhierarchy. */
  4008. if ((parent->use_hierarchy) ||
  4009. (mem->use_hierarchy && !list_empty(&cgrp->children))) {
  4010. cgroup_unlock();
  4011. return -EINVAL;
  4012. }
  4013. mem->oom_kill_disable = val;
  4014. if (!val)
  4015. memcg_oom_recover(mem);
  4016. cgroup_unlock();
  4017. return 0;
  4018. }
  4019. #ifdef CONFIG_NUMA
  4020. static const struct file_operations mem_control_numa_stat_file_operations = {
  4021. .read = seq_read,
  4022. .llseek = seq_lseek,
  4023. .release = single_release,
  4024. };
  4025. static int mem_control_numa_stat_open(struct inode *unused, struct file *file)
  4026. {
  4027. struct cgroup *cont = file->f_dentry->d_parent->d_fsdata;
  4028. file->f_op = &mem_control_numa_stat_file_operations;
  4029. return single_open(file, mem_control_numa_stat_show, cont);
  4030. }
  4031. #endif /* CONFIG_NUMA */
  4032. static struct cftype mem_cgroup_files[] = {
  4033. {
  4034. .name = "usage_in_bytes",
  4035. .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
  4036. .read_u64 = mem_cgroup_read,
  4037. .register_event = mem_cgroup_usage_register_event,
  4038. .unregister_event = mem_cgroup_usage_unregister_event,
  4039. },
  4040. {
  4041. .name = "max_usage_in_bytes",
  4042. .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
  4043. .trigger = mem_cgroup_reset,
  4044. .read_u64 = mem_cgroup_read,
  4045. },
  4046. {
  4047. .name = "limit_in_bytes",
  4048. .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
  4049. .write_string = mem_cgroup_write,
  4050. .read_u64 = mem_cgroup_read,
  4051. },
  4052. {
  4053. .name = "soft_limit_in_bytes",
  4054. .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
  4055. .write_string = mem_cgroup_write,
  4056. .read_u64 = mem_cgroup_read,
  4057. },
  4058. {
  4059. .name = "failcnt",
  4060. .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
  4061. .trigger = mem_cgroup_reset,
  4062. .read_u64 = mem_cgroup_read,
  4063. },
  4064. {
  4065. .name = "stat",
  4066. .read_map = mem_control_stat_show,
  4067. },
  4068. {
  4069. .name = "force_empty",
  4070. .trigger = mem_cgroup_force_empty_write,
  4071. },
  4072. {
  4073. .name = "use_hierarchy",
  4074. .write_u64 = mem_cgroup_hierarchy_write,
  4075. .read_u64 = mem_cgroup_hierarchy_read,
  4076. },
  4077. {
  4078. .name = "swappiness",
  4079. .read_u64 = mem_cgroup_swappiness_read,
  4080. .write_u64 = mem_cgroup_swappiness_write,
  4081. },
  4082. {
  4083. .name = "move_charge_at_immigrate",
  4084. .read_u64 = mem_cgroup_move_charge_read,
  4085. .write_u64 = mem_cgroup_move_charge_write,
  4086. },
  4087. {
  4088. .name = "oom_control",
  4089. .read_map = mem_cgroup_oom_control_read,
  4090. .write_u64 = mem_cgroup_oom_control_write,
  4091. .register_event = mem_cgroup_oom_register_event,
  4092. .unregister_event = mem_cgroup_oom_unregister_event,
  4093. .private = MEMFILE_PRIVATE(_OOM_TYPE, OOM_CONTROL),
  4094. },
  4095. #ifdef CONFIG_NUMA
  4096. {
  4097. .name = "numa_stat",
  4098. .open = mem_control_numa_stat_open,
  4099. .mode = S_IRUGO,
  4100. },
  4101. #endif
  4102. };
  4103. #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
  4104. static struct cftype memsw_cgroup_files[] = {
  4105. {
  4106. .name = "memsw.usage_in_bytes",
  4107. .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
  4108. .read_u64 = mem_cgroup_read,
  4109. .register_event = mem_cgroup_usage_register_event,
  4110. .unregister_event = mem_cgroup_usage_unregister_event,
  4111. },
  4112. {
  4113. .name = "memsw.max_usage_in_bytes",
  4114. .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
  4115. .trigger = mem_cgroup_reset,
  4116. .read_u64 = mem_cgroup_read,
  4117. },
  4118. {
  4119. .name = "memsw.limit_in_bytes",
  4120. .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
  4121. .write_string = mem_cgroup_write,
  4122. .read_u64 = mem_cgroup_read,
  4123. },
  4124. {
  4125. .name = "memsw.failcnt",
  4126. .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
  4127. .trigger = mem_cgroup_reset,
  4128. .read_u64 = mem_cgroup_read,
  4129. },
  4130. };
  4131. static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
  4132. {
  4133. if (!do_swap_account)
  4134. return 0;
  4135. return cgroup_add_files(cont, ss, memsw_cgroup_files,
  4136. ARRAY_SIZE(memsw_cgroup_files));
  4137. };
  4138. #else
  4139. static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
  4140. {
  4141. return 0;
  4142. }
  4143. #endif
  4144. static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
  4145. {
  4146. struct mem_cgroup_per_node *pn;
  4147. struct mem_cgroup_per_zone *mz;
  4148. enum lru_list l;
  4149. int zone, tmp = node;
  4150. /*
  4151. * This routine is called against possible nodes.
  4152. * But it's BUG to call kmalloc() against offline node.
  4153. *
  4154. * TODO: this routine can waste much memory for nodes which will
  4155. * never be onlined. It's better to use memory hotplug callback
  4156. * function.
  4157. */
  4158. if (!node_state(node, N_NORMAL_MEMORY))
  4159. tmp = -1;
  4160. pn = kzalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
  4161. if (!pn)
  4162. return 1;
  4163. mem->info.nodeinfo[node] = pn;
  4164. for (zone = 0; zone < MAX_NR_ZONES; zone++) {
  4165. mz = &pn->zoneinfo[zone];
  4166. for_each_lru(l)
  4167. INIT_LIST_HEAD(&mz->lists[l]);
  4168. mz->usage_in_excess = 0;
  4169. mz->on_tree = false;
  4170. mz->mem = mem;
  4171. }
  4172. return 0;
  4173. }
  4174. static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
  4175. {
  4176. kfree(mem->info.nodeinfo[node]);
  4177. }
  4178. static struct mem_cgroup *mem_cgroup_alloc(void)
  4179. {
  4180. struct mem_cgroup *mem;
  4181. int size = sizeof(struct mem_cgroup);
  4182. /* Can be very big if MAX_NUMNODES is very big */
  4183. if (size < PAGE_SIZE)
  4184. mem = kzalloc(size, GFP_KERNEL);
  4185. else
  4186. mem = vzalloc(size);
  4187. if (!mem)
  4188. return NULL;
  4189. mem->stat = alloc_percpu(struct mem_cgroup_stat_cpu);
  4190. if (!mem->stat)
  4191. goto out_free;
  4192. spin_lock_init(&mem->pcp_counter_lock);
  4193. return mem;
  4194. out_free:
  4195. if (size < PAGE_SIZE)
  4196. kfree(mem);
  4197. else
  4198. vfree(mem);
  4199. return NULL;
  4200. }
  4201. /*
  4202. * At destroying mem_cgroup, references from swap_cgroup can remain.
  4203. * (scanning all at force_empty is too costly...)
  4204. *
  4205. * Instead of clearing all references at force_empty, we remember
  4206. * the number of reference from swap_cgroup and free mem_cgroup when
  4207. * it goes down to 0.
  4208. *
  4209. * Removal of cgroup itself succeeds regardless of refs from swap.
  4210. */
  4211. static void __mem_cgroup_free(struct mem_cgroup *mem)
  4212. {
  4213. int node;
  4214. mem_cgroup_remove_from_trees(mem);
  4215. free_css_id(&mem_cgroup_subsys, &mem->css);
  4216. for_each_node_state(node, N_POSSIBLE)
  4217. free_mem_cgroup_per_zone_info(mem, node);
  4218. free_percpu(mem->stat);
  4219. if (sizeof(struct mem_cgroup) < PAGE_SIZE)
  4220. kfree(mem);
  4221. else
  4222. vfree(mem);
  4223. }
  4224. static void mem_cgroup_get(struct mem_cgroup *mem)
  4225. {
  4226. atomic_inc(&mem->refcnt);
  4227. }
  4228. static void __mem_cgroup_put(struct mem_cgroup *mem, int count)
  4229. {
  4230. if (atomic_sub_and_test(count, &mem->refcnt)) {
  4231. struct mem_cgroup *parent = parent_mem_cgroup(mem);
  4232. __mem_cgroup_free(mem);
  4233. if (parent)
  4234. mem_cgroup_put(parent);
  4235. }
  4236. }
  4237. static void mem_cgroup_put(struct mem_cgroup *mem)
  4238. {
  4239. __mem_cgroup_put(mem, 1);
  4240. }
  4241. /*
  4242. * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
  4243. */
  4244. static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem)
  4245. {
  4246. if (!mem->res.parent)
  4247. return NULL;
  4248. return mem_cgroup_from_res_counter(mem->res.parent, res);
  4249. }
  4250. #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
  4251. static void __init enable_swap_cgroup(void)
  4252. {
  4253. if (!mem_cgroup_disabled() && really_do_swap_account)
  4254. do_swap_account = 1;
  4255. }
  4256. #else
  4257. static void __init enable_swap_cgroup(void)
  4258. {
  4259. }
  4260. #endif
  4261. static int mem_cgroup_soft_limit_tree_init(void)
  4262. {
  4263. struct mem_cgroup_tree_per_node *rtpn;
  4264. struct mem_cgroup_tree_per_zone *rtpz;
  4265. int tmp, node, zone;
  4266. for_each_node_state(node, N_POSSIBLE) {
  4267. tmp = node;
  4268. if (!node_state(node, N_NORMAL_MEMORY))
  4269. tmp = -1;
  4270. rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp);
  4271. if (!rtpn)
  4272. return 1;
  4273. soft_limit_tree.rb_tree_per_node[node] = rtpn;
  4274. for (zone = 0; zone < MAX_NR_ZONES; zone++) {
  4275. rtpz = &rtpn->rb_tree_per_zone[zone];
  4276. rtpz->rb_root = RB_ROOT;
  4277. spin_lock_init(&rtpz->lock);
  4278. }
  4279. }
  4280. return 0;
  4281. }
  4282. static struct cgroup_subsys_state * __ref
  4283. mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
  4284. {
  4285. struct mem_cgroup *mem, *parent;
  4286. long error = -ENOMEM;
  4287. int node;
  4288. mem = mem_cgroup_alloc();
  4289. if (!mem)
  4290. return ERR_PTR(error);
  4291. for_each_node_state(node, N_POSSIBLE)
  4292. if (alloc_mem_cgroup_per_zone_info(mem, node))
  4293. goto free_out;
  4294. /* root ? */
  4295. if (cont->parent == NULL) {
  4296. int cpu;
  4297. enable_swap_cgroup();
  4298. parent = NULL;
  4299. root_mem_cgroup = mem;
  4300. if (mem_cgroup_soft_limit_tree_init())
  4301. goto free_out;
  4302. for_each_possible_cpu(cpu) {
  4303. struct memcg_stock_pcp *stock =
  4304. &per_cpu(memcg_stock, cpu);
  4305. INIT_WORK(&stock->work, drain_local_stock);
  4306. }
  4307. hotcpu_notifier(memcg_cpu_hotplug_callback, 0);
  4308. } else {
  4309. parent = mem_cgroup_from_cont(cont->parent);
  4310. mem->use_hierarchy = parent->use_hierarchy;
  4311. mem->oom_kill_disable = parent->oom_kill_disable;
  4312. }
  4313. if (parent && parent->use_hierarchy) {
  4314. res_counter_init(&mem->res, &parent->res);
  4315. res_counter_init(&mem->memsw, &parent->memsw);
  4316. /*
  4317. * We increment refcnt of the parent to ensure that we can
  4318. * safely access it on res_counter_charge/uncharge.
  4319. * This refcnt will be decremented when freeing this
  4320. * mem_cgroup(see mem_cgroup_put).
  4321. */
  4322. mem_cgroup_get(parent);
  4323. } else {
  4324. res_counter_init(&mem->res, NULL);
  4325. res_counter_init(&mem->memsw, NULL);
  4326. }
  4327. mem->last_scanned_child = 0;
  4328. mem->last_scanned_node = MAX_NUMNODES;
  4329. INIT_LIST_HEAD(&mem->oom_notify);
  4330. if (parent)
  4331. mem->swappiness = mem_cgroup_swappiness(parent);
  4332. atomic_set(&mem->refcnt, 1);
  4333. mem->move_charge_at_immigrate = 0;
  4334. mutex_init(&mem->thresholds_lock);
  4335. return &mem->css;
  4336. free_out:
  4337. __mem_cgroup_free(mem);
  4338. root_mem_cgroup = NULL;
  4339. return ERR_PTR(error);
  4340. }
  4341. static int mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
  4342. struct cgroup *cont)
  4343. {
  4344. struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
  4345. return mem_cgroup_force_empty(mem, false);
  4346. }
  4347. static void mem_cgroup_destroy(struct cgroup_subsys *ss,
  4348. struct cgroup *cont)
  4349. {
  4350. struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
  4351. mem_cgroup_put(mem);
  4352. }
  4353. static int mem_cgroup_populate(struct cgroup_subsys *ss,
  4354. struct cgroup *cont)
  4355. {
  4356. int ret;
  4357. ret = cgroup_add_files(cont, ss, mem_cgroup_files,
  4358. ARRAY_SIZE(mem_cgroup_files));
  4359. if (!ret)
  4360. ret = register_memsw_files(cont, ss);
  4361. return ret;
  4362. }
  4363. #ifdef CONFIG_MMU
  4364. /* Handlers for move charge at task migration. */
  4365. #define PRECHARGE_COUNT_AT_ONCE 256
  4366. static int mem_cgroup_do_precharge(unsigned long count)
  4367. {
  4368. int ret = 0;
  4369. int batch_count = PRECHARGE_COUNT_AT_ONCE;
  4370. struct mem_cgroup *mem = mc.to;
  4371. if (mem_cgroup_is_root(mem)) {
  4372. mc.precharge += count;
  4373. /* we don't need css_get for root */
  4374. return ret;
  4375. }
  4376. /* try to charge at once */
  4377. if (count > 1) {
  4378. struct res_counter *dummy;
  4379. /*
  4380. * "mem" cannot be under rmdir() because we've already checked
  4381. * by cgroup_lock_live_cgroup() that it is not removed and we
  4382. * are still under the same cgroup_mutex. So we can postpone
  4383. * css_get().
  4384. */
  4385. if (res_counter_charge(&mem->res, PAGE_SIZE * count, &dummy))
  4386. goto one_by_one;
  4387. if (do_swap_account && res_counter_charge(&mem->memsw,
  4388. PAGE_SIZE * count, &dummy)) {
  4389. res_counter_uncharge(&mem->res, PAGE_SIZE * count);
  4390. goto one_by_one;
  4391. }
  4392. mc.precharge += count;
  4393. return ret;
  4394. }
  4395. one_by_one:
  4396. /* fall back to one by one charge */
  4397. while (count--) {
  4398. if (signal_pending(current)) {
  4399. ret = -EINTR;
  4400. break;
  4401. }
  4402. if (!batch_count--) {
  4403. batch_count = PRECHARGE_COUNT_AT_ONCE;
  4404. cond_resched();
  4405. }
  4406. ret = __mem_cgroup_try_charge(NULL, GFP_KERNEL, 1, &mem, false);
  4407. if (ret || !mem)
  4408. /* mem_cgroup_clear_mc() will do uncharge later */
  4409. return -ENOMEM;
  4410. mc.precharge++;
  4411. }
  4412. return ret;
  4413. }
  4414. /**
  4415. * is_target_pte_for_mc - check a pte whether it is valid for move charge
  4416. * @vma: the vma the pte to be checked belongs
  4417. * @addr: the address corresponding to the pte to be checked
  4418. * @ptent: the pte to be checked
  4419. * @target: the pointer the target page or swap ent will be stored(can be NULL)
  4420. *
  4421. * Returns
  4422. * 0(MC_TARGET_NONE): if the pte is not a target for move charge.
  4423. * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for
  4424. * move charge. if @target is not NULL, the page is stored in target->page
  4425. * with extra refcnt got(Callers should handle it).
  4426. * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a
  4427. * target for charge migration. if @target is not NULL, the entry is stored
  4428. * in target->ent.
  4429. *
  4430. * Called with pte lock held.
  4431. */
  4432. union mc_target {
  4433. struct page *page;
  4434. swp_entry_t ent;
  4435. };
  4436. enum mc_target_type {
  4437. MC_TARGET_NONE, /* not used */
  4438. MC_TARGET_PAGE,
  4439. MC_TARGET_SWAP,
  4440. };
  4441. static struct page *mc_handle_present_pte(struct vm_area_struct *vma,
  4442. unsigned long addr, pte_t ptent)
  4443. {
  4444. struct page *page = vm_normal_page(vma, addr, ptent);
  4445. if (!page || !page_mapped(page))
  4446. return NULL;
  4447. if (PageAnon(page)) {
  4448. /* we don't move shared anon */
  4449. if (!move_anon() || page_mapcount(page) > 2)
  4450. return NULL;
  4451. } else if (!move_file())
  4452. /* we ignore mapcount for file pages */
  4453. return NULL;
  4454. if (!get_page_unless_zero(page))
  4455. return NULL;
  4456. return page;
  4457. }
  4458. static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
  4459. unsigned long addr, pte_t ptent, swp_entry_t *entry)
  4460. {
  4461. int usage_count;
  4462. struct page *page = NULL;
  4463. swp_entry_t ent = pte_to_swp_entry(ptent);
  4464. if (!move_anon() || non_swap_entry(ent))
  4465. return NULL;
  4466. usage_count = mem_cgroup_count_swap_user(ent, &page);
  4467. if (usage_count > 1) { /* we don't move shared anon */
  4468. if (page)
  4469. put_page(page);
  4470. return NULL;
  4471. }
  4472. if (do_swap_account)
  4473. entry->val = ent.val;
  4474. return page;
  4475. }
  4476. static struct page *mc_handle_file_pte(struct vm_area_struct *vma,
  4477. unsigned long addr, pte_t ptent, swp_entry_t *entry)
  4478. {
  4479. struct page *page = NULL;
  4480. struct inode *inode;
  4481. struct address_space *mapping;
  4482. pgoff_t pgoff;
  4483. if (!vma->vm_file) /* anonymous vma */
  4484. return NULL;
  4485. if (!move_file())
  4486. return NULL;
  4487. inode = vma->vm_file->f_path.dentry->d_inode;
  4488. mapping = vma->vm_file->f_mapping;
  4489. if (pte_none(ptent))
  4490. pgoff = linear_page_index(vma, addr);
  4491. else /* pte_file(ptent) is true */
  4492. pgoff = pte_to_pgoff(ptent);
  4493. /* page is moved even if it's not RSS of this task(page-faulted). */
  4494. page = find_get_page(mapping, pgoff);
  4495. #ifdef CONFIG_SWAP
  4496. /* shmem/tmpfs may report page out on swap: account for that too. */
  4497. if (radix_tree_exceptional_entry(page)) {
  4498. swp_entry_t swap = radix_to_swp_entry(page);
  4499. if (do_swap_account)
  4500. *entry = swap;
  4501. page = find_get_page(&swapper_space, swap.val);
  4502. }
  4503. #endif
  4504. return page;
  4505. }
  4506. static int is_target_pte_for_mc(struct vm_area_struct *vma,
  4507. unsigned long addr, pte_t ptent, union mc_target *target)
  4508. {
  4509. struct page *page = NULL;
  4510. struct page_cgroup *pc;
  4511. int ret = 0;
  4512. swp_entry_t ent = { .val = 0 };
  4513. if (pte_present(ptent))
  4514. page = mc_handle_present_pte(vma, addr, ptent);
  4515. else if (is_swap_pte(ptent))
  4516. page = mc_handle_swap_pte(vma, addr, ptent, &ent);
  4517. else if (pte_none(ptent) || pte_file(ptent))
  4518. page = mc_handle_file_pte(vma, addr, ptent, &ent);
  4519. if (!page && !ent.val)
  4520. return 0;
  4521. if (page) {
  4522. pc = lookup_page_cgroup(page);
  4523. /*
  4524. * Do only loose check w/o page_cgroup lock.
  4525. * mem_cgroup_move_account() checks the pc is valid or not under
  4526. * the lock.
  4527. */
  4528. if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
  4529. ret = MC_TARGET_PAGE;
  4530. if (target)
  4531. target->page = page;
  4532. }
  4533. if (!ret || !target)
  4534. put_page(page);
  4535. }
  4536. /* There is a swap entry and a page doesn't exist or isn't charged */
  4537. if (ent.val && !ret &&
  4538. css_id(&mc.from->css) == lookup_swap_cgroup(ent)) {
  4539. ret = MC_TARGET_SWAP;
  4540. if (target)
  4541. target->ent = ent;
  4542. }
  4543. return ret;
  4544. }
  4545. static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd,
  4546. unsigned long addr, unsigned long end,
  4547. struct mm_walk *walk)
  4548. {
  4549. struct vm_area_struct *vma = walk->private;
  4550. pte_t *pte;
  4551. spinlock_t *ptl;
  4552. split_huge_page_pmd(walk->mm, pmd);
  4553. pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
  4554. for (; addr != end; pte++, addr += PAGE_SIZE)
  4555. if (is_target_pte_for_mc(vma, addr, *pte, NULL))
  4556. mc.precharge++; /* increment precharge temporarily */
  4557. pte_unmap_unlock(pte - 1, ptl);
  4558. cond_resched();
  4559. return 0;
  4560. }
  4561. static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm)
  4562. {
  4563. unsigned long precharge;
  4564. struct vm_area_struct *vma;
  4565. down_read(&mm->mmap_sem);
  4566. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  4567. struct mm_walk mem_cgroup_count_precharge_walk = {
  4568. .pmd_entry = mem_cgroup_count_precharge_pte_range,
  4569. .mm = mm,
  4570. .private = vma,
  4571. };
  4572. if (is_vm_hugetlb_page(vma))
  4573. continue;
  4574. walk_page_range(vma->vm_start, vma->vm_end,
  4575. &mem_cgroup_count_precharge_walk);
  4576. }
  4577. up_read(&mm->mmap_sem);
  4578. precharge = mc.precharge;
  4579. mc.precharge = 0;
  4580. return precharge;
  4581. }
  4582. static int mem_cgroup_precharge_mc(struct mm_struct *mm)
  4583. {
  4584. unsigned long precharge = mem_cgroup_count_precharge(mm);
  4585. VM_BUG_ON(mc.moving_task);
  4586. mc.moving_task = current;
  4587. return mem_cgroup_do_precharge(precharge);
  4588. }
  4589. /* cancels all extra charges on mc.from and mc.to, and wakes up all waiters. */
  4590. static void __mem_cgroup_clear_mc(void)
  4591. {
  4592. struct mem_cgroup *from = mc.from;
  4593. struct mem_cgroup *to = mc.to;
  4594. /* we must uncharge all the leftover precharges from mc.to */
  4595. if (mc.precharge) {
  4596. __mem_cgroup_cancel_charge(mc.to, mc.precharge);
  4597. mc.precharge = 0;
  4598. }
  4599. /*
  4600. * we didn't uncharge from mc.from at mem_cgroup_move_account(), so
  4601. * we must uncharge here.
  4602. */
  4603. if (mc.moved_charge) {
  4604. __mem_cgroup_cancel_charge(mc.from, mc.moved_charge);
  4605. mc.moved_charge = 0;
  4606. }
  4607. /* we must fixup refcnts and charges */
  4608. if (mc.moved_swap) {
  4609. /* uncharge swap account from the old cgroup */
  4610. if (!mem_cgroup_is_root(mc.from))
  4611. res_counter_uncharge(&mc.from->memsw,
  4612. PAGE_SIZE * mc.moved_swap);
  4613. __mem_cgroup_put(mc.from, mc.moved_swap);
  4614. if (!mem_cgroup_is_root(mc.to)) {
  4615. /*
  4616. * we charged both to->res and to->memsw, so we should
  4617. * uncharge to->res.
  4618. */
  4619. res_counter_uncharge(&mc.to->res,
  4620. PAGE_SIZE * mc.moved_swap);
  4621. }
  4622. /* we've already done mem_cgroup_get(mc.to) */
  4623. mc.moved_swap = 0;
  4624. }
  4625. memcg_oom_recover(from);
  4626. memcg_oom_recover(to);
  4627. wake_up_all(&mc.waitq);
  4628. }
  4629. static void mem_cgroup_clear_mc(void)
  4630. {
  4631. struct mem_cgroup *from = mc.from;
  4632. /*
  4633. * we must clear moving_task before waking up waiters at the end of
  4634. * task migration.
  4635. */
  4636. mc.moving_task = NULL;
  4637. __mem_cgroup_clear_mc();
  4638. spin_lock(&mc.lock);
  4639. mc.from = NULL;
  4640. mc.to = NULL;
  4641. spin_unlock(&mc.lock);
  4642. mem_cgroup_end_move(from);
  4643. }
  4644. static int mem_cgroup_can_attach(struct cgroup_subsys *ss,
  4645. struct cgroup *cgroup,
  4646. struct task_struct *p)
  4647. {
  4648. int ret = 0;
  4649. struct mem_cgroup *mem = mem_cgroup_from_cont(cgroup);
  4650. if (mem->move_charge_at_immigrate) {
  4651. struct mm_struct *mm;
  4652. struct mem_cgroup *from = mem_cgroup_from_task(p);
  4653. VM_BUG_ON(from == mem);
  4654. mm = get_task_mm(p);
  4655. if (!mm)
  4656. return 0;
  4657. /* We move charges only when we move a owner of the mm */
  4658. if (mm->owner == p) {
  4659. VM_BUG_ON(mc.from);
  4660. VM_BUG_ON(mc.to);
  4661. VM_BUG_ON(mc.precharge);
  4662. VM_BUG_ON(mc.moved_charge);
  4663. VM_BUG_ON(mc.moved_swap);
  4664. mem_cgroup_start_move(from);
  4665. spin_lock(&mc.lock);
  4666. mc.from = from;
  4667. mc.to = mem;
  4668. spin_unlock(&mc.lock);
  4669. /* We set mc.moving_task later */
  4670. ret = mem_cgroup_precharge_mc(mm);
  4671. if (ret)
  4672. mem_cgroup_clear_mc();
  4673. }
  4674. mmput(mm);
  4675. }
  4676. return ret;
  4677. }
  4678. static void mem_cgroup_cancel_attach(struct cgroup_subsys *ss,
  4679. struct cgroup *cgroup,
  4680. struct task_struct *p)
  4681. {
  4682. mem_cgroup_clear_mc();
  4683. }
  4684. static int mem_cgroup_move_charge_pte_range(pmd_t *pmd,
  4685. unsigned long addr, unsigned long end,
  4686. struct mm_walk *walk)
  4687. {
  4688. int ret = 0;
  4689. struct vm_area_struct *vma = walk->private;
  4690. pte_t *pte;
  4691. spinlock_t *ptl;
  4692. split_huge_page_pmd(walk->mm, pmd);
  4693. retry:
  4694. pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
  4695. for (; addr != end; addr += PAGE_SIZE) {
  4696. pte_t ptent = *(pte++);
  4697. union mc_target target;
  4698. int type;
  4699. struct page *page;
  4700. struct page_cgroup *pc;
  4701. swp_entry_t ent;
  4702. if (!mc.precharge)
  4703. break;
  4704. type = is_target_pte_for_mc(vma, addr, ptent, &target);
  4705. switch (type) {
  4706. case MC_TARGET_PAGE:
  4707. page = target.page;
  4708. if (isolate_lru_page(page))
  4709. goto put;
  4710. pc = lookup_page_cgroup(page);
  4711. if (!mem_cgroup_move_account(page, 1, pc,
  4712. mc.from, mc.to, false)) {
  4713. mc.precharge--;
  4714. /* we uncharge from mc.from later. */
  4715. mc.moved_charge++;
  4716. }
  4717. putback_lru_page(page);
  4718. put: /* is_target_pte_for_mc() gets the page */
  4719. put_page(page);
  4720. break;
  4721. case MC_TARGET_SWAP:
  4722. ent = target.ent;
  4723. if (!mem_cgroup_move_swap_account(ent,
  4724. mc.from, mc.to, false)) {
  4725. mc.precharge--;
  4726. /* we fixup refcnts and charges later. */
  4727. mc.moved_swap++;
  4728. }
  4729. break;
  4730. default:
  4731. break;
  4732. }
  4733. }
  4734. pte_unmap_unlock(pte - 1, ptl);
  4735. cond_resched();
  4736. if (addr != end) {
  4737. /*
  4738. * We have consumed all precharges we got in can_attach().
  4739. * We try charge one by one, but don't do any additional
  4740. * charges to mc.to if we have failed in charge once in attach()
  4741. * phase.
  4742. */
  4743. ret = mem_cgroup_do_precharge(1);
  4744. if (!ret)
  4745. goto retry;
  4746. }
  4747. return ret;
  4748. }
  4749. static void mem_cgroup_move_charge(struct mm_struct *mm)
  4750. {
  4751. struct vm_area_struct *vma;
  4752. lru_add_drain_all();
  4753. retry:
  4754. if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
  4755. /*
  4756. * Someone who are holding the mmap_sem might be waiting in
  4757. * waitq. So we cancel all extra charges, wake up all waiters,
  4758. * and retry. Because we cancel precharges, we might not be able
  4759. * to move enough charges, but moving charge is a best-effort
  4760. * feature anyway, so it wouldn't be a big problem.
  4761. */
  4762. __mem_cgroup_clear_mc();
  4763. cond_resched();
  4764. goto retry;
  4765. }
  4766. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  4767. int ret;
  4768. struct mm_walk mem_cgroup_move_charge_walk = {
  4769. .pmd_entry = mem_cgroup_move_charge_pte_range,
  4770. .mm = mm,
  4771. .private = vma,
  4772. };
  4773. if (is_vm_hugetlb_page(vma))
  4774. continue;
  4775. ret = walk_page_range(vma->vm_start, vma->vm_end,
  4776. &mem_cgroup_move_charge_walk);
  4777. if (ret)
  4778. /*
  4779. * means we have consumed all precharges and failed in
  4780. * doing additional charge. Just abandon here.
  4781. */
  4782. break;
  4783. }
  4784. up_read(&mm->mmap_sem);
  4785. }
  4786. static void mem_cgroup_move_task(struct cgroup_subsys *ss,
  4787. struct cgroup *cont,
  4788. struct cgroup *old_cont,
  4789. struct task_struct *p)
  4790. {
  4791. struct mm_struct *mm = get_task_mm(p);
  4792. if (mm) {
  4793. if (mc.to)
  4794. mem_cgroup_move_charge(mm);
  4795. put_swap_token(mm);
  4796. mmput(mm);
  4797. }
  4798. if (mc.to)
  4799. mem_cgroup_clear_mc();
  4800. }
  4801. #else /* !CONFIG_MMU */
  4802. static int mem_cgroup_can_attach(struct cgroup_subsys *ss,
  4803. struct cgroup *cgroup,
  4804. struct task_struct *p)
  4805. {
  4806. return 0;
  4807. }
  4808. static void mem_cgroup_cancel_attach(struct cgroup_subsys *ss,
  4809. struct cgroup *cgroup,
  4810. struct task_struct *p)
  4811. {
  4812. }
  4813. static void mem_cgroup_move_task(struct cgroup_subsys *ss,
  4814. struct cgroup *cont,
  4815. struct cgroup *old_cont,
  4816. struct task_struct *p)
  4817. {
  4818. }
  4819. #endif
  4820. struct cgroup_subsys mem_cgroup_subsys = {
  4821. .name = "memory",
  4822. .subsys_id = mem_cgroup_subsys_id,
  4823. .create = mem_cgroup_create,
  4824. .pre_destroy = mem_cgroup_pre_destroy,
  4825. .destroy = mem_cgroup_destroy,
  4826. .populate = mem_cgroup_populate,
  4827. .can_attach = mem_cgroup_can_attach,
  4828. .cancel_attach = mem_cgroup_cancel_attach,
  4829. .attach = mem_cgroup_move_task,
  4830. .early_init = 0,
  4831. .use_id = 1,
  4832. };
  4833. #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
  4834. static int __init enable_swap_account(char *s)
  4835. {
  4836. /* consider enabled if no parameter or 1 is given */
  4837. if (!strcmp(s, "1"))
  4838. really_do_swap_account = 1;
  4839. else if (!strcmp(s, "0"))
  4840. really_do_swap_account = 0;
  4841. return 1;
  4842. }
  4843. __setup("swapaccount=", enable_swap_account);
  4844. #endif