memcontrol.c 110 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358
  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. /*
  1122. * try to call OOM killer. returns false if we should exit memory-reclaim loop.
  1123. */
  1124. bool mem_cgroup_handle_oom(struct mem_cgroup *mem, gfp_t mask)
  1125. {
  1126. DEFINE_WAIT(wait);
  1127. bool locked;
  1128. /* At first, try to OOM lock hierarchy under mem.*/
  1129. mutex_lock(&memcg_oom_mutex);
  1130. locked = mem_cgroup_oom_lock(mem);
  1131. /*
  1132. * Even if signal_pending(), we can't quit charge() loop without
  1133. * accounting. So, UNINTERRUPTIBLE is appropriate. But SIGKILL
  1134. * under OOM is always welcomed, use TASK_KILLABLE here.
  1135. */
  1136. if (!locked)
  1137. prepare_to_wait(&memcg_oom_waitq, &wait, TASK_KILLABLE);
  1138. mutex_unlock(&memcg_oom_mutex);
  1139. if (locked)
  1140. mem_cgroup_out_of_memory(mem, mask);
  1141. else {
  1142. schedule();
  1143. finish_wait(&memcg_oom_waitq, &wait);
  1144. }
  1145. mutex_lock(&memcg_oom_mutex);
  1146. mem_cgroup_oom_unlock(mem);
  1147. /*
  1148. * Here, we use global waitq .....more fine grained waitq ?
  1149. * Assume following hierarchy.
  1150. * A/
  1151. * 01
  1152. * 02
  1153. * assume OOM happens both in A and 01 at the same time. Tthey are
  1154. * mutually exclusive by lock. (kill in 01 helps A.)
  1155. * When we use per memcg waitq, we have to wake up waiters on A and 02
  1156. * in addtion to waiters on 01. We use global waitq for avoiding mess.
  1157. * It will not be a big problem.
  1158. * (And a task may be moved to other groups while it's waiting for OOM.)
  1159. */
  1160. wake_up_all(&memcg_oom_waitq);
  1161. mutex_unlock(&memcg_oom_mutex);
  1162. if (test_thread_flag(TIF_MEMDIE) || fatal_signal_pending(current))
  1163. return false;
  1164. /* Give chance to dying process */
  1165. schedule_timeout(1);
  1166. return true;
  1167. }
  1168. /*
  1169. * Currently used to update mapped file statistics, but the routine can be
  1170. * generalized to update other statistics as well.
  1171. */
  1172. void mem_cgroup_update_file_mapped(struct page *page, int val)
  1173. {
  1174. struct mem_cgroup *mem;
  1175. struct page_cgroup *pc;
  1176. pc = lookup_page_cgroup(page);
  1177. if (unlikely(!pc))
  1178. return;
  1179. lock_page_cgroup(pc);
  1180. mem = pc->mem_cgroup;
  1181. if (!mem || !PageCgroupUsed(pc))
  1182. goto done;
  1183. /*
  1184. * Preemption is already disabled. We can use __this_cpu_xxx
  1185. */
  1186. if (val > 0) {
  1187. __this_cpu_inc(mem->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
  1188. SetPageCgroupFileMapped(pc);
  1189. } else {
  1190. __this_cpu_dec(mem->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
  1191. ClearPageCgroupFileMapped(pc);
  1192. }
  1193. done:
  1194. unlock_page_cgroup(pc);
  1195. }
  1196. /*
  1197. * size of first charge trial. "32" comes from vmscan.c's magic value.
  1198. * TODO: maybe necessary to use big numbers in big irons.
  1199. */
  1200. #define CHARGE_SIZE (32 * PAGE_SIZE)
  1201. struct memcg_stock_pcp {
  1202. struct mem_cgroup *cached; /* this never be root cgroup */
  1203. int charge;
  1204. struct work_struct work;
  1205. };
  1206. static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock);
  1207. static atomic_t memcg_drain_count;
  1208. /*
  1209. * Try to consume stocked charge on this cpu. If success, PAGE_SIZE is consumed
  1210. * from local stock and true is returned. If the stock is 0 or charges from a
  1211. * cgroup which is not current target, returns false. This stock will be
  1212. * refilled.
  1213. */
  1214. static bool consume_stock(struct mem_cgroup *mem)
  1215. {
  1216. struct memcg_stock_pcp *stock;
  1217. bool ret = true;
  1218. stock = &get_cpu_var(memcg_stock);
  1219. if (mem == stock->cached && stock->charge)
  1220. stock->charge -= PAGE_SIZE;
  1221. else /* need to call res_counter_charge */
  1222. ret = false;
  1223. put_cpu_var(memcg_stock);
  1224. return ret;
  1225. }
  1226. /*
  1227. * Returns stocks cached in percpu to res_counter and reset cached information.
  1228. */
  1229. static void drain_stock(struct memcg_stock_pcp *stock)
  1230. {
  1231. struct mem_cgroup *old = stock->cached;
  1232. if (stock->charge) {
  1233. res_counter_uncharge(&old->res, stock->charge);
  1234. if (do_swap_account)
  1235. res_counter_uncharge(&old->memsw, stock->charge);
  1236. }
  1237. stock->cached = NULL;
  1238. stock->charge = 0;
  1239. }
  1240. /*
  1241. * This must be called under preempt disabled or must be called by
  1242. * a thread which is pinned to local cpu.
  1243. */
  1244. static void drain_local_stock(struct work_struct *dummy)
  1245. {
  1246. struct memcg_stock_pcp *stock = &__get_cpu_var(memcg_stock);
  1247. drain_stock(stock);
  1248. }
  1249. /*
  1250. * Cache charges(val) which is from res_counter, to local per_cpu area.
  1251. * This will be consumed by consumt_stock() function, later.
  1252. */
  1253. static void refill_stock(struct mem_cgroup *mem, int val)
  1254. {
  1255. struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
  1256. if (stock->cached != mem) { /* reset if necessary */
  1257. drain_stock(stock);
  1258. stock->cached = mem;
  1259. }
  1260. stock->charge += val;
  1261. put_cpu_var(memcg_stock);
  1262. }
  1263. /*
  1264. * Tries to drain stocked charges in other cpus. This function is asynchronous
  1265. * and just put a work per cpu for draining localy on each cpu. Caller can
  1266. * expects some charges will be back to res_counter later but cannot wait for
  1267. * it.
  1268. */
  1269. static void drain_all_stock_async(void)
  1270. {
  1271. int cpu;
  1272. /* This function is for scheduling "drain" in asynchronous way.
  1273. * The result of "drain" is not directly handled by callers. Then,
  1274. * if someone is calling drain, we don't have to call drain more.
  1275. * Anyway, WORK_STRUCT_PENDING check in queue_work_on() will catch if
  1276. * there is a race. We just do loose check here.
  1277. */
  1278. if (atomic_read(&memcg_drain_count))
  1279. return;
  1280. /* Notify other cpus that system-wide "drain" is running */
  1281. atomic_inc(&memcg_drain_count);
  1282. get_online_cpus();
  1283. for_each_online_cpu(cpu) {
  1284. struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
  1285. schedule_work_on(cpu, &stock->work);
  1286. }
  1287. put_online_cpus();
  1288. atomic_dec(&memcg_drain_count);
  1289. /* We don't wait for flush_work */
  1290. }
  1291. /* This is a synchronous drain interface. */
  1292. static void drain_all_stock_sync(void)
  1293. {
  1294. /* called when force_empty is called */
  1295. atomic_inc(&memcg_drain_count);
  1296. schedule_on_each_cpu(drain_local_stock);
  1297. atomic_dec(&memcg_drain_count);
  1298. }
  1299. static int __cpuinit memcg_stock_cpu_callback(struct notifier_block *nb,
  1300. unsigned long action,
  1301. void *hcpu)
  1302. {
  1303. int cpu = (unsigned long)hcpu;
  1304. struct memcg_stock_pcp *stock;
  1305. if (action != CPU_DEAD)
  1306. return NOTIFY_OK;
  1307. stock = &per_cpu(memcg_stock, cpu);
  1308. drain_stock(stock);
  1309. return NOTIFY_OK;
  1310. }
  1311. /*
  1312. * Unlike exported interface, "oom" parameter is added. if oom==true,
  1313. * oom-killer can be invoked.
  1314. */
  1315. static int __mem_cgroup_try_charge(struct mm_struct *mm,
  1316. gfp_t gfp_mask, struct mem_cgroup **memcg, bool oom)
  1317. {
  1318. struct mem_cgroup *mem, *mem_over_limit;
  1319. int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
  1320. struct res_counter *fail_res;
  1321. int csize = CHARGE_SIZE;
  1322. /*
  1323. * Unlike gloval-vm's OOM-kill, we're not in memory shortage
  1324. * in system level. So, allow to go ahead dying process in addition to
  1325. * MEMDIE process.
  1326. */
  1327. if (unlikely(test_thread_flag(TIF_MEMDIE)
  1328. || fatal_signal_pending(current)))
  1329. goto bypass;
  1330. /*
  1331. * We always charge the cgroup the mm_struct belongs to.
  1332. * The mm_struct's mem_cgroup changes on task migration if the
  1333. * thread group leader migrates. It's possible that mm is not
  1334. * set, if so charge the init_mm (happens for pagecache usage).
  1335. */
  1336. mem = *memcg;
  1337. if (likely(!mem)) {
  1338. mem = try_get_mem_cgroup_from_mm(mm);
  1339. *memcg = mem;
  1340. } else {
  1341. css_get(&mem->css);
  1342. }
  1343. if (unlikely(!mem))
  1344. return 0;
  1345. VM_BUG_ON(css_is_removed(&mem->css));
  1346. if (mem_cgroup_is_root(mem))
  1347. goto done;
  1348. while (1) {
  1349. int ret = 0;
  1350. unsigned long flags = 0;
  1351. if (consume_stock(mem))
  1352. goto done;
  1353. ret = res_counter_charge(&mem->res, csize, &fail_res);
  1354. if (likely(!ret)) {
  1355. if (!do_swap_account)
  1356. break;
  1357. ret = res_counter_charge(&mem->memsw, csize, &fail_res);
  1358. if (likely(!ret))
  1359. break;
  1360. /* mem+swap counter fails */
  1361. res_counter_uncharge(&mem->res, csize);
  1362. flags |= MEM_CGROUP_RECLAIM_NOSWAP;
  1363. mem_over_limit = mem_cgroup_from_res_counter(fail_res,
  1364. memsw);
  1365. } else
  1366. /* mem counter fails */
  1367. mem_over_limit = mem_cgroup_from_res_counter(fail_res,
  1368. res);
  1369. /* reduce request size and retry */
  1370. if (csize > PAGE_SIZE) {
  1371. csize = PAGE_SIZE;
  1372. continue;
  1373. }
  1374. if (!(gfp_mask & __GFP_WAIT))
  1375. goto nomem;
  1376. ret = mem_cgroup_hierarchical_reclaim(mem_over_limit, NULL,
  1377. gfp_mask, flags);
  1378. if (ret)
  1379. continue;
  1380. /*
  1381. * try_to_free_mem_cgroup_pages() might not give us a full
  1382. * picture of reclaim. Some pages are reclaimed and might be
  1383. * moved to swap cache or just unmapped from the cgroup.
  1384. * Check the limit again to see if the reclaim reduced the
  1385. * current usage of the cgroup before giving up
  1386. *
  1387. */
  1388. if (mem_cgroup_check_under_limit(mem_over_limit))
  1389. continue;
  1390. /* try to avoid oom while someone is moving charge */
  1391. if (mc.moving_task && current != mc.moving_task) {
  1392. struct mem_cgroup *from, *to;
  1393. bool do_continue = false;
  1394. /*
  1395. * There is a small race that "from" or "to" can be
  1396. * freed by rmdir, so we use css_tryget().
  1397. */
  1398. rcu_read_lock();
  1399. from = mc.from;
  1400. to = mc.to;
  1401. if (from && css_tryget(&from->css)) {
  1402. if (mem_over_limit->use_hierarchy)
  1403. do_continue = css_is_ancestor(
  1404. &from->css,
  1405. &mem_over_limit->css);
  1406. else
  1407. do_continue = (from == mem_over_limit);
  1408. css_put(&from->css);
  1409. }
  1410. if (!do_continue && to && css_tryget(&to->css)) {
  1411. if (mem_over_limit->use_hierarchy)
  1412. do_continue = css_is_ancestor(
  1413. &to->css,
  1414. &mem_over_limit->css);
  1415. else
  1416. do_continue = (to == mem_over_limit);
  1417. css_put(&to->css);
  1418. }
  1419. rcu_read_unlock();
  1420. if (do_continue) {
  1421. DEFINE_WAIT(wait);
  1422. prepare_to_wait(&mc.waitq, &wait,
  1423. TASK_INTERRUPTIBLE);
  1424. /* moving charge context might have finished. */
  1425. if (mc.moving_task)
  1426. schedule();
  1427. finish_wait(&mc.waitq, &wait);
  1428. continue;
  1429. }
  1430. }
  1431. if (!nr_retries--) {
  1432. if (!oom)
  1433. goto nomem;
  1434. if (mem_cgroup_handle_oom(mem_over_limit, gfp_mask)) {
  1435. nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
  1436. continue;
  1437. }
  1438. /* When we reach here, current task is dying .*/
  1439. css_put(&mem->css);
  1440. goto bypass;
  1441. }
  1442. }
  1443. if (csize > PAGE_SIZE)
  1444. refill_stock(mem, csize - PAGE_SIZE);
  1445. done:
  1446. return 0;
  1447. nomem:
  1448. css_put(&mem->css);
  1449. return -ENOMEM;
  1450. bypass:
  1451. *memcg = NULL;
  1452. return 0;
  1453. }
  1454. /*
  1455. * Somemtimes we have to undo a charge we got by try_charge().
  1456. * This function is for that and do uncharge, put css's refcnt.
  1457. * gotten by try_charge().
  1458. */
  1459. static void __mem_cgroup_cancel_charge(struct mem_cgroup *mem,
  1460. unsigned long count)
  1461. {
  1462. if (!mem_cgroup_is_root(mem)) {
  1463. res_counter_uncharge(&mem->res, PAGE_SIZE * count);
  1464. if (do_swap_account)
  1465. res_counter_uncharge(&mem->memsw, PAGE_SIZE * count);
  1466. VM_BUG_ON(test_bit(CSS_ROOT, &mem->css.flags));
  1467. WARN_ON_ONCE(count > INT_MAX);
  1468. __css_put(&mem->css, (int)count);
  1469. }
  1470. /* we don't need css_put for root */
  1471. }
  1472. static void mem_cgroup_cancel_charge(struct mem_cgroup *mem)
  1473. {
  1474. __mem_cgroup_cancel_charge(mem, 1);
  1475. }
  1476. /*
  1477. * A helper function to get mem_cgroup from ID. must be called under
  1478. * rcu_read_lock(). The caller must check css_is_removed() or some if
  1479. * it's concern. (dropping refcnt from swap can be called against removed
  1480. * memcg.)
  1481. */
  1482. static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
  1483. {
  1484. struct cgroup_subsys_state *css;
  1485. /* ID 0 is unused ID */
  1486. if (!id)
  1487. return NULL;
  1488. css = css_lookup(&mem_cgroup_subsys, id);
  1489. if (!css)
  1490. return NULL;
  1491. return container_of(css, struct mem_cgroup, css);
  1492. }
  1493. struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
  1494. {
  1495. struct mem_cgroup *mem = NULL;
  1496. struct page_cgroup *pc;
  1497. unsigned short id;
  1498. swp_entry_t ent;
  1499. VM_BUG_ON(!PageLocked(page));
  1500. pc = lookup_page_cgroup(page);
  1501. lock_page_cgroup(pc);
  1502. if (PageCgroupUsed(pc)) {
  1503. mem = pc->mem_cgroup;
  1504. if (mem && !css_tryget(&mem->css))
  1505. mem = NULL;
  1506. } else if (PageSwapCache(page)) {
  1507. ent.val = page_private(page);
  1508. id = lookup_swap_cgroup(ent);
  1509. rcu_read_lock();
  1510. mem = mem_cgroup_lookup(id);
  1511. if (mem && !css_tryget(&mem->css))
  1512. mem = NULL;
  1513. rcu_read_unlock();
  1514. }
  1515. unlock_page_cgroup(pc);
  1516. return mem;
  1517. }
  1518. /*
  1519. * commit a charge got by __mem_cgroup_try_charge() and makes page_cgroup to be
  1520. * USED state. If already USED, uncharge and return.
  1521. */
  1522. static void __mem_cgroup_commit_charge(struct mem_cgroup *mem,
  1523. struct page_cgroup *pc,
  1524. enum charge_type ctype)
  1525. {
  1526. /* try_charge() can return NULL to *memcg, taking care of it. */
  1527. if (!mem)
  1528. return;
  1529. lock_page_cgroup(pc);
  1530. if (unlikely(PageCgroupUsed(pc))) {
  1531. unlock_page_cgroup(pc);
  1532. mem_cgroup_cancel_charge(mem);
  1533. return;
  1534. }
  1535. pc->mem_cgroup = mem;
  1536. /*
  1537. * We access a page_cgroup asynchronously without lock_page_cgroup().
  1538. * Especially when a page_cgroup is taken from a page, pc->mem_cgroup
  1539. * is accessed after testing USED bit. To make pc->mem_cgroup visible
  1540. * before USED bit, we need memory barrier here.
  1541. * See mem_cgroup_add_lru_list(), etc.
  1542. */
  1543. smp_wmb();
  1544. switch (ctype) {
  1545. case MEM_CGROUP_CHARGE_TYPE_CACHE:
  1546. case MEM_CGROUP_CHARGE_TYPE_SHMEM:
  1547. SetPageCgroupCache(pc);
  1548. SetPageCgroupUsed(pc);
  1549. break;
  1550. case MEM_CGROUP_CHARGE_TYPE_MAPPED:
  1551. ClearPageCgroupCache(pc);
  1552. SetPageCgroupUsed(pc);
  1553. break;
  1554. default:
  1555. break;
  1556. }
  1557. mem_cgroup_charge_statistics(mem, pc, true);
  1558. unlock_page_cgroup(pc);
  1559. /*
  1560. * "charge_statistics" updated event counter. Then, check it.
  1561. * Insert ancestor (and ancestor's ancestors), to softlimit RB-tree.
  1562. * if they exceeds softlimit.
  1563. */
  1564. memcg_check_events(mem, pc->page);
  1565. }
  1566. /**
  1567. * __mem_cgroup_move_account - move account of the page
  1568. * @pc: page_cgroup of the page.
  1569. * @from: mem_cgroup which the page is moved from.
  1570. * @to: mem_cgroup which the page is moved to. @from != @to.
  1571. * @uncharge: whether we should call uncharge and css_put against @from.
  1572. *
  1573. * The caller must confirm following.
  1574. * - page is not on LRU (isolate_page() is useful.)
  1575. * - the pc is locked, used, and ->mem_cgroup points to @from.
  1576. *
  1577. * This function doesn't do "charge" nor css_get to new cgroup. It should be
  1578. * done by a caller(__mem_cgroup_try_charge would be usefull). If @uncharge is
  1579. * true, this function does "uncharge" from old cgroup, but it doesn't if
  1580. * @uncharge is false, so a caller should do "uncharge".
  1581. */
  1582. static void __mem_cgroup_move_account(struct page_cgroup *pc,
  1583. struct mem_cgroup *from, struct mem_cgroup *to, bool uncharge)
  1584. {
  1585. VM_BUG_ON(from == to);
  1586. VM_BUG_ON(PageLRU(pc->page));
  1587. VM_BUG_ON(!PageCgroupLocked(pc));
  1588. VM_BUG_ON(!PageCgroupUsed(pc));
  1589. VM_BUG_ON(pc->mem_cgroup != from);
  1590. if (PageCgroupFileMapped(pc)) {
  1591. /* Update mapped_file data for mem_cgroup */
  1592. preempt_disable();
  1593. __this_cpu_dec(from->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
  1594. __this_cpu_inc(to->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
  1595. preempt_enable();
  1596. }
  1597. mem_cgroup_charge_statistics(from, pc, false);
  1598. if (uncharge)
  1599. /* This is not "cancel", but cancel_charge does all we need. */
  1600. mem_cgroup_cancel_charge(from);
  1601. /* caller should have done css_get */
  1602. pc->mem_cgroup = to;
  1603. mem_cgroup_charge_statistics(to, pc, true);
  1604. /*
  1605. * We charges against "to" which may not have any tasks. Then, "to"
  1606. * can be under rmdir(). But in current implementation, caller of
  1607. * this function is just force_empty() and move charge, so it's
  1608. * garanteed that "to" is never removed. So, we don't check rmdir
  1609. * status here.
  1610. */
  1611. }
  1612. /*
  1613. * check whether the @pc is valid for moving account and call
  1614. * __mem_cgroup_move_account()
  1615. */
  1616. static int mem_cgroup_move_account(struct page_cgroup *pc,
  1617. struct mem_cgroup *from, struct mem_cgroup *to, bool uncharge)
  1618. {
  1619. int ret = -EINVAL;
  1620. lock_page_cgroup(pc);
  1621. if (PageCgroupUsed(pc) && pc->mem_cgroup == from) {
  1622. __mem_cgroup_move_account(pc, from, to, uncharge);
  1623. ret = 0;
  1624. }
  1625. unlock_page_cgroup(pc);
  1626. /*
  1627. * check events
  1628. */
  1629. memcg_check_events(to, pc->page);
  1630. memcg_check_events(from, pc->page);
  1631. return ret;
  1632. }
  1633. /*
  1634. * move charges to its parent.
  1635. */
  1636. static int mem_cgroup_move_parent(struct page_cgroup *pc,
  1637. struct mem_cgroup *child,
  1638. gfp_t gfp_mask)
  1639. {
  1640. struct page *page = pc->page;
  1641. struct cgroup *cg = child->css.cgroup;
  1642. struct cgroup *pcg = cg->parent;
  1643. struct mem_cgroup *parent;
  1644. int ret;
  1645. /* Is ROOT ? */
  1646. if (!pcg)
  1647. return -EINVAL;
  1648. ret = -EBUSY;
  1649. if (!get_page_unless_zero(page))
  1650. goto out;
  1651. if (isolate_lru_page(page))
  1652. goto put;
  1653. parent = mem_cgroup_from_cont(pcg);
  1654. ret = __mem_cgroup_try_charge(NULL, gfp_mask, &parent, false);
  1655. if (ret || !parent)
  1656. goto put_back;
  1657. ret = mem_cgroup_move_account(pc, child, parent, true);
  1658. if (ret)
  1659. mem_cgroup_cancel_charge(parent);
  1660. put_back:
  1661. putback_lru_page(page);
  1662. put:
  1663. put_page(page);
  1664. out:
  1665. return ret;
  1666. }
  1667. /*
  1668. * Charge the memory controller for page usage.
  1669. * Return
  1670. * 0 if the charge was successful
  1671. * < 0 if the cgroup is over its limit
  1672. */
  1673. static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
  1674. gfp_t gfp_mask, enum charge_type ctype,
  1675. struct mem_cgroup *memcg)
  1676. {
  1677. struct mem_cgroup *mem;
  1678. struct page_cgroup *pc;
  1679. int ret;
  1680. pc = lookup_page_cgroup(page);
  1681. /* can happen at boot */
  1682. if (unlikely(!pc))
  1683. return 0;
  1684. prefetchw(pc);
  1685. mem = memcg;
  1686. ret = __mem_cgroup_try_charge(mm, gfp_mask, &mem, true);
  1687. if (ret || !mem)
  1688. return ret;
  1689. __mem_cgroup_commit_charge(mem, pc, ctype);
  1690. return 0;
  1691. }
  1692. int mem_cgroup_newpage_charge(struct page *page,
  1693. struct mm_struct *mm, gfp_t gfp_mask)
  1694. {
  1695. if (mem_cgroup_disabled())
  1696. return 0;
  1697. if (PageCompound(page))
  1698. return 0;
  1699. /*
  1700. * If already mapped, we don't have to account.
  1701. * If page cache, page->mapping has address_space.
  1702. * But page->mapping may have out-of-use anon_vma pointer,
  1703. * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
  1704. * is NULL.
  1705. */
  1706. if (page_mapped(page) || (page->mapping && !PageAnon(page)))
  1707. return 0;
  1708. if (unlikely(!mm))
  1709. mm = &init_mm;
  1710. return mem_cgroup_charge_common(page, mm, gfp_mask,
  1711. MEM_CGROUP_CHARGE_TYPE_MAPPED, NULL);
  1712. }
  1713. static void
  1714. __mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
  1715. enum charge_type ctype);
  1716. int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
  1717. gfp_t gfp_mask)
  1718. {
  1719. struct mem_cgroup *mem = NULL;
  1720. int ret;
  1721. if (mem_cgroup_disabled())
  1722. return 0;
  1723. if (PageCompound(page))
  1724. return 0;
  1725. /*
  1726. * Corner case handling. This is called from add_to_page_cache()
  1727. * in usual. But some FS (shmem) precharges this page before calling it
  1728. * and call add_to_page_cache() with GFP_NOWAIT.
  1729. *
  1730. * For GFP_NOWAIT case, the page may be pre-charged before calling
  1731. * add_to_page_cache(). (See shmem.c) check it here and avoid to call
  1732. * charge twice. (It works but has to pay a bit larger cost.)
  1733. * And when the page is SwapCache, it should take swap information
  1734. * into account. This is under lock_page() now.
  1735. */
  1736. if (!(gfp_mask & __GFP_WAIT)) {
  1737. struct page_cgroup *pc;
  1738. pc = lookup_page_cgroup(page);
  1739. if (!pc)
  1740. return 0;
  1741. lock_page_cgroup(pc);
  1742. if (PageCgroupUsed(pc)) {
  1743. unlock_page_cgroup(pc);
  1744. return 0;
  1745. }
  1746. unlock_page_cgroup(pc);
  1747. }
  1748. if (unlikely(!mm && !mem))
  1749. mm = &init_mm;
  1750. if (page_is_file_cache(page))
  1751. return mem_cgroup_charge_common(page, mm, gfp_mask,
  1752. MEM_CGROUP_CHARGE_TYPE_CACHE, NULL);
  1753. /* shmem */
  1754. if (PageSwapCache(page)) {
  1755. ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
  1756. if (!ret)
  1757. __mem_cgroup_commit_charge_swapin(page, mem,
  1758. MEM_CGROUP_CHARGE_TYPE_SHMEM);
  1759. } else
  1760. ret = mem_cgroup_charge_common(page, mm, gfp_mask,
  1761. MEM_CGROUP_CHARGE_TYPE_SHMEM, mem);
  1762. return ret;
  1763. }
  1764. /*
  1765. * While swap-in, try_charge -> commit or cancel, the page is locked.
  1766. * And when try_charge() successfully returns, one refcnt to memcg without
  1767. * struct page_cgroup is acquired. This refcnt will be consumed by
  1768. * "commit()" or removed by "cancel()"
  1769. */
  1770. int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
  1771. struct page *page,
  1772. gfp_t mask, struct mem_cgroup **ptr)
  1773. {
  1774. struct mem_cgroup *mem;
  1775. int ret;
  1776. if (mem_cgroup_disabled())
  1777. return 0;
  1778. if (!do_swap_account)
  1779. goto charge_cur_mm;
  1780. /*
  1781. * A racing thread's fault, or swapoff, may have already updated
  1782. * the pte, and even removed page from swap cache: in those cases
  1783. * do_swap_page()'s pte_same() test will fail; but there's also a
  1784. * KSM case which does need to charge the page.
  1785. */
  1786. if (!PageSwapCache(page))
  1787. goto charge_cur_mm;
  1788. mem = try_get_mem_cgroup_from_page(page);
  1789. if (!mem)
  1790. goto charge_cur_mm;
  1791. *ptr = mem;
  1792. ret = __mem_cgroup_try_charge(NULL, mask, ptr, true);
  1793. /* drop extra refcnt from tryget */
  1794. css_put(&mem->css);
  1795. return ret;
  1796. charge_cur_mm:
  1797. if (unlikely(!mm))
  1798. mm = &init_mm;
  1799. return __mem_cgroup_try_charge(mm, mask, ptr, true);
  1800. }
  1801. static void
  1802. __mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
  1803. enum charge_type ctype)
  1804. {
  1805. struct page_cgroup *pc;
  1806. if (mem_cgroup_disabled())
  1807. return;
  1808. if (!ptr)
  1809. return;
  1810. cgroup_exclude_rmdir(&ptr->css);
  1811. pc = lookup_page_cgroup(page);
  1812. mem_cgroup_lru_del_before_commit_swapcache(page);
  1813. __mem_cgroup_commit_charge(ptr, pc, ctype);
  1814. mem_cgroup_lru_add_after_commit_swapcache(page);
  1815. /*
  1816. * Now swap is on-memory. This means this page may be
  1817. * counted both as mem and swap....double count.
  1818. * Fix it by uncharging from memsw. Basically, this SwapCache is stable
  1819. * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
  1820. * may call delete_from_swap_cache() before reach here.
  1821. */
  1822. if (do_swap_account && PageSwapCache(page)) {
  1823. swp_entry_t ent = {.val = page_private(page)};
  1824. unsigned short id;
  1825. struct mem_cgroup *memcg;
  1826. id = swap_cgroup_record(ent, 0);
  1827. rcu_read_lock();
  1828. memcg = mem_cgroup_lookup(id);
  1829. if (memcg) {
  1830. /*
  1831. * This recorded memcg can be obsolete one. So, avoid
  1832. * calling css_tryget
  1833. */
  1834. if (!mem_cgroup_is_root(memcg))
  1835. res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
  1836. mem_cgroup_swap_statistics(memcg, false);
  1837. mem_cgroup_put(memcg);
  1838. }
  1839. rcu_read_unlock();
  1840. }
  1841. /*
  1842. * At swapin, we may charge account against cgroup which has no tasks.
  1843. * So, rmdir()->pre_destroy() can be called while we do this charge.
  1844. * In that case, we need to call pre_destroy() again. check it here.
  1845. */
  1846. cgroup_release_and_wakeup_rmdir(&ptr->css);
  1847. }
  1848. void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr)
  1849. {
  1850. __mem_cgroup_commit_charge_swapin(page, ptr,
  1851. MEM_CGROUP_CHARGE_TYPE_MAPPED);
  1852. }
  1853. void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem)
  1854. {
  1855. if (mem_cgroup_disabled())
  1856. return;
  1857. if (!mem)
  1858. return;
  1859. mem_cgroup_cancel_charge(mem);
  1860. }
  1861. static void
  1862. __do_uncharge(struct mem_cgroup *mem, const enum charge_type ctype)
  1863. {
  1864. struct memcg_batch_info *batch = NULL;
  1865. bool uncharge_memsw = true;
  1866. /* If swapout, usage of swap doesn't decrease */
  1867. if (!do_swap_account || ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
  1868. uncharge_memsw = false;
  1869. /*
  1870. * do_batch > 0 when unmapping pages or inode invalidate/truncate.
  1871. * In those cases, all pages freed continously can be expected to be in
  1872. * the same cgroup and we have chance to coalesce uncharges.
  1873. * But we do uncharge one by one if this is killed by OOM(TIF_MEMDIE)
  1874. * because we want to do uncharge as soon as possible.
  1875. */
  1876. if (!current->memcg_batch.do_batch || test_thread_flag(TIF_MEMDIE))
  1877. goto direct_uncharge;
  1878. batch = &current->memcg_batch;
  1879. /*
  1880. * In usual, we do css_get() when we remember memcg pointer.
  1881. * But in this case, we keep res->usage until end of a series of
  1882. * uncharges. Then, it's ok to ignore memcg's refcnt.
  1883. */
  1884. if (!batch->memcg)
  1885. batch->memcg = mem;
  1886. /*
  1887. * In typical case, batch->memcg == mem. This means we can
  1888. * merge a series of uncharges to an uncharge of res_counter.
  1889. * If not, we uncharge res_counter ony by one.
  1890. */
  1891. if (batch->memcg != mem)
  1892. goto direct_uncharge;
  1893. /* remember freed charge and uncharge it later */
  1894. batch->bytes += PAGE_SIZE;
  1895. if (uncharge_memsw)
  1896. batch->memsw_bytes += PAGE_SIZE;
  1897. return;
  1898. direct_uncharge:
  1899. res_counter_uncharge(&mem->res, PAGE_SIZE);
  1900. if (uncharge_memsw)
  1901. res_counter_uncharge(&mem->memsw, PAGE_SIZE);
  1902. return;
  1903. }
  1904. /*
  1905. * uncharge if !page_mapped(page)
  1906. */
  1907. static struct mem_cgroup *
  1908. __mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
  1909. {
  1910. struct page_cgroup *pc;
  1911. struct mem_cgroup *mem = NULL;
  1912. struct mem_cgroup_per_zone *mz;
  1913. if (mem_cgroup_disabled())
  1914. return NULL;
  1915. if (PageSwapCache(page))
  1916. return NULL;
  1917. /*
  1918. * Check if our page_cgroup is valid
  1919. */
  1920. pc = lookup_page_cgroup(page);
  1921. if (unlikely(!pc || !PageCgroupUsed(pc)))
  1922. return NULL;
  1923. lock_page_cgroup(pc);
  1924. mem = pc->mem_cgroup;
  1925. if (!PageCgroupUsed(pc))
  1926. goto unlock_out;
  1927. switch (ctype) {
  1928. case MEM_CGROUP_CHARGE_TYPE_MAPPED:
  1929. case MEM_CGROUP_CHARGE_TYPE_DROP:
  1930. if (page_mapped(page))
  1931. goto unlock_out;
  1932. break;
  1933. case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
  1934. if (!PageAnon(page)) { /* Shared memory */
  1935. if (page->mapping && !page_is_file_cache(page))
  1936. goto unlock_out;
  1937. } else if (page_mapped(page)) /* Anon */
  1938. goto unlock_out;
  1939. break;
  1940. default:
  1941. break;
  1942. }
  1943. if (!mem_cgroup_is_root(mem))
  1944. __do_uncharge(mem, ctype);
  1945. if (ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
  1946. mem_cgroup_swap_statistics(mem, true);
  1947. mem_cgroup_charge_statistics(mem, pc, false);
  1948. ClearPageCgroupUsed(pc);
  1949. /*
  1950. * pc->mem_cgroup is not cleared here. It will be accessed when it's
  1951. * freed from LRU. This is safe because uncharged page is expected not
  1952. * to be reused (freed soon). Exception is SwapCache, it's handled by
  1953. * special functions.
  1954. */
  1955. mz = page_cgroup_zoneinfo(pc);
  1956. unlock_page_cgroup(pc);
  1957. memcg_check_events(mem, page);
  1958. /* at swapout, this memcg will be accessed to record to swap */
  1959. if (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
  1960. css_put(&mem->css);
  1961. return mem;
  1962. unlock_out:
  1963. unlock_page_cgroup(pc);
  1964. return NULL;
  1965. }
  1966. void mem_cgroup_uncharge_page(struct page *page)
  1967. {
  1968. /* early check. */
  1969. if (page_mapped(page))
  1970. return;
  1971. if (page->mapping && !PageAnon(page))
  1972. return;
  1973. __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
  1974. }
  1975. void mem_cgroup_uncharge_cache_page(struct page *page)
  1976. {
  1977. VM_BUG_ON(page_mapped(page));
  1978. VM_BUG_ON(page->mapping);
  1979. __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
  1980. }
  1981. /*
  1982. * Batch_start/batch_end is called in unmap_page_range/invlidate/trucate.
  1983. * In that cases, pages are freed continuously and we can expect pages
  1984. * are in the same memcg. All these calls itself limits the number of
  1985. * pages freed at once, then uncharge_start/end() is called properly.
  1986. * This may be called prural(2) times in a context,
  1987. */
  1988. void mem_cgroup_uncharge_start(void)
  1989. {
  1990. current->memcg_batch.do_batch++;
  1991. /* We can do nest. */
  1992. if (current->memcg_batch.do_batch == 1) {
  1993. current->memcg_batch.memcg = NULL;
  1994. current->memcg_batch.bytes = 0;
  1995. current->memcg_batch.memsw_bytes = 0;
  1996. }
  1997. }
  1998. void mem_cgroup_uncharge_end(void)
  1999. {
  2000. struct memcg_batch_info *batch = &current->memcg_batch;
  2001. if (!batch->do_batch)
  2002. return;
  2003. batch->do_batch--;
  2004. if (batch->do_batch) /* If stacked, do nothing. */
  2005. return;
  2006. if (!batch->memcg)
  2007. return;
  2008. /*
  2009. * This "batch->memcg" is valid without any css_get/put etc...
  2010. * bacause we hide charges behind us.
  2011. */
  2012. if (batch->bytes)
  2013. res_counter_uncharge(&batch->memcg->res, batch->bytes);
  2014. if (batch->memsw_bytes)
  2015. res_counter_uncharge(&batch->memcg->memsw, batch->memsw_bytes);
  2016. /* forget this pointer (for sanity check) */
  2017. batch->memcg = NULL;
  2018. }
  2019. #ifdef CONFIG_SWAP
  2020. /*
  2021. * called after __delete_from_swap_cache() and drop "page" account.
  2022. * memcg information is recorded to swap_cgroup of "ent"
  2023. */
  2024. void
  2025. mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
  2026. {
  2027. struct mem_cgroup *memcg;
  2028. int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT;
  2029. if (!swapout) /* this was a swap cache but the swap is unused ! */
  2030. ctype = MEM_CGROUP_CHARGE_TYPE_DROP;
  2031. memcg = __mem_cgroup_uncharge_common(page, ctype);
  2032. /* record memcg information */
  2033. if (do_swap_account && swapout && memcg) {
  2034. swap_cgroup_record(ent, css_id(&memcg->css));
  2035. mem_cgroup_get(memcg);
  2036. }
  2037. if (swapout && memcg)
  2038. css_put(&memcg->css);
  2039. }
  2040. #endif
  2041. #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
  2042. /*
  2043. * called from swap_entry_free(). remove record in swap_cgroup and
  2044. * uncharge "memsw" account.
  2045. */
  2046. void mem_cgroup_uncharge_swap(swp_entry_t ent)
  2047. {
  2048. struct mem_cgroup *memcg;
  2049. unsigned short id;
  2050. if (!do_swap_account)
  2051. return;
  2052. id = swap_cgroup_record(ent, 0);
  2053. rcu_read_lock();
  2054. memcg = mem_cgroup_lookup(id);
  2055. if (memcg) {
  2056. /*
  2057. * We uncharge this because swap is freed.
  2058. * This memcg can be obsolete one. We avoid calling css_tryget
  2059. */
  2060. if (!mem_cgroup_is_root(memcg))
  2061. res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
  2062. mem_cgroup_swap_statistics(memcg, false);
  2063. mem_cgroup_put(memcg);
  2064. }
  2065. rcu_read_unlock();
  2066. }
  2067. /**
  2068. * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record.
  2069. * @entry: swap entry to be moved
  2070. * @from: mem_cgroup which the entry is moved from
  2071. * @to: mem_cgroup which the entry is moved to
  2072. * @need_fixup: whether we should fixup res_counters and refcounts.
  2073. *
  2074. * It succeeds only when the swap_cgroup's record for this entry is the same
  2075. * as the mem_cgroup's id of @from.
  2076. *
  2077. * Returns 0 on success, -EINVAL on failure.
  2078. *
  2079. * The caller must have charged to @to, IOW, called res_counter_charge() about
  2080. * both res and memsw, and called css_get().
  2081. */
  2082. static int mem_cgroup_move_swap_account(swp_entry_t entry,
  2083. struct mem_cgroup *from, struct mem_cgroup *to, bool need_fixup)
  2084. {
  2085. unsigned short old_id, new_id;
  2086. old_id = css_id(&from->css);
  2087. new_id = css_id(&to->css);
  2088. if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) {
  2089. mem_cgroup_swap_statistics(from, false);
  2090. mem_cgroup_swap_statistics(to, true);
  2091. /*
  2092. * This function is only called from task migration context now.
  2093. * It postpones res_counter and refcount handling till the end
  2094. * of task migration(mem_cgroup_clear_mc()) for performance
  2095. * improvement. But we cannot postpone mem_cgroup_get(to)
  2096. * because if the process that has been moved to @to does
  2097. * swap-in, the refcount of @to might be decreased to 0.
  2098. */
  2099. mem_cgroup_get(to);
  2100. if (need_fixup) {
  2101. if (!mem_cgroup_is_root(from))
  2102. res_counter_uncharge(&from->memsw, PAGE_SIZE);
  2103. mem_cgroup_put(from);
  2104. /*
  2105. * we charged both to->res and to->memsw, so we should
  2106. * uncharge to->res.
  2107. */
  2108. if (!mem_cgroup_is_root(to))
  2109. res_counter_uncharge(&to->res, PAGE_SIZE);
  2110. css_put(&to->css);
  2111. }
  2112. return 0;
  2113. }
  2114. return -EINVAL;
  2115. }
  2116. #else
  2117. static inline int mem_cgroup_move_swap_account(swp_entry_t entry,
  2118. struct mem_cgroup *from, struct mem_cgroup *to, bool need_fixup)
  2119. {
  2120. return -EINVAL;
  2121. }
  2122. #endif
  2123. /*
  2124. * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
  2125. * page belongs to.
  2126. */
  2127. int mem_cgroup_prepare_migration(struct page *page, struct mem_cgroup **ptr)
  2128. {
  2129. struct page_cgroup *pc;
  2130. struct mem_cgroup *mem = NULL;
  2131. int ret = 0;
  2132. if (mem_cgroup_disabled())
  2133. return 0;
  2134. pc = lookup_page_cgroup(page);
  2135. lock_page_cgroup(pc);
  2136. if (PageCgroupUsed(pc)) {
  2137. mem = pc->mem_cgroup;
  2138. css_get(&mem->css);
  2139. }
  2140. unlock_page_cgroup(pc);
  2141. if (mem) {
  2142. ret = __mem_cgroup_try_charge(NULL, GFP_KERNEL, &mem, false);
  2143. css_put(&mem->css);
  2144. }
  2145. *ptr = mem;
  2146. return ret;
  2147. }
  2148. /* remove redundant charge if migration failed*/
  2149. void mem_cgroup_end_migration(struct mem_cgroup *mem,
  2150. struct page *oldpage, struct page *newpage)
  2151. {
  2152. struct page *target, *unused;
  2153. struct page_cgroup *pc;
  2154. enum charge_type ctype;
  2155. if (!mem)
  2156. return;
  2157. cgroup_exclude_rmdir(&mem->css);
  2158. /* at migration success, oldpage->mapping is NULL. */
  2159. if (oldpage->mapping) {
  2160. target = oldpage;
  2161. unused = NULL;
  2162. } else {
  2163. target = newpage;
  2164. unused = oldpage;
  2165. }
  2166. if (PageAnon(target))
  2167. ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
  2168. else if (page_is_file_cache(target))
  2169. ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
  2170. else
  2171. ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
  2172. /* unused page is not on radix-tree now. */
  2173. if (unused)
  2174. __mem_cgroup_uncharge_common(unused, ctype);
  2175. pc = lookup_page_cgroup(target);
  2176. /*
  2177. * __mem_cgroup_commit_charge() check PCG_USED bit of page_cgroup.
  2178. * So, double-counting is effectively avoided.
  2179. */
  2180. __mem_cgroup_commit_charge(mem, pc, ctype);
  2181. /*
  2182. * Both of oldpage and newpage are still under lock_page().
  2183. * Then, we don't have to care about race in radix-tree.
  2184. * But we have to be careful that this page is unmapped or not.
  2185. *
  2186. * There is a case for !page_mapped(). At the start of
  2187. * migration, oldpage was mapped. But now, it's zapped.
  2188. * But we know *target* page is not freed/reused under us.
  2189. * mem_cgroup_uncharge_page() does all necessary checks.
  2190. */
  2191. if (ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED)
  2192. mem_cgroup_uncharge_page(target);
  2193. /*
  2194. * At migration, we may charge account against cgroup which has no tasks
  2195. * So, rmdir()->pre_destroy() can be called while we do this charge.
  2196. * In that case, we need to call pre_destroy() again. check it here.
  2197. */
  2198. cgroup_release_and_wakeup_rmdir(&mem->css);
  2199. }
  2200. /*
  2201. * A call to try to shrink memory usage on charge failure at shmem's swapin.
  2202. * Calling hierarchical_reclaim is not enough because we should update
  2203. * last_oom_jiffies to prevent pagefault_out_of_memory from invoking global OOM.
  2204. * Moreover considering hierarchy, we should reclaim from the mem_over_limit,
  2205. * not from the memcg which this page would be charged to.
  2206. * try_charge_swapin does all of these works properly.
  2207. */
  2208. int mem_cgroup_shmem_charge_fallback(struct page *page,
  2209. struct mm_struct *mm,
  2210. gfp_t gfp_mask)
  2211. {
  2212. struct mem_cgroup *mem = NULL;
  2213. int ret;
  2214. if (mem_cgroup_disabled())
  2215. return 0;
  2216. ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
  2217. if (!ret)
  2218. mem_cgroup_cancel_charge_swapin(mem); /* it does !mem check */
  2219. return ret;
  2220. }
  2221. static DEFINE_MUTEX(set_limit_mutex);
  2222. static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
  2223. unsigned long long val)
  2224. {
  2225. int retry_count;
  2226. u64 memswlimit;
  2227. int ret = 0;
  2228. int children = mem_cgroup_count_children(memcg);
  2229. u64 curusage, oldusage;
  2230. /*
  2231. * For keeping hierarchical_reclaim simple, how long we should retry
  2232. * is depends on callers. We set our retry-count to be function
  2233. * of # of children which we should visit in this loop.
  2234. */
  2235. retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
  2236. oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
  2237. while (retry_count) {
  2238. if (signal_pending(current)) {
  2239. ret = -EINTR;
  2240. break;
  2241. }
  2242. /*
  2243. * Rather than hide all in some function, I do this in
  2244. * open coded manner. You see what this really does.
  2245. * We have to guarantee mem->res.limit < mem->memsw.limit.
  2246. */
  2247. mutex_lock(&set_limit_mutex);
  2248. memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
  2249. if (memswlimit < val) {
  2250. ret = -EINVAL;
  2251. mutex_unlock(&set_limit_mutex);
  2252. break;
  2253. }
  2254. ret = res_counter_set_limit(&memcg->res, val);
  2255. if (!ret) {
  2256. if (memswlimit == val)
  2257. memcg->memsw_is_minimum = true;
  2258. else
  2259. memcg->memsw_is_minimum = false;
  2260. }
  2261. mutex_unlock(&set_limit_mutex);
  2262. if (!ret)
  2263. break;
  2264. mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL,
  2265. MEM_CGROUP_RECLAIM_SHRINK);
  2266. curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
  2267. /* Usage is reduced ? */
  2268. if (curusage >= oldusage)
  2269. retry_count--;
  2270. else
  2271. oldusage = curusage;
  2272. }
  2273. return ret;
  2274. }
  2275. static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
  2276. unsigned long long val)
  2277. {
  2278. int retry_count;
  2279. u64 memlimit, oldusage, curusage;
  2280. int children = mem_cgroup_count_children(memcg);
  2281. int ret = -EBUSY;
  2282. /* see mem_cgroup_resize_res_limit */
  2283. retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
  2284. oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
  2285. while (retry_count) {
  2286. if (signal_pending(current)) {
  2287. ret = -EINTR;
  2288. break;
  2289. }
  2290. /*
  2291. * Rather than hide all in some function, I do this in
  2292. * open coded manner. You see what this really does.
  2293. * We have to guarantee mem->res.limit < mem->memsw.limit.
  2294. */
  2295. mutex_lock(&set_limit_mutex);
  2296. memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
  2297. if (memlimit > val) {
  2298. ret = -EINVAL;
  2299. mutex_unlock(&set_limit_mutex);
  2300. break;
  2301. }
  2302. ret = res_counter_set_limit(&memcg->memsw, val);
  2303. if (!ret) {
  2304. if (memlimit == val)
  2305. memcg->memsw_is_minimum = true;
  2306. else
  2307. memcg->memsw_is_minimum = false;
  2308. }
  2309. mutex_unlock(&set_limit_mutex);
  2310. if (!ret)
  2311. break;
  2312. mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL,
  2313. MEM_CGROUP_RECLAIM_NOSWAP |
  2314. MEM_CGROUP_RECLAIM_SHRINK);
  2315. curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
  2316. /* Usage is reduced ? */
  2317. if (curusage >= oldusage)
  2318. retry_count--;
  2319. else
  2320. oldusage = curusage;
  2321. }
  2322. return ret;
  2323. }
  2324. unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
  2325. gfp_t gfp_mask, int nid,
  2326. int zid)
  2327. {
  2328. unsigned long nr_reclaimed = 0;
  2329. struct mem_cgroup_per_zone *mz, *next_mz = NULL;
  2330. unsigned long reclaimed;
  2331. int loop = 0;
  2332. struct mem_cgroup_tree_per_zone *mctz;
  2333. unsigned long long excess;
  2334. if (order > 0)
  2335. return 0;
  2336. mctz = soft_limit_tree_node_zone(nid, zid);
  2337. /*
  2338. * This loop can run a while, specially if mem_cgroup's continuously
  2339. * keep exceeding their soft limit and putting the system under
  2340. * pressure
  2341. */
  2342. do {
  2343. if (next_mz)
  2344. mz = next_mz;
  2345. else
  2346. mz = mem_cgroup_largest_soft_limit_node(mctz);
  2347. if (!mz)
  2348. break;
  2349. reclaimed = mem_cgroup_hierarchical_reclaim(mz->mem, zone,
  2350. gfp_mask,
  2351. MEM_CGROUP_RECLAIM_SOFT);
  2352. nr_reclaimed += reclaimed;
  2353. spin_lock(&mctz->lock);
  2354. /*
  2355. * If we failed to reclaim anything from this memory cgroup
  2356. * it is time to move on to the next cgroup
  2357. */
  2358. next_mz = NULL;
  2359. if (!reclaimed) {
  2360. do {
  2361. /*
  2362. * Loop until we find yet another one.
  2363. *
  2364. * By the time we get the soft_limit lock
  2365. * again, someone might have aded the
  2366. * group back on the RB tree. Iterate to
  2367. * make sure we get a different mem.
  2368. * mem_cgroup_largest_soft_limit_node returns
  2369. * NULL if no other cgroup is present on
  2370. * the tree
  2371. */
  2372. next_mz =
  2373. __mem_cgroup_largest_soft_limit_node(mctz);
  2374. if (next_mz == mz) {
  2375. css_put(&next_mz->mem->css);
  2376. next_mz = NULL;
  2377. } else /* next_mz == NULL or other memcg */
  2378. break;
  2379. } while (1);
  2380. }
  2381. __mem_cgroup_remove_exceeded(mz->mem, mz, mctz);
  2382. excess = res_counter_soft_limit_excess(&mz->mem->res);
  2383. /*
  2384. * One school of thought says that we should not add
  2385. * back the node to the tree if reclaim returns 0.
  2386. * But our reclaim could return 0, simply because due
  2387. * to priority we are exposing a smaller subset of
  2388. * memory to reclaim from. Consider this as a longer
  2389. * term TODO.
  2390. */
  2391. /* If excess == 0, no tree ops */
  2392. __mem_cgroup_insert_exceeded(mz->mem, mz, mctz, excess);
  2393. spin_unlock(&mctz->lock);
  2394. css_put(&mz->mem->css);
  2395. loop++;
  2396. /*
  2397. * Could not reclaim anything and there are no more
  2398. * mem cgroups to try or we seem to be looping without
  2399. * reclaiming anything.
  2400. */
  2401. if (!nr_reclaimed &&
  2402. (next_mz == NULL ||
  2403. loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS))
  2404. break;
  2405. } while (!nr_reclaimed);
  2406. if (next_mz)
  2407. css_put(&next_mz->mem->css);
  2408. return nr_reclaimed;
  2409. }
  2410. /*
  2411. * This routine traverse page_cgroup in given list and drop them all.
  2412. * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
  2413. */
  2414. static int mem_cgroup_force_empty_list(struct mem_cgroup *mem,
  2415. int node, int zid, enum lru_list lru)
  2416. {
  2417. struct zone *zone;
  2418. struct mem_cgroup_per_zone *mz;
  2419. struct page_cgroup *pc, *busy;
  2420. unsigned long flags, loop;
  2421. struct list_head *list;
  2422. int ret = 0;
  2423. zone = &NODE_DATA(node)->node_zones[zid];
  2424. mz = mem_cgroup_zoneinfo(mem, node, zid);
  2425. list = &mz->lists[lru];
  2426. loop = MEM_CGROUP_ZSTAT(mz, lru);
  2427. /* give some margin against EBUSY etc...*/
  2428. loop += 256;
  2429. busy = NULL;
  2430. while (loop--) {
  2431. ret = 0;
  2432. spin_lock_irqsave(&zone->lru_lock, flags);
  2433. if (list_empty(list)) {
  2434. spin_unlock_irqrestore(&zone->lru_lock, flags);
  2435. break;
  2436. }
  2437. pc = list_entry(list->prev, struct page_cgroup, lru);
  2438. if (busy == pc) {
  2439. list_move(&pc->lru, list);
  2440. busy = NULL;
  2441. spin_unlock_irqrestore(&zone->lru_lock, flags);
  2442. continue;
  2443. }
  2444. spin_unlock_irqrestore(&zone->lru_lock, flags);
  2445. ret = mem_cgroup_move_parent(pc, mem, GFP_KERNEL);
  2446. if (ret == -ENOMEM)
  2447. break;
  2448. if (ret == -EBUSY || ret == -EINVAL) {
  2449. /* found lock contention or "pc" is obsolete. */
  2450. busy = pc;
  2451. cond_resched();
  2452. } else
  2453. busy = NULL;
  2454. }
  2455. if (!ret && !list_empty(list))
  2456. return -EBUSY;
  2457. return ret;
  2458. }
  2459. /*
  2460. * make mem_cgroup's charge to be 0 if there is no task.
  2461. * This enables deleting this mem_cgroup.
  2462. */
  2463. static int mem_cgroup_force_empty(struct mem_cgroup *mem, bool free_all)
  2464. {
  2465. int ret;
  2466. int node, zid, shrink;
  2467. int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
  2468. struct cgroup *cgrp = mem->css.cgroup;
  2469. css_get(&mem->css);
  2470. shrink = 0;
  2471. /* should free all ? */
  2472. if (free_all)
  2473. goto try_to_free;
  2474. move_account:
  2475. do {
  2476. ret = -EBUSY;
  2477. if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
  2478. goto out;
  2479. ret = -EINTR;
  2480. if (signal_pending(current))
  2481. goto out;
  2482. /* This is for making all *used* pages to be on LRU. */
  2483. lru_add_drain_all();
  2484. drain_all_stock_sync();
  2485. ret = 0;
  2486. for_each_node_state(node, N_HIGH_MEMORY) {
  2487. for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) {
  2488. enum lru_list l;
  2489. for_each_lru(l) {
  2490. ret = mem_cgroup_force_empty_list(mem,
  2491. node, zid, l);
  2492. if (ret)
  2493. break;
  2494. }
  2495. }
  2496. if (ret)
  2497. break;
  2498. }
  2499. /* it seems parent cgroup doesn't have enough mem */
  2500. if (ret == -ENOMEM)
  2501. goto try_to_free;
  2502. cond_resched();
  2503. /* "ret" should also be checked to ensure all lists are empty. */
  2504. } while (mem->res.usage > 0 || ret);
  2505. out:
  2506. css_put(&mem->css);
  2507. return ret;
  2508. try_to_free:
  2509. /* returns EBUSY if there is a task or if we come here twice. */
  2510. if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) {
  2511. ret = -EBUSY;
  2512. goto out;
  2513. }
  2514. /* we call try-to-free pages for make this cgroup empty */
  2515. lru_add_drain_all();
  2516. /* try to free all pages in this cgroup */
  2517. shrink = 1;
  2518. while (nr_retries && mem->res.usage > 0) {
  2519. int progress;
  2520. if (signal_pending(current)) {
  2521. ret = -EINTR;
  2522. goto out;
  2523. }
  2524. progress = try_to_free_mem_cgroup_pages(mem, GFP_KERNEL,
  2525. false, get_swappiness(mem));
  2526. if (!progress) {
  2527. nr_retries--;
  2528. /* maybe some writeback is necessary */
  2529. congestion_wait(BLK_RW_ASYNC, HZ/10);
  2530. }
  2531. }
  2532. lru_add_drain();
  2533. /* try move_account...there may be some *locked* pages. */
  2534. goto move_account;
  2535. }
  2536. int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
  2537. {
  2538. return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true);
  2539. }
  2540. static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft)
  2541. {
  2542. return mem_cgroup_from_cont(cont)->use_hierarchy;
  2543. }
  2544. static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft,
  2545. u64 val)
  2546. {
  2547. int retval = 0;
  2548. struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
  2549. struct cgroup *parent = cont->parent;
  2550. struct mem_cgroup *parent_mem = NULL;
  2551. if (parent)
  2552. parent_mem = mem_cgroup_from_cont(parent);
  2553. cgroup_lock();
  2554. /*
  2555. * If parent's use_hierarchy is set, we can't make any modifications
  2556. * in the child subtrees. If it is unset, then the change can
  2557. * occur, provided the current cgroup has no children.
  2558. *
  2559. * For the root cgroup, parent_mem is NULL, we allow value to be
  2560. * set if there are no children.
  2561. */
  2562. if ((!parent_mem || !parent_mem->use_hierarchy) &&
  2563. (val == 1 || val == 0)) {
  2564. if (list_empty(&cont->children))
  2565. mem->use_hierarchy = val;
  2566. else
  2567. retval = -EBUSY;
  2568. } else
  2569. retval = -EINVAL;
  2570. cgroup_unlock();
  2571. return retval;
  2572. }
  2573. struct mem_cgroup_idx_data {
  2574. s64 val;
  2575. enum mem_cgroup_stat_index idx;
  2576. };
  2577. static int
  2578. mem_cgroup_get_idx_stat(struct mem_cgroup *mem, void *data)
  2579. {
  2580. struct mem_cgroup_idx_data *d = data;
  2581. d->val += mem_cgroup_read_stat(mem, d->idx);
  2582. return 0;
  2583. }
  2584. static void
  2585. mem_cgroup_get_recursive_idx_stat(struct mem_cgroup *mem,
  2586. enum mem_cgroup_stat_index idx, s64 *val)
  2587. {
  2588. struct mem_cgroup_idx_data d;
  2589. d.idx = idx;
  2590. d.val = 0;
  2591. mem_cgroup_walk_tree(mem, &d, mem_cgroup_get_idx_stat);
  2592. *val = d.val;
  2593. }
  2594. static inline u64 mem_cgroup_usage(struct mem_cgroup *mem, bool swap)
  2595. {
  2596. u64 idx_val, val;
  2597. if (!mem_cgroup_is_root(mem)) {
  2598. if (!swap)
  2599. return res_counter_read_u64(&mem->res, RES_USAGE);
  2600. else
  2601. return res_counter_read_u64(&mem->memsw, RES_USAGE);
  2602. }
  2603. mem_cgroup_get_recursive_idx_stat(mem, MEM_CGROUP_STAT_CACHE, &idx_val);
  2604. val = idx_val;
  2605. mem_cgroup_get_recursive_idx_stat(mem, MEM_CGROUP_STAT_RSS, &idx_val);
  2606. val += idx_val;
  2607. if (swap) {
  2608. mem_cgroup_get_recursive_idx_stat(mem,
  2609. MEM_CGROUP_STAT_SWAPOUT, &idx_val);
  2610. val += idx_val;
  2611. }
  2612. return val << PAGE_SHIFT;
  2613. }
  2614. static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
  2615. {
  2616. struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
  2617. u64 val;
  2618. int type, name;
  2619. type = MEMFILE_TYPE(cft->private);
  2620. name = MEMFILE_ATTR(cft->private);
  2621. switch (type) {
  2622. case _MEM:
  2623. if (name == RES_USAGE)
  2624. val = mem_cgroup_usage(mem, false);
  2625. else
  2626. val = res_counter_read_u64(&mem->res, name);
  2627. break;
  2628. case _MEMSWAP:
  2629. if (name == RES_USAGE)
  2630. val = mem_cgroup_usage(mem, true);
  2631. else
  2632. val = res_counter_read_u64(&mem->memsw, name);
  2633. break;
  2634. default:
  2635. BUG();
  2636. break;
  2637. }
  2638. return val;
  2639. }
  2640. /*
  2641. * The user of this function is...
  2642. * RES_LIMIT.
  2643. */
  2644. static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
  2645. const char *buffer)
  2646. {
  2647. struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
  2648. int type, name;
  2649. unsigned long long val;
  2650. int ret;
  2651. type = MEMFILE_TYPE(cft->private);
  2652. name = MEMFILE_ATTR(cft->private);
  2653. switch (name) {
  2654. case RES_LIMIT:
  2655. if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
  2656. ret = -EINVAL;
  2657. break;
  2658. }
  2659. /* This function does all necessary parse...reuse it */
  2660. ret = res_counter_memparse_write_strategy(buffer, &val);
  2661. if (ret)
  2662. break;
  2663. if (type == _MEM)
  2664. ret = mem_cgroup_resize_limit(memcg, val);
  2665. else
  2666. ret = mem_cgroup_resize_memsw_limit(memcg, val);
  2667. break;
  2668. case RES_SOFT_LIMIT:
  2669. ret = res_counter_memparse_write_strategy(buffer, &val);
  2670. if (ret)
  2671. break;
  2672. /*
  2673. * For memsw, soft limits are hard to implement in terms
  2674. * of semantics, for now, we support soft limits for
  2675. * control without swap
  2676. */
  2677. if (type == _MEM)
  2678. ret = res_counter_set_soft_limit(&memcg->res, val);
  2679. else
  2680. ret = -EINVAL;
  2681. break;
  2682. default:
  2683. ret = -EINVAL; /* should be BUG() ? */
  2684. break;
  2685. }
  2686. return ret;
  2687. }
  2688. static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
  2689. unsigned long long *mem_limit, unsigned long long *memsw_limit)
  2690. {
  2691. struct cgroup *cgroup;
  2692. unsigned long long min_limit, min_memsw_limit, tmp;
  2693. min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
  2694. min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
  2695. cgroup = memcg->css.cgroup;
  2696. if (!memcg->use_hierarchy)
  2697. goto out;
  2698. while (cgroup->parent) {
  2699. cgroup = cgroup->parent;
  2700. memcg = mem_cgroup_from_cont(cgroup);
  2701. if (!memcg->use_hierarchy)
  2702. break;
  2703. tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
  2704. min_limit = min(min_limit, tmp);
  2705. tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
  2706. min_memsw_limit = min(min_memsw_limit, tmp);
  2707. }
  2708. out:
  2709. *mem_limit = min_limit;
  2710. *memsw_limit = min_memsw_limit;
  2711. return;
  2712. }
  2713. static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
  2714. {
  2715. struct mem_cgroup *mem;
  2716. int type, name;
  2717. mem = mem_cgroup_from_cont(cont);
  2718. type = MEMFILE_TYPE(event);
  2719. name = MEMFILE_ATTR(event);
  2720. switch (name) {
  2721. case RES_MAX_USAGE:
  2722. if (type == _MEM)
  2723. res_counter_reset_max(&mem->res);
  2724. else
  2725. res_counter_reset_max(&mem->memsw);
  2726. break;
  2727. case RES_FAILCNT:
  2728. if (type == _MEM)
  2729. res_counter_reset_failcnt(&mem->res);
  2730. else
  2731. res_counter_reset_failcnt(&mem->memsw);
  2732. break;
  2733. }
  2734. return 0;
  2735. }
  2736. static u64 mem_cgroup_move_charge_read(struct cgroup *cgrp,
  2737. struct cftype *cft)
  2738. {
  2739. return mem_cgroup_from_cont(cgrp)->move_charge_at_immigrate;
  2740. }
  2741. #ifdef CONFIG_MMU
  2742. static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
  2743. struct cftype *cft, u64 val)
  2744. {
  2745. struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp);
  2746. if (val >= (1 << NR_MOVE_TYPE))
  2747. return -EINVAL;
  2748. /*
  2749. * We check this value several times in both in can_attach() and
  2750. * attach(), so we need cgroup lock to prevent this value from being
  2751. * inconsistent.
  2752. */
  2753. cgroup_lock();
  2754. mem->move_charge_at_immigrate = val;
  2755. cgroup_unlock();
  2756. return 0;
  2757. }
  2758. #else
  2759. static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
  2760. struct cftype *cft, u64 val)
  2761. {
  2762. return -ENOSYS;
  2763. }
  2764. #endif
  2765. /* For read statistics */
  2766. enum {
  2767. MCS_CACHE,
  2768. MCS_RSS,
  2769. MCS_FILE_MAPPED,
  2770. MCS_PGPGIN,
  2771. MCS_PGPGOUT,
  2772. MCS_SWAP,
  2773. MCS_INACTIVE_ANON,
  2774. MCS_ACTIVE_ANON,
  2775. MCS_INACTIVE_FILE,
  2776. MCS_ACTIVE_FILE,
  2777. MCS_UNEVICTABLE,
  2778. NR_MCS_STAT,
  2779. };
  2780. struct mcs_total_stat {
  2781. s64 stat[NR_MCS_STAT];
  2782. };
  2783. struct {
  2784. char *local_name;
  2785. char *total_name;
  2786. } memcg_stat_strings[NR_MCS_STAT] = {
  2787. {"cache", "total_cache"},
  2788. {"rss", "total_rss"},
  2789. {"mapped_file", "total_mapped_file"},
  2790. {"pgpgin", "total_pgpgin"},
  2791. {"pgpgout", "total_pgpgout"},
  2792. {"swap", "total_swap"},
  2793. {"inactive_anon", "total_inactive_anon"},
  2794. {"active_anon", "total_active_anon"},
  2795. {"inactive_file", "total_inactive_file"},
  2796. {"active_file", "total_active_file"},
  2797. {"unevictable", "total_unevictable"}
  2798. };
  2799. static int mem_cgroup_get_local_stat(struct mem_cgroup *mem, void *data)
  2800. {
  2801. struct mcs_total_stat *s = data;
  2802. s64 val;
  2803. /* per cpu stat */
  2804. val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_CACHE);
  2805. s->stat[MCS_CACHE] += val * PAGE_SIZE;
  2806. val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_RSS);
  2807. s->stat[MCS_RSS] += val * PAGE_SIZE;
  2808. val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_FILE_MAPPED);
  2809. s->stat[MCS_FILE_MAPPED] += val * PAGE_SIZE;
  2810. val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_PGPGIN_COUNT);
  2811. s->stat[MCS_PGPGIN] += val;
  2812. val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_PGPGOUT_COUNT);
  2813. s->stat[MCS_PGPGOUT] += val;
  2814. if (do_swap_account) {
  2815. val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_SWAPOUT);
  2816. s->stat[MCS_SWAP] += val * PAGE_SIZE;
  2817. }
  2818. /* per zone stat */
  2819. val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_ANON);
  2820. s->stat[MCS_INACTIVE_ANON] += val * PAGE_SIZE;
  2821. val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_ANON);
  2822. s->stat[MCS_ACTIVE_ANON] += val * PAGE_SIZE;
  2823. val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_FILE);
  2824. s->stat[MCS_INACTIVE_FILE] += val * PAGE_SIZE;
  2825. val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_FILE);
  2826. s->stat[MCS_ACTIVE_FILE] += val * PAGE_SIZE;
  2827. val = mem_cgroup_get_local_zonestat(mem, LRU_UNEVICTABLE);
  2828. s->stat[MCS_UNEVICTABLE] += val * PAGE_SIZE;
  2829. return 0;
  2830. }
  2831. static void
  2832. mem_cgroup_get_total_stat(struct mem_cgroup *mem, struct mcs_total_stat *s)
  2833. {
  2834. mem_cgroup_walk_tree(mem, s, mem_cgroup_get_local_stat);
  2835. }
  2836. static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
  2837. struct cgroup_map_cb *cb)
  2838. {
  2839. struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
  2840. struct mcs_total_stat mystat;
  2841. int i;
  2842. memset(&mystat, 0, sizeof(mystat));
  2843. mem_cgroup_get_local_stat(mem_cont, &mystat);
  2844. for (i = 0; i < NR_MCS_STAT; i++) {
  2845. if (i == MCS_SWAP && !do_swap_account)
  2846. continue;
  2847. cb->fill(cb, memcg_stat_strings[i].local_name, mystat.stat[i]);
  2848. }
  2849. /* Hierarchical information */
  2850. {
  2851. unsigned long long limit, memsw_limit;
  2852. memcg_get_hierarchical_limit(mem_cont, &limit, &memsw_limit);
  2853. cb->fill(cb, "hierarchical_memory_limit", limit);
  2854. if (do_swap_account)
  2855. cb->fill(cb, "hierarchical_memsw_limit", memsw_limit);
  2856. }
  2857. memset(&mystat, 0, sizeof(mystat));
  2858. mem_cgroup_get_total_stat(mem_cont, &mystat);
  2859. for (i = 0; i < NR_MCS_STAT; i++) {
  2860. if (i == MCS_SWAP && !do_swap_account)
  2861. continue;
  2862. cb->fill(cb, memcg_stat_strings[i].total_name, mystat.stat[i]);
  2863. }
  2864. #ifdef CONFIG_DEBUG_VM
  2865. cb->fill(cb, "inactive_ratio", calc_inactive_ratio(mem_cont, NULL));
  2866. {
  2867. int nid, zid;
  2868. struct mem_cgroup_per_zone *mz;
  2869. unsigned long recent_rotated[2] = {0, 0};
  2870. unsigned long recent_scanned[2] = {0, 0};
  2871. for_each_online_node(nid)
  2872. for (zid = 0; zid < MAX_NR_ZONES; zid++) {
  2873. mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
  2874. recent_rotated[0] +=
  2875. mz->reclaim_stat.recent_rotated[0];
  2876. recent_rotated[1] +=
  2877. mz->reclaim_stat.recent_rotated[1];
  2878. recent_scanned[0] +=
  2879. mz->reclaim_stat.recent_scanned[0];
  2880. recent_scanned[1] +=
  2881. mz->reclaim_stat.recent_scanned[1];
  2882. }
  2883. cb->fill(cb, "recent_rotated_anon", recent_rotated[0]);
  2884. cb->fill(cb, "recent_rotated_file", recent_rotated[1]);
  2885. cb->fill(cb, "recent_scanned_anon", recent_scanned[0]);
  2886. cb->fill(cb, "recent_scanned_file", recent_scanned[1]);
  2887. }
  2888. #endif
  2889. return 0;
  2890. }
  2891. static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft)
  2892. {
  2893. struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
  2894. return get_swappiness(memcg);
  2895. }
  2896. static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
  2897. u64 val)
  2898. {
  2899. struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
  2900. struct mem_cgroup *parent;
  2901. if (val > 100)
  2902. return -EINVAL;
  2903. if (cgrp->parent == NULL)
  2904. return -EINVAL;
  2905. parent = mem_cgroup_from_cont(cgrp->parent);
  2906. cgroup_lock();
  2907. /* If under hierarchy, only empty-root can set this value */
  2908. if ((parent->use_hierarchy) ||
  2909. (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
  2910. cgroup_unlock();
  2911. return -EINVAL;
  2912. }
  2913. spin_lock(&memcg->reclaim_param_lock);
  2914. memcg->swappiness = val;
  2915. spin_unlock(&memcg->reclaim_param_lock);
  2916. cgroup_unlock();
  2917. return 0;
  2918. }
  2919. static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap)
  2920. {
  2921. struct mem_cgroup_threshold_ary *t;
  2922. u64 usage;
  2923. int i;
  2924. rcu_read_lock();
  2925. if (!swap)
  2926. t = rcu_dereference(memcg->thresholds);
  2927. else
  2928. t = rcu_dereference(memcg->memsw_thresholds);
  2929. if (!t)
  2930. goto unlock;
  2931. usage = mem_cgroup_usage(memcg, swap);
  2932. /*
  2933. * current_threshold points to threshold just below usage.
  2934. * If it's not true, a threshold was crossed after last
  2935. * call of __mem_cgroup_threshold().
  2936. */
  2937. i = atomic_read(&t->current_threshold);
  2938. /*
  2939. * Iterate backward over array of thresholds starting from
  2940. * current_threshold and check if a threshold is crossed.
  2941. * If none of thresholds below usage is crossed, we read
  2942. * only one element of the array here.
  2943. */
  2944. for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--)
  2945. eventfd_signal(t->entries[i].eventfd, 1);
  2946. /* i = current_threshold + 1 */
  2947. i++;
  2948. /*
  2949. * Iterate forward over array of thresholds starting from
  2950. * current_threshold+1 and check if a threshold is crossed.
  2951. * If none of thresholds above usage is crossed, we read
  2952. * only one element of the array here.
  2953. */
  2954. for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++)
  2955. eventfd_signal(t->entries[i].eventfd, 1);
  2956. /* Update current_threshold */
  2957. atomic_set(&t->current_threshold, i - 1);
  2958. unlock:
  2959. rcu_read_unlock();
  2960. }
  2961. static void mem_cgroup_threshold(struct mem_cgroup *memcg)
  2962. {
  2963. __mem_cgroup_threshold(memcg, false);
  2964. if (do_swap_account)
  2965. __mem_cgroup_threshold(memcg, true);
  2966. }
  2967. static int compare_thresholds(const void *a, const void *b)
  2968. {
  2969. const struct mem_cgroup_threshold *_a = a;
  2970. const struct mem_cgroup_threshold *_b = b;
  2971. return _a->threshold - _b->threshold;
  2972. }
  2973. static int mem_cgroup_register_event(struct cgroup *cgrp, struct cftype *cft,
  2974. struct eventfd_ctx *eventfd, const char *args)
  2975. {
  2976. struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
  2977. struct mem_cgroup_threshold_ary *thresholds, *thresholds_new;
  2978. int type = MEMFILE_TYPE(cft->private);
  2979. u64 threshold, usage;
  2980. int size;
  2981. int i, ret;
  2982. ret = res_counter_memparse_write_strategy(args, &threshold);
  2983. if (ret)
  2984. return ret;
  2985. mutex_lock(&memcg->thresholds_lock);
  2986. if (type == _MEM)
  2987. thresholds = memcg->thresholds;
  2988. else if (type == _MEMSWAP)
  2989. thresholds = memcg->memsw_thresholds;
  2990. else
  2991. BUG();
  2992. usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
  2993. /* Check if a threshold crossed before adding a new one */
  2994. if (thresholds)
  2995. __mem_cgroup_threshold(memcg, type == _MEMSWAP);
  2996. if (thresholds)
  2997. size = thresholds->size + 1;
  2998. else
  2999. size = 1;
  3000. /* Allocate memory for new array of thresholds */
  3001. thresholds_new = kmalloc(sizeof(*thresholds_new) +
  3002. size * sizeof(struct mem_cgroup_threshold),
  3003. GFP_KERNEL);
  3004. if (!thresholds_new) {
  3005. ret = -ENOMEM;
  3006. goto unlock;
  3007. }
  3008. thresholds_new->size = size;
  3009. /* Copy thresholds (if any) to new array */
  3010. if (thresholds)
  3011. memcpy(thresholds_new->entries, thresholds->entries,
  3012. thresholds->size *
  3013. sizeof(struct mem_cgroup_threshold));
  3014. /* Add new threshold */
  3015. thresholds_new->entries[size - 1].eventfd = eventfd;
  3016. thresholds_new->entries[size - 1].threshold = threshold;
  3017. /* Sort thresholds. Registering of new threshold isn't time-critical */
  3018. sort(thresholds_new->entries, size,
  3019. sizeof(struct mem_cgroup_threshold),
  3020. compare_thresholds, NULL);
  3021. /* Find current threshold */
  3022. atomic_set(&thresholds_new->current_threshold, -1);
  3023. for (i = 0; i < size; i++) {
  3024. if (thresholds_new->entries[i].threshold < usage) {
  3025. /*
  3026. * thresholds_new->current_threshold will not be used
  3027. * until rcu_assign_pointer(), so it's safe to increment
  3028. * it here.
  3029. */
  3030. atomic_inc(&thresholds_new->current_threshold);
  3031. }
  3032. }
  3033. if (type == _MEM)
  3034. rcu_assign_pointer(memcg->thresholds, thresholds_new);
  3035. else
  3036. rcu_assign_pointer(memcg->memsw_thresholds, thresholds_new);
  3037. /* To be sure that nobody uses thresholds before freeing it */
  3038. synchronize_rcu();
  3039. kfree(thresholds);
  3040. unlock:
  3041. mutex_unlock(&memcg->thresholds_lock);
  3042. return ret;
  3043. }
  3044. static int mem_cgroup_unregister_event(struct cgroup *cgrp, struct cftype *cft,
  3045. struct eventfd_ctx *eventfd)
  3046. {
  3047. struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
  3048. struct mem_cgroup_threshold_ary *thresholds, *thresholds_new;
  3049. int type = MEMFILE_TYPE(cft->private);
  3050. u64 usage;
  3051. int size = 0;
  3052. int i, j, ret;
  3053. mutex_lock(&memcg->thresholds_lock);
  3054. if (type == _MEM)
  3055. thresholds = memcg->thresholds;
  3056. else if (type == _MEMSWAP)
  3057. thresholds = memcg->memsw_thresholds;
  3058. else
  3059. BUG();
  3060. /*
  3061. * Something went wrong if we trying to unregister a threshold
  3062. * if we don't have thresholds
  3063. */
  3064. BUG_ON(!thresholds);
  3065. usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
  3066. /* Check if a threshold crossed before removing */
  3067. __mem_cgroup_threshold(memcg, type == _MEMSWAP);
  3068. /* Calculate new number of threshold */
  3069. for (i = 0; i < thresholds->size; i++) {
  3070. if (thresholds->entries[i].eventfd != eventfd)
  3071. size++;
  3072. }
  3073. /* Set thresholds array to NULL if we don't have thresholds */
  3074. if (!size) {
  3075. thresholds_new = NULL;
  3076. goto assign;
  3077. }
  3078. /* Allocate memory for new array of thresholds */
  3079. thresholds_new = kmalloc(sizeof(*thresholds_new) +
  3080. size * sizeof(struct mem_cgroup_threshold),
  3081. GFP_KERNEL);
  3082. if (!thresholds_new) {
  3083. ret = -ENOMEM;
  3084. goto unlock;
  3085. }
  3086. thresholds_new->size = size;
  3087. /* Copy thresholds and find current threshold */
  3088. atomic_set(&thresholds_new->current_threshold, -1);
  3089. for (i = 0, j = 0; i < thresholds->size; i++) {
  3090. if (thresholds->entries[i].eventfd == eventfd)
  3091. continue;
  3092. thresholds_new->entries[j] = thresholds->entries[i];
  3093. if (thresholds_new->entries[j].threshold < usage) {
  3094. /*
  3095. * thresholds_new->current_threshold will not be used
  3096. * until rcu_assign_pointer(), so it's safe to increment
  3097. * it here.
  3098. */
  3099. atomic_inc(&thresholds_new->current_threshold);
  3100. }
  3101. j++;
  3102. }
  3103. assign:
  3104. if (type == _MEM)
  3105. rcu_assign_pointer(memcg->thresholds, thresholds_new);
  3106. else
  3107. rcu_assign_pointer(memcg->memsw_thresholds, thresholds_new);
  3108. /* To be sure that nobody uses thresholds before freeing it */
  3109. synchronize_rcu();
  3110. kfree(thresholds);
  3111. unlock:
  3112. mutex_unlock(&memcg->thresholds_lock);
  3113. return ret;
  3114. }
  3115. static struct cftype mem_cgroup_files[] = {
  3116. {
  3117. .name = "usage_in_bytes",
  3118. .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
  3119. .read_u64 = mem_cgroup_read,
  3120. .register_event = mem_cgroup_register_event,
  3121. .unregister_event = mem_cgroup_unregister_event,
  3122. },
  3123. {
  3124. .name = "max_usage_in_bytes",
  3125. .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
  3126. .trigger = mem_cgroup_reset,
  3127. .read_u64 = mem_cgroup_read,
  3128. },
  3129. {
  3130. .name = "limit_in_bytes",
  3131. .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
  3132. .write_string = mem_cgroup_write,
  3133. .read_u64 = mem_cgroup_read,
  3134. },
  3135. {
  3136. .name = "soft_limit_in_bytes",
  3137. .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
  3138. .write_string = mem_cgroup_write,
  3139. .read_u64 = mem_cgroup_read,
  3140. },
  3141. {
  3142. .name = "failcnt",
  3143. .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
  3144. .trigger = mem_cgroup_reset,
  3145. .read_u64 = mem_cgroup_read,
  3146. },
  3147. {
  3148. .name = "stat",
  3149. .read_map = mem_control_stat_show,
  3150. },
  3151. {
  3152. .name = "force_empty",
  3153. .trigger = mem_cgroup_force_empty_write,
  3154. },
  3155. {
  3156. .name = "use_hierarchy",
  3157. .write_u64 = mem_cgroup_hierarchy_write,
  3158. .read_u64 = mem_cgroup_hierarchy_read,
  3159. },
  3160. {
  3161. .name = "swappiness",
  3162. .read_u64 = mem_cgroup_swappiness_read,
  3163. .write_u64 = mem_cgroup_swappiness_write,
  3164. },
  3165. {
  3166. .name = "move_charge_at_immigrate",
  3167. .read_u64 = mem_cgroup_move_charge_read,
  3168. .write_u64 = mem_cgroup_move_charge_write,
  3169. },
  3170. };
  3171. #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
  3172. static struct cftype memsw_cgroup_files[] = {
  3173. {
  3174. .name = "memsw.usage_in_bytes",
  3175. .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
  3176. .read_u64 = mem_cgroup_read,
  3177. .register_event = mem_cgroup_register_event,
  3178. .unregister_event = mem_cgroup_unregister_event,
  3179. },
  3180. {
  3181. .name = "memsw.max_usage_in_bytes",
  3182. .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
  3183. .trigger = mem_cgroup_reset,
  3184. .read_u64 = mem_cgroup_read,
  3185. },
  3186. {
  3187. .name = "memsw.limit_in_bytes",
  3188. .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
  3189. .write_string = mem_cgroup_write,
  3190. .read_u64 = mem_cgroup_read,
  3191. },
  3192. {
  3193. .name = "memsw.failcnt",
  3194. .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
  3195. .trigger = mem_cgroup_reset,
  3196. .read_u64 = mem_cgroup_read,
  3197. },
  3198. };
  3199. static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
  3200. {
  3201. if (!do_swap_account)
  3202. return 0;
  3203. return cgroup_add_files(cont, ss, memsw_cgroup_files,
  3204. ARRAY_SIZE(memsw_cgroup_files));
  3205. };
  3206. #else
  3207. static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
  3208. {
  3209. return 0;
  3210. }
  3211. #endif
  3212. static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
  3213. {
  3214. struct mem_cgroup_per_node *pn;
  3215. struct mem_cgroup_per_zone *mz;
  3216. enum lru_list l;
  3217. int zone, tmp = node;
  3218. /*
  3219. * This routine is called against possible nodes.
  3220. * But it's BUG to call kmalloc() against offline node.
  3221. *
  3222. * TODO: this routine can waste much memory for nodes which will
  3223. * never be onlined. It's better to use memory hotplug callback
  3224. * function.
  3225. */
  3226. if (!node_state(node, N_NORMAL_MEMORY))
  3227. tmp = -1;
  3228. pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
  3229. if (!pn)
  3230. return 1;
  3231. mem->info.nodeinfo[node] = pn;
  3232. memset(pn, 0, sizeof(*pn));
  3233. for (zone = 0; zone < MAX_NR_ZONES; zone++) {
  3234. mz = &pn->zoneinfo[zone];
  3235. for_each_lru(l)
  3236. INIT_LIST_HEAD(&mz->lists[l]);
  3237. mz->usage_in_excess = 0;
  3238. mz->on_tree = false;
  3239. mz->mem = mem;
  3240. }
  3241. return 0;
  3242. }
  3243. static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
  3244. {
  3245. kfree(mem->info.nodeinfo[node]);
  3246. }
  3247. static struct mem_cgroup *mem_cgroup_alloc(void)
  3248. {
  3249. struct mem_cgroup *mem;
  3250. int size = sizeof(struct mem_cgroup);
  3251. /* Can be very big if MAX_NUMNODES is very big */
  3252. if (size < PAGE_SIZE)
  3253. mem = kmalloc(size, GFP_KERNEL);
  3254. else
  3255. mem = vmalloc(size);
  3256. if (!mem)
  3257. return NULL;
  3258. memset(mem, 0, size);
  3259. mem->stat = alloc_percpu(struct mem_cgroup_stat_cpu);
  3260. if (!mem->stat) {
  3261. if (size < PAGE_SIZE)
  3262. kfree(mem);
  3263. else
  3264. vfree(mem);
  3265. mem = NULL;
  3266. }
  3267. return mem;
  3268. }
  3269. /*
  3270. * At destroying mem_cgroup, references from swap_cgroup can remain.
  3271. * (scanning all at force_empty is too costly...)
  3272. *
  3273. * Instead of clearing all references at force_empty, we remember
  3274. * the number of reference from swap_cgroup and free mem_cgroup when
  3275. * it goes down to 0.
  3276. *
  3277. * Removal of cgroup itself succeeds regardless of refs from swap.
  3278. */
  3279. static void __mem_cgroup_free(struct mem_cgroup *mem)
  3280. {
  3281. int node;
  3282. mem_cgroup_remove_from_trees(mem);
  3283. free_css_id(&mem_cgroup_subsys, &mem->css);
  3284. for_each_node_state(node, N_POSSIBLE)
  3285. free_mem_cgroup_per_zone_info(mem, node);
  3286. free_percpu(mem->stat);
  3287. if (sizeof(struct mem_cgroup) < PAGE_SIZE)
  3288. kfree(mem);
  3289. else
  3290. vfree(mem);
  3291. }
  3292. static void mem_cgroup_get(struct mem_cgroup *mem)
  3293. {
  3294. atomic_inc(&mem->refcnt);
  3295. }
  3296. static void __mem_cgroup_put(struct mem_cgroup *mem, int count)
  3297. {
  3298. if (atomic_sub_and_test(count, &mem->refcnt)) {
  3299. struct mem_cgroup *parent = parent_mem_cgroup(mem);
  3300. __mem_cgroup_free(mem);
  3301. if (parent)
  3302. mem_cgroup_put(parent);
  3303. }
  3304. }
  3305. static void mem_cgroup_put(struct mem_cgroup *mem)
  3306. {
  3307. __mem_cgroup_put(mem, 1);
  3308. }
  3309. /*
  3310. * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
  3311. */
  3312. static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem)
  3313. {
  3314. if (!mem->res.parent)
  3315. return NULL;
  3316. return mem_cgroup_from_res_counter(mem->res.parent, res);
  3317. }
  3318. #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
  3319. static void __init enable_swap_cgroup(void)
  3320. {
  3321. if (!mem_cgroup_disabled() && really_do_swap_account)
  3322. do_swap_account = 1;
  3323. }
  3324. #else
  3325. static void __init enable_swap_cgroup(void)
  3326. {
  3327. }
  3328. #endif
  3329. static int mem_cgroup_soft_limit_tree_init(void)
  3330. {
  3331. struct mem_cgroup_tree_per_node *rtpn;
  3332. struct mem_cgroup_tree_per_zone *rtpz;
  3333. int tmp, node, zone;
  3334. for_each_node_state(node, N_POSSIBLE) {
  3335. tmp = node;
  3336. if (!node_state(node, N_NORMAL_MEMORY))
  3337. tmp = -1;
  3338. rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp);
  3339. if (!rtpn)
  3340. return 1;
  3341. soft_limit_tree.rb_tree_per_node[node] = rtpn;
  3342. for (zone = 0; zone < MAX_NR_ZONES; zone++) {
  3343. rtpz = &rtpn->rb_tree_per_zone[zone];
  3344. rtpz->rb_root = RB_ROOT;
  3345. spin_lock_init(&rtpz->lock);
  3346. }
  3347. }
  3348. return 0;
  3349. }
  3350. static struct cgroup_subsys_state * __ref
  3351. mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
  3352. {
  3353. struct mem_cgroup *mem, *parent;
  3354. long error = -ENOMEM;
  3355. int node;
  3356. mem = mem_cgroup_alloc();
  3357. if (!mem)
  3358. return ERR_PTR(error);
  3359. for_each_node_state(node, N_POSSIBLE)
  3360. if (alloc_mem_cgroup_per_zone_info(mem, node))
  3361. goto free_out;
  3362. /* root ? */
  3363. if (cont->parent == NULL) {
  3364. int cpu;
  3365. enable_swap_cgroup();
  3366. parent = NULL;
  3367. root_mem_cgroup = mem;
  3368. if (mem_cgroup_soft_limit_tree_init())
  3369. goto free_out;
  3370. for_each_possible_cpu(cpu) {
  3371. struct memcg_stock_pcp *stock =
  3372. &per_cpu(memcg_stock, cpu);
  3373. INIT_WORK(&stock->work, drain_local_stock);
  3374. }
  3375. hotcpu_notifier(memcg_stock_cpu_callback, 0);
  3376. } else {
  3377. parent = mem_cgroup_from_cont(cont->parent);
  3378. mem->use_hierarchy = parent->use_hierarchy;
  3379. }
  3380. if (parent && parent->use_hierarchy) {
  3381. res_counter_init(&mem->res, &parent->res);
  3382. res_counter_init(&mem->memsw, &parent->memsw);
  3383. /*
  3384. * We increment refcnt of the parent to ensure that we can
  3385. * safely access it on res_counter_charge/uncharge.
  3386. * This refcnt will be decremented when freeing this
  3387. * mem_cgroup(see mem_cgroup_put).
  3388. */
  3389. mem_cgroup_get(parent);
  3390. } else {
  3391. res_counter_init(&mem->res, NULL);
  3392. res_counter_init(&mem->memsw, NULL);
  3393. }
  3394. mem->last_scanned_child = 0;
  3395. spin_lock_init(&mem->reclaim_param_lock);
  3396. if (parent)
  3397. mem->swappiness = get_swappiness(parent);
  3398. atomic_set(&mem->refcnt, 1);
  3399. mem->move_charge_at_immigrate = 0;
  3400. mutex_init(&mem->thresholds_lock);
  3401. return &mem->css;
  3402. free_out:
  3403. __mem_cgroup_free(mem);
  3404. root_mem_cgroup = NULL;
  3405. return ERR_PTR(error);
  3406. }
  3407. static int mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
  3408. struct cgroup *cont)
  3409. {
  3410. struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
  3411. return mem_cgroup_force_empty(mem, false);
  3412. }
  3413. static void mem_cgroup_destroy(struct cgroup_subsys *ss,
  3414. struct cgroup *cont)
  3415. {
  3416. struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
  3417. mem_cgroup_put(mem);
  3418. }
  3419. static int mem_cgroup_populate(struct cgroup_subsys *ss,
  3420. struct cgroup *cont)
  3421. {
  3422. int ret;
  3423. ret = cgroup_add_files(cont, ss, mem_cgroup_files,
  3424. ARRAY_SIZE(mem_cgroup_files));
  3425. if (!ret)
  3426. ret = register_memsw_files(cont, ss);
  3427. return ret;
  3428. }
  3429. #ifdef CONFIG_MMU
  3430. /* Handlers for move charge at task migration. */
  3431. #define PRECHARGE_COUNT_AT_ONCE 256
  3432. static int mem_cgroup_do_precharge(unsigned long count)
  3433. {
  3434. int ret = 0;
  3435. int batch_count = PRECHARGE_COUNT_AT_ONCE;
  3436. struct mem_cgroup *mem = mc.to;
  3437. if (mem_cgroup_is_root(mem)) {
  3438. mc.precharge += count;
  3439. /* we don't need css_get for root */
  3440. return ret;
  3441. }
  3442. /* try to charge at once */
  3443. if (count > 1) {
  3444. struct res_counter *dummy;
  3445. /*
  3446. * "mem" cannot be under rmdir() because we've already checked
  3447. * by cgroup_lock_live_cgroup() that it is not removed and we
  3448. * are still under the same cgroup_mutex. So we can postpone
  3449. * css_get().
  3450. */
  3451. if (res_counter_charge(&mem->res, PAGE_SIZE * count, &dummy))
  3452. goto one_by_one;
  3453. if (do_swap_account && res_counter_charge(&mem->memsw,
  3454. PAGE_SIZE * count, &dummy)) {
  3455. res_counter_uncharge(&mem->res, PAGE_SIZE * count);
  3456. goto one_by_one;
  3457. }
  3458. mc.precharge += count;
  3459. VM_BUG_ON(test_bit(CSS_ROOT, &mem->css.flags));
  3460. WARN_ON_ONCE(count > INT_MAX);
  3461. __css_get(&mem->css, (int)count);
  3462. return ret;
  3463. }
  3464. one_by_one:
  3465. /* fall back to one by one charge */
  3466. while (count--) {
  3467. if (signal_pending(current)) {
  3468. ret = -EINTR;
  3469. break;
  3470. }
  3471. if (!batch_count--) {
  3472. batch_count = PRECHARGE_COUNT_AT_ONCE;
  3473. cond_resched();
  3474. }
  3475. ret = __mem_cgroup_try_charge(NULL, GFP_KERNEL, &mem, false);
  3476. if (ret || !mem)
  3477. /* mem_cgroup_clear_mc() will do uncharge later */
  3478. return -ENOMEM;
  3479. mc.precharge++;
  3480. }
  3481. return ret;
  3482. }
  3483. /**
  3484. * is_target_pte_for_mc - check a pte whether it is valid for move charge
  3485. * @vma: the vma the pte to be checked belongs
  3486. * @addr: the address corresponding to the pte to be checked
  3487. * @ptent: the pte to be checked
  3488. * @target: the pointer the target page or swap ent will be stored(can be NULL)
  3489. *
  3490. * Returns
  3491. * 0(MC_TARGET_NONE): if the pte is not a target for move charge.
  3492. * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for
  3493. * move charge. if @target is not NULL, the page is stored in target->page
  3494. * with extra refcnt got(Callers should handle it).
  3495. * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a
  3496. * target for charge migration. if @target is not NULL, the entry is stored
  3497. * in target->ent.
  3498. *
  3499. * Called with pte lock held.
  3500. */
  3501. union mc_target {
  3502. struct page *page;
  3503. swp_entry_t ent;
  3504. };
  3505. enum mc_target_type {
  3506. MC_TARGET_NONE, /* not used */
  3507. MC_TARGET_PAGE,
  3508. MC_TARGET_SWAP,
  3509. };
  3510. static int is_target_pte_for_mc(struct vm_area_struct *vma,
  3511. unsigned long addr, pte_t ptent, union mc_target *target)
  3512. {
  3513. struct page *page = NULL;
  3514. struct page_cgroup *pc;
  3515. int ret = 0;
  3516. swp_entry_t ent = { .val = 0 };
  3517. int usage_count = 0;
  3518. bool move_anon = test_bit(MOVE_CHARGE_TYPE_ANON,
  3519. &mc.to->move_charge_at_immigrate);
  3520. if (!pte_present(ptent)) {
  3521. /* TODO: handle swap of shmes/tmpfs */
  3522. if (pte_none(ptent) || pte_file(ptent))
  3523. return 0;
  3524. else if (is_swap_pte(ptent)) {
  3525. ent = pte_to_swp_entry(ptent);
  3526. if (!move_anon || non_swap_entry(ent))
  3527. return 0;
  3528. usage_count = mem_cgroup_count_swap_user(ent, &page);
  3529. }
  3530. } else {
  3531. page = vm_normal_page(vma, addr, ptent);
  3532. if (!page || !page_mapped(page))
  3533. return 0;
  3534. /*
  3535. * TODO: We don't move charges of file(including shmem/tmpfs)
  3536. * pages for now.
  3537. */
  3538. if (!move_anon || !PageAnon(page))
  3539. return 0;
  3540. if (!get_page_unless_zero(page))
  3541. return 0;
  3542. usage_count = page_mapcount(page);
  3543. }
  3544. if (usage_count > 1) {
  3545. /*
  3546. * TODO: We don't move charges of shared(used by multiple
  3547. * processes) pages for now.
  3548. */
  3549. if (page)
  3550. put_page(page);
  3551. return 0;
  3552. }
  3553. if (page) {
  3554. pc = lookup_page_cgroup(page);
  3555. /*
  3556. * Do only loose check w/o page_cgroup lock.
  3557. * mem_cgroup_move_account() checks the pc is valid or not under
  3558. * the lock.
  3559. */
  3560. if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
  3561. ret = MC_TARGET_PAGE;
  3562. if (target)
  3563. target->page = page;
  3564. }
  3565. if (!ret || !target)
  3566. put_page(page);
  3567. }
  3568. /* throught */
  3569. if (ent.val && do_swap_account && !ret &&
  3570. css_id(&mc.from->css) == lookup_swap_cgroup(ent)) {
  3571. ret = MC_TARGET_SWAP;
  3572. if (target)
  3573. target->ent = ent;
  3574. }
  3575. return ret;
  3576. }
  3577. static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd,
  3578. unsigned long addr, unsigned long end,
  3579. struct mm_walk *walk)
  3580. {
  3581. struct vm_area_struct *vma = walk->private;
  3582. pte_t *pte;
  3583. spinlock_t *ptl;
  3584. pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
  3585. for (; addr != end; pte++, addr += PAGE_SIZE)
  3586. if (is_target_pte_for_mc(vma, addr, *pte, NULL))
  3587. mc.precharge++; /* increment precharge temporarily */
  3588. pte_unmap_unlock(pte - 1, ptl);
  3589. cond_resched();
  3590. return 0;
  3591. }
  3592. static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm)
  3593. {
  3594. unsigned long precharge;
  3595. struct vm_area_struct *vma;
  3596. down_read(&mm->mmap_sem);
  3597. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  3598. struct mm_walk mem_cgroup_count_precharge_walk = {
  3599. .pmd_entry = mem_cgroup_count_precharge_pte_range,
  3600. .mm = mm,
  3601. .private = vma,
  3602. };
  3603. if (is_vm_hugetlb_page(vma))
  3604. continue;
  3605. /* TODO: We don't move charges of shmem/tmpfs pages for now. */
  3606. if (vma->vm_flags & VM_SHARED)
  3607. continue;
  3608. walk_page_range(vma->vm_start, vma->vm_end,
  3609. &mem_cgroup_count_precharge_walk);
  3610. }
  3611. up_read(&mm->mmap_sem);
  3612. precharge = mc.precharge;
  3613. mc.precharge = 0;
  3614. return precharge;
  3615. }
  3616. static int mem_cgroup_precharge_mc(struct mm_struct *mm)
  3617. {
  3618. return mem_cgroup_do_precharge(mem_cgroup_count_precharge(mm));
  3619. }
  3620. static void mem_cgroup_clear_mc(void)
  3621. {
  3622. /* we must uncharge all the leftover precharges from mc.to */
  3623. if (mc.precharge) {
  3624. __mem_cgroup_cancel_charge(mc.to, mc.precharge);
  3625. mc.precharge = 0;
  3626. }
  3627. /*
  3628. * we didn't uncharge from mc.from at mem_cgroup_move_account(), so
  3629. * we must uncharge here.
  3630. */
  3631. if (mc.moved_charge) {
  3632. __mem_cgroup_cancel_charge(mc.from, mc.moved_charge);
  3633. mc.moved_charge = 0;
  3634. }
  3635. /* we must fixup refcnts and charges */
  3636. if (mc.moved_swap) {
  3637. WARN_ON_ONCE(mc.moved_swap > INT_MAX);
  3638. /* uncharge swap account from the old cgroup */
  3639. if (!mem_cgroup_is_root(mc.from))
  3640. res_counter_uncharge(&mc.from->memsw,
  3641. PAGE_SIZE * mc.moved_swap);
  3642. __mem_cgroup_put(mc.from, mc.moved_swap);
  3643. if (!mem_cgroup_is_root(mc.to)) {
  3644. /*
  3645. * we charged both to->res and to->memsw, so we should
  3646. * uncharge to->res.
  3647. */
  3648. res_counter_uncharge(&mc.to->res,
  3649. PAGE_SIZE * mc.moved_swap);
  3650. VM_BUG_ON(test_bit(CSS_ROOT, &mc.to->css.flags));
  3651. __css_put(&mc.to->css, mc.moved_swap);
  3652. }
  3653. /* we've already done mem_cgroup_get(mc.to) */
  3654. mc.moved_swap = 0;
  3655. }
  3656. mc.from = NULL;
  3657. mc.to = NULL;
  3658. mc.moving_task = NULL;
  3659. wake_up_all(&mc.waitq);
  3660. }
  3661. static int mem_cgroup_can_attach(struct cgroup_subsys *ss,
  3662. struct cgroup *cgroup,
  3663. struct task_struct *p,
  3664. bool threadgroup)
  3665. {
  3666. int ret = 0;
  3667. struct mem_cgroup *mem = mem_cgroup_from_cont(cgroup);
  3668. if (mem->move_charge_at_immigrate) {
  3669. struct mm_struct *mm;
  3670. struct mem_cgroup *from = mem_cgroup_from_task(p);
  3671. VM_BUG_ON(from == mem);
  3672. mm = get_task_mm(p);
  3673. if (!mm)
  3674. return 0;
  3675. /* We move charges only when we move a owner of the mm */
  3676. if (mm->owner == p) {
  3677. VM_BUG_ON(mc.from);
  3678. VM_BUG_ON(mc.to);
  3679. VM_BUG_ON(mc.precharge);
  3680. VM_BUG_ON(mc.moved_charge);
  3681. VM_BUG_ON(mc.moved_swap);
  3682. VM_BUG_ON(mc.moving_task);
  3683. mc.from = from;
  3684. mc.to = mem;
  3685. mc.precharge = 0;
  3686. mc.moved_charge = 0;
  3687. mc.moved_swap = 0;
  3688. mc.moving_task = current;
  3689. ret = mem_cgroup_precharge_mc(mm);
  3690. if (ret)
  3691. mem_cgroup_clear_mc();
  3692. }
  3693. mmput(mm);
  3694. }
  3695. return ret;
  3696. }
  3697. static void mem_cgroup_cancel_attach(struct cgroup_subsys *ss,
  3698. struct cgroup *cgroup,
  3699. struct task_struct *p,
  3700. bool threadgroup)
  3701. {
  3702. mem_cgroup_clear_mc();
  3703. }
  3704. static int mem_cgroup_move_charge_pte_range(pmd_t *pmd,
  3705. unsigned long addr, unsigned long end,
  3706. struct mm_walk *walk)
  3707. {
  3708. int ret = 0;
  3709. struct vm_area_struct *vma = walk->private;
  3710. pte_t *pte;
  3711. spinlock_t *ptl;
  3712. retry:
  3713. pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
  3714. for (; addr != end; addr += PAGE_SIZE) {
  3715. pte_t ptent = *(pte++);
  3716. union mc_target target;
  3717. int type;
  3718. struct page *page;
  3719. struct page_cgroup *pc;
  3720. swp_entry_t ent;
  3721. if (!mc.precharge)
  3722. break;
  3723. type = is_target_pte_for_mc(vma, addr, ptent, &target);
  3724. switch (type) {
  3725. case MC_TARGET_PAGE:
  3726. page = target.page;
  3727. if (isolate_lru_page(page))
  3728. goto put;
  3729. pc = lookup_page_cgroup(page);
  3730. if (!mem_cgroup_move_account(pc,
  3731. mc.from, mc.to, false)) {
  3732. mc.precharge--;
  3733. /* we uncharge from mc.from later. */
  3734. mc.moved_charge++;
  3735. }
  3736. putback_lru_page(page);
  3737. put: /* is_target_pte_for_mc() gets the page */
  3738. put_page(page);
  3739. break;
  3740. case MC_TARGET_SWAP:
  3741. ent = target.ent;
  3742. if (!mem_cgroup_move_swap_account(ent,
  3743. mc.from, mc.to, false)) {
  3744. mc.precharge--;
  3745. /* we fixup refcnts and charges later. */
  3746. mc.moved_swap++;
  3747. }
  3748. break;
  3749. default:
  3750. break;
  3751. }
  3752. }
  3753. pte_unmap_unlock(pte - 1, ptl);
  3754. cond_resched();
  3755. if (addr != end) {
  3756. /*
  3757. * We have consumed all precharges we got in can_attach().
  3758. * We try charge one by one, but don't do any additional
  3759. * charges to mc.to if we have failed in charge once in attach()
  3760. * phase.
  3761. */
  3762. ret = mem_cgroup_do_precharge(1);
  3763. if (!ret)
  3764. goto retry;
  3765. }
  3766. return ret;
  3767. }
  3768. static void mem_cgroup_move_charge(struct mm_struct *mm)
  3769. {
  3770. struct vm_area_struct *vma;
  3771. lru_add_drain_all();
  3772. down_read(&mm->mmap_sem);
  3773. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  3774. int ret;
  3775. struct mm_walk mem_cgroup_move_charge_walk = {
  3776. .pmd_entry = mem_cgroup_move_charge_pte_range,
  3777. .mm = mm,
  3778. .private = vma,
  3779. };
  3780. if (is_vm_hugetlb_page(vma))
  3781. continue;
  3782. /* TODO: We don't move charges of shmem/tmpfs pages for now. */
  3783. if (vma->vm_flags & VM_SHARED)
  3784. continue;
  3785. ret = walk_page_range(vma->vm_start, vma->vm_end,
  3786. &mem_cgroup_move_charge_walk);
  3787. if (ret)
  3788. /*
  3789. * means we have consumed all precharges and failed in
  3790. * doing additional charge. Just abandon here.
  3791. */
  3792. break;
  3793. }
  3794. up_read(&mm->mmap_sem);
  3795. }
  3796. static void mem_cgroup_move_task(struct cgroup_subsys *ss,
  3797. struct cgroup *cont,
  3798. struct cgroup *old_cont,
  3799. struct task_struct *p,
  3800. bool threadgroup)
  3801. {
  3802. struct mm_struct *mm;
  3803. if (!mc.to)
  3804. /* no need to move charge */
  3805. return;
  3806. mm = get_task_mm(p);
  3807. if (mm) {
  3808. mem_cgroup_move_charge(mm);
  3809. mmput(mm);
  3810. }
  3811. mem_cgroup_clear_mc();
  3812. }
  3813. #else /* !CONFIG_MMU */
  3814. static int mem_cgroup_can_attach(struct cgroup_subsys *ss,
  3815. struct cgroup *cgroup,
  3816. struct task_struct *p,
  3817. bool threadgroup)
  3818. {
  3819. return 0;
  3820. }
  3821. static void mem_cgroup_cancel_attach(struct cgroup_subsys *ss,
  3822. struct cgroup *cgroup,
  3823. struct task_struct *p,
  3824. bool threadgroup)
  3825. {
  3826. }
  3827. static void mem_cgroup_move_task(struct cgroup_subsys *ss,
  3828. struct cgroup *cont,
  3829. struct cgroup *old_cont,
  3830. struct task_struct *p,
  3831. bool threadgroup)
  3832. {
  3833. }
  3834. #endif
  3835. struct cgroup_subsys mem_cgroup_subsys = {
  3836. .name = "memory",
  3837. .subsys_id = mem_cgroup_subsys_id,
  3838. .create = mem_cgroup_create,
  3839. .pre_destroy = mem_cgroup_pre_destroy,
  3840. .destroy = mem_cgroup_destroy,
  3841. .populate = mem_cgroup_populate,
  3842. .can_attach = mem_cgroup_can_attach,
  3843. .cancel_attach = mem_cgroup_cancel_attach,
  3844. .attach = mem_cgroup_move_task,
  3845. .early_init = 0,
  3846. .use_id = 1,
  3847. };
  3848. #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
  3849. static int __init disable_swap_account(char *s)
  3850. {
  3851. really_do_swap_account = 0;
  3852. return 1;
  3853. }
  3854. __setup("noswapaccount", disable_swap_account);
  3855. #endif