Skip to content

Commit 4d46a67

Browse files
larperaxisralfbaechle
authored andcommitted
MIPS: Fix race condition in lazy cache flushing.
The lazy cache flushing implemented in the MIPS kernel suffers from a race condition that is exposed by do_set_pte() in mm/memory.c. A pre-condition is a file-system that writes to the page from the CPU in its readpage method and then calls flush_dcache_page(). One example is ubifs. Another pre-condition is that the dcache flush is postponed in __flush_dcache_page(). Upon a page fault for an executable mapping not existing in the page-cache, the following will happen: 1. Write to the page 2. flush_dcache_page 3. flush_icache_page 4. set_pte_at 5. update_mmu_cache (commits the flush of a dcache-dirty page) Between steps 4 and 5 another thread can hit the same page and it will encounter a valid pte. Because the data still is in the L1 dcache the CPU will fetch stale data from L2 into the icache and execute garbage. This fix moves the commit of the cache flush to step 3 to close the race window. It also reduces the amount of flushes on non-executable mappings because we never enter __flush_dcache_page() for non-aliasing CPUs. Regressions can occur in drivers that mistakenly relies on the flush_dcache_page() in get_user_pages() for DMA operations. [ralf@linux-mips.org: Folded in patch 9346 to fix highmem issue.] Signed-off-by: Lars Persson <larper@axis.com> Cc: linux-mips@linux-mips.org Cc: paul.burton@imgtec.com Cc: linux-kernel@vger.kernel.org Patchwork: https://patchwork.linux-mips.org/patch/9346/ Patchwork: https://patchwork.linux-mips.org/patch/9738/ Signed-off-by: Ralf Baechle <ralf@linux-mips.org>
1 parent 5b9593f commit 4d46a67

2 files changed

Lines changed: 35 additions & 15 deletions

File tree

‎arch/mips/include/asm/cacheflush.h‎

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,20 @@
2929
* - flush_icache_all() flush the entire instruction cache
3030
* - flush_data_cache_page() flushes a page from the data cache
3131
*/
32+
33+
/*
34+
* This flag is used to indicate that the page pointed to by a pte
35+
* is dirty and requires cleaning before returning it to the user.
36+
*/
37+
#define PG_dcache_dirty PG_arch_1
38+
39+
#define Page_dcache_dirty(page) \
40+
test_bit(PG_dcache_dirty, &(page)->flags)
41+
#define SetPageDcacheDirty(page) \
42+
set_bit(PG_dcache_dirty, &(page)->flags)
43+
#define ClearPageDcacheDirty(page) \
44+
clear_bit(PG_dcache_dirty, &(page)->flags)
45+
3246
extern void (*flush_cache_all)(void);
3347
extern void (*__flush_cache_all)(void);
3448
extern void (*flush_cache_mm)(struct mm_struct *mm);
@@ -37,13 +51,15 @@ extern void (*flush_cache_range)(struct vm_area_struct *vma,
3751
unsigned long start, unsigned long end);
3852
extern void (*flush_cache_page)(struct vm_area_struct *vma, unsigned long page, unsigned long pfn);
3953
extern void __flush_dcache_page(struct page *page);
54+
extern void __flush_icache_page(struct vm_area_struct *vma, struct page *page);
4055

4156
#define ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE 1
4257
static inline void flush_dcache_page(struct page *page)
4358
{
44-
if (cpu_has_dc_aliases || !cpu_has_ic_fills_f_dc)
59+
if (cpu_has_dc_aliases)
4560
__flush_dcache_page(page);
46-
61+
else if (!cpu_has_ic_fills_f_dc)
62+
SetPageDcacheDirty(page);
4763
}
4864

4965
#define flush_dcache_mmap_lock(mapping) do { } while (0)
@@ -61,6 +77,11 @@ static inline void flush_anon_page(struct vm_area_struct *vma,
6177
static inline void flush_icache_page(struct vm_area_struct *vma,
6278
struct page *page)
6379
{
80+
if (!cpu_has_ic_fills_f_dc && (vma->vm_flags & VM_EXEC) &&
81+
Page_dcache_dirty(page)) {
82+
__flush_icache_page(vma, page);
83+
ClearPageDcacheDirty(page);
84+
}
6485
}
6586

6687
extern void (*flush_icache_range)(unsigned long start, unsigned long end);
@@ -95,19 +116,6 @@ extern void (*flush_icache_all)(void);
95116
extern void (*local_flush_data_cache_page)(void * addr);
96117
extern void (*flush_data_cache_page)(unsigned long addr);
97118

98-
/*
99-
* This flag is used to indicate that the page pointed to by a pte
100-
* is dirty and requires cleaning before returning it to the user.
101-
*/
102-
#define PG_dcache_dirty PG_arch_1
103-
104-
#define Page_dcache_dirty(page) \
105-
test_bit(PG_dcache_dirty, &(page)->flags)
106-
#define SetPageDcacheDirty(page) \
107-
set_bit(PG_dcache_dirty, &(page)->flags)
108-
#define ClearPageDcacheDirty(page) \
109-
clear_bit(PG_dcache_dirty, &(page)->flags)
110-
111119
/* Run kernel code uncached, useful for cache probing functions. */
112120
unsigned long run_uncached(void *func);
113121

‎arch/mips/mm/cache.c‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,18 @@ void __flush_anon_page(struct page *page, unsigned long vmaddr)
119119

120120
EXPORT_SYMBOL(__flush_anon_page);
121121

122+
void __flush_icache_page(struct vm_area_struct *vma, struct page *page)
123+
{
124+
unsigned long addr;
125+
126+
if (PageHighMem(page))
127+
return;
128+
129+
addr = (unsigned long) page_address(page);
130+
flush_data_cache_page(addr);
131+
}
132+
EXPORT_SYMBOL_GPL(__flush_icache_page);
133+
122134
void __update_cache(struct vm_area_struct *vma, unsigned long address,
123135
pte_t pte)
124136
{

0 commit comments

Comments
 (0)