memcontrol.c 142 KB

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