linux-riscv.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/2] Fix the page table size of KASAN use.
@ 2020-02-07  9:52 Zong Li
  2020-02-07  9:52 ` [PATCH 1/2] riscv: allocate a complete page size for each page table Zong Li
  2020-02-07  9:52 ` [PATCH 2/2] riscv: adjust the indent Zong Li
  0 siblings, 2 replies; 5+ messages in thread
From: Zong Li @ 2020-02-07  9:52 UTC (permalink / raw)
  To: paul.walmsley, palmer, aou, linux-riscv, linux-kernel; +Cc: Zong Li

Each page table should be created by allocating a complete page size
for it. Otherwise, the content of the page table would be corrupted
somewhere through memory allocation which allocates the memory at the
middle of the page table for other use. For example, if it only
allocates 200 pmd entries memory size for pmd page table, then the
original 201 entry will be used to other purpose, and cause the
unexpected fault when accessing the page table.

Zong Li (2):
  riscv: allocate a complete page size for each page table
  riscv: adjust the indent

 arch/riscv/mm/kasan_init.c | 53 ++++++++++++++++++++++----------------
 1 file changed, 31 insertions(+), 22 deletions(-)

-- 
2.25.0



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

* [PATCH 1/2] riscv: allocate a complete page size for each page table
  2020-02-07  9:52 [PATCH 0/2] Fix the page table size of KASAN use Zong Li
@ 2020-02-07  9:52 ` Zong Li
  2020-02-07  9:52 ` [PATCH 2/2] riscv: adjust the indent Zong Li
  1 sibling, 0 replies; 5+ messages in thread
From: Zong Li @ 2020-02-07  9:52 UTC (permalink / raw)
  To: paul.walmsley, palmer, aou, linux-riscv, linux-kernel; +Cc: Zong Li

Each page table should be created by allocating a complete page size
for it. Otherwise, the content of the page table would be corrupted
somewhere through memory allocation which allocates the memory at the
middle of the page table for other use.

Signed-off-by: Zong Li <zong.li@sifive.com>
---
 arch/riscv/mm/kasan_init.c | 27 ++++++++++++++++-----------
 1 file changed, 16 insertions(+), 11 deletions(-)

diff --git a/arch/riscv/mm/kasan_init.c b/arch/riscv/mm/kasan_init.c
index f0cc86040587..f8eaf7e73a23 100644
--- a/arch/riscv/mm/kasan_init.c
+++ b/arch/riscv/mm/kasan_init.c
@@ -46,29 +46,34 @@ asmlinkage void __init kasan_early_init(void)
 
 static void __init populate(void *start, void *end)
 {
-	unsigned long i;
+	unsigned long i, offset;
 	unsigned long vaddr = (unsigned long)start & PAGE_MASK;
 	unsigned long vend = PAGE_ALIGN((unsigned long)end);
 	unsigned long n_pages = (vend - vaddr) / PAGE_SIZE;
+	unsigned long n_ptes =
+	    ((n_pages + PTRS_PER_PTE) & -PTRS_PER_PTE) / PTRS_PER_PTE;
 	unsigned long n_pmds =
-		(n_pages % PTRS_PER_PTE) ? n_pages / PTRS_PER_PTE + 1 :
-						n_pages / PTRS_PER_PTE;
+	    ((n_ptes + PTRS_PER_PMD) & -PTRS_PER_PMD) / PTRS_PER_PMD;
+
+	pte_t *pte =
+	    memblock_alloc(n_ptes * PTRS_PER_PTE * sizeof(pte_t), PAGE_SIZE);
+	pmd_t *pmd =
+	    memblock_alloc(n_pmds * PTRS_PER_PMD * sizeof(pmd_t), PAGE_SIZE);
 	pgd_t *pgd = pgd_offset_k(vaddr);
-	pmd_t *pmd = memblock_alloc(n_pmds * sizeof(pmd_t), PAGE_SIZE);
-	pte_t *pte = memblock_alloc(n_pages * sizeof(pte_t), PAGE_SIZE);
 
 	for (i = 0; i < n_pages; i++) {
 		phys_addr_t phys = memblock_phys_alloc(PAGE_SIZE, PAGE_SIZE);
-
-		set_pte(pte + i, pfn_pte(PHYS_PFN(phys), PAGE_KERNEL));
+		set_pte(&pte[i], pfn_pte(PHYS_PFN(phys), PAGE_KERNEL));
 	}
 
-	for (i = 0; i < n_pmds; ++pgd, i += PTRS_PER_PMD)
-		set_pgd(pgd, pfn_pgd(PFN_DOWN(__pa(((uintptr_t)(pmd + i)))),
+	for (i = 0, offset = 0; i < n_ptes; i++, offset += PTRS_PER_PTE)
+		set_pmd(&pmd[i],
+			pfn_pmd(PFN_DOWN(__pa(&pte[offset])),
 				__pgprot(_PAGE_TABLE)));
 
-	for (i = 0; i < n_pages; ++pmd, i += PTRS_PER_PTE)
-		set_pmd(pmd, pfn_pmd(PFN_DOWN(__pa((uintptr_t)(pte + i))),
+	for (i = 0, offset = 0; i < n_pmds; i++, offset += PTRS_PER_PMD)
+		set_pgd(&pgd[i],
+			pfn_pgd(PFN_DOWN(__pa(&pmd[offset])),
 				__pgprot(_PAGE_TABLE)));
 
 	flush_tlb_all();
-- 
2.25.0



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

* [PATCH 2/2] riscv: adjust the indent
  2020-02-07  9:52 [PATCH 0/2] Fix the page table size of KASAN use Zong Li
  2020-02-07  9:52 ` [PATCH 1/2] riscv: allocate a complete page size for each page table Zong Li
@ 2020-02-07  9:52 ` Zong Li
  2020-02-07 10:58   ` Joe Perches
  1 sibling, 1 reply; 5+ messages in thread
From: Zong Li @ 2020-02-07  9:52 UTC (permalink / raw)
  To: paul.walmsley, palmer, aou, linux-riscv, linux-kernel; +Cc: Zong Li

Adjust the indent to match Linux coding style.

Signed-off-by: Zong Li <zong.li@sifive.com>
---
 arch/riscv/mm/kasan_init.c | 26 +++++++++++++++-----------
 1 file changed, 15 insertions(+), 11 deletions(-)

diff --git a/arch/riscv/mm/kasan_init.c b/arch/riscv/mm/kasan_init.c
index f8eaf7e73a23..ec0ca90dd900 100644
--- a/arch/riscv/mm/kasan_init.c
+++ b/arch/riscv/mm/kasan_init.c
@@ -19,18 +19,20 @@ asmlinkage void __init kasan_early_init(void)
 	for (i = 0; i < PTRS_PER_PTE; ++i)
 		set_pte(kasan_early_shadow_pte + i,
 			mk_pte(virt_to_page(kasan_early_shadow_page),
-			PAGE_KERNEL));
+			       PAGE_KERNEL));
 
 	for (i = 0; i < PTRS_PER_PMD; ++i)
 		set_pmd(kasan_early_shadow_pmd + i,
-		 pfn_pmd(PFN_DOWN(__pa((uintptr_t)kasan_early_shadow_pte)),
-			__pgprot(_PAGE_TABLE)));
+			pfn_pmd(PFN_DOWN
+				(__pa((uintptr_t) kasan_early_shadow_pte)),
+				__pgprot(_PAGE_TABLE)));
 
 	for (i = KASAN_SHADOW_START; i < KASAN_SHADOW_END;
 	     i += PGDIR_SIZE, ++pgd)
 		set_pgd(pgd,
-		 pfn_pgd(PFN_DOWN(__pa(((uintptr_t)kasan_early_shadow_pmd))),
-			__pgprot(_PAGE_TABLE)));
+			pfn_pgd(PFN_DOWN
+				(__pa(((uintptr_t) kasan_early_shadow_pmd))),
+				__pgprot(_PAGE_TABLE)));
 
 	/* init for swapper_pg_dir */
 	pgd = pgd_offset_k(KASAN_SHADOW_START);
@@ -38,8 +40,9 @@ asmlinkage void __init kasan_early_init(void)
 	for (i = KASAN_SHADOW_START; i < KASAN_SHADOW_END;
 	     i += PGDIR_SIZE, ++pgd)
 		set_pgd(pgd,
-		 pfn_pgd(PFN_DOWN(__pa(((uintptr_t)kasan_early_shadow_pmd))),
-			__pgprot(_PAGE_TABLE)));
+			pfn_pgd(PFN_DOWN
+				(__pa(((uintptr_t) kasan_early_shadow_pmd))),
+				__pgprot(_PAGE_TABLE)));
 
 	flush_tlb_all();
 }
@@ -86,7 +89,8 @@ void __init kasan_init(void)
 	unsigned long i;
 
 	kasan_populate_early_shadow((void *)KASAN_SHADOW_START,
-			(void *)kasan_mem_to_shadow((void *)VMALLOC_END));
+				    (void *)kasan_mem_to_shadow((void *)
+								VMALLOC_END));
 
 	for_each_memblock(memory, reg) {
 		void *start = (void *)__va(reg->base);
@@ -95,14 +99,14 @@ void __init kasan_init(void)
 		if (start >= end)
 			break;
 
-		populate(kasan_mem_to_shadow(start),
-			 kasan_mem_to_shadow(end));
+		populate(kasan_mem_to_shadow(start), kasan_mem_to_shadow(end));
 	};
 
 	for (i = 0; i < PTRS_PER_PTE; i++)
 		set_pte(&kasan_early_shadow_pte[i],
 			mk_pte(virt_to_page(kasan_early_shadow_page),
-			__pgprot(_PAGE_PRESENT | _PAGE_READ | _PAGE_ACCESSED)));
+			       __pgprot(_PAGE_PRESENT | _PAGE_READ |
+					_PAGE_ACCESSED)));
 
 	memset(kasan_early_shadow_page, 0, PAGE_SIZE);
 	init_task.kasan_depth = 0;
-- 
2.25.0



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

* Re: [PATCH 2/2] riscv: adjust the indent
  2020-02-07  9:52 ` [PATCH 2/2] riscv: adjust the indent Zong Li
@ 2020-02-07 10:58   ` Joe Perches
  2020-02-08  8:45     ` Zong Li
  0 siblings, 1 reply; 5+ messages in thread
From: Joe Perches @ 2020-02-07 10:58 UTC (permalink / raw)
  To: Zong Li, paul.walmsley, palmer, aou, linux-riscv, linux-kernel

On Fri, 2020-02-07 at 17:52 +0800, Zong Li wrote:
> Adjust the indent to match Linux coding style.

trivia:

> diff --git a/arch/riscv/mm/kasan_init.c b/arch/riscv/mm/kasan_init.c
[]
> @@ -86,7 +89,8 @@ void __init kasan_init(void)
>  	unsigned long i;
>  
>  	kasan_populate_early_shadow((void *)KASAN_SHADOW_START,
> -			(void *)kasan_mem_to_shadow((void *)VMALLOC_END));
> +				    (void *)kasan_mem_to_shadow((void *)
> +								VMALLOC_END));

could also remove an unnecessary (void *) as kasan_mem_to_shadow
returns a void *

	kasan_populate_early_shadow((void *)KASAN_SHADOW_START,
				    kasan_mem_to_shadow((void *)VMALLOC_END));



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

* Re: [PATCH 2/2] riscv: adjust the indent
  2020-02-07 10:58   ` Joe Perches
@ 2020-02-08  8:45     ` Zong Li
  0 siblings, 0 replies; 5+ messages in thread
From: Zong Li @ 2020-02-08  8:45 UTC (permalink / raw)
  To: Joe Perches
  Cc: linux-riscv, Albert Ou, Palmer Dabbelt,
	linux-kernel@vger.kernel.org List, Paul Walmsley

On Fri, Feb 7, 2020 at 6:59 PM Joe Perches <joe@perches.com> wrote:
>
> On Fri, 2020-02-07 at 17:52 +0800, Zong Li wrote:
> > Adjust the indent to match Linux coding style.
>
> trivia:
>
> > diff --git a/arch/riscv/mm/kasan_init.c b/arch/riscv/mm/kasan_init.c
> []
> > @@ -86,7 +89,8 @@ void __init kasan_init(void)
> >       unsigned long i;
> >
> >       kasan_populate_early_shadow((void *)KASAN_SHADOW_START,
> > -                     (void *)kasan_mem_to_shadow((void *)VMALLOC_END));
> > +                                 (void *)kasan_mem_to_shadow((void *)
> > +                                                             VMALLOC_END));
>
> could also remove an unnecessary (void *) as kasan_mem_to_shadow
> returns a void *
>
>         kasan_populate_early_shadow((void *)KASAN_SHADOW_START,
>                                     kasan_mem_to_shadow((void *)VMALLOC_END));
>

OK, I'll remove it in next version. Thanks.


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

end of thread, other threads:[~2020-02-08  8:45 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-02-07  9:52 [PATCH 0/2] Fix the page table size of KASAN use Zong Li
2020-02-07  9:52 ` [PATCH 1/2] riscv: allocate a complete page size for each page table Zong Li
2020-02-07  9:52 ` [PATCH 2/2] riscv: adjust the indent Zong Li
2020-02-07 10:58   ` Joe Perches
2020-02-08  8:45     ` Zong Li

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).