vmscan.c 37 KB

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