From nobody Mon Feb 9 17:25:16 2026 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=1769491054; cv=none; d=zohomail.com; s=zohoarc; b=acEqyTxAouSSl+zdZuPBPdFU1q8xLJQ8R2sJuZ3biVOlAlHyHP8krHmeTnV+OXT5pBoGpJOe0OJtKsf6CWD/NHfPmz/JhsdhQNW1ZIF4Z4NUfDpkG+z/HVoIVhzSb8celdif1ml2mdDNlzWJ9AheWHg4PYAqbFXiI+US4l1HoLw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1769491054; 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=ZdZcekSc5vy8wLxJHCJ2z+EqJ/GXzJFP2NRW8mt0sAY=; b=fmwBeJVXrHnbB66jQ3JSBVXJF0gQhcO24ke2ixICExWgaSqL75Cttx3pdSBk91jSt0eywm4TNwg2hfolrHitie8/wzP/FP8AGcQQu2zzAWrKTpQjawakJ0LvfIp9FNxXz9d6O9ojvAirJ+3nFbJVwWZY2YHPlg113cmce9iL04g= 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 1769491054689517.875704331335; Mon, 26 Jan 2026 21:17:34 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1vkbS9-0006zK-Qc; Tue, 27 Jan 2026 00:17:25 -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 1vkbS7-0006yt-Tl for qemu-devel@nongnu.org; Tue, 27 Jan 2026 00:17:23 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1vkbS6-0008Pu-3p for qemu-devel@nongnu.org; Tue, 27 Jan 2026 00:17:23 -0500 Received: from mail-pj1-f71.google.com (mail-pj1-f71.google.com [209.85.216.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-464-DZoj1i5rNyq2NsyATB58pw-1; Tue, 27 Jan 2026 00:17:19 -0500 Received: by mail-pj1-f71.google.com with SMTP id 98e67ed59e1d1-34ea5074935so4455280a91.0 for ; Mon, 26 Jan 2026 21:17:19 -0800 (PST) Received: from rhel9-box.lan ([122.163.48.79]) by smtp.googlemail.com with ESMTPSA id 98e67ed59e1d1-353f6230d5dsm1110925a91.17.2026.01.26.21.17.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Jan 2026 21:17:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1769491041; 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=ZdZcekSc5vy8wLxJHCJ2z+EqJ/GXzJFP2NRW8mt0sAY=; b=fFD/tUkqbMsxSNXbtMlS14iCNJHdH5wwy/rGXpdNGInlzdGdXKuoYr26CCgS74/x8kzzES VzFHQCH4IyItDe1og6S3D2atSxFrsXFbDtMKlNhdFZMLtw7yr0XUS0AFYZN2QxIf4rW5iG 7d7NCDrKoE27WFnxuRvg/ahjTRxOJ2A= X-MC-Unique: DZoj1i5rNyq2NsyATB58pw-1 X-Mimecast-MFC-AGG-ID: DZoj1i5rNyq2NsyATB58pw_1769491039 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1769491038; x=1770095838; 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=ZdZcekSc5vy8wLxJHCJ2z+EqJ/GXzJFP2NRW8mt0sAY=; b=GF+Yr/pD/4ie/PoUZzrSTOUZ1ZQpIqpJ+fkxWPB5ZxtR0lxuDZJbA664G3iS2YnolH 5BVnWujLQf1PfoQRL58Xjs0QjK1bLa+vavgnNxf12JTIIRDx6CsXQJBStTjoTfz6/qp3 6wUSbp6sa0TSo73dNO1r3F29y4HHLlzECS3cvnGtJuyVDEgDFjlLaCr3wBLd5Kr0TZ3t atHxU47xUwz1TDXvGo+HD1yfGr28MPYs6hTDveV9Qgdjkzi+usJ9gNJObyYvfp25Be9c 5Wy5bEOcqVq74BElrLWAnRCgx8JF668fVIJ3PSeT9JvYMfhJOYEpyJpcXPgpud8yMFdO 8ehQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769491038; x=1770095838; 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=ZdZcekSc5vy8wLxJHCJ2z+EqJ/GXzJFP2NRW8mt0sAY=; b=dqX+rQUQ9lKs7AzNiwNtbIsMOSIbd6zV1Yj3d/QBN+Gc3rVW4X+7Qk7+hk+03h7Gtm kXNXZlR7MCLaZZrWRXgLkVdvZmQpDsmbbnlPPtdYdAKA/FrtvPEbQOfx8awaaib78tTj YZTmcKQRW1rIWzEcvbyf4yeRMhMIrRKBFNwdQTVZ9xfs3/iLscMVyAZRSowYd6mfICfs 0rbH4Ii3gHyhqGBGV74NNr2fyBUnkqUFG6jvU4gSjBU6kPNoYuPuoc8VnUzyprWCgkbC i2CPRarIqEJ91GQxJ8FjuFxB+xRxkV5zSxS8NyDZyg5uPXY30tbCJyXnuQXg/ga651yZ 1Upw== X-Forwarded-Encrypted: i=1; AJvYcCUA3j+jjOuPozX13OHt/iea8gDbIgqkOt1aXUy0bK1U8VBlvzpCO+fPNPUqjeQKT30UCDEZWa8M2caM@nongnu.org X-Gm-Message-State: AOJu0Yxr+mdxQ+cTD15DYmajE6sa84pHxA3KT2beIo6VHzhVCUVqZnF/ 5drIEAdanyWZc3ijVYacT547rB3Jt3j6+XvLah0eDb+/JhqLpcQTT+1VSS4He5XS7If+sGhkNBL LvsDe3Etzkayr23jyIBm6sG1M0RiBFTr1o45IAfk1Qv/Jo5fDPTr/vu1KPrT835tJ X-Gm-Gg: AZuq6aJCkKp1RGfNwZ2ImDUXgVdYHsqcWUtYGesHg8X7u+teklBCun7BwFRtlF8V5v0 VjTwZ98gWpR43666olQj9u8RfHYhr3Zin6FWHgpwXSwodmYuUtI2zizJU7kp71rO5dzbdRGjW4F SaTM733t9yUdmsBfW/mGc9/A1cuzSyHgICoHLFKA2IBbvxM6H9hSLkH1sg1slCiEnEcs0+IjB3q 4zffPyDk9vYU+7fmO+urdAiuU7Rq+MQzRhXzJjN8QsHE9KM1uJUXDpNBxFelFpXXKmoZnQVzc1s W2lcUukJbZxAktTMextISXg+Tj2ng+5gsiadYsXc4s50/FoC1kMWwIue/DaK8eDApsYCmRtNTK3 XIQXOzIOFn80BwLrscONKJOhXypllb5mFGjQHcRPWbw== X-Received: by 2002:a17:90b:2d83:b0:34c:6124:3616 with SMTP id 98e67ed59e1d1-353fed87b05mr624013a91.27.1769491038309; Mon, 26 Jan 2026 21:17:18 -0800 (PST) X-Received: by 2002:a17:90b:2d83:b0:34c:6124:3616 with SMTP id 98e67ed59e1d1-353fed87b05mr623995a91.27.1769491037866; Mon, 26 Jan 2026 21:17:17 -0800 (PST) From: Ani Sinha To: Paolo Bonzini Cc: kraxel@redhat.com, Ani Sinha , kvm@vger.kernel.org, qemu-devel@nongnu.org Subject: [PATCH v3 14/33] accel/kvm: rebind current VCPUs to the new KVM VM file descriptor upon reset Date: Tue, 27 Jan 2026 10:45:42 +0530 Message-ID: <20260127051612.219475-15-anisinha@redhat.com> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20260127051612.219475-1-anisinha@redhat.com> References: <20260127051612.219475-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.133.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_H3=0.001, RCVD_IN_MSPIKE_WL=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: qemu development 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: 1769491055745158500 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 | 200 +++++++++++++++++++++++++++++++++-------- accel/kvm/trace-events | 1 + 2 files changed, 166 insertions(+), 35 deletions(-) diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c index 538a4cc731..d43a363488 100644 --- a/accel/kvm/kvm-all.c +++ b/accel/kvm/kvm-all.c @@ -127,6 +127,10 @@ static NotifierList kvm_irqchip_change_notifiers =3D static NotifierWithReturnList register_vmfd_changed_notifiers =3D NOTIFIER_WITH_RETURN_LIST_INITIALIZER(register_vmfd_changed_notifiers); =20 +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; @@ -420,6 +424,83 @@ 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); + } + trace_kvm_rebind_vcpus(); + + err: + return ret; +} + static void kvm_park_vcpu(CPUState *cpu) { struct KVMParkedVcpu *vcpu; @@ -508,19 +589,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; @@ -548,39 +621,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) { @@ -605,14 +686,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; } } @@ -2710,6 +2830,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); diff --git a/accel/kvm/trace-events b/accel/kvm/trace-events index e4beda0148..4a8921c632 100644 --- a/accel/kvm/trace-events +++ b/accel/kvm/trace-events @@ -15,6 +15,7 @@ kvm_park_vcpu(int cpu_index, unsigned long arch_cpu_id) "= index: %d id: %lu" kvm_unpark_vcpu(unsigned long arch_cpu_id, const char *msg) "id: %lu %s" kvm_irqchip_commit_routes(void) "" kvm_reset_vmfd(void) "" +kvm_rebind_vcpus(void) "" kvm_irqchip_add_msi_route(char *name, int vector, int virq) "dev %s vector= %d virq %d" kvm_irqchip_update_msi_route(int virq) "Updating MSI route virq=3D%d" kvm_irqchip_release_virq(int virq) "virq %d" --=20 2.42.0