From nobody Tue Nov 26 18:20:04 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=1706153475; cv=none; d=zohomail.com; s=zohoarc; b=PGqLOm3kz+pcP6UovtYg7UfHfs+bY7ZkdWfnFf+onbmYz47nWfcgA2ZxSGlKYhGTdYMBvtOtt5lu0S3Bt5ZZGkZr4xWysGNHSN5dIzIPfOy4pUFLhbyVazpVEett94PvrqO1gYyTbaBqnAaB/kerJHCwqt4QfvmS/BSAKYPGarY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1706153475; 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=ACn4mhJ7PCuxaHV9UThigLwS7HcWDc/+yUbEKKOO7Os=; b=PS1mdovyvaaoE6ZsKrZVBB3fRLw86cXwaiKJf+PShKVFhSZxneWxaL8yWx7fc9JLag9ysgQcLa1jCATF730V2IEW/6FK50L/BAfq/cjkQ+otzrZToUZJJbMYXJwOVZTl8PbhIQH0BdEzxJgjgFAb/zxFr8WVi7lqaQ3KY+oN6lM= 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 1706153475109760.5284421031247; Wed, 24 Jan 2024 19:31:15 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rSqPW-00034F-SV; Wed, 24 Jan 2024 22:28:14 -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 1rSqPT-00033S-RQ for qemu-devel@nongnu.org; Wed, 24 Jan 2024 22:28:11 -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 1rSqPR-0001Dr-29 for qemu-devel@nongnu.org; Wed, 24 Jan 2024 22:28:11 -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:23:54 -0800 Received: from lxy-clx-4s.sh.intel.com ([10.239.48.52]) by orviesa005.jf.intel.com with ESMTP; 24 Jan 2024 19:23:48 -0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1706153289; x=1737689289; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=2mNwsjv6vluae8uBwx0X4bXjQ8lU44YLJ4VZgJtV40o=; b=b6cukiNilijnOl1pzcEfWLuG2DglDn0LVF2lpkeihFcbjGbj0a0gPqg/ PlEd0ziwo/otsbS0lQdyRcdcdsf1VZCMKP4yo8KgA+tFjLV8cs7w9YkpD zqcG1iScfkwBMt7ZvBtEDeczX0sXr+bsXx3HGhjhRVfHy1fiAjttwDRDi oG8bq3ck+s+7p98+fum0WDTvkOy+XW5mEGlxMYZyGUN3AH0vMP7iSGByD oxBcm7yUKNGZ5Z6qoj9JKImAxRqM/x0LWPTerPjlbkeH1yOCLT0thExuR zON5BnqrPjbKvrm2WPVLPgS5ECUb/Q9ea5XZm6fglhuFjq6riHrJzpIop A==; X-IronPort-AV: E=McAfee;i="6600,9927,10962"; a="9427927" X-IronPort-AV: E=Sophos;i="6.05,216,1701158400"; d="scan'208";a="9427927" X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.05,216,1701158400"; d="scan'208";a="2084440" 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 03/66] HostMem: Add mechanism to opt in kvm guest memfd via MachineState Date: Wed, 24 Jan 2024 22:22:25 -0500 Message-Id: <20240125032328.2522472-4-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: 1706153476599100003 Content-Type: text/plain; charset="utf-8" Add a new member "guest_memfd" to memory backends. When it's set to true, it enables RAM_GUEST_MEMFD in ram_flags, thus private kvm guest_memfd will be allocated during RAMBlock allocation. Memory backend's @guest_memfd is wired with @require_guest_memfd field of MachineState. It avoid looking up the machine in phymem.c. MachineState::require_guest_memfd is supposed to be set by any VMs that requires KVM guest memfd as private memory, e.g., TDX VM. Signed-off-by: Xiaoyao Li Reviewed-by: David Hildenbrand --- Changes in v4: - rename "require_guest_memfd" to "guest_memfd" in struct HostMemoryBackend; (David Hildenbrand) --- backends/hostmem-file.c | 1 + backends/hostmem-memfd.c | 1 + backends/hostmem-ram.c | 1 + backends/hostmem.c | 1 + hw/core/machine.c | 5 +++++ include/hw/boards.h | 2 ++ include/sysemu/hostmem.h | 1 + 7 files changed, 12 insertions(+) diff --git a/backends/hostmem-file.c b/backends/hostmem-file.c index ac3e433cbddb..3c69db794607 100644 --- a/backends/hostmem-file.c +++ b/backends/hostmem-file.c @@ -85,6 +85,7 @@ file_backend_memory_alloc(HostMemoryBackend *backend, Err= or **errp) ram_flags |=3D fb->readonly ? RAM_READONLY_FD : 0; ram_flags |=3D fb->rom =3D=3D ON_OFF_AUTO_ON ? RAM_READONLY : 0; ram_flags |=3D backend->reserve ? 0 : RAM_NORESERVE; + ram_flags |=3D backend->guest_memfd ? RAM_GUEST_MEMFD : 0; ram_flags |=3D fb->is_pmem ? RAM_PMEM : 0; ram_flags |=3D RAM_NAMED_FILE; return memory_region_init_ram_from_file(&backend->mr, OBJECT(backend),= name, diff --git a/backends/hostmem-memfd.c b/backends/hostmem-memfd.c index 3923ea9364d5..745ead0034dc 100644 --- a/backends/hostmem-memfd.c +++ b/backends/hostmem-memfd.c @@ -55,6 +55,7 @@ memfd_backend_memory_alloc(HostMemoryBackend *backend, Er= ror **errp) name =3D host_memory_backend_get_name(backend); ram_flags =3D backend->share ? RAM_SHARED : 0; ram_flags |=3D backend->reserve ? 0 : RAM_NORESERVE; + ram_flags |=3D backend->guest_memfd ? RAM_GUEST_MEMFD : 0; return memory_region_init_ram_from_fd(&backend->mr, OBJECT(backend), n= ame, backend->size, ram_flags, fd, 0,= errp); } diff --git a/backends/hostmem-ram.c b/backends/hostmem-ram.c index d121249f0f45..f7d81af783a5 100644 --- a/backends/hostmem-ram.c +++ b/backends/hostmem-ram.c @@ -30,6 +30,7 @@ ram_backend_memory_alloc(HostMemoryBackend *backend, Erro= r **errp) name =3D host_memory_backend_get_name(backend); ram_flags =3D backend->share ? RAM_SHARED : 0; ram_flags |=3D backend->reserve ? 0 : RAM_NORESERVE; + ram_flags |=3D backend->guest_memfd ? RAM_GUEST_MEMFD : 0; return memory_region_init_ram_flags_nomigrate(&backend->mr, OBJECT(bac= kend), name, backend->size, ram_flags, errp); diff --git a/backends/hostmem.c b/backends/hostmem.c index 30f69b2cb5e9..b1c39e8065e2 100644 --- a/backends/hostmem.c +++ b/backends/hostmem.c @@ -276,6 +276,7 @@ static void host_memory_backend_init(Object *obj) /* TODO: convert access to globals to compat properties */ backend->merge =3D machine_mem_merge(machine); backend->dump =3D machine_dump_guest_core(machine); + backend->guest_memfd =3D machine_require_guest_memfd(machine); backend->reserve =3D true; backend->prealloc_threads =3D machine->smp.cpus; } diff --git a/hw/core/machine.c b/hw/core/machine.c index fb5afdcae4cc..514a201dc570 100644 --- a/hw/core/machine.c +++ b/hw/core/machine.c @@ -1192,6 +1192,11 @@ bool machine_mem_merge(MachineState *machine) return machine->mem_merge; } =20 +bool machine_require_guest_memfd(MachineState *machine) +{ + return machine->require_guest_memfd; +} + static char *cpu_slot_to_string(const CPUArchId *cpu) { GString *s =3D g_string_new(NULL); diff --git a/include/hw/boards.h b/include/hw/boards.h index bcfde8a84d10..2e6156ae96b8 100644 --- a/include/hw/boards.h +++ b/include/hw/boards.h @@ -36,6 +36,7 @@ bool machine_usb(MachineState *machine); int machine_phandle_start(MachineState *machine); bool machine_dump_guest_core(MachineState *machine); bool machine_mem_merge(MachineState *machine); +bool machine_require_guest_memfd(MachineState *machine); HotpluggableCPUList *machine_query_hotpluggable_cpus(MachineState *machine= ); void machine_set_cpu_numa_node(MachineState *machine, const CpuInstanceProperties *props, @@ -370,6 +371,7 @@ struct MachineState { char *dt_compatible; bool dump_guest_core; bool mem_merge; + bool require_guest_memfd; bool usb; bool usb_disabled; char *firmware; diff --git a/include/sysemu/hostmem.h b/include/sysemu/hostmem.h index 0e411aaa29e1..04b884bf42a6 100644 --- a/include/sysemu/hostmem.h +++ b/include/sysemu/hostmem.h @@ -74,6 +74,7 @@ struct HostMemoryBackend { uint64_t size; bool merge, dump, use_canonical_path; bool prealloc, is_mapped, share, reserve; + bool guest_memfd; uint32_t prealloc_threads; ThreadContext *prealloc_context; DECLARE_BITMAP(host_nodes, MAX_NODES + 1); --=20 2.34.1