From nobody Thu Nov 6 20:35:58 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1487748058783957.885157876989; Tue, 21 Feb 2017 23:20:58 -0800 (PST) Received: from localhost ([::1]:50387 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cgREO-0007jc-Ew for importer@patchew.org; Wed, 22 Feb 2017 02:20:56 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:38195) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cgQVF-0003CM-0G for qemu-devel@nongnu.org; Wed, 22 Feb 2017 01:34:20 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cgQVB-0008DA-36 for qemu-devel@nongnu.org; Wed, 22 Feb 2017 01:34:16 -0500 Received: from ozlabs.org ([103.22.144.67]:35217) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1cgQVA-0008Af-Ka; Wed, 22 Feb 2017 01:34:12 -0500 Received: by ozlabs.org (Postfix, from userid 1007) id 3vSndM37Ktz9sNF; Wed, 22 Feb 2017 17:33:58 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gibson.dropbear.id.au; s=201602; t=1487745239; bh=QZ0aOKDBcD7bTJdnCzkCWdNKIB3n6Wg8LuQycpscMLk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=dPmrkG8XZedbmNAha5dDaSR99fHyk7d5f80/lfwka52gsPMTdEWzg4yggVdxN1Wvf mJBPivd/39rJUJkg9V1v2+DVbpouo4fZiRivPDWoZmX5Pv/zP5nbh5HZfar5R5g5WH SnxN+ykSEzqLN10Sr9SFtYXZIwyc12zPeNh0s734= From: David Gibson To: peter.maydell@linaro.org Date: Wed, 22 Feb 2017 17:33:47 +1100 Message-Id: <20170222063348.32176-43-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.9.3 In-Reply-To: <20170222063348.32176-1-david@gibson.dropbear.id.au> References: <20170222063348.32176-1-david@gibson.dropbear.id.au> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 103.22.144.67 Subject: [Qemu-devel] [PULL 42/43] hw/ppc/spapr: Check for valid page size when hot plugging memory X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: lvivier@redhat.com, thuth@redhat.com, qemu-devel@nongnu.org, mdroth@linux.vnet.ibm.com, agraf@suse.de, qemu-ppc@nongnu.org, imammedo@redhat.com, David Gibson Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Thomas Huth On POWER, the valid page sizes that the guest can use are bound to the CPU and not to the memory region. QEMU already has some fancy logic to find out the right maximum memory size to tell it to the guest during boot (see getrampagesize() in the file target/ppc/kvm.c for more information). However, once we're booted and the guest is using huge pages already, it is currently still possible to hot-plug memory regions that does not support huge pages - which of course does not work on POWER, since the guest thinks that it is possible to use huge pages everywhere. The KVM_RUN ioctl will then abort with -EFAULT, QEMU spills out a not very helpful error message together with a register dump and the user is annoyed that the VM unexpectedly died. To avoid this situation, we should check the page size of hot-plugged DIMMs to see whether it is possible to use it in the current VM. If it does not fit, we can print out a better error message and refuse to add it, so that the VM does not die unexpectely and the user has a second chance to plug a DIMM with a matching memory backend instead. Buglink: https://bugzilla.redhat.com/show_bug.cgi?id=3D1419466 Signed-off-by: Thomas Huth [dwg: Fix a build error on 32-bit builds with KVM] Signed-off-by: David Gibson --- hw/ppc/spapr.c | 8 ++++++++ target/ppc/kvm.c | 32 ++++++++++++++++++++++++++++---- target/ppc/kvm_ppc.h | 7 +++++++ 3 files changed, 43 insertions(+), 4 deletions(-) diff --git a/hw/ppc/spapr.c b/hw/ppc/spapr.c index 6f37288..5904e64 100644 --- a/hw/ppc/spapr.c +++ b/hw/ppc/spapr.c @@ -2367,6 +2367,7 @@ static void spapr_memory_plug(HotplugHandler *hotplug= _dev, DeviceState *dev, uint64_t align =3D memory_region_get_alignment(mr); uint64_t size =3D memory_region_size(mr); uint64_t addr; + char *mem_dev; =20 if (size % SPAPR_MEMORY_BLOCK_SIZE) { error_setg(&local_err, "Hotplugged memory size must be a multiple = of " @@ -2374,6 +2375,13 @@ static void spapr_memory_plug(HotplugHandler *hotplu= g_dev, DeviceState *dev, goto out; } =20 + mem_dev =3D object_property_get_str(OBJECT(dimm), PC_DIMM_MEMDEV_PROP,= NULL); + if (mem_dev && !kvmppc_is_mem_backend_page_size_ok(mem_dev)) { + error_setg(&local_err, "Memory backend has bad page size. " + "Use 'memory-backend-file' with correct mem-path."); + goto out; + } + pc_dimm_memory_plug(dev, &ms->hotplug_memory, mr, align, &local_err); if (local_err) { goto out; diff --git a/target/ppc/kvm.c b/target/ppc/kvm.c index 663d2e7..52bbea5 100644 --- a/target/ppc/kvm.c +++ b/target/ppc/kvm.c @@ -438,12 +438,13 @@ static bool kvm_valid_page_size(uint32_t flags, long = rampgsize, uint32_t shift) return (1ul << shift) <=3D rampgsize; } =20 +static long max_cpu_page_size; + static void kvm_fixup_page_sizes(PowerPCCPU *cpu) { static struct kvm_ppc_smmu_info smmu_info; static bool has_smmu_info; CPUPPCState *env =3D &cpu->env; - long rampagesize; int iq, ik, jq, jk; bool has_64k_pages =3D false; =20 @@ -458,7 +459,9 @@ static void kvm_fixup_page_sizes(PowerPCCPU *cpu) has_smmu_info =3D true; } =20 - rampagesize =3D getrampagesize(); + if (!max_cpu_page_size) { + max_cpu_page_size =3D getrampagesize(); + } =20 /* Convert to QEMU form */ memset(&env->sps, 0, sizeof(env->sps)); @@ -478,14 +481,14 @@ static void kvm_fixup_page_sizes(PowerPCCPU *cpu) struct ppc_one_seg_page_size *qsps =3D &env->sps.sps[iq]; struct kvm_ppc_one_seg_page_size *ksps =3D &smmu_info.sps[ik]; =20 - if (!kvm_valid_page_size(smmu_info.flags, rampagesize, + if (!kvm_valid_page_size(smmu_info.flags, max_cpu_page_size, ksps->page_shift)) { continue; } qsps->page_shift =3D ksps->page_shift; qsps->slb_enc =3D ksps->slb_enc; for (jk =3D jq =3D 0; jk < KVM_PPC_PAGE_SIZES_MAX_SZ; jk++) { - if (!kvm_valid_page_size(smmu_info.flags, rampagesize, + if (!kvm_valid_page_size(smmu_info.flags, max_cpu_page_size, ksps->enc[jk].page_shift)) { continue; } @@ -510,12 +513,33 @@ static void kvm_fixup_page_sizes(PowerPCCPU *cpu) env->mmu_model &=3D ~POWERPC_MMU_64K; } } + +bool kvmppc_is_mem_backend_page_size_ok(char *obj_path) +{ + Object *mem_obj =3D object_resolve_path(obj_path, NULL); + char *mempath =3D object_property_get_str(mem_obj, "mem-path", NULL); + long pagesize; + + if (mempath) { + pagesize =3D gethugepagesize(mempath); + } else { + pagesize =3D getpagesize(); + } + + return pagesize >=3D max_cpu_page_size; +} + #else /* defined (TARGET_PPC64) */ =20 static inline void kvm_fixup_page_sizes(PowerPCCPU *cpu) { } =20 +bool kvmppc_is_mem_backend_page_size_ok(char *obj_path) +{ + return true; +} + #endif /* !defined (TARGET_PPC64) */ =20 unsigned long kvm_arch_vcpu_id(CPUState *cpu) diff --git a/target/ppc/kvm_ppc.h b/target/ppc/kvm_ppc.h index 151c00b..8da2ee4 100644 --- a/target/ppc/kvm_ppc.h +++ b/target/ppc/kvm_ppc.h @@ -60,6 +60,8 @@ int kvmppc_enable_hwrng(void); int kvmppc_put_books_sregs(PowerPCCPU *cpu); PowerPCCPUClass *kvm_ppc_get_host_cpu_class(void); =20 +bool kvmppc_is_mem_backend_page_size_ok(char *obj_path); + #else =20 static inline uint32_t kvmppc_get_tbfreq(void) @@ -192,6 +194,11 @@ static inline uint64_t kvmppc_rma_size(uint64_t curren= t_size, return ram_size; } =20 +static inline bool kvmppc_is_mem_backend_page_size_ok(char *obj_path) +{ + return true; +} + #endif /* !CONFIG_USER_ONLY */ =20 static inline bool kvmppc_has_cap_epr(void) --=20 2.9.3