2022-04-05 01:03:12

by Steven Rostedt

[permalink] [raw]
Subject: [PATCH] Bluetooth: hci_qca: Use del_timer_sync() before freeing

[ Resending due to cut and paste failure of email address ]

From: Steven Rostedt (Google) <[email protected]>

While looking at a crash report on a timer list being corrupted, which
usually happens when a timer is freed while still active. This is
commonly triggered by code calling del_timer() instead of
del_timer_sync() just before freeing.

One possible culprit is the hci_qca driver, which does exactly that.

Cc: [email protected]
Fixes: 0ff252c1976da ("Bluetooth: hciuart: Add support QCA chipset for
UART") Signed-off-by: Steven Rostedt (Google) <[email protected]>
---
diff --git a/drivers/bluetooth/hci_qca.c b/drivers/bluetooth/hci_qca.c
index f6e91fb432a3..73a8c72b5aae 100644
--- a/drivers/bluetooth/hci_qca.c
+++ b/drivers/bluetooth/hci_qca.c
@@ -696,8 +696,8 @@ static int qca_close(struct hci_uart *hu)
skb_queue_purge(&qca->tx_wait_q);
skb_queue_purge(&qca->txq);
skb_queue_purge(&qca->rx_memdump_q);
- del_timer(&qca->tx_idle_timer);
- del_timer(&qca->wake_retrans_timer);
+ del_timer_sync(&qca->tx_idle_timer);
+ del_timer_sync(&qca->wake_retrans_timer);
destroy_workqueue(qca->workqueue);
qca->hu = NULL;


2022-04-05 03:37:08

by Eric Dumazet

[permalink] [raw]
Subject: Re: [PATCH] Bluetooth: hci_qca: Use del_timer_sync() before freeing


On 4/4/22 15:22, Steven Rostedt wrote:
> [ Resending due to cut and paste failure of email address ]
>
> From: Steven Rostedt (Google) <[email protected]>
>
> While looking at a crash report on a timer list being corrupted, which
> usually happens when a timer is freed while still active. This is
> commonly triggered by code calling del_timer() instead of
> del_timer_sync() just before freeing.
>
> One possible culprit is the hci_qca driver, which does exactly that.
>
> Cc: [email protected]
> Fixes: 0ff252c1976da ("Bluetooth: hciuart: Add support QCA chipset for
> UART") Signed-off-by: Steven Rostedt (Google) <[email protected]>
> ---
> diff --git a/drivers/bluetooth/hci_qca.c b/drivers/bluetooth/hci_qca.c
> index f6e91fb432a3..73a8c72b5aae 100644
> --- a/drivers/bluetooth/hci_qca.c
> +++ b/drivers/bluetooth/hci_qca.c
> @@ -696,8 +696,8 @@ static int qca_close(struct hci_uart *hu)
> skb_queue_purge(&qca->tx_wait_q);
> skb_queue_purge(&qca->txq);
> skb_queue_purge(&qca->rx_memdump_q);
> - del_timer(&qca->tx_idle_timer);
> - del_timer(&qca->wake_retrans_timer);
> + del_timer_sync(&qca->tx_idle_timer);
> + del_timer_sync(&qca->wake_retrans_timer);


It seems the wake_retrans_timer could be re-armed from a work queue.

So perhaps we need to make sure qca->workqueue is destroyed

before these del_timer_sync() calls ?

> destroy_workqueue(qca->workqueue);


ie move this destroy_workqueue() up ?


> qca->hu = NULL;
>

2022-04-05 03:39:33

by Steven Rostedt

[permalink] [raw]
Subject: Re: [PATCH] Bluetooth: hci_qca: Use del_timer_sync() before freeing

On Mon, 4 Apr 2022 17:22:00 -0700
Eric Dumazet <[email protected]> wrote:
> > diff --git a/drivers/bluetooth/hci_qca.c b/drivers/bluetooth/hci_qca.c
> > index f6e91fb432a3..73a8c72b5aae 100644
> > --- a/drivers/bluetooth/hci_qca.c
> > +++ b/drivers/bluetooth/hci_qca.c
> > @@ -696,8 +696,8 @@ static int qca_close(struct hci_uart *hu)
> > skb_queue_purge(&qca->tx_wait_q);
> > skb_queue_purge(&qca->txq);
> > skb_queue_purge(&qca->rx_memdump_q);
> > - del_timer(&qca->tx_idle_timer);
> > - del_timer(&qca->wake_retrans_timer);
> > + del_timer_sync(&qca->tx_idle_timer);
> > + del_timer_sync(&qca->wake_retrans_timer);
>
>
> It seems the wake_retrans_timer could be re-armed from a work queue.
>
> So perhaps we need to make sure qca->workqueue is destroyed
>
> before these del_timer_sync() calls ?
>
> > destroy_workqueue(qca->workqueue);
>
>
> ie move this destroy_workqueue() up ?

Yeah, that could be a problem. I would think moving it up would help,
if that's what requeue's the timers.

-- Steve


>
>
> > qca->hu = NULL;
> >

2022-04-06 17:36:29

by Guenter Roeck

[permalink] [raw]
Subject: Re: [PATCH] Bluetooth: hci_qca: Use del_timer_sync() before freeing

On Mon, Apr 04, 2022 at 05:22:00PM -0700, Eric Dumazet wrote:
>
> On 4/4/22 15:22, Steven Rostedt wrote:
> > [ Resending due to cut and paste failure of email address ]
> >
> > From: Steven Rostedt (Google) <[email protected]>
> >
> > While looking at a crash report on a timer list being corrupted, which
> > usually happens when a timer is freed while still active. This is
> > commonly triggered by code calling del_timer() instead of
> > del_timer_sync() just before freeing.
> >
> > One possible culprit is the hci_qca driver, which does exactly that.
> >
> > Cc: [email protected]
> > Fixes: 0ff252c1976da ("Bluetooth: hciuart: Add support QCA chipset for
> > UART") Signed-off-by: Steven Rostedt (Google) <[email protected]>
> > ---
> > diff --git a/drivers/bluetooth/hci_qca.c b/drivers/bluetooth/hci_qca.c
> > index f6e91fb432a3..73a8c72b5aae 100644
> > --- a/drivers/bluetooth/hci_qca.c
> > +++ b/drivers/bluetooth/hci_qca.c
> > @@ -696,8 +696,8 @@ static int qca_close(struct hci_uart *hu)
> > skb_queue_purge(&qca->tx_wait_q);
> > skb_queue_purge(&qca->txq);
> > skb_queue_purge(&qca->rx_memdump_q);
> > - del_timer(&qca->tx_idle_timer);
> > - del_timer(&qca->wake_retrans_timer);
> > + del_timer_sync(&qca->tx_idle_timer);
> > + del_timer_sync(&qca->wake_retrans_timer);
>
>
> It seems the wake_retrans_timer could be re-armed from a work queue.
>
> So perhaps we need to make sure qca->workqueue is destroyed
>
> before these del_timer_sync() calls ?
>
> > destroy_workqueue(qca->workqueue);
>
>
> ie move this destroy_workqueue() up ?
>

What prevents the timer code from queueing work into the destroyed
workqueue ?

Thanks,
Guenter

2022-04-06 17:41:54

by Steven Rostedt

[permalink] [raw]
Subject: Re: [PATCH] Bluetooth: hci_qca: Use del_timer_sync() before freeing

On Wed, 6 Apr 2022 08:39:07 -0700
Guenter Roeck <[email protected]> wrote:

> > ie move this destroy_workqueue() up ?
> >
>
> What prevents the timer code from queueing work into the destroyed
> workqueue ?

So we have a chicken verses egg issue here?

-- Steve

2022-04-06 18:16:15

by Guenter Roeck

[permalink] [raw]
Subject: Re: [PATCH] Bluetooth: hci_qca: Use del_timer_sync() before freeing

On 4/6/22 08:46, Steven Rostedt wrote:
> On Wed, 6 Apr 2022 08:39:07 -0700
> Guenter Roeck <[email protected]> wrote:
>
>>> ie move this destroy_workqueue() up ?
>>>
>>
>> What prevents the timer code from queueing work into the destroyed
>> workqueue ?
>
> So we have a chicken verses egg issue here?
>

Almost looks like it, unless I am missing something. Maybe some flag
is needed to prevent the timer handling code from queuing into the
destroyed workqueue, or the workqueue handler from updating the timer.

Guenter

2022-04-06 18:27:03

by Steven Rostedt

[permalink] [raw]
Subject: Re: [PATCH] Bluetooth: hci_qca: Use del_timer_sync() before freeing

On Wed, 6 Apr 2022 09:36:10 -0700
Guenter Roeck <[email protected]> wrote:

> > So we have a chicken verses egg issue here?
> >
>
> Almost looks like it, unless I am missing something. Maybe some flag
> is needed to prevent the timer handling code from queuing into the
> destroyed workqueue, or the workqueue handler from updating the timer.

That's exactly what I was thinking. I do not know all the code here. I
could try to write a patch, but I may likely miss something.

-- Steve

2022-04-07 11:00:14

by Eric Dumazet

[permalink] [raw]
Subject: Re: [PATCH] Bluetooth: hci_qca: Use del_timer_sync() before freeing


On 4/6/22 09:46, Steven Rostedt wrote:
> On Wed, 6 Apr 2022 09:36:10 -0700
> Guenter Roeck <[email protected]> wrote:
>
>>> So we have a chicken verses egg issue here?
>>>
>> Almost looks like it, unless I am missing something. Maybe some flag
>> is needed to prevent the timer handling code from queuing into the
>> destroyed workqueue, or the workqueue handler from updating the timer.
> That's exactly what I was thinking. I do not know all the code here. I
> could try to write a patch, but I may likely miss something.
>
> -- Steve


Take a look at

https://git.kernel.org/pub/scm/linux/kernel/git/netdev/net.git/commit/?id=1946014ca3b19be9e485e780e862c375c6f98bad


Ie, use an ->liveĀ  (or ->dead) field.