swap.c 23 KB

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