Re: [PATCH v5 11/39] x86/mm: Update pte_modify for _PAGE_COW
From: Edgecombe, Rick P
Date: Thu Feb 09 2023 - 12:09:36 EST
On Thu, 2023-02-09 at 15:08 +0100, Borislav Petkov wrote:
> On Thu, Jan 19, 2023 at 01:22:49PM -0800, Rick Edgecombe wrote:
> > 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.
>
> "In order to separate the two, change the software-defined ..."
>
> From section "2) Describe your changes" in
> Documentation/process/submitting-patches.rst:
>
> "Describe your changes in imperative mood, e.g. "make xyzzy do frotz"
> instead of "[This patch] makes xyzzy do frotz" or "[I] changed xyzzy
> to do frotz", as if you are giving orders to the codebase to change
> its behaviour."
Yea, this is ambiguous. It's actually trying to say that "the software-
defined..." *were* changed in previous patches. I'll change it to make
that clear.
>
> > +static inline pte_t __pte_mkdirty(pte_t pte, bool soft)
> > +{
> > + pteval_t dirty = _PAGE_DIRTY;
> > +
> > + if (soft)
> > + dirty |= _PAGE_SOFT_DIRTY;
> > +
> > + return pte_set_flags(pte, dirty);
> > +}
>
> Dunno, do you even need that __pte_mkdirty() helper?
>
> AFAIU, pte_mkdirty() will always set _PAGE_SOFT_DIRTY too so whatever
> the __pte_mkdirty() thing needs to do, you can simply do it by foot
> in
> the two callsites.
>
> And this way you won't have the confusion: should I use pte_mkdirty()
> or
> __pte_mkdirty()?
>
> Ditto for the pmd variants.
>
> Otherwise, this is starting to make more sense now.
The thing is it would need to duplicate the pte_write() and shadow
stack enablement check and know when to set the Cow(soon to be
SavedDirty) bit.
I see that having a similar helper is not ideal, but isn't it nice that
this special critical logic for setting the Cow bit is all in one
place? I actually tried it the other way, but thought that it was nicer
to have a helper that might drive future people to not miss the Cow bit
part.
What do you think, can we leave it or give it a new name? Maybe
pte_set_dirty() to be more like the x86-only pte_set_flags() family of
functions? Then we have:
static inline pte_t pte_mkdirty(pte_t pte)
{
pte = pte_set_flags(pte, _PAGE_SOFT_DIRTY);
return pte_set_dirty(pte);
}
And...
static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
...
/*
* Dirty bit is not preserved above so it can be done
* in a special way for the shadow stack case, where it
* may need to set _PAGE_SAVED_DIRTY. __pte_mkdirty() will do
* this in the case of shadow stack.
*/
if (oldval & _PAGE_DIRTY)
pte_result = pte_set_dirty(pte_result);
return pte_result;
}