Re: [PATCH 17/35] mm: Fixup places that call pte_mkwrite() directly

From: Dave Hansen
Date: Wed Feb 09 2022 - 16:51:59 EST


On 1/30/22 13:18, Rick Edgecombe wrote:
> - do_anonymous_page() and migrate_vma_insert_page() check VM_WRITE directly
> and call pte_mkwrite(), which is the same as maybe_mkwrite(). Change
> them to maybe_mkwrite().

Those look OK.

> - In do_numa_page(), if the numa entry was writable, then pte_mkwrite()
> is called directly. Fix it by doing maybe_mkwrite(). Make the same
> changes to do_huge_pmd_numa_page().

This is another "what", not "why" changelog. This change puzzles me.

*Why* is this needed? It sounds like pte_mkwrite() doesn't work for
shadow stack PTEs. Let's say that explicitly.

I also this this is ab/misuse of maybe_mkwrite().

The shadow stack VMA *REQUIRES* PTEs with Dirty=1. There's no *maybe*
about it. The rest of this is essentially a hack to get
VM_SHADOW_STACK-required bits into the PTE. We have a place where we
store those VMA-required bits: vma->vm_page_prot. Look at how we store
the pkey bits in there for instance.

Let's say we set _PAGE_DIRTY in vma->vm_page_prot. We'd come into
do_anonymous_page() for instance and do this:

> entry = mk_pte(page, vma->vm_page_prot); <--- PTE is Write=0,Dirty=1 Yay!
> entry = pte_sw_mkyoung(entry);
> if (vma->vm_flags & VM_WRITE) <--- False, skip the pte_mkwrite()
> entry = pte_mkwrite(pte_mkdirty(entry));

In other words, it "just works" because shadow stack VMAs don't have
VM_WRITE set.

I think the other VM_WRITE checks would be fine too, although I'm unsure
about the change_page_attr() one.