Apply it against Avi kvm-userspace git tree.
Izik Eidus (2):
qemu: add ksm support
qemu: add ksmctl.
qemu/ksm.h | 70 ++++++++++++++++++++++++++++++++++++++++++++
qemu/vl.c | 34 +++++++++++++++++++++
user/Makefile | 6 +++-
user/config-x86-common.mak | 2 +-
user/ksmctl.c | 69 +++++++++++++++++++++++++++++++++++++++++++
5 files changed, 179 insertions(+), 2 deletions(-)
create mode 100644 qemu/ksm.h
create mode 100644 user/ksmctl.c
Signed-off-by: Izik Eidus <[email protected]>
---
qemu/ksm.h | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
qemu/vl.c | 34 +++++++++++++++++++++++++++++
2 files changed, 104 insertions(+), 0 deletions(-)
create mode 100644 qemu/ksm.h
diff --git a/qemu/ksm.h b/qemu/ksm.h
new file mode 100644
index 0000000..2fb91a8
--- /dev/null
+++ b/qemu/ksm.h
@@ -0,0 +1,70 @@
+#ifndef __LINUX_KSM_H
+#define __LINUX_KSM_H
+
+/*
+ * Userspace interface for /dev/ksm - kvm shared memory
+ */
+
+
+#include <sys/types.h>
+#include <sys/ioctl.h>
+
+#include <asm/types.h>
+
+#define KSM_API_VERSION 1
+
+#define ksm_control_flags_run 1
+
+/* for KSM_REGISTER_MEMORY_REGION */
+struct ksm_memory_region {
+ __u32 npages; /* number of pages to share */
+ __u32 pad;
+ __u64 addr; /* the begining of the virtual address */
+ __u64 reserved_bits;
+};
+
+struct ksm_kthread_info {
+ __u32 sleep; /* number of microsecoends to sleep */
+ __u32 pages_to_scan; /* number of pages to scan */
+ __u32 flags; /* control flags */
+ __u32 pad;
+ __u64 reserved_bits;
+};
+
+#define KSMIO 0xAB
+
+/* ioctls for /dev/ksm */
+
+#define KSM_GET_API_VERSION _IO(KSMIO, 0x00)
+/*
+ * KSM_CREATE_SHARED_MEMORY_AREA - create the shared memory reagion fd
+ */
+#define KSM_CREATE_SHARED_MEMORY_AREA _IO(KSMIO, 0x01) /* return SMA fd */
+/*
+ * KSM_START_STOP_KTHREAD - control the kernel thread scanning speed
+ * (can stop the kernel thread from working by setting running = 0)
+ */
+#define KSM_START_STOP_KTHREAD _IOW(KSMIO, 0x02,\
+ struct ksm_kthread_info)
+/*
+ * KSM_GET_INFO_KTHREAD - return information about the kernel thread
+ * scanning speed.
+ */
+#define KSM_GET_INFO_KTHREAD _IOW(KSMIO, 0x03,\
+ struct ksm_kthread_info)
+
+
+/* ioctls for SMA fds */
+
+/*
+ * KSM_REGISTER_MEMORY_REGION - register virtual address memory area to be
+ * scanned by kvm.
+ */
+#define KSM_REGISTER_MEMORY_REGION _IOW(KSMIO, 0x20,\
+ struct ksm_memory_region)
+/*
+ * KSM_REMOVE_MEMORY_REGION - remove virtual address memory area from ksm.
+ */
+#define KSM_REMOVE_MEMORY_REGION _IO(KSMIO, 0x21)
+
+#endif
diff --git a/qemu/vl.c b/qemu/vl.c
index c52d2d7..54a9dd9 100644
--- a/qemu/vl.c
+++ b/qemu/vl.c
@@ -130,6 +130,7 @@ int main(int argc, char **argv)
#define main qemu_main
#endif /* CONFIG_COCOA */
+#include "ksm.h"
#include "hw/hw.h"
#include "hw/boards.h"
#include "hw/usb.h"
@@ -4873,6 +4874,37 @@ static void termsig_setup(void)
#endif
+static int ksm_register_memory(void)
+{
+ int fd;
+ int ksm_fd;
+ int r = 1;
+ struct ksm_memory_region ksm_region;
+
+ fd = open("/dev/ksm", O_RDWR | O_TRUNC, (mode_t)0600);
+ if (fd == -1)
+ goto out;
+
+ ksm_fd = ioctl(fd, KSM_CREATE_SHARED_MEMORY_AREA);
+ if (ksm_fd == -1)
+ goto out_free;
+
+ ksm_region.npages = phys_ram_size / TARGET_PAGE_SIZE;
+ ksm_region.addr = (unsigned long)phys_ram_base;
+ r = ioctl(ksm_fd, KSM_REGISTER_MEMORY_REGION, &ksm_region);
+ if (r)
+ goto out_free1;
+
+ return r;
+
+out_free1:
+ close(ksm_fd);
+out_free:
+ close(fd);
+out:
+ return r;
+}
+
int main(int argc, char **argv, char **envp)
{
#ifdef CONFIG_GDBSTUB
@@ -5862,6 +5894,8 @@ int main(int argc, char **argv, char **envp)
/* init the dynamic translator */
cpu_exec_init_all(tb_size * 1024 * 1024);
+ ksm_register_memory();
+
bdrv_init();
dma_helper_init();
--
1.5.6.5
userspace tool to control the ksm kernel thread
Signed-off-by: Izik Eidus <[email protected]>
---
user/Makefile | 6 +++-
user/config-x86-common.mak | 2 +-
user/ksmctl.c | 69 ++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 75 insertions(+), 2 deletions(-)
create mode 100644 user/ksmctl.c
diff --git a/user/Makefile b/user/Makefile
index cf7f8ed..a291b37 100644
--- a/user/Makefile
+++ b/user/Makefile
@@ -39,6 +39,10 @@ autodepend-flags = -MMD -MF $(dir $*).$(notdir $*).d
LDFLAGS += -pthread -lrt
+ksmctl_objs= ksmctl.o
+ksmctl: $(ksmctl_objs)
+ $(CC) $(LDFLAGS) $^ -o $@
+
kvmtrace_objs= kvmtrace.o
kvmctl: $(kvmctl_objs)
@@ -56,4 +60,4 @@ $(libcflat): $(cflatobjs)
-include .*.d
clean: arch_clean
- $(RM) kvmctl kvmtrace *.o *.a .*.d $(libcflat) $(cflatobjs)
+ $(RM) ksmctl kvmctl kvmtrace *.o *.a .*.d $(libcflat) $(cflatobjs)
diff --git a/user/config-x86-common.mak b/user/config-x86-common.mak
index e789fd4..4303aee 100644
--- a/user/config-x86-common.mak
+++ b/user/config-x86-common.mak
@@ -1,6 +1,6 @@
#This is a make file with common rules for both x86 & x86-64
-all: kvmctl kvmtrace test_cases
+all: ksmctl kvmctl kvmtrace test_cases
kvmctl_objs= main.o iotable.o ../libkvm/libkvm.a
balloon_ctl: balloon_ctl.o
diff --git a/user/ksmctl.c b/user/ksmctl.c
new file mode 100644
index 0000000..034469f
--- /dev/null
+++ b/user/ksmctl.c
@@ -0,0 +1,69 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/ioctl.h>
+#include <fcntl.h>
+#include <sys/mman.h>
+#include <unistd.h>
+#include "../qemu/ksm.h"
+
+int main(int argc, char *argv[])
+{
+ int fd;
+ int used = 0;
+ int fd_start;
+ struct ksm_kthread_info info;
+
+
+ if (argc < 2) {
+ fprintf(stderr, "usage: %s {start npages sleep | stop | info}\n", argv[0]);
+ exit(1);
+ }
+
+ fd = open("/dev/ksm", O_RDWR | O_TRUNC, (mode_t)0600);
+ if (fd == -1) {
+ fprintf(stderr, "could not open /dev/ksm\n");
+ exit(1);
+ }
+
+ if (!strncmp(argv[1], "start", strlen(argv[1]))) {
+ used = 1;
+ if (argc < 4) {
+ fprintf(stderr,
+ "usage: %s start npages_to_scan sleep\n",
+ argv[0]);
+ exit(1);
+ }
+ info.pages_to_scan = atoi(argv[2]);
+ info.sleep = atoi(argv[3]);
+ info.flags = ksm_control_flags_run;
+
+ fd_start = ioctl(fd, KSM_START_STOP_KTHREAD, &info);
+ if (fd_start == -1) {
+ fprintf(stderr, "KSM_START_KTHREAD failed\n");
+ exit(1);
+ }
+ printf("created scanner\n");
+ }
+
+ if (!strncmp(argv[1], "stop", strlen(argv[1]))) {
+ used = 1;
+ info.flags = 0;
+ fd_start = ioctl(fd, KSM_START_STOP_KTHREAD, &info);
+ printf("stopped scanner\n");
+ }
+
+ if (!strncmp(argv[1], "info", strlen(argv[1]))) {
+ used = 1;
+ ioctl(fd, KSM_GET_INFO_KTHREAD, &info);
+ printf("flags %d, pages_to_scan %d, sleep_time %d\n",
+ info.flags, info.pages_to_scan, info.sleep);
+ }
+
+ if (!used)
+ fprintf(stderr, "unknown command %s\n", argv[1]);
+
+ return 0;
+}
--
1.5.6.5