Skip to contentSkip to conversation

nds32: Correct the cache operation for catch aliasing case

PATCH In review Vincent Chen 2 participants 5 messages May 15, 2018 – May 17, 2018

lore
Share & tools
lore
#0 lore raw

cover 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(-)
#1 lore raw

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.

---
 arch/nds32/mm/cacheflush.c |   21 ++++++++++++---------
 1 files changed, 12 insertions(+), 9 deletions(-)
arch/nds32/mm/cacheflush.c +12 -9
index 6eb786a..288cf10 100644
@@ -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);
 	}
Signature · 1 line
1.7.1
#2 lore raw

PATCH 2/3 nds32: Flush the cache of the page at vmaddr instead of kaddr in flush_anon_page

In reply to Vincent Chen

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

---
 arch/nds32/mm/cacheflush.c |    9 +++++++--
 1 files changed, 7 insertions(+), 2 deletions(-)
arch/nds32/mm/cacheflush.c +7 -2
index 288cf10..acfdb45 100644
@@ -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);
 }
 
Signature · 1 line
1.7.1
#3 lore raw

PATCH 3/3 nds32: Disable local irq before calling cpu_dcache_wb_page in copy_user_highpage

In reply to Vincent Chen

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.

---
 arch/nds32/mm/cacheflush.c |    4 +---
 1 files changed, 1 insertions(+), 3 deletions(-)
arch/nds32/mm/cacheflush.c +1 -3
index acfdb45..c97caaf 100644
@@ -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);
Signature · 1 line
1.7.1
Reviewed-by on the v1 series
#4 lore raw
In reply to Vincent Chen

2018-05-15 16:46 GMT+08:00 Vincent Chen <[email protected]>:

Quoted 23 lines

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

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

Thank you, Vincent.