memcontrol.c 79 KB

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