swap.c 25 KB

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