swap.c 27 KB

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