swap.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  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. /*
  168. * pagevec_move_tail() must be called with IRQ disabled.
  169. * Otherwise this may cause nasty races.
  170. */
  171. static void pagevec_move_tail(struct pagevec *pvec)
  172. {
  173. int i;
  174. int pgmoved = 0;
  175. struct zone *zone = NULL;
  176. for (i = 0; i < pagevec_count(pvec); i++) {
  177. struct page *page = pvec->pages[i];
  178. struct zone *pagezone = page_zone(page);
  179. if (pagezone != zone) {
  180. if (zone)
  181. spin_unlock(&zone->lru_lock);
  182. zone = pagezone;
  183. spin_lock(&zone->lru_lock);
  184. }
  185. if (PageLRU(page) && !PageActive(page) && !PageUnevictable(page)) {
  186. enum lru_list lru = page_lru_base_type(page);
  187. list_move_tail(&page->lru, &zone->lru[lru].list);
  188. mem_cgroup_rotate_reclaimable_page(page);
  189. pgmoved++;
  190. }
  191. }
  192. if (zone)
  193. spin_unlock(&zone->lru_lock);
  194. __count_vm_events(PGROTATED, pgmoved);
  195. release_pages(pvec->pages, pvec->nr, pvec->cold);
  196. pagevec_reinit(pvec);
  197. }
  198. /*
  199. * Writeback is about to end against a page which has been marked for immediate
  200. * reclaim. If it still appears to be reclaimable, move it to the tail of the
  201. * inactive list.
  202. */
  203. void rotate_reclaimable_page(struct page *page)
  204. {
  205. if (!PageLocked(page) && !PageDirty(page) && !PageActive(page) &&
  206. !PageUnevictable(page) && PageLRU(page)) {
  207. struct pagevec *pvec;
  208. unsigned long flags;
  209. page_cache_get(page);
  210. local_irq_save(flags);
  211. pvec = &__get_cpu_var(lru_rotate_pvecs);
  212. if (!pagevec_add(pvec, page))
  213. pagevec_move_tail(pvec);
  214. local_irq_restore(flags);
  215. }
  216. }
  217. static void update_page_reclaim_stat(struct zone *zone, struct page *page,
  218. int file, int rotated)
  219. {
  220. struct zone_reclaim_stat *reclaim_stat = &zone->reclaim_stat;
  221. struct zone_reclaim_stat *memcg_reclaim_stat;
  222. memcg_reclaim_stat = mem_cgroup_get_reclaim_stat_from_page(page);
  223. reclaim_stat->recent_scanned[file]++;
  224. if (rotated)
  225. reclaim_stat->recent_rotated[file]++;
  226. if (!memcg_reclaim_stat)
  227. return;
  228. memcg_reclaim_stat->recent_scanned[file]++;
  229. if (rotated)
  230. memcg_reclaim_stat->recent_rotated[file]++;
  231. }
  232. /*
  233. * FIXME: speed this up?
  234. */
  235. void activate_page(struct page *page)
  236. {
  237. struct zone *zone = page_zone(page);
  238. spin_lock_irq(&zone->lru_lock);
  239. if (PageLRU(page) && !PageActive(page) && !PageUnevictable(page)) {
  240. int file = page_is_file_cache(page);
  241. int lru = page_lru_base_type(page);
  242. del_page_from_lru_list(zone, page, lru);
  243. SetPageActive(page);
  244. lru += LRU_ACTIVE;
  245. add_page_to_lru_list(zone, page, lru);
  246. __count_vm_event(PGACTIVATE);
  247. update_page_reclaim_stat(zone, page, file, 1);
  248. }
  249. spin_unlock_irq(&zone->lru_lock);
  250. }
  251. /*
  252. * Mark a page as having seen activity.
  253. *
  254. * inactive,unreferenced -> inactive,referenced
  255. * inactive,referenced -> active,unreferenced
  256. * active,unreferenced -> active,referenced
  257. */
  258. void mark_page_accessed(struct page *page)
  259. {
  260. if (!PageActive(page) && !PageUnevictable(page) &&
  261. PageReferenced(page) && PageLRU(page)) {
  262. activate_page(page);
  263. ClearPageReferenced(page);
  264. } else if (!PageReferenced(page)) {
  265. SetPageReferenced(page);
  266. }
  267. }
  268. EXPORT_SYMBOL(mark_page_accessed);
  269. void __lru_cache_add(struct page *page, enum lru_list lru)
  270. {
  271. struct pagevec *pvec = &get_cpu_var(lru_add_pvecs)[lru];
  272. page_cache_get(page);
  273. if (!pagevec_add(pvec, page))
  274. ____pagevec_lru_add(pvec, lru);
  275. put_cpu_var(lru_add_pvecs);
  276. }
  277. EXPORT_SYMBOL(__lru_cache_add);
  278. /**
  279. * lru_cache_add_lru - add a page to a page list
  280. * @page: the page to be added to the LRU.
  281. * @lru: the LRU list to which the page is added.
  282. */
  283. void lru_cache_add_lru(struct page *page, enum lru_list lru)
  284. {
  285. if (PageActive(page)) {
  286. VM_BUG_ON(PageUnevictable(page));
  287. ClearPageActive(page);
  288. } else if (PageUnevictable(page)) {
  289. VM_BUG_ON(PageActive(page));
  290. ClearPageUnevictable(page);
  291. }
  292. VM_BUG_ON(PageLRU(page) || PageActive(page) || PageUnevictable(page));
  293. __lru_cache_add(page, lru);
  294. }
  295. /**
  296. * add_page_to_unevictable_list - add a page to the unevictable list
  297. * @page: the page to be added to the unevictable list
  298. *
  299. * Add page directly to its zone's unevictable list. To avoid races with
  300. * tasks that might be making the page evictable, through eg. munlock,
  301. * munmap or exit, while it's not on the lru, we want to add the page
  302. * while it's locked or otherwise "invisible" to other tasks. This is
  303. * difficult to do when using the pagevec cache, so bypass that.
  304. */
  305. void add_page_to_unevictable_list(struct page *page)
  306. {
  307. struct zone *zone = page_zone(page);
  308. spin_lock_irq(&zone->lru_lock);
  309. SetPageUnevictable(page);
  310. SetPageLRU(page);
  311. add_page_to_lru_list(zone, page, LRU_UNEVICTABLE);
  312. spin_unlock_irq(&zone->lru_lock);
  313. }
  314. /*
  315. * If the page can not be invalidated, it is moved to the
  316. * inactive list to speed up its reclaim. It is moved to the
  317. * head of the list, rather than the tail, to give the flusher
  318. * threads some time to write it out, as this is much more
  319. * effective than the single-page writeout from reclaim.
  320. *
  321. * If the page isn't page_mapped and dirty/writeback, the page
  322. * could reclaim asap using PG_reclaim.
  323. *
  324. * 1. active, mapped page -> none
  325. * 2. active, dirty/writeback page -> inactive, head, PG_reclaim
  326. * 3. inactive, mapped page -> none
  327. * 4. inactive, dirty/writeback page -> inactive, head, PG_reclaim
  328. * 5. inactive, clean -> inactive, tail
  329. * 6. Others -> none
  330. *
  331. * In 4, why it moves inactive's head, the VM expects the page would
  332. * be write it out by flusher threads as this is much more effective
  333. * than the single-page writeout from reclaim.
  334. */
  335. static void lru_deactivate(struct page *page, struct zone *zone)
  336. {
  337. int lru, file;
  338. bool active;
  339. if (!PageLRU(page))
  340. return;
  341. /* Some processes are using the page */
  342. if (page_mapped(page))
  343. return;
  344. active = PageActive(page);
  345. file = page_is_file_cache(page);
  346. lru = page_lru_base_type(page);
  347. del_page_from_lru_list(zone, page, lru + active);
  348. ClearPageActive(page);
  349. ClearPageReferenced(page);
  350. add_page_to_lru_list(zone, page, lru);
  351. if (PageWriteback(page) || PageDirty(page)) {
  352. /*
  353. * PG_reclaim could be raced with end_page_writeback
  354. * It can make readahead confusing. But race window
  355. * is _really_ small and it's non-critical problem.
  356. */
  357. SetPageReclaim(page);
  358. } else {
  359. /*
  360. * The page's writeback ends up during pagevec
  361. * We moves tha page into tail of inactive.
  362. */
  363. list_move_tail(&page->lru, &zone->lru[lru].list);
  364. mem_cgroup_rotate_reclaimable_page(page);
  365. __count_vm_event(PGROTATED);
  366. }
  367. if (active)
  368. __count_vm_event(PGDEACTIVATE);
  369. update_page_reclaim_stat(zone, page, file, 0);
  370. }
  371. static void ____pagevec_lru_deactivate(struct pagevec *pvec)
  372. {
  373. int i;
  374. struct zone *zone = NULL;
  375. for (i = 0; i < pagevec_count(pvec); i++) {
  376. struct page *page = pvec->pages[i];
  377. struct zone *pagezone = page_zone(page);
  378. if (pagezone != zone) {
  379. if (zone)
  380. spin_unlock_irq(&zone->lru_lock);
  381. zone = pagezone;
  382. spin_lock_irq(&zone->lru_lock);
  383. }
  384. lru_deactivate(page, zone);
  385. }
  386. if (zone)
  387. spin_unlock_irq(&zone->lru_lock);
  388. release_pages(pvec->pages, pvec->nr, pvec->cold);
  389. pagevec_reinit(pvec);
  390. }
  391. /*
  392. * Drain pages out of the cpu's pagevecs.
  393. * Either "cpu" is the current CPU, and preemption has already been
  394. * disabled; or "cpu" is being hot-unplugged, and is already dead.
  395. */
  396. static void drain_cpu_pagevecs(int cpu)
  397. {
  398. struct pagevec *pvecs = per_cpu(lru_add_pvecs, cpu);
  399. struct pagevec *pvec;
  400. int lru;
  401. for_each_lru(lru) {
  402. pvec = &pvecs[lru - LRU_BASE];
  403. if (pagevec_count(pvec))
  404. ____pagevec_lru_add(pvec, lru);
  405. }
  406. pvec = &per_cpu(lru_rotate_pvecs, cpu);
  407. if (pagevec_count(pvec)) {
  408. unsigned long flags;
  409. /* No harm done if a racing interrupt already did this */
  410. local_irq_save(flags);
  411. pagevec_move_tail(pvec);
  412. local_irq_restore(flags);
  413. }
  414. pvec = &per_cpu(lru_deactivate_pvecs, cpu);
  415. if (pagevec_count(pvec))
  416. ____pagevec_lru_deactivate(pvec);
  417. }
  418. /**
  419. * deactivate_page - forcefully deactivate a page
  420. * @page: page to deactivate
  421. *
  422. * This function hints the VM that @page is a good reclaim candidate,
  423. * for example if its invalidation fails due to the page being dirty
  424. * or under writeback.
  425. */
  426. void deactivate_page(struct page *page)
  427. {
  428. if (likely(get_page_unless_zero(page))) {
  429. struct pagevec *pvec = &get_cpu_var(lru_deactivate_pvecs);
  430. if (!pagevec_add(pvec, page))
  431. ____pagevec_lru_deactivate(pvec);
  432. put_cpu_var(lru_deactivate_pvecs);
  433. }
  434. }
  435. void lru_add_drain(void)
  436. {
  437. drain_cpu_pagevecs(get_cpu());
  438. put_cpu();
  439. }
  440. static void lru_add_drain_per_cpu(struct work_struct *dummy)
  441. {
  442. lru_add_drain();
  443. }
  444. /*
  445. * Returns 0 for success
  446. */
  447. int lru_add_drain_all(void)
  448. {
  449. return schedule_on_each_cpu(lru_add_drain_per_cpu);
  450. }
  451. /*
  452. * Batched page_cache_release(). Decrement the reference count on all the
  453. * passed pages. If it fell to zero then remove the page from the LRU and
  454. * free it.
  455. *
  456. * Avoid taking zone->lru_lock if possible, but if it is taken, retain it
  457. * for the remainder of the operation.
  458. *
  459. * The locking in this function is against shrink_inactive_list(): we recheck
  460. * the page count inside the lock to see whether shrink_inactive_list()
  461. * grabbed the page via the LRU. If it did, give up: shrink_inactive_list()
  462. * will free it.
  463. */
  464. void release_pages(struct page **pages, int nr, int cold)
  465. {
  466. int i;
  467. struct pagevec pages_to_free;
  468. struct zone *zone = NULL;
  469. unsigned long uninitialized_var(flags);
  470. pagevec_init(&pages_to_free, cold);
  471. for (i = 0; i < nr; i++) {
  472. struct page *page = pages[i];
  473. if (unlikely(PageCompound(page))) {
  474. if (zone) {
  475. spin_unlock_irqrestore(&zone->lru_lock, flags);
  476. zone = NULL;
  477. }
  478. put_compound_page(page);
  479. continue;
  480. }
  481. if (!put_page_testzero(page))
  482. continue;
  483. if (PageLRU(page)) {
  484. struct zone *pagezone = page_zone(page);
  485. if (pagezone != zone) {
  486. if (zone)
  487. spin_unlock_irqrestore(&zone->lru_lock,
  488. flags);
  489. zone = pagezone;
  490. spin_lock_irqsave(&zone->lru_lock, flags);
  491. }
  492. VM_BUG_ON(!PageLRU(page));
  493. __ClearPageLRU(page);
  494. del_page_from_lru(zone, page);
  495. }
  496. if (!pagevec_add(&pages_to_free, page)) {
  497. if (zone) {
  498. spin_unlock_irqrestore(&zone->lru_lock, flags);
  499. zone = NULL;
  500. }
  501. __pagevec_free(&pages_to_free);
  502. pagevec_reinit(&pages_to_free);
  503. }
  504. }
  505. if (zone)
  506. spin_unlock_irqrestore(&zone->lru_lock, flags);
  507. pagevec_free(&pages_to_free);
  508. }
  509. EXPORT_SYMBOL(release_pages);
  510. /*
  511. * The pages which we're about to release may be in the deferred lru-addition
  512. * queues. That would prevent them from really being freed right now. That's
  513. * OK from a correctness point of view but is inefficient - those pages may be
  514. * cache-warm and we want to give them back to the page allocator ASAP.
  515. *
  516. * So __pagevec_release() will drain those queues here. __pagevec_lru_add()
  517. * and __pagevec_lru_add_active() call release_pages() directly to avoid
  518. * mutual recursion.
  519. */
  520. void __pagevec_release(struct pagevec *pvec)
  521. {
  522. lru_add_drain();
  523. release_pages(pvec->pages, pagevec_count(pvec), pvec->cold);
  524. pagevec_reinit(pvec);
  525. }
  526. EXPORT_SYMBOL(__pagevec_release);
  527. /* used by __split_huge_page_refcount() */
  528. void lru_add_page_tail(struct zone* zone,
  529. struct page *page, struct page *page_tail)
  530. {
  531. int active;
  532. enum lru_list lru;
  533. const int file = 0;
  534. struct list_head *head;
  535. VM_BUG_ON(!PageHead(page));
  536. VM_BUG_ON(PageCompound(page_tail));
  537. VM_BUG_ON(PageLRU(page_tail));
  538. VM_BUG_ON(!spin_is_locked(&zone->lru_lock));
  539. SetPageLRU(page_tail);
  540. if (page_evictable(page_tail, NULL)) {
  541. if (PageActive(page)) {
  542. SetPageActive(page_tail);
  543. active = 1;
  544. lru = LRU_ACTIVE_ANON;
  545. } else {
  546. active = 0;
  547. lru = LRU_INACTIVE_ANON;
  548. }
  549. update_page_reclaim_stat(zone, page_tail, file, active);
  550. if (likely(PageLRU(page)))
  551. head = page->lru.prev;
  552. else
  553. head = &zone->lru[lru].list;
  554. __add_page_to_lru_list(zone, page_tail, lru, head);
  555. } else {
  556. SetPageUnevictable(page_tail);
  557. add_page_to_lru_list(zone, page_tail, LRU_UNEVICTABLE);
  558. }
  559. }
  560. /*
  561. * Add the passed pages to the LRU, then drop the caller's refcount
  562. * on them. Reinitialises the caller's pagevec.
  563. */
  564. void ____pagevec_lru_add(struct pagevec *pvec, enum lru_list lru)
  565. {
  566. int i;
  567. struct zone *zone = NULL;
  568. VM_BUG_ON(is_unevictable_lru(lru));
  569. for (i = 0; i < pagevec_count(pvec); i++) {
  570. struct page *page = pvec->pages[i];
  571. struct zone *pagezone = page_zone(page);
  572. int file;
  573. int active;
  574. if (pagezone != zone) {
  575. if (zone)
  576. spin_unlock_irq(&zone->lru_lock);
  577. zone = pagezone;
  578. spin_lock_irq(&zone->lru_lock);
  579. }
  580. VM_BUG_ON(PageActive(page));
  581. VM_BUG_ON(PageUnevictable(page));
  582. VM_BUG_ON(PageLRU(page));
  583. SetPageLRU(page);
  584. active = is_active_lru(lru);
  585. file = is_file_lru(lru);
  586. if (active)
  587. SetPageActive(page);
  588. update_page_reclaim_stat(zone, page, file, active);
  589. add_page_to_lru_list(zone, page, lru);
  590. }
  591. if (zone)
  592. spin_unlock_irq(&zone->lru_lock);
  593. release_pages(pvec->pages, pvec->nr, pvec->cold);
  594. pagevec_reinit(pvec);
  595. }
  596. EXPORT_SYMBOL(____pagevec_lru_add);
  597. /*
  598. * Try to drop buffers from the pages in a pagevec
  599. */
  600. void pagevec_strip(struct pagevec *pvec)
  601. {
  602. int i;
  603. for (i = 0; i < pagevec_count(pvec); i++) {
  604. struct page *page = pvec->pages[i];
  605. if (page_has_private(page) && trylock_page(page)) {
  606. if (page_has_private(page))
  607. try_to_release_page(page, 0);
  608. unlock_page(page);
  609. }
  610. }
  611. }
  612. /**
  613. * pagevec_lookup - gang pagecache lookup
  614. * @pvec: Where the resulting pages are placed
  615. * @mapping: The address_space to search
  616. * @start: The starting page index
  617. * @nr_pages: The maximum number of pages
  618. *
  619. * pagevec_lookup() will search for and return a group of up to @nr_pages pages
  620. * in the mapping. The pages are placed in @pvec. pagevec_lookup() takes a
  621. * reference against the pages in @pvec.
  622. *
  623. * The search returns a group of mapping-contiguous pages with ascending
  624. * indexes. There may be holes in the indices due to not-present pages.
  625. *
  626. * pagevec_lookup() returns the number of pages which were found.
  627. */
  628. unsigned pagevec_lookup(struct pagevec *pvec, struct address_space *mapping,
  629. pgoff_t start, unsigned nr_pages)
  630. {
  631. pvec->nr = find_get_pages(mapping, start, nr_pages, pvec->pages);
  632. return pagevec_count(pvec);
  633. }
  634. EXPORT_SYMBOL(pagevec_lookup);
  635. unsigned pagevec_lookup_tag(struct pagevec *pvec, struct address_space *mapping,
  636. pgoff_t *index, int tag, unsigned nr_pages)
  637. {
  638. pvec->nr = find_get_pages_tag(mapping, index, tag,
  639. nr_pages, pvec->pages);
  640. return pagevec_count(pvec);
  641. }
  642. EXPORT_SYMBOL(pagevec_lookup_tag);
  643. /*
  644. * Perform any setup for the swap system
  645. */
  646. void __init swap_setup(void)
  647. {
  648. unsigned long megs = totalram_pages >> (20 - PAGE_SHIFT);
  649. #ifdef CONFIG_SWAP
  650. bdi_init(swapper_space.backing_dev_info);
  651. #endif
  652. /* Use a smaller cluster for small-memory machines */
  653. if (megs < 16)
  654. page_cluster = 2;
  655. else
  656. page_cluster = 3;
  657. /*
  658. * Right now other parts of the system means that we
  659. * _really_ don't want to cluster much more
  660. */
  661. }