memcontrol.c 131 KB

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