memcontrol.c 86 KB

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