aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoonsoo Kim <iamjoonsoo.kim@lge.com>2016-03-17 17:19:26 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2016-03-17 18:09:34 -0400
commitfe896d1878949ea92ba547587bc3075cc688fb8f (patch)
tree582ae505611bafae117c0de8498916485699ac78
parent444eb2a449ef36fe115431ed7b71467c4563c7f1 (diff)
mm: introduce page reference manipulation functions
The success of CMA allocation largely depends on the success of migration and key factor of it is page reference count. Until now, page reference is manipulated by direct calling atomic functions so we cannot follow up who and where manipulate it. Then, it is hard to find actual reason of CMA allocation failure. CMA allocation should be guaranteed to succeed so finding offending place is really important. In this patch, call sites where page reference is manipulated are converted to introduced wrapper function. This is preparation step to add tracepoint to each page reference manipulation function. With this facility, we can easily find reason of CMA allocation failure. There is no functional change in this patch. In addition, this patch also converts reference read sites. It will help a second step that renames page._count to something else and prevents later attempt to direct access to it (Suggested by Andrew). Signed-off-by: Joonsoo Kim <iamjoonsoo.kim@lge.com> Acked-by: Michal Nazarewicz <mina86@mina86.com> Acked-by: Vlastimil Babka <vbabka@suse.cz> Cc: Minchan Kim <minchan@kernel.org> Cc: Mel Gorman <mgorman@techsingularity.net> Cc: "Kirill A. Shutemov" <kirill.shutemov@linux.intel.com> Cc: Sergey Senozhatsky <sergey.senozhatsky.work@gmail.com> Cc: Steven Rostedt <rostedt@goodmis.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
-rw-r--r--arch/mips/mm/gup.c2
-rw-r--r--arch/powerpc/mm/mmu_context_hash64.c3
-rw-r--r--arch/powerpc/mm/pgtable_64.c2
-rw-r--r--arch/powerpc/platforms/512x/mpc512x_shared.c2
-rw-r--r--arch/x86/mm/gup.c2
-rw-r--r--drivers/block/aoe/aoecmd.c4
-rw-r--r--drivers/net/ethernet/freescale/gianfar.c2
-rw-r--r--drivers/net/ethernet/intel/fm10k/fm10k_main.c2
-rw-r--r--drivers/net/ethernet/intel/igb/igb_main.c2
-rw-r--r--drivers/net/ethernet/intel/ixgbe/ixgbe_main.c2
-rw-r--r--drivers/net/ethernet/intel/ixgbevf/ixgbevf_main.c2
-rw-r--r--drivers/net/ethernet/mellanox/mlx4/en_rx.c9
-rw-r--r--drivers/net/ethernet/sun/niu.c2
-rw-r--r--fs/nilfs2/page.c2
-rw-r--r--include/linux/mm.h25
-rw-r--r--include/linux/page_ref.h85
-rw-r--r--include/linux/pagemap.h19
-rw-r--r--mm/debug.c2
-rw-r--r--mm/huge_memory.c6
-rw-r--r--mm/internal.h7
-rw-r--r--mm/memory_hotplug.c4
-rw-r--r--mm/migrate.c10
-rw-r--r--mm/page_alloc.c12
-rw-r--r--mm/vmscan.c6
-rw-r--r--net/core/sock.c2
25 files changed, 134 insertions, 82 deletions
diff --git a/arch/mips/mm/gup.c b/arch/mips/mm/gup.c
index 1afd87c999b0..6cdffc76735c 100644
--- a/arch/mips/mm/gup.c
+++ b/arch/mips/mm/gup.c
@@ -64,7 +64,7 @@ static inline void get_head_page_multiple(struct page *page, int nr)
64{ 64{
65 VM_BUG_ON(page != compound_head(page)); 65 VM_BUG_ON(page != compound_head(page));
66 VM_BUG_ON(page_count(page) == 0); 66 VM_BUG_ON(page_count(page) == 0);
67 atomic_add(nr, &page->_count); 67 page_ref_add(page, nr);
68 SetPageReferenced(page); 68 SetPageReferenced(page);
69} 69}
70 70
diff --git a/arch/powerpc/mm/mmu_context_hash64.c b/arch/powerpc/mm/mmu_context_hash64.c
index 4e4efbc2658e..9ca6fe16cb29 100644
--- a/arch/powerpc/mm/mmu_context_hash64.c
+++ b/arch/powerpc/mm/mmu_context_hash64.c
@@ -118,8 +118,7 @@ static void destroy_pagetable_page(struct mm_struct *mm)
118 /* drop all the pending references */ 118 /* drop all the pending references */
119 count = ((unsigned long)pte_frag & ~PAGE_MASK) >> PTE_FRAG_SIZE_SHIFT; 119 count = ((unsigned long)pte_frag & ~PAGE_MASK) >> PTE_FRAG_SIZE_SHIFT;
120 /* We allow PTE_FRAG_NR fragments from a PTE page */ 120 /* We allow PTE_FRAG_NR fragments from a PTE page */
121 count = atomic_sub_return(PTE_FRAG_NR - count, &page->_count); 121 if (page_ref_sub_and_test(page, PTE_FRAG_NR - count)) {
122 if (!count) {
123 pgtable_page_dtor(page); 122 pgtable_page_dtor(page);
124 free_hot_cold_page(page, 0); 123 free_hot_cold_page(page, 0);
125 } 124 }
diff --git a/arch/powerpc/mm/pgtable_64.c b/arch/powerpc/mm/pgtable_64.c
index cdf2123d46db..d9cc66cbdbb7 100644
--- a/arch/powerpc/mm/pgtable_64.c
+++ b/arch/powerpc/mm/pgtable_64.c
@@ -403,7 +403,7 @@ static pte_t *__alloc_for_cache(struct mm_struct *mm, int kernel)
403 * count. 403 * count.
404 */ 404 */
405 if (likely(!mm->context.pte_frag)) { 405 if (likely(!mm->context.pte_frag)) {
406 atomic_set(&page->_count, PTE_FRAG_NR); 406 set_page_count(page, PTE_FRAG_NR);
407 mm->context.pte_frag = ret + PTE_FRAG_SIZE; 407 mm->context.pte_frag = ret + PTE_FRAG_SIZE;
408 } 408 }
409 spin_unlock(&mm->page_table_lock); 409 spin_unlock(&mm->page_table_lock);
diff --git a/arch/powerpc/platforms/512x/mpc512x_shared.c b/arch/powerpc/platforms/512x/mpc512x_shared.c
index 711f3d352af7..452da2391153 100644
--- a/arch/powerpc/platforms/512x/mpc512x_shared.c
+++ b/arch/powerpc/platforms/512x/mpc512x_shared.c
@@ -188,7 +188,7 @@ static struct fsl_diu_shared_fb __attribute__ ((__aligned__(8))) diu_shared_fb;
188static inline void mpc512x_free_bootmem(struct page *page) 188static inline void mpc512x_free_bootmem(struct page *page)
189{ 189{
190 BUG_ON(PageTail(page)); 190 BUG_ON(PageTail(page));
191 BUG_ON(atomic_read(&page->_count) > 1); 191 BUG_ON(page_ref_count(page) > 1);
192 free_reserved_page(page); 192 free_reserved_page(page);
193} 193}
194 194
diff --git a/arch/x86/mm/gup.c b/arch/x86/mm/gup.c
index d8a798d8bf50..f8d0b5e8bdfd 100644
--- a/arch/x86/mm/gup.c
+++ b/arch/x86/mm/gup.c
@@ -131,7 +131,7 @@ static inline void get_head_page_multiple(struct page *page, int nr)
131{ 131{
132 VM_BUG_ON_PAGE(page != compound_head(page), page); 132 VM_BUG_ON_PAGE(page != compound_head(page), page);
133 VM_BUG_ON_PAGE(page_count(page) == 0, page); 133 VM_BUG_ON_PAGE(page_count(page) == 0, page);
134 atomic_add(nr, &page->_count); 134 page_ref_add(page, nr);
135 SetPageReferenced(page); 135 SetPageReferenced(page);
136} 136}
137 137
diff --git a/drivers/block/aoe/aoecmd.c b/drivers/block/aoe/aoecmd.c
index d048d2009e89..437b3a822f44 100644
--- a/drivers/block/aoe/aoecmd.c
+++ b/drivers/block/aoe/aoecmd.c
@@ -875,7 +875,7 @@ bio_pageinc(struct bio *bio)
875 * compound pages is no longer allowed by the kernel. 875 * compound pages is no longer allowed by the kernel.
876 */ 876 */
877 page = compound_head(bv.bv_page); 877 page = compound_head(bv.bv_page);
878 atomic_inc(&page->_count); 878 page_ref_inc(page);
879 } 879 }
880} 880}
881 881
@@ -888,7 +888,7 @@ bio_pagedec(struct bio *bio)
888 888
889 bio_for_each_segment(bv, bio, iter) { 889 bio_for_each_segment(bv, bio, iter) {
890 page = compound_head(bv.bv_page); 890 page = compound_head(bv.bv_page);
891 atomic_dec(&page->_count); 891 page_ref_dec(page);
892 } 892 }
893} 893}
894 894
diff --git a/drivers/net/ethernet/freescale/gianfar.c b/drivers/net/ethernet/freescale/gianfar.c
index b9ecf197ad11..f21b2c479780 100644
--- a/drivers/net/ethernet/freescale/gianfar.c
+++ b/drivers/net/ethernet/freescale/gianfar.c
@@ -2944,7 +2944,7 @@ static bool gfar_add_rx_frag(struct gfar_rx_buff *rxb, u32 lstatus,
2944 /* change offset to the other half */ 2944 /* change offset to the other half */
2945 rxb->page_offset ^= GFAR_RXB_TRUESIZE; 2945 rxb->page_offset ^= GFAR_RXB_TRUESIZE;
2946 2946
2947 atomic_inc(&page->_count); 2947 page_ref_inc(page);
2948 2948
2949 return true; 2949 return true;
2950} 2950}
diff --git a/drivers/net/ethernet/intel/fm10k/fm10k_main.c b/drivers/net/ethernet/intel/fm10k/fm10k_main.c
index b243c3cbe68f..b4547ebed774 100644
--- a/drivers/net/ethernet/intel/fm10k/fm10k_main.c
+++ b/drivers/net/ethernet/intel/fm10k/fm10k_main.c
@@ -243,7 +243,7 @@ static bool fm10k_can_reuse_rx_page(struct fm10k_rx_buffer *rx_buffer,
243 /* Even if we own the page, we are not allowed to use atomic_set() 243 /* Even if we own the page, we are not allowed to use atomic_set()
244 * This would break get_page_unless_zero() users. 244 * This would break get_page_unless_zero() users.
245 */ 245 */
246 atomic_inc(&page->_count); 246 page_ref_inc(page);
247 247
248 return true; 248 return true;
249} 249}
diff --git a/drivers/net/ethernet/intel/igb/igb_main.c b/drivers/net/ethernet/intel/igb/igb_main.c
index 31e5f3942839..5b4ad1ad4d5f 100644
--- a/drivers/net/ethernet/intel/igb/igb_main.c
+++ b/drivers/net/ethernet/intel/igb/igb_main.c
@@ -6630,7 +6630,7 @@ static bool igb_can_reuse_rx_page(struct igb_rx_buffer *rx_buffer,
6630 /* Even if we own the page, we are not allowed to use atomic_set() 6630 /* Even if we own the page, we are not allowed to use atomic_set()
6631 * This would break get_page_unless_zero() users. 6631 * This would break get_page_unless_zero() users.
6632 */ 6632 */
6633 atomic_inc(&page->_count); 6633 page_ref_inc(page);
6634 6634
6635 return true; 6635 return true;
6636} 6636}
diff --git a/drivers/net/ethernet/intel/ixgbe/ixgbe_main.c b/drivers/net/ethernet/intel/ixgbe/ixgbe_main.c
index c4003a88bbf6..e6035ff6b861 100644
--- a/drivers/net/ethernet/intel/ixgbe/ixgbe_main.c
+++ b/drivers/net/ethernet/intel/ixgbe/ixgbe_main.c
@@ -1942,7 +1942,7 @@ static bool ixgbe_add_rx_frag(struct ixgbe_ring *rx_ring,
1942 /* Even if we own the page, we are not allowed to use atomic_set() 1942 /* Even if we own the page, we are not allowed to use atomic_set()
1943 * This would break get_page_unless_zero() users. 1943 * This would break get_page_unless_zero() users.
1944 */ 1944 */
1945 atomic_inc(&page->_count); 1945 page_ref_inc(page);
1946 1946
1947 return true; 1947 return true;
1948} 1948}
diff --git a/drivers/net/ethernet/intel/ixgbevf/ixgbevf_main.c b/drivers/net/ethernet/intel/ixgbevf/ixgbevf_main.c
index 3558f019b631..0ea14c0a2e74 100644
--- a/drivers/net/ethernet/intel/ixgbevf/ixgbevf_main.c
+++ b/drivers/net/ethernet/intel/ixgbevf/ixgbevf_main.c
@@ -837,7 +837,7 @@ add_tail_frag:
837 /* Even if we own the page, we are not allowed to use atomic_set() 837 /* Even if we own the page, we are not allowed to use atomic_set()
838 * This would break get_page_unless_zero() users. 838 * This would break get_page_unless_zero() users.
839 */ 839 */
840 atomic_inc(&page->_count); 840 page_ref_inc(page);
841 841
842 return true; 842 return true;
843} 843}
diff --git a/drivers/net/ethernet/mellanox/mlx4/en_rx.c b/drivers/net/ethernet/mellanox/mlx4/en_rx.c
index 41440b2b20a3..86bcfe510e4e 100644
--- a/drivers/net/ethernet/mellanox/mlx4/en_rx.c
+++ b/drivers/net/ethernet/mellanox/mlx4/en_rx.c
@@ -82,8 +82,7 @@ static int mlx4_alloc_pages(struct mlx4_en_priv *priv,
82 /* Not doing get_page() for each frag is a big win 82 /* Not doing get_page() for each frag is a big win
83 * on asymetric workloads. Note we can not use atomic_set(). 83 * on asymetric workloads. Note we can not use atomic_set().
84 */ 84 */
85 atomic_add(page_alloc->page_size / frag_info->frag_stride - 1, 85 page_ref_add(page, page_alloc->page_size / frag_info->frag_stride - 1);
86 &page->_count);
87 return 0; 86 return 0;
88} 87}
89 88
@@ -127,7 +126,7 @@ out:
127 dma_unmap_page(priv->ddev, page_alloc[i].dma, 126 dma_unmap_page(priv->ddev, page_alloc[i].dma,
128 page_alloc[i].page_size, PCI_DMA_FROMDEVICE); 127 page_alloc[i].page_size, PCI_DMA_FROMDEVICE);
129 page = page_alloc[i].page; 128 page = page_alloc[i].page;
130 atomic_set(&page->_count, 1); 129 set_page_count(page, 1);
131 put_page(page); 130 put_page(page);
132 } 131 }
133 } 132 }
@@ -165,7 +164,7 @@ static int mlx4_en_init_allocator(struct mlx4_en_priv *priv,
165 164
166 en_dbg(DRV, priv, " frag %d allocator: - size:%d frags:%d\n", 165 en_dbg(DRV, priv, " frag %d allocator: - size:%d frags:%d\n",
167 i, ring->page_alloc[i].page_size, 166 i, ring->page_alloc[i].page_size,
168 atomic_read(&ring->page_alloc[i].page->_count)); 167 page_ref_count(ring->page_alloc[i].page));
169 } 168 }
170 return 0; 169 return 0;
171 170
@@ -177,7 +176,7 @@ out:
177 dma_unmap_page(priv->ddev, page_alloc->dma, 176 dma_unmap_page(priv->ddev, page_alloc->dma,
178 page_alloc->page_size, PCI_DMA_FROMDEVICE); 177 page_alloc->page_size, PCI_DMA_FROMDEVICE);
179 page = page_alloc->page; 178 page = page_alloc->page;
180 atomic_set(&page->_count, 1); 179 set_page_count(page, 1);
181 put_page(page); 180 put_page(page);
182 page_alloc->page = NULL; 181 page_alloc->page = NULL;
183 } 182 }
diff --git a/drivers/net/ethernet/sun/niu.c b/drivers/net/ethernet/sun/niu.c
index ab6051a43134..9cc45649f477 100644
--- a/drivers/net/ethernet/sun/niu.c
+++ b/drivers/net/ethernet/sun/niu.c
@@ -3341,7 +3341,7 @@ static int niu_rbr_add_page(struct niu *np, struct rx_ring_info *rp,
3341 3341
3342 niu_hash_page(rp, page, addr); 3342 niu_hash_page(rp, page, addr);
3343 if (rp->rbr_blocks_per_page > 1) 3343 if (rp->rbr_blocks_per_page > 1)
3344 atomic_add(rp->rbr_blocks_per_page - 1, &page->_count); 3344 page_ref_add(page, rp->rbr_blocks_per_page - 1);
3345 3345
3346 for (i = 0; i < rp->rbr_blocks_per_page; i++) { 3346 for (i = 0; i < rp->rbr_blocks_per_page; i++) {
3347 __le32 *rbr = &rp->rbr[start_index + i]; 3347 __le32 *rbr = &rp->rbr[start_index + i];
diff --git a/fs/nilfs2/page.c b/fs/nilfs2/page.c
index 45d650addd56..c20df77eff99 100644
--- a/fs/nilfs2/page.c
+++ b/fs/nilfs2/page.c
@@ -180,7 +180,7 @@ void nilfs_page_bug(struct page *page)
180 180
181 printk(KERN_CRIT "NILFS_PAGE_BUG(%p): cnt=%d index#=%llu flags=0x%lx " 181 printk(KERN_CRIT "NILFS_PAGE_BUG(%p): cnt=%d index#=%llu flags=0x%lx "
182 "mapping=%p ino=%lu\n", 182 "mapping=%p ino=%lu\n",
183 page, atomic_read(&page->_count), 183 page, page_ref_count(page),
184 (unsigned long long)page->index, page->flags, m, ino); 184 (unsigned long long)page->index, page->flags, m, ino);
185 185
186 if (page_has_buffers(page)) { 186 if (page_has_buffers(page)) {
diff --git a/include/linux/mm.h b/include/linux/mm.h
index f7fd64227d3a..997fc2e5d9d8 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -22,6 +22,7 @@
22#include <linux/resource.h> 22#include <linux/resource.h>
23#include <linux/page_ext.h> 23#include <linux/page_ext.h>
24#include <linux/err.h> 24#include <linux/err.h>
25#include <linux/page_ref.h>
25 26
26struct mempolicy; 27struct mempolicy;
27struct anon_vma; 28struct anon_vma;
@@ -386,8 +387,8 @@ static inline int pmd_devmap(pmd_t pmd)
386 */ 387 */
387static inline int put_page_testzero(struct page *page) 388static inline int put_page_testzero(struct page *page)
388{ 389{
389 VM_BUG_ON_PAGE(atomic_read(&page->_count) == 0, page); 390 VM_BUG_ON_PAGE(page_ref_count(page) == 0, page);
390 return atomic_dec_and_test(&page->_count); 391 return page_ref_dec_and_test(page);
391} 392}
392 393
393/* 394/*
@@ -398,7 +399,7 @@ static inline int put_page_testzero(struct page *page)
398 */ 399 */
399static inline int get_page_unless_zero(struct page *page) 400static inline int get_page_unless_zero(struct page *page)
400{ 401{
401 return atomic_inc_not_zero(&page->_count); 402 return page_ref_add_unless(page, 1, 0);
402} 403}
403 404
404extern int page_is_ram(unsigned long pfn); 405extern int page_is_ram(unsigned long pfn);
@@ -486,11 +487,6 @@ static inline int total_mapcount(struct page *page)
486} 487}
487#endif 488#endif
488 489
489static inline int page_count(struct page *page)
490{
491 return atomic_read(&compound_head(page)->_count);
492}
493
494static inline struct page *virt_to_head_page(const void *x) 490static inline struct page *virt_to_head_page(const void *x)
495{ 491{
496 struct page *page = virt_to_page(x); 492 struct page *page = virt_to_page(x);
@@ -498,15 +494,6 @@ static inline struct page *virt_to_head_page(const void *x)
498 return compound_head(page); 494 return compound_head(page);
499} 495}
500 496
501/*
502 * Setup the page count before being freed into the page allocator for
503 * the first time (boot or memory hotplug)
504 */
505static inline void init_page_count(struct page *page)
506{
507 atomic_set(&page->_count, 1);
508}
509
510void __put_page(struct page *page); 497void __put_page(struct page *page);
511 498
512void put_pages_list(struct list_head *pages); 499void put_pages_list(struct list_head *pages);
@@ -716,8 +703,8 @@ static inline void get_page(struct page *page)
716 * Getting a normal page or the head of a compound page 703 * Getting a normal page or the head of a compound page
717 * requires to already have an elevated page->_count. 704 * requires to already have an elevated page->_count.
718 */ 705 */
719 VM_BUG_ON_PAGE(atomic_read(&page->_count) <= 0, page); 706 VM_BUG_ON_PAGE(page_ref_count(page) <= 0, page);
720 atomic_inc(&page->_count); 707 page_ref_inc(page);
721 708
722 if (unlikely(is_zone_device_page(page))) 709 if (unlikely(is_zone_device_page(page)))
723 get_zone_device_page(page); 710 get_zone_device_page(page);
diff --git a/include/linux/page_ref.h b/include/linux/page_ref.h
new file mode 100644
index 000000000000..30f5817f6b8e
--- /dev/null
+++ b/include/linux/page_ref.h
@@ -0,0 +1,85 @@
1#ifndef _LINUX_PAGE_REF_H
2#define _LINUX_PAGE_REF_H
3
4#include <linux/atomic.h>
5#include <linux/mm_types.h>
6#include <linux/page-flags.h>
7
8static inline int page_ref_count(struct page *page)
9{
10 return atomic_read(&page->_count);
11}
12
13static inline int page_count(struct page *page)
14{
15 return atomic_read(&compound_head(page)->_count);
16}
17
18static inline void set_page_count(struct page *page, int v)
19{
20 atomic_set(&page->_count, v);
21}
22
23/*
24 * Setup the page count before being freed into the page allocator for
25 * the first time (boot or memory hotplug)
26 */
27static inline void init_page_count(struct page *page)
28{
29 set_page_count(page, 1);
30}
31
32static inline void page_ref_add(struct page *page, int nr)
33{
34 atomic_add(nr, &page->_count);
35}
36
37static inline void page_ref_sub(struct page *page, int nr)
38{
39 atomic_sub(nr, &page->_count);
40}
41
42static inline void page_ref_inc(struct page *page)
43{
44 atomic_inc(&page->_count);
45}
46
47static inline void page_ref_dec(struct page *page)
48{
49 atomic_dec(&page->_count);
50}
51
52static inline int page_ref_sub_and_test(struct page *page, int nr)
53{
54 return atomic_sub_and_test(nr, &page->_count);
55}
56
57static inline int page_ref_dec_and_test(struct page *page)
58{
59 return atomic_dec_and_test(&page->_count);
60}
61
62static inline int page_ref_dec_return(struct page *page)
63{
64 return atomic_dec_return(&page->_count);
65}
66
67static inline int page_ref_add_unless(struct page *page, int nr, int u)
68{
69 return atomic_add_unless(&page->_count, nr, u);
70}
71
72static inline int page_ref_freeze(struct page *page, int count)
73{
74 return likely(atomic_cmpxchg(&page->_count, count, 0) == count);
75}
76
77static inline void page_ref_unfreeze(struct page *page, int count)
78{
79 VM_BUG_ON_PAGE(page_count(page) != 0, page);
80 VM_BUG_ON(count == 0);
81
82 atomic_set(&page->_count, count);
83}
84
85#endif
diff --git a/include/linux/pagemap.h b/include/linux/pagemap.h
index 183b15ea052b..1ebd65c91422 100644
--- a/include/linux/pagemap.h
+++ b/include/linux/pagemap.h
@@ -165,7 +165,7 @@ static inline int page_cache_get_speculative(struct page *page)
165 * SMP requires. 165 * SMP requires.
166 */ 166 */
167 VM_BUG_ON_PAGE(page_count(page) == 0, page); 167 VM_BUG_ON_PAGE(page_count(page) == 0, page);
168 atomic_inc(&page->_count); 168 page_ref_inc(page);
169 169
170#else 170#else
171 if (unlikely(!get_page_unless_zero(page))) { 171 if (unlikely(!get_page_unless_zero(page))) {
@@ -194,10 +194,10 @@ static inline int page_cache_add_speculative(struct page *page, int count)
194 VM_BUG_ON(!in_atomic()); 194 VM_BUG_ON(!in_atomic());
195# endif 195# endif
196 VM_BUG_ON_PAGE(page_count(page) == 0, page); 196 VM_BUG_ON_PAGE(page_count(page) == 0, page);
197 atomic_add(count, &page->_count); 197 page_ref_add(page, count);
198 198
199#else 199#else
200 if (unlikely(!atomic_add_unless(&page->_count, count, 0))) 200 if (unlikely(!page_ref_add_unless(page, count, 0)))
201 return 0; 201 return 0;
202#endif 202#endif
203 VM_BUG_ON_PAGE(PageCompound(page) && page != compound_head(page), page); 203 VM_BUG_ON_PAGE(PageCompound(page) && page != compound_head(page), page);
@@ -205,19 +205,6 @@ static inline int page_cache_add_speculative(struct page *page, int count)
205 return 1; 205 return 1;
206} 206}
207 207
208static inline int page_freeze_refs(struct page *page, int count)
209{
210 return likely(atomic_cmpxchg(&page->_count, count, 0) == count);
211}
212
213static inline void page_unfreeze_refs(struct page *page, int count)
214{
215 VM_BUG_ON_PAGE(page_count(page) != 0, page);
216 VM_BUG_ON(count == 0);
217
218 atomic_set(&page->_count, count);
219}
220
221#ifdef CONFIG_NUMA 208#ifdef CONFIG_NUMA
222extern struct page *__page_cache_alloc(gfp_t gfp); 209extern struct page *__page_cache_alloc(gfp_t gfp);
223#else 210#else
diff --git a/mm/debug.c b/mm/debug.c
index df7247b0b532..8865bfb41b0b 100644
--- a/mm/debug.c
+++ b/mm/debug.c
@@ -43,7 +43,7 @@ const struct trace_print_flags vmaflag_names[] = {
43void __dump_page(struct page *page, const char *reason) 43void __dump_page(struct page *page, const char *reason)
44{ 44{
45 pr_emerg("page:%p count:%d mapcount:%d mapping:%p index:%#lx", 45 pr_emerg("page:%p count:%d mapcount:%d mapping:%p index:%#lx",
46 page, atomic_read(&page->_count), page_mapcount(page), 46 page, page_ref_count(page), page_mapcount(page),
47 page->mapping, page->index); 47 page->mapping, page->index);
48 if (PageCompound(page)) 48 if (PageCompound(page))
49 pr_cont(" compound_mapcount: %d", compound_mapcount(page)); 49 pr_cont(" compound_mapcount: %d", compound_mapcount(page));
diff --git a/mm/huge_memory.c b/mm/huge_memory.c
index e08b1659ff19..bb944c771c82 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -2888,7 +2888,7 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,
2888 2888
2889 page = pmd_page(*pmd); 2889 page = pmd_page(*pmd);
2890 VM_BUG_ON_PAGE(!page_count(page), page); 2890 VM_BUG_ON_PAGE(!page_count(page), page);
2891 atomic_add(HPAGE_PMD_NR - 1, &page->_count); 2891 page_ref_add(page, HPAGE_PMD_NR - 1);
2892 write = pmd_write(*pmd); 2892 write = pmd_write(*pmd);
2893 young = pmd_young(*pmd); 2893 young = pmd_young(*pmd);
2894 dirty = pmd_dirty(*pmd); 2894 dirty = pmd_dirty(*pmd);
@@ -3257,7 +3257,7 @@ static void __split_huge_page_tail(struct page *head, int tail,
3257 struct page *page_tail = head + tail; 3257 struct page *page_tail = head + tail;
3258 3258
3259 VM_BUG_ON_PAGE(atomic_read(&page_tail->_mapcount) != -1, page_tail); 3259 VM_BUG_ON_PAGE(atomic_read(&page_tail->_mapcount) != -1, page_tail);
3260 VM_BUG_ON_PAGE(atomic_read(&page_tail->_count) != 0, page_tail); 3260 VM_BUG_ON_PAGE(page_ref_count(page_tail) != 0, page_tail);
3261 3261
3262 /* 3262 /*
3263 * tail_page->_count is zero and not changing from under us. But 3263 * tail_page->_count is zero and not changing from under us. But
@@ -3270,7 +3270,7 @@ static void __split_huge_page_tail(struct page *head, int tail,
3270 * atomic_set() here would be safe on all archs (and not only on x86), 3270 * atomic_set() here would be safe on all archs (and not only on x86),
3271 * it's safer to use atomic_inc(). 3271 * it's safer to use atomic_inc().
3272 */ 3272 */
3273 atomic_inc(&page_tail->_count); 3273 page_ref_inc(page_tail);
3274 3274
3275 page_tail->flags &= ~PAGE_FLAGS_CHECK_AT_PREP; 3275 page_tail->flags &= ~PAGE_FLAGS_CHECK_AT_PREP;
3276 page_tail->flags |= (head->flags & 3276 page_tail->flags |= (head->flags &
diff --git a/mm/internal.h b/mm/internal.h
index 4042a8a05672..57d7b0e839f0 100644
--- a/mm/internal.h
+++ b/mm/internal.h
@@ -38,11 +38,6 @@
38void free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *start_vma, 38void free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *start_vma,
39 unsigned long floor, unsigned long ceiling); 39 unsigned long floor, unsigned long ceiling);
40 40
41static inline void set_page_count(struct page *page, int v)
42{
43 atomic_set(&page->_count, v);
44}
45
46extern int __do_page_cache_readahead(struct address_space *mapping, 41extern int __do_page_cache_readahead(struct address_space *mapping,
47 struct file *filp, pgoff_t offset, unsigned long nr_to_read, 42 struct file *filp, pgoff_t offset, unsigned long nr_to_read,
48 unsigned long lookahead_size); 43 unsigned long lookahead_size);
@@ -64,7 +59,7 @@ static inline unsigned long ra_submit(struct file_ra_state *ra,
64static inline void set_page_refcounted(struct page *page) 59static inline void set_page_refcounted(struct page *page)
65{ 60{
66 VM_BUG_ON_PAGE(PageTail(page), page); 61 VM_BUG_ON_PAGE(PageTail(page), page);
67 VM_BUG_ON_PAGE(atomic_read(&page->_count), page); 62 VM_BUG_ON_PAGE(page_ref_count(page), page);
68 set_page_count(page, 1); 63 set_page_count(page, 1);
69} 64}
70 65
diff --git a/mm/memory_hotplug.c b/mm/memory_hotplug.c
index c832ef3565cc..e62aa078f5c9 100644
--- a/mm/memory_hotplug.c
+++ b/mm/memory_hotplug.c
@@ -167,7 +167,7 @@ void get_page_bootmem(unsigned long info, struct page *page,
167 page->lru.next = (struct list_head *) type; 167 page->lru.next = (struct list_head *) type;
168 SetPagePrivate(page); 168 SetPagePrivate(page);
169 set_page_private(page, info); 169 set_page_private(page, info);
170 atomic_inc(&page->_count); 170 page_ref_inc(page);
171} 171}
172 172
173void put_page_bootmem(struct page *page) 173void put_page_bootmem(struct page *page)
@@ -178,7 +178,7 @@ void put_page_bootmem(struct page *page)
178 BUG_ON(type < MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE || 178 BUG_ON(type < MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE ||
179 type > MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE); 179 type > MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE);
180 180
181 if (atomic_dec_return(&page->_count) == 1) { 181 if (page_ref_dec_return(page) == 1) {
182 ClearPagePrivate(page); 182 ClearPagePrivate(page);
183 set_page_private(page, 0); 183 set_page_private(page, 0);
184 INIT_LIST_HEAD(&page->lru); 184 INIT_LIST_HEAD(&page->lru);
diff --git a/mm/migrate.c b/mm/migrate.c
index fdaf0818fb30..577c94b8e959 100644
--- a/mm/migrate.c
+++ b/mm/migrate.c
@@ -349,7 +349,7 @@ int migrate_page_move_mapping(struct address_space *mapping,
349 return -EAGAIN; 349 return -EAGAIN;
350 } 350 }
351 351
352 if (!page_freeze_refs(page, expected_count)) { 352 if (!page_ref_freeze(page, expected_count)) {
353 spin_unlock_irq(&mapping->tree_lock); 353 spin_unlock_irq(&mapping->tree_lock);
354 return -EAGAIN; 354 return -EAGAIN;
355 } 355 }
@@ -363,7 +363,7 @@ int migrate_page_move_mapping(struct address_space *mapping,
363 */ 363 */
364 if (mode == MIGRATE_ASYNC && head && 364 if (mode == MIGRATE_ASYNC && head &&
365 !buffer_migrate_lock_buffers(head, mode)) { 365 !buffer_migrate_lock_buffers(head, mode)) {
366 page_unfreeze_refs(page, expected_count); 366 page_ref_unfreeze(page, expected_count);
367 spin_unlock_irq(&mapping->tree_lock); 367 spin_unlock_irq(&mapping->tree_lock);
368 return -EAGAIN; 368 return -EAGAIN;
369 } 369 }
@@ -397,7 +397,7 @@ int migrate_page_move_mapping(struct address_space *mapping,
397 * to one less reference. 397 * to one less reference.
398 * We know this isn't the last reference. 398 * We know this isn't the last reference.
399 */ 399 */
400 page_unfreeze_refs(page, expected_count - 1); 400 page_ref_unfreeze(page, expected_count - 1);
401 401
402 spin_unlock(&mapping->tree_lock); 402 spin_unlock(&mapping->tree_lock);
403 /* Leave irq disabled to prevent preemption while updating stats */ 403 /* Leave irq disabled to prevent preemption while updating stats */
@@ -451,7 +451,7 @@ int migrate_huge_page_move_mapping(struct address_space *mapping,
451 return -EAGAIN; 451 return -EAGAIN;
452 } 452 }
453 453
454 if (!page_freeze_refs(page, expected_count)) { 454 if (!page_ref_freeze(page, expected_count)) {
455 spin_unlock_irq(&mapping->tree_lock); 455 spin_unlock_irq(&mapping->tree_lock);
456 return -EAGAIN; 456 return -EAGAIN;
457 } 457 }
@@ -463,7 +463,7 @@ int migrate_huge_page_move_mapping(struct address_space *mapping,
463 463
464 radix_tree_replace_slot(pslot, newpage); 464 radix_tree_replace_slot(pslot, newpage);
465 465
466 page_unfreeze_refs(page, expected_count - 1); 466 page_ref_unfreeze(page, expected_count - 1);
467 467
468 spin_unlock_irq(&mapping->tree_lock); 468 spin_unlock_irq(&mapping->tree_lock);
469 469
diff --git a/mm/page_alloc.c b/mm/page_alloc.c
index 096a00d98a45..30134a8f7cc8 100644
--- a/mm/page_alloc.c
+++ b/mm/page_alloc.c
@@ -766,7 +766,7 @@ static inline int free_pages_check(struct page *page)
766 bad_reason = "nonzero mapcount"; 766 bad_reason = "nonzero mapcount";
767 if (unlikely(page->mapping != NULL)) 767 if (unlikely(page->mapping != NULL))
768 bad_reason = "non-NULL mapping"; 768 bad_reason = "non-NULL mapping";
769 if (unlikely(atomic_read(&page->_count) != 0)) 769 if (unlikely(page_ref_count(page) != 0))
770 bad_reason = "nonzero _count"; 770 bad_reason = "nonzero _count";
771 if (unlikely(page->flags & PAGE_FLAGS_CHECK_AT_FREE)) { 771 if (unlikely(page->flags & PAGE_FLAGS_CHECK_AT_FREE)) {
772 bad_reason = "PAGE_FLAGS_CHECK_AT_FREE flag(s) set"; 772 bad_reason = "PAGE_FLAGS_CHECK_AT_FREE flag(s) set";
@@ -1462,7 +1462,7 @@ static inline int check_new_page(struct page *page)
1462 bad_reason = "nonzero mapcount"; 1462 bad_reason = "nonzero mapcount";
1463 if (unlikely(page->mapping != NULL)) 1463 if (unlikely(page->mapping != NULL))
1464 bad_reason = "non-NULL mapping"; 1464 bad_reason = "non-NULL mapping";
1465 if (unlikely(atomic_read(&page->_count) != 0)) 1465 if (unlikely(page_ref_count(page) != 0))
1466 bad_reason = "nonzero _count"; 1466 bad_reason = "nonzero _count";
1467 if (unlikely(page->flags & __PG_HWPOISON)) { 1467 if (unlikely(page->flags & __PG_HWPOISON)) {
1468 bad_reason = "HWPoisoned (hardware-corrupted)"; 1468 bad_reason = "HWPoisoned (hardware-corrupted)";
@@ -3475,7 +3475,7 @@ refill:
3475 /* Even if we own the page, we do not use atomic_set(). 3475 /* Even if we own the page, we do not use atomic_set().
3476 * This would break get_page_unless_zero() users. 3476 * This would break get_page_unless_zero() users.
3477 */ 3477 */
3478 atomic_add(size - 1, &page->_count); 3478 page_ref_add(page, size - 1);
3479 3479
3480 /* reset page count bias and offset to start of new frag */ 3480 /* reset page count bias and offset to start of new frag */
3481 nc->pfmemalloc = page_is_pfmemalloc(page); 3481 nc->pfmemalloc = page_is_pfmemalloc(page);
@@ -3487,7 +3487,7 @@ refill:
3487 if (unlikely(offset < 0)) { 3487 if (unlikely(offset < 0)) {
3488 page = virt_to_page(nc->va); 3488 page = virt_to_page(nc->va);
3489 3489
3490 if (!atomic_sub_and_test(nc->pagecnt_bias, &page->_count)) 3490 if (!page_ref_sub_and_test(page, nc->pagecnt_bias))
3491 goto refill; 3491 goto refill;
3492 3492
3493#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) 3493#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE)
@@ -3495,7 +3495,7 @@ refill:
3495 size = nc->size; 3495 size = nc->size;
3496#endif 3496#endif
3497 /* OK, page count is 0, we can safely set it */ 3497 /* OK, page count is 0, we can safely set it */
3498 atomic_set(&page->_count, size); 3498 set_page_count(page, size);
3499 3499
3500 /* reset page count bias and offset to start of new frag */ 3500 /* reset page count bias and offset to start of new frag */
3501 nc->pagecnt_bias = size; 3501 nc->pagecnt_bias = size;
@@ -6852,7 +6852,7 @@ bool has_unmovable_pages(struct zone *zone, struct page *page, int count,
6852 * This check already skips compound tails of THP 6852 * This check already skips compound tails of THP
6853 * because their page->_count is zero at all time. 6853 * because their page->_count is zero at all time.
6854 */ 6854 */
6855 if (!atomic_read(&page->_count)) { 6855 if (!page_ref_count(page)) {
6856 if (PageBuddy(page)) 6856 if (PageBuddy(page))
6857 iter += (1 << page_order(page)) - 1; 6857 iter += (1 << page_order(page)) - 1;
6858 continue; 6858 continue;
diff --git a/mm/vmscan.c b/mm/vmscan.c
index b41b82d4bab1..b934223eaa45 100644
--- a/mm/vmscan.c
+++ b/mm/vmscan.c
@@ -638,11 +638,11 @@ static int __remove_mapping(struct address_space *mapping, struct page *page,
638 * Note that if SetPageDirty is always performed via set_page_dirty, 638 * Note that if SetPageDirty is always performed via set_page_dirty,
639 * and thus under tree_lock, then this ordering is not required. 639 * and thus under tree_lock, then this ordering is not required.
640 */ 640 */
641 if (!page_freeze_refs(page, 2)) 641 if (!page_ref_freeze(page, 2))
642 goto cannot_free; 642 goto cannot_free;
643 /* note: atomic_cmpxchg in page_freeze_refs provides the smp_rmb */ 643 /* note: atomic_cmpxchg in page_freeze_refs provides the smp_rmb */
644 if (unlikely(PageDirty(page))) { 644 if (unlikely(PageDirty(page))) {
645 page_unfreeze_refs(page, 2); 645 page_ref_unfreeze(page, 2);
646 goto cannot_free; 646 goto cannot_free;
647 } 647 }
648 648
@@ -704,7 +704,7 @@ int remove_mapping(struct address_space *mapping, struct page *page)
704 * drops the pagecache ref for us without requiring another 704 * drops the pagecache ref for us without requiring another
705 * atomic operation. 705 * atomic operation.
706 */ 706 */
707 page_unfreeze_refs(page, 1); 707 page_ref_unfreeze(page, 1);
708 return 1; 708 return 1;
709 } 709 }
710 return 0; 710 return 0;
diff --git a/net/core/sock.c b/net/core/sock.c
index 6c1c8bc93412..67e7efe12ff7 100644
--- a/net/core/sock.c
+++ b/net/core/sock.c
@@ -1903,7 +1903,7 @@ EXPORT_SYMBOL(sock_cmsg_send);
1903bool skb_page_frag_refill(unsigned int sz, struct page_frag *pfrag, gfp_t gfp) 1903bool skb_page_frag_refill(unsigned int sz, struct page_frag *pfrag, gfp_t gfp)
1904{ 1904{
1905 if (pfrag->page) { 1905 if (pfrag->page) {
1906 if (atomic_read(&pfrag->page->_count) == 1) { 1906 if (page_ref_count(pfrag->page) == 1) {
1907 pfrag->offset = 0; 1907 pfrag->offset = 0;
1908 return true; 1908 return true;
1909 } 1909 }