Skip to contentSkip to conversation

flush_cache_page in kernel/ptrace.c

RFC Russell King 2 participants 4 messages Sep 9, 2004 – Sep 17, 2004

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

Hi,

(This has turned into something of a rambling message, sorry.)

I'm presently looking at sorting out cache issues on ARM VIPT aliasing caches, and I'm concerned with the above usage of flush_cache_page().

At present, flush_cache_page() is used to handle the case where we unmap a page or alter the page permissions on the target page with one exception - access_process_vm(). Based upon the former, the decision to implement this function is:

do we need to flush the cache when we unmap or change the mapping permissions?

However, access_process_vm() also includes into this:

or we need to ensure cache coherency between the kernel and user space mapping of this page.

I argue that the use of flush_cache_page() here in the generic code is wrong, and if an architecture wishes to use it for this purpose, it should do so within it's architecture private implementation of copy_to_user_page() and copy_from_user_page().

Further, I wish to question why access_process_vm() is different from the case where a user binary has a shared file mmapping and another process writes data into that file. In this case we make no attempt to call flush_cache_page() prior to writing the page cache page, yet we do ultimately expect the same behaviour.

As for why... In the case of kernel/ptrace.c, the VIPT cache I'm looking at would need:

- flush_cache_page would have to set up a temporary mapping of the same cache colour as the user mapping of the page, then flush the cache line entries in that user coherent mapping, and tear it down.

- copy_*_user_page() reads/writes the kernel mapping, and then uses flush_dcache_page() to ensure coherency

- flush_dcache_page() then flushes the kernel mapping, and then tries to find a user space mapping of the page, or will setup it's own mapping and flush that.

IOW, flush_cache_page() and flush_dcache_page() are both grossly inefficient and overhanded for what needs to be done here, which can be done far more effectively inside an appropriate copy_*_user_page() implementation:

- map the page at an user coherent mapping
- flush cache lines for the affected region in the user coherent and kernel mappings
- copy data to/from kernel mapping and, if copy_to_user_page, ensure it's written back
- tear down user coherent mapping

Rambling on a bit, what this then gives me is a sane and clean usage model for flush_cache_page(). At this point, all users of flush_cache_page() have the PFN for the page to be flushed readily available.

Why is this important? Because on ARM VIPT caches, to be able to flush cache lines, we need to have a mapping setup which corresponds to the cache colour of the desired lines to be flushed - which in turn means that flush_cache_page() may need to create its own mapping, especially if it's being asked to operate on a page outside our currently visible MM space.

Why can't the page tables be walked to obtain this information? Check out pte_offset_map() / pte_offset_map_nested() - it would be a violation of the semantics of these to call either inside flush_dcache_page().

Comments?

(And am I hating these aliasing VIPT writeback caches yet? Most
certainly I am.)

Signature · 4 lines
Russell King
 Linux kernel    2.6 ARM Linux   - http://www.arm.linux.org.uk/
 maintainer of:  2.6 PCMCIA      - http://pcmcia.arm.linux.org.uk/
                 2.6 Serial core
#1 lore raw
Quoted 5 lines · Russell King

I argue that the use of flush_cache_page() here in the generic code is wrong, and if an architecture wishes to use it for this purpose, it should do so within it's architecture private implementation of copy_to_user_page() and copy_from_user_page().

I can't fault that argument. Let's just make it so. Can we have the arch people involved (the normal virtual cache suspects: mips, sparc, sh, pa-risc, who else?) take a quick look and send their suggested patch to do so for their architecture.

I hereby nominate Russell to be President, Vice General and Grand Poobah of said patches, and charge him to remove the generic version line at the appropriate time.. In recognition of this service, he'll get an extra "l" to make up for all the ones he has lost over time.

Russelll?

Linus

#2 lore raw
Quoted 11 lines · Linus Torvalds
Quoted 4 lines · Russell King

I argue that the use of flush_cache_page() here in the generic code is wrong, and if an architecture wishes to use it for this purpose, it should do so within it's architecture private implementation of copy_to_user_page() and copy_from_user_page().

I can't fault that argument. Let's just make it so. Can we have the arch people involved (the normal virtual cache suspects: mips, sparc, sh, pa-risc, who else?) take a quick look and send their suggested patch to do so for their architecture.

Quoted 6 lines

I hereby nominate Russell to be President, Vice General and Grand Poobah of said patches, and charge him to remove the generic version line at the appropriate time.. In recognition of this service, he'll get an extra "l" to make up for all the ones he has lost over time.

Russelll?

That's fine by me - please send your patches my way. 8)

Thanks.

Signature · 4 lines
Russell King
 Linux kernel    2.6 ARM Linux   - http://www.arm.linux.org.uk/
 maintainer of:  2.6 PCMCIA      - http://pcmcia.arm.linux.org.uk/
                 2.6 Serial core
#3 lore raw
Quoted 1 line · Russell King

That's fine by me - please send your patches my way. 8)

Here's a patch which I've put together to achieve the first stage. We remove flush_cache_page() from kernel/ptrace.c, adding it to the arch-specific copy_{to,from}_user_page() where flush_cache_page is non-empty.

Can architecture people test and check this patch please?

Thanks.

===== Documentation/cachetlb.txt 1.11 vs edited =====

Documentation/cachetlb.txt +0 -4
@@ -354,10 +354,6 @@
 	of arbitrary user pages (f.e. for ptrace()) it will use
 	these two routines.
 
-	The page has been kmap()'d, and flush_cache_page() has
-	just been called for the user mapping of this page (if
-	necessary).
-
 	Any necessary cache flushing or other coherency operations
 	that need to occur should happen here.  If the processor's
 	instruction cache does not snoop cpu stores, it is very

===== kernel/ptrace.c 1.36 vs edited =====

kernel/ptrace.c +0 -2
@@ -202,8 +202,6 @@
 		if (bytes > PAGE_SIZE-offset)
 			bytes = PAGE_SIZE-offset;
 
-		flush_cache_page(vma, addr);
-
 		maddr = kmap(page);
 		if (write) {
 			copy_to_user_page(vma, page, addr,

===== include/asm-arm/cacheflush.h 1.15 vs edited =====

include/asm-arm/cacheflush.h +10 -4
@@ -237,11 +237,17 @@
  * space" model to handle this.
  */
 #define copy_to_user_page(vma, page, vaddr, dst, src, len) \
-do { memcpy(dst, src, len); \
-     flush_icache_user_range(vma, page, vaddr, len); \
-} while (0)
+	do {					\
+		flush_cache_page(vma, vaddr);	\
+		memcpy(dst, src, len);		\
+		flush_dcache_page(page);	\
+	} while (0)
+
 #define copy_from_user_page(vma, page, vaddr, dst, src, len) \
-	memcpy(dst, src, len)
+	do {					\
+		flush_cache_page(vma, vaddr);	\
+		memcpy(dst, src, len);		\
+	} while (0)
 
 /*
  * Convert calls to our calling convention.

===== include/asm-m68k/cacheflush.h 1.9 vs edited =====

include/asm-m68k/cacheflush.h +10 -2
@@ -132,10 +132,18 @@
 #define flush_dcache_mmap_unlock(mapping)	do { } while (0)
 #define flush_icache_page(vma, page)	__flush_page_to_ram(page_address(page))
 #define flush_icache_user_range(vma,pg,adr,len)	do { } while (0)
+
 #define copy_to_user_page(vma, page, vaddr, dst, src, len) \
-	memcpy(dst, src, len)
+	do {					\
+		flush_cache_page(vma, vaddr);	\
+		memcpy(dst, src, len);		\
+	} while (0)
+
 #define copy_from_user_page(vma, page, vaddr, dst, src, len) \
-	memcpy(dst, src, len)
+	do {					\
+		flush_cache_page(vma, vaddr);	\
+		memcpy(dst, src, len);		\
+	} while (0)
 
 extern void flush_icache_range(unsigned long address, unsigned long endaddr);
 

===== include/asm-parisc/cacheflush.h 1.11 vs edited =====

include/asm-parisc/cacheflush.h +9 -3
@@ -77,11 +77,17 @@
 #define flush_icache_range(s,e)		do { flush_kernel_dcache_range_asm(s,e); flush_kernel_icache_range_asm(s,e); } while (0)
 
 #define copy_to_user_page(vma, page, vaddr, dst, src, len) \
-do { memcpy(dst, src, len); \
-     flush_kernel_dcache_range_asm((unsigned long)dst, (unsigned long)dst + len); \
+do { \
+	flush_cache_page(vma, vaddr); \
+	memcpy(dst, src, len); \
+	flush_kernel_dcache_range_asm((unsigned long)dst, (unsigned long)dst + len); \
 } while (0)
+
 #define copy_from_user_page(vma, page, vaddr, dst, src, len) \
-	memcpy(dst, src, len)
+do { \
+	flush_cache_page(vma, vaddr); \
+	memcpy(dst, src, len); \
+} while (0)
 
 static inline void flush_cache_range(struct vm_area_struct *vma,
 		unsigned long start, unsigned long end)

===== include/asm-sh64/cacheflush.h 1.1 vs edited =====

include/asm-sh64/cacheflush.h +9 -5
@@ -30,13 +30,17 @@
 #define flush_icache_page(vma, page)	do { } while (0)
 
 #define copy_to_user_page(vma, page, vaddr, dst, src, len) \
-do { memcpy(dst, src, len); \
-     flush_icache_user_range(vma, page, vaddr, len); \
-} while (0)
+	do {							\
+		flush_cache_page(vma, vaddr);			\
+		memcpy(dst, src, len);				\
+		flush_icache_user_range(vma, page, vaddr, len);	\
+	} while (0)
 
 #define copy_from_user_page(vma, page, vaddr, dst, src, len) \
-	memcpy(dst, src, len)
-
+	do {							\
+		flush_cache_page(vma, vaddr);			\
+		memcpy(dst, src, len);				\
+	} while (0)
 
 #endif /* __ASSEMBLY__ */
 

===== include/asm-sh/cacheflush.h 1.2 vs edited =====

include/asm-sh/cacheflush.h +10 -4
@@ -14,10 +14,16 @@
 #define flush_cache_vunmap(start, end)		flush_cache_all()
 
 #define copy_to_user_page(vma, page, vaddr, dst, src, len) \
-do { memcpy(dst, src, len); \
-     flush_icache_user_range(vma, page, vaddr, len); \
-} while (0)
+	do {							\
+		flush_cache_page(vma, vaddr);			\
+		memcpy(dst, src, len);				\
+		flush_icache_user_range(vma, page, vaddr, len);	\
+	} while (0)
+
 #define copy_from_user_page(vma, page, vaddr, dst, src, len) \
-	memcpy(dst, src, len)
+	do {							\
+		flush_cache_page(vma, vaddr);			\
+		memcpy(dst, src, len);				\
+	} while (0)
 
 #endif /* __ASM_SH_CACHEFLUSH_H */

===== include/asm-sparc64/cacheflush.h 1.6 vs edited =====

include/asm-sparc64/cacheflush.h +9 -2
@@ -38,9 +38,16 @@
 #define flush_icache_user_range(vma,pg,adr,len)	do { } while (0)
 
 #define copy_to_user_page(vma, page, vaddr, dst, src, len) \
-	memcpy(dst, src, len)
+	do {					\
+		flush_cache_page(vma, vaddr);	\
+		memcpy(dst, src, len);		\
+	} while (0)
+
 #define copy_from_user_page(vma, page, vaddr, dst, src, len) \
-	memcpy(dst, src, len)
+	do {					\
+		flush_cache_page(vma, vaddr);	\
+		memcpy(dst, src, len);		\
+	} while (0)
 
 extern void flush_dcache_page(struct page *page);
 #define flush_dcache_mmap_lock(mapping)		do { } while (0)

===== include/asm-sparc/cacheflush.h 1.5 vs edited =====

include/asm-sparc/cacheflush.h +8 -2
@@ -57,9 +57,15 @@
 #define flush_icache_user_range(vma,pg,adr,len)	do { } while (0)
 
 #define copy_to_user_page(vma, page, vaddr, dst, src, len) \
-	memcpy(dst, src, len)
+	do {					\
+		flush_cache_page(vma, vaddr);	\
+		memcpy(dst, src, len);		\
+	} while (0)
 #define copy_from_user_page(vma, page, vaddr, dst, src, len) \
-	memcpy(dst, src, len)
+	do {					\
+		flush_cache_page(vma, vaddr);	\
+		memcpy(dst, src, len);		\
+	} while (0)
 
 BTFIXUPDEF_CALL(void, __flush_page_to_ram, unsigned long)
 BTFIXUPDEF_CALL(void, flush_sig_insns, struct mm_struct *, unsigned long)
Signature · 4 lines
Russell King
 Linux kernel    2.6 ARM Linux   - http://www.arm.linux.org.uk/
 maintainer of:  2.6 PCMCIA      - http://pcmcia.arm.linux.org.uk/
                 2.6 Serial core