vmscan.c 98 KB

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