From nobody Tue Nov 5 19:52:08 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=1694663640; cv=none; d=zohomail.com; s=zohoarc; b=SiXtO+tq0JS9pQnr5YLvf83VUjQE/rmV3WA01vK+PBCCFy3aiibFZ211lhxECDMVE9iZifZPU7am0Qz5Rt/EEs8n7pIQv9aAxxOVCwtbrJZdND4y+AQ3rctNcZqLxUJyqy1WpQS70V8qZ+HBISYwjXwzIpNlpQgUylWkoRPsvCo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1694663640; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=TnohJQA2A6mcY0fE8fJZlec9Dwm72UmlV4vWRlAlk3c=; b=FSQ9Tf3cDoYkQOiY2yKvFQnh0iQeoGV7nfbrD2Mt8ssHdgqbGNKc2jLk/W17Xs7ny1vBnxMr6aioQ8aVV0QMX1E2J0j4+rAbCo7gAtBNAUfu8rUVJkb2OdYMFv0JTAu/cIMNknmXwktnRuLpd8YRoiSLhNTIuLULrCYypv7xGD8= 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 1694663640759537.6732939389703; Wed, 13 Sep 2023 20:54:00 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qgdOQ-0005nN-GO; Wed, 13 Sep 2023 23:51:50 -0400 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 1qgdOP-0005mr-L0 for qemu-devel@nongnu.org; Wed, 13 Sep 2023 23:51:49 -0400 Received: from mgamail.intel.com ([134.134.136.24]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qgdON-0000r7-Hn for qemu-devel@nongnu.org; Wed, 13 Sep 2023 23:51:49 -0400 Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by orsmga102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Sep 2023 20:51:46 -0700 Received: from lxy-clx-4s.sh.intel.com ([10.239.48.52]) by fmsmga004.fm.intel.com with ESMTP; 13 Sep 2023 20:51:41 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1694663507; x=1726199507; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=/jXG+nYWes+m4u3i/upIK6DDfXg0VKkiaU2dn2txLpQ=; b=S2u4G2034YW1GEIPGzruJpxc4ll9Yme6W4/Hu7ORwy39v60cA1D7IMlv UWb3A1E5FRjyuW8lD14RcbL7K514RAWZsdNpfQYyvUKpxuGeVKzeN5VCX 3xQ4UDeuWkkH+sTTir1ZJYfG2lDEeBDQr3qqY2wvoGxseP1y9ChvC6J72 4coWh3vQoqgnhW+emSUM/qt4+WHLSXueSbu8rK4imxNdIPz/87zh3MCVq xO3Si4dOg/z9Hqj+hEeLOLUnyVs9nqNkEc1aayPGzXLPsbuQrRgDKfzyZ uwaHzBB+Z3gOoab/9YpXOS/QSjks0dWmfXwPlp8Zc48i4S1kaWq6BERQp A==; X-IronPort-AV: E=McAfee;i="6600,9927,10832"; a="381528348" X-IronPort-AV: E=Sophos;i="6.02,144,1688454000"; d="scan'208";a="381528348" X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10832"; a="814500567" X-IronPort-AV: E=Sophos;i="6.02,144,1688454000"; d="scan'208";a="814500567" 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=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 , isaku.yamahata@gmail.com, Sean Christopherson , Claudio Fontana Subject: [RFC PATCH v2 05/21] kvm: Enable KVM_SET_USER_MEMORY_REGION2 for memslot Date: Wed, 13 Sep 2023 23:51:01 -0400 Message-Id: <20230914035117.3285885-6-xiaoyao.li@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230914035117.3285885-1-xiaoyao.li@intel.com> References: <20230914035117.3285885-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=134.134.136.24; envelope-from=xiaoyao.li@intel.com; helo=mgamail.intel.com X-Spam_score_int: -43 X-Spam_score: -4.4 X-Spam_bar: ---- X-Spam_report: (-4.4 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, HK_RANDOM_ENVFROM=0.001, HK_RANDOM_FROM=0.001, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham 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: 1694663642612100002 Content-Type: text/plain; charset="utf-8" From: Chao Peng Switch to KVM_SET_USER_MEMORY_REGION2 when supported by KVM. With KVM_SET_USER_MEMORY_REGION2, QEMU can set up memory region that backend'ed both by hva-based shared memory and gmem fd based private memory. Signed-off-by: Chao Peng Codeveloped-by: Xiaoyao Li Signed-off-by: Xiaoyao Li --- accel/kvm/kvm-all.c | 55 ++++++++++++++++++++++++++++++++++------ accel/kvm/trace-events | 2 +- include/sysemu/kvm_int.h | 2 ++ 3 files changed, 50 insertions(+), 9 deletions(-) diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c index 185ae16d9620..91cee0878366 100644 --- a/accel/kvm/kvm-all.c +++ b/accel/kvm/kvm-all.c @@ -288,35 +288,68 @@ int kvm_physical_memory_addr_from_host(KVMState *s, v= oid *ram, static int kvm_set_user_memory_region(KVMMemoryListener *kml, KVMSlot *slo= t, bool new) { KVMState *s =3D kvm_state; - struct kvm_userspace_memory_region mem; + struct kvm_userspace_memory_region2 mem; + static int cap_user_memory2 =3D -1; int ret; =20 + if (cap_user_memory2 =3D=3D -1) { + cap_user_memory2 =3D kvm_check_extension(s, KVM_CAP_USER_MEMORY2); + } + + if (!cap_user_memory2 && slot->gmem_fd >=3D 0) { + error_report("%s, KVM doesn't support gmem!", __func__); + exit(1); + } + mem.slot =3D slot->slot | (kml->as_id << 16); mem.guest_phys_addr =3D slot->start_addr; mem.userspace_addr =3D (unsigned long)slot->ram; mem.flags =3D slot->flags; + mem.gmem_fd =3D slot->gmem_fd; + mem.gmem_offset =3D slot->ofs; =20 if (slot->memory_size && !new && (mem.flags ^ slot->old_flags) & KVM_M= EM_READONLY) { /* Set the slot size to 0 before setting the slot to the desired * value. This is needed based on KVM commit 75d61fbc. */ mem.memory_size =3D 0; - ret =3D kvm_vm_ioctl(s, KVM_SET_USER_MEMORY_REGION, &mem); + + if (cap_user_memory2) { + ret =3D kvm_vm_ioctl(s, KVM_SET_USER_MEMORY_REGION2, &mem); + } else { + ret =3D kvm_vm_ioctl(s, KVM_SET_USER_MEMORY_REGION, &mem); + } if (ret < 0) { goto err; } } mem.memory_size =3D slot->memory_size; - ret =3D kvm_vm_ioctl(s, KVM_SET_USER_MEMORY_REGION, &mem); + if (cap_user_memory2) { + ret =3D kvm_vm_ioctl(s, KVM_SET_USER_MEMORY_REGION2, &mem); + } else { + ret =3D kvm_vm_ioctl(s, KVM_SET_USER_MEMORY_REGION, &mem); + } slot->old_flags =3D mem.flags; err: trace_kvm_set_user_memory(mem.slot >> 16, (uint16_t)mem.slot, mem.flag= s, mem.guest_phys_addr, mem.memory_size, - mem.userspace_addr, ret); + mem.userspace_addr, mem.gmem_fd, + mem.gmem_offset, ret); if (ret < 0) { - error_report("%s: KVM_SET_USER_MEMORY_REGION failed, slot=3D%d," - " start=3D0x%" PRIx64 ", size=3D0x%" PRIx64 ": %s", - __func__, mem.slot, slot->start_addr, - (uint64_t)mem.memory_size, strerror(errno)); + if (cap_user_memory2) { + error_report("%s: KVM_SET_USER_MEMORY_REGION2 failed, slot= =3D%d," + " start=3D0x%" PRIx64 ", size=3D0x%" PRIx64 "," + " flags=3D0x%" PRIx32 "," + " gmem_fd=3D%" PRId32 ", gmem_offset=3D0x%" PRIx64= ": %s", + __func__, mem.slot, slot->start_addr, + (uint64_t)mem.memory_size, mem.flags, + mem.gmem_fd, (uint64_t)mem.gmem_offset, + strerror(errno)); + } else { + error_report("%s: KVM_SET_USER_MEMORY_REGION failed, slot= =3D%d," + " start=3D0x%" PRIx64 ", size=3D0x%" PRIx64 ":= %s", + __func__, mem.slot, slot->start_addr, + (uint64_t)mem.memory_size, strerror(errno)); + } } return ret; } @@ -472,6 +505,9 @@ static int kvm_mem_flags(MemoryRegion *mr) if (readonly && kvm_readonly_mem_allowed) { flags |=3D KVM_MEM_READONLY; } + if (memory_region_has_gmem_fd(mr)) { + flags |=3D KVM_MEM_PRIVATE; + } return flags; } =20 @@ -1402,6 +1438,9 @@ static void kvm_set_phys_mem(KVMMemoryListener *kml, mem->ram_start_offset =3D ram_start_offset; mem->ram =3D ram; mem->flags =3D kvm_mem_flags(mr); + mem->gmem_fd =3D mr->ram_block->gmem_fd; + mem->ofs =3D (uint8_t*)ram - mr->ram_block->host; + kvm_slot_init_dirty_bitmap(mem); err =3D kvm_set_user_memory_region(kml, mem, true); if (err) { diff --git a/accel/kvm/trace-events b/accel/kvm/trace-events index 14ebfa1b991c..80694683acea 100644 --- a/accel/kvm/trace-events +++ b/accel/kvm/trace-events @@ -15,7 +15,7 @@ kvm_irqchip_update_msi_route(int virq) "Updating MSI rout= e virq=3D%d" kvm_irqchip_release_virq(int virq) "virq %d" kvm_set_ioeventfd_mmio(int fd, uint64_t addr, uint32_t val, bool assign, u= int32_t size, bool datamatch) "fd: %d @0x%" PRIx64 " val=3D0x%x assign: %d = size: %d match: %d" kvm_set_ioeventfd_pio(int fd, uint16_t addr, uint32_t val, bool assign, ui= nt32_t size, bool datamatch) "fd: %d @0x%x val=3D0x%x assign: %d size: %d m= atch: %d" -kvm_set_user_memory(uint16_t as, uint16_t slot, uint32_t flags, uint64_t g= uest_phys_addr, uint64_t memory_size, uint64_t userspace_addr, int ret) "Ad= drSpace#%d Slot#%d flags=3D0x%x gpa=3D0x%"PRIx64 " size=3D0x%"PRIx64 " ua= =3D0x%"PRIx64 " ret=3D%d" +kvm_set_user_memory(uint16_t as, uint16_t slot, uint32_t flags, uint64_t g= uest_phys_addr, uint64_t memory_size, uint64_t userspace_addr, uint32_t fd,= uint64_t fd_offset, int ret) "AddrSpace#%d Slot#%d flags=3D0x%x gpa=3D0x%"= PRIx64 " size=3D0x%"PRIx64 " ua=3D0x%"PRIx64 " gmem_fd=3D%d" " gmem_fd_offs= et=3D0x%" PRIx64 " ret=3D%d" kvm_clear_dirty_log(uint32_t slot, uint64_t start, uint32_t size) "slot#%"= PRId32" start 0x%"PRIx64" size 0x%"PRIx32 kvm_resample_fd_notify(int gsi) "gsi %d" kvm_dirty_ring_full(int id) "vcpu %d" diff --git a/include/sysemu/kvm_int.h b/include/sysemu/kvm_int.h index 511b42bde5c4..9990889d3494 100644 --- a/include/sysemu/kvm_int.h +++ b/include/sysemu/kvm_int.h @@ -30,6 +30,8 @@ typedef struct KVMSlot int as_id; /* Cache of the offset in ram address space */ ram_addr_t ram_start_offset; + int gmem_fd; + hwaddr ofs; } KVMSlot; =20 typedef struct KVMMemoryUpdate { --=20 2.34.1