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

powerpc, mm: Fix mprotect on book3s 32-bit

On 32-bit book3s with hash-MMUs, tlb_flush() was a no-op. This was
unnoticed because all uses until recently were for unmaps, and thus
handled by __tlb_remove_tlb_entry().

After commit 4a18419f71cd ("mm/mprotect: use mmu_gather") in kernel 5.19,
tlb_gather_mmu() started being used for mprotect as well. This caused
mprotect to simply not work on these machines:

int *ptr = mmap(NULL, 4096, PROT_READ|PROT_WRITE,
MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
*ptr = 1; // force HPTE to be created
mprotect(ptr, 4096, PROT_READ);
*ptr = 2; // should segfault, but succeeds

Fixed by making tlb_flush() actually flush TLB pages. This finally
agrees with the behaviour of boot3s64's tlb_flush().

Fixes: 4a18419f71cd ("mm/mprotect: use mmu_gather")
Cc: stable@vger.kernel.org
Reviewed-by: Christophe Leroy <christophe.leroy@csgroup.eu>
Reviewed-by: Ritesh Harjani (IBM) <ritesh.list@gmail.com>
Signed-off-by: Dave Vasilevsky <dave@vasilevsky.ca>
Signed-off-by: Madhavan Srinivasan <maddy@linux.ibm.com>
Link: https://patch.msgid.link/20251116-vasi-mprotect-g3-v3-1-59a9bd33ba00@vasilevsky.ca

authored by

Dave Vasilevsky and committed by
Madhavan Srinivasan
78fc63ff fb2ff9fa

+13 -1
+4 -1
arch/powerpc/include/asm/book3s/32/tlbflush.h
··· 11 11 void hash__flush_tlb_mm(struct mm_struct *mm); 12 12 void hash__flush_tlb_page(struct vm_area_struct *vma, unsigned long vmaddr); 13 13 void hash__flush_range(struct mm_struct *mm, unsigned long start, unsigned long end); 14 + void hash__flush_gather(struct mmu_gather *tlb); 14 15 15 16 #ifdef CONFIG_SMP 16 17 void _tlbie(unsigned long address); ··· 30 29 static inline void tlb_flush(struct mmu_gather *tlb) 31 30 { 32 31 /* 603 needs to flush the whole TLB here since it doesn't use a hash table. */ 33 - if (!mmu_has_feature(MMU_FTR_HPTE_TABLE)) 32 + if (mmu_has_feature(MMU_FTR_HPTE_TABLE)) 33 + hash__flush_gather(tlb); 34 + else 34 35 _tlbia(); 35 36 } 36 37
+9
arch/powerpc/mm/book3s32/tlb.c
··· 105 105 flush_hash_pages(mm->context.id, vmaddr, pmd_val(*pmd), 1); 106 106 } 107 107 EXPORT_SYMBOL(hash__flush_tlb_page); 108 + 109 + void hash__flush_gather(struct mmu_gather *tlb) 110 + { 111 + if (tlb->fullmm || tlb->need_flush_all) 112 + hash__flush_tlb_mm(tlb->mm); 113 + else 114 + hash__flush_range(tlb->mm, tlb->start, tlb->end); 115 + } 116 + EXPORT_SYMBOL(hash__flush_gather);