memcontrol.c 86 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432
  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. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. */
  19. #include <linux/res_counter.h>
  20. #include <linux/memcontrol.h>
  21. #include <linux/cgroup.h>
  22. #include <linux/mm.h>
  23. #include <linux/pagemap.h>
  24. #include <linux/smp.h>
  25. #include <linux/page-flags.h>
  26. #include <linux/backing-dev.h>
  27. #include <linux/bit_spinlock.h>
  28. #include <linux/rcupdate.h>
  29. #include <linux/limits.h>
  30. #include <linux/mutex.h>
  31. #include <linux/rbtree.h>
  32. #include <linux/slab.h>
  33. #include <linux/swap.h>
  34. #include <linux/spinlock.h>
  35. #include <linux/fs.h>
  36. #include <linux/seq_file.h>
  37. #include <linux/vmalloc.h>
  38. #include <linux/mm_inline.h>
  39. #include <linux/page_cgroup.h>
  40. #include <linux/cpu.h>
  41. #include "internal.h"
  42. #include <asm/uaccess.h>
  43. struct cgroup_subsys mem_cgroup_subsys __read_mostly;
  44. #define MEM_CGROUP_RECLAIM_RETRIES 5
  45. struct mem_cgroup *root_mem_cgroup __read_mostly;
  46. #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
  47. /* Turned on only when memory cgroup is enabled && really_do_swap_account = 1 */
  48. int do_swap_account __read_mostly;
  49. static int really_do_swap_account __initdata = 1; /* for remember boot option*/
  50. #else
  51. #define do_swap_account (0)
  52. #endif
  53. static DEFINE_MUTEX(memcg_tasklist); /* can be hold under cgroup_mutex */
  54. #define SOFTLIMIT_EVENTS_THRESH (1000)
  55. /*
  56. * Statistics for memory cgroup.
  57. */
  58. enum mem_cgroup_stat_index {
  59. /*
  60. * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss.
  61. */
  62. MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */
  63. MEM_CGROUP_STAT_RSS, /* # of pages charged as anon rss */
  64. MEM_CGROUP_STAT_FILE_MAPPED, /* # of pages charged as file rss */
  65. MEM_CGROUP_STAT_PGPGIN_COUNT, /* # of pages paged in */
  66. MEM_CGROUP_STAT_PGPGOUT_COUNT, /* # of pages paged out */
  67. MEM_CGROUP_STAT_EVENTS, /* sum of pagein + pageout for internal use */
  68. MEM_CGROUP_STAT_SWAPOUT, /* # of pages, swapped out */
  69. MEM_CGROUP_STAT_NSTATS,
  70. };
  71. struct mem_cgroup_stat_cpu {
  72. s64 count[MEM_CGROUP_STAT_NSTATS];
  73. } ____cacheline_aligned_in_smp;
  74. struct mem_cgroup_stat {
  75. struct mem_cgroup_stat_cpu cpustat[0];
  76. };
  77. static inline void
  78. __mem_cgroup_stat_reset_safe(struct mem_cgroup_stat_cpu *stat,
  79. enum mem_cgroup_stat_index idx)
  80. {
  81. stat->count[idx] = 0;
  82. }
  83. static inline s64
  84. __mem_cgroup_stat_read_local(struct mem_cgroup_stat_cpu *stat,
  85. enum mem_cgroup_stat_index idx)
  86. {
  87. return stat->count[idx];
  88. }
  89. /*
  90. * For accounting under irq disable, no need for increment preempt count.
  91. */
  92. static inline void __mem_cgroup_stat_add_safe(struct mem_cgroup_stat_cpu *stat,
  93. enum mem_cgroup_stat_index idx, int val)
  94. {
  95. stat->count[idx] += val;
  96. }
  97. static s64 mem_cgroup_read_stat(struct mem_cgroup_stat *stat,
  98. enum mem_cgroup_stat_index idx)
  99. {
  100. int cpu;
  101. s64 ret = 0;
  102. for_each_possible_cpu(cpu)
  103. ret += stat->cpustat[cpu].count[idx];
  104. return ret;
  105. }
  106. static s64 mem_cgroup_local_usage(struct mem_cgroup_stat *stat)
  107. {
  108. s64 ret;
  109. ret = mem_cgroup_read_stat(stat, MEM_CGROUP_STAT_CACHE);
  110. ret += mem_cgroup_read_stat(stat, MEM_CGROUP_STAT_RSS);
  111. return ret;
  112. }
  113. /*
  114. * per-zone information in memory controller.
  115. */
  116. struct mem_cgroup_per_zone {
  117. /*
  118. * spin_lock to protect the per cgroup LRU
  119. */
  120. struct list_head lists[NR_LRU_LISTS];
  121. unsigned long count[NR_LRU_LISTS];
  122. struct zone_reclaim_stat reclaim_stat;
  123. struct rb_node tree_node; /* RB tree node */
  124. unsigned long long usage_in_excess;/* Set to the value by which */
  125. /* the soft limit is exceeded*/
  126. bool on_tree;
  127. struct mem_cgroup *mem; /* Back pointer, we cannot */
  128. /* use container_of */
  129. };
  130. /* Macro for accessing counter */
  131. #define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)])
  132. struct mem_cgroup_per_node {
  133. struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
  134. };
  135. struct mem_cgroup_lru_info {
  136. struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES];
  137. };
  138. /*
  139. * Cgroups above their limits are maintained in a RB-Tree, independent of
  140. * their hierarchy representation
  141. */
  142. struct mem_cgroup_tree_per_zone {
  143. struct rb_root rb_root;
  144. spinlock_t lock;
  145. };
  146. struct mem_cgroup_tree_per_node {
  147. struct mem_cgroup_tree_per_zone rb_tree_per_zone[MAX_NR_ZONES];
  148. };
  149. struct mem_cgroup_tree {
  150. struct mem_cgroup_tree_per_node *rb_tree_per_node[MAX_NUMNODES];
  151. };
  152. static struct mem_cgroup_tree soft_limit_tree __read_mostly;
  153. /*
  154. * The memory controller data structure. The memory controller controls both
  155. * page cache and RSS per cgroup. We would eventually like to provide
  156. * statistics based on the statistics developed by Rik Van Riel for clock-pro,
  157. * to help the administrator determine what knobs to tune.
  158. *
  159. * TODO: Add a water mark for the memory controller. Reclaim will begin when
  160. * we hit the water mark. May be even add a low water mark, such that
  161. * no reclaim occurs from a cgroup at it's low water mark, this is
  162. * a feature that will be implemented much later in the future.
  163. */
  164. struct mem_cgroup {
  165. struct cgroup_subsys_state css;
  166. /*
  167. * the counter to account for memory usage
  168. */
  169. struct res_counter res;
  170. /*
  171. * the counter to account for mem+swap usage.
  172. */
  173. struct res_counter memsw;
  174. /*
  175. * Per cgroup active and inactive list, similar to the
  176. * per zone LRU lists.
  177. */
  178. struct mem_cgroup_lru_info info;
  179. /*
  180. protect against reclaim related member.
  181. */
  182. spinlock_t reclaim_param_lock;
  183. int prev_priority; /* for recording reclaim priority */
  184. /*
  185. * While reclaiming in a hierarchy, we cache the last child we
  186. * reclaimed from.
  187. */
  188. int last_scanned_child;
  189. /*
  190. * Should the accounting and control be hierarchical, per subtree?
  191. */
  192. bool use_hierarchy;
  193. unsigned long last_oom_jiffies;
  194. atomic_t refcnt;
  195. unsigned int swappiness;
  196. /* set when res.limit == memsw.limit */
  197. bool memsw_is_minimum;
  198. /*
  199. * statistics. This must be placed at the end of memcg.
  200. */
  201. struct mem_cgroup_stat stat;
  202. };
  203. /*
  204. * Maximum loops in mem_cgroup_hierarchical_reclaim(), used for soft
  205. * limit reclaim to prevent infinite loops, if they ever occur.
  206. */
  207. #define MEM_CGROUP_MAX_RECLAIM_LOOPS (100)
  208. #define MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS (2)
  209. enum charge_type {
  210. MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
  211. MEM_CGROUP_CHARGE_TYPE_MAPPED,
  212. MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */
  213. MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */
  214. MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
  215. MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */
  216. NR_CHARGE_TYPE,
  217. };
  218. /* only for here (for easy reading.) */
  219. #define PCGF_CACHE (1UL << PCG_CACHE)
  220. #define PCGF_USED (1UL << PCG_USED)
  221. #define PCGF_LOCK (1UL << PCG_LOCK)
  222. /* Not used, but added here for completeness */
  223. #define PCGF_ACCT (1UL << PCG_ACCT)
  224. /* for encoding cft->private value on file */
  225. #define _MEM (0)
  226. #define _MEMSWAP (1)
  227. #define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val))
  228. #define MEMFILE_TYPE(val) (((val) >> 16) & 0xffff)
  229. #define MEMFILE_ATTR(val) ((val) & 0xffff)
  230. /*
  231. * Reclaim flags for mem_cgroup_hierarchical_reclaim
  232. */
  233. #define MEM_CGROUP_RECLAIM_NOSWAP_BIT 0x0
  234. #define MEM_CGROUP_RECLAIM_NOSWAP (1 << MEM_CGROUP_RECLAIM_NOSWAP_BIT)
  235. #define MEM_CGROUP_RECLAIM_SHRINK_BIT 0x1
  236. #define MEM_CGROUP_RECLAIM_SHRINK (1 << MEM_CGROUP_RECLAIM_SHRINK_BIT)
  237. #define MEM_CGROUP_RECLAIM_SOFT_BIT 0x2
  238. #define MEM_CGROUP_RECLAIM_SOFT (1 << MEM_CGROUP_RECLAIM_SOFT_BIT)
  239. static void mem_cgroup_get(struct mem_cgroup *mem);
  240. static void mem_cgroup_put(struct mem_cgroup *mem);
  241. static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem);
  242. static void drain_all_stock_async(void);
  243. static struct mem_cgroup_per_zone *
  244. mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid)
  245. {
  246. return &mem->info.nodeinfo[nid]->zoneinfo[zid];
  247. }
  248. static struct mem_cgroup_per_zone *
  249. page_cgroup_zoneinfo(struct page_cgroup *pc)
  250. {
  251. struct mem_cgroup *mem = pc->mem_cgroup;
  252. int nid = page_cgroup_nid(pc);
  253. int zid = page_cgroup_zid(pc);
  254. if (!mem)
  255. return NULL;
  256. return mem_cgroup_zoneinfo(mem, nid, zid);
  257. }
  258. static struct mem_cgroup_tree_per_zone *
  259. soft_limit_tree_node_zone(int nid, int zid)
  260. {
  261. return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
  262. }
  263. static struct mem_cgroup_tree_per_zone *
  264. soft_limit_tree_from_page(struct page *page)
  265. {
  266. int nid = page_to_nid(page);
  267. int zid = page_zonenum(page);
  268. return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
  269. }
  270. static void
  271. __mem_cgroup_insert_exceeded(struct mem_cgroup *mem,
  272. struct mem_cgroup_per_zone *mz,
  273. struct mem_cgroup_tree_per_zone *mctz,
  274. unsigned long long new_usage_in_excess)
  275. {
  276. struct rb_node **p = &mctz->rb_root.rb_node;
  277. struct rb_node *parent = NULL;
  278. struct mem_cgroup_per_zone *mz_node;
  279. if (mz->on_tree)
  280. return;
  281. mz->usage_in_excess = new_usage_in_excess;
  282. if (!mz->usage_in_excess)
  283. return;
  284. while (*p) {
  285. parent = *p;
  286. mz_node = rb_entry(parent, struct mem_cgroup_per_zone,
  287. tree_node);
  288. if (mz->usage_in_excess < mz_node->usage_in_excess)
  289. p = &(*p)->rb_left;
  290. /*
  291. * We can't avoid mem cgroups that are over their soft
  292. * limit by the same amount
  293. */
  294. else if (mz->usage_in_excess >= mz_node->usage_in_excess)
  295. p = &(*p)->rb_right;
  296. }
  297. rb_link_node(&mz->tree_node, parent, p);
  298. rb_insert_color(&mz->tree_node, &mctz->rb_root);
  299. mz->on_tree = true;
  300. }
  301. static void
  302. __mem_cgroup_remove_exceeded(struct mem_cgroup *mem,
  303. struct mem_cgroup_per_zone *mz,
  304. struct mem_cgroup_tree_per_zone *mctz)
  305. {
  306. if (!mz->on_tree)
  307. return;
  308. rb_erase(&mz->tree_node, &mctz->rb_root);
  309. mz->on_tree = false;
  310. }
  311. static void
  312. mem_cgroup_remove_exceeded(struct mem_cgroup *mem,
  313. struct mem_cgroup_per_zone *mz,
  314. struct mem_cgroup_tree_per_zone *mctz)
  315. {
  316. spin_lock(&mctz->lock);
  317. __mem_cgroup_remove_exceeded(mem, mz, mctz);
  318. spin_unlock(&mctz->lock);
  319. }
  320. static bool mem_cgroup_soft_limit_check(struct mem_cgroup *mem)
  321. {
  322. bool ret = false;
  323. int cpu;
  324. s64 val;
  325. struct mem_cgroup_stat_cpu *cpustat;
  326. cpu = get_cpu();
  327. cpustat = &mem->stat.cpustat[cpu];
  328. val = __mem_cgroup_stat_read_local(cpustat, MEM_CGROUP_STAT_EVENTS);
  329. if (unlikely(val > SOFTLIMIT_EVENTS_THRESH)) {
  330. __mem_cgroup_stat_reset_safe(cpustat, MEM_CGROUP_STAT_EVENTS);
  331. ret = true;
  332. }
  333. put_cpu();
  334. return ret;
  335. }
  336. static void mem_cgroup_update_tree(struct mem_cgroup *mem, struct page *page)
  337. {
  338. unsigned long long excess;
  339. struct mem_cgroup_per_zone *mz;
  340. struct mem_cgroup_tree_per_zone *mctz;
  341. int nid = page_to_nid(page);
  342. int zid = page_zonenum(page);
  343. mctz = soft_limit_tree_from_page(page);
  344. /*
  345. * Necessary to update all ancestors when hierarchy is used.
  346. * because their event counter is not touched.
  347. */
  348. for (; mem; mem = parent_mem_cgroup(mem)) {
  349. mz = mem_cgroup_zoneinfo(mem, nid, zid);
  350. excess = res_counter_soft_limit_excess(&mem->res);
  351. /*
  352. * We have to update the tree if mz is on RB-tree or
  353. * mem is over its softlimit.
  354. */
  355. if (excess || mz->on_tree) {
  356. spin_lock(&mctz->lock);
  357. /* if on-tree, remove it */
  358. if (mz->on_tree)
  359. __mem_cgroup_remove_exceeded(mem, mz, mctz);
  360. /*
  361. * Insert again. mz->usage_in_excess will be updated.
  362. * If excess is 0, no tree ops.
  363. */
  364. __mem_cgroup_insert_exceeded(mem, mz, mctz, excess);
  365. spin_unlock(&mctz->lock);
  366. }
  367. }
  368. }
  369. static void mem_cgroup_remove_from_trees(struct mem_cgroup *mem)
  370. {
  371. int node, zone;
  372. struct mem_cgroup_per_zone *mz;
  373. struct mem_cgroup_tree_per_zone *mctz;
  374. for_each_node_state(node, N_POSSIBLE) {
  375. for (zone = 0; zone < MAX_NR_ZONES; zone++) {
  376. mz = mem_cgroup_zoneinfo(mem, node, zone);
  377. mctz = soft_limit_tree_node_zone(node, zone);
  378. mem_cgroup_remove_exceeded(mem, mz, mctz);
  379. }
  380. }
  381. }
  382. static inline unsigned long mem_cgroup_get_excess(struct mem_cgroup *mem)
  383. {
  384. return res_counter_soft_limit_excess(&mem->res) >> PAGE_SHIFT;
  385. }
  386. static struct mem_cgroup_per_zone *
  387. __mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
  388. {
  389. struct rb_node *rightmost = NULL;
  390. struct mem_cgroup_per_zone *mz;
  391. retry:
  392. mz = NULL;
  393. rightmost = rb_last(&mctz->rb_root);
  394. if (!rightmost)
  395. goto done; /* Nothing to reclaim from */
  396. mz = rb_entry(rightmost, struct mem_cgroup_per_zone, tree_node);
  397. /*
  398. * Remove the node now but someone else can add it back,
  399. * we will to add it back at the end of reclaim to its correct
  400. * position in the tree.
  401. */
  402. __mem_cgroup_remove_exceeded(mz->mem, mz, mctz);
  403. if (!res_counter_soft_limit_excess(&mz->mem->res) ||
  404. !css_tryget(&mz->mem->css))
  405. goto retry;
  406. done:
  407. return mz;
  408. }
  409. static struct mem_cgroup_per_zone *
  410. mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
  411. {
  412. struct mem_cgroup_per_zone *mz;
  413. spin_lock(&mctz->lock);
  414. mz = __mem_cgroup_largest_soft_limit_node(mctz);
  415. spin_unlock(&mctz->lock);
  416. return mz;
  417. }
  418. static void mem_cgroup_swap_statistics(struct mem_cgroup *mem,
  419. bool charge)
  420. {
  421. int val = (charge) ? 1 : -1;
  422. struct mem_cgroup_stat *stat = &mem->stat;
  423. struct mem_cgroup_stat_cpu *cpustat;
  424. int cpu = get_cpu();
  425. cpustat = &stat->cpustat[cpu];
  426. __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_SWAPOUT, val);
  427. put_cpu();
  428. }
  429. static void mem_cgroup_charge_statistics(struct mem_cgroup *mem,
  430. struct page_cgroup *pc,
  431. bool charge)
  432. {
  433. int val = (charge) ? 1 : -1;
  434. struct mem_cgroup_stat *stat = &mem->stat;
  435. struct mem_cgroup_stat_cpu *cpustat;
  436. int cpu = get_cpu();
  437. cpustat = &stat->cpustat[cpu];
  438. if (PageCgroupCache(pc))
  439. __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_CACHE, val);
  440. else
  441. __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_RSS, val);
  442. if (charge)
  443. __mem_cgroup_stat_add_safe(cpustat,
  444. MEM_CGROUP_STAT_PGPGIN_COUNT, 1);
  445. else
  446. __mem_cgroup_stat_add_safe(cpustat,
  447. MEM_CGROUP_STAT_PGPGOUT_COUNT, 1);
  448. __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_EVENTS, 1);
  449. put_cpu();
  450. }
  451. static unsigned long mem_cgroup_get_local_zonestat(struct mem_cgroup *mem,
  452. enum lru_list idx)
  453. {
  454. int nid, zid;
  455. struct mem_cgroup_per_zone *mz;
  456. u64 total = 0;
  457. for_each_online_node(nid)
  458. for (zid = 0; zid < MAX_NR_ZONES; zid++) {
  459. mz = mem_cgroup_zoneinfo(mem, nid, zid);
  460. total += MEM_CGROUP_ZSTAT(mz, idx);
  461. }
  462. return total;
  463. }
  464. static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
  465. {
  466. return container_of(cgroup_subsys_state(cont,
  467. mem_cgroup_subsys_id), struct mem_cgroup,
  468. css);
  469. }
  470. struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
  471. {
  472. /*
  473. * mm_update_next_owner() may clear mm->owner to NULL
  474. * if it races with swapoff, page migration, etc.
  475. * So this can be called with p == NULL.
  476. */
  477. if (unlikely(!p))
  478. return NULL;
  479. return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
  480. struct mem_cgroup, css);
  481. }
  482. static struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
  483. {
  484. struct mem_cgroup *mem = NULL;
  485. if (!mm)
  486. return NULL;
  487. /*
  488. * Because we have no locks, mm->owner's may be being moved to other
  489. * cgroup. We use css_tryget() here even if this looks
  490. * pessimistic (rather than adding locks here).
  491. */
  492. rcu_read_lock();
  493. do {
  494. mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
  495. if (unlikely(!mem))
  496. break;
  497. } while (!css_tryget(&mem->css));
  498. rcu_read_unlock();
  499. return mem;
  500. }
  501. /*
  502. * Call callback function against all cgroup under hierarchy tree.
  503. */
  504. static int mem_cgroup_walk_tree(struct mem_cgroup *root, void *data,
  505. int (*func)(struct mem_cgroup *, void *))
  506. {
  507. int found, ret, nextid;
  508. struct cgroup_subsys_state *css;
  509. struct mem_cgroup *mem;
  510. if (!root->use_hierarchy)
  511. return (*func)(root, data);
  512. nextid = 1;
  513. do {
  514. ret = 0;
  515. mem = NULL;
  516. rcu_read_lock();
  517. css = css_get_next(&mem_cgroup_subsys, nextid, &root->css,
  518. &found);
  519. if (css && css_tryget(css))
  520. mem = container_of(css, struct mem_cgroup, css);
  521. rcu_read_unlock();
  522. if (mem) {
  523. ret = (*func)(mem, data);
  524. css_put(&mem->css);
  525. }
  526. nextid = found + 1;
  527. } while (!ret && css);
  528. return ret;
  529. }
  530. static inline bool mem_cgroup_is_root(struct mem_cgroup *mem)
  531. {
  532. return (mem == root_mem_cgroup);
  533. }
  534. /*
  535. * Following LRU functions are allowed to be used without PCG_LOCK.
  536. * Operations are called by routine of global LRU independently from memcg.
  537. * What we have to take care of here is validness of pc->mem_cgroup.
  538. *
  539. * Changes to pc->mem_cgroup happens when
  540. * 1. charge
  541. * 2. moving account
  542. * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
  543. * It is added to LRU before charge.
  544. * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
  545. * When moving account, the page is not on LRU. It's isolated.
  546. */
  547. void mem_cgroup_del_lru_list(struct page *page, enum lru_list lru)
  548. {
  549. struct page_cgroup *pc;
  550. struct mem_cgroup_per_zone *mz;
  551. if (mem_cgroup_disabled())
  552. return;
  553. pc = lookup_page_cgroup(page);
  554. /* can happen while we handle swapcache. */
  555. if (!TestClearPageCgroupAcctLRU(pc))
  556. return;
  557. VM_BUG_ON(!pc->mem_cgroup);
  558. /*
  559. * We don't check PCG_USED bit. It's cleared when the "page" is finally
  560. * removed from global LRU.
  561. */
  562. mz = page_cgroup_zoneinfo(pc);
  563. MEM_CGROUP_ZSTAT(mz, lru) -= 1;
  564. if (mem_cgroup_is_root(pc->mem_cgroup))
  565. return;
  566. VM_BUG_ON(list_empty(&pc->lru));
  567. list_del_init(&pc->lru);
  568. return;
  569. }
  570. void mem_cgroup_del_lru(struct page *page)
  571. {
  572. mem_cgroup_del_lru_list(page, page_lru(page));
  573. }
  574. void mem_cgroup_rotate_lru_list(struct page *page, enum lru_list lru)
  575. {
  576. struct mem_cgroup_per_zone *mz;
  577. struct page_cgroup *pc;
  578. if (mem_cgroup_disabled())
  579. return;
  580. pc = lookup_page_cgroup(page);
  581. /*
  582. * Used bit is set without atomic ops but after smp_wmb().
  583. * For making pc->mem_cgroup visible, insert smp_rmb() here.
  584. */
  585. smp_rmb();
  586. /* unused or root page is not rotated. */
  587. if (!PageCgroupUsed(pc) || mem_cgroup_is_root(pc->mem_cgroup))
  588. return;
  589. mz = page_cgroup_zoneinfo(pc);
  590. list_move(&pc->lru, &mz->lists[lru]);
  591. }
  592. void mem_cgroup_add_lru_list(struct page *page, enum lru_list lru)
  593. {
  594. struct page_cgroup *pc;
  595. struct mem_cgroup_per_zone *mz;
  596. if (mem_cgroup_disabled())
  597. return;
  598. pc = lookup_page_cgroup(page);
  599. VM_BUG_ON(PageCgroupAcctLRU(pc));
  600. /*
  601. * Used bit is set without atomic ops but after smp_wmb().
  602. * For making pc->mem_cgroup visible, insert smp_rmb() here.
  603. */
  604. smp_rmb();
  605. if (!PageCgroupUsed(pc))
  606. return;
  607. mz = page_cgroup_zoneinfo(pc);
  608. MEM_CGROUP_ZSTAT(mz, lru) += 1;
  609. SetPageCgroupAcctLRU(pc);
  610. if (mem_cgroup_is_root(pc->mem_cgroup))
  611. return;
  612. list_add(&pc->lru, &mz->lists[lru]);
  613. }
  614. /*
  615. * At handling SwapCache, pc->mem_cgroup may be changed while it's linked to
  616. * lru because the page may.be reused after it's fully uncharged (because of
  617. * SwapCache behavior).To handle that, unlink page_cgroup from LRU when charge
  618. * it again. This function is only used to charge SwapCache. It's done under
  619. * lock_page and expected that zone->lru_lock is never held.
  620. */
  621. static void mem_cgroup_lru_del_before_commit_swapcache(struct page *page)
  622. {
  623. unsigned long flags;
  624. struct zone *zone = page_zone(page);
  625. struct page_cgroup *pc = lookup_page_cgroup(page);
  626. spin_lock_irqsave(&zone->lru_lock, flags);
  627. /*
  628. * Forget old LRU when this page_cgroup is *not* used. This Used bit
  629. * is guarded by lock_page() because the page is SwapCache.
  630. */
  631. if (!PageCgroupUsed(pc))
  632. mem_cgroup_del_lru_list(page, page_lru(page));
  633. spin_unlock_irqrestore(&zone->lru_lock, flags);
  634. }
  635. static void mem_cgroup_lru_add_after_commit_swapcache(struct page *page)
  636. {
  637. unsigned long flags;
  638. struct zone *zone = page_zone(page);
  639. struct page_cgroup *pc = lookup_page_cgroup(page);
  640. spin_lock_irqsave(&zone->lru_lock, flags);
  641. /* link when the page is linked to LRU but page_cgroup isn't */
  642. if (PageLRU(page) && !PageCgroupAcctLRU(pc))
  643. mem_cgroup_add_lru_list(page, page_lru(page));
  644. spin_unlock_irqrestore(&zone->lru_lock, flags);
  645. }
  646. void mem_cgroup_move_lists(struct page *page,
  647. enum lru_list from, enum lru_list to)
  648. {
  649. if (mem_cgroup_disabled())
  650. return;
  651. mem_cgroup_del_lru_list(page, from);
  652. mem_cgroup_add_lru_list(page, to);
  653. }
  654. int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
  655. {
  656. int ret;
  657. struct mem_cgroup *curr = NULL;
  658. task_lock(task);
  659. rcu_read_lock();
  660. curr = try_get_mem_cgroup_from_mm(task->mm);
  661. rcu_read_unlock();
  662. task_unlock(task);
  663. if (!curr)
  664. return 0;
  665. if (curr->use_hierarchy)
  666. ret = css_is_ancestor(&curr->css, &mem->css);
  667. else
  668. ret = (curr == mem);
  669. css_put(&curr->css);
  670. return ret;
  671. }
  672. /*
  673. * prev_priority control...this will be used in memory reclaim path.
  674. */
  675. int mem_cgroup_get_reclaim_priority(struct mem_cgroup *mem)
  676. {
  677. int prev_priority;
  678. spin_lock(&mem->reclaim_param_lock);
  679. prev_priority = mem->prev_priority;
  680. spin_unlock(&mem->reclaim_param_lock);
  681. return prev_priority;
  682. }
  683. void mem_cgroup_note_reclaim_priority(struct mem_cgroup *mem, int priority)
  684. {
  685. spin_lock(&mem->reclaim_param_lock);
  686. if (priority < mem->prev_priority)
  687. mem->prev_priority = priority;
  688. spin_unlock(&mem->reclaim_param_lock);
  689. }
  690. void mem_cgroup_record_reclaim_priority(struct mem_cgroup *mem, int priority)
  691. {
  692. spin_lock(&mem->reclaim_param_lock);
  693. mem->prev_priority = priority;
  694. spin_unlock(&mem->reclaim_param_lock);
  695. }
  696. static int calc_inactive_ratio(struct mem_cgroup *memcg, unsigned long *present_pages)
  697. {
  698. unsigned long active;
  699. unsigned long inactive;
  700. unsigned long gb;
  701. unsigned long inactive_ratio;
  702. inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_ANON);
  703. active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_ANON);
  704. gb = (inactive + active) >> (30 - PAGE_SHIFT);
  705. if (gb)
  706. inactive_ratio = int_sqrt(10 * gb);
  707. else
  708. inactive_ratio = 1;
  709. if (present_pages) {
  710. present_pages[0] = inactive;
  711. present_pages[1] = active;
  712. }
  713. return inactive_ratio;
  714. }
  715. int mem_cgroup_inactive_anon_is_low(struct mem_cgroup *memcg)
  716. {
  717. unsigned long active;
  718. unsigned long inactive;
  719. unsigned long present_pages[2];
  720. unsigned long inactive_ratio;
  721. inactive_ratio = calc_inactive_ratio(memcg, present_pages);
  722. inactive = present_pages[0];
  723. active = present_pages[1];
  724. if (inactive * inactive_ratio < active)
  725. return 1;
  726. return 0;
  727. }
  728. int mem_cgroup_inactive_file_is_low(struct mem_cgroup *memcg)
  729. {
  730. unsigned long active;
  731. unsigned long inactive;
  732. inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_FILE);
  733. active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_FILE);
  734. return (active > inactive);
  735. }
  736. unsigned long mem_cgroup_zone_nr_pages(struct mem_cgroup *memcg,
  737. struct zone *zone,
  738. enum lru_list lru)
  739. {
  740. int nid = zone->zone_pgdat->node_id;
  741. int zid = zone_idx(zone);
  742. struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
  743. return MEM_CGROUP_ZSTAT(mz, lru);
  744. }
  745. struct zone_reclaim_stat *mem_cgroup_get_reclaim_stat(struct mem_cgroup *memcg,
  746. struct zone *zone)
  747. {
  748. int nid = zone->zone_pgdat->node_id;
  749. int zid = zone_idx(zone);
  750. struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
  751. return &mz->reclaim_stat;
  752. }
  753. struct zone_reclaim_stat *
  754. mem_cgroup_get_reclaim_stat_from_page(struct page *page)
  755. {
  756. struct page_cgroup *pc;
  757. struct mem_cgroup_per_zone *mz;
  758. if (mem_cgroup_disabled())
  759. return NULL;
  760. pc = lookup_page_cgroup(page);
  761. /*
  762. * Used bit is set without atomic ops but after smp_wmb().
  763. * For making pc->mem_cgroup visible, insert smp_rmb() here.
  764. */
  765. smp_rmb();
  766. if (!PageCgroupUsed(pc))
  767. return NULL;
  768. mz = page_cgroup_zoneinfo(pc);
  769. if (!mz)
  770. return NULL;
  771. return &mz->reclaim_stat;
  772. }
  773. unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
  774. struct list_head *dst,
  775. unsigned long *scanned, int order,
  776. int mode, struct zone *z,
  777. struct mem_cgroup *mem_cont,
  778. int active, int file)
  779. {
  780. unsigned long nr_taken = 0;
  781. struct page *page;
  782. unsigned long scan;
  783. LIST_HEAD(pc_list);
  784. struct list_head *src;
  785. struct page_cgroup *pc, *tmp;
  786. int nid = z->zone_pgdat->node_id;
  787. int zid = zone_idx(z);
  788. struct mem_cgroup_per_zone *mz;
  789. int lru = LRU_FILE * file + active;
  790. int ret;
  791. BUG_ON(!mem_cont);
  792. mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
  793. src = &mz->lists[lru];
  794. scan = 0;
  795. list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
  796. if (scan >= nr_to_scan)
  797. break;
  798. page = pc->page;
  799. if (unlikely(!PageCgroupUsed(pc)))
  800. continue;
  801. if (unlikely(!PageLRU(page)))
  802. continue;
  803. scan++;
  804. ret = __isolate_lru_page(page, mode, file);
  805. switch (ret) {
  806. case 0:
  807. list_move(&page->lru, dst);
  808. mem_cgroup_del_lru(page);
  809. nr_taken++;
  810. break;
  811. case -EBUSY:
  812. /* we don't affect global LRU but rotate in our LRU */
  813. mem_cgroup_rotate_lru_list(page, page_lru(page));
  814. break;
  815. default:
  816. break;
  817. }
  818. }
  819. *scanned = scan;
  820. return nr_taken;
  821. }
  822. #define mem_cgroup_from_res_counter(counter, member) \
  823. container_of(counter, struct mem_cgroup, member)
  824. static bool mem_cgroup_check_under_limit(struct mem_cgroup *mem)
  825. {
  826. if (do_swap_account) {
  827. if (res_counter_check_under_limit(&mem->res) &&
  828. res_counter_check_under_limit(&mem->memsw))
  829. return true;
  830. } else
  831. if (res_counter_check_under_limit(&mem->res))
  832. return true;
  833. return false;
  834. }
  835. static unsigned int get_swappiness(struct mem_cgroup *memcg)
  836. {
  837. struct cgroup *cgrp = memcg->css.cgroup;
  838. unsigned int swappiness;
  839. /* root ? */
  840. if (cgrp->parent == NULL)
  841. return vm_swappiness;
  842. spin_lock(&memcg->reclaim_param_lock);
  843. swappiness = memcg->swappiness;
  844. spin_unlock(&memcg->reclaim_param_lock);
  845. return swappiness;
  846. }
  847. static int mem_cgroup_count_children_cb(struct mem_cgroup *mem, void *data)
  848. {
  849. int *val = data;
  850. (*val)++;
  851. return 0;
  852. }
  853. /**
  854. * mem_cgroup_print_mem_info: Called from OOM with tasklist_lock held in read mode.
  855. * @memcg: The memory cgroup that went over limit
  856. * @p: Task that is going to be killed
  857. *
  858. * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
  859. * enabled
  860. */
  861. void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
  862. {
  863. struct cgroup *task_cgrp;
  864. struct cgroup *mem_cgrp;
  865. /*
  866. * Need a buffer in BSS, can't rely on allocations. The code relies
  867. * on the assumption that OOM is serialized for memory controller.
  868. * If this assumption is broken, revisit this code.
  869. */
  870. static char memcg_name[PATH_MAX];
  871. int ret;
  872. if (!memcg)
  873. return;
  874. rcu_read_lock();
  875. mem_cgrp = memcg->css.cgroup;
  876. task_cgrp = task_cgroup(p, mem_cgroup_subsys_id);
  877. ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX);
  878. if (ret < 0) {
  879. /*
  880. * Unfortunately, we are unable to convert to a useful name
  881. * But we'll still print out the usage information
  882. */
  883. rcu_read_unlock();
  884. goto done;
  885. }
  886. rcu_read_unlock();
  887. printk(KERN_INFO "Task in %s killed", memcg_name);
  888. rcu_read_lock();
  889. ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX);
  890. if (ret < 0) {
  891. rcu_read_unlock();
  892. goto done;
  893. }
  894. rcu_read_unlock();
  895. /*
  896. * Continues from above, so we don't need an KERN_ level
  897. */
  898. printk(KERN_CONT " as a result of limit of %s\n", memcg_name);
  899. done:
  900. printk(KERN_INFO "memory: usage %llukB, limit %llukB, failcnt %llu\n",
  901. res_counter_read_u64(&memcg->res, RES_USAGE) >> 10,
  902. res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10,
  903. res_counter_read_u64(&memcg->res, RES_FAILCNT));
  904. printk(KERN_INFO "memory+swap: usage %llukB, limit %llukB, "
  905. "failcnt %llu\n",
  906. res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10,
  907. res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10,
  908. res_counter_read_u64(&memcg->memsw, RES_FAILCNT));
  909. }
  910. /*
  911. * This function returns the number of memcg under hierarchy tree. Returns
  912. * 1(self count) if no children.
  913. */
  914. static int mem_cgroup_count_children(struct mem_cgroup *mem)
  915. {
  916. int num = 0;
  917. mem_cgroup_walk_tree(mem, &num, mem_cgroup_count_children_cb);
  918. return num;
  919. }
  920. /*
  921. * Visit the first child (need not be the first child as per the ordering
  922. * of the cgroup list, since we track last_scanned_child) of @mem and use
  923. * that to reclaim free pages from.
  924. */
  925. static struct mem_cgroup *
  926. mem_cgroup_select_victim(struct mem_cgroup *root_mem)
  927. {
  928. struct mem_cgroup *ret = NULL;
  929. struct cgroup_subsys_state *css;
  930. int nextid, found;
  931. if (!root_mem->use_hierarchy) {
  932. css_get(&root_mem->css);
  933. ret = root_mem;
  934. }
  935. while (!ret) {
  936. rcu_read_lock();
  937. nextid = root_mem->last_scanned_child + 1;
  938. css = css_get_next(&mem_cgroup_subsys, nextid, &root_mem->css,
  939. &found);
  940. if (css && css_tryget(css))
  941. ret = container_of(css, struct mem_cgroup, css);
  942. rcu_read_unlock();
  943. /* Updates scanning parameter */
  944. spin_lock(&root_mem->reclaim_param_lock);
  945. if (!css) {
  946. /* this means start scan from ID:1 */
  947. root_mem->last_scanned_child = 0;
  948. } else
  949. root_mem->last_scanned_child = found;
  950. spin_unlock(&root_mem->reclaim_param_lock);
  951. }
  952. return ret;
  953. }
  954. /*
  955. * Scan the hierarchy if needed to reclaim memory. We remember the last child
  956. * we reclaimed from, so that we don't end up penalizing one child extensively
  957. * based on its position in the children list.
  958. *
  959. * root_mem is the original ancestor that we've been reclaim from.
  960. *
  961. * We give up and return to the caller when we visit root_mem twice.
  962. * (other groups can be removed while we're walking....)
  963. *
  964. * If shrink==true, for avoiding to free too much, this returns immedieately.
  965. */
  966. static int mem_cgroup_hierarchical_reclaim(struct mem_cgroup *root_mem,
  967. struct zone *zone,
  968. gfp_t gfp_mask,
  969. unsigned long reclaim_options)
  970. {
  971. struct mem_cgroup *victim;
  972. int ret, total = 0;
  973. int loop = 0;
  974. bool noswap = reclaim_options & MEM_CGROUP_RECLAIM_NOSWAP;
  975. bool shrink = reclaim_options & MEM_CGROUP_RECLAIM_SHRINK;
  976. bool check_soft = reclaim_options & MEM_CGROUP_RECLAIM_SOFT;
  977. unsigned long excess = mem_cgroup_get_excess(root_mem);
  978. /* If memsw_is_minimum==1, swap-out is of-no-use. */
  979. if (root_mem->memsw_is_minimum)
  980. noswap = true;
  981. while (1) {
  982. victim = mem_cgroup_select_victim(root_mem);
  983. if (victim == root_mem) {
  984. loop++;
  985. if (loop >= 1)
  986. drain_all_stock_async();
  987. if (loop >= 2) {
  988. /*
  989. * If we have not been able to reclaim
  990. * anything, it might because there are
  991. * no reclaimable pages under this hierarchy
  992. */
  993. if (!check_soft || !total) {
  994. css_put(&victim->css);
  995. break;
  996. }
  997. /*
  998. * We want to do more targetted reclaim.
  999. * excess >> 2 is not to excessive so as to
  1000. * reclaim too much, nor too less that we keep
  1001. * coming back to reclaim from this cgroup
  1002. */
  1003. if (total >= (excess >> 2) ||
  1004. (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS)) {
  1005. css_put(&victim->css);
  1006. break;
  1007. }
  1008. }
  1009. }
  1010. if (!mem_cgroup_local_usage(&victim->stat)) {
  1011. /* this cgroup's local usage == 0 */
  1012. css_put(&victim->css);
  1013. continue;
  1014. }
  1015. /* we use swappiness of local cgroup */
  1016. if (check_soft)
  1017. ret = mem_cgroup_shrink_node_zone(victim, gfp_mask,
  1018. noswap, get_swappiness(victim), zone,
  1019. zone->zone_pgdat->node_id);
  1020. else
  1021. ret = try_to_free_mem_cgroup_pages(victim, gfp_mask,
  1022. noswap, get_swappiness(victim));
  1023. css_put(&victim->css);
  1024. /*
  1025. * At shrinking usage, we can't check we should stop here or
  1026. * reclaim more. It's depends on callers. last_scanned_child
  1027. * will work enough for keeping fairness under tree.
  1028. */
  1029. if (shrink)
  1030. return ret;
  1031. total += ret;
  1032. if (check_soft) {
  1033. if (res_counter_check_under_soft_limit(&root_mem->res))
  1034. return total;
  1035. } else if (mem_cgroup_check_under_limit(root_mem))
  1036. return 1 + total;
  1037. }
  1038. return total;
  1039. }
  1040. bool mem_cgroup_oom_called(struct task_struct *task)
  1041. {
  1042. bool ret = false;
  1043. struct mem_cgroup *mem;
  1044. struct mm_struct *mm;
  1045. rcu_read_lock();
  1046. mm = task->mm;
  1047. if (!mm)
  1048. mm = &init_mm;
  1049. mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
  1050. if (mem && time_before(jiffies, mem->last_oom_jiffies + HZ/10))
  1051. ret = true;
  1052. rcu_read_unlock();
  1053. return ret;
  1054. }
  1055. static int record_last_oom_cb(struct mem_cgroup *mem, void *data)
  1056. {
  1057. mem->last_oom_jiffies = jiffies;
  1058. return 0;
  1059. }
  1060. static void record_last_oom(struct mem_cgroup *mem)
  1061. {
  1062. mem_cgroup_walk_tree(mem, NULL, record_last_oom_cb);
  1063. }
  1064. /*
  1065. * Currently used to update mapped file statistics, but the routine can be
  1066. * generalized to update other statistics as well.
  1067. */
  1068. void mem_cgroup_update_file_mapped(struct page *page, int val)
  1069. {
  1070. struct mem_cgroup *mem;
  1071. struct mem_cgroup_stat *stat;
  1072. struct mem_cgroup_stat_cpu *cpustat;
  1073. int cpu;
  1074. struct page_cgroup *pc;
  1075. pc = lookup_page_cgroup(page);
  1076. if (unlikely(!pc))
  1077. return;
  1078. lock_page_cgroup(pc);
  1079. mem = pc->mem_cgroup;
  1080. if (!mem)
  1081. goto done;
  1082. if (!PageCgroupUsed(pc))
  1083. goto done;
  1084. /*
  1085. * Preemption is already disabled, we don't need get_cpu()
  1086. */
  1087. cpu = smp_processor_id();
  1088. stat = &mem->stat;
  1089. cpustat = &stat->cpustat[cpu];
  1090. __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_FILE_MAPPED, val);
  1091. done:
  1092. unlock_page_cgroup(pc);
  1093. }
  1094. /*
  1095. * size of first charge trial. "32" comes from vmscan.c's magic value.
  1096. * TODO: maybe necessary to use big numbers in big irons.
  1097. */
  1098. #define CHARGE_SIZE (32 * PAGE_SIZE)
  1099. struct memcg_stock_pcp {
  1100. struct mem_cgroup *cached; /* this never be root cgroup */
  1101. int charge;
  1102. struct work_struct work;
  1103. };
  1104. static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock);
  1105. static atomic_t memcg_drain_count;
  1106. /*
  1107. * Try to consume stocked charge on this cpu. If success, PAGE_SIZE is consumed
  1108. * from local stock and true is returned. If the stock is 0 or charges from a
  1109. * cgroup which is not current target, returns false. This stock will be
  1110. * refilled.
  1111. */
  1112. static bool consume_stock(struct mem_cgroup *mem)
  1113. {
  1114. struct memcg_stock_pcp *stock;
  1115. bool ret = true;
  1116. stock = &get_cpu_var(memcg_stock);
  1117. if (mem == stock->cached && stock->charge)
  1118. stock->charge -= PAGE_SIZE;
  1119. else /* need to call res_counter_charge */
  1120. ret = false;
  1121. put_cpu_var(memcg_stock);
  1122. return ret;
  1123. }
  1124. /*
  1125. * Returns stocks cached in percpu to res_counter and reset cached information.
  1126. */
  1127. static void drain_stock(struct memcg_stock_pcp *stock)
  1128. {
  1129. struct mem_cgroup *old = stock->cached;
  1130. if (stock->charge) {
  1131. res_counter_uncharge(&old->res, stock->charge);
  1132. if (do_swap_account)
  1133. res_counter_uncharge(&old->memsw, stock->charge);
  1134. }
  1135. stock->cached = NULL;
  1136. stock->charge = 0;
  1137. }
  1138. /*
  1139. * This must be called under preempt disabled or must be called by
  1140. * a thread which is pinned to local cpu.
  1141. */
  1142. static void drain_local_stock(struct work_struct *dummy)
  1143. {
  1144. struct memcg_stock_pcp *stock = &__get_cpu_var(memcg_stock);
  1145. drain_stock(stock);
  1146. }
  1147. /*
  1148. * Cache charges(val) which is from res_counter, to local per_cpu area.
  1149. * This will be consumed by consumt_stock() function, later.
  1150. */
  1151. static void refill_stock(struct mem_cgroup *mem, int val)
  1152. {
  1153. struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
  1154. if (stock->cached != mem) { /* reset if necessary */
  1155. drain_stock(stock);
  1156. stock->cached = mem;
  1157. }
  1158. stock->charge += val;
  1159. put_cpu_var(memcg_stock);
  1160. }
  1161. /*
  1162. * Tries to drain stocked charges in other cpus. This function is asynchronous
  1163. * and just put a work per cpu for draining localy on each cpu. Caller can
  1164. * expects some charges will be back to res_counter later but cannot wait for
  1165. * it.
  1166. */
  1167. static void drain_all_stock_async(void)
  1168. {
  1169. int cpu;
  1170. /* This function is for scheduling "drain" in asynchronous way.
  1171. * The result of "drain" is not directly handled by callers. Then,
  1172. * if someone is calling drain, we don't have to call drain more.
  1173. * Anyway, WORK_STRUCT_PENDING check in queue_work_on() will catch if
  1174. * there is a race. We just do loose check here.
  1175. */
  1176. if (atomic_read(&memcg_drain_count))
  1177. return;
  1178. /* Notify other cpus that system-wide "drain" is running */
  1179. atomic_inc(&memcg_drain_count);
  1180. get_online_cpus();
  1181. for_each_online_cpu(cpu) {
  1182. struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
  1183. schedule_work_on(cpu, &stock->work);
  1184. }
  1185. put_online_cpus();
  1186. atomic_dec(&memcg_drain_count);
  1187. /* We don't wait for flush_work */
  1188. }
  1189. /* This is a synchronous drain interface. */
  1190. static void drain_all_stock_sync(void)
  1191. {
  1192. /* called when force_empty is called */
  1193. atomic_inc(&memcg_drain_count);
  1194. schedule_on_each_cpu(drain_local_stock);
  1195. atomic_dec(&memcg_drain_count);
  1196. }
  1197. static int __cpuinit memcg_stock_cpu_callback(struct notifier_block *nb,
  1198. unsigned long action,
  1199. void *hcpu)
  1200. {
  1201. int cpu = (unsigned long)hcpu;
  1202. struct memcg_stock_pcp *stock;
  1203. if (action != CPU_DEAD)
  1204. return NOTIFY_OK;
  1205. stock = &per_cpu(memcg_stock, cpu);
  1206. drain_stock(stock);
  1207. return NOTIFY_OK;
  1208. }
  1209. /*
  1210. * Unlike exported interface, "oom" parameter is added. if oom==true,
  1211. * oom-killer can be invoked.
  1212. */
  1213. static int __mem_cgroup_try_charge(struct mm_struct *mm,
  1214. gfp_t gfp_mask, struct mem_cgroup **memcg,
  1215. bool oom, struct page *page)
  1216. {
  1217. struct mem_cgroup *mem, *mem_over_limit;
  1218. int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
  1219. struct res_counter *fail_res;
  1220. int csize = CHARGE_SIZE;
  1221. if (unlikely(test_thread_flag(TIF_MEMDIE))) {
  1222. /* Don't account this! */
  1223. *memcg = NULL;
  1224. return 0;
  1225. }
  1226. /*
  1227. * We always charge the cgroup the mm_struct belongs to.
  1228. * The mm_struct's mem_cgroup changes on task migration if the
  1229. * thread group leader migrates. It's possible that mm is not
  1230. * set, if so charge the init_mm (happens for pagecache usage).
  1231. */
  1232. mem = *memcg;
  1233. if (likely(!mem)) {
  1234. mem = try_get_mem_cgroup_from_mm(mm);
  1235. *memcg = mem;
  1236. } else {
  1237. css_get(&mem->css);
  1238. }
  1239. if (unlikely(!mem))
  1240. return 0;
  1241. VM_BUG_ON(css_is_removed(&mem->css));
  1242. if (mem_cgroup_is_root(mem))
  1243. goto done;
  1244. while (1) {
  1245. int ret = 0;
  1246. unsigned long flags = 0;
  1247. if (consume_stock(mem))
  1248. goto charged;
  1249. ret = res_counter_charge(&mem->res, csize, &fail_res);
  1250. if (likely(!ret)) {
  1251. if (!do_swap_account)
  1252. break;
  1253. ret = res_counter_charge(&mem->memsw, csize, &fail_res);
  1254. if (likely(!ret))
  1255. break;
  1256. /* mem+swap counter fails */
  1257. res_counter_uncharge(&mem->res, csize);
  1258. flags |= MEM_CGROUP_RECLAIM_NOSWAP;
  1259. mem_over_limit = mem_cgroup_from_res_counter(fail_res,
  1260. memsw);
  1261. } else
  1262. /* mem counter fails */
  1263. mem_over_limit = mem_cgroup_from_res_counter(fail_res,
  1264. res);
  1265. /* reduce request size and retry */
  1266. if (csize > PAGE_SIZE) {
  1267. csize = PAGE_SIZE;
  1268. continue;
  1269. }
  1270. if (!(gfp_mask & __GFP_WAIT))
  1271. goto nomem;
  1272. ret = mem_cgroup_hierarchical_reclaim(mem_over_limit, NULL,
  1273. gfp_mask, flags);
  1274. if (ret)
  1275. continue;
  1276. /*
  1277. * try_to_free_mem_cgroup_pages() might not give us a full
  1278. * picture of reclaim. Some pages are reclaimed and might be
  1279. * moved to swap cache or just unmapped from the cgroup.
  1280. * Check the limit again to see if the reclaim reduced the
  1281. * current usage of the cgroup before giving up
  1282. *
  1283. */
  1284. if (mem_cgroup_check_under_limit(mem_over_limit))
  1285. continue;
  1286. if (!nr_retries--) {
  1287. if (oom) {
  1288. mutex_lock(&memcg_tasklist);
  1289. mem_cgroup_out_of_memory(mem_over_limit, gfp_mask);
  1290. mutex_unlock(&memcg_tasklist);
  1291. record_last_oom(mem_over_limit);
  1292. }
  1293. goto nomem;
  1294. }
  1295. }
  1296. if (csize > PAGE_SIZE)
  1297. refill_stock(mem, csize - PAGE_SIZE);
  1298. charged:
  1299. /*
  1300. * Insert ancestor (and ancestor's ancestors), to softlimit RB-tree.
  1301. * if they exceeds softlimit.
  1302. */
  1303. if (mem_cgroup_soft_limit_check(mem))
  1304. mem_cgroup_update_tree(mem, page);
  1305. done:
  1306. return 0;
  1307. nomem:
  1308. css_put(&mem->css);
  1309. return -ENOMEM;
  1310. }
  1311. /*
  1312. * Somemtimes we have to undo a charge we got by try_charge().
  1313. * This function is for that and do uncharge, put css's refcnt.
  1314. * gotten by try_charge().
  1315. */
  1316. static void mem_cgroup_cancel_charge(struct mem_cgroup *mem)
  1317. {
  1318. if (!mem_cgroup_is_root(mem)) {
  1319. res_counter_uncharge(&mem->res, PAGE_SIZE);
  1320. if (do_swap_account)
  1321. res_counter_uncharge(&mem->memsw, PAGE_SIZE);
  1322. }
  1323. css_put(&mem->css);
  1324. }
  1325. /*
  1326. * A helper function to get mem_cgroup from ID. must be called under
  1327. * rcu_read_lock(). The caller must check css_is_removed() or some if
  1328. * it's concern. (dropping refcnt from swap can be called against removed
  1329. * memcg.)
  1330. */
  1331. static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
  1332. {
  1333. struct cgroup_subsys_state *css;
  1334. /* ID 0 is unused ID */
  1335. if (!id)
  1336. return NULL;
  1337. css = css_lookup(&mem_cgroup_subsys, id);
  1338. if (!css)
  1339. return NULL;
  1340. return container_of(css, struct mem_cgroup, css);
  1341. }
  1342. static struct mem_cgroup *try_get_mem_cgroup_from_swapcache(struct page *page)
  1343. {
  1344. struct mem_cgroup *mem;
  1345. struct page_cgroup *pc;
  1346. unsigned short id;
  1347. swp_entry_t ent;
  1348. VM_BUG_ON(!PageLocked(page));
  1349. if (!PageSwapCache(page))
  1350. return NULL;
  1351. pc = lookup_page_cgroup(page);
  1352. lock_page_cgroup(pc);
  1353. if (PageCgroupUsed(pc)) {
  1354. mem = pc->mem_cgroup;
  1355. if (mem && !css_tryget(&mem->css))
  1356. mem = NULL;
  1357. } else {
  1358. ent.val = page_private(page);
  1359. id = lookup_swap_cgroup(ent);
  1360. rcu_read_lock();
  1361. mem = mem_cgroup_lookup(id);
  1362. if (mem && !css_tryget(&mem->css))
  1363. mem = NULL;
  1364. rcu_read_unlock();
  1365. }
  1366. unlock_page_cgroup(pc);
  1367. return mem;
  1368. }
  1369. /*
  1370. * commit a charge got by __mem_cgroup_try_charge() and makes page_cgroup to be
  1371. * USED state. If already USED, uncharge and return.
  1372. */
  1373. static void __mem_cgroup_commit_charge(struct mem_cgroup *mem,
  1374. struct page_cgroup *pc,
  1375. enum charge_type ctype)
  1376. {
  1377. /* try_charge() can return NULL to *memcg, taking care of it. */
  1378. if (!mem)
  1379. return;
  1380. lock_page_cgroup(pc);
  1381. if (unlikely(PageCgroupUsed(pc))) {
  1382. unlock_page_cgroup(pc);
  1383. mem_cgroup_cancel_charge(mem);
  1384. return;
  1385. }
  1386. pc->mem_cgroup = mem;
  1387. /*
  1388. * We access a page_cgroup asynchronously without lock_page_cgroup().
  1389. * Especially when a page_cgroup is taken from a page, pc->mem_cgroup
  1390. * is accessed after testing USED bit. To make pc->mem_cgroup visible
  1391. * before USED bit, we need memory barrier here.
  1392. * See mem_cgroup_add_lru_list(), etc.
  1393. */
  1394. smp_wmb();
  1395. switch (ctype) {
  1396. case MEM_CGROUP_CHARGE_TYPE_CACHE:
  1397. case MEM_CGROUP_CHARGE_TYPE_SHMEM:
  1398. SetPageCgroupCache(pc);
  1399. SetPageCgroupUsed(pc);
  1400. break;
  1401. case MEM_CGROUP_CHARGE_TYPE_MAPPED:
  1402. ClearPageCgroupCache(pc);
  1403. SetPageCgroupUsed(pc);
  1404. break;
  1405. default:
  1406. break;
  1407. }
  1408. mem_cgroup_charge_statistics(mem, pc, true);
  1409. unlock_page_cgroup(pc);
  1410. }
  1411. /**
  1412. * mem_cgroup_move_account - move account of the page
  1413. * @pc: page_cgroup of the page.
  1414. * @from: mem_cgroup which the page is moved from.
  1415. * @to: mem_cgroup which the page is moved to. @from != @to.
  1416. *
  1417. * The caller must confirm following.
  1418. * - page is not on LRU (isolate_page() is useful.)
  1419. *
  1420. * returns 0 at success,
  1421. * returns -EBUSY when lock is busy or "pc" is unstable.
  1422. *
  1423. * This function does "uncharge" from old cgroup but doesn't do "charge" to
  1424. * new cgroup. It should be done by a caller.
  1425. */
  1426. static int mem_cgroup_move_account(struct page_cgroup *pc,
  1427. struct mem_cgroup *from, struct mem_cgroup *to)
  1428. {
  1429. struct mem_cgroup_per_zone *from_mz, *to_mz;
  1430. int nid, zid;
  1431. int ret = -EBUSY;
  1432. struct page *page;
  1433. int cpu;
  1434. struct mem_cgroup_stat *stat;
  1435. struct mem_cgroup_stat_cpu *cpustat;
  1436. VM_BUG_ON(from == to);
  1437. VM_BUG_ON(PageLRU(pc->page));
  1438. nid = page_cgroup_nid(pc);
  1439. zid = page_cgroup_zid(pc);
  1440. from_mz = mem_cgroup_zoneinfo(from, nid, zid);
  1441. to_mz = mem_cgroup_zoneinfo(to, nid, zid);
  1442. if (!trylock_page_cgroup(pc))
  1443. return ret;
  1444. if (!PageCgroupUsed(pc))
  1445. goto out;
  1446. if (pc->mem_cgroup != from)
  1447. goto out;
  1448. if (!mem_cgroup_is_root(from))
  1449. res_counter_uncharge(&from->res, PAGE_SIZE);
  1450. mem_cgroup_charge_statistics(from, pc, false);
  1451. page = pc->page;
  1452. if (page_mapped(page) && !PageAnon(page)) {
  1453. cpu = smp_processor_id();
  1454. /* Update mapped_file data for mem_cgroup "from" */
  1455. stat = &from->stat;
  1456. cpustat = &stat->cpustat[cpu];
  1457. __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_FILE_MAPPED,
  1458. -1);
  1459. /* Update mapped_file data for mem_cgroup "to" */
  1460. stat = &to->stat;
  1461. cpustat = &stat->cpustat[cpu];
  1462. __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_FILE_MAPPED,
  1463. 1);
  1464. }
  1465. if (do_swap_account && !mem_cgroup_is_root(from))
  1466. res_counter_uncharge(&from->memsw, PAGE_SIZE);
  1467. css_put(&from->css);
  1468. css_get(&to->css);
  1469. pc->mem_cgroup = to;
  1470. mem_cgroup_charge_statistics(to, pc, true);
  1471. ret = 0;
  1472. out:
  1473. unlock_page_cgroup(pc);
  1474. /*
  1475. * We charges against "to" which may not have any tasks. Then, "to"
  1476. * can be under rmdir(). But in current implementation, caller of
  1477. * this function is just force_empty() and it's garanteed that
  1478. * "to" is never removed. So, we don't check rmdir status here.
  1479. */
  1480. return ret;
  1481. }
  1482. /*
  1483. * move charges to its parent.
  1484. */
  1485. static int mem_cgroup_move_parent(struct page_cgroup *pc,
  1486. struct mem_cgroup *child,
  1487. gfp_t gfp_mask)
  1488. {
  1489. struct page *page = pc->page;
  1490. struct cgroup *cg = child->css.cgroup;
  1491. struct cgroup *pcg = cg->parent;
  1492. struct mem_cgroup *parent;
  1493. int ret;
  1494. /* Is ROOT ? */
  1495. if (!pcg)
  1496. return -EINVAL;
  1497. parent = mem_cgroup_from_cont(pcg);
  1498. ret = __mem_cgroup_try_charge(NULL, gfp_mask, &parent, false, page);
  1499. if (ret || !parent)
  1500. return ret;
  1501. if (!get_page_unless_zero(page)) {
  1502. ret = -EBUSY;
  1503. goto uncharge;
  1504. }
  1505. ret = isolate_lru_page(page);
  1506. if (ret)
  1507. goto cancel;
  1508. ret = mem_cgroup_move_account(pc, child, parent);
  1509. putback_lru_page(page);
  1510. if (!ret) {
  1511. put_page(page);
  1512. /* drop extra refcnt by try_charge() */
  1513. css_put(&parent->css);
  1514. return 0;
  1515. }
  1516. cancel:
  1517. put_page(page);
  1518. uncharge:
  1519. mem_cgroup_cancel_charge(parent);
  1520. return ret;
  1521. }
  1522. /*
  1523. * Charge the memory controller for page usage.
  1524. * Return
  1525. * 0 if the charge was successful
  1526. * < 0 if the cgroup is over its limit
  1527. */
  1528. static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
  1529. gfp_t gfp_mask, enum charge_type ctype,
  1530. struct mem_cgroup *memcg)
  1531. {
  1532. struct mem_cgroup *mem;
  1533. struct page_cgroup *pc;
  1534. int ret;
  1535. pc = lookup_page_cgroup(page);
  1536. /* can happen at boot */
  1537. if (unlikely(!pc))
  1538. return 0;
  1539. prefetchw(pc);
  1540. mem = memcg;
  1541. ret = __mem_cgroup_try_charge(mm, gfp_mask, &mem, true, page);
  1542. if (ret || !mem)
  1543. return ret;
  1544. __mem_cgroup_commit_charge(mem, pc, ctype);
  1545. return 0;
  1546. }
  1547. int mem_cgroup_newpage_charge(struct page *page,
  1548. struct mm_struct *mm, gfp_t gfp_mask)
  1549. {
  1550. if (mem_cgroup_disabled())
  1551. return 0;
  1552. if (PageCompound(page))
  1553. return 0;
  1554. /*
  1555. * If already mapped, we don't have to account.
  1556. * If page cache, page->mapping has address_space.
  1557. * But page->mapping may have out-of-use anon_vma pointer,
  1558. * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
  1559. * is NULL.
  1560. */
  1561. if (page_mapped(page) || (page->mapping && !PageAnon(page)))
  1562. return 0;
  1563. if (unlikely(!mm))
  1564. mm = &init_mm;
  1565. return mem_cgroup_charge_common(page, mm, gfp_mask,
  1566. MEM_CGROUP_CHARGE_TYPE_MAPPED, NULL);
  1567. }
  1568. static void
  1569. __mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
  1570. enum charge_type ctype);
  1571. int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
  1572. gfp_t gfp_mask)
  1573. {
  1574. struct mem_cgroup *mem = NULL;
  1575. int ret;
  1576. if (mem_cgroup_disabled())
  1577. return 0;
  1578. if (PageCompound(page))
  1579. return 0;
  1580. /*
  1581. * Corner case handling. This is called from add_to_page_cache()
  1582. * in usual. But some FS (shmem) precharges this page before calling it
  1583. * and call add_to_page_cache() with GFP_NOWAIT.
  1584. *
  1585. * For GFP_NOWAIT case, the page may be pre-charged before calling
  1586. * add_to_page_cache(). (See shmem.c) check it here and avoid to call
  1587. * charge twice. (It works but has to pay a bit larger cost.)
  1588. * And when the page is SwapCache, it should take swap information
  1589. * into account. This is under lock_page() now.
  1590. */
  1591. if (!(gfp_mask & __GFP_WAIT)) {
  1592. struct page_cgroup *pc;
  1593. pc = lookup_page_cgroup(page);
  1594. if (!pc)
  1595. return 0;
  1596. lock_page_cgroup(pc);
  1597. if (PageCgroupUsed(pc)) {
  1598. unlock_page_cgroup(pc);
  1599. return 0;
  1600. }
  1601. unlock_page_cgroup(pc);
  1602. }
  1603. if (unlikely(!mm && !mem))
  1604. mm = &init_mm;
  1605. if (page_is_file_cache(page))
  1606. return mem_cgroup_charge_common(page, mm, gfp_mask,
  1607. MEM_CGROUP_CHARGE_TYPE_CACHE, NULL);
  1608. /* shmem */
  1609. if (PageSwapCache(page)) {
  1610. ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
  1611. if (!ret)
  1612. __mem_cgroup_commit_charge_swapin(page, mem,
  1613. MEM_CGROUP_CHARGE_TYPE_SHMEM);
  1614. } else
  1615. ret = mem_cgroup_charge_common(page, mm, gfp_mask,
  1616. MEM_CGROUP_CHARGE_TYPE_SHMEM, mem);
  1617. return ret;
  1618. }
  1619. /*
  1620. * While swap-in, try_charge -> commit or cancel, the page is locked.
  1621. * And when try_charge() successfully returns, one refcnt to memcg without
  1622. * struct page_cgroup is acquired. This refcnt will be consumed by
  1623. * "commit()" or removed by "cancel()"
  1624. */
  1625. int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
  1626. struct page *page,
  1627. gfp_t mask, struct mem_cgroup **ptr)
  1628. {
  1629. struct mem_cgroup *mem;
  1630. int ret;
  1631. if (mem_cgroup_disabled())
  1632. return 0;
  1633. if (!do_swap_account)
  1634. goto charge_cur_mm;
  1635. /*
  1636. * A racing thread's fault, or swapoff, may have already updated
  1637. * the pte, and even removed page from swap cache: in those cases
  1638. * do_swap_page()'s pte_same() test will fail; but there's also a
  1639. * KSM case which does need to charge the page.
  1640. */
  1641. if (!PageSwapCache(page))
  1642. goto charge_cur_mm;
  1643. mem = try_get_mem_cgroup_from_swapcache(page);
  1644. if (!mem)
  1645. goto charge_cur_mm;
  1646. *ptr = mem;
  1647. ret = __mem_cgroup_try_charge(NULL, mask, ptr, true, page);
  1648. /* drop extra refcnt from tryget */
  1649. css_put(&mem->css);
  1650. return ret;
  1651. charge_cur_mm:
  1652. if (unlikely(!mm))
  1653. mm = &init_mm;
  1654. return __mem_cgroup_try_charge(mm, mask, ptr, true, page);
  1655. }
  1656. static void
  1657. __mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
  1658. enum charge_type ctype)
  1659. {
  1660. struct page_cgroup *pc;
  1661. if (mem_cgroup_disabled())
  1662. return;
  1663. if (!ptr)
  1664. return;
  1665. cgroup_exclude_rmdir(&ptr->css);
  1666. pc = lookup_page_cgroup(page);
  1667. mem_cgroup_lru_del_before_commit_swapcache(page);
  1668. __mem_cgroup_commit_charge(ptr, pc, ctype);
  1669. mem_cgroup_lru_add_after_commit_swapcache(page);
  1670. /*
  1671. * Now swap is on-memory. This means this page may be
  1672. * counted both as mem and swap....double count.
  1673. * Fix it by uncharging from memsw. Basically, this SwapCache is stable
  1674. * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
  1675. * may call delete_from_swap_cache() before reach here.
  1676. */
  1677. if (do_swap_account && PageSwapCache(page)) {
  1678. swp_entry_t ent = {.val = page_private(page)};
  1679. unsigned short id;
  1680. struct mem_cgroup *memcg;
  1681. id = swap_cgroup_record(ent, 0);
  1682. rcu_read_lock();
  1683. memcg = mem_cgroup_lookup(id);
  1684. if (memcg) {
  1685. /*
  1686. * This recorded memcg can be obsolete one. So, avoid
  1687. * calling css_tryget
  1688. */
  1689. if (!mem_cgroup_is_root(memcg))
  1690. res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
  1691. mem_cgroup_swap_statistics(memcg, false);
  1692. mem_cgroup_put(memcg);
  1693. }
  1694. rcu_read_unlock();
  1695. }
  1696. /*
  1697. * At swapin, we may charge account against cgroup which has no tasks.
  1698. * So, rmdir()->pre_destroy() can be called while we do this charge.
  1699. * In that case, we need to call pre_destroy() again. check it here.
  1700. */
  1701. cgroup_release_and_wakeup_rmdir(&ptr->css);
  1702. }
  1703. void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr)
  1704. {
  1705. __mem_cgroup_commit_charge_swapin(page, ptr,
  1706. MEM_CGROUP_CHARGE_TYPE_MAPPED);
  1707. }
  1708. void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem)
  1709. {
  1710. if (mem_cgroup_disabled())
  1711. return;
  1712. if (!mem)
  1713. return;
  1714. mem_cgroup_cancel_charge(mem);
  1715. }
  1716. static void
  1717. __do_uncharge(struct mem_cgroup *mem, const enum charge_type ctype)
  1718. {
  1719. struct memcg_batch_info *batch = NULL;
  1720. bool uncharge_memsw = true;
  1721. /* If swapout, usage of swap doesn't decrease */
  1722. if (!do_swap_account || ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
  1723. uncharge_memsw = false;
  1724. /*
  1725. * do_batch > 0 when unmapping pages or inode invalidate/truncate.
  1726. * In those cases, all pages freed continously can be expected to be in
  1727. * the same cgroup and we have chance to coalesce uncharges.
  1728. * But we do uncharge one by one if this is killed by OOM(TIF_MEMDIE)
  1729. * because we want to do uncharge as soon as possible.
  1730. */
  1731. if (!current->memcg_batch.do_batch || test_thread_flag(TIF_MEMDIE))
  1732. goto direct_uncharge;
  1733. batch = &current->memcg_batch;
  1734. /*
  1735. * In usual, we do css_get() when we remember memcg pointer.
  1736. * But in this case, we keep res->usage until end of a series of
  1737. * uncharges. Then, it's ok to ignore memcg's refcnt.
  1738. */
  1739. if (!batch->memcg)
  1740. batch->memcg = mem;
  1741. /*
  1742. * In typical case, batch->memcg == mem. This means we can
  1743. * merge a series of uncharges to an uncharge of res_counter.
  1744. * If not, we uncharge res_counter ony by one.
  1745. */
  1746. if (batch->memcg != mem)
  1747. goto direct_uncharge;
  1748. /* remember freed charge and uncharge it later */
  1749. batch->bytes += PAGE_SIZE;
  1750. if (uncharge_memsw)
  1751. batch->memsw_bytes += PAGE_SIZE;
  1752. return;
  1753. direct_uncharge:
  1754. res_counter_uncharge(&mem->res, PAGE_SIZE);
  1755. if (uncharge_memsw)
  1756. res_counter_uncharge(&mem->memsw, PAGE_SIZE);
  1757. return;
  1758. }
  1759. /*
  1760. * uncharge if !page_mapped(page)
  1761. */
  1762. static struct mem_cgroup *
  1763. __mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
  1764. {
  1765. struct page_cgroup *pc;
  1766. struct mem_cgroup *mem = NULL;
  1767. struct mem_cgroup_per_zone *mz;
  1768. if (mem_cgroup_disabled())
  1769. return NULL;
  1770. if (PageSwapCache(page))
  1771. return NULL;
  1772. /*
  1773. * Check if our page_cgroup is valid
  1774. */
  1775. pc = lookup_page_cgroup(page);
  1776. if (unlikely(!pc || !PageCgroupUsed(pc)))
  1777. return NULL;
  1778. lock_page_cgroup(pc);
  1779. mem = pc->mem_cgroup;
  1780. if (!PageCgroupUsed(pc))
  1781. goto unlock_out;
  1782. switch (ctype) {
  1783. case MEM_CGROUP_CHARGE_TYPE_MAPPED:
  1784. case MEM_CGROUP_CHARGE_TYPE_DROP:
  1785. if (page_mapped(page))
  1786. goto unlock_out;
  1787. break;
  1788. case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
  1789. if (!PageAnon(page)) { /* Shared memory */
  1790. if (page->mapping && !page_is_file_cache(page))
  1791. goto unlock_out;
  1792. } else if (page_mapped(page)) /* Anon */
  1793. goto unlock_out;
  1794. break;
  1795. default:
  1796. break;
  1797. }
  1798. if (!mem_cgroup_is_root(mem))
  1799. __do_uncharge(mem, ctype);
  1800. if (ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
  1801. mem_cgroup_swap_statistics(mem, true);
  1802. mem_cgroup_charge_statistics(mem, pc, false);
  1803. ClearPageCgroupUsed(pc);
  1804. /*
  1805. * pc->mem_cgroup is not cleared here. It will be accessed when it's
  1806. * freed from LRU. This is safe because uncharged page is expected not
  1807. * to be reused (freed soon). Exception is SwapCache, it's handled by
  1808. * special functions.
  1809. */
  1810. mz = page_cgroup_zoneinfo(pc);
  1811. unlock_page_cgroup(pc);
  1812. if (mem_cgroup_soft_limit_check(mem))
  1813. mem_cgroup_update_tree(mem, page);
  1814. /* at swapout, this memcg will be accessed to record to swap */
  1815. if (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
  1816. css_put(&mem->css);
  1817. return mem;
  1818. unlock_out:
  1819. unlock_page_cgroup(pc);
  1820. return NULL;
  1821. }
  1822. void mem_cgroup_uncharge_page(struct page *page)
  1823. {
  1824. /* early check. */
  1825. if (page_mapped(page))
  1826. return;
  1827. if (page->mapping && !PageAnon(page))
  1828. return;
  1829. __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
  1830. }
  1831. void mem_cgroup_uncharge_cache_page(struct page *page)
  1832. {
  1833. VM_BUG_ON(page_mapped(page));
  1834. VM_BUG_ON(page->mapping);
  1835. __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
  1836. }
  1837. /*
  1838. * Batch_start/batch_end is called in unmap_page_range/invlidate/trucate.
  1839. * In that cases, pages are freed continuously and we can expect pages
  1840. * are in the same memcg. All these calls itself limits the number of
  1841. * pages freed at once, then uncharge_start/end() is called properly.
  1842. * This may be called prural(2) times in a context,
  1843. */
  1844. void mem_cgroup_uncharge_start(void)
  1845. {
  1846. current->memcg_batch.do_batch++;
  1847. /* We can do nest. */
  1848. if (current->memcg_batch.do_batch == 1) {
  1849. current->memcg_batch.memcg = NULL;
  1850. current->memcg_batch.bytes = 0;
  1851. current->memcg_batch.memsw_bytes = 0;
  1852. }
  1853. }
  1854. void mem_cgroup_uncharge_end(void)
  1855. {
  1856. struct memcg_batch_info *batch = &current->memcg_batch;
  1857. if (!batch->do_batch)
  1858. return;
  1859. batch->do_batch--;
  1860. if (batch->do_batch) /* If stacked, do nothing. */
  1861. return;
  1862. if (!batch->memcg)
  1863. return;
  1864. /*
  1865. * This "batch->memcg" is valid without any css_get/put etc...
  1866. * bacause we hide charges behind us.
  1867. */
  1868. if (batch->bytes)
  1869. res_counter_uncharge(&batch->memcg->res, batch->bytes);
  1870. if (batch->memsw_bytes)
  1871. res_counter_uncharge(&batch->memcg->memsw, batch->memsw_bytes);
  1872. /* forget this pointer (for sanity check) */
  1873. batch->memcg = NULL;
  1874. }
  1875. #ifdef CONFIG_SWAP
  1876. /*
  1877. * called after __delete_from_swap_cache() and drop "page" account.
  1878. * memcg information is recorded to swap_cgroup of "ent"
  1879. */
  1880. void
  1881. mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
  1882. {
  1883. struct mem_cgroup *memcg;
  1884. int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT;
  1885. if (!swapout) /* this was a swap cache but the swap is unused ! */
  1886. ctype = MEM_CGROUP_CHARGE_TYPE_DROP;
  1887. memcg = __mem_cgroup_uncharge_common(page, ctype);
  1888. /* record memcg information */
  1889. if (do_swap_account && swapout && memcg) {
  1890. swap_cgroup_record(ent, css_id(&memcg->css));
  1891. mem_cgroup_get(memcg);
  1892. }
  1893. if (swapout && memcg)
  1894. css_put(&memcg->css);
  1895. }
  1896. #endif
  1897. #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
  1898. /*
  1899. * called from swap_entry_free(). remove record in swap_cgroup and
  1900. * uncharge "memsw" account.
  1901. */
  1902. void mem_cgroup_uncharge_swap(swp_entry_t ent)
  1903. {
  1904. struct mem_cgroup *memcg;
  1905. unsigned short id;
  1906. if (!do_swap_account)
  1907. return;
  1908. id = swap_cgroup_record(ent, 0);
  1909. rcu_read_lock();
  1910. memcg = mem_cgroup_lookup(id);
  1911. if (memcg) {
  1912. /*
  1913. * We uncharge this because swap is freed.
  1914. * This memcg can be obsolete one. We avoid calling css_tryget
  1915. */
  1916. if (!mem_cgroup_is_root(memcg))
  1917. res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
  1918. mem_cgroup_swap_statistics(memcg, false);
  1919. mem_cgroup_put(memcg);
  1920. }
  1921. rcu_read_unlock();
  1922. }
  1923. #endif
  1924. /*
  1925. * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
  1926. * page belongs to.
  1927. */
  1928. int mem_cgroup_prepare_migration(struct page *page, struct mem_cgroup **ptr)
  1929. {
  1930. struct page_cgroup *pc;
  1931. struct mem_cgroup *mem = NULL;
  1932. int ret = 0;
  1933. if (mem_cgroup_disabled())
  1934. return 0;
  1935. pc = lookup_page_cgroup(page);
  1936. lock_page_cgroup(pc);
  1937. if (PageCgroupUsed(pc)) {
  1938. mem = pc->mem_cgroup;
  1939. css_get(&mem->css);
  1940. }
  1941. unlock_page_cgroup(pc);
  1942. if (mem) {
  1943. ret = __mem_cgroup_try_charge(NULL, GFP_KERNEL, &mem, false,
  1944. page);
  1945. css_put(&mem->css);
  1946. }
  1947. *ptr = mem;
  1948. return ret;
  1949. }
  1950. /* remove redundant charge if migration failed*/
  1951. void mem_cgroup_end_migration(struct mem_cgroup *mem,
  1952. struct page *oldpage, struct page *newpage)
  1953. {
  1954. struct page *target, *unused;
  1955. struct page_cgroup *pc;
  1956. enum charge_type ctype;
  1957. if (!mem)
  1958. return;
  1959. cgroup_exclude_rmdir(&mem->css);
  1960. /* at migration success, oldpage->mapping is NULL. */
  1961. if (oldpage->mapping) {
  1962. target = oldpage;
  1963. unused = NULL;
  1964. } else {
  1965. target = newpage;
  1966. unused = oldpage;
  1967. }
  1968. if (PageAnon(target))
  1969. ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
  1970. else if (page_is_file_cache(target))
  1971. ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
  1972. else
  1973. ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
  1974. /* unused page is not on radix-tree now. */
  1975. if (unused)
  1976. __mem_cgroup_uncharge_common(unused, ctype);
  1977. pc = lookup_page_cgroup(target);
  1978. /*
  1979. * __mem_cgroup_commit_charge() check PCG_USED bit of page_cgroup.
  1980. * So, double-counting is effectively avoided.
  1981. */
  1982. __mem_cgroup_commit_charge(mem, pc, ctype);
  1983. /*
  1984. * Both of oldpage and newpage are still under lock_page().
  1985. * Then, we don't have to care about race in radix-tree.
  1986. * But we have to be careful that this page is unmapped or not.
  1987. *
  1988. * There is a case for !page_mapped(). At the start of
  1989. * migration, oldpage was mapped. But now, it's zapped.
  1990. * But we know *target* page is not freed/reused under us.
  1991. * mem_cgroup_uncharge_page() does all necessary checks.
  1992. */
  1993. if (ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED)
  1994. mem_cgroup_uncharge_page(target);
  1995. /*
  1996. * At migration, we may charge account against cgroup which has no tasks
  1997. * So, rmdir()->pre_destroy() can be called while we do this charge.
  1998. * In that case, we need to call pre_destroy() again. check it here.
  1999. */
  2000. cgroup_release_and_wakeup_rmdir(&mem->css);
  2001. }
  2002. /*
  2003. * A call to try to shrink memory usage on charge failure at shmem's swapin.
  2004. * Calling hierarchical_reclaim is not enough because we should update
  2005. * last_oom_jiffies to prevent pagefault_out_of_memory from invoking global OOM.
  2006. * Moreover considering hierarchy, we should reclaim from the mem_over_limit,
  2007. * not from the memcg which this page would be charged to.
  2008. * try_charge_swapin does all of these works properly.
  2009. */
  2010. int mem_cgroup_shmem_charge_fallback(struct page *page,
  2011. struct mm_struct *mm,
  2012. gfp_t gfp_mask)
  2013. {
  2014. struct mem_cgroup *mem = NULL;
  2015. int ret;
  2016. if (mem_cgroup_disabled())
  2017. return 0;
  2018. ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
  2019. if (!ret)
  2020. mem_cgroup_cancel_charge_swapin(mem); /* it does !mem check */
  2021. return ret;
  2022. }
  2023. static DEFINE_MUTEX(set_limit_mutex);
  2024. static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
  2025. unsigned long long val)
  2026. {
  2027. int retry_count;
  2028. int progress;
  2029. u64 memswlimit;
  2030. int ret = 0;
  2031. int children = mem_cgroup_count_children(memcg);
  2032. u64 curusage, oldusage;
  2033. /*
  2034. * For keeping hierarchical_reclaim simple, how long we should retry
  2035. * is depends on callers. We set our retry-count to be function
  2036. * of # of children which we should visit in this loop.
  2037. */
  2038. retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
  2039. oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
  2040. while (retry_count) {
  2041. if (signal_pending(current)) {
  2042. ret = -EINTR;
  2043. break;
  2044. }
  2045. /*
  2046. * Rather than hide all in some function, I do this in
  2047. * open coded manner. You see what this really does.
  2048. * We have to guarantee mem->res.limit < mem->memsw.limit.
  2049. */
  2050. mutex_lock(&set_limit_mutex);
  2051. memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
  2052. if (memswlimit < val) {
  2053. ret = -EINVAL;
  2054. mutex_unlock(&set_limit_mutex);
  2055. break;
  2056. }
  2057. ret = res_counter_set_limit(&memcg->res, val);
  2058. if (!ret) {
  2059. if (memswlimit == val)
  2060. memcg->memsw_is_minimum = true;
  2061. else
  2062. memcg->memsw_is_minimum = false;
  2063. }
  2064. mutex_unlock(&set_limit_mutex);
  2065. if (!ret)
  2066. break;
  2067. progress = mem_cgroup_hierarchical_reclaim(memcg, NULL,
  2068. GFP_KERNEL,
  2069. MEM_CGROUP_RECLAIM_SHRINK);
  2070. curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
  2071. /* Usage is reduced ? */
  2072. if (curusage >= oldusage)
  2073. retry_count--;
  2074. else
  2075. oldusage = curusage;
  2076. }
  2077. return ret;
  2078. }
  2079. static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
  2080. unsigned long long val)
  2081. {
  2082. int retry_count;
  2083. u64 memlimit, oldusage, curusage;
  2084. int children = mem_cgroup_count_children(memcg);
  2085. int ret = -EBUSY;
  2086. /* see mem_cgroup_resize_res_limit */
  2087. retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
  2088. oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
  2089. while (retry_count) {
  2090. if (signal_pending(current)) {
  2091. ret = -EINTR;
  2092. break;
  2093. }
  2094. /*
  2095. * Rather than hide all in some function, I do this in
  2096. * open coded manner. You see what this really does.
  2097. * We have to guarantee mem->res.limit < mem->memsw.limit.
  2098. */
  2099. mutex_lock(&set_limit_mutex);
  2100. memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
  2101. if (memlimit > val) {
  2102. ret = -EINVAL;
  2103. mutex_unlock(&set_limit_mutex);
  2104. break;
  2105. }
  2106. ret = res_counter_set_limit(&memcg->memsw, val);
  2107. if (!ret) {
  2108. if (memlimit == val)
  2109. memcg->memsw_is_minimum = true;
  2110. else
  2111. memcg->memsw_is_minimum = false;
  2112. }
  2113. mutex_unlock(&set_limit_mutex);
  2114. if (!ret)
  2115. break;
  2116. mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL,
  2117. MEM_CGROUP_RECLAIM_NOSWAP |
  2118. MEM_CGROUP_RECLAIM_SHRINK);
  2119. curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
  2120. /* Usage is reduced ? */
  2121. if (curusage >= oldusage)
  2122. retry_count--;
  2123. else
  2124. oldusage = curusage;
  2125. }
  2126. return ret;
  2127. }
  2128. unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
  2129. gfp_t gfp_mask, int nid,
  2130. int zid)
  2131. {
  2132. unsigned long nr_reclaimed = 0;
  2133. struct mem_cgroup_per_zone *mz, *next_mz = NULL;
  2134. unsigned long reclaimed;
  2135. int loop = 0;
  2136. struct mem_cgroup_tree_per_zone *mctz;
  2137. unsigned long long excess;
  2138. if (order > 0)
  2139. return 0;
  2140. mctz = soft_limit_tree_node_zone(nid, zid);
  2141. /*
  2142. * This loop can run a while, specially if mem_cgroup's continuously
  2143. * keep exceeding their soft limit and putting the system under
  2144. * pressure
  2145. */
  2146. do {
  2147. if (next_mz)
  2148. mz = next_mz;
  2149. else
  2150. mz = mem_cgroup_largest_soft_limit_node(mctz);
  2151. if (!mz)
  2152. break;
  2153. reclaimed = mem_cgroup_hierarchical_reclaim(mz->mem, zone,
  2154. gfp_mask,
  2155. MEM_CGROUP_RECLAIM_SOFT);
  2156. nr_reclaimed += reclaimed;
  2157. spin_lock(&mctz->lock);
  2158. /*
  2159. * If we failed to reclaim anything from this memory cgroup
  2160. * it is time to move on to the next cgroup
  2161. */
  2162. next_mz = NULL;
  2163. if (!reclaimed) {
  2164. do {
  2165. /*
  2166. * Loop until we find yet another one.
  2167. *
  2168. * By the time we get the soft_limit lock
  2169. * again, someone might have aded the
  2170. * group back on the RB tree. Iterate to
  2171. * make sure we get a different mem.
  2172. * mem_cgroup_largest_soft_limit_node returns
  2173. * NULL if no other cgroup is present on
  2174. * the tree
  2175. */
  2176. next_mz =
  2177. __mem_cgroup_largest_soft_limit_node(mctz);
  2178. if (next_mz == mz) {
  2179. css_put(&next_mz->mem->css);
  2180. next_mz = NULL;
  2181. } else /* next_mz == NULL or other memcg */
  2182. break;
  2183. } while (1);
  2184. }
  2185. __mem_cgroup_remove_exceeded(mz->mem, mz, mctz);
  2186. excess = res_counter_soft_limit_excess(&mz->mem->res);
  2187. /*
  2188. * One school of thought says that we should not add
  2189. * back the node to the tree if reclaim returns 0.
  2190. * But our reclaim could return 0, simply because due
  2191. * to priority we are exposing a smaller subset of
  2192. * memory to reclaim from. Consider this as a longer
  2193. * term TODO.
  2194. */
  2195. /* If excess == 0, no tree ops */
  2196. __mem_cgroup_insert_exceeded(mz->mem, mz, mctz, excess);
  2197. spin_unlock(&mctz->lock);
  2198. css_put(&mz->mem->css);
  2199. loop++;
  2200. /*
  2201. * Could not reclaim anything and there are no more
  2202. * mem cgroups to try or we seem to be looping without
  2203. * reclaiming anything.
  2204. */
  2205. if (!nr_reclaimed &&
  2206. (next_mz == NULL ||
  2207. loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS))
  2208. break;
  2209. } while (!nr_reclaimed);
  2210. if (next_mz)
  2211. css_put(&next_mz->mem->css);
  2212. return nr_reclaimed;
  2213. }
  2214. /*
  2215. * This routine traverse page_cgroup in given list and drop them all.
  2216. * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
  2217. */
  2218. static int mem_cgroup_force_empty_list(struct mem_cgroup *mem,
  2219. int node, int zid, enum lru_list lru)
  2220. {
  2221. struct zone *zone;
  2222. struct mem_cgroup_per_zone *mz;
  2223. struct page_cgroup *pc, *busy;
  2224. unsigned long flags, loop;
  2225. struct list_head *list;
  2226. int ret = 0;
  2227. zone = &NODE_DATA(node)->node_zones[zid];
  2228. mz = mem_cgroup_zoneinfo(mem, node, zid);
  2229. list = &mz->lists[lru];
  2230. loop = MEM_CGROUP_ZSTAT(mz, lru);
  2231. /* give some margin against EBUSY etc...*/
  2232. loop += 256;
  2233. busy = NULL;
  2234. while (loop--) {
  2235. ret = 0;
  2236. spin_lock_irqsave(&zone->lru_lock, flags);
  2237. if (list_empty(list)) {
  2238. spin_unlock_irqrestore(&zone->lru_lock, flags);
  2239. break;
  2240. }
  2241. pc = list_entry(list->prev, struct page_cgroup, lru);
  2242. if (busy == pc) {
  2243. list_move(&pc->lru, list);
  2244. busy = 0;
  2245. spin_unlock_irqrestore(&zone->lru_lock, flags);
  2246. continue;
  2247. }
  2248. spin_unlock_irqrestore(&zone->lru_lock, flags);
  2249. ret = mem_cgroup_move_parent(pc, mem, GFP_KERNEL);
  2250. if (ret == -ENOMEM)
  2251. break;
  2252. if (ret == -EBUSY || ret == -EINVAL) {
  2253. /* found lock contention or "pc" is obsolete. */
  2254. busy = pc;
  2255. cond_resched();
  2256. } else
  2257. busy = NULL;
  2258. }
  2259. if (!ret && !list_empty(list))
  2260. return -EBUSY;
  2261. return ret;
  2262. }
  2263. /*
  2264. * make mem_cgroup's charge to be 0 if there is no task.
  2265. * This enables deleting this mem_cgroup.
  2266. */
  2267. static int mem_cgroup_force_empty(struct mem_cgroup *mem, bool free_all)
  2268. {
  2269. int ret;
  2270. int node, zid, shrink;
  2271. int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
  2272. struct cgroup *cgrp = mem->css.cgroup;
  2273. css_get(&mem->css);
  2274. shrink = 0;
  2275. /* should free all ? */
  2276. if (free_all)
  2277. goto try_to_free;
  2278. move_account:
  2279. while (mem->res.usage > 0) {
  2280. ret = -EBUSY;
  2281. if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
  2282. goto out;
  2283. ret = -EINTR;
  2284. if (signal_pending(current))
  2285. goto out;
  2286. /* This is for making all *used* pages to be on LRU. */
  2287. lru_add_drain_all();
  2288. drain_all_stock_sync();
  2289. ret = 0;
  2290. for_each_node_state(node, N_HIGH_MEMORY) {
  2291. for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) {
  2292. enum lru_list l;
  2293. for_each_lru(l) {
  2294. ret = mem_cgroup_force_empty_list(mem,
  2295. node, zid, l);
  2296. if (ret)
  2297. break;
  2298. }
  2299. }
  2300. if (ret)
  2301. break;
  2302. }
  2303. /* it seems parent cgroup doesn't have enough mem */
  2304. if (ret == -ENOMEM)
  2305. goto try_to_free;
  2306. cond_resched();
  2307. }
  2308. ret = 0;
  2309. out:
  2310. css_put(&mem->css);
  2311. return ret;
  2312. try_to_free:
  2313. /* returns EBUSY if there is a task or if we come here twice. */
  2314. if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) {
  2315. ret = -EBUSY;
  2316. goto out;
  2317. }
  2318. /* we call try-to-free pages for make this cgroup empty */
  2319. lru_add_drain_all();
  2320. /* try to free all pages in this cgroup */
  2321. shrink = 1;
  2322. while (nr_retries && mem->res.usage > 0) {
  2323. int progress;
  2324. if (signal_pending(current)) {
  2325. ret = -EINTR;
  2326. goto out;
  2327. }
  2328. progress = try_to_free_mem_cgroup_pages(mem, GFP_KERNEL,
  2329. false, get_swappiness(mem));
  2330. if (!progress) {
  2331. nr_retries--;
  2332. /* maybe some writeback is necessary */
  2333. congestion_wait(BLK_RW_ASYNC, HZ/10);
  2334. }
  2335. }
  2336. lru_add_drain();
  2337. /* try move_account...there may be some *locked* pages. */
  2338. if (mem->res.usage)
  2339. goto move_account;
  2340. ret = 0;
  2341. goto out;
  2342. }
  2343. int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
  2344. {
  2345. return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true);
  2346. }
  2347. static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft)
  2348. {
  2349. return mem_cgroup_from_cont(cont)->use_hierarchy;
  2350. }
  2351. static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft,
  2352. u64 val)
  2353. {
  2354. int retval = 0;
  2355. struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
  2356. struct cgroup *parent = cont->parent;
  2357. struct mem_cgroup *parent_mem = NULL;
  2358. if (parent)
  2359. parent_mem = mem_cgroup_from_cont(parent);
  2360. cgroup_lock();
  2361. /*
  2362. * If parent's use_hierarchy is set, we can't make any modifications
  2363. * in the child subtrees. If it is unset, then the change can
  2364. * occur, provided the current cgroup has no children.
  2365. *
  2366. * For the root cgroup, parent_mem is NULL, we allow value to be
  2367. * set if there are no children.
  2368. */
  2369. if ((!parent_mem || !parent_mem->use_hierarchy) &&
  2370. (val == 1 || val == 0)) {
  2371. if (list_empty(&cont->children))
  2372. mem->use_hierarchy = val;
  2373. else
  2374. retval = -EBUSY;
  2375. } else
  2376. retval = -EINVAL;
  2377. cgroup_unlock();
  2378. return retval;
  2379. }
  2380. struct mem_cgroup_idx_data {
  2381. s64 val;
  2382. enum mem_cgroup_stat_index idx;
  2383. };
  2384. static int
  2385. mem_cgroup_get_idx_stat(struct mem_cgroup *mem, void *data)
  2386. {
  2387. struct mem_cgroup_idx_data *d = data;
  2388. d->val += mem_cgroup_read_stat(&mem->stat, d->idx);
  2389. return 0;
  2390. }
  2391. static void
  2392. mem_cgroup_get_recursive_idx_stat(struct mem_cgroup *mem,
  2393. enum mem_cgroup_stat_index idx, s64 *val)
  2394. {
  2395. struct mem_cgroup_idx_data d;
  2396. d.idx = idx;
  2397. d.val = 0;
  2398. mem_cgroup_walk_tree(mem, &d, mem_cgroup_get_idx_stat);
  2399. *val = d.val;
  2400. }
  2401. static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
  2402. {
  2403. struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
  2404. u64 idx_val, val;
  2405. int type, name;
  2406. type = MEMFILE_TYPE(cft->private);
  2407. name = MEMFILE_ATTR(cft->private);
  2408. switch (type) {
  2409. case _MEM:
  2410. if (name == RES_USAGE && mem_cgroup_is_root(mem)) {
  2411. mem_cgroup_get_recursive_idx_stat(mem,
  2412. MEM_CGROUP_STAT_CACHE, &idx_val);
  2413. val = idx_val;
  2414. mem_cgroup_get_recursive_idx_stat(mem,
  2415. MEM_CGROUP_STAT_RSS, &idx_val);
  2416. val += idx_val;
  2417. val <<= PAGE_SHIFT;
  2418. } else
  2419. val = res_counter_read_u64(&mem->res, name);
  2420. break;
  2421. case _MEMSWAP:
  2422. if (name == RES_USAGE && mem_cgroup_is_root(mem)) {
  2423. mem_cgroup_get_recursive_idx_stat(mem,
  2424. MEM_CGROUP_STAT_CACHE, &idx_val);
  2425. val = idx_val;
  2426. mem_cgroup_get_recursive_idx_stat(mem,
  2427. MEM_CGROUP_STAT_RSS, &idx_val);
  2428. val += idx_val;
  2429. mem_cgroup_get_recursive_idx_stat(mem,
  2430. MEM_CGROUP_STAT_SWAPOUT, &idx_val);
  2431. val += idx_val;
  2432. val <<= PAGE_SHIFT;
  2433. } else
  2434. val = res_counter_read_u64(&mem->memsw, name);
  2435. break;
  2436. default:
  2437. BUG();
  2438. break;
  2439. }
  2440. return val;
  2441. }
  2442. /*
  2443. * The user of this function is...
  2444. * RES_LIMIT.
  2445. */
  2446. static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
  2447. const char *buffer)
  2448. {
  2449. struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
  2450. int type, name;
  2451. unsigned long long val;
  2452. int ret;
  2453. type = MEMFILE_TYPE(cft->private);
  2454. name = MEMFILE_ATTR(cft->private);
  2455. switch (name) {
  2456. case RES_LIMIT:
  2457. if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
  2458. ret = -EINVAL;
  2459. break;
  2460. }
  2461. /* This function does all necessary parse...reuse it */
  2462. ret = res_counter_memparse_write_strategy(buffer, &val);
  2463. if (ret)
  2464. break;
  2465. if (type == _MEM)
  2466. ret = mem_cgroup_resize_limit(memcg, val);
  2467. else
  2468. ret = mem_cgroup_resize_memsw_limit(memcg, val);
  2469. break;
  2470. case RES_SOFT_LIMIT:
  2471. ret = res_counter_memparse_write_strategy(buffer, &val);
  2472. if (ret)
  2473. break;
  2474. /*
  2475. * For memsw, soft limits are hard to implement in terms
  2476. * of semantics, for now, we support soft limits for
  2477. * control without swap
  2478. */
  2479. if (type == _MEM)
  2480. ret = res_counter_set_soft_limit(&memcg->res, val);
  2481. else
  2482. ret = -EINVAL;
  2483. break;
  2484. default:
  2485. ret = -EINVAL; /* should be BUG() ? */
  2486. break;
  2487. }
  2488. return ret;
  2489. }
  2490. static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
  2491. unsigned long long *mem_limit, unsigned long long *memsw_limit)
  2492. {
  2493. struct cgroup *cgroup;
  2494. unsigned long long min_limit, min_memsw_limit, tmp;
  2495. min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
  2496. min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
  2497. cgroup = memcg->css.cgroup;
  2498. if (!memcg->use_hierarchy)
  2499. goto out;
  2500. while (cgroup->parent) {
  2501. cgroup = cgroup->parent;
  2502. memcg = mem_cgroup_from_cont(cgroup);
  2503. if (!memcg->use_hierarchy)
  2504. break;
  2505. tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
  2506. min_limit = min(min_limit, tmp);
  2507. tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
  2508. min_memsw_limit = min(min_memsw_limit, tmp);
  2509. }
  2510. out:
  2511. *mem_limit = min_limit;
  2512. *memsw_limit = min_memsw_limit;
  2513. return;
  2514. }
  2515. static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
  2516. {
  2517. struct mem_cgroup *mem;
  2518. int type, name;
  2519. mem = mem_cgroup_from_cont(cont);
  2520. type = MEMFILE_TYPE(event);
  2521. name = MEMFILE_ATTR(event);
  2522. switch (name) {
  2523. case RES_MAX_USAGE:
  2524. if (type == _MEM)
  2525. res_counter_reset_max(&mem->res);
  2526. else
  2527. res_counter_reset_max(&mem->memsw);
  2528. break;
  2529. case RES_FAILCNT:
  2530. if (type == _MEM)
  2531. res_counter_reset_failcnt(&mem->res);
  2532. else
  2533. res_counter_reset_failcnt(&mem->memsw);
  2534. break;
  2535. }
  2536. return 0;
  2537. }
  2538. /* For read statistics */
  2539. enum {
  2540. MCS_CACHE,
  2541. MCS_RSS,
  2542. MCS_FILE_MAPPED,
  2543. MCS_PGPGIN,
  2544. MCS_PGPGOUT,
  2545. MCS_SWAP,
  2546. MCS_INACTIVE_ANON,
  2547. MCS_ACTIVE_ANON,
  2548. MCS_INACTIVE_FILE,
  2549. MCS_ACTIVE_FILE,
  2550. MCS_UNEVICTABLE,
  2551. NR_MCS_STAT,
  2552. };
  2553. struct mcs_total_stat {
  2554. s64 stat[NR_MCS_STAT];
  2555. };
  2556. struct {
  2557. char *local_name;
  2558. char *total_name;
  2559. } memcg_stat_strings[NR_MCS_STAT] = {
  2560. {"cache", "total_cache"},
  2561. {"rss", "total_rss"},
  2562. {"mapped_file", "total_mapped_file"},
  2563. {"pgpgin", "total_pgpgin"},
  2564. {"pgpgout", "total_pgpgout"},
  2565. {"swap", "total_swap"},
  2566. {"inactive_anon", "total_inactive_anon"},
  2567. {"active_anon", "total_active_anon"},
  2568. {"inactive_file", "total_inactive_file"},
  2569. {"active_file", "total_active_file"},
  2570. {"unevictable", "total_unevictable"}
  2571. };
  2572. static int mem_cgroup_get_local_stat(struct mem_cgroup *mem, void *data)
  2573. {
  2574. struct mcs_total_stat *s = data;
  2575. s64 val;
  2576. /* per cpu stat */
  2577. val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_CACHE);
  2578. s->stat[MCS_CACHE] += val * PAGE_SIZE;
  2579. val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS);
  2580. s->stat[MCS_RSS] += val * PAGE_SIZE;
  2581. val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_FILE_MAPPED);
  2582. s->stat[MCS_FILE_MAPPED] += val * PAGE_SIZE;
  2583. val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_PGPGIN_COUNT);
  2584. s->stat[MCS_PGPGIN] += val;
  2585. val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_PGPGOUT_COUNT);
  2586. s->stat[MCS_PGPGOUT] += val;
  2587. if (do_swap_account) {
  2588. val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_SWAPOUT);
  2589. s->stat[MCS_SWAP] += val * PAGE_SIZE;
  2590. }
  2591. /* per zone stat */
  2592. val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_ANON);
  2593. s->stat[MCS_INACTIVE_ANON] += val * PAGE_SIZE;
  2594. val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_ANON);
  2595. s->stat[MCS_ACTIVE_ANON] += val * PAGE_SIZE;
  2596. val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_FILE);
  2597. s->stat[MCS_INACTIVE_FILE] += val * PAGE_SIZE;
  2598. val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_FILE);
  2599. s->stat[MCS_ACTIVE_FILE] += val * PAGE_SIZE;
  2600. val = mem_cgroup_get_local_zonestat(mem, LRU_UNEVICTABLE);
  2601. s->stat[MCS_UNEVICTABLE] += val * PAGE_SIZE;
  2602. return 0;
  2603. }
  2604. static void
  2605. mem_cgroup_get_total_stat(struct mem_cgroup *mem, struct mcs_total_stat *s)
  2606. {
  2607. mem_cgroup_walk_tree(mem, s, mem_cgroup_get_local_stat);
  2608. }
  2609. static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
  2610. struct cgroup_map_cb *cb)
  2611. {
  2612. struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
  2613. struct mcs_total_stat mystat;
  2614. int i;
  2615. memset(&mystat, 0, sizeof(mystat));
  2616. mem_cgroup_get_local_stat(mem_cont, &mystat);
  2617. for (i = 0; i < NR_MCS_STAT; i++) {
  2618. if (i == MCS_SWAP && !do_swap_account)
  2619. continue;
  2620. cb->fill(cb, memcg_stat_strings[i].local_name, mystat.stat[i]);
  2621. }
  2622. /* Hierarchical information */
  2623. {
  2624. unsigned long long limit, memsw_limit;
  2625. memcg_get_hierarchical_limit(mem_cont, &limit, &memsw_limit);
  2626. cb->fill(cb, "hierarchical_memory_limit", limit);
  2627. if (do_swap_account)
  2628. cb->fill(cb, "hierarchical_memsw_limit", memsw_limit);
  2629. }
  2630. memset(&mystat, 0, sizeof(mystat));
  2631. mem_cgroup_get_total_stat(mem_cont, &mystat);
  2632. for (i = 0; i < NR_MCS_STAT; i++) {
  2633. if (i == MCS_SWAP && !do_swap_account)
  2634. continue;
  2635. cb->fill(cb, memcg_stat_strings[i].total_name, mystat.stat[i]);
  2636. }
  2637. #ifdef CONFIG_DEBUG_VM
  2638. cb->fill(cb, "inactive_ratio", calc_inactive_ratio(mem_cont, NULL));
  2639. {
  2640. int nid, zid;
  2641. struct mem_cgroup_per_zone *mz;
  2642. unsigned long recent_rotated[2] = {0, 0};
  2643. unsigned long recent_scanned[2] = {0, 0};
  2644. for_each_online_node(nid)
  2645. for (zid = 0; zid < MAX_NR_ZONES; zid++) {
  2646. mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
  2647. recent_rotated[0] +=
  2648. mz->reclaim_stat.recent_rotated[0];
  2649. recent_rotated[1] +=
  2650. mz->reclaim_stat.recent_rotated[1];
  2651. recent_scanned[0] +=
  2652. mz->reclaim_stat.recent_scanned[0];
  2653. recent_scanned[1] +=
  2654. mz->reclaim_stat.recent_scanned[1];
  2655. }
  2656. cb->fill(cb, "recent_rotated_anon", recent_rotated[0]);
  2657. cb->fill(cb, "recent_rotated_file", recent_rotated[1]);
  2658. cb->fill(cb, "recent_scanned_anon", recent_scanned[0]);
  2659. cb->fill(cb, "recent_scanned_file", recent_scanned[1]);
  2660. }
  2661. #endif
  2662. return 0;
  2663. }
  2664. static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft)
  2665. {
  2666. struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
  2667. return get_swappiness(memcg);
  2668. }
  2669. static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
  2670. u64 val)
  2671. {
  2672. struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
  2673. struct mem_cgroup *parent;
  2674. if (val > 100)
  2675. return -EINVAL;
  2676. if (cgrp->parent == NULL)
  2677. return -EINVAL;
  2678. parent = mem_cgroup_from_cont(cgrp->parent);
  2679. cgroup_lock();
  2680. /* If under hierarchy, only empty-root can set this value */
  2681. if ((parent->use_hierarchy) ||
  2682. (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
  2683. cgroup_unlock();
  2684. return -EINVAL;
  2685. }
  2686. spin_lock(&memcg->reclaim_param_lock);
  2687. memcg->swappiness = val;
  2688. spin_unlock(&memcg->reclaim_param_lock);
  2689. cgroup_unlock();
  2690. return 0;
  2691. }
  2692. static struct cftype mem_cgroup_files[] = {
  2693. {
  2694. .name = "usage_in_bytes",
  2695. .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
  2696. .read_u64 = mem_cgroup_read,
  2697. },
  2698. {
  2699. .name = "max_usage_in_bytes",
  2700. .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
  2701. .trigger = mem_cgroup_reset,
  2702. .read_u64 = mem_cgroup_read,
  2703. },
  2704. {
  2705. .name = "limit_in_bytes",
  2706. .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
  2707. .write_string = mem_cgroup_write,
  2708. .read_u64 = mem_cgroup_read,
  2709. },
  2710. {
  2711. .name = "soft_limit_in_bytes",
  2712. .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
  2713. .write_string = mem_cgroup_write,
  2714. .read_u64 = mem_cgroup_read,
  2715. },
  2716. {
  2717. .name = "failcnt",
  2718. .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
  2719. .trigger = mem_cgroup_reset,
  2720. .read_u64 = mem_cgroup_read,
  2721. },
  2722. {
  2723. .name = "stat",
  2724. .read_map = mem_control_stat_show,
  2725. },
  2726. {
  2727. .name = "force_empty",
  2728. .trigger = mem_cgroup_force_empty_write,
  2729. },
  2730. {
  2731. .name = "use_hierarchy",
  2732. .write_u64 = mem_cgroup_hierarchy_write,
  2733. .read_u64 = mem_cgroup_hierarchy_read,
  2734. },
  2735. {
  2736. .name = "swappiness",
  2737. .read_u64 = mem_cgroup_swappiness_read,
  2738. .write_u64 = mem_cgroup_swappiness_write,
  2739. },
  2740. };
  2741. #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
  2742. static struct cftype memsw_cgroup_files[] = {
  2743. {
  2744. .name = "memsw.usage_in_bytes",
  2745. .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
  2746. .read_u64 = mem_cgroup_read,
  2747. },
  2748. {
  2749. .name = "memsw.max_usage_in_bytes",
  2750. .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
  2751. .trigger = mem_cgroup_reset,
  2752. .read_u64 = mem_cgroup_read,
  2753. },
  2754. {
  2755. .name = "memsw.limit_in_bytes",
  2756. .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
  2757. .write_string = mem_cgroup_write,
  2758. .read_u64 = mem_cgroup_read,
  2759. },
  2760. {
  2761. .name = "memsw.failcnt",
  2762. .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
  2763. .trigger = mem_cgroup_reset,
  2764. .read_u64 = mem_cgroup_read,
  2765. },
  2766. };
  2767. static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
  2768. {
  2769. if (!do_swap_account)
  2770. return 0;
  2771. return cgroup_add_files(cont, ss, memsw_cgroup_files,
  2772. ARRAY_SIZE(memsw_cgroup_files));
  2773. };
  2774. #else
  2775. static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
  2776. {
  2777. return 0;
  2778. }
  2779. #endif
  2780. static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
  2781. {
  2782. struct mem_cgroup_per_node *pn;
  2783. struct mem_cgroup_per_zone *mz;
  2784. enum lru_list l;
  2785. int zone, tmp = node;
  2786. /*
  2787. * This routine is called against possible nodes.
  2788. * But it's BUG to call kmalloc() against offline node.
  2789. *
  2790. * TODO: this routine can waste much memory for nodes which will
  2791. * never be onlined. It's better to use memory hotplug callback
  2792. * function.
  2793. */
  2794. if (!node_state(node, N_NORMAL_MEMORY))
  2795. tmp = -1;
  2796. pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
  2797. if (!pn)
  2798. return 1;
  2799. mem->info.nodeinfo[node] = pn;
  2800. memset(pn, 0, sizeof(*pn));
  2801. for (zone = 0; zone < MAX_NR_ZONES; zone++) {
  2802. mz = &pn->zoneinfo[zone];
  2803. for_each_lru(l)
  2804. INIT_LIST_HEAD(&mz->lists[l]);
  2805. mz->usage_in_excess = 0;
  2806. mz->on_tree = false;
  2807. mz->mem = mem;
  2808. }
  2809. return 0;
  2810. }
  2811. static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
  2812. {
  2813. kfree(mem->info.nodeinfo[node]);
  2814. }
  2815. static int mem_cgroup_size(void)
  2816. {
  2817. int cpustat_size = nr_cpu_ids * sizeof(struct mem_cgroup_stat_cpu);
  2818. return sizeof(struct mem_cgroup) + cpustat_size;
  2819. }
  2820. static struct mem_cgroup *mem_cgroup_alloc(void)
  2821. {
  2822. struct mem_cgroup *mem;
  2823. int size = mem_cgroup_size();
  2824. if (size < PAGE_SIZE)
  2825. mem = kmalloc(size, GFP_KERNEL);
  2826. else
  2827. mem = vmalloc(size);
  2828. if (mem)
  2829. memset(mem, 0, size);
  2830. return mem;
  2831. }
  2832. /*
  2833. * At destroying mem_cgroup, references from swap_cgroup can remain.
  2834. * (scanning all at force_empty is too costly...)
  2835. *
  2836. * Instead of clearing all references at force_empty, we remember
  2837. * the number of reference from swap_cgroup and free mem_cgroup when
  2838. * it goes down to 0.
  2839. *
  2840. * Removal of cgroup itself succeeds regardless of refs from swap.
  2841. */
  2842. static void __mem_cgroup_free(struct mem_cgroup *mem)
  2843. {
  2844. int node;
  2845. mem_cgroup_remove_from_trees(mem);
  2846. free_css_id(&mem_cgroup_subsys, &mem->css);
  2847. for_each_node_state(node, N_POSSIBLE)
  2848. free_mem_cgroup_per_zone_info(mem, node);
  2849. if (mem_cgroup_size() < PAGE_SIZE)
  2850. kfree(mem);
  2851. else
  2852. vfree(mem);
  2853. }
  2854. static void mem_cgroup_get(struct mem_cgroup *mem)
  2855. {
  2856. atomic_inc(&mem->refcnt);
  2857. }
  2858. static void mem_cgroup_put(struct mem_cgroup *mem)
  2859. {
  2860. if (atomic_dec_and_test(&mem->refcnt)) {
  2861. struct mem_cgroup *parent = parent_mem_cgroup(mem);
  2862. __mem_cgroup_free(mem);
  2863. if (parent)
  2864. mem_cgroup_put(parent);
  2865. }
  2866. }
  2867. /*
  2868. * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
  2869. */
  2870. static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem)
  2871. {
  2872. if (!mem->res.parent)
  2873. return NULL;
  2874. return mem_cgroup_from_res_counter(mem->res.parent, res);
  2875. }
  2876. #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
  2877. static void __init enable_swap_cgroup(void)
  2878. {
  2879. if (!mem_cgroup_disabled() && really_do_swap_account)
  2880. do_swap_account = 1;
  2881. }
  2882. #else
  2883. static void __init enable_swap_cgroup(void)
  2884. {
  2885. }
  2886. #endif
  2887. static int mem_cgroup_soft_limit_tree_init(void)
  2888. {
  2889. struct mem_cgroup_tree_per_node *rtpn;
  2890. struct mem_cgroup_tree_per_zone *rtpz;
  2891. int tmp, node, zone;
  2892. for_each_node_state(node, N_POSSIBLE) {
  2893. tmp = node;
  2894. if (!node_state(node, N_NORMAL_MEMORY))
  2895. tmp = -1;
  2896. rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp);
  2897. if (!rtpn)
  2898. return 1;
  2899. soft_limit_tree.rb_tree_per_node[node] = rtpn;
  2900. for (zone = 0; zone < MAX_NR_ZONES; zone++) {
  2901. rtpz = &rtpn->rb_tree_per_zone[zone];
  2902. rtpz->rb_root = RB_ROOT;
  2903. spin_lock_init(&rtpz->lock);
  2904. }
  2905. }
  2906. return 0;
  2907. }
  2908. static struct cgroup_subsys_state * __ref
  2909. mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
  2910. {
  2911. struct mem_cgroup *mem, *parent;
  2912. long error = -ENOMEM;
  2913. int node;
  2914. mem = mem_cgroup_alloc();
  2915. if (!mem)
  2916. return ERR_PTR(error);
  2917. for_each_node_state(node, N_POSSIBLE)
  2918. if (alloc_mem_cgroup_per_zone_info(mem, node))
  2919. goto free_out;
  2920. /* root ? */
  2921. if (cont->parent == NULL) {
  2922. int cpu;
  2923. enable_swap_cgroup();
  2924. parent = NULL;
  2925. root_mem_cgroup = mem;
  2926. if (mem_cgroup_soft_limit_tree_init())
  2927. goto free_out;
  2928. for_each_possible_cpu(cpu) {
  2929. struct memcg_stock_pcp *stock =
  2930. &per_cpu(memcg_stock, cpu);
  2931. INIT_WORK(&stock->work, drain_local_stock);
  2932. }
  2933. hotcpu_notifier(memcg_stock_cpu_callback, 0);
  2934. } else {
  2935. parent = mem_cgroup_from_cont(cont->parent);
  2936. mem->use_hierarchy = parent->use_hierarchy;
  2937. }
  2938. if (parent && parent->use_hierarchy) {
  2939. res_counter_init(&mem->res, &parent->res);
  2940. res_counter_init(&mem->memsw, &parent->memsw);
  2941. /*
  2942. * We increment refcnt of the parent to ensure that we can
  2943. * safely access it on res_counter_charge/uncharge.
  2944. * This refcnt will be decremented when freeing this
  2945. * mem_cgroup(see mem_cgroup_put).
  2946. */
  2947. mem_cgroup_get(parent);
  2948. } else {
  2949. res_counter_init(&mem->res, NULL);
  2950. res_counter_init(&mem->memsw, NULL);
  2951. }
  2952. mem->last_scanned_child = 0;
  2953. spin_lock_init(&mem->reclaim_param_lock);
  2954. if (parent)
  2955. mem->swappiness = get_swappiness(parent);
  2956. atomic_set(&mem->refcnt, 1);
  2957. return &mem->css;
  2958. free_out:
  2959. __mem_cgroup_free(mem);
  2960. root_mem_cgroup = NULL;
  2961. return ERR_PTR(error);
  2962. }
  2963. static int mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
  2964. struct cgroup *cont)
  2965. {
  2966. struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
  2967. return mem_cgroup_force_empty(mem, false);
  2968. }
  2969. static void mem_cgroup_destroy(struct cgroup_subsys *ss,
  2970. struct cgroup *cont)
  2971. {
  2972. struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
  2973. mem_cgroup_put(mem);
  2974. }
  2975. static int mem_cgroup_populate(struct cgroup_subsys *ss,
  2976. struct cgroup *cont)
  2977. {
  2978. int ret;
  2979. ret = cgroup_add_files(cont, ss, mem_cgroup_files,
  2980. ARRAY_SIZE(mem_cgroup_files));
  2981. if (!ret)
  2982. ret = register_memsw_files(cont, ss);
  2983. return ret;
  2984. }
  2985. static void mem_cgroup_move_task(struct cgroup_subsys *ss,
  2986. struct cgroup *cont,
  2987. struct cgroup *old_cont,
  2988. struct task_struct *p,
  2989. bool threadgroup)
  2990. {
  2991. mutex_lock(&memcg_tasklist);
  2992. /*
  2993. * FIXME: It's better to move charges of this process from old
  2994. * memcg to new memcg. But it's just on TODO-List now.
  2995. */
  2996. mutex_unlock(&memcg_tasklist);
  2997. }
  2998. struct cgroup_subsys mem_cgroup_subsys = {
  2999. .name = "memory",
  3000. .subsys_id = mem_cgroup_subsys_id,
  3001. .create = mem_cgroup_create,
  3002. .pre_destroy = mem_cgroup_pre_destroy,
  3003. .destroy = mem_cgroup_destroy,
  3004. .populate = mem_cgroup_populate,
  3005. .attach = mem_cgroup_move_task,
  3006. .early_init = 0,
  3007. .use_id = 1,
  3008. };
  3009. #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
  3010. static int __init disable_swap_account(char *s)
  3011. {
  3012. really_do_swap_account = 0;
  3013. return 1;
  3014. }
  3015. __setup("noswapaccount", disable_swap_account);
  3016. #endif