swap.c 32 KB

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