memcontrol.c 86 KB

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