public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
From: Marcelo Tosatti <marcelo@kvack.org>
To: Avi Kivity <avi@qumranet.com>
Cc: kvm-devel <kvm-devel@lists.sourceforge.net>
Subject: [PATCH] QEMU/KVM: large page support
Date: Sat, 23 Feb 2008 11:48:12 -0300	[thread overview]
Message-ID: <20080223144812.GB1040@dmt> (raw)


Add an option so the user can specify the hugetlbfs mounted path, with
fallback to 4k pages on error.

Align the 4GB+ memslot on large page boundary.

Signed-off-by: Marcelo Tosatti <mtosatti@redhat.com>

Index: kvm-userspace.gitpara/qemu/hw/pc.c
===================================================================
--- kvm-userspace.gitpara.orig/qemu/hw/pc.c
+++ kvm-userspace.gitpara/qemu/hw/pc.c
@@ -854,6 +854,15 @@ static void pc_init1(ram_addr_t ram_size
     /* above 4giga memory allocation */
     if (above_4g_mem_size > 0) {
         ram_addr = qemu_ram_alloc(above_4g_mem_size);
+	if (hpagesize) {
+		if (ram_addr & (hpagesize-1)) {
+			unsigned long aligned_addr;
+			aligned_addr = (ram_addr + hpagesize - 1) &
+					 ~(hpagesize-1);
+			qemu_ram_alloc(aligned_addr - ram_addr);
+			ram_addr = aligned_addr;
+		}
+	}
         cpu_register_physical_memory(0x100000000, above_4g_mem_size, ram_addr);
 
 	if (kvm_enabled())
Index: kvm-userspace.gitpara/qemu/sysemu.h
===================================================================
--- kvm-userspace.gitpara.orig/qemu/sysemu.h
+++ kvm-userspace.gitpara/qemu/sysemu.h
@@ -99,6 +99,7 @@ extern int no_quit;
 extern int semihosting_enabled;
 extern int autostart;
 extern int old_param;
+extern int hpagesize;
 extern const char *bootp_filename;
 
 
Index: kvm-userspace.gitpara/qemu/vl.c
===================================================================
--- kvm-userspace.gitpara.orig/qemu/vl.c
+++ kvm-userspace.gitpara/qemu/vl.c
@@ -237,6 +237,9 @@ int semihosting_enabled = 0;
 int autostart = 1;
 int time_drift_fix = 0;
 unsigned int kvm_shadow_memory = 0;
+const char *hugetlbpath = NULL;
+const char *hugetlbfile = NULL;
+int hpagesize = 0;
 const char *cpu_vendor_string;
 #ifdef TARGET_ARM
 int old_param = 0;
@@ -8071,6 +8074,7 @@ static void help(int exitcode)
 #endif
            "-tdf            inject timer interrupts that got lost\n"
            "-kvm-shadow-memory megs set the amount of shadow pages to be allocated\n"
+           "-hugetlb-path   set the path to hugetlbfs mounted directory, also enables allocation of guest memory with huge pages\n"
 	   "-option-rom rom load a file, rom, into the option ROM space\n"
 #ifdef TARGET_SPARC
            "-prom-env variable=value  set OpenBIOS nvram variables\n"
@@ -8189,6 +8193,7 @@ enum {
     QEMU_OPTION_incoming,
     QEMU_OPTION_tdf,
     QEMU_OPTION_kvm_shadow_memory,
+    QEMU_OPTION_hugetlbpath,
 };
 
 typedef struct QEMUOption {
@@ -8310,6 +8315,7 @@ const QEMUOption qemu_options[] = {
 #endif
     { "clock", HAS_ARG, QEMU_OPTION_clock },
     { "startdate", HAS_ARG, QEMU_OPTION_startdate },
+    { "hugetlb-path", HAS_ARG, QEMU_OPTION_hugetlbpath },
     { NULL },
 };
 
@@ -8561,6 +8567,94 @@ void qemu_get_launch_info(int *argc, cha
     *opt_incoming = incoming;
 }
 
+
+static int gethugepagesize(void)
+{ 
+	int ret, fd;
+	char buf[4096];
+	char *needle = "Hugepagesize:";
+	char *size;
+	unsigned long hugepagesize;
+
+	fd = open("/proc/meminfo", O_RDONLY);
+	if (fd < 0) {
+		perror("open");
+		exit(0);
+	}
+
+	ret = read(fd, buf, sizeof(buf));
+	if (ret < 0) {
+		perror("read");
+		exit(0);
+	}
+
+	size = strstr(buf, needle);
+	if (!size)
+		return 0;
+	size += strlen(needle);
+	hugepagesize = strtol(size, NULL, 0);
+	return hugepagesize;
+}
+
+void cleanup_hugetlb(void)
+{
+	if (hugetlbfile)
+		unlink(hugetlbfile);
+}
+
+void *alloc_huge_area(unsigned long memory, const char *path)
+{
+	void *area;
+	int fd;
+	char *filename;
+	char *tmpfile = "/kvm.XXXXXX";
+
+	filename = qemu_malloc(4096);
+	if (!filename) 
+		return NULL;
+
+	memset(filename, 0, 4096);
+	strncpy(filename, path, 4096 - strlen(tmpfile) - 1);
+	strcat(filename, tmpfile);
+	
+	hpagesize = gethugepagesize() * 1024;
+	if (!hpagesize)
+		return NULL;
+
+	mkstemp(filename);
+	fd = open(filename, O_RDWR);
+	if (fd < 0) {
+		perror("open");
+		hpagesize = 0;
+		exit(0);
+	}
+	memory = (memory+hpagesize-1) & ~(hpagesize-1);
+
+	area = mmap(0, memory, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
+	if (area == MAP_FAILED) {
+		perror("mmap");
+		hpagesize = 0;
+		exit(0);
+	}
+
+	hugetlbfile = filename;
+	atexit(cleanup_hugetlb);
+
+	return area;
+}
+
+void *qemu_alloc_physram(unsigned long memory)
+{
+	void *area = NULL;
+
+	if (hugetlbpath)
+		area = alloc_huge_area(memory, hugetlbpath);
+	if (!area)
+		area = qemu_vmalloc(memory);
+
+	return area;
+}
+
 int main(int argc, char **argv)
 {
 #ifdef CONFIG_GDBSTUB
@@ -9159,6 +9253,9 @@ int main(int argc, char **argv)
             case QEMU_OPTION_kvm_shadow_memory:
                 kvm_shadow_memory = (int64_t)atoi(optarg) * 1024 * 1024 / 4096;
                 break;
+            case QEMU_OPTION_hugetlbpath:
+		hugetlbpath = optarg;
+		break;
             case QEMU_OPTION_name:
                 qemu_name = optarg;
                 break;
@@ -9394,7 +9491,7 @@ int main(int argc, char **argv)
 
             ret = kvm_qemu_check_extension(KVM_CAP_USER_MEMORY);
             if (ret) {
-                phys_ram_base = qemu_vmalloc(phys_ram_size);
+                phys_ram_base = qemu_alloc_physram(phys_ram_size);
 	        if (!phys_ram_base) {
 		        fprintf(stderr, "Could not allocate physical memory\n");
 		        exit(1);

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/

             reply	other threads:[~2008-02-23 14:48 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-02-23 14:48 Marcelo Tosatti [this message]
2008-02-23 18:29 ` [PATCH] QEMU/KVM: large page support Anthony Liguori
2008-02-23 22:54   ` Marcelo Tosatti
2008-02-23 22:56     ` Anthony Liguori

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20080223144812.GB1040@dmt \
    --to=marcelo@kvack.org \
    --cc=avi@qumranet.com \
    --cc=kvm-devel@lists.sourceforge.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox