swap.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  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(!PageLRU(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. }
  58. static void __put_single_page(struct page *page)
  59. {
  60. __page_cache_release(page);
  61. free_hot_cold_page(page, 0);
  62. }
  63. static void __put_compound_page(struct page *page)
  64. {
  65. compound_page_dtor *dtor;
  66. __page_cache_release(page);
  67. dtor = get_compound_page_dtor(page);
  68. (*dtor)(page);
  69. }
  70. static void put_compound_page(struct page *page)
  71. {
  72. if (unlikely(PageTail(page))) {
  73. /* __split_huge_page_refcount can run under us */
  74. struct page *page_head = compound_trans_head(page);
  75. if (likely(page != page_head &&
  76. get_page_unless_zero(page_head))) {
  77. unsigned long flags;
  78. /*
  79. * THP can not break up slab pages so avoid taking
  80. * compound_lock(). Slab performs non-atomic bit ops
  81. * on page->flags for better performance. In particular
  82. * slab_unlock() in slub used to be a hot path. It is
  83. * still hot on arches that do not support
  84. * this_cpu_cmpxchg_double().
  85. */
  86. if (PageSlab(page_head)) {
  87. if (PageTail(page)) {
  88. if (put_page_testzero(page_head))
  89. VM_BUG_ON(1);
  90. atomic_dec(&page->_mapcount);
  91. goto skip_lock_tail;
  92. } else
  93. goto skip_lock;
  94. }
  95. /*
  96. * page_head wasn't a dangling pointer but it
  97. * may not be a head page anymore by the time
  98. * we obtain the lock. That is ok as long as it
  99. * can't be freed from under us.
  100. */
  101. flags = compound_lock_irqsave(page_head);
  102. if (unlikely(!PageTail(page))) {
  103. /* __split_huge_page_refcount run before us */
  104. compound_unlock_irqrestore(page_head, flags);
  105. skip_lock:
  106. if (put_page_testzero(page_head))
  107. __put_single_page(page_head);
  108. out_put_single:
  109. if (put_page_testzero(page))
  110. __put_single_page(page);
  111. return;
  112. }
  113. VM_BUG_ON(page_head != page->first_page);
  114. /*
  115. * We can release the refcount taken by
  116. * get_page_unless_zero() now that
  117. * __split_huge_page_refcount() is blocked on
  118. * the compound_lock.
  119. */
  120. if (put_page_testzero(page_head))
  121. VM_BUG_ON(1);
  122. /* __split_huge_page_refcount will wait now */
  123. VM_BUG_ON(page_mapcount(page) <= 0);
  124. atomic_dec(&page->_mapcount);
  125. VM_BUG_ON(atomic_read(&page_head->_count) <= 0);
  126. VM_BUG_ON(atomic_read(&page->_count) != 0);
  127. compound_unlock_irqrestore(page_head, flags);
  128. skip_lock_tail:
  129. if (put_page_testzero(page_head)) {
  130. if (PageHead(page_head))
  131. __put_compound_page(page_head);
  132. else
  133. __put_single_page(page_head);
  134. }
  135. } else {
  136. /* page_head is a dangling pointer */
  137. VM_BUG_ON(PageTail(page));
  138. goto out_put_single;
  139. }
  140. } else if (put_page_testzero(page)) {
  141. if (PageHead(page))
  142. __put_compound_page(page);
  143. else
  144. __put_single_page(page);
  145. }
  146. }
  147. void put_page(struct page *page)
  148. {
  149. if (unlikely(PageCompound(page)))
  150. put_compound_page(page);
  151. else if (put_page_testzero(page))
  152. __put_single_page(page);
  153. }
  154. EXPORT_SYMBOL(put_page);
  155. /*
  156. * This function is exported but must not be called by anything other
  157. * than get_page(). It implements the slow path of get_page().
  158. */
  159. bool __get_page_tail(struct page *page)
  160. {
  161. /*
  162. * This takes care of get_page() if run on a tail page
  163. * returned by one of the get_user_pages/follow_page variants.
  164. * get_user_pages/follow_page itself doesn't need the compound
  165. * lock because it runs __get_page_tail_foll() under the
  166. * proper PT lock that already serializes against
  167. * split_huge_page().
  168. */
  169. unsigned long flags;
  170. bool got = false;
  171. struct page *page_head = compound_trans_head(page);
  172. if (likely(page != page_head && get_page_unless_zero(page_head))) {
  173. /* Ref to put_compound_page() comment. */
  174. if (PageSlab(page_head)) {
  175. if (likely(PageTail(page))) {
  176. __get_page_tail_foll(page, false);
  177. return true;
  178. } else {
  179. put_page(page_head);
  180. return false;
  181. }
  182. }
  183. /*
  184. * page_head wasn't a dangling pointer but it
  185. * may not be a head page anymore by the time
  186. * we obtain the lock. That is ok as long as it
  187. * can't be freed from under us.
  188. */
  189. flags = compound_lock_irqsave(page_head);
  190. /* here __split_huge_page_refcount won't run anymore */
  191. if (likely(PageTail(page))) {
  192. __get_page_tail_foll(page, false);
  193. got = true;
  194. }
  195. compound_unlock_irqrestore(page_head, flags);
  196. if (unlikely(!got))
  197. put_page(page_head);
  198. }
  199. return got;
  200. }
  201. EXPORT_SYMBOL(__get_page_tail);
  202. /**
  203. * put_pages_list() - release a list of pages
  204. * @pages: list of pages threaded on page->lru
  205. *
  206. * Release a list of pages which are strung together on page.lru. Currently
  207. * used by read_cache_pages() and related error recovery code.
  208. */
  209. void put_pages_list(struct list_head *pages)
  210. {
  211. while (!list_empty(pages)) {
  212. struct page *victim;
  213. victim = list_entry(pages->prev, struct page, lru);
  214. list_del(&victim->lru);
  215. page_cache_release(victim);
  216. }
  217. }
  218. EXPORT_SYMBOL(put_pages_list);
  219. /*
  220. * get_kernel_pages() - pin kernel pages in memory
  221. * @kiov: An array of struct kvec structures
  222. * @nr_segs: number of segments to pin
  223. * @write: pinning for read/write, currently ignored
  224. * @pages: array that receives pointers to the pages pinned.
  225. * Should be at least nr_segs long.
  226. *
  227. * Returns number of pages pinned. This may be fewer than the number
  228. * requested. If nr_pages is 0 or negative, returns 0. If no pages
  229. * were pinned, returns -errno. Each page returned must be released
  230. * with a put_page() call when it is finished with.
  231. */
  232. int get_kernel_pages(const struct kvec *kiov, int nr_segs, int write,
  233. struct page **pages)
  234. {
  235. int seg;
  236. for (seg = 0; seg < nr_segs; seg++) {
  237. if (WARN_ON(kiov[seg].iov_len != PAGE_SIZE))
  238. return seg;
  239. pages[seg] = kmap_to_page(kiov[seg].iov_base);
  240. page_cache_get(pages[seg]);
  241. }
  242. return seg;
  243. }
  244. EXPORT_SYMBOL_GPL(get_kernel_pages);
  245. /*
  246. * get_kernel_page() - pin a kernel page in memory
  247. * @start: starting kernel address
  248. * @write: pinning for read/write, currently ignored
  249. * @pages: array that receives pointer to the page pinned.
  250. * Must be at least nr_segs long.
  251. *
  252. * Returns 1 if page is pinned. If the page was not pinned, returns
  253. * -errno. The page returned must be released with a put_page() call
  254. * when it is finished with.
  255. */
  256. int get_kernel_page(unsigned long start, int write, struct page **pages)
  257. {
  258. const struct kvec kiov = {
  259. .iov_base = (void *)start,
  260. .iov_len = PAGE_SIZE
  261. };
  262. return get_kernel_pages(&kiov, 1, write, pages);
  263. }
  264. EXPORT_SYMBOL_GPL(get_kernel_page);
  265. static void pagevec_lru_move_fn(struct pagevec *pvec,
  266. void (*move_fn)(struct page *page, struct lruvec *lruvec, void *arg),
  267. void *arg)
  268. {
  269. int i;
  270. struct zone *zone = NULL;
  271. struct lruvec *lruvec;
  272. unsigned long flags = 0;
  273. for (i = 0; i < pagevec_count(pvec); i++) {
  274. struct page *page = pvec->pages[i];
  275. struct zone *pagezone = page_zone(page);
  276. if (pagezone != zone) {
  277. if (zone)
  278. spin_unlock_irqrestore(&zone->lru_lock, flags);
  279. zone = pagezone;
  280. spin_lock_irqsave(&zone->lru_lock, flags);
  281. }
  282. lruvec = mem_cgroup_page_lruvec(page, zone);
  283. (*move_fn)(page, lruvec, arg);
  284. }
  285. if (zone)
  286. spin_unlock_irqrestore(&zone->lru_lock, flags);
  287. release_pages(pvec->pages, pvec->nr, pvec->cold);
  288. pagevec_reinit(pvec);
  289. }
  290. static void pagevec_move_tail_fn(struct page *page, struct lruvec *lruvec,
  291. void *arg)
  292. {
  293. int *pgmoved = arg;
  294. if (PageLRU(page) && !PageActive(page) && !PageUnevictable(page)) {
  295. enum lru_list lru = page_lru_base_type(page);
  296. list_move_tail(&page->lru, &lruvec->lists[lru]);
  297. (*pgmoved)++;
  298. }
  299. }
  300. /*
  301. * pagevec_move_tail() must be called with IRQ disabled.
  302. * Otherwise this may cause nasty races.
  303. */
  304. static void pagevec_move_tail(struct pagevec *pvec)
  305. {
  306. int pgmoved = 0;
  307. pagevec_lru_move_fn(pvec, pagevec_move_tail_fn, &pgmoved);
  308. __count_vm_events(PGROTATED, pgmoved);
  309. }
  310. /*
  311. * Writeback is about to end against a page which has been marked for immediate
  312. * reclaim. If it still appears to be reclaimable, move it to the tail of the
  313. * inactive list.
  314. */
  315. void rotate_reclaimable_page(struct page *page)
  316. {
  317. if (!PageLocked(page) && !PageDirty(page) && !PageActive(page) &&
  318. !PageUnevictable(page) && PageLRU(page)) {
  319. struct pagevec *pvec;
  320. unsigned long flags;
  321. page_cache_get(page);
  322. local_irq_save(flags);
  323. pvec = &__get_cpu_var(lru_rotate_pvecs);
  324. if (!pagevec_add(pvec, page))
  325. pagevec_move_tail(pvec);
  326. local_irq_restore(flags);
  327. }
  328. }
  329. static void update_page_reclaim_stat(struct lruvec *lruvec,
  330. int file, int rotated)
  331. {
  332. struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat;
  333. reclaim_stat->recent_scanned[file]++;
  334. if (rotated)
  335. reclaim_stat->recent_rotated[file]++;
  336. }
  337. static void __activate_page(struct page *page, struct lruvec *lruvec,
  338. void *arg)
  339. {
  340. if (PageLRU(page) && !PageActive(page) && !PageUnevictable(page)) {
  341. int file = page_is_file_cache(page);
  342. int lru = page_lru_base_type(page);
  343. del_page_from_lru_list(page, lruvec, lru);
  344. SetPageActive(page);
  345. lru += LRU_ACTIVE;
  346. add_page_to_lru_list(page, lruvec, lru);
  347. trace_mm_lru_activate(page, page_to_pfn(page));
  348. __count_vm_event(PGACTIVATE);
  349. update_page_reclaim_stat(lruvec, file, 1);
  350. }
  351. }
  352. #ifdef CONFIG_SMP
  353. static DEFINE_PER_CPU(struct pagevec, activate_page_pvecs);
  354. static void activate_page_drain(int cpu)
  355. {
  356. struct pagevec *pvec = &per_cpu(activate_page_pvecs, cpu);
  357. if (pagevec_count(pvec))
  358. pagevec_lru_move_fn(pvec, __activate_page, NULL);
  359. }
  360. void activate_page(struct page *page)
  361. {
  362. if (PageLRU(page) && !PageActive(page) && !PageUnevictable(page)) {
  363. struct pagevec *pvec = &get_cpu_var(activate_page_pvecs);
  364. page_cache_get(page);
  365. if (!pagevec_add(pvec, page))
  366. pagevec_lru_move_fn(pvec, __activate_page, NULL);
  367. put_cpu_var(activate_page_pvecs);
  368. }
  369. }
  370. #else
  371. static inline void activate_page_drain(int cpu)
  372. {
  373. }
  374. void activate_page(struct page *page)
  375. {
  376. struct zone *zone = page_zone(page);
  377. spin_lock_irq(&zone->lru_lock);
  378. __activate_page(page, mem_cgroup_page_lruvec(page, zone), NULL);
  379. spin_unlock_irq(&zone->lru_lock);
  380. }
  381. #endif
  382. static void __lru_cache_activate_page(struct page *page)
  383. {
  384. struct pagevec *pvec = &get_cpu_var(lru_add_pvec);
  385. int i;
  386. /*
  387. * Search backwards on the optimistic assumption that the page being
  388. * activated has just been added to this pagevec. Note that only
  389. * the local pagevec is examined as a !PageLRU page could be in the
  390. * process of being released, reclaimed, migrated or on a remote
  391. * pagevec that is currently being drained. Furthermore, marking
  392. * a remote pagevec's page PageActive potentially hits a race where
  393. * a page is marked PageActive just after it is added to the inactive
  394. * list causing accounting errors and BUG_ON checks to trigger.
  395. */
  396. for (i = pagevec_count(pvec) - 1; i >= 0; i--) {
  397. struct page *pagevec_page = pvec->pages[i];
  398. if (pagevec_page == page) {
  399. SetPageActive(page);
  400. break;
  401. }
  402. }
  403. put_cpu_var(lru_add_pvec);
  404. }
  405. /*
  406. * Mark a page as having seen activity.
  407. *
  408. * inactive,unreferenced -> inactive,referenced
  409. * inactive,referenced -> active,unreferenced
  410. * active,unreferenced -> active,referenced
  411. */
  412. void mark_page_accessed(struct page *page)
  413. {
  414. if (!PageActive(page) && !PageUnevictable(page) &&
  415. PageReferenced(page)) {
  416. /*
  417. * If the page is on the LRU, queue it for activation via
  418. * activate_page_pvecs. Otherwise, assume the page is on a
  419. * pagevec, mark it active and it'll be moved to the active
  420. * LRU on the next drain.
  421. */
  422. if (PageLRU(page))
  423. activate_page(page);
  424. else
  425. __lru_cache_activate_page(page);
  426. ClearPageReferenced(page);
  427. } else if (!PageReferenced(page)) {
  428. SetPageReferenced(page);
  429. }
  430. }
  431. EXPORT_SYMBOL(mark_page_accessed);
  432. /*
  433. * Queue the page for addition to the LRU via pagevec. The decision on whether
  434. * to add the page to the [in]active [file|anon] list is deferred until the
  435. * pagevec is drained. This gives a chance for the caller of __lru_cache_add()
  436. * have the page added to the active list using mark_page_accessed().
  437. */
  438. void __lru_cache_add(struct page *page)
  439. {
  440. struct pagevec *pvec = &get_cpu_var(lru_add_pvec);
  441. page_cache_get(page);
  442. if (!pagevec_space(pvec))
  443. __pagevec_lru_add(pvec);
  444. pagevec_add(pvec, page);
  445. put_cpu_var(lru_add_pvec);
  446. }
  447. EXPORT_SYMBOL(__lru_cache_add);
  448. /**
  449. * lru_cache_add - add a page to a page list
  450. * @page: the page to be added to the LRU.
  451. */
  452. void lru_cache_add(struct page *page)
  453. {
  454. if (PageActive(page)) {
  455. VM_BUG_ON(PageUnevictable(page));
  456. } else if (PageUnevictable(page)) {
  457. VM_BUG_ON(PageActive(page));
  458. }
  459. VM_BUG_ON(PageLRU(page));
  460. __lru_cache_add(page);
  461. }
  462. /**
  463. * add_page_to_unevictable_list - add a page to the unevictable list
  464. * @page: the page to be added to the unevictable list
  465. *
  466. * Add page directly to its zone's unevictable list. To avoid races with
  467. * tasks that might be making the page evictable, through eg. munlock,
  468. * munmap or exit, while it's not on the lru, we want to add the page
  469. * while it's locked or otherwise "invisible" to other tasks. This is
  470. * difficult to do when using the pagevec cache, so bypass that.
  471. */
  472. void add_page_to_unevictable_list(struct page *page)
  473. {
  474. struct zone *zone = page_zone(page);
  475. struct lruvec *lruvec;
  476. spin_lock_irq(&zone->lru_lock);
  477. lruvec = mem_cgroup_page_lruvec(page, zone);
  478. SetPageUnevictable(page);
  479. SetPageLRU(page);
  480. add_page_to_lru_list(page, lruvec, LRU_UNEVICTABLE);
  481. spin_unlock_irq(&zone->lru_lock);
  482. }
  483. /*
  484. * If the page can not be invalidated, it is moved to the
  485. * inactive list to speed up its reclaim. It is moved to the
  486. * head of the list, rather than the tail, to give the flusher
  487. * threads some time to write it out, as this is much more
  488. * effective than the single-page writeout from reclaim.
  489. *
  490. * If the page isn't page_mapped and dirty/writeback, the page
  491. * could reclaim asap using PG_reclaim.
  492. *
  493. * 1. active, mapped page -> none
  494. * 2. active, dirty/writeback page -> inactive, head, PG_reclaim
  495. * 3. inactive, mapped page -> none
  496. * 4. inactive, dirty/writeback page -> inactive, head, PG_reclaim
  497. * 5. inactive, clean -> inactive, tail
  498. * 6. Others -> none
  499. *
  500. * In 4, why it moves inactive's head, the VM expects the page would
  501. * be write it out by flusher threads as this is much more effective
  502. * than the single-page writeout from reclaim.
  503. */
  504. static void lru_deactivate_fn(struct page *page, struct lruvec *lruvec,
  505. void *arg)
  506. {
  507. int lru, file;
  508. bool active;
  509. if (!PageLRU(page))
  510. return;
  511. if (PageUnevictable(page))
  512. return;
  513. /* Some processes are using the page */
  514. if (page_mapped(page))
  515. return;
  516. active = PageActive(page);
  517. file = page_is_file_cache(page);
  518. lru = page_lru_base_type(page);
  519. del_page_from_lru_list(page, lruvec, lru + active);
  520. ClearPageActive(page);
  521. ClearPageReferenced(page);
  522. add_page_to_lru_list(page, lruvec, lru);
  523. if (PageWriteback(page) || PageDirty(page)) {
  524. /*
  525. * PG_reclaim could be raced with end_page_writeback
  526. * It can make readahead confusing. But race window
  527. * is _really_ small and it's non-critical problem.
  528. */
  529. SetPageReclaim(page);
  530. } else {
  531. /*
  532. * The page's writeback ends up during pagevec
  533. * We moves tha page into tail of inactive.
  534. */
  535. list_move_tail(&page->lru, &lruvec->lists[lru]);
  536. __count_vm_event(PGROTATED);
  537. }
  538. if (active)
  539. __count_vm_event(PGDEACTIVATE);
  540. update_page_reclaim_stat(lruvec, file, 0);
  541. }
  542. /*
  543. * Drain pages out of the cpu's pagevecs.
  544. * Either "cpu" is the current CPU, and preemption has already been
  545. * disabled; or "cpu" is being hot-unplugged, and is already dead.
  546. */
  547. void lru_add_drain_cpu(int cpu)
  548. {
  549. struct pagevec *pvec = &per_cpu(lru_add_pvec, cpu);
  550. if (pagevec_count(pvec))
  551. __pagevec_lru_add(pvec);
  552. pvec = &per_cpu(lru_rotate_pvecs, cpu);
  553. if (pagevec_count(pvec)) {
  554. unsigned long flags;
  555. /* No harm done if a racing interrupt already did this */
  556. local_irq_save(flags);
  557. pagevec_move_tail(pvec);
  558. local_irq_restore(flags);
  559. }
  560. pvec = &per_cpu(lru_deactivate_pvecs, cpu);
  561. if (pagevec_count(pvec))
  562. pagevec_lru_move_fn(pvec, lru_deactivate_fn, NULL);
  563. activate_page_drain(cpu);
  564. }
  565. /**
  566. * deactivate_page - forcefully deactivate a page
  567. * @page: page to deactivate
  568. *
  569. * This function hints the VM that @page is a good reclaim candidate,
  570. * for example if its invalidation fails due to the page being dirty
  571. * or under writeback.
  572. */
  573. void deactivate_page(struct page *page)
  574. {
  575. /*
  576. * In a workload with many unevictable page such as mprotect, unevictable
  577. * page deactivation for accelerating reclaim is pointless.
  578. */
  579. if (PageUnevictable(page))
  580. return;
  581. if (likely(get_page_unless_zero(page))) {
  582. struct pagevec *pvec = &get_cpu_var(lru_deactivate_pvecs);
  583. if (!pagevec_add(pvec, page))
  584. pagevec_lru_move_fn(pvec, lru_deactivate_fn, NULL);
  585. put_cpu_var(lru_deactivate_pvecs);
  586. }
  587. }
  588. void lru_add_drain(void)
  589. {
  590. lru_add_drain_cpu(get_cpu());
  591. put_cpu();
  592. }
  593. static void lru_add_drain_per_cpu(struct work_struct *dummy)
  594. {
  595. lru_add_drain();
  596. }
  597. /*
  598. * Returns 0 for success
  599. */
  600. int lru_add_drain_all(void)
  601. {
  602. return schedule_on_each_cpu(lru_add_drain_per_cpu);
  603. }
  604. /*
  605. * Batched page_cache_release(). Decrement the reference count on all the
  606. * passed pages. If it fell to zero then remove the page from the LRU and
  607. * free it.
  608. *
  609. * Avoid taking zone->lru_lock if possible, but if it is taken, retain it
  610. * for the remainder of the operation.
  611. *
  612. * The locking in this function is against shrink_inactive_list(): we recheck
  613. * the page count inside the lock to see whether shrink_inactive_list()
  614. * grabbed the page via the LRU. If it did, give up: shrink_inactive_list()
  615. * will free it.
  616. */
  617. void release_pages(struct page **pages, int nr, int cold)
  618. {
  619. int i;
  620. LIST_HEAD(pages_to_free);
  621. struct zone *zone = NULL;
  622. struct lruvec *lruvec;
  623. unsigned long uninitialized_var(flags);
  624. for (i = 0; i < nr; i++) {
  625. struct page *page = pages[i];
  626. if (unlikely(PageCompound(page))) {
  627. if (zone) {
  628. spin_unlock_irqrestore(&zone->lru_lock, flags);
  629. zone = NULL;
  630. }
  631. put_compound_page(page);
  632. continue;
  633. }
  634. if (!put_page_testzero(page))
  635. continue;
  636. if (PageLRU(page)) {
  637. struct zone *pagezone = page_zone(page);
  638. if (pagezone != zone) {
  639. if (zone)
  640. spin_unlock_irqrestore(&zone->lru_lock,
  641. flags);
  642. zone = pagezone;
  643. spin_lock_irqsave(&zone->lru_lock, flags);
  644. }
  645. lruvec = mem_cgroup_page_lruvec(page, zone);
  646. VM_BUG_ON(!PageLRU(page));
  647. __ClearPageLRU(page);
  648. del_page_from_lru_list(page, lruvec, page_off_lru(page));
  649. }
  650. /* Clear Active bit in case of parallel mark_page_accessed */
  651. ClearPageActive(page);
  652. list_add(&page->lru, &pages_to_free);
  653. }
  654. if (zone)
  655. spin_unlock_irqrestore(&zone->lru_lock, flags);
  656. free_hot_cold_page_list(&pages_to_free, cold);
  657. }
  658. EXPORT_SYMBOL(release_pages);
  659. /*
  660. * The pages which we're about to release may be in the deferred lru-addition
  661. * queues. That would prevent them from really being freed right now. That's
  662. * OK from a correctness point of view but is inefficient - those pages may be
  663. * cache-warm and we want to give them back to the page allocator ASAP.
  664. *
  665. * So __pagevec_release() will drain those queues here. __pagevec_lru_add()
  666. * and __pagevec_lru_add_active() call release_pages() directly to avoid
  667. * mutual recursion.
  668. */
  669. void __pagevec_release(struct pagevec *pvec)
  670. {
  671. lru_add_drain();
  672. release_pages(pvec->pages, pagevec_count(pvec), pvec->cold);
  673. pagevec_reinit(pvec);
  674. }
  675. EXPORT_SYMBOL(__pagevec_release);
  676. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  677. /* used by __split_huge_page_refcount() */
  678. void lru_add_page_tail(struct page *page, struct page *page_tail,
  679. struct lruvec *lruvec, struct list_head *list)
  680. {
  681. int uninitialized_var(active);
  682. enum lru_list lru;
  683. const int file = 0;
  684. VM_BUG_ON(!PageHead(page));
  685. VM_BUG_ON(PageCompound(page_tail));
  686. VM_BUG_ON(PageLRU(page_tail));
  687. VM_BUG_ON(NR_CPUS != 1 &&
  688. !spin_is_locked(&lruvec_zone(lruvec)->lru_lock));
  689. if (!list)
  690. SetPageLRU(page_tail);
  691. if (page_evictable(page_tail)) {
  692. if (PageActive(page)) {
  693. SetPageActive(page_tail);
  694. active = 1;
  695. lru = LRU_ACTIVE_ANON;
  696. } else {
  697. active = 0;
  698. lru = LRU_INACTIVE_ANON;
  699. }
  700. } else {
  701. SetPageUnevictable(page_tail);
  702. lru = LRU_UNEVICTABLE;
  703. }
  704. if (likely(PageLRU(page)))
  705. list_add_tail(&page_tail->lru, &page->lru);
  706. else if (list) {
  707. /* page reclaim is reclaiming a huge page */
  708. get_page(page_tail);
  709. list_add_tail(&page_tail->lru, list);
  710. } else {
  711. struct list_head *list_head;
  712. /*
  713. * Head page has not yet been counted, as an hpage,
  714. * so we must account for each subpage individually.
  715. *
  716. * Use the standard add function to put page_tail on the list,
  717. * but then correct its position so they all end up in order.
  718. */
  719. add_page_to_lru_list(page_tail, lruvec, lru);
  720. list_head = page_tail->lru.prev;
  721. list_move_tail(&page_tail->lru, list_head);
  722. }
  723. if (!PageUnevictable(page))
  724. update_page_reclaim_stat(lruvec, file, active);
  725. }
  726. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  727. static void __pagevec_lru_add_fn(struct page *page, struct lruvec *lruvec,
  728. void *arg)
  729. {
  730. int file = page_is_file_cache(page);
  731. int active = PageActive(page);
  732. enum lru_list lru = page_lru(page);
  733. VM_BUG_ON(PageUnevictable(page));
  734. VM_BUG_ON(PageLRU(page));
  735. SetPageLRU(page);
  736. add_page_to_lru_list(page, lruvec, lru);
  737. update_page_reclaim_stat(lruvec, file, active);
  738. trace_mm_lru_insertion(page, page_to_pfn(page), lru, trace_pagemap_flags(page));
  739. }
  740. /*
  741. * Add the passed pages to the LRU, then drop the caller's refcount
  742. * on them. Reinitialises the caller's pagevec.
  743. */
  744. void __pagevec_lru_add(struct pagevec *pvec)
  745. {
  746. pagevec_lru_move_fn(pvec, __pagevec_lru_add_fn, NULL);
  747. }
  748. EXPORT_SYMBOL(__pagevec_lru_add);
  749. /**
  750. * pagevec_lookup - gang pagecache lookup
  751. * @pvec: Where the resulting pages are placed
  752. * @mapping: The address_space to search
  753. * @start: The starting page index
  754. * @nr_pages: The maximum number of pages
  755. *
  756. * pagevec_lookup() will search for and return a group of up to @nr_pages pages
  757. * in the mapping. The pages are placed in @pvec. pagevec_lookup() takes a
  758. * reference against the pages in @pvec.
  759. *
  760. * The search returns a group of mapping-contiguous pages with ascending
  761. * indexes. There may be holes in the indices due to not-present pages.
  762. *
  763. * pagevec_lookup() returns the number of pages which were found.
  764. */
  765. unsigned pagevec_lookup(struct pagevec *pvec, struct address_space *mapping,
  766. pgoff_t start, unsigned nr_pages)
  767. {
  768. pvec->nr = find_get_pages(mapping, start, nr_pages, pvec->pages);
  769. return pagevec_count(pvec);
  770. }
  771. EXPORT_SYMBOL(pagevec_lookup);
  772. unsigned pagevec_lookup_tag(struct pagevec *pvec, struct address_space *mapping,
  773. pgoff_t *index, int tag, unsigned nr_pages)
  774. {
  775. pvec->nr = find_get_pages_tag(mapping, index, tag,
  776. nr_pages, pvec->pages);
  777. return pagevec_count(pvec);
  778. }
  779. EXPORT_SYMBOL(pagevec_lookup_tag);
  780. /*
  781. * Perform any setup for the swap system
  782. */
  783. void __init swap_setup(void)
  784. {
  785. unsigned long megs = totalram_pages >> (20 - PAGE_SHIFT);
  786. #ifdef CONFIG_SWAP
  787. int i;
  788. bdi_init(swapper_spaces[0].backing_dev_info);
  789. for (i = 0; i < MAX_SWAPFILES; i++) {
  790. spin_lock_init(&swapper_spaces[i].tree_lock);
  791. INIT_LIST_HEAD(&swapper_spaces[i].i_mmap_nonlinear);
  792. }
  793. #endif
  794. /* Use a smaller cluster for small-memory machines */
  795. if (megs < 16)
  796. page_cluster = 2;
  797. else
  798. page_cluster = 3;
  799. /*
  800. * Right now other parts of the system means that we
  801. * _really_ don't want to cluster much more
  802. */
  803. }