memcontrol.c 77 KB

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