vmscan.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892
  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. if (zone_is_near_oom(zone))
  824. goto force_reclaim_mapped;
  825. /*
  826. * `distress' is a measure of how much trouble we're having
  827. * reclaiming pages. 0 -> no problems. 100 -> great trouble.
  828. */
  829. distress = 100 >> min(zone->prev_priority, priority);
  830. /*
  831. * The point of this algorithm is to decide when to start
  832. * reclaiming mapped memory instead of just pagecache. Work out
  833. * how much memory
  834. * is mapped.
  835. */
  836. mapped_ratio = ((global_page_state(NR_FILE_MAPPED) +
  837. global_page_state(NR_ANON_PAGES)) * 100) /
  838. vm_total_pages;
  839. /*
  840. * Now decide how much we really want to unmap some pages. The
  841. * mapped ratio is downgraded - just because there's a lot of
  842. * mapped memory doesn't necessarily mean that page reclaim
  843. * isn't succeeding.
  844. *
  845. * The distress ratio is important - we don't want to start
  846. * going oom.
  847. *
  848. * A 100% value of vm_swappiness overrides this algorithm
  849. * altogether.
  850. */
  851. swap_tendency = mapped_ratio / 2 + distress + sc->swappiness;
  852. /*
  853. * Now use this metric to decide whether to start moving mapped
  854. * memory onto the inactive list.
  855. */
  856. if (swap_tendency >= 100)
  857. force_reclaim_mapped:
  858. reclaim_mapped = 1;
  859. }
  860. lru_add_drain();
  861. spin_lock_irq(&zone->lru_lock);
  862. pgmoved = isolate_lru_pages(nr_pages, &zone->active_list,
  863. &l_hold, &pgscanned, sc->order, ISOLATE_ACTIVE);
  864. zone->pages_scanned += pgscanned;
  865. __mod_zone_page_state(zone, NR_ACTIVE, -pgmoved);
  866. spin_unlock_irq(&zone->lru_lock);
  867. while (!list_empty(&l_hold)) {
  868. cond_resched();
  869. page = lru_to_page(&l_hold);
  870. list_del(&page->lru);
  871. if (page_mapped(page)) {
  872. if (!reclaim_mapped ||
  873. (total_swap_pages == 0 && PageAnon(page)) ||
  874. page_referenced(page, 0)) {
  875. list_add(&page->lru, &l_active);
  876. continue;
  877. }
  878. }
  879. list_add(&page->lru, &l_inactive);
  880. }
  881. pagevec_init(&pvec, 1);
  882. pgmoved = 0;
  883. spin_lock_irq(&zone->lru_lock);
  884. while (!list_empty(&l_inactive)) {
  885. page = lru_to_page(&l_inactive);
  886. prefetchw_prev_lru_page(page, &l_inactive, flags);
  887. VM_BUG_ON(PageLRU(page));
  888. SetPageLRU(page);
  889. VM_BUG_ON(!PageActive(page));
  890. ClearPageActive(page);
  891. list_move(&page->lru, &zone->inactive_list);
  892. pgmoved++;
  893. if (!pagevec_add(&pvec, page)) {
  894. __mod_zone_page_state(zone, NR_INACTIVE, pgmoved);
  895. spin_unlock_irq(&zone->lru_lock);
  896. pgdeactivate += pgmoved;
  897. pgmoved = 0;
  898. if (buffer_heads_over_limit)
  899. pagevec_strip(&pvec);
  900. __pagevec_release(&pvec);
  901. spin_lock_irq(&zone->lru_lock);
  902. }
  903. }
  904. __mod_zone_page_state(zone, NR_INACTIVE, pgmoved);
  905. pgdeactivate += pgmoved;
  906. if (buffer_heads_over_limit) {
  907. spin_unlock_irq(&zone->lru_lock);
  908. pagevec_strip(&pvec);
  909. spin_lock_irq(&zone->lru_lock);
  910. }
  911. pgmoved = 0;
  912. while (!list_empty(&l_active)) {
  913. page = lru_to_page(&l_active);
  914. prefetchw_prev_lru_page(page, &l_active, flags);
  915. VM_BUG_ON(PageLRU(page));
  916. SetPageLRU(page);
  917. VM_BUG_ON(!PageActive(page));
  918. list_move(&page->lru, &zone->active_list);
  919. pgmoved++;
  920. if (!pagevec_add(&pvec, page)) {
  921. __mod_zone_page_state(zone, NR_ACTIVE, pgmoved);
  922. pgmoved = 0;
  923. spin_unlock_irq(&zone->lru_lock);
  924. __pagevec_release(&pvec);
  925. spin_lock_irq(&zone->lru_lock);
  926. }
  927. }
  928. __mod_zone_page_state(zone, NR_ACTIVE, pgmoved);
  929. __count_zone_vm_events(PGREFILL, zone, pgscanned);
  930. __count_vm_events(PGDEACTIVATE, pgdeactivate);
  931. spin_unlock_irq(&zone->lru_lock);
  932. pagevec_release(&pvec);
  933. }
  934. /*
  935. * This is a basic per-zone page freer. Used by both kswapd and direct reclaim.
  936. */
  937. static unsigned long shrink_zone(int priority, struct zone *zone,
  938. struct scan_control *sc)
  939. {
  940. unsigned long nr_active;
  941. unsigned long nr_inactive;
  942. unsigned long nr_to_scan;
  943. unsigned long nr_reclaimed = 0;
  944. atomic_inc(&zone->reclaim_in_progress);
  945. /*
  946. * Add one to `nr_to_scan' just to make sure that the kernel will
  947. * slowly sift through the active list.
  948. */
  949. zone->nr_scan_active +=
  950. (zone_page_state(zone, NR_ACTIVE) >> priority) + 1;
  951. nr_active = zone->nr_scan_active;
  952. if (nr_active >= sc->swap_cluster_max)
  953. zone->nr_scan_active = 0;
  954. else
  955. nr_active = 0;
  956. zone->nr_scan_inactive +=
  957. (zone_page_state(zone, NR_INACTIVE) >> priority) + 1;
  958. nr_inactive = zone->nr_scan_inactive;
  959. if (nr_inactive >= sc->swap_cluster_max)
  960. zone->nr_scan_inactive = 0;
  961. else
  962. nr_inactive = 0;
  963. while (nr_active || nr_inactive) {
  964. if (nr_active) {
  965. nr_to_scan = min(nr_active,
  966. (unsigned long)sc->swap_cluster_max);
  967. nr_active -= nr_to_scan;
  968. shrink_active_list(nr_to_scan, zone, sc, priority);
  969. }
  970. if (nr_inactive) {
  971. nr_to_scan = min(nr_inactive,
  972. (unsigned long)sc->swap_cluster_max);
  973. nr_inactive -= nr_to_scan;
  974. nr_reclaimed += shrink_inactive_list(nr_to_scan, zone,
  975. sc);
  976. }
  977. }
  978. throttle_vm_writeout(sc->gfp_mask);
  979. atomic_dec(&zone->reclaim_in_progress);
  980. return nr_reclaimed;
  981. }
  982. /*
  983. * This is the direct reclaim path, for page-allocating processes. We only
  984. * try to reclaim pages from zones which will satisfy the caller's allocation
  985. * request.
  986. *
  987. * We reclaim from a zone even if that zone is over pages_high. Because:
  988. * a) The caller may be trying to free *extra* pages to satisfy a higher-order
  989. * allocation or
  990. * b) The zones may be over pages_high but they must go *over* pages_high to
  991. * satisfy the `incremental min' zone defense algorithm.
  992. *
  993. * Returns the number of reclaimed pages.
  994. *
  995. * If a zone is deemed to be full of pinned pages then just give it a light
  996. * scan then give up on it.
  997. */
  998. static unsigned long shrink_zones(int priority, struct zone **zones,
  999. struct scan_control *sc)
  1000. {
  1001. unsigned long nr_reclaimed = 0;
  1002. int i;
  1003. sc->all_unreclaimable = 1;
  1004. for (i = 0; zones[i] != NULL; i++) {
  1005. struct zone *zone = zones[i];
  1006. if (!populated_zone(zone))
  1007. continue;
  1008. if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
  1009. continue;
  1010. note_zone_scanning_priority(zone, priority);
  1011. if (zone->all_unreclaimable && priority != DEF_PRIORITY)
  1012. continue; /* Let kswapd poll it */
  1013. sc->all_unreclaimable = 0;
  1014. nr_reclaimed += shrink_zone(priority, zone, sc);
  1015. }
  1016. return nr_reclaimed;
  1017. }
  1018. /*
  1019. * This is the main entry point to direct page reclaim.
  1020. *
  1021. * If a full scan of the inactive list fails to free enough memory then we
  1022. * are "out of memory" and something needs to be killed.
  1023. *
  1024. * If the caller is !__GFP_FS then the probability of a failure is reasonably
  1025. * high - the zone may be full of dirty or under-writeback pages, which this
  1026. * caller can't do much about. We kick pdflush and take explicit naps in the
  1027. * hope that some of these pages can be written. But if the allocating task
  1028. * holds filesystem locks which prevent writeout this might not work, and the
  1029. * allocation attempt will fail.
  1030. */
  1031. unsigned long try_to_free_pages(struct zone **zones, int order, gfp_t gfp_mask)
  1032. {
  1033. int priority;
  1034. int ret = 0;
  1035. unsigned long total_scanned = 0;
  1036. unsigned long nr_reclaimed = 0;
  1037. struct reclaim_state *reclaim_state = current->reclaim_state;
  1038. unsigned long lru_pages = 0;
  1039. int i;
  1040. struct scan_control sc = {
  1041. .gfp_mask = gfp_mask,
  1042. .may_writepage = !laptop_mode,
  1043. .swap_cluster_max = SWAP_CLUSTER_MAX,
  1044. .may_swap = 1,
  1045. .swappiness = vm_swappiness,
  1046. .order = order,
  1047. };
  1048. count_vm_event(ALLOCSTALL);
  1049. for (i = 0; zones[i] != NULL; i++) {
  1050. struct zone *zone = zones[i];
  1051. if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
  1052. continue;
  1053. lru_pages += zone_page_state(zone, NR_ACTIVE)
  1054. + zone_page_state(zone, NR_INACTIVE);
  1055. }
  1056. for (priority = DEF_PRIORITY; priority >= 0; priority--) {
  1057. sc.nr_scanned = 0;
  1058. if (!priority)
  1059. disable_swap_token();
  1060. nr_reclaimed += shrink_zones(priority, zones, &sc);
  1061. shrink_slab(sc.nr_scanned, gfp_mask, lru_pages);
  1062. if (reclaim_state) {
  1063. nr_reclaimed += reclaim_state->reclaimed_slab;
  1064. reclaim_state->reclaimed_slab = 0;
  1065. }
  1066. total_scanned += sc.nr_scanned;
  1067. if (nr_reclaimed >= sc.swap_cluster_max) {
  1068. ret = 1;
  1069. goto out;
  1070. }
  1071. /*
  1072. * Try to write back as many pages as we just scanned. This
  1073. * tends to cause slow streaming writers to write data to the
  1074. * disk smoothly, at the dirtying rate, which is nice. But
  1075. * that's undesirable in laptop mode, where we *want* lumpy
  1076. * writeout. So in laptop mode, write out the whole world.
  1077. */
  1078. if (total_scanned > sc.swap_cluster_max +
  1079. sc.swap_cluster_max / 2) {
  1080. wakeup_pdflush(laptop_mode ? 0 : total_scanned);
  1081. sc.may_writepage = 1;
  1082. }
  1083. /* Take a nap, wait for some writeback to complete */
  1084. if (sc.nr_scanned && priority < DEF_PRIORITY - 2)
  1085. congestion_wait(WRITE, HZ/10);
  1086. }
  1087. /* top priority shrink_caches still had more to do? don't OOM, then */
  1088. if (!sc.all_unreclaimable)
  1089. ret = 1;
  1090. out:
  1091. /*
  1092. * Now that we've scanned all the zones at this priority level, note
  1093. * that level within the zone so that the next thread which performs
  1094. * scanning of this zone will immediately start out at this priority
  1095. * level. This affects only the decision whether or not to bring
  1096. * mapped pages onto the inactive list.
  1097. */
  1098. if (priority < 0)
  1099. priority = 0;
  1100. for (i = 0; zones[i] != 0; i++) {
  1101. struct zone *zone = zones[i];
  1102. if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
  1103. continue;
  1104. zone->prev_priority = priority;
  1105. }
  1106. return ret;
  1107. }
  1108. /*
  1109. * For kswapd, balance_pgdat() will work across all this node's zones until
  1110. * they are all at pages_high.
  1111. *
  1112. * Returns the number of pages which were actually freed.
  1113. *
  1114. * There is special handling here for zones which are full of pinned pages.
  1115. * This can happen if the pages are all mlocked, or if they are all used by
  1116. * device drivers (say, ZONE_DMA). Or if they are all in use by hugetlb.
  1117. * What we do is to detect the case where all pages in the zone have been
  1118. * scanned twice and there has been zero successful reclaim. Mark the zone as
  1119. * dead and from now on, only perform a short scan. Basically we're polling
  1120. * the zone for when the problem goes away.
  1121. *
  1122. * kswapd scans the zones in the highmem->normal->dma direction. It skips
  1123. * zones which have free_pages > pages_high, but once a zone is found to have
  1124. * free_pages <= pages_high, we scan that zone and the lower zones regardless
  1125. * of the number of free pages in the lower zones. This interoperates with
  1126. * the page allocator fallback scheme to ensure that aging of pages is balanced
  1127. * across the zones.
  1128. */
  1129. static unsigned long balance_pgdat(pg_data_t *pgdat, int order)
  1130. {
  1131. int all_zones_ok;
  1132. int priority;
  1133. int i;
  1134. unsigned long total_scanned;
  1135. unsigned long nr_reclaimed;
  1136. struct reclaim_state *reclaim_state = current->reclaim_state;
  1137. struct scan_control sc = {
  1138. .gfp_mask = GFP_KERNEL,
  1139. .may_swap = 1,
  1140. .swap_cluster_max = SWAP_CLUSTER_MAX,
  1141. .swappiness = vm_swappiness,
  1142. .order = order,
  1143. };
  1144. /*
  1145. * temp_priority is used to remember the scanning priority at which
  1146. * this zone was successfully refilled to free_pages == pages_high.
  1147. */
  1148. int temp_priority[MAX_NR_ZONES];
  1149. loop_again:
  1150. total_scanned = 0;
  1151. nr_reclaimed = 0;
  1152. sc.may_writepage = !laptop_mode;
  1153. count_vm_event(PAGEOUTRUN);
  1154. for (i = 0; i < pgdat->nr_zones; i++)
  1155. temp_priority[i] = DEF_PRIORITY;
  1156. for (priority = DEF_PRIORITY; priority >= 0; priority--) {
  1157. int end_zone = 0; /* Inclusive. 0 = ZONE_DMA */
  1158. unsigned long lru_pages = 0;
  1159. /* The swap token gets in the way of swapout... */
  1160. if (!priority)
  1161. disable_swap_token();
  1162. all_zones_ok = 1;
  1163. /*
  1164. * Scan in the highmem->dma direction for the highest
  1165. * zone which needs scanning
  1166. */
  1167. for (i = pgdat->nr_zones - 1; i >= 0; i--) {
  1168. struct zone *zone = pgdat->node_zones + i;
  1169. if (!populated_zone(zone))
  1170. continue;
  1171. if (zone->all_unreclaimable && priority != DEF_PRIORITY)
  1172. continue;
  1173. if (!zone_watermark_ok(zone, order, zone->pages_high,
  1174. 0, 0)) {
  1175. end_zone = i;
  1176. break;
  1177. }
  1178. }
  1179. if (i < 0)
  1180. goto out;
  1181. for (i = 0; i <= end_zone; i++) {
  1182. struct zone *zone = pgdat->node_zones + i;
  1183. lru_pages += zone_page_state(zone, NR_ACTIVE)
  1184. + zone_page_state(zone, NR_INACTIVE);
  1185. }
  1186. /*
  1187. * Now scan the zone in the dma->highmem direction, stopping
  1188. * at the last zone which needs scanning.
  1189. *
  1190. * We do this because the page allocator works in the opposite
  1191. * direction. This prevents the page allocator from allocating
  1192. * pages behind kswapd's direction of progress, which would
  1193. * cause too much scanning of the lower zones.
  1194. */
  1195. for (i = 0; i <= end_zone; i++) {
  1196. struct zone *zone = pgdat->node_zones + i;
  1197. int nr_slab;
  1198. if (!populated_zone(zone))
  1199. continue;
  1200. if (zone->all_unreclaimable && priority != DEF_PRIORITY)
  1201. continue;
  1202. if (!zone_watermark_ok(zone, order, zone->pages_high,
  1203. end_zone, 0))
  1204. all_zones_ok = 0;
  1205. temp_priority[i] = priority;
  1206. sc.nr_scanned = 0;
  1207. note_zone_scanning_priority(zone, priority);
  1208. nr_reclaimed += shrink_zone(priority, zone, &sc);
  1209. reclaim_state->reclaimed_slab = 0;
  1210. nr_slab = shrink_slab(sc.nr_scanned, GFP_KERNEL,
  1211. lru_pages);
  1212. nr_reclaimed += reclaim_state->reclaimed_slab;
  1213. total_scanned += sc.nr_scanned;
  1214. if (zone->all_unreclaimable)
  1215. continue;
  1216. if (nr_slab == 0 && zone->pages_scanned >=
  1217. (zone_page_state(zone, NR_ACTIVE)
  1218. + zone_page_state(zone, NR_INACTIVE)) * 6)
  1219. zone->all_unreclaimable = 1;
  1220. /*
  1221. * If we've done a decent amount of scanning and
  1222. * the reclaim ratio is low, start doing writepage
  1223. * even in laptop mode
  1224. */
  1225. if (total_scanned > SWAP_CLUSTER_MAX * 2 &&
  1226. total_scanned > nr_reclaimed + nr_reclaimed / 2)
  1227. sc.may_writepage = 1;
  1228. }
  1229. if (all_zones_ok)
  1230. break; /* kswapd: all done */
  1231. /*
  1232. * OK, kswapd is getting into trouble. Take a nap, then take
  1233. * another pass across the zones.
  1234. */
  1235. if (total_scanned && priority < DEF_PRIORITY - 2)
  1236. congestion_wait(WRITE, HZ/10);
  1237. /*
  1238. * We do this so kswapd doesn't build up large priorities for
  1239. * example when it is freeing in parallel with allocators. It
  1240. * matches the direct reclaim path behaviour in terms of impact
  1241. * on zone->*_priority.
  1242. */
  1243. if (nr_reclaimed >= SWAP_CLUSTER_MAX)
  1244. break;
  1245. }
  1246. out:
  1247. /*
  1248. * Note within each zone the priority level at which this zone was
  1249. * brought into a happy state. So that the next thread which scans this
  1250. * zone will start out at that priority level.
  1251. */
  1252. for (i = 0; i < pgdat->nr_zones; i++) {
  1253. struct zone *zone = pgdat->node_zones + i;
  1254. zone->prev_priority = temp_priority[i];
  1255. }
  1256. if (!all_zones_ok) {
  1257. cond_resched();
  1258. try_to_freeze();
  1259. goto loop_again;
  1260. }
  1261. return nr_reclaimed;
  1262. }
  1263. /*
  1264. * The background pageout daemon, started as a kernel thread
  1265. * from the init process.
  1266. *
  1267. * This basically trickles out pages so that we have _some_
  1268. * free memory available even if there is no other activity
  1269. * that frees anything up. This is needed for things like routing
  1270. * etc, where we otherwise might have all activity going on in
  1271. * asynchronous contexts that cannot page things out.
  1272. *
  1273. * If there are applications that are active memory-allocators
  1274. * (most normal use), this basically shouldn't matter.
  1275. */
  1276. static int kswapd(void *p)
  1277. {
  1278. unsigned long order;
  1279. pg_data_t *pgdat = (pg_data_t*)p;
  1280. struct task_struct *tsk = current;
  1281. DEFINE_WAIT(wait);
  1282. struct reclaim_state reclaim_state = {
  1283. .reclaimed_slab = 0,
  1284. };
  1285. cpumask_t cpumask;
  1286. cpumask = node_to_cpumask(pgdat->node_id);
  1287. if (!cpus_empty(cpumask))
  1288. set_cpus_allowed(tsk, cpumask);
  1289. current->reclaim_state = &reclaim_state;
  1290. /*
  1291. * Tell the memory management that we're a "memory allocator",
  1292. * and that if we need more memory we should get access to it
  1293. * regardless (see "__alloc_pages()"). "kswapd" should
  1294. * never get caught in the normal page freeing logic.
  1295. *
  1296. * (Kswapd normally doesn't need memory anyway, but sometimes
  1297. * you need a small amount of memory in order to be able to
  1298. * page out something else, and this flag essentially protects
  1299. * us from recursively trying to free more memory as we're
  1300. * trying to free the first piece of memory in the first place).
  1301. */
  1302. tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD;
  1303. set_freezable();
  1304. order = 0;
  1305. for ( ; ; ) {
  1306. unsigned long new_order;
  1307. prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
  1308. new_order = pgdat->kswapd_max_order;
  1309. pgdat->kswapd_max_order = 0;
  1310. if (order < new_order) {
  1311. /*
  1312. * Don't sleep if someone wants a larger 'order'
  1313. * allocation
  1314. */
  1315. order = new_order;
  1316. } else {
  1317. if (!freezing(current))
  1318. schedule();
  1319. order = pgdat->kswapd_max_order;
  1320. }
  1321. finish_wait(&pgdat->kswapd_wait, &wait);
  1322. if (!try_to_freeze()) {
  1323. /* We can speed up thawing tasks if we don't call
  1324. * balance_pgdat after returning from the refrigerator
  1325. */
  1326. balance_pgdat(pgdat, order);
  1327. }
  1328. }
  1329. return 0;
  1330. }
  1331. /*
  1332. * A zone is low on free memory, so wake its kswapd task to service it.
  1333. */
  1334. void wakeup_kswapd(struct zone *zone, int order)
  1335. {
  1336. pg_data_t *pgdat;
  1337. if (!populated_zone(zone))
  1338. return;
  1339. pgdat = zone->zone_pgdat;
  1340. if (zone_watermark_ok(zone, order, zone->pages_low, 0, 0))
  1341. return;
  1342. if (pgdat->kswapd_max_order < order)
  1343. pgdat->kswapd_max_order = order;
  1344. if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
  1345. return;
  1346. if (!waitqueue_active(&pgdat->kswapd_wait))
  1347. return;
  1348. wake_up_interruptible(&pgdat->kswapd_wait);
  1349. }
  1350. #ifdef CONFIG_PM
  1351. /*
  1352. * Helper function for shrink_all_memory(). Tries to reclaim 'nr_pages' pages
  1353. * from LRU lists system-wide, for given pass and priority, and returns the
  1354. * number of reclaimed pages
  1355. *
  1356. * For pass > 3 we also try to shrink the LRU lists that contain a few pages
  1357. */
  1358. static unsigned long shrink_all_zones(unsigned long nr_pages, int prio,
  1359. int pass, struct scan_control *sc)
  1360. {
  1361. struct zone *zone;
  1362. unsigned long nr_to_scan, ret = 0;
  1363. for_each_zone(zone) {
  1364. if (!populated_zone(zone))
  1365. continue;
  1366. if (zone->all_unreclaimable && prio != DEF_PRIORITY)
  1367. continue;
  1368. /* For pass = 0 we don't shrink the active list */
  1369. if (pass > 0) {
  1370. zone->nr_scan_active +=
  1371. (zone_page_state(zone, NR_ACTIVE) >> prio) + 1;
  1372. if (zone->nr_scan_active >= nr_pages || pass > 3) {
  1373. zone->nr_scan_active = 0;
  1374. nr_to_scan = min(nr_pages,
  1375. zone_page_state(zone, NR_ACTIVE));
  1376. shrink_active_list(nr_to_scan, zone, sc, prio);
  1377. }
  1378. }
  1379. zone->nr_scan_inactive +=
  1380. (zone_page_state(zone, NR_INACTIVE) >> prio) + 1;
  1381. if (zone->nr_scan_inactive >= nr_pages || pass > 3) {
  1382. zone->nr_scan_inactive = 0;
  1383. nr_to_scan = min(nr_pages,
  1384. zone_page_state(zone, NR_INACTIVE));
  1385. ret += shrink_inactive_list(nr_to_scan, zone, sc);
  1386. if (ret >= nr_pages)
  1387. return ret;
  1388. }
  1389. }
  1390. return ret;
  1391. }
  1392. static unsigned long count_lru_pages(void)
  1393. {
  1394. return global_page_state(NR_ACTIVE) + global_page_state(NR_INACTIVE);
  1395. }
  1396. /*
  1397. * Try to free `nr_pages' of memory, system-wide, and return the number of
  1398. * freed pages.
  1399. *
  1400. * Rather than trying to age LRUs the aim is to preserve the overall
  1401. * LRU order by reclaiming preferentially
  1402. * inactive > active > active referenced > active mapped
  1403. */
  1404. unsigned long shrink_all_memory(unsigned long nr_pages)
  1405. {
  1406. unsigned long lru_pages, nr_slab;
  1407. unsigned long ret = 0;
  1408. int pass;
  1409. struct reclaim_state reclaim_state;
  1410. struct scan_control sc = {
  1411. .gfp_mask = GFP_KERNEL,
  1412. .may_swap = 0,
  1413. .swap_cluster_max = nr_pages,
  1414. .may_writepage = 1,
  1415. .swappiness = vm_swappiness,
  1416. };
  1417. current->reclaim_state = &reclaim_state;
  1418. lru_pages = count_lru_pages();
  1419. nr_slab = global_page_state(NR_SLAB_RECLAIMABLE);
  1420. /* If slab caches are huge, it's better to hit them first */
  1421. while (nr_slab >= lru_pages) {
  1422. reclaim_state.reclaimed_slab = 0;
  1423. shrink_slab(nr_pages, sc.gfp_mask, lru_pages);
  1424. if (!reclaim_state.reclaimed_slab)
  1425. break;
  1426. ret += reclaim_state.reclaimed_slab;
  1427. if (ret >= nr_pages)
  1428. goto out;
  1429. nr_slab -= reclaim_state.reclaimed_slab;
  1430. }
  1431. /*
  1432. * We try to shrink LRUs in 5 passes:
  1433. * 0 = Reclaim from inactive_list only
  1434. * 1 = Reclaim from active list but don't reclaim mapped
  1435. * 2 = 2nd pass of type 1
  1436. * 3 = Reclaim mapped (normal reclaim)
  1437. * 4 = 2nd pass of type 3
  1438. */
  1439. for (pass = 0; pass < 5; pass++) {
  1440. int prio;
  1441. /* Force reclaiming mapped pages in the passes #3 and #4 */
  1442. if (pass > 2) {
  1443. sc.may_swap = 1;
  1444. sc.swappiness = 100;
  1445. }
  1446. for (prio = DEF_PRIORITY; prio >= 0; prio--) {
  1447. unsigned long nr_to_scan = nr_pages - ret;
  1448. sc.nr_scanned = 0;
  1449. ret += shrink_all_zones(nr_to_scan, prio, pass, &sc);
  1450. if (ret >= nr_pages)
  1451. goto out;
  1452. reclaim_state.reclaimed_slab = 0;
  1453. shrink_slab(sc.nr_scanned, sc.gfp_mask,
  1454. count_lru_pages());
  1455. ret += reclaim_state.reclaimed_slab;
  1456. if (ret >= nr_pages)
  1457. goto out;
  1458. if (sc.nr_scanned && prio < DEF_PRIORITY - 2)
  1459. congestion_wait(WRITE, HZ / 10);
  1460. }
  1461. }
  1462. /*
  1463. * If ret = 0, we could not shrink LRUs, but there may be something
  1464. * in slab caches
  1465. */
  1466. if (!ret) {
  1467. do {
  1468. reclaim_state.reclaimed_slab = 0;
  1469. shrink_slab(nr_pages, sc.gfp_mask, count_lru_pages());
  1470. ret += reclaim_state.reclaimed_slab;
  1471. } while (ret < nr_pages && reclaim_state.reclaimed_slab > 0);
  1472. }
  1473. out:
  1474. current->reclaim_state = NULL;
  1475. return ret;
  1476. }
  1477. #endif
  1478. /* It's optimal to keep kswapds on the same CPUs as their memory, but
  1479. not required for correctness. So if the last cpu in a node goes
  1480. away, we get changed to run anywhere: as the first one comes back,
  1481. restore their cpu bindings. */
  1482. static int __devinit cpu_callback(struct notifier_block *nfb,
  1483. unsigned long action, void *hcpu)
  1484. {
  1485. pg_data_t *pgdat;
  1486. cpumask_t mask;
  1487. if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN) {
  1488. for_each_online_pgdat(pgdat) {
  1489. mask = node_to_cpumask(pgdat->node_id);
  1490. if (any_online_cpu(mask) != NR_CPUS)
  1491. /* One of our CPUs online: restore mask */
  1492. set_cpus_allowed(pgdat->kswapd, mask);
  1493. }
  1494. }
  1495. return NOTIFY_OK;
  1496. }
  1497. /*
  1498. * This kswapd start function will be called by init and node-hot-add.
  1499. * On node-hot-add, kswapd will moved to proper cpus if cpus are hot-added.
  1500. */
  1501. int kswapd_run(int nid)
  1502. {
  1503. pg_data_t *pgdat = NODE_DATA(nid);
  1504. int ret = 0;
  1505. if (pgdat->kswapd)
  1506. return 0;
  1507. pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid);
  1508. if (IS_ERR(pgdat->kswapd)) {
  1509. /* failure at boot is fatal */
  1510. BUG_ON(system_state == SYSTEM_BOOTING);
  1511. printk("Failed to start kswapd on node %d\n",nid);
  1512. ret = -1;
  1513. }
  1514. return ret;
  1515. }
  1516. static int __init kswapd_init(void)
  1517. {
  1518. int nid;
  1519. swap_setup();
  1520. for_each_online_node(nid)
  1521. kswapd_run(nid);
  1522. hotcpu_notifier(cpu_callback, 0);
  1523. return 0;
  1524. }
  1525. module_init(kswapd_init)
  1526. #ifdef CONFIG_NUMA
  1527. /*
  1528. * Zone reclaim mode
  1529. *
  1530. * If non-zero call zone_reclaim when the number of free pages falls below
  1531. * the watermarks.
  1532. */
  1533. int zone_reclaim_mode __read_mostly;
  1534. #define RECLAIM_OFF 0
  1535. #define RECLAIM_ZONE (1<<0) /* Run shrink_cache on the zone */
  1536. #define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */
  1537. #define RECLAIM_SWAP (1<<2) /* Swap pages out during reclaim */
  1538. /*
  1539. * Priority for ZONE_RECLAIM. This determines the fraction of pages
  1540. * of a node considered for each zone_reclaim. 4 scans 1/16th of
  1541. * a zone.
  1542. */
  1543. #define ZONE_RECLAIM_PRIORITY 4
  1544. /*
  1545. * Percentage of pages in a zone that must be unmapped for zone_reclaim to
  1546. * occur.
  1547. */
  1548. int sysctl_min_unmapped_ratio = 1;
  1549. /*
  1550. * If the number of slab pages in a zone grows beyond this percentage then
  1551. * slab reclaim needs to occur.
  1552. */
  1553. int sysctl_min_slab_ratio = 5;
  1554. /*
  1555. * Try to free up some pages from this zone through reclaim.
  1556. */
  1557. static int __zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
  1558. {
  1559. /* Minimum pages needed in order to stay on node */
  1560. const unsigned long nr_pages = 1 << order;
  1561. struct task_struct *p = current;
  1562. struct reclaim_state reclaim_state;
  1563. int priority;
  1564. unsigned long nr_reclaimed = 0;
  1565. struct scan_control sc = {
  1566. .may_writepage = !!(zone_reclaim_mode & RECLAIM_WRITE),
  1567. .may_swap = !!(zone_reclaim_mode & RECLAIM_SWAP),
  1568. .swap_cluster_max = max_t(unsigned long, nr_pages,
  1569. SWAP_CLUSTER_MAX),
  1570. .gfp_mask = gfp_mask,
  1571. .swappiness = vm_swappiness,
  1572. };
  1573. unsigned long slab_reclaimable;
  1574. disable_swap_token();
  1575. cond_resched();
  1576. /*
  1577. * We need to be able to allocate from the reserves for RECLAIM_SWAP
  1578. * and we also need to be able to write out pages for RECLAIM_WRITE
  1579. * and RECLAIM_SWAP.
  1580. */
  1581. p->flags |= PF_MEMALLOC | PF_SWAPWRITE;
  1582. reclaim_state.reclaimed_slab = 0;
  1583. p->reclaim_state = &reclaim_state;
  1584. if (zone_page_state(zone, NR_FILE_PAGES) -
  1585. zone_page_state(zone, NR_FILE_MAPPED) >
  1586. zone->min_unmapped_pages) {
  1587. /*
  1588. * Free memory by calling shrink zone with increasing
  1589. * priorities until we have enough memory freed.
  1590. */
  1591. priority = ZONE_RECLAIM_PRIORITY;
  1592. do {
  1593. note_zone_scanning_priority(zone, priority);
  1594. nr_reclaimed += shrink_zone(priority, zone, &sc);
  1595. priority--;
  1596. } while (priority >= 0 && nr_reclaimed < nr_pages);
  1597. }
  1598. slab_reclaimable = zone_page_state(zone, NR_SLAB_RECLAIMABLE);
  1599. if (slab_reclaimable > zone->min_slab_pages) {
  1600. /*
  1601. * shrink_slab() does not currently allow us to determine how
  1602. * many pages were freed in this zone. So we take the current
  1603. * number of slab pages and shake the slab until it is reduced
  1604. * by the same nr_pages that we used for reclaiming unmapped
  1605. * pages.
  1606. *
  1607. * Note that shrink_slab will free memory on all zones and may
  1608. * take a long time.
  1609. */
  1610. while (shrink_slab(sc.nr_scanned, gfp_mask, order) &&
  1611. zone_page_state(zone, NR_SLAB_RECLAIMABLE) >
  1612. slab_reclaimable - nr_pages)
  1613. ;
  1614. /*
  1615. * Update nr_reclaimed by the number of slab pages we
  1616. * reclaimed from this zone.
  1617. */
  1618. nr_reclaimed += slab_reclaimable -
  1619. zone_page_state(zone, NR_SLAB_RECLAIMABLE);
  1620. }
  1621. p->reclaim_state = NULL;
  1622. current->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE);
  1623. return nr_reclaimed >= nr_pages;
  1624. }
  1625. int zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
  1626. {
  1627. cpumask_t mask;
  1628. int node_id;
  1629. /*
  1630. * Zone reclaim reclaims unmapped file backed pages and
  1631. * slab pages if we are over the defined limits.
  1632. *
  1633. * A small portion of unmapped file backed pages is needed for
  1634. * file I/O otherwise pages read by file I/O will be immediately
  1635. * thrown out if the zone is overallocated. So we do not reclaim
  1636. * if less than a specified percentage of the zone is used by
  1637. * unmapped file backed pages.
  1638. */
  1639. if (zone_page_state(zone, NR_FILE_PAGES) -
  1640. zone_page_state(zone, NR_FILE_MAPPED) <= zone->min_unmapped_pages
  1641. && zone_page_state(zone, NR_SLAB_RECLAIMABLE)
  1642. <= zone->min_slab_pages)
  1643. return 0;
  1644. /*
  1645. * Avoid concurrent zone reclaims, do not reclaim in a zone that does
  1646. * not have reclaimable pages and if we should not delay the allocation
  1647. * then do not scan.
  1648. */
  1649. if (!(gfp_mask & __GFP_WAIT) ||
  1650. zone->all_unreclaimable ||
  1651. atomic_read(&zone->reclaim_in_progress) > 0 ||
  1652. (current->flags & PF_MEMALLOC))
  1653. return 0;
  1654. /*
  1655. * Only run zone reclaim on the local zone or on zones that do not
  1656. * have associated processors. This will favor the local processor
  1657. * over remote processors and spread off node memory allocations
  1658. * as wide as possible.
  1659. */
  1660. node_id = zone_to_nid(zone);
  1661. mask = node_to_cpumask(node_id);
  1662. if (!cpus_empty(mask) && node_id != numa_node_id())
  1663. return 0;
  1664. return __zone_reclaim(zone, gfp_mask, order);
  1665. }
  1666. #endif