swap.c 23 KB

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