Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp3088076rwb; Mon, 15 Aug 2022 17:50:18 -0700 (PDT) X-Google-Smtp-Source: AA6agR5gok03+L6xp03+9SBefDwl2xsIXjuybiZpkUyr7o/f03RjOW0lC2XG3w46zy0ej11n3A6T X-Received: by 2002:a17:907:1c19:b0:730:9e1f:7204 with SMTP id nc25-20020a1709071c1900b007309e1f7204mr11801468ejc.244.1660611018305; Mon, 15 Aug 2022 17:50:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660611018; cv=none; d=google.com; s=arc-20160816; b=mpSwuaJNQeCLONzU3RozdHxkYz6vGFHwJuuyg7IkvsUgGCnWJYHFNqW3xmHBkds3mi PZjs8tkEfDW4WRIjfm27bTNK6gx4UVvkSsPBAyakd0dU+9Qu6M4WztgEZL8+D2j+3frQ 7cGt1MnlumnykLJ139roVhoH3qQVfP1l7eAE0swtrhdfhdExfTg+jy/Tyrm/1nelpxTW pLVvoOKN0aTGd0Jl6KNhVIAUegCY40+KziSa1RVIeBmfaIMGF7UuiBpzTgwU1bJpuKLx RA6FRfYrnUKcjnFZLObpF8NgcyC1d+PRMowqHgypIgxBreZUzGUSMSo8smx9SkucZWuB wLmw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=dZyIHaHfMKKUSqJIfF+QWiOcZ0Wy85KRhCA5Pfzv33Y=; b=nUnGDvuSz7eKVkzteILxEwRWVvkmygKZNJB3a/6KV+7npFN4h7y9NUEaduzNkrEhiy 5StPU2C2HoPQz3m7SsC6IxJKsmxaiLL3Y4KFRrVm/JooTWkt4qJQAwZu6YLYCr0inI8W ODAjk9A1nQhYEMVYzp/PKZldIhX0eZmYSjsKgmg5U1/gK8wa/JN3v25w0o/6deh5gnzq iJFUjsiDKKkHCrzz11auRhW7+WLr+06kIWlAQKJqjvF9TiKzMZfxl8d1KxiUuEyJzV4L gTY69SkQyiqoFql9vhPvvGATUNck1+d3VnZqwy8Ge6CYuU8q+pkjMYXkE2TothKE09hh rQhA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="t4/R5rKm"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x7-20020a056402414700b0043d10cc3a66si8170130eda.403.2022.08.15.17.49.52; Mon, 15 Aug 2022 17:50:18 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="t4/R5rKm"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343563AbiHOVU7 (ORCPT + 99 others); Mon, 15 Aug 2022 17:20:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53856 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241239AbiHOVNp (ORCPT ); Mon, 15 Aug 2022 17:13:45 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7C91BDAB9C; Mon, 15 Aug 2022 12:19:43 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id C59EEB81122; Mon, 15 Aug 2022 19:19:41 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id EFE9EC433D6; Mon, 15 Aug 2022 19:19:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1660591180; bh=VwI+tU7kIt7/Mj5JWlsCMmZ+77AuLLrmY9LqbYMIQB0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=t4/R5rKmrNL7iO4eA8RmNOc9bj1OUTJkE6xF/W06H72wblglkMxV6uW/Nms+Zc4W8 R4COBc10ftiqBiW+ACJHNAbGfH4u+F66QyHA8ZjZ7AlRlrO3KINhB+EtUeD9W3TQ+o k/lGzZPJFId/XcuixG0HyzxeVYRaY3B9X56G/m5o= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, syzbot+63bed493aebbf6872647@syzkaller.appspotmail.com, Schspa Shi , Marcel Holtmann , Sasha Levin Subject: [PATCH 5.18 0496/1095] Bluetooth: When HCI work queue is drained, only queue chained work Date: Mon, 15 Aug 2022 19:58:15 +0200 Message-Id: <20220815180450.051320724@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220815180429.240518113@linuxfoundation.org> References: <20220815180429.240518113@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Schspa Shi [ Upstream commit 877afadad2dce8aae1f2aad8ce47e072d4f6165e ] The HCI command, event, and data packet processing workqueue is drained to avoid deadlock in commit 76727c02c1e1 ("Bluetooth: Call drain_workqueue() before resetting state"). There is another delayed work, which will queue command to this drained workqueue. Which results in the following error report: Bluetooth: hci2: command 0x040f tx timeout WARNING: CPU: 1 PID: 18374 at kernel/workqueue.c:1438 __queue_work+0xdad/0x1140 Workqueue: events hci_cmd_timeout RIP: 0010:__queue_work+0xdad/0x1140 RSP: 0000:ffffc90002cffc60 EFLAGS: 00010093 RAX: 0000000000000000 RBX: ffff8880b9d3ec00 RCX: 0000000000000000 RDX: ffff888024ba0000 RSI: ffffffff814e048d RDI: ffff8880b9d3ec08 RBP: 0000000000000008 R08: 0000000000000000 R09: 00000000b9d39700 R10: ffffffff814f73c6 R11: 0000000000000000 R12: ffff88807cce4c60 R13: 0000000000000000 R14: ffff8880796d8800 R15: ffff8880796d8800 FS: 0000000000000000(0000) GS:ffff8880b9d00000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: 000000c0174b4000 CR3: 000000007cae9000 CR4: 00000000003506e0 DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000 DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400 Call Trace: ? queue_work_on+0xcb/0x110 ? lockdep_hardirqs_off+0x90/0xd0 queue_work_on+0xee/0x110 process_one_work+0x996/0x1610 ? pwq_dec_nr_in_flight+0x2a0/0x2a0 ? rwlock_bug.part.0+0x90/0x90 ? _raw_spin_lock_irq+0x41/0x50 worker_thread+0x665/0x1080 ? process_one_work+0x1610/0x1610 kthread+0x2e9/0x3a0 ? kthread_complete_and_exit+0x40/0x40 ret_from_fork+0x1f/0x30 To fix this, we can add a new HCI_DRAIN_WQ flag, and don't queue the timeout workqueue while command workqueue is draining. Fixes: 76727c02c1e1 ("Bluetooth: Call drain_workqueue() before resetting state") Reported-by: syzbot+63bed493aebbf6872647@syzkaller.appspotmail.com Signed-off-by: Schspa Shi Signed-off-by: Marcel Holtmann Signed-off-by: Sasha Levin --- include/net/bluetooth/hci.h | 1 + net/bluetooth/hci_core.c | 10 +++++++++- net/bluetooth/hci_event.c | 5 +++-- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/include/net/bluetooth/hci.h b/include/net/bluetooth/hci.h index 62a9bb022aed..5f2342205ae1 100644 --- a/include/net/bluetooth/hci.h +++ b/include/net/bluetooth/hci.h @@ -361,6 +361,7 @@ enum { HCI_QUALITY_REPORT, HCI_OFFLOAD_CODECS_ENABLED, HCI_LE_SIMULTANEOUS_ROLES, + HCI_CMD_DRAIN_WORKQUEUE, __HCI_NUM_FLAGS, }; diff --git a/net/bluetooth/hci_core.c b/net/bluetooth/hci_core.c index 19df3905c5f8..2b8b51b89452 100644 --- a/net/bluetooth/hci_core.c +++ b/net/bluetooth/hci_core.c @@ -593,6 +593,11 @@ static int hci_dev_do_reset(struct hci_dev *hdev) skb_queue_purge(&hdev->rx_q); skb_queue_purge(&hdev->cmd_q); + /* Cancel these to avoid queueing non-chained pending work */ + hci_dev_set_flag(hdev, HCI_CMD_DRAIN_WORKQUEUE); + cancel_delayed_work(&hdev->cmd_timer); + cancel_delayed_work(&hdev->ncmd_timer); + /* Avoid potential lockdep warnings from the *_flush() calls by * ensuring the workqueue is empty up front. */ @@ -606,6 +611,8 @@ static int hci_dev_do_reset(struct hci_dev *hdev) if (hdev->flush) hdev->flush(hdev); + hci_dev_clear_flag(hdev, HCI_CMD_DRAIN_WORKQUEUE); + atomic_set(&hdev->cmd_cnt, 1); hdev->acl_cnt = 0; hdev->sco_cnt = 0; hdev->le_cnt = 0; @@ -3863,7 +3870,8 @@ static void hci_cmd_work(struct work_struct *work) if (res < 0) __hci_cmd_sync_cancel(hdev, -res); - if (test_bit(HCI_RESET, &hdev->flags)) + if (test_bit(HCI_RESET, &hdev->flags) || + hci_dev_test_flag(hdev, HCI_CMD_DRAIN_WORKQUEUE)) cancel_delayed_work(&hdev->cmd_timer); else schedule_delayed_work(&hdev->cmd_timer, diff --git a/net/bluetooth/hci_event.c b/net/bluetooth/hci_event.c index af17dfb20e01..7cb956d3abb2 100644 --- a/net/bluetooth/hci_event.c +++ b/net/bluetooth/hci_event.c @@ -3768,8 +3768,9 @@ static inline void handle_cmd_cnt_and_timer(struct hci_dev *hdev, u8 ncmd) cancel_delayed_work(&hdev->ncmd_timer); atomic_set(&hdev->cmd_cnt, 1); } else { - schedule_delayed_work(&hdev->ncmd_timer, - HCI_NCMD_TIMEOUT); + if (!hci_dev_test_flag(hdev, HCI_CMD_DRAIN_WORKQUEUE)) + schedule_delayed_work(&hdev->ncmd_timer, + HCI_NCMD_TIMEOUT); } } } -- 2.35.1