vmscan.c 50 KB

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