[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20230228101558.b4dosk54jojfqkgi@pengutronix.de>
Date: Tue, 28 Feb 2023 11:15:58 +0100
From: Uwe Kleine-König <u.kleine-koenig@...gutronix.de>
To: Munehisa Kamata <kamatam@...zon.com>
Cc: thierry.reding@...il.com, tobetter@...il.com,
linux-pwm@...r.kernel.org, linux-kernel@...r.kernel.org,
stable@...r.kernel.org, kernel@...gutronix.de
Subject: [PATCH] pwm: Zero-initialize the pwm_state passed to driver's
.get_state()
This is just to ensure that .usage_power is properly initialized and
doesn't contain random stack data. The other members of struct pwm_state
should get a value assigned in a successful call to .get_state(). So in
the absence of bugs in driver implementations, this is only a safe-guard
and no fix.
Reported-by: Munehisa Kamata <kamatam@...zon.com>
Signed-off-by: Uwe Kleine-König <u.kleine-koenig@...gutronix.de>
---
drivers/pwm/core.c | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)
Hello,
On Sat, Feb 25, 2023 at 05:37:21PM -0800, Munehisa Kamata wrote:
> Zero-initialize the on-stack structure to avoid unexpected behaviors. Some
> drivers may not set or initialize all the values in pwm_state through their
> .get_state() callback and therefore some random values may remain there and
> be set into pwm->state eventually.
>
> This actually caused regression on ODROID-N2+ as reported in [1]; kernel
> fails to boot due to random panic or hang-up.
>
> [1] https://forum.odroid.com/viewtopic.php?f=177&t=46360
>
> Fixes: c73a3107624d ("pwm: Handle .get_state() failures")
> Cc: stable@...r.kernel.org # 6.2
> Signed-off-by: Munehisa Kamata <kamatam@...zon.com>
My patch is essentially the same as Munehisa's, just written a bit
differently (to maybe make it easier for the compiler to optimize it?)
and with an explaining comment. The actual motivation is different so
the commit log is considerably different, too.
I was unsure how to honor Munehisa's effort, I went with a
"Reported-by". Please tell me if you want this to be different.
Best regards
Uwe
diff --git a/drivers/pwm/core.c b/drivers/pwm/core.c
index e01147f66e15..533ef5bd3add 100644
--- a/drivers/pwm/core.c
+++ b/drivers/pwm/core.c
@@ -115,7 +115,14 @@ static int pwm_device_request(struct pwm_device *pwm, const char *label)
}
if (pwm->chip->ops->get_state) {
- struct pwm_state state;
+ /*
+ * Zero-initialize state because most drivers are unaware of
+ * .usage_power. The other members of state are supposed to be
+ * set by lowlevel drivers. We still initialize the whole
+ * structure for simplicity even though this might paper over
+ * faulty implementations of .get_state().
+ */
+ struct pwm_state state = { 0, };
err = pwm->chip->ops->get_state(pwm->chip, pwm, &state);
trace_pwm_get(pwm, &state, err);
--
2.39.1
--
Pengutronix e.K. | Uwe Kleine-König |
Industrial Linux Solutions | https://www.pengutronix.de/ |
Download attachment "signature.asc" of type "application/pgp-signature" (489 bytes)
Powered by blists - more mailing lists