memcontrol.c 127 KB

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