Re: [PATCH] usb: use mutex_lock in iowarrior_read()

Oliver Neukum
2024-09-17T08:33:24Z

On 17.09.24 08:23, Jeongjun Park wrote:
> Oliver Neukum <oneukum@suse.com> wrote:

> Okay. But O_NONBLOCK flag check already exists, and I don't know
> if we need to branch separately to mutex_trylock just because O_NONBLOCK
> flag exists. I think mutex_lock_interruptible is enough.

It will still block.

> And the point of locking is too late. I think it would be more appropriate to
> read file->private_data and then lock it right away.

You are right. dev->present should be checked under the lock only.

> I think this patch is a more appropriate patch:
>
> ---
> drivers/usb/misc/iowarrior.c | 41 +++++++++++++++++++++++++++---------
> 1 file changed, 31 insertions(+), 10 deletions(-)
>
> diff --git a/drivers/usb/misc/iowarrior.c b/drivers/usb/misc/iowarrior.c
> index 6d28467ce352..6fb4ecebbc15 100644
> --- a/drivers/usb/misc/iowarrior.c
> +++ b/drivers/usb/misc/iowarrior.c
> @@ -277,28 +277,40 @@ static ssize_t iowarrior_read(struct file *file,
> char __user *buffer,
> struct iowarrior *dev;
> int read_idx;
> int offset;
> + int retval = 0;
>
> dev = file->private_data;
>
> + if (mutex_lock_interruptible(&dev->mutex)) {

This blocks. To quote the man page:

O_NONBLOCK or O_NDELAY
When possible, the file is opened in nonblocking mode.
Neither the open() nor any subsequent I/O operations on the file descriptor which is
returned will cause the calling process to wait.

[..]
> +unlock_exit:
> + mutex_unlock(&dev->mutex);
> +exit:
> + return retval;

The rest looks good to me.

Regards
Oliver