From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id DF381D49232 for ; Fri, 12 Dec 2025 15:09:41 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1vU4hu-0002Ff-GX; Fri, 12 Dec 2025 10:05:22 -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 1vU4hi-00028w-Q1 for qemu-devel@nongnu.org; Fri, 12 Dec 2025 10:05:11 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1vU4hg-0005z5-SU for qemu-devel@nongnu.org; Fri, 12 Dec 2025 10:05:10 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1765551908; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=EWbJ3b/FRgtMaf7F/qkvMf2cvadC5nmdaEsA8MbTH9w=; b=VpMhkquGSj1NKVMbN3IrD4Tr7eKzcQ8fqtFsoVn5tOIsJ0z/15bqyf2KaxYWFhZveJOFFd aoNcwYVV9dSSEp1vDpnptxjY9aNH40K3eDgQCgdbsXrEpb3wIphzbreXUUgFCQShymQPtx 3tgIwyMVux16PLh8hxK4RxlF5BBo/cQ= Received: from mail-pl1-f199.google.com (mail-pl1-f199.google.com [209.85.214.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-37-KeyOaELwOcyYN4Zp6P7krA-1; Fri, 12 Dec 2025 10:05:01 -0500 X-MC-Unique: KeyOaELwOcyYN4Zp6P7krA-1 X-Mimecast-MFC-AGG-ID: KeyOaELwOcyYN4Zp6P7krA_1765551901 Received: by mail-pl1-f199.google.com with SMTP id d9443c01a7336-2982b47ce35so15036145ad.2 for ; Fri, 12 Dec 2025 07:05:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1765551901; x=1766156701; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=EWbJ3b/FRgtMaf7F/qkvMf2cvadC5nmdaEsA8MbTH9w=; b=Y30FlYf5NNs3FeKvgbZqKsXMHSF47aYgZyeF3QJZJjjEyHlBlS3eAdT42xgitGcalZ jHYPHMT6PzqI7O5v2jZADUHYHmvNROEZk22ER0QEWGOn2OcXE1VVW4QUfgpWImXVqDqJ HA6pnWuzscywqF92u6scTEUY0O20UfNB9K4OjdGwrEQw3NFps98OO2BTO+uf3cen8WCp QM11oGztuFVFROkNj6lUFMauA0eyDAiKYS1XE2OP5G7Y+t8EHPTKNHFtzEfArdkMJzQP ntv/isB19RFU5F9XcSJlkKrwSOv09MaLl/XnzeIvKXpIGUkvWvUS1d3gsF+IvrVN8xzT Qbkw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1765551901; x=1766156701; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=EWbJ3b/FRgtMaf7F/qkvMf2cvadC5nmdaEsA8MbTH9w=; b=W4zkXYq7gRd/ZI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= X-Received: by 2002:a17:903:22c4:b0:29e:1415:df95 with SMTP id d9443c01a7336-29f2435fd39mr19701815ad.51.1765551900653; Fri, 12 Dec 2025 07:05:00 -0800 (PST) X-Google-Smtp-Source: AGHT+IGeXsVbGlfUiHlR+wSUHOyHm6iD/qWLG4BYVCfCmsPMHGb4JdQGK7Enc4RBbFandqHGXmbZ/g== X-Received: by 2002:a17:903:22c4:b0:29e:1415:df95 with SMTP id d9443c01a7336-29f2435fd39mr19701405ad.51.1765551899951; Fri, 12 Dec 2025 07:04:59 -0800 (PST) Received: from rhel9-box.lan ([122.172.173.62]) by smtp.googlemail.com with ESMTPSA id d9443c01a7336-29ee9d38ad1sm57046655ad.29.2025.12.12.07.04.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Dec 2025 07:04:59 -0800 (PST) From: Ani Sinha To: Paolo Bonzini Cc: vkuznets@redhat.com, kraxel@redhat.com, qemu-devel@nongnu.org, Ani Sinha , kvm@vger.kernel.org Subject: [PATCH v1 11/28] accel/kvm: rebind current VCPUs to the new KVM VM file descriptor upon reset Date: Fri, 12 Dec 2025 20:33:39 +0530 Message-ID: <20251212150359.548787-12-anisinha@redhat.com> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20251212150359.548787-1-anisinha@redhat.com> References: <20251212150359.548787-1-anisinha@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=170.10.129.124; envelope-from=anisinha@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 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, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_PASS=-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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Confidential guests needs to generate a new KVM file descriptor upon virtual machine reset. Existing VCPUs needs to be reattached to this new KVM VM file descriptor. As a part of this, new VCPU file descriptors against this new KVM VM file descriptor needs to be created and re-initialized. Resources allocated against the old VCPU fds needs to be released. This change makes this happen. Signed-off-by: Ani Sinha --- accel/kvm/kvm-all.c | 201 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 166 insertions(+), 35 deletions(-) diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c index 5b854c9866..638f193626 100644 --- a/accel/kvm/kvm-all.c +++ b/accel/kvm/kvm-all.c @@ -130,6 +130,12 @@ static NotifierWithReturnList register_vmfd_changed_notifiers = static NotifierWithReturnList register_vmfd_pre_change_notifiers = NOTIFIER_WITH_RETURN_LIST_INITIALIZER(register_vmfd_pre_change_notifiers); +static int kvm_rebind_vcpus(Error **errp); + +static int map_kvm_run(KVMState *s, CPUState *cpu, Error **errp); +static int map_kvm_dirty_gfns(KVMState *s, CPUState *cpu, Error **errp); +static int vcpu_unmap_regions(KVMState *s, CPUState *cpu); + struct KVMResampleFd { int gsi; EventNotifier *resample_event; @@ -423,6 +429,82 @@ err: return ret; } +static int kvm_rebind_vcpus(Error **errp) +{ + CPUState *cpu; + unsigned long vcpu_id; + KVMState *s = kvm_state; + int kvm_fd, ret = 0; + + CPU_FOREACH(cpu) { + vcpu_id = kvm_arch_vcpu_id(cpu); + + if (cpu->kvm_fd) { + close(cpu->kvm_fd); + } + + ret = kvm_arch_destroy_vcpu(cpu); + if (ret < 0) { + goto err; + } + + if (s->coalesced_mmio_ring == (void *)cpu->kvm_run + PAGE_SIZE) { + s->coalesced_mmio_ring = NULL; + } + + ret = vcpu_unmap_regions(s, cpu); + if (ret < 0) { + goto err; + } + + ret = kvm_arch_pre_create_vcpu(cpu, errp); + if (ret < 0) { + goto err; + } + + kvm_fd = kvm_vm_ioctl(s, KVM_CREATE_VCPU, vcpu_id); + if (kvm_fd < 0) { + error_report("KVM_CREATE_VCPU IOCTL failed for vCPU %lu (%s)", + vcpu_id, strerror(kvm_fd)); + return kvm_fd; + } + + cpu->kvm_fd = kvm_fd; + + cpu->vcpu_dirty = false; + cpu->dirty_pages = 0; + cpu->throttle_us_per_full = 0; + + ret = map_kvm_run(s, cpu, errp); + if (ret < 0) { + goto err; + } + + if (s->kvm_dirty_ring_size) { + ret = map_kvm_dirty_gfns(s, cpu, errp); + if (ret < 0) { + goto err; + } + } + + ret = kvm_arch_init_vcpu(cpu); + if (ret < 0) { + error_setg_errno(errp, -ret, + "kvm_init_vcpu: kvm_arch_init_vcpu failed (%lu)", + vcpu_id); + } + + close(cpu->kvm_vcpu_stats_fd); + cpu->kvm_vcpu_stats_fd = kvm_vcpu_ioctl(cpu, KVM_GET_STATS_FD, NULL); + kvm_init_cpu_signals(cpu); + + kvm_cpu_synchronize_post_init(cpu); + } + + err: + return ret; +} + static void kvm_park_vcpu(CPUState *cpu) { struct KVMParkedVcpu *vcpu; @@ -511,19 +593,11 @@ int kvm_create_and_park_vcpu(CPUState *cpu) return ret; } -static int do_kvm_destroy_vcpu(CPUState *cpu) +static int vcpu_unmap_regions(KVMState *s, CPUState *cpu) { - KVMState *s = kvm_state; int mmap_size; int ret = 0; - trace_kvm_destroy_vcpu(cpu->cpu_index, kvm_arch_vcpu_id(cpu)); - - ret = kvm_arch_destroy_vcpu(cpu); - if (ret < 0) { - goto err; - } - mmap_size = kvm_ioctl(s, KVM_GET_VCPU_MMAP_SIZE, 0); if (mmap_size < 0) { ret = mmap_size; @@ -551,39 +625,47 @@ static int do_kvm_destroy_vcpu(CPUState *cpu) cpu->kvm_dirty_gfns = NULL; } - kvm_park_vcpu(cpu); -err: + err: return ret; } -void kvm_destroy_vcpu(CPUState *cpu) -{ - if (do_kvm_destroy_vcpu(cpu) < 0) { - error_report("kvm_destroy_vcpu failed"); - exit(EXIT_FAILURE); - } -} - -int kvm_init_vcpu(CPUState *cpu, Error **errp) +static int do_kvm_destroy_vcpu(CPUState *cpu) { KVMState *s = kvm_state; - int mmap_size; - int ret; + int ret = 0; - trace_kvm_init_vcpu(cpu->cpu_index, kvm_arch_vcpu_id(cpu)); + trace_kvm_destroy_vcpu(cpu->cpu_index, kvm_arch_vcpu_id(cpu)); - ret = kvm_arch_pre_create_vcpu(cpu, errp); + ret = kvm_arch_destroy_vcpu(cpu); if (ret < 0) { goto err; } - ret = kvm_create_vcpu(cpu); + /* If I am the CPU that created coalesced_mmio_ring, then discard it */ + if (s->coalesced_mmio_ring == (void *)cpu->kvm_run + PAGE_SIZE) { + s->coalesced_mmio_ring = NULL; + } + + ret = vcpu_unmap_regions(s, cpu); if (ret < 0) { - error_setg_errno(errp, -ret, - "kvm_init_vcpu: kvm_create_vcpu failed (%lu)", - kvm_arch_vcpu_id(cpu)); goto err; } + kvm_park_vcpu(cpu); +err: + return ret; +} + +void kvm_destroy_vcpu(CPUState *cpu) +{ + if (do_kvm_destroy_vcpu(cpu) < 0) { + error_report("kvm_destroy_vcpu failed"); + exit(EXIT_FAILURE); + } +} + +static int map_kvm_run(KVMState *s, CPUState *cpu, Error **errp) +{ + int mmap_size, ret = 0; mmap_size = kvm_ioctl(s, KVM_GET_VCPU_MMAP_SIZE, 0); if (mmap_size < 0) { @@ -608,14 +690,53 @@ int kvm_init_vcpu(CPUState *cpu, Error **errp) (void *)cpu->kvm_run + s->coalesced_mmio * PAGE_SIZE; } + err: + return ret; +} + +static int map_kvm_dirty_gfns(KVMState *s, CPUState *cpu, Error **errp) +{ + int ret = 0; + /* Use MAP_SHARED to share pages with the kernel */ + cpu->kvm_dirty_gfns = mmap(NULL, s->kvm_dirty_ring_bytes, + PROT_READ | PROT_WRITE, MAP_SHARED, + cpu->kvm_fd, + PAGE_SIZE * KVM_DIRTY_LOG_PAGE_OFFSET); + if (cpu->kvm_dirty_gfns == MAP_FAILED) { + ret = -errno; + } + + return ret; +} + +int kvm_init_vcpu(CPUState *cpu, Error **errp) +{ + KVMState *s = kvm_state; + int ret; + + trace_kvm_init_vcpu(cpu->cpu_index, kvm_arch_vcpu_id(cpu)); + + ret = kvm_arch_pre_create_vcpu(cpu, errp); + if (ret < 0) { + goto err; + } + + ret = kvm_create_vcpu(cpu); + if (ret < 0) { + error_setg_errno(errp, -ret, + "kvm_init_vcpu: kvm_create_vcpu failed (%lu)", + kvm_arch_vcpu_id(cpu)); + goto err; + } + + ret = map_kvm_run(s, cpu, errp); + if (ret < 0) { + goto err; + } + if (s->kvm_dirty_ring_size) { - /* Use MAP_SHARED to share pages with the kernel */ - cpu->kvm_dirty_gfns = mmap(NULL, s->kvm_dirty_ring_bytes, - PROT_READ | PROT_WRITE, MAP_SHARED, - cpu->kvm_fd, - PAGE_SIZE * KVM_DIRTY_LOG_PAGE_OFFSET); - if (cpu->kvm_dirty_gfns == MAP_FAILED) { - ret = -errno; + ret = map_kvm_dirty_gfns(s, cpu, errp); + if (ret < 0) { goto err; } } @@ -2716,6 +2837,16 @@ static int kvm_reset_vmfd(MachineState *ms) } assert(!err); + /* + * rebind new vcpu fds with the new kvm fds + * These can only be called after kvm_arch_vmfd_change_ops() + */ + ret = kvm_rebind_vcpus(&err); + if (ret < 0) { + return ret; + } + assert(!err); + /* these can be only called after ram_block_rebind() */ memory_listener_register(&kml->listener, &address_space_memory); memory_listener_register(&kvm_io_listener, &address_space_io); -- 2.42.0