Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp29579210rwd; Wed, 5 Jul 2023 14:10:28 -0700 (PDT) X-Google-Smtp-Source: APBJJlG8c4wpml60jMXSlnyJivvjxu6pwbhDHUZhY0R/np8ftenA5+D0DB+OUzM4ncJtUjEgjX5x X-Received: by 2002:a05:6a20:548a:b0:12e:7ff9:b612 with SMTP id i10-20020a056a20548a00b0012e7ff9b612mr8496pzk.45.1688591427986; Wed, 05 Jul 2023 14:10:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688591427; cv=none; d=google.com; s=arc-20160816; b=nPnLeZc2fEYmmFKGmKSjqeurWBVBfY8XmJsXTKfAiqbnlTQtO33dVWQfqcw4JXKgiq F50HMzQ8/Zs/1wJgMmmj7xj773WLrCXFduhui1woztVCqFxesZtoRytka+7sJFgQcbvz Rb8O3a7iwKJQPvT0bhEHmbrITB56SQT2LaUTZHy8i7kRHXL4bummUxnQGoFLTKTD8IHl tBAEzGNCsQnZM14kmgdKDSnVm2Q7AYr4d9A2U7s6qZ8/kYYF5O1lz1aDoRj0cBd3UF/g Sw5oOCxwo7+QTKJJ0xnDJyUedgyU9OUNUXTsTh2pThLtt7zpM10ag3Cs1QuK1PV0463q Dcdg== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=qmj/KUtCtgJQVjZIE+0mLvUSZaWRXEiyQldCxLAvqj8=; fh=WbN+C46Ib+eDGzyU6KxsbONAj6+ajh+l45N/ovgYlZ0=; b=Aly0riygmeY4e7Q2Jbgl/JI/2rC+UZeqsc8Bb4zScFLU2r9SZSvxt2amZ5hB7+BOx0 AGQIMT+OmTQhz2vbPaR+o+96vvKXP8v2Jx/+qw73PaVjGO+3aFsf1YEKjKejxdd2ex8N tvaGrodifRo3Od1TLhk0G0Zau+Lu9xbCN135RgaREs56/QKh3tiHKv6rXWy47lpyREF2 0noTGKWBu2X1Gx2J0xHNwa5wbTyLNtUg5fUcNQgX4dQR3TO7+eXtaAbrQz9izC3JXEjO mKKu1ameurJG81zdAvbK+yQ3lwai+A1CeFV3BfW83BWNEUuxfFhtDs9C5u+ISJ6Luo6s EN6Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=gRPWZ2BW; spf=pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z4-20020aa79584000000b006825e062715si12376308pfj.228.2023.07.05.14.10.02; Wed, 05 Jul 2023 14:10:27 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-bluetooth-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=@chromium.org header.s=google header.b=gRPWZ2BW; spf=pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231465AbjGEVIJ (ORCPT + 99 others); Wed, 5 Jul 2023 17:08:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57928 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230157AbjGEVII (ORCPT ); Wed, 5 Jul 2023 17:08:08 -0400 Received: from mail-pg1-x52c.google.com (mail-pg1-x52c.google.com [IPv6:2607:f8b0:4864:20::52c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3B1DE1732 for ; Wed, 5 Jul 2023 14:08:07 -0700 (PDT) Received: by mail-pg1-x52c.google.com with SMTP id 41be03b00d2f7-55b1238a024so50355a12.0 for ; Wed, 05 Jul 2023 14:08:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1688591286; x=1691183286; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=qmj/KUtCtgJQVjZIE+0mLvUSZaWRXEiyQldCxLAvqj8=; b=gRPWZ2BWJvbUXs3pMf0J4YjWzBJUwnYJQ1VdzD43dITmA6klOnCedfdoFSq171iZOl Y+81KpQb2qtZi0VDAsFIXZxC8J6kieLofCsSM59CnX4Ktj4kbUmMpLIJ+I0ipjzh3aaL FBQI5/XYwVE/a52jk7FA2fcK4Rn2oFPGRryGI= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688591286; x=1691183286; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=qmj/KUtCtgJQVjZIE+0mLvUSZaWRXEiyQldCxLAvqj8=; b=Am/BQUYcQlkX+rRzTxsjpKNwb9Awiacwc9wEdtnxt/qitICuGeg7xIWu1Wo38Rwr8v Kov0AaDFlPUr399YGd4qUiWTLLo9yYx+PC8vvYfuaKOUQ/ufA9fW5A/Vbn8/h58e9UCr ewz1qcwdQ6BSbS+lUpYY5w7jWQ5gff/R5qIYgy0f/5CItYHVpyv3N1ZPpSDGcWJAXwmK 6FhuIqHdSFc3Af7M5PYDeL5YUt5r0EbzGkfM7r65A7zq+9zIC0ZrMu8aP4T41W+zoVAh Xop8gYz1IiGniWGNnaGqLLrV8fvZxIY9A/0h7UWJw97hZaVBicpVuj3r/KOtjVZVOD7i gJEw== X-Gm-Message-State: ABy/qLaD1WgS7fvU/lL0s+vIsHLv+I7v96K1Zb0jMrRfKXRJbZc/vRFu trCbKtEnbeIGyqj3VST39w5RWkZEIVvTDmXGQUw= X-Received: by 2002:a05:6a20:970a:b0:126:a80d:4960 with SMTP id hr10-20020a056a20970a00b00126a80d4960mr12881pzc.30.1688591286299; Wed, 05 Jul 2023 14:08:06 -0700 (PDT) Received: from localhost (21.160.199.104.bc.googleusercontent.com. [104.199.160.21]) by smtp.gmail.com with UTF8SMTPSA id m30-20020a637d5e000000b0054fe07d2f3dsm6229pgn.11.2023.07.05.14.08.04 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 05 Jul 2023 14:08:05 -0700 (PDT) From: Ying Hsu To: linux-bluetooth@vger.kernel.org Cc: chromeos-bluetooth-upstreaming@chromium.org, Ying Hsu , Johan Hedberg , Luiz Augusto von Dentz , Marcel Holtmann , linux-kernel@vger.kernel.org Subject: [PATCH] Bluetooth: Fix hci_suspend_sync crash Date: Wed, 5 Jul 2023 21:06:47 +0000 Message-ID: <20230705210647.1.I636c21e4dc8fe3352f4d7aef26c0ec3857e24ca0@changeid> X-Mailer: git-send-email 2.41.0.255.g8b1d071c50-goog MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=unavailable 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-bluetooth@vger.kernel.org If hci_unregister_dev() frees the hci_dev object but hci_suspend_notifier may still be accessing it, it can cause the program to crash. Here's the call trace: <4>[102152.653246] Call Trace: <4>[102152.653254] hci_suspend_sync+0x109/0x301 [bluetooth] <4>[102152.653259] hci_suspend_dev+0x78/0xcd [bluetooth] <4>[102152.653263] hci_suspend_notifier+0x42/0x7a [bluetooth] <4>[102152.653268] notifier_call_chain+0x43/0x6b <4>[102152.653271] __blocking_notifier_call_chain+0x48/0x69 <4>[102152.653273] __pm_notifier_call_chain+0x22/0x39 <4>[102152.653276] pm_suspend+0x287/0x57c <4>[102152.653278] state_store+0xae/0xe5 <4>[102152.653281] kernfs_fop_write+0x109/0x173 <4>[102152.653284] __vfs_write+0x16f/0x1a2 <4>[102152.653287] ? selinux_file_permission+0xca/0x16f <4>[102152.653289] ? security_file_permission+0x36/0x109 <4>[102152.653291] vfs_write+0x114/0x21d <4>[102152.653293] __x64_sys_write+0x7b/0xdb <4>[102152.653296] do_syscall_64+0x59/0x194 <4>[102152.653299] entry_SYSCALL_64_after_hwframe+0x5c/0xc1 This patch holds the reference count of the hci_dev object while processing it in hci_suspend_notifier to avoid potential crash caused by the race condition. Signed-off-by: Ying Hsu --- Tested with 100 times suspend/resume on a Chromebook device. net/bluetooth/hci_core.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/net/bluetooth/hci_core.c b/net/bluetooth/hci_core.c index b421e196f60c..bdbe26ffbd39 100644 --- a/net/bluetooth/hci_core.c +++ b/net/bluetooth/hci_core.c @@ -2434,6 +2434,9 @@ static int hci_suspend_notifier(struct notifier_block *nb, unsigned long action, if (hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) return NOTIFY_DONE; + /* To avoid a potential race with hci_unregister_dev. */ + hci_dev_hold(hdev); + if (action == PM_SUSPEND_PREPARE) ret = hci_suspend_dev(hdev); else if (action == PM_POST_SUSPEND) @@ -2443,6 +2446,7 @@ static int hci_suspend_notifier(struct notifier_block *nb, unsigned long action, bt_dev_err(hdev, "Suspend notifier action (%lu) failed: %d", action, ret); + hci_dev_put(hdev); return NOTIFY_DONE; } -- 2.41.0.255.g8b1d071c50-goog