Re: [PATCH V3 2/4] mm/debug_vm_pgtable: Add tests validating advanced arch page table helpers
From: Anshuman Khandual
Date: Mon Jun 29 2020 - 14:55:09 EST
On 06/27/2020 12:56 PM, Christophe Leroy wrote:
>
>
> Le 15/06/2020 Ã 05:37, Anshuman Khandual a ÃcritÂ:
>> This adds new tests validating for these following arch advanced page table
>> helpers. These tests create and test specific mapping types at various page
>> table levels.
>>
>> 1. pxxp_set_wrprotect()
>> 2. pxxp_get_and_clear()
>> 3. pxxp_set_access_flags()
>> 4. pxxp_get_and_clear_full()
>> 5. pxxp_test_and_clear_young()
>> 6. pxx_leaf()
>> 7. pxx_set_huge()
>> 8. pxx_(clear|mk)_savedwrite()
>> 9. huge_pxxp_xxx()
>>
>> Cc: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx>
>> Cc: Mike Rapoport <rppt@xxxxxxxxxxxxx>
>> Cc: Vineet Gupta <vgupta@xxxxxxxxxxxx>
>> Cc: Catalin Marinas <catalin.marinas@xxxxxxx>
>> Cc: Will Deacon <will@xxxxxxxxxx>
>> Cc: Benjamin Herrenschmidt <benh@xxxxxxxxxxxxxxxxxxx>
>> Cc: Paul Mackerras <paulus@xxxxxxxxx>
>> Cc: Michael Ellerman <mpe@xxxxxxxxxxxxxx>
>> Cc: Heiko Carstens <heiko.carstens@xxxxxxxxxx>
>> Cc: Vasily Gorbik <gor@xxxxxxxxxxxxx>
>> Cc: Christian Borntraeger <borntraeger@xxxxxxxxxx>
>> Cc: Thomas Gleixner <tglx@xxxxxxxxxxxxx>
>> Cc: Ingo Molnar <mingo@xxxxxxxxxx>
>> Cc: Borislav Petkov <bp@xxxxxxxxx>
>> Cc: "H. Peter Anvin" <hpa@xxxxxxxxx>
>> Cc: Kirill A. Shutemov <kirill@xxxxxxxxxxxxx>
>> Cc: Paul Walmsley <paul.walmsley@xxxxxxxxxx>
>> Cc: Palmer Dabbelt <palmer@xxxxxxxxxxx>
>> Cc: linux-snps-arc@xxxxxxxxxxxxxxxxxxx
>> Cc: linux-arm-kernel@xxxxxxxxxxxxxxxxxxx
>> Cc: linuxppc-dev@xxxxxxxxxxxxxxxx
>> Cc: linux-s390@xxxxxxxxxxxxxxx
>> Cc: linux-riscv@xxxxxxxxxxxxxxxxxxx
>> Cc: x86@xxxxxxxxxx
>> Cc: linux-mm@xxxxxxxxx
>> Cc: linux-arch@xxxxxxxxxxxxxxx
>> Cc: linux-kernel@xxxxxxxxxxxxxxx
>> Suggested-by: Catalin Marinas <catalin.marinas@xxxxxxx>
>> Signed-off-by: Anshuman Khandual <anshuman.khandual@xxxxxxx>
>> ---
>> Â mm/debug_vm_pgtable.c | 306 ++++++++++++++++++++++++++++++++++++++++++
>> Â 1 file changed, 306 insertions(+)
>>
>> diff --git a/mm/debug_vm_pgtable.c b/mm/debug_vm_pgtable.c
>> index ffa163d4c63c..e3f9f8317a98 100644
>> --- a/mm/debug_vm_pgtable.c
>> +++ b/mm/debug_vm_pgtable.c
>> @@ -21,6 +21,7 @@
>> Â #include <linux/module.h>
>> Â #include <linux/pfn_t.h>
>> Â #include <linux/printk.h>
>> +#include <linux/pgtable.h>
>> Â #include <linux/random.h>
>> Â #include <linux/spinlock.h>
>> Â #include <linux/swap.h>
>> @@ -28,6 +29,7 @@
>> Â #include <linux/start_kernel.h>
>> Â #include <linux/sched/mm.h>
>> Â #include <asm/pgalloc.h>
>> +#include <asm/tlbflush.h>
>> Â Â #define VMFLAGSÂÂÂ (VM_READ|VM_WRITE|VM_EXEC)
>> Â @@ -55,6 +57,54 @@ static void __init pte_basic_tests(unsigned long pfn, pgprot_t prot)
>> ÂÂÂÂÂ WARN_ON(pte_write(pte_wrprotect(pte_mkwrite(pte))));
>> Â }
>> Â +static void __init pte_advanced_tests(struct mm_struct *mm,
>> +ÂÂÂÂÂÂÂÂÂÂÂ struct vm_area_struct *vma, pte_t *ptep,
>> +ÂÂÂÂÂÂÂÂÂÂÂ unsigned long pfn, unsigned long vaddr, pgprot_t prot)
>
> Align args properly.
>
>> +{
>> +ÂÂÂ pte_t pte = pfn_pte(pfn, prot);
>> +
>> +ÂÂÂ pte = pfn_pte(pfn, prot);
>> +ÂÂÂ set_pte_at(mm, vaddr, ptep, pte);
>> +ÂÂÂ ptep_set_wrprotect(mm, vaddr, ptep);
>> +ÂÂÂ pte = READ_ONCE(*ptep);
>> +ÂÂÂ WARN_ON(pte_write(pte));
>> +
>> +ÂÂÂ pte = pfn_pte(pfn, prot);
>> +ÂÂÂ set_pte_at(mm, vaddr, ptep, pte);
>> +ÂÂÂ ptep_get_and_clear(mm, vaddr, ptep);
>> +ÂÂÂ pte = READ_ONCE(*ptep);
>> +ÂÂÂ WARN_ON(!pte_none(pte));
>> +
>> +ÂÂÂ pte = pfn_pte(pfn, prot);
>> +ÂÂÂ pte = pte_wrprotect(pte);
>> +ÂÂÂ pte = pte_mkclean(pte);
>> +ÂÂÂ set_pte_at(mm, vaddr, ptep, pte);
>> +ÂÂÂ pte = pte_mkwrite(pte);
>> +ÂÂÂ pte = pte_mkdirty(pte);
>> +ÂÂÂ ptep_set_access_flags(vma, vaddr, ptep, pte, 1);
>> +ÂÂÂ pte = READ_ONCE(*ptep);
>> +ÂÂÂ WARN_ON(!(pte_write(pte) && pte_dirty(pte)));
>> +
>> +ÂÂÂ pte = pfn_pte(pfn, prot);
>> +ÂÂÂ set_pte_at(mm, vaddr, ptep, pte);
>> +ÂÂÂ ptep_get_and_clear_full(mm, vaddr, ptep, 1);
>> +ÂÂÂ pte = READ_ONCE(*ptep);
>> +ÂÂÂ WARN_ON(!pte_none(pte));
>> +
>> +ÂÂÂ pte = pte_mkyoung(pte);
>> +ÂÂÂ set_pte_at(mm, vaddr, ptep, pte);
>> +ÂÂÂ ptep_test_and_clear_young(vma, vaddr, ptep);
>> +ÂÂÂ pte = READ_ONCE(*ptep);
>> +ÂÂÂ WARN_ON(pte_young(pte));
>> +}
>> +
>> +static void __init pte_savedwrite_tests(unsigned long pfn, pgprot_t prot)
>> +{
>> +ÂÂÂ pte_t pte = pfn_pte(pfn, prot);
>> +
>> +ÂÂÂ WARN_ON(!pte_savedwrite(pte_mk_savedwrite(pte_clear_savedwrite(pte))));
>> +ÂÂÂ WARN_ON(pte_savedwrite(pte_clear_savedwrite(pte_mk_savedwrite(pte))));
>> +}
>> Â #ifdef CONFIG_TRANSPARENT_HUGEPAGE
>> Â static void __init pmd_basic_tests(unsigned long pfn, pgprot_t prot)
>> Â {
>> @@ -77,6 +127,89 @@ static void __init pmd_basic_tests(unsigned long pfn, pgprot_t prot)
>> ÂÂÂÂÂ WARN_ON(!pmd_bad(pmd_mkhuge(pmd)));
>> Â }
>> Â +static void __init pmd_advanced_tests(struct mm_struct *mm,
>> +ÂÂÂÂÂÂÂ struct vm_area_struct *vma, pmd_t *pmdp,
>> +ÂÂÂÂÂÂÂ unsigned long pfn, unsigned long vaddr, pgprot_t prot)
>
> Align args properly
>
>> +{
>> +ÂÂÂ pmd_t pmd = pfn_pmd(pfn, prot);
>> +
>> +ÂÂÂ if (!has_transparent_hugepage())
>> +ÂÂÂÂÂÂÂ return;
>> +
>> +ÂÂÂ /* Align the address wrt HPAGE_PMD_SIZE */
>> +ÂÂÂ vaddr = (vaddr & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE;
>> +
>> +ÂÂÂ pmd = pfn_pmd(pfn, prot);
>> +ÂÂÂ set_pmd_at(mm, vaddr, pmdp, pmd);
>> +ÂÂÂ pmdp_set_wrprotect(mm, vaddr, pmdp);
>> +ÂÂÂ pmd = READ_ONCE(*pmdp);
>> +ÂÂÂ WARN_ON(pmd_write(pmd));
>> +
>> +ÂÂÂ pmd = pfn_pmd(pfn, prot);
>> +ÂÂÂ set_pmd_at(mm, vaddr, pmdp, pmd);
>> +ÂÂÂ pmdp_huge_get_and_clear(mm, vaddr, pmdp);
>> +ÂÂÂ pmd = READ_ONCE(*pmdp);
>> +ÂÂÂ WARN_ON(!pmd_none(pmd));
>> +
>> +ÂÂÂ pmd = pfn_pmd(pfn, prot);
>> +ÂÂÂ pmd = pmd_wrprotect(pmd);
>> +ÂÂÂ pmd = pmd_mkclean(pmd);
>> +ÂÂÂ set_pmd_at(mm, vaddr, pmdp, pmd);
>> +ÂÂÂ pmd = pmd_mkwrite(pmd);
>> +ÂÂÂ pmd = pmd_mkdirty(pmd);
>> +ÂÂÂ pmdp_set_access_flags(vma, vaddr, pmdp, pmd, 1);
>> +ÂÂÂ pmd = READ_ONCE(*pmdp);
>> +ÂÂÂ WARN_ON(!(pmd_write(pmd) && pmd_dirty(pmd)));
>> +
>> +ÂÂÂ pmd = pmd_mkhuge(pfn_pmd(pfn, prot));
>> +ÂÂÂ set_pmd_at(mm, vaddr, pmdp, pmd);
>> +ÂÂÂ pmdp_huge_get_and_clear_full(vma, vaddr, pmdp, 1);
>> +ÂÂÂ pmd = READ_ONCE(*pmdp);
>> +ÂÂÂ WARN_ON(!pmd_none(pmd));
>> +
>> +ÂÂÂ pmd = pmd_mkyoung(pmd);
>> +ÂÂÂ set_pmd_at(mm, vaddr, pmdp, pmd);
>> +ÂÂÂ pmdp_test_and_clear_young(vma, vaddr, pmdp);
>> +ÂÂÂ pmd = READ_ONCE(*pmdp);
>> +ÂÂÂ WARN_ON(pmd_young(pmd));
>> +}
>> +
>> +static void __init pmd_leaf_tests(unsigned long pfn, pgprot_t prot)
>> +{
>> +ÂÂÂ pmd_t pmd = pfn_pmd(pfn, prot);
>> +
>> +ÂÂÂ /*
>> +ÂÂÂÂ * PMD based THP is a leaf entry.
>> +ÂÂÂÂ */
>> +ÂÂÂ pmd = pmd_mkhuge(pmd);
>> +ÂÂÂ WARN_ON(!pmd_leaf(pmd));
>> +}
>> +
>> +static void __init pmd_huge_tests(pmd_t *pmdp, unsigned long pfn, pgprot_t prot)
>> +{
>> +ÂÂÂ pmd_t pmd;
>> +
>> +ÂÂÂ if (!IS_ENABLED(CONFIG_HAVE_ARCH_HUGE_VMAP))
>> +ÂÂÂÂÂÂÂ return;
>> +ÂÂÂ /*
>> +ÂÂÂÂ * X86 defined pmd_set_huge() verifies that the given
>> +ÂÂÂÂ * PMD is not a populated non-leaf entry.
>> +ÂÂÂÂ */
>> +ÂÂÂ WRITE_ONCE(*pmdp, __pmd(0));
>> +ÂÂÂ WARN_ON(!pmd_set_huge(pmdp, __pfn_to_phys(pfn), prot));
>> +ÂÂÂ WARN_ON(!pmd_clear_huge(pmdp));
>> +ÂÂÂ pmd = READ_ONCE(*pmdp);
>> +ÂÂÂ WARN_ON(!pmd_none(pmd));
>> +}
>> +
>> +static void __init pmd_savedwrite_tests(unsigned long pfn, pgprot_t prot)
>> +{
>> +ÂÂÂ pmd_t pmd = pfn_pmd(pfn, prot);
>> +
>> +ÂÂÂ WARN_ON(!pmd_savedwrite(pmd_mk_savedwrite(pmd_clear_savedwrite(pmd))));
>> +ÂÂÂ WARN_ON(pmd_savedwrite(pmd_clear_savedwrite(pmd_mk_savedwrite(pmd))));
>> +}
>> +
>> Â #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
>> Â static void __init pud_basic_tests(unsigned long pfn, pgprot_t prot)
>> Â {
>> @@ -100,12 +233,115 @@ static void __init pud_basic_tests(unsigned long pfn, pgprot_t prot)
>> ÂÂÂÂÂÂ */
>> ÂÂÂÂÂ WARN_ON(!pud_bad(pud_mkhuge(pud)));
>> Â }
>> +
>> +static void pud_advanced_tests(struct mm_struct *mm,
>> +ÂÂÂÂÂÂÂ struct vm_area_struct *vma, pud_t *pudp,
>> +ÂÂÂÂÂÂÂ unsigned long pfn, unsigned long vaddr, pgprot_t prot)
>
> Align args properly
>
>> +{
>> +ÂÂÂ pud_t pud = pfn_pud(pfn, prot);
>> +
>> +ÂÂÂ if (!has_transparent_hugepage())
>> +ÂÂÂÂÂÂÂ return;
>> +
>> +ÂÂÂ /* Align the address wrt HPAGE_PUD_SIZE */
>> +ÂÂÂ vaddr = (vaddr & HPAGE_PUD_MASK) + HPAGE_PUD_SIZE;
>> +
>> +ÂÂÂ set_pud_at(mm, vaddr, pudp, pud);
>> +ÂÂÂ pudp_set_wrprotect(mm, vaddr, pudp);
>> +ÂÂÂ pud = READ_ONCE(*pudp);
>> +ÂÂÂ WARN_ON(pud_write(pud));
>> +
>> +#ifndef __PAGETABLE_PMD_FOLDED
>> +ÂÂÂ pud = pfn_pud(pfn, prot);
>> +ÂÂÂ set_pud_at(mm, vaddr, pudp, pud);
>> +ÂÂÂ pudp_huge_get_and_clear(mm, vaddr, pudp);
>> +ÂÂÂ pud = READ_ONCE(*pudp);
>> +ÂÂÂ WARN_ON(!pud_none(pud));
>> +
>> +ÂÂÂ pud = pfn_pud(pfn, prot);
>> +ÂÂÂ set_pud_at(mm, vaddr, pudp, pud);
>> +ÂÂÂ pudp_huge_get_and_clear_full(mm, vaddr, pudp, 1);
>> +ÂÂÂ pud = READ_ONCE(*pudp);
>> +ÂÂÂ WARN_ON(!pud_none(pud));
>> +#endif /* __PAGETABLE_PMD_FOLDED */
>> +ÂÂÂ pud = pfn_pud(pfn, prot);
>> +ÂÂÂ pud = pud_wrprotect(pud);
>> +ÂÂÂ pud = pud_mkclean(pud);
>> +ÂÂÂ set_pud_at(mm, vaddr, pudp, pud);
>> +ÂÂÂ pud = pud_mkwrite(pud);
>> +ÂÂÂ pud = pud_mkdirty(pud);
>> +ÂÂÂ pudp_set_access_flags(vma, vaddr, pudp, pud, 1);
>> +ÂÂÂ pud = READ_ONCE(*pudp);
>> +ÂÂÂ WARN_ON(!(pud_write(pud) && pud_dirty(pud)));
>> +
>> +ÂÂÂ pud = pud_mkyoung(pud);
>> +ÂÂÂ set_pud_at(mm, vaddr, pudp, pud);
>> +ÂÂÂ pudp_test_and_clear_young(vma, vaddr, pudp);
>> +ÂÂÂ pud = READ_ONCE(*pudp);
>> +ÂÂÂ WARN_ON(pud_young(pud));
>> +}
>> +
>> +static void __init pud_leaf_tests(unsigned long pfn, pgprot_t prot)
>> +{
>> +ÂÂÂ pud_t pud = pfn_pud(pfn, prot);
>> +
>> +ÂÂÂ /*
>> +ÂÂÂÂ * PUD based THP is a leaf entry.
>> +ÂÂÂÂ */
>> +ÂÂÂ pud = pud_mkhuge(pud);
>> +ÂÂÂ WARN_ON(!pud_leaf(pud));
>> +}
>> +
>> +static void __init pud_huge_tests(pud_t *pudp, unsigned long pfn, pgprot_t prot)
>> +{
>> +ÂÂÂ pud_t pud;
>> +
>> +ÂÂÂ if (!IS_ENABLED(CONFIG_HAVE_ARCH_HUGE_VMAP))
>> +ÂÂÂÂÂÂÂ return;
>> +ÂÂÂ /*
>> +ÂÂÂÂ * X86 defined pud_set_huge() verifies that the given
>> +ÂÂÂÂ * PUD is not a populated non-leaf entry.
>> +ÂÂÂÂ */
>> +ÂÂÂ WRITE_ONCE(*pudp, __pud(0));
>> +ÂÂÂ WARN_ON(!pud_set_huge(pudp, __pfn_to_phys(pfn), prot));
>> +ÂÂÂ WARN_ON(!pud_clear_huge(pudp));
>> +ÂÂÂ pud = READ_ONCE(*pudp);
>> +ÂÂÂ WARN_ON(!pud_none(pud));
>> +}
>>  #else /* !CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
>> Â static void __init pud_basic_tests(unsigned long pfn, pgprot_t prot) { }
>> +static void pud_advanced_tests(struct mm_struct *mm,
>> +ÂÂÂÂÂÂÂ struct vm_area_struct *vma, pud_t *pudp,
>> +ÂÂÂÂÂÂÂ unsigned long pfn, unsigned long vaddr, pgprot_t prot)
>
> Align args properly
>
>> +{
>> +}
>> +static void __init pud_leaf_tests(unsigned long pfn, pgprot_t prot) { }
>> +static void __init pud_huge_tests(pud_t *pudp, unsigned long pfn, pgprot_t prot)
>> +{
>> +}
>> Â #endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
>>  #else /* !CONFIG_TRANSPARENT_HUGEPAGE */
>> Â static void __init pmd_basic_tests(unsigned long pfn, pgprot_t prot) { }
>> Â static void __init pud_basic_tests(unsigned long pfn, pgprot_t prot) { }
>> +static void __init pmd_advanced_tests(struct mm_struct *mm,
>> +ÂÂÂÂÂÂÂ struct vm_area_struct *vma, pmd_t *pmdp,
>> +ÂÂÂÂÂÂÂ unsigned long pfn, unsigned long vaddr, pgprot_t prot)
>
> Align args properly
>
>> +{
>> +}
>> +static void __init pud_advanced_tests(struct mm_struct *mm,
>> +ÂÂÂÂÂÂÂ struct vm_area_struct *vma, pud_t *pudp,
>> +ÂÂÂÂÂÂÂ unsigned long pfn, unsigned long vaddr, pgprot_t prot)
>
> Align args properly
>
Sure, will fix the arguments alignment in the above mentioned places.