Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1695733 > unrolled thread

Race condition in vfio will cause deadwait

Started byWanlong Gao <gaowanlong@huawei.com>
First post2017-07-25 15:40 +0200
Last post2017-07-26 23:30 +0200
Articles 2 — 2 participants

Back to article view | Back to linux.kernel

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Race condition in vfio will cause deadwait Wanlong Gao <gaowanlong@huawei.com> - 2017-07-25 15:40 +0200
    Re: Race condition in vfio will cause deadwait Alex Williamson <alex.williamson@redhat.com> - 2017-07-26 23:30 +0200

#1695733 — Race condition in vfio will cause deadwait

FromWanlong Gao <gaowanlong@huawei.com>
Date2017-07-25 15:40 +0200
SubjectRace condition in vfio will cause deadwait
Message-ID<u78e6-3nc-17@gated-at.bofh.it>
From: Wen Congyang <wencongyang2@huawei.com>

Hi, Alex Williamson

When using vfio, we encounter a problem: too many lspci processes are blocked in D state.
I analyzed all processes, and found one process is blocked in pci_dev_lock(), another
process is blocked in vfio_del_group_put(). I checked the backtrace, and found the following
race condition:

Process A(use sysfs to unbind the device):
device_release_driver()
    device_lock(dev)
    __device_release_driver()
        dev->bus->remove(dev) // pci_device_remove()
            drv->remove(pci_dev) // vfio_pci_remove()
                vfio_del_group_dev()
                    vfio_device_put()
                    wait vfio_device is remove from the group. Process B gets vfio_device,
                    so we will wait it.

Process B:
vfio_group_fops_unl_ioctl()
    vfio_group_get_device_fd()
        vfio_device_get_from_name() // we get vfio_device here
        device->ops->open() // vfio_pci_open()
            vfio_pci_enable()
                pci_reset_function()
                    pci_dev_reset(dev, 0)
                        pci_dev_lock()
                            pci_cfg_access_lock(dev) // After this, lspci will be blocked
                            device_lock(&dev->dev) // the lock is hold by another process A

Now, Process A is waiting Process B, and Process B is waiting Process A.

I think we use pci_try_reset_function() to instead of pci_reset_function() can break this deadwait.


Thanks,
Wen Congyang

[toc] | [next] | [standalone]


#1697572

FromAlex Williamson <alex.williamson@redhat.com>
Date2017-07-26 23:30 +0200
Message-ID<u7C2u-5CF-25@gated-at.bofh.it>
In reply to#1695733
On Tue, 25 Jul 2017 21:30:04 +0800
Wanlong Gao <gaowanlong@huawei.com> wrote:

> From: Wen Congyang <wencongyang2@huawei.com>
> 
> Hi, Alex Williamson
> 
> When using vfio, we encounter a problem: too many lspci processes are blocked in D state.
> I analyzed all processes, and found one process is blocked in pci_dev_lock(), another
> process is blocked in vfio_del_group_put(). I checked the backtrace, and found the following
> race condition:
> 
> Process A(use sysfs to unbind the device):
> device_release_driver()
>     device_lock(dev)
>     __device_release_driver()
>         dev->bus->remove(dev) // pci_device_remove()
>             drv->remove(pci_dev) // vfio_pci_remove()
>                 vfio_del_group_dev()
>                     vfio_device_put()
>                     wait vfio_device is remove from the group. Process B gets vfio_device,
>                     so we will wait it.
> 
> Process B:
> vfio_group_fops_unl_ioctl()
>     vfio_group_get_device_fd()
>         vfio_device_get_from_name() // we get vfio_device here
>         device->ops->open() // vfio_pci_open()
>             vfio_pci_enable()
>                 pci_reset_function()
>                     pci_dev_reset(dev, 0)
>                         pci_dev_lock()
>                             pci_cfg_access_lock(dev) // After this, lspci will be blocked
>                             device_lock(&dev->dev) // the lock is hold by another process A
> 
> Now, Process A is waiting Process B, and Process B is waiting Process A.
> 
> I think we use pci_try_reset_function() to instead of pci_reset_function() can break this deadwait.

Thanks for your report and analysis, I think you're right.  Using the
trylock version of reset seems like the best approach, I've also used
lock contention as a reason to fail the open path for the user here.  I
would welcome your testing and feedback on the proposed patch:

https://lkml.org/lkml/2017/7/26/838

Thanks,
Alex

[toc] | [prev] | [standalone]


Back to top | Article view | linux.kernel


csiph-web