memcontrol.c 79 KB

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