From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f171.google.com (mail-pl1-f171.google.com [209.85.214.171]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6C080486B8A for ; Tue, 25 Aug 2026 17:34:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.171 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787679268; cv=none; b=S0MqE1fMEWeSZal66Xy74XqwsgQ4gUiWyJCb2h+cSZDk9DGEYN091aFsB3gvPeKD98Dg3DVekDMIEsgHz5FTG2pxhC4Hf6Y3N2OfUHnU5XbzzPEt1rG12MELYI9zEybad9tfb29YlcrBDCG+99QnRos/8wrpn+IZ2WylZ+JrEIk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787679268; c=relaxed/simple; bh=el5u/uzfnjQoekOn8fL6ipokPGPfXCdBAO+xTEzOups=; h=Message-ID:MIME-Version:From:To:Cc:Date:Subject:Content-Type; b=Ulz5V717WxXU3ByLp5gnb5UOqAMc+hpbAswnOUvP6yBqlAVDNkVoMACjIJwQGe6+V/ztAUlIm1yML1DzKM3t2SNqQV8J8Jjd5GRcXiSpl0kJ23oyfJN5v24C+9TB8UjFnsD7NoL+ZzhpZNHClpQGkr0vIFazumrR5vvm/dLw+tU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=nc0dU+7S; arc=none smtp.client-ip=209.85.214.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="nc0dU+7S" Received: by mail-pl1-f171.google.com with SMTP id d9443c01a7336-2d032846c95so1688775ad.1 for ; Tue, 25 Aug 2026 10:34:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1787679263; x=1788284063; darn=vger.kernel.org; h=content-transfer-encoding:content-type:subject:date:cc:to:from :mime-version:message-id:from:to:cc:subject:date:message-id:reply-to :content-type; bh=57hPk6B4v1KETwUArfZSnqqRqUE15uxO2Lfbtp7crWY=; b=nc0dU+7SebjA8MEH3HQ4mCIRT4CtLZtpnkRW5AmXQYzLeN1EPOr/lmM4y6kJ9FWB5o dWmjTQ/fKlOinYi7Dj4jUiP6KqMWc6zv+tBYhOB86QdPggmtcj5JRBLZT4FrAq6QsQ+3 iIohQaD8ciyLHtXPBf9+FccbHoh9FaHxoi0dknxjJ+qBWkJDvWpi+Z31xt7om4BgNvzY J84WEx54lWjx/2XkuWuCU1jJXJflCMz+Oa0oLymnh0hXwi1Wrek8UmCLd3eJ1xve76iE sbZxCG8ULscdbiHdHBuaRKTKxiSKRAEtubVX8wBErpedbC/7RyBW8bVR0epO46Zm+nWi X+cg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1787679263; x=1788284063; h=content-transfer-encoding:content-type:subject:date:cc:to:from :mime-version:message-id:x-gm-gg:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to:content-type; bh=57hPk6B4v1KETwUArfZSnqqRqUE15uxO2Lfbtp7crWY=; b=dHq1gJPg1KZQpHlG23cd6nX33mVxMgBigD/MkfwUFgs65BnwFecGij95EQ1N75caPz JIqD6OUNQ4b44gRWtt5UVuFSmLD38/5a26p+vsvdVBKjvfXhujPQkYGaxZbYi5OLFRFe sf5RwY7ChSoAqYtF1atCqyb9ZRGHHNr4O49+VaH7ojv9zNvnoHZ9ueNLmQNF4NiXFOY2 +KBajHpLav110qbbDWwYCF/DEoBJ5tvuO+Eg0BH5naOYPGtJgMctoznLA62tUkgTCKRT YrQt8x/y8caLDToVbhfRlX/xrlWuqEfVJUQ7w8qhOQh2gFyo/2pXaNSXiFlraEinm7Iw Di2Q== X-Gm-Message-State: AFuF++npatdBwOqQXzrtDDJkJBeaDBGgkVyHe78fHRg/26X4JvV61/tf E3thGFIxP+1sEzX75bteXlZHF4Fj6GPsexWagMlomk35fl91XevcWIL2 X-Gm-Gg: AR+sD12mDMKD1tEgJco5B4hIHIVOqXQU7Oafg6jZb8zcsXIZxNHuJGhI0dTHM9uuG+T nWW8kThlC95VRG5BBS3WX43CrX36x3nIqRsNlfVV8ZOWnhLpIM7UD10xYF0R+IWdTe+qvBv+Wgq Bu2+UBX8SKwoD+Cce3WP4B9DEMRXWvhmy8UU+r2EjhZGgNect20GaV0VeG/GePSr2HkcHmO2kgd dr1gazu6rFF5xFh2QR3SctpUn+DJ7PE0iuHi0RBIRG/oqRZengOLZwZ50rmSOw+Wbz0UGcorU9p 4J0wFH3eIUv/KxULxnJ6op6RD34PgLgkR96wUtSNHNj4LEv0yMC+5pNE95oe4H+HIzAPcvo4vA9 dp1s67udHFZ8RXX5iDh/T8MDkPGtC1U3dZmYoaQ69KVYf9ShWAuYallNIEVfJHO8JoHKXOgrUWz IkcI5s0hcj+AOUwGtzCIhiTmq9764rwxkcjlKERj/FKjeSZwCdGmMqg0DZae0G9wfBetXoyuaoD rCtD9r9Cwfc/Y7ezSn2qlNW06SeHFQyBIqSGmknBeCSlyn00GGFPsgUkA== X-Received: by 2002:a17:902:e949:b0:2d6:3c2f:6a5 with SMTP id d9443c01a7336-2d64b1d68bemr710709365ad.14.1787679262972; Tue, 25 Aug 2026 10:34:22 -0700 (PDT) Received: from manush ([2406:7400:94:a5e3:4d43:ded5:1c68:7d38]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-3283d884d5esm465303eec.17.2026.08.25.10.34.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Aug 2026 10:34:22 -0700 (PDT) Message-ID: <6a8dd21e.b12cec6a.f9ff0.b637@mx.google.com> X-Mailer: git-send-email-equivalent-ps1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: "Manush Prajwal" To: jassisinghbrar@gmail.com, matthias.bgg@gmail.com, angelogioacchino.delregno@collabora.com Cc: linux-kernel@vger.kernel.org, linux-mediatek@lists.infradead.org, linux-arm-kernel@lists.infradead.org, Markus.Elfring@web.de Date: 25 Aug 2026 23:04:22 +0530 Subject: [PATCH v4] mailbox: mtk-cmdq: fix runtime PM usage counter leak in cmdq_mbox_flush() Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable cmdq_mbox_flush() calls pm_runtime_get_sync() and returns its err= or=0D=0Acode directly on failure. Per Documentation/power/runtime= _pm.rst,=0D=0Apm_runtime_get_sync() does not drop the usage count= er on error, so the=0D=0Acaller is expected to release the refere= nce itself. The early return=0D=0Ahere skipped that, leaking a ru= ntime PM usage count on cmdq->mbox.dev=0D=0Aon every failed resum= e. v2 fixed this by switching to=0D=0Apm_runtime_resume_and_get()= , which performs the get-and-put=0D=0Ainternally on failure.=0D=0A= =0D=0AMarkus Elfring additionally pointed out that the "out:" and= "wait:"=0D=0Alabels duplicate the same pm_runtime_mark_last_busy= () +=0D=0Apm_runtime_put_autosuspend() + return sequence. That du= plication=0D=0Ahides a second, separate leak: the "wait:" path's = timeout branch=0D=0A(readl_poll_timeout_atomic() failing) returns= -EFAULT directly,=0D=0Awithout ever calling pm_runtime_mark_last= _busy() /=0D=0Apm_runtime_put_autosuspend(), so the pm_runtime re= ference taken at=0D=0Afunction entry is leaked on every polling t= imeout too.=0D=0A=0D=0AFix both by merging the two exit sequences= into one common "out_pm:"=0D=0Alabel reached by both "out:" and = "wait:", using "ret" to carry the=0D=0Areturn value. "ret" is exp= licitly reset to 0 on both success paths=0D=0Abefore reaching "ou= t_pm:", since pm_runtime_resume_and_get() can=0D=0Areturn a posit= ive value (e.g. 1) on success, not just 0, and that=0D=0Astale va= lue must not leak into the function's return value.=0D=0A=0D=0AVe= rified the merged exit path with a standalone model of the four=0D=0A= possible outcomes (initial get failure, "out:" success, "wait:"=0D=0A= success, "wait:" timeout): the runtime PM refcount returns to its= =0D=0Astarting value and the return code is correct on every path= .=0D=0A=0D=0ASigned-off-by: Manush Prajwal =0D=0A---=0D=0Av4:=0D=0A- Add parentheses to the function n= ame in the summary phrase=0D=0A (cmdq_mbox_flush -> cmdq_mbox_fl= ush()), per Markus Elfring's review.=0D=0A=0D=0A- Reformat this c= hangelog so each version identifier stands on its=0D=0A own line= and blocks are separated by blank lines, per Markus=0D=0A Elfri= ng's review.=0D=0A=0D=0A- No functional change from v3. Still no = Fixes tag: I don't have a=0D=0A git history for this file availa= ble in my current environment to=0D=0A identify the commit that = introduced this exit path with confidence,=0D=0A and would rathe= r leave the tag off than guess a SHA. Still happy to=0D=0A add i= t in a v5 if Markus or anyone else on Cc can point at the right=0D=0A= commit.=0D=0A=0D=0Av3:=0D=0A- Fix the pm_runtime leak on the "w= ait:" timeout path found while=0D=0A addressing Markus Elfring's= duplicate-code comment, by merging the=0D=0A "out:" and "wait:"= exit sequences into a single "out_pm:" label.=0D=0A=0D=0A- Add M= atthias Brugger and AngeloGioacchino Del Regno plus the=0D=0A li= nux-mediatek and linux-arm-kernel lists to Cc, per MAINTAINERS'=0D=0A= "ARM/Mediatek SoC support" entry (matches any drivers/mailbox/m= tk-*.c=0D=0A filename), per Markus Elfring's review.=0D=0A=0D=0A= v2:=0D=0A- Use pm_runtime_resume_and_get() instead of pm_runtime_= get_sync()=0D=0A plus a manual pm_runtime_put_noidle() on the er= ror path, per Markus=0D=0A Elfring's review.=0D=0A=0D=0A drivers= /mailbox/mtk-cmdq-mailbox.c | 15 +++++++--------=0D=0A 1 file cha= nged, 7 insertions(+), 8 deletions(-)=0D=0A=0D=0Adiff --git a/dri= vers/mailbox/mtk-cmdq-mailbox.c b/drivers/mailbox/mtk-cmdq-mailbo= x.c=0D=0Aindex e523c84b4..d4e5f6a7b 100644=0D=0A--- a/drivers/mai= lbox/mtk-cmdq-mailbox.c=0D=0A+++ b/drivers/mailbox/mtk-cmdq-mailb= ox.c=0D=0A@@ -565,7 +565,7 @@ static int cmdq_mbox_flush(struct m= box_chan *chan, unsigned long timeout)=0D=0A int ret;=0D=0A= =0D=0A- ret =3D pm_runtime_get_sync(cmdq->mbox.dev);=0D=0A+= ret =3D pm_runtime_resume_and_get(cmdq->mbox.dev);=0D=0A = if (ret < 0)=0D=0A return ret;=0D=0A=0D=0A@@= -590,22 +590,21 @@ static int cmdq_mbox_flush(struct mbox_chan *= chan, unsigned long timeout)=0D=0A cmdq_thread_disable(cmd= q, thread);=0D=0A=0D=0A out:=0D=0A spin_unlock_irqrestore(= &thread->chan->lock, flags);=0D=0A- pm_runtime_mark_last_bu= sy(cmdq->mbox.dev);=0D=0A- pm_runtime_put_autosuspend(cmdq-= >mbox.dev);=0D=0A-=0D=0A- return 0;=0D=0A+ ret =3D 0;= =0D=0A+ goto out_pm;=0D=0A=0D=0A wait:=0D=0A cmdq_th= read_resume(thread);=0D=0A spin_unlock_irqrestore(&thread-= >chan->lock, flags);=0D=0A+ ret =3D 0;=0D=0A if (rea= dl_poll_timeout_atomic(thread->base + CMDQ_THR_ENABLE_TASK,=0D=0A= enable, enable =3D=3D 0, 1,= timeout)) {=0D=0A dev_err(cmdq->mbox.dev, "Fail t= o wait GCE thread 0x%x done\n",=0D=0A (u32= )(thread->base - cmdq->base));=0D=0A-=0D=0A- return= -EFAULT;=0D=0A+ ret =3D -EFAULT;=0D=0A }=0D=0A= +=0D=0A+out_pm:=0D=0A pm_runtime_mark_last_busy(cmdq->mbox= .dev);=0D=0A pm_runtime_put_autosuspend(cmdq->mbox.dev);=0D=0A= - return 0;=0D=0A+ return ret;=0D=0A }=0D=0A--=0D=0A2= .46.2.windows.1