memcontrol.c 108 KB

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