mm: add a ptep_modify_prot transaction abstraction
This patch adds an API for doing read-modify-write updates to a pte's protection bits which may race against hardware updates to the pte. After reading the pte, the hardware may asynchonously set the accessed or dirty bits on a pte, which would be lost when writing back the modified pte value. The existing technique to handle this race is to use ptep_get_and_clear() atomically fetch the old pte value and clear it in memory. This has the effect of marking the pte as non-present, which will prevent the hardware from updating its state. When the new value is written back, the pte will be present again, and the hardware can resume updating the access/dirty flags. When running in a virtualized environment, pagetable updates are relatively expensive, since they generally involve some trap into the hypervisor. To mitigate the cost of these updates, we tend to batch them. However, because of the atomic nature of ptep_get_and_clear(), it is inherently non-batchable. This new interface allows batching by giving the underlying implementation enough information to open a transaction between the read and write phases: ptep_modify_prot_start() returns the current pte value, and puts the pte entry into a state where either the hardware will not update the pte, or if it does, the updates will be preserved on commit. ptep_modify_prot_commit() writes back the updated pte, makes sure that any hardware updates made since ptep_modify_prot_start() are preserved. ptep_modify_prot_start() and _commit() must be exactly paired, and used while holding the appropriate pte lock. They do not protect against other software updates of the pte in any way. The current implementations of ptep_modify_prot_start and _commit are functionally unchanged from before: _start() uses ptep_get_and_clear() fetch the pte and zero the entry, preventing any hardware updates. _commit() simply writes the new pte value back knowing that the hardware has not updated the pte in the meantime. The only current user of this interface is mprotect Signed-off-by: Jeremy Fitzhardinge <jeremy.fitzhardinge@citrix.com> Acked-by: Linus Torvalds <torvalds@linux-foundation.org> Acked-by: Hugh Dickins <hugh@veritas.com> Signed-off-by: Ingo Molnar <mingo@elte.hu>
This commit is contained in:
parent
d02859ecb3
commit
1ea0704e0d
@ -197,6 +197,63 @@ static inline int pmd_none_or_clear_bad(pmd_t *pmd)
|
||||
}
|
||||
#endif /* CONFIG_MMU */
|
||||
|
||||
static inline pte_t __ptep_modify_prot_start(struct mm_struct *mm,
|
||||
unsigned long addr,
|
||||
pte_t *ptep)
|
||||
{
|
||||
/*
|
||||
* Get the current pte state, but zero it out to make it
|
||||
* non-present, preventing the hardware from asynchronously
|
||||
* updating it.
|
||||
*/
|
||||
return ptep_get_and_clear(mm, addr, ptep);
|
||||
}
|
||||
|
||||
static inline void __ptep_modify_prot_commit(struct mm_struct *mm,
|
||||
unsigned long addr,
|
||||
pte_t *ptep, pte_t pte)
|
||||
{
|
||||
/*
|
||||
* The pte is non-present, so there's no hardware state to
|
||||
* preserve.
|
||||
*/
|
||||
set_pte_at(mm, addr, ptep, pte);
|
||||
}
|
||||
|
||||
#ifndef __HAVE_ARCH_PTEP_MODIFY_PROT_TRANSACTION
|
||||
/*
|
||||
* Start a pte protection read-modify-write transaction, which
|
||||
* protects against asynchronous hardware modifications to the pte.
|
||||
* The intention is not to prevent the hardware from making pte
|
||||
* updates, but to prevent any updates it may make from being lost.
|
||||
*
|
||||
* This does not protect against other software modifications of the
|
||||
* pte; the appropriate pte lock must be held over the transation.
|
||||
*
|
||||
* Note that this interface is intended to be batchable, meaning that
|
||||
* ptep_modify_prot_commit may not actually update the pte, but merely
|
||||
* queue the update to be done at some later time. The update must be
|
||||
* actually committed before the pte lock is released, however.
|
||||
*/
|
||||
static inline pte_t ptep_modify_prot_start(struct mm_struct *mm,
|
||||
unsigned long addr,
|
||||
pte_t *ptep)
|
||||
{
|
||||
return __ptep_modify_prot_start(mm, addr, ptep);
|
||||
}
|
||||
|
||||
/*
|
||||
* Commit an update to a pte, leaving any hardware-controlled bits in
|
||||
* the PTE unmodified.
|
||||
*/
|
||||
static inline void ptep_modify_prot_commit(struct mm_struct *mm,
|
||||
unsigned long addr,
|
||||
pte_t *ptep, pte_t pte)
|
||||
{
|
||||
__ptep_modify_prot_commit(mm, addr, ptep, pte);
|
||||
}
|
||||
#endif /* __HAVE_ARCH_PTEP_MODIFY_PROT_TRANSACTION */
|
||||
|
||||
/*
|
||||
* A facility to provide lazy MMU batching. This allows PTE updates and
|
||||
* page invalidations to be delayed until a call to leave lazy MMU mode
|
||||
|
@ -47,19 +47,17 @@ static void change_pte_range(struct mm_struct *mm, pmd_t *pmd,
|
||||
if (pte_present(oldpte)) {
|
||||
pte_t ptent;
|
||||
|
||||
/* Avoid an SMP race with hardware updated dirty/clean
|
||||
* bits by wiping the pte and then setting the new pte
|
||||
* into place.
|
||||
*/
|
||||
ptent = ptep_get_and_clear(mm, addr, pte);
|
||||
ptent = ptep_modify_prot_start(mm, addr, pte);
|
||||
ptent = pte_modify(ptent, newprot);
|
||||
|
||||
/*
|
||||
* Avoid taking write faults for pages we know to be
|
||||
* dirty.
|
||||
*/
|
||||
if (dirty_accountable && pte_dirty(ptent))
|
||||
ptent = pte_mkwrite(ptent);
|
||||
set_pte_at(mm, addr, pte, ptent);
|
||||
|
||||
ptep_modify_prot_commit(mm, addr, pte, ptent);
|
||||
#ifdef CONFIG_MIGRATION
|
||||
} else if (!pte_file(oldpte)) {
|
||||
swp_entry_t entry = pte_to_swp_entry(oldpte);
|
||||
|
Loading…
Reference in New Issue
Block a user