Commit 2f3fdcd7 authored by Ian Abbott's avatar Ian Abbott Committed by Greg Kroah-Hartman

staging: comedi: add rw_semaphore to protect against device detachment

The 'read' and 'write' file operations on comedi devices do not use the
main mutex in the `struct comedi_device` to avoid contention with ioctls
that may take a while to complete.  However, it is necessary to protect
against the device being detached while the operation is in progress.
Add member `struct rw_semaphore attach_lock` to `struct comedi_device`
for this purpose and initialize it on creation.

The actual locking and unlocking will be implemented by subsequent
patches.  Tasks that are attaching or detaching comedi devices will
write-acquire the new semaphore whilst also holding the main mutex in
the `struct comedi_device`.  Tasks that wish to protect against the
comedi device being detached need to acquire either the main mutex, or
read-acquire the new semaphore, or both in that order.
Signed-off-by: default avatarIan Abbott <abbotti@mev.co.uk>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent ab3cb2e3
...@@ -91,6 +91,7 @@ static void comedi_device_init(struct comedi_device *dev) ...@@ -91,6 +91,7 @@ static void comedi_device_init(struct comedi_device *dev)
{ {
spin_lock_init(&dev->spinlock); spin_lock_init(&dev->spinlock);
mutex_init(&dev->mutex); mutex_init(&dev->mutex);
init_rwsem(&dev->attach_lock);
dev->minor = -1; dev->minor = -1;
} }
......
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
#include <linux/dma-mapping.h> #include <linux/dma-mapping.h>
#include <linux/mutex.h> #include <linux/mutex.h>
#include <linux/spinlock_types.h> #include <linux/spinlock_types.h>
#include <linux/rwsem.h>
#include "comedi.h" #include "comedi.h"
...@@ -184,6 +185,7 @@ struct comedi_device { ...@@ -184,6 +185,7 @@ struct comedi_device {
bool ioenabled:1; bool ioenabled:1;
spinlock_t spinlock; spinlock_t spinlock;
struct mutex mutex; struct mutex mutex;
struct rw_semaphore attach_lock;
int n_subdevices; int n_subdevices;
struct comedi_subdevice *subdevices; struct comedi_subdevice *subdevices;
......
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