memcontrol.c 140 KB

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