vmscan.c 106 KB

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