vmscan.c 93 KB

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