vmscan.c 52 KB

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