vmscan.c 93 KB

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