vfio: Use cdev_device_add() instead of device_create()
authorJason Gunthorpe <jgg@nvidia.com>
Fri, 15 Oct 2021 11:40:54 +0000 (08:40 -0300)
committerAlex Williamson <alex.williamson@redhat.com>
Fri, 15 Oct 2021 19:58:20 +0000 (13:58 -0600)
commit9cef73918e15d2284e71022291a8a07901e80bad
treef61dcce6458a1bc36e605ab5ea8ba998153fdaaa
parent2b678aa2f0990a25e15cdef66256a131566ecd2e
vfio: Use cdev_device_add() instead of device_create()

Modernize how vfio is creating the group char dev and sysfs presence.

These days drivers with state should use cdev_device_add() and
cdev_device_del() to manage the cdev and sysfs lifetime.

This API requires the driver to put the struct device and struct cdev
inside its state struct (vfio_group), and then use the usual
device_initialize()/cdev_device_add()/cdev_device_del() sequence.

Split the code to make this possible:

 - vfio_group_alloc()/vfio_group_release() are pair'd functions to
   alloc/free the vfio_group. release is done under the struct device
   kref.

 - vfio_create_group()/vfio_group_put() are pairs that manage the
   sysfs/cdev lifetime. Once the uses count is zero the vfio group's
   userspace presence is destroyed.

 - The IDR is replaced with an IDA. container_of(inode->i_cdev)
   is used to get back to the vfio_group during fops open. The IDA
   assigns unique minor numbers.

Reviewed-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Kevin Tian <kevin.tian@intel.com>
Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
Link: https://lore.kernel.org/r/5-v3-2fdfe4ca2cc6+18c-vfio_group_cdev_jgg@nvidia.com
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
drivers/vfio/vfio.c