2021-08-09 10:19:13

by Gavin Shan

[permalink] [raw]
Subject: [PATCH v6 00/12] mm/debug_vm_pgtable: Enhancements

There are couple of issues with current implementations and this series
tries to resolve the issues:

(a) All needed information are scattered in variables, passed to various
test functions. The code is organized in pretty much relaxed fashion.

(b) The page isn't allocated from buddy during page table entry modifying
tests. The page can be invalid, conflicting to the implementations
of set_xxx_at() on ARM64. The target page is accessed so that the
iCache can be flushed when execution permission is given on ARM64.
Besides, the target page can be unmapped and accessing to it causes
kernel crash.

"struct pgtable_debug_args" is introduced to address issue (a). For issue
(b), the used page is allocated from buddy in page table entry modifying
tests. The corresponding tets will be skipped if we fail to allocate the
(huge) page. For other test cases, the original page around to kernel
symbol (@start_kernel) is still used.

The patches are organized as below. PATCH[2-10] could be combined to one
patch, but it will make the review harder:

PATCH[1] introduces "struct pgtable_debug_args" as place holder of all
needed information. With it, the old and new implementation
can coexist.
PATCH[2-10] uses "struct pgtable_debug_args" in various test functions.
PATCH[11] removes the unused code for old implementation.
PATCH[12] fixes the issue of corrupted page flag for ARM64

Changelog
=========
v6:
* Populate saved page table entry pointers after
they're allocated in init_args() (Anshuman)
* Fix imbalanced preemption count issue by replacing
pte_alloc_mmap() with pte_alloc() in init_args() (syzbot)
v5:
* Pick r-bs from Anshuman and rebase to 5.14.rc4 (Gavin)
* Use args->start_p4dp to free p4d entries (Anshuman)
* Introduce helper to allocate huge page in init_arg() (Anshuman)
* Bail early if the allocated page doesn't exist in
swap_migration_tests() and correct the comments (Anshuman)
* Add fixes tag to PATCH[v4 12/12] (Christophe)
* Address misc comments (Anshuman)
v4:
* Determine the page allocation method according to
MAX_ORDER (Anshuman)
* Move existing comments to init_args() (Anshuman)
* Code refactoring as suggested by Anshuman (Anshuman)
* Improved commit log and add comments for flush_dcache_page()
in PATCH[v4 12/12] (Anshuman)
* Address misc comments (Anshuman)
v3:
* Fix the warning caused by allocating more pages than
(1 << (MAX_ORDER - 1)) in init_args() (syzbot)
* Fix build warning by dropping unused variables in separate
patches (0-day)
* Missed "WARN_ON(!pud_none(pud))" in pud_huge_tests() in
PATCH[v2 09/12] (0-day)
* Fix the subjects for PATCH[05/12] and PATCH[09/12] (Gavin)
v2:
* Rename struct vm_pgtable_debug to struct pgtable_debug_args.
The parameter name to various test functions are renamed
to "@args" (Anshuman)
* Code changes as suggested by Anshuman (Anshuman)

Gavin Shan (12):
mm/debug_vm_pgtable: Introduce struct pgtable_debug_args
mm/debug_vm_pgtable: Use struct pgtable_debug_args in basic tests
mm/debug_vm_pgtable: Use struct pgtable_debug_args in leaf and
savewrite tests
mm/debug_vm_pgtable: Use struct pgtable_debug_args in protnone and
devmap tests
mm/debug_vm_pgtable: Use struct pgtable_debug_args in soft_dirty and
swap tests
mm/debug_vm_pgtable: Use struct pgtable_debug_args in migration and
thp tests
mm/debug_vm_pgtable: Use struct pgtable_debug_args in PTE modifying
tests
mm/debug_vm_pgtable: Use struct pgtable_debug_args in PMD modifying
tests
mm/debug_vm_pgtable: Use struct pgtable_debug_args in PUD modifying
tests
mm/debug_vm_pgtable: Use struct pgtable_debug_args in PGD and P4D
modifying tests
mm/debug_vm_pgtable: Remove unused code
mm/debug_vm_pgtable: Fix corrupted page flag

mm/debug_vm_pgtable.c | 918 +++++++++++++++++++++++++-----------------
1 file changed, 559 insertions(+), 359 deletions(-)

--
2.23.0


2021-08-09 10:19:18

by Gavin Shan

[permalink] [raw]
Subject: [PATCH v6 03/12] mm/debug_vm_pgtable: Use struct pgtable_debug_args in leaf and savewrite tests

This uses struct pgtable_debug_args in the leaf and savewrite test
functions.

Signed-off-by: Gavin Shan <[email protected]>
Reviewed-by: Anshuman Khandual <[email protected]>
---
mm/debug_vm_pgtable.c | 32 ++++++++++++++++----------------
1 file changed, 16 insertions(+), 16 deletions(-)

diff --git a/mm/debug_vm_pgtable.c b/mm/debug_vm_pgtable.c
index 64b5a76e0f6d..759316143a21 100644
--- a/mm/debug_vm_pgtable.c
+++ b/mm/debug_vm_pgtable.c
@@ -161,9 +161,9 @@ static void __init pte_advanced_tests(struct mm_struct *mm,
WARN_ON(pte_young(pte));
}

-static void __init pte_savedwrite_tests(unsigned long pfn, pgprot_t prot)
+static void __init pte_savedwrite_tests(struct pgtable_debug_args *args)
{
- pte_t pte = pfn_pte(pfn, prot);
+ pte_t pte = pfn_pte(args->fixed_pte_pfn, args->page_prot_none);

if (!IS_ENABLED(CONFIG_NUMA_BALANCING))
return;
@@ -262,7 +262,7 @@ static void __init pmd_advanced_tests(struct mm_struct *mm,
pgtable = pgtable_trans_huge_withdraw(mm, pmdp);
}

-static void __init pmd_leaf_tests(unsigned long pfn, pgprot_t prot)
+static void __init pmd_leaf_tests(struct pgtable_debug_args *args)
{
pmd_t pmd;

@@ -270,7 +270,7 @@ static void __init pmd_leaf_tests(unsigned long pfn, pgprot_t prot)
return;

pr_debug("Validating PMD leaf\n");
- pmd = pfn_pmd(pfn, prot);
+ pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot);

/*
* PMD based THP is a leaf entry.
@@ -279,7 +279,7 @@ static void __init pmd_leaf_tests(unsigned long pfn, pgprot_t prot)
WARN_ON(!pmd_leaf(pmd));
}

-static void __init pmd_savedwrite_tests(unsigned long pfn, pgprot_t prot)
+static void __init pmd_savedwrite_tests(struct pgtable_debug_args *args)
{
pmd_t pmd;

@@ -290,7 +290,7 @@ static void __init pmd_savedwrite_tests(unsigned long pfn, pgprot_t prot)
return;

pr_debug("Validating PMD saved write\n");
- pmd = pfn_pmd(pfn, prot);
+ pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot_none);
WARN_ON(!pmd_savedwrite(pmd_mk_savedwrite(pmd_clear_savedwrite(pmd))));
WARN_ON(pmd_savedwrite(pmd_clear_savedwrite(pmd_mk_savedwrite(pmd))));
}
@@ -388,7 +388,7 @@ static void __init pud_advanced_tests(struct mm_struct *mm,
pudp_huge_get_and_clear(mm, vaddr, pudp);
}

-static void __init pud_leaf_tests(unsigned long pfn, pgprot_t prot)
+static void __init pud_leaf_tests(struct pgtable_debug_args *args)
{
pud_t pud;

@@ -396,7 +396,7 @@ static void __init pud_leaf_tests(unsigned long pfn, pgprot_t prot)
return;

pr_debug("Validating PUD leaf\n");
- pud = pfn_pud(pfn, prot);
+ pud = pfn_pud(args->fixed_pud_pfn, args->page_prot);
/*
* PUD based THP is a leaf entry.
*/
@@ -411,7 +411,7 @@ static void __init pud_advanced_tests(struct mm_struct *mm,
pgprot_t prot)
{
}
-static void __init pud_leaf_tests(unsigned long pfn, pgprot_t prot) { }
+static void __init pud_leaf_tests(struct pgtable_debug_args *args) { }
#endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
#else /* !CONFIG_TRANSPARENT_HUGEPAGE */
static void __init pmd_basic_tests(struct pgtable_debug_args *args, int idx) { }
@@ -428,9 +428,9 @@ static void __init pud_advanced_tests(struct mm_struct *mm,
pgprot_t prot)
{
}
-static void __init pmd_leaf_tests(unsigned long pfn, pgprot_t prot) { }
-static void __init pud_leaf_tests(unsigned long pfn, pgprot_t prot) { }
-static void __init pmd_savedwrite_tests(unsigned long pfn, pgprot_t prot) { }
+static void __init pmd_leaf_tests(struct pgtable_debug_args *args) { }
+static void __init pud_leaf_tests(struct pgtable_debug_args *args) { }
+static void __init pmd_savedwrite_tests(struct pgtable_debug_args *args) { }
#endif /* CONFIG_TRANSPARENT_HUGEPAGE */

#ifdef CONFIG_HAVE_ARCH_HUGE_VMAP
@@ -1321,11 +1321,11 @@ static int __init debug_vm_pgtable(void)
p4d_basic_tests(&args);
pgd_basic_tests(&args);

- pmd_leaf_tests(pmd_aligned, prot);
- pud_leaf_tests(pud_aligned, prot);
+ pmd_leaf_tests(&args);
+ pud_leaf_tests(&args);

- pte_savedwrite_tests(pte_aligned, protnone);
- pmd_savedwrite_tests(pmd_aligned, protnone);
+ pte_savedwrite_tests(&args);
+ pmd_savedwrite_tests(&args);

pte_special_tests(pte_aligned, prot);
pte_protnone_tests(pte_aligned, protnone);
--
2.23.0

2021-08-09 10:19:19

by Gavin Shan

[permalink] [raw]
Subject: [PATCH v6 04/12] mm/debug_vm_pgtable: Use struct pgtable_debug_args in protnone and devmap tests

This uses struct pgtable_debug_args in protnone and devmap test
functions. After that, the unused variable @protnone in debug_vm_pgtable()
is dropped.

Signed-off-by: Gavin Shan <[email protected]>
Reviewed-by: Anshuman Khandual <[email protected]>
---
mm/debug_vm_pgtable.c | 58 +++++++++++++++++++------------------------
1 file changed, 26 insertions(+), 32 deletions(-)

diff --git a/mm/debug_vm_pgtable.c b/mm/debug_vm_pgtable.c
index 759316143a21..8598aefeba4d 100644
--- a/mm/debug_vm_pgtable.c
+++ b/mm/debug_vm_pgtable.c
@@ -662,9 +662,9 @@ static void __init pmd_populate_tests(struct mm_struct *mm, pmd_t *pmdp,
WARN_ON(pmd_bad(pmd));
}

-static void __init pte_special_tests(unsigned long pfn, pgprot_t prot)
+static void __init pte_special_tests(struct pgtable_debug_args *args)
{
- pte_t pte = pfn_pte(pfn, prot);
+ pte_t pte = pfn_pte(args->fixed_pte_pfn, args->page_prot);

if (!IS_ENABLED(CONFIG_ARCH_HAS_PTE_SPECIAL))
return;
@@ -673,9 +673,9 @@ static void __init pte_special_tests(unsigned long pfn, pgprot_t prot)
WARN_ON(!pte_special(pte_mkspecial(pte)));
}

-static void __init pte_protnone_tests(unsigned long pfn, pgprot_t prot)
+static void __init pte_protnone_tests(struct pgtable_debug_args *args)
{
- pte_t pte = pfn_pte(pfn, prot);
+ pte_t pte = pfn_pte(args->fixed_pte_pfn, args->page_prot_none);

if (!IS_ENABLED(CONFIG_NUMA_BALANCING))
return;
@@ -686,7 +686,7 @@ static void __init pte_protnone_tests(unsigned long pfn, pgprot_t prot)
}

#ifdef CONFIG_TRANSPARENT_HUGEPAGE
-static void __init pmd_protnone_tests(unsigned long pfn, pgprot_t prot)
+static void __init pmd_protnone_tests(struct pgtable_debug_args *args)
{
pmd_t pmd;

@@ -697,25 +697,25 @@ static void __init pmd_protnone_tests(unsigned long pfn, pgprot_t prot)
return;

pr_debug("Validating PMD protnone\n");
- pmd = pmd_mkhuge(pfn_pmd(pfn, prot));
+ pmd = pmd_mkhuge(pfn_pmd(args->fixed_pmd_pfn, args->page_prot_none));
WARN_ON(!pmd_protnone(pmd));
WARN_ON(!pmd_present(pmd));
}
#else /* !CONFIG_TRANSPARENT_HUGEPAGE */
-static void __init pmd_protnone_tests(unsigned long pfn, pgprot_t prot) { }
+static void __init pmd_protnone_tests(struct pgtable_debug_args *args) { }
#endif /* CONFIG_TRANSPARENT_HUGEPAGE */

#ifdef CONFIG_ARCH_HAS_PTE_DEVMAP
-static void __init pte_devmap_tests(unsigned long pfn, pgprot_t prot)
+static void __init pte_devmap_tests(struct pgtable_debug_args *args)
{
- pte_t pte = pfn_pte(pfn, prot);
+ pte_t pte = pfn_pte(args->fixed_pte_pfn, args->page_prot);

pr_debug("Validating PTE devmap\n");
WARN_ON(!pte_devmap(pte_mkdevmap(pte)));
}

#ifdef CONFIG_TRANSPARENT_HUGEPAGE
-static void __init pmd_devmap_tests(unsigned long pfn, pgprot_t prot)
+static void __init pmd_devmap_tests(struct pgtable_debug_args *args)
{
pmd_t pmd;

@@ -723,12 +723,12 @@ static void __init pmd_devmap_tests(unsigned long pfn, pgprot_t prot)
return;

pr_debug("Validating PMD devmap\n");
- pmd = pfn_pmd(pfn, prot);
+ pmd = pfn_pmd(args->fixed_pmd_pfn, args->page_prot);
WARN_ON(!pmd_devmap(pmd_mkdevmap(pmd)));
}

#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
-static void __init pud_devmap_tests(unsigned long pfn, pgprot_t prot)
+static void __init pud_devmap_tests(struct pgtable_debug_args *args)
{
pud_t pud;

@@ -736,20 +736,20 @@ static void __init pud_devmap_tests(unsigned long pfn, pgprot_t prot)
return;

pr_debug("Validating PUD devmap\n");
- pud = pfn_pud(pfn, prot);
+ pud = pfn_pud(args->fixed_pud_pfn, args->page_prot);
WARN_ON(!pud_devmap(pud_mkdevmap(pud)));
}
#else /* !CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
-static void __init pud_devmap_tests(unsigned long pfn, pgprot_t prot) { }
+static void __init pud_devmap_tests(struct pgtable_debug_args *args) { }
#endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
#else /* CONFIG_TRANSPARENT_HUGEPAGE */
-static void __init pmd_devmap_tests(unsigned long pfn, pgprot_t prot) { }
-static void __init pud_devmap_tests(unsigned long pfn, pgprot_t prot) { }
+static void __init pmd_devmap_tests(struct pgtable_debug_args *args) { }
+static void __init pud_devmap_tests(struct pgtable_debug_args *args) { }
#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
#else
-static void __init pte_devmap_tests(unsigned long pfn, pgprot_t prot) { }
-static void __init pmd_devmap_tests(unsigned long pfn, pgprot_t prot) { }
-static void __init pud_devmap_tests(unsigned long pfn, pgprot_t prot) { }
+static void __init pte_devmap_tests(struct pgtable_debug_args *args) { }
+static void __init pmd_devmap_tests(struct pgtable_debug_args *args) { }
+static void __init pud_devmap_tests(struct pgtable_debug_args *args) { }
#endif /* CONFIG_ARCH_HAS_PTE_DEVMAP */

static void __init pte_soft_dirty_tests(unsigned long pfn, pgprot_t prot)
@@ -1227,7 +1227,7 @@ static int __init debug_vm_pgtable(void)
pmd_t *pmdp, *saved_pmdp, pmd;
pte_t *ptep;
pgtable_t saved_ptep;
- pgprot_t prot, protnone;
+ pgprot_t prot;
phys_addr_t paddr;
unsigned long vaddr, pte_aligned, pmd_aligned;
unsigned long pud_aligned;
@@ -1247,12 +1247,6 @@ static int __init debug_vm_pgtable(void)
return 1;
}

- /*
- * __P000 (or even __S000) will help create page table entries with
- * PROT_NONE permission as required for pxx_protnone_tests().
- */
- protnone = __P000;
-
vma = vm_area_alloc(mm);
if (!vma) {
pr_err("vma allocation failed\n");
@@ -1327,13 +1321,13 @@ static int __init debug_vm_pgtable(void)
pte_savedwrite_tests(&args);
pmd_savedwrite_tests(&args);

- pte_special_tests(pte_aligned, prot);
- pte_protnone_tests(pte_aligned, protnone);
- pmd_protnone_tests(pmd_aligned, protnone);
+ pte_special_tests(&args);
+ pte_protnone_tests(&args);
+ pmd_protnone_tests(&args);

- pte_devmap_tests(pte_aligned, prot);
- pmd_devmap_tests(pmd_aligned, prot);
- pud_devmap_tests(pud_aligned, prot);
+ pte_devmap_tests(&args);
+ pmd_devmap_tests(&args);
+ pud_devmap_tests(&args);

pte_soft_dirty_tests(pte_aligned, prot);
pmd_soft_dirty_tests(pmd_aligned, prot);
--
2.23.0

2021-08-09 10:32:09

by Christophe Leroy

[permalink] [raw]
Subject: Re: [PATCH v6 00/12] mm/debug_vm_pgtable: Enhancements



Le 09/08/2021 à 11:26, Gavin Shan a écrit :
> There are couple of issues with current implementations and this series
> tries to resolve the issues:
>
> (a) All needed information are scattered in variables, passed to various
> test functions. The code is organized in pretty much relaxed fashion.
>
> (b) The page isn't allocated from buddy during page table entry modifying
> tests. The page can be invalid, conflicting to the implementations
> of set_xxx_at() on ARM64. The target page is accessed so that the
> iCache can be flushed when execution permission is given on ARM64.
> Besides, the target page can be unmapped and accessing to it causes
> kernel crash.
>
> "struct pgtable_debug_args" is introduced to address issue (a). For issue
> (b), the used page is allocated from buddy in page table entry modifying
> tests. The corresponding tets will be skipped if we fail to allocate the
> (huge) page. For other test cases, the original page around to kernel
> symbol (@start_kernel) is still used.
>
> The patches are organized as below. PATCH[2-10] could be combined to one
> patch, but it will make the review harder:
>
> PATCH[1] introduces "struct pgtable_debug_args" as place holder of all
> needed information. With it, the old and new implementation
> can coexist.
> PATCH[2-10] uses "struct pgtable_debug_args" in various test functions.
> PATCH[11] removes the unused code for old implementation.
> PATCH[12] fixes the issue of corrupted page flag for ARM64


I tested this series on powerpc 8xx, no failure reported.

Christophe

>
> Changelog
> =========
> v6:
> * Populate saved page table entry pointers after
> they're allocated in init_args() (Anshuman)
> * Fix imbalanced preemption count issue by replacing
> pte_alloc_mmap() with pte_alloc() in init_args() (syzbot)
> v5:
> * Pick r-bs from Anshuman and rebase to 5.14.rc4 (Gavin)
> * Use args->start_p4dp to free p4d entries (Anshuman)
> * Introduce helper to allocate huge page in init_arg() (Anshuman)
> * Bail early if the allocated page doesn't exist in
> swap_migration_tests() and correct the comments (Anshuman)
> * Add fixes tag to PATCH[v4 12/12] (Christophe)
> * Address misc comments (Anshuman)
> v4:
> * Determine the page allocation method according to
> MAX_ORDER (Anshuman)
> * Move existing comments to init_args() (Anshuman)
> * Code refactoring as suggested by Anshuman (Anshuman)
> * Improved commit log and add comments for flush_dcache_page()
> in PATCH[v4 12/12] (Anshuman)
> * Address misc comments (Anshuman)
> v3:
> * Fix the warning caused by allocating more pages than
> (1 << (MAX_ORDER - 1)) in init_args() (syzbot)
> * Fix build warning by dropping unused variables in separate
> patches (0-day)
> * Missed "WARN_ON(!pud_none(pud))" in pud_huge_tests() in
> PATCH[v2 09/12] (0-day)
> * Fix the subjects for PATCH[05/12] and PATCH[09/12] (Gavin)
> v2:
> * Rename struct vm_pgtable_debug to struct pgtable_debug_args.
> The parameter name to various test functions are renamed
> to "@args" (Anshuman)
> * Code changes as suggested by Anshuman (Anshuman)
>
> Gavin Shan (12):
> mm/debug_vm_pgtable: Introduce struct pgtable_debug_args
> mm/debug_vm_pgtable: Use struct pgtable_debug_args in basic tests
> mm/debug_vm_pgtable: Use struct pgtable_debug_args in leaf and
> savewrite tests
> mm/debug_vm_pgtable: Use struct pgtable_debug_args in protnone and
> devmap tests
> mm/debug_vm_pgtable: Use struct pgtable_debug_args in soft_dirty and
> swap tests
> mm/debug_vm_pgtable: Use struct pgtable_debug_args in migration and
> thp tests
> mm/debug_vm_pgtable: Use struct pgtable_debug_args in PTE modifying
> tests
> mm/debug_vm_pgtable: Use struct pgtable_debug_args in PMD modifying
> tests
> mm/debug_vm_pgtable: Use struct pgtable_debug_args in PUD modifying
> tests
> mm/debug_vm_pgtable: Use struct pgtable_debug_args in PGD and P4D
> modifying tests
> mm/debug_vm_pgtable: Remove unused code
> mm/debug_vm_pgtable: Fix corrupted page flag
>
> mm/debug_vm_pgtable.c | 918 +++++++++++++++++++++++++-----------------
> 1 file changed, 559 insertions(+), 359 deletions(-)
>

2021-08-09 11:43:20

by Gavin Shan

[permalink] [raw]
Subject: [PATCH v6 01/12] mm/debug_vm_pgtable: Introduce struct pgtable_debug_args

In debug_vm_pgtable(), there are many local variables introduced to
track the needed information and they are passed to the functions for
various test cases. It'd better to introduce a struct as place holder
for these information. With it, what the tests functions need is the
struct. In this way, the code is simplified and easier to be maintained.

Besides, set_xxx_at() could access the data on the corresponding pages
in the page table modifying tests. So the accessed pages in the tests
should have been allocated from buddy. Otherwise, we're accessing pages
that aren't owned by us. This causes issues like page flag corruption
or kernel crash on accessing unmapped page when CONFIG_DEBUG_PAGEALLOC
is enabled.

This introduces "struct pgtable_debug_args". The struct is initialized
and destroyed, but the information in the struct isn't used yet. It will
be used in subsequent patches.

Signed-off-by: Gavin Shan <[email protected]>
Reviewed-by: Anshuman Khandual <[email protected]>
---
mm/debug_vm_pgtable.c | 270 +++++++++++++++++++++++++++++++++++++++++-
1 file changed, 269 insertions(+), 1 deletion(-)

diff --git a/mm/debug_vm_pgtable.c b/mm/debug_vm_pgtable.c
index 1c922691aa61..7b6bcf59e376 100644
--- a/mm/debug_vm_pgtable.c
+++ b/mm/debug_vm_pgtable.c
@@ -58,6 +58,37 @@
#define RANDOM_ORVALUE (GENMASK(BITS_PER_LONG - 1, 0) & ~ARCH_SKIP_MASK)
#define RANDOM_NZVALUE GENMASK(7, 0)

+struct pgtable_debug_args {
+ struct mm_struct *mm;
+ struct vm_area_struct *vma;
+
+ pgd_t *pgdp;
+ p4d_t *p4dp;
+ pud_t *pudp;
+ pmd_t *pmdp;
+ pte_t *ptep;
+
+ p4d_t *start_p4dp;
+ pud_t *start_pudp;
+ pmd_t *start_pmdp;
+ pgtable_t start_ptep;
+
+ unsigned long vaddr;
+ pgprot_t page_prot;
+ pgprot_t page_prot_none;
+
+ bool is_contiguous_page;
+ unsigned long pud_pfn;
+ unsigned long pmd_pfn;
+ unsigned long pte_pfn;
+
+ unsigned long fixed_pgd_pfn;
+ unsigned long fixed_p4d_pfn;
+ unsigned long fixed_pud_pfn;
+ unsigned long fixed_pmd_pfn;
+ unsigned long fixed_pte_pfn;
+};
+
static void __init pte_basic_tests(unsigned long pfn, int idx)
{
pgprot_t prot = protection_map[idx];
@@ -955,8 +986,239 @@ static unsigned long __init get_random_vaddr(void)
return random_vaddr;
}

+static void __init destroy_args(struct pgtable_debug_args *args)
+{
+ struct page *page = NULL;
+
+ /* Free (huge) page */
+ if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) &&
+ IS_ENABLED(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD) &&
+ has_transparent_hugepage() &&
+ args->pud_pfn != ULONG_MAX) {
+ if (args->is_contiguous_page) {
+ free_contig_range(args->pud_pfn,
+ (1 << (HPAGE_PUD_SHIFT - PAGE_SHIFT)));
+ } else {
+ page = pfn_to_page(args->pud_pfn);
+ __free_pages(page, HPAGE_PUD_SHIFT - PAGE_SHIFT);
+ }
+
+ args->pud_pfn = ULONG_MAX;
+ args->pmd_pfn = ULONG_MAX;
+ args->pte_pfn = ULONG_MAX;
+ }
+
+ if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) &&
+ has_transparent_hugepage() &&
+ args->pmd_pfn != ULONG_MAX) {
+ if (args->is_contiguous_page) {
+ free_contig_range(args->pmd_pfn, (1 << HPAGE_PMD_ORDER));
+ } else {
+ page = pfn_to_page(args->pmd_pfn);
+ __free_pages(page, HPAGE_PMD_ORDER);
+ }
+
+ args->pmd_pfn = ULONG_MAX;
+ args->pte_pfn = ULONG_MAX;
+ }
+
+ if (args->pte_pfn != ULONG_MAX) {
+ page = pfn_to_page(args->pte_pfn);
+ __free_pages(page, 0);
+
+ args->pte_pfn = ULONG_MAX;
+ }
+
+ /* Free page table entries */
+ if (args->start_ptep) {
+ pte_free(args->mm, args->start_ptep);
+ mm_dec_nr_ptes(args->mm);
+ }
+
+ if (args->start_pmdp) {
+ pmd_free(args->mm, args->start_pmdp);
+ mm_dec_nr_pmds(args->mm);
+ }
+
+ if (args->start_pudp) {
+ pud_free(args->mm, args->start_pudp);
+ mm_dec_nr_puds(args->mm);
+ }
+
+ if (args->start_p4dp)
+ p4d_free(args->mm, args->start_p4dp);
+
+ /* Free vma and mm struct */
+ if (args->vma)
+ vm_area_free(args->vma);
+
+ if (args->mm)
+ mmdrop(args->mm);
+}
+
+static struct page * __init
+debug_vm_pgtable_alloc_huge_page(struct pgtable_debug_args *args, int order)
+{
+ struct page *page = NULL;
+
+#ifdef CONFIG_CONTIG_ALLOC
+ if (order >= MAX_ORDER) {
+ page = alloc_contig_pages((1 << order), GFP_KERNEL,
+ first_online_node, NULL);
+ if (page) {
+ args->is_contiguous_page = true;
+ return page;
+ }
+ }
+#endif
+
+ if (order < MAX_ORDER)
+ page = alloc_pages(GFP_KERNEL, order);
+
+ return page;
+}
+
+static int __init init_args(struct pgtable_debug_args *args)
+{
+ struct page *page = NULL;
+ phys_addr_t phys;
+ int ret = 0;
+
+ /*
+ * Initialize the debugging data.
+ *
+ * __P000 (or even __S000) will help create page table entries with
+ * PROT_NONE permission as required for pxx_protnone_tests().
+ */
+ memset(args, 0, sizeof(*args));
+ args->vaddr = get_random_vaddr();
+ args->page_prot = vm_get_page_prot(VMFLAGS);
+ args->page_prot_none = __P000;
+ args->is_contiguous_page = false;
+ args->pud_pfn = ULONG_MAX;
+ args->pmd_pfn = ULONG_MAX;
+ args->pte_pfn = ULONG_MAX;
+ args->fixed_pgd_pfn = ULONG_MAX;
+ args->fixed_p4d_pfn = ULONG_MAX;
+ args->fixed_pud_pfn = ULONG_MAX;
+ args->fixed_pmd_pfn = ULONG_MAX;
+ args->fixed_pte_pfn = ULONG_MAX;
+
+ /* Allocate mm and vma */
+ args->mm = mm_alloc();
+ if (!args->mm) {
+ pr_err("Failed to allocate mm struct\n");
+ ret = -ENOMEM;
+ goto error;
+ }
+
+ args->vma = vm_area_alloc(args->mm);
+ if (!args->vma) {
+ pr_err("Failed to allocate vma\n");
+ ret = -ENOMEM;
+ goto error;
+ }
+
+ /*
+ * Allocate page table entries. They will be modified in the tests.
+ * Lets save the page table entries so that they can be released
+ * when the tests are completed.
+ */
+ args->pgdp = pgd_offset(args->mm, args->vaddr);
+ args->p4dp = p4d_alloc(args->mm, args->pgdp, args->vaddr);
+ if (!args->p4dp) {
+ pr_err("Failed to allocate p4d entries\n");
+ ret = -ENOMEM;
+ goto error;
+ }
+ args->start_p4dp = p4d_offset(args->pgdp, 0UL);
+ WARN_ON(!args->start_p4dp);
+
+ args->pudp = pud_alloc(args->mm, args->p4dp, args->vaddr);
+ if (!args->pudp) {
+ pr_err("Failed to allocate pud entries\n");
+ ret = -ENOMEM;
+ goto error;
+ }
+ args->start_pudp = pud_offset(args->p4dp, 0UL);
+ WARN_ON(!args->start_pudp);
+
+ args->pmdp = pmd_alloc(args->mm, args->pudp, args->vaddr);
+ if (!args->pmdp) {
+ pr_err("Failed to allocate pmd entries\n");
+ ret = -ENOMEM;
+ goto error;
+ }
+ args->start_pmdp = pmd_offset(args->pudp, 0UL);
+ WARN_ON(!args->start_pmdp);
+
+ if (pte_alloc(args->mm, args->pmdp)) {
+ pr_err("Failed to allocate pte entries\n");
+ ret = -ENOMEM;
+ goto error;
+ }
+ args->start_ptep = pmd_pgtable(READ_ONCE(*args->pmdp));
+ WARN_ON(!args->start_ptep);
+
+ /*
+ * PFN for mapping at PTE level is determined from a standard kernel
+ * text symbol. But pfns for higher page table levels are derived by
+ * masking lower bits of this real pfn. These derived pfns might not
+ * exist on the platform but that does not really matter as pfn_pxx()
+ * helpers will still create appropriate entries for the test. This
+ * helps avoid large memory block allocations to be used for mapping
+ * at higher page table levels in some of the tests.
+ */
+ phys = __pa_symbol(&start_kernel);
+ args->fixed_pgd_pfn = __phys_to_pfn(phys & PGDIR_MASK);
+ args->fixed_p4d_pfn = __phys_to_pfn(phys & P4D_MASK);
+ args->fixed_pud_pfn = __phys_to_pfn(phys & PUD_MASK);
+ args->fixed_pmd_pfn = __phys_to_pfn(phys & PMD_MASK);
+ args->fixed_pte_pfn = __phys_to_pfn(phys & PAGE_MASK);
+ WARN_ON(!pfn_valid(args->fixed_pte_pfn));
+
+ /*
+ * Allocate (huge) pages because some of the tests need to access
+ * the data in the pages. The corresponding tests will be skipped
+ * if we fail to allocate (huge) pages.
+ */
+ if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) &&
+ IS_ENABLED(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD) &&
+ has_transparent_hugepage()) {
+ page = debug_vm_pgtable_alloc_huge_page(args,
+ HPAGE_PUD_SHIFT - PAGE_SHIFT);
+ if (page) {
+ args->pud_pfn = page_to_pfn(page);
+ args->pmd_pfn = args->pud_pfn;
+ args->pte_pfn = args->pud_pfn;
+ return 0;
+ }
+ }
+
+ if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) &&
+ has_transparent_hugepage()) {
+ page = debug_vm_pgtable_alloc_huge_page(args, HPAGE_PMD_ORDER);
+ if (page) {
+ args->pmd_pfn = page_to_pfn(page);
+ args->pte_pfn = args->pmd_pfn;
+ return 0;
+ }
+ }
+
+ page = alloc_pages(GFP_KERNEL, 0);
+ if (page)
+ args->pte_pfn = page_to_pfn(page);
+
+ return 0;
+
+error:
+ destroy_args(args);
+ return ret;
+}
+
static int __init debug_vm_pgtable(void)
{
+ struct pgtable_debug_args args;
struct vm_area_struct *vma;
struct mm_struct *mm;
pgd_t *pgdp;
@@ -970,9 +1232,13 @@ static int __init debug_vm_pgtable(void)
unsigned long vaddr, pte_aligned, pmd_aligned;
unsigned long pud_aligned, p4d_aligned, pgd_aligned;
spinlock_t *ptl = NULL;
- int idx;
+ int idx, ret;

pr_info("Validating architecture page table helpers\n");
+ ret = init_args(&args);
+ if (ret)
+ return ret;
+
prot = vm_get_page_prot(VMFLAGS);
vaddr = get_random_vaddr();
mm = mm_alloc();
@@ -1127,6 +1393,8 @@ static int __init debug_vm_pgtable(void)
mm_dec_nr_pmds(mm);
mm_dec_nr_ptes(mm);
mmdrop(mm);
+
+ destroy_args(&args);
return 0;
}
late_initcall(debug_vm_pgtable);
--
2.23.0

2021-08-09 11:43:52

by Gavin Shan

[permalink] [raw]
Subject: [PATCH v6 09/12] mm/debug_vm_pgtable: Use struct pgtable_debug_args in PUD modifying tests

This uses struct pgtable_debug_args in PUD modifying tests. The allocated
huge page is used when set_pud_at() is used. The corresponding tests
are skipped if the huge page doesn't exist. Besides, the following unused
variables in debug_vm_pgtable() are dropped: @prot, @paddr, @pud_aligned.

Signed-off-by: Gavin Shan <[email protected]>
Reviewed-by: Anshuman Khandual <[email protected]>
---
mm/debug_vm_pgtable.c | 126 ++++++++++++++++--------------------------
1 file changed, 48 insertions(+), 78 deletions(-)

diff --git a/mm/debug_vm_pgtable.c b/mm/debug_vm_pgtable.c
index abf778f729fd..4e453ae0f291 100644
--- a/mm/debug_vm_pgtable.c
+++ b/mm/debug_vm_pgtable.c
@@ -337,55 +337,56 @@ static void __init pud_basic_tests(struct pgtable_debug_args *args, int idx)
WARN_ON(!pud_bad(pud_mkhuge(pud)));
}

-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)
+static void __init pud_advanced_tests(struct pgtable_debug_args *args)
{
+ unsigned long vaddr = args->vaddr;
pud_t pud;

if (!has_transparent_hugepage())
return;

+ if (args->pud_pfn == ULONG_MAX)
+ return;
+
pr_debug("Validating PUD advanced\n");
/* Align the address wrt HPAGE_PUD_SIZE */
vaddr &= HPAGE_PUD_MASK;

- pud = pfn_pud(pfn, prot);
- set_pud_at(mm, vaddr, pudp, pud);
- pudp_set_wrprotect(mm, vaddr, pudp);
- pud = READ_ONCE(*pudp);
+ pud = pfn_pud(args->pud_pfn, args->page_prot);
+ set_pud_at(args->mm, vaddr, args->pudp, pud);
+ pudp_set_wrprotect(args->mm, vaddr, args->pudp);
+ pud = READ_ONCE(*args->pudp);
WARN_ON(pud_write(pud));

#ifndef __PAGETABLE_PMD_FOLDED
- pudp_huge_get_and_clear(mm, vaddr, pudp);
- pud = READ_ONCE(*pudp);
+ pudp_huge_get_and_clear(args->mm, vaddr, args->pudp);
+ pud = READ_ONCE(*args->pudp);
WARN_ON(!pud_none(pud));
#endif /* __PAGETABLE_PMD_FOLDED */
- pud = pfn_pud(pfn, prot);
+ pud = pfn_pud(args->pud_pfn, args->page_prot);
pud = pud_wrprotect(pud);
pud = pud_mkclean(pud);
- set_pud_at(mm, vaddr, pudp, pud);
+ set_pud_at(args->mm, vaddr, args->pudp, pud);
pud = pud_mkwrite(pud);
pud = pud_mkdirty(pud);
- pudp_set_access_flags(vma, vaddr, pudp, pud, 1);
- pud = READ_ONCE(*pudp);
+ pudp_set_access_flags(args->vma, vaddr, args->pudp, pud, 1);
+ pud = READ_ONCE(*args->pudp);
WARN_ON(!(pud_write(pud) && pud_dirty(pud)));

#ifndef __PAGETABLE_PMD_FOLDED
- pudp_huge_get_and_clear_full(mm, vaddr, pudp, 1);
- pud = READ_ONCE(*pudp);
+ pudp_huge_get_and_clear_full(args->mm, vaddr, args->pudp, 1);
+ pud = READ_ONCE(*args->pudp);
WARN_ON(!pud_none(pud));
#endif /* __PAGETABLE_PMD_FOLDED */

- pud = pfn_pud(pfn, prot);
+ pud = pfn_pud(args->pud_pfn, args->page_prot);
pud = pud_mkyoung(pud);
- set_pud_at(mm, vaddr, pudp, pud);
- pudp_test_and_clear_young(vma, vaddr, pudp);
- pud = READ_ONCE(*pudp);
+ set_pud_at(args->mm, vaddr, args->pudp, pud);
+ pudp_test_and_clear_young(args->vma, vaddr, args->pudp);
+ pud = READ_ONCE(*args->pudp);
WARN_ON(pud_young(pud));

- pudp_huge_get_and_clear(mm, vaddr, pudp);
+ pudp_huge_get_and_clear(args->mm, vaddr, args->pudp);
}

static void __init pud_leaf_tests(struct pgtable_debug_args *args)
@@ -405,24 +406,14 @@ static void __init pud_leaf_tests(struct pgtable_debug_args *args)
}
#else /* !CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
static void __init pud_basic_tests(struct pgtable_debug_args *args, int idx) { }
-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)
-{
-}
+static void __init pud_advanced_tests(struct pgtable_debug_args *args) { }
static void __init pud_leaf_tests(struct pgtable_debug_args *args) { }
#endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
#else /* !CONFIG_TRANSPARENT_HUGEPAGE */
static void __init pmd_basic_tests(struct pgtable_debug_args *args, int idx) { }
static void __init pud_basic_tests(struct pgtable_debug_args *args, int idx) { }
static void __init pmd_advanced_tests(struct pgtable_debug_args *args) { }
-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)
-{
-}
+static void __init pud_advanced_tests(struct pgtable_debug_args *args) { }
static void __init pmd_leaf_tests(struct pgtable_debug_args *args) { }
static void __init pud_leaf_tests(struct pgtable_debug_args *args) { }
static void __init pmd_savedwrite_tests(struct pgtable_debug_args *args) { }
@@ -448,11 +439,11 @@ static void __init pmd_huge_tests(struct pgtable_debug_args *args)
WARN_ON(!pmd_none(pmd));
}

-static void __init pud_huge_tests(pud_t *pudp, unsigned long pfn, pgprot_t prot)
+static void __init pud_huge_tests(struct pgtable_debug_args *args)
{
pud_t pud;

- if (!arch_vmap_pud_supported(prot))
+ if (!arch_vmap_pud_supported(args->page_prot))
return;

pr_debug("Validating PUD huge\n");
@@ -460,15 +451,15 @@ static void __init pud_huge_tests(pud_t *pudp, unsigned long pfn, pgprot_t prot)
* 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);
+ WRITE_ONCE(*args->pudp, __pud(0));
+ WARN_ON(!pud_set_huge(args->pudp, __pfn_to_phys(args->fixed_pud_pfn), args->page_prot));
+ WARN_ON(!pud_clear_huge(args->pudp));
+ pud = READ_ONCE(*args->pudp);
WARN_ON(!pud_none(pud));
}
#else /* !CONFIG_HAVE_ARCH_HUGE_VMAP */
static void __init pmd_huge_tests(struct pgtable_debug_args *args) { }
-static void __init pud_huge_tests(pud_t *pudp, unsigned long pfn, pgprot_t prot) { }
+static void __init pud_huge_tests(struct pgtable_debug_args *args) { }
#endif /* CONFIG_HAVE_ARCH_HUGE_VMAP */

static void __init p4d_basic_tests(struct pgtable_debug_args *args)
@@ -490,27 +481,26 @@ static void __init pgd_basic_tests(struct pgtable_debug_args *args)
}

#ifndef __PAGETABLE_PUD_FOLDED
-static void __init pud_clear_tests(struct mm_struct *mm, pud_t *pudp)
+static void __init pud_clear_tests(struct pgtable_debug_args *args)
{
- pud_t pud = READ_ONCE(*pudp);
+ pud_t pud = READ_ONCE(*args->pudp);

- if (mm_pmd_folded(mm))
+ if (mm_pmd_folded(args->mm))
return;

pr_debug("Validating PUD clear\n");
pud = __pud(pud_val(pud) | RANDOM_ORVALUE);
- WRITE_ONCE(*pudp, pud);
- pud_clear(pudp);
- pud = READ_ONCE(*pudp);
+ WRITE_ONCE(*args->pudp, pud);
+ pud_clear(args->pudp);
+ pud = READ_ONCE(*args->pudp);
WARN_ON(!pud_none(pud));
}

-static void __init pud_populate_tests(struct mm_struct *mm, pud_t *pudp,
- pmd_t *pmdp)
+static void __init pud_populate_tests(struct pgtable_debug_args *args)
{
pud_t pud;

- if (mm_pmd_folded(mm))
+ if (mm_pmd_folded(args->mm))
return;

pr_debug("Validating PUD populate\n");
@@ -518,16 +508,13 @@ static void __init pud_populate_tests(struct mm_struct *mm, pud_t *pudp,
* This entry points to next level page table page.
* Hence this must not qualify as pud_bad().
*/
- pud_populate(mm, pudp, pmdp);
- pud = READ_ONCE(*pudp);
+ pud_populate(args->mm, args->pudp, args->start_pmdp);
+ pud = READ_ONCE(*args->pudp);
WARN_ON(pud_bad(pud));
}
#else /* !__PAGETABLE_PUD_FOLDED */
-static void __init pud_clear_tests(struct mm_struct *mm, pud_t *pudp) { }
-static void __init pud_populate_tests(struct mm_struct *mm, pud_t *pudp,
- pmd_t *pmdp)
-{
-}
+static void __init pud_clear_tests(struct pgtable_debug_args *args) { }
+static void __init pud_populate_tests(struct pgtable_debug_args *args) { }
#endif /* PAGETABLE_PUD_FOLDED */

#ifndef __PAGETABLE_P4D_FOLDED
@@ -1217,10 +1204,7 @@ static int __init debug_vm_pgtable(void)
pud_t *pudp, *saved_pudp;
pmd_t *pmdp, *saved_pmdp, pmd;
pgtable_t saved_ptep;
- pgprot_t prot;
- phys_addr_t paddr;
unsigned long vaddr;
- unsigned long pud_aligned;
spinlock_t *ptl = NULL;
int idx, ret;

@@ -1229,7 +1213,6 @@ static int __init debug_vm_pgtable(void)
if (ret)
return ret;

- prot = vm_get_page_prot(VMFLAGS);
vaddr = get_random_vaddr();
mm = mm_alloc();
if (!mm) {
@@ -1243,19 +1226,6 @@ static int __init debug_vm_pgtable(void)
return 1;
}

- /*
- * PFN for mapping at PTE level is determined from a standard kernel
- * text symbol. But pfns for higher page table levels are derived by
- * masking lower bits of this real pfn. These derived pfns might not
- * exist on the platform but that does not really matter as pfn_pxx()
- * helpers will still create appropriate entries for the test. This
- * helps avoid large memory block allocations to be used for mapping
- * at higher page table levels.
- */
- paddr = __pa_symbol(&start_kernel);
-
- pud_aligned = (paddr & PUD_MASK) >> PAGE_SHIFT;
-
pgdp = pgd_offset(mm, vaddr);
p4dp = p4d_alloc(mm, pgdp, vaddr);
pudp = pud_alloc(mm, p4dp, vaddr);
@@ -1348,11 +1318,11 @@ static int __init debug_vm_pgtable(void)
pmd_populate_tests(&args);
spin_unlock(ptl);

- ptl = pud_lock(mm, pudp);
- pud_clear_tests(mm, pudp);
- pud_advanced_tests(mm, vma, pudp, pud_aligned, vaddr, prot);
- pud_huge_tests(pudp, pud_aligned, prot);
- pud_populate_tests(mm, pudp, saved_pmdp);
+ ptl = pud_lock(args.mm, args.pudp);
+ pud_clear_tests(&args);
+ pud_advanced_tests(&args);
+ pud_huge_tests(&args);
+ pud_populate_tests(&args);
spin_unlock(ptl);

spin_lock(&mm->page_table_lock);
--
2.23.0

2021-08-10 16:45:10

by Gerald Schaefer

[permalink] [raw]
Subject: Re: [PATCH v6 00/12] mm/debug_vm_pgtable: Enhancements

On Mon, 9 Aug 2021 17:26:19 +0800
Gavin Shan <[email protected]> wrote:

> There are couple of issues with current implementations and this series
> tries to resolve the issues:
>
> (a) All needed information are scattered in variables, passed to various
> test functions. The code is organized in pretty much relaxed fashion.
>
> (b) The page isn't allocated from buddy during page table entry modifying
> tests. The page can be invalid, conflicting to the implementations
> of set_xxx_at() on ARM64. The target page is accessed so that the
> iCache can be flushed when execution permission is given on ARM64.
> Besides, the target page can be unmapped and accessing to it causes
> kernel crash.
>
> "struct pgtable_debug_args" is introduced to address issue (a). For issue
> (b), the used page is allocated from buddy in page table entry modifying
> tests. The corresponding tets will be skipped if we fail to allocate the
> (huge) page. For other test cases, the original page around to kernel
> symbol (@start_kernel) is still used.
>
> The patches are organized as below. PATCH[2-10] could be combined to one
> patch, but it will make the review harder:
>
> PATCH[1] introduces "struct pgtable_debug_args" as place holder of all
> needed information. With it, the old and new implementation
> can coexist.
> PATCH[2-10] uses "struct pgtable_debug_args" in various test functions.
> PATCH[11] removes the unused code for old implementation.
> PATCH[12] fixes the issue of corrupted page flag for ARM64

Tested on s390, all seems to work fine.

Gerald

2021-08-12 11:14:00

by Anshuman Khandual

[permalink] [raw]
Subject: Re: [PATCH v6 00/12] mm/debug_vm_pgtable: Enhancements



On 8/9/21 2:56 PM, Gavin Shan wrote:
> There are couple of issues with current implementations and this series
> tries to resolve the issues:
>
> (a) All needed information are scattered in variables, passed to various
> test functions. The code is organized in pretty much relaxed fashion.
>
> (b) The page isn't allocated from buddy during page table entry modifying
> tests. The page can be invalid, conflicting to the implementations
> of set_xxx_at() on ARM64. The target page is accessed so that the
> iCache can be flushed when execution permission is given on ARM64.
> Besides, the target page can be unmapped and accessing to it causes
> kernel crash.
>
> "struct pgtable_debug_args" is introduced to address issue (a). For issue
> (b), the used page is allocated from buddy in page table entry modifying
> tests. The corresponding tets will be skipped if we fail to allocate the
> (huge) page. For other test cases, the original page around to kernel
> symbol (@start_kernel) is still used.
>
> The patches are organized as below. PATCH[2-10] could be combined to one
> patch, but it will make the review harder:
>
> PATCH[1] introduces "struct pgtable_debug_args" as place holder of all
> needed information. With it, the old and new implementation
> can coexist.
> PATCH[2-10] uses "struct pgtable_debug_args" in various test functions.
> PATCH[11] removes the unused code for old implementation.
> PATCH[12] fixes the issue of corrupted page flag for ARM64
>
> Changelog
> =========
> v6:
> * Populate saved page table entry pointers after
> they're allocated in init_args() (Anshuman)
> * Fix imbalanced preemption count issue by replacing
> pte_alloc_mmap() with pte_alloc() in init_args() (syzbot)

+ [email protected]

Hello Gavin/Vineet,

This series still need to be tested on ARC ?

- Anshuman

2021-08-13 10:53:40

by Gavin Shan

[permalink] [raw]
Subject: Re: [PATCH v6 00/12] mm/debug_vm_pgtable: Enhancements

Hi Anshuman and Vineet,

On 8/12/21 9:11 PM, Anshuman Khandual wrote:
> On 8/9/21 2:56 PM, Gavin Shan wrote:
>> There are couple of issues with current implementations and this series
>> tries to resolve the issues:
>>
>> (a) All needed information are scattered in variables, passed to various
>> test functions. The code is organized in pretty much relaxed fashion.
>>
>> (b) The page isn't allocated from buddy during page table entry modifying
>> tests. The page can be invalid, conflicting to the implementations
>> of set_xxx_at() on ARM64. The target page is accessed so that the
>> iCache can be flushed when execution permission is given on ARM64.
>> Besides, the target page can be unmapped and accessing to it causes
>> kernel crash.
>>
>> "struct pgtable_debug_args" is introduced to address issue (a). For issue
>> (b), the used page is allocated from buddy in page table entry modifying
>> tests. The corresponding tets will be skipped if we fail to allocate the
>> (huge) page. For other test cases, the original page around to kernel
>> symbol (@start_kernel) is still used.
>>
>> The patches are organized as below. PATCH[2-10] could be combined to one
>> patch, but it will make the review harder:
>>
>> PATCH[1] introduces "struct pgtable_debug_args" as place holder of all
>> needed information. With it, the old and new implementation
>> can coexist.
>> PATCH[2-10] uses "struct pgtable_debug_args" in various test functions.
>> PATCH[11] removes the unused code for old implementation.
>> PATCH[12] fixes the issue of corrupted page flag for ARM64
>>
>> Changelog
>> =========
>> v6:
>> * Populate saved page table entry pointers after
>> they're allocated in init_args() (Anshuman)
>> * Fix imbalanced preemption count issue by replacing
>> pte_alloc_mmap() with pte_alloc() in init_args() (syzbot)
>
> + [email protected]
>
> Hello Gavin/Vineet,
>
> This series still need to be tested on ARC ?
>

Yes, I'm unable to test ARC as it's not supported by QEMU yet.
It would great if Vineet can give it a try on ARC :)

Thanks,
Gavin

2021-08-13 19:46:02

by Vineet Gupta

[permalink] [raw]
Subject: Re: [PATCH v6 00/12] mm/debug_vm_pgtable: Enhancements

On 8/13/21 2:25 AM, Gavin Shan wrote:
> Hi Anshuman and Vineet,
>
> On 8/12/21 9:11 PM, Anshuman Khandual wrote:
>> On 8/9/21 2:56 PM, Gavin Shan wrote:
>>> There are couple of issues with current implementations and this series
>>> tries to resolve the issues:
>>>
>>>    (a) All needed information are scattered in variables, passed to
>>> various
>>>        test functions. The code is organized in pretty much relaxed
>>> fashion.
>>>
>>>    (b) The page isn't allocated from buddy during page table entry
>>> modifying
>>>        tests. The page can be invalid, conflicting to the
>>> implementations
>>>        of set_xxx_at() on ARM64. The target page is accessed so that
>>> the
>>>        iCache can be flushed when execution permission is given on
>>> ARM64.
>>>        Besides, the target page can be unmapped and accessing to it
>>> causes
>>>        kernel crash.
>>>
>>> "struct pgtable_debug_args" is introduced to address issue (a). For
>>> issue
>>> (b), the used page is allocated from buddy in page table entry
>>> modifying
>>> tests. The corresponding tets will be skipped if we fail to allocate
>>> the
>>> (huge) page. For other test cases, the original page around to kernel
>>> symbol (@start_kernel) is still used.
>>>
>>> The patches are organized as below. PATCH[2-10] could be combined to
>>> one
>>> patch, but it will make the review harder:
>>>
>>>    PATCH[1] introduces "struct pgtable_debug_args" as place holder
>>> of all
>>>             needed information. With it, the old and new implementation
>>>             can coexist.
>>>    PATCH[2-10] uses "struct pgtable_debug_args" in various test
>>> functions.
>>>    PATCH[11] removes the unused code for old implementation.
>>>    PATCH[12] fixes the issue of corrupted page flag for ARM64
>>>
>>> Changelog
>>> =========
>>> v6:
>>>     * Populate saved page table entry pointers after
>>>       they're allocated in init_args() (Anshuman)
>>>     * Fix imbalanced preemption count issue by replacing
>>>       pte_alloc_mmap() with pte_alloc() in init_args() (syzbot)
>>
>> + [email protected]

Please also keep linux-snps-arc CC'ed for ARC changes.

>>
>> Hello Gavin/Vineet,
>>
>> This series still need to be tested on ARC ?
>>
>
> Yes, I'm unable to test ARC as it's not supported by QEMU yet.
> It would great if Vineet can give it a try on ARC :)

We do have a working QEMU (in the process of being upstreamed) you could try
https://github.com/foss-for-synopsys-dwc-arc-processors/qemu #master

Is this code in some shared git repo we can pull - instead of apply 8 files.

Thx,
-Vineet

2021-08-14 02:59:04

by Gavin Shan

[permalink] [raw]
Subject: Re: [PATCH v6 00/12] mm/debug_vm_pgtable: Enhancements

Hi Vineet,

On 8/14/21 5:43 AM, Vineet Gupta wrote:
> On 8/13/21 2:25 AM, Gavin Shan wrote:
>> On 8/12/21 9:11 PM, Anshuman Khandual wrote:
>>> On 8/9/21 2:56 PM, Gavin Shan wrote:
>>>> There are couple of issues with current implementations and this series
>>>> tries to resolve the issues:
>>>>
>>>>    (a) All needed information are scattered in variables, passed to various
>>>>        test functions. The code is organized in pretty much relaxed fashion.
>>>>
>>>>    (b) The page isn't allocated from buddy during page table entry modifying
>>>>        tests. The page can be invalid, conflicting to the implementations
>>>>        of set_xxx_at() on ARM64. The target page is accessed so that the
>>>>        iCache can be flushed when execution permission is given on ARM64.
>>>>        Besides, the target page can be unmapped and accessing to it causes
>>>>        kernel crash.
>>>>
>>>> "struct pgtable_debug_args" is introduced to address issue (a). For issue
>>>> (b), the used page is allocated from buddy in page table entry modifying
>>>> tests. The corresponding tets will be skipped if we fail to allocate the
>>>> (huge) page. For other test cases, the original page around to kernel
>>>> symbol (@start_kernel) is still used.
>>>>
>>>> The patches are organized as below. PATCH[2-10] could be combined to one
>>>> patch, but it will make the review harder:
>>>>
>>>>    PATCH[1] introduces "struct pgtable_debug_args" as place holder of all
>>>>             needed information. With it, the old and new implementation
>>>>             can coexist.
>>>>    PATCH[2-10] uses "struct pgtable_debug_args" in various test functions.
>>>>    PATCH[11] removes the unused code for old implementation.
>>>>    PATCH[12] fixes the issue of corrupted page flag for ARM64
>>>>
>>>> Changelog
>>>> =========
>>>> v6:
>>>>     * Populate saved page table entry pointers after
>>>>       they're allocated in init_args() (Anshuman)
>>>>     * Fix imbalanced preemption count issue by replacing
>>>>       pte_alloc_mmap() with pte_alloc() in init_args() (syzbot)
>>>
>>> + [email protected]
>
> Please also keep linux-snps-arc CC'ed for ARC changes.
>

Sure.

>>>
>>> Hello Gavin/Vineet,
>>>
>>> This series still need to be tested on ARC ?
>>>
>>
>> Yes, I'm unable to test ARC as it's not supported by QEMU yet.
>> It would great if Vineet can give it a try on ARC :)
>
> We do have a working QEMU (in the process of being upstreamed) you could try
> https://github.com/foss-for-synopsys-dwc-arc-processors/qemu #master
>
> Is this code in some shared git repo we can pull - instead of apply 8 files.
>

Here is repo with v6 series included. Please help to have
a try.

https://github.com/gwshan/linux # kvm/arm64_debug

I tried to build the cross-compiler with buildroot with following
2 repos. Unfortunately, none of them worked for me. The QEMU
binary can be built successfully with the provided repo though.


[1] git://github.com/foss-for-synopsys-dwc-arc-processors/buildroot.git
(branch: master)

There are build errors after choosing little-endian ARM from
"make menuconfig"

[2] https://github.com/buildroot/buildroot
(branch: master)

The build is successful, but the kernel can't be built because
of the following errors:

# make ARCH=arc CROSS_COMPILE=arc-linux- axs103_smp_defconfig
# make -j 40
:
LD vmlinux.o
arc-linux-ld: error: attempting to link /home/gavin/sandbox/buildroot/buildroot.arc/output/host/lib
/gcc/arc-buildroot-linux-uclibc/10.2.0/libgcc.a(_muldi3.o) with a binary vmlinux.o of different
architecture

Thanks,
Gavin