From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752157AbdGYNap (ORCPT ); Tue, 25 Jul 2017 09:30:45 -0400 Received: from szxga01-in.huawei.com ([45.249.212.187]:10265 "EHLO szxga01-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750980AbdGYNan (ORCPT ); Tue, 25 Jul 2017 09:30:43 -0400 Subject: Race condition in vfio will cause deadwait CC: , , , "wencongyang (A)" , "Wangli (T)" , "Wanghui (John)" , guijianfeng References: <03bf80d2-546d-614a-7af9-1f786fe01285@huawei.com> To: Alex Williamson From: Wanlong Gao X-Forwarded-Message-Id: <03bf80d2-546d-614a-7af9-1f786fe01285@huawei.com> Message-ID: <4b8a662f-23fa-2777-a7ce-084a6f828f8f@huawei.com> Date: Tue, 25 Jul 2017 21:30:04 +0800 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.4.0 MIME-Version: 1.0 In-Reply-To: <03bf80d2-546d-614a-7af9-1f786fe01285@huawei.com> Content-Type: text/plain; charset="windows-1252" Content-Transfer-Encoding: 7bit X-Originating-IP: [10.177.97.93] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A090201.597747FF.003A,ss=1,re=0.000,recu=0.000,reip=0.000,cl=1,cld=1,fgs=0, ip=0.0.0.0, so=2014-11-16 11:51:01, dmn=2013-03-21 17:37:32 X-Mirapoint-Loop-Id: 52b52987de3a54d80950d7d093d04c11 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Wen Congyang 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