From nobody Sun Dec 14 12:37:16 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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=quarantine dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1765552182; cv=none; d=zohomail.com; s=zohoarc; b=SJswlofmZ2r+QCfr+s5L9XgbKLJYIaElUPlMU9wbQRgDMj2904F7K/F5zhYX/In/nYgUUjdxlYQ6yiO67LzKG8l6Gjafloub9D1K1TwVMpHwYhAOBXFWr8WbhmLrn2rxbB8q6Aac/G0DniiXXtKkvBtnhgfST8HRNZ7sKpLiVJc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1765552182; 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=EWbJ3b/FRgtMaf7F/qkvMf2cvadC5nmdaEsA8MbTH9w=; b=EW049U3wYVTHvHIM9TXzFk47WTXg3lpIplPBVEQ6Q+TqyV4mXA71EdPb7fax0i5FjKewIYba63+FDJOeED/grVjAzgvJd7A2foaee81mrpMWRM9hiUUkPsqWcrRjktsrRQCZiWRhTgl9BrOYXFdhreyIgcPpEI9e5Nf2CPlHGdQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 176555218205886.11929639031109; Fri, 12 Dec 2025 07:09:42 -0800 (PST) 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 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 Received: by mail-pl1-f199.google.com with SMTP id d9443c01a7336-2982b47ce35so15036145ad.2 for ; Fri, 12 Dec 2025 07:05:01 -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) 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= X-MC-Unique: KeyOaELwOcyYN4Zp6P7krA-1 X-Mimecast-MFC-AGG-ID: KeyOaELwOcyYN4Zp6P7krA_1765551901 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/ZIq4anBN/EI4J3U0swqDjTeDBvdBgNnqjWu2+oVNEvjlZjhjlY6ut4 JF7CFlkPYNOLxKiS8bEyhacMirIIRA72OVzmhw7miManF2+d7ChgFtPEreHglezjxcKq 9vSKkIXDexxTGf9zF6AmY4CJtqfj07q+WzFd7m/JQiuF1yz81NJ/q07Few5s1m3JbYgJ GKnTYKyFvHgKhz2JsKkW172drxLa06SlwoFksBVBZxqSy+utXVC20FfVdoCPBozwbRyC 2RM4AVmN8vJEftYs6nmAUDFNnc/2/54XUGhYO+LYeU/ImvuSCI8eqLUev8R4cRyJWiv/ MfUA== X-Forwarded-Encrypted: i=1; AJvYcCVZX7vpsY+LbbqQ5FLK+Yj4uesdj7c/nMuJSbB8koGO2TPIWSAJ44Z2XsSWRLtbPPRmCOi59HuQwxPQ@nongnu.org X-Gm-Message-State: AOJu0YxXrardhJEMXVHTSpoo8rDtaqpSdzxnzvUg5OM8fsoQy3MzvHLs 7/Rpqqyqr51zCdFiqdpFaFCAc1Y5A5QUcfF58khA+rRQQxfDqSWHavlwV1QSXa2yuIBcK9xmjup kbjFT6T3s7FY1qe8KzDCc1rx5i4m3xcV8YPtU1rxysQGUh5RpJZsfJXpu X-Gm-Gg: AY/fxX5yCbPlPifGwWgEaIZiiT2JWlv1cjzj8AwYLKj3TDJpE3M4uLiPM0Wr5aVWmW3 TUVba1hnORwy5Uj3DNXx/Pv7dEfCH3WCEdoUftABJGsw33trGHTtEcMyvS+5YZ9b7cDefgxxeCe 5gyKMrjNRlThCV5q2H3e5GP9a5yhP2z6WBhAP+WBBdo2JLTO6v+LMaLO8XxO01/r3QfdIU9oifH jfUdWFRIQtCOrW6JHnqlXdZXrsuvVKkSyMAYOrzdJwRYb+XzMe0c6IG4VyE08aJ9EczeDSqfNH0 zBOCdkIZbEsHmUUhrtBjVB6n4P9eOPloa1pUfBlsI988ilQtToEeJLkw27tgy2LXK2wVlVKBOFI /NpbEbx4wCQn4ZHhvZbrSKj2SsrE9aFI9PL+GxRExb3Y= 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) 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: 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=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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1765552182771158500 Content-Type: text/plain; charset="utf-8" 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 cha= nge 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_no= tifiers =3D static NotifierWithReturnList register_vmfd_pre_change_notifiers =3D NOTIFIER_WITH_RETURN_LIST_INITIALIZER(register_vmfd_pre_change_notifie= rs); =20 +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; } =20 +static int kvm_rebind_vcpus(Error **errp) +{ + CPUState *cpu; + unsigned long vcpu_id; + KVMState *s =3D kvm_state; + int kvm_fd, ret =3D 0; + + CPU_FOREACH(cpu) { + vcpu_id =3D kvm_arch_vcpu_id(cpu); + + if (cpu->kvm_fd) { + close(cpu->kvm_fd); + } + + ret =3D kvm_arch_destroy_vcpu(cpu); + if (ret < 0) { + goto err; + } + + if (s->coalesced_mmio_ring =3D=3D (void *)cpu->kvm_run + PAGE_SIZE= ) { + s->coalesced_mmio_ring =3D NULL; + } + + ret =3D vcpu_unmap_regions(s, cpu); + if (ret < 0) { + goto err; + } + + ret =3D kvm_arch_pre_create_vcpu(cpu, errp); + if (ret < 0) { + goto err; + } + + kvm_fd =3D 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 =3D kvm_fd; + + cpu->vcpu_dirty =3D false; + cpu->dirty_pages =3D 0; + cpu->throttle_us_per_full =3D 0; + + ret =3D map_kvm_run(s, cpu, errp); + if (ret < 0) { + goto err; + } + + if (s->kvm_dirty_ring_size) { + ret =3D map_kvm_dirty_gfns(s, cpu, errp); + if (ret < 0) { + goto err; + } + } + + ret =3D kvm_arch_init_vcpu(cpu); + if (ret < 0) { + error_setg_errno(errp, -ret, + "kvm_init_vcpu: kvm_arch_init_vcpu failed (%l= u)", + vcpu_id); + } + + close(cpu->kvm_vcpu_stats_fd); + cpu->kvm_vcpu_stats_fd =3D kvm_vcpu_ioctl(cpu, KVM_GET_STATS_FD, N= ULL); + 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; } =20 -static int do_kvm_destroy_vcpu(CPUState *cpu) +static int vcpu_unmap_regions(KVMState *s, CPUState *cpu) { - KVMState *s =3D kvm_state; int mmap_size; int ret =3D 0; =20 - trace_kvm_destroy_vcpu(cpu->cpu_index, kvm_arch_vcpu_id(cpu)); - - ret =3D kvm_arch_destroy_vcpu(cpu); - if (ret < 0) { - goto err; - } - mmap_size =3D kvm_ioctl(s, KVM_GET_VCPU_MMAP_SIZE, 0); if (mmap_size < 0) { ret =3D mmap_size; @@ -551,39 +625,47 @@ static int do_kvm_destroy_vcpu(CPUState *cpu) cpu->kvm_dirty_gfns =3D NULL; } =20 - kvm_park_vcpu(cpu); -err: + err: return ret; } =20 -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 =3D kvm_state; - int mmap_size; - int ret; + int ret =3D 0; =20 - trace_kvm_init_vcpu(cpu->cpu_index, kvm_arch_vcpu_id(cpu)); + trace_kvm_destroy_vcpu(cpu->cpu_index, kvm_arch_vcpu_id(cpu)); =20 - ret =3D kvm_arch_pre_create_vcpu(cpu, errp); + ret =3D kvm_arch_destroy_vcpu(cpu); if (ret < 0) { goto err; } =20 - ret =3D kvm_create_vcpu(cpu); + /* If I am the CPU that created coalesced_mmio_ring, then discard it */ + if (s->coalesced_mmio_ring =3D=3D (void *)cpu->kvm_run + PAGE_SIZE) { + s->coalesced_mmio_ring =3D NULL; + } + + ret =3D 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 =3D 0; =20 mmap_size =3D 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; } =20 + err: + return ret; +} + +static int map_kvm_dirty_gfns(KVMState *s, CPUState *cpu, Error **errp) +{ + int ret =3D 0; + /* Use MAP_SHARED to share pages with the kernel */ + cpu->kvm_dirty_gfns =3D 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 =3D=3D MAP_FAILED) { + ret =3D -errno; + } + + return ret; +} + +int kvm_init_vcpu(CPUState *cpu, Error **errp) +{ + KVMState *s =3D kvm_state; + int ret; + + trace_kvm_init_vcpu(cpu->cpu_index, kvm_arch_vcpu_id(cpu)); + + ret =3D kvm_arch_pre_create_vcpu(cpu, errp); + if (ret < 0) { + goto err; + } + + ret =3D 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 =3D 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 =3D 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 =3D=3D MAP_FAILED) { - ret =3D -errno; + ret =3D 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); =20 + /* + * rebind new vcpu fds with the new kvm fds + * These can only be called after kvm_arch_vmfd_change_ops() + */ + ret =3D 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); --=20 2.42.0