memcontrol.c 146 KB

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