vmscan.c 84 KB

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