From nobody Tue Nov 26 18:22:01 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass header.i=@intel.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1706153582; cv=none; d=zohomail.com; s=zohoarc; b=ZuDBrexSgkxrQWuaMuasw/vBy1iDD3V2fm22h/kZy2V3rvFCKzrFq9rrkMXmjKSPr5fE0hdLUyiYT1Y3+yqjbyhvBHOtgjm2AokPqKp8BctTCKktZxVlmNn77yCqAGW0on2ksVAgiKwXNakF77z4WcfQ/ue290Dp1773Wd9lWgE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1706153582; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=Zw+m0cPU9OgHwHRL3a9r+c0YObRs2zroQi1W6Q9OFfg=; b=oKC82D6XLne4LHmyAWkrCWJwUolxqAz5hencthHh8uCEyWv2DAIT17AEp/O7V6z8Nnsjqgx1UQyU04grP5dX/NsV/plaxUkvSENXGpWpZ+Fzr33EJdBqSPyb9BBluIg9mTyWnGNDfhF5a7kCv5vO0uXA523AbpLH9kLUr8Gw34g= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=@intel.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1706153582815877.4173413810072; Wed, 24 Jan 2024 19:33:02 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rSqPY-00035e-RW; Wed, 24 Jan 2024 22:28:16 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rSqPY-00035K-14 for qemu-devel@nongnu.org; Wed, 24 Jan 2024 22:28:16 -0500 Received: from mgamail.intel.com ([192.198.163.10]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rSqPW-0001Dy-7S for qemu-devel@nongnu.org; Wed, 24 Jan 2024 22:28:15 -0500 Received: from orviesa005.jf.intel.com ([10.64.159.145]) by fmvoesa104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Jan 2024 19:24:21 -0800 Received: from lxy-clx-4s.sh.intel.com ([10.239.48.52]) by orviesa005.jf.intel.com with ESMTP; 24 Jan 2024 19:24:16 -0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1706153294; x=1737689294; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Uni5ljiplp5k1TOtlm3XfrP7/ANeaONuXLXuV9G5ctA=; b=hJKfV1UXsgGRao4bYHb+gkRGb3i2Kyj+bWPCxRc6+vZApBvNsoN5uNFs KtZcc8kxC+T9QKplVylDHelmH0o/Ww6adEqny8fdf26bRmczkpUggIjhR 1j3QGOAbfdoAFqLZqzQEyFYqrwjvj5ysYVL+8kxxmaSxWcPtNWyXIbVyT VbCVLIEOItRqTXl/dPb6HhPkfeYdWfNvBJEy0sV6LrFqqFWNvEs2WNDw2 5gtUn2AL9IyAOjkQtK61y8fYQpinGLLeMtoEcCLzZBMfXhuj3u5yzYb8o S0fk52IrS24EHEUQGrGaqakMFCIotW1SjK0EsB0S3pGt0m09sf0zKQveM Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10962"; a="9427999" X-IronPort-AV: E=Sophos;i="6.05,216,1701158400"; d="scan'208";a="9427999" X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.05,216,1701158400"; d="scan'208";a="2084663" From: Xiaoyao Li To: Paolo Bonzini , David Hildenbrand , Igor Mammedov , "Michael S . Tsirkin" , Marcel Apfelbaum , Richard Henderson , Peter Xu , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Cornelia Huck , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , Eric Blake , Markus Armbruster , Marcelo Tosatti Cc: qemu-devel@nongnu.org, kvm@vger.kernel.org, xiaoyao.li@intel.com, Michael Roth , Sean Christopherson , Claudio Fontana , Gerd Hoffmann , Isaku Yamahata , Chenyi Qiang Subject: [PATCH v4 08/66] kvm: handle KVM_EXIT_MEMORY_FAULT Date: Wed, 24 Jan 2024 22:22:30 -0500 Message-Id: <20240125032328.2522472-9-xiaoyao.li@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240125032328.2522472-1-xiaoyao.li@intel.com> References: <20240125032328.2522472-1-xiaoyao.li@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=192.198.163.10; envelope-from=xiaoyao.li@intel.com; helo=mgamail.intel.com X-Spam_score_int: -15 X-Spam_score: -1.6 X-Spam_bar: - X-Spam_report: (-1.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1.5, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, HK_RANDOM_ENVFROM=0.999, HK_RANDOM_FROM=1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @intel.com) X-ZM-MESSAGEID: 1706153584751100003 Content-Type: text/plain; charset="utf-8" From: Chao Peng When geeting KVM_EXIT_MEMORY_FAULT exit, it indicates userspace needs to do the memory conversion on the RAMBlock to turn the memory into desired attribute, i.e., private/shared. Currently only KVM_MEMORY_EXIT_FLAG_PRIVATE in flags is valid when KVM_EXIT_MEMORY_FAULT happens. Note, KVM_EXIT_MEMORY_FAULT makes sense only when the RAMBlock has guest_memfd memory backend. Note, KVM_EXIT_MEMORY_FAULT returns with -EFAULT, so special handling is added. When page is converted from shared to private, the original shared memory can be discarded via ram_block_discard_range(). Note, shared memory can be discarded only when it's not back'ed by hugetlb because hugetlb is supposed to be pre-allocated and no need for discarding. Signed-off-by: Chao Peng Co-developed-by: Xiaoyao Li Signed-off-by: Xiaoyao Li --- Changes in v4: - open-coded ram_block_discard logic; - change warn_report() to error_report(); (Daniel) --- accel/kvm/kvm-all.c | 94 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 84 insertions(+), 10 deletions(-) diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c index 3bc78a16b48d..2421b8a5b314 100644 --- a/accel/kvm/kvm-all.c +++ b/accel/kvm/kvm-all.c @@ -2893,6 +2893,68 @@ static void kvm_eat_signals(CPUState *cpu) } while (sigismember(&chkset, SIG_IPI)); } =20 +static int kvm_convert_memory(hwaddr start, hwaddr size, bool to_private) +{ + MemoryRegionSection section; + ram_addr_t offset; + MemoryRegion *mr; + RAMBlock *rb; + void *addr; + int ret =3D -1; + + if (!QEMU_PTR_IS_ALIGNED(start, qemu_host_page_size) || + !QEMU_PTR_IS_ALIGNED(size, qemu_host_page_size)) { + return -1; + } + + if (!size) { + return -1; + } + + section =3D memory_region_find(get_system_memory(), start, size); + mr =3D section.mr; + if (!mr) { + return -1; + } + + if (memory_region_has_guest_memfd(mr)) { + if (to_private) { + ret =3D kvm_set_memory_attributes_private(start, size); + } else { + ret =3D kvm_set_memory_attributes_shared(start, size); + } + + if (ret) { + memory_region_unref(section.mr); + return ret; + } + + addr =3D memory_region_get_ram_ptr(mr) + section.offset_within_reg= ion; + rb =3D qemu_ram_block_from_host(addr, false, &offset); + + if (to_private) { + if (rb->page_size !=3D qemu_host_page_size) { + /* + * shared memory is back'ed by hugetlb, which is supposed = to be + * pre-allocated and doesn't need to be discarded + */ + return 0; + } else { + ret =3D ram_block_discard_range(rb, offset, size); + } + } else { + ret =3D ram_block_discard_guest_memfd_range(rb, offset, size); + } + } else { + error_report("Convert non guest_memfd backed memory region " + "(0x%"HWADDR_PRIx" ,+ 0x%"HWADDR_PRIx") to %s", + start, size, to_private ? "private" : "shared"); + } + + memory_region_unref(section.mr); + return ret; +} + int kvm_cpu_exec(CPUState *cpu) { struct kvm_run *run =3D cpu->kvm_run; @@ -2960,18 +3022,20 @@ int kvm_cpu_exec(CPUState *cpu) ret =3D EXCP_INTERRUPT; break; } - fprintf(stderr, "error: kvm run failed %s\n", - strerror(-run_ret)); + if (!(run_ret =3D=3D -EFAULT && run->exit_reason =3D=3D KVM_EX= IT_MEMORY_FAULT)) { + fprintf(stderr, "error: kvm run failed %s\n", + strerror(-run_ret)); #ifdef TARGET_PPC - if (run_ret =3D=3D -EBUSY) { - fprintf(stderr, - "This is probably because your SMT is enabled.\n" - "VCPU can only run on primary threads with all " - "secondary threads offline.\n"); - } + if (run_ret =3D=3D -EBUSY) { + fprintf(stderr, + "This is probably because your SMT is enabled.= \n" + "VCPU can only run on primary threads with all= " + "secondary threads offline.\n"); + } #endif - ret =3D -1; - break; + ret =3D -1; + break; + } } =20 trace_kvm_run_exit(cpu->cpu_index, run->exit_reason); @@ -3054,6 +3118,16 @@ int kvm_cpu_exec(CPUState *cpu) break; } break; + case KVM_EXIT_MEMORY_FAULT: + if (run->memory_fault.flags & ~KVM_MEMORY_EXIT_FLAG_PRIVATE) { + error_report("KVM_EXIT_MEMORY_FAULT: Unknown flag 0x%" PRI= x64, + (uint64_t)run->memory_fault.flags); + ret =3D -1; + break; + } + ret =3D kvm_convert_memory(run->memory_fault.gpa, run->memory_= fault.size, + run->memory_fault.flags & KVM_MEMORY_= EXIT_FLAG_PRIVATE); + break; default: ret =3D kvm_arch_handle_exit(cpu, run); break; --=20 2.34.1