memcontrol.c 110 KB

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