swap.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047
  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. void activate_page(struct page *page)
  281. {
  282. struct zone *zone = page_zone(page);
  283. page = compound_head(page);
  284. spin_lock_irq(zone_lru_lock(zone));
  285. __activate_page(page, mem_cgroup_page_lruvec(page, zone->zone_pgdat), NULL);
  286. spin_unlock_irq(zone_lru_lock(zone));
  287. }
  288. #endif
  289. static void __lru_cache_activate_page(struct page *page)
  290. {
  291. struct pagevec *pvec = &get_cpu_var(lru_add_pvec);
  292. int i;
  293. /*
  294. * Search backwards on the optimistic assumption that the page being
  295. * activated has just been added to this pagevec. Note that only
  296. * the local pagevec is examined as a !PageLRU page could be in the
  297. * process of being released, reclaimed, migrated or on a remote
  298. * pagevec that is currently being drained. Furthermore, marking
  299. * a remote pagevec's page PageActive potentially hits a race where
  300. * a page is marked PageActive just after it is added to the inactive
  301. * list causing accounting errors and BUG_ON checks to trigger.
  302. */
  303. for (i = pagevec_count(pvec) - 1; i >= 0; i--) {
  304. struct page *pagevec_page = pvec->pages[i];
  305. if (pagevec_page == page) {
  306. SetPageActive(page);
  307. break;
  308. }
  309. }
  310. put_cpu_var(lru_add_pvec);
  311. }
  312. /*
  313. * Mark a page as having seen activity.
  314. *
  315. * inactive,unreferenced -> inactive,referenced
  316. * inactive,referenced -> active,unreferenced
  317. * active,unreferenced -> active,referenced
  318. *
  319. * When a newly allocated page is not yet visible, so safe for non-atomic ops,
  320. * __SetPageReferenced(page) may be substituted for mark_page_accessed(page).
  321. */
  322. void mark_page_accessed(struct page *page)
  323. {
  324. page = compound_head(page);
  325. if (!PageActive(page) && !PageUnevictable(page) &&
  326. PageReferenced(page)) {
  327. /*
  328. * If the page is on the LRU, queue it for activation via
  329. * activate_page_pvecs. Otherwise, assume the page is on a
  330. * pagevec, mark it active and it'll be moved to the active
  331. * LRU on the next drain.
  332. */
  333. if (PageLRU(page))
  334. activate_page(page);
  335. else
  336. __lru_cache_activate_page(page);
  337. ClearPageReferenced(page);
  338. if (page_is_file_cache(page))
  339. workingset_activation(page);
  340. } else if (!PageReferenced(page)) {
  341. SetPageReferenced(page);
  342. }
  343. if (page_is_idle(page))
  344. clear_page_idle(page);
  345. }
  346. EXPORT_SYMBOL(mark_page_accessed);
  347. static void __lru_cache_add(struct page *page)
  348. {
  349. struct pagevec *pvec = &get_cpu_var(lru_add_pvec);
  350. get_page(page);
  351. if (!pagevec_add(pvec, page) || PageCompound(page))
  352. __pagevec_lru_add(pvec);
  353. put_cpu_var(lru_add_pvec);
  354. }
  355. /**
  356. * lru_cache_add_anon - add a page to the page lists
  357. * @page: the page to add
  358. */
  359. void lru_cache_add_anon(struct page *page)
  360. {
  361. if (PageActive(page))
  362. ClearPageActive(page);
  363. __lru_cache_add(page);
  364. }
  365. void lru_cache_add_file(struct page *page)
  366. {
  367. if (PageActive(page))
  368. ClearPageActive(page);
  369. __lru_cache_add(page);
  370. }
  371. EXPORT_SYMBOL(lru_cache_add_file);
  372. /**
  373. * lru_cache_add - add a page to a page list
  374. * @page: the page to be added to the LRU.
  375. *
  376. * Queue the page for addition to the LRU via pagevec. The decision on whether
  377. * to add the page to the [in]active [file|anon] list is deferred until the
  378. * pagevec is drained. This gives a chance for the caller of lru_cache_add()
  379. * have the page added to the active list using mark_page_accessed().
  380. */
  381. void lru_cache_add(struct page *page)
  382. {
  383. VM_BUG_ON_PAGE(PageActive(page) && PageUnevictable(page), page);
  384. VM_BUG_ON_PAGE(PageLRU(page), page);
  385. __lru_cache_add(page);
  386. }
  387. /**
  388. * lru_cache_add_active_or_unevictable
  389. * @page: the page to be added to LRU
  390. * @vma: vma in which page is mapped for determining reclaimability
  391. *
  392. * Place @page on the active or unevictable LRU list, depending on its
  393. * evictability. Note that if the page is not evictable, it goes
  394. * directly back onto it's zone's unevictable list, it does NOT use a
  395. * per cpu pagevec.
  396. */
  397. void lru_cache_add_active_or_unevictable(struct page *page,
  398. struct vm_area_struct *vma)
  399. {
  400. VM_BUG_ON_PAGE(PageLRU(page), page);
  401. if (likely((vma->vm_flags & (VM_LOCKED | VM_SPECIAL)) != VM_LOCKED))
  402. SetPageActive(page);
  403. else if (!TestSetPageMlocked(page)) {
  404. /*
  405. * We use the irq-unsafe __mod_zone_page_stat because this
  406. * counter is not modified from interrupt context, and the pte
  407. * lock is held(spinlock), which implies preemption disabled.
  408. */
  409. __mod_zone_page_state(page_zone(page), NR_MLOCK,
  410. hpage_nr_pages(page));
  411. count_vm_event(UNEVICTABLE_PGMLOCKED);
  412. }
  413. lru_cache_add(page);
  414. }
  415. /*
  416. * If the page can not be invalidated, it is moved to the
  417. * inactive list to speed up its reclaim. It is moved to the
  418. * head of the list, rather than the tail, to give the flusher
  419. * threads some time to write it out, as this is much more
  420. * effective than the single-page writeout from reclaim.
  421. *
  422. * If the page isn't page_mapped and dirty/writeback, the page
  423. * could reclaim asap using PG_reclaim.
  424. *
  425. * 1. active, mapped page -> none
  426. * 2. active, dirty/writeback page -> inactive, head, PG_reclaim
  427. * 3. inactive, mapped page -> none
  428. * 4. inactive, dirty/writeback page -> inactive, head, PG_reclaim
  429. * 5. inactive, clean -> inactive, tail
  430. * 6. Others -> none
  431. *
  432. * In 4, why it moves inactive's head, the VM expects the page would
  433. * be write it out by flusher threads as this is much more effective
  434. * than the single-page writeout from reclaim.
  435. */
  436. static void lru_deactivate_file_fn(struct page *page, struct lruvec *lruvec,
  437. void *arg)
  438. {
  439. int lru, file;
  440. bool active;
  441. if (!PageLRU(page))
  442. return;
  443. if (PageUnevictable(page))
  444. return;
  445. /* Some processes are using the page */
  446. if (page_mapped(page))
  447. return;
  448. active = PageActive(page);
  449. file = page_is_file_cache(page);
  450. lru = page_lru_base_type(page);
  451. del_page_from_lru_list(page, lruvec, lru + active);
  452. ClearPageActive(page);
  453. ClearPageReferenced(page);
  454. add_page_to_lru_list(page, lruvec, lru);
  455. if (PageWriteback(page) || PageDirty(page)) {
  456. /*
  457. * PG_reclaim could be raced with end_page_writeback
  458. * It can make readahead confusing. But race window
  459. * is _really_ small and it's non-critical problem.
  460. */
  461. SetPageReclaim(page);
  462. } else {
  463. /*
  464. * The page's writeback ends up during pagevec
  465. * We moves tha page into tail of inactive.
  466. */
  467. list_move_tail(&page->lru, &lruvec->lists[lru]);
  468. __count_vm_event(PGROTATED);
  469. }
  470. if (active)
  471. __count_vm_event(PGDEACTIVATE);
  472. update_page_reclaim_stat(lruvec, file, 0);
  473. }
  474. static void lru_lazyfree_fn(struct page *page, struct lruvec *lruvec,
  475. void *arg)
  476. {
  477. if (PageLRU(page) && PageAnon(page) && PageSwapBacked(page) &&
  478. !PageSwapCache(page) && !PageUnevictable(page)) {
  479. bool active = PageActive(page);
  480. del_page_from_lru_list(page, lruvec,
  481. LRU_INACTIVE_ANON + active);
  482. ClearPageActive(page);
  483. ClearPageReferenced(page);
  484. /*
  485. * lazyfree pages are clean anonymous pages. They have
  486. * SwapBacked flag cleared to distinguish normal anonymous
  487. * pages
  488. */
  489. ClearPageSwapBacked(page);
  490. add_page_to_lru_list(page, lruvec, LRU_INACTIVE_FILE);
  491. __count_vm_events(PGLAZYFREE, hpage_nr_pages(page));
  492. count_memcg_page_event(page, PGLAZYFREE);
  493. update_page_reclaim_stat(lruvec, 1, 0);
  494. }
  495. }
  496. /*
  497. * Drain pages out of the cpu's pagevecs.
  498. * Either "cpu" is the current CPU, and preemption has already been
  499. * disabled; or "cpu" is being hot-unplugged, and is already dead.
  500. */
  501. void lru_add_drain_cpu(int cpu)
  502. {
  503. struct pagevec *pvec = &per_cpu(lru_add_pvec, cpu);
  504. if (pagevec_count(pvec))
  505. __pagevec_lru_add(pvec);
  506. pvec = &per_cpu(lru_rotate_pvecs, cpu);
  507. if (pagevec_count(pvec)) {
  508. unsigned long flags;
  509. /* No harm done if a racing interrupt already did this */
  510. local_irq_save(flags);
  511. pagevec_move_tail(pvec);
  512. local_irq_restore(flags);
  513. }
  514. pvec = &per_cpu(lru_deactivate_file_pvecs, cpu);
  515. if (pagevec_count(pvec))
  516. pagevec_lru_move_fn(pvec, lru_deactivate_file_fn, NULL);
  517. pvec = &per_cpu(lru_lazyfree_pvecs, cpu);
  518. if (pagevec_count(pvec))
  519. pagevec_lru_move_fn(pvec, lru_lazyfree_fn, NULL);
  520. activate_page_drain(cpu);
  521. }
  522. /**
  523. * deactivate_file_page - forcefully deactivate a file page
  524. * @page: page to deactivate
  525. *
  526. * This function hints the VM that @page is a good reclaim candidate,
  527. * for example if its invalidation fails due to the page being dirty
  528. * or under writeback.
  529. */
  530. void deactivate_file_page(struct page *page)
  531. {
  532. /*
  533. * In a workload with many unevictable page such as mprotect,
  534. * unevictable page deactivation for accelerating reclaim is pointless.
  535. */
  536. if (PageUnevictable(page))
  537. return;
  538. if (likely(get_page_unless_zero(page))) {
  539. struct pagevec *pvec = &get_cpu_var(lru_deactivate_file_pvecs);
  540. if (!pagevec_add(pvec, page) || PageCompound(page))
  541. pagevec_lru_move_fn(pvec, lru_deactivate_file_fn, NULL);
  542. put_cpu_var(lru_deactivate_file_pvecs);
  543. }
  544. }
  545. /**
  546. * mark_page_lazyfree - make an anon page lazyfree
  547. * @page: page to deactivate
  548. *
  549. * mark_page_lazyfree() moves @page to the inactive file list.
  550. * This is done to accelerate the reclaim of @page.
  551. */
  552. void mark_page_lazyfree(struct page *page)
  553. {
  554. if (PageLRU(page) && PageAnon(page) && PageSwapBacked(page) &&
  555. !PageSwapCache(page) && !PageUnevictable(page)) {
  556. struct pagevec *pvec = &get_cpu_var(lru_lazyfree_pvecs);
  557. get_page(page);
  558. if (!pagevec_add(pvec, page) || PageCompound(page))
  559. pagevec_lru_move_fn(pvec, lru_lazyfree_fn, NULL);
  560. put_cpu_var(lru_lazyfree_pvecs);
  561. }
  562. }
  563. void lru_add_drain(void)
  564. {
  565. lru_add_drain_cpu(get_cpu());
  566. put_cpu();
  567. }
  568. #ifdef CONFIG_SMP
  569. static DEFINE_PER_CPU(struct work_struct, lru_add_drain_work);
  570. static void lru_add_drain_per_cpu(struct work_struct *dummy)
  571. {
  572. lru_add_drain();
  573. }
  574. /*
  575. * Doesn't need any cpu hotplug locking because we do rely on per-cpu
  576. * kworkers being shut down before our page_alloc_cpu_dead callback is
  577. * executed on the offlined cpu.
  578. * Calling this function with cpu hotplug locks held can actually lead
  579. * to obscure indirect dependencies via WQ context.
  580. */
  581. void lru_add_drain_all(void)
  582. {
  583. static DEFINE_MUTEX(lock);
  584. static struct cpumask has_work;
  585. int cpu;
  586. /*
  587. * Make sure nobody triggers this path before mm_percpu_wq is fully
  588. * initialized.
  589. */
  590. if (WARN_ON(!mm_percpu_wq))
  591. return;
  592. mutex_lock(&lock);
  593. cpumask_clear(&has_work);
  594. for_each_online_cpu(cpu) {
  595. struct work_struct *work = &per_cpu(lru_add_drain_work, cpu);
  596. if (pagevec_count(&per_cpu(lru_add_pvec, cpu)) ||
  597. pagevec_count(&per_cpu(lru_rotate_pvecs, cpu)) ||
  598. pagevec_count(&per_cpu(lru_deactivate_file_pvecs, cpu)) ||
  599. pagevec_count(&per_cpu(lru_lazyfree_pvecs, cpu)) ||
  600. need_activate_page_drain(cpu)) {
  601. INIT_WORK(work, lru_add_drain_per_cpu);
  602. queue_work_on(cpu, mm_percpu_wq, work);
  603. cpumask_set_cpu(cpu, &has_work);
  604. }
  605. }
  606. for_each_cpu(cpu, &has_work)
  607. flush_work(&per_cpu(lru_add_drain_work, cpu));
  608. mutex_unlock(&lock);
  609. }
  610. #else
  611. void lru_add_drain_all(void)
  612. {
  613. lru_add_drain();
  614. }
  615. #endif
  616. /**
  617. * release_pages - batched put_page()
  618. * @pages: array of pages to release
  619. * @nr: number of pages
  620. *
  621. * Decrement the reference count on all the pages in @pages. If it
  622. * fell to zero, remove the page from the LRU and free it.
  623. */
  624. void release_pages(struct page **pages, int nr)
  625. {
  626. int i;
  627. LIST_HEAD(pages_to_free);
  628. struct pglist_data *locked_pgdat = NULL;
  629. struct lruvec *lruvec;
  630. unsigned long uninitialized_var(flags);
  631. unsigned int uninitialized_var(lock_batch);
  632. for (i = 0; i < nr; i++) {
  633. struct page *page = pages[i];
  634. /*
  635. * Make sure the IRQ-safe lock-holding time does not get
  636. * excessive with a continuous string of pages from the
  637. * same pgdat. The lock is held only if pgdat != NULL.
  638. */
  639. if (locked_pgdat && ++lock_batch == SWAP_CLUSTER_MAX) {
  640. spin_unlock_irqrestore(&locked_pgdat->lru_lock, flags);
  641. locked_pgdat = NULL;
  642. }
  643. if (is_huge_zero_page(page))
  644. continue;
  645. if (is_zone_device_page(page)) {
  646. if (locked_pgdat) {
  647. spin_unlock_irqrestore(&locked_pgdat->lru_lock,
  648. flags);
  649. locked_pgdat = NULL;
  650. }
  651. /*
  652. * ZONE_DEVICE pages that return 'false' from
  653. * put_devmap_managed_page() do not require special
  654. * processing, and instead, expect a call to
  655. * put_page_testzero().
  656. */
  657. if (put_devmap_managed_page(page))
  658. continue;
  659. }
  660. page = compound_head(page);
  661. if (!put_page_testzero(page))
  662. continue;
  663. if (PageCompound(page)) {
  664. if (locked_pgdat) {
  665. spin_unlock_irqrestore(&locked_pgdat->lru_lock, flags);
  666. locked_pgdat = NULL;
  667. }
  668. __put_compound_page(page);
  669. continue;
  670. }
  671. if (PageLRU(page)) {
  672. struct pglist_data *pgdat = page_pgdat(page);
  673. if (pgdat != locked_pgdat) {
  674. if (locked_pgdat)
  675. spin_unlock_irqrestore(&locked_pgdat->lru_lock,
  676. flags);
  677. lock_batch = 0;
  678. locked_pgdat = pgdat;
  679. spin_lock_irqsave(&locked_pgdat->lru_lock, flags);
  680. }
  681. lruvec = mem_cgroup_page_lruvec(page, locked_pgdat);
  682. VM_BUG_ON_PAGE(!PageLRU(page), page);
  683. __ClearPageLRU(page);
  684. del_page_from_lru_list(page, lruvec, page_off_lru(page));
  685. }
  686. /* Clear Active bit in case of parallel mark_page_accessed */
  687. __ClearPageActive(page);
  688. __ClearPageWaiters(page);
  689. list_add(&page->lru, &pages_to_free);
  690. }
  691. if (locked_pgdat)
  692. spin_unlock_irqrestore(&locked_pgdat->lru_lock, flags);
  693. mem_cgroup_uncharge_list(&pages_to_free);
  694. free_unref_page_list(&pages_to_free);
  695. }
  696. EXPORT_SYMBOL(release_pages);
  697. /*
  698. * The pages which we're about to release may be in the deferred lru-addition
  699. * queues. That would prevent them from really being freed right now. That's
  700. * OK from a correctness point of view but is inefficient - those pages may be
  701. * cache-warm and we want to give them back to the page allocator ASAP.
  702. *
  703. * So __pagevec_release() will drain those queues here. __pagevec_lru_add()
  704. * and __pagevec_lru_add_active() call release_pages() directly to avoid
  705. * mutual recursion.
  706. */
  707. void __pagevec_release(struct pagevec *pvec)
  708. {
  709. if (!pvec->percpu_pvec_drained) {
  710. lru_add_drain();
  711. pvec->percpu_pvec_drained = true;
  712. }
  713. release_pages(pvec->pages, pagevec_count(pvec));
  714. pagevec_reinit(pvec);
  715. }
  716. EXPORT_SYMBOL(__pagevec_release);
  717. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  718. /* used by __split_huge_page_refcount() */
  719. void lru_add_page_tail(struct page *page, struct page *page_tail,
  720. struct lruvec *lruvec, struct list_head *list)
  721. {
  722. const int file = 0;
  723. VM_BUG_ON_PAGE(!PageHead(page), page);
  724. VM_BUG_ON_PAGE(PageCompound(page_tail), page);
  725. VM_BUG_ON_PAGE(PageLRU(page_tail), page);
  726. VM_BUG_ON(NR_CPUS != 1 &&
  727. !spin_is_locked(&lruvec_pgdat(lruvec)->lru_lock));
  728. if (!list)
  729. SetPageLRU(page_tail);
  730. if (likely(PageLRU(page)))
  731. list_add_tail(&page_tail->lru, &page->lru);
  732. else if (list) {
  733. /* page reclaim is reclaiming a huge page */
  734. get_page(page_tail);
  735. list_add_tail(&page_tail->lru, list);
  736. } else {
  737. struct list_head *list_head;
  738. /*
  739. * Head page has not yet been counted, as an hpage,
  740. * so we must account for each subpage individually.
  741. *
  742. * Use the standard add function to put page_tail on the list,
  743. * but then correct its position so they all end up in order.
  744. */
  745. add_page_to_lru_list(page_tail, lruvec, page_lru(page_tail));
  746. list_head = page_tail->lru.prev;
  747. list_move_tail(&page_tail->lru, list_head);
  748. }
  749. if (!PageUnevictable(page))
  750. update_page_reclaim_stat(lruvec, file, PageActive(page_tail));
  751. }
  752. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  753. static void __pagevec_lru_add_fn(struct page *page, struct lruvec *lruvec,
  754. void *arg)
  755. {
  756. enum lru_list lru;
  757. int was_unevictable = TestClearPageUnevictable(page);
  758. VM_BUG_ON_PAGE(PageLRU(page), page);
  759. SetPageLRU(page);
  760. /*
  761. * Page becomes evictable in two ways:
  762. * 1) Within LRU lock [munlock_vma_pages() and __munlock_pagevec()].
  763. * 2) Before acquiring LRU lock to put the page to correct LRU and then
  764. * a) do PageLRU check with lock [check_move_unevictable_pages]
  765. * b) do PageLRU check before lock [clear_page_mlock]
  766. *
  767. * (1) & (2a) are ok as LRU lock will serialize them. For (2b), we need
  768. * following strict ordering:
  769. *
  770. * #0: __pagevec_lru_add_fn #1: clear_page_mlock
  771. *
  772. * SetPageLRU() TestClearPageMlocked()
  773. * smp_mb() // explicit ordering // above provides strict
  774. * // ordering
  775. * PageMlocked() PageLRU()
  776. *
  777. *
  778. * if '#1' does not observe setting of PG_lru by '#0' and fails
  779. * isolation, the explicit barrier will make sure that page_evictable
  780. * check will put the page in correct LRU. Without smp_mb(), SetPageLRU
  781. * can be reordered after PageMlocked check and can make '#1' to fail
  782. * the isolation of the page whose Mlocked bit is cleared (#0 is also
  783. * looking at the same page) and the evictable page will be stranded
  784. * in an unevictable LRU.
  785. */
  786. smp_mb();
  787. if (page_evictable(page)) {
  788. lru = page_lru(page);
  789. update_page_reclaim_stat(lruvec, page_is_file_cache(page),
  790. PageActive(page));
  791. if (was_unevictable)
  792. count_vm_event(UNEVICTABLE_PGRESCUED);
  793. } else {
  794. lru = LRU_UNEVICTABLE;
  795. ClearPageActive(page);
  796. SetPageUnevictable(page);
  797. if (!was_unevictable)
  798. count_vm_event(UNEVICTABLE_PGCULLED);
  799. }
  800. add_page_to_lru_list(page, lruvec, lru);
  801. trace_mm_lru_insertion(page, lru);
  802. }
  803. /*
  804. * Add the passed pages to the LRU, then drop the caller's refcount
  805. * on them. Reinitialises the caller's pagevec.
  806. */
  807. void __pagevec_lru_add(struct pagevec *pvec)
  808. {
  809. pagevec_lru_move_fn(pvec, __pagevec_lru_add_fn, NULL);
  810. }
  811. EXPORT_SYMBOL(__pagevec_lru_add);
  812. /**
  813. * pagevec_lookup_entries - gang pagecache lookup
  814. * @pvec: Where the resulting entries are placed
  815. * @mapping: The address_space to search
  816. * @start: The starting entry index
  817. * @nr_entries: The maximum number of pages
  818. * @indices: The cache indices corresponding to the entries in @pvec
  819. *
  820. * pagevec_lookup_entries() will search for and return a group of up
  821. * to @nr_pages pages and shadow entries in the mapping. All
  822. * entries are placed in @pvec. pagevec_lookup_entries() takes a
  823. * reference against actual pages in @pvec.
  824. *
  825. * The search returns a group of mapping-contiguous entries with
  826. * ascending indexes. There may be holes in the indices due to
  827. * not-present entries.
  828. *
  829. * pagevec_lookup_entries() returns the number of entries which were
  830. * found.
  831. */
  832. unsigned pagevec_lookup_entries(struct pagevec *pvec,
  833. struct address_space *mapping,
  834. pgoff_t start, unsigned nr_entries,
  835. pgoff_t *indices)
  836. {
  837. pvec->nr = find_get_entries(mapping, start, nr_entries,
  838. pvec->pages, indices);
  839. return pagevec_count(pvec);
  840. }
  841. /**
  842. * pagevec_remove_exceptionals - pagevec exceptionals pruning
  843. * @pvec: The pagevec to prune
  844. *
  845. * pagevec_lookup_entries() fills both pages and exceptional radix
  846. * tree entries into the pagevec. This function prunes all
  847. * exceptionals from @pvec without leaving holes, so that it can be
  848. * passed on to page-only pagevec operations.
  849. */
  850. void pagevec_remove_exceptionals(struct pagevec *pvec)
  851. {
  852. int i, j;
  853. for (i = 0, j = 0; i < pagevec_count(pvec); i++) {
  854. struct page *page = pvec->pages[i];
  855. if (!radix_tree_exceptional_entry(page))
  856. pvec->pages[j++] = page;
  857. }
  858. pvec->nr = j;
  859. }
  860. /**
  861. * pagevec_lookup_range - gang pagecache lookup
  862. * @pvec: Where the resulting pages are placed
  863. * @mapping: The address_space to search
  864. * @start: The starting page index
  865. * @end: The final page index
  866. *
  867. * pagevec_lookup_range() will search for & return a group of up to PAGEVEC_SIZE
  868. * pages in the mapping starting from index @start and upto index @end
  869. * (inclusive). The pages are placed in @pvec. pagevec_lookup() takes a
  870. * reference against the pages in @pvec.
  871. *
  872. * The search returns a group of mapping-contiguous pages with ascending
  873. * indexes. There may be holes in the indices due to not-present pages. We
  874. * also update @start to index the next page for the traversal.
  875. *
  876. * pagevec_lookup_range() returns the number of pages which were found. If this
  877. * number is smaller than PAGEVEC_SIZE, the end of specified range has been
  878. * reached.
  879. */
  880. unsigned pagevec_lookup_range(struct pagevec *pvec,
  881. struct address_space *mapping, pgoff_t *start, pgoff_t end)
  882. {
  883. pvec->nr = find_get_pages_range(mapping, start, end, PAGEVEC_SIZE,
  884. pvec->pages);
  885. return pagevec_count(pvec);
  886. }
  887. EXPORT_SYMBOL(pagevec_lookup_range);
  888. unsigned pagevec_lookup_range_tag(struct pagevec *pvec,
  889. struct address_space *mapping, pgoff_t *index, pgoff_t end,
  890. int tag)
  891. {
  892. pvec->nr = find_get_pages_range_tag(mapping, index, end, tag,
  893. PAGEVEC_SIZE, pvec->pages);
  894. return pagevec_count(pvec);
  895. }
  896. EXPORT_SYMBOL(pagevec_lookup_range_tag);
  897. unsigned pagevec_lookup_range_nr_tag(struct pagevec *pvec,
  898. struct address_space *mapping, pgoff_t *index, pgoff_t end,
  899. int tag, unsigned max_pages)
  900. {
  901. pvec->nr = find_get_pages_range_tag(mapping, index, end, tag,
  902. min_t(unsigned int, max_pages, PAGEVEC_SIZE), pvec->pages);
  903. return pagevec_count(pvec);
  904. }
  905. EXPORT_SYMBOL(pagevec_lookup_range_nr_tag);
  906. /*
  907. * Perform any setup for the swap system
  908. */
  909. void __init swap_setup(void)
  910. {
  911. unsigned long megs = totalram_pages >> (20 - PAGE_SHIFT);
  912. /* Use a smaller cluster for small-memory machines */
  913. if (megs < 16)
  914. page_cluster = 2;
  915. else
  916. page_cluster = 3;
  917. /*
  918. * Right now other parts of the system means that we
  919. * _really_ don't want to cluster much more
  920. */
  921. }