Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp980948rwb; Sat, 17 Sep 2022 20:47:22 -0700 (PDT) X-Google-Smtp-Source: AMsMyM66jM9yMu0cD524Aak2IjFIbfQX8Je9TqmFcIxAS6yngOyOfrDEZv4w9B5FjWMTJnLmlgzN X-Received: by 2002:a05:6402:156:b0:440:b458:93df with SMTP id s22-20020a056402015600b00440b45893dfmr10506644edu.337.1663472842447; Sat, 17 Sep 2022 20:47:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663472842; cv=none; d=google.com; s=arc-20160816; b=TkU5/uAVUH72uGNKgbkyh2ewjOTVTuH8nNcPhySWb5Fhv5Bg+amhyfyvNBn/E8hA35 bRgzOliPpjECS4MqvVLtNVbd5S2kSm8RGSu11J8vIV/s/kudQve0ZP8EEL9S2GQLe7UQ FfcXAJNqVWxxr5brIQQYsAMwjLv2I2u/1z8NWszg45VdfiStD4tRNI7iCkjY4XV8S04c f6ZL9NiQuS1mcsKhi5/8lpAdkDO5Y3dPnVqOy9ysOEEFK4GjKO9eys1gwHXz68nGHEJR xfogz6pvL3SF0gkIpyq1r4JH64Pr0O5gnBE9rbKbpVpAA/V6H0h66IDQXST7zu8Q1KRY Fxlw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:message-id:date:subject:cc:to:from; bh=GhnyOwzpmyplfQqwsud/YgaU0V8t2GsLBuW4uAifO24=; b=LjqvBYrWW6gSIo9pgENhOOGkDLskyRicXmQa9DqVZVN0MrvIEpz0hvs6y4to/u+z0N 8550d1JWi5cseCBkKnrQCORrsdk5eW1MHvKBpHJpea4nTXmSBJYdQT48oogPMtbyjbEh Y83gQdkpZ9nFR58zTYQXhr10jWE2HU+10IIHNbj18aAKoTWk8w6N9nPmXYGYRib/ZtPQ DCZh16dgS6NTMHPmEfPErox2OiW5GXAQz0yvt0DOuCDe6F76egjrbm/Ldx7Wj0qQuWPZ llapvZnqfDm0XuRpo/ravrEIIRpa+MiJ7gPIkPmSy3qonCbFepVGFbrweS2p7jn5XTzu jiKA== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ey9-20020a0564022a0900b004536911d180si5222241edb.562.2022.09.17.20.46.56; Sat, 17 Sep 2022 20:47:22 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229566AbiIRDdb (ORCPT + 99 others); Sat, 17 Sep 2022 23:33:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48306 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229471AbiIRDd3 (ORCPT ); Sat, 17 Sep 2022 23:33:29 -0400 Received: from zju.edu.cn (spam.zju.edu.cn [61.164.42.155]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 3A83E26AFF; Sat, 17 Sep 2022 20:33:26 -0700 (PDT) Received: from ubuntu.localdomain (unknown [10.162.98.155]) by mail-app2 (Coremail) with SMTP id by_KCgA3PiJ5kSZjDuz2BQ--.48716S2; Sun, 18 Sep 2022 11:33:21 +0800 (CST) From: Duoming Zhou To: linux-kernel@vger.kernel.org, linux-usb@vger.kernel.org Cc: peter.chen@kernel.org, gregkh@linuxfoundation.org, Duoming Zhou Subject: [PATCH] usb: chipidea: fix deadlock in ci_otg_del_timer Date: Sun, 18 Sep 2022 11:33:12 +0800 Message-Id: <20220918033312.94348-1-duoming@zju.edu.cn> X-Mailer: git-send-email 2.17.1 X-CM-TRANSID: by_KCgA3PiJ5kSZjDuz2BQ--.48716S2 X-Coremail-Antispam: 1UD129KBjvJXoW7KFyDGrWkWFW8tF1xJr4fZrb_yoW8AFykpa 17Xa4ftF1DAr4Ut3W8t3Wv9ryfXanFgrZrJFyfK3yUZw1xuF1UKFykG3yFqFW7J3Z3Krsx tr4rXr93AF1fC37anT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUkI1xkIjI8I6I8E6xAIw20EY4v20xvaj40_Wr0E3s1l1IIY67AE w4v_Jr0_Jr4l8cAvFVAK0II2c7xJM28CjxkF64kEwVA0rcxSw2x7M28EF7xvwVC0I7IYx2 IY67AKxVWDJVCq3wA2z4x0Y4vE2Ix0cI8IcVCY1x0267AKxVW8Jr0_Cr1UM28EF7xvwVC2 z280aVAFwI0_GcCE3s1l84ACjcxK6I8E87Iv6xkF7I0E14v26rxl6s0DM2AIxVAIcxkEcV Aq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj6xIIjxv20xvE14v26r1j 6r18McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Yz7v_Jr0_Gr1lF7xvr2IYc2Ij64 vIr41lF7I21c0EjII2zVCS5cI20VAGYxC7MxAIw28IcxkI7VAKI48JMxAIw28IcVCjz48v 1sIEY20_GFWkJr1UJwCFx2IqxVCFs4IE7xkEbVWUJVW8JwC20s026c02F40E14v26r1j6r 18MI8I3I0E7480Y4vE14v26r106r1rMI8E67AF67kF1VAFwI0_JF0_Jw1lIxkGc2Ij64vI r41lIxAIcVC0I7IYx2IY67AKxVWUJVWUCwCI42IY6xIIjxv20xvEc7CjxVAFwI0_Jr0_Gr 1lIxAIcVCF04k26cxKx2IYs7xG6r1j6r1xMIIF0xvEx4A2jsIE14v26r1j6r4UMIIF0xvE x4A2jsIEc7CjxVAFwI0_Jr0_GrUvcSsGvfC2KfnxnUUI43ZEXa7VUbXdbUUUUUU== X-CM-SenderInfo: qssqjiasttq6lmxovvfxof0/1tbiAgoQAVZdtbgaKwAmsI X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_PASS, SPF_PASS 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 There is a deadlock in ci_otg_del_timer(), the process is shown below: (thread 1) | (thread 2) ci_otg_del_timer() | ci_otg_hrtimer_func() ... | spin_lock_irqsave() //(1) | ... ... | hrtimer_cancel() | spin_lock_irqsave() //(2) (block forever) We hold ci->lock in position (1) and use hrtimer_cancel() to wait ci_otg_hrtimer_func() to stop, but ci_otg_hrtimer_func() also need ci->lock in position (2). As a result, the hrtimer_cancel() in ci_otg_del_timer() will be blocked forever. This patch extracts hrtimer_cancel() from the protection of spin_lock_irqsave() in order that the ci_otg_hrtimer_func() could obtain the ci->lock. What`s more, there will be no race happen. Because the "next_timer" is always under the protection of spin_lock_irqsave() and we only check whether "next_timer" equals to NUM_OTG_FSM_TIMERS in the following code. Fixes: 3a316ec4c91c ("usb: chipidea: use hrtimer for otg fsm timers") Signed-off-by: Duoming Zhou --- drivers/usb/chipidea/otg_fsm.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/drivers/usb/chipidea/otg_fsm.c b/drivers/usb/chipidea/otg_fsm.c index 61b157b9c66..a7858462428 100644 --- a/drivers/usb/chipidea/otg_fsm.c +++ b/drivers/usb/chipidea/otg_fsm.c @@ -256,8 +256,10 @@ static void ci_otg_del_timer(struct ci_hdrc *ci, enum otg_fsm_timer t) ci->enabled_otg_timer_bits &= ~(1 << t); if (ci->next_otg_timer == t) { if (ci->enabled_otg_timer_bits == 0) { + spin_unlock_irqrestore(&ci->lock, flags); /* No enabled timers after delete it */ hrtimer_cancel(&ci->otg_fsm_hrtimer); + spin_lock_irqsave(&ci->lock, flags); ci->next_otg_timer = NUM_OTG_FSM_TIMERS; } else { /* Find the next timer */ -- 2.17.1