Commit 8f988d87 authored by Ian Abbott's avatar Ian Abbott Committed by Greg Kroah-Hartman

staging/comedi: keep reference to class device after destroyed

When a dynamically allocated `struct comedi_device` gets automatically
unconfigured by a call to `comedi_auto_unconfig()` from a lower-level
driver's bus removal function (e.g. when a USB device is disconnected),
the class device in `dev->class_dev` (where `dev` points to the `struct
comedi_device`) is destroyed by a call to `device_destroy()` that
matches a previous call to `device_create()`.

However, if the `struct comedi_device` is still associated with an open
file object, the now invalid `dev->class_dev` pointer may still be
passed to `dev_printk()` (via `dev_dbg()` etc.), producing bogus output
or worse.

To fix this, call `get_device()` on the class device if
`device_create()` was successful.  Add a matching call to `put_device()`
in `comedi_dev_kref_release()` when the `struct comedi_device` is freed.
The calls to `dev_dbg()` etc. after the call to `device_destroy()` will
still produce valid output, although the device will have been
unregistered in sysfs.
Signed-off-by: default avatarIan Abbott <abbotti@mev.co.uk>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent a3b22220
...@@ -91,6 +91,7 @@ static void comedi_dev_kref_release(struct kref *kref) ...@@ -91,6 +91,7 @@ static void comedi_dev_kref_release(struct kref *kref)
container_of(kref, struct comedi_device, refcount); container_of(kref, struct comedi_device, refcount);
mutex_destroy(&dev->mutex); mutex_destroy(&dev->mutex);
put_device(dev->class_dev);
kfree(dev); kfree(dev);
} }
...@@ -2514,7 +2515,7 @@ struct comedi_device *comedi_alloc_board_minor(struct device *hardware_device) ...@@ -2514,7 +2515,7 @@ struct comedi_device *comedi_alloc_board_minor(struct device *hardware_device)
csdev = device_create(comedi_class, hardware_device, csdev = device_create(comedi_class, hardware_device,
MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i", i); MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i", i);
if (!IS_ERR(csdev)) if (!IS_ERR(csdev))
dev->class_dev = csdev; dev->class_dev = get_device(csdev);
/* Note: dev->mutex needs to be unlocked by the caller. */ /* Note: dev->mutex needs to be unlocked by the caller. */
return dev; return dev;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment