vmscan.c 83 KB

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