tools/testing/selftests/mm/merge.c

Source file repositories/reference/linux-study-clean/tools/testing/selftests/mm/merge.c

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/mm/merge.c
Extension
.c
Size
48197 bytes
Lines
1593
Domain
Support Tooling And Documentation
Bucket
tools
Inferred role
Support Tooling And Documentation: implementation source
Status
source implementation candidate

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

// SPDX-License-Identifier: GPL-2.0-or-later

#define _GNU_SOURCE
#include "kselftest_harness.h"
#include <linux/prctl.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/prctl.h>
#include <sys/syscall.h>
#include <sys/wait.h>
#include <linux/perf_event.h>
#include "vm_util.h"
#include <linux/mman.h>

FIXTURE(merge)
{
	unsigned int page_size;
	char *carveout;
	struct procmap_fd procmap;
};

static char *map_carveout(unsigned int page_size)
{
	return mmap(NULL, 30 * page_size, PROT_NONE,
		    MAP_ANON | MAP_PRIVATE, -1, 0);
}

static pid_t do_fork(struct procmap_fd *procmap)
{
	pid_t pid = fork();

	if (pid == -1)
		return -1;
	if (pid != 0) {
		wait(NULL);
		return pid;
	}

	/* Reopen for child. */
	if (close_procmap(procmap))
		return -1;
	if (open_self_procmap(procmap))
		return -1;

	return 0;
}

#ifdef __NR_mseal
static int sys_mseal(void *ptr, size_t len, unsigned long flags)
{
	return syscall(__NR_mseal, (unsigned long)ptr, len, flags);
}
#else
static int sys_mseal(void *ptr, size_t len, unsigned long flags)
{
	errno = ENOSYS;
	return -1;
}
#endif

FIXTURE_SETUP(merge)
{
	self->page_size = psize();
	/* Carve out PROT_NONE region to map over. */
	self->carveout = map_carveout(self->page_size);
	ASSERT_NE(self->carveout, MAP_FAILED);
	/* Setup PROCMAP_QUERY interface. */
	ASSERT_EQ(open_self_procmap(&self->procmap), 0);
}

FIXTURE_TEARDOWN(merge)
{
	ASSERT_EQ(munmap(self->carveout, 30 * self->page_size), 0);
	/* May fail for parent of forked process. */
	close_procmap(&self->procmap);
	/*
	 * Clear unconditionally, as some tests set this. It is no issue if this
	 * fails (KSM may be disabled for instance).
	 */
	prctl(PR_SET_MEMORY_MERGE, 0, 0, 0, 0);
}

FIXTURE(merge_with_fork)
{
	unsigned int page_size;
	char *carveout;
	struct procmap_fd procmap;

Annotation

Implementation Notes