MIPS: Remove race window in page fault handling
PATCH v2 1/1 MIPS: Remove race window in page fault handling
Multicore MIPSes without I/D hardware coherency suffered from a race condition in the page fault handler. The page table entry was published before any pending lazy D-cache flush was committed, hence it allowed execution of stale page cache data by other VPEs in the system.
To make the cache handling safe we need to perform flushing already in the set_pte_at function. MIPSes without coherent I-caches can get a small increase in flushes due to the unavailability of the execute flag in set_pte_at.
--- arch/mips/include/asm/pgtable.h | 22 +++++++++++++++++----- arch/mips/mm/cache.c | 16 ++++++++-------- 2 files changed, 25 insertions(+), 13 deletions(-)
arch/mips/include/asm/pgtable.h +17 -5
@@ -122,6 +122,9 @@ do { \ } \ }while (0 ) +static inline void set_pte_at(struct mm_struct *mm, unsigned long addr, +pte_t *ptep,pte_t pteval); +#if defined(CONFIG_64BIT_PHYS_ADDR ) && defined(CONFIG_CPU_MIPS32 )#define pte_none (pte) (!(((pte).pte_low | (pte).pte_high) & ~_PAGE_GLOBAL))
@@ -145,7 +148,6 @@ static inline void set_pte(pte_t *ptep, pte_t pte) } } } -#define set_pte_at (mm, addr, ptep, pteval) set_pte(ptep, pteval)static inline void pte_clear (struct mm_struct *mm,unsigned long addr,pte_t *ptep) {
@@ -183,7 +185,6 @@ static inline void set_pte(pte_t *ptep, pte_t pteval) }#endif } -#define set_pte_at (mm, addr, ptep, pteval) set_pte(ptep, pteval)static inline void pte_clear (struct mm_struct *mm,unsigned long addr,pte_t *ptep) {
@@ -198,6 +199,20 @@ static inline void pte_clear(struct mm_struct *mm, unsigned long addr, pte_t *pt }#endif +extern void mips_flush_dcache_from_pte (pte_t pteval,unsigned long address); + +static inline void set_pte_at (struct mm_struct *mm,unsigned long addr, +pte_t *ptep,pte_t pteval) +{ +if (cpu_has_dc_aliases || !cpu_has_ic_fills_f_dc) { +if (pte_present (pteval)) +mips_flush_dcache_from_pte (pteval, addr); + } + +set_pte (ptep, pteval); +} + +/* * (pmds are folded into puds so this doesn't get actually called, * but the define is needed for a generic inline function.)
@@ -390,15 +405,12 @@ static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)extern void __update_tlb (struct vm_area_struct *vma,unsigned long address,pte_t pte); -extern void __update_cache (struct vm_area_struct *vma,unsigned long address, -pte_t pte);static inline void update_mmu_cache (struct vm_area_struct *vma,unsigned long address,pte_t *ptep) {pte_t pte = *ptep;__update_tlb (vma, address, pte); -__update_cache (vma, address, pte); } static inline void update_mmu_cache_pmd(struct vm_area_struct *vma,
arch/mips/mm/cache.c +8 -8
@@ -119,21 +119,21 @@ void __flush_anon_page(struct page *page, unsigned long vmaddr)EXPORT_SYMBOL (__flush_anon_page); -void __update_cache (struct vm_area_struct *vma,unsigned long address, -pte_t pte) +void mips_flush_dcache_from_pte (pte_t pteval,unsigned long address) {struct page *page; -unsigned long pfn, addr; -int exec = (vma->vm_flags &VM_EXEC ) && !cpu_has_ic_fills_f_dc; +unsigned long pfn =pte_pfn (pteval); - pfn =pte_pfn (pte);if (unlikely (!pfn_valid (pfn)))return ; + page =pfn_to_page (pfn);if (page_mapping (page) &&Page_dcache_dirty (page)) { - addr = (unsigned long )page_address (page); -if (exec ||pages_do_alias (addr, address &PAGE_MASK )) -flush_data_cache_page (addr); +unsigned long page_addr = (unsigned long )page_address (page); + +if (!cpu_has_ic_fills_f_dc || +pages_do_alias (page_addr, address &PAGE_MASK )) +flush_data_cache_page (page_addr);ClearPageDcacheDirty (page); } }
Signature · 1 line
1.7.10.4
Quoted 22 lines · Lars Persson
Multicore MIPSes without I/D hardware coherency suffered from a race condition in the page fault handler. The page table entry was published before any pending lazy D-cache flush was committed, hence it allowed execution of stale page cache data by other VPEs in the system.
To make the cache handling safe we need to perform flushing already in the
set_pte_atfunction. MIPSes without coherent I-caches can get a small increase in flushes due to the unavailability of the execute flag inset_pte_at.Signed-off-by Lars Persson <[email protected]>--- arch/mips/include/asm/pgtable.h | 22 +++++++++++++++++----- arch/mips/mm/cache.c | 16 ++++++++-------- 2 files changed, 25 insertions(+), 13 deletions(-)arch/mips/include/asm/pgtable.h +0
-0
arch/mips/include/asm/pgtable.h do { \ · line 127
Quoted 6 lines
arch/mips/include/asm/pgtable.h +3
-0@@ -122,6 +122,9 @@ do { \ } \ }while (0 ) +static inline void set_pte_at(struct mm_struct *mm, unsigned long addr, +pte_t *ptep,pte_t pteval); +
Is it possible to reorder the code such that this declaration is not necessary?
Quoted 38 lines
arch/mips/include/asm/pgtable.h +14
-2#if defined(CONFIG_64BIT_PHYS_ADDR ) && defined(CONFIG_CPU_MIPS32 )#define pte_none (pte) (!(((pte).pte_low | (pte).pte_high) & ~_PAGE_GLOBAL))@@ -145,7 +148,6 @@ static inline void set_pte(pte_t *ptep, pte_t pte) } } } -#define set_pte_at (mm, addr, ptep, pteval) set_pte(ptep, pteval)static inline void pte_clear (struct mm_struct *mm,unsigned long addr,pte_t *ptep) {@@ -183,7 +185,6 @@ static inline void set_pte(pte_t *ptep, pte_t pteval) }#endif } -#define set_pte_at (mm, addr, ptep, pteval) set_pte(ptep, pteval)static inline void pte_clear (struct mm_struct *mm,unsigned long addr,pte_t *ptep) {@@ -198,6 +199,20 @@ static inline void pte_clear(struct mm_struct *mm, unsigned long addr, pte_t *pt }#endif +extern void mips_flush_dcache_from_pte (pte_t pteval,unsigned long address); + +static inline void set_pte_at (struct mm_struct *mm,unsigned long addr, +pte_t *ptep,pte_t pteval) +{ +if (cpu_has_dc_aliases || !cpu_has_ic_fills_f_dc) { +if (pte_present (pteval)) +mips_flush_dcache_from_pte (pteval, addr); + } + +set_pte (ptep, pteval); +} + +
[...]
Quoted 6 lines · David Daney
arch/mips/include/asm/pgtable.h do { \ · line 127
Quoted 3 lines
arch/mips/include/asm/pgtable.h +3
-0+static inline void set_pte_at (struct mm_struct *mm,unsigned long addr, +pte_t *ptep,pte_t pteval); +Is it possible to reorder the code such that this declaration is not necessary?
That's not as obvious as one might think initially. set_pte_at needs to be defined after set_pte but before clear_pte which is calling set_pte_at.
Of both set_pte and clear_pte there are two #ifdefd variants.
set_pte_at is a fairly small function only but it's invoked quite a few times so I was a little concerned about the effect on I'm experimenting with outlining set_pte_at entirely. ip22_defconfig with the patch applied as posted; this is the effect on code size.
text data bss dec hex filename
3790118 175304 84544 4049966 3dcc2e vmlinux as posted
3789062 175304 84544 4048910 3dc80e vmlinux set_pte_at outlined
Ralf
Quoted 19 lines · Ralf Baechle
Quoted 6 lines · David Daney
arch/mips/include/asm/pgtable.h do { \ · line 127
Quoted 3 lines
arch/mips/include/asm/pgtable.h +3
-0+static inline void set_pte_at (struct mm_struct *mm,unsigned long addr, +pte_t *ptep,pte_t pteval); +Is it possible to reorder the code such that this declaration is not necessary?
That's not as obvious as one might think initially.
set_pte_atneeds to be defined afterset_ptebut beforeclear_ptewhich is callingset_pte_at.Of both
set_pteandclear_ptethere are two #ifdefd variants.
set_pte_atis a fairly small function only but it's invoked quite a few times so I was a little concerned about the effect on I'm experimenting with outliningset_pte_atentirely.ip22_defconfigwith the patch applied as posted; this is the effect on code size.
Quoted 5 lines
text data bss dec hex filename
3790118 175304 84544 4049966 3dcc2e vmlinux as posted
3789062 175304 84544 4048910 3dc80e vmlinuxset_pte_atoutlinedRalf
Hi Ralf
Should I update the patch with outlined set_pte_at ?
Best Regards,
Lars
Quoted 25 lines · Lars Persson
Quoted 23 lines · Ralf Baechle
Quoted 6 lines · David Daney
arch/mips/include/asm/pgtable.h do { \ · line 127
Quoted 3 lines
arch/mips/include/asm/pgtable.h +3
-0+static inline void set_pte_at (struct mm_struct *mm,unsigned long addr, +pte_t *ptep,pte_t pteval); +Is it possible to reorder the code such that this declaration is not necessary?
That's not as obvious as one might think initially.
set_pte_atneeds to be defined afterset_ptebut beforeclear_ptewhich is callingset_pte_at.Of both
set_pteandclear_ptethere are two #ifdefd variants.
set_pte_atis a fairly small function only but it's invoked quite a few times so I was a little concerned about the effect on I'm experimenting with outliningset_pte_atentirely.ip22_defconfigwith the patch applied as posted; this is the effect on code size.text data bss dec hex filename
3790118 175304 84544 4049966 3dcc2e vmlinux as posted
3789062 175304 84544 4048910 3dc80e vmlinuxset_pte_atoutlined
Quoted 5 lines
Quoted 1 line
Ralf
Hi Ralf
Should I update the patch with outlined
set_pte_at?
Not necessary; I've already done that myself. I was just waiting for comments.
Thanks for your work on tracking this down. I wonder, how did you discover this issue?
Ralf
Quoted 31 lines · Ralf Baechle
Quoted 29 lines · Lars Persson
Quoted 19 lines · Ralf Baechle
Quoted 6 lines · David Daney
arch/mips/include/asm/pgtable.h do { \ · line 127
Quoted 3 lines
arch/mips/include/asm/pgtable.h +3
-0+static inline void set_pte_at (struct mm_struct *mm,unsigned long addr, +pte_t *ptep,pte_t pteval); +Is it possible to reorder the code such that this declaration is not necessary?
That's not as obvious as one might think initially.
set_pte_atneeds to be defined afterset_ptebut beforeclear_ptewhich is callingset_pte_at.Of both
set_pteandclear_ptethere are two #ifdefd variants.
set_pte_atis a fairly small function only but it's invoked quite a few times so I was a little concerned about the effect on I'm experimenting with outliningset_pte_atentirely.ip22_defconfigwith the patch applied as posted; this is the effect on code size.Quoted 5 lines
text data bss dec hex filename
3790118 175304 84544 4049966 3dcc2e vmlinux as posted
3789062 175304 84544 4048910 3dc80e vmlinuxset_pte_atoutlinedRalf
Hi Ralf
Quoted 7 lines
Quoted 1 line
Should I update the patch with outlined
set_pte_at?Not necessary; I've already done that myself. I was just waiting for comments.
Thanks for your work on tracking this down. I wonder, how did you discover this issue?
This one was tricky to track down. We had sporadic SIGILLs in multi-threaded apps for a long time. Eventually we got a test case that triggered more page cache evictions and the frequency of SIGILLs increased enough to catch it with a JTAG debugger.
Kernel call stacks showed one thread handling an illegal instruction exception while another thread was somewhere around the set_pte_at/update_mmu_cache calls for the same page.
BR,
Lars
Quoted 8 lines · Lars Persson
This one was tricky to track down. We had sporadic SIGILLs in multi-threaded apps for a long time. Eventually we got a test case that triggered more page cache evictions and the frequency of SIGILLs increased enough to catch it with a JTAG debugger.
Kernel call stacks showed one thread handling an illegal instruction exception while another thread was somewhere around the set_pte_at/update_mmu_cache calls for the same page.
Some of those coherency bugs are almost impossibly hard to track down and fix properly!
Anyway, I'm going to send the outlined version of your fix to Linus.
Ralf
On Fri, Aug 15, 2014 at 02:08:56PM +0200, Lars Persson wrote:
Btw, this patch applies with minor variations all the way back to at least 2.6.12 and in even older kernel set_pte_at() doesn't exist yet ...
Ralf