Re: [PATCH v21 08/26] x86/mm: Introduce _PAGE_COW

Yu, Yu-cheng
2021-03-02T01:40:24Z

On 3/1/2021 7:52 AM, Borislav Petkov wrote:
> On Wed, Feb 17, 2021 at 02:27:12PM -0800, Yu-cheng Yu wrote:
>> @@ -182,7 +206,7 @@ static inline int pud_young(pud_t pud)
>>
>> static inline int pte_write(pte_t pte)
>> {
>> - return pte_flags(pte) & _PAGE_RW;
>
> Put here a comment along the lines of:
>
> /*
> * Shadow stack pages are always writable - but not by normal
> * instructions but only by shadow stack operations. Therefore, the
> * W=0, D=1 test.
> */
>
> to make it clear what this means.
>
>> + return (pte_flags(pte) & _PAGE_RW) || pte_shstk(pte);
>> }
>>
>> static inline int pte_huge(pte_t pte)
>> @@ -314,6 +338,24 @@ static inline pte_t pte_clear_flags(pte_t pte, pteval_t clear)
>> return native_make_pte(v & ~clear);
>> }
>>
>> +static inline pte_t pte_make_cow(pte_t pte)
>
> pte_mkcow like the rest of the "pte_mkX" functions.
>
> And below too, for the other newly added pXd_make_* helpers.
>
>
>> static inline pmd_t pmd_mkdirty(pmd_t pmd)
>> {
>> - return pmd_set_flags(pmd, _PAGE_DIRTY | _PAGE_SOFT_DIRTY);
>> + pmdval_t dirty = _PAGE_DIRTY;
>> +
>> + /* Avoid creating (HW)Dirty=1, Write=0 PMDs */
>> + if (cpu_feature_enabled(X86_FEATURE_SHSTK) && !(pmd_flags(pmd) & _PAGE_RW))
>
> !(pmd_write(pmd))
>
>> + dirty = _PAGE_COW;
>> +
>> + return pmd_set_flags(pmd, dirty | _PAGE_SOFT_DIRTY);
>> +}
>
> ...
>
>> static inline pud_t pud_mkdirty(pud_t pud)
>> {
>> - return pud_set_flags(pud, _PAGE_DIRTY | _PAGE_SOFT_DIRTY);
>> + pudval_t dirty = _PAGE_DIRTY;
>> +
>> + /* Avoid creating (HW)Dirty=1, Write=0 PUDs */
>> + if (cpu_feature_enabled(X86_FEATURE_SHSTK) && !(pud_flags(pud) & _PAGE_RW))
>
> !(pud_write(pud))
>
>

I will update. Thanks!

--
Yu-cheng