Re: [PATCH v17 11/26] x86/mm: Update ptep_set_wrprotect() and pmdp_set_wrprotect() for transition from _PAGE_DIRTY to _PAGE_COW

From: Peter Zijlstra
Date: Tue Jan 26 2021 - 12:57:01 EST


On Mon, Jan 25, 2021 at 07:27:09PM +0100, Borislav Petkov wrote:

> > + pte_t old_pte, new_pte;
> > +
> > + do {
> > + old_pte = READ_ONCE(*ptep);
> > + new_pte = pte_wrprotect(old_pte);
>
> Maybe I'm missing something but those two can happen outside of the
> loop, no? Or is *ptep somehow changing concurrently while the loop is
> doing the CMPXCHG and you need to recreate it each time?
>
> IOW, you can generate upfront and do the empty loop...
>
> > +
> > + } while (!try_cmpxchg(&ptep->pte, &old_pte.pte, new_pte.pte));
> > +
> > + return;
> > + }

Empty loop would be wrong, but that wants to be written like:

old_pte = READ_ONCE(*ptep);
do {
new_pte = pte_wrprotect(old_pte);
} while (try_cmpxchg(&ptep->pte, &old_pte.pte, new_pte.pte));

Since try_cmpxchg() will update old_pte on failure.