Skip to content

mm/ksm: keep address spaces with another user granule out of KSM - #3232

Merged
RyanTheTide merged 2 commits into
mainfrom
mm/ksm-other-granule
Dec 1, 2026
Merged

RyanTheTide merged 2 commits into
mainfrom
mm/ksm-other-granule

Conversation

@DjDeveloperr

@DjDeveloperr DjDeveloperr commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

What: KSM now keeps out every address space whose user granule isn't the native one.

  • The gap: a 4K process on a 16K kernel could opt into process-wide merging with PR_SET_MEMORY_MERGE, or inherit that setting from a native parent through exec. Once global KSM was running, ksmd passed that process's pages to merge code that assumes whole native pages. MADV_MERGEABLE was already refused for such a process.
  • The fix: PR_SET_MEMORY_MERGE now fails there the same way, with EOPNOTSUPP. An exec into another granule drops the inherited setting, no VMA of such an mm is marked mergeable, and the scanner drops such an mm from its list.
  • Test: a new selftest covers it.

Tested: the host build at W=1 is clean for every object that uses the changed code, with CONFIG_KSM, CONFIG_MM_SUBPAGE and user4k enabled. On an unfixed 16K kernel with KSM stopped, the new selftest fails its 3 granule cases and passes the native control. J773g passes: the selftest fails as expected on the stock kernel, then passes 4/4 on the fixed kernel with KSM stopped and 4/4 with KSM running. The native ksm_functional_tests pass. The J714s row is pending.
Risk: two-way door · every Mac on an Aurora 16K kernel: KSM users with 4K processes; native KSM is unchanged

Not yet

  • The M4 mini and M5 Pro both have posted KSM rows. Refresh affected results after the base merge, then rerun the canceled CI checks.

the J714s row

Details

ksm_supports_mm() is true for an mm with native user pages. It is used at every way into KSM:

  • ksm_enable_merge_any() (PR_SET_MEMORY_MERGE) returns -EOPNOTSUPP, as MADV_MERGEABLE does.
  • ksm_execve() clears an inherited MMF_VM_MERGE_ANY and does not enter KSM. The exec still succeeds. The granule of an exec's mm is set when the mm is allocated (__mm_alloc() → arch_mm_init_exec()), before create_init_stack_vma() calls ksm_execve(). That also holds for the replacement mm that bprm_set_page_shift() builds when the image selects another granule. That mm copies its flags from the caller's mm again, so the final mm's granule decides.
  • ksm_vma_flags(), __ksm_add_vma() and ksm_madvise() mark no VMA of such an mm mergeable.
  • scan_get_next_rmap_item() treats such an mm like one with no mergeable VMAs: it frees its rmap items and drops it from the list. With the entry points closed, no such mm should reach the scanner; this is the backstop.

Consequences:

  • After an exec into a 4K process, PR_GET_MEMORY_MERGE reports 0.
  • A native program that the 4K process starts later doesn't get the setting back.
  • A native exec keeps it, as before.
  • Without CONFIG_MM_SUBPAGE, ksm_supports_mm() is a constant true.

Documentation/arch/arm64/user4k.rst now says so.

The KSM commit of draft #2993 makes the same kernel change. This PR carries it on its own, with a selftest and hardware rows. If either lands first, the other can drop it.

Selftest tools/testing/selftests/arm64/user4k/ksm-merge-any, in the default program list. Each case runs in a child that re-execs the test in its role, so ksmd need not run:

  1. A 4K process's PR_SET_MEMORY_MERGE fails with EOPNOTSUPP, and /proc/self/ksm_stat shows it is not mergeable.
  2. A native process with the setting execs a 4K image. The setting is gone, and the image has no mergeable VMA.
  3. The same 4K image then execs a native one, which doesn't get the setting back.
  4. Control: a native exec keeps the setting.

On a kernel without the fix, run it only with KSM stopped. A 4K process that finds itself let in backs out at once, before ksmd could have compared its pages twice, which a merge needs.

Evidence

Unfixed host kernel (7.3.0-1-28, 16K, /sys/kernel/mm/ksm/run 0):

  • not ok 1 a 4K process cannot opt into KSM: PR_SET_MEMORY_MERGE in a 4K process: 0 (accepted)
  • not ok 2 the setting does not follow a native process into a 4K exec: PR_GET_MEMORY_MERGE 1, ksm_mergeable 1
  • not ok 3 nor back into a native exec after that: PR_GET_MEMORY_MERGE 1, ksm_mergeable 1
  • ok 4 a native exec keeps the setting

Host build of mm/ksm.o, mm/vma_exec.o, mm/vma.o, mm/madvise.o, fs/exec.o, fs/proc/base.o, kernel/fork.o and kernel/sys.o with the asahi profile (CONFIG_KSM=y, CONFIG_MM_SUBPAGE=y, CONFIG_ARM64_USER4K_EXPERIMENTAL=y) at W=1: no errors or warnings.

J773g (M4 mini), see the row comment:

  • stock control, KSM stopped: not ok 1–3, ok 4
  • fixed kernel: ksm-merge-any 4/4 with KSM stopped and 4/4 with KSM running
  • ksm_functional_tests: 9 pass, 1 skip (userfaultfd), 0 fail
  • after unmerging: 0 shared pages; run restored to 0
  • no WARNING:/BUG/Oops lines and no ksm or rmap errors

checkpatch reports 0 errors. The only warning is the usual MAINTAINERS note for a new selftest file. The selftest builds with -Wall -Wextra and no warnings under gcc and clang.

KSM hashes, compares, write-protects and replaces whole native pages. An
address space with another user granule maps a native page in parts that
it tracks one by one. get_mergeable_page() and madvise(MADV_MERGEABLE)
refused such an address space, but the process-wide setting did not:

- PR_SET_MEMORY_MERGE entered the mm into KSM and marked its VMAs
  mergeable, and ksm_vma_flags() marked new ones.
- MMF_VM_MERGE_ANY survives exec, and ksm_execve() entered the new mm
  even where exec had selected another granule for it.
- ksmd then passed the pages of that mm to the merge code.

Add ksm_supports_mm(), true for an mm with native user pages, and use it
throughout: ksm_enable_merge_any() fails with EOPNOTSUPP, as
MADV_MERGEABLE does; ksm_execve() drops the inherited setting, so the
exec succeeds out of KSM; ksm_vma_flags(), __ksm_add_vma() and
ksm_madvise() mark no VMA of such an mm mergeable; and the scanner drops
such an mm from its list like one without mergeable VMAs. The granule of
an exec's mm is set when it is allocated, before ksm_execve() runs, also
for the mm that replaces it when the image selects another granule.

Without CONFIG_MM_SUBPAGE, ksm_supports_mm() is always true.

Signed-off-by: DjDeveloperr <[email protected]>
Check that PR_SET_MEMORY_MERGE fails with EOPNOTSUPP in a 4K process on
a 16K kernel, that the setting of a native process does not carry over
into a 4K exec nor back into a native exec after that, and that a native
exec keeps it. ksmd need not run: the checks read the process-wide
setting and /proc/self/ksm_stat.

Signed-off-by: DjDeveloperr <[email protected]>
@DjDeveloperr

Copy link
Copy Markdown
Contributor Author

J773g (M4 Mac mini, user4k=on) at cd88c18b4e7d: PASS. A 4K process can no longer opt into KSM, and an inherited merge-any is dropped on exec into 4K, with KSM stopped and with it running.

How it was run:

  • ksm-merge-any was built from this head with gcc -O2 -Wall -Wextra (no warnings) and run as an ordinary user (uid 1000).
  • The candidate is one guarded one-shot of main 255761f1d55f plus this PR, plus drm/asahi: charge and reclaim the CPU-faulted pages of revocable mappings #3224. This PR's 5 files are identical to its head, and no files overlap.
  • KSM stays stopped on the stock kernel: /sys/kernel/mm/ksm/run was 0 before and after the control.
ksm-merge-any stock 7.3.0-ARCH-m4mainefed1ae3, KSM run 0 candidate, KSM stopped candidate, KSM running (run 1)
1 a 4K process cannot opt into KSM not ok (PR_SET_MEMORY_MERGE returned 0, accepted) ok ok
2 the setting does not follow a native process into a 4K exec not ok ok ok
3 nor back into a native exec after that not ok ok ok
4 a native exec keeps the setting ok ok ok

tools/testing/selftests/mm/ksm_functional_tests from this head, as root with KSM running: 9 pass, 1 skip (__NR_userfaultfd failed), 0 fail. Then run was set to 2 (unmerge), which left pages_shared 0 and pages_sharing 0, and then it was restored to 0.

Kernel log:

  • Over the KSM steps: no lines at all.
  • Whole boot: 0 WARNING:, BUG or Oops lines, and no ksm or rmap errors.
  • Stock control: no kernel lines.

Smoke on the candidate: dEQP-VK.api.smoke.* 8/8 and api.external.memory.dma_buf 44 pass / 6 not supported. One s2idle passed, Wi-Fi had 0 rx errors, and there were 0 GPU faults and 0 failed units.

The Mac went back to the stock kernel with KSM run 0, and the kit and test binaries were removed from it.

@DjDeveloperr

Copy link
Copy Markdown
Contributor Author

Verdict at cd88c18b4e7d: ready

Blockers: none

Checked: no path still lets memory of an mm with another granule reach KSM. I went through every caller of __ksm_enter(), every place a VMA gets VM_MERGEABLE, and how the granule is set.

  • prctl: PR_SET_MEMORY_MERGE → ksm_enable_merge_any() returns -EOPNOTSUPP before it touches the flag or the list. That is the only caller (kernel/sys.c).
  • madvise: ksm_madvise(MADV_MERGEABLE) checks vma->vm_mm, so process_madvise() on another process's 4K mm is refused too, before __ksm_enter().
  • New VMAs: ksm_vma_flags() (mmap, brk, stack) and __ksm_add_vma() (ksm_add_vmas(), including ksm_disable_merge_any()'s rollback) mark nothing in such an mm. mremap, mprotect and VMA splits only carry an existing VM_MERGEABLE, which such an mm never gets.
  • exec: ksm_execve() runs from create_init_stack_vma() on the mm being built, after __mm_alloc() has fixed its granule. The replacement mm in bprm_set_page_shift() also goes through create_init_stack_vma(). That mm copies MMF_VM_MERGE_ANY from the caller's mm, so it gets the check too. A native first-pass mm that had already entered KSM is dropped through mmput() → ksm_exit() when it is replaced.
  • fork: ksm_fork() enters the child only if the parent has MMF_VM_MERGEABLE. MMF_VM_MERGE_ANY is in MMF_INIT_LEGACY_MASK, so it carries over, but the child has the parent's granule, and a 4K parent can no longer be in KSM.
  • The granule cannot change later: mm_page_shift() is set at allocation, and bprm_set_page_shift() builds a new mm rather than changing one ("Table geometry is immutable").
  • sysfs: run, merge_across_nodes, use_zero_pages and the other knobs only act on mms already on ksm_mm_list. None of them adds an mm.
  • Backstop: scan_get_next_rmap_item() sends such an mm to no_vmas. That sets the scan address to 0 and frees its rmap items, then takes it off the list, clears MMF_VM_MERGEABLE and drops the mm before any page is looked up. get_mergeable_page() uses the same check.
  • Merged-code rules: no runtime switch and no SoC or board gate. ksm_supports_mm() compares against PAGE_SIZE, and without CONFIG_MM_SUBPAGE it is always true. The selftest's 16384/4096 values only choose skips.

This is shared mm code, so the merge gate needs both rows listed under "Not yet" (J714s and J773g). The PR is still a draft.

Evidence
  • Build at cd88c18b4e7d with the asahi profile (LLVM, CONFIG_KSM, CONFIG_MM_SUBPAGE and CONFIG_ARM64_USER4K_EXPERIMENTAL all on) and W=1: mm/ksm.o, mm/vma_exec.o, mm/vma.o, mm/madvise.o, mm/mmap.o, fs/exec.o, fs/proc/base.o, kernel/fork.o and kernel/sys.o, exit 0, 0 warnings.
  • checkpatch --strict on the kernel commit: no problems.
  • ksm-merge-any builds with -Wall -Wextra under gcc and clang with no warnings.
  • ksm-merge-any on an unfixed 16K kernel (this host, 7.3.0-1-28, /sys/kernel/mm/ksm/run 0): not ok 1, not ok 2 and not ok 3 (PR_SET_MEMORY_MERGE in a 4K process: 0 (accepted), PR_GET_MEMORY_MERGE 1, ksm_mergeable 1), and ok 4 (the native control). That matches the PR.
  • CI was still queued at this head.

@DjDeveloperr

Copy link
Copy Markdown
Contributor Author

J714s (MacBook Pro 14" M5 Pro, T6050, 16K pages) row at cd88c18b4e7d: PASS.

@github-actions

github-actions Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Merge readiness at cd88c18b: not ready yet

Check Detail
CI at head no failing: apple-build, host-tests, KUnit ane-boot, KUnit ane-pm, KUnit dcp-fabric, KUnit dcp-topology
Review no needs one approving review from Core or Omarchy Mac (not the author)
Requested changes ok none
Hardware row at head ok comment at cd88c18b
Conflicts ok none
PR text ok Risk: two-way · every Mac on an Aurora 16K kernel: KSM users with 4K processes; native KSM is unchanged

Areas: none matched in CODEOWNERS

Updated by pr-readiness on pushes, edits, reviews and comments, and on a schedule. Rules: merge policy · labels.

@aurora-silicon-review aurora-silicon-review Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: HOLD at cd88c18b

Blockers:

  1. Apple build and KUnit CI was cancelled at cd88c18b. Host tests, pr-text, and pr-readiness passed, but Apple build and KUnit completed as cancelled and never ran to success. Re-run Apple build and KUnit so CI is fully green at cd88c18b.

Checked:

  • Full non-test diff (Documentation/arch/arm64/user4k.rst, include/linux/ksm.h, mm/ksm.c) plus new selftest (tools/testing/selftests/arm64/user4k/ksm-merge-any.c), MERGEABLE against main.
  • ksm_supports_mm() (mm_page_size(mm) == PAGE_SIZE) cleanly gates ksm_enable_merge_any(), ksm_madvise(MADV_MERGEABLE), ksm_vma_flags(), __ksm_add_vma(), get_mergeable_page(), scan_get_next_rmap_item(), and ksm_execve() (clearing inherited MMF_VM_MERGE_ANY across non-native exec).
  • Dual-generation exact-head hardware proof at cd88c18b4e7d: both J773g (M4 mini) and J714s (M5 Pro, 16K pages) passed ksm-merge-any (4/4 stopped and running) and ksm_functional_tests (9 pass / 1 skip / 0 fail) with 0 warnings/oops.
Evidence
  • Code and dual-gen hardware verification are complete; strictly holding on rerunning the cancelled Apple build and KUnit workflow at cd88c18b.

@github-actions github-actions Bot removed the ci-failing A check failed at the PR head (bot) label Nov 30, 2026

@aurora-silicon-review aurora-silicon-review Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: MERGE at cd88c18b

Blockers: None.

Checked:

  • Hosted CI at cd88c18b is now completely green (23P/0F/0Q including Apple build and KUnit, Host tests, pr-text, and readiness), clearing the sole remaining hold on this PR.
  • Previously audited code and dual-generation hardware rows at cd88c18b remain valid; branch is MERGEABLE into main.
Evidence
  • Head commit: cd88c18b4e7d3e2cb854075aeea5bed4a8662e6d (MERGEABLE into main).
  • CI snapshot: 23P/0F/0Q.

@github-actions github-actions Bot added the ci-failing A check failed at the PR head (bot) label Dec 1, 2026
@RyanTheTide

Copy link
Copy Markdown
Contributor

Merging at cd88c18b4e7d (B2 merge train, 12-01 merge bar). Approval at this head: aurora-silicon-review[bot]. Row: the hardware row at cd88c18 that the readiness check found (PR body or comment). CI: linux train L3 = main 429e7e1 + 28 approved PRs (candidate 45c2fa4b): Apple driver compile pass, Apple DTBs W=1 pass, planned KUnit dcp-fabric/dcp-topology/dcp-display/macsmc/spi-hid-apple/mt7932/asahi/sep pass; ave failed to build only because of #3153 (unused static helper under -Werror; #3153 held out) and passes with #3153 reverted from the candidate; host tests apple-dcp and apple-scaler-recovery pass. The rest of the hosted run is queued behind the backlog. Revert: git revert -m 1 <merge commit>.

@RyanTheTide
RyanTheTide merged commit d9295a7 into main Dec 1, 2026
27 of 114 checks passed
@RyanTheTide
RyanTheTide deleted the mm/ksm-other-granule branch December 1, 2026 08:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

blast:all Macs affected, from Merge risk (bot) ci-failing A check failed at the PR head (bot) door:two-way Revertible or not, from Merge risk (bot) needs-review No approving review from Core or Omarchy Mac yet (bot)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants