Re: Potential thread synchronization issue in qcow2.c and qcow2-cluster.c

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



> I profiled all executions of
> qemu_mutex_lock_iothread(), and found that
> it only protects the vl.c:main_loop_wai() thread but does NOT protect
> the qemu-kvm.c:kvm_cpu_exec() thread. Did I miss something or is this
> a defect?

Hi again, I took another look at qemu-kvm 0.12.3 and here is how I read it:

The mutex which is supposed to protect IO emulation is qemu-kvm.c:qemu_mutex.

The cpu thread will unlock qemu_mutex in pre_kvm_run() before
ioctl(fd, KVM_RUN, 0).  Then it will lock qemu_mutex again in
post_kvm_run().

The io thread will unlock qemu_mutex via
qemu-kvm.c:qemu_mutex_unlock_iothread() before waiting in select().
Then it will lock qemu_mutex again in
qemu-kvm.c:qemu_mutex_lock_iothread().

I believe this *does* protect IO emulation correctly.  The code is
confusing because there are multiple definitions of the same functions
and #ifdefs, maybe I made a mistake.

> Here is the trace showing that
> qemu_mutex_lock_iothread() does not protect the thread
> that executes. kvm_cpu_exec()->...->qcow_aio_write_cb().
>
> home/ctang/kvm/qemu-kvm-0.12.3/qemu-kvm.c : 2530    thread: b7e056d0
>       /home/ctang/kvm/bin/qemu-system-x86_64(qemu_mutex_unlock_iothread+0x1a)
> [0x8092242]
>       /home/ctang/kvm/bin/qemu-system-x86_64(main_loop_wait+0x221) [0x806edef]
>       /home/ctang/kvm/bin/qemu-system-x86_64(kvm_main_loop+0x1ff) [0x80916a1]
>       /home/ctang/kvm/bin/qemu-system-x86_64 [0x806f5c2]
>       /home/ctang/kvm/bin/qemu-system-x86_64(main+0x2e2c) [0x80736d1]
>       /lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe5) [0xb7e33775]
>       /home/ctang/kvm/bin/qemu-system-x86_64 [0x8068bb1]
>
> block/qcow2-cluster.c : 721    thread: b7dc2b90
>       /home/ctang/kvm/bin/qemu-system-x86_64(qcow2_alloc_cluster_offset+0x3c)
> [0x81175fa]
>       /home/ctang/kvm/bin/qemu-system-x86_64(qcow_aio_write_cb+0x158)
> [0x8111d73]
>       /home/ctang/kvm/bin/qemu-system-x86_64(qcow_aio_writev+0x94) [0x8112054]
>       /home/ctang/kvm/bin/qemu-system-x86_64(bdrv_aio_writev+0xe1) [0x80fa8e9]
>       /home/ctang/kvm/bin/qemu-system-x86_64 [0x81f4a96]
>       /home/ctang/kvm/bin/qemu-system-x86_64 [0x81f4c04]
>       /home/ctang/kvm/bin/qemu-system-x86_64(dma_bdrv_write+0x48) [0x81f4cbf]
>       /home/ctang/kvm/bin/qemu-system-x86_64 [0x80a437c]
>       /home/ctang/kvm/bin/qemu-system-x86_64(bmdma_cmd_writeb+0x73)
> [0x80a9503]
>       /home/ctang/kvm/bin/qemu-system-x86_64 [0x812b1eb]
>       /home/ctang/kvm/bin/qemu-system-x86_64(cpu_outb+0x27) [0x812b4e6]
>       /home/ctang/kvm/bin/qemu-system-x86_64 [0x808d267]
>       /home/ctang/kvm/bin/qemu-system-x86_64(kvm_run+0x2f4) [0x808f4b8]
>       /home/ctang/kvm/bin/qemu-system-x86_64(kvm_cpu_exec+0x56) [0x80907b2]
>       /home/ctang/kvm/bin/qemu-system-x86_64 [0x8090f4d]
>       /home/ctang/kvm/bin/qemu-system-x86_64 [0x8091098]
>       /lib/tls/i686/cmov/libpthread.so.0 [0xb7fd24ff]
>       /lib/tls/i686/cmov/libc.so.6(clone+0x5e) [0xb7f0149e]
>
> /home/ctang/kvm/qemu-kvm-0.12.3/qemu-kvm.c : 2537    thread: b7e056d0
>       /home/ctang/kvm/bin/qemu-system-x86_64(qemu_mutex_lock_iothread+0x1a)
> [0x809229d]
>       /home/ctang/kvm/bin/qemu-system-x86_64(main_loop_wait+0x25c) [0x806ee2a]
>       /home/ctang/kvm/bin/qemu-system-x86_64(kvm_main_loop+0x1ff) [0x80916a1]
>       /home/ctang/kvm/bin/qemu-system-x86_64 [0x806f5c2]
>       /home/ctang/kvm/bin/qemu-system-x86_64(main+0x2e2c) [0x80736d1]
>       /lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe5) [0xb7e33775]
>       /home/ctang/kvm/bin/qemu-system-x86_64 [0x8068bb1]

kvm_cpu_exec() never calls qemu_mutex_lock_iothread() but it does lock
the underlying mutex via post_kvm_run().  It's just confusing because
vl.c calls it the "iothread mutex" whereas qemu-kvm.c calls it "qemu
mutex" and there are wrapper functions.

Does this help?

Stefan
--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [KVM ARM]     [KVM ia64]     [KVM ppc]     [Virtualization Tools]     [Spice Development]     [Libvirt]     [Libvirt Users]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite Questions]     [Linux Kernel]     [Linux SCSI]     [XFree86]
  Powered by Linux