memcontrol.c 96 KB

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