linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH mmotm] mm/thp: refix __split_huge_pmd_locked() for migration PMD
@ 2022-03-03  1:43 Hugh Dickins
  2022-03-03 19:51 ` Yang Shi
  0 siblings, 1 reply; 3+ messages in thread
From: Hugh Dickins @ 2022-03-03  1:43 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Ralph Campbell, Yang Shi, Zi Yan, Kirill A. Shutemov,
	linux-kernel, linux-mm

Migration entries do not contribute to a page's reference count: move
__split_huge_pmd_locked()'s page_ref_add() into pmd_migration's else
block (along with the page_count() check - a page is quite likely to
to have reference count frozen to 0 when a migration entry is found).

This will fix a very rare anonymous memory leak, after a split_huge_pmd()
raced with an anon split_huge_page() or an anon THP migrate_pages(): since
the wrongly raised refcount stopped the page (perhaps small, perhaps huge,
depending on when the race hit) from ever being freed.  At first I thought
there were worse risks, from prematurely unfreezing a frozen page: but now
think that would only affect page cache pages, which do not come this way
(except for anonymous pages in swap cache, perhaps).

Fixes: ec0abae6dcdf ("mm/thp: fix __split_huge_pmd_locked() for migration PMD")
Signed-off-by: Hugh Dickins <hughd@google.com>
---
That's an unfair "Fixes": it did not introduce the problem, but it
missed this aspect of the problem; and will be a good guide to where this
refix should go if stable backports are asked for.

 mm/huge_memory.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -2039,9 +2039,9 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,
 		young = pmd_young(old_pmd);
 		soft_dirty = pmd_soft_dirty(old_pmd);
 		uffd_wp = pmd_uffd_wp(old_pmd);
+		VM_BUG_ON_PAGE(!page_count(page), page);
+		page_ref_add(page, HPAGE_PMD_NR - 1);
 	}
-	VM_BUG_ON_PAGE(!page_count(page), page);
-	page_ref_add(page, HPAGE_PMD_NR - 1);
 
 	/*
 	 * Withdraw the table only after we mark the pmd entry invalid.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH mmotm] mm/thp: refix __split_huge_pmd_locked() for migration PMD
  2022-03-03  1:43 [PATCH mmotm] mm/thp: refix __split_huge_pmd_locked() for migration PMD Hugh Dickins
@ 2022-03-03 19:51 ` Yang Shi
  2022-03-03 22:45   ` Hugh Dickins
  0 siblings, 1 reply; 3+ messages in thread
From: Yang Shi @ 2022-03-03 19:51 UTC (permalink / raw)
  To: Hugh Dickins
  Cc: Andrew Morton, Ralph Campbell, Zi Yan, Kirill A. Shutemov,
	Linux Kernel Mailing List, Linux MM

On Wed, Mar 2, 2022 at 5:43 PM Hugh Dickins <hughd@google.com> wrote:
>
> Migration entries do not contribute to a page's reference count: move
> __split_huge_pmd_locked()'s page_ref_add() into pmd_migration's else
> block (along with the page_count() check - a page is quite likely to
> to have reference count frozen to 0 when a migration entry is found).
>
> This will fix a very rare anonymous memory leak, after a split_huge_pmd()
> raced with an anon split_huge_page() or an anon THP migrate_pages(): since
> the wrongly raised refcount stopped the page (perhaps small, perhaps huge,
> depending on when the race hit) from ever being freed.  At first I thought
> there were worse risks, from prematurely unfreezing a frozen page: but now
> think that would only affect page cache pages, which do not come this way
> (except for anonymous pages in swap cache, perhaps).

Thanks for catching this. I agree there may be anon memory leak due to
bumped refcount. But I don't think it could affect page cache page
since that code (bumping refcount) is never called for page cache page
IIUC.

The patch looks good to me. Reviewed-by: Yang Shi <shy828301@gmail.com>

>
> Fixes: ec0abae6dcdf ("mm/thp: fix __split_huge_pmd_locked() for migration PMD")
> Signed-off-by: Hugh Dickins <hughd@google.com>
> ---
> That's an unfair "Fixes": it did not introduce the problem, but it
> missed this aspect of the problem; and will be a good guide to where this
> refix should go if stable backports are asked for.
>
>  mm/huge_memory.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
>
> --- a/mm/huge_memory.c
> +++ b/mm/huge_memory.c
> @@ -2039,9 +2039,9 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,
>                 young = pmd_young(old_pmd);
>                 soft_dirty = pmd_soft_dirty(old_pmd);
>                 uffd_wp = pmd_uffd_wp(old_pmd);
> +               VM_BUG_ON_PAGE(!page_count(page), page);
> +               page_ref_add(page, HPAGE_PMD_NR - 1);
>         }
> -       VM_BUG_ON_PAGE(!page_count(page), page);
> -       page_ref_add(page, HPAGE_PMD_NR - 1);
>
>         /*
>          * Withdraw the table only after we mark the pmd entry invalid.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH mmotm] mm/thp: refix __split_huge_pmd_locked() for migration PMD
  2022-03-03 19:51 ` Yang Shi
@ 2022-03-03 22:45   ` Hugh Dickins
  0 siblings, 0 replies; 3+ messages in thread
From: Hugh Dickins @ 2022-03-03 22:45 UTC (permalink / raw)
  To: Yang Shi
  Cc: Hugh Dickins, Andrew Morton, Ralph Campbell, Zi Yan,
	Kirill A. Shutemov, Linux Kernel Mailing List, Linux MM

On Thu, 3 Mar 2022, Yang Shi wrote:
> On Wed, Mar 2, 2022 at 5:43 PM Hugh Dickins <hughd@google.com> wrote:
> >
> > Migration entries do not contribute to a page's reference count: move
> > __split_huge_pmd_locked()'s page_ref_add() into pmd_migration's else
> > block (along with the page_count() check - a page is quite likely to
> > to have reference count frozen to 0 when a migration entry is found).
> >
> > This will fix a very rare anonymous memory leak, after a split_huge_pmd()
> > raced with an anon split_huge_page() or an anon THP migrate_pages(): since
> > the wrongly raised refcount stopped the page (perhaps small, perhaps huge,
> > depending on when the race hit) from ever being freed.  At first I thought
> > there were worse risks, from prematurely unfreezing a frozen page: but now
> > think that would only affect page cache pages, which do not come this way
> > (except for anonymous pages in swap cache, perhaps).
> 
> Thanks for catching this. I agree there may be anon memory leak due to
> bumped refcount. But I don't think it could affect page cache page
> since that code (bumping refcount) is never called for page cache page
> IIUC.

Yes, that's what I meant by unfreezing "would only affect page cache pages,
which do not come this way".  But then remembered that anonymous pages in
swap cache also have non-NULL page_mapping(), and involve freezing in the
same way that page cache pages do.  Now, offhand I forget the status of
hugepage swap, so maybe that's impossible, hence "perhaps".  Easier to
fix than to think through all the ramifications of not fixing, as usual.

> 
> The patch looks good to me. Reviewed-by: Yang Shi <shy828301@gmail.com>

Thanks,
Hugh

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-03-03 22:45 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-03  1:43 [PATCH mmotm] mm/thp: refix __split_huge_pmd_locked() for migration PMD Hugh Dickins
2022-03-03 19:51 ` Yang Shi
2022-03-03 22:45   ` Hugh Dickins

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).