From: Oliver Neukum <oneukum@suse.com>
To: Jeongjun Park <aha310510@gmail.com>, Oliver Neukum <oneukum@suse.com>
Cc: Greg KH <gregkh@linuxfoundation.org>,
colin.i.king@gmail.com, linux-usb@vger.kernel.org,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH] usb: use mutex_lock in iowarrior_read()
Date: Mon, 16 Sep 2024 15:15:57 +0200 [thread overview]
Message-ID: <bf971924-9d91-40a3-a4c2-5b518e2ce2fd@suse.com> (raw)
In-Reply-To: <CAO9qdTHrbG-aWetpM_e7zHUhrwPD=7uCHPbWSMoorgnwjKEOmA@mail.gmail.com>
Hi,
On 16.09.24 14:44, Jeongjun Park wrote:
> Oliver Neukum <oneukum@suse.com> wrote:
>>
>>
>>
>> On 16.09.24 06:15, Greg KH wrote:
>>> On Mon, Sep 16, 2024 at 01:06:29PM +0900, Jeongjun Park wrote:
>>> Please use the guard() form here, it makes the change much simpler and
>>> easier to review and maintain.
>>
>> That would break the O_NONBLOCK case.
>>
>> Looking at the code it indeed looks like iowarrior_read() can race
>> with itself. Strictly speaking it always could happen if a task used
>> fork() after open(). The driver tries to restrict its usage to one
>> thread, but I doubt that the logic is functional.
>>
>> It seems to me the correct fix is something like this:
>
> Well, I don't know why it's necessary to modify it like this.
> I think it would be more appropriate to patch it to make it
> more maintainable by using guard() as Greg suggested.
Allow me to explain detail.
guard() internally uses mutex_lock(). That means that
a) it will block
b) having blocked it will sleep in the state TASK_UNINTERRUPTIBLE
The driver itself uses TASK_INTERRUPTIBLE in iowarrior_read(),
when it waits for IO. That is entirely correct, as it waits for
an external device doing an operation that may never occur. You
must use TASK_INTERRUPTIBLE.
Now, if you use mutex_lock() to wait for a task waiting for IO
to occur in the state TASK_INTERRUPTIBLE, you are indirectlywaiting for
an event that you must wait for in TASK_INTERRUPTIBLE in the state
TASK_UNINTERRUPTIBLE.
That is a bug. You have created a task that cannot be killed (uid may not match),
but may have to be killed. Furthermore you block even in case the
device has been opened with O_NONBLOCK, which is a second bug.
These limitations are inherent in guard(). Therefore you cannot use
guard here.
Regards
Oliver
next prev parent reply other threads:[~2024-09-16 13:16 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 [this message]
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
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=bf971924-9d91-40a3-a4c2-5b518e2ce2fd@suse.com \
--to=oneukum@suse.com \
--cc=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 \
/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