memcontrol.c 82 KB

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