This patch addresses below two issues and prepares the code to address
3rd issue listed below.
1. mdev device is placed on the mdev bus before it is created in the
vendor driver. Once a device is placed on the mdev bus without creating
its supporting underlying vendor device, mdev driver's probe() gets
triggered. However there isn't a stable mdev available to work on.
create_store()
mdev_create_device()
device_register()
...
vfio_mdev_probe()
[...]
parent->ops->create()
vfio_ap_mdev_create()
mdev_set_drvdata(mdev, matrix_mdev);
/* Valid pointer set above */
Due to this way of initialization, mdev driver who wants to use the mdev,
doesn't have a valid mdev to work on.
2. Current creation sequence is,
parent->ops_create()
groups_register()
Remove sequence is,
parent->ops->remove()
groups_unregister()
However, remove sequence should be exact mirror of creation sequence.
Once this is achieved, all users of the mdev will be terminated first
before removing underlying vendor device.
(Follow standard linux driver model).
At that point vendor's remove() ops shouldn't fail because taking the
device off the bus should terminate any usage.
3. When remove operation fails, mdev sysfs removal attempts to add the
file back on already removed device. Following call trace [1] is observed.
[1] call trace:
kernel: WARNING: CPU: 2 PID: 9348 at fs/sysfs/file.c:327 sysfs_create_file_ns+0x7f/0x90
kernel: CPU: 2 PID: 9348 Comm: bash Kdump: loaded Not tainted 5.1.0-rc6-vdevbus+ #6
kernel: Hardware name: Supermicro SYS-6028U-TR4+/X10DRU-i+, BIOS 2.0b 08/09/2016
kernel: RIP: 0010:sysfs_create_file_ns+0x7f/0x90
kernel: Call Trace:
kernel: remove_store+0xdc/0x100 [mdev]
kernel: kernfs_fop_write+0x113/0x1a0
kernel: vfs_write+0xad/0x1b0
kernel: ksys_write+0x5a/0xe0
kernel: do_syscall_64+0x5a/0x210
kernel: entry_SYSCALL_64_after_hwframe+0x49/0xbe
Therefore, mdev core is improved in following ways.
1. Split the device registration/deregistration sequence so that some
things can be done between initialization of the device and hooking it
up to the bus respectively after deregistering it from the bus but
before giving up our final reference.
In particular, this means invoking the ->create() and ->remove()
callbacks in those new windows. This gives the vendor driver an
initialized mdev device to work with during creation.
At the same time, a bus driver who wish to bind to mdev driver also
gets initialized mdev device.
This follows standard Linux kernel bus and device model.
2. During remove flow, first remove the device from the bus. This
ensures that any bus specific devices are removed.
Once device is taken off the mdev bus, invoke remove() of mdev
from the vendor driver.
3. The driver core device model provides way to register and auto
unregister the device sysfs attribute groups at dev->groups.
Make use of dev->groups to let core create the groups and eliminate
code to avoid explicit groups creation and removal.
To ensure, that new sequence is solid, a below stack dump of a
process is taken who attempts to remove the device while device is in
use by vfio driver and user application.
This stack dump validates that vfio driver guards against such device
removal when device is in use.
cat /proc/21962/stack
[<0>] vfio_del_group_dev+0x216/0x3c0 [vfio]
[<0>] mdev_remove+0x21/0x40 [mdev]
[<0>] device_release_driver_internal+0xe8/0x1b0
[<0>] bus_remove_device+0xf9/0x170
[<0>] device_del+0x168/0x350
[<0>] mdev_device_remove_common+0x1d/0x50 [mdev]
[<0>] mdev_device_remove+0x8c/0xd0 [mdev]
[<0>] remove_store+0x71/0x90 [mdev]
[<0>] kernfs_fop_write+0x113/0x1a0
[<0>] vfs_write+0xad/0x1b0
[<0>] ksys_write+0x5a/0xe0
[<0>] do_syscall_64+0x5a/0x210
[<0>] entry_SYSCALL_64_after_hwframe+0x49/0xbe
[<0>] 0xffffffffffffffff
This prepares the code to eliminate calling device_create_file() in
subsequent patch.
Reviewed-by: Cornelia Huck <cohuck@redhat.com>
Signed-off-by: Parav Pandit <parav@mellanox.com>
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
385 lines
8 KiB
C
385 lines
8 KiB
C
/*
|
|
* Mediated device Core Driver
|
|
*
|
|
* Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
|
|
* Author: Neo Jia <cjia@nvidia.com>
|
|
* Kirti Wankhede <kwankhede@nvidia.com>
|
|
*
|
|
* This program is free software; you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
* published by the Free Software Foundation.
|
|
*/
|
|
|
|
#include <linux/module.h>
|
|
#include <linux/device.h>
|
|
#include <linux/slab.h>
|
|
#include <linux/uuid.h>
|
|
#include <linux/sysfs.h>
|
|
#include <linux/mdev.h>
|
|
|
|
#include "mdev_private.h"
|
|
|
|
#define DRIVER_VERSION "0.1"
|
|
#define DRIVER_AUTHOR "NVIDIA Corporation"
|
|
#define DRIVER_DESC "Mediated device Core Driver"
|
|
|
|
static LIST_HEAD(parent_list);
|
|
static DEFINE_MUTEX(parent_list_lock);
|
|
static struct class_compat *mdev_bus_compat_class;
|
|
|
|
static LIST_HEAD(mdev_list);
|
|
static DEFINE_MUTEX(mdev_list_lock);
|
|
|
|
struct device *mdev_parent_dev(struct mdev_device *mdev)
|
|
{
|
|
return mdev->parent->dev;
|
|
}
|
|
EXPORT_SYMBOL(mdev_parent_dev);
|
|
|
|
void *mdev_get_drvdata(struct mdev_device *mdev)
|
|
{
|
|
return mdev->driver_data;
|
|
}
|
|
EXPORT_SYMBOL(mdev_get_drvdata);
|
|
|
|
void mdev_set_drvdata(struct mdev_device *mdev, void *data)
|
|
{
|
|
mdev->driver_data = data;
|
|
}
|
|
EXPORT_SYMBOL(mdev_set_drvdata);
|
|
|
|
struct device *mdev_dev(struct mdev_device *mdev)
|
|
{
|
|
return &mdev->dev;
|
|
}
|
|
EXPORT_SYMBOL(mdev_dev);
|
|
|
|
struct mdev_device *mdev_from_dev(struct device *dev)
|
|
{
|
|
return dev_is_mdev(dev) ? to_mdev_device(dev) : NULL;
|
|
}
|
|
EXPORT_SYMBOL(mdev_from_dev);
|
|
|
|
const guid_t *mdev_uuid(struct mdev_device *mdev)
|
|
{
|
|
return &mdev->uuid;
|
|
}
|
|
EXPORT_SYMBOL(mdev_uuid);
|
|
|
|
/* Should be called holding parent_list_lock */
|
|
static struct mdev_parent *__find_parent_device(struct device *dev)
|
|
{
|
|
struct mdev_parent *parent;
|
|
|
|
list_for_each_entry(parent, &parent_list, next) {
|
|
if (parent->dev == dev)
|
|
return parent;
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
static void mdev_release_parent(struct kref *kref)
|
|
{
|
|
struct mdev_parent *parent = container_of(kref, struct mdev_parent,
|
|
ref);
|
|
struct device *dev = parent->dev;
|
|
|
|
kfree(parent);
|
|
put_device(dev);
|
|
}
|
|
|
|
static struct mdev_parent *mdev_get_parent(struct mdev_parent *parent)
|
|
{
|
|
if (parent)
|
|
kref_get(&parent->ref);
|
|
|
|
return parent;
|
|
}
|
|
|
|
static void mdev_put_parent(struct mdev_parent *parent)
|
|
{
|
|
if (parent)
|
|
kref_put(&parent->ref, mdev_release_parent);
|
|
}
|
|
|
|
static int mdev_device_remove_cb(struct device *dev, void *data)
|
|
{
|
|
if (dev_is_mdev(dev))
|
|
mdev_device_remove(dev);
|
|
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
* mdev_register_device : Register a device
|
|
* @dev: device structure representing parent device.
|
|
* @ops: Parent device operation structure to be registered.
|
|
*
|
|
* Add device to list of registered parent devices.
|
|
* Returns a negative value on error, otherwise 0.
|
|
*/
|
|
int mdev_register_device(struct device *dev, const struct mdev_parent_ops *ops)
|
|
{
|
|
int ret;
|
|
struct mdev_parent *parent;
|
|
|
|
/* check for mandatory ops */
|
|
if (!ops || !ops->create || !ops->remove || !ops->supported_type_groups)
|
|
return -EINVAL;
|
|
|
|
dev = get_device(dev);
|
|
if (!dev)
|
|
return -EINVAL;
|
|
|
|
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);
|
|
|
|
parent->dev = dev;
|
|
parent->ops = ops;
|
|
|
|
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;
|
|
}
|
|
}
|
|
|
|
ret = parent_create_sysfs_files(parent);
|
|
if (ret)
|
|
goto add_dev_err;
|
|
|
|
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");
|
|
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);
|
|
|
|
/*
|
|
* 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.
|
|
*/
|
|
|
|
void mdev_unregister_device(struct device *dev)
|
|
{
|
|
struct mdev_parent *parent;
|
|
|
|
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);
|
|
class_compat_remove_link(mdev_bus_compat_class, dev, NULL);
|
|
|
|
device_for_each_child(dev, NULL, mdev_device_remove_cb);
|
|
|
|
parent_remove_sysfs_files(parent);
|
|
|
|
mutex_unlock(&parent_list_lock);
|
|
mdev_put_parent(parent);
|
|
}
|
|
EXPORT_SYMBOL(mdev_unregister_device);
|
|
|
|
static void mdev_device_release(struct device *dev)
|
|
{
|
|
struct mdev_device *mdev = to_mdev_device(dev);
|
|
|
|
mutex_lock(&mdev_list_lock);
|
|
list_del(&mdev->next);
|
|
mutex_unlock(&mdev_list_lock);
|
|
|
|
dev_dbg(&mdev->dev, "MDEV: destroying\n");
|
|
kfree(mdev);
|
|
}
|
|
|
|
int mdev_device_create(struct kobject *kobj,
|
|
struct device *dev, const guid_t *uuid)
|
|
{
|
|
int ret;
|
|
struct mdev_device *mdev, *tmp;
|
|
struct mdev_parent *parent;
|
|
struct mdev_type *type = to_mdev_type(kobj);
|
|
|
|
parent = mdev_get_parent(type->parent);
|
|
if (!parent)
|
|
return -EINVAL;
|
|
|
|
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;
|
|
}
|
|
}
|
|
|
|
mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
|
|
if (!mdev) {
|
|
mutex_unlock(&mdev_list_lock);
|
|
ret = -ENOMEM;
|
|
goto mdev_fail;
|
|
}
|
|
|
|
guid_copy(&mdev->uuid, uuid);
|
|
list_add(&mdev->next, &mdev_list);
|
|
mutex_unlock(&mdev_list_lock);
|
|
|
|
mdev->parent = parent;
|
|
|
|
device_initialize(&mdev->dev);
|
|
mdev->dev.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_kobj = kobj;
|
|
|
|
ret = parent->ops->create(kobj, mdev);
|
|
if (ret)
|
|
goto ops_create_fail;
|
|
|
|
ret = device_add(&mdev->dev);
|
|
if (ret)
|
|
goto add_fail;
|
|
|
|
ret = mdev_create_sysfs_files(&mdev->dev, type);
|
|
if (ret)
|
|
goto sysfs_fail;
|
|
|
|
mdev->active = true;
|
|
dev_dbg(&mdev->dev, "MDEV: created\n");
|
|
|
|
return 0;
|
|
|
|
sysfs_fail:
|
|
device_del(&mdev->dev);
|
|
add_fail:
|
|
parent->ops->remove(mdev);
|
|
ops_create_fail:
|
|
put_device(&mdev->dev);
|
|
mdev_fail:
|
|
mdev_put_parent(parent);
|
|
return ret;
|
|
}
|
|
|
|
int mdev_device_remove(struct device *dev)
|
|
{
|
|
struct mdev_device *mdev, *tmp;
|
|
struct mdev_parent *parent;
|
|
struct mdev_type *type;
|
|
int ret;
|
|
|
|
mdev = to_mdev_device(dev);
|
|
|
|
mutex_lock(&mdev_list_lock);
|
|
list_for_each_entry(tmp, &mdev_list, next) {
|
|
if (tmp == mdev)
|
|
break;
|
|
}
|
|
|
|
if (tmp != mdev) {
|
|
mutex_unlock(&mdev_list_lock);
|
|
return -ENODEV;
|
|
}
|
|
|
|
if (!mdev->active) {
|
|
mutex_unlock(&mdev_list_lock);
|
|
return -EAGAIN;
|
|
}
|
|
|
|
mdev->active = false;
|
|
mutex_unlock(&mdev_list_lock);
|
|
|
|
type = to_mdev_type(mdev->type_kobj);
|
|
mdev_remove_sysfs_files(dev, type);
|
|
device_del(&mdev->dev);
|
|
parent = mdev->parent;
|
|
ret = parent->ops->remove(mdev);
|
|
if (ret)
|
|
dev_err(&mdev->dev, "Remove failed: err=%d\n", ret);
|
|
|
|
/* Balances with device_initialize() */
|
|
put_device(&mdev->dev);
|
|
mdev_put_parent(parent);
|
|
|
|
return 0;
|
|
}
|
|
|
|
int mdev_set_iommu_device(struct device *dev, struct device *iommu_device)
|
|
{
|
|
struct mdev_device *mdev = to_mdev_device(dev);
|
|
|
|
mdev->iommu_device = iommu_device;
|
|
|
|
return 0;
|
|
}
|
|
EXPORT_SYMBOL(mdev_set_iommu_device);
|
|
|
|
struct device *mdev_get_iommu_device(struct device *dev)
|
|
{
|
|
struct mdev_device *mdev = to_mdev_device(dev);
|
|
|
|
return mdev->iommu_device;
|
|
}
|
|
EXPORT_SYMBOL(mdev_get_iommu_device);
|
|
|
|
static int __init mdev_init(void)
|
|
{
|
|
return mdev_bus_register();
|
|
}
|
|
|
|
static void __exit mdev_exit(void)
|
|
{
|
|
if (mdev_bus_compat_class)
|
|
class_compat_unregister(mdev_bus_compat_class);
|
|
|
|
mdev_bus_unregister();
|
|
}
|
|
|
|
module_init(mdev_init)
|
|
module_exit(mdev_exit)
|
|
|
|
MODULE_VERSION(DRIVER_VERSION);
|
|
MODULE_LICENSE("GPL v2");
|
|
MODULE_AUTHOR(DRIVER_AUTHOR);
|
|
MODULE_DESCRIPTION(DRIVER_DESC);
|
|
MODULE_SOFTDEP("post: vfio_mdev");
|