Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Documentation/arch/arm64/user4k.rst
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,11 @@ 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 a process with another granule.
``MADV_MERGEABLE`` and ``PR_SET_MEMORY_MERGE`` fail with ``EOPNOTSUPP``
there. A ``PR_SET_MEMORY_MERGE`` setting does not carry over into an exec
that selects another granule, and so is gone after a later native exec too.

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
Expand Down
22 changes: 19 additions & 3 deletions include/linux/ksm.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@
#include <linux/sched.h>

#ifdef CONFIG_KSM
/*
* 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, so it stays out of KSM.
*/
static inline bool ksm_supports_mm(const struct mm_struct *mm)
{
return mm_page_size(mm) == PAGE_SIZE;
}

int ksm_madvise(struct vm_area_struct *vma, unsigned long start,
unsigned long end, int advice, vm_flags_t *vm_flags);
vma_flags_t ksm_vma_flags(struct mm_struct *mm, const struct file *file,
Expand Down Expand Up @@ -68,10 +78,16 @@ static inline void ksm_fork(struct mm_struct *mm, struct mm_struct *oldmm)

static inline int ksm_execve(struct mm_struct *mm)
{
if (mm_flags_test(MMF_VM_MERGE_ANY, mm))
return __ksm_enter(mm);
if (!mm_flags_test(MMF_VM_MERGE_ANY, mm))
return 0;

return 0;
/* The setting does not carry over into an image with another granule. */
if (!ksm_supports_mm(mm)) {
mm_flags_clear(MMF_VM_MERGE_ANY, mm);
return 0;
}

return __ksm_enter(mm);
}

static inline void ksm_exit(struct mm_struct *mm)
Expand Down
23 changes: 18 additions & 5 deletions mm/ksm.c
Original file line number Diff line number Diff line change
Expand Up @@ -821,7 +821,7 @@ static struct page *get_mergeable_page(struct ksm_rmap_item *rmap_item)
struct folio *folio;

/* The KSM comparison and replacement paths still require native leaves. */
if (mm_page_size(mm) != PAGE_SIZE)
if (!ksm_supports_mm(mm))
return NULL;

mmap_read_lock(mm);
Expand Down Expand Up @@ -2686,7 +2686,12 @@ static struct ksm_rmap_item *scan_get_next_rmap_item(struct page **page)
vma_iter_init(&vmi, mm, ksm_scan.address);

mmap_read_lock(mm);
if (ksm_test_exit(mm))
/*
* An address space with another user granule is never entered (see
* ksm_supports_mm()). Should one get here, drop it like an mm without
* mergeable VMAs, before any of its pages reach the merge code.
*/
if (ksm_test_exit(mm) || !ksm_supports_mm(mm))
goto no_vmas;

for_each_vma(vmi, vma) {
Expand Down Expand Up @@ -2874,7 +2879,8 @@ 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)
{
if (__ksm_should_add_vma(vma->vm_file, vma->flags))
if (ksm_supports_mm(vma->vm_mm) &&
__ksm_should_add_vma(vma->vm_file, vma->flags))
vm_flags_set(vma, VM_MERGEABLE);
}

Expand Down Expand Up @@ -2906,7 +2912,7 @@ static int __ksm_del_vma(struct vm_area_struct *vma)
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) &&
if (mm_flags_test(MMF_VM_MERGE_ANY, mm) && ksm_supports_mm(mm) &&
__ksm_should_add_vma(file, vma_flags)) {
vma_flags_set(&vma_flags, VMA_MERGEABLE_BIT);
/*
Expand Down Expand Up @@ -2950,12 +2956,17 @@ static int ksm_del_vmas(struct mm_struct *mm)
*
* @mm: Pointer to mm
*
* Returns 0 on success, otherwise error code
* Returns 0 on success, -EOPNOTSUPP for an mm with another user granule,
* otherwise error code
*/
int ksm_enable_merge_any(struct mm_struct *mm)
{
int err;

/* As MADV_MERGEABLE: see ksm_supports_mm(). */
if (!ksm_supports_mm(mm))
return -EOPNOTSUPP;

if (mm_flags_test(MMF_VM_MERGE_ANY, mm))
return 0;

Expand Down Expand Up @@ -3019,6 +3030,8 @@ int ksm_madvise(struct vm_area_struct *vma, unsigned long start,

switch (advice) {
case MADV_MERGEABLE:
if (!ksm_supports_mm(mm))
return -EOPNOTSUPP;
if (vma->vm_flags & VM_MERGEABLE)
return 0;
if (!vma_ksm_compatible(vma))
Expand Down
2 changes: 1 addition & 1 deletion tools/testing/selftests/arm64/user4k/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# SPDX-License-Identifier: GPL-2.0
TEST_GEN_PROGS := page-contract proc-args gem-userptr
TEST_GEN_PROGS := page-contract proc-args gem-userptr ksm-merge-any
TEST_GEN_PROGS_EXTENDED := iommufd-init coarse-shmem-init bpf-uptr-init vhost-log-init uprobes-init user-events-init uffd-move-init swap-fixture-init exec-fixture-init mixed-process memory-interfaces advice reclaim-advice proc-pages proc-memory core-fixture-init context-stress-init mlock-fixture-init lazyfree-fixture-init thread-fixture-init splice-fixture-init dio-fixture-init uring-fixture-init gem-fixture-init compat-fixture-init granule-proc-init granule-swap-init aslr-fixture-init cpu-granule-init user16k-smoke-init cpu-granule16-init granule-threeway-init compat-mlock-init mglru-granule-init memcg-pressure-init memcg-accounting-init runtime-app-init fex-contract-init exec-policy-init uffd-wp-init shmem-uffd-swap-init compat-uffd-init shmem-hole-init shmem-thp-init shmem-collapse-init shmem-pin-init shmem-race-init shmem-pressure-init
TEST_GEN_PROGS_EXTENDED += vfio-fixture-init sysv-shm-init perf-ring-init
TEST_GEN_PROGS_EXTENDED += aio-ring-init numa-policy-init numa-balancing-init cow-pool
Expand Down
169 changes: 169 additions & 0 deletions tools/testing/selftests/arm64/user4k/ksm-merge-any.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
// SPDX-License-Identifier: GPL-2.0-only
/*
* KSM and 4K processes on a 16K kernel. KSM hashes, compares and replaces
* whole native pages, so a process with another user granule stays out of
* it: PR_SET_MEMORY_MERGE fails there with EOPNOTSUPP, as MADV_MERGEABLE
* does, and the setting of a native process does not carry over into an
* exec that selects another granule, nor back into a native exec after
* that. A native exec keeps it. ksmd need not run.
*
* On a kernel that lets a 4K process into KSM, run this only with KSM
* stopped (/sys/kernel/mm/ksm/run 0): a running ksmd would merge the 4K
* process's pages. A 4K process that finds itself let in backs out at once,
* before ksmd can have compared its pages twice, which a merge needs.
*
* Each case runs in a child that execs this program again in its role and
* reports through its exit status.
*/
#define _GNU_SOURCE
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../../../../../include/uapi/linux/prctl.h"
#include <sys/auxv.h>
#include <sys/prctl.h>
#include <sys/wait.h>
#include <unistd.h>
#include "../../kselftest.h"

#define NATIVE 16384UL
#define LEAF 4096UL

/* A yes/no field of /proc/self/ksm_stat: 1, 0, or -1 where there is none. */
static int ksm_stat_field(const char *key)
{
char line[128];
size_t n = strlen(key);
int ret = -1;
FILE *f = fopen("/proc/self/ksm_stat", "r");

if (!f)
return -1;
while (fgets(line, sizeof(line), f))
if (!strncmp(line, key, n) && line[n] == ':')
ret = !strncmp(line + n + 1, " yes", 4);
fclose(f);
return ret;
}

static int merge_any(void)
{
return prctl(PR_GET_MEMORY_MERGE, 0UL, 0UL, 0UL, 0UL);
}

/*
* The process is out of KSM: no process-wide setting, no mergeable VMA.
* Where it is in, it backs out at once (see the top of this file).
*/
static int out_of_ksm(void)
{
int any = merge_any(), mergeable = ksm_stat_field("ksm_mergeable");

if (any > 0)
prctl(PR_SET_MEMORY_MERGE, 0UL, 0UL, 0UL, 0UL);
fprintf(stderr, "PR_GET_MEMORY_MERGE %d, ksm_mergeable %d\n",
any, mergeable);
return any == 0 && mergeable == 0;
}

/* The roles: each returns its exit status, 0 when it passes. */
static int role(const char *name, char *argv0)
{
unsigned long size = getauxval(AT_PAGESZ);

if (!strcmp(name, "refuses")) {
int ret, err;

if (size != LEAF)
return KSFT_SKIP;
ret = prctl(PR_SET_MEMORY_MERGE, 1UL, 0UL, 0UL, 0UL);
err = errno;
if (!ret)
prctl(PR_SET_MEMORY_MERGE, 0UL, 0UL, 0UL, 0UL);
fprintf(stderr, "PR_SET_MEMORY_MERGE in a 4K process: %d (%s)\n",
ret, ret ? strerror(err) : "accepted");
return !(ret == -1 && err == EOPNOTSUPP && out_of_ksm());
}
if (!strcmp(name, "inherited") ||
!strcmp(name, "inherited-then-native")) {
if (size != LEAF)
return KSFT_SKIP;
if (!out_of_ksm())
return 1;
if (!strcmp(name, "inherited"))
return 0;
if (prctl(PR_AURORA_SET_EXEC_PAGE_SIZE, 0UL, 0UL, 0UL, 0UL))
return 1;
execl("/proc/self/exe", argv0, "native-after", NULL);
return 1;
}
if (!strcmp(name, "native-after")) {
if (size != NATIVE)
return KSFT_SKIP;
return !out_of_ksm();
}
if (!strcmp(name, "native-kept")) {
int any = merge_any();

if (size != NATIVE)
return KSFT_SKIP;
fprintf(stderr, "PR_GET_MEMORY_MERGE after a native exec: %d\n",
any);
return any != 1;
}
return 1;
}

/*
* Runs @name in a child: with the process-wide KSM setting of a native
* process first if @merge, then in an exec at @size (native for zero).
*/
static void run(const char *desc, char *argv0, int merge, unsigned long size,
const char *name)
{
int status;
pid_t pid = fork();

if (pid < 0)
ksft_exit_fail_msg("fork: %s\n", strerror(errno));
if (!pid) {
if (merge && prctl(PR_SET_MEMORY_MERGE, 1UL, 0UL, 0UL, 0UL)) {
fprintf(stderr, "PR_SET_MEMORY_MERGE: %s\n",
strerror(errno));
_exit(1);
}
if (prctl(PR_AURORA_SET_EXEC_PAGE_SIZE, size, 0UL, 0UL, 0UL) &&
size)
_exit(KSFT_SKIP);
execl("/proc/self/exe", argv0, name, NULL);
_exit(1);
}
if (waitpid(pid, &status, 0) != pid)
ksft_exit_fail_msg("waitpid: %s\n", strerror(errno));
if (WIFEXITED(status) && WEXITSTATUS(status) == KSFT_SKIP)
ksft_test_result_skip("%s: no 4K processes\n", desc);
else
ksft_test_result(WIFEXITED(status) && !WEXITSTATUS(status),
"%s\n", desc);
}

int main(int argc, char **argv)
{
if (argc > 1)
return role(argv[1], argv[0]);

ksft_print_header();
if (getauxval(AT_PAGESZ) != NATIVE)
ksft_exit_skip("not a 16K process on a 16K kernel\n");
if (merge_any() < 0)
ksft_exit_skip("no KSM: %s\n", strerror(errno));
ksft_set_plan(4);
run("a 4K process cannot opt into KSM", argv[0], 0, LEAF, "refuses");
run("the setting does not follow a native process into a 4K exec",
argv[0], 1, LEAF, "inherited");
run("nor back into a native exec after that", argv[0], 1, LEAF,
"inherited-then-native");
run("a native exec keeps the setting", argv[0], 1, 0, "native-kept");
ksft_finished();
}
Loading