Skip to contentSkip to conversation

arm64: support FEAT_BBM level 2 and large block mapping when rodata=full

PATCH Applied Yang Shi 11 participants 51 messages Sep 17, 2025 – Mar 19

Subsystems (3)
lore
Share & tools
lore

Yang Shi

author
#0 lore raw

v8 cover arm64: support FEAT_BBM level 2 and large block mapping when rodata=full

On systems with BBML2_NOABORT support, it causes the linear map to be mapped with large blocks, even when rodata=full, and leads to some nice performance improvements.

Ryan tested v7 on an AmpereOne system (a VM with 12G RAM) in all 3 possible modes by hacking the BBML2 feature detection code:

- mode 1: All CPUs support BBML2 so the linear map uses large mappings
- mode 2: Boot CPU does not support BBML2 so linear map uses pte mappings
- mode 3: Boot CPU supports BBML2 but secondaries do not so linear map initially uses large mappings but is then repainted to use pte mappings

In all cases, mm selftests run and no regressions are observed. In all cases, ptdump of linear map is as expected. Because there are just some cleanups between v7 and v8, so I kept using Ryan's test result:

Mode 1:
=======
---[ Linear Mapping start ]---

0xffff000000000000-0xffff000000200000           2M PMD       RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED
0xffff000000200000-0xffff000000210000          64K PTE       RW NX SHD AF    CON     UXN    MEM/NORMAL-TAGGED
0xffff000000210000-0xffff000000400000        1984K PTE       ro NX SHD AF            UXN    MEM/NORMAL
0xffff000000400000-0xffff000002400000          32M PMD       ro NX SHD AF        BLK UXN    MEM/NORMAL
0xffff000002400000-0xffff000002550000        1344K PTE       ro NX SHD AF            UXN    MEM/NORMAL
0xffff000002550000-0xffff000002600000         704K PTE       RW NX SHD AF    CON     UXN    MEM/NORMAL-TAGGED
0xffff000002600000-0xffff000004000000          26M PMD       RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED
0xffff000004000000-0xffff000040000000         960M PMD       RW NX SHD AF    CON BLK UXN    MEM/NORMAL-TAGGED
0xffff000040000000-0xffff000140000000           4G PUD       RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED
0xffff000140000000-0xffff000142000000          32M PMD       RW NX SHD AF    CON BLK UXN    MEM/NORMAL-TAGGED
0xffff000142000000-0xffff000142120000        1152K PTE       RW NX SHD AF    CON     UXN    MEM/NORMAL-TAGGED
0xffff000142120000-0xffff000142128000          32K PTE       RW NX SHD AF            UXN    MEM/NORMAL-TAGGED
0xffff000142128000-0xffff000142159000         196K PTE       ro NX SHD AF            UXN    MEM/NORMAL-TAGGED
0xffff000142159000-0xffff000142160000          28K PTE       RW NX SHD AF            UXN    MEM/NORMAL-TAGGED
0xffff000142160000-0xffff000142240000         896K PTE       RW NX SHD AF    CON     UXN    MEM/NORMAL-TAGGED
0xffff000142240000-0xffff00014224e000          56K PTE       RW NX SHD AF            UXN    MEM/NORMAL-TAGGED
0xffff00014224e000-0xffff000142250000           8K PTE       ro NX SHD AF            UXN    MEM/NORMAL-TAGGED
0xffff000142250000-0xffff000142260000          64K PTE       RW NX SHD AF            UXN    MEM/NORMAL-TAGGED
0xffff000142260000-0xffff000142280000         128K PTE       RW NX SHD AF    CON     UXN    MEM/NORMAL-TAGGED
0xffff000142280000-0xffff000142288000          32K PTE       RW NX SHD AF            UXN    MEM/NORMAL-TAGGED
0xffff000142288000-0xffff000142290000          32K PTE       ro NX SHD AF            UXN    MEM/NORMAL-TAGGED
0xffff000142290000-0xffff0001422a0000          64K PTE       RW NX SHD AF            UXN    MEM/NORMAL-TAGGED
0xffff0001422a0000-0xffff000142465000        1812K PTE       ro NX SHD AF            UXN    MEM/NORMAL-TAGGED
0xffff000142465000-0xffff000142470000          44K PTE       RW NX SHD AF            UXN    MEM/NORMAL-TAGGED
0xffff000142470000-0xffff000142600000        1600K PTE       RW NX SHD AF    CON     UXN    MEM/NORMAL-TAGGED
0xffff000142600000-0xffff000144000000          26M PMD       RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED
0xffff000144000000-0xffff000180000000         960M PMD       RW NX SHD AF    CON BLK UXN    MEM/NORMAL-TAGGED
0xffff000180000000-0xffff000181a00000          26M PMD       RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED
0xffff000181a00000-0xffff000181b90000        1600K PTE       RW NX SHD AF    CON     UXN    MEM/NORMAL-TAGGED
0xffff000181b90000-0xffff000181b9d000          52K PTE       RW NX SHD AF            UXN    MEM/NORMAL-TAGGED
0xffff000181b9d000-0xffff000181c80000         908K PTE       ro NX SHD AF            UXN    MEM/NORMAL-TAGGED
0xffff000181c80000-0xffff000181c90000          64K PTE       RW NX SHD AF            UXN    MEM/NORMAL-TAGGED
0xffff000181c90000-0xffff000181ca0000          64K PTE       RW NX SHD AF    CON     UXN    MEM/NORMAL-TAGGED
0xffff000181ca0000-0xffff000181dbd000        1140K PTE       ro NX SHD AF            UXN    MEM/NORMAL-TAGGED
0xffff000181dbd000-0xffff000181dc0000          12K PTE       RW NX SHD AF            UXN    MEM/NORMAL-TAGGED
0xffff000181dc0000-0xffff000181e00000         256K PTE       RW NX SHD AF    CON     UXN    MEM/NORMAL-TAGGED
0xffff000181e00000-0xffff000182000000           2M PMD       RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED
0xffff000182000000-0xffff0001c0000000         992M PMD       RW NX SHD AF    CON BLK UXN    MEM/NORMAL-TAGGED
0xffff0001c0000000-0xffff000300000000           5G PUD       RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED

0xffff000300000000-0xffff008000000000 500G PUD
0xffff008000000000-0xffff800000000000 130560G PGD
---[ Linear Mapping end ]---

Mode 3:
=======
---[ Linear Mapping start ]---

0xffff000000000000-0xffff000000210000        2112K PTE       RW NX SHD AF            UXN    MEM/NORMAL-TAGGED
0xffff000000210000-0xffff000000400000        1984K PTE       ro NX SHD AF            UXN    MEM/NORMAL
0xffff000000400000-0xffff000002400000          32M PMD       ro NX SHD AF        BLK UXN    MEM/NORMAL
0xffff000002400000-0xffff000002550000        1344K PTE       ro NX SHD AF            UXN    MEM/NORMAL
0xffff000002550000-0xffff000143a61000     5264452K PTE       RW NX SHD AF            UXN    MEM/NORMAL-TAGGED
0xffff000143a61000-0xffff000143c61000           2M PTE       ro NX SHD AF            UXN    MEM/NORMAL-TAGGED
0xffff000143c61000-0xffff000181b9a000     1015012K PTE       RW NX SHD AF            UXN    MEM/NORMAL-TAGGED
0xffff000181b9a000-0xffff000181d9a000           2M PTE       ro NX SHD AF            UXN    MEM/NORMAL-TAGGED
0xffff000181d9a000-0xffff000300000000     6261144K PTE       RW NX SHD AF            UXN    MEM/NORMAL-TAGGED

0xffff000300000000-0xffff008000000000 500G PUD
0xffff008000000000-0xffff800000000000 130560G PGD
---[ Linear Mapping end ]---

Performance Testing
===================
* Memory use after boot
Before:

MemTotal:       258988984 kB
MemFree:        254821700 kB

After:

MemTotal:       259505132 kB
MemFree:        255410264 kB

Around 500MB more memory are free to use. The larger the machine, the more memory saved.

* Memcached
We saw performance degradation when running Memcached benchmark with rodata=full vs rodata=on. Our profiling pointed to kernel TLB pressure. With this patchset we saw ops/sec is increased by around 3.5%, P99 latency is reduced by around 9.6%.
The gain mainly came from reduced kernel TLB misses. The kernel TLB MPKI is reduced by 28.5%.

The benchmark data is now on par with rodata=on too.

* Disk encryption (dm-crypt) benchmark
Ran fio benchmark with the below command on a 128G ramdisk (ext4) with disk encryption (by dm-crypt).

fio --directory=/data --random_generator=lfsr --norandommap            \
    --randrepeat 1 --status-interval=999 --rw=write --bs=4k --loops=1  \
    --ioengine=sync --iodepth=1 --numjobs=1 --fsync_on_close=1         \
    --group_reporting --thread --name=iops-test-job --eta-newline=1    \
    --size 100G

The IOPS is increased by 90% - 150% (the variance is high, but the worst number of good case is around 90% more than the best number of bad case). The bandwidth is increased and the avg clat is reduced proportionally.

* Sequential file read
Read 100G file sequentially on XFS (xfs_io read with page cache populated). The bandwidth is increased by 150%.

Additionally Ryan also ran this through a random selection of benchmarks on AmpereOne. None show any regressions, and various benchmarks show statistically significant improvement. I'm just showing those improvements here:

+----------------------+----------------------------------------------------------+-------------------------+
| Benchmark            | Result Class                                             | Improvement vs 6.17-rc1 |
+======================+==========================================================+=========================+
| micromm/vmalloc      | full_fit_alloc_test: p:1, h:0, l:500000 (usec)           |              (I) -9.00% |
|                      | kvfree_rcu_1_arg_vmalloc_test: p:1, h:0, l:500000 (usec) |              (I) -6.93% |
|                      | kvfree_rcu_2_arg_vmalloc_test: p:1, h:0, l:500000 (usec) |              (I) -6.77% |
|                      | pcpu_alloc_test: p:1, h:0, l:500000 (usec)               |              (I) -4.63% |
+----------------------+----------------------------------------------------------+-------------------------+
| mmtests/hackbench    | process-sockets-30 (seconds)                             |              (I) -2.96% |
+----------------------+----------------------------------------------------------+-------------------------+
| mmtests/kernbench    | syst-192 (seconds)                                       |             (I) -12.77% |
+----------------------+----------------------------------------------------------+-------------------------+
| pts/perl-benchmark   | Test: Interpreter (Seconds)                              |              (I) -4.86% |
+----------------------+----------------------------------------------------------+-------------------------+
| pts/pgbench          | Scale: 1 Clients: 1 Read Write (TPS)                     |               (I) 5.07% |
|                      | Scale: 1 Clients: 1 Read Write - Latency (ms)            |              (I) -4.72% |
|                      | Scale: 100 Clients: 1000 Read Write (TPS)                |               (I) 2.58% |
|                      | Scale: 100 Clients: 1000 Read Write - Latency (ms)       |              (I) -2.52% |
+----------------------+----------------------------------------------------------+-------------------------+
| pts/sqlite-speedtest | Timed Time - Size 1,000 (Seconds)                        |              (I) -2.68% |
+----------------------+----------------------------------------------------------+-------------------------+

Changes since v7 [1]
====================
- Rebased on v6.17-rc6 and Shijie's rodata series (https://git.kernel.org/pub/scm/linux/kernel/git/arm64/linux.git/commit/?id=bfbbb0d3215f)
which has been picked up by Will.
- Patch 1: Fixed pmd_leaf/pud_leaf issue since the code may need to change
permission for invalid entries per Jinjiang Tu.
- Patch 1: Removed pageattr_pgd_entry and pageattr_p4d_entry per Ryan.
- Used (-1ULL) instead of -1 per Catalin.
- Added comment about arm64 lazy mmu allow sleeping per Ryan.
- Squashed patch #4 in v7 into patch #3.
- Squashed patch #6 in v7 into patch #4.
- Added patch #5 to fix a arm64 kprobes bug. It guarantees set_memory_rox()
is called before vfree(). It can go into separately or with this series
together.
- Collected all the R-bs and A-bs.

Changes since v6 [2]
====================
- Patch 1: Minor refactor to implement walk_kernel_page_table_range() in terms of walk_kernel_page_table_range_lockless(). Also lead to adding *pmd argument to the lockless variant for consistency (per Catalin).
- Misc function/variable renames to improve clarity and consistency.
- Share same syncrhonization flag between idmap_kpti_install_ng_mappings and wait_linear_map_split_to_ptes, which allows removal of bbml2_ptes[] to save ~20K from kernel image.
- Only take pgtable_split_lock and enter lazy mmu mode once for both splits.
- Only walk the pgtable once for the common "split single page" case.
- Bypass split to contpmd and contpte when spllitting linear map to ptes.

[1] https://lore.kernel.org/linux-arm-kernel/[email protected]/
[2] https://lore.kernel.org/linux-arm-kernel/[email protected]/

Dev Jain (1):
arm64: Enable permission change on arm64 kernel block mappings

Ryan Roberts (1):
arm64: mm: split linear mapping if BBML2 unsupported on secondary CPUs

Yang Shi (3):
arm64: cpufeature: add AmpereOne to BBML2 allow list
arm64: mm: support large block mapping when rodata=full
arm64: kprobes: call set_memory_rox() for kprobe page

 arch/arm64/include/asm/cpufeature.h |   2 +
 arch/arm64/include/asm/mmu.h        |   3 +
 arch/arm64/include/asm/pgtable.h    |   5 ++
 arch/arm64/kernel/cpufeature.c      |  12 +++-
 arch/arm64/kernel/probes/kprobes.c  |  12 ++++
 arch/arm64/mm/mmu.c                 | 422 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----
 arch/arm64/mm/pageattr.c            | 123 ++++++++++++++++++++++++---------
 arch/arm64/mm/proc.S                |  27 ++++++--
 include/linux/pagewalk.h            |   3 +
 mm/pagewalk.c                       |  36 ++++++----
 10 files changed, 581 insertions(+), 64 deletions(-)
author
#1 lore raw

PATCH v8 1/5 arm64: Enable permission change on arm64 kernel block mappings

From: Dev Jain <[email protected]>

This patch paves the path to enable huge mappings in vmalloc space and linear map space by default on arm64. For this we must ensure that we can handle any permission games on the kernel (init_mm) pagetable. Previously, __change_memory_common() used apply_to_page_range() which does not support changing permissions for block mappings. We move away from this by using the pagewalk API, similar to what riscv does right now. It is the responsibility of the caller to ensure that the range over which permissions are being changed falls on leaf mapping boundaries. For systems with BBML2, this will be handled in future patches by dyanmically splitting the mappings when required.

Unlike apply_to_page_range(), the pagewalk API currently enforces the init_mm.mmap_lock to be held. To avoid the unnecessary bottleneck of the mmap_lock for our usecase, this patch extends this generic API to be used locklessly, so as to retain the existing behaviour for changing permissions. Apart from this reason, it is noted at [1] that KFENCE can manipulate kernel pgtable entries during softirqs. It does this by calling set_memory_valid() -> __change_memory_common(). This being a non-sleepable context, we cannot take the init_mm mmap lock.

Add comments to highlight the conditions under which we can use the lockless variant - no underlying VMA, and the user having exclusive control over the range, thus guaranteeing no concurrent access.

We require that the start and end of a given range do not partially overlap block mappings, or cont mappings. Return -EINVAL in case a partial block mapping is detected in any of the PGD/P4D/PUD/PMD levels; add a corresponding comment in update_range_prot() to warn that eliminating such a condition is the responsibility of the caller.

Note that, the pte level callback may change permissions for a whole contpte block, and that will be done one pte at a time, as opposed to an atomic operation for the block mappings. This is fine as any access will decode either the old or the new permission until the TLBI.

apply_to_page_range() currently performs all pte level callbacks while in lazy mmu mode. Since arm64 can optimize performance by batching barriers when modifying kernel pgtables in lazy mmu mode, we would like to continue to benefit from this optimisation. Unfortunately walk_kernel_page_table_range() does not use lazy mmu mode. However, since the pagewalk framework is not allocating any memory, we can safely bracket the whole operation inside lazy mmu mode ourselves. Therefore, wrap the call to walk_kernel_page_table_range() with the lazy MMU helpers.

---
 arch/arm64/mm/pageattr.c | 119 +++++++++++++++++++++++++++++----------
 include/linux/pagewalk.h |   3 +
 mm/pagewalk.c            |  36 ++++++++----
 3 files changed, 115 insertions(+), 43 deletions(-)
arch/arm64/mm/pageattr.c +88 -31
index 667aff1efe49..c0648764c403 100644
@@ -8,6 +8,7 @@
 #include <linux/mem_encrypt.h>
 #include <linux/sched.h>
 #include <linux/vmalloc.h>
+#include <linux/pagewalk.h>
 
 #include <asm/cacheflush.h>
 #include <asm/pgtable-prot.h>
@@ -20,6 +21,65 @@ struct page_change_data {
 	pgprot_t clear_mask;
 };
 
+static ptdesc_t set_pageattr_masks(ptdesc_t val, struct mm_walk *walk)
+{
+	struct page_change_data *masks = walk->private;
+
+	val &= ~(pgprot_val(masks->clear_mask));
+	val |= (pgprot_val(masks->set_mask));
+
+	return val;
+}
+
+static int pageattr_pud_entry(pud_t *pud, unsigned long addr,
+			      unsigned long next, struct mm_walk *walk)
+{
+	pud_t val = pudp_get(pud);
+
+	if (pud_sect(val)) {
+		if (WARN_ON_ONCE((next - addr) != PUD_SIZE))
+			return -EINVAL;
+		val = __pud(set_pageattr_masks(pud_val(val), walk));
+		set_pud(pud, val);
+		walk->action = ACTION_CONTINUE;
+	}
+
+	return 0;
+}
+
+static int pageattr_pmd_entry(pmd_t *pmd, unsigned long addr,
+			      unsigned long next, struct mm_walk *walk)
+{
+	pmd_t val = pmdp_get(pmd);
+
+	if (pmd_sect(val)) {
+		if (WARN_ON_ONCE((next - addr) != PMD_SIZE))
+			return -EINVAL;
+		val = __pmd(set_pageattr_masks(pmd_val(val), walk));
+		set_pmd(pmd, val);
+		walk->action = ACTION_CONTINUE;
+	}
+
+	return 0;
+}
+
+static int pageattr_pte_entry(pte_t *pte, unsigned long addr,
+			      unsigned long next, struct mm_walk *walk)
+{
+	pte_t val = __ptep_get(pte);
+
+	val = __pte(set_pageattr_masks(pte_val(val), walk));
+	__set_pte(pte, val);
+
+	return 0;
+}
+
+static const struct mm_walk_ops pageattr_ops = {
+	.pud_entry	= pageattr_pud_entry,
+	.pmd_entry	= pageattr_pmd_entry,
+	.pte_entry	= pageattr_pte_entry,
+};
+
 bool rodata_full __ro_after_init = true;
 
 bool can_set_direct_map(void)
@@ -37,32 +97,35 @@ bool can_set_direct_map(void)
 		arm64_kfence_can_set_direct_map() || is_realm_world();
 }
 
-static int change_page_range(pte_t *ptep, unsigned long addr, void *data)
+static int update_range_prot(unsigned long start, unsigned long size,
+			     pgprot_t set_mask, pgprot_t clear_mask)
 {
-	struct page_change_data *cdata = data;
-	pte_t pte = __ptep_get(ptep);
+	struct page_change_data data;
+	int ret;
 
-	pte = clear_pte_bit(pte, cdata->clear_mask);
-	pte = set_pte_bit(pte, cdata->set_mask);
+	data.set_mask = set_mask;
+	data.clear_mask = clear_mask;
 
-	__set_pte(ptep, pte);
-	return 0;
+	arch_enter_lazy_mmu_mode();
+
+	/*
+	 * The caller must ensure that the range we are operating on does not
+	 * partially overlap a block mapping, or a cont mapping. Any such case
+	 * must be eliminated by splitting the mapping.
+	 */
+	ret = walk_kernel_page_table_range_lockless(start, start + size,
+						    &pageattr_ops, NULL, &data);
+	arch_leave_lazy_mmu_mode();
+
+	return ret;
 }
 
-/*
- * This function assumes that the range is mapped with PAGE_SIZE pages.
- */
 static int __change_memory_common(unsigned long start, unsigned long size,
-				pgprot_t set_mask, pgprot_t clear_mask)
+				  pgprot_t set_mask, pgprot_t clear_mask)
 {
-	struct page_change_data data;
 	int ret;
 
-	data.set_mask = set_mask;
-	data.clear_mask = clear_mask;
-
-	ret = apply_to_page_range(&init_mm, start, size, change_page_range,
-					&data);
+	ret = update_range_prot(start, size, set_mask, clear_mask);
 
 	/*
 	 * If the memory is being made valid without changing any other bits
@@ -174,32 +237,26 @@ int set_memory_valid(unsigned long addr, int numpages, int enable)
 
 int set_direct_map_invalid_noflush(struct page *page)
 {
-	struct page_change_data data = {
-		.set_mask = __pgprot(0),
-		.clear_mask = __pgprot(PTE_VALID),
-	};
+	pgprot_t clear_mask = __pgprot(PTE_VALID);
+	pgprot_t set_mask = __pgprot(0);
 
 	if (!can_set_direct_map())
 		return 0;
 
-	return apply_to_page_range(&init_mm,
-				   (unsigned long)page_address(page),
-				   PAGE_SIZE, change_page_range, &data);
+	return update_range_prot((unsigned long)page_address(page),
+				 PAGE_SIZE, set_mask, clear_mask);
 }
 
 int set_direct_map_default_noflush(struct page *page)
 {
-	struct page_change_data data = {
-		.set_mask = __pgprot(PTE_VALID | PTE_WRITE),
-		.clear_mask = __pgprot(PTE_RDONLY),
-	};
+	pgprot_t set_mask = __pgprot(PTE_VALID | PTE_WRITE);
+	pgprot_t clear_mask = __pgprot(PTE_RDONLY);
 
 	if (!can_set_direct_map())
 		return 0;
 
-	return apply_to_page_range(&init_mm,
-				   (unsigned long)page_address(page),
-				   PAGE_SIZE, change_page_range, &data);
+	return update_range_prot((unsigned long)page_address(page),
+				 PAGE_SIZE, set_mask, clear_mask);
 }
 
 static int __set_memory_enc_dec(unsigned long addr,
include/linux/pagewalk.h +3 -0
index 682472c15495..88e18615dd72 100644
@@ -134,6 +134,9 @@ int walk_page_range(struct mm_struct *mm, unsigned long start,
 int walk_kernel_page_table_range(unsigned long start,
 		unsigned long end, const struct mm_walk_ops *ops,
 		pgd_t *pgd, void *private);
+int walk_kernel_page_table_range_lockless(unsigned long start,
+		unsigned long end, const struct mm_walk_ops *ops,
+		pgd_t *pgd, void *private);
 int walk_page_range_vma(struct vm_area_struct *vma, unsigned long start,
 			unsigned long end, const struct mm_walk_ops *ops,
 			void *private);
mm/pagewalk.c +24 -12
index 648038247a8d..936689d8bcac 100644
@@ -606,10 +606,32 @@ int walk_page_range(struct mm_struct *mm, unsigned long start,
 int walk_kernel_page_table_range(unsigned long start, unsigned long end,
 		const struct mm_walk_ops *ops, pgd_t *pgd, void *private)
 {
-	struct mm_struct *mm = &init_mm;
+	/*
+	 * Kernel intermediate page tables are usually not freed, so the mmap
+	 * read lock is sufficient. But there are some exceptions.
+	 * E.g. memory hot-remove. In which case, the mmap lock is insufficient
+	 * to prevent the intermediate kernel pages tables belonging to the
+	 * specified address range from being freed. The caller should take
+	 * other actions to prevent this race.
+	 */
+	mmap_assert_locked(&init_mm);
+
+	return walk_kernel_page_table_range_lockless(start, end, ops, pgd,
+						     private);
+}
+
+/*
+ * Use this function to walk the kernel page tables locklessly. It should be
+ * guaranteed that the caller has exclusive access over the range they are
+ * operating on - that there should be no concurrent access, for example,
+ * changing permissions for vmalloc objects.
+ */
+int walk_kernel_page_table_range_lockless(unsigned long start, unsigned long end,
+		const struct mm_walk_ops *ops, pgd_t *pgd, void *private)
+{
 	struct mm_walk walk = {
 		.ops		= ops,
-		.mm		= mm,
+		.mm		= &init_mm,
 		.pgd		= pgd,
 		.private	= private,
 		.no_vma		= true
@@ -620,16 +642,6 @@ int walk_kernel_page_table_range(unsigned long start, unsigned long end,
 	if (!check_ops_valid(ops))
 		return -EINVAL;
 
-	/*
-	 * Kernel intermediate page tables are usually not freed, so the mmap
-	 * read lock is sufficient. But there are some exceptions.
-	 * E.g. memory hot-remove. In which case, the mmap lock is insufficient
-	 * to prevent the intermediate kernel pages tables belonging to the
-	 * specified address range from being freed. The caller should take
-	 * other actions to prevent this race.
-	 */
-	mmap_assert_locked(mm);
-
 	return walk_pgd_range(start, end, &walk);
 }
 
Signature · 1 line
2.47.0
author
#2 lore raw

PATCH v8 2/5 arm64: cpufeature: add AmpereOne to BBML2 allow list

In reply to Yang Shi

AmpereOne supports BBML2 without conflict abort, add to the allow list.

---
 arch/arm64/kernel/cpufeature.c | 2 ++
 1 file changed, 2 insertions(+)
arch/arm64/kernel/cpufeature.c +2 -0
index ef269a5a37e1..ba07eeff2a8d 100644
@@ -2235,6 +2235,8 @@ static bool has_bbml2_noabort(const struct arm64_cpu_capabilities *caps, int sco
 	static const struct midr_range supports_bbml2_noabort_list[] = {
 		MIDR_REV_RANGE(MIDR_CORTEX_X4, 0, 3, 0xf),
 		MIDR_REV_RANGE(MIDR_NEOVERSE_V3, 0, 2, 0xf),
+		MIDR_ALL_VERSIONS(MIDR_AMPERE1),
+		MIDR_ALL_VERSIONS(MIDR_AMPERE1A),
 		{}
 	};
 
Signature · 1 line
2.47.0
author
#3 lore raw

PATCH v8 3/5 arm64: mm: support large block mapping when rodata=full

In reply to Yang Shi

When rodata=full is specified, kernel linear mapping has to be mapped at PTE level since large page table can't be split due to break-before-make rule on ARM64.

This resulted in a couple of problems:
- performance degradation
- more TLB pressure
- memory waste for kernel page table

With FEAT_BBM level 2 support, splitting large block page table to smaller ones doesn't need to make the page table entry invalid anymore. This allows kernel split large block mapping on the fly.

Add kernel page table split support and use large block mapping by default when FEAT_BBM level 2 is supported for rodata=full. When changing permissions for kernel linear mapping, the page table will be split to smaller size.

The machine without FEAT_BBM level 2 will fallback to have kernel linear mapping PTE-mapped when rodata=full.

With this we saw significant performance boost with some benchmarks and much less memory consumption on my AmpereOne machine (192 cores, 1P) with 256GB memory.

* Memory use after boot
Before:

MemTotal:       258988984 kB
MemFree:        254821700 kB

After:

MemTotal:       259505132 kB
MemFree:        255410264 kB

Around 500MB more memory are free to use. The larger the machine, the more memory saved.

* Memcached
We saw performance degradation when running Memcached benchmark with rodata=full vs rodata=on. Our profiling pointed to kernel TLB pressure. With this patchset we saw ops/sec is increased by around 3.5%, P99 latency is reduced by around 9.6%.
The gain mainly came from reduced kernel TLB misses. The kernel TLB MPKI is reduced by 28.5%.

The benchmark data is now on par with rodata=on too.

* Disk encryption (dm-crypt) benchmark
Ran fio benchmark with the below command on a 128G ramdisk (ext4) with disk encryption (by dm-crypt).

fio --directory=/data --random_generator=lfsr --norandommap            \
    --randrepeat 1 --status-interval=999 --rw=write --bs=4k --loops=1  \
    --ioengine=sync --iodepth=1 --numjobs=1 --fsync_on_close=1         \
    --group_reporting --thread --name=iops-test-job --eta-newline=1    \
    --size 100G

The IOPS is increased by 90% - 150% (the variance is high, but the worst number of good case is around 90% more than the best number of bad case). The bandwidth is increased and the avg clat is reduced proportionally.

* Sequential file read
Read 100G file sequentially on XFS (xfs_io read with page cache populated). The bandwidth is increased by 150%.

---
 arch/arm64/include/asm/cpufeature.h |   2 +
 arch/arm64/include/asm/mmu.h        |   1 +
 arch/arm64/include/asm/pgtable.h    |   5 +
 arch/arm64/kernel/cpufeature.c      |   7 +-
 arch/arm64/mm/mmu.c                 | 264 +++++++++++++++++++++++++++-
 arch/arm64/mm/pageattr.c            |   4 +
 6 files changed, 277 insertions(+), 6 deletions(-)
arch/arm64/include/asm/cpufeature.h +2 -0
index bf13d676aae2..e223cbf350e4 100644
@@ -871,6 +871,8 @@ static inline bool system_supports_pmuv3(void)
 	return cpus_have_final_cap(ARM64_HAS_PMUV3);
 }
 
+bool cpu_supports_bbml2_noabort(void);
+
 static inline bool system_supports_bbml2_noabort(void)
 {
 	return alternative_has_cap_unlikely(ARM64_HAS_BBML2_NOABORT);
arch/arm64/include/asm/mmu.h +1 -0
index 49f1a810df16..a7cc95d97ceb 100644
@@ -78,6 +78,7 @@ extern void create_pgd_mapping(struct mm_struct *mm, phys_addr_t phys,
 			       pgprot_t prot, bool page_mappings_only);
 extern void *fixmap_remap_fdt(phys_addr_t dt_phys, int *size, pgprot_t prot);
 extern void mark_linear_text_alias_ro(void);
+extern int split_kernel_leaf_mapping(unsigned long start, unsigned long end);
 
 /*
  * This check is triggered during the early boot before the cpufeature
arch/arm64/include/asm/pgtable.h +5 -0
index abd2dee416b3..aa89c2e67ebc 100644
@@ -371,6 +371,11 @@ static inline pmd_t pmd_mkcont(pmd_t pmd)
 	return __pmd(pmd_val(pmd) | PMD_SECT_CONT);
 }
 
+static inline pmd_t pmd_mknoncont(pmd_t pmd)
+{
+	return __pmd(pmd_val(pmd) & ~PMD_SECT_CONT);
+}
+
 #ifdef CONFIG_HAVE_ARCH_USERFAULTFD_WP
 static inline int pte_uffd_wp(pte_t pte)
 {
arch/arm64/kernel/cpufeature.c +6 -1
index ba07eeff2a8d..7dc092e33fcc 100644
@@ -2218,7 +2218,7 @@ static bool hvhe_possible(const struct arm64_cpu_capabilities *entry,
 	return arm64_test_sw_feature_override(ARM64_SW_FEATURE_OVERRIDE_HVHE);
 }
 
-static bool has_bbml2_noabort(const struct arm64_cpu_capabilities *caps, int scope)
+bool cpu_supports_bbml2_noabort(void)
 {
 	/*
 	 * We want to allow usage of BBML2 in as wide a range of kernel contexts
@@ -2252,6 +2252,11 @@ static bool has_bbml2_noabort(const struct arm64_cpu_capabilities *caps, int sco
 	return true;
 }
 
+static bool has_bbml2_noabort(const struct arm64_cpu_capabilities *caps, int scope)
+{
+	return cpu_supports_bbml2_noabort();
+}
+
 #ifdef CONFIG_ARM64_PAN
 static void cpu_enable_pan(const struct arm64_cpu_capabilities *__unused)
 {
arch/arm64/mm/mmu.c +259 -5

308 lines not on this page · raw message

arch/arm64/mm/pageattr.c +4 -0
index c0648764c403..5135f2d66958 100644
@@ -106,6 +106,10 @@ static int update_range_prot(unsigned long start, unsigned long size,
 	data.set_mask = set_mask;
 	data.clear_mask = clear_mask;
 
+	ret = split_kernel_leaf_mapping(start, start + size);
+	if (WARN_ON_ONCE(ret))
+		return ret;
+
 	arch_enter_lazy_mmu_mode();
 
 	/*
Signature · 1 line
2.47.0
#4 lore raw

Re: [PATCH v8 3/5] arm64: mm: support large block mapping when rodata=full

Hi,

Quoted 56 lines · Yang Shi

When rodata=full is specified, kernel linear mapping has to be mapped at PTE level since large page table can't be split due to break-before-make rule on ARM64.

This resulted in a couple of problems:
- performance degradation
- more TLB pressure
- memory waste for kernel page table

With FEAT_BBM level 2 support, splitting large block page table to smaller ones doesn't need to make the page table entry invalid anymore. This allows kernel split large block mapping on the fly.

Add kernel page table split support and use large block mapping by default when FEAT_BBM level 2 is supported for rodata=full. When changing permissions for kernel linear mapping, the page table will be split to smaller size.

The machine without FEAT_BBM level 2 will fallback to have kernel linear mapping PTE-mapped when rodata=full.

With this we saw significant performance boost with some benchmarks and much less memory consumption on my AmpereOne machine (192 cores, 1P) with 256GB memory.

* Memory use after boot
Before:

MemTotal:       258988984 kB
MemFree:        254821700 kB

After:

MemTotal:       259505132 kB
MemFree:        255410264 kB

Around 500MB more memory are free to use. The larger the machine, the more memory saved.

* Memcached
We saw performance degradation when running Memcached benchmark with rodata=full vs rodata=on. Our profiling pointed to kernel TLB pressure. With this patchset we saw ops/sec is increased by around 3.5%, P99 latency is reduced by around 9.6%.
The gain mainly came from reduced kernel TLB misses. The kernel TLB MPKI is reduced by 28.5%.

The benchmark data is now on par with rodata=on too.

* Disk encryption (dm-crypt) benchmark
Ran fio benchmark with the below command on a 128G ramdisk (ext4) with disk encryption (by dm-crypt).

fio --directory=/data --random_generator=lfsr --norandommap            \
    --randrepeat 1 --status-interval=999 --rw=write --bs=4k --loops=1  \
    --ioengine=sync --iodepth=1 --numjobs=1 --fsync_on_close=1         \
    --group_reporting --thread --name=iops-test-job --eta-newline=1    \
    --size 100G
Quoted 9 lines

The IOPS is increased by 90% - 150% (the variance is high, but the worst number of good case is around 90% more than the best number of bad case). The bandwidth is increased and the avg clat is reduced proportionally.

* Sequential file read
Read 100G file sequentially on XFS (xfs_io read with page cache populated). The bandwidth is increased by 150%.

With lock debugging enabled, we see a large number of "BUG: sleeping function called from invalid context at kernel/locking/mutex.c:580" and "BUG: Invalid wait context:" backtraces when running v6.18-rc3. Please see example below.

Bisect points to this patch.

Please let me know if there is anything I can do to help tracking down the problem.

Thanks,
Guenter

---
Example log:

[    0.537499] BUG: sleeping function called from invalid context at kernel/locking/mutex.c:580
[    0.537501] in_atomic(): 1, irqs_disabled(): 0, non_block: 0, pid: 1, name: swapper/0
[    0.537502] preempt_count: 1, expected: 0
[    0.537504] 2 locks held by swapper/0/1:
[    0.537505]  #0: ffffb60b01211960 (sched_domains_mutex){+.+.}-{4:4}, at: sched_domains_mutex_lock+0x24/0x38
[    0.537510]  #1: ffffb60b01595838 (rcu_read_lock){....}-{1:3}, at: rcu_lock_acquire+0x0/0x40
[    0.537516] CPU: 0 UID: 0 PID: 1 Comm: swapper/0 Not tainted 6.18.0-dbg-DEV #1 NONE
[    0.537517] Call trace:
[    0.537518]  show_stack+0x20/0x38 (C)
[    0.537520]  __dump_stack+0x28/0x38
[    0.537522]  dump_stack_lvl+0xac/0xf0
[    0.537525]  dump_stack+0x18/0x3c
[    0.537527]  __might_resched+0x248/0x2a0
[    0.537529]  __might_sleep+0x40/0x90
[    0.537531]  __mutex_lock_common+0x70/0x1818
[    0.537533]  mutex_lock_nested+0x34/0x48
[    0.537534]  split_kernel_leaf_mapping+0x74/0x1a0
[    0.537536]  update_range_prot+0x40/0x150
[    0.537537]  __change_memory_common+0x30/0x148
[    0.537538]  __kernel_map_pages+0x70/0x88
[    0.537540]  __free_frozen_pages+0x6e4/0x7b8
[    0.537542]  free_frozen_pages+0x1c/0x30
[    0.537544]  __free_slab+0xf0/0x168
[    0.537547]  free_slab+0x2c/0xf8
[    0.537549]  free_to_partial_list+0x4e0/0x620
[    0.537551]  __slab_free+0x228/0x250
[    0.537553]  kfree+0x3c4/0x4c0
[    0.537555]  destroy_sched_domain+0xf8/0x140
[    0.537557]  cpu_attach_domain+0x17c/0x610
[    0.537558]  build_sched_domains+0x15a4/0x1718
[    0.537560]  sched_init_domains+0xbc/0xf8
[    0.537561]  sched_init_smp+0x30/0x98
[    0.537562]  kernel_init_freeable+0x148/0x230
[    0.537564]  kernel_init+0x28/0x148
[    0.537566]  ret_from_fork+0x10/0x20
[    0.537569] =============================
[    0.537569] [ BUG: Invalid wait context ]
[    0.537571] 6.18.0-dbg-DEV #1 Tainted: G        W
[    0.537572] -----------------------------
[    0.537572] swapper/0/1 is trying to lock:
[    0.537573] ffffb60b011f3830 (pgtable_split_lock){+.+.}-{4:4}, at: split_kernel_leaf_mapping+0x74/0x1a0
[    0.537576] other info that might help us debug this:
[    0.537577] context-{5:5}
[    0.537578] 2 locks held by swapper/0/1:
[    0.537579]  #0: ffffb60b01211960 (sched_domains_mutex){+.+.}-{4:4}, at: sched_domains_mutex_lock+0x24/0x38
[    0.537582]  #1: ffffb60b01595838 (rcu_read_lock){....}-{1:3}, at: rcu_lock_acquire+0x0/0x40
[    0.537585] stack backtrace:
[    0.537585] CPU: 0 UID: 0 PID: 1 Comm: swapper/0 Tainted: G        W           6.18.0-dbg-DEV #1 NONE
[    0.537587] Tainted: [W]=WARN
[    0.537588] Call trace:
[    0.537589]  show_stack+0x20/0x38 (C)
[    0.537591]  __dump_stack+0x28/0x38
[    0.537593]  dump_stack_lvl+0xac/0xf0
[    0.537596]  dump_stack+0x18/0x3c
[    0.537598]  __lock_acquire+0x980/0x2a20
[    0.537600]  lock_acquire+0x124/0x2b8
[    0.537602]  __mutex_lock_common+0xd8/0x1818
[    0.537604]  mutex_lock_nested+0x34/0x48
[    0.537605]  split_kernel_leaf_mapping+0x74/0x1a0
[    0.537607]  update_range_prot+0x40/0x150
[    0.537608]  __change_memory_common+0x30/0x148
[    0.537609]  __kernel_map_pages+0x70/0x88
[    0.537610]  __free_frozen_pages+0x6e4/0x7b8
[    0.537613]  free_frozen_pages+0x1c/0x30
[    0.537615]  __free_slab+0xf0/0x168
[    0.537617]  free_slab+0x2c/0xf8
[    0.537619]  free_to_partial_list+0x4e0/0x620
[    0.537621]  __slab_free+0x228/0x250
[    0.537623]  kfree+0x3c4/0x4c0
[    0.537625]  destroy_sched_domain+0xf8/0x140
[    0.537627]  cpu_attach_domain+0x17c/0x610
[    0.537628]  build_sched_domains+0x15a4/0x1718
[    0.537630]  sched_init_domains+0xbc/0xf8
[    0.537631]  sched_init_smp+0x30/0x98
[    0.537632]  kernel_init_freeable+0x148/0x230
[    0.537633]  kernel_init+0x28/0x148
[    0.537635]  ret_from_fork+0x10/0x20

---
bisect:

# bad: [3a8660878839faadb4f1a6dd72c3179c1df56787] Linux 6.18-rc1
# good: [e5f0a698b34ed76002dc5cff3804a61c80233a7a] Linux 6.17
git bisect start 'v6.18-rc1' 'v6.17'
# bad: [58809f614e0e3f4e12b489bddf680bfeb31c0a20] Merge tag 'drm-next-2025-10-01' of https://gitlab.freedesktop.org/drm/kernel
git bisect bad 58809f614e0e3f4e12b489bddf680bfeb31c0a20
# bad: [a8253f807760e9c80eada9e5354e1240ccf325f9] Merge tag 'soc-newsoc-6.18' of git://git.kernel.org/pub/scm/linux/kernel/git/soc/soc
git bisect bad a8253f807760e9c80eada9e5354e1240ccf325f9
# bad: [4b81e2eb9e4db8f6094c077d0c8b27c264901c1b] Merge tag 'timers-vdso-2025-09-29' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
git bisect bad 4b81e2eb9e4db8f6094c077d0c8b27c264901c1b
# bad: [f1004b2f19d7e9add9d707f64d9fcbc50f67921b] Merge tag 'm68k-for-v6.18-tag1' of git://git.kernel.org/pub/scm/linux/kernel/git/geert/linux-m68k
git bisect bad f1004b2f19d7e9add9d707f64d9fcbc50f67921b
# good: [a9401710a5f5681abd2a6f21f9e76bc9f2e81891] Merge tag 'v6.18-rc-part1-smb3-common' of git://git.samba.org/ksmbd
git bisect good a9401710a5f5681abd2a6f21f9e76bc9f2e81891
# good: [fe68bb2861808ed5c48d399bd7e670ab76829d55] Merge tag 'microblaze-v6.18' of git://git.monstr.eu/linux-2.6-microblaze
git bisect good fe68bb2861808ed5c48d399bd7e670ab76829d55
# bad: [f2d64a22faeeecff385b4c91fab5fe036ab00162] Merge branch 'for-next/perf' into for-next/core
git bisect bad f2d64a22faeeecff385b4c91fab5fe036ab00162
# good: [30f9386820cddbba59b48ae0670c3a1646dd440e] Merge branch 'for-next/misc' into for-next/core
git bisect good 30f9386820cddbba59b48ae0670c3a1646dd440e
# good: [43de0ac332b815cf56dbdce63687de9acfd35d49] drivers/perf: hisi: Relax the event ID check in the framework
git bisect good 43de0ac332b815cf56dbdce63687de9acfd35d49
# good: [5973a62efa34c80c9a4e5eac1fca6f6209b902af] arm64: map [_text, _stext) virtual address range non-executable+read-only git bisect good 5973a62efa34c80c9a4e5eac1fca6f6209b902af
# good: [b3abb08d6f628a76c36bf7da9508e1a67bf186a0] drivers/perf: hisi: Refactor the event configuration of L3C PMU
git bisect good b3abb08d6f628a76c36bf7da9508e1a67bf186a0
# good: [6d2f913fda5683fbd4c3580262e10386c1263dfb] Documentation: hisi-pmu: Add introduction to HiSilicon V3 PMU
git bisect good 6d2f913fda5683fbd4c3580262e10386c1263dfb
# good: [2084660ad288c998b6f0c885e266deb364f65fba] perf/dwc_pcie: Fix use of uninitialized variable
git bisect good 2084660ad288c998b6f0c885e266deb364f65fba
# bad: [77dfca70baefcb988318a72fe69eb99f6dabbbb1] Merge branch 'for-next/mm' into for-next/core
git bisect bad 77dfca70baefcb988318a72fe69eb99f6dabbbb1

# first bad commit: [77dfca70baefcb988318a72fe69eb99f6dabbbb1] Merge branch 'for-next/mm' into for-next/core

---
bisect into branch:

- git checkout -b testing 77dfca70baefcb988318a72fe69eb99f6dabbbb1
- git rebase 77dfca70baefcb988318a72fe69eb99f6dabbbb1~1
[ fix minor conflict similar to the conflict resolution in 77dfca70baefc]
- git diff 77dfca70baefcb988318a72fe69eb99f6dabbbb1
[ confirmed that there are no differences ]
- confirm that the problem is still seen at the tip of the rebase
- git bisect start HEAD 77dfca70baefcb988318a72fe69eb99f6dabbbb1~1
- run bisect

Results:

# bad: [47fc25df1ae3ae8412f1b812fb586c714d04a5e6] arm64: map [_text, _stext) virtual address range non-executable+read-only # good: [30f9386820cddbba59b48ae0670c3a1646dd440e] Merge branch 'for-next/misc' into for-next/core
git bisect start 'HEAD' '77dfca70baefcb988318a72fe69eb99f6dabbbb1~1'
# good: [805491d19fc21271b5c27f4602f8f66b625c110f] arm64/Kconfig: Remove CONFIG_RODATA_FULL_DEFAULT_ENABLED git bisect good 805491d19fc21271b5c27f4602f8f66b625c110f
# bad: [13c7d7426232cc4489df7cd2e1f646a22d3f6172] arm64: mm: support large block mapping when rodata=full git bisect bad 13c7d7426232cc4489df7cd2e1f646a22d3f6172
# good: [a4d9c67e503f2b73c2d89d8e8209dfd241bdc8d8] arm64: Enable permission change on arm64 kernel block mappings git bisect good a4d9c67e503f2b73c2d89d8e8209dfd241bdc8d8

# first bad commit: [13c7d7426232cc4489df7cd2e1f646a22d3f6172] arm64: mm: support large block mapping when rodata=full
#5 lore raw

Re: [PATCH v8 3/5] arm64: mm: support large block mapping when rodata=full

Quoted 74 lines · Guenter Roeck

Hi,

Quoted 56 lines · Yang Shi

When rodata=full is specified, kernel linear mapping has to be mapped at PTE level since large page table can't be split due to break-before-make rule on ARM64.

This resulted in a couple of problems:
- performance degradation
- more TLB pressure
- memory waste for kernel page table

With FEAT_BBM level 2 support, splitting large block page table to smaller ones doesn't need to make the page table entry invalid anymore. This allows kernel split large block mapping on the fly.

Add kernel page table split support and use large block mapping by default when FEAT_BBM level 2 is supported for rodata=full. When changing permissions for kernel linear mapping, the page table will be split to smaller size.

The machine without FEAT_BBM level 2 will fallback to have kernel linear mapping PTE-mapped when rodata=full.

With this we saw significant performance boost with some benchmarks and much less memory consumption on my AmpereOne machine (192 cores, 1P) with 256GB memory.

* Memory use after boot
Before:

MemTotal:       258988984 kB
MemFree:        254821700 kB

After:

MemTotal:       259505132 kB
MemFree:        255410264 kB

Around 500MB more memory are free to use. The larger the machine, the more memory saved.

* Memcached
We saw performance degradation when running Memcached benchmark with rodata=full vs rodata=on. Our profiling pointed to kernel TLB pressure. With this patchset we saw ops/sec is increased by around 3.5%, P99 latency is reduced by around 9.6%.
The gain mainly came from reduced kernel TLB misses. The kernel TLB MPKI is reduced by 28.5%.

The benchmark data is now on par with rodata=on too.

* Disk encryption (dm-crypt) benchmark
Ran fio benchmark with the below command on a 128G ramdisk (ext4) with disk encryption (by dm-crypt).

fio --directory=/data --random_generator=lfsr --norandommap            \
    --randrepeat 1 --status-interval=999 --rw=write --bs=4k --loops=1  \
    --ioengine=sync --iodepth=1 --numjobs=1 --fsync_on_close=1         \
    --group_reporting --thread --name=iops-test-job --eta-newline=1    \
    --size 100G
Quoted 9 lines

The IOPS is increased by 90% - 150% (the variance is high, but the worst number of good case is around 90% more than the best number of bad case). The bandwidth is increased and the avg clat is reduced proportionally.

* Sequential file read
Read 100G file sequentially on XFS (xfs_io read with page cache populated). The bandwidth is increased by 150%.

With lock debugging enabled, we see a large number of "BUG: sleeping function called from invalid context at kernel/locking/mutex.c:580" and "BUG: Invalid wait context:" backtraces when running v6.18-rc3. Please see example below.

Quoted 4 lines

Bisect points to this patch.

Please let me know if there is anything I can do to help tracking down the problem.

Thanks for the report - ouch!

I expect you're running on a system that supports BBML2_NOABORT, based on the stack trace, I expect you have CONFIG_DEBUG_PAGEALLOC enabled? That will cause permission tricks to be played on the linear map at page allocation and free time, which can happen in non-sleepable contexts. And with this patch we are taking pgtable_split_lock (a mutex) in split_kernel_leaf_mapping(), which is called as a result of the permission change request.

However, when CONFIG_DEBUG_PAGEALLOC enabled we always force-map the linear map by PTE so split_kernel_leaf_mapping() is actually unneccessary and will return without actually having to split anything. So we could add an early "if (force_pte_mapping()) return 0;" to bypass the function entirely in this case, and I *think* that should solve it.

But I'm also concerned about KFENCE. I can't remember it's exact semantics off the top of my head, so I'm concerned we could see similar problems there (where we only force pte mapping for the KFENCE pool).

I'll investigate fully tomorrow and hopefully provide a fix.

Yang Shi, Do you have any additional thoughts?

Thanks,
Ryan

Quoted 145 lines

Thanks,
Guenter

---
Example log:

[    0.537499] BUG: sleeping function called from invalid context at kernel/locking/mutex.c:580
[    0.537501] in_atomic(): 1, irqs_disabled(): 0, non_block: 0, pid: 1, name: swapper/0
[    0.537502] preempt_count: 1, expected: 0
[    0.537504] 2 locks held by swapper/0/1:
[    0.537505]  #0: ffffb60b01211960 (sched_domains_mutex){+.+.}-{4:4}, at: sched_domains_mutex_lock+0x24/0x38
[    0.537510]  #1: ffffb60b01595838 (rcu_read_lock){....}-{1:3}, at: rcu_lock_acquire+0x0/0x40
[    0.537516] CPU: 0 UID: 0 PID: 1 Comm: swapper/0 Not tainted 6.18.0-dbg-DEV #1 NONE
[    0.537517] Call trace:
[    0.537518]  show_stack+0x20/0x38 (C)
[    0.537520]  __dump_stack+0x28/0x38
[    0.537522]  dump_stack_lvl+0xac/0xf0
[    0.537525]  dump_stack+0x18/0x3c
[    0.537527]  __might_resched+0x248/0x2a0
[    0.537529]  __might_sleep+0x40/0x90
[    0.537531]  __mutex_lock_common+0x70/0x1818
[    0.537533]  mutex_lock_nested+0x34/0x48
[    0.537534]  split_kernel_leaf_mapping+0x74/0x1a0
[    0.537536]  update_range_prot+0x40/0x150
[    0.537537]  __change_memory_common+0x30/0x148
[    0.537538]  __kernel_map_pages+0x70/0x88
[    0.537540]  __free_frozen_pages+0x6e4/0x7b8
[    0.537542]  free_frozen_pages+0x1c/0x30
[    0.537544]  __free_slab+0xf0/0x168
[    0.537547]  free_slab+0x2c/0xf8
[    0.537549]  free_to_partial_list+0x4e0/0x620
[    0.537551]  __slab_free+0x228/0x250
[    0.537553]  kfree+0x3c4/0x4c0
[    0.537555]  destroy_sched_domain+0xf8/0x140
[    0.537557]  cpu_attach_domain+0x17c/0x610
[    0.537558]  build_sched_domains+0x15a4/0x1718
[    0.537560]  sched_init_domains+0xbc/0xf8
[    0.537561]  sched_init_smp+0x30/0x98
[    0.537562]  kernel_init_freeable+0x148/0x230
[    0.537564]  kernel_init+0x28/0x148
[    0.537566]  ret_from_fork+0x10/0x20
[    0.537569] =============================
[    0.537569] [ BUG: Invalid wait context ]
[    0.537571] 6.18.0-dbg-DEV #1 Tainted: G        W
[    0.537572] -----------------------------
[    0.537572] swapper/0/1 is trying to lock:
[    0.537573] ffffb60b011f3830 (pgtable_split_lock){+.+.}-{4:4}, at: split_kernel_leaf_mapping+0x74/0x1a0
[    0.537576] other info that might help us debug this:
[    0.537577] context-{5:5}
[    0.537578] 2 locks held by swapper/0/1:
[    0.537579]  #0: ffffb60b01211960 (sched_domains_mutex){+.+.}-{4:4}, at: sched_domains_mutex_lock+0x24/0x38
[    0.537582]  #1: ffffb60b01595838 (rcu_read_lock){....}-{1:3}, at: rcu_lock_acquire+0x0/0x40
[    0.537585] stack backtrace:
[    0.537585] CPU: 0 UID: 0 PID: 1 Comm: swapper/0 Tainted: G        W           6.18.0-dbg-DEV #1 NONE
[    0.537587] Tainted: [W]=WARN
[    0.537588] Call trace:
[    0.537589]  show_stack+0x20/0x38 (C)
[    0.537591]  __dump_stack+0x28/0x38
[    0.537593]  dump_stack_lvl+0xac/0xf0
[    0.537596]  dump_stack+0x18/0x3c
[    0.537598]  __lock_acquire+0x980/0x2a20
[    0.537600]  lock_acquire+0x124/0x2b8
[    0.537602]  __mutex_lock_common+0xd8/0x1818
[    0.537604]  mutex_lock_nested+0x34/0x48
[    0.537605]  split_kernel_leaf_mapping+0x74/0x1a0
[    0.537607]  update_range_prot+0x40/0x150
[    0.537608]  __change_memory_common+0x30/0x148
[    0.537609]  __kernel_map_pages+0x70/0x88
[    0.537610]  __free_frozen_pages+0x6e4/0x7b8
[    0.537613]  free_frozen_pages+0x1c/0x30
[    0.537615]  __free_slab+0xf0/0x168
[    0.537617]  free_slab+0x2c/0xf8
[    0.537619]  free_to_partial_list+0x4e0/0x620
[    0.537621]  __slab_free+0x228/0x250
[    0.537623]  kfree+0x3c4/0x4c0
[    0.537625]  destroy_sched_domain+0xf8/0x140
[    0.537627]  cpu_attach_domain+0x17c/0x610
[    0.537628]  build_sched_domains+0x15a4/0x1718
[    0.537630]  sched_init_domains+0xbc/0xf8
[    0.537631]  sched_init_smp+0x30/0x98
[    0.537632]  kernel_init_freeable+0x148/0x230
[    0.537633]  kernel_init+0x28/0x148
[    0.537635]  ret_from_fork+0x10/0x20

---
bisect:

# bad: [3a8660878839faadb4f1a6dd72c3179c1df56787] Linux 6.18-rc1
# good: [e5f0a698b34ed76002dc5cff3804a61c80233a7a] Linux 6.17
git bisect start 'v6.18-rc1' 'v6.17'
# bad: [58809f614e0e3f4e12b489bddf680bfeb31c0a20] Merge tag 'drm-next-2025-10-01' of https://gitlab.freedesktop.org/drm/kernel
git bisect bad 58809f614e0e3f4e12b489bddf680bfeb31c0a20
# bad: [a8253f807760e9c80eada9e5354e1240ccf325f9] Merge tag 'soc-newsoc-6.18' of git://git.kernel.org/pub/scm/linux/kernel/git/soc/soc
git bisect bad a8253f807760e9c80eada9e5354e1240ccf325f9
# bad: [4b81e2eb9e4db8f6094c077d0c8b27c264901c1b] Merge tag 'timers-vdso-2025-09-29' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
git bisect bad 4b81e2eb9e4db8f6094c077d0c8b27c264901c1b
# bad: [f1004b2f19d7e9add9d707f64d9fcbc50f67921b] Merge tag 'm68k-for-v6.18-tag1' of git://git.kernel.org/pub/scm/linux/kernel/git/geert/linux-m68k
git bisect bad f1004b2f19d7e9add9d707f64d9fcbc50f67921b
# good: [a9401710a5f5681abd2a6f21f9e76bc9f2e81891] Merge tag 'v6.18-rc-part1-smb3-common' of git://git.samba.org/ksmbd
git bisect good a9401710a5f5681abd2a6f21f9e76bc9f2e81891
# good: [fe68bb2861808ed5c48d399bd7e670ab76829d55] Merge tag 'microblaze-v6.18' of git://git.monstr.eu/linux-2.6-microblaze
git bisect good fe68bb2861808ed5c48d399bd7e670ab76829d55
# bad: [f2d64a22faeeecff385b4c91fab5fe036ab00162] Merge branch 'for-next/perf' into for-next/core
git bisect bad f2d64a22faeeecff385b4c91fab5fe036ab00162
# good: [30f9386820cddbba59b48ae0670c3a1646dd440e] Merge branch 'for-next/misc' into for-next/core
git bisect good 30f9386820cddbba59b48ae0670c3a1646dd440e
# good: [43de0ac332b815cf56dbdce63687de9acfd35d49] drivers/perf: hisi: Relax the event ID check in the framework
git bisect good 43de0ac332b815cf56dbdce63687de9acfd35d49
# good: [5973a62efa34c80c9a4e5eac1fca6f6209b902af] arm64: map [_text, _stext) virtual address range non-executable+read-only git bisect good 5973a62efa34c80c9a4e5eac1fca6f6209b902af
# good: [b3abb08d6f628a76c36bf7da9508e1a67bf186a0] drivers/perf: hisi: Refactor the event configuration of L3C PMU
git bisect good b3abb08d6f628a76c36bf7da9508e1a67bf186a0
# good: [6d2f913fda5683fbd4c3580262e10386c1263dfb] Documentation: hisi-pmu: Add introduction to HiSilicon V3 PMU
git bisect good 6d2f913fda5683fbd4c3580262e10386c1263dfb
# good: [2084660ad288c998b6f0c885e266deb364f65fba] perf/dwc_pcie: Fix use of uninitialized variable
git bisect good 2084660ad288c998b6f0c885e266deb364f65fba
# bad: [77dfca70baefcb988318a72fe69eb99f6dabbbb1] Merge branch 'for-next/mm' into for-next/core
git bisect bad 77dfca70baefcb988318a72fe69eb99f6dabbbb1

# first bad commit: [77dfca70baefcb988318a72fe69eb99f6dabbbb1] Merge branch 'for-next/mm' into for-next/core

---
bisect into branch:

- git checkout -b testing 77dfca70baefcb988318a72fe69eb99f6dabbbb1
- git rebase 77dfca70baefcb988318a72fe69eb99f6dabbbb1~1
[ fix minor conflict similar to the conflict resolution in 77dfca70baefc]
- git diff 77dfca70baefcb988318a72fe69eb99f6dabbbb1
[ confirmed that there are no differences ]
- confirm that the problem is still seen at the tip of the rebase
- git bisect start HEAD 77dfca70baefcb988318a72fe69eb99f6dabbbb1~1
- run bisect

Results:

# bad: [47fc25df1ae3ae8412f1b812fb586c714d04a5e6] arm64: map [_text, _stext) virtual address range non-executable+read-only # good: [30f9386820cddbba59b48ae0670c3a1646dd440e] Merge branch 'for-next/misc' into for-next/core
git bisect start 'HEAD' '77dfca70baefcb988318a72fe69eb99f6dabbbb1~1'
# good: [805491d19fc21271b5c27f4602f8f66b625c110f] arm64/Kconfig: Remove CONFIG_RODATA_FULL_DEFAULT_ENABLED git bisect good 805491d19fc21271b5c27f4602f8f66b625c110f
# bad: [13c7d7426232cc4489df7cd2e1f646a22d3f6172] arm64: mm: support large block mapping when rodata=full git bisect bad 13c7d7426232cc4489df7cd2e1f646a22d3f6172
# good: [a4d9c67e503f2b73c2d89d8e8209dfd241bdc8d8] arm64: Enable permission change on arm64 kernel block mappings git bisect good a4d9c67e503f2b73c2d89d8e8209dfd241bdc8d8

# first bad commit: [13c7d7426232cc4489df7cd2e1f646a22d3f6172] arm64: mm: support large block mapping when rodata=full
#6 lore raw

Re: [PATCH v8 3/5] arm64: mm: support large block mapping when rodata=full

Quoted 95 lines · Ryan Roberts
Quoted 74 lines · Guenter Roeck

Hi,

Quoted 56 lines · Yang Shi

When rodata=full is specified, kernel linear mapping has to be mapped at PTE level since large page table can't be split due to break-before-make rule on ARM64.

This resulted in a couple of problems:
- performance degradation
- more TLB pressure
- memory waste for kernel page table

With FEAT_BBM level 2 support, splitting large block page table to smaller ones doesn't need to make the page table entry invalid anymore. This allows kernel split large block mapping on the fly.

Add kernel page table split support and use large block mapping by default when FEAT_BBM level 2 is supported for rodata=full. When changing permissions for kernel linear mapping, the page table will be split to smaller size.

The machine without FEAT_BBM level 2 will fallback to have kernel linear mapping PTE-mapped when rodata=full.

With this we saw significant performance boost with some benchmarks and much less memory consumption on my AmpereOne machine (192 cores, 1P) with 256GB memory.

* Memory use after boot
Before:

MemTotal:       258988984 kB
MemFree:        254821700 kB

After:

MemTotal:       259505132 kB
MemFree:        255410264 kB

Around 500MB more memory are free to use. The larger the machine, the more memory saved.

* Memcached
We saw performance degradation when running Memcached benchmark with rodata=full vs rodata=on. Our profiling pointed to kernel TLB pressure. With this patchset we saw ops/sec is increased by around 3.5%, P99 latency is reduced by around 9.6%.
The gain mainly came from reduced kernel TLB misses. The kernel TLB MPKI is reduced by 28.5%.

The benchmark data is now on par with rodata=on too.

* Disk encryption (dm-crypt) benchmark
Ran fio benchmark with the below command on a 128G ramdisk (ext4) with disk encryption (by dm-crypt).

fio --directory=/data --random_generator=lfsr --norandommap            \
    --randrepeat 1 --status-interval=999 --rw=write --bs=4k --loops=1  \
    --ioengine=sync --iodepth=1 --numjobs=1 --fsync_on_close=1         \
    --group_reporting --thread --name=iops-test-job --eta-newline=1    \
    --size 100G
Quoted 9 lines

The IOPS is increased by 90% - 150% (the variance is high, but the worst number of good case is around 90% more than the best number of bad case). The bandwidth is increased and the avg clat is reduced proportionally.

* Sequential file read
Read 100G file sequentially on XFS (xfs_io read with page cache populated). The bandwidth is increased by 150%.

With lock debugging enabled, we see a large number of "BUG: sleeping function called from invalid context at kernel/locking/mutex.c:580" and "BUG: Invalid wait context:" backtraces when running v6.18-rc3. Please see example below.

Quoted 4 lines

Bisect points to this patch.

Please let me know if there is anything I can do to help tracking down the problem.

Thanks for the report - ouch!

I expect you're running on a system that supports BBML2_NOABORT, based on the stack trace, I expect you have CONFIG_DEBUG_PAGEALLOC enabled? That will cause permission tricks to be played on the linear map at page allocation and free time, which can happen in non-sleepable contexts. And with this patch we are taking pgtable_split_lock (a mutex) in split_kernel_leaf_mapping(), which is called as a result of the permission change request.

However, when CONFIG_DEBUG_PAGEALLOC enabled we always force-map the linear map by PTE so split_kernel_leaf_mapping() is actually unneccessary and will return without actually having to split anything. So we could add an early "if (force_pte_mapping()) return 0;" to bypass the function entirely in this case, and I *think* that should solve it.

Quoted 5 lines

But I'm also concerned about KFENCE. I can't remember it's exact semantics off the top of my head, so I'm concerned we could see similar problems there (where we only force pte mapping for the KFENCE pool).

I'll investigate fully tomorrow and hopefully provide a fix.

Here's a proposed fix, although I can't get access to a system with BBML2 until tomorrow at the earliest. Guenter, I wonder if you could check that this resolves your issue?

---8<---
commit 602ec2db74e5abfb058bd03934475ead8558eb72

Author: Ryan Roberts <[email protected]>
Date:   Sun Nov 2 11:45:18 2025 +0000

arm64: mm: Don't attempt to split known pte-mapped regions

It has been reported that split_kernel_leaf_mapping() is trying to sleep in non-sleepable context. It does this when acquiring the pgtable_split_lock mutex, when either CONFIG_DEBUG_ALLOC or CONFIG_KFENCE are enabled, which change linear map permissions within softirq context during memory allocation and/or freeing.

But it turns out that the memory for which these features may attempt to modify the permissions is always mapped by pte, so there is no need to attempt to split the mapping. So let's exit early in these cases and avoid attempting to take the mutex.

Fixes a166563e7ec3 ("arm64: mm: support large block mapping when rodata=full")
arch/arm64/mm/mmu.c +20 -10
index b8d37eb037fc..6e26f070bb49 100644
@@ -708,6 +708,16 @@ static int split_kernel_leaf_mapping_locked(unsigned long addr)
 	return ret;
 }
 
+static inline bool force_pte_mapping(void)
+{
+	bool bbml2 = system_capabilities_finalized() ?
+		system_supports_bbml2_noabort() : cpu_supports_bbml2_noabort();
+
+	return (!bbml2 && (rodata_full || arm64_kfence_can_set_direct_map() ||
+			   is_realm_world())) ||
+		debug_pagealloc_enabled();
+}
+
 static DEFINE_MUTEX(pgtable_split_lock);
 
 int split_kernel_leaf_mapping(unsigned long start, unsigned long end)
@@ -723,6 +733,16 @@ int split_kernel_leaf_mapping(unsigned long start, unsigned long end)
 	if (!system_supports_bbml2_noabort())
 		return 0;
 
+	/*
+	 * If the region is within a pte-mapped area, there is no need to try to
+	 * split. Additionally, CONFIG_DEBUG_ALLOC and CONFIG_KFENCE may change
+	 * permissions from softirq context so for those cases (which are always
+	 * pte-mapped), we must not go any further because taking the mutex
+	 * below may sleep.
+	 */
+	if (force_pte_mapping() || is_kfence_address((void *)start))
+		return 0;
+
 	/*
 	 * Ensure start and end are at least page-aligned since this is the
 	 * finest granularity we can split to.
@@ -1009,16 +1029,6 @@ static inline void arm64_kfence_map_pool(phys_addr_t kfence_pool, pgd_t *pgdp) {
 
 #endif /* CONFIG_KFENCE */
 
-static inline bool force_pte_mapping(void)
-{
-	bool bbml2 = system_capabilities_finalized() ?
-		system_supports_bbml2_noabort() : cpu_supports_bbml2_noabort();
-
-	return (!bbml2 && (rodata_full || arm64_kfence_can_set_direct_map() ||
-			   is_realm_world())) ||
-		debug_pagealloc_enabled();
-}
-
 static void __init map_mem(pgd_t *pgdp)
 {
 	static const u64 direct_map_end = _PAGE_END(VA_BITS_MIN);

---8<---

Thanks,
Ryan

Quoted 152 lines

Yang Shi, Do you have any additional thoughts?

Thanks,
Ryan

Quoted 145 lines

Thanks,
Guenter

---
Example log:

[    0.537499] BUG: sleeping function called from invalid context at kernel/locking/mutex.c:580
[    0.537501] in_atomic(): 1, irqs_disabled(): 0, non_block: 0, pid: 1, name: swapper/0
[    0.537502] preempt_count: 1, expected: 0
[    0.537504] 2 locks held by swapper/0/1:
[    0.537505]  #0: ffffb60b01211960 (sched_domains_mutex){+.+.}-{4:4}, at: sched_domains_mutex_lock+0x24/0x38
[    0.537510]  #1: ffffb60b01595838 (rcu_read_lock){....}-{1:3}, at: rcu_lock_acquire+0x0/0x40
[    0.537516] CPU: 0 UID: 0 PID: 1 Comm: swapper/0 Not tainted 6.18.0-dbg-DEV #1 NONE
[    0.537517] Call trace:
[    0.537518]  show_stack+0x20/0x38 (C)
[    0.537520]  __dump_stack+0x28/0x38
[    0.537522]  dump_stack_lvl+0xac/0xf0
[    0.537525]  dump_stack+0x18/0x3c
[    0.537527]  __might_resched+0x248/0x2a0
[    0.537529]  __might_sleep+0x40/0x90
[    0.537531]  __mutex_lock_common+0x70/0x1818
[    0.537533]  mutex_lock_nested+0x34/0x48
[    0.537534]  split_kernel_leaf_mapping+0x74/0x1a0
[    0.537536]  update_range_prot+0x40/0x150
[    0.537537]  __change_memory_common+0x30/0x148
[    0.537538]  __kernel_map_pages+0x70/0x88
[    0.537540]  __free_frozen_pages+0x6e4/0x7b8
[    0.537542]  free_frozen_pages+0x1c/0x30
[    0.537544]  __free_slab+0xf0/0x168
[    0.537547]  free_slab+0x2c/0xf8
[    0.537549]  free_to_partial_list+0x4e0/0x620
[    0.537551]  __slab_free+0x228/0x250
[    0.537553]  kfree+0x3c4/0x4c0
[    0.537555]  destroy_sched_domain+0xf8/0x140
[    0.537557]  cpu_attach_domain+0x17c/0x610
[    0.537558]  build_sched_domains+0x15a4/0x1718
[    0.537560]  sched_init_domains+0xbc/0xf8
[    0.537561]  sched_init_smp+0x30/0x98
[    0.537562]  kernel_init_freeable+0x148/0x230
[    0.537564]  kernel_init+0x28/0x148
[    0.537566]  ret_from_fork+0x10/0x20
[    0.537569] =============================
[    0.537569] [ BUG: Invalid wait context ]
[    0.537571] 6.18.0-dbg-DEV #1 Tainted: G        W
[    0.537572] -----------------------------
[    0.537572] swapper/0/1 is trying to lock:
[    0.537573] ffffb60b011f3830 (pgtable_split_lock){+.+.}-{4:4}, at: split_kernel_leaf_mapping+0x74/0x1a0
[    0.537576] other info that might help us debug this:
[    0.537577] context-{5:5}
[    0.537578] 2 locks held by swapper/0/1:
[    0.537579]  #0: ffffb60b01211960 (sched_domains_mutex){+.+.}-{4:4}, at: sched_domains_mutex_lock+0x24/0x38
[    0.537582]  #1: ffffb60b01595838 (rcu_read_lock){....}-{1:3}, at: rcu_lock_acquire+0x0/0x40
[    0.537585] stack backtrace:
[    0.537585] CPU: 0 UID: 0 PID: 1 Comm: swapper/0 Tainted: G        W           6.18.0-dbg-DEV #1 NONE
[    0.537587] Tainted: [W]=WARN
[    0.537588] Call trace:
[    0.537589]  show_stack+0x20/0x38 (C)
[    0.537591]  __dump_stack+0x28/0x38
[    0.537593]  dump_stack_lvl+0xac/0xf0
[    0.537596]  dump_stack+0x18/0x3c
[    0.537598]  __lock_acquire+0x980/0x2a20
[    0.537600]  lock_acquire+0x124/0x2b8
[    0.537602]  __mutex_lock_common+0xd8/0x1818
[    0.537604]  mutex_lock_nested+0x34/0x48
[    0.537605]  split_kernel_leaf_mapping+0x74/0x1a0
[    0.537607]  update_range_prot+0x40/0x150
[    0.537608]  __change_memory_common+0x30/0x148
[    0.537609]  __kernel_map_pages+0x70/0x88
[    0.537610]  __free_frozen_pages+0x6e4/0x7b8
[    0.537613]  free_frozen_pages+0x1c/0x30
[    0.537615]  __free_slab+0xf0/0x168
[    0.537617]  free_slab+0x2c/0xf8
[    0.537619]  free_to_partial_list+0x4e0/0x620
[    0.537621]  __slab_free+0x228/0x250
[    0.537623]  kfree+0x3c4/0x4c0
[    0.537625]  destroy_sched_domain+0xf8/0x140
[    0.537627]  cpu_attach_domain+0x17c/0x610
[    0.537628]  build_sched_domains+0x15a4/0x1718
[    0.537630]  sched_init_domains+0xbc/0xf8
[    0.537631]  sched_init_smp+0x30/0x98
[    0.537632]  kernel_init_freeable+0x148/0x230
[    0.537633]  kernel_init+0x28/0x148
[    0.537635]  ret_from_fork+0x10/0x20

---
bisect:

# bad: [3a8660878839faadb4f1a6dd72c3179c1df56787] Linux 6.18-rc1
# good: [e5f0a698b34ed76002dc5cff3804a61c80233a7a] Linux 6.17
git bisect start 'v6.18-rc1' 'v6.17'
# bad: [58809f614e0e3f4e12b489bddf680bfeb31c0a20] Merge tag 'drm-next-2025-10-01' of https://gitlab.freedesktop.org/drm/kernel
git bisect bad 58809f614e0e3f4e12b489bddf680bfeb31c0a20
# bad: [a8253f807760e9c80eada9e5354e1240ccf325f9] Merge tag 'soc-newsoc-6.18' of git://git.kernel.org/pub/scm/linux/kernel/git/soc/soc
git bisect bad a8253f807760e9c80eada9e5354e1240ccf325f9
# bad: [4b81e2eb9e4db8f6094c077d0c8b27c264901c1b] Merge tag 'timers-vdso-2025-09-29' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
git bisect bad 4b81e2eb9e4db8f6094c077d0c8b27c264901c1b
# bad: [f1004b2f19d7e9add9d707f64d9fcbc50f67921b] Merge tag 'm68k-for-v6.18-tag1' of git://git.kernel.org/pub/scm/linux/kernel/git/geert/linux-m68k
git bisect bad f1004b2f19d7e9add9d707f64d9fcbc50f67921b
# good: [a9401710a5f5681abd2a6f21f9e76bc9f2e81891] Merge tag 'v6.18-rc-part1-smb3-common' of git://git.samba.org/ksmbd
git bisect good a9401710a5f5681abd2a6f21f9e76bc9f2e81891
# good: [fe68bb2861808ed5c48d399bd7e670ab76829d55] Merge tag 'microblaze-v6.18' of git://git.monstr.eu/linux-2.6-microblaze
git bisect good fe68bb2861808ed5c48d399bd7e670ab76829d55
# bad: [f2d64a22faeeecff385b4c91fab5fe036ab00162] Merge branch 'for-next/perf' into for-next/core
git bisect bad f2d64a22faeeecff385b4c91fab5fe036ab00162
# good: [30f9386820cddbba59b48ae0670c3a1646dd440e] Merge branch 'for-next/misc' into for-next/core
git bisect good 30f9386820cddbba59b48ae0670c3a1646dd440e
# good: [43de0ac332b815cf56dbdce63687de9acfd35d49] drivers/perf: hisi: Relax the event ID check in the framework
git bisect good 43de0ac332b815cf56dbdce63687de9acfd35d49
# good: [5973a62efa34c80c9a4e5eac1fca6f6209b902af] arm64: map [_text, _stext) virtual address range non-executable+read-only git bisect good 5973a62efa34c80c9a4e5eac1fca6f6209b902af
# good: [b3abb08d6f628a76c36bf7da9508e1a67bf186a0] drivers/perf: hisi: Refactor the event configuration of L3C PMU
git bisect good b3abb08d6f628a76c36bf7da9508e1a67bf186a0
# good: [6d2f913fda5683fbd4c3580262e10386c1263dfb] Documentation: hisi-pmu: Add introduction to HiSilicon V3 PMU
git bisect good 6d2f913fda5683fbd4c3580262e10386c1263dfb
# good: [2084660ad288c998b6f0c885e266deb364f65fba] perf/dwc_pcie: Fix use of uninitialized variable
git bisect good 2084660ad288c998b6f0c885e266deb364f65fba
# bad: [77dfca70baefcb988318a72fe69eb99f6dabbbb1] Merge branch 'for-next/mm' into for-next/core
git bisect bad 77dfca70baefcb988318a72fe69eb99f6dabbbb1

# first bad commit: [77dfca70baefcb988318a72fe69eb99f6dabbbb1] Merge branch 'for-next/mm' into for-next/core

---
bisect into branch:

- git checkout -b testing 77dfca70baefcb988318a72fe69eb99f6dabbbb1
- git rebase 77dfca70baefcb988318a72fe69eb99f6dabbbb1~1
[ fix minor conflict similar to the conflict resolution in 77dfca70baefc]
- git diff 77dfca70baefcb988318a72fe69eb99f6dabbbb1
[ confirmed that there are no differences ]
- confirm that the problem is still seen at the tip of the rebase
- git bisect start HEAD 77dfca70baefcb988318a72fe69eb99f6dabbbb1~1
- run bisect

Results:

# bad: [47fc25df1ae3ae8412f1b812fb586c714d04a5e6] arm64: map [_text, _stext) virtual address range non-executable+read-only # good: [30f9386820cddbba59b48ae0670c3a1646dd440e] Merge branch 'for-next/misc' into for-next/core
git bisect start 'HEAD' '77dfca70baefcb988318a72fe69eb99f6dabbbb1~1'
# good: [805491d19fc21271b5c27f4602f8f66b625c110f] arm64/Kconfig: Remove CONFIG_RODATA_FULL_DEFAULT_ENABLED git bisect good 805491d19fc21271b5c27f4602f8f66b625c110f
# bad: [13c7d7426232cc4489df7cd2e1f646a22d3f6172] arm64: mm: support large block mapping when rodata=full git bisect bad 13c7d7426232cc4489df7cd2e1f646a22d3f6172
# good: [a4d9c67e503f2b73c2d89d8e8209dfd241bdc8d8] arm64: Enable permission change on arm64 kernel block mappings git bisect good a4d9c67e503f2b73c2d89d8e8209dfd241bdc8d8

# first bad commit: [13c7d7426232cc4489df7cd2e1f646a22d3f6172] arm64: mm: support large block mapping when rodata=full
#7 lore raw

Re: [PATCH v8 3/5] arm64: mm: support large block mapping when rodata=full

On Sun, Nov 02, 2025 at 12:11:11PM +0000, Ryan Roberts wrote:
...

Quoted 4 lines

Here's a proposed fix, although I can't get access to a system with BBML2 until tomorrow at the earliest. Guenter, I wonder if you could check that this resolves your issue?

Testing.

Thanks!

Guenter

#8 lore raw

Re: [PATCH v8 3/5] arm64: mm: support large block mapping when rodata=full

In reply to Ryan Roberts
Quoted 342 lines · Ryan Roberts
Quoted 95 lines · Ryan Roberts
Quoted 74 lines · Guenter Roeck

Hi,

Quoted 56 lines · Yang Shi

When rodata=full is specified, kernel linear mapping has to be mapped at PTE level since large page table can't be split due to break-before-make rule on ARM64.

This resulted in a couple of problems:
- performance degradation
- more TLB pressure
- memory waste for kernel page table

With FEAT_BBM level 2 support, splitting large block page table to smaller ones doesn't need to make the page table entry invalid anymore. This allows kernel split large block mapping on the fly.

Add kernel page table split support and use large block mapping by default when FEAT_BBM level 2 is supported for rodata=full. When changing permissions for kernel linear mapping, the page table will be split to smaller size.

The machine without FEAT_BBM level 2 will fallback to have kernel linear mapping PTE-mapped when rodata=full.

With this we saw significant performance boost with some benchmarks and much less memory consumption on my AmpereOne machine (192 cores, 1P) with 256GB memory.

* Memory use after boot
Before:

MemTotal:       258988984 kB
MemFree:        254821700 kB

After:

MemTotal:       259505132 kB
MemFree:        255410264 kB

Around 500MB more memory are free to use. The larger the machine, the more memory saved.

* Memcached
We saw performance degradation when running Memcached benchmark with rodata=full vs rodata=on. Our profiling pointed to kernel TLB pressure. With this patchset we saw ops/sec is increased by around 3.5%, P99 latency is reduced by around 9.6%.
The gain mainly came from reduced kernel TLB misses. The kernel TLB MPKI is reduced by 28.5%.

The benchmark data is now on par with rodata=on too.

* Disk encryption (dm-crypt) benchmark
Ran fio benchmark with the below command on a 128G ramdisk (ext4) with disk encryption (by dm-crypt).

fio --directory=/data --random_generator=lfsr --norandommap            \
    --randrepeat 1 --status-interval=999 --rw=write --bs=4k --loops=1  \
    --ioengine=sync --iodepth=1 --numjobs=1 --fsync_on_close=1         \
    --group_reporting --thread --name=iops-test-job --eta-newline=1    \
    --size 100G
Quoted 9 lines

The IOPS is increased by 90% - 150% (the variance is high, but the worst number of good case is around 90% more than the best number of bad case). The bandwidth is increased and the avg clat is reduced proportionally.

* Sequential file read
Read 100G file sequentially on XFS (xfs_io read with page cache populated). The bandwidth is increased by 150%.

With lock debugging enabled, we see a large number of "BUG: sleeping function called from invalid context at kernel/locking/mutex.c:580" and "BUG: Invalid wait context:" backtraces when running v6.18-rc3. Please see example below.

Quoted 4 lines

Bisect points to this patch.

Please let me know if there is anything I can do to help tracking down the problem.

Thanks for the report - ouch!

I expect you're running on a system that supports BBML2_NOABORT, based on the stack trace, I expect you have CONFIG_DEBUG_PAGEALLOC enabled? That will cause permission tricks to be played on the linear map at page allocation and free time, which can happen in non-sleepable contexts. And with this patch we are taking pgtable_split_lock (a mutex) in split_kernel_leaf_mapping(), which is called as a result of the permission change request.

However, when CONFIG_DEBUG_PAGEALLOC enabled we always force-map the linear map by PTE so split_kernel_leaf_mapping() is actually unneccessary and will return without actually having to split anything. So we could add an early "if (force_pte_mapping()) return 0;" to bypass the function entirely in this case, and I *think* that should solve it.

Quoted 5 lines

But I'm also concerned about KFENCE. I can't remember it's exact semantics off the top of my head, so I'm concerned we could see similar problems there (where we only force pte mapping for the KFENCE pool).

I'll investigate fully tomorrow and hopefully provide a fix.

Here's a proposed fix, although I can't get access to a system with BBML2 until tomorrow at the earliest. Guenter, I wonder if you could check that this resolves your issue?

---8<---
commit 602ec2db74e5abfb058bd03934475ead8558eb72

Author: Ryan Roberts <[email protected]>
Date:   Sun Nov 2 11:45:18 2025 +0000

arm64: mm: Don't attempt to split known pte-mapped regions

It has been reported that split_kernel_leaf_mapping() is trying to sleep in non-sleepable context. It does this when acquiring the pgtable_split_lock mutex, when either CONFIG_DEBUG_ALLOC or CONFIG_KFENCE are enabled, which change linear map permissions within softirq context during memory allocation and/or freeing.

But it turns out that the memory for which these features may attempt to modify the permissions is always mapped by pte, so there is no need to attempt to split the mapping. So let's exit early in these cases and avoid attempting to take the mutex.

Fixes a166563e7ec3 ("arm64: mm: support large block mapping when rodata=full")
arch/arm64/mm/mmu.c +20 -11
index b8d37eb037fc..6e26f070bb49 100644
@@ -708,6 +708,16 @@ static int split_kernel_leaf_mapping_locked(unsigned long addr)
        return ret;
 }
 
+static inline bool force_pte_mapping(void)
+{
+       bool bbml2 = system_capabilities_finalized() ?
+               system_supports_bbml2_noabort() : cpu_supports_bbml2_noabort();
+
+       return (!bbml2 && (rodata_full || arm64_kfence_can_set_direct_map() ||
+                          is_realm_world())) ||
+               debug_pagealloc_enabled();
+}
+
 static DEFINE_MUTEX(pgtable_split_lock);
 
 int split_kernel_leaf_mapping(unsigned long start, unsigned long end)
@@ -723,6 +733,16 @@ int split_kernel_leaf_mapping(unsigned long start, unsigned long end)
        if (!system_supports_bbml2_noabort())
                return 0;
 
+       /*
+        * If the region is within a pte-mapped area, there is no need to try to
+        * split. Additionally, CONFIG_DEBUG_ALLOC and CONFIG_KFENCE may change
+        * permissions from softirq context so for those cases (which are always
+        * pte-mapped), we must not go any further because taking the mutex
+        * below may sleep.
+        */
+       if (force_pte_mapping() || is_kfence_address((void *)start))
+               return 0;
+
        /*
         * Ensure start and end are at least page-aligned since this is the
         * finest granularity we can split to.
@@ -1009,16 +1029,6 @@ static inline void arm64_kfence_map_pool(phys_addr_t kfence_pool, pgd_t *pgdp) {
 
 #endif /* CONFIG_KFENCE */
 
-static inline bool force_pte_mapping(void)
-{
-       bool bbml2 = system_capabilities_finalized() ?
-               system_supports_bbml2_noabort() : cpu_supports_bbml2_noabort();
-
-       return (!bbml2 && (rodata_full || arm64_kfence_can_set_direct_map() ||
-                          is_realm_world())) ||
-               debug_pagealloc_enabled();
-}
-
 static void __init map_mem(pgd_t *pgdp)
 {
        static const u64 direct_map_end = _PAGE_END(VA_BITS_MIN);
---8<---

Thanks,
Ryan

Quoted 152 lines

Yang Shi, Do you have any additional thoughts?

Thanks,
Ryan

Quoted 145 lines

Thanks,
Guenter

---
Example log:

[    0.537499] BUG: sleeping function called from invalid context at kernel/locking/mutex.c:580
[    0.537501] in_atomic(): 1, irqs_disabled(): 0, non_block: 0, pid: 1, name: swapper/0
[    0.537502] preempt_count: 1, expected: 0
[    0.537504] 2 locks held by swapper/0/1:
[    0.537505]  #0: ffffb60b01211960 (sched_domains_mutex){+.+.}-{4:4}, at: sched_domains_mutex_lock+0x24/0x38
[    0.537510]  #1: ffffb60b01595838 (rcu_read_lock){....}-{1:3}, at: rcu_lock_acquire+0x0/0x40
[    0.537516] CPU: 0 UID: 0 PID: 1 Comm: swapper/0 Not tainted 6.18.0-dbg-DEV #1 NONE
[    0.537517] Call trace:
[    0.537518]  show_stack+0x20/0x38 (C)
[    0.537520]  __dump_stack+0x28/0x38
[    0.537522]  dump_stack_lvl+0xac/0xf0
[    0.537525]  dump_stack+0x18/0x3c
[    0.537527]  __might_resched+0x248/0x2a0
[    0.537529]  __might_sleep+0x40/0x90
[    0.537531]  __mutex_lock_common+0x70/0x1818
[    0.537533]  mutex_lock_nested+0x34/0x48
[    0.537534]  split_kernel_leaf_mapping+0x74/0x1a0
[    0.537536]  update_range_prot+0x40/0x150
[    0.537537]  __change_memory_common+0x30/0x148
[    0.537538]  __kernel_map_pages+0x70/0x88
[    0.537540]  __free_frozen_pages+0x6e4/0x7b8
[    0.537542]  free_frozen_pages+0x1c/0x30
[    0.537544]  __free_slab+0xf0/0x168
[    0.537547]  free_slab+0x2c/0xf8
[    0.537549]  free_to_partial_list+0x4e0/0x620
[    0.537551]  __slab_free+0x228/0x250
[    0.537553]  kfree+0x3c4/0x4c0
[    0.537555]  destroy_sched_domain+0xf8/0x140
[    0.537557]  cpu_attach_domain+0x17c/0x610
[    0.537558]  build_sched_domains+0x15a4/0x1718
[    0.537560]  sched_init_domains+0xbc/0xf8
[    0.537561]  sched_init_smp+0x30/0x98
[    0.537562]  kernel_init_freeable+0x148/0x230
[    0.537564]  kernel_init+0x28/0x148
[    0.537566]  ret_from_fork+0x10/0x20
[    0.537569] =============================
[    0.537569] [ BUG: Invalid wait context ]
[    0.537571] 6.18.0-dbg-DEV #1 Tainted: G        W
[    0.537572] -----------------------------
[    0.537572] swapper/0/1 is trying to lock:
[    0.537573] ffffb60b011f3830 (pgtable_split_lock){+.+.}-{4:4}, at: split_kernel_leaf_mapping+0x74/0x1a0
[    0.537576] other info that might help us debug this:
[    0.537577] context-{5:5}
[    0.537578] 2 locks held by swapper/0/1:
[    0.537579]  #0: ffffb60b01211960 (sched_domains_mutex){+.+.}-{4:4}, at: sched_domains_mutex_lock+0x24/0x38
[    0.537582]  #1: ffffb60b01595838 (rcu_read_lock){....}-{1:3}, at: rcu_lock_acquire+0x0/0x40
[    0.537585] stack backtrace:
[    0.537585] CPU: 0 UID: 0 PID: 1 Comm: swapper/0 Tainted: G        W           6.18.0-dbg-DEV #1 NONE
[    0.537587] Tainted: [W]=WARN
[    0.537588] Call trace:
[    0.537589]  show_stack+0x20/0x38 (C)
[    0.537591]  __dump_stack+0x28/0x38
[    0.537593]  dump_stack_lvl+0xac/0xf0
[    0.537596]  dump_stack+0x18/0x3c
[    0.537598]  __lock_acquire+0x980/0x2a20
[    0.537600]  lock_acquire+0x124/0x2b8
[    0.537602]  __mutex_lock_common+0xd8/0x1818
[    0.537604]  mutex_lock_nested+0x34/0x48
[    0.537605]  split_kernel_leaf_mapping+0x74/0x1a0
[    0.537607]  update_range_prot+0x40/0x150
[    0.537608]  __change_memory_common+0x30/0x148
[    0.537609]  __kernel_map_pages+0x70/0x88
[    0.537610]  __free_frozen_pages+0x6e4/0x7b8
[    0.537613]  free_frozen_pages+0x1c/0x30
[    0.537615]  __free_slab+0xf0/0x168
[    0.537617]  free_slab+0x2c/0xf8
[    0.537619]  free_to_partial_list+0x4e0/0x620
[    0.537621]  __slab_free+0x228/0x250
[    0.537623]  kfree+0x3c4/0x4c0
[    0.537625]  destroy_sched_domain+0xf8/0x140
[    0.537627]  cpu_attach_domain+0x17c/0x610
[    0.537628]  build_sched_domains+0x15a4/0x1718
[    0.537630]  sched_init_domains+0xbc/0xf8
[    0.537631]  sched_init_smp+0x30/0x98
[    0.537632]  kernel_init_freeable+0x148/0x230
[    0.537633]  kernel_init+0x28/0x148
[    0.537635]  ret_from_fork+0x10/0x20

---
bisect:

# bad: [3a8660878839faadb4f1a6dd72c3179c1df56787] Linux 6.18-rc1
# good: [e5f0a698b34ed76002dc5cff3804a61c80233a7a] Linux 6.17
git bisect start 'v6.18-rc1' 'v6.17'
# bad: [58809f614e0e3f4e12b489bddf680bfeb31c0a20] Merge tag 'drm-next-2025-10-01' of https://gitlab.freedesktop.org/drm/kernel
git bisect bad 58809f614e0e3f4e12b489bddf680bfeb31c0a20
# bad: [a8253f807760e9c80eada9e5354e1240ccf325f9] Merge tag 'soc-newsoc-6.18' of git://git.kernel.org/pub/scm/linux/kernel/git/soc/soc
git bisect bad a8253f807760e9c80eada9e5354e1240ccf325f9
# bad: [4b81e2eb9e4db8f6094c077d0c8b27c264901c1b] Merge tag 'timers-vdso-2025-09-29' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
git bisect bad 4b81e2eb9e4db8f6094c077d0c8b27c264901c1b
# bad: [f1004b2f19d7e9add9d707f64d9fcbc50f67921b] Merge tag 'm68k-for-v6.18-tag1' of git://git.kernel.org/pub/scm/linux/kernel/git/geert/linux-m68k
git bisect bad f1004b2f19d7e9add9d707f64d9fcbc50f67921b
# good: [a9401710a5f5681abd2a6f21f9e76bc9f2e81891] Merge tag 'v6.18-rc-part1-smb3-common' of git://git.samba.org/ksmbd
git bisect good a9401710a5f5681abd2a6f21f9e76bc9f2e81891
# good: [fe68bb2861808ed5c48d399bd7e670ab76829d55] Merge tag 'microblaze-v6.18' of git://git.monstr.eu/linux-2.6-microblaze
git bisect good fe68bb2861808ed5c48d399bd7e670ab76829d55
# bad: [f2d64a22faeeecff385b4c91fab5fe036ab00162] Merge branch 'for-next/perf' into for-next/core
git bisect bad f2d64a22faeeecff385b4c91fab5fe036ab00162
# good: [30f9386820cddbba59b48ae0670c3a1646dd440e] Merge branch 'for-next/misc' into for-next/core
git bisect good 30f9386820cddbba59b48ae0670c3a1646dd440e
# good: [43de0ac332b815cf56dbdce63687de9acfd35d49] drivers/perf: hisi: Relax the event ID check in the framework
git bisect good 43de0ac332b815cf56dbdce63687de9acfd35d49
# good: [5973a62efa34c80c9a4e5eac1fca6f6209b902af] arm64: map [_text, _stext) virtual address range non-executable+read-only git bisect good 5973a62efa34c80c9a4e5eac1fca6f6209b902af
# good: [b3abb08d6f628a76c36bf7da9508e1a67bf186a0] drivers/perf: hisi: Refactor the event configuration of L3C PMU
git bisect good b3abb08d6f628a76c36bf7da9508e1a67bf186a0
# good: [6d2f913fda5683fbd4c3580262e10386c1263dfb] Documentation: hisi-pmu: Add introduction to HiSilicon V3 PMU
git bisect good 6d2f913fda5683fbd4c3580262e10386c1263dfb
# good: [2084660ad288c998b6f0c885e266deb364f65fba] perf/dwc_pcie: Fix use of uninitialized variable
git bisect good 2084660ad288c998b6f0c885e266deb364f65fba
# bad: [77dfca70baefcb988318a72fe69eb99f6dabbbb1] Merge branch 'for-next/mm' into for-next/core
git bisect bad 77dfca70baefcb988318a72fe69eb99f6dabbbb1

# first bad commit: [77dfca70baefcb988318a72fe69eb99f6dabbbb1] Merge branch 'for-next/mm' into for-next/core

---
bisect into branch:

- git checkout -b testing 77dfca70baefcb988318a72fe69eb99f6dabbbb1
- git rebase 77dfca70baefcb988318a72fe69eb99f6dabbbb1~1
[ fix minor conflict similar to the conflict resolution in 77dfca70baefc]
- git diff 77dfca70baefcb988318a72fe69eb99f6dabbbb1
[ confirmed that there are no differences ]
- confirm that the problem is still seen at the tip of the rebase
- git bisect start HEAD 77dfca70baefcb988318a72fe69eb99f6dabbbb1~1
- run bisect

Results:

# bad: [47fc25df1ae3ae8412f1b812fb586c714d04a5e6] arm64: map [_text, _stext) virtual address range non-executable+read-only # good: [30f9386820cddbba59b48ae0670c3a1646dd440e] Merge branch 'for-next/misc' into for-next/core
git bisect start 'HEAD' '77dfca70baefcb988318a72fe69eb99f6dabbbb1~1'
# good: [805491d19fc21271b5c27f4602f8f66b625c110f] arm64/Kconfig: Remove CONFIG_RODATA_FULL_DEFAULT_ENABLED git bisect good 805491d19fc21271b5c27f4602f8f66b625c110f
# bad: [13c7d7426232cc4489df7cd2e1f646a22d3f6172] arm64: mm: support large block mapping when rodata=full git bisect bad 13c7d7426232cc4489df7cd2e1f646a22d3f6172
# good: [a4d9c67e503f2b73c2d89d8e8209dfd241bdc8d8] arm64: Enable permission change on arm64 kernel block mappings git bisect good a4d9c67e503f2b73c2d89d8e8209dfd241bdc8d8

# first bad commit: [13c7d7426232cc4489df7cd2e1f646a22d3f6172] arm64: mm: support large block mapping when rodata=full
Tested-by
#9 lore raw

Re: [PATCH v8 3/5] arm64: mm: support large block mapping when rodata=full

In reply to Ryan Roberts

On Sun, Nov 2, 2025 at 7:09 AM Ryan Roberts <[email protected]> wrote:
...

Quoted 12 lines

commit 602ec2db74e5abfb058bd03934475ead8558eb72

Author: Ryan Roberts <[email protected]>
Date:   Sun Nov 2 11:45:18 2025 +0000

arm64: mm: Don't attempt to split known pte-mapped regions

It has been reported that split_kernel_leaf_mapping() is trying to sleep in non-sleepable context. It does this when acquiring the pgtable_split_lock mutex, when either CONFIG_DEBUG_ALLOC or CONFIG_KFENCE are enabled, which change linear map permissions within softirq context during memory allocation and/or freeing.

Quoted 8 lines

But it turns out that the memory for which these features may attempt to modify the permissions is always mapped by pte, so there is no need to attempt to split the mapping. So let's exit early in these cases and avoid attempting to take the mutex.

Fixes a166563e7ec3 ("arm64: mm: support large block mapping when rodata=full")

Thanks a lot for the quick turnaround!

Guenter

#10 lore raw

Re: [PATCH v8 3/5] arm64: mm: support large block mapping when rodata=full

Quoted 1 line · Guenter Roeck

On Sun, Nov 2, 2025 at 7:09 AM Ryan Roberts <[email protected]> wrote:

Oops. That was sent from my Google address and got messed up. Copying Ryan this time. Sorry for the noise.

Guenter

Quoted 27 lines

...

Quoted 12 lines

commit 602ec2db74e5abfb058bd03934475ead8558eb72

Author: Ryan Roberts <[email protected]>
Date:   Sun Nov 2 11:45:18 2025 +0000

arm64: mm: Don't attempt to split known pte-mapped regions

It has been reported that split_kernel_leaf_mapping() is trying to sleep in non-sleepable context. It does this when acquiring the pgtable_split_lock mutex, when either CONFIG_DEBUG_ALLOC or CONFIG_KFENCE are enabled, which change linear map permissions within softirq context during memory allocation and/or freeing.

Quoted 8 lines

But it turns out that the memory for which these features may attempt to modify the permissions is always mapped by pte, so there is no need to attempt to split the mapping. So let's exit early in these cases and avoid attempting to take the mutex.

Fixes a166563e7ec3 ("arm64: mm: support large block mapping when rodata=full")

Thanks a lot for the quick turnaround!

Guenter

author
#11 lore raw

Re: [PATCH v8 3/5] arm64: mm: support large block mapping when rodata=full

In reply to Ryan Roberts
Quoted 94 lines · Ryan Roberts
Quoted 93 lines · Ryan Roberts
Quoted 73 lines · Guenter Roeck

Hi,

Quoted 56 lines · Yang Shi

When rodata=full is specified, kernel linear mapping has to be mapped at PTE level since large page table can't be split due to break-before-make rule on ARM64.

This resulted in a couple of problems:
- performance degradation
- more TLB pressure
- memory waste for kernel page table

With FEAT_BBM level 2 support, splitting large block page table to smaller ones doesn't need to make the page table entry invalid anymore. This allows kernel split large block mapping on the fly.

Add kernel page table split support and use large block mapping by default when FEAT_BBM level 2 is supported for rodata=full. When changing permissions for kernel linear mapping, the page table will be split to smaller size.

The machine without FEAT_BBM level 2 will fallback to have kernel linear mapping PTE-mapped when rodata=full.

With this we saw significant performance boost with some benchmarks and much less memory consumption on my AmpereOne machine (192 cores, 1P) with 256GB memory.

* Memory use after boot
Before:

MemTotal:       258988984 kB
MemFree:        254821700 kB

After:

MemTotal:       259505132 kB
MemFree:        255410264 kB

Around 500MB more memory are free to use. The larger the machine, the more memory saved.

* Memcached
We saw performance degradation when running Memcached benchmark with rodata=full vs rodata=on. Our profiling pointed to kernel TLB pressure. With this patchset we saw ops/sec is increased by around 3.5%, P99 latency is reduced by around 9.6%.
The gain mainly came from reduced kernel TLB misses. The kernel TLB MPKI is reduced by 28.5%.

The benchmark data is now on par with rodata=on too.

* Disk encryption (dm-crypt) benchmark
Ran fio benchmark with the below command on a 128G ramdisk (ext4) with disk encryption (by dm-crypt).

fio --directory=/data --random_generator=lfsr --norandommap            \
     --randrepeat 1 --status-interval=999 --rw=write --bs=4k --loops=1  \
     --ioengine=sync --iodepth=1 --numjobs=1 --fsync_on_close=1         \
     --group_reporting --thread --name=iops-test-job --eta-newline=1    \
     --size 100G
Quoted 9 lines

The IOPS is increased by 90% - 150% (the variance is high, but the worst number of good case is around 90% more than the best number of bad case). The bandwidth is increased and the avg clat is reduced proportionally.

* Sequential file read
Read 100G file sequentially on XFS (xfs_io read with page cache populated). The bandwidth is increased by 150%.

With lock debugging enabled, we see a large number of "BUG: sleeping function called from invalid context at kernel/locking/mutex.c:580" and "BUG: Invalid wait context:" backtraces when running v6.18-rc3. Please see example below.

Quoted 4 lines

Bisect points to this patch.

Please let me know if there is anything I can do to help tracking down the problem.

Thanks for the report - ouch!

I expect you're running on a system that supports BBML2_NOABORT, based on the stack trace, I expect you have CONFIG_DEBUG_PAGEALLOC enabled? That will cause permission tricks to be played on the linear map at page allocation and free time, which can happen in non-sleepable contexts. And with this patch we are taking pgtable_split_lock (a mutex) in split_kernel_leaf_mapping(), which is called as a result of the permission change request.

However, when CONFIG_DEBUG_PAGEALLOC enabled we always force-map the linear map by PTE so split_kernel_leaf_mapping() is actually unneccessary and will return without actually having to split anything. So we could add an early "if (force_pte_mapping()) return 0;" to bypass the function entirely in this case, and I *think* that should solve it.

Quoted 5 lines
Quoted 5 lines

But I'm also concerned about KFENCE. I can't remember it's exact semantics off the top of my head, so I'm concerned we could see similar problems there (where we only force pte mapping for the KFENCE pool).

I'll investigate fully tomorrow and hopefully provide a fix.

Hi Ryan,

Thanks a lot for the quick fix. I have some comments about kfence below.

Quoted 49 lines

Here's a proposed fix, although I can't get access to a system with BBML2 until tomorrow at the earliest. Guenter, I wonder if you could check that this resolves your issue?

---8<---
commit 602ec2db74e5abfb058bd03934475ead8558eb72

Author: Ryan Roberts <[email protected]>
Date:   Sun Nov 2 11:45:18 2025 +0000

arm64: mm: Don't attempt to split known pte-mapped regions

It has been reported that split_kernel_leaf_mapping() is trying to sleep in non-sleepable context. It does this when acquiring the pgtable_split_lock mutex, when either CONFIG_DEBUG_ALLOC or CONFIG_KFENCE are enabled, which change linear map permissions within softirq context during memory allocation and/or freeing.

But it turns out that the memory for which these features may attempt to modify the permissions is always mapped by pte, so there is no need to attempt to split the mapping. So let's exit early in these cases and avoid attempting to take the mutex.

Fixes a166563e7ec3 ("arm64: mm: support large block mapping when rodata=full")
arch/arm64/mm/mmu.c +10 -0
index b8d37eb037fc..6e26f070bb49 100644
@@ -708,6 +708,16 @@ static int split_kernel_leaf_mapping_locked(unsigned long addr)
  	return ret;
  }
 
+static inline bool force_pte_mapping(void)
+{
+	bool bbml2 = system_capabilities_finalized() ?
+		system_supports_bbml2_noabort() : cpu_supports_bbml2_noabort();
+
+	return (!bbml2 && (rodata_full || arm64_kfence_can_set_direct_map() ||
+			   is_realm_world())) ||
+		debug_pagealloc_enabled();
+}
+
  static DEFINE_MUTEX(pgtable_split_lock);
 
  int split_kernel_leaf_mapping(unsigned long start, unsigned long end)
@@ -723,6 +733,16 @@ int split_kernel_leaf_mapping(unsigned long start, unsigned long end)
  	if (!system_supports_bbml2_noabort())

arch/arm64/mm/mmu.c int split_kernel_leaf_mapping(unsigned long start, unsigned long end) · line 743

Quoted 10 lines
arch/arm64/mm/mmu.c +8 -0
  		return 0;
 
+	/*
+	 * If the region is within a pte-mapped area, there is no need to try to
+	 * split. Additionally, CONFIG_DEBUG_ALLOC and CONFIG_KFENCE may change
+	 * permissions from softirq context so for those cases (which are always
+	 * pte-mapped), we must not go any further because taking the mutex
+	 * below may sleep.
+	 */
+	if (force_pte_mapping() || is_kfence_address((void *)start))

IIUC this may break kfence late init? The kfence_late_init() allocates pages from buddy allocator, then protects them (setting them to invalid). But the protection requires split page table, this check will prevent kernel from splitting page table because __kfence_pool is initialized before doing protection. So there is kind of circular dependency.

The below fix may work?

if (force_pte_mapping() || (READ_ONCE(kfence_enabled) && is_kfence_address((void *)start)))

The kfence_enabled won't be set until protection is done. So if it is set, we know kfence address must be mapped by PTE.

Thanks,
Yang

Quoted 176 lines
arch/arm64/mm/mmu.c +2 -11
+		return 0;
+
  	/*
  	 * Ensure start and end are at least page-aligned since this is the
  	 * finest granularity we can split to.
@@ -1009,16 +1029,6 @@ static inline void arm64_kfence_map_pool(phys_addr_t kfence_pool, pgd_t *pgdp) {
 
  #endif /* CONFIG_KFENCE */
 
-static inline bool force_pte_mapping(void)
-{
-	bool bbml2 = system_capabilities_finalized() ?
-		system_supports_bbml2_noabort() : cpu_supports_bbml2_noabort();
-
-	return (!bbml2 && (rodata_full || arm64_kfence_can_set_direct_map() ||
-			   is_realm_world())) ||
-		debug_pagealloc_enabled();
-}
-
  static void __init map_mem(pgd_t *pgdp)
  {
  	static const u64 direct_map_end = _PAGE_END(VA_BITS_MIN);
---8<---

Thanks,
Ryan

Quoted 149 lines

Yang Shi, Do you have any additional thoughts?

Thanks,
Ryan

Quoted 144 lines

Thanks,
Guenter

---
Example log:

[    0.537499] BUG: sleeping function called from invalid context at kernel/locking/mutex.c:580
[    0.537501] in_atomic(): 1, irqs_disabled(): 0, non_block: 0, pid: 1, name: swapper/0
[    0.537502] preempt_count: 1, expected: 0
[    0.537504] 2 locks held by swapper/0/1:
[    0.537505]  #0: ffffb60b01211960 (sched_domains_mutex){+.+.}-{4:4}, at: sched_domains_mutex_lock+0x24/0x38
[    0.537510]  #1: ffffb60b01595838 (rcu_read_lock){....}-{1:3}, at: rcu_lock_acquire+0x0/0x40
[    0.537516] CPU: 0 UID: 0 PID: 1 Comm: swapper/0 Not tainted 6.18.0-dbg-DEV #1 NONE
[    0.537517] Call trace:
[    0.537518]  show_stack+0x20/0x38 (C)
[    0.537520]  __dump_stack+0x28/0x38
[    0.537522]  dump_stack_lvl+0xac/0xf0
[    0.537525]  dump_stack+0x18/0x3c
[    0.537527]  __might_resched+0x248/0x2a0
[    0.537529]  __might_sleep+0x40/0x90
[    0.537531]  __mutex_lock_common+0x70/0x1818
[    0.537533]  mutex_lock_nested+0x34/0x48
[    0.537534]  split_kernel_leaf_mapping+0x74/0x1a0
[    0.537536]  update_range_prot+0x40/0x150
[    0.537537]  __change_memory_common+0x30/0x148
[    0.537538]  __kernel_map_pages+0x70/0x88
[    0.537540]  __free_frozen_pages+0x6e4/0x7b8
[    0.537542]  free_frozen_pages+0x1c/0x30
[    0.537544]  __free_slab+0xf0/0x168
[    0.537547]  free_slab+0x2c/0xf8
[    0.537549]  free_to_partial_list+0x4e0/0x620
[    0.537551]  __slab_free+0x228/0x250
[    0.537553]  kfree+0x3c4/0x4c0
[    0.537555]  destroy_sched_domain+0xf8/0x140
[    0.537557]  cpu_attach_domain+0x17c/0x610
[    0.537558]  build_sched_domains+0x15a4/0x1718
[    0.537560]  sched_init_domains+0xbc/0xf8
[    0.537561]  sched_init_smp+0x30/0x98
[    0.537562]  kernel_init_freeable+0x148/0x230
[    0.537564]  kernel_init+0x28/0x148
[    0.537566]  ret_from_fork+0x10/0x20
[    0.537569] =============================
[    0.537569] [ BUG: Invalid wait context ]
[    0.537571] 6.18.0-dbg-DEV #1 Tainted: G        W
[    0.537572] -----------------------------
[    0.537572] swapper/0/1 is trying to lock:
[    0.537573] ffffb60b011f3830 (pgtable_split_lock){+.+.}-{4:4}, at: split_kernel_leaf_mapping+0x74/0x1a0
[    0.537576] other info that might help us debug this:
[    0.537577] context-{5:5}
[    0.537578] 2 locks held by swapper/0/1:
[    0.537579]  #0: ffffb60b01211960 (sched_domains_mutex){+.+.}-{4:4}, at: sched_domains_mutex_lock+0x24/0x38
[    0.537582]  #1: ffffb60b01595838 (rcu_read_lock){....}-{1:3}, at: rcu_lock_acquire+0x0/0x40
[    0.537585] stack backtrace:
[    0.537585] CPU: 0 UID: 0 PID: 1 Comm: swapper/0 Tainted: G        W           6.18.0-dbg-DEV #1 NONE
[    0.537587] Tainted: [W]=WARN
[    0.537588] Call trace:
[    0.537589]  show_stack+0x20/0x38 (C)
[    0.537591]  __dump_stack+0x28/0x38
[    0.537593]  dump_stack_lvl+0xac/0xf0
[    0.537596]  dump_stack+0x18/0x3c
[    0.537598]  __lock_acquire+0x980/0x2a20
[    0.537600]  lock_acquire+0x124/0x2b8
[    0.537602]  __mutex_lock_common+0xd8/0x1818
[    0.537604]  mutex_lock_nested+0x34/0x48
[    0.537605]  split_kernel_leaf_mapping+0x74/0x1a0
[    0.537607]  update_range_prot+0x40/0x150
[    0.537608]  __change_memory_common+0x30/0x148
[    0.537609]  __kernel_map_pages+0x70/0x88
[    0.537610]  __free_frozen_pages+0x6e4/0x7b8
[    0.537613]  free_frozen_pages+0x1c/0x30
[    0.537615]  __free_slab+0xf0/0x168
[    0.537617]  free_slab+0x2c/0xf8
[    0.537619]  free_to_partial_list+0x4e0/0x620
[    0.537621]  __slab_free+0x228/0x250
[    0.537623]  kfree+0x3c4/0x4c0
[    0.537625]  destroy_sched_domain+0xf8/0x140
[    0.537627]  cpu_attach_domain+0x17c/0x610
[    0.537628]  build_sched_domains+0x15a4/0x1718
[    0.537630]  sched_init_domains+0xbc/0xf8
[    0.537631]  sched_init_smp+0x30/0x98
[    0.537632]  kernel_init_freeable+0x148/0x230
[    0.537633]  kernel_init+0x28/0x148
[    0.537635]  ret_from_fork+0x10/0x20

---
bisect:

# bad: [3a8660878839faadb4f1a6dd72c3179c1df56787] Linux 6.18-rc1
# good: [e5f0a698b34ed76002dc5cff3804a61c80233a7a] Linux 6.17
git bisect start 'v6.18-rc1' 'v6.17'
# bad: [58809f614e0e3f4e12b489bddf680bfeb31c0a20] Merge tag 'drm-next-2025-10-01' of https://gitlab.freedesktop.org/drm/kernel
git bisect bad 58809f614e0e3f4e12b489bddf680bfeb31c0a20
# bad: [a8253f807760e9c80eada9e5354e1240ccf325f9] Merge tag 'soc-newsoc-6.18' of git://git.kernel.org/pub/scm/linux/kernel/git/soc/soc
git bisect bad a8253f807760e9c80eada9e5354e1240ccf325f9
# bad: [4b81e2eb9e4db8f6094c077d0c8b27c264901c1b] Merge tag 'timers-vdso-2025-09-29' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
git bisect bad 4b81e2eb9e4db8f6094c077d0c8b27c264901c1b
# bad: [f1004b2f19d7e9add9d707f64d9fcbc50f67921b] Merge tag 'm68k-for-v6.18-tag1' of git://git.kernel.org/pub/scm/linux/kernel/git/geert/linux-m68k
git bisect bad f1004b2f19d7e9add9d707f64d9fcbc50f67921b
# good: [a9401710a5f5681abd2a6f21f9e76bc9f2e81891] Merge tag 'v6.18-rc-part1-smb3-common' of git://git.samba.org/ksmbd
git bisect good a9401710a5f5681abd2a6f21f9e76bc9f2e81891
# good: [fe68bb2861808ed5c48d399bd7e670ab76829d55] Merge tag 'microblaze-v6.18' of git://git.monstr.eu/linux-2.6-microblaze
git bisect good fe68bb2861808ed5c48d399bd7e670ab76829d55
# bad: [f2d64a22faeeecff385b4c91fab5fe036ab00162] Merge branch 'for-next/perf' into for-next/core
git bisect bad f2d64a22faeeecff385b4c91fab5fe036ab00162
# good: [30f9386820cddbba59b48ae0670c3a1646dd440e] Merge branch 'for-next/misc' into for-next/core
git bisect good 30f9386820cddbba59b48ae0670c3a1646dd440e
# good: [43de0ac332b815cf56dbdce63687de9acfd35d49] drivers/perf: hisi: Relax the event ID check in the framework
git bisect good 43de0ac332b815cf56dbdce63687de9acfd35d49
# good: [5973a62efa34c80c9a4e5eac1fca6f6209b902af] arm64: map [_text, _stext) virtual address range non-executable+read-only git bisect good 5973a62efa34c80c9a4e5eac1fca6f6209b902af
# good: [b3abb08d6f628a76c36bf7da9508e1a67bf186a0] drivers/perf: hisi: Refactor the event configuration of L3C PMU
git bisect good b3abb08d6f628a76c36bf7da9508e1a67bf186a0
# good: [6d2f913fda5683fbd4c3580262e10386c1263dfb] Documentation: hisi-pmu: Add introduction to HiSilicon V3 PMU
git bisect good 6d2f913fda5683fbd4c3580262e10386c1263dfb
# good: [2084660ad288c998b6f0c885e266deb364f65fba] perf/dwc_pcie: Fix use of uninitialized variable
git bisect good 2084660ad288c998b6f0c885e266deb364f65fba
# bad: [77dfca70baefcb988318a72fe69eb99f6dabbbb1] Merge branch 'for-next/mm' into for-next/core
git bisect bad 77dfca70baefcb988318a72fe69eb99f6dabbbb1

# first bad commit: [77dfca70baefcb988318a72fe69eb99f6dabbbb1] Merge branch 'for-next/mm' into for-next/core

---
bisect into branch:

- git checkout -b testing 77dfca70baefcb988318a72fe69eb99f6dabbbb1
- git rebase 77dfca70baefcb988318a72fe69eb99f6dabbbb1~1
[ fix minor conflict similar to the conflict resolution in 77dfca70baefc]
- git diff 77dfca70baefcb988318a72fe69eb99f6dabbbb1
[ confirmed that there are no differences ]
- confirm that the problem is still seen at the tip of the rebase
- git bisect start HEAD 77dfca70baefcb988318a72fe69eb99f6dabbbb1~1
- run bisect

Results:

# bad: [47fc25df1ae3ae8412f1b812fb586c714d04a5e6] arm64: map [_text, _stext) virtual address range non-executable+read-only # good: [30f9386820cddbba59b48ae0670c3a1646dd440e] Merge branch 'for-next/misc' into for-next/core
git bisect start 'HEAD' '77dfca70baefcb988318a72fe69eb99f6dabbbb1~1'
# good: [805491d19fc21271b5c27f4602f8f66b625c110f] arm64/Kconfig: Remove CONFIG_RODATA_FULL_DEFAULT_ENABLED git bisect good 805491d19fc21271b5c27f4602f8f66b625c110f
# bad: [13c7d7426232cc4489df7cd2e1f646a22d3f6172] arm64: mm: support large block mapping when rodata=full git bisect bad 13c7d7426232cc4489df7cd2e1f646a22d3f6172
# good: [a4d9c67e503f2b73c2d89d8e8209dfd241bdc8d8] arm64: Enable permission change on arm64 kernel block mappings git bisect good a4d9c67e503f2b73c2d89d8e8209dfd241bdc8d8

# first bad commit: [13c7d7426232cc4489df7cd2e1f646a22d3f6172] arm64: mm: support large block mapping when rodata=full
#12 lore raw

Re: [PATCH v8 3/5] arm64: mm: support large block mapping when rodata=full

Quoted 2 lines · Yang Shi

[...]

Quoted 10 lines
Quoted 10 lines
arch/arm64/mm/mmu.c +0 -0
@@ -723,6 +733,16 @@ int split_kernel_leaf_mapping(unsigned long start,
 unsigned long end)
       if (!system_supports_bbml2_noabort())
           return 0;

  +    /*
+     * If the region is within a pte-mapped area, there is no need to try to +     * split. Additionally, CONFIG_DEBUG_ALLOC and CONFIG_KFENCE may change +     * permissions from softirq context so for those cases (which are always +     * pte-mapped), we must not go any further because taking the mutex +     * below may sleep.

Quoted 8 lines
Quoted 2 lines

+     */
+    if (force_pte_mapping() || is_kfence_address((void *)start))

IIUC this may break kfence late init? The kfence_late_init() allocates pages from buddy allocator, then protects them (setting them to invalid). But the protection requires split page table, this check will prevent kernel from splitting page table because __kfence_pool is initialized before doing protection. So there is kind of circular dependency.

I hadn't considered late init. But I guess the requirement is that the kfence pool needs to be pte mapped whenever kfence is enabled.

For early init; that requirement is clearly met since we pte map it in the arch code. For late init, as far as I can tell, the memory is initially block mapped, is allocarted from the buddy then every other page is protected via kfence_init_pool() from kfence_init_pool(). This will have the effect of splitting every page in the pool to pte mappings (as long as your suggested fix below is applied).

It all feels a bit accidental though.

Quoted 8 lines

The below fix may work?

if (force_pte_mapping() || (READ_ONCE(kfence_enabled) && is_kfence_address((void *)start)))

The kfence_enabled won't be set until protection is done. So if it is set, we know kfence address must be mapped by PTE.

I think it will work, but it feels a bit hacky, and kfence_enabled is currently static in core.c.

I wonder if it would be preferable to explicitly do the pte mapping in arch_kfence_init_pool()? It looks like that's how x86 does it...

Quoted 214 lines

Thanks,
Yang

Quoted 205 lines

+        return 0;
+
      /*
       * Ensure start and end are at least page-aligned since this is the
       * finest granularity we can split to.

arch/arm64/mm/mmu.c +0 -10
@@ -1009,16 +1029,6 @@ static inline void arm64_kfence_map_pool(phys_addr_t
 kfence_pool, pgd_t *pgdp) {
     #endif /* CONFIG_KFENCE */
   -static inline bool force_pte_mapping(void)
-{
-    bool bbml2 = system_capabilities_finalized() ?
-        system_supports_bbml2_noabort() : cpu_supports_bbml2_noabort();
-
-    return (!bbml2 && (rodata_full || arm64_kfence_can_set_direct_map() ||
-               is_realm_world())) ||
-        debug_pagealloc_enabled();
-}
-
   static void __init map_mem(pgd_t *pgdp)
   {
       static const u64 direct_map_end = _PAGE_END(VA_BITS_MIN);
---8<---

Thanks,
Ryan

Quoted 179 lines

Yang Shi, Do you have any additional thoughts?

Thanks,
Ryan

Quoted 174 lines

Thanks,
Guenter

---
Example log:

[    0.537499] BUG: sleeping function called from invalid context at kernel/ locking/mutex.c:580
[    0.537501] in_atomic(): 1, irqs_disabled(): 0, non_block: 0, pid: 1,
name: swapper/0
[    0.537502] preempt_count: 1, expected: 0
[    0.537504] 2 locks held by swapper/0/1:
[    0.537505]  #0: ffffb60b01211960 (sched_domains_mutex){+.+.}-{4:4}, at:
sched_domains_mutex_lock+0x24/0x38
[    0.537510]  #1: ffffb60b01595838 (rcu_read_lock){....}-{1:3}, at:
rcu_lock_acquire+0x0/0x40
[    0.537516] CPU: 0 UID: 0 PID: 1 Comm: swapper/0 Not tainted 6.18.0-dbg- DEV #1 NONE
[    0.537517] Call trace:
[    0.537518]  show_stack+0x20/0x38 (C)
[    0.537520]  __dump_stack+0x28/0x38
[    0.537522]  dump_stack_lvl+0xac/0xf0
[    0.537525]  dump_stack+0x18/0x3c
[    0.537527]  __might_resched+0x248/0x2a0
[    0.537529]  __might_sleep+0x40/0x90
[    0.537531]  __mutex_lock_common+0x70/0x1818
[    0.537533]  mutex_lock_nested+0x34/0x48
[    0.537534]  split_kernel_leaf_mapping+0x74/0x1a0
[    0.537536]  update_range_prot+0x40/0x150
[    0.537537]  __change_memory_common+0x30/0x148
[    0.537538]  __kernel_map_pages+0x70/0x88
[    0.537540]  __free_frozen_pages+0x6e4/0x7b8
[    0.537542]  free_frozen_pages+0x1c/0x30
[    0.537544]  __free_slab+0xf0/0x168
[    0.537547]  free_slab+0x2c/0xf8
[    0.537549]  free_to_partial_list+0x4e0/0x620
[    0.537551]  __slab_free+0x228/0x250
[    0.537553]  kfree+0x3c4/0x4c0
[    0.537555]  destroy_sched_domain+0xf8/0x140
[    0.537557]  cpu_attach_domain+0x17c/0x610
[    0.537558]  build_sched_domains+0x15a4/0x1718
[    0.537560]  sched_init_domains+0xbc/0xf8
[    0.537561]  sched_init_smp+0x30/0x98
[    0.537562]  kernel_init_freeable+0x148/0x230
[    0.537564]  kernel_init+0x28/0x148
[    0.537566]  ret_from_fork+0x10/0x20
[    0.537569] =============================
[    0.537569] [ BUG: Invalid wait context ]
[    0.537571] 6.18.0-dbg-DEV #1 Tainted: G        W
[    0.537572] -----------------------------
[    0.537572] swapper/0/1 is trying to lock:
[    0.537573] ffffb60b011f3830 (pgtable_split_lock){+.+.}-{4:4}, at:
split_kernel_leaf_mapping+0x74/0x1a0
[    0.537576] other info that might help us debug this:
[    0.537577] context-{5:5}
[    0.537578] 2 locks held by swapper/0/1:
[    0.537579]  #0: ffffb60b01211960 (sched_domains_mutex){+.+.}-{4:4}, at:
sched_domains_mutex_lock+0x24/0x38
[    0.537582]  #1: ffffb60b01595838 (rcu_read_lock){....}-{1:3}, at:
rcu_lock_acquire+0x0/0x40
[    0.537585] stack backtrace:
[    0.537585] CPU: 0 UID: 0 PID: 1 Comm: swapper/0 Tainted: G
W           6.18.0-dbg-DEV #1 NONE
[    0.537587] Tainted: [W]=WARN
[    0.537588] Call trace:
[    0.537589]  show_stack+0x20/0x38 (C)
[    0.537591]  __dump_stack+0x28/0x38
[    0.537593]  dump_stack_lvl+0xac/0xf0
[    0.537596]  dump_stack+0x18/0x3c
[    0.537598]  __lock_acquire+0x980/0x2a20
[    0.537600]  lock_acquire+0x124/0x2b8
[    0.537602]  __mutex_lock_common+0xd8/0x1818
[    0.537604]  mutex_lock_nested+0x34/0x48
[    0.537605]  split_kernel_leaf_mapping+0x74/0x1a0
[    0.537607]  update_range_prot+0x40/0x150
[    0.537608]  __change_memory_common+0x30/0x148
[    0.537609]  __kernel_map_pages+0x70/0x88
[    0.537610]  __free_frozen_pages+0x6e4/0x7b8
[    0.537613]  free_frozen_pages+0x1c/0x30
[    0.537615]  __free_slab+0xf0/0x168
[    0.537617]  free_slab+0x2c/0xf8
[    0.537619]  free_to_partial_list+0x4e0/0x620
[    0.537621]  __slab_free+0x228/0x250
[    0.537623]  kfree+0x3c4/0x4c0
[    0.537625]  destroy_sched_domain+0xf8/0x140
[    0.537627]  cpu_attach_domain+0x17c/0x610
[    0.537628]  build_sched_domains+0x15a4/0x1718
[    0.537630]  sched_init_domains+0xbc/0xf8
[    0.537631]  sched_init_smp+0x30/0x98
[    0.537632]  kernel_init_freeable+0x148/0x230
[    0.537633]  kernel_init+0x28/0x148
[    0.537635]  ret_from_fork+0x10/0x20

---
bisect:

# bad: [3a8660878839faadb4f1a6dd72c3179c1df56787] Linux 6.18-rc1 # good: [e5f0a698b34ed76002dc5cff3804a61c80233a7a] Linux 6.17 git bisect start 'v6.18-rc1' 'v6.17'
# bad: [58809f614e0e3f4e12b489bddf680bfeb31c0a20] Merge tag 'drm- next-2025-10-01' of https://gitlab.freedesktop.org/drm/kernel
git bisect bad 58809f614e0e3f4e12b489bddf680bfeb31c0a20
# bad: [a8253f807760e9c80eada9e5354e1240ccf325f9] Merge tag 'soc- newsoc-6.18' of git://git.kernel.org/pub/scm/linux/kernel/git/soc/soc
git bisect bad a8253f807760e9c80eada9e5354e1240ccf325f9
# bad: [4b81e2eb9e4db8f6094c077d0c8b27c264901c1b] Merge tag 'timers- vdso-2025-09-29' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
git bisect bad 4b81e2eb9e4db8f6094c077d0c8b27c264901c1b
# bad: [f1004b2f19d7e9add9d707f64d9fcbc50f67921b] Merge tag 'm68k-for-v6.18- tag1' of git://git.kernel.org/pub/scm/linux/kernel/git/geert/linux-m68k
git bisect bad f1004b2f19d7e9add9d707f64d9fcbc50f67921b
# good: [a9401710a5f5681abd2a6f21f9e76bc9f2e81891] Merge tag 'v6.18-rc- part1-smb3-common' of git://git.samba.org/ksmbd
git bisect good a9401710a5f5681abd2a6f21f9e76bc9f2e81891
# good: [fe68bb2861808ed5c48d399bd7e670ab76829d55] Merge tag 'microblaze- v6.18' of git://git.monstr.eu/linux-2.6-microblaze
git bisect good fe68bb2861808ed5c48d399bd7e670ab76829d55
# bad: [f2d64a22faeeecff385b4c91fab5fe036ab00162] Merge branch 'for-next/ perf' into for-next/core
git bisect bad f2d64a22faeeecff385b4c91fab5fe036ab00162
# good: [30f9386820cddbba59b48ae0670c3a1646dd440e] Merge branch 'for-next/ misc' into for-next/core
git bisect good 30f9386820cddbba59b48ae0670c3a1646dd440e
# good: [43de0ac332b815cf56dbdce63687de9acfd35d49] drivers/perf: hisi: Relax the event ID check in the framework
git bisect good 43de0ac332b815cf56dbdce63687de9acfd35d49
# good: [5973a62efa34c80c9a4e5eac1fca6f6209b902af] arm64: map [_text, _stext) virtual address range non-executable+read-only
git bisect good 5973a62efa34c80c9a4e5eac1fca6f6209b902af
# good: [b3abb08d6f628a76c36bf7da9508e1a67bf186a0] drivers/perf: hisi:
Refactor the event configuration of L3C PMU
git bisect good b3abb08d6f628a76c36bf7da9508e1a67bf186a0
# good: [6d2f913fda5683fbd4c3580262e10386c1263dfb] Documentation: hisi-pmu:
Add introduction to HiSilicon V3 PMU
git bisect good 6d2f913fda5683fbd4c3580262e10386c1263dfb
# good: [2084660ad288c998b6f0c885e266deb364f65fba] perf/dwc_pcie: Fix use of uninitialized variable
git bisect good 2084660ad288c998b6f0c885e266deb364f65fba
# bad: [77dfca70baefcb988318a72fe69eb99f6dabbbb1] Merge branch 'for-next/mm' into for-next/core
git bisect bad 77dfca70baefcb988318a72fe69eb99f6dabbbb1

# first bad commit: [77dfca70baefcb988318a72fe69eb99f6dabbbb1] Merge branch
'for-next/mm' into for-next/core

---
bisect into branch:

- git checkout -b testing 77dfca70baefcb988318a72fe69eb99f6dabbbb1
- git rebase 77dfca70baefcb988318a72fe69eb99f6dabbbb1~1
   [ fix minor conflict similar to the conflict resolution in 77dfca70baefc]
- git diff 77dfca70baefcb988318a72fe69eb99f6dabbbb1
   [ confirmed that there are no differences ]
- confirm that the problem is still seen at the tip of the rebase
- git bisect start HEAD 77dfca70baefcb988318a72fe69eb99f6dabbbb1~1
- run bisect

Results:

# bad: [47fc25df1ae3ae8412f1b812fb586c714d04a5e6] arm64: map [_text, _stext) virtual address range non-executable+read-only
# good: [30f9386820cddbba59b48ae0670c3a1646dd440e] Merge branch 'for-next/ misc' into for-next/core
git bisect start 'HEAD' '77dfca70baefcb988318a72fe69eb99f6dabbbb1~1' # good: [805491d19fc21271b5c27f4602f8f66b625c110f] arm64/Kconfig: Remove CONFIG_RODATA_FULL_DEFAULT_ENABLED
git bisect good 805491d19fc21271b5c27f4602f8f66b625c110f
# bad: [13c7d7426232cc4489df7cd2e1f646a22d3f6172] arm64: mm: support large block mapping when rodata=full
git bisect bad 13c7d7426232cc4489df7cd2e1f646a22d3f6172
# good: [a4d9c67e503f2b73c2d89d8e8209dfd241bdc8d8] arm64: Enable permission change on arm64 kernel block mappings
git bisect good a4d9c67e503f2b73c2d89d8e8209dfd241bdc8d8

# first bad commit: [13c7d7426232cc4489df7cd2e1f646a22d3f6172] arm64: mm:
support large block mapping when rodata=full
author
#13 lore raw

Re: [PATCH v8 3/5] arm64: mm: support large block mapping when rodata=full

Quoted 24 lines · Ryan Roberts
Quoted 1 line · Yang Shi

[...]

Quoted 9 lines
Quoted 9 lines
arch/arm64/mm/mmu.c +4 -0
@@ -723,6 +733,16 @@ int split_kernel_leaf_mapping(unsigned long start,
 unsigned long end)
       if (!system_supports_bbml2_noabort())
           return 0;
   +    /*
+     * If the region is within a pte-mapped area, there is no need to try to
+     * split. Additionally, CONFIG_DEBUG_ALLOC and CONFIG_KFENCE may change
+     * permissions from softirq context so for those cases (which are always
+     * pte-mapped), we must not go any further because taking the mutex
Quoted 8 lines

arch/arm64/mm/mmu.c int split_kernel_leaf_mapping(unsigned long start, unsigned long end) · line 743

Quoted 3 lines
arch/arm64/mm/mmu.c +3 -0
+     * below may sleep.
+     */
+    if (force_pte_mapping() || is_kfence_address((void *)start))

IIUC this may break kfence late init? The kfence_late_init() allocates pages from buddy allocator, then protects them (setting them to invalid). But the protection requires split page table, this check will prevent kernel from splitting page table because __kfence_pool is initialized before doing protection. So there is kind of circular dependency.

I hadn't considered late init. But I guess the requirement is that the kfence pool needs to be pte mapped whenever kfence is enabled.

Quoted 8 lines

For early init; that requirement is clearly met since we pte map it in the arch code. For late init, as far as I can tell, the memory is initially block mapped, is allocarted from the buddy then every other page is protected via kfence_init_pool() from kfence_init_pool(). This will have the effect of splitting every page in the pool to pte mappings (as long as your suggested fix below is applied).

It all feels a bit accidental though.

Yeah, it is not that explicit and obvious.

Quoted 6 lines
Quoted 6 lines

The below fix may work?

if (force_pte_mapping() || (READ_ONCE(kfence_enabled) && is_kfence_address((void *)start)))

Quoted 7 lines
Quoted 2 lines

The kfence_enabled won't be set until protection is done. So if it is set, we know kfence address must be mapped by PTE.

I think it will work, but it feels a bit hacky, and kfence_enabled is currently static in core.c.

I wonder if it would be preferable to explicitly do the pte mapping in arch_kfence_init_pool()? It looks like that's how x86 does it...

I agree, this looks better and strongly and explicitly convey the PTE-mapping for kfence pool requirement.

Thanks,
Yang

Quoted 213 lines
Quoted 213 lines

Thanks,
Yang

Quoted 205 lines
arch/arm64/mm/mmu.c +2 -10
+        return 0;
+
       /*
        * Ensure start and end are at least page-aligned since this is the
        * finest granularity we can split to.
@@ -1009,16 +1029,6 @@ static inline void arm64_kfence_map_pool(phys_addr_t
 kfence_pool, pgd_t *pgdp) {
     #endif /* CONFIG_KFENCE */
   -static inline bool force_pte_mapping(void)
-{
-    bool bbml2 = system_capabilities_finalized() ?
-        system_supports_bbml2_noabort() : cpu_supports_bbml2_noabort();
-
-    return (!bbml2 && (rodata_full || arm64_kfence_can_set_direct_map() ||
-               is_realm_world())) ||
-        debug_pagealloc_enabled();
-}
-
   static void __init map_mem(pgd_t *pgdp)
   {
       static const u64 direct_map_end = _PAGE_END(VA_BITS_MIN);
---8<---

Thanks,
Ryan

Quoted 179 lines

Yang Shi, Do you have any additional thoughts?

Thanks,
Ryan

Quoted 174 lines

Thanks,
Guenter

---
Example log:

[    0.537499] BUG: sleeping function called from invalid context at kernel/ locking/mutex.c:580
[    0.537501] in_atomic(): 1, irqs_disabled(): 0, non_block: 0, pid: 1,
name: swapper/0
[    0.537502] preempt_count: 1, expected: 0
[    0.537504] 2 locks held by swapper/0/1:
[    0.537505]  #0: ffffb60b01211960 (sched_domains_mutex){+.+.}-{4:4}, at:
sched_domains_mutex_lock+0x24/0x38
[    0.537510]  #1: ffffb60b01595838 (rcu_read_lock){....}-{1:3}, at:
rcu_lock_acquire+0x0/0x40
[    0.537516] CPU: 0 UID: 0 PID: 1 Comm: swapper/0 Not tainted 6.18.0-dbg- DEV #1 NONE
[    0.537517] Call trace:
[    0.537518]  show_stack+0x20/0x38 (C)
[    0.537520]  __dump_stack+0x28/0x38
[    0.537522]  dump_stack_lvl+0xac/0xf0
[    0.537525]  dump_stack+0x18/0x3c
[    0.537527]  __might_resched+0x248/0x2a0
[    0.537529]  __might_sleep+0x40/0x90
[    0.537531]  __mutex_lock_common+0x70/0x1818
[    0.537533]  mutex_lock_nested+0x34/0x48
[    0.537534]  split_kernel_leaf_mapping+0x74/0x1a0
[    0.537536]  update_range_prot+0x40/0x150
[    0.537537]  __change_memory_common+0x30/0x148
[    0.537538]  __kernel_map_pages+0x70/0x88
[    0.537540]  __free_frozen_pages+0x6e4/0x7b8
[    0.537542]  free_frozen_pages+0x1c/0x30
[    0.537544]  __free_slab+0xf0/0x168
[    0.537547]  free_slab+0x2c/0xf8
[    0.537549]  free_to_partial_list+0x4e0/0x620
[    0.537551]  __slab_free+0x228/0x250
[    0.537553]  kfree+0x3c4/0x4c0
[    0.537555]  destroy_sched_domain+0xf8/0x140
[    0.537557]  cpu_attach_domain+0x17c/0x610
[    0.537558]  build_sched_domains+0x15a4/0x1718
[    0.537560]  sched_init_domains+0xbc/0xf8
[    0.537561]  sched_init_smp+0x30/0x98
[    0.537562]  kernel_init_freeable+0x148/0x230
[    0.537564]  kernel_init+0x28/0x148
[    0.537566]  ret_from_fork+0x10/0x20
[    0.537569] =============================
[    0.537569] [ BUG: Invalid wait context ]
[    0.537571] 6.18.0-dbg-DEV #1 Tainted: G        W
[    0.537572] -----------------------------
[    0.537572] swapper/0/1 is trying to lock:
[    0.537573] ffffb60b011f3830 (pgtable_split_lock){+.+.}-{4:4}, at:
split_kernel_leaf_mapping+0x74/0x1a0
[    0.537576] other info that might help us debug this:
[    0.537577] context-{5:5}
[    0.537578] 2 locks held by swapper/0/1:
[    0.537579]  #0: ffffb60b01211960 (sched_domains_mutex){+.+.}-{4:4}, at:
sched_domains_mutex_lock+0x24/0x38
[    0.537582]  #1: ffffb60b01595838 (rcu_read_lock){....}-{1:3}, at:
rcu_lock_acquire+0x0/0x40
[    0.537585] stack backtrace:
[    0.537585] CPU: 0 UID: 0 PID: 1 Comm: swapper/0 Tainted: G
W           6.18.0-dbg-DEV #1 NONE
[    0.537587] Tainted: [W]=WARN
[    0.537588] Call trace:
[    0.537589]  show_stack+0x20/0x38 (C)
[    0.537591]  __dump_stack+0x28/0x38
[    0.537593]  dump_stack_lvl+0xac/0xf0
[    0.537596]  dump_stack+0x18/0x3c
[    0.537598]  __lock_acquire+0x980/0x2a20
[    0.537600]  lock_acquire+0x124/0x2b8
[    0.537602]  __mutex_lock_common+0xd8/0x1818
[    0.537604]  mutex_lock_nested+0x34/0x48
[    0.537605]  split_kernel_leaf_mapping+0x74/0x1a0
[    0.537607]  update_range_prot+0x40/0x150
[    0.537608]  __change_memory_common+0x30/0x148
[    0.537609]  __kernel_map_pages+0x70/0x88
[    0.537610]  __free_frozen_pages+0x6e4/0x7b8
[    0.537613]  free_frozen_pages+0x1c/0x30
[    0.537615]  __free_slab+0xf0/0x168
[    0.537617]  free_slab+0x2c/0xf8
[    0.537619]  free_to_partial_list+0x4e0/0x620
[    0.537621]  __slab_free+0x228/0x250
[    0.537623]  kfree+0x3c4/0x4c0
[    0.537625]  destroy_sched_domain+0xf8/0x140
[    0.537627]  cpu_attach_domain+0x17c/0x610
[    0.537628]  build_sched_domains+0x15a4/0x1718
[    0.537630]  sched_init_domains+0xbc/0xf8
[    0.537631]  sched_init_smp+0x30/0x98
[    0.537632]  kernel_init_freeable+0x148/0x230
[    0.537633]  kernel_init+0x28/0x148
[    0.537635]  ret_from_fork+0x10/0x20

---
bisect:

# bad: [3a8660878839faadb4f1a6dd72c3179c1df56787] Linux 6.18-rc1 # good: [e5f0a698b34ed76002dc5cff3804a61c80233a7a] Linux 6.17 git bisect start 'v6.18-rc1' 'v6.17'
# bad: [58809f614e0e3f4e12b489bddf680bfeb31c0a20] Merge tag 'drm- next-2025-10-01' of https://gitlab.freedesktop.org/drm/kernel
git bisect bad 58809f614e0e3f4e12b489bddf680bfeb31c0a20
# bad: [a8253f807760e9c80eada9e5354e1240ccf325f9] Merge tag 'soc- newsoc-6.18' of git://git.kernel.org/pub/scm/linux/kernel/git/soc/soc
git bisect bad a8253f807760e9c80eada9e5354e1240ccf325f9
# bad: [4b81e2eb9e4db8f6094c077d0c8b27c264901c1b] Merge tag 'timers- vdso-2025-09-29' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
git bisect bad 4b81e2eb9e4db8f6094c077d0c8b27c264901c1b
# bad: [f1004b2f19d7e9add9d707f64d9fcbc50f67921b] Merge tag 'm68k-for-v6.18- tag1' of git://git.kernel.org/pub/scm/linux/kernel/git/geert/linux-m68k
git bisect bad f1004b2f19d7e9add9d707f64d9fcbc50f67921b
# good: [a9401710a5f5681abd2a6f21f9e76bc9f2e81891] Merge tag 'v6.18-rc- part1-smb3-common' of git://git.samba.org/ksmbd
git bisect good a9401710a5f5681abd2a6f21f9e76bc9f2e81891
# good: [fe68bb2861808ed5c48d399bd7e670ab76829d55] Merge tag 'microblaze- v6.18' of git://git.monstr.eu/linux-2.6-microblaze
git bisect good fe68bb2861808ed5c48d399bd7e670ab76829d55
# bad: [f2d64a22faeeecff385b4c91fab5fe036ab00162] Merge branch 'for-next/ perf' into for-next/core
git bisect bad f2d64a22faeeecff385b4c91fab5fe036ab00162
# good: [30f9386820cddbba59b48ae0670c3a1646dd440e] Merge branch 'for-next/ misc' into for-next/core
git bisect good 30f9386820cddbba59b48ae0670c3a1646dd440e
# good: [43de0ac332b815cf56dbdce63687de9acfd35d49] drivers/perf: hisi: Relax the event ID check in the framework
git bisect good 43de0ac332b815cf56dbdce63687de9acfd35d49
# good: [5973a62efa34c80c9a4e5eac1fca6f6209b902af] arm64: map [_text, _stext) virtual address range non-executable+read-only
git bisect good 5973a62efa34c80c9a4e5eac1fca6f6209b902af
# good: [b3abb08d6f628a76c36bf7da9508e1a67bf186a0] drivers/perf: hisi:
Refactor the event configuration of L3C PMU
git bisect good b3abb08d6f628a76c36bf7da9508e1a67bf186a0
# good: [6d2f913fda5683fbd4c3580262e10386c1263dfb] Documentation: hisi-pmu:
Add introduction to HiSilicon V3 PMU
git bisect good 6d2f913fda5683fbd4c3580262e10386c1263dfb
# good: [2084660ad288c998b6f0c885e266deb364f65fba] perf/dwc_pcie: Fix use of uninitialized variable
git bisect good 2084660ad288c998b6f0c885e266deb364f65fba
# bad: [77dfca70baefcb988318a72fe69eb99f6dabbbb1] Merge branch 'for-next/mm' into for-next/core
git bisect bad 77dfca70baefcb988318a72fe69eb99f6dabbbb1

# first bad commit: [77dfca70baefcb988318a72fe69eb99f6dabbbb1] Merge branch
'for-next/mm' into for-next/core

---
bisect into branch:

- git checkout -b testing 77dfca70baefcb988318a72fe69eb99f6dabbbb1
- git rebase 77dfca70baefcb988318a72fe69eb99f6dabbbb1~1
   [ fix minor conflict similar to the conflict resolution in 77dfca70baefc]
- git diff 77dfca70baefcb988318a72fe69eb99f6dabbbb1
   [ confirmed that there are no differences ]
- confirm that the problem is still seen at the tip of the rebase
- git bisect start HEAD 77dfca70baefcb988318a72fe69eb99f6dabbbb1~1
- run bisect

Results:

# bad: [47fc25df1ae3ae8412f1b812fb586c714d04a5e6] arm64: map [_text, _stext) virtual address range non-executable+read-only
# good: [30f9386820cddbba59b48ae0670c3a1646dd440e] Merge branch 'for-next/ misc' into for-next/core
git bisect start 'HEAD' '77dfca70baefcb988318a72fe69eb99f6dabbbb1~1' # good: [805491d19fc21271b5c27f4602f8f66b625c110f] arm64/Kconfig: Remove CONFIG_RODATA_FULL_DEFAULT_ENABLED
git bisect good 805491d19fc21271b5c27f4602f8f66b625c110f
# bad: [13c7d7426232cc4489df7cd2e1f646a22d3f6172] arm64: mm: support large block mapping when rodata=full
git bisect bad 13c7d7426232cc4489df7cd2e1f646a22d3f6172
# good: [a4d9c67e503f2b73c2d89d8e8209dfd241bdc8d8] arm64: Enable permission change on arm64 kernel block mappings
git bisect good a4d9c67e503f2b73c2d89d8e8209dfd241bdc8d8

# first bad commit: [13c7d7426232cc4489df7cd2e1f646a22d3f6172] arm64: mm:
support large block mapping when rodata=full
Reviewed-by
#14 lore raw

Re: [PATCH v8 3/5] arm64: mm: support large block mapping when rodata=full

In reply to Ryan Roberts
Quoted 78 lines
Quoted 25 lines
Quoted 10 lines
Quoted 1 line

With lock debugging enabled, we see a large number of "BUG: sleeping function called from invalid context at kernel/locking/mutex.c:580" and "BUG: Invalid wait context:" backtraces when running v6.18-rc3. Please see example below.

Bisect points to this patch.

Please let me know if there is anything I can do to help tracking down the problem.

Thanks for the report - ouch!

I expect you're running on a system that supports BBML2_NOABORT, based on the stack trace, I expect you have CONFIG_DEBUG_PAGEALLOC enabled? That will cause permission tricks to be played on the linear map at page allocation and free time, which can happen in non-sleepable contexts. And with this patch we are taking pgtable_split_lock (a mutex) in split_kernel_leaf_mapping(), which is called as a result of the permission change request.

However, when CONFIG_DEBUG_PAGEALLOC enabled we always force-map the linear map by PTE so split_kernel_leaf_mapping() is actually unneccessary and will return without actually having to split anything. So we could add an early "if (force_pte_mapping()) return 0;" to bypass the function entirely in this case, and I *think* that should solve it.

Quoted 5 lines

But I'm also concerned about KFENCE. I can't remember it's exact semantics off the top of my head, so I'm concerned we could see similar problems there (where we only force pte mapping for the KFENCE pool).

I'll investigate fully tomorrow and hopefully provide a fix.

Here's a proposed fix, although I can't get access to a system with BBML2 until tomorrow at the earliest. Guenter, I wonder if you could check that this resolves your issue?

---8<---
commit 602ec2db74e5abfb058bd03934475ead8558eb72

Author: Ryan Roberts <[email protected]>
Date:   Sun Nov 2 11:45:18 2025 +0000

arm64: mm: Don't attempt to split known pte-mapped regions

It has been reported that split_kernel_leaf_mapping() is trying to sleep in non-sleepable context. It does this when acquiring the pgtable_split_lock mutex, when either CONFIG_DEBUG_ALLOC or CONFIG_KFENCE are enabled, which change linear map permissions within softirq context during memory allocation and/or freeing.

But it turns out that the memory for which these features may attempt to modify the permissions is always mapped by pte, so there is no need to attempt to split the mapping. So let's exit early in these cases and avoid attempting to take the mutex.

Fixes a166563e7ec3 ("arm64: mm: support large block mapping when rodata=full")
arch/arm64/mm/mmu.c +10 -0
index b8d37eb037fc..6e26f070bb49 100644
@@ -708,6 +708,16 @@ static int split_kernel_leaf_mapping_locked(unsigned long addr)
  	return ret;
  }
 
+static inline bool force_pte_mapping(void)
+{
+	bool bbml2 = system_capabilities_finalized() ?
+		system_supports_bbml2_noabort() : cpu_supports_bbml2_noabort();
+
+	return (!bbml2 && (rodata_full || arm64_kfence_can_set_direct_map() ||
+			   is_realm_world())) ||
+		debug_pagealloc_enabled();
+}
+
  static DEFINE_MUTEX(pgtable_split_lock);
 
  int split_kernel_leaf_mapping(unsigned long start, unsigned long end)

arch/arm64/mm/mmu.c int split_kernel_leaf_mapping(unsigned long start, unsigned long end) · line 738

Quoted 6 lines
arch/arm64/mm/mmu.c +3 -0
@@ -723,6 +733,16 @@ int split_kernel_leaf_mapping(unsigned long start, unsigned long end)
  	if (!system_supports_bbml2_noabort())
  		return 0;
 
+	/*
+	 * If the region is within a pte-mapped area, there is no need to try to
+	 * split. Additionally, CONFIG_DEBUG_ALLOC and CONFIG_KFENCE may change

Nit: CONFIG_DEBUG_PAGEALLOC.

Quoted 14 lines
arch/arm64/mm/mmu.c +7 -0
+	 * permissions from softirq context so for those cases (which are always
+	 * pte-mapped), we must not go any further because taking the mutex
+	 * below may sleep.
+	 */
+	if (force_pte_mapping() || is_kfence_address((void *)start))
+		return 0;
+
  	/*
  	 * Ensure start and end are at least page-aligned since this is the
  	 * finest granularity we can split to.
@@ -1009,16 +1029,6 @@ static inline void arm64_kfence_map_pool(phys_addr_t kfence_pool, pgd_t *pgdp) {
 
  #endif /* CONFIG_KFENCE */
 

arch/arm64/mm/mmu.c static inline void arm64_kfence_map_pool(phys_addr_t kfence_pool, pgd_t *pgdp) { · line 1021

Quoted 10 lines
arch/arm64/mm/mmu.c +0 -10
-static inline bool force_pte_mapping(void)
-{
-	bool bbml2 = system_capabilities_finalized() ?
-		system_supports_bbml2_noabort() : cpu_supports_bbml2_noabort();
-
-	return (!bbml2 && (rodata_full || arm64_kfence_can_set_direct_map() ||
-			   is_realm_world())) ||
-		debug_pagealloc_enabled();
-}
-

Otherwise LGTM.

Quoted 13 lines
arch/arm64/mm/mmu.c +0 -1
  static void __init map_mem(pgd_t *pgdp)
  {
  	static const u64 direct_map_end = _PAGE_END(VA_BITS_MIN);
---8<---

Thanks,
Ryan

Quoted 5 lines

Yang Shi, Do you have any additional thoughts?

Thanks,
Ryan

author
#15 lore raw

PATCH v8 4/5 arm64: mm: split linear mapping if BBML2 unsupported on secondary CPUs

In reply to Yang Shi

From: Ryan Roberts <[email protected]>

The kernel linear mapping is painted in very early stage of system boot. The cpufeature has not been finalized yet at this point. So the linear mapping is determined by the capability of boot CPU only. If the boot CPU supports BBML2, large block mappings will be used for linear mapping.

But the secondary CPUs may not support BBML2, so repaint the linear mapping if large block mapping is used and the secondary CPUs don't support BBML2 once cpufeature is finalized on all CPUs.

If the boot CPU doesn't support BBML2 or the secondary CPUs have the same BBML2 capability with the boot CPU, repainting the linear mapping is not needed.

Repainting is implemented by the boot CPU, which we know supports BBML2, so it is safe for the live mapping size to change for this CPU. The linear map region is walked using the pagewalk API and any discovered large leaf mappings are split to pte mappings using the existing helper functions. Since the repainting is performed inside of a stop_machine(), we must use GFP_ATOMIC to allocate the extra intermediate pgtables. But since we are still early in boot, it is expected that there is plenty of memory available so we will never need to sleep for reclaim, and so GFP_ATOMIC is acceptable here.

The secondary CPUs are all put into a waiting area with the idmap in TTBR0 and reserved map in TTBR1 while this is performed since they cannot be allowed to observe any size changes on the live mappings. Some of this infrastructure is reused from the kpti case. Specifically we share the same flag (was __idmap_kpti_flag, now idmap_kpti_bbml2_flag) since it means we don't have to reserve any extra pgtable memory to idmap the extra flag.

---
 arch/arm64/include/asm/mmu.h   |   2 +
 arch/arm64/kernel/cpufeature.c |   3 +
 arch/arm64/mm/mmu.c            | 182 +++++++++++++++++++++++++++++----
 arch/arm64/mm/proc.S           |  27 +++--
 4 files changed, 187 insertions(+), 27 deletions(-)
arch/arm64/include/asm/mmu.h +2 -0
index a7cc95d97ceb..ff6fd0bbd7d2 100644
@@ -79,6 +79,8 @@ extern void create_pgd_mapping(struct mm_struct *mm, phys_addr_t phys,
 extern void *fixmap_remap_fdt(phys_addr_t dt_phys, int *size, pgprot_t prot);
 extern void mark_linear_text_alias_ro(void);
 extern int split_kernel_leaf_mapping(unsigned long start, unsigned long end);
+extern void init_idmap_kpti_bbml2_flag(void);
+extern void linear_map_maybe_split_to_ptes(void);
 
 /*
  * This check is triggered during the early boot before the cpufeature
arch/arm64/kernel/cpufeature.c +3 -0
index 7dc092e33fcc..dd3dbe4c9359 100644
@@ -86,6 +86,7 @@
 #include <asm/kvm_host.h>
 #include <asm/mmu.h>
 #include <asm/mmu_context.h>
+#include <asm/mmu.h>
 #include <asm/mte.h>
 #include <asm/hypervisor.h>
 #include <asm/processor.h>
@@ -2028,6 +2029,7 @@ static void __init kpti_install_ng_mappings(void)
 	if (arm64_use_ng_mappings)
 		return;
 
+	init_idmap_kpti_bbml2_flag();
 	stop_machine(__kpti_install_ng_mappings, NULL, cpu_online_mask);
 }
 
@@ -3955,6 +3957,7 @@ void __init setup_system_features(void)
 {
 	setup_system_capabilities();
 
+	linear_map_maybe_split_to_ptes();
 	kpti_install_ng_mappings();
 
 	sve_setup();
arch/arm64/mm/mmu.c +162 -20

292 lines not on this page · raw message

arch/arm64/mm/proc.S +20 -7
index 8c75965afc9e..86818511962b 100644
@@ -245,10 +245,6 @@ SYM_FUNC_ALIAS(__pi_idmap_cpu_replace_ttbr1, idmap_cpu_replace_ttbr1)
  *
  * Called exactly once from stop_machine context by each CPU found during boot.
  */
-	.pushsection	".data", "aw", %progbits
-SYM_DATA(__idmap_kpti_flag, .long 1)
-	.popsection
-
 SYM_TYPED_FUNC_START(idmap_kpti_install_ng_mappings)
 	cpu		.req	w0
 	temp_pte	.req	x0
@@ -273,7 +269,7 @@ SYM_TYPED_FUNC_START(idmap_kpti_install_ng_mappings)
 
 	mov	x5, x3				// preserve temp_pte arg
 	mrs	swapper_ttb, ttbr1_el1
-	adr_l	flag_ptr, __idmap_kpti_flag
+	adr_l	flag_ptr, idmap_kpti_bbml2_flag
 
 	cbnz	cpu, __idmap_kpti_secondary
 
@@ -416,7 +412,25 @@ alternative_else_nop_endif
 __idmap_kpti_secondary:
 	/* Uninstall swapper before surgery begins */
 	__idmap_cpu_set_reserved_ttbr1 x16, x17
+	b scondary_cpu_wait
+
+	.unreq	swapper_ttb
+	.unreq	flag_ptr
+SYM_FUNC_END(idmap_kpti_install_ng_mappings)
+	.popsection
+#endif
+
+	.pushsection ".idmap.text", "a"
+SYM_TYPED_FUNC_START(wait_linear_map_split_to_ptes)
+	/* Must be same registers as in idmap_kpti_install_ng_mappings */
+	swapper_ttb	.req	x3
+	flag_ptr	.req	x4
+
+	mrs     swapper_ttb, ttbr1_el1
+	adr_l   flag_ptr, idmap_kpti_bbml2_flag
+	__idmap_cpu_set_reserved_ttbr1 x16, x17
 
+scondary_cpu_wait:
 	/* Increment the flag to let the boot CPU we're ready */
 1:	ldxr	w16, [flag_ptr]
 	add	w16, w16, #1
@@ -436,9 +450,8 @@ __idmap_kpti_secondary:
 
 	.unreq	swapper_ttb
 	.unreq	flag_ptr
-SYM_FUNC_END(idmap_kpti_install_ng_mappings)
+SYM_FUNC_END(wait_linear_map_split_to_ptes)
 	.popsection
-#endif
 
 /*
  *	__cpu_setup
Signature · 1 line
2.47.0
#16 lore raw

Re: [PATCH v8 4/5] arm64: mm: split linear mapping if BBML2 unsupported on secondary CPUs

Review

Quoted 9 lines · Yang Shi
(quoted diff) +9 -0
+	.pushsection ".idmap.text", "a"
+SYM_TYPED_FUNC_START(wait_linear_map_split_to_ptes)
+	/* Must be same registers as in idmap_kpti_install_ng_mappings */
+	swapper_ttb	.req	x3
+	flag_ptr	.req	x4
+
+	mrs     swapper_ttb, ttbr1_el1
+	adr_l   flag_ptr, idmap_kpti_bbml2_flag
+	__idmap_cpu_set_reserved_ttbr1 x16, x17

I'm getting build failures here, using CONFIG_CFI using clang-21:

ld.lld-21: error: undefined symbol: __kcfi_typeid_wait_linear_map_split_to_ptes

Quoted 1 line
Quoted 1 line
Quoted 1 line

referenced by arch/arm64/mm/proc.o:(.idmap.text+0x298) in archive vmlinux.a

I can get it to build by using plain SYM_FUNC_START() instead of SYM_TYPED_FUNC_START(), but I have no idea if that makes any sense:

arch/arm64/mm/proc.S +1 -1
@@ -439,7 +439,7 @@ SYM_FUNC_END(idmap_kpti_install_ng_mappings)
 #endif
 
        .pushsection ".idmap.text", "a"
-SYM_TYPED_FUNC_START(wait_linear_map_split_to_ptes)
+SYM_FUNC_START(wait_linear_map_split_to_ptes)
        /* Must be same registers as in idmap_kpti_install_ng_mappings */
        swapper_ttb     .req    x3
        flag_ptr        .req    x4

Arnd

#17 lore raw

Re: [PATCH v8 4/5] arm64: mm: split linear mapping if BBML2 unsupported on secondary CPUs

Quoted 24 lines · Arnd Bergmann

Review

Quoted 9 lines · Yang Shi
(quoted diff) +9 -0
+	.pushsection ".idmap.text", "a"
+SYM_TYPED_FUNC_START(wait_linear_map_split_to_ptes)
+	/* Must be same registers as in idmap_kpti_install_ng_mappings */
+	swapper_ttb	.req	x3
+	flag_ptr	.req	x4
+
+	mrs     swapper_ttb, ttbr1_el1
+	adr_l   flag_ptr, idmap_kpti_bbml2_flag
+	__idmap_cpu_set_reserved_ttbr1 x16, x17

I'm getting build failures here, using CONFIG_CFI using clang-21:

ld.lld-21: error: undefined symbol: __kcfi_typeid_wait_linear_map_split_to_ptes

Quoted 1 line
Quoted 1 line
Quoted 1 line

referenced by arch/arm64/mm/proc.o:(.idmap.text+0x298) in archive vmlinux.a

I can get it to build by using plain SYM_FUNC_START() instead of SYM_TYPED_FUNC_START(), but I have no idea if that makes any sense:

arch/arm64/mm/proc.S SYM_FUNC_END(idmap_kpti_install_ng_mappings) · line 445

Quoted 8 lines
arch/arm64/mm/proc.S +1 -1
@@ -439,7 +439,7 @@ SYM_FUNC_END(idmap_kpti_install_ng_mappings)
 #endif
 
        .pushsection ".idmap.text", "a"
-SYM_TYPED_FUNC_START(wait_linear_map_split_to_ptes)
+SYM_FUNC_START(wait_linear_map_split_to_ptes)
        /* Must be same registers as in idmap_kpti_install_ng_mappings */
        swapper_ttb     .req    x3
        flag_ptr        .req    x4

This is not the right fix: the indirect call from linear_map_split_to_ptes() will be instrumented, and so it requires the CFI annotation to precede the function entry point, which is what SYM_TYPED_FUNC_START() is supposed to emit.

The typeid symbol is injected by the compiler into every object file that takes the address of the function in question, and so the fact that it is missing seems to suggest that linear_map_split_to_ptes() has been optimized away entirely. Could you double check arch/arm64/mm/mmu.o if that is the case?

#18 lore raw

Re: [PATCH v8 4/5] arm64: mm: split linear mapping if BBML2 unsupported on secondary CPUs

Quoted 20 lines · Ard Biesheuvel

arch/arm64/mm/proc.S SYM_FUNC_END(idmap_kpti_install_ng_mappings) · line 445

Quoted 13 lines · Arnd Bergmann

On Wed, Sep 17, 2025, at 21:02, Yang Shi wrote:

arch/arm64/mm/proc.S +1 -1
@@ -439,7 +439,7 @@ SYM_FUNC_END(idmap_kpti_install_ng_mappings)
 #endif
 
        .pushsection ".idmap.text", "a"
-SYM_TYPED_FUNC_START(wait_linear_map_split_to_ptes)
+SYM_FUNC_START(wait_linear_map_split_to_ptes)
        /* Must be same registers as in idmap_kpti_install_ng_mappings */
        swapper_ttb     .req    x3
        flag_ptr        .req    x4

This is not the right fix: the indirect call from linear_map_split_to_ptes() will be instrumented, and so it requires the CFI annotation to precede the function entry point, which is what SYM_TYPED_FUNC_START() is supposed to emit.

Quoted 5 lines

The typeid symbol is injected by the compiler into every object file that takes the address of the function in question, and so the fact that it is missing seems to suggest that linear_map_split_to_ptes() has been optimized away entirely. Could you double check arch/arm64/mm/mmu.o if that is the case?

Ah, thanks for the explanation. This has indeed been optimized out by a patch in my randconfig tree that makes CONFIG_SMP optional for arm64. Without SMP, the calling code path is never used because smp_processor_id() is fixed to 0.

I'll fix up that other patch then.

Arnd

author
#19 lore raw

PATCH v8 5/5 arm64: kprobes: call set_memory_rox() for kprobe page

In reply to Yang Shi

The kprobe page is allocated by execmem allocator with ROX permission. It needs to call set_memory_rox() to set proper permission for the direct map too. It was missed.

And the set_memory_rox() guarantees the direct map will be split if it needs so that set_direct_map calls in vfree() won't fail.

Fixes 10d5e97c1bf8 ("arm64: use PAGE_KERNEL_ROX directly in alloc_insn_page")
---
 arch/arm64/kernel/probes/kprobes.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
arch/arm64/kernel/probes/kprobes.c +12 -0
index 0c5d408afd95..c4f8c4750f1e 100644
@@ -10,6 +10,7 @@
 
 #define pr_fmt(fmt) "kprobes: " fmt
 
+#include <linux/execmem.h>
 #include <linux/extable.h>
 #include <linux/kasan.h>
 #include <linux/kernel.h>
@@ -41,6 +42,17 @@ DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
 static void __kprobes
 post_kprobe_handler(struct kprobe *, struct kprobe_ctlblk *, struct pt_regs *);
 
+void *alloc_insn_page(void)
+{
+	void *page;
+
+	page = execmem_alloc(EXECMEM_KPROBES, PAGE_SIZE);
+	if (!page)
+		return NULL;
+	set_memory_rox((unsigned long)page, 1);
+	return page;
+}
+
 static void __kprobes arch_prepare_ss_slot(struct kprobe *p)
 {
 	kprobe_opcode_t *addr = p->ainsn.xol_insn;
Signature · 1 line
2.47.0
maintainer
#20 lore raw

Re: [PATCH v8 5/5] arm64: kprobes: call set_memory_rox() for kprobe page

Quoted 26 lines · Yang Shi

The kprobe page is allocated by execmem allocator with ROX permission. It needs to call set_memory_rox() to set proper permission for the direct map too. It was missed.

And the set_memory_rox() guarantees the direct map will be split if it needs so that set_direct_map calls in vfree() won't fail.

Fixes 10d5e97c1bf8 ("arm64: use PAGE_KERNEL_ROX directly in alloc_insn_page")
---
 arch/arm64/kernel/probes/kprobes.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
arch/arm64/kernel/probes/kprobes.c +1 -0
index 0c5d408afd95..c4f8c4750f1e 100644
@@ -10,6 +10,7 @@
 
 #define pr_fmt(fmt) "kprobes: " fmt
 
+#include <linux/execmem.h>
 #include <linux/extable.h>
 #include <linux/kasan.h>
 #include <linux/kernel.h>

arch/arm64/kernel/probes/kprobes.c DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk); · line 47

Quoted 6 lines
arch/arm64/kernel/probes/kprobes.c +3 -0
@@ -41,6 +42,17 @@ DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
 static void __kprobes
 post_kprobe_handler(struct kprobe *, struct kprobe_ctlblk *, struct pt_regs *);
 
+void *alloc_insn_page(void)
+{
+	void *page;

Nit: I'd call this 'addr'. 'page' makes me think of a struct page.

arch/arm64/kernel/probes/kprobes.c DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk); · line 52

Quoted 5 lines
arch/arm64/kernel/probes/kprobes.c +5 -0
+
+	page = execmem_alloc(EXECMEM_KPROBES, PAGE_SIZE);
+	if (!page)
+		return NULL;
+	set_memory_rox((unsigned long)page, 1);

It's unfortunate that we change the attributes of the ROX vmap first to RO, then to back to ROX so that we get the linear map changed. Maybe factor out some of the code in change_memory_common() to only change the linear map.

Otherwise it looks fine.

Signature · 1 line
Catalin
author
#21 lore raw

Re: [PATCH v8 5/5] arm64: kprobes: call set_memory_rox() for kprobe page

Quoted 30 lines · Catalin Marinas
Quoted 29 lines · Yang Shi

The kprobe page is allocated by execmem allocator with ROX permission. It needs to call set_memory_rox() to set proper permission for the direct map too. It was missed.

And the set_memory_rox() guarantees the direct map will be split if it needs so that set_direct_map calls in vfree() won't fail.

Fixes 10d5e97c1bf8 ("arm64: use PAGE_KERNEL_ROX directly in alloc_insn_page")
---
  arch/arm64/kernel/probes/kprobes.c | 12 ++++++++++++
  1 file changed, 12 insertions(+)
arch/arm64/kernel/probes/kprobes.c +1 -0
index 0c5d408afd95..c4f8c4750f1e 100644
@@ -10,6 +10,7 @@
 
  #define pr_fmt(fmt) "kprobes: " fmt
 
+#include <linux/execmem.h>
  #include <linux/extable.h>
  #include <linux/kasan.h>
  #include <linux/kernel.h>
@@ -41,6 +42,17 @@ DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
  static void __kprobes
  post_kprobe_handler(struct kprobe *, struct kprobe_ctlblk *, struct pt_regs *);
 
Quoted 4 lines

arch/arm64/kernel/probes/kprobes.c DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk); · line 47

Quoted 3 lines
arch/arm64/kernel/probes/kprobes.c +3 -0
+void *alloc_insn_page(void)
+{
+	void *page;

Nit: I'd call this 'addr'. 'page' makes me think of a struct page.

Sure.

Quoted 10 lines

arch/arm64/kernel/probes/kprobes.c DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk); · line 52

Quoted 6 lines
arch/arm64/kernel/probes/kprobes.c +5 -0
+
+	page = execmem_alloc(EXECMEM_KPROBES, PAGE_SIZE);
+	if (!page)
+		return NULL;
+	set_memory_rox((unsigned long)page, 1);

It's unfortunate that we change the attributes of the ROX vmap first to RO, then to back to ROX so that we get the linear map changed. Maybe factor out some of the code in change_memory_common() to only change the linear map.

I want to make sure I understand you correctly, you meant set_memory_rox() should do:

change linear map to RO (call a new helper, for example, set_direct_map_ro()) change vmap to ROX (call change_memory_common())

Is it correct?

If so set_memory_ro() should do the similar thing.

And I think we should have the cleanup patch separate from this bug fix patch because the bug fix patch should be applied to -stable release too. Keeping it simpler makes the backport easier.

Shall I squash the cleanup patch into patch #1?

Thanks,
Yang

Quoted 3 lines

Otherwise it looks fine.

#22 lore raw

Re: [PATCH v8 5/5] arm64: kprobes: call set_memory_rox() for kprobe page

Quoted 58 lines · Yang Shi
Quoted 25 lines · Catalin Marinas
Quoted 24 lines · Yang Shi

The kprobe page is allocated by execmem allocator with ROX permission. It needs to call set_memory_rox() to set proper permission for the direct map too. It was missed.

And the set_memory_rox() guarantees the direct map will be split if it needs so that set_direct_map calls in vfree() won't fail.

Fixes 10d5e97c1bf8 ("arm64: use PAGE_KERNEL_ROX directly in alloc_insn_page")

---
  arch/arm64/kernel/probes/kprobes.c | 12 ++++++++++++

  1 file changed, 12 insertions(+)
arch/arm64/kernel/probes/ renamed from arch/arm64/kernel/probes/kprobes.c +0 -0

kprobes.c
index 0c5d408afd95..c4f8c4750f1e 100644

arch/arm64/kernel/probes/kprobes.c +0 -0
@@ -10,6 +10,7 @@
     #define pr_fmt(fmt) "kprobes: " fmt

  +#include <linux/execmem.h>
  #include <linux/extable.h>
  #include <linux/kasan.h>
  #include <linux/kernel.h>

Quoted 8 lines
Quoted 7 lines
arch/arm64/kernel/probes/kprobes.c +0 -0
@@ -41,6 +42,17 @@ DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);

  static void __kprobes
  post_kprobe_handler(struct kprobe *, struct kprobe_ctlblk *, struct pt_regs *);
  +void *alloc_insn_page(void)
+{
+    void *page;

Nit: I'd call this 'addr'. 'page' makes me think of a struct page.

Sure.

Quoted 10 lines
Quoted 6 lines

+
+    page = execmem_alloc(EXECMEM_KPROBES, PAGE_SIZE); +    if (!page)
+        return NULL;
+    set_memory_rox((unsigned long)page, 1);

It's unfortunate that we change the attributes of the ROX vmap first to RO, then to back to ROX so that we get the linear map changed. Maybe factor out some of the code in change_memory_common() to only change the linear map.

I want to make sure I understand you correctly, you meant set_memory_rox() should do:

change linear map to RO (call a new helper, for example, set_direct_map_ro()) change vmap to ROX (call change_memory_common())

Is it correct?

Quoted 7 lines

If so set_memory_ro() should do the similar thing.

And I think we should have the cleanup patch separate from this bug fix patch because the bug fix patch should be applied to -stable release too. Keeping it simpler makes the backport easier.

Shall I squash the cleanup patch into patch #1?

Personally I think we should drop this patch from the series and handle it separately.

We worked out that the requirement is to either never call set_memory_*() or to call set_memory_*() for the entire vmalloc'ed range prior to optionally calling set_memory_*() for a sub-range in order to guarrantee vm_reset_perms() works correctly.

Given this is only allocating a single page, it is impossible to call set_memory_*() for a sub-range. So the requirement is met.

I agree it looks odd/wrong to have different permissions in the linear map vs the vmap but that is an orthogonal bug that can be fixed separately.

What do you think?

Thanks,
Ryan

Quoted 8 lines

Thanks,
Yang

Quoted 3 lines

Otherwise it looks fine.

author
#23 lore raw

Re: [PATCH v8 5/5] arm64: kprobes: call set_memory_rox() for kprobe page

Quoted 71 lines · Ryan Roberts
Quoted 54 lines · Yang Shi
Quoted 25 lines · Catalin Marinas
Quoted 24 lines · Yang Shi

The kprobe page is allocated by execmem allocator with ROX permission. It needs to call set_memory_rox() to set proper permission for the direct map too. It was missed.

And the set_memory_rox() guarantees the direct map will be split if it needs so that set_direct_map calls in vfree() won't fail.

Fixes 10d5e97c1bf8 ("arm64: use PAGE_KERNEL_ROX directly in alloc_insn_page")
---
   arch/arm64/kernel/probes/kprobes.c | 12 ++++++++++++
   1 file changed, 12 insertions(+)
arch/arm64/kernel/probes/ renamed from arch/arm64/kernel/probes/kprobes.c +0 -0

kprobes.c
index 0c5d408afd95..c4f8c4750f1e 100644

arch/arm64/kernel/probes/kprobes.c +0 -0
@@ -10,6 +10,7 @@
     #define pr_fmt(fmt) "kprobes: " fmt
   +#include <linux/execmem.h>
   #include <linux/extable.h>
   #include <linux/kasan.h>
   #include <linux/kernel.h>
Quoted 8 lines

arch/arm64/kernel/probes/kprobes.c DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk); · line 47

Quoted 7 lines
arch/arm64/kernel/probes/kprobes.c +2 -0
@@ -41,6 +42,17 @@ DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
   static void __kprobes
   post_kprobe_handler(struct kprobe *, struct kprobe_ctlblk *, struct pt_regs
 *);
   +void *alloc_insn_page(void)
+{
+    void *page;

Nit: I'd call this 'addr'. 'page' makes me think of a struct page.

Sure.

Quoted 9 lines

arch/arm64/kernel/probes/kprobes.c DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk); · line 52

Quoted 5 lines
arch/arm64/kernel/probes/kprobes.c +5 -0
+
+    page = execmem_alloc(EXECMEM_KPROBES, PAGE_SIZE);
+    if (!page)
+        return NULL;
+    set_memory_rox((unsigned long)page, 1);

It's unfortunate that we change the attributes of the ROX vmap first to RO, then to back to ROX so that we get the linear map changed. Maybe factor out some of the code in change_memory_common() to only change the linear map.

I want to make sure I understand you correctly, you meant set_memory_rox() should do:

change linear map to RO (call a new helper, for example, set_direct_map_ro()) change vmap to ROX (call change_memory_common())

Is it correct?

Quoted 7 lines

If so set_memory_ro() should do the similar thing.

And I think we should have the cleanup patch separate from this bug fix patch because the bug fix patch should be applied to -stable release too. Keeping it simpler makes the backport easier.

Shall I squash the cleanup patch into patch #1?

Personally I think we should drop this patch from the series and handle it separately.

We worked out that the requirement is to either never call set_memory_*() or to call set_memory_*() for the entire vmalloc'ed range prior to optionally calling set_memory_*() for a sub-range in order to guarrantee vm_reset_perms() works correctly.

Quoted 7 lines

Given this is only allocating a single page, it is impossible to call set_memory_*() for a sub-range. So the requirement is met.

I agree it looks odd/wrong to have different permissions in the linear map vs the vmap but that is an orthogonal bug that can be fixed separately.

What do you think?

Yeah, sounds good to me.

Thanks,
Yang

Quoted 10 lines

Thanks,
Ryan

Quoted 5 lines

Thanks,
Yang

Quoted 2 lines

Otherwise it looks fine.

maintainer Reviewed-by
#24 lore raw

Re: [PATCH v8 5/5] arm64: kprobes: call set_memory_rox() for kprobe page

In reply to Yang Shi
Quoted 11 lines · Yang Shi
Quoted 9 lines · Catalin Marinas

arch/arm64/kernel/probes/kprobes.c DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk); · line 52

Quoted 4 lines · Yang Shi
arch/arm64/kernel/probes/kprobes.c +4 -0
+	page = execmem_alloc(EXECMEM_KPROBES, PAGE_SIZE);
+	if (!page)
+		return NULL;
+	set_memory_rox((unsigned long)page, 1);

It's unfortunate that we change the attributes of the ROX vmap first to RO, then to back to ROX so that we get the linear map changed. Maybe factor out some of the code in change_memory_common() to only change the linear map.

Quoted 6 lines

I want to make sure I understand you correctly, you meant set_memory_rox() should do:

change linear map to RO (call a new helper, for example, set_direct_map_ro())
change vmap to ROX (call change_memory_common())

set_memory_rox() is correct. What I meant is that in alloc_insn_page(), execmem_alloc() already returns RX memory. Calling set_memory_rox() does indeed change the linear map to RO but it also changes the vmap memory to RO and then to RX. There's no need for the alloc_insn_page() to do this but we shouldn't change set_memory_rox() for this, the latter is correct. I was thinking of alloc_insn_page() calling a new function that only changes the linear map.

Quoted 3 lines

And I think we should have the cleanup patch separate from this bug fix patch because the bug fix patch should be applied to -stable release too. Keeping it simpler makes the backport easier.

Yes, for now you can leave it as is, that's not a critical path.

Quoted 1 line

Shall I squash the cleanup patch into patch #1?

No, I'd leave it as a separate fix, especially if we want to backport it.

Anyway, for now, with the nitpick on the address variable name:

author
#25 lore raw

Re: [PATCH v8 5/5] arm64: kprobes: call set_memory_rox() for kprobe page

Quoted 17 lines · Catalin Marinas
Quoted 10 lines · Yang Shi
Quoted 9 lines · Catalin Marinas

arch/arm64/kernel/probes/kprobes.c DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk); · line 52

Quoted 4 lines · Yang Shi
arch/arm64/kernel/probes/kprobes.c +4 -0
+	page = execmem_alloc(EXECMEM_KPROBES, PAGE_SIZE);
+	if (!page)
+		return NULL;
+	set_memory_rox((unsigned long)page, 1);

It's unfortunate that we change the attributes of the ROX vmap first to RO, then to back to ROX so that we get the linear map changed. Maybe factor out some of the code in change_memory_common() to only change the linear map.

Quoted 6 lines

I want to make sure I understand you correctly, you meant set_memory_rox() should do:

change linear map to RO (call a new helper, for example, set_direct_map_ro())
change vmap to ROX (call change_memory_common())

Quoted 7 lines

set_memory_rox() is correct. What I meant is that in alloc_insn_page(), execmem_alloc() already returns RX memory. Calling set_memory_rox() does indeed change the linear map to RO but it also changes the vmap memory to RO and then to RX. There's no need for the alloc_insn_page() to do this but we shouldn't change set_memory_rox() for this, the latter is correct. I was thinking of alloc_insn_page() calling a new function that only changes the linear map.

Aha, I see. If we have the new helper, it also allows us to refactor set_memory_rox() to what I said.

Quoted 5 lines
Quoted 4 lines

And I think we should have the cleanup patch separate from this bug fix patch because the bug fix patch should be applied to -stable release too. Keeping it simpler makes the backport easier.

Yes, for now you can leave it as is, that's not a critical path.

Sure.

Quoted 4 lines
Quoted 2 lines

Shall I squash the cleanup patch into patch #1?

No, I'd leave it as a separate fix, especially if we want to backport it.

I meant the potential cleanup patch. Anyway it can be handled in a separate patch at anytime.

Quoted 4 lines

Anyway, for now, with the nitpick on the address variable name:

Thank you. Ryan also suggested separate the fix from this series. I will fix the variable name nit then post it separately instead of posting a new series.

Yang

maintainer Applied
#26 lore raw
In reply to Yang Shi
Quoted 8 lines · Yang Shi

On systems with BBML2_NOABORT support, it causes the linear map to be mapped with large blocks, even when rodata=full, and leads to some nice performance improvements.

Ryan tested v7 on an AmpereOne system (a VM with 12G RAM) in all 3 possible modes by hacking the BBML2 feature detection code:

[...]

Applied patches 1 and 3 to arm64 (for-next/mm), thanks!

[1/5] arm64: Enable permission change on arm64 kernel block mappings

      https://git.kernel.org/arm64/c/a660194dd101
[3/5] arm64: mm: support large block mapping when rodata=full
      https://git.kernel.org/arm64/c/a166563e7ec3

I also picked up the BBML allow-list addition (second patch) on for-next/cpufeature.

The fourth patch ("arm64: mm: split linear mapping if BBML2 unsupported on secondary CPUs") has some really horrible conflicts. These are partly due to some of the type cleanups on for-next/mm but I think mainly due to Kevin's kpti rework that landed after -rc1.

So I think the best bet might be to leave that one for next time, if that's ok?

Cheers,

Signature · 5 lines
Will

https://fixes.arm64.dev
https://next.arm64.dev
https://will.arm64.dev
#27 lore raw
Quoted 17 lines · Will Deacon
Quoted 8 lines · Yang Shi

On systems with BBML2_NOABORT support, it causes the linear map to be mapped with large blocks, even when rodata=full, and leads to some nice performance improvements.

Ryan tested v7 on an AmpereOne system (a VM with 12G RAM) in all 3 possible modes by hacking the BBML2 feature detection code:

[...]

Applied patches 1 and 3 to arm64 (for-next/mm), thanks!

[1/5] arm64: Enable permission change on arm64 kernel block mappings

      https://git.kernel.org/arm64/c/a660194dd101
[3/5] arm64: mm: support large block mapping when rodata=full
      https://git.kernel.org/arm64/c/a166563e7ec3
Quoted 7 lines

I also picked up the BBML allow-list addition (second patch) on for-next/cpufeature.

The fourth patch ("arm64: mm: split linear mapping if BBML2 unsupported on secondary CPUs") has some really horrible conflicts. These are partly due to some of the type cleanups on for-next/mm but I think mainly due to Kevin's kpti rework that landed after -rc1.

Thanks Will, although I'm nervous that without this patch, some platforms might not boot; Wikipedia tells me that there are some Google, Mediatek and Qualcomm SoCs that pair X4 CPUs (which is on the BBML2_NOABORT allow list) with A720 and/or A520 (which are not). See previous mail at [1].

I'd be happy to rebase it if you can let me know the prefered base SHA/tree?

[1]
https://lore.kernel.org/linux-arm-kernel/[email protected]/

Thanks,
Ryan

Quoted 5 lines

So I think the best bet might be to leave that one for next time, if that's ok?

Cheers,

maintainer
#28 lore raw
Quoted 26 lines · Ryan Roberts
Quoted 17 lines · Will Deacon
Quoted 8 lines · Yang Shi

On systems with BBML2_NOABORT support, it causes the linear map to be mapped with large blocks, even when rodata=full, and leads to some nice performance improvements.

Ryan tested v7 on an AmpereOne system (a VM with 12G RAM) in all 3 possible modes by hacking the BBML2 feature detection code:

[...]

Applied patches 1 and 3 to arm64 (for-next/mm), thanks!

[1/5] arm64: Enable permission change on arm64 kernel block mappings

      https://git.kernel.org/arm64/c/a660194dd101
[3/5] arm64: mm: support large block mapping when rodata=full
      https://git.kernel.org/arm64/c/a166563e7ec3
Quoted 7 lines

I also picked up the BBML allow-list addition (second patch) on for-next/cpufeature.

The fourth patch ("arm64: mm: split linear mapping if BBML2 unsupported on secondary CPUs") has some really horrible conflicts. These are partly due to some of the type cleanups on for-next/mm but I think mainly due to Kevin's kpti rework that landed after -rc1.

Quoted 4 lines

Thanks Will, although I'm nervous that without this patch, some platforms might not boot; Wikipedia tells me that there are some Google, Mediatek and Qualcomm SoCs that pair X4 CPUs (which is on the BBML2_NOABORT allow list) with A720 and/or A520 (which are not). See previous mail at [1].

I'd be surprised if these SoCs are booting on the X4 but who knows.

Lemme have another look at applying the patch with fresh eyes, but I do wonder whether having X4 on the allow list really makes any sense. Are there any SoCs out there that _don't_ pair it with CPUs that aren't on the allow list? (apologies for the double negative).

Will

#29 lore raw
Quoted 27 lines · Will Deacon
Quoted 26 lines · Ryan Roberts
Quoted 24 lines · Will Deacon
Quoted 8 lines · Yang Shi

On systems with BBML2_NOABORT support, it causes the linear map to be mapped with large blocks, even when rodata=full, and leads to some nice performance improvements.

Ryan tested v7 on an AmpereOne system (a VM with 12G RAM) in all 3 possible modes by hacking the BBML2 feature detection code:

[...]

Applied patches 1 and 3 to arm64 (for-next/mm), thanks!

[1/5] arm64: Enable permission change on arm64 kernel block mappings

      https://git.kernel.org/arm64/c/a660194dd101
[3/5] arm64: mm: support large block mapping when rodata=full
      https://git.kernel.org/arm64/c/a166563e7ec3

I also picked up the BBML allow-list addition (second patch) on for-next/cpufeature.

The fourth patch ("arm64: mm: split linear mapping if BBML2 unsupported on secondary CPUs") has some really horrible conflicts. These are partly due to some of the type cleanups on for-next/mm but I think mainly due to Kevin's kpti rework that landed after -rc1.

Quoted 6 lines
Quoted 4 lines

Thanks Will, although I'm nervous that without this patch, some platforms might not boot; Wikipedia tells me that there are some Google, Mediatek and Qualcomm SoCs that pair X4 CPUs (which is on the BBML2_NOABORT allow list) with A720 and/or A520 (which are not). See previous mail at [1].

I'd be surprised if these SoCs are booting on the X4 but who knows.

Ahh. You can probably tell I'm a bit naive to some of this system level stuff... I had assumed they would want to boot on the big CPU to reduce boot time.

Quoted 5 lines

Lemme have another look at applying the patch with fresh eyes, but I do wonder whether having X4 on the allow list really makes any sense. Are there any SoCs out there that _don't_ pair it with CPUs that aren't on the allow list? (apologies for the double negative).

Hmm, that's a fair question. I'm not aware of any. So I guess the simplest solution is to remove X4 from the allow list and ditch fourth patch.

Quoted 2 lines

Will

maintainer
#30 lore raw
Quoted 33 lines · Ryan Roberts
Quoted 32 lines · Will Deacon
Quoted 30 lines · Ryan Roberts
Quoted 17 lines · Will Deacon
Quoted 8 lines · Yang Shi

On systems with BBML2_NOABORT support, it causes the linear map to be mapped with large blocks, even when rodata=full, and leads to some nice performance improvements.

Ryan tested v7 on an AmpereOne system (a VM with 12G RAM) in all 3 possible modes by hacking the BBML2 feature detection code:

[...]

Applied patches 1 and 3 to arm64 (for-next/mm), thanks!

[1/5] arm64: Enable permission change on arm64 kernel block mappings

      https://git.kernel.org/arm64/c/a660194dd101
[3/5] arm64: mm: support large block mapping when rodata=full
      https://git.kernel.org/arm64/c/a166563e7ec3
Quoted 7 lines

I also picked up the BBML allow-list addition (second patch) on for-next/cpufeature.

The fourth patch ("arm64: mm: split linear mapping if BBML2 unsupported on secondary CPUs") has some really horrible conflicts. These are partly due to some of the type cleanups on for-next/mm but I think mainly due to Kevin's kpti rework that landed after -rc1.

Thanks Will, although I'm nervous that without this patch, some platforms might not boot; Wikipedia tells me that there are some Google, Mediatek and Qualcomm SoCs that pair X4 CPUs (which is on the BBML2_NOABORT allow list) with A720 and/or A520 (which are not). See previous mail at [1].

Quoted 4 lines
Quoted 1 line

I'd be surprised if these SoCs are booting on the X4 but who knows.

Ahh. You can probably tell I'm a bit naive to some of this system level stuff... I had assumed they would want to boot on the big CPU to reduce boot time.

One of the problems is that the boot CPU becomes CPU0 and that inevitably means it ends up being responsible for a tonne of extra stuff (interrupts, TZ, etc) and in many cases can't be offlined. So it's all a trade-off.

Quoted 7 lines
Quoted 4 lines

Lemme have another look at applying the patch with fresh eyes, but I do wonder whether having X4 on the allow list really makes any sense. Are there any SoCs out there that _don't_ pair it with CPUs that aren't on the allow list? (apologies for the double negative).

Hmm, that's a fair question. I'm not aware of any. So I guess the simplest solution is to remove X4 from the allow list and ditch fourth patch.

That's probably a good idea but I have a horrible feeling we _are_ going to need your patch once the errata start flying about :)

So how about we:

- Remove X4 from the list
- I try harder to apply your patch for secondary CPUs...
- ... if I fail, we can apply it next time around

Sound reasonable?

Will

#31 lore raw
Quoted 54 lines · Will Deacon
Quoted 33 lines · Ryan Roberts
Quoted 32 lines · Will Deacon
Quoted 30 lines · Ryan Roberts
Quoted 17 lines · Will Deacon
Quoted 8 lines · Yang Shi

On systems with BBML2_NOABORT support, it causes the linear map to be mapped with large blocks, even when rodata=full, and leads to some nice performance improvements.

Ryan tested v7 on an AmpereOne system (a VM with 12G RAM) in all 3 possible modes by hacking the BBML2 feature detection code:

[...]

Applied patches 1 and 3 to arm64 (for-next/mm), thanks!

[1/5] arm64: Enable permission change on arm64 kernel block mappings

      https://git.kernel.org/arm64/c/a660194dd101
[3/5] arm64: mm: support large block mapping when rodata=full
      https://git.kernel.org/arm64/c/a166563e7ec3
Quoted 7 lines

I also picked up the BBML allow-list addition (second patch) on for-next/cpufeature.

The fourth patch ("arm64: mm: split linear mapping if BBML2 unsupported on secondary CPUs") has some really horrible conflicts. These are partly due to some of the type cleanups on for-next/mm but I think mainly due to Kevin's kpti rework that landed after -rc1.

Thanks Will, although I'm nervous that without this patch, some platforms might not boot; Wikipedia tells me that there are some Google, Mediatek and Qualcomm SoCs that pair X4 CPUs (which is on the BBML2_NOABORT allow list) with A720 and/or A520 (which are not). See previous mail at [1].

Quoted 4 lines
Quoted 1 line

I'd be surprised if these SoCs are booting on the X4 but who knows.

Ahh. You can probably tell I'm a bit naive to some of this system level stuff... I had assumed they would want to boot on the big CPU to reduce boot time.

One of the problems is that the boot CPU becomes CPU0 and that inevitably means it ends up being responsible for a tonne of extra stuff (interrupts, TZ, etc) and in many cases can't be offlined. So it's all a trade-off.

Quoted 7 lines
Quoted 4 lines

Lemme have another look at applying the patch with fresh eyes, but I do wonder whether having X4 on the allow list really makes any sense. Are there any SoCs out there that _don't_ pair it with CPUs that aren't on the allow list? (apologies for the double negative).

Hmm, that's a fair question. I'm not aware of any. So I guess the simplest solution is to remove X4 from the allow list and ditch fourth patch.

That's probably a good idea but I have a horrible feeling we _are_ going to need your patch once the errata start flying about :)

Quoted 7 lines

So how about we:

- Remove X4 from the list
- I try harder to apply your patch for secondary CPUs...
- ... if I fail, we can apply it next time around

Sound reasonable?

Yeah that works for me. Cheers!

Quoted 2 lines

Will

maintainer
#32 lore raw
Quoted 10 lines · Ryan Roberts
Quoted 7 lines · Will Deacon

So how about we:

- Remove X4 from the list
- I try harder to apply your patch for secondary CPUs...
- ... if I fail, we can apply it next time around

Sound reasonable?

Yeah that works for me. Cheers!

So after all that, the conflict was straightforward once I sat down and looked at it properly.

Please can you check for-next/core? I forcefully triggered the repainting path in qemu and it booted without any problems.

Will

#33 lore raw
Quoted 10 lines · Will Deacon
Quoted 9 lines · Ryan Roberts
Quoted 7 lines · Will Deacon

So how about we:

- Remove X4 from the list
- I try harder to apply your patch for secondary CPUs...
- ... if I fail, we can apply it next time around

Sound reasonable?

Quoted 7 lines
Quoted 1 line

Yeah that works for me. Cheers!

So after all that, the conflict was straightforward once I sat down and looked at it properly.

Please can you check for-next/core? I forcefully triggered the repainting path in qemu and it booted without any problems.

Thanks Will, I took a look and didn't spot any problems. Thanks for squeezing this in.

Quoted 2 lines

Will

author
#34 lore raw
In reply to Will Deacon
Quoted 11 lines · Will Deacon
Quoted 8 lines · Yang Shi

On systems with BBML2_NOABORT support, it causes the linear map to be mapped with large blocks, even when rodata=full, and leads to some nice performance improvements.

Ryan tested v7 on an AmpereOne system (a VM with 12G RAM) in all 3 possible modes by hacking the BBML2 feature detection code:

[...]

Applied patches 1 and 3 to arm64 (for-next/mm), thanks!

Quoted 7 lines

[1/5] arm64: Enable permission change on arm64 kernel block mappings

       https://git.kernel.org/arm64/c/a660194dd101
[3/5] arm64: mm: support large block mapping when rodata=full
       https://git.kernel.org/arm64/c/a166563e7ec3

I also picked up the BBML allow-list addition (second patch) on for-next/cpufeature.

Hi Will,

Thank you so much!

Quoted 8 lines

The fourth patch ("arm64: mm: split linear mapping if BBML2 unsupported on secondary CPUs") has some really horrible conflicts. These are partly due to some of the type cleanups on for-next/mm but I think mainly due to Kevin's kpti rework that landed after -rc1.

So I think the best bet might be to leave that one for next time, if that's ok?

I saw you and Ryan just figured out how to move forward. You guys are definitely more knowledgeable than me regarding the asymmetric systems.

Thanks,
Yang

Quoted 2 lines

Cheers,

#35 lore raw
In reply to Yang Shi
Quoted 3 lines · Yang Shi

On systems with BBML2_NOABORT support, it causes the linear map to be mapped with large blocks, even when rodata=full, and leads to some nice performance improvements.

Hi,

I find this feature is incompatible with realm. The calltrace is as follows:

[    0.000000][    T0] ------------[ cut here ]------------
[    0.000000][    T0] WARNING: CPU: 0 PID: 0 at arch/arm64/mm/pageattr.c:56 pageattr_pmd_entry+0x60/0x78
[    0.000000][    T0] Modules linked in:
[    0.000000][    T0] CPU: 0 PID: 0 Comm: swapper/0 Not tainted 6.6.0 #16
[    0.000000][    T0] Hardware name: linux,dummy-virt (DT)
[    0.000000][    T0] pstate: 800000c5 (Nzcv daIF -PAN -UAO -TCO -DIT -SSBS BTYPE=--)
[    0.000000][    T0] pc : pageattr_pmd_entry+0x60/0x78
[    0.000000][    T0] lr : walk_pmd_range.isra.0+0x170/0x1f0
[    0.000000][    T0] sp : ffffcb90a0f337d0
[    0.000000][    T0] x29: ffffcb90a0f337d0 x28: 0000000000000000 x27: ffff0000035e0000
[    0.000000][    T0] x26: ffffcb90a0f338f8 x25: ffff00001fff60d0 x24: ffff0000035d0000
[    0.000000][    T0] x23: 0400000000000001 x22: 0c00000000000001 x21: ffff0000035dffff
[    0.000000][    T0] x20: ffffcb909fe3b7f0 x19: ffff0000035e0000 x18: ffffffffffffffff
[    0.000000][    T0] x17: 7220303030303178 x16: 307e303030306435 x15: ffffcb90a0f334c8
[    0.000000][    T0] x14: 0000000000000000 x13: 205d305420202020 x12: 5b5d303030303030
[    0.000000][    T0] x11: 00000000ffff7fff x10: 00000000ffff7fff x9 : ffffcb909f1e27d8
[    0.000000][    T0] x8 : 00000000000bffe8 x7 : c0000000ffff7fff x6 : 0000000000000001
[    0.000000][    T0] x5 : 0000000000000001 x4 : 0078000083400705 x3 : ffffcb90a0f338f8
[    0.000000][    T0] x2 : 0000000000010000 x1 : ffff0000035d0000 x0 : ffff00001fff60d0
[    0.000000][    T0] Call trace:
[    0.000000][    T0]  pageattr_pmd_entry+0x60/0x78
[    0.000000][    T0]  walk_pud_range+0x124/0x190
[    0.000000][    T0]  walk_pgd_range+0x158/0x1b0
[    0.000000][    T0]  walk_kernel_page_table_range_lockless+0x58/0x98
[    0.000000][    T0]  update_range_prot+0xb8/0x108
[    0.000000][    T0]  __change_memory_common+0x30/0x1a8
[    0.000000][    T0]  __set_memory_enc_dec.part.0+0x170/0x260
[    0.000000][    T0]  realm_set_memory_decrypted+0x6c/0xb0
[    0.000000][    T0]  set_memory_decrypted+0x38/0x58
[    0.000000][    T0]  its_alloc_pages_node+0xc4/0x140
[    0.000000][    T0]  its_probe_one+0xbc/0x3c0
[    0.000000][    T0]  its_of_probe.isra.0+0x130/0x220
[    0.000000][    T0]  its_init+0x160/0x2f8
[    0.000000][    T0]  gic_init_bases+0x1fc/0x318
[    0.000000][    T0]  gic_of_init+0x2a0/0x300
[    0.000000][    T0]  of_irq_init+0x238/0x4b8
[    0.000000][    T0]  irqchip_init+0x20/0x50
[    0.000000][    T0]  init_IRQ+0x1c/0x100
[    0.000000][    T0]  start_kernel+0x1ec/0x4f0
[    0.000000][    T0]  __primary_switched+0xbc/0xd0
[    0.000000][    T0] ---[ end trace 0000000000000000 ]---
[    0.000000][    T0] ------------[ cut here ]------------
[    0.000000][    T0] Failed to decrypt memory, 16 pages will be leaked

realm feature relies on rodata=full to dynamically update kernel page table prot.

In init_IRQ(), realm_set_memory_decrypted() is called to update kernel page table prot. At this time, secondary cpus aren't booted, BBML2 noabort feature isn't initializated, and system_supports_bbml2_noabort() still returns false. As a result, split_kernel_leaf_mapping() is skipped, leading to WARN_ON_ONCE((next - addr) != PMD_SIZE) in pageattr_pmd_entry().

Before setup_system_features(), we don't know if all cpus support BBML2 noabort, and we couldn't split kernel page table, in case another cpu that doesn't support BBML2 noabort is running.

How could we fix this issue?

1. force pte mapping if realm feature is enabled? Although force_pte_mapping() return true if is_realm_world() return true, arm64_rsi_init() is called after map_mem(). So is_realm_world() still return false during map_mem(). Thus realm feature relies on rodata=full. If we fix by this solution, we need to add a new cmdline to force pte mapping.

2. If we could try to split kernel page table before setup_system_features()?

Thanks.

Quoted 201 lines

Ryan tested v7 on an AmpereOne system (a VM with 12G RAM) in all 3 possible modes by hacking the BBML2 feature detection code:

- mode 1: All CPUs support BBML2 so the linear map uses large mappings
- mode 2: Boot CPU does not support BBML2 so linear map uses pte mappings
- mode 3: Boot CPU supports BBML2 but secondaries do not so linear map initially uses large mappings but is then repainted to use pte mappings

In all cases, mm selftests run and no regressions are observed. In all cases, ptdump of linear map is as expected. Because there are just some cleanups between v7 and v8, so I kept using Ryan's test result:

Mode 1:
=======
---[ Linear Mapping start ]---

0xffff000000000000-0xffff000000200000           2M PMD       RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED
0xffff000000200000-0xffff000000210000          64K PTE       RW NX SHD AF    CON     UXN    MEM/NORMAL-TAGGED
0xffff000000210000-0xffff000000400000        1984K PTE       ro NX SHD AF            UXN    MEM/NORMAL
0xffff000000400000-0xffff000002400000          32M PMD       ro NX SHD AF        BLK UXN    MEM/NORMAL
0xffff000002400000-0xffff000002550000        1344K PTE       ro NX SHD AF            UXN    MEM/NORMAL
0xffff000002550000-0xffff000002600000         704K PTE       RW NX SHD AF    CON     UXN    MEM/NORMAL-TAGGED
0xffff000002600000-0xffff000004000000          26M PMD       RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED
0xffff000004000000-0xffff000040000000         960M PMD       RW NX SHD AF    CON BLK UXN    MEM/NORMAL-TAGGED
0xffff000040000000-0xffff000140000000           4G PUD       RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED
0xffff000140000000-0xffff000142000000          32M PMD       RW NX SHD AF    CON BLK UXN    MEM/NORMAL-TAGGED
0xffff000142000000-0xffff000142120000        1152K PTE       RW NX SHD AF    CON     UXN    MEM/NORMAL-TAGGED
0xffff000142120000-0xffff000142128000          32K PTE       RW NX SHD AF            UXN    MEM/NORMAL-TAGGED
0xffff000142128000-0xffff000142159000         196K PTE       ro NX SHD AF            UXN    MEM/NORMAL-TAGGED
0xffff000142159000-0xffff000142160000          28K PTE       RW NX SHD AF            UXN    MEM/NORMAL-TAGGED
0xffff000142160000-0xffff000142240000         896K PTE       RW NX SHD AF    CON     UXN    MEM/NORMAL-TAGGED
0xffff000142240000-0xffff00014224e000          56K PTE       RW NX SHD AF            UXN    MEM/NORMAL-TAGGED
0xffff00014224e000-0xffff000142250000           8K PTE       ro NX SHD AF            UXN    MEM/NORMAL-TAGGED
0xffff000142250000-0xffff000142260000          64K PTE       RW NX SHD AF            UXN    MEM/NORMAL-TAGGED
0xffff000142260000-0xffff000142280000         128K PTE       RW NX SHD AF    CON     UXN    MEM/NORMAL-TAGGED
0xffff000142280000-0xffff000142288000          32K PTE       RW NX SHD AF            UXN    MEM/NORMAL-TAGGED
0xffff000142288000-0xffff000142290000          32K PTE       ro NX SHD AF            UXN    MEM/NORMAL-TAGGED
0xffff000142290000-0xffff0001422a0000          64K PTE       RW NX SHD AF            UXN    MEM/NORMAL-TAGGED
0xffff0001422a0000-0xffff000142465000        1812K PTE       ro NX SHD AF            UXN    MEM/NORMAL-TAGGED
0xffff000142465000-0xffff000142470000          44K PTE       RW NX SHD AF            UXN    MEM/NORMAL-TAGGED
0xffff000142470000-0xffff000142600000        1600K PTE       RW NX SHD AF    CON     UXN    MEM/NORMAL-TAGGED
0xffff000142600000-0xffff000144000000          26M PMD       RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED
0xffff000144000000-0xffff000180000000         960M PMD       RW NX SHD AF    CON BLK UXN    MEM/NORMAL-TAGGED
0xffff000180000000-0xffff000181a00000          26M PMD       RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED
0xffff000181a00000-0xffff000181b90000        1600K PTE       RW NX SHD AF    CON     UXN    MEM/NORMAL-TAGGED
0xffff000181b90000-0xffff000181b9d000          52K PTE       RW NX SHD AF            UXN    MEM/NORMAL-TAGGED
0xffff000181b9d000-0xffff000181c80000         908K PTE       ro NX SHD AF            UXN    MEM/NORMAL-TAGGED
0xffff000181c80000-0xffff000181c90000          64K PTE       RW NX SHD AF            UXN    MEM/NORMAL-TAGGED
0xffff000181c90000-0xffff000181ca0000          64K PTE       RW NX SHD AF    CON     UXN    MEM/NORMAL-TAGGED
0xffff000181ca0000-0xffff000181dbd000        1140K PTE       ro NX SHD AF            UXN    MEM/NORMAL-TAGGED
0xffff000181dbd000-0xffff000181dc0000          12K PTE       RW NX SHD AF            UXN    MEM/NORMAL-TAGGED
0xffff000181dc0000-0xffff000181e00000         256K PTE       RW NX SHD AF    CON     UXN    MEM/NORMAL-TAGGED
0xffff000181e00000-0xffff000182000000           2M PMD       RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED
0xffff000182000000-0xffff0001c0000000         992M PMD       RW NX SHD AF    CON BLK UXN    MEM/NORMAL-TAGGED
0xffff0001c0000000-0xffff000300000000           5G PUD       RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED

0xffff000300000000-0xffff008000000000 500G PUD
0xffff008000000000-0xffff800000000000 130560G PGD
---[ Linear Mapping end ]---

Mode 3:
=======
---[ Linear Mapping start ]---

0xffff000000000000-0xffff000000210000        2112K PTE       RW NX SHD AF            UXN    MEM/NORMAL-TAGGED
0xffff000000210000-0xffff000000400000        1984K PTE       ro NX SHD AF            UXN    MEM/NORMAL
0xffff000000400000-0xffff000002400000          32M PMD       ro NX SHD AF        BLK UXN    MEM/NORMAL
0xffff000002400000-0xffff000002550000        1344K PTE       ro NX SHD AF            UXN    MEM/NORMAL
0xffff000002550000-0xffff000143a61000     5264452K PTE       RW NX SHD AF            UXN    MEM/NORMAL-TAGGED
0xffff000143a61000-0xffff000143c61000           2M PTE       ro NX SHD AF            UXN    MEM/NORMAL-TAGGED
0xffff000143c61000-0xffff000181b9a000     1015012K PTE       RW NX SHD AF            UXN    MEM/NORMAL-TAGGED
0xffff000181b9a000-0xffff000181d9a000           2M PTE       ro NX SHD AF            UXN    MEM/NORMAL-TAGGED
0xffff000181d9a000-0xffff000300000000     6261144K PTE       RW NX SHD AF            UXN    MEM/NORMAL-TAGGED

0xffff000300000000-0xffff008000000000 500G PUD
0xffff008000000000-0xffff800000000000 130560G PGD
---[ Linear Mapping end ]---

Performance Testing
===================
* Memory use after boot
Before:

MemTotal:       258988984 kB
MemFree:        254821700 kB

After:

MemTotal:       259505132 kB
MemFree:        255410264 kB

Around 500MB more memory are free to use. The larger the machine, the more memory saved.

* Memcached
We saw performance degradation when running Memcached benchmark with rodata=full vs rodata=on. Our profiling pointed to kernel TLB pressure. With this patchset we saw ops/sec is increased by around 3.5%, P99 latency is reduced by around 9.6%.
The gain mainly came from reduced kernel TLB misses. The kernel TLB MPKI is reduced by 28.5%.

The benchmark data is now on par with rodata=on too.

* Disk encryption (dm-crypt) benchmark
Ran fio benchmark with the below command on a 128G ramdisk (ext4) with disk encryption (by dm-crypt).

fio --directory=/data --random_generator=lfsr --norandommap            \
     --randrepeat 1 --status-interval=999 --rw=write --bs=4k --loops=1  \
     --ioengine=sync --iodepth=1 --numjobs=1 --fsync_on_close=1         \
     --group_reporting --thread --name=iops-test-job --eta-newline=1    \
     --size 100G

The IOPS is increased by 90% - 150% (the variance is high, but the worst number of good case is around 90% more than the best number of bad case). The bandwidth is increased and the avg clat is reduced proportionally.

* Sequential file read
Read 100G file sequentially on XFS (xfs_io read with page cache populated). The bandwidth is increased by 150%.

Additionally Ryan also ran this through a random selection of benchmarks on AmpereOne. None show any regressions, and various benchmarks show statistically significant improvement. I'm just showing those improvements here:

+----------------------+----------------------------------------------------------+-------------------------+
| Benchmark            | Result Class                                             | Improvement vs 6.17-rc1 |
+======================+==========================================================+=========================+
| micromm/vmalloc      | full_fit_alloc_test: p:1, h:0, l:500000 (usec)           |              (I) -9.00% |
|                      | kvfree_rcu_1_arg_vmalloc_test: p:1, h:0, l:500000 (usec) |              (I) -6.93% |
|                      | kvfree_rcu_2_arg_vmalloc_test: p:1, h:0, l:500000 (usec) |              (I) -6.77% |
|                      | pcpu_alloc_test: p:1, h:0, l:500000 (usec)               |              (I) -4.63% |
+----------------------+----------------------------------------------------------+-------------------------+
| mmtests/hackbench    | process-sockets-30 (seconds)                             |              (I) -2.96% |
+----------------------+----------------------------------------------------------+-------------------------+
| mmtests/kernbench    | syst-192 (seconds)                                       |             (I) -12.77% |
+----------------------+----------------------------------------------------------+-------------------------+
| pts/perl-benchmark   | Test: Interpreter (Seconds)                              |              (I) -4.86% |
+----------------------+----------------------------------------------------------+-------------------------+
| pts/pgbench          | Scale: 1 Clients: 1 Read Write (TPS)                     |               (I) 5.07% |
|                      | Scale: 1 Clients: 1 Read Write - Latency (ms)            |              (I) -4.72% |
|                      | Scale: 100 Clients: 1000 Read Write (TPS)                |               (I) 2.58% |
|                      | Scale: 100 Clients: 1000 Read Write - Latency (ms)       |              (I) -2.52% |
+----------------------+----------------------------------------------------------+-------------------------+
| pts/sqlite-speedtest | Timed Time - Size 1,000 (Seconds)                        |              (I) -2.68% |
+----------------------+----------------------------------------------------------+-------------------------+

Changes since v7 [1]
====================
- Rebased on v6.17-rc6 and Shijie's rodata series (https://git.kernel.org/pub/scm/linux/kernel/git/arm64/linux.git/commit/?id=bfbbb0d3215f)
which has been picked up by Will.
- Patch 1: Fixed pmd_leaf/pud_leaf issue since the code may need to change
permission for invalid entries per Jinjiang Tu.
- Patch 1: Removed pageattr_pgd_entry and pageattr_p4d_entry per Ryan.
- Used (-1ULL) instead of -1 per Catalin.
- Added comment about arm64 lazy mmu allow sleeping per Ryan.
- Squashed patch #4 in v7 into patch #3.
- Squashed patch #6 in v7 into patch #4.
- Added patch #5 to fix a arm64 kprobes bug. It guarantees set_memory_rox()
is called before vfree(). It can go into separately or with this series
together.
- Collected all the R-bs and A-bs.

Changes since v6 [2]
====================
- Patch 1: Minor refactor to implement walk_kernel_page_table_range() in terms of walk_kernel_page_table_range_lockless(). Also lead to adding *pmd argument to the lockless variant for consistency (per Catalin).
- Misc function/variable renames to improve clarity and consistency.
- Share same syncrhonization flag between idmap_kpti_install_ng_mappings and wait_linear_map_split_to_ptes, which allows removal of bbml2_ptes[] to save ~20K from kernel image.
- Only take pgtable_split_lock and enter lazy mmu mode once for both splits.
- Only walk the pgtable once for the common "split single page" case.
- Bypass split to contpmd and contpte when spllitting linear map to ptes.

[1] https://lore.kernel.org/linux-arm-kernel/[email protected]/
[2] https://lore.kernel.org/linux-arm-kernel/[email protected]/

Dev Jain (1):
arm64: Enable permission change on arm64 kernel block mappings

Ryan Roberts (1):
arm64: mm: split linear mapping if BBML2 unsupported on secondary CPUs

Yang Shi (3):
arm64: cpufeature: add AmpereOne to BBML2 allow list
arm64: mm: support large block mapping when rodata=full
arm64: kprobes: call set_memory_rox() for kprobe page

  arch/arm64/include/asm/cpufeature.h |   2 +
  arch/arm64/include/asm/mmu.h        |   3 +
  arch/arm64/include/asm/pgtable.h    |   5 ++
  arch/arm64/kernel/cpufeature.c      |  12 +++-
  arch/arm64/kernel/probes/kprobes.c  |  12 ++++
  arch/arm64/mm/mmu.c                 | 422 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----
  arch/arm64/mm/pageattr.c            | 123 ++++++++++++++++++++++++---------
  arch/arm64/mm/proc.S                |  27 ++++++--
  include/linux/pagewalk.h            |   3 +
  mm/pagewalk.c                       |  36 ++++++----
  10 files changed, 581 insertions(+), 64 deletions(-)
#36 lore raw

Thanks for the report!

+ Kevin, who was looking at some adjacent issues and may have some ideas for how to fix.

Quoted 68 lines · Jinjiang Tu
Quoted 3 lines · Yang Shi

On systems with BBML2_NOABORT support, it causes the linear map to be mapped with large blocks, even when rodata=full, and leads to some nice performance improvements.

Hi,

I find this feature is incompatible with realm. The calltrace is as follows:

[    0.000000][    T0] ------------[ cut here ]------------
[    0.000000][    T0] WARNING: CPU: 0 PID: 0 at arch/arm64/mm/pageattr.c:56 pageattr_pmd_entry+0x60/0x78
[    0.000000][    T0] Modules linked in:
[    0.000000][    T0] CPU: 0 PID: 0 Comm: swapper/0 Not tainted 6.6.0 #16 [    0.000000][    T0] Hardware name: linux,dummy-virt (DT)
[    0.000000][    T0] pstate: 800000c5 (Nzcv daIF -PAN -UAO -TCO -DIT -SSBS BTYPE=--)
[    0.000000][    T0] pc : pageattr_pmd_entry+0x60/0x78
[    0.000000][    T0] lr : walk_pmd_range.isra.0+0x170/0x1f0
[    0.000000][    T0] sp : ffffcb90a0f337d0
[    0.000000][    T0] x29: ffffcb90a0f337d0 x28: 0000000000000000 x27:
ffff0000035e0000
[    0.000000][    T0] x26: ffffcb90a0f338f8 x25: ffff00001fff60d0 x24:
ffff0000035d0000
[    0.000000][    T0] x23: 0400000000000001 x22: 0c00000000000001 x21:
ffff0000035dffff
[    0.000000][    T0] x20: ffffcb909fe3b7f0 x19: ffff0000035e0000 x18:
ffffffffffffffff
[    0.000000][    T0] x17: 7220303030303178 x16: 307e303030306435 x15:
ffffcb90a0f334c8
[    0.000000][    T0] x14: 0000000000000000 x13: 205d305420202020 x12:
5b5d303030303030
[    0.000000][    T0] x11: 00000000ffff7fff x10: 00000000ffff7fff x9 :
ffffcb909f1e27d8
[    0.000000][    T0] x8 : 00000000000bffe8 x7 : c0000000ffff7fff x6 :
0000000000000001
[    0.000000][    T0] x5 : 0000000000000001 x4 : 0078000083400705 x3 :
ffffcb90a0f338f8
[    0.000000][    T0] x2 : 0000000000010000 x1 : ffff0000035d0000 x0 :
ffff00001fff60d0
[    0.000000][    T0] Call trace:
[    0.000000][    T0]  pageattr_pmd_entry+0x60/0x78
[    0.000000][    T0]  walk_pud_range+0x124/0x190
[    0.000000][    T0]  walk_pgd_range+0x158/0x1b0
[    0.000000][    T0]  walk_kernel_page_table_range_lockless+0x58/0x98 [    0.000000][    T0]  update_range_prot+0xb8/0x108
[    0.000000][    T0]  __change_memory_common+0x30/0x1a8
[    0.000000][    T0]  __set_memory_enc_dec.part.0+0x170/0x260 [    0.000000][    T0]  realm_set_memory_decrypted+0x6c/0xb0
[    0.000000][    T0]  set_memory_decrypted+0x38/0x58
[    0.000000][    T0]  its_alloc_pages_node+0xc4/0x140
[    0.000000][    T0]  its_probe_one+0xbc/0x3c0
[    0.000000][    T0]  its_of_probe.isra.0+0x130/0x220
[    0.000000][    T0]  its_init+0x160/0x2f8
[    0.000000][    T0]  gic_init_bases+0x1fc/0x318
[    0.000000][    T0]  gic_of_init+0x2a0/0x300
[    0.000000][    T0]  of_irq_init+0x238/0x4b8
[    0.000000][    T0]  irqchip_init+0x20/0x50
[    0.000000][    T0]  init_IRQ+0x1c/0x100
[    0.000000][    T0]  start_kernel+0x1ec/0x4f0
[    0.000000][    T0]  __primary_switched+0xbc/0xd0
[    0.000000][    T0] ---[ end trace 0000000000000000 ]---
[    0.000000][    T0] ------------[ cut here ]------------
[    0.000000][    T0] Failed to decrypt memory, 16 pages will be leaked

realm feature relies on rodata=full to dynamically update kernel page table prot.

Quoted 8 lines

In init_IRQ(), realm_set_memory_decrypted() is called to update kernel page table prot.
At this time, secondary cpus aren't booted, BBML2 noabort feature isn't initializated,
and system_supports_bbml2_noabort() still returns false. As a result, split_kernel_leaf_mapping() is skipped, leading to WARN_ON_ONCE((next - addr) != PMD_SIZE)
in pageattr_pmd_entry().

If no secondary cpus are yet running, then it is technically safe to split because we know all online cpus (i.e. just the boot cpu) supports BBML2_NOABORT. So we could explicitly only disallow splitting during the window between booting secondary cpus and finalizing the system caps. Feels a bit hacky though...

Quoted 7 lines

Before setup_system_features(), we don't know if all cpus support BBML2 noabort, and we
couldn't split kernel page table, in case another cpu that doesn't support BBML2 noabort
is running.

Quoted 7 lines

How could we fix this issue?

1. force pte mapping if realm feature is enabled? Although force_pte_mapping() return true if is_realm_world() return true, arm64_rsi_init() is called after map_mem(). So is_realm_world() still return false during map_mem(). Thus realm feature relies on rodata=full. If we fix by this solution, we need to add a new cmdline to force pte mapping.

I think we just need to make is_realm_world() work earlier in boot? I think this has been a known issue for a while. Not sure if there is any plan to fix it though.

Quoted 2 lines

2. If we could try to split kernel page table before setup_system_features()?

Another option would be to initially map by pte then collapse to block mappings once we have determined that all cpus support BBML2_NOABORT. We originally opted not to do that because it's a tax on symetric systems. But we could throw in the towel if it's the least bad solution we can come up with for solving this. I think it might help some of Kevin's use cases too?

Thanks,
Ryan

Quoted 283 lines

Thanks.

Quoted 280 lines

Ryan tested v7 on an AmpereOne system (a VM with 12G RAM) in all 3 possible modes by hacking the BBML2 feature detection code:

   - mode 1: All CPUs support BBML2 so the linear map uses large mappings
   - mode 2: Boot CPU does not support BBML2 so linear map uses pte mappings
   - mode 3: Boot CPU supports BBML2 but secondaries do not so linear map initially uses large mappings but is then repainted to use pte mappings

In all cases, mm selftests run and no regressions are observed. In all cases, ptdump of linear map is as expected. Because there are just some cleanups between v7 and v8, so I kept using Ryan's test result:

Mode 1:
=======
---[ Linear Mapping start ]--- 0xffff000000000000-0xffff000000200000           2M PMD       RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED 0xffff000000200000-0xffff000000210000          64K PTE       RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000000210000-0xffff000000400000        1984K PTE       ro NX SHD AF            UXN    MEM/NORMAL 0xffff000000400000-0xffff000002400000          32M PMD       ro NX SHD AF        BLK UXN    MEM/NORMAL 0xffff000002400000-0xffff000002550000        1344K PTE       ro NX SHD AF            UXN    MEM/NORMAL 0xffff000002550000-0xffff000002600000         704K PTE       RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000002600000-0xffff000004000000          26M PMD       RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED 0xffff000004000000-0xffff000040000000         960M PMD       RW NX SHD AF CON BLK UXN    MEM/NORMAL-TAGGED 0xffff000040000000-0xffff000140000000           4G PUD       RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED 0xffff000140000000-0xffff000142000000          32M PMD       RW NX SHD AF CON BLK UXN    MEM/NORMAL-TAGGED 0xffff000142000000-0xffff000142120000        1152K PTE       RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000142120000-0xffff000142128000          32K PTE       RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142128000-0xffff000142159000         196K PTE       ro NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142159000-0xffff000142160000          28K PTE       RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142160000-0xffff000142240000         896K PTE       RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000142240000-0xffff00014224e000          56K PTE       RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff00014224e000-0xffff000142250000           8K PTE       ro NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142250000-0xffff000142260000          64K PTE       RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142260000-0xffff000142280000         128K PTE       RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000142280000-0xffff000142288000          32K PTE       RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142288000-0xffff000142290000          32K PTE       ro NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142290000-0xffff0001422a0000          64K PTE       RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff0001422a0000-0xffff000142465000        1812K PTE       ro NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142465000-0xffff000142470000          44K PTE       RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142470000-0xffff000142600000        1600K PTE       RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000142600000-0xffff000144000000          26M PMD       RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED 0xffff000144000000-0xffff000180000000         960M PMD       RW NX SHD AF CON BLK UXN    MEM/NORMAL-TAGGED 0xffff000180000000-0xffff000181a00000          26M PMD       RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED 0xffff000181a00000-0xffff000181b90000        1600K PTE       RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000181b90000-0xffff000181b9d000          52K PTE       RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000181b9d000-0xffff000181c80000         908K PTE       ro NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000181c80000-0xffff000181c90000          64K PTE       RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000181c90000-0xffff000181ca0000          64K PTE       RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000181ca0000-0xffff000181dbd000        1140K PTE       ro NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000181dbd000-0xffff000181dc0000          12K PTE       RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000181dc0000-0xffff000181e00000         256K PTE       RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000181e00000-0xffff000182000000           2M PMD       RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED 0xffff000182000000-0xffff0001c0000000         992M PMD       RW NX SHD AF CON BLK UXN    MEM/NORMAL-TAGGED 0xffff0001c0000000-0xffff000300000000           5G PUD       RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED 0xffff000300000000-0xffff008000000000         500G PUD 0xffff008000000000-0xffff800000000000      130560G PGD
---[ Linear Mapping end ]---

Mode 3:
=======
---[ Linear Mapping start ]--- 0xffff000000000000-0xffff000000210000        2112K PTE       RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000000210000-0xffff000000400000        1984K PTE       ro NX SHD AF            UXN    MEM/NORMAL 0xffff000000400000-0xffff000002400000          32M PMD       ro NX SHD AF        BLK UXN    MEM/NORMAL 0xffff000002400000-0xffff000002550000        1344K PTE       ro NX SHD AF            UXN    MEM/NORMAL 0xffff000002550000-0xffff000143a61000     5264452K PTE       RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000143a61000-0xffff000143c61000           2M PTE       ro NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000143c61000-0xffff000181b9a000     1015012K PTE       RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000181b9a000-0xffff000181d9a000           2M PTE       ro NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000181d9a000-0xffff000300000000     6261144K PTE       RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000300000000-0xffff008000000000         500G PUD 0xffff008000000000-0xffff800000000000      130560G PGD
---[ Linear Mapping end ]---

Performance Testing
===================
* Memory use after boot
Before:
MemTotal:       258988984 kB
MemFree:        254821700 kB

After:
MemTotal:       259505132 kB
MemFree:        255410264 kB

Around 500MB more memory are free to use.  The larger the machine, the more memory saved.

* Memcached
We saw performance degradation when running Memcached benchmark with rodata=full vs rodata=on.  Our profiling pointed to kernel TLB pressure. With this patchset we saw ops/sec is increased by around 3.5%, P99 latency is reduced by around 9.6%.
The gain mainly came from reduced kernel TLB misses.  The kernel TLB MPKI is reduced by 28.5%.

The benchmark data is now on par with rodata=on too.

* Disk encryption (dm-crypt) benchmark
Ran fio benchmark with the below command on a 128G ramdisk (ext4) with disk encryption (by dm-crypt).
fio --directory=/data --random_generator=lfsr --norandommap            \
     --randrepeat 1 --status-interval=999 --rw=write --bs=4k --loops=1  \
     --ioengine=sync --iodepth=1 --numjobs=1 --fsync_on_close=1         \
     --group_reporting --thread --name=iops-test-job --eta-newline=1    \
     --size 100G

The IOPS is increased by 90% - 150% (the variance is high, but the worst number of good case is around 90% more than the best number of bad case). The bandwidth is increased and the avg clat is reduced proportionally.

* Sequential file read
Read 100G file sequentially on XFS (xfs_io read with page cache populated). The bandwidth is increased by 150%.

Additionally Ryan also ran this through a random selection of benchmarks on AmpereOne. None show any regressions, and various benchmarks show statistically significant improvement. I'm just showing those improvements here:

+----------------------
+----------------------------------------------------------
+-------------------------+
| Benchmark            | Result
Class                                             | Improvement vs 6.17-rc1 |
+======================+==========================================================+=========================+
| micromm/vmalloc      | full_fit_alloc_test: p:1, h:0, l:500000
(usec)           |              (I) -9.00% |
|                      | kvfree_rcu_1_arg_vmalloc_test: p:1, h:0, l:500000
(usec) |              (I) -6.93% |
|                      | kvfree_rcu_2_arg_vmalloc_test: p:1, h:0, l:500000
(usec) |              (I) -6.77% |
|                      | pcpu_alloc_test: p:1, h:0, l:500000
(usec)               |              (I) -4.63% |
+----------------------
+----------------------------------------------------------
+-------------------------+
| mmtests/hackbench    | process-sockets-30
(seconds)                             |              (I) -2.96% |
+----------------------
+----------------------------------------------------------
+-------------------------+
| mmtests/kernbench    | syst-192
(seconds)                                       |             (I) -12.77% |
+----------------------
+----------------------------------------------------------
+-------------------------+
| pts/perl-benchmark   | Test: Interpreter
(Seconds)                              |              (I) -4.86% |
+----------------------
+----------------------------------------------------------
+-------------------------+
| pts/pgbench          | Scale: 1 Clients: 1 Read Write
(TPS)                     |               (I) 5.07% |
|                      | Scale: 1 Clients: 1 Read Write - Latency
(ms)            |              (I) -4.72% |
|                      | Scale: 100 Clients: 1000 Read Write
(TPS)                |               (I) 2.58% |
|                      | Scale: 100 Clients: 1000 Read Write - Latency
(ms)       |              (I) -2.52% |
+----------------------
+----------------------------------------------------------
+-------------------------+
| pts/sqlite-speedtest | Timed Time - Size 1,000
(Seconds)                        |              (I) -2.68% |
+----------------------
+----------------------------------------------------------
+-------------------------+

Changes since v7 [1]
====================
- Rebased on v6.17-rc6 and Shijie's rodata series (https://git.kernel.org/pub/
scm/linux/kernel/git/arm64/linux.git/commit/?id=bfbbb0d3215f) which has been picked up by Will.
- Patch 1: Fixed pmd_leaf/pud_leaf issue since the code may need to change permission for invalid entries per Jinjiang Tu.
- Patch 1: Removed pageattr_pgd_entry and pageattr_p4d_entry per Ryan.
- Used (-1ULL) instead of -1 per Catalin.
- Added comment about arm64 lazy mmu allow sleeping per Ryan.
- Squashed patch #4 in v7 into patch #3.
- Squashed patch #6 in v7 into patch #4.
- Added patch #5 to fix a arm64 kprobes bug. It guarantees set_memory_rox() is called before vfree(). It can go into separately or with this series together.
- Collected all the R-bs and A-bs.

Changes since v6 [2]
====================
- Patch 1: Minor refactor to implement walk_kernel_page_table_range() in terms of walk_kernel_page_table_range_lockless(). Also lead to adding *pmd argument to the lockless variant for consistency (per Catalin).
- Misc function/variable renames to improve clarity and consistency.
- Share same syncrhonization flag between idmap_kpti_install_ng_mappings and wait_linear_map_split_to_ptes, which allows removal of bbml2_ptes[] to save ~20K from kernel image.
- Only take pgtable_split_lock and enter lazy mmu mode once for both splits.
- Only walk the pgtable once for the common "split single page" case.
- Bypass split to contpmd and contpte when spllitting linear map to ptes.

[1] https://lore.kernel.org/linux-arm-kernel/20250829115250.2395585-1-
[email protected]/
[2] https://lore.kernel.org/linux-arm-kernel/20250805081350.3854670-1-
[email protected]/

Dev Jain (1):
       arm64: Enable permission change on arm64 kernel block mappings

Ryan Roberts (1):
       arm64: mm: split linear mapping if BBML2 unsupported on secondary CPUs

Yang Shi (3):
       arm64: cpufeature: add AmpereOne to BBML2 allow list
       arm64: mm: support large block mapping when rodata=full
       arm64: kprobes: call set_memory_rox() for kprobe page

  arch/arm64/include/asm/cpufeature.h |   2 + arch/arm64/include/asm/mmu.h        |   3 + arch/arm64/include/asm/pgtable.h    |   5 ++ arch/arm64/kernel/cpufeature.c      |  12 +++- arch/arm64/kernel/probes/kprobes.c  |  12 ++++ arch/arm64/mm/mmu.c                 | 422 ++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----
  arch/arm64/mm/pageattr.c            | 123 ++++++++++++++++++++++++--------- arch/arm64/mm/proc.S                |  27 ++++++-- include/linux/pagewalk.h            |   3 +
  mm/pagewalk.c                       |  36 ++++++----

  10 files changed, 581 insertions(+), 64 deletions(-)
author
#37 lore raw
Quoted 12 lines · Ryan Roberts

Thanks for the report!

+ Kevin, who was looking at some adjacent issues and may have some ideas for how to fix.

Quoted 5 lines · Jinjiang Tu
Quoted 3 lines · Yang Shi

On systems with BBML2_NOABORT support, it causes the linear map to be mapped with large blocks, even when rodata=full, and leads to some nice performance improvements.

Hi,

Hi Jinjiang,

Thanks for reporting the problem.

Quoted 69 lines
Quoted 61 lines

I find this feature is incompatible with realm. The calltrace is as follows:

[    0.000000][    T0] ------------[ cut here ]------------
[    0.000000][    T0] WARNING: CPU: 0 PID: 0 at arch/arm64/mm/pageattr.c:56 pageattr_pmd_entry+0x60/0x78
[    0.000000][    T0] Modules linked in:
[    0.000000][    T0] CPU: 0 PID: 0 Comm: swapper/0 Not tainted 6.6.0 #16 [    0.000000][    T0] Hardware name: linux,dummy-virt (DT)
[    0.000000][    T0] pstate: 800000c5 (Nzcv daIF -PAN -UAO -TCO -DIT -SSBS BTYPE=--)
[    0.000000][    T0] pc : pageattr_pmd_entry+0x60/0x78
[    0.000000][    T0] lr : walk_pmd_range.isra.0+0x170/0x1f0
[    0.000000][    T0] sp : ffffcb90a0f337d0
[    0.000000][    T0] x29: ffffcb90a0f337d0 x28: 0000000000000000 x27:
ffff0000035e0000
[    0.000000][    T0] x26: ffffcb90a0f338f8 x25: ffff00001fff60d0 x24:
ffff0000035d0000
[    0.000000][    T0] x23: 0400000000000001 x22: 0c00000000000001 x21:
ffff0000035dffff
[    0.000000][    T0] x20: ffffcb909fe3b7f0 x19: ffff0000035e0000 x18:
ffffffffffffffff
[    0.000000][    T0] x17: 7220303030303178 x16: 307e303030306435 x15:
ffffcb90a0f334c8
[    0.000000][    T0] x14: 0000000000000000 x13: 205d305420202020 x12:
5b5d303030303030
[    0.000000][    T0] x11: 00000000ffff7fff x10: 00000000ffff7fff x9 :
ffffcb909f1e27d8
[    0.000000][    T0] x8 : 00000000000bffe8 x7 : c0000000ffff7fff x6 :
0000000000000001
[    0.000000][    T0] x5 : 0000000000000001 x4 : 0078000083400705 x3 :
ffffcb90a0f338f8
[    0.000000][    T0] x2 : 0000000000010000 x1 : ffff0000035d0000 x0 :
ffff00001fff60d0
[    0.000000][    T0] Call trace:
[    0.000000][    T0]  pageattr_pmd_entry+0x60/0x78
[    0.000000][    T0]  walk_pud_range+0x124/0x190
[    0.000000][    T0]  walk_pgd_range+0x158/0x1b0
[    0.000000][    T0]  walk_kernel_page_table_range_lockless+0x58/0x98 [    0.000000][    T0]  update_range_prot+0xb8/0x108
[    0.000000][    T0]  __change_memory_common+0x30/0x1a8
[    0.000000][    T0]  __set_memory_enc_dec.part.0+0x170/0x260 [    0.000000][    T0]  realm_set_memory_decrypted+0x6c/0xb0
[    0.000000][    T0]  set_memory_decrypted+0x38/0x58
[    0.000000][    T0]  its_alloc_pages_node+0xc4/0x140
[    0.000000][    T0]  its_probe_one+0xbc/0x3c0
[    0.000000][    T0]  its_of_probe.isra.0+0x130/0x220
[    0.000000][    T0]  its_init+0x160/0x2f8
[    0.000000][    T0]  gic_init_bases+0x1fc/0x318
[    0.000000][    T0]  gic_of_init+0x2a0/0x300
[    0.000000][    T0]  of_irq_init+0x238/0x4b8
[    0.000000][    T0]  irqchip_init+0x20/0x50
[    0.000000][    T0]  init_IRQ+0x1c/0x100
[    0.000000][    T0]  start_kernel+0x1ec/0x4f0
[    0.000000][    T0]  __primary_switched+0xbc/0xd0
[    0.000000][    T0] ---[ end trace 0000000000000000 ]---
[    0.000000][    T0] ------------[ cut here ]------------
[    0.000000][    T0] Failed to decrypt memory, 16 pages will be leaked

realm feature relies on rodata=full to dynamically update kernel page table prot.

Quoted 8 lines

In init_IRQ(), realm_set_memory_decrypted() is called to update kernel page table prot.
At this time, secondary cpus aren't booted, BBML2 noabort feature isn't initializated,
and system_supports_bbml2_noabort() still returns false. As a result, split_kernel_leaf_mapping() is skipped, leading to WARN_ON_ONCE((next - addr) != PMD_SIZE)
in pageattr_pmd_entry().

Quoted 4 lines

If no secondary cpus are yet running, then it is technically safe to split because we know all online cpus (i.e. just the boot cpu) supports BBML2_NOABORT. So we could explicitly only disallow splitting during the window between booting secondary cpus and finalizing the system caps. Feels a bit hacky though...

I think we can check whether system feature has been finalized or not. If it has not been finalized yet, we just need to check whether the current cpu (should be just boot cpu) supports BBML2_NOABORT or not. It sounds ok to me.

Quoted 7 lines
Quoted 7 lines

Before setup_system_features(), we don't know if all cpus support BBML2 noabort, and we
couldn't split kernel page table, in case another cpu that doesn't support BBML2 noabort
is running.

Quoted 7 lines
Quoted 7 lines

How could we fix this issue?

1. force pte mapping if realm feature is enabled? Although force_pte_mapping() return true if is_realm_world() return true, arm64_rsi_init() is called after map_mem(). So is_realm_world() still return false during map_mem(). Thus realm feature relies on rodata=full. If we fix by this solution, we need to add a new cmdline to force pte mapping.

I don't quite get why is_realm_world() relies on rodata=full. I understand realm needs PTE mapping if BBML2_NOABORT is not supported. But it doesn't mean real relies on rodata=full.

Quoted 10 lines

I think we just need to make is_realm_world() work earlier in boot? I think this has been a known issue for a while. Not sure if there is any plan to fix it though.

Quoted 1 line

2. If we could try to split kernel page table before setup_system_features()?

Another option would be to initially map by pte then collapse to block mappings once we have determined that all cpus support BBML2_NOABORT. We originally opted not to do that because it's a tax on symetric systems. But we could throw in the towel if it's the least bad solution we can come up with for solving this. I think it might help some of Kevin's use cases too?

May be an option too. When we discussed this there was no usecase for direct mapping collapse. But if we can have multiple usecases, it may be worth it. AFAICT, the ROX execmem cache may need this, which Will or someone else from Google is going to work on.

Checking current cpu BBML2_NOABORT capability before system feature is finalized seems like a fast way to stop bleeding IMHO before we find more elegant long-term solution.

Thanks,
Yang

Quoted 286 lines

Thanks,
Ryan

Quoted 281 lines

Thanks.

Quoted 279 lines

Ryan tested v7 on an AmpereOne system (a VM with 12G RAM) in all 3 possible modes by hacking the BBML2 feature detection code:

   - mode 1: All CPUs support BBML2 so the linear map uses large mappings
   - mode 2: Boot CPU does not support BBML2 so linear map uses pte mappings
   - mode 3: Boot CPU supports BBML2 but secondaries do not so linear map initially uses large mappings but is then repainted to use pte mappings

In all cases, mm selftests run and no regressions are observed. In all cases, ptdump of linear map is as expected. Because there are just some cleanups between v7 and v8, so I kept using Ryan's test result:

Mode 1:
=======
---[ Linear Mapping start ]--- 0xffff000000000000-0xffff000000200000           2M PMD       RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED 0xffff000000200000-0xffff000000210000          64K PTE       RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000000210000-0xffff000000400000        1984K PTE       ro NX SHD AF            UXN    MEM/NORMAL 0xffff000000400000-0xffff000002400000          32M PMD       ro NX SHD AF        BLK UXN    MEM/NORMAL 0xffff000002400000-0xffff000002550000        1344K PTE       ro NX SHD AF            UXN    MEM/NORMAL 0xffff000002550000-0xffff000002600000         704K PTE       RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000002600000-0xffff000004000000          26M PMD       RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED 0xffff000004000000-0xffff000040000000         960M PMD       RW NX SHD AF CON BLK UXN    MEM/NORMAL-TAGGED 0xffff000040000000-0xffff000140000000           4G PUD       RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED 0xffff000140000000-0xffff000142000000          32M PMD       RW NX SHD AF CON BLK UXN    MEM/NORMAL-TAGGED 0xffff000142000000-0xffff000142120000        1152K PTE       RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000142120000-0xffff000142128000          32K PTE       RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142128000-0xffff000142159000         196K PTE       ro NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142159000-0xffff000142160000          28K PTE       RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142160000-0xffff000142240000         896K PTE       RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000142240000-0xffff00014224e000          56K PTE       RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff00014224e000-0xffff000142250000           8K PTE       ro NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142250000-0xffff000142260000          64K PTE       RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142260000-0xffff000142280000         128K PTE       RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000142280000-0xffff000142288000          32K PTE       RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142288000-0xffff000142290000          32K PTE       ro NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142290000-0xffff0001422a0000          64K PTE       RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff0001422a0000-0xffff000142465000        1812K PTE       ro NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142465000-0xffff000142470000          44K PTE       RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142470000-0xffff000142600000        1600K PTE       RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000142600000-0xffff000144000000          26M PMD       RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED 0xffff000144000000-0xffff000180000000         960M PMD       RW NX SHD AF CON BLK UXN    MEM/NORMAL-TAGGED 0xffff000180000000-0xffff000181a00000          26M PMD       RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED 0xffff000181a00000-0xffff000181b90000        1600K PTE       RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000181b90000-0xffff000181b9d000          52K PTE       RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000181b9d000-0xffff000181c80000         908K PTE       ro NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000181c80000-0xffff000181c90000          64K PTE       RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000181c90000-0xffff000181ca0000          64K PTE       RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000181ca0000-0xffff000181dbd000        1140K PTE       ro NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000181dbd000-0xffff000181dc0000          12K PTE       RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000181dc0000-0xffff000181e00000         256K PTE       RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000181e00000-0xffff000182000000           2M PMD       RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED 0xffff000182000000-0xffff0001c0000000         992M PMD       RW NX SHD AF CON BLK UXN    MEM/NORMAL-TAGGED 0xffff0001c0000000-0xffff000300000000           5G PUD       RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED 0xffff000300000000-0xffff008000000000         500G PUD 0xffff008000000000-0xffff800000000000      130560G PGD
---[ Linear Mapping end ]---

Mode 3:
=======
---[ Linear Mapping start ]--- 0xffff000000000000-0xffff000000210000        2112K PTE       RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000000210000-0xffff000000400000        1984K PTE       ro NX SHD AF            UXN    MEM/NORMAL 0xffff000000400000-0xffff000002400000          32M PMD       ro NX SHD AF        BLK UXN    MEM/NORMAL 0xffff000002400000-0xffff000002550000        1344K PTE       ro NX SHD AF            UXN    MEM/NORMAL 0xffff000002550000-0xffff000143a61000     5264452K PTE       RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000143a61000-0xffff000143c61000           2M PTE       ro NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000143c61000-0xffff000181b9a000     1015012K PTE       RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000181b9a000-0xffff000181d9a000           2M PTE       ro NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000181d9a000-0xffff000300000000     6261144K PTE       RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000300000000-0xffff008000000000         500G PUD 0xffff008000000000-0xffff800000000000      130560G PGD
---[ Linear Mapping end ]---

Performance Testing
===================
* Memory use after boot
Before:
MemTotal:       258988984 kB
MemFree:        254821700 kB

After:
MemTotal:       259505132 kB
MemFree:        255410264 kB

Around 500MB more memory are free to use.  The larger the machine, the more memory saved.

* Memcached
We saw performance degradation when running Memcached benchmark with rodata=full vs rodata=on.  Our profiling pointed to kernel TLB pressure. With this patchset we saw ops/sec is increased by around 3.5%, P99 latency is reduced by around 9.6%.
The gain mainly came from reduced kernel TLB misses.  The kernel TLB MPKI is reduced by 28.5%.

The benchmark data is now on par with rodata=on too.

* Disk encryption (dm-crypt) benchmark
Ran fio benchmark with the below command on a 128G ramdisk (ext4) with disk encryption (by dm-crypt).
fio --directory=/data --random_generator=lfsr --norandommap            \
     --randrepeat 1 --status-interval=999 --rw=write --bs=4k --loops=1  \
     --ioengine=sync --iodepth=1 --numjobs=1 --fsync_on_close=1         \
     --group_reporting --thread --name=iops-test-job --eta-newline=1    \
     --size 100G

The IOPS is increased by 90% - 150% (the variance is high, but the worst number of good case is around 90% more than the best number of bad case). The bandwidth is increased and the avg clat is reduced proportionally.

* Sequential file read
Read 100G file sequentially on XFS (xfs_io read with page cache populated). The bandwidth is increased by 150%.

Additionally Ryan also ran this through a random selection of benchmarks on AmpereOne. None show any regressions, and various benchmarks show statistically significant improvement. I'm just showing those improvements here:

+----------------------
+----------------------------------------------------------
+-------------------------+
| Benchmark            | Result
Class                                             | Improvement vs 6.17-rc1 |
+======================+==========================================================+=========================+
| micromm/vmalloc      | full_fit_alloc_test: p:1, h:0, l:500000
(usec)           |              (I) -9.00% |
|                      | kvfree_rcu_1_arg_vmalloc_test: p:1, h:0, l:500000
(usec) |              (I) -6.93% |
|                      | kvfree_rcu_2_arg_vmalloc_test: p:1, h:0, l:500000
(usec) |              (I) -6.77% |
|                      | pcpu_alloc_test: p:1, h:0, l:500000
(usec)               |              (I) -4.63% |
+----------------------
+----------------------------------------------------------
+-------------------------+
| mmtests/hackbench    | process-sockets-30
(seconds)                             |              (I) -2.96% |
+----------------------
+----------------------------------------------------------
+-------------------------+
| mmtests/kernbench    | syst-192
(seconds)                                       |             (I) -12.77% |
+----------------------
+----------------------------------------------------------
+-------------------------+
| pts/perl-benchmark   | Test: Interpreter
(Seconds)                              |              (I) -4.86% |
+----------------------
+----------------------------------------------------------
+-------------------------+
| pts/pgbench          | Scale: 1 Clients: 1 Read Write
(TPS)                     |               (I) 5.07% |
|                      | Scale: 1 Clients: 1 Read Write - Latency
(ms)            |              (I) -4.72% |
|                      | Scale: 100 Clients: 1000 Read Write
(TPS)                |               (I) 2.58% |
|                      | Scale: 100 Clients: 1000 Read Write - Latency
(ms)       |              (I) -2.52% |
+----------------------
+----------------------------------------------------------
+-------------------------+
| pts/sqlite-speedtest | Timed Time - Size 1,000
(Seconds)                        |              (I) -2.68% |
+----------------------
+----------------------------------------------------------
+-------------------------+

Changes since v7 [1]
====================
- Rebased on v6.17-rc6 and Shijie's rodata series (https://git.kernel.org/pub/
scm/linux/kernel/git/arm64/linux.git/commit/?id=bfbbb0d3215f)
   which has been picked up by Will.
- Patch 1: Fixed pmd_leaf/pud_leaf issue since the code may need to change permission for invalid entries per Jinjiang Tu.
- Patch 1: Removed pageattr_pgd_entry and pageattr_p4d_entry per Ryan.
- Used (-1ULL) instead of -1 per Catalin.
- Added comment about arm64 lazy mmu allow sleeping per Ryan.
- Squashed patch #4 in v7 into patch #3.
- Squashed patch #6 in v7 into patch #4.
- Added patch #5 to fix a arm64 kprobes bug. It guarantees set_memory_rox() is called before vfree(). It can go into separately or with this series together.
- Collected all the R-bs and A-bs.

Changes since v6 [2]
====================
- Patch 1: Minor refactor to implement walk_kernel_page_table_range() in terms of walk_kernel_page_table_range_lockless(). Also lead to adding *pmd argument to the lockless variant for consistency (per Catalin).
- Misc function/variable renames to improve clarity and consistency.
- Share same syncrhonization flag between idmap_kpti_install_ng_mappings and wait_linear_map_split_to_ptes, which allows removal of bbml2_ptes[] to save ~20K from kernel image.
- Only take pgtable_split_lock and enter lazy mmu mode once for both splits.
- Only walk the pgtable once for the common "split single page" case.
- Bypass split to contpmd and contpte when spllitting linear map to ptes.

[1] https://lore.kernel.org/linux-arm-kernel/20250829115250.2395585-1-
[email protected]/
[2] https://lore.kernel.org/linux-arm-kernel/20250805081350.3854670-1-
[email protected]/

Dev Jain (1):
       arm64: Enable permission change on arm64 kernel block mappings

Ryan Roberts (1):
       arm64: mm: split linear mapping if BBML2 unsupported on secondary CPUs

Yang Shi (3):
       arm64: cpufeature: add AmpereOne to BBML2 allow list
       arm64: mm: support large block mapping when rodata=full
       arm64: kprobes: call set_memory_rox() for kprobe page

   arch/arm64/include/asm/cpufeature.h |   2 +
   arch/arm64/include/asm/mmu.h        |   3 +
   arch/arm64/include/asm/pgtable.h    |   5 ++
   arch/arm64/kernel/cpufeature.c      |  12 +++-
   arch/arm64/kernel/probes/kprobes.c  |  12 ++++
   arch/arm64/mm/mmu.c                 | 422 ++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----
   arch/arm64/mm/pageattr.c            | 123 ++++++++++++++++++++++++---------
   arch/arm64/mm/proc.S                |  27 ++++++--
   include/linux/pagewalk.h            |   3 +
   mm/pagewalk.c                       |  36 ++++++----
   10 files changed, 581 insertions(+), 64 deletions(-)
#38 lore raw
Quoted 107 lines · Yang Shi
Quoted 13 lines · Ryan Roberts

Thanks for the report!

+ Kevin, who was looking at some adjacent issues and may have some ideas for how
to fix.

Quoted 5 lines · Jinjiang Tu
Quoted 3 lines · Yang Shi

On systems with BBML2_NOABORT support, it causes the linear map to be mapped with large blocks, even when rodata=full, and leads to some nice performance improvements.

Hi,

Hi Jinjiang,

Thanks for reporting the problem.

Quoted 70 lines
Quoted 62 lines

I find this feature is incompatible with realm. The calltrace is as follows:

[    0.000000][    T0] ------------[ cut here ]------------
[    0.000000][    T0] WARNING: CPU: 0 PID: 0 at arch/arm64/mm/pageattr.c:56 pageattr_pmd_entry+0x60/0x78
[    0.000000][    T0] Modules linked in:
[    0.000000][    T0] CPU: 0 PID: 0 Comm: swapper/0 Not tainted 6.6.0 #16 [    0.000000][    T0] Hardware name: linux,dummy-virt (DT)
[    0.000000][    T0] pstate: 800000c5 (Nzcv daIF -PAN -UAO -TCO -DIT -SSBS
BTYPE=--)
[    0.000000][    T0] pc : pageattr_pmd_entry+0x60/0x78
[    0.000000][    T0] lr : walk_pmd_range.isra.0+0x170/0x1f0
[    0.000000][    T0] sp : ffffcb90a0f337d0
[    0.000000][    T0] x29: ffffcb90a0f337d0 x28: 0000000000000000 x27:
ffff0000035e0000
[    0.000000][    T0] x26: ffffcb90a0f338f8 x25: ffff00001fff60d0 x24:
ffff0000035d0000
[    0.000000][    T0] x23: 0400000000000001 x22: 0c00000000000001 x21:
ffff0000035dffff
[    0.000000][    T0] x20: ffffcb909fe3b7f0 x19: ffff0000035e0000 x18:
ffffffffffffffff
[    0.000000][    T0] x17: 7220303030303178 x16: 307e303030306435 x15:
ffffcb90a0f334c8
[    0.000000][    T0] x14: 0000000000000000 x13: 205d305420202020 x12:
5b5d303030303030
[    0.000000][    T0] x11: 00000000ffff7fff x10: 00000000ffff7fff x9 :
ffffcb909f1e27d8
[    0.000000][    T0] x8 : 00000000000bffe8 x7 : c0000000ffff7fff x6 :
0000000000000001
[    0.000000][    T0] x5 : 0000000000000001 x4 : 0078000083400705 x3 :
ffffcb90a0f338f8
[    0.000000][    T0] x2 : 0000000000010000 x1 : ffff0000035d0000 x0 :
ffff00001fff60d0
[    0.000000][    T0] Call trace:
[    0.000000][    T0]  pageattr_pmd_entry+0x60/0x78
[    0.000000][    T0]  walk_pud_range+0x124/0x190
[    0.000000][    T0]  walk_pgd_range+0x158/0x1b0
[    0.000000][    T0] walk_kernel_page_table_range_lockless+0x58/0x98 [    0.000000][    T0]  update_range_prot+0xb8/0x108
[    0.000000][    T0]  __change_memory_common+0x30/0x1a8
[    0.000000][    T0] __set_memory_enc_dec.part.0+0x170/0x260
[    0.000000][    T0]  realm_set_memory_decrypted+0x6c/0xb0
[    0.000000][    T0]  set_memory_decrypted+0x38/0x58
[    0.000000][    T0]  its_alloc_pages_node+0xc4/0x140
[    0.000000][    T0]  its_probe_one+0xbc/0x3c0
[    0.000000][    T0]  its_of_probe.isra.0+0x130/0x220
[    0.000000][    T0]  its_init+0x160/0x2f8
[    0.000000][    T0]  gic_init_bases+0x1fc/0x318
[    0.000000][    T0]  gic_of_init+0x2a0/0x300
[    0.000000][    T0]  of_irq_init+0x238/0x4b8
[    0.000000][    T0]  irqchip_init+0x20/0x50
[    0.000000][    T0]  init_IRQ+0x1c/0x100
[    0.000000][    T0]  start_kernel+0x1ec/0x4f0
[    0.000000][    T0]  __primary_switched+0xbc/0xd0
[    0.000000][    T0] ---[ end trace 0000000000000000 ]---
[    0.000000][    T0] ------------[ cut here ]------------
[    0.000000][    T0] Failed to decrypt memory, 16 pages will be leaked

realm feature relies on rodata=full to dynamically update kernel page table prot.

Quoted 8 lines

In init_IRQ(), realm_set_memory_decrypted() is called to update kernel page table prot.
At this time, secondary cpus aren't booted, BBML2 noabort feature isn't initializated,
and system_supports_bbml2_noabort() still returns false. As a result, split_kernel_leaf_mapping() is skipped, leading to WARN_ON_ONCE((next - addr) != PMD_SIZE)
in pageattr_pmd_entry().

Quoted 4 lines

If no secondary cpus are yet running, then it is technically safe to split because we know all online cpus (i.e. just the boot cpu) supports BBML2_NOABORT. So we could explicitly only disallow splitting during the window between booting secondary cpus and finalizing the system caps. Feels a bit hacky though...

I think we can check whether system feature has been finalized or not. If it has not been finalized yet, we just need to check whether the current cpu (should be just boot cpu) supports BBML2_NOABORT or not. It sounds ok to me.

Quoted 9 lines
Quoted 9 lines

Before setup_system_features(), we don't know if all cpus support BBML2 noabort, and we
couldn't split kernel page table, in case another cpu that doesn't support BBML2 noabort
is running.

How could we fix this issue?

Quoted 7 lines
Quoted 5 lines
Quoted 5 lines

1. force pte mapping if realm feature is enabled? Although force_pte_mapping() return true if is_realm_world() return true, arm64_rsi_init() is called after map_mem(). So is_realm_world() still return false during map_mem(). Thus realm feature relies on rodata=full. If we fix by this solution, we need to add a new cmdline to force pte mapping.

I don't quite get why is_realm_world() relies on rodata=full. I understand realm needs PTE mapping if BBML2_NOABORT is not supported. But it doesn't mean real relies on rodata=full.

https://lore.kernel.org/all/[email protected]/

This is the discussion why realm relies on rodata=full. The initization of realm coudn't move to before map_mem(), so is_realm_world() is false. As a result, realm need rodata=full to indicate we need to make pages shared/protected at page granularity.

Quoted 322 lines
Quoted 11 lines

I think we just need to make is_realm_world() work earlier in boot? I think this has been a known issue for a while. Not sure if there is any plan to fix it though.

Quoted 1 line

2. If we could try to split kernel page table before setup_system_features()?

Another option would be to initially map by pte then collapse to block mappings once we have determined that all cpus support BBML2_NOABORT. We originally opted not to do that because it's a tax on symetric systems. But we could throw in the towel if it's the least bad solution we can come up with for solving this. I think it might help some of Kevin's use cases too?

May be an option too. When we discussed this there was no usecase for direct mapping collapse. But if we can have multiple usecases, it may be worth it. AFAICT, the ROX execmem cache may need this, which Will or someone else from Google is going to work on.

Checking current cpu BBML2_NOABORT capability before system feature is finalized seems like a fast way to stop bleeding IMHO before we find more elegant long-term solution.

Thanks,
Yang

Quoted 301 lines

Thanks,
Ryan

Quoted 296 lines

Thanks.

Quoted 294 lines

Ryan tested v7 on an AmpereOne system (a VM with 12G RAM) in all 3 possible modes by hacking the BBML2 feature detection code:

    - mode 1: All CPUs support BBML2 so the linear map uses large mappings
    - mode 2: Boot CPU does not support BBML2 so linear map uses pte mappings
    - mode 3: Boot CPU supports BBML2 but secondaries do not so linear map initially uses large mappings but is then repainted to use pte mappings

In all cases, mm selftests run and no regressions are observed. In all cases, ptdump of linear map is as expected. Because there are just some cleanups between v7 and v8, so I kept using Ryan's test result:

Mode 1:
=======
---[ Linear Mapping start ]--- 0xffff000000000000-0xffff000000200000           2M PMD RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED 0xffff000000200000-0xffff000000210000          64K PTE RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000000210000-0xffff000000400000        1984K PTE ro NX SHD AF            UXN    MEM/NORMAL 0xffff000000400000-0xffff000002400000          32M PMD ro NX SHD AF        BLK UXN    MEM/NORMAL 0xffff000002400000-0xffff000002550000        1344K PTE ro NX SHD AF            UXN    MEM/NORMAL 0xffff000002550000-0xffff000002600000         704K PTE RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000002600000-0xffff000004000000          26M PMD RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED 0xffff000004000000-0xffff000040000000         960M PMD RW NX SHD AF CON BLK UXN    MEM/NORMAL-TAGGED 0xffff000040000000-0xffff000140000000           4G PUD RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED 0xffff000140000000-0xffff000142000000          32M PMD RW NX SHD AF CON BLK UXN    MEM/NORMAL-TAGGED 0xffff000142000000-0xffff000142120000        1152K PTE RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000142120000-0xffff000142128000          32K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142128000-0xffff000142159000         196K PTE ro NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142159000-0xffff000142160000          28K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142160000-0xffff000142240000         896K PTE RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000142240000-0xffff00014224e000          56K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff00014224e000-0xffff000142250000           8K PTE ro NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142250000-0xffff000142260000          64K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142260000-0xffff000142280000         128K PTE RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000142280000-0xffff000142288000          32K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142288000-0xffff000142290000          32K PTE ro NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142290000-0xffff0001422a0000          64K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff0001422a0000-0xffff000142465000        1812K PTE ro NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142465000-0xffff000142470000          44K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142470000-0xffff000142600000        1600K PTE RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000142600000-0xffff000144000000          26M PMD RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED 0xffff000144000000-0xffff000180000000         960M PMD RW NX SHD AF CON BLK UXN    MEM/NORMAL-TAGGED 0xffff000180000000-0xffff000181a00000          26M PMD RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED 0xffff000181a00000-0xffff000181b90000        1600K PTE RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000181b90000-0xffff000181b9d000          52K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000181b9d000-0xffff000181c80000         908K PTE ro NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000181c80000-0xffff000181c90000          64K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000181c90000-0xffff000181ca0000          64K PTE RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000181ca0000-0xffff000181dbd000        1140K PTE ro NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000181dbd000-0xffff000181dc0000          12K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000181dc0000-0xffff000181e00000         256K PTE RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000181e00000-0xffff000182000000           2M PMD RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED 0xffff000182000000-0xffff0001c0000000         992M PMD RW NX SHD AF CON BLK UXN    MEM/NORMAL-TAGGED 0xffff0001c0000000-0xffff000300000000           5G PUD RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED 0xffff000300000000-0xffff008000000000         500G PUD 0xffff008000000000-0xffff800000000000      130560G PGD ---[ Linear Mapping end ]---

Mode 3:
=======
---[ Linear Mapping start ]--- 0xffff000000000000-0xffff000000210000        2112K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000000210000-0xffff000000400000        1984K PTE ro NX SHD AF            UXN    MEM/NORMAL 0xffff000000400000-0xffff000002400000          32M PMD ro NX SHD AF        BLK UXN    MEM/NORMAL 0xffff000002400000-0xffff000002550000        1344K PTE ro NX SHD AF            UXN    MEM/NORMAL 0xffff000002550000-0xffff000143a61000     5264452K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000143a61000-0xffff000143c61000           2M PTE ro NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000143c61000-0xffff000181b9a000     1015012K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000181b9a000-0xffff000181d9a000           2M PTE ro NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000181d9a000-0xffff000300000000     6261144K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000300000000-0xffff008000000000         500G PUD 0xffff008000000000-0xffff800000000000      130560G PGD ---[ Linear Mapping end ]---

Performance Testing
===================
* Memory use after boot
Before:
MemTotal:       258988984 kB
MemFree:        254821700 kB

After:
MemTotal:       259505132 kB
MemFree:        255410264 kB

Around 500MB more memory are free to use.  The larger the machine, the more memory saved.

* Memcached
We saw performance degradation when running Memcached benchmark with rodata=full vs rodata=on.  Our profiling pointed to kernel TLB pressure. With this patchset we saw ops/sec is increased by around 3.5%, P99 latency is reduced by around 9.6%.
The gain mainly came from reduced kernel TLB misses.  The kernel TLB MPKI is reduced by 28.5%.

The benchmark data is now on par with rodata=on too.

* Disk encryption (dm-crypt) benchmark
Ran fio benchmark with the below command on a 128G ramdisk (ext4) with disk encryption (by dm-crypt).
fio --directory=/data --random_generator=lfsr
--norandommap            \
      --randrepeat 1 --status-interval=999 --rw=write --bs=4k --loops=1  \
      --ioengine=sync --iodepth=1 --numjobs=1 --fsync_on_close=1         \
      --group_reporting --thread --name=iops-test-job --eta-newline=1    \
      --size 100G

The IOPS is increased by 90% - 150% (the variance is high, but the worst number of good case is around 90% more than the best number of bad case). The bandwidth is increased and the avg clat is reduced proportionally.

* Sequential file read
Read 100G file sequentially on XFS (xfs_io read with page cache populated). The bandwidth is increased by 150%.

Additionally Ryan also ran this through a random selection of benchmarks on AmpereOne. None show any regressions, and various benchmarks show statistically significant improvement. I'm just showing those improvements here:

+----------------------
+----------------------------------------------------------
+-------------------------+
| Benchmark            | Result
Class                                             | Improvement vs 6.17-rc1 |
+======================+==========================================================+=========================+ 
| micromm/vmalloc      | full_fit_alloc_test: p:1, h:0, l:500000
(usec)           |              (I) -9.00% |
|                      | kvfree_rcu_1_arg_vmalloc_test: p:1, h:0, l:500000
(usec) |              (I) -6.93% |
|                      | kvfree_rcu_2_arg_vmalloc_test: p:1, h:0, l:500000
(usec) |              (I) -6.77% |
|                      | pcpu_alloc_test: p:1, h:0, l:500000
(usec)               |              (I) -4.63% |
+----------------------
+----------------------------------------------------------
+-------------------------+
| mmtests/hackbench    | process-sockets-30
(seconds)                             |              (I) -2.96% |
+----------------------
+----------------------------------------------------------
+-------------------------+
| mmtests/kernbench    | syst-192
(seconds) |             (I) -12.77% |
+----------------------
+----------------------------------------------------------
+-------------------------+
| pts/perl-benchmark   | Test: Interpreter
(Seconds)                              |              (I) -4.86% |
+----------------------
+----------------------------------------------------------
+-------------------------+
| pts/pgbench          | Scale: 1 Clients: 1 Read Write
(TPS)                     |               (I) 5.07% |
|                      | Scale: 1 Clients: 1 Read Write - Latency
(ms)            |              (I) -4.72% |
|                      | Scale: 100 Clients: 1000 Read Write
(TPS)                |               (I) 2.58% |
|                      | Scale: 100 Clients: 1000 Read Write - Latency
(ms)       |              (I) -2.52% |
+----------------------
+----------------------------------------------------------
+-------------------------+
| pts/sqlite-speedtest | Timed Time - Size 1,000
(Seconds)                        |              (I) -2.68% |
+----------------------
+----------------------------------------------------------
+-------------------------+

Changes since v7 [1]
====================
- Rebased on v6.17-rc6 and Shijie's rodata series
(https://git.kernel.org/pub/
scm/linux/kernel/git/arm64/linux.git/commit/?id=bfbbb0d3215f) which has been picked up by Will.
- Patch 1: Fixed pmd_leaf/pud_leaf issue since the code may need to change
    permission for invalid entries per Jinjiang Tu.
- Patch 1: Removed pageattr_pgd_entry and pageattr_p4d_entry per Ryan.
- Used (-1ULL) instead of -1 per Catalin.
- Added comment about arm64 lazy mmu allow sleeping per Ryan.
- Squashed patch #4 in v7 into patch #3.
- Squashed patch #6 in v7 into patch #4.
- Added patch #5 to fix a arm64 kprobes bug. It guarantees set_memory_rox()
    is called before vfree(). It can go into separately or with this series together.
- Collected all the R-bs and A-bs.

Changes since v6 [2]
====================
- Patch 1: Minor refactor to implement walk_kernel_page_table_range() in terms
    of walk_kernel_page_table_range_lockless(). Also lead to adding *pmd argument to the lockless variant for consistency (per Catalin).
- Misc function/variable renames to improve clarity and consistency.
- Share same syncrhonization flag between idmap_kpti_install_ng_mappings and
    wait_linear_map_split_to_ptes, which allows removal of bbml2_ptes[] to save ~20K from kernel image.
- Only take pgtable_split_lock and enter lazy mmu mode once for
both splits.
- Only walk the pgtable once for the common "split single page" case.
- Bypass split to contpmd and contpte when spllitting linear map to ptes.

[1] https://lore.kernel.org/linux-arm-kernel/20250829115250.2395585-1-
[email protected]/
[2] https://lore.kernel.org/linux-arm-kernel/20250805081350.3854670-1-
[email protected]/

Dev Jain (1):
        arm64: Enable permission change on arm64 kernel block mappings

Ryan Roberts (1):
        arm64: mm: split linear mapping if BBML2 unsupported on secondary CPUs

Yang Shi (3):
        arm64: cpufeature: add AmpereOne to BBML2 allow list
        arm64: mm: support large block mapping when rodata=full
        arm64: kprobes: call set_memory_rox() for kprobe page

   arch/arm64/include/asm/cpufeature.h |   2 +
   arch/arm64/include/asm/mmu.h        |   3 +
   arch/arm64/include/asm/pgtable.h    |   5 ++
   arch/arm64/kernel/cpufeature.c      |  12 +++-
   arch/arm64/kernel/probes/kprobes.c  |  12 ++++
   arch/arm64/mm/mmu.c                 | 422
++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----

   arch/arm64/mm/pageattr.c            | 123
++++++++++++++++++++++++---------
   arch/arm64/mm/proc.S                |  27 ++++++-- include/linux/pagewalk.h            |   3 + mm/pagewalk.c                       |  36 ++++++----

   10 files changed, 581 insertions(+), 64 deletions(-)
#39 lore raw
Quoted 93 lines · Jinjiang Tu
Quoted 91 lines · Yang Shi
Quoted 12 lines · Ryan Roberts

Thanks for the report!

+ Kevin, who was looking at some adjacent issues and may have some ideas for how to fix.

Quoted 5 lines · Jinjiang Tu
Quoted 3 lines · Yang Shi

On systems with BBML2_NOABORT support, it causes the linear map to be mapped with large blocks, even when rodata=full, and leads to some nice performance improvements.

Hi,

Hi Jinjiang,

Thanks for reporting the problem.

Quoted 71 lines
Quoted 71 lines

I find this feature is incompatible with realm. The calltrace is as follows:

[    0.000000][    T0] ------------[ cut here ]------------
[    0.000000][    T0] WARNING: CPU: 0 PID: 0 at arch/arm64/mm/pageattr.c:56 pageattr_pmd_entry+0x60/0x78
[    0.000000][    T0] Modules linked in:
[    0.000000][    T0] CPU: 0 PID: 0 Comm: swapper/0 Not tainted 6.6.0 #16 [    0.000000][    T0] Hardware name: linux,dummy-virt (DT)
[    0.000000][    T0] pstate: 800000c5 (Nzcv daIF -PAN -UAO -TCO -DIT -SSBS BTYPE=--)
[    0.000000][    T0] pc : pageattr_pmd_entry+0x60/0x78
[    0.000000][    T0] lr : walk_pmd_range.isra.0+0x170/0x1f0
[    0.000000][    T0] sp : ffffcb90a0f337d0
[    0.000000][    T0] x29: ffffcb90a0f337d0 x28: 0000000000000000 x27:
ffff0000035e0000
[    0.000000][    T0] x26: ffffcb90a0f338f8 x25: ffff00001fff60d0 x24:
ffff0000035d0000
[    0.000000][    T0] x23: 0400000000000001 x22: 0c00000000000001 x21:
ffff0000035dffff
[    0.000000][    T0] x20: ffffcb909fe3b7f0 x19: ffff0000035e0000 x18:
ffffffffffffffff
[    0.000000][    T0] x17: 7220303030303178 x16: 307e303030306435 x15:
ffffcb90a0f334c8
[    0.000000][    T0] x14: 0000000000000000 x13: 205d305420202020 x12:
5b5d303030303030
[    0.000000][    T0] x11: 00000000ffff7fff x10: 00000000ffff7fff x9 :
ffffcb909f1e27d8
[    0.000000][    T0] x8 : 00000000000bffe8 x7 : c0000000ffff7fff x6 :
0000000000000001
[    0.000000][    T0] x5 : 0000000000000001 x4 : 0078000083400705 x3 :
ffffcb90a0f338f8
[    0.000000][    T0] x2 : 0000000000010000 x1 : ffff0000035d0000 x0 :
ffff00001fff60d0
[    0.000000][    T0] Call trace:
[    0.000000][    T0]  pageattr_pmd_entry+0x60/0x78
[    0.000000][    T0]  walk_pud_range+0x124/0x190
[    0.000000][    T0]  walk_pgd_range+0x158/0x1b0
[    0.000000][    T0] walk_kernel_page_table_range_lockless+0x58/0x98 [    0.000000][    T0]  update_range_prot+0xb8/0x108
[    0.000000][    T0]  __change_memory_common+0x30/0x1a8
[    0.000000][    T0] __set_memory_enc_dec.part.0+0x170/0x260
[    0.000000][    T0]  realm_set_memory_decrypted+0x6c/0xb0
[    0.000000][    T0]  set_memory_decrypted+0x38/0x58
[    0.000000][    T0]  its_alloc_pages_node+0xc4/0x140
[    0.000000][    T0]  its_probe_one+0xbc/0x3c0
[    0.000000][    T0]  its_of_probe.isra.0+0x130/0x220
[    0.000000][    T0]  its_init+0x160/0x2f8
[    0.000000][    T0]  gic_init_bases+0x1fc/0x318
[    0.000000][    T0]  gic_of_init+0x2a0/0x300
[    0.000000][    T0]  of_irq_init+0x238/0x4b8
[    0.000000][    T0]  irqchip_init+0x20/0x50
[    0.000000][    T0]  init_IRQ+0x1c/0x100
[    0.000000][    T0]  start_kernel+0x1ec/0x4f0
[    0.000000][    T0]  __primary_switched+0xbc/0xd0
[    0.000000][    T0] ---[ end trace 0000000000000000 ]---
[    0.000000][    T0] ------------[ cut here ]------------
[    0.000000][    T0] Failed to decrypt memory, 16 pages will be leaked

realm feature relies on rodata=full to dynamically update kernel page table prot.

In init_IRQ(), realm_set_memory_decrypted() is called to update kernel page table prot.
At this time, secondary cpus aren't booted, BBML2 noabort feature isn't initializated,
and system_supports_bbml2_noabort() still returns false. As a result, split_kernel_leaf_mapping() is skipped, leading to WARN_ON_ONCE((next - addr) !=
PMD_SIZE)
in pageattr_pmd_entry().

Quoted 8 lines
Quoted 8 lines
Quoted 4 lines

If no secondary cpus are yet running, then it is technically safe to split because we know all online cpus (i.e. just the boot cpu) supports BBML2_NOABORT. So we could explicitly only disallow splitting during the window between booting secondary cpus and finalizing the system caps. Feels a bit hacky though...

I think we can check whether system feature has been finalized or not. If it has not been finalized yet, we just need to check whether the current cpu (should be just boot cpu) supports BBML2_NOABORT or not. It sounds ok to me.

No I don't think that's sufficient; if the secondary cpus are started (even if not running the code path doing the split) we have to assume the secondary cpus are sharing the linear map pgtables, so if we split them on the boot cpu and the secondary cpus don't support BBML2_NOABORT, things could break.

I think 2 options would be:

- disallow split for the window between starting the secondary cpus and
finalizing the system caps.

- Do the split in stop_machine() if any request for splitting is made between
starting the secondary cpus and finalizing the system caps.

Both feel pretty ugly. I'll have a chat with Catalin and try to guage opinons...

In the meantime, would you mind trying this (uncompiled, untested) patch? It's attempting to implement option 1. TBH, I'm not sure if this is legal since we will now try to get a mutex; is that allowed in early code that can't sleep? I guess we only have a single thread running so there can't be any contention...

---8<---

arch/arm64/mm/mmu.c +0 -0
index 8e1d80a7033e3..72790126db55c 100644
@@ -779,7 +779,16 @@ int split_kernel_leaf_mapping(unsigned long start, unsigned

long end)
* and let the permission change code raise a warning if not already
* pte-mapped.
*/
- if (!system_supports_bbml2_noabort())
+ if (system_capabilities_finalized() && !system_supports_bbml2_noabort())
+ return 0;
+
+ /*
+ * If system capabilities are not finalized and there is only 1 online
+ * cpu, then we must be running on the boot cpu during early boot before
+ * any secondaries have started. If the boot cpu supports bbml2, we can
+ * safely split.
+ */
+ if (num_online_cpus() > 1 || !cpu_supports_bbml2_noabort())
return 0;

/*
---8<---

Thanks,
Ryan

Quoted 346 lines
Quoted 18 lines
Quoted 10 lines
Quoted 10 lines

Before setup_system_features(), we don't know if all cpus support BBML2 noabort,
and we
couldn't split kernel page table, in case another cpu that doesn't support BBML2
noabort
is running.

Quoted 7 lines
Quoted 7 lines

How could we fix this issue?

1. force pte mapping if realm feature is enabled? Although force_pte_mapping() return true if is_realm_world() return true, arm64_rsi_init() is called after map_mem(). So is_realm_world() still return false during map_mem(). Thus realm feature relies on rodata=full. If we fix by this solution, we need to add a new cmdline to force pte mapping.

Quoted 3 lines

I don't quite get why is_realm_world() relies on rodata=full. I understand realm needs PTE mapping if BBML2_NOABORT is not supported. But it doesn't mean real relies on rodata=full.

https://lore.kernel.org/all/[email protected]/

This is the discussion why realm relies on rodata=full. The initization of realm coudn't move to before map_mem(), so is_realm_world() is false. As a result, realm need rodata=full to indicate we need to make pages shared/protected at page granularity.

Quoted 317 lines
Quoted 11 lines

I think we just need to make is_realm_world() work earlier in boot? I think this has been a known issue for a while. Not sure if there is any plan to fix it though.

Quoted 1 line

2. If we could try to split kernel page table before setup_system_features()?

Another option would be to initially map by pte then collapse to block mappings once we have determined that all cpus support BBML2_NOABORT. We originally opted not to do that because it's a tax on symetric systems. But we could throw in the towel if it's the least bad solution we can come up with for solving this. I think it might help some of Kevin's use cases too?

May be an option too. When we discussed this there was no usecase for direct mapping collapse. But if we can have multiple usecases, it may be worth it. AFAICT, the ROX execmem cache may need this, which Will or someone else from Google is going to work on.

Checking current cpu BBML2_NOABORT capability before system feature is finalized seems like a fast way to stop bleeding IMHO before we find more elegant long-term solution.

Thanks,
Yang

Quoted 291 lines

Thanks,
Ryan

Quoted 286 lines

Thanks.

Quoted 284 lines

Ryan tested v7 on an AmpereOne system (a VM with 12G RAM) in all 3 possible modes by hacking the BBML2 feature detection code:

    - mode 1: All CPUs support BBML2 so the linear map uses large mappings
    - mode 2: Boot CPU does not support BBML2 so linear map uses pte mappings
    - mode 3: Boot CPU supports BBML2 but secondaries do not so linear map initially uses large mappings but is then repainted to use pte mappings

In all cases, mm selftests run and no regressions are observed. In all cases, ptdump of linear map is as expected. Because there are just some cleanups between v7 and v8, so I kept using Ryan's test result:

Mode 1:
=======
---[ Linear Mapping start ]--- 0xffff000000000000-0xffff000000200000           2M PMD RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED 0xffff000000200000-0xffff000000210000          64K PTE RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000000210000-0xffff000000400000        1984K PTE ro NX SHD AF            UXN    MEM/NORMAL 0xffff000000400000-0xffff000002400000          32M PMD ro NX SHD AF        BLK UXN    MEM/NORMAL 0xffff000002400000-0xffff000002550000        1344K PTE ro NX SHD AF            UXN    MEM/NORMAL 0xffff000002550000-0xffff000002600000         704K PTE RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000002600000-0xffff000004000000          26M PMD RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED 0xffff000004000000-0xffff000040000000         960M PMD RW NX SHD AF CON BLK UXN    MEM/NORMAL-TAGGED 0xffff000040000000-0xffff000140000000           4G PUD RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED 0xffff000140000000-0xffff000142000000          32M PMD RW NX SHD AF CON BLK UXN    MEM/NORMAL-TAGGED 0xffff000142000000-0xffff000142120000        1152K PTE RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000142120000-0xffff000142128000          32K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142128000-0xffff000142159000         196K PTE ro NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142159000-0xffff000142160000          28K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142160000-0xffff000142240000         896K PTE RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000142240000-0xffff00014224e000          56K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff00014224e000-0xffff000142250000           8K PTE ro NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142250000-0xffff000142260000          64K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142260000-0xffff000142280000         128K PTE RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000142280000-0xffff000142288000          32K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142288000-0xffff000142290000          32K PTE ro NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142290000-0xffff0001422a0000          64K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff0001422a0000-0xffff000142465000        1812K PTE ro NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142465000-0xffff000142470000          44K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142470000-0xffff000142600000        1600K PTE RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000142600000-0xffff000144000000          26M PMD RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED 0xffff000144000000-0xffff000180000000         960M PMD RW NX SHD AF CON BLK UXN    MEM/NORMAL-TAGGED 0xffff000180000000-0xffff000181a00000          26M PMD RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED 0xffff000181a00000-0xffff000181b90000        1600K PTE RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000181b90000-0xffff000181b9d000          52K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000181b9d000-0xffff000181c80000         908K PTE ro NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000181c80000-0xffff000181c90000          64K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000181c90000-0xffff000181ca0000          64K PTE RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000181ca0000-0xffff000181dbd000        1140K PTE ro NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000181dbd000-0xffff000181dc0000          12K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000181dc0000-0xffff000181e00000         256K PTE RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000181e00000-0xffff000182000000           2M PMD RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED 0xffff000182000000-0xffff0001c0000000         992M PMD RW NX SHD AF CON BLK UXN    MEM/NORMAL-TAGGED 0xffff0001c0000000-0xffff000300000000           5G PUD RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED 0xffff000300000000-0xffff008000000000         500G PUD 0xffff008000000000-0xffff800000000000      130560G PGD ---[ Linear Mapping end ]---

Mode 3:
=======
---[ Linear Mapping start ]--- 0xffff000000000000-0xffff000000210000        2112K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000000210000-0xffff000000400000        1984K PTE ro NX SHD AF            UXN    MEM/NORMAL 0xffff000000400000-0xffff000002400000          32M PMD ro NX SHD AF        BLK UXN    MEM/NORMAL 0xffff000002400000-0xffff000002550000        1344K PTE ro NX SHD AF            UXN    MEM/NORMAL 0xffff000002550000-0xffff000143a61000     5264452K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000143a61000-0xffff000143c61000           2M PTE ro NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000143c61000-0xffff000181b9a000     1015012K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000181b9a000-0xffff000181d9a000           2M PTE ro NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000181d9a000-0xffff000300000000     6261144K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000300000000-0xffff008000000000         500G PUD 0xffff008000000000-0xffff800000000000      130560G PGD ---[ Linear Mapping end ]---

Performance Testing
===================
* Memory use after boot
Before:
MemTotal:       258988984 kB
MemFree:        254821700 kB

After:
MemTotal:       259505132 kB
MemFree:        255410264 kB

Around 500MB more memory are free to use.  The larger the machine, the more memory saved.

* Memcached
We saw performance degradation when running Memcached benchmark with rodata=full vs rodata=on.  Our profiling pointed to kernel TLB pressure. With this patchset we saw ops/sec is increased by around 3.5%, P99 latency is reduced by around 9.6%.
The gain mainly came from reduced kernel TLB misses.  The kernel TLB MPKI is reduced by 28.5%.

The benchmark data is now on par with rodata=on too.

* Disk encryption (dm-crypt) benchmark
Ran fio benchmark with the below command on a 128G ramdisk (ext4) with disk encryption (by dm-crypt).
fio --directory=/data --random_generator=lfsr --norandommap            \
      --randrepeat 1 --status-interval=999 --rw=write --bs=4k --loops=1  \
      --ioengine=sync --iodepth=1 --numjobs=1 --fsync_on_close=1         \
      --group_reporting --thread --name=iops-test-job --eta-newline=1    \
      --size 100G

The IOPS is increased by 90% - 150% (the variance is high, but the worst number of good case is around 90% more than the best number of bad case). The bandwidth is increased and the avg clat is reduced proportionally.

* Sequential file read
Read 100G file sequentially on XFS (xfs_io read with page cache populated). The bandwidth is increased by 150%.

Additionally Ryan also ran this through a random selection of benchmarks on AmpereOne. None show any regressions, and various benchmarks show statistically
significant improvement. I'm just showing those improvements here:

+----------------------
+----------------------------------------------------------
+-------------------------+
| Benchmark            | Result
Class                                             | Improvement vs 6.17-rc1 |
+======================+==========================================================+=========================+
| micromm/vmalloc      | full_fit_alloc_test: p:1, h:0, l:500000
(usec)           |              (I) -9.00% |
|                      | kvfree_rcu_1_arg_vmalloc_test: p:1, h:0, l:500000
(usec) |              (I) -6.93% |
|                      | kvfree_rcu_2_arg_vmalloc_test: p:1, h:0, l:500000
(usec) |              (I) -6.77% |
|                      | pcpu_alloc_test: p:1, h:0, l:500000
(usec)               |              (I) -4.63% |
+----------------------
+----------------------------------------------------------
+-------------------------+
| mmtests/hackbench    | process-sockets-30
(seconds)                             |              (I) -2.96% |
+----------------------
+----------------------------------------------------------
+-------------------------+
| mmtests/kernbench    | syst-192
(seconds) |             (I) -12.77% |
+----------------------
+----------------------------------------------------------
+-------------------------+
| pts/perl-benchmark   | Test: Interpreter
(Seconds)                              |              (I) -4.86% |
+----------------------
+----------------------------------------------------------
+-------------------------+
| pts/pgbench          | Scale: 1 Clients: 1 Read Write
(TPS)                     |               (I) 5.07% |
|                      | Scale: 1 Clients: 1 Read Write - Latency
(ms)            |              (I) -4.72% |
|                      | Scale: 100 Clients: 1000 Read Write
(TPS)                |               (I) 2.58% |
|                      | Scale: 100 Clients: 1000 Read Write - Latency
(ms)       |              (I) -2.52% |
+----------------------
+----------------------------------------------------------
+-------------------------+
| pts/sqlite-speedtest | Timed Time - Size 1,000
(Seconds)                        |              (I) -2.68% |
+----------------------
+----------------------------------------------------------
+-------------------------+

Changes since v7 [1]
====================
- Rebased on v6.17-rc6 and Shijie's rodata series (https://git.kernel.org/pub/
scm/linux/kernel/git/arm64/linux.git/commit/?id=bfbbb0d3215f) which has been picked up by Will.
- Patch 1: Fixed pmd_leaf/pud_leaf issue since the code may need to change permission for invalid entries per Jinjiang Tu.
- Patch 1: Removed pageattr_pgd_entry and pageattr_p4d_entry per Ryan.
- Used (-1ULL) instead of -1 per Catalin.
- Added comment about arm64 lazy mmu allow sleeping per Ryan.
- Squashed patch #4 in v7 into patch #3.
- Squashed patch #6 in v7 into patch #4.
- Added patch #5 to fix a arm64 kprobes bug. It guarantees set_memory_rox() is called before vfree(). It can go into separately or with this series together.
- Collected all the R-bs and A-bs.

Changes since v6 [2]
====================
- Patch 1: Minor refactor to implement walk_kernel_page_table_range() in terms of walk_kernel_page_table_range_lockless(). Also lead to adding *pmd argument
    to the lockless variant for consistency (per Catalin).
- Misc function/variable renames to improve clarity and consistency.
- Share same syncrhonization flag between idmap_kpti_install_ng_mappings and wait_linear_map_split_to_ptes, which allows removal of bbml2_ptes[] to save
    ~20K from kernel image.
- Only take pgtable_split_lock and enter lazy mmu mode once for both splits.
- Only walk the pgtable once for the common "split single page" case.
- Bypass split to contpmd and contpte when spllitting linear map to ptes.

[1] https://lore.kernel.org/linux-arm-kernel/20250829115250.2395585-1-
[email protected]/
[2] https://lore.kernel.org/linux-arm-kernel/20250805081350.3854670-1-
[email protected]/

Dev Jain (1):
        arm64: Enable permission change on arm64 kernel block mappings

Ryan Roberts (1):
        arm64: mm: split linear mapping if BBML2 unsupported on secondary CPUs

Yang Shi (3):
        arm64: cpufeature: add AmpereOne to BBML2 allow list
        arm64: mm: support large block mapping when rodata=full
        arm64: kprobes: call set_memory_rox() for kprobe page

   arch/arm64/include/asm/cpufeature.h |   2 + arch/arm64/include/asm/mmu.h        |   3 + arch/arm64/include/asm/pgtable.h    |   5 ++ arch/arm64/kernel/cpufeature.c      |  12 +++- arch/arm64/kernel/probes/kprobes.c  |  12 ++++ arch/arm64/mm/mmu.c                 | 422 ++++++++++++++++++++++++++++++
++++
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

+----
   arch/arm64/mm/pageattr.c            | 123 ++++++++++++++++++++++++---------
   arch/arm64/mm/proc.S                |  27 ++++++--
   include/linux/pagewalk.h            |   3 +
   mm/pagewalk.c                       |  36 ++++++----
   10 files changed, 581 insertions(+), 64 deletions(-)
author
#40 lore raw
Quoted 94 lines · Ryan Roberts
Quoted 93 lines · Jinjiang Tu
Quoted 92 lines · Yang Shi
Quoted 12 lines · Ryan Roberts

Thanks for the report!

+ Kevin, who was looking at some adjacent issues and may have some ideas for how to fix.

Quoted 5 lines · Jinjiang Tu
Quoted 3 lines · Yang Shi

On systems with BBML2_NOABORT support, it causes the linear map to be mapped with large blocks, even when rodata=full, and leads to some nice performance improvements.

Hi,

Hi Jinjiang,

Thanks for reporting the problem.

Quoted 74 lines
Quoted 61 lines

I find this feature is incompatible with realm. The calltrace is as follows:

[    0.000000][    T0] ------------[ cut here ]------------
[    0.000000][    T0] WARNING: CPU: 0 PID: 0 at arch/arm64/mm/pageattr.c:56 pageattr_pmd_entry+0x60/0x78
[    0.000000][    T0] Modules linked in:
[    0.000000][    T0] CPU: 0 PID: 0 Comm: swapper/0 Not tainted 6.6.0 #16 [    0.000000][    T0] Hardware name: linux,dummy-virt (DT)
[    0.000000][    T0] pstate: 800000c5 (Nzcv daIF -PAN -UAO -TCO -DIT -SSBS BTYPE=--)
[    0.000000][    T0] pc : pageattr_pmd_entry+0x60/0x78
[    0.000000][    T0] lr : walk_pmd_range.isra.0+0x170/0x1f0
[    0.000000][    T0] sp : ffffcb90a0f337d0
[    0.000000][    T0] x29: ffffcb90a0f337d0 x28: 0000000000000000 x27:
ffff0000035e0000
[    0.000000][    T0] x26: ffffcb90a0f338f8 x25: ffff00001fff60d0 x24:
ffff0000035d0000
[    0.000000][    T0] x23: 0400000000000001 x22: 0c00000000000001 x21:
ffff0000035dffff
[    0.000000][    T0] x20: ffffcb909fe3b7f0 x19: ffff0000035e0000 x18:
ffffffffffffffff
[    0.000000][    T0] x17: 7220303030303178 x16: 307e303030306435 x15:
ffffcb90a0f334c8
[    0.000000][    T0] x14: 0000000000000000 x13: 205d305420202020 x12:
5b5d303030303030
[    0.000000][    T0] x11: 00000000ffff7fff x10: 00000000ffff7fff x9 :
ffffcb909f1e27d8
[    0.000000][    T0] x8 : 00000000000bffe8 x7 : c0000000ffff7fff x6 :
0000000000000001
[    0.000000][    T0] x5 : 0000000000000001 x4 : 0078000083400705 x3 :
ffffcb90a0f338f8
[    0.000000][    T0] x2 : 0000000000010000 x1 : ffff0000035d0000 x0 :
ffff00001fff60d0
[    0.000000][    T0] Call trace:
[    0.000000][    T0]  pageattr_pmd_entry+0x60/0x78
[    0.000000][    T0]  walk_pud_range+0x124/0x190
[    0.000000][    T0]  walk_pgd_range+0x158/0x1b0
[    0.000000][    T0] walk_kernel_page_table_range_lockless+0x58/0x98 [    0.000000][    T0]  update_range_prot+0xb8/0x108
[    0.000000][    T0]  __change_memory_common+0x30/0x1a8
[    0.000000][    T0] __set_memory_enc_dec.part.0+0x170/0x260
[    0.000000][    T0]  realm_set_memory_decrypted+0x6c/0xb0
[    0.000000][    T0]  set_memory_decrypted+0x38/0x58
[    0.000000][    T0]  its_alloc_pages_node+0xc4/0x140
[    0.000000][    T0]  its_probe_one+0xbc/0x3c0
[    0.000000][    T0]  its_of_probe.isra.0+0x130/0x220
[    0.000000][    T0]  its_init+0x160/0x2f8
[    0.000000][    T0]  gic_init_bases+0x1fc/0x318
[    0.000000][    T0]  gic_of_init+0x2a0/0x300
[    0.000000][    T0]  of_irq_init+0x238/0x4b8
[    0.000000][    T0]  irqchip_init+0x20/0x50
[    0.000000][    T0]  init_IRQ+0x1c/0x100
[    0.000000][    T0]  start_kernel+0x1ec/0x4f0
[    0.000000][    T0]  __primary_switched+0xbc/0xd0
[    0.000000][    T0] ---[ end trace 0000000000000000 ]---
[    0.000000][    T0] ------------[ cut here ]------------
[    0.000000][    T0] Failed to decrypt memory, 16 pages will be leaked

realm feature relies on rodata=full to dynamically update kernel page table prot.

Quoted 9 lines

In init_IRQ(), realm_set_memory_decrypted() is called to update kernel page table prot.
At this time, secondary cpus aren't booted, BBML2 noabort feature isn't initializated,
and system_supports_bbml2_noabort() still returns false. As a result, split_kernel_leaf_mapping() is skipped, leading to WARN_ON_ONCE((next - addr) !=
PMD_SIZE)
in pageattr_pmd_entry().

If no secondary cpus are yet running, then it is technically safe to split because we know all online cpus (i.e. just the boot cpu) supports BBML2_NOABORT. So we could explicitly only disallow splitting during the window between booting secondary cpus and finalizing the system caps. Feels a bit hacky though...

Quoted 7 lines
Quoted 3 lines
Quoted 3 lines

I think we can check whether system feature has been finalized or not. If it has not been finalized yet, we just need to check whether the current cpu (should be just boot cpu) supports BBML2_NOABORT or not. It sounds ok to me.

No I don't think that's sufficient; if the secondary cpus are started (even if not running the code path doing the split) we have to assume the secondary cpus are sharing the linear map pgtables, so if we split them on the boot cpu and the secondary cpus don't support BBML2_NOABORT, things could break.

Yes, I agree. I meant it should be ok for this splat because init_IRQ() is called in very early stage of boot and the secondary cpus have been not up yet. But there may be also split window between system caps have not been finalized and the secondary cpus are already up.

Quoted 5 lines

I think 2 options would be:

- disallow split for the window between starting the secondary cpus and
finalizing the system caps.

IOW, it should be ok to allow split before the secondary cpus are up if the boot cpu supports BBML2_NOABORT.

Quoted 11 lines

- Do the split in stop_machine() if any request for splitting is made between
starting the secondary cpus and finalizing the system caps.

Both feel pretty ugly. I'll have a chat with Catalin and try to guage opinons...

In the meantime, would you mind trying this (uncompiled, untested) patch? It's attempting to implement option 1. TBH, I'm not sure if this is legal since we will now try to get a mutex; is that allowed in early code that can't sleep? I guess we only have a single thread running so there can't be any contention...

Yeah, it should not go into slow path.

Quoted 24 lines

---8<---

arch/arm64/mm/mmu.c +10 -1
index 8e1d80a7033e3..72790126db55c 100644
@@ -779,7 +779,16 @@ int split_kernel_leaf_mapping(unsigned long start, unsigned
 long end)
  	 * and let the permission change code raise a warning if not already
  	 * pte-mapped.
  	 */
-	if (!system_supports_bbml2_noabort())
+	if (system_capabilities_finalized() && !system_supports_bbml2_noabort())
+		return 0;
+
+	/*
+	 * If system capabilities are not finalized and there is only 1 online
+	 * cpu, then we must be running on the boot cpu during early boot before
+	 * any secondaries have started. If the boot cpu supports bbml2, we can
+	 * safely split.
+	 */
+	if (num_online_cpus() > 1 || !cpu_supports_bbml2_noabort())
  		return 0;
 
Quoted 2 lines

/*
---8<---

Thank you for proposing the patch. This is what I had in my mind too.

Regards,
Yang

Quoted 344 lines

Thanks,
Ryan

Quoted 338 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

Before setup_system_features(), we don't know if all cpus support BBML2 noabort,
and we
couldn't split kernel page table, in case another cpu that doesn't support BBML2
noabort
is running.

How could we fix this issue?

Quoted 8 lines
Quoted 5 lines
Quoted 5 lines

1. force pte mapping if realm feature is enabled? Although force_pte_mapping() return true if is_realm_world() return true, arm64_rsi_init() is called after map_mem(). So is_realm_world() still return false during map_mem(). Thus realm feature relies on rodata=full. If we fix by this solution, we need to add a new cmdline to force pte mapping.

I don't quite get why is_realm_world() relies on rodata=full. I understand realm needs PTE mapping if BBML2_NOABORT is not supported. But it doesn't mean real relies on rodata=full.

https://lore.kernel.org/all/[email protected]/

This is the discussion why realm relies on rodata=full. The initization of realm coudn't move to before map_mem(), so is_realm_world() is false. As a result, realm need rodata=full to indicate we need to make pages shared/protected at page granularity.

Quoted 313 lines
Quoted 10 lines

I think we just need to make is_realm_world() work earlier in boot? I think this has been a known issue for a while. Not sure if there is any plan to fix it though.

Quoted 1 line

2. If we could try to split kernel page table before setup_system_features()?

Another option would be to initially map by pte then collapse to block mappings once we have determined that all cpus support BBML2_NOABORT. We originally opted not to do that because it's a tax on symetric systems. But we could throw in the towel if it's the least bad solution we can come up with for solving this. I think it might help some of Kevin's use cases too?

May be an option too. When we discussed this there was no usecase for direct mapping collapse. But if we can have multiple usecases, it may be worth it. AFAICT, the ROX execmem cache may need this, which Will or someone else from Google is going to work on.

Checking current cpu BBML2_NOABORT capability before system feature is finalized seems like a fast way to stop bleeding IMHO before we find more elegant long-term solution.

Thanks,
Yang

Quoted 290 lines

Thanks,
Ryan

Quoted 286 lines

Thanks.

Quoted 284 lines

Ryan tested v7 on an AmpereOne system (a VM with 12G RAM) in all 3 possible modes by hacking the BBML2 feature detection code:

    - mode 1: All CPUs support BBML2 so the linear map uses large mappings
    - mode 2: Boot CPU does not support BBML2 so linear map uses pte mappings
    - mode 3: Boot CPU supports BBML2 but secondaries do not so linear map initially uses large mappings but is then repainted to use pte mappings

In all cases, mm selftests run and no regressions are observed. In all cases, ptdump of linear map is as expected. Because there are just some cleanups between v7 and v8, so I kept using Ryan's test result:

Mode 1:
=======
---[ Linear Mapping start ]--- 0xffff000000000000-0xffff000000200000           2M PMD RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED 0xffff000000200000-0xffff000000210000          64K PTE RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000000210000-0xffff000000400000        1984K PTE ro NX SHD AF            UXN    MEM/NORMAL 0xffff000000400000-0xffff000002400000          32M PMD ro NX SHD AF        BLK UXN    MEM/NORMAL 0xffff000002400000-0xffff000002550000        1344K PTE ro NX SHD AF            UXN    MEM/NORMAL 0xffff000002550000-0xffff000002600000         704K PTE RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000002600000-0xffff000004000000          26M PMD RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED 0xffff000004000000-0xffff000040000000         960M PMD RW NX SHD AF CON BLK UXN    MEM/NORMAL-TAGGED 0xffff000040000000-0xffff000140000000           4G PUD RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED 0xffff000140000000-0xffff000142000000          32M PMD RW NX SHD AF CON BLK UXN    MEM/NORMAL-TAGGED 0xffff000142000000-0xffff000142120000        1152K PTE RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000142120000-0xffff000142128000          32K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142128000-0xffff000142159000         196K PTE ro NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142159000-0xffff000142160000          28K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142160000-0xffff000142240000         896K PTE RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000142240000-0xffff00014224e000          56K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff00014224e000-0xffff000142250000           8K PTE ro NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142250000-0xffff000142260000          64K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142260000-0xffff000142280000         128K PTE RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000142280000-0xffff000142288000          32K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142288000-0xffff000142290000          32K PTE ro NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142290000-0xffff0001422a0000          64K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff0001422a0000-0xffff000142465000        1812K PTE ro NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142465000-0xffff000142470000          44K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142470000-0xffff000142600000        1600K PTE RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000142600000-0xffff000144000000          26M PMD RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED 0xffff000144000000-0xffff000180000000         960M PMD RW NX SHD AF CON BLK UXN    MEM/NORMAL-TAGGED 0xffff000180000000-0xffff000181a00000          26M PMD RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED 0xffff000181a00000-0xffff000181b90000        1600K PTE RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000181b90000-0xffff000181b9d000          52K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000181b9d000-0xffff000181c80000         908K PTE ro NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000181c80000-0xffff000181c90000          64K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000181c90000-0xffff000181ca0000          64K PTE RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000181ca0000-0xffff000181dbd000        1140K PTE ro NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000181dbd000-0xffff000181dc0000          12K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000181dc0000-0xffff000181e00000         256K PTE RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000181e00000-0xffff000182000000           2M PMD RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED 0xffff000182000000-0xffff0001c0000000         992M PMD RW NX SHD AF CON BLK UXN    MEM/NORMAL-TAGGED 0xffff0001c0000000-0xffff000300000000           5G PUD RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED 0xffff000300000000-0xffff008000000000         500G PUD 0xffff008000000000-0xffff800000000000      130560G PGD ---[ Linear Mapping end ]---

Mode 3:
=======
---[ Linear Mapping start ]--- 0xffff000000000000-0xffff000000210000        2112K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000000210000-0xffff000000400000        1984K PTE ro NX SHD AF            UXN    MEM/NORMAL 0xffff000000400000-0xffff000002400000          32M PMD ro NX SHD AF        BLK UXN    MEM/NORMAL 0xffff000002400000-0xffff000002550000        1344K PTE ro NX SHD AF            UXN    MEM/NORMAL 0xffff000002550000-0xffff000143a61000     5264452K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000143a61000-0xffff000143c61000           2M PTE ro NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000143c61000-0xffff000181b9a000     1015012K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000181b9a000-0xffff000181d9a000           2M PTE ro NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000181d9a000-0xffff000300000000     6261144K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000300000000-0xffff008000000000         500G PUD 0xffff008000000000-0xffff800000000000      130560G PGD ---[ Linear Mapping end ]---

Performance Testing
===================
* Memory use after boot
Before:
MemTotal:       258988984 kB
MemFree:        254821700 kB

After:
MemTotal:       259505132 kB
MemFree:        255410264 kB

Around 500MB more memory are free to use.  The larger the machine, the more memory saved.

* Memcached
We saw performance degradation when running Memcached benchmark with rodata=full vs rodata=on.  Our profiling pointed to kernel TLB pressure. With this patchset we saw ops/sec is increased by around 3.5%, P99 latency is reduced by around 9.6%.
The gain mainly came from reduced kernel TLB misses.  The kernel TLB MPKI is reduced by 28.5%.

The benchmark data is now on par with rodata=on too.

* Disk encryption (dm-crypt) benchmark
Ran fio benchmark with the below command on a 128G ramdisk (ext4) with disk encryption (by dm-crypt).
fio --directory=/data --random_generator=lfsr --norandommap            \
      --randrepeat 1 --status-interval=999 --rw=write --bs=4k --loops=1  \
      --ioengine=sync --iodepth=1 --numjobs=1 --fsync_on_close=1         \
      --group_reporting --thread --name=iops-test-job --eta-newline=1    \
      --size 100G

The IOPS is increased by 90% - 150% (the variance is high, but the worst number of good case is around 90% more than the best number of bad case). The bandwidth is increased and the avg clat is reduced proportionally.

* Sequential file read
Read 100G file sequentially on XFS (xfs_io read with page cache populated). The bandwidth is increased by 150%.

Additionally Ryan also ran this through a random selection of benchmarks on AmpereOne. None show any regressions, and various benchmarks show statistically
significant improvement. I'm just showing those improvements here:

+----------------------
+----------------------------------------------------------
+-------------------------+
| Benchmark            | Result
Class                                             | Improvement vs 6.17-rc1 |
+======================+==========================================================+=========================+
| micromm/vmalloc      | full_fit_alloc_test: p:1, h:0, l:500000
(usec)           |              (I) -9.00% |
|                      | kvfree_rcu_1_arg_vmalloc_test: p:1, h:0, l:500000
(usec) |              (I) -6.93% |
|                      | kvfree_rcu_2_arg_vmalloc_test: p:1, h:0, l:500000
(usec) |              (I) -6.77% |
|                      | pcpu_alloc_test: p:1, h:0, l:500000
(usec)               |              (I) -4.63% |
+----------------------
+----------------------------------------------------------
+-------------------------+
| mmtests/hackbench    | process-sockets-30
(seconds)                             |              (I) -2.96% |
+----------------------
+----------------------------------------------------------
+-------------------------+
| mmtests/kernbench    | syst-192
(seconds) |             (I) -12.77% |
+----------------------
+----------------------------------------------------------
+-------------------------+
| pts/perl-benchmark   | Test: Interpreter
(Seconds)                              |              (I) -4.86% |
+----------------------
+----------------------------------------------------------
+-------------------------+
| pts/pgbench          | Scale: 1 Clients: 1 Read Write
(TPS)                     |               (I) 5.07% |
|                      | Scale: 1 Clients: 1 Read Write - Latency
(ms)            |              (I) -4.72% |
|                      | Scale: 100 Clients: 1000 Read Write
(TPS)                |               (I) 2.58% |
|                      | Scale: 100 Clients: 1000 Read Write - Latency
(ms)       |              (I) -2.52% |
+----------------------
+----------------------------------------------------------
+-------------------------+
| pts/sqlite-speedtest | Timed Time - Size 1,000
(Seconds)                        |              (I) -2.68% |
+----------------------
+----------------------------------------------------------
+-------------------------+

Changes since v7 [1]
====================
- Rebased on v6.17-rc6 and Shijie's rodata series (https://git.kernel.org/pub/
scm/linux/kernel/git/arm64/linux.git/commit/?id=bfbbb0d3215f)
    which has been picked up by Will.
- Patch 1: Fixed pmd_leaf/pud_leaf issue since the code may need to change permission for invalid entries per Jinjiang Tu.
- Patch 1: Removed pageattr_pgd_entry and pageattr_p4d_entry per Ryan.
- Used (-1ULL) instead of -1 per Catalin.
- Added comment about arm64 lazy mmu allow sleeping per Ryan.
- Squashed patch #4 in v7 into patch #3.
- Squashed patch #6 in v7 into patch #4.
- Added patch #5 to fix a arm64 kprobes bug. It guarantees set_memory_rox() is called before vfree(). It can go into separately or with this series together.
- Collected all the R-bs and A-bs.

Changes since v6 [2]
====================
- Patch 1: Minor refactor to implement walk_kernel_page_table_range() in terms of walk_kernel_page_table_range_lockless(). Also lead to adding *pmd
argument
    to the lockless variant for consistency (per Catalin).
- Misc function/variable renames to improve clarity and consistency.
- Share same syncrhonization flag between idmap_kpti_install_ng_mappings and wait_linear_map_split_to_ptes, which allows removal of bbml2_ptes[] to
save
    ~20K from kernel image.
- Only take pgtable_split_lock and enter lazy mmu mode once for both splits.
- Only walk the pgtable once for the common "split single page" case.
- Bypass split to contpmd and contpte when spllitting linear map to ptes.

[1] https://lore.kernel.org/linux-arm-kernel/20250829115250.2395585-1-
[email protected]/
[2] https://lore.kernel.org/linux-arm-kernel/20250805081350.3854670-1-
[email protected]/

Dev Jain (1):
        arm64: Enable permission change on arm64 kernel block mappings

Ryan Roberts (1):
        arm64: mm: split linear mapping if BBML2 unsupported on secondary CPUs

Yang Shi (3):
        arm64: cpufeature: add AmpereOne to BBML2 allow list
        arm64: mm: support large block mapping when rodata=full
        arm64: kprobes: call set_memory_rox() for kprobe page

    arch/arm64/include/asm/cpufeature.h |   2 +
    arch/arm64/include/asm/mmu.h        |   3 +
    arch/arm64/include/asm/pgtable.h    |   5 ++
    arch/arm64/kernel/cpufeature.c      |  12 +++-
    arch/arm64/kernel/probes/kprobes.c  |  12 ++++
    arch/arm64/mm/mmu.c                 | 422 ++++++++++++++++++++++++++++++
++++
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+----
    arch/arm64/mm/pageattr.c            | 123 ++++++++++++++++++++++++---------
    arch/arm64/mm/proc.S                |  27 ++++++--
    include/linux/pagewalk.h            |   3 +
    mm/pagewalk.c                       |  36 ++++++----
    10 files changed, 581 insertions(+), 64 deletions(-)
#41 lore raw
In reply to Ryan Roberts
Quoted 110 lines · Ryan Roberts
Quoted 89 lines · Jinjiang Tu
Quoted 88 lines · Yang Shi
Quoted 12 lines · Ryan Roberts

Thanks for the report!

+ Kevin, who was looking at some adjacent issues and may have some ideas for how to fix.

Quoted 5 lines · Jinjiang Tu
Quoted 3 lines · Yang Shi

On systems with BBML2_NOABORT support, it causes the linear map to be mapped with large blocks, even when rodata=full, and leads to some nice performance improvements.

Hi,

Hi Jinjiang,

Thanks for reporting the problem.

Quoted 70 lines
Quoted 70 lines

I find this feature is incompatible with realm. The calltrace is as follows:

[    0.000000][    T0] ------------[ cut here ]------------
[    0.000000][    T0] WARNING: CPU: 0 PID: 0 at arch/arm64/mm/pageattr.c:56 pageattr_pmd_entry+0x60/0x78
[    0.000000][    T0] Modules linked in:
[    0.000000][    T0] CPU: 0 PID: 0 Comm: swapper/0 Not tainted 6.6.0 #16 [    0.000000][    T0] Hardware name: linux,dummy-virt (DT)
[    0.000000][    T0] pstate: 800000c5 (Nzcv daIF -PAN -UAO -TCO -DIT -SSBS BTYPE=--)
[    0.000000][    T0] pc : pageattr_pmd_entry+0x60/0x78
[    0.000000][    T0] lr : walk_pmd_range.isra.0+0x170/0x1f0
[    0.000000][    T0] sp : ffffcb90a0f337d0
[    0.000000][    T0] x29: ffffcb90a0f337d0 x28: 0000000000000000 x27:
ffff0000035e0000
[    0.000000][    T0] x26: ffffcb90a0f338f8 x25: ffff00001fff60d0 x24:
ffff0000035d0000
[    0.000000][    T0] x23: 0400000000000001 x22: 0c00000000000001 x21:
ffff0000035dffff
[    0.000000][    T0] x20: ffffcb909fe3b7f0 x19: ffff0000035e0000 x18:
ffffffffffffffff
[    0.000000][    T0] x17: 7220303030303178 x16: 307e303030306435 x15:
ffffcb90a0f334c8
[    0.000000][    T0] x14: 0000000000000000 x13: 205d305420202020 x12:
5b5d303030303030
[    0.000000][    T0] x11: 00000000ffff7fff x10: 00000000ffff7fff x9 :
ffffcb909f1e27d8
[    0.000000][    T0] x8 : 00000000000bffe8 x7 : c0000000ffff7fff x6 :
0000000000000001
[    0.000000][    T0] x5 : 0000000000000001 x4 : 0078000083400705 x3 :
ffffcb90a0f338f8
[    0.000000][    T0] x2 : 0000000000010000 x1 : ffff0000035d0000 x0 :
ffff00001fff60d0
[    0.000000][    T0] Call trace:
[    0.000000][    T0]  pageattr_pmd_entry+0x60/0x78
[    0.000000][    T0]  walk_pud_range+0x124/0x190
[    0.000000][    T0]  walk_pgd_range+0x158/0x1b0
[    0.000000][    T0] walk_kernel_page_table_range_lockless+0x58/0x98 [    0.000000][    T0]  update_range_prot+0xb8/0x108
[    0.000000][    T0]  __change_memory_common+0x30/0x1a8
[    0.000000][    T0] __set_memory_enc_dec.part.0+0x170/0x260
[    0.000000][    T0]  realm_set_memory_decrypted+0x6c/0xb0
[    0.000000][    T0]  set_memory_decrypted+0x38/0x58
[    0.000000][    T0]  its_alloc_pages_node+0xc4/0x140
[    0.000000][    T0]  its_probe_one+0xbc/0x3c0
[    0.000000][    T0]  its_of_probe.isra.0+0x130/0x220
[    0.000000][    T0]  its_init+0x160/0x2f8
[    0.000000][    T0]  gic_init_bases+0x1fc/0x318
[    0.000000][    T0]  gic_of_init+0x2a0/0x300
[    0.000000][    T0]  of_irq_init+0x238/0x4b8
[    0.000000][    T0]  irqchip_init+0x20/0x50
[    0.000000][    T0]  init_IRQ+0x1c/0x100
[    0.000000][    T0]  start_kernel+0x1ec/0x4f0
[    0.000000][    T0]  __primary_switched+0xbc/0xd0
[    0.000000][    T0] ---[ end trace 0000000000000000 ]---
[    0.000000][    T0] ------------[ cut here ]------------
[    0.000000][    T0] Failed to decrypt memory, 16 pages will be leaked

realm feature relies on rodata=full to dynamically update kernel page table prot.

In init_IRQ(), realm_set_memory_decrypted() is called to update kernel page table prot.
At this time, secondary cpus aren't booted, BBML2 noabort feature isn't initializated,
and system_supports_bbml2_noabort() still returns false. As a result, split_kernel_leaf_mapping() is skipped, leading to WARN_ON_ONCE((next - addr) !=
PMD_SIZE)
in pageattr_pmd_entry().

Quoted 7 lines
Quoted 7 lines
Quoted 4 lines

If no secondary cpus are yet running, then it is technically safe to split because we know all online cpus (i.e. just the boot cpu) supports BBML2_NOABORT. So we could explicitly only disallow splitting during the window between booting secondary cpus and finalizing the system caps. Feels a bit hacky though...

I think we can check whether system feature has been finalized or not. If it has not been finalized yet, we just need to check whether the current cpu (should be just boot cpu) supports BBML2_NOABORT or not. It sounds ok to me.

No I don't think that's sufficient; if the secondary cpus are started (even if not running the code path doing the split) we have to assume the secondary cpus are sharing the linear map pgtables, so if we split them on the boot cpu and the secondary cpus don't support BBML2_NOABORT, things could break.

I think 2 options would be:

- disallow split for the window between starting the secondary cpus and
finalizing the system caps.

- Do the split in stop_machine() if any request for splitting is made between
starting the secondary cpus and finalizing the system caps.

Quoted 7 lines

Both feel pretty ugly. I'll have a chat with Catalin and try to guage opinons...

In the meantime, would you mind trying this (uncompiled, untested) patch? It's attempting to implement option 1. TBH, I'm not sure if this is legal since we will now try to get a mutex; is that allowed in early code that can't sleep? I guess we only have a single thread running so there can't be any contention...

page table is allocated from buddy with GFP_PGTABLE_KERNEL. In init_IRQ(), the buddy is initialized, but we shoudn't assume it? And, is GFP_PGTABLE_KERNEL reasonable here? allocation may block.

Quoted 371 lines

---8<---

arch/arm64/mm/mmu.c +10 -1
index 8e1d80a7033e3..72790126db55c 100644
@@ -779,7 +779,16 @@ int split_kernel_leaf_mapping(unsigned long start, unsigned
 long end)
  	 * and let the permission change code raise a warning if not already
  	 * pte-mapped.
  	 */
-	if (!system_supports_bbml2_noabort())
+	if (system_capabilities_finalized() && !system_supports_bbml2_noabort())
+		return 0;
+
+	/*
+	 * If system capabilities are not finalized and there is only 1 online
+	 * cpu, then we must be running on the boot cpu during early boot before
+	 * any secondaries have started. If the boot cpu supports bbml2, we can
+	 * safely split.
+	 */
+	if (num_online_cpus() > 1 || !cpu_supports_bbml2_noabort())
  		return 0;
 

/*
---8<---

Thanks,
Ryan

Quoted 338 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

Before setup_system_features(), we don't know if all cpus support BBML2 noabort,
and we
couldn't split kernel page table, in case another cpu that doesn't support BBML2
noabort
is running.

How could we fix this issue?

Quoted 8 lines
Quoted 5 lines
Quoted 5 lines

1. force pte mapping if realm feature is enabled? Although force_pte_mapping() return true if is_realm_world() return true, arm64_rsi_init() is called after map_mem(). So is_realm_world() still return false during map_mem(). Thus realm feature relies on rodata=full. If we fix by this solution, we need to add a new cmdline to force pte mapping.

I don't quite get why is_realm_world() relies on rodata=full. I understand realm needs PTE mapping if BBML2_NOABORT is not supported. But it doesn't mean real relies on rodata=full.

https://lore.kernel.org/all/[email protected]/

This is the discussion why realm relies on rodata=full. The initization of realm coudn't move to before map_mem(), so is_realm_world() is false. As a result, realm need rodata=full to indicate we need to make pages shared/protected at page granularity.

Quoted 313 lines
Quoted 10 lines

I think we just need to make is_realm_world() work earlier in boot? I think this has been a known issue for a while. Not sure if there is any plan to fix it though.

Quoted 1 line

2. If we could try to split kernel page table before setup_system_features()?

Another option would be to initially map by pte then collapse to block mappings once we have determined that all cpus support BBML2_NOABORT. We originally opted not to do that because it's a tax on symetric systems. But we could throw in the towel if it's the least bad solution we can come up with for solving this. I think it might help some of Kevin's use cases too?

May be an option too. When we discussed this there was no usecase for direct mapping collapse. But if we can have multiple usecases, it may be worth it. AFAICT, the ROX execmem cache may need this, which Will or someone else from Google is going to work on.

Checking current cpu BBML2_NOABORT capability before system feature is finalized seems like a fast way to stop bleeding IMHO before we find more elegant long-term solution.

Thanks,
Yang

Quoted 290 lines

Thanks,
Ryan

Quoted 286 lines

Thanks.

Quoted 284 lines

Ryan tested v7 on an AmpereOne system (a VM with 12G RAM) in all 3 possible modes by hacking the BBML2 feature detection code:

    - mode 1: All CPUs support BBML2 so the linear map uses large mappings
    - mode 2: Boot CPU does not support BBML2 so linear map uses pte mappings
    - mode 3: Boot CPU supports BBML2 but secondaries do not so linear map initially uses large mappings but is then repainted to use pte mappings

In all cases, mm selftests run and no regressions are observed. In all cases, ptdump of linear map is as expected. Because there are just some cleanups between v7 and v8, so I kept using Ryan's test result:

Mode 1:
=======
---[ Linear Mapping start ]--- 0xffff000000000000-0xffff000000200000           2M PMD RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED 0xffff000000200000-0xffff000000210000          64K PTE RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000000210000-0xffff000000400000        1984K PTE ro NX SHD AF            UXN    MEM/NORMAL 0xffff000000400000-0xffff000002400000          32M PMD ro NX SHD AF        BLK UXN    MEM/NORMAL 0xffff000002400000-0xffff000002550000        1344K PTE ro NX SHD AF            UXN    MEM/NORMAL 0xffff000002550000-0xffff000002600000         704K PTE RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000002600000-0xffff000004000000          26M PMD RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED 0xffff000004000000-0xffff000040000000         960M PMD RW NX SHD AF CON BLK UXN    MEM/NORMAL-TAGGED 0xffff000040000000-0xffff000140000000           4G PUD RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED 0xffff000140000000-0xffff000142000000          32M PMD RW NX SHD AF CON BLK UXN    MEM/NORMAL-TAGGED 0xffff000142000000-0xffff000142120000        1152K PTE RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000142120000-0xffff000142128000          32K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142128000-0xffff000142159000         196K PTE ro NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142159000-0xffff000142160000          28K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142160000-0xffff000142240000         896K PTE RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000142240000-0xffff00014224e000          56K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff00014224e000-0xffff000142250000           8K PTE ro NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142250000-0xffff000142260000          64K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142260000-0xffff000142280000         128K PTE RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000142280000-0xffff000142288000          32K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142288000-0xffff000142290000          32K PTE ro NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142290000-0xffff0001422a0000          64K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff0001422a0000-0xffff000142465000        1812K PTE ro NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142465000-0xffff000142470000          44K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142470000-0xffff000142600000        1600K PTE RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000142600000-0xffff000144000000          26M PMD RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED 0xffff000144000000-0xffff000180000000         960M PMD RW NX SHD AF CON BLK UXN    MEM/NORMAL-TAGGED 0xffff000180000000-0xffff000181a00000          26M PMD RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED 0xffff000181a00000-0xffff000181b90000        1600K PTE RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000181b90000-0xffff000181b9d000          52K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000181b9d000-0xffff000181c80000         908K PTE ro NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000181c80000-0xffff000181c90000          64K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000181c90000-0xffff000181ca0000          64K PTE RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000181ca0000-0xffff000181dbd000        1140K PTE ro NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000181dbd000-0xffff000181dc0000          12K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000181dc0000-0xffff000181e00000         256K PTE RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000181e00000-0xffff000182000000           2M PMD RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED 0xffff000182000000-0xffff0001c0000000         992M PMD RW NX SHD AF CON BLK UXN    MEM/NORMAL-TAGGED 0xffff0001c0000000-0xffff000300000000           5G PUD RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED 0xffff000300000000-0xffff008000000000         500G PUD 0xffff008000000000-0xffff800000000000      130560G PGD ---[ Linear Mapping end ]---

Mode 3:
=======
---[ Linear Mapping start ]--- 0xffff000000000000-0xffff000000210000        2112K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000000210000-0xffff000000400000        1984K PTE ro NX SHD AF            UXN    MEM/NORMAL 0xffff000000400000-0xffff000002400000          32M PMD ro NX SHD AF        BLK UXN    MEM/NORMAL 0xffff000002400000-0xffff000002550000        1344K PTE ro NX SHD AF            UXN    MEM/NORMAL 0xffff000002550000-0xffff000143a61000     5264452K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000143a61000-0xffff000143c61000           2M PTE ro NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000143c61000-0xffff000181b9a000     1015012K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000181b9a000-0xffff000181d9a000           2M PTE ro NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000181d9a000-0xffff000300000000     6261144K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000300000000-0xffff008000000000         500G PUD 0xffff008000000000-0xffff800000000000      130560G PGD ---[ Linear Mapping end ]---

Performance Testing
===================
* Memory use after boot
Before:
MemTotal:       258988984 kB
MemFree:        254821700 kB

After:
MemTotal:       259505132 kB
MemFree:        255410264 kB

Around 500MB more memory are free to use.  The larger the machine, the more memory saved.

* Memcached
We saw performance degradation when running Memcached benchmark with rodata=full vs rodata=on.  Our profiling pointed to kernel TLB pressure. With this patchset we saw ops/sec is increased by around 3.5%, P99 latency is reduced by around 9.6%.
The gain mainly came from reduced kernel TLB misses.  The kernel TLB MPKI is reduced by 28.5%.

The benchmark data is now on par with rodata=on too.

* Disk encryption (dm-crypt) benchmark
Ran fio benchmark with the below command on a 128G ramdisk (ext4) with disk encryption (by dm-crypt).
fio --directory=/data --random_generator=lfsr --norandommap            \
      --randrepeat 1 --status-interval=999 --rw=write --bs=4k --loops=1  \
      --ioengine=sync --iodepth=1 --numjobs=1 --fsync_on_close=1         \
      --group_reporting --thread --name=iops-test-job --eta-newline=1    \
      --size 100G

The IOPS is increased by 90% - 150% (the variance is high, but the worst number of good case is around 90% more than the best number of bad case). The bandwidth is increased and the avg clat is reduced proportionally.

* Sequential file read
Read 100G file sequentially on XFS (xfs_io read with page cache populated). The bandwidth is increased by 150%.

Additionally Ryan also ran this through a random selection of benchmarks on AmpereOne. None show any regressions, and various benchmarks show statistically
significant improvement. I'm just showing those improvements here:

+----------------------
+----------------------------------------------------------
+-------------------------+
| Benchmark            | Result
Class                                             | Improvement vs 6.17-rc1 |
+======================+==========================================================+=========================+
| micromm/vmalloc      | full_fit_alloc_test: p:1, h:0, l:500000
(usec)           |              (I) -9.00% |
|                      | kvfree_rcu_1_arg_vmalloc_test: p:1, h:0, l:500000
(usec) |              (I) -6.93% |
|                      | kvfree_rcu_2_arg_vmalloc_test: p:1, h:0, l:500000
(usec) |              (I) -6.77% |
|                      | pcpu_alloc_test: p:1, h:0, l:500000
(usec)               |              (I) -4.63% |
+----------------------
+----------------------------------------------------------
+-------------------------+
| mmtests/hackbench    | process-sockets-30
(seconds)                             |              (I) -2.96% |
+----------------------
+----------------------------------------------------------
+-------------------------+
| mmtests/kernbench    | syst-192
(seconds) |             (I) -12.77% |
+----------------------
+----------------------------------------------------------
+-------------------------+
| pts/perl-benchmark   | Test: Interpreter
(Seconds)                              |              (I) -4.86% |
+----------------------
+----------------------------------------------------------
+-------------------------+
| pts/pgbench          | Scale: 1 Clients: 1 Read Write
(TPS)                     |               (I) 5.07% |
|                      | Scale: 1 Clients: 1 Read Write - Latency
(ms)            |              (I) -4.72% |
|                      | Scale: 100 Clients: 1000 Read Write
(TPS)                |               (I) 2.58% |
|                      | Scale: 100 Clients: 1000 Read Write - Latency
(ms)       |              (I) -2.52% |
+----------------------
+----------------------------------------------------------
+-------------------------+
| pts/sqlite-speedtest | Timed Time - Size 1,000
(Seconds)                        |              (I) -2.68% |
+----------------------
+----------------------------------------------------------
+-------------------------+

Changes since v7 [1]
====================
- Rebased on v6.17-rc6 and Shijie's rodata series (https://git.kernel.org/pub/
scm/linux/kernel/git/arm64/linux.git/commit/?id=bfbbb0d3215f)
    which has been picked up by Will.
- Patch 1: Fixed pmd_leaf/pud_leaf issue since the code may need to change permission for invalid entries per Jinjiang Tu.
- Patch 1: Removed pageattr_pgd_entry and pageattr_p4d_entry per Ryan.
- Used (-1ULL) instead of -1 per Catalin.
- Added comment about arm64 lazy mmu allow sleeping per Ryan.
- Squashed patch #4 in v7 into patch #3.
- Squashed patch #6 in v7 into patch #4.
- Added patch #5 to fix a arm64 kprobes bug. It guarantees set_memory_rox() is called before vfree(). It can go into separately or with this series together.
- Collected all the R-bs and A-bs.

Changes since v6 [2]
====================
- Patch 1: Minor refactor to implement walk_kernel_page_table_range() in terms of walk_kernel_page_table_range_lockless(). Also lead to adding *pmd
argument
    to the lockless variant for consistency (per Catalin).
- Misc function/variable renames to improve clarity and consistency.
- Share same syncrhonization flag between idmap_kpti_install_ng_mappings and wait_linear_map_split_to_ptes, which allows removal of bbml2_ptes[] to
save
    ~20K from kernel image.
- Only take pgtable_split_lock and enter lazy mmu mode once for both splits.
- Only walk the pgtable once for the common "split single page" case.
- Bypass split to contpmd and contpte when spllitting linear map to ptes.

[1] https://lore.kernel.org/linux-arm-kernel/20250829115250.2395585-1-
[email protected]/
[2] https://lore.kernel.org/linux-arm-kernel/20250805081350.3854670-1-
[email protected]/

Dev Jain (1):
        arm64: Enable permission change on arm64 kernel block mappings

Ryan Roberts (1):
        arm64: mm: split linear mapping if BBML2 unsupported on secondary CPUs

Yang Shi (3):
        arm64: cpufeature: add AmpereOne to BBML2 allow list
        arm64: mm: support large block mapping when rodata=full
        arm64: kprobes: call set_memory_rox() for kprobe page

    arch/arm64/include/asm/cpufeature.h |   2 +
    arch/arm64/include/asm/mmu.h        |   3 +
    arch/arm64/include/asm/pgtable.h    |   5 ++
    arch/arm64/kernel/cpufeature.c      |  12 +++-
    arch/arm64/kernel/probes/kprobes.c  |  12 ++++
    arch/arm64/mm/mmu.c                 | 422 ++++++++++++++++++++++++++++++
++++
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+----
    arch/arm64/mm/pageattr.c            | 123 ++++++++++++++++++++++++---------
    arch/arm64/mm/proc.S                |  27 ++++++--
    include/linux/pagewalk.h            |   3 +
    mm/pagewalk.c                       |  36 ++++++----
    10 files changed, 581 insertions(+), 64 deletions(-)
#42 lore raw
Quoted 118 lines · Jinjiang Tu
Quoted 116 lines · Ryan Roberts
Quoted 96 lines · Jinjiang Tu
Quoted 95 lines · Yang Shi
Quoted 13 lines · Ryan Roberts

Thanks for the report!

+ Kevin, who was looking at some adjacent issues and may have some ideas for how
to fix.

Quoted 5 lines · Jinjiang Tu
Quoted 3 lines · Yang Shi

On systems with BBML2_NOABORT support, it causes the linear map to be mapped with large blocks, even when rodata=full, and leads to some nice performance improvements.

Hi,

Hi Jinjiang,

Thanks for reporting the problem.

Quoted 76 lines
Quoted 61 lines

I find this feature is incompatible with realm. The calltrace is as follows:

[    0.000000][    T0] ------------[ cut here ]------------
[    0.000000][    T0] WARNING: CPU: 0 PID: 0 at arch/arm64/mm/pageattr.c:56 pageattr_pmd_entry+0x60/0x78
[    0.000000][    T0] Modules linked in:
[    0.000000][    T0] CPU: 0 PID: 0 Comm: swapper/0 Not tainted 6.6.0 #16 [    0.000000][    T0] Hardware name: linux,dummy-virt (DT)
[    0.000000][    T0] pstate: 800000c5 (Nzcv daIF -PAN -UAO -TCO -DIT -SSBS BTYPE=--)
[    0.000000][    T0] pc : pageattr_pmd_entry+0x60/0x78
[    0.000000][    T0] lr : walk_pmd_range.isra.0+0x170/0x1f0
[    0.000000][    T0] sp : ffffcb90a0f337d0
[    0.000000][    T0] x29: ffffcb90a0f337d0 x28: 0000000000000000 x27:
ffff0000035e0000
[    0.000000][    T0] x26: ffffcb90a0f338f8 x25: ffff00001fff60d0 x24:
ffff0000035d0000
[    0.000000][    T0] x23: 0400000000000001 x22: 0c00000000000001 x21:
ffff0000035dffff
[    0.000000][    T0] x20: ffffcb909fe3b7f0 x19: ffff0000035e0000 x18:
ffffffffffffffff
[    0.000000][    T0] x17: 7220303030303178 x16: 307e303030306435 x15:
ffffcb90a0f334c8
[    0.000000][    T0] x14: 0000000000000000 x13: 205d305420202020 x12:
5b5d303030303030
[    0.000000][    T0] x11: 00000000ffff7fff x10: 00000000ffff7fff x9 :
ffffcb909f1e27d8
[    0.000000][    T0] x8 : 00000000000bffe8 x7 : c0000000ffff7fff x6 :
0000000000000001
[    0.000000][    T0] x5 : 0000000000000001 x4 : 0078000083400705 x3 :
ffffcb90a0f338f8
[    0.000000][    T0] x2 : 0000000000010000 x1 : ffff0000035d0000 x0 :
ffff00001fff60d0
[    0.000000][    T0] Call trace:
[    0.000000][    T0]  pageattr_pmd_entry+0x60/0x78
[    0.000000][    T0]  walk_pud_range+0x124/0x190
[    0.000000][    T0]  walk_pgd_range+0x158/0x1b0
[    0.000000][    T0] walk_kernel_page_table_range_lockless+0x58/0x98 [    0.000000][    T0]  update_range_prot+0xb8/0x108
[    0.000000][    T0]  __change_memory_common+0x30/0x1a8
[    0.000000][    T0] __set_memory_enc_dec.part.0+0x170/0x260
[    0.000000][    T0]  realm_set_memory_decrypted+0x6c/0xb0
[    0.000000][    T0]  set_memory_decrypted+0x38/0x58
[    0.000000][    T0]  its_alloc_pages_node+0xc4/0x140
[    0.000000][    T0]  its_probe_one+0xbc/0x3c0
[    0.000000][    T0]  its_of_probe.isra.0+0x130/0x220
[    0.000000][    T0]  its_init+0x160/0x2f8
[    0.000000][    T0]  gic_init_bases+0x1fc/0x318
[    0.000000][    T0]  gic_of_init+0x2a0/0x300
[    0.000000][    T0]  of_irq_init+0x238/0x4b8
[    0.000000][    T0]  irqchip_init+0x20/0x50
[    0.000000][    T0]  init_IRQ+0x1c/0x100
[    0.000000][    T0]  start_kernel+0x1ec/0x4f0
[    0.000000][    T0]  __primary_switched+0xbc/0xd0
[    0.000000][    T0] ---[ end trace 0000000000000000 ]---
[    0.000000][    T0] ------------[ cut here ]------------
[    0.000000][    T0] Failed to decrypt memory, 16 pages will be leaked

realm feature relies on rodata=full to dynamically update kernel page table prot.

Quoted 9 lines

In init_IRQ(), realm_set_memory_decrypted() is called to update kernel page table prot.
At this time, secondary cpus aren't booted, BBML2 noabort feature isn't initializated,
and system_supports_bbml2_noabort() still returns false. As a result, split_kernel_leaf_mapping() is skipped, leading to WARN_ON_ONCE((next - addr) !=
PMD_SIZE)
in pageattr_pmd_entry().

If no secondary cpus are yet running, then it is technically safe to split because we know all online cpus (i.e. just the boot cpu) supports BBML2_NOABORT.
So we could explicitly only disallow splitting during the window between booting
secondary cpus and finalizing the system caps. Feels a bit hacky though...

Quoted 3 lines
Quoted 3 lines

I think we can check whether system feature has been finalized or not. If it has not been finalized yet, we just need to check whether the current cpu (should be just boot cpu) supports BBML2_NOABORT or not. It sounds ok to me.

No I don't think that's sufficient; if the secondary cpus are started (even if not running the code path doing the split) we have to assume the secondary cpus are sharing the linear map pgtables, so if we split them on the boot cpu and the secondary cpus don't support BBML2_NOABORT, things could break.

I think 2 options would be:

  - disallow split for the window between starting the secondary cpus and finalizing the system caps.

  - Do the split in stop_machine() if any request for splitting is made between starting the secondary cpus and finalizing the system caps.

Both feel pretty ugly. I'll have a chat with Catalin and try to guage opinons...

Quoted 7 lines
Quoted 4 lines

In the meantime, would you mind trying this (uncompiled, untested) patch? It's attempting to implement option 1. TBH, I'm not sure if this is legal since we will now try to get a mutex; is that allowed in early code that can't sleep? I guess we only have a single thread running so there can't be any contention...

page table is allocated from buddy with GFP_PGTABLE_KERNEL. In init_IRQ(), the buddy is initialized, but we shoudn't assume it?

Yes that's a fair point. So we also need to reject split requests made prior to initializing the buddy. It looks like there is an optional mem_init() arch hook which arm64 doesn't currently use which we may be able to use to signal "buddy available"?

Quoted 2 lines

And, is GFP_PGTABLE_KERNEL
reasonable here? allocation may block.

I think in practice it should be fine; this early in boot we can't possibly be out of memory so won't try to block.

But I was really just intending testing with this patch to validate that there weren't any other issues, not proposing it as the final fix. Personally I'm leaning more and more to initially mapping by PTE then collapsing once we know the capabilities of the whole system.

Thanks,
Ryan

Quoted 386 lines
Quoted 386 lines

---8<---

arch/arm64/mm/mmu.c +10 -1
index 8e1d80a7033e3..72790126db55c 100644
@@ -779,7 +779,16 @@ int split_kernel_leaf_mapping(unsigned long start, unsigned
 long end)
        * and let the permission change code raise a warning if not already
        * pte-mapped.
        */
-    if (!system_supports_bbml2_noabort())
+    if (system_capabilities_finalized() && !system_supports_bbml2_noabort())
+        return 0;
+
+    /*
+     * If system capabilities are not finalized and there is only 1 online
+     * cpu, then we must be running on the boot cpu during early boot before
+     * any secondaries have started. If the boot cpu supports bbml2, we can
+     * safely split.
+     */
+    if (num_online_cpus() > 1 || !cpu_supports_bbml2_noabort())
           return 0;
 

      /*
---8<---

Thanks,
Ryan

Quoted 352 lines
Quoted 16 lines
Quoted 8 lines
Quoted 8 lines

Before setup_system_features(), we don't know if all cpus support BBML2 noabort,
and we
couldn't split kernel page table, in case another cpu that doesn't support BBML2
noabort
is running.

Quoted 8 lines
Quoted 8 lines

How could we fix this issue?

1. force pte mapping if realm feature is enabled? Although force_pte_mapping()
return true if is_realm_world() return true, arm64_rsi_init() is called after map_mem(). So is_realm_world() still return false during map_mem(). Thus realm feature relies on rodata=full. If we fix by this solution, we need to add a new cmdline to force pte mapping.

Quoted 3 lines

I don't quite get why is_realm_world() relies on rodata=full. I understand realm needs PTE mapping if BBML2_NOABORT is not supported. But it doesn't mean real relies on rodata=full.

https://lore.kernel.org/all/[email protected]/

This is the discussion why realm relies on rodata=full. The initization of realm coudn't move to before map_mem(), so is_realm_world() is false. As a result, realm
need rodata=full to indicate we need to make pages shared/protected at page granularity.

Quoted 325 lines
Quoted 6 lines

I think we just need to make is_realm_world() work earlier in boot? I think this
has been a known issue for a while. Not sure if there is any plan to fix it though.

Quoted 1 line

2. If we could try to split kernel page table before setup_system_features()?

Quoted 8 lines

Another option would be to initially map by pte then collapse to block mappings
once we have determined that all cpus support BBML2_NOABORT. We originally opted
not to do that because it's a tax on symetric systems. But we could throw in the
towel if it's the least bad solution we can come up with for solving this. I think it might help some of Kevin's use cases too?

May be an option too. When we discussed this there was no usecase for direct mapping collapse. But if we can have multiple usecases, it may be worth it. AFAICT, the ROX execmem cache may need this, which Will or someone else from Google is going to work on.

Checking current cpu BBML2_NOABORT capability before system feature is finalized seems like a fast way to stop bleeding IMHO before we find more elegant long-term solution.

Thanks,
Yang

Quoted 298 lines

Thanks,
Ryan

Quoted 294 lines

Thanks.

Quoted 292 lines

Ryan tested v7 on an AmpereOne system (a VM with 12G RAM) in all 3 possible modes by hacking the BBML2 feature detection code:

     - mode 1: All CPUs support BBML2 so the linear map uses large mappings
     - mode 2: Boot CPU does not support BBML2 so linear map uses pte mappings
     - mode 3: Boot CPU supports BBML2 but secondaries do not so linear map initially uses large mappings but is then repainted to use pte mappings

In all cases, mm selftests run and no regressions are observed. In all cases,
ptdump of linear map is as expected. Because there are just some cleanups between v7 and v8, so I kept using Ryan's test result:

Mode 1:
=======
---[ Linear Mapping start ]--- 0xffff000000000000-0xffff000000200000           2M PMD RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED 0xffff000000200000-0xffff000000210000          64K PTE RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000000210000-0xffff000000400000        1984K PTE ro NX SHD AF            UXN    MEM/NORMAL 0xffff000000400000-0xffff000002400000          32M PMD ro NX SHD AF        BLK UXN    MEM/NORMAL 0xffff000002400000-0xffff000002550000        1344K PTE ro NX SHD AF            UXN    MEM/NORMAL 0xffff000002550000-0xffff000002600000         704K PTE RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000002600000-0xffff000004000000          26M PMD RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED 0xffff000004000000-0xffff000040000000         960M PMD RW NX SHD AF CON BLK UXN    MEM/NORMAL-TAGGED 0xffff000040000000-0xffff000140000000           4G PUD RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED 0xffff000140000000-0xffff000142000000          32M PMD RW NX SHD AF CON BLK UXN    MEM/NORMAL-TAGGED 0xffff000142000000-0xffff000142120000        1152K PTE RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000142120000-0xffff000142128000          32K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142128000-0xffff000142159000         196K PTE ro NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142159000-0xffff000142160000          28K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142160000-0xffff000142240000         896K PTE RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000142240000-0xffff00014224e000          56K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff00014224e000-0xffff000142250000           8K PTE ro NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142250000-0xffff000142260000          64K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142260000-0xffff000142280000         128K PTE RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000142280000-0xffff000142288000          32K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142288000-0xffff000142290000          32K PTE ro NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142290000-0xffff0001422a0000          64K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff0001422a0000-0xffff000142465000        1812K PTE ro NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142465000-0xffff000142470000          44K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142470000-0xffff000142600000        1600K PTE RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000142600000-0xffff000144000000          26M PMD RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED 0xffff000144000000-0xffff000180000000         960M PMD RW NX SHD AF CON BLK UXN    MEM/NORMAL-TAGGED 0xffff000180000000-0xffff000181a00000          26M PMD RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED 0xffff000181a00000-0xffff000181b90000        1600K PTE RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000181b90000-0xffff000181b9d000          52K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000181b9d000-0xffff000181c80000         908K PTE ro NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000181c80000-0xffff000181c90000          64K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000181c90000-0xffff000181ca0000          64K PTE RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000181ca0000-0xffff000181dbd000        1140K PTE ro NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000181dbd000-0xffff000181dc0000          12K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000181dc0000-0xffff000181e00000         256K PTE RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000181e00000-0xffff000182000000           2M PMD RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED 0xffff000182000000-0xffff0001c0000000         992M PMD RW NX SHD AF CON BLK UXN    MEM/NORMAL-TAGGED 0xffff0001c0000000-0xffff000300000000           5G PUD RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED 0xffff000300000000-0xffff008000000000         500G PUD 0xffff008000000000-0xffff800000000000      130560G PGD ---[ Linear Mapping end ]---

Mode 3:
=======
---[ Linear Mapping start ]--- 0xffff000000000000-0xffff000000210000        2112K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000000210000-0xffff000000400000        1984K PTE ro NX SHD AF            UXN    MEM/NORMAL 0xffff000000400000-0xffff000002400000          32M PMD ro NX SHD AF        BLK UXN    MEM/NORMAL 0xffff000002400000-0xffff000002550000        1344K PTE ro NX SHD AF            UXN    MEM/NORMAL 0xffff000002550000-0xffff000143a61000     5264452K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000143a61000-0xffff000143c61000           2M PTE ro NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000143c61000-0xffff000181b9a000     1015012K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000181b9a000-0xffff000181d9a000           2M PTE ro NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000181d9a000-0xffff000300000000     6261144K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000300000000-0xffff008000000000         500G PUD 0xffff008000000000-0xffff800000000000      130560G PGD ---[ Linear Mapping end ]---

Performance Testing
===================
* Memory use after boot
Before:
MemTotal:       258988984 kB
MemFree:        254821700 kB

After:
MemTotal:       259505132 kB
MemFree:        255410264 kB

Around 500MB more memory are free to use.  The larger the machine, the more memory saved.

* Memcached
We saw performance degradation when running Memcached benchmark with rodata=full vs rodata=on.  Our profiling pointed to kernel TLB pressure. With this patchset we saw ops/sec is increased by around 3.5%, P99 latency is reduced by around 9.6%.
The gain mainly came from reduced kernel TLB misses.  The kernel TLB MPKI is reduced by 28.5%.

The benchmark data is now on par with rodata=on too.

* Disk encryption (dm-crypt) benchmark
Ran fio benchmark with the below command on a 128G ramdisk (ext4) with disk encryption (by dm-crypt).
fio --directory=/data --random_generator=lfsr --norandommap            \
       --randrepeat 1 --status-interval=999 --rw=write --bs=4k --loops=1  \
       --ioengine=sync --iodepth=1 --numjobs=1 --fsync_on_close=1         \
       --group_reporting --thread --name=iops-test-job --eta-newline=1    \
       --size 100G

The IOPS is increased by 90% - 150% (the variance is high, but the worst number of good case is around 90% more than the best number of bad case). The bandwidth is increased and the avg clat is reduced proportionally.

* Sequential file read
Read 100G file sequentially on XFS (xfs_io read with page cache populated). The bandwidth is increased by 150%.

Additionally Ryan also ran this through a random selection of benchmarks on AmpereOne. None show any regressions, and various benchmarks show statistically
significant improvement. I'm just showing those improvements here:

+----------------------
+----------------------------------------------------------
+-------------------------+
| Benchmark            | Result
Class                                             | Improvement vs 6.17-
rc1 |
+======================+==========================================================+=========================+
| micromm/vmalloc      | full_fit_alloc_test: p:1, h:0, l:500000
(usec)           |              (I) -9.00% |
|                      | kvfree_rcu_1_arg_vmalloc_test: p:1, h:0, l:500000
(usec) |              (I) -6.93% |
|                      | kvfree_rcu_2_arg_vmalloc_test: p:1, h:0, l:500000
(usec) |              (I) -6.77% |
|                      | pcpu_alloc_test: p:1, h:0, l:500000
(usec)               |              (I) -4.63% |
+----------------------
+----------------------------------------------------------
+-------------------------+
| mmtests/hackbench    | process-sockets-30
(seconds)                             |              (I) -2.96% |
+----------------------
+----------------------------------------------------------
+-------------------------+
| mmtests/kernbench    | syst-192
(seconds) |             (I) -12.77% |
+----------------------
+----------------------------------------------------------
+-------------------------+
| pts/perl-benchmark   | Test: Interpreter
(Seconds)                              |              (I) -4.86% |
+----------------------
+----------------------------------------------------------
+-------------------------+
| pts/pgbench          | Scale: 1 Clients: 1 Read Write
(TPS)                     |               (I) 5.07% |
|                      | Scale: 1 Clients: 1 Read Write - Latency
(ms)            |              (I) -4.72% |
|                      | Scale: 100 Clients: 1000 Read Write
(TPS)                |               (I) 2.58% |
|                      | Scale: 100 Clients: 1000 Read Write - Latency
(ms)       |              (I) -2.52% |
+----------------------
+----------------------------------------------------------
+-------------------------+
| pts/sqlite-speedtest | Timed Time - Size 1,000
(Seconds)                        |              (I) -2.68% |
+----------------------
+----------------------------------------------------------
+-------------------------+

Changes since v7 [1]
====================
- Rebased on v6.17-rc6 and Shijie's rodata series (https://
git.kernel.org/pub/ scm/linux/kernel/git/arm64/linux.git/commit/?id=bfbbb0d3215f) which has been picked up by Will.
- Patch 1: Fixed pmd_leaf/pud_leaf issue since the code may need to change permission for invalid entries per Jinjiang Tu.
- Patch 1: Removed pageattr_pgd_entry and pageattr_p4d_entry per Ryan.
- Used (-1ULL) instead of -1 per Catalin.
- Added comment about arm64 lazy mmu allow sleeping per Ryan.
- Squashed patch #4 in v7 into patch #3.
- Squashed patch #6 in v7 into patch #4.
- Added patch #5 to fix a arm64 kprobes bug. It guarantees set_memory_rox() is called before vfree(). It can go into separately or with this series together.
- Collected all the R-bs and A-bs.

Changes since v6 [2]
====================
- Patch 1: Minor refactor to implement walk_kernel_page_table_range() in terms
     of walk_kernel_page_table_range_lockless(). Also lead to adding *pmd argument
     to the lockless variant for consistency (per Catalin).
- Misc function/variable renames to improve clarity and consistency.
- Share same syncrhonization flag between idmap_kpti_install_ng_mappings and wait_linear_map_split_to_ptes, which allows removal of bbml2_ptes[] to save
     ~20K from kernel image.
- Only take pgtable_split_lock and enter lazy mmu mode once for both splits.
- Only walk the pgtable once for the common "split single page" case.
- Bypass split to contpmd and contpte when spllitting linear map to ptes.

[1] https://lore.kernel.org/linux-arm-kernel/20250829115250.2395585-1-
[email protected]/
[2] https://lore.kernel.org/linux-arm-kernel/20250805081350.3854670-1-
[email protected]/

Dev Jain (1):
         arm64: Enable permission change on arm64 kernel block mappings

Ryan Roberts (1):
         arm64: mm: split linear mapping if BBML2 unsupported on secondary CPUs

Yang Shi (3):
         arm64: cpufeature: add AmpereOne to BBML2 allow list
         arm64: mm: support large block mapping when rodata=full
         arm64: kprobes: call set_memory_rox() for kprobe page

    arch/arm64/include/asm/cpufeature.h |   2 + arch/arm64/include/asm/mmu.h        |   3 + arch/arm64/include/asm/pgtable.h    |   5 ++ arch/arm64/kernel/cpufeature.c      |  12 +++- arch/arm64/kernel/probes/kprobes.c  |  12 ++++ arch/arm64/mm/mmu.c                 | 422 ++++++++++++++++++++++++++++++
++++
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

+----
    arch/arm64/mm/pageattr.c            | 123 +++++++++++++++++++++++
+---------
    arch/arm64/mm/proc.S                |  27 ++++++--
    include/linux/pagewalk.h            |   3 +
    mm/pagewalk.c                       |  36 ++++++----
    10 files changed, 581 insertions(+), 64 deletions(-)
#43 lore raw
Quoted 130 lines · Ryan Roberts
Quoted 116 lines · Jinjiang Tu
Quoted 115 lines · Ryan Roberts
Quoted 95 lines · Jinjiang Tu
Quoted 94 lines · Yang Shi
Quoted 13 lines · Ryan Roberts

Thanks for the report!

+ Kevin, who was looking at some adjacent issues and may have some ideas for how
to fix.

Quoted 5 lines · Jinjiang Tu
Quoted 3 lines · Yang Shi

On systems with BBML2_NOABORT support, it causes the linear map to be mapped with large blocks, even when rodata=full, and leads to some nice performance improvements.

Hi,

Hi Jinjiang,

Thanks for reporting the problem.

Quoted 76 lines
Quoted 61 lines

I find this feature is incompatible with realm. The calltrace is as follows:

[    0.000000][    T0] ------------[ cut here ]------------
[    0.000000][    T0] WARNING: CPU: 0 PID: 0 at arch/arm64/mm/pageattr.c:56 pageattr_pmd_entry+0x60/0x78
[    0.000000][    T0] Modules linked in:
[    0.000000][    T0] CPU: 0 PID: 0 Comm: swapper/0 Not tainted 6.6.0 #16 [    0.000000][    T0] Hardware name: linux,dummy-virt (DT)
[    0.000000][    T0] pstate: 800000c5 (Nzcv daIF -PAN -UAO -TCO -DIT -SSBS BTYPE=--)
[    0.000000][    T0] pc : pageattr_pmd_entry+0x60/0x78
[    0.000000][    T0] lr : walk_pmd_range.isra.0+0x170/0x1f0
[    0.000000][    T0] sp : ffffcb90a0f337d0
[    0.000000][    T0] x29: ffffcb90a0f337d0 x28: 0000000000000000 x27:
ffff0000035e0000
[    0.000000][    T0] x26: ffffcb90a0f338f8 x25: ffff00001fff60d0 x24:
ffff0000035d0000
[    0.000000][    T0] x23: 0400000000000001 x22: 0c00000000000001 x21:
ffff0000035dffff
[    0.000000][    T0] x20: ffffcb909fe3b7f0 x19: ffff0000035e0000 x18:
ffffffffffffffff
[    0.000000][    T0] x17: 7220303030303178 x16: 307e303030306435 x15:
ffffcb90a0f334c8
[    0.000000][    T0] x14: 0000000000000000 x13: 205d305420202020 x12:
5b5d303030303030
[    0.000000][    T0] x11: 00000000ffff7fff x10: 00000000ffff7fff x9 :
ffffcb909f1e27d8
[    0.000000][    T0] x8 : 00000000000bffe8 x7 : c0000000ffff7fff x6 :
0000000000000001
[    0.000000][    T0] x5 : 0000000000000001 x4 : 0078000083400705 x3 :
ffffcb90a0f338f8
[    0.000000][    T0] x2 : 0000000000010000 x1 : ffff0000035d0000 x0 :
ffff00001fff60d0
[    0.000000][    T0] Call trace:
[    0.000000][    T0]  pageattr_pmd_entry+0x60/0x78
[    0.000000][    T0]  walk_pud_range+0x124/0x190
[    0.000000][    T0]  walk_pgd_range+0x158/0x1b0
[    0.000000][    T0] walk_kernel_page_table_range_lockless+0x58/0x98 [    0.000000][    T0]  update_range_prot+0xb8/0x108
[    0.000000][    T0]  __change_memory_common+0x30/0x1a8
[    0.000000][    T0] __set_memory_enc_dec.part.0+0x170/0x260
[    0.000000][    T0]  realm_set_memory_decrypted+0x6c/0xb0
[    0.000000][    T0]  set_memory_decrypted+0x38/0x58
[    0.000000][    T0]  its_alloc_pages_node+0xc4/0x140
[    0.000000][    T0]  its_probe_one+0xbc/0x3c0
[    0.000000][    T0]  its_of_probe.isra.0+0x130/0x220
[    0.000000][    T0]  its_init+0x160/0x2f8
[    0.000000][    T0]  gic_init_bases+0x1fc/0x318
[    0.000000][    T0]  gic_of_init+0x2a0/0x300
[    0.000000][    T0]  of_irq_init+0x238/0x4b8
[    0.000000][    T0]  irqchip_init+0x20/0x50
[    0.000000][    T0]  init_IRQ+0x1c/0x100
[    0.000000][    T0]  start_kernel+0x1ec/0x4f0
[    0.000000][    T0]  __primary_switched+0xbc/0xd0
[    0.000000][    T0] ---[ end trace 0000000000000000 ]---
[    0.000000][    T0] ------------[ cut here ]------------
[    0.000000][    T0] Failed to decrypt memory, 16 pages will be leaked

realm feature relies on rodata=full to dynamically update kernel page table prot.

Quoted 9 lines

In init_IRQ(), realm_set_memory_decrypted() is called to update kernel page table prot.
At this time, secondary cpus aren't booted, BBML2 noabort feature isn't initializated,
and system_supports_bbml2_noabort() still returns false. As a result, split_kernel_leaf_mapping() is skipped, leading to WARN_ON_ONCE((next - addr) !=
PMD_SIZE)
in pageattr_pmd_entry().

If no secondary cpus are yet running, then it is technically safe to split because we know all online cpus (i.e. just the boot cpu) supports BBML2_NOABORT.
So we could explicitly only disallow splitting during the window between booting
secondary cpus and finalizing the system caps. Feels a bit hacky though...

Quoted 3 lines
Quoted 3 lines

I think we can check whether system feature has been finalized or not. If it has not been finalized yet, we just need to check whether the current cpu (should be just boot cpu) supports BBML2_NOABORT or not. It sounds ok to me.

No I don't think that's sufficient; if the secondary cpus are started (even if not running the code path doing the split) we have to assume the secondary cpus are sharing the linear map pgtables, so if we split them on the boot cpu and the secondary cpus don't support BBML2_NOABORT, things could break.

I think 2 options would be:

  - disallow split for the window between starting the secondary cpus and
finalizing the system caps.

  - Do the split in stop_machine() if any request for splitting is made between
starting the secondary cpus and finalizing the system caps.

Both feel pretty ugly. I'll have a chat with Catalin and try to guage opinons...

Quoted 6 lines
Quoted 4 lines

In the meantime, would you mind trying this (uncompiled, untested) patch? It's attempting to implement option 1. TBH, I'm not sure if this is legal since we will now try to get a mutex; is that allowed in early code that can't sleep? I guess we only have a single thread running so there can't be any contention...

page table is allocated from buddy with GFP_PGTABLE_KERNEL. In init_IRQ(), the buddy is initialized, but we shoudn't assume it?

Yes that's a fair point. So we also need to reject split requests made prior to initializing the buddy. It looks like there is an optional mem_init() arch hook which arm64 doesn't currently use which we may be able to use to signal "buddy available"?

Quoted 2 lines

And, is GFP_PGTABLE_KERNEL
reasonable here? allocation may block.

Quoted 7 lines

I think in practice it should be fine; this early in boot we can't possibly be out of memory so won't try to block.

But I was really just intending testing with this patch to validate that there weren't any other issues, not proposing it as the final fix. Personally I'm leaning more and more to initially mapping by PTE then collapsing once we know the capabilities of the whole system.

I will test it ASAP when the test environment is available.

Quoted 388 lines

Thanks,
Ryan

Quoted 382 lines
Quoted 382 lines

---8<---

arch/arm64/mm/mmu.c +10 -1
index 8e1d80a7033e3..72790126db55c 100644
@@ -779,7 +779,16 @@ int split_kernel_leaf_mapping(unsigned long start, unsigned
 long end)
        * and let the permission change code raise a warning if not already
        * pte-mapped.
        */
-    if (!system_supports_bbml2_noabort())
+    if (system_capabilities_finalized() && !system_supports_bbml2_noabort())
+        return 0;
+
+    /*
+     * If system capabilities are not finalized and there is only 1 online
+     * cpu, then we must be running on the boot cpu during early boot before
+     * any secondaries have started. If the boot cpu supports bbml2, we can
+     * safely split.
+     */
+    if (num_online_cpus() > 1 || !cpu_supports_bbml2_noabort())
           return 0;
 

      /*
---8<---

Thanks,
Ryan

Quoted 351 lines
Quoted 16 lines
Quoted 8 lines
Quoted 8 lines

Before setup_system_features(), we don't know if all cpus support BBML2 noabort,
and we
couldn't split kernel page table, in case another cpu that doesn't support BBML2
noabort
is running.

Quoted 8 lines
Quoted 8 lines

How could we fix this issue?

1. force pte mapping if realm feature is enabled? Although force_pte_mapping()
return true if is_realm_world() return true, arm64_rsi_init() is called after map_mem(). So is_realm_world() still return false during map_mem(). Thus realm feature relies on rodata=full. If we fix by this solution, we need to add a new cmdline to force pte mapping.

Quoted 3 lines

I don't quite get why is_realm_world() relies on rodata=full. I understand realm needs PTE mapping if BBML2_NOABORT is not supported. But it doesn't mean real relies on rodata=full.

https://lore.kernel.org/all/[email protected]/

This is the discussion why realm relies on rodata=full. The initization of realm coudn't move to before map_mem(), so is_realm_world() is false. As a result, realm
need rodata=full to indicate we need to make pages shared/protected at page granularity.

Quoted 324 lines
Quoted 6 lines

I think we just need to make is_realm_world() work earlier in boot? I think this
has been a known issue for a while. Not sure if there is any plan to fix it though.

Quoted 1 line

2. If we could try to split kernel page table before setup_system_features()?

Quoted 8 lines

Another option would be to initially map by pte then collapse to block mappings
once we have determined that all cpus support BBML2_NOABORT. We originally opted
not to do that because it's a tax on symetric systems. But we could throw in the
towel if it's the least bad solution we can come up with for solving this. I think it might help some of Kevin's use cases too?

May be an option too. When we discussed this there was no usecase for direct mapping collapse. But if we can have multiple usecases, it may be worth it. AFAICT, the ROX execmem cache may need this, which Will or someone else from Google is going to work on.

Checking current cpu BBML2_NOABORT capability before system feature is finalized seems like a fast way to stop bleeding IMHO before we find more elegant long-term solution.

Thanks,
Yang

Quoted 298 lines

Thanks,
Ryan

Quoted 294 lines

Thanks.

Quoted 292 lines

Ryan tested v7 on an AmpereOne system (a VM with 12G RAM) in all 3 possible modes by hacking the BBML2 feature detection code:

     - mode 1: All CPUs support BBML2 so the linear map uses large mappings
     - mode 2: Boot CPU does not support BBML2 so linear map uses pte
mappings
     - mode 3: Boot CPU supports BBML2 but secondaries do not so linear map initially uses large mappings but is then repainted to use pte
mappings

In all cases, mm selftests run and no regressions are observed. In all cases,
ptdump of linear map is as expected. Because there are just some cleanups between v7 and v8, so I kept using Ryan's test result:

Mode 1:
=======
---[ Linear Mapping start ]--- 0xffff000000000000-0xffff000000200000           2M PMD RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED 0xffff000000200000-0xffff000000210000          64K PTE RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000000210000-0xffff000000400000        1984K PTE ro NX SHD AF            UXN    MEM/NORMAL 0xffff000000400000-0xffff000002400000          32M PMD ro NX SHD AF        BLK UXN    MEM/NORMAL 0xffff000002400000-0xffff000002550000        1344K PTE ro NX SHD AF            UXN    MEM/NORMAL 0xffff000002550000-0xffff000002600000         704K PTE RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000002600000-0xffff000004000000          26M PMD RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED 0xffff000004000000-0xffff000040000000         960M PMD RW NX SHD AF CON BLK UXN    MEM/NORMAL-TAGGED 0xffff000040000000-0xffff000140000000           4G PUD RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED 0xffff000140000000-0xffff000142000000          32M PMD RW NX SHD AF CON BLK UXN    MEM/NORMAL-TAGGED 0xffff000142000000-0xffff000142120000        1152K PTE RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000142120000-0xffff000142128000          32K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142128000-0xffff000142159000         196K PTE ro NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142159000-0xffff000142160000          28K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142160000-0xffff000142240000         896K PTE RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000142240000-0xffff00014224e000          56K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff00014224e000-0xffff000142250000           8K PTE ro NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142250000-0xffff000142260000          64K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142260000-0xffff000142280000         128K PTE RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000142280000-0xffff000142288000          32K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142288000-0xffff000142290000          32K PTE ro NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142290000-0xffff0001422a0000          64K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff0001422a0000-0xffff000142465000        1812K PTE ro NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142465000-0xffff000142470000          44K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142470000-0xffff000142600000        1600K PTE RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000142600000-0xffff000144000000          26M PMD RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED 0xffff000144000000-0xffff000180000000         960M PMD RW NX SHD AF CON BLK UXN    MEM/NORMAL-TAGGED 0xffff000180000000-0xffff000181a00000          26M PMD RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED 0xffff000181a00000-0xffff000181b90000        1600K PTE RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000181b90000-0xffff000181b9d000          52K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000181b9d000-0xffff000181c80000         908K PTE ro NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000181c80000-0xffff000181c90000          64K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000181c90000-0xffff000181ca0000          64K PTE RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000181ca0000-0xffff000181dbd000        1140K PTE ro NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000181dbd000-0xffff000181dc0000          12K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000181dc0000-0xffff000181e00000         256K PTE RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000181e00000-0xffff000182000000           2M PMD RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED 0xffff000182000000-0xffff0001c0000000         992M PMD RW NX SHD AF CON BLK UXN    MEM/NORMAL-TAGGED 0xffff0001c0000000-0xffff000300000000           5G PUD RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED 0xffff000300000000-0xffff008000000000         500G PUD 0xffff008000000000-0xffff800000000000      130560G PGD ---[ Linear Mapping end ]---

Mode 3:
=======
---[ Linear Mapping start ]--- 0xffff000000000000-0xffff000000210000        2112K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000000210000-0xffff000000400000        1984K PTE ro NX SHD AF            UXN    MEM/NORMAL 0xffff000000400000-0xffff000002400000          32M PMD ro NX SHD AF        BLK UXN    MEM/NORMAL 0xffff000002400000-0xffff000002550000        1344K PTE ro NX SHD AF            UXN    MEM/NORMAL 0xffff000002550000-0xffff000143a61000     5264452K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000143a61000-0xffff000143c61000           2M PTE ro NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000143c61000-0xffff000181b9a000     1015012K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000181b9a000-0xffff000181d9a000           2M PTE ro NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000181d9a000-0xffff000300000000     6261144K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000300000000-0xffff008000000000         500G PUD 0xffff008000000000-0xffff800000000000      130560G PGD ---[ Linear Mapping end ]---

Performance Testing
===================
* Memory use after boot
Before:
MemTotal:       258988984 kB
MemFree:        254821700 kB

After:
MemTotal:       259505132 kB
MemFree:        255410264 kB

Around 500MB more memory are free to use.  The larger the machine, the more memory saved.

* Memcached
We saw performance degradation when running Memcached benchmark with rodata=full vs rodata=on.  Our profiling pointed to kernel TLB pressure. With this patchset we saw ops/sec is increased by around 3.5%, P99 latency is reduced by around 9.6%.
The gain mainly came from reduced kernel TLB misses.  The kernel TLB MPKI is reduced by 28.5%.

The benchmark data is now on par with rodata=on too.

* Disk encryption (dm-crypt) benchmark
Ran fio benchmark with the below command on a 128G ramdisk (ext4) with disk encryption (by dm-crypt).
fio --directory=/data --random_generator=lfsr --norandommap            \
       --randrepeat 1 --status-interval=999 --rw=write --bs=4k --loops=1  \
       --ioengine=sync --iodepth=1 --numjobs=1 --fsync_on_close=1         \
       --group_reporting --thread --name=iops-test-job --eta-newline=1    \
       --size 100G

The IOPS is increased by 90% - 150% (the variance is high, but the worst number of good case is around 90% more than the best number of bad case). The bandwidth is increased and the avg clat is reduced proportionally.

* Sequential file read
Read 100G file sequentially on XFS (xfs_io read with page cache populated). The bandwidth is increased by 150%.

Additionally Ryan also ran this through a random selection of benchmarks on AmpereOne. None show any regressions, and various benchmarks show statistically
significant improvement. I'm just showing those improvements here:

+----------------------
+----------------------------------------------------------
+-------------------------+
| Benchmark            | Result
Class                                             | Improvement vs 6.17-
rc1 |
+======================+==========================================================+=========================+
| micromm/vmalloc      | full_fit_alloc_test: p:1, h:0, l:500000
(usec)           |              (I) -9.00% |
|                      | kvfree_rcu_1_arg_vmalloc_test: p:1, h:0, l:500000
(usec) |              (I) -6.93% |
|                      | kvfree_rcu_2_arg_vmalloc_test: p:1, h:0, l:500000
(usec) |              (I) -6.77% |
|                      | pcpu_alloc_test: p:1, h:0, l:500000
(usec)               |              (I) -4.63% |
+----------------------
+----------------------------------------------------------
+-------------------------+
| mmtests/hackbench    | process-sockets-30
(seconds)                             |              (I) -2.96% |
+----------------------
+----------------------------------------------------------
+-------------------------+
| mmtests/kernbench    | syst-192
(seconds) |             (I) -12.77% |
+----------------------
+----------------------------------------------------------
+-------------------------+
| pts/perl-benchmark   | Test: Interpreter
(Seconds)                              |              (I) -4.86% |
+----------------------
+----------------------------------------------------------
+-------------------------+
| pts/pgbench          | Scale: 1 Clients: 1 Read Write
(TPS)                     |               (I) 5.07% |
|                      | Scale: 1 Clients: 1 Read Write - Latency
(ms)            |              (I) -4.72% |
|                      | Scale: 100 Clients: 1000 Read Write
(TPS)                |               (I) 2.58% |
|                      | Scale: 100 Clients: 1000 Read Write - Latency
(ms)       |              (I) -2.52% |
+----------------------
+----------------------------------------------------------
+-------------------------+
| pts/sqlite-speedtest | Timed Time - Size 1,000
(Seconds)                        |              (I) -2.68% |
+----------------------
+----------------------------------------------------------
+-------------------------+

Changes since v7 [1]
====================
- Rebased on v6.17-rc6 and Shijie's rodata series (https://
git.kernel.org/pub/ scm/linux/kernel/git/arm64/linux.git/commit/?id=bfbbb0d3215f)
     which has been picked up by Will.
- Patch 1: Fixed pmd_leaf/pud_leaf issue since the code may need to change permission for invalid entries per Jinjiang Tu.
- Patch 1: Removed pageattr_pgd_entry and pageattr_p4d_entry per Ryan.
- Used (-1ULL) instead of -1 per Catalin.
- Added comment about arm64 lazy mmu allow sleeping per Ryan.
- Squashed patch #4 in v7 into patch #3.
- Squashed patch #6 in v7 into patch #4.
- Added patch #5 to fix a arm64 kprobes bug. It guarantees set_memory_rox() is called before vfree(). It can go into separately or with this series together.
- Collected all the R-bs and A-bs.

Changes since v6 [2]
====================
- Patch 1: Minor refactor to implement walk_kernel_page_table_range() in terms
     of walk_kernel_page_table_range_lockless(). Also lead to adding *pmd
argument
     to the lockless variant for consistency (per Catalin).
- Misc function/variable renames to improve clarity and consistency.
- Share same syncrhonization flag between idmap_kpti_install_ng_mappings and wait_linear_map_split_to_ptes, which allows removal of bbml2_ptes[] to
save
     ~20K from kernel image.
- Only take pgtable_split_lock and enter lazy mmu mode once for both splits.
- Only walk the pgtable once for the common "split single page" case.
- Bypass split to contpmd and contpte when spllitting linear map to ptes.

[1] https://lore.kernel.org/linux-arm-kernel/20250829115250.2395585-1-
[email protected]/
[2] https://lore.kernel.org/linux-arm-kernel/20250805081350.3854670-1-
[email protected]/

Dev Jain (1):
         arm64: Enable permission change on arm64 kernel block mappings

Ryan Roberts (1):
         arm64: mm: split linear mapping if BBML2 unsupported on
secondary CPUs

Yang Shi (3):
         arm64: cpufeature: add AmpereOne to BBML2 allow list
         arm64: mm: support large block mapping when rodata=full
         arm64: kprobes: call set_memory_rox() for kprobe page

     arch/arm64/include/asm/cpufeature.h |   2 +
     arch/arm64/include/asm/mmu.h        |   3 +
     arch/arm64/include/asm/pgtable.h    |   5 ++
     arch/arm64/kernel/cpufeature.c      |  12 +++-
     arch/arm64/kernel/probes/kprobes.c  |  12 ++++
     arch/arm64/mm/mmu.c                 | 422 ++++++++++++++++++++++++++++++
++++
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+----
     arch/arm64/mm/pageattr.c            | 123 +++++++++++++++++++++++
+---------
     arch/arm64/mm/proc.S                |  27 ++++++--
     include/linux/pagewalk.h            |   3 +
     mm/pagewalk.c                       |  36 ++++++----
     10 files changed, 581 insertions(+), 64 deletions(-)
author
#44 lore raw
In reply to Jinjiang Tu
Quoted 115 lines · Jinjiang Tu
Quoted 113 lines · Yang Shi
Quoted 13 lines · Ryan Roberts

Thanks for the report!

+ Kevin, who was looking at some adjacent issues and may have some ideas for how
to fix.

Quoted 5 lines · Jinjiang Tu
Quoted 3 lines · Yang Shi

On systems with BBML2_NOABORT support, it causes the linear map to be mapped with large blocks, even when rodata=full, and leads to some nice performance improvements.

Hi,

Hi Jinjiang,

Thanks for reporting the problem.

Quoted 70 lines
Quoted 62 lines

I find this feature is incompatible with realm. The calltrace is as follows:

[    0.000000][    T0] ------------[ cut here ]------------
[    0.000000][    T0] WARNING: CPU: 0 PID: 0 at arch/arm64/mm/pageattr.c:56 pageattr_pmd_entry+0x60/0x78
[    0.000000][    T0] Modules linked in:
[    0.000000][    T0] CPU: 0 PID: 0 Comm: swapper/0 Not tainted 6.6.0 #16 [    0.000000][    T0] Hardware name: linux,dummy-virt (DT)
[    0.000000][    T0] pstate: 800000c5 (Nzcv daIF -PAN -UAO -TCO -DIT -SSBS
BTYPE=--)
[    0.000000][    T0] pc : pageattr_pmd_entry+0x60/0x78
[    0.000000][    T0] lr : walk_pmd_range.isra.0+0x170/0x1f0
[    0.000000][    T0] sp : ffffcb90a0f337d0
[    0.000000][    T0] x29: ffffcb90a0f337d0 x28: 0000000000000000 x27:
ffff0000035e0000
[    0.000000][    T0] x26: ffffcb90a0f338f8 x25: ffff00001fff60d0 x24:
ffff0000035d0000
[    0.000000][    T0] x23: 0400000000000001 x22: 0c00000000000001 x21:
ffff0000035dffff
[    0.000000][    T0] x20: ffffcb909fe3b7f0 x19: ffff0000035e0000 x18:
ffffffffffffffff
[    0.000000][    T0] x17: 7220303030303178 x16: 307e303030306435 x15:
ffffcb90a0f334c8
[    0.000000][    T0] x14: 0000000000000000 x13: 205d305420202020 x12:
5b5d303030303030
[    0.000000][    T0] x11: 00000000ffff7fff x10: 00000000ffff7fff x9 :
ffffcb909f1e27d8
[    0.000000][    T0] x8 : 00000000000bffe8 x7 : c0000000ffff7fff x6 :
0000000000000001
[    0.000000][    T0] x5 : 0000000000000001 x4 : 0078000083400705 x3 :
ffffcb90a0f338f8
[    0.000000][    T0] x2 : 0000000000010000 x1 : ffff0000035d0000 x0 :
ffff00001fff60d0
[    0.000000][    T0] Call trace:
[    0.000000][    T0]  pageattr_pmd_entry+0x60/0x78
[    0.000000][    T0]  walk_pud_range+0x124/0x190
[    0.000000][    T0]  walk_pgd_range+0x158/0x1b0
[    0.000000][    T0] walk_kernel_page_table_range_lockless+0x58/0x98 [    0.000000][    T0]  update_range_prot+0xb8/0x108
[    0.000000][    T0]  __change_memory_common+0x30/0x1a8
[    0.000000][    T0] __set_memory_enc_dec.part.0+0x170/0x260
[    0.000000][    T0]  realm_set_memory_decrypted+0x6c/0xb0
[    0.000000][    T0]  set_memory_decrypted+0x38/0x58
[    0.000000][    T0]  its_alloc_pages_node+0xc4/0x140
[    0.000000][    T0]  its_probe_one+0xbc/0x3c0
[    0.000000][    T0]  its_of_probe.isra.0+0x130/0x220
[    0.000000][    T0]  its_init+0x160/0x2f8
[    0.000000][    T0]  gic_init_bases+0x1fc/0x318
[    0.000000][    T0]  gic_of_init+0x2a0/0x300
[    0.000000][    T0]  of_irq_init+0x238/0x4b8
[    0.000000][    T0]  irqchip_init+0x20/0x50
[    0.000000][    T0]  init_IRQ+0x1c/0x100
[    0.000000][    T0]  start_kernel+0x1ec/0x4f0
[    0.000000][    T0]  __primary_switched+0xbc/0xd0
[    0.000000][    T0] ---[ end trace 0000000000000000 ]---
[    0.000000][    T0] ------------[ cut here ]------------
[    0.000000][    T0] Failed to decrypt memory, 16 pages will be leaked

realm feature relies on rodata=full to dynamically update kernel page table prot.

Quoted 8 lines

In init_IRQ(), realm_set_memory_decrypted() is called to update kernel page table prot.
At this time, secondary cpus aren't booted, BBML2 noabort feature isn't initializated,
and system_supports_bbml2_noabort() still returns false. As a result, split_kernel_leaf_mapping() is skipped, leading to WARN_ON_ONCE((next - addr) != PMD_SIZE)
in pageattr_pmd_entry().

Quoted 4 lines

If no secondary cpus are yet running, then it is technically safe to split because we know all online cpus (i.e. just the boot cpu) supports BBML2_NOABORT. So we could explicitly only disallow splitting during the window between booting secondary cpus and finalizing the system caps. Feels a bit hacky though...

I think we can check whether system feature has been finalized or not. If it has not been finalized yet, we just need to check whether the current cpu (should be just boot cpu) supports BBML2_NOABORT or not. It sounds ok to me.

Quoted 14 lines
Quoted 14 lines

Before setup_system_features(), we don't know if all cpus support BBML2 noabort, and we
couldn't split kernel page table, in case another cpu that doesn't support BBML2 noabort
is running.

How could we fix this issue?

1. force pte mapping if realm feature is enabled? Although force_pte_mapping() return true if is_realm_world() return true, arm64_rsi_init() is called after map_mem(). So is_realm_world() still return false during map_mem(). Thus realm feature relies on rodata=full. If we fix by this solution, we need to add a new cmdline to force pte mapping.

Quoted 7 lines
Quoted 1 line

I don't quite get why is_realm_world() relies on rodata=full. I understand realm needs PTE mapping if BBML2_NOABORT is not supported. But it doesn't mean real relies on rodata=full.

https://lore.kernel.org/all/[email protected]/

This is the discussion why realm relies on rodata=full. The initization of realm coudn't move to before map_mem(), so is_realm_world() is false. As a result, realm need rodata=full to indicate we need to make pages shared/protected at page granularity.

If you read the follow-up discussions, you would seen something different. Will's reply is quite clear about "rodata=full has absolutely nothing to do with realms". See https://lore.kernel.org/all/20250227172254.GB25617@willie-the-truck/

It just happens both requires PTE mapping if BBML2_NOABORT is not supported.

Thanks,
Yang

Quoted 324 lines
Quoted 324 lines
Quoted 6 lines

I think we just need to make is_realm_world() work earlier in boot? I think this has been a known issue for a while. Not sure if there is any plan to fix it though.

Quoted 6 lines
Quoted 1 line

2. If we could try to split kernel page table before setup_system_features()?

Another option would be to initially map by pte then collapse to block mappings once we have determined that all cpus support BBML2_NOABORT. We originally opted not to do that because it's a tax on symetric systems. But we could throw in the towel if it's the least bad solution we can come up with for solving this. I think it might help some of Kevin's use cases too?

May be an option too. When we discussed this there was no usecase for direct mapping collapse. But if we can have multiple usecases, it may be worth it. AFAICT, the ROX execmem cache may need this, which Will or someone else from Google is going to work on.

Checking current cpu BBML2_NOABORT capability before system feature is finalized seems like a fast way to stop bleeding IMHO before we find more elegant long-term solution.

Thanks,
Yang

Quoted 302 lines

Thanks,
Ryan

Quoted 297 lines

Thanks.

Quoted 295 lines

Ryan tested v7 on an AmpereOne system (a VM with 12G RAM) in all 3 possible modes by hacking the BBML2 feature detection code:

    - mode 1: All CPUs support BBML2 so the linear map uses large mappings
    - mode 2: Boot CPU does not support BBML2 so linear map uses pte mappings
    - mode 3: Boot CPU supports BBML2 but secondaries do not so linear map initially uses large mappings but is then repainted to use pte mappings

In all cases, mm selftests run and no regressions are observed. In all cases, ptdump of linear map is as expected. Because there are just some cleanups between v7 and v8, so I kept using Ryan's test result:

Mode 1:
=======
---[ Linear Mapping start ]--- 0xffff000000000000-0xffff000000200000           2M PMD RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED 0xffff000000200000-0xffff000000210000          64K PTE RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000000210000-0xffff000000400000        1984K PTE ro NX SHD AF            UXN    MEM/NORMAL 0xffff000000400000-0xffff000002400000          32M PMD ro NX SHD AF        BLK UXN    MEM/NORMAL 0xffff000002400000-0xffff000002550000        1344K PTE ro NX SHD AF            UXN    MEM/NORMAL 0xffff000002550000-0xffff000002600000         704K PTE RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000002600000-0xffff000004000000          26M PMD RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED 0xffff000004000000-0xffff000040000000         960M PMD RW NX SHD AF CON BLK UXN    MEM/NORMAL-TAGGED 0xffff000040000000-0xffff000140000000           4G PUD RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED 0xffff000140000000-0xffff000142000000          32M PMD RW NX SHD AF CON BLK UXN    MEM/NORMAL-TAGGED 0xffff000142000000-0xffff000142120000        1152K PTE RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000142120000-0xffff000142128000          32K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142128000-0xffff000142159000         196K PTE ro NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142159000-0xffff000142160000          28K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142160000-0xffff000142240000         896K PTE RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000142240000-0xffff00014224e000          56K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff00014224e000-0xffff000142250000           8K PTE ro NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142250000-0xffff000142260000          64K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142260000-0xffff000142280000         128K PTE RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000142280000-0xffff000142288000          32K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142288000-0xffff000142290000          32K PTE ro NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142290000-0xffff0001422a0000          64K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff0001422a0000-0xffff000142465000        1812K PTE ro NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142465000-0xffff000142470000          44K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000142470000-0xffff000142600000        1600K PTE RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000142600000-0xffff000144000000          26M PMD RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED 0xffff000144000000-0xffff000180000000         960M PMD RW NX SHD AF CON BLK UXN    MEM/NORMAL-TAGGED 0xffff000180000000-0xffff000181a00000          26M PMD RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED 0xffff000181a00000-0xffff000181b90000        1600K PTE RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000181b90000-0xffff000181b9d000          52K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000181b9d000-0xffff000181c80000         908K PTE ro NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000181c80000-0xffff000181c90000          64K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000181c90000-0xffff000181ca0000          64K PTE RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000181ca0000-0xffff000181dbd000        1140K PTE ro NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000181dbd000-0xffff000181dc0000          12K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000181dc0000-0xffff000181e00000         256K PTE RW NX SHD AF CON     UXN    MEM/NORMAL-TAGGED 0xffff000181e00000-0xffff000182000000           2M PMD RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED 0xffff000182000000-0xffff0001c0000000         992M PMD RW NX SHD AF CON BLK UXN    MEM/NORMAL-TAGGED 0xffff0001c0000000-0xffff000300000000           5G PUD RW NX SHD AF        BLK UXN    MEM/NORMAL-TAGGED 0xffff000300000000-0xffff008000000000         500G PUD 0xffff008000000000-0xffff800000000000      130560G PGD ---[ Linear Mapping end ]---

Mode 3:
=======
---[ Linear Mapping start ]--- 0xffff000000000000-0xffff000000210000        2112K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000000210000-0xffff000000400000        1984K PTE ro NX SHD AF            UXN    MEM/NORMAL 0xffff000000400000-0xffff000002400000          32M PMD ro NX SHD AF        BLK UXN    MEM/NORMAL 0xffff000002400000-0xffff000002550000        1344K PTE ro NX SHD AF            UXN    MEM/NORMAL 0xffff000002550000-0xffff000143a61000     5264452K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000143a61000-0xffff000143c61000           2M PTE ro NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000143c61000-0xffff000181b9a000     1015012K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000181b9a000-0xffff000181d9a000           2M PTE ro NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000181d9a000-0xffff000300000000     6261144K PTE RW NX SHD AF            UXN    MEM/NORMAL-TAGGED 0xffff000300000000-0xffff008000000000         500G PUD 0xffff008000000000-0xffff800000000000      130560G PGD ---[ Linear Mapping end ]---

Performance Testing
===================
* Memory use after boot
Before:
MemTotal:       258988984 kB
MemFree:        254821700 kB

After:
MemTotal:       259505132 kB
MemFree:        255410264 kB

Around 500MB more memory are free to use.  The larger the machine, the more memory saved.

* Memcached
We saw performance degradation when running Memcached benchmark with rodata=full vs rodata=on.  Our profiling pointed to kernel TLB pressure. With this patchset we saw ops/sec is increased by around 3.5%, P99 latency is reduced by around 9.6%.
The gain mainly came from reduced kernel TLB misses.  The kernel TLB MPKI is reduced by 28.5%.

The benchmark data is now on par with rodata=on too.

* Disk encryption (dm-crypt) benchmark
Ran fio benchmark with the below command on a 128G ramdisk (ext4) with disk encryption (by dm-crypt).
fio --directory=/data --random_generator=lfsr
--norandommap            \
      --randrepeat 1 --status-interval=999 --rw=write --bs=4k --loops=1  \
      --ioengine=sync --iodepth=1 --numjobs=1 --fsync_on_close=1         \
      --group_reporting --thread --name=iops-test-job --eta-newline=1    \
      --size 100G

The IOPS is increased by 90% - 150% (the variance is high, but the worst number of good case is around 90% more than the best number of bad case). The bandwidth is increased and the avg clat is reduced proportionally.

* Sequential file read
Read 100G file sequentially on XFS (xfs_io read with page cache populated). The bandwidth is increased by 150%.

Additionally Ryan also ran this through a random selection of benchmarks on AmpereOne. None show any regressions, and various benchmarks show statistically significant improvement. I'm just showing those improvements here:

+----------------------
+----------------------------------------------------------
+-------------------------+
| Benchmark            | Result
Class                                             | Improvement vs 6.17-rc1 |
+======================+==========================================================+=========================+ 
| micromm/vmalloc      | full_fit_alloc_test: p:1, h:0, l:500000
(usec)           |              (I) -9.00% |
|                      | kvfree_rcu_1_arg_vmalloc_test: p:1, h:0, l:500000
(usec) |              (I) -6.93% |
|                      | kvfree_rcu_2_arg_vmalloc_test: p:1, h:0, l:500000
(usec) |              (I) -6.77% |
|                      | pcpu_alloc_test: p:1, h:0, l:500000
(usec)               |              (I) -4.63% |
+----------------------
+----------------------------------------------------------
+-------------------------+
| mmtests/hackbench    | process-sockets-30
(seconds)                             |              (I) -2.96% |
+----------------------
+----------------------------------------------------------
+-------------------------+
| mmtests/kernbench    | syst-192
(seconds) |             (I) -12.77% |
+----------------------
+----------------------------------------------------------
+-------------------------+
| pts/perl-benchmark   | Test: Interpreter
(Seconds)                              |              (I) -4.86% |
+----------------------
+----------------------------------------------------------
+-------------------------+
| pts/pgbench          | Scale: 1 Clients: 1 Read Write
(TPS)                     |               (I) 5.07% |
|                      | Scale: 1 Clients: 1 Read Write - Latency
(ms)            |              (I) -4.72% |
|                      | Scale: 100 Clients: 1000 Read Write
(TPS)                |               (I) 2.58% |
|                      | Scale: 100 Clients: 1000 Read Write - Latency
(ms)       |              (I) -2.52% |
+----------------------
+----------------------------------------------------------
+-------------------------+
| pts/sqlite-speedtest | Timed Time - Size 1,000
(Seconds)                        |              (I) -2.68% |
+----------------------
+----------------------------------------------------------
+-------------------------+

Changes since v7 [1]
====================
- Rebased on v6.17-rc6 and Shijie's rodata series
(https://git.kernel.org/pub/
scm/linux/kernel/git/arm64/linux.git/commit/?id=bfbbb0d3215f) which has been picked up by Will.
- Patch 1: Fixed pmd_leaf/pud_leaf issue since the code may need to change
    permission for invalid entries per Jinjiang Tu.
- Patch 1: Removed pageattr_pgd_entry and pageattr_p4d_entry per Ryan.
- Used (-1ULL) instead of -1 per Catalin.
- Added comment about arm64 lazy mmu allow sleeping per Ryan.
- Squashed patch #4 in v7 into patch #3.
- Squashed patch #6 in v7 into patch #4.
- Added patch #5 to fix a arm64 kprobes bug. It guarantees set_memory_rox()
    is called before vfree(). It can go into separately or with this series together.
- Collected all the R-bs and A-bs.

Changes since v6 [2]
====================
- Patch 1: Minor refactor to implement walk_kernel_page_table_range() in terms
    of walk_kernel_page_table_range_lockless(). Also lead to adding *pmd argument to the lockless variant for consistency (per Catalin).
- Misc function/variable renames to improve clarity and consistency.
- Share same syncrhonization flag between idmap_kpti_install_ng_mappings and
    wait_linear_map_split_to_ptes, which allows removal of bbml2_ptes[] to save ~20K from kernel image.
- Only take pgtable_split_lock and enter lazy mmu mode once for
both splits.
- Only walk the pgtable once for the common "split single page" case.
- Bypass split to contpmd and contpte when spllitting linear map
to ptes.

[1] https://lore.kernel.org/linux-arm-kernel/20250829115250.2395585-1-
[email protected]/
[2] https://lore.kernel.org/linux-arm-kernel/20250805081350.3854670-1-
[email protected]/

Dev Jain (1):
        arm64: Enable permission change on arm64 kernel block mappings

Ryan Roberts (1):
        arm64: mm: split linear mapping if BBML2 unsupported on secondary CPUs

Yang Shi (3):
        arm64: cpufeature: add AmpereOne to BBML2 allow list
        arm64: mm: support large block mapping when rodata=full
        arm64: kprobes: call set_memory_rox() for kprobe page

   arch/arm64/include/asm/cpufeature.h |   2 +
   arch/arm64/include/asm/mmu.h        |   3 +
   arch/arm64/include/asm/pgtable.h    |   5 ++
   arch/arm64/kernel/cpufeature.c      |  12 +++-
   arch/arm64/kernel/probes/kprobes.c  |  12 ++++
   arch/arm64/mm/mmu.c                 | 422
++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----

   arch/arm64/mm/pageattr.c            | 123
++++++++++++++++++++++++---------
   arch/arm64/mm/proc.S                |  27 ++++++-- include/linux/pagewalk.h            |   3 + mm/pagewalk.c                       |  36 ++++++----

   10 files changed, 581 insertions(+), 64 deletions(-)
#45 lore raw
In reply to Yang Shi
Quoted 8 lines · Yang Shi
Quoted 5 lines · Ryan Roberts

Thanks for the report!

+ Kevin, who was looking at some adjacent issues and may have some ideas for how
to fix.

Indeed, specifically to protect page table pages (PTPs) by mapping them with a privileged pkey. pagetable_alloc() is called on several occasions before all secondaries are up (including from init_IRQ() in fact), but we cannot call set_memory_pkey() at that point for the same reason that Jinjiang pointed out.

The approach I went for is to allocate a whole block on boot and defer the call to set_memory_pkey() until it's safe to do so [1]. It's not a particularly nice solution as this restricts the number of PTPs we can allocate in that window, especially if more of them get allocated when set_memory_decrypted() splits the linear map.

[1]
https://lore.kernel.org/linux-hardening/[email protected]/

Quoted 101 lines
Quoted 10 lines · Jinjiang Tu
Quoted 7 lines · Yang Shi
Quoted 5 lines

On systems with BBML2_NOABORT support, it causes the linear map to be mapped
with large blocks, even when rodata=full, and leads to some nice performance
improvements.

Hi,

Hi Jinjiang,

Thanks for reporting the problem.

Quoted 77 lines
Quoted 67 lines

I find this feature is incompatible with realm. The calltrace is as follows:

[    0.000000][    T0] ------------[ cut here ]------------ [    0.000000][    T0] WARNING: CPU: 0 PID: 0 at arch/arm64/mm/pageattr.c:56
pageattr_pmd_entry+0x60/0x78
[    0.000000][    T0] Modules linked in:
[    0.000000][    T0] CPU: 0 PID: 0 Comm: swapper/0 Not tainted 6.6.0 #16
[    0.000000][    T0] Hardware name: linux,dummy-virt (DT) [    0.000000][    T0] pstate: 800000c5 (Nzcv daIF -PAN -UAO -TCO -DIT -SSBS
BTYPE=--)
[    0.000000][    T0] pc : pageattr_pmd_entry+0x60/0x78
[    0.000000][    T0] lr : walk_pmd_range.isra.0+0x170/0x1f0 [    0.000000][    T0] sp : ffffcb90a0f337d0
[    0.000000][    T0] x29: ffffcb90a0f337d0 x28: 0000000000000000 x27:
ffff0000035e0000
[    0.000000][    T0] x26: ffffcb90a0f338f8 x25: ffff00001fff60d0 x24:
ffff0000035d0000
[    0.000000][    T0] x23: 0400000000000001 x22: 0c00000000000001 x21:
ffff0000035dffff
[    0.000000][    T0] x20: ffffcb909fe3b7f0 x19: ffff0000035e0000 x18:
ffffffffffffffff
[    0.000000][    T0] x17: 7220303030303178 x16: 307e303030306435 x15:
ffffcb90a0f334c8
[    0.000000][    T0] x14: 0000000000000000 x13: 205d305420202020 x12:
5b5d303030303030
[    0.000000][    T0] x11: 00000000ffff7fff x10: 00000000ffff7fff x9 :
ffffcb909f1e27d8
[    0.000000][    T0] x8 : 00000000000bffe8 x7 : c0000000ffff7fff x6 :
0000000000000001
[    0.000000][    T0] x5 : 0000000000000001 x4 : 0078000083400705 x3 :
ffffcb90a0f338f8
[    0.000000][    T0] x2 : 0000000000010000 x1 : ffff0000035d0000 x0 :
ffff00001fff60d0
[    0.000000][    T0] Call trace:
[    0.000000][    T0]  pageattr_pmd_entry+0x60/0x78
[    0.000000][    T0]  walk_pud_range+0x124/0x190
[    0.000000][    T0]  walk_pgd_range+0x158/0x1b0
[    0.000000][    T0]  walk_kernel_page_table_range_lockless+0x58/0x98 [    0.000000][    T0]  update_range_prot+0xb8/0x108
[    0.000000][    T0]  __change_memory_common+0x30/0x1a8
[    0.000000][    T0]  __set_memory_enc_dec.part.0+0x170/0x260 [    0.000000][    T0]  realm_set_memory_decrypted+0x6c/0xb0 [    0.000000][    T0]  set_memory_decrypted+0x38/0x58
[    0.000000][    T0]  its_alloc_pages_node+0xc4/0x140
[    0.000000][    T0]  its_probe_one+0xbc/0x3c0
[    0.000000][    T0]  its_of_probe.isra.0+0x130/0x220
[    0.000000][    T0]  its_init+0x160/0x2f8
[    0.000000][    T0]  gic_init_bases+0x1fc/0x318
[    0.000000][    T0]  gic_of_init+0x2a0/0x300
[    0.000000][    T0]  of_irq_init+0x238/0x4b8
[    0.000000][    T0]  irqchip_init+0x20/0x50
[    0.000000][    T0]  init_IRQ+0x1c/0x100
[    0.000000][    T0]  start_kernel+0x1ec/0x4f0
[    0.000000][    T0]  __primary_switched+0xbc/0xd0
[    0.000000][    T0] ---[ end trace 0000000000000000 ]--- [    0.000000][    T0] ------------[ cut here ]------------ [    0.000000][    T0] Failed to decrypt memory, 16 pages will be leaked

realm feature relies on rodata=full to dynamically update kernel page table prot.

Quoted 10 lines

In init_IRQ(), realm_set_memory_decrypted() is called to update kernel page
table prot.
At this time, secondary cpus aren't booted, BBML2 noabort feature isn't initializated,
and system_supports_bbml2_noabort() still returns false. As a result, split_kernel_leaf_mapping() is skipped, leading to WARN_ON_ONCE((next - addr) !=
PMD_SIZE)
in pageattr_pmd_entry().

Quoted 8 lines

If no secondary cpus are yet running, then it is technically safe to split
because we know all online cpus (i.e. just the boot cpu) supports BBML2_NOABORT.
So we could explicitly only disallow splitting during the window between booting
secondary cpus and finalizing the system caps. Feels a bit hacky though...

Quoted 4 lines

I think we can check whether system feature has been finalized or not. If it has not been finalized yet, we just need to check whether the current cpu (should be just boot cpu) supports BBML2_NOABORT or not. It sounds ok to me.

That assumes that no secondary has booted yet, otherwise we cannot safely split live mappings without knowing that all CPUs support BBML2-noabort. It might work for this particular case, but it is fragile. It wouldn't help for the page table protection case, as PTPs get allocated while secondaries are booting up (e.g. stack allocation when forking kthreadd).

Quoted 44 lines
Quoted 12 lines
Quoted 12 lines

Before setup_system_features(), we don't know if all cpus support BBML2 noabort,
and we
couldn't split kernel page table, in case another cpu that doesn't support BBML2
noabort
is running.

How could we fix this issue?

Quoted 9 lines
Quoted 9 lines

1. force pte mapping if realm feature is enabled? Although force_pte_mapping()
return true if is_realm_world() return true, arm64_rsi_init() is called after
map_mem(). So is_realm_world() still return false during map_mem(). Thus
realm feature relies on rodata=full. If we fix by this solution, we need
to add a new cmdline to force pte mapping.

I don't quite get why is_realm_world() relies on rodata=full. I understand realm needs PTE mapping if BBML2_NOABORT is not supported. But it doesn't mean real relies on rodata=full.

Quoted 8 lines

I think we just need to make is_realm_world() work earlier in boot? I think this
has been a known issue for a while. Not sure if there is any plan to fix it
though.

Quoted 2 lines

2. If we could try to split kernel page table before setup_system_features()?

Quoted 9 lines

Another option would be to initially map by pte then collapse to block mappings
once we have determined that all cpus support BBML2_NOABORT. We originally opted
not to do that because it's a tax on symetric systems. But we could throw in the
towel if it's the least bad solution we can come up with for solving this. I
think it might help some of Kevin's use cases too?

Quoted 4 lines

May be an option too. When we discussed this there was no usecase for direct mapping collapse. But if we can have multiple usecases, it may be worth it. AFAICT, the ROX execmem cache may need this, which Will or someone else from Google is going to work on.

Not sure about the execmem cache (do we call execmem_alloc() before secondaries are up?), but I think that would indeed solve the issue for the page table protection use-case. Besides in terms of complexity it's probably not much worse than what we currently have, i.e. basically the reverse (splitting the linear map if some CPU doesn't have BBML2-noabort). Penalising symmetric systems is not great, though.

- Kevin

Quoted 9 lines

Checking current cpu BBML2_NOABORT capability before system feature is finalized seems like a fast way to stop bleeding IMHO before we find more elegant long-term solution.

Thanks,
Yang

Quoted 1 line

[...]

#46 lore raw
Quoted 131 lines · Kevin Brodsky
Quoted 8 lines · Yang Shi
Quoted 5 lines · Ryan Roberts

Thanks for the report!

+ Kevin, who was looking at some adjacent issues and may have some ideas for how
to fix.

Indeed, specifically to protect page table pages (PTPs) by mapping them with a privileged pkey. pagetable_alloc() is called on several occasions before all secondaries are up (including from init_IRQ() in fact), but we cannot call set_memory_pkey() at that point for the same reason that Jinjiang pointed out.

The approach I went for is to allocate a whole block on boot and defer the call to set_memory_pkey() until it's safe to do so [1]. It's not a particularly nice solution as this restricts the number of PTPs we can allocate in that window, especially if more of them get allocated when set_memory_decrypted() splits the linear map.

[1]
https://lore.kernel.org/linux-hardening/[email protected]/

Quoted 101 lines
Quoted 10 lines · Jinjiang Tu
Quoted 7 lines · Yang Shi
Quoted 5 lines

On systems with BBML2_NOABORT support, it causes the linear map to be mapped
with large blocks, even when rodata=full, and leads to some nice performance
improvements.

Hi,

Hi Jinjiang,

Thanks for reporting the problem.

Quoted 77 lines
Quoted 67 lines

I find this feature is incompatible with realm. The calltrace is as follows:

[    0.000000][    T0] ------------[ cut here ]------------ [    0.000000][    T0] WARNING: CPU: 0 PID: 0 at arch/arm64/mm/pageattr.c:56
pageattr_pmd_entry+0x60/0x78
[    0.000000][    T0] Modules linked in:
[    0.000000][    T0] CPU: 0 PID: 0 Comm: swapper/0 Not tainted 6.6.0 #16
[    0.000000][    T0] Hardware name: linux,dummy-virt (DT) [    0.000000][    T0] pstate: 800000c5 (Nzcv daIF -PAN -UAO -TCO -DIT -SSBS
BTYPE=--)
[    0.000000][    T0] pc : pageattr_pmd_entry+0x60/0x78
[    0.000000][    T0] lr : walk_pmd_range.isra.0+0x170/0x1f0 [    0.000000][    T0] sp : ffffcb90a0f337d0
[    0.000000][    T0] x29: ffffcb90a0f337d0 x28: 0000000000000000 x27:
ffff0000035e0000
[    0.000000][    T0] x26: ffffcb90a0f338f8 x25: ffff00001fff60d0 x24:
ffff0000035d0000
[    0.000000][    T0] x23: 0400000000000001 x22: 0c00000000000001 x21:
ffff0000035dffff
[    0.000000][    T0] x20: ffffcb909fe3b7f0 x19: ffff0000035e0000 x18:
ffffffffffffffff
[    0.000000][    T0] x17: 7220303030303178 x16: 307e303030306435 x15:
ffffcb90a0f334c8
[    0.000000][    T0] x14: 0000000000000000 x13: 205d305420202020 x12:
5b5d303030303030
[    0.000000][    T0] x11: 00000000ffff7fff x10: 00000000ffff7fff x9 :
ffffcb909f1e27d8
[    0.000000][    T0] x8 : 00000000000bffe8 x7 : c0000000ffff7fff x6 :
0000000000000001
[    0.000000][    T0] x5 : 0000000000000001 x4 : 0078000083400705 x3 :
ffffcb90a0f338f8
[    0.000000][    T0] x2 : 0000000000010000 x1 : ffff0000035d0000 x0 :
ffff00001fff60d0
[    0.000000][    T0] Call trace:
[    0.000000][    T0]  pageattr_pmd_entry+0x60/0x78
[    0.000000][    T0]  walk_pud_range+0x124/0x190
[    0.000000][    T0]  walk_pgd_range+0x158/0x1b0
[    0.000000][    T0]  walk_kernel_page_table_range_lockless+0x58/0x98 [    0.000000][    T0]  update_range_prot+0xb8/0x108
[    0.000000][    T0]  __change_memory_common+0x30/0x1a8
[    0.000000][    T0]  __set_memory_enc_dec.part.0+0x170/0x260 [    0.000000][    T0]  realm_set_memory_decrypted+0x6c/0xb0 [    0.000000][    T0]  set_memory_decrypted+0x38/0x58
[    0.000000][    T0]  its_alloc_pages_node+0xc4/0x140
[    0.000000][    T0]  its_probe_one+0xbc/0x3c0
[    0.000000][    T0]  its_of_probe.isra.0+0x130/0x220
[    0.000000][    T0]  its_init+0x160/0x2f8
[    0.000000][    T0]  gic_init_bases+0x1fc/0x318
[    0.000000][    T0]  gic_of_init+0x2a0/0x300
[    0.000000][    T0]  of_irq_init+0x238/0x4b8
[    0.000000][    T0]  irqchip_init+0x20/0x50
[    0.000000][    T0]  init_IRQ+0x1c/0x100
[    0.000000][    T0]  start_kernel+0x1ec/0x4f0
[    0.000000][    T0]  __primary_switched+0xbc/0xd0
[    0.000000][    T0] ---[ end trace 0000000000000000 ]--- [    0.000000][    T0] ------------[ cut here ]------------ [    0.000000][    T0] Failed to decrypt memory, 16 pages will be leaked

realm feature relies on rodata=full to dynamically update kernel page table prot.

Quoted 10 lines

In init_IRQ(), realm_set_memory_decrypted() is called to update kernel page
table prot.
At this time, secondary cpus aren't booted, BBML2 noabort feature isn't initializated,
and system_supports_bbml2_noabort() still returns false. As a result, split_kernel_leaf_mapping() is skipped, leading to WARN_ON_ONCE((next - addr) !=
PMD_SIZE)
in pageattr_pmd_entry().

Quoted 8 lines

If no secondary cpus are yet running, then it is technically safe to split
because we know all online cpus (i.e. just the boot cpu) supports BBML2_NOABORT.
So we could explicitly only disallow splitting during the window between booting
secondary cpus and finalizing the system caps. Feels a bit hacky though...

Quoted 4 lines

I think we can check whether system feature has been finalized or not. If it has not been finalized yet, we just need to check whether the current cpu (should be just boot cpu) supports BBML2_NOABORT or not. It sounds ok to me.

Quoted 54 lines

That assumes that no secondary has booted yet, otherwise we cannot safely split live mappings without knowing that all CPUs support BBML2-noabort. It might work for this particular case, but it is fragile. It wouldn't help for the page table protection case, as PTPs get allocated while secondaries are booting up (e.g. stack allocation when forking kthreadd).

Quoted 47 lines
Quoted 12 lines
Quoted 12 lines

Before setup_system_features(), we don't know if all cpus support BBML2 noabort,
and we
couldn't split kernel page table, in case another cpu that doesn't support BBML2
noabort
is running.

How could we fix this issue?

Quoted 9 lines
Quoted 9 lines

1. force pte mapping if realm feature is enabled? Although force_pte_mapping()
return true if is_realm_world() return true, arm64_rsi_init() is called after
map_mem(). So is_realm_world() still return false during map_mem(). Thus
realm feature relies on rodata=full. If we fix by this solution, we need
to add a new cmdline to force pte mapping.

I don't quite get why is_realm_world() relies on rodata=full. I understand realm needs PTE mapping if BBML2_NOABORT is not supported. But it doesn't mean real relies on rodata=full.

Quoted 8 lines

I think we just need to make is_realm_world() work earlier in boot? I think this
has been a known issue for a while. Not sure if there is any plan to fix it
though.

Quoted 2 lines

2. If we could try to split kernel page table before setup_system_features()?

Quoted 9 lines

Another option would be to initially map by pte then collapse to block mappings
once we have determined that all cpus support BBML2_NOABORT. We originally opted
not to do that because it's a tax on symetric systems. But we could throw in the
towel if it's the least bad solution we can come up with for solving this. I
think it might help some of Kevin's use cases too?

May be an option too. When we discussed this there was no usecase for direct mapping collapse. But if we can have multiple usecases, it may be worth it.

I could imagine that if user space creates and destroys lots of secretmem areas, then it will completely split the linear map to ptes and that will never recover currently. So I think in the long term, having the ability to collapse would be useful. I just don't particularly like forcing symetric systems to map by pte initially (which is slow) only to collapse later (which will cost even more time). But it does feel inherrently more robust.

Quoted 21 lines
Quoted 2 lines

AFAICT, the ROX execmem cache may need this, which Will or someone else from Google is going to work on.

Not sure about the execmem cache (do we call execmem_alloc() before secondaries are up?), but I think that would indeed solve the issue for the page table protection use-case. Besides in terms of complexity it's probably not much worse than what we currently have, i.e. basically the reverse (splitting the linear map if some CPU doesn't have BBML2-noabort). Penalising symmetric systems is not great, though.

- Kevin

Quoted 9 lines

Checking current cpu BBML2_NOABORT capability before system feature is finalized seems like a fast way to stop bleeding IMHO before we find more elegant long-term solution.

Thanks,
Yang

Quoted 1 line

[...]

#47 lore raw
Quoted 12 lines · Ryan Roberts
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

Another option would be to initially map by pte then collapse to block mappings
once we have determined that all cpus support BBML2_NOABORT. We originally opted
not to do that because it's a tax on symetric systems. But we could throw in the
towel if it's the least bad solution we can come up with for solving this. I
think it might help some of Kevin's use cases too?

Quoted 3 lines
Quoted 3 lines

May be an option too. When we discussed this there was no usecase for direct mapping collapse. But if we can have multiple usecases, it may be worth it.

Quoted 6 lines

I could imagine that if user space creates and destroys lots of secretmem areas, then it will completely split the linear map to ptes and that will never recover currently. So I think in the long term, having the ability to collapse would be useful. I just don't particularly like forcing symetric systems to map by pte initially (which is slow) only to collapse later (which will cost even more time). But it does feel inherrently more robust.

Now that you spell it out, I'm realising this would actually make things pretty complicated for protected page tables. In that series, page tables for the linear map are allocated by a separate memblock-based allocator [1], tracking the allocated ranges to set their pkey later. There's a strong assumption that these page tables are never freed.

If we initially PTE-mapped the linear map and then later collapsed it, that assumption clearly wouldn't hold. It could be handled by poking holes in the tracked ranges, but it gets ugly and increases fragmentation.

On the whole I'm not sure there's a sufficiently good argument to always have the linear map PTE-mapped initially.

- Kevin

[1]
https://lore.kernel.org/linux-hardening/[email protected]/

#48 lore raw
Quoted 20 lines · Kevin Brodsky
Quoted 12 lines · Ryan Roberts
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

Another option would be to initially map by pte then collapse to block mappings
once we have determined that all cpus support BBML2_NOABORT. We originally opted
not to do that because it's a tax on symetric systems. But we could throw in the
towel if it's the least bad solution we can come up with for solving this. I
think it might help some of Kevin's use cases too?

Quoted 3 lines
Quoted 3 lines

May be an option too. When we discussed this there was no usecase for direct mapping collapse. But if we can have multiple usecases, it may be worth it.

Quoted 6 lines

I could imagine that if user space creates and destroys lots of secretmem areas, then it will completely split the linear map to ptes and that will never recover currently. So I think in the long term, having the ability to collapse would be useful. I just don't particularly like forcing symetric systems to map by pte initially (which is slow) only to collapse later (which will cost even more time). But it does feel inherrently more robust.

Quoted 8 lines

Now that you spell it out, I'm realising this would actually make things pretty complicated for protected page tables. In that series, page tables for the linear map are allocated by a separate memblock-based allocator [1], tracking the allocated ranges to set their pkey later. There's a strong assumption that these page tables are never freed.

If we initially PTE-mapped the linear map and then later collapsed it, that assumption clearly wouldn't hold.

Sorry I don't understand why the assumptions change? All I'm proposing is walkng the linear map to find compatible PTEs and collapsing them into the biggest possible blocks. The pages aren't being freed, they are just being mapped differently (which can be done live for BBML2_NOABORT). PTEs with different pkeys would be considered incompatible, so we would end up with a boundary in the leaf mappings at that point.

Quoted 2 lines

It could be handled by poking
holes in the tracked ranges, but it gets ugly and increases fragmentation.

You'd still want page tables to be allocated from contiguous physical (and virtual) memory so that the boundaries where pkeys change are minimized.

I guess I've misunderstood something...

Quoted 9 lines

On the whole I'm not sure there's a sufficiently good argument to always have the linear map PTE-mapped initially.

- Kevin

[1]
https://lore.kernel.org/linux-hardening/[email protected]/

#49 lore raw
Quoted 26 lines · Ryan Roberts
Quoted 25 lines · Kevin Brodsky
Quoted 12 lines · Ryan Roberts
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

Another option would be to initially map by pte then collapse to block mappings
once we have determined that all cpus support BBML2_NOABORT. We originally opted
not to do that because it's a tax on symetric systems. But we could throw in the
towel if it's the least bad solution we can come up with for solving this. I
think it might help some of Kevin's use cases too?

Quoted 3 lines
Quoted 3 lines

May be an option too. When we discussed this there was no usecase for direct mapping collapse. But if we can have multiple usecases, it may be worth it.

Quoted 6 lines

I could imagine that if user space creates and destroys lots of secretmem areas, then it will completely split the linear map to ptes and that will never recover currently. So I think in the long term, having the ability to collapse would be useful. I just don't particularly like forcing symetric systems to map by pte initially (which is slow) only to collapse later (which will cost even more time). But it does feel inherrently more robust.

Now that you spell it out, I'm realising this would actually make things pretty complicated for protected page tables. In that series, page tables for the linear map are allocated by a separate memblock-based allocator [1], tracking the allocated ranges to set their pkey later. There's a strong assumption that these page tables are never freed.

Quoted 8 lines
Quoted 2 lines

If we initially PTE-mapped the linear map and then later collapsed it, that assumption clearly wouldn't hold.

Sorry I don't understand why the assumptions change? All I'm proposing is walkng the linear map to find compatible PTEs and collapsing them into the biggest possible blocks. The pages aren't being freed, they are just being mapped differently (which can be done live for BBML2_NOABORT). PTEs with different pkeys would be considered incompatible, so we would end up with a boundary in the leaf mappings at that point.

I'm not sure I'm following, if all entries in a PTE page are compatible, then surely we just convert the parent PMD entry to become a leaf and then free the PTE page? And same idea one level above.

Quoted 5 lines
Quoted 3 lines

It could be handled by poking
holes in the tracked ranges, but it gets ugly and increases fragmentation.

You'd still want page tables to be allocated from contiguous physical (and virtual) memory so that the boundaries where pkeys change are minimized.

Yes that's for sure, that's why I'm concerned with individual pages being freed in a middle of a block.

Quoted 1 line

I guess I've misunderstood something...

I might have too :/

- Kevin

#50 lore raw
Quoted 36 lines · Kevin Brodsky
Quoted 26 lines · Ryan Roberts
Quoted 25 lines · Kevin Brodsky
Quoted 12 lines · Ryan Roberts
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

Another option would be to initially map by pte then collapse to block mappings
once we have determined that all cpus support BBML2_NOABORT. We originally opted
not to do that because it's a tax on symetric systems. But we could throw in the
towel if it's the least bad solution we can come up with for solving this. I
think it might help some of Kevin's use cases too?

Quoted 3 lines
Quoted 3 lines

May be an option too. When we discussed this there was no usecase for direct mapping collapse. But if we can have multiple usecases, it may be worth it.

Quoted 6 lines

I could imagine that if user space creates and destroys lots of secretmem areas, then it will completely split the linear map to ptes and that will never recover currently. So I think in the long term, having the ability to collapse would be useful. I just don't particularly like forcing symetric systems to map by pte initially (which is slow) only to collapse later (which will cost even more time). But it does feel inherrently more robust.

Now that you spell it out, I'm realising this would actually make things pretty complicated for protected page tables. In that series, page tables for the linear map are allocated by a separate memblock-based allocator [1], tracking the allocated ranges to set their pkey later. There's a strong assumption that these page tables are never freed.

Quoted 8 lines
Quoted 2 lines

If we initially PTE-mapped the linear map and then later collapsed it, that assumption clearly wouldn't hold.

Sorry I don't understand why the assumptions change? All I'm proposing is walkng the linear map to find compatible PTEs and collapsing them into the biggest possible blocks. The pages aren't being freed, they are just being mapped differently (which can be done live for BBML2_NOABORT). PTEs with different pkeys would be considered incompatible, so we would end up with a boundary in the leaf mappings at that point.

Quoted 3 lines

I'm not sure I'm following, if all entries in a PTE page are compatible, then surely we just convert the parent PMD entry to become a leaf and then free the PTE page? And same idea one level above.

Ahh - good point! That totally passed me by before. But I'm not sure it's the end of the world...

We would end up with about 0.2% (4K/2M if I've done my maths correctly?) of the linear map sub-optimally mapped. Personally I don't think that would be the end of the world.

Quoted 15 lines
Quoted 6 lines
Quoted 4 lines

It could be handled by poking
holes in the tracked ranges, but it gets ugly and increases fragmentation.

You'd still want page tables to be allocated from contiguous physical (and virtual) memory so that the boundaries where pkeys change are minimized.

Yes that's for sure, that's why I'm concerned with individual pages being freed in a middle of a block.

Quoted 1 line

I guess I've misunderstood something...

I might have too :/

- Kevin