memcontrol.c 156 KB

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