swap.c 29 KB

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