Skip to content

Commit b11931e

Browse files
npigginmpe
authored andcommitted
powerpc/64s: add pte_needs_flush and huge_pmd_needs_flush
Allow PTE changes to avoid flushing the TLB when access permissions are being relaxed, the dirty bit is being set, and the accessed bit is being changed. Relaxing access permissions and setting dirty and accessed bits do not require a flush because the MMU will re-load the PTE and notice the updates (it may also cause a spurious fault). Clearing the accessed bit does not require a flush because of the imprecise PTE accessed bit accounting that is already performed, as documented in ptep_clear_flush_young(). This reduces TLB flushing for some mprotect(2) calls. Signed-off-by: Nicholas Piggin <[email protected]> Signed-off-by: Michael Ellerman <[email protected]> Reviewed-by: Christophe Leroy <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 78c73c8 commit b11931e

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

arch/powerpc/include/asm/book3s/64/pgtable.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,9 @@ static inline int __ptep_test_and_clear_young(struct mm_struct *mm,
413413
* event of it not getting flushed for a long time the delay
414414
* shouldn't really matter because there's no real memory
415415
* pressure for swapout to react to. ]
416+
*
417+
* Note: this optimisation also exists in pte_needs_flush() and
418+
* huge_pmd_needs_flush().
416419
*/
417420
#define __HAVE_ARCH_PTEP_CLEAR_YOUNG_FLUSH
418421
#define ptep_clear_flush_young ptep_test_and_clear_young

arch/powerpc/include/asm/book3s/64/tlbflush.h

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,62 @@ static inline void flush_tlb_fix_spurious_fault(struct vm_area_struct *vma,
163163
*/
164164
}
165165

166+
static inline bool __pte_flags_need_flush(unsigned long oldval,
167+
unsigned long newval)
168+
{
169+
unsigned long delta = oldval ^ newval;
170+
171+
/*
172+
* The return value of this function doesn't matter for hash,
173+
* ptep_modify_prot_start() does a pte_update() which does or schedules
174+
* any necessary hash table update and flush.
175+
*/
176+
if (!radix_enabled())
177+
return true;
178+
179+
/*
180+
* We do not expect kernel mappings or non-PTEs or not-present PTEs.
181+
*/
182+
VM_WARN_ON_ONCE(oldval & _PAGE_PRIVILEGED);
183+
VM_WARN_ON_ONCE(newval & _PAGE_PRIVILEGED);
184+
VM_WARN_ON_ONCE(!(oldval & _PAGE_PTE));
185+
VM_WARN_ON_ONCE(!(newval & _PAGE_PTE));
186+
VM_WARN_ON_ONCE(!(oldval & _PAGE_PRESENT));
187+
VM_WARN_ON_ONCE(!(newval & _PAGE_PRESENT));
188+
189+
/*
190+
* Must flush on any change except READ, WRITE, EXEC, DIRTY, ACCESSED.
191+
*
192+
* In theory, some changed software bits could be tolerated, in
193+
* practice those should rarely if ever matter.
194+
*/
195+
196+
if (delta & ~(_PAGE_RWX | _PAGE_DIRTY | _PAGE_ACCESSED))
197+
return true;
198+
199+
/*
200+
* If any of the above was present in old but cleared in new, flush.
201+
* With the exception of _PAGE_ACCESSED, don't worry about flushing
202+
* if that was cleared (see the comment in ptep_clear_flush_young()).
203+
*/
204+
if ((delta & ~_PAGE_ACCESSED) & oldval)
205+
return true;
206+
207+
return false;
208+
}
209+
210+
static inline bool pte_needs_flush(pte_t oldpte, pte_t newpte)
211+
{
212+
return __pte_flags_need_flush(pte_val(oldpte), pte_val(newpte));
213+
}
214+
#define pte_needs_flush pte_needs_flush
215+
216+
static inline bool huge_pmd_needs_flush(pmd_t oldpmd, pmd_t newpmd)
217+
{
218+
return __pte_flags_need_flush(pmd_val(oldpmd), pmd_val(newpmd));
219+
}
220+
#define huge_pmd_needs_flush huge_pmd_needs_flush
221+
166222
extern bool tlbie_capable;
167223
extern bool tlbie_enabled;
168224

0 commit comments

Comments
 (0)