Repository navigation
mm/ksm: keep address spaces with another user granule out of KSM - #3232
Conversation
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]>
|
J773g (M4 Mac mini, How it was run:
Kernel log:
Smoke on the candidate: The Mac went back to the stock kernel with KSM run 0, and the kit and test binaries were removed from it. |
|
Verdict at Blockers: none Checked: no path still lets memory of an mm with another granule reach KSM. I went through every caller of
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
|
|
J714s (MacBook Pro 14" M5 Pro, T6050, 16K pages) row at
|
|
Merge readiness at
Areas: none matched in CODEOWNERS Updated by pr-readiness on pushes, edits, reviews and comments, and on a schedule. Rules: merge policy · labels. |
There was a problem hiding this comment.
Verdict: HOLD at cd88c18b
Blockers:
Apple build and KUnitCI was cancelled atcd88c18b.Host tests,pr-text, andpr-readinesspassed, butApple build and KUnitcompleted ascancelledand never ran to success. Re-runApple build and KUnitso CI is fully green atcd88c18b.
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),MERGEABLEagainstmain. ksm_supports_mm()(mm_page_size(mm) == PAGE_SIZE) cleanly gatesksm_enable_merge_any(),ksm_madvise(MADV_MERGEABLE),ksm_vma_flags(),__ksm_add_vma(),get_mergeable_page(),scan_get_next_rmap_item(), andksm_execve()(clearing inheritedMMF_VM_MERGE_ANYacross non-native exec).- Dual-generation exact-head hardware proof at
cd88c18b4e7d: both J773g (M4 mini) and J714s (M5 Pro, 16K pages) passedksm-merge-any(4/4 stopped and running) andksm_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 KUnitworkflow atcd88c18b.
There was a problem hiding this comment.
Verdict: MERGE at cd88c18b
Blockers: None.
Checked:
- Hosted CI at
cd88c18bis now completely green (23P/0F/0QincludingApple build and KUnit,Host tests,pr-text, andreadiness), clearing the sole remaining hold on this PR. - Previously audited code and dual-generation hardware rows at
cd88c18bremain valid; branch isMERGEABLEintomain.
Evidence
- Head commit:
cd88c18b4e7d3e2cb854075aeea5bed4a8662e6d(MERGEABLEintomain). - CI snapshot:
23P/0F/0Q.
|
Merging at |
What: KSM now keeps out every address space whose user granule isn't the native one.
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_MERGEABLEwas already refused for such a process.PR_SET_MEMORY_MERGEnow fails there the same way, withEOPNOTSUPP. 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.Tested: the host build at
W=1is clean for every object that uses the changed code, withCONFIG_KSM,CONFIG_MM_SUBPAGEand 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 nativeksm_functional_testspass. 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 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, asMADV_MERGEABLEdoes.ksm_execve()clears an inheritedMMF_VM_MERGE_ANYand 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()), beforecreate_init_stack_vma()callsksm_execve(). That also holds for the replacement mm thatbprm_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()andksm_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:
PR_GET_MEMORY_MERGEreports 0.CONFIG_MM_SUBPAGE,ksm_supports_mm()is a constant true.Documentation/arch/arm64/user4k.rstnow 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:PR_SET_MEMORY_MERGEfails withEOPNOTSUPP, and/proc/self/ksm_statshows it is not mergeable.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/run0):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 1not ok 3 nor back into a native exec after that:PR_GET_MEMORY_MERGE 1, ksm_mergeable 1ok 4 a native exec keeps the settingHost build of
mm/ksm.o,mm/vma_exec.o,mm/vma.o,mm/madvise.o,fs/exec.o,fs/proc/base.o,kernel/fork.oandkernel/sys.owith the asahi profile (CONFIG_KSM=y,CONFIG_MM_SUBPAGE=y,CONFIG_ARM64_USER4K_EXPERIMENTAL=y) atW=1: no errors or warnings.J773g (M4 mini), see the row comment:
not ok1–3,ok 4ksm-merge-any4/4 with KSM stopped and 4/4 with KSM runningksm_functional_tests: 9 pass, 1 skip (userfaultfd), 0 failrunrestored to 0WARNING:/BUG/Oops lines and no ksm or rmap errorscheckpatch reports 0 errors. The only warning is the usual MAINTAINERS note for a new selftest file. The selftest builds with
-Wall -Wextraand no warnings under gcc and clang.