memcontrol.c 111 KB

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