[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [PULL v2 37/61] accel/kvm: Extract common KVM vCPU {creation,parking
From: |
Peter Maydell |
Subject: |
Re: [PULL v2 37/61] accel/kvm: Extract common KVM vCPU {creation,parking} code |
Date: |
Thu, 25 Jul 2024 11:35:35 +0100 |
On Tue, 23 Jul 2024 at 11:58, Michael S. Tsirkin <mst@redhat.com> wrote:
>
> From: Salil Mehta <salil.mehta@huawei.com>
>
> KVM vCPU creation is done once during the vCPU realization when Qemu vCPU
> thread
> is spawned. This is common to all the architectures as of now.
>
> Hot-unplug of vCPU results in destruction of the vCPU object in QOM but the
> corresponding KVM vCPU object in the Host KVM is not destroyed as KVM doesn't
> support vCPU removal. Therefore, its representative KVM vCPU object/context in
> Qemu is parked.
>
> Refactor architecture common logic so that some APIs could be reused by vCPU
> Hotplug code of some architectures likes ARM, Loongson etc. Update new/old
> APIs
> with trace events. New APIs qemu_{create,park,unpark}_vcpu() can be externally
> called. No functional change is intended here.
Hi; Coverity points out an issue with this code (CID 1558552):
> +int kvm_unpark_vcpu(KVMState *s, unsigned long vcpu_id)
> +{
> + struct KVMParkedVcpu *cpu;
> + int kvm_fd = -ENOENT;
> +
> + QLIST_FOREACH(cpu, &s->kvm_parked_vcpus, node) {
> + if (cpu->vcpu_id == vcpu_id) {
> + QLIST_REMOVE(cpu, node);
> + kvm_fd = cpu->kvm_fd;
> + g_free(cpu);
> + }
> + }
If you are going to remove an entry from a list as you
iterate over it, you can't use QLIST_FOREACH(), because
QLIST_FOREACH will look at the next pointer of the
iteration variable at the end of the loop when it
wants to advance to the next node. In this case we've
already freed 'cpu', so it would be reading freed memory.
Should we break out of the loop when we find the entry?
If we do need to continue iteration after removing the
list node, you need to use QLIST_FOREACH_SAFE() to do
the list iteration.
> -static int kvm_get_vcpu(KVMState *s, unsigned long vcpu_id)
> -{
> - struct KVMParkedVcpu *cpu;
> -
> - QLIST_FOREACH(cpu, &s->kvm_parked_vcpus, node) {
> - if (cpu->vcpu_id == vcpu_id) {
> - int kvm_fd;
> -
> - QLIST_REMOVE(cpu, node);
> - kvm_fd = cpu->kvm_fd;
> - g_free(cpu);
> - return kvm_fd;
In this old piece of code we were OK using QLIST_FOREACH
because we returned immediately we took the node off
the list and didn't continue the iteration.
> - }
> - }
> -
> - return kvm_vm_ioctl(s, KVM_CREATE_VCPU, (void *)vcpu_id);
> -}
thanks
-- PMM
- [PULL v2 30/61] pcie_sriov: Ensure PF and VF are mutually exclusive, (continued)
- [PULL v2 30/61] pcie_sriov: Ensure PF and VF are mutually exclusive, Michael S. Tsirkin, 2024/07/23
- [PULL v2 31/61] pcie_sriov: Check PCI Express for SR-IOV PF, Michael S. Tsirkin, 2024/07/23
- [PULL v2 33/61] virtio-pci: Implement SR-IOV PF, Michael S. Tsirkin, 2024/07/23
- [PULL v2 32/61] pcie_sriov: Allow user to create SR-IOV device, Michael S. Tsirkin, 2024/07/23
- [PULL v2 34/61] virtio-net: Implement SR-IOV VF, Michael S. Tsirkin, 2024/07/23
- [PULL v2 35/61] docs: Document composable SR-IOV device, Michael S. Tsirkin, 2024/07/23
- [PULL v2 36/61] smbios: make memory device size configurable per Machine, Michael S. Tsirkin, 2024/07/23
- [PULL v2 37/61] accel/kvm: Extract common KVM vCPU {creation,parking} code, Michael S. Tsirkin, 2024/07/23
- Re: [PULL v2 37/61] accel/kvm: Extract common KVM vCPU {creation,parking} code,
Peter Maydell <=
[PULL v2 38/61] hw/acpi: Move CPU ctrl-dev MMIO region len macro to common header file, Michael S. Tsirkin, 2024/07/23
[PULL v2 39/61] hw/acpi: Update ACPI GED framework to support vCPU Hotplug, Michael S. Tsirkin, 2024/07/23
[PULL v2 40/61] hw/acpi: Update GED _EVT method AML with CPU scan, Michael S. Tsirkin, 2024/07/23
[PULL v2 41/61] hw/acpi: Update CPUs AML with cpu-(ctrl)dev change, Michael S. Tsirkin, 2024/07/23
[PULL v2 42/61] physmem: Add helper function to destroy CPU AddressSpace, Michael S. Tsirkin, 2024/07/23
[PULL v2 43/61] gdbstub: Add helper function to unregister GDB register space, Michael S. Tsirkin, 2024/07/23
[PULL v2 45/61] virtio-iommu: Remove probe_done, Michael S. Tsirkin, 2024/07/23