Received: by 2002:a05:6602:2086:0:0:0:0 with SMTP id a6csp4547112ioa; Wed, 27 Apr 2022 06:15:55 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzg7Poce0m5q0EdZ23JenkFwB81pJDg3wwV3Gg6fixpRGQ/U0QFw8hfHV3BRihcYW11YXfp X-Received: by 2002:a05:6870:538f:b0:e1:def7:7640 with SMTP id h15-20020a056870538f00b000e1def77640mr11063421oan.34.1651065355521; Wed, 27 Apr 2022 06:15:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1651065355; cv=none; d=google.com; s=arc-20160816; b=ImWNt22V0qcX+IPQeFqTF8jWQnVpJb5xcSxWESpw1MCy/d5JhCTGKr8jhtXQd5GZ8O XYvVX64D+gPfMLgM7jvc0hXjK5MLfF9WU55QNQAx85lYme0Pzn9gHya2wS+K1MqwCyq9 p4ijg0Udh6CT7jp9CRiPS8IH5bAH4htwqz6QjRNAd1ZF/3J/nDB69bMcMWFKU+gowh3H n/dNyDv+UN/fwi1rNJMCWQR4jYUnqgNsqSwOnediY1U4K2sFkapOVHV4bTl99K6jiTCo PumyOUgURfxNPO3veI0ecLGhycWivz0bI7fcd+MbYrI1ZGcxuBNaxXeGKnx6Qp0XNJgH yUNg== 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:authorized-sender; bh=lRKrBT5BnrbE9ZCnEjsqIa6zKQDhgE+M+aZ793noyFI=; b=CE82af4RQlf+vw0RNeNspk5I6ZPu5E332rvwdww6ebIPyLY//qFDSPwI2c9rY0cz76 ZNjtcKkSH2rxaHAksY21Z/720iNxLyRzJxvmSz35E+WlOAUTd3gV9Ph6EHCDeqb5yQg9 ysL4muJdsaCZ8u/FnElgnUrnNOprtgu87Scz4wzMdQ35d4U+inIIksY6qwTnBkFKjKCX Bv6xD+eD1c8TQexGcb8jIDmGJYIJfms8S9RLRHeWzEalcz8VONgLPRKx+nWi1kg9bBGi Ep9iAxZ4RDZgd/4RYZCiwO1df1gBGyFLLu5RHtB5QFc+2RRvuRAdReIOK0ADaD6WsvfL B8dg== ARC-Authentication-Results: i=1; mx.google.com; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id e23-20020a9d63d7000000b0060414324b81si691163otl.333.2022.04.27.06.15.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Apr 2022 06:15:55 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 92AE018B20; Wed, 27 Apr 2022 05:48:47 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234715AbiD0Mvr (ORCPT + 99 others); Wed, 27 Apr 2022 08:51:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39456 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234714AbiD0Mvk (ORCPT ); Wed, 27 Apr 2022 08:51:40 -0400 X-Greylist: delayed 62 seconds by postgrey-1.37 at lindbergh.monkeyblade.net; Wed, 27 Apr 2022 05:48:28 PDT Received: from bin-mail-out-05.binero.net (bin-mail-out-05.binero.net [195.74.38.228]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BD4C62CC8A for ; Wed, 27 Apr 2022 05:48:28 -0700 (PDT) X-Halon-ID: 28f2a90e-c628-11ec-9da0-005056917f90 Authorized-sender: andreas@gaisler.com Received: from [192.168.0.25] (h-98-128-223-123.na.cust.bahnhof.se [98.128.223.123]) by bin-vsp-out-02.atm.binero.net (Halon) with ESMTPA id 28f2a90e-c628-11ec-9da0-005056917f90; Wed, 27 Apr 2022 14:47:14 +0200 (CEST) Message-ID: <2f5295c1-d787-84a5-1b3e-813f96dd4ae2@gaisler.com> Date: Wed, 27 Apr 2022 14:47:12 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.7.0 Subject: Re: [PATCH net] drivers: net: can: Fix deadlock in grcan_close() Content-Language: en-US To: Oliver Hartkopp , Duoming Zhou , linux-kernel@vger.kernel.org Cc: wg@grandegger.com, mkl@pengutronix.de, davem@davemloft.net, kuba@kernel.org, pabeni@redhat.com, linux-can@vger.kernel.org, netdev@vger.kernel.org References: <20220425042400.66517-1-duoming@zju.edu.cn> From: Andreas Larsson In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3.7 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,NICE_REPLY_A, RDNS_NONE,SPF_HELO_NONE 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-kernel@vger.kernel.org On 2022-04-26 21:12, Oliver Hartkopp wrote: > On 25.04.22 06:24, Duoming Zhou wrote: >> There are deadlocks caused by del_timer_sync(&priv->hang_timer) >> and del_timer_sync(&priv->rr_timer) in grcan_close(), one of >> the deadlocks are shown below: >> >>     (Thread 1)              |      (Thread 2) >>                             | grcan_reset_timer() >> grcan_close()              |  mod_timer() >>   spin_lock_irqsave() //(1) |  (wait a time) >>   ...                       | grcan_initiate_running_reset() >>   del_timer_sync()          |  spin_lock_irqsave() //(2) >>   (wait timer to stop)      |  ... >> >> We hold priv->lock in position (1) of thread 1 and use >> del_timer_sync() to wait timer to stop, but timer handler >> also need priv->lock in position (2) of thread 2. >> As a result, grcan_close() will block forever. >> >> This patch extracts del_timer_sync() from the protection of >> spin_lock_irqsave(), which could let timer handler to obtain >> the needed lock. >> >> Signed-off-by: Duoming Zhou >> --- >>   drivers/net/can/grcan.c | 2 ++ >>   1 file changed, 2 insertions(+) >> >> diff --git a/drivers/net/can/grcan.c b/drivers/net/can/grcan.c >> index d0c5a7a60da..1189057b5d6 100644 >> --- a/drivers/net/can/grcan.c >> +++ b/drivers/net/can/grcan.c >> @@ -1102,8 +1102,10 @@ static int grcan_close(struct net_device *dev) >>       priv->closing = true; >>       if (priv->need_txbug_workaround) { >> +        spin_unlock_irqrestore(&priv->lock, flags); >>           del_timer_sync(&priv->hang_timer); >>           del_timer_sync(&priv->rr_timer); >> +        spin_lock_irqsave(&priv->lock, flags); > > It looks weird to unlock and re-lock the operations like this. This > breaks the intended locking for the closing process. > > Isn't there any possibility to e.g. move that entire if-section before > the lock? All functions wishing to start the timers both check priv->closing and then, if false, start the timer within the priv->lock spinlock. Given that, it should be ok that del_timer_sync is not done within the spinlock as therefore no one can restart any timers after priv->closing has been set to true. It looks a bit weird, but setting priv->closing to true needs to happen within the priv->lock spinlock protection and needs to happen before del_timer_sync to avoid a race between grcan_close and someone starting the timer. Reviewed-by: Andreas Larsson -- Andreas Larsson