vmscan.c 70 KB

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