All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jason Gunthorpe <jgg@nvidia.com>
To: Christoph Hellwig <hch@lst.de>
Cc: Alex Williamson <alex.williamson@redhat.com>,
	Cornelia Huck <cohuck@redhat.com>,
	kvm@vger.kernel.org, Kirti Wankhede <kwankhede@nvidia.com>,
	"Raj, Ashok" <ashok.raj@intel.com>,
	Dan Williams <dan.j.williams@intel.com>,
	Daniel Vetter <daniel@ffwll.ch>,
	Leon Romanovsky <leonro@nvidia.com>,
	Max Gurtovoy <mgurtovoy@nvidia.com>,
	Tarun Gupta <targupta@nvidia.com>
Subject: Re: [PATCH 02/12] vfio/mdev: Allow the mdev_parent_ops to specify the device driver to bind
Date: Mon, 26 Apr 2021 11:11:40 -0300	[thread overview]
Message-ID: <20210426141140.GU1370958@nvidia.com> (raw)
In-Reply-To: <20210426140257.GA15209@lst.de>

On Mon, Apr 26, 2021 at 04:02:57PM +0200, Christoph Hellwig wrote:
> On Fri, Apr 23, 2021 at 08:02:59PM -0300, Jason Gunthorpe wrote:
> > +/*
> > + * mdev drivers can refuse to bind during probe(), in this case we want to fail
> > + * the creation of the mdev all the way back to sysfs. This is a weird model
> > + * that doesn't fit in the driver core well, nor does it seem to appear any
> > + * place else in the kernel, so use a simple hack.
> > + */
> > +static int mdev_bind_driver(struct mdev_device *mdev)
> > +{
> > +	struct mdev_driver *drv = mdev->type->parent->ops->device_driver;
> > +	int ret;
> > +
> > +	if (!drv)
> > +		drv = &vfio_mdev_driver;
> > +
> > +	while (1) {
> > +		device_lock(&mdev->dev);
> > +		if (mdev->dev.driver == &drv->driver) {
> > +			ret = 0;
> > +			goto out_unlock;
> > +		}
> > +		if (mdev->probe_err) {
> > +			ret = mdev->probe_err;
> > +			goto out_unlock;
> > +		}
> > +		device_unlock(&mdev->dev);
> > +		ret = device_attach(&mdev->dev);
> > +		if (ret)
> > +			return ret;
> > +		mdev->probe_err = -EINVAL;
> > +	}
> > +	return 0;
> > +
> > +out_unlock:
> > +	device_unlock(&mdev->dev);
> > +	return ret;
> > +}
> 
> This looks strange to me, and I think by open coding
> device_attach we could do much better here, something like:

I look at this for a long time, it is strange.

> static int mdev_bind_driver(struct mdev_device *mdev)
> {
> 	struct mdev_driver *drv = mdev->type->parent->ops->device_driver;
> 	int ret = -EINVAL;
> 
> 	if (!drv)
> 		drv = &vfio_mdev_driver;
> 
> 	device_lock(&mdev->dev);
> 	if (WARN_ON_ONCE(device_is_bound(dev)))
> 		goto out_unlock;
> 	if (mdev->dev.p->dead)
> 	 	goto out_unlock;

'p' is private to the driver core so we can't touch it here

> 	mdev->dev.driver = &drv->driver;
> 	ret = device_bind_driver(&mdev->dev);

It is really counter intuitive but device_bind_driver() doesn't
actually call probe, or do a lot of other essential stuff.

As far as I can see the driver core has three different ways to bind
drivers:
 - The normal 'really_probe()' path with all the bells and whistles.
 - You can set dev.driver before calling device_add() and related
 - You can call device_bind_driver() 'somehow'.

The later two completely skip all the really_probe() stuff, so things
like devm and more become broken. They also don't call probe(), that
is up to the caller. They seem only usable in very niche special
cases, unfortunately.

Some callers open code the probe() but then they have ordering
problems with the sysfs and other little issues.

In this case 99% of the time the driver will already be bound here and
this routine does nothing - the only case I worried about about is
some kind of defered probe by default which calling device_attach()
will defeat.

Thanks,
Jason

  reply	other threads:[~2021-04-26 14:11 UTC|newest]

Thread overview: 74+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-23 23:02 [PATCH 00/12] Remove vfio_mdev.c, mdev_parent_ops and more Jason Gunthorpe
2021-04-23 23:02 ` [Intel-gfx] " Jason Gunthorpe
2021-04-23 23:02 ` Jason Gunthorpe
2021-04-23 23:02 ` [PATCH 01/12] vfio/mdev: Remove CONFIG_VFIO_MDEV_DEVICE Jason Gunthorpe
2021-04-23 23:02   ` [Intel-gfx] " Jason Gunthorpe
2021-04-23 23:02   ` Jason Gunthorpe
2021-04-24  0:08   ` Randy Dunlap
2021-04-24  0:08     ` [Intel-gfx] " Randy Dunlap
2021-04-24  0:08     ` Randy Dunlap
2021-04-26 18:26     ` Jason Gunthorpe
2021-04-26 18:26       ` [Intel-gfx] " Jason Gunthorpe
2021-04-26 18:26       ` Jason Gunthorpe
2021-04-26 19:11       ` Randy Dunlap
2021-04-26 19:11         ` [Intel-gfx] " Randy Dunlap
2021-04-26 19:11         ` Randy Dunlap
2021-04-23 23:02 ` [PATCH 02/12] vfio/mdev: Allow the mdev_parent_ops to specify the device driver to bind Jason Gunthorpe
2021-04-26 14:02   ` Christoph Hellwig
2021-04-26 14:11     ` Jason Gunthorpe [this message]
2021-04-23 23:03 ` [PATCH 03/12] vfio/mtty: Convert to use vfio_register_group_dev() Jason Gunthorpe
2021-04-26 14:06   ` Christoph Hellwig
2021-04-23 23:03 ` [PATCH 04/12] vfio/mdpy: " Jason Gunthorpe
2021-04-26 14:06   ` Christoph Hellwig
2021-04-23 23:03 ` [PATCH 05/12] vfio/mbochs: " Jason Gunthorpe
2021-04-26 14:06   ` Christoph Hellwig
2021-04-23 23:03 ` [PATCH 06/12] vfio/ap_ops: " Jason Gunthorpe
2021-04-26 14:07   ` Christoph Hellwig
2021-04-26 17:48   ` Cornelia Huck
2021-04-26 18:10     ` Jason Gunthorpe
2021-04-26 23:41     ` Halil Pasic
2021-05-03 20:14     ` Tony Krowiak
2021-05-03 20:33       ` Jason Gunthorpe
2021-05-04 13:58         ` Tony Krowiak
2021-05-04 16:04           ` Jason Gunthorpe
2021-05-05 13:07             ` Tony Krowiak
2021-05-04 15:30       ` Cornelia Huck
2021-05-05 12:30         ` Tony Krowiak
2021-05-05 17:47           ` Jason Gunthorpe
2021-05-05 16:28       ` Tony Krowiak
2021-04-23 23:03 ` [PATCH 07/12] vfio/ccw: " Jason Gunthorpe
2021-04-23 23:03 ` [PATCH 08/12] vfio/gvt: " Jason Gunthorpe
2021-04-23 23:03   ` [Intel-gfx] " Jason Gunthorpe
2021-04-23 23:03   ` Jason Gunthorpe
2021-04-26 14:13   ` Christoph Hellwig
2021-04-26 14:13     ` [Intel-gfx] " Christoph Hellwig
2021-04-26 15:44     ` Jason Gunthorpe
2021-04-26 15:44       ` [Intel-gfx] " Jason Gunthorpe
2021-04-26 15:44       ` Jason Gunthorpe
2021-04-23 23:03 ` [PATCH 09/12] vfio/mdev: Remove mdev_parent_ops dev_attr_groups Jason Gunthorpe
2021-04-26 14:14   ` Christoph Hellwig
2021-04-23 23:03 ` [PATCH 10/12] vfio/mdev: Remove mdev_parent_ops Jason Gunthorpe
2021-04-23 23:03   ` [Intel-gfx] " Jason Gunthorpe
2021-04-23 23:03   ` Jason Gunthorpe
2021-04-26 14:19   ` Christoph Hellwig
2021-04-26 14:19     ` [Intel-gfx] " Christoph Hellwig
2021-04-26 18:33     ` Jason Gunthorpe
2021-04-26 18:33       ` [Intel-gfx] " Jason Gunthorpe
2021-04-26 18:33       ` Jason Gunthorpe
2021-04-23 23:03 ` [PATCH 11/12] vfio/mdev: Use the driver core to create the 'remove' file Jason Gunthorpe
2021-04-26 14:20   ` Christoph Hellwig
2021-04-26 19:07     ` Jason Gunthorpe
2021-04-23 23:03 ` [PATCH 12/12] vfio/mdev: Remove mdev drvdata Jason Gunthorpe
2021-04-26 14:20   ` Christoph Hellwig
2021-04-26 16:43 ` [PATCH 00/12] Remove vfio_mdev.c, mdev_parent_ops and more Christian Borntraeger
2021-04-26 16:43   ` [Intel-gfx] " Christian Borntraeger
2021-04-26 16:43   ` Christian Borntraeger
2021-04-26 17:42   ` Jason Gunthorpe
2021-04-26 17:42     ` [Intel-gfx] " Jason Gunthorpe
2021-04-26 17:42     ` Jason Gunthorpe
2021-04-27  7:33     ` Christian Borntraeger
2021-04-27  7:33       ` [Intel-gfx] " Christian Borntraeger
2021-04-27  7:33       ` Christian Borntraeger
2021-04-27 23:21       ` Jason Gunthorpe
2021-04-27 23:21         ` [Intel-gfx] " Jason Gunthorpe
2021-04-27 23:21         ` Jason Gunthorpe

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=20210426141140.GU1370958@nvidia.com \
    --to=jgg@nvidia.com \
    --cc=alex.williamson@redhat.com \
    --cc=ashok.raj@intel.com \
    --cc=cohuck@redhat.com \
    --cc=dan.j.williams@intel.com \
    --cc=daniel@ffwll.ch \
    --cc=hch@lst.de \
    --cc=kvm@vger.kernel.org \
    --cc=kwankhede@nvidia.com \
    --cc=leonro@nvidia.com \
    --cc=mgurtovoy@nvidia.com \
    --cc=targupta@nvidia.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.