2021-11-06 04:41:43 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
/*
|
|
|
|
* hugepage-mremap:
|
|
|
|
*
|
|
|
|
* Example of remapping huge page memory in a user application using the
|
2022-03-05 12:28:48 +08:00
|
|
|
* mremap system call. The path to a file in a hugetlbfs filesystem must
|
|
|
|
* be passed as the last argument to this test. The amount of memory used
|
|
|
|
* by this test in MBs can optionally be passed as an argument. If no memory
|
|
|
|
* amount is passed, the default amount is 10MB.
|
2022-01-15 06:07:52 +08:00
|
|
|
*
|
|
|
|
* To make sure the test triggers pmd sharing and goes through the 'unshare'
|
|
|
|
* path in the mremap code use 1GB (1024) or more.
|
2021-11-06 04:41:43 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#define _GNU_SOURCE
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h> /* Definition of O_* constants */
|
|
|
|
#include <sys/syscall.h> /* Definition of SYS_* constants */
|
|
|
|
#include <linux/userfaultfd.h>
|
|
|
|
#include <sys/ioctl.h>
|
2022-10-14 22:40:13 +08:00
|
|
|
#include <string.h>
|
2023-04-13 00:42:47 +08:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include "vm_util.h"
|
2021-11-06 04:41:43 +08:00
|
|
|
|
2022-01-15 06:07:52 +08:00
|
|
|
#define DEFAULT_LENGTH_MB 10UL
|
|
|
|
#define MB_TO_BYTES(x) (x * 1024 * 1024)
|
2021-11-06 04:41:43 +08:00
|
|
|
|
|
|
|
#define PROTECTION (PROT_READ | PROT_WRITE | PROT_EXEC)
|
|
|
|
#define FLAGS (MAP_SHARED | MAP_ANONYMOUS)
|
|
|
|
|
|
|
|
static void check_bytes(char *addr)
|
|
|
|
{
|
|
|
|
printf("First hex is %x\n", *((unsigned int *)addr));
|
|
|
|
}
|
|
|
|
|
2022-01-15 06:07:52 +08:00
|
|
|
static void write_bytes(char *addr, size_t len)
|
2021-11-06 04:41:43 +08:00
|
|
|
{
|
|
|
|
unsigned long i;
|
|
|
|
|
2022-01-15 06:07:52 +08:00
|
|
|
for (i = 0; i < len; i++)
|
2021-11-06 04:41:43 +08:00
|
|
|
*(addr + i) = (char)i;
|
|
|
|
}
|
|
|
|
|
2022-01-15 06:07:52 +08:00
|
|
|
static int read_bytes(char *addr, size_t len)
|
2021-11-06 04:41:43 +08:00
|
|
|
{
|
|
|
|
unsigned long i;
|
|
|
|
|
|
|
|
check_bytes(addr);
|
2022-01-15 06:07:52 +08:00
|
|
|
for (i = 0; i < len; i++)
|
2021-11-06 04:41:43 +08:00
|
|
|
if (*(addr + i) != (char)i) {
|
|
|
|
printf("Mismatch at %lu\n", i);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void register_region_with_uffd(char *addr, size_t len)
|
|
|
|
{
|
|
|
|
long uffd; /* userfaultfd file descriptor */
|
|
|
|
struct uffdio_api uffdio_api;
|
|
|
|
|
|
|
|
/* Create and enable userfaultfd object. */
|
|
|
|
|
|
|
|
uffd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK);
|
|
|
|
if (uffd == -1) {
|
|
|
|
perror("userfaultfd");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
uffdio_api.api = UFFD_API;
|
|
|
|
uffdio_api.features = 0;
|
|
|
|
if (ioctl(uffd, UFFDIO_API, &uffdio_api) == -1) {
|
|
|
|
perror("ioctl-UFFDIO_API");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create a private anonymous mapping. The memory will be
|
|
|
|
* demand-zero paged--that is, not yet allocated. When we
|
|
|
|
* actually touch the memory, it will be allocated via
|
|
|
|
* the userfaultfd.
|
|
|
|
*/
|
|
|
|
|
|
|
|
addr = mmap(NULL, len, PROT_READ | PROT_WRITE,
|
|
|
|
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
|
|
|
if (addr == MAP_FAILED) {
|
|
|
|
perror("mmap");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Address returned by mmap() = %p\n", addr);
|
|
|
|
|
|
|
|
/* Register the memory range of the mapping we just created for
|
|
|
|
* handling by the userfaultfd object. In mode, we request to track
|
|
|
|
* missing pages (i.e., pages that have not yet been faulted in).
|
|
|
|
*/
|
2023-04-13 00:42:47 +08:00
|
|
|
if (uffd_register(uffd, addr, len, true, false, false)) {
|
2021-11-06 04:41:43 +08:00
|
|
|
perror("ioctl-UFFDIO_REGISTER");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-15 06:07:52 +08:00
|
|
|
int main(int argc, char *argv[])
|
2021-11-06 04:41:43 +08:00
|
|
|
{
|
2022-07-19 17:04:14 +08:00
|
|
|
size_t length = 0;
|
2022-10-14 22:40:13 +08:00
|
|
|
int ret = 0, fd;
|
2022-03-05 12:28:48 +08:00
|
|
|
|
2022-10-14 22:40:13 +08:00
|
|
|
if (argc >= 2 && !strcmp(argv[1], "-h")) {
|
|
|
|
printf("Usage: %s [length_in_MB]\n", argv[0]);
|
2022-03-05 12:28:48 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2022-01-15 06:07:52 +08:00
|
|
|
/* Read memory length as the first arg if valid, otherwise fallback to
|
2022-03-05 12:28:48 +08:00
|
|
|
* the default length.
|
2022-01-15 06:07:52 +08:00
|
|
|
*/
|
2022-10-14 22:40:13 +08:00
|
|
|
if (argc >= 2)
|
|
|
|
length = (size_t)atoi(argv[1]);
|
|
|
|
else
|
|
|
|
length = DEFAULT_LENGTH_MB;
|
2022-01-15 06:07:52 +08:00
|
|
|
|
|
|
|
length = MB_TO_BYTES(length);
|
2022-10-14 22:40:13 +08:00
|
|
|
fd = memfd_create(argv[0], MFD_HUGETLB);
|
2021-11-06 04:41:43 +08:00
|
|
|
if (fd < 0) {
|
|
|
|
perror("Open failed");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* mmap to a PUD aligned address to hopefully trigger pmd sharing. */
|
|
|
|
unsigned long suggested_addr = 0x7eaa40000000;
|
2022-01-15 06:07:52 +08:00
|
|
|
void *haddr = mmap((void *)suggested_addr, length, PROTECTION,
|
2021-11-06 04:41:43 +08:00
|
|
|
MAP_HUGETLB | MAP_SHARED | MAP_POPULATE, fd, 0);
|
|
|
|
printf("Map haddr: Returned address is %p\n", haddr);
|
|
|
|
if (haddr == MAP_FAILED) {
|
|
|
|
perror("mmap1");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* mmap again to a dummy address to hopefully trigger pmd sharing. */
|
|
|
|
suggested_addr = 0x7daa40000000;
|
2022-01-15 06:07:52 +08:00
|
|
|
void *daddr = mmap((void *)suggested_addr, length, PROTECTION,
|
2021-11-06 04:41:43 +08:00
|
|
|
MAP_HUGETLB | MAP_SHARED | MAP_POPULATE, fd, 0);
|
|
|
|
printf("Map daddr: Returned address is %p\n", daddr);
|
|
|
|
if (daddr == MAP_FAILED) {
|
|
|
|
perror("mmap3");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
suggested_addr = 0x7faa40000000;
|
|
|
|
void *vaddr =
|
2022-01-15 06:07:52 +08:00
|
|
|
mmap((void *)suggested_addr, length, PROTECTION, FLAGS, -1, 0);
|
2021-11-06 04:41:43 +08:00
|
|
|
printf("Map vaddr: Returned address is %p\n", vaddr);
|
|
|
|
if (vaddr == MAP_FAILED) {
|
|
|
|
perror("mmap2");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2022-01-15 06:07:52 +08:00
|
|
|
register_region_with_uffd(haddr, length);
|
2021-11-06 04:41:43 +08:00
|
|
|
|
2022-01-15 06:07:52 +08:00
|
|
|
void *addr = mremap(haddr, length, length,
|
2021-11-06 04:41:43 +08:00
|
|
|
MREMAP_MAYMOVE | MREMAP_FIXED, vaddr);
|
|
|
|
if (addr == MAP_FAILED) {
|
|
|
|
perror("mremap");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Mremap: Returned address is %p\n", addr);
|
|
|
|
check_bytes(addr);
|
2022-01-15 06:07:52 +08:00
|
|
|
write_bytes(addr, length);
|
|
|
|
ret = read_bytes(addr, length);
|
2021-11-06 04:41:43 +08:00
|
|
|
|
2022-01-15 06:07:52 +08:00
|
|
|
munmap(addr, length);
|
2021-11-06 04:41:43 +08:00
|
|
|
|
2022-05-10 09:20:54 +08:00
|
|
|
addr = mremap(addr, length, length, 0);
|
|
|
|
if (addr != MAP_FAILED) {
|
|
|
|
printf("mremap: Expected failure, but call succeeded\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2022-03-05 12:28:48 +08:00
|
|
|
close(fd);
|
|
|
|
|
2021-11-06 04:41:43 +08:00
|
|
|
return ret;
|
|
|
|
}
|