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. struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *mem)
  248. {
  249. return &mem->css;
  250. }
  251. static struct mem_cgroup_per_zone *
  252. page_cgroup_zoneinfo(struct page_cgroup *pc)
  253. {
  254. struct mem_cgroup *mem = pc->mem_cgroup;
  255. int nid = page_cgroup_nid(pc);
  256. int zid = page_cgroup_zid(pc);
  257. if (!mem)
  258. return NULL;
  259. return mem_cgroup_zoneinfo(mem, nid, zid);
  260. }
  261. static struct mem_cgroup_tree_per_zone *
  262. soft_limit_tree_node_zone(int nid, int zid)
  263. {
  264. return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
  265. }
  266. static struct mem_cgroup_tree_per_zone *
  267. soft_limit_tree_from_page(struct page *page)
  268. {
  269. int nid = page_to_nid(page);
  270. int zid = page_zonenum(page);
  271. return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
  272. }
  273. static void
  274. __mem_cgroup_insert_exceeded(struct mem_cgroup *mem,
  275. struct mem_cgroup_per_zone *mz,
  276. struct mem_cgroup_tree_per_zone *mctz,
  277. unsigned long long new_usage_in_excess)
  278. {
  279. struct rb_node **p = &mctz->rb_root.rb_node;
  280. struct rb_node *parent = NULL;
  281. struct mem_cgroup_per_zone *mz_node;
  282. if (mz->on_tree)
  283. return;
  284. mz->usage_in_excess = new_usage_in_excess;
  285. if (!mz->usage_in_excess)
  286. return;
  287. while (*p) {
  288. parent = *p;
  289. mz_node = rb_entry(parent, struct mem_cgroup_per_zone,
  290. tree_node);
  291. if (mz->usage_in_excess < mz_node->usage_in_excess)
  292. p = &(*p)->rb_left;
  293. /*
  294. * We can't avoid mem cgroups that are over their soft
  295. * limit by the same amount
  296. */
  297. else if (mz->usage_in_excess >= mz_node->usage_in_excess)
  298. p = &(*p)->rb_right;
  299. }
  300. rb_link_node(&mz->tree_node, parent, p);
  301. rb_insert_color(&mz->tree_node, &mctz->rb_root);
  302. mz->on_tree = true;
  303. }
  304. static void
  305. __mem_cgroup_remove_exceeded(struct mem_cgroup *mem,
  306. struct mem_cgroup_per_zone *mz,
  307. struct mem_cgroup_tree_per_zone *mctz)
  308. {
  309. if (!mz->on_tree)
  310. return;
  311. rb_erase(&mz->tree_node, &mctz->rb_root);
  312. mz->on_tree = false;
  313. }
  314. static void
  315. mem_cgroup_remove_exceeded(struct mem_cgroup *mem,
  316. struct mem_cgroup_per_zone *mz,
  317. struct mem_cgroup_tree_per_zone *mctz)
  318. {
  319. spin_lock(&mctz->lock);
  320. __mem_cgroup_remove_exceeded(mem, mz, mctz);
  321. spin_unlock(&mctz->lock);
  322. }
  323. static bool mem_cgroup_soft_limit_check(struct mem_cgroup *mem)
  324. {
  325. bool ret = false;
  326. int cpu;
  327. s64 val;
  328. struct mem_cgroup_stat_cpu *cpustat;
  329. cpu = get_cpu();
  330. cpustat = &mem->stat.cpustat[cpu];
  331. val = __mem_cgroup_stat_read_local(cpustat, MEM_CGROUP_STAT_EVENTS);
  332. if (unlikely(val > SOFTLIMIT_EVENTS_THRESH)) {
  333. __mem_cgroup_stat_reset_safe(cpustat, MEM_CGROUP_STAT_EVENTS);
  334. ret = true;
  335. }
  336. put_cpu();
  337. return ret;
  338. }
  339. static void mem_cgroup_update_tree(struct mem_cgroup *mem, struct page *page)
  340. {
  341. unsigned long long excess;
  342. struct mem_cgroup_per_zone *mz;
  343. struct mem_cgroup_tree_per_zone *mctz;
  344. int nid = page_to_nid(page);
  345. int zid = page_zonenum(page);
  346. mctz = soft_limit_tree_from_page(page);
  347. /*
  348. * Necessary to update all ancestors when hierarchy is used.
  349. * because their event counter is not touched.
  350. */
  351. for (; mem; mem = parent_mem_cgroup(mem)) {
  352. mz = mem_cgroup_zoneinfo(mem, nid, zid);
  353. excess = res_counter_soft_limit_excess(&mem->res);
  354. /*
  355. * We have to update the tree if mz is on RB-tree or
  356. * mem is over its softlimit.
  357. */
  358. if (excess || mz->on_tree) {
  359. spin_lock(&mctz->lock);
  360. /* if on-tree, remove it */
  361. if (mz->on_tree)
  362. __mem_cgroup_remove_exceeded(mem, mz, mctz);
  363. /*
  364. * Insert again. mz->usage_in_excess will be updated.
  365. * If excess is 0, no tree ops.
  366. */
  367. __mem_cgroup_insert_exceeded(mem, mz, mctz, excess);
  368. spin_unlock(&mctz->lock);
  369. }
  370. }
  371. }
  372. static void mem_cgroup_remove_from_trees(struct mem_cgroup *mem)
  373. {
  374. int node, zone;
  375. struct mem_cgroup_per_zone *mz;
  376. struct mem_cgroup_tree_per_zone *mctz;
  377. for_each_node_state(node, N_POSSIBLE) {
  378. for (zone = 0; zone < MAX_NR_ZONES; zone++) {
  379. mz = mem_cgroup_zoneinfo(mem, node, zone);
  380. mctz = soft_limit_tree_node_zone(node, zone);
  381. mem_cgroup_remove_exceeded(mem, mz, mctz);
  382. }
  383. }
  384. }
  385. static inline unsigned long mem_cgroup_get_excess(struct mem_cgroup *mem)
  386. {
  387. return res_counter_soft_limit_excess(&mem->res) >> PAGE_SHIFT;
  388. }
  389. static struct mem_cgroup_per_zone *
  390. __mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
  391. {
  392. struct rb_node *rightmost = NULL;
  393. struct mem_cgroup_per_zone *mz;
  394. retry:
  395. mz = NULL;
  396. rightmost = rb_last(&mctz->rb_root);
  397. if (!rightmost)
  398. goto done; /* Nothing to reclaim from */
  399. mz = rb_entry(rightmost, struct mem_cgroup_per_zone, tree_node);
  400. /*
  401. * Remove the node now but someone else can add it back,
  402. * we will to add it back at the end of reclaim to its correct
  403. * position in the tree.
  404. */
  405. __mem_cgroup_remove_exceeded(mz->mem, mz, mctz);
  406. if (!res_counter_soft_limit_excess(&mz->mem->res) ||
  407. !css_tryget(&mz->mem->css))
  408. goto retry;
  409. done:
  410. return mz;
  411. }
  412. static struct mem_cgroup_per_zone *
  413. mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
  414. {
  415. struct mem_cgroup_per_zone *mz;
  416. spin_lock(&mctz->lock);
  417. mz = __mem_cgroup_largest_soft_limit_node(mctz);
  418. spin_unlock(&mctz->lock);
  419. return mz;
  420. }
  421. static void mem_cgroup_swap_statistics(struct mem_cgroup *mem,
  422. bool charge)
  423. {
  424. int val = (charge) ? 1 : -1;
  425. struct mem_cgroup_stat *stat = &mem->stat;
  426. struct mem_cgroup_stat_cpu *cpustat;
  427. int cpu = get_cpu();
  428. cpustat = &stat->cpustat[cpu];
  429. __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_SWAPOUT, val);
  430. put_cpu();
  431. }
  432. static void mem_cgroup_charge_statistics(struct mem_cgroup *mem,
  433. struct page_cgroup *pc,
  434. bool charge)
  435. {
  436. int val = (charge) ? 1 : -1;
  437. struct mem_cgroup_stat *stat = &mem->stat;
  438. struct mem_cgroup_stat_cpu *cpustat;
  439. int cpu = get_cpu();
  440. cpustat = &stat->cpustat[cpu];
  441. if (PageCgroupCache(pc))
  442. __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_CACHE, val);
  443. else
  444. __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_RSS, val);
  445. if (charge)
  446. __mem_cgroup_stat_add_safe(cpustat,
  447. MEM_CGROUP_STAT_PGPGIN_COUNT, 1);
  448. else
  449. __mem_cgroup_stat_add_safe(cpustat,
  450. MEM_CGROUP_STAT_PGPGOUT_COUNT, 1);
  451. __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_EVENTS, 1);
  452. put_cpu();
  453. }
  454. static unsigned long mem_cgroup_get_local_zonestat(struct mem_cgroup *mem,
  455. enum lru_list idx)
  456. {
  457. int nid, zid;
  458. struct mem_cgroup_per_zone *mz;
  459. u64 total = 0;
  460. for_each_online_node(nid)
  461. for (zid = 0; zid < MAX_NR_ZONES; zid++) {
  462. mz = mem_cgroup_zoneinfo(mem, nid, zid);
  463. total += MEM_CGROUP_ZSTAT(mz, idx);
  464. }
  465. return total;
  466. }
  467. static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
  468. {
  469. return container_of(cgroup_subsys_state(cont,
  470. mem_cgroup_subsys_id), struct mem_cgroup,
  471. css);
  472. }
  473. struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
  474. {
  475. /*
  476. * mm_update_next_owner() may clear mm->owner to NULL
  477. * if it races with swapoff, page migration, etc.
  478. * So this can be called with p == NULL.
  479. */
  480. if (unlikely(!p))
  481. return NULL;
  482. return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
  483. struct mem_cgroup, css);
  484. }
  485. static struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
  486. {
  487. struct mem_cgroup *mem = NULL;
  488. if (!mm)
  489. return NULL;
  490. /*
  491. * Because we have no locks, mm->owner's may be being moved to other
  492. * cgroup. We use css_tryget() here even if this looks
  493. * pessimistic (rather than adding locks here).
  494. */
  495. rcu_read_lock();
  496. do {
  497. mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
  498. if (unlikely(!mem))
  499. break;
  500. } while (!css_tryget(&mem->css));
  501. rcu_read_unlock();
  502. return mem;
  503. }
  504. /*
  505. * Call callback function against all cgroup under hierarchy tree.
  506. */
  507. static int mem_cgroup_walk_tree(struct mem_cgroup *root, void *data,
  508. int (*func)(struct mem_cgroup *, void *))
  509. {
  510. int found, ret, nextid;
  511. struct cgroup_subsys_state *css;
  512. struct mem_cgroup *mem;
  513. if (!root->use_hierarchy)
  514. return (*func)(root, data);
  515. nextid = 1;
  516. do {
  517. ret = 0;
  518. mem = NULL;
  519. rcu_read_lock();
  520. css = css_get_next(&mem_cgroup_subsys, nextid, &root->css,
  521. &found);
  522. if (css && css_tryget(css))
  523. mem = container_of(css, struct mem_cgroup, css);
  524. rcu_read_unlock();
  525. if (mem) {
  526. ret = (*func)(mem, data);
  527. css_put(&mem->css);
  528. }
  529. nextid = found + 1;
  530. } while (!ret && css);
  531. return ret;
  532. }
  533. static inline bool mem_cgroup_is_root(struct mem_cgroup *mem)
  534. {
  535. return (mem == root_mem_cgroup);
  536. }
  537. /*
  538. * Following LRU functions are allowed to be used without PCG_LOCK.
  539. * Operations are called by routine of global LRU independently from memcg.
  540. * What we have to take care of here is validness of pc->mem_cgroup.
  541. *
  542. * Changes to pc->mem_cgroup happens when
  543. * 1. charge
  544. * 2. moving account
  545. * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
  546. * It is added to LRU before charge.
  547. * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
  548. * When moving account, the page is not on LRU. It's isolated.
  549. */
  550. void mem_cgroup_del_lru_list(struct page *page, enum lru_list lru)
  551. {
  552. struct page_cgroup *pc;
  553. struct mem_cgroup_per_zone *mz;
  554. if (mem_cgroup_disabled())
  555. return;
  556. pc = lookup_page_cgroup(page);
  557. /* can happen while we handle swapcache. */
  558. if (!TestClearPageCgroupAcctLRU(pc))
  559. return;
  560. VM_BUG_ON(!pc->mem_cgroup);
  561. /*
  562. * We don't check PCG_USED bit. It's cleared when the "page" is finally
  563. * removed from global LRU.
  564. */
  565. mz = page_cgroup_zoneinfo(pc);
  566. MEM_CGROUP_ZSTAT(mz, lru) -= 1;
  567. if (mem_cgroup_is_root(pc->mem_cgroup))
  568. return;
  569. VM_BUG_ON(list_empty(&pc->lru));
  570. list_del_init(&pc->lru);
  571. return;
  572. }
  573. void mem_cgroup_del_lru(struct page *page)
  574. {
  575. mem_cgroup_del_lru_list(page, page_lru(page));
  576. }
  577. void mem_cgroup_rotate_lru_list(struct page *page, enum lru_list lru)
  578. {
  579. struct mem_cgroup_per_zone *mz;
  580. struct page_cgroup *pc;
  581. if (mem_cgroup_disabled())
  582. return;
  583. pc = lookup_page_cgroup(page);
  584. /*
  585. * Used bit is set without atomic ops but after smp_wmb().
  586. * For making pc->mem_cgroup visible, insert smp_rmb() here.
  587. */
  588. smp_rmb();
  589. /* unused or root page is not rotated. */
  590. if (!PageCgroupUsed(pc) || mem_cgroup_is_root(pc->mem_cgroup))
  591. return;
  592. mz = page_cgroup_zoneinfo(pc);
  593. list_move(&pc->lru, &mz->lists[lru]);
  594. }
  595. void mem_cgroup_add_lru_list(struct page *page, enum lru_list lru)
  596. {
  597. struct page_cgroup *pc;
  598. struct mem_cgroup_per_zone *mz;
  599. if (mem_cgroup_disabled())
  600. return;
  601. pc = lookup_page_cgroup(page);
  602. VM_BUG_ON(PageCgroupAcctLRU(pc));
  603. /*
  604. * Used bit is set without atomic ops but after smp_wmb().
  605. * For making pc->mem_cgroup visible, insert smp_rmb() here.
  606. */
  607. smp_rmb();
  608. if (!PageCgroupUsed(pc))
  609. return;
  610. mz = page_cgroup_zoneinfo(pc);
  611. MEM_CGROUP_ZSTAT(mz, lru) += 1;
  612. SetPageCgroupAcctLRU(pc);
  613. if (mem_cgroup_is_root(pc->mem_cgroup))
  614. return;
  615. list_add(&pc->lru, &mz->lists[lru]);
  616. }
  617. /*
  618. * At handling SwapCache, pc->mem_cgroup may be changed while it's linked to
  619. * lru because the page may.be reused after it's fully uncharged (because of
  620. * SwapCache behavior).To handle that, unlink page_cgroup from LRU when charge
  621. * it again. This function is only used to charge SwapCache. It's done under
  622. * lock_page and expected that zone->lru_lock is never held.
  623. */
  624. static void mem_cgroup_lru_del_before_commit_swapcache(struct page *page)
  625. {
  626. unsigned long flags;
  627. struct zone *zone = page_zone(page);
  628. struct page_cgroup *pc = lookup_page_cgroup(page);
  629. spin_lock_irqsave(&zone->lru_lock, flags);
  630. /*
  631. * Forget old LRU when this page_cgroup is *not* used. This Used bit
  632. * is guarded by lock_page() because the page is SwapCache.
  633. */
  634. if (!PageCgroupUsed(pc))
  635. mem_cgroup_del_lru_list(page, page_lru(page));
  636. spin_unlock_irqrestore(&zone->lru_lock, flags);
  637. }
  638. static void mem_cgroup_lru_add_after_commit_swapcache(struct page *page)
  639. {
  640. unsigned long flags;
  641. struct zone *zone = page_zone(page);
  642. struct page_cgroup *pc = lookup_page_cgroup(page);
  643. spin_lock_irqsave(&zone->lru_lock, flags);
  644. /* link when the page is linked to LRU but page_cgroup isn't */
  645. if (PageLRU(page) && !PageCgroupAcctLRU(pc))
  646. mem_cgroup_add_lru_list(page, page_lru(page));
  647. spin_unlock_irqrestore(&zone->lru_lock, flags);
  648. }
  649. void mem_cgroup_move_lists(struct page *page,
  650. enum lru_list from, enum lru_list to)
  651. {
  652. if (mem_cgroup_disabled())
  653. return;
  654. mem_cgroup_del_lru_list(page, from);
  655. mem_cgroup_add_lru_list(page, to);
  656. }
  657. int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
  658. {
  659. int ret;
  660. struct mem_cgroup *curr = NULL;
  661. task_lock(task);
  662. rcu_read_lock();
  663. curr = try_get_mem_cgroup_from_mm(task->mm);
  664. rcu_read_unlock();
  665. task_unlock(task);
  666. if (!curr)
  667. return 0;
  668. /*
  669. * We should check use_hierarchy of "mem" not "curr". Because checking
  670. * use_hierarchy of "curr" here make this function true if hierarchy is
  671. * enabled in "curr" and "curr" is a child of "mem" in *cgroup*
  672. * hierarchy(even if use_hierarchy is disabled in "mem").
  673. */
  674. if (mem->use_hierarchy)
  675. ret = css_is_ancestor(&curr->css, &mem->css);
  676. else
  677. ret = (curr == mem);
  678. css_put(&curr->css);
  679. return ret;
  680. }
  681. /*
  682. * prev_priority control...this will be used in memory reclaim path.
  683. */
  684. int mem_cgroup_get_reclaim_priority(struct mem_cgroup *mem)
  685. {
  686. int prev_priority;
  687. spin_lock(&mem->reclaim_param_lock);
  688. prev_priority = mem->prev_priority;
  689. spin_unlock(&mem->reclaim_param_lock);
  690. return prev_priority;
  691. }
  692. void mem_cgroup_note_reclaim_priority(struct mem_cgroup *mem, int priority)
  693. {
  694. spin_lock(&mem->reclaim_param_lock);
  695. if (priority < mem->prev_priority)
  696. mem->prev_priority = priority;
  697. spin_unlock(&mem->reclaim_param_lock);
  698. }
  699. void mem_cgroup_record_reclaim_priority(struct mem_cgroup *mem, int priority)
  700. {
  701. spin_lock(&mem->reclaim_param_lock);
  702. mem->prev_priority = priority;
  703. spin_unlock(&mem->reclaim_param_lock);
  704. }
  705. static int calc_inactive_ratio(struct mem_cgroup *memcg, unsigned long *present_pages)
  706. {
  707. unsigned long active;
  708. unsigned long inactive;
  709. unsigned long gb;
  710. unsigned long inactive_ratio;
  711. inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_ANON);
  712. active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_ANON);
  713. gb = (inactive + active) >> (30 - PAGE_SHIFT);
  714. if (gb)
  715. inactive_ratio = int_sqrt(10 * gb);
  716. else
  717. inactive_ratio = 1;
  718. if (present_pages) {
  719. present_pages[0] = inactive;
  720. present_pages[1] = active;
  721. }
  722. return inactive_ratio;
  723. }
  724. int mem_cgroup_inactive_anon_is_low(struct mem_cgroup *memcg)
  725. {
  726. unsigned long active;
  727. unsigned long inactive;
  728. unsigned long present_pages[2];
  729. unsigned long inactive_ratio;
  730. inactive_ratio = calc_inactive_ratio(memcg, present_pages);
  731. inactive = present_pages[0];
  732. active = present_pages[1];
  733. if (inactive * inactive_ratio < active)
  734. return 1;
  735. return 0;
  736. }
  737. int mem_cgroup_inactive_file_is_low(struct mem_cgroup *memcg)
  738. {
  739. unsigned long active;
  740. unsigned long inactive;
  741. inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_FILE);
  742. active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_FILE);
  743. return (active > inactive);
  744. }
  745. unsigned long mem_cgroup_zone_nr_pages(struct mem_cgroup *memcg,
  746. struct zone *zone,
  747. enum lru_list lru)
  748. {
  749. int nid = zone->zone_pgdat->node_id;
  750. int zid = zone_idx(zone);
  751. struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
  752. return MEM_CGROUP_ZSTAT(mz, lru);
  753. }
  754. struct zone_reclaim_stat *mem_cgroup_get_reclaim_stat(struct mem_cgroup *memcg,
  755. struct zone *zone)
  756. {
  757. int nid = zone->zone_pgdat->node_id;
  758. int zid = zone_idx(zone);
  759. struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
  760. return &mz->reclaim_stat;
  761. }
  762. struct zone_reclaim_stat *
  763. mem_cgroup_get_reclaim_stat_from_page(struct page *page)
  764. {
  765. struct page_cgroup *pc;
  766. struct mem_cgroup_per_zone *mz;
  767. if (mem_cgroup_disabled())
  768. return NULL;
  769. pc = lookup_page_cgroup(page);
  770. /*
  771. * Used bit is set without atomic ops but after smp_wmb().
  772. * For making pc->mem_cgroup visible, insert smp_rmb() here.
  773. */
  774. smp_rmb();
  775. if (!PageCgroupUsed(pc))
  776. return NULL;
  777. mz = page_cgroup_zoneinfo(pc);
  778. if (!mz)
  779. return NULL;
  780. return &mz->reclaim_stat;
  781. }
  782. unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
  783. struct list_head *dst,
  784. unsigned long *scanned, int order,
  785. int mode, struct zone *z,
  786. struct mem_cgroup *mem_cont,
  787. int active, int file)
  788. {
  789. unsigned long nr_taken = 0;
  790. struct page *page;
  791. unsigned long scan;
  792. LIST_HEAD(pc_list);
  793. struct list_head *src;
  794. struct page_cgroup *pc, *tmp;
  795. int nid = z->zone_pgdat->node_id;
  796. int zid = zone_idx(z);
  797. struct mem_cgroup_per_zone *mz;
  798. int lru = LRU_FILE * file + active;
  799. int ret;
  800. BUG_ON(!mem_cont);
  801. mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
  802. src = &mz->lists[lru];
  803. scan = 0;
  804. list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
  805. if (scan >= nr_to_scan)
  806. break;
  807. page = pc->page;
  808. if (unlikely(!PageCgroupUsed(pc)))
  809. continue;
  810. if (unlikely(!PageLRU(page)))
  811. continue;
  812. scan++;
  813. ret = __isolate_lru_page(page, mode, file);
  814. switch (ret) {
  815. case 0:
  816. list_move(&page->lru, dst);
  817. mem_cgroup_del_lru(page);
  818. nr_taken++;
  819. break;
  820. case -EBUSY:
  821. /* we don't affect global LRU but rotate in our LRU */
  822. mem_cgroup_rotate_lru_list(page, page_lru(page));
  823. break;
  824. default:
  825. break;
  826. }
  827. }
  828. *scanned = scan;
  829. return nr_taken;
  830. }
  831. #define mem_cgroup_from_res_counter(counter, member) \
  832. container_of(counter, struct mem_cgroup, member)
  833. static bool mem_cgroup_check_under_limit(struct mem_cgroup *mem)
  834. {
  835. if (do_swap_account) {
  836. if (res_counter_check_under_limit(&mem->res) &&
  837. res_counter_check_under_limit(&mem->memsw))
  838. return true;
  839. } else
  840. if (res_counter_check_under_limit(&mem->res))
  841. return true;
  842. return false;
  843. }
  844. static unsigned int get_swappiness(struct mem_cgroup *memcg)
  845. {
  846. struct cgroup *cgrp = memcg->css.cgroup;
  847. unsigned int swappiness;
  848. /* root ? */
  849. if (cgrp->parent == NULL)
  850. return vm_swappiness;
  851. spin_lock(&memcg->reclaim_param_lock);
  852. swappiness = memcg->swappiness;
  853. spin_unlock(&memcg->reclaim_param_lock);
  854. return swappiness;
  855. }
  856. static int mem_cgroup_count_children_cb(struct mem_cgroup *mem, void *data)
  857. {
  858. int *val = data;
  859. (*val)++;
  860. return 0;
  861. }
  862. /**
  863. * mem_cgroup_print_mem_info: Called from OOM with tasklist_lock held in read mode.
  864. * @memcg: The memory cgroup that went over limit
  865. * @p: Task that is going to be killed
  866. *
  867. * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
  868. * enabled
  869. */
  870. void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
  871. {
  872. struct cgroup *task_cgrp;
  873. struct cgroup *mem_cgrp;
  874. /*
  875. * Need a buffer in BSS, can't rely on allocations. The code relies
  876. * on the assumption that OOM is serialized for memory controller.
  877. * If this assumption is broken, revisit this code.
  878. */
  879. static char memcg_name[PATH_MAX];
  880. int ret;
  881. if (!memcg || !p)
  882. return;
  883. rcu_read_lock();
  884. mem_cgrp = memcg->css.cgroup;
  885. task_cgrp = task_cgroup(p, mem_cgroup_subsys_id);
  886. ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX);
  887. if (ret < 0) {
  888. /*
  889. * Unfortunately, we are unable to convert to a useful name
  890. * But we'll still print out the usage information
  891. */
  892. rcu_read_unlock();
  893. goto done;
  894. }
  895. rcu_read_unlock();
  896. printk(KERN_INFO "Task in %s killed", memcg_name);
  897. rcu_read_lock();
  898. ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX);
  899. if (ret < 0) {
  900. rcu_read_unlock();
  901. goto done;
  902. }
  903. rcu_read_unlock();
  904. /*
  905. * Continues from above, so we don't need an KERN_ level
  906. */
  907. printk(KERN_CONT " as a result of limit of %s\n", memcg_name);
  908. done:
  909. printk(KERN_INFO "memory: usage %llukB, limit %llukB, failcnt %llu\n",
  910. res_counter_read_u64(&memcg->res, RES_USAGE) >> 10,
  911. res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10,
  912. res_counter_read_u64(&memcg->res, RES_FAILCNT));
  913. printk(KERN_INFO "memory+swap: usage %llukB, limit %llukB, "
  914. "failcnt %llu\n",
  915. res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10,
  916. res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10,
  917. res_counter_read_u64(&memcg->memsw, RES_FAILCNT));
  918. }
  919. /*
  920. * This function returns the number of memcg under hierarchy tree. Returns
  921. * 1(self count) if no children.
  922. */
  923. static int mem_cgroup_count_children(struct mem_cgroup *mem)
  924. {
  925. int num = 0;
  926. mem_cgroup_walk_tree(mem, &num, mem_cgroup_count_children_cb);
  927. return num;
  928. }
  929. /*
  930. * Visit the first child (need not be the first child as per the ordering
  931. * of the cgroup list, since we track last_scanned_child) of @mem and use
  932. * that to reclaim free pages from.
  933. */
  934. static struct mem_cgroup *
  935. mem_cgroup_select_victim(struct mem_cgroup *root_mem)
  936. {
  937. struct mem_cgroup *ret = NULL;
  938. struct cgroup_subsys_state *css;
  939. int nextid, found;
  940. if (!root_mem->use_hierarchy) {
  941. css_get(&root_mem->css);
  942. ret = root_mem;
  943. }
  944. while (!ret) {
  945. rcu_read_lock();
  946. nextid = root_mem->last_scanned_child + 1;
  947. css = css_get_next(&mem_cgroup_subsys, nextid, &root_mem->css,
  948. &found);
  949. if (css && css_tryget(css))
  950. ret = container_of(css, struct mem_cgroup, css);
  951. rcu_read_unlock();
  952. /* Updates scanning parameter */
  953. spin_lock(&root_mem->reclaim_param_lock);
  954. if (!css) {
  955. /* this means start scan from ID:1 */
  956. root_mem->last_scanned_child = 0;
  957. } else
  958. root_mem->last_scanned_child = found;
  959. spin_unlock(&root_mem->reclaim_param_lock);
  960. }
  961. return ret;
  962. }
  963. /*
  964. * Scan the hierarchy if needed to reclaim memory. We remember the last child
  965. * we reclaimed from, so that we don't end up penalizing one child extensively
  966. * based on its position in the children list.
  967. *
  968. * root_mem is the original ancestor that we've been reclaim from.
  969. *
  970. * We give up and return to the caller when we visit root_mem twice.
  971. * (other groups can be removed while we're walking....)
  972. *
  973. * If shrink==true, for avoiding to free too much, this returns immedieately.
  974. */
  975. static int mem_cgroup_hierarchical_reclaim(struct mem_cgroup *root_mem,
  976. struct zone *zone,
  977. gfp_t gfp_mask,
  978. unsigned long reclaim_options)
  979. {
  980. struct mem_cgroup *victim;
  981. int ret, total = 0;
  982. int loop = 0;
  983. bool noswap = reclaim_options & MEM_CGROUP_RECLAIM_NOSWAP;
  984. bool shrink = reclaim_options & MEM_CGROUP_RECLAIM_SHRINK;
  985. bool check_soft = reclaim_options & MEM_CGROUP_RECLAIM_SOFT;
  986. unsigned long excess = mem_cgroup_get_excess(root_mem);
  987. /* If memsw_is_minimum==1, swap-out is of-no-use. */
  988. if (root_mem->memsw_is_minimum)
  989. noswap = true;
  990. while (1) {
  991. victim = mem_cgroup_select_victim(root_mem);
  992. if (victim == root_mem) {
  993. loop++;
  994. if (loop >= 1)
  995. drain_all_stock_async();
  996. if (loop >= 2) {
  997. /*
  998. * If we have not been able to reclaim
  999. * anything, it might because there are
  1000. * no reclaimable pages under this hierarchy
  1001. */
  1002. if (!check_soft || !total) {
  1003. css_put(&victim->css);
  1004. break;
  1005. }
  1006. /*
  1007. * We want to do more targetted reclaim.
  1008. * excess >> 2 is not to excessive so as to
  1009. * reclaim too much, nor too less that we keep
  1010. * coming back to reclaim from this cgroup
  1011. */
  1012. if (total >= (excess >> 2) ||
  1013. (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS)) {
  1014. css_put(&victim->css);
  1015. break;
  1016. }
  1017. }
  1018. }
  1019. if (!mem_cgroup_local_usage(&victim->stat)) {
  1020. /* this cgroup's local usage == 0 */
  1021. css_put(&victim->css);
  1022. continue;
  1023. }
  1024. /* we use swappiness of local cgroup */
  1025. if (check_soft)
  1026. ret = mem_cgroup_shrink_node_zone(victim, gfp_mask,
  1027. noswap, get_swappiness(victim), zone,
  1028. zone->zone_pgdat->node_id);
  1029. else
  1030. ret = try_to_free_mem_cgroup_pages(victim, gfp_mask,
  1031. noswap, get_swappiness(victim));
  1032. css_put(&victim->css);
  1033. /*
  1034. * At shrinking usage, we can't check we should stop here or
  1035. * reclaim more. It's depends on callers. last_scanned_child
  1036. * will work enough for keeping fairness under tree.
  1037. */
  1038. if (shrink)
  1039. return ret;
  1040. total += ret;
  1041. if (check_soft) {
  1042. if (res_counter_check_under_soft_limit(&root_mem->res))
  1043. return total;
  1044. } else if (mem_cgroup_check_under_limit(root_mem))
  1045. return 1 + total;
  1046. }
  1047. return total;
  1048. }
  1049. bool mem_cgroup_oom_called(struct task_struct *task)
  1050. {
  1051. bool ret = false;
  1052. struct mem_cgroup *mem;
  1053. struct mm_struct *mm;
  1054. rcu_read_lock();
  1055. mm = task->mm;
  1056. if (!mm)
  1057. mm = &init_mm;
  1058. mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
  1059. if (mem && time_before(jiffies, mem->last_oom_jiffies + HZ/10))
  1060. ret = true;
  1061. rcu_read_unlock();
  1062. return ret;
  1063. }
  1064. static int record_last_oom_cb(struct mem_cgroup *mem, void *data)
  1065. {
  1066. mem->last_oom_jiffies = jiffies;
  1067. return 0;
  1068. }
  1069. static void record_last_oom(struct mem_cgroup *mem)
  1070. {
  1071. mem_cgroup_walk_tree(mem, NULL, record_last_oom_cb);
  1072. }
  1073. /*
  1074. * Currently used to update mapped file statistics, but the routine can be
  1075. * generalized to update other statistics as well.
  1076. */
  1077. void mem_cgroup_update_file_mapped(struct page *page, int val)
  1078. {
  1079. struct mem_cgroup *mem;
  1080. struct mem_cgroup_stat *stat;
  1081. struct mem_cgroup_stat_cpu *cpustat;
  1082. int cpu;
  1083. struct page_cgroup *pc;
  1084. pc = lookup_page_cgroup(page);
  1085. if (unlikely(!pc))
  1086. return;
  1087. lock_page_cgroup(pc);
  1088. mem = pc->mem_cgroup;
  1089. if (!mem)
  1090. goto done;
  1091. if (!PageCgroupUsed(pc))
  1092. goto done;
  1093. /*
  1094. * Preemption is already disabled, we don't need get_cpu()
  1095. */
  1096. cpu = smp_processor_id();
  1097. stat = &mem->stat;
  1098. cpustat = &stat->cpustat[cpu];
  1099. __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_FILE_MAPPED, val);
  1100. done:
  1101. unlock_page_cgroup(pc);
  1102. }
  1103. /*
  1104. * size of first charge trial. "32" comes from vmscan.c's magic value.
  1105. * TODO: maybe necessary to use big numbers in big irons.
  1106. */
  1107. #define CHARGE_SIZE (32 * PAGE_SIZE)
  1108. struct memcg_stock_pcp {
  1109. struct mem_cgroup *cached; /* this never be root cgroup */
  1110. int charge;
  1111. struct work_struct work;
  1112. };
  1113. static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock);
  1114. static atomic_t memcg_drain_count;
  1115. /*
  1116. * Try to consume stocked charge on this cpu. If success, PAGE_SIZE is consumed
  1117. * from local stock and true is returned. If the stock is 0 or charges from a
  1118. * cgroup which is not current target, returns false. This stock will be
  1119. * refilled.
  1120. */
  1121. static bool consume_stock(struct mem_cgroup *mem)
  1122. {
  1123. struct memcg_stock_pcp *stock;
  1124. bool ret = true;
  1125. stock = &get_cpu_var(memcg_stock);
  1126. if (mem == stock->cached && stock->charge)
  1127. stock->charge -= PAGE_SIZE;
  1128. else /* need to call res_counter_charge */
  1129. ret = false;
  1130. put_cpu_var(memcg_stock);
  1131. return ret;
  1132. }
  1133. /*
  1134. * Returns stocks cached in percpu to res_counter and reset cached information.
  1135. */
  1136. static void drain_stock(struct memcg_stock_pcp *stock)
  1137. {
  1138. struct mem_cgroup *old = stock->cached;
  1139. if (stock->charge) {
  1140. res_counter_uncharge(&old->res, stock->charge);
  1141. if (do_swap_account)
  1142. res_counter_uncharge(&old->memsw, stock->charge);
  1143. }
  1144. stock->cached = NULL;
  1145. stock->charge = 0;
  1146. }
  1147. /*
  1148. * This must be called under preempt disabled or must be called by
  1149. * a thread which is pinned to local cpu.
  1150. */
  1151. static void drain_local_stock(struct work_struct *dummy)
  1152. {
  1153. struct memcg_stock_pcp *stock = &__get_cpu_var(memcg_stock);
  1154. drain_stock(stock);
  1155. }
  1156. /*
  1157. * Cache charges(val) which is from res_counter, to local per_cpu area.
  1158. * This will be consumed by consumt_stock() function, later.
  1159. */
  1160. static void refill_stock(struct mem_cgroup *mem, int val)
  1161. {
  1162. struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
  1163. if (stock->cached != mem) { /* reset if necessary */
  1164. drain_stock(stock);
  1165. stock->cached = mem;
  1166. }
  1167. stock->charge += val;
  1168. put_cpu_var(memcg_stock);
  1169. }
  1170. /*
  1171. * Tries to drain stocked charges in other cpus. This function is asynchronous
  1172. * and just put a work per cpu for draining localy on each cpu. Caller can
  1173. * expects some charges will be back to res_counter later but cannot wait for
  1174. * it.
  1175. */
  1176. static void drain_all_stock_async(void)
  1177. {
  1178. int cpu;
  1179. /* This function is for scheduling "drain" in asynchronous way.
  1180. * The result of "drain" is not directly handled by callers. Then,
  1181. * if someone is calling drain, we don't have to call drain more.
  1182. * Anyway, WORK_STRUCT_PENDING check in queue_work_on() will catch if
  1183. * there is a race. We just do loose check here.
  1184. */
  1185. if (atomic_read(&memcg_drain_count))
  1186. return;
  1187. /* Notify other cpus that system-wide "drain" is running */
  1188. atomic_inc(&memcg_drain_count);
  1189. get_online_cpus();
  1190. for_each_online_cpu(cpu) {
  1191. struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
  1192. schedule_work_on(cpu, &stock->work);
  1193. }
  1194. put_online_cpus();
  1195. atomic_dec(&memcg_drain_count);
  1196. /* We don't wait for flush_work */
  1197. }
  1198. /* This is a synchronous drain interface. */
  1199. static void drain_all_stock_sync(void)
  1200. {
  1201. /* called when force_empty is called */
  1202. atomic_inc(&memcg_drain_count);
  1203. schedule_on_each_cpu(drain_local_stock);
  1204. atomic_dec(&memcg_drain_count);
  1205. }
  1206. static int __cpuinit memcg_stock_cpu_callback(struct notifier_block *nb,
  1207. unsigned long action,
  1208. void *hcpu)
  1209. {
  1210. int cpu = (unsigned long)hcpu;
  1211. struct memcg_stock_pcp *stock;
  1212. if (action != CPU_DEAD)
  1213. return NOTIFY_OK;
  1214. stock = &per_cpu(memcg_stock, cpu);
  1215. drain_stock(stock);
  1216. return NOTIFY_OK;
  1217. }
  1218. /*
  1219. * Unlike exported interface, "oom" parameter is added. if oom==true,
  1220. * oom-killer can be invoked.
  1221. */
  1222. static int __mem_cgroup_try_charge(struct mm_struct *mm,
  1223. gfp_t gfp_mask, struct mem_cgroup **memcg,
  1224. bool oom, struct page *page)
  1225. {
  1226. struct mem_cgroup *mem, *mem_over_limit;
  1227. int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
  1228. struct res_counter *fail_res;
  1229. int csize = CHARGE_SIZE;
  1230. if (unlikely(test_thread_flag(TIF_MEMDIE))) {
  1231. /* Don't account this! */
  1232. *memcg = NULL;
  1233. return 0;
  1234. }
  1235. /*
  1236. * We always charge the cgroup the mm_struct belongs to.
  1237. * The mm_struct's mem_cgroup changes on task migration if the
  1238. * thread group leader migrates. It's possible that mm is not
  1239. * set, if so charge the init_mm (happens for pagecache usage).
  1240. */
  1241. mem = *memcg;
  1242. if (likely(!mem)) {
  1243. mem = try_get_mem_cgroup_from_mm(mm);
  1244. *memcg = mem;
  1245. } else {
  1246. css_get(&mem->css);
  1247. }
  1248. if (unlikely(!mem))
  1249. return 0;
  1250. VM_BUG_ON(css_is_removed(&mem->css));
  1251. if (mem_cgroup_is_root(mem))
  1252. goto done;
  1253. while (1) {
  1254. int ret = 0;
  1255. unsigned long flags = 0;
  1256. if (consume_stock(mem))
  1257. goto charged;
  1258. ret = res_counter_charge(&mem->res, csize, &fail_res);
  1259. if (likely(!ret)) {
  1260. if (!do_swap_account)
  1261. break;
  1262. ret = res_counter_charge(&mem->memsw, csize, &fail_res);
  1263. if (likely(!ret))
  1264. break;
  1265. /* mem+swap counter fails */
  1266. res_counter_uncharge(&mem->res, csize);
  1267. flags |= MEM_CGROUP_RECLAIM_NOSWAP;
  1268. mem_over_limit = mem_cgroup_from_res_counter(fail_res,
  1269. memsw);
  1270. } else
  1271. /* mem counter fails */
  1272. mem_over_limit = mem_cgroup_from_res_counter(fail_res,
  1273. res);
  1274. /* reduce request size and retry */
  1275. if (csize > PAGE_SIZE) {
  1276. csize = PAGE_SIZE;
  1277. continue;
  1278. }
  1279. if (!(gfp_mask & __GFP_WAIT))
  1280. goto nomem;
  1281. ret = mem_cgroup_hierarchical_reclaim(mem_over_limit, NULL,
  1282. gfp_mask, flags);
  1283. if (ret)
  1284. continue;
  1285. /*
  1286. * try_to_free_mem_cgroup_pages() might not give us a full
  1287. * picture of reclaim. Some pages are reclaimed and might be
  1288. * moved to swap cache or just unmapped from the cgroup.
  1289. * Check the limit again to see if the reclaim reduced the
  1290. * current usage of the cgroup before giving up
  1291. *
  1292. */
  1293. if (mem_cgroup_check_under_limit(mem_over_limit))
  1294. continue;
  1295. if (!nr_retries--) {
  1296. if (oom) {
  1297. mem_cgroup_out_of_memory(mem_over_limit, gfp_mask);
  1298. record_last_oom(mem_over_limit);
  1299. }
  1300. goto nomem;
  1301. }
  1302. }
  1303. if (csize > PAGE_SIZE)
  1304. refill_stock(mem, csize - PAGE_SIZE);
  1305. charged:
  1306. /*
  1307. * Insert ancestor (and ancestor's ancestors), to softlimit RB-tree.
  1308. * if they exceeds softlimit.
  1309. */
  1310. if (mem_cgroup_soft_limit_check(mem))
  1311. mem_cgroup_update_tree(mem, page);
  1312. done:
  1313. return 0;
  1314. nomem:
  1315. css_put(&mem->css);
  1316. return -ENOMEM;
  1317. }
  1318. /*
  1319. * Somemtimes we have to undo a charge we got by try_charge().
  1320. * This function is for that and do uncharge, put css's refcnt.
  1321. * gotten by try_charge().
  1322. */
  1323. static void mem_cgroup_cancel_charge(struct mem_cgroup *mem)
  1324. {
  1325. if (!mem_cgroup_is_root(mem)) {
  1326. res_counter_uncharge(&mem->res, PAGE_SIZE);
  1327. if (do_swap_account)
  1328. res_counter_uncharge(&mem->memsw, PAGE_SIZE);
  1329. }
  1330. css_put(&mem->css);
  1331. }
  1332. /*
  1333. * A helper function to get mem_cgroup from ID. must be called under
  1334. * rcu_read_lock(). The caller must check css_is_removed() or some if
  1335. * it's concern. (dropping refcnt from swap can be called against removed
  1336. * memcg.)
  1337. */
  1338. static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
  1339. {
  1340. struct cgroup_subsys_state *css;
  1341. /* ID 0 is unused ID */
  1342. if (!id)
  1343. return NULL;
  1344. css = css_lookup(&mem_cgroup_subsys, id);
  1345. if (!css)
  1346. return NULL;
  1347. return container_of(css, struct mem_cgroup, css);
  1348. }
  1349. struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
  1350. {
  1351. struct mem_cgroup *mem = NULL;
  1352. struct page_cgroup *pc;
  1353. unsigned short id;
  1354. swp_entry_t ent;
  1355. VM_BUG_ON(!PageLocked(page));
  1356. pc = lookup_page_cgroup(page);
  1357. lock_page_cgroup(pc);
  1358. if (PageCgroupUsed(pc)) {
  1359. mem = pc->mem_cgroup;
  1360. if (mem && !css_tryget(&mem->css))
  1361. mem = NULL;
  1362. } else if (PageSwapCache(page)) {
  1363. ent.val = page_private(page);
  1364. id = lookup_swap_cgroup(ent);
  1365. rcu_read_lock();
  1366. mem = mem_cgroup_lookup(id);
  1367. if (mem && !css_tryget(&mem->css))
  1368. mem = NULL;
  1369. rcu_read_unlock();
  1370. }
  1371. unlock_page_cgroup(pc);
  1372. return mem;
  1373. }
  1374. /*
  1375. * commit a charge got by __mem_cgroup_try_charge() and makes page_cgroup to be
  1376. * USED state. If already USED, uncharge and return.
  1377. */
  1378. static void __mem_cgroup_commit_charge(struct mem_cgroup *mem,
  1379. struct page_cgroup *pc,
  1380. enum charge_type ctype)
  1381. {
  1382. /* try_charge() can return NULL to *memcg, taking care of it. */
  1383. if (!mem)
  1384. return;
  1385. lock_page_cgroup(pc);
  1386. if (unlikely(PageCgroupUsed(pc))) {
  1387. unlock_page_cgroup(pc);
  1388. mem_cgroup_cancel_charge(mem);
  1389. return;
  1390. }
  1391. pc->mem_cgroup = mem;
  1392. /*
  1393. * We access a page_cgroup asynchronously without lock_page_cgroup().
  1394. * Especially when a page_cgroup is taken from a page, pc->mem_cgroup
  1395. * is accessed after testing USED bit. To make pc->mem_cgroup visible
  1396. * before USED bit, we need memory barrier here.
  1397. * See mem_cgroup_add_lru_list(), etc.
  1398. */
  1399. smp_wmb();
  1400. switch (ctype) {
  1401. case MEM_CGROUP_CHARGE_TYPE_CACHE:
  1402. case MEM_CGROUP_CHARGE_TYPE_SHMEM:
  1403. SetPageCgroupCache(pc);
  1404. SetPageCgroupUsed(pc);
  1405. break;
  1406. case MEM_CGROUP_CHARGE_TYPE_MAPPED:
  1407. ClearPageCgroupCache(pc);
  1408. SetPageCgroupUsed(pc);
  1409. break;
  1410. default:
  1411. break;
  1412. }
  1413. mem_cgroup_charge_statistics(mem, pc, true);
  1414. unlock_page_cgroup(pc);
  1415. }
  1416. /**
  1417. * __mem_cgroup_move_account - move account of the page
  1418. * @pc: page_cgroup of the page.
  1419. * @from: mem_cgroup which the page is moved from.
  1420. * @to: mem_cgroup which the page is moved to. @from != @to.
  1421. *
  1422. * The caller must confirm following.
  1423. * - page is not on LRU (isolate_page() is useful.)
  1424. * - the pc is locked, used, and ->mem_cgroup points to @from.
  1425. *
  1426. * This function does "uncharge" from old cgroup but doesn't do "charge" to
  1427. * new cgroup. It should be done by a caller.
  1428. */
  1429. static void __mem_cgroup_move_account(struct page_cgroup *pc,
  1430. struct mem_cgroup *from, struct mem_cgroup *to)
  1431. {
  1432. struct page *page;
  1433. int cpu;
  1434. struct mem_cgroup_stat *stat;
  1435. struct mem_cgroup_stat_cpu *cpustat;
  1436. VM_BUG_ON(from == to);
  1437. VM_BUG_ON(PageLRU(pc->page));
  1438. VM_BUG_ON(!PageCgroupLocked(pc));
  1439. VM_BUG_ON(!PageCgroupUsed(pc));
  1440. VM_BUG_ON(pc->mem_cgroup != from);
  1441. if (!mem_cgroup_is_root(from))
  1442. res_counter_uncharge(&from->res, PAGE_SIZE);
  1443. mem_cgroup_charge_statistics(from, pc, false);
  1444. page = pc->page;
  1445. if (page_mapped(page) && !PageAnon(page)) {
  1446. cpu = smp_processor_id();
  1447. /* Update mapped_file data for mem_cgroup "from" */
  1448. stat = &from->stat;
  1449. cpustat = &stat->cpustat[cpu];
  1450. __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_FILE_MAPPED,
  1451. -1);
  1452. /* Update mapped_file data for mem_cgroup "to" */
  1453. stat = &to->stat;
  1454. cpustat = &stat->cpustat[cpu];
  1455. __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_FILE_MAPPED,
  1456. 1);
  1457. }
  1458. if (do_swap_account && !mem_cgroup_is_root(from))
  1459. res_counter_uncharge(&from->memsw, PAGE_SIZE);
  1460. css_put(&from->css);
  1461. css_get(&to->css);
  1462. pc->mem_cgroup = to;
  1463. mem_cgroup_charge_statistics(to, pc, true);
  1464. /*
  1465. * We charges against "to" which may not have any tasks. Then, "to"
  1466. * can be under rmdir(). But in current implementation, caller of
  1467. * this function is just force_empty() and it's garanteed that
  1468. * "to" is never removed. So, we don't check rmdir status here.
  1469. */
  1470. }
  1471. /*
  1472. * check whether the @pc is valid for moving account and call
  1473. * __mem_cgroup_move_account()
  1474. */
  1475. static int mem_cgroup_move_account(struct page_cgroup *pc,
  1476. struct mem_cgroup *from, struct mem_cgroup *to)
  1477. {
  1478. int ret = -EINVAL;
  1479. lock_page_cgroup(pc);
  1480. if (PageCgroupUsed(pc) && pc->mem_cgroup == from) {
  1481. __mem_cgroup_move_account(pc, from, to);
  1482. ret = 0;
  1483. }
  1484. unlock_page_cgroup(pc);
  1485. return ret;
  1486. }
  1487. /*
  1488. * move charges to its parent.
  1489. */
  1490. static int mem_cgroup_move_parent(struct page_cgroup *pc,
  1491. struct mem_cgroup *child,
  1492. gfp_t gfp_mask)
  1493. {
  1494. struct page *page = pc->page;
  1495. struct cgroup *cg = child->css.cgroup;
  1496. struct cgroup *pcg = cg->parent;
  1497. struct mem_cgroup *parent;
  1498. int ret;
  1499. /* Is ROOT ? */
  1500. if (!pcg)
  1501. return -EINVAL;
  1502. ret = -EBUSY;
  1503. if (!get_page_unless_zero(page))
  1504. goto out;
  1505. if (isolate_lru_page(page))
  1506. goto put;
  1507. parent = mem_cgroup_from_cont(pcg);
  1508. ret = __mem_cgroup_try_charge(NULL, gfp_mask, &parent, false, page);
  1509. if (ret || !parent)
  1510. goto put_back;
  1511. ret = mem_cgroup_move_account(pc, child, parent);
  1512. if (!ret)
  1513. css_put(&parent->css); /* drop extra refcnt by try_charge() */
  1514. else
  1515. mem_cgroup_cancel_charge(parent); /* does css_put */
  1516. put_back:
  1517. putback_lru_page(page);
  1518. put:
  1519. put_page(page);
  1520. out:
  1521. return ret;
  1522. }
  1523. /*
  1524. * Charge the memory controller for page usage.
  1525. * Return
  1526. * 0 if the charge was successful
  1527. * < 0 if the cgroup is over its limit
  1528. */
  1529. static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
  1530. gfp_t gfp_mask, enum charge_type ctype,
  1531. struct mem_cgroup *memcg)
  1532. {
  1533. struct mem_cgroup *mem;
  1534. struct page_cgroup *pc;
  1535. int ret;
  1536. pc = lookup_page_cgroup(page);
  1537. /* can happen at boot */
  1538. if (unlikely(!pc))
  1539. return 0;
  1540. prefetchw(pc);
  1541. mem = memcg;
  1542. ret = __mem_cgroup_try_charge(mm, gfp_mask, &mem, true, page);
  1543. if (ret || !mem)
  1544. return ret;
  1545. __mem_cgroup_commit_charge(mem, pc, ctype);
  1546. return 0;
  1547. }
  1548. int mem_cgroup_newpage_charge(struct page *page,
  1549. struct mm_struct *mm, gfp_t gfp_mask)
  1550. {
  1551. if (mem_cgroup_disabled())
  1552. return 0;
  1553. if (PageCompound(page))
  1554. return 0;
  1555. /*
  1556. * If already mapped, we don't have to account.
  1557. * If page cache, page->mapping has address_space.
  1558. * But page->mapping may have out-of-use anon_vma pointer,
  1559. * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
  1560. * is NULL.
  1561. */
  1562. if (page_mapped(page) || (page->mapping && !PageAnon(page)))
  1563. return 0;
  1564. if (unlikely(!mm))
  1565. mm = &init_mm;
  1566. return mem_cgroup_charge_common(page, mm, gfp_mask,
  1567. MEM_CGROUP_CHARGE_TYPE_MAPPED, NULL);
  1568. }
  1569. static void
  1570. __mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
  1571. enum charge_type ctype);
  1572. int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
  1573. gfp_t gfp_mask)
  1574. {
  1575. struct mem_cgroup *mem = NULL;
  1576. int ret;
  1577. if (mem_cgroup_disabled())
  1578. return 0;
  1579. if (PageCompound(page))
  1580. return 0;
  1581. /*
  1582. * Corner case handling. This is called from add_to_page_cache()
  1583. * in usual. But some FS (shmem) precharges this page before calling it
  1584. * and call add_to_page_cache() with GFP_NOWAIT.
  1585. *
  1586. * For GFP_NOWAIT case, the page may be pre-charged before calling
  1587. * add_to_page_cache(). (See shmem.c) check it here and avoid to call
  1588. * charge twice. (It works but has to pay a bit larger cost.)
  1589. * And when the page is SwapCache, it should take swap information
  1590. * into account. This is under lock_page() now.
  1591. */
  1592. if (!(gfp_mask & __GFP_WAIT)) {
  1593. struct page_cgroup *pc;
  1594. pc = lookup_page_cgroup(page);
  1595. if (!pc)
  1596. return 0;
  1597. lock_page_cgroup(pc);
  1598. if (PageCgroupUsed(pc)) {
  1599. unlock_page_cgroup(pc);
  1600. return 0;
  1601. }
  1602. unlock_page_cgroup(pc);
  1603. }
  1604. if (unlikely(!mm && !mem))
  1605. mm = &init_mm;
  1606. if (page_is_file_cache(page))
  1607. return mem_cgroup_charge_common(page, mm, gfp_mask,
  1608. MEM_CGROUP_CHARGE_TYPE_CACHE, NULL);
  1609. /* shmem */
  1610. if (PageSwapCache(page)) {
  1611. ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
  1612. if (!ret)
  1613. __mem_cgroup_commit_charge_swapin(page, mem,
  1614. MEM_CGROUP_CHARGE_TYPE_SHMEM);
  1615. } else
  1616. ret = mem_cgroup_charge_common(page, mm, gfp_mask,
  1617. MEM_CGROUP_CHARGE_TYPE_SHMEM, mem);
  1618. return ret;
  1619. }
  1620. /*
  1621. * While swap-in, try_charge -> commit or cancel, the page is locked.
  1622. * And when try_charge() successfully returns, one refcnt to memcg without
  1623. * struct page_cgroup is acquired. This refcnt will be consumed by
  1624. * "commit()" or removed by "cancel()"
  1625. */
  1626. int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
  1627. struct page *page,
  1628. gfp_t mask, struct mem_cgroup **ptr)
  1629. {
  1630. struct mem_cgroup *mem;
  1631. int ret;
  1632. if (mem_cgroup_disabled())
  1633. return 0;
  1634. if (!do_swap_account)
  1635. goto charge_cur_mm;
  1636. /*
  1637. * A racing thread's fault, or swapoff, may have already updated
  1638. * the pte, and even removed page from swap cache: in those cases
  1639. * do_swap_page()'s pte_same() test will fail; but there's also a
  1640. * KSM case which does need to charge the page.
  1641. */
  1642. if (!PageSwapCache(page))
  1643. goto charge_cur_mm;
  1644. mem = try_get_mem_cgroup_from_page(page);
  1645. if (!mem)
  1646. goto charge_cur_mm;
  1647. *ptr = mem;
  1648. ret = __mem_cgroup_try_charge(NULL, mask, ptr, true, page);
  1649. /* drop extra refcnt from tryget */
  1650. css_put(&mem->css);
  1651. return ret;
  1652. charge_cur_mm:
  1653. if (unlikely(!mm))
  1654. mm = &init_mm;
  1655. return __mem_cgroup_try_charge(mm, mask, ptr, true, page);
  1656. }
  1657. static void
  1658. __mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
  1659. enum charge_type ctype)
  1660. {
  1661. struct page_cgroup *pc;
  1662. if (mem_cgroup_disabled())
  1663. return;
  1664. if (!ptr)
  1665. return;
  1666. cgroup_exclude_rmdir(&ptr->css);
  1667. pc = lookup_page_cgroup(page);
  1668. mem_cgroup_lru_del_before_commit_swapcache(page);
  1669. __mem_cgroup_commit_charge(ptr, pc, ctype);
  1670. mem_cgroup_lru_add_after_commit_swapcache(page);
  1671. /*
  1672. * Now swap is on-memory. This means this page may be
  1673. * counted both as mem and swap....double count.
  1674. * Fix it by uncharging from memsw. Basically, this SwapCache is stable
  1675. * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
  1676. * may call delete_from_swap_cache() before reach here.
  1677. */
  1678. if (do_swap_account && PageSwapCache(page)) {
  1679. swp_entry_t ent = {.val = page_private(page)};
  1680. unsigned short id;
  1681. struct mem_cgroup *memcg;
  1682. id = swap_cgroup_record(ent, 0);
  1683. rcu_read_lock();
  1684. memcg = mem_cgroup_lookup(id);
  1685. if (memcg) {
  1686. /*
  1687. * This recorded memcg can be obsolete one. So, avoid
  1688. * calling css_tryget
  1689. */
  1690. if (!mem_cgroup_is_root(memcg))
  1691. res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
  1692. mem_cgroup_swap_statistics(memcg, false);
  1693. mem_cgroup_put(memcg);
  1694. }
  1695. rcu_read_unlock();
  1696. }
  1697. /*
  1698. * At swapin, we may charge account against cgroup which has no tasks.
  1699. * So, rmdir()->pre_destroy() can be called while we do this charge.
  1700. * In that case, we need to call pre_destroy() again. check it here.
  1701. */
  1702. cgroup_release_and_wakeup_rmdir(&ptr->css);
  1703. }
  1704. void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr)
  1705. {
  1706. __mem_cgroup_commit_charge_swapin(page, ptr,
  1707. MEM_CGROUP_CHARGE_TYPE_MAPPED);
  1708. }
  1709. void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem)
  1710. {
  1711. if (mem_cgroup_disabled())
  1712. return;
  1713. if (!mem)
  1714. return;
  1715. mem_cgroup_cancel_charge(mem);
  1716. }
  1717. static void
  1718. __do_uncharge(struct mem_cgroup *mem, const enum charge_type ctype)
  1719. {
  1720. struct memcg_batch_info *batch = NULL;
  1721. bool uncharge_memsw = true;
  1722. /* If swapout, usage of swap doesn't decrease */
  1723. if (!do_swap_account || ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
  1724. uncharge_memsw = false;
  1725. /*
  1726. * do_batch > 0 when unmapping pages or inode invalidate/truncate.
  1727. * In those cases, all pages freed continously can be expected to be in
  1728. * the same cgroup and we have chance to coalesce uncharges.
  1729. * But we do uncharge one by one if this is killed by OOM(TIF_MEMDIE)
  1730. * because we want to do uncharge as soon as possible.
  1731. */
  1732. if (!current->memcg_batch.do_batch || test_thread_flag(TIF_MEMDIE))
  1733. goto direct_uncharge;
  1734. batch = &current->memcg_batch;
  1735. /*
  1736. * In usual, we do css_get() when we remember memcg pointer.
  1737. * But in this case, we keep res->usage until end of a series of
  1738. * uncharges. Then, it's ok to ignore memcg's refcnt.
  1739. */
  1740. if (!batch->memcg)
  1741. batch->memcg = mem;
  1742. /*
  1743. * In typical case, batch->memcg == mem. This means we can
  1744. * merge a series of uncharges to an uncharge of res_counter.
  1745. * If not, we uncharge res_counter ony by one.
  1746. */
  1747. if (batch->memcg != mem)
  1748. goto direct_uncharge;
  1749. /* remember freed charge and uncharge it later */
  1750. batch->bytes += PAGE_SIZE;
  1751. if (uncharge_memsw)
  1752. batch->memsw_bytes += PAGE_SIZE;
  1753. return;
  1754. direct_uncharge:
  1755. res_counter_uncharge(&mem->res, PAGE_SIZE);
  1756. if (uncharge_memsw)
  1757. res_counter_uncharge(&mem->memsw, PAGE_SIZE);
  1758. return;
  1759. }
  1760. /*
  1761. * uncharge if !page_mapped(page)
  1762. */
  1763. static struct mem_cgroup *
  1764. __mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
  1765. {
  1766. struct page_cgroup *pc;
  1767. struct mem_cgroup *mem = NULL;
  1768. struct mem_cgroup_per_zone *mz;
  1769. if (mem_cgroup_disabled())
  1770. return NULL;
  1771. if (PageSwapCache(page))
  1772. return NULL;
  1773. /*
  1774. * Check if our page_cgroup is valid
  1775. */
  1776. pc = lookup_page_cgroup(page);
  1777. if (unlikely(!pc || !PageCgroupUsed(pc)))
  1778. return NULL;
  1779. lock_page_cgroup(pc);
  1780. mem = pc->mem_cgroup;
  1781. if (!PageCgroupUsed(pc))
  1782. goto unlock_out;
  1783. switch (ctype) {
  1784. case MEM_CGROUP_CHARGE_TYPE_MAPPED:
  1785. case MEM_CGROUP_CHARGE_TYPE_DROP:
  1786. if (page_mapped(page))
  1787. goto unlock_out;
  1788. break;
  1789. case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
  1790. if (!PageAnon(page)) { /* Shared memory */
  1791. if (page->mapping && !page_is_file_cache(page))
  1792. goto unlock_out;
  1793. } else if (page_mapped(page)) /* Anon */
  1794. goto unlock_out;
  1795. break;
  1796. default:
  1797. break;
  1798. }
  1799. if (!mem_cgroup_is_root(mem))
  1800. __do_uncharge(mem, ctype);
  1801. if (ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
  1802. mem_cgroup_swap_statistics(mem, true);
  1803. mem_cgroup_charge_statistics(mem, pc, false);
  1804. ClearPageCgroupUsed(pc);
  1805. /*
  1806. * pc->mem_cgroup is not cleared here. It will be accessed when it's
  1807. * freed from LRU. This is safe because uncharged page is expected not
  1808. * to be reused (freed soon). Exception is SwapCache, it's handled by
  1809. * special functions.
  1810. */
  1811. mz = page_cgroup_zoneinfo(pc);
  1812. unlock_page_cgroup(pc);
  1813. if (mem_cgroup_soft_limit_check(mem))
  1814. mem_cgroup_update_tree(mem, page);
  1815. /* at swapout, this memcg will be accessed to record to swap */
  1816. if (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
  1817. css_put(&mem->css);
  1818. return mem;
  1819. unlock_out:
  1820. unlock_page_cgroup(pc);
  1821. return NULL;
  1822. }
  1823. void mem_cgroup_uncharge_page(struct page *page)
  1824. {
  1825. /* early check. */
  1826. if (page_mapped(page))
  1827. return;
  1828. if (page->mapping && !PageAnon(page))
  1829. return;
  1830. __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
  1831. }
  1832. void mem_cgroup_uncharge_cache_page(struct page *page)
  1833. {
  1834. VM_BUG_ON(page_mapped(page));
  1835. VM_BUG_ON(page->mapping);
  1836. __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
  1837. }
  1838. /*
  1839. * Batch_start/batch_end is called in unmap_page_range/invlidate/trucate.
  1840. * In that cases, pages are freed continuously and we can expect pages
  1841. * are in the same memcg. All these calls itself limits the number of
  1842. * pages freed at once, then uncharge_start/end() is called properly.
  1843. * This may be called prural(2) times in a context,
  1844. */
  1845. void mem_cgroup_uncharge_start(void)
  1846. {
  1847. current->memcg_batch.do_batch++;
  1848. /* We can do nest. */
  1849. if (current->memcg_batch.do_batch == 1) {
  1850. current->memcg_batch.memcg = NULL;
  1851. current->memcg_batch.bytes = 0;
  1852. current->memcg_batch.memsw_bytes = 0;
  1853. }
  1854. }
  1855. void mem_cgroup_uncharge_end(void)
  1856. {
  1857. struct memcg_batch_info *batch = &current->memcg_batch;
  1858. if (!batch->do_batch)
  1859. return;
  1860. batch->do_batch--;
  1861. if (batch->do_batch) /* If stacked, do nothing. */
  1862. return;
  1863. if (!batch->memcg)
  1864. return;
  1865. /*
  1866. * This "batch->memcg" is valid without any css_get/put etc...
  1867. * bacause we hide charges behind us.
  1868. */
  1869. if (batch->bytes)
  1870. res_counter_uncharge(&batch->memcg->res, batch->bytes);
  1871. if (batch->memsw_bytes)
  1872. res_counter_uncharge(&batch->memcg->memsw, batch->memsw_bytes);
  1873. /* forget this pointer (for sanity check) */
  1874. batch->memcg = NULL;
  1875. }
  1876. #ifdef CONFIG_SWAP
  1877. /*
  1878. * called after __delete_from_swap_cache() and drop "page" account.
  1879. * memcg information is recorded to swap_cgroup of "ent"
  1880. */
  1881. void
  1882. mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
  1883. {
  1884. struct mem_cgroup *memcg;
  1885. int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT;
  1886. if (!swapout) /* this was a swap cache but the swap is unused ! */
  1887. ctype = MEM_CGROUP_CHARGE_TYPE_DROP;
  1888. memcg = __mem_cgroup_uncharge_common(page, ctype);
  1889. /* record memcg information */
  1890. if (do_swap_account && swapout && memcg) {
  1891. swap_cgroup_record(ent, css_id(&memcg->css));
  1892. mem_cgroup_get(memcg);
  1893. }
  1894. if (swapout && memcg)
  1895. css_put(&memcg->css);
  1896. }
  1897. #endif
  1898. #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
  1899. /*
  1900. * called from swap_entry_free(). remove record in swap_cgroup and
  1901. * uncharge "memsw" account.
  1902. */
  1903. void mem_cgroup_uncharge_swap(swp_entry_t ent)
  1904. {
  1905. struct mem_cgroup *memcg;
  1906. unsigned short id;
  1907. if (!do_swap_account)
  1908. return;
  1909. id = swap_cgroup_record(ent, 0);
  1910. rcu_read_lock();
  1911. memcg = mem_cgroup_lookup(id);
  1912. if (memcg) {
  1913. /*
  1914. * We uncharge this because swap is freed.
  1915. * This memcg can be obsolete one. We avoid calling css_tryget
  1916. */
  1917. if (!mem_cgroup_is_root(memcg))
  1918. res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
  1919. mem_cgroup_swap_statistics(memcg, false);
  1920. mem_cgroup_put(memcg);
  1921. }
  1922. rcu_read_unlock();
  1923. }
  1924. #endif
  1925. /*
  1926. * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
  1927. * page belongs to.
  1928. */
  1929. int mem_cgroup_prepare_migration(struct page *page, struct mem_cgroup **ptr)
  1930. {
  1931. struct page_cgroup *pc;
  1932. struct mem_cgroup *mem = NULL;
  1933. int ret = 0;
  1934. if (mem_cgroup_disabled())
  1935. return 0;
  1936. pc = lookup_page_cgroup(page);
  1937. lock_page_cgroup(pc);
  1938. if (PageCgroupUsed(pc)) {
  1939. mem = pc->mem_cgroup;
  1940. css_get(&mem->css);
  1941. }
  1942. unlock_page_cgroup(pc);
  1943. if (mem) {
  1944. ret = __mem_cgroup_try_charge(NULL, GFP_KERNEL, &mem, false,
  1945. page);
  1946. css_put(&mem->css);
  1947. }
  1948. *ptr = mem;
  1949. return ret;
  1950. }
  1951. /* remove redundant charge if migration failed*/
  1952. void mem_cgroup_end_migration(struct mem_cgroup *mem,
  1953. struct page *oldpage, struct page *newpage)
  1954. {
  1955. struct page *target, *unused;
  1956. struct page_cgroup *pc;
  1957. enum charge_type ctype;
  1958. if (!mem)
  1959. return;
  1960. cgroup_exclude_rmdir(&mem->css);
  1961. /* at migration success, oldpage->mapping is NULL. */
  1962. if (oldpage->mapping) {
  1963. target = oldpage;
  1964. unused = NULL;
  1965. } else {
  1966. target = newpage;
  1967. unused = oldpage;
  1968. }
  1969. if (PageAnon(target))
  1970. ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
  1971. else if (page_is_file_cache(target))
  1972. ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
  1973. else
  1974. ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
  1975. /* unused page is not on radix-tree now. */
  1976. if (unused)
  1977. __mem_cgroup_uncharge_common(unused, ctype);
  1978. pc = lookup_page_cgroup(target);
  1979. /*
  1980. * __mem_cgroup_commit_charge() check PCG_USED bit of page_cgroup.
  1981. * So, double-counting is effectively avoided.
  1982. */
  1983. __mem_cgroup_commit_charge(mem, pc, ctype);
  1984. /*
  1985. * Both of oldpage and newpage are still under lock_page().
  1986. * Then, we don't have to care about race in radix-tree.
  1987. * But we have to be careful that this page is unmapped or not.
  1988. *
  1989. * There is a case for !page_mapped(). At the start of
  1990. * migration, oldpage was mapped. But now, it's zapped.
  1991. * But we know *target* page is not freed/reused under us.
  1992. * mem_cgroup_uncharge_page() does all necessary checks.
  1993. */
  1994. if (ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED)
  1995. mem_cgroup_uncharge_page(target);
  1996. /*
  1997. * At migration, we may charge account against cgroup which has no tasks
  1998. * So, rmdir()->pre_destroy() can be called while we do this charge.
  1999. * In that case, we need to call pre_destroy() again. check it here.
  2000. */
  2001. cgroup_release_and_wakeup_rmdir(&mem->css);
  2002. }
  2003. /*
  2004. * A call to try to shrink memory usage on charge failure at shmem's swapin.
  2005. * Calling hierarchical_reclaim is not enough because we should update
  2006. * last_oom_jiffies to prevent pagefault_out_of_memory from invoking global OOM.
  2007. * Moreover considering hierarchy, we should reclaim from the mem_over_limit,
  2008. * not from the memcg which this page would be charged to.
  2009. * try_charge_swapin does all of these works properly.
  2010. */
  2011. int mem_cgroup_shmem_charge_fallback(struct page *page,
  2012. struct mm_struct *mm,
  2013. gfp_t gfp_mask)
  2014. {
  2015. struct mem_cgroup *mem = NULL;
  2016. int ret;
  2017. if (mem_cgroup_disabled())
  2018. return 0;
  2019. ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
  2020. if (!ret)
  2021. mem_cgroup_cancel_charge_swapin(mem); /* it does !mem check */
  2022. return ret;
  2023. }
  2024. static DEFINE_MUTEX(set_limit_mutex);
  2025. static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
  2026. unsigned long long val)
  2027. {
  2028. int retry_count;
  2029. u64 memswlimit;
  2030. int ret = 0;
  2031. int children = mem_cgroup_count_children(memcg);
  2032. u64 curusage, oldusage;
  2033. /*
  2034. * For keeping hierarchical_reclaim simple, how long we should retry
  2035. * is depends on callers. We set our retry-count to be function
  2036. * of # of children which we should visit in this loop.
  2037. */
  2038. retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
  2039. oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
  2040. while (retry_count) {
  2041. if (signal_pending(current)) {
  2042. ret = -EINTR;
  2043. break;
  2044. }
  2045. /*
  2046. * Rather than hide all in some function, I do this in
  2047. * open coded manner. You see what this really does.
  2048. * We have to guarantee mem->res.limit < mem->memsw.limit.
  2049. */
  2050. mutex_lock(&set_limit_mutex);
  2051. memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
  2052. if (memswlimit < val) {
  2053. ret = -EINVAL;
  2054. mutex_unlock(&set_limit_mutex);
  2055. break;
  2056. }
  2057. ret = res_counter_set_limit(&memcg->res, val);
  2058. if (!ret) {
  2059. if (memswlimit == val)
  2060. memcg->memsw_is_minimum = true;
  2061. else
  2062. memcg->memsw_is_minimum = false;
  2063. }
  2064. mutex_unlock(&set_limit_mutex);
  2065. if (!ret)
  2066. break;
  2067. mem_cgroup_hierarchical_reclaim(memcg, NULL, 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