From: Matthew Rosato <mjrosato@linux.ibm.com>
To: "Liu, Yi L" <yi.l.liu@intel.com>,
"alex.williamson@redhat.com" <alex.williamson@redhat.com>,
"pbonzini@redhat.com" <pbonzini@redhat.com>
Cc: "jgg@nvidia.com" <jgg@nvidia.com>,
"Tian, Kevin" <kevin.tian@intel.com>,
"cohuck@redhat.com" <cohuck@redhat.com>,
"farman@linux.ibm.com" <farman@linux.ibm.com>,
"pmorel@linux.ibm.com" <pmorel@linux.ibm.com>,
"borntraeger@linux.ibm.com" <borntraeger@linux.ibm.com>,
"frankja@linux.ibm.com" <frankja@linux.ibm.com>,
"imbrenda@linux.ibm.com" <imbrenda@linux.ibm.com>,
"david@redhat.com" <david@redhat.com>,
"akrowiak@linux.ibm.com" <akrowiak@linux.ibm.com>,
"jjherne@linux.ibm.com" <jjherne@linux.ibm.com>,
"pasic@linux.ibm.com" <pasic@linux.ibm.com>,
"zhenyuw@linux.intel.com" <zhenyuw@linux.intel.com>,
"Wang, Zhi A" <zhi.a.wang@intel.com>, "Christopherson, ,
Sean" <seanjc@google.com>,
"linux-s390@vger.kernel.org" <linux-s390@vger.kernel.org>,
"kvm@vger.kernel.org" <kvm@vger.kernel.org>,
"intel-gvt-dev@lists.freedesktop.org"
<intel-gvt-dev@lists.freedesktop.org>,
"intel-gfx@lists.freedesktop.org"
<intel-gfx@lists.freedesktop.org>,
"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH] kvm/vfio: Fix potential deadlock on vfio group_lock
Date: Fri, 20 Jan 2023 10:45:40 -0500 [thread overview]
Message-ID: <6eff63d8-d825-aecd-12b5-e8dbf55f4372@linux.ibm.com> (raw)
In-Reply-To: <DS0PR11MB7529B08476DF1764F1B6C007C3C59@DS0PR11MB7529.namprd11.prod.outlook.com>
On 1/20/23 10:08 AM, Liu, Yi L wrote:
>> From: Liu, Yi L <yi.l.liu@intel.com>
>> Sent: Friday, January 20, 2023 11:05 PM
>>
>> Currently it is possible that the final put of a KVM reference comes from
>> vfio during its device close operation. This occurs while the vfio group
>> lock is held; however, if the vfio device is still in the kvm device list,
>> then the following call chain could result in a deadlock:
>>
>> VFIO holds group->group_lock/group_rwsem
>> -> kvm_put_kvm
>> -> kvm_destroy_vm
>> -> kvm_destroy_devices
>> -> kvm_vfio_destroy
>> -> kvm_vfio_file_set_kvm
>> -> vfio_file_set_kvm
>> -> try to hold group->group_lock/group_rwsem
>>
>> The key function is the kvm_destroy_devices() which triggers destroy cb
>> of kvm_device_ops. It calls back to vfio and try to hold group_lock. So
>> if this path doesn't call back to vfio, this dead lock would be fixed.
>> Actually, there is a way for it. KVM provides another point to free the
>> kvm-vfio device which is the point when the device file descriptor is
>> closed. This can be achieved by providing the release cb instead of the
>> destroy cb. Also rename kvm_vfio_destroy() to be kvm_vfio_release().
>>
>> /*
>> * Destroy is responsible for freeing dev.
>> *
>> * Destroy may be called before or after destructors are called
>> * on emulated I/O regions, depending on whether a reference is
>> * held by a vcpu or other kvm component that gets destroyed
>> * after the emulated I/O.
>> */
>> void (*destroy)(struct kvm_device *dev);
>>
>> /*
>> * Release is an alternative method to free the device. It is
>> * called when the device file descriptor is closed. Once
>> * release is called, the destroy method will not be called
>> * anymore as the device is removed from the device list of
>> * the VM. kvm->lock is held.
>> */
>> void (*release)(struct kvm_device *dev);
>>
>> Fixes: 421cfe6596f6 ("vfio: remove VFIO_GROUP_NOTIFY_SET_KVM")
>> Reported-by: Alex Williamson <alex.williamson@redhat.com>
>> Suggested-by: Kevin Tian <kevin.tian@intel.com>
>> Reviewed-by: Jason Gunthorpe <jgg@nvidia.com>
>> Signed-off-by: Yi Liu <yi.l.liu@intel.com>
>
> More background can be found in Mathew's work.
> https://lore.kernel.org/kvm/20230114000351.115444-1-mjrosato@linux.ibm.com/T/#u
>
Thanks Yi.
Reviewed-by: Matthew Rosato <mjrosato@linux.ibm.com>
One small nit: There is a comment at the very end of kvm_vfio_release on the kfree(dev) that still references .destroy, this should be updated to .release
next prev parent reply other threads:[~2023-01-20 15:45 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-01-20 15:05 [PATCH] kvm/vfio: Fix potential deadlock on vfio group_lock Yi Liu
2023-01-20 15:08 ` Liu, Yi L
2023-01-20 15:45 ` Matthew Rosato [this message]
2023-01-20 15:49 ` Alex Williamson
2023-01-20 17:58 ` Alex Williamson
2023-01-31 14:27 ` Anthony Krowiak
2023-01-31 14:34 ` Jason Gunthorpe
2023-01-31 14:46 ` Anthony Krowiak
2023-01-31 14:48 ` Jason Gunthorpe
2023-01-31 15:00 ` Matthew Rosato
2023-01-31 15:12 ` Jason Gunthorpe
2023-01-31 14:35 ` Matthew Rosato
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=6eff63d8-d825-aecd-12b5-e8dbf55f4372@linux.ibm.com \
--to=mjrosato@linux.ibm.com \
--cc=akrowiak@linux.ibm.com \
--cc=alex.williamson@redhat.com \
--cc=borntraeger@linux.ibm.com \
--cc=cohuck@redhat.com \
--cc=david@redhat.com \
--cc=farman@linux.ibm.com \
--cc=frankja@linux.ibm.com \
--cc=imbrenda@linux.ibm.com \
--cc=intel-gfx@lists.freedesktop.org \
--cc=intel-gvt-dev@lists.freedesktop.org \
--cc=jgg@nvidia.com \
--cc=jjherne@linux.ibm.com \
--cc=kevin.tian@intel.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-s390@vger.kernel.org \
--cc=pasic@linux.ibm.com \
--cc=pbonzini@redhat.com \
--cc=pmorel@linux.ibm.com \
--cc=seanjc@google.com \
--cc=yi.l.liu@intel.com \
--cc=zhenyuw@linux.intel.com \
--cc=zhi.a.wang@intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox