vmscan.c 96 KB

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