memcontrol.c 79 KB

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