vmscan.c 52 KB

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