2018-05-15 08:59:34

by Vincent Chen

[permalink] [raw]
Subject: [PATCH 0/3]nds32: Correct the cache operation for catch aliasing case

The following 3 issues are fixed in this patchset

1. In function flush_dacache_page and copy_user_highpage, the local irq is
enabled when the cache of the page at address page_address(page) is written
back to memory. It possibly causes data corruption. To fix this problem,
the local irq is disabled before executing d-cache write-back and
invalidate in this patchset.

2. According to Documentation/cachetlb.txt, the cache of the page at vmaddr
shall be flushed in flush_anon_page instead of the cache of the page at
page_address(page). We correct it and add the modification to this
patchset.

3. Removing unneeded cache invalidation in copy_user_highpage function.


Vincent Chen (3):
nds32: Correct flush_dcache_page function
nds32: Flush the cache of the page at vmaddr instead of kaddr in
flush_anon_page
nds32: Disable local irq before calling cpu_dcache_wb_page in
copy_user_highpage

arch/nds32/mm/cacheflush.c | 34 ++++++++++++++++++++--------------
1 files changed, 20 insertions(+), 14 deletions(-)



2018-05-15 08:58:45

by Vincent Chen

[permalink] [raw]
Subject: [PATCH 1/3] nds32: Correct flush_dcache_page function

1. Disable local irq before d-cache write-back and invalidate.
The cpu_dcache_wbinval_page function is composed of d-cache
write-back and invalidate. If the local irq is enabled when calling
cpu_dcache_wbinval_page, the content of d-cache is possibly updated
between write-back and invalidate. In this case, the updated data will
be dropped due to the following d-cache invalidation. Therefore, we
disable the local irq before calling cpu_dcache_wbinval_page.

2. Correct the data write-back for page aliasing case.
Only the page whose (page->index << PAGE_SHIFT) is located at the
same page color as page_address(page) needs to execute data write-back
in flush_dcache_page function.

Signed-off-by: Vincent Chen <[email protected]>
---
arch/nds32/mm/cacheflush.c | 21 ++++++++++++---------
1 files changed, 12 insertions(+), 9 deletions(-)

diff --git a/arch/nds32/mm/cacheflush.c b/arch/nds32/mm/cacheflush.c
index 6eb786a..288cf10 100644
--- a/arch/nds32/mm/cacheflush.c
+++ b/arch/nds32/mm/cacheflush.c
@@ -198,17 +198,20 @@ void flush_dcache_page(struct page *page)
if (mapping && !mapping_mapped(mapping))
set_bit(PG_dcache_dirty, &page->flags);
else {
- int i, pc;
- unsigned long vto, kaddr, flags;
+ unsigned long kaddr, flags;
+
kaddr = (unsigned long)page_address(page);
- cpu_dcache_wbinval_page(kaddr);
- pc = CACHE_SET(DCACHE) * CACHE_LINE_SIZE(DCACHE) / PAGE_SIZE;
local_irq_save(flags);
- for (i = 0; i < pc; i++) {
- vto =
- kremap0(kaddr + i * PAGE_SIZE, page_to_phys(page));
- cpu_dcache_wbinval_page(vto);
- kunmap01(vto);
+ cpu_dcache_wbinval_page(kaddr);
+ if (mapping) {
+ unsigned long vaddr, kto;
+
+ vaddr = page->index << PAGE_SHIFT;
+ if (aliasing(vaddr, kaddr)) {
+ kto = kremap0(vaddr, page_to_phys(page));
+ cpu_dcache_wbinval_page(kto);
+ kunmap01(kto);
+ }
}
local_irq_restore(flags);
}
--
1.7.1


2018-05-15 08:59:05

by Vincent Chen

[permalink] [raw]
Subject: [PATCH 3/3] nds32: Disable local irq before calling cpu_dcache_wb_page in copy_user_highpage

In order to ensure that all data in source page has been written back
to memory before copy_page, the local irq shall be disabled before
calling cpu_dcache_wb_page(). In addition, removing unneeded page
invalidation for 'to' page.

Signed-off-by: Vincent Chen <[email protected]>
---
arch/nds32/mm/cacheflush.c | 4 +---
1 files changed, 1 insertions(+), 3 deletions(-)

diff --git a/arch/nds32/mm/cacheflush.c b/arch/nds32/mm/cacheflush.c
index acfdb45..c97caaf 100644
--- a/arch/nds32/mm/cacheflush.c
+++ b/arch/nds32/mm/cacheflush.c
@@ -156,11 +156,9 @@ void copy_user_highpage(struct page *to, struct page *from,
pto = page_to_phys(to);
pfrom = page_to_phys(from);

+ local_irq_save(flags);
if (aliasing(vaddr, (unsigned long)kfrom))
cpu_dcache_wb_page((unsigned long)kfrom);
- if (aliasing(vaddr, (unsigned long)kto))
- cpu_dcache_inval_page((unsigned long)kto);
- local_irq_save(flags);
vto = kremap0(vaddr, pto);
vfrom = kremap1(vaddr, pfrom);
copy_page((void *)vto, (void *)vfrom);
--
1.7.1


2018-05-15 09:02:02

by Vincent Chen

[permalink] [raw]
Subject: [PATCH 2/3] nds32: Flush the cache of the page at vmaddr instead of kaddr in flush_anon_page

According to Documentation/cachetlb.txt, the cache of the page at vmaddr
shall be flushed in flush_anon_page instead of the cache of the page at
page_address(page).

Signed-off-by: Vincent Chen <[email protected]>
---
arch/nds32/mm/cacheflush.c | 9 +++++++--
1 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/arch/nds32/mm/cacheflush.c b/arch/nds32/mm/cacheflush.c
index 288cf10..acfdb45 100644
--- a/arch/nds32/mm/cacheflush.c
+++ b/arch/nds32/mm/cacheflush.c
@@ -254,7 +254,7 @@ void copy_from_user_page(struct vm_area_struct *vma, struct page *page,
void flush_anon_page(struct vm_area_struct *vma,
struct page *page, unsigned long vaddr)
{
- unsigned long flags;
+ unsigned long kaddr, flags, ktmp;
if (!PageAnon(page))
return;

@@ -264,7 +264,12 @@ void flush_anon_page(struct vm_area_struct *vma,
local_irq_save(flags);
if (vma->vm_flags & VM_EXEC)
cpu_icache_inval_page(vaddr & PAGE_MASK);
- cpu_dcache_wbinval_page((unsigned long)page_address(page));
+ kaddr = (unsigned long)page_address(page);
+ if (aliasing(vaddr, kaddr)) {
+ ktmp = kremap0(vaddr, page_to_phys(page));
+ cpu_dcache_wbinval_page(ktmp);
+ kunmap01(ktmp);
+ }
local_irq_restore(flags);
}

--
1.7.1


2018-05-17 07:00:00

by Greentime Hu

[permalink] [raw]
Subject: Re: [PATCH 0/3]nds32: Correct the cache operation for catch aliasing case

2018-05-15 16:46 GMT+08:00 Vincent Chen <[email protected]>:
> The following 3 issues are fixed in this patchset
>
> 1. In function flush_dacache_page and copy_user_highpage, the local irq is
> enabled when the cache of the page at address page_address(page) is written
> back to memory. It possibly causes data corruption. To fix this problem,
> the local irq is disabled before executing d-cache write-back and
> invalidate in this patchset.
>
> 2. According to Documentation/cachetlb.txt, the cache of the page at vmaddr
> shall be flushed in flush_anon_page instead of the cache of the page at
> page_address(page). We correct it and add the modification to this
> patchset.
>
> 3. Removing unneeded cache invalidation in copy_user_highpage function.
>
>
> Vincent Chen (3):
> nds32: Correct flush_dcache_page function
> nds32: Flush the cache of the page at vmaddr instead of kaddr in
> flush_anon_page
> nds32: Disable local irq before calling cpu_dcache_wb_page in
> copy_user_highpage
>
> arch/nds32/mm/cacheflush.c | 34 ++++++++++++++++++++--------------
> 1 files changed, 20 insertions(+), 14 deletions(-)
>

Thank you, Vincent.
Reviewed-by: Greentime Hu <[email protected]>