swap.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  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_unref_page(page);
  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);
  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_anon - 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. * lru_cache_add_active_or_unevictable
  392. * @page: the page to be added to LRU
  393. * @vma: vma in which page is mapped for determining reclaimability
  394. *
  395. * Place @page on the active or unevictable LRU list, depending on its
  396. * evictability. Note that if the page is not evictable, it goes
  397. * directly back onto it's zone's unevictable list, it does NOT use a
  398. * per cpu pagevec.
  399. */
  400. void lru_cache_add_active_or_unevictable(struct page *page,
  401. struct vm_area_struct *vma)
  402. {
  403. VM_BUG_ON_PAGE(PageLRU(page), page);
  404. if (likely((vma->vm_flags & (VM_LOCKED | VM_SPECIAL)) != VM_LOCKED))
  405. SetPageActive(page);
  406. else if (!TestSetPageMlocked(page)) {
  407. /*
  408. * We use the irq-unsafe __mod_zone_page_stat because this
  409. * counter is not modified from interrupt context, and the pte
  410. * lock is held(spinlock), which implies preemption disabled.
  411. */
  412. __mod_zone_page_state(page_zone(page), NR_MLOCK,
  413. hpage_nr_pages(page));
  414. count_vm_event(UNEVICTABLE_PGMLOCKED);
  415. }
  416. lru_cache_add(page);
  417. }
  418. /*
  419. * If the page can not be invalidated, it is moved to the
  420. * inactive list to speed up its reclaim. It is moved to the
  421. * head of the list, rather than the tail, to give the flusher
  422. * threads some time to write it out, as this is much more
  423. * effective than the single-page writeout from reclaim.
  424. *
  425. * If the page isn't page_mapped and dirty/writeback, the page
  426. * could reclaim asap using PG_reclaim.
  427. *
  428. * 1. active, mapped page -> none
  429. * 2. active, dirty/writeback page -> inactive, head, PG_reclaim
  430. * 3. inactive, mapped page -> none
  431. * 4. inactive, dirty/writeback page -> inactive, head, PG_reclaim
  432. * 5. inactive, clean -> inactive, tail
  433. * 6. Others -> none
  434. *
  435. * In 4, why it moves inactive's head, the VM expects the page would
  436. * be write it out by flusher threads as this is much more effective
  437. * than the single-page writeout from reclaim.
  438. */
  439. static void lru_deactivate_file_fn(struct page *page, struct lruvec *lruvec,
  440. void *arg)
  441. {
  442. int lru, file;
  443. bool active;
  444. if (!PageLRU(page))
  445. return;
  446. if (PageUnevictable(page))
  447. return;
  448. /* Some processes are using the page */
  449. if (page_mapped(page))
  450. return;
  451. active = PageActive(page);
  452. file = page_is_file_cache(page);
  453. lru = page_lru_base_type(page);
  454. del_page_from_lru_list(page, lruvec, lru + active);
  455. ClearPageActive(page);
  456. ClearPageReferenced(page);
  457. add_page_to_lru_list(page, lruvec, lru);
  458. if (PageWriteback(page) || PageDirty(page)) {
  459. /*
  460. * PG_reclaim could be raced with end_page_writeback
  461. * It can make readahead confusing. But race window
  462. * is _really_ small and it's non-critical problem.
  463. */
  464. SetPageReclaim(page);
  465. } else {
  466. /*
  467. * The page's writeback ends up during pagevec
  468. * We moves tha page into tail of inactive.
  469. */
  470. list_move_tail(&page->lru, &lruvec->lists[lru]);
  471. __count_vm_event(PGROTATED);
  472. }
  473. if (active)
  474. __count_vm_event(PGDEACTIVATE);
  475. update_page_reclaim_stat(lruvec, file, 0);
  476. }
  477. static void lru_lazyfree_fn(struct page *page, struct lruvec *lruvec,
  478. void *arg)
  479. {
  480. if (PageLRU(page) && PageAnon(page) && PageSwapBacked(page) &&
  481. !PageSwapCache(page) && !PageUnevictable(page)) {
  482. bool active = PageActive(page);
  483. del_page_from_lru_list(page, lruvec,
  484. LRU_INACTIVE_ANON + active);
  485. ClearPageActive(page);
  486. ClearPageReferenced(page);
  487. /*
  488. * lazyfree pages are clean anonymous pages. They have
  489. * SwapBacked flag cleared to distinguish normal anonymous
  490. * pages
  491. */
  492. ClearPageSwapBacked(page);
  493. add_page_to_lru_list(page, lruvec, LRU_INACTIVE_FILE);
  494. __count_vm_events(PGLAZYFREE, hpage_nr_pages(page));
  495. count_memcg_page_event(page, PGLAZYFREE);
  496. update_page_reclaim_stat(lruvec, 1, 0);
  497. }
  498. }
  499. /*
  500. * Drain pages out of the cpu's pagevecs.
  501. * Either "cpu" is the current CPU, and preemption has already been
  502. * disabled; or "cpu" is being hot-unplugged, and is already dead.
  503. */
  504. void lru_add_drain_cpu(int cpu)
  505. {
  506. struct pagevec *pvec = &per_cpu(lru_add_pvec, cpu);
  507. if (pagevec_count(pvec))
  508. __pagevec_lru_add(pvec);
  509. pvec = &per_cpu(lru_rotate_pvecs, cpu);
  510. if (pagevec_count(pvec)) {
  511. unsigned long flags;
  512. /* No harm done if a racing interrupt already did this */
  513. local_irq_save(flags);
  514. pagevec_move_tail(pvec);
  515. local_irq_restore(flags);
  516. }
  517. pvec = &per_cpu(lru_deactivate_file_pvecs, cpu);
  518. if (pagevec_count(pvec))
  519. pagevec_lru_move_fn(pvec, lru_deactivate_file_fn, NULL);
  520. pvec = &per_cpu(lru_lazyfree_pvecs, cpu);
  521. if (pagevec_count(pvec))
  522. pagevec_lru_move_fn(pvec, lru_lazyfree_fn, NULL);
  523. activate_page_drain(cpu);
  524. }
  525. /**
  526. * deactivate_file_page - forcefully deactivate a file page
  527. * @page: page to deactivate
  528. *
  529. * This function hints the VM that @page is a good reclaim candidate,
  530. * for example if its invalidation fails due to the page being dirty
  531. * or under writeback.
  532. */
  533. void deactivate_file_page(struct page *page)
  534. {
  535. /*
  536. * In a workload with many unevictable page such as mprotect,
  537. * unevictable page deactivation for accelerating reclaim is pointless.
  538. */
  539. if (PageUnevictable(page))
  540. return;
  541. if (likely(get_page_unless_zero(page))) {
  542. struct pagevec *pvec = &get_cpu_var(lru_deactivate_file_pvecs);
  543. if (!pagevec_add(pvec, page) || PageCompound(page))
  544. pagevec_lru_move_fn(pvec, lru_deactivate_file_fn, NULL);
  545. put_cpu_var(lru_deactivate_file_pvecs);
  546. }
  547. }
  548. /**
  549. * mark_page_lazyfree - make an anon page lazyfree
  550. * @page: page to deactivate
  551. *
  552. * mark_page_lazyfree() moves @page to the inactive file list.
  553. * This is done to accelerate the reclaim of @page.
  554. */
  555. void mark_page_lazyfree(struct page *page)
  556. {
  557. if (PageLRU(page) && PageAnon(page) && PageSwapBacked(page) &&
  558. !PageSwapCache(page) && !PageUnevictable(page)) {
  559. struct pagevec *pvec = &get_cpu_var(lru_lazyfree_pvecs);
  560. get_page(page);
  561. if (!pagevec_add(pvec, page) || PageCompound(page))
  562. pagevec_lru_move_fn(pvec, lru_lazyfree_fn, NULL);
  563. put_cpu_var(lru_lazyfree_pvecs);
  564. }
  565. }
  566. void lru_add_drain(void)
  567. {
  568. lru_add_drain_cpu(get_cpu());
  569. put_cpu();
  570. }
  571. static void lru_add_drain_per_cpu(struct work_struct *dummy)
  572. {
  573. lru_add_drain();
  574. }
  575. static DEFINE_PER_CPU(struct work_struct, lru_add_drain_work);
  576. /*
  577. * Doesn't need any cpu hotplug locking because we do rely on per-cpu
  578. * kworkers being shut down before our page_alloc_cpu_dead callback is
  579. * executed on the offlined cpu.
  580. * Calling this function with cpu hotplug locks held can actually lead
  581. * to obscure indirect dependencies via WQ context.
  582. */
  583. void lru_add_drain_all(void)
  584. {
  585. static DEFINE_MUTEX(lock);
  586. static struct cpumask has_work;
  587. int cpu;
  588. /*
  589. * Make sure nobody triggers this path before mm_percpu_wq is fully
  590. * initialized.
  591. */
  592. if (WARN_ON(!mm_percpu_wq))
  593. return;
  594. mutex_lock(&lock);
  595. cpumask_clear(&has_work);
  596. for_each_online_cpu(cpu) {
  597. struct work_struct *work = &per_cpu(lru_add_drain_work, cpu);
  598. if (pagevec_count(&per_cpu(lru_add_pvec, cpu)) ||
  599. pagevec_count(&per_cpu(lru_rotate_pvecs, cpu)) ||
  600. pagevec_count(&per_cpu(lru_deactivate_file_pvecs, cpu)) ||
  601. pagevec_count(&per_cpu(lru_lazyfree_pvecs, cpu)) ||
  602. need_activate_page_drain(cpu)) {
  603. INIT_WORK(work, lru_add_drain_per_cpu);
  604. queue_work_on(cpu, mm_percpu_wq, work);
  605. cpumask_set_cpu(cpu, &has_work);
  606. }
  607. }
  608. for_each_cpu(cpu, &has_work)
  609. flush_work(&per_cpu(lru_add_drain_work, cpu));
  610. mutex_unlock(&lock);
  611. }
  612. /**
  613. * release_pages - batched put_page()
  614. * @pages: array of pages to release
  615. * @nr: number of pages
  616. *
  617. * Decrement the reference count on all the pages in @pages. If it
  618. * fell to zero, remove the page from the LRU and free it.
  619. */
  620. void release_pages(struct page **pages, int nr)
  621. {
  622. int i;
  623. LIST_HEAD(pages_to_free);
  624. struct pglist_data *locked_pgdat = NULL;
  625. struct lruvec *lruvec;
  626. unsigned long uninitialized_var(flags);
  627. unsigned int uninitialized_var(lock_batch);
  628. for (i = 0; i < nr; i++) {
  629. struct page *page = pages[i];
  630. /*
  631. * Make sure the IRQ-safe lock-holding time does not get
  632. * excessive with a continuous string of pages from the
  633. * same pgdat. The lock is held only if pgdat != NULL.
  634. */
  635. if (locked_pgdat && ++lock_batch == SWAP_CLUSTER_MAX) {
  636. spin_unlock_irqrestore(&locked_pgdat->lru_lock, flags);
  637. locked_pgdat = NULL;
  638. }
  639. if (is_huge_zero_page(page))
  640. continue;
  641. /* Device public page can not be huge page */
  642. if (is_device_public_page(page)) {
  643. if (locked_pgdat) {
  644. spin_unlock_irqrestore(&locked_pgdat->lru_lock,
  645. flags);
  646. locked_pgdat = NULL;
  647. }
  648. put_devmap_managed_page(page);
  649. continue;
  650. }
  651. page = compound_head(page);
  652. if (!put_page_testzero(page))
  653. continue;
  654. if (PageCompound(page)) {
  655. if (locked_pgdat) {
  656. spin_unlock_irqrestore(&locked_pgdat->lru_lock, flags);
  657. locked_pgdat = NULL;
  658. }
  659. __put_compound_page(page);
  660. continue;
  661. }
  662. if (PageLRU(page)) {
  663. struct pglist_data *pgdat = page_pgdat(page);
  664. if (pgdat != locked_pgdat) {
  665. if (locked_pgdat)
  666. spin_unlock_irqrestore(&locked_pgdat->lru_lock,
  667. flags);
  668. lock_batch = 0;
  669. locked_pgdat = pgdat;
  670. spin_lock_irqsave(&locked_pgdat->lru_lock, flags);
  671. }
  672. lruvec = mem_cgroup_page_lruvec(page, locked_pgdat);
  673. VM_BUG_ON_PAGE(!PageLRU(page), page);
  674. __ClearPageLRU(page);
  675. del_page_from_lru_list(page, lruvec, page_off_lru(page));
  676. }
  677. /* Clear Active bit in case of parallel mark_page_accessed */
  678. __ClearPageActive(page);
  679. __ClearPageWaiters(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_unref_page_list(&pages_to_free);
  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. if (!pvec->percpu_pvec_drained) {
  701. lru_add_drain();
  702. pvec->percpu_pvec_drained = true;
  703. }
  704. release_pages(pvec->pages, pagevec_count(pvec));
  705. pagevec_reinit(pvec);
  706. }
  707. EXPORT_SYMBOL(__pagevec_release);
  708. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  709. /* used by __split_huge_page_refcount() */
  710. void lru_add_page_tail(struct page *page, struct page *page_tail,
  711. struct lruvec *lruvec, struct list_head *list)
  712. {
  713. const int file = 0;
  714. VM_BUG_ON_PAGE(!PageHead(page), page);
  715. VM_BUG_ON_PAGE(PageCompound(page_tail), page);
  716. VM_BUG_ON_PAGE(PageLRU(page_tail), page);
  717. VM_BUG_ON(NR_CPUS != 1 &&
  718. !spin_is_locked(&lruvec_pgdat(lruvec)->lru_lock));
  719. if (!list)
  720. SetPageLRU(page_tail);
  721. if (likely(PageLRU(page)))
  722. list_add_tail(&page_tail->lru, &page->lru);
  723. else if (list) {
  724. /* page reclaim is reclaiming a huge page */
  725. get_page(page_tail);
  726. list_add_tail(&page_tail->lru, list);
  727. } else {
  728. struct list_head *list_head;
  729. /*
  730. * Head page has not yet been counted, as an hpage,
  731. * so we must account for each subpage individually.
  732. *
  733. * Use the standard add function to put page_tail on the list,
  734. * but then correct its position so they all end up in order.
  735. */
  736. add_page_to_lru_list(page_tail, lruvec, page_lru(page_tail));
  737. list_head = page_tail->lru.prev;
  738. list_move_tail(&page_tail->lru, list_head);
  739. }
  740. if (!PageUnevictable(page))
  741. update_page_reclaim_stat(lruvec, file, PageActive(page_tail));
  742. }
  743. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  744. static void __pagevec_lru_add_fn(struct page *page, struct lruvec *lruvec,
  745. void *arg)
  746. {
  747. enum lru_list lru;
  748. int was_unevictable = TestClearPageUnevictable(page);
  749. VM_BUG_ON_PAGE(PageLRU(page), page);
  750. SetPageLRU(page);
  751. /*
  752. * Page becomes evictable in two ways:
  753. * 1) Within LRU lock [munlock_vma_pages() and __munlock_pagevec()].
  754. * 2) Before acquiring LRU lock to put the page to correct LRU and then
  755. * a) do PageLRU check with lock [check_move_unevictable_pages]
  756. * b) do PageLRU check before lock [clear_page_mlock]
  757. *
  758. * (1) & (2a) are ok as LRU lock will serialize them. For (2b), we need
  759. * following strict ordering:
  760. *
  761. * #0: __pagevec_lru_add_fn #1: clear_page_mlock
  762. *
  763. * SetPageLRU() TestClearPageMlocked()
  764. * smp_mb() // explicit ordering // above provides strict
  765. * // ordering
  766. * PageMlocked() PageLRU()
  767. *
  768. *
  769. * if '#1' does not observe setting of PG_lru by '#0' and fails
  770. * isolation, the explicit barrier will make sure that page_evictable
  771. * check will put the page in correct LRU. Without smp_mb(), SetPageLRU
  772. * can be reordered after PageMlocked check and can make '#1' to fail
  773. * the isolation of the page whose Mlocked bit is cleared (#0 is also
  774. * looking at the same page) and the evictable page will be stranded
  775. * in an unevictable LRU.
  776. */
  777. smp_mb();
  778. if (page_evictable(page)) {
  779. lru = page_lru(page);
  780. update_page_reclaim_stat(lruvec, page_is_file_cache(page),
  781. PageActive(page));
  782. if (was_unevictable)
  783. count_vm_event(UNEVICTABLE_PGRESCUED);
  784. } else {
  785. lru = LRU_UNEVICTABLE;
  786. ClearPageActive(page);
  787. SetPageUnevictable(page);
  788. if (!was_unevictable)
  789. count_vm_event(UNEVICTABLE_PGCULLED);
  790. }
  791. add_page_to_lru_list(page, lruvec, lru);
  792. trace_mm_lru_insertion(page, lru);
  793. }
  794. /*
  795. * Add the passed pages to the LRU, then drop the caller's refcount
  796. * on them. Reinitialises the caller's pagevec.
  797. */
  798. void __pagevec_lru_add(struct pagevec *pvec)
  799. {
  800. pagevec_lru_move_fn(pvec, __pagevec_lru_add_fn, NULL);
  801. }
  802. EXPORT_SYMBOL(__pagevec_lru_add);
  803. /**
  804. * pagevec_lookup_entries - gang pagecache lookup
  805. * @pvec: Where the resulting entries are placed
  806. * @mapping: The address_space to search
  807. * @start: The starting entry index
  808. * @nr_entries: The maximum number of pages
  809. * @indices: The cache indices corresponding to the entries in @pvec
  810. *
  811. * pagevec_lookup_entries() will search for and return a group of up
  812. * to @nr_pages pages and shadow entries in the mapping. All
  813. * entries are placed in @pvec. pagevec_lookup_entries() takes a
  814. * reference against actual pages in @pvec.
  815. *
  816. * The search returns a group of mapping-contiguous entries with
  817. * ascending indexes. There may be holes in the indices due to
  818. * not-present entries.
  819. *
  820. * pagevec_lookup_entries() returns the number of entries which were
  821. * found.
  822. */
  823. unsigned pagevec_lookup_entries(struct pagevec *pvec,
  824. struct address_space *mapping,
  825. pgoff_t start, unsigned nr_entries,
  826. pgoff_t *indices)
  827. {
  828. pvec->nr = find_get_entries(mapping, start, nr_entries,
  829. pvec->pages, indices);
  830. return pagevec_count(pvec);
  831. }
  832. /**
  833. * pagevec_remove_exceptionals - pagevec exceptionals pruning
  834. * @pvec: The pagevec to prune
  835. *
  836. * pagevec_lookup_entries() fills both pages and exceptional radix
  837. * tree entries into the pagevec. This function prunes all
  838. * exceptionals from @pvec without leaving holes, so that it can be
  839. * passed on to page-only pagevec operations.
  840. */
  841. void pagevec_remove_exceptionals(struct pagevec *pvec)
  842. {
  843. int i, j;
  844. for (i = 0, j = 0; i < pagevec_count(pvec); i++) {
  845. struct page *page = pvec->pages[i];
  846. if (!xa_is_value(page))
  847. pvec->pages[j++] = page;
  848. }
  849. pvec->nr = j;
  850. }
  851. /**
  852. * pagevec_lookup_range - gang pagecache lookup
  853. * @pvec: Where the resulting pages are placed
  854. * @mapping: The address_space to search
  855. * @start: The starting page index
  856. * @end: The final page index
  857. *
  858. * pagevec_lookup_range() will search for & return a group of up to PAGEVEC_SIZE
  859. * pages in the mapping starting from index @start and upto index @end
  860. * (inclusive). The pages are placed in @pvec. pagevec_lookup() takes a
  861. * reference against the pages in @pvec.
  862. *
  863. * The search returns a group of mapping-contiguous pages with ascending
  864. * indexes. There may be holes in the indices due to not-present pages. We
  865. * also update @start to index the next page for the traversal.
  866. *
  867. * pagevec_lookup_range() returns the number of pages which were found. If this
  868. * number is smaller than PAGEVEC_SIZE, the end of specified range has been
  869. * reached.
  870. */
  871. unsigned pagevec_lookup_range(struct pagevec *pvec,
  872. struct address_space *mapping, pgoff_t *start, pgoff_t end)
  873. {
  874. pvec->nr = find_get_pages_range(mapping, start, end, PAGEVEC_SIZE,
  875. pvec->pages);
  876. return pagevec_count(pvec);
  877. }
  878. EXPORT_SYMBOL(pagevec_lookup_range);
  879. unsigned pagevec_lookup_range_tag(struct pagevec *pvec,
  880. struct address_space *mapping, pgoff_t *index, pgoff_t end,
  881. xa_mark_t tag)
  882. {
  883. pvec->nr = find_get_pages_range_tag(mapping, index, end, tag,
  884. PAGEVEC_SIZE, pvec->pages);
  885. return pagevec_count(pvec);
  886. }
  887. EXPORT_SYMBOL(pagevec_lookup_range_tag);
  888. unsigned pagevec_lookup_range_nr_tag(struct pagevec *pvec,
  889. struct address_space *mapping, pgoff_t *index, pgoff_t end,
  890. xa_mark_t tag, unsigned max_pages)
  891. {
  892. pvec->nr = find_get_pages_range_tag(mapping, index, end, tag,
  893. min_t(unsigned int, max_pages, PAGEVEC_SIZE), pvec->pages);
  894. return pagevec_count(pvec);
  895. }
  896. EXPORT_SYMBOL(pagevec_lookup_range_nr_tag);
  897. /*
  898. * Perform any setup for the swap system
  899. */
  900. void __init swap_setup(void)
  901. {
  902. unsigned long megs = totalram_pages >> (20 - PAGE_SHIFT);
  903. /* Use a smaller cluster for small-memory machines */
  904. if (megs < 16)
  905. page_cluster = 2;
  906. else
  907. page_cluster = 3;
  908. /*
  909. * Right now other parts of the system means that we
  910. * _really_ don't want to cluster much more
  911. */
  912. }