vmscan.c 40 KB

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