All of lore.kernel.org
 help / color / mirror / Atom feed
From: Kirti Wankhede <kwankhede@nvidia.com>
To: Christoph Hellwig <hch@lst.de>,
	Tony Krowiak <akrowiak@linux.ibm.com>,
	Halil Pasic <pasic@linux.ibm.com>,
	Jason Herne <jjherne@linux.ibm.com>,
	Eric Farman <farman@linux.ibm.com>,
	Matthew Rosato <mjrosato@linux.ibm.com>,
	Zhenyu Wang <zhenyuw@linux.intel.com>,
	Zhi Wang <zhi.a.wang@intel.com>,
	Alex Williamson <alex.williamson@redhat.com>
Cc: kvm@vger.kernel.org, linux-s390@vger.kernel.org,
	intel-gvt-dev@lists.freedesktop.org, Neo Jia <cjia@nvidia.com>,
	Dheeraj Nigam <dnigam@nvidia.com>,
	Tarun Gupta <targupta@nvidia.com>
Subject: Re: [PATCH 02/13] vfio/mdev: embedd struct mdev_parent in the parent data structure
Date: Thu, 16 Jun 2022 00:59:47 +0530	[thread overview]
Message-ID: <ab47e216-f027-2083-308b-89c6aaa2e806@nvidia.com> (raw)
In-Reply-To: <20220614045428.278494-3-hch@lst.de>



On 6/14/2022 10:24 AM, Christoph Hellwig wrote:
<snip>


>   /*
> - * mdev_register_device : Register a device
> + * mdev_register_parent: Register a device as parent for mdevs
> + * @parent: parent structure registered
>    * @dev: device structure representing parent device.
>    * @mdev_driver: Device driver to bind to the newly created mdev
>    *
> - * Add device to list of registered parent devices.
>    * Returns a negative value on error, otherwise 0.
>    */
> -int mdev_register_device(struct device *dev, struct mdev_driver *mdev_driver)
> +int mdev_register_parent(struct mdev_parent *parent, struct device *dev,
> +		struct mdev_driver *mdev_driver)
>   {
> -	int ret;
> -	struct mdev_parent *parent;
>   	char *env_string = "MDEV_STATE=registered";
>   	char *envp[] = { env_string, NULL };
> +	int ret;
>   
>   	/* check for mandatory ops */
>   	if (!mdev_driver->supported_type_groups)
>   		return -EINVAL;
>   
> -	dev = get_device(dev);
> -	if (!dev)
> -		return -EINVAL;
> -

Hold the reference for device here once rather than helding multiple 
times while adding sysfs for each mdev_types below. See more below

> -	mutex_lock(&parent_list_lock);
> -
> -	/* Check for duplicate */
> -	parent = __find_parent_device(dev);
> -	if (parent) {
> -		parent = NULL;
> -		ret = -EEXIST;
> -		goto add_dev_err;
> -	}
> -
> -	parent = kzalloc(sizeof(*parent), GFP_KERNEL);
> -	if (!parent) {
> -		ret = -ENOMEM;
> -		goto add_dev_err;
> -	}
> -
> -	kref_init(&parent->ref);
> +	memset(parent, 0, sizeof(*parent));
>   	init_rwsem(&parent->unreg_sem);
> -
>   	parent->dev = dev;
>   	parent->mdev_driver = mdev_driver;
>   
>   	if (!mdev_bus_compat_class) {
>   		mdev_bus_compat_class = class_compat_register("mdev_bus");
> -		if (!mdev_bus_compat_class) {
> -			ret = -ENOMEM;
> -			goto add_dev_err;
> -		}
> +		if (!mdev_bus_compat_class)
> +			return -ENOMEM;
>   	}
>   
>   	ret = parent_create_sysfs_files(parent);
>   	if (ret)
> -		goto add_dev_err;
> +		return ret;
>   
>   	ret = class_compat_create_link(mdev_bus_compat_class, dev, NULL);
>   	if (ret)
>   		dev_warn(dev, "Failed to create compatibility class link\n");
>   
> -	list_add(&parent->next, &parent_list);
> -	mutex_unlock(&parent_list_lock);
> -
>   	dev_info(dev, "MDEV: Registered\n");
>   	kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
> -
>   	return 0;
> -
> -add_dev_err:
> -	mutex_unlock(&parent_list_lock);
> -	if (parent)
> -		mdev_put_parent(parent);
> -	else
> -		put_device(dev);
> -	return ret;
>   }
> -EXPORT_SYMBOL(mdev_register_device);
> +EXPORT_SYMBOL(mdev_register_parent);
>   
>   /*
> - * mdev_unregister_device : Unregister a parent device
> - * @dev: device structure representing parent device.
> - *
> - * Remove device from list of registered parent devices. Give a chance to free
> - * existing mediated devices for given device.
> + * mdev_unregister_parent : Unregister a parent device
> + * @parent: parent structure to unregister
>    */
> -
> -void mdev_unregister_device(struct device *dev)
> +void mdev_unregister_parent(struct mdev_parent *parent)
>   {
> -	struct mdev_parent *parent;
>   	char *env_string = "MDEV_STATE=unregistered";
>   	char *envp[] = { env_string, NULL };
>   
> -	mutex_lock(&parent_list_lock);
> -	parent = __find_parent_device(dev);
> -
> -	if (!parent) {
> -		mutex_unlock(&parent_list_lock);
> -		return;
> -	}
> -	dev_info(dev, "MDEV: Unregistering\n");
> -
> -	list_del(&parent->next);
> -	mutex_unlock(&parent_list_lock);
> +	dev_info(parent->dev, "MDEV: Unregistering\n");
>   
>   	down_write(&parent->unreg_sem);
> -
> -	class_compat_remove_link(mdev_bus_compat_class, dev, NULL);
> -
> -	device_for_each_child(dev, NULL, mdev_device_remove_cb);
> -
> +	class_compat_remove_link(mdev_bus_compat_class, parent->dev, NULL);
> +	device_for_each_child(parent->dev, NULL, mdev_device_remove_cb);
>   	parent_remove_sysfs_files(parent);
>   	up_write(&parent->unreg_sem);
>   
> -	mdev_put_parent(parent);
> -
> -	/* We still have the caller's reference to use for the uevent */
> -	kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
> +	kobject_uevent_env(&parent->dev->kobj, KOBJ_CHANGE, envp);
>   }
> -EXPORT_SYMBOL(mdev_unregister_device);
> +EXPORT_SYMBOL(mdev_unregister_parent);
>   
>   static void mdev_device_release(struct device *dev)
>   {
> diff --git a/drivers/vfio/mdev/mdev_private.h b/drivers/vfio/mdev/mdev_private.h
> index 7c9fc79f3d838..297f911fdc890 100644
> --- a/drivers/vfio/mdev/mdev_private.h
> +++ b/drivers/vfio/mdev/mdev_private.h
> @@ -13,17 +13,6 @@
>   int  mdev_bus_register(void);
>   void mdev_bus_unregister(void);
>   
> -struct mdev_parent {
> -	struct device *dev;
> -	struct mdev_driver *mdev_driver;
> -	struct kref ref;
> -	struct list_head next;
> -	struct kset *mdev_types_kset;
> -	struct list_head type_list;
> -	/* Synchronize device creation/removal with parent unregistration */
> -	struct rw_semaphore unreg_sem;
> -};
> -
>   struct mdev_type {
>   	struct kobject kobj;
>   	struct kobject *devices_kobj;
> @@ -48,16 +37,4 @@ void mdev_remove_sysfs_files(struct mdev_device *mdev);
>   int mdev_device_create(struct mdev_type *kobj, const guid_t *uuid);
>   int  mdev_device_remove(struct mdev_device *dev);
>   
> -void mdev_release_parent(struct kref *kref);
> -
> -static inline void mdev_get_parent(struct mdev_parent *parent)
> -{
> -	kref_get(&parent->ref);
> -}
> -
> -static inline void mdev_put_parent(struct mdev_parent *parent)
> -{
> -	kref_put(&parent->ref, mdev_release_parent);
> -}
> -
>   #endif /* MDEV_PRIVATE_H */
> diff --git a/drivers/vfio/mdev/mdev_sysfs.c b/drivers/vfio/mdev/mdev_sysfs.c
> index 4bfbf49aaa66a..b71ffc5594870 100644
> --- a/drivers/vfio/mdev/mdev_sysfs.c
> +++ b/drivers/vfio/mdev/mdev_sysfs.c
> @@ -81,7 +81,7 @@ static void mdev_type_release(struct kobject *kobj)
>   
>   	pr_debug("Releasing group %s\n", kobj->name);
>   	/* Pairs with the get in add_mdev_supported_type() */
> -	mdev_put_parent(type->parent);
> +	put_device(type->parent->dev);
>   	kfree(type);
>   }
>   
> @@ -110,7 +110,7 @@ static struct mdev_type *add_mdev_supported_type(struct mdev_parent *parent,
>   	type->kobj.kset = parent->mdev_types_kset;
>   	type->parent = parent;
>   	/* Pairs with the put in mdev_type_release() */
> -	mdev_get_parent(parent);
> +	get_device(parent->dev);
>   	type->type_group_id = type_group_id;
>

Here device reference is held and release multiple times for each 
mdev_type. It should be held once from mdev_register_parent() and 
released from mdev_unregister_parent().

Thanks,
Kirti

  parent reply	other threads:[~2022-06-15 19:30 UTC|newest]

Thread overview: 59+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-14  4:54 simplify the mdev interface v2 Christoph Hellwig
2022-06-14  4:54 ` [PATCH 01/13] vfio/mdev: make mdev.h standalone includable Christoph Hellwig
2022-06-14  9:50   ` Tian, Kevin
2022-06-15 19:12   ` Kirti Wankhede
2022-06-23 19:39   ` [PATCH 1/13] " Jason Gunthorpe
2022-06-14  4:54 ` [PATCH 02/13] vfio/mdev: embedd struct mdev_parent in the parent data structure Christoph Hellwig
2022-06-14  9:54   ` Tian, Kevin
2022-06-15 19:29   ` Kirti Wankhede [this message]
2022-06-23 20:18     ` Jason Gunthorpe
2022-06-24 12:29       ` Kirti Wankhede
2022-06-24 12:33         ` Jason Gunthorpe
2022-06-24 12:53           ` Kirti Wankhede
2022-06-24 13:05             ` Jason Gunthorpe
2022-06-24 13:14               ` Kirti Wankhede
2022-06-23 20:59   ` [PATCH 2/13] " Jason Gunthorpe
2022-06-14  4:54 ` [PATCH 03/13] vfio/mdev: simplify mdev_type handling Christoph Hellwig
2022-06-14  6:14   ` Yi Liu
2022-06-14 10:06   ` Tian, Kevin
2022-06-15  6:28     ` Christoph Hellwig
2022-06-15  6:11       ` Zhenyu Wang
2022-06-15 19:55   ` Kirti Wankhede
2022-06-19  7:42     ` Christoph Hellwig
2022-06-23 21:27   ` [PATCH 3/13] " Jason Gunthorpe
2022-06-24 12:32     ` Kirti Wankhede
2022-06-14  4:54 ` [PATCH 04/13] vfio/mdev: remove mdev_{create,remove}_sysfs_files Christoph Hellwig
2022-06-15 20:03   ` Kirti Wankhede
2022-06-19  7:37     ` Christoph Hellwig
2022-06-14  4:54 ` [PATCH 05/13] vfio/mdev: remove mdev_from_dev Christoph Hellwig
2022-06-15 20:06   ` Kirti Wankhede
2022-06-14  4:54 ` [PATCH 06/13] vfio/mdev: unexport mdev_bus_type Christoph Hellwig
2022-06-15 20:08   ` Kirti Wankhede
2022-06-14  4:54 ` [PATCH 07/13] vfio/mdev: remove mdev_parent_dev Christoph Hellwig
2022-06-15 20:11   ` Kirti Wankhede
2022-06-14  4:54 ` [PATCH 08/13] vfio/mdev: remove mtype_get_parent_dev Christoph Hellwig
2022-06-15 20:13   ` Kirti Wankhede
2022-06-14  4:54 ` [PATCH 09/13] vfio/mdev: consolidate all the device_api sysfs into the core code Christoph Hellwig
2022-06-14 10:10   ` Tian, Kevin
2022-06-15 20:24   ` Kirti Wankhede
2022-06-23 21:30   ` [PATCH 9/13] " Jason Gunthorpe
2022-06-14  4:54 ` [PATCH 10/13] vfio/mdev: consolidate all the name " Christoph Hellwig
2022-06-14 10:11   ` Tian, Kevin
2022-06-15 20:31   ` Kirti Wankhede
2022-06-23 21:32   ` Jason Gunthorpe
2022-06-14  4:54 ` [PATCH 11/13] vfio/mdev: consolidate all the available_instance " Christoph Hellwig
2022-06-14 10:14   ` Tian, Kevin
2022-06-14 10:29     ` Tian, Kevin
2022-06-15 20:37   ` Kirti Wankhede
2022-06-23 21:36   ` Jason Gunthorpe
2022-06-14  4:54 ` [PATCH 12/13] vfio/mdev: consolidate all the description " Christoph Hellwig
2022-06-15 20:52   ` Kirti Wankhede
2022-06-23 21:37   ` Jason Gunthorpe
2022-06-14  4:54 ` [PATCH 13/13] vfio/mdev: add mdev available instance checking to the core Christoph Hellwig
2022-06-14 10:32   ` Tian, Kevin
2022-06-15  6:29     ` Christoph Hellwig
2022-06-14  5:03 ` simplify the mdev interface v2 Yi Liu
2022-06-14  5:17   ` Christoph Hellwig
2022-06-14  5:30     ` Yi Liu
2022-06-14  5:44       ` Christoph Hellwig
2022-06-14  5:47         ` Yi Liu

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=ab47e216-f027-2083-308b-89c6aaa2e806@nvidia.com \
    --to=kwankhede@nvidia.com \
    --cc=akrowiak@linux.ibm.com \
    --cc=alex.williamson@redhat.com \
    --cc=cjia@nvidia.com \
    --cc=dnigam@nvidia.com \
    --cc=farman@linux.ibm.com \
    --cc=hch@lst.de \
    --cc=intel-gvt-dev@lists.freedesktop.org \
    --cc=jjherne@linux.ibm.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-s390@vger.kernel.org \
    --cc=mjrosato@linux.ibm.com \
    --cc=pasic@linux.ibm.com \
    --cc=targupta@nvidia.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 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.