swap.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162
  1. /*
  2. * linux/mm/swap.c
  3. *
  4. * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
  5. */
  6. /*
  7. * This file contains the default values for the operation of the
  8. * Linux VM subsystem. Fine-tuning documentation can be found in
  9. * Documentation/sysctl/vm.txt.
  10. * Started 18.12.91
  11. * Swap aging added 23.2.95, Stephen Tweedie.
  12. * Buffermem limits added 12.3.98, Rik van Riel.
  13. */
  14. #include <linux/mm.h>
  15. #include <linux/sched.h>
  16. #include <linux/kernel_stat.h>
  17. #include <linux/swap.h>
  18. #include <linux/mman.h>
  19. #include <linux/pagemap.h>
  20. #include <linux/pagevec.h>
  21. #include <linux/init.h>
  22. #include <linux/export.h>
  23. #include <linux/mm_inline.h>
  24. #include <linux/percpu_counter.h>
  25. #include <linux/percpu.h>
  26. #include <linux/cpu.h>
  27. #include <linux/notifier.h>
  28. #include <linux/backing-dev.h>
  29. #include <linux/memcontrol.h>
  30. #include <linux/gfp.h>
  31. #include <linux/uio.h>
  32. #include <linux/hugetlb.h>
  33. #include "internal.h"
  34. #define CREATE_TRACE_POINTS
  35. #include <trace/events/pagemap.h>
  36. /* How many pages do we try to swap or page in/out together? */
  37. int page_cluster;
  38. static DEFINE_PER_CPU(struct pagevec, lru_add_pvec);
  39. static DEFINE_PER_CPU(struct pagevec, lru_rotate_pvecs);
  40. static DEFINE_PER_CPU(struct pagevec, lru_deactivate_file_pvecs);
  41. /*
  42. * This path almost never happens for VM activity - pages are normally
  43. * freed via pagevecs. But it gets used by networking.
  44. */
  45. static void __page_cache_release(struct page *page)
  46. {
  47. if (PageLRU(page)) {
  48. struct zone *zone = page_zone(page);
  49. struct lruvec *lruvec;
  50. unsigned long flags;
  51. spin_lock_irqsave(&zone->lru_lock, flags);
  52. lruvec = mem_cgroup_page_lruvec(page, zone);
  53. VM_BUG_ON_PAGE(!PageLRU(page), page);
  54. __ClearPageLRU(page);
  55. del_page_from_lru_list(page, lruvec, page_off_lru(page));
  56. spin_unlock_irqrestore(&zone->lru_lock, flags);
  57. }
  58. mem_cgroup_uncharge(page);
  59. }
  60. static void __put_single_page(struct page *page)
  61. {
  62. __page_cache_release(page);
  63. free_hot_cold_page(page, false);
  64. }
  65. static void __put_compound_page(struct page *page)
  66. {
  67. compound_page_dtor *dtor;
  68. /*
  69. * __page_cache_release() is supposed to be called for thp, not for
  70. * hugetlb. This is because hugetlb page does never have PageLRU set
  71. * (it's never listed to any LRU lists) and no memcg routines should
  72. * be called for hugetlb (it has a separate hugetlb_cgroup.)
  73. */
  74. if (!PageHuge(page))
  75. __page_cache_release(page);
  76. dtor = get_compound_page_dtor(page);
  77. (*dtor)(page);
  78. }
  79. /**
  80. * Two special cases here: we could avoid taking compound_lock_irqsave
  81. * and could skip the tail refcounting(in _mapcount).
  82. *
  83. * 1. Hugetlbfs page:
  84. *
  85. * PageHeadHuge will remain true until the compound page
  86. * is released and enters the buddy allocator, and it could
  87. * not be split by __split_huge_page_refcount().
  88. *
  89. * So if we see PageHeadHuge set, and we have the tail page pin,
  90. * then we could safely put head page.
  91. *
  92. * 2. Slab THP page:
  93. *
  94. * PG_slab is cleared before the slab frees the head page, and
  95. * tail pin cannot be the last reference left on the head page,
  96. * because the slab code is free to reuse the compound page
  97. * after a kfree/kmem_cache_free without having to check if
  98. * there's any tail pin left. In turn all tail pinsmust be always
  99. * released while the head is still pinned by the slab code
  100. * and so we know PG_slab will be still set too.
  101. *
  102. * So if we see PageSlab set, and we have the tail page pin,
  103. * then we could safely put head page.
  104. */
  105. static __always_inline
  106. void put_unrefcounted_compound_page(struct page *page_head, struct page *page)
  107. {
  108. /*
  109. * If @page is a THP tail, we must read the tail page
  110. * flags after the head page flags. The
  111. * __split_huge_page_refcount side enforces write memory barriers
  112. * between clearing PageTail and before the head page
  113. * can be freed and reallocated.
  114. */
  115. smp_rmb();
  116. if (likely(PageTail(page))) {
  117. /*
  118. * __split_huge_page_refcount cannot race
  119. * here, see the comment above this function.
  120. */
  121. VM_BUG_ON_PAGE(!PageHead(page_head), page_head);
  122. VM_BUG_ON_PAGE(page_mapcount(page) != 0, page);
  123. if (put_page_testzero(page_head)) {
  124. /*
  125. * If this is the tail of a slab THP page,
  126. * the tail pin must not be the last reference
  127. * held on the page, because the PG_slab cannot
  128. * be cleared before all tail pins (which skips
  129. * the _mapcount tail refcounting) have been
  130. * released.
  131. *
  132. * If this is the tail of a hugetlbfs page,
  133. * the tail pin may be the last reference on
  134. * the page instead, because PageHeadHuge will
  135. * not go away until the compound page enters
  136. * the buddy allocator.
  137. */
  138. VM_BUG_ON_PAGE(PageSlab(page_head), page_head);
  139. __put_compound_page(page_head);
  140. }
  141. } else
  142. /*
  143. * __split_huge_page_refcount run before us,
  144. * @page was a THP tail. The split @page_head
  145. * has been freed and reallocated as slab or
  146. * hugetlbfs page of smaller order (only
  147. * possible if reallocated as slab on x86).
  148. */
  149. if (put_page_testzero(page))
  150. __put_single_page(page);
  151. }
  152. static __always_inline
  153. void put_refcounted_compound_page(struct page *page_head, struct page *page)
  154. {
  155. if (likely(page != page_head && get_page_unless_zero(page_head))) {
  156. unsigned long flags;
  157. /*
  158. * @page_head wasn't a dangling pointer but it may not
  159. * be a head page anymore by the time we obtain the
  160. * lock. That is ok as long as it can't be freed from
  161. * under us.
  162. */
  163. flags = compound_lock_irqsave(page_head);
  164. if (unlikely(!PageTail(page))) {
  165. /* __split_huge_page_refcount run before us */
  166. compound_unlock_irqrestore(page_head, flags);
  167. if (put_page_testzero(page_head)) {
  168. /*
  169. * The @page_head may have been freed
  170. * and reallocated as a compound page
  171. * of smaller order and then freed
  172. * again. All we know is that it
  173. * cannot have become: a THP page, a
  174. * compound page of higher order, a
  175. * tail page. That is because we
  176. * still hold the refcount of the
  177. * split THP tail and page_head was
  178. * the THP head before the split.
  179. */
  180. if (PageHead(page_head))
  181. __put_compound_page(page_head);
  182. else
  183. __put_single_page(page_head);
  184. }
  185. out_put_single:
  186. if (put_page_testzero(page))
  187. __put_single_page(page);
  188. return;
  189. }
  190. VM_BUG_ON_PAGE(page_head != page->first_page, page);
  191. /*
  192. * We can release the refcount taken by
  193. * get_page_unless_zero() now that
  194. * __split_huge_page_refcount() is blocked on the
  195. * compound_lock.
  196. */
  197. if (put_page_testzero(page_head))
  198. VM_BUG_ON_PAGE(1, page_head);
  199. /* __split_huge_page_refcount will wait now */
  200. VM_BUG_ON_PAGE(page_mapcount(page) <= 0, page);
  201. atomic_dec(&page->_mapcount);
  202. VM_BUG_ON_PAGE(atomic_read(&page_head->_count) <= 0, page_head);
  203. VM_BUG_ON_PAGE(atomic_read(&page->_count) != 0, page);
  204. compound_unlock_irqrestore(page_head, flags);
  205. if (put_page_testzero(page_head)) {
  206. if (PageHead(page_head))
  207. __put_compound_page(page_head);
  208. else
  209. __put_single_page(page_head);
  210. }
  211. } else {
  212. /* @page_head is a dangling pointer */
  213. VM_BUG_ON_PAGE(PageTail(page), page);
  214. goto out_put_single;
  215. }
  216. }
  217. static void put_compound_page(struct page *page)
  218. {
  219. struct page *page_head;
  220. /*
  221. * We see the PageCompound set and PageTail not set, so @page maybe:
  222. * 1. hugetlbfs head page, or
  223. * 2. THP head page.
  224. */
  225. if (likely(!PageTail(page))) {
  226. if (put_page_testzero(page)) {
  227. /*
  228. * By the time all refcounts have been released
  229. * split_huge_page cannot run anymore from under us.
  230. */
  231. if (PageHead(page))
  232. __put_compound_page(page);
  233. else
  234. __put_single_page(page);
  235. }
  236. return;
  237. }
  238. /*
  239. * We see the PageCompound set and PageTail set, so @page maybe:
  240. * 1. a tail hugetlbfs page, or
  241. * 2. a tail THP page, or
  242. * 3. a split THP page.
  243. *
  244. * Case 3 is possible, as we may race with
  245. * __split_huge_page_refcount tearing down a THP page.
  246. */
  247. page_head = compound_head_by_tail(page);
  248. if (!__compound_tail_refcounted(page_head))
  249. put_unrefcounted_compound_page(page_head, page);
  250. else
  251. put_refcounted_compound_page(page_head, page);
  252. }
  253. void put_page(struct page *page)
  254. {
  255. if (unlikely(PageCompound(page)))
  256. put_compound_page(page);
  257. else if (put_page_testzero(page))
  258. __put_single_page(page);
  259. }
  260. EXPORT_SYMBOL(put_page);
  261. /*
  262. * This function is exported but must not be called by anything other
  263. * than get_page(). It implements the slow path of get_page().
  264. */
  265. bool __get_page_tail(struct page *page)
  266. {
  267. /*
  268. * This takes care of get_page() if run on a tail page
  269. * returned by one of the get_user_pages/follow_page variants.
  270. * get_user_pages/follow_page itself doesn't need the compound
  271. * lock because it runs __get_page_tail_foll() under the
  272. * proper PT lock that already serializes against
  273. * split_huge_page().
  274. */
  275. unsigned long flags;
  276. bool got;
  277. struct page *page_head = compound_head(page);
  278. /* Ref to put_compound_page() comment. */
  279. if (!__compound_tail_refcounted(page_head)) {
  280. smp_rmb();
  281. if (likely(PageTail(page))) {
  282. /*
  283. * This is a hugetlbfs page or a slab
  284. * page. __split_huge_page_refcount
  285. * cannot race here.
  286. */
  287. VM_BUG_ON_PAGE(!PageHead(page_head), page_head);
  288. __get_page_tail_foll(page, true);
  289. return true;
  290. } else {
  291. /*
  292. * __split_huge_page_refcount run
  293. * before us, "page" was a THP
  294. * tail. The split page_head has been
  295. * freed and reallocated as slab or
  296. * hugetlbfs page of smaller order
  297. * (only possible if reallocated as
  298. * slab on x86).
  299. */
  300. return false;
  301. }
  302. }
  303. got = false;
  304. if (likely(page != page_head && get_page_unless_zero(page_head))) {
  305. /*
  306. * page_head wasn't a dangling pointer but it
  307. * may not be a head page anymore by the time
  308. * we obtain the lock. That is ok as long as it
  309. * can't be freed from under us.
  310. */
  311. flags = compound_lock_irqsave(page_head);
  312. /* here __split_huge_page_refcount won't run anymore */
  313. if (likely(PageTail(page))) {
  314. __get_page_tail_foll(page, false);
  315. got = true;
  316. }
  317. compound_unlock_irqrestore(page_head, flags);
  318. if (unlikely(!got))
  319. put_page(page_head);
  320. }
  321. return got;
  322. }
  323. EXPORT_SYMBOL(__get_page_tail);
  324. /**
  325. * put_pages_list() - release a list of pages
  326. * @pages: list of pages threaded on page->lru
  327. *
  328. * Release a list of pages which are strung together on page.lru. Currently
  329. * used by read_cache_pages() and related error recovery code.
  330. */
  331. void put_pages_list(struct list_head *pages)
  332. {
  333. while (!list_empty(pages)) {
  334. struct page *victim;
  335. victim = list_entry(pages->prev, struct page, lru);
  336. list_del(&victim->lru);
  337. page_cache_release(victim);
  338. }
  339. }
  340. EXPORT_SYMBOL(put_pages_list);
  341. /*
  342. * get_kernel_pages() - pin kernel pages in memory
  343. * @kiov: An array of struct kvec structures
  344. * @nr_segs: number of segments to pin
  345. * @write: pinning for read/write, currently ignored
  346. * @pages: array that receives pointers to the pages pinned.
  347. * Should be at least nr_segs long.
  348. *
  349. * Returns number of pages pinned. This may be fewer than the number
  350. * requested. If nr_pages is 0 or negative, returns 0. If no pages
  351. * were pinned, returns -errno. Each page returned must be released
  352. * with a put_page() call when it is finished with.
  353. */
  354. int get_kernel_pages(const struct kvec *kiov, int nr_segs, int write,
  355. struct page **pages)
  356. {
  357. int seg;
  358. for (seg = 0; seg < nr_segs; seg++) {
  359. if (WARN_ON(kiov[seg].iov_len != PAGE_SIZE))
  360. return seg;
  361. pages[seg] = kmap_to_page(kiov[seg].iov_base);
  362. page_cache_get(pages[seg]);
  363. }
  364. return seg;
  365. }
  366. EXPORT_SYMBOL_GPL(get_kernel_pages);
  367. /*
  368. * get_kernel_page() - pin a kernel page in memory
  369. * @start: starting kernel address
  370. * @write: pinning for read/write, currently ignored
  371. * @pages: array that receives pointer to the page pinned.
  372. * Must be at least nr_segs long.
  373. *
  374. * Returns 1 if page is pinned. If the page was not pinned, returns
  375. * -errno. The page returned must be released with a put_page() call
  376. * when it is finished with.
  377. */
  378. int get_kernel_page(unsigned long start, int write, struct page **pages)
  379. {
  380. const struct kvec kiov = {
  381. .iov_base = (void *)start,
  382. .iov_len = PAGE_SIZE
  383. };
  384. return get_kernel_pages(&kiov, 1, write, pages);
  385. }
  386. EXPORT_SYMBOL_GPL(get_kernel_page);
  387. static void pagevec_lru_move_fn(struct pagevec *pvec,
  388. void (*move_fn)(struct page *page, struct lruvec *lruvec, void *arg),
  389. void *arg)
  390. {
  391. int i;
  392. struct zone *zone = NULL;
  393. struct lruvec *lruvec;
  394. unsigned long flags = 0;
  395. for (i = 0; i < pagevec_count(pvec); i++) {
  396. struct page *page = pvec->pages[i];
  397. struct zone *pagezone = page_zone(page);
  398. if (pagezone != zone) {
  399. if (zone)
  400. spin_unlock_irqrestore(&zone->lru_lock, flags);
  401. zone = pagezone;
  402. spin_lock_irqsave(&zone->lru_lock, flags);
  403. }
  404. lruvec = mem_cgroup_page_lruvec(page, zone);
  405. (*move_fn)(page, lruvec, arg);
  406. }
  407. if (zone)
  408. spin_unlock_irqrestore(&zone->lru_lock, flags);
  409. release_pages(pvec->pages, pvec->nr, pvec->cold);
  410. pagevec_reinit(pvec);
  411. }
  412. static void pagevec_move_tail_fn(struct page *page, struct lruvec *lruvec,
  413. void *arg)
  414. {
  415. int *pgmoved = arg;
  416. if (PageLRU(page) && !PageActive(page) && !PageUnevictable(page)) {
  417. enum lru_list lru = page_lru_base_type(page);
  418. list_move_tail(&page->lru, &lruvec->lists[lru]);
  419. (*pgmoved)++;
  420. }
  421. }
  422. /*
  423. * pagevec_move_tail() must be called with IRQ disabled.
  424. * Otherwise this may cause nasty races.
  425. */
  426. static void pagevec_move_tail(struct pagevec *pvec)
  427. {
  428. int pgmoved = 0;
  429. pagevec_lru_move_fn(pvec, pagevec_move_tail_fn, &pgmoved);
  430. __count_vm_events(PGROTATED, pgmoved);
  431. }
  432. /*
  433. * Writeback is about to end against a page which has been marked for immediate
  434. * reclaim. If it still appears to be reclaimable, move it to the tail of the
  435. * inactive list.
  436. */
  437. void rotate_reclaimable_page(struct page *page)
  438. {
  439. if (!PageLocked(page) && !PageDirty(page) && !PageActive(page) &&
  440. !PageUnevictable(page) && PageLRU(page)) {
  441. struct pagevec *pvec;
  442. unsigned long flags;
  443. page_cache_get(page);
  444. local_irq_save(flags);
  445. pvec = this_cpu_ptr(&lru_rotate_pvecs);
  446. if (!pagevec_add(pvec, page))
  447. pagevec_move_tail(pvec);
  448. local_irq_restore(flags);
  449. }
  450. }
  451. static void update_page_reclaim_stat(struct lruvec *lruvec,
  452. int file, int rotated)
  453. {
  454. struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat;
  455. reclaim_stat->recent_scanned[file]++;
  456. if (rotated)
  457. reclaim_stat->recent_rotated[file]++;
  458. }
  459. static void __activate_page(struct page *page, struct lruvec *lruvec,
  460. void *arg)
  461. {
  462. if (PageLRU(page) && !PageActive(page) && !PageUnevictable(page)) {
  463. int file = page_is_file_cache(page);
  464. int lru = page_lru_base_type(page);
  465. del_page_from_lru_list(page, lruvec, lru);
  466. SetPageActive(page);
  467. lru += LRU_ACTIVE;
  468. add_page_to_lru_list(page, lruvec, lru);
  469. trace_mm_lru_activate(page);
  470. __count_vm_event(PGACTIVATE);
  471. update_page_reclaim_stat(lruvec, file, 1);
  472. }
  473. }
  474. #ifdef CONFIG_SMP
  475. static DEFINE_PER_CPU(struct pagevec, activate_page_pvecs);
  476. static void activate_page_drain(int cpu)
  477. {
  478. struct pagevec *pvec = &per_cpu(activate_page_pvecs, cpu);
  479. if (pagevec_count(pvec))
  480. pagevec_lru_move_fn(pvec, __activate_page, NULL);
  481. }
  482. static bool need_activate_page_drain(int cpu)
  483. {
  484. return pagevec_count(&per_cpu(activate_page_pvecs, cpu)) != 0;
  485. }
  486. void activate_page(struct page *page)
  487. {
  488. if (PageLRU(page) && !PageActive(page) && !PageUnevictable(page)) {
  489. struct pagevec *pvec = &get_cpu_var(activate_page_pvecs);
  490. page_cache_get(page);
  491. if (!pagevec_add(pvec, page))
  492. pagevec_lru_move_fn(pvec, __activate_page, NULL);
  493. put_cpu_var(activate_page_pvecs);
  494. }
  495. }
  496. #else
  497. static inline void activate_page_drain(int cpu)
  498. {
  499. }
  500. static bool need_activate_page_drain(int cpu)
  501. {
  502. return false;
  503. }
  504. void activate_page(struct page *page)
  505. {
  506. struct zone *zone = page_zone(page);
  507. spin_lock_irq(&zone->lru_lock);
  508. __activate_page(page, mem_cgroup_page_lruvec(page, zone), NULL);
  509. spin_unlock_irq(&zone->lru_lock);
  510. }
  511. #endif
  512. static void __lru_cache_activate_page(struct page *page)
  513. {
  514. struct pagevec *pvec = &get_cpu_var(lru_add_pvec);
  515. int i;
  516. /*
  517. * Search backwards on the optimistic assumption that the page being
  518. * activated has just been added to this pagevec. Note that only
  519. * the local pagevec is examined as a !PageLRU page could be in the
  520. * process of being released, reclaimed, migrated or on a remote
  521. * pagevec that is currently being drained. Furthermore, marking
  522. * a remote pagevec's page PageActive potentially hits a race where
  523. * a page is marked PageActive just after it is added to the inactive
  524. * list causing accounting errors and BUG_ON checks to trigger.
  525. */
  526. for (i = pagevec_count(pvec) - 1; i >= 0; i--) {
  527. struct page *pagevec_page = pvec->pages[i];
  528. if (pagevec_page == page) {
  529. SetPageActive(page);
  530. break;
  531. }
  532. }
  533. put_cpu_var(lru_add_pvec);
  534. }
  535. /*
  536. * Mark a page as having seen activity.
  537. *
  538. * inactive,unreferenced -> inactive,referenced
  539. * inactive,referenced -> active,unreferenced
  540. * active,unreferenced -> active,referenced
  541. *
  542. * When a newly allocated page is not yet visible, so safe for non-atomic ops,
  543. * __SetPageReferenced(page) may be substituted for mark_page_accessed(page).
  544. */
  545. void mark_page_accessed(struct page *page)
  546. {
  547. if (!PageActive(page) && !PageUnevictable(page) &&
  548. PageReferenced(page)) {
  549. /*
  550. * If the page is on the LRU, queue it for activation via
  551. * activate_page_pvecs. Otherwise, assume the page is on a
  552. * pagevec, mark it active and it'll be moved to the active
  553. * LRU on the next drain.
  554. */
  555. if (PageLRU(page))
  556. activate_page(page);
  557. else
  558. __lru_cache_activate_page(page);
  559. ClearPageReferenced(page);
  560. if (page_is_file_cache(page))
  561. workingset_activation(page);
  562. } else if (!PageReferenced(page)) {
  563. SetPageReferenced(page);
  564. }
  565. }
  566. EXPORT_SYMBOL(mark_page_accessed);
  567. static void __lru_cache_add(struct page *page)
  568. {
  569. struct pagevec *pvec = &get_cpu_var(lru_add_pvec);
  570. page_cache_get(page);
  571. if (!pagevec_space(pvec))
  572. __pagevec_lru_add(pvec);
  573. pagevec_add(pvec, page);
  574. put_cpu_var(lru_add_pvec);
  575. }
  576. /**
  577. * lru_cache_add: add a page to the page lists
  578. * @page: the page to add
  579. */
  580. void lru_cache_add_anon(struct page *page)
  581. {
  582. if (PageActive(page))
  583. ClearPageActive(page);
  584. __lru_cache_add(page);
  585. }
  586. void lru_cache_add_file(struct page *page)
  587. {
  588. if (PageActive(page))
  589. ClearPageActive(page);
  590. __lru_cache_add(page);
  591. }
  592. EXPORT_SYMBOL(lru_cache_add_file);
  593. /**
  594. * lru_cache_add - add a page to a page list
  595. * @page: the page to be added to the LRU.
  596. *
  597. * Queue the page for addition to the LRU via pagevec. The decision on whether
  598. * to add the page to the [in]active [file|anon] list is deferred until the
  599. * pagevec is drained. This gives a chance for the caller of lru_cache_add()
  600. * have the page added to the active list using mark_page_accessed().
  601. */
  602. void lru_cache_add(struct page *page)
  603. {
  604. VM_BUG_ON_PAGE(PageActive(page) && PageUnevictable(page), page);
  605. VM_BUG_ON_PAGE(PageLRU(page), page);
  606. __lru_cache_add(page);
  607. }
  608. /**
  609. * add_page_to_unevictable_list - add a page to the unevictable list
  610. * @page: the page to be added to the unevictable list
  611. *
  612. * Add page directly to its zone's unevictable list. To avoid races with
  613. * tasks that might be making the page evictable, through eg. munlock,
  614. * munmap or exit, while it's not on the lru, we want to add the page
  615. * while it's locked or otherwise "invisible" to other tasks. This is
  616. * difficult to do when using the pagevec cache, so bypass that.
  617. */
  618. void add_page_to_unevictable_list(struct page *page)
  619. {
  620. struct zone *zone = page_zone(page);
  621. struct lruvec *lruvec;
  622. spin_lock_irq(&zone->lru_lock);
  623. lruvec = mem_cgroup_page_lruvec(page, zone);
  624. ClearPageActive(page);
  625. SetPageUnevictable(page);
  626. SetPageLRU(page);
  627. add_page_to_lru_list(page, lruvec, LRU_UNEVICTABLE);
  628. spin_unlock_irq(&zone->lru_lock);
  629. }
  630. /**
  631. * lru_cache_add_active_or_unevictable
  632. * @page: the page to be added to LRU
  633. * @vma: vma in which page is mapped for determining reclaimability
  634. *
  635. * Place @page on the active or unevictable LRU list, depending on its
  636. * evictability. Note that if the page is not evictable, it goes
  637. * directly back onto it's zone's unevictable list, it does NOT use a
  638. * per cpu pagevec.
  639. */
  640. void lru_cache_add_active_or_unevictable(struct page *page,
  641. struct vm_area_struct *vma)
  642. {
  643. VM_BUG_ON_PAGE(PageLRU(page), page);
  644. if (likely((vma->vm_flags & (VM_LOCKED | VM_SPECIAL)) != VM_LOCKED)) {
  645. SetPageActive(page);
  646. lru_cache_add(page);
  647. return;
  648. }
  649. if (!TestSetPageMlocked(page)) {
  650. /*
  651. * We use the irq-unsafe __mod_zone_page_stat because this
  652. * counter is not modified from interrupt context, and the pte
  653. * lock is held(spinlock), which implies preemption disabled.
  654. */
  655. __mod_zone_page_state(page_zone(page), NR_MLOCK,
  656. hpage_nr_pages(page));
  657. count_vm_event(UNEVICTABLE_PGMLOCKED);
  658. }
  659. add_page_to_unevictable_list(page);
  660. }
  661. /*
  662. * If the page can not be invalidated, it is moved to the
  663. * inactive list to speed up its reclaim. It is moved to the
  664. * head of the list, rather than the tail, to give the flusher
  665. * threads some time to write it out, as this is much more
  666. * effective than the single-page writeout from reclaim.
  667. *
  668. * If the page isn't page_mapped and dirty/writeback, the page
  669. * could reclaim asap using PG_reclaim.
  670. *
  671. * 1. active, mapped page -> none
  672. * 2. active, dirty/writeback page -> inactive, head, PG_reclaim
  673. * 3. inactive, mapped page -> none
  674. * 4. inactive, dirty/writeback page -> inactive, head, PG_reclaim
  675. * 5. inactive, clean -> inactive, tail
  676. * 6. Others -> none
  677. *
  678. * In 4, why it moves inactive's head, the VM expects the page would
  679. * be write it out by flusher threads as this is much more effective
  680. * than the single-page writeout from reclaim.
  681. */
  682. static void lru_deactivate_file_fn(struct page *page, struct lruvec *lruvec,
  683. void *arg)
  684. {
  685. int lru, file;
  686. bool active;
  687. if (!PageLRU(page))
  688. return;
  689. if (PageUnevictable(page))
  690. return;
  691. /* Some processes are using the page */
  692. if (page_mapped(page))
  693. return;
  694. active = PageActive(page);
  695. file = page_is_file_cache(page);
  696. lru = page_lru_base_type(page);
  697. del_page_from_lru_list(page, lruvec, lru + active);
  698. ClearPageActive(page);
  699. ClearPageReferenced(page);
  700. add_page_to_lru_list(page, lruvec, lru);
  701. if (PageWriteback(page) || PageDirty(page)) {
  702. /*
  703. * PG_reclaim could be raced with end_page_writeback
  704. * It can make readahead confusing. But race window
  705. * is _really_ small and it's non-critical problem.
  706. */
  707. SetPageReclaim(page);
  708. } else {
  709. /*
  710. * The page's writeback ends up during pagevec
  711. * We moves tha page into tail of inactive.
  712. */
  713. list_move_tail(&page->lru, &lruvec->lists[lru]);
  714. __count_vm_event(PGROTATED);
  715. }
  716. if (active)
  717. __count_vm_event(PGDEACTIVATE);
  718. update_page_reclaim_stat(lruvec, file, 0);
  719. }
  720. /*
  721. * Drain pages out of the cpu's pagevecs.
  722. * Either "cpu" is the current CPU, and preemption has already been
  723. * disabled; or "cpu" is being hot-unplugged, and is already dead.
  724. */
  725. void lru_add_drain_cpu(int cpu)
  726. {
  727. struct pagevec *pvec = &per_cpu(lru_add_pvec, cpu);
  728. if (pagevec_count(pvec))
  729. __pagevec_lru_add(pvec);
  730. pvec = &per_cpu(lru_rotate_pvecs, cpu);
  731. if (pagevec_count(pvec)) {
  732. unsigned long flags;
  733. /* No harm done if a racing interrupt already did this */
  734. local_irq_save(flags);
  735. pagevec_move_tail(pvec);
  736. local_irq_restore(flags);
  737. }
  738. pvec = &per_cpu(lru_deactivate_file_pvecs, cpu);
  739. if (pagevec_count(pvec))
  740. pagevec_lru_move_fn(pvec, lru_deactivate_file_fn, NULL);
  741. activate_page_drain(cpu);
  742. }
  743. /**
  744. * deactivate_file_page - forcefully deactivate a file page
  745. * @page: page to deactivate
  746. *
  747. * This function hints the VM that @page is a good reclaim candidate,
  748. * for example if its invalidation fails due to the page being dirty
  749. * or under writeback.
  750. */
  751. void deactivate_file_page(struct page *page)
  752. {
  753. /*
  754. * In a workload with many unevictable page such as mprotect,
  755. * unevictable page deactivation for accelerating reclaim is pointless.
  756. */
  757. if (PageUnevictable(page))
  758. return;
  759. if (likely(get_page_unless_zero(page))) {
  760. struct pagevec *pvec = &get_cpu_var(lru_deactivate_file_pvecs);
  761. if (!pagevec_add(pvec, page))
  762. pagevec_lru_move_fn(pvec, lru_deactivate_file_fn, NULL);
  763. put_cpu_var(lru_deactivate_file_pvecs);
  764. }
  765. }
  766. void lru_add_drain(void)
  767. {
  768. lru_add_drain_cpu(get_cpu());
  769. put_cpu();
  770. }
  771. static void lru_add_drain_per_cpu(struct work_struct *dummy)
  772. {
  773. lru_add_drain();
  774. }
  775. static DEFINE_PER_CPU(struct work_struct, lru_add_drain_work);
  776. void lru_add_drain_all(void)
  777. {
  778. static DEFINE_MUTEX(lock);
  779. static struct cpumask has_work;
  780. int cpu;
  781. mutex_lock(&lock);
  782. get_online_cpus();
  783. cpumask_clear(&has_work);
  784. for_each_online_cpu(cpu) {
  785. struct work_struct *work = &per_cpu(lru_add_drain_work, cpu);
  786. if (pagevec_count(&per_cpu(lru_add_pvec, cpu)) ||
  787. pagevec_count(&per_cpu(lru_rotate_pvecs, cpu)) ||
  788. pagevec_count(&per_cpu(lru_deactivate_file_pvecs, cpu)) ||
  789. need_activate_page_drain(cpu)) {
  790. INIT_WORK(work, lru_add_drain_per_cpu);
  791. schedule_work_on(cpu, work);
  792. cpumask_set_cpu(cpu, &has_work);
  793. }
  794. }
  795. for_each_cpu(cpu, &has_work)
  796. flush_work(&per_cpu(lru_add_drain_work, cpu));
  797. put_online_cpus();
  798. mutex_unlock(&lock);
  799. }
  800. /**
  801. * release_pages - batched page_cache_release()
  802. * @pages: array of pages to release
  803. * @nr: number of pages
  804. * @cold: whether the pages are cache cold
  805. *
  806. * Decrement the reference count on all the pages in @pages. If it
  807. * fell to zero, remove the page from the LRU and free it.
  808. */
  809. void release_pages(struct page **pages, int nr, bool cold)
  810. {
  811. int i;
  812. LIST_HEAD(pages_to_free);
  813. struct zone *zone = NULL;
  814. struct lruvec *lruvec;
  815. unsigned long uninitialized_var(flags);
  816. unsigned int uninitialized_var(lock_batch);
  817. for (i = 0; i < nr; i++) {
  818. struct page *page = pages[i];
  819. if (unlikely(PageCompound(page))) {
  820. if (zone) {
  821. spin_unlock_irqrestore(&zone->lru_lock, flags);
  822. zone = NULL;
  823. }
  824. put_compound_page(page);
  825. continue;
  826. }
  827. /*
  828. * Make sure the IRQ-safe lock-holding time does not get
  829. * excessive with a continuous string of pages from the
  830. * same zone. The lock is held only if zone != NULL.
  831. */
  832. if (zone && ++lock_batch == SWAP_CLUSTER_MAX) {
  833. spin_unlock_irqrestore(&zone->lru_lock, flags);
  834. zone = NULL;
  835. }
  836. if (!put_page_testzero(page))
  837. continue;
  838. if (PageLRU(page)) {
  839. struct zone *pagezone = page_zone(page);
  840. if (pagezone != zone) {
  841. if (zone)
  842. spin_unlock_irqrestore(&zone->lru_lock,
  843. flags);
  844. lock_batch = 0;
  845. zone = pagezone;
  846. spin_lock_irqsave(&zone->lru_lock, flags);
  847. }
  848. lruvec = mem_cgroup_page_lruvec(page, zone);
  849. VM_BUG_ON_PAGE(!PageLRU(page), page);
  850. __ClearPageLRU(page);
  851. del_page_from_lru_list(page, lruvec, page_off_lru(page));
  852. }
  853. /* Clear Active bit in case of parallel mark_page_accessed */
  854. __ClearPageActive(page);
  855. list_add(&page->lru, &pages_to_free);
  856. }
  857. if (zone)
  858. spin_unlock_irqrestore(&zone->lru_lock, flags);
  859. mem_cgroup_uncharge_list(&pages_to_free);
  860. free_hot_cold_page_list(&pages_to_free, cold);
  861. }
  862. EXPORT_SYMBOL(release_pages);
  863. /*
  864. * The pages which we're about to release may be in the deferred lru-addition
  865. * queues. That would prevent them from really being freed right now. That's
  866. * OK from a correctness point of view but is inefficient - those pages may be
  867. * cache-warm and we want to give them back to the page allocator ASAP.
  868. *
  869. * So __pagevec_release() will drain those queues here. __pagevec_lru_add()
  870. * and __pagevec_lru_add_active() call release_pages() directly to avoid
  871. * mutual recursion.
  872. */
  873. void __pagevec_release(struct pagevec *pvec)
  874. {
  875. lru_add_drain();
  876. release_pages(pvec->pages, pagevec_count(pvec), pvec->cold);
  877. pagevec_reinit(pvec);
  878. }
  879. EXPORT_SYMBOL(__pagevec_release);
  880. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  881. /* used by __split_huge_page_refcount() */
  882. void lru_add_page_tail(struct page *page, struct page *page_tail,
  883. struct lruvec *lruvec, struct list_head *list)
  884. {
  885. const int file = 0;
  886. VM_BUG_ON_PAGE(!PageHead(page), page);
  887. VM_BUG_ON_PAGE(PageCompound(page_tail), page);
  888. VM_BUG_ON_PAGE(PageLRU(page_tail), page);
  889. VM_BUG_ON(NR_CPUS != 1 &&
  890. !spin_is_locked(&lruvec_zone(lruvec)->lru_lock));
  891. if (!list)
  892. SetPageLRU(page_tail);
  893. if (likely(PageLRU(page)))
  894. list_add_tail(&page_tail->lru, &page->lru);
  895. else if (list) {
  896. /* page reclaim is reclaiming a huge page */
  897. get_page(page_tail);
  898. list_add_tail(&page_tail->lru, list);
  899. } else {
  900. struct list_head *list_head;
  901. /*
  902. * Head page has not yet been counted, as an hpage,
  903. * so we must account for each subpage individually.
  904. *
  905. * Use the standard add function to put page_tail on the list,
  906. * but then correct its position so they all end up in order.
  907. */
  908. add_page_to_lru_list(page_tail, lruvec, page_lru(page_tail));
  909. list_head = page_tail->lru.prev;
  910. list_move_tail(&page_tail->lru, list_head);
  911. }
  912. if (!PageUnevictable(page))
  913. update_page_reclaim_stat(lruvec, file, PageActive(page_tail));
  914. }
  915. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  916. static void __pagevec_lru_add_fn(struct page *page, struct lruvec *lruvec,
  917. void *arg)
  918. {
  919. int file = page_is_file_cache(page);
  920. int active = PageActive(page);
  921. enum lru_list lru = page_lru(page);
  922. VM_BUG_ON_PAGE(PageLRU(page), page);
  923. SetPageLRU(page);
  924. add_page_to_lru_list(page, lruvec, lru);
  925. update_page_reclaim_stat(lruvec, file, active);
  926. trace_mm_lru_insertion(page, lru);
  927. }
  928. /*
  929. * Add the passed pages to the LRU, then drop the caller's refcount
  930. * on them. Reinitialises the caller's pagevec.
  931. */
  932. void __pagevec_lru_add(struct pagevec *pvec)
  933. {
  934. pagevec_lru_move_fn(pvec, __pagevec_lru_add_fn, NULL);
  935. }
  936. EXPORT_SYMBOL(__pagevec_lru_add);
  937. /**
  938. * pagevec_lookup_entries - gang pagecache lookup
  939. * @pvec: Where the resulting entries are placed
  940. * @mapping: The address_space to search
  941. * @start: The starting entry index
  942. * @nr_entries: The maximum number of entries
  943. * @indices: The cache indices corresponding to the entries in @pvec
  944. *
  945. * pagevec_lookup_entries() will search for and return a group of up
  946. * to @nr_entries pages and shadow entries in the mapping. All
  947. * entries are placed in @pvec. pagevec_lookup_entries() takes a
  948. * reference against actual pages in @pvec.
  949. *
  950. * The search returns a group of mapping-contiguous entries with
  951. * ascending indexes. There may be holes in the indices due to
  952. * not-present entries.
  953. *
  954. * pagevec_lookup_entries() returns the number of entries which were
  955. * found.
  956. */
  957. unsigned pagevec_lookup_entries(struct pagevec *pvec,
  958. struct address_space *mapping,
  959. pgoff_t start, unsigned nr_pages,
  960. pgoff_t *indices)
  961. {
  962. pvec->nr = find_get_entries(mapping, start, nr_pages,
  963. pvec->pages, indices);
  964. return pagevec_count(pvec);
  965. }
  966. /**
  967. * pagevec_remove_exceptionals - pagevec exceptionals pruning
  968. * @pvec: The pagevec to prune
  969. *
  970. * pagevec_lookup_entries() fills both pages and exceptional radix
  971. * tree entries into the pagevec. This function prunes all
  972. * exceptionals from @pvec without leaving holes, so that it can be
  973. * passed on to page-only pagevec operations.
  974. */
  975. void pagevec_remove_exceptionals(struct pagevec *pvec)
  976. {
  977. int i, j;
  978. for (i = 0, j = 0; i < pagevec_count(pvec); i++) {
  979. struct page *page = pvec->pages[i];
  980. if (!radix_tree_exceptional_entry(page))
  981. pvec->pages[j++] = page;
  982. }
  983. pvec->nr = j;
  984. }
  985. /**
  986. * pagevec_lookup - gang pagecache lookup
  987. * @pvec: Where the resulting pages are placed
  988. * @mapping: The address_space to search
  989. * @start: The starting page index
  990. * @nr_pages: The maximum number of pages
  991. *
  992. * pagevec_lookup() will search for and return a group of up to @nr_pages pages
  993. * in the mapping. The pages are placed in @pvec. pagevec_lookup() takes a
  994. * reference against the pages in @pvec.
  995. *
  996. * The search returns a group of mapping-contiguous pages with ascending
  997. * indexes. There may be holes in the indices due to not-present pages.
  998. *
  999. * pagevec_lookup() returns the number of pages which were found.
  1000. */
  1001. unsigned pagevec_lookup(struct pagevec *pvec, struct address_space *mapping,
  1002. pgoff_t start, unsigned nr_pages)
  1003. {
  1004. pvec->nr = find_get_pages(mapping, start, nr_pages, pvec->pages);
  1005. return pagevec_count(pvec);
  1006. }
  1007. EXPORT_SYMBOL(pagevec_lookup);
  1008. unsigned pagevec_lookup_tag(struct pagevec *pvec, struct address_space *mapping,
  1009. pgoff_t *index, int tag, unsigned nr_pages)
  1010. {
  1011. pvec->nr = find_get_pages_tag(mapping, index, tag,
  1012. nr_pages, pvec->pages);
  1013. return pagevec_count(pvec);
  1014. }
  1015. EXPORT_SYMBOL(pagevec_lookup_tag);
  1016. /*
  1017. * Perform any setup for the swap system
  1018. */
  1019. void __init swap_setup(void)
  1020. {
  1021. unsigned long megs = totalram_pages >> (20 - PAGE_SHIFT);
  1022. #ifdef CONFIG_SWAP
  1023. int i;
  1024. for (i = 0; i < MAX_SWAPFILES; i++)
  1025. spin_lock_init(&swapper_spaces[i].tree_lock);
  1026. #endif
  1027. /* Use a smaller cluster for small-memory machines */
  1028. if (megs < 16)
  1029. page_cluster = 2;
  1030. else
  1031. page_cluster = 3;
  1032. /*
  1033. * Right now other parts of the system means that we
  1034. * _really_ don't want to cluster much more
  1035. */
  1036. }