Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp2089438pxb; Wed, 9 Feb 2022 10:35:51 -0800 (PST) X-Google-Smtp-Source: ABdhPJzunH4fKQ5ZPlnnreLGkQ6Q0h0pjYQu46o+WeFHHI03jf+7AFuBEuQYWxdQag84QSSIej+P X-Received: by 2002:a63:af08:: with SMTP id w8mr2976489pge.1.1644431751289; Wed, 09 Feb 2022 10:35:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1644431751; cv=none; d=google.com; s=arc-20160816; b=R7Unx0CjbgmaImFbEPBYaDwJIFlNIKPQXqYeKT1/ouBddYsaHlMUBl3hm3kyRp84lC BFY+jsbRLVQtoWFlkWUEX3V/E7izMxkSbpBe/BmlnV1O8Xq3tYPNb2ItCalqJCCsVd1g CwvMofOy+p1WbE1FE5N2G2fKuRP+k9YHKaOB3tAcDnUN1zhhUZlkqhP9ly1KPPb165L9 2veQcP9hodzg8JD7CW2QQc55HFaNYD298TU73CT0bq+qxXVMkjglELTLSvySq2Ea1rXS Xfy/iKy2F8OXePMYEdtJTRN1RfmmkOjFlTtLzxqcgG7OTEZaAg9NRx4RJMeI3IXXKCeK vCKw== 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=G145ATgYlktTZZZBYz8emfwGOJAdFw3VZV/qjZY83wU=; b=zgn8ctoY9/R9KQKREAfe0/hd1MYbiIGCzLc38ba7EcI0Fx7w8H0yj1tg0/9+TXgj78 ZvGRcOib3zOiEey7sHiBVb7UlMRZc30HuWOTG3uWK4EsA5WWpHnPqs6kV/tPb0Jr3shj PMjxxVfTbU3b7HAgROMQn3NOK2/V+kK5UlgMnp7fpkfESkMOlKw8b7bvVw+RI8NDmwUu paTYA/lJgokiDuGH2DmXER0aseaFe5EXqB4j7BvU4NiBT9nTwxN/CaBosZ9wZqVTdzcL 7aXDgdZjwyfrdR7TT51PzkdsnbePm57bcn9VyFhsoOH04i0trqxBlAtNG78CU9fQvDQn uZHg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Ef0C2Wcv; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id p2si14934235plq.147.2022.02.09.10.35.38; Wed, 09 Feb 2022 10:35:51 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Ef0C2Wcv; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S235054AbiBIOaf (ORCPT + 99 others); Wed, 9 Feb 2022 09:30:35 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60604 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231747AbiBIOae (ORCPT ); Wed, 9 Feb 2022 09:30:34 -0500 Received: from mga06.intel.com (mga06.intel.com [134.134.136.31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A5EA4C0613C9; Wed, 9 Feb 2022 06:30:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1644417037; x=1675953037; h=date:from:to:cc:subject:message-id:references: mime-version:content-transfer-encoding:in-reply-to; bh=JN0xlf+7SwfocC10ULLvpqebae/Mg6iq3gz8bo8oEj8=; b=Ef0C2WcvGUMIFoyRHOnC1aaXG3Ftk9YU43gYBwWhWj8TtPiZe6lGK2+D NqZsjGDUNZAUzlIK1Ix622cymB8BeRH90KF33iJxgPljzuQtiM5ZcBKNI 1S/ejvWZxucK6o7y1hA+61SK7dEr+QNJ5+VgUFsx391fQIuYckKtR4nKV 6f//X4bJzykkSAeGlBNzGlGQvm0kE3GN1GNlD29RD1W7KjVu5/Cs3C/CI lZPbMIo8dxFuyWd3GWy8R3FiaRRXTOi0bCnjJEM96MEYXExUP21hP1VnQ izsmy8+8Os3VcM4CMP+GoLFWi+v06TYh4cyfvhxlg91UrZHeKNTZfywn8 g==; X-IronPort-AV: E=McAfee;i="6200,9189,10252"; a="309958356" X-IronPort-AV: E=Sophos;i="5.88,355,1635231600"; d="scan'208,223";a="309958356" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 09 Feb 2022 06:30:36 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.88,355,1635231600"; d="scan'208,223";a="678608906" Received: from kuha.fi.intel.com ([10.237.72.185]) by fmsmga001.fm.intel.com with SMTP; 09 Feb 2022 06:30:32 -0800 Received: by kuha.fi.intel.com (sSMTP sendmail emulation); Wed, 09 Feb 2022 16:30:31 +0200 Date: Wed, 9 Feb 2022 16:30:31 +0200 From: Heikki Krogerus To: Jia-Ju Bai Cc: Greg KH , kyletso@google.com, jackp@codeaurora.org, 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> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="DBjLCJp5eah5t2cJ" Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <037de7ac-e210-bdf5-ec7a-8c0c88a0be20@gmail.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 --DBjLCJp5eah5t2cJ Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit 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)? thanks, -- heikki --DBjLCJp5eah5t2cJ Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="0001-usb-typec-ucsi-Test-fix.patch" From 2ad06425a3df7be656f8a5b3c202aab45554fd17 Mon Sep 17 00:00:00 2001 From: Heikki Krogerus Date: Wed, 9 Feb 2022 17:27:19 +0300 Subject: [PATCH] usb: typec: ucsi: Test fix Interim. Not-Signed-off-by: Heikki Krogerus --- drivers/usb/typec/ucsi/ucsi.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/drivers/usb/typec/ucsi/ucsi.c b/drivers/usb/typec/ucsi/ucsi.c index f0c2fa19f3e0f..225104beda8be 100644 --- a/drivers/usb/typec/ucsi/ucsi.c +++ b/drivers/usb/typec/ucsi/ucsi.c @@ -956,14 +956,18 @@ static int ucsi_dr_swap(struct typec_port *port, enum typec_data_role role) if (ret < 0) goto out_unlock; + mutex_unlock(&con->lock); + if (!wait_for_completion_timeout(&con->complete, msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS))) - ret = -ETIMEDOUT; + return -ETIMEDOUT; + + return 0; out_unlock: mutex_unlock(&con->lock); - return ret < 0 ? ret : 0; + return ret; } static int ucsi_pr_swap(struct typec_port *port, enum typec_role role) @@ -992,11 +996,13 @@ static int ucsi_pr_swap(struct typec_port *port, enum typec_role role) if (ret < 0) goto out_unlock; + mutex_unlock(&con->lock); + if (!wait_for_completion_timeout(&con->complete, - msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS))) { - ret = -ETIMEDOUT; - goto out_unlock; - } + msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS))) + return -ETIMEDOUT; + + mutex_lock(&con->lock); /* Something has gone wrong while swapping the role */ if (UCSI_CONSTAT_PWR_OPMODE(con->status.flags) != @@ -1372,6 +1378,7 @@ void ucsi_unregister(struct ucsi *ucsi) ucsi->ops->async_write(ucsi, UCSI_CONTROL, &cmd, sizeof(cmd)); for (i = 0; i < ucsi->cap.num_connectors; i++) { + complete(&ucsi->connector[i].complete); cancel_work_sync(&ucsi->connector[i].work); ucsi_unregister_partner(&ucsi->connector[i]); ucsi_unregister_altmodes(&ucsi->connector[i], -- 2.34.1 --DBjLCJp5eah5t2cJ--