vmscan.c 119 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/mm/vmscan.c
  4. *
  5. * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
  6. *
  7. * Swap reorganised 29.12.95, Stephen Tweedie.
  8. * kswapd added: 7.1.96 sct
  9. * Removed kswapd_ctl limits, and swap out as many pages as needed
  10. * to bring the system back to freepages.high: 2.4.97, Rik van Riel.
  11. * Zone aware kswapd started 02/00, Kanoj Sarcar (kanoj@sgi.com).
  12. * Multiqueue VM started 5.8.00, Rik van Riel.
  13. */
  14. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  15. #include <linux/mm.h>
  16. #include <linux/sched/mm.h>
  17. #include <linux/module.h>
  18. #include <linux/gfp.h>
  19. #include <linux/kernel_stat.h>
  20. #include <linux/swap.h>
  21. #include <linux/pagemap.h>
  22. #include <linux/init.h>
  23. #include <linux/highmem.h>
  24. #include <linux/vmpressure.h>
  25. #include <linux/vmstat.h>
  26. #include <linux/file.h>
  27. #include <linux/writeback.h>
  28. #include <linux/blkdev.h>
  29. #include <linux/buffer_head.h> /* for try_to_release_page(),
  30. buffer_heads_over_limit */
  31. #include <linux/mm_inline.h>
  32. #include <linux/backing-dev.h>
  33. #include <linux/rmap.h>
  34. #include <linux/topology.h>
  35. #include <linux/cpu.h>
  36. #include <linux/cpuset.h>
  37. #include <linux/compaction.h>
  38. #include <linux/notifier.h>
  39. #include <linux/rwsem.h>
  40. #include <linux/delay.h>
  41. #include <linux/kthread.h>
  42. #include <linux/freezer.h>
  43. #include <linux/memcontrol.h>
  44. #include <linux/delayacct.h>
  45. #include <linux/sysctl.h>
  46. #include <linux/oom.h>
  47. #include <linux/prefetch.h>
  48. #include <linux/printk.h>
  49. #include <linux/dax.h>
  50. #include <asm/tlbflush.h>
  51. #include <asm/div64.h>
  52. #include <linux/swapops.h>
  53. #include <linux/balloon_compaction.h>
  54. #include "internal.h"
  55. #define CREATE_TRACE_POINTS
  56. #include <trace/events/vmscan.h>
  57. struct scan_control {
  58. /* How many pages shrink_list() should reclaim */
  59. unsigned long nr_to_reclaim;
  60. /*
  61. * Nodemask of nodes allowed by the caller. If NULL, all nodes
  62. * are scanned.
  63. */
  64. nodemask_t *nodemask;
  65. /*
  66. * The memory cgroup that hit its limit and as a result is the
  67. * primary target of this reclaim invocation.
  68. */
  69. struct mem_cgroup *target_mem_cgroup;
  70. /* Writepage batching in laptop mode; RECLAIM_WRITE */
  71. unsigned int may_writepage:1;
  72. /* Can mapped pages be reclaimed? */
  73. unsigned int may_unmap:1;
  74. /* Can pages be swapped as part of reclaim? */
  75. unsigned int may_swap:1;
  76. /*
  77. * Cgroups are not reclaimed below their configured memory.low,
  78. * unless we threaten to OOM. If any cgroups are skipped due to
  79. * memory.low and nothing was reclaimed, go back for memory.low.
  80. */
  81. unsigned int memcg_low_reclaim:1;
  82. unsigned int memcg_low_skipped:1;
  83. unsigned int hibernation_mode:1;
  84. /* One of the zones is ready for compaction */
  85. unsigned int compaction_ready:1;
  86. /* Allocation order */
  87. s8 order;
  88. /* Scan (total_size >> priority) pages at once */
  89. s8 priority;
  90. /* The highest zone to isolate pages for reclaim from */
  91. s8 reclaim_idx;
  92. /* This context's GFP mask */
  93. gfp_t gfp_mask;
  94. /* Incremented by the number of inactive pages that were scanned */
  95. unsigned long nr_scanned;
  96. /* Number of pages freed so far during a call to shrink_zones() */
  97. unsigned long nr_reclaimed;
  98. struct {
  99. unsigned int dirty;
  100. unsigned int unqueued_dirty;
  101. unsigned int congested;
  102. unsigned int writeback;
  103. unsigned int immediate;
  104. unsigned int file_taken;
  105. unsigned int taken;
  106. } nr;
  107. };
  108. #ifdef ARCH_HAS_PREFETCH
  109. #define prefetch_prev_lru_page(_page, _base, _field) \
  110. do { \
  111. if ((_page)->lru.prev != _base) { \
  112. struct page *prev; \
  113. \
  114. prev = lru_to_page(&(_page->lru)); \
  115. prefetch(&prev->_field); \
  116. } \
  117. } while (0)
  118. #else
  119. #define prefetch_prev_lru_page(_page, _base, _field) do { } while (0)
  120. #endif
  121. #ifdef ARCH_HAS_PREFETCHW
  122. #define prefetchw_prev_lru_page(_page, _base, _field) \
  123. do { \
  124. if ((_page)->lru.prev != _base) { \
  125. struct page *prev; \
  126. \
  127. prev = lru_to_page(&(_page->lru)); \
  128. prefetchw(&prev->_field); \
  129. } \
  130. } while (0)
  131. #else
  132. #define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0)
  133. #endif
  134. /*
  135. * From 0 .. 100. Higher means more swappy.
  136. */
  137. int vm_swappiness = 60;
  138. /*
  139. * The total number of pages which are beyond the high watermark within all
  140. * zones.
  141. */
  142. unsigned long vm_total_pages;
  143. static LIST_HEAD(shrinker_list);
  144. static DECLARE_RWSEM(shrinker_rwsem);
  145. #ifdef CONFIG_MEMCG_KMEM
  146. static DEFINE_IDR(shrinker_idr);
  147. static int shrinker_nr_max;
  148. static int prealloc_memcg_shrinker(struct shrinker *shrinker)
  149. {
  150. int id, ret = -ENOMEM;
  151. down_write(&shrinker_rwsem);
  152. /* This may call shrinker, so it must use down_read_trylock() */
  153. id = idr_alloc(&shrinker_idr, shrinker, 0, 0, GFP_KERNEL);
  154. if (id < 0)
  155. goto unlock;
  156. if (id >= shrinker_nr_max) {
  157. if (memcg_expand_shrinker_maps(id)) {
  158. idr_remove(&shrinker_idr, id);
  159. goto unlock;
  160. }
  161. shrinker_nr_max = id + 1;
  162. }
  163. shrinker->id = id;
  164. ret = 0;
  165. unlock:
  166. up_write(&shrinker_rwsem);
  167. return ret;
  168. }
  169. static void unregister_memcg_shrinker(struct shrinker *shrinker)
  170. {
  171. int id = shrinker->id;
  172. BUG_ON(id < 0);
  173. down_write(&shrinker_rwsem);
  174. idr_remove(&shrinker_idr, id);
  175. up_write(&shrinker_rwsem);
  176. }
  177. #else /* CONFIG_MEMCG_KMEM */
  178. static int prealloc_memcg_shrinker(struct shrinker *shrinker)
  179. {
  180. return 0;
  181. }
  182. static void unregister_memcg_shrinker(struct shrinker *shrinker)
  183. {
  184. }
  185. #endif /* CONFIG_MEMCG_KMEM */
  186. #ifdef CONFIG_MEMCG
  187. static bool global_reclaim(struct scan_control *sc)
  188. {
  189. return !sc->target_mem_cgroup;
  190. }
  191. /**
  192. * sane_reclaim - is the usual dirty throttling mechanism operational?
  193. * @sc: scan_control in question
  194. *
  195. * The normal page dirty throttling mechanism in balance_dirty_pages() is
  196. * completely broken with the legacy memcg and direct stalling in
  197. * shrink_page_list() is used for throttling instead, which lacks all the
  198. * niceties such as fairness, adaptive pausing, bandwidth proportional
  199. * allocation and configurability.
  200. *
  201. * This function tests whether the vmscan currently in progress can assume
  202. * that the normal dirty throttling mechanism is operational.
  203. */
  204. static bool sane_reclaim(struct scan_control *sc)
  205. {
  206. struct mem_cgroup *memcg = sc->target_mem_cgroup;
  207. if (!memcg)
  208. return true;
  209. #ifdef CONFIG_CGROUP_WRITEBACK
  210. if (cgroup_subsys_on_dfl(memory_cgrp_subsys))
  211. return true;
  212. #endif
  213. return false;
  214. }
  215. static void set_memcg_congestion(pg_data_t *pgdat,
  216. struct mem_cgroup *memcg,
  217. bool congested)
  218. {
  219. struct mem_cgroup_per_node *mn;
  220. if (!memcg)
  221. return;
  222. mn = mem_cgroup_nodeinfo(memcg, pgdat->node_id);
  223. WRITE_ONCE(mn->congested, congested);
  224. }
  225. static bool memcg_congested(pg_data_t *pgdat,
  226. struct mem_cgroup *memcg)
  227. {
  228. struct mem_cgroup_per_node *mn;
  229. mn = mem_cgroup_nodeinfo(memcg, pgdat->node_id);
  230. return READ_ONCE(mn->congested);
  231. }
  232. #else
  233. static bool global_reclaim(struct scan_control *sc)
  234. {
  235. return true;
  236. }
  237. static bool sane_reclaim(struct scan_control *sc)
  238. {
  239. return true;
  240. }
  241. static inline void set_memcg_congestion(struct pglist_data *pgdat,
  242. struct mem_cgroup *memcg, bool congested)
  243. {
  244. }
  245. static inline bool memcg_congested(struct pglist_data *pgdat,
  246. struct mem_cgroup *memcg)
  247. {
  248. return false;
  249. }
  250. #endif
  251. /*
  252. * This misses isolated pages which are not accounted for to save counters.
  253. * As the data only determines if reclaim or compaction continues, it is
  254. * not expected that isolated pages will be a dominating factor.
  255. */
  256. unsigned long zone_reclaimable_pages(struct zone *zone)
  257. {
  258. unsigned long nr;
  259. nr = zone_page_state_snapshot(zone, NR_ZONE_INACTIVE_FILE) +
  260. zone_page_state_snapshot(zone, NR_ZONE_ACTIVE_FILE);
  261. if (get_nr_swap_pages() > 0)
  262. nr += zone_page_state_snapshot(zone, NR_ZONE_INACTIVE_ANON) +
  263. zone_page_state_snapshot(zone, NR_ZONE_ACTIVE_ANON);
  264. return nr;
  265. }
  266. /**
  267. * lruvec_lru_size - Returns the number of pages on the given LRU list.
  268. * @lruvec: lru vector
  269. * @lru: lru to use
  270. * @zone_idx: zones to consider (use MAX_NR_ZONES for the whole LRU list)
  271. */
  272. unsigned long lruvec_lru_size(struct lruvec *lruvec, enum lru_list lru, int zone_idx)
  273. {
  274. unsigned long lru_size;
  275. int zid;
  276. if (!mem_cgroup_disabled())
  277. lru_size = mem_cgroup_get_lru_size(lruvec, lru);
  278. else
  279. lru_size = node_page_state(lruvec_pgdat(lruvec), NR_LRU_BASE + lru);
  280. for (zid = zone_idx + 1; zid < MAX_NR_ZONES; zid++) {
  281. struct zone *zone = &lruvec_pgdat(lruvec)->node_zones[zid];
  282. unsigned long size;
  283. if (!managed_zone(zone))
  284. continue;
  285. if (!mem_cgroup_disabled())
  286. size = mem_cgroup_get_zone_lru_size(lruvec, lru, zid);
  287. else
  288. size = zone_page_state(&lruvec_pgdat(lruvec)->node_zones[zid],
  289. NR_ZONE_LRU_BASE + lru);
  290. lru_size -= min(size, lru_size);
  291. }
  292. return lru_size;
  293. }
  294. /*
  295. * Add a shrinker callback to be called from the vm.
  296. */
  297. int prealloc_shrinker(struct shrinker *shrinker)
  298. {
  299. size_t size = sizeof(*shrinker->nr_deferred);
  300. if (shrinker->flags & SHRINKER_NUMA_AWARE)
  301. size *= nr_node_ids;
  302. shrinker->nr_deferred = kzalloc(size, GFP_KERNEL);
  303. if (!shrinker->nr_deferred)
  304. return -ENOMEM;
  305. /*
  306. * There is a window between prealloc_shrinker()
  307. * and register_shrinker_prepared(). We don't want
  308. * to clear bit of a shrinker in such the state
  309. * in shrink_slab_memcg(), since this will impose
  310. * restrictions on a code registering a shrinker
  311. * (they would have to guarantee, their LRU lists
  312. * are empty till shrinker is completely registered).
  313. * So, we differ the situation, when 1)a shrinker
  314. * is semi-registered (id is assigned, but it has
  315. * not yet linked to shrinker_list) and 2)shrinker
  316. * is not registered (id is not assigned).
  317. */
  318. INIT_LIST_HEAD(&shrinker->list);
  319. if (shrinker->flags & SHRINKER_MEMCG_AWARE) {
  320. if (prealloc_memcg_shrinker(shrinker))
  321. goto free_deferred;
  322. }
  323. return 0;
  324. free_deferred:
  325. kfree(shrinker->nr_deferred);
  326. shrinker->nr_deferred = NULL;
  327. return -ENOMEM;
  328. }
  329. void free_prealloced_shrinker(struct shrinker *shrinker)
  330. {
  331. if (!shrinker->nr_deferred)
  332. return;
  333. if (shrinker->flags & SHRINKER_MEMCG_AWARE)
  334. unregister_memcg_shrinker(shrinker);
  335. kfree(shrinker->nr_deferred);
  336. shrinker->nr_deferred = NULL;
  337. }
  338. void register_shrinker_prepared(struct shrinker *shrinker)
  339. {
  340. down_write(&shrinker_rwsem);
  341. list_add_tail(&shrinker->list, &shrinker_list);
  342. up_write(&shrinker_rwsem);
  343. }
  344. int register_shrinker(struct shrinker *shrinker)
  345. {
  346. int err = prealloc_shrinker(shrinker);
  347. if (err)
  348. return err;
  349. register_shrinker_prepared(shrinker);
  350. return 0;
  351. }
  352. EXPORT_SYMBOL(register_shrinker);
  353. /*
  354. * Remove one
  355. */
  356. void unregister_shrinker(struct shrinker *shrinker)
  357. {
  358. if (!shrinker->nr_deferred)
  359. return;
  360. if (shrinker->flags & SHRINKER_MEMCG_AWARE)
  361. unregister_memcg_shrinker(shrinker);
  362. down_write(&shrinker_rwsem);
  363. list_del(&shrinker->list);
  364. up_write(&shrinker_rwsem);
  365. kfree(shrinker->nr_deferred);
  366. shrinker->nr_deferred = NULL;
  367. }
  368. EXPORT_SYMBOL(unregister_shrinker);
  369. #define SHRINK_BATCH 128
  370. static unsigned long do_shrink_slab(struct shrink_control *shrinkctl,
  371. struct shrinker *shrinker, int priority)
  372. {
  373. unsigned long freed = 0;
  374. unsigned long long delta;
  375. long total_scan;
  376. long freeable;
  377. long nr;
  378. long new_nr;
  379. int nid = shrinkctl->nid;
  380. long batch_size = shrinker->batch ? shrinker->batch
  381. : SHRINK_BATCH;
  382. long scanned = 0, next_deferred;
  383. freeable = shrinker->count_objects(shrinker, shrinkctl);
  384. if (freeable == 0 || freeable == SHRINK_EMPTY)
  385. return freeable;
  386. /*
  387. * copy the current shrinker scan count into a local variable
  388. * and zero it so that other concurrent shrinker invocations
  389. * don't also do this scanning work.
  390. */
  391. nr = atomic_long_xchg(&shrinker->nr_deferred[nid], 0);
  392. total_scan = nr;
  393. delta = freeable >> priority;
  394. delta *= 4;
  395. do_div(delta, shrinker->seeks);
  396. total_scan += delta;
  397. if (total_scan < 0) {
  398. pr_err("shrink_slab: %pF negative objects to delete nr=%ld\n",
  399. shrinker->scan_objects, total_scan);
  400. total_scan = freeable;
  401. next_deferred = nr;
  402. } else
  403. next_deferred = total_scan;
  404. /*
  405. * We need to avoid excessive windup on filesystem shrinkers
  406. * due to large numbers of GFP_NOFS allocations causing the
  407. * shrinkers to return -1 all the time. This results in a large
  408. * nr being built up so when a shrink that can do some work
  409. * comes along it empties the entire cache due to nr >>>
  410. * freeable. This is bad for sustaining a working set in
  411. * memory.
  412. *
  413. * Hence only allow the shrinker to scan the entire cache when
  414. * a large delta change is calculated directly.
  415. */
  416. if (delta < freeable / 4)
  417. total_scan = min(total_scan, freeable / 2);
  418. /*
  419. * Avoid risking looping forever due to too large nr value:
  420. * never try to free more than twice the estimate number of
  421. * freeable entries.
  422. */
  423. if (total_scan > freeable * 2)
  424. total_scan = freeable * 2;
  425. trace_mm_shrink_slab_start(shrinker, shrinkctl, nr,
  426. freeable, delta, total_scan, priority);
  427. /*
  428. * Normally, we should not scan less than batch_size objects in one
  429. * pass to avoid too frequent shrinker calls, but if the slab has less
  430. * than batch_size objects in total and we are really tight on memory,
  431. * we will try to reclaim all available objects, otherwise we can end
  432. * up failing allocations although there are plenty of reclaimable
  433. * objects spread over several slabs with usage less than the
  434. * batch_size.
  435. *
  436. * We detect the "tight on memory" situations by looking at the total
  437. * number of objects we want to scan (total_scan). If it is greater
  438. * than the total number of objects on slab (freeable), we must be
  439. * scanning at high prio and therefore should try to reclaim as much as
  440. * possible.
  441. */
  442. while (total_scan >= batch_size ||
  443. total_scan >= freeable) {
  444. unsigned long ret;
  445. unsigned long nr_to_scan = min(batch_size, total_scan);
  446. shrinkctl->nr_to_scan = nr_to_scan;
  447. shrinkctl->nr_scanned = nr_to_scan;
  448. ret = shrinker->scan_objects(shrinker, shrinkctl);
  449. if (ret == SHRINK_STOP)
  450. break;
  451. freed += ret;
  452. count_vm_events(SLABS_SCANNED, shrinkctl->nr_scanned);
  453. total_scan -= shrinkctl->nr_scanned;
  454. scanned += shrinkctl->nr_scanned;
  455. cond_resched();
  456. }
  457. if (next_deferred >= scanned)
  458. next_deferred -= scanned;
  459. else
  460. next_deferred = 0;
  461. /*
  462. * move the unused scan count back into the shrinker in a
  463. * manner that handles concurrent updates. If we exhausted the
  464. * scan, there is no need to do an update.
  465. */
  466. if (next_deferred > 0)
  467. new_nr = atomic_long_add_return(next_deferred,
  468. &shrinker->nr_deferred[nid]);
  469. else
  470. new_nr = atomic_long_read(&shrinker->nr_deferred[nid]);
  471. trace_mm_shrink_slab_end(shrinker, nid, freed, nr, new_nr, total_scan);
  472. return freed;
  473. }
  474. #ifdef CONFIG_MEMCG_KMEM
  475. static unsigned long shrink_slab_memcg(gfp_t gfp_mask, int nid,
  476. struct mem_cgroup *memcg, int priority)
  477. {
  478. struct memcg_shrinker_map *map;
  479. unsigned long freed = 0;
  480. int ret, i;
  481. if (!memcg_kmem_enabled() || !mem_cgroup_online(memcg))
  482. return 0;
  483. if (!down_read_trylock(&shrinker_rwsem))
  484. return 0;
  485. map = rcu_dereference_protected(memcg->nodeinfo[nid]->shrinker_map,
  486. true);
  487. if (unlikely(!map))
  488. goto unlock;
  489. for_each_set_bit(i, map->map, shrinker_nr_max) {
  490. struct shrink_control sc = {
  491. .gfp_mask = gfp_mask,
  492. .nid = nid,
  493. .memcg = memcg,
  494. };
  495. struct shrinker *shrinker;
  496. shrinker = idr_find(&shrinker_idr, i);
  497. if (unlikely(!shrinker)) {
  498. clear_bit(i, map->map);
  499. continue;
  500. }
  501. /* See comment in prealloc_shrinker() */
  502. if (unlikely(list_empty(&shrinker->list)))
  503. continue;
  504. ret = do_shrink_slab(&sc, shrinker, priority);
  505. if (ret == SHRINK_EMPTY)
  506. ret = 0;
  507. freed += ret;
  508. if (rwsem_is_contended(&shrinker_rwsem)) {
  509. freed = freed ? : 1;
  510. break;
  511. }
  512. }
  513. unlock:
  514. up_read(&shrinker_rwsem);
  515. return freed;
  516. }
  517. #else /* CONFIG_MEMCG_KMEM */
  518. static unsigned long shrink_slab_memcg(gfp_t gfp_mask, int nid,
  519. struct mem_cgroup *memcg, int priority)
  520. {
  521. return 0;
  522. }
  523. #endif /* CONFIG_MEMCG_KMEM */
  524. /**
  525. * shrink_slab - shrink slab caches
  526. * @gfp_mask: allocation context
  527. * @nid: node whose slab caches to target
  528. * @memcg: memory cgroup whose slab caches to target
  529. * @priority: the reclaim priority
  530. *
  531. * Call the shrink functions to age shrinkable caches.
  532. *
  533. * @nid is passed along to shrinkers with SHRINKER_NUMA_AWARE set,
  534. * unaware shrinkers will receive a node id of 0 instead.
  535. *
  536. * @memcg specifies the memory cgroup to target. Unaware shrinkers
  537. * are called only if it is the root cgroup.
  538. *
  539. * @priority is sc->priority, we take the number of objects and >> by priority
  540. * in order to get the scan target.
  541. *
  542. * Returns the number of reclaimed slab objects.
  543. */
  544. static unsigned long shrink_slab(gfp_t gfp_mask, int nid,
  545. struct mem_cgroup *memcg,
  546. int priority)
  547. {
  548. struct shrinker *shrinker;
  549. unsigned long freed = 0;
  550. int ret;
  551. if (!mem_cgroup_is_root(memcg))
  552. return shrink_slab_memcg(gfp_mask, nid, memcg, priority);
  553. if (!down_read_trylock(&shrinker_rwsem))
  554. goto out;
  555. list_for_each_entry(shrinker, &shrinker_list, list) {
  556. struct shrink_control sc = {
  557. .gfp_mask = gfp_mask,
  558. .nid = nid,
  559. .memcg = memcg,
  560. };
  561. if (!(shrinker->flags & SHRINKER_NUMA_AWARE))
  562. sc.nid = 0;
  563. ret = do_shrink_slab(&sc, shrinker, priority);
  564. if (ret == SHRINK_EMPTY)
  565. ret = 0;
  566. freed += ret;
  567. /*
  568. * Bail out if someone want to register a new shrinker to
  569. * prevent the regsitration from being stalled for long periods
  570. * by parallel ongoing shrinking.
  571. */
  572. if (rwsem_is_contended(&shrinker_rwsem)) {
  573. freed = freed ? : 1;
  574. break;
  575. }
  576. }
  577. up_read(&shrinker_rwsem);
  578. out:
  579. cond_resched();
  580. return freed;
  581. }
  582. void drop_slab_node(int nid)
  583. {
  584. unsigned long freed;
  585. do {
  586. struct mem_cgroup *memcg = NULL;
  587. freed = 0;
  588. memcg = mem_cgroup_iter(NULL, NULL, NULL);
  589. do {
  590. freed += shrink_slab(GFP_KERNEL, nid, memcg, 0);
  591. } while ((memcg = mem_cgroup_iter(NULL, memcg, NULL)) != NULL);
  592. } while (freed > 10);
  593. }
  594. void drop_slab(void)
  595. {
  596. int nid;
  597. for_each_online_node(nid)
  598. drop_slab_node(nid);
  599. }
  600. static inline int is_page_cache_freeable(struct page *page)
  601. {
  602. /*
  603. * A freeable page cache page is referenced only by the caller
  604. * that isolated the page, the page cache radix tree and
  605. * optional buffer heads at page->private.
  606. */
  607. int radix_pins = PageTransHuge(page) && PageSwapCache(page) ?
  608. HPAGE_PMD_NR : 1;
  609. return page_count(page) - page_has_private(page) == 1 + radix_pins;
  610. }
  611. static int may_write_to_inode(struct inode *inode, struct scan_control *sc)
  612. {
  613. if (current->flags & PF_SWAPWRITE)
  614. return 1;
  615. if (!inode_write_congested(inode))
  616. return 1;
  617. if (inode_to_bdi(inode) == current->backing_dev_info)
  618. return 1;
  619. return 0;
  620. }
  621. /*
  622. * We detected a synchronous write error writing a page out. Probably
  623. * -ENOSPC. We need to propagate that into the address_space for a subsequent
  624. * fsync(), msync() or close().
  625. *
  626. * The tricky part is that after writepage we cannot touch the mapping: nothing
  627. * prevents it from being freed up. But we have a ref on the page and once
  628. * that page is locked, the mapping is pinned.
  629. *
  630. * We're allowed to run sleeping lock_page() here because we know the caller has
  631. * __GFP_FS.
  632. */
  633. static void handle_write_error(struct address_space *mapping,
  634. struct page *page, int error)
  635. {
  636. lock_page(page);
  637. if (page_mapping(page) == mapping)
  638. mapping_set_error(mapping, error);
  639. unlock_page(page);
  640. }
  641. /* possible outcome of pageout() */
  642. typedef enum {
  643. /* failed to write page out, page is locked */
  644. PAGE_KEEP,
  645. /* move page to the active list, page is locked */
  646. PAGE_ACTIVATE,
  647. /* page has been sent to the disk successfully, page is unlocked */
  648. PAGE_SUCCESS,
  649. /* page is clean and locked */
  650. PAGE_CLEAN,
  651. } pageout_t;
  652. /*
  653. * pageout is called by shrink_page_list() for each dirty page.
  654. * Calls ->writepage().
  655. */
  656. static pageout_t pageout(struct page *page, struct address_space *mapping,
  657. struct scan_control *sc)
  658. {
  659. /*
  660. * If the page is dirty, only perform writeback if that write
  661. * will be non-blocking. To prevent this allocation from being
  662. * stalled by pagecache activity. But note that there may be
  663. * stalls if we need to run get_block(). We could test
  664. * PagePrivate for that.
  665. *
  666. * If this process is currently in __generic_file_write_iter() against
  667. * this page's queue, we can perform writeback even if that
  668. * will block.
  669. *
  670. * If the page is swapcache, write it back even if that would
  671. * block, for some throttling. This happens by accident, because
  672. * swap_backing_dev_info is bust: it doesn't reflect the
  673. * congestion state of the swapdevs. Easy to fix, if needed.
  674. */
  675. if (!is_page_cache_freeable(page))
  676. return PAGE_KEEP;
  677. if (!mapping) {
  678. /*
  679. * Some data journaling orphaned pages can have
  680. * page->mapping == NULL while being dirty with clean buffers.
  681. */
  682. if (page_has_private(page)) {
  683. if (try_to_free_buffers(page)) {
  684. ClearPageDirty(page);
  685. pr_info("%s: orphaned page\n", __func__);
  686. return PAGE_CLEAN;
  687. }
  688. }
  689. return PAGE_KEEP;
  690. }
  691. if (mapping->a_ops->writepage == NULL)
  692. return PAGE_ACTIVATE;
  693. if (!may_write_to_inode(mapping->host, sc))
  694. return PAGE_KEEP;
  695. if (clear_page_dirty_for_io(page)) {
  696. int res;
  697. struct writeback_control wbc = {
  698. .sync_mode = WB_SYNC_NONE,
  699. .nr_to_write = SWAP_CLUSTER_MAX,
  700. .range_start = 0,
  701. .range_end = LLONG_MAX,
  702. .for_reclaim = 1,
  703. };
  704. SetPageReclaim(page);
  705. res = mapping->a_ops->writepage(page, &wbc);
  706. if (res < 0)
  707. handle_write_error(mapping, page, res);
  708. if (res == AOP_WRITEPAGE_ACTIVATE) {
  709. ClearPageReclaim(page);
  710. return PAGE_ACTIVATE;
  711. }
  712. if (!PageWriteback(page)) {
  713. /* synchronous write or broken a_ops? */
  714. ClearPageReclaim(page);
  715. }
  716. trace_mm_vmscan_writepage(page);
  717. inc_node_page_state(page, NR_VMSCAN_WRITE);
  718. return PAGE_SUCCESS;
  719. }
  720. return PAGE_CLEAN;
  721. }
  722. /*
  723. * Same as remove_mapping, but if the page is removed from the mapping, it
  724. * gets returned with a refcount of 0.
  725. */
  726. static int __remove_mapping(struct address_space *mapping, struct page *page,
  727. bool reclaimed)
  728. {
  729. unsigned long flags;
  730. int refcount;
  731. BUG_ON(!PageLocked(page));
  732. BUG_ON(mapping != page_mapping(page));
  733. xa_lock_irqsave(&mapping->i_pages, flags);
  734. /*
  735. * The non racy check for a busy page.
  736. *
  737. * Must be careful with the order of the tests. When someone has
  738. * a ref to the page, it may be possible that they dirty it then
  739. * drop the reference. So if PageDirty is tested before page_count
  740. * here, then the following race may occur:
  741. *
  742. * get_user_pages(&page);
  743. * [user mapping goes away]
  744. * write_to(page);
  745. * !PageDirty(page) [good]
  746. * SetPageDirty(page);
  747. * put_page(page);
  748. * !page_count(page) [good, discard it]
  749. *
  750. * [oops, our write_to data is lost]
  751. *
  752. * Reversing the order of the tests ensures such a situation cannot
  753. * escape unnoticed. The smp_rmb is needed to ensure the page->flags
  754. * load is not satisfied before that of page->_refcount.
  755. *
  756. * Note that if SetPageDirty is always performed via set_page_dirty,
  757. * and thus under the i_pages lock, then this ordering is not required.
  758. */
  759. if (unlikely(PageTransHuge(page)) && PageSwapCache(page))
  760. refcount = 1 + HPAGE_PMD_NR;
  761. else
  762. refcount = 2;
  763. if (!page_ref_freeze(page, refcount))
  764. goto cannot_free;
  765. /* note: atomic_cmpxchg in page_freeze_refs provides the smp_rmb */
  766. if (unlikely(PageDirty(page))) {
  767. page_ref_unfreeze(page, refcount);
  768. goto cannot_free;
  769. }
  770. if (PageSwapCache(page)) {
  771. swp_entry_t swap = { .val = page_private(page) };
  772. mem_cgroup_swapout(page, swap);
  773. __delete_from_swap_cache(page);
  774. xa_unlock_irqrestore(&mapping->i_pages, flags);
  775. put_swap_page(page, swap);
  776. } else {
  777. void (*freepage)(struct page *);
  778. void *shadow = NULL;
  779. freepage = mapping->a_ops->freepage;
  780. /*
  781. * Remember a shadow entry for reclaimed file cache in
  782. * order to detect refaults, thus thrashing, later on.
  783. *
  784. * But don't store shadows in an address space that is
  785. * already exiting. This is not just an optizimation,
  786. * inode reclaim needs to empty out the radix tree or
  787. * the nodes are lost. Don't plant shadows behind its
  788. * back.
  789. *
  790. * We also don't store shadows for DAX mappings because the
  791. * only page cache pages found in these are zero pages
  792. * covering holes, and because we don't want to mix DAX
  793. * exceptional entries and shadow exceptional entries in the
  794. * same address_space.
  795. */
  796. if (reclaimed && page_is_file_cache(page) &&
  797. !mapping_exiting(mapping) && !dax_mapping(mapping))
  798. shadow = workingset_eviction(mapping, page);
  799. __delete_from_page_cache(page, shadow);
  800. xa_unlock_irqrestore(&mapping->i_pages, flags);
  801. if (freepage != NULL)
  802. freepage(page);
  803. }
  804. return 1;
  805. cannot_free:
  806. xa_unlock_irqrestore(&mapping->i_pages, flags);
  807. return 0;
  808. }
  809. /*
  810. * Attempt to detach a locked page from its ->mapping. If it is dirty or if
  811. * someone else has a ref on the page, abort and return 0. If it was
  812. * successfully detached, return 1. Assumes the caller has a single ref on
  813. * this page.
  814. */
  815. int remove_mapping(struct address_space *mapping, struct page *page)
  816. {
  817. if (__remove_mapping(mapping, page, false)) {
  818. /*
  819. * Unfreezing the refcount with 1 rather than 2 effectively
  820. * drops the pagecache ref for us without requiring another
  821. * atomic operation.
  822. */
  823. page_ref_unfreeze(page, 1);
  824. return 1;
  825. }
  826. return 0;
  827. }
  828. /**
  829. * putback_lru_page - put previously isolated page onto appropriate LRU list
  830. * @page: page to be put back to appropriate lru list
  831. *
  832. * Add previously isolated @page to appropriate LRU list.
  833. * Page may still be unevictable for other reasons.
  834. *
  835. * lru_lock must not be held, interrupts must be enabled.
  836. */
  837. void putback_lru_page(struct page *page)
  838. {
  839. lru_cache_add(page);
  840. put_page(page); /* drop ref from isolate */
  841. }
  842. enum page_references {
  843. PAGEREF_RECLAIM,
  844. PAGEREF_RECLAIM_CLEAN,
  845. PAGEREF_KEEP,
  846. PAGEREF_ACTIVATE,
  847. };
  848. static enum page_references page_check_references(struct page *page,
  849. struct scan_control *sc)
  850. {
  851. int referenced_ptes, referenced_page;
  852. unsigned long vm_flags;
  853. referenced_ptes = page_referenced(page, 1, sc->target_mem_cgroup,
  854. &vm_flags);
  855. referenced_page = TestClearPageReferenced(page);
  856. /*
  857. * Mlock lost the isolation race with us. Let try_to_unmap()
  858. * move the page to the unevictable list.
  859. */
  860. if (vm_flags & VM_LOCKED)
  861. return PAGEREF_RECLAIM;
  862. if (referenced_ptes) {
  863. if (PageSwapBacked(page))
  864. return PAGEREF_ACTIVATE;
  865. /*
  866. * All mapped pages start out with page table
  867. * references from the instantiating fault, so we need
  868. * to look twice if a mapped file page is used more
  869. * than once.
  870. *
  871. * Mark it and spare it for another trip around the
  872. * inactive list. Another page table reference will
  873. * lead to its activation.
  874. *
  875. * Note: the mark is set for activated pages as well
  876. * so that recently deactivated but used pages are
  877. * quickly recovered.
  878. */
  879. SetPageReferenced(page);
  880. if (referenced_page || referenced_ptes > 1)
  881. return PAGEREF_ACTIVATE;
  882. /*
  883. * Activate file-backed executable pages after first usage.
  884. */
  885. if (vm_flags & VM_EXEC)
  886. return PAGEREF_ACTIVATE;
  887. return PAGEREF_KEEP;
  888. }
  889. /* Reclaim if clean, defer dirty pages to writeback */
  890. if (referenced_page && !PageSwapBacked(page))
  891. return PAGEREF_RECLAIM_CLEAN;
  892. return PAGEREF_RECLAIM;
  893. }
  894. /* Check if a page is dirty or under writeback */
  895. static void page_check_dirty_writeback(struct page *page,
  896. bool *dirty, bool *writeback)
  897. {
  898. struct address_space *mapping;
  899. /*
  900. * Anonymous pages are not handled by flushers and must be written
  901. * from reclaim context. Do not stall reclaim based on them
  902. */
  903. if (!page_is_file_cache(page) ||
  904. (PageAnon(page) && !PageSwapBacked(page))) {
  905. *dirty = false;
  906. *writeback = false;
  907. return;
  908. }
  909. /* By default assume that the page flags are accurate */
  910. *dirty = PageDirty(page);
  911. *writeback = PageWriteback(page);
  912. /* Verify dirty/writeback state if the filesystem supports it */
  913. if (!page_has_private(page))
  914. return;
  915. mapping = page_mapping(page);
  916. if (mapping && mapping->a_ops->is_dirty_writeback)
  917. mapping->a_ops->is_dirty_writeback(page, dirty, writeback);
  918. }
  919. /*
  920. * shrink_page_list() returns the number of reclaimed pages
  921. */
  922. static unsigned long shrink_page_list(struct list_head *page_list,
  923. struct pglist_data *pgdat,
  924. struct scan_control *sc,
  925. enum ttu_flags ttu_flags,
  926. struct reclaim_stat *stat,
  927. bool force_reclaim)
  928. {
  929. LIST_HEAD(ret_pages);
  930. LIST_HEAD(free_pages);
  931. int pgactivate = 0;
  932. unsigned nr_unqueued_dirty = 0;
  933. unsigned nr_dirty = 0;
  934. unsigned nr_congested = 0;
  935. unsigned nr_reclaimed = 0;
  936. unsigned nr_writeback = 0;
  937. unsigned nr_immediate = 0;
  938. unsigned nr_ref_keep = 0;
  939. unsigned nr_unmap_fail = 0;
  940. cond_resched();
  941. while (!list_empty(page_list)) {
  942. struct address_space *mapping;
  943. struct page *page;
  944. int may_enter_fs;
  945. enum page_references references = PAGEREF_RECLAIM_CLEAN;
  946. bool dirty, writeback;
  947. cond_resched();
  948. page = lru_to_page(page_list);
  949. list_del(&page->lru);
  950. if (!trylock_page(page))
  951. goto keep;
  952. VM_BUG_ON_PAGE(PageActive(page), page);
  953. sc->nr_scanned++;
  954. if (unlikely(!page_evictable(page)))
  955. goto activate_locked;
  956. if (!sc->may_unmap && page_mapped(page))
  957. goto keep_locked;
  958. /* Double the slab pressure for mapped and swapcache pages */
  959. if ((page_mapped(page) || PageSwapCache(page)) &&
  960. !(PageAnon(page) && !PageSwapBacked(page)))
  961. sc->nr_scanned++;
  962. may_enter_fs = (sc->gfp_mask & __GFP_FS) ||
  963. (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO));
  964. /*
  965. * The number of dirty pages determines if a node is marked
  966. * reclaim_congested which affects wait_iff_congested. kswapd
  967. * will stall and start writing pages if the tail of the LRU
  968. * is all dirty unqueued pages.
  969. */
  970. page_check_dirty_writeback(page, &dirty, &writeback);
  971. if (dirty || writeback)
  972. nr_dirty++;
  973. if (dirty && !writeback)
  974. nr_unqueued_dirty++;
  975. /*
  976. * Treat this page as congested if the underlying BDI is or if
  977. * pages are cycling through the LRU so quickly that the
  978. * pages marked for immediate reclaim are making it to the
  979. * end of the LRU a second time.
  980. */
  981. mapping = page_mapping(page);
  982. if (((dirty || writeback) && mapping &&
  983. inode_write_congested(mapping->host)) ||
  984. (writeback && PageReclaim(page)))
  985. nr_congested++;
  986. /*
  987. * If a page at the tail of the LRU is under writeback, there
  988. * are three cases to consider.
  989. *
  990. * 1) If reclaim is encountering an excessive number of pages
  991. * under writeback and this page is both under writeback and
  992. * PageReclaim then it indicates that pages are being queued
  993. * for IO but are being recycled through the LRU before the
  994. * IO can complete. Waiting on the page itself risks an
  995. * indefinite stall if it is impossible to writeback the
  996. * page due to IO error or disconnected storage so instead
  997. * note that the LRU is being scanned too quickly and the
  998. * caller can stall after page list has been processed.
  999. *
  1000. * 2) Global or new memcg reclaim encounters a page that is
  1001. * not marked for immediate reclaim, or the caller does not
  1002. * have __GFP_FS (or __GFP_IO if it's simply going to swap,
  1003. * not to fs). In this case mark the page for immediate
  1004. * reclaim and continue scanning.
  1005. *
  1006. * Require may_enter_fs because we would wait on fs, which
  1007. * may not have submitted IO yet. And the loop driver might
  1008. * enter reclaim, and deadlock if it waits on a page for
  1009. * which it is needed to do the write (loop masks off
  1010. * __GFP_IO|__GFP_FS for this reason); but more thought
  1011. * would probably show more reasons.
  1012. *
  1013. * 3) Legacy memcg encounters a page that is already marked
  1014. * PageReclaim. memcg does not have any dirty pages
  1015. * throttling so we could easily OOM just because too many
  1016. * pages are in writeback and there is nothing else to
  1017. * reclaim. Wait for the writeback to complete.
  1018. *
  1019. * In cases 1) and 2) we activate the pages to get them out of
  1020. * the way while we continue scanning for clean pages on the
  1021. * inactive list and refilling from the active list. The
  1022. * observation here is that waiting for disk writes is more
  1023. * expensive than potentially causing reloads down the line.
  1024. * Since they're marked for immediate reclaim, they won't put
  1025. * memory pressure on the cache working set any longer than it
  1026. * takes to write them to disk.
  1027. */
  1028. if (PageWriteback(page)) {
  1029. /* Case 1 above */
  1030. if (current_is_kswapd() &&
  1031. PageReclaim(page) &&
  1032. test_bit(PGDAT_WRITEBACK, &pgdat->flags)) {
  1033. nr_immediate++;
  1034. goto activate_locked;
  1035. /* Case 2 above */
  1036. } else if (sane_reclaim(sc) ||
  1037. !PageReclaim(page) || !may_enter_fs) {
  1038. /*
  1039. * This is slightly racy - end_page_writeback()
  1040. * might have just cleared PageReclaim, then
  1041. * setting PageReclaim here end up interpreted
  1042. * as PageReadahead - but that does not matter
  1043. * enough to care. What we do want is for this
  1044. * page to have PageReclaim set next time memcg
  1045. * reclaim reaches the tests above, so it will
  1046. * then wait_on_page_writeback() to avoid OOM;
  1047. * and it's also appropriate in global reclaim.
  1048. */
  1049. SetPageReclaim(page);
  1050. nr_writeback++;
  1051. goto activate_locked;
  1052. /* Case 3 above */
  1053. } else {
  1054. unlock_page(page);
  1055. wait_on_page_writeback(page);
  1056. /* then go back and try same page again */
  1057. list_add_tail(&page->lru, page_list);
  1058. continue;
  1059. }
  1060. }
  1061. if (!force_reclaim)
  1062. references = page_check_references(page, sc);
  1063. switch (references) {
  1064. case PAGEREF_ACTIVATE:
  1065. goto activate_locked;
  1066. case PAGEREF_KEEP:
  1067. nr_ref_keep++;
  1068. goto keep_locked;
  1069. case PAGEREF_RECLAIM:
  1070. case PAGEREF_RECLAIM_CLEAN:
  1071. ; /* try to reclaim the page below */
  1072. }
  1073. /*
  1074. * Anonymous process memory has backing store?
  1075. * Try to allocate it some swap space here.
  1076. * Lazyfree page could be freed directly
  1077. */
  1078. if (PageAnon(page) && PageSwapBacked(page)) {
  1079. if (!PageSwapCache(page)) {
  1080. if (!(sc->gfp_mask & __GFP_IO))
  1081. goto keep_locked;
  1082. if (PageTransHuge(page)) {
  1083. /* cannot split THP, skip it */
  1084. if (!can_split_huge_page(page, NULL))
  1085. goto activate_locked;
  1086. /*
  1087. * Split pages without a PMD map right
  1088. * away. Chances are some or all of the
  1089. * tail pages can be freed without IO.
  1090. */
  1091. if (!compound_mapcount(page) &&
  1092. split_huge_page_to_list(page,
  1093. page_list))
  1094. goto activate_locked;
  1095. }
  1096. if (!add_to_swap(page)) {
  1097. if (!PageTransHuge(page))
  1098. goto activate_locked;
  1099. /* Fallback to swap normal pages */
  1100. if (split_huge_page_to_list(page,
  1101. page_list))
  1102. goto activate_locked;
  1103. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  1104. count_vm_event(THP_SWPOUT_FALLBACK);
  1105. #endif
  1106. if (!add_to_swap(page))
  1107. goto activate_locked;
  1108. }
  1109. may_enter_fs = 1;
  1110. /* Adding to swap updated mapping */
  1111. mapping = page_mapping(page);
  1112. }
  1113. } else if (unlikely(PageTransHuge(page))) {
  1114. /* Split file THP */
  1115. if (split_huge_page_to_list(page, page_list))
  1116. goto keep_locked;
  1117. }
  1118. /*
  1119. * The page is mapped into the page tables of one or more
  1120. * processes. Try to unmap it here.
  1121. */
  1122. if (page_mapped(page)) {
  1123. enum ttu_flags flags = ttu_flags | TTU_BATCH_FLUSH;
  1124. if (unlikely(PageTransHuge(page)))
  1125. flags |= TTU_SPLIT_HUGE_PMD;
  1126. if (!try_to_unmap(page, flags)) {
  1127. nr_unmap_fail++;
  1128. goto activate_locked;
  1129. }
  1130. }
  1131. if (PageDirty(page)) {
  1132. /*
  1133. * Only kswapd can writeback filesystem pages
  1134. * to avoid risk of stack overflow. But avoid
  1135. * injecting inefficient single-page IO into
  1136. * flusher writeback as much as possible: only
  1137. * write pages when we've encountered many
  1138. * dirty pages, and when we've already scanned
  1139. * the rest of the LRU for clean pages and see
  1140. * the same dirty pages again (PageReclaim).
  1141. */
  1142. if (page_is_file_cache(page) &&
  1143. (!current_is_kswapd() || !PageReclaim(page) ||
  1144. !test_bit(PGDAT_DIRTY, &pgdat->flags))) {
  1145. /*
  1146. * Immediately reclaim when written back.
  1147. * Similar in principal to deactivate_page()
  1148. * except we already have the page isolated
  1149. * and know it's dirty
  1150. */
  1151. inc_node_page_state(page, NR_VMSCAN_IMMEDIATE);
  1152. SetPageReclaim(page);
  1153. goto activate_locked;
  1154. }
  1155. if (references == PAGEREF_RECLAIM_CLEAN)
  1156. goto keep_locked;
  1157. if (!may_enter_fs)
  1158. goto keep_locked;
  1159. if (!sc->may_writepage)
  1160. goto keep_locked;
  1161. /*
  1162. * Page is dirty. Flush the TLB if a writable entry
  1163. * potentially exists to avoid CPU writes after IO
  1164. * starts and then write it out here.
  1165. */
  1166. try_to_unmap_flush_dirty();
  1167. switch (pageout(page, mapping, sc)) {
  1168. case PAGE_KEEP:
  1169. goto keep_locked;
  1170. case PAGE_ACTIVATE:
  1171. goto activate_locked;
  1172. case PAGE_SUCCESS:
  1173. if (PageWriteback(page))
  1174. goto keep;
  1175. if (PageDirty(page))
  1176. goto keep;
  1177. /*
  1178. * A synchronous write - probably a ramdisk. Go
  1179. * ahead and try to reclaim the page.
  1180. */
  1181. if (!trylock_page(page))
  1182. goto keep;
  1183. if (PageDirty(page) || PageWriteback(page))
  1184. goto keep_locked;
  1185. mapping = page_mapping(page);
  1186. case PAGE_CLEAN:
  1187. ; /* try to free the page below */
  1188. }
  1189. }
  1190. /*
  1191. * If the page has buffers, try to free the buffer mappings
  1192. * associated with this page. If we succeed we try to free
  1193. * the page as well.
  1194. *
  1195. * We do this even if the page is PageDirty().
  1196. * try_to_release_page() does not perform I/O, but it is
  1197. * possible for a page to have PageDirty set, but it is actually
  1198. * clean (all its buffers are clean). This happens if the
  1199. * buffers were written out directly, with submit_bh(). ext3
  1200. * will do this, as well as the blockdev mapping.
  1201. * try_to_release_page() will discover that cleanness and will
  1202. * drop the buffers and mark the page clean - it can be freed.
  1203. *
  1204. * Rarely, pages can have buffers and no ->mapping. These are
  1205. * the pages which were not successfully invalidated in
  1206. * truncate_complete_page(). We try to drop those buffers here
  1207. * and if that worked, and the page is no longer mapped into
  1208. * process address space (page_count == 1) it can be freed.
  1209. * Otherwise, leave the page on the LRU so it is swappable.
  1210. */
  1211. if (page_has_private(page)) {
  1212. if (!try_to_release_page(page, sc->gfp_mask))
  1213. goto activate_locked;
  1214. if (!mapping && page_count(page) == 1) {
  1215. unlock_page(page);
  1216. if (put_page_testzero(page))
  1217. goto free_it;
  1218. else {
  1219. /*
  1220. * rare race with speculative reference.
  1221. * the speculative reference will free
  1222. * this page shortly, so we may
  1223. * increment nr_reclaimed here (and
  1224. * leave it off the LRU).
  1225. */
  1226. nr_reclaimed++;
  1227. continue;
  1228. }
  1229. }
  1230. }
  1231. if (PageAnon(page) && !PageSwapBacked(page)) {
  1232. /* follow __remove_mapping for reference */
  1233. if (!page_ref_freeze(page, 1))
  1234. goto keep_locked;
  1235. if (PageDirty(page)) {
  1236. page_ref_unfreeze(page, 1);
  1237. goto keep_locked;
  1238. }
  1239. count_vm_event(PGLAZYFREED);
  1240. count_memcg_page_event(page, PGLAZYFREED);
  1241. } else if (!mapping || !__remove_mapping(mapping, page, true))
  1242. goto keep_locked;
  1243. /*
  1244. * At this point, we have no other references and there is
  1245. * no way to pick any more up (removed from LRU, removed
  1246. * from pagecache). Can use non-atomic bitops now (and
  1247. * we obviously don't have to worry about waking up a process
  1248. * waiting on the page lock, because there are no references.
  1249. */
  1250. __ClearPageLocked(page);
  1251. free_it:
  1252. nr_reclaimed++;
  1253. /*
  1254. * Is there need to periodically free_page_list? It would
  1255. * appear not as the counts should be low
  1256. */
  1257. if (unlikely(PageTransHuge(page))) {
  1258. mem_cgroup_uncharge(page);
  1259. (*get_compound_page_dtor(page))(page);
  1260. } else
  1261. list_add(&page->lru, &free_pages);
  1262. continue;
  1263. activate_locked:
  1264. /* Not a candidate for swapping, so reclaim swap space. */
  1265. if (PageSwapCache(page) && (mem_cgroup_swap_full(page) ||
  1266. PageMlocked(page)))
  1267. try_to_free_swap(page);
  1268. VM_BUG_ON_PAGE(PageActive(page), page);
  1269. if (!PageMlocked(page)) {
  1270. SetPageActive(page);
  1271. pgactivate++;
  1272. count_memcg_page_event(page, PGACTIVATE);
  1273. }
  1274. keep_locked:
  1275. unlock_page(page);
  1276. keep:
  1277. list_add(&page->lru, &ret_pages);
  1278. VM_BUG_ON_PAGE(PageLRU(page) || PageUnevictable(page), page);
  1279. }
  1280. mem_cgroup_uncharge_list(&free_pages);
  1281. try_to_unmap_flush();
  1282. free_unref_page_list(&free_pages);
  1283. list_splice(&ret_pages, page_list);
  1284. count_vm_events(PGACTIVATE, pgactivate);
  1285. if (stat) {
  1286. stat->nr_dirty = nr_dirty;
  1287. stat->nr_congested = nr_congested;
  1288. stat->nr_unqueued_dirty = nr_unqueued_dirty;
  1289. stat->nr_writeback = nr_writeback;
  1290. stat->nr_immediate = nr_immediate;
  1291. stat->nr_activate = pgactivate;
  1292. stat->nr_ref_keep = nr_ref_keep;
  1293. stat->nr_unmap_fail = nr_unmap_fail;
  1294. }
  1295. return nr_reclaimed;
  1296. }
  1297. unsigned long reclaim_clean_pages_from_list(struct zone *zone,
  1298. struct list_head *page_list)
  1299. {
  1300. struct scan_control sc = {
  1301. .gfp_mask = GFP_KERNEL,
  1302. .priority = DEF_PRIORITY,
  1303. .may_unmap = 1,
  1304. };
  1305. unsigned long ret;
  1306. struct page *page, *next;
  1307. LIST_HEAD(clean_pages);
  1308. list_for_each_entry_safe(page, next, page_list, lru) {
  1309. if (page_is_file_cache(page) && !PageDirty(page) &&
  1310. !__PageMovable(page)) {
  1311. ClearPageActive(page);
  1312. list_move(&page->lru, &clean_pages);
  1313. }
  1314. }
  1315. ret = shrink_page_list(&clean_pages, zone->zone_pgdat, &sc,
  1316. TTU_IGNORE_ACCESS, NULL, true);
  1317. list_splice(&clean_pages, page_list);
  1318. mod_node_page_state(zone->zone_pgdat, NR_ISOLATED_FILE, -ret);
  1319. return ret;
  1320. }
  1321. /*
  1322. * Attempt to remove the specified page from its LRU. Only take this page
  1323. * if it is of the appropriate PageActive status. Pages which are being
  1324. * freed elsewhere are also ignored.
  1325. *
  1326. * page: page to consider
  1327. * mode: one of the LRU isolation modes defined above
  1328. *
  1329. * returns 0 on success, -ve errno on failure.
  1330. */
  1331. int __isolate_lru_page(struct page *page, isolate_mode_t mode)
  1332. {
  1333. int ret = -EINVAL;
  1334. /* Only take pages on the LRU. */
  1335. if (!PageLRU(page))
  1336. return ret;
  1337. /* Compaction should not handle unevictable pages but CMA can do so */
  1338. if (PageUnevictable(page) && !(mode & ISOLATE_UNEVICTABLE))
  1339. return ret;
  1340. ret = -EBUSY;
  1341. /*
  1342. * To minimise LRU disruption, the caller can indicate that it only
  1343. * wants to isolate pages it will be able to operate on without
  1344. * blocking - clean pages for the most part.
  1345. *
  1346. * ISOLATE_ASYNC_MIGRATE is used to indicate that it only wants to pages
  1347. * that it is possible to migrate without blocking
  1348. */
  1349. if (mode & ISOLATE_ASYNC_MIGRATE) {
  1350. /* All the caller can do on PageWriteback is block */
  1351. if (PageWriteback(page))
  1352. return ret;
  1353. if (PageDirty(page)) {
  1354. struct address_space *mapping;
  1355. bool migrate_dirty;
  1356. /*
  1357. * Only pages without mappings or that have a
  1358. * ->migratepage callback are possible to migrate
  1359. * without blocking. However, we can be racing with
  1360. * truncation so it's necessary to lock the page
  1361. * to stabilise the mapping as truncation holds
  1362. * the page lock until after the page is removed
  1363. * from the page cache.
  1364. */
  1365. if (!trylock_page(page))
  1366. return ret;
  1367. mapping = page_mapping(page);
  1368. migrate_dirty = !mapping || mapping->a_ops->migratepage;
  1369. unlock_page(page);
  1370. if (!migrate_dirty)
  1371. return ret;
  1372. }
  1373. }
  1374. if ((mode & ISOLATE_UNMAPPED) && page_mapped(page))
  1375. return ret;
  1376. if (likely(get_page_unless_zero(page))) {
  1377. /*
  1378. * Be careful not to clear PageLRU until after we're
  1379. * sure the page is not being freed elsewhere -- the
  1380. * page release code relies on it.
  1381. */
  1382. ClearPageLRU(page);
  1383. ret = 0;
  1384. }
  1385. return ret;
  1386. }
  1387. /*
  1388. * Update LRU sizes after isolating pages. The LRU size updates must
  1389. * be complete before mem_cgroup_update_lru_size due to a santity check.
  1390. */
  1391. static __always_inline void update_lru_sizes(struct lruvec *lruvec,
  1392. enum lru_list lru, unsigned long *nr_zone_taken)
  1393. {
  1394. int zid;
  1395. for (zid = 0; zid < MAX_NR_ZONES; zid++) {
  1396. if (!nr_zone_taken[zid])
  1397. continue;
  1398. __update_lru_size(lruvec, lru, zid, -nr_zone_taken[zid]);
  1399. #ifdef CONFIG_MEMCG
  1400. mem_cgroup_update_lru_size(lruvec, lru, zid, -nr_zone_taken[zid]);
  1401. #endif
  1402. }
  1403. }
  1404. /*
  1405. * zone_lru_lock is heavily contended. Some of the functions that
  1406. * shrink the lists perform better by taking out a batch of pages
  1407. * and working on them outside the LRU lock.
  1408. *
  1409. * For pagecache intensive workloads, this function is the hottest
  1410. * spot in the kernel (apart from copy_*_user functions).
  1411. *
  1412. * Appropriate locks must be held before calling this function.
  1413. *
  1414. * @nr_to_scan: The number of eligible pages to look through on the list.
  1415. * @lruvec: The LRU vector to pull pages from.
  1416. * @dst: The temp list to put pages on to.
  1417. * @nr_scanned: The number of pages that were scanned.
  1418. * @sc: The scan_control struct for this reclaim session
  1419. * @mode: One of the LRU isolation modes
  1420. * @lru: LRU list id for isolating
  1421. *
  1422. * returns how many pages were moved onto *@dst.
  1423. */
  1424. static unsigned long isolate_lru_pages(unsigned long nr_to_scan,
  1425. struct lruvec *lruvec, struct list_head *dst,
  1426. unsigned long *nr_scanned, struct scan_control *sc,
  1427. isolate_mode_t mode, enum lru_list lru)
  1428. {
  1429. struct list_head *src = &lruvec->lists[lru];
  1430. unsigned long nr_taken = 0;
  1431. unsigned long nr_zone_taken[MAX_NR_ZONES] = { 0 };
  1432. unsigned long nr_skipped[MAX_NR_ZONES] = { 0, };
  1433. unsigned long skipped = 0;
  1434. unsigned long scan, total_scan, nr_pages;
  1435. LIST_HEAD(pages_skipped);
  1436. scan = 0;
  1437. for (total_scan = 0;
  1438. scan < nr_to_scan && nr_taken < nr_to_scan && !list_empty(src);
  1439. total_scan++) {
  1440. struct page *page;
  1441. page = lru_to_page(src);
  1442. prefetchw_prev_lru_page(page, src, flags);
  1443. VM_BUG_ON_PAGE(!PageLRU(page), page);
  1444. if (page_zonenum(page) > sc->reclaim_idx) {
  1445. list_move(&page->lru, &pages_skipped);
  1446. nr_skipped[page_zonenum(page)]++;
  1447. continue;
  1448. }
  1449. /*
  1450. * Do not count skipped pages because that makes the function
  1451. * return with no isolated pages if the LRU mostly contains
  1452. * ineligible pages. This causes the VM to not reclaim any
  1453. * pages, triggering a premature OOM.
  1454. */
  1455. scan++;
  1456. switch (__isolate_lru_page(page, mode)) {
  1457. case 0:
  1458. nr_pages = hpage_nr_pages(page);
  1459. nr_taken += nr_pages;
  1460. nr_zone_taken[page_zonenum(page)] += nr_pages;
  1461. list_move(&page->lru, dst);
  1462. break;
  1463. case -EBUSY:
  1464. /* else it is being freed elsewhere */
  1465. list_move(&page->lru, src);
  1466. continue;
  1467. default:
  1468. BUG();
  1469. }
  1470. }
  1471. /*
  1472. * Splice any skipped pages to the start of the LRU list. Note that
  1473. * this disrupts the LRU order when reclaiming for lower zones but
  1474. * we cannot splice to the tail. If we did then the SWAP_CLUSTER_MAX
  1475. * scanning would soon rescan the same pages to skip and put the
  1476. * system at risk of premature OOM.
  1477. */
  1478. if (!list_empty(&pages_skipped)) {
  1479. int zid;
  1480. list_splice(&pages_skipped, src);
  1481. for (zid = 0; zid < MAX_NR_ZONES; zid++) {
  1482. if (!nr_skipped[zid])
  1483. continue;
  1484. __count_zid_vm_events(PGSCAN_SKIP, zid, nr_skipped[zid]);
  1485. skipped += nr_skipped[zid];
  1486. }
  1487. }
  1488. *nr_scanned = total_scan;
  1489. trace_mm_vmscan_lru_isolate(sc->reclaim_idx, sc->order, nr_to_scan,
  1490. total_scan, skipped, nr_taken, mode, lru);
  1491. update_lru_sizes(lruvec, lru, nr_zone_taken);
  1492. return nr_taken;
  1493. }
  1494. /**
  1495. * isolate_lru_page - tries to isolate a page from its LRU list
  1496. * @page: page to isolate from its LRU list
  1497. *
  1498. * Isolates a @page from an LRU list, clears PageLRU and adjusts the
  1499. * vmstat statistic corresponding to whatever LRU list the page was on.
  1500. *
  1501. * Returns 0 if the page was removed from an LRU list.
  1502. * Returns -EBUSY if the page was not on an LRU list.
  1503. *
  1504. * The returned page will have PageLRU() cleared. If it was found on
  1505. * the active list, it will have PageActive set. If it was found on
  1506. * the unevictable list, it will have the PageUnevictable bit set. That flag
  1507. * may need to be cleared by the caller before letting the page go.
  1508. *
  1509. * The vmstat statistic corresponding to the list on which the page was
  1510. * found will be decremented.
  1511. *
  1512. * Restrictions:
  1513. *
  1514. * (1) Must be called with an elevated refcount on the page. This is a
  1515. * fundamentnal difference from isolate_lru_pages (which is called
  1516. * without a stable reference).
  1517. * (2) the lru_lock must not be held.
  1518. * (3) interrupts must be enabled.
  1519. */
  1520. int isolate_lru_page(struct page *page)
  1521. {
  1522. int ret = -EBUSY;
  1523. VM_BUG_ON_PAGE(!page_count(page), page);
  1524. WARN_RATELIMIT(PageTail(page), "trying to isolate tail page");
  1525. if (PageLRU(page)) {
  1526. struct zone *zone = page_zone(page);
  1527. struct lruvec *lruvec;
  1528. spin_lock_irq(zone_lru_lock(zone));
  1529. lruvec = mem_cgroup_page_lruvec(page, zone->zone_pgdat);
  1530. if (PageLRU(page)) {
  1531. int lru = page_lru(page);
  1532. get_page(page);
  1533. ClearPageLRU(page);
  1534. del_page_from_lru_list(page, lruvec, lru);
  1535. ret = 0;
  1536. }
  1537. spin_unlock_irq(zone_lru_lock(zone));
  1538. }
  1539. return ret;
  1540. }
  1541. /*
  1542. * A direct reclaimer may isolate SWAP_CLUSTER_MAX pages from the LRU list and
  1543. * then get resheduled. When there are massive number of tasks doing page
  1544. * allocation, such sleeping direct reclaimers may keep piling up on each CPU,
  1545. * the LRU list will go small and be scanned faster than necessary, leading to
  1546. * unnecessary swapping, thrashing and OOM.
  1547. */
  1548. static int too_many_isolated(struct pglist_data *pgdat, int file,
  1549. struct scan_control *sc)
  1550. {
  1551. unsigned long inactive, isolated;
  1552. if (current_is_kswapd())
  1553. return 0;
  1554. if (!sane_reclaim(sc))
  1555. return 0;
  1556. if (file) {
  1557. inactive = node_page_state(pgdat, NR_INACTIVE_FILE);
  1558. isolated = node_page_state(pgdat, NR_ISOLATED_FILE);
  1559. } else {
  1560. inactive = node_page_state(pgdat, NR_INACTIVE_ANON);
  1561. isolated = node_page_state(pgdat, NR_ISOLATED_ANON);
  1562. }
  1563. /*
  1564. * GFP_NOIO/GFP_NOFS callers are allowed to isolate more pages, so they
  1565. * won't get blocked by normal direct-reclaimers, forming a circular
  1566. * deadlock.
  1567. */
  1568. if ((sc->gfp_mask & (__GFP_IO | __GFP_FS)) == (__GFP_IO | __GFP_FS))
  1569. inactive >>= 3;
  1570. return isolated > inactive;
  1571. }
  1572. static noinline_for_stack void
  1573. putback_inactive_pages(struct lruvec *lruvec, struct list_head *page_list)
  1574. {
  1575. struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat;
  1576. struct pglist_data *pgdat = lruvec_pgdat(lruvec);
  1577. LIST_HEAD(pages_to_free);
  1578. /*
  1579. * Put back any unfreeable pages.
  1580. */
  1581. while (!list_empty(page_list)) {
  1582. struct page *page = lru_to_page(page_list);
  1583. int lru;
  1584. VM_BUG_ON_PAGE(PageLRU(page), page);
  1585. list_del(&page->lru);
  1586. if (unlikely(!page_evictable(page))) {
  1587. spin_unlock_irq(&pgdat->lru_lock);
  1588. putback_lru_page(page);
  1589. spin_lock_irq(&pgdat->lru_lock);
  1590. continue;
  1591. }
  1592. lruvec = mem_cgroup_page_lruvec(page, pgdat);
  1593. SetPageLRU(page);
  1594. lru = page_lru(page);
  1595. add_page_to_lru_list(page, lruvec, lru);
  1596. if (is_active_lru(lru)) {
  1597. int file = is_file_lru(lru);
  1598. int numpages = hpage_nr_pages(page);
  1599. reclaim_stat->recent_rotated[file] += numpages;
  1600. }
  1601. if (put_page_testzero(page)) {
  1602. __ClearPageLRU(page);
  1603. __ClearPageActive(page);
  1604. del_page_from_lru_list(page, lruvec, lru);
  1605. if (unlikely(PageCompound(page))) {
  1606. spin_unlock_irq(&pgdat->lru_lock);
  1607. mem_cgroup_uncharge(page);
  1608. (*get_compound_page_dtor(page))(page);
  1609. spin_lock_irq(&pgdat->lru_lock);
  1610. } else
  1611. list_add(&page->lru, &pages_to_free);
  1612. }
  1613. }
  1614. /*
  1615. * To save our caller's stack, now use input list for pages to free.
  1616. */
  1617. list_splice(&pages_to_free, page_list);
  1618. }
  1619. /*
  1620. * If a kernel thread (such as nfsd for loop-back mounts) services
  1621. * a backing device by writing to the page cache it sets PF_LESS_THROTTLE.
  1622. * In that case we should only throttle if the backing device it is
  1623. * writing to is congested. In other cases it is safe to throttle.
  1624. */
  1625. static int current_may_throttle(void)
  1626. {
  1627. return !(current->flags & PF_LESS_THROTTLE) ||
  1628. current->backing_dev_info == NULL ||
  1629. bdi_write_congested(current->backing_dev_info);
  1630. }
  1631. /*
  1632. * shrink_inactive_list() is a helper for shrink_node(). It returns the number
  1633. * of reclaimed pages
  1634. */
  1635. static noinline_for_stack unsigned long
  1636. shrink_inactive_list(unsigned long nr_to_scan, struct lruvec *lruvec,
  1637. struct scan_control *sc, enum lru_list lru)
  1638. {
  1639. LIST_HEAD(page_list);
  1640. unsigned long nr_scanned;
  1641. unsigned long nr_reclaimed = 0;
  1642. unsigned long nr_taken;
  1643. struct reclaim_stat stat = {};
  1644. isolate_mode_t isolate_mode = 0;
  1645. int file = is_file_lru(lru);
  1646. struct pglist_data *pgdat = lruvec_pgdat(lruvec);
  1647. struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat;
  1648. bool stalled = false;
  1649. while (unlikely(too_many_isolated(pgdat, file, sc))) {
  1650. if (stalled)
  1651. return 0;
  1652. /* wait a bit for the reclaimer. */
  1653. msleep(100);
  1654. stalled = true;
  1655. /* We are about to die and free our memory. Return now. */
  1656. if (fatal_signal_pending(current))
  1657. return SWAP_CLUSTER_MAX;
  1658. }
  1659. lru_add_drain();
  1660. if (!sc->may_unmap)
  1661. isolate_mode |= ISOLATE_UNMAPPED;
  1662. spin_lock_irq(&pgdat->lru_lock);
  1663. nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &page_list,
  1664. &nr_scanned, sc, isolate_mode, lru);
  1665. __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, nr_taken);
  1666. reclaim_stat->recent_scanned[file] += nr_taken;
  1667. if (current_is_kswapd()) {
  1668. if (global_reclaim(sc))
  1669. __count_vm_events(PGSCAN_KSWAPD, nr_scanned);
  1670. count_memcg_events(lruvec_memcg(lruvec), PGSCAN_KSWAPD,
  1671. nr_scanned);
  1672. } else {
  1673. if (global_reclaim(sc))
  1674. __count_vm_events(PGSCAN_DIRECT, nr_scanned);
  1675. count_memcg_events(lruvec_memcg(lruvec), PGSCAN_DIRECT,
  1676. nr_scanned);
  1677. }
  1678. spin_unlock_irq(&pgdat->lru_lock);
  1679. if (nr_taken == 0)
  1680. return 0;
  1681. nr_reclaimed = shrink_page_list(&page_list, pgdat, sc, 0,
  1682. &stat, false);
  1683. spin_lock_irq(&pgdat->lru_lock);
  1684. if (current_is_kswapd()) {
  1685. if (global_reclaim(sc))
  1686. __count_vm_events(PGSTEAL_KSWAPD, nr_reclaimed);
  1687. count_memcg_events(lruvec_memcg(lruvec), PGSTEAL_KSWAPD,
  1688. nr_reclaimed);
  1689. } else {
  1690. if (global_reclaim(sc))
  1691. __count_vm_events(PGSTEAL_DIRECT, nr_reclaimed);
  1692. count_memcg_events(lruvec_memcg(lruvec), PGSTEAL_DIRECT,
  1693. nr_reclaimed);
  1694. }
  1695. putback_inactive_pages(lruvec, &page_list);
  1696. __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, -nr_taken);
  1697. spin_unlock_irq(&pgdat->lru_lock);
  1698. mem_cgroup_uncharge_list(&page_list);
  1699. free_unref_page_list(&page_list);
  1700. /*
  1701. * If dirty pages are scanned that are not queued for IO, it
  1702. * implies that flushers are not doing their job. This can
  1703. * happen when memory pressure pushes dirty pages to the end of
  1704. * the LRU before the dirty limits are breached and the dirty
  1705. * data has expired. It can also happen when the proportion of
  1706. * dirty pages grows not through writes but through memory
  1707. * pressure reclaiming all the clean cache. And in some cases,
  1708. * the flushers simply cannot keep up with the allocation
  1709. * rate. Nudge the flusher threads in case they are asleep.
  1710. */
  1711. if (stat.nr_unqueued_dirty == nr_taken)
  1712. wakeup_flusher_threads(WB_REASON_VMSCAN);
  1713. sc->nr.dirty += stat.nr_dirty;
  1714. sc->nr.congested += stat.nr_congested;
  1715. sc->nr.unqueued_dirty += stat.nr_unqueued_dirty;
  1716. sc->nr.writeback += stat.nr_writeback;
  1717. sc->nr.immediate += stat.nr_immediate;
  1718. sc->nr.taken += nr_taken;
  1719. if (file)
  1720. sc->nr.file_taken += nr_taken;
  1721. trace_mm_vmscan_lru_shrink_inactive(pgdat->node_id,
  1722. nr_scanned, nr_reclaimed, &stat, sc->priority, file);
  1723. return nr_reclaimed;
  1724. }
  1725. /*
  1726. * This moves pages from the active list to the inactive list.
  1727. *
  1728. * We move them the other way if the page is referenced by one or more
  1729. * processes, from rmap.
  1730. *
  1731. * If the pages are mostly unmapped, the processing is fast and it is
  1732. * appropriate to hold zone_lru_lock across the whole operation. But if
  1733. * the pages are mapped, the processing is slow (page_referenced()) so we
  1734. * should drop zone_lru_lock around each page. It's impossible to balance
  1735. * this, so instead we remove the pages from the LRU while processing them.
  1736. * It is safe to rely on PG_active against the non-LRU pages in here because
  1737. * nobody will play with that bit on a non-LRU page.
  1738. *
  1739. * The downside is that we have to touch page->_refcount against each page.
  1740. * But we had to alter page->flags anyway.
  1741. *
  1742. * Returns the number of pages moved to the given lru.
  1743. */
  1744. static unsigned move_active_pages_to_lru(struct lruvec *lruvec,
  1745. struct list_head *list,
  1746. struct list_head *pages_to_free,
  1747. enum lru_list lru)
  1748. {
  1749. struct pglist_data *pgdat = lruvec_pgdat(lruvec);
  1750. struct page *page;
  1751. int nr_pages;
  1752. int nr_moved = 0;
  1753. while (!list_empty(list)) {
  1754. page = lru_to_page(list);
  1755. lruvec = mem_cgroup_page_lruvec(page, pgdat);
  1756. VM_BUG_ON_PAGE(PageLRU(page), page);
  1757. SetPageLRU(page);
  1758. nr_pages = hpage_nr_pages(page);
  1759. update_lru_size(lruvec, lru, page_zonenum(page), nr_pages);
  1760. list_move(&page->lru, &lruvec->lists[lru]);
  1761. if (put_page_testzero(page)) {
  1762. __ClearPageLRU(page);
  1763. __ClearPageActive(page);
  1764. del_page_from_lru_list(page, lruvec, lru);
  1765. if (unlikely(PageCompound(page))) {
  1766. spin_unlock_irq(&pgdat->lru_lock);
  1767. mem_cgroup_uncharge(page);
  1768. (*get_compound_page_dtor(page))(page);
  1769. spin_lock_irq(&pgdat->lru_lock);
  1770. } else
  1771. list_add(&page->lru, pages_to_free);
  1772. } else {
  1773. nr_moved += nr_pages;
  1774. }
  1775. }
  1776. if (!is_active_lru(lru)) {
  1777. __count_vm_events(PGDEACTIVATE, nr_moved);
  1778. count_memcg_events(lruvec_memcg(lruvec), PGDEACTIVATE,
  1779. nr_moved);
  1780. }
  1781. return nr_moved;
  1782. }
  1783. static void shrink_active_list(unsigned long nr_to_scan,
  1784. struct lruvec *lruvec,
  1785. struct scan_control *sc,
  1786. enum lru_list lru)
  1787. {
  1788. unsigned long nr_taken;
  1789. unsigned long nr_scanned;
  1790. unsigned long vm_flags;
  1791. LIST_HEAD(l_hold); /* The pages which were snipped off */
  1792. LIST_HEAD(l_active);
  1793. LIST_HEAD(l_inactive);
  1794. struct page *page;
  1795. struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat;
  1796. unsigned nr_deactivate, nr_activate;
  1797. unsigned nr_rotated = 0;
  1798. isolate_mode_t isolate_mode = 0;
  1799. int file = is_file_lru(lru);
  1800. struct pglist_data *pgdat = lruvec_pgdat(lruvec);
  1801. lru_add_drain();
  1802. if (!sc->may_unmap)
  1803. isolate_mode |= ISOLATE_UNMAPPED;
  1804. spin_lock_irq(&pgdat->lru_lock);
  1805. nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &l_hold,
  1806. &nr_scanned, sc, isolate_mode, lru);
  1807. __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, nr_taken);
  1808. reclaim_stat->recent_scanned[file] += nr_taken;
  1809. __count_vm_events(PGREFILL, nr_scanned);
  1810. count_memcg_events(lruvec_memcg(lruvec), PGREFILL, nr_scanned);
  1811. spin_unlock_irq(&pgdat->lru_lock);
  1812. while (!list_empty(&l_hold)) {
  1813. cond_resched();
  1814. page = lru_to_page(&l_hold);
  1815. list_del(&page->lru);
  1816. if (unlikely(!page_evictable(page))) {
  1817. putback_lru_page(page);
  1818. continue;
  1819. }
  1820. if (unlikely(buffer_heads_over_limit)) {
  1821. if (page_has_private(page) && trylock_page(page)) {
  1822. if (page_has_private(page))
  1823. try_to_release_page(page, 0);
  1824. unlock_page(page);
  1825. }
  1826. }
  1827. if (page_referenced(page, 0, sc->target_mem_cgroup,
  1828. &vm_flags)) {
  1829. nr_rotated += hpage_nr_pages(page);
  1830. /*
  1831. * Identify referenced, file-backed active pages and
  1832. * give them one more trip around the active list. So
  1833. * that executable code get better chances to stay in
  1834. * memory under moderate memory pressure. Anon pages
  1835. * are not likely to be evicted by use-once streaming
  1836. * IO, plus JVM can create lots of anon VM_EXEC pages,
  1837. * so we ignore them here.
  1838. */
  1839. if ((vm_flags & VM_EXEC) && page_is_file_cache(page)) {
  1840. list_add(&page->lru, &l_active);
  1841. continue;
  1842. }
  1843. }
  1844. ClearPageActive(page); /* we are de-activating */
  1845. list_add(&page->lru, &l_inactive);
  1846. }
  1847. /*
  1848. * Move pages back to the lru list.
  1849. */
  1850. spin_lock_irq(&pgdat->lru_lock);
  1851. /*
  1852. * Count referenced pages from currently used mappings as rotated,
  1853. * even though only some of them are actually re-activated. This
  1854. * helps balance scan pressure between file and anonymous pages in
  1855. * get_scan_count.
  1856. */
  1857. reclaim_stat->recent_rotated[file] += nr_rotated;
  1858. nr_activate = move_active_pages_to_lru(lruvec, &l_active, &l_hold, lru);
  1859. nr_deactivate = move_active_pages_to_lru(lruvec, &l_inactive, &l_hold, lru - LRU_ACTIVE);
  1860. __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, -nr_taken);
  1861. spin_unlock_irq(&pgdat->lru_lock);
  1862. mem_cgroup_uncharge_list(&l_hold);
  1863. free_unref_page_list(&l_hold);
  1864. trace_mm_vmscan_lru_shrink_active(pgdat->node_id, nr_taken, nr_activate,
  1865. nr_deactivate, nr_rotated, sc->priority, file);
  1866. }
  1867. /*
  1868. * The inactive anon list should be small enough that the VM never has
  1869. * to do too much work.
  1870. *
  1871. * The inactive file list should be small enough to leave most memory
  1872. * to the established workingset on the scan-resistant active list,
  1873. * but large enough to avoid thrashing the aggregate readahead window.
  1874. *
  1875. * Both inactive lists should also be large enough that each inactive
  1876. * page has a chance to be referenced again before it is reclaimed.
  1877. *
  1878. * If that fails and refaulting is observed, the inactive list grows.
  1879. *
  1880. * The inactive_ratio is the target ratio of ACTIVE to INACTIVE pages
  1881. * on this LRU, maintained by the pageout code. An inactive_ratio
  1882. * of 3 means 3:1 or 25% of the pages are kept on the inactive list.
  1883. *
  1884. * total target max
  1885. * memory ratio inactive
  1886. * -------------------------------------
  1887. * 10MB 1 5MB
  1888. * 100MB 1 50MB
  1889. * 1GB 3 250MB
  1890. * 10GB 10 0.9GB
  1891. * 100GB 31 3GB
  1892. * 1TB 101 10GB
  1893. * 10TB 320 32GB
  1894. */
  1895. static bool inactive_list_is_low(struct lruvec *lruvec, bool file,
  1896. struct mem_cgroup *memcg,
  1897. struct scan_control *sc, bool actual_reclaim)
  1898. {
  1899. enum lru_list active_lru = file * LRU_FILE + LRU_ACTIVE;
  1900. struct pglist_data *pgdat = lruvec_pgdat(lruvec);
  1901. enum lru_list inactive_lru = file * LRU_FILE;
  1902. unsigned long inactive, active;
  1903. unsigned long inactive_ratio;
  1904. unsigned long refaults;
  1905. unsigned long gb;
  1906. /*
  1907. * If we don't have swap space, anonymous page deactivation
  1908. * is pointless.
  1909. */
  1910. if (!file && !total_swap_pages)
  1911. return false;
  1912. inactive = lruvec_lru_size(lruvec, inactive_lru, sc->reclaim_idx);
  1913. active = lruvec_lru_size(lruvec, active_lru, sc->reclaim_idx);
  1914. if (memcg)
  1915. refaults = memcg_page_state(memcg, WORKINGSET_ACTIVATE);
  1916. else
  1917. refaults = node_page_state(pgdat, WORKINGSET_ACTIVATE);
  1918. /*
  1919. * When refaults are being observed, it means a new workingset
  1920. * is being established. Disable active list protection to get
  1921. * rid of the stale workingset quickly.
  1922. */
  1923. if (file && actual_reclaim && lruvec->refaults != refaults) {
  1924. inactive_ratio = 0;
  1925. } else {
  1926. gb = (inactive + active) >> (30 - PAGE_SHIFT);
  1927. if (gb)
  1928. inactive_ratio = int_sqrt(10 * gb);
  1929. else
  1930. inactive_ratio = 1;
  1931. }
  1932. if (actual_reclaim)
  1933. trace_mm_vmscan_inactive_list_is_low(pgdat->node_id, sc->reclaim_idx,
  1934. lruvec_lru_size(lruvec, inactive_lru, MAX_NR_ZONES), inactive,
  1935. lruvec_lru_size(lruvec, active_lru, MAX_NR_ZONES), active,
  1936. inactive_ratio, file);
  1937. return inactive * inactive_ratio < active;
  1938. }
  1939. static unsigned long shrink_list(enum lru_list lru, unsigned long nr_to_scan,
  1940. struct lruvec *lruvec, struct mem_cgroup *memcg,
  1941. struct scan_control *sc)
  1942. {
  1943. if (is_active_lru(lru)) {
  1944. if (inactive_list_is_low(lruvec, is_file_lru(lru),
  1945. memcg, sc, true))
  1946. shrink_active_list(nr_to_scan, lruvec, sc, lru);
  1947. return 0;
  1948. }
  1949. return shrink_inactive_list(nr_to_scan, lruvec, sc, lru);
  1950. }
  1951. enum scan_balance {
  1952. SCAN_EQUAL,
  1953. SCAN_FRACT,
  1954. SCAN_ANON,
  1955. SCAN_FILE,
  1956. };
  1957. /*
  1958. * Determine how aggressively the anon and file LRU lists should be
  1959. * scanned. The relative value of each set of LRU lists is determined
  1960. * by looking at the fraction of the pages scanned we did rotate back
  1961. * onto the active list instead of evict.
  1962. *
  1963. * nr[0] = anon inactive pages to scan; nr[1] = anon active pages to scan
  1964. * nr[2] = file inactive pages to scan; nr[3] = file active pages to scan
  1965. */
  1966. static void get_scan_count(struct lruvec *lruvec, struct mem_cgroup *memcg,
  1967. struct scan_control *sc, unsigned long *nr,
  1968. unsigned long *lru_pages)
  1969. {
  1970. int swappiness = mem_cgroup_swappiness(memcg);
  1971. struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat;
  1972. u64 fraction[2];
  1973. u64 denominator = 0; /* gcc */
  1974. struct pglist_data *pgdat = lruvec_pgdat(lruvec);
  1975. unsigned long anon_prio, file_prio;
  1976. enum scan_balance scan_balance;
  1977. unsigned long anon, file;
  1978. unsigned long ap, fp;
  1979. enum lru_list lru;
  1980. /* If we have no swap space, do not bother scanning anon pages. */
  1981. if (!sc->may_swap || mem_cgroup_get_nr_swap_pages(memcg) <= 0) {
  1982. scan_balance = SCAN_FILE;
  1983. goto out;
  1984. }
  1985. /*
  1986. * Global reclaim will swap to prevent OOM even with no
  1987. * swappiness, but memcg users want to use this knob to
  1988. * disable swapping for individual groups completely when
  1989. * using the memory controller's swap limit feature would be
  1990. * too expensive.
  1991. */
  1992. if (!global_reclaim(sc) && !swappiness) {
  1993. scan_balance = SCAN_FILE;
  1994. goto out;
  1995. }
  1996. /*
  1997. * Do not apply any pressure balancing cleverness when the
  1998. * system is close to OOM, scan both anon and file equally
  1999. * (unless the swappiness setting disagrees with swapping).
  2000. */
  2001. if (!sc->priority && swappiness) {
  2002. scan_balance = SCAN_EQUAL;
  2003. goto out;
  2004. }
  2005. /*
  2006. * Prevent the reclaimer from falling into the cache trap: as
  2007. * cache pages start out inactive, every cache fault will tip
  2008. * the scan balance towards the file LRU. And as the file LRU
  2009. * shrinks, so does the window for rotation from references.
  2010. * This means we have a runaway feedback loop where a tiny
  2011. * thrashing file LRU becomes infinitely more attractive than
  2012. * anon pages. Try to detect this based on file LRU size.
  2013. */
  2014. if (global_reclaim(sc)) {
  2015. unsigned long pgdatfile;
  2016. unsigned long pgdatfree;
  2017. int z;
  2018. unsigned long total_high_wmark = 0;
  2019. pgdatfree = sum_zone_node_page_state(pgdat->node_id, NR_FREE_PAGES);
  2020. pgdatfile = node_page_state(pgdat, NR_ACTIVE_FILE) +
  2021. node_page_state(pgdat, NR_INACTIVE_FILE);
  2022. for (z = 0; z < MAX_NR_ZONES; z++) {
  2023. struct zone *zone = &pgdat->node_zones[z];
  2024. if (!managed_zone(zone))
  2025. continue;
  2026. total_high_wmark += high_wmark_pages(zone);
  2027. }
  2028. if (unlikely(pgdatfile + pgdatfree <= total_high_wmark)) {
  2029. /*
  2030. * Force SCAN_ANON if there are enough inactive
  2031. * anonymous pages on the LRU in eligible zones.
  2032. * Otherwise, the small LRU gets thrashed.
  2033. */
  2034. if (!inactive_list_is_low(lruvec, false, memcg, sc, false) &&
  2035. lruvec_lru_size(lruvec, LRU_INACTIVE_ANON, sc->reclaim_idx)
  2036. >> sc->priority) {
  2037. scan_balance = SCAN_ANON;
  2038. goto out;
  2039. }
  2040. }
  2041. }
  2042. /*
  2043. * If there is enough inactive page cache, i.e. if the size of the
  2044. * inactive list is greater than that of the active list *and* the
  2045. * inactive list actually has some pages to scan on this priority, we
  2046. * do not reclaim anything from the anonymous working set right now.
  2047. * Without the second condition we could end up never scanning an
  2048. * lruvec even if it has plenty of old anonymous pages unless the
  2049. * system is under heavy pressure.
  2050. */
  2051. if (!inactive_list_is_low(lruvec, true, memcg, sc, false) &&
  2052. lruvec_lru_size(lruvec, LRU_INACTIVE_FILE, sc->reclaim_idx) >> sc->priority) {
  2053. scan_balance = SCAN_FILE;
  2054. goto out;
  2055. }
  2056. scan_balance = SCAN_FRACT;
  2057. /*
  2058. * With swappiness at 100, anonymous and file have the same priority.
  2059. * This scanning priority is essentially the inverse of IO cost.
  2060. */
  2061. anon_prio = swappiness;
  2062. file_prio = 200 - anon_prio;
  2063. /*
  2064. * OK, so we have swap space and a fair amount of page cache
  2065. * pages. We use the recently rotated / recently scanned
  2066. * ratios to determine how valuable each cache is.
  2067. *
  2068. * Because workloads change over time (and to avoid overflow)
  2069. * we keep these statistics as a floating average, which ends
  2070. * up weighing recent references more than old ones.
  2071. *
  2072. * anon in [0], file in [1]
  2073. */
  2074. anon = lruvec_lru_size(lruvec, LRU_ACTIVE_ANON, MAX_NR_ZONES) +
  2075. lruvec_lru_size(lruvec, LRU_INACTIVE_ANON, MAX_NR_ZONES);
  2076. file = lruvec_lru_size(lruvec, LRU_ACTIVE_FILE, MAX_NR_ZONES) +
  2077. lruvec_lru_size(lruvec, LRU_INACTIVE_FILE, MAX_NR_ZONES);
  2078. spin_lock_irq(&pgdat->lru_lock);
  2079. if (unlikely(reclaim_stat->recent_scanned[0] > anon / 4)) {
  2080. reclaim_stat->recent_scanned[0] /= 2;
  2081. reclaim_stat->recent_rotated[0] /= 2;
  2082. }
  2083. if (unlikely(reclaim_stat->recent_scanned[1] > file / 4)) {
  2084. reclaim_stat->recent_scanned[1] /= 2;
  2085. reclaim_stat->recent_rotated[1] /= 2;
  2086. }
  2087. /*
  2088. * The amount of pressure on anon vs file pages is inversely
  2089. * proportional to the fraction of recently scanned pages on
  2090. * each list that were recently referenced and in active use.
  2091. */
  2092. ap = anon_prio * (reclaim_stat->recent_scanned[0] + 1);
  2093. ap /= reclaim_stat->recent_rotated[0] + 1;
  2094. fp = file_prio * (reclaim_stat->recent_scanned[1] + 1);
  2095. fp /= reclaim_stat->recent_rotated[1] + 1;
  2096. spin_unlock_irq(&pgdat->lru_lock);
  2097. fraction[0] = ap;
  2098. fraction[1] = fp;
  2099. denominator = ap + fp + 1;
  2100. out:
  2101. *lru_pages = 0;
  2102. for_each_evictable_lru(lru) {
  2103. int file = is_file_lru(lru);
  2104. unsigned long size;
  2105. unsigned long scan;
  2106. size = lruvec_lru_size(lruvec, lru, sc->reclaim_idx);
  2107. scan = size >> sc->priority;
  2108. /*
  2109. * If the cgroup's already been deleted, make sure to
  2110. * scrape out the remaining cache.
  2111. */
  2112. if (!scan && !mem_cgroup_online(memcg))
  2113. scan = min(size, SWAP_CLUSTER_MAX);
  2114. switch (scan_balance) {
  2115. case SCAN_EQUAL:
  2116. /* Scan lists relative to size */
  2117. break;
  2118. case SCAN_FRACT:
  2119. /*
  2120. * Scan types proportional to swappiness and
  2121. * their relative recent reclaim efficiency.
  2122. */
  2123. scan = div64_u64(scan * fraction[file],
  2124. denominator);
  2125. break;
  2126. case SCAN_FILE:
  2127. case SCAN_ANON:
  2128. /* Scan one type exclusively */
  2129. if ((scan_balance == SCAN_FILE) != file) {
  2130. size = 0;
  2131. scan = 0;
  2132. }
  2133. break;
  2134. default:
  2135. /* Look ma, no brain */
  2136. BUG();
  2137. }
  2138. *lru_pages += size;
  2139. nr[lru] = scan;
  2140. }
  2141. }
  2142. /*
  2143. * This is a basic per-node page freer. Used by both kswapd and direct reclaim.
  2144. */
  2145. static void shrink_node_memcg(struct pglist_data *pgdat, struct mem_cgroup *memcg,
  2146. struct scan_control *sc, unsigned long *lru_pages)
  2147. {
  2148. struct lruvec *lruvec = mem_cgroup_lruvec(pgdat, memcg);
  2149. unsigned long nr[NR_LRU_LISTS];
  2150. unsigned long targets[NR_LRU_LISTS];
  2151. unsigned long nr_to_scan;
  2152. enum lru_list lru;
  2153. unsigned long nr_reclaimed = 0;
  2154. unsigned long nr_to_reclaim = sc->nr_to_reclaim;
  2155. struct blk_plug plug;
  2156. bool scan_adjusted;
  2157. get_scan_count(lruvec, memcg, sc, nr, lru_pages);
  2158. /* Record the original scan target for proportional adjustments later */
  2159. memcpy(targets, nr, sizeof(nr));
  2160. /*
  2161. * Global reclaiming within direct reclaim at DEF_PRIORITY is a normal
  2162. * event that can occur when there is little memory pressure e.g.
  2163. * multiple streaming readers/writers. Hence, we do not abort scanning
  2164. * when the requested number of pages are reclaimed when scanning at
  2165. * DEF_PRIORITY on the assumption that the fact we are direct
  2166. * reclaiming implies that kswapd is not keeping up and it is best to
  2167. * do a batch of work at once. For memcg reclaim one check is made to
  2168. * abort proportional reclaim if either the file or anon lru has already
  2169. * dropped to zero at the first pass.
  2170. */
  2171. scan_adjusted = (global_reclaim(sc) && !current_is_kswapd() &&
  2172. sc->priority == DEF_PRIORITY);
  2173. blk_start_plug(&plug);
  2174. while (nr[LRU_INACTIVE_ANON] || nr[LRU_ACTIVE_FILE] ||
  2175. nr[LRU_INACTIVE_FILE]) {
  2176. unsigned long nr_anon, nr_file, percentage;
  2177. unsigned long nr_scanned;
  2178. for_each_evictable_lru(lru) {
  2179. if (nr[lru]) {
  2180. nr_to_scan = min(nr[lru], SWAP_CLUSTER_MAX);
  2181. nr[lru] -= nr_to_scan;
  2182. nr_reclaimed += shrink_list(lru, nr_to_scan,
  2183. lruvec, memcg, sc);
  2184. }
  2185. }
  2186. cond_resched();
  2187. if (nr_reclaimed < nr_to_reclaim || scan_adjusted)
  2188. continue;
  2189. /*
  2190. * For kswapd and memcg, reclaim at least the number of pages
  2191. * requested. Ensure that the anon and file LRUs are scanned
  2192. * proportionally what was requested by get_scan_count(). We
  2193. * stop reclaiming one LRU and reduce the amount scanning
  2194. * proportional to the original scan target.
  2195. */
  2196. nr_file = nr[LRU_INACTIVE_FILE] + nr[LRU_ACTIVE_FILE];
  2197. nr_anon = nr[LRU_INACTIVE_ANON] + nr[LRU_ACTIVE_ANON];
  2198. /*
  2199. * It's just vindictive to attack the larger once the smaller
  2200. * has gone to zero. And given the way we stop scanning the
  2201. * smaller below, this makes sure that we only make one nudge
  2202. * towards proportionality once we've got nr_to_reclaim.
  2203. */
  2204. if (!nr_file || !nr_anon)
  2205. break;
  2206. if (nr_file > nr_anon) {
  2207. unsigned long scan_target = targets[LRU_INACTIVE_ANON] +
  2208. targets[LRU_ACTIVE_ANON] + 1;
  2209. lru = LRU_BASE;
  2210. percentage = nr_anon * 100 / scan_target;
  2211. } else {
  2212. unsigned long scan_target = targets[LRU_INACTIVE_FILE] +
  2213. targets[LRU_ACTIVE_FILE] + 1;
  2214. lru = LRU_FILE;
  2215. percentage = nr_file * 100 / scan_target;
  2216. }
  2217. /* Stop scanning the smaller of the LRU */
  2218. nr[lru] = 0;
  2219. nr[lru + LRU_ACTIVE] = 0;
  2220. /*
  2221. * Recalculate the other LRU scan count based on its original
  2222. * scan target and the percentage scanning already complete
  2223. */
  2224. lru = (lru == LRU_FILE) ? LRU_BASE : LRU_FILE;
  2225. nr_scanned = targets[lru] - nr[lru];
  2226. nr[lru] = targets[lru] * (100 - percentage) / 100;
  2227. nr[lru] -= min(nr[lru], nr_scanned);
  2228. lru += LRU_ACTIVE;
  2229. nr_scanned = targets[lru] - nr[lru];
  2230. nr[lru] = targets[lru] * (100 - percentage) / 100;
  2231. nr[lru] -= min(nr[lru], nr_scanned);
  2232. scan_adjusted = true;
  2233. }
  2234. blk_finish_plug(&plug);
  2235. sc->nr_reclaimed += nr_reclaimed;
  2236. /*
  2237. * Even if we did not try to evict anon pages at all, we want to
  2238. * rebalance the anon lru active/inactive ratio.
  2239. */
  2240. if (inactive_list_is_low(lruvec, false, memcg, sc, true))
  2241. shrink_active_list(SWAP_CLUSTER_MAX, lruvec,
  2242. sc, LRU_ACTIVE_ANON);
  2243. }
  2244. /* Use reclaim/compaction for costly allocs or under memory pressure */
  2245. static bool in_reclaim_compaction(struct scan_control *sc)
  2246. {
  2247. if (IS_ENABLED(CONFIG_COMPACTION) && sc->order &&
  2248. (sc->order > PAGE_ALLOC_COSTLY_ORDER ||
  2249. sc->priority < DEF_PRIORITY - 2))
  2250. return true;
  2251. return false;
  2252. }
  2253. /*
  2254. * Reclaim/compaction is used for high-order allocation requests. It reclaims
  2255. * order-0 pages before compacting the zone. should_continue_reclaim() returns
  2256. * true if more pages should be reclaimed such that when the page allocator
  2257. * calls try_to_compact_zone() that it will have enough free pages to succeed.
  2258. * It will give up earlier than that if there is difficulty reclaiming pages.
  2259. */
  2260. static inline bool should_continue_reclaim(struct pglist_data *pgdat,
  2261. unsigned long nr_reclaimed,
  2262. unsigned long nr_scanned,
  2263. struct scan_control *sc)
  2264. {
  2265. unsigned long pages_for_compaction;
  2266. unsigned long inactive_lru_pages;
  2267. int z;
  2268. /* If not in reclaim/compaction mode, stop */
  2269. if (!in_reclaim_compaction(sc))
  2270. return false;
  2271. /* Consider stopping depending on scan and reclaim activity */
  2272. if (sc->gfp_mask & __GFP_RETRY_MAYFAIL) {
  2273. /*
  2274. * For __GFP_RETRY_MAYFAIL allocations, stop reclaiming if the
  2275. * full LRU list has been scanned and we are still failing
  2276. * to reclaim pages. This full LRU scan is potentially
  2277. * expensive but a __GFP_RETRY_MAYFAIL caller really wants to succeed
  2278. */
  2279. if (!nr_reclaimed && !nr_scanned)
  2280. return false;
  2281. } else {
  2282. /*
  2283. * For non-__GFP_RETRY_MAYFAIL allocations which can presumably
  2284. * fail without consequence, stop if we failed to reclaim
  2285. * any pages from the last SWAP_CLUSTER_MAX number of
  2286. * pages that were scanned. This will return to the
  2287. * caller faster at the risk reclaim/compaction and
  2288. * the resulting allocation attempt fails
  2289. */
  2290. if (!nr_reclaimed)
  2291. return false;
  2292. }
  2293. /*
  2294. * If we have not reclaimed enough pages for compaction and the
  2295. * inactive lists are large enough, continue reclaiming
  2296. */
  2297. pages_for_compaction = compact_gap(sc->order);
  2298. inactive_lru_pages = node_page_state(pgdat, NR_INACTIVE_FILE);
  2299. if (get_nr_swap_pages() > 0)
  2300. inactive_lru_pages += node_page_state(pgdat, NR_INACTIVE_ANON);
  2301. if (sc->nr_reclaimed < pages_for_compaction &&
  2302. inactive_lru_pages > pages_for_compaction)
  2303. return true;
  2304. /* If compaction would go ahead or the allocation would succeed, stop */
  2305. for (z = 0; z <= sc->reclaim_idx; z++) {
  2306. struct zone *zone = &pgdat->node_zones[z];
  2307. if (!managed_zone(zone))
  2308. continue;
  2309. switch (compaction_suitable(zone, sc->order, 0, sc->reclaim_idx)) {
  2310. case COMPACT_SUCCESS:
  2311. case COMPACT_CONTINUE:
  2312. return false;
  2313. default:
  2314. /* check next zone */
  2315. ;
  2316. }
  2317. }
  2318. return true;
  2319. }
  2320. static bool pgdat_memcg_congested(pg_data_t *pgdat, struct mem_cgroup *memcg)
  2321. {
  2322. return test_bit(PGDAT_CONGESTED, &pgdat->flags) ||
  2323. (memcg && memcg_congested(pgdat, memcg));
  2324. }
  2325. static bool shrink_node(pg_data_t *pgdat, struct scan_control *sc)
  2326. {
  2327. struct reclaim_state *reclaim_state = current->reclaim_state;
  2328. unsigned long nr_reclaimed, nr_scanned;
  2329. bool reclaimable = false;
  2330. do {
  2331. struct mem_cgroup *root = sc->target_mem_cgroup;
  2332. struct mem_cgroup_reclaim_cookie reclaim = {
  2333. .pgdat = pgdat,
  2334. .priority = sc->priority,
  2335. };
  2336. unsigned long node_lru_pages = 0;
  2337. struct mem_cgroup *memcg;
  2338. memset(&sc->nr, 0, sizeof(sc->nr));
  2339. nr_reclaimed = sc->nr_reclaimed;
  2340. nr_scanned = sc->nr_scanned;
  2341. memcg = mem_cgroup_iter(root, NULL, &reclaim);
  2342. do {
  2343. unsigned long lru_pages;
  2344. unsigned long reclaimed;
  2345. unsigned long scanned;
  2346. switch (mem_cgroup_protected(root, memcg)) {
  2347. case MEMCG_PROT_MIN:
  2348. /*
  2349. * Hard protection.
  2350. * If there is no reclaimable memory, OOM.
  2351. */
  2352. continue;
  2353. case MEMCG_PROT_LOW:
  2354. /*
  2355. * Soft protection.
  2356. * Respect the protection only as long as
  2357. * there is an unprotected supply
  2358. * of reclaimable memory from other cgroups.
  2359. */
  2360. if (!sc->memcg_low_reclaim) {
  2361. sc->memcg_low_skipped = 1;
  2362. continue;
  2363. }
  2364. memcg_memory_event(memcg, MEMCG_LOW);
  2365. break;
  2366. case MEMCG_PROT_NONE:
  2367. break;
  2368. }
  2369. reclaimed = sc->nr_reclaimed;
  2370. scanned = sc->nr_scanned;
  2371. shrink_node_memcg(pgdat, memcg, sc, &lru_pages);
  2372. node_lru_pages += lru_pages;
  2373. shrink_slab(sc->gfp_mask, pgdat->node_id,
  2374. memcg, sc->priority);
  2375. /* Record the group's reclaim efficiency */
  2376. vmpressure(sc->gfp_mask, memcg, false,
  2377. sc->nr_scanned - scanned,
  2378. sc->nr_reclaimed - reclaimed);
  2379. /*
  2380. * Direct reclaim and kswapd have to scan all memory
  2381. * cgroups to fulfill the overall scan target for the
  2382. * node.
  2383. *
  2384. * Limit reclaim, on the other hand, only cares about
  2385. * nr_to_reclaim pages to be reclaimed and it will
  2386. * retry with decreasing priority if one round over the
  2387. * whole hierarchy is not sufficient.
  2388. */
  2389. if (!global_reclaim(sc) &&
  2390. sc->nr_reclaimed >= sc->nr_to_reclaim) {
  2391. mem_cgroup_iter_break(root, memcg);
  2392. break;
  2393. }
  2394. } while ((memcg = mem_cgroup_iter(root, memcg, &reclaim)));
  2395. if (reclaim_state) {
  2396. sc->nr_reclaimed += reclaim_state->reclaimed_slab;
  2397. reclaim_state->reclaimed_slab = 0;
  2398. }
  2399. /* Record the subtree's reclaim efficiency */
  2400. vmpressure(sc->gfp_mask, sc->target_mem_cgroup, true,
  2401. sc->nr_scanned - nr_scanned,
  2402. sc->nr_reclaimed - nr_reclaimed);
  2403. if (sc->nr_reclaimed - nr_reclaimed)
  2404. reclaimable = true;
  2405. if (current_is_kswapd()) {
  2406. /*
  2407. * If reclaim is isolating dirty pages under writeback,
  2408. * it implies that the long-lived page allocation rate
  2409. * is exceeding the page laundering rate. Either the
  2410. * global limits are not being effective at throttling
  2411. * processes due to the page distribution throughout
  2412. * zones or there is heavy usage of a slow backing
  2413. * device. The only option is to throttle from reclaim
  2414. * context which is not ideal as there is no guarantee
  2415. * the dirtying process is throttled in the same way
  2416. * balance_dirty_pages() manages.
  2417. *
  2418. * Once a node is flagged PGDAT_WRITEBACK, kswapd will
  2419. * count the number of pages under pages flagged for
  2420. * immediate reclaim and stall if any are encountered
  2421. * in the nr_immediate check below.
  2422. */
  2423. if (sc->nr.writeback && sc->nr.writeback == sc->nr.taken)
  2424. set_bit(PGDAT_WRITEBACK, &pgdat->flags);
  2425. /*
  2426. * Tag a node as congested if all the dirty pages
  2427. * scanned were backed by a congested BDI and
  2428. * wait_iff_congested will stall.
  2429. */
  2430. if (sc->nr.dirty && sc->nr.dirty == sc->nr.congested)
  2431. set_bit(PGDAT_CONGESTED, &pgdat->flags);
  2432. /* Allow kswapd to start writing pages during reclaim.*/
  2433. if (sc->nr.unqueued_dirty == sc->nr.file_taken)
  2434. set_bit(PGDAT_DIRTY, &pgdat->flags);
  2435. /*
  2436. * If kswapd scans pages marked marked for immediate
  2437. * reclaim and under writeback (nr_immediate), it
  2438. * implies that pages are cycling through the LRU
  2439. * faster than they are written so also forcibly stall.
  2440. */
  2441. if (sc->nr.immediate)
  2442. congestion_wait(BLK_RW_ASYNC, HZ/10);
  2443. }
  2444. /*
  2445. * Legacy memcg will stall in page writeback so avoid forcibly
  2446. * stalling in wait_iff_congested().
  2447. */
  2448. if (!global_reclaim(sc) && sane_reclaim(sc) &&
  2449. sc->nr.dirty && sc->nr.dirty == sc->nr.congested)
  2450. set_memcg_congestion(pgdat, root, true);
  2451. /*
  2452. * Stall direct reclaim for IO completions if underlying BDIs
  2453. * and node is congested. Allow kswapd to continue until it
  2454. * starts encountering unqueued dirty pages or cycling through
  2455. * the LRU too quickly.
  2456. */
  2457. if (!sc->hibernation_mode && !current_is_kswapd() &&
  2458. current_may_throttle() && pgdat_memcg_congested(pgdat, root))
  2459. wait_iff_congested(BLK_RW_ASYNC, HZ/10);
  2460. } while (should_continue_reclaim(pgdat, sc->nr_reclaimed - nr_reclaimed,
  2461. sc->nr_scanned - nr_scanned, sc));
  2462. /*
  2463. * Kswapd gives up on balancing particular nodes after too
  2464. * many failures to reclaim anything from them and goes to
  2465. * sleep. On reclaim progress, reset the failure counter. A
  2466. * successful direct reclaim run will revive a dormant kswapd.
  2467. */
  2468. if (reclaimable)
  2469. pgdat->kswapd_failures = 0;
  2470. return reclaimable;
  2471. }
  2472. /*
  2473. * Returns true if compaction should go ahead for a costly-order request, or
  2474. * the allocation would already succeed without compaction. Return false if we
  2475. * should reclaim first.
  2476. */
  2477. static inline bool compaction_ready(struct zone *zone, struct scan_control *sc)
  2478. {
  2479. unsigned long watermark;
  2480. enum compact_result suitable;
  2481. suitable = compaction_suitable(zone, sc->order, 0, sc->reclaim_idx);
  2482. if (suitable == COMPACT_SUCCESS)
  2483. /* Allocation should succeed already. Don't reclaim. */
  2484. return true;
  2485. if (suitable == COMPACT_SKIPPED)
  2486. /* Compaction cannot yet proceed. Do reclaim. */
  2487. return false;
  2488. /*
  2489. * Compaction is already possible, but it takes time to run and there
  2490. * are potentially other callers using the pages just freed. So proceed
  2491. * with reclaim to make a buffer of free pages available to give
  2492. * compaction a reasonable chance of completing and allocating the page.
  2493. * Note that we won't actually reclaim the whole buffer in one attempt
  2494. * as the target watermark in should_continue_reclaim() is lower. But if
  2495. * we are already above the high+gap watermark, don't reclaim at all.
  2496. */
  2497. watermark = high_wmark_pages(zone) + compact_gap(sc->order);
  2498. return zone_watermark_ok_safe(zone, 0, watermark, sc->reclaim_idx);
  2499. }
  2500. /*
  2501. * This is the direct reclaim path, for page-allocating processes. We only
  2502. * try to reclaim pages from zones which will satisfy the caller's allocation
  2503. * request.
  2504. *
  2505. * If a zone is deemed to be full of pinned pages then just give it a light
  2506. * scan then give up on it.
  2507. */
  2508. static void shrink_zones(struct zonelist *zonelist, struct scan_control *sc)
  2509. {
  2510. struct zoneref *z;
  2511. struct zone *zone;
  2512. unsigned long nr_soft_reclaimed;
  2513. unsigned long nr_soft_scanned;
  2514. gfp_t orig_mask;
  2515. pg_data_t *last_pgdat = NULL;
  2516. /*
  2517. * If the number of buffer_heads in the machine exceeds the maximum
  2518. * allowed level, force direct reclaim to scan the highmem zone as
  2519. * highmem pages could be pinning lowmem pages storing buffer_heads
  2520. */
  2521. orig_mask = sc->gfp_mask;
  2522. if (buffer_heads_over_limit) {
  2523. sc->gfp_mask |= __GFP_HIGHMEM;
  2524. sc->reclaim_idx = gfp_zone(sc->gfp_mask);
  2525. }
  2526. for_each_zone_zonelist_nodemask(zone, z, zonelist,
  2527. sc->reclaim_idx, sc->nodemask) {
  2528. /*
  2529. * Take care memory controller reclaiming has small influence
  2530. * to global LRU.
  2531. */
  2532. if (global_reclaim(sc)) {
  2533. if (!cpuset_zone_allowed(zone,
  2534. GFP_KERNEL | __GFP_HARDWALL))
  2535. continue;
  2536. /*
  2537. * If we already have plenty of memory free for
  2538. * compaction in this zone, don't free any more.
  2539. * Even though compaction is invoked for any
  2540. * non-zero order, only frequent costly order
  2541. * reclamation is disruptive enough to become a
  2542. * noticeable problem, like transparent huge
  2543. * page allocations.
  2544. */
  2545. if (IS_ENABLED(CONFIG_COMPACTION) &&
  2546. sc->order > PAGE_ALLOC_COSTLY_ORDER &&
  2547. compaction_ready(zone, sc)) {
  2548. sc->compaction_ready = true;
  2549. continue;
  2550. }
  2551. /*
  2552. * Shrink each node in the zonelist once. If the
  2553. * zonelist is ordered by zone (not the default) then a
  2554. * node may be shrunk multiple times but in that case
  2555. * the user prefers lower zones being preserved.
  2556. */
  2557. if (zone->zone_pgdat == last_pgdat)
  2558. continue;
  2559. /*
  2560. * This steals pages from memory cgroups over softlimit
  2561. * and returns the number of reclaimed pages and
  2562. * scanned pages. This works for global memory pressure
  2563. * and balancing, not for a memcg's limit.
  2564. */
  2565. nr_soft_scanned = 0;
  2566. nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(zone->zone_pgdat,
  2567. sc->order, sc->gfp_mask,
  2568. &nr_soft_scanned);
  2569. sc->nr_reclaimed += nr_soft_reclaimed;
  2570. sc->nr_scanned += nr_soft_scanned;
  2571. /* need some check for avoid more shrink_zone() */
  2572. }
  2573. /* See comment about same check for global reclaim above */
  2574. if (zone->zone_pgdat == last_pgdat)
  2575. continue;
  2576. last_pgdat = zone->zone_pgdat;
  2577. shrink_node(zone->zone_pgdat, sc);
  2578. }
  2579. /*
  2580. * Restore to original mask to avoid the impact on the caller if we
  2581. * promoted it to __GFP_HIGHMEM.
  2582. */
  2583. sc->gfp_mask = orig_mask;
  2584. }
  2585. static void snapshot_refaults(struct mem_cgroup *root_memcg, pg_data_t *pgdat)
  2586. {
  2587. struct mem_cgroup *memcg;
  2588. memcg = mem_cgroup_iter(root_memcg, NULL, NULL);
  2589. do {
  2590. unsigned long refaults;
  2591. struct lruvec *lruvec;
  2592. if (memcg)
  2593. refaults = memcg_page_state(memcg, WORKINGSET_ACTIVATE);
  2594. else
  2595. refaults = node_page_state(pgdat, WORKINGSET_ACTIVATE);
  2596. lruvec = mem_cgroup_lruvec(pgdat, memcg);
  2597. lruvec->refaults = refaults;
  2598. } while ((memcg = mem_cgroup_iter(root_memcg, memcg, NULL)));
  2599. }
  2600. /*
  2601. * This is the main entry point to direct page reclaim.
  2602. *
  2603. * If a full scan of the inactive list fails to free enough memory then we
  2604. * are "out of memory" and something needs to be killed.
  2605. *
  2606. * If the caller is !__GFP_FS then the probability of a failure is reasonably
  2607. * high - the zone may be full of dirty or under-writeback pages, which this
  2608. * caller can't do much about. We kick the writeback threads and take explicit
  2609. * naps in the hope that some of these pages can be written. But if the
  2610. * allocating task holds filesystem locks which prevent writeout this might not
  2611. * work, and the allocation attempt will fail.
  2612. *
  2613. * returns: 0, if no pages reclaimed
  2614. * else, the number of pages reclaimed
  2615. */
  2616. static unsigned long do_try_to_free_pages(struct zonelist *zonelist,
  2617. struct scan_control *sc)
  2618. {
  2619. int initial_priority = sc->priority;
  2620. pg_data_t *last_pgdat;
  2621. struct zoneref *z;
  2622. struct zone *zone;
  2623. retry:
  2624. delayacct_freepages_start();
  2625. if (global_reclaim(sc))
  2626. __count_zid_vm_events(ALLOCSTALL, sc->reclaim_idx, 1);
  2627. do {
  2628. vmpressure_prio(sc->gfp_mask, sc->target_mem_cgroup,
  2629. sc->priority);
  2630. sc->nr_scanned = 0;
  2631. shrink_zones(zonelist, sc);
  2632. if (sc->nr_reclaimed >= sc->nr_to_reclaim)
  2633. break;
  2634. if (sc->compaction_ready)
  2635. break;
  2636. /*
  2637. * If we're getting trouble reclaiming, start doing
  2638. * writepage even in laptop mode.
  2639. */
  2640. if (sc->priority < DEF_PRIORITY - 2)
  2641. sc->may_writepage = 1;
  2642. } while (--sc->priority >= 0);
  2643. last_pgdat = NULL;
  2644. for_each_zone_zonelist_nodemask(zone, z, zonelist, sc->reclaim_idx,
  2645. sc->nodemask) {
  2646. if (zone->zone_pgdat == last_pgdat)
  2647. continue;
  2648. last_pgdat = zone->zone_pgdat;
  2649. snapshot_refaults(sc->target_mem_cgroup, zone->zone_pgdat);
  2650. set_memcg_congestion(last_pgdat, sc->target_mem_cgroup, false);
  2651. }
  2652. delayacct_freepages_end();
  2653. if (sc->nr_reclaimed)
  2654. return sc->nr_reclaimed;
  2655. /* Aborted reclaim to try compaction? don't OOM, then */
  2656. if (sc->compaction_ready)
  2657. return 1;
  2658. /* Untapped cgroup reserves? Don't OOM, retry. */
  2659. if (sc->memcg_low_skipped) {
  2660. sc->priority = initial_priority;
  2661. sc->memcg_low_reclaim = 1;
  2662. sc->memcg_low_skipped = 0;
  2663. goto retry;
  2664. }
  2665. return 0;
  2666. }
  2667. static bool allow_direct_reclaim(pg_data_t *pgdat)
  2668. {
  2669. struct zone *zone;
  2670. unsigned long pfmemalloc_reserve = 0;
  2671. unsigned long free_pages = 0;
  2672. int i;
  2673. bool wmark_ok;
  2674. if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES)
  2675. return true;
  2676. for (i = 0; i <= ZONE_NORMAL; i++) {
  2677. zone = &pgdat->node_zones[i];
  2678. if (!managed_zone(zone))
  2679. continue;
  2680. if (!zone_reclaimable_pages(zone))
  2681. continue;
  2682. pfmemalloc_reserve += min_wmark_pages(zone);
  2683. free_pages += zone_page_state(zone, NR_FREE_PAGES);
  2684. }
  2685. /* If there are no reserves (unexpected config) then do not throttle */
  2686. if (!pfmemalloc_reserve)
  2687. return true;
  2688. wmark_ok = free_pages > pfmemalloc_reserve / 2;
  2689. /* kswapd must be awake if processes are being throttled */
  2690. if (!wmark_ok && waitqueue_active(&pgdat->kswapd_wait)) {
  2691. pgdat->kswapd_classzone_idx = min(pgdat->kswapd_classzone_idx,
  2692. (enum zone_type)ZONE_NORMAL);
  2693. wake_up_interruptible(&pgdat->kswapd_wait);
  2694. }
  2695. return wmark_ok;
  2696. }
  2697. /*
  2698. * Throttle direct reclaimers if backing storage is backed by the network
  2699. * and the PFMEMALLOC reserve for the preferred node is getting dangerously
  2700. * depleted. kswapd will continue to make progress and wake the processes
  2701. * when the low watermark is reached.
  2702. *
  2703. * Returns true if a fatal signal was delivered during throttling. If this
  2704. * happens, the page allocator should not consider triggering the OOM killer.
  2705. */
  2706. static bool throttle_direct_reclaim(gfp_t gfp_mask, struct zonelist *zonelist,
  2707. nodemask_t *nodemask)
  2708. {
  2709. struct zoneref *z;
  2710. struct zone *zone;
  2711. pg_data_t *pgdat = NULL;
  2712. /*
  2713. * Kernel threads should not be throttled as they may be indirectly
  2714. * responsible for cleaning pages necessary for reclaim to make forward
  2715. * progress. kjournald for example may enter direct reclaim while
  2716. * committing a transaction where throttling it could forcing other
  2717. * processes to block on log_wait_commit().
  2718. */
  2719. if (current->flags & PF_KTHREAD)
  2720. goto out;
  2721. /*
  2722. * If a fatal signal is pending, this process should not throttle.
  2723. * It should return quickly so it can exit and free its memory
  2724. */
  2725. if (fatal_signal_pending(current))
  2726. goto out;
  2727. /*
  2728. * Check if the pfmemalloc reserves are ok by finding the first node
  2729. * with a usable ZONE_NORMAL or lower zone. The expectation is that
  2730. * GFP_KERNEL will be required for allocating network buffers when
  2731. * swapping over the network so ZONE_HIGHMEM is unusable.
  2732. *
  2733. * Throttling is based on the first usable node and throttled processes
  2734. * wait on a queue until kswapd makes progress and wakes them. There
  2735. * is an affinity then between processes waking up and where reclaim
  2736. * progress has been made assuming the process wakes on the same node.
  2737. * More importantly, processes running on remote nodes will not compete
  2738. * for remote pfmemalloc reserves and processes on different nodes
  2739. * should make reasonable progress.
  2740. */
  2741. for_each_zone_zonelist_nodemask(zone, z, zonelist,
  2742. gfp_zone(gfp_mask), nodemask) {
  2743. if (zone_idx(zone) > ZONE_NORMAL)
  2744. continue;
  2745. /* Throttle based on the first usable node */
  2746. pgdat = zone->zone_pgdat;
  2747. if (allow_direct_reclaim(pgdat))
  2748. goto out;
  2749. break;
  2750. }
  2751. /* If no zone was usable by the allocation flags then do not throttle */
  2752. if (!pgdat)
  2753. goto out;
  2754. /* Account for the throttling */
  2755. count_vm_event(PGSCAN_DIRECT_THROTTLE);
  2756. /*
  2757. * If the caller cannot enter the filesystem, it's possible that it
  2758. * is due to the caller holding an FS lock or performing a journal
  2759. * transaction in the case of a filesystem like ext[3|4]. In this case,
  2760. * it is not safe to block on pfmemalloc_wait as kswapd could be
  2761. * blocked waiting on the same lock. Instead, throttle for up to a
  2762. * second before continuing.
  2763. */
  2764. if (!(gfp_mask & __GFP_FS)) {
  2765. wait_event_interruptible_timeout(pgdat->pfmemalloc_wait,
  2766. allow_direct_reclaim(pgdat), HZ);
  2767. goto check_pending;
  2768. }
  2769. /* Throttle until kswapd wakes the process */
  2770. wait_event_killable(zone->zone_pgdat->pfmemalloc_wait,
  2771. allow_direct_reclaim(pgdat));
  2772. check_pending:
  2773. if (fatal_signal_pending(current))
  2774. return true;
  2775. out:
  2776. return false;
  2777. }
  2778. unsigned long try_to_free_pages(struct zonelist *zonelist, int order,
  2779. gfp_t gfp_mask, nodemask_t *nodemask)
  2780. {
  2781. unsigned long nr_reclaimed;
  2782. struct scan_control sc = {
  2783. .nr_to_reclaim = SWAP_CLUSTER_MAX,
  2784. .gfp_mask = current_gfp_context(gfp_mask),
  2785. .reclaim_idx = gfp_zone(gfp_mask),
  2786. .order = order,
  2787. .nodemask = nodemask,
  2788. .priority = DEF_PRIORITY,
  2789. .may_writepage = !laptop_mode,
  2790. .may_unmap = 1,
  2791. .may_swap = 1,
  2792. };
  2793. /*
  2794. * scan_control uses s8 fields for order, priority, and reclaim_idx.
  2795. * Confirm they are large enough for max values.
  2796. */
  2797. BUILD_BUG_ON(MAX_ORDER > S8_MAX);
  2798. BUILD_BUG_ON(DEF_PRIORITY > S8_MAX);
  2799. BUILD_BUG_ON(MAX_NR_ZONES > S8_MAX);
  2800. /*
  2801. * Do not enter reclaim if fatal signal was delivered while throttled.
  2802. * 1 is returned so that the page allocator does not OOM kill at this
  2803. * point.
  2804. */
  2805. if (throttle_direct_reclaim(sc.gfp_mask, zonelist, nodemask))
  2806. return 1;
  2807. trace_mm_vmscan_direct_reclaim_begin(order,
  2808. sc.may_writepage,
  2809. sc.gfp_mask,
  2810. sc.reclaim_idx);
  2811. nr_reclaimed = do_try_to_free_pages(zonelist, &sc);
  2812. trace_mm_vmscan_direct_reclaim_end(nr_reclaimed);
  2813. return nr_reclaimed;
  2814. }
  2815. #ifdef CONFIG_MEMCG
  2816. unsigned long mem_cgroup_shrink_node(struct mem_cgroup *memcg,
  2817. gfp_t gfp_mask, bool noswap,
  2818. pg_data_t *pgdat,
  2819. unsigned long *nr_scanned)
  2820. {
  2821. struct scan_control sc = {
  2822. .nr_to_reclaim = SWAP_CLUSTER_MAX,
  2823. .target_mem_cgroup = memcg,
  2824. .may_writepage = !laptop_mode,
  2825. .may_unmap = 1,
  2826. .reclaim_idx = MAX_NR_ZONES - 1,
  2827. .may_swap = !noswap,
  2828. };
  2829. unsigned long lru_pages;
  2830. sc.gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) |
  2831. (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK);
  2832. trace_mm_vmscan_memcg_softlimit_reclaim_begin(sc.order,
  2833. sc.may_writepage,
  2834. sc.gfp_mask,
  2835. sc.reclaim_idx);
  2836. /*
  2837. * NOTE: Although we can get the priority field, using it
  2838. * here is not a good idea, since it limits the pages we can scan.
  2839. * if we don't reclaim here, the shrink_node from balance_pgdat
  2840. * will pick up pages from other mem cgroup's as well. We hack
  2841. * the priority and make it zero.
  2842. */
  2843. shrink_node_memcg(pgdat, memcg, &sc, &lru_pages);
  2844. trace_mm_vmscan_memcg_softlimit_reclaim_end(sc.nr_reclaimed);
  2845. *nr_scanned = sc.nr_scanned;
  2846. return sc.nr_reclaimed;
  2847. }
  2848. unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg,
  2849. unsigned long nr_pages,
  2850. gfp_t gfp_mask,
  2851. bool may_swap)
  2852. {
  2853. struct zonelist *zonelist;
  2854. unsigned long nr_reclaimed;
  2855. int nid;
  2856. unsigned int noreclaim_flag;
  2857. struct scan_control sc = {
  2858. .nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX),
  2859. .gfp_mask = (current_gfp_context(gfp_mask) & GFP_RECLAIM_MASK) |
  2860. (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK),
  2861. .reclaim_idx = MAX_NR_ZONES - 1,
  2862. .target_mem_cgroup = memcg,
  2863. .priority = DEF_PRIORITY,
  2864. .may_writepage = !laptop_mode,
  2865. .may_unmap = 1,
  2866. .may_swap = may_swap,
  2867. };
  2868. /*
  2869. * Unlike direct reclaim via alloc_pages(), memcg's reclaim doesn't
  2870. * take care of from where we get pages. So the node where we start the
  2871. * scan does not need to be the current node.
  2872. */
  2873. nid = mem_cgroup_select_victim_node(memcg);
  2874. zonelist = &NODE_DATA(nid)->node_zonelists[ZONELIST_FALLBACK];
  2875. trace_mm_vmscan_memcg_reclaim_begin(0,
  2876. sc.may_writepage,
  2877. sc.gfp_mask,
  2878. sc.reclaim_idx);
  2879. noreclaim_flag = memalloc_noreclaim_save();
  2880. nr_reclaimed = do_try_to_free_pages(zonelist, &sc);
  2881. memalloc_noreclaim_restore(noreclaim_flag);
  2882. trace_mm_vmscan_memcg_reclaim_end(nr_reclaimed);
  2883. return nr_reclaimed;
  2884. }
  2885. #endif
  2886. static void age_active_anon(struct pglist_data *pgdat,
  2887. struct scan_control *sc)
  2888. {
  2889. struct mem_cgroup *memcg;
  2890. if (!total_swap_pages)
  2891. return;
  2892. memcg = mem_cgroup_iter(NULL, NULL, NULL);
  2893. do {
  2894. struct lruvec *lruvec = mem_cgroup_lruvec(pgdat, memcg);
  2895. if (inactive_list_is_low(lruvec, false, memcg, sc, true))
  2896. shrink_active_list(SWAP_CLUSTER_MAX, lruvec,
  2897. sc, LRU_ACTIVE_ANON);
  2898. memcg = mem_cgroup_iter(NULL, memcg, NULL);
  2899. } while (memcg);
  2900. }
  2901. /*
  2902. * Returns true if there is an eligible zone balanced for the request order
  2903. * and classzone_idx
  2904. */
  2905. static bool pgdat_balanced(pg_data_t *pgdat, int order, int classzone_idx)
  2906. {
  2907. int i;
  2908. unsigned long mark = -1;
  2909. struct zone *zone;
  2910. for (i = 0; i <= classzone_idx; i++) {
  2911. zone = pgdat->node_zones + i;
  2912. if (!managed_zone(zone))
  2913. continue;
  2914. mark = high_wmark_pages(zone);
  2915. if (zone_watermark_ok_safe(zone, order, mark, classzone_idx))
  2916. return true;
  2917. }
  2918. /*
  2919. * If a node has no populated zone within classzone_idx, it does not
  2920. * need balancing by definition. This can happen if a zone-restricted
  2921. * allocation tries to wake a remote kswapd.
  2922. */
  2923. if (mark == -1)
  2924. return true;
  2925. return false;
  2926. }
  2927. /* Clear pgdat state for congested, dirty or under writeback. */
  2928. static void clear_pgdat_congested(pg_data_t *pgdat)
  2929. {
  2930. clear_bit(PGDAT_CONGESTED, &pgdat->flags);
  2931. clear_bit(PGDAT_DIRTY, &pgdat->flags);
  2932. clear_bit(PGDAT_WRITEBACK, &pgdat->flags);
  2933. }
  2934. /*
  2935. * Prepare kswapd for sleeping. This verifies that there are no processes
  2936. * waiting in throttle_direct_reclaim() and that watermarks have been met.
  2937. *
  2938. * Returns true if kswapd is ready to sleep
  2939. */
  2940. static bool prepare_kswapd_sleep(pg_data_t *pgdat, int order, int classzone_idx)
  2941. {
  2942. /*
  2943. * The throttled processes are normally woken up in balance_pgdat() as
  2944. * soon as allow_direct_reclaim() is true. But there is a potential
  2945. * race between when kswapd checks the watermarks and a process gets
  2946. * throttled. There is also a potential race if processes get
  2947. * throttled, kswapd wakes, a large process exits thereby balancing the
  2948. * zones, which causes kswapd to exit balance_pgdat() before reaching
  2949. * the wake up checks. If kswapd is going to sleep, no process should
  2950. * be sleeping on pfmemalloc_wait, so wake them now if necessary. If
  2951. * the wake up is premature, processes will wake kswapd and get
  2952. * throttled again. The difference from wake ups in balance_pgdat() is
  2953. * that here we are under prepare_to_wait().
  2954. */
  2955. if (waitqueue_active(&pgdat->pfmemalloc_wait))
  2956. wake_up_all(&pgdat->pfmemalloc_wait);
  2957. /* Hopeless node, leave it to direct reclaim */
  2958. if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES)
  2959. return true;
  2960. if (pgdat_balanced(pgdat, order, classzone_idx)) {
  2961. clear_pgdat_congested(pgdat);
  2962. return true;
  2963. }
  2964. return false;
  2965. }
  2966. /*
  2967. * kswapd shrinks a node of pages that are at or below the highest usable
  2968. * zone that is currently unbalanced.
  2969. *
  2970. * Returns true if kswapd scanned at least the requested number of pages to
  2971. * reclaim or if the lack of progress was due to pages under writeback.
  2972. * This is used to determine if the scanning priority needs to be raised.
  2973. */
  2974. static bool kswapd_shrink_node(pg_data_t *pgdat,
  2975. struct scan_control *sc)
  2976. {
  2977. struct zone *zone;
  2978. int z;
  2979. /* Reclaim a number of pages proportional to the number of zones */
  2980. sc->nr_to_reclaim = 0;
  2981. for (z = 0; z <= sc->reclaim_idx; z++) {
  2982. zone = pgdat->node_zones + z;
  2983. if (!managed_zone(zone))
  2984. continue;
  2985. sc->nr_to_reclaim += max(high_wmark_pages(zone), SWAP_CLUSTER_MAX);
  2986. }
  2987. /*
  2988. * Historically care was taken to put equal pressure on all zones but
  2989. * now pressure is applied based on node LRU order.
  2990. */
  2991. shrink_node(pgdat, sc);
  2992. /*
  2993. * Fragmentation may mean that the system cannot be rebalanced for
  2994. * high-order allocations. If twice the allocation size has been
  2995. * reclaimed then recheck watermarks only at order-0 to prevent
  2996. * excessive reclaim. Assume that a process requested a high-order
  2997. * can direct reclaim/compact.
  2998. */
  2999. if (sc->order && sc->nr_reclaimed >= compact_gap(sc->order))
  3000. sc->order = 0;
  3001. return sc->nr_scanned >= sc->nr_to_reclaim;
  3002. }
  3003. /*
  3004. * For kswapd, balance_pgdat() will reclaim pages across a node from zones
  3005. * that are eligible for use by the caller until at least one zone is
  3006. * balanced.
  3007. *
  3008. * Returns the order kswapd finished reclaiming at.
  3009. *
  3010. * kswapd scans the zones in the highmem->normal->dma direction. It skips
  3011. * zones which have free_pages > high_wmark_pages(zone), but once a zone is
  3012. * found to have free_pages <= high_wmark_pages(zone), any page is that zone
  3013. * or lower is eligible for reclaim until at least one usable zone is
  3014. * balanced.
  3015. */
  3016. static int balance_pgdat(pg_data_t *pgdat, int order, int classzone_idx)
  3017. {
  3018. int i;
  3019. unsigned long nr_soft_reclaimed;
  3020. unsigned long nr_soft_scanned;
  3021. struct zone *zone;
  3022. struct scan_control sc = {
  3023. .gfp_mask = GFP_KERNEL,
  3024. .order = order,
  3025. .priority = DEF_PRIORITY,
  3026. .may_writepage = !laptop_mode,
  3027. .may_unmap = 1,
  3028. .may_swap = 1,
  3029. };
  3030. __fs_reclaim_acquire();
  3031. count_vm_event(PAGEOUTRUN);
  3032. do {
  3033. unsigned long nr_reclaimed = sc.nr_reclaimed;
  3034. bool raise_priority = true;
  3035. bool ret;
  3036. sc.reclaim_idx = classzone_idx;
  3037. /*
  3038. * If the number of buffer_heads exceeds the maximum allowed
  3039. * then consider reclaiming from all zones. This has a dual
  3040. * purpose -- on 64-bit systems it is expected that
  3041. * buffer_heads are stripped during active rotation. On 32-bit
  3042. * systems, highmem pages can pin lowmem memory and shrinking
  3043. * buffers can relieve lowmem pressure. Reclaim may still not
  3044. * go ahead if all eligible zones for the original allocation
  3045. * request are balanced to avoid excessive reclaim from kswapd.
  3046. */
  3047. if (buffer_heads_over_limit) {
  3048. for (i = MAX_NR_ZONES - 1; i >= 0; i--) {
  3049. zone = pgdat->node_zones + i;
  3050. if (!managed_zone(zone))
  3051. continue;
  3052. sc.reclaim_idx = i;
  3053. break;
  3054. }
  3055. }
  3056. /*
  3057. * Only reclaim if there are no eligible zones. Note that
  3058. * sc.reclaim_idx is not used as buffer_heads_over_limit may
  3059. * have adjusted it.
  3060. */
  3061. if (pgdat_balanced(pgdat, sc.order, classzone_idx))
  3062. goto out;
  3063. /*
  3064. * Do some background aging of the anon list, to give
  3065. * pages a chance to be referenced before reclaiming. All
  3066. * pages are rotated regardless of classzone as this is
  3067. * about consistent aging.
  3068. */
  3069. age_active_anon(pgdat, &sc);
  3070. /*
  3071. * If we're getting trouble reclaiming, start doing writepage
  3072. * even in laptop mode.
  3073. */
  3074. if (sc.priority < DEF_PRIORITY - 2)
  3075. sc.may_writepage = 1;
  3076. /* Call soft limit reclaim before calling shrink_node. */
  3077. sc.nr_scanned = 0;
  3078. nr_soft_scanned = 0;
  3079. nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(pgdat, sc.order,
  3080. sc.gfp_mask, &nr_soft_scanned);
  3081. sc.nr_reclaimed += nr_soft_reclaimed;
  3082. /*
  3083. * There should be no need to raise the scanning priority if
  3084. * enough pages are already being scanned that that high
  3085. * watermark would be met at 100% efficiency.
  3086. */
  3087. if (kswapd_shrink_node(pgdat, &sc))
  3088. raise_priority = false;
  3089. /*
  3090. * If the low watermark is met there is no need for processes
  3091. * to be throttled on pfmemalloc_wait as they should not be
  3092. * able to safely make forward progress. Wake them
  3093. */
  3094. if (waitqueue_active(&pgdat->pfmemalloc_wait) &&
  3095. allow_direct_reclaim(pgdat))
  3096. wake_up_all(&pgdat->pfmemalloc_wait);
  3097. /* Check if kswapd should be suspending */
  3098. __fs_reclaim_release();
  3099. ret = try_to_freeze();
  3100. __fs_reclaim_acquire();
  3101. if (ret || kthread_should_stop())
  3102. break;
  3103. /*
  3104. * Raise priority if scanning rate is too low or there was no
  3105. * progress in reclaiming pages
  3106. */
  3107. nr_reclaimed = sc.nr_reclaimed - nr_reclaimed;
  3108. if (raise_priority || !nr_reclaimed)
  3109. sc.priority--;
  3110. } while (sc.priority >= 1);
  3111. if (!sc.nr_reclaimed)
  3112. pgdat->kswapd_failures++;
  3113. out:
  3114. snapshot_refaults(NULL, pgdat);
  3115. __fs_reclaim_release();
  3116. /*
  3117. * Return the order kswapd stopped reclaiming at as
  3118. * prepare_kswapd_sleep() takes it into account. If another caller
  3119. * entered the allocator slow path while kswapd was awake, order will
  3120. * remain at the higher level.
  3121. */
  3122. return sc.order;
  3123. }
  3124. /*
  3125. * pgdat->kswapd_classzone_idx is the highest zone index that a recent
  3126. * allocation request woke kswapd for. When kswapd has not woken recently,
  3127. * the value is MAX_NR_ZONES which is not a valid index. This compares a
  3128. * given classzone and returns it or the highest classzone index kswapd
  3129. * was recently woke for.
  3130. */
  3131. static enum zone_type kswapd_classzone_idx(pg_data_t *pgdat,
  3132. enum zone_type classzone_idx)
  3133. {
  3134. if (pgdat->kswapd_classzone_idx == MAX_NR_ZONES)
  3135. return classzone_idx;
  3136. return max(pgdat->kswapd_classzone_idx, classzone_idx);
  3137. }
  3138. static void kswapd_try_to_sleep(pg_data_t *pgdat, int alloc_order, int reclaim_order,
  3139. unsigned int classzone_idx)
  3140. {
  3141. long remaining = 0;
  3142. DEFINE_WAIT(wait);
  3143. if (freezing(current) || kthread_should_stop())
  3144. return;
  3145. prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
  3146. /*
  3147. * Try to sleep for a short interval. Note that kcompactd will only be
  3148. * woken if it is possible to sleep for a short interval. This is
  3149. * deliberate on the assumption that if reclaim cannot keep an
  3150. * eligible zone balanced that it's also unlikely that compaction will
  3151. * succeed.
  3152. */
  3153. if (prepare_kswapd_sleep(pgdat, reclaim_order, classzone_idx)) {
  3154. /*
  3155. * Compaction records what page blocks it recently failed to
  3156. * isolate pages from and skips them in the future scanning.
  3157. * When kswapd is going to sleep, it is reasonable to assume
  3158. * that pages and compaction may succeed so reset the cache.
  3159. */
  3160. reset_isolation_suitable(pgdat);
  3161. /*
  3162. * We have freed the memory, now we should compact it to make
  3163. * allocation of the requested order possible.
  3164. */
  3165. wakeup_kcompactd(pgdat, alloc_order, classzone_idx);
  3166. remaining = schedule_timeout(HZ/10);
  3167. /*
  3168. * If woken prematurely then reset kswapd_classzone_idx and
  3169. * order. The values will either be from a wakeup request or
  3170. * the previous request that slept prematurely.
  3171. */
  3172. if (remaining) {
  3173. pgdat->kswapd_classzone_idx = kswapd_classzone_idx(pgdat, classzone_idx);
  3174. pgdat->kswapd_order = max(pgdat->kswapd_order, reclaim_order);
  3175. }
  3176. finish_wait(&pgdat->kswapd_wait, &wait);
  3177. prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
  3178. }
  3179. /*
  3180. * After a short sleep, check if it was a premature sleep. If not, then
  3181. * go fully to sleep until explicitly woken up.
  3182. */
  3183. if (!remaining &&
  3184. prepare_kswapd_sleep(pgdat, reclaim_order, classzone_idx)) {
  3185. trace_mm_vmscan_kswapd_sleep(pgdat->node_id);
  3186. /*
  3187. * vmstat counters are not perfectly accurate and the estimated
  3188. * value for counters such as NR_FREE_PAGES can deviate from the
  3189. * true value by nr_online_cpus * threshold. To avoid the zone
  3190. * watermarks being breached while under pressure, we reduce the
  3191. * per-cpu vmstat threshold while kswapd is awake and restore
  3192. * them before going back to sleep.
  3193. */
  3194. set_pgdat_percpu_threshold(pgdat, calculate_normal_threshold);
  3195. if (!kthread_should_stop())
  3196. schedule();
  3197. set_pgdat_percpu_threshold(pgdat, calculate_pressure_threshold);
  3198. } else {
  3199. if (remaining)
  3200. count_vm_event(KSWAPD_LOW_WMARK_HIT_QUICKLY);
  3201. else
  3202. count_vm_event(KSWAPD_HIGH_WMARK_HIT_QUICKLY);
  3203. }
  3204. finish_wait(&pgdat->kswapd_wait, &wait);
  3205. }
  3206. /*
  3207. * The background pageout daemon, started as a kernel thread
  3208. * from the init process.
  3209. *
  3210. * This basically trickles out pages so that we have _some_
  3211. * free memory available even if there is no other activity
  3212. * that frees anything up. This is needed for things like routing
  3213. * etc, where we otherwise might have all activity going on in
  3214. * asynchronous contexts that cannot page things out.
  3215. *
  3216. * If there are applications that are active memory-allocators
  3217. * (most normal use), this basically shouldn't matter.
  3218. */
  3219. static int kswapd(void *p)
  3220. {
  3221. unsigned int alloc_order, reclaim_order;
  3222. unsigned int classzone_idx = MAX_NR_ZONES - 1;
  3223. pg_data_t *pgdat = (pg_data_t*)p;
  3224. struct task_struct *tsk = current;
  3225. struct reclaim_state reclaim_state = {
  3226. .reclaimed_slab = 0,
  3227. };
  3228. const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id);
  3229. if (!cpumask_empty(cpumask))
  3230. set_cpus_allowed_ptr(tsk, cpumask);
  3231. current->reclaim_state = &reclaim_state;
  3232. /*
  3233. * Tell the memory management that we're a "memory allocator",
  3234. * and that if we need more memory we should get access to it
  3235. * regardless (see "__alloc_pages()"). "kswapd" should
  3236. * never get caught in the normal page freeing logic.
  3237. *
  3238. * (Kswapd normally doesn't need memory anyway, but sometimes
  3239. * you need a small amount of memory in order to be able to
  3240. * page out something else, and this flag essentially protects
  3241. * us from recursively trying to free more memory as we're
  3242. * trying to free the first piece of memory in the first place).
  3243. */
  3244. tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD;
  3245. set_freezable();
  3246. pgdat->kswapd_order = 0;
  3247. pgdat->kswapd_classzone_idx = MAX_NR_ZONES;
  3248. for ( ; ; ) {
  3249. bool ret;
  3250. alloc_order = reclaim_order = pgdat->kswapd_order;
  3251. classzone_idx = kswapd_classzone_idx(pgdat, classzone_idx);
  3252. kswapd_try_sleep:
  3253. kswapd_try_to_sleep(pgdat, alloc_order, reclaim_order,
  3254. classzone_idx);
  3255. /* Read the new order and classzone_idx */
  3256. alloc_order = reclaim_order = pgdat->kswapd_order;
  3257. classzone_idx = kswapd_classzone_idx(pgdat, 0);
  3258. pgdat->kswapd_order = 0;
  3259. pgdat->kswapd_classzone_idx = MAX_NR_ZONES;
  3260. ret = try_to_freeze();
  3261. if (kthread_should_stop())
  3262. break;
  3263. /*
  3264. * We can speed up thawing tasks if we don't call balance_pgdat
  3265. * after returning from the refrigerator
  3266. */
  3267. if (ret)
  3268. continue;
  3269. /*
  3270. * Reclaim begins at the requested order but if a high-order
  3271. * reclaim fails then kswapd falls back to reclaiming for
  3272. * order-0. If that happens, kswapd will consider sleeping
  3273. * for the order it finished reclaiming at (reclaim_order)
  3274. * but kcompactd is woken to compact for the original
  3275. * request (alloc_order).
  3276. */
  3277. trace_mm_vmscan_kswapd_wake(pgdat->node_id, classzone_idx,
  3278. alloc_order);
  3279. reclaim_order = balance_pgdat(pgdat, alloc_order, classzone_idx);
  3280. if (reclaim_order < alloc_order)
  3281. goto kswapd_try_sleep;
  3282. }
  3283. tsk->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD);
  3284. current->reclaim_state = NULL;
  3285. return 0;
  3286. }
  3287. /*
  3288. * A zone is low on free memory or too fragmented for high-order memory. If
  3289. * kswapd should reclaim (direct reclaim is deferred), wake it up for the zone's
  3290. * pgdat. It will wake up kcompactd after reclaiming memory. If kswapd reclaim
  3291. * has failed or is not needed, still wake up kcompactd if only compaction is
  3292. * needed.
  3293. */
  3294. void wakeup_kswapd(struct zone *zone, gfp_t gfp_flags, int order,
  3295. enum zone_type classzone_idx)
  3296. {
  3297. pg_data_t *pgdat;
  3298. if (!managed_zone(zone))
  3299. return;
  3300. if (!cpuset_zone_allowed(zone, gfp_flags))
  3301. return;
  3302. pgdat = zone->zone_pgdat;
  3303. pgdat->kswapd_classzone_idx = kswapd_classzone_idx(pgdat,
  3304. classzone_idx);
  3305. pgdat->kswapd_order = max(pgdat->kswapd_order, order);
  3306. if (!waitqueue_active(&pgdat->kswapd_wait))
  3307. return;
  3308. /* Hopeless node, leave it to direct reclaim if possible */
  3309. if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES ||
  3310. pgdat_balanced(pgdat, order, classzone_idx)) {
  3311. /*
  3312. * There may be plenty of free memory available, but it's too
  3313. * fragmented for high-order allocations. Wake up kcompactd
  3314. * and rely on compaction_suitable() to determine if it's
  3315. * needed. If it fails, it will defer subsequent attempts to
  3316. * ratelimit its work.
  3317. */
  3318. if (!(gfp_flags & __GFP_DIRECT_RECLAIM))
  3319. wakeup_kcompactd(pgdat, order, classzone_idx);
  3320. return;
  3321. }
  3322. trace_mm_vmscan_wakeup_kswapd(pgdat->node_id, classzone_idx, order,
  3323. gfp_flags);
  3324. wake_up_interruptible(&pgdat->kswapd_wait);
  3325. }
  3326. #ifdef CONFIG_HIBERNATION
  3327. /*
  3328. * Try to free `nr_to_reclaim' of memory, system-wide, and return the number of
  3329. * freed pages.
  3330. *
  3331. * Rather than trying to age LRUs the aim is to preserve the overall
  3332. * LRU order by reclaiming preferentially
  3333. * inactive > active > active referenced > active mapped
  3334. */
  3335. unsigned long shrink_all_memory(unsigned long nr_to_reclaim)
  3336. {
  3337. struct reclaim_state reclaim_state;
  3338. struct scan_control sc = {
  3339. .nr_to_reclaim = nr_to_reclaim,
  3340. .gfp_mask = GFP_HIGHUSER_MOVABLE,
  3341. .reclaim_idx = MAX_NR_ZONES - 1,
  3342. .priority = DEF_PRIORITY,
  3343. .may_writepage = 1,
  3344. .may_unmap = 1,
  3345. .may_swap = 1,
  3346. .hibernation_mode = 1,
  3347. };
  3348. struct zonelist *zonelist = node_zonelist(numa_node_id(), sc.gfp_mask);
  3349. struct task_struct *p = current;
  3350. unsigned long nr_reclaimed;
  3351. unsigned int noreclaim_flag;
  3352. fs_reclaim_acquire(sc.gfp_mask);
  3353. noreclaim_flag = memalloc_noreclaim_save();
  3354. reclaim_state.reclaimed_slab = 0;
  3355. p->reclaim_state = &reclaim_state;
  3356. nr_reclaimed = do_try_to_free_pages(zonelist, &sc);
  3357. p->reclaim_state = NULL;
  3358. memalloc_noreclaim_restore(noreclaim_flag);
  3359. fs_reclaim_release(sc.gfp_mask);
  3360. return nr_reclaimed;
  3361. }
  3362. #endif /* CONFIG_HIBERNATION */
  3363. /* It's optimal to keep kswapds on the same CPUs as their memory, but
  3364. not required for correctness. So if the last cpu in a node goes
  3365. away, we get changed to run anywhere: as the first one comes back,
  3366. restore their cpu bindings. */
  3367. static int kswapd_cpu_online(unsigned int cpu)
  3368. {
  3369. int nid;
  3370. for_each_node_state(nid, N_MEMORY) {
  3371. pg_data_t *pgdat = NODE_DATA(nid);
  3372. const struct cpumask *mask;
  3373. mask = cpumask_of_node(pgdat->node_id);
  3374. if (cpumask_any_and(cpu_online_mask, mask) < nr_cpu_ids)
  3375. /* One of our CPUs online: restore mask */
  3376. set_cpus_allowed_ptr(pgdat->kswapd, mask);
  3377. }
  3378. return 0;
  3379. }
  3380. /*
  3381. * This kswapd start function will be called by init and node-hot-add.
  3382. * On node-hot-add, kswapd will moved to proper cpus if cpus are hot-added.
  3383. */
  3384. int kswapd_run(int nid)
  3385. {
  3386. pg_data_t *pgdat = NODE_DATA(nid);
  3387. int ret = 0;
  3388. if (pgdat->kswapd)
  3389. return 0;
  3390. pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid);
  3391. if (IS_ERR(pgdat->kswapd)) {
  3392. /* failure at boot is fatal */
  3393. BUG_ON(system_state < SYSTEM_RUNNING);
  3394. pr_err("Failed to start kswapd on node %d\n", nid);
  3395. ret = PTR_ERR(pgdat->kswapd);
  3396. pgdat->kswapd = NULL;
  3397. }
  3398. return ret;
  3399. }
  3400. /*
  3401. * Called by memory hotplug when all memory in a node is offlined. Caller must
  3402. * hold mem_hotplug_begin/end().
  3403. */
  3404. void kswapd_stop(int nid)
  3405. {
  3406. struct task_struct *kswapd = NODE_DATA(nid)->kswapd;
  3407. if (kswapd) {
  3408. kthread_stop(kswapd);
  3409. NODE_DATA(nid)->kswapd = NULL;
  3410. }
  3411. }
  3412. static int __init kswapd_init(void)
  3413. {
  3414. int nid, ret;
  3415. swap_setup();
  3416. for_each_node_state(nid, N_MEMORY)
  3417. kswapd_run(nid);
  3418. ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
  3419. "mm/vmscan:online", kswapd_cpu_online,
  3420. NULL);
  3421. WARN_ON(ret < 0);
  3422. return 0;
  3423. }
  3424. module_init(kswapd_init)
  3425. #ifdef CONFIG_NUMA
  3426. /*
  3427. * Node reclaim mode
  3428. *
  3429. * If non-zero call node_reclaim when the number of free pages falls below
  3430. * the watermarks.
  3431. */
  3432. int node_reclaim_mode __read_mostly;
  3433. #define RECLAIM_OFF 0
  3434. #define RECLAIM_ZONE (1<<0) /* Run shrink_inactive_list on the zone */
  3435. #define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */
  3436. #define RECLAIM_UNMAP (1<<2) /* Unmap pages during reclaim */
  3437. /*
  3438. * Priority for NODE_RECLAIM. This determines the fraction of pages
  3439. * of a node considered for each zone_reclaim. 4 scans 1/16th of
  3440. * a zone.
  3441. */
  3442. #define NODE_RECLAIM_PRIORITY 4
  3443. /*
  3444. * Percentage of pages in a zone that must be unmapped for node_reclaim to
  3445. * occur.
  3446. */
  3447. int sysctl_min_unmapped_ratio = 1;
  3448. /*
  3449. * If the number of slab pages in a zone grows beyond this percentage then
  3450. * slab reclaim needs to occur.
  3451. */
  3452. int sysctl_min_slab_ratio = 5;
  3453. static inline unsigned long node_unmapped_file_pages(struct pglist_data *pgdat)
  3454. {
  3455. unsigned long file_mapped = node_page_state(pgdat, NR_FILE_MAPPED);
  3456. unsigned long file_lru = node_page_state(pgdat, NR_INACTIVE_FILE) +
  3457. node_page_state(pgdat, NR_ACTIVE_FILE);
  3458. /*
  3459. * It's possible for there to be more file mapped pages than
  3460. * accounted for by the pages on the file LRU lists because
  3461. * tmpfs pages accounted for as ANON can also be FILE_MAPPED
  3462. */
  3463. return (file_lru > file_mapped) ? (file_lru - file_mapped) : 0;
  3464. }
  3465. /* Work out how many page cache pages we can reclaim in this reclaim_mode */
  3466. static unsigned long node_pagecache_reclaimable(struct pglist_data *pgdat)
  3467. {
  3468. unsigned long nr_pagecache_reclaimable;
  3469. unsigned long delta = 0;
  3470. /*
  3471. * If RECLAIM_UNMAP is set, then all file pages are considered
  3472. * potentially reclaimable. Otherwise, we have to worry about
  3473. * pages like swapcache and node_unmapped_file_pages() provides
  3474. * a better estimate
  3475. */
  3476. if (node_reclaim_mode & RECLAIM_UNMAP)
  3477. nr_pagecache_reclaimable = node_page_state(pgdat, NR_FILE_PAGES);
  3478. else
  3479. nr_pagecache_reclaimable = node_unmapped_file_pages(pgdat);
  3480. /* If we can't clean pages, remove dirty pages from consideration */
  3481. if (!(node_reclaim_mode & RECLAIM_WRITE))
  3482. delta += node_page_state(pgdat, NR_FILE_DIRTY);
  3483. /* Watch for any possible underflows due to delta */
  3484. if (unlikely(delta > nr_pagecache_reclaimable))
  3485. delta = nr_pagecache_reclaimable;
  3486. return nr_pagecache_reclaimable - delta;
  3487. }
  3488. /*
  3489. * Try to free up some pages from this node through reclaim.
  3490. */
  3491. static int __node_reclaim(struct pglist_data *pgdat, gfp_t gfp_mask, unsigned int order)
  3492. {
  3493. /* Minimum pages needed in order to stay on node */
  3494. const unsigned long nr_pages = 1 << order;
  3495. struct task_struct *p = current;
  3496. struct reclaim_state reclaim_state;
  3497. unsigned int noreclaim_flag;
  3498. struct scan_control sc = {
  3499. .nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX),
  3500. .gfp_mask = current_gfp_context(gfp_mask),
  3501. .order = order,
  3502. .priority = NODE_RECLAIM_PRIORITY,
  3503. .may_writepage = !!(node_reclaim_mode & RECLAIM_WRITE),
  3504. .may_unmap = !!(node_reclaim_mode & RECLAIM_UNMAP),
  3505. .may_swap = 1,
  3506. .reclaim_idx = gfp_zone(gfp_mask),
  3507. };
  3508. cond_resched();
  3509. fs_reclaim_acquire(sc.gfp_mask);
  3510. /*
  3511. * We need to be able to allocate from the reserves for RECLAIM_UNMAP
  3512. * and we also need to be able to write out pages for RECLAIM_WRITE
  3513. * and RECLAIM_UNMAP.
  3514. */
  3515. noreclaim_flag = memalloc_noreclaim_save();
  3516. p->flags |= PF_SWAPWRITE;
  3517. reclaim_state.reclaimed_slab = 0;
  3518. p->reclaim_state = &reclaim_state;
  3519. if (node_pagecache_reclaimable(pgdat) > pgdat->min_unmapped_pages) {
  3520. /*
  3521. * Free memory by calling shrink node with increasing
  3522. * priorities until we have enough memory freed.
  3523. */
  3524. do {
  3525. shrink_node(pgdat, &sc);
  3526. } while (sc.nr_reclaimed < nr_pages && --sc.priority >= 0);
  3527. }
  3528. p->reclaim_state = NULL;
  3529. current->flags &= ~PF_SWAPWRITE;
  3530. memalloc_noreclaim_restore(noreclaim_flag);
  3531. fs_reclaim_release(sc.gfp_mask);
  3532. return sc.nr_reclaimed >= nr_pages;
  3533. }
  3534. int node_reclaim(struct pglist_data *pgdat, gfp_t gfp_mask, unsigned int order)
  3535. {
  3536. int ret;
  3537. /*
  3538. * Node reclaim reclaims unmapped file backed pages and
  3539. * slab pages if we are over the defined limits.
  3540. *
  3541. * A small portion of unmapped file backed pages is needed for
  3542. * file I/O otherwise pages read by file I/O will be immediately
  3543. * thrown out if the node is overallocated. So we do not reclaim
  3544. * if less than a specified percentage of the node is used by
  3545. * unmapped file backed pages.
  3546. */
  3547. if (node_pagecache_reclaimable(pgdat) <= pgdat->min_unmapped_pages &&
  3548. node_page_state(pgdat, NR_SLAB_RECLAIMABLE) <= pgdat->min_slab_pages)
  3549. return NODE_RECLAIM_FULL;
  3550. /*
  3551. * Do not scan if the allocation should not be delayed.
  3552. */
  3553. if (!gfpflags_allow_blocking(gfp_mask) || (current->flags & PF_MEMALLOC))
  3554. return NODE_RECLAIM_NOSCAN;
  3555. /*
  3556. * Only run node reclaim on the local node or on nodes that do not
  3557. * have associated processors. This will favor the local processor
  3558. * over remote processors and spread off node memory allocations
  3559. * as wide as possible.
  3560. */
  3561. if (node_state(pgdat->node_id, N_CPU) && pgdat->node_id != numa_node_id())
  3562. return NODE_RECLAIM_NOSCAN;
  3563. if (test_and_set_bit(PGDAT_RECLAIM_LOCKED, &pgdat->flags))
  3564. return NODE_RECLAIM_NOSCAN;
  3565. ret = __node_reclaim(pgdat, gfp_mask, order);
  3566. clear_bit(PGDAT_RECLAIM_LOCKED, &pgdat->flags);
  3567. if (!ret)
  3568. count_vm_event(PGSCAN_ZONE_RECLAIM_FAILED);
  3569. return ret;
  3570. }
  3571. #endif
  3572. /*
  3573. * page_evictable - test whether a page is evictable
  3574. * @page: the page to test
  3575. *
  3576. * Test whether page is evictable--i.e., should be placed on active/inactive
  3577. * lists vs unevictable list.
  3578. *
  3579. * Reasons page might not be evictable:
  3580. * (1) page's mapping marked unevictable
  3581. * (2) page is part of an mlocked VMA
  3582. *
  3583. */
  3584. int page_evictable(struct page *page)
  3585. {
  3586. int ret;
  3587. /* Prevent address_space of inode and swap cache from being freed */
  3588. rcu_read_lock();
  3589. ret = !mapping_unevictable(page_mapping(page)) && !PageMlocked(page);
  3590. rcu_read_unlock();
  3591. return ret;
  3592. }
  3593. #ifdef CONFIG_SHMEM
  3594. /**
  3595. * check_move_unevictable_pages - check pages for evictability and move to appropriate zone lru list
  3596. * @pages: array of pages to check
  3597. * @nr_pages: number of pages to check
  3598. *
  3599. * Checks pages for evictability and moves them to the appropriate lru list.
  3600. *
  3601. * This function is only used for SysV IPC SHM_UNLOCK.
  3602. */
  3603. void check_move_unevictable_pages(struct page **pages, int nr_pages)
  3604. {
  3605. struct lruvec *lruvec;
  3606. struct pglist_data *pgdat = NULL;
  3607. int pgscanned = 0;
  3608. int pgrescued = 0;
  3609. int i;
  3610. for (i = 0; i < nr_pages; i++) {
  3611. struct page *page = pages[i];
  3612. struct pglist_data *pagepgdat = page_pgdat(page);
  3613. pgscanned++;
  3614. if (pagepgdat != pgdat) {
  3615. if (pgdat)
  3616. spin_unlock_irq(&pgdat->lru_lock);
  3617. pgdat = pagepgdat;
  3618. spin_lock_irq(&pgdat->lru_lock);
  3619. }
  3620. lruvec = mem_cgroup_page_lruvec(page, pgdat);
  3621. if (!PageLRU(page) || !PageUnevictable(page))
  3622. continue;
  3623. if (page_evictable(page)) {
  3624. enum lru_list lru = page_lru_base_type(page);
  3625. VM_BUG_ON_PAGE(PageActive(page), page);
  3626. ClearPageUnevictable(page);
  3627. del_page_from_lru_list(page, lruvec, LRU_UNEVICTABLE);
  3628. add_page_to_lru_list(page, lruvec, lru);
  3629. pgrescued++;
  3630. }
  3631. }
  3632. if (pgdat) {
  3633. __count_vm_events(UNEVICTABLE_PGRESCUED, pgrescued);
  3634. __count_vm_events(UNEVICTABLE_PGSCANNED, pgscanned);
  3635. spin_unlock_irq(&pgdat->lru_lock);
  3636. }
  3637. }
  3638. #endif /* CONFIG_SHMEM */