vmscan.c 53 KB

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