Received: by 2002:a05:6358:bb9e:b0:b9:5105:a5b4 with SMTP id df30csp669728rwb; Fri, 2 Sep 2022 23:51:55 -0700 (PDT) X-Google-Smtp-Source: AA6agR4RbewG7qaFnx2bGQqaeFJasc+gdXE3W8rwylv+1LFP/l7oeWLDc3mvEp6TJuLMexi94mzl X-Received: by 2002:a17:907:160d:b0:741:a253:422f with SMTP id hb13-20020a170907160d00b00741a253422fmr16743693ejc.640.1662187914928; Fri, 02 Sep 2022 23:51:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662187914; cv=none; d=google.com; s=arc-20160816; b=RDT8LCqBlVIjJWXbnpvEv+a4MxdZNY256r1EVyB++BuYBFgca1A0HTSt8LTboQMQ44 Kt8fclPNc9rzT+0ssh3/Fr0ig8Xi9v+Ju1zkSdVQvtmhuG15IVLAR4OWhJR/7it7YKo0 WqOUjR0CHCJf1NCzpb400X4ONAyM2j9WcNzY4kvVxAkS9gU96qhK/dCvi7fHUmvr05UJ 2VJPKX5sKqm06J8bmXxEoG1kwo02uMfvpz+Jm3z7g1Kh4IkLfNWY6WdrZlwyg9E7ZOck K/ezzBBxpW4hWQ2CgCrWOnDWtF0dlqIMlqVQ5TiUuRKHCT8FoiHi626rH0LuNk5a5ujP JuJQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id; bh=qcB3xbRPwvYo/2KO1CVoIWDi6/QnmYziw4Aw0UoCJF4=; b=Ae9C4IEFt5hY/AvAFivrvmLTzpBvfFxTeCsQawYkBNPJK+3RwGV51o3KYc/ZAbi8mh +aeiQVYRKNFjF9yv/XIPR/GLaJafmjYC265dzZ7z6ReqQTdGgMCnf04rzjxWTac1XZdK xl3HItSe5IveutcpmwC9BTJqj/HtqRmr+uYr8iP7TKJJmPcY+kGUgEjSbTtWg+xRsPs+ nXig5dpIh5ItBpl/YspVQmxtFQy8shU+fZkSEpbssEQn/dfYy8ipyjiP7TDI7Fuc3Wn0 JuQLYR7rdlJ7TLhN0JI417ppMiJmhXYV72omjTlyQd5b0oQVyYF7PnKiG5FCR9ivmCtL pT5Q== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id v6-20020a056402348600b0044648d791b4si3766138edc.227.2022.09.02.23.51.11; Fri, 02 Sep 2022 23:51:54 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229626AbiICGtf (ORCPT + 99 others); Sat, 3 Sep 2022 02:49:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58614 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229580AbiICGte (ORCPT ); Sat, 3 Sep 2022 02:49:34 -0400 Received: from www262.sakura.ne.jp (www262.sakura.ne.jp [202.181.97.72]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 925B2BE0F for ; Fri, 2 Sep 2022 23:49:31 -0700 (PDT) Received: from fsav412.sakura.ne.jp (fsav412.sakura.ne.jp [133.242.250.111]) by www262.sakura.ne.jp (8.15.2/8.15.2) with ESMTP id 2836nTLb007007; Sat, 3 Sep 2022 15:49:29 +0900 (JST) (envelope-from penguin-kernel@I-love.SAKURA.ne.jp) Received: from www262.sakura.ne.jp (202.181.97.72) by fsav412.sakura.ne.jp (F-Secure/fsigk_smtp/550/fsav412.sakura.ne.jp); Sat, 03 Sep 2022 15:49:29 +0900 (JST) X-Virus-Status: clean(F-Secure/fsigk_smtp/550/fsav412.sakura.ne.jp) Received: from [192.168.1.9] (M106072142033.v4.enabler.ne.jp [106.72.142.33]) (authenticated bits=0) by www262.sakura.ne.jp (8.15.2/8.15.2) with ESMTPSA id 2836nSJ7007000 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=NO); Sat, 3 Sep 2022 15:49:29 +0900 (JST) (envelope-from penguin-kernel@I-love.SAKURA.ne.jp) Message-ID: <289ba1a9-e4f7-d0b5-545e-a98dcf365c68@I-love.SAKURA.ne.jp> Date: Sat, 3 Sep 2022 15:49:26 +0900 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:102.0) Gecko/20100101 Thunderbird/102.2.0 Subject: Re: [PATCH] Bluetooth: use hdev->workqueue when queuing hdev->{cmd,ncmd}_timer works Content-Language: en-US To: Luiz Augusto von Dentz Cc: Marcel Holtmann , Johan Hedberg , Schspa Shi , syzbot , syzkaller-bugs@googlegroups.com, Lai Jiangshan , Tejun Heo , "linux-bluetooth@vger.kernel.org" References: <00000000000016512d05e76bd837@google.com> <733e6931-aa66-5295-d8a8-49063b7347f1@I-love.SAKURA.ne.jp> From: Tetsuo Handa In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,NICE_REPLY_A, SPF_HELO_NONE,SPF_NONE,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-bluetooth@vger.kernel.org On 2022/09/03 6:31, Luiz Augusto von Dentz wrote: > Hi Tetsuo, > > On Fri, Sep 2, 2022 at 11:45 AM Luiz Augusto von Dentz wrote: >> Didn't we introduce HCI_CMD_DRAIN_WORKQUEUE exactly to avoid queuing >> after the cancel pattern? HCI_CMD_DRAIN_WORKQUEUE does not help for this case. What extid=243b7d89777f90f7613b is reporting is hci_cmd_timeout() { hci_dev_do_reset() { starts sleeping due to e.g. preemption hci_dev_set_flag(hdev, HCI_CMD_DRAIN_WORKQUEUE); // Sets HCI_CMD_DRAIN_WORKQUEUE flag cancel_delayed_work(&hdev->cmd_timer); // does nothing because hci_cmd_timeout() is already running cancel_delayed_work(&hdev->ncmd_timer); drain_workqueue(hdev->workqueue) { sets __WQ_DRAINING flag on hdev->workqueue starts waiting for completion of all works on hdev->workqueue finishes sleeping due to e.g. preemption queue_work(hdev->workqueue, &hdev->cmd_work) // <= complains attempt to queue work from system_wq into __WQ_DRAINING hdev->workqueue } finishes waiting for completion of all works on hdev->workqueue clears __WQ_DRAINING flag } } race condition. Notice that cancel_delayed_work() does not wait for completion of already started hci_cmd_timeout() callback. If you need to wait for completion of already started callback, you need to use _sync version (e.g. cancel_delayed_work_sync()). And watch out for locking dependency when using _sync version. >> I wonder if wouldn't be better to introduce >> some function that disables/enables the workqueue so we don't have to >> do extra tracking in the driver/subsystem? >> > > I was thinking on doing something like the following: > > https://gist.github.com/Vudentz/a2288015fedbed366fcdb612264a9d16 That patch does not close race, for @@ -4037,6 +4038,10 @@ static void hci_cmd_work(struct work_struct *work) BT_DBG("%s cmd_cnt %d cmd queued %d", hdev->name, atomic_read(&hdev->cmd_cnt), skb_queue_len(&hdev->cmd_q)); + /* Don't queue while draining */ + if (hci_dev_test_flag(hdev, HCI_CMD_DRAIN_WORKQUEUE)) + return; /* * BUG: WE ARE FREE TO SLEEP FOR ARBITRARY DURATION IMMEDIATELY AFTER CHECKING THE FLAG. * ANY "TEST AND DO SOMETHING" NEEDS TO BE PROTECTED BY A LOCK MECHANISM. */ + /* Send queued commands */ if (atomic_read(&hdev->cmd_cnt)) { skb = skb_dequeue(&hdev->cmd_q); . In other words, HCI_CMD_DRAIN_WORKQUEUE does not fix what extid=63bed493aebbf6872647 is reporting. If "TEST AND DO SOMETHING" does not sleep, RCU is a handy lock mechanism. > > Since there is no reason to queue any command if we are draining and > are gonna reset at the end it is pretty useless to queue commands at > that point. Then, you can add that check.