From nobody Tue Sep 9 16:53:39 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id CA012C76196 for ; Thu, 6 Apr 2023 13:47:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238739AbjDFNrN (ORCPT ); Thu, 6 Apr 2023 09:47:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51690 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238344AbjDFNrL (ORCPT ); Thu, 6 Apr 2023 09:47:11 -0400 Received: from mx0b-001ae601.pphosted.com (mx0a-001ae601.pphosted.com [67.231.149.25]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C62086EB6 for ; Thu, 6 Apr 2023 06:47:09 -0700 (PDT) Received: from pps.filterd (m0077473.ppops.net [127.0.0.1]) by mx0a-001ae601.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 3365A8Qr028169; Thu, 6 Apr 2023 08:46:47 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cirrus.com; h=from : to : cc : subject : date : message-id : mime-version : content-transfer-encoding : content-type; s=PODMain02222019; bh=IcGX+N/DevlgoXLY+O/yuLAaJ4V5pIBbxyzs7p+ew1c=; b=BTpr/ZQe2OO7a7rRoKLao2n+SSPAhwV5x2CS6PSkRMaNZVVV4/gu3n46jcC+ooJw3vzQ 5VcYSGcy8Z7W+ViIwfb6sYTCIkMQefWRkpcMvhSyobp3eCJwcUeFTyIMUMi8TQ39VZ+0 JgUgUajlwx5cvRGycFuew8ciopldw7gE0CBOG9Z0DwePl+2/Aiz7FHysq+OJkIcDOSXa a9MqW0ylDdoPnPTzyVjbLncb6ZxXlgNT+MaC9GXjOMe02tjLZFirYybHMhHadNlVBoWO 5Og0cT1DhB9xWyuxN7MYQTiCgXyiIMZNm0INKHkLHE7MH6TcSoLyDwC2DZvB40Rzmvt6 Lw== Received: from ediex02.ad.cirrus.com ([84.19.233.68]) by mx0a-001ae601.pphosted.com (PPS) with ESMTPS id 3ppj13fkhb-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 06 Apr 2023 08:46:47 -0500 Received: from ediex01.ad.cirrus.com (198.61.84.80) by ediex02.ad.cirrus.com (198.61.84.81) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.26; Thu, 6 Apr 2023 08:46:45 -0500 Received: from ediswmail.ad.cirrus.com (198.61.86.93) by ediex01.ad.cirrus.com (198.61.84.80) with Microsoft SMTP Server id 15.2.1118.26 via Frontend Transport; Thu, 6 Apr 2023 08:46:45 -0500 Received: from EDIN4L06LR3.ad.cirrus.com (EDIN4L06LR3.ad.cirrus.com [198.61.65.178]) by ediswmail.ad.cirrus.com (Postfix) with ESMTP id 44277478; Thu, 6 Apr 2023 13:46:43 +0000 (UTC) From: Richard Fitzgerald To: CC: , , , , , Richard Fitzgerald Subject: [PATCH RESEND] soundwire: bus: Fix unbalanced pm_runtime_put() causing usage count underflow Date: Thu, 6 Apr 2023 14:46:40 +0100 Message-ID: <20230406134640.8582-1-rf@opensource.cirrus.com> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Proofpoint-GUID: q4nUoijmp0C7Bu09z2VJiLcxffgeGEVR X-Proofpoint-ORIG-GUID: q4nUoijmp0C7Bu09z2VJiLcxffgeGEVR X-Proofpoint-Spam-Reason: safe Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This reverts commit 443a98e649b4 ("soundwire: bus: use pm_runtime_resume_and_get()") Change calls to pm_runtime_resume_and_get() back to pm_runtime_get_sync(). This fixes a usage count underrun caused by doing a pm_runtime_put() even though pm_runtime_resume_and_get() returned an error. The three affected functions ignore -EACCES error from trying to get pm_runtime, and carry on, including a put at the end of the function. But pm_runtime_resume_and_get() does not increment the usage count if it returns an error. So in the -EACCES case you must not call pm_runtime_put(). The documentation for pm_runtime_get_sync() says: "Consider using pm_runtime_resume_and_get() ... as this is likely to result in cleaner code." In this case I don't think it results in cleaner code because the pm_runtime_put() at the end of the function would have to be conditional on the return value from pm_runtime_resume_and_get() at the top of the function. pm_runtime_get_sync() doesn't have this problem because it always increments the count, so always needs a put. The code can just flow through and do the pm_runtime_put() unconditionally. Signed-off-by: Richard Fitzgerald Reviewed-by: Pierre-Louis Bossart --- drivers/soundwire/bus.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/drivers/soundwire/bus.c b/drivers/soundwire/bus.c index e157a39a82ce..1ea6a64f8c4a 100644 --- a/drivers/soundwire/bus.c +++ b/drivers/soundwire/bus.c @@ -584,9 +584,11 @@ int sdw_nread(struct sdw_slave *slave, u32 addr, size_= t count, u8 *val) { int ret; =20 - ret =3D pm_runtime_resume_and_get(&slave->dev); - if (ret < 0 && ret !=3D -EACCES) + ret =3D pm_runtime_get_sync(&slave->dev); + if (ret < 0 && ret !=3D -EACCES) { + pm_runtime_put_noidle(&slave->dev); return ret; + } =20 ret =3D sdw_nread_no_pm(slave, addr, count, val); =20 @@ -613,9 +615,11 @@ int sdw_nwrite(struct sdw_slave *slave, u32 addr, size= _t count, const u8 *val) { int ret; =20 - ret =3D pm_runtime_resume_and_get(&slave->dev); - if (ret < 0 && ret !=3D -EACCES) + ret =3D pm_runtime_get_sync(&slave->dev); + if (ret < 0 && ret !=3D -EACCES) { + pm_runtime_put_noidle(&slave->dev); return ret; + } =20 ret =3D sdw_nwrite_no_pm(slave, addr, count, val); =20 @@ -1590,9 +1594,10 @@ static int sdw_handle_slave_alerts(struct sdw_slave = *slave) =20 sdw_modify_slave_status(slave, SDW_SLAVE_ALERT); =20 - ret =3D pm_runtime_resume_and_get(&slave->dev); + ret =3D pm_runtime_get_sync(&slave->dev); if (ret < 0 && ret !=3D -EACCES) { dev_err(&slave->dev, "Failed to resume device: %d\n", ret); + pm_runtime_put_noidle(&slave->dev); return ret; } =20 --=20 2.30.2