[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <5c823fa2.353ff.17a83a190e2.Coremail.linma@zju.edu.cn>
Date: Thu, 8 Jul 2021 09:00:41 +0800 (GMT+08:00)
From: LinMa <linma@....edu.cn>
To: "Tetsuo Handa" <penguin-kernel@...ove.sakura.ne.jp>
Cc: "Luiz Augusto von Dentz" <luiz.dentz@...il.com>,
"Marcel Holtmann" <marcel@...tmann.org>,
"Johan Hedberg" <johan.hedberg@...il.com>,
"linux-bluetooth@...r.kernel.org" <linux-bluetooth@...r.kernel.org>,
"David S. Miller" <davem@...emloft.net>,
"Jakub Kicinski" <kuba@...nel.org>,
"open list:NETWORKING [GENERAL]" <netdev@...r.kernel.org>
Subject: Re: Re: [PATCH v2] Bluetooth: call lock_sock() outside of spinlock
section
>
> diff --git a/net/bluetooth/hci_sock.c b/net/bluetooth/hci_sock.c
> index b04a5a02ecf3..0525883f4639 100644
> --- a/net/bluetooth/hci_sock.c
> +++ b/net/bluetooth/hci_sock.c
> @@ -759,19 +759,14 @@ void hci_sock_dev_event(struct hci_dev *hdev, int event)
> if (event == HCI_DEV_UNREG) {
> struct sock *sk;
>
> - /* Detach sockets from device */
> + /* Change socket state and notify */
> read_lock(&hci_sk_list.lock);
> sk_for_each(sk, &hci_sk_list.head) {
> - lock_sock(sk);
> if (hci_pi(sk)->hdev == hdev) {
> - hci_pi(sk)->hdev = NULL;
> sk->sk_err = EPIPE;
> sk->sk_state = BT_OPEN;
> sk->sk_state_change(sk);
> -
> - hci_dev_put(hdev);
> }
> - release_sock(sk);
> }
> read_unlock(&hci_sk_list.lock);
> }
>
> ? I can't judge because I don't know how this works. I worry that
> without lock_sock()/release_sock(), this races with e.g. hci_sock_bind().
>
> We could take away the backward goto if we can do something like below.
>
> diff --git a/net/bluetooth/hci_sock.c b/net/bluetooth/hci_sock.c
> index b04a5a02ecf3..1ca03769badf 100644
> --- a/net/bluetooth/hci_sock.c
> +++ b/net/bluetooth/hci_sock.c
> @@ -43,6 +43,8 @@ static DEFINE_IDA(sock_cookie_ida);
>
> static atomic_t monitor_promisc = ATOMIC_INIT(0);
>
> +static DEFINE_MUTEX(sock_list_lock);
> +
> /* ----- HCI socket interface ----- */
>
> /* Socket info */
> @@ -760,7 +762,7 @@ void hci_sock_dev_event(struct hci_dev *hdev, int event)
> struct sock *sk;
>
> /* Detach sockets from device */
> - read_lock(&hci_sk_list.lock);
> + mutex_lock(&sock_list_lock);
> sk_for_each(sk, &hci_sk_list.head) {
> lock_sock(sk);
> if (hci_pi(sk)->hdev == hdev) {
> @@ -773,7 +775,7 @@ void hci_sock_dev_event(struct hci_dev *hdev, int event)
> }
> release_sock(sk);
> }
> - read_unlock(&hci_sk_list.lock);
> + mutex_unlock(&sock_list_lock);
> }
> }
>
> @@ -838,6 +840,7 @@ static int hci_sock_release(struct socket *sock)
> if (!sk)
> return 0;
>
> + mutex_lock(&sock_list_lock);
> lock_sock(sk);
>
> switch (hci_pi(sk)->channel) {
> @@ -860,6 +863,7 @@ static int hci_sock_release(struct socket *sock)
> }
>
> bt_sock_unlink(&hci_sk_list, sk);
> + mutex_unlock(&sock_list_lock);
>
> hdev = hci_pi(sk)->hdev;
> if (hdev) {
> @@ -2049,7 +2053,9 @@ static int hci_sock_create(struct net *net, struct socket *sock, int protocol,
> sock->state = SS_UNCONNECTED;
> sk->sk_state = BT_OPEN;
>
> + mutex_lock(&sock_list_lock);
> bt_sock_link(&hci_sk_list, sk);
> + mutex_unlock(&sock_list_lock);
> return 0;
> }
>
>
> > It is also weird that this only manifests in the Bluetooth
> > HCI sockets or other subsystems don't use such locking mechanism
> > anymore?
>
Hello Tetsuo,
Yeah, that's a great patch indeed. Add one extra mutex lock for handling this.
In fact, I have tried to replace all the hci_sk_list.lock from rwlock_t to mutext.
> https://patchwork.kernel.org/project/bluetooth/patch/CAJjojJsj9pzF4j2MVvsM-hCpvyR7OkZn232yt3MdOGnLxOiRRg@mail.gmail.com/
> However, from the lock principle in the Linux kernel, this lock
> replacement is not appropriate. I take a lot of time to try with other
> lock combinations for this case but failed. For example, I tried to
> replace the rwlock_t in the hci_sk_list with a sleep-able mutex lock.
Because I have seem other part of code in kernel uses this combination: mutex_t + lock_sock. It shouldn't trigger any locking errors. (Will test it)
> Also, this regression is currently 7th top
> crashers for syzbot, and I'd like to apply this patch as soon as possible.
>
XD, Yeah. Because the bug crash point is located at function hci_sock_dev_event(). Whenever syzkaller fuzzes Bluetooth stack and the executor exits, the crash happens.
> I think that this patch can serve as a response to Lin's comment
> > In short, I have no idea if there is any lock replacing solution for
> > this bug. I need help and suggestions because the lock mechanism is
> > just so difficult.
Thanks for that, it's quite appreciating.
Regards
Lin Ma
Powered by blists - more mailing lists