* [BUG] staging: wfx: possible deadlock in wfx_conf_tx() and wfx_add_interface()
@ 2022-02-01 7:09 Jia-Ju Bai
2022-02-01 11:33 ` Hillf Danton
0 siblings, 1 reply; 4+ messages in thread
From: Jia-Ju Bai @ 2022-02-01 7:09 UTC (permalink / raw)
To: jerome.pouiller, Greg KH; +Cc: linux-staging, linux-kernel
Hello,
My static analysis tool reports a possible deadlock in the wfx driver in
Linux 5.16:
wfx_conf_tx()
mutex_lock(&wdev->conf_mutex); --> Line 225 (Lock A)
wfx_update_pm()
wait_for_completion_timeout(&wvif->set_pm_mode_complete, ...); -->
Line 3019 (Wait X)
wfx_add_interface()
mutex_lock(&wdev->conf_mutex); --> Line 737 (Lock A)
complete(&wvif->set_pm_mode_complete); --> Line 758 (Wake X)
When wfx_conf_tx() is executed, "Wait X" is performed by holding "Lock
A". If wfx_add_interface() is executed at this time, "Wake X" cannot be
performed to wake up "Wait X" in wfx_conf_tx(), because "Lock A" has
been already hold by wfx_conf_tx(), causing a possible deadlock.
I find that "Wait X" is performed with a timeout, to relieve the
possible deadlock; but I think this timeout can cause inefficient execution.
I am not quite sure whether this possible problem is real and how to fix
it if it is real.
Any feedback would be appreciated, thanks :)
Best wishes,
Jia-Ju Bai
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [BUG] staging: wfx: possible deadlock in wfx_conf_tx() and wfx_add_interface()
2022-02-01 7:09 [BUG] staging: wfx: possible deadlock in wfx_conf_tx() and wfx_add_interface() Jia-Ju Bai
@ 2022-02-01 11:33 ` Hillf Danton
2022-02-01 11:56 ` Jérôme Pouiller
2022-02-05 8:34 ` Jia-Ju Bai
0 siblings, 2 replies; 4+ messages in thread
From: Hillf Danton @ 2022-02-01 11:33 UTC (permalink / raw)
To: Jia-Ju Bai; +Cc: jerome.pouiller, Greg KH, linux-staging, linux-kernel
On Tue, 1 Feb 2022 15:09:34 +0800 Jia-Ju Bai wrote:
> Hello,
>
> My static analysis tool reports a possible deadlock in the wfx driver in
> Linux 5.16:
>
> wfx_conf_tx()
> mutex_lock(&wdev->conf_mutex); --> Line 225 (Lock A)
> wfx_update_pm()
> wait_for_completion_timeout(&wvif->set_pm_mode_complete, ...); -->
> Line 3019 (Wait X)
>
> wfx_add_interface()
> mutex_lock(&wdev->conf_mutex); --> Line 737 (Lock A)
> complete(&wvif->set_pm_mode_complete); --> Line 758 (Wake X)
>
> When wfx_conf_tx() is executed, "Wait X" is performed by holding "Lock
> A". If wfx_add_interface() is executed at this time, "Wake X" cannot be
> performed to wake up "Wait X" in wfx_conf_tx(), because "Lock A" has
> been already hold by wfx_conf_tx(), causing a possible deadlock.
> I find that "Wait X" is performed with a timeout, to relieve the
> possible deadlock; but I think this timeout can cause inefficient execution.
>
> I am not quite sure whether this possible problem is real and how to fix
> it if it is real.
> Any feedback would be appreciated, thanks :)
>
>
> Best wishes,
> Jia-Ju Bai
Hey Jia-Ju
Thank you for reporting it.
Given the init_completion() prior to complete() in wfx_add_interface(),
no waiter is waken up by the complete(), so it has nothing to do with
the waiter in the conf path.
BTW if the unusual wfx init is a real use case then we can add a new helper.
Mind introducing your toy to LKML? How much time have you been put in it?
Its current status and future works?
Hillf
+++ x/include/linux/completion.h
@@ -74,6 +74,12 @@ static inline void complete_release(stru
# define DECLARE_COMPLETION_ONSTACK_MAP(work, map) DECLARE_COMPLETION(work)
#endif
+static inline void __init_completion(struct completion *x, unsigned int done)
+{
+ x->done = done;
+ init_swait_queue_head(&x->wait);
+}
+
/**
* init_completion - Initialize a dynamically allocated completion
* @x: pointer to completion structure that is to be initialized
@@ -83,8 +89,12 @@ static inline void complete_release(stru
*/
static inline void init_completion(struct completion *x)
{
- x->done = 0;
- init_swait_queue_head(&x->wait);
+ __init_completion(x, 0);
+}
+
+static inline void init_completion_done(struct completion *x)
+{
+ __init_completion(x, 1);
}
/**
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [BUG] staging: wfx: possible deadlock in wfx_conf_tx() and wfx_add_interface()
2022-02-01 11:33 ` Hillf Danton
@ 2022-02-01 11:56 ` Jérôme Pouiller
2022-02-05 8:34 ` Jia-Ju Bai
1 sibling, 0 replies; 4+ messages in thread
From: Jérôme Pouiller @ 2022-02-01 11:56 UTC (permalink / raw)
To: Jia-Ju Bai, Hillf Danton; +Cc: Greg KH, linux-staging, linux-kernel
Hello,
On Tuesday 1 February 2022 12:33:03 CET Hillf Danton wrote:
> On Tue, 1 Feb 2022 15:09:34 +0800 Jia-Ju Bai wrote:
> > Hello,
> >
> > My static analysis tool reports a possible deadlock in the wfx driver in
> > Linux 5.16:
> >
> > wfx_conf_tx()
> > mutex_lock(&wdev->conf_mutex); --> Line 225 (Lock A)
> > wfx_update_pm()
> > wait_for_completion_timeout(&wvif->set_pm_mode_complete, ...); -->
> > Line 3019 (Wait X)
> >
> > wfx_add_interface()
> > mutex_lock(&wdev->conf_mutex); --> Line 737 (Lock A)
> > complete(&wvif->set_pm_mode_complete); --> Line 758 (Wake X)
> >
> > When wfx_conf_tx() is executed, "Wait X" is performed by holding "Lock
> > A". If wfx_add_interface() is executed at this time, "Wake X" cannot be
> > performed to wake up "Wait X" in wfx_conf_tx(), because "Lock A" has
> > been already hold by wfx_conf_tx(), causing a possible deadlock.
> > I find that "Wait X" is performed with a timeout, to relieve the
> > possible deadlock; but I think this timeout can cause inefficient execution.
> >
> > I am not quite sure whether this possible problem is real and how to fix
> > it if it is real.
> > Any feedback would be appreciated, thanks :)
> >
> >
> > Best wishes,
> > Jia-Ju Bai
>
> Hey Jia-Ju
>
> Thank you for reporting it.
>
> Given the init_completion() prior to complete() in wfx_add_interface(),
> no waiter is waken up by the complete(), so it has nothing to do with
> the waiter in the conf path.
Absolutely. The completion is done by wfx_hif_pm_mode_complete_indication()
(which is not behind a mutex).
> BTW if the unusual wfx init is a real use case then we can add a new helper.
Indeed, it could make the code better. I don't know if there would be other
users.
--
Jérôme Pouiller
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [BUG] staging: wfx: possible deadlock in wfx_conf_tx() and wfx_add_interface()
2022-02-01 11:33 ` Hillf Danton
2022-02-01 11:56 ` Jérôme Pouiller
@ 2022-02-05 8:34 ` Jia-Ju Bai
1 sibling, 0 replies; 4+ messages in thread
From: Jia-Ju Bai @ 2022-02-05 8:34 UTC (permalink / raw)
To: Hillf Danton; +Cc: jerome.pouiller, Greg KH, linux-staging, linux-kernel
On 2022/2/1 19:33, Hillf Danton wrote:
> On Tue, 1 Feb 2022 15:09:34 +0800 Jia-Ju Bai wrote:
>> Hello,
>>
>> My static analysis tool reports a possible deadlock in the wfx driver in
>> Linux 5.16:
>>
>> wfx_conf_tx()
>> mutex_lock(&wdev->conf_mutex); --> Line 225 (Lock A)
>> wfx_update_pm()
>> wait_for_completion_timeout(&wvif->set_pm_mode_complete, ...); -->
>> Line 3019 (Wait X)
>>
>> wfx_add_interface()
>> mutex_lock(&wdev->conf_mutex); --> Line 737 (Lock A)
>> complete(&wvif->set_pm_mode_complete); --> Line 758 (Wake X)
>>
>> When wfx_conf_tx() is executed, "Wait X" is performed by holding "Lock
>> A". If wfx_add_interface() is executed at this time, "Wake X" cannot be
>> performed to wake up "Wait X" in wfx_conf_tx(), because "Lock A" has
>> been already hold by wfx_conf_tx(), causing a possible deadlock.
>> I find that "Wait X" is performed with a timeout, to relieve the
>> possible deadlock; but I think this timeout can cause inefficient execution.
>>
>> I am not quite sure whether this possible problem is real and how to fix
>> it if it is real.
>> Any feedback would be appreciated, thanks :)
>>
>>
>> Best wishes,
>> Jia-Ju Bai
> Hey Jia-Ju
>
> Thank you for reporting it.
>
> Given the init_completion() prior to complete() in wfx_add_interface(),
> no waiter is waken up by the complete(), so it has nothing to do with
> the waiter in the conf path.
Hi Hillf,
Thanks for your reply and detailed explanation :)
>
> BTW if the unusual wfx init is a real use case then we can add a new helper.
>
> Mind introducing your toy to LKML? How much time have you been put in it?
> Its current status and future works?
Do you mean my static analysis tool that generated the report?
In fact, I spent 3-4 months of my part time on implementing this tool,
which can detect deadlocks caused by locking cycles and improper
waiting/waking operations.
This tool still reports some false positives, due to missing some
special patterns in the kernel code, such as "init_completion() prior to
complete()" in this false bug.
Thus, I am improving the tool to reduce false positives now.
Any suggestion on deadlock detection or the tool would be appreciated,
thanks :)
Best wishes,
Jia-Ju Bai
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2022-02-05 8:35 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-02-01 7:09 [BUG] staging: wfx: possible deadlock in wfx_conf_tx() and wfx_add_interface() Jia-Ju Bai
2022-02-01 11:33 ` Hillf Danton
2022-02-01 11:56 ` Jérôme Pouiller
2022-02-05 8:34 ` Jia-Ju Bai
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox