memcontrol.c 110 KB

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