swap.c 32 KB

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