swap.c 26 KB

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