swap.c 32 KB

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