aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/vfio/mdev/mdev_core.c
diff options
context:
space:
mode:
authorParav Pandit <parav@mellanox.com>2019-06-06 12:52:32 -0400
committerAlex Williamson <alex.williamson@redhat.com>2019-06-06 12:52:32 -0400
commit522ecce08ab20b57342d65b05601818e0f95fb2c (patch)
tree48becc81ae75faeb15d4f7bf66820f4d2bc994bc /drivers/vfio/mdev/mdev_core.c
parentf2c7c76c5d0a443053e94adb9f0918fa2fb85c3a (diff)
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>
Diffstat (limited to 'drivers/vfio/mdev/mdev_core.c')
-rw-r--r--drivers/vfio/mdev/mdev_core.c94
1 files changed, 25 insertions, 69 deletions
diff --git a/drivers/vfio/mdev/mdev_core.c b/drivers/vfio/mdev/mdev_core.c
index 3cc1a05fde1c..0bef0cae1d4b 100644
--- a/drivers/vfio/mdev/mdev_core.c
+++ b/drivers/vfio/mdev/mdev_core.c
@@ -102,55 +102,10 @@ static void mdev_put_parent(struct mdev_parent *parent)
102 kref_put(&parent->ref, mdev_release_parent); 102 kref_put(&parent->ref, mdev_release_parent);
103} 103}
104 104
105static int mdev_device_create_ops(struct kobject *kobj,
106 struct mdev_device *mdev)
107{
108 struct mdev_parent *parent = mdev->parent;
109 int ret;
110
111 ret = parent->ops->create(kobj, mdev);
112 if (ret)
113 return ret;
114
115 ret = sysfs_create_groups(&mdev->dev.kobj,
116 parent->ops->mdev_attr_groups);
117 if (ret)
118 parent->ops->remove(mdev);
119
120 return ret;
121}
122
123/*
124 * mdev_device_remove_ops gets called from sysfs's 'remove' and when parent
125 * device is being unregistered from mdev device framework.
126 * - 'force_remove' is set to 'false' when called from sysfs's 'remove' which
127 * indicates that if the mdev device is active, used by VMM or userspace
128 * application, vendor driver could return error then don't remove the device.
129 * - 'force_remove' is set to 'true' when called from mdev_unregister_device()
130 * which indicate that parent device is being removed from mdev device
131 * framework so remove mdev device forcefully.
132 */
133static int mdev_device_remove_ops(struct mdev_device *mdev, bool force_remove)
134{
135 struct mdev_parent *parent = mdev->parent;
136 int ret;
137
138 /*
139 * Vendor driver can return error if VMM or userspace application is
140 * using this mdev device.
141 */
142 ret = parent->ops->remove(mdev);
143 if (ret && !force_remove)
144 return ret;
145
146 sysfs_remove_groups(&mdev->dev.kobj, parent->ops->mdev_attr_groups);
147 return 0;
148}
149
150static int mdev_device_remove_cb(struct device *dev, void *data) 105static int mdev_device_remove_cb(struct device *dev, void *data)
151{ 106{
152 if (dev_is_mdev(dev)) 107 if (dev_is_mdev(dev))
153 mdev_device_remove(dev, true); 108 mdev_device_remove(dev);
154 109
155 return 0; 110 return 0;
156} 111}
@@ -310,41 +265,43 @@ int mdev_device_create(struct kobject *kobj,
310 265
311 mdev->parent = parent; 266 mdev->parent = parent;
312 267
268 device_initialize(&mdev->dev);
313 mdev->dev.parent = dev; 269 mdev->dev.parent = dev;
314 mdev->dev.bus = &mdev_bus_type; 270 mdev->dev.bus = &mdev_bus_type;
315 mdev->dev.release = mdev_device_release; 271 mdev->dev.release = mdev_device_release;
316 dev_set_name(&mdev->dev, "%pUl", uuid); 272 dev_set_name(&mdev->dev, "%pUl", uuid);
273 mdev->dev.groups = parent->ops->mdev_attr_groups;
274 mdev->type_kobj = kobj;
317 275
318 ret = device_register(&mdev->dev); 276 ret = parent->ops->create(kobj, mdev);
319 if (ret) { 277 if (ret)
320 put_device(&mdev->dev); 278 goto ops_create_fail;
321 goto mdev_fail;
322 }
323 279
324 ret = mdev_device_create_ops(kobj, mdev); 280 ret = device_add(&mdev->dev);
325 if (ret) 281 if (ret)
326 goto create_fail; 282 goto add_fail;
327 283
328 ret = mdev_create_sysfs_files(&mdev->dev, type); 284 ret = mdev_create_sysfs_files(&mdev->dev, type);
329 if (ret) { 285 if (ret)
330 mdev_device_remove_ops(mdev, true); 286 goto sysfs_fail;
331 goto create_fail;
332 }
333 287
334 mdev->type_kobj = kobj;
335 mdev->active = true; 288 mdev->active = true;
336 dev_dbg(&mdev->dev, "MDEV: created\n"); 289 dev_dbg(&mdev->dev, "MDEV: created\n");
337 290
338 return 0; 291 return 0;
339 292
340create_fail: 293sysfs_fail:
341 device_unregister(&mdev->dev); 294 device_del(&mdev->dev);
295add_fail:
296 parent->ops->remove(mdev);
297ops_create_fail:
298 put_device(&mdev->dev);
342mdev_fail: 299mdev_fail:
343 mdev_put_parent(parent); 300 mdev_put_parent(parent);
344 return ret; 301 return ret;
345} 302}
346 303
347int mdev_device_remove(struct device *dev, bool force_remove) 304int mdev_device_remove(struct device *dev)
348{ 305{
349 struct mdev_device *mdev, *tmp; 306 struct mdev_device *mdev, *tmp;
350 struct mdev_parent *parent; 307 struct mdev_parent *parent;
@@ -373,16 +330,15 @@ int mdev_device_remove(struct device *dev, bool force_remove)
373 mutex_unlock(&mdev_list_lock); 330 mutex_unlock(&mdev_list_lock);
374 331
375 type = to_mdev_type(mdev->type_kobj); 332 type = to_mdev_type(mdev->type_kobj);
333 mdev_remove_sysfs_files(dev, type);
334 device_del(&mdev->dev);
376 parent = mdev->parent; 335 parent = mdev->parent;
336 ret = parent->ops->remove(mdev);
337 if (ret)
338 dev_err(&mdev->dev, "Remove failed: err=%d\n", ret);
377 339
378 ret = mdev_device_remove_ops(mdev, force_remove); 340 /* Balances with device_initialize() */
379 if (ret) { 341 put_device(&mdev->dev);
380 mdev->active = true;
381 return ret;
382 }
383
384 mdev_remove_sysfs_files(dev, type);
385 device_unregister(dev);
386 mdev_put_parent(parent); 342 mdev_put_parent(parent);
387 343
388 return 0; 344 return 0;