From: Yang Shi <shy828301@gmail.com>
To: Andrew Morton <akpm@linux-foundation.org>
Cc: "Kirill A. Shutemov" <kirill.shutemov@linux.intel.com>,
Jann Horn <jannh@google.com>,
Matthew Wilcox <willy@infradead.org>,
David Hildenbrand <david@redhat.com>,
Linux MM <linux-mm@kvack.org>,
Linux Kernel Mailing List <linux-kernel@vger.kernel.org>,
stable <stable@vger.kernel.org>
Subject: Re: [v4 PATCH] fs/proc: task_mmu.c: don't read mapcount for migration entry
Date: Thu, 3 Feb 2022 14:18:56 -0800 [thread overview]
Message-ID: <CAHbLzkpb2Bs8buDOAGCt7hpjy2824HfK3RsTHM+gbzmZ1wvKRA@mail.gmail.com> (raw)
In-Reply-To: <20220203141226.d510a9fe3fb1f55fc75926e5@linux-foundation.org>
On Thu, Feb 3, 2022 at 2:12 PM Andrew Morton <akpm@linux-foundation.org> wrote:
>
> On Thu, 3 Feb 2022 10:26:41 -0800 Yang Shi <shy828301@gmail.com> wrote:
>
> > v4: * s/Treated/Treat per David
> > * Collected acked-by tag from David
> > v3: * Fixed the fix tag, the one used by v2 was not accurate
> > * Added comment about the risk calling page_mapcount() per David
> > * Fix pagemap
> > v2: * Added proper fix tag per Jann Horn
> > * Rebased to the latest linus's tree
>
> The v2->v4 delta shows changes which aren't described above?
They are.
v4: * s/Treated/Treat per David
* Collected acked-by tag from David
v3: * Fixed the fix tag, the one used by v2 was not accurate
* Added comment about the risk calling page_mapcount() per David
* Fix pagemap
>
> --- a/fs/proc/task_mmu.c~fs-proc-task_mmuc-dont-read-mapcount-for-migration-entry-v4
> +++ a/fs/proc/task_mmu.c
> @@ -469,9 +469,12 @@ static void smaps_account(struct mem_siz
> * If any subpage of the compound page mapped with PTE it would elevate
> * page_count().
> *
> - * Treated regular migration entries as mapcount == 1 without reading
> - * mapcount since calling page_mapcount() for migration entries is
> - * racy against THP splitting.
> + * The page_mapcount() is called to get a snapshot of the mapcount.
> + * Without holding the page lock this snapshot can be slightly wrong as
> + * we cannot always read the mapcount atomically. It is not safe to
> + * call page_mapcount() even with PTL held if the page is not mapped,
> + * especially for migration entries. Treat regular migration entries
> + * as mapcount == 1.
> */
> if ((page_count(page) == 1) || migration) {
> smaps_page_accumulate(mss, page, size, size << PSS_SHIFT, dirty,
> @@ -1393,6 +1396,7 @@ static pagemap_entry_t pte_to_pagemap_en
> {
> u64 frame = 0, flags = 0;
> struct page *page = NULL;
> + bool migration = false;
>
> if (pte_present(pte)) {
> if (pm->show_pfn)
> @@ -1414,13 +1418,14 @@ static pagemap_entry_t pte_to_pagemap_en
> frame = swp_type(entry) |
> (swp_offset(entry) << MAX_SWAPFILES_SHIFT);
> flags |= PM_SWAP;
> + migration = is_migration_entry(entry);
> if (is_pfn_swap_entry(entry))
> page = pfn_swap_entry_to_page(entry);
> }
>
> if (page && !PageAnon(page))
> flags |= PM_FILE;
> - if (page && page_mapcount(page) == 1)
> + if (page && !migration && page_mapcount(page) == 1)
> flags |= PM_MMAP_EXCLUSIVE;
> if (vma->vm_flags & VM_SOFTDIRTY)
> flags |= PM_SOFT_DIRTY;
> @@ -1436,6 +1441,7 @@ static int pagemap_pmd_range(pmd_t *pmdp
> spinlock_t *ptl;
> pte_t *pte, *orig_pte;
> int err = 0;
> + bool migration = false;
>
> #ifdef CONFIG_TRANSPARENT_HUGEPAGE
> ptl = pmd_trans_huge_lock(pmdp, vma);
> @@ -1476,11 +1482,12 @@ static int pagemap_pmd_range(pmd_t *pmdp
> if (pmd_swp_uffd_wp(pmd))
> flags |= PM_UFFD_WP;
> VM_BUG_ON(!is_pmd_migration_entry(pmd));
> + migration = is_migration_entry(entry);
> page = pfn_swap_entry_to_page(entry);
> }
> #endif
>
> - if (page && page_mapcount(page) == 1)
> + if (page && !migration && page_mapcount(page) == 1)
> flags |= PM_MMAP_EXCLUSIVE;
>
> for (; addr != end; addr += PAGE_SIZE) {
> _
>
next prev parent reply other threads:[~2022-02-03 22:19 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-02-03 18:26 Yang Shi
2022-02-03 22:12 ` Andrew Morton
2022-02-03 22:18 ` Yang Shi [this message]
2023-03-23 9:52 ` Vlastimil Babka
2023-03-23 10:08 ` David Hildenbrand
2023-03-23 10:11 ` Vlastimil Babka
2023-03-23 20:45 ` Yang Shi
2023-03-24 11:25 ` Vlastimil Babka
2023-03-24 20:12 ` Yang Shi
2023-04-03 7:29 ` David Hildenbrand
2023-04-04 0:50 ` Yang Shi
2023-04-13 23:58 ` David Rientjes
2023-04-18 21:17 ` Yang Shi
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=CAHbLzkpb2Bs8buDOAGCt7hpjy2824HfK3RsTHM+gbzmZ1wvKRA@mail.gmail.com \
--to=shy828301@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=david@redhat.com \
--cc=jannh@google.com \
--cc=kirill.shutemov@linux.intel.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=stable@vger.kernel.org \
--cc=willy@infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox