From: Andre Przywara Subject: [PATCH 3/3] NUMA: realize NUMA memory pinning Date: Wed, 23 Jun 2010 23:09:37 +0200 Message-ID: <1277327377-29629-4-git-send-email-andre.przywara@amd.com> References: <1277327377-29629-1-git-send-email-andre.przywara@amd.com> Mime-Version: 1.0 Content-Type: text/plain Cc: , , Andre Przywara To: Return-path: Received: from outbound-va3.frontbridge.com ([216.32.180.16]:40991 "EHLO VA3EHSOBE009.bigfish.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753335Ab0FWV0K (ORCPT ); Wed, 23 Jun 2010 17:26:10 -0400 In-Reply-To: <1277327377-29629-1-git-send-email-andre.przywara@amd.com> Sender: kvm-owner@vger.kernel.org List-ID: According to the user-provided assignment bind the respective part of the guest's memory to the given host node. This uses Linux' libnuma interface to realize the pinning right after the allocation. Failures are not fatal, but produce a warning. Signed-off-by: Andre Przywara --- hw/pc.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 51 insertions(+), 0 deletions(-) diff --git a/hw/pc.c b/hw/pc.c index 1f61609..b6d4d7a 100644 --- a/hw/pc.c +++ b/hw/pc.c @@ -41,6 +41,11 @@ #include "device-assignment.h" #include "kvm.h" +#ifdef CONFIG_NUMA +#include +#include +#endif + /* output Bochs bios info messages */ //#define DEBUG_BIOS @@ -874,6 +879,49 @@ void pc_cpus_init(const char *cpu_model) } } +static void bind_numa(ram_addr_t ram_addr, ram_addr_t border_4g, + int below_4g) +{ +#ifdef CONFIG_NUMA + int i, skip; + char* ram_ptr; + nodemask_t nodemask; + ram_addr_t len, ram_offset; + + ram_ptr = qemu_get_ram_ptr(ram_addr); + + ram_offset = 0; + skip = !below_4g; + for (i = 0; i < nb_numa_nodes; i++) { + len = node_mem[i]; + if (ram_offset <= border_4g && ram_offset + len > border_4g) { + len = border_4g - ram_offset; + if (skip) { + ram_offset = 0; + len = node_mem[i] - len; + skip = 0; + } + } + if (skip && ram_offset + len <= border_4g) { + ram_offset += len; + continue; + } + if (!skip && node_pin[i] >= 0) { + nodemask_zero(&nodemask); + nodemask_set_compat(&nodemask, node_pin[i]); + if (mbind(ram_ptr + ram_offset, len, MPOL_BIND, + nodemask.n, NUMA_NUM_NODES, 0)) { + perror("mbind"); + } + } + ram_offset += len; + if (below_4g && ram_offset >= border_4g) + return; + } +#endif + return; +} + void pc_memory_init(ram_addr_t ram_size, const char *kernel_filename, const char *kernel_cmdline, @@ -906,6 +954,8 @@ void pc_memory_init(ram_addr_t ram_size, below_4g_mem_size - 0x100000, ram_addr + 0x100000); + bind_numa(ram_addr, below_4g_mem_size, 1); + /* above 4giga memory allocation */ if (above_4g_mem_size > 0) { #if TARGET_PHYS_ADDR_BITS == 32 @@ -915,6 +965,7 @@ void pc_memory_init(ram_addr_t ram_size, cpu_register_physical_memory(0x100000000ULL, above_4g_mem_size, ram_addr); + bind_numa(ram_addr, below_4g_mem_size, 0); #endif } -- 1.6.4