2019-06-04 16:11:33 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2016-11-17 04:46:13 +08:00
|
|
|
/*
|
|
|
|
* File attributes for Mediated devices
|
|
|
|
*
|
|
|
|
* Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
|
|
|
|
* Author: Neo Jia <cjia@nvidia.com>
|
|
|
|
* Kirti Wankhede <kwankhede@nvidia.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/sysfs.h>
|
|
|
|
#include <linux/ctype.h>
|
|
|
|
#include <linux/device.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/uuid.h>
|
|
|
|
#include <linux/mdev.h>
|
|
|
|
|
|
|
|
#include "mdev_private.h"
|
|
|
|
|
|
|
|
/* Static functions */
|
|
|
|
|
|
|
|
static ssize_t mdev_type_attr_show(struct kobject *kobj,
|
|
|
|
struct attribute *__attr, char *buf)
|
|
|
|
{
|
|
|
|
struct mdev_type_attribute *attr = to_mdev_type_attr(__attr);
|
|
|
|
struct mdev_type *type = to_mdev_type(kobj);
|
|
|
|
ssize_t ret = -EIO;
|
|
|
|
|
|
|
|
if (attr->show)
|
|
|
|
ret = attr->show(kobj, type->parent->dev, buf);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t mdev_type_attr_store(struct kobject *kobj,
|
|
|
|
struct attribute *__attr,
|
|
|
|
const char *buf, size_t count)
|
|
|
|
{
|
|
|
|
struct mdev_type_attribute *attr = to_mdev_type_attr(__attr);
|
|
|
|
struct mdev_type *type = to_mdev_type(kobj);
|
|
|
|
ssize_t ret = -EIO;
|
|
|
|
|
|
|
|
if (attr->store)
|
|
|
|
ret = attr->store(&type->kobj, type->parent->dev, buf, count);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct sysfs_ops mdev_type_sysfs_ops = {
|
|
|
|
.show = mdev_type_attr_show,
|
|
|
|
.store = mdev_type_attr_store,
|
|
|
|
};
|
|
|
|
|
|
|
|
static ssize_t create_store(struct kobject *kobj, struct device *dev,
|
|
|
|
const char *buf, size_t count)
|
|
|
|
{
|
|
|
|
char *str;
|
2019-01-11 03:00:27 +08:00
|
|
|
guid_t uuid;
|
2016-11-17 04:46:13 +08:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
if ((count < UUID_STRING_LEN) || (count > UUID_STRING_LEN + 1))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
str = kstrndup(buf, count, GFP_KERNEL);
|
|
|
|
if (!str)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2019-01-11 03:00:27 +08:00
|
|
|
ret = guid_parse(str, &uuid);
|
2016-11-17 04:46:13 +08:00
|
|
|
kfree(str);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2019-01-11 03:00:27 +08:00
|
|
|
ret = mdev_device_create(kobj, dev, &uuid);
|
2016-11-17 04:46:13 +08:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2019-12-18 20:31:19 +08:00
|
|
|
static MDEV_TYPE_ATTR_WO(create);
|
2016-11-17 04:46:13 +08:00
|
|
|
|
|
|
|
static void mdev_type_release(struct kobject *kobj)
|
|
|
|
{
|
|
|
|
struct mdev_type *type = to_mdev_type(kobj);
|
|
|
|
|
|
|
|
pr_debug("Releasing group %s\n", kobj->name);
|
|
|
|
kfree(type);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct kobj_type mdev_type_ktype = {
|
|
|
|
.sysfs_ops = &mdev_type_sysfs_ops,
|
|
|
|
.release = mdev_type_release,
|
|
|
|
};
|
|
|
|
|
2018-11-13 16:45:43 +08:00
|
|
|
static struct mdev_type *add_mdev_supported_type(struct mdev_parent *parent,
|
|
|
|
struct attribute_group *group)
|
2016-11-17 04:46:13 +08:00
|
|
|
{
|
|
|
|
struct mdev_type *type;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!group->name) {
|
|
|
|
pr_err("%s: Type name empty!\n", __func__);
|
|
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
type = kzalloc(sizeof(*type), GFP_KERNEL);
|
|
|
|
if (!type)
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
|
|
|
|
type->kobj.kset = parent->mdev_types_kset;
|
2021-04-07 03:40:25 +08:00
|
|
|
type->parent = parent;
|
2016-11-17 04:46:13 +08:00
|
|
|
|
|
|
|
ret = kobject_init_and_add(&type->kobj, &mdev_type_ktype, NULL,
|
|
|
|
"%s-%s", dev_driver_string(parent->dev),
|
|
|
|
group->name);
|
|
|
|
if (ret) {
|
2020-05-28 10:01:09 +08:00
|
|
|
kobject_put(&type->kobj);
|
2016-11-17 04:46:13 +08:00
|
|
|
return ERR_PTR(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = sysfs_create_file(&type->kobj, &mdev_type_attr_create.attr);
|
|
|
|
if (ret)
|
|
|
|
goto attr_create_failed;
|
|
|
|
|
|
|
|
type->devices_kobj = kobject_create_and_add("devices", &type->kobj);
|
|
|
|
if (!type->devices_kobj) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto attr_devices_failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = sysfs_create_files(&type->kobj,
|
|
|
|
(const struct attribute **)group->attrs);
|
|
|
|
if (ret) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto attrs_failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
type->group = group;
|
|
|
|
return type;
|
|
|
|
|
|
|
|
attrs_failed:
|
|
|
|
kobject_put(type->devices_kobj);
|
|
|
|
attr_devices_failed:
|
|
|
|
sysfs_remove_file(&type->kobj, &mdev_type_attr_create.attr);
|
|
|
|
attr_create_failed:
|
|
|
|
kobject_del(&type->kobj);
|
|
|
|
kobject_put(&type->kobj);
|
|
|
|
return ERR_PTR(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void remove_mdev_supported_type(struct mdev_type *type)
|
|
|
|
{
|
|
|
|
sysfs_remove_files(&type->kobj,
|
|
|
|
(const struct attribute **)type->group->attrs);
|
|
|
|
kobject_put(type->devices_kobj);
|
|
|
|
sysfs_remove_file(&type->kobj, &mdev_type_attr_create.attr);
|
|
|
|
kobject_del(&type->kobj);
|
|
|
|
kobject_put(&type->kobj);
|
|
|
|
}
|
|
|
|
|
2016-12-30 23:13:38 +08:00
|
|
|
static int add_mdev_supported_type_groups(struct mdev_parent *parent)
|
2016-11-17 04:46:13 +08:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; parent->ops->supported_type_groups[i]; i++) {
|
|
|
|
struct mdev_type *type;
|
|
|
|
|
|
|
|
type = add_mdev_supported_type(parent,
|
|
|
|
parent->ops->supported_type_groups[i]);
|
|
|
|
if (IS_ERR(type)) {
|
|
|
|
struct mdev_type *ltype, *tmp;
|
|
|
|
|
|
|
|
list_for_each_entry_safe(ltype, tmp, &parent->type_list,
|
|
|
|
next) {
|
|
|
|
list_del(<ype->next);
|
|
|
|
remove_mdev_supported_type(ltype);
|
|
|
|
}
|
|
|
|
return PTR_ERR(type);
|
|
|
|
}
|
|
|
|
list_add(&type->next, &parent->type_list);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* mdev sysfs functions */
|
2016-12-30 23:13:38 +08:00
|
|
|
void parent_remove_sysfs_files(struct mdev_parent *parent)
|
2016-11-17 04:46:13 +08:00
|
|
|
{
|
|
|
|
struct mdev_type *type, *tmp;
|
|
|
|
|
|
|
|
list_for_each_entry_safe(type, tmp, &parent->type_list, next) {
|
|
|
|
list_del(&type->next);
|
|
|
|
remove_mdev_supported_type(type);
|
|
|
|
}
|
|
|
|
|
|
|
|
sysfs_remove_groups(&parent->dev->kobj, parent->ops->dev_attr_groups);
|
|
|
|
kset_unregister(parent->mdev_types_kset);
|
|
|
|
}
|
|
|
|
|
2016-12-30 23:13:38 +08:00
|
|
|
int parent_create_sysfs_files(struct mdev_parent *parent)
|
2016-11-17 04:46:13 +08:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
parent->mdev_types_kset = kset_create_and_add("mdev_supported_types",
|
|
|
|
NULL, &parent->dev->kobj);
|
|
|
|
|
|
|
|
if (!parent->mdev_types_kset)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
INIT_LIST_HEAD(&parent->type_list);
|
|
|
|
|
|
|
|
ret = sysfs_create_groups(&parent->dev->kobj,
|
|
|
|
parent->ops->dev_attr_groups);
|
|
|
|
if (ret)
|
|
|
|
goto create_err;
|
|
|
|
|
|
|
|
ret = add_mdev_supported_type_groups(parent);
|
|
|
|
if (ret)
|
|
|
|
sysfs_remove_groups(&parent->dev->kobj,
|
|
|
|
parent->ops->dev_attr_groups);
|
|
|
|
else
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
create_err:
|
|
|
|
kset_unregister(parent->mdev_types_kset);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t remove_store(struct device *dev, struct device_attribute *attr,
|
|
|
|
const char *buf, size_t count)
|
|
|
|
{
|
|
|
|
unsigned long val;
|
|
|
|
|
|
|
|
if (kstrtoul(buf, 0, &val) < 0)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (val && device_remove_file_self(dev, attr)) {
|
|
|
|
int ret;
|
|
|
|
|
vfio/mdev: Improve the create/remove sequence
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>
2019-06-07 00:52:32 +08:00
|
|
|
ret = mdev_device_remove(dev);
|
2019-06-07 00:52:33 +08:00
|
|
|
if (ret)
|
2016-11-17 04:46:13 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
static DEVICE_ATTR_WO(remove);
|
|
|
|
|
|
|
|
static const struct attribute *mdev_device_attrs[] = {
|
|
|
|
&dev_attr_remove.attr,
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
|
|
|
int mdev_create_sysfs_files(struct device *dev, struct mdev_type *type)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = sysfs_create_link(type->devices_kobj, &dev->kobj, dev_name(dev));
|
|
|
|
if (ret)
|
2018-05-19 01:40:33 +08:00
|
|
|
return ret;
|
2016-11-17 04:46:13 +08:00
|
|
|
|
|
|
|
ret = sysfs_create_link(&dev->kobj, &type->kobj, "mdev_type");
|
|
|
|
if (ret)
|
|
|
|
goto type_link_failed;
|
|
|
|
|
2018-05-19 01:40:33 +08:00
|
|
|
ret = sysfs_create_files(&dev->kobj, mdev_device_attrs);
|
|
|
|
if (ret)
|
|
|
|
goto create_files_failed;
|
|
|
|
|
2016-11-17 04:46:13 +08:00
|
|
|
return ret;
|
|
|
|
|
2018-05-19 01:40:33 +08:00
|
|
|
create_files_failed:
|
|
|
|
sysfs_remove_link(&dev->kobj, "mdev_type");
|
2016-11-17 04:46:13 +08:00
|
|
|
type_link_failed:
|
|
|
|
sysfs_remove_link(type->devices_kobj, dev_name(dev));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void mdev_remove_sysfs_files(struct device *dev, struct mdev_type *type)
|
|
|
|
{
|
2019-05-01 06:49:32 +08:00
|
|
|
sysfs_remove_files(&dev->kobj, mdev_device_attrs);
|
2016-11-17 04:46:13 +08:00
|
|
|
sysfs_remove_link(&dev->kobj, "mdev_type");
|
|
|
|
sysfs_remove_link(type->devices_kobj, dev_name(dev));
|
|
|
|
}
|