memcontrol.c 119 KB

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