vmscan.c 106 KB

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