swap.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  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/memremap.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 <linux/uio.h>
  33. #include <linux/hugetlb.h>
  34. #include <linux/page_idle.h>
  35. #include "internal.h"
  36. #define CREATE_TRACE_POINTS
  37. #include <trace/events/pagemap.h>
  38. /* How many pages do we try to swap or page in/out together? */
  39. int page_cluster;
  40. static DEFINE_PER_CPU(struct pagevec, lru_add_pvec);
  41. static DEFINE_PER_CPU(struct pagevec, lru_rotate_pvecs);
  42. static DEFINE_PER_CPU(struct pagevec, lru_deactivate_file_pvecs);
  43. static DEFINE_PER_CPU(struct pagevec, lru_deactivate_pvecs);
  44. #ifdef CONFIG_SMP
  45. static DEFINE_PER_CPU(struct pagevec, activate_page_pvecs);
  46. #endif
  47. /*
  48. * This path almost never happens for VM activity - pages are normally
  49. * freed via pagevecs. But it gets used by networking.
  50. */
  51. static void __page_cache_release(struct page *page)
  52. {
  53. if (PageLRU(page)) {
  54. struct zone *zone = page_zone(page);
  55. struct lruvec *lruvec;
  56. unsigned long flags;
  57. spin_lock_irqsave(&zone->lru_lock, flags);
  58. lruvec = mem_cgroup_page_lruvec(page, zone);
  59. VM_BUG_ON_PAGE(!PageLRU(page), page);
  60. __ClearPageLRU(page);
  61. del_page_from_lru_list(page, lruvec, page_off_lru(page));
  62. spin_unlock_irqrestore(&zone->lru_lock, flags);
  63. }
  64. mem_cgroup_uncharge(page);
  65. }
  66. static void __put_single_page(struct page *page)
  67. {
  68. __page_cache_release(page);
  69. free_hot_cold_page(page, false);
  70. }
  71. static void __put_compound_page(struct page *page)
  72. {
  73. compound_page_dtor *dtor;
  74. /*
  75. * __page_cache_release() is supposed to be called for thp, not for
  76. * hugetlb. This is because hugetlb page does never have PageLRU set
  77. * (it's never listed to any LRU lists) and no memcg routines should
  78. * be called for hugetlb (it has a separate hugetlb_cgroup.)
  79. */
  80. if (!PageHuge(page))
  81. __page_cache_release(page);
  82. dtor = get_compound_page_dtor(page);
  83. (*dtor)(page);
  84. }
  85. void __put_page(struct page *page)
  86. {
  87. if (unlikely(PageCompound(page)))
  88. __put_compound_page(page);
  89. else
  90. __put_single_page(page);
  91. }
  92. EXPORT_SYMBOL(__put_page);
  93. /**
  94. * put_pages_list() - release a list of pages
  95. * @pages: list of pages threaded on page->lru
  96. *
  97. * Release a list of pages which are strung together on page.lru. Currently
  98. * used by read_cache_pages() and related error recovery code.
  99. */
  100. void put_pages_list(struct list_head *pages)
  101. {
  102. while (!list_empty(pages)) {
  103. struct page *victim;
  104. victim = list_entry(pages->prev, struct page, lru);
  105. list_del(&victim->lru);
  106. put_page(victim);
  107. }
  108. }
  109. EXPORT_SYMBOL(put_pages_list);
  110. /*
  111. * get_kernel_pages() - pin kernel pages in memory
  112. * @kiov: An array of struct kvec structures
  113. * @nr_segs: number of segments to pin
  114. * @write: pinning for read/write, currently ignored
  115. * @pages: array that receives pointers to the pages pinned.
  116. * Should be at least nr_segs long.
  117. *
  118. * Returns number of pages pinned. This may be fewer than the number
  119. * requested. If nr_pages is 0 or negative, returns 0. If no pages
  120. * were pinned, returns -errno. Each page returned must be released
  121. * with a put_page() call when it is finished with.
  122. */
  123. int get_kernel_pages(const struct kvec *kiov, int nr_segs, int write,
  124. struct page **pages)
  125. {
  126. int seg;
  127. for (seg = 0; seg < nr_segs; seg++) {
  128. if (WARN_ON(kiov[seg].iov_len != PAGE_SIZE))
  129. return seg;
  130. pages[seg] = kmap_to_page(kiov[seg].iov_base);
  131. get_page(pages[seg]);
  132. }
  133. return seg;
  134. }
  135. EXPORT_SYMBOL_GPL(get_kernel_pages);
  136. /*
  137. * get_kernel_page() - pin a kernel page in memory
  138. * @start: starting kernel address
  139. * @write: pinning for read/write, currently ignored
  140. * @pages: array that receives pointer to the page pinned.
  141. * Must be at least nr_segs long.
  142. *
  143. * Returns 1 if page is pinned. If the page was not pinned, returns
  144. * -errno. The page returned must be released with a put_page() call
  145. * when it is finished with.
  146. */
  147. int get_kernel_page(unsigned long start, int write, struct page **pages)
  148. {
  149. const struct kvec kiov = {
  150. .iov_base = (void *)start,
  151. .iov_len = PAGE_SIZE
  152. };
  153. return get_kernel_pages(&kiov, 1, write, pages);
  154. }
  155. EXPORT_SYMBOL_GPL(get_kernel_page);
  156. static void pagevec_lru_move_fn(struct pagevec *pvec,
  157. void (*move_fn)(struct page *page, struct lruvec *lruvec, void *arg),
  158. void *arg)
  159. {
  160. int i;
  161. struct zone *zone = NULL;
  162. struct lruvec *lruvec;
  163. unsigned long flags = 0;
  164. for (i = 0; i < pagevec_count(pvec); i++) {
  165. struct page *page = pvec->pages[i];
  166. struct zone *pagezone = page_zone(page);
  167. if (pagezone != zone) {
  168. if (zone)
  169. spin_unlock_irqrestore(&zone->lru_lock, flags);
  170. zone = pagezone;
  171. spin_lock_irqsave(&zone->lru_lock, flags);
  172. }
  173. lruvec = mem_cgroup_page_lruvec(page, zone);
  174. (*move_fn)(page, lruvec, arg);
  175. }
  176. if (zone)
  177. spin_unlock_irqrestore(&zone->lru_lock, flags);
  178. release_pages(pvec->pages, pvec->nr, pvec->cold);
  179. pagevec_reinit(pvec);
  180. }
  181. static void pagevec_move_tail_fn(struct page *page, struct lruvec *lruvec,
  182. void *arg)
  183. {
  184. int *pgmoved = arg;
  185. if (PageLRU(page) && !PageActive(page) && !PageUnevictable(page)) {
  186. enum lru_list lru = page_lru_base_type(page);
  187. list_move_tail(&page->lru, &lruvec->lists[lru]);
  188. (*pgmoved)++;
  189. }
  190. }
  191. /*
  192. * pagevec_move_tail() must be called with IRQ disabled.
  193. * Otherwise this may cause nasty races.
  194. */
  195. static void pagevec_move_tail(struct pagevec *pvec)
  196. {
  197. int pgmoved = 0;
  198. pagevec_lru_move_fn(pvec, pagevec_move_tail_fn, &pgmoved);
  199. __count_vm_events(PGROTATED, pgmoved);
  200. }
  201. /*
  202. * Writeback is about to end against a page which has been marked for immediate
  203. * reclaim. If it still appears to be reclaimable, move it to the tail of the
  204. * inactive list.
  205. */
  206. void rotate_reclaimable_page(struct page *page)
  207. {
  208. if (!PageLocked(page) && !PageDirty(page) && !PageActive(page) &&
  209. !PageUnevictable(page) && PageLRU(page)) {
  210. struct pagevec *pvec;
  211. unsigned long flags;
  212. get_page(page);
  213. local_irq_save(flags);
  214. pvec = this_cpu_ptr(&lru_rotate_pvecs);
  215. if (!pagevec_add(pvec, page))
  216. pagevec_move_tail(pvec);
  217. local_irq_restore(flags);
  218. }
  219. }
  220. static void update_page_reclaim_stat(struct lruvec *lruvec,
  221. int file, int rotated)
  222. {
  223. struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat;
  224. reclaim_stat->recent_scanned[file]++;
  225. if (rotated)
  226. reclaim_stat->recent_rotated[file]++;
  227. }
  228. static void __activate_page(struct page *page, struct lruvec *lruvec,
  229. void *arg)
  230. {
  231. if (PageLRU(page) && !PageActive(page) && !PageUnevictable(page)) {
  232. int file = page_is_file_cache(page);
  233. int lru = page_lru_base_type(page);
  234. del_page_from_lru_list(page, lruvec, lru);
  235. SetPageActive(page);
  236. lru += LRU_ACTIVE;
  237. add_page_to_lru_list(page, lruvec, lru);
  238. trace_mm_lru_activate(page);
  239. __count_vm_event(PGACTIVATE);
  240. update_page_reclaim_stat(lruvec, file, 1);
  241. }
  242. }
  243. #ifdef CONFIG_SMP
  244. static void activate_page_drain(int cpu)
  245. {
  246. struct pagevec *pvec = &per_cpu(activate_page_pvecs, cpu);
  247. if (pagevec_count(pvec))
  248. pagevec_lru_move_fn(pvec, __activate_page, NULL);
  249. }
  250. static bool need_activate_page_drain(int cpu)
  251. {
  252. return pagevec_count(&per_cpu(activate_page_pvecs, cpu)) != 0;
  253. }
  254. void activate_page(struct page *page)
  255. {
  256. if (PageLRU(page) && !PageActive(page) && !PageUnevictable(page)) {
  257. struct pagevec *pvec = &get_cpu_var(activate_page_pvecs);
  258. get_page(page);
  259. if (!pagevec_add(pvec, page))
  260. pagevec_lru_move_fn(pvec, __activate_page, NULL);
  261. put_cpu_var(activate_page_pvecs);
  262. }
  263. }
  264. #else
  265. static inline void activate_page_drain(int cpu)
  266. {
  267. }
  268. static bool need_activate_page_drain(int cpu)
  269. {
  270. return false;
  271. }
  272. void activate_page(struct page *page)
  273. {
  274. struct zone *zone = page_zone(page);
  275. spin_lock_irq(&zone->lru_lock);
  276. __activate_page(page, mem_cgroup_page_lruvec(page, zone), NULL);
  277. spin_unlock_irq(&zone->lru_lock);
  278. }
  279. #endif
  280. static void __lru_cache_activate_page(struct page *page)
  281. {
  282. struct pagevec *pvec = &get_cpu_var(lru_add_pvec);
  283. int i;
  284. /*
  285. * Search backwards on the optimistic assumption that the page being
  286. * activated has just been added to this pagevec. Note that only
  287. * the local pagevec is examined as a !PageLRU page could be in the
  288. * process of being released, reclaimed, migrated or on a remote
  289. * pagevec that is currently being drained. Furthermore, marking
  290. * a remote pagevec's page PageActive potentially hits a race where
  291. * a page is marked PageActive just after it is added to the inactive
  292. * list causing accounting errors and BUG_ON checks to trigger.
  293. */
  294. for (i = pagevec_count(pvec) - 1; i >= 0; i--) {
  295. struct page *pagevec_page = pvec->pages[i];
  296. if (pagevec_page == page) {
  297. SetPageActive(page);
  298. break;
  299. }
  300. }
  301. put_cpu_var(lru_add_pvec);
  302. }
  303. /*
  304. * Mark a page as having seen activity.
  305. *
  306. * inactive,unreferenced -> inactive,referenced
  307. * inactive,referenced -> active,unreferenced
  308. * active,unreferenced -> active,referenced
  309. *
  310. * When a newly allocated page is not yet visible, so safe for non-atomic ops,
  311. * __SetPageReferenced(page) may be substituted for mark_page_accessed(page).
  312. */
  313. void mark_page_accessed(struct page *page)
  314. {
  315. page = compound_head(page);
  316. if (!PageActive(page) && !PageUnevictable(page) &&
  317. PageReferenced(page)) {
  318. /*
  319. * If the page is on the LRU, queue it for activation via
  320. * activate_page_pvecs. Otherwise, assume the page is on a
  321. * pagevec, mark it active and it'll be moved to the active
  322. * LRU on the next drain.
  323. */
  324. if (PageLRU(page))
  325. activate_page(page);
  326. else
  327. __lru_cache_activate_page(page);
  328. ClearPageReferenced(page);
  329. if (page_is_file_cache(page))
  330. workingset_activation(page);
  331. } else if (!PageReferenced(page)) {
  332. SetPageReferenced(page);
  333. }
  334. if (page_is_idle(page))
  335. clear_page_idle(page);
  336. }
  337. EXPORT_SYMBOL(mark_page_accessed);
  338. static void __lru_cache_add(struct page *page)
  339. {
  340. struct pagevec *pvec = &get_cpu_var(lru_add_pvec);
  341. get_page(page);
  342. if (!pagevec_space(pvec))
  343. __pagevec_lru_add(pvec);
  344. pagevec_add(pvec, page);
  345. put_cpu_var(lru_add_pvec);
  346. }
  347. /**
  348. * lru_cache_add: add a page to the page lists
  349. * @page: the page to add
  350. */
  351. void lru_cache_add_anon(struct page *page)
  352. {
  353. if (PageActive(page))
  354. ClearPageActive(page);
  355. __lru_cache_add(page);
  356. }
  357. void lru_cache_add_file(struct page *page)
  358. {
  359. if (PageActive(page))
  360. ClearPageActive(page);
  361. __lru_cache_add(page);
  362. }
  363. EXPORT_SYMBOL(lru_cache_add_file);
  364. /**
  365. * lru_cache_add - add a page to a page list
  366. * @page: the page to be added to the LRU.
  367. *
  368. * Queue the page for addition to the LRU via pagevec. The decision on whether
  369. * to add the page to the [in]active [file|anon] list is deferred until the
  370. * pagevec is drained. This gives a chance for the caller of lru_cache_add()
  371. * have the page added to the active list using mark_page_accessed().
  372. */
  373. void lru_cache_add(struct page *page)
  374. {
  375. VM_BUG_ON_PAGE(PageActive(page) && PageUnevictable(page), page);
  376. VM_BUG_ON_PAGE(PageLRU(page), page);
  377. __lru_cache_add(page);
  378. }
  379. /**
  380. * add_page_to_unevictable_list - add a page to the unevictable list
  381. * @page: the page to be added to the unevictable list
  382. *
  383. * Add page directly to its zone's unevictable list. To avoid races with
  384. * tasks that might be making the page evictable, through eg. munlock,
  385. * munmap or exit, while it's not on the lru, we want to add the page
  386. * while it's locked or otherwise "invisible" to other tasks. This is
  387. * difficult to do when using the pagevec cache, so bypass that.
  388. */
  389. void add_page_to_unevictable_list(struct page *page)
  390. {
  391. struct zone *zone = page_zone(page);
  392. struct lruvec *lruvec;
  393. spin_lock_irq(&zone->lru_lock);
  394. lruvec = mem_cgroup_page_lruvec(page, zone);
  395. ClearPageActive(page);
  396. SetPageUnevictable(page);
  397. SetPageLRU(page);
  398. add_page_to_lru_list(page, lruvec, LRU_UNEVICTABLE);
  399. spin_unlock_irq(&zone->lru_lock);
  400. }
  401. /**
  402. * lru_cache_add_active_or_unevictable
  403. * @page: the page to be added to LRU
  404. * @vma: vma in which page is mapped for determining reclaimability
  405. *
  406. * Place @page on the active or unevictable LRU list, depending on its
  407. * evictability. Note that if the page is not evictable, it goes
  408. * directly back onto it's zone's unevictable list, it does NOT use a
  409. * per cpu pagevec.
  410. */
  411. void lru_cache_add_active_or_unevictable(struct page *page,
  412. struct vm_area_struct *vma)
  413. {
  414. VM_BUG_ON_PAGE(PageLRU(page), page);
  415. if (likely((vma->vm_flags & (VM_LOCKED | VM_SPECIAL)) != VM_LOCKED)) {
  416. SetPageActive(page);
  417. lru_cache_add(page);
  418. return;
  419. }
  420. if (!TestSetPageMlocked(page)) {
  421. /*
  422. * We use the irq-unsafe __mod_zone_page_stat because this
  423. * counter is not modified from interrupt context, and the pte
  424. * lock is held(spinlock), which implies preemption disabled.
  425. */
  426. __mod_zone_page_state(page_zone(page), NR_MLOCK,
  427. hpage_nr_pages(page));
  428. count_vm_event(UNEVICTABLE_PGMLOCKED);
  429. }
  430. add_page_to_unevictable_list(page);
  431. }
  432. /*
  433. * If the page can not be invalidated, it is moved to the
  434. * inactive list to speed up its reclaim. It is moved to the
  435. * head of the list, rather than the tail, to give the flusher
  436. * threads some time to write it out, as this is much more
  437. * effective than the single-page writeout from reclaim.
  438. *
  439. * If the page isn't page_mapped and dirty/writeback, the page
  440. * could reclaim asap using PG_reclaim.
  441. *
  442. * 1. active, mapped page -> none
  443. * 2. active, dirty/writeback page -> inactive, head, PG_reclaim
  444. * 3. inactive, mapped page -> none
  445. * 4. inactive, dirty/writeback page -> inactive, head, PG_reclaim
  446. * 5. inactive, clean -> inactive, tail
  447. * 6. Others -> none
  448. *
  449. * In 4, why it moves inactive's head, the VM expects the page would
  450. * be write it out by flusher threads as this is much more effective
  451. * than the single-page writeout from reclaim.
  452. */
  453. static void lru_deactivate_file_fn(struct page *page, struct lruvec *lruvec,
  454. void *arg)
  455. {
  456. int lru, file;
  457. bool active;
  458. if (!PageLRU(page))
  459. return;
  460. if (PageUnevictable(page))
  461. return;
  462. /* Some processes are using the page */
  463. if (page_mapped(page))
  464. return;
  465. active = PageActive(page);
  466. file = page_is_file_cache(page);
  467. lru = page_lru_base_type(page);
  468. del_page_from_lru_list(page, lruvec, lru + active);
  469. ClearPageActive(page);
  470. ClearPageReferenced(page);
  471. add_page_to_lru_list(page, lruvec, lru);
  472. if (PageWriteback(page) || PageDirty(page)) {
  473. /*
  474. * PG_reclaim could be raced with end_page_writeback
  475. * It can make readahead confusing. But race window
  476. * is _really_ small and it's non-critical problem.
  477. */
  478. SetPageReclaim(page);
  479. } else {
  480. /*
  481. * The page's writeback ends up during pagevec
  482. * We moves tha page into tail of inactive.
  483. */
  484. list_move_tail(&page->lru, &lruvec->lists[lru]);
  485. __count_vm_event(PGROTATED);
  486. }
  487. if (active)
  488. __count_vm_event(PGDEACTIVATE);
  489. update_page_reclaim_stat(lruvec, file, 0);
  490. }
  491. static void lru_deactivate_fn(struct page *page, struct lruvec *lruvec,
  492. void *arg)
  493. {
  494. if (PageLRU(page) && PageActive(page) && !PageUnevictable(page)) {
  495. int file = page_is_file_cache(page);
  496. int lru = page_lru_base_type(page);
  497. del_page_from_lru_list(page, lruvec, lru + LRU_ACTIVE);
  498. ClearPageActive(page);
  499. ClearPageReferenced(page);
  500. add_page_to_lru_list(page, lruvec, lru);
  501. __count_vm_event(PGDEACTIVATE);
  502. update_page_reclaim_stat(lruvec, file, 0);
  503. }
  504. }
  505. /*
  506. * Drain pages out of the cpu's pagevecs.
  507. * Either "cpu" is the current CPU, and preemption has already been
  508. * disabled; or "cpu" is being hot-unplugged, and is already dead.
  509. */
  510. void lru_add_drain_cpu(int cpu)
  511. {
  512. struct pagevec *pvec = &per_cpu(lru_add_pvec, cpu);
  513. if (pagevec_count(pvec))
  514. __pagevec_lru_add(pvec);
  515. pvec = &per_cpu(lru_rotate_pvecs, cpu);
  516. if (pagevec_count(pvec)) {
  517. unsigned long flags;
  518. /* No harm done if a racing interrupt already did this */
  519. local_irq_save(flags);
  520. pagevec_move_tail(pvec);
  521. local_irq_restore(flags);
  522. }
  523. pvec = &per_cpu(lru_deactivate_file_pvecs, cpu);
  524. if (pagevec_count(pvec))
  525. pagevec_lru_move_fn(pvec, lru_deactivate_file_fn, NULL);
  526. pvec = &per_cpu(lru_deactivate_pvecs, cpu);
  527. if (pagevec_count(pvec))
  528. pagevec_lru_move_fn(pvec, lru_deactivate_fn, NULL);
  529. activate_page_drain(cpu);
  530. }
  531. /**
  532. * deactivate_file_page - forcefully deactivate a file page
  533. * @page: page to deactivate
  534. *
  535. * This function hints the VM that @page is a good reclaim candidate,
  536. * for example if its invalidation fails due to the page being dirty
  537. * or under writeback.
  538. */
  539. void deactivate_file_page(struct page *page)
  540. {
  541. /*
  542. * In a workload with many unevictable page such as mprotect,
  543. * unevictable page deactivation for accelerating reclaim is pointless.
  544. */
  545. if (PageUnevictable(page))
  546. return;
  547. if (likely(get_page_unless_zero(page))) {
  548. struct pagevec *pvec = &get_cpu_var(lru_deactivate_file_pvecs);
  549. if (!pagevec_add(pvec, page))
  550. pagevec_lru_move_fn(pvec, lru_deactivate_file_fn, NULL);
  551. put_cpu_var(lru_deactivate_file_pvecs);
  552. }
  553. }
  554. /**
  555. * deactivate_page - deactivate a page
  556. * @page: page to deactivate
  557. *
  558. * deactivate_page() moves @page to the inactive list if @page was on the active
  559. * list and was not an unevictable page. This is done to accelerate the reclaim
  560. * of @page.
  561. */
  562. void deactivate_page(struct page *page)
  563. {
  564. if (PageLRU(page) && PageActive(page) && !PageUnevictable(page)) {
  565. struct pagevec *pvec = &get_cpu_var(lru_deactivate_pvecs);
  566. get_page(page);
  567. if (!pagevec_add(pvec, page))
  568. pagevec_lru_move_fn(pvec, lru_deactivate_fn, NULL);
  569. put_cpu_var(lru_deactivate_pvecs);
  570. }
  571. }
  572. void lru_add_drain(void)
  573. {
  574. lru_add_drain_cpu(get_cpu());
  575. put_cpu();
  576. }
  577. static void lru_add_drain_per_cpu(struct work_struct *dummy)
  578. {
  579. lru_add_drain();
  580. }
  581. static DEFINE_PER_CPU(struct work_struct, lru_add_drain_work);
  582. void lru_add_drain_all(void)
  583. {
  584. static DEFINE_MUTEX(lock);
  585. static struct cpumask has_work;
  586. int cpu;
  587. mutex_lock(&lock);
  588. get_online_cpus();
  589. cpumask_clear(&has_work);
  590. for_each_online_cpu(cpu) {
  591. struct work_struct *work = &per_cpu(lru_add_drain_work, cpu);
  592. if (pagevec_count(&per_cpu(lru_add_pvec, cpu)) ||
  593. pagevec_count(&per_cpu(lru_rotate_pvecs, cpu)) ||
  594. pagevec_count(&per_cpu(lru_deactivate_file_pvecs, cpu)) ||
  595. pagevec_count(&per_cpu(lru_deactivate_pvecs, cpu)) ||
  596. need_activate_page_drain(cpu)) {
  597. INIT_WORK(work, lru_add_drain_per_cpu);
  598. schedule_work_on(cpu, work);
  599. cpumask_set_cpu(cpu, &has_work);
  600. }
  601. }
  602. for_each_cpu(cpu, &has_work)
  603. flush_work(&per_cpu(lru_add_drain_work, cpu));
  604. put_online_cpus();
  605. mutex_unlock(&lock);
  606. }
  607. /**
  608. * release_pages - batched put_page()
  609. * @pages: array of pages to release
  610. * @nr: number of pages
  611. * @cold: whether the pages are cache cold
  612. *
  613. * Decrement the reference count on all the pages in @pages. If it
  614. * fell to zero, remove the page from the LRU and free it.
  615. */
  616. void release_pages(struct page **pages, int nr, bool cold)
  617. {
  618. int i;
  619. LIST_HEAD(pages_to_free);
  620. struct zone *zone = NULL;
  621. struct lruvec *lruvec;
  622. unsigned long uninitialized_var(flags);
  623. unsigned int uninitialized_var(lock_batch);
  624. for (i = 0; i < nr; i++) {
  625. struct page *page = pages[i];
  626. /*
  627. * Make sure the IRQ-safe lock-holding time does not get
  628. * excessive with a continuous string of pages from the
  629. * same zone. The lock is held only if zone != NULL.
  630. */
  631. if (zone && ++lock_batch == SWAP_CLUSTER_MAX) {
  632. spin_unlock_irqrestore(&zone->lru_lock, flags);
  633. zone = NULL;
  634. }
  635. if (is_huge_zero_page(page)) {
  636. put_huge_zero_page();
  637. continue;
  638. }
  639. page = compound_head(page);
  640. if (!put_page_testzero(page))
  641. continue;
  642. if (PageCompound(page)) {
  643. if (zone) {
  644. spin_unlock_irqrestore(&zone->lru_lock, flags);
  645. zone = NULL;
  646. }
  647. __put_compound_page(page);
  648. continue;
  649. }
  650. if (PageLRU(page)) {
  651. struct zone *pagezone = page_zone(page);
  652. if (pagezone != zone) {
  653. if (zone)
  654. spin_unlock_irqrestore(&zone->lru_lock,
  655. flags);
  656. lock_batch = 0;
  657. zone = pagezone;
  658. spin_lock_irqsave(&zone->lru_lock, flags);
  659. }
  660. lruvec = mem_cgroup_page_lruvec(page, zone);
  661. VM_BUG_ON_PAGE(!PageLRU(page), page);
  662. __ClearPageLRU(page);
  663. del_page_from_lru_list(page, lruvec, page_off_lru(page));
  664. }
  665. /* Clear Active bit in case of parallel mark_page_accessed */
  666. __ClearPageActive(page);
  667. list_add(&page->lru, &pages_to_free);
  668. }
  669. if (zone)
  670. spin_unlock_irqrestore(&zone->lru_lock, flags);
  671. mem_cgroup_uncharge_list(&pages_to_free);
  672. free_hot_cold_page_list(&pages_to_free, cold);
  673. }
  674. EXPORT_SYMBOL(release_pages);
  675. /*
  676. * The pages which we're about to release may be in the deferred lru-addition
  677. * queues. That would prevent them from really being freed right now. That's
  678. * OK from a correctness point of view but is inefficient - those pages may be
  679. * cache-warm and we want to give them back to the page allocator ASAP.
  680. *
  681. * So __pagevec_release() will drain those queues here. __pagevec_lru_add()
  682. * and __pagevec_lru_add_active() call release_pages() directly to avoid
  683. * mutual recursion.
  684. */
  685. void __pagevec_release(struct pagevec *pvec)
  686. {
  687. lru_add_drain();
  688. release_pages(pvec->pages, pagevec_count(pvec), pvec->cold);
  689. pagevec_reinit(pvec);
  690. }
  691. EXPORT_SYMBOL(__pagevec_release);
  692. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  693. /* used by __split_huge_page_refcount() */
  694. void lru_add_page_tail(struct page *page, struct page *page_tail,
  695. struct lruvec *lruvec, struct list_head *list)
  696. {
  697. const int file = 0;
  698. VM_BUG_ON_PAGE(!PageHead(page), page);
  699. VM_BUG_ON_PAGE(PageCompound(page_tail), page);
  700. VM_BUG_ON_PAGE(PageLRU(page_tail), page);
  701. VM_BUG_ON(NR_CPUS != 1 &&
  702. !spin_is_locked(&lruvec_zone(lruvec)->lru_lock));
  703. if (!list)
  704. SetPageLRU(page_tail);
  705. if (likely(PageLRU(page)))
  706. list_add_tail(&page_tail->lru, &page->lru);
  707. else if (list) {
  708. /* page reclaim is reclaiming a huge page */
  709. get_page(page_tail);
  710. list_add_tail(&page_tail->lru, list);
  711. } else {
  712. struct list_head *list_head;
  713. /*
  714. * Head page has not yet been counted, as an hpage,
  715. * so we must account for each subpage individually.
  716. *
  717. * Use the standard add function to put page_tail on the list,
  718. * but then correct its position so they all end up in order.
  719. */
  720. add_page_to_lru_list(page_tail, lruvec, page_lru(page_tail));
  721. list_head = page_tail->lru.prev;
  722. list_move_tail(&page_tail->lru, list_head);
  723. }
  724. if (!PageUnevictable(page))
  725. update_page_reclaim_stat(lruvec, file, PageActive(page_tail));
  726. }
  727. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  728. static void __pagevec_lru_add_fn(struct page *page, struct lruvec *lruvec,
  729. void *arg)
  730. {
  731. int file = page_is_file_cache(page);
  732. int active = PageActive(page);
  733. enum lru_list lru = page_lru(page);
  734. VM_BUG_ON_PAGE(PageLRU(page), page);
  735. SetPageLRU(page);
  736. add_page_to_lru_list(page, lruvec, lru);
  737. update_page_reclaim_stat(lruvec, file, active);
  738. trace_mm_lru_insertion(page, lru);
  739. }
  740. /*
  741. * Add the passed pages to the LRU, then drop the caller's refcount
  742. * on them. Reinitialises the caller's pagevec.
  743. */
  744. void __pagevec_lru_add(struct pagevec *pvec)
  745. {
  746. pagevec_lru_move_fn(pvec, __pagevec_lru_add_fn, NULL);
  747. }
  748. EXPORT_SYMBOL(__pagevec_lru_add);
  749. /**
  750. * pagevec_lookup_entries - gang pagecache lookup
  751. * @pvec: Where the resulting entries are placed
  752. * @mapping: The address_space to search
  753. * @start: The starting entry index
  754. * @nr_entries: The maximum number of entries
  755. * @indices: The cache indices corresponding to the entries in @pvec
  756. *
  757. * pagevec_lookup_entries() will search for and return a group of up
  758. * to @nr_entries pages and shadow entries in the mapping. All
  759. * entries are placed in @pvec. pagevec_lookup_entries() takes a
  760. * reference against actual pages in @pvec.
  761. *
  762. * The search returns a group of mapping-contiguous entries with
  763. * ascending indexes. There may be holes in the indices due to
  764. * not-present entries.
  765. *
  766. * pagevec_lookup_entries() returns the number of entries which were
  767. * found.
  768. */
  769. unsigned pagevec_lookup_entries(struct pagevec *pvec,
  770. struct address_space *mapping,
  771. pgoff_t start, unsigned nr_pages,
  772. pgoff_t *indices)
  773. {
  774. pvec->nr = find_get_entries(mapping, start, nr_pages,
  775. pvec->pages, indices);
  776. return pagevec_count(pvec);
  777. }
  778. /**
  779. * pagevec_remove_exceptionals - pagevec exceptionals pruning
  780. * @pvec: The pagevec to prune
  781. *
  782. * pagevec_lookup_entries() fills both pages and exceptional radix
  783. * tree entries into the pagevec. This function prunes all
  784. * exceptionals from @pvec without leaving holes, so that it can be
  785. * passed on to page-only pagevec operations.
  786. */
  787. void pagevec_remove_exceptionals(struct pagevec *pvec)
  788. {
  789. int i, j;
  790. for (i = 0, j = 0; i < pagevec_count(pvec); i++) {
  791. struct page *page = pvec->pages[i];
  792. if (!radix_tree_exceptional_entry(page))
  793. pvec->pages[j++] = page;
  794. }
  795. pvec->nr = j;
  796. }
  797. /**
  798. * pagevec_lookup - gang pagecache lookup
  799. * @pvec: Where the resulting pages are placed
  800. * @mapping: The address_space to search
  801. * @start: The starting page index
  802. * @nr_pages: The maximum number of pages
  803. *
  804. * pagevec_lookup() will search for and return a group of up to @nr_pages pages
  805. * in the mapping. The pages are placed in @pvec. pagevec_lookup() takes a
  806. * reference against the pages in @pvec.
  807. *
  808. * The search returns a group of mapping-contiguous pages with ascending
  809. * indexes. There may be holes in the indices due to not-present pages.
  810. *
  811. * pagevec_lookup() returns the number of pages which were found.
  812. */
  813. unsigned pagevec_lookup(struct pagevec *pvec, struct address_space *mapping,
  814. pgoff_t start, unsigned nr_pages)
  815. {
  816. pvec->nr = find_get_pages(mapping, start, nr_pages, pvec->pages);
  817. return pagevec_count(pvec);
  818. }
  819. EXPORT_SYMBOL(pagevec_lookup);
  820. unsigned pagevec_lookup_tag(struct pagevec *pvec, struct address_space *mapping,
  821. pgoff_t *index, int tag, unsigned nr_pages)
  822. {
  823. pvec->nr = find_get_pages_tag(mapping, index, tag,
  824. nr_pages, pvec->pages);
  825. return pagevec_count(pvec);
  826. }
  827. EXPORT_SYMBOL(pagevec_lookup_tag);
  828. /*
  829. * Perform any setup for the swap system
  830. */
  831. void __init swap_setup(void)
  832. {
  833. unsigned long megs = totalram_pages >> (20 - PAGE_SHIFT);
  834. #ifdef CONFIG_SWAP
  835. int i;
  836. for (i = 0; i < MAX_SWAPFILES; i++)
  837. spin_lock_init(&swapper_spaces[i].tree_lock);
  838. #endif
  839. /* Use a smaller cluster for small-memory machines */
  840. if (megs < 16)
  841. page_cluster = 2;
  842. else
  843. page_cluster = 3;
  844. /*
  845. * Right now other parts of the system means that we
  846. * _really_ don't want to cluster much more
  847. */
  848. }