swap.c 23 KB

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