vmscan.c 50 KB

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