[PATCH v3 0/9] x86/module: rework ROX cache to avoid writable copy

Mike Rapoport posted 9 patches 10 months, 3 weeks ago
arch/um/kernel/um_arch.c             |  11 +-
arch/x86/Kconfig                     |   1 +
arch/x86/entry/vdso/vma.c            |   3 +-
arch/x86/include/asm/alternative.h   |  14 +-
arch/x86/include/asm/pgtable_types.h |   2 +
arch/x86/kernel/alternative.c        | 181 +++++++++-------------
arch/x86/kernel/ftrace.c             |  30 ++--
arch/x86/kernel/module.c             |  45 ++----
arch/x86/mm/pat/cpa-test.c           |   2 +-
arch/x86/mm/pat/set_memory.c         | 220 ++++++++++++++++++++++++++-
include/linux/execmem.h              |  31 ++++
include/linux/module.h               |  16 --
include/linux/moduleloader.h         |   4 -
include/linux/vm_event_item.h        |   2 +
kernel/module/main.c                 |  78 +++-------
kernel/module/strict_rwx.c           |   9 +-
mm/execmem.c                         |  39 +++--
mm/vmstat.c                          |   2 +
18 files changed, 422 insertions(+), 268 deletions(-)
[PATCH v3 0/9] x86/module: rework ROX cache to avoid writable copy
Posted by Mike Rapoport 10 months, 3 weeks ago
From: "Mike Rapoport (Microsoft)" <rppt@kernel.org>

Hi,

Following Peter's comments [1] these patches rework handling of ROX caches
for module text allocations. 

Instead of using a writable copy that really complicates alternatives
patching, temporarily remap parts of a large ROX page as RW for the time of
module formation and then restore it's ROX protections when the module is
ready.

To keep the ROX memory mapped with large pages, make set_memory_rox()
capable of restoring large pages (more details are in patch 3).

Since this is really about x86, I believe this should go in via tip tree.

The patches also available in git
https://git.kernel.org/rppt/h/execmem/x86-rox/v10

v3 changes:
* instead of adding a new module state handle ROX restoration locally in
  load_module() as Petr suggested

v2: https://lore.kernel.org/all/20250121095739.986006-1-rppt@kernel.org
* only collapse large mappings in set_memory_rox()
* simplify RW <-> ROX remapping
* don't remove ROX cache pages from the direct map (patch 4)

v1: https://lore.kernel.org/all/20241227072825.1288491-1-rppt@kernel.org

[1] https://lore.kernel.org/all/20241209083818.GK8562@noisy.programming.kicks-ass.net

Kirill A. Shutemov (1):
  x86/mm/pat: restore large ROX pages after fragmentation

Mike Rapoport (Microsoft) (8):
  x86/mm/pat: cpa-test: fix length for CPA_ARRAY test
  x86/mm/pat: drop duplicate variable in cpa_flush()
  execmem: don't remove ROX cache from the direct map
  execmem: add API for temporal remapping as RW and restoring ROX afterwards
  module: switch to execmem API for remapping as RW and restoring ROX
  Revert "x86/module: prepare module loading for ROX allocations of text"
  module: drop unused module_writable_address()
  x86: re-enable EXECMEM_ROX support

 arch/um/kernel/um_arch.c             |  11 +-
 arch/x86/Kconfig                     |   1 +
 arch/x86/entry/vdso/vma.c            |   3 +-
 arch/x86/include/asm/alternative.h   |  14 +-
 arch/x86/include/asm/pgtable_types.h |   2 +
 arch/x86/kernel/alternative.c        | 181 +++++++++-------------
 arch/x86/kernel/ftrace.c             |  30 ++--
 arch/x86/kernel/module.c             |  45 ++----
 arch/x86/mm/pat/cpa-test.c           |   2 +-
 arch/x86/mm/pat/set_memory.c         | 220 ++++++++++++++++++++++++++-
 include/linux/execmem.h              |  31 ++++
 include/linux/module.h               |  16 --
 include/linux/moduleloader.h         |   4 -
 include/linux/vm_event_item.h        |   2 +
 kernel/module/main.c                 |  78 +++-------
 kernel/module/strict_rwx.c           |   9 +-
 mm/execmem.c                         |  39 +++--
 mm/vmstat.c                          |   2 +
 18 files changed, 422 insertions(+), 268 deletions(-)


base-commit: ffd294d346d185b70e28b1a28abe367bbfe53c04
-- 
2.45.2
Re: [PATCH v3 0/9] x86/module: rework ROX cache to avoid writable copy
Posted by Lorenzo Stoakes 10 months, 2 weeks ago
Hi Mike,

I bisected issues with the kernel booting correctly to an earlier revision
of this series [2]. I have now tested this series in the same environment
and found that it functions correctly with no issues, so feel free to add:

Tested-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>

Cheers, Lorenzo

[2]:https://lore.kernel.org/linux-mm/86eba318-464b-4b9b-a79e-64039b17be34@lucifer.local/

On Sun, Jan 26, 2025 at 09:47:24AM +0200, Mike Rapoport wrote:
> From: "Mike Rapoport (Microsoft)" <rppt@kernel.org>
>
> Hi,
>
> Following Peter's comments [1] these patches rework handling of ROX caches
> for module text allocations.
>
> Instead of using a writable copy that really complicates alternatives
> patching, temporarily remap parts of a large ROX page as RW for the time of
> module formation and then restore it's ROX protections when the module is
> ready.
>
> To keep the ROX memory mapped with large pages, make set_memory_rox()
> capable of restoring large pages (more details are in patch 3).
>
> Since this is really about x86, I believe this should go in via tip tree.
>
> The patches also available in git
> https://git.kernel.org/rppt/h/execmem/x86-rox/v10
>
> v3 changes:
> * instead of adding a new module state handle ROX restoration locally in
>   load_module() as Petr suggested
>
> v2: https://lore.kernel.org/all/20250121095739.986006-1-rppt@kernel.org
> * only collapse large mappings in set_memory_rox()
> * simplify RW <-> ROX remapping
> * don't remove ROX cache pages from the direct map (patch 4)
>
> v1: https://lore.kernel.org/all/20241227072825.1288491-1-rppt@kernel.org
>
> [1] https://lore.kernel.org/all/20241209083818.GK8562@noisy.programming.kicks-ass.net
>
> Kirill A. Shutemov (1):
>   x86/mm/pat: restore large ROX pages after fragmentation
>
> Mike Rapoport (Microsoft) (8):
>   x86/mm/pat: cpa-test: fix length for CPA_ARRAY test
>   x86/mm/pat: drop duplicate variable in cpa_flush()
>   execmem: don't remove ROX cache from the direct map
>   execmem: add API for temporal remapping as RW and restoring ROX afterwards
>   module: switch to execmem API for remapping as RW and restoring ROX
>   Revert "x86/module: prepare module loading for ROX allocations of text"
>   module: drop unused module_writable_address()
>   x86: re-enable EXECMEM_ROX support
>
>  arch/um/kernel/um_arch.c             |  11 +-
>  arch/x86/Kconfig                     |   1 +
>  arch/x86/entry/vdso/vma.c            |   3 +-
>  arch/x86/include/asm/alternative.h   |  14 +-
>  arch/x86/include/asm/pgtable_types.h |   2 +
>  arch/x86/kernel/alternative.c        | 181 +++++++++-------------
>  arch/x86/kernel/ftrace.c             |  30 ++--
>  arch/x86/kernel/module.c             |  45 ++----
>  arch/x86/mm/pat/cpa-test.c           |   2 +-
>  arch/x86/mm/pat/set_memory.c         | 220 ++++++++++++++++++++++++++-
>  include/linux/execmem.h              |  31 ++++
>  include/linux/module.h               |  16 --
>  include/linux/moduleloader.h         |   4 -
>  include/linux/vm_event_item.h        |   2 +
>  kernel/module/main.c                 |  78 +++-------
>  kernel/module/strict_rwx.c           |   9 +-
>  mm/execmem.c                         |  39 +++--
>  mm/vmstat.c                          |   2 +
>  18 files changed, 422 insertions(+), 268 deletions(-)
>
>
> base-commit: ffd294d346d185b70e28b1a28abe367bbfe53c04
> --
> 2.45.2
>
Re: [PATCH v3 0/9] x86/module: rework ROX cache to avoid writable copy
Posted by Peter Zijlstra 10 months, 3 weeks ago
On Sun, Jan 26, 2025 at 09:47:24AM +0200, Mike Rapoport wrote:
> v3 changes:
> * instead of adding a new module state handle ROX restoration locally in
>   load_module() as Petr suggested

OK, updated the patches I have pending for post -rc1.