memcontrol.c 142 KB

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