• Andrea Parri (Microsoft)'s avatar
    PCI: hv: Fix synchronization between channel callback and hv_compose_msi_msg() · a765ed47
    Andrea Parri (Microsoft) authored
    Dexuan wrote:
    
      "[...]  when we disable AccelNet, the host PCI VSP driver sends a
       PCI_EJECT message first, and the channel callback may set
       hpdev->state to hv_pcichild_ejecting on a different CPU.  This can
       cause hv_compose_msi_msg() to exit from the loop and 'return', and
       the on-stack variable 'ctxt' is invalid.  Now, if the response
       message from the host arrives, the channel callback will try to
       access the invalid 'ctxt' variable, and this may cause a crash."
    
    Schematically:
    
      Hyper-V sends PCI_EJECT msg
        hv_pci_onchannelcallback()
          state = hv_pcichild_ejecting
                                           hv_compose_msi_msg()
                                             alloc and init comp_pkt
                                             state == hv_pcichild_ejecting
      Hyper-V sends VM_PKT_COMP msg
        hv_pci_onchannelcallback()
          retrieve address of comp_pkt
                                             'free' comp_pkt and return
          comp_pkt->completion_func()
    
    Dexuan also showed how the crash can be triggered after introducing
    suitable delays in the driver code, thus validating the 'assumption'
    that the host can still normally respond to the guest's compose_msi
    request after the host has started to eject the PCI device.
    
    Fix the synchronization by leveraging the requestor lock as follows:
    
      - Before 'return'-ing in hv_compose_msi_msg(), remove the ID (while
        holding the requestor lock) associated to the completion packet.
    
      - Retrieve the address *and call ->completion_func() within a same
        (requestor) critical section in hv_pci_onchannelcallback().
    Reported-by: default avatarWei Hu <weh@microsoft.com>
    Reported-by: default avatarDexuan Cui <decui@microsoft.com>
    Suggested-by: default avatarMichael Kelley <mikelley@microsoft.com>
    Signed-off-by: default avatarAndrea Parri (Microsoft) <parri.andrea@gmail.com>
    Reviewed-by: default avatarMichael Kelley <mikelley@microsoft.com>
    Link: https://lore.kernel.org/r/20220419122325.10078-7-parri.andrea@gmail.comSigned-off-by: default avatarWei Liu <wei.liu@kernel.org>
    a765ed47
pci-hyperv.c 108 KB