From 443e7e6b8d4dc183a86da81d30ac70a7d5ce08cd Mon Sep 17 00:00:00 2001 From: Alyx-ML <256263590+Alyx-ML@users.noreply.github.com> Date: Sun, 22 Nov 2026 01:10:46 +0000 Subject: [PATCH 1/3] splice: look up a user4k quarter before allocating its references subpage_pipe_buf_retain() runs for every anonymous page spliced into a pipe with nosteal_pipe_buf_ops (vmsplice). It allocated the reference array first and only then looked up the page's first 4K quarter. On a native anonymous page, and on every page while user4k is off, that lookup finds no owner, so the array was freed again at once. The splice paid a kmalloc and a kfree for nothing, and a failed allocation returned -ENOMEM for a page that never needed it. Look up the first quarter before allocating, and return early when it has no owner. Release that one reference if the allocation then fails. Pages with owners get exactly the same references as before. Verified with a host harness built from the real old and new function text, under ASan and UBSan, over 16,896 cases (offsets and lengths in 512-byte steps, all 16 owner patterns of the four quarters, allocation success and failure): return codes, fragment lists and native page puts match in every case, no slot reference or allocation leaks, 4,224 calls no longer allocate, and the 4,224 -ENOMEM results on pages without owners are gone. The kernel object was not built here. Signed-off-by: Alyx-ML <256263590+Alyx-ML@users.noreply.github.com> --- fs/splice.c | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/fs/splice.c b/fs/splice.c index 4b707ce00e86d..9ec2998b94ab7 100644 --- a/fs/splice.c +++ b/fs/splice.c @@ -219,22 +219,35 @@ static const struct pipe_buf_operations subpage_pipe_buf_ops = { static int subpage_pipe_buf_retain(struct pipe_buffer *buf) { struct subpage_pipe_refs *refs; + struct mm_subpage *first; unsigned int pos, count = 0; if (!buf->len || buf->ops != &nosteal_pipe_buf_ops || !folio_test_anon(page_folio(buf->page))) return 0; + /* + * Look up the first quarter before allocating. It has no owner on a + * native anonymous page, and none at all while user4k is off, so the + * common splice keeps its native reference without an allocation. + */ + pos = round_down(buf->offset, MM_SUBPAGE_SIZE); + first = mm_subpage_get_from_phys(page_to_phys(buf->page) + pos); + if (!first) + return 0; refs = kmalloc(struct_size(refs, fragments, PAGE_SIZE / MM_SUBPAGE_SIZE), GFP_KERNEL); - if (!refs) + if (!refs) { + mm_subpage_put(first); return -ENOMEM; - for (pos = round_down(buf->offset, MM_SUBPAGE_SIZE); - pos < buf->offset + buf->len; pos += MM_SUBPAGE_SIZE) { - struct mm_subpage *slot = mm_subpage_get_from_phys(page_to_phys(buf->page) + pos); + } + for (; pos < buf->offset + buf->len; pos += MM_SUBPAGE_SIZE) { + struct mm_subpage *slot = first; + if (count) + slot = mm_subpage_get_from_phys(page_to_phys(buf->page) + pos); if (!slot) { release_user_fragments(refs->fragments, count, false); kfree(refs); - return count ? -EFAULT : 0; + return -EFAULT; } refs->fragments[count++] = (struct user_page_fragment) { .folio = page_folio(buf->page), From 35c319733de134bf98ea48e7c5da1aa97d726252 Mon Sep 17 00:00:00 2001 From: Alyx-ML <256263590+Alyx-ML@users.noreply.github.com> Date: Sun, 22 Nov 2026 01:10:47 +0000 Subject: [PATCH 2/3] mm/ksm: keep address spaces with another user granule out of KSM KSM still assumes that a user page is one whole native page. get_mergeable_page() already refuses a 4K-on-16K address space, and madvise(MADV_MERGEABLE) fails there with EOPNOTSUPP. Three other ways in were left open: - prctl(PR_SET_MEMORY_MERGE) calls ksm_enable_merge_any(), which put the mm on the KSM list and marked its VMAs VM_MERGEABLE. - MMF_VM_MERGE_ANY is in MMF_INIT_LEGACY_MASK, so it survives exec. ksm_execve() entered the new mm into KSM even when exec had picked a 4K granule for it. - With the mm on the list, ksmd walked its VMAs and passed its pages to the merge code, which write-protects and replaces native PTEs. Close all three, using the same mm_page_size(mm) != PAGE_SIZE test as the madvise gate: - ksm_enable_merge_any() returns -EOPNOTSUPP for such an mm, so PR_SET_MEMORY_MERGE fails the same way MADV_MERGEABLE does. - ksm_execve() clears the inherited MMF_VM_MERGE_ANY and does not enter KSM. The exec still succeeds. arch_mm_init_exec() sets the granule in __mm_alloc() before mm_init() copies the flags, and create_init_stack_vma() calls ksm_execve() later, so the granule is known by then. This covers the second mm that bprm_set_page_shift() builds as well. PR_GET_MEMORY_MERGE then reports 0, and a native program that this process starts later does not inherit the setting. - ksm_vma_flags() and __ksm_add_vma() never set VM_MERGEABLE for such an mm. - scan_get_next_rmap_item() treats such an mm as having no mergeable VMAs. It then frees any rmap items and drops the mm from the KSM list, the same path an mm with no VM_MERGEABLE VMAs takes. Without CONFIG_MM_SUBPAGE, mm_page_size() is the constant PAGE_SIZE and every new test folds away. With the option built in but user4k off, every mm is native and the tests are always false. user4k is now on by default (commit 36e4785abf63 ("arm64: user4k: turn 4K userspace on by default, keep user4k=off")), so any process can pick a 4K granule with the page-size prctl and reach these paths. Verified on a host harness that compiles the real __ksm_should_add_vma, __ksm_add_vma, ksm_vma_flags, ksm_add_vmas, ksm_enable_merge_any and ksm_execve from this tree against small stubs. All 13 checks pass. The same harness fails 7 of them on the code before this change. A build with the option off passes the native checks. The scanner change was checked by reading. No kernel was built or booted for this change, and there is no user4k selftest for KSM yet. Signed-off-by: Alyx-ML <256263590+Alyx-ML@users.noreply.github.com> --- include/linux/ksm.h | 8 ++++++++ mm/ksm.c | 15 ++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/include/linux/ksm.h b/include/linux/ksm.h index d39d0d5483a23..662cd12dd91d2 100644 --- a/include/linux/ksm.h +++ b/include/linux/ksm.h @@ -68,6 +68,14 @@ static inline void ksm_fork(struct mm_struct *mm, struct mm_struct *oldmm) static inline int ksm_execve(struct mm_struct *mm) { + /* + * KSM merges native pages only. An exec that selected another user + * granule drops the inherited merge-any setting rather than enter KSM. + */ + if (mm_page_size(mm) != PAGE_SIZE) { + mm_flags_clear(MMF_VM_MERGE_ANY, mm); + return 0; + } if (mm_flags_test(MMF_VM_MERGE_ANY, mm)) return __ksm_enter(mm); diff --git a/mm/ksm.c b/mm/ksm.c index 4b4cba6614b61..edbf6d06b527d 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -2688,6 +2688,12 @@ static struct ksm_rmap_item *scan_get_next_rmap_item(struct page **page) mmap_read_lock(mm); if (ksm_test_exit(mm)) goto no_vmas; + /* + * The merge paths still assume native leaves. Treat an mm with another + * user granule as having no mergeable VMAs, so it leaves the KSM list. + */ + if (mm_page_size(mm) != PAGE_SIZE) + goto no_vmas; for_each_vma(vmi, vma) { if (!(vma->vm_flags & VM_MERGEABLE)) @@ -2874,6 +2880,9 @@ static bool __ksm_should_add_vma(const struct file *file, vma_flags_t vma_flags) static void __ksm_add_vma(struct vm_area_struct *vma) { + /* KSM merges native pages only; see get_mergeable_page(). */ + if (mm_page_size(vma->vm_mm) != PAGE_SIZE) + return; if (__ksm_should_add_vma(vma->vm_file, vma->flags)) vm_flags_set(vma, VM_MERGEABLE); } @@ -2907,6 +2916,7 @@ vma_flags_t ksm_vma_flags(struct mm_struct *mm, const struct file *file, vma_flags_t vma_flags) { if (mm_flags_test(MMF_VM_MERGE_ANY, mm) && + mm_page_size(mm) == PAGE_SIZE && __ksm_should_add_vma(file, vma_flags)) { vma_flags_set(&vma_flags, VMA_MERGEABLE_BIT); /* @@ -2950,12 +2960,15 @@ static int ksm_del_vmas(struct mm_struct *mm) * * @mm: Pointer to mm * - * Returns 0 on success, otherwise error code + * Returns 0 on success, otherwise error code. An mm with a non-native user + * granule gets -EOPNOTSUPP, as MADV_MERGEABLE does. */ int ksm_enable_merge_any(struct mm_struct *mm) { int err; + if (mm_page_size(mm) != PAGE_SIZE) + return -EOPNOTSUPP; if (mm_flags_test(MMF_VM_MERGE_ANY, mm)) return 0; From 15a2fdc3cea0cffed56de940de0536f321c83aa9 Mon Sep 17 00:00:00 2001 From: Alyx-ML <256263590+Alyx-ML@users.noreply.github.com> Date: Sun, 22 Nov 2026 01:10:47 +0000 Subject: [PATCH 3/3] Documentation: arm64: user4k: list what differs from upstream, also when off user4k.rst said the shared MM code "do[es] not change with the parameter". That is true, but a reader can take it to mean that booting with user4k=off, the switch since user4k became the default, gives upstream behaviour. It does not. The shared code is always built in, and some of it behaves differently for every process: - vm_insert_pages() returns EFAULT for a start address that is not aligned to the page size (mm/memory.c). - The fault_around_bytes debugfs file accepts values down to 4K, so writing 0 reads back 4096 instead of the native page size. - generic_access_phys() handles at most one user page per call. Callers that loop, such as access_process_vm(), move the same total. - locked_vm is counted in 4K units (MM_ACCOUNT_SHIFT). Limit checks and VmLck give the same results for native address spaces. List these in user4k.rst, and point to the list from the Kconfig help. Also say plainly what KSM does for an address space with another granule: it does not merge its memory. MADV_MERGEABLE and PR_SET_MEMORY_MERGE fail with EOPNOTSUPP there, and an exec that picks another granule drops an inherited PR_SET_MEMORY_MERGE setting. Each listed difference was checked against the code in this tree. The KSM text matches "mm/ksm: keep address spaces with another user granule out of KSM". Signed-off-by: Alyx-ML <256263590+Alyx-ML@users.noreply.github.com> --- Documentation/arch/arm64/user4k.rst | 25 +++++++++++++++++++++++-- arch/arm64/Kconfig.debug | 5 ++++- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/Documentation/arch/arm64/user4k.rst b/Documentation/arch/arm64/user4k.rst index 2cb5c41905243..2774a9be03a3b 100644 --- a/Documentation/arch/arm64/user4k.rst +++ b/Documentation/arch/arm64/user4k.rst @@ -73,8 +73,23 @@ While it is off: 16K pages). ``page_ext`` itself is still allocated if another user, such as page_owner, needs it. -The shared MM code paths that serve every process are compiled in either -way and do not change with the parameter. +The shared MM code that serves every process is compiled in either way, and +the parameter does not switch it back to the upstream code. A kernel built +with this option therefore differs from upstream even while the feature is +off. The differences that callers can see are: + +* ``vm_insert_pages()`` returns ``EFAULT`` for a start address that is not + aligned to the address space's page size. +* The ``fault_around_bytes`` debugfs file accepts values down to 4K, so + writing ``0`` reads back 4096 rather than the native page size. +* ``generic_access_phys()`` handles at most one user page per call. + ``access_process_vm()`` and other callers that loop still move the same + total. +* ``locked_vm`` is counted in 4K units for every process. Limit checks and + ``VmLck`` give the same results as before for native address spaces. + +Other changes, such as the 64K zero page and ``pgtable_t`` holding a +``pte_t`` pointer, keep native results the same. While it is on, the CPU capability line appears on CPUs that support the 4K translation granule, and the launcher below can select 4K pages. @@ -182,6 +197,12 @@ mapping into neighboring slots. Mock IOMMU/VFIO tests validate the recorded translations and ownership paths, not physical-device DMA on arbitrary hardware. +KSM does not merge the memory of an address space with a non-native +granule. ``MADV_MERGEABLE`` and ``PR_SET_MEMORY_MERGE`` fail there with +``EOPNOTSUPP``. An exec that selects another granule drops an inherited +``PR_SET_MEMORY_MERGE`` setting, so ``PR_GET_MEMORY_MERGE`` then reports it +off, and native programs that this process starts later do not inherit it. + Alternative-granule THP/KSM and hugetlb support, remaining legacy pin consumers, larger-than-native process activation and broader device integration still need work. Userfaultfd feature bits and per-range ioctl diff --git a/arch/arm64/Kconfig.debug b/arch/arm64/Kconfig.debug index 630d444592c0d..d7e8904af85ac 100644 --- a/arch/arm64/Kconfig.debug +++ b/arch/arm64/Kconfig.debug @@ -42,7 +42,9 @@ config ARM64_USER4K_EXPERIMENTAL prctls refuse every non-native size with EINVAL, every exec gets the native granule, the CPUs report no alternative user granule and the per-page 4K ownership record in page_ext is not - reserved. The shared MM code paths remain compiled in either way. + reserved. The shared MM code paths remain compiled in either way, so + a few behaviours differ from upstream even while it is off; see + Documentation/arch/arm64/user4k.rst. While it is on, an experimental prctl selects a supported granule for the next exec. Ordinary address spaces and kernel tables retain @@ -50,6 +52,7 @@ config ARM64_USER4K_EXPERIMENTAL and device mappings still require integration and validation. The private prctl numbers are not upstream assignments or a stable ABI promise. Programs that do not use the prctl keep the native granule. + KSM does not merge the memory of programs that use another granule. config ARM64_CPUINFO_KUNIT_TEST bool "KUnit tests for the /proc/cpuinfo model name" if !KUNIT_ALL_TESTS