Re: [PATCH v6 13/41] mm: Make pte_mkwrite() take a VMA

2023-03-01 Thread Deepak Gupta

On Sat, Feb 18, 2023 at 01:14:05PM -0800, Rick Edgecombe wrote:

The x86 Control-flow Enforcement Technology (CET) feature includes a new
type of memory called shadow stack. This shadow stack memory has some
unusual properties, which requires some core mm changes to function
properly.

One of these unusual properties is that shadow stack memory is writable,
but only in limited ways. These limits are applied via a specific PTE
bit combination. Nevertheless, the memory is writable, and core mm code
will need to apply the writable permissions in the typical paths that
call pte_mkwrite().

In addition to VM_WRITE, the shadow stack VMA's will have a flag denoting
that they are special shadow stack flavor of writable memory. So make
pte_mkwrite() take a VMA, so that the x86 implementation of it can know to
create regular writable memory or shadow stack memory.

Apply the same changes for pmd_mkwrite() and huge_pte_mkwrite().

No functional change.

Cc: linux-...@vger.kernel.org
Cc: linux-ker...@vger.kernel.org
Cc: linux-al...@vger.kernel.org
Cc: linux-snps-...@lists.infradead.org
Cc: linux-arm-ker...@lists.infradead.org
Cc: linux-c...@vger.kernel.org
Cc: linux-hexa...@vger.kernel.org
Cc: linux-i...@vger.kernel.org
Cc: loonga...@lists.linux.dev
Cc: linux-m...@lists.linux-m68k.org
Cc: Michal Simek 
Cc: Dinh Nguyen 
Cc: linux-m...@vger.kernel.org
Cc: openr...@lists.librecores.org
Cc: linux-par...@vger.kernel.org
Cc: linuxppc-dev@lists.ozlabs.org
Cc: linux-ri...@lists.infradead.org
Cc: linux-s...@vger.kernel.org
Cc: linux...@vger.kernel.org
Cc: sparcli...@vger.kernel.org
Cc: linux...@lists.infradead.org
Cc: xen-de...@lists.xenproject.org
Cc: linux-a...@vger.kernel.org
Cc: linux...@kvack.org
Tested-by: Pengfei Xu 
Suggested-by: David Hildenbrand 
Signed-off-by: Rick Edgecombe 



Acked-by: Deepak Gupta 


Re: [PATCH v6 13/41] mm: Make pte_mkwrite() take a VMA

2023-02-20 Thread Edgecombe, Rick P
On Mon, 2023-02-20 at 12:23 +0100, David Hildenbrand wrote:
> That looks painful but IMHO worth it :)
> 
> Acked-by: David Hildenbrand 

Thanks. Yes it was not the most fun, but I agree - worth it.


Re: [PATCH v6 13/41] mm: Make pte_mkwrite() take a VMA

2023-02-20 Thread Edgecombe, Rick P
On Mon, 2023-02-20 at 12:00 +1100, Michael Ellerman wrote:
> Acked-by: Michael Ellerman  (powerpc)

Thanks!


Re: [PATCH v6 13/41] mm: Make pte_mkwrite() take a VMA

2023-02-20 Thread David Hildenbrand

On 18.02.23 22:14, Rick Edgecombe wrote:

The x86 Control-flow Enforcement Technology (CET) feature includes a new
type of memory called shadow stack. This shadow stack memory has some
unusual properties, which requires some core mm changes to function
properly.

One of these unusual properties is that shadow stack memory is writable,
but only in limited ways. These limits are applied via a specific PTE
bit combination. Nevertheless, the memory is writable, and core mm code
will need to apply the writable permissions in the typical paths that
call pte_mkwrite().

In addition to VM_WRITE, the shadow stack VMA's will have a flag denoting
that they are special shadow stack flavor of writable memory. So make
pte_mkwrite() take a VMA, so that the x86 implementation of it can know to
create regular writable memory or shadow stack memory.

Apply the same changes for pmd_mkwrite() and huge_pte_mkwrite().

No functional change.

Cc: linux-...@vger.kernel.org
Cc: linux-ker...@vger.kernel.org
Cc: linux-al...@vger.kernel.org
Cc: linux-snps-...@lists.infradead.org
Cc: linux-arm-ker...@lists.infradead.org
Cc: linux-c...@vger.kernel.org
Cc: linux-hexa...@vger.kernel.org
Cc: linux-i...@vger.kernel.org
Cc: loonga...@lists.linux.dev
Cc: linux-m...@lists.linux-m68k.org
Cc: Michal Simek 
Cc: Dinh Nguyen 
Cc: linux-m...@vger.kernel.org
Cc: openr...@lists.librecores.org
Cc: linux-par...@vger.kernel.org
Cc: linuxppc-dev@lists.ozlabs.org
Cc: linux-ri...@lists.infradead.org
Cc: linux-s...@vger.kernel.org
Cc: linux...@vger.kernel.org
Cc: sparcli...@vger.kernel.org
Cc: linux...@lists.infradead.org
Cc: xen-de...@lists.xenproject.org
Cc: linux-a...@vger.kernel.org
Cc: linux...@kvack.org
Tested-by: Pengfei Xu 
Suggested-by: David Hildenbrand 
Signed-off-by: Rick Edgecombe 

---
Hi Non-x86 Arch’s,

x86 has a feature that allows for the creation of a special type of
writable memory (shadow stack) that is only writable in limited specific
ways. Previously, changes were proposed to core MM code to teach it to
decide when to create normally writable memory or the special shadow stack
writable memory, but David Hildenbrand suggested[0] to change
pXX_mkwrite() to take a VMA, so awareness of shadow stack memory can be
moved into x86 code.

Since pXX_mkwrite() is defined in every arch, it requires some tree-wide
changes. So that is why you are seeing some patches out of a big x86
series pop up in your arch mailing list. There is no functional change.
After this refactor, the shadow stack series goes on to use the arch
helpers to push shadow stack memory details inside arch/x86.

Testing was just 0-day build testing.

Hopefully that is enough context. Thanks!

[0] 
https://lore.kernel.org/lkml/0e29a2d0-08d8-bcd6-ff26-4bea0e403...@redhat.com/#t

v6:
  - New patch
---
  Documentation/mm/arch_pgtable_helpers.rst|  9 ++---
  arch/alpha/include/asm/pgtable.h |  6 +-
  arch/arc/include/asm/hugepage.h  |  2 +-
  arch/arc/include/asm/pgtable-bits-arcv2.h|  7 ++-
  arch/arm/include/asm/pgtable-3level.h|  7 ++-
  arch/arm/include/asm/pgtable.h   |  2 +-
  arch/arm64/include/asm/pgtable.h |  4 ++--
  arch/csky/include/asm/pgtable.h  |  2 +-
  arch/hexagon/include/asm/pgtable.h   |  2 +-
  arch/ia64/include/asm/pgtable.h  |  2 +-
  arch/loongarch/include/asm/pgtable.h |  4 ++--
  arch/m68k/include/asm/mcf_pgtable.h  |  2 +-
  arch/m68k/include/asm/motorola_pgtable.h |  6 +-
  arch/m68k/include/asm/sun3_pgtable.h |  6 +-
  arch/microblaze/include/asm/pgtable.h|  2 +-
  arch/mips/include/asm/pgtable.h  |  6 +++---
  arch/nios2/include/asm/pgtable.h |  2 +-
  arch/openrisc/include/asm/pgtable.h  |  2 +-
  arch/parisc/include/asm/pgtable.h|  6 +-
  arch/powerpc/include/asm/book3s/32/pgtable.h |  2 +-
  arch/powerpc/include/asm/book3s/64/pgtable.h |  4 ++--
  arch/powerpc/include/asm/nohash/32/pgtable.h |  2 +-
  arch/powerpc/include/asm/nohash/32/pte-8xx.h |  2 +-
  arch/powerpc/include/asm/nohash/64/pgtable.h |  2 +-
  arch/riscv/include/asm/pgtable.h |  6 +++---
  arch/s390/include/asm/hugetlb.h  |  4 ++--
  arch/s390/include/asm/pgtable.h  |  4 ++--
  arch/sh/include/asm/pgtable_32.h | 10 --
  arch/sparc/include/asm/pgtable_32.h  |  2 +-
  arch/sparc/include/asm/pgtable_64.h  |  6 +++---
  arch/um/include/asm/pgtable.h|  2 +-
  arch/x86/include/asm/pgtable.h   |  6 --
  arch/xtensa/include/asm/pgtable.h|  2 +-
  include/asm-generic/hugetlb.h|  4 ++--
  include/linux/mm.h   |  2 +-
  mm/debug_vm_pgtable.c| 16 
  mm/huge_memory.c |  6 +++---
  mm/hugetlb.c |  4 ++--
  mm/memory.c 

Re: [PATCH v6 13/41] mm: Make pte_mkwrite() take a VMA

2023-02-19 Thread Michael Ellerman
Rick Edgecombe  writes:
> The x86 Control-flow Enforcement Technology (CET) feature includes a new
> type of memory called shadow stack. This shadow stack memory has some
> unusual properties, which requires some core mm changes to function
> properly.
...
> ---
> Hi Non-x86 Arch’s,
>
> x86 has a feature that allows for the creation of a special type of
> writable memory (shadow stack) that is only writable in limited specific
> ways. Previously, changes were proposed to core MM code to teach it to
> decide when to create normally writable memory or the special shadow stack
> writable memory, but David Hildenbrand suggested[0] to change
> pXX_mkwrite() to take a VMA, so awareness of shadow stack memory can be
> moved into x86 code.
>
> Since pXX_mkwrite() is defined in every arch, it requires some tree-wide
> changes. So that is why you are seeing some patches out of a big x86
> series pop up in your arch mailing list. There is no functional change.
> After this refactor, the shadow stack series goes on to use the arch
> helpers to push shadow stack memory details inside arch/x86.
...
> ---
>  Documentation/mm/arch_pgtable_helpers.rst|  9 ++---
>  arch/alpha/include/asm/pgtable.h |  6 +-
>  arch/arc/include/asm/hugepage.h  |  2 +-
>  arch/arc/include/asm/pgtable-bits-arcv2.h|  7 ++-
>  arch/arm/include/asm/pgtable-3level.h|  7 ++-
>  arch/arm/include/asm/pgtable.h   |  2 +-
>  arch/arm64/include/asm/pgtable.h |  4 ++--
>  arch/csky/include/asm/pgtable.h  |  2 +-
>  arch/hexagon/include/asm/pgtable.h   |  2 +-
>  arch/ia64/include/asm/pgtable.h  |  2 +-
>  arch/loongarch/include/asm/pgtable.h |  4 ++--
>  arch/m68k/include/asm/mcf_pgtable.h  |  2 +-
>  arch/m68k/include/asm/motorola_pgtable.h |  6 +-
>  arch/m68k/include/asm/sun3_pgtable.h |  6 +-
>  arch/microblaze/include/asm/pgtable.h|  2 +-
>  arch/mips/include/asm/pgtable.h  |  6 +++---
>  arch/nios2/include/asm/pgtable.h |  2 +-
>  arch/openrisc/include/asm/pgtable.h  |  2 +-
>  arch/parisc/include/asm/pgtable.h|  6 +-
>  arch/powerpc/include/asm/book3s/32/pgtable.h |  2 +-
>  arch/powerpc/include/asm/book3s/64/pgtable.h |  4 ++--
>  arch/powerpc/include/asm/nohash/32/pgtable.h |  2 +-
>  arch/powerpc/include/asm/nohash/32/pte-8xx.h |  2 +-
>  arch/powerpc/include/asm/nohash/64/pgtable.h |  2 +-

Looks like you discovered the joys of ppc's at-least 5 different MMU
implementations, sorry :)

Acked-by: Michael Ellerman  (powerpc)

cheers


Re: [PATCH v6 13/41] mm: Make pte_mkwrite() take a VMA

2023-02-19 Thread Kees Cook
On Sat, Feb 18, 2023 at 01:14:05PM -0800, Rick Edgecombe wrote:
> The x86 Control-flow Enforcement Technology (CET) feature includes a new
> type of memory called shadow stack. This shadow stack memory has some
> unusual properties, which requires some core mm changes to function
> properly.
> 
> One of these unusual properties is that shadow stack memory is writable,
> but only in limited ways. These limits are applied via a specific PTE
> bit combination. Nevertheless, the memory is writable, and core mm code
> will need to apply the writable permissions in the typical paths that
> call pte_mkwrite().
> 
> In addition to VM_WRITE, the shadow stack VMA's will have a flag denoting
> that they are special shadow stack flavor of writable memory. So make
> pte_mkwrite() take a VMA, so that the x86 implementation of it can know to
> create regular writable memory or shadow stack memory.
> 
> Apply the same changes for pmd_mkwrite() and huge_pte_mkwrite().
> 
> No functional change.
> 
> Cc: linux-...@vger.kernel.org
> Cc: linux-ker...@vger.kernel.org
> Cc: linux-al...@vger.kernel.org
> Cc: linux-snps-...@lists.infradead.org
> Cc: linux-arm-ker...@lists.infradead.org
> Cc: linux-c...@vger.kernel.org
> Cc: linux-hexa...@vger.kernel.org
> Cc: linux-i...@vger.kernel.org
> Cc: loonga...@lists.linux.dev
> Cc: linux-m...@lists.linux-m68k.org
> Cc: Michal Simek 
> Cc: Dinh Nguyen 
> Cc: linux-m...@vger.kernel.org
> Cc: openr...@lists.librecores.org
> Cc: linux-par...@vger.kernel.org
> Cc: linuxppc-dev@lists.ozlabs.org
> Cc: linux-ri...@lists.infradead.org
> Cc: linux-s...@vger.kernel.org
> Cc: linux...@vger.kernel.org
> Cc: sparcli...@vger.kernel.org
> Cc: linux...@lists.infradead.org
> Cc: xen-de...@lists.xenproject.org
> Cc: linux-a...@vger.kernel.org
> Cc: linux...@kvack.org
> Tested-by: Pengfei Xu 
> Suggested-by: David Hildenbrand 
> Signed-off-by: Rick Edgecombe 

I'm not an arch maintainer, but it looks like a correct tree-wide
refactor.

Reviewed-by: Kees Cook 

-- 
Kees Cook


[PATCH v6 13/41] mm: Make pte_mkwrite() take a VMA

2023-02-18 Thread Rick Edgecombe
The x86 Control-flow Enforcement Technology (CET) feature includes a new
type of memory called shadow stack. This shadow stack memory has some
unusual properties, which requires some core mm changes to function
properly.

One of these unusual properties is that shadow stack memory is writable,
but only in limited ways. These limits are applied via a specific PTE
bit combination. Nevertheless, the memory is writable, and core mm code
will need to apply the writable permissions in the typical paths that
call pte_mkwrite().

In addition to VM_WRITE, the shadow stack VMA's will have a flag denoting
that they are special shadow stack flavor of writable memory. So make
pte_mkwrite() take a VMA, so that the x86 implementation of it can know to
create regular writable memory or shadow stack memory.

Apply the same changes for pmd_mkwrite() and huge_pte_mkwrite().

No functional change.

Cc: linux-...@vger.kernel.org
Cc: linux-ker...@vger.kernel.org
Cc: linux-al...@vger.kernel.org
Cc: linux-snps-...@lists.infradead.org
Cc: linux-arm-ker...@lists.infradead.org
Cc: linux-c...@vger.kernel.org
Cc: linux-hexa...@vger.kernel.org
Cc: linux-i...@vger.kernel.org
Cc: loonga...@lists.linux.dev
Cc: linux-m...@lists.linux-m68k.org
Cc: Michal Simek 
Cc: Dinh Nguyen 
Cc: linux-m...@vger.kernel.org
Cc: openr...@lists.librecores.org
Cc: linux-par...@vger.kernel.org
Cc: linuxppc-dev@lists.ozlabs.org
Cc: linux-ri...@lists.infradead.org
Cc: linux-s...@vger.kernel.org
Cc: linux...@vger.kernel.org
Cc: sparcli...@vger.kernel.org
Cc: linux...@lists.infradead.org
Cc: xen-de...@lists.xenproject.org
Cc: linux-a...@vger.kernel.org
Cc: linux...@kvack.org
Tested-by: Pengfei Xu 
Suggested-by: David Hildenbrand 
Signed-off-by: Rick Edgecombe 

---
Hi Non-x86 Arch’s,

x86 has a feature that allows for the creation of a special type of
writable memory (shadow stack) that is only writable in limited specific
ways. Previously, changes were proposed to core MM code to teach it to
decide when to create normally writable memory or the special shadow stack
writable memory, but David Hildenbrand suggested[0] to change
pXX_mkwrite() to take a VMA, so awareness of shadow stack memory can be
moved into x86 code.

Since pXX_mkwrite() is defined in every arch, it requires some tree-wide
changes. So that is why you are seeing some patches out of a big x86
series pop up in your arch mailing list. There is no functional change.
After this refactor, the shadow stack series goes on to use the arch
helpers to push shadow stack memory details inside arch/x86.

Testing was just 0-day build testing.

Hopefully that is enough context. Thanks!

[0] 
https://lore.kernel.org/lkml/0e29a2d0-08d8-bcd6-ff26-4bea0e403...@redhat.com/#t

v6:
 - New patch
---
 Documentation/mm/arch_pgtable_helpers.rst|  9 ++---
 arch/alpha/include/asm/pgtable.h |  6 +-
 arch/arc/include/asm/hugepage.h  |  2 +-
 arch/arc/include/asm/pgtable-bits-arcv2.h|  7 ++-
 arch/arm/include/asm/pgtable-3level.h|  7 ++-
 arch/arm/include/asm/pgtable.h   |  2 +-
 arch/arm64/include/asm/pgtable.h |  4 ++--
 arch/csky/include/asm/pgtable.h  |  2 +-
 arch/hexagon/include/asm/pgtable.h   |  2 +-
 arch/ia64/include/asm/pgtable.h  |  2 +-
 arch/loongarch/include/asm/pgtable.h |  4 ++--
 arch/m68k/include/asm/mcf_pgtable.h  |  2 +-
 arch/m68k/include/asm/motorola_pgtable.h |  6 +-
 arch/m68k/include/asm/sun3_pgtable.h |  6 +-
 arch/microblaze/include/asm/pgtable.h|  2 +-
 arch/mips/include/asm/pgtable.h  |  6 +++---
 arch/nios2/include/asm/pgtable.h |  2 +-
 arch/openrisc/include/asm/pgtable.h  |  2 +-
 arch/parisc/include/asm/pgtable.h|  6 +-
 arch/powerpc/include/asm/book3s/32/pgtable.h |  2 +-
 arch/powerpc/include/asm/book3s/64/pgtable.h |  4 ++--
 arch/powerpc/include/asm/nohash/32/pgtable.h |  2 +-
 arch/powerpc/include/asm/nohash/32/pte-8xx.h |  2 +-
 arch/powerpc/include/asm/nohash/64/pgtable.h |  2 +-
 arch/riscv/include/asm/pgtable.h |  6 +++---
 arch/s390/include/asm/hugetlb.h  |  4 ++--
 arch/s390/include/asm/pgtable.h  |  4 ++--
 arch/sh/include/asm/pgtable_32.h | 10 --
 arch/sparc/include/asm/pgtable_32.h  |  2 +-
 arch/sparc/include/asm/pgtable_64.h  |  6 +++---
 arch/um/include/asm/pgtable.h|  2 +-
 arch/x86/include/asm/pgtable.h   |  6 --
 arch/xtensa/include/asm/pgtable.h|  2 +-
 include/asm-generic/hugetlb.h|  4 ++--
 include/linux/mm.h   |  2 +-
 mm/debug_vm_pgtable.c| 16 
 mm/huge_memory.c |  6 +++---
 mm/hugetlb.c |  4 ++--
 mm/memory.c  |  4 ++--
 mm/migrate_device.c  |  2 +-
 mm/mprotect.c