vmscan.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840
  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 <asm/tlbflush.h>
  40. #include <asm/div64.h>
  41. #include <linux/swapops.h>
  42. #include "internal.h"
  43. struct scan_control {
  44. /* Incremented by the number of inactive pages that were scanned */
  45. unsigned long nr_scanned;
  46. /* This context's GFP mask */
  47. gfp_t gfp_mask;
  48. int may_writepage;
  49. /* Can pages be swapped as part of reclaim? */
  50. int may_swap;
  51. /* This context's SWAP_CLUSTER_MAX. If freeing memory for
  52. * suspend, we effectively ignore SWAP_CLUSTER_MAX.
  53. * In this context, it doesn't matter that we scan the
  54. * whole list at once. */
  55. int swap_cluster_max;
  56. int swappiness;
  57. int all_unreclaimable;
  58. int order;
  59. };
  60. #define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru))
  61. #ifdef ARCH_HAS_PREFETCH
  62. #define prefetch_prev_lru_page(_page, _base, _field) \
  63. do { \
  64. if ((_page)->lru.prev != _base) { \
  65. struct page *prev; \
  66. \
  67. prev = lru_to_page(&(_page->lru)); \
  68. prefetch(&prev->_field); \
  69. } \
  70. } while (0)
  71. #else
  72. #define prefetch_prev_lru_page(_page, _base, _field) do { } while (0)
  73. #endif
  74. #ifdef ARCH_HAS_PREFETCHW
  75. #define prefetchw_prev_lru_page(_page, _base, _field) \
  76. do { \
  77. if ((_page)->lru.prev != _base) { \
  78. struct page *prev; \
  79. \
  80. prev = lru_to_page(&(_page->lru)); \
  81. prefetchw(&prev->_field); \
  82. } \
  83. } while (0)
  84. #else
  85. #define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0)
  86. #endif
  87. /*
  88. * From 0 .. 100. Higher means more swappy.
  89. */
  90. int vm_swappiness = 60;
  91. long vm_total_pages; /* The total number of pages which the VM controls */
  92. static LIST_HEAD(shrinker_list);
  93. static DECLARE_RWSEM(shrinker_rwsem);
  94. /*
  95. * Add a shrinker callback to be called from the vm
  96. */
  97. void register_shrinker(struct shrinker *shrinker)
  98. {
  99. shrinker->nr = 0;
  100. down_write(&shrinker_rwsem);
  101. list_add_tail(&shrinker->list, &shrinker_list);
  102. up_write(&shrinker_rwsem);
  103. }
  104. EXPORT_SYMBOL(register_shrinker);
  105. /*
  106. * Remove one
  107. */
  108. void unregister_shrinker(struct shrinker *shrinker)
  109. {
  110. down_write(&shrinker_rwsem);
  111. list_del(&shrinker->list);
  112. up_write(&shrinker_rwsem);
  113. }
  114. EXPORT_SYMBOL(unregister_shrinker);
  115. #define SHRINK_BATCH 128
  116. /*
  117. * Call the shrink functions to age shrinkable caches
  118. *
  119. * Here we assume it costs one seek to replace a lru page and that it also
  120. * takes a seek to recreate a cache object. With this in mind we age equal
  121. * percentages of the lru and ageable caches. This should balance the seeks
  122. * generated by these structures.
  123. *
  124. * If the vm encounted mapped pages on the LRU it increase the pressure on
  125. * slab to avoid swapping.
  126. *
  127. * We do weird things to avoid (scanned*seeks*entries) overflowing 32 bits.
  128. *
  129. * `lru_pages' represents the number of on-LRU pages in all the zones which
  130. * are eligible for the caller's allocation attempt. It is used for balancing
  131. * slab reclaim versus page reclaim.
  132. *
  133. * Returns the number of slab objects which we shrunk.
  134. */
  135. unsigned long shrink_slab(unsigned long scanned, gfp_t gfp_mask,
  136. unsigned long lru_pages)
  137. {
  138. struct shrinker *shrinker;
  139. unsigned long ret = 0;
  140. if (scanned == 0)
  141. scanned = SWAP_CLUSTER_MAX;
  142. if (!down_read_trylock(&shrinker_rwsem))
  143. return 1; /* Assume we'll be able to shrink next time */
  144. list_for_each_entry(shrinker, &shrinker_list, list) {
  145. unsigned long long delta;
  146. unsigned long total_scan;
  147. unsigned long max_pass = (*shrinker->shrink)(0, gfp_mask);
  148. delta = (4 * scanned) / shrinker->seeks;
  149. delta *= max_pass;
  150. do_div(delta, lru_pages + 1);
  151. shrinker->nr += delta;
  152. if (shrinker->nr < 0) {
  153. printk(KERN_ERR "%s: nr=%ld\n",
  154. __FUNCTION__, shrinker->nr);
  155. shrinker->nr = max_pass;
  156. }
  157. /*
  158. * Avoid risking looping forever due to too large nr value:
  159. * never try to free more than twice the estimate number of
  160. * freeable entries.
  161. */
  162. if (shrinker->nr > max_pass * 2)
  163. shrinker->nr = max_pass * 2;
  164. total_scan = shrinker->nr;
  165. shrinker->nr = 0;
  166. while (total_scan >= SHRINK_BATCH) {
  167. long this_scan = SHRINK_BATCH;
  168. int shrink_ret;
  169. int nr_before;
  170. nr_before = (*shrinker->shrink)(0, gfp_mask);
  171. shrink_ret = (*shrinker->shrink)(this_scan, gfp_mask);
  172. if (shrink_ret == -1)
  173. break;
  174. if (shrink_ret < nr_before)
  175. ret += nr_before - shrink_ret;
  176. count_vm_events(SLABS_SCANNED, this_scan);
  177. total_scan -= this_scan;
  178. cond_resched();
  179. }
  180. shrinker->nr += total_scan;
  181. }
  182. up_read(&shrinker_rwsem);
  183. return ret;
  184. }
  185. /* Called without lock on whether page is mapped, so answer is unstable */
  186. static inline int page_mapping_inuse(struct page *page)
  187. {
  188. struct address_space *mapping;
  189. /* Page is in somebody's page tables. */
  190. if (page_mapped(page))
  191. return 1;
  192. /* Be more reluctant to reclaim swapcache than pagecache */
  193. if (PageSwapCache(page))
  194. return 1;
  195. mapping = page_mapping(page);
  196. if (!mapping)
  197. return 0;
  198. /* File is mmap'd by somebody? */
  199. return mapping_mapped(mapping);
  200. }
  201. static inline int is_page_cache_freeable(struct page *page)
  202. {
  203. return page_count(page) - !!PagePrivate(page) == 2;
  204. }
  205. static int may_write_to_queue(struct backing_dev_info *bdi)
  206. {
  207. if (current->flags & PF_SWAPWRITE)
  208. return 1;
  209. if (!bdi_write_congested(bdi))
  210. return 1;
  211. if (bdi == current->backing_dev_info)
  212. return 1;
  213. return 0;
  214. }
  215. /*
  216. * We detected a synchronous write error writing a page out. Probably
  217. * -ENOSPC. We need to propagate that into the address_space for a subsequent
  218. * fsync(), msync() or close().
  219. *
  220. * The tricky part is that after writepage we cannot touch the mapping: nothing
  221. * prevents it from being freed up. But we have a ref on the page and once
  222. * that page is locked, the mapping is pinned.
  223. *
  224. * We're allowed to run sleeping lock_page() here because we know the caller has
  225. * __GFP_FS.
  226. */
  227. static void handle_write_error(struct address_space *mapping,
  228. struct page *page, int error)
  229. {
  230. lock_page(page);
  231. if (page_mapping(page) == mapping)
  232. mapping_set_error(mapping, error);
  233. unlock_page(page);
  234. }
  235. /* possible outcome of pageout() */
  236. typedef enum {
  237. /* failed to write page out, page is locked */
  238. PAGE_KEEP,
  239. /* move page to the active list, page is locked */
  240. PAGE_ACTIVATE,
  241. /* page has been sent to the disk successfully, page is unlocked */
  242. PAGE_SUCCESS,
  243. /* page is clean and locked */
  244. PAGE_CLEAN,
  245. } pageout_t;
  246. /*
  247. * pageout is called by shrink_page_list() for each dirty page.
  248. * Calls ->writepage().
  249. */
  250. static pageout_t pageout(struct page *page, struct address_space *mapping)
  251. {
  252. /*
  253. * If the page is dirty, only perform writeback if that write
  254. * will be non-blocking. To prevent this allocation from being
  255. * stalled by pagecache activity. But note that there may be
  256. * stalls if we need to run get_block(). We could test
  257. * PagePrivate for that.
  258. *
  259. * If this process is currently in generic_file_write() against
  260. * this page's queue, we can perform writeback even if that
  261. * will block.
  262. *
  263. * If the page is swapcache, write it back even if that would
  264. * block, for some throttling. This happens by accident, because
  265. * swap_backing_dev_info is bust: it doesn't reflect the
  266. * congestion state of the swapdevs. Easy to fix, if needed.
  267. * See swapfile.c:page_queue_congested().
  268. */
  269. if (!is_page_cache_freeable(page))
  270. return PAGE_KEEP;
  271. if (!mapping) {
  272. /*
  273. * Some data journaling orphaned pages can have
  274. * page->mapping == NULL while being dirty with clean buffers.
  275. */
  276. if (PagePrivate(page)) {
  277. if (try_to_free_buffers(page)) {
  278. ClearPageDirty(page);
  279. printk("%s: orphaned page\n", __FUNCTION__);
  280. return PAGE_CLEAN;
  281. }
  282. }
  283. return PAGE_KEEP;
  284. }
  285. if (mapping->a_ops->writepage == NULL)
  286. return PAGE_ACTIVATE;
  287. if (!may_write_to_queue(mapping->backing_dev_info))
  288. return PAGE_KEEP;
  289. if (clear_page_dirty_for_io(page)) {
  290. int res;
  291. struct writeback_control wbc = {
  292. .sync_mode = WB_SYNC_NONE,
  293. .nr_to_write = SWAP_CLUSTER_MAX,
  294. .range_start = 0,
  295. .range_end = LLONG_MAX,
  296. .nonblocking = 1,
  297. .for_reclaim = 1,
  298. };
  299. SetPageReclaim(page);
  300. res = mapping->a_ops->writepage(page, &wbc);
  301. if (res < 0)
  302. handle_write_error(mapping, page, res);
  303. if (res == AOP_WRITEPAGE_ACTIVATE) {
  304. ClearPageReclaim(page);
  305. return PAGE_ACTIVATE;
  306. }
  307. if (!PageWriteback(page)) {
  308. /* synchronous write or broken a_ops? */
  309. ClearPageReclaim(page);
  310. }
  311. inc_zone_page_state(page, NR_VMSCAN_WRITE);
  312. return PAGE_SUCCESS;
  313. }
  314. return PAGE_CLEAN;
  315. }
  316. /*
  317. * Attempt to detach a locked page from its ->mapping. If it is dirty or if
  318. * someone else has a ref on the page, abort and return 0. If it was
  319. * successfully detached, return 1. Assumes the caller has a single ref on
  320. * this page.
  321. */
  322. int remove_mapping(struct address_space *mapping, struct page *page)
  323. {
  324. BUG_ON(!PageLocked(page));
  325. BUG_ON(mapping != page_mapping(page));
  326. write_lock_irq(&mapping->tree_lock);
  327. /*
  328. * The non racy check for a busy page.
  329. *
  330. * Must be careful with the order of the tests. When someone has
  331. * a ref to the page, it may be possible that they dirty it then
  332. * drop the reference. So if PageDirty is tested before page_count
  333. * here, then the following race may occur:
  334. *
  335. * get_user_pages(&page);
  336. * [user mapping goes away]
  337. * write_to(page);
  338. * !PageDirty(page) [good]
  339. * SetPageDirty(page);
  340. * put_page(page);
  341. * !page_count(page) [good, discard it]
  342. *
  343. * [oops, our write_to data is lost]
  344. *
  345. * Reversing the order of the tests ensures such a situation cannot
  346. * escape unnoticed. The smp_rmb is needed to ensure the page->flags
  347. * load is not satisfied before that of page->_count.
  348. *
  349. * Note that if SetPageDirty is always performed via set_page_dirty,
  350. * and thus under tree_lock, then this ordering is not required.
  351. */
  352. if (unlikely(page_count(page) != 2))
  353. goto cannot_free;
  354. smp_rmb();
  355. if (unlikely(PageDirty(page)))
  356. goto cannot_free;
  357. if (PageSwapCache(page)) {
  358. swp_entry_t swap = { .val = page_private(page) };
  359. __delete_from_swap_cache(page);
  360. write_unlock_irq(&mapping->tree_lock);
  361. swap_free(swap);
  362. __put_page(page); /* The pagecache ref */
  363. return 1;
  364. }
  365. __remove_from_page_cache(page);
  366. write_unlock_irq(&mapping->tree_lock);
  367. __put_page(page);
  368. return 1;
  369. cannot_free:
  370. write_unlock_irq(&mapping->tree_lock);
  371. return 0;
  372. }
  373. /*
  374. * shrink_page_list() returns the number of reclaimed pages
  375. */
  376. static unsigned long shrink_page_list(struct list_head *page_list,
  377. struct scan_control *sc)
  378. {
  379. LIST_HEAD(ret_pages);
  380. struct pagevec freed_pvec;
  381. int pgactivate = 0;
  382. unsigned long nr_reclaimed = 0;
  383. cond_resched();
  384. pagevec_init(&freed_pvec, 1);
  385. while (!list_empty(page_list)) {
  386. struct address_space *mapping;
  387. struct page *page;
  388. int may_enter_fs;
  389. int referenced;
  390. cond_resched();
  391. page = lru_to_page(page_list);
  392. list_del(&page->lru);
  393. if (TestSetPageLocked(page))
  394. goto keep;
  395. VM_BUG_ON(PageActive(page));
  396. sc->nr_scanned++;
  397. if (!sc->may_swap && page_mapped(page))
  398. goto keep_locked;
  399. /* Double the slab pressure for mapped and swapcache pages */
  400. if (page_mapped(page) || PageSwapCache(page))
  401. sc->nr_scanned++;
  402. if (PageWriteback(page))
  403. goto keep_locked;
  404. referenced = page_referenced(page, 1);
  405. /* In active use or really unfreeable? Activate it. */
  406. if (sc->order <= PAGE_ALLOC_COSTLY_ORDER &&
  407. referenced && page_mapping_inuse(page))
  408. goto activate_locked;
  409. #ifdef CONFIG_SWAP
  410. /*
  411. * Anonymous process memory has backing store?
  412. * Try to allocate it some swap space here.
  413. */
  414. if (PageAnon(page) && !PageSwapCache(page))
  415. if (!add_to_swap(page, GFP_ATOMIC))
  416. goto activate_locked;
  417. #endif /* CONFIG_SWAP */
  418. mapping = page_mapping(page);
  419. may_enter_fs = (sc->gfp_mask & __GFP_FS) ||
  420. (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO));
  421. /*
  422. * The page is mapped into the page tables of one or more
  423. * processes. Try to unmap it here.
  424. */
  425. if (page_mapped(page) && mapping) {
  426. switch (try_to_unmap(page, 0)) {
  427. case SWAP_FAIL:
  428. goto activate_locked;
  429. case SWAP_AGAIN:
  430. goto keep_locked;
  431. case SWAP_SUCCESS:
  432. ; /* try to free the page below */
  433. }
  434. }
  435. if (PageDirty(page)) {
  436. if (sc->order <= PAGE_ALLOC_COSTLY_ORDER && referenced)
  437. goto keep_locked;
  438. if (!may_enter_fs)
  439. goto keep_locked;
  440. if (!sc->may_writepage)
  441. goto keep_locked;
  442. /* Page is dirty, try to write it out here */
  443. switch(pageout(page, mapping)) {
  444. case PAGE_KEEP:
  445. goto keep_locked;
  446. case PAGE_ACTIVATE:
  447. goto activate_locked;
  448. case PAGE_SUCCESS:
  449. if (PageWriteback(page) || PageDirty(page))
  450. goto keep;
  451. /*
  452. * A synchronous write - probably a ramdisk. Go
  453. * ahead and try to reclaim the page.
  454. */
  455. if (TestSetPageLocked(page))
  456. goto keep;
  457. if (PageDirty(page) || PageWriteback(page))
  458. goto keep_locked;
  459. mapping = page_mapping(page);
  460. case PAGE_CLEAN:
  461. ; /* try to free the page below */
  462. }
  463. }
  464. /*
  465. * If the page has buffers, try to free the buffer mappings
  466. * associated with this page. If we succeed we try to free
  467. * the page as well.
  468. *
  469. * We do this even if the page is PageDirty().
  470. * try_to_release_page() does not perform I/O, but it is
  471. * possible for a page to have PageDirty set, but it is actually
  472. * clean (all its buffers are clean). This happens if the
  473. * buffers were written out directly, with submit_bh(). ext3
  474. * will do this, as well as the blockdev mapping.
  475. * try_to_release_page() will discover that cleanness and will
  476. * drop the buffers and mark the page clean - it can be freed.
  477. *
  478. * Rarely, pages can have buffers and no ->mapping. These are
  479. * the pages which were not successfully invalidated in
  480. * truncate_complete_page(). We try to drop those buffers here
  481. * and if that worked, and the page is no longer mapped into
  482. * process address space (page_count == 1) it can be freed.
  483. * Otherwise, leave the page on the LRU so it is swappable.
  484. */
  485. if (PagePrivate(page)) {
  486. if (!try_to_release_page(page, sc->gfp_mask))
  487. goto activate_locked;
  488. if (!mapping && page_count(page) == 1)
  489. goto free_it;
  490. }
  491. if (!mapping || !remove_mapping(mapping, page))
  492. goto keep_locked;
  493. free_it:
  494. unlock_page(page);
  495. nr_reclaimed++;
  496. if (!pagevec_add(&freed_pvec, page))
  497. __pagevec_release_nonlru(&freed_pvec);
  498. continue;
  499. activate_locked:
  500. SetPageActive(page);
  501. pgactivate++;
  502. keep_locked:
  503. unlock_page(page);
  504. keep:
  505. list_add(&page->lru, &ret_pages);
  506. VM_BUG_ON(PageLRU(page));
  507. }
  508. list_splice(&ret_pages, page_list);
  509. if (pagevec_count(&freed_pvec))
  510. __pagevec_release_nonlru(&freed_pvec);
  511. count_vm_events(PGACTIVATE, pgactivate);
  512. return nr_reclaimed;
  513. }
  514. /* LRU Isolation modes. */
  515. #define ISOLATE_INACTIVE 0 /* Isolate inactive pages. */
  516. #define ISOLATE_ACTIVE 1 /* Isolate active pages. */
  517. #define ISOLATE_BOTH 2 /* Isolate both active and inactive pages. */
  518. /*
  519. * Attempt to remove the specified page from its LRU. Only take this page
  520. * if it is of the appropriate PageActive status. Pages which are being
  521. * freed elsewhere are also ignored.
  522. *
  523. * page: page to consider
  524. * mode: one of the LRU isolation modes defined above
  525. *
  526. * returns 0 on success, -ve errno on failure.
  527. */
  528. static int __isolate_lru_page(struct page *page, int mode)
  529. {
  530. int ret = -EINVAL;
  531. /* Only take pages on the LRU. */
  532. if (!PageLRU(page))
  533. return ret;
  534. /*
  535. * When checking the active state, we need to be sure we are
  536. * dealing with comparible boolean values. Take the logical not
  537. * of each.
  538. */
  539. if (mode != ISOLATE_BOTH && (!PageActive(page) != !mode))
  540. return ret;
  541. ret = -EBUSY;
  542. if (likely(get_page_unless_zero(page))) {
  543. /*
  544. * Be careful not to clear PageLRU until after we're
  545. * sure the page is not being freed elsewhere -- the
  546. * page release code relies on it.
  547. */
  548. ClearPageLRU(page);
  549. ret = 0;
  550. }
  551. return ret;
  552. }
  553. /*
  554. * zone->lru_lock is heavily contended. Some of the functions that
  555. * shrink the lists perform better by taking out a batch of pages
  556. * and working on them outside the LRU lock.
  557. *
  558. * For pagecache intensive workloads, this function is the hottest
  559. * spot in the kernel (apart from copy_*_user functions).
  560. *
  561. * Appropriate locks must be held before calling this function.
  562. *
  563. * @nr_to_scan: The number of pages to look through on the list.
  564. * @src: The LRU list to pull pages off.
  565. * @dst: The temp list to put pages on to.
  566. * @scanned: The number of pages that were scanned.
  567. * @order: The caller's attempted allocation order
  568. * @mode: One of the LRU isolation modes
  569. *
  570. * returns how many pages were moved onto *@dst.
  571. */
  572. static unsigned long isolate_lru_pages(unsigned long nr_to_scan,
  573. struct list_head *src, struct list_head *dst,
  574. unsigned long *scanned, int order, int mode)
  575. {
  576. unsigned long nr_taken = 0;
  577. unsigned long scan;
  578. for (scan = 0; scan < nr_to_scan && !list_empty(src); scan++) {
  579. struct page *page;
  580. unsigned long pfn;
  581. unsigned long end_pfn;
  582. unsigned long page_pfn;
  583. int zone_id;
  584. page = lru_to_page(src);
  585. prefetchw_prev_lru_page(page, src, flags);
  586. VM_BUG_ON(!PageLRU(page));
  587. switch (__isolate_lru_page(page, mode)) {
  588. case 0:
  589. list_move(&page->lru, dst);
  590. nr_taken++;
  591. break;
  592. case -EBUSY:
  593. /* else it is being freed elsewhere */
  594. list_move(&page->lru, src);
  595. continue;
  596. default:
  597. BUG();
  598. }
  599. if (!order)
  600. continue;
  601. /*
  602. * Attempt to take all pages in the order aligned region
  603. * surrounding the tag page. Only take those pages of
  604. * the same active state as that tag page. We may safely
  605. * round the target page pfn down to the requested order
  606. * as the mem_map is guarenteed valid out to MAX_ORDER,
  607. * where that page is in a different zone we will detect
  608. * it from its zone id and abort this block scan.
  609. */
  610. zone_id = page_zone_id(page);
  611. page_pfn = page_to_pfn(page);
  612. pfn = page_pfn & ~((1 << order) - 1);
  613. end_pfn = pfn + (1 << order);
  614. for (; pfn < end_pfn; pfn++) {
  615. struct page *cursor_page;
  616. /* The target page is in the block, ignore it. */
  617. if (unlikely(pfn == page_pfn))
  618. continue;
  619. /* Avoid holes within the zone. */
  620. if (unlikely(!pfn_valid_within(pfn)))
  621. break;
  622. cursor_page = pfn_to_page(pfn);
  623. /* Check that we have not crossed a zone boundary. */
  624. if (unlikely(page_zone_id(cursor_page) != zone_id))
  625. continue;
  626. switch (__isolate_lru_page(cursor_page, mode)) {
  627. case 0:
  628. list_move(&cursor_page->lru, dst);
  629. nr_taken++;
  630. scan++;
  631. break;
  632. case -EBUSY:
  633. /* else it is being freed elsewhere */
  634. list_move(&cursor_page->lru, src);
  635. default:
  636. break;
  637. }
  638. }
  639. }
  640. *scanned = scan;
  641. return nr_taken;
  642. }
  643. /*
  644. * clear_active_flags() is a helper for shrink_active_list(), clearing
  645. * any active bits from the pages in the list.
  646. */
  647. static unsigned long clear_active_flags(struct list_head *page_list)
  648. {
  649. int nr_active = 0;
  650. struct page *page;
  651. list_for_each_entry(page, page_list, lru)
  652. if (PageActive(page)) {
  653. ClearPageActive(page);
  654. nr_active++;
  655. }
  656. return nr_active;
  657. }
  658. /*
  659. * shrink_inactive_list() is a helper for shrink_zone(). It returns the number
  660. * of reclaimed pages
  661. */
  662. static unsigned long shrink_inactive_list(unsigned long max_scan,
  663. struct zone *zone, struct scan_control *sc)
  664. {
  665. LIST_HEAD(page_list);
  666. struct pagevec pvec;
  667. unsigned long nr_scanned = 0;
  668. unsigned long nr_reclaimed = 0;
  669. pagevec_init(&pvec, 1);
  670. lru_add_drain();
  671. spin_lock_irq(&zone->lru_lock);
  672. do {
  673. struct page *page;
  674. unsigned long nr_taken;
  675. unsigned long nr_scan;
  676. unsigned long nr_freed;
  677. unsigned long nr_active;
  678. nr_taken = isolate_lru_pages(sc->swap_cluster_max,
  679. &zone->inactive_list,
  680. &page_list, &nr_scan, sc->order,
  681. (sc->order > PAGE_ALLOC_COSTLY_ORDER)?
  682. ISOLATE_BOTH : ISOLATE_INACTIVE);
  683. nr_active = clear_active_flags(&page_list);
  684. __count_vm_events(PGDEACTIVATE, nr_active);
  685. __mod_zone_page_state(zone, NR_ACTIVE, -nr_active);
  686. __mod_zone_page_state(zone, NR_INACTIVE,
  687. -(nr_taken - nr_active));
  688. zone->pages_scanned += nr_scan;
  689. spin_unlock_irq(&zone->lru_lock);
  690. nr_scanned += nr_scan;
  691. nr_freed = shrink_page_list(&page_list, sc);
  692. nr_reclaimed += nr_freed;
  693. local_irq_disable();
  694. if (current_is_kswapd()) {
  695. __count_zone_vm_events(PGSCAN_KSWAPD, zone, nr_scan);
  696. __count_vm_events(KSWAPD_STEAL, nr_freed);
  697. } else
  698. __count_zone_vm_events(PGSCAN_DIRECT, zone, nr_scan);
  699. __count_zone_vm_events(PGSTEAL, zone, nr_freed);
  700. if (nr_taken == 0)
  701. goto done;
  702. spin_lock(&zone->lru_lock);
  703. /*
  704. * Put back any unfreeable pages.
  705. */
  706. while (!list_empty(&page_list)) {
  707. page = lru_to_page(&page_list);
  708. VM_BUG_ON(PageLRU(page));
  709. SetPageLRU(page);
  710. list_del(&page->lru);
  711. if (PageActive(page))
  712. add_page_to_active_list(zone, page);
  713. else
  714. add_page_to_inactive_list(zone, page);
  715. if (!pagevec_add(&pvec, page)) {
  716. spin_unlock_irq(&zone->lru_lock);
  717. __pagevec_release(&pvec);
  718. spin_lock_irq(&zone->lru_lock);
  719. }
  720. }
  721. } while (nr_scanned < max_scan);
  722. spin_unlock(&zone->lru_lock);
  723. done:
  724. local_irq_enable();
  725. pagevec_release(&pvec);
  726. return nr_reclaimed;
  727. }
  728. /*
  729. * We are about to scan this zone at a certain priority level. If that priority
  730. * level is smaller (ie: more urgent) than the previous priority, then note
  731. * that priority level within the zone. This is done so that when the next
  732. * process comes in to scan this zone, it will immediately start out at this
  733. * priority level rather than having to build up its own scanning priority.
  734. * Here, this priority affects only the reclaim-mapped threshold.
  735. */
  736. static inline void note_zone_scanning_priority(struct zone *zone, int priority)
  737. {
  738. if (priority < zone->prev_priority)
  739. zone->prev_priority = priority;
  740. }
  741. static inline int zone_is_near_oom(struct zone *zone)
  742. {
  743. return zone->pages_scanned >= (zone_page_state(zone, NR_ACTIVE)
  744. + zone_page_state(zone, NR_INACTIVE))*3;
  745. }
  746. /*
  747. * This moves pages from the active list to the inactive list.
  748. *
  749. * We move them the other way if the page is referenced by one or more
  750. * processes, from rmap.
  751. *
  752. * If the pages are mostly unmapped, the processing is fast and it is
  753. * appropriate to hold zone->lru_lock across the whole operation. But if
  754. * the pages are mapped, the processing is slow (page_referenced()) so we
  755. * should drop zone->lru_lock around each page. It's impossible to balance
  756. * this, so instead we remove the pages from the LRU while processing them.
  757. * It is safe to rely on PG_active against the non-LRU pages in here because
  758. * nobody will play with that bit on a non-LRU page.
  759. *
  760. * The downside is that we have to touch page->_count against each page.
  761. * But we had to alter page->flags anyway.
  762. */
  763. static void shrink_active_list(unsigned long nr_pages, struct zone *zone,
  764. struct scan_control *sc, int priority)
  765. {
  766. unsigned long pgmoved;
  767. int pgdeactivate = 0;
  768. unsigned long pgscanned;
  769. LIST_HEAD(l_hold); /* The pages which were snipped off */
  770. LIST_HEAD(l_inactive); /* Pages to go onto the inactive_list */
  771. LIST_HEAD(l_active); /* Pages to go onto the active_list */
  772. struct page *page;
  773. struct pagevec pvec;
  774. int reclaim_mapped = 0;
  775. if (sc->may_swap) {
  776. long mapped_ratio;
  777. long distress;
  778. long swap_tendency;
  779. if (zone_is_near_oom(zone))
  780. goto force_reclaim_mapped;
  781. /*
  782. * `distress' is a measure of how much trouble we're having
  783. * reclaiming pages. 0 -> no problems. 100 -> great trouble.
  784. */
  785. distress = 100 >> min(zone->prev_priority, priority);
  786. /*
  787. * The point of this algorithm is to decide when to start
  788. * reclaiming mapped memory instead of just pagecache. Work out
  789. * how much memory
  790. * is mapped.
  791. */
  792. mapped_ratio = ((global_page_state(NR_FILE_MAPPED) +
  793. global_page_state(NR_ANON_PAGES)) * 100) /
  794. vm_total_pages;
  795. /*
  796. * Now decide how much we really want to unmap some pages. The
  797. * mapped ratio is downgraded - just because there's a lot of
  798. * mapped memory doesn't necessarily mean that page reclaim
  799. * isn't succeeding.
  800. *
  801. * The distress ratio is important - we don't want to start
  802. * going oom.
  803. *
  804. * A 100% value of vm_swappiness overrides this algorithm
  805. * altogether.
  806. */
  807. swap_tendency = mapped_ratio / 2 + distress + sc->swappiness;
  808. /*
  809. * Now use this metric to decide whether to start moving mapped
  810. * memory onto the inactive list.
  811. */
  812. if (swap_tendency >= 100)
  813. force_reclaim_mapped:
  814. reclaim_mapped = 1;
  815. }
  816. lru_add_drain();
  817. spin_lock_irq(&zone->lru_lock);
  818. pgmoved = isolate_lru_pages(nr_pages, &zone->active_list,
  819. &l_hold, &pgscanned, sc->order, ISOLATE_ACTIVE);
  820. zone->pages_scanned += pgscanned;
  821. __mod_zone_page_state(zone, NR_ACTIVE, -pgmoved);
  822. spin_unlock_irq(&zone->lru_lock);
  823. while (!list_empty(&l_hold)) {
  824. cond_resched();
  825. page = lru_to_page(&l_hold);
  826. list_del(&page->lru);
  827. if (page_mapped(page)) {
  828. if (!reclaim_mapped ||
  829. (total_swap_pages == 0 && PageAnon(page)) ||
  830. page_referenced(page, 0)) {
  831. list_add(&page->lru, &l_active);
  832. continue;
  833. }
  834. }
  835. list_add(&page->lru, &l_inactive);
  836. }
  837. pagevec_init(&pvec, 1);
  838. pgmoved = 0;
  839. spin_lock_irq(&zone->lru_lock);
  840. while (!list_empty(&l_inactive)) {
  841. page = lru_to_page(&l_inactive);
  842. prefetchw_prev_lru_page(page, &l_inactive, flags);
  843. VM_BUG_ON(PageLRU(page));
  844. SetPageLRU(page);
  845. VM_BUG_ON(!PageActive(page));
  846. ClearPageActive(page);
  847. list_move(&page->lru, &zone->inactive_list);
  848. pgmoved++;
  849. if (!pagevec_add(&pvec, page)) {
  850. __mod_zone_page_state(zone, NR_INACTIVE, pgmoved);
  851. spin_unlock_irq(&zone->lru_lock);
  852. pgdeactivate += pgmoved;
  853. pgmoved = 0;
  854. if (buffer_heads_over_limit)
  855. pagevec_strip(&pvec);
  856. __pagevec_release(&pvec);
  857. spin_lock_irq(&zone->lru_lock);
  858. }
  859. }
  860. __mod_zone_page_state(zone, NR_INACTIVE, pgmoved);
  861. pgdeactivate += pgmoved;
  862. if (buffer_heads_over_limit) {
  863. spin_unlock_irq(&zone->lru_lock);
  864. pagevec_strip(&pvec);
  865. spin_lock_irq(&zone->lru_lock);
  866. }
  867. pgmoved = 0;
  868. while (!list_empty(&l_active)) {
  869. page = lru_to_page(&l_active);
  870. prefetchw_prev_lru_page(page, &l_active, flags);
  871. VM_BUG_ON(PageLRU(page));
  872. SetPageLRU(page);
  873. VM_BUG_ON(!PageActive(page));
  874. list_move(&page->lru, &zone->active_list);
  875. pgmoved++;
  876. if (!pagevec_add(&pvec, page)) {
  877. __mod_zone_page_state(zone, NR_ACTIVE, pgmoved);
  878. pgmoved = 0;
  879. spin_unlock_irq(&zone->lru_lock);
  880. __pagevec_release(&pvec);
  881. spin_lock_irq(&zone->lru_lock);
  882. }
  883. }
  884. __mod_zone_page_state(zone, NR_ACTIVE, pgmoved);
  885. __count_zone_vm_events(PGREFILL, zone, pgscanned);
  886. __count_vm_events(PGDEACTIVATE, pgdeactivate);
  887. spin_unlock_irq(&zone->lru_lock);
  888. pagevec_release(&pvec);
  889. }
  890. /*
  891. * This is a basic per-zone page freer. Used by both kswapd and direct reclaim.
  892. */
  893. static unsigned long shrink_zone(int priority, struct zone *zone,
  894. struct scan_control *sc)
  895. {
  896. unsigned long nr_active;
  897. unsigned long nr_inactive;
  898. unsigned long nr_to_scan;
  899. unsigned long nr_reclaimed = 0;
  900. atomic_inc(&zone->reclaim_in_progress);
  901. /*
  902. * Add one to `nr_to_scan' just to make sure that the kernel will
  903. * slowly sift through the active list.
  904. */
  905. zone->nr_scan_active +=
  906. (zone_page_state(zone, NR_ACTIVE) >> priority) + 1;
  907. nr_active = zone->nr_scan_active;
  908. if (nr_active >= sc->swap_cluster_max)
  909. zone->nr_scan_active = 0;
  910. else
  911. nr_active = 0;
  912. zone->nr_scan_inactive +=
  913. (zone_page_state(zone, NR_INACTIVE) >> priority) + 1;
  914. nr_inactive = zone->nr_scan_inactive;
  915. if (nr_inactive >= sc->swap_cluster_max)
  916. zone->nr_scan_inactive = 0;
  917. else
  918. nr_inactive = 0;
  919. while (nr_active || nr_inactive) {
  920. if (nr_active) {
  921. nr_to_scan = min(nr_active,
  922. (unsigned long)sc->swap_cluster_max);
  923. nr_active -= nr_to_scan;
  924. shrink_active_list(nr_to_scan, zone, sc, priority);
  925. }
  926. if (nr_inactive) {
  927. nr_to_scan = min(nr_inactive,
  928. (unsigned long)sc->swap_cluster_max);
  929. nr_inactive -= nr_to_scan;
  930. nr_reclaimed += shrink_inactive_list(nr_to_scan, zone,
  931. sc);
  932. }
  933. }
  934. throttle_vm_writeout(sc->gfp_mask);
  935. atomic_dec(&zone->reclaim_in_progress);
  936. return nr_reclaimed;
  937. }
  938. /*
  939. * This is the direct reclaim path, for page-allocating processes. We only
  940. * try to reclaim pages from zones which will satisfy the caller's allocation
  941. * request.
  942. *
  943. * We reclaim from a zone even if that zone is over pages_high. Because:
  944. * a) The caller may be trying to free *extra* pages to satisfy a higher-order
  945. * allocation or
  946. * b) The zones may be over pages_high but they must go *over* pages_high to
  947. * satisfy the `incremental min' zone defense algorithm.
  948. *
  949. * Returns the number of reclaimed pages.
  950. *
  951. * If a zone is deemed to be full of pinned pages then just give it a light
  952. * scan then give up on it.
  953. */
  954. static unsigned long shrink_zones(int priority, struct zone **zones,
  955. struct scan_control *sc)
  956. {
  957. unsigned long nr_reclaimed = 0;
  958. int i;
  959. sc->all_unreclaimable = 1;
  960. for (i = 0; zones[i] != NULL; i++) {
  961. struct zone *zone = zones[i];
  962. if (!populated_zone(zone))
  963. continue;
  964. if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
  965. continue;
  966. note_zone_scanning_priority(zone, priority);
  967. if (zone->all_unreclaimable && priority != DEF_PRIORITY)
  968. continue; /* Let kswapd poll it */
  969. sc->all_unreclaimable = 0;
  970. nr_reclaimed += shrink_zone(priority, zone, sc);
  971. }
  972. return nr_reclaimed;
  973. }
  974. /*
  975. * This is the main entry point to direct page reclaim.
  976. *
  977. * If a full scan of the inactive list fails to free enough memory then we
  978. * are "out of memory" and something needs to be killed.
  979. *
  980. * If the caller is !__GFP_FS then the probability of a failure is reasonably
  981. * high - the zone may be full of dirty or under-writeback pages, which this
  982. * caller can't do much about. We kick pdflush and take explicit naps in the
  983. * hope that some of these pages can be written. But if the allocating task
  984. * holds filesystem locks which prevent writeout this might not work, and the
  985. * allocation attempt will fail.
  986. */
  987. unsigned long try_to_free_pages(struct zone **zones, int order, gfp_t gfp_mask)
  988. {
  989. int priority;
  990. int ret = 0;
  991. unsigned long total_scanned = 0;
  992. unsigned long nr_reclaimed = 0;
  993. struct reclaim_state *reclaim_state = current->reclaim_state;
  994. unsigned long lru_pages = 0;
  995. int i;
  996. struct scan_control sc = {
  997. .gfp_mask = gfp_mask,
  998. .may_writepage = !laptop_mode,
  999. .swap_cluster_max = SWAP_CLUSTER_MAX,
  1000. .may_swap = 1,
  1001. .swappiness = vm_swappiness,
  1002. .order = order,
  1003. };
  1004. count_vm_event(ALLOCSTALL);
  1005. for (i = 0; zones[i] != NULL; i++) {
  1006. struct zone *zone = zones[i];
  1007. if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
  1008. continue;
  1009. lru_pages += zone_page_state(zone, NR_ACTIVE)
  1010. + zone_page_state(zone, NR_INACTIVE);
  1011. }
  1012. for (priority = DEF_PRIORITY; priority >= 0; priority--) {
  1013. sc.nr_scanned = 0;
  1014. if (!priority)
  1015. disable_swap_token();
  1016. nr_reclaimed += shrink_zones(priority, zones, &sc);
  1017. shrink_slab(sc.nr_scanned, gfp_mask, lru_pages);
  1018. if (reclaim_state) {
  1019. nr_reclaimed += reclaim_state->reclaimed_slab;
  1020. reclaim_state->reclaimed_slab = 0;
  1021. }
  1022. total_scanned += sc.nr_scanned;
  1023. if (nr_reclaimed >= sc.swap_cluster_max) {
  1024. ret = 1;
  1025. goto out;
  1026. }
  1027. /*
  1028. * Try to write back as many pages as we just scanned. This
  1029. * tends to cause slow streaming writers to write data to the
  1030. * disk smoothly, at the dirtying rate, which is nice. But
  1031. * that's undesirable in laptop mode, where we *want* lumpy
  1032. * writeout. So in laptop mode, write out the whole world.
  1033. */
  1034. if (total_scanned > sc.swap_cluster_max +
  1035. sc.swap_cluster_max / 2) {
  1036. wakeup_pdflush(laptop_mode ? 0 : total_scanned);
  1037. sc.may_writepage = 1;
  1038. }
  1039. /* Take a nap, wait for some writeback to complete */
  1040. if (sc.nr_scanned && priority < DEF_PRIORITY - 2)
  1041. congestion_wait(WRITE, HZ/10);
  1042. }
  1043. /* top priority shrink_caches still had more to do? don't OOM, then */
  1044. if (!sc.all_unreclaimable)
  1045. ret = 1;
  1046. out:
  1047. /*
  1048. * Now that we've scanned all the zones at this priority level, note
  1049. * that level within the zone so that the next thread which performs
  1050. * scanning of this zone will immediately start out at this priority
  1051. * level. This affects only the decision whether or not to bring
  1052. * mapped pages onto the inactive list.
  1053. */
  1054. if (priority < 0)
  1055. priority = 0;
  1056. for (i = 0; zones[i] != 0; i++) {
  1057. struct zone *zone = zones[i];
  1058. if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
  1059. continue;
  1060. zone->prev_priority = priority;
  1061. }
  1062. return ret;
  1063. }
  1064. /*
  1065. * For kswapd, balance_pgdat() will work across all this node's zones until
  1066. * they are all at pages_high.
  1067. *
  1068. * Returns the number of pages which were actually freed.
  1069. *
  1070. * There is special handling here for zones which are full of pinned pages.
  1071. * This can happen if the pages are all mlocked, or if they are all used by
  1072. * device drivers (say, ZONE_DMA). Or if they are all in use by hugetlb.
  1073. * What we do is to detect the case where all pages in the zone have been
  1074. * scanned twice and there has been zero successful reclaim. Mark the zone as
  1075. * dead and from now on, only perform a short scan. Basically we're polling
  1076. * the zone for when the problem goes away.
  1077. *
  1078. * kswapd scans the zones in the highmem->normal->dma direction. It skips
  1079. * zones which have free_pages > pages_high, but once a zone is found to have
  1080. * free_pages <= pages_high, we scan that zone and the lower zones regardless
  1081. * of the number of free pages in the lower zones. This interoperates with
  1082. * the page allocator fallback scheme to ensure that aging of pages is balanced
  1083. * across the zones.
  1084. */
  1085. static unsigned long balance_pgdat(pg_data_t *pgdat, int order)
  1086. {
  1087. int all_zones_ok;
  1088. int priority;
  1089. int i;
  1090. unsigned long total_scanned;
  1091. unsigned long nr_reclaimed;
  1092. struct reclaim_state *reclaim_state = current->reclaim_state;
  1093. struct scan_control sc = {
  1094. .gfp_mask = GFP_KERNEL,
  1095. .may_swap = 1,
  1096. .swap_cluster_max = SWAP_CLUSTER_MAX,
  1097. .swappiness = vm_swappiness,
  1098. .order = order,
  1099. };
  1100. /*
  1101. * temp_priority is used to remember the scanning priority at which
  1102. * this zone was successfully refilled to free_pages == pages_high.
  1103. */
  1104. int temp_priority[MAX_NR_ZONES];
  1105. loop_again:
  1106. total_scanned = 0;
  1107. nr_reclaimed = 0;
  1108. sc.may_writepage = !laptop_mode;
  1109. count_vm_event(PAGEOUTRUN);
  1110. for (i = 0; i < pgdat->nr_zones; i++)
  1111. temp_priority[i] = DEF_PRIORITY;
  1112. for (priority = DEF_PRIORITY; priority >= 0; priority--) {
  1113. int end_zone = 0; /* Inclusive. 0 = ZONE_DMA */
  1114. unsigned long lru_pages = 0;
  1115. /* The swap token gets in the way of swapout... */
  1116. if (!priority)
  1117. disable_swap_token();
  1118. all_zones_ok = 1;
  1119. /*
  1120. * Scan in the highmem->dma direction for the highest
  1121. * zone which needs scanning
  1122. */
  1123. for (i = pgdat->nr_zones - 1; i >= 0; i--) {
  1124. struct zone *zone = pgdat->node_zones + i;
  1125. if (!populated_zone(zone))
  1126. continue;
  1127. if (zone->all_unreclaimable && priority != DEF_PRIORITY)
  1128. continue;
  1129. if (!zone_watermark_ok(zone, order, zone->pages_high,
  1130. 0, 0)) {
  1131. end_zone = i;
  1132. break;
  1133. }
  1134. }
  1135. if (i < 0)
  1136. goto out;
  1137. for (i = 0; i <= end_zone; i++) {
  1138. struct zone *zone = pgdat->node_zones + i;
  1139. lru_pages += zone_page_state(zone, NR_ACTIVE)
  1140. + zone_page_state(zone, NR_INACTIVE);
  1141. }
  1142. /*
  1143. * Now scan the zone in the dma->highmem direction, stopping
  1144. * at the last zone which needs scanning.
  1145. *
  1146. * We do this because the page allocator works in the opposite
  1147. * direction. This prevents the page allocator from allocating
  1148. * pages behind kswapd's direction of progress, which would
  1149. * cause too much scanning of the lower zones.
  1150. */
  1151. for (i = 0; i <= end_zone; i++) {
  1152. struct zone *zone = pgdat->node_zones + i;
  1153. int nr_slab;
  1154. if (!populated_zone(zone))
  1155. continue;
  1156. if (zone->all_unreclaimable && priority != DEF_PRIORITY)
  1157. continue;
  1158. if (!zone_watermark_ok(zone, order, zone->pages_high,
  1159. end_zone, 0))
  1160. all_zones_ok = 0;
  1161. temp_priority[i] = priority;
  1162. sc.nr_scanned = 0;
  1163. note_zone_scanning_priority(zone, priority);
  1164. nr_reclaimed += shrink_zone(priority, zone, &sc);
  1165. reclaim_state->reclaimed_slab = 0;
  1166. nr_slab = shrink_slab(sc.nr_scanned, GFP_KERNEL,
  1167. lru_pages);
  1168. nr_reclaimed += reclaim_state->reclaimed_slab;
  1169. total_scanned += sc.nr_scanned;
  1170. if (zone->all_unreclaimable)
  1171. continue;
  1172. if (nr_slab == 0 && zone->pages_scanned >=
  1173. (zone_page_state(zone, NR_ACTIVE)
  1174. + zone_page_state(zone, NR_INACTIVE)) * 6)
  1175. zone->all_unreclaimable = 1;
  1176. /*
  1177. * If we've done a decent amount of scanning and
  1178. * the reclaim ratio is low, start doing writepage
  1179. * even in laptop mode
  1180. */
  1181. if (total_scanned > SWAP_CLUSTER_MAX * 2 &&
  1182. total_scanned > nr_reclaimed + nr_reclaimed / 2)
  1183. sc.may_writepage = 1;
  1184. }
  1185. if (all_zones_ok)
  1186. break; /* kswapd: all done */
  1187. /*
  1188. * OK, kswapd is getting into trouble. Take a nap, then take
  1189. * another pass across the zones.
  1190. */
  1191. if (total_scanned && priority < DEF_PRIORITY - 2)
  1192. congestion_wait(WRITE, HZ/10);
  1193. /*
  1194. * We do this so kswapd doesn't build up large priorities for
  1195. * example when it is freeing in parallel with allocators. It
  1196. * matches the direct reclaim path behaviour in terms of impact
  1197. * on zone->*_priority.
  1198. */
  1199. if (nr_reclaimed >= SWAP_CLUSTER_MAX)
  1200. break;
  1201. }
  1202. out:
  1203. /*
  1204. * Note within each zone the priority level at which this zone was
  1205. * brought into a happy state. So that the next thread which scans this
  1206. * zone will start out at that priority level.
  1207. */
  1208. for (i = 0; i < pgdat->nr_zones; i++) {
  1209. struct zone *zone = pgdat->node_zones + i;
  1210. zone->prev_priority = temp_priority[i];
  1211. }
  1212. if (!all_zones_ok) {
  1213. cond_resched();
  1214. try_to_freeze();
  1215. goto loop_again;
  1216. }
  1217. return nr_reclaimed;
  1218. }
  1219. /*
  1220. * The background pageout daemon, started as a kernel thread
  1221. * from the init process.
  1222. *
  1223. * This basically trickles out pages so that we have _some_
  1224. * free memory available even if there is no other activity
  1225. * that frees anything up. This is needed for things like routing
  1226. * etc, where we otherwise might have all activity going on in
  1227. * asynchronous contexts that cannot page things out.
  1228. *
  1229. * If there are applications that are active memory-allocators
  1230. * (most normal use), this basically shouldn't matter.
  1231. */
  1232. static int kswapd(void *p)
  1233. {
  1234. unsigned long order;
  1235. pg_data_t *pgdat = (pg_data_t*)p;
  1236. struct task_struct *tsk = current;
  1237. DEFINE_WAIT(wait);
  1238. struct reclaim_state reclaim_state = {
  1239. .reclaimed_slab = 0,
  1240. };
  1241. cpumask_t cpumask;
  1242. cpumask = node_to_cpumask(pgdat->node_id);
  1243. if (!cpus_empty(cpumask))
  1244. set_cpus_allowed(tsk, cpumask);
  1245. current->reclaim_state = &reclaim_state;
  1246. /*
  1247. * Tell the memory management that we're a "memory allocator",
  1248. * and that if we need more memory we should get access to it
  1249. * regardless (see "__alloc_pages()"). "kswapd" should
  1250. * never get caught in the normal page freeing logic.
  1251. *
  1252. * (Kswapd normally doesn't need memory anyway, but sometimes
  1253. * you need a small amount of memory in order to be able to
  1254. * page out something else, and this flag essentially protects
  1255. * us from recursively trying to free more memory as we're
  1256. * trying to free the first piece of memory in the first place).
  1257. */
  1258. tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD;
  1259. set_freezable();
  1260. order = 0;
  1261. for ( ; ; ) {
  1262. unsigned long new_order;
  1263. prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
  1264. new_order = pgdat->kswapd_max_order;
  1265. pgdat->kswapd_max_order = 0;
  1266. if (order < new_order) {
  1267. /*
  1268. * Don't sleep if someone wants a larger 'order'
  1269. * allocation
  1270. */
  1271. order = new_order;
  1272. } else {
  1273. if (!freezing(current))
  1274. schedule();
  1275. order = pgdat->kswapd_max_order;
  1276. }
  1277. finish_wait(&pgdat->kswapd_wait, &wait);
  1278. if (!try_to_freeze()) {
  1279. /* We can speed up thawing tasks if we don't call
  1280. * balance_pgdat after returning from the refrigerator
  1281. */
  1282. balance_pgdat(pgdat, order);
  1283. }
  1284. }
  1285. return 0;
  1286. }
  1287. /*
  1288. * A zone is low on free memory, so wake its kswapd task to service it.
  1289. */
  1290. void wakeup_kswapd(struct zone *zone, int order)
  1291. {
  1292. pg_data_t *pgdat;
  1293. if (!populated_zone(zone))
  1294. return;
  1295. pgdat = zone->zone_pgdat;
  1296. if (zone_watermark_ok(zone, order, zone->pages_low, 0, 0))
  1297. return;
  1298. if (pgdat->kswapd_max_order < order)
  1299. pgdat->kswapd_max_order = order;
  1300. if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
  1301. return;
  1302. if (!waitqueue_active(&pgdat->kswapd_wait))
  1303. return;
  1304. wake_up_interruptible(&pgdat->kswapd_wait);
  1305. }
  1306. #ifdef CONFIG_PM
  1307. /*
  1308. * Helper function for shrink_all_memory(). Tries to reclaim 'nr_pages' pages
  1309. * from LRU lists system-wide, for given pass and priority, and returns the
  1310. * number of reclaimed pages
  1311. *
  1312. * For pass > 3 we also try to shrink the LRU lists that contain a few pages
  1313. */
  1314. static unsigned long shrink_all_zones(unsigned long nr_pages, int prio,
  1315. int pass, struct scan_control *sc)
  1316. {
  1317. struct zone *zone;
  1318. unsigned long nr_to_scan, ret = 0;
  1319. for_each_zone(zone) {
  1320. if (!populated_zone(zone))
  1321. continue;
  1322. if (zone->all_unreclaimable && prio != DEF_PRIORITY)
  1323. continue;
  1324. /* For pass = 0 we don't shrink the active list */
  1325. if (pass > 0) {
  1326. zone->nr_scan_active +=
  1327. (zone_page_state(zone, NR_ACTIVE) >> prio) + 1;
  1328. if (zone->nr_scan_active >= nr_pages || pass > 3) {
  1329. zone->nr_scan_active = 0;
  1330. nr_to_scan = min(nr_pages,
  1331. zone_page_state(zone, NR_ACTIVE));
  1332. shrink_active_list(nr_to_scan, zone, sc, prio);
  1333. }
  1334. }
  1335. zone->nr_scan_inactive +=
  1336. (zone_page_state(zone, NR_INACTIVE) >> prio) + 1;
  1337. if (zone->nr_scan_inactive >= nr_pages || pass > 3) {
  1338. zone->nr_scan_inactive = 0;
  1339. nr_to_scan = min(nr_pages,
  1340. zone_page_state(zone, NR_INACTIVE));
  1341. ret += shrink_inactive_list(nr_to_scan, zone, sc);
  1342. if (ret >= nr_pages)
  1343. return ret;
  1344. }
  1345. }
  1346. return ret;
  1347. }
  1348. static unsigned long count_lru_pages(void)
  1349. {
  1350. return global_page_state(NR_ACTIVE) + global_page_state(NR_INACTIVE);
  1351. }
  1352. /*
  1353. * Try to free `nr_pages' of memory, system-wide, and return the number of
  1354. * freed pages.
  1355. *
  1356. * Rather than trying to age LRUs the aim is to preserve the overall
  1357. * LRU order by reclaiming preferentially
  1358. * inactive > active > active referenced > active mapped
  1359. */
  1360. unsigned long shrink_all_memory(unsigned long nr_pages)
  1361. {
  1362. unsigned long lru_pages, nr_slab;
  1363. unsigned long ret = 0;
  1364. int pass;
  1365. struct reclaim_state reclaim_state;
  1366. struct scan_control sc = {
  1367. .gfp_mask = GFP_KERNEL,
  1368. .may_swap = 0,
  1369. .swap_cluster_max = nr_pages,
  1370. .may_writepage = 1,
  1371. .swappiness = vm_swappiness,
  1372. };
  1373. current->reclaim_state = &reclaim_state;
  1374. lru_pages = count_lru_pages();
  1375. nr_slab = global_page_state(NR_SLAB_RECLAIMABLE);
  1376. /* If slab caches are huge, it's better to hit them first */
  1377. while (nr_slab >= lru_pages) {
  1378. reclaim_state.reclaimed_slab = 0;
  1379. shrink_slab(nr_pages, sc.gfp_mask, lru_pages);
  1380. if (!reclaim_state.reclaimed_slab)
  1381. break;
  1382. ret += reclaim_state.reclaimed_slab;
  1383. if (ret >= nr_pages)
  1384. goto out;
  1385. nr_slab -= reclaim_state.reclaimed_slab;
  1386. }
  1387. /*
  1388. * We try to shrink LRUs in 5 passes:
  1389. * 0 = Reclaim from inactive_list only
  1390. * 1 = Reclaim from active list but don't reclaim mapped
  1391. * 2 = 2nd pass of type 1
  1392. * 3 = Reclaim mapped (normal reclaim)
  1393. * 4 = 2nd pass of type 3
  1394. */
  1395. for (pass = 0; pass < 5; pass++) {
  1396. int prio;
  1397. /* Force reclaiming mapped pages in the passes #3 and #4 */
  1398. if (pass > 2) {
  1399. sc.may_swap = 1;
  1400. sc.swappiness = 100;
  1401. }
  1402. for (prio = DEF_PRIORITY; prio >= 0; prio--) {
  1403. unsigned long nr_to_scan = nr_pages - ret;
  1404. sc.nr_scanned = 0;
  1405. ret += shrink_all_zones(nr_to_scan, prio, pass, &sc);
  1406. if (ret >= nr_pages)
  1407. goto out;
  1408. reclaim_state.reclaimed_slab = 0;
  1409. shrink_slab(sc.nr_scanned, sc.gfp_mask,
  1410. count_lru_pages());
  1411. ret += reclaim_state.reclaimed_slab;
  1412. if (ret >= nr_pages)
  1413. goto out;
  1414. if (sc.nr_scanned && prio < DEF_PRIORITY - 2)
  1415. congestion_wait(WRITE, HZ / 10);
  1416. }
  1417. }
  1418. /*
  1419. * If ret = 0, we could not shrink LRUs, but there may be something
  1420. * in slab caches
  1421. */
  1422. if (!ret) {
  1423. do {
  1424. reclaim_state.reclaimed_slab = 0;
  1425. shrink_slab(nr_pages, sc.gfp_mask, count_lru_pages());
  1426. ret += reclaim_state.reclaimed_slab;
  1427. } while (ret < nr_pages && reclaim_state.reclaimed_slab > 0);
  1428. }
  1429. out:
  1430. current->reclaim_state = NULL;
  1431. return ret;
  1432. }
  1433. #endif
  1434. /* It's optimal to keep kswapds on the same CPUs as their memory, but
  1435. not required for correctness. So if the last cpu in a node goes
  1436. away, we get changed to run anywhere: as the first one comes back,
  1437. restore their cpu bindings. */
  1438. static int __devinit cpu_callback(struct notifier_block *nfb,
  1439. unsigned long action, void *hcpu)
  1440. {
  1441. pg_data_t *pgdat;
  1442. cpumask_t mask;
  1443. if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN) {
  1444. for_each_online_pgdat(pgdat) {
  1445. mask = node_to_cpumask(pgdat->node_id);
  1446. if (any_online_cpu(mask) != NR_CPUS)
  1447. /* One of our CPUs online: restore mask */
  1448. set_cpus_allowed(pgdat->kswapd, mask);
  1449. }
  1450. }
  1451. return NOTIFY_OK;
  1452. }
  1453. /*
  1454. * This kswapd start function will be called by init and node-hot-add.
  1455. * On node-hot-add, kswapd will moved to proper cpus if cpus are hot-added.
  1456. */
  1457. int kswapd_run(int nid)
  1458. {
  1459. pg_data_t *pgdat = NODE_DATA(nid);
  1460. int ret = 0;
  1461. if (pgdat->kswapd)
  1462. return 0;
  1463. pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid);
  1464. if (IS_ERR(pgdat->kswapd)) {
  1465. /* failure at boot is fatal */
  1466. BUG_ON(system_state == SYSTEM_BOOTING);
  1467. printk("Failed to start kswapd on node %d\n",nid);
  1468. ret = -1;
  1469. }
  1470. return ret;
  1471. }
  1472. static int __init kswapd_init(void)
  1473. {
  1474. int nid;
  1475. swap_setup();
  1476. for_each_online_node(nid)
  1477. kswapd_run(nid);
  1478. hotcpu_notifier(cpu_callback, 0);
  1479. return 0;
  1480. }
  1481. module_init(kswapd_init)
  1482. #ifdef CONFIG_NUMA
  1483. /*
  1484. * Zone reclaim mode
  1485. *
  1486. * If non-zero call zone_reclaim when the number of free pages falls below
  1487. * the watermarks.
  1488. */
  1489. int zone_reclaim_mode __read_mostly;
  1490. #define RECLAIM_OFF 0
  1491. #define RECLAIM_ZONE (1<<0) /* Run shrink_cache on the zone */
  1492. #define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */
  1493. #define RECLAIM_SWAP (1<<2) /* Swap pages out during reclaim */
  1494. /*
  1495. * Priority for ZONE_RECLAIM. This determines the fraction of pages
  1496. * of a node considered for each zone_reclaim. 4 scans 1/16th of
  1497. * a zone.
  1498. */
  1499. #define ZONE_RECLAIM_PRIORITY 4
  1500. /*
  1501. * Percentage of pages in a zone that must be unmapped for zone_reclaim to
  1502. * occur.
  1503. */
  1504. int sysctl_min_unmapped_ratio = 1;
  1505. /*
  1506. * If the number of slab pages in a zone grows beyond this percentage then
  1507. * slab reclaim needs to occur.
  1508. */
  1509. int sysctl_min_slab_ratio = 5;
  1510. /*
  1511. * Try to free up some pages from this zone through reclaim.
  1512. */
  1513. static int __zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
  1514. {
  1515. /* Minimum pages needed in order to stay on node */
  1516. const unsigned long nr_pages = 1 << order;
  1517. struct task_struct *p = current;
  1518. struct reclaim_state reclaim_state;
  1519. int priority;
  1520. unsigned long nr_reclaimed = 0;
  1521. struct scan_control sc = {
  1522. .may_writepage = !!(zone_reclaim_mode & RECLAIM_WRITE),
  1523. .may_swap = !!(zone_reclaim_mode & RECLAIM_SWAP),
  1524. .swap_cluster_max = max_t(unsigned long, nr_pages,
  1525. SWAP_CLUSTER_MAX),
  1526. .gfp_mask = gfp_mask,
  1527. .swappiness = vm_swappiness,
  1528. };
  1529. unsigned long slab_reclaimable;
  1530. disable_swap_token();
  1531. cond_resched();
  1532. /*
  1533. * We need to be able to allocate from the reserves for RECLAIM_SWAP
  1534. * and we also need to be able to write out pages for RECLAIM_WRITE
  1535. * and RECLAIM_SWAP.
  1536. */
  1537. p->flags |= PF_MEMALLOC | PF_SWAPWRITE;
  1538. reclaim_state.reclaimed_slab = 0;
  1539. p->reclaim_state = &reclaim_state;
  1540. if (zone_page_state(zone, NR_FILE_PAGES) -
  1541. zone_page_state(zone, NR_FILE_MAPPED) >
  1542. zone->min_unmapped_pages) {
  1543. /*
  1544. * Free memory by calling shrink zone with increasing
  1545. * priorities until we have enough memory freed.
  1546. */
  1547. priority = ZONE_RECLAIM_PRIORITY;
  1548. do {
  1549. note_zone_scanning_priority(zone, priority);
  1550. nr_reclaimed += shrink_zone(priority, zone, &sc);
  1551. priority--;
  1552. } while (priority >= 0 && nr_reclaimed < nr_pages);
  1553. }
  1554. slab_reclaimable = zone_page_state(zone, NR_SLAB_RECLAIMABLE);
  1555. if (slab_reclaimable > zone->min_slab_pages) {
  1556. /*
  1557. * shrink_slab() does not currently allow us to determine how
  1558. * many pages were freed in this zone. So we take the current
  1559. * number of slab pages and shake the slab until it is reduced
  1560. * by the same nr_pages that we used for reclaiming unmapped
  1561. * pages.
  1562. *
  1563. * Note that shrink_slab will free memory on all zones and may
  1564. * take a long time.
  1565. */
  1566. while (shrink_slab(sc.nr_scanned, gfp_mask, order) &&
  1567. zone_page_state(zone, NR_SLAB_RECLAIMABLE) >
  1568. slab_reclaimable - nr_pages)
  1569. ;
  1570. /*
  1571. * Update nr_reclaimed by the number of slab pages we
  1572. * reclaimed from this zone.
  1573. */
  1574. nr_reclaimed += slab_reclaimable -
  1575. zone_page_state(zone, NR_SLAB_RECLAIMABLE);
  1576. }
  1577. p->reclaim_state = NULL;
  1578. current->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE);
  1579. return nr_reclaimed >= nr_pages;
  1580. }
  1581. int zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
  1582. {
  1583. cpumask_t mask;
  1584. int node_id;
  1585. /*
  1586. * Zone reclaim reclaims unmapped file backed pages and
  1587. * slab pages if we are over the defined limits.
  1588. *
  1589. * A small portion of unmapped file backed pages is needed for
  1590. * file I/O otherwise pages read by file I/O will be immediately
  1591. * thrown out if the zone is overallocated. So we do not reclaim
  1592. * if less than a specified percentage of the zone is used by
  1593. * unmapped file backed pages.
  1594. */
  1595. if (zone_page_state(zone, NR_FILE_PAGES) -
  1596. zone_page_state(zone, NR_FILE_MAPPED) <= zone->min_unmapped_pages
  1597. && zone_page_state(zone, NR_SLAB_RECLAIMABLE)
  1598. <= zone->min_slab_pages)
  1599. return 0;
  1600. /*
  1601. * Avoid concurrent zone reclaims, do not reclaim in a zone that does
  1602. * not have reclaimable pages and if we should not delay the allocation
  1603. * then do not scan.
  1604. */
  1605. if (!(gfp_mask & __GFP_WAIT) ||
  1606. zone->all_unreclaimable ||
  1607. atomic_read(&zone->reclaim_in_progress) > 0 ||
  1608. (current->flags & PF_MEMALLOC))
  1609. return 0;
  1610. /*
  1611. * Only run zone reclaim on the local zone or on zones that do not
  1612. * have associated processors. This will favor the local processor
  1613. * over remote processors and spread off node memory allocations
  1614. * as wide as possible.
  1615. */
  1616. node_id = zone_to_nid(zone);
  1617. mask = node_to_cpumask(node_id);
  1618. if (!cpus_empty(mask) && node_id != numa_node_id())
  1619. return 0;
  1620. return __zone_reclaim(zone, gfp_mask, order);
  1621. }
  1622. #endif