swap.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  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/export.h>
  23. #include <linux/mm_inline.h>
  24. #include <linux/percpu_counter.h>
  25. #include <linux/percpu.h>
  26. #include <linux/cpu.h>
  27. #include <linux/notifier.h>
  28. #include <linux/backing-dev.h>
  29. #include <linux/memcontrol.h>
  30. #include <linux/gfp.h>
  31. #include "internal.h"
  32. /* How many pages do we try to swap or page in/out together? */
  33. int page_cluster;
  34. static DEFINE_PER_CPU(struct pagevec[NR_LRU_LISTS], lru_add_pvecs);
  35. static DEFINE_PER_CPU(struct pagevec, lru_rotate_pvecs);
  36. static DEFINE_PER_CPU(struct pagevec, lru_deactivate_pvecs);
  37. /*
  38. * This path almost never happens for VM activity - pages are normally
  39. * freed via pagevecs. But it gets used by networking.
  40. */
  41. static void __page_cache_release(struct page *page)
  42. {
  43. if (PageLRU(page)) {
  44. unsigned long flags;
  45. struct zone *zone = page_zone(page);
  46. spin_lock_irqsave(&zone->lru_lock, flags);
  47. VM_BUG_ON(!PageLRU(page));
  48. __ClearPageLRU(page);
  49. del_page_from_lru_list(zone, page, page_off_lru(page));
  50. spin_unlock_irqrestore(&zone->lru_lock, flags);
  51. }
  52. }
  53. static void __put_single_page(struct page *page)
  54. {
  55. __page_cache_release(page);
  56. free_hot_cold_page(page, 0);
  57. }
  58. static void __put_compound_page(struct page *page)
  59. {
  60. compound_page_dtor *dtor;
  61. __page_cache_release(page);
  62. dtor = get_compound_page_dtor(page);
  63. (*dtor)(page);
  64. }
  65. static void put_compound_page(struct page *page)
  66. {
  67. if (unlikely(PageTail(page))) {
  68. /* __split_huge_page_refcount can run under us */
  69. struct page *page_head = compound_trans_head(page);
  70. if (likely(page != page_head &&
  71. get_page_unless_zero(page_head))) {
  72. unsigned long flags;
  73. /*
  74. * THP can not break up slab pages so avoid taking
  75. * compound_lock(). Slab performs non-atomic bit ops
  76. * on page->flags for better performance. In particular
  77. * slab_unlock() in slub used to be a hot path. It is
  78. * still hot on arches that do not support
  79. * this_cpu_cmpxchg_double().
  80. */
  81. if (PageSlab(page_head)) {
  82. if (PageTail(page)) {
  83. if (put_page_testzero(page_head))
  84. VM_BUG_ON(1);
  85. atomic_dec(&page->_mapcount);
  86. goto skip_lock_tail;
  87. } else
  88. goto skip_lock;
  89. }
  90. /*
  91. * page_head wasn't a dangling pointer but it
  92. * may not be a head page anymore by the time
  93. * we obtain the lock. That is ok as long as it
  94. * can't be freed from under us.
  95. */
  96. flags = compound_lock_irqsave(page_head);
  97. if (unlikely(!PageTail(page))) {
  98. /* __split_huge_page_refcount run before us */
  99. compound_unlock_irqrestore(page_head, flags);
  100. skip_lock:
  101. if (put_page_testzero(page_head))
  102. __put_single_page(page_head);
  103. out_put_single:
  104. if (put_page_testzero(page))
  105. __put_single_page(page);
  106. return;
  107. }
  108. VM_BUG_ON(page_head != page->first_page);
  109. /*
  110. * We can release the refcount taken by
  111. * get_page_unless_zero() now that
  112. * __split_huge_page_refcount() is blocked on
  113. * the compound_lock.
  114. */
  115. if (put_page_testzero(page_head))
  116. VM_BUG_ON(1);
  117. /* __split_huge_page_refcount will wait now */
  118. VM_BUG_ON(page_mapcount(page) <= 0);
  119. atomic_dec(&page->_mapcount);
  120. VM_BUG_ON(atomic_read(&page_head->_count) <= 0);
  121. VM_BUG_ON(atomic_read(&page->_count) != 0);
  122. compound_unlock_irqrestore(page_head, flags);
  123. skip_lock_tail:
  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. * This function is exported but must not be called by anything other
  152. * than get_page(). It implements the slow path of get_page().
  153. */
  154. bool __get_page_tail(struct page *page)
  155. {
  156. /*
  157. * This takes care of get_page() if run on a tail page
  158. * returned by one of the get_user_pages/follow_page variants.
  159. * get_user_pages/follow_page itself doesn't need the compound
  160. * lock because it runs __get_page_tail_foll() under the
  161. * proper PT lock that already serializes against
  162. * split_huge_page().
  163. */
  164. unsigned long flags;
  165. bool got = false;
  166. struct page *page_head = compound_trans_head(page);
  167. if (likely(page != page_head && get_page_unless_zero(page_head))) {
  168. /* Ref to put_compound_page() comment. */
  169. if (PageSlab(page_head)) {
  170. if (likely(PageTail(page))) {
  171. __get_page_tail_foll(page, false);
  172. return true;
  173. } else {
  174. put_page(page_head);
  175. return false;
  176. }
  177. }
  178. /*
  179. * page_head wasn't a dangling pointer but it
  180. * may not be a head page anymore by the time
  181. * we obtain the lock. That is ok as long as it
  182. * can't be freed from under us.
  183. */
  184. flags = compound_lock_irqsave(page_head);
  185. /* here __split_huge_page_refcount won't run anymore */
  186. if (likely(PageTail(page))) {
  187. __get_page_tail_foll(page, false);
  188. got = true;
  189. }
  190. compound_unlock_irqrestore(page_head, flags);
  191. if (unlikely(!got))
  192. put_page(page_head);
  193. }
  194. return got;
  195. }
  196. EXPORT_SYMBOL(__get_page_tail);
  197. /**
  198. * put_pages_list() - release a list of pages
  199. * @pages: list of pages threaded on page->lru
  200. *
  201. * Release a list of pages which are strung together on page.lru. Currently
  202. * used by read_cache_pages() and related error recovery code.
  203. */
  204. void put_pages_list(struct list_head *pages)
  205. {
  206. while (!list_empty(pages)) {
  207. struct page *victim;
  208. victim = list_entry(pages->prev, struct page, lru);
  209. list_del(&victim->lru);
  210. page_cache_release(victim);
  211. }
  212. }
  213. EXPORT_SYMBOL(put_pages_list);
  214. static void pagevec_lru_move_fn(struct pagevec *pvec,
  215. void (*move_fn)(struct page *page, void *arg),
  216. void *arg)
  217. {
  218. int i;
  219. struct zone *zone = NULL;
  220. unsigned long flags = 0;
  221. for (i = 0; i < pagevec_count(pvec); i++) {
  222. struct page *page = pvec->pages[i];
  223. struct zone *pagezone = page_zone(page);
  224. if (pagezone != zone) {
  225. if (zone)
  226. spin_unlock_irqrestore(&zone->lru_lock, flags);
  227. zone = pagezone;
  228. spin_lock_irqsave(&zone->lru_lock, flags);
  229. }
  230. (*move_fn)(page, arg);
  231. }
  232. if (zone)
  233. spin_unlock_irqrestore(&zone->lru_lock, flags);
  234. release_pages(pvec->pages, pvec->nr, pvec->cold);
  235. pagevec_reinit(pvec);
  236. }
  237. static void pagevec_move_tail_fn(struct page *page, void *arg)
  238. {
  239. int *pgmoved = arg;
  240. if (PageLRU(page) && !PageActive(page) && !PageUnevictable(page)) {
  241. enum lru_list lru = page_lru_base_type(page);
  242. struct lruvec *lruvec;
  243. lruvec = mem_cgroup_lru_move_lists(page_zone(page),
  244. page, lru, lru);
  245. list_move_tail(&page->lru, &lruvec->lists[lru]);
  246. (*pgmoved)++;
  247. }
  248. }
  249. /*
  250. * pagevec_move_tail() must be called with IRQ disabled.
  251. * Otherwise this may cause nasty races.
  252. */
  253. static void pagevec_move_tail(struct pagevec *pvec)
  254. {
  255. int pgmoved = 0;
  256. pagevec_lru_move_fn(pvec, pagevec_move_tail_fn, &pgmoved);
  257. __count_vm_events(PGROTATED, pgmoved);
  258. }
  259. /*
  260. * Writeback is about to end against a page which has been marked for immediate
  261. * reclaim. If it still appears to be reclaimable, move it to the tail of the
  262. * inactive list.
  263. */
  264. void rotate_reclaimable_page(struct page *page)
  265. {
  266. if (!PageLocked(page) && !PageDirty(page) && !PageActive(page) &&
  267. !PageUnevictable(page) && PageLRU(page)) {
  268. struct pagevec *pvec;
  269. unsigned long flags;
  270. page_cache_get(page);
  271. local_irq_save(flags);
  272. pvec = &__get_cpu_var(lru_rotate_pvecs);
  273. if (!pagevec_add(pvec, page))
  274. pagevec_move_tail(pvec);
  275. local_irq_restore(flags);
  276. }
  277. }
  278. static void update_page_reclaim_stat(struct zone *zone, struct page *page,
  279. int file, int rotated)
  280. {
  281. struct zone_reclaim_stat *reclaim_stat;
  282. reclaim_stat = mem_cgroup_get_reclaim_stat_from_page(page);
  283. if (!reclaim_stat)
  284. reclaim_stat = &zone->lruvec.reclaim_stat;
  285. reclaim_stat->recent_scanned[file]++;
  286. if (rotated)
  287. reclaim_stat->recent_rotated[file]++;
  288. }
  289. static void __activate_page(struct page *page, void *arg)
  290. {
  291. struct zone *zone = page_zone(page);
  292. if (PageLRU(page) && !PageActive(page) && !PageUnevictable(page)) {
  293. int file = page_is_file_cache(page);
  294. int lru = page_lru_base_type(page);
  295. del_page_from_lru_list(zone, page, lru);
  296. SetPageActive(page);
  297. lru += LRU_ACTIVE;
  298. add_page_to_lru_list(zone, page, lru);
  299. __count_vm_event(PGACTIVATE);
  300. update_page_reclaim_stat(zone, page, file, 1);
  301. }
  302. }
  303. #ifdef CONFIG_SMP
  304. static DEFINE_PER_CPU(struct pagevec, activate_page_pvecs);
  305. static void activate_page_drain(int cpu)
  306. {
  307. struct pagevec *pvec = &per_cpu(activate_page_pvecs, cpu);
  308. if (pagevec_count(pvec))
  309. pagevec_lru_move_fn(pvec, __activate_page, NULL);
  310. }
  311. void activate_page(struct page *page)
  312. {
  313. if (PageLRU(page) && !PageActive(page) && !PageUnevictable(page)) {
  314. struct pagevec *pvec = &get_cpu_var(activate_page_pvecs);
  315. page_cache_get(page);
  316. if (!pagevec_add(pvec, page))
  317. pagevec_lru_move_fn(pvec, __activate_page, NULL);
  318. put_cpu_var(activate_page_pvecs);
  319. }
  320. }
  321. #else
  322. static inline void activate_page_drain(int cpu)
  323. {
  324. }
  325. void activate_page(struct page *page)
  326. {
  327. struct zone *zone = page_zone(page);
  328. spin_lock_irq(&zone->lru_lock);
  329. __activate_page(page, NULL);
  330. spin_unlock_irq(&zone->lru_lock);
  331. }
  332. #endif
  333. /*
  334. * Mark a page as having seen activity.
  335. *
  336. * inactive,unreferenced -> inactive,referenced
  337. * inactive,referenced -> active,unreferenced
  338. * active,unreferenced -> active,referenced
  339. */
  340. void mark_page_accessed(struct page *page)
  341. {
  342. if (!PageActive(page) && !PageUnevictable(page) &&
  343. PageReferenced(page) && PageLRU(page)) {
  344. activate_page(page);
  345. ClearPageReferenced(page);
  346. } else if (!PageReferenced(page)) {
  347. SetPageReferenced(page);
  348. }
  349. }
  350. EXPORT_SYMBOL(mark_page_accessed);
  351. void __lru_cache_add(struct page *page, enum lru_list lru)
  352. {
  353. struct pagevec *pvec = &get_cpu_var(lru_add_pvecs)[lru];
  354. page_cache_get(page);
  355. if (!pagevec_add(pvec, page))
  356. __pagevec_lru_add(pvec, lru);
  357. put_cpu_var(lru_add_pvecs);
  358. }
  359. EXPORT_SYMBOL(__lru_cache_add);
  360. /**
  361. * lru_cache_add_lru - add a page to a page list
  362. * @page: the page to be added to the LRU.
  363. * @lru: the LRU list to which the page is added.
  364. */
  365. void lru_cache_add_lru(struct page *page, enum lru_list lru)
  366. {
  367. if (PageActive(page)) {
  368. VM_BUG_ON(PageUnevictable(page));
  369. ClearPageActive(page);
  370. } else if (PageUnevictable(page)) {
  371. VM_BUG_ON(PageActive(page));
  372. ClearPageUnevictable(page);
  373. }
  374. VM_BUG_ON(PageLRU(page) || PageActive(page) || PageUnevictable(page));
  375. __lru_cache_add(page, lru);
  376. }
  377. /**
  378. * add_page_to_unevictable_list - add a page to the unevictable list
  379. * @page: the page to be added to the unevictable list
  380. *
  381. * Add page directly to its zone's unevictable list. To avoid races with
  382. * tasks that might be making the page evictable, through eg. munlock,
  383. * munmap or exit, while it's not on the lru, we want to add the page
  384. * while it's locked or otherwise "invisible" to other tasks. This is
  385. * difficult to do when using the pagevec cache, so bypass that.
  386. */
  387. void add_page_to_unevictable_list(struct page *page)
  388. {
  389. struct zone *zone = page_zone(page);
  390. spin_lock_irq(&zone->lru_lock);
  391. SetPageUnevictable(page);
  392. SetPageLRU(page);
  393. add_page_to_lru_list(zone, page, LRU_UNEVICTABLE);
  394. spin_unlock_irq(&zone->lru_lock);
  395. }
  396. /*
  397. * If the page can not be invalidated, it is moved to the
  398. * inactive list to speed up its reclaim. It is moved to the
  399. * head of the list, rather than the tail, to give the flusher
  400. * threads some time to write it out, as this is much more
  401. * effective than the single-page writeout from reclaim.
  402. *
  403. * If the page isn't page_mapped and dirty/writeback, the page
  404. * could reclaim asap using PG_reclaim.
  405. *
  406. * 1. active, mapped page -> none
  407. * 2. active, dirty/writeback page -> inactive, head, PG_reclaim
  408. * 3. inactive, mapped page -> none
  409. * 4. inactive, dirty/writeback page -> inactive, head, PG_reclaim
  410. * 5. inactive, clean -> inactive, tail
  411. * 6. Others -> none
  412. *
  413. * In 4, why it moves inactive's head, the VM expects the page would
  414. * be write it out by flusher threads as this is much more effective
  415. * than the single-page writeout from reclaim.
  416. */
  417. static void lru_deactivate_fn(struct page *page, void *arg)
  418. {
  419. int lru, file;
  420. bool active;
  421. struct zone *zone = page_zone(page);
  422. if (!PageLRU(page))
  423. return;
  424. if (PageUnevictable(page))
  425. return;
  426. /* Some processes are using the page */
  427. if (page_mapped(page))
  428. return;
  429. active = PageActive(page);
  430. file = page_is_file_cache(page);
  431. lru = page_lru_base_type(page);
  432. del_page_from_lru_list(zone, page, lru + active);
  433. ClearPageActive(page);
  434. ClearPageReferenced(page);
  435. add_page_to_lru_list(zone, page, lru);
  436. if (PageWriteback(page) || PageDirty(page)) {
  437. /*
  438. * PG_reclaim could be raced with end_page_writeback
  439. * It can make readahead confusing. But race window
  440. * is _really_ small and it's non-critical problem.
  441. */
  442. SetPageReclaim(page);
  443. } else {
  444. struct lruvec *lruvec;
  445. /*
  446. * The page's writeback ends up during pagevec
  447. * We moves tha page into tail of inactive.
  448. */
  449. lruvec = mem_cgroup_lru_move_lists(zone, page, lru, lru);
  450. list_move_tail(&page->lru, &lruvec->lists[lru]);
  451. __count_vm_event(PGROTATED);
  452. }
  453. if (active)
  454. __count_vm_event(PGDEACTIVATE);
  455. update_page_reclaim_stat(zone, page, file, 0);
  456. }
  457. /*
  458. * Drain pages out of the cpu's pagevecs.
  459. * Either "cpu" is the current CPU, and preemption has already been
  460. * disabled; or "cpu" is being hot-unplugged, and is already dead.
  461. */
  462. void lru_add_drain_cpu(int cpu)
  463. {
  464. struct pagevec *pvecs = per_cpu(lru_add_pvecs, cpu);
  465. struct pagevec *pvec;
  466. int lru;
  467. for_each_lru(lru) {
  468. pvec = &pvecs[lru - LRU_BASE];
  469. if (pagevec_count(pvec))
  470. __pagevec_lru_add(pvec, lru);
  471. }
  472. pvec = &per_cpu(lru_rotate_pvecs, cpu);
  473. if (pagevec_count(pvec)) {
  474. unsigned long flags;
  475. /* No harm done if a racing interrupt already did this */
  476. local_irq_save(flags);
  477. pagevec_move_tail(pvec);
  478. local_irq_restore(flags);
  479. }
  480. pvec = &per_cpu(lru_deactivate_pvecs, cpu);
  481. if (pagevec_count(pvec))
  482. pagevec_lru_move_fn(pvec, lru_deactivate_fn, NULL);
  483. activate_page_drain(cpu);
  484. }
  485. /**
  486. * deactivate_page - forcefully deactivate a page
  487. * @page: page to deactivate
  488. *
  489. * This function hints the VM that @page is a good reclaim candidate,
  490. * for example if its invalidation fails due to the page being dirty
  491. * or under writeback.
  492. */
  493. void deactivate_page(struct page *page)
  494. {
  495. /*
  496. * In a workload with many unevictable page such as mprotect, unevictable
  497. * page deactivation for accelerating reclaim is pointless.
  498. */
  499. if (PageUnevictable(page))
  500. return;
  501. if (likely(get_page_unless_zero(page))) {
  502. struct pagevec *pvec = &get_cpu_var(lru_deactivate_pvecs);
  503. if (!pagevec_add(pvec, page))
  504. pagevec_lru_move_fn(pvec, lru_deactivate_fn, NULL);
  505. put_cpu_var(lru_deactivate_pvecs);
  506. }
  507. }
  508. void lru_add_drain(void)
  509. {
  510. lru_add_drain_cpu(get_cpu());
  511. put_cpu();
  512. }
  513. static void lru_add_drain_per_cpu(struct work_struct *dummy)
  514. {
  515. lru_add_drain();
  516. }
  517. /*
  518. * Returns 0 for success
  519. */
  520. int lru_add_drain_all(void)
  521. {
  522. return schedule_on_each_cpu(lru_add_drain_per_cpu);
  523. }
  524. /*
  525. * Batched page_cache_release(). Decrement the reference count on all the
  526. * passed pages. If it fell to zero then remove the page from the LRU and
  527. * free it.
  528. *
  529. * Avoid taking zone->lru_lock if possible, but if it is taken, retain it
  530. * for the remainder of the operation.
  531. *
  532. * The locking in this function is against shrink_inactive_list(): we recheck
  533. * the page count inside the lock to see whether shrink_inactive_list()
  534. * grabbed the page via the LRU. If it did, give up: shrink_inactive_list()
  535. * will free it.
  536. */
  537. void release_pages(struct page **pages, int nr, int cold)
  538. {
  539. int i;
  540. LIST_HEAD(pages_to_free);
  541. struct zone *zone = NULL;
  542. unsigned long uninitialized_var(flags);
  543. for (i = 0; i < nr; i++) {
  544. struct page *page = pages[i];
  545. if (unlikely(PageCompound(page))) {
  546. if (zone) {
  547. spin_unlock_irqrestore(&zone->lru_lock, flags);
  548. zone = NULL;
  549. }
  550. put_compound_page(page);
  551. continue;
  552. }
  553. if (!put_page_testzero(page))
  554. continue;
  555. if (PageLRU(page)) {
  556. struct zone *pagezone = page_zone(page);
  557. if (pagezone != zone) {
  558. if (zone)
  559. spin_unlock_irqrestore(&zone->lru_lock,
  560. flags);
  561. zone = pagezone;
  562. spin_lock_irqsave(&zone->lru_lock, flags);
  563. }
  564. VM_BUG_ON(!PageLRU(page));
  565. __ClearPageLRU(page);
  566. del_page_from_lru_list(zone, page, page_off_lru(page));
  567. }
  568. list_add(&page->lru, &pages_to_free);
  569. }
  570. if (zone)
  571. spin_unlock_irqrestore(&zone->lru_lock, flags);
  572. free_hot_cold_page_list(&pages_to_free, cold);
  573. }
  574. EXPORT_SYMBOL(release_pages);
  575. /*
  576. * The pages which we're about to release may be in the deferred lru-addition
  577. * queues. That would prevent them from really being freed right now. That's
  578. * OK from a correctness point of view but is inefficient - those pages may be
  579. * cache-warm and we want to give them back to the page allocator ASAP.
  580. *
  581. * So __pagevec_release() will drain those queues here. __pagevec_lru_add()
  582. * and __pagevec_lru_add_active() call release_pages() directly to avoid
  583. * mutual recursion.
  584. */
  585. void __pagevec_release(struct pagevec *pvec)
  586. {
  587. lru_add_drain();
  588. release_pages(pvec->pages, pagevec_count(pvec), pvec->cold);
  589. pagevec_reinit(pvec);
  590. }
  591. EXPORT_SYMBOL(__pagevec_release);
  592. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  593. /* used by __split_huge_page_refcount() */
  594. void lru_add_page_tail(struct zone* zone,
  595. struct page *page, struct page *page_tail)
  596. {
  597. int uninitialized_var(active);
  598. enum lru_list lru;
  599. const int file = 0;
  600. VM_BUG_ON(!PageHead(page));
  601. VM_BUG_ON(PageCompound(page_tail));
  602. VM_BUG_ON(PageLRU(page_tail));
  603. VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&zone->lru_lock));
  604. SetPageLRU(page_tail);
  605. if (page_evictable(page_tail, NULL)) {
  606. if (PageActive(page)) {
  607. SetPageActive(page_tail);
  608. active = 1;
  609. lru = LRU_ACTIVE_ANON;
  610. } else {
  611. active = 0;
  612. lru = LRU_INACTIVE_ANON;
  613. }
  614. } else {
  615. SetPageUnevictable(page_tail);
  616. lru = LRU_UNEVICTABLE;
  617. }
  618. if (likely(PageLRU(page)))
  619. list_add_tail(&page_tail->lru, &page->lru);
  620. else {
  621. struct list_head *list_head;
  622. /*
  623. * Head page has not yet been counted, as an hpage,
  624. * so we must account for each subpage individually.
  625. *
  626. * Use the standard add function to put page_tail on the list,
  627. * but then correct its position so they all end up in order.
  628. */
  629. add_page_to_lru_list(zone, page_tail, lru);
  630. list_head = page_tail->lru.prev;
  631. list_move_tail(&page_tail->lru, list_head);
  632. }
  633. if (!PageUnevictable(page))
  634. update_page_reclaim_stat(zone, page_tail, file, active);
  635. }
  636. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  637. static void __pagevec_lru_add_fn(struct page *page, void *arg)
  638. {
  639. enum lru_list lru = (enum lru_list)arg;
  640. struct zone *zone = page_zone(page);
  641. int file = is_file_lru(lru);
  642. int active = is_active_lru(lru);
  643. VM_BUG_ON(PageActive(page));
  644. VM_BUG_ON(PageUnevictable(page));
  645. VM_BUG_ON(PageLRU(page));
  646. SetPageLRU(page);
  647. if (active)
  648. SetPageActive(page);
  649. add_page_to_lru_list(zone, page, lru);
  650. update_page_reclaim_stat(zone, page, file, active);
  651. }
  652. /*
  653. * Add the passed pages to the LRU, then drop the caller's refcount
  654. * on them. Reinitialises the caller's pagevec.
  655. */
  656. void __pagevec_lru_add(struct pagevec *pvec, enum lru_list lru)
  657. {
  658. VM_BUG_ON(is_unevictable_lru(lru));
  659. pagevec_lru_move_fn(pvec, __pagevec_lru_add_fn, (void *)lru);
  660. }
  661. EXPORT_SYMBOL(__pagevec_lru_add);
  662. /**
  663. * pagevec_lookup - gang pagecache lookup
  664. * @pvec: Where the resulting pages are placed
  665. * @mapping: The address_space to search
  666. * @start: The starting page index
  667. * @nr_pages: The maximum number of pages
  668. *
  669. * pagevec_lookup() will search for and return a group of up to @nr_pages pages
  670. * in the mapping. The pages are placed in @pvec. pagevec_lookup() takes a
  671. * reference against the pages in @pvec.
  672. *
  673. * The search returns a group of mapping-contiguous pages with ascending
  674. * indexes. There may be holes in the indices due to not-present pages.
  675. *
  676. * pagevec_lookup() returns the number of pages which were found.
  677. */
  678. unsigned pagevec_lookup(struct pagevec *pvec, struct address_space *mapping,
  679. pgoff_t start, unsigned nr_pages)
  680. {
  681. pvec->nr = find_get_pages(mapping, start, nr_pages, pvec->pages);
  682. return pagevec_count(pvec);
  683. }
  684. EXPORT_SYMBOL(pagevec_lookup);
  685. unsigned pagevec_lookup_tag(struct pagevec *pvec, struct address_space *mapping,
  686. pgoff_t *index, int tag, unsigned nr_pages)
  687. {
  688. pvec->nr = find_get_pages_tag(mapping, index, tag,
  689. nr_pages, pvec->pages);
  690. return pagevec_count(pvec);
  691. }
  692. EXPORT_SYMBOL(pagevec_lookup_tag);
  693. /*
  694. * Perform any setup for the swap system
  695. */
  696. void __init swap_setup(void)
  697. {
  698. unsigned long megs = totalram_pages >> (20 - PAGE_SHIFT);
  699. #ifdef CONFIG_SWAP
  700. bdi_init(swapper_space.backing_dev_info);
  701. #endif
  702. /* Use a smaller cluster for small-memory machines */
  703. if (megs < 16)
  704. page_cluster = 2;
  705. else
  706. page_cluster = 3;
  707. /*
  708. * Right now other parts of the system means that we
  709. * _really_ don't want to cluster much more
  710. */
  711. }