From: "Niklas Söderlund" <niklas.soderlund@ragnatech.se>
To: Yaohan Li <shunsheng1218@gmail.com>
Cc: "Rafael J . Wysocki" <rafael@kernel.org>,
Daniel Lezcano <daniel.lezcano@kernel.org>,
Zhang Rui <rui.zhang@intel.com>,
Lukasz Luba <lukasz.luba@arm.com>,
Magnus Damm <magnus.damm@gmail.com>,
linux-renesas-soc@vger.kernel.org, linux-pm@vger.kernel.org,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH v3] thermal: rcar_gen3: register the interrupt handlers after the zones
Date: Tue, 24 Nov 2026 14:15:47 +0100 [thread overview]
Message-ID: <20261124131547.GI3898744@ragnatech.se> (raw)
In-Reply-To: <20261124095110.131258-1-shunsheng1218@gmail.com>
Hi Yaohan,
This is a very large patch doing lots of different things and needs to
be broken up. And I'm concerned about removing the remove() function in
favor of devm_add_action_or_reset() design.
When I suggested altering the probe order I had in mind moving the init
order of things to make sure it was cleaned up in the right order not
making it depend on devm. Maybe that is not possible, but if this is the
only solution it needs to be broke apart in discreet steps that can be
reviewed.
On 2026-11-24 17:51:10 +0800, Yaohan Li wrote:
> The interrupt handlers are requested before the thermal zones are
> registered, and devres releases resources in the reverse order. That
> leaves the same object exposed at both ends: during probe the handler
> exists while part of the sensors and zones are not initialised yet, and
> during teardown it outlives the zones it reports to. The handler walks
> every sensor and calls thermal_zone_device_update() through tsc->zone,
> which is never cleared when the zone is unregistered.
>
> Fix the order rather than the removal path. Look the interrupts up
> first, keep the source masked while the sensors are initialised, and
> register the handlers only once every zone exists. A devres action
> registered after the handlers masks the source again, and the source is
> enabled last, so the teardown order comes from devres itself: source
> off, handlers, hwmon, zones, MMIO mappings, runtime PM. ->remove() has
> nothing left to do and is gone.
>
> The thermal core copies the zone operations when a zone is registered,
> so whether set_trips() is available has to be decided before the zones
> are created. Registering the handlers last moves that decision after
> the zones exist, so when the interrupts cannot be requested the zones
> are released and registered again without set_trips(): the driver keeps
> the polling configuration it had before instead of failing the probe.
> The zones are created and dropped once more in that path only.
>
> rcar_gen3_thermal_init() clears REG_GEN3_IRQEN as well. Nothing but
> rcar_gen3_thermal_enable_irqs() writes the register again, at the end of
> the probe and from ->resume(), so a source that the firmware or an
> earlier bind left enabled does not stay on while the zones are being
> created.
>
> Check the sensor count before reading the fuses, because the fuse
> reader dereferences tscs[0]. Return -ENODEV when no sensor resources
> are present.
>
> Signed-off-by: Yaohan Li <shunsheng1218@gmail.com>
>
> ---
> Prepared against v7.4-rc1-1969-g666539eecdf4.
>
> Compile tested only: arm64, aarch64-linux-gnu-gcc 11.4,
> CONFIG_ARCH_RENESAS, CONFIG_ARCH_RCAR_GEN3 and CONFIG_RCAR_GEN3_THERMAL.
> No board was available, so probe, the polling fallback, resume and
> teardown were not exercised on hardware, and there was no way to make an
> interrupt request fail on a real device.
>
> Changes in v3:
> - Register the interrupt handlers after the thermal zones and let devres
> own the whole teardown, as suggested; ->remove() is gone.
> - Look the interrupts up before the zones are registered, because the
> thermal core copies the zone operations at registration time.
> - Split the sensor initialisation from enabling the interrupt source and
> re-enable it from ->resume().
> - Clear REG_GEN3_IRQEN in the sensor initialisation, so that the source
> stays masked until it is enabled on purpose.
> - Keep the polling fallback when the handlers cannot be requested:
> release the zones through a devres group and register them again
> without set_trips().
> - Check for a device without sensors before the fuses are read.
> - Drop the has_irqs() helper.
>
> AI assistance: prepared with Claude (Anthropic) as a coding assistant; the
> author reviewed the reasoning and takes responsibility for the content.
> ---
> drivers/thermal/renesas/rcar_gen3_thermal.c | 197 ++++++++++++++------
> 1 file changed, 145 insertions(+), 52 deletions(-)
>
> diff --git a/drivers/thermal/renesas/rcar_gen3_thermal.c b/drivers/thermal/renesas/rcar_gen3_thermal.c
> index 9c64cbc2ee67..7e4d6aa6e5dd 100644
> --- a/drivers/thermal/renesas/rcar_gen3_thermal.c
> +++ b/drivers/thermal/renesas/rcar_gen3_thermal.c
> @@ -103,6 +103,8 @@ struct rcar_gen3_thermal_tsc {
> int thcode[3];
> };
>
> +#define RCAR_GEN3_THERMAL_MAX_IRQ 2
> +
> struct rcar_gen3_thermal_priv {
> struct rcar_gen3_thermal_tsc *tscs[TSC_MAX_NUM];
> struct thermal_zone_device_ops ops;
> @@ -110,6 +112,7 @@ struct rcar_gen3_thermal_priv {
> int ptat[3];
> int tj_t;
> const struct rcar_thermal_info *info;
> + int irqs[RCAR_GEN3_THERMAL_MAX_IRQ];
> };
>
> static inline u32 rcar_gen3_thermal_read(struct rcar_gen3_thermal_tsc *tsc,
> @@ -324,8 +327,7 @@ static bool rcar_gen3_thermal_read_fuses(struct rcar_gen3_thermal_priv *priv)
> return true;
> }
>
> -static void rcar_gen3_thermal_init(struct rcar_gen3_thermal_priv *priv,
> - struct rcar_gen3_thermal_tsc *tsc)
> +static void rcar_gen3_thermal_init(struct rcar_gen3_thermal_tsc *tsc)
> {
> u32 reg_val;
>
> @@ -335,11 +337,9 @@ static void rcar_gen3_thermal_init(struct rcar_gen3_thermal_priv *priv,
>
> usleep_range(1000, 2000);
>
> + rcar_gen3_thermal_write(tsc, REG_GEN3_IRQEN, 0);
> rcar_gen3_thermal_write(tsc, REG_GEN3_IRQCTL, 0);
> rcar_gen3_thermal_write(tsc, REG_GEN3_IRQMSK, 0);
> - if (priv->ops.set_trips)
> - rcar_gen3_thermal_write(tsc, REG_GEN3_IRQEN,
> - IRQ_TEMPD1 | IRQ_TEMP2);
>
> reg_val = rcar_gen3_thermal_read(tsc, REG_GEN3_THCTR);
> reg_val |= THCTR_THSST;
> @@ -466,9 +466,9 @@ static const struct of_device_id rcar_gen3_thermal_dt_ids[] = {
> };
> MODULE_DEVICE_TABLE(of, rcar_gen3_thermal_dt_ids);
>
> -static void rcar_gen3_thermal_remove(struct platform_device *pdev)
> +static void rcar_gen3_thermal_pm_put(void *data)
> {
> - struct device *dev = &pdev->dev;
> + struct device *dev = data;
>
> pm_runtime_put(dev);
> pm_runtime_disable(dev);
> @@ -481,25 +481,66 @@ static void rcar_gen3_hwmon_action(void *data)
> thermal_remove_hwmon_sysfs(zone);
> }
>
> -static int rcar_gen3_thermal_request_irqs(struct rcar_gen3_thermal_priv *priv,
> - struct platform_device *pdev)
> +static int rcar_gen3_thermal_register_zones(struct rcar_gen3_thermal_priv *priv,
> + struct device *dev)
> {
> - struct device *dev = &pdev->dev;
> unsigned int i;
> - char *irqname;
> - int ret, irq;
> + int ret;
> +
> + for (i = 0; i < priv->num_tscs; i++) {
> + struct rcar_gen3_thermal_tsc *tsc = priv->tscs[i];
> + struct thermal_zone_device *zone;
>
> - for (i = 0; i < 2; i++) {
> + zone = devm_thermal_of_zone_register(dev, i, tsc, &priv->ops);
> + if (IS_ERR(zone)) {
> + dev_err(dev, "Sensor %u: Can't register thermal zone\n", i);
> + return PTR_ERR(zone);
> + }
> + tsc->zone = zone;
> +
> + ret = thermal_add_hwmon_sysfs(zone);
> + if (ret)
> + return ret;
> +
> + ret = devm_add_action_or_reset(dev, rcar_gen3_hwmon_action, zone);
> + if (ret)
> + return ret;
> + }
> +
> + return 0;
> +}
> +
> +static int rcar_gen3_thermal_get_irqs(struct rcar_gen3_thermal_priv *priv,
> + struct platform_device *pdev)
> +{
> + unsigned int i;
> + int irq;
> +
> + for (i = 0; i < RCAR_GEN3_THERMAL_MAX_IRQ; i++) {
> irq = platform_get_irq_optional(pdev, i);
> if (irq < 0)
> return irq;
>
> + priv->irqs[i] = irq;
> + }
> +
> + return 0;
> +}
> +
> +static int rcar_gen3_thermal_request_irqs(struct rcar_gen3_thermal_priv *priv,
> + struct device *dev)
> +{
> + unsigned int i;
> + char *irqname;
> + int ret;
> +
> + for (i = 0; i < RCAR_GEN3_THERMAL_MAX_IRQ; i++) {
> irqname = devm_kasprintf(dev, GFP_KERNEL, "%s:ch%d",
> dev_name(dev), i);
> if (!irqname)
> return -ENOMEM;
>
> - ret = devm_request_threaded_irq(dev, irq, NULL,
> + ret = devm_request_threaded_irq(dev, priv->irqs[i], NULL,
> rcar_gen3_thermal_irq,
> IRQF_ONESHOT, irqname, priv);
> if (ret)
> @@ -509,14 +550,34 @@ static int rcar_gen3_thermal_request_irqs(struct rcar_gen3_thermal_priv *priv,
> return 0;
> }
>
> +static void rcar_gen3_thermal_enable_irqs(struct rcar_gen3_thermal_priv *priv)
> +{
> + unsigned int i;
> +
> + for (i = 0; i < priv->num_tscs; i++)
> + rcar_gen3_thermal_write(priv->tscs[i], REG_GEN3_IRQEN,
> + IRQ_TEMPD1 | IRQ_TEMP2);
> +}
> +
> +static void rcar_gen3_thermal_disable_irqs(void *data)
> +{
> + struct rcar_gen3_thermal_priv *priv = data;
> + unsigned int i;
> +
> + for (i = 0; i < priv->num_tscs; i++) {
> + rcar_gen3_thermal_write(priv->tscs[i], REG_GEN3_IRQEN, 0);
> + rcar_gen3_thermal_write(priv->tscs[i], REG_GEN3_IRQMSK, 0);
> + }
> +}
> +
> static int rcar_gen3_thermal_probe(struct platform_device *pdev)
> {
> struct rcar_gen3_thermal_priv *priv;
> struct device *dev = &pdev->dev;
> struct resource *res;
> - struct thermal_zone_device *zone;
> unsigned int i;
> int ret;
> + void *group;
>
> priv = devm_kzalloc_obj(dev, *priv);
> if (!priv)
> @@ -525,14 +586,25 @@ static int rcar_gen3_thermal_probe(struct platform_device *pdev)
> priv->ops = rcar_gen3_tz_of_ops;
>
> priv->info = of_device_get_match_data(dev);
> +
> platform_set_drvdata(pdev, priv);
>
> - if (rcar_gen3_thermal_request_irqs(priv, pdev))
> + /*
> + * The interrupts are looked up here but their handlers are registered
> + * only once the thermal zones exist. The thermal core copies the zone
> + * operations when a zone is registered, so whether set_trips() is
> + * available has to be decided before the zones are created.
> + */
> + if (rcar_gen3_thermal_get_irqs(priv, pdev))
> priv->ops.set_trips = NULL;
>
> pm_runtime_enable(dev);
> pm_runtime_get_sync(dev);
>
> + ret = devm_add_action_or_reset(dev, rcar_gen3_thermal_pm_put, dev);
> + if (ret)
> + return ret;
> +
> for (i = 0; i < TSC_MAX_NUM; i++) {
> struct rcar_gen3_thermal_tsc *tsc;
>
> @@ -541,22 +613,20 @@ static int rcar_gen3_thermal_probe(struct platform_device *pdev)
> break;
>
> tsc = devm_kzalloc_obj(dev, *tsc);
> - if (!tsc) {
> - ret = -ENOMEM;
> - goto error_unregister;
> - }
> + if (!tsc)
> + return -ENOMEM;
>
> tsc->priv = priv;
> tsc->base = devm_ioremap_resource(dev, res);
> - if (IS_ERR(tsc->base)) {
> - ret = PTR_ERR(tsc->base);
> - goto error_unregister;
> - }
> + if (IS_ERR(tsc->base))
> + return PTR_ERR(tsc->base);
>
> priv->tscs[i] = tsc;
> }
>
> priv->num_tscs = i;
> + if (!priv->num_tscs)
> + return -ENODEV;
>
> if (!rcar_gen3_thermal_read_fuses(priv))
> dev_info(dev, "No calibration values fused, fallback to driver values\n");
> @@ -564,40 +634,64 @@ static int rcar_gen3_thermal_probe(struct platform_device *pdev)
> rcar_gen3_thermal_shared_coefs(priv);
>
> for (i = 0; i < priv->num_tscs; i++) {
> - struct rcar_gen3_thermal_tsc *tsc = priv->tscs[i];
> + rcar_gen3_thermal_init(priv->tscs[i]);
> + rcar_gen3_thermal_tsc_coefs(priv, priv->tscs[i]);
> + }
>
> - rcar_gen3_thermal_init(priv, tsc);
> - rcar_gen3_thermal_tsc_coefs(priv, tsc);
> +register_zones:
> + group = devres_open_group(dev, NULL, GFP_KERNEL);
> + if (!group)
> + return -ENOMEM;
>
> - zone = devm_thermal_of_zone_register(dev, i, tsc, &priv->ops);
> - if (IS_ERR(zone)) {
> - dev_err(dev, "Sensor %u: Can't register thermal zone\n", i);
> - ret = PTR_ERR(zone);
> - goto error_unregister;
> + ret = rcar_gen3_thermal_register_zones(priv, dev);
> + if (ret)
> + goto release_group;
> +
> + /*
> + * Handlers last and the source enabled after them, so devres runs the
> + * teardown in the opposite order: source off, handlers, hwmon, zones,
> + * MMIO mappings, runtime PM.
> + */
> + if (priv->ops.set_trips) {
> + ret = rcar_gen3_thermal_request_irqs(priv, dev);
> + if (ret) {
> + dev_info(dev, "Failed to request interrupts (%d), polling instead\n",
> + ret);
> +
> + /*
> + * The thermal core copies the zone operations when a
> + * zone is registered, so the zones above cannot be
> + * switched to polling afterwards. Release them and
> + * register them again without set_trips().
> + */
> + devres_release_group(dev, group);
> +
> + for (i = 0; i < priv->num_tscs; i++)
> + priv->tscs[i]->zone = NULL;
> +
> + rcar_gen3_thermal_disable_irqs(priv);
> + priv->ops.set_trips = NULL;
> + goto register_zones;
> }
> - tsc->zone = zone;
>
> - ret = thermal_add_hwmon_sysfs(tsc->zone);
> + ret = devm_add_action_or_reset(dev,
> + rcar_gen3_thermal_disable_irqs,
> + priv);
> if (ret)
> - goto error_unregister;
> + goto release_group;
>
> - ret = devm_add_action_or_reset(dev, rcar_gen3_hwmon_action, zone);
> - if (ret)
> - goto error_unregister;
> -
> - dev_info(dev, "Sensor %u: Loaded\n", i);
> + rcar_gen3_thermal_enable_irqs(priv);
> }
>
> - if (!priv->num_tscs) {
> - ret = -ENODEV;
> - goto error_unregister;
> - }
> + devres_remove_group(dev, group);
>
> - return 0;
> + for (i = 0; i < priv->num_tscs; i++)
> + dev_info(dev, "Sensor %u: Loaded\n", i);
>
> -error_unregister:
> - rcar_gen3_thermal_remove(pdev);
> + return 0;
>
> +release_group:
> + devres_release_group(dev, group);
> return ret;
> }
>
> @@ -606,11 +700,11 @@ static int rcar_gen3_thermal_resume(struct device *dev)
> struct rcar_gen3_thermal_priv *priv = dev_get_drvdata(dev);
> unsigned int i;
>
> - for (i = 0; i < priv->num_tscs; i++) {
> - struct rcar_gen3_thermal_tsc *tsc = priv->tscs[i];
> + for (i = 0; i < priv->num_tscs; i++)
> + rcar_gen3_thermal_init(priv->tscs[i]);
>
> - rcar_gen3_thermal_init(priv, tsc);
> - }
> + if (priv->ops.set_trips)
> + rcar_gen3_thermal_enable_irqs(priv);
>
> return 0;
> }
> @@ -625,7 +719,6 @@ static struct platform_driver rcar_gen3_thermal_driver = {
> .of_match_table = rcar_gen3_thermal_dt_ids,
> },
> .probe = rcar_gen3_thermal_probe,
> - .remove = rcar_gen3_thermal_remove,
> };
> module_platform_driver(rcar_gen3_thermal_driver);
>
> --
> 2.34.1
>
--
Kind Regards,
Niklas Söderlund
prev parent reply other threads:[~2026-11-24 13:15 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-17 14:53 [PATCH] thermal: rcar_gen3: quiesce the interrupt source before releasing it Yaohan Li
2026-11-18 9:31 ` Niklas Söderlund
2026-11-19 2:29 ` [PATCH v2] thermal: rcar_gen3: release the IRQ handlers before the zones are freed Yaohan Li
2026-11-22 14:48 ` Niklas Söderlund
2026-11-23 2:21 ` Yaohan Li
2026-11-23 6:47 ` Niklas Söderlund
2026-11-24 2:22 ` Yaohan Li
2026-11-24 6:40 ` Niklas Söderlund
2026-11-24 9:51 ` [PATCH v3] thermal: rcar_gen3: register the interrupt handlers after the zones Yaohan Li
2026-11-24 13:15 ` Niklas Söderlund [this message]
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=20261124131547.GI3898744@ragnatech.se \
--to=niklas.soderlund@ragnatech.se \
--cc=daniel.lezcano@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-pm@vger.kernel.org \
--cc=linux-renesas-soc@vger.kernel.org \
--cc=lukasz.luba@arm.com \
--cc=magnus.damm@gmail.com \
--cc=rafael@kernel.org \
--cc=rui.zhang@intel.com \
--cc=shunsheng1218@gmail.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
all inboxes | Powered by JetHome®