2021-04-28 22:52:46

by Peter Xu

[permalink] [raw]
Subject: [PATCH 1/6] mm/thp: Simplify copying of huge zero page pmd when fork

Huge zero page is handled in a special path in copy_huge_pmd(), however it
should share most codes with a normal thp page. Trying to share more code with
it by removing the special path. The only leftover so far is the huge zero
page refcounting (mm_get_huge_zero_page()), because that's separately done with
a global counter.

This prepares for a future patch to modify the huge pmd to be installed, so
that we don't need to duplicate it explicitly into huge zero page case too.

Cc: Kirill A. Shutemov <[email protected]>
Signed-off-by: Peter Xu <[email protected]>
---
mm/huge_memory.c | 9 +++------
1 file changed, 3 insertions(+), 6 deletions(-)

diff --git a/mm/huge_memory.c b/mm/huge_memory.c
index 98456017744d6..22bf2d0fff79b 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -1076,17 +1076,13 @@ int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm,
* a page table.
*/
if (is_huge_zero_pmd(pmd)) {
- struct page *zero_page;
/*
* get_huge_zero_page() will never allocate a new page here,
* since we already have a zero page to copy. It just takes a
* reference.
*/
- zero_page = mm_get_huge_zero_page(dst_mm);
- set_huge_zero_page(pgtable, dst_mm, vma, addr, dst_pmd,
- zero_page);
- ret = 0;
- goto out_unlock;
+ mm_get_huge_zero_page(dst_mm);
+ goto out_zero_page;
}

src_page = pmd_page(pmd);
@@ -1110,6 +1106,7 @@ int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm,
get_page(src_page);
page_dup_rmap(src_page, true);
add_mm_counter(dst_mm, MM_ANONPAGES, HPAGE_PMD_NR);
+out_zero_page:
mm_inc_nr_ptes(dst_mm);
pgtable_trans_huge_deposit(dst_mm, dst_pmd, pgtable);

--
2.26.2


2021-04-29 08:08:13

by David Hildenbrand

[permalink] [raw]
Subject: Re: [PATCH 1/6] mm/thp: Simplify copying of huge zero page pmd when fork

On 29.04.21 00:50, Peter Xu wrote:
> Huge zero page is handled in a special path in copy_huge_pmd(), however it
> should share most codes with a normal thp page. Trying to share more code with
> it by removing the special path. The only leftover so far is the huge zero
> page refcounting (mm_get_huge_zero_page()), because that's separately done with
> a global counter.
>
> This prepares for a future patch to modify the huge pmd to be installed, so
> that we don't need to duplicate it explicitly into huge zero page case too.
>
> Cc: Kirill A. Shutemov <[email protected]>
> Signed-off-by: Peter Xu <[email protected]>
> ---
> mm/huge_memory.c | 9 +++------
> 1 file changed, 3 insertions(+), 6 deletions(-)
>
> diff --git a/mm/huge_memory.c b/mm/huge_memory.c
> index 98456017744d6..22bf2d0fff79b 100644
> --- a/mm/huge_memory.c
> +++ b/mm/huge_memory.c
> @@ -1076,17 +1076,13 @@ int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm,
> * a page table.
> */
> if (is_huge_zero_pmd(pmd)) {
> - struct page *zero_page;
> /*
> * get_huge_zero_page() will never allocate a new page here,
> * since we already have a zero page to copy. It just takes a
> * reference.
> */
> - zero_page = mm_get_huge_zero_page(dst_mm);
> - set_huge_zero_page(pgtable, dst_mm, vma, addr, dst_pmd,
> - zero_page);
> - ret = 0;
> - goto out_unlock;
> + mm_get_huge_zero_page(dst_mm);
> + goto out_zero_page;
> }
>
> src_page = pmd_page(pmd);
> @@ -1110,6 +1106,7 @@ int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm,
> get_page(src_page);
> page_dup_rmap(src_page, true);
> add_mm_counter(dst_mm, MM_ANONPAGES, HPAGE_PMD_NR);
> +out_zero_page:
> mm_inc_nr_ptes(dst_mm);
> pgtable_trans_huge_deposit(dst_mm, dst_pmd, pgtable);
>
>

AFAIKs, the only change is that we're now doing an additional

pmdp_set_wrprotect(src_mm, addr, src_pmd)
pmd = pmd_mkold(pmd_wrprotect(pmd));

But as we are copying the zeropage, it should already be
write-protected, so no effective change.

LGTM

--
Thanks,

David / dhildenb