Repository navigation
Conversation
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>
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 36e4785 ("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>
…hen 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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Part of the #2755 split. Draft until it has a run on a Mac (the last Evidence line says which). user4k is on by default now (36e4785), so any process can reach
these paths through the page-size prctl.
Summary
Evidence
checks. Build: passed, Apple build and KUnit. There is no user4k KSM selftest yet.
PR_SET_MEMORY_MERGEfailswith EOPNOTSUPP, and a native process where KSM still merges.
Merge Danger
Door: two-way
Blast Radius: KSM for 4K-granule processes
Without CONFIG_MM_SUBPAGE every new test folds away.
Assisted-by: LLM (Claude:Opus 5.5)