vmscan.c 95 KB

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