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

2021-01-22 Thread Yu, Yu-cheng
On 1/21/2021 2:32 PM, David Laight wrote: From: Randy Dunlap Sent: 21 January 2021 22:19 On 1/21/21 2:16 PM, David Laight wrote: From: Yu, Yu-cheng On 1/21/2021 10:44 AM, Borislav Petkov wrote: On Tue, Dec 29, 2020 at 01:30:35PM -0800, Yu-cheng Yu wrote: [...] @@ -343,6 +349,16 @@ static

RE: [PATCH v17 08/26] x86/mm: Introduce _PAGE_COW

2021-01-21 Thread David Laight
From: Randy Dunlap > Sent: 21 January 2021 22:19 > > On 1/21/21 2:16 PM, David Laight wrote: > > From: Yu, Yu-cheng > >> > >> On 1/21/2021 10:44 AM, Borislav Petkov wrote: > >>> On Tue, Dec 29, 2020 at 01:30:35PM -0800, Yu-cheng Yu wrote: > >> [...] > @@ -343,6 +349,16 @@ static inline pte_t

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

2021-01-21 Thread Randy Dunlap
On 1/21/21 2:16 PM, David Laight wrote: > From: Yu, Yu-cheng >> >> On 1/21/2021 10:44 AM, Borislav Petkov wrote: >>> On Tue, Dec 29, 2020 at 01:30:35PM -0800, Yu-cheng Yu wrote: >> [...] @@ -343,6 +349,16 @@ static inline pte_t pte_mkold(pte_t pte) static inline pte_t pte_wrprotec

RE: [PATCH v17 08/26] x86/mm: Introduce _PAGE_COW

2021-01-21 Thread David Laight
From: Yu, Yu-cheng > > On 1/21/2021 10:44 AM, Borislav Petkov wrote: > > On Tue, Dec 29, 2020 at 01:30:35PM -0800, Yu-cheng Yu wrote: > [...] > >> @@ -343,6 +349,16 @@ static inline pte_t pte_mkold(pte_t pte) > >> > >> static inline pte_t pte_wrprotect(pte_t pte) > >> { > >> + /* > >> + *

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

2021-01-21 Thread Yu, Yu-cheng
On 1/21/2021 12:41 PM, Borislav Petkov wrote: On Thu, Jan 21, 2021 at 12:16:23PM -0800, Yu, Yu-cheng wrote: It clears _PAGE_DIRTY and sets _PAGE_COW. That is, if (pte.pte & _PAGE_DIRTY) { pte.pte &= ~_PAGE_DIRTY; pte.pte |= _PAGE_COW; } So, shifting makes resulting code more e

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

2021-01-21 Thread Yu, Yu-cheng
On 1/21/2021 12:26 PM, Dave Hansen wrote: Usually, the compiler is better at making code efficient than humans. I find that coding it in the most human-readable way is best unless I *know* the compiler is unable to generate god code. "good code", even. I really want a "god code" compiler, tho

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

2021-01-21 Thread Borislav Petkov
On Thu, Jan 21, 2021 at 12:16:23PM -0800, Yu, Yu-cheng wrote: > It clears _PAGE_DIRTY and sets _PAGE_COW. That is, > > if (pte.pte & _PAGE_DIRTY) { > pte.pte &= ~_PAGE_DIRTY; > pte.pte |= _PAGE_COW; > } > > So, shifting makes resulting code more efficient. Efficient for what? Is thi

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

2021-01-21 Thread Dave Hansen
> Usually, the compiler is better at making code efficient than humans. I > find that coding it in the most human-readable way is best unless I > *know* the compiler is unable to generate god code. "good code", even. I really want a "god code" compiler, though. :)

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

2021-01-21 Thread Dave Hansen
On 1/21/21 12:16 PM, Yu, Yu-cheng wrote: > >>> @@ -343,6 +349,16 @@ static inline pte_t pte_mkold(pte_t pte) >>>     static inline pte_t pte_wrprotect(pte_t pte) >>>   { >>> +    /* >>> + * Blindly clearing _PAGE_RW might accidentally create >>> + * a shadow stack PTE (RW=0, Dirty=1).  Mov

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

2021-01-21 Thread Yu, Yu-cheng
On 1/21/2021 10:44 AM, Borislav Petkov wrote: On Tue, Dec 29, 2020 at 01:30:35PM -0800, Yu-cheng Yu wrote: [...] @@ -343,6 +349,16 @@ static inline pte_t pte_mkold(pte_t pte) static inline pte_t pte_wrprotect(pte_t pte) { + /* +* Blindly clearing _PAGE_RW might accidentall

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

2021-01-21 Thread Borislav Petkov
On Tue, Dec 29, 2020 at 01:30:35PM -0800, Yu-cheng Yu wrote: > @@ -182,6 +182,12 @@ static inline int pud_young(pud_t pud) > > static inline int pte_write(pte_t pte) > { > + /* > + * If _PAGE_DIRTY is set, the PTE must either have _PAGE_RW or be > + * a shadow stack PTE, which is

[PATCH v17 08/26] x86/mm: Introduce _PAGE_COW

2020-12-29 Thread Yu-cheng Yu
There is essentially no room left in the x86 hardware PTEs on some OSes (not Linux). That left the hardware architects looking for a way to represent a new memory type (shadow stack) within the existing bits. They chose to repurpose a lightly-used state: Write=0, Dirty=1. The reason it's lightly