Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp5112473pxb; Mon, 28 Mar 2022 08:06:57 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzHyBL54GSeANue2iy9iKvC3grOiWRNAu+i8S0kChvI8oYocl6UGpkFrQXZzvL2ADc7Rp34 X-Received: by 2002:a17:906:2bc5:b0:6cd:e676:3624 with SMTP id n5-20020a1709062bc500b006cde6763624mr28375908ejg.277.1648480015600; Mon, 28 Mar 2022 08:06:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1648480015; cv=none; d=google.com; s=arc-20160816; b=FnQYwPOCPbvitY95k9PwdtNc4OWO4gY13Xfdt6zY9/d4rdKqO0ZpnCU3gOHixhFNMp hgfvmL1m2n1gNJCoQdpknei4xZA2e5dtYaY1FsmkRo3ClT757TByL4niXYfdtF8UTgcR 7TsXDjAMPjb3tRkJHE3go/ZvcfFEnEBP4c0o+z5e9Vbf7aNwFQyAtcUB52BKu6Jh6dU1 C0/fqoL3r7fpGvIlYMbMZeHAKySKA8lCKTyabaBg/a2y/TONfDBgB/DG1FXZZr5TM9IM mBZ8yfmqPgn5jXRs3ENF34IXDCDPrPgeOlLkpwfyrXVUHnPfqWl09ysViIUReZY3RuA2 YYqA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=JP0eI3tKZRrhUe5AMa3AUFzid2DyMS+ZYWhh3aJBvQw=; b=w59ZnFjyrzyrBSS5c4nKs7yjjVEWFtYOEXSm9VauC+lSHQ+hYJzYQQuFXThGh3o0ZR QAg36l+aP7lZgQvtkLwTmwGCDP+SIlE5Pt5vMzYkx3xx1k53Nr5pMpR0e50o1Au62n6/ 3j3yVPxxEJxKZ/JUrzo4UntcIKzCqHq+231aFAQ9w561HVGOpRUMB6gjDTJui9O0Qaoj SToM/yF4u8LRmBLBlCL8ShKWR83l9ia+tvvPAMt3w2D9GZvI7H3S0pejC9v0RvCui+2w W/m+grgeCKCUahgnj0dGNi8GaRhyFqA8GfPAjPbnElJHnj9C1dW9QCdfqtO5xDDVKJRq HB/Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="hPL/X0jF"; 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=intel.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id gg11-20020a170906e28b00b006df76385c7csi13652354ejb.284.2022.03.28.08.06.25; Mon, 28 Mar 2022 08:06:55 -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=@intel.com header.s=Intel header.b="hPL/X0jF"; 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=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239362AbiC1Iao (ORCPT + 99 others); Mon, 28 Mar 2022 04:30:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52554 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239371AbiC1Iaj (ORCPT ); Mon, 28 Mar 2022 04:30:39 -0400 Received: from mga17.intel.com (mga17.intel.com [192.55.52.151]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1F93114090; Mon, 28 Mar 2022 01:28:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1648456137; x=1679992137; h=date:from:to:cc:subject:message-id:references: mime-version:content-transfer-encoding:in-reply-to; bh=bX7y6PYsoKmkrmqLzNupZvbiwlzgwmoedmetGhJMRzs=; b=hPL/X0jFULZlU73qPGP2e6OsawjNVFfCoXYEibm3BmS5rGPiuczOAySU /Rcn4JsTdlGH6yG+XXA/Pu9V60EGa1baRb+5rZEXb5ZD8uOEWx5kqz3gX vIWPGor57quoXZkCM+n+HAWPvWFqowv32dO3zQVyrcR6WSJvprCNm8ShN IUPfahaY/NUQktIJGfG40LPrEnAVOs3toXAqI8qkwCzo31kvAZkq8yKnd 8YZEIRRUhPjWQ6k1CRzabImukS/J9afjeY9psTkyKkjK26YPgVw/06/AZ rtcBV6wZMvbpU/BrRN9dwuNMcuf7ZsQAx8ZNNAJeSaE//C4Zq4JJOZZfk w==; X-IronPort-AV: E=McAfee;i="6200,9189,10299"; a="239542233" X-IronPort-AV: E=Sophos;i="5.90,216,1643702400"; d="scan'208";a="239542233" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Mar 2022 01:28:56 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.90,216,1643702400"; d="scan'208";a="694288700" Received: from kuha.fi.intel.com ([10.237.72.185]) by fmsmga001.fm.intel.com with SMTP; 28 Mar 2022 01:28:53 -0700 Received: by kuha.fi.intel.com (sSMTP sendmail emulation); Mon, 28 Mar 2022 11:28:52 +0300 Date: Mon, 28 Mar 2022 11:28:52 +0300 From: Heikki Krogerus To: Jack Pham Cc: Jia-Ju Bai , Greg KH , kyletso@google.com, andy.shevchenko@gmail.com, unixbhaskar@gmail.com, subbaram@codeaurora.org, mrana@codeaurora.org, "linux-usb@vger.kernel.org" , linux-kernel Subject: Re: [BUG] usb: typec: ucsi: possible deadlock in ucsi_pr_swap() and ucsi_handle_connector_change() Message-ID: References: <037de7ac-e210-bdf5-ec7a-8c0c88a0be20@gmail.com> <20220325203959.GA19752@jackp-linux.qualcomm.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20220325203959.GA19752@jackp-linux.qualcomm.com> X-Spam-Status: No, score=-4.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,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-kernel@vger.kernel.org On Fri, Mar 25, 2022 at 01:39:59PM -0700, Jack Pham wrote: > Hi Heikki, > > On Wed, Feb 09, 2022 at 04:30:31PM +0200, Heikki Krogerus wrote: > > On Wed, Feb 09, 2022 at 11:50:57AM +0800, Jia-Ju Bai wrote: > > > Hello, > > > > > > My static analysis tool reports a possible deadlock in the ucsi driver in > > > Linux 5.16: > > > > > > ucsi_pr_swap() > > > ? mutex_lock(&con->lock); --> Line 962 (Lock A) > > > ? wait_for_completion_timeout(&con->complete, ...) --> Line 981 (Wait X) > > > > > > ucsi_handle_connector_change() > > > ? mutex_lock(&con->lock); --> Line 763 (Lock A) > > > ? complete(&con->complete); --> Line 782 (Wake X) > > > ? complete(&con->complete); --> Line 807 (Wake X) > > > > > > When ucsi_pr_swap() is executed, "Wait X" is performed by holding "Lock A". > > > If ucsi_handle_connector_change() is executed at this time, "Wake X" cannot > > > be performed to wake up "Wait X" in ucsi_handle_connector_change(), because > > > "Lock A" has been already held by ucsi_handle_connector_change(), causing a > > > possible deadlock. > > > I find that "Wait X" is performed with a timeout, to relieve the possible > > > deadlock; but I think this timeout can cause inefficient execution. > > > > > > I am not quite sure whether this possible problem is real. > > > Any feedback would be appreciated, thanks :) > > > > This is probable a regression from commit ad74b8649bea ("usb: typec: > > ucsi: Preliminary support for alternate modes"). Can you test does > > this patch fix the issue (attached)? > > We encountered a slightly different twist to this bug. Instead of > deadlocking, we see that the dr_swap() / pr_swap() operations actually > jump out of the wait_for_completion_timeout() immediately, even before > any partner change occurs. This is because the con->complete may > already have its done flag set to true from the first time > ucsi_handle_connector_change() runs, and is never reset after that. > > In addition to the unlocking below, I think we need to also add > reinit_completion() calls at the start of ucsi_{pr,dr}_swap(). OK. I'll add that to the patch. thanks, -- heikki