Skip to contentSkip to conversation

mm: flush dcache before writing into page to avoid alias

PATCH In review [email protected] 3 participants 3 messages Feb 2, 2010 – Feb 4, 2010

Subsystems (4)
lore
Share & tools
lore

[email protected]

author maintainer
#0 lore raw

PATCH 5/16 mm: flush dcache before writing into page to avoid alias

From: anfei zhou <[email protected]>

The cache alias problem will happen if the changes of user shared mapping is not flushed before copying, then user and kernel mapping may be mapped into two different cache line, it is impossible to guarantee the coherence after iov_iter_copy_from_user_atomic. So the right steps should be:

	flush_dcache_page(page);
	kmap_atomic(page);
	write to page;
	kunmap_atomic(page);
	flush_dcache_page(page);

More precisely, we might create two new APIs flush_dcache_user_page and flush_dcache_kern_page to replace the two flush_dcache_page accordingly.

Here is a snippet tested on omap2430 with VIPT cache, and I think it is not ARM-specific:

	int val = 0x11111111;
	fd = open("abc", O_RDWR);
	addr = mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
	*(addr+0) = 0x44444444;
	tmp = *(addr+0);
	*(addr+1) = 0x77777777;
	write(fd, &val, sizeof(int));
	close(fd);

The results are not always 0x11111111 0x77777777 at the beginning as expected. Sometimes we see 0x44444444 0x77777777.

Cc Russell King <[email protected]>
Cc Miklos Szeredi <[email protected]>
Cc Nick Piggin <[email protected]>
Cc Linus Torvalds <[email protected]>

---

 fs/fuse/file.c |    3 +++
 mm/filemap.c   |    3 +++
 2 files changed, 6 insertions(+)
fs/fuse/file.c renamed from fs/fuse/file.c~mm-flush-dcache-before-writing-into-page-to-avoid-alias +3 -0
diff -puN fs/fuse/file.c~mm-flush-dcache-before-writing-into-page-to-avoid-alias fs/fuse/file.c
@@ -828,6 +828,9 @@ static ssize_t fuse_fill_write_pages(str
 		if (!page)
 			break;
 
+		if (mapping_writably_mapped(mapping))
+			flush_dcache_page(page);
+
 		pagefault_disable();
 		tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
 		pagefault_enable();
mm/filemap.c renamed from mm/filemap.c~mm-flush-dcache-before-writing-into-page-to-avoid-alias +3 -0
diff -puN mm/filemap.c~mm-flush-dcache-before-writing-into-page-to-avoid-alias mm/filemap.c
@@ -2232,6 +2232,9 @@ again:
 		if (unlikely(status))
 			break;
 
+		if (mapping_writably_mapped(mapping))
+			flush_dcache_page(page);
+
 		pagefault_disable();
 		copied = iov_iter_copy_from_user_atomic(page, i, offset, bytes);
 		pagefault_enable();

_

#1 lore raw
Quoted 8 lines · On Tue, 2010-02-02 at 13:44 -0800, [email protected] wrote:

From: anfei zhou <[email protected]>

The cache alias problem will happen if the changes of user shared mapping is not flushed before copying, then user and kernel mapping may be mapped into two different cache line, it is impossible to guarantee the coherence after iov_iter_copy_from_user_atomic. So the right steps should be:

	flush_dcache_page(page);

This is likely unnecessary if the page has come down a standard path ... for fuse, it is possible it didn't go through __get_user_pages().

Quoted 7 lines
	kmap_atomic(page);
	write to page;
	kunmap_atomic(page);
	flush_dcache_page(page);

More precisely, we might create two new APIs flush_dcache_user_page and flush_dcache_kern_page to replace the two flush_dcache_page accordingly.

We already have one of those: flush_kernel_dcache_page().

James

#2 lore raw
Quoted 8 lines · James Bottomley
Quoted 7 lines · On Tue, 2010-02-02 at 13:44 -0800, [email protected] wrote:

From: anfei zhou <[email protected]>

The cache alias problem will happen if the changes of user shared mapping is not flushed before copying, then user and kernel mapping may be mapped into two different cache line, it is impossible to guarantee the coherence after iov_iter_copy_from_user_atomic. So the right steps should be:

Quoted 5 lines
Quoted 1 line
	flush_dcache_page(page);

This is likely unnecessary if the page has come down a standard path ... for fuse, it is possible it didn't go through __get_user_pages().

The problem here is iov_iter_copy_from_user_atomic will write the page, and that page has been written by (*addr = val) before, but not flushed yet. If these two writes are at the same cache line (but aliased), then we can not guarantee the result by any order.

Quoted 10 lines
Quoted 7 lines
	kmap_atomic(page);
	write to page;
	kunmap_atomic(page);
	flush_dcache_page(page);

More precisely, we might create two new APIs flush_dcache_user_page and flush_dcache_kern_page to replace the two flush_dcache_page accordingly.

We already have one of those: flush_kernel_dcache_page().

Yes, but it's only called in a few places, and others usually use flush_dcache_page, why?

And not every arch with aliasing problem has implement it, such as MIPS.

Regards,
Anfei.

Quoted 3 lines

James