swap.c 28 KB

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