memcontrol.c 86 KB

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