Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

sh: Improve performance of SH4 versions of copy/clear_user_highpage

The previous implementation of clear_user_highpage and copy_user_highpage
checked to see if there was a D-cache aliasing issue between the user
and kernel mappings of a page, but if there was they always did a
flush with writeback on the dirtied kernel alias.

However as we now have the ability to map a page into kernel space
with the same cache colour as the user mapping, there is no need to
write back this data.

Currently we also invalidate the kernel alias as a precaution, however
I'm not sure if this is actually required.

Also correct the definition of FIX_CMAP_END so that the mappings created
by kmap_coherent() are actually at the correct colour.

Signed-off-by: Stuart Menefy <stuart.menefy@st.com>
Signed-off-by: Paul Mundt <lethal@linux-sh.org>

authored by

Stuart Menefy and committed by
Paul Mundt
39ac11c1 49fb2cd2

+60 -14
+7 -1
arch/sh/include/asm/fixmap.h
··· 46 46 * fix-mapped? 47 47 */ 48 48 enum fixed_addresses { 49 + /* 50 + * The FIX_CMAP entries are used by kmap_coherent() to get virtual 51 + * addresses which are of a known color, and so their values are 52 + * important. __fix_to_virt(FIX_CMAP_END - n) must give an address 53 + * which is the same color as a page (n<<PAGE_SHIFT). 54 + */ 49 55 #define FIX_N_COLOURS 8 50 56 FIX_CMAP_BEGIN, 51 - FIX_CMAP_END = FIX_CMAP_BEGIN + (FIX_N_COLOURS * NR_CPUS), 57 + FIX_CMAP_END = FIX_CMAP_BEGIN + (FIX_N_COLOURS * NR_CPUS) - 1, 52 58 FIX_UNCACHED, 53 59 #ifdef CONFIG_HIGHMEM 54 60 FIX_KMAP_BEGIN, /* reserved pte's for temporary kernel mappings */
+53 -13
arch/sh/mm/cache.c
··· 46 46 preempt_enable(); 47 47 } 48 48 49 + /* 50 + * copy_to_user_page 51 + * @vma: vm_area_struct holding the pages 52 + * @page: struct page 53 + * @vaddr: user space address 54 + * @dst: address of page in kernel space (possibly from kmap) 55 + * @src: source address in kernel logical memory 56 + * @len: length of data in bytes (may be less than PAGE_SIZE) 57 + * 58 + * Copy data into the address space of a process other than the current 59 + * process (eg for ptrace). 60 + */ 49 61 void copy_to_user_page(struct vm_area_struct *vma, struct page *page, 50 62 unsigned long vaddr, void *dst, const void *src, 51 63 unsigned long len) ··· 93 81 } 94 82 } 95 83 84 + /* 85 + * copy_user_highpage 86 + * @to: destination page 87 + * @from: source page 88 + * @vaddr: address of pages in user address space 89 + * @vma: vm_area_struct holding the pages 90 + * 91 + * This is used in COW implementation to copy data from page @from to 92 + * page @to. @from was previousl mapped at @vaddr, and @to will be. 93 + * As this is used only in the COW implementation, this means that the 94 + * source is unmodified, and so we don't have to worry about cache 95 + * aliasing on that side. 96 + */ 97 + #ifdef CONFIG_HIGHMEM 98 + /* 99 + * If we ever have a real highmem system, this code will need fixing 100 + * (as will clear_user/clear_user_highmem), because the kmap potentitally 101 + * creates another alias risk. 102 + */ 103 + #error This code is broken with real HIGHMEM 104 + #endif 96 105 void copy_user_highpage(struct page *to, struct page *from, 97 106 unsigned long vaddr, struct vm_area_struct *vma) 98 107 { 99 108 void *vfrom, *vto; 100 109 101 110 vto = kmap_atomic(to, KM_USER1); 111 + vfrom = kmap_atomic(from, KM_USER0); 112 + 113 + if (pages_do_alias((unsigned long)vto, vaddr & PAGE_MASK)) 114 + __flush_invalidate_region(vto, PAGE_SIZE); 102 115 103 116 if (boot_cpu_data.dcache.n_aliases && page_mapped(from) && 104 117 !test_bit(PG_dcache_dirty, &from->flags)) { 105 - vfrom = kmap_coherent(from, vaddr); 118 + void *vto_coloured = kmap_coherent(to, vaddr); 119 + copy_page(vto_coloured, vfrom); 120 + kunmap_coherent(vto_coloured); 121 + } else 106 122 copy_page(vto, vfrom); 107 - kunmap_coherent(vfrom); 108 - } else { 109 - vfrom = kmap_atomic(from, KM_USER0); 110 - copy_page(vto, vfrom); 111 - kunmap_atomic(vfrom, KM_USER0); 112 - } 113 123 114 - if (pages_do_alias((unsigned long)vto, vaddr & PAGE_MASK)) 115 - __flush_purge_region(vto, PAGE_SIZE); 116 - 124 + kunmap_atomic(vfrom, KM_USER0); 117 125 kunmap_atomic(vto, KM_USER1); 126 + 118 127 /* Make sure this page is cleared on other CPU's too before using it */ 119 128 smp_wmb(); 120 129 } ··· 145 112 { 146 113 void *kaddr = kmap_atomic(page, KM_USER0); 147 114 148 - clear_page(kaddr); 115 + if (pages_do_alias((unsigned long)kaddr, vaddr & PAGE_MASK)) { 116 + void *vto; 149 117 150 - if (pages_do_alias((unsigned long)kaddr, vaddr & PAGE_MASK)) 151 - __flush_purge_region(kaddr, PAGE_SIZE); 118 + /* Kernel alias may have modified data in the cache. */ 119 + __flush_invalidate_region(kaddr, PAGE_SIZE); 120 + 121 + vto = kmap_coherent(page, vaddr); 122 + clear_page(vto); 123 + kunmap_coherent(vto); 124 + } else 125 + clear_page(kaddr); 152 126 153 127 kunmap_atomic(kaddr, KM_USER0); 154 128 }