Skip to contentSkip to conversation

mm/thp: Update mm's MM_ANONPAGES stat in set_huge_zero_page()

RFC In review Anshuman Khandual 3 participants 6 messages May 13, 2021 – May 17, 2021

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

RFC 1/1 mm/thp: Update mm's MM_ANONPAGES stat in set_huge_zero_page()

Although the zero huge page is being shared across various processes, each mapping needs to update its mm's MM_ANONPAGES stat by HPAGE_PMD_NR in order to be consistent. This just updates the stats in set_huge_zero_page() after the mapping gets created.

---
Should it update MM_SHMEM_PAGES instead ? Applies on latest mainline.

 mm/huge_memory.c | 1 +
 1 file changed, 1 insertion(+)
mm/huge_memory.c +1 -0
index 63ed6b25deaa..262703304807 100644
@@ -706,6 +706,7 @@ static void set_huge_zero_page(pgtable_t pgtable, struct mm_struct *mm,
 	if (pgtable)
 		pgtable_trans_huge_deposit(mm, pmd, pgtable);
 	set_pmd_at(mm, haddr, pmd, entry);
+	add_mm_counter(mm, MM_ANONPAGES, HPAGE_PMD_NR);
 	mm_inc_nr_ptes(mm);
 }
 
Signature · 1 line
2.20.1
reviewer
#1 lore raw
Quoted 4 lines · Anshuman Khandual

Although the zero huge page is being shared across various processes, each mapping needs to update its mm's MM_ANONPAGES stat by HPAGE_PMD_NR in order to be consistent. This just updates the stats in set_huge_zero_page() after the mapping gets created.

In addition, MM_ANONPAGES stats should be decreased at zap_huge_pmd() and __split_huge_pmd_locked() when the zero huge page mapping is removed from a process, right?

Quoted 8 lines

---
Should it update MM_SHMEM_PAGES instead ? Applies on latest mainline.

zero huge page is added via do_huge_pmd_anonymous_page(), I think MM_ANONPAGES is appropriate.

Quoted 18 lines
 mm/huge_memory.c | 1 +
 1 file changed, 1 insertion(+)
mm/huge_memory.c +1 -0
index 63ed6b25deaa..262703304807 100644
@@ -706,6 +706,7 @@ static void set_huge_zero_page(pgtable_t pgtable, struct mm_struct *mm,
 	if (pgtable)
 		pgtable_trans_huge_deposit(mm, pmd, pgtable);
 	set_pmd_at(mm, haddr, pmd, entry);
+	add_mm_counter(mm, MM_ANONPAGES, HPAGE_PMD_NR);
 	mm_inc_nr_ptes(mm);
 }
 
Signature · 1 line
2.20.1

—
Best Regards,
Yan Zi

#2 lore raw
Quoted 8 lines · Zi Yan
Quoted 4 lines · Anshuman Khandual

Although the zero huge page is being shared across various processes, each mapping needs to update its mm's MM_ANONPAGES stat by HPAGE_PMD_NR in order to be consistent. This just updates the stats in set_huge_zero_page() after the mapping gets created.

In addition, MM_ANONPAGES stats should be decreased at zap_huge_pmd() and

Right, would something like this work ?

mm/huge_memory.c +1 -0
index 63ed6b2..776984d 100644
@@ -1678,6 +1678,7 @@ int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
                        tlb_remove_page_size(tlb, pmd_page(orig_pmd), HPAGE_PMD_SIZE);
        } else if (is_huge_zero_pmd(orig_pmd)) {
                zap_deposited_table(tlb->mm, pmd);
+               add_mm_counter(tlb->mm, MM_ANONPAGES, -HPAGE_PMD_NR);
                spin_unlock(ptl);
                tlb_remove_page_size(tlb, pmd_page(orig_pmd), HPAGE_PMD_SIZE);
        } else {
Quoted 2 lines

__split_huge_pmd_locked() when the zero huge page mapping is removed from a process, right?

__split_huge_pmd_locked() calls __split_huge_zero_page_pmd() which will replace a zero huge page with multiple (HPAGE_PMD_NR) zero small pages. Why should MM_ANONPAGES stats change for the MM when the mapping is still out there but in normal pages now.

Quoted 10 lines
Quoted 9 lines

---
Should it update MM_SHMEM_PAGES instead ? Applies on latest mainline.

Quoted 2 lines

zero huge page is added via do_huge_pmd_anonymous_page(), I think MM_ANONPAGES is appropriate.

Okay, sure.

Quoted 25 lines
Quoted 19 lines

 mm/huge_memory.c | 1 +
 1 file changed, 1 insertion(+)
mm/huge_memory.c +1 -0
index 63ed6b25deaa..262703304807 100644
@@ -706,6 +706,7 @@ static void set_huge_zero_page(pgtable_t pgtable, struct mm_struct *mm,
 	if (pgtable)
 		pgtable_trans_huge_deposit(mm, pmd, pgtable);
 	set_pmd_at(mm, haddr, pmd, entry);
+	add_mm_counter(mm, MM_ANONPAGES, HPAGE_PMD_NR);
 	mm_inc_nr_ptes(mm);
 }
 
Signature · 1 line
2.20.1

—
Best Regards,
Yan Zi

reviewer
#3 lore raw
Quoted 18 lines · Anshuman Khandual
Quoted 8 lines · Zi Yan
Quoted 4 lines · Anshuman Khandual

Although the zero huge page is being shared across various processes, each mapping needs to update its mm's MM_ANONPAGES stat by HPAGE_PMD_NR in order to be consistent. This just updates the stats in set_huge_zero_page() after the mapping gets created.

In addition, MM_ANONPAGES stats should be decreased at zap_huge_pmd() and

Right, would something like this work ?

mm/huge_memory.c +0 -0
index 63ed6b2..776984d 100644

mm/huge_memory.c int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma, · line 1684

Quoted 7 lines
mm/huge_memory.c +1 -0
@@ -1678,6 +1678,7 @@ int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
                        tlb_remove_page_size(tlb, pmd_page(orig_pmd), HPAGE_PMD_SIZE);
        } else if (is_huge_zero_pmd(orig_pmd)) {
                zap_deposited_table(tlb->mm, pmd);
+               add_mm_counter(tlb->mm, MM_ANONPAGES, -HPAGE_PMD_NR);
                spin_unlock(ptl);
                tlb_remove_page_size(tlb, pmd_page(orig_pmd), HPAGE_PMD_SIZE);
        } else {

LGTM.

Quoted 7 lines
Quoted 2 lines

__split_huge_pmd_locked() when the zero huge page mapping is removed from a process, right?

__split_huge_pmd_locked() calls __split_huge_zero_page_pmd() which will replace a zero huge page with multiple (HPAGE_PMD_NR) zero small pages. Why should MM_ANONPAGES stats change for the MM when the mapping is still out there but in normal pages now.

Ah, you are right. I missed this part. No need to change __split_huge_pmd_locked().

Quoted 10 lines
Quoted 10 lines
Quoted 9 lines

---
Should it update MM_SHMEM_PAGES instead ? Applies on latest mainline.

Quoted 30 lines
Quoted 2 lines

zero huge page is added via do_huge_pmd_anonymous_page(), I think MM_ANONPAGES is appropriate.

Okay, sure.

Quoted 25 lines
Quoted 19 lines

 mm/huge_memory.c | 1 +
 1 file changed, 1 insertion(+)
mm/huge_memory.c +1 -0
index 63ed6b25deaa..262703304807 100644
@@ -706,6 +706,7 @@ static void set_huge_zero_page(pgtable_t pgtable, struct mm_struct *mm,
 	if (pgtable)
 		pgtable_trans_huge_deposit(mm, pmd, pgtable);
 	set_pmd_at(mm, haddr, pmd, entry);
+	add_mm_counter(mm, MM_ANONPAGES, HPAGE_PMD_NR);
 	mm_inc_nr_ptes(mm);
 }
 
Signature · 1 line
2.20.1

—
Best Regards,
Yan Zi

—
Best Regards,
Yan, Zi

#4 lore raw
In reply to Anshuman Khandual
Quoted 5 lines · Anshuman Khandual

Although the zero huge page is being shared across various processes, each mapping needs to update its mm's MM_ANONPAGES stat by HPAGE_PMD_NR in order to be consistent. This just updates the stats in set_huge_zero_page() after the mapping gets created.

I don't get why MM_ANONPAGES needs to be inc'ed when huge zero page is installed. This may cause inconsistency between some counters, for example, MM_ANONPAGES may be much bigger than anon LRU.

MM_ANONPAGES should not be inc'ed unless a new page is allocated and installed, right?

Quoted 28 lines

---
Should it update MM_SHMEM_PAGES instead ? Applies on latest mainline.

 mm/huge_memory.c | 1 +
 1 file changed, 1 insertion(+)
mm/huge_memory.c +1 -1
index 63ed6b25deaa..262703304807 100644
@@ -706,6 +706,7 @@ static void set_huge_zero_page(pgtable_t pgtable, struct mm_struct *mm,
        if (pgtable)
                pgtable_trans_huge_deposit(mm, pmd, pgtable);
        set_pmd_at(mm, haddr, pmd, entry);
+       add_mm_counter(mm, MM_ANONPAGES, HPAGE_PMD_NR);
        mm_inc_nr_ptes(mm);
 }
 
--

2.20.1

#5 lore raw
Quoted 9 lines · Yang Shi
Quoted 5 lines · Anshuman Khandual

Although the zero huge page is being shared across various processes, each mapping needs to update its mm's MM_ANONPAGES stat by HPAGE_PMD_NR in order to be consistent. This just updates the stats in set_huge_zero_page() after the mapping gets created.

Quoted 6 lines

I don't get why MM_ANONPAGES needs to be inc'ed when huge zero page is installed. This may cause inconsistency between some counters, for example, MM_ANONPAGES may be much bigger than anon LRU.

MM_ANONPAGES should not be inc'ed unless a new page is allocated and installed, right?

I just realized I mixed MM_ANONPAGES up with the global anon pages counter. Take back my comment. Sorry for the confusion.

Quoted 29 lines
Quoted 29 lines

---
Should it update MM_SHMEM_PAGES instead ? Applies on latest mainline.

 mm/huge_memory.c | 1 +
 1 file changed, 1 insertion(+)
mm/huge_memory.c +1 -1
index 63ed6b25deaa..262703304807 100644
@@ -706,6 +706,7 @@ static void set_huge_zero_page(pgtable_t pgtable, struct mm_struct *mm,
        if (pgtable)
                pgtable_trans_huge_deposit(mm, pmd, pgtable);
        set_pmd_at(mm, haddr, pmd, entry);
+       add_mm_counter(mm, MM_ANONPAGES, HPAGE_PMD_NR);
        mm_inc_nr_ptes(mm);
 }
 
--

2.20.1