Skip to content

Commit ac53c4f

Browse files
daviddaneyralfbaechle
authored andcommitted
MIPS: Avoid mcheck by flushing page range in huge_ptep_set_access_flags()
Problem: 1) Huge page mapping of anonymous memory is initially invalid. Will be faulted in by copy-on-write mechanism. 2) Userspace attempts store at the end of the huge mapping. 3) TLB Refill exception handler fill TLB with a normal (4K sized) invalid page at the end of the huge mapping virtual address range. 4) Userspace restarted, and re-attempts the store at the end of the huge mapping. 5) Page from #3 is invalid, we get a fault and go to the hugepage fault handler. This tries to map a huge page and calls huge_ptep_set_access_flags() to install the mapping. 6) We just call the generic ptep_set_access_flags() to set up the page tables, but the flush there assumes a normal (4K sized) page and only tries to flush the first part of the huge page virtual address out of the TLB, since the existing entry from step #3 doesn't conflict, nothing is flushed. 7) We attempt to load the mapping into the TLB, but because it conflicts with the entry from step #3, we get a Machine Check exception. The fix: Flush the entire rage covered by the huge page in huge_ptep_set_access_flags(), and remove the optimization in local_flush_tlb_range() so that the flush actually does the correct thing. Signed-off-by: David Daney <[email protected]> Cc: [email protected] Cc: [email protected] Cc: Hillf Danton <[email protected]> Patchwork: https://patchwork.linux-mips.org/patch/4661/ Signed-off-by: Ralf Baechle <[email protected]> (cherry picked from commit dd617f258cc39d36be26afee9912624a2d23112c)
1 parent 9489e9d commit ac53c4f

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

arch/mips/include/asm/hugetlb.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,17 @@ static inline int huge_ptep_set_access_flags(struct vm_area_struct *vma,
9595
pte_t *ptep, pte_t pte,
9696
int dirty)
9797
{
98-
return ptep_set_access_flags(vma, addr, ptep, pte, dirty);
98+
int changed = !pte_same(*ptep, pte);
99+
100+
if (changed) {
101+
set_pte_at(vma->vm_mm, addr, ptep, pte);
102+
/*
103+
* There could be some standard sized pages in there,
104+
* get them all.
105+
*/
106+
flush_tlb_range(vma, addr, addr + HPAGE_SIZE);
107+
}
108+
return changed;
99109
}
100110

101111
static inline pte_t huge_ptep_get(pte_t *ptep)

arch/mips/mm/tlb-r4k.c

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -120,18 +120,11 @@ void local_flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
120120

121121
if (cpu_context(cpu, mm) != 0) {
122122
unsigned long size, flags;
123-
int huge = is_vm_hugetlb_page(vma);
124123

125124
ENTER_CRITICAL(flags);
126-
if (huge) {
127-
start = round_down(start, HPAGE_SIZE);
128-
end = round_up(end, HPAGE_SIZE);
129-
size = (end - start) >> HPAGE_SHIFT;
130-
} else {
131-
start = round_down(start, PAGE_SIZE << 1);
132-
end = round_up(end, PAGE_SIZE << 1);
133-
size = (end - start) >> (PAGE_SHIFT + 1);
134-
}
125+
start = round_down(start, PAGE_SIZE << 1);
126+
end = round_up(end, PAGE_SIZE << 1);
127+
size = (end - start) >> (PAGE_SHIFT + 1);
135128
if (size <= current_cpu_data.tlbsize/2) {
136129
int oldpid = read_c0_entryhi();
137130
int newpid = cpu_asid(cpu, mm);
@@ -140,10 +133,7 @@ void local_flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
140133
int idx;
141134

142135
write_c0_entryhi(start | newpid);
143-
if (huge)
144-
start += HPAGE_SIZE;
145-
else
146-
start += (PAGE_SIZE << 1);
136+
start += (PAGE_SIZE << 1);
147137
mtc0_tlbw_hazard();
148138
tlb_probe();
149139
tlb_probe_hazard();

0 commit comments

Comments
 (0)