swap.c 29 KB

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