swap.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164
  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 <linux/page_idle.h>
  34. #include "internal.h"
  35. #define CREATE_TRACE_POINTS
  36. #include <trace/events/pagemap.h>
  37. /* How many pages do we try to swap or page in/out together? */
  38. int page_cluster;
  39. static DEFINE_PER_CPU(struct pagevec, lru_add_pvec);
  40. static DEFINE_PER_CPU(struct pagevec, lru_rotate_pvecs);
  41. static DEFINE_PER_CPU(struct pagevec, lru_deactivate_file_pvecs);
  42. /*
  43. * This path almost never happens for VM activity - pages are normally
  44. * freed via pagevecs. But it gets used by networking.
  45. */
  46. static void __page_cache_release(struct page *page)
  47. {
  48. if (PageLRU(page)) {
  49. struct zone *zone = page_zone(page);
  50. struct lruvec *lruvec;
  51. unsigned long flags;
  52. spin_lock_irqsave(&zone->lru_lock, flags);
  53. lruvec = mem_cgroup_page_lruvec(page, zone);
  54. VM_BUG_ON_PAGE(!PageLRU(page), page);
  55. __ClearPageLRU(page);
  56. del_page_from_lru_list(page, lruvec, page_off_lru(page));
  57. spin_unlock_irqrestore(&zone->lru_lock, flags);
  58. }
  59. mem_cgroup_uncharge(page);
  60. }
  61. static void __put_single_page(struct page *page)
  62. {
  63. __page_cache_release(page);
  64. free_hot_cold_page(page, false);
  65. }
  66. static void __put_compound_page(struct page *page)
  67. {
  68. compound_page_dtor *dtor;
  69. /*
  70. * __page_cache_release() is supposed to be called for thp, not for
  71. * hugetlb. This is because hugetlb page does never have PageLRU set
  72. * (it's never listed to any LRU lists) and no memcg routines should
  73. * be called for hugetlb (it has a separate hugetlb_cgroup.)
  74. */
  75. if (!PageHuge(page))
  76. __page_cache_release(page);
  77. dtor = get_compound_page_dtor(page);
  78. (*dtor)(page);
  79. }
  80. /**
  81. * Two special cases here: we could avoid taking compound_lock_irqsave
  82. * and could skip the tail refcounting(in _mapcount).
  83. *
  84. * 1. Hugetlbfs page:
  85. *
  86. * PageHeadHuge will remain true until the compound page
  87. * is released and enters the buddy allocator, and it could
  88. * not be split by __split_huge_page_refcount().
  89. *
  90. * So if we see PageHeadHuge set, and we have the tail page pin,
  91. * then we could safely put head page.
  92. *
  93. * 2. Slab THP page:
  94. *
  95. * PG_slab is cleared before the slab frees the head page, and
  96. * tail pin cannot be the last reference left on the head page,
  97. * because the slab code is free to reuse the compound page
  98. * after a kfree/kmem_cache_free without having to check if
  99. * there's any tail pin left. In turn all tail pinsmust be always
  100. * released while the head is still pinned by the slab code
  101. * and so we know PG_slab will be still set too.
  102. *
  103. * So if we see PageSlab set, and we have the tail page pin,
  104. * then we could safely put head page.
  105. */
  106. static __always_inline
  107. void put_unrefcounted_compound_page(struct page *page_head, struct page *page)
  108. {
  109. /*
  110. * If @page is a THP tail, we must read the tail page
  111. * flags after the head page flags. The
  112. * __split_huge_page_refcount side enforces write memory barriers
  113. * between clearing PageTail and before the head page
  114. * can be freed and reallocated.
  115. */
  116. smp_rmb();
  117. if (likely(PageTail(page))) {
  118. /*
  119. * __split_huge_page_refcount cannot race
  120. * here, see the comment above this function.
  121. */
  122. VM_BUG_ON_PAGE(!PageHead(page_head), page_head);
  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 != compound_head(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(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. if (page_is_idle(page))
  566. clear_page_idle(page);
  567. }
  568. EXPORT_SYMBOL(mark_page_accessed);
  569. static void __lru_cache_add(struct page *page)
  570. {
  571. struct pagevec *pvec = &get_cpu_var(lru_add_pvec);
  572. page_cache_get(page);
  573. if (!pagevec_space(pvec))
  574. __pagevec_lru_add(pvec);
  575. pagevec_add(pvec, page);
  576. put_cpu_var(lru_add_pvec);
  577. }
  578. /**
  579. * lru_cache_add: add a page to the page lists
  580. * @page: the page to add
  581. */
  582. void lru_cache_add_anon(struct page *page)
  583. {
  584. if (PageActive(page))
  585. ClearPageActive(page);
  586. __lru_cache_add(page);
  587. }
  588. void lru_cache_add_file(struct page *page)
  589. {
  590. if (PageActive(page))
  591. ClearPageActive(page);
  592. __lru_cache_add(page);
  593. }
  594. EXPORT_SYMBOL(lru_cache_add_file);
  595. /**
  596. * lru_cache_add - add a page to a page list
  597. * @page: the page to be added to the LRU.
  598. *
  599. * Queue the page for addition to the LRU via pagevec. The decision on whether
  600. * to add the page to the [in]active [file|anon] list is deferred until the
  601. * pagevec is drained. This gives a chance for the caller of lru_cache_add()
  602. * have the page added to the active list using mark_page_accessed().
  603. */
  604. void lru_cache_add(struct page *page)
  605. {
  606. VM_BUG_ON_PAGE(PageActive(page) && PageUnevictable(page), page);
  607. VM_BUG_ON_PAGE(PageLRU(page), page);
  608. __lru_cache_add(page);
  609. }
  610. /**
  611. * add_page_to_unevictable_list - add a page to the unevictable list
  612. * @page: the page to be added to the unevictable list
  613. *
  614. * Add page directly to its zone's unevictable list. To avoid races with
  615. * tasks that might be making the page evictable, through eg. munlock,
  616. * munmap or exit, while it's not on the lru, we want to add the page
  617. * while it's locked or otherwise "invisible" to other tasks. This is
  618. * difficult to do when using the pagevec cache, so bypass that.
  619. */
  620. void add_page_to_unevictable_list(struct page *page)
  621. {
  622. struct zone *zone = page_zone(page);
  623. struct lruvec *lruvec;
  624. spin_lock_irq(&zone->lru_lock);
  625. lruvec = mem_cgroup_page_lruvec(page, zone);
  626. ClearPageActive(page);
  627. SetPageUnevictable(page);
  628. SetPageLRU(page);
  629. add_page_to_lru_list(page, lruvec, LRU_UNEVICTABLE);
  630. spin_unlock_irq(&zone->lru_lock);
  631. }
  632. /**
  633. * lru_cache_add_active_or_unevictable
  634. * @page: the page to be added to LRU
  635. * @vma: vma in which page is mapped for determining reclaimability
  636. *
  637. * Place @page on the active or unevictable LRU list, depending on its
  638. * evictability. Note that if the page is not evictable, it goes
  639. * directly back onto it's zone's unevictable list, it does NOT use a
  640. * per cpu pagevec.
  641. */
  642. void lru_cache_add_active_or_unevictable(struct page *page,
  643. struct vm_area_struct *vma)
  644. {
  645. VM_BUG_ON_PAGE(PageLRU(page), page);
  646. if (likely((vma->vm_flags & (VM_LOCKED | VM_SPECIAL)) != VM_LOCKED)) {
  647. SetPageActive(page);
  648. lru_cache_add(page);
  649. return;
  650. }
  651. if (!TestSetPageMlocked(page)) {
  652. /*
  653. * We use the irq-unsafe __mod_zone_page_stat because this
  654. * counter is not modified from interrupt context, and the pte
  655. * lock is held(spinlock), which implies preemption disabled.
  656. */
  657. __mod_zone_page_state(page_zone(page), NR_MLOCK,
  658. hpage_nr_pages(page));
  659. count_vm_event(UNEVICTABLE_PGMLOCKED);
  660. }
  661. add_page_to_unevictable_list(page);
  662. }
  663. /*
  664. * If the page can not be invalidated, it is moved to the
  665. * inactive list to speed up its reclaim. It is moved to the
  666. * head of the list, rather than the tail, to give the flusher
  667. * threads some time to write it out, as this is much more
  668. * effective than the single-page writeout from reclaim.
  669. *
  670. * If the page isn't page_mapped and dirty/writeback, the page
  671. * could reclaim asap using PG_reclaim.
  672. *
  673. * 1. active, mapped page -> none
  674. * 2. active, dirty/writeback page -> inactive, head, PG_reclaim
  675. * 3. inactive, mapped page -> none
  676. * 4. inactive, dirty/writeback page -> inactive, head, PG_reclaim
  677. * 5. inactive, clean -> inactive, tail
  678. * 6. Others -> none
  679. *
  680. * In 4, why it moves inactive's head, the VM expects the page would
  681. * be write it out by flusher threads as this is much more effective
  682. * than the single-page writeout from reclaim.
  683. */
  684. static void lru_deactivate_file_fn(struct page *page, struct lruvec *lruvec,
  685. void *arg)
  686. {
  687. int lru, file;
  688. bool active;
  689. if (!PageLRU(page))
  690. return;
  691. if (PageUnevictable(page))
  692. return;
  693. /* Some processes are using the page */
  694. if (page_mapped(page))
  695. return;
  696. active = PageActive(page);
  697. file = page_is_file_cache(page);
  698. lru = page_lru_base_type(page);
  699. del_page_from_lru_list(page, lruvec, lru + active);
  700. ClearPageActive(page);
  701. ClearPageReferenced(page);
  702. add_page_to_lru_list(page, lruvec, lru);
  703. if (PageWriteback(page) || PageDirty(page)) {
  704. /*
  705. * PG_reclaim could be raced with end_page_writeback
  706. * It can make readahead confusing. But race window
  707. * is _really_ small and it's non-critical problem.
  708. */
  709. SetPageReclaim(page);
  710. } else {
  711. /*
  712. * The page's writeback ends up during pagevec
  713. * We moves tha page into tail of inactive.
  714. */
  715. list_move_tail(&page->lru, &lruvec->lists[lru]);
  716. __count_vm_event(PGROTATED);
  717. }
  718. if (active)
  719. __count_vm_event(PGDEACTIVATE);
  720. update_page_reclaim_stat(lruvec, file, 0);
  721. }
  722. /*
  723. * Drain pages out of the cpu's pagevecs.
  724. * Either "cpu" is the current CPU, and preemption has already been
  725. * disabled; or "cpu" is being hot-unplugged, and is already dead.
  726. */
  727. void lru_add_drain_cpu(int cpu)
  728. {
  729. struct pagevec *pvec = &per_cpu(lru_add_pvec, cpu);
  730. if (pagevec_count(pvec))
  731. __pagevec_lru_add(pvec);
  732. pvec = &per_cpu(lru_rotate_pvecs, cpu);
  733. if (pagevec_count(pvec)) {
  734. unsigned long flags;
  735. /* No harm done if a racing interrupt already did this */
  736. local_irq_save(flags);
  737. pagevec_move_tail(pvec);
  738. local_irq_restore(flags);
  739. }
  740. pvec = &per_cpu(lru_deactivate_file_pvecs, cpu);
  741. if (pagevec_count(pvec))
  742. pagevec_lru_move_fn(pvec, lru_deactivate_file_fn, NULL);
  743. activate_page_drain(cpu);
  744. }
  745. /**
  746. * deactivate_file_page - forcefully deactivate a file page
  747. * @page: page to deactivate
  748. *
  749. * This function hints the VM that @page is a good reclaim candidate,
  750. * for example if its invalidation fails due to the page being dirty
  751. * or under writeback.
  752. */
  753. void deactivate_file_page(struct page *page)
  754. {
  755. /*
  756. * In a workload with many unevictable page such as mprotect,
  757. * unevictable page deactivation for accelerating reclaim is pointless.
  758. */
  759. if (PageUnevictable(page))
  760. return;
  761. if (likely(get_page_unless_zero(page))) {
  762. struct pagevec *pvec = &get_cpu_var(lru_deactivate_file_pvecs);
  763. if (!pagevec_add(pvec, page))
  764. pagevec_lru_move_fn(pvec, lru_deactivate_file_fn, NULL);
  765. put_cpu_var(lru_deactivate_file_pvecs);
  766. }
  767. }
  768. void lru_add_drain(void)
  769. {
  770. lru_add_drain_cpu(get_cpu());
  771. put_cpu();
  772. }
  773. static void lru_add_drain_per_cpu(struct work_struct *dummy)
  774. {
  775. lru_add_drain();
  776. }
  777. static DEFINE_PER_CPU(struct work_struct, lru_add_drain_work);
  778. void lru_add_drain_all(void)
  779. {
  780. static DEFINE_MUTEX(lock);
  781. static struct cpumask has_work;
  782. int cpu;
  783. mutex_lock(&lock);
  784. get_online_cpus();
  785. cpumask_clear(&has_work);
  786. for_each_online_cpu(cpu) {
  787. struct work_struct *work = &per_cpu(lru_add_drain_work, cpu);
  788. if (pagevec_count(&per_cpu(lru_add_pvec, cpu)) ||
  789. pagevec_count(&per_cpu(lru_rotate_pvecs, cpu)) ||
  790. pagevec_count(&per_cpu(lru_deactivate_file_pvecs, cpu)) ||
  791. need_activate_page_drain(cpu)) {
  792. INIT_WORK(work, lru_add_drain_per_cpu);
  793. schedule_work_on(cpu, work);
  794. cpumask_set_cpu(cpu, &has_work);
  795. }
  796. }
  797. for_each_cpu(cpu, &has_work)
  798. flush_work(&per_cpu(lru_add_drain_work, cpu));
  799. put_online_cpus();
  800. mutex_unlock(&lock);
  801. }
  802. /**
  803. * release_pages - batched page_cache_release()
  804. * @pages: array of pages to release
  805. * @nr: number of pages
  806. * @cold: whether the pages are cache cold
  807. *
  808. * Decrement the reference count on all the pages in @pages. If it
  809. * fell to zero, remove the page from the LRU and free it.
  810. */
  811. void release_pages(struct page **pages, int nr, bool cold)
  812. {
  813. int i;
  814. LIST_HEAD(pages_to_free);
  815. struct zone *zone = NULL;
  816. struct lruvec *lruvec;
  817. unsigned long uninitialized_var(flags);
  818. unsigned int uninitialized_var(lock_batch);
  819. for (i = 0; i < nr; i++) {
  820. struct page *page = pages[i];
  821. if (unlikely(PageCompound(page))) {
  822. if (zone) {
  823. spin_unlock_irqrestore(&zone->lru_lock, flags);
  824. zone = NULL;
  825. }
  826. put_compound_page(page);
  827. continue;
  828. }
  829. /*
  830. * Make sure the IRQ-safe lock-holding time does not get
  831. * excessive with a continuous string of pages from the
  832. * same zone. The lock is held only if zone != NULL.
  833. */
  834. if (zone && ++lock_batch == SWAP_CLUSTER_MAX) {
  835. spin_unlock_irqrestore(&zone->lru_lock, flags);
  836. zone = NULL;
  837. }
  838. if (!put_page_testzero(page))
  839. continue;
  840. if (PageLRU(page)) {
  841. struct zone *pagezone = page_zone(page);
  842. if (pagezone != zone) {
  843. if (zone)
  844. spin_unlock_irqrestore(&zone->lru_lock,
  845. flags);
  846. lock_batch = 0;
  847. zone = pagezone;
  848. spin_lock_irqsave(&zone->lru_lock, flags);
  849. }
  850. lruvec = mem_cgroup_page_lruvec(page, zone);
  851. VM_BUG_ON_PAGE(!PageLRU(page), page);
  852. __ClearPageLRU(page);
  853. del_page_from_lru_list(page, lruvec, page_off_lru(page));
  854. }
  855. /* Clear Active bit in case of parallel mark_page_accessed */
  856. __ClearPageActive(page);
  857. list_add(&page->lru, &pages_to_free);
  858. }
  859. if (zone)
  860. spin_unlock_irqrestore(&zone->lru_lock, flags);
  861. mem_cgroup_uncharge_list(&pages_to_free);
  862. free_hot_cold_page_list(&pages_to_free, cold);
  863. }
  864. EXPORT_SYMBOL(release_pages);
  865. /*
  866. * The pages which we're about to release may be in the deferred lru-addition
  867. * queues. That would prevent them from really being freed right now. That's
  868. * OK from a correctness point of view but is inefficient - those pages may be
  869. * cache-warm and we want to give them back to the page allocator ASAP.
  870. *
  871. * So __pagevec_release() will drain those queues here. __pagevec_lru_add()
  872. * and __pagevec_lru_add_active() call release_pages() directly to avoid
  873. * mutual recursion.
  874. */
  875. void __pagevec_release(struct pagevec *pvec)
  876. {
  877. lru_add_drain();
  878. release_pages(pvec->pages, pagevec_count(pvec), pvec->cold);
  879. pagevec_reinit(pvec);
  880. }
  881. EXPORT_SYMBOL(__pagevec_release);
  882. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  883. /* used by __split_huge_page_refcount() */
  884. void lru_add_page_tail(struct page *page, struct page *page_tail,
  885. struct lruvec *lruvec, struct list_head *list)
  886. {
  887. const int file = 0;
  888. VM_BUG_ON_PAGE(!PageHead(page), page);
  889. VM_BUG_ON_PAGE(PageCompound(page_tail), page);
  890. VM_BUG_ON_PAGE(PageLRU(page_tail), page);
  891. VM_BUG_ON(NR_CPUS != 1 &&
  892. !spin_is_locked(&lruvec_zone(lruvec)->lru_lock));
  893. if (!list)
  894. SetPageLRU(page_tail);
  895. if (likely(PageLRU(page)))
  896. list_add_tail(&page_tail->lru, &page->lru);
  897. else if (list) {
  898. /* page reclaim is reclaiming a huge page */
  899. get_page(page_tail);
  900. list_add_tail(&page_tail->lru, list);
  901. } else {
  902. struct list_head *list_head;
  903. /*
  904. * Head page has not yet been counted, as an hpage,
  905. * so we must account for each subpage individually.
  906. *
  907. * Use the standard add function to put page_tail on the list,
  908. * but then correct its position so they all end up in order.
  909. */
  910. add_page_to_lru_list(page_tail, lruvec, page_lru(page_tail));
  911. list_head = page_tail->lru.prev;
  912. list_move_tail(&page_tail->lru, list_head);
  913. }
  914. if (!PageUnevictable(page))
  915. update_page_reclaim_stat(lruvec, file, PageActive(page_tail));
  916. }
  917. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  918. static void __pagevec_lru_add_fn(struct page *page, struct lruvec *lruvec,
  919. void *arg)
  920. {
  921. int file = page_is_file_cache(page);
  922. int active = PageActive(page);
  923. enum lru_list lru = page_lru(page);
  924. VM_BUG_ON_PAGE(PageLRU(page), page);
  925. SetPageLRU(page);
  926. add_page_to_lru_list(page, lruvec, lru);
  927. update_page_reclaim_stat(lruvec, file, active);
  928. trace_mm_lru_insertion(page, lru);
  929. }
  930. /*
  931. * Add the passed pages to the LRU, then drop the caller's refcount
  932. * on them. Reinitialises the caller's pagevec.
  933. */
  934. void __pagevec_lru_add(struct pagevec *pvec)
  935. {
  936. pagevec_lru_move_fn(pvec, __pagevec_lru_add_fn, NULL);
  937. }
  938. EXPORT_SYMBOL(__pagevec_lru_add);
  939. /**
  940. * pagevec_lookup_entries - gang pagecache lookup
  941. * @pvec: Where the resulting entries are placed
  942. * @mapping: The address_space to search
  943. * @start: The starting entry index
  944. * @nr_entries: The maximum number of entries
  945. * @indices: The cache indices corresponding to the entries in @pvec
  946. *
  947. * pagevec_lookup_entries() will search for and return a group of up
  948. * to @nr_entries pages and shadow entries in the mapping. All
  949. * entries are placed in @pvec. pagevec_lookup_entries() takes a
  950. * reference against actual pages in @pvec.
  951. *
  952. * The search returns a group of mapping-contiguous entries with
  953. * ascending indexes. There may be holes in the indices due to
  954. * not-present entries.
  955. *
  956. * pagevec_lookup_entries() returns the number of entries which were
  957. * found.
  958. */
  959. unsigned pagevec_lookup_entries(struct pagevec *pvec,
  960. struct address_space *mapping,
  961. pgoff_t start, unsigned nr_pages,
  962. pgoff_t *indices)
  963. {
  964. pvec->nr = find_get_entries(mapping, start, nr_pages,
  965. pvec->pages, indices);
  966. return pagevec_count(pvec);
  967. }
  968. /**
  969. * pagevec_remove_exceptionals - pagevec exceptionals pruning
  970. * @pvec: The pagevec to prune
  971. *
  972. * pagevec_lookup_entries() fills both pages and exceptional radix
  973. * tree entries into the pagevec. This function prunes all
  974. * exceptionals from @pvec without leaving holes, so that it can be
  975. * passed on to page-only pagevec operations.
  976. */
  977. void pagevec_remove_exceptionals(struct pagevec *pvec)
  978. {
  979. int i, j;
  980. for (i = 0, j = 0; i < pagevec_count(pvec); i++) {
  981. struct page *page = pvec->pages[i];
  982. if (!radix_tree_exceptional_entry(page))
  983. pvec->pages[j++] = page;
  984. }
  985. pvec->nr = j;
  986. }
  987. /**
  988. * pagevec_lookup - gang pagecache lookup
  989. * @pvec: Where the resulting pages are placed
  990. * @mapping: The address_space to search
  991. * @start: The starting page index
  992. * @nr_pages: The maximum number of pages
  993. *
  994. * pagevec_lookup() will search for and return a group of up to @nr_pages pages
  995. * in the mapping. The pages are placed in @pvec. pagevec_lookup() takes a
  996. * reference against the pages in @pvec.
  997. *
  998. * The search returns a group of mapping-contiguous pages with ascending
  999. * indexes. There may be holes in the indices due to not-present pages.
  1000. *
  1001. * pagevec_lookup() returns the number of pages which were found.
  1002. */
  1003. unsigned pagevec_lookup(struct pagevec *pvec, struct address_space *mapping,
  1004. pgoff_t start, unsigned nr_pages)
  1005. {
  1006. pvec->nr = find_get_pages(mapping, start, nr_pages, pvec->pages);
  1007. return pagevec_count(pvec);
  1008. }
  1009. EXPORT_SYMBOL(pagevec_lookup);
  1010. unsigned pagevec_lookup_tag(struct pagevec *pvec, struct address_space *mapping,
  1011. pgoff_t *index, int tag, unsigned nr_pages)
  1012. {
  1013. pvec->nr = find_get_pages_tag(mapping, index, tag,
  1014. nr_pages, pvec->pages);
  1015. return pagevec_count(pvec);
  1016. }
  1017. EXPORT_SYMBOL(pagevec_lookup_tag);
  1018. /*
  1019. * Perform any setup for the swap system
  1020. */
  1021. void __init swap_setup(void)
  1022. {
  1023. unsigned long megs = totalram_pages >> (20 - PAGE_SHIFT);
  1024. #ifdef CONFIG_SWAP
  1025. int i;
  1026. for (i = 0; i < MAX_SWAPFILES; i++)
  1027. spin_lock_init(&swapper_spaces[i].tree_lock);
  1028. #endif
  1029. /* Use a smaller cluster for small-memory machines */
  1030. if (megs < 16)
  1031. page_cluster = 2;
  1032. else
  1033. page_cluster = 3;
  1034. /*
  1035. * Right now other parts of the system means that we
  1036. * _really_ don't want to cluster much more
  1037. */
  1038. }