swap.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  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. } else if (!PageReferenced(page)) {
  517. SetPageReferenced(page);
  518. }
  519. }
  520. EXPORT_SYMBOL(mark_page_accessed);
  521. /*
  522. * Queue the page for addition to the LRU via pagevec. The decision on whether
  523. * to add the page to the [in]active [file|anon] list is deferred until the
  524. * pagevec is drained. This gives a chance for the caller of __lru_cache_add()
  525. * have the page added to the active list using mark_page_accessed().
  526. */
  527. void __lru_cache_add(struct page *page)
  528. {
  529. struct pagevec *pvec = &get_cpu_var(lru_add_pvec);
  530. page_cache_get(page);
  531. if (!pagevec_space(pvec))
  532. __pagevec_lru_add(pvec);
  533. pagevec_add(pvec, page);
  534. put_cpu_var(lru_add_pvec);
  535. }
  536. EXPORT_SYMBOL(__lru_cache_add);
  537. /**
  538. * lru_cache_add - add a page to a page list
  539. * @page: the page to be added to the LRU.
  540. */
  541. void lru_cache_add(struct page *page)
  542. {
  543. VM_BUG_ON_PAGE(PageActive(page) && PageUnevictable(page), page);
  544. VM_BUG_ON_PAGE(PageLRU(page), page);
  545. __lru_cache_add(page);
  546. }
  547. /**
  548. * add_page_to_unevictable_list - add a page to the unevictable list
  549. * @page: the page to be added to the unevictable list
  550. *
  551. * Add page directly to its zone's unevictable list. To avoid races with
  552. * tasks that might be making the page evictable, through eg. munlock,
  553. * munmap or exit, while it's not on the lru, we want to add the page
  554. * while it's locked or otherwise "invisible" to other tasks. This is
  555. * difficult to do when using the pagevec cache, so bypass that.
  556. */
  557. void add_page_to_unevictable_list(struct page *page)
  558. {
  559. struct zone *zone = page_zone(page);
  560. struct lruvec *lruvec;
  561. spin_lock_irq(&zone->lru_lock);
  562. lruvec = mem_cgroup_page_lruvec(page, zone);
  563. ClearPageActive(page);
  564. SetPageUnevictable(page);
  565. SetPageLRU(page);
  566. add_page_to_lru_list(page, lruvec, LRU_UNEVICTABLE);
  567. spin_unlock_irq(&zone->lru_lock);
  568. }
  569. /*
  570. * If the page can not be invalidated, it is moved to the
  571. * inactive list to speed up its reclaim. It is moved to the
  572. * head of the list, rather than the tail, to give the flusher
  573. * threads some time to write it out, as this is much more
  574. * effective than the single-page writeout from reclaim.
  575. *
  576. * If the page isn't page_mapped and dirty/writeback, the page
  577. * could reclaim asap using PG_reclaim.
  578. *
  579. * 1. active, mapped page -> none
  580. * 2. active, dirty/writeback page -> inactive, head, PG_reclaim
  581. * 3. inactive, mapped page -> none
  582. * 4. inactive, dirty/writeback page -> inactive, head, PG_reclaim
  583. * 5. inactive, clean -> inactive, tail
  584. * 6. Others -> none
  585. *
  586. * In 4, why it moves inactive's head, the VM expects the page would
  587. * be write it out by flusher threads as this is much more effective
  588. * than the single-page writeout from reclaim.
  589. */
  590. static void lru_deactivate_fn(struct page *page, struct lruvec *lruvec,
  591. void *arg)
  592. {
  593. int lru, file;
  594. bool active;
  595. if (!PageLRU(page))
  596. return;
  597. if (PageUnevictable(page))
  598. return;
  599. /* Some processes are using the page */
  600. if (page_mapped(page))
  601. return;
  602. active = PageActive(page);
  603. file = page_is_file_cache(page);
  604. lru = page_lru_base_type(page);
  605. del_page_from_lru_list(page, lruvec, lru + active);
  606. ClearPageActive(page);
  607. ClearPageReferenced(page);
  608. add_page_to_lru_list(page, lruvec, lru);
  609. if (PageWriteback(page) || PageDirty(page)) {
  610. /*
  611. * PG_reclaim could be raced with end_page_writeback
  612. * It can make readahead confusing. But race window
  613. * is _really_ small and it's non-critical problem.
  614. */
  615. SetPageReclaim(page);
  616. } else {
  617. /*
  618. * The page's writeback ends up during pagevec
  619. * We moves tha page into tail of inactive.
  620. */
  621. list_move_tail(&page->lru, &lruvec->lists[lru]);
  622. __count_vm_event(PGROTATED);
  623. }
  624. if (active)
  625. __count_vm_event(PGDEACTIVATE);
  626. update_page_reclaim_stat(lruvec, file, 0);
  627. }
  628. /*
  629. * Drain pages out of the cpu's pagevecs.
  630. * Either "cpu" is the current CPU, and preemption has already been
  631. * disabled; or "cpu" is being hot-unplugged, and is already dead.
  632. */
  633. void lru_add_drain_cpu(int cpu)
  634. {
  635. struct pagevec *pvec = &per_cpu(lru_add_pvec, cpu);
  636. if (pagevec_count(pvec))
  637. __pagevec_lru_add(pvec);
  638. pvec = &per_cpu(lru_rotate_pvecs, cpu);
  639. if (pagevec_count(pvec)) {
  640. unsigned long flags;
  641. /* No harm done if a racing interrupt already did this */
  642. local_irq_save(flags);
  643. pagevec_move_tail(pvec);
  644. local_irq_restore(flags);
  645. }
  646. pvec = &per_cpu(lru_deactivate_pvecs, cpu);
  647. if (pagevec_count(pvec))
  648. pagevec_lru_move_fn(pvec, lru_deactivate_fn, NULL);
  649. activate_page_drain(cpu);
  650. }
  651. /**
  652. * deactivate_page - forcefully deactivate a page
  653. * @page: page to deactivate
  654. *
  655. * This function hints the VM that @page is a good reclaim candidate,
  656. * for example if its invalidation fails due to the page being dirty
  657. * or under writeback.
  658. */
  659. void deactivate_page(struct page *page)
  660. {
  661. /*
  662. * In a workload with many unevictable page such as mprotect, unevictable
  663. * page deactivation for accelerating reclaim is pointless.
  664. */
  665. if (PageUnevictable(page))
  666. return;
  667. if (likely(get_page_unless_zero(page))) {
  668. struct pagevec *pvec = &get_cpu_var(lru_deactivate_pvecs);
  669. if (!pagevec_add(pvec, page))
  670. pagevec_lru_move_fn(pvec, lru_deactivate_fn, NULL);
  671. put_cpu_var(lru_deactivate_pvecs);
  672. }
  673. }
  674. void lru_add_drain(void)
  675. {
  676. lru_add_drain_cpu(get_cpu());
  677. put_cpu();
  678. }
  679. static void lru_add_drain_per_cpu(struct work_struct *dummy)
  680. {
  681. lru_add_drain();
  682. }
  683. static DEFINE_PER_CPU(struct work_struct, lru_add_drain_work);
  684. void lru_add_drain_all(void)
  685. {
  686. static DEFINE_MUTEX(lock);
  687. static struct cpumask has_work;
  688. int cpu;
  689. mutex_lock(&lock);
  690. get_online_cpus();
  691. cpumask_clear(&has_work);
  692. for_each_online_cpu(cpu) {
  693. struct work_struct *work = &per_cpu(lru_add_drain_work, cpu);
  694. if (pagevec_count(&per_cpu(lru_add_pvec, cpu)) ||
  695. pagevec_count(&per_cpu(lru_rotate_pvecs, cpu)) ||
  696. pagevec_count(&per_cpu(lru_deactivate_pvecs, cpu)) ||
  697. need_activate_page_drain(cpu)) {
  698. INIT_WORK(work, lru_add_drain_per_cpu);
  699. schedule_work_on(cpu, work);
  700. cpumask_set_cpu(cpu, &has_work);
  701. }
  702. }
  703. for_each_cpu(cpu, &has_work)
  704. flush_work(&per_cpu(lru_add_drain_work, cpu));
  705. put_online_cpus();
  706. mutex_unlock(&lock);
  707. }
  708. /*
  709. * Batched page_cache_release(). Decrement the reference count on all the
  710. * passed pages. If it fell to zero then remove the page from the LRU and
  711. * free it.
  712. *
  713. * Avoid taking zone->lru_lock if possible, but if it is taken, retain it
  714. * for the remainder of the operation.
  715. *
  716. * The locking in this function is against shrink_inactive_list(): we recheck
  717. * the page count inside the lock to see whether shrink_inactive_list()
  718. * grabbed the page via the LRU. If it did, give up: shrink_inactive_list()
  719. * will free it.
  720. */
  721. void release_pages(struct page **pages, int nr, int cold)
  722. {
  723. int i;
  724. LIST_HEAD(pages_to_free);
  725. struct zone *zone = NULL;
  726. struct lruvec *lruvec;
  727. unsigned long uninitialized_var(flags);
  728. for (i = 0; i < nr; i++) {
  729. struct page *page = pages[i];
  730. if (unlikely(PageCompound(page))) {
  731. if (zone) {
  732. spin_unlock_irqrestore(&zone->lru_lock, flags);
  733. zone = NULL;
  734. }
  735. put_compound_page(page);
  736. continue;
  737. }
  738. if (!put_page_testzero(page))
  739. continue;
  740. if (PageLRU(page)) {
  741. struct zone *pagezone = page_zone(page);
  742. if (pagezone != zone) {
  743. if (zone)
  744. spin_unlock_irqrestore(&zone->lru_lock,
  745. flags);
  746. zone = pagezone;
  747. spin_lock_irqsave(&zone->lru_lock, flags);
  748. }
  749. lruvec = mem_cgroup_page_lruvec(page, zone);
  750. VM_BUG_ON_PAGE(!PageLRU(page), page);
  751. __ClearPageLRU(page);
  752. del_page_from_lru_list(page, lruvec, page_off_lru(page));
  753. }
  754. /* Clear Active bit in case of parallel mark_page_accessed */
  755. ClearPageActive(page);
  756. list_add(&page->lru, &pages_to_free);
  757. }
  758. if (zone)
  759. spin_unlock_irqrestore(&zone->lru_lock, flags);
  760. free_hot_cold_page_list(&pages_to_free, cold);
  761. }
  762. EXPORT_SYMBOL(release_pages);
  763. /*
  764. * The pages which we're about to release may be in the deferred lru-addition
  765. * queues. That would prevent them from really being freed right now. That's
  766. * OK from a correctness point of view but is inefficient - those pages may be
  767. * cache-warm and we want to give them back to the page allocator ASAP.
  768. *
  769. * So __pagevec_release() will drain those queues here. __pagevec_lru_add()
  770. * and __pagevec_lru_add_active() call release_pages() directly to avoid
  771. * mutual recursion.
  772. */
  773. void __pagevec_release(struct pagevec *pvec)
  774. {
  775. lru_add_drain();
  776. release_pages(pvec->pages, pagevec_count(pvec), pvec->cold);
  777. pagevec_reinit(pvec);
  778. }
  779. EXPORT_SYMBOL(__pagevec_release);
  780. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  781. /* used by __split_huge_page_refcount() */
  782. void lru_add_page_tail(struct page *page, struct page *page_tail,
  783. struct lruvec *lruvec, struct list_head *list)
  784. {
  785. const int file = 0;
  786. VM_BUG_ON_PAGE(!PageHead(page), page);
  787. VM_BUG_ON_PAGE(PageCompound(page_tail), page);
  788. VM_BUG_ON_PAGE(PageLRU(page_tail), page);
  789. VM_BUG_ON(NR_CPUS != 1 &&
  790. !spin_is_locked(&lruvec_zone(lruvec)->lru_lock));
  791. if (!list)
  792. SetPageLRU(page_tail);
  793. if (likely(PageLRU(page)))
  794. list_add_tail(&page_tail->lru, &page->lru);
  795. else if (list) {
  796. /* page reclaim is reclaiming a huge page */
  797. get_page(page_tail);
  798. list_add_tail(&page_tail->lru, list);
  799. } else {
  800. struct list_head *list_head;
  801. /*
  802. * Head page has not yet been counted, as an hpage,
  803. * so we must account for each subpage individually.
  804. *
  805. * Use the standard add function to put page_tail on the list,
  806. * but then correct its position so they all end up in order.
  807. */
  808. add_page_to_lru_list(page_tail, lruvec, page_lru(page_tail));
  809. list_head = page_tail->lru.prev;
  810. list_move_tail(&page_tail->lru, list_head);
  811. }
  812. if (!PageUnevictable(page))
  813. update_page_reclaim_stat(lruvec, file, PageActive(page_tail));
  814. }
  815. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  816. static void __pagevec_lru_add_fn(struct page *page, struct lruvec *lruvec,
  817. void *arg)
  818. {
  819. int file = page_is_file_cache(page);
  820. int active = PageActive(page);
  821. enum lru_list lru = page_lru(page);
  822. VM_BUG_ON_PAGE(PageLRU(page), page);
  823. SetPageLRU(page);
  824. add_page_to_lru_list(page, lruvec, lru);
  825. update_page_reclaim_stat(lruvec, file, active);
  826. trace_mm_lru_insertion(page, page_to_pfn(page), lru, trace_pagemap_flags(page));
  827. }
  828. /*
  829. * Add the passed pages to the LRU, then drop the caller's refcount
  830. * on them. Reinitialises the caller's pagevec.
  831. */
  832. void __pagevec_lru_add(struct pagevec *pvec)
  833. {
  834. pagevec_lru_move_fn(pvec, __pagevec_lru_add_fn, NULL);
  835. }
  836. EXPORT_SYMBOL(__pagevec_lru_add);
  837. /**
  838. * pagevec_lookup - gang pagecache lookup
  839. * @pvec: Where the resulting pages are placed
  840. * @mapping: The address_space to search
  841. * @start: The starting page index
  842. * @nr_pages: The maximum number of pages
  843. *
  844. * pagevec_lookup() will search for and return a group of up to @nr_pages pages
  845. * in the mapping. The pages are placed in @pvec. pagevec_lookup() takes a
  846. * reference against the pages in @pvec.
  847. *
  848. * The search returns a group of mapping-contiguous pages with ascending
  849. * indexes. There may be holes in the indices due to not-present pages.
  850. *
  851. * pagevec_lookup() returns the number of pages which were found.
  852. */
  853. unsigned pagevec_lookup(struct pagevec *pvec, struct address_space *mapping,
  854. pgoff_t start, unsigned nr_pages)
  855. {
  856. pvec->nr = find_get_pages(mapping, start, nr_pages, pvec->pages);
  857. return pagevec_count(pvec);
  858. }
  859. EXPORT_SYMBOL(pagevec_lookup);
  860. unsigned pagevec_lookup_tag(struct pagevec *pvec, struct address_space *mapping,
  861. pgoff_t *index, int tag, unsigned nr_pages)
  862. {
  863. pvec->nr = find_get_pages_tag(mapping, index, tag,
  864. nr_pages, pvec->pages);
  865. return pagevec_count(pvec);
  866. }
  867. EXPORT_SYMBOL(pagevec_lookup_tag);
  868. /*
  869. * Perform any setup for the swap system
  870. */
  871. void __init swap_setup(void)
  872. {
  873. unsigned long megs = totalram_pages >> (20 - PAGE_SHIFT);
  874. #ifdef CONFIG_SWAP
  875. int i;
  876. if (bdi_init(swapper_spaces[0].backing_dev_info))
  877. panic("Failed to init swap bdi");
  878. for (i = 0; i < MAX_SWAPFILES; i++) {
  879. spin_lock_init(&swapper_spaces[i].tree_lock);
  880. INIT_LIST_HEAD(&swapper_spaces[i].i_mmap_nonlinear);
  881. }
  882. #endif
  883. /* Use a smaller cluster for small-memory machines */
  884. if (megs < 16)
  885. page_cluster = 2;
  886. else
  887. page_cluster = 3;
  888. /*
  889. * Right now other parts of the system means that we
  890. * _really_ don't want to cluster much more
  891. */
  892. }