3
0
Fork 0
forked from mirrors/linux
kernel/arch/mips
Kevin Brodsky d82d3bf411 mm: pass mm down to pagetable_{pte,pmd}_ctor
Patch series "Always call constructor for kernel page tables", v2.

There has been much confusion around exactly when page table
constructors/destructors (pagetable_*_[cd]tor) are supposed to be called. 
They were initially introduced for user PTEs only (to support split page
table locks), then at the PMD level for the same purpose.  Accounting was
added later on, starting at the PTE level and then moving to higher levels
(PMD, PUD).  Finally, with my earlier series "Account page tables at all
levels" [1], the ctor/dtor is run for all levels, all the way to PGD.

I thought this was the end of the story, and it hopefully is for user
pgtables, but I was wrong for what concerns kernel pgtables.  The current
situation there makes very little sense:

* At the PTE level, the ctor/dtor is not called (at least in the generic
  implementation).  Specific helpers are used for kernel pgtables at this
  level (pte_{alloc,free}_kernel()) and those have never called the
  ctor/dtor, most likely because they were initially irrelevant in the
  kernel case.

* At all other levels, the ctor/dtor is normally called.  This is
  potentially wasteful at the PMD level (more on that later).

This series aims to ensure that the ctor/dtor is always called for kernel
pgtables, as it already is for user pgtables.  Besides consistency, the
main motivation is to guarantee that ctor/dtor hooks are systematically
called; this makes it possible to insert hooks to protect page tables [2],
for instance.  There is however an extra challenge: split locks are not
used for kernel pgtables, and it would therefore be wasteful to initialise
them (ptlock_init()).

It is worth clarifying exactly when split locks are used.  They clearly
are for user pgtables, but as illustrated in commit 61444cde91 ("ARM:
8591/1: mm: use fully constructed struct pages for EFI pgd allocations"),
they also are for special page tables like efi_mm.  The one case where
split locks are definitely unused is pgtables owned by init_mm; this is
consistent with the behaviour of apply_to_pte_range().

The approach chosen in this series is therefore to pass the mm associated
to the pgtables being constructed to pagetable_{pte,pmd}_ctor() (patch 1),
and skip ptlock_init() if mm == &init_mm (patch 3 and 7).  This makes it
possible to call the PTE ctor/dtor from pte_{alloc,free}_kernel() without
unintended consequences (patch 3).  As a result the accounting functions
are now called at all levels for kernel pgtables, and split locks are
never initialised.

In configurations where ptlocks are dynamically allocated (32-bit,
PREEMPT_RT, etc.) and ARCH_ENABLE_SPLIT_PMD_PTLOCK is selected, this
series results in the removal of a kmem_cache allocation for every kernel
PMD.  Additionally, for certain architectures that do not use
<asm-generic/pgalloc.h> such as s390, the same optimisation occurs at the
PTE level.

===

Things get more complicated when it comes to special pgtable allocators
(patch 8-12).  All architectures need such allocators to create initial
kernel pgtables; we are not concerned with those as the ctor cannot be
called so early in the boot sequence.  However, those allocators may also
be used later in the boot sequence or during normal operations.  There are
two main use-cases:

1. Mapping EFI memory: efi_mm (arm, arm64, riscv)
2. arch_add_memory(): init_mm

The ctor is already explicitly run (at the PTE/PMD level) in the first
case, as required for pgtables that are not associated with init_mm. 
However the same allocators may also be used for the second use-case (or
others), and this is where it gets messy.  Patch 1 calls the ctor with
NULL as mm in those situations, as the actual mm isn't available. 
Practically this means that ptlocks will be unconditionally initialised. 
This is fine on arm - create_mapping_late() is only used for the EFI
mapping.  On arm64, __create_pgd_mapping() is also used by
arch_add_memory(); patch 8/9/11 ensure that ctors are called at all levels
with the appropriate mm.  The situation is similar on riscv, but
propagating the mm down to the ctor would require significant refactoring.
Since they are already called unconditionally, this series leaves riscv
no worse off - patch 10 adds comments to clarify the situation.

From a cursory look at other architectures implementing arch_add_memory(),
s390 and x86 may also need a similar treatment to add constructor calls. 
This is to be taken care of in a future version or as a follow-up.

===

The complications in those special pgtable allocators beg the question:
does it really make sense to treat efi_mm and init_mm differently in e.g. 
apply_to_pte_range()?  Maybe what we really need is a way to tell if an mm
corresponds to user memory or not, and never use split locks for non-user
mm's.  Feedback and suggestions welcome!


This patch (of 12):

In preparation for calling constructors for all kernel page tables while
eliding unnecessary ptlock initialisation, let's pass down the associated
mm to the PTE/PMD level ctors.  (These are the two levels where ptlocks
are used.)

In most cases the mm is already around at the point of calling the ctor so
we simply pass it down.  This is however not the case for special page
table allocators:

* arch/arm/mm/mmu.c
* arch/arm64/mm/mmu.c
* arch/riscv/mm/init.c

In those cases, the page tables being allocated are either for standard
kernel memory (init_mm) or special page directories, which may not be
associated to any mm.  For now let's pass NULL as mm; this will be refined
where possible in future patches.

No functional change in this patch.

Link: https://lore.kernel.org/linux-mm/20250103184415.2744423-1-kevin.brodsky@arm.com/ [1]
Link: https://lore.kernel.org/linux-hardening/20250203101839.1223008-1-kevin.brodsky@arm.com/ [2]
Link: https://lkml.kernel.org/r/20250408095222.860601-1-kevin.brodsky@arm.com
Link: https://lkml.kernel.org/r/20250408095222.860601-2-kevin.brodsky@arm.com
Signed-off-by: Kevin Brodsky <kevin.brodsky@arm.com>
Reviewed-by: Alexander Gordeev <agordeev@linux.ibm.com>	[s390]
Cc: Albert Ou <aou@eecs.berkeley.edu>
Cc: Andreas Larsson <andreas@gaisler.com>
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: David S. Miller <davem@davemloft.net>
Cc: Geert Uytterhoeven <geert@linux-m68k.org>
Cc: Kevin Brodsky <kevin.brodsky@arm.com>
Cc: Linus Waleij <linus.walleij@linaro.org>
Cc: Madhavan Srinivasan <maddy@linux.ibm.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Matthew Wilcox (Oracle) <willy@infradead.org>
Cc: Michael Ellerman <mpe@ellerman.id.au>
Cc: Mike Rapoport <rppt@kernel.org>
Cc: Palmer Dabbelt <palmer@dabbelt.com>
Cc: Paul Walmsley <paul.walmsley@sifive.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Qi Zheng <zhengqi.arch@bytedance.com>
Cc: Ryan Roberts <ryan.roberts@arm.com>
Cc: Will Deacon <will@kernel.org>
Cc: Yang Shi <yang@os.amperecomputing.com>
Cc: <x86@kernel.org>
Cc: Dave Hansen <dave.hansen@linux.intel.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
2025-05-11 17:48:21 -07:00
..
alchemy MIPS: Remove unused function dump_au1000_dma_channel() in dma.c 2024-08-29 10:38:18 +02:00
ath25
ath79
bcm47xx
bcm63xx gpiolib: legacy: Kill GPIOF_INIT_* definitions 2024-09-02 11:47:06 +02:00
bmips
boot Added support for multi-cluster configuration 2025-03-29 12:47:09 -07:00
cavium-octeon irqdomain: Rename irq_set_default_host() to irq_set_default_domain() 2025-04-04 16:39:10 +02:00
cobalt
configs lib/crc: remove unnecessary prompt for CONFIG_CRC_ITU_T 2025-04-04 11:31:42 -07:00
crypto crypto: lib/chacha - remove unused arch-specific init support 2025-03-21 17:39:06 +08:00
dec MIPS: dec: Declare which_prom() as static 2025-02-27 10:40:45 +01:00
fw
generic
include mm: pass mm down to pagetable_{pte,pmd}_ctor 2025-05-11 17:48:21 -07:00
ingenic
jazz mips/jazz: remove unused jazz_handle_int() declaration 2024-08-29 10:39:00 +02:00
kernel MIPS: CPS: Fix potential NULL pointer dereferences in cps_prepare_cpus() 2025-04-27 10:13:22 +02:00
kvm KVM: MIPS: Switch to use hrtimer_setup() 2025-02-18 10:32:30 +01:00
lantiq
lib asm-generic changes for 6.15 2025-03-27 09:46:53 -07:00
loongson2ef MIPS: Loongson2ef: Replace deprecated strncpy() with strscpy() 2025-02-27 10:40:01 +01:00
loongson32
loongson64 - The 6 patch series "Enable strict percpu address space checks" from 2025-04-01 09:29:18 -07:00
math-emu mips/math-emu: fix emulation of the prefx instruction 2025-01-11 12:49:05 +01:00
mm arch: remove mk_pmd() 2025-05-11 17:48:04 -07:00
mobileye
mti-malta
n64
net
pci irqdomain: Rename irq_get_default_host() to irq_get_default_domain() 2025-04-04 16:39:10 +02:00
pic32
power
ralink clocksource/drivers/ralink: Add Ralink System Tick Counter driver 2024-11-13 13:49:33 +01:00
rb532
sgi-ip22 treewide: Switch/rename to timer_delete[_sync]() 2025-04-05 10:30:12 +02:00
sgi-ip27 irqdomain: Rename irq_set_default_host() to irq_set_default_domain() 2025-04-04 16:39:10 +02:00
sgi-ip30 irqdomain: Rename irq_set_default_host() to irq_set_default_domain() 2025-04-04 16:39:10 +02:00
sgi-ip32
sibyte mips: sibyte: add missing MODULE_DESCRIPTION() macro 2024-07-23 09:47:40 +02:00
sni mips: sni: Do not include <linux/fb.h> 2025-02-21 10:19:48 +01:00
tools
txx9
vdso MIPS: vdso: Switch to generic storage implementation 2025-02-21 09:54:02 +01:00
Kbuild
Kbuild.platforms
Kconfig Kbuild updates for v6.15 2025-04-05 15:46:50 -07:00
Kconfig.debug
Makefile kbuild: Introduce Kconfig symbol for linking vmlinux with relocations 2025-03-17 00:29:50 +09:00
Makefile.postlink kbuild: Create intermediate vmlinux build with relocations preserved 2025-03-17 00:29:50 +09:00