mm: differentiate page_mapped() from page_mapcount() for compound pages
authorKirill A. Shutemov <kirill.shutemov@linux.intel.com>
Sat, 16 Jan 2016 00:53:46 +0000 (16:53 -0800)
committerLinus Torvalds <torvalds@linux-foundation.org>
Sat, 16 Jan 2016 01:56:32 +0000 (17:56 -0800)
Let's define page_mapped() to be true for compound pages if any
sub-pages of the compound page is mapped (with PMD or PTE).

On other hand page_mapcount() return mapcount for this particular small
page.

This will make cases like page_get_anon_vma() behave correctly once we
allow huge pages to be mapped with PTE.

Most users outside core-mm should use page_mapcount() instead of
page_mapped().

Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
Tested-by: Sasha Levin <sasha.levin@oracle.com>
Tested-by: Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
Acked-by: Jerome Marchand <jmarchan@redhat.com>
Cc: Vlastimil Babka <vbabka@suse.cz>
Cc: Andrea Arcangeli <aarcange@redhat.com>
Cc: Hugh Dickins <hughd@google.com>
Cc: Dave Hansen <dave.hansen@intel.com>
Cc: Mel Gorman <mgorman@suse.de>
Cc: Rik van Riel <riel@redhat.com>
Cc: Naoya Horiguchi <n-horiguchi@ah.jp.nec.com>
Cc: Steve Capper <steve.capper@linaro.org>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Michal Hocko <mhocko@suse.cz>
Cc: Christoph Lameter <cl@linux.com>
Cc: David Rientjes <rientjes@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
arch/arc/mm/cache.c
arch/arm/mm/flush.c
arch/mips/mm/c-r4k.c
arch/mips/mm/cache.c
arch/mips/mm/init.c
arch/sh/mm/cache-sh4.c
arch/sh/mm/cache.c
arch/xtensa/mm/tlb.c
fs/proc/page.c
include/linux/mm.h
mm/filemap.c

index ff7ff6cbb8112408c05a38a2f8e001265d5d3726..b65f797e9ad6723abd7c38bba09e382df52450b4 100644 (file)
@@ -617,7 +617,7 @@ void flush_dcache_page(struct page *page)
         */
        if (!mapping_mapped(mapping)) {
                clear_bit(PG_dc_clean, &page->flags);
-       } else if (page_mapped(page)) {
+       } else if (page_mapcount(page)) {
 
                /* kernel reading from page with U-mapping */
                phys_addr_t paddr = (unsigned long)page_address(page);
@@ -857,7 +857,7 @@ void copy_user_highpage(struct page *to, struct page *from,
         * For !VIPT cache, all of this gets compiled out as
         * addr_not_cache_congruent() is 0
         */
-       if (page_mapped(from) && addr_not_cache_congruent(kfrom, u_vaddr)) {
+       if (page_mapcount(from) && addr_not_cache_congruent(kfrom, u_vaddr)) {
                __flush_dcache_page((unsigned long)kfrom, u_vaddr);
                clean_src_k_mappings = 1;
        }
index c2acebb7eedcf8cc9ad2ff2acbd5cdf98138e937..d0ba3551d49a4b05371db7a12c02e151be9d4eae 100644 (file)
@@ -330,7 +330,7 @@ void flush_dcache_page(struct page *page)
        mapping = page_mapping(page);
 
        if (!cache_ops_need_broadcast() &&
-           mapping && !page_mapped(page))
+           mapping && !page_mapcount(page))
                clear_bit(PG_dcache_clean, &page->flags);
        else {
                __flush_dcache_page(mapping, page);
index 5d3a25e1cfaea62cf7859f3408e3d101f9bf4060..caac3d747a909dbd75d9f0935b8a67766795174c 100644 (file)
@@ -587,7 +587,8 @@ static inline void local_r4k_flush_cache_page(void *args)
                 * another ASID than the current one.
                 */
                map_coherent = (cpu_has_dc_aliases &&
-                               page_mapped(page) && !Page_dcache_dirty(page));
+                               page_mapcount(page) &&
+                               !Page_dcache_dirty(page));
                if (map_coherent)
                        vaddr = kmap_coherent(page, addr);
                else
index aab218c36e0d3e2f7669c47343e583e527103169..3f159caf6dbc902d20d2284913aea498c67cd596 100644 (file)
@@ -106,7 +106,7 @@ void __flush_anon_page(struct page *page, unsigned long vmaddr)
        unsigned long addr = (unsigned long) page_address(page);
 
        if (pages_do_alias(addr, vmaddr)) {
-               if (page_mapped(page) && !Page_dcache_dirty(page)) {
+               if (page_mapcount(page) && !Page_dcache_dirty(page)) {
                        void *kaddr;
 
                        kaddr = kmap_coherent(page, vmaddr);
index 8770e619185eb034b317ce3de837c5185ba05511..7e5fa0938c2174cefe9d5bfb0f04249626b14af1 100644 (file)
@@ -165,7 +165,7 @@ void copy_user_highpage(struct page *to, struct page *from,
 
        vto = kmap_atomic(to);
        if (cpu_has_dc_aliases &&
-           page_mapped(from) && !Page_dcache_dirty(from)) {
+           page_mapcount(from) && !Page_dcache_dirty(from)) {
                vfrom = kmap_coherent(from, vaddr);
                copy_page(vto, vfrom);
                kunmap_coherent();
@@ -187,7 +187,7 @@ void copy_to_user_page(struct vm_area_struct *vma,
        unsigned long len)
 {
        if (cpu_has_dc_aliases &&
-           page_mapped(page) && !Page_dcache_dirty(page)) {
+           page_mapcount(page) && !Page_dcache_dirty(page)) {
                void *vto = kmap_coherent(page, vaddr) + (vaddr & ~PAGE_MASK);
                memcpy(vto, src, len);
                kunmap_coherent();
@@ -205,7 +205,7 @@ void copy_from_user_page(struct vm_area_struct *vma,
        unsigned long len)
 {
        if (cpu_has_dc_aliases &&
-           page_mapped(page) && !Page_dcache_dirty(page)) {
+           page_mapcount(page) && !Page_dcache_dirty(page)) {
                void *vfrom = kmap_coherent(page, vaddr) + (vaddr & ~PAGE_MASK);
                memcpy(dst, vfrom, len);
                kunmap_coherent();
index 51d8f7f31d1d797392ab2813f4fb3d4d33480598..58aaa4f33b8129b8dd7e8118ec2691478cf79757 100644 (file)
@@ -241,7 +241,7 @@ static void sh4_flush_cache_page(void *args)
                 */
                map_coherent = (current_cpu_data.dcache.n_aliases &&
                        test_bit(PG_dcache_clean, &page->flags) &&
-                       page_mapped(page));
+                       page_mapcount(page));
                if (map_coherent)
                        vaddr = kmap_coherent(page, address);
                else
index f770e3992620e8a1673ee1a2bd47280be55cccd4..e58cfbf4515008c32f670519b0904bb4aafad706 100644 (file)
@@ -59,7 +59,7 @@ void copy_to_user_page(struct vm_area_struct *vma, struct page *page,
                       unsigned long vaddr, void *dst, const void *src,
                       unsigned long len)
 {
-       if (boot_cpu_data.dcache.n_aliases && page_mapped(page) &&
+       if (boot_cpu_data.dcache.n_aliases && page_mapcount(page) &&
            test_bit(PG_dcache_clean, &page->flags)) {
                void *vto = kmap_coherent(page, vaddr) + (vaddr & ~PAGE_MASK);
                memcpy(vto, src, len);
@@ -78,7 +78,7 @@ void copy_from_user_page(struct vm_area_struct *vma, struct page *page,
                         unsigned long vaddr, void *dst, const void *src,
                         unsigned long len)
 {
-       if (boot_cpu_data.dcache.n_aliases && page_mapped(page) &&
+       if (boot_cpu_data.dcache.n_aliases && page_mapcount(page) &&
            test_bit(PG_dcache_clean, &page->flags)) {
                void *vfrom = kmap_coherent(page, vaddr) + (vaddr & ~PAGE_MASK);
                memcpy(dst, vfrom, len);
@@ -97,7 +97,7 @@ void copy_user_highpage(struct page *to, struct page *from,
 
        vto = kmap_atomic(to);
 
-       if (boot_cpu_data.dcache.n_aliases && page_mapped(from) &&
+       if (boot_cpu_data.dcache.n_aliases && page_mapcount(from) &&
            test_bit(PG_dcache_clean, &from->flags)) {
                vfrom = kmap_coherent(from, vaddr);
                copy_page(vto, vfrom);
@@ -153,7 +153,7 @@ void __flush_anon_page(struct page *page, unsigned long vmaddr)
        unsigned long addr = (unsigned long) page_address(page);
 
        if (pages_do_alias(addr, vmaddr)) {
-               if (boot_cpu_data.dcache.n_aliases && page_mapped(page) &&
+               if (boot_cpu_data.dcache.n_aliases && page_mapcount(page) &&
                    test_bit(PG_dcache_clean, &page->flags)) {
                        void *kaddr;
 
index 5ece856c5725c7cc72d0a0175bf9229330fabec5..35c822286bbe8b194e654cceef50972c260922c6 100644 (file)
@@ -245,7 +245,7 @@ static int check_tlb_entry(unsigned w, unsigned e, bool dtlb)
                                                page_mapcount(p));
                                if (!page_count(p))
                                        rc |= TLB_INSANE;
-                               else if (page_mapped(p))
+                               else if (page_mapcount(p))
                                        rc |= TLB_SUSPICIOUS;
                        } else {
                                rc |= TLB_INSANE;
index 93484034a03d04c38cc5ff7779fb95e7611fbd09..b2855eea54050655818a424b850bc4d94d5d8f47 100644 (file)
@@ -103,9 +103,9 @@ u64 stable_page_flags(struct page *page)
         * pseudo flags for the well known (anonymous) memory mapped pages
         *
         * Note that page->_mapcount is overloaded in SLOB/SLUB/SLQB, so the
-        * simple test in page_mapped() is not enough.
+        * simple test in page_mapcount() is not enough.
         */
-       if (!PageSlab(page) && page_mapped(page))
+       if (!PageSlab(page) && page_mapcount(page))
                u |= 1 << KPF_MMAP;
        if (PageAnon(page))
                u |= 1 << KPF_ANON;
index 67e0fab225e8bf68c04dc9c0e07d7ef3d2e49595..6b56cfd9fd09191ecdc4efdf09f968a0ac38f4e0 100644 (file)
@@ -953,10 +953,21 @@ static inline pgoff_t page_file_index(struct page *page)
 
 /*
  * Return true if this page is mapped into pagetables.
+ * For compound page it returns true if any subpage of compound page is mapped.
  */
-static inline int page_mapped(struct page *page)
+static inline bool page_mapped(struct page *page)
 {
-       return atomic_read(&(page)->_mapcount) + compound_mapcount(page) >= 0;
+       int i;
+       if (likely(!PageCompound(page)))
+               return atomic_read(&page->_mapcount) >= 0;
+       page = compound_head(page);
+       if (atomic_read(compound_mapcount_ptr(page)) >= 0)
+               return true;
+       for (i = 0; i < hpage_nr_pages(page); i++) {
+               if (atomic_read(&page[i]._mapcount) >= 0)
+                       return true;
+       }
+       return false;
 }
 
 /*
index a729345ed6ec37cb480dfdcd5c885b21940b86d4..847ee43c28068a0fb744fe124c1d8afa429d0719 100644 (file)
@@ -204,7 +204,7 @@ void __delete_from_page_cache(struct page *page, void *shadow,
                __dec_zone_page_state(page, NR_FILE_PAGES);
        if (PageSwapBacked(page))
                __dec_zone_page_state(page, NR_SHMEM);
-       BUG_ON(page_mapped(page));
+       VM_BUG_ON_PAGE(page_mapped(page), page);
 
        /*
         * At this point page must be either written or cleaned by truncate.