Skip to content
  • Alexandru Elisei's avatar
    Use independent read/write locks for ioport and mmio · 85f439ec
    Alexandru Elisei authored
    kvmtool uses brlock for protecting accesses to the ioport and mmio
    red-black trees. brlock allows concurrent reads, but only one writer,
    which is assumed not to be a VCPU thread. This is done by issuing a
    compiler barrier on read and pausing the entire virtual machine on
    writes. When KVM_BRLOCK_DEBUG is defined, brlock uses instead a pthread
    read/write lock.
    
    When we will implement reassignable BARs, the mmio or ioport mapping
    will be done as a result of a VCPU mmio access. When brlock is a
    read/write lock, it means that we will try to acquire a write lock with
    the read lock already held by the same VCPU and we will deadlock. When
    it's not, a VCPU will have to call kvm__pause, which means the virtual
    machine will stay paused forever.
    
    Let's avoid all this by using separate pthread_rwlock_t locks for the
    mmio and the ioport red-black trees and carefully choosing our read
    critical region such that modification as a result of a guest mmio
    access doesn't deadlock.
    
    In theory, this leaves us with a small window of opportunity for a VCPU
    to modify a node used by another VCPU. Inserting in the trees is done by
    the main thread before starting the virtual machine, and deleting is
    done after the virtual machine has been paused to be destroyed, so in
    practice this can only happen if the guest is bugged.
    85f439ec