memcontrol.c 127 KB

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