From: "Uwe Kleine-König" <u.kleine-koenig@pengutronix.de>
To: Saravana Kannan <saravanak@google.com>
Cc: linux-pwm@vger.kernel.org, Jonathan Corbet <corbet@lwn.net>,
Thierry Reding <thierry.reding@gmail.com>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
linux-doc@vger.kernel.org, Wolfram Sang <wsa@kernel.org>,
Mark Brown <broonie@kernel.org>,
James Clark <james.clark@arm.com>,
kernel@pengutronix.de, Yang Yingliang <yangyingliang@huawei.com>,
Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
Matti Vaittinen <mazziesaccount@gmail.com>
Subject: Re: [PATCH 01/18] pwm: Provide devm_pwmchip_alloc() function
Date: Tue, 10 Oct 2023 10:05:08 +0200 [thread overview]
Message-ID: <20231010080508.7ssnroaefyaeeedd@pengutronix.de> (raw)
In-Reply-To: <20230725211004.peqxxb4y3j62gmnp@pengutronix.de>
[-- Attachment #1: Type: text/plain, Size: 4660 bytes --]
Hello Saravana,
you were pointed out to me as the expert for device links. I found a
problem with these.
On Tue, Jul 25, 2023 at 11:10:04PM +0200, Uwe Kleine-König wrote:
> Today I managed to trigger the problem I intend to address with this
> series. My machine to test this on is an stm32mp157. To be able to
> trigger the problem reliably I applied the following patches on top of
> v6.5-rc1:
>
> - pwm: stm32: Don't modify HW state in .remove() callback
> This is a cleanup that I already sent out.
> https://lore.kernel.org/r/20230713155142.2454010-2-u.kleine-koenig@pengutronix.de
> The purpose for reproducing the problem is to not trigger further
> calls to the apply callback.
>
> - The following patch:
>
> diff --git a/drivers/pwm/pwm-stm32.c b/drivers/pwm/pwm-stm32.c
> index 687967d3265f..c7fc02b0fa3c 100644
> --- a/drivers/pwm/pwm-stm32.c
> +++ b/drivers/pwm/pwm-stm32.c
> @@ -451,6 +451,10 @@ static int stm32_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
> struct stm32_pwm *priv = to_stm32_pwm_dev(chip);
> int ret;
>
> + dev_info(chip->dev, "%s:%d\n", __func__, __LINE__);
> + msleep(5000);
> + dev_info(chip->dev, "%s:%d\n", __func__, __LINE__);
> +
> enabled = pwm->state.enabled;
>
> if (enabled && !state->enabled) {
> @@ -650,7 +654,11 @@ static void stm32_pwm_remove(struct platform_device *pdev)
> {
> struct stm32_pwm *priv = platform_get_drvdata(pdev);
>
> + dev_info(&pdev->dev, "%s:%d\n", __func__, __LINE__);
> pwmchip_remove(&priv->chip);
> + dev_info(&pdev->dev, "%s:%d\n", __func__, __LINE__);
> +
> + priv->regmap = NULL;
> }
>
> static int __maybe_unused stm32_pwm_suspend(struct device *dev)
>
> The first hunk is only there to widen the race window. The second is to
> give some diagnostics and make stm32_pwm_apply() crash if it continues
> to run after the msleep. (Without it it didn't crash reproducibly, don't
> understand why. *shrug*)
>
> The device tree contains a pwm-fan device making use of one of the PWMs.
>
> Now I do the following:
>
> echo fan > /sys/bus/platform/drivers/pwm-fan/unbind & sleep 1; echo 40007000.timer:pwm > /sys/bus/platform/drivers/stm32-pwm/unbind
>
> Unbinding the fan device has two effects:
>
> - The device link between fan and pwm looses its property to unbind fan
> when pwm gets unbound.
> (Its .status changes from DL_STATE_ACTIVE to DL_STATE_AVAILABLE)
> - It calls pwm_fan_cleanup() which triggers a call to
> pwm_apply_state().
>
> So when the pwm device gets unbound the first thread is sleeping in
> stm32_pwm_apply(). The driver calls pwmchip_remove() and sets
> priv->regmap to NULL. Then a few seconds later the first thread wakes up
> in stm32_pwm_apply() with the chip freed and priv->regmap = NULL. Bang!
>
> This looks as follows:
>
> root@crown:~# echo fan > /sys/bus/platform/drivers/pwm-fan/unbind & sleep 1; echo 40007000.timer:pwm > /sys/bus/platform/drivers/stm32-pwm/unbind
> [ 187.182113] stm32-pwm 40007000.timer:pwm: stm32_pwm_apply:454
> [ 188.164769] stm32-pwm 40007000.timer:pwm: stm32_pwm_remove:657
> [ 188.184555] stm32-pwm 40007000.timer:pwm: stm32_pwm_remove:659
> root@crown:~# [ 192.236423] platform 40007000.timer:pwm: stm32_pwm_apply:456
> [ 192.240727] 8<--- cut here ---
> [ 192.243759] Unable to handle kernel NULL pointer dereference at virtual address 0000001c when read
> ...
>
> Even without the crash you can see that stm32_pwm_apply() is still
> running after pwmchip_remove() completed.
>
> I'm unsure if the device link could be improved here to ensure that the
> fan is completely unbound even if it started unbinding already before
> the pwm device gets unbound. (And if it could, would this fit the device
> links purpose and so be a sensible improvement?)
While I think that there is something to be done in the pwm core that
this doesn't explode (i.e. do proper lifetime tracking such that a
pwm_chip doesn't disappear while still being used---and I'm working on
that) I expected that the device links between pwm consumer and provider
would prevent the above described oops, too. But somehow the fan already
going away (but still using the PWM) when the PWM is unbound, results in
the PWM disappearing before the fan is completely gone.
Is this expected, or a problem that can (and should?) be fixed?
If you need more context or a tester, don't hesitate to ask.
Best regards
Uwe
--
Pengutronix e.K. | Uwe Kleine-König |
Industrial Linux Solutions | https://www.pengutronix.de/ |
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]
next prev parent reply other threads:[~2023-10-10 8:05 UTC|newest]
Thread overview: 40+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-07-18 18:18 [PATCH 00/18] pwm: Provide devm_pwmchip_alloc() function Uwe Kleine-König
2023-07-18 18:18 ` [PATCH 01/18] " Uwe Kleine-König
2023-07-18 20:05 ` Andy Shevchenko
2023-07-19 10:31 ` Uwe Kleine-König
2023-07-19 7:59 ` Thierry Reding
2023-07-19 8:57 ` Uwe Kleine-König
2023-07-25 21:10 ` Uwe Kleine-König
2023-10-10 8:05 ` Uwe Kleine-König [this message]
2023-10-13 21:42 ` Saravana Kannan
2023-10-14 16:17 ` Uwe Kleine-König
2023-10-17 23:35 ` Saravana Kannan
2023-10-18 1:42 ` Saravana Kannan
2023-10-18 11:17 ` Uwe Kleine-König
2023-10-18 21:01 ` Saravana Kannan
2023-10-18 21:20 ` Uwe Kleine-König
2023-10-18 22:25 ` Saravana Kannan
2023-07-18 18:18 ` [PATCH 02/18] pwm: ab8500: Make use of " Uwe Kleine-König
2023-07-18 18:18 ` [PATCH 03/18] pwm: apple: " Uwe Kleine-König
2023-07-18 18:18 ` [PATCH 04/18] pwm: berlin: " Uwe Kleine-König
2023-07-18 18:18 ` [PATCH 05/18] pwm: clk: " Uwe Kleine-König
2023-07-18 18:18 ` [PATCH 06/18] pwm: fsl-ftm: " Uwe Kleine-König
2023-07-18 18:18 ` [PATCH 07/18] pwm: hibvt: " Uwe Kleine-König
2023-07-18 18:18 ` [PATCH 08/18] pwm: imx1: " Uwe Kleine-König
2023-07-18 18:18 ` [PATCH 09/18] pwm: imx27: " Uwe Kleine-König
2023-07-18 18:18 ` [PATCH 10/18] pwm: jz4740: " Uwe Kleine-König
2023-07-18 18:18 ` [PATCH 11/18] pwm: keembay: " Uwe Kleine-König
2023-07-18 18:18 ` [PATCH 12/18] pwm: lpc18xx-sct: " Uwe Kleine-König
2023-07-18 18:18 ` [PATCH 13/18] pwm: lpc32xx: " Uwe Kleine-König
2023-07-18 18:18 ` [PATCH 14/18] pwm: mxs: " Uwe Kleine-König
2023-07-18 18:18 ` [PATCH 15/18] pwm: ntxec: " Uwe Kleine-König
2023-07-18 18:18 ` [PATCH 16/18] pwm: pxa: " Uwe Kleine-König
2023-07-18 18:18 ` [PATCH 17/18] pwm: stm32: " Uwe Kleine-König
2023-07-18 18:18 ` [PATCH 18/18] gpio: mvebu: " Uwe Kleine-König
2023-07-29 14:09 ` Bartosz Golaszewski
2023-07-29 21:37 ` Uwe Kleine-König
2023-07-30 10:07 ` Bartosz Golaszewski
2023-07-30 14:09 ` Uwe Kleine-König
2023-08-03 9:42 ` Uwe Kleine-König
2023-08-03 11:51 ` Andy Shevchenko
2023-08-03 15:34 ` Uwe Kleine-König
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20231010080508.7ssnroaefyaeeedd@pengutronix.de \
--to=u.kleine-koenig@pengutronix.de \
--cc=andriy.shevchenko@linux.intel.com \
--cc=broonie@kernel.org \
--cc=corbet@lwn.net \
--cc=gregkh@linuxfoundation.org \
--cc=james.clark@arm.com \
--cc=kernel@pengutronix.de \
--cc=linux-doc@vger.kernel.org \
--cc=linux-pwm@vger.kernel.org \
--cc=mazziesaccount@gmail.com \
--cc=saravanak@google.com \
--cc=thierry.reding@gmail.com \
--cc=wsa@kernel.org \
--cc=yangyingliang@huawei.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox