From: Prashanth K <quic_prashk@quicinc.com>
To: Alan Stern <stern@rowland.harvard.edu>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
Christophe JAILLET <christophe.jaillet@wanadoo.fr>,
Xiu Jianfeng <xiujianfeng@huawei.com>,
Pratham Pratap <quic_ppratap@quicinc.com>,
Jack Pham <quic_jackp@quicinc.com>, <linux-usb@vger.kernel.org>,
<linux-kernel@vger.kernel.org>
Subject: Re: [PATCH] usb: gadget: u_serial: Add null pointer check in gserial_resume
Date: Fri, 10 Feb 2023 11:52:23 +0530 [thread overview]
Message-ID: <53d48954-3f7e-fd02-5e8e-2912c16565b3@quicinc.com> (raw)
In-Reply-To: <Y+VgHdJjrd0ZvY33@rowland.harvard.edu>
On 10-02-23 02:35 am, Alan Stern wrote:
> On Thu, Feb 09, 2023 at 11:57:17PM +0530, Prashanth K wrote:
>>
>>
>> On 09-02-23 09:33 pm, Alan Stern wrote:
>>> On Thu, Feb 09, 2023 at 09:13:37PM +0530, Prashanth K wrote:
>>>>
>>>>
>>>> On 09-02-23 08:39 pm, Alan Stern wrote:
>>>>> You should consider having _two_ spinlocks: One in the gs_port structure
>>>>> (the way it is now) and a separate global lock. The first would be used
>>>>> in situations where you know you have a valid pointer. The second would
>>>>> be used in situations where you don't know if the pointer is non-NULL
>>>>> or where you are changing the pointer's value.
>>>> Lets say we replaced the existing spinlock in gserial_resume and
>>>> gserial_disconnect with a new static spinlock, and kept the spinlocks in
>>>> other functions unchanged. In that case, wouldn't it cause additional race
>>>> conditions as we are using 2 different locks.
>>>
>>> Not race conditions, but possibilities for deadlock.
>>>
>>> Indeed, you would have to be very careful about avoiding deadlock
>>> scenarios. In particular, you would have to ensure that the code never
>>> tries to acquire the global spinlock while already holding one of the
>>> per-port spinlocks.
>>>
>>> Alan Stern
>> Hi Alan, instead of doing these and causing potential regressions, can we
>> just have the null pointer check which i suggested in the beginning? The
>> major concern was that port might become null after the null pointer check.
>
> What you are describing is a data race: gserial_disconnect() can write
> to gser->ioport at the same time that gserial_resume() reads from it.
> Unless you're working on a fast path -- which this isn't -- you should
> strive to avoid data races by using proper locking. That means adding
> the extra spinlock, or finding some other way to make these two accesses
> be mutually exclusive.
>
> With a little care you can ensure there won't be any regressions. Just
> do what I said above: Make sure the code never tries to acquire the
> global spinlock while already holding one of the per-port spinlocks.
>
>> We mark gser->ioport as null pointer in gserial_disconnect, and in
>> gserial_resume we copy the gser->ioport to *port in the beginning.
>>
>> struct gs_port *port = gser->ioport;
>>
>> And hence it wont cause null pointer deref after the check as we don't
>> de-reference anything from gser->ioport afterwards. We only use the local
>> pointer *port afterwards.
>
> You cannot depend on this to work the way you want. The compiler will
> optimize your source code, and one of the optimizations might be to
> eliminate the "port" variable entirely and replace it with gser->ioport.
>
> Alan Stern
Hi Alan, Thanks for the detailed info. I checked and included few cases
here.
This would cause a deadlock if gserial_disconnect acquires port_lock and
gserial_resume acquires static_lock.
gserial_disconnect {
spin_lock(port)
...
spin_lock(static)
gser->ioport = NULL;
spin_unlock(static)
...
spin_unlock(port)
}
gserial_resume {
struct gs_port *port = gser->ioport;
spin_lock(static)
if (!port)
return
spin_lock(port)
spin_unlock(static)
...
spin_unlock(port)
}
------------------------------------------------------------------
This would cause additional races when gserial_disconnect releases
port_lock and some other functions acquire it.
gserial_disconnect {
spin_lock(port)
...
spin_unlock(port)
spin_lock(static)
gser->ioport = NULL;
spin_unlock(static)
spin_lock(port)
...
spin_unlock(port)
}
gserial_resume {
struct gs_port *port = gser->ioport;
spin_lock(static)
if (!port)
return
spin_lock(port)
spin_unlock(static)
...
spin_unlock(port)
}
------------------------------------------------------------------
And this seems like a viable option to me, what do you suggest?
gserial_disconnect {
spin_lock(static)
spin_lock(port)
...
gser->ioport = NULL;
...
spin_lock(port)
spin_unlock(static)
}
gserial_resume {
struct gs_port *port = gser->ioport;
spin_lock(static)
if (!port)
return
spin_lock(port)
...
spin_unlock(port)
spin_unlock(static)
}
Thanks,
Prashanth K
next prev parent reply other threads:[~2023-02-10 6:22 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-02-08 13:54 [PATCH] usb: gadget: u_serial: Add null pointer check in gserial_resume Prashanth K
2023-02-08 14:54 ` Greg Kroah-Hartman
2023-02-08 15:45 ` Prashanth K
2023-02-08 20:21 ` Alan Stern
2023-02-09 5:01 ` Prashanth K
2023-02-09 7:01 ` Greg Kroah-Hartman
2023-02-09 7:03 ` Prashanth K
2023-02-09 14:07 ` Prashanth K
2023-02-09 15:09 ` Alan Stern
2023-02-09 15:43 ` Prashanth K
2023-02-09 16:03 ` Alan Stern
2023-02-09 18:27 ` Prashanth K
2023-02-09 21:05 ` Alan Stern
2023-02-10 6:22 ` Prashanth K [this message]
2023-02-10 6:56 ` Prashanth K
2023-02-10 15:47 ` Alan Stern
2023-02-11 6:25 ` Prashanth K
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=53d48954-3f7e-fd02-5e8e-2912c16565b3@quicinc.com \
--to=quic_prashk@quicinc.com \
--cc=christophe.jaillet@wanadoo.fr \
--cc=gregkh@linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-usb@vger.kernel.org \
--cc=quic_jackp@quicinc.com \
--cc=quic_ppratap@quicinc.com \
--cc=stern@rowland.harvard.edu \
--cc=xiujianfeng@huawei.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