swap.c 31 KB

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