memcontrol.c 79 KB

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