All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Tian, Kevin" <kevin.tian@intel.com>
To: Matthew Rosato <mjrosato@linux.ibm.com>,
	Alex Williamson <alex.williamson@redhat.com>
Cc: "akrowiak@linux.ibm.com" <akrowiak@linux.ibm.com>,
	"jjherne@linux.ibm.com" <jjherne@linux.ibm.com>,
	"farman@linux.ibm.com" <farman@linux.ibm.com>,
	"borntraeger@linux.ibm.com" <borntraeger@linux.ibm.com>,
	"frankja@linux.ibm.com" <frankja@linux.ibm.com>,
	"pmorel@linux.ibm.com" <pmorel@linux.ibm.com>,
	"david@redhat.com" <david@redhat.com>, "Christopherson, ,
	Sean" <seanjc@google.com>,
	"intel-gfx@lists.freedesktop.org"
	<intel-gfx@lists.freedesktop.org>,
	"cohuck@redhat.com" <cohuck@redhat.com>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	"pasic@linux.ibm.com" <pasic@linux.ibm.com>,
	"Liu, Yi L" <yi.l.liu@intel.com>,
	"jgg@nvidia.com" <jgg@nvidia.com>,
	"kvm@vger.kernel.org" <kvm@vger.kernel.org>,
	"pbonzini@redhat.com" <pbonzini@redhat.com>,
	"linux-s390@vger.kernel.org" <linux-s390@vger.kernel.org>,
	"imbrenda@linux.ibm.com" <imbrenda@linux.ibm.com>,
	"intel-gvt-dev@lists.freedesktop.org"
	<intel-gvt-dev@lists.freedesktop.org>
Subject: Re: [Intel-gfx] [PATCH v4] vfio: fix potential deadlock on vfio group lock
Date: Thu, 19 Jan 2023 03:43:36 +0000	[thread overview]
Message-ID: <BN9PR11MB5276CC29F17B87D14D1E61FF8CC49@BN9PR11MB5276.namprd11.prod.outlook.com> (raw)
In-Reply-To: <b5a7efc9-7cfa-3314-fe36-b8da4a25265d@linux.ibm.com>

> From: Matthew Rosato <mjrosato@linux.ibm.com>
> Sent: Wednesday, January 18, 2023 10:56 PM
> 
> On 1/18/23 4:03 AM, Tian, Kevin wrote:
> >> From: Alex Williamson
> >> Sent: Wednesday, January 18, 2023 5:23 AM
> >>
> >> On Fri, 13 Jan 2023 19:03:51 -0500
> >> Matthew Rosato <mjrosato@linux.ibm.com> wrote:
> >>
> >>>  void vfio_device_group_close(struct vfio_device *device)
> >>>  {
> >>> +	void (*put_kvm)(struct kvm *kvm);
> >>> +	struct kvm *kvm;
> >>> +
> >>>  	mutex_lock(&device->group->group_lock);
> >>> +	kvm = device->kvm;
> >>> +	put_kvm = device->put_kvm;
> >>>  	vfio_device_close(device, device->group->iommufd);
> >>> +	if (kvm == device->kvm)
> >>> +		kvm = NULL;
> >>
> >> Hmm, so we're using whether the device->kvm pointer gets cleared in
> >> last_close to detect whether we should put the kvm reference.  That's a
> >> bit obscure.  Our get and put is also asymmetric.
> >>
> >> Did we decide that we couldn't do this via a schedule_work() from the
> >> last_close function, ie. implementing our own version of an async put?
> >> It seems like that potentially has a cleaner implementation, symmetric
> >> call points, handling all the storing and clearing of kvm related
> >> pointers within the get/put wrappers, passing only a vfio_device to the
> >> put wrapper, using the "vfio_device_" prefix for both.  Potentially
> >> we'd just want an unconditional flush outside of lock here for
> >> deterministic release.
> >>
> >> What's the downside?  Thanks,
> >>
> >
> > btw I guess this can be also fixed by Yi's work here:
> >
> > https://lore.kernel.org/kvm/20230117134942.101112-6-yi.l.liu@intel.com/
> >
> > with set_kvm(NULL) moved to the release callback of kvm_vfio device,
> > such circular lock dependency can be avoided too.
> 
> Oh, interesting...  It seems to me that this would eliminate the reported call
> chain altogether:
> 
> kvm_put_kvm
>  -> kvm_destroy_vm
>   -> kvm_destroy_devices
>    -> kvm_vfio_destroy (starting here -- this would no longer be executed)
>     -> kvm_vfio_file_set_kvm
>      -> vfio_file_set_kvm
>       -> group->group_lock/group_rwsem
> 
> because kvm_destroy_devices now can't end up calling kvm_vfio_destroy
> and friends, it won't try and acquire the group lock a 2nd time making a
> kvm_put_kvm while the group lock is held OK to do.  The vfio_file_set_kvm
> call will now always come from a separate thread of execution,
> kvm_vfio_group_add, kvm_vfio_group_del or the release thread:
> 
> kvm_device_release (where the group->group_lock would not be held since
> vfio does not trigger closing of the kvm fd)
>  -> kvm_vfio_destroy (or, kvm_vfio_release)
>   -> kvm_vfio_file_set_kvm
>    -> vfio_file_set_kvm
>     -> group->group_lock/group_rwsem

Yes, that's my point. If Alex/Jason are also OK with it probably Yi can
send that patch separately as a fix to this issue. It's much simpler. 😊

WARNING: multiple messages have this Message-ID (diff)
From: "Tian, Kevin" <kevin.tian@intel.com>
To: Matthew Rosato <mjrosato@linux.ibm.com>,
	Alex Williamson <alex.williamson@redhat.com>
Cc: "akrowiak@linux.ibm.com" <akrowiak@linux.ibm.com>,
	"jjherne@linux.ibm.com" <jjherne@linux.ibm.com>,
	"farman@linux.ibm.com" <farman@linux.ibm.com>,
	"imbrenda@linux.ibm.com" <imbrenda@linux.ibm.com>,
	"frankja@linux.ibm.com" <frankja@linux.ibm.com>,
	"pmorel@linux.ibm.com" <pmorel@linux.ibm.com>,
	"david@redhat.com" <david@redhat.com>, "Christopherson,,
	Sean" <seanjc@google.com>,
	"intel-gfx@lists.freedesktop.org"
	<intel-gfx@lists.freedesktop.org>,
	"cohuck@redhat.com" <cohuck@redhat.com>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	"zhenyuw@linux.intel.com" <zhenyuw@linux.intel.com>,
	"pasic@linux.ibm.com" <pasic@linux.ibm.com>,
	"jgg@nvidia.com" <jgg@nvidia.com>,
	"kvm@vger.kernel.org" <kvm@vger.kernel.org>,
	"pbonzini@redhat.com" <pbonzini@redhat.com>,
	"linux-s390@vger.kernel.org" <linux-s390@vger.kernel.org>,
	"borntraeger@linux.ibm.com" <borntraeger@linux.ibm.com>,
	"Liu, Yi L" <yi.l.liu@intel.com>,
	"intel-gvt-dev@lists.freedesktop.org" 
	<intel-gvt-dev@lists.freedesktop.org>,
	"Wang, Zhi A" <zhi.a.wang@intel.com>
Subject: RE: [PATCH v4] vfio: fix potential deadlock on vfio group lock
Date: Thu, 19 Jan 2023 03:43:36 +0000	[thread overview]
Message-ID: <BN9PR11MB5276CC29F17B87D14D1E61FF8CC49@BN9PR11MB5276.namprd11.prod.outlook.com> (raw)
In-Reply-To: <b5a7efc9-7cfa-3314-fe36-b8da4a25265d@linux.ibm.com>

> From: Matthew Rosato <mjrosato@linux.ibm.com>
> Sent: Wednesday, January 18, 2023 10:56 PM
> 
> On 1/18/23 4:03 AM, Tian, Kevin wrote:
> >> From: Alex Williamson
> >> Sent: Wednesday, January 18, 2023 5:23 AM
> >>
> >> On Fri, 13 Jan 2023 19:03:51 -0500
> >> Matthew Rosato <mjrosato@linux.ibm.com> wrote:
> >>
> >>>  void vfio_device_group_close(struct vfio_device *device)
> >>>  {
> >>> +	void (*put_kvm)(struct kvm *kvm);
> >>> +	struct kvm *kvm;
> >>> +
> >>>  	mutex_lock(&device->group->group_lock);
> >>> +	kvm = device->kvm;
> >>> +	put_kvm = device->put_kvm;
> >>>  	vfio_device_close(device, device->group->iommufd);
> >>> +	if (kvm == device->kvm)
> >>> +		kvm = NULL;
> >>
> >> Hmm, so we're using whether the device->kvm pointer gets cleared in
> >> last_close to detect whether we should put the kvm reference.  That's a
> >> bit obscure.  Our get and put is also asymmetric.
> >>
> >> Did we decide that we couldn't do this via a schedule_work() from the
> >> last_close function, ie. implementing our own version of an async put?
> >> It seems like that potentially has a cleaner implementation, symmetric
> >> call points, handling all the storing and clearing of kvm related
> >> pointers within the get/put wrappers, passing only a vfio_device to the
> >> put wrapper, using the "vfio_device_" prefix for both.  Potentially
> >> we'd just want an unconditional flush outside of lock here for
> >> deterministic release.
> >>
> >> What's the downside?  Thanks,
> >>
> >
> > btw I guess this can be also fixed by Yi's work here:
> >
> > https://lore.kernel.org/kvm/20230117134942.101112-6-yi.l.liu@intel.com/
> >
> > with set_kvm(NULL) moved to the release callback of kvm_vfio device,
> > such circular lock dependency can be avoided too.
> 
> Oh, interesting...  It seems to me that this would eliminate the reported call
> chain altogether:
> 
> kvm_put_kvm
>  -> kvm_destroy_vm
>   -> kvm_destroy_devices
>    -> kvm_vfio_destroy (starting here -- this would no longer be executed)
>     -> kvm_vfio_file_set_kvm
>      -> vfio_file_set_kvm
>       -> group->group_lock/group_rwsem
> 
> because kvm_destroy_devices now can't end up calling kvm_vfio_destroy
> and friends, it won't try and acquire the group lock a 2nd time making a
> kvm_put_kvm while the group lock is held OK to do.  The vfio_file_set_kvm
> call will now always come from a separate thread of execution,
> kvm_vfio_group_add, kvm_vfio_group_del or the release thread:
> 
> kvm_device_release (where the group->group_lock would not be held since
> vfio does not trigger closing of the kvm fd)
>  -> kvm_vfio_destroy (or, kvm_vfio_release)
>   -> kvm_vfio_file_set_kvm
>    -> vfio_file_set_kvm
>     -> group->group_lock/group_rwsem

Yes, that's my point. If Alex/Jason are also OK with it probably Yi can
send that patch separately as a fix to this issue. It's much simpler. 😊

  reply	other threads:[~2023-01-19  3:43 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-14  0:03 [PATCH v4] vfio: fix potential deadlock on vfio group lock Matthew Rosato
2023-01-14  0:03 ` [Intel-gfx] " Matthew Rosato
2023-01-14  1:12 ` [Intel-gfx] ✓ Fi.CI.BAT: success for vfio: fix potential deadlock on vfio group lock (rev3) Patchwork
2023-01-14  8:37 ` [Intel-gfx] ✓ Fi.CI.IGT: " Patchwork
2023-01-16 15:03 ` [Intel-gfx] [PATCH v4] vfio: fix potential deadlock on vfio group lock Jason Gunthorpe
2023-01-16 15:03   ` Jason Gunthorpe
2023-01-17  9:05 ` [Intel-gfx] " Tian, Kevin
2023-01-17  9:05   ` Tian, Kevin
2023-01-17 21:22 ` [Intel-gfx] " Alex Williamson
2023-01-17 21:22   ` Alex Williamson
2023-01-18  9:03   ` [Intel-gfx] " Tian, Kevin
2023-01-18  9:03     ` Tian, Kevin
2023-01-18 14:55     ` [Intel-gfx] " Matthew Rosato
2023-01-18 14:55       ` Matthew Rosato
2023-01-19  3:43       ` Tian, Kevin [this message]
2023-01-19  3:43         ` Tian, Kevin
2023-01-19 19:05         ` [Intel-gfx] " Alex Williamson
2023-01-19 19:05           ` Alex Williamson
2023-01-18 14:15   ` [Intel-gfx] " Matthew Rosato
2023-01-18 14:15     ` 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=BN9PR11MB5276CC29F17B87D14D1E61FF8CC49@BN9PR11MB5276.namprd11.prod.outlook.com \
    --to=kevin.tian@intel.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=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-s390@vger.kernel.org \
    --cc=mjrosato@linux.ibm.com \
    --cc=pasic@linux.ibm.com \
    --cc=pbonzini@redhat.com \
    --cc=pmorel@linux.ibm.com \
    --cc=seanjc@google.com \
    --cc=yi.l.liu@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.