memcontrol.c 142 KB

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