All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Tian, Kevin" <kevin.tian@intel.com>
To: Jason Gunthorpe <jgg@nvidia.com>,
	Alex Williamson <alex.williamson@redhat.com>,
	Cornelia Huck <cohuck@redhat.com>,
	"kvm@vger.kernel.org" <kvm@vger.kernel.org>,
	Kirti Wankhede <kwankhede@nvidia.com>
Cc: "Raj, Ashok" <ashok.raj@intel.com>,
	"Williams, Dan J" <dan.j.williams@intel.com>,
	Christoph Hellwig <hch@lst.de>,
	Leon Romanovsky <leonro@nvidia.com>,
	Max Gurtovoy <mgurtovoy@nvidia.com>,
	Tarun Gupta <targupta@nvidia.com>
Subject: RE: [PATCH 08/18] vfio/mdev: Reorganize mdev_device_create()
Date: Fri, 26 Mar 2021 03:31:38 +0000	[thread overview]
Message-ID: <MWHPR11MB1886AF9D6F7F37B553AF9C358C619@MWHPR11MB1886.namprd11.prod.outlook.com> (raw)
In-Reply-To: <8-v1-7dedf20b2b75+4f785-vfio2_jgg@nvidia.com>

> From: Jason Gunthorpe <jgg@nvidia.com>
> Sent: Wednesday, March 24, 2021 1:55 AM
> 
> Once the memory for the struct mdev_device is allocated it should
> immediately be device_initialize()'d and filled in so that put_device()
> can always be used to undo the allocation.
> 
> Place the mdev_get/put_parent() so that they are clearly protecting the
> mdev->parent pointer. Move the final put to the release function so that
> the lifetime rules are trivial to understand.
> 
> Remove mdev_device_free() as the release function via device_put() is now
> usable in all cases.
> 
> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>

Reviewed-by: Kevin Tian <kevin.tian@intel.com>

> ---
>  drivers/vfio/mdev/mdev_core.c | 46 +++++++++++++++--------------------
>  1 file changed, 20 insertions(+), 26 deletions(-)
> 
> diff --git a/drivers/vfio/mdev/mdev_core.c
> b/drivers/vfio/mdev/mdev_core.c
> index 7ec21c907397a5..517b6fd351b63a 100644
> --- a/drivers/vfio/mdev/mdev_core.c
> +++ b/drivers/vfio/mdev/mdev_core.c
> @@ -71,7 +71,6 @@ static void mdev_device_remove_common(struct
> mdev_device *mdev)
> 
>  	/* Balances with device_initialize() */
>  	put_device(&mdev->dev);
> -	mdev_put_parent(parent);
>  }
> 
>  static int mdev_device_remove_cb(struct device *dev, void *data)
> @@ -208,8 +207,13 @@ void mdev_unregister_device(struct device *dev)
>  }
>  EXPORT_SYMBOL(mdev_unregister_device);
> 
> -static void mdev_device_free(struct mdev_device *mdev)
> +static void mdev_device_release(struct device *dev)
>  {
> +	struct mdev_device *mdev = to_mdev_device(dev);
> +
> +	/* Pairs with the get in mdev_device_create() */
> +	mdev_put_parent(mdev->parent);
> +
>  	mutex_lock(&mdev_list_lock);
>  	list_del(&mdev->next);
>  	mutex_unlock(&mdev_list_lock);
> @@ -218,59 +222,50 @@ static void mdev_device_free(struct mdev_device
> *mdev)
>  	kfree(mdev);
>  }
> 
> -static void mdev_device_release(struct device *dev)
> -{
> -	struct mdev_device *mdev = to_mdev_device(dev);
> -
> -	mdev_device_free(mdev);
> -}
> -
>  int mdev_device_create(struct mdev_type *type, const guid_t *uuid)
>  {
>  	int ret;
>  	struct mdev_device *mdev, *tmp;
>  	struct mdev_parent *parent = type->parent;
> 
> -	mdev_get_parent(parent);
>  	mutex_lock(&mdev_list_lock);
> 
>  	/* Check for duplicate */
>  	list_for_each_entry(tmp, &mdev_list, next) {
>  		if (guid_equal(&tmp->uuid, uuid)) {
>  			mutex_unlock(&mdev_list_lock);
> -			ret = -EEXIST;
> -			goto mdev_fail;
> +			return -EEXIST;
>  		}
>  	}
> 
>  	mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
>  	if (!mdev) {
>  		mutex_unlock(&mdev_list_lock);
> -		ret = -ENOMEM;
> -		goto mdev_fail;
> +		return -ENOMEM;
>  	}
> 
> +	device_initialize(&mdev->dev);
> +	mdev->dev.parent  = parent->dev;
> +	mdev->dev.bus = &mdev_bus_type;
> +	mdev->dev.release = mdev_device_release;
> +	mdev->dev.groups = parent->ops->mdev_attr_groups;
> +	mdev->type = type;
> +	mdev->parent = parent;
> +	/* Pairs with the put in mdev_device_release() */
> +	mdev_get_parent(parent);
> +
>  	guid_copy(&mdev->uuid, uuid);
>  	list_add(&mdev->next, &mdev_list);
>  	mutex_unlock(&mdev_list_lock);
> 
> -	mdev->parent = parent;
> +	dev_set_name(&mdev->dev, "%pUl", uuid);
> 
>  	/* Check if parent unregistration has started */
>  	if (!down_read_trylock(&parent->unreg_sem)) {
> -		mdev_device_free(mdev);
>  		ret = -ENODEV;
>  		goto mdev_fail;
>  	}
> 
> -	device_initialize(&mdev->dev);
> -	mdev->dev.parent = parent->dev;
> -	mdev->dev.bus     = &mdev_bus_type;
> -	mdev->dev.release = mdev_device_release;
> -	dev_set_name(&mdev->dev, "%pUl", uuid);
> -	mdev->dev.groups = parent->ops->mdev_attr_groups;
> -	mdev->type = type;
> -
>  	ret = parent->ops->create(&type->kobj, mdev);
>  	if (ret)
>  		goto ops_create_fail;
> @@ -295,9 +290,8 @@ int mdev_device_create(struct mdev_type *type,
> const guid_t *uuid)
>  	parent->ops->remove(mdev);
>  ops_create_fail:
>  	up_read(&parent->unreg_sem);
> -	put_device(&mdev->dev);
>  mdev_fail:
> -	mdev_put_parent(parent);
> +	put_device(&mdev->dev);
>  	return ret;
>  }
> 
> --
> 2.31.0


  parent reply	other threads:[~2021-03-26  3:32 UTC|newest]

Thread overview: 101+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-23 17:55 [PATCH 00/18] Make vfio_mdev type safe Jason Gunthorpe
2021-03-23 17:55 ` [Intel-gfx] " Jason Gunthorpe
2021-03-23 17:55 ` Jason Gunthorpe
2021-03-23 17:55 ` [PATCH 01/18] vfio/mdev: Fix missing static's on MDEV_TYPE_ATTR's Jason Gunthorpe
2021-03-23 17:55 ` [PATCH 02/18] vfio/mdev: Add missing typesafety around mdev_device Jason Gunthorpe
2021-03-26  2:04   ` Tian, Kevin
2021-03-30 15:24   ` Cornelia Huck
2021-03-23 17:55 ` [PATCH 03/18] vfio/mdev: Simplify driver registration Jason Gunthorpe
2021-03-23 19:14   ` Christoph Hellwig
2021-03-26 12:10     ` Jason Gunthorpe
2021-03-26 12:55       ` Christoph Hellwig
2021-03-26  2:17   ` Tian, Kevin
2021-03-30 15:28   ` Cornelia Huck
2021-03-23 17:55 ` [PATCH 04/18] vfio/mdev: Use struct mdev_type in struct mdev_device Jason Gunthorpe
2021-03-23 19:15   ` Christoph Hellwig
2021-03-26  2:29   ` Tian, Kevin
2021-04-06 18:41     ` Jason Gunthorpe
2021-03-30 15:31   ` Cornelia Huck
2021-03-23 17:55 ` [PATCH 05/18] vfio/mdev: Do not allow a mdev_type to have a NULL parent pointer Jason Gunthorpe
2021-03-23 19:15   ` Christoph Hellwig
2021-04-06 16:08     ` Jason Gunthorpe
2021-03-26  2:38   ` Tian, Kevin
2021-03-29  8:42   ` Max Gurtovoy
2021-03-30 15:34   ` Cornelia Huck
2021-03-23 17:55 ` [PATCH 06/18] vfio/mdev: Expose mdev_get/put_parent to mdev_private.h Jason Gunthorpe
2021-03-23 19:16   ` Christoph Hellwig
2021-03-26  2:47   ` Tian, Kevin
2021-03-30 15:37   ` Cornelia Huck
2021-03-23 17:55 ` [PATCH 07/18] vfio/mdev: Add missing reference counting to mdev_type Jason Gunthorpe
2021-03-23 19:17   ` Christoph Hellwig
2021-03-26  2:52   ` Tian, Kevin
2021-03-30 15:38   ` Cornelia Huck
2021-03-23 17:55 ` [PATCH 08/18] vfio/mdev: Reorganize mdev_device_create() Jason Gunthorpe
2021-03-23 19:20   ` Christoph Hellwig
2021-03-26  3:33     ` Tian, Kevin
2021-03-26  3:36       ` Tian, Kevin
2021-04-06 16:40     ` Jason Gunthorpe
2021-03-26  3:31   ` Tian, Kevin [this message]
2021-03-30 15:50   ` Cornelia Huck
2021-03-23 17:55 ` [PATCH 09/18] vfio/mdev: Add missing error handling to dev_set_name() Jason Gunthorpe
2021-03-23 19:21   ` Christoph Hellwig
2021-03-26  3:35   ` Tian, Kevin
2021-03-29  8:50   ` Max Gurtovoy
2021-03-30 15:53   ` Cornelia Huck
2021-03-23 17:55 ` [PATCH 10/18] vfio/mdev: Remove duplicate storage of parent in mdev_device Jason Gunthorpe
2021-03-23 19:22   ` Christoph Hellwig
2021-03-26  3:53   ` Tian, Kevin
2021-03-26 11:53     ` Jason Gunthorpe
2021-03-30 16:14       ` Cornelia Huck
2021-03-30 16:50         ` Jason Gunthorpe
2021-03-30 16:15   ` Cornelia Huck
2021-03-23 17:55 ` [PATCH 11/18] vfio/mdev: Add mdev/mtype_get_type_group_id() Jason Gunthorpe
2021-03-23 19:23   ` Christoph Hellwig
2021-04-06 18:53     ` Jason Gunthorpe
2021-03-26  5:52   ` Tian, Kevin
2021-03-30 16:26   ` Cornelia Huck
2021-03-23 17:55 ` [PATCH 12/18] vfio/mtty: Use mdev_get_type_group_id() Jason Gunthorpe
2021-03-23 19:24   ` Christoph Hellwig
2021-03-23 17:55 ` [PATCH 13/18] vfio/mdpy: " Jason Gunthorpe
2021-03-23 19:25   ` Christoph Hellwig
2021-03-23 17:55 ` [PATCH 14/18] vfio/mbochs: " Jason Gunthorpe
2021-03-23 19:25   ` Christoph Hellwig
2021-03-23 17:55 ` [PATCH 15/18] vfio/gvt: Make DRM_I915_GVT depend on VFIO_MDEV Jason Gunthorpe
2021-03-23 17:55   ` [Intel-gfx] " Jason Gunthorpe
2021-03-23 19:26   ` Christoph Hellwig
2021-03-23 19:39     ` Jason Gunthorpe
2021-03-23 19:39       ` [Intel-gfx] " Jason Gunthorpe
2021-03-30  4:39       ` Zhenyu Wang
2021-03-30  4:39         ` Zhenyu Wang
2021-03-26  6:03   ` Tian, Kevin
2021-03-26  6:03     ` Tian, Kevin
2021-03-23 17:55 ` [PATCH 16/18] vfio/gvt: Use mdev_get_type_group_id() Jason Gunthorpe
2021-03-23 17:55   ` [Intel-gfx] " Jason Gunthorpe
2021-03-23 19:28   ` Christoph Hellwig
2021-03-26  6:09   ` Tian, Kevin
2021-03-26  6:09     ` [Intel-gfx] " Tian, Kevin
2021-03-23 17:55 ` [PATCH 17/18] vfio/mdev: Remove kobj from mdev_parent_ops->create() Jason Gunthorpe
2021-03-23 17:55   ` [Intel-gfx] " Jason Gunthorpe
2021-03-23 17:55   ` Jason Gunthorpe
2021-03-23 19:29   ` Christoph Hellwig
2021-03-23 19:29     ` [Intel-gfx] " Christoph Hellwig
2021-03-26  6:11   ` Tian, Kevin
2021-03-26  6:11     ` [Intel-gfx] " Tian, Kevin
2021-03-26  6:11     ` Tian, Kevin
2021-03-30 16:29   ` Cornelia Huck
2021-03-30 16:29     ` [Intel-gfx] " Cornelia Huck
2021-03-30 16:29     ` Cornelia Huck
2021-03-23 17:55 ` [PATCH 18/18] vfio/mdev: Correct the function signatures for the mdev_type_attributes Jason Gunthorpe
2021-03-23 17:55   ` [Intel-gfx] " Jason Gunthorpe
2021-03-23 17:55   ` Jason Gunthorpe
2021-03-23 19:31   ` Christoph Hellwig
2021-03-23 19:31     ` [Intel-gfx] " Christoph Hellwig
2021-04-06 18:34     ` Jason Gunthorpe
2021-04-06 18:34       ` [Intel-gfx] " Jason Gunthorpe
2021-04-06 18:34       ` Jason Gunthorpe
2021-03-26  7:03   ` Tian, Kevin
2021-03-26  7:03     ` [Intel-gfx] " Tian, Kevin
2021-03-26  7:03     ` Tian, Kevin
2021-03-30 16:35   ` Cornelia Huck
2021-03-30 16:35     ` [Intel-gfx] " Cornelia Huck
2021-03-30 16:35     ` Cornelia Huck

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=MWHPR11MB1886AF9D6F7F37B553AF9C358C619@MWHPR11MB1886.namprd11.prod.outlook.com \
    --to=kevin.tian@intel.com \
    --cc=alex.williamson@redhat.com \
    --cc=ashok.raj@intel.com \
    --cc=cohuck@redhat.com \
    --cc=dan.j.williams@intel.com \
    --cc=hch@lst.de \
    --cc=jgg@nvidia.com \
    --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.