[PATCH v4 11/39] x86/mm: Update pte_modify for _PAGE_COW

From: Rick Edgecombe
Date: Fri Dec 02 2022 - 19:38:40 EST


From: Yu-cheng Yu <yu-cheng.yu@xxxxxxxxx>

The Write=0,Dirty=1 PTE has been used to indicate copy-on-write pages.
However, newer x86 processors also regard a Write=0,Dirty=1 PTE as a
shadow stack page. In order to separate the two, the software-defined
_PAGE_DIRTY is changed to _PAGE_COW for the copy-on-write case, and
pte_*() are updated to do this.

pte_modify() takes a "raw" pgprot_t which was not necessarily created
with any of the existing PTE bit helpers. That means that it can return a
pte_t with Write=0,Dirty=1, a shadow stack PTE, when it did not intend to
create one.

However pte_modify() changes a PTE to 'newprot', but it doesn't use the
pte_*(). Modify it to also move _PAGE_DIRTY to _PAGE_COW. Apply the same
changes to pmd_modify().

Tested-by: Pengfei Xu <pengfei.xu@xxxxxxxxx>
Tested-by: John Allen <john.allen@xxxxxxx>
Signed-off-by: Yu-cheng Yu <yu-cheng.yu@xxxxxxxxx>
Co-developed-by: Rick Edgecombe <rick.p.edgecombe@xxxxxxxxx>
Signed-off-by: Rick Edgecombe <rick.p.edgecombe@xxxxxxxxx>
---

v4:
- Fix an issue in soft-dirty test, where pte_modify() would detect
_PAGE_COW in pte_dirty() and set the soft dirty bit in pte_mkdirty().

v2:
- Update commit log with text and suggestions from (Dave Hansen)
- Drop fixup_dirty_pte() in favor of clearing the HW dirty bit along
with the _PAGE_CHG_MASK masking, then calling pte_mkdirty() (Dave
Hansen)

arch/x86/include/asm/pgtable.h | 40 +++++++++++++++++++++++++++++-----
1 file changed, 34 insertions(+), 6 deletions(-)

diff --git a/arch/x86/include/asm/pgtable.h b/arch/x86/include/asm/pgtable.h
index ee5fbdc2615f..67bd2627c293 100644
--- a/arch/x86/include/asm/pgtable.h
+++ b/arch/x86/include/asm/pgtable.h
@@ -698,26 +698,54 @@ static inline u64 flip_protnone_guard(u64 oldval, u64 val, u64 mask);

static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
{
+ pteval_t _page_chg_mask_no_dirty = _PAGE_CHG_MASK & ~_PAGE_DIRTY;
pteval_t val = pte_val(pte), oldval = val;
+ pte_t pte_result;

/*
* Chop off the NX bit (if present), and add the NX portion of
* the newprot (if present):
*/
- val &= _PAGE_CHG_MASK;
- val |= check_pgprot(newprot) & ~_PAGE_CHG_MASK;
+ val &= _page_chg_mask_no_dirty;
+ val |= check_pgprot(newprot) & ~_page_chg_mask_no_dirty;
val = flip_protnone_guard(oldval, val, PTE_PFN_MASK);
- return __pte(val);
+
+ pte_result = __pte(val);
+
+ /*
+ * Dirty bit is not preserved above so it can be done
+ * in a special way for the shadow stack case, where it
+ * needs to set _PAGE_COW. pte_mkcow() will do this in
+ * the case of shadow stack.
+ */
+ if (pte_dirty(pte_result))
+ pte_result = pte_mkcow(pte_result);
+
+ return pte_result;
}

static inline pmd_t pmd_modify(pmd_t pmd, pgprot_t newprot)
{
+ pteval_t _hpage_chg_mask_no_dirty = _HPAGE_CHG_MASK & ~_PAGE_DIRTY;
pmdval_t val = pmd_val(pmd), oldval = val;
+ pmd_t pmd_result;

- val &= _HPAGE_CHG_MASK;
- val |= check_pgprot(newprot) & ~_HPAGE_CHG_MASK;
+ val &= _hpage_chg_mask_no_dirty;
+ val |= check_pgprot(newprot) & ~_hpage_chg_mask_no_dirty;
val = flip_protnone_guard(oldval, val, PHYSICAL_PMD_PAGE_MASK);
- return __pmd(val);
+
+ pmd_result = __pmd(val);
+
+ /*
+ * Dirty bit is not preserved above so it can be done
+ * in a special way for the shadow stack case, where it
+ * needs to set _PAGE_COW. pmd_mkcow() will do this in
+ * the case of shadow stack.
+ */
+ if (pmd_dirty(pmd_result))
+ pmd_result = pmd_mkcow(pmd_result);
+
+ return pmd_result;
}

/*
--
2.17.1