vmscan.c 107 KB

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