mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Arseniy Krasnov <avkrasnov@salutedevices.com>
To: Marcel Holtmann <marcel@holtmann.org>,
	Luiz Augusto von Dentz <luiz.dentz@gmail.com>
Cc: <linux-bluetooth@vger.kernel.org>, <linux-kernel@vger.kernel.org>,
	<oxffffaa@gmail.com>, <ddrokosov@salutedevices.com>
Subject: Re: [PATCH v1] Bluetooth: hci_uart: fix race during initialization
Date: Mon, 16 Dec 2024 14:58:28 +0300	[thread overview]
Message-ID: <e18ebe28-e300-f9e3-ef66-1b7811ebd447@salutedevices.com> (raw)
In-Reply-To: <a1db0c90-1803-e01c-3e23-d18e4343a4eb@salutedevices.com>

Hi, sorry, pls ping

Thanks

On 27.11.2024 15:38, Arseniy Krasnov wrote:
> 'hci_register_dev()' calls power up function, which is executed by
> kworker - 'hci_power_on()'. This function does access to bluetooth chip
> using callbacks from 'hci_ldisc.c', for example 'hci_uart_send_frame()'.
> Now 'hci_uart_send_frame()' checks 'HCI_UART_PROTO_READY' bit set, and
> if not - it fails. Problem is that 'HCI_UART_PROTO_READY' is set after
> 'hci_register_dev()', and there is tiny chance that 'hci_power_on()' will
> be executed before setting this bit. In that case HCI init logic fails.
> 
> Patch adds one more bit in addition to 'HCI_UART_PROTO_READY' which
> allows to perform power up logic without original bit set.
> 
> Signed-off-by: Arseniy Krasnov <avkrasnov@salutedevices.com>
> ---
>  drivers/bluetooth/hci_ldisc.c | 19 ++++++++++++++-----
>  drivers/bluetooth/hci_uart.h  |  1 +
>  2 files changed, 15 insertions(+), 5 deletions(-)
> 
> diff --git a/drivers/bluetooth/hci_ldisc.c b/drivers/bluetooth/hci_ldisc.c
> index 30192bb083549..2e20f00649bd7 100644
> --- a/drivers/bluetooth/hci_ldisc.c
> +++ b/drivers/bluetooth/hci_ldisc.c
> @@ -102,7 +102,8 @@ static inline struct sk_buff *hci_uart_dequeue(struct hci_uart *hu)
>  	if (!skb) {
>  		percpu_down_read(&hu->proto_lock);
>  
> -		if (test_bit(HCI_UART_PROTO_READY, &hu->flags))
> +		if (test_bit(HCI_UART_PROTO_READY, &hu->flags) ||
> +		    test_bit(HCI_UART_PROTO_INIT, &hu->flags))
>  			skb = hu->proto->dequeue(hu);
>  
>  		percpu_up_read(&hu->proto_lock);
> @@ -124,7 +125,8 @@ int hci_uart_tx_wakeup(struct hci_uart *hu)
>  	if (!percpu_down_read_trylock(&hu->proto_lock))
>  		return 0;
>  
> -	if (!test_bit(HCI_UART_PROTO_READY, &hu->flags))
> +	if (!test_bit(HCI_UART_PROTO_READY, &hu->flags) &&
> +	    !test_bit(HCI_UART_PROTO_INIT, &hu->flags))
>  		goto no_schedule;
>  
>  	set_bit(HCI_UART_TX_WAKEUP, &hu->tx_state);
> @@ -278,7 +280,8 @@ static int hci_uart_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
>  
>  	percpu_down_read(&hu->proto_lock);
>  
> -	if (!test_bit(HCI_UART_PROTO_READY, &hu->flags)) {
> +	if (!test_bit(HCI_UART_PROTO_READY, &hu->flags) &&
> +	    !test_bit(HCI_UART_PROTO_INIT, &hu->flags)) {
>  		percpu_up_read(&hu->proto_lock);
>  		return -EUNATCH;
>  	}
> @@ -582,7 +585,8 @@ static void hci_uart_tty_wakeup(struct tty_struct *tty)
>  	if (tty != hu->tty)
>  		return;
>  
> -	if (test_bit(HCI_UART_PROTO_READY, &hu->flags))
> +	if (test_bit(HCI_UART_PROTO_READY, &hu->flags) ||
> +	    test_bit(HCI_UART_PROTO_INIT, &hu->flags))
>  		hci_uart_tx_wakeup(hu);
>  }
>  
> @@ -608,7 +612,8 @@ static void hci_uart_tty_receive(struct tty_struct *tty, const u8 *data,
>  
>  	percpu_down_read(&hu->proto_lock);
>  
> -	if (!test_bit(HCI_UART_PROTO_READY, &hu->flags)) {
> +	if (!test_bit(HCI_UART_PROTO_READY, &hu->flags) &&
> +	    !test_bit(HCI_UART_PROTO_INIT, &hu->flags)) {
>  		percpu_up_read(&hu->proto_lock);
>  		return;
>  	}
> @@ -704,12 +709,16 @@ static int hci_uart_set_proto(struct hci_uart *hu, int id)
>  
>  	hu->proto = p;
>  
> +	set_bit(HCI_UART_PROTO_INIT, &hu->flags);
> +
>  	err = hci_uart_register_dev(hu);
>  	if (err) {
>  		return err;
>  	}
>  
>  	set_bit(HCI_UART_PROTO_READY, &hu->flags);
> +	clear_bit(HCI_UART_PROTO_INIT, &hu->flags);
> +
>  	return 0;
>  }
>  
> diff --git a/drivers/bluetooth/hci_uart.h b/drivers/bluetooth/hci_uart.h
> index 00bf7ae82c5b7..39f39704be41f 100644
> --- a/drivers/bluetooth/hci_uart.h
> +++ b/drivers/bluetooth/hci_uart.h
> @@ -89,6 +89,7 @@ struct hci_uart {
>  #define HCI_UART_REGISTERED		1
>  #define HCI_UART_PROTO_READY		2
>  #define HCI_UART_NO_SUSPEND_NOTIFIER	3
> +#define HCI_UART_PROTO_INIT		4
>  
>  /* TX states  */
>  #define HCI_UART_SENDING	1

  reply	other threads:[~2024-12-16 11:58 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-27 12:38 Arseniy Krasnov
2024-12-16 11:58 ` Arseniy Krasnov [this message]
2024-12-16 14:52 ` Luiz Augusto von Dentz
2024-12-16 15:06   ` Arseniy Krasnov
2025-02-12  9:52 Arseniy Krasnov

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=e18ebe28-e300-f9e3-ef66-1b7811ebd447@salutedevices.com \
    --to=avkrasnov@salutedevices.com \
    --cc=ddrokosov@salutedevices.com \
    --cc=linux-bluetooth@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=luiz.dentz@gmail.com \
    --cc=marcel@holtmann.org \
    --cc=oxffffaa@gmail.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

all inboxes | Powered by JetHome®