public inbox for linux-usb@vger.kernel.org
 help / color / mirror / Atom feed
From: Jeongjun Park <aha310510@gmail.com>
To: oneukum@suse.com
Cc: aha310510@gmail.com, colin.i.king@gmail.com,
	gregkh@linuxfoundation.org, linux-kernel@vger.kernel.org,
	linux-usb@vger.kernel.org
Subject: Re: [PATCH] usb: use mutex_lock in iowarrior_read()
Date: Wed, 18 Sep 2024 00:41:07 +0900	[thread overview]
Message-ID: <20240917154107.137653-1-aha310510@gmail.com> (raw)
In-Reply-To: <d88289f9-e22a-4960-9b3b-ad0b3ab17a89@suse.com>

Oliver Neukum <oneukum@suse.com> wrote:
>
> On 17.09.24 12:01, Jeongjun Park wrote:
>
> > Okay, I understand. Then I think it would be appropriate to do
> > the patch below to prevent blocking, but I have one question.
> >
> > Currently, many misc usb drivers do not seem to handle the
> > O_NONBLOCK flag when using mutex_lock. If this is really
>
> Yes. The quality of many drivers could be improved.
> Feel free to make patches. However, the lack of quality elsewhere
> does not justify a regression. Hence code fixing drivers already
> correctly supporting O_NONBLOCK must be correct in that regard.
>
> > necessary code, I think it would require code modifications to
> > other functions inside iowarrior and many misc usb drivers.
> >
> > What do you think about this?
>
> The formatting seems to be broken. In terms of content it is good.

Oh, this format was sent broken. I'm sending you the patch below again.
I'll send you a new patch with this patch right away.

And I'll try to write a patch for other functions and misc usbs that don't
support O_NONBLOCK properly soon.

Regards,
Jeongjun Park

---
 drivers/usb/misc/iowarrior.c | 46 ++++++++++++++++++++++++++++--------
 1 file changed, 36 insertions(+), 10 deletions(-)

diff --git a/drivers/usb/misc/iowarrior.c b/drivers/usb/misc/iowarrior.c
index 6d28467ce352..dbf0ed04f7c3 100644
--- a/drivers/usb/misc/iowarrior.c
+++ b/drivers/usb/misc/iowarrior.c
@@ -277,28 +277,45 @@ 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 (file->f_flags & O_NONBLOCK) {
+		retval = mutex_trylock(&dev->mutex);
+		if (!retval)
+			return -EAGAIN;
+	} else {
+		retval = mutex_lock_interruptible(&dev->mutex);
+		if (retval)
+			return -ERESTARTSYS;
+	}
+
 	/* verify that the device wasn't unplugged */
-	if (!dev || !dev->present)
-		return -ENODEV;
+	if (!dev->present) {
+		retval = -ENODEV;
+		goto exit;
+	}
 
 	dev_dbg(&dev->interface->dev, "minor %d, count = %zd\n",
 		dev->minor, count);
 
 	/* read count must be packet size (+ time stamp) */
 	if ((count != dev->report_size)
-	    && (count != (dev->report_size + 1)))
-		return -EINVAL;
+	    && (count != (dev->report_size + 1))) {
+		retval = -EINVAL;
+		goto exit;
+	}
 
 	/* repeat until no buffer overrun in callback handler occur */
 	do {
 		atomic_set(&dev->overflow_flag, 0);
 		if ((read_idx = read_index(dev)) == -1) {
 			/* queue empty */
-			if (file->f_flags & O_NONBLOCK)
-				return -EAGAIN;
+			if (file->f_flags & O_NONBLOCK) {
+				retval = -EAGAIN;
+				goto exit;
+			}
 			else {
 				//next line will return when there is either new data, or the device is unplugged
 				int r = wait_event_interruptible(dev->read_wait,
@@ -309,28 +326,37 @@ static ssize_t iowarrior_read(struct file *file, char __user *buffer,
 								  -1));
 				if (r) {
 					//we were interrupted by a signal
-					return -ERESTART;
+					retval = -ERESTART;
+					goto exit;
 				}
 				if (!dev->present) {
 					//The device was unplugged
-					return -ENODEV;
+					retval = -ENODEV;
+					goto exit;
 				}
 				if (read_idx == -1) {
 					// Can this happen ???
-					return 0;
+					retval = 0;
+					goto exit;
 				}
 			}
 		}
 
 		offset = read_idx * (dev->report_size + 1);
 		if (copy_to_user(buffer, dev->read_queue + offset, count)) {
-			return -EFAULT;
+			retval = -EFAULT;
+			goto exit;
 		}
 	} while (atomic_read(&dev->overflow_flag));
 
 	read_idx = ++read_idx == MAX_INTERRUPT_BUFFER ? 0 : read_idx;
 	atomic_set(&dev->read_idx, read_idx);
+	mutex_unlock(&dev->mutex);
 	return count;
+
+exit:
+	mutex_unlock(&dev->mutex);
+	return retval;
 }
 
 /*
--

>
>         Regards
>                 Oliver

  reply	other threads:[~2024-09-17 15:41 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-16  4:06 [PATCH] usb: use mutex_lock in iowarrior_read() Jeongjun Park
2024-09-16  4:15 ` Greg KH
2024-09-16  4:43   ` Jeongjun Park
2024-09-16  6:50     ` Greg KH
2024-09-16  8:35   ` Oliver Neukum
2024-09-16 12:44     ` Jeongjun Park
2024-09-16 13:15       ` Oliver Neukum
2024-09-17  6:23         ` Jeongjun Park
2024-09-17  8:33           ` Oliver Neukum
2024-09-17 10:01             ` Jeongjun Park
2024-09-17 13:17               ` Oliver Neukum
2024-09-17 15:41                 ` Jeongjun Park [this message]
2024-09-17 16:02                   ` Oliver Neukum
2024-09-17 16:09                     ` Jeongjun Park

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240917154107.137653-1-aha310510@gmail.com \
    --to=aha310510@gmail.com \
    --cc=colin.i.king@gmail.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=oneukum@suse.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox