swap.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  1. /*
  2. * linux/mm/swap.c
  3. *
  4. * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
  5. */
  6. /*
  7. * This file contains the default values for the operation of the
  8. * Linux VM subsystem. Fine-tuning documentation can be found in
  9. * Documentation/sysctl/vm.txt.
  10. * Started 18.12.91
  11. * Swap aging added 23.2.95, Stephen Tweedie.
  12. * Buffermem limits added 12.3.98, Rik van Riel.
  13. */
  14. #include <linux/mm.h>
  15. #include <linux/sched.h>
  16. #include <linux/kernel_stat.h>
  17. #include <linux/swap.h>
  18. #include <linux/mman.h>
  19. #include <linux/pagemap.h>
  20. #include <linux/pagevec.h>
  21. #include <linux/init.h>
  22. #include <linux/module.h>
  23. #include <linux/mm_inline.h>
  24. #include <linux/buffer_head.h> /* for try_to_release_page() */
  25. #include <linux/percpu_counter.h>
  26. #include <linux/percpu.h>
  27. #include <linux/cpu.h>
  28. #include <linux/notifier.h>
  29. #include <linux/backing-dev.h>
  30. #include <linux/memcontrol.h>
  31. #include <linux/gfp.h>
  32. #include "internal.h"
  33. /* How many pages do we try to swap or page in/out together? */
  34. int page_cluster;
  35. static DEFINE_PER_CPU(struct pagevec[NR_LRU_LISTS], lru_add_pvecs);
  36. static DEFINE_PER_CPU(struct pagevec, lru_rotate_pvecs);
  37. static DEFINE_PER_CPU(struct pagevec, lru_deactivate_pvecs);
  38. /*
  39. * This path almost never happens for VM activity - pages are normally
  40. * freed via pagevecs. But it gets used by networking.
  41. */
  42. static void __page_cache_release(struct page *page)
  43. {
  44. if (PageLRU(page)) {
  45. unsigned long flags;
  46. struct zone *zone = page_zone(page);
  47. spin_lock_irqsave(&zone->lru_lock, flags);
  48. VM_BUG_ON(!PageLRU(page));
  49. __ClearPageLRU(page);
  50. del_page_from_lru(zone, page);
  51. spin_unlock_irqrestore(&zone->lru_lock, flags);
  52. }
  53. }
  54. static void __put_single_page(struct page *page)
  55. {
  56. __page_cache_release(page);
  57. free_hot_cold_page(page, 0);
  58. }
  59. static void __put_compound_page(struct page *page)
  60. {
  61. compound_page_dtor *dtor;
  62. __page_cache_release(page);
  63. dtor = get_compound_page_dtor(page);
  64. (*dtor)(page);
  65. }
  66. static void put_compound_page(struct page *page)
  67. {
  68. if (unlikely(PageTail(page))) {
  69. /* __split_huge_page_refcount can run under us */
  70. struct page *page_head = page->first_page;
  71. smp_rmb();
  72. /*
  73. * If PageTail is still set after smp_rmb() we can be sure
  74. * that the page->first_page we read wasn't a dangling pointer.
  75. * See __split_huge_page_refcount() smp_wmb().
  76. */
  77. if (likely(PageTail(page) && get_page_unless_zero(page_head))) {
  78. unsigned long flags;
  79. /*
  80. * Verify that our page_head wasn't converted
  81. * to a a regular page before we got a
  82. * reference on it.
  83. */
  84. if (unlikely(!PageHead(page_head))) {
  85. /* PageHead is cleared after PageTail */
  86. smp_rmb();
  87. VM_BUG_ON(PageTail(page));
  88. goto out_put_head;
  89. }
  90. /*
  91. * Only run compound_lock on a valid PageHead,
  92. * after having it pinned with
  93. * get_page_unless_zero() above.
  94. */
  95. smp_mb();
  96. /* page_head wasn't a dangling pointer */
  97. flags = compound_lock_irqsave(page_head);
  98. if (unlikely(!PageTail(page))) {
  99. /* __split_huge_page_refcount run before us */
  100. compound_unlock_irqrestore(page_head, flags);
  101. VM_BUG_ON(PageHead(page_head));
  102. out_put_head:
  103. if (put_page_testzero(page_head))
  104. __put_single_page(page_head);
  105. out_put_single:
  106. if (put_page_testzero(page))
  107. __put_single_page(page);
  108. return;
  109. }
  110. VM_BUG_ON(page_head != page->first_page);
  111. /*
  112. * We can release the refcount taken by
  113. * get_page_unless_zero now that
  114. * split_huge_page_refcount is blocked on the
  115. * compound_lock.
  116. */
  117. if (put_page_testzero(page_head))
  118. VM_BUG_ON(1);
  119. /* __split_huge_page_refcount will wait now */
  120. VM_BUG_ON(atomic_read(&page->_count) <= 0);
  121. atomic_dec(&page->_count);
  122. VM_BUG_ON(atomic_read(&page_head->_count) <= 0);
  123. compound_unlock_irqrestore(page_head, flags);
  124. if (put_page_testzero(page_head)) {
  125. if (PageHead(page_head))
  126. __put_compound_page(page_head);
  127. else
  128. __put_single_page(page_head);
  129. }
  130. } else {
  131. /* page_head is a dangling pointer */
  132. VM_BUG_ON(PageTail(page));
  133. goto out_put_single;
  134. }
  135. } else if (put_page_testzero(page)) {
  136. if (PageHead(page))
  137. __put_compound_page(page);
  138. else
  139. __put_single_page(page);
  140. }
  141. }
  142. void put_page(struct page *page)
  143. {
  144. if (unlikely(PageCompound(page)))
  145. put_compound_page(page);
  146. else if (put_page_testzero(page))
  147. __put_single_page(page);
  148. }
  149. EXPORT_SYMBOL(put_page);
  150. /**
  151. * put_pages_list() - release a list of pages
  152. * @pages: list of pages threaded on page->lru
  153. *
  154. * Release a list of pages which are strung together on page.lru. Currently
  155. * used by read_cache_pages() and related error recovery code.
  156. */
  157. void put_pages_list(struct list_head *pages)
  158. {
  159. while (!list_empty(pages)) {
  160. struct page *victim;
  161. victim = list_entry(pages->prev, struct page, lru);
  162. list_del(&victim->lru);
  163. page_cache_release(victim);
  164. }
  165. }
  166. EXPORT_SYMBOL(put_pages_list);
  167. static void pagevec_lru_move_fn(struct pagevec *pvec,
  168. void (*move_fn)(struct page *page, void *arg),
  169. void *arg)
  170. {
  171. int i;
  172. struct zone *zone = NULL;
  173. unsigned long flags = 0;
  174. for (i = 0; i < pagevec_count(pvec); i++) {
  175. struct page *page = pvec->pages[i];
  176. struct zone *pagezone = page_zone(page);
  177. if (pagezone != zone) {
  178. if (zone)
  179. spin_unlock_irqrestore(&zone->lru_lock, flags);
  180. zone = pagezone;
  181. spin_lock_irqsave(&zone->lru_lock, flags);
  182. }
  183. (*move_fn)(page, arg);
  184. }
  185. if (zone)
  186. spin_unlock_irqrestore(&zone->lru_lock, flags);
  187. release_pages(pvec->pages, pvec->nr, pvec->cold);
  188. pagevec_reinit(pvec);
  189. }
  190. static void pagevec_move_tail_fn(struct page *page, void *arg)
  191. {
  192. int *pgmoved = arg;
  193. struct zone *zone = page_zone(page);
  194. if (PageLRU(page) && !PageActive(page) && !PageUnevictable(page)) {
  195. enum lru_list lru = page_lru_base_type(page);
  196. list_move_tail(&page->lru, &zone->lru[lru].list);
  197. mem_cgroup_rotate_reclaimable_page(page);
  198. (*pgmoved)++;
  199. }
  200. }
  201. /*
  202. * pagevec_move_tail() must be called with IRQ disabled.
  203. * Otherwise this may cause nasty races.
  204. */
  205. static void pagevec_move_tail(struct pagevec *pvec)
  206. {
  207. int pgmoved = 0;
  208. pagevec_lru_move_fn(pvec, pagevec_move_tail_fn, &pgmoved);
  209. __count_vm_events(PGROTATED, pgmoved);
  210. }
  211. /*
  212. * Writeback is about to end against a page which has been marked for immediate
  213. * reclaim. If it still appears to be reclaimable, move it to the tail of the
  214. * inactive list.
  215. */
  216. void rotate_reclaimable_page(struct page *page)
  217. {
  218. if (!PageLocked(page) && !PageDirty(page) && !PageActive(page) &&
  219. !PageUnevictable(page) && PageLRU(page)) {
  220. struct pagevec *pvec;
  221. unsigned long flags;
  222. page_cache_get(page);
  223. local_irq_save(flags);
  224. pvec = &__get_cpu_var(lru_rotate_pvecs);
  225. if (!pagevec_add(pvec, page))
  226. pagevec_move_tail(pvec);
  227. local_irq_restore(flags);
  228. }
  229. }
  230. static void update_page_reclaim_stat(struct zone *zone, struct page *page,
  231. int file, int rotated)
  232. {
  233. struct zone_reclaim_stat *reclaim_stat = &zone->reclaim_stat;
  234. struct zone_reclaim_stat *memcg_reclaim_stat;
  235. memcg_reclaim_stat = mem_cgroup_get_reclaim_stat_from_page(page);
  236. reclaim_stat->recent_scanned[file]++;
  237. if (rotated)
  238. reclaim_stat->recent_rotated[file]++;
  239. if (!memcg_reclaim_stat)
  240. return;
  241. memcg_reclaim_stat->recent_scanned[file]++;
  242. if (rotated)
  243. memcg_reclaim_stat->recent_rotated[file]++;
  244. }
  245. /*
  246. * FIXME: speed this up?
  247. */
  248. void activate_page(struct page *page)
  249. {
  250. struct zone *zone = page_zone(page);
  251. spin_lock_irq(&zone->lru_lock);
  252. if (PageLRU(page) && !PageActive(page) && !PageUnevictable(page)) {
  253. int file = page_is_file_cache(page);
  254. int lru = page_lru_base_type(page);
  255. del_page_from_lru_list(zone, page, lru);
  256. SetPageActive(page);
  257. lru += LRU_ACTIVE;
  258. add_page_to_lru_list(zone, page, lru);
  259. __count_vm_event(PGACTIVATE);
  260. update_page_reclaim_stat(zone, page, file, 1);
  261. }
  262. spin_unlock_irq(&zone->lru_lock);
  263. }
  264. /*
  265. * Mark a page as having seen activity.
  266. *
  267. * inactive,unreferenced -> inactive,referenced
  268. * inactive,referenced -> active,unreferenced
  269. * active,unreferenced -> active,referenced
  270. */
  271. void mark_page_accessed(struct page *page)
  272. {
  273. if (!PageActive(page) && !PageUnevictable(page) &&
  274. PageReferenced(page) && PageLRU(page)) {
  275. activate_page(page);
  276. ClearPageReferenced(page);
  277. } else if (!PageReferenced(page)) {
  278. SetPageReferenced(page);
  279. }
  280. }
  281. EXPORT_SYMBOL(mark_page_accessed);
  282. void __lru_cache_add(struct page *page, enum lru_list lru)
  283. {
  284. struct pagevec *pvec = &get_cpu_var(lru_add_pvecs)[lru];
  285. page_cache_get(page);
  286. if (!pagevec_add(pvec, page))
  287. ____pagevec_lru_add(pvec, lru);
  288. put_cpu_var(lru_add_pvecs);
  289. }
  290. EXPORT_SYMBOL(__lru_cache_add);
  291. /**
  292. * lru_cache_add_lru - add a page to a page list
  293. * @page: the page to be added to the LRU.
  294. * @lru: the LRU list to which the page is added.
  295. */
  296. void lru_cache_add_lru(struct page *page, enum lru_list lru)
  297. {
  298. if (PageActive(page)) {
  299. VM_BUG_ON(PageUnevictable(page));
  300. ClearPageActive(page);
  301. } else if (PageUnevictable(page)) {
  302. VM_BUG_ON(PageActive(page));
  303. ClearPageUnevictable(page);
  304. }
  305. VM_BUG_ON(PageLRU(page) || PageActive(page) || PageUnevictable(page));
  306. __lru_cache_add(page, lru);
  307. }
  308. /**
  309. * add_page_to_unevictable_list - add a page to the unevictable list
  310. * @page: the page to be added to the unevictable list
  311. *
  312. * Add page directly to its zone's unevictable list. To avoid races with
  313. * tasks that might be making the page evictable, through eg. munlock,
  314. * munmap or exit, while it's not on the lru, we want to add the page
  315. * while it's locked or otherwise "invisible" to other tasks. This is
  316. * difficult to do when using the pagevec cache, so bypass that.
  317. */
  318. void add_page_to_unevictable_list(struct page *page)
  319. {
  320. struct zone *zone = page_zone(page);
  321. spin_lock_irq(&zone->lru_lock);
  322. SetPageUnevictable(page);
  323. SetPageLRU(page);
  324. add_page_to_lru_list(zone, page, LRU_UNEVICTABLE);
  325. spin_unlock_irq(&zone->lru_lock);
  326. }
  327. /*
  328. * If the page can not be invalidated, it is moved to the
  329. * inactive list to speed up its reclaim. It is moved to the
  330. * head of the list, rather than the tail, to give the flusher
  331. * threads some time to write it out, as this is much more
  332. * effective than the single-page writeout from reclaim.
  333. *
  334. * If the page isn't page_mapped and dirty/writeback, the page
  335. * could reclaim asap using PG_reclaim.
  336. *
  337. * 1. active, mapped page -> none
  338. * 2. active, dirty/writeback page -> inactive, head, PG_reclaim
  339. * 3. inactive, mapped page -> none
  340. * 4. inactive, dirty/writeback page -> inactive, head, PG_reclaim
  341. * 5. inactive, clean -> inactive, tail
  342. * 6. Others -> none
  343. *
  344. * In 4, why it moves inactive's head, the VM expects the page would
  345. * be write it out by flusher threads as this is much more effective
  346. * than the single-page writeout from reclaim.
  347. */
  348. static void lru_deactivate_fn(struct page *page, void *arg)
  349. {
  350. int lru, file;
  351. bool active;
  352. struct zone *zone = page_zone(page);
  353. if (!PageLRU(page))
  354. return;
  355. if (PageUnevictable(page))
  356. return;
  357. /* Some processes are using the page */
  358. if (page_mapped(page))
  359. return;
  360. active = PageActive(page);
  361. file = page_is_file_cache(page);
  362. lru = page_lru_base_type(page);
  363. del_page_from_lru_list(zone, page, lru + active);
  364. ClearPageActive(page);
  365. ClearPageReferenced(page);
  366. add_page_to_lru_list(zone, page, lru);
  367. if (PageWriteback(page) || PageDirty(page)) {
  368. /*
  369. * PG_reclaim could be raced with end_page_writeback
  370. * It can make readahead confusing. But race window
  371. * is _really_ small and it's non-critical problem.
  372. */
  373. SetPageReclaim(page);
  374. } else {
  375. /*
  376. * The page's writeback ends up during pagevec
  377. * We moves tha page into tail of inactive.
  378. */
  379. list_move_tail(&page->lru, &zone->lru[lru].list);
  380. mem_cgroup_rotate_reclaimable_page(page);
  381. __count_vm_event(PGROTATED);
  382. }
  383. if (active)
  384. __count_vm_event(PGDEACTIVATE);
  385. update_page_reclaim_stat(zone, page, file, 0);
  386. }
  387. /*
  388. * Drain pages out of the cpu's pagevecs.
  389. * Either "cpu" is the current CPU, and preemption has already been
  390. * disabled; or "cpu" is being hot-unplugged, and is already dead.
  391. */
  392. static void drain_cpu_pagevecs(int cpu)
  393. {
  394. struct pagevec *pvecs = per_cpu(lru_add_pvecs, cpu);
  395. struct pagevec *pvec;
  396. int lru;
  397. for_each_lru(lru) {
  398. pvec = &pvecs[lru - LRU_BASE];
  399. if (pagevec_count(pvec))
  400. ____pagevec_lru_add(pvec, lru);
  401. }
  402. pvec = &per_cpu(lru_rotate_pvecs, cpu);
  403. if (pagevec_count(pvec)) {
  404. unsigned long flags;
  405. /* No harm done if a racing interrupt already did this */
  406. local_irq_save(flags);
  407. pagevec_move_tail(pvec);
  408. local_irq_restore(flags);
  409. }
  410. pvec = &per_cpu(lru_deactivate_pvecs, cpu);
  411. if (pagevec_count(pvec))
  412. pagevec_lru_move_fn(pvec, lru_deactivate_fn, NULL);
  413. }
  414. /**
  415. * deactivate_page - forcefully deactivate a page
  416. * @page: page to deactivate
  417. *
  418. * This function hints the VM that @page is a good reclaim candidate,
  419. * for example if its invalidation fails due to the page being dirty
  420. * or under writeback.
  421. */
  422. void deactivate_page(struct page *page)
  423. {
  424. if (likely(get_page_unless_zero(page))) {
  425. struct pagevec *pvec = &get_cpu_var(lru_deactivate_pvecs);
  426. if (!pagevec_add(pvec, page))
  427. pagevec_lru_move_fn(pvec, lru_deactivate_fn, NULL);
  428. put_cpu_var(lru_deactivate_pvecs);
  429. }
  430. }
  431. void lru_add_drain(void)
  432. {
  433. drain_cpu_pagevecs(get_cpu());
  434. put_cpu();
  435. }
  436. static void lru_add_drain_per_cpu(struct work_struct *dummy)
  437. {
  438. lru_add_drain();
  439. }
  440. /*
  441. * Returns 0 for success
  442. */
  443. int lru_add_drain_all(void)
  444. {
  445. return schedule_on_each_cpu(lru_add_drain_per_cpu);
  446. }
  447. /*
  448. * Batched page_cache_release(). Decrement the reference count on all the
  449. * passed pages. If it fell to zero then remove the page from the LRU and
  450. * free it.
  451. *
  452. * Avoid taking zone->lru_lock if possible, but if it is taken, retain it
  453. * for the remainder of the operation.
  454. *
  455. * The locking in this function is against shrink_inactive_list(): we recheck
  456. * the page count inside the lock to see whether shrink_inactive_list()
  457. * grabbed the page via the LRU. If it did, give up: shrink_inactive_list()
  458. * will free it.
  459. */
  460. void release_pages(struct page **pages, int nr, int cold)
  461. {
  462. int i;
  463. struct pagevec pages_to_free;
  464. struct zone *zone = NULL;
  465. unsigned long uninitialized_var(flags);
  466. pagevec_init(&pages_to_free, cold);
  467. for (i = 0; i < nr; i++) {
  468. struct page *page = pages[i];
  469. if (unlikely(PageCompound(page))) {
  470. if (zone) {
  471. spin_unlock_irqrestore(&zone->lru_lock, flags);
  472. zone = NULL;
  473. }
  474. put_compound_page(page);
  475. continue;
  476. }
  477. if (!put_page_testzero(page))
  478. continue;
  479. if (PageLRU(page)) {
  480. struct zone *pagezone = page_zone(page);
  481. if (pagezone != zone) {
  482. if (zone)
  483. spin_unlock_irqrestore(&zone->lru_lock,
  484. flags);
  485. zone = pagezone;
  486. spin_lock_irqsave(&zone->lru_lock, flags);
  487. }
  488. VM_BUG_ON(!PageLRU(page));
  489. __ClearPageLRU(page);
  490. del_page_from_lru(zone, page);
  491. }
  492. if (!pagevec_add(&pages_to_free, page)) {
  493. if (zone) {
  494. spin_unlock_irqrestore(&zone->lru_lock, flags);
  495. zone = NULL;
  496. }
  497. __pagevec_free(&pages_to_free);
  498. pagevec_reinit(&pages_to_free);
  499. }
  500. }
  501. if (zone)
  502. spin_unlock_irqrestore(&zone->lru_lock, flags);
  503. pagevec_free(&pages_to_free);
  504. }
  505. EXPORT_SYMBOL(release_pages);
  506. /*
  507. * The pages which we're about to release may be in the deferred lru-addition
  508. * queues. That would prevent them from really being freed right now. That's
  509. * OK from a correctness point of view but is inefficient - those pages may be
  510. * cache-warm and we want to give them back to the page allocator ASAP.
  511. *
  512. * So __pagevec_release() will drain those queues here. __pagevec_lru_add()
  513. * and __pagevec_lru_add_active() call release_pages() directly to avoid
  514. * mutual recursion.
  515. */
  516. void __pagevec_release(struct pagevec *pvec)
  517. {
  518. lru_add_drain();
  519. release_pages(pvec->pages, pagevec_count(pvec), pvec->cold);
  520. pagevec_reinit(pvec);
  521. }
  522. EXPORT_SYMBOL(__pagevec_release);
  523. /* used by __split_huge_page_refcount() */
  524. void lru_add_page_tail(struct zone* zone,
  525. struct page *page, struct page *page_tail)
  526. {
  527. int active;
  528. enum lru_list lru;
  529. const int file = 0;
  530. struct list_head *head;
  531. VM_BUG_ON(!PageHead(page));
  532. VM_BUG_ON(PageCompound(page_tail));
  533. VM_BUG_ON(PageLRU(page_tail));
  534. VM_BUG_ON(!spin_is_locked(&zone->lru_lock));
  535. SetPageLRU(page_tail);
  536. if (page_evictable(page_tail, NULL)) {
  537. if (PageActive(page)) {
  538. SetPageActive(page_tail);
  539. active = 1;
  540. lru = LRU_ACTIVE_ANON;
  541. } else {
  542. active = 0;
  543. lru = LRU_INACTIVE_ANON;
  544. }
  545. update_page_reclaim_stat(zone, page_tail, file, active);
  546. if (likely(PageLRU(page)))
  547. head = page->lru.prev;
  548. else
  549. head = &zone->lru[lru].list;
  550. __add_page_to_lru_list(zone, page_tail, lru, head);
  551. } else {
  552. SetPageUnevictable(page_tail);
  553. add_page_to_lru_list(zone, page_tail, LRU_UNEVICTABLE);
  554. }
  555. }
  556. static void ____pagevec_lru_add_fn(struct page *page, void *arg)
  557. {
  558. enum lru_list lru = (enum lru_list)arg;
  559. struct zone *zone = page_zone(page);
  560. int file = is_file_lru(lru);
  561. int active = is_active_lru(lru);
  562. VM_BUG_ON(PageActive(page));
  563. VM_BUG_ON(PageUnevictable(page));
  564. VM_BUG_ON(PageLRU(page));
  565. SetPageLRU(page);
  566. if (active)
  567. SetPageActive(page);
  568. update_page_reclaim_stat(zone, page, file, active);
  569. add_page_to_lru_list(zone, page, lru);
  570. }
  571. /*
  572. * Add the passed pages to the LRU, then drop the caller's refcount
  573. * on them. Reinitialises the caller's pagevec.
  574. */
  575. void ____pagevec_lru_add(struct pagevec *pvec, enum lru_list lru)
  576. {
  577. VM_BUG_ON(is_unevictable_lru(lru));
  578. pagevec_lru_move_fn(pvec, ____pagevec_lru_add_fn, (void *)lru);
  579. }
  580. EXPORT_SYMBOL(____pagevec_lru_add);
  581. /*
  582. * Try to drop buffers from the pages in a pagevec
  583. */
  584. void pagevec_strip(struct pagevec *pvec)
  585. {
  586. int i;
  587. for (i = 0; i < pagevec_count(pvec); i++) {
  588. struct page *page = pvec->pages[i];
  589. if (page_has_private(page) && trylock_page(page)) {
  590. if (page_has_private(page))
  591. try_to_release_page(page, 0);
  592. unlock_page(page);
  593. }
  594. }
  595. }
  596. /**
  597. * pagevec_lookup - gang pagecache lookup
  598. * @pvec: Where the resulting pages are placed
  599. * @mapping: The address_space to search
  600. * @start: The starting page index
  601. * @nr_pages: The maximum number of pages
  602. *
  603. * pagevec_lookup() will search for and return a group of up to @nr_pages pages
  604. * in the mapping. The pages are placed in @pvec. pagevec_lookup() takes a
  605. * reference against the pages in @pvec.
  606. *
  607. * The search returns a group of mapping-contiguous pages with ascending
  608. * indexes. There may be holes in the indices due to not-present pages.
  609. *
  610. * pagevec_lookup() returns the number of pages which were found.
  611. */
  612. unsigned pagevec_lookup(struct pagevec *pvec, struct address_space *mapping,
  613. pgoff_t start, unsigned nr_pages)
  614. {
  615. pvec->nr = find_get_pages(mapping, start, nr_pages, pvec->pages);
  616. return pagevec_count(pvec);
  617. }
  618. EXPORT_SYMBOL(pagevec_lookup);
  619. unsigned pagevec_lookup_tag(struct pagevec *pvec, struct address_space *mapping,
  620. pgoff_t *index, int tag, unsigned nr_pages)
  621. {
  622. pvec->nr = find_get_pages_tag(mapping, index, tag,
  623. nr_pages, pvec->pages);
  624. return pagevec_count(pvec);
  625. }
  626. EXPORT_SYMBOL(pagevec_lookup_tag);
  627. /*
  628. * Perform any setup for the swap system
  629. */
  630. void __init swap_setup(void)
  631. {
  632. unsigned long megs = totalram_pages >> (20 - PAGE_SHIFT);
  633. #ifdef CONFIG_SWAP
  634. bdi_init(swapper_space.backing_dev_info);
  635. #endif
  636. /* Use a smaller cluster for small-memory machines */
  637. if (megs < 16)
  638. page_cluster = 2;
  639. else
  640. page_cluster = 3;
  641. /*
  642. * Right now other parts of the system means that we
  643. * _really_ don't want to cluster much more
  644. */
  645. }