memcontrol.c 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214
  1. /* memcontrol.c - Memory Controller
  2. *
  3. * Copyright IBM Corporation, 2007
  4. * Author Balbir Singh <balbir@linux.vnet.ibm.com>
  5. *
  6. * Copyright 2007 OpenVZ SWsoft Inc
  7. * Author: Pavel Emelianov <xemul@openvz.org>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. */
  19. #include <linux/res_counter.h>
  20. #include <linux/memcontrol.h>
  21. #include <linux/cgroup.h>
  22. #include <linux/mm.h>
  23. #include <linux/pagemap.h>
  24. #include <linux/smp.h>
  25. #include <linux/page-flags.h>
  26. #include <linux/backing-dev.h>
  27. #include <linux/bit_spinlock.h>
  28. #include <linux/rcupdate.h>
  29. #include <linux/limits.h>
  30. #include <linux/mutex.h>
  31. #include <linux/rbtree.h>
  32. #include <linux/slab.h>
  33. #include <linux/swap.h>
  34. #include <linux/spinlock.h>
  35. #include <linux/fs.h>
  36. #include <linux/seq_file.h>
  37. #include <linux/vmalloc.h>
  38. #include <linux/mm_inline.h>
  39. #include <linux/page_cgroup.h>
  40. #include "internal.h"
  41. #include <asm/uaccess.h>
  42. struct cgroup_subsys mem_cgroup_subsys __read_mostly;
  43. #define MEM_CGROUP_RECLAIM_RETRIES 5
  44. struct mem_cgroup *root_mem_cgroup __read_mostly;
  45. #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
  46. /* Turned on only when memory cgroup is enabled && really_do_swap_account = 1 */
  47. int do_swap_account __read_mostly;
  48. static int really_do_swap_account __initdata = 1; /* for remember boot option*/
  49. #else
  50. #define do_swap_account (0)
  51. #endif
  52. static DEFINE_MUTEX(memcg_tasklist); /* can be hold under cgroup_mutex */
  53. #define SOFTLIMIT_EVENTS_THRESH (1000)
  54. /*
  55. * Statistics for memory cgroup.
  56. */
  57. enum mem_cgroup_stat_index {
  58. /*
  59. * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss.
  60. */
  61. MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */
  62. MEM_CGROUP_STAT_RSS, /* # of pages charged as anon rss */
  63. MEM_CGROUP_STAT_MAPPED_FILE, /* # of pages charged as file rss */
  64. MEM_CGROUP_STAT_PGPGIN_COUNT, /* # of pages paged in */
  65. MEM_CGROUP_STAT_PGPGOUT_COUNT, /* # of pages paged out */
  66. MEM_CGROUP_STAT_EVENTS, /* sum of pagein + pageout for internal use */
  67. MEM_CGROUP_STAT_SWAPOUT, /* # of pages, swapped out */
  68. MEM_CGROUP_STAT_NSTATS,
  69. };
  70. struct mem_cgroup_stat_cpu {
  71. s64 count[MEM_CGROUP_STAT_NSTATS];
  72. } ____cacheline_aligned_in_smp;
  73. struct mem_cgroup_stat {
  74. struct mem_cgroup_stat_cpu cpustat[0];
  75. };
  76. static inline void
  77. __mem_cgroup_stat_reset_safe(struct mem_cgroup_stat_cpu *stat,
  78. enum mem_cgroup_stat_index idx)
  79. {
  80. stat->count[idx] = 0;
  81. }
  82. static inline s64
  83. __mem_cgroup_stat_read_local(struct mem_cgroup_stat_cpu *stat,
  84. enum mem_cgroup_stat_index idx)
  85. {
  86. return stat->count[idx];
  87. }
  88. /*
  89. * For accounting under irq disable, no need for increment preempt count.
  90. */
  91. static inline void __mem_cgroup_stat_add_safe(struct mem_cgroup_stat_cpu *stat,
  92. enum mem_cgroup_stat_index idx, int val)
  93. {
  94. stat->count[idx] += val;
  95. }
  96. static s64 mem_cgroup_read_stat(struct mem_cgroup_stat *stat,
  97. enum mem_cgroup_stat_index idx)
  98. {
  99. int cpu;
  100. s64 ret = 0;
  101. for_each_possible_cpu(cpu)
  102. ret += stat->cpustat[cpu].count[idx];
  103. return ret;
  104. }
  105. static s64 mem_cgroup_local_usage(struct mem_cgroup_stat *stat)
  106. {
  107. s64 ret;
  108. ret = mem_cgroup_read_stat(stat, MEM_CGROUP_STAT_CACHE);
  109. ret += mem_cgroup_read_stat(stat, MEM_CGROUP_STAT_RSS);
  110. return ret;
  111. }
  112. /*
  113. * per-zone information in memory controller.
  114. */
  115. struct mem_cgroup_per_zone {
  116. /*
  117. * spin_lock to protect the per cgroup LRU
  118. */
  119. struct list_head lists[NR_LRU_LISTS];
  120. unsigned long count[NR_LRU_LISTS];
  121. struct zone_reclaim_stat reclaim_stat;
  122. struct rb_node tree_node; /* RB tree node */
  123. unsigned long long usage_in_excess;/* Set to the value by which */
  124. /* the soft limit is exceeded*/
  125. bool on_tree;
  126. struct mem_cgroup *mem; /* Back pointer, we cannot */
  127. /* use container_of */
  128. };
  129. /* Macro for accessing counter */
  130. #define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)])
  131. struct mem_cgroup_per_node {
  132. struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
  133. };
  134. struct mem_cgroup_lru_info {
  135. struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES];
  136. };
  137. /*
  138. * Cgroups above their limits are maintained in a RB-Tree, independent of
  139. * their hierarchy representation
  140. */
  141. struct mem_cgroup_tree_per_zone {
  142. struct rb_root rb_root;
  143. spinlock_t lock;
  144. };
  145. struct mem_cgroup_tree_per_node {
  146. struct mem_cgroup_tree_per_zone rb_tree_per_zone[MAX_NR_ZONES];
  147. };
  148. struct mem_cgroup_tree {
  149. struct mem_cgroup_tree_per_node *rb_tree_per_node[MAX_NUMNODES];
  150. };
  151. static struct mem_cgroup_tree soft_limit_tree __read_mostly;
  152. /*
  153. * The memory controller data structure. The memory controller controls both
  154. * page cache and RSS per cgroup. We would eventually like to provide
  155. * statistics based on the statistics developed by Rik Van Riel for clock-pro,
  156. * to help the administrator determine what knobs to tune.
  157. *
  158. * TODO: Add a water mark for the memory controller. Reclaim will begin when
  159. * we hit the water mark. May be even add a low water mark, such that
  160. * no reclaim occurs from a cgroup at it's low water mark, this is
  161. * a feature that will be implemented much later in the future.
  162. */
  163. struct mem_cgroup {
  164. struct cgroup_subsys_state css;
  165. /*
  166. * the counter to account for memory usage
  167. */
  168. struct res_counter res;
  169. /*
  170. * the counter to account for mem+swap usage.
  171. */
  172. struct res_counter memsw;
  173. /*
  174. * Per cgroup active and inactive list, similar to the
  175. * per zone LRU lists.
  176. */
  177. struct mem_cgroup_lru_info info;
  178. /*
  179. protect against reclaim related member.
  180. */
  181. spinlock_t reclaim_param_lock;
  182. int prev_priority; /* for recording reclaim priority */
  183. /*
  184. * While reclaiming in a hiearchy, we cache the last child we
  185. * reclaimed from.
  186. */
  187. int last_scanned_child;
  188. /*
  189. * Should the accounting and control be hierarchical, per subtree?
  190. */
  191. bool use_hierarchy;
  192. unsigned long last_oom_jiffies;
  193. atomic_t refcnt;
  194. unsigned int swappiness;
  195. /* set when res.limit == memsw.limit */
  196. bool memsw_is_minimum;
  197. /*
  198. * statistics. This must be placed at the end of memcg.
  199. */
  200. struct mem_cgroup_stat stat;
  201. };
  202. /*
  203. * Maximum loops in mem_cgroup_hierarchical_reclaim(), used for soft
  204. * limit reclaim to prevent infinite loops, if they ever occur.
  205. */
  206. #define MEM_CGROUP_MAX_RECLAIM_LOOPS (100)
  207. #define MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS (2)
  208. enum charge_type {
  209. MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
  210. MEM_CGROUP_CHARGE_TYPE_MAPPED,
  211. MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */
  212. MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */
  213. MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
  214. MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */
  215. NR_CHARGE_TYPE,
  216. };
  217. /* only for here (for easy reading.) */
  218. #define PCGF_CACHE (1UL << PCG_CACHE)
  219. #define PCGF_USED (1UL << PCG_USED)
  220. #define PCGF_LOCK (1UL << PCG_LOCK)
  221. /* Not used, but added here for completeness */
  222. #define PCGF_ACCT (1UL << PCG_ACCT)
  223. /* for encoding cft->private value on file */
  224. #define _MEM (0)
  225. #define _MEMSWAP (1)
  226. #define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val))
  227. #define MEMFILE_TYPE(val) (((val) >> 16) & 0xffff)
  228. #define MEMFILE_ATTR(val) ((val) & 0xffff)
  229. /*
  230. * Reclaim flags for mem_cgroup_hierarchical_reclaim
  231. */
  232. #define MEM_CGROUP_RECLAIM_NOSWAP_BIT 0x0
  233. #define MEM_CGROUP_RECLAIM_NOSWAP (1 << MEM_CGROUP_RECLAIM_NOSWAP_BIT)
  234. #define MEM_CGROUP_RECLAIM_SHRINK_BIT 0x1
  235. #define MEM_CGROUP_RECLAIM_SHRINK (1 << MEM_CGROUP_RECLAIM_SHRINK_BIT)
  236. #define MEM_CGROUP_RECLAIM_SOFT_BIT 0x2
  237. #define MEM_CGROUP_RECLAIM_SOFT (1 << MEM_CGROUP_RECLAIM_SOFT_BIT)
  238. static void mem_cgroup_get(struct mem_cgroup *mem);
  239. static void mem_cgroup_put(struct mem_cgroup *mem);
  240. static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem);
  241. static struct mem_cgroup_per_zone *
  242. mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid)
  243. {
  244. return &mem->info.nodeinfo[nid]->zoneinfo[zid];
  245. }
  246. static struct mem_cgroup_per_zone *
  247. page_cgroup_zoneinfo(struct page_cgroup *pc)
  248. {
  249. struct mem_cgroup *mem = pc->mem_cgroup;
  250. int nid = page_cgroup_nid(pc);
  251. int zid = page_cgroup_zid(pc);
  252. if (!mem)
  253. return NULL;
  254. return mem_cgroup_zoneinfo(mem, nid, zid);
  255. }
  256. static struct mem_cgroup_tree_per_zone *
  257. soft_limit_tree_node_zone(int nid, int zid)
  258. {
  259. return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
  260. }
  261. static struct mem_cgroup_tree_per_zone *
  262. soft_limit_tree_from_page(struct page *page)
  263. {
  264. int nid = page_to_nid(page);
  265. int zid = page_zonenum(page);
  266. return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
  267. }
  268. static void
  269. __mem_cgroup_insert_exceeded(struct mem_cgroup *mem,
  270. struct mem_cgroup_per_zone *mz,
  271. struct mem_cgroup_tree_per_zone *mctz)
  272. {
  273. struct rb_node **p = &mctz->rb_root.rb_node;
  274. struct rb_node *parent = NULL;
  275. struct mem_cgroup_per_zone *mz_node;
  276. if (mz->on_tree)
  277. return;
  278. mz->usage_in_excess = res_counter_soft_limit_excess(&mem->res);
  279. while (*p) {
  280. parent = *p;
  281. mz_node = rb_entry(parent, struct mem_cgroup_per_zone,
  282. tree_node);
  283. if (mz->usage_in_excess < mz_node->usage_in_excess)
  284. p = &(*p)->rb_left;
  285. /*
  286. * We can't avoid mem cgroups that are over their soft
  287. * limit by the same amount
  288. */
  289. else if (mz->usage_in_excess >= mz_node->usage_in_excess)
  290. p = &(*p)->rb_right;
  291. }
  292. rb_link_node(&mz->tree_node, parent, p);
  293. rb_insert_color(&mz->tree_node, &mctz->rb_root);
  294. mz->on_tree = true;
  295. }
  296. static void
  297. __mem_cgroup_remove_exceeded(struct mem_cgroup *mem,
  298. struct mem_cgroup_per_zone *mz,
  299. struct mem_cgroup_tree_per_zone *mctz)
  300. {
  301. if (!mz->on_tree)
  302. return;
  303. rb_erase(&mz->tree_node, &mctz->rb_root);
  304. mz->on_tree = false;
  305. }
  306. static void
  307. mem_cgroup_insert_exceeded(struct mem_cgroup *mem,
  308. struct mem_cgroup_per_zone *mz,
  309. struct mem_cgroup_tree_per_zone *mctz)
  310. {
  311. spin_lock(&mctz->lock);
  312. __mem_cgroup_insert_exceeded(mem, mz, mctz);
  313. spin_unlock(&mctz->lock);
  314. }
  315. static void
  316. mem_cgroup_remove_exceeded(struct mem_cgroup *mem,
  317. struct mem_cgroup_per_zone *mz,
  318. struct mem_cgroup_tree_per_zone *mctz)
  319. {
  320. spin_lock(&mctz->lock);
  321. __mem_cgroup_remove_exceeded(mem, mz, mctz);
  322. spin_unlock(&mctz->lock);
  323. }
  324. static bool mem_cgroup_soft_limit_check(struct mem_cgroup *mem)
  325. {
  326. bool ret = false;
  327. int cpu;
  328. s64 val;
  329. struct mem_cgroup_stat_cpu *cpustat;
  330. cpu = get_cpu();
  331. cpustat = &mem->stat.cpustat[cpu];
  332. val = __mem_cgroup_stat_read_local(cpustat, MEM_CGROUP_STAT_EVENTS);
  333. if (unlikely(val > SOFTLIMIT_EVENTS_THRESH)) {
  334. __mem_cgroup_stat_reset_safe(cpustat, MEM_CGROUP_STAT_EVENTS);
  335. ret = true;
  336. }
  337. put_cpu();
  338. return ret;
  339. }
  340. static void mem_cgroup_update_tree(struct mem_cgroup *mem, struct page *page)
  341. {
  342. unsigned long long prev_usage_in_excess, new_usage_in_excess;
  343. bool updated_tree = false;
  344. struct mem_cgroup_per_zone *mz;
  345. struct mem_cgroup_tree_per_zone *mctz;
  346. mz = mem_cgroup_zoneinfo(mem, page_to_nid(page), page_zonenum(page));
  347. mctz = soft_limit_tree_from_page(page);
  348. /*
  349. * We do updates in lazy mode, mem's are removed
  350. * lazily from the per-zone, per-node rb tree
  351. */
  352. prev_usage_in_excess = mz->usage_in_excess;
  353. new_usage_in_excess = res_counter_soft_limit_excess(&mem->res);
  354. if (prev_usage_in_excess) {
  355. mem_cgroup_remove_exceeded(mem, mz, mctz);
  356. updated_tree = true;
  357. }
  358. if (!new_usage_in_excess)
  359. goto done;
  360. mem_cgroup_insert_exceeded(mem, mz, mctz);
  361. done:
  362. if (updated_tree) {
  363. spin_lock(&mctz->lock);
  364. mz->usage_in_excess = new_usage_in_excess;
  365. spin_unlock(&mctz->lock);
  366. }
  367. }
  368. static void mem_cgroup_remove_from_trees(struct mem_cgroup *mem)
  369. {
  370. int node, zone;
  371. struct mem_cgroup_per_zone *mz;
  372. struct mem_cgroup_tree_per_zone *mctz;
  373. for_each_node_state(node, N_POSSIBLE) {
  374. for (zone = 0; zone < MAX_NR_ZONES; zone++) {
  375. mz = mem_cgroup_zoneinfo(mem, node, zone);
  376. mctz = soft_limit_tree_node_zone(node, zone);
  377. mem_cgroup_remove_exceeded(mem, mz, mctz);
  378. }
  379. }
  380. }
  381. static inline unsigned long mem_cgroup_get_excess(struct mem_cgroup *mem)
  382. {
  383. return res_counter_soft_limit_excess(&mem->res) >> PAGE_SHIFT;
  384. }
  385. static struct mem_cgroup_per_zone *
  386. __mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
  387. {
  388. struct rb_node *rightmost = NULL;
  389. struct mem_cgroup_per_zone *mz = NULL;
  390. retry:
  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, *mem_over_soft_limit;
  1101. int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
  1102. struct res_counter *fail_res, *soft_fail_res = NULL;
  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. &soft_fail_res);
  1131. if (likely(!ret)) {
  1132. if (!do_swap_account)
  1133. break;
  1134. ret = res_counter_charge(&mem->memsw, PAGE_SIZE,
  1135. &fail_res, NULL);
  1136. if (likely(!ret))
  1137. break;
  1138. /* mem+swap counter fails */
  1139. res_counter_uncharge(&mem->res, PAGE_SIZE, NULL);
  1140. flags |= MEM_CGROUP_RECLAIM_NOSWAP;
  1141. mem_over_limit = mem_cgroup_from_res_counter(fail_res,
  1142. memsw);
  1143. } else
  1144. /* mem counter fails */
  1145. mem_over_limit = mem_cgroup_from_res_counter(fail_res,
  1146. res);
  1147. if (!(gfp_mask & __GFP_WAIT))
  1148. goto nomem;
  1149. ret = mem_cgroup_hierarchical_reclaim(mem_over_limit, NULL,
  1150. gfp_mask, flags);
  1151. if (ret)
  1152. continue;
  1153. /*
  1154. * try_to_free_mem_cgroup_pages() might not give us a full
  1155. * picture of reclaim. Some pages are reclaimed and might be
  1156. * moved to swap cache or just unmapped from the cgroup.
  1157. * Check the limit again to see if the reclaim reduced the
  1158. * current usage of the cgroup before giving up
  1159. *
  1160. */
  1161. if (mem_cgroup_check_under_limit(mem_over_limit))
  1162. continue;
  1163. if (!nr_retries--) {
  1164. if (oom) {
  1165. mutex_lock(&memcg_tasklist);
  1166. mem_cgroup_out_of_memory(mem_over_limit, gfp_mask);
  1167. mutex_unlock(&memcg_tasklist);
  1168. record_last_oom(mem_over_limit);
  1169. }
  1170. goto nomem;
  1171. }
  1172. }
  1173. /*
  1174. * Insert just the ancestor, we should trickle down to the correct
  1175. * cgroup for reclaim, since the other nodes will be below their
  1176. * soft limit
  1177. */
  1178. if (soft_fail_res) {
  1179. mem_over_soft_limit =
  1180. mem_cgroup_from_res_counter(soft_fail_res, res);
  1181. if (mem_cgroup_soft_limit_check(mem_over_soft_limit))
  1182. mem_cgroup_update_tree(mem_over_soft_limit, page);
  1183. }
  1184. done:
  1185. return 0;
  1186. nomem:
  1187. css_put(&mem->css);
  1188. return -ENOMEM;
  1189. }
  1190. /*
  1191. * A helper function to get mem_cgroup from ID. must be called under
  1192. * rcu_read_lock(). The caller must check css_is_removed() or some if
  1193. * it's concern. (dropping refcnt from swap can be called against removed
  1194. * memcg.)
  1195. */
  1196. static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
  1197. {
  1198. struct cgroup_subsys_state *css;
  1199. /* ID 0 is unused ID */
  1200. if (!id)
  1201. return NULL;
  1202. css = css_lookup(&mem_cgroup_subsys, id);
  1203. if (!css)
  1204. return NULL;
  1205. return container_of(css, struct mem_cgroup, css);
  1206. }
  1207. static struct mem_cgroup *try_get_mem_cgroup_from_swapcache(struct page *page)
  1208. {
  1209. struct mem_cgroup *mem;
  1210. struct page_cgroup *pc;
  1211. unsigned short id;
  1212. swp_entry_t ent;
  1213. VM_BUG_ON(!PageLocked(page));
  1214. if (!PageSwapCache(page))
  1215. return NULL;
  1216. pc = lookup_page_cgroup(page);
  1217. lock_page_cgroup(pc);
  1218. if (PageCgroupUsed(pc)) {
  1219. mem = pc->mem_cgroup;
  1220. if (mem && !css_tryget(&mem->css))
  1221. mem = NULL;
  1222. } else {
  1223. ent.val = page_private(page);
  1224. id = lookup_swap_cgroup(ent);
  1225. rcu_read_lock();
  1226. mem = mem_cgroup_lookup(id);
  1227. if (mem && !css_tryget(&mem->css))
  1228. mem = NULL;
  1229. rcu_read_unlock();
  1230. }
  1231. unlock_page_cgroup(pc);
  1232. return mem;
  1233. }
  1234. /*
  1235. * commit a charge got by __mem_cgroup_try_charge() and makes page_cgroup to be
  1236. * USED state. If already USED, uncharge and return.
  1237. */
  1238. static void __mem_cgroup_commit_charge(struct mem_cgroup *mem,
  1239. struct page_cgroup *pc,
  1240. enum charge_type ctype)
  1241. {
  1242. /* try_charge() can return NULL to *memcg, taking care of it. */
  1243. if (!mem)
  1244. return;
  1245. lock_page_cgroup(pc);
  1246. if (unlikely(PageCgroupUsed(pc))) {
  1247. unlock_page_cgroup(pc);
  1248. if (!mem_cgroup_is_root(mem)) {
  1249. res_counter_uncharge(&mem->res, PAGE_SIZE, NULL);
  1250. if (do_swap_account)
  1251. res_counter_uncharge(&mem->memsw, PAGE_SIZE,
  1252. NULL);
  1253. }
  1254. css_put(&mem->css);
  1255. return;
  1256. }
  1257. pc->mem_cgroup = mem;
  1258. /*
  1259. * We access a page_cgroup asynchronously without lock_page_cgroup().
  1260. * Especially when a page_cgroup is taken from a page, pc->mem_cgroup
  1261. * is accessed after testing USED bit. To make pc->mem_cgroup visible
  1262. * before USED bit, we need memory barrier here.
  1263. * See mem_cgroup_add_lru_list(), etc.
  1264. */
  1265. smp_wmb();
  1266. switch (ctype) {
  1267. case MEM_CGROUP_CHARGE_TYPE_CACHE:
  1268. case MEM_CGROUP_CHARGE_TYPE_SHMEM:
  1269. SetPageCgroupCache(pc);
  1270. SetPageCgroupUsed(pc);
  1271. break;
  1272. case MEM_CGROUP_CHARGE_TYPE_MAPPED:
  1273. ClearPageCgroupCache(pc);
  1274. SetPageCgroupUsed(pc);
  1275. break;
  1276. default:
  1277. break;
  1278. }
  1279. mem_cgroup_charge_statistics(mem, pc, true);
  1280. unlock_page_cgroup(pc);
  1281. }
  1282. /**
  1283. * mem_cgroup_move_account - move account of the page
  1284. * @pc: page_cgroup of the page.
  1285. * @from: mem_cgroup which the page is moved from.
  1286. * @to: mem_cgroup which the page is moved to. @from != @to.
  1287. *
  1288. * The caller must confirm following.
  1289. * - page is not on LRU (isolate_page() is useful.)
  1290. *
  1291. * returns 0 at success,
  1292. * returns -EBUSY when lock is busy or "pc" is unstable.
  1293. *
  1294. * This function does "uncharge" from old cgroup but doesn't do "charge" to
  1295. * new cgroup. It should be done by a caller.
  1296. */
  1297. static int mem_cgroup_move_account(struct page_cgroup *pc,
  1298. struct mem_cgroup *from, struct mem_cgroup *to)
  1299. {
  1300. struct mem_cgroup_per_zone *from_mz, *to_mz;
  1301. int nid, zid;
  1302. int ret = -EBUSY;
  1303. struct page *page;
  1304. int cpu;
  1305. struct mem_cgroup_stat *stat;
  1306. struct mem_cgroup_stat_cpu *cpustat;
  1307. VM_BUG_ON(from == to);
  1308. VM_BUG_ON(PageLRU(pc->page));
  1309. nid = page_cgroup_nid(pc);
  1310. zid = page_cgroup_zid(pc);
  1311. from_mz = mem_cgroup_zoneinfo(from, nid, zid);
  1312. to_mz = mem_cgroup_zoneinfo(to, nid, zid);
  1313. if (!trylock_page_cgroup(pc))
  1314. return ret;
  1315. if (!PageCgroupUsed(pc))
  1316. goto out;
  1317. if (pc->mem_cgroup != from)
  1318. goto out;
  1319. if (!mem_cgroup_is_root(from))
  1320. res_counter_uncharge(&from->res, PAGE_SIZE, NULL);
  1321. mem_cgroup_charge_statistics(from, pc, false);
  1322. page = pc->page;
  1323. if (page_is_file_cache(page) && page_mapped(page)) {
  1324. cpu = smp_processor_id();
  1325. /* Update mapped_file data for mem_cgroup "from" */
  1326. stat = &from->stat;
  1327. cpustat = &stat->cpustat[cpu];
  1328. __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_MAPPED_FILE,
  1329. -1);
  1330. /* Update mapped_file data for mem_cgroup "to" */
  1331. stat = &to->stat;
  1332. cpustat = &stat->cpustat[cpu];
  1333. __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_MAPPED_FILE,
  1334. 1);
  1335. }
  1336. if (do_swap_account && !mem_cgroup_is_root(from))
  1337. res_counter_uncharge(&from->memsw, PAGE_SIZE, NULL);
  1338. css_put(&from->css);
  1339. css_get(&to->css);
  1340. pc->mem_cgroup = to;
  1341. mem_cgroup_charge_statistics(to, pc, true);
  1342. ret = 0;
  1343. out:
  1344. unlock_page_cgroup(pc);
  1345. /*
  1346. * We charges against "to" which may not have any tasks. Then, "to"
  1347. * can be under rmdir(). But in current implementation, caller of
  1348. * this function is just force_empty() and it's garanteed that
  1349. * "to" is never removed. So, we don't check rmdir status here.
  1350. */
  1351. return ret;
  1352. }
  1353. /*
  1354. * move charges to its parent.
  1355. */
  1356. static int mem_cgroup_move_parent(struct page_cgroup *pc,
  1357. struct mem_cgroup *child,
  1358. gfp_t gfp_mask)
  1359. {
  1360. struct page *page = pc->page;
  1361. struct cgroup *cg = child->css.cgroup;
  1362. struct cgroup *pcg = cg->parent;
  1363. struct mem_cgroup *parent;
  1364. int ret;
  1365. /* Is ROOT ? */
  1366. if (!pcg)
  1367. return -EINVAL;
  1368. parent = mem_cgroup_from_cont(pcg);
  1369. ret = __mem_cgroup_try_charge(NULL, gfp_mask, &parent, false, page);
  1370. if (ret || !parent)
  1371. return ret;
  1372. if (!get_page_unless_zero(page)) {
  1373. ret = -EBUSY;
  1374. goto uncharge;
  1375. }
  1376. ret = isolate_lru_page(page);
  1377. if (ret)
  1378. goto cancel;
  1379. ret = mem_cgroup_move_account(pc, child, parent);
  1380. putback_lru_page(page);
  1381. if (!ret) {
  1382. put_page(page);
  1383. /* drop extra refcnt by try_charge() */
  1384. css_put(&parent->css);
  1385. return 0;
  1386. }
  1387. cancel:
  1388. put_page(page);
  1389. uncharge:
  1390. /* drop extra refcnt by try_charge() */
  1391. css_put(&parent->css);
  1392. /* uncharge if move fails */
  1393. if (!mem_cgroup_is_root(parent)) {
  1394. res_counter_uncharge(&parent->res, PAGE_SIZE, NULL);
  1395. if (do_swap_account)
  1396. res_counter_uncharge(&parent->memsw, PAGE_SIZE, NULL);
  1397. }
  1398. return ret;
  1399. }
  1400. /*
  1401. * Charge the memory controller for page usage.
  1402. * Return
  1403. * 0 if the charge was successful
  1404. * < 0 if the cgroup is over its limit
  1405. */
  1406. static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
  1407. gfp_t gfp_mask, enum charge_type ctype,
  1408. struct mem_cgroup *memcg)
  1409. {
  1410. struct mem_cgroup *mem;
  1411. struct page_cgroup *pc;
  1412. int ret;
  1413. pc = lookup_page_cgroup(page);
  1414. /* can happen at boot */
  1415. if (unlikely(!pc))
  1416. return 0;
  1417. prefetchw(pc);
  1418. mem = memcg;
  1419. ret = __mem_cgroup_try_charge(mm, gfp_mask, &mem, true, page);
  1420. if (ret || !mem)
  1421. return ret;
  1422. __mem_cgroup_commit_charge(mem, pc, ctype);
  1423. return 0;
  1424. }
  1425. int mem_cgroup_newpage_charge(struct page *page,
  1426. struct mm_struct *mm, gfp_t gfp_mask)
  1427. {
  1428. if (mem_cgroup_disabled())
  1429. return 0;
  1430. if (PageCompound(page))
  1431. return 0;
  1432. /*
  1433. * If already mapped, we don't have to account.
  1434. * If page cache, page->mapping has address_space.
  1435. * But page->mapping may have out-of-use anon_vma pointer,
  1436. * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
  1437. * is NULL.
  1438. */
  1439. if (page_mapped(page) || (page->mapping && !PageAnon(page)))
  1440. return 0;
  1441. if (unlikely(!mm))
  1442. mm = &init_mm;
  1443. return mem_cgroup_charge_common(page, mm, gfp_mask,
  1444. MEM_CGROUP_CHARGE_TYPE_MAPPED, NULL);
  1445. }
  1446. static void
  1447. __mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
  1448. enum charge_type ctype);
  1449. int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
  1450. gfp_t gfp_mask)
  1451. {
  1452. struct mem_cgroup *mem = NULL;
  1453. int ret;
  1454. if (mem_cgroup_disabled())
  1455. return 0;
  1456. if (PageCompound(page))
  1457. return 0;
  1458. /*
  1459. * Corner case handling. This is called from add_to_page_cache()
  1460. * in usual. But some FS (shmem) precharges this page before calling it
  1461. * and call add_to_page_cache() with GFP_NOWAIT.
  1462. *
  1463. * For GFP_NOWAIT case, the page may be pre-charged before calling
  1464. * add_to_page_cache(). (See shmem.c) check it here and avoid to call
  1465. * charge twice. (It works but has to pay a bit larger cost.)
  1466. * And when the page is SwapCache, it should take swap information
  1467. * into account. This is under lock_page() now.
  1468. */
  1469. if (!(gfp_mask & __GFP_WAIT)) {
  1470. struct page_cgroup *pc;
  1471. pc = lookup_page_cgroup(page);
  1472. if (!pc)
  1473. return 0;
  1474. lock_page_cgroup(pc);
  1475. if (PageCgroupUsed(pc)) {
  1476. unlock_page_cgroup(pc);
  1477. return 0;
  1478. }
  1479. unlock_page_cgroup(pc);
  1480. }
  1481. if (unlikely(!mm && !mem))
  1482. mm = &init_mm;
  1483. if (page_is_file_cache(page))
  1484. return mem_cgroup_charge_common(page, mm, gfp_mask,
  1485. MEM_CGROUP_CHARGE_TYPE_CACHE, NULL);
  1486. /* shmem */
  1487. if (PageSwapCache(page)) {
  1488. ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
  1489. if (!ret)
  1490. __mem_cgroup_commit_charge_swapin(page, mem,
  1491. MEM_CGROUP_CHARGE_TYPE_SHMEM);
  1492. } else
  1493. ret = mem_cgroup_charge_common(page, mm, gfp_mask,
  1494. MEM_CGROUP_CHARGE_TYPE_SHMEM, mem);
  1495. return ret;
  1496. }
  1497. /*
  1498. * While swap-in, try_charge -> commit or cancel, the page is locked.
  1499. * And when try_charge() successfully returns, one refcnt to memcg without
  1500. * struct page_cgroup is aquired. This refcnt will be cumsumed by
  1501. * "commit()" or removed by "cancel()"
  1502. */
  1503. int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
  1504. struct page *page,
  1505. gfp_t mask, struct mem_cgroup **ptr)
  1506. {
  1507. struct mem_cgroup *mem;
  1508. int ret;
  1509. if (mem_cgroup_disabled())
  1510. return 0;
  1511. if (!do_swap_account)
  1512. goto charge_cur_mm;
  1513. /*
  1514. * A racing thread's fault, or swapoff, may have already updated
  1515. * the pte, and even removed page from swap cache: return success
  1516. * to go on to do_swap_page()'s pte_same() test, which should fail.
  1517. */
  1518. if (!PageSwapCache(page))
  1519. return 0;
  1520. mem = try_get_mem_cgroup_from_swapcache(page);
  1521. if (!mem)
  1522. goto charge_cur_mm;
  1523. *ptr = mem;
  1524. ret = __mem_cgroup_try_charge(NULL, mask, ptr, true, page);
  1525. /* drop extra refcnt from tryget */
  1526. css_put(&mem->css);
  1527. return ret;
  1528. charge_cur_mm:
  1529. if (unlikely(!mm))
  1530. mm = &init_mm;
  1531. return __mem_cgroup_try_charge(mm, mask, ptr, true, page);
  1532. }
  1533. static void
  1534. __mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
  1535. enum charge_type ctype)
  1536. {
  1537. struct page_cgroup *pc;
  1538. if (mem_cgroup_disabled())
  1539. return;
  1540. if (!ptr)
  1541. return;
  1542. cgroup_exclude_rmdir(&ptr->css);
  1543. pc = lookup_page_cgroup(page);
  1544. mem_cgroup_lru_del_before_commit_swapcache(page);
  1545. __mem_cgroup_commit_charge(ptr, pc, ctype);
  1546. mem_cgroup_lru_add_after_commit_swapcache(page);
  1547. /*
  1548. * Now swap is on-memory. This means this page may be
  1549. * counted both as mem and swap....double count.
  1550. * Fix it by uncharging from memsw. Basically, this SwapCache is stable
  1551. * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
  1552. * may call delete_from_swap_cache() before reach here.
  1553. */
  1554. if (do_swap_account && PageSwapCache(page)) {
  1555. swp_entry_t ent = {.val = page_private(page)};
  1556. unsigned short id;
  1557. struct mem_cgroup *memcg;
  1558. id = swap_cgroup_record(ent, 0);
  1559. rcu_read_lock();
  1560. memcg = mem_cgroup_lookup(id);
  1561. if (memcg) {
  1562. /*
  1563. * This recorded memcg can be obsolete one. So, avoid
  1564. * calling css_tryget
  1565. */
  1566. if (!mem_cgroup_is_root(memcg))
  1567. res_counter_uncharge(&memcg->memsw, PAGE_SIZE,
  1568. NULL);
  1569. mem_cgroup_swap_statistics(memcg, false);
  1570. mem_cgroup_put(memcg);
  1571. }
  1572. rcu_read_unlock();
  1573. }
  1574. /*
  1575. * At swapin, we may charge account against cgroup which has no tasks.
  1576. * So, rmdir()->pre_destroy() can be called while we do this charge.
  1577. * In that case, we need to call pre_destroy() again. check it here.
  1578. */
  1579. cgroup_release_and_wakeup_rmdir(&ptr->css);
  1580. }
  1581. void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr)
  1582. {
  1583. __mem_cgroup_commit_charge_swapin(page, ptr,
  1584. MEM_CGROUP_CHARGE_TYPE_MAPPED);
  1585. }
  1586. void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem)
  1587. {
  1588. if (mem_cgroup_disabled())
  1589. return;
  1590. if (!mem)
  1591. return;
  1592. if (!mem_cgroup_is_root(mem)) {
  1593. res_counter_uncharge(&mem->res, PAGE_SIZE, NULL);
  1594. if (do_swap_account)
  1595. res_counter_uncharge(&mem->memsw, PAGE_SIZE, NULL);
  1596. }
  1597. css_put(&mem->css);
  1598. }
  1599. /*
  1600. * uncharge if !page_mapped(page)
  1601. */
  1602. static struct mem_cgroup *
  1603. __mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
  1604. {
  1605. struct page_cgroup *pc;
  1606. struct mem_cgroup *mem = NULL;
  1607. struct mem_cgroup_per_zone *mz;
  1608. bool soft_limit_excess = false;
  1609. if (mem_cgroup_disabled())
  1610. return NULL;
  1611. if (PageSwapCache(page))
  1612. return NULL;
  1613. /*
  1614. * Check if our page_cgroup is valid
  1615. */
  1616. pc = lookup_page_cgroup(page);
  1617. if (unlikely(!pc || !PageCgroupUsed(pc)))
  1618. return NULL;
  1619. lock_page_cgroup(pc);
  1620. mem = pc->mem_cgroup;
  1621. if (!PageCgroupUsed(pc))
  1622. goto unlock_out;
  1623. switch (ctype) {
  1624. case MEM_CGROUP_CHARGE_TYPE_MAPPED:
  1625. case MEM_CGROUP_CHARGE_TYPE_DROP:
  1626. if (page_mapped(page))
  1627. goto unlock_out;
  1628. break;
  1629. case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
  1630. if (!PageAnon(page)) { /* Shared memory */
  1631. if (page->mapping && !page_is_file_cache(page))
  1632. goto unlock_out;
  1633. } else if (page_mapped(page)) /* Anon */
  1634. goto unlock_out;
  1635. break;
  1636. default:
  1637. break;
  1638. }
  1639. if (!mem_cgroup_is_root(mem)) {
  1640. res_counter_uncharge(&mem->res, PAGE_SIZE, &soft_limit_excess);
  1641. if (do_swap_account &&
  1642. (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT))
  1643. res_counter_uncharge(&mem->memsw, PAGE_SIZE, NULL);
  1644. }
  1645. if (ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
  1646. mem_cgroup_swap_statistics(mem, true);
  1647. mem_cgroup_charge_statistics(mem, pc, false);
  1648. ClearPageCgroupUsed(pc);
  1649. /*
  1650. * pc->mem_cgroup is not cleared here. It will be accessed when it's
  1651. * freed from LRU. This is safe because uncharged page is expected not
  1652. * to be reused (freed soon). Exception is SwapCache, it's handled by
  1653. * special functions.
  1654. */
  1655. mz = page_cgroup_zoneinfo(pc);
  1656. unlock_page_cgroup(pc);
  1657. if (soft_limit_excess && mem_cgroup_soft_limit_check(mem))
  1658. mem_cgroup_update_tree(mem, page);
  1659. /* at swapout, this memcg will be accessed to record to swap */
  1660. if (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
  1661. css_put(&mem->css);
  1662. return mem;
  1663. unlock_out:
  1664. unlock_page_cgroup(pc);
  1665. return NULL;
  1666. }
  1667. void mem_cgroup_uncharge_page(struct page *page)
  1668. {
  1669. /* early check. */
  1670. if (page_mapped(page))
  1671. return;
  1672. if (page->mapping && !PageAnon(page))
  1673. return;
  1674. __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
  1675. }
  1676. void mem_cgroup_uncharge_cache_page(struct page *page)
  1677. {
  1678. VM_BUG_ON(page_mapped(page));
  1679. VM_BUG_ON(page->mapping);
  1680. __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
  1681. }
  1682. #ifdef CONFIG_SWAP
  1683. /*
  1684. * called after __delete_from_swap_cache() and drop "page" account.
  1685. * memcg information is recorded to swap_cgroup of "ent"
  1686. */
  1687. void
  1688. mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
  1689. {
  1690. struct mem_cgroup *memcg;
  1691. int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT;
  1692. if (!swapout) /* this was a swap cache but the swap is unused ! */
  1693. ctype = MEM_CGROUP_CHARGE_TYPE_DROP;
  1694. memcg = __mem_cgroup_uncharge_common(page, ctype);
  1695. /* record memcg information */
  1696. if (do_swap_account && swapout && memcg) {
  1697. swap_cgroup_record(ent, css_id(&memcg->css));
  1698. mem_cgroup_get(memcg);
  1699. }
  1700. if (swapout && memcg)
  1701. css_put(&memcg->css);
  1702. }
  1703. #endif
  1704. #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
  1705. /*
  1706. * called from swap_entry_free(). remove record in swap_cgroup and
  1707. * uncharge "memsw" account.
  1708. */
  1709. void mem_cgroup_uncharge_swap(swp_entry_t ent)
  1710. {
  1711. struct mem_cgroup *memcg;
  1712. unsigned short id;
  1713. if (!do_swap_account)
  1714. return;
  1715. id = swap_cgroup_record(ent, 0);
  1716. rcu_read_lock();
  1717. memcg = mem_cgroup_lookup(id);
  1718. if (memcg) {
  1719. /*
  1720. * We uncharge this because swap is freed.
  1721. * This memcg can be obsolete one. We avoid calling css_tryget
  1722. */
  1723. if (!mem_cgroup_is_root(memcg))
  1724. res_counter_uncharge(&memcg->memsw, PAGE_SIZE, NULL);
  1725. mem_cgroup_swap_statistics(memcg, false);
  1726. mem_cgroup_put(memcg);
  1727. }
  1728. rcu_read_unlock();
  1729. }
  1730. #endif
  1731. /*
  1732. * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
  1733. * page belongs to.
  1734. */
  1735. int mem_cgroup_prepare_migration(struct page *page, struct mem_cgroup **ptr)
  1736. {
  1737. struct page_cgroup *pc;
  1738. struct mem_cgroup *mem = NULL;
  1739. int ret = 0;
  1740. if (mem_cgroup_disabled())
  1741. return 0;
  1742. pc = lookup_page_cgroup(page);
  1743. lock_page_cgroup(pc);
  1744. if (PageCgroupUsed(pc)) {
  1745. mem = pc->mem_cgroup;
  1746. css_get(&mem->css);
  1747. }
  1748. unlock_page_cgroup(pc);
  1749. if (mem) {
  1750. ret = __mem_cgroup_try_charge(NULL, GFP_KERNEL, &mem, false,
  1751. page);
  1752. css_put(&mem->css);
  1753. }
  1754. *ptr = mem;
  1755. return ret;
  1756. }
  1757. /* remove redundant charge if migration failed*/
  1758. void mem_cgroup_end_migration(struct mem_cgroup *mem,
  1759. struct page *oldpage, struct page *newpage)
  1760. {
  1761. struct page *target, *unused;
  1762. struct page_cgroup *pc;
  1763. enum charge_type ctype;
  1764. if (!mem)
  1765. return;
  1766. cgroup_exclude_rmdir(&mem->css);
  1767. /* at migration success, oldpage->mapping is NULL. */
  1768. if (oldpage->mapping) {
  1769. target = oldpage;
  1770. unused = NULL;
  1771. } else {
  1772. target = newpage;
  1773. unused = oldpage;
  1774. }
  1775. if (PageAnon(target))
  1776. ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
  1777. else if (page_is_file_cache(target))
  1778. ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
  1779. else
  1780. ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
  1781. /* unused page is not on radix-tree now. */
  1782. if (unused)
  1783. __mem_cgroup_uncharge_common(unused, ctype);
  1784. pc = lookup_page_cgroup(target);
  1785. /*
  1786. * __mem_cgroup_commit_charge() check PCG_USED bit of page_cgroup.
  1787. * So, double-counting is effectively avoided.
  1788. */
  1789. __mem_cgroup_commit_charge(mem, pc, ctype);
  1790. /*
  1791. * Both of oldpage and newpage are still under lock_page().
  1792. * Then, we don't have to care about race in radix-tree.
  1793. * But we have to be careful that this page is unmapped or not.
  1794. *
  1795. * There is a case for !page_mapped(). At the start of
  1796. * migration, oldpage was mapped. But now, it's zapped.
  1797. * But we know *target* page is not freed/reused under us.
  1798. * mem_cgroup_uncharge_page() does all necessary checks.
  1799. */
  1800. if (ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED)
  1801. mem_cgroup_uncharge_page(target);
  1802. /*
  1803. * At migration, we may charge account against cgroup which has no tasks
  1804. * So, rmdir()->pre_destroy() can be called while we do this charge.
  1805. * In that case, we need to call pre_destroy() again. check it here.
  1806. */
  1807. cgroup_release_and_wakeup_rmdir(&mem->css);
  1808. }
  1809. /*
  1810. * A call to try to shrink memory usage on charge failure at shmem's swapin.
  1811. * Calling hierarchical_reclaim is not enough because we should update
  1812. * last_oom_jiffies to prevent pagefault_out_of_memory from invoking global OOM.
  1813. * Moreover considering hierarchy, we should reclaim from the mem_over_limit,
  1814. * not from the memcg which this page would be charged to.
  1815. * try_charge_swapin does all of these works properly.
  1816. */
  1817. int mem_cgroup_shmem_charge_fallback(struct page *page,
  1818. struct mm_struct *mm,
  1819. gfp_t gfp_mask)
  1820. {
  1821. struct mem_cgroup *mem = NULL;
  1822. int ret;
  1823. if (mem_cgroup_disabled())
  1824. return 0;
  1825. ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
  1826. if (!ret)
  1827. mem_cgroup_cancel_charge_swapin(mem); /* it does !mem check */
  1828. return ret;
  1829. }
  1830. static DEFINE_MUTEX(set_limit_mutex);
  1831. static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
  1832. unsigned long long val)
  1833. {
  1834. int retry_count;
  1835. int progress;
  1836. u64 memswlimit;
  1837. int ret = 0;
  1838. int children = mem_cgroup_count_children(memcg);
  1839. u64 curusage, oldusage;
  1840. /*
  1841. * For keeping hierarchical_reclaim simple, how long we should retry
  1842. * is depends on callers. We set our retry-count to be function
  1843. * of # of children which we should visit in this loop.
  1844. */
  1845. retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
  1846. oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
  1847. while (retry_count) {
  1848. if (signal_pending(current)) {
  1849. ret = -EINTR;
  1850. break;
  1851. }
  1852. /*
  1853. * Rather than hide all in some function, I do this in
  1854. * open coded manner. You see what this really does.
  1855. * We have to guarantee mem->res.limit < mem->memsw.limit.
  1856. */
  1857. mutex_lock(&set_limit_mutex);
  1858. memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
  1859. if (memswlimit < val) {
  1860. ret = -EINVAL;
  1861. mutex_unlock(&set_limit_mutex);
  1862. break;
  1863. }
  1864. ret = res_counter_set_limit(&memcg->res, val);
  1865. if (!ret) {
  1866. if (memswlimit == val)
  1867. memcg->memsw_is_minimum = true;
  1868. else
  1869. memcg->memsw_is_minimum = false;
  1870. }
  1871. mutex_unlock(&set_limit_mutex);
  1872. if (!ret)
  1873. break;
  1874. progress = mem_cgroup_hierarchical_reclaim(memcg, NULL,
  1875. GFP_KERNEL,
  1876. MEM_CGROUP_RECLAIM_SHRINK);
  1877. curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
  1878. /* Usage is reduced ? */
  1879. if (curusage >= oldusage)
  1880. retry_count--;
  1881. else
  1882. oldusage = curusage;
  1883. }
  1884. return ret;
  1885. }
  1886. static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
  1887. unsigned long long val)
  1888. {
  1889. int retry_count;
  1890. u64 memlimit, oldusage, curusage;
  1891. int children = mem_cgroup_count_children(memcg);
  1892. int ret = -EBUSY;
  1893. /* see mem_cgroup_resize_res_limit */
  1894. retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
  1895. oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
  1896. while (retry_count) {
  1897. if (signal_pending(current)) {
  1898. ret = -EINTR;
  1899. break;
  1900. }
  1901. /*
  1902. * Rather than hide all in some function, I do this in
  1903. * open coded manner. You see what this really does.
  1904. * We have to guarantee mem->res.limit < mem->memsw.limit.
  1905. */
  1906. mutex_lock(&set_limit_mutex);
  1907. memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
  1908. if (memlimit > val) {
  1909. ret = -EINVAL;
  1910. mutex_unlock(&set_limit_mutex);
  1911. break;
  1912. }
  1913. ret = res_counter_set_limit(&memcg->memsw, val);
  1914. if (!ret) {
  1915. if (memlimit == val)
  1916. memcg->memsw_is_minimum = true;
  1917. else
  1918. memcg->memsw_is_minimum = false;
  1919. }
  1920. mutex_unlock(&set_limit_mutex);
  1921. if (!ret)
  1922. break;
  1923. mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL,
  1924. MEM_CGROUP_RECLAIM_NOSWAP |
  1925. MEM_CGROUP_RECLAIM_SHRINK);
  1926. curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
  1927. /* Usage is reduced ? */
  1928. if (curusage >= oldusage)
  1929. retry_count--;
  1930. else
  1931. oldusage = curusage;
  1932. }
  1933. return ret;
  1934. }
  1935. unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
  1936. gfp_t gfp_mask, int nid,
  1937. int zid)
  1938. {
  1939. unsigned long nr_reclaimed = 0;
  1940. struct mem_cgroup_per_zone *mz, *next_mz = NULL;
  1941. unsigned long reclaimed;
  1942. int loop = 0;
  1943. struct mem_cgroup_tree_per_zone *mctz;
  1944. if (order > 0)
  1945. return 0;
  1946. mctz = soft_limit_tree_node_zone(nid, zid);
  1947. /*
  1948. * This loop can run a while, specially if mem_cgroup's continuously
  1949. * keep exceeding their soft limit and putting the system under
  1950. * pressure
  1951. */
  1952. do {
  1953. if (next_mz)
  1954. mz = next_mz;
  1955. else
  1956. mz = mem_cgroup_largest_soft_limit_node(mctz);
  1957. if (!mz)
  1958. break;
  1959. reclaimed = mem_cgroup_hierarchical_reclaim(mz->mem, zone,
  1960. gfp_mask,
  1961. MEM_CGROUP_RECLAIM_SOFT);
  1962. nr_reclaimed += reclaimed;
  1963. spin_lock(&mctz->lock);
  1964. /*
  1965. * If we failed to reclaim anything from this memory cgroup
  1966. * it is time to move on to the next cgroup
  1967. */
  1968. next_mz = NULL;
  1969. if (!reclaimed) {
  1970. do {
  1971. /*
  1972. * Loop until we find yet another one.
  1973. *
  1974. * By the time we get the soft_limit lock
  1975. * again, someone might have aded the
  1976. * group back on the RB tree. Iterate to
  1977. * make sure we get a different mem.
  1978. * mem_cgroup_largest_soft_limit_node returns
  1979. * NULL if no other cgroup is present on
  1980. * the tree
  1981. */
  1982. next_mz =
  1983. __mem_cgroup_largest_soft_limit_node(mctz);
  1984. if (next_mz == mz) {
  1985. css_put(&next_mz->mem->css);
  1986. next_mz = NULL;
  1987. } else /* next_mz == NULL or other memcg */
  1988. break;
  1989. } while (1);
  1990. }
  1991. mz->usage_in_excess =
  1992. res_counter_soft_limit_excess(&mz->mem->res);
  1993. __mem_cgroup_remove_exceeded(mz->mem, mz, mctz);
  1994. /*
  1995. * One school of thought says that we should not add
  1996. * back the node to the tree if reclaim returns 0.
  1997. * But our reclaim could return 0, simply because due
  1998. * to priority we are exposing a smaller subset of
  1999. * memory to reclaim from. Consider this as a longer
  2000. * term TODO.
  2001. */
  2002. if (mz->usage_in_excess)
  2003. __mem_cgroup_insert_exceeded(mz->mem, mz, mctz);
  2004. spin_unlock(&mctz->lock);
  2005. css_put(&mz->mem->css);
  2006. loop++;
  2007. /*
  2008. * Could not reclaim anything and there are no more
  2009. * mem cgroups to try or we seem to be looping without
  2010. * reclaiming anything.
  2011. */
  2012. if (!nr_reclaimed &&
  2013. (next_mz == NULL ||
  2014. loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS))
  2015. break;
  2016. } while (!nr_reclaimed);
  2017. if (next_mz)
  2018. css_put(&next_mz->mem->css);
  2019. return nr_reclaimed;
  2020. }
  2021. /*
  2022. * This routine traverse page_cgroup in given list and drop them all.
  2023. * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
  2024. */
  2025. static int mem_cgroup_force_empty_list(struct mem_cgroup *mem,
  2026. int node, int zid, enum lru_list lru)
  2027. {
  2028. struct zone *zone;
  2029. struct mem_cgroup_per_zone *mz;
  2030. struct page_cgroup *pc, *busy;
  2031. unsigned long flags, loop;
  2032. struct list_head *list;
  2033. int ret = 0;
  2034. zone = &NODE_DATA(node)->node_zones[zid];
  2035. mz = mem_cgroup_zoneinfo(mem, node, zid);
  2036. list = &mz->lists[lru];
  2037. loop = MEM_CGROUP_ZSTAT(mz, lru);
  2038. /* give some margin against EBUSY etc...*/
  2039. loop += 256;
  2040. busy = NULL;
  2041. while (loop--) {
  2042. ret = 0;
  2043. spin_lock_irqsave(&zone->lru_lock, flags);
  2044. if (list_empty(list)) {
  2045. spin_unlock_irqrestore(&zone->lru_lock, flags);
  2046. break;
  2047. }
  2048. pc = list_entry(list->prev, struct page_cgroup, lru);
  2049. if (busy == pc) {
  2050. list_move(&pc->lru, list);
  2051. busy = 0;
  2052. spin_unlock_irqrestore(&zone->lru_lock, flags);
  2053. continue;
  2054. }
  2055. spin_unlock_irqrestore(&zone->lru_lock, flags);
  2056. ret = mem_cgroup_move_parent(pc, mem, GFP_KERNEL);
  2057. if (ret == -ENOMEM)
  2058. break;
  2059. if (ret == -EBUSY || ret == -EINVAL) {
  2060. /* found lock contention or "pc" is obsolete. */
  2061. busy = pc;
  2062. cond_resched();
  2063. } else
  2064. busy = NULL;
  2065. }
  2066. if (!ret && !list_empty(list))
  2067. return -EBUSY;
  2068. return ret;
  2069. }
  2070. /*
  2071. * make mem_cgroup's charge to be 0 if there is no task.
  2072. * This enables deleting this mem_cgroup.
  2073. */
  2074. static int mem_cgroup_force_empty(struct mem_cgroup *mem, bool free_all)
  2075. {
  2076. int ret;
  2077. int node, zid, shrink;
  2078. int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
  2079. struct cgroup *cgrp = mem->css.cgroup;
  2080. css_get(&mem->css);
  2081. shrink = 0;
  2082. /* should free all ? */
  2083. if (free_all)
  2084. goto try_to_free;
  2085. move_account:
  2086. while (mem->res.usage > 0) {
  2087. ret = -EBUSY;
  2088. if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
  2089. goto out;
  2090. ret = -EINTR;
  2091. if (signal_pending(current))
  2092. goto out;
  2093. /* This is for making all *used* pages to be on LRU. */
  2094. lru_add_drain_all();
  2095. ret = 0;
  2096. for_each_node_state(node, N_HIGH_MEMORY) {
  2097. for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) {
  2098. enum lru_list l;
  2099. for_each_lru(l) {
  2100. ret = mem_cgroup_force_empty_list(mem,
  2101. node, zid, l);
  2102. if (ret)
  2103. break;
  2104. }
  2105. }
  2106. if (ret)
  2107. break;
  2108. }
  2109. /* it seems parent cgroup doesn't have enough mem */
  2110. if (ret == -ENOMEM)
  2111. goto try_to_free;
  2112. cond_resched();
  2113. }
  2114. ret = 0;
  2115. out:
  2116. css_put(&mem->css);
  2117. return ret;
  2118. try_to_free:
  2119. /* returns EBUSY if there is a task or if we come here twice. */
  2120. if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) {
  2121. ret = -EBUSY;
  2122. goto out;
  2123. }
  2124. /* we call try-to-free pages for make this cgroup empty */
  2125. lru_add_drain_all();
  2126. /* try to free all pages in this cgroup */
  2127. shrink = 1;
  2128. while (nr_retries && mem->res.usage > 0) {
  2129. int progress;
  2130. if (signal_pending(current)) {
  2131. ret = -EINTR;
  2132. goto out;
  2133. }
  2134. progress = try_to_free_mem_cgroup_pages(mem, GFP_KERNEL,
  2135. false, get_swappiness(mem));
  2136. if (!progress) {
  2137. nr_retries--;
  2138. /* maybe some writeback is necessary */
  2139. congestion_wait(BLK_RW_ASYNC, HZ/10);
  2140. }
  2141. }
  2142. lru_add_drain();
  2143. /* try move_account...there may be some *locked* pages. */
  2144. if (mem->res.usage)
  2145. goto move_account;
  2146. ret = 0;
  2147. goto out;
  2148. }
  2149. int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
  2150. {
  2151. return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true);
  2152. }
  2153. static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft)
  2154. {
  2155. return mem_cgroup_from_cont(cont)->use_hierarchy;
  2156. }
  2157. static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft,
  2158. u64 val)
  2159. {
  2160. int retval = 0;
  2161. struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
  2162. struct cgroup *parent = cont->parent;
  2163. struct mem_cgroup *parent_mem = NULL;
  2164. if (parent)
  2165. parent_mem = mem_cgroup_from_cont(parent);
  2166. cgroup_lock();
  2167. /*
  2168. * If parent's use_hiearchy is set, we can't make any modifications
  2169. * in the child subtrees. If it is unset, then the change can
  2170. * occur, provided the current cgroup has no children.
  2171. *
  2172. * For the root cgroup, parent_mem is NULL, we allow value to be
  2173. * set if there are no children.
  2174. */
  2175. if ((!parent_mem || !parent_mem->use_hierarchy) &&
  2176. (val == 1 || val == 0)) {
  2177. if (list_empty(&cont->children))
  2178. mem->use_hierarchy = val;
  2179. else
  2180. retval = -EBUSY;
  2181. } else
  2182. retval = -EINVAL;
  2183. cgroup_unlock();
  2184. return retval;
  2185. }
  2186. struct mem_cgroup_idx_data {
  2187. s64 val;
  2188. enum mem_cgroup_stat_index idx;
  2189. };
  2190. static int
  2191. mem_cgroup_get_idx_stat(struct mem_cgroup *mem, void *data)
  2192. {
  2193. struct mem_cgroup_idx_data *d = data;
  2194. d->val += mem_cgroup_read_stat(&mem->stat, d->idx);
  2195. return 0;
  2196. }
  2197. static void
  2198. mem_cgroup_get_recursive_idx_stat(struct mem_cgroup *mem,
  2199. enum mem_cgroup_stat_index idx, s64 *val)
  2200. {
  2201. struct mem_cgroup_idx_data d;
  2202. d.idx = idx;
  2203. d.val = 0;
  2204. mem_cgroup_walk_tree(mem, &d, mem_cgroup_get_idx_stat);
  2205. *val = d.val;
  2206. }
  2207. static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
  2208. {
  2209. struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
  2210. u64 idx_val, val;
  2211. int type, name;
  2212. type = MEMFILE_TYPE(cft->private);
  2213. name = MEMFILE_ATTR(cft->private);
  2214. switch (type) {
  2215. case _MEM:
  2216. if (name == RES_USAGE && mem_cgroup_is_root(mem)) {
  2217. mem_cgroup_get_recursive_idx_stat(mem,
  2218. MEM_CGROUP_STAT_CACHE, &idx_val);
  2219. val = idx_val;
  2220. mem_cgroup_get_recursive_idx_stat(mem,
  2221. MEM_CGROUP_STAT_RSS, &idx_val);
  2222. val += idx_val;
  2223. val <<= PAGE_SHIFT;
  2224. } else
  2225. val = res_counter_read_u64(&mem->res, name);
  2226. break;
  2227. case _MEMSWAP:
  2228. if (name == RES_USAGE && mem_cgroup_is_root(mem)) {
  2229. mem_cgroup_get_recursive_idx_stat(mem,
  2230. MEM_CGROUP_STAT_CACHE, &idx_val);
  2231. val = idx_val;
  2232. mem_cgroup_get_recursive_idx_stat(mem,
  2233. MEM_CGROUP_STAT_RSS, &idx_val);
  2234. val += idx_val;
  2235. mem_cgroup_get_recursive_idx_stat(mem,
  2236. MEM_CGROUP_STAT_SWAPOUT, &idx_val);
  2237. val <<= PAGE_SHIFT;
  2238. } else
  2239. val = res_counter_read_u64(&mem->memsw, name);
  2240. break;
  2241. default:
  2242. BUG();
  2243. break;
  2244. }
  2245. return val;
  2246. }
  2247. /*
  2248. * The user of this function is...
  2249. * RES_LIMIT.
  2250. */
  2251. static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
  2252. const char *buffer)
  2253. {
  2254. struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
  2255. int type, name;
  2256. unsigned long long val;
  2257. int ret;
  2258. type = MEMFILE_TYPE(cft->private);
  2259. name = MEMFILE_ATTR(cft->private);
  2260. switch (name) {
  2261. case RES_LIMIT:
  2262. if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
  2263. ret = -EINVAL;
  2264. break;
  2265. }
  2266. /* This function does all necessary parse...reuse it */
  2267. ret = res_counter_memparse_write_strategy(buffer, &val);
  2268. if (ret)
  2269. break;
  2270. if (type == _MEM)
  2271. ret = mem_cgroup_resize_limit(memcg, val);
  2272. else
  2273. ret = mem_cgroup_resize_memsw_limit(memcg, val);
  2274. break;
  2275. case RES_SOFT_LIMIT:
  2276. ret = res_counter_memparse_write_strategy(buffer, &val);
  2277. if (ret)
  2278. break;
  2279. /*
  2280. * For memsw, soft limits are hard to implement in terms
  2281. * of semantics, for now, we support soft limits for
  2282. * control without swap
  2283. */
  2284. if (type == _MEM)
  2285. ret = res_counter_set_soft_limit(&memcg->res, val);
  2286. else
  2287. ret = -EINVAL;
  2288. break;
  2289. default:
  2290. ret = -EINVAL; /* should be BUG() ? */
  2291. break;
  2292. }
  2293. return ret;
  2294. }
  2295. static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
  2296. unsigned long long *mem_limit, unsigned long long *memsw_limit)
  2297. {
  2298. struct cgroup *cgroup;
  2299. unsigned long long min_limit, min_memsw_limit, tmp;
  2300. min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
  2301. min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
  2302. cgroup = memcg->css.cgroup;
  2303. if (!memcg->use_hierarchy)
  2304. goto out;
  2305. while (cgroup->parent) {
  2306. cgroup = cgroup->parent;
  2307. memcg = mem_cgroup_from_cont(cgroup);
  2308. if (!memcg->use_hierarchy)
  2309. break;
  2310. tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
  2311. min_limit = min(min_limit, tmp);
  2312. tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
  2313. min_memsw_limit = min(min_memsw_limit, tmp);
  2314. }
  2315. out:
  2316. *mem_limit = min_limit;
  2317. *memsw_limit = min_memsw_limit;
  2318. return;
  2319. }
  2320. static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
  2321. {
  2322. struct mem_cgroup *mem;
  2323. int type, name;
  2324. mem = mem_cgroup_from_cont(cont);
  2325. type = MEMFILE_TYPE(event);
  2326. name = MEMFILE_ATTR(event);
  2327. switch (name) {
  2328. case RES_MAX_USAGE:
  2329. if (type == _MEM)
  2330. res_counter_reset_max(&mem->res);
  2331. else
  2332. res_counter_reset_max(&mem->memsw);
  2333. break;
  2334. case RES_FAILCNT:
  2335. if (type == _MEM)
  2336. res_counter_reset_failcnt(&mem->res);
  2337. else
  2338. res_counter_reset_failcnt(&mem->memsw);
  2339. break;
  2340. }
  2341. return 0;
  2342. }
  2343. /* For read statistics */
  2344. enum {
  2345. MCS_CACHE,
  2346. MCS_RSS,
  2347. MCS_MAPPED_FILE,
  2348. MCS_PGPGIN,
  2349. MCS_PGPGOUT,
  2350. MCS_SWAP,
  2351. MCS_INACTIVE_ANON,
  2352. MCS_ACTIVE_ANON,
  2353. MCS_INACTIVE_FILE,
  2354. MCS_ACTIVE_FILE,
  2355. MCS_UNEVICTABLE,
  2356. NR_MCS_STAT,
  2357. };
  2358. struct mcs_total_stat {
  2359. s64 stat[NR_MCS_STAT];
  2360. };
  2361. struct {
  2362. char *local_name;
  2363. char *total_name;
  2364. } memcg_stat_strings[NR_MCS_STAT] = {
  2365. {"cache", "total_cache"},
  2366. {"rss", "total_rss"},
  2367. {"mapped_file", "total_mapped_file"},
  2368. {"pgpgin", "total_pgpgin"},
  2369. {"pgpgout", "total_pgpgout"},
  2370. {"swap", "total_swap"},
  2371. {"inactive_anon", "total_inactive_anon"},
  2372. {"active_anon", "total_active_anon"},
  2373. {"inactive_file", "total_inactive_file"},
  2374. {"active_file", "total_active_file"},
  2375. {"unevictable", "total_unevictable"}
  2376. };
  2377. static int mem_cgroup_get_local_stat(struct mem_cgroup *mem, void *data)
  2378. {
  2379. struct mcs_total_stat *s = data;
  2380. s64 val;
  2381. /* per cpu stat */
  2382. val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_CACHE);
  2383. s->stat[MCS_CACHE] += val * PAGE_SIZE;
  2384. val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS);
  2385. s->stat[MCS_RSS] += val * PAGE_SIZE;
  2386. val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_MAPPED_FILE);
  2387. s->stat[MCS_MAPPED_FILE] += val * PAGE_SIZE;
  2388. val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_PGPGIN_COUNT);
  2389. s->stat[MCS_PGPGIN] += val;
  2390. val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_PGPGOUT_COUNT);
  2391. s->stat[MCS_PGPGOUT] += val;
  2392. if (do_swap_account) {
  2393. val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_SWAPOUT);
  2394. s->stat[MCS_SWAP] += val * PAGE_SIZE;
  2395. }
  2396. /* per zone stat */
  2397. val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_ANON);
  2398. s->stat[MCS_INACTIVE_ANON] += val * PAGE_SIZE;
  2399. val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_ANON);
  2400. s->stat[MCS_ACTIVE_ANON] += val * PAGE_SIZE;
  2401. val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_FILE);
  2402. s->stat[MCS_INACTIVE_FILE] += val * PAGE_SIZE;
  2403. val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_FILE);
  2404. s->stat[MCS_ACTIVE_FILE] += val * PAGE_SIZE;
  2405. val = mem_cgroup_get_local_zonestat(mem, LRU_UNEVICTABLE);
  2406. s->stat[MCS_UNEVICTABLE] += val * PAGE_SIZE;
  2407. return 0;
  2408. }
  2409. static void
  2410. mem_cgroup_get_total_stat(struct mem_cgroup *mem, struct mcs_total_stat *s)
  2411. {
  2412. mem_cgroup_walk_tree(mem, s, mem_cgroup_get_local_stat);
  2413. }
  2414. static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
  2415. struct cgroup_map_cb *cb)
  2416. {
  2417. struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
  2418. struct mcs_total_stat mystat;
  2419. int i;
  2420. memset(&mystat, 0, sizeof(mystat));
  2421. mem_cgroup_get_local_stat(mem_cont, &mystat);
  2422. for (i = 0; i < NR_MCS_STAT; i++) {
  2423. if (i == MCS_SWAP && !do_swap_account)
  2424. continue;
  2425. cb->fill(cb, memcg_stat_strings[i].local_name, mystat.stat[i]);
  2426. }
  2427. /* Hierarchical information */
  2428. {
  2429. unsigned long long limit, memsw_limit;
  2430. memcg_get_hierarchical_limit(mem_cont, &limit, &memsw_limit);
  2431. cb->fill(cb, "hierarchical_memory_limit", limit);
  2432. if (do_swap_account)
  2433. cb->fill(cb, "hierarchical_memsw_limit", memsw_limit);
  2434. }
  2435. memset(&mystat, 0, sizeof(mystat));
  2436. mem_cgroup_get_total_stat(mem_cont, &mystat);
  2437. for (i = 0; i < NR_MCS_STAT; i++) {
  2438. if (i == MCS_SWAP && !do_swap_account)
  2439. continue;
  2440. cb->fill(cb, memcg_stat_strings[i].total_name, mystat.stat[i]);
  2441. }
  2442. #ifdef CONFIG_DEBUG_VM
  2443. cb->fill(cb, "inactive_ratio", calc_inactive_ratio(mem_cont, NULL));
  2444. {
  2445. int nid, zid;
  2446. struct mem_cgroup_per_zone *mz;
  2447. unsigned long recent_rotated[2] = {0, 0};
  2448. unsigned long recent_scanned[2] = {0, 0};
  2449. for_each_online_node(nid)
  2450. for (zid = 0; zid < MAX_NR_ZONES; zid++) {
  2451. mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
  2452. recent_rotated[0] +=
  2453. mz->reclaim_stat.recent_rotated[0];
  2454. recent_rotated[1] +=
  2455. mz->reclaim_stat.recent_rotated[1];
  2456. recent_scanned[0] +=
  2457. mz->reclaim_stat.recent_scanned[0];
  2458. recent_scanned[1] +=
  2459. mz->reclaim_stat.recent_scanned[1];
  2460. }
  2461. cb->fill(cb, "recent_rotated_anon", recent_rotated[0]);
  2462. cb->fill(cb, "recent_rotated_file", recent_rotated[1]);
  2463. cb->fill(cb, "recent_scanned_anon", recent_scanned[0]);
  2464. cb->fill(cb, "recent_scanned_file", recent_scanned[1]);
  2465. }
  2466. #endif
  2467. return 0;
  2468. }
  2469. static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft)
  2470. {
  2471. struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
  2472. return get_swappiness(memcg);
  2473. }
  2474. static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
  2475. u64 val)
  2476. {
  2477. struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
  2478. struct mem_cgroup *parent;
  2479. if (val > 100)
  2480. return -EINVAL;
  2481. if (cgrp->parent == NULL)
  2482. return -EINVAL;
  2483. parent = mem_cgroup_from_cont(cgrp->parent);
  2484. cgroup_lock();
  2485. /* If under hierarchy, only empty-root can set this value */
  2486. if ((parent->use_hierarchy) ||
  2487. (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
  2488. cgroup_unlock();
  2489. return -EINVAL;
  2490. }
  2491. spin_lock(&memcg->reclaim_param_lock);
  2492. memcg->swappiness = val;
  2493. spin_unlock(&memcg->reclaim_param_lock);
  2494. cgroup_unlock();
  2495. return 0;
  2496. }
  2497. static struct cftype mem_cgroup_files[] = {
  2498. {
  2499. .name = "usage_in_bytes",
  2500. .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
  2501. .read_u64 = mem_cgroup_read,
  2502. },
  2503. {
  2504. .name = "max_usage_in_bytes",
  2505. .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
  2506. .trigger = mem_cgroup_reset,
  2507. .read_u64 = mem_cgroup_read,
  2508. },
  2509. {
  2510. .name = "limit_in_bytes",
  2511. .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
  2512. .write_string = mem_cgroup_write,
  2513. .read_u64 = mem_cgroup_read,
  2514. },
  2515. {
  2516. .name = "soft_limit_in_bytes",
  2517. .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
  2518. .write_string = mem_cgroup_write,
  2519. .read_u64 = mem_cgroup_read,
  2520. },
  2521. {
  2522. .name = "failcnt",
  2523. .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
  2524. .trigger = mem_cgroup_reset,
  2525. .read_u64 = mem_cgroup_read,
  2526. },
  2527. {
  2528. .name = "stat",
  2529. .read_map = mem_control_stat_show,
  2530. },
  2531. {
  2532. .name = "force_empty",
  2533. .trigger = mem_cgroup_force_empty_write,
  2534. },
  2535. {
  2536. .name = "use_hierarchy",
  2537. .write_u64 = mem_cgroup_hierarchy_write,
  2538. .read_u64 = mem_cgroup_hierarchy_read,
  2539. },
  2540. {
  2541. .name = "swappiness",
  2542. .read_u64 = mem_cgroup_swappiness_read,
  2543. .write_u64 = mem_cgroup_swappiness_write,
  2544. },
  2545. };
  2546. #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
  2547. static struct cftype memsw_cgroup_files[] = {
  2548. {
  2549. .name = "memsw.usage_in_bytes",
  2550. .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
  2551. .read_u64 = mem_cgroup_read,
  2552. },
  2553. {
  2554. .name = "memsw.max_usage_in_bytes",
  2555. .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
  2556. .trigger = mem_cgroup_reset,
  2557. .read_u64 = mem_cgroup_read,
  2558. },
  2559. {
  2560. .name = "memsw.limit_in_bytes",
  2561. .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
  2562. .write_string = mem_cgroup_write,
  2563. .read_u64 = mem_cgroup_read,
  2564. },
  2565. {
  2566. .name = "memsw.failcnt",
  2567. .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
  2568. .trigger = mem_cgroup_reset,
  2569. .read_u64 = mem_cgroup_read,
  2570. },
  2571. };
  2572. static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
  2573. {
  2574. if (!do_swap_account)
  2575. return 0;
  2576. return cgroup_add_files(cont, ss, memsw_cgroup_files,
  2577. ARRAY_SIZE(memsw_cgroup_files));
  2578. };
  2579. #else
  2580. static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
  2581. {
  2582. return 0;
  2583. }
  2584. #endif
  2585. static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
  2586. {
  2587. struct mem_cgroup_per_node *pn;
  2588. struct mem_cgroup_per_zone *mz;
  2589. enum lru_list l;
  2590. int zone, tmp = node;
  2591. /*
  2592. * This routine is called against possible nodes.
  2593. * But it's BUG to call kmalloc() against offline node.
  2594. *
  2595. * TODO: this routine can waste much memory for nodes which will
  2596. * never be onlined. It's better to use memory hotplug callback
  2597. * function.
  2598. */
  2599. if (!node_state(node, N_NORMAL_MEMORY))
  2600. tmp = -1;
  2601. pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
  2602. if (!pn)
  2603. return 1;
  2604. mem->info.nodeinfo[node] = pn;
  2605. memset(pn, 0, sizeof(*pn));
  2606. for (zone = 0; zone < MAX_NR_ZONES; zone++) {
  2607. mz = &pn->zoneinfo[zone];
  2608. for_each_lru(l)
  2609. INIT_LIST_HEAD(&mz->lists[l]);
  2610. mz->usage_in_excess = 0;
  2611. mz->on_tree = false;
  2612. mz->mem = mem;
  2613. }
  2614. return 0;
  2615. }
  2616. static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
  2617. {
  2618. kfree(mem->info.nodeinfo[node]);
  2619. }
  2620. static int mem_cgroup_size(void)
  2621. {
  2622. int cpustat_size = nr_cpu_ids * sizeof(struct mem_cgroup_stat_cpu);
  2623. return sizeof(struct mem_cgroup) + cpustat_size;
  2624. }
  2625. static struct mem_cgroup *mem_cgroup_alloc(void)
  2626. {
  2627. struct mem_cgroup *mem;
  2628. int size = mem_cgroup_size();
  2629. if (size < PAGE_SIZE)
  2630. mem = kmalloc(size, GFP_KERNEL);
  2631. else
  2632. mem = vmalloc(size);
  2633. if (mem)
  2634. memset(mem, 0, size);
  2635. return mem;
  2636. }
  2637. /*
  2638. * At destroying mem_cgroup, references from swap_cgroup can remain.
  2639. * (scanning all at force_empty is too costly...)
  2640. *
  2641. * Instead of clearing all references at force_empty, we remember
  2642. * the number of reference from swap_cgroup and free mem_cgroup when
  2643. * it goes down to 0.
  2644. *
  2645. * Removal of cgroup itself succeeds regardless of refs from swap.
  2646. */
  2647. static void __mem_cgroup_free(struct mem_cgroup *mem)
  2648. {
  2649. int node;
  2650. mem_cgroup_remove_from_trees(mem);
  2651. free_css_id(&mem_cgroup_subsys, &mem->css);
  2652. for_each_node_state(node, N_POSSIBLE)
  2653. free_mem_cgroup_per_zone_info(mem, node);
  2654. if (mem_cgroup_size() < PAGE_SIZE)
  2655. kfree(mem);
  2656. else
  2657. vfree(mem);
  2658. }
  2659. static void mem_cgroup_get(struct mem_cgroup *mem)
  2660. {
  2661. atomic_inc(&mem->refcnt);
  2662. }
  2663. static void mem_cgroup_put(struct mem_cgroup *mem)
  2664. {
  2665. if (atomic_dec_and_test(&mem->refcnt)) {
  2666. struct mem_cgroup *parent = parent_mem_cgroup(mem);
  2667. __mem_cgroup_free(mem);
  2668. if (parent)
  2669. mem_cgroup_put(parent);
  2670. }
  2671. }
  2672. /*
  2673. * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
  2674. */
  2675. static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem)
  2676. {
  2677. if (!mem->res.parent)
  2678. return NULL;
  2679. return mem_cgroup_from_res_counter(mem->res.parent, res);
  2680. }
  2681. #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
  2682. static void __init enable_swap_cgroup(void)
  2683. {
  2684. if (!mem_cgroup_disabled() && really_do_swap_account)
  2685. do_swap_account = 1;
  2686. }
  2687. #else
  2688. static void __init enable_swap_cgroup(void)
  2689. {
  2690. }
  2691. #endif
  2692. static int mem_cgroup_soft_limit_tree_init(void)
  2693. {
  2694. struct mem_cgroup_tree_per_node *rtpn;
  2695. struct mem_cgroup_tree_per_zone *rtpz;
  2696. int tmp, node, zone;
  2697. for_each_node_state(node, N_POSSIBLE) {
  2698. tmp = node;
  2699. if (!node_state(node, N_NORMAL_MEMORY))
  2700. tmp = -1;
  2701. rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp);
  2702. if (!rtpn)
  2703. return 1;
  2704. soft_limit_tree.rb_tree_per_node[node] = rtpn;
  2705. for (zone = 0; zone < MAX_NR_ZONES; zone++) {
  2706. rtpz = &rtpn->rb_tree_per_zone[zone];
  2707. rtpz->rb_root = RB_ROOT;
  2708. spin_lock_init(&rtpz->lock);
  2709. }
  2710. }
  2711. return 0;
  2712. }
  2713. static struct cgroup_subsys_state * __ref
  2714. mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
  2715. {
  2716. struct mem_cgroup *mem, *parent;
  2717. long error = -ENOMEM;
  2718. int node;
  2719. mem = mem_cgroup_alloc();
  2720. if (!mem)
  2721. return ERR_PTR(error);
  2722. for_each_node_state(node, N_POSSIBLE)
  2723. if (alloc_mem_cgroup_per_zone_info(mem, node))
  2724. goto free_out;
  2725. /* root ? */
  2726. if (cont->parent == NULL) {
  2727. enable_swap_cgroup();
  2728. parent = NULL;
  2729. root_mem_cgroup = mem;
  2730. if (mem_cgroup_soft_limit_tree_init())
  2731. goto free_out;
  2732. } else {
  2733. parent = mem_cgroup_from_cont(cont->parent);
  2734. mem->use_hierarchy = parent->use_hierarchy;
  2735. }
  2736. if (parent && parent->use_hierarchy) {
  2737. res_counter_init(&mem->res, &parent->res);
  2738. res_counter_init(&mem->memsw, &parent->memsw);
  2739. /*
  2740. * We increment refcnt of the parent to ensure that we can
  2741. * safely access it on res_counter_charge/uncharge.
  2742. * This refcnt will be decremented when freeing this
  2743. * mem_cgroup(see mem_cgroup_put).
  2744. */
  2745. mem_cgroup_get(parent);
  2746. } else {
  2747. res_counter_init(&mem->res, NULL);
  2748. res_counter_init(&mem->memsw, NULL);
  2749. }
  2750. mem->last_scanned_child = 0;
  2751. spin_lock_init(&mem->reclaim_param_lock);
  2752. if (parent)
  2753. mem->swappiness = get_swappiness(parent);
  2754. atomic_set(&mem->refcnt, 1);
  2755. return &mem->css;
  2756. free_out:
  2757. __mem_cgroup_free(mem);
  2758. root_mem_cgroup = NULL;
  2759. return ERR_PTR(error);
  2760. }
  2761. static int mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
  2762. struct cgroup *cont)
  2763. {
  2764. struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
  2765. return mem_cgroup_force_empty(mem, false);
  2766. }
  2767. static void mem_cgroup_destroy(struct cgroup_subsys *ss,
  2768. struct cgroup *cont)
  2769. {
  2770. struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
  2771. mem_cgroup_put(mem);
  2772. }
  2773. static int mem_cgroup_populate(struct cgroup_subsys *ss,
  2774. struct cgroup *cont)
  2775. {
  2776. int ret;
  2777. ret = cgroup_add_files(cont, ss, mem_cgroup_files,
  2778. ARRAY_SIZE(mem_cgroup_files));
  2779. if (!ret)
  2780. ret = register_memsw_files(cont, ss);
  2781. return ret;
  2782. }
  2783. static void mem_cgroup_move_task(struct cgroup_subsys *ss,
  2784. struct cgroup *cont,
  2785. struct cgroup *old_cont,
  2786. struct task_struct *p,
  2787. bool threadgroup)
  2788. {
  2789. mutex_lock(&memcg_tasklist);
  2790. /*
  2791. * FIXME: It's better to move charges of this process from old
  2792. * memcg to new memcg. But it's just on TODO-List now.
  2793. */
  2794. mutex_unlock(&memcg_tasklist);
  2795. }
  2796. struct cgroup_subsys mem_cgroup_subsys = {
  2797. .name = "memory",
  2798. .subsys_id = mem_cgroup_subsys_id,
  2799. .create = mem_cgroup_create,
  2800. .pre_destroy = mem_cgroup_pre_destroy,
  2801. .destroy = mem_cgroup_destroy,
  2802. .populate = mem_cgroup_populate,
  2803. .attach = mem_cgroup_move_task,
  2804. .early_init = 0,
  2805. .use_id = 1,
  2806. };
  2807. #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
  2808. static int __init disable_swap_account(char *s)
  2809. {
  2810. really_do_swap_account = 0;
  2811. return 1;
  2812. }
  2813. __setup("noswapaccount", disable_swap_account);
  2814. #endif