From nobody Tue Feb 10 10:54:51 2026 Received: from relmlie5.idc.renesas.com (relmlor1.renesas.com [210.160.252.171]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 86F91378810; Fri, 30 Jan 2026 12:25:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=210.160.252.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769775911; cv=none; b=N0GocKFFAbJFju0q6VdnWucKBE6K0i6MOtupvRDXvUw/hG6ylSjrEM2JFlNNASrnTseP17pijANeC+9QjURNJdNyccrTnp/lnNNbkROUzg7xp43aV7bHOwuRt8dLdNjEVJs/u1umnKOU7+v/wZldaiJleBkKk/8jgTQxgVSjdpw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769775911; c=relaxed/simple; bh=xhmPVXoMDCu27t4fzv10/A0dd5EUYSmW7DlIX7IuBis=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=dFV/qsodaF+euR+OJWa1BfNSVnstGCGhW7mDSBJvkFTuMFTJojzUsCkjJ4i5O9WbycTXeN2MUkaL1lX0BIhZT6eLOnrdVWGdWrWvKgxC/SPbKjfdZmwwI48smQGmebyoEaN9SHgicfbHfUI6lFlG6SI+4TdTAnjSDo+FNCSqPr8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=renesas.com; spf=pass smtp.mailfrom=renesas.com; arc=none smtp.client-ip=210.160.252.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=renesas.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=renesas.com X-CSE-ConnectionGUID: 4kL42DtVQM+HTyQ2uqJ0Ww== X-CSE-MsgGUID: 2sbEYGBaRJ2+U+YnQiScmA== Received: from unknown (HELO relmlir6.idc.renesas.com) ([10.200.68.152]) by relmlie5.idc.renesas.com with ESMTP; 30 Jan 2026 21:25:09 +0900 Received: from demon-pc.localdomain (unknown [10.226.92.78]) by relmlir6.idc.renesas.com (Postfix) with ESMTP id 044F84177CAA; Fri, 30 Jan 2026 21:25:04 +0900 (JST) From: Cosmin Tanislav To: Biju Das , William Breathitt Gray , =?UTF-8?q?Uwe=20Kleine-K=C3=B6nig?= , Lee Jones , Thierry Reding Cc: linux-iio@vger.kernel.org, linux-renesas-soc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-pwm@vger.kernel.org, Cosmin Tanislav , stable@vger.kernel.org Subject: [PATCH 4/5] counter: rz-mtu3-cnt: prevent counter from being toggled multiple times Date: Fri, 30 Jan 2026 14:23:52 +0200 Message-ID: <20260130122353.2263273-5-cosmin-gabriel.tanislav.xa@renesas.com> X-Mailer: git-send-email 2.52.0 In-Reply-To: <20260130122353.2263273-1-cosmin-gabriel.tanislav.xa@renesas.com> References: <20260130122353.2263273-1-cosmin-gabriel.tanislav.xa@renesas.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Runtime PM counter is incremented / decremented each time the sysfs enable file is written to. If user writes 0 to the sysfs enable file multiple times, runtime PM usage count underflows, generating the following message. rz-mtu3-counter rz-mtu3-counter.0: Runtime PM usage count underflow! At the same time, hardware registers end up being accessed with clocks off in rz_mtu3_terminate_counter() to disable an already disabled channel. If user writes 1 to the sysfs enable file multiple times, runtime PM usage count will be incremented each time, requiring the same number of 0 writes to get it back to 0. If user writes 0 to the sysfs enable file while PWM is in progress, PWM is stopped without counter being the owner of the underlying MTU3 channel. Check against the cached count_is_enabled value and exit if the user is trying to set the same enable value. Cc: stable@vger.kernel.org Fixes: 0be8907359df ("counter: Add Renesas RZ/G2L MTU3a counter driver") Signed-off-by: Cosmin Tanislav --- drivers/counter/rz-mtu3-cnt.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/drivers/counter/rz-mtu3-cnt.c b/drivers/counter/rz-mtu3-cnt.c index e755d54dfece..a4a8ef2d88f0 100644 --- a/drivers/counter/rz-mtu3-cnt.c +++ b/drivers/counter/rz-mtu3-cnt.c @@ -499,21 +499,25 @@ static int rz_mtu3_count_enable_write(struct counter_= device *counter, struct rz_mtu3_cnt *const priv =3D counter_priv(counter); int ret =3D 0; =20 + mutex_lock(&priv->lock); + + if (priv->count_is_enabled[count->id] =3D=3D enable) + goto exit; + if (enable) { - mutex_lock(&priv->lock); pm_runtime_get_sync(ch->dev); ret =3D rz_mtu3_initialize_counter(counter, count->id); if (ret =3D=3D 0) priv->count_is_enabled[count->id] =3D true; - mutex_unlock(&priv->lock); } else { - mutex_lock(&priv->lock); rz_mtu3_terminate_counter(counter, count->id); priv->count_is_enabled[count->id] =3D false; pm_runtime_put(ch->dev); - mutex_unlock(&priv->lock); } =20 +exit: + mutex_unlock(&priv->lock); + return ret; } =20 --=20 2.52.0