Re: [PATCH v3 01/15] vfio: Add helpers for unifying vfio_device life cycle

2022-09-11 Thread Ethan Zhao

Hi, Kevin,

在 2022/9/9 18:22, Kevin Tian 写道:

The idea is to let vfio core manage the vfio_device life cycle instead
of duplicating the logic cross drivers. This is also a preparatory
step for adding struct device into vfio_device.

New pair of helpers together with a kref in vfio_device:

  - vfio_alloc_device()
  - vfio_put_device()


To be honest, this pair of functions make me confusing to understand their

behaviour from wording point of view:

- vfio_alloc_device(),  Okay, it allocates the vfio device, no reference
 count thing. but,
- vfio_put_device()
 seems it will decrease reference count and then if it is zero, free it.
 so they are not of one *pair* about wording.

How about
 
- vfio_alloc_device() / - vfio_free_device()

or
- vfio_get_device() / - vfio_put_device(), perhaps not match their behviour
in following code.

 


Thanks,
Ethan
 



Drivers can register @init/@release callbacks to manage any private
state wrapping the vfio_device.

However vfio-ccw doesn't fit this model due to a life cycle mess
that its private structure mixes both parent and mdev info hence must
be allocated/freed outside of the life cycle of vfio device.

Per prior discussions this won't be fixed in short term by IBM folks.

Instead of waiting for those modifications introduce another helper
vfio_init_device() so ccw can call it to initialize a pre-allocated
vfio_device.

Further implication of the ccw trick is that vfio_device cannot be
freed uniformly in vfio core. Instead, require *EVERY* driver to
implement @release and free vfio_device inside. Then ccw can choose
to delay the free at its own discretion.

Another trick down the road is that kvzalloc() is used to accommodate
the need of gvt which uses vzalloc() while all others use kzalloc().
So drivers should call a helper vfio_free_device() to free the
vfio_device instead of assuming that kfree() or vfree() is appliable.

Later once the ccw mess is fixed we can remove those tricks and
fully handle structure alloc/free in vfio core.

Existing vfio_{un}init_group_dev() will be deprecated after all
existing usages are converted to the new model.

Suggested-by: Jason Gunthorpe 
Co-developed-by: Yi Liu 
Signed-off-by: Yi Liu 
Signed-off-by: Kevin Tian 
Reviewed-by: Tony Krowiak 
Reviewed-by: Jason Gunthorpe 
Reviewed-by: Eric Auger 
---
  drivers/vfio/vfio_main.c | 92 
  include/linux/vfio.h | 25 ++-
  2 files changed, 116 insertions(+), 1 deletion(-)

diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c
index 27d9186f35d5..adc1b697bb78 100644
--- a/drivers/vfio/vfio_main.c
+++ b/drivers/vfio/vfio_main.c
@@ -498,6 +498,98 @@ void vfio_uninit_group_dev(struct vfio_device *device)
  }
  EXPORT_SYMBOL_GPL(vfio_uninit_group_dev);
  
+/* Release helper called by vfio_put_device() */

+void vfio_device_release(struct kref *kref)
+{
+   struct vfio_device *device =
+   container_of(kref, struct vfio_device, kref);
+
+   vfio_uninit_group_dev(device);
+
+   /*
+* kvfree() cannot be done here due to a life cycle mess in
+* vfio-ccw. Before the ccw part is fixed all drivers are
+* required to support @release and call vfio_free_device()
+* from there.
+*/
+   device->ops->release(device);
+}
+EXPORT_SYMBOL_GPL(vfio_device_release);
+
+/*
+ * Alloc and initialize vfio_device so it can be registered to vfio
+ * core.
+ *
+ * Drivers should use the wrapper vfio_alloc_device() for allocation.
+ * @size is the size of the structure to be allocated, including any
+ * private data used by the driver.
+ *
+ * Driver may provide an @init callback to cover device private data.
+ *
+ * Use vfio_put_device() to release the structure after success return.
+ */
+struct vfio_device *_vfio_alloc_device(size_t size, struct device *dev,
+  const struct vfio_device_ops *ops)
+{
+   struct vfio_device *device;
+   int ret;
+
+   if (WARN_ON(size < sizeof(struct vfio_device)))
+   return ERR_PTR(-EINVAL);
+
+   device = kvzalloc(size, GFP_KERNEL);
+   if (!device)
+   return ERR_PTR(-ENOMEM);
+
+   ret = vfio_init_device(device, dev, ops);
+   if (ret)
+   goto out_free;
+   return device;
+
+out_free:
+   kvfree(device);
+   return ERR_PTR(ret);
+}
+EXPORT_SYMBOL_GPL(_vfio_alloc_device);
+
+/*
+ * Initialize a vfio_device so it can be registered to vfio core.
+ *
+ * Only vfio-ccw driver should call this interface.
+ */
+int vfio_init_device(struct vfio_device *device, struct device *dev,
+const struct vfio_device_ops *ops)
+{
+   int ret;
+
+   vfio_init_group_dev(device, dev, ops);
+
+   if (ops->init) {
+   ret = ops->init(device);
+   if (ret)
+   goto out_uninit;
+   }
+
+   kref_init(>kref);
+   return 0;
+
+out_uninit:
+   

[PATCH] r_du_lvdsenc.c : Check return value of devm_ioremap_resource with IS_ERR

2013-12-10 Thread Ethan Zhao
Laurent,

Next time I scan the NULL check, if hit your driver again, I will resend it.

Thanks,
Ethan

On Tue, Dec 10, 2013 at 10:24 AM, Laurent Pinchart
 wrote:
> Hi Ethan,
>
> Thank you for the patch.
>
> On Sunday 08 December 2013 10:41:46 ethan.zhao wrote:
>> function devm_ioremap_resource() doesn't return NULL, should check its
>> return value with helper IS_ERR().
>>
>> Signed-off-by: ethan.zhao 
>
> This issue has already been addressed by a patch sent to the dri-devel mailing
> list by Wei Yongjun (https://lkml.org/lkml/2013/8/22/637).
>
> I've asked for a v2 to be submitted but haven't received any reply. I'll fix
> the problem myself and send the patch now.
>
>> ---
>>  drivers/gpu/drm/rcar-du/rcar_du_lvdsenc.c | 2 +-
>>  1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/drivers/gpu/drm/rcar-du/rcar_du_lvdsenc.c
>> b/drivers/gpu/drm/rcar-du/rcar_du_lvdsenc.c index a0f6a17..93190ab 100644
>> --- a/drivers/gpu/drm/rcar-du/rcar_du_lvdsenc.c
>> +++ b/drivers/gpu/drm/rcar-du/rcar_du_lvdsenc.c
>> @@ -151,7 +151,7 @@ static int rcar_du_lvdsenc_get_resources(struct
>> rcar_du_lvdsenc *lvds, }
>>
>>   lvds->mmio = devm_ioremap_resource(>dev, mem);
>> - if (lvds->mmio == NULL) {
>> + if (IS_ERR(lvds->mmio)) {
>>   dev_err(>dev, "failed to remap memory resource for %s\n",
>>   name);
>>   return -ENOMEM;
>
> --
> Regards,
>
> Laurent Pinchart
>