swap.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  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;
  329. reclaim_stat = mem_cgroup_get_reclaim_stat_from_page(page);
  330. if (!reclaim_stat)
  331. reclaim_stat = &zone->lruvec.reclaim_stat;
  332. reclaim_stat->recent_scanned[file]++;
  333. if (rotated)
  334. reclaim_stat->recent_rotated[file]++;
  335. }
  336. static void __activate_page(struct page *page, void *arg)
  337. {
  338. struct zone *zone = page_zone(page);
  339. if (PageLRU(page) && !PageActive(page) && !PageUnevictable(page)) {
  340. int file = page_is_file_cache(page);
  341. int lru = page_lru_base_type(page);
  342. del_page_from_lru_list(zone, page, lru);
  343. SetPageActive(page);
  344. lru += LRU_ACTIVE;
  345. add_page_to_lru_list(zone, page, lru);
  346. __count_vm_event(PGACTIVATE);
  347. update_page_reclaim_stat(zone, page, file, 1);
  348. }
  349. }
  350. #ifdef CONFIG_SMP
  351. static DEFINE_PER_CPU(struct pagevec, activate_page_pvecs);
  352. static void activate_page_drain(int cpu)
  353. {
  354. struct pagevec *pvec = &per_cpu(activate_page_pvecs, cpu);
  355. if (pagevec_count(pvec))
  356. pagevec_lru_move_fn(pvec, __activate_page, NULL);
  357. }
  358. void activate_page(struct page *page)
  359. {
  360. if (PageLRU(page) && !PageActive(page) && !PageUnevictable(page)) {
  361. struct pagevec *pvec = &get_cpu_var(activate_page_pvecs);
  362. page_cache_get(page);
  363. if (!pagevec_add(pvec, page))
  364. pagevec_lru_move_fn(pvec, __activate_page, NULL);
  365. put_cpu_var(activate_page_pvecs);
  366. }
  367. }
  368. #else
  369. static inline void activate_page_drain(int cpu)
  370. {
  371. }
  372. void activate_page(struct page *page)
  373. {
  374. struct zone *zone = page_zone(page);
  375. spin_lock_irq(&zone->lru_lock);
  376. __activate_page(page, NULL);
  377. spin_unlock_irq(&zone->lru_lock);
  378. }
  379. #endif
  380. /*
  381. * Mark a page as having seen activity.
  382. *
  383. * inactive,unreferenced -> inactive,referenced
  384. * inactive,referenced -> active,unreferenced
  385. * active,unreferenced -> active,referenced
  386. */
  387. void mark_page_accessed(struct page *page)
  388. {
  389. if (!PageActive(page) && !PageUnevictable(page) &&
  390. PageReferenced(page) && PageLRU(page)) {
  391. activate_page(page);
  392. ClearPageReferenced(page);
  393. } else if (!PageReferenced(page)) {
  394. SetPageReferenced(page);
  395. }
  396. }
  397. EXPORT_SYMBOL(mark_page_accessed);
  398. void __lru_cache_add(struct page *page, enum lru_list lru)
  399. {
  400. struct pagevec *pvec = &get_cpu_var(lru_add_pvecs)[lru];
  401. page_cache_get(page);
  402. if (!pagevec_add(pvec, page))
  403. __pagevec_lru_add(pvec, lru);
  404. put_cpu_var(lru_add_pvecs);
  405. }
  406. EXPORT_SYMBOL(__lru_cache_add);
  407. /**
  408. * lru_cache_add_lru - add a page to a page list
  409. * @page: the page to be added to the LRU.
  410. * @lru: the LRU list to which the page is added.
  411. */
  412. void lru_cache_add_lru(struct page *page, enum lru_list lru)
  413. {
  414. if (PageActive(page)) {
  415. VM_BUG_ON(PageUnevictable(page));
  416. ClearPageActive(page);
  417. } else if (PageUnevictable(page)) {
  418. VM_BUG_ON(PageActive(page));
  419. ClearPageUnevictable(page);
  420. }
  421. VM_BUG_ON(PageLRU(page) || PageActive(page) || PageUnevictable(page));
  422. __lru_cache_add(page, lru);
  423. }
  424. /**
  425. * add_page_to_unevictable_list - add a page to the unevictable list
  426. * @page: the page to be added to the unevictable list
  427. *
  428. * Add page directly to its zone's unevictable list. To avoid races with
  429. * tasks that might be making the page evictable, through eg. munlock,
  430. * munmap or exit, while it's not on the lru, we want to add the page
  431. * while it's locked or otherwise "invisible" to other tasks. This is
  432. * difficult to do when using the pagevec cache, so bypass that.
  433. */
  434. void add_page_to_unevictable_list(struct page *page)
  435. {
  436. struct zone *zone = page_zone(page);
  437. spin_lock_irq(&zone->lru_lock);
  438. SetPageUnevictable(page);
  439. SetPageLRU(page);
  440. add_page_to_lru_list(zone, page, LRU_UNEVICTABLE);
  441. spin_unlock_irq(&zone->lru_lock);
  442. }
  443. /*
  444. * If the page can not be invalidated, it is moved to the
  445. * inactive list to speed up its reclaim. It is moved to the
  446. * head of the list, rather than the tail, to give the flusher
  447. * threads some time to write it out, as this is much more
  448. * effective than the single-page writeout from reclaim.
  449. *
  450. * If the page isn't page_mapped and dirty/writeback, the page
  451. * could reclaim asap using PG_reclaim.
  452. *
  453. * 1. active, mapped page -> none
  454. * 2. active, dirty/writeback page -> inactive, head, PG_reclaim
  455. * 3. inactive, mapped page -> none
  456. * 4. inactive, dirty/writeback page -> inactive, head, PG_reclaim
  457. * 5. inactive, clean -> inactive, tail
  458. * 6. Others -> none
  459. *
  460. * In 4, why it moves inactive's head, the VM expects the page would
  461. * be write it out by flusher threads as this is much more effective
  462. * than the single-page writeout from reclaim.
  463. */
  464. static void lru_deactivate_fn(struct page *page, void *arg)
  465. {
  466. int lru, file;
  467. bool active;
  468. struct zone *zone = page_zone(page);
  469. if (!PageLRU(page))
  470. return;
  471. if (PageUnevictable(page))
  472. return;
  473. /* Some processes are using the page */
  474. if (page_mapped(page))
  475. return;
  476. active = PageActive(page);
  477. file = page_is_file_cache(page);
  478. lru = page_lru_base_type(page);
  479. del_page_from_lru_list(zone, page, lru + active);
  480. ClearPageActive(page);
  481. ClearPageReferenced(page);
  482. add_page_to_lru_list(zone, page, lru);
  483. if (PageWriteback(page) || PageDirty(page)) {
  484. /*
  485. * PG_reclaim could be raced with end_page_writeback
  486. * It can make readahead confusing. But race window
  487. * is _really_ small and it's non-critical problem.
  488. */
  489. SetPageReclaim(page);
  490. } else {
  491. struct lruvec *lruvec;
  492. /*
  493. * The page's writeback ends up during pagevec
  494. * We moves tha page into tail of inactive.
  495. */
  496. lruvec = mem_cgroup_lru_move_lists(zone, page, lru, lru);
  497. list_move_tail(&page->lru, &lruvec->lists[lru]);
  498. __count_vm_event(PGROTATED);
  499. }
  500. if (active)
  501. __count_vm_event(PGDEACTIVATE);
  502. update_page_reclaim_stat(zone, page, file, 0);
  503. }
  504. /*
  505. * Drain pages out of the cpu's pagevecs.
  506. * Either "cpu" is the current CPU, and preemption has already been
  507. * disabled; or "cpu" is being hot-unplugged, and is already dead.
  508. */
  509. void lru_add_drain_cpu(int cpu)
  510. {
  511. struct pagevec *pvecs = per_cpu(lru_add_pvecs, cpu);
  512. struct pagevec *pvec;
  513. int lru;
  514. for_each_lru(lru) {
  515. pvec = &pvecs[lru - LRU_BASE];
  516. if (pagevec_count(pvec))
  517. __pagevec_lru_add(pvec, lru);
  518. }
  519. pvec = &per_cpu(lru_rotate_pvecs, cpu);
  520. if (pagevec_count(pvec)) {
  521. unsigned long flags;
  522. /* No harm done if a racing interrupt already did this */
  523. local_irq_save(flags);
  524. pagevec_move_tail(pvec);
  525. local_irq_restore(flags);
  526. }
  527. pvec = &per_cpu(lru_deactivate_pvecs, cpu);
  528. if (pagevec_count(pvec))
  529. pagevec_lru_move_fn(pvec, lru_deactivate_fn, NULL);
  530. activate_page_drain(cpu);
  531. }
  532. /**
  533. * deactivate_page - forcefully deactivate a page
  534. * @page: page to deactivate
  535. *
  536. * This function hints the VM that @page is a good reclaim candidate,
  537. * for example if its invalidation fails due to the page being dirty
  538. * or under writeback.
  539. */
  540. void deactivate_page(struct page *page)
  541. {
  542. /*
  543. * In a workload with many unevictable page such as mprotect, unevictable
  544. * page deactivation for accelerating reclaim is pointless.
  545. */
  546. if (PageUnevictable(page))
  547. return;
  548. if (likely(get_page_unless_zero(page))) {
  549. struct pagevec *pvec = &get_cpu_var(lru_deactivate_pvecs);
  550. if (!pagevec_add(pvec, page))
  551. pagevec_lru_move_fn(pvec, lru_deactivate_fn, NULL);
  552. put_cpu_var(lru_deactivate_pvecs);
  553. }
  554. }
  555. void lru_add_drain(void)
  556. {
  557. lru_add_drain_cpu(get_cpu());
  558. put_cpu();
  559. }
  560. static void lru_add_drain_per_cpu(struct work_struct *dummy)
  561. {
  562. lru_add_drain();
  563. }
  564. /*
  565. * Returns 0 for success
  566. */
  567. int lru_add_drain_all(void)
  568. {
  569. return schedule_on_each_cpu(lru_add_drain_per_cpu);
  570. }
  571. /*
  572. * Batched page_cache_release(). Decrement the reference count on all the
  573. * passed pages. If it fell to zero then remove the page from the LRU and
  574. * free it.
  575. *
  576. * Avoid taking zone->lru_lock if possible, but if it is taken, retain it
  577. * for the remainder of the operation.
  578. *
  579. * The locking in this function is against shrink_inactive_list(): we recheck
  580. * the page count inside the lock to see whether shrink_inactive_list()
  581. * grabbed the page via the LRU. If it did, give up: shrink_inactive_list()
  582. * will free it.
  583. */
  584. void release_pages(struct page **pages, int nr, int cold)
  585. {
  586. int i;
  587. LIST_HEAD(pages_to_free);
  588. struct zone *zone = NULL;
  589. unsigned long uninitialized_var(flags);
  590. for (i = 0; i < nr; i++) {
  591. struct page *page = pages[i];
  592. if (unlikely(PageCompound(page))) {
  593. if (zone) {
  594. spin_unlock_irqrestore(&zone->lru_lock, flags);
  595. zone = NULL;
  596. }
  597. put_compound_page(page);
  598. continue;
  599. }
  600. if (!put_page_testzero(page))
  601. continue;
  602. if (PageLRU(page)) {
  603. struct zone *pagezone = page_zone(page);
  604. if (pagezone != zone) {
  605. if (zone)
  606. spin_unlock_irqrestore(&zone->lru_lock,
  607. flags);
  608. zone = pagezone;
  609. spin_lock_irqsave(&zone->lru_lock, flags);
  610. }
  611. VM_BUG_ON(!PageLRU(page));
  612. __ClearPageLRU(page);
  613. del_page_from_lru_list(zone, page, page_off_lru(page));
  614. }
  615. list_add(&page->lru, &pages_to_free);
  616. }
  617. if (zone)
  618. spin_unlock_irqrestore(&zone->lru_lock, flags);
  619. free_hot_cold_page_list(&pages_to_free, cold);
  620. }
  621. EXPORT_SYMBOL(release_pages);
  622. /*
  623. * The pages which we're about to release may be in the deferred lru-addition
  624. * queues. That would prevent them from really being freed right now. That's
  625. * OK from a correctness point of view but is inefficient - those pages may be
  626. * cache-warm and we want to give them back to the page allocator ASAP.
  627. *
  628. * So __pagevec_release() will drain those queues here. __pagevec_lru_add()
  629. * and __pagevec_lru_add_active() call release_pages() directly to avoid
  630. * mutual recursion.
  631. */
  632. void __pagevec_release(struct pagevec *pvec)
  633. {
  634. lru_add_drain();
  635. release_pages(pvec->pages, pagevec_count(pvec), pvec->cold);
  636. pagevec_reinit(pvec);
  637. }
  638. EXPORT_SYMBOL(__pagevec_release);
  639. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  640. /* used by __split_huge_page_refcount() */
  641. void lru_add_page_tail(struct zone* zone,
  642. struct page *page, struct page *page_tail)
  643. {
  644. int uninitialized_var(active);
  645. enum lru_list lru;
  646. const int file = 0;
  647. VM_BUG_ON(!PageHead(page));
  648. VM_BUG_ON(PageCompound(page_tail));
  649. VM_BUG_ON(PageLRU(page_tail));
  650. VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&zone->lru_lock));
  651. SetPageLRU(page_tail);
  652. if (page_evictable(page_tail)) {
  653. if (PageActive(page)) {
  654. SetPageActive(page_tail);
  655. active = 1;
  656. lru = LRU_ACTIVE_ANON;
  657. } else {
  658. active = 0;
  659. lru = LRU_INACTIVE_ANON;
  660. }
  661. } else {
  662. SetPageUnevictable(page_tail);
  663. lru = LRU_UNEVICTABLE;
  664. }
  665. if (likely(PageLRU(page)))
  666. list_add_tail(&page_tail->lru, &page->lru);
  667. else {
  668. struct list_head *list_head;
  669. /*
  670. * Head page has not yet been counted, as an hpage,
  671. * so we must account for each subpage individually.
  672. *
  673. * Use the standard add function to put page_tail on the list,
  674. * but then correct its position so they all end up in order.
  675. */
  676. add_page_to_lru_list(zone, page_tail, lru);
  677. list_head = page_tail->lru.prev;
  678. list_move_tail(&page_tail->lru, list_head);
  679. }
  680. if (!PageUnevictable(page))
  681. update_page_reclaim_stat(zone, page_tail, file, active);
  682. }
  683. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  684. static void __pagevec_lru_add_fn(struct page *page, void *arg)
  685. {
  686. enum lru_list lru = (enum lru_list)arg;
  687. struct zone *zone = page_zone(page);
  688. int file = is_file_lru(lru);
  689. int active = is_active_lru(lru);
  690. VM_BUG_ON(PageActive(page));
  691. VM_BUG_ON(PageUnevictable(page));
  692. VM_BUG_ON(PageLRU(page));
  693. SetPageLRU(page);
  694. if (active)
  695. SetPageActive(page);
  696. add_page_to_lru_list(zone, page, lru);
  697. update_page_reclaim_stat(zone, page, file, active);
  698. }
  699. /*
  700. * Add the passed pages to the LRU, then drop the caller's refcount
  701. * on them. Reinitialises the caller's pagevec.
  702. */
  703. void __pagevec_lru_add(struct pagevec *pvec, enum lru_list lru)
  704. {
  705. VM_BUG_ON(is_unevictable_lru(lru));
  706. pagevec_lru_move_fn(pvec, __pagevec_lru_add_fn, (void *)lru);
  707. }
  708. EXPORT_SYMBOL(__pagevec_lru_add);
  709. /**
  710. * pagevec_lookup - gang pagecache lookup
  711. * @pvec: Where the resulting pages are placed
  712. * @mapping: The address_space to search
  713. * @start: The starting page index
  714. * @nr_pages: The maximum number of pages
  715. *
  716. * pagevec_lookup() will search for and return a group of up to @nr_pages pages
  717. * in the mapping. The pages are placed in @pvec. pagevec_lookup() takes a
  718. * reference against the pages in @pvec.
  719. *
  720. * The search returns a group of mapping-contiguous pages with ascending
  721. * indexes. There may be holes in the indices due to not-present pages.
  722. *
  723. * pagevec_lookup() returns the number of pages which were found.
  724. */
  725. unsigned pagevec_lookup(struct pagevec *pvec, struct address_space *mapping,
  726. pgoff_t start, unsigned nr_pages)
  727. {
  728. pvec->nr = find_get_pages(mapping, start, nr_pages, pvec->pages);
  729. return pagevec_count(pvec);
  730. }
  731. EXPORT_SYMBOL(pagevec_lookup);
  732. unsigned pagevec_lookup_tag(struct pagevec *pvec, struct address_space *mapping,
  733. pgoff_t *index, int tag, unsigned nr_pages)
  734. {
  735. pvec->nr = find_get_pages_tag(mapping, index, tag,
  736. nr_pages, pvec->pages);
  737. return pagevec_count(pvec);
  738. }
  739. EXPORT_SYMBOL(pagevec_lookup_tag);
  740. /*
  741. * Perform any setup for the swap system
  742. */
  743. void __init swap_setup(void)
  744. {
  745. unsigned long megs = totalram_pages >> (20 - PAGE_SHIFT);
  746. #ifdef CONFIG_SWAP
  747. int i;
  748. bdi_init(swapper_spaces[0].backing_dev_info);
  749. for (i = 0; i < MAX_SWAPFILES; i++) {
  750. spin_lock_init(&swapper_spaces[i].tree_lock);
  751. INIT_LIST_HEAD(&swapper_spaces[i].i_mmap_nonlinear);
  752. }
  753. #endif
  754. /* Use a smaller cluster for small-memory machines */
  755. if (megs < 16)
  756. page_cluster = 2;
  757. else
  758. page_cluster = 3;
  759. /*
  760. * Right now other parts of the system means that we
  761. * _really_ don't want to cluster much more
  762. */
  763. }