memcontrol.c 141 KB

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