memcontrol.c 123 KB

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