swap.c 27 KB

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