vmscan.c 52 KB

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