swap.c 27 KB

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