[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20200116153807.GI20978@mellanox.com>
Date: Thu, 16 Jan 2020 15:38:10 +0000
From: Jason Gunthorpe <jgg@...lanox.com>
To: Jason Wang <jasowang@...hat.com>
CC: "mst@...hat.com" <mst@...hat.com>,
"linux-kernel@...r.kernel.org" <linux-kernel@...r.kernel.org>,
"kvm@...r.kernel.org" <kvm@...r.kernel.org>,
"virtualization@...ts.linux-foundation.org"
<virtualization@...ts.linux-foundation.org>,
"netdev@...r.kernel.org" <netdev@...r.kernel.org>,
"tiwei.bie@...el.com" <tiwei.bie@...el.com>,
"maxime.coquelin@...hat.com" <maxime.coquelin@...hat.com>,
"cunming.liang@...el.com" <cunming.liang@...el.com>,
"zhihong.wang@...el.com" <zhihong.wang@...el.com>,
"rob.miller@...adcom.com" <rob.miller@...adcom.com>,
"xiao.w.wang@...el.com" <xiao.w.wang@...el.com>,
"haotian.wang@...ive.com" <haotian.wang@...ive.com>,
"lingshan.zhu@...el.com" <lingshan.zhu@...el.com>,
"eperezma@...hat.com" <eperezma@...hat.com>,
"lulu@...hat.com" <lulu@...hat.com>,
Parav Pandit <parav@...lanox.com>,
"kevin.tian@...el.com" <kevin.tian@...el.com>,
"stefanha@...hat.com" <stefanha@...hat.com>,
"rdunlap@...radead.org" <rdunlap@...radead.org>,
"hch@...radead.org" <hch@...radead.org>,
"aadam@...hat.com" <aadam@...hat.com>,
"jakub.kicinski@...ronome.com" <jakub.kicinski@...ronome.com>,
Jiri Pirko <jiri@...lanox.com>,
Shahaf Shuler <shahafs@...lanox.com>,
"hanand@...inx.com" <hanand@...inx.com>,
"mhabets@...arflare.com" <mhabets@...arflare.com>
Subject: Re: [PATCH 4/5] virtio: introduce a vDPA based transport
On Thu, Jan 16, 2020 at 08:42:30PM +0800, Jason Wang wrote:
> diff --git a/drivers/virtio/virtio_vdpa.c b/drivers/virtio/virtio_vdpa.c
> new file mode 100644
> index 000000000000..86936e5e7ec3
> +++ b/drivers/virtio/virtio_vdpa.c
> @@ -0,0 +1,400 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +/*
> + * VIRTIO based driver for vDPA device
> + *
> + * Copyright (c) 2020, Red Hat. All rights reserved.
> + * Author: Jason Wang <jasowang@...hat.com>
> + *
> + */
> +
> +#include <linux/init.h>
> +#include <linux/module.h>
> +#include <linux/device.h>
> +#include <linux/kernel.h>
> +#include <linux/slab.h>
> +#include <linux/uuid.h>
> +#include <linux/virtio.h>
> +#include <linux/vdpa.h>
> +#include <linux/virtio_config.h>
> +#include <linux/virtio_ring.h>
> +
> +#define MOD_VERSION "0.1"
> +#define MOD_AUTHOR "Jason Wang <jasowang@...hat.com>"
> +#define MOD_DESC "vDPA bus driver for virtio devices"
> +#define MOD_LICENSE "GPL v2"
> +
> +#define to_virtio_vdpa_device(dev) \
> + container_of(dev, struct virtio_vdpa_device, vdev)
Should be a static function
> +struct virtio_vdpa_device {
> + struct virtio_device vdev;
> + struct vdpa_device *vdpa;
> + u64 features;
> +
> + /* The lock to protect virtqueue list */
> + spinlock_t lock;
> + /* List of virtio_vdpa_vq_info */
> + struct list_head virtqueues;
> +};
> +
> +struct virtio_vdpa_vq_info {
> + /* the actual virtqueue */
> + struct virtqueue *vq;
> +
> + /* the list node for the virtqueues list */
> + struct list_head node;
> +};
> +
> +static struct vdpa_device *vd_get_vdpa(struct virtio_device *vdev)
> +{
> + struct virtio_vdpa_device *vd_dev = to_virtio_vdpa_device(vdev);
> + struct vdpa_device *vdpa = vd_dev->vdpa;
> +
> + return vdpa;
Bit of a long way to say
return to_virtio_vdpa_device(vdev)->vdpa
?
> +err_vq:
> + vring_del_virtqueue(vq);
> +error_new_virtqueue:
> + ops->set_vq_ready(vdpa, index, 0);
> + WARN_ON(ops->get_vq_ready(vdpa, index));
A warn_on during error unwind? Sketchy, deserves a comment I think
> +static void virtio_vdpa_release_dev(struct device *_d)
> +{
> + struct virtio_device *vdev =
> + container_of(_d, struct virtio_device, dev);
> + struct virtio_vdpa_device *vd_dev =
> + container_of(vdev, struct virtio_vdpa_device, vdev);
> + struct vdpa_device *vdpa = vd_dev->vdpa;
> +
> + devm_kfree(&vdpa->dev, vd_dev);
> +}
It is unusual for the release function to not be owned by the
subsystem, through the class. I'm not sure there are enough module ref
counts to ensure that this function is not unloaded?
Usually to make this all work sanely the subsytem provides some
allocation function
vdpa_dev = vdpa_alloc_dev(parent, ops, sizeof(struct virtio_vdpa_device))
struct virtio_vdpa_device *priv = vdpa_priv(vdpa_dev)
Then the subsystem naturally owns all the memory.
Otherwise it gets tricky to ensure that the module doesn't unload
before all the krefs are put.
> +
> +static int virtio_vdpa_probe(struct device *dev)
> +{
> + struct vdpa_device *vdpa = dev_to_vdpa(dev);
The probe function for a class should accept the classes type already,
no casting.
> + const struct vdpa_config_ops *ops = vdpa->config;
> + struct virtio_vdpa_device *vd_dev;
> + int rc;
> +
> + vd_dev = devm_kzalloc(dev, sizeof(*vd_dev), GFP_KERNEL);
> + if (!vd_dev)
> + return -ENOMEM;
This is not right, the struct device lifetime is controled by a kref,
not via devm. If you want to use a devm unwind then the unwind is
put_device, not devm_kfree.
In this simple situation I don't see a reason to use devm.
> + vd_dev->vdev.dev.parent = &vdpa->dev;
> + vd_dev->vdev.dev.release = virtio_vdpa_release_dev;
> + vd_dev->vdev.config = &virtio_vdpa_config_ops;
> + vd_dev->vdpa = vdpa;
> + INIT_LIST_HEAD(&vd_dev->virtqueues);
> + spin_lock_init(&vd_dev->lock);
> +
> + vd_dev->vdev.id.device = ops->get_device_id(vdpa);
> + if (vd_dev->vdev.id.device == 0)
> + return -ENODEV;
> +
> + vd_dev->vdev.id.vendor = ops->get_vendor_id(vdpa);
> + rc = register_virtio_device(&vd_dev->vdev);
> + if (rc)
> + put_device(dev);
And a ugly unwind like this is why you want to have device_initialize()
exposed to the driver, so there is a clear pairing that calling
device_initialize() must be followed by put_device. This should also
use the goto unwind style
> + else
> + dev_set_drvdata(dev, vd_dev);
> +
> + return rc;
> +}
> +
> +static void virtio_vdpa_remove(struct device *dev)
> +{
Remove should also already accept the right type
> + struct virtio_vdpa_device *vd_dev = dev_get_drvdata(dev);
> +
> + unregister_virtio_device(&vd_dev->vdev);
> +}
> +
> +static struct vdpa_driver virtio_vdpa_driver = {
> + .drv = {
> + .name = "virtio_vdpa",
> + },
> + .probe = virtio_vdpa_probe,
> + .remove = virtio_vdpa_remove,
> +};
Still a little unclear on binding, is this supposed to bind to all
vdpa devices?
Where is the various THIS_MODULE's I expect to see in a scheme like
this?
All function pointers must be protected by a held module reference
count, ie the above probe/remove and all the pointers in ops.
> +static int __init virtio_vdpa_init(void)
> +{
> + return register_vdpa_driver(&virtio_vdpa_driver);
> +}
> +
> +static void __exit virtio_vdpa_exit(void)
> +{
> + unregister_vdpa_driver(&virtio_vdpa_driver);
> +}
> +
> +module_init(virtio_vdpa_init)
> +module_exit(virtio_vdpa_exit)
Best to provide the usual 'module_pci_driver' like scheme for this
boiler plate.
> +MODULE_VERSION(MOD_VERSION);
> +MODULE_LICENSE(MOD_LICENSE);
> +MODULE_AUTHOR(MOD_AUTHOR);
> +MODULE_DESCRIPTION(MOD_DESC);
Why the indirection with 2nd defines?
Jason
Powered by blists - more mailing lists