memcontrol.c 88 KB

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