staging: comedi: protect against detach during write operation
The 'write' file operation for comedi devices does not use the main
mutex in the `struct comedi_device` to avoid contention with some ioctls
that may take a while to complete. Use the `attach_lock` semaphore to
protect against detachment while the 'write' operation is in progress.
This is a `struct rw_semaphore` and we read-lock it to protect against
device detachment.
Note that `comedi_device_cancel_all()` is called during device
detachment, which cancels any ongoing asynchronous commands. This will
wake up any blocked writers which will then release the `attach_lock`
semaphore and complete the 'write' operation early.
The only time the 'write' file operation does use the main mutex is at
the end of the command when it has to call `do_become_nonbusy()` to mark
the subdevice as no longer busy handling an asynchronous command. To
avoid deadlock, it has to remove the task from the wait queue and
release the `attach_lock` semaphore before acquiring the main mutex. It
then needs to confirm that the device is still attached. Unfortunately,
we do not yet protect against a dynamically allocated `struct
comedi_device` being deleted during the operation. This will be
addressed by a later patch.
Signed-off-by: Ian Abbott <abbotti@mev.co.uk>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>