vmscan.c 93 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346
  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. VM_BUG_ON(!page_count(page));
  1061. if (PageLRU(page)) {
  1062. struct zone *zone = page_zone(page);
  1063. spin_lock_irq(&zone->lru_lock);
  1064. if (PageLRU(page)) {
  1065. int lru = page_lru(page);
  1066. ret = 0;
  1067. get_page(page);
  1068. ClearPageLRU(page);
  1069. del_page_from_lru_list(zone, page, lru);
  1070. }
  1071. spin_unlock_irq(&zone->lru_lock);
  1072. }
  1073. return ret;
  1074. }
  1075. /*
  1076. * Are there way too many processes in the direct reclaim path already?
  1077. */
  1078. static int too_many_isolated(struct zone *zone, int file,
  1079. struct scan_control *sc)
  1080. {
  1081. unsigned long inactive, isolated;
  1082. if (current_is_kswapd())
  1083. return 0;
  1084. if (!scanning_global_lru(sc))
  1085. return 0;
  1086. if (file) {
  1087. inactive = zone_page_state(zone, NR_INACTIVE_FILE);
  1088. isolated = zone_page_state(zone, NR_ISOLATED_FILE);
  1089. } else {
  1090. inactive = zone_page_state(zone, NR_INACTIVE_ANON);
  1091. isolated = zone_page_state(zone, NR_ISOLATED_ANON);
  1092. }
  1093. return isolated > inactive;
  1094. }
  1095. /*
  1096. * TODO: Try merging with migrations version of putback_lru_pages
  1097. */
  1098. static noinline_for_stack void
  1099. putback_lru_pages(struct zone *zone, struct scan_control *sc,
  1100. unsigned long nr_anon, unsigned long nr_file,
  1101. struct list_head *page_list)
  1102. {
  1103. struct page *page;
  1104. struct pagevec pvec;
  1105. struct zone_reclaim_stat *reclaim_stat = get_reclaim_stat(zone, sc);
  1106. pagevec_init(&pvec, 1);
  1107. /*
  1108. * Put back any unfreeable pages.
  1109. */
  1110. spin_lock(&zone->lru_lock);
  1111. while (!list_empty(page_list)) {
  1112. int lru;
  1113. page = lru_to_page(page_list);
  1114. VM_BUG_ON(PageLRU(page));
  1115. list_del(&page->lru);
  1116. if (unlikely(!page_evictable(page, NULL))) {
  1117. spin_unlock_irq(&zone->lru_lock);
  1118. putback_lru_page(page);
  1119. spin_lock_irq(&zone->lru_lock);
  1120. continue;
  1121. }
  1122. SetPageLRU(page);
  1123. lru = page_lru(page);
  1124. add_page_to_lru_list(zone, page, lru);
  1125. if (is_active_lru(lru)) {
  1126. int file = is_file_lru(lru);
  1127. int numpages = hpage_nr_pages(page);
  1128. reclaim_stat->recent_rotated[file] += numpages;
  1129. }
  1130. if (!pagevec_add(&pvec, page)) {
  1131. spin_unlock_irq(&zone->lru_lock);
  1132. __pagevec_release(&pvec);
  1133. spin_lock_irq(&zone->lru_lock);
  1134. }
  1135. }
  1136. __mod_zone_page_state(zone, NR_ISOLATED_ANON, -nr_anon);
  1137. __mod_zone_page_state(zone, NR_ISOLATED_FILE, -nr_file);
  1138. spin_unlock_irq(&zone->lru_lock);
  1139. pagevec_release(&pvec);
  1140. }
  1141. static noinline_for_stack void update_isolated_counts(struct zone *zone,
  1142. struct scan_control *sc,
  1143. unsigned long *nr_anon,
  1144. unsigned long *nr_file,
  1145. struct list_head *isolated_list)
  1146. {
  1147. unsigned long nr_active;
  1148. unsigned int count[NR_LRU_LISTS] = { 0, };
  1149. struct zone_reclaim_stat *reclaim_stat = get_reclaim_stat(zone, sc);
  1150. nr_active = clear_active_flags(isolated_list, count);
  1151. __count_vm_events(PGDEACTIVATE, nr_active);
  1152. __mod_zone_page_state(zone, NR_ACTIVE_FILE,
  1153. -count[LRU_ACTIVE_FILE]);
  1154. __mod_zone_page_state(zone, NR_INACTIVE_FILE,
  1155. -count[LRU_INACTIVE_FILE]);
  1156. __mod_zone_page_state(zone, NR_ACTIVE_ANON,
  1157. -count[LRU_ACTIVE_ANON]);
  1158. __mod_zone_page_state(zone, NR_INACTIVE_ANON,
  1159. -count[LRU_INACTIVE_ANON]);
  1160. *nr_anon = count[LRU_ACTIVE_ANON] + count[LRU_INACTIVE_ANON];
  1161. *nr_file = count[LRU_ACTIVE_FILE] + count[LRU_INACTIVE_FILE];
  1162. __mod_zone_page_state(zone, NR_ISOLATED_ANON, *nr_anon);
  1163. __mod_zone_page_state(zone, NR_ISOLATED_FILE, *nr_file);
  1164. reclaim_stat->recent_scanned[0] += *nr_anon;
  1165. reclaim_stat->recent_scanned[1] += *nr_file;
  1166. }
  1167. /*
  1168. * Returns true if the caller should wait to clean dirty/writeback pages.
  1169. *
  1170. * If we are direct reclaiming for contiguous pages and we do not reclaim
  1171. * everything in the list, try again and wait for writeback IO to complete.
  1172. * This will stall high-order allocations noticeably. Only do that when really
  1173. * need to free the pages under high memory pressure.
  1174. */
  1175. static inline bool should_reclaim_stall(unsigned long nr_taken,
  1176. unsigned long nr_freed,
  1177. int priority,
  1178. struct scan_control *sc)
  1179. {
  1180. int lumpy_stall_priority;
  1181. /* kswapd should not stall on sync IO */
  1182. if (current_is_kswapd())
  1183. return false;
  1184. /* Only stall on lumpy reclaim */
  1185. if (sc->reclaim_mode & RECLAIM_MODE_SINGLE)
  1186. return false;
  1187. /* If we have relaimed everything on the isolated list, no stall */
  1188. if (nr_freed == nr_taken)
  1189. return false;
  1190. /*
  1191. * For high-order allocations, there are two stall thresholds.
  1192. * High-cost allocations stall immediately where as lower
  1193. * order allocations such as stacks require the scanning
  1194. * priority to be much higher before stalling.
  1195. */
  1196. if (sc->order > PAGE_ALLOC_COSTLY_ORDER)
  1197. lumpy_stall_priority = DEF_PRIORITY;
  1198. else
  1199. lumpy_stall_priority = DEF_PRIORITY / 3;
  1200. return priority <= lumpy_stall_priority;
  1201. }
  1202. /*
  1203. * shrink_inactive_list() is a helper for shrink_zone(). It returns the number
  1204. * of reclaimed pages
  1205. */
  1206. static noinline_for_stack unsigned long
  1207. shrink_inactive_list(unsigned long nr_to_scan, struct zone *zone,
  1208. struct scan_control *sc, int priority, int file)
  1209. {
  1210. LIST_HEAD(page_list);
  1211. unsigned long nr_scanned;
  1212. unsigned long nr_reclaimed = 0;
  1213. unsigned long nr_taken;
  1214. unsigned long nr_anon;
  1215. unsigned long nr_file;
  1216. while (unlikely(too_many_isolated(zone, file, sc))) {
  1217. congestion_wait(BLK_RW_ASYNC, HZ/10);
  1218. /* We are about to die and free our memory. Return now. */
  1219. if (fatal_signal_pending(current))
  1220. return SWAP_CLUSTER_MAX;
  1221. }
  1222. set_reclaim_mode(priority, sc, false);
  1223. lru_add_drain();
  1224. spin_lock_irq(&zone->lru_lock);
  1225. if (scanning_global_lru(sc)) {
  1226. nr_taken = isolate_pages_global(nr_to_scan,
  1227. &page_list, &nr_scanned, sc->order,
  1228. sc->reclaim_mode & RECLAIM_MODE_LUMPYRECLAIM ?
  1229. ISOLATE_BOTH : ISOLATE_INACTIVE,
  1230. zone, 0, file);
  1231. zone->pages_scanned += nr_scanned;
  1232. if (current_is_kswapd())
  1233. __count_zone_vm_events(PGSCAN_KSWAPD, zone,
  1234. nr_scanned);
  1235. else
  1236. __count_zone_vm_events(PGSCAN_DIRECT, zone,
  1237. nr_scanned);
  1238. } else {
  1239. nr_taken = mem_cgroup_isolate_pages(nr_to_scan,
  1240. &page_list, &nr_scanned, sc->order,
  1241. sc->reclaim_mode & RECLAIM_MODE_LUMPYRECLAIM ?
  1242. ISOLATE_BOTH : ISOLATE_INACTIVE,
  1243. zone, sc->mem_cgroup,
  1244. 0, file);
  1245. /*
  1246. * mem_cgroup_isolate_pages() keeps track of
  1247. * scanned pages on its own.
  1248. */
  1249. }
  1250. if (nr_taken == 0) {
  1251. spin_unlock_irq(&zone->lru_lock);
  1252. return 0;
  1253. }
  1254. update_isolated_counts(zone, sc, &nr_anon, &nr_file, &page_list);
  1255. spin_unlock_irq(&zone->lru_lock);
  1256. nr_reclaimed = shrink_page_list(&page_list, zone, sc);
  1257. /* Check if we should syncronously wait for writeback */
  1258. if (should_reclaim_stall(nr_taken, nr_reclaimed, priority, sc)) {
  1259. set_reclaim_mode(priority, sc, true);
  1260. nr_reclaimed += shrink_page_list(&page_list, zone, sc);
  1261. }
  1262. local_irq_disable();
  1263. if (current_is_kswapd())
  1264. __count_vm_events(KSWAPD_STEAL, nr_reclaimed);
  1265. __count_zone_vm_events(PGSTEAL, zone, nr_reclaimed);
  1266. putback_lru_pages(zone, sc, nr_anon, nr_file, &page_list);
  1267. trace_mm_vmscan_lru_shrink_inactive(zone->zone_pgdat->node_id,
  1268. zone_idx(zone),
  1269. nr_scanned, nr_reclaimed,
  1270. priority,
  1271. trace_shrink_flags(file, sc->reclaim_mode));
  1272. return nr_reclaimed;
  1273. }
  1274. /*
  1275. * This moves pages from the active list to the inactive list.
  1276. *
  1277. * We move them the other way if the page is referenced by one or more
  1278. * processes, from rmap.
  1279. *
  1280. * If the pages are mostly unmapped, the processing is fast and it is
  1281. * appropriate to hold zone->lru_lock across the whole operation. But if
  1282. * the pages are mapped, the processing is slow (page_referenced()) so we
  1283. * should drop zone->lru_lock around each page. It's impossible to balance
  1284. * this, so instead we remove the pages from the LRU while processing them.
  1285. * It is safe to rely on PG_active against the non-LRU pages in here because
  1286. * nobody will play with that bit on a non-LRU page.
  1287. *
  1288. * The downside is that we have to touch page->_count against each page.
  1289. * But we had to alter page->flags anyway.
  1290. */
  1291. static void move_active_pages_to_lru(struct zone *zone,
  1292. struct list_head *list,
  1293. enum lru_list lru)
  1294. {
  1295. unsigned long pgmoved = 0;
  1296. struct pagevec pvec;
  1297. struct page *page;
  1298. pagevec_init(&pvec, 1);
  1299. while (!list_empty(list)) {
  1300. page = lru_to_page(list);
  1301. VM_BUG_ON(PageLRU(page));
  1302. SetPageLRU(page);
  1303. list_move(&page->lru, &zone->lru[lru].list);
  1304. mem_cgroup_add_lru_list(page, lru);
  1305. pgmoved += hpage_nr_pages(page);
  1306. if (!pagevec_add(&pvec, page) || list_empty(list)) {
  1307. spin_unlock_irq(&zone->lru_lock);
  1308. if (buffer_heads_over_limit)
  1309. pagevec_strip(&pvec);
  1310. __pagevec_release(&pvec);
  1311. spin_lock_irq(&zone->lru_lock);
  1312. }
  1313. }
  1314. __mod_zone_page_state(zone, NR_LRU_BASE + lru, pgmoved);
  1315. if (!is_active_lru(lru))
  1316. __count_vm_events(PGDEACTIVATE, pgmoved);
  1317. }
  1318. static void shrink_active_list(unsigned long nr_pages, struct zone *zone,
  1319. struct scan_control *sc, int priority, int file)
  1320. {
  1321. unsigned long nr_taken;
  1322. unsigned long pgscanned;
  1323. unsigned long vm_flags;
  1324. LIST_HEAD(l_hold); /* The pages which were snipped off */
  1325. LIST_HEAD(l_active);
  1326. LIST_HEAD(l_inactive);
  1327. struct page *page;
  1328. struct zone_reclaim_stat *reclaim_stat = get_reclaim_stat(zone, sc);
  1329. unsigned long nr_rotated = 0;
  1330. lru_add_drain();
  1331. spin_lock_irq(&zone->lru_lock);
  1332. if (scanning_global_lru(sc)) {
  1333. nr_taken = isolate_pages_global(nr_pages, &l_hold,
  1334. &pgscanned, sc->order,
  1335. ISOLATE_ACTIVE, zone,
  1336. 1, file);
  1337. zone->pages_scanned += pgscanned;
  1338. } else {
  1339. nr_taken = mem_cgroup_isolate_pages(nr_pages, &l_hold,
  1340. &pgscanned, sc->order,
  1341. ISOLATE_ACTIVE, zone,
  1342. sc->mem_cgroup, 1, file);
  1343. /*
  1344. * mem_cgroup_isolate_pages() keeps track of
  1345. * scanned pages on its own.
  1346. */
  1347. }
  1348. reclaim_stat->recent_scanned[file] += nr_taken;
  1349. __count_zone_vm_events(PGREFILL, zone, pgscanned);
  1350. if (file)
  1351. __mod_zone_page_state(zone, NR_ACTIVE_FILE, -nr_taken);
  1352. else
  1353. __mod_zone_page_state(zone, NR_ACTIVE_ANON, -nr_taken);
  1354. __mod_zone_page_state(zone, NR_ISOLATED_ANON + file, nr_taken);
  1355. spin_unlock_irq(&zone->lru_lock);
  1356. while (!list_empty(&l_hold)) {
  1357. cond_resched();
  1358. page = lru_to_page(&l_hold);
  1359. list_del(&page->lru);
  1360. if (unlikely(!page_evictable(page, NULL))) {
  1361. putback_lru_page(page);
  1362. continue;
  1363. }
  1364. if (page_referenced(page, 0, sc->mem_cgroup, &vm_flags)) {
  1365. nr_rotated += hpage_nr_pages(page);
  1366. /*
  1367. * Identify referenced, file-backed active pages and
  1368. * give them one more trip around the active list. So
  1369. * that executable code get better chances to stay in
  1370. * memory under moderate memory pressure. Anon pages
  1371. * are not likely to be evicted by use-once streaming
  1372. * IO, plus JVM can create lots of anon VM_EXEC pages,
  1373. * so we ignore them here.
  1374. */
  1375. if ((vm_flags & VM_EXEC) && page_is_file_cache(page)) {
  1376. list_add(&page->lru, &l_active);
  1377. continue;
  1378. }
  1379. }
  1380. ClearPageActive(page); /* we are de-activating */
  1381. list_add(&page->lru, &l_inactive);
  1382. }
  1383. /*
  1384. * Move pages back to the lru list.
  1385. */
  1386. spin_lock_irq(&zone->lru_lock);
  1387. /*
  1388. * Count referenced pages from currently used mappings as rotated,
  1389. * even though only some of them are actually re-activated. This
  1390. * helps balance scan pressure between file and anonymous pages in
  1391. * get_scan_ratio.
  1392. */
  1393. reclaim_stat->recent_rotated[file] += nr_rotated;
  1394. move_active_pages_to_lru(zone, &l_active,
  1395. LRU_ACTIVE + file * LRU_FILE);
  1396. move_active_pages_to_lru(zone, &l_inactive,
  1397. LRU_BASE + file * LRU_FILE);
  1398. __mod_zone_page_state(zone, NR_ISOLATED_ANON + file, -nr_taken);
  1399. spin_unlock_irq(&zone->lru_lock);
  1400. }
  1401. #ifdef CONFIG_SWAP
  1402. static int inactive_anon_is_low_global(struct zone *zone)
  1403. {
  1404. unsigned long active, inactive;
  1405. active = zone_page_state(zone, NR_ACTIVE_ANON);
  1406. inactive = zone_page_state(zone, NR_INACTIVE_ANON);
  1407. if (inactive * zone->inactive_ratio < active)
  1408. return 1;
  1409. return 0;
  1410. }
  1411. /**
  1412. * inactive_anon_is_low - check if anonymous pages need to be deactivated
  1413. * @zone: zone to check
  1414. * @sc: scan control of this context
  1415. *
  1416. * Returns true if the zone does not have enough inactive anon pages,
  1417. * meaning some active anon pages need to be deactivated.
  1418. */
  1419. static int inactive_anon_is_low(struct zone *zone, struct scan_control *sc)
  1420. {
  1421. int low;
  1422. /*
  1423. * If we don't have swap space, anonymous page deactivation
  1424. * is pointless.
  1425. */
  1426. if (!total_swap_pages)
  1427. return 0;
  1428. if (scanning_global_lru(sc))
  1429. low = inactive_anon_is_low_global(zone);
  1430. else
  1431. low = mem_cgroup_inactive_anon_is_low(sc->mem_cgroup);
  1432. return low;
  1433. }
  1434. #else
  1435. static inline int inactive_anon_is_low(struct zone *zone,
  1436. struct scan_control *sc)
  1437. {
  1438. return 0;
  1439. }
  1440. #endif
  1441. static int inactive_file_is_low_global(struct zone *zone)
  1442. {
  1443. unsigned long active, inactive;
  1444. active = zone_page_state(zone, NR_ACTIVE_FILE);
  1445. inactive = zone_page_state(zone, NR_INACTIVE_FILE);
  1446. return (active > inactive);
  1447. }
  1448. /**
  1449. * inactive_file_is_low - check if file pages need to be deactivated
  1450. * @zone: zone to check
  1451. * @sc: scan control of this context
  1452. *
  1453. * When the system is doing streaming IO, memory pressure here
  1454. * ensures that active file pages get deactivated, until more
  1455. * than half of the file pages are on the inactive list.
  1456. *
  1457. * Once we get to that situation, protect the system's working
  1458. * set from being evicted by disabling active file page aging.
  1459. *
  1460. * This uses a different ratio than the anonymous pages, because
  1461. * the page cache uses a use-once replacement algorithm.
  1462. */
  1463. static int inactive_file_is_low(struct zone *zone, struct scan_control *sc)
  1464. {
  1465. int low;
  1466. if (scanning_global_lru(sc))
  1467. low = inactive_file_is_low_global(zone);
  1468. else
  1469. low = mem_cgroup_inactive_file_is_low(sc->mem_cgroup);
  1470. return low;
  1471. }
  1472. static int inactive_list_is_low(struct zone *zone, struct scan_control *sc,
  1473. int file)
  1474. {
  1475. if (file)
  1476. return inactive_file_is_low(zone, sc);
  1477. else
  1478. return inactive_anon_is_low(zone, sc);
  1479. }
  1480. static unsigned long shrink_list(enum lru_list lru, unsigned long nr_to_scan,
  1481. struct zone *zone, struct scan_control *sc, int priority)
  1482. {
  1483. int file = is_file_lru(lru);
  1484. if (is_active_lru(lru)) {
  1485. if (inactive_list_is_low(zone, sc, file))
  1486. shrink_active_list(nr_to_scan, zone, sc, priority, file);
  1487. return 0;
  1488. }
  1489. return shrink_inactive_list(nr_to_scan, zone, sc, priority, file);
  1490. }
  1491. /*
  1492. * Smallish @nr_to_scan's are deposited in @nr_saved_scan,
  1493. * until we collected @swap_cluster_max pages to scan.
  1494. */
  1495. static unsigned long nr_scan_try_batch(unsigned long nr_to_scan,
  1496. unsigned long *nr_saved_scan)
  1497. {
  1498. unsigned long nr;
  1499. *nr_saved_scan += nr_to_scan;
  1500. nr = *nr_saved_scan;
  1501. if (nr >= SWAP_CLUSTER_MAX)
  1502. *nr_saved_scan = 0;
  1503. else
  1504. nr = 0;
  1505. return nr;
  1506. }
  1507. /*
  1508. * Determine how aggressively the anon and file LRU lists should be
  1509. * scanned. The relative value of each set of LRU lists is determined
  1510. * by looking at the fraction of the pages scanned we did rotate back
  1511. * onto the active list instead of evict.
  1512. *
  1513. * nr[0] = anon pages to scan; nr[1] = file pages to scan
  1514. */
  1515. static void get_scan_count(struct zone *zone, struct scan_control *sc,
  1516. unsigned long *nr, int priority)
  1517. {
  1518. unsigned long anon, file, free;
  1519. unsigned long anon_prio, file_prio;
  1520. unsigned long ap, fp;
  1521. struct zone_reclaim_stat *reclaim_stat = get_reclaim_stat(zone, sc);
  1522. u64 fraction[2], denominator;
  1523. enum lru_list l;
  1524. int noswap = 0;
  1525. /* If we have no swap space, do not bother scanning anon pages. */
  1526. if (!sc->may_swap || (nr_swap_pages <= 0)) {
  1527. noswap = 1;
  1528. fraction[0] = 0;
  1529. fraction[1] = 1;
  1530. denominator = 1;
  1531. goto out;
  1532. }
  1533. anon = zone_nr_lru_pages(zone, sc, LRU_ACTIVE_ANON) +
  1534. zone_nr_lru_pages(zone, sc, LRU_INACTIVE_ANON);
  1535. file = zone_nr_lru_pages(zone, sc, LRU_ACTIVE_FILE) +
  1536. zone_nr_lru_pages(zone, sc, LRU_INACTIVE_FILE);
  1537. if (scanning_global_lru(sc)) {
  1538. free = zone_page_state(zone, NR_FREE_PAGES);
  1539. /* If we have very few page cache pages,
  1540. force-scan anon pages. */
  1541. if (unlikely(file + free <= high_wmark_pages(zone))) {
  1542. fraction[0] = 1;
  1543. fraction[1] = 0;
  1544. denominator = 1;
  1545. goto out;
  1546. }
  1547. }
  1548. /*
  1549. * With swappiness at 100, anonymous and file have the same priority.
  1550. * This scanning priority is essentially the inverse of IO cost.
  1551. */
  1552. anon_prio = sc->swappiness;
  1553. file_prio = 200 - sc->swappiness;
  1554. /*
  1555. * OK, so we have swap space and a fair amount of page cache
  1556. * pages. We use the recently rotated / recently scanned
  1557. * ratios to determine how valuable each cache is.
  1558. *
  1559. * Because workloads change over time (and to avoid overflow)
  1560. * we keep these statistics as a floating average, which ends
  1561. * up weighing recent references more than old ones.
  1562. *
  1563. * anon in [0], file in [1]
  1564. */
  1565. spin_lock_irq(&zone->lru_lock);
  1566. if (unlikely(reclaim_stat->recent_scanned[0] > anon / 4)) {
  1567. reclaim_stat->recent_scanned[0] /= 2;
  1568. reclaim_stat->recent_rotated[0] /= 2;
  1569. }
  1570. if (unlikely(reclaim_stat->recent_scanned[1] > file / 4)) {
  1571. reclaim_stat->recent_scanned[1] /= 2;
  1572. reclaim_stat->recent_rotated[1] /= 2;
  1573. }
  1574. /*
  1575. * The amount of pressure on anon vs file pages is inversely
  1576. * proportional to the fraction of recently scanned pages on
  1577. * each list that were recently referenced and in active use.
  1578. */
  1579. ap = (anon_prio + 1) * (reclaim_stat->recent_scanned[0] + 1);
  1580. ap /= reclaim_stat->recent_rotated[0] + 1;
  1581. fp = (file_prio + 1) * (reclaim_stat->recent_scanned[1] + 1);
  1582. fp /= reclaim_stat->recent_rotated[1] + 1;
  1583. spin_unlock_irq(&zone->lru_lock);
  1584. fraction[0] = ap;
  1585. fraction[1] = fp;
  1586. denominator = ap + fp + 1;
  1587. out:
  1588. for_each_evictable_lru(l) {
  1589. int file = is_file_lru(l);
  1590. unsigned long scan;
  1591. scan = zone_nr_lru_pages(zone, sc, l);
  1592. if (priority || noswap) {
  1593. scan >>= priority;
  1594. scan = div64_u64(scan * fraction[file], denominator);
  1595. }
  1596. nr[l] = nr_scan_try_batch(scan,
  1597. &reclaim_stat->nr_saved_scan[l]);
  1598. }
  1599. }
  1600. /*
  1601. * Reclaim/compaction depends on a number of pages being freed. To avoid
  1602. * disruption to the system, a small number of order-0 pages continue to be
  1603. * rotated and reclaimed in the normal fashion. However, by the time we get
  1604. * back to the allocator and call try_to_compact_zone(), we ensure that
  1605. * there are enough free pages for it to be likely successful
  1606. */
  1607. static inline bool should_continue_reclaim(struct zone *zone,
  1608. unsigned long nr_reclaimed,
  1609. unsigned long nr_scanned,
  1610. struct scan_control *sc)
  1611. {
  1612. unsigned long pages_for_compaction;
  1613. unsigned long inactive_lru_pages;
  1614. /* If not in reclaim/compaction mode, stop */
  1615. if (!(sc->reclaim_mode & RECLAIM_MODE_COMPACTION))
  1616. return false;
  1617. /* Consider stopping depending on scan and reclaim activity */
  1618. if (sc->gfp_mask & __GFP_REPEAT) {
  1619. /*
  1620. * For __GFP_REPEAT allocations, stop reclaiming if the
  1621. * full LRU list has been scanned and we are still failing
  1622. * to reclaim pages. This full LRU scan is potentially
  1623. * expensive but a __GFP_REPEAT caller really wants to succeed
  1624. */
  1625. if (!nr_reclaimed && !nr_scanned)
  1626. return false;
  1627. } else {
  1628. /*
  1629. * For non-__GFP_REPEAT allocations which can presumably
  1630. * fail without consequence, stop if we failed to reclaim
  1631. * any pages from the last SWAP_CLUSTER_MAX number of
  1632. * pages that were scanned. This will return to the
  1633. * caller faster at the risk reclaim/compaction and
  1634. * the resulting allocation attempt fails
  1635. */
  1636. if (!nr_reclaimed)
  1637. return false;
  1638. }
  1639. /*
  1640. * If we have not reclaimed enough pages for compaction and the
  1641. * inactive lists are large enough, continue reclaiming
  1642. */
  1643. pages_for_compaction = (2UL << sc->order);
  1644. inactive_lru_pages = zone_nr_lru_pages(zone, sc, LRU_INACTIVE_ANON) +
  1645. zone_nr_lru_pages(zone, sc, LRU_INACTIVE_FILE);
  1646. if (sc->nr_reclaimed < pages_for_compaction &&
  1647. inactive_lru_pages > pages_for_compaction)
  1648. return true;
  1649. /* If compaction would go ahead or the allocation would succeed, stop */
  1650. switch (compaction_suitable(zone, sc->order)) {
  1651. case COMPACT_PARTIAL:
  1652. case COMPACT_CONTINUE:
  1653. return false;
  1654. default:
  1655. return true;
  1656. }
  1657. }
  1658. /*
  1659. * This is a basic per-zone page freer. Used by both kswapd and direct reclaim.
  1660. */
  1661. static void shrink_zone(int priority, struct zone *zone,
  1662. struct scan_control *sc)
  1663. {
  1664. unsigned long nr[NR_LRU_LISTS];
  1665. unsigned long nr_to_scan;
  1666. enum lru_list l;
  1667. unsigned long nr_reclaimed, nr_scanned;
  1668. unsigned long nr_to_reclaim = sc->nr_to_reclaim;
  1669. restart:
  1670. nr_reclaimed = 0;
  1671. nr_scanned = sc->nr_scanned;
  1672. get_scan_count(zone, sc, nr, priority);
  1673. while (nr[LRU_INACTIVE_ANON] || nr[LRU_ACTIVE_FILE] ||
  1674. nr[LRU_INACTIVE_FILE]) {
  1675. for_each_evictable_lru(l) {
  1676. if (nr[l]) {
  1677. nr_to_scan = min_t(unsigned long,
  1678. nr[l], SWAP_CLUSTER_MAX);
  1679. nr[l] -= nr_to_scan;
  1680. nr_reclaimed += shrink_list(l, nr_to_scan,
  1681. zone, sc, priority);
  1682. }
  1683. }
  1684. /*
  1685. * On large memory systems, scan >> priority can become
  1686. * really large. This is fine for the starting priority;
  1687. * we want to put equal scanning pressure on each zone.
  1688. * However, if the VM has a harder time of freeing pages,
  1689. * with multiple processes reclaiming pages, the total
  1690. * freeing target can get unreasonably large.
  1691. */
  1692. if (nr_reclaimed >= nr_to_reclaim && priority < DEF_PRIORITY)
  1693. break;
  1694. }
  1695. sc->nr_reclaimed += nr_reclaimed;
  1696. /*
  1697. * Even if we did not try to evict anon pages at all, we want to
  1698. * rebalance the anon lru active/inactive ratio.
  1699. */
  1700. if (inactive_anon_is_low(zone, sc))
  1701. shrink_active_list(SWAP_CLUSTER_MAX, zone, sc, priority, 0);
  1702. /* reclaim/compaction might need reclaim to continue */
  1703. if (should_continue_reclaim(zone, nr_reclaimed,
  1704. sc->nr_scanned - nr_scanned, sc))
  1705. goto restart;
  1706. throttle_vm_writeout(sc->gfp_mask);
  1707. }
  1708. /*
  1709. * This is the direct reclaim path, for page-allocating processes. We only
  1710. * try to reclaim pages from zones which will satisfy the caller's allocation
  1711. * request.
  1712. *
  1713. * We reclaim from a zone even if that zone is over high_wmark_pages(zone).
  1714. * Because:
  1715. * a) The caller may be trying to free *extra* pages to satisfy a higher-order
  1716. * allocation or
  1717. * b) The target zone may be at high_wmark_pages(zone) but the lower zones
  1718. * must go *over* high_wmark_pages(zone) to satisfy the `incremental min'
  1719. * zone defense algorithm.
  1720. *
  1721. * If a zone is deemed to be full of pinned pages then just give it a light
  1722. * scan then give up on it.
  1723. */
  1724. static void shrink_zones(int priority, struct zonelist *zonelist,
  1725. struct scan_control *sc)
  1726. {
  1727. struct zoneref *z;
  1728. struct zone *zone;
  1729. for_each_zone_zonelist_nodemask(zone, z, zonelist,
  1730. gfp_zone(sc->gfp_mask), sc->nodemask) {
  1731. if (!populated_zone(zone))
  1732. continue;
  1733. /*
  1734. * Take care memory controller reclaiming has small influence
  1735. * to global LRU.
  1736. */
  1737. if (scanning_global_lru(sc)) {
  1738. if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
  1739. continue;
  1740. if (zone->all_unreclaimable && priority != DEF_PRIORITY)
  1741. continue; /* Let kswapd poll it */
  1742. }
  1743. shrink_zone(priority, zone, sc);
  1744. }
  1745. }
  1746. static bool zone_reclaimable(struct zone *zone)
  1747. {
  1748. return zone->pages_scanned < zone_reclaimable_pages(zone) * 6;
  1749. }
  1750. /* All zones in zonelist are unreclaimable? */
  1751. static bool all_unreclaimable(struct zonelist *zonelist,
  1752. struct scan_control *sc)
  1753. {
  1754. struct zoneref *z;
  1755. struct zone *zone;
  1756. for_each_zone_zonelist_nodemask(zone, z, zonelist,
  1757. gfp_zone(sc->gfp_mask), sc->nodemask) {
  1758. if (!populated_zone(zone))
  1759. continue;
  1760. if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
  1761. continue;
  1762. if (!zone->all_unreclaimable)
  1763. return false;
  1764. }
  1765. return true;
  1766. }
  1767. /*
  1768. * This is the main entry point to direct page reclaim.
  1769. *
  1770. * If a full scan of the inactive list fails to free enough memory then we
  1771. * are "out of memory" and something needs to be killed.
  1772. *
  1773. * If the caller is !__GFP_FS then the probability of a failure is reasonably
  1774. * high - the zone may be full of dirty or under-writeback pages, which this
  1775. * caller can't do much about. We kick the writeback threads and take explicit
  1776. * naps in the hope that some of these pages can be written. But if the
  1777. * allocating task holds filesystem locks which prevent writeout this might not
  1778. * work, and the allocation attempt will fail.
  1779. *
  1780. * returns: 0, if no pages reclaimed
  1781. * else, the number of pages reclaimed
  1782. */
  1783. static unsigned long do_try_to_free_pages(struct zonelist *zonelist,
  1784. struct scan_control *sc)
  1785. {
  1786. int priority;
  1787. unsigned long total_scanned = 0;
  1788. struct reclaim_state *reclaim_state = current->reclaim_state;
  1789. struct zoneref *z;
  1790. struct zone *zone;
  1791. unsigned long writeback_threshold;
  1792. get_mems_allowed();
  1793. delayacct_freepages_start();
  1794. if (scanning_global_lru(sc))
  1795. count_vm_event(ALLOCSTALL);
  1796. for (priority = DEF_PRIORITY; priority >= 0; priority--) {
  1797. sc->nr_scanned = 0;
  1798. if (!priority)
  1799. disable_swap_token();
  1800. shrink_zones(priority, zonelist, sc);
  1801. /*
  1802. * Don't shrink slabs when reclaiming memory from
  1803. * over limit cgroups
  1804. */
  1805. if (scanning_global_lru(sc)) {
  1806. unsigned long lru_pages = 0;
  1807. for_each_zone_zonelist(zone, z, zonelist,
  1808. gfp_zone(sc->gfp_mask)) {
  1809. if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
  1810. continue;
  1811. lru_pages += zone_reclaimable_pages(zone);
  1812. }
  1813. shrink_slab(sc->nr_scanned, sc->gfp_mask, lru_pages);
  1814. if (reclaim_state) {
  1815. sc->nr_reclaimed += reclaim_state->reclaimed_slab;
  1816. reclaim_state->reclaimed_slab = 0;
  1817. }
  1818. }
  1819. total_scanned += sc->nr_scanned;
  1820. if (sc->nr_reclaimed >= sc->nr_to_reclaim)
  1821. goto out;
  1822. /*
  1823. * Try to write back as many pages as we just scanned. This
  1824. * tends to cause slow streaming writers to write data to the
  1825. * disk smoothly, at the dirtying rate, which is nice. But
  1826. * that's undesirable in laptop mode, where we *want* lumpy
  1827. * writeout. So in laptop mode, write out the whole world.
  1828. */
  1829. writeback_threshold = sc->nr_to_reclaim + sc->nr_to_reclaim / 2;
  1830. if (total_scanned > writeback_threshold) {
  1831. wakeup_flusher_threads(laptop_mode ? 0 : total_scanned);
  1832. sc->may_writepage = 1;
  1833. }
  1834. /* Take a nap, wait for some writeback to complete */
  1835. if (!sc->hibernation_mode && sc->nr_scanned &&
  1836. priority < DEF_PRIORITY - 2) {
  1837. struct zone *preferred_zone;
  1838. first_zones_zonelist(zonelist, gfp_zone(sc->gfp_mask),
  1839. &cpuset_current_mems_allowed,
  1840. &preferred_zone);
  1841. wait_iff_congested(preferred_zone, BLK_RW_ASYNC, HZ/10);
  1842. }
  1843. }
  1844. out:
  1845. delayacct_freepages_end();
  1846. put_mems_allowed();
  1847. if (sc->nr_reclaimed)
  1848. return sc->nr_reclaimed;
  1849. /*
  1850. * As hibernation is going on, kswapd is freezed so that it can't mark
  1851. * the zone into all_unreclaimable. Thus bypassing all_unreclaimable
  1852. * check.
  1853. */
  1854. if (oom_killer_disabled)
  1855. return 0;
  1856. /* top priority shrink_zones still had more to do? don't OOM, then */
  1857. if (scanning_global_lru(sc) && !all_unreclaimable(zonelist, sc))
  1858. return 1;
  1859. return 0;
  1860. }
  1861. unsigned long try_to_free_pages(struct zonelist *zonelist, int order,
  1862. gfp_t gfp_mask, nodemask_t *nodemask)
  1863. {
  1864. unsigned long nr_reclaimed;
  1865. struct scan_control sc = {
  1866. .gfp_mask = gfp_mask,
  1867. .may_writepage = !laptop_mode,
  1868. .nr_to_reclaim = SWAP_CLUSTER_MAX,
  1869. .may_unmap = 1,
  1870. .may_swap = 1,
  1871. .swappiness = vm_swappiness,
  1872. .order = order,
  1873. .mem_cgroup = NULL,
  1874. .nodemask = nodemask,
  1875. };
  1876. trace_mm_vmscan_direct_reclaim_begin(order,
  1877. sc.may_writepage,
  1878. gfp_mask);
  1879. nr_reclaimed = do_try_to_free_pages(zonelist, &sc);
  1880. trace_mm_vmscan_direct_reclaim_end(nr_reclaimed);
  1881. return nr_reclaimed;
  1882. }
  1883. #ifdef CONFIG_CGROUP_MEM_RES_CTLR
  1884. unsigned long mem_cgroup_shrink_node_zone(struct mem_cgroup *mem,
  1885. gfp_t gfp_mask, bool noswap,
  1886. unsigned int swappiness,
  1887. struct zone *zone)
  1888. {
  1889. struct scan_control sc = {
  1890. .nr_to_reclaim = SWAP_CLUSTER_MAX,
  1891. .may_writepage = !laptop_mode,
  1892. .may_unmap = 1,
  1893. .may_swap = !noswap,
  1894. .swappiness = swappiness,
  1895. .order = 0,
  1896. .mem_cgroup = mem,
  1897. };
  1898. sc.gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) |
  1899. (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK);
  1900. trace_mm_vmscan_memcg_softlimit_reclaim_begin(0,
  1901. sc.may_writepage,
  1902. sc.gfp_mask);
  1903. /*
  1904. * NOTE: Although we can get the priority field, using it
  1905. * here is not a good idea, since it limits the pages we can scan.
  1906. * if we don't reclaim here, the shrink_zone from balance_pgdat
  1907. * will pick up pages from other mem cgroup's as well. We hack
  1908. * the priority and make it zero.
  1909. */
  1910. shrink_zone(0, zone, &sc);
  1911. trace_mm_vmscan_memcg_softlimit_reclaim_end(sc.nr_reclaimed);
  1912. return sc.nr_reclaimed;
  1913. }
  1914. unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *mem_cont,
  1915. gfp_t gfp_mask,
  1916. bool noswap,
  1917. unsigned int swappiness)
  1918. {
  1919. struct zonelist *zonelist;
  1920. unsigned long nr_reclaimed;
  1921. struct scan_control sc = {
  1922. .may_writepage = !laptop_mode,
  1923. .may_unmap = 1,
  1924. .may_swap = !noswap,
  1925. .nr_to_reclaim = SWAP_CLUSTER_MAX,
  1926. .swappiness = swappiness,
  1927. .order = 0,
  1928. .mem_cgroup = mem_cont,
  1929. .nodemask = NULL, /* we don't care the placement */
  1930. };
  1931. sc.gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) |
  1932. (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK);
  1933. zonelist = NODE_DATA(numa_node_id())->node_zonelists;
  1934. trace_mm_vmscan_memcg_reclaim_begin(0,
  1935. sc.may_writepage,
  1936. sc.gfp_mask);
  1937. nr_reclaimed = do_try_to_free_pages(zonelist, &sc);
  1938. trace_mm_vmscan_memcg_reclaim_end(nr_reclaimed);
  1939. return nr_reclaimed;
  1940. }
  1941. #endif
  1942. /*
  1943. * pgdat_balanced is used when checking if a node is balanced for high-order
  1944. * allocations. Only zones that meet watermarks and are in a zone allowed
  1945. * by the callers classzone_idx are added to balanced_pages. The total of
  1946. * balanced pages must be at least 25% of the zones allowed by classzone_idx
  1947. * for the node to be considered balanced. Forcing all zones to be balanced
  1948. * for high orders can cause excessive reclaim when there are imbalanced zones.
  1949. * The choice of 25% is due to
  1950. * o a 16M DMA zone that is balanced will not balance a zone on any
  1951. * reasonable sized machine
  1952. * o On all other machines, the top zone must be at least a reasonable
  1953. * percentage of the middle zones. For example, on 32-bit x86, highmem
  1954. * would need to be at least 256M for it to be balance a whole node.
  1955. * Similarly, on x86-64 the Normal zone would need to be at least 1G
  1956. * to balance a node on its own. These seemed like reasonable ratios.
  1957. */
  1958. static bool pgdat_balanced(pg_data_t *pgdat, unsigned long balanced_pages,
  1959. int classzone_idx)
  1960. {
  1961. unsigned long present_pages = 0;
  1962. int i;
  1963. for (i = 0; i <= classzone_idx; i++)
  1964. present_pages += pgdat->node_zones[i].present_pages;
  1965. return balanced_pages > (present_pages >> 2);
  1966. }
  1967. /* is kswapd sleeping prematurely? */
  1968. static bool sleeping_prematurely(pg_data_t *pgdat, int order, long remaining,
  1969. int classzone_idx)
  1970. {
  1971. int i;
  1972. unsigned long balanced = 0;
  1973. bool all_zones_ok = true;
  1974. /* If a direct reclaimer woke kswapd within HZ/10, it's premature */
  1975. if (remaining)
  1976. return true;
  1977. /* Check the watermark levels */
  1978. for (i = 0; i < pgdat->nr_zones; i++) {
  1979. struct zone *zone = pgdat->node_zones + i;
  1980. if (!populated_zone(zone))
  1981. continue;
  1982. /*
  1983. * balance_pgdat() skips over all_unreclaimable after
  1984. * DEF_PRIORITY. Effectively, it considers them balanced so
  1985. * they must be considered balanced here as well if kswapd
  1986. * is to sleep
  1987. */
  1988. if (zone->all_unreclaimable) {
  1989. balanced += zone->present_pages;
  1990. continue;
  1991. }
  1992. if (!zone_watermark_ok_safe(zone, order, high_wmark_pages(zone),
  1993. classzone_idx, 0))
  1994. all_zones_ok = false;
  1995. else
  1996. balanced += zone->present_pages;
  1997. }
  1998. /*
  1999. * For high-order requests, the balanced zones must contain at least
  2000. * 25% of the nodes pages for kswapd to sleep. For order-0, all zones
  2001. * must be balanced
  2002. */
  2003. if (order)
  2004. return !pgdat_balanced(pgdat, balanced, classzone_idx);
  2005. else
  2006. return !all_zones_ok;
  2007. }
  2008. /*
  2009. * For kswapd, balance_pgdat() will work across all this node's zones until
  2010. * they are all at high_wmark_pages(zone).
  2011. *
  2012. * Returns the final order kswapd was reclaiming at
  2013. *
  2014. * There is special handling here for zones which are full of pinned pages.
  2015. * This can happen if the pages are all mlocked, or if they are all used by
  2016. * device drivers (say, ZONE_DMA). Or if they are all in use by hugetlb.
  2017. * What we do is to detect the case where all pages in the zone have been
  2018. * scanned twice and there has been zero successful reclaim. Mark the zone as
  2019. * dead and from now on, only perform a short scan. Basically we're polling
  2020. * the zone for when the problem goes away.
  2021. *
  2022. * kswapd scans the zones in the highmem->normal->dma direction. It skips
  2023. * zones which have free_pages > high_wmark_pages(zone), but once a zone is
  2024. * found to have free_pages <= high_wmark_pages(zone), we scan that zone and the
  2025. * lower zones regardless of the number of free pages in the lower zones. This
  2026. * interoperates with the page allocator fallback scheme to ensure that aging
  2027. * of pages is balanced across the zones.
  2028. */
  2029. static unsigned long balance_pgdat(pg_data_t *pgdat, int order,
  2030. int *classzone_idx)
  2031. {
  2032. int all_zones_ok;
  2033. unsigned long balanced;
  2034. int priority;
  2035. int i;
  2036. int end_zone = 0; /* Inclusive. 0 = ZONE_DMA */
  2037. unsigned long total_scanned;
  2038. struct reclaim_state *reclaim_state = current->reclaim_state;
  2039. struct scan_control sc = {
  2040. .gfp_mask = GFP_KERNEL,
  2041. .may_unmap = 1,
  2042. .may_swap = 1,
  2043. /*
  2044. * kswapd doesn't want to be bailed out while reclaim. because
  2045. * we want to put equal scanning pressure on each zone.
  2046. */
  2047. .nr_to_reclaim = ULONG_MAX,
  2048. .swappiness = vm_swappiness,
  2049. .order = order,
  2050. .mem_cgroup = NULL,
  2051. };
  2052. loop_again:
  2053. total_scanned = 0;
  2054. sc.nr_reclaimed = 0;
  2055. sc.may_writepage = !laptop_mode;
  2056. count_vm_event(PAGEOUTRUN);
  2057. for (priority = DEF_PRIORITY; priority >= 0; priority--) {
  2058. unsigned long lru_pages = 0;
  2059. int has_under_min_watermark_zone = 0;
  2060. /* The swap token gets in the way of swapout... */
  2061. if (!priority)
  2062. disable_swap_token();
  2063. all_zones_ok = 1;
  2064. balanced = 0;
  2065. /*
  2066. * Scan in the highmem->dma direction for the highest
  2067. * zone which needs scanning
  2068. */
  2069. for (i = pgdat->nr_zones - 1; i >= 0; i--) {
  2070. struct zone *zone = pgdat->node_zones + i;
  2071. if (!populated_zone(zone))
  2072. continue;
  2073. if (zone->all_unreclaimable && priority != DEF_PRIORITY)
  2074. continue;
  2075. /*
  2076. * Do some background aging of the anon list, to give
  2077. * pages a chance to be referenced before reclaiming.
  2078. */
  2079. if (inactive_anon_is_low(zone, &sc))
  2080. shrink_active_list(SWAP_CLUSTER_MAX, zone,
  2081. &sc, priority, 0);
  2082. if (!zone_watermark_ok_safe(zone, order,
  2083. high_wmark_pages(zone), 0, 0)) {
  2084. end_zone = i;
  2085. *classzone_idx = i;
  2086. break;
  2087. }
  2088. }
  2089. if (i < 0)
  2090. goto out;
  2091. for (i = 0; i <= end_zone; i++) {
  2092. struct zone *zone = pgdat->node_zones + i;
  2093. lru_pages += zone_reclaimable_pages(zone);
  2094. }
  2095. /*
  2096. * Now scan the zone in the dma->highmem direction, stopping
  2097. * at the last zone which needs scanning.
  2098. *
  2099. * We do this because the page allocator works in the opposite
  2100. * direction. This prevents the page allocator from allocating
  2101. * pages behind kswapd's direction of progress, which would
  2102. * cause too much scanning of the lower zones.
  2103. */
  2104. for (i = 0; i <= end_zone; i++) {
  2105. struct zone *zone = pgdat->node_zones + i;
  2106. int nr_slab;
  2107. unsigned long balance_gap;
  2108. if (!populated_zone(zone))
  2109. continue;
  2110. if (zone->all_unreclaimable && priority != DEF_PRIORITY)
  2111. continue;
  2112. sc.nr_scanned = 0;
  2113. /*
  2114. * Call soft limit reclaim before calling shrink_zone.
  2115. * For now we ignore the return value
  2116. */
  2117. mem_cgroup_soft_limit_reclaim(zone, order, sc.gfp_mask);
  2118. /*
  2119. * We put equal pressure on every zone, unless
  2120. * one zone has way too many pages free
  2121. * already. The "too many pages" is defined
  2122. * as the high wmark plus a "gap" where the
  2123. * gap is either the low watermark or 1%
  2124. * of the zone, whichever is smaller.
  2125. */
  2126. balance_gap = min(low_wmark_pages(zone),
  2127. (zone->present_pages +
  2128. KSWAPD_ZONE_BALANCE_GAP_RATIO-1) /
  2129. KSWAPD_ZONE_BALANCE_GAP_RATIO);
  2130. if (!zone_watermark_ok_safe(zone, order,
  2131. high_wmark_pages(zone) + balance_gap,
  2132. end_zone, 0))
  2133. shrink_zone(priority, zone, &sc);
  2134. reclaim_state->reclaimed_slab = 0;
  2135. nr_slab = shrink_slab(sc.nr_scanned, GFP_KERNEL,
  2136. lru_pages);
  2137. sc.nr_reclaimed += reclaim_state->reclaimed_slab;
  2138. total_scanned += sc.nr_scanned;
  2139. if (zone->all_unreclaimable)
  2140. continue;
  2141. if (nr_slab == 0 &&
  2142. !zone_reclaimable(zone))
  2143. zone->all_unreclaimable = 1;
  2144. /*
  2145. * If we've done a decent amount of scanning and
  2146. * the reclaim ratio is low, start doing writepage
  2147. * even in laptop mode
  2148. */
  2149. if (total_scanned > SWAP_CLUSTER_MAX * 2 &&
  2150. total_scanned > sc.nr_reclaimed + sc.nr_reclaimed / 2)
  2151. sc.may_writepage = 1;
  2152. if (!zone_watermark_ok_safe(zone, order,
  2153. high_wmark_pages(zone), end_zone, 0)) {
  2154. all_zones_ok = 0;
  2155. /*
  2156. * We are still under min water mark. This
  2157. * means that we have a GFP_ATOMIC allocation
  2158. * failure risk. Hurry up!
  2159. */
  2160. if (!zone_watermark_ok_safe(zone, order,
  2161. min_wmark_pages(zone), end_zone, 0))
  2162. has_under_min_watermark_zone = 1;
  2163. } else {
  2164. /*
  2165. * If a zone reaches its high watermark,
  2166. * consider it to be no longer congested. It's
  2167. * possible there are dirty pages backed by
  2168. * congested BDIs but as pressure is relieved,
  2169. * spectulatively avoid congestion waits
  2170. */
  2171. zone_clear_flag(zone, ZONE_CONGESTED);
  2172. if (i <= *classzone_idx)
  2173. balanced += zone->present_pages;
  2174. }
  2175. }
  2176. if (all_zones_ok || (order && pgdat_balanced(pgdat, balanced, *classzone_idx)))
  2177. break; /* kswapd: all done */
  2178. /*
  2179. * OK, kswapd is getting into trouble. Take a nap, then take
  2180. * another pass across the zones.
  2181. */
  2182. if (total_scanned && (priority < DEF_PRIORITY - 2)) {
  2183. if (has_under_min_watermark_zone)
  2184. count_vm_event(KSWAPD_SKIP_CONGESTION_WAIT);
  2185. else
  2186. congestion_wait(BLK_RW_ASYNC, HZ/10);
  2187. }
  2188. /*
  2189. * We do this so kswapd doesn't build up large priorities for
  2190. * example when it is freeing in parallel with allocators. It
  2191. * matches the direct reclaim path behaviour in terms of impact
  2192. * on zone->*_priority.
  2193. */
  2194. if (sc.nr_reclaimed >= SWAP_CLUSTER_MAX)
  2195. break;
  2196. }
  2197. out:
  2198. /*
  2199. * order-0: All zones must meet high watermark for a balanced node
  2200. * high-order: Balanced zones must make up at least 25% of the node
  2201. * for the node to be balanced
  2202. */
  2203. if (!(all_zones_ok || (order && pgdat_balanced(pgdat, balanced, *classzone_idx)))) {
  2204. cond_resched();
  2205. try_to_freeze();
  2206. /*
  2207. * Fragmentation may mean that the system cannot be
  2208. * rebalanced for high-order allocations in all zones.
  2209. * At this point, if nr_reclaimed < SWAP_CLUSTER_MAX,
  2210. * it means the zones have been fully scanned and are still
  2211. * not balanced. For high-order allocations, there is
  2212. * little point trying all over again as kswapd may
  2213. * infinite loop.
  2214. *
  2215. * Instead, recheck all watermarks at order-0 as they
  2216. * are the most important. If watermarks are ok, kswapd will go
  2217. * back to sleep. High-order users can still perform direct
  2218. * reclaim if they wish.
  2219. */
  2220. if (sc.nr_reclaimed < SWAP_CLUSTER_MAX)
  2221. order = sc.order = 0;
  2222. goto loop_again;
  2223. }
  2224. /*
  2225. * If kswapd was reclaiming at a higher order, it has the option of
  2226. * sleeping without all zones being balanced. Before it does, it must
  2227. * ensure that the watermarks for order-0 on *all* zones are met and
  2228. * that the congestion flags are cleared. The congestion flag must
  2229. * be cleared as kswapd is the only mechanism that clears the flag
  2230. * and it is potentially going to sleep here.
  2231. */
  2232. if (order) {
  2233. for (i = 0; i <= end_zone; i++) {
  2234. struct zone *zone = pgdat->node_zones + i;
  2235. if (!populated_zone(zone))
  2236. continue;
  2237. if (zone->all_unreclaimable && priority != DEF_PRIORITY)
  2238. continue;
  2239. /* Confirm the zone is balanced for order-0 */
  2240. if (!zone_watermark_ok(zone, 0,
  2241. high_wmark_pages(zone), 0, 0)) {
  2242. order = sc.order = 0;
  2243. goto loop_again;
  2244. }
  2245. /* If balanced, clear the congested flag */
  2246. zone_clear_flag(zone, ZONE_CONGESTED);
  2247. }
  2248. }
  2249. /*
  2250. * Return the order we were reclaiming at so sleeping_prematurely()
  2251. * makes a decision on the order we were last reclaiming at. However,
  2252. * if another caller entered the allocator slow path while kswapd
  2253. * was awake, order will remain at the higher level
  2254. */
  2255. *classzone_idx = end_zone;
  2256. return order;
  2257. }
  2258. static void kswapd_try_to_sleep(pg_data_t *pgdat, int order, int classzone_idx)
  2259. {
  2260. long remaining = 0;
  2261. DEFINE_WAIT(wait);
  2262. if (freezing(current) || kthread_should_stop())
  2263. return;
  2264. prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
  2265. /* Try to sleep for a short interval */
  2266. if (!sleeping_prematurely(pgdat, order, remaining, classzone_idx)) {
  2267. remaining = schedule_timeout(HZ/10);
  2268. finish_wait(&pgdat->kswapd_wait, &wait);
  2269. prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
  2270. }
  2271. /*
  2272. * After a short sleep, check if it was a premature sleep. If not, then
  2273. * go fully to sleep until explicitly woken up.
  2274. */
  2275. if (!sleeping_prematurely(pgdat, order, remaining, classzone_idx)) {
  2276. trace_mm_vmscan_kswapd_sleep(pgdat->node_id);
  2277. /*
  2278. * vmstat counters are not perfectly accurate and the estimated
  2279. * value for counters such as NR_FREE_PAGES can deviate from the
  2280. * true value by nr_online_cpus * threshold. To avoid the zone
  2281. * watermarks being breached while under pressure, we reduce the
  2282. * per-cpu vmstat threshold while kswapd is awake and restore
  2283. * them before going back to sleep.
  2284. */
  2285. set_pgdat_percpu_threshold(pgdat, calculate_normal_threshold);
  2286. schedule();
  2287. set_pgdat_percpu_threshold(pgdat, calculate_pressure_threshold);
  2288. } else {
  2289. if (remaining)
  2290. count_vm_event(KSWAPD_LOW_WMARK_HIT_QUICKLY);
  2291. else
  2292. count_vm_event(KSWAPD_HIGH_WMARK_HIT_QUICKLY);
  2293. }
  2294. finish_wait(&pgdat->kswapd_wait, &wait);
  2295. }
  2296. /*
  2297. * The background pageout daemon, started as a kernel thread
  2298. * from the init process.
  2299. *
  2300. * This basically trickles out pages so that we have _some_
  2301. * free memory available even if there is no other activity
  2302. * that frees anything up. This is needed for things like routing
  2303. * etc, where we otherwise might have all activity going on in
  2304. * asynchronous contexts that cannot page things out.
  2305. *
  2306. * If there are applications that are active memory-allocators
  2307. * (most normal use), this basically shouldn't matter.
  2308. */
  2309. static int kswapd(void *p)
  2310. {
  2311. unsigned long order;
  2312. int classzone_idx;
  2313. pg_data_t *pgdat = (pg_data_t*)p;
  2314. struct task_struct *tsk = current;
  2315. struct reclaim_state reclaim_state = {
  2316. .reclaimed_slab = 0,
  2317. };
  2318. const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id);
  2319. lockdep_set_current_reclaim_state(GFP_KERNEL);
  2320. if (!cpumask_empty(cpumask))
  2321. set_cpus_allowed_ptr(tsk, cpumask);
  2322. current->reclaim_state = &reclaim_state;
  2323. /*
  2324. * Tell the memory management that we're a "memory allocator",
  2325. * and that if we need more memory we should get access to it
  2326. * regardless (see "__alloc_pages()"). "kswapd" should
  2327. * never get caught in the normal page freeing logic.
  2328. *
  2329. * (Kswapd normally doesn't need memory anyway, but sometimes
  2330. * you need a small amount of memory in order to be able to
  2331. * page out something else, and this flag essentially protects
  2332. * us from recursively trying to free more memory as we're
  2333. * trying to free the first piece of memory in the first place).
  2334. */
  2335. tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD;
  2336. set_freezable();
  2337. order = 0;
  2338. classzone_idx = MAX_NR_ZONES - 1;
  2339. for ( ; ; ) {
  2340. unsigned long new_order;
  2341. int new_classzone_idx;
  2342. int ret;
  2343. new_order = pgdat->kswapd_max_order;
  2344. new_classzone_idx = pgdat->classzone_idx;
  2345. pgdat->kswapd_max_order = 0;
  2346. pgdat->classzone_idx = MAX_NR_ZONES - 1;
  2347. if (order < new_order || classzone_idx > new_classzone_idx) {
  2348. /*
  2349. * Don't sleep if someone wants a larger 'order'
  2350. * allocation or has tigher zone constraints
  2351. */
  2352. order = new_order;
  2353. classzone_idx = new_classzone_idx;
  2354. } else {
  2355. kswapd_try_to_sleep(pgdat, order, classzone_idx);
  2356. order = pgdat->kswapd_max_order;
  2357. classzone_idx = pgdat->classzone_idx;
  2358. pgdat->kswapd_max_order = 0;
  2359. pgdat->classzone_idx = MAX_NR_ZONES - 1;
  2360. }
  2361. ret = try_to_freeze();
  2362. if (kthread_should_stop())
  2363. break;
  2364. /*
  2365. * We can speed up thawing tasks if we don't call balance_pgdat
  2366. * after returning from the refrigerator
  2367. */
  2368. if (!ret) {
  2369. trace_mm_vmscan_kswapd_wake(pgdat->node_id, order);
  2370. order = balance_pgdat(pgdat, order, &classzone_idx);
  2371. }
  2372. }
  2373. return 0;
  2374. }
  2375. /*
  2376. * A zone is low on free memory, so wake its kswapd task to service it.
  2377. */
  2378. void wakeup_kswapd(struct zone *zone, int order, enum zone_type classzone_idx)
  2379. {
  2380. pg_data_t *pgdat;
  2381. if (!populated_zone(zone))
  2382. return;
  2383. if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
  2384. return;
  2385. pgdat = zone->zone_pgdat;
  2386. if (pgdat->kswapd_max_order < order) {
  2387. pgdat->kswapd_max_order = order;
  2388. pgdat->classzone_idx = min(pgdat->classzone_idx, classzone_idx);
  2389. }
  2390. if (!waitqueue_active(&pgdat->kswapd_wait))
  2391. return;
  2392. if (zone_watermark_ok_safe(zone, order, low_wmark_pages(zone), 0, 0))
  2393. return;
  2394. trace_mm_vmscan_wakeup_kswapd(pgdat->node_id, zone_idx(zone), order);
  2395. wake_up_interruptible(&pgdat->kswapd_wait);
  2396. }
  2397. /*
  2398. * The reclaimable count would be mostly accurate.
  2399. * The less reclaimable pages may be
  2400. * - mlocked pages, which will be moved to unevictable list when encountered
  2401. * - mapped pages, which may require several travels to be reclaimed
  2402. * - dirty pages, which is not "instantly" reclaimable
  2403. */
  2404. unsigned long global_reclaimable_pages(void)
  2405. {
  2406. int nr;
  2407. nr = global_page_state(NR_ACTIVE_FILE) +
  2408. global_page_state(NR_INACTIVE_FILE);
  2409. if (nr_swap_pages > 0)
  2410. nr += global_page_state(NR_ACTIVE_ANON) +
  2411. global_page_state(NR_INACTIVE_ANON);
  2412. return nr;
  2413. }
  2414. unsigned long zone_reclaimable_pages(struct zone *zone)
  2415. {
  2416. int nr;
  2417. nr = zone_page_state(zone, NR_ACTIVE_FILE) +
  2418. zone_page_state(zone, NR_INACTIVE_FILE);
  2419. if (nr_swap_pages > 0)
  2420. nr += zone_page_state(zone, NR_ACTIVE_ANON) +
  2421. zone_page_state(zone, NR_INACTIVE_ANON);
  2422. return nr;
  2423. }
  2424. #ifdef CONFIG_HIBERNATION
  2425. /*
  2426. * Try to free `nr_to_reclaim' of memory, system-wide, and return the number of
  2427. * freed pages.
  2428. *
  2429. * Rather than trying to age LRUs the aim is to preserve the overall
  2430. * LRU order by reclaiming preferentially
  2431. * inactive > active > active referenced > active mapped
  2432. */
  2433. unsigned long shrink_all_memory(unsigned long nr_to_reclaim)
  2434. {
  2435. struct reclaim_state reclaim_state;
  2436. struct scan_control sc = {
  2437. .gfp_mask = GFP_HIGHUSER_MOVABLE,
  2438. .may_swap = 1,
  2439. .may_unmap = 1,
  2440. .may_writepage = 1,
  2441. .nr_to_reclaim = nr_to_reclaim,
  2442. .hibernation_mode = 1,
  2443. .swappiness = vm_swappiness,
  2444. .order = 0,
  2445. };
  2446. struct zonelist * zonelist = node_zonelist(numa_node_id(), sc.gfp_mask);
  2447. struct task_struct *p = current;
  2448. unsigned long nr_reclaimed;
  2449. p->flags |= PF_MEMALLOC;
  2450. lockdep_set_current_reclaim_state(sc.gfp_mask);
  2451. reclaim_state.reclaimed_slab = 0;
  2452. p->reclaim_state = &reclaim_state;
  2453. nr_reclaimed = do_try_to_free_pages(zonelist, &sc);
  2454. p->reclaim_state = NULL;
  2455. lockdep_clear_current_reclaim_state();
  2456. p->flags &= ~PF_MEMALLOC;
  2457. return nr_reclaimed;
  2458. }
  2459. #endif /* CONFIG_HIBERNATION */
  2460. /* It's optimal to keep kswapds on the same CPUs as their memory, but
  2461. not required for correctness. So if the last cpu in a node goes
  2462. away, we get changed to run anywhere: as the first one comes back,
  2463. restore their cpu bindings. */
  2464. static int __devinit cpu_callback(struct notifier_block *nfb,
  2465. unsigned long action, void *hcpu)
  2466. {
  2467. int nid;
  2468. if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN) {
  2469. for_each_node_state(nid, N_HIGH_MEMORY) {
  2470. pg_data_t *pgdat = NODE_DATA(nid);
  2471. const struct cpumask *mask;
  2472. mask = cpumask_of_node(pgdat->node_id);
  2473. if (cpumask_any_and(cpu_online_mask, mask) < nr_cpu_ids)
  2474. /* One of our CPUs online: restore mask */
  2475. set_cpus_allowed_ptr(pgdat->kswapd, mask);
  2476. }
  2477. }
  2478. return NOTIFY_OK;
  2479. }
  2480. /*
  2481. * This kswapd start function will be called by init and node-hot-add.
  2482. * On node-hot-add, kswapd will moved to proper cpus if cpus are hot-added.
  2483. */
  2484. int kswapd_run(int nid)
  2485. {
  2486. pg_data_t *pgdat = NODE_DATA(nid);
  2487. int ret = 0;
  2488. if (pgdat->kswapd)
  2489. return 0;
  2490. pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid);
  2491. if (IS_ERR(pgdat->kswapd)) {
  2492. /* failure at boot is fatal */
  2493. BUG_ON(system_state == SYSTEM_BOOTING);
  2494. printk("Failed to start kswapd on node %d\n",nid);
  2495. ret = -1;
  2496. }
  2497. return ret;
  2498. }
  2499. /*
  2500. * Called by memory hotplug when all memory in a node is offlined.
  2501. */
  2502. void kswapd_stop(int nid)
  2503. {
  2504. struct task_struct *kswapd = NODE_DATA(nid)->kswapd;
  2505. if (kswapd)
  2506. kthread_stop(kswapd);
  2507. }
  2508. static int __init kswapd_init(void)
  2509. {
  2510. int nid;
  2511. swap_setup();
  2512. for_each_node_state(nid, N_HIGH_MEMORY)
  2513. kswapd_run(nid);
  2514. hotcpu_notifier(cpu_callback, 0);
  2515. return 0;
  2516. }
  2517. module_init(kswapd_init)
  2518. #ifdef CONFIG_NUMA
  2519. /*
  2520. * Zone reclaim mode
  2521. *
  2522. * If non-zero call zone_reclaim when the number of free pages falls below
  2523. * the watermarks.
  2524. */
  2525. int zone_reclaim_mode __read_mostly;
  2526. #define RECLAIM_OFF 0
  2527. #define RECLAIM_ZONE (1<<0) /* Run shrink_inactive_list on the zone */
  2528. #define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */
  2529. #define RECLAIM_SWAP (1<<2) /* Swap pages out during reclaim */
  2530. /*
  2531. * Priority for ZONE_RECLAIM. This determines the fraction of pages
  2532. * of a node considered for each zone_reclaim. 4 scans 1/16th of
  2533. * a zone.
  2534. */
  2535. #define ZONE_RECLAIM_PRIORITY 4
  2536. /*
  2537. * Percentage of pages in a zone that must be unmapped for zone_reclaim to
  2538. * occur.
  2539. */
  2540. int sysctl_min_unmapped_ratio = 1;
  2541. /*
  2542. * If the number of slab pages in a zone grows beyond this percentage then
  2543. * slab reclaim needs to occur.
  2544. */
  2545. int sysctl_min_slab_ratio = 5;
  2546. static inline unsigned long zone_unmapped_file_pages(struct zone *zone)
  2547. {
  2548. unsigned long file_mapped = zone_page_state(zone, NR_FILE_MAPPED);
  2549. unsigned long file_lru = zone_page_state(zone, NR_INACTIVE_FILE) +
  2550. zone_page_state(zone, NR_ACTIVE_FILE);
  2551. /*
  2552. * It's possible for there to be more file mapped pages than
  2553. * accounted for by the pages on the file LRU lists because
  2554. * tmpfs pages accounted for as ANON can also be FILE_MAPPED
  2555. */
  2556. return (file_lru > file_mapped) ? (file_lru - file_mapped) : 0;
  2557. }
  2558. /* Work out how many page cache pages we can reclaim in this reclaim_mode */
  2559. static long zone_pagecache_reclaimable(struct zone *zone)
  2560. {
  2561. long nr_pagecache_reclaimable;
  2562. long delta = 0;
  2563. /*
  2564. * If RECLAIM_SWAP is set, then all file pages are considered
  2565. * potentially reclaimable. Otherwise, we have to worry about
  2566. * pages like swapcache and zone_unmapped_file_pages() provides
  2567. * a better estimate
  2568. */
  2569. if (zone_reclaim_mode & RECLAIM_SWAP)
  2570. nr_pagecache_reclaimable = zone_page_state(zone, NR_FILE_PAGES);
  2571. else
  2572. nr_pagecache_reclaimable = zone_unmapped_file_pages(zone);
  2573. /* If we can't clean pages, remove dirty pages from consideration */
  2574. if (!(zone_reclaim_mode & RECLAIM_WRITE))
  2575. delta += zone_page_state(zone, NR_FILE_DIRTY);
  2576. /* Watch for any possible underflows due to delta */
  2577. if (unlikely(delta > nr_pagecache_reclaimable))
  2578. delta = nr_pagecache_reclaimable;
  2579. return nr_pagecache_reclaimable - delta;
  2580. }
  2581. /*
  2582. * Try to free up some pages from this zone through reclaim.
  2583. */
  2584. static int __zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
  2585. {
  2586. /* Minimum pages needed in order to stay on node */
  2587. const unsigned long nr_pages = 1 << order;
  2588. struct task_struct *p = current;
  2589. struct reclaim_state reclaim_state;
  2590. int priority;
  2591. struct scan_control sc = {
  2592. .may_writepage = !!(zone_reclaim_mode & RECLAIM_WRITE),
  2593. .may_unmap = !!(zone_reclaim_mode & RECLAIM_SWAP),
  2594. .may_swap = 1,
  2595. .nr_to_reclaim = max_t(unsigned long, nr_pages,
  2596. SWAP_CLUSTER_MAX),
  2597. .gfp_mask = gfp_mask,
  2598. .swappiness = vm_swappiness,
  2599. .order = order,
  2600. };
  2601. unsigned long nr_slab_pages0, nr_slab_pages1;
  2602. cond_resched();
  2603. /*
  2604. * We need to be able to allocate from the reserves for RECLAIM_SWAP
  2605. * and we also need to be able to write out pages for RECLAIM_WRITE
  2606. * and RECLAIM_SWAP.
  2607. */
  2608. p->flags |= PF_MEMALLOC | PF_SWAPWRITE;
  2609. lockdep_set_current_reclaim_state(gfp_mask);
  2610. reclaim_state.reclaimed_slab = 0;
  2611. p->reclaim_state = &reclaim_state;
  2612. if (zone_pagecache_reclaimable(zone) > zone->min_unmapped_pages) {
  2613. /*
  2614. * Free memory by calling shrink zone with increasing
  2615. * priorities until we have enough memory freed.
  2616. */
  2617. priority = ZONE_RECLAIM_PRIORITY;
  2618. do {
  2619. shrink_zone(priority, zone, &sc);
  2620. priority--;
  2621. } while (priority >= 0 && sc.nr_reclaimed < nr_pages);
  2622. }
  2623. nr_slab_pages0 = zone_page_state(zone, NR_SLAB_RECLAIMABLE);
  2624. if (nr_slab_pages0 > zone->min_slab_pages) {
  2625. /*
  2626. * shrink_slab() does not currently allow us to determine how
  2627. * many pages were freed in this zone. So we take the current
  2628. * number of slab pages and shake the slab until it is reduced
  2629. * by the same nr_pages that we used for reclaiming unmapped
  2630. * pages.
  2631. *
  2632. * Note that shrink_slab will free memory on all zones and may
  2633. * take a long time.
  2634. */
  2635. for (;;) {
  2636. unsigned long lru_pages = zone_reclaimable_pages(zone);
  2637. /* No reclaimable slab or very low memory pressure */
  2638. if (!shrink_slab(sc.nr_scanned, gfp_mask, lru_pages))
  2639. break;
  2640. /* Freed enough memory */
  2641. nr_slab_pages1 = zone_page_state(zone,
  2642. NR_SLAB_RECLAIMABLE);
  2643. if (nr_slab_pages1 + nr_pages <= nr_slab_pages0)
  2644. break;
  2645. }
  2646. /*
  2647. * Update nr_reclaimed by the number of slab pages we
  2648. * reclaimed from this zone.
  2649. */
  2650. nr_slab_pages1 = zone_page_state(zone, NR_SLAB_RECLAIMABLE);
  2651. if (nr_slab_pages1 < nr_slab_pages0)
  2652. sc.nr_reclaimed += nr_slab_pages0 - nr_slab_pages1;
  2653. }
  2654. p->reclaim_state = NULL;
  2655. current->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE);
  2656. lockdep_clear_current_reclaim_state();
  2657. return sc.nr_reclaimed >= nr_pages;
  2658. }
  2659. int zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
  2660. {
  2661. int node_id;
  2662. int ret;
  2663. /*
  2664. * Zone reclaim reclaims unmapped file backed pages and
  2665. * slab pages if we are over the defined limits.
  2666. *
  2667. * A small portion of unmapped file backed pages is needed for
  2668. * file I/O otherwise pages read by file I/O will be immediately
  2669. * thrown out if the zone is overallocated. So we do not reclaim
  2670. * if less than a specified percentage of the zone is used by
  2671. * unmapped file backed pages.
  2672. */
  2673. if (zone_pagecache_reclaimable(zone) <= zone->min_unmapped_pages &&
  2674. zone_page_state(zone, NR_SLAB_RECLAIMABLE) <= zone->min_slab_pages)
  2675. return ZONE_RECLAIM_FULL;
  2676. if (zone->all_unreclaimable)
  2677. return ZONE_RECLAIM_FULL;
  2678. /*
  2679. * Do not scan if the allocation should not be delayed.
  2680. */
  2681. if (!(gfp_mask & __GFP_WAIT) || (current->flags & PF_MEMALLOC))
  2682. return ZONE_RECLAIM_NOSCAN;
  2683. /*
  2684. * Only run zone reclaim on the local zone or on zones that do not
  2685. * have associated processors. This will favor the local processor
  2686. * over remote processors and spread off node memory allocations
  2687. * as wide as possible.
  2688. */
  2689. node_id = zone_to_nid(zone);
  2690. if (node_state(node_id, N_CPU) && node_id != numa_node_id())
  2691. return ZONE_RECLAIM_NOSCAN;
  2692. if (zone_test_and_set_flag(zone, ZONE_RECLAIM_LOCKED))
  2693. return ZONE_RECLAIM_NOSCAN;
  2694. ret = __zone_reclaim(zone, gfp_mask, order);
  2695. zone_clear_flag(zone, ZONE_RECLAIM_LOCKED);
  2696. if (!ret)
  2697. count_vm_event(PGSCAN_ZONE_RECLAIM_FAILED);
  2698. return ret;
  2699. }
  2700. #endif
  2701. /*
  2702. * page_evictable - test whether a page is evictable
  2703. * @page: the page to test
  2704. * @vma: the VMA in which the page is or will be mapped, may be NULL
  2705. *
  2706. * Test whether page is evictable--i.e., should be placed on active/inactive
  2707. * lists vs unevictable list. The vma argument is !NULL when called from the
  2708. * fault path to determine how to instantate a new page.
  2709. *
  2710. * Reasons page might not be evictable:
  2711. * (1) page's mapping marked unevictable
  2712. * (2) page is part of an mlocked VMA
  2713. *
  2714. */
  2715. int page_evictable(struct page *page, struct vm_area_struct *vma)
  2716. {
  2717. if (mapping_unevictable(page_mapping(page)))
  2718. return 0;
  2719. if (PageMlocked(page) || (vma && is_mlocked_vma(vma, page)))
  2720. return 0;
  2721. return 1;
  2722. }
  2723. /**
  2724. * check_move_unevictable_page - check page for evictability and move to appropriate zone lru list
  2725. * @page: page to check evictability and move to appropriate lru list
  2726. * @zone: zone page is in
  2727. *
  2728. * Checks a page for evictability and moves the page to the appropriate
  2729. * zone lru list.
  2730. *
  2731. * Restrictions: zone->lru_lock must be held, page must be on LRU and must
  2732. * have PageUnevictable set.
  2733. */
  2734. static void check_move_unevictable_page(struct page *page, struct zone *zone)
  2735. {
  2736. VM_BUG_ON(PageActive(page));
  2737. retry:
  2738. ClearPageUnevictable(page);
  2739. if (page_evictable(page, NULL)) {
  2740. enum lru_list l = page_lru_base_type(page);
  2741. __dec_zone_state(zone, NR_UNEVICTABLE);
  2742. list_move(&page->lru, &zone->lru[l].list);
  2743. mem_cgroup_move_lists(page, LRU_UNEVICTABLE, l);
  2744. __inc_zone_state(zone, NR_INACTIVE_ANON + l);
  2745. __count_vm_event(UNEVICTABLE_PGRESCUED);
  2746. } else {
  2747. /*
  2748. * rotate unevictable list
  2749. */
  2750. SetPageUnevictable(page);
  2751. list_move(&page->lru, &zone->lru[LRU_UNEVICTABLE].list);
  2752. mem_cgroup_rotate_lru_list(page, LRU_UNEVICTABLE);
  2753. if (page_evictable(page, NULL))
  2754. goto retry;
  2755. }
  2756. }
  2757. /**
  2758. * scan_mapping_unevictable_pages - scan an address space for evictable pages
  2759. * @mapping: struct address_space to scan for evictable pages
  2760. *
  2761. * Scan all pages in mapping. Check unevictable pages for
  2762. * evictability and move them to the appropriate zone lru list.
  2763. */
  2764. void scan_mapping_unevictable_pages(struct address_space *mapping)
  2765. {
  2766. pgoff_t next = 0;
  2767. pgoff_t end = (i_size_read(mapping->host) + PAGE_CACHE_SIZE - 1) >>
  2768. PAGE_CACHE_SHIFT;
  2769. struct zone *zone;
  2770. struct pagevec pvec;
  2771. if (mapping->nrpages == 0)
  2772. return;
  2773. pagevec_init(&pvec, 0);
  2774. while (next < end &&
  2775. pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) {
  2776. int i;
  2777. int pg_scanned = 0;
  2778. zone = NULL;
  2779. for (i = 0; i < pagevec_count(&pvec); i++) {
  2780. struct page *page = pvec.pages[i];
  2781. pgoff_t page_index = page->index;
  2782. struct zone *pagezone = page_zone(page);
  2783. pg_scanned++;
  2784. if (page_index > next)
  2785. next = page_index;
  2786. next++;
  2787. if (pagezone != zone) {
  2788. if (zone)
  2789. spin_unlock_irq(&zone->lru_lock);
  2790. zone = pagezone;
  2791. spin_lock_irq(&zone->lru_lock);
  2792. }
  2793. if (PageLRU(page) && PageUnevictable(page))
  2794. check_move_unevictable_page(page, zone);
  2795. }
  2796. if (zone)
  2797. spin_unlock_irq(&zone->lru_lock);
  2798. pagevec_release(&pvec);
  2799. count_vm_events(UNEVICTABLE_PGSCANNED, pg_scanned);
  2800. }
  2801. }
  2802. /**
  2803. * scan_zone_unevictable_pages - check unevictable list for evictable pages
  2804. * @zone - zone of which to scan the unevictable list
  2805. *
  2806. * Scan @zone's unevictable LRU lists to check for pages that have become
  2807. * evictable. Move those that have to @zone's inactive list where they
  2808. * become candidates for reclaim, unless shrink_inactive_zone() decides
  2809. * to reactivate them. Pages that are still unevictable are rotated
  2810. * back onto @zone's unevictable list.
  2811. */
  2812. #define SCAN_UNEVICTABLE_BATCH_SIZE 16UL /* arbitrary lock hold batch size */
  2813. static void scan_zone_unevictable_pages(struct zone *zone)
  2814. {
  2815. struct list_head *l_unevictable = &zone->lru[LRU_UNEVICTABLE].list;
  2816. unsigned long scan;
  2817. unsigned long nr_to_scan = zone_page_state(zone, NR_UNEVICTABLE);
  2818. while (nr_to_scan > 0) {
  2819. unsigned long batch_size = min(nr_to_scan,
  2820. SCAN_UNEVICTABLE_BATCH_SIZE);
  2821. spin_lock_irq(&zone->lru_lock);
  2822. for (scan = 0; scan < batch_size; scan++) {
  2823. struct page *page = lru_to_page(l_unevictable);
  2824. if (!trylock_page(page))
  2825. continue;
  2826. prefetchw_prev_lru_page(page, l_unevictable, flags);
  2827. if (likely(PageLRU(page) && PageUnevictable(page)))
  2828. check_move_unevictable_page(page, zone);
  2829. unlock_page(page);
  2830. }
  2831. spin_unlock_irq(&zone->lru_lock);
  2832. nr_to_scan -= batch_size;
  2833. }
  2834. }
  2835. /**
  2836. * scan_all_zones_unevictable_pages - scan all unevictable lists for evictable pages
  2837. *
  2838. * A really big hammer: scan all zones' unevictable LRU lists to check for
  2839. * pages that have become evictable. Move those back to the zones'
  2840. * inactive list where they become candidates for reclaim.
  2841. * This occurs when, e.g., we have unswappable pages on the unevictable lists,
  2842. * and we add swap to the system. As such, it runs in the context of a task
  2843. * that has possibly/probably made some previously unevictable pages
  2844. * evictable.
  2845. */
  2846. static void scan_all_zones_unevictable_pages(void)
  2847. {
  2848. struct zone *zone;
  2849. for_each_zone(zone) {
  2850. scan_zone_unevictable_pages(zone);
  2851. }
  2852. }
  2853. /*
  2854. * scan_unevictable_pages [vm] sysctl handler. On demand re-scan of
  2855. * all nodes' unevictable lists for evictable pages
  2856. */
  2857. unsigned long scan_unevictable_pages;
  2858. int scan_unevictable_handler(struct ctl_table *table, int write,
  2859. void __user *buffer,
  2860. size_t *length, loff_t *ppos)
  2861. {
  2862. proc_doulongvec_minmax(table, write, buffer, length, ppos);
  2863. if (write && *(unsigned long *)table->data)
  2864. scan_all_zones_unevictable_pages();
  2865. scan_unevictable_pages = 0;
  2866. return 0;
  2867. }
  2868. #ifdef CONFIG_NUMA
  2869. /*
  2870. * per node 'scan_unevictable_pages' attribute. On demand re-scan of
  2871. * a specified node's per zone unevictable lists for evictable pages.
  2872. */
  2873. static ssize_t read_scan_unevictable_node(struct sys_device *dev,
  2874. struct sysdev_attribute *attr,
  2875. char *buf)
  2876. {
  2877. return sprintf(buf, "0\n"); /* always zero; should fit... */
  2878. }
  2879. static ssize_t write_scan_unevictable_node(struct sys_device *dev,
  2880. struct sysdev_attribute *attr,
  2881. const char *buf, size_t count)
  2882. {
  2883. struct zone *node_zones = NODE_DATA(dev->id)->node_zones;
  2884. struct zone *zone;
  2885. unsigned long res;
  2886. unsigned long req = strict_strtoul(buf, 10, &res);
  2887. if (!req)
  2888. return 1; /* zero is no-op */
  2889. for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
  2890. if (!populated_zone(zone))
  2891. continue;
  2892. scan_zone_unevictable_pages(zone);
  2893. }
  2894. return 1;
  2895. }
  2896. static SYSDEV_ATTR(scan_unevictable_pages, S_IRUGO | S_IWUSR,
  2897. read_scan_unevictable_node,
  2898. write_scan_unevictable_node);
  2899. int scan_unevictable_register_node(struct node *node)
  2900. {
  2901. return sysdev_create_file(&node->sysdev, &attr_scan_unevictable_pages);
  2902. }
  2903. void scan_unevictable_unregister_node(struct node *node)
  2904. {
  2905. sysdev_remove_file(&node->sysdev, &attr_scan_unevictable_pages);
  2906. }
  2907. #endif